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_ONLINE_PERCENT
, /* value is % of online CPUs */
87 ZTI_MODE_BATCH
, /* cpu-intensive; value is ignored */
88 ZTI_MODE_NULL
, /* don't create a taskq */
92 #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) }
93 #define ZTI_PCT(n) { ZTI_MODE_ONLINE_PERCENT, (n), 1 }
94 #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 }
95 #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 }
97 #define ZTI_N(n) ZTI_P(n, 1)
98 #define ZTI_ONE ZTI_N(1)
100 typedef struct zio_taskq_info
{
101 zti_modes_t zti_mode
;
106 static const char *const zio_taskq_types
[ZIO_TASKQ_TYPES
] = {
107 "iss", "iss_h", "int", "int_h"
111 * This table defines the taskq settings for each ZFS I/O type. When
112 * initializing a pool, we use this table to create an appropriately sized
113 * taskq. Some operations are low volume and therefore have a small, static
114 * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
115 * macros. Other operations process a large amount of data; the ZTI_BATCH
116 * macro causes us to create a taskq oriented for throughput. Some operations
117 * are so high frequency and short-lived that the taskq itself can become a a
118 * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
119 * additional degree of parallelism specified by the number of threads per-
120 * taskq and the number of taskqs; when dispatching an event in this case, the
121 * particular taskq is chosen at random.
123 * The different taskq priorities are to handle the different contexts (issue
124 * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that
125 * need to be handled with minimum delay.
127 const zio_taskq_info_t zio_taskqs
[ZIO_TYPES
][ZIO_TASKQ_TYPES
] = {
128 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
129 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* NULL */
130 { ZTI_N(8), ZTI_NULL
, ZTI_BATCH
, ZTI_NULL
}, /* READ */
131 { ZTI_BATCH
, ZTI_N(5), ZTI_N(16), ZTI_N(5) }, /* WRITE */
132 { ZTI_P(4, 8), ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* FREE */
133 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* CLAIM */
134 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* IOCTL */
137 static void spa_sync_version(void *arg
, dmu_tx_t
*tx
);
138 static void spa_sync_props(void *arg
, dmu_tx_t
*tx
);
139 static boolean_t
spa_has_active_shared_spare(spa_t
*spa
);
140 static inline int spa_load_impl(spa_t
*spa
, uint64_t, nvlist_t
*config
,
141 spa_load_state_t state
, spa_import_type_t type
, boolean_t mosconfig
,
143 static void spa_vdev_resilver_done(spa_t
*spa
);
145 uint_t zio_taskq_batch_pct
= 100; /* 1 thread per cpu in pset */
146 id_t zio_taskq_psrset_bind
= PS_NONE
;
147 boolean_t zio_taskq_sysdc
= B_TRUE
; /* use SDC scheduling class */
148 uint_t zio_taskq_basedc
= 80; /* base duty cycle */
150 boolean_t spa_create_process
= B_TRUE
; /* no process ==> no sysdc */
153 * This (illegal) pool name is used when temporarily importing a spa_t in order
154 * to get the vdev stats associated with the imported devices.
156 #define TRYIMPORT_NAME "$import"
159 * ==========================================================================
160 * SPA properties routines
161 * ==========================================================================
165 * Add a (source=src, propname=propval) list to an nvlist.
168 spa_prop_add_list(nvlist_t
*nvl
, zpool_prop_t prop
, char *strval
,
169 uint64_t intval
, zprop_source_t src
)
171 const char *propname
= zpool_prop_to_name(prop
);
174 VERIFY(nvlist_alloc(&propval
, NV_UNIQUE_NAME
, KM_PUSHPAGE
) == 0);
175 VERIFY(nvlist_add_uint64(propval
, ZPROP_SOURCE
, src
) == 0);
178 VERIFY(nvlist_add_string(propval
, ZPROP_VALUE
, strval
) == 0);
180 VERIFY(nvlist_add_uint64(propval
, ZPROP_VALUE
, intval
) == 0);
182 VERIFY(nvlist_add_nvlist(nvl
, propname
, propval
) == 0);
183 nvlist_free(propval
);
187 * Get property values from the spa configuration.
190 spa_prop_get_config(spa_t
*spa
, nvlist_t
**nvp
)
192 vdev_t
*rvd
= spa
->spa_root_vdev
;
193 dsl_pool_t
*pool
= spa
->spa_dsl_pool
;
197 uint64_t cap
, version
;
198 zprop_source_t src
= ZPROP_SRC_NONE
;
199 spa_config_dirent_t
*dp
;
202 ASSERT(MUTEX_HELD(&spa
->spa_props_lock
));
205 alloc
= metaslab_class_get_alloc(spa_normal_class(spa
));
206 size
= metaslab_class_get_space(spa_normal_class(spa
));
207 spa_prop_add_list(*nvp
, ZPOOL_PROP_NAME
, spa_name(spa
), 0, src
);
208 spa_prop_add_list(*nvp
, ZPOOL_PROP_SIZE
, NULL
, size
, src
);
209 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALLOCATED
, NULL
, alloc
, src
);
210 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREE
, NULL
,
214 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
215 vdev_t
*tvd
= rvd
->vdev_child
[c
];
216 space
+= tvd
->vdev_max_asize
- tvd
->vdev_asize
;
218 spa_prop_add_list(*nvp
, ZPOOL_PROP_EXPANDSZ
, NULL
, space
,
221 spa_prop_add_list(*nvp
, ZPOOL_PROP_READONLY
, NULL
,
222 (spa_mode(spa
) == FREAD
), src
);
224 cap
= (size
== 0) ? 0 : (alloc
* 100 / size
);
225 spa_prop_add_list(*nvp
, ZPOOL_PROP_CAPACITY
, NULL
, cap
, src
);
227 spa_prop_add_list(*nvp
, ZPOOL_PROP_DEDUPRATIO
, NULL
,
228 ddt_get_pool_dedup_ratio(spa
), src
);
230 spa_prop_add_list(*nvp
, ZPOOL_PROP_HEALTH
, NULL
,
231 rvd
->vdev_state
, src
);
233 version
= spa_version(spa
);
234 if (version
== zpool_prop_default_numeric(ZPOOL_PROP_VERSION
))
235 src
= ZPROP_SRC_DEFAULT
;
237 src
= ZPROP_SRC_LOCAL
;
238 spa_prop_add_list(*nvp
, ZPOOL_PROP_VERSION
, NULL
, version
, src
);
242 dsl_dir_t
*freedir
= pool
->dp_free_dir
;
245 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
246 * when opening pools before this version freedir will be NULL.
248 if (freedir
!= NULL
) {
249 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
, NULL
,
250 freedir
->dd_phys
->dd_used_bytes
, src
);
252 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
,
257 spa_prop_add_list(*nvp
, ZPOOL_PROP_GUID
, NULL
, spa_guid(spa
), src
);
259 if (spa
->spa_comment
!= NULL
) {
260 spa_prop_add_list(*nvp
, ZPOOL_PROP_COMMENT
, spa
->spa_comment
,
264 if (spa
->spa_root
!= NULL
)
265 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALTROOT
, spa
->spa_root
,
268 if ((dp
= list_head(&spa
->spa_config_list
)) != NULL
) {
269 if (dp
->scd_path
== NULL
) {
270 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
271 "none", 0, ZPROP_SRC_LOCAL
);
272 } else if (strcmp(dp
->scd_path
, spa_config_path
) != 0) {
273 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
274 dp
->scd_path
, 0, ZPROP_SRC_LOCAL
);
280 * Get zpool property values.
283 spa_prop_get(spa_t
*spa
, nvlist_t
**nvp
)
285 objset_t
*mos
= spa
->spa_meta_objset
;
290 err
= nvlist_alloc(nvp
, NV_UNIQUE_NAME
, KM_PUSHPAGE
);
294 mutex_enter(&spa
->spa_props_lock
);
297 * Get properties from the spa config.
299 spa_prop_get_config(spa
, nvp
);
301 /* If no pool property object, no more prop to get. */
302 if (mos
== NULL
|| spa
->spa_pool_props_object
== 0) {
303 mutex_exit(&spa
->spa_props_lock
);
308 * Get properties from the MOS pool property object.
310 for (zap_cursor_init(&zc
, mos
, spa
->spa_pool_props_object
);
311 (err
= zap_cursor_retrieve(&zc
, &za
)) == 0;
312 zap_cursor_advance(&zc
)) {
315 zprop_source_t src
= ZPROP_SRC_DEFAULT
;
318 if ((prop
= zpool_name_to_prop(za
.za_name
)) == ZPROP_INVAL
)
321 switch (za
.za_integer_length
) {
323 /* integer property */
324 if (za
.za_first_integer
!=
325 zpool_prop_default_numeric(prop
))
326 src
= ZPROP_SRC_LOCAL
;
328 if (prop
== ZPOOL_PROP_BOOTFS
) {
330 dsl_dataset_t
*ds
= NULL
;
332 dp
= spa_get_dsl(spa
);
333 dsl_pool_config_enter(dp
, FTAG
);
334 if ((err
= dsl_dataset_hold_obj(dp
,
335 za
.za_first_integer
, FTAG
, &ds
))) {
336 dsl_pool_config_exit(dp
, FTAG
);
341 MAXNAMELEN
+ strlen(MOS_DIR_NAME
) + 1,
343 dsl_dataset_name(ds
, strval
);
344 dsl_dataset_rele(ds
, FTAG
);
345 dsl_pool_config_exit(dp
, FTAG
);
348 intval
= za
.za_first_integer
;
351 spa_prop_add_list(*nvp
, prop
, strval
, intval
, src
);
355 MAXNAMELEN
+ strlen(MOS_DIR_NAME
) + 1);
360 /* string property */
361 strval
= kmem_alloc(za
.za_num_integers
, KM_PUSHPAGE
);
362 err
= zap_lookup(mos
, spa
->spa_pool_props_object
,
363 za
.za_name
, 1, za
.za_num_integers
, strval
);
365 kmem_free(strval
, za
.za_num_integers
);
368 spa_prop_add_list(*nvp
, prop
, strval
, 0, src
);
369 kmem_free(strval
, za
.za_num_integers
);
376 zap_cursor_fini(&zc
);
377 mutex_exit(&spa
->spa_props_lock
);
379 if (err
&& err
!= ENOENT
) {
389 * Validate the given pool properties nvlist and modify the list
390 * for the property values to be set.
393 spa_prop_validate(spa_t
*spa
, nvlist_t
*props
)
396 int error
= 0, reset_bootfs
= 0;
398 boolean_t has_feature
= B_FALSE
;
401 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
403 char *strval
, *slash
, *check
, *fname
;
404 const char *propname
= nvpair_name(elem
);
405 zpool_prop_t prop
= zpool_name_to_prop(propname
);
409 if (!zpool_prop_feature(propname
)) {
410 error
= SET_ERROR(EINVAL
);
415 * Sanitize the input.
417 if (nvpair_type(elem
) != DATA_TYPE_UINT64
) {
418 error
= SET_ERROR(EINVAL
);
422 if (nvpair_value_uint64(elem
, &intval
) != 0) {
423 error
= SET_ERROR(EINVAL
);
428 error
= SET_ERROR(EINVAL
);
432 fname
= strchr(propname
, '@') + 1;
433 if (zfeature_lookup_name(fname
, NULL
) != 0) {
434 error
= SET_ERROR(EINVAL
);
438 has_feature
= B_TRUE
;
441 case ZPOOL_PROP_VERSION
:
442 error
= nvpair_value_uint64(elem
, &intval
);
444 (intval
< spa_version(spa
) ||
445 intval
> SPA_VERSION_BEFORE_FEATURES
||
447 error
= SET_ERROR(EINVAL
);
450 case ZPOOL_PROP_DELEGATION
:
451 case ZPOOL_PROP_AUTOREPLACE
:
452 case ZPOOL_PROP_LISTSNAPS
:
453 case ZPOOL_PROP_AUTOEXPAND
:
454 error
= nvpair_value_uint64(elem
, &intval
);
455 if (!error
&& intval
> 1)
456 error
= SET_ERROR(EINVAL
);
459 case ZPOOL_PROP_BOOTFS
:
461 * If the pool version is less than SPA_VERSION_BOOTFS,
462 * or the pool is still being created (version == 0),
463 * the bootfs property cannot be set.
465 if (spa_version(spa
) < SPA_VERSION_BOOTFS
) {
466 error
= SET_ERROR(ENOTSUP
);
471 * Make sure the vdev config is bootable
473 if (!vdev_is_bootable(spa
->spa_root_vdev
)) {
474 error
= SET_ERROR(ENOTSUP
);
480 error
= nvpair_value_string(elem
, &strval
);
486 if (strval
== NULL
|| strval
[0] == '\0') {
487 objnum
= zpool_prop_default_numeric(
492 if ((error
= dmu_objset_hold(strval
,FTAG
,&os
)))
495 /* Must be ZPL and not gzip compressed. */
497 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
498 error
= SET_ERROR(ENOTSUP
);
500 dsl_prop_get_int_ds(dmu_objset_ds(os
),
501 zfs_prop_to_name(ZFS_PROP_COMPRESSION
),
503 !BOOTFS_COMPRESS_VALID(compress
)) {
504 error
= SET_ERROR(ENOTSUP
);
506 objnum
= dmu_objset_id(os
);
508 dmu_objset_rele(os
, FTAG
);
512 case ZPOOL_PROP_FAILUREMODE
:
513 error
= nvpair_value_uint64(elem
, &intval
);
514 if (!error
&& (intval
< ZIO_FAILURE_MODE_WAIT
||
515 intval
> ZIO_FAILURE_MODE_PANIC
))
516 error
= SET_ERROR(EINVAL
);
519 * This is a special case which only occurs when
520 * the pool has completely failed. This allows
521 * the user to change the in-core failmode property
522 * without syncing it out to disk (I/Os might
523 * currently be blocked). We do this by returning
524 * EIO to the caller (spa_prop_set) to trick it
525 * into thinking we encountered a property validation
528 if (!error
&& spa_suspended(spa
)) {
529 spa
->spa_failmode
= intval
;
530 error
= SET_ERROR(EIO
);
534 case ZPOOL_PROP_CACHEFILE
:
535 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
538 if (strval
[0] == '\0')
541 if (strcmp(strval
, "none") == 0)
544 if (strval
[0] != '/') {
545 error
= SET_ERROR(EINVAL
);
549 slash
= strrchr(strval
, '/');
550 ASSERT(slash
!= NULL
);
552 if (slash
[1] == '\0' || strcmp(slash
, "/.") == 0 ||
553 strcmp(slash
, "/..") == 0)
554 error
= SET_ERROR(EINVAL
);
557 case ZPOOL_PROP_COMMENT
:
558 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
560 for (check
= strval
; *check
!= '\0'; check
++) {
561 if (!isprint(*check
)) {
562 error
= SET_ERROR(EINVAL
);
567 if (strlen(strval
) > ZPROP_MAX_COMMENT
)
568 error
= SET_ERROR(E2BIG
);
571 case ZPOOL_PROP_DEDUPDITTO
:
572 if (spa_version(spa
) < SPA_VERSION_DEDUP
)
573 error
= SET_ERROR(ENOTSUP
);
575 error
= nvpair_value_uint64(elem
, &intval
);
577 intval
!= 0 && intval
< ZIO_DEDUPDITTO_MIN
)
578 error
= SET_ERROR(EINVAL
);
589 if (!error
&& reset_bootfs
) {
590 error
= nvlist_remove(props
,
591 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), DATA_TYPE_STRING
);
594 error
= nvlist_add_uint64(props
,
595 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), objnum
);
603 spa_configfile_set(spa_t
*spa
, nvlist_t
*nvp
, boolean_t need_sync
)
606 spa_config_dirent_t
*dp
;
608 if (nvlist_lookup_string(nvp
, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
),
612 dp
= kmem_alloc(sizeof (spa_config_dirent_t
),
615 if (cachefile
[0] == '\0')
616 dp
->scd_path
= spa_strdup(spa_config_path
);
617 else if (strcmp(cachefile
, "none") == 0)
620 dp
->scd_path
= spa_strdup(cachefile
);
622 list_insert_head(&spa
->spa_config_list
, dp
);
624 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
628 spa_prop_set(spa_t
*spa
, nvlist_t
*nvp
)
631 nvpair_t
*elem
= NULL
;
632 boolean_t need_sync
= B_FALSE
;
634 if ((error
= spa_prop_validate(spa
, nvp
)) != 0)
637 while ((elem
= nvlist_next_nvpair(nvp
, elem
)) != NULL
) {
638 zpool_prop_t prop
= zpool_name_to_prop(nvpair_name(elem
));
640 if (prop
== ZPOOL_PROP_CACHEFILE
||
641 prop
== ZPOOL_PROP_ALTROOT
||
642 prop
== ZPOOL_PROP_READONLY
)
645 if (prop
== ZPOOL_PROP_VERSION
|| prop
== ZPROP_INVAL
) {
648 if (prop
== ZPOOL_PROP_VERSION
) {
649 VERIFY(nvpair_value_uint64(elem
, &ver
) == 0);
651 ASSERT(zpool_prop_feature(nvpair_name(elem
)));
652 ver
= SPA_VERSION_FEATURES
;
656 /* Save time if the version is already set. */
657 if (ver
== spa_version(spa
))
661 * In addition to the pool directory object, we might
662 * create the pool properties object, the features for
663 * read object, the features for write object, or the
664 * feature descriptions object.
666 error
= dsl_sync_task(spa
->spa_name
, NULL
,
667 spa_sync_version
, &ver
, 6);
678 return (dsl_sync_task(spa
->spa_name
, NULL
, spa_sync_props
,
686 * If the bootfs property value is dsobj, clear it.
689 spa_prop_clear_bootfs(spa_t
*spa
, uint64_t dsobj
, dmu_tx_t
*tx
)
691 if (spa
->spa_bootfs
== dsobj
&& spa
->spa_pool_props_object
!= 0) {
692 VERIFY(zap_remove(spa
->spa_meta_objset
,
693 spa
->spa_pool_props_object
,
694 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), tx
) == 0);
701 spa_change_guid_check(void *arg
, dmu_tx_t
*tx
)
703 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
704 vdev_t
*rvd
= spa
->spa_root_vdev
;
706 ASSERTV(uint64_t *newguid
= arg
);
708 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
709 vdev_state
= rvd
->vdev_state
;
710 spa_config_exit(spa
, SCL_STATE
, FTAG
);
712 if (vdev_state
!= VDEV_STATE_HEALTHY
)
713 return (SET_ERROR(ENXIO
));
715 ASSERT3U(spa_guid(spa
), !=, *newguid
);
721 spa_change_guid_sync(void *arg
, dmu_tx_t
*tx
)
723 uint64_t *newguid
= arg
;
724 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
726 vdev_t
*rvd
= spa
->spa_root_vdev
;
728 oldguid
= spa_guid(spa
);
730 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
731 rvd
->vdev_guid
= *newguid
;
732 rvd
->vdev_guid_sum
+= (*newguid
- oldguid
);
733 vdev_config_dirty(rvd
);
734 spa_config_exit(spa
, SCL_STATE
, FTAG
);
736 spa_history_log_internal(spa
, "guid change", tx
, "old=%llu new=%llu",
741 * Change the GUID for the pool. This is done so that we can later
742 * re-import a pool built from a clone of our own vdevs. We will modify
743 * the root vdev's guid, our own pool guid, and then mark all of our
744 * vdevs dirty. Note that we must make sure that all our vdevs are
745 * online when we do this, or else any vdevs that weren't present
746 * would be orphaned from our pool. We are also going to issue a
747 * sysevent to update any watchers.
750 spa_change_guid(spa_t
*spa
)
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
);
772 * ==========================================================================
773 * SPA state manipulation (open/create/destroy/import/export)
774 * ==========================================================================
778 spa_error_entry_compare(const void *a
, const void *b
)
780 spa_error_entry_t
*sa
= (spa_error_entry_t
*)a
;
781 spa_error_entry_t
*sb
= (spa_error_entry_t
*)b
;
784 ret
= bcmp(&sa
->se_bookmark
, &sb
->se_bookmark
,
785 sizeof (zbookmark_t
));
796 * Utility function which retrieves copies of the current logs and
797 * re-initializes them in the process.
800 spa_get_errlists(spa_t
*spa
, avl_tree_t
*last
, avl_tree_t
*scrub
)
802 ASSERT(MUTEX_HELD(&spa
->spa_errlist_lock
));
804 bcopy(&spa
->spa_errlist_last
, last
, sizeof (avl_tree_t
));
805 bcopy(&spa
->spa_errlist_scrub
, scrub
, sizeof (avl_tree_t
));
807 avl_create(&spa
->spa_errlist_scrub
,
808 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
809 offsetof(spa_error_entry_t
, se_avl
));
810 avl_create(&spa
->spa_errlist_last
,
811 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
812 offsetof(spa_error_entry_t
, se_avl
));
816 spa_taskqs_init(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
818 const zio_taskq_info_t
*ztip
= &zio_taskqs
[t
][q
];
819 enum zti_modes mode
= ztip
->zti_mode
;
820 uint_t value
= ztip
->zti_value
;
821 uint_t count
= ztip
->zti_count
;
822 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
825 boolean_t batch
= B_FALSE
;
827 if (mode
== ZTI_MODE_NULL
) {
829 tqs
->stqs_taskq
= NULL
;
833 ASSERT3U(count
, >, 0);
835 tqs
->stqs_count
= count
;
836 tqs
->stqs_taskq
= kmem_alloc(count
* sizeof (taskq_t
*), KM_SLEEP
);
838 for (i
= 0; i
< count
; i
++) {
843 ASSERT3U(value
, >=, 1);
844 value
= MAX(value
, 1);
849 flags
|= TASKQ_THREADS_CPU_PCT
;
850 value
= zio_taskq_batch_pct
;
853 case ZTI_MODE_ONLINE_PERCENT
:
854 flags
|= TASKQ_THREADS_CPU_PCT
;
858 panic("unrecognized mode for %s_%s taskq (%u:%u) in "
860 zio_type_name
[t
], zio_taskq_types
[q
], mode
, value
);
865 (void) snprintf(name
, sizeof (name
), "%s_%s_%u",
866 zio_type_name
[t
], zio_taskq_types
[q
], i
);
868 (void) snprintf(name
, sizeof (name
), "%s_%s",
869 zio_type_name
[t
], zio_taskq_types
[q
]);
872 if (zio_taskq_sysdc
&& spa
->spa_proc
!= &p0
) {
874 flags
|= TASKQ_DC_BATCH
;
876 tq
= taskq_create_sysdc(name
, value
, 50, INT_MAX
,
877 spa
->spa_proc
, zio_taskq_basedc
, flags
);
879 tq
= taskq_create_proc(name
, value
, maxclsyspri
, 50,
880 INT_MAX
, spa
->spa_proc
, flags
);
883 tqs
->stqs_taskq
[i
] = tq
;
888 spa_taskqs_fini(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
890 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
893 if (tqs
->stqs_taskq
== NULL
) {
894 ASSERT3U(tqs
->stqs_count
, ==, 0);
898 for (i
= 0; i
< tqs
->stqs_count
; i
++) {
899 ASSERT3P(tqs
->stqs_taskq
[i
], !=, NULL
);
900 taskq_destroy(tqs
->stqs_taskq
[i
]);
903 kmem_free(tqs
->stqs_taskq
, tqs
->stqs_count
* sizeof (taskq_t
*));
904 tqs
->stqs_taskq
= NULL
;
908 * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority.
909 * Note that a type may have multiple discrete taskqs to avoid lock contention
910 * on the taskq itself. In that case we choose which taskq at random by using
911 * the low bits of gethrtime().
914 spa_taskq_dispatch_ent(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
915 task_func_t
*func
, void *arg
, uint_t flags
, taskq_ent_t
*ent
)
917 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
920 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
921 ASSERT3U(tqs
->stqs_count
, !=, 0);
923 if (tqs
->stqs_count
== 1) {
924 tq
= tqs
->stqs_taskq
[0];
926 tq
= tqs
->stqs_taskq
[((uint64_t)gethrtime()) % tqs
->stqs_count
];
929 taskq_dispatch_ent(tq
, func
, arg
, flags
, ent
);
933 * Same as spa_taskq_dispatch_ent() but block on the task until completion.
936 spa_taskq_dispatch_sync(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
937 task_func_t
*func
, void *arg
, uint_t flags
)
939 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
943 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
944 ASSERT3U(tqs
->stqs_count
, !=, 0);
946 if (tqs
->stqs_count
== 1) {
947 tq
= tqs
->stqs_taskq
[0];
949 tq
= tqs
->stqs_taskq
[((uint64_t)gethrtime()) % tqs
->stqs_count
];
952 id
= taskq_dispatch(tq
, func
, arg
, flags
);
954 taskq_wait_id(tq
, id
);
958 spa_create_zio_taskqs(spa_t
*spa
)
962 for (t
= 0; t
< ZIO_TYPES
; t
++) {
963 for (q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
964 spa_taskqs_init(spa
, t
, q
);
969 #if defined(_KERNEL) && defined(HAVE_SPA_THREAD)
971 spa_thread(void *arg
)
976 user_t
*pu
= PTOU(curproc
);
978 CALLB_CPR_INIT(&cprinfo
, &spa
->spa_proc_lock
, callb_generic_cpr
,
981 ASSERT(curproc
!= &p0
);
982 (void) snprintf(pu
->u_psargs
, sizeof (pu
->u_psargs
),
983 "zpool-%s", spa
->spa_name
);
984 (void) strlcpy(pu
->u_comm
, pu
->u_psargs
, sizeof (pu
->u_comm
));
986 /* bind this thread to the requested psrset */
987 if (zio_taskq_psrset_bind
!= PS_NONE
) {
989 mutex_enter(&cpu_lock
);
990 mutex_enter(&pidlock
);
991 mutex_enter(&curproc
->p_lock
);
993 if (cpupart_bind_thread(curthread
, zio_taskq_psrset_bind
,
994 0, NULL
, NULL
) == 0) {
995 curthread
->t_bind_pset
= zio_taskq_psrset_bind
;
998 "Couldn't bind process for zfs pool \"%s\" to "
999 "pset %d\n", spa
->spa_name
, zio_taskq_psrset_bind
);
1002 mutex_exit(&curproc
->p_lock
);
1003 mutex_exit(&pidlock
);
1004 mutex_exit(&cpu_lock
);
1008 if (zio_taskq_sysdc
) {
1009 sysdc_thread_enter(curthread
, 100, 0);
1012 spa
->spa_proc
= curproc
;
1013 spa
->spa_did
= curthread
->t_did
;
1015 spa_create_zio_taskqs(spa
);
1017 mutex_enter(&spa
->spa_proc_lock
);
1018 ASSERT(spa
->spa_proc_state
== SPA_PROC_CREATED
);
1020 spa
->spa_proc_state
= SPA_PROC_ACTIVE
;
1021 cv_broadcast(&spa
->spa_proc_cv
);
1023 CALLB_CPR_SAFE_BEGIN(&cprinfo
);
1024 while (spa
->spa_proc_state
== SPA_PROC_ACTIVE
)
1025 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1026 CALLB_CPR_SAFE_END(&cprinfo
, &spa
->spa_proc_lock
);
1028 ASSERT(spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
);
1029 spa
->spa_proc_state
= SPA_PROC_GONE
;
1030 spa
->spa_proc
= &p0
;
1031 cv_broadcast(&spa
->spa_proc_cv
);
1032 CALLB_CPR_EXIT(&cprinfo
); /* drops spa_proc_lock */
1034 mutex_enter(&curproc
->p_lock
);
1040 * Activate an uninitialized pool.
1043 spa_activate(spa_t
*spa
, int mode
)
1045 ASSERT(spa
->spa_state
== POOL_STATE_UNINITIALIZED
);
1047 spa
->spa_state
= POOL_STATE_ACTIVE
;
1048 spa
->spa_mode
= mode
;
1050 spa
->spa_normal_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
1051 spa
->spa_log_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
1053 /* Try to create a covering process */
1054 mutex_enter(&spa
->spa_proc_lock
);
1055 ASSERT(spa
->spa_proc_state
== SPA_PROC_NONE
);
1056 ASSERT(spa
->spa_proc
== &p0
);
1059 #ifdef HAVE_SPA_THREAD
1060 /* Only create a process if we're going to be around a while. */
1061 if (spa_create_process
&& strcmp(spa
->spa_name
, TRYIMPORT_NAME
) != 0) {
1062 if (newproc(spa_thread
, (caddr_t
)spa
, syscid
, maxclsyspri
,
1064 spa
->spa_proc_state
= SPA_PROC_CREATED
;
1065 while (spa
->spa_proc_state
== SPA_PROC_CREATED
) {
1066 cv_wait(&spa
->spa_proc_cv
,
1067 &spa
->spa_proc_lock
);
1069 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1070 ASSERT(spa
->spa_proc
!= &p0
);
1071 ASSERT(spa
->spa_did
!= 0);
1075 "Couldn't create process for zfs pool \"%s\"\n",
1080 #endif /* HAVE_SPA_THREAD */
1081 mutex_exit(&spa
->spa_proc_lock
);
1083 /* If we didn't create a process, we need to create our taskqs. */
1084 if (spa
->spa_proc
== &p0
) {
1085 spa_create_zio_taskqs(spa
);
1088 list_create(&spa
->spa_config_dirty_list
, sizeof (vdev_t
),
1089 offsetof(vdev_t
, vdev_config_dirty_node
));
1090 list_create(&spa
->spa_state_dirty_list
, sizeof (vdev_t
),
1091 offsetof(vdev_t
, vdev_state_dirty_node
));
1093 txg_list_create(&spa
->spa_vdev_txg_list
,
1094 offsetof(struct vdev
, vdev_txg_node
));
1096 avl_create(&spa
->spa_errlist_scrub
,
1097 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1098 offsetof(spa_error_entry_t
, se_avl
));
1099 avl_create(&spa
->spa_errlist_last
,
1100 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1101 offsetof(spa_error_entry_t
, se_avl
));
1105 * Opposite of spa_activate().
1108 spa_deactivate(spa_t
*spa
)
1112 ASSERT(spa
->spa_sync_on
== B_FALSE
);
1113 ASSERT(spa
->spa_dsl_pool
== NULL
);
1114 ASSERT(spa
->spa_root_vdev
== NULL
);
1115 ASSERT(spa
->spa_async_zio_root
== NULL
);
1116 ASSERT(spa
->spa_state
!= POOL_STATE_UNINITIALIZED
);
1118 txg_list_destroy(&spa
->spa_vdev_txg_list
);
1120 list_destroy(&spa
->spa_config_dirty_list
);
1121 list_destroy(&spa
->spa_state_dirty_list
);
1123 taskq_cancel_id(system_taskq
, spa
->spa_deadman_tqid
);
1125 for (t
= 0; t
< ZIO_TYPES
; t
++) {
1126 for (q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1127 spa_taskqs_fini(spa
, t
, q
);
1131 metaslab_class_destroy(spa
->spa_normal_class
);
1132 spa
->spa_normal_class
= NULL
;
1134 metaslab_class_destroy(spa
->spa_log_class
);
1135 spa
->spa_log_class
= NULL
;
1138 * If this was part of an import or the open otherwise failed, we may
1139 * still have errors left in the queues. Empty them just in case.
1141 spa_errlog_drain(spa
);
1143 avl_destroy(&spa
->spa_errlist_scrub
);
1144 avl_destroy(&spa
->spa_errlist_last
);
1146 spa
->spa_state
= POOL_STATE_UNINITIALIZED
;
1148 mutex_enter(&spa
->spa_proc_lock
);
1149 if (spa
->spa_proc_state
!= SPA_PROC_NONE
) {
1150 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1151 spa
->spa_proc_state
= SPA_PROC_DEACTIVATE
;
1152 cv_broadcast(&spa
->spa_proc_cv
);
1153 while (spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
) {
1154 ASSERT(spa
->spa_proc
!= &p0
);
1155 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1157 ASSERT(spa
->spa_proc_state
== SPA_PROC_GONE
);
1158 spa
->spa_proc_state
= SPA_PROC_NONE
;
1160 ASSERT(spa
->spa_proc
== &p0
);
1161 mutex_exit(&spa
->spa_proc_lock
);
1164 * We want to make sure spa_thread() has actually exited the ZFS
1165 * module, so that the module can't be unloaded out from underneath
1168 if (spa
->spa_did
!= 0) {
1169 thread_join(spa
->spa_did
);
1175 * Verify a pool configuration, and construct the vdev tree appropriately. This
1176 * will create all the necessary vdevs in the appropriate layout, with each vdev
1177 * in the CLOSED state. This will prep the pool before open/creation/import.
1178 * All vdev validation is done by the vdev_alloc() routine.
1181 spa_config_parse(spa_t
*spa
, vdev_t
**vdp
, nvlist_t
*nv
, vdev_t
*parent
,
1182 uint_t id
, int atype
)
1189 if ((error
= vdev_alloc(spa
, vdp
, nv
, parent
, id
, atype
)) != 0)
1192 if ((*vdp
)->vdev_ops
->vdev_op_leaf
)
1195 error
= nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1198 if (error
== ENOENT
)
1204 return (SET_ERROR(EINVAL
));
1207 for (c
= 0; c
< children
; c
++) {
1209 if ((error
= spa_config_parse(spa
, &vd
, child
[c
], *vdp
, c
,
1217 ASSERT(*vdp
!= NULL
);
1223 * Opposite of spa_load().
1226 spa_unload(spa_t
*spa
)
1230 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1235 spa_async_suspend(spa
);
1240 if (spa
->spa_sync_on
) {
1241 txg_sync_stop(spa
->spa_dsl_pool
);
1242 spa
->spa_sync_on
= B_FALSE
;
1246 * Wait for any outstanding async I/O to complete.
1248 if (spa
->spa_async_zio_root
!= NULL
) {
1249 (void) zio_wait(spa
->spa_async_zio_root
);
1250 spa
->spa_async_zio_root
= NULL
;
1253 bpobj_close(&spa
->spa_deferred_bpobj
);
1256 * Close the dsl pool.
1258 if (spa
->spa_dsl_pool
) {
1259 dsl_pool_close(spa
->spa_dsl_pool
);
1260 spa
->spa_dsl_pool
= NULL
;
1261 spa
->spa_meta_objset
= NULL
;
1266 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1269 * Drop and purge level 2 cache
1271 spa_l2cache_drop(spa
);
1276 if (spa
->spa_root_vdev
)
1277 vdev_free(spa
->spa_root_vdev
);
1278 ASSERT(spa
->spa_root_vdev
== NULL
);
1280 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1281 vdev_free(spa
->spa_spares
.sav_vdevs
[i
]);
1282 if (spa
->spa_spares
.sav_vdevs
) {
1283 kmem_free(spa
->spa_spares
.sav_vdevs
,
1284 spa
->spa_spares
.sav_count
* sizeof (void *));
1285 spa
->spa_spares
.sav_vdevs
= NULL
;
1287 if (spa
->spa_spares
.sav_config
) {
1288 nvlist_free(spa
->spa_spares
.sav_config
);
1289 spa
->spa_spares
.sav_config
= NULL
;
1291 spa
->spa_spares
.sav_count
= 0;
1293 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
1294 vdev_clear_stats(spa
->spa_l2cache
.sav_vdevs
[i
]);
1295 vdev_free(spa
->spa_l2cache
.sav_vdevs
[i
]);
1297 if (spa
->spa_l2cache
.sav_vdevs
) {
1298 kmem_free(spa
->spa_l2cache
.sav_vdevs
,
1299 spa
->spa_l2cache
.sav_count
* sizeof (void *));
1300 spa
->spa_l2cache
.sav_vdevs
= NULL
;
1302 if (spa
->spa_l2cache
.sav_config
) {
1303 nvlist_free(spa
->spa_l2cache
.sav_config
);
1304 spa
->spa_l2cache
.sav_config
= NULL
;
1306 spa
->spa_l2cache
.sav_count
= 0;
1308 spa
->spa_async_suspended
= 0;
1310 if (spa
->spa_comment
!= NULL
) {
1311 spa_strfree(spa
->spa_comment
);
1312 spa
->spa_comment
= NULL
;
1315 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1319 * Load (or re-load) the current list of vdevs describing the active spares for
1320 * this pool. When this is called, we have some form of basic information in
1321 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1322 * then re-generate a more complete list including status information.
1325 spa_load_spares(spa_t
*spa
)
1332 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1335 * First, close and free any existing spare vdevs.
1337 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1338 vd
= spa
->spa_spares
.sav_vdevs
[i
];
1340 /* Undo the call to spa_activate() below */
1341 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1342 B_FALSE
)) != NULL
&& tvd
->vdev_isspare
)
1343 spa_spare_remove(tvd
);
1348 if (spa
->spa_spares
.sav_vdevs
)
1349 kmem_free(spa
->spa_spares
.sav_vdevs
,
1350 spa
->spa_spares
.sav_count
* sizeof (void *));
1352 if (spa
->spa_spares
.sav_config
== NULL
)
1355 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
1356 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
1358 spa
->spa_spares
.sav_count
= (int)nspares
;
1359 spa
->spa_spares
.sav_vdevs
= NULL
;
1365 * Construct the array of vdevs, opening them to get status in the
1366 * process. For each spare, there is potentially two different vdev_t
1367 * structures associated with it: one in the list of spares (used only
1368 * for basic validation purposes) and one in the active vdev
1369 * configuration (if it's spared in). During this phase we open and
1370 * validate each vdev on the spare list. If the vdev also exists in the
1371 * active configuration, then we also mark this vdev as an active spare.
1373 spa
->spa_spares
.sav_vdevs
= kmem_alloc(nspares
* sizeof (void *),
1375 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1376 VERIFY(spa_config_parse(spa
, &vd
, spares
[i
], NULL
, 0,
1377 VDEV_ALLOC_SPARE
) == 0);
1380 spa
->spa_spares
.sav_vdevs
[i
] = vd
;
1382 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1383 B_FALSE
)) != NULL
) {
1384 if (!tvd
->vdev_isspare
)
1388 * We only mark the spare active if we were successfully
1389 * able to load the vdev. Otherwise, importing a pool
1390 * with a bad active spare would result in strange
1391 * behavior, because multiple pool would think the spare
1392 * is actively in use.
1394 * There is a vulnerability here to an equally bizarre
1395 * circumstance, where a dead active spare is later
1396 * brought back to life (onlined or otherwise). Given
1397 * the rarity of this scenario, and the extra complexity
1398 * it adds, we ignore the possibility.
1400 if (!vdev_is_dead(tvd
))
1401 spa_spare_activate(tvd
);
1405 vd
->vdev_aux
= &spa
->spa_spares
;
1407 if (vdev_open(vd
) != 0)
1410 if (vdev_validate_aux(vd
) == 0)
1415 * Recompute the stashed list of spares, with status information
1418 VERIFY(nvlist_remove(spa
->spa_spares
.sav_config
, ZPOOL_CONFIG_SPARES
,
1419 DATA_TYPE_NVLIST_ARRAY
) == 0);
1421 spares
= kmem_alloc(spa
->spa_spares
.sav_count
* sizeof (void *),
1423 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1424 spares
[i
] = vdev_config_generate(spa
,
1425 spa
->spa_spares
.sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_SPARE
);
1426 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
1427 ZPOOL_CONFIG_SPARES
, spares
, spa
->spa_spares
.sav_count
) == 0);
1428 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1429 nvlist_free(spares
[i
]);
1430 kmem_free(spares
, spa
->spa_spares
.sav_count
* sizeof (void *));
1434 * Load (or re-load) the current list of vdevs describing the active l2cache for
1435 * this pool. When this is called, we have some form of basic information in
1436 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1437 * then re-generate a more complete list including status information.
1438 * Devices which are already active have their details maintained, and are
1442 spa_load_l2cache(spa_t
*spa
)
1446 int i
, j
, oldnvdevs
;
1448 vdev_t
*vd
, **oldvdevs
, **newvdevs
;
1449 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
1451 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1453 if (sav
->sav_config
!= NULL
) {
1454 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
,
1455 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
1456 newvdevs
= kmem_alloc(nl2cache
* sizeof (void *), KM_PUSHPAGE
);
1462 oldvdevs
= sav
->sav_vdevs
;
1463 oldnvdevs
= sav
->sav_count
;
1464 sav
->sav_vdevs
= NULL
;
1468 * Process new nvlist of vdevs.
1470 for (i
= 0; i
< nl2cache
; i
++) {
1471 VERIFY(nvlist_lookup_uint64(l2cache
[i
], ZPOOL_CONFIG_GUID
,
1475 for (j
= 0; j
< oldnvdevs
; j
++) {
1477 if (vd
!= NULL
&& guid
== vd
->vdev_guid
) {
1479 * Retain previous vdev for add/remove ops.
1487 if (newvdevs
[i
] == NULL
) {
1491 VERIFY(spa_config_parse(spa
, &vd
, l2cache
[i
], NULL
, 0,
1492 VDEV_ALLOC_L2CACHE
) == 0);
1497 * Commit this vdev as an l2cache device,
1498 * even if it fails to open.
1500 spa_l2cache_add(vd
);
1505 spa_l2cache_activate(vd
);
1507 if (vdev_open(vd
) != 0)
1510 (void) vdev_validate_aux(vd
);
1512 if (!vdev_is_dead(vd
))
1513 l2arc_add_vdev(spa
, vd
);
1518 * Purge vdevs that were dropped
1520 for (i
= 0; i
< oldnvdevs
; i
++) {
1525 ASSERT(vd
->vdev_isl2cache
);
1527 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
1528 pool
!= 0ULL && l2arc_vdev_present(vd
))
1529 l2arc_remove_vdev(vd
);
1530 vdev_clear_stats(vd
);
1536 kmem_free(oldvdevs
, oldnvdevs
* sizeof (void *));
1538 if (sav
->sav_config
== NULL
)
1541 sav
->sav_vdevs
= newvdevs
;
1542 sav
->sav_count
= (int)nl2cache
;
1545 * Recompute the stashed list of l2cache devices, with status
1546 * information this time.
1548 VERIFY(nvlist_remove(sav
->sav_config
, ZPOOL_CONFIG_L2CACHE
,
1549 DATA_TYPE_NVLIST_ARRAY
) == 0);
1551 l2cache
= kmem_alloc(sav
->sav_count
* sizeof (void *), KM_PUSHPAGE
);
1552 for (i
= 0; i
< sav
->sav_count
; i
++)
1553 l2cache
[i
] = vdev_config_generate(spa
,
1554 sav
->sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_L2CACHE
);
1555 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
,
1556 ZPOOL_CONFIG_L2CACHE
, l2cache
, sav
->sav_count
) == 0);
1558 for (i
= 0; i
< sav
->sav_count
; i
++)
1559 nvlist_free(l2cache
[i
]);
1561 kmem_free(l2cache
, sav
->sav_count
* sizeof (void *));
1565 load_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
**value
)
1568 char *packed
= NULL
;
1573 error
= dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
);
1577 nvsize
= *(uint64_t *)db
->db_data
;
1578 dmu_buf_rele(db
, FTAG
);
1580 packed
= kmem_alloc(nvsize
, KM_PUSHPAGE
| KM_NODEBUG
);
1581 error
= dmu_read(spa
->spa_meta_objset
, obj
, 0, nvsize
, packed
,
1584 error
= nvlist_unpack(packed
, nvsize
, value
, 0);
1585 kmem_free(packed
, nvsize
);
1591 * Checks to see if the given vdev could not be opened, in which case we post a
1592 * sysevent to notify the autoreplace code that the device has been removed.
1595 spa_check_removed(vdev_t
*vd
)
1599 for (c
= 0; c
< vd
->vdev_children
; c
++)
1600 spa_check_removed(vd
->vdev_child
[c
]);
1602 if (vd
->vdev_ops
->vdev_op_leaf
&& vdev_is_dead(vd
) &&
1604 zfs_ereport_post(FM_EREPORT_RESOURCE_AUTOREPLACE
,
1605 vd
->vdev_spa
, vd
, NULL
, 0, 0);
1606 spa_event_notify(vd
->vdev_spa
, vd
, FM_EREPORT_ZFS_DEVICE_CHECK
);
1611 * Validate the current config against the MOS config
1614 spa_config_valid(spa_t
*spa
, nvlist_t
*config
)
1616 vdev_t
*mrvd
, *rvd
= spa
->spa_root_vdev
;
1620 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nv
) == 0);
1622 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1623 VERIFY(spa_config_parse(spa
, &mrvd
, nv
, NULL
, 0, VDEV_ALLOC_LOAD
) == 0);
1625 ASSERT3U(rvd
->vdev_children
, ==, mrvd
->vdev_children
);
1628 * If we're doing a normal import, then build up any additional
1629 * diagnostic information about missing devices in this config.
1630 * We'll pass this up to the user for further processing.
1632 if (!(spa
->spa_import_flags
& ZFS_IMPORT_MISSING_LOG
)) {
1633 nvlist_t
**child
, *nv
;
1636 child
= kmem_alloc(rvd
->vdev_children
* sizeof (nvlist_t
**),
1638 VERIFY(nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_PUSHPAGE
) == 0);
1640 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1641 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1642 vdev_t
*mtvd
= mrvd
->vdev_child
[c
];
1644 if (tvd
->vdev_ops
== &vdev_missing_ops
&&
1645 mtvd
->vdev_ops
!= &vdev_missing_ops
&&
1647 child
[idx
++] = vdev_config_generate(spa
, mtvd
,
1652 VERIFY(nvlist_add_nvlist_array(nv
,
1653 ZPOOL_CONFIG_CHILDREN
, child
, idx
) == 0);
1654 VERIFY(nvlist_add_nvlist(spa
->spa_load_info
,
1655 ZPOOL_CONFIG_MISSING_DEVICES
, nv
) == 0);
1657 for (i
= 0; i
< idx
; i
++)
1658 nvlist_free(child
[i
]);
1661 kmem_free(child
, rvd
->vdev_children
* sizeof (char **));
1665 * Compare the root vdev tree with the information we have
1666 * from the MOS config (mrvd). Check each top-level vdev
1667 * with the corresponding MOS config top-level (mtvd).
1669 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1670 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1671 vdev_t
*mtvd
= mrvd
->vdev_child
[c
];
1674 * Resolve any "missing" vdevs in the current configuration.
1675 * If we find that the MOS config has more accurate information
1676 * about the top-level vdev then use that vdev instead.
1678 if (tvd
->vdev_ops
== &vdev_missing_ops
&&
1679 mtvd
->vdev_ops
!= &vdev_missing_ops
) {
1681 if (!(spa
->spa_import_flags
& ZFS_IMPORT_MISSING_LOG
))
1685 * Device specific actions.
1687 if (mtvd
->vdev_islog
) {
1688 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
1691 * XXX - once we have 'readonly' pool
1692 * support we should be able to handle
1693 * missing data devices by transitioning
1694 * the pool to readonly.
1700 * Swap the missing vdev with the data we were
1701 * able to obtain from the MOS config.
1703 vdev_remove_child(rvd
, tvd
);
1704 vdev_remove_child(mrvd
, mtvd
);
1706 vdev_add_child(rvd
, mtvd
);
1707 vdev_add_child(mrvd
, tvd
);
1709 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1711 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1714 } else if (mtvd
->vdev_islog
) {
1716 * Load the slog device's state from the MOS config
1717 * since it's possible that the label does not
1718 * contain the most up-to-date information.
1720 vdev_load_log_state(tvd
, mtvd
);
1725 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1728 * Ensure we were able to validate the config.
1730 return (rvd
->vdev_guid_sum
== spa
->spa_uberblock
.ub_guid_sum
);
1734 * Check for missing log devices
1737 spa_check_logs(spa_t
*spa
)
1739 boolean_t rv
= B_FALSE
;
1741 switch (spa
->spa_log_state
) {
1744 case SPA_LOG_MISSING
:
1745 /* need to recheck in case slog has been restored */
1746 case SPA_LOG_UNKNOWN
:
1747 rv
= (dmu_objset_find(spa
->spa_name
, zil_check_log_chain
,
1748 NULL
, DS_FIND_CHILDREN
) != 0);
1750 spa_set_log_state(spa
, SPA_LOG_MISSING
);
1757 spa_passivate_log(spa_t
*spa
)
1759 vdev_t
*rvd
= spa
->spa_root_vdev
;
1760 boolean_t slog_found
= B_FALSE
;
1763 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
1765 if (!spa_has_slogs(spa
))
1768 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1769 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1770 metaslab_group_t
*mg
= tvd
->vdev_mg
;
1772 if (tvd
->vdev_islog
) {
1773 metaslab_group_passivate(mg
);
1774 slog_found
= B_TRUE
;
1778 return (slog_found
);
1782 spa_activate_log(spa_t
*spa
)
1784 vdev_t
*rvd
= spa
->spa_root_vdev
;
1787 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
1789 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1790 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1791 metaslab_group_t
*mg
= tvd
->vdev_mg
;
1793 if (tvd
->vdev_islog
)
1794 metaslab_group_activate(mg
);
1799 spa_offline_log(spa_t
*spa
)
1803 error
= dmu_objset_find(spa_name(spa
), zil_vdev_offline
,
1804 NULL
, DS_FIND_CHILDREN
);
1807 * We successfully offlined the log device, sync out the
1808 * current txg so that the "stubby" block can be removed
1811 txg_wait_synced(spa
->spa_dsl_pool
, 0);
1817 spa_aux_check_removed(spa_aux_vdev_t
*sav
)
1821 for (i
= 0; i
< sav
->sav_count
; i
++)
1822 spa_check_removed(sav
->sav_vdevs
[i
]);
1826 spa_claim_notify(zio_t
*zio
)
1828 spa_t
*spa
= zio
->io_spa
;
1833 mutex_enter(&spa
->spa_props_lock
); /* any mutex will do */
1834 if (spa
->spa_claim_max_txg
< zio
->io_bp
->blk_birth
)
1835 spa
->spa_claim_max_txg
= zio
->io_bp
->blk_birth
;
1836 mutex_exit(&spa
->spa_props_lock
);
1839 typedef struct spa_load_error
{
1840 uint64_t sle_meta_count
;
1841 uint64_t sle_data_count
;
1845 spa_load_verify_done(zio_t
*zio
)
1847 blkptr_t
*bp
= zio
->io_bp
;
1848 spa_load_error_t
*sle
= zio
->io_private
;
1849 dmu_object_type_t type
= BP_GET_TYPE(bp
);
1850 int error
= zio
->io_error
;
1853 if ((BP_GET_LEVEL(bp
) != 0 || DMU_OT_IS_METADATA(type
)) &&
1854 type
!= DMU_OT_INTENT_LOG
)
1855 atomic_add_64(&sle
->sle_meta_count
, 1);
1857 atomic_add_64(&sle
->sle_data_count
, 1);
1859 zio_data_buf_free(zio
->io_data
, zio
->io_size
);
1864 spa_load_verify_cb(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
1865 const zbookmark_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
1869 size_t size
= BP_GET_PSIZE(bp
);
1870 void *data
= zio_data_buf_alloc(size
);
1872 zio_nowait(zio_read(rio
, spa
, bp
, data
, size
,
1873 spa_load_verify_done
, rio
->io_private
, ZIO_PRIORITY_SCRUB
,
1874 ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_CANFAIL
|
1875 ZIO_FLAG_SCRUB
| ZIO_FLAG_RAW
, zb
));
1881 spa_load_verify(spa_t
*spa
)
1884 spa_load_error_t sle
= { 0 };
1885 zpool_rewind_policy_t policy
;
1886 boolean_t verify_ok
= B_FALSE
;
1889 zpool_get_rewind_policy(spa
->spa_config
, &policy
);
1891 if (policy
.zrp_request
& ZPOOL_NEVER_REWIND
)
1894 rio
= zio_root(spa
, NULL
, &sle
,
1895 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
);
1897 error
= traverse_pool(spa
, spa
->spa_verify_min_txg
,
1898 TRAVERSE_PRE
| TRAVERSE_PREFETCH
, spa_load_verify_cb
, rio
);
1900 (void) zio_wait(rio
);
1902 spa
->spa_load_meta_errors
= sle
.sle_meta_count
;
1903 spa
->spa_load_data_errors
= sle
.sle_data_count
;
1905 if (!error
&& sle
.sle_meta_count
<= policy
.zrp_maxmeta
&&
1906 sle
.sle_data_count
<= policy
.zrp_maxdata
) {
1910 spa
->spa_load_txg
= spa
->spa_uberblock
.ub_txg
;
1911 spa
->spa_load_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
1913 loss
= spa
->spa_last_ubsync_txg_ts
- spa
->spa_load_txg_ts
;
1914 VERIFY(nvlist_add_uint64(spa
->spa_load_info
,
1915 ZPOOL_CONFIG_LOAD_TIME
, spa
->spa_load_txg_ts
) == 0);
1916 VERIFY(nvlist_add_int64(spa
->spa_load_info
,
1917 ZPOOL_CONFIG_REWIND_TIME
, loss
) == 0);
1918 VERIFY(nvlist_add_uint64(spa
->spa_load_info
,
1919 ZPOOL_CONFIG_LOAD_DATA_ERRORS
, sle
.sle_data_count
) == 0);
1921 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
;
1925 if (error
!= ENXIO
&& error
!= EIO
)
1926 error
= SET_ERROR(EIO
);
1930 return (verify_ok
? 0 : EIO
);
1934 * Find a value in the pool props object.
1937 spa_prop_find(spa_t
*spa
, zpool_prop_t prop
, uint64_t *val
)
1939 (void) zap_lookup(spa
->spa_meta_objset
, spa
->spa_pool_props_object
,
1940 zpool_prop_to_name(prop
), sizeof (uint64_t), 1, val
);
1944 * Find a value in the pool directory object.
1947 spa_dir_prop(spa_t
*spa
, const char *name
, uint64_t *val
)
1949 return (zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
1950 name
, sizeof (uint64_t), 1, val
));
1954 spa_vdev_err(vdev_t
*vdev
, vdev_aux_t aux
, int err
)
1956 vdev_set_state(vdev
, B_TRUE
, VDEV_STATE_CANT_OPEN
, aux
);
1961 * Fix up config after a partly-completed split. This is done with the
1962 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
1963 * pool have that entry in their config, but only the splitting one contains
1964 * a list of all the guids of the vdevs that are being split off.
1966 * This function determines what to do with that list: either rejoin
1967 * all the disks to the pool, or complete the splitting process. To attempt
1968 * the rejoin, each disk that is offlined is marked online again, and
1969 * we do a reopen() call. If the vdev label for every disk that was
1970 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
1971 * then we call vdev_split() on each disk, and complete the split.
1973 * Otherwise we leave the config alone, with all the vdevs in place in
1974 * the original pool.
1977 spa_try_repair(spa_t
*spa
, nvlist_t
*config
)
1984 boolean_t attempt_reopen
;
1986 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
, &nvl
) != 0)
1989 /* check that the config is complete */
1990 if (nvlist_lookup_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
1991 &glist
, &gcount
) != 0)
1994 vd
= kmem_zalloc(gcount
* sizeof (vdev_t
*), KM_PUSHPAGE
);
1996 /* attempt to online all the vdevs & validate */
1997 attempt_reopen
= B_TRUE
;
1998 for (i
= 0; i
< gcount
; i
++) {
1999 if (glist
[i
] == 0) /* vdev is hole */
2002 vd
[i
] = spa_lookup_by_guid(spa
, glist
[i
], B_FALSE
);
2003 if (vd
[i
] == NULL
) {
2005 * Don't bother attempting to reopen the disks;
2006 * just do the split.
2008 attempt_reopen
= B_FALSE
;
2010 /* attempt to re-online it */
2011 vd
[i
]->vdev_offline
= B_FALSE
;
2015 if (attempt_reopen
) {
2016 vdev_reopen(spa
->spa_root_vdev
);
2018 /* check each device to see what state it's in */
2019 for (extracted
= 0, i
= 0; i
< gcount
; i
++) {
2020 if (vd
[i
] != NULL
&&
2021 vd
[i
]->vdev_stat
.vs_aux
!= VDEV_AUX_SPLIT_POOL
)
2028 * If every disk has been moved to the new pool, or if we never
2029 * even attempted to look at them, then we split them off for
2032 if (!attempt_reopen
|| gcount
== extracted
) {
2033 for (i
= 0; i
< gcount
; i
++)
2036 vdev_reopen(spa
->spa_root_vdev
);
2039 kmem_free(vd
, gcount
* sizeof (vdev_t
*));
2043 spa_load(spa_t
*spa
, spa_load_state_t state
, spa_import_type_t type
,
2044 boolean_t mosconfig
)
2046 nvlist_t
*config
= spa
->spa_config
;
2047 char *ereport
= FM_EREPORT_ZFS_POOL
;
2053 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
, &pool_guid
))
2054 return (SET_ERROR(EINVAL
));
2056 ASSERT(spa
->spa_comment
== NULL
);
2057 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
2058 spa
->spa_comment
= spa_strdup(comment
);
2061 * Versioning wasn't explicitly added to the label until later, so if
2062 * it's not present treat it as the initial version.
2064 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
2065 &spa
->spa_ubsync
.ub_version
) != 0)
2066 spa
->spa_ubsync
.ub_version
= SPA_VERSION_INITIAL
;
2068 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
2069 &spa
->spa_config_txg
);
2071 if ((state
== SPA_LOAD_IMPORT
|| state
== SPA_LOAD_TRYIMPORT
) &&
2072 spa_guid_exists(pool_guid
, 0)) {
2073 error
= SET_ERROR(EEXIST
);
2075 spa
->spa_config_guid
= pool_guid
;
2077 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
,
2079 VERIFY(nvlist_dup(nvl
, &spa
->spa_config_splitting
,
2083 nvlist_free(spa
->spa_load_info
);
2084 spa
->spa_load_info
= fnvlist_alloc();
2086 gethrestime(&spa
->spa_loaded_ts
);
2087 error
= spa_load_impl(spa
, pool_guid
, config
, state
, type
,
2088 mosconfig
, &ereport
);
2091 spa
->spa_minref
= refcount_count(&spa
->spa_refcount
);
2093 if (error
!= EEXIST
) {
2094 spa
->spa_loaded_ts
.tv_sec
= 0;
2095 spa
->spa_loaded_ts
.tv_nsec
= 0;
2097 if (error
!= EBADF
) {
2098 zfs_ereport_post(ereport
, spa
, NULL
, NULL
, 0, 0);
2101 spa
->spa_load_state
= error
? SPA_LOAD_ERROR
: SPA_LOAD_NONE
;
2108 * Load an existing storage pool, using the pool's builtin spa_config as a
2109 * source of configuration information.
2111 __attribute__((always_inline
))
2113 spa_load_impl(spa_t
*spa
, uint64_t pool_guid
, nvlist_t
*config
,
2114 spa_load_state_t state
, spa_import_type_t type
, boolean_t mosconfig
,
2118 nvlist_t
*nvroot
= NULL
;
2121 uberblock_t
*ub
= &spa
->spa_uberblock
;
2122 uint64_t children
, config_cache_txg
= spa
->spa_config_txg
;
2123 int orig_mode
= spa
->spa_mode
;
2126 boolean_t missing_feat_write
= B_FALSE
;
2129 * If this is an untrusted config, access the pool in read-only mode.
2130 * This prevents things like resilvering recently removed devices.
2133 spa
->spa_mode
= FREAD
;
2135 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
2137 spa
->spa_load_state
= state
;
2139 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvroot
))
2140 return (SET_ERROR(EINVAL
));
2142 parse
= (type
== SPA_IMPORT_EXISTING
?
2143 VDEV_ALLOC_LOAD
: VDEV_ALLOC_SPLIT
);
2146 * Create "The Godfather" zio to hold all async IOs
2148 spa
->spa_async_zio_root
= zio_root(spa
, NULL
, NULL
,
2149 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_GODFATHER
);
2152 * Parse the configuration into a vdev tree. We explicitly set the
2153 * value that will be returned by spa_version() since parsing the
2154 * configuration requires knowing the version number.
2156 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2157 error
= spa_config_parse(spa
, &rvd
, nvroot
, NULL
, 0, parse
);
2158 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2163 ASSERT(spa
->spa_root_vdev
== rvd
);
2165 if (type
!= SPA_IMPORT_ASSEMBLE
) {
2166 ASSERT(spa_guid(spa
) == pool_guid
);
2170 * Try to open all vdevs, loading each label in the process.
2172 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2173 error
= vdev_open(rvd
);
2174 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2179 * We need to validate the vdev labels against the configuration that
2180 * we have in hand, which is dependent on the setting of mosconfig. If
2181 * mosconfig is true then we're validating the vdev labels based on
2182 * that config. Otherwise, we're validating against the cached config
2183 * (zpool.cache) that was read when we loaded the zfs module, and then
2184 * later we will recursively call spa_load() and validate against
2187 * If we're assembling a new pool that's been split off from an
2188 * existing pool, the labels haven't yet been updated so we skip
2189 * validation for now.
2191 if (type
!= SPA_IMPORT_ASSEMBLE
) {
2192 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2193 error
= vdev_validate(rvd
, mosconfig
);
2194 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2199 if (rvd
->vdev_state
<= VDEV_STATE_CANT_OPEN
)
2200 return (SET_ERROR(ENXIO
));
2204 * Find the best uberblock.
2206 vdev_uberblock_load(rvd
, ub
, &label
);
2209 * If we weren't able to find a single valid uberblock, return failure.
2211 if (ub
->ub_txg
== 0) {
2213 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, ENXIO
));
2217 * If the pool has an unsupported version we can't open it.
2219 if (!SPA_VERSION_IS_SUPPORTED(ub
->ub_version
)) {
2221 return (spa_vdev_err(rvd
, VDEV_AUX_VERSION_NEWER
, ENOTSUP
));
2224 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
2228 * If we weren't able to find what's necessary for reading the
2229 * MOS in the label, return failure.
2231 if (label
== NULL
|| nvlist_lookup_nvlist(label
,
2232 ZPOOL_CONFIG_FEATURES_FOR_READ
, &features
) != 0) {
2234 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
2239 * Update our in-core representation with the definitive values
2242 nvlist_free(spa
->spa_label_features
);
2243 VERIFY(nvlist_dup(features
, &spa
->spa_label_features
, 0) == 0);
2249 * Look through entries in the label nvlist's features_for_read. If
2250 * there is a feature listed there which we don't understand then we
2251 * cannot open a pool.
2253 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
2254 nvlist_t
*unsup_feat
;
2257 VERIFY(nvlist_alloc(&unsup_feat
, NV_UNIQUE_NAME
, KM_SLEEP
) ==
2260 for (nvp
= nvlist_next_nvpair(spa
->spa_label_features
, NULL
);
2262 nvp
= nvlist_next_nvpair(spa
->spa_label_features
, nvp
)) {
2263 if (!zfeature_is_supported(nvpair_name(nvp
))) {
2264 VERIFY(nvlist_add_string(unsup_feat
,
2265 nvpair_name(nvp
), "") == 0);
2269 if (!nvlist_empty(unsup_feat
)) {
2270 VERIFY(nvlist_add_nvlist(spa
->spa_load_info
,
2271 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
) == 0);
2272 nvlist_free(unsup_feat
);
2273 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
2277 nvlist_free(unsup_feat
);
2281 * If the vdev guid sum doesn't match the uberblock, we have an
2282 * incomplete configuration. We first check to see if the pool
2283 * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN).
2284 * If it is, defer the vdev_guid_sum check till later so we
2285 * can handle missing vdevs.
2287 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VDEV_CHILDREN
,
2288 &children
) != 0 && mosconfig
&& type
!= SPA_IMPORT_ASSEMBLE
&&
2289 rvd
->vdev_guid_sum
!= ub
->ub_guid_sum
)
2290 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
, ENXIO
));
2292 if (type
!= SPA_IMPORT_ASSEMBLE
&& spa
->spa_config_splitting
) {
2293 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2294 spa_try_repair(spa
, config
);
2295 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2296 nvlist_free(spa
->spa_config_splitting
);
2297 spa
->spa_config_splitting
= NULL
;
2301 * Initialize internal SPA structures.
2303 spa
->spa_state
= POOL_STATE_ACTIVE
;
2304 spa
->spa_ubsync
= spa
->spa_uberblock
;
2305 spa
->spa_verify_min_txg
= spa
->spa_extreme_rewind
?
2306 TXG_INITIAL
- 1 : spa_last_synced_txg(spa
) - TXG_DEFER_SIZE
- 1;
2307 spa
->spa_first_txg
= spa
->spa_last_ubsync_txg
?
2308 spa
->spa_last_ubsync_txg
: spa_last_synced_txg(spa
) + 1;
2309 spa
->spa_claim_max_txg
= spa
->spa_first_txg
;
2310 spa
->spa_prev_software_version
= ub
->ub_software_version
;
2312 error
= dsl_pool_init(spa
, spa
->spa_first_txg
, &spa
->spa_dsl_pool
);
2314 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2315 spa
->spa_meta_objset
= spa
->spa_dsl_pool
->dp_meta_objset
;
2317 if (spa_dir_prop(spa
, DMU_POOL_CONFIG
, &spa
->spa_config_object
) != 0)
2318 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2320 if (spa_version(spa
) >= SPA_VERSION_FEATURES
) {
2321 boolean_t missing_feat_read
= B_FALSE
;
2322 nvlist_t
*unsup_feat
, *enabled_feat
;
2324 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_READ
,
2325 &spa
->spa_feat_for_read_obj
) != 0) {
2326 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2329 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_WRITE
,
2330 &spa
->spa_feat_for_write_obj
) != 0) {
2331 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2334 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_DESCRIPTIONS
,
2335 &spa
->spa_feat_desc_obj
) != 0) {
2336 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2339 enabled_feat
= fnvlist_alloc();
2340 unsup_feat
= fnvlist_alloc();
2342 if (!feature_is_supported(spa
->spa_meta_objset
,
2343 spa
->spa_feat_for_read_obj
, spa
->spa_feat_desc_obj
,
2344 unsup_feat
, enabled_feat
))
2345 missing_feat_read
= B_TRUE
;
2347 if (spa_writeable(spa
) || state
== SPA_LOAD_TRYIMPORT
) {
2348 if (!feature_is_supported(spa
->spa_meta_objset
,
2349 spa
->spa_feat_for_write_obj
, spa
->spa_feat_desc_obj
,
2350 unsup_feat
, enabled_feat
)) {
2351 missing_feat_write
= B_TRUE
;
2355 fnvlist_add_nvlist(spa
->spa_load_info
,
2356 ZPOOL_CONFIG_ENABLED_FEAT
, enabled_feat
);
2358 if (!nvlist_empty(unsup_feat
)) {
2359 fnvlist_add_nvlist(spa
->spa_load_info
,
2360 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
);
2363 fnvlist_free(enabled_feat
);
2364 fnvlist_free(unsup_feat
);
2366 if (!missing_feat_read
) {
2367 fnvlist_add_boolean(spa
->spa_load_info
,
2368 ZPOOL_CONFIG_CAN_RDONLY
);
2372 * If the state is SPA_LOAD_TRYIMPORT, our objective is
2373 * twofold: to determine whether the pool is available for
2374 * import in read-write mode and (if it is not) whether the
2375 * pool is available for import in read-only mode. If the pool
2376 * is available for import in read-write mode, it is displayed
2377 * as available in userland; if it is not available for import
2378 * in read-only mode, it is displayed as unavailable in
2379 * userland. If the pool is available for import in read-only
2380 * mode but not read-write mode, it is displayed as unavailable
2381 * in userland with a special note that the pool is actually
2382 * available for open in read-only mode.
2384 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
2385 * missing a feature for write, we must first determine whether
2386 * the pool can be opened read-only before returning to
2387 * userland in order to know whether to display the
2388 * abovementioned note.
2390 if (missing_feat_read
|| (missing_feat_write
&&
2391 spa_writeable(spa
))) {
2392 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
2397 spa
->spa_is_initializing
= B_TRUE
;
2398 error
= dsl_pool_open(spa
->spa_dsl_pool
);
2399 spa
->spa_is_initializing
= B_FALSE
;
2401 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2405 nvlist_t
*policy
= NULL
, *nvconfig
;
2407 if (load_nvlist(spa
, spa
->spa_config_object
, &nvconfig
) != 0)
2408 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2410 if (!spa_is_root(spa
) && nvlist_lookup_uint64(nvconfig
,
2411 ZPOOL_CONFIG_HOSTID
, &hostid
) == 0) {
2413 unsigned long myhostid
= 0;
2415 VERIFY(nvlist_lookup_string(nvconfig
,
2416 ZPOOL_CONFIG_HOSTNAME
, &hostname
) == 0);
2419 myhostid
= zone_get_hostid(NULL
);
2422 * We're emulating the system's hostid in userland, so
2423 * we can't use zone_get_hostid().
2425 (void) ddi_strtoul(hw_serial
, NULL
, 10, &myhostid
);
2426 #endif /* _KERNEL */
2427 if (hostid
!= 0 && myhostid
!= 0 &&
2428 hostid
!= myhostid
) {
2429 nvlist_free(nvconfig
);
2430 cmn_err(CE_WARN
, "pool '%s' could not be "
2431 "loaded as it was last accessed by "
2432 "another system (host: %s hostid: 0x%lx). "
2433 "See: http://zfsonlinux.org/msg/ZFS-8000-EY",
2434 spa_name(spa
), hostname
,
2435 (unsigned long)hostid
);
2436 return (SET_ERROR(EBADF
));
2439 if (nvlist_lookup_nvlist(spa
->spa_config
,
2440 ZPOOL_REWIND_POLICY
, &policy
) == 0)
2441 VERIFY(nvlist_add_nvlist(nvconfig
,
2442 ZPOOL_REWIND_POLICY
, policy
) == 0);
2444 spa_config_set(spa
, nvconfig
);
2446 spa_deactivate(spa
);
2447 spa_activate(spa
, orig_mode
);
2449 return (spa_load(spa
, state
, SPA_IMPORT_EXISTING
, B_TRUE
));
2452 if (spa_dir_prop(spa
, DMU_POOL_SYNC_BPOBJ
, &obj
) != 0)
2453 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2454 error
= bpobj_open(&spa
->spa_deferred_bpobj
, spa
->spa_meta_objset
, obj
);
2456 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2459 * Load the bit that tells us to use the new accounting function
2460 * (raid-z deflation). If we have an older pool, this will not
2463 error
= spa_dir_prop(spa
, DMU_POOL_DEFLATE
, &spa
->spa_deflate
);
2464 if (error
!= 0 && error
!= ENOENT
)
2465 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2467 error
= spa_dir_prop(spa
, DMU_POOL_CREATION_VERSION
,
2468 &spa
->spa_creation_version
);
2469 if (error
!= 0 && error
!= ENOENT
)
2470 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2473 * Load the persistent error log. If we have an older pool, this will
2476 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_LAST
, &spa
->spa_errlog_last
);
2477 if (error
!= 0 && error
!= ENOENT
)
2478 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2480 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_SCRUB
,
2481 &spa
->spa_errlog_scrub
);
2482 if (error
!= 0 && error
!= ENOENT
)
2483 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2486 * Load the history object. If we have an older pool, this
2487 * will not be present.
2489 error
= spa_dir_prop(spa
, DMU_POOL_HISTORY
, &spa
->spa_history
);
2490 if (error
!= 0 && error
!= ENOENT
)
2491 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2494 * If we're assembling the pool from the split-off vdevs of
2495 * an existing pool, we don't want to attach the spares & cache
2500 * Load any hot spares for this pool.
2502 error
= spa_dir_prop(spa
, DMU_POOL_SPARES
, &spa
->spa_spares
.sav_object
);
2503 if (error
!= 0 && error
!= ENOENT
)
2504 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2505 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
2506 ASSERT(spa_version(spa
) >= SPA_VERSION_SPARES
);
2507 if (load_nvlist(spa
, spa
->spa_spares
.sav_object
,
2508 &spa
->spa_spares
.sav_config
) != 0)
2509 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2511 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2512 spa_load_spares(spa
);
2513 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2514 } else if (error
== 0) {
2515 spa
->spa_spares
.sav_sync
= B_TRUE
;
2519 * Load any level 2 ARC devices for this pool.
2521 error
= spa_dir_prop(spa
, DMU_POOL_L2CACHE
,
2522 &spa
->spa_l2cache
.sav_object
);
2523 if (error
!= 0 && error
!= ENOENT
)
2524 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2525 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
2526 ASSERT(spa_version(spa
) >= SPA_VERSION_L2CACHE
);
2527 if (load_nvlist(spa
, spa
->spa_l2cache
.sav_object
,
2528 &spa
->spa_l2cache
.sav_config
) != 0)
2529 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2531 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2532 spa_load_l2cache(spa
);
2533 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2534 } else if (error
== 0) {
2535 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
2538 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
2540 error
= spa_dir_prop(spa
, DMU_POOL_PROPS
, &spa
->spa_pool_props_object
);
2541 if (error
&& error
!= ENOENT
)
2542 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2545 uint64_t autoreplace
;
2547 spa_prop_find(spa
, ZPOOL_PROP_BOOTFS
, &spa
->spa_bootfs
);
2548 spa_prop_find(spa
, ZPOOL_PROP_AUTOREPLACE
, &autoreplace
);
2549 spa_prop_find(spa
, ZPOOL_PROP_DELEGATION
, &spa
->spa_delegation
);
2550 spa_prop_find(spa
, ZPOOL_PROP_FAILUREMODE
, &spa
->spa_failmode
);
2551 spa_prop_find(spa
, ZPOOL_PROP_AUTOEXPAND
, &spa
->spa_autoexpand
);
2552 spa_prop_find(spa
, ZPOOL_PROP_DEDUPDITTO
,
2553 &spa
->spa_dedup_ditto
);
2555 spa
->spa_autoreplace
= (autoreplace
!= 0);
2559 * If the 'autoreplace' property is set, then post a resource notifying
2560 * the ZFS DE that it should not issue any faults for unopenable
2561 * devices. We also iterate over the vdevs, and post a sysevent for any
2562 * unopenable vdevs so that the normal autoreplace handler can take
2565 if (spa
->spa_autoreplace
&& state
!= SPA_LOAD_TRYIMPORT
) {
2566 spa_check_removed(spa
->spa_root_vdev
);
2568 * For the import case, this is done in spa_import(), because
2569 * at this point we're using the spare definitions from
2570 * the MOS config, not necessarily from the userland config.
2572 if (state
!= SPA_LOAD_IMPORT
) {
2573 spa_aux_check_removed(&spa
->spa_spares
);
2574 spa_aux_check_removed(&spa
->spa_l2cache
);
2579 * Load the vdev state for all toplevel vdevs.
2584 * Propagate the leaf DTLs we just loaded all the way up the tree.
2586 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2587 vdev_dtl_reassess(rvd
, 0, 0, B_FALSE
);
2588 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2591 * Load the DDTs (dedup tables).
2593 error
= ddt_load(spa
);
2595 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2597 spa_update_dspace(spa
);
2600 * Validate the config, using the MOS config to fill in any
2601 * information which might be missing. If we fail to validate
2602 * the config then declare the pool unfit for use. If we're
2603 * assembling a pool from a split, the log is not transferred
2606 if (type
!= SPA_IMPORT_ASSEMBLE
) {
2609 if (load_nvlist(spa
, spa
->spa_config_object
, &nvconfig
) != 0)
2610 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2612 if (!spa_config_valid(spa
, nvconfig
)) {
2613 nvlist_free(nvconfig
);
2614 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
,
2617 nvlist_free(nvconfig
);
2620 * Now that we've validated the config, check the state of the
2621 * root vdev. If it can't be opened, it indicates one or
2622 * more toplevel vdevs are faulted.
2624 if (rvd
->vdev_state
<= VDEV_STATE_CANT_OPEN
)
2625 return (SET_ERROR(ENXIO
));
2627 if (spa_check_logs(spa
)) {
2628 *ereport
= FM_EREPORT_ZFS_LOG_REPLAY
;
2629 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_LOG
, ENXIO
));
2633 if (missing_feat_write
) {
2634 ASSERT(state
== SPA_LOAD_TRYIMPORT
);
2637 * At this point, we know that we can open the pool in
2638 * read-only mode but not read-write mode. We now have enough
2639 * information and can return to userland.
2641 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
, ENOTSUP
));
2645 * We've successfully opened the pool, verify that we're ready
2646 * to start pushing transactions.
2648 if (state
!= SPA_LOAD_TRYIMPORT
) {
2649 if ((error
= spa_load_verify(spa
)))
2650 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
2654 if (spa_writeable(spa
) && (state
== SPA_LOAD_RECOVER
||
2655 spa
->spa_load_max_txg
== UINT64_MAX
)) {
2657 int need_update
= B_FALSE
;
2660 ASSERT(state
!= SPA_LOAD_TRYIMPORT
);
2663 * Claim log blocks that haven't been committed yet.
2664 * This must all happen in a single txg.
2665 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
2666 * invoked from zil_claim_log_block()'s i/o done callback.
2667 * Price of rollback is that we abandon the log.
2669 spa
->spa_claiming
= B_TRUE
;
2671 tx
= dmu_tx_create_assigned(spa_get_dsl(spa
),
2672 spa_first_txg(spa
));
2673 (void) dmu_objset_find(spa_name(spa
),
2674 zil_claim
, tx
, DS_FIND_CHILDREN
);
2677 spa
->spa_claiming
= B_FALSE
;
2679 spa_set_log_state(spa
, SPA_LOG_GOOD
);
2680 spa
->spa_sync_on
= B_TRUE
;
2681 txg_sync_start(spa
->spa_dsl_pool
);
2684 * Wait for all claims to sync. We sync up to the highest
2685 * claimed log block birth time so that claimed log blocks
2686 * don't appear to be from the future. spa_claim_max_txg
2687 * will have been set for us by either zil_check_log_chain()
2688 * (invoked from spa_check_logs()) or zil_claim() above.
2690 txg_wait_synced(spa
->spa_dsl_pool
, spa
->spa_claim_max_txg
);
2693 * If the config cache is stale, or we have uninitialized
2694 * metaslabs (see spa_vdev_add()), then update the config.
2696 * If this is a verbatim import, trust the current
2697 * in-core spa_config and update the disk labels.
2699 if (config_cache_txg
!= spa
->spa_config_txg
||
2700 state
== SPA_LOAD_IMPORT
||
2701 state
== SPA_LOAD_RECOVER
||
2702 (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
))
2703 need_update
= B_TRUE
;
2705 for (c
= 0; c
< rvd
->vdev_children
; c
++)
2706 if (rvd
->vdev_child
[c
]->vdev_ms_array
== 0)
2707 need_update
= B_TRUE
;
2710 * Update the config cache asychronously in case we're the
2711 * root pool, in which case the config cache isn't writable yet.
2714 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
2717 * Check all DTLs to see if anything needs resilvering.
2719 if (!dsl_scan_resilvering(spa
->spa_dsl_pool
) &&
2720 vdev_resilver_needed(rvd
, NULL
, NULL
))
2721 spa_async_request(spa
, SPA_ASYNC_RESILVER
);
2724 * Log the fact that we booted up (so that we can detect if
2725 * we rebooted in the middle of an operation).
2727 spa_history_log_version(spa
, "open");
2730 * Delete any inconsistent datasets.
2732 (void) dmu_objset_find(spa_name(spa
),
2733 dsl_destroy_inconsistent
, NULL
, DS_FIND_CHILDREN
);
2736 * Clean up any stale temporary dataset userrefs.
2738 dsl_pool_clean_tmp_userrefs(spa
->spa_dsl_pool
);
2745 spa_load_retry(spa_t
*spa
, spa_load_state_t state
, int mosconfig
)
2747 int mode
= spa
->spa_mode
;
2750 spa_deactivate(spa
);
2752 spa
->spa_load_max_txg
--;
2754 spa_activate(spa
, mode
);
2755 spa_async_suspend(spa
);
2757 return (spa_load(spa
, state
, SPA_IMPORT_EXISTING
, mosconfig
));
2761 * If spa_load() fails this function will try loading prior txg's. If
2762 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
2763 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
2764 * function will not rewind the pool and will return the same error as
2768 spa_load_best(spa_t
*spa
, spa_load_state_t state
, int mosconfig
,
2769 uint64_t max_request
, int rewind_flags
)
2771 nvlist_t
*loadinfo
= NULL
;
2772 nvlist_t
*config
= NULL
;
2773 int load_error
, rewind_error
;
2774 uint64_t safe_rewind_txg
;
2777 if (spa
->spa_load_txg
&& state
== SPA_LOAD_RECOVER
) {
2778 spa
->spa_load_max_txg
= spa
->spa_load_txg
;
2779 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
2781 spa
->spa_load_max_txg
= max_request
;
2784 load_error
= rewind_error
= spa_load(spa
, state
, SPA_IMPORT_EXISTING
,
2786 if (load_error
== 0)
2789 if (spa
->spa_root_vdev
!= NULL
)
2790 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
2792 spa
->spa_last_ubsync_txg
= spa
->spa_uberblock
.ub_txg
;
2793 spa
->spa_last_ubsync_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
2795 if (rewind_flags
& ZPOOL_NEVER_REWIND
) {
2796 nvlist_free(config
);
2797 return (load_error
);
2800 if (state
== SPA_LOAD_RECOVER
) {
2801 /* Price of rolling back is discarding txgs, including log */
2802 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
2805 * If we aren't rolling back save the load info from our first
2806 * import attempt so that we can restore it after attempting
2809 loadinfo
= spa
->spa_load_info
;
2810 spa
->spa_load_info
= fnvlist_alloc();
2813 spa
->spa_load_max_txg
= spa
->spa_last_ubsync_txg
;
2814 safe_rewind_txg
= spa
->spa_last_ubsync_txg
- TXG_DEFER_SIZE
;
2815 min_txg
= (rewind_flags
& ZPOOL_EXTREME_REWIND
) ?
2816 TXG_INITIAL
: safe_rewind_txg
;
2819 * Continue as long as we're finding errors, we're still within
2820 * the acceptable rewind range, and we're still finding uberblocks
2822 while (rewind_error
&& spa
->spa_uberblock
.ub_txg
>= min_txg
&&
2823 spa
->spa_uberblock
.ub_txg
<= spa
->spa_load_max_txg
) {
2824 if (spa
->spa_load_max_txg
< safe_rewind_txg
)
2825 spa
->spa_extreme_rewind
= B_TRUE
;
2826 rewind_error
= spa_load_retry(spa
, state
, mosconfig
);
2829 spa
->spa_extreme_rewind
= B_FALSE
;
2830 spa
->spa_load_max_txg
= UINT64_MAX
;
2832 if (config
&& (rewind_error
|| state
!= SPA_LOAD_RECOVER
))
2833 spa_config_set(spa
, config
);
2835 if (state
== SPA_LOAD_RECOVER
) {
2836 ASSERT3P(loadinfo
, ==, NULL
);
2837 return (rewind_error
);
2839 /* Store the rewind info as part of the initial load info */
2840 fnvlist_add_nvlist(loadinfo
, ZPOOL_CONFIG_REWIND_INFO
,
2841 spa
->spa_load_info
);
2843 /* Restore the initial load info */
2844 fnvlist_free(spa
->spa_load_info
);
2845 spa
->spa_load_info
= loadinfo
;
2847 return (load_error
);
2854 * The import case is identical to an open except that the configuration is sent
2855 * down from userland, instead of grabbed from the configuration cache. For the
2856 * case of an open, the pool configuration will exist in the
2857 * POOL_STATE_UNINITIALIZED state.
2859 * The stats information (gen/count/ustats) is used to gather vdev statistics at
2860 * the same time open the pool, without having to keep around the spa_t in some
2864 spa_open_common(const char *pool
, spa_t
**spapp
, void *tag
, nvlist_t
*nvpolicy
,
2868 spa_load_state_t state
= SPA_LOAD_OPEN
;
2870 int locked
= B_FALSE
;
2871 int firstopen
= B_FALSE
;
2876 * As disgusting as this is, we need to support recursive calls to this
2877 * function because dsl_dir_open() is called during spa_load(), and ends
2878 * up calling spa_open() again. The real fix is to figure out how to
2879 * avoid dsl_dir_open() calling this in the first place.
2881 if (mutex_owner(&spa_namespace_lock
) != curthread
) {
2882 mutex_enter(&spa_namespace_lock
);
2886 if ((spa
= spa_lookup(pool
)) == NULL
) {
2888 mutex_exit(&spa_namespace_lock
);
2889 return (SET_ERROR(ENOENT
));
2892 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
) {
2893 zpool_rewind_policy_t policy
;
2897 zpool_get_rewind_policy(nvpolicy
? nvpolicy
: spa
->spa_config
,
2899 if (policy
.zrp_request
& ZPOOL_DO_REWIND
)
2900 state
= SPA_LOAD_RECOVER
;
2902 spa_activate(spa
, spa_mode_global
);
2904 if (state
!= SPA_LOAD_RECOVER
)
2905 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
2907 error
= spa_load_best(spa
, state
, B_FALSE
, policy
.zrp_txg
,
2908 policy
.zrp_request
);
2910 if (error
== EBADF
) {
2912 * If vdev_validate() returns failure (indicated by
2913 * EBADF), it indicates that one of the vdevs indicates
2914 * that the pool has been exported or destroyed. If
2915 * this is the case, the config cache is out of sync and
2916 * we should remove the pool from the namespace.
2919 spa_deactivate(spa
);
2920 spa_config_sync(spa
, B_TRUE
, B_TRUE
);
2923 mutex_exit(&spa_namespace_lock
);
2924 return (SET_ERROR(ENOENT
));
2929 * We can't open the pool, but we still have useful
2930 * information: the state of each vdev after the
2931 * attempted vdev_open(). Return this to the user.
2933 if (config
!= NULL
&& spa
->spa_config
) {
2934 VERIFY(nvlist_dup(spa
->spa_config
, config
,
2936 VERIFY(nvlist_add_nvlist(*config
,
2937 ZPOOL_CONFIG_LOAD_INFO
,
2938 spa
->spa_load_info
) == 0);
2941 spa_deactivate(spa
);
2942 spa
->spa_last_open_failed
= error
;
2944 mutex_exit(&spa_namespace_lock
);
2950 spa_open_ref(spa
, tag
);
2953 *config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
2956 * If we've recovered the pool, pass back any information we
2957 * gathered while doing the load.
2959 if (state
== SPA_LOAD_RECOVER
) {
2960 VERIFY(nvlist_add_nvlist(*config
, ZPOOL_CONFIG_LOAD_INFO
,
2961 spa
->spa_load_info
) == 0);
2965 spa
->spa_last_open_failed
= 0;
2966 spa
->spa_last_ubsync_txg
= 0;
2967 spa
->spa_load_txg
= 0;
2968 mutex_exit(&spa_namespace_lock
);
2973 zvol_create_minors(spa
->spa_name
);
2982 spa_open_rewind(const char *name
, spa_t
**spapp
, void *tag
, nvlist_t
*policy
,
2985 return (spa_open_common(name
, spapp
, tag
, policy
, config
));
2989 spa_open(const char *name
, spa_t
**spapp
, void *tag
)
2991 return (spa_open_common(name
, spapp
, tag
, NULL
, NULL
));
2995 * Lookup the given spa_t, incrementing the inject count in the process,
2996 * preventing it from being exported or destroyed.
2999 spa_inject_addref(char *name
)
3003 mutex_enter(&spa_namespace_lock
);
3004 if ((spa
= spa_lookup(name
)) == NULL
) {
3005 mutex_exit(&spa_namespace_lock
);
3008 spa
->spa_inject_ref
++;
3009 mutex_exit(&spa_namespace_lock
);
3015 spa_inject_delref(spa_t
*spa
)
3017 mutex_enter(&spa_namespace_lock
);
3018 spa
->spa_inject_ref
--;
3019 mutex_exit(&spa_namespace_lock
);
3023 * Add spares device information to the nvlist.
3026 spa_add_spares(spa_t
*spa
, nvlist_t
*config
)
3036 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
3038 if (spa
->spa_spares
.sav_count
== 0)
3041 VERIFY(nvlist_lookup_nvlist(config
,
3042 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
3043 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
3044 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
3046 VERIFY(nvlist_add_nvlist_array(nvroot
,
3047 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
3048 VERIFY(nvlist_lookup_nvlist_array(nvroot
,
3049 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
3052 * Go through and find any spares which have since been
3053 * repurposed as an active spare. If this is the case, update
3054 * their status appropriately.
3056 for (i
= 0; i
< nspares
; i
++) {
3057 VERIFY(nvlist_lookup_uint64(spares
[i
],
3058 ZPOOL_CONFIG_GUID
, &guid
) == 0);
3059 if (spa_spare_exists(guid
, &pool
, NULL
) &&
3061 VERIFY(nvlist_lookup_uint64_array(
3062 spares
[i
], ZPOOL_CONFIG_VDEV_STATS
,
3063 (uint64_t **)&vs
, &vsc
) == 0);
3064 vs
->vs_state
= VDEV_STATE_CANT_OPEN
;
3065 vs
->vs_aux
= VDEV_AUX_SPARED
;
3072 * Add l2cache device information to the nvlist, including vdev stats.
3075 spa_add_l2cache(spa_t
*spa
, nvlist_t
*config
)
3078 uint_t i
, j
, nl2cache
;
3085 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
3087 if (spa
->spa_l2cache
.sav_count
== 0)
3090 VERIFY(nvlist_lookup_nvlist(config
,
3091 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
3092 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_l2cache
.sav_config
,
3093 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
3094 if (nl2cache
!= 0) {
3095 VERIFY(nvlist_add_nvlist_array(nvroot
,
3096 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
3097 VERIFY(nvlist_lookup_nvlist_array(nvroot
,
3098 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
3101 * Update level 2 cache device stats.
3104 for (i
= 0; i
< nl2cache
; i
++) {
3105 VERIFY(nvlist_lookup_uint64(l2cache
[i
],
3106 ZPOOL_CONFIG_GUID
, &guid
) == 0);
3109 for (j
= 0; j
< spa
->spa_l2cache
.sav_count
; j
++) {
3111 spa
->spa_l2cache
.sav_vdevs
[j
]->vdev_guid
) {
3112 vd
= spa
->spa_l2cache
.sav_vdevs
[j
];
3118 VERIFY(nvlist_lookup_uint64_array(l2cache
[i
],
3119 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
, &vsc
)
3121 vdev_get_stats(vd
, vs
);
3127 spa_add_feature_stats(spa_t
*spa
, nvlist_t
*config
)
3133 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
3134 VERIFY(nvlist_alloc(&features
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
3136 if (spa
->spa_feat_for_read_obj
!= 0) {
3137 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
3138 spa
->spa_feat_for_read_obj
);
3139 zap_cursor_retrieve(&zc
, &za
) == 0;
3140 zap_cursor_advance(&zc
)) {
3141 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
3142 za
.za_num_integers
== 1);
3143 VERIFY3U(0, ==, nvlist_add_uint64(features
, za
.za_name
,
3144 za
.za_first_integer
));
3146 zap_cursor_fini(&zc
);
3149 if (spa
->spa_feat_for_write_obj
!= 0) {
3150 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
3151 spa
->spa_feat_for_write_obj
);
3152 zap_cursor_retrieve(&zc
, &za
) == 0;
3153 zap_cursor_advance(&zc
)) {
3154 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
3155 za
.za_num_integers
== 1);
3156 VERIFY3U(0, ==, nvlist_add_uint64(features
, za
.za_name
,
3157 za
.za_first_integer
));
3159 zap_cursor_fini(&zc
);
3162 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_FEATURE_STATS
,
3164 nvlist_free(features
);
3168 spa_get_stats(const char *name
, nvlist_t
**config
,
3169 char *altroot
, size_t buflen
)
3175 error
= spa_open_common(name
, &spa
, FTAG
, NULL
, config
);
3179 * This still leaves a window of inconsistency where the spares
3180 * or l2cache devices could change and the config would be
3181 * self-inconsistent.
3183 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
3185 if (*config
!= NULL
) {
3186 uint64_t loadtimes
[2];
3188 loadtimes
[0] = spa
->spa_loaded_ts
.tv_sec
;
3189 loadtimes
[1] = spa
->spa_loaded_ts
.tv_nsec
;
3190 VERIFY(nvlist_add_uint64_array(*config
,
3191 ZPOOL_CONFIG_LOADED_TIME
, loadtimes
, 2) == 0);
3193 VERIFY(nvlist_add_uint64(*config
,
3194 ZPOOL_CONFIG_ERRCOUNT
,
3195 spa_get_errlog_size(spa
)) == 0);
3197 if (spa_suspended(spa
))
3198 VERIFY(nvlist_add_uint64(*config
,
3199 ZPOOL_CONFIG_SUSPENDED
,
3200 spa
->spa_failmode
) == 0);
3202 spa_add_spares(spa
, *config
);
3203 spa_add_l2cache(spa
, *config
);
3204 spa_add_feature_stats(spa
, *config
);
3209 * We want to get the alternate root even for faulted pools, so we cheat
3210 * and call spa_lookup() directly.
3214 mutex_enter(&spa_namespace_lock
);
3215 spa
= spa_lookup(name
);
3217 spa_altroot(spa
, altroot
, buflen
);
3221 mutex_exit(&spa_namespace_lock
);
3223 spa_altroot(spa
, altroot
, buflen
);
3228 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
3229 spa_close(spa
, FTAG
);
3236 * Validate that the auxiliary device array is well formed. We must have an
3237 * array of nvlists, each which describes a valid leaf vdev. If this is an
3238 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
3239 * specified, as long as they are well-formed.
3242 spa_validate_aux_devs(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
,
3243 spa_aux_vdev_t
*sav
, const char *config
, uint64_t version
,
3244 vdev_labeltype_t label
)
3251 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
3254 * It's acceptable to have no devs specified.
3256 if (nvlist_lookup_nvlist_array(nvroot
, config
, &dev
, &ndev
) != 0)
3260 return (SET_ERROR(EINVAL
));
3263 * Make sure the pool is formatted with a version that supports this
3266 if (spa_version(spa
) < version
)
3267 return (SET_ERROR(ENOTSUP
));
3270 * Set the pending device list so we correctly handle device in-use
3273 sav
->sav_pending
= dev
;
3274 sav
->sav_npending
= ndev
;
3276 for (i
= 0; i
< ndev
; i
++) {
3277 if ((error
= spa_config_parse(spa
, &vd
, dev
[i
], NULL
, 0,
3281 if (!vd
->vdev_ops
->vdev_op_leaf
) {
3283 error
= SET_ERROR(EINVAL
);
3288 * The L2ARC currently only supports disk devices in
3289 * kernel context. For user-level testing, we allow it.
3292 if ((strcmp(config
, ZPOOL_CONFIG_L2CACHE
) == 0) &&
3293 strcmp(vd
->vdev_ops
->vdev_op_type
, VDEV_TYPE_DISK
) != 0) {
3294 error
= SET_ERROR(ENOTBLK
);
3301 if ((error
= vdev_open(vd
)) == 0 &&
3302 (error
= vdev_label_init(vd
, crtxg
, label
)) == 0) {
3303 VERIFY(nvlist_add_uint64(dev
[i
], ZPOOL_CONFIG_GUID
,
3304 vd
->vdev_guid
) == 0);
3310 (mode
!= VDEV_ALLOC_SPARE
&& mode
!= VDEV_ALLOC_L2CACHE
))
3317 sav
->sav_pending
= NULL
;
3318 sav
->sav_npending
= 0;
3323 spa_validate_aux(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
)
3327 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
3329 if ((error
= spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
3330 &spa
->spa_spares
, ZPOOL_CONFIG_SPARES
, SPA_VERSION_SPARES
,
3331 VDEV_LABEL_SPARE
)) != 0) {
3335 return (spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
3336 &spa
->spa_l2cache
, ZPOOL_CONFIG_L2CACHE
, SPA_VERSION_L2CACHE
,
3337 VDEV_LABEL_L2CACHE
));
3341 spa_set_aux_vdevs(spa_aux_vdev_t
*sav
, nvlist_t
**devs
, int ndevs
,
3346 if (sav
->sav_config
!= NULL
) {
3352 * Generate new dev list by concatentating with the
3355 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
, config
,
3356 &olddevs
, &oldndevs
) == 0);
3358 newdevs
= kmem_alloc(sizeof (void *) *
3359 (ndevs
+ oldndevs
), KM_PUSHPAGE
);
3360 for (i
= 0; i
< oldndevs
; i
++)
3361 VERIFY(nvlist_dup(olddevs
[i
], &newdevs
[i
],
3363 for (i
= 0; i
< ndevs
; i
++)
3364 VERIFY(nvlist_dup(devs
[i
], &newdevs
[i
+ oldndevs
],
3367 VERIFY(nvlist_remove(sav
->sav_config
, config
,
3368 DATA_TYPE_NVLIST_ARRAY
) == 0);
3370 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
,
3371 config
, newdevs
, ndevs
+ oldndevs
) == 0);
3372 for (i
= 0; i
< oldndevs
+ ndevs
; i
++)
3373 nvlist_free(newdevs
[i
]);
3374 kmem_free(newdevs
, (oldndevs
+ ndevs
) * sizeof (void *));
3377 * Generate a new dev list.
3379 VERIFY(nvlist_alloc(&sav
->sav_config
, NV_UNIQUE_NAME
,
3381 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
, config
,
3387 * Stop and drop level 2 ARC devices
3390 spa_l2cache_drop(spa_t
*spa
)
3394 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
3396 for (i
= 0; i
< sav
->sav_count
; i
++) {
3399 vd
= sav
->sav_vdevs
[i
];
3402 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
3403 pool
!= 0ULL && l2arc_vdev_present(vd
))
3404 l2arc_remove_vdev(vd
);
3412 spa_create(const char *pool
, nvlist_t
*nvroot
, nvlist_t
*props
,
3416 char *altroot
= NULL
;
3421 uint64_t txg
= TXG_INITIAL
;
3422 nvlist_t
**spares
, **l2cache
;
3423 uint_t nspares
, nl2cache
;
3424 uint64_t version
, obj
;
3425 boolean_t has_features
;
3430 * If this pool already exists, return failure.
3432 mutex_enter(&spa_namespace_lock
);
3433 if (spa_lookup(pool
) != NULL
) {
3434 mutex_exit(&spa_namespace_lock
);
3435 return (SET_ERROR(EEXIST
));
3439 * Allocate a new spa_t structure.
3441 (void) nvlist_lookup_string(props
,
3442 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
3443 spa
= spa_add(pool
, NULL
, altroot
);
3444 spa_activate(spa
, spa_mode_global
);
3446 if (props
&& (error
= spa_prop_validate(spa
, props
))) {
3447 spa_deactivate(spa
);
3449 mutex_exit(&spa_namespace_lock
);
3453 has_features
= B_FALSE
;
3454 for (elem
= nvlist_next_nvpair(props
, NULL
);
3455 elem
!= NULL
; elem
= nvlist_next_nvpair(props
, elem
)) {
3456 if (zpool_prop_feature(nvpair_name(elem
)))
3457 has_features
= B_TRUE
;
3460 if (has_features
|| nvlist_lookup_uint64(props
,
3461 zpool_prop_to_name(ZPOOL_PROP_VERSION
), &version
) != 0) {
3462 version
= SPA_VERSION
;
3464 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
3466 spa
->spa_first_txg
= txg
;
3467 spa
->spa_uberblock
.ub_txg
= txg
- 1;
3468 spa
->spa_uberblock
.ub_version
= version
;
3469 spa
->spa_ubsync
= spa
->spa_uberblock
;
3472 * Create "The Godfather" zio to hold all async IOs
3474 spa
->spa_async_zio_root
= zio_root(spa
, NULL
, NULL
,
3475 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_GODFATHER
);
3478 * Create the root vdev.
3480 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3482 error
= spa_config_parse(spa
, &rvd
, nvroot
, NULL
, 0, VDEV_ALLOC_ADD
);
3484 ASSERT(error
!= 0 || rvd
!= NULL
);
3485 ASSERT(error
!= 0 || spa
->spa_root_vdev
== rvd
);
3487 if (error
== 0 && !zfs_allocatable_devs(nvroot
))
3488 error
= SET_ERROR(EINVAL
);
3491 (error
= vdev_create(rvd
, txg
, B_FALSE
)) == 0 &&
3492 (error
= spa_validate_aux(spa
, nvroot
, txg
,
3493 VDEV_ALLOC_ADD
)) == 0) {
3494 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
3495 vdev_metaslab_set_size(rvd
->vdev_child
[c
]);
3496 vdev_expand(rvd
->vdev_child
[c
], txg
);
3500 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3504 spa_deactivate(spa
);
3506 mutex_exit(&spa_namespace_lock
);
3511 * Get the list of spares, if specified.
3513 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
3514 &spares
, &nspares
) == 0) {
3515 VERIFY(nvlist_alloc(&spa
->spa_spares
.sav_config
, NV_UNIQUE_NAME
,
3517 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
3518 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
3519 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3520 spa_load_spares(spa
);
3521 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3522 spa
->spa_spares
.sav_sync
= B_TRUE
;
3526 * Get the list of level 2 cache devices, if specified.
3528 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
3529 &l2cache
, &nl2cache
) == 0) {
3530 VERIFY(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
3531 NV_UNIQUE_NAME
, KM_PUSHPAGE
) == 0);
3532 VERIFY(nvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
3533 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
3534 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3535 spa_load_l2cache(spa
);
3536 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3537 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
3540 spa
->spa_is_initializing
= B_TRUE
;
3541 spa
->spa_dsl_pool
= dp
= dsl_pool_create(spa
, zplprops
, txg
);
3542 spa
->spa_meta_objset
= dp
->dp_meta_objset
;
3543 spa
->spa_is_initializing
= B_FALSE
;
3546 * Create DDTs (dedup tables).
3550 spa_update_dspace(spa
);
3552 tx
= dmu_tx_create_assigned(dp
, txg
);
3555 * Create the pool config object.
3557 spa
->spa_config_object
= dmu_object_alloc(spa
->spa_meta_objset
,
3558 DMU_OT_PACKED_NVLIST
, SPA_CONFIG_BLOCKSIZE
,
3559 DMU_OT_PACKED_NVLIST_SIZE
, sizeof (uint64_t), tx
);
3561 if (zap_add(spa
->spa_meta_objset
,
3562 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CONFIG
,
3563 sizeof (uint64_t), 1, &spa
->spa_config_object
, tx
) != 0) {
3564 cmn_err(CE_PANIC
, "failed to add pool config");
3567 if (spa_version(spa
) >= SPA_VERSION_FEATURES
)
3568 spa_feature_create_zap_objects(spa
, tx
);
3570 if (zap_add(spa
->spa_meta_objset
,
3571 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CREATION_VERSION
,
3572 sizeof (uint64_t), 1, &version
, tx
) != 0) {
3573 cmn_err(CE_PANIC
, "failed to add pool version");
3576 /* Newly created pools with the right version are always deflated. */
3577 if (version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
3578 spa
->spa_deflate
= TRUE
;
3579 if (zap_add(spa
->spa_meta_objset
,
3580 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
3581 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
) != 0) {
3582 cmn_err(CE_PANIC
, "failed to add deflate");
3587 * Create the deferred-free bpobj. Turn off compression
3588 * because sync-to-convergence takes longer if the blocksize
3591 obj
= bpobj_alloc(spa
->spa_meta_objset
, 1 << 14, tx
);
3592 dmu_object_set_compress(spa
->spa_meta_objset
, obj
,
3593 ZIO_COMPRESS_OFF
, tx
);
3594 if (zap_add(spa
->spa_meta_objset
,
3595 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_SYNC_BPOBJ
,
3596 sizeof (uint64_t), 1, &obj
, tx
) != 0) {
3597 cmn_err(CE_PANIC
, "failed to add bpobj");
3599 VERIFY3U(0, ==, bpobj_open(&spa
->spa_deferred_bpobj
,
3600 spa
->spa_meta_objset
, obj
));
3603 * Create the pool's history object.
3605 if (version
>= SPA_VERSION_ZPOOL_HISTORY
)
3606 spa_history_create_obj(spa
, tx
);
3609 * Set pool properties.
3611 spa
->spa_bootfs
= zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS
);
3612 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
3613 spa
->spa_failmode
= zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE
);
3614 spa
->spa_autoexpand
= zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND
);
3616 if (props
!= NULL
) {
3617 spa_configfile_set(spa
, props
, B_FALSE
);
3618 spa_sync_props(props
, tx
);
3623 spa
->spa_sync_on
= B_TRUE
;
3624 txg_sync_start(spa
->spa_dsl_pool
);
3627 * We explicitly wait for the first transaction to complete so that our
3628 * bean counters are appropriately updated.
3630 txg_wait_synced(spa
->spa_dsl_pool
, txg
);
3632 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
3634 spa_history_log_version(spa
, "create");
3636 spa
->spa_minref
= refcount_count(&spa
->spa_refcount
);
3638 mutex_exit(&spa_namespace_lock
);
3645 * Get the root pool information from the root disk, then import the root pool
3646 * during the system boot up time.
3648 extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t
**);
3651 spa_generate_rootconf(char *devpath
, char *devid
, uint64_t *guid
)
3654 nvlist_t
*nvtop
, *nvroot
;
3657 if (vdev_disk_read_rootlabel(devpath
, devid
, &config
) != 0)
3661 * Add this top-level vdev to the child array.
3663 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
3665 VERIFY(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
3667 VERIFY(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_GUID
, guid
) == 0);
3670 * Put this pool's top-level vdevs into a root vdev.
3672 VERIFY(nvlist_alloc(&nvroot
, NV_UNIQUE_NAME
, KM_PUSHPAGE
) == 0);
3673 VERIFY(nvlist_add_string(nvroot
, ZPOOL_CONFIG_TYPE
,
3674 VDEV_TYPE_ROOT
) == 0);
3675 VERIFY(nvlist_add_uint64(nvroot
, ZPOOL_CONFIG_ID
, 0ULL) == 0);
3676 VERIFY(nvlist_add_uint64(nvroot
, ZPOOL_CONFIG_GUID
, pgid
) == 0);
3677 VERIFY(nvlist_add_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
3681 * Replace the existing vdev_tree with the new root vdev in
3682 * this pool's configuration (remove the old, add the new).
3684 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, nvroot
) == 0);
3685 nvlist_free(nvroot
);
3690 * Walk the vdev tree and see if we can find a device with "better"
3691 * configuration. A configuration is "better" if the label on that
3692 * device has a more recent txg.
3695 spa_alt_rootvdev(vdev_t
*vd
, vdev_t
**avd
, uint64_t *txg
)
3699 for (c
= 0; c
< vd
->vdev_children
; c
++)
3700 spa_alt_rootvdev(vd
->vdev_child
[c
], avd
, txg
);
3702 if (vd
->vdev_ops
->vdev_op_leaf
) {
3706 if (vdev_disk_read_rootlabel(vd
->vdev_physpath
, vd
->vdev_devid
,
3710 VERIFY(nvlist_lookup_uint64(label
, ZPOOL_CONFIG_POOL_TXG
,
3714 * Do we have a better boot device?
3716 if (label_txg
> *txg
) {
3725 * Import a root pool.
3727 * For x86. devpath_list will consist of devid and/or physpath name of
3728 * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
3729 * The GRUB "findroot" command will return the vdev we should boot.
3731 * For Sparc, devpath_list consists the physpath name of the booting device
3732 * no matter the rootpool is a single device pool or a mirrored pool.
3734 * "/pci@1f,0/ide@d/disk@0,0:a"
3737 spa_import_rootpool(char *devpath
, char *devid
)
3740 vdev_t
*rvd
, *bvd
, *avd
= NULL
;
3741 nvlist_t
*config
, *nvtop
;
3747 * Read the label from the boot device and generate a configuration.
3749 config
= spa_generate_rootconf(devpath
, devid
, &guid
);
3750 #if defined(_OBP) && defined(_KERNEL)
3751 if (config
== NULL
) {
3752 if (strstr(devpath
, "/iscsi/ssd") != NULL
) {
3754 get_iscsi_bootpath_phy(devpath
);
3755 config
= spa_generate_rootconf(devpath
, devid
, &guid
);
3759 if (config
== NULL
) {
3760 cmn_err(CE_NOTE
, "Cannot read the pool label from '%s'",
3762 return (SET_ERROR(EIO
));
3765 VERIFY(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
3767 VERIFY(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_TXG
, &txg
) == 0);
3769 mutex_enter(&spa_namespace_lock
);
3770 if ((spa
= spa_lookup(pname
)) != NULL
) {
3772 * Remove the existing root pool from the namespace so that we
3773 * can replace it with the correct config we just read in.
3778 spa
= spa_add(pname
, config
, NULL
);
3779 spa
->spa_is_root
= B_TRUE
;
3780 spa
->spa_import_flags
= ZFS_IMPORT_VERBATIM
;
3783 * Build up a vdev tree based on the boot device's label config.
3785 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
3787 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3788 error
= spa_config_parse(spa
, &rvd
, nvtop
, NULL
, 0,
3789 VDEV_ALLOC_ROOTPOOL
);
3790 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3792 mutex_exit(&spa_namespace_lock
);
3793 nvlist_free(config
);
3794 cmn_err(CE_NOTE
, "Can not parse the config for pool '%s'",
3800 * Get the boot vdev.
3802 if ((bvd
= vdev_lookup_by_guid(rvd
, guid
)) == NULL
) {
3803 cmn_err(CE_NOTE
, "Can not find the boot vdev for guid %llu",
3804 (u_longlong_t
)guid
);
3805 error
= SET_ERROR(ENOENT
);
3810 * Determine if there is a better boot device.
3813 spa_alt_rootvdev(rvd
, &avd
, &txg
);
3815 cmn_err(CE_NOTE
, "The boot device is 'degraded'. Please "
3816 "try booting from '%s'", avd
->vdev_path
);
3817 error
= SET_ERROR(EINVAL
);
3822 * If the boot device is part of a spare vdev then ensure that
3823 * we're booting off the active spare.
3825 if (bvd
->vdev_parent
->vdev_ops
== &vdev_spare_ops
&&
3826 !bvd
->vdev_isspare
) {
3827 cmn_err(CE_NOTE
, "The boot device is currently spared. Please "
3828 "try booting from '%s'",
3830 vdev_child
[bvd
->vdev_parent
->vdev_children
- 1]->vdev_path
);
3831 error
= SET_ERROR(EINVAL
);
3837 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3839 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3840 mutex_exit(&spa_namespace_lock
);
3842 nvlist_free(config
);
3849 * Import a non-root pool into the system.
3852 spa_import(char *pool
, nvlist_t
*config
, nvlist_t
*props
, uint64_t flags
)
3855 char *altroot
= NULL
;
3856 spa_load_state_t state
= SPA_LOAD_IMPORT
;
3857 zpool_rewind_policy_t policy
;
3858 uint64_t mode
= spa_mode_global
;
3859 uint64_t readonly
= B_FALSE
;
3862 nvlist_t
**spares
, **l2cache
;
3863 uint_t nspares
, nl2cache
;
3866 * If a pool with this name exists, return failure.
3868 mutex_enter(&spa_namespace_lock
);
3869 if (spa_lookup(pool
) != NULL
) {
3870 mutex_exit(&spa_namespace_lock
);
3871 return (SET_ERROR(EEXIST
));
3875 * Create and initialize the spa structure.
3877 (void) nvlist_lookup_string(props
,
3878 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
3879 (void) nvlist_lookup_uint64(props
,
3880 zpool_prop_to_name(ZPOOL_PROP_READONLY
), &readonly
);
3883 spa
= spa_add(pool
, config
, altroot
);
3884 spa
->spa_import_flags
= flags
;
3887 * Verbatim import - Take a pool and insert it into the namespace
3888 * as if it had been loaded at boot.
3890 if (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
) {
3892 spa_configfile_set(spa
, props
, B_FALSE
);
3894 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
3896 mutex_exit(&spa_namespace_lock
);
3897 spa_history_log_version(spa
, "import");
3902 spa_activate(spa
, mode
);
3905 * Don't start async tasks until we know everything is healthy.
3907 spa_async_suspend(spa
);
3909 zpool_get_rewind_policy(config
, &policy
);
3910 if (policy
.zrp_request
& ZPOOL_DO_REWIND
)
3911 state
= SPA_LOAD_RECOVER
;
3914 * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig
3915 * because the user-supplied config is actually the one to trust when
3918 if (state
!= SPA_LOAD_RECOVER
)
3919 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
3921 error
= spa_load_best(spa
, state
, B_TRUE
, policy
.zrp_txg
,
3922 policy
.zrp_request
);
3925 * Propagate anything learned while loading the pool and pass it
3926 * back to caller (i.e. rewind info, missing devices, etc).
3928 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
3929 spa
->spa_load_info
) == 0);
3931 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3933 * Toss any existing sparelist, as it doesn't have any validity
3934 * anymore, and conflicts with spa_has_spare().
3936 if (spa
->spa_spares
.sav_config
) {
3937 nvlist_free(spa
->spa_spares
.sav_config
);
3938 spa
->spa_spares
.sav_config
= NULL
;
3939 spa_load_spares(spa
);
3941 if (spa
->spa_l2cache
.sav_config
) {
3942 nvlist_free(spa
->spa_l2cache
.sav_config
);
3943 spa
->spa_l2cache
.sav_config
= NULL
;
3944 spa_load_l2cache(spa
);
3947 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
3950 error
= spa_validate_aux(spa
, nvroot
, -1ULL,
3953 error
= spa_validate_aux(spa
, nvroot
, -1ULL,
3954 VDEV_ALLOC_L2CACHE
);
3955 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3958 spa_configfile_set(spa
, props
, B_FALSE
);
3960 if (error
!= 0 || (props
&& spa_writeable(spa
) &&
3961 (error
= spa_prop_set(spa
, props
)))) {
3963 spa_deactivate(spa
);
3965 mutex_exit(&spa_namespace_lock
);
3969 spa_async_resume(spa
);
3972 * Override any spares and level 2 cache devices as specified by
3973 * the user, as these may have correct device names/devids, etc.
3975 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
3976 &spares
, &nspares
) == 0) {
3977 if (spa
->spa_spares
.sav_config
)
3978 VERIFY(nvlist_remove(spa
->spa_spares
.sav_config
,
3979 ZPOOL_CONFIG_SPARES
, DATA_TYPE_NVLIST_ARRAY
) == 0);
3981 VERIFY(nvlist_alloc(&spa
->spa_spares
.sav_config
,
3982 NV_UNIQUE_NAME
, KM_PUSHPAGE
) == 0);
3983 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
3984 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
3985 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3986 spa_load_spares(spa
);
3987 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3988 spa
->spa_spares
.sav_sync
= B_TRUE
;
3990 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
3991 &l2cache
, &nl2cache
) == 0) {
3992 if (spa
->spa_l2cache
.sav_config
)
3993 VERIFY(nvlist_remove(spa
->spa_l2cache
.sav_config
,
3994 ZPOOL_CONFIG_L2CACHE
, DATA_TYPE_NVLIST_ARRAY
) == 0);
3996 VERIFY(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
3997 NV_UNIQUE_NAME
, KM_PUSHPAGE
) == 0);
3998 VERIFY(nvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
3999 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
4000 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4001 spa_load_l2cache(spa
);
4002 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4003 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
4007 * Check for any removed devices.
4009 if (spa
->spa_autoreplace
) {
4010 spa_aux_check_removed(&spa
->spa_spares
);
4011 spa_aux_check_removed(&spa
->spa_l2cache
);
4014 if (spa_writeable(spa
)) {
4016 * Update the config cache to include the newly-imported pool.
4018 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
4022 * It's possible that the pool was expanded while it was exported.
4023 * We kick off an async task to handle this for us.
4025 spa_async_request(spa
, SPA_ASYNC_AUTOEXPAND
);
4027 mutex_exit(&spa_namespace_lock
);
4028 spa_history_log_version(spa
, "import");
4031 zvol_create_minors(pool
);
4038 spa_tryimport(nvlist_t
*tryconfig
)
4040 nvlist_t
*config
= NULL
;
4046 if (nvlist_lookup_string(tryconfig
, ZPOOL_CONFIG_POOL_NAME
, &poolname
))
4049 if (nvlist_lookup_uint64(tryconfig
, ZPOOL_CONFIG_POOL_STATE
, &state
))
4053 * Create and initialize the spa structure.
4055 mutex_enter(&spa_namespace_lock
);
4056 spa
= spa_add(TRYIMPORT_NAME
, tryconfig
, NULL
);
4057 spa_activate(spa
, FREAD
);
4060 * Pass off the heavy lifting to spa_load().
4061 * Pass TRUE for mosconfig because the user-supplied config
4062 * is actually the one to trust when doing an import.
4064 error
= spa_load(spa
, SPA_LOAD_TRYIMPORT
, SPA_IMPORT_EXISTING
, B_TRUE
);
4067 * If 'tryconfig' was at least parsable, return the current config.
4069 if (spa
->spa_root_vdev
!= NULL
) {
4070 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
4071 VERIFY(nvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
,
4073 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
4075 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_TIMESTAMP
,
4076 spa
->spa_uberblock
.ub_timestamp
) == 0);
4077 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
4078 spa
->spa_load_info
) == 0);
4081 * If the bootfs property exists on this pool then we
4082 * copy it out so that external consumers can tell which
4083 * pools are bootable.
4085 if ((!error
|| error
== EEXIST
) && spa
->spa_bootfs
) {
4086 char *tmpname
= kmem_alloc(MAXPATHLEN
, KM_PUSHPAGE
);
4089 * We have to play games with the name since the
4090 * pool was opened as TRYIMPORT_NAME.
4092 if (dsl_dsobj_to_dsname(spa_name(spa
),
4093 spa
->spa_bootfs
, tmpname
) == 0) {
4095 char *dsname
= kmem_alloc(MAXPATHLEN
, KM_PUSHPAGE
);
4097 cp
= strchr(tmpname
, '/');
4099 (void) strlcpy(dsname
, tmpname
,
4102 (void) snprintf(dsname
, MAXPATHLEN
,
4103 "%s/%s", poolname
, ++cp
);
4105 VERIFY(nvlist_add_string(config
,
4106 ZPOOL_CONFIG_BOOTFS
, dsname
) == 0);
4107 kmem_free(dsname
, MAXPATHLEN
);
4109 kmem_free(tmpname
, MAXPATHLEN
);
4113 * Add the list of hot spares and level 2 cache devices.
4115 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
4116 spa_add_spares(spa
, config
);
4117 spa_add_l2cache(spa
, config
);
4118 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
4122 spa_deactivate(spa
);
4124 mutex_exit(&spa_namespace_lock
);
4130 * Pool export/destroy
4132 * The act of destroying or exporting a pool is very simple. We make sure there
4133 * is no more pending I/O and any references to the pool are gone. Then, we
4134 * update the pool state and sync all the labels to disk, removing the
4135 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
4136 * we don't sync the labels or remove the configuration cache.
4139 spa_export_common(char *pool
, int new_state
, nvlist_t
**oldconfig
,
4140 boolean_t force
, boolean_t hardforce
)
4147 if (!(spa_mode_global
& FWRITE
))
4148 return (SET_ERROR(EROFS
));
4150 mutex_enter(&spa_namespace_lock
);
4151 if ((spa
= spa_lookup(pool
)) == NULL
) {
4152 mutex_exit(&spa_namespace_lock
);
4153 return (SET_ERROR(ENOENT
));
4157 * Put a hold on the pool, drop the namespace lock, stop async tasks,
4158 * reacquire the namespace lock, and see if we can export.
4160 spa_open_ref(spa
, FTAG
);
4161 mutex_exit(&spa_namespace_lock
);
4162 spa_async_suspend(spa
);
4163 mutex_enter(&spa_namespace_lock
);
4164 spa_close(spa
, FTAG
);
4167 * The pool will be in core if it's openable,
4168 * in which case we can modify its state.
4170 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
&& spa
->spa_sync_on
) {
4172 * Objsets may be open only because they're dirty, so we
4173 * have to force it to sync before checking spa_refcnt.
4175 txg_wait_synced(spa
->spa_dsl_pool
, 0);
4178 * A pool cannot be exported or destroyed if there are active
4179 * references. If we are resetting a pool, allow references by
4180 * fault injection handlers.
4182 if (!spa_refcount_zero(spa
) ||
4183 (spa
->spa_inject_ref
!= 0 &&
4184 new_state
!= POOL_STATE_UNINITIALIZED
)) {
4185 spa_async_resume(spa
);
4186 mutex_exit(&spa_namespace_lock
);
4187 return (SET_ERROR(EBUSY
));
4191 * A pool cannot be exported if it has an active shared spare.
4192 * This is to prevent other pools stealing the active spare
4193 * from an exported pool. At user's own will, such pool can
4194 * be forcedly exported.
4196 if (!force
&& new_state
== POOL_STATE_EXPORTED
&&
4197 spa_has_active_shared_spare(spa
)) {
4198 spa_async_resume(spa
);
4199 mutex_exit(&spa_namespace_lock
);
4200 return (SET_ERROR(EXDEV
));
4204 * We want this to be reflected on every label,
4205 * so mark them all dirty. spa_unload() will do the
4206 * final sync that pushes these changes out.
4208 if (new_state
!= POOL_STATE_UNINITIALIZED
&& !hardforce
) {
4209 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4210 spa
->spa_state
= new_state
;
4211 spa
->spa_final_txg
= spa_last_synced_txg(spa
) +
4213 vdev_config_dirty(spa
->spa_root_vdev
);
4214 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4218 spa_event_notify(spa
, NULL
, FM_EREPORT_ZFS_POOL_DESTROY
);
4220 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
4222 spa_deactivate(spa
);
4225 if (oldconfig
&& spa
->spa_config
)
4226 VERIFY(nvlist_dup(spa
->spa_config
, oldconfig
, 0) == 0);
4228 if (new_state
!= POOL_STATE_UNINITIALIZED
) {
4230 spa_config_sync(spa
, B_TRUE
, B_TRUE
);
4233 mutex_exit(&spa_namespace_lock
);
4239 * Destroy a storage pool.
4242 spa_destroy(char *pool
)
4244 return (spa_export_common(pool
, POOL_STATE_DESTROYED
, NULL
,
4249 * Export a storage pool.
4252 spa_export(char *pool
, nvlist_t
**oldconfig
, boolean_t force
,
4253 boolean_t hardforce
)
4255 return (spa_export_common(pool
, POOL_STATE_EXPORTED
, oldconfig
,
4260 * Similar to spa_export(), this unloads the spa_t without actually removing it
4261 * from the namespace in any way.
4264 spa_reset(char *pool
)
4266 return (spa_export_common(pool
, POOL_STATE_UNINITIALIZED
, NULL
,
4271 * ==========================================================================
4272 * Device manipulation
4273 * ==========================================================================
4277 * Add a device to a storage pool.
4280 spa_vdev_add(spa_t
*spa
, nvlist_t
*nvroot
)
4284 vdev_t
*rvd
= spa
->spa_root_vdev
;
4286 nvlist_t
**spares
, **l2cache
;
4287 uint_t nspares
, nl2cache
;
4290 ASSERT(spa_writeable(spa
));
4292 txg
= spa_vdev_enter(spa
);
4294 if ((error
= spa_config_parse(spa
, &vd
, nvroot
, NULL
, 0,
4295 VDEV_ALLOC_ADD
)) != 0)
4296 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
4298 spa
->spa_pending_vdev
= vd
; /* spa_vdev_exit() will clear this */
4300 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
, &spares
,
4304 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
, &l2cache
,
4308 if (vd
->vdev_children
== 0 && nspares
== 0 && nl2cache
== 0)
4309 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
4311 if (vd
->vdev_children
!= 0 &&
4312 (error
= vdev_create(vd
, txg
, B_FALSE
)) != 0)
4313 return (spa_vdev_exit(spa
, vd
, txg
, error
));
4316 * We must validate the spares and l2cache devices after checking the
4317 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
4319 if ((error
= spa_validate_aux(spa
, nvroot
, txg
, VDEV_ALLOC_ADD
)) != 0)
4320 return (spa_vdev_exit(spa
, vd
, txg
, error
));
4323 * Transfer each new top-level vdev from vd to rvd.
4325 for (c
= 0; c
< vd
->vdev_children
; c
++) {
4328 * Set the vdev id to the first hole, if one exists.
4330 for (id
= 0; id
< rvd
->vdev_children
; id
++) {
4331 if (rvd
->vdev_child
[id
]->vdev_ishole
) {
4332 vdev_free(rvd
->vdev_child
[id
]);
4336 tvd
= vd
->vdev_child
[c
];
4337 vdev_remove_child(vd
, tvd
);
4339 vdev_add_child(rvd
, tvd
);
4340 vdev_config_dirty(tvd
);
4344 spa_set_aux_vdevs(&spa
->spa_spares
, spares
, nspares
,
4345 ZPOOL_CONFIG_SPARES
);
4346 spa_load_spares(spa
);
4347 spa
->spa_spares
.sav_sync
= B_TRUE
;
4350 if (nl2cache
!= 0) {
4351 spa_set_aux_vdevs(&spa
->spa_l2cache
, l2cache
, nl2cache
,
4352 ZPOOL_CONFIG_L2CACHE
);
4353 spa_load_l2cache(spa
);
4354 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
4358 * We have to be careful when adding new vdevs to an existing pool.
4359 * If other threads start allocating from these vdevs before we
4360 * sync the config cache, and we lose power, then upon reboot we may
4361 * fail to open the pool because there are DVAs that the config cache
4362 * can't translate. Therefore, we first add the vdevs without
4363 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
4364 * and then let spa_config_update() initialize the new metaslabs.
4366 * spa_load() checks for added-but-not-initialized vdevs, so that
4367 * if we lose power at any point in this sequence, the remaining
4368 * steps will be completed the next time we load the pool.
4370 (void) spa_vdev_exit(spa
, vd
, txg
, 0);
4372 mutex_enter(&spa_namespace_lock
);
4373 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
4374 mutex_exit(&spa_namespace_lock
);
4380 * Attach a device to a mirror. The arguments are the path to any device
4381 * in the mirror, and the nvroot for the new device. If the path specifies
4382 * a device that is not mirrored, we automatically insert the mirror vdev.
4384 * If 'replacing' is specified, the new device is intended to replace the
4385 * existing device; in this case the two devices are made into their own
4386 * mirror using the 'replacing' vdev, which is functionally identical to
4387 * the mirror vdev (it actually reuses all the same ops) but has a few
4388 * extra rules: you can't attach to it after it's been created, and upon
4389 * completion of resilvering, the first disk (the one being replaced)
4390 * is automatically detached.
4393 spa_vdev_attach(spa_t
*spa
, uint64_t guid
, nvlist_t
*nvroot
, int replacing
)
4395 uint64_t txg
, dtl_max_txg
;
4396 vdev_t
*oldvd
, *newvd
, *newrootvd
, *pvd
, *tvd
;
4398 char *oldvdpath
, *newvdpath
;
4401 ASSERTV(vdev_t
*rvd
= spa
->spa_root_vdev
);
4403 ASSERT(spa_writeable(spa
));
4405 txg
= spa_vdev_enter(spa
);
4407 oldvd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
4410 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
4412 if (!oldvd
->vdev_ops
->vdev_op_leaf
)
4413 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4415 pvd
= oldvd
->vdev_parent
;
4417 if ((error
= spa_config_parse(spa
, &newrootvd
, nvroot
, NULL
, 0,
4418 VDEV_ALLOC_ATTACH
)) != 0)
4419 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
4421 if (newrootvd
->vdev_children
!= 1)
4422 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
4424 newvd
= newrootvd
->vdev_child
[0];
4426 if (!newvd
->vdev_ops
->vdev_op_leaf
)
4427 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
4429 if ((error
= vdev_create(newrootvd
, txg
, replacing
)) != 0)
4430 return (spa_vdev_exit(spa
, newrootvd
, txg
, error
));
4433 * Spares can't replace logs
4435 if (oldvd
->vdev_top
->vdev_islog
&& newvd
->vdev_isspare
)
4436 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4440 * For attach, the only allowable parent is a mirror or the root
4443 if (pvd
->vdev_ops
!= &vdev_mirror_ops
&&
4444 pvd
->vdev_ops
!= &vdev_root_ops
)
4445 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4447 pvops
= &vdev_mirror_ops
;
4450 * Active hot spares can only be replaced by inactive hot
4453 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
4454 oldvd
->vdev_isspare
&&
4455 !spa_has_spare(spa
, newvd
->vdev_guid
))
4456 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4459 * If the source is a hot spare, and the parent isn't already a
4460 * spare, then we want to create a new hot spare. Otherwise, we
4461 * want to create a replacing vdev. The user is not allowed to
4462 * attach to a spared vdev child unless the 'isspare' state is
4463 * the same (spare replaces spare, non-spare replaces
4466 if (pvd
->vdev_ops
== &vdev_replacing_ops
&&
4467 spa_version(spa
) < SPA_VERSION_MULTI_REPLACE
) {
4468 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4469 } else if (pvd
->vdev_ops
== &vdev_spare_ops
&&
4470 newvd
->vdev_isspare
!= oldvd
->vdev_isspare
) {
4471 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4474 if (newvd
->vdev_isspare
)
4475 pvops
= &vdev_spare_ops
;
4477 pvops
= &vdev_replacing_ops
;
4481 * Make sure the new device is big enough.
4483 if (newvd
->vdev_asize
< vdev_get_min_asize(oldvd
))
4484 return (spa_vdev_exit(spa
, newrootvd
, txg
, EOVERFLOW
));
4487 * The new device cannot have a higher alignment requirement
4488 * than the top-level vdev.
4490 if (newvd
->vdev_ashift
> oldvd
->vdev_top
->vdev_ashift
)
4491 return (spa_vdev_exit(spa
, newrootvd
, txg
, EDOM
));
4494 * If this is an in-place replacement, update oldvd's path and devid
4495 * to make it distinguishable from newvd, and unopenable from now on.
4497 if (strcmp(oldvd
->vdev_path
, newvd
->vdev_path
) == 0) {
4498 spa_strfree(oldvd
->vdev_path
);
4499 oldvd
->vdev_path
= kmem_alloc(strlen(newvd
->vdev_path
) + 5,
4501 (void) sprintf(oldvd
->vdev_path
, "%s/%s",
4502 newvd
->vdev_path
, "old");
4503 if (oldvd
->vdev_devid
!= NULL
) {
4504 spa_strfree(oldvd
->vdev_devid
);
4505 oldvd
->vdev_devid
= NULL
;
4509 /* mark the device being resilvered */
4510 newvd
->vdev_resilvering
= B_TRUE
;
4513 * If the parent is not a mirror, or if we're replacing, insert the new
4514 * mirror/replacing/spare vdev above oldvd.
4516 if (pvd
->vdev_ops
!= pvops
)
4517 pvd
= vdev_add_parent(oldvd
, pvops
);
4519 ASSERT(pvd
->vdev_top
->vdev_parent
== rvd
);
4520 ASSERT(pvd
->vdev_ops
== pvops
);
4521 ASSERT(oldvd
->vdev_parent
== pvd
);
4524 * Extract the new device from its root and add it to pvd.
4526 vdev_remove_child(newrootvd
, newvd
);
4527 newvd
->vdev_id
= pvd
->vdev_children
;
4528 newvd
->vdev_crtxg
= oldvd
->vdev_crtxg
;
4529 vdev_add_child(pvd
, newvd
);
4531 tvd
= newvd
->vdev_top
;
4532 ASSERT(pvd
->vdev_top
== tvd
);
4533 ASSERT(tvd
->vdev_parent
== rvd
);
4535 vdev_config_dirty(tvd
);
4538 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
4539 * for any dmu_sync-ed blocks. It will propagate upward when
4540 * spa_vdev_exit() calls vdev_dtl_reassess().
4542 dtl_max_txg
= txg
+ TXG_CONCURRENT_STATES
;
4544 vdev_dtl_dirty(newvd
, DTL_MISSING
, TXG_INITIAL
,
4545 dtl_max_txg
- TXG_INITIAL
);
4547 if (newvd
->vdev_isspare
) {
4548 spa_spare_activate(newvd
);
4549 spa_event_notify(spa
, newvd
, FM_EREPORT_ZFS_DEVICE_SPARE
);
4552 oldvdpath
= spa_strdup(oldvd
->vdev_path
);
4553 newvdpath
= spa_strdup(newvd
->vdev_path
);
4554 newvd_isspare
= newvd
->vdev_isspare
;
4557 * Mark newvd's DTL dirty in this txg.
4559 vdev_dirty(tvd
, VDD_DTL
, newvd
, txg
);
4562 * Restart the resilver
4564 dsl_resilver_restart(spa
->spa_dsl_pool
, dtl_max_txg
);
4569 (void) spa_vdev_exit(spa
, newrootvd
, dtl_max_txg
, 0);
4571 spa_history_log_internal(spa
, "vdev attach", NULL
,
4572 "%s vdev=%s %s vdev=%s",
4573 replacing
&& newvd_isspare
? "spare in" :
4574 replacing
? "replace" : "attach", newvdpath
,
4575 replacing
? "for" : "to", oldvdpath
);
4577 spa_strfree(oldvdpath
);
4578 spa_strfree(newvdpath
);
4580 if (spa
->spa_bootfs
)
4581 spa_event_notify(spa
, newvd
, FM_EREPORT_ZFS_BOOTFS_VDEV_ATTACH
);
4587 * Detach a device from a mirror or replacing vdev.
4588 * If 'replace_done' is specified, only detach if the parent
4589 * is a replacing vdev.
4592 spa_vdev_detach(spa_t
*spa
, uint64_t guid
, uint64_t pguid
, int replace_done
)
4596 vdev_t
*vd
, *pvd
, *cvd
, *tvd
;
4597 boolean_t unspare
= B_FALSE
;
4598 uint64_t unspare_guid
= 0;
4601 ASSERTV(vdev_t
*rvd
= spa
->spa_root_vdev
);
4602 ASSERT(spa_writeable(spa
));
4604 txg
= spa_vdev_enter(spa
);
4606 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
4609 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
4611 if (!vd
->vdev_ops
->vdev_op_leaf
)
4612 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4614 pvd
= vd
->vdev_parent
;
4617 * If the parent/child relationship is not as expected, don't do it.
4618 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
4619 * vdev that's replacing B with C. The user's intent in replacing
4620 * is to go from M(A,B) to M(A,C). If the user decides to cancel
4621 * the replace by detaching C, the expected behavior is to end up
4622 * M(A,B). But suppose that right after deciding to detach C,
4623 * the replacement of B completes. We would have M(A,C), and then
4624 * ask to detach C, which would leave us with just A -- not what
4625 * the user wanted. To prevent this, we make sure that the
4626 * parent/child relationship hasn't changed -- in this example,
4627 * that C's parent is still the replacing vdev R.
4629 if (pvd
->vdev_guid
!= pguid
&& pguid
!= 0)
4630 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
4633 * Only 'replacing' or 'spare' vdevs can be replaced.
4635 if (replace_done
&& pvd
->vdev_ops
!= &vdev_replacing_ops
&&
4636 pvd
->vdev_ops
!= &vdev_spare_ops
)
4637 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4639 ASSERT(pvd
->vdev_ops
!= &vdev_spare_ops
||
4640 spa_version(spa
) >= SPA_VERSION_SPARES
);
4643 * Only mirror, replacing, and spare vdevs support detach.
4645 if (pvd
->vdev_ops
!= &vdev_replacing_ops
&&
4646 pvd
->vdev_ops
!= &vdev_mirror_ops
&&
4647 pvd
->vdev_ops
!= &vdev_spare_ops
)
4648 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4651 * If this device has the only valid copy of some data,
4652 * we cannot safely detach it.
4654 if (vdev_dtl_required(vd
))
4655 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
4657 ASSERT(pvd
->vdev_children
>= 2);
4660 * If we are detaching the second disk from a replacing vdev, then
4661 * check to see if we changed the original vdev's path to have "/old"
4662 * at the end in spa_vdev_attach(). If so, undo that change now.
4664 if (pvd
->vdev_ops
== &vdev_replacing_ops
&& vd
->vdev_id
> 0 &&
4665 vd
->vdev_path
!= NULL
) {
4666 size_t len
= strlen(vd
->vdev_path
);
4668 for (c
= 0; c
< pvd
->vdev_children
; c
++) {
4669 cvd
= pvd
->vdev_child
[c
];
4671 if (cvd
== vd
|| cvd
->vdev_path
== NULL
)
4674 if (strncmp(cvd
->vdev_path
, vd
->vdev_path
, len
) == 0 &&
4675 strcmp(cvd
->vdev_path
+ len
, "/old") == 0) {
4676 spa_strfree(cvd
->vdev_path
);
4677 cvd
->vdev_path
= spa_strdup(vd
->vdev_path
);
4684 * If we are detaching the original disk from a spare, then it implies
4685 * that the spare should become a real disk, and be removed from the
4686 * active spare list for the pool.
4688 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
4690 pvd
->vdev_child
[pvd
->vdev_children
- 1]->vdev_isspare
)
4694 * Erase the disk labels so the disk can be used for other things.
4695 * This must be done after all other error cases are handled,
4696 * but before we disembowel vd (so we can still do I/O to it).
4697 * But if we can't do it, don't treat the error as fatal --
4698 * it may be that the unwritability of the disk is the reason
4699 * it's being detached!
4701 error
= vdev_label_init(vd
, 0, VDEV_LABEL_REMOVE
);
4704 * Remove vd from its parent and compact the parent's children.
4706 vdev_remove_child(pvd
, vd
);
4707 vdev_compact_children(pvd
);
4710 * Remember one of the remaining children so we can get tvd below.
4712 cvd
= pvd
->vdev_child
[pvd
->vdev_children
- 1];
4715 * If we need to remove the remaining child from the list of hot spares,
4716 * do it now, marking the vdev as no longer a spare in the process.
4717 * We must do this before vdev_remove_parent(), because that can
4718 * change the GUID if it creates a new toplevel GUID. For a similar
4719 * reason, we must remove the spare now, in the same txg as the detach;
4720 * otherwise someone could attach a new sibling, change the GUID, and
4721 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
4724 ASSERT(cvd
->vdev_isspare
);
4725 spa_spare_remove(cvd
);
4726 unspare_guid
= cvd
->vdev_guid
;
4727 (void) spa_vdev_remove(spa
, unspare_guid
, B_TRUE
);
4728 cvd
->vdev_unspare
= B_TRUE
;
4732 * If the parent mirror/replacing vdev only has one child,
4733 * the parent is no longer needed. Remove it from the tree.
4735 if (pvd
->vdev_children
== 1) {
4736 if (pvd
->vdev_ops
== &vdev_spare_ops
)
4737 cvd
->vdev_unspare
= B_FALSE
;
4738 vdev_remove_parent(cvd
);
4739 cvd
->vdev_resilvering
= B_FALSE
;
4744 * We don't set tvd until now because the parent we just removed
4745 * may have been the previous top-level vdev.
4747 tvd
= cvd
->vdev_top
;
4748 ASSERT(tvd
->vdev_parent
== rvd
);
4751 * Reevaluate the parent vdev state.
4753 vdev_propagate_state(cvd
);
4756 * If the 'autoexpand' property is set on the pool then automatically
4757 * try to expand the size of the pool. For example if the device we
4758 * just detached was smaller than the others, it may be possible to
4759 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
4760 * first so that we can obtain the updated sizes of the leaf vdevs.
4762 if (spa
->spa_autoexpand
) {
4764 vdev_expand(tvd
, txg
);
4767 vdev_config_dirty(tvd
);
4770 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
4771 * vd->vdev_detached is set and free vd's DTL object in syncing context.
4772 * But first make sure we're not on any *other* txg's DTL list, to
4773 * prevent vd from being accessed after it's freed.
4775 vdpath
= spa_strdup(vd
->vdev_path
);
4776 for (t
= 0; t
< TXG_SIZE
; t
++)
4777 (void) txg_list_remove_this(&tvd
->vdev_dtl_list
, vd
, t
);
4778 vd
->vdev_detached
= B_TRUE
;
4779 vdev_dirty(tvd
, VDD_DTL
, vd
, txg
);
4781 spa_event_notify(spa
, vd
, FM_EREPORT_ZFS_DEVICE_REMOVE
);
4783 /* hang on to the spa before we release the lock */
4784 spa_open_ref(spa
, FTAG
);
4786 error
= spa_vdev_exit(spa
, vd
, txg
, 0);
4788 spa_history_log_internal(spa
, "detach", NULL
,
4790 spa_strfree(vdpath
);
4793 * If this was the removal of the original device in a hot spare vdev,
4794 * then we want to go through and remove the device from the hot spare
4795 * list of every other pool.
4798 spa_t
*altspa
= NULL
;
4800 mutex_enter(&spa_namespace_lock
);
4801 while ((altspa
= spa_next(altspa
)) != NULL
) {
4802 if (altspa
->spa_state
!= POOL_STATE_ACTIVE
||
4806 spa_open_ref(altspa
, FTAG
);
4807 mutex_exit(&spa_namespace_lock
);
4808 (void) spa_vdev_remove(altspa
, unspare_guid
, B_TRUE
);
4809 mutex_enter(&spa_namespace_lock
);
4810 spa_close(altspa
, FTAG
);
4812 mutex_exit(&spa_namespace_lock
);
4814 /* search the rest of the vdevs for spares to remove */
4815 spa_vdev_resilver_done(spa
);
4818 /* all done with the spa; OK to release */
4819 mutex_enter(&spa_namespace_lock
);
4820 spa_close(spa
, FTAG
);
4821 mutex_exit(&spa_namespace_lock
);
4827 * Split a set of devices from their mirrors, and create a new pool from them.
4830 spa_vdev_split_mirror(spa_t
*spa
, char *newname
, nvlist_t
*config
,
4831 nvlist_t
*props
, boolean_t exp
)
4834 uint64_t txg
, *glist
;
4836 uint_t c
, children
, lastlog
;
4837 nvlist_t
**child
, *nvl
, *tmp
;
4839 char *altroot
= NULL
;
4840 vdev_t
*rvd
, **vml
= NULL
; /* vdev modify list */
4841 boolean_t activate_slog
;
4843 ASSERT(spa_writeable(spa
));
4845 txg
= spa_vdev_enter(spa
);
4847 /* clear the log and flush everything up to now */
4848 activate_slog
= spa_passivate_log(spa
);
4849 (void) spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
4850 error
= spa_offline_log(spa
);
4851 txg
= spa_vdev_config_enter(spa
);
4854 spa_activate_log(spa
);
4857 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
4859 /* check new spa name before going any further */
4860 if (spa_lookup(newname
) != NULL
)
4861 return (spa_vdev_exit(spa
, NULL
, txg
, EEXIST
));
4864 * scan through all the children to ensure they're all mirrors
4866 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvl
) != 0 ||
4867 nvlist_lookup_nvlist_array(nvl
, ZPOOL_CONFIG_CHILDREN
, &child
,
4869 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
4871 /* first, check to ensure we've got the right child count */
4872 rvd
= spa
->spa_root_vdev
;
4874 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
4875 vdev_t
*vd
= rvd
->vdev_child
[c
];
4877 /* don't count the holes & logs as children */
4878 if (vd
->vdev_islog
|| vd
->vdev_ishole
) {
4886 if (children
!= (lastlog
!= 0 ? lastlog
: rvd
->vdev_children
))
4887 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
4889 /* next, ensure no spare or cache devices are part of the split */
4890 if (nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_SPARES
, &tmp
) == 0 ||
4891 nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_L2CACHE
, &tmp
) == 0)
4892 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
4894 vml
= kmem_zalloc(children
* sizeof (vdev_t
*), KM_PUSHPAGE
);
4895 glist
= kmem_zalloc(children
* sizeof (uint64_t), KM_PUSHPAGE
);
4897 /* then, loop over each vdev and validate it */
4898 for (c
= 0; c
< children
; c
++) {
4899 uint64_t is_hole
= 0;
4901 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
4905 if (spa
->spa_root_vdev
->vdev_child
[c
]->vdev_ishole
||
4906 spa
->spa_root_vdev
->vdev_child
[c
]->vdev_islog
) {
4909 error
= SET_ERROR(EINVAL
);
4914 /* which disk is going to be split? */
4915 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_GUID
,
4917 error
= SET_ERROR(EINVAL
);
4921 /* look it up in the spa */
4922 vml
[c
] = spa_lookup_by_guid(spa
, glist
[c
], B_FALSE
);
4923 if (vml
[c
] == NULL
) {
4924 error
= SET_ERROR(ENODEV
);
4928 /* make sure there's nothing stopping the split */
4929 if (vml
[c
]->vdev_parent
->vdev_ops
!= &vdev_mirror_ops
||
4930 vml
[c
]->vdev_islog
||
4931 vml
[c
]->vdev_ishole
||
4932 vml
[c
]->vdev_isspare
||
4933 vml
[c
]->vdev_isl2cache
||
4934 !vdev_writeable(vml
[c
]) ||
4935 vml
[c
]->vdev_children
!= 0 ||
4936 vml
[c
]->vdev_state
!= VDEV_STATE_HEALTHY
||
4937 c
!= spa
->spa_root_vdev
->vdev_child
[c
]->vdev_id
) {
4938 error
= SET_ERROR(EINVAL
);
4942 if (vdev_dtl_required(vml
[c
])) {
4943 error
= SET_ERROR(EBUSY
);
4947 /* we need certain info from the top level */
4948 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_ARRAY
,
4949 vml
[c
]->vdev_top
->vdev_ms_array
) == 0);
4950 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_SHIFT
,
4951 vml
[c
]->vdev_top
->vdev_ms_shift
) == 0);
4952 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASIZE
,
4953 vml
[c
]->vdev_top
->vdev_asize
) == 0);
4954 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASHIFT
,
4955 vml
[c
]->vdev_top
->vdev_ashift
) == 0);
4959 kmem_free(vml
, children
* sizeof (vdev_t
*));
4960 kmem_free(glist
, children
* sizeof (uint64_t));
4961 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
4964 /* stop writers from using the disks */
4965 for (c
= 0; c
< children
; c
++) {
4967 vml
[c
]->vdev_offline
= B_TRUE
;
4969 vdev_reopen(spa
->spa_root_vdev
);
4972 * Temporarily record the splitting vdevs in the spa config. This
4973 * will disappear once the config is regenerated.
4975 VERIFY(nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, KM_PUSHPAGE
) == 0);
4976 VERIFY(nvlist_add_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
4977 glist
, children
) == 0);
4978 kmem_free(glist
, children
* sizeof (uint64_t));
4980 mutex_enter(&spa
->spa_props_lock
);
4981 VERIFY(nvlist_add_nvlist(spa
->spa_config
, ZPOOL_CONFIG_SPLIT
,
4983 mutex_exit(&spa
->spa_props_lock
);
4984 spa
->spa_config_splitting
= nvl
;
4985 vdev_config_dirty(spa
->spa_root_vdev
);
4987 /* configure and create the new pool */
4988 VERIFY(nvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
, newname
) == 0);
4989 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
4990 exp
? POOL_STATE_EXPORTED
: POOL_STATE_ACTIVE
) == 0);
4991 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
4992 spa_version(spa
)) == 0);
4993 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
4994 spa
->spa_config_txg
) == 0);
4995 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
4996 spa_generate_guid(NULL
)) == 0);
4997 (void) nvlist_lookup_string(props
,
4998 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
5000 /* add the new pool to the namespace */
5001 newspa
= spa_add(newname
, config
, altroot
);
5002 newspa
->spa_config_txg
= spa
->spa_config_txg
;
5003 spa_set_log_state(newspa
, SPA_LOG_CLEAR
);
5005 /* release the spa config lock, retaining the namespace lock */
5006 spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
5008 if (zio_injection_enabled
)
5009 zio_handle_panic_injection(spa
, FTAG
, 1);
5011 spa_activate(newspa
, spa_mode_global
);
5012 spa_async_suspend(newspa
);
5014 /* create the new pool from the disks of the original pool */
5015 error
= spa_load(newspa
, SPA_LOAD_IMPORT
, SPA_IMPORT_ASSEMBLE
, B_TRUE
);
5019 /* if that worked, generate a real config for the new pool */
5020 if (newspa
->spa_root_vdev
!= NULL
) {
5021 VERIFY(nvlist_alloc(&newspa
->spa_config_splitting
,
5022 NV_UNIQUE_NAME
, KM_PUSHPAGE
) == 0);
5023 VERIFY(nvlist_add_uint64(newspa
->spa_config_splitting
,
5024 ZPOOL_CONFIG_SPLIT_GUID
, spa_guid(spa
)) == 0);
5025 spa_config_set(newspa
, spa_config_generate(newspa
, NULL
, -1ULL,
5030 if (props
!= NULL
) {
5031 spa_configfile_set(newspa
, props
, B_FALSE
);
5032 error
= spa_prop_set(newspa
, props
);
5037 /* flush everything */
5038 txg
= spa_vdev_config_enter(newspa
);
5039 vdev_config_dirty(newspa
->spa_root_vdev
);
5040 (void) spa_vdev_config_exit(newspa
, NULL
, txg
, 0, FTAG
);
5042 if (zio_injection_enabled
)
5043 zio_handle_panic_injection(spa
, FTAG
, 2);
5045 spa_async_resume(newspa
);
5047 /* finally, update the original pool's config */
5048 txg
= spa_vdev_config_enter(spa
);
5049 tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
5050 error
= dmu_tx_assign(tx
, TXG_WAIT
);
5053 for (c
= 0; c
< children
; c
++) {
5054 if (vml
[c
] != NULL
) {
5057 spa_history_log_internal(spa
, "detach", tx
,
5058 "vdev=%s", vml
[c
]->vdev_path
);
5062 vdev_config_dirty(spa
->spa_root_vdev
);
5063 spa
->spa_config_splitting
= NULL
;
5067 (void) spa_vdev_exit(spa
, NULL
, txg
, 0);
5069 if (zio_injection_enabled
)
5070 zio_handle_panic_injection(spa
, FTAG
, 3);
5072 /* split is complete; log a history record */
5073 spa_history_log_internal(newspa
, "split", NULL
,
5074 "from pool %s", spa_name(spa
));
5076 kmem_free(vml
, children
* sizeof (vdev_t
*));
5078 /* if we're not going to mount the filesystems in userland, export */
5080 error
= spa_export_common(newname
, POOL_STATE_EXPORTED
, NULL
,
5087 spa_deactivate(newspa
);
5090 txg
= spa_vdev_config_enter(spa
);
5092 /* re-online all offlined disks */
5093 for (c
= 0; c
< children
; c
++) {
5095 vml
[c
]->vdev_offline
= B_FALSE
;
5097 vdev_reopen(spa
->spa_root_vdev
);
5099 nvlist_free(spa
->spa_config_splitting
);
5100 spa
->spa_config_splitting
= NULL
;
5101 (void) spa_vdev_exit(spa
, NULL
, txg
, error
);
5103 kmem_free(vml
, children
* sizeof (vdev_t
*));
5108 spa_nvlist_lookup_by_guid(nvlist_t
**nvpp
, int count
, uint64_t target_guid
)
5112 for (i
= 0; i
< count
; i
++) {
5115 VERIFY(nvlist_lookup_uint64(nvpp
[i
], ZPOOL_CONFIG_GUID
,
5118 if (guid
== target_guid
)
5126 spa_vdev_remove_aux(nvlist_t
*config
, char *name
, nvlist_t
**dev
, int count
,
5127 nvlist_t
*dev_to_remove
)
5129 nvlist_t
**newdev
= NULL
;
5133 newdev
= kmem_alloc((count
- 1) * sizeof (void *), KM_PUSHPAGE
);
5135 for (i
= 0, j
= 0; i
< count
; i
++) {
5136 if (dev
[i
] == dev_to_remove
)
5138 VERIFY(nvlist_dup(dev
[i
], &newdev
[j
++], KM_PUSHPAGE
) == 0);
5141 VERIFY(nvlist_remove(config
, name
, DATA_TYPE_NVLIST_ARRAY
) == 0);
5142 VERIFY(nvlist_add_nvlist_array(config
, name
, newdev
, count
- 1) == 0);
5144 for (i
= 0; i
< count
- 1; i
++)
5145 nvlist_free(newdev
[i
]);
5148 kmem_free(newdev
, (count
- 1) * sizeof (void *));
5152 * Evacuate the device.
5155 spa_vdev_remove_evacuate(spa_t
*spa
, vdev_t
*vd
)
5160 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
5161 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
5162 ASSERT(vd
== vd
->vdev_top
);
5165 * Evacuate the device. We don't hold the config lock as writer
5166 * since we need to do I/O but we do keep the
5167 * spa_namespace_lock held. Once this completes the device
5168 * should no longer have any blocks allocated on it.
5170 if (vd
->vdev_islog
) {
5171 if (vd
->vdev_stat
.vs_alloc
!= 0)
5172 error
= spa_offline_log(spa
);
5174 error
= SET_ERROR(ENOTSUP
);
5181 * The evacuation succeeded. Remove any remaining MOS metadata
5182 * associated with this vdev, and wait for these changes to sync.
5184 ASSERT0(vd
->vdev_stat
.vs_alloc
);
5185 txg
= spa_vdev_config_enter(spa
);
5186 vd
->vdev_removing
= B_TRUE
;
5187 vdev_dirty(vd
, 0, NULL
, txg
);
5188 vdev_config_dirty(vd
);
5189 spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
5195 * Complete the removal by cleaning up the namespace.
5198 spa_vdev_remove_from_namespace(spa_t
*spa
, vdev_t
*vd
)
5200 vdev_t
*rvd
= spa
->spa_root_vdev
;
5201 uint64_t id
= vd
->vdev_id
;
5202 boolean_t last_vdev
= (id
== (rvd
->vdev_children
- 1));
5204 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
5205 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
5206 ASSERT(vd
== vd
->vdev_top
);
5209 * Only remove any devices which are empty.
5211 if (vd
->vdev_stat
.vs_alloc
!= 0)
5214 (void) vdev_label_init(vd
, 0, VDEV_LABEL_REMOVE
);
5216 if (list_link_active(&vd
->vdev_state_dirty_node
))
5217 vdev_state_clean(vd
);
5218 if (list_link_active(&vd
->vdev_config_dirty_node
))
5219 vdev_config_clean(vd
);
5224 vdev_compact_children(rvd
);
5226 vd
= vdev_alloc_common(spa
, id
, 0, &vdev_hole_ops
);
5227 vdev_add_child(rvd
, vd
);
5229 vdev_config_dirty(rvd
);
5232 * Reassess the health of our root vdev.
5238 * Remove a device from the pool -
5240 * Removing a device from the vdev namespace requires several steps
5241 * and can take a significant amount of time. As a result we use
5242 * the spa_vdev_config_[enter/exit] functions which allow us to
5243 * grab and release the spa_config_lock while still holding the namespace
5244 * lock. During each step the configuration is synced out.
5248 * Remove a device from the pool. Currently, this supports removing only hot
5249 * spares, slogs, and level 2 ARC devices.
5252 spa_vdev_remove(spa_t
*spa
, uint64_t guid
, boolean_t unspare
)
5255 metaslab_group_t
*mg
;
5256 nvlist_t
**spares
, **l2cache
, *nv
;
5258 uint_t nspares
, nl2cache
;
5260 boolean_t locked
= MUTEX_HELD(&spa_namespace_lock
);
5262 ASSERT(spa_writeable(spa
));
5265 txg
= spa_vdev_enter(spa
);
5267 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
5269 if (spa
->spa_spares
.sav_vdevs
!= NULL
&&
5270 nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
5271 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0 &&
5272 (nv
= spa_nvlist_lookup_by_guid(spares
, nspares
, guid
)) != NULL
) {
5274 * Only remove the hot spare if it's not currently in use
5277 if (vd
== NULL
|| unspare
) {
5278 spa_vdev_remove_aux(spa
->spa_spares
.sav_config
,
5279 ZPOOL_CONFIG_SPARES
, spares
, nspares
, nv
);
5280 spa_load_spares(spa
);
5281 spa
->spa_spares
.sav_sync
= B_TRUE
;
5283 error
= SET_ERROR(EBUSY
);
5285 } else if (spa
->spa_l2cache
.sav_vdevs
!= NULL
&&
5286 nvlist_lookup_nvlist_array(spa
->spa_l2cache
.sav_config
,
5287 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0 &&
5288 (nv
= spa_nvlist_lookup_by_guid(l2cache
, nl2cache
, guid
)) != NULL
) {
5290 * Cache devices can always be removed.
5292 spa_vdev_remove_aux(spa
->spa_l2cache
.sav_config
,
5293 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
, nv
);
5294 spa_load_l2cache(spa
);
5295 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
5296 } else if (vd
!= NULL
&& vd
->vdev_islog
) {
5298 ASSERT(vd
== vd
->vdev_top
);
5301 * XXX - Once we have bp-rewrite this should
5302 * become the common case.
5308 * Stop allocating from this vdev.
5310 metaslab_group_passivate(mg
);
5313 * Wait for the youngest allocations and frees to sync,
5314 * and then wait for the deferral of those frees to finish.
5316 spa_vdev_config_exit(spa
, NULL
,
5317 txg
+ TXG_CONCURRENT_STATES
+ TXG_DEFER_SIZE
, 0, FTAG
);
5320 * Attempt to evacuate the vdev.
5322 error
= spa_vdev_remove_evacuate(spa
, vd
);
5324 txg
= spa_vdev_config_enter(spa
);
5327 * If we couldn't evacuate the vdev, unwind.
5330 metaslab_group_activate(mg
);
5331 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5335 * Clean up the vdev namespace.
5337 spa_vdev_remove_from_namespace(spa
, vd
);
5339 } else if (vd
!= NULL
) {
5341 * Normal vdevs cannot be removed (yet).
5343 error
= SET_ERROR(ENOTSUP
);
5346 * There is no vdev of any kind with the specified guid.
5348 error
= SET_ERROR(ENOENT
);
5352 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5358 * Find any device that's done replacing, or a vdev marked 'unspare' that's
5359 * current spared, so we can detach it.
5362 spa_vdev_resilver_done_hunt(vdev_t
*vd
)
5364 vdev_t
*newvd
, *oldvd
;
5367 for (c
= 0; c
< vd
->vdev_children
; c
++) {
5368 oldvd
= spa_vdev_resilver_done_hunt(vd
->vdev_child
[c
]);
5374 * Check for a completed replacement. We always consider the first
5375 * vdev in the list to be the oldest vdev, and the last one to be
5376 * the newest (see spa_vdev_attach() for how that works). In
5377 * the case where the newest vdev is faulted, we will not automatically
5378 * remove it after a resilver completes. This is OK as it will require
5379 * user intervention to determine which disk the admin wishes to keep.
5381 if (vd
->vdev_ops
== &vdev_replacing_ops
) {
5382 ASSERT(vd
->vdev_children
> 1);
5384 newvd
= vd
->vdev_child
[vd
->vdev_children
- 1];
5385 oldvd
= vd
->vdev_child
[0];
5387 if (vdev_dtl_empty(newvd
, DTL_MISSING
) &&
5388 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
5389 !vdev_dtl_required(oldvd
))
5394 * Check for a completed resilver with the 'unspare' flag set.
5396 if (vd
->vdev_ops
== &vdev_spare_ops
) {
5397 vdev_t
*first
= vd
->vdev_child
[0];
5398 vdev_t
*last
= vd
->vdev_child
[vd
->vdev_children
- 1];
5400 if (last
->vdev_unspare
) {
5403 } else if (first
->vdev_unspare
) {
5410 if (oldvd
!= NULL
&&
5411 vdev_dtl_empty(newvd
, DTL_MISSING
) &&
5412 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
5413 !vdev_dtl_required(oldvd
))
5417 * If there are more than two spares attached to a disk,
5418 * and those spares are not required, then we want to
5419 * attempt to free them up now so that they can be used
5420 * by other pools. Once we're back down to a single
5421 * disk+spare, we stop removing them.
5423 if (vd
->vdev_children
> 2) {
5424 newvd
= vd
->vdev_child
[1];
5426 if (newvd
->vdev_isspare
&& last
->vdev_isspare
&&
5427 vdev_dtl_empty(last
, DTL_MISSING
) &&
5428 vdev_dtl_empty(last
, DTL_OUTAGE
) &&
5429 !vdev_dtl_required(newvd
))
5438 spa_vdev_resilver_done(spa_t
*spa
)
5440 vdev_t
*vd
, *pvd
, *ppvd
;
5441 uint64_t guid
, sguid
, pguid
, ppguid
;
5443 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5445 while ((vd
= spa_vdev_resilver_done_hunt(spa
->spa_root_vdev
)) != NULL
) {
5446 pvd
= vd
->vdev_parent
;
5447 ppvd
= pvd
->vdev_parent
;
5448 guid
= vd
->vdev_guid
;
5449 pguid
= pvd
->vdev_guid
;
5450 ppguid
= ppvd
->vdev_guid
;
5453 * If we have just finished replacing a hot spared device, then
5454 * we need to detach the parent's first child (the original hot
5457 if (ppvd
->vdev_ops
== &vdev_spare_ops
&& pvd
->vdev_id
== 0 &&
5458 ppvd
->vdev_children
== 2) {
5459 ASSERT(pvd
->vdev_ops
== &vdev_replacing_ops
);
5460 sguid
= ppvd
->vdev_child
[1]->vdev_guid
;
5462 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5463 if (spa_vdev_detach(spa
, guid
, pguid
, B_TRUE
) != 0)
5465 if (sguid
&& spa_vdev_detach(spa
, sguid
, ppguid
, B_TRUE
) != 0)
5467 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5470 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5474 * Update the stored path or FRU for this vdev.
5477 spa_vdev_set_common(spa_t
*spa
, uint64_t guid
, const char *value
,
5481 boolean_t sync
= B_FALSE
;
5483 ASSERT(spa_writeable(spa
));
5485 spa_vdev_state_enter(spa
, SCL_ALL
);
5487 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
5488 return (spa_vdev_state_exit(spa
, NULL
, ENOENT
));
5490 if (!vd
->vdev_ops
->vdev_op_leaf
)
5491 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
5494 if (strcmp(value
, vd
->vdev_path
) != 0) {
5495 spa_strfree(vd
->vdev_path
);
5496 vd
->vdev_path
= spa_strdup(value
);
5500 if (vd
->vdev_fru
== NULL
) {
5501 vd
->vdev_fru
= spa_strdup(value
);
5503 } else if (strcmp(value
, vd
->vdev_fru
) != 0) {
5504 spa_strfree(vd
->vdev_fru
);
5505 vd
->vdev_fru
= spa_strdup(value
);
5510 return (spa_vdev_state_exit(spa
, sync
? vd
: NULL
, 0));
5514 spa_vdev_setpath(spa_t
*spa
, uint64_t guid
, const char *newpath
)
5516 return (spa_vdev_set_common(spa
, guid
, newpath
, B_TRUE
));
5520 spa_vdev_setfru(spa_t
*spa
, uint64_t guid
, const char *newfru
)
5522 return (spa_vdev_set_common(spa
, guid
, newfru
, B_FALSE
));
5526 * ==========================================================================
5528 * ==========================================================================
5532 spa_scan_stop(spa_t
*spa
)
5534 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
5535 if (dsl_scan_resilvering(spa
->spa_dsl_pool
))
5536 return (SET_ERROR(EBUSY
));
5537 return (dsl_scan_cancel(spa
->spa_dsl_pool
));
5541 spa_scan(spa_t
*spa
, pool_scan_func_t func
)
5543 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
5545 if (func
>= POOL_SCAN_FUNCS
|| func
== POOL_SCAN_NONE
)
5546 return (SET_ERROR(ENOTSUP
));
5549 * If a resilver was requested, but there is no DTL on a
5550 * writeable leaf device, we have nothing to do.
5552 if (func
== POOL_SCAN_RESILVER
&&
5553 !vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
)) {
5554 spa_async_request(spa
, SPA_ASYNC_RESILVER_DONE
);
5558 return (dsl_scan(spa
->spa_dsl_pool
, func
));
5562 * ==========================================================================
5563 * SPA async task processing
5564 * ==========================================================================
5568 spa_async_remove(spa_t
*spa
, vdev_t
*vd
)
5572 if (vd
->vdev_remove_wanted
) {
5573 vd
->vdev_remove_wanted
= B_FALSE
;
5574 vd
->vdev_delayed_close
= B_FALSE
;
5575 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_REMOVED
, VDEV_AUX_NONE
);
5578 * We want to clear the stats, but we don't want to do a full
5579 * vdev_clear() as that will cause us to throw away
5580 * degraded/faulted state as well as attempt to reopen the
5581 * device, all of which is a waste.
5583 vd
->vdev_stat
.vs_read_errors
= 0;
5584 vd
->vdev_stat
.vs_write_errors
= 0;
5585 vd
->vdev_stat
.vs_checksum_errors
= 0;
5587 vdev_state_dirty(vd
->vdev_top
);
5590 for (c
= 0; c
< vd
->vdev_children
; c
++)
5591 spa_async_remove(spa
, vd
->vdev_child
[c
]);
5595 spa_async_probe(spa_t
*spa
, vdev_t
*vd
)
5599 if (vd
->vdev_probe_wanted
) {
5600 vd
->vdev_probe_wanted
= B_FALSE
;
5601 vdev_reopen(vd
); /* vdev_open() does the actual probe */
5604 for (c
= 0; c
< vd
->vdev_children
; c
++)
5605 spa_async_probe(spa
, vd
->vdev_child
[c
]);
5609 spa_async_autoexpand(spa_t
*spa
, vdev_t
*vd
)
5613 if (!spa
->spa_autoexpand
)
5616 for (c
= 0; c
< vd
->vdev_children
; c
++) {
5617 vdev_t
*cvd
= vd
->vdev_child
[c
];
5618 spa_async_autoexpand(spa
, cvd
);
5621 if (!vd
->vdev_ops
->vdev_op_leaf
|| vd
->vdev_physpath
== NULL
)
5624 spa_event_notify(vd
->vdev_spa
, vd
, FM_EREPORT_ZFS_DEVICE_AUTOEXPAND
);
5628 spa_async_thread(spa_t
*spa
)
5632 ASSERT(spa
->spa_sync_on
);
5634 mutex_enter(&spa
->spa_async_lock
);
5635 tasks
= spa
->spa_async_tasks
;
5636 spa
->spa_async_tasks
= 0;
5637 mutex_exit(&spa
->spa_async_lock
);
5640 * See if the config needs to be updated.
5642 if (tasks
& SPA_ASYNC_CONFIG_UPDATE
) {
5643 uint64_t old_space
, new_space
;
5645 mutex_enter(&spa_namespace_lock
);
5646 old_space
= metaslab_class_get_space(spa_normal_class(spa
));
5647 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
5648 new_space
= metaslab_class_get_space(spa_normal_class(spa
));
5649 mutex_exit(&spa_namespace_lock
);
5652 * If the pool grew as a result of the config update,
5653 * then log an internal history event.
5655 if (new_space
!= old_space
) {
5656 spa_history_log_internal(spa
, "vdev online", NULL
,
5657 "pool '%s' size: %llu(+%llu)",
5658 spa_name(spa
), new_space
, new_space
- old_space
);
5663 * See if any devices need to be marked REMOVED.
5665 if (tasks
& SPA_ASYNC_REMOVE
) {
5666 spa_vdev_state_enter(spa
, SCL_NONE
);
5667 spa_async_remove(spa
, spa
->spa_root_vdev
);
5668 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++)
5669 spa_async_remove(spa
, spa
->spa_l2cache
.sav_vdevs
[i
]);
5670 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
5671 spa_async_remove(spa
, spa
->spa_spares
.sav_vdevs
[i
]);
5672 (void) spa_vdev_state_exit(spa
, NULL
, 0);
5675 if ((tasks
& SPA_ASYNC_AUTOEXPAND
) && !spa_suspended(spa
)) {
5676 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
5677 spa_async_autoexpand(spa
, spa
->spa_root_vdev
);
5678 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
5682 * See if any devices need to be probed.
5684 if (tasks
& SPA_ASYNC_PROBE
) {
5685 spa_vdev_state_enter(spa
, SCL_NONE
);
5686 spa_async_probe(spa
, spa
->spa_root_vdev
);
5687 (void) spa_vdev_state_exit(spa
, NULL
, 0);
5691 * If any devices are done replacing, detach them.
5693 if (tasks
& SPA_ASYNC_RESILVER_DONE
)
5694 spa_vdev_resilver_done(spa
);
5697 * Kick off a resilver.
5699 if (tasks
& SPA_ASYNC_RESILVER
)
5700 dsl_resilver_restart(spa
->spa_dsl_pool
, 0);
5703 * Let the world know that we're done.
5705 mutex_enter(&spa
->spa_async_lock
);
5706 spa
->spa_async_thread
= NULL
;
5707 cv_broadcast(&spa
->spa_async_cv
);
5708 mutex_exit(&spa
->spa_async_lock
);
5713 spa_async_suspend(spa_t
*spa
)
5715 mutex_enter(&spa
->spa_async_lock
);
5716 spa
->spa_async_suspended
++;
5717 while (spa
->spa_async_thread
!= NULL
)
5718 cv_wait(&spa
->spa_async_cv
, &spa
->spa_async_lock
);
5719 mutex_exit(&spa
->spa_async_lock
);
5723 spa_async_resume(spa_t
*spa
)
5725 mutex_enter(&spa
->spa_async_lock
);
5726 ASSERT(spa
->spa_async_suspended
!= 0);
5727 spa
->spa_async_suspended
--;
5728 mutex_exit(&spa
->spa_async_lock
);
5732 spa_async_dispatch(spa_t
*spa
)
5734 mutex_enter(&spa
->spa_async_lock
);
5735 if (spa
->spa_async_tasks
&& !spa
->spa_async_suspended
&&
5736 spa
->spa_async_thread
== NULL
&&
5737 rootdir
!= NULL
&& !vn_is_readonly(rootdir
))
5738 spa
->spa_async_thread
= thread_create(NULL
, 0,
5739 spa_async_thread
, spa
, 0, &p0
, TS_RUN
, maxclsyspri
);
5740 mutex_exit(&spa
->spa_async_lock
);
5744 spa_async_request(spa_t
*spa
, int task
)
5746 zfs_dbgmsg("spa=%s async request task=%u", spa
->spa_name
, task
);
5747 mutex_enter(&spa
->spa_async_lock
);
5748 spa
->spa_async_tasks
|= task
;
5749 mutex_exit(&spa
->spa_async_lock
);
5753 * ==========================================================================
5754 * SPA syncing routines
5755 * ==========================================================================
5759 bpobj_enqueue_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
5762 bpobj_enqueue(bpo
, bp
, tx
);
5767 spa_free_sync_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
5771 zio_nowait(zio_free_sync(zio
, zio
->io_spa
, dmu_tx_get_txg(tx
), bp
,
5777 spa_sync_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
*nv
, dmu_tx_t
*tx
)
5779 char *packed
= NULL
;
5784 VERIFY(nvlist_size(nv
, &nvsize
, NV_ENCODE_XDR
) == 0);
5787 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
5788 * information. This avoids the dbuf_will_dirty() path and
5789 * saves us a pre-read to get data we don't actually care about.
5791 bufsize
= P2ROUNDUP((uint64_t)nvsize
, SPA_CONFIG_BLOCKSIZE
);
5792 packed
= vmem_alloc(bufsize
, KM_PUSHPAGE
);
5794 VERIFY(nvlist_pack(nv
, &packed
, &nvsize
, NV_ENCODE_XDR
,
5796 bzero(packed
+ nvsize
, bufsize
- nvsize
);
5798 dmu_write(spa
->spa_meta_objset
, obj
, 0, bufsize
, packed
, tx
);
5800 vmem_free(packed
, bufsize
);
5802 VERIFY(0 == dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
));
5803 dmu_buf_will_dirty(db
, tx
);
5804 *(uint64_t *)db
->db_data
= nvsize
;
5805 dmu_buf_rele(db
, FTAG
);
5809 spa_sync_aux_dev(spa_t
*spa
, spa_aux_vdev_t
*sav
, dmu_tx_t
*tx
,
5810 const char *config
, const char *entry
)
5820 * Update the MOS nvlist describing the list of available devices.
5821 * spa_validate_aux() will have already made sure this nvlist is
5822 * valid and the vdevs are labeled appropriately.
5824 if (sav
->sav_object
== 0) {
5825 sav
->sav_object
= dmu_object_alloc(spa
->spa_meta_objset
,
5826 DMU_OT_PACKED_NVLIST
, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE
,
5827 sizeof (uint64_t), tx
);
5828 VERIFY(zap_update(spa
->spa_meta_objset
,
5829 DMU_POOL_DIRECTORY_OBJECT
, entry
, sizeof (uint64_t), 1,
5830 &sav
->sav_object
, tx
) == 0);
5833 VERIFY(nvlist_alloc(&nvroot
, NV_UNIQUE_NAME
, KM_PUSHPAGE
) == 0);
5834 if (sav
->sav_count
== 0) {
5835 VERIFY(nvlist_add_nvlist_array(nvroot
, config
, NULL
, 0) == 0);
5837 list
= kmem_alloc(sav
->sav_count
* sizeof (void *), KM_PUSHPAGE
);
5838 for (i
= 0; i
< sav
->sav_count
; i
++)
5839 list
[i
] = vdev_config_generate(spa
, sav
->sav_vdevs
[i
],
5840 B_FALSE
, VDEV_CONFIG_L2CACHE
);
5841 VERIFY(nvlist_add_nvlist_array(nvroot
, config
, list
,
5842 sav
->sav_count
) == 0);
5843 for (i
= 0; i
< sav
->sav_count
; i
++)
5844 nvlist_free(list
[i
]);
5845 kmem_free(list
, sav
->sav_count
* sizeof (void *));
5848 spa_sync_nvlist(spa
, sav
->sav_object
, nvroot
, tx
);
5849 nvlist_free(nvroot
);
5851 sav
->sav_sync
= B_FALSE
;
5855 spa_sync_config_object(spa_t
*spa
, dmu_tx_t
*tx
)
5859 if (list_is_empty(&spa
->spa_config_dirty_list
))
5862 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
5864 config
= spa_config_generate(spa
, spa
->spa_root_vdev
,
5865 dmu_tx_get_txg(tx
), B_FALSE
);
5868 * If we're upgrading the spa version then make sure that
5869 * the config object gets updated with the correct version.
5871 if (spa
->spa_ubsync
.ub_version
< spa
->spa_uberblock
.ub_version
)
5872 fnvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
5873 spa
->spa_uberblock
.ub_version
);
5875 spa_config_exit(spa
, SCL_STATE
, FTAG
);
5877 if (spa
->spa_config_syncing
)
5878 nvlist_free(spa
->spa_config_syncing
);
5879 spa
->spa_config_syncing
= config
;
5881 spa_sync_nvlist(spa
, spa
->spa_config_object
, config
, tx
);
5885 spa_sync_version(void *arg
, dmu_tx_t
*tx
)
5887 uint64_t *versionp
= arg
;
5888 uint64_t version
= *versionp
;
5889 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
5892 * Setting the version is special cased when first creating the pool.
5894 ASSERT(tx
->tx_txg
!= TXG_INITIAL
);
5896 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
5897 ASSERT(version
>= spa_version(spa
));
5899 spa
->spa_uberblock
.ub_version
= version
;
5900 vdev_config_dirty(spa
->spa_root_vdev
);
5901 spa_history_log_internal(spa
, "set", tx
, "version=%lld", version
);
5905 * Set zpool properties.
5908 spa_sync_props(void *arg
, dmu_tx_t
*tx
)
5910 nvlist_t
*nvp
= arg
;
5911 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
5912 objset_t
*mos
= spa
->spa_meta_objset
;
5913 nvpair_t
*elem
= NULL
;
5915 mutex_enter(&spa
->spa_props_lock
);
5917 while ((elem
= nvlist_next_nvpair(nvp
, elem
))) {
5919 char *strval
, *fname
;
5921 const char *propname
;
5922 zprop_type_t proptype
;
5923 zfeature_info_t
*feature
;
5925 prop
= zpool_name_to_prop(nvpair_name(elem
));
5926 switch ((int)prop
) {
5929 * We checked this earlier in spa_prop_validate().
5931 ASSERT(zpool_prop_feature(nvpair_name(elem
)));
5933 fname
= strchr(nvpair_name(elem
), '@') + 1;
5934 VERIFY3U(0, ==, zfeature_lookup_name(fname
, &feature
));
5936 spa_feature_enable(spa
, feature
, tx
);
5937 spa_history_log_internal(spa
, "set", tx
,
5938 "%s=enabled", nvpair_name(elem
));
5941 case ZPOOL_PROP_VERSION
:
5942 VERIFY(nvpair_value_uint64(elem
, &intval
) == 0);
5944 * The version is synced seperatly before other
5945 * properties and should be correct by now.
5947 ASSERT3U(spa_version(spa
), >=, intval
);
5950 case ZPOOL_PROP_ALTROOT
:
5952 * 'altroot' is a non-persistent property. It should
5953 * have been set temporarily at creation or import time.
5955 ASSERT(spa
->spa_root
!= NULL
);
5958 case ZPOOL_PROP_READONLY
:
5959 case ZPOOL_PROP_CACHEFILE
:
5961 * 'readonly' and 'cachefile' are also non-persisitent
5965 case ZPOOL_PROP_COMMENT
:
5966 VERIFY(nvpair_value_string(elem
, &strval
) == 0);
5967 if (spa
->spa_comment
!= NULL
)
5968 spa_strfree(spa
->spa_comment
);
5969 spa
->spa_comment
= spa_strdup(strval
);
5971 * We need to dirty the configuration on all the vdevs
5972 * so that their labels get updated. It's unnecessary
5973 * to do this for pool creation since the vdev's
5974 * configuratoin has already been dirtied.
5976 if (tx
->tx_txg
!= TXG_INITIAL
)
5977 vdev_config_dirty(spa
->spa_root_vdev
);
5978 spa_history_log_internal(spa
, "set", tx
,
5979 "%s=%s", nvpair_name(elem
), strval
);
5983 * Set pool property values in the poolprops mos object.
5985 if (spa
->spa_pool_props_object
== 0) {
5986 spa
->spa_pool_props_object
=
5987 zap_create_link(mos
, DMU_OT_POOL_PROPS
,
5988 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_PROPS
,
5992 /* normalize the property name */
5993 propname
= zpool_prop_to_name(prop
);
5994 proptype
= zpool_prop_get_type(prop
);
5996 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
5997 ASSERT(proptype
== PROP_TYPE_STRING
);
5998 VERIFY(nvpair_value_string(elem
, &strval
) == 0);
5999 VERIFY(zap_update(mos
,
6000 spa
->spa_pool_props_object
, propname
,
6001 1, strlen(strval
) + 1, strval
, tx
) == 0);
6002 spa_history_log_internal(spa
, "set", tx
,
6003 "%s=%s", nvpair_name(elem
), strval
);
6004 } else if (nvpair_type(elem
) == DATA_TYPE_UINT64
) {
6005 VERIFY(nvpair_value_uint64(elem
, &intval
) == 0);
6007 if (proptype
== PROP_TYPE_INDEX
) {
6009 VERIFY(zpool_prop_index_to_string(
6010 prop
, intval
, &unused
) == 0);
6012 VERIFY(zap_update(mos
,
6013 spa
->spa_pool_props_object
, propname
,
6014 8, 1, &intval
, tx
) == 0);
6015 spa_history_log_internal(spa
, "set", tx
,
6016 "%s=%lld", nvpair_name(elem
), intval
);
6018 ASSERT(0); /* not allowed */
6022 case ZPOOL_PROP_DELEGATION
:
6023 spa
->spa_delegation
= intval
;
6025 case ZPOOL_PROP_BOOTFS
:
6026 spa
->spa_bootfs
= intval
;
6028 case ZPOOL_PROP_FAILUREMODE
:
6029 spa
->spa_failmode
= intval
;
6031 case ZPOOL_PROP_AUTOEXPAND
:
6032 spa
->spa_autoexpand
= intval
;
6033 if (tx
->tx_txg
!= TXG_INITIAL
)
6034 spa_async_request(spa
,
6035 SPA_ASYNC_AUTOEXPAND
);
6037 case ZPOOL_PROP_DEDUPDITTO
:
6038 spa
->spa_dedup_ditto
= intval
;
6047 mutex_exit(&spa
->spa_props_lock
);
6051 * Perform one-time upgrade on-disk changes. spa_version() does not
6052 * reflect the new version this txg, so there must be no changes this
6053 * txg to anything that the upgrade code depends on after it executes.
6054 * Therefore this must be called after dsl_pool_sync() does the sync
6058 spa_sync_upgrades(spa_t
*spa
, dmu_tx_t
*tx
)
6060 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
6062 ASSERT(spa
->spa_sync_pass
== 1);
6064 rrw_enter(&dp
->dp_config_rwlock
, RW_WRITER
, FTAG
);
6066 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_ORIGIN
&&
6067 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_ORIGIN
) {
6068 dsl_pool_create_origin(dp
, tx
);
6070 /* Keeping the origin open increases spa_minref */
6071 spa
->spa_minref
+= 3;
6074 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_NEXT_CLONES
&&
6075 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_NEXT_CLONES
) {
6076 dsl_pool_upgrade_clones(dp
, tx
);
6079 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_DIR_CLONES
&&
6080 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_DIR_CLONES
) {
6081 dsl_pool_upgrade_dir_clones(dp
, tx
);
6083 /* Keeping the freedir open increases spa_minref */
6084 spa
->spa_minref
+= 3;
6087 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_FEATURES
&&
6088 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
6089 spa_feature_create_zap_objects(spa
, tx
);
6091 rrw_exit(&dp
->dp_config_rwlock
, FTAG
);
6095 * Sync the specified transaction group. New blocks may be dirtied as
6096 * part of the process, so we iterate until it converges.
6099 spa_sync(spa_t
*spa
, uint64_t txg
)
6101 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
6102 objset_t
*mos
= spa
->spa_meta_objset
;
6103 bpobj_t
*defer_bpo
= &spa
->spa_deferred_bpobj
;
6104 bplist_t
*free_bpl
= &spa
->spa_free_bplist
[txg
& TXG_MASK
];
6105 vdev_t
*rvd
= spa
->spa_root_vdev
;
6111 VERIFY(spa_writeable(spa
));
6114 * Lock out configuration changes.
6116 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
6118 spa
->spa_syncing_txg
= txg
;
6119 spa
->spa_sync_pass
= 0;
6122 * If there are any pending vdev state changes, convert them
6123 * into config changes that go out with this transaction group.
6125 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
6126 while (list_head(&spa
->spa_state_dirty_list
) != NULL
) {
6128 * We need the write lock here because, for aux vdevs,
6129 * calling vdev_config_dirty() modifies sav_config.
6130 * This is ugly and will become unnecessary when we
6131 * eliminate the aux vdev wart by integrating all vdevs
6132 * into the root vdev tree.
6134 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
6135 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_WRITER
);
6136 while ((vd
= list_head(&spa
->spa_state_dirty_list
)) != NULL
) {
6137 vdev_state_clean(vd
);
6138 vdev_config_dirty(vd
);
6140 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
6141 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
6143 spa_config_exit(spa
, SCL_STATE
, FTAG
);
6145 tx
= dmu_tx_create_assigned(dp
, txg
);
6147 spa
->spa_sync_starttime
= gethrtime();
6148 taskq_cancel_id(system_taskq
, spa
->spa_deadman_tqid
);
6149 spa
->spa_deadman_tqid
= taskq_dispatch_delay(system_taskq
,
6150 spa_deadman
, spa
, TQ_PUSHPAGE
, ddi_get_lbolt() +
6151 NSEC_TO_TICK(spa
->spa_deadman_synctime
));
6154 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
6155 * set spa_deflate if we have no raid-z vdevs.
6157 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_RAIDZ_DEFLATE
&&
6158 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
6161 for (i
= 0; i
< rvd
->vdev_children
; i
++) {
6162 vd
= rvd
->vdev_child
[i
];
6163 if (vd
->vdev_deflate_ratio
!= SPA_MINBLOCKSIZE
)
6166 if (i
== rvd
->vdev_children
) {
6167 spa
->spa_deflate
= TRUE
;
6168 VERIFY(0 == zap_add(spa
->spa_meta_objset
,
6169 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
6170 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
));
6175 * If anything has changed in this txg, or if someone is waiting
6176 * for this txg to sync (eg, spa_vdev_remove()), push the
6177 * deferred frees from the previous txg. If not, leave them
6178 * alone so that we don't generate work on an otherwise idle
6181 if (!txg_list_empty(&dp
->dp_dirty_datasets
, txg
) ||
6182 !txg_list_empty(&dp
->dp_dirty_dirs
, txg
) ||
6183 !txg_list_empty(&dp
->dp_sync_tasks
, txg
) ||
6184 ((dsl_scan_active(dp
->dp_scan
) ||
6185 txg_sync_waiting(dp
)) && !spa_shutting_down(spa
))) {
6186 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
6187 VERIFY3U(bpobj_iterate(defer_bpo
,
6188 spa_free_sync_cb
, zio
, tx
), ==, 0);
6189 VERIFY0(zio_wait(zio
));
6193 * Iterate to convergence.
6196 int pass
= ++spa
->spa_sync_pass
;
6198 spa_sync_config_object(spa
, tx
);
6199 spa_sync_aux_dev(spa
, &spa
->spa_spares
, tx
,
6200 ZPOOL_CONFIG_SPARES
, DMU_POOL_SPARES
);
6201 spa_sync_aux_dev(spa
, &spa
->spa_l2cache
, tx
,
6202 ZPOOL_CONFIG_L2CACHE
, DMU_POOL_L2CACHE
);
6203 spa_errlog_sync(spa
, txg
);
6204 dsl_pool_sync(dp
, txg
);
6206 if (pass
< zfs_sync_pass_deferred_free
) {
6207 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
6208 bplist_iterate(free_bpl
, spa_free_sync_cb
,
6210 VERIFY(zio_wait(zio
) == 0);
6212 bplist_iterate(free_bpl
, bpobj_enqueue_cb
,
6217 dsl_scan_sync(dp
, tx
);
6219 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, txg
)))
6223 spa_sync_upgrades(spa
, tx
);
6225 } while (dmu_objset_is_dirty(mos
, txg
));
6228 * Rewrite the vdev configuration (which includes the uberblock)
6229 * to commit the transaction group.
6231 * If there are no dirty vdevs, we sync the uberblock to a few
6232 * random top-level vdevs that are known to be visible in the
6233 * config cache (see spa_vdev_add() for a complete description).
6234 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
6238 * We hold SCL_STATE to prevent vdev open/close/etc.
6239 * while we're attempting to write the vdev labels.
6241 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
6243 if (list_is_empty(&spa
->spa_config_dirty_list
)) {
6244 vdev_t
*svd
[SPA_DVAS_PER_BP
];
6246 int children
= rvd
->vdev_children
;
6247 int c0
= spa_get_random(children
);
6249 for (c
= 0; c
< children
; c
++) {
6250 vd
= rvd
->vdev_child
[(c0
+ c
) % children
];
6251 if (vd
->vdev_ms_array
== 0 || vd
->vdev_islog
)
6253 svd
[svdcount
++] = vd
;
6254 if (svdcount
== SPA_DVAS_PER_BP
)
6257 error
= vdev_config_sync(svd
, svdcount
, txg
, B_FALSE
);
6259 error
= vdev_config_sync(svd
, svdcount
, txg
,
6262 error
= vdev_config_sync(rvd
->vdev_child
,
6263 rvd
->vdev_children
, txg
, B_FALSE
);
6265 error
= vdev_config_sync(rvd
->vdev_child
,
6266 rvd
->vdev_children
, txg
, B_TRUE
);
6270 spa
->spa_last_synced_guid
= rvd
->vdev_guid
;
6272 spa_config_exit(spa
, SCL_STATE
, FTAG
);
6276 zio_suspend(spa
, NULL
);
6277 zio_resume_wait(spa
);
6281 taskq_cancel_id(system_taskq
, spa
->spa_deadman_tqid
);
6282 spa
->spa_deadman_tqid
= 0;
6285 * Clear the dirty config list.
6287 while ((vd
= list_head(&spa
->spa_config_dirty_list
)) != NULL
)
6288 vdev_config_clean(vd
);
6291 * Now that the new config has synced transactionally,
6292 * let it become visible to the config cache.
6294 if (spa
->spa_config_syncing
!= NULL
) {
6295 spa_config_set(spa
, spa
->spa_config_syncing
);
6296 spa
->spa_config_txg
= txg
;
6297 spa
->spa_config_syncing
= NULL
;
6300 spa
->spa_ubsync
= spa
->spa_uberblock
;
6302 dsl_pool_sync_done(dp
, txg
);
6305 * Update usable space statistics.
6307 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, TXG_CLEAN(txg
))))
6308 vdev_sync_done(vd
, txg
);
6310 spa_update_dspace(spa
);
6313 * It had better be the case that we didn't dirty anything
6314 * since vdev_config_sync().
6316 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
, txg
));
6317 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
6318 ASSERT(txg_list_empty(&spa
->spa_vdev_txg_list
, txg
));
6320 spa
->spa_sync_pass
= 0;
6322 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
6324 spa_handle_ignored_writes(spa
);
6327 * If any async tasks have been requested, kick them off.
6329 spa_async_dispatch(spa
);
6333 * Sync all pools. We don't want to hold the namespace lock across these
6334 * operations, so we take a reference on the spa_t and drop the lock during the
6338 spa_sync_allpools(void)
6341 mutex_enter(&spa_namespace_lock
);
6342 while ((spa
= spa_next(spa
)) != NULL
) {
6343 if (spa_state(spa
) != POOL_STATE_ACTIVE
||
6344 !spa_writeable(spa
) || spa_suspended(spa
))
6346 spa_open_ref(spa
, FTAG
);
6347 mutex_exit(&spa_namespace_lock
);
6348 txg_wait_synced(spa_get_dsl(spa
), 0);
6349 mutex_enter(&spa_namespace_lock
);
6350 spa_close(spa
, FTAG
);
6352 mutex_exit(&spa_namespace_lock
);
6356 * ==========================================================================
6357 * Miscellaneous routines
6358 * ==========================================================================
6362 * Remove all pools in the system.
6370 * Remove all cached state. All pools should be closed now,
6371 * so every spa in the AVL tree should be unreferenced.
6373 mutex_enter(&spa_namespace_lock
);
6374 while ((spa
= spa_next(NULL
)) != NULL
) {
6376 * Stop async tasks. The async thread may need to detach
6377 * a device that's been replaced, which requires grabbing
6378 * spa_namespace_lock, so we must drop it here.
6380 spa_open_ref(spa
, FTAG
);
6381 mutex_exit(&spa_namespace_lock
);
6382 spa_async_suspend(spa
);
6383 mutex_enter(&spa_namespace_lock
);
6384 spa_close(spa
, FTAG
);
6386 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
6388 spa_deactivate(spa
);
6392 mutex_exit(&spa_namespace_lock
);
6396 spa_lookup_by_guid(spa_t
*spa
, uint64_t guid
, boolean_t aux
)
6401 if ((vd
= vdev_lookup_by_guid(spa
->spa_root_vdev
, guid
)) != NULL
)
6405 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
6406 vd
= spa
->spa_l2cache
.sav_vdevs
[i
];
6407 if (vd
->vdev_guid
== guid
)
6411 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
6412 vd
= spa
->spa_spares
.sav_vdevs
[i
];
6413 if (vd
->vdev_guid
== guid
)
6422 spa_upgrade(spa_t
*spa
, uint64_t version
)
6424 ASSERT(spa_writeable(spa
));
6426 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6429 * This should only be called for a non-faulted pool, and since a
6430 * future version would result in an unopenable pool, this shouldn't be
6433 ASSERT(SPA_VERSION_IS_SUPPORTED(spa
->spa_uberblock
.ub_version
));
6434 ASSERT(version
>= spa
->spa_uberblock
.ub_version
);
6436 spa
->spa_uberblock
.ub_version
= version
;
6437 vdev_config_dirty(spa
->spa_root_vdev
);
6439 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6441 txg_wait_synced(spa_get_dsl(spa
), 0);
6445 spa_has_spare(spa_t
*spa
, uint64_t guid
)
6449 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
6451 for (i
= 0; i
< sav
->sav_count
; i
++)
6452 if (sav
->sav_vdevs
[i
]->vdev_guid
== guid
)
6455 for (i
= 0; i
< sav
->sav_npending
; i
++) {
6456 if (nvlist_lookup_uint64(sav
->sav_pending
[i
], ZPOOL_CONFIG_GUID
,
6457 &spareguid
) == 0 && spareguid
== guid
)
6465 * Check if a pool has an active shared spare device.
6466 * Note: reference count of an active spare is 2, as a spare and as a replace
6469 spa_has_active_shared_spare(spa_t
*spa
)
6473 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
6475 for (i
= 0; i
< sav
->sav_count
; i
++) {
6476 if (spa_spare_exists(sav
->sav_vdevs
[i
]->vdev_guid
, &pool
,
6477 &refcnt
) && pool
!= 0ULL && pool
== spa_guid(spa
) &&
6486 * Post a FM_EREPORT_ZFS_* event from sys/fm/fs/zfs.h. The payload will be
6487 * filled in from the spa and (optionally) the vdev. This doesn't do anything
6488 * in the userland libzpool, as we don't want consumers to misinterpret ztest
6489 * or zdb as real changes.
6492 spa_event_notify(spa_t
*spa
, vdev_t
*vd
, const char *name
)
6495 zfs_ereport_post(name
, spa
, vd
, NULL
, 0, 0);
6499 #if defined(_KERNEL) && defined(HAVE_SPL)
6500 /* state manipulation functions */
6501 EXPORT_SYMBOL(spa_open
);
6502 EXPORT_SYMBOL(spa_open_rewind
);
6503 EXPORT_SYMBOL(spa_get_stats
);
6504 EXPORT_SYMBOL(spa_create
);
6505 EXPORT_SYMBOL(spa_import_rootpool
);
6506 EXPORT_SYMBOL(spa_import
);
6507 EXPORT_SYMBOL(spa_tryimport
);
6508 EXPORT_SYMBOL(spa_destroy
);
6509 EXPORT_SYMBOL(spa_export
);
6510 EXPORT_SYMBOL(spa_reset
);
6511 EXPORT_SYMBOL(spa_async_request
);
6512 EXPORT_SYMBOL(spa_async_suspend
);
6513 EXPORT_SYMBOL(spa_async_resume
);
6514 EXPORT_SYMBOL(spa_inject_addref
);
6515 EXPORT_SYMBOL(spa_inject_delref
);
6516 EXPORT_SYMBOL(spa_scan_stat_init
);
6517 EXPORT_SYMBOL(spa_scan_get_stats
);
6519 /* device maniion */
6520 EXPORT_SYMBOL(spa_vdev_add
);
6521 EXPORT_SYMBOL(spa_vdev_attach
);
6522 EXPORT_SYMBOL(spa_vdev_detach
);
6523 EXPORT_SYMBOL(spa_vdev_remove
);
6524 EXPORT_SYMBOL(spa_vdev_setpath
);
6525 EXPORT_SYMBOL(spa_vdev_setfru
);
6526 EXPORT_SYMBOL(spa_vdev_split_mirror
);
6528 /* spare statech is global across all pools) */
6529 EXPORT_SYMBOL(spa_spare_add
);
6530 EXPORT_SYMBOL(spa_spare_remove
);
6531 EXPORT_SYMBOL(spa_spare_exists
);
6532 EXPORT_SYMBOL(spa_spare_activate
);
6534 /* L2ARC statech is global across all pools) */
6535 EXPORT_SYMBOL(spa_l2cache_add
);
6536 EXPORT_SYMBOL(spa_l2cache_remove
);
6537 EXPORT_SYMBOL(spa_l2cache_exists
);
6538 EXPORT_SYMBOL(spa_l2cache_activate
);
6539 EXPORT_SYMBOL(spa_l2cache_drop
);
6542 EXPORT_SYMBOL(spa_scan
);
6543 EXPORT_SYMBOL(spa_scan_stop
);
6546 EXPORT_SYMBOL(spa_sync
); /* only for DMU use */
6547 EXPORT_SYMBOL(spa_sync_allpools
);
6550 EXPORT_SYMBOL(spa_prop_set
);
6551 EXPORT_SYMBOL(spa_prop_get
);
6552 EXPORT_SYMBOL(spa_prop_clear_bootfs
);
6554 /* asynchronous event notification */
6555 EXPORT_SYMBOL(spa_event_notify
);