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) 2011, 2014 by Delphix. All rights reserved.
25 * Copyright (c) 2015, Nexenta Systems, Inc. All rights reserved.
26 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
27 * Copyright 2013 Saso Kiselkov. All rights reserved.
28 * Copyright (c) 2014 Integros [integros.com]
29 * Copyright 2016 Toomas Soome <tsoome@me.com>
33 * SPA: Storage Pool Allocator
35 * This file contains all the routines used when modifying on-disk SPA state.
36 * This includes opening, importing, destroying, exporting a pool, and syncing a
40 #include <sys/zfs_context.h>
41 #include <sys/fm/fs/zfs.h>
42 #include <sys/spa_impl.h>
44 #include <sys/zio_checksum.h>
46 #include <sys/dmu_tx.h>
50 #include <sys/vdev_impl.h>
51 #include <sys/metaslab.h>
52 #include <sys/metaslab_impl.h>
53 #include <sys/uberblock_impl.h>
56 #include <sys/dmu_traverse.h>
57 #include <sys/dmu_objset.h>
58 #include <sys/unique.h>
59 #include <sys/dsl_pool.h>
60 #include <sys/dsl_dataset.h>
61 #include <sys/dsl_dir.h>
62 #include <sys/dsl_prop.h>
63 #include <sys/dsl_synctask.h>
64 #include <sys/fs/zfs.h>
66 #include <sys/callb.h>
67 #include <sys/systeminfo.h>
68 #include <sys/spa_boot.h>
69 #include <sys/zfs_ioctl.h>
70 #include <sys/dsl_scan.h>
71 #include <sys/zfeature.h>
72 #include <sys/dsl_destroy.h>
75 #include <sys/bootprops.h>
76 #include <sys/callb.h>
77 #include <sys/cpupart.h>
79 #include <sys/sysdc.h>
84 #include "zfs_comutil.h"
87 * The interval, in seconds, at which failed configuration cache file writes
90 static int zfs_ccw_retry_interval
= 300;
92 typedef enum zti_modes
{
93 ZTI_MODE_FIXED
, /* value is # of threads (min 1) */
94 ZTI_MODE_BATCH
, /* cpu-intensive; value is ignored */
95 ZTI_MODE_NULL
, /* don't create a taskq */
99 #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) }
100 #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 }
101 #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 }
103 #define ZTI_N(n) ZTI_P(n, 1)
104 #define ZTI_ONE ZTI_N(1)
106 typedef struct zio_taskq_info
{
107 zti_modes_t zti_mode
;
112 static const char *const zio_taskq_types
[ZIO_TASKQ_TYPES
] = {
113 "issue", "issue_high", "intr", "intr_high"
117 * This table defines the taskq settings for each ZFS I/O type. When
118 * initializing a pool, we use this table to create an appropriately sized
119 * taskq. Some operations are low volume and therefore have a small, static
120 * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
121 * macros. Other operations process a large amount of data; the ZTI_BATCH
122 * macro causes us to create a taskq oriented for throughput. Some operations
123 * are so high frequency and short-lived that the taskq itself can become a a
124 * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
125 * additional degree of parallelism specified by the number of threads per-
126 * taskq and the number of taskqs; when dispatching an event in this case, the
127 * particular taskq is chosen at random.
129 * The different taskq priorities are to handle the different contexts (issue
130 * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that
131 * need to be handled with minimum delay.
133 const zio_taskq_info_t zio_taskqs
[ZIO_TYPES
][ZIO_TASKQ_TYPES
] = {
134 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
135 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* NULL */
136 { ZTI_N(8), ZTI_NULL
, ZTI_P(12, 8), ZTI_NULL
}, /* READ */
137 { ZTI_BATCH
, ZTI_N(5), ZTI_N(8), ZTI_N(5) }, /* WRITE */
138 { ZTI_P(12, 8), ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* FREE */
139 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* CLAIM */
140 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* IOCTL */
143 static sysevent_t
*spa_event_create(spa_t
*spa
, vdev_t
*vd
, const char *name
);
144 static void spa_event_post(sysevent_t
*ev
);
145 static void spa_sync_version(void *arg
, dmu_tx_t
*tx
);
146 static void spa_sync_props(void *arg
, dmu_tx_t
*tx
);
147 static boolean_t
spa_has_active_shared_spare(spa_t
*spa
);
148 static int spa_load_impl(spa_t
*spa
, uint64_t, nvlist_t
*config
,
149 spa_load_state_t state
, spa_import_type_t type
, boolean_t mosconfig
,
151 static void spa_vdev_resilver_done(spa_t
*spa
);
153 uint_t zio_taskq_batch_pct
= 75; /* 1 thread per cpu in pset */
154 id_t zio_taskq_psrset_bind
= PS_NONE
;
155 boolean_t zio_taskq_sysdc
= B_TRUE
; /* use SDC scheduling class */
156 uint_t zio_taskq_basedc
= 80; /* base duty cycle */
158 boolean_t spa_create_process
= B_TRUE
; /* no process ==> no sysdc */
159 extern int zfs_sync_pass_deferred_free
;
162 * This (illegal) pool name is used when temporarily importing a spa_t in order
163 * to get the vdev stats associated with the imported devices.
165 #define TRYIMPORT_NAME "$import"
168 * ==========================================================================
169 * SPA properties routines
170 * ==========================================================================
174 * Add a (source=src, propname=propval) list to an nvlist.
177 spa_prop_add_list(nvlist_t
*nvl
, zpool_prop_t prop
, char *strval
,
178 uint64_t intval
, zprop_source_t src
)
180 const char *propname
= zpool_prop_to_name(prop
);
183 VERIFY(nvlist_alloc(&propval
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
184 VERIFY(nvlist_add_uint64(propval
, ZPROP_SOURCE
, src
) == 0);
187 VERIFY(nvlist_add_string(propval
, ZPROP_VALUE
, strval
) == 0);
189 VERIFY(nvlist_add_uint64(propval
, ZPROP_VALUE
, intval
) == 0);
191 VERIFY(nvlist_add_nvlist(nvl
, propname
, propval
) == 0);
192 nvlist_free(propval
);
196 * Get property values from the spa configuration.
199 spa_prop_get_config(spa_t
*spa
, nvlist_t
**nvp
)
201 vdev_t
*rvd
= spa
->spa_root_vdev
;
202 dsl_pool_t
*pool
= spa
->spa_dsl_pool
;
203 uint64_t size
, alloc
, cap
, version
;
204 zprop_source_t src
= ZPROP_SRC_NONE
;
205 spa_config_dirent_t
*dp
;
206 metaslab_class_t
*mc
= spa_normal_class(spa
);
208 ASSERT(MUTEX_HELD(&spa
->spa_props_lock
));
211 alloc
= metaslab_class_get_alloc(spa_normal_class(spa
));
212 size
= metaslab_class_get_space(spa_normal_class(spa
));
213 spa_prop_add_list(*nvp
, ZPOOL_PROP_NAME
, spa_name(spa
), 0, src
);
214 spa_prop_add_list(*nvp
, ZPOOL_PROP_SIZE
, NULL
, size
, src
);
215 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALLOCATED
, NULL
, alloc
, src
);
216 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREE
, NULL
,
219 spa_prop_add_list(*nvp
, ZPOOL_PROP_FRAGMENTATION
, NULL
,
220 metaslab_class_fragmentation(mc
), src
);
221 spa_prop_add_list(*nvp
, ZPOOL_PROP_EXPANDSZ
, NULL
,
222 metaslab_class_expandable_space(mc
), src
);
223 spa_prop_add_list(*nvp
, ZPOOL_PROP_READONLY
, NULL
,
224 (spa_mode(spa
) == FREAD
), src
);
226 cap
= (size
== 0) ? 0 : (alloc
* 100 / size
);
227 spa_prop_add_list(*nvp
, ZPOOL_PROP_CAPACITY
, NULL
, cap
, src
);
229 spa_prop_add_list(*nvp
, ZPOOL_PROP_DEDUPRATIO
, NULL
,
230 ddt_get_pool_dedup_ratio(spa
), src
);
232 spa_prop_add_list(*nvp
, ZPOOL_PROP_HEALTH
, NULL
,
233 rvd
->vdev_state
, src
);
235 version
= spa_version(spa
);
236 if (version
== zpool_prop_default_numeric(ZPOOL_PROP_VERSION
))
237 src
= ZPROP_SRC_DEFAULT
;
239 src
= ZPROP_SRC_LOCAL
;
240 spa_prop_add_list(*nvp
, ZPOOL_PROP_VERSION
, NULL
, version
, src
);
245 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
246 * when opening pools before this version freedir will be NULL.
248 if (pool
->dp_free_dir
!= NULL
) {
249 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
, NULL
,
250 dsl_dir_phys(pool
->dp_free_dir
)->dd_used_bytes
,
253 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
,
257 if (pool
->dp_leak_dir
!= NULL
) {
258 spa_prop_add_list(*nvp
, ZPOOL_PROP_LEAKED
, NULL
,
259 dsl_dir_phys(pool
->dp_leak_dir
)->dd_used_bytes
,
262 spa_prop_add_list(*nvp
, ZPOOL_PROP_LEAKED
,
267 spa_prop_add_list(*nvp
, ZPOOL_PROP_GUID
, NULL
, spa_guid(spa
), src
);
269 if (spa
->spa_comment
!= NULL
) {
270 spa_prop_add_list(*nvp
, ZPOOL_PROP_COMMENT
, spa
->spa_comment
,
274 if (spa
->spa_root
!= NULL
)
275 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALTROOT
, spa
->spa_root
,
278 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_BLOCKS
)) {
279 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXBLOCKSIZE
, NULL
,
280 MIN(zfs_max_recordsize
, SPA_MAXBLOCKSIZE
), ZPROP_SRC_NONE
);
282 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXBLOCKSIZE
, NULL
,
283 SPA_OLD_MAXBLOCKSIZE
, ZPROP_SRC_NONE
);
286 if ((dp
= list_head(&spa
->spa_config_list
)) != NULL
) {
287 if (dp
->scd_path
== NULL
) {
288 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
289 "none", 0, ZPROP_SRC_LOCAL
);
290 } else if (strcmp(dp
->scd_path
, spa_config_path
) != 0) {
291 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
292 dp
->scd_path
, 0, ZPROP_SRC_LOCAL
);
298 * Get zpool property values.
301 spa_prop_get(spa_t
*spa
, nvlist_t
**nvp
)
303 objset_t
*mos
= spa
->spa_meta_objset
;
308 VERIFY(nvlist_alloc(nvp
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
310 mutex_enter(&spa
->spa_props_lock
);
313 * Get properties from the spa config.
315 spa_prop_get_config(spa
, nvp
);
317 /* If no pool property object, no more prop to get. */
318 if (mos
== NULL
|| spa
->spa_pool_props_object
== 0) {
319 mutex_exit(&spa
->spa_props_lock
);
324 * Get properties from the MOS pool property object.
326 for (zap_cursor_init(&zc
, mos
, spa
->spa_pool_props_object
);
327 (err
= zap_cursor_retrieve(&zc
, &za
)) == 0;
328 zap_cursor_advance(&zc
)) {
331 zprop_source_t src
= ZPROP_SRC_DEFAULT
;
334 if ((prop
= zpool_name_to_prop(za
.za_name
)) == ZPROP_INVAL
)
337 switch (za
.za_integer_length
) {
339 /* integer property */
340 if (za
.za_first_integer
!=
341 zpool_prop_default_numeric(prop
))
342 src
= ZPROP_SRC_LOCAL
;
344 if (prop
== ZPOOL_PROP_BOOTFS
) {
346 dsl_dataset_t
*ds
= NULL
;
348 dp
= spa_get_dsl(spa
);
349 dsl_pool_config_enter(dp
, FTAG
);
350 if (err
= dsl_dataset_hold_obj(dp
,
351 za
.za_first_integer
, FTAG
, &ds
)) {
352 dsl_pool_config_exit(dp
, FTAG
);
356 strval
= kmem_alloc(ZFS_MAX_DATASET_NAME_LEN
,
358 dsl_dataset_name(ds
, strval
);
359 dsl_dataset_rele(ds
, FTAG
);
360 dsl_pool_config_exit(dp
, FTAG
);
363 intval
= za
.za_first_integer
;
366 spa_prop_add_list(*nvp
, prop
, strval
, intval
, src
);
369 kmem_free(strval
, ZFS_MAX_DATASET_NAME_LEN
);
374 /* string property */
375 strval
= kmem_alloc(za
.za_num_integers
, KM_SLEEP
);
376 err
= zap_lookup(mos
, spa
->spa_pool_props_object
,
377 za
.za_name
, 1, za
.za_num_integers
, strval
);
379 kmem_free(strval
, za
.za_num_integers
);
382 spa_prop_add_list(*nvp
, prop
, strval
, 0, src
);
383 kmem_free(strval
, za
.za_num_integers
);
390 zap_cursor_fini(&zc
);
391 mutex_exit(&spa
->spa_props_lock
);
393 if (err
&& err
!= ENOENT
) {
403 * Validate the given pool properties nvlist and modify the list
404 * for the property values to be set.
407 spa_prop_validate(spa_t
*spa
, nvlist_t
*props
)
410 int error
= 0, reset_bootfs
= 0;
412 boolean_t has_feature
= B_FALSE
;
415 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
417 char *strval
, *slash
, *check
, *fname
;
418 const char *propname
= nvpair_name(elem
);
419 zpool_prop_t prop
= zpool_name_to_prop(propname
);
423 if (!zpool_prop_feature(propname
)) {
424 error
= SET_ERROR(EINVAL
);
429 * Sanitize the input.
431 if (nvpair_type(elem
) != DATA_TYPE_UINT64
) {
432 error
= SET_ERROR(EINVAL
);
436 if (nvpair_value_uint64(elem
, &intval
) != 0) {
437 error
= SET_ERROR(EINVAL
);
442 error
= SET_ERROR(EINVAL
);
446 fname
= strchr(propname
, '@') + 1;
447 if (zfeature_lookup_name(fname
, NULL
) != 0) {
448 error
= SET_ERROR(EINVAL
);
452 has_feature
= B_TRUE
;
455 case ZPOOL_PROP_VERSION
:
456 error
= nvpair_value_uint64(elem
, &intval
);
458 (intval
< spa_version(spa
) ||
459 intval
> SPA_VERSION_BEFORE_FEATURES
||
461 error
= SET_ERROR(EINVAL
);
464 case ZPOOL_PROP_DELEGATION
:
465 case ZPOOL_PROP_AUTOREPLACE
:
466 case ZPOOL_PROP_LISTSNAPS
:
467 case ZPOOL_PROP_AUTOEXPAND
:
468 error
= nvpair_value_uint64(elem
, &intval
);
469 if (!error
&& intval
> 1)
470 error
= SET_ERROR(EINVAL
);
473 case ZPOOL_PROP_BOOTFS
:
475 * If the pool version is less than SPA_VERSION_BOOTFS,
476 * or the pool is still being created (version == 0),
477 * the bootfs property cannot be set.
479 if (spa_version(spa
) < SPA_VERSION_BOOTFS
) {
480 error
= SET_ERROR(ENOTSUP
);
485 * Make sure the vdev config is bootable
487 if (!vdev_is_bootable(spa
->spa_root_vdev
)) {
488 error
= SET_ERROR(ENOTSUP
);
494 error
= nvpair_value_string(elem
, &strval
);
500 if (strval
== NULL
|| strval
[0] == '\0') {
501 objnum
= zpool_prop_default_numeric(
506 if (error
= dmu_objset_hold(strval
, FTAG
, &os
))
510 * Must be ZPL, and its property settings
511 * must be supported by GRUB (compression
512 * is not gzip, and large blocks are not used).
515 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
516 error
= SET_ERROR(ENOTSUP
);
518 dsl_prop_get_int_ds(dmu_objset_ds(os
),
519 zfs_prop_to_name(ZFS_PROP_COMPRESSION
),
521 !BOOTFS_COMPRESS_VALID(propval
)) {
522 error
= SET_ERROR(ENOTSUP
);
524 objnum
= dmu_objset_id(os
);
526 dmu_objset_rele(os
, FTAG
);
530 case ZPOOL_PROP_FAILUREMODE
:
531 error
= nvpair_value_uint64(elem
, &intval
);
532 if (!error
&& (intval
< ZIO_FAILURE_MODE_WAIT
||
533 intval
> ZIO_FAILURE_MODE_PANIC
))
534 error
= SET_ERROR(EINVAL
);
537 * This is a special case which only occurs when
538 * the pool has completely failed. This allows
539 * the user to change the in-core failmode property
540 * without syncing it out to disk (I/Os might
541 * currently be blocked). We do this by returning
542 * EIO to the caller (spa_prop_set) to trick it
543 * into thinking we encountered a property validation
546 if (!error
&& spa_suspended(spa
)) {
547 spa
->spa_failmode
= intval
;
548 error
= SET_ERROR(EIO
);
552 case ZPOOL_PROP_CACHEFILE
:
553 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
556 if (strval
[0] == '\0')
559 if (strcmp(strval
, "none") == 0)
562 if (strval
[0] != '/') {
563 error
= SET_ERROR(EINVAL
);
567 slash
= strrchr(strval
, '/');
568 ASSERT(slash
!= NULL
);
570 if (slash
[1] == '\0' || strcmp(slash
, "/.") == 0 ||
571 strcmp(slash
, "/..") == 0)
572 error
= SET_ERROR(EINVAL
);
575 case ZPOOL_PROP_COMMENT
:
576 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
578 for (check
= strval
; *check
!= '\0'; check
++) {
580 * The kernel doesn't have an easy isprint()
581 * check. For this kernel check, we merely
582 * check ASCII apart from DEL. Fix this if
583 * there is an easy-to-use kernel isprint().
585 if (*check
>= 0x7f) {
586 error
= SET_ERROR(EINVAL
);
590 if (strlen(strval
) > ZPROP_MAX_COMMENT
)
594 case ZPOOL_PROP_DEDUPDITTO
:
595 if (spa_version(spa
) < SPA_VERSION_DEDUP
)
596 error
= SET_ERROR(ENOTSUP
);
598 error
= nvpair_value_uint64(elem
, &intval
);
600 intval
!= 0 && intval
< ZIO_DEDUPDITTO_MIN
)
601 error
= SET_ERROR(EINVAL
);
609 if (!error
&& reset_bootfs
) {
610 error
= nvlist_remove(props
,
611 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), DATA_TYPE_STRING
);
614 error
= nvlist_add_uint64(props
,
615 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), objnum
);
623 spa_configfile_set(spa_t
*spa
, nvlist_t
*nvp
, boolean_t need_sync
)
626 spa_config_dirent_t
*dp
;
628 if (nvlist_lookup_string(nvp
, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
),
632 dp
= kmem_alloc(sizeof (spa_config_dirent_t
),
635 if (cachefile
[0] == '\0')
636 dp
->scd_path
= spa_strdup(spa_config_path
);
637 else if (strcmp(cachefile
, "none") == 0)
640 dp
->scd_path
= spa_strdup(cachefile
);
642 list_insert_head(&spa
->spa_config_list
, dp
);
644 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
648 spa_prop_set(spa_t
*spa
, nvlist_t
*nvp
)
651 nvpair_t
*elem
= NULL
;
652 boolean_t need_sync
= B_FALSE
;
654 if ((error
= spa_prop_validate(spa
, nvp
)) != 0)
657 while ((elem
= nvlist_next_nvpair(nvp
, elem
)) != NULL
) {
658 zpool_prop_t prop
= zpool_name_to_prop(nvpair_name(elem
));
660 if (prop
== ZPOOL_PROP_CACHEFILE
||
661 prop
== ZPOOL_PROP_ALTROOT
||
662 prop
== ZPOOL_PROP_READONLY
)
665 if (prop
== ZPOOL_PROP_VERSION
|| prop
== ZPROP_INVAL
) {
668 if (prop
== ZPOOL_PROP_VERSION
) {
669 VERIFY(nvpair_value_uint64(elem
, &ver
) == 0);
671 ASSERT(zpool_prop_feature(nvpair_name(elem
)));
672 ver
= SPA_VERSION_FEATURES
;
676 /* Save time if the version is already set. */
677 if (ver
== spa_version(spa
))
681 * In addition to the pool directory object, we might
682 * create the pool properties object, the features for
683 * read object, the features for write object, or the
684 * feature descriptions object.
686 error
= dsl_sync_task(spa
->spa_name
, NULL
,
687 spa_sync_version
, &ver
,
688 6, ZFS_SPACE_CHECK_RESERVED
);
699 return (dsl_sync_task(spa
->spa_name
, NULL
, spa_sync_props
,
700 nvp
, 6, ZFS_SPACE_CHECK_RESERVED
));
707 * If the bootfs property value is dsobj, clear it.
710 spa_prop_clear_bootfs(spa_t
*spa
, uint64_t dsobj
, dmu_tx_t
*tx
)
712 if (spa
->spa_bootfs
== dsobj
&& spa
->spa_pool_props_object
!= 0) {
713 VERIFY(zap_remove(spa
->spa_meta_objset
,
714 spa
->spa_pool_props_object
,
715 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), tx
) == 0);
722 spa_change_guid_check(void *arg
, dmu_tx_t
*tx
)
724 uint64_t *newguid
= arg
;
725 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
726 vdev_t
*rvd
= spa
->spa_root_vdev
;
729 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
730 vdev_state
= rvd
->vdev_state
;
731 spa_config_exit(spa
, SCL_STATE
, FTAG
);
733 if (vdev_state
!= VDEV_STATE_HEALTHY
)
734 return (SET_ERROR(ENXIO
));
736 ASSERT3U(spa_guid(spa
), !=, *newguid
);
742 spa_change_guid_sync(void *arg
, dmu_tx_t
*tx
)
744 uint64_t *newguid
= arg
;
745 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
747 vdev_t
*rvd
= spa
->spa_root_vdev
;
749 oldguid
= spa_guid(spa
);
751 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
752 rvd
->vdev_guid
= *newguid
;
753 rvd
->vdev_guid_sum
+= (*newguid
- oldguid
);
754 vdev_config_dirty(rvd
);
755 spa_config_exit(spa
, SCL_STATE
, FTAG
);
757 spa_history_log_internal(spa
, "guid change", tx
, "old=%llu new=%llu",
762 * Change the GUID for the pool. This is done so that we can later
763 * re-import a pool built from a clone of our own vdevs. We will modify
764 * the root vdev's guid, our own pool guid, and then mark all of our
765 * vdevs dirty. Note that we must make sure that all our vdevs are
766 * online when we do this, or else any vdevs that weren't present
767 * would be orphaned from our pool. We are also going to issue a
768 * sysevent to update any watchers.
771 spa_change_guid(spa_t
*spa
)
776 mutex_enter(&spa
->spa_vdev_top_lock
);
777 mutex_enter(&spa_namespace_lock
);
778 guid
= spa_generate_guid(NULL
);
780 error
= dsl_sync_task(spa
->spa_name
, spa_change_guid_check
,
781 spa_change_guid_sync
, &guid
, 5, ZFS_SPACE_CHECK_RESERVED
);
784 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
785 spa_event_notify(spa
, NULL
, ESC_ZFS_POOL_REGUID
);
788 mutex_exit(&spa_namespace_lock
);
789 mutex_exit(&spa
->spa_vdev_top_lock
);
795 * ==========================================================================
796 * SPA state manipulation (open/create/destroy/import/export)
797 * ==========================================================================
801 spa_error_entry_compare(const void *a
, const void *b
)
803 spa_error_entry_t
*sa
= (spa_error_entry_t
*)a
;
804 spa_error_entry_t
*sb
= (spa_error_entry_t
*)b
;
807 ret
= bcmp(&sa
->se_bookmark
, &sb
->se_bookmark
,
808 sizeof (zbookmark_phys_t
));
819 * Utility function which retrieves copies of the current logs and
820 * re-initializes them in the process.
823 spa_get_errlists(spa_t
*spa
, avl_tree_t
*last
, avl_tree_t
*scrub
)
825 ASSERT(MUTEX_HELD(&spa
->spa_errlist_lock
));
827 bcopy(&spa
->spa_errlist_last
, last
, sizeof (avl_tree_t
));
828 bcopy(&spa
->spa_errlist_scrub
, scrub
, sizeof (avl_tree_t
));
830 avl_create(&spa
->spa_errlist_scrub
,
831 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
832 offsetof(spa_error_entry_t
, se_avl
));
833 avl_create(&spa
->spa_errlist_last
,
834 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
835 offsetof(spa_error_entry_t
, se_avl
));
839 spa_taskqs_init(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
841 const zio_taskq_info_t
*ztip
= &zio_taskqs
[t
][q
];
842 enum zti_modes mode
= ztip
->zti_mode
;
843 uint_t value
= ztip
->zti_value
;
844 uint_t count
= ztip
->zti_count
;
845 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
848 boolean_t batch
= B_FALSE
;
850 if (mode
== ZTI_MODE_NULL
) {
852 tqs
->stqs_taskq
= NULL
;
856 ASSERT3U(count
, >, 0);
858 tqs
->stqs_count
= count
;
859 tqs
->stqs_taskq
= kmem_alloc(count
* sizeof (taskq_t
*), KM_SLEEP
);
863 ASSERT3U(value
, >=, 1);
864 value
= MAX(value
, 1);
869 flags
|= TASKQ_THREADS_CPU_PCT
;
870 value
= zio_taskq_batch_pct
;
874 panic("unrecognized mode for %s_%s taskq (%u:%u) in "
876 zio_type_name
[t
], zio_taskq_types
[q
], mode
, value
);
880 for (uint_t i
= 0; i
< count
; i
++) {
884 (void) snprintf(name
, sizeof (name
), "%s_%s_%u",
885 zio_type_name
[t
], zio_taskq_types
[q
], i
);
887 (void) snprintf(name
, sizeof (name
), "%s_%s",
888 zio_type_name
[t
], zio_taskq_types
[q
]);
891 if (zio_taskq_sysdc
&& spa
->spa_proc
!= &p0
) {
893 flags
|= TASKQ_DC_BATCH
;
895 tq
= taskq_create_sysdc(name
, value
, 50, INT_MAX
,
896 spa
->spa_proc
, zio_taskq_basedc
, flags
);
898 pri_t pri
= maxclsyspri
;
900 * The write issue taskq can be extremely CPU
901 * intensive. Run it at slightly lower priority
902 * than the other taskqs.
904 if (t
== ZIO_TYPE_WRITE
&& q
== ZIO_TASKQ_ISSUE
)
907 tq
= taskq_create_proc(name
, value
, pri
, 50,
908 INT_MAX
, spa
->spa_proc
, flags
);
911 tqs
->stqs_taskq
[i
] = tq
;
916 spa_taskqs_fini(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
918 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
920 if (tqs
->stqs_taskq
== NULL
) {
921 ASSERT0(tqs
->stqs_count
);
925 for (uint_t i
= 0; i
< tqs
->stqs_count
; i
++) {
926 ASSERT3P(tqs
->stqs_taskq
[i
], !=, NULL
);
927 taskq_destroy(tqs
->stqs_taskq
[i
]);
930 kmem_free(tqs
->stqs_taskq
, tqs
->stqs_count
* sizeof (taskq_t
*));
931 tqs
->stqs_taskq
= NULL
;
935 * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority.
936 * Note that a type may have multiple discrete taskqs to avoid lock contention
937 * on the taskq itself. In that case we choose which taskq at random by using
938 * the low bits of gethrtime().
941 spa_taskq_dispatch_ent(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
942 task_func_t
*func
, void *arg
, uint_t flags
, taskq_ent_t
*ent
)
944 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
947 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
948 ASSERT3U(tqs
->stqs_count
, !=, 0);
950 if (tqs
->stqs_count
== 1) {
951 tq
= tqs
->stqs_taskq
[0];
953 tq
= tqs
->stqs_taskq
[gethrtime() % tqs
->stqs_count
];
956 taskq_dispatch_ent(tq
, func
, arg
, flags
, ent
);
960 spa_create_zio_taskqs(spa_t
*spa
)
962 for (int t
= 0; t
< ZIO_TYPES
; t
++) {
963 for (int q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
964 spa_taskqs_init(spa
, t
, q
);
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 /* Only create a process if we're going to be around a while. */
1060 if (spa_create_process
&& strcmp(spa
->spa_name
, TRYIMPORT_NAME
) != 0) {
1061 if (newproc(spa_thread
, (caddr_t
)spa
, syscid
, maxclsyspri
,
1063 spa
->spa_proc_state
= SPA_PROC_CREATED
;
1064 while (spa
->spa_proc_state
== SPA_PROC_CREATED
) {
1065 cv_wait(&spa
->spa_proc_cv
,
1066 &spa
->spa_proc_lock
);
1068 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1069 ASSERT(spa
->spa_proc
!= &p0
);
1070 ASSERT(spa
->spa_did
!= 0);
1074 "Couldn't create process for zfs pool \"%s\"\n",
1079 mutex_exit(&spa
->spa_proc_lock
);
1081 /* If we didn't create a process, we need to create our taskqs. */
1082 if (spa
->spa_proc
== &p0
) {
1083 spa_create_zio_taskqs(spa
);
1086 list_create(&spa
->spa_config_dirty_list
, sizeof (vdev_t
),
1087 offsetof(vdev_t
, vdev_config_dirty_node
));
1088 list_create(&spa
->spa_evicting_os_list
, sizeof (objset_t
),
1089 offsetof(objset_t
, os_evicting_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
)
1110 ASSERT(spa
->spa_sync_on
== B_FALSE
);
1111 ASSERT(spa
->spa_dsl_pool
== NULL
);
1112 ASSERT(spa
->spa_root_vdev
== NULL
);
1113 ASSERT(spa
->spa_async_zio_root
== NULL
);
1114 ASSERT(spa
->spa_state
!= POOL_STATE_UNINITIALIZED
);
1116 spa_evicting_os_wait(spa
);
1118 txg_list_destroy(&spa
->spa_vdev_txg_list
);
1120 list_destroy(&spa
->spa_config_dirty_list
);
1121 list_destroy(&spa
->spa_evicting_os_list
);
1122 list_destroy(&spa
->spa_state_dirty_list
);
1124 for (int t
= 0; t
< ZIO_TYPES
; t
++) {
1125 for (int q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1126 spa_taskqs_fini(spa
, t
, q
);
1130 metaslab_class_destroy(spa
->spa_normal_class
);
1131 spa
->spa_normal_class
= NULL
;
1133 metaslab_class_destroy(spa
->spa_log_class
);
1134 spa
->spa_log_class
= NULL
;
1137 * If this was part of an import or the open otherwise failed, we may
1138 * still have errors left in the queues. Empty them just in case.
1140 spa_errlog_drain(spa
);
1142 avl_destroy(&spa
->spa_errlist_scrub
);
1143 avl_destroy(&spa
->spa_errlist_last
);
1145 spa
->spa_state
= POOL_STATE_UNINITIALIZED
;
1147 mutex_enter(&spa
->spa_proc_lock
);
1148 if (spa
->spa_proc_state
!= SPA_PROC_NONE
) {
1149 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1150 spa
->spa_proc_state
= SPA_PROC_DEACTIVATE
;
1151 cv_broadcast(&spa
->spa_proc_cv
);
1152 while (spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
) {
1153 ASSERT(spa
->spa_proc
!= &p0
);
1154 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1156 ASSERT(spa
->spa_proc_state
== SPA_PROC_GONE
);
1157 spa
->spa_proc_state
= SPA_PROC_NONE
;
1159 ASSERT(spa
->spa_proc
== &p0
);
1160 mutex_exit(&spa
->spa_proc_lock
);
1163 * We want to make sure spa_thread() has actually exited the ZFS
1164 * module, so that the module can't be unloaded out from underneath
1167 if (spa
->spa_did
!= 0) {
1168 thread_join(spa
->spa_did
);
1174 * Verify a pool configuration, and construct the vdev tree appropriately. This
1175 * will create all the necessary vdevs in the appropriate layout, with each vdev
1176 * in the CLOSED state. This will prep the pool before open/creation/import.
1177 * All vdev validation is done by the vdev_alloc() routine.
1180 spa_config_parse(spa_t
*spa
, vdev_t
**vdp
, nvlist_t
*nv
, vdev_t
*parent
,
1181 uint_t id
, int atype
)
1187 if ((error
= vdev_alloc(spa
, vdp
, nv
, parent
, id
, atype
)) != 0)
1190 if ((*vdp
)->vdev_ops
->vdev_op_leaf
)
1193 error
= nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1196 if (error
== ENOENT
)
1202 return (SET_ERROR(EINVAL
));
1205 for (int c
= 0; c
< children
; c
++) {
1207 if ((error
= spa_config_parse(spa
, &vd
, child
[c
], *vdp
, c
,
1215 ASSERT(*vdp
!= NULL
);
1221 * Opposite of spa_load().
1224 spa_unload(spa_t
*spa
)
1228 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1233 spa_async_suspend(spa
);
1238 if (spa
->spa_sync_on
) {
1239 txg_sync_stop(spa
->spa_dsl_pool
);
1240 spa
->spa_sync_on
= B_FALSE
;
1244 * Even though vdev_free() also calls vdev_metaslab_fini, we need
1245 * to call it earlier, before we wait for async i/o to complete.
1246 * This ensures that there is no async metaslab prefetching, by
1247 * calling taskq_wait(mg_taskq).
1249 if (spa
->spa_root_vdev
!= NULL
) {
1250 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1251 for (int c
= 0; c
< spa
->spa_root_vdev
->vdev_children
; c
++)
1252 vdev_metaslab_fini(spa
->spa_root_vdev
->vdev_child
[c
]);
1253 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1257 * Wait for any outstanding async I/O to complete.
1259 if (spa
->spa_async_zio_root
!= NULL
) {
1260 for (int i
= 0; i
< max_ncpus
; i
++)
1261 (void) zio_wait(spa
->spa_async_zio_root
[i
]);
1262 kmem_free(spa
->spa_async_zio_root
, max_ncpus
* sizeof (void *));
1263 spa
->spa_async_zio_root
= NULL
;
1266 bpobj_close(&spa
->spa_deferred_bpobj
);
1268 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1273 if (spa
->spa_root_vdev
)
1274 vdev_free(spa
->spa_root_vdev
);
1275 ASSERT(spa
->spa_root_vdev
== NULL
);
1278 * Close the dsl pool.
1280 if (spa
->spa_dsl_pool
) {
1281 dsl_pool_close(spa
->spa_dsl_pool
);
1282 spa
->spa_dsl_pool
= NULL
;
1283 spa
->spa_meta_objset
= NULL
;
1289 * Drop and purge level 2 cache
1291 spa_l2cache_drop(spa
);
1293 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1294 vdev_free(spa
->spa_spares
.sav_vdevs
[i
]);
1295 if (spa
->spa_spares
.sav_vdevs
) {
1296 kmem_free(spa
->spa_spares
.sav_vdevs
,
1297 spa
->spa_spares
.sav_count
* sizeof (void *));
1298 spa
->spa_spares
.sav_vdevs
= NULL
;
1300 if (spa
->spa_spares
.sav_config
) {
1301 nvlist_free(spa
->spa_spares
.sav_config
);
1302 spa
->spa_spares
.sav_config
= NULL
;
1304 spa
->spa_spares
.sav_count
= 0;
1306 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
1307 vdev_clear_stats(spa
->spa_l2cache
.sav_vdevs
[i
]);
1308 vdev_free(spa
->spa_l2cache
.sav_vdevs
[i
]);
1310 if (spa
->spa_l2cache
.sav_vdevs
) {
1311 kmem_free(spa
->spa_l2cache
.sav_vdevs
,
1312 spa
->spa_l2cache
.sav_count
* sizeof (void *));
1313 spa
->spa_l2cache
.sav_vdevs
= NULL
;
1315 if (spa
->spa_l2cache
.sav_config
) {
1316 nvlist_free(spa
->spa_l2cache
.sav_config
);
1317 spa
->spa_l2cache
.sav_config
= NULL
;
1319 spa
->spa_l2cache
.sav_count
= 0;
1321 spa
->spa_async_suspended
= 0;
1323 if (spa
->spa_comment
!= NULL
) {
1324 spa_strfree(spa
->spa_comment
);
1325 spa
->spa_comment
= NULL
;
1328 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1332 * Load (or re-load) the current list of vdevs describing the active spares for
1333 * this pool. When this is called, we have some form of basic information in
1334 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1335 * then re-generate a more complete list including status information.
1338 spa_load_spares(spa_t
*spa
)
1345 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1348 * First, close and free any existing spare vdevs.
1350 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1351 vd
= spa
->spa_spares
.sav_vdevs
[i
];
1353 /* Undo the call to spa_activate() below */
1354 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1355 B_FALSE
)) != NULL
&& tvd
->vdev_isspare
)
1356 spa_spare_remove(tvd
);
1361 if (spa
->spa_spares
.sav_vdevs
)
1362 kmem_free(spa
->spa_spares
.sav_vdevs
,
1363 spa
->spa_spares
.sav_count
* sizeof (void *));
1365 if (spa
->spa_spares
.sav_config
== NULL
)
1368 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
1369 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
1371 spa
->spa_spares
.sav_count
= (int)nspares
;
1372 spa
->spa_spares
.sav_vdevs
= NULL
;
1378 * Construct the array of vdevs, opening them to get status in the
1379 * process. For each spare, there is potentially two different vdev_t
1380 * structures associated with it: one in the list of spares (used only
1381 * for basic validation purposes) and one in the active vdev
1382 * configuration (if it's spared in). During this phase we open and
1383 * validate each vdev on the spare list. If the vdev also exists in the
1384 * active configuration, then we also mark this vdev as an active spare.
1386 spa
->spa_spares
.sav_vdevs
= kmem_alloc(nspares
* sizeof (void *),
1388 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1389 VERIFY(spa_config_parse(spa
, &vd
, spares
[i
], NULL
, 0,
1390 VDEV_ALLOC_SPARE
) == 0);
1393 spa
->spa_spares
.sav_vdevs
[i
] = vd
;
1395 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1396 B_FALSE
)) != NULL
) {
1397 if (!tvd
->vdev_isspare
)
1401 * We only mark the spare active if we were successfully
1402 * able to load the vdev. Otherwise, importing a pool
1403 * with a bad active spare would result in strange
1404 * behavior, because multiple pool would think the spare
1405 * is actively in use.
1407 * There is a vulnerability here to an equally bizarre
1408 * circumstance, where a dead active spare is later
1409 * brought back to life (onlined or otherwise). Given
1410 * the rarity of this scenario, and the extra complexity
1411 * it adds, we ignore the possibility.
1413 if (!vdev_is_dead(tvd
))
1414 spa_spare_activate(tvd
);
1418 vd
->vdev_aux
= &spa
->spa_spares
;
1420 if (vdev_open(vd
) != 0)
1423 if (vdev_validate_aux(vd
) == 0)
1428 * Recompute the stashed list of spares, with status information
1431 VERIFY(nvlist_remove(spa
->spa_spares
.sav_config
, ZPOOL_CONFIG_SPARES
,
1432 DATA_TYPE_NVLIST_ARRAY
) == 0);
1434 spares
= kmem_alloc(spa
->spa_spares
.sav_count
* sizeof (void *),
1436 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1437 spares
[i
] = vdev_config_generate(spa
,
1438 spa
->spa_spares
.sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_SPARE
);
1439 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
1440 ZPOOL_CONFIG_SPARES
, spares
, spa
->spa_spares
.sav_count
) == 0);
1441 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1442 nvlist_free(spares
[i
]);
1443 kmem_free(spares
, spa
->spa_spares
.sav_count
* sizeof (void *));
1447 * Load (or re-load) the current list of vdevs describing the active l2cache for
1448 * this pool. When this is called, we have some form of basic information in
1449 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1450 * then re-generate a more complete list including status information.
1451 * Devices which are already active have their details maintained, and are
1455 spa_load_l2cache(spa_t
*spa
)
1459 int i
, j
, oldnvdevs
;
1461 vdev_t
*vd
, **oldvdevs
, **newvdevs
;
1462 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
1464 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1466 if (sav
->sav_config
!= NULL
) {
1467 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
,
1468 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
1469 newvdevs
= kmem_alloc(nl2cache
* sizeof (void *), KM_SLEEP
);
1475 oldvdevs
= sav
->sav_vdevs
;
1476 oldnvdevs
= sav
->sav_count
;
1477 sav
->sav_vdevs
= NULL
;
1481 * Process new nvlist of vdevs.
1483 for (i
= 0; i
< nl2cache
; i
++) {
1484 VERIFY(nvlist_lookup_uint64(l2cache
[i
], ZPOOL_CONFIG_GUID
,
1488 for (j
= 0; j
< oldnvdevs
; j
++) {
1490 if (vd
!= NULL
&& guid
== vd
->vdev_guid
) {
1492 * Retain previous vdev for add/remove ops.
1500 if (newvdevs
[i
] == NULL
) {
1504 VERIFY(spa_config_parse(spa
, &vd
, l2cache
[i
], NULL
, 0,
1505 VDEV_ALLOC_L2CACHE
) == 0);
1510 * Commit this vdev as an l2cache device,
1511 * even if it fails to open.
1513 spa_l2cache_add(vd
);
1518 spa_l2cache_activate(vd
);
1520 if (vdev_open(vd
) != 0)
1523 (void) vdev_validate_aux(vd
);
1525 if (!vdev_is_dead(vd
))
1526 l2arc_add_vdev(spa
, vd
);
1531 * Purge vdevs that were dropped
1533 for (i
= 0; i
< oldnvdevs
; i
++) {
1538 ASSERT(vd
->vdev_isl2cache
);
1540 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
1541 pool
!= 0ULL && l2arc_vdev_present(vd
))
1542 l2arc_remove_vdev(vd
);
1543 vdev_clear_stats(vd
);
1549 kmem_free(oldvdevs
, oldnvdevs
* sizeof (void *));
1551 if (sav
->sav_config
== NULL
)
1554 sav
->sav_vdevs
= newvdevs
;
1555 sav
->sav_count
= (int)nl2cache
;
1558 * Recompute the stashed list of l2cache devices, with status
1559 * information this time.
1561 VERIFY(nvlist_remove(sav
->sav_config
, ZPOOL_CONFIG_L2CACHE
,
1562 DATA_TYPE_NVLIST_ARRAY
) == 0);
1564 l2cache
= kmem_alloc(sav
->sav_count
* sizeof (void *), KM_SLEEP
);
1565 for (i
= 0; i
< sav
->sav_count
; i
++)
1566 l2cache
[i
] = vdev_config_generate(spa
,
1567 sav
->sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_L2CACHE
);
1568 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
,
1569 ZPOOL_CONFIG_L2CACHE
, l2cache
, sav
->sav_count
) == 0);
1571 for (i
= 0; i
< sav
->sav_count
; i
++)
1572 nvlist_free(l2cache
[i
]);
1574 kmem_free(l2cache
, sav
->sav_count
* sizeof (void *));
1578 load_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
**value
)
1581 char *packed
= NULL
;
1586 error
= dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
);
1590 nvsize
= *(uint64_t *)db
->db_data
;
1591 dmu_buf_rele(db
, FTAG
);
1593 packed
= kmem_alloc(nvsize
, KM_SLEEP
);
1594 error
= dmu_read(spa
->spa_meta_objset
, obj
, 0, nvsize
, packed
,
1597 error
= nvlist_unpack(packed
, nvsize
, value
, 0);
1598 kmem_free(packed
, nvsize
);
1604 * Checks to see if the given vdev could not be opened, in which case we post a
1605 * sysevent to notify the autoreplace code that the device has been removed.
1608 spa_check_removed(vdev_t
*vd
)
1610 for (int c
= 0; c
< vd
->vdev_children
; c
++)
1611 spa_check_removed(vd
->vdev_child
[c
]);
1613 if (vd
->vdev_ops
->vdev_op_leaf
&& vdev_is_dead(vd
) &&
1615 zfs_post_autoreplace(vd
->vdev_spa
, vd
);
1616 spa_event_notify(vd
->vdev_spa
, vd
, ESC_ZFS_VDEV_CHECK
);
1621 spa_config_valid_zaps(vdev_t
*vd
, vdev_t
*mvd
)
1623 ASSERT3U(vd
->vdev_children
, ==, mvd
->vdev_children
);
1625 vd
->vdev_top_zap
= mvd
->vdev_top_zap
;
1626 vd
->vdev_leaf_zap
= mvd
->vdev_leaf_zap
;
1628 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
1629 spa_config_valid_zaps(vd
->vdev_child
[i
], mvd
->vdev_child
[i
]);
1634 * Validate the current config against the MOS config
1637 spa_config_valid(spa_t
*spa
, nvlist_t
*config
)
1639 vdev_t
*mrvd
, *rvd
= spa
->spa_root_vdev
;
1642 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nv
) == 0);
1644 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1645 VERIFY(spa_config_parse(spa
, &mrvd
, nv
, NULL
, 0, VDEV_ALLOC_LOAD
) == 0);
1647 ASSERT3U(rvd
->vdev_children
, ==, mrvd
->vdev_children
);
1650 * If we're doing a normal import, then build up any additional
1651 * diagnostic information about missing devices in this config.
1652 * We'll pass this up to the user for further processing.
1654 if (!(spa
->spa_import_flags
& ZFS_IMPORT_MISSING_LOG
)) {
1655 nvlist_t
**child
, *nv
;
1658 child
= kmem_alloc(rvd
->vdev_children
* sizeof (nvlist_t
**),
1660 VERIFY(nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
1662 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
1663 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1664 vdev_t
*mtvd
= mrvd
->vdev_child
[c
];
1666 if (tvd
->vdev_ops
== &vdev_missing_ops
&&
1667 mtvd
->vdev_ops
!= &vdev_missing_ops
&&
1669 child
[idx
++] = vdev_config_generate(spa
, mtvd
,
1674 VERIFY(nvlist_add_nvlist_array(nv
,
1675 ZPOOL_CONFIG_CHILDREN
, child
, idx
) == 0);
1676 VERIFY(nvlist_add_nvlist(spa
->spa_load_info
,
1677 ZPOOL_CONFIG_MISSING_DEVICES
, nv
) == 0);
1679 for (int i
= 0; i
< idx
; i
++)
1680 nvlist_free(child
[i
]);
1683 kmem_free(child
, rvd
->vdev_children
* sizeof (char **));
1687 * Compare the root vdev tree with the information we have
1688 * from the MOS config (mrvd). Check each top-level vdev
1689 * with the corresponding MOS config top-level (mtvd).
1691 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
1692 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1693 vdev_t
*mtvd
= mrvd
->vdev_child
[c
];
1696 * Resolve any "missing" vdevs in the current configuration.
1697 * If we find that the MOS config has more accurate information
1698 * about the top-level vdev then use that vdev instead.
1700 if (tvd
->vdev_ops
== &vdev_missing_ops
&&
1701 mtvd
->vdev_ops
!= &vdev_missing_ops
) {
1703 if (!(spa
->spa_import_flags
& ZFS_IMPORT_MISSING_LOG
))
1707 * Device specific actions.
1709 if (mtvd
->vdev_islog
) {
1710 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
1713 * XXX - once we have 'readonly' pool
1714 * support we should be able to handle
1715 * missing data devices by transitioning
1716 * the pool to readonly.
1722 * Swap the missing vdev with the data we were
1723 * able to obtain from the MOS config.
1725 vdev_remove_child(rvd
, tvd
);
1726 vdev_remove_child(mrvd
, mtvd
);
1728 vdev_add_child(rvd
, mtvd
);
1729 vdev_add_child(mrvd
, tvd
);
1731 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1733 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1737 if (mtvd
->vdev_islog
) {
1739 * Load the slog device's state from the MOS
1740 * config since it's possible that the label
1741 * does not contain the most up-to-date
1744 vdev_load_log_state(tvd
, mtvd
);
1749 * Per-vdev ZAP info is stored exclusively in the MOS.
1751 spa_config_valid_zaps(tvd
, mtvd
);
1756 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1759 * Ensure we were able to validate the config.
1761 return (rvd
->vdev_guid_sum
== spa
->spa_uberblock
.ub_guid_sum
);
1765 * Check for missing log devices
1768 spa_check_logs(spa_t
*spa
)
1770 boolean_t rv
= B_FALSE
;
1771 dsl_pool_t
*dp
= spa_get_dsl(spa
);
1773 switch (spa
->spa_log_state
) {
1774 case SPA_LOG_MISSING
:
1775 /* need to recheck in case slog has been restored */
1776 case SPA_LOG_UNKNOWN
:
1777 rv
= (dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
1778 zil_check_log_chain
, NULL
, DS_FIND_CHILDREN
) != 0);
1780 spa_set_log_state(spa
, SPA_LOG_MISSING
);
1787 spa_passivate_log(spa_t
*spa
)
1789 vdev_t
*rvd
= spa
->spa_root_vdev
;
1790 boolean_t slog_found
= B_FALSE
;
1792 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
1794 if (!spa_has_slogs(spa
))
1797 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
1798 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1799 metaslab_group_t
*mg
= tvd
->vdev_mg
;
1801 if (tvd
->vdev_islog
) {
1802 metaslab_group_passivate(mg
);
1803 slog_found
= B_TRUE
;
1807 return (slog_found
);
1811 spa_activate_log(spa_t
*spa
)
1813 vdev_t
*rvd
= spa
->spa_root_vdev
;
1815 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
1817 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
1818 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1819 metaslab_group_t
*mg
= tvd
->vdev_mg
;
1821 if (tvd
->vdev_islog
)
1822 metaslab_group_activate(mg
);
1827 spa_offline_log(spa_t
*spa
)
1831 error
= dmu_objset_find(spa_name(spa
), zil_vdev_offline
,
1832 NULL
, DS_FIND_CHILDREN
);
1835 * We successfully offlined the log device, sync out the
1836 * current txg so that the "stubby" block can be removed
1839 txg_wait_synced(spa
->spa_dsl_pool
, 0);
1845 spa_aux_check_removed(spa_aux_vdev_t
*sav
)
1847 for (int i
= 0; i
< sav
->sav_count
; i
++)
1848 spa_check_removed(sav
->sav_vdevs
[i
]);
1852 spa_claim_notify(zio_t
*zio
)
1854 spa_t
*spa
= zio
->io_spa
;
1859 mutex_enter(&spa
->spa_props_lock
); /* any mutex will do */
1860 if (spa
->spa_claim_max_txg
< zio
->io_bp
->blk_birth
)
1861 spa
->spa_claim_max_txg
= zio
->io_bp
->blk_birth
;
1862 mutex_exit(&spa
->spa_props_lock
);
1865 typedef struct spa_load_error
{
1866 uint64_t sle_meta_count
;
1867 uint64_t sle_data_count
;
1871 spa_load_verify_done(zio_t
*zio
)
1873 blkptr_t
*bp
= zio
->io_bp
;
1874 spa_load_error_t
*sle
= zio
->io_private
;
1875 dmu_object_type_t type
= BP_GET_TYPE(bp
);
1876 int error
= zio
->io_error
;
1877 spa_t
*spa
= zio
->io_spa
;
1880 if ((BP_GET_LEVEL(bp
) != 0 || DMU_OT_IS_METADATA(type
)) &&
1881 type
!= DMU_OT_INTENT_LOG
)
1882 atomic_inc_64(&sle
->sle_meta_count
);
1884 atomic_inc_64(&sle
->sle_data_count
);
1886 zio_data_buf_free(zio
->io_data
, zio
->io_size
);
1888 mutex_enter(&spa
->spa_scrub_lock
);
1889 spa
->spa_scrub_inflight
--;
1890 cv_broadcast(&spa
->spa_scrub_io_cv
);
1891 mutex_exit(&spa
->spa_scrub_lock
);
1895 * Maximum number of concurrent scrub i/os to create while verifying
1896 * a pool while importing it.
1898 int spa_load_verify_maxinflight
= 10000;
1899 boolean_t spa_load_verify_metadata
= B_TRUE
;
1900 boolean_t spa_load_verify_data
= B_TRUE
;
1904 spa_load_verify_cb(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
1905 const zbookmark_phys_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
1907 if (bp
== NULL
|| BP_IS_HOLE(bp
) || BP_IS_EMBEDDED(bp
))
1910 * Note: normally this routine will not be called if
1911 * spa_load_verify_metadata is not set. However, it may be useful
1912 * to manually set the flag after the traversal has begun.
1914 if (!spa_load_verify_metadata
)
1916 if (BP_GET_BUFC_TYPE(bp
) == ARC_BUFC_DATA
&& !spa_load_verify_data
)
1920 size_t size
= BP_GET_PSIZE(bp
);
1921 void *data
= zio_data_buf_alloc(size
);
1923 mutex_enter(&spa
->spa_scrub_lock
);
1924 while (spa
->spa_scrub_inflight
>= spa_load_verify_maxinflight
)
1925 cv_wait(&spa
->spa_scrub_io_cv
, &spa
->spa_scrub_lock
);
1926 spa
->spa_scrub_inflight
++;
1927 mutex_exit(&spa
->spa_scrub_lock
);
1929 zio_nowait(zio_read(rio
, spa
, bp
, data
, size
,
1930 spa_load_verify_done
, rio
->io_private
, ZIO_PRIORITY_SCRUB
,
1931 ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_CANFAIL
|
1932 ZIO_FLAG_SCRUB
| ZIO_FLAG_RAW
, zb
));
1938 verify_dataset_name_len(dsl_pool_t
*dp
, dsl_dataset_t
*ds
, void *arg
)
1940 if (dsl_dataset_namelen(ds
) >= ZFS_MAX_DATASET_NAME_LEN
)
1941 return (SET_ERROR(ENAMETOOLONG
));
1947 spa_load_verify(spa_t
*spa
)
1950 spa_load_error_t sle
= { 0 };
1951 zpool_rewind_policy_t policy
;
1952 boolean_t verify_ok
= B_FALSE
;
1955 zpool_get_rewind_policy(spa
->spa_config
, &policy
);
1957 if (policy
.zrp_request
& ZPOOL_NEVER_REWIND
)
1960 dsl_pool_config_enter(spa
->spa_dsl_pool
, FTAG
);
1961 error
= dmu_objset_find_dp(spa
->spa_dsl_pool
,
1962 spa
->spa_dsl_pool
->dp_root_dir_obj
, verify_dataset_name_len
, NULL
,
1964 dsl_pool_config_exit(spa
->spa_dsl_pool
, FTAG
);
1968 rio
= zio_root(spa
, NULL
, &sle
,
1969 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
);
1971 if (spa_load_verify_metadata
) {
1972 error
= traverse_pool(spa
, spa
->spa_verify_min_txg
,
1973 TRAVERSE_PRE
| TRAVERSE_PREFETCH_METADATA
,
1974 spa_load_verify_cb
, rio
);
1977 (void) zio_wait(rio
);
1979 spa
->spa_load_meta_errors
= sle
.sle_meta_count
;
1980 spa
->spa_load_data_errors
= sle
.sle_data_count
;
1982 if (!error
&& sle
.sle_meta_count
<= policy
.zrp_maxmeta
&&
1983 sle
.sle_data_count
<= policy
.zrp_maxdata
) {
1987 spa
->spa_load_txg
= spa
->spa_uberblock
.ub_txg
;
1988 spa
->spa_load_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
1990 loss
= spa
->spa_last_ubsync_txg_ts
- spa
->spa_load_txg_ts
;
1991 VERIFY(nvlist_add_uint64(spa
->spa_load_info
,
1992 ZPOOL_CONFIG_LOAD_TIME
, spa
->spa_load_txg_ts
) == 0);
1993 VERIFY(nvlist_add_int64(spa
->spa_load_info
,
1994 ZPOOL_CONFIG_REWIND_TIME
, loss
) == 0);
1995 VERIFY(nvlist_add_uint64(spa
->spa_load_info
,
1996 ZPOOL_CONFIG_LOAD_DATA_ERRORS
, sle
.sle_data_count
) == 0);
1998 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
;
2002 if (error
!= ENXIO
&& error
!= EIO
)
2003 error
= SET_ERROR(EIO
);
2007 return (verify_ok
? 0 : EIO
);
2011 * Find a value in the pool props object.
2014 spa_prop_find(spa_t
*spa
, zpool_prop_t prop
, uint64_t *val
)
2016 (void) zap_lookup(spa
->spa_meta_objset
, spa
->spa_pool_props_object
,
2017 zpool_prop_to_name(prop
), sizeof (uint64_t), 1, val
);
2021 * Find a value in the pool directory object.
2024 spa_dir_prop(spa_t
*spa
, const char *name
, uint64_t *val
)
2026 return (zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
2027 name
, sizeof (uint64_t), 1, val
));
2031 spa_vdev_err(vdev_t
*vdev
, vdev_aux_t aux
, int err
)
2033 vdev_set_state(vdev
, B_TRUE
, VDEV_STATE_CANT_OPEN
, aux
);
2038 * Fix up config after a partly-completed split. This is done with the
2039 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
2040 * pool have that entry in their config, but only the splitting one contains
2041 * a list of all the guids of the vdevs that are being split off.
2043 * This function determines what to do with that list: either rejoin
2044 * all the disks to the pool, or complete the splitting process. To attempt
2045 * the rejoin, each disk that is offlined is marked online again, and
2046 * we do a reopen() call. If the vdev label for every disk that was
2047 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
2048 * then we call vdev_split() on each disk, and complete the split.
2050 * Otherwise we leave the config alone, with all the vdevs in place in
2051 * the original pool.
2054 spa_try_repair(spa_t
*spa
, nvlist_t
*config
)
2061 boolean_t attempt_reopen
;
2063 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
, &nvl
) != 0)
2066 /* check that the config is complete */
2067 if (nvlist_lookup_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
2068 &glist
, &gcount
) != 0)
2071 vd
= kmem_zalloc(gcount
* sizeof (vdev_t
*), KM_SLEEP
);
2073 /* attempt to online all the vdevs & validate */
2074 attempt_reopen
= B_TRUE
;
2075 for (i
= 0; i
< gcount
; i
++) {
2076 if (glist
[i
] == 0) /* vdev is hole */
2079 vd
[i
] = spa_lookup_by_guid(spa
, glist
[i
], B_FALSE
);
2080 if (vd
[i
] == NULL
) {
2082 * Don't bother attempting to reopen the disks;
2083 * just do the split.
2085 attempt_reopen
= B_FALSE
;
2087 /* attempt to re-online it */
2088 vd
[i
]->vdev_offline
= B_FALSE
;
2092 if (attempt_reopen
) {
2093 vdev_reopen(spa
->spa_root_vdev
);
2095 /* check each device to see what state it's in */
2096 for (extracted
= 0, i
= 0; i
< gcount
; i
++) {
2097 if (vd
[i
] != NULL
&&
2098 vd
[i
]->vdev_stat
.vs_aux
!= VDEV_AUX_SPLIT_POOL
)
2105 * If every disk has been moved to the new pool, or if we never
2106 * even attempted to look at them, then we split them off for
2109 if (!attempt_reopen
|| gcount
== extracted
) {
2110 for (i
= 0; i
< gcount
; i
++)
2113 vdev_reopen(spa
->spa_root_vdev
);
2116 kmem_free(vd
, gcount
* sizeof (vdev_t
*));
2120 spa_load(spa_t
*spa
, spa_load_state_t state
, spa_import_type_t type
,
2121 boolean_t mosconfig
)
2123 nvlist_t
*config
= spa
->spa_config
;
2124 char *ereport
= FM_EREPORT_ZFS_POOL
;
2130 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
, &pool_guid
))
2131 return (SET_ERROR(EINVAL
));
2133 ASSERT(spa
->spa_comment
== NULL
);
2134 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
2135 spa
->spa_comment
= spa_strdup(comment
);
2138 * Versioning wasn't explicitly added to the label until later, so if
2139 * it's not present treat it as the initial version.
2141 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
2142 &spa
->spa_ubsync
.ub_version
) != 0)
2143 spa
->spa_ubsync
.ub_version
= SPA_VERSION_INITIAL
;
2145 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
2146 &spa
->spa_config_txg
);
2148 if ((state
== SPA_LOAD_IMPORT
|| state
== SPA_LOAD_TRYIMPORT
) &&
2149 spa_guid_exists(pool_guid
, 0)) {
2150 error
= SET_ERROR(EEXIST
);
2152 spa
->spa_config_guid
= pool_guid
;
2154 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
,
2156 VERIFY(nvlist_dup(nvl
, &spa
->spa_config_splitting
,
2160 nvlist_free(spa
->spa_load_info
);
2161 spa
->spa_load_info
= fnvlist_alloc();
2163 gethrestime(&spa
->spa_loaded_ts
);
2164 error
= spa_load_impl(spa
, pool_guid
, config
, state
, type
,
2165 mosconfig
, &ereport
);
2169 * Don't count references from objsets that are already closed
2170 * and are making their way through the eviction process.
2172 spa_evicting_os_wait(spa
);
2173 spa
->spa_minref
= refcount_count(&spa
->spa_refcount
);
2175 if (error
!= EEXIST
) {
2176 spa
->spa_loaded_ts
.tv_sec
= 0;
2177 spa
->spa_loaded_ts
.tv_nsec
= 0;
2179 if (error
!= EBADF
) {
2180 zfs_ereport_post(ereport
, spa
, NULL
, NULL
, 0, 0);
2183 spa
->spa_load_state
= error
? SPA_LOAD_ERROR
: SPA_LOAD_NONE
;
2190 * Count the number of per-vdev ZAPs associated with all of the vdevs in the
2191 * vdev tree rooted in the given vd, and ensure that each ZAP is present in the
2192 * spa's per-vdev ZAP list.
2195 vdev_count_verify_zaps(vdev_t
*vd
)
2197 spa_t
*spa
= vd
->vdev_spa
;
2199 if (vd
->vdev_top_zap
!= 0) {
2201 ASSERT0(zap_lookup_int(spa
->spa_meta_objset
,
2202 spa
->spa_all_vdev_zaps
, vd
->vdev_top_zap
));
2204 if (vd
->vdev_leaf_zap
!= 0) {
2206 ASSERT0(zap_lookup_int(spa
->spa_meta_objset
,
2207 spa
->spa_all_vdev_zaps
, vd
->vdev_leaf_zap
));
2210 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
2211 total
+= vdev_count_verify_zaps(vd
->vdev_child
[i
]);
2218 * Load an existing storage pool, using the pool's builtin spa_config as a
2219 * source of configuration information.
2222 spa_load_impl(spa_t
*spa
, uint64_t pool_guid
, nvlist_t
*config
,
2223 spa_load_state_t state
, spa_import_type_t type
, boolean_t mosconfig
,
2227 nvlist_t
*nvroot
= NULL
;
2230 uberblock_t
*ub
= &spa
->spa_uberblock
;
2231 uint64_t children
, config_cache_txg
= spa
->spa_config_txg
;
2232 int orig_mode
= spa
->spa_mode
;
2235 boolean_t missing_feat_write
= B_FALSE
;
2238 * If this is an untrusted config, access the pool in read-only mode.
2239 * This prevents things like resilvering recently removed devices.
2242 spa
->spa_mode
= FREAD
;
2244 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
2246 spa
->spa_load_state
= state
;
2248 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvroot
))
2249 return (SET_ERROR(EINVAL
));
2251 parse
= (type
== SPA_IMPORT_EXISTING
?
2252 VDEV_ALLOC_LOAD
: VDEV_ALLOC_SPLIT
);
2255 * Create "The Godfather" zio to hold all async IOs
2257 spa
->spa_async_zio_root
= kmem_alloc(max_ncpus
* sizeof (void *),
2259 for (int i
= 0; i
< max_ncpus
; i
++) {
2260 spa
->spa_async_zio_root
[i
] = zio_root(spa
, NULL
, NULL
,
2261 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
2262 ZIO_FLAG_GODFATHER
);
2266 * Parse the configuration into a vdev tree. We explicitly set the
2267 * value that will be returned by spa_version() since parsing the
2268 * configuration requires knowing the version number.
2270 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2271 error
= spa_config_parse(spa
, &rvd
, nvroot
, NULL
, 0, parse
);
2272 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2277 ASSERT(spa
->spa_root_vdev
== rvd
);
2278 ASSERT3U(spa
->spa_min_ashift
, >=, SPA_MINBLOCKSHIFT
);
2279 ASSERT3U(spa
->spa_max_ashift
, <=, SPA_MAXBLOCKSHIFT
);
2281 if (type
!= SPA_IMPORT_ASSEMBLE
) {
2282 ASSERT(spa_guid(spa
) == pool_guid
);
2286 * Try to open all vdevs, loading each label in the process.
2288 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2289 error
= vdev_open(rvd
);
2290 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2295 * We need to validate the vdev labels against the configuration that
2296 * we have in hand, which is dependent on the setting of mosconfig. If
2297 * mosconfig is true then we're validating the vdev labels based on
2298 * that config. Otherwise, we're validating against the cached config
2299 * (zpool.cache) that was read when we loaded the zfs module, and then
2300 * later we will recursively call spa_load() and validate against
2303 * If we're assembling a new pool that's been split off from an
2304 * existing pool, the labels haven't yet been updated so we skip
2305 * validation for now.
2307 if (type
!= SPA_IMPORT_ASSEMBLE
) {
2308 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2309 error
= vdev_validate(rvd
, mosconfig
);
2310 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2315 if (rvd
->vdev_state
<= VDEV_STATE_CANT_OPEN
)
2316 return (SET_ERROR(ENXIO
));
2320 * Find the best uberblock.
2322 vdev_uberblock_load(rvd
, ub
, &label
);
2325 * If we weren't able to find a single valid uberblock, return failure.
2327 if (ub
->ub_txg
== 0) {
2329 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, ENXIO
));
2333 * If the pool has an unsupported version we can't open it.
2335 if (!SPA_VERSION_IS_SUPPORTED(ub
->ub_version
)) {
2337 return (spa_vdev_err(rvd
, VDEV_AUX_VERSION_NEWER
, ENOTSUP
));
2340 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
2344 * If we weren't able to find what's necessary for reading the
2345 * MOS in the label, return failure.
2347 if (label
== NULL
|| nvlist_lookup_nvlist(label
,
2348 ZPOOL_CONFIG_FEATURES_FOR_READ
, &features
) != 0) {
2350 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
2355 * Update our in-core representation with the definitive values
2358 nvlist_free(spa
->spa_label_features
);
2359 VERIFY(nvlist_dup(features
, &spa
->spa_label_features
, 0) == 0);
2365 * Look through entries in the label nvlist's features_for_read. If
2366 * there is a feature listed there which we don't understand then we
2367 * cannot open a pool.
2369 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
2370 nvlist_t
*unsup_feat
;
2372 VERIFY(nvlist_alloc(&unsup_feat
, NV_UNIQUE_NAME
, KM_SLEEP
) ==
2375 for (nvpair_t
*nvp
= nvlist_next_nvpair(spa
->spa_label_features
,
2377 nvp
= nvlist_next_nvpair(spa
->spa_label_features
, nvp
)) {
2378 if (!zfeature_is_supported(nvpair_name(nvp
))) {
2379 VERIFY(nvlist_add_string(unsup_feat
,
2380 nvpair_name(nvp
), "") == 0);
2384 if (!nvlist_empty(unsup_feat
)) {
2385 VERIFY(nvlist_add_nvlist(spa
->spa_load_info
,
2386 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
) == 0);
2387 nvlist_free(unsup_feat
);
2388 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
2392 nvlist_free(unsup_feat
);
2396 * If the vdev guid sum doesn't match the uberblock, we have an
2397 * incomplete configuration. We first check to see if the pool
2398 * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN).
2399 * If it is, defer the vdev_guid_sum check till later so we
2400 * can handle missing vdevs.
2402 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VDEV_CHILDREN
,
2403 &children
) != 0 && mosconfig
&& type
!= SPA_IMPORT_ASSEMBLE
&&
2404 rvd
->vdev_guid_sum
!= ub
->ub_guid_sum
)
2405 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
, ENXIO
));
2407 if (type
!= SPA_IMPORT_ASSEMBLE
&& spa
->spa_config_splitting
) {
2408 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2409 spa_try_repair(spa
, config
);
2410 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2411 nvlist_free(spa
->spa_config_splitting
);
2412 spa
->spa_config_splitting
= NULL
;
2416 * Initialize internal SPA structures.
2418 spa
->spa_state
= POOL_STATE_ACTIVE
;
2419 spa
->spa_ubsync
= spa
->spa_uberblock
;
2420 spa
->spa_verify_min_txg
= spa
->spa_extreme_rewind
?
2421 TXG_INITIAL
- 1 : spa_last_synced_txg(spa
) - TXG_DEFER_SIZE
- 1;
2422 spa
->spa_first_txg
= spa
->spa_last_ubsync_txg
?
2423 spa
->spa_last_ubsync_txg
: spa_last_synced_txg(spa
) + 1;
2424 spa
->spa_claim_max_txg
= spa
->spa_first_txg
;
2425 spa
->spa_prev_software_version
= ub
->ub_software_version
;
2427 error
= dsl_pool_init(spa
, spa
->spa_first_txg
, &spa
->spa_dsl_pool
);
2429 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2430 spa
->spa_meta_objset
= spa
->spa_dsl_pool
->dp_meta_objset
;
2432 if (spa_dir_prop(spa
, DMU_POOL_CONFIG
, &spa
->spa_config_object
) != 0)
2433 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2435 if (spa_version(spa
) >= SPA_VERSION_FEATURES
) {
2436 boolean_t missing_feat_read
= B_FALSE
;
2437 nvlist_t
*unsup_feat
, *enabled_feat
;
2439 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_READ
,
2440 &spa
->spa_feat_for_read_obj
) != 0) {
2441 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2444 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_WRITE
,
2445 &spa
->spa_feat_for_write_obj
) != 0) {
2446 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2449 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_DESCRIPTIONS
,
2450 &spa
->spa_feat_desc_obj
) != 0) {
2451 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2454 enabled_feat
= fnvlist_alloc();
2455 unsup_feat
= fnvlist_alloc();
2457 if (!spa_features_check(spa
, B_FALSE
,
2458 unsup_feat
, enabled_feat
))
2459 missing_feat_read
= B_TRUE
;
2461 if (spa_writeable(spa
) || state
== SPA_LOAD_TRYIMPORT
) {
2462 if (!spa_features_check(spa
, B_TRUE
,
2463 unsup_feat
, enabled_feat
)) {
2464 missing_feat_write
= B_TRUE
;
2468 fnvlist_add_nvlist(spa
->spa_load_info
,
2469 ZPOOL_CONFIG_ENABLED_FEAT
, enabled_feat
);
2471 if (!nvlist_empty(unsup_feat
)) {
2472 fnvlist_add_nvlist(spa
->spa_load_info
,
2473 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
);
2476 fnvlist_free(enabled_feat
);
2477 fnvlist_free(unsup_feat
);
2479 if (!missing_feat_read
) {
2480 fnvlist_add_boolean(spa
->spa_load_info
,
2481 ZPOOL_CONFIG_CAN_RDONLY
);
2485 * If the state is SPA_LOAD_TRYIMPORT, our objective is
2486 * twofold: to determine whether the pool is available for
2487 * import in read-write mode and (if it is not) whether the
2488 * pool is available for import in read-only mode. If the pool
2489 * is available for import in read-write mode, it is displayed
2490 * as available in userland; if it is not available for import
2491 * in read-only mode, it is displayed as unavailable in
2492 * userland. If the pool is available for import in read-only
2493 * mode but not read-write mode, it is displayed as unavailable
2494 * in userland with a special note that the pool is actually
2495 * available for open in read-only mode.
2497 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
2498 * missing a feature for write, we must first determine whether
2499 * the pool can be opened read-only before returning to
2500 * userland in order to know whether to display the
2501 * abovementioned note.
2503 if (missing_feat_read
|| (missing_feat_write
&&
2504 spa_writeable(spa
))) {
2505 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
2510 * Load refcounts for ZFS features from disk into an in-memory
2511 * cache during SPA initialization.
2513 for (spa_feature_t i
= 0; i
< SPA_FEATURES
; i
++) {
2516 error
= feature_get_refcount_from_disk(spa
,
2517 &spa_feature_table
[i
], &refcount
);
2519 spa
->spa_feat_refcount_cache
[i
] = refcount
;
2520 } else if (error
== ENOTSUP
) {
2521 spa
->spa_feat_refcount_cache
[i
] =
2522 SPA_FEATURE_DISABLED
;
2524 return (spa_vdev_err(rvd
,
2525 VDEV_AUX_CORRUPT_DATA
, EIO
));
2530 if (spa_feature_is_active(spa
, SPA_FEATURE_ENABLED_TXG
)) {
2531 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_ENABLED_TXG
,
2532 &spa
->spa_feat_enabled_txg_obj
) != 0)
2533 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2536 spa
->spa_is_initializing
= B_TRUE
;
2537 error
= dsl_pool_open(spa
->spa_dsl_pool
);
2538 spa
->spa_is_initializing
= B_FALSE
;
2540 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2544 nvlist_t
*policy
= NULL
, *nvconfig
;
2546 if (load_nvlist(spa
, spa
->spa_config_object
, &nvconfig
) != 0)
2547 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2549 if (!spa_is_root(spa
) && nvlist_lookup_uint64(nvconfig
,
2550 ZPOOL_CONFIG_HOSTID
, &hostid
) == 0) {
2552 unsigned long myhostid
= 0;
2554 VERIFY(nvlist_lookup_string(nvconfig
,
2555 ZPOOL_CONFIG_HOSTNAME
, &hostname
) == 0);
2558 myhostid
= zone_get_hostid(NULL
);
2561 * We're emulating the system's hostid in userland, so
2562 * we can't use zone_get_hostid().
2564 (void) ddi_strtoul(hw_serial
, NULL
, 10, &myhostid
);
2565 #endif /* _KERNEL */
2566 if (hostid
!= 0 && myhostid
!= 0 &&
2567 hostid
!= myhostid
) {
2568 nvlist_free(nvconfig
);
2569 cmn_err(CE_WARN
, "pool '%s' could not be "
2570 "loaded as it was last accessed by "
2571 "another system (host: %s hostid: 0x%lx). "
2572 "See: http://illumos.org/msg/ZFS-8000-EY",
2573 spa_name(spa
), hostname
,
2574 (unsigned long)hostid
);
2575 return (SET_ERROR(EBADF
));
2578 if (nvlist_lookup_nvlist(spa
->spa_config
,
2579 ZPOOL_REWIND_POLICY
, &policy
) == 0)
2580 VERIFY(nvlist_add_nvlist(nvconfig
,
2581 ZPOOL_REWIND_POLICY
, policy
) == 0);
2583 spa_config_set(spa
, nvconfig
);
2585 spa_deactivate(spa
);
2586 spa_activate(spa
, orig_mode
);
2588 return (spa_load(spa
, state
, SPA_IMPORT_EXISTING
, B_TRUE
));
2591 /* Grab the secret checksum salt from the MOS. */
2592 error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
2593 DMU_POOL_CHECKSUM_SALT
, 1,
2594 sizeof (spa
->spa_cksum_salt
.zcs_bytes
),
2595 spa
->spa_cksum_salt
.zcs_bytes
);
2596 if (error
== ENOENT
) {
2597 /* Generate a new salt for subsequent use */
2598 (void) random_get_pseudo_bytes(spa
->spa_cksum_salt
.zcs_bytes
,
2599 sizeof (spa
->spa_cksum_salt
.zcs_bytes
));
2600 } else if (error
!= 0) {
2601 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2604 if (spa_dir_prop(spa
, DMU_POOL_SYNC_BPOBJ
, &obj
) != 0)
2605 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2606 error
= bpobj_open(&spa
->spa_deferred_bpobj
, spa
->spa_meta_objset
, obj
);
2608 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2611 * Load the bit that tells us to use the new accounting function
2612 * (raid-z deflation). If we have an older pool, this will not
2615 error
= spa_dir_prop(spa
, DMU_POOL_DEFLATE
, &spa
->spa_deflate
);
2616 if (error
!= 0 && error
!= ENOENT
)
2617 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2619 error
= spa_dir_prop(spa
, DMU_POOL_CREATION_VERSION
,
2620 &spa
->spa_creation_version
);
2621 if (error
!= 0 && error
!= ENOENT
)
2622 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2625 * Load the persistent error log. If we have an older pool, this will
2628 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_LAST
, &spa
->spa_errlog_last
);
2629 if (error
!= 0 && error
!= ENOENT
)
2630 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2632 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_SCRUB
,
2633 &spa
->spa_errlog_scrub
);
2634 if (error
!= 0 && error
!= ENOENT
)
2635 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2638 * Load the history object. If we have an older pool, this
2639 * will not be present.
2641 error
= spa_dir_prop(spa
, DMU_POOL_HISTORY
, &spa
->spa_history
);
2642 if (error
!= 0 && error
!= ENOENT
)
2643 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2646 * Load the per-vdev ZAP map. If we have an older pool, this will not
2647 * be present; in this case, defer its creation to a later time to
2648 * avoid dirtying the MOS this early / out of sync context. See
2649 * spa_sync_config_object.
2652 /* The sentinel is only available in the MOS config. */
2653 nvlist_t
*mos_config
;
2654 if (load_nvlist(spa
, spa
->spa_config_object
, &mos_config
) != 0)
2655 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2657 error
= spa_dir_prop(spa
, DMU_POOL_VDEV_ZAP_MAP
,
2658 &spa
->spa_all_vdev_zaps
);
2660 if (error
== ENOENT
) {
2661 VERIFY(!nvlist_exists(mos_config
,
2662 ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
));
2663 spa
->spa_avz_action
= AVZ_ACTION_INITIALIZE
;
2664 ASSERT0(vdev_count_verify_zaps(spa
->spa_root_vdev
));
2665 } else if (error
!= 0) {
2666 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2667 } else if (!nvlist_exists(mos_config
, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
)) {
2669 * An older version of ZFS overwrote the sentinel value, so
2670 * we have orphaned per-vdev ZAPs in the MOS. Defer their
2671 * destruction to later; see spa_sync_config_object.
2673 spa
->spa_avz_action
= AVZ_ACTION_DESTROY
;
2675 * We're assuming that no vdevs have had their ZAPs created
2676 * before this. Better be sure of it.
2678 ASSERT0(vdev_count_verify_zaps(spa
->spa_root_vdev
));
2680 nvlist_free(mos_config
);
2683 * If we're assembling the pool from the split-off vdevs of
2684 * an existing pool, we don't want to attach the spares & cache
2689 * Load any hot spares for this pool.
2691 error
= spa_dir_prop(spa
, DMU_POOL_SPARES
, &spa
->spa_spares
.sav_object
);
2692 if (error
!= 0 && error
!= ENOENT
)
2693 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2694 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
2695 ASSERT(spa_version(spa
) >= SPA_VERSION_SPARES
);
2696 if (load_nvlist(spa
, spa
->spa_spares
.sav_object
,
2697 &spa
->spa_spares
.sav_config
) != 0)
2698 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2700 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2701 spa_load_spares(spa
);
2702 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2703 } else if (error
== 0) {
2704 spa
->spa_spares
.sav_sync
= B_TRUE
;
2708 * Load any level 2 ARC devices for this pool.
2710 error
= spa_dir_prop(spa
, DMU_POOL_L2CACHE
,
2711 &spa
->spa_l2cache
.sav_object
);
2712 if (error
!= 0 && error
!= ENOENT
)
2713 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2714 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
2715 ASSERT(spa_version(spa
) >= SPA_VERSION_L2CACHE
);
2716 if (load_nvlist(spa
, spa
->spa_l2cache
.sav_object
,
2717 &spa
->spa_l2cache
.sav_config
) != 0)
2718 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2720 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2721 spa_load_l2cache(spa
);
2722 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2723 } else if (error
== 0) {
2724 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
2727 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
2729 error
= spa_dir_prop(spa
, DMU_POOL_PROPS
, &spa
->spa_pool_props_object
);
2730 if (error
&& error
!= ENOENT
)
2731 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2734 uint64_t autoreplace
;
2736 spa_prop_find(spa
, ZPOOL_PROP_BOOTFS
, &spa
->spa_bootfs
);
2737 spa_prop_find(spa
, ZPOOL_PROP_AUTOREPLACE
, &autoreplace
);
2738 spa_prop_find(spa
, ZPOOL_PROP_DELEGATION
, &spa
->spa_delegation
);
2739 spa_prop_find(spa
, ZPOOL_PROP_FAILUREMODE
, &spa
->spa_failmode
);
2740 spa_prop_find(spa
, ZPOOL_PROP_AUTOEXPAND
, &spa
->spa_autoexpand
);
2741 spa_prop_find(spa
, ZPOOL_PROP_DEDUPDITTO
,
2742 &spa
->spa_dedup_ditto
);
2744 spa
->spa_autoreplace
= (autoreplace
!= 0);
2748 * If the 'autoreplace' property is set, then post a resource notifying
2749 * the ZFS DE that it should not issue any faults for unopenable
2750 * devices. We also iterate over the vdevs, and post a sysevent for any
2751 * unopenable vdevs so that the normal autoreplace handler can take
2754 if (spa
->spa_autoreplace
&& state
!= SPA_LOAD_TRYIMPORT
) {
2755 spa_check_removed(spa
->spa_root_vdev
);
2757 * For the import case, this is done in spa_import(), because
2758 * at this point we're using the spare definitions from
2759 * the MOS config, not necessarily from the userland config.
2761 if (state
!= SPA_LOAD_IMPORT
) {
2762 spa_aux_check_removed(&spa
->spa_spares
);
2763 spa_aux_check_removed(&spa
->spa_l2cache
);
2768 * Load the vdev state for all toplevel vdevs.
2773 * Propagate the leaf DTLs we just loaded all the way up the tree.
2775 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2776 vdev_dtl_reassess(rvd
, 0, 0, B_FALSE
);
2777 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2780 * Load the DDTs (dedup tables).
2782 error
= ddt_load(spa
);
2784 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2786 spa_update_dspace(spa
);
2789 * Validate the config, using the MOS config to fill in any
2790 * information which might be missing. If we fail to validate
2791 * the config then declare the pool unfit for use. If we're
2792 * assembling a pool from a split, the log is not transferred
2795 if (type
!= SPA_IMPORT_ASSEMBLE
) {
2798 if (load_nvlist(spa
, spa
->spa_config_object
, &nvconfig
) != 0)
2799 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2801 if (!spa_config_valid(spa
, nvconfig
)) {
2802 nvlist_free(nvconfig
);
2803 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
,
2806 nvlist_free(nvconfig
);
2809 * Now that we've validated the config, check the state of the
2810 * root vdev. If it can't be opened, it indicates one or
2811 * more toplevel vdevs are faulted.
2813 if (rvd
->vdev_state
<= VDEV_STATE_CANT_OPEN
)
2814 return (SET_ERROR(ENXIO
));
2816 if (spa_writeable(spa
) && spa_check_logs(spa
)) {
2817 *ereport
= FM_EREPORT_ZFS_LOG_REPLAY
;
2818 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_LOG
, ENXIO
));
2822 if (missing_feat_write
) {
2823 ASSERT(state
== SPA_LOAD_TRYIMPORT
);
2826 * At this point, we know that we can open the pool in
2827 * read-only mode but not read-write mode. We now have enough
2828 * information and can return to userland.
2830 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
, ENOTSUP
));
2834 * We've successfully opened the pool, verify that we're ready
2835 * to start pushing transactions.
2837 if (state
!= SPA_LOAD_TRYIMPORT
) {
2838 if (error
= spa_load_verify(spa
))
2839 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
2843 if (spa_writeable(spa
) && (state
== SPA_LOAD_RECOVER
||
2844 spa
->spa_load_max_txg
== UINT64_MAX
)) {
2846 int need_update
= B_FALSE
;
2847 dsl_pool_t
*dp
= spa_get_dsl(spa
);
2849 ASSERT(state
!= SPA_LOAD_TRYIMPORT
);
2852 * Claim log blocks that haven't been committed yet.
2853 * This must all happen in a single txg.
2854 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
2855 * invoked from zil_claim_log_block()'s i/o done callback.
2856 * Price of rollback is that we abandon the log.
2858 spa
->spa_claiming
= B_TRUE
;
2860 tx
= dmu_tx_create_assigned(dp
, spa_first_txg(spa
));
2861 (void) dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
2862 zil_claim
, tx
, DS_FIND_CHILDREN
);
2865 spa
->spa_claiming
= B_FALSE
;
2867 spa_set_log_state(spa
, SPA_LOG_GOOD
);
2868 spa
->spa_sync_on
= B_TRUE
;
2869 txg_sync_start(spa
->spa_dsl_pool
);
2872 * Wait for all claims to sync. We sync up to the highest
2873 * claimed log block birth time so that claimed log blocks
2874 * don't appear to be from the future. spa_claim_max_txg
2875 * will have been set for us by either zil_check_log_chain()
2876 * (invoked from spa_check_logs()) or zil_claim() above.
2878 txg_wait_synced(spa
->spa_dsl_pool
, spa
->spa_claim_max_txg
);
2881 * If the config cache is stale, or we have uninitialized
2882 * metaslabs (see spa_vdev_add()), then update the config.
2884 * If this is a verbatim import, trust the current
2885 * in-core spa_config and update the disk labels.
2887 if (config_cache_txg
!= spa
->spa_config_txg
||
2888 state
== SPA_LOAD_IMPORT
||
2889 state
== SPA_LOAD_RECOVER
||
2890 (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
))
2891 need_update
= B_TRUE
;
2893 for (int c
= 0; c
< rvd
->vdev_children
; c
++)
2894 if (rvd
->vdev_child
[c
]->vdev_ms_array
== 0)
2895 need_update
= B_TRUE
;
2898 * Update the config cache asychronously in case we're the
2899 * root pool, in which case the config cache isn't writable yet.
2902 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
2905 * Check all DTLs to see if anything needs resilvering.
2907 if (!dsl_scan_resilvering(spa
->spa_dsl_pool
) &&
2908 vdev_resilver_needed(rvd
, NULL
, NULL
))
2909 spa_async_request(spa
, SPA_ASYNC_RESILVER
);
2912 * Log the fact that we booted up (so that we can detect if
2913 * we rebooted in the middle of an operation).
2915 spa_history_log_version(spa
, "open");
2918 * Delete any inconsistent datasets.
2920 (void) dmu_objset_find(spa_name(spa
),
2921 dsl_destroy_inconsistent
, NULL
, DS_FIND_CHILDREN
);
2924 * Clean up any stale temporary dataset userrefs.
2926 dsl_pool_clean_tmp_userrefs(spa
->spa_dsl_pool
);
2933 spa_load_retry(spa_t
*spa
, spa_load_state_t state
, int mosconfig
)
2935 int mode
= spa
->spa_mode
;
2938 spa_deactivate(spa
);
2940 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
- 1;
2942 spa_activate(spa
, mode
);
2943 spa_async_suspend(spa
);
2945 return (spa_load(spa
, state
, SPA_IMPORT_EXISTING
, mosconfig
));
2949 * If spa_load() fails this function will try loading prior txg's. If
2950 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
2951 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
2952 * function will not rewind the pool and will return the same error as
2956 spa_load_best(spa_t
*spa
, spa_load_state_t state
, int mosconfig
,
2957 uint64_t max_request
, int rewind_flags
)
2959 nvlist_t
*loadinfo
= NULL
;
2960 nvlist_t
*config
= NULL
;
2961 int load_error
, rewind_error
;
2962 uint64_t safe_rewind_txg
;
2965 if (spa
->spa_load_txg
&& state
== SPA_LOAD_RECOVER
) {
2966 spa
->spa_load_max_txg
= spa
->spa_load_txg
;
2967 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
2969 spa
->spa_load_max_txg
= max_request
;
2970 if (max_request
!= UINT64_MAX
)
2971 spa
->spa_extreme_rewind
= B_TRUE
;
2974 load_error
= rewind_error
= spa_load(spa
, state
, SPA_IMPORT_EXISTING
,
2976 if (load_error
== 0)
2979 if (spa
->spa_root_vdev
!= NULL
)
2980 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
2982 spa
->spa_last_ubsync_txg
= spa
->spa_uberblock
.ub_txg
;
2983 spa
->spa_last_ubsync_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
2985 if (rewind_flags
& ZPOOL_NEVER_REWIND
) {
2986 nvlist_free(config
);
2987 return (load_error
);
2990 if (state
== SPA_LOAD_RECOVER
) {
2991 /* Price of rolling back is discarding txgs, including log */
2992 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
2995 * If we aren't rolling back save the load info from our first
2996 * import attempt so that we can restore it after attempting
2999 loadinfo
= spa
->spa_load_info
;
3000 spa
->spa_load_info
= fnvlist_alloc();
3003 spa
->spa_load_max_txg
= spa
->spa_last_ubsync_txg
;
3004 safe_rewind_txg
= spa
->spa_last_ubsync_txg
- TXG_DEFER_SIZE
;
3005 min_txg
= (rewind_flags
& ZPOOL_EXTREME_REWIND
) ?
3006 TXG_INITIAL
: safe_rewind_txg
;
3009 * Continue as long as we're finding errors, we're still within
3010 * the acceptable rewind range, and we're still finding uberblocks
3012 while (rewind_error
&& spa
->spa_uberblock
.ub_txg
>= min_txg
&&
3013 spa
->spa_uberblock
.ub_txg
<= spa
->spa_load_max_txg
) {
3014 if (spa
->spa_load_max_txg
< safe_rewind_txg
)
3015 spa
->spa_extreme_rewind
= B_TRUE
;
3016 rewind_error
= spa_load_retry(spa
, state
, mosconfig
);
3019 spa
->spa_extreme_rewind
= B_FALSE
;
3020 spa
->spa_load_max_txg
= UINT64_MAX
;
3022 if (config
&& (rewind_error
|| state
!= SPA_LOAD_RECOVER
))
3023 spa_config_set(spa
, config
);
3025 if (state
== SPA_LOAD_RECOVER
) {
3026 ASSERT3P(loadinfo
, ==, NULL
);
3027 return (rewind_error
);
3029 /* Store the rewind info as part of the initial load info */
3030 fnvlist_add_nvlist(loadinfo
, ZPOOL_CONFIG_REWIND_INFO
,
3031 spa
->spa_load_info
);
3033 /* Restore the initial load info */
3034 fnvlist_free(spa
->spa_load_info
);
3035 spa
->spa_load_info
= loadinfo
;
3037 return (load_error
);
3044 * The import case is identical to an open except that the configuration is sent
3045 * down from userland, instead of grabbed from the configuration cache. For the
3046 * case of an open, the pool configuration will exist in the
3047 * POOL_STATE_UNINITIALIZED state.
3049 * The stats information (gen/count/ustats) is used to gather vdev statistics at
3050 * the same time open the pool, without having to keep around the spa_t in some
3054 spa_open_common(const char *pool
, spa_t
**spapp
, void *tag
, nvlist_t
*nvpolicy
,
3058 spa_load_state_t state
= SPA_LOAD_OPEN
;
3060 int locked
= B_FALSE
;
3065 * As disgusting as this is, we need to support recursive calls to this
3066 * function because dsl_dir_open() is called during spa_load(), and ends
3067 * up calling spa_open() again. The real fix is to figure out how to
3068 * avoid dsl_dir_open() calling this in the first place.
3070 if (mutex_owner(&spa_namespace_lock
) != curthread
) {
3071 mutex_enter(&spa_namespace_lock
);
3075 if ((spa
= spa_lookup(pool
)) == NULL
) {
3077 mutex_exit(&spa_namespace_lock
);
3078 return (SET_ERROR(ENOENT
));
3081 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
) {
3082 zpool_rewind_policy_t policy
;
3084 zpool_get_rewind_policy(nvpolicy
? nvpolicy
: spa
->spa_config
,
3086 if (policy
.zrp_request
& ZPOOL_DO_REWIND
)
3087 state
= SPA_LOAD_RECOVER
;
3089 spa_activate(spa
, spa_mode_global
);
3091 if (state
!= SPA_LOAD_RECOVER
)
3092 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
3094 error
= spa_load_best(spa
, state
, B_FALSE
, policy
.zrp_txg
,
3095 policy
.zrp_request
);
3097 if (error
== EBADF
) {
3099 * If vdev_validate() returns failure (indicated by
3100 * EBADF), it indicates that one of the vdevs indicates
3101 * that the pool has been exported or destroyed. If
3102 * this is the case, the config cache is out of sync and
3103 * we should remove the pool from the namespace.
3106 spa_deactivate(spa
);
3107 spa_config_sync(spa
, B_TRUE
, B_TRUE
);
3110 mutex_exit(&spa_namespace_lock
);
3111 return (SET_ERROR(ENOENT
));
3116 * We can't open the pool, but we still have useful
3117 * information: the state of each vdev after the
3118 * attempted vdev_open(). Return this to the user.
3120 if (config
!= NULL
&& spa
->spa_config
) {
3121 VERIFY(nvlist_dup(spa
->spa_config
, config
,
3123 VERIFY(nvlist_add_nvlist(*config
,
3124 ZPOOL_CONFIG_LOAD_INFO
,
3125 spa
->spa_load_info
) == 0);
3128 spa_deactivate(spa
);
3129 spa
->spa_last_open_failed
= error
;
3131 mutex_exit(&spa_namespace_lock
);
3137 spa_open_ref(spa
, tag
);
3140 *config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
3143 * If we've recovered the pool, pass back any information we
3144 * gathered while doing the load.
3146 if (state
== SPA_LOAD_RECOVER
) {
3147 VERIFY(nvlist_add_nvlist(*config
, ZPOOL_CONFIG_LOAD_INFO
,
3148 spa
->spa_load_info
) == 0);
3152 spa
->spa_last_open_failed
= 0;
3153 spa
->spa_last_ubsync_txg
= 0;
3154 spa
->spa_load_txg
= 0;
3155 mutex_exit(&spa_namespace_lock
);
3164 spa_open_rewind(const char *name
, spa_t
**spapp
, void *tag
, nvlist_t
*policy
,
3167 return (spa_open_common(name
, spapp
, tag
, policy
, config
));
3171 spa_open(const char *name
, spa_t
**spapp
, void *tag
)
3173 return (spa_open_common(name
, spapp
, tag
, NULL
, NULL
));
3177 * Lookup the given spa_t, incrementing the inject count in the process,
3178 * preventing it from being exported or destroyed.
3181 spa_inject_addref(char *name
)
3185 mutex_enter(&spa_namespace_lock
);
3186 if ((spa
= spa_lookup(name
)) == NULL
) {
3187 mutex_exit(&spa_namespace_lock
);
3190 spa
->spa_inject_ref
++;
3191 mutex_exit(&spa_namespace_lock
);
3197 spa_inject_delref(spa_t
*spa
)
3199 mutex_enter(&spa_namespace_lock
);
3200 spa
->spa_inject_ref
--;
3201 mutex_exit(&spa_namespace_lock
);
3205 * Add spares device information to the nvlist.
3208 spa_add_spares(spa_t
*spa
, nvlist_t
*config
)
3218 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
3220 if (spa
->spa_spares
.sav_count
== 0)
3223 VERIFY(nvlist_lookup_nvlist(config
,
3224 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
3225 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
3226 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
3228 VERIFY(nvlist_add_nvlist_array(nvroot
,
3229 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
3230 VERIFY(nvlist_lookup_nvlist_array(nvroot
,
3231 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
3234 * Go through and find any spares which have since been
3235 * repurposed as an active spare. If this is the case, update
3236 * their status appropriately.
3238 for (i
= 0; i
< nspares
; i
++) {
3239 VERIFY(nvlist_lookup_uint64(spares
[i
],
3240 ZPOOL_CONFIG_GUID
, &guid
) == 0);
3241 if (spa_spare_exists(guid
, &pool
, NULL
) &&
3243 VERIFY(nvlist_lookup_uint64_array(
3244 spares
[i
], ZPOOL_CONFIG_VDEV_STATS
,
3245 (uint64_t **)&vs
, &vsc
) == 0);
3246 vs
->vs_state
= VDEV_STATE_CANT_OPEN
;
3247 vs
->vs_aux
= VDEV_AUX_SPARED
;
3254 * Add l2cache device information to the nvlist, including vdev stats.
3257 spa_add_l2cache(spa_t
*spa
, nvlist_t
*config
)
3260 uint_t i
, j
, nl2cache
;
3267 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
3269 if (spa
->spa_l2cache
.sav_count
== 0)
3272 VERIFY(nvlist_lookup_nvlist(config
,
3273 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
3274 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_l2cache
.sav_config
,
3275 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
3276 if (nl2cache
!= 0) {
3277 VERIFY(nvlist_add_nvlist_array(nvroot
,
3278 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
3279 VERIFY(nvlist_lookup_nvlist_array(nvroot
,
3280 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
3283 * Update level 2 cache device stats.
3286 for (i
= 0; i
< nl2cache
; i
++) {
3287 VERIFY(nvlist_lookup_uint64(l2cache
[i
],
3288 ZPOOL_CONFIG_GUID
, &guid
) == 0);
3291 for (j
= 0; j
< spa
->spa_l2cache
.sav_count
; j
++) {
3293 spa
->spa_l2cache
.sav_vdevs
[j
]->vdev_guid
) {
3294 vd
= spa
->spa_l2cache
.sav_vdevs
[j
];
3300 VERIFY(nvlist_lookup_uint64_array(l2cache
[i
],
3301 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
, &vsc
)
3303 vdev_get_stats(vd
, vs
);
3309 spa_add_feature_stats(spa_t
*spa
, nvlist_t
*config
)
3315 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
3316 VERIFY(nvlist_alloc(&features
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
3318 if (spa
->spa_feat_for_read_obj
!= 0) {
3319 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
3320 spa
->spa_feat_for_read_obj
);
3321 zap_cursor_retrieve(&zc
, &za
) == 0;
3322 zap_cursor_advance(&zc
)) {
3323 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
3324 za
.za_num_integers
== 1);
3325 VERIFY3U(0, ==, nvlist_add_uint64(features
, za
.za_name
,
3326 za
.za_first_integer
));
3328 zap_cursor_fini(&zc
);
3331 if (spa
->spa_feat_for_write_obj
!= 0) {
3332 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
3333 spa
->spa_feat_for_write_obj
);
3334 zap_cursor_retrieve(&zc
, &za
) == 0;
3335 zap_cursor_advance(&zc
)) {
3336 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
3337 za
.za_num_integers
== 1);
3338 VERIFY3U(0, ==, nvlist_add_uint64(features
, za
.za_name
,
3339 za
.za_first_integer
));
3341 zap_cursor_fini(&zc
);
3344 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_FEATURE_STATS
,
3346 nvlist_free(features
);
3350 spa_get_stats(const char *name
, nvlist_t
**config
,
3351 char *altroot
, size_t buflen
)
3357 error
= spa_open_common(name
, &spa
, FTAG
, NULL
, config
);
3361 * This still leaves a window of inconsistency where the spares
3362 * or l2cache devices could change and the config would be
3363 * self-inconsistent.
3365 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
3367 if (*config
!= NULL
) {
3368 uint64_t loadtimes
[2];
3370 loadtimes
[0] = spa
->spa_loaded_ts
.tv_sec
;
3371 loadtimes
[1] = spa
->spa_loaded_ts
.tv_nsec
;
3372 VERIFY(nvlist_add_uint64_array(*config
,
3373 ZPOOL_CONFIG_LOADED_TIME
, loadtimes
, 2) == 0);
3375 VERIFY(nvlist_add_uint64(*config
,
3376 ZPOOL_CONFIG_ERRCOUNT
,
3377 spa_get_errlog_size(spa
)) == 0);
3379 if (spa_suspended(spa
))
3380 VERIFY(nvlist_add_uint64(*config
,
3381 ZPOOL_CONFIG_SUSPENDED
,
3382 spa
->spa_failmode
) == 0);
3384 spa_add_spares(spa
, *config
);
3385 spa_add_l2cache(spa
, *config
);
3386 spa_add_feature_stats(spa
, *config
);
3391 * We want to get the alternate root even for faulted pools, so we cheat
3392 * and call spa_lookup() directly.
3396 mutex_enter(&spa_namespace_lock
);
3397 spa
= spa_lookup(name
);
3399 spa_altroot(spa
, altroot
, buflen
);
3403 mutex_exit(&spa_namespace_lock
);
3405 spa_altroot(spa
, altroot
, buflen
);
3410 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
3411 spa_close(spa
, FTAG
);
3418 * Validate that the auxiliary device array is well formed. We must have an
3419 * array of nvlists, each which describes a valid leaf vdev. If this is an
3420 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
3421 * specified, as long as they are well-formed.
3424 spa_validate_aux_devs(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
,
3425 spa_aux_vdev_t
*sav
, const char *config
, uint64_t version
,
3426 vdev_labeltype_t label
)
3433 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
3436 * It's acceptable to have no devs specified.
3438 if (nvlist_lookup_nvlist_array(nvroot
, config
, &dev
, &ndev
) != 0)
3442 return (SET_ERROR(EINVAL
));
3445 * Make sure the pool is formatted with a version that supports this
3448 if (spa_version(spa
) < version
)
3449 return (SET_ERROR(ENOTSUP
));
3452 * Set the pending device list so we correctly handle device in-use
3455 sav
->sav_pending
= dev
;
3456 sav
->sav_npending
= ndev
;
3458 for (i
= 0; i
< ndev
; i
++) {
3459 if ((error
= spa_config_parse(spa
, &vd
, dev
[i
], NULL
, 0,
3463 if (!vd
->vdev_ops
->vdev_op_leaf
) {
3465 error
= SET_ERROR(EINVAL
);
3470 * The L2ARC currently only supports disk devices in
3471 * kernel context. For user-level testing, we allow it.
3474 if ((strcmp(config
, ZPOOL_CONFIG_L2CACHE
) == 0) &&
3475 strcmp(vd
->vdev_ops
->vdev_op_type
, VDEV_TYPE_DISK
) != 0) {
3476 error
= SET_ERROR(ENOTBLK
);
3483 if ((error
= vdev_open(vd
)) == 0 &&
3484 (error
= vdev_label_init(vd
, crtxg
, label
)) == 0) {
3485 VERIFY(nvlist_add_uint64(dev
[i
], ZPOOL_CONFIG_GUID
,
3486 vd
->vdev_guid
) == 0);
3492 (mode
!= VDEV_ALLOC_SPARE
&& mode
!= VDEV_ALLOC_L2CACHE
))
3499 sav
->sav_pending
= NULL
;
3500 sav
->sav_npending
= 0;
3505 spa_validate_aux(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
)
3509 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
3511 if ((error
= spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
3512 &spa
->spa_spares
, ZPOOL_CONFIG_SPARES
, SPA_VERSION_SPARES
,
3513 VDEV_LABEL_SPARE
)) != 0) {
3517 return (spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
3518 &spa
->spa_l2cache
, ZPOOL_CONFIG_L2CACHE
, SPA_VERSION_L2CACHE
,
3519 VDEV_LABEL_L2CACHE
));
3523 spa_set_aux_vdevs(spa_aux_vdev_t
*sav
, nvlist_t
**devs
, int ndevs
,
3528 if (sav
->sav_config
!= NULL
) {
3534 * Generate new dev list by concatentating with the
3537 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
, config
,
3538 &olddevs
, &oldndevs
) == 0);
3540 newdevs
= kmem_alloc(sizeof (void *) *
3541 (ndevs
+ oldndevs
), KM_SLEEP
);
3542 for (i
= 0; i
< oldndevs
; i
++)
3543 VERIFY(nvlist_dup(olddevs
[i
], &newdevs
[i
],
3545 for (i
= 0; i
< ndevs
; i
++)
3546 VERIFY(nvlist_dup(devs
[i
], &newdevs
[i
+ oldndevs
],
3549 VERIFY(nvlist_remove(sav
->sav_config
, config
,
3550 DATA_TYPE_NVLIST_ARRAY
) == 0);
3552 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
,
3553 config
, newdevs
, ndevs
+ oldndevs
) == 0);
3554 for (i
= 0; i
< oldndevs
+ ndevs
; i
++)
3555 nvlist_free(newdevs
[i
]);
3556 kmem_free(newdevs
, (oldndevs
+ ndevs
) * sizeof (void *));
3559 * Generate a new dev list.
3561 VERIFY(nvlist_alloc(&sav
->sav_config
, NV_UNIQUE_NAME
,
3563 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
, config
,
3569 * Stop and drop level 2 ARC devices
3572 spa_l2cache_drop(spa_t
*spa
)
3576 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
3578 for (i
= 0; i
< sav
->sav_count
; i
++) {
3581 vd
= sav
->sav_vdevs
[i
];
3584 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
3585 pool
!= 0ULL && l2arc_vdev_present(vd
))
3586 l2arc_remove_vdev(vd
);
3594 spa_create(const char *pool
, nvlist_t
*nvroot
, nvlist_t
*props
,
3598 char *altroot
= NULL
;
3603 uint64_t txg
= TXG_INITIAL
;
3604 nvlist_t
**spares
, **l2cache
;
3605 uint_t nspares
, nl2cache
;
3606 uint64_t version
, obj
;
3607 boolean_t has_features
;
3610 * If this pool already exists, return failure.
3612 mutex_enter(&spa_namespace_lock
);
3613 if (spa_lookup(pool
) != NULL
) {
3614 mutex_exit(&spa_namespace_lock
);
3615 return (SET_ERROR(EEXIST
));
3619 * Allocate a new spa_t structure.
3621 (void) nvlist_lookup_string(props
,
3622 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
3623 spa
= spa_add(pool
, NULL
, altroot
);
3624 spa_activate(spa
, spa_mode_global
);
3626 if (props
&& (error
= spa_prop_validate(spa
, props
))) {
3627 spa_deactivate(spa
);
3629 mutex_exit(&spa_namespace_lock
);
3633 has_features
= B_FALSE
;
3634 for (nvpair_t
*elem
= nvlist_next_nvpair(props
, NULL
);
3635 elem
!= NULL
; elem
= nvlist_next_nvpair(props
, elem
)) {
3636 if (zpool_prop_feature(nvpair_name(elem
)))
3637 has_features
= B_TRUE
;
3640 if (has_features
|| nvlist_lookup_uint64(props
,
3641 zpool_prop_to_name(ZPOOL_PROP_VERSION
), &version
) != 0) {
3642 version
= SPA_VERSION
;
3644 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
3646 spa
->spa_first_txg
= txg
;
3647 spa
->spa_uberblock
.ub_txg
= txg
- 1;
3648 spa
->spa_uberblock
.ub_version
= version
;
3649 spa
->spa_ubsync
= spa
->spa_uberblock
;
3650 spa
->spa_load_state
= SPA_LOAD_CREATE
;
3653 * Create "The Godfather" zio to hold all async IOs
3655 spa
->spa_async_zio_root
= kmem_alloc(max_ncpus
* sizeof (void *),
3657 for (int i
= 0; i
< max_ncpus
; i
++) {
3658 spa
->spa_async_zio_root
[i
] = zio_root(spa
, NULL
, NULL
,
3659 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
3660 ZIO_FLAG_GODFATHER
);
3664 * Create the root vdev.
3666 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3668 error
= spa_config_parse(spa
, &rvd
, nvroot
, NULL
, 0, VDEV_ALLOC_ADD
);
3670 ASSERT(error
!= 0 || rvd
!= NULL
);
3671 ASSERT(error
!= 0 || spa
->spa_root_vdev
== rvd
);
3673 if (error
== 0 && !zfs_allocatable_devs(nvroot
))
3674 error
= SET_ERROR(EINVAL
);
3677 (error
= vdev_create(rvd
, txg
, B_FALSE
)) == 0 &&
3678 (error
= spa_validate_aux(spa
, nvroot
, txg
,
3679 VDEV_ALLOC_ADD
)) == 0) {
3680 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
3681 vdev_metaslab_set_size(rvd
->vdev_child
[c
]);
3682 vdev_expand(rvd
->vdev_child
[c
], txg
);
3686 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3690 spa_deactivate(spa
);
3692 mutex_exit(&spa_namespace_lock
);
3697 * Get the list of spares, if specified.
3699 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
3700 &spares
, &nspares
) == 0) {
3701 VERIFY(nvlist_alloc(&spa
->spa_spares
.sav_config
, NV_UNIQUE_NAME
,
3703 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
3704 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
3705 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3706 spa_load_spares(spa
);
3707 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3708 spa
->spa_spares
.sav_sync
= B_TRUE
;
3712 * Get the list of level 2 cache devices, if specified.
3714 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
3715 &l2cache
, &nl2cache
) == 0) {
3716 VERIFY(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
3717 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
3718 VERIFY(nvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
3719 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
3720 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3721 spa_load_l2cache(spa
);
3722 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3723 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
3726 spa
->spa_is_initializing
= B_TRUE
;
3727 spa
->spa_dsl_pool
= dp
= dsl_pool_create(spa
, zplprops
, txg
);
3728 spa
->spa_meta_objset
= dp
->dp_meta_objset
;
3729 spa
->spa_is_initializing
= B_FALSE
;
3732 * Create DDTs (dedup tables).
3736 spa_update_dspace(spa
);
3738 tx
= dmu_tx_create_assigned(dp
, txg
);
3741 * Create the pool config object.
3743 spa
->spa_config_object
= dmu_object_alloc(spa
->spa_meta_objset
,
3744 DMU_OT_PACKED_NVLIST
, SPA_CONFIG_BLOCKSIZE
,
3745 DMU_OT_PACKED_NVLIST_SIZE
, sizeof (uint64_t), tx
);
3747 if (zap_add(spa
->spa_meta_objset
,
3748 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CONFIG
,
3749 sizeof (uint64_t), 1, &spa
->spa_config_object
, tx
) != 0) {
3750 cmn_err(CE_PANIC
, "failed to add pool config");
3753 if (spa_version(spa
) >= SPA_VERSION_FEATURES
)
3754 spa_feature_create_zap_objects(spa
, tx
);
3756 if (zap_add(spa
->spa_meta_objset
,
3757 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CREATION_VERSION
,
3758 sizeof (uint64_t), 1, &version
, tx
) != 0) {
3759 cmn_err(CE_PANIC
, "failed to add pool version");
3762 /* Newly created pools with the right version are always deflated. */
3763 if (version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
3764 spa
->spa_deflate
= TRUE
;
3765 if (zap_add(spa
->spa_meta_objset
,
3766 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
3767 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
) != 0) {
3768 cmn_err(CE_PANIC
, "failed to add deflate");
3773 * Create the deferred-free bpobj. Turn off compression
3774 * because sync-to-convergence takes longer if the blocksize
3777 obj
= bpobj_alloc(spa
->spa_meta_objset
, 1 << 14, tx
);
3778 dmu_object_set_compress(spa
->spa_meta_objset
, obj
,
3779 ZIO_COMPRESS_OFF
, tx
);
3780 if (zap_add(spa
->spa_meta_objset
,
3781 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_SYNC_BPOBJ
,
3782 sizeof (uint64_t), 1, &obj
, tx
) != 0) {
3783 cmn_err(CE_PANIC
, "failed to add bpobj");
3785 VERIFY3U(0, ==, bpobj_open(&spa
->spa_deferred_bpobj
,
3786 spa
->spa_meta_objset
, obj
));
3789 * Create the pool's history object.
3791 if (version
>= SPA_VERSION_ZPOOL_HISTORY
)
3792 spa_history_create_obj(spa
, tx
);
3795 * Generate some random noise for salted checksums to operate on.
3797 (void) random_get_pseudo_bytes(spa
->spa_cksum_salt
.zcs_bytes
,
3798 sizeof (spa
->spa_cksum_salt
.zcs_bytes
));
3801 * Set pool properties.
3803 spa
->spa_bootfs
= zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS
);
3804 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
3805 spa
->spa_failmode
= zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE
);
3806 spa
->spa_autoexpand
= zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND
);
3808 if (props
!= NULL
) {
3809 spa_configfile_set(spa
, props
, B_FALSE
);
3810 spa_sync_props(props
, tx
);
3815 spa
->spa_sync_on
= B_TRUE
;
3816 txg_sync_start(spa
->spa_dsl_pool
);
3819 * We explicitly wait for the first transaction to complete so that our
3820 * bean counters are appropriately updated.
3822 txg_wait_synced(spa
->spa_dsl_pool
, txg
);
3824 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
3825 spa_event_notify(spa
, NULL
, ESC_ZFS_POOL_CREATE
);
3827 spa_history_log_version(spa
, "create");
3830 * Don't count references from objsets that are already closed
3831 * and are making their way through the eviction process.
3833 spa_evicting_os_wait(spa
);
3834 spa
->spa_minref
= refcount_count(&spa
->spa_refcount
);
3835 spa
->spa_load_state
= SPA_LOAD_NONE
;
3837 mutex_exit(&spa_namespace_lock
);
3844 * Get the root pool information from the root disk, then import the root pool
3845 * during the system boot up time.
3847 extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t
**);
3850 spa_generate_rootconf(char *devpath
, char *devid
, uint64_t *guid
)
3853 nvlist_t
*nvtop
, *nvroot
;
3856 if (vdev_disk_read_rootlabel(devpath
, devid
, &config
) != 0)
3860 * Add this top-level vdev to the child array.
3862 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
3864 VERIFY(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
3866 VERIFY(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_GUID
, guid
) == 0);
3869 * Put this pool's top-level vdevs into a root vdev.
3871 VERIFY(nvlist_alloc(&nvroot
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
3872 VERIFY(nvlist_add_string(nvroot
, ZPOOL_CONFIG_TYPE
,
3873 VDEV_TYPE_ROOT
) == 0);
3874 VERIFY(nvlist_add_uint64(nvroot
, ZPOOL_CONFIG_ID
, 0ULL) == 0);
3875 VERIFY(nvlist_add_uint64(nvroot
, ZPOOL_CONFIG_GUID
, pgid
) == 0);
3876 VERIFY(nvlist_add_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
3880 * Replace the existing vdev_tree with the new root vdev in
3881 * this pool's configuration (remove the old, add the new).
3883 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, nvroot
) == 0);
3884 nvlist_free(nvroot
);
3889 * Walk the vdev tree and see if we can find a device with "better"
3890 * configuration. A configuration is "better" if the label on that
3891 * device has a more recent txg.
3894 spa_alt_rootvdev(vdev_t
*vd
, vdev_t
**avd
, uint64_t *txg
)
3896 for (int c
= 0; c
< vd
->vdev_children
; c
++)
3897 spa_alt_rootvdev(vd
->vdev_child
[c
], avd
, txg
);
3899 if (vd
->vdev_ops
->vdev_op_leaf
) {
3903 if (vdev_disk_read_rootlabel(vd
->vdev_physpath
, vd
->vdev_devid
,
3907 VERIFY(nvlist_lookup_uint64(label
, ZPOOL_CONFIG_POOL_TXG
,
3911 * Do we have a better boot device?
3913 if (label_txg
> *txg
) {
3922 * Import a root pool.
3924 * For x86. devpath_list will consist of devid and/or physpath name of
3925 * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
3926 * The GRUB "findroot" command will return the vdev we should boot.
3928 * For Sparc, devpath_list consists the physpath name of the booting device
3929 * no matter the rootpool is a single device pool or a mirrored pool.
3931 * "/pci@1f,0/ide@d/disk@0,0:a"
3934 spa_import_rootpool(char *devpath
, char *devid
)
3937 vdev_t
*rvd
, *bvd
, *avd
= NULL
;
3938 nvlist_t
*config
, *nvtop
;
3944 * Read the label from the boot device and generate a configuration.
3946 config
= spa_generate_rootconf(devpath
, devid
, &guid
);
3947 #if defined(_OBP) && defined(_KERNEL)
3948 if (config
== NULL
) {
3949 if (strstr(devpath
, "/iscsi/ssd") != NULL
) {
3951 get_iscsi_bootpath_phy(devpath
);
3952 config
= spa_generate_rootconf(devpath
, devid
, &guid
);
3956 if (config
== NULL
) {
3957 cmn_err(CE_NOTE
, "Cannot read the pool label from '%s'",
3959 return (SET_ERROR(EIO
));
3962 VERIFY(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
3964 VERIFY(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_TXG
, &txg
) == 0);
3966 mutex_enter(&spa_namespace_lock
);
3967 if ((spa
= spa_lookup(pname
)) != NULL
) {
3969 * Remove the existing root pool from the namespace so that we
3970 * can replace it with the correct config we just read in.
3975 spa
= spa_add(pname
, config
, NULL
);
3976 spa
->spa_is_root
= B_TRUE
;
3977 spa
->spa_import_flags
= ZFS_IMPORT_VERBATIM
;
3980 * Build up a vdev tree based on the boot device's label config.
3982 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
3984 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3985 error
= spa_config_parse(spa
, &rvd
, nvtop
, NULL
, 0,
3986 VDEV_ALLOC_ROOTPOOL
);
3987 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3989 mutex_exit(&spa_namespace_lock
);
3990 nvlist_free(config
);
3991 cmn_err(CE_NOTE
, "Can not parse the config for pool '%s'",
3997 * Get the boot vdev.
3999 if ((bvd
= vdev_lookup_by_guid(rvd
, guid
)) == NULL
) {
4000 cmn_err(CE_NOTE
, "Can not find the boot vdev for guid %llu",
4001 (u_longlong_t
)guid
);
4002 error
= SET_ERROR(ENOENT
);
4007 * Determine if there is a better boot device.
4010 spa_alt_rootvdev(rvd
, &avd
, &txg
);
4012 cmn_err(CE_NOTE
, "The boot device is 'degraded'. Please "
4013 "try booting from '%s'", avd
->vdev_path
);
4014 error
= SET_ERROR(EINVAL
);
4019 * If the boot device is part of a spare vdev then ensure that
4020 * we're booting off the active spare.
4022 if (bvd
->vdev_parent
->vdev_ops
== &vdev_spare_ops
&&
4023 !bvd
->vdev_isspare
) {
4024 cmn_err(CE_NOTE
, "The boot device is currently spared. Please "
4025 "try booting from '%s'",
4027 vdev_child
[bvd
->vdev_parent
->vdev_children
- 1]->vdev_path
);
4028 error
= SET_ERROR(EINVAL
);
4034 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4036 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4037 mutex_exit(&spa_namespace_lock
);
4039 nvlist_free(config
);
4046 * Import a non-root pool into the system.
4049 spa_import(const char *pool
, nvlist_t
*config
, nvlist_t
*props
, uint64_t flags
)
4052 char *altroot
= NULL
;
4053 spa_load_state_t state
= SPA_LOAD_IMPORT
;
4054 zpool_rewind_policy_t policy
;
4055 uint64_t mode
= spa_mode_global
;
4056 uint64_t readonly
= B_FALSE
;
4059 nvlist_t
**spares
, **l2cache
;
4060 uint_t nspares
, nl2cache
;
4063 * If a pool with this name exists, return failure.
4065 mutex_enter(&spa_namespace_lock
);
4066 if (spa_lookup(pool
) != NULL
) {
4067 mutex_exit(&spa_namespace_lock
);
4068 return (SET_ERROR(EEXIST
));
4072 * Create and initialize the spa structure.
4074 (void) nvlist_lookup_string(props
,
4075 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
4076 (void) nvlist_lookup_uint64(props
,
4077 zpool_prop_to_name(ZPOOL_PROP_READONLY
), &readonly
);
4080 spa
= spa_add(pool
, config
, altroot
);
4081 spa
->spa_import_flags
= flags
;
4084 * Verbatim import - Take a pool and insert it into the namespace
4085 * as if it had been loaded at boot.
4087 if (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
) {
4089 spa_configfile_set(spa
, props
, B_FALSE
);
4091 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
4092 spa_event_notify(spa
, NULL
, ESC_ZFS_POOL_IMPORT
);
4094 mutex_exit(&spa_namespace_lock
);
4098 spa_activate(spa
, mode
);
4101 * Don't start async tasks until we know everything is healthy.
4103 spa_async_suspend(spa
);
4105 zpool_get_rewind_policy(config
, &policy
);
4106 if (policy
.zrp_request
& ZPOOL_DO_REWIND
)
4107 state
= SPA_LOAD_RECOVER
;
4110 * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig
4111 * because the user-supplied config is actually the one to trust when
4114 if (state
!= SPA_LOAD_RECOVER
)
4115 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
4117 error
= spa_load_best(spa
, state
, B_TRUE
, policy
.zrp_txg
,
4118 policy
.zrp_request
);
4121 * Propagate anything learned while loading the pool and pass it
4122 * back to caller (i.e. rewind info, missing devices, etc).
4124 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
4125 spa
->spa_load_info
) == 0);
4127 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4129 * Toss any existing sparelist, as it doesn't have any validity
4130 * anymore, and conflicts with spa_has_spare().
4132 if (spa
->spa_spares
.sav_config
) {
4133 nvlist_free(spa
->spa_spares
.sav_config
);
4134 spa
->spa_spares
.sav_config
= NULL
;
4135 spa_load_spares(spa
);
4137 if (spa
->spa_l2cache
.sav_config
) {
4138 nvlist_free(spa
->spa_l2cache
.sav_config
);
4139 spa
->spa_l2cache
.sav_config
= NULL
;
4140 spa_load_l2cache(spa
);
4143 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
4146 error
= spa_validate_aux(spa
, nvroot
, -1ULL,
4149 error
= spa_validate_aux(spa
, nvroot
, -1ULL,
4150 VDEV_ALLOC_L2CACHE
);
4151 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4154 spa_configfile_set(spa
, props
, B_FALSE
);
4156 if (error
!= 0 || (props
&& spa_writeable(spa
) &&
4157 (error
= spa_prop_set(spa
, props
)))) {
4159 spa_deactivate(spa
);
4161 mutex_exit(&spa_namespace_lock
);
4165 spa_async_resume(spa
);
4168 * Override any spares and level 2 cache devices as specified by
4169 * the user, as these may have correct device names/devids, etc.
4171 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
4172 &spares
, &nspares
) == 0) {
4173 if (spa
->spa_spares
.sav_config
)
4174 VERIFY(nvlist_remove(spa
->spa_spares
.sav_config
,
4175 ZPOOL_CONFIG_SPARES
, DATA_TYPE_NVLIST_ARRAY
) == 0);
4177 VERIFY(nvlist_alloc(&spa
->spa_spares
.sav_config
,
4178 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
4179 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
4180 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
4181 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4182 spa_load_spares(spa
);
4183 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4184 spa
->spa_spares
.sav_sync
= B_TRUE
;
4186 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
4187 &l2cache
, &nl2cache
) == 0) {
4188 if (spa
->spa_l2cache
.sav_config
)
4189 VERIFY(nvlist_remove(spa
->spa_l2cache
.sav_config
,
4190 ZPOOL_CONFIG_L2CACHE
, DATA_TYPE_NVLIST_ARRAY
) == 0);
4192 VERIFY(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
4193 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
4194 VERIFY(nvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
4195 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
4196 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4197 spa_load_l2cache(spa
);
4198 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4199 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
4203 * Check for any removed devices.
4205 if (spa
->spa_autoreplace
) {
4206 spa_aux_check_removed(&spa
->spa_spares
);
4207 spa_aux_check_removed(&spa
->spa_l2cache
);
4210 if (spa_writeable(spa
)) {
4212 * Update the config cache to include the newly-imported pool.
4214 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
4218 * It's possible that the pool was expanded while it was exported.
4219 * We kick off an async task to handle this for us.
4221 spa_async_request(spa
, SPA_ASYNC_AUTOEXPAND
);
4223 spa_history_log_version(spa
, "import");
4225 spa_event_notify(spa
, NULL
, ESC_ZFS_POOL_IMPORT
);
4227 mutex_exit(&spa_namespace_lock
);
4233 spa_tryimport(nvlist_t
*tryconfig
)
4235 nvlist_t
*config
= NULL
;
4241 if (nvlist_lookup_string(tryconfig
, ZPOOL_CONFIG_POOL_NAME
, &poolname
))
4244 if (nvlist_lookup_uint64(tryconfig
, ZPOOL_CONFIG_POOL_STATE
, &state
))
4248 * Create and initialize the spa structure.
4250 mutex_enter(&spa_namespace_lock
);
4251 spa
= spa_add(TRYIMPORT_NAME
, tryconfig
, NULL
);
4252 spa_activate(spa
, FREAD
);
4255 * Pass off the heavy lifting to spa_load().
4256 * Pass TRUE for mosconfig because the user-supplied config
4257 * is actually the one to trust when doing an import.
4259 error
= spa_load(spa
, SPA_LOAD_TRYIMPORT
, SPA_IMPORT_EXISTING
, B_TRUE
);
4262 * If 'tryconfig' was at least parsable, return the current config.
4264 if (spa
->spa_root_vdev
!= NULL
) {
4265 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
4266 VERIFY(nvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
,
4268 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
4270 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_TIMESTAMP
,
4271 spa
->spa_uberblock
.ub_timestamp
) == 0);
4272 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
4273 spa
->spa_load_info
) == 0);
4276 * If the bootfs property exists on this pool then we
4277 * copy it out so that external consumers can tell which
4278 * pools are bootable.
4280 if ((!error
|| error
== EEXIST
) && spa
->spa_bootfs
) {
4281 char *tmpname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
4284 * We have to play games with the name since the
4285 * pool was opened as TRYIMPORT_NAME.
4287 if (dsl_dsobj_to_dsname(spa_name(spa
),
4288 spa
->spa_bootfs
, tmpname
) == 0) {
4290 char *dsname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
4292 cp
= strchr(tmpname
, '/');
4294 (void) strlcpy(dsname
, tmpname
,
4297 (void) snprintf(dsname
, MAXPATHLEN
,
4298 "%s/%s", poolname
, ++cp
);
4300 VERIFY(nvlist_add_string(config
,
4301 ZPOOL_CONFIG_BOOTFS
, dsname
) == 0);
4302 kmem_free(dsname
, MAXPATHLEN
);
4304 kmem_free(tmpname
, MAXPATHLEN
);
4308 * Add the list of hot spares and level 2 cache devices.
4310 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
4311 spa_add_spares(spa
, config
);
4312 spa_add_l2cache(spa
, config
);
4313 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
4317 spa_deactivate(spa
);
4319 mutex_exit(&spa_namespace_lock
);
4325 * Pool export/destroy
4327 * The act of destroying or exporting a pool is very simple. We make sure there
4328 * is no more pending I/O and any references to the pool are gone. Then, we
4329 * update the pool state and sync all the labels to disk, removing the
4330 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
4331 * we don't sync the labels or remove the configuration cache.
4334 spa_export_common(char *pool
, int new_state
, nvlist_t
**oldconfig
,
4335 boolean_t force
, boolean_t hardforce
)
4342 if (!(spa_mode_global
& FWRITE
))
4343 return (SET_ERROR(EROFS
));
4345 mutex_enter(&spa_namespace_lock
);
4346 if ((spa
= spa_lookup(pool
)) == NULL
) {
4347 mutex_exit(&spa_namespace_lock
);
4348 return (SET_ERROR(ENOENT
));
4352 * Put a hold on the pool, drop the namespace lock, stop async tasks,
4353 * reacquire the namespace lock, and see if we can export.
4355 spa_open_ref(spa
, FTAG
);
4356 mutex_exit(&spa_namespace_lock
);
4357 spa_async_suspend(spa
);
4358 mutex_enter(&spa_namespace_lock
);
4359 spa_close(spa
, FTAG
);
4362 * The pool will be in core if it's openable,
4363 * in which case we can modify its state.
4365 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
&& spa
->spa_sync_on
) {
4367 * Objsets may be open only because they're dirty, so we
4368 * have to force it to sync before checking spa_refcnt.
4370 txg_wait_synced(spa
->spa_dsl_pool
, 0);
4371 spa_evicting_os_wait(spa
);
4374 * A pool cannot be exported or destroyed if there are active
4375 * references. If we are resetting a pool, allow references by
4376 * fault injection handlers.
4378 if (!spa_refcount_zero(spa
) ||
4379 (spa
->spa_inject_ref
!= 0 &&
4380 new_state
!= POOL_STATE_UNINITIALIZED
)) {
4381 spa_async_resume(spa
);
4382 mutex_exit(&spa_namespace_lock
);
4383 return (SET_ERROR(EBUSY
));
4387 * A pool cannot be exported if it has an active shared spare.
4388 * This is to prevent other pools stealing the active spare
4389 * from an exported pool. At user's own will, such pool can
4390 * be forcedly exported.
4392 if (!force
&& new_state
== POOL_STATE_EXPORTED
&&
4393 spa_has_active_shared_spare(spa
)) {
4394 spa_async_resume(spa
);
4395 mutex_exit(&spa_namespace_lock
);
4396 return (SET_ERROR(EXDEV
));
4400 * We want this to be reflected on every label,
4401 * so mark them all dirty. spa_unload() will do the
4402 * final sync that pushes these changes out.
4404 if (new_state
!= POOL_STATE_UNINITIALIZED
&& !hardforce
) {
4405 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4406 spa
->spa_state
= new_state
;
4407 spa
->spa_final_txg
= spa_last_synced_txg(spa
) +
4409 vdev_config_dirty(spa
->spa_root_vdev
);
4410 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4414 spa_event_notify(spa
, NULL
, ESC_ZFS_POOL_DESTROY
);
4416 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
4418 spa_deactivate(spa
);
4421 if (oldconfig
&& spa
->spa_config
)
4422 VERIFY(nvlist_dup(spa
->spa_config
, oldconfig
, 0) == 0);
4424 if (new_state
!= POOL_STATE_UNINITIALIZED
) {
4426 spa_config_sync(spa
, B_TRUE
, B_TRUE
);
4429 mutex_exit(&spa_namespace_lock
);
4435 * Destroy a storage pool.
4438 spa_destroy(char *pool
)
4440 return (spa_export_common(pool
, POOL_STATE_DESTROYED
, NULL
,
4445 * Export a storage pool.
4448 spa_export(char *pool
, nvlist_t
**oldconfig
, boolean_t force
,
4449 boolean_t hardforce
)
4451 return (spa_export_common(pool
, POOL_STATE_EXPORTED
, oldconfig
,
4456 * Similar to spa_export(), this unloads the spa_t without actually removing it
4457 * from the namespace in any way.
4460 spa_reset(char *pool
)
4462 return (spa_export_common(pool
, POOL_STATE_UNINITIALIZED
, NULL
,
4467 * ==========================================================================
4468 * Device manipulation
4469 * ==========================================================================
4473 * Add a device to a storage pool.
4476 spa_vdev_add(spa_t
*spa
, nvlist_t
*nvroot
)
4480 vdev_t
*rvd
= spa
->spa_root_vdev
;
4482 nvlist_t
**spares
, **l2cache
;
4483 uint_t nspares
, nl2cache
;
4485 ASSERT(spa_writeable(spa
));
4487 txg
= spa_vdev_enter(spa
);
4489 if ((error
= spa_config_parse(spa
, &vd
, nvroot
, NULL
, 0,
4490 VDEV_ALLOC_ADD
)) != 0)
4491 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
4493 spa
->spa_pending_vdev
= vd
; /* spa_vdev_exit() will clear this */
4495 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
, &spares
,
4499 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
, &l2cache
,
4503 if (vd
->vdev_children
== 0 && nspares
== 0 && nl2cache
== 0)
4504 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
4506 if (vd
->vdev_children
!= 0 &&
4507 (error
= vdev_create(vd
, txg
, B_FALSE
)) != 0)
4508 return (spa_vdev_exit(spa
, vd
, txg
, error
));
4511 * We must validate the spares and l2cache devices after checking the
4512 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
4514 if ((error
= spa_validate_aux(spa
, nvroot
, txg
, VDEV_ALLOC_ADD
)) != 0)
4515 return (spa_vdev_exit(spa
, vd
, txg
, error
));
4518 * Transfer each new top-level vdev from vd to rvd.
4520 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
4523 * Set the vdev id to the first hole, if one exists.
4525 for (id
= 0; id
< rvd
->vdev_children
; id
++) {
4526 if (rvd
->vdev_child
[id
]->vdev_ishole
) {
4527 vdev_free(rvd
->vdev_child
[id
]);
4531 tvd
= vd
->vdev_child
[c
];
4532 vdev_remove_child(vd
, tvd
);
4534 vdev_add_child(rvd
, tvd
);
4535 vdev_config_dirty(tvd
);
4539 spa_set_aux_vdevs(&spa
->spa_spares
, spares
, nspares
,
4540 ZPOOL_CONFIG_SPARES
);
4541 spa_load_spares(spa
);
4542 spa
->spa_spares
.sav_sync
= B_TRUE
;
4545 if (nl2cache
!= 0) {
4546 spa_set_aux_vdevs(&spa
->spa_l2cache
, l2cache
, nl2cache
,
4547 ZPOOL_CONFIG_L2CACHE
);
4548 spa_load_l2cache(spa
);
4549 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
4553 * We have to be careful when adding new vdevs to an existing pool.
4554 * If other threads start allocating from these vdevs before we
4555 * sync the config cache, and we lose power, then upon reboot we may
4556 * fail to open the pool because there are DVAs that the config cache
4557 * can't translate. Therefore, we first add the vdevs without
4558 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
4559 * and then let spa_config_update() initialize the new metaslabs.
4561 * spa_load() checks for added-but-not-initialized vdevs, so that
4562 * if we lose power at any point in this sequence, the remaining
4563 * steps will be completed the next time we load the pool.
4565 (void) spa_vdev_exit(spa
, vd
, txg
, 0);
4567 mutex_enter(&spa_namespace_lock
);
4568 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
4569 spa_event_notify(spa
, NULL
, ESC_ZFS_VDEV_ADD
);
4570 mutex_exit(&spa_namespace_lock
);
4576 * Attach a device to a mirror. The arguments are the path to any device
4577 * in the mirror, and the nvroot for the new device. If the path specifies
4578 * a device that is not mirrored, we automatically insert the mirror vdev.
4580 * If 'replacing' is specified, the new device is intended to replace the
4581 * existing device; in this case the two devices are made into their own
4582 * mirror using the 'replacing' vdev, which is functionally identical to
4583 * the mirror vdev (it actually reuses all the same ops) but has a few
4584 * extra rules: you can't attach to it after it's been created, and upon
4585 * completion of resilvering, the first disk (the one being replaced)
4586 * is automatically detached.
4589 spa_vdev_attach(spa_t
*spa
, uint64_t guid
, nvlist_t
*nvroot
, int replacing
)
4591 uint64_t txg
, dtl_max_txg
;
4592 vdev_t
*rvd
= spa
->spa_root_vdev
;
4593 vdev_t
*oldvd
, *newvd
, *newrootvd
, *pvd
, *tvd
;
4595 char *oldvdpath
, *newvdpath
;
4599 ASSERT(spa_writeable(spa
));
4601 txg
= spa_vdev_enter(spa
);
4603 oldvd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
4606 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
4608 if (!oldvd
->vdev_ops
->vdev_op_leaf
)
4609 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4611 pvd
= oldvd
->vdev_parent
;
4613 if ((error
= spa_config_parse(spa
, &newrootvd
, nvroot
, NULL
, 0,
4614 VDEV_ALLOC_ATTACH
)) != 0)
4615 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
4617 if (newrootvd
->vdev_children
!= 1)
4618 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
4620 newvd
= newrootvd
->vdev_child
[0];
4622 if (!newvd
->vdev_ops
->vdev_op_leaf
)
4623 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
4625 if ((error
= vdev_create(newrootvd
, txg
, replacing
)) != 0)
4626 return (spa_vdev_exit(spa
, newrootvd
, txg
, error
));
4629 * Spares can't replace logs
4631 if (oldvd
->vdev_top
->vdev_islog
&& newvd
->vdev_isspare
)
4632 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4636 * For attach, the only allowable parent is a mirror or the root
4639 if (pvd
->vdev_ops
!= &vdev_mirror_ops
&&
4640 pvd
->vdev_ops
!= &vdev_root_ops
)
4641 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4643 pvops
= &vdev_mirror_ops
;
4646 * Active hot spares can only be replaced by inactive hot
4649 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
4650 oldvd
->vdev_isspare
&&
4651 !spa_has_spare(spa
, newvd
->vdev_guid
))
4652 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4655 * If the source is a hot spare, and the parent isn't already a
4656 * spare, then we want to create a new hot spare. Otherwise, we
4657 * want to create a replacing vdev. The user is not allowed to
4658 * attach to a spared vdev child unless the 'isspare' state is
4659 * the same (spare replaces spare, non-spare replaces
4662 if (pvd
->vdev_ops
== &vdev_replacing_ops
&&
4663 spa_version(spa
) < SPA_VERSION_MULTI_REPLACE
) {
4664 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4665 } else if (pvd
->vdev_ops
== &vdev_spare_ops
&&
4666 newvd
->vdev_isspare
!= oldvd
->vdev_isspare
) {
4667 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4670 if (newvd
->vdev_isspare
)
4671 pvops
= &vdev_spare_ops
;
4673 pvops
= &vdev_replacing_ops
;
4677 * Make sure the new device is big enough.
4679 if (newvd
->vdev_asize
< vdev_get_min_asize(oldvd
))
4680 return (spa_vdev_exit(spa
, newrootvd
, txg
, EOVERFLOW
));
4683 * The new device cannot have a higher alignment requirement
4684 * than the top-level vdev.
4686 if (newvd
->vdev_ashift
> oldvd
->vdev_top
->vdev_ashift
)
4687 return (spa_vdev_exit(spa
, newrootvd
, txg
, EDOM
));
4690 * If this is an in-place replacement, update oldvd's path and devid
4691 * to make it distinguishable from newvd, and unopenable from now on.
4693 if (strcmp(oldvd
->vdev_path
, newvd
->vdev_path
) == 0) {
4694 spa_strfree(oldvd
->vdev_path
);
4695 oldvd
->vdev_path
= kmem_alloc(strlen(newvd
->vdev_path
) + 5,
4697 (void) sprintf(oldvd
->vdev_path
, "%s/%s",
4698 newvd
->vdev_path
, "old");
4699 if (oldvd
->vdev_devid
!= NULL
) {
4700 spa_strfree(oldvd
->vdev_devid
);
4701 oldvd
->vdev_devid
= NULL
;
4705 /* mark the device being resilvered */
4706 newvd
->vdev_resilver_txg
= txg
;
4709 * If the parent is not a mirror, or if we're replacing, insert the new
4710 * mirror/replacing/spare vdev above oldvd.
4712 if (pvd
->vdev_ops
!= pvops
)
4713 pvd
= vdev_add_parent(oldvd
, pvops
);
4715 ASSERT(pvd
->vdev_top
->vdev_parent
== rvd
);
4716 ASSERT(pvd
->vdev_ops
== pvops
);
4717 ASSERT(oldvd
->vdev_parent
== pvd
);
4720 * Extract the new device from its root and add it to pvd.
4722 vdev_remove_child(newrootvd
, newvd
);
4723 newvd
->vdev_id
= pvd
->vdev_children
;
4724 newvd
->vdev_crtxg
= oldvd
->vdev_crtxg
;
4725 vdev_add_child(pvd
, newvd
);
4727 tvd
= newvd
->vdev_top
;
4728 ASSERT(pvd
->vdev_top
== tvd
);
4729 ASSERT(tvd
->vdev_parent
== rvd
);
4731 vdev_config_dirty(tvd
);
4734 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
4735 * for any dmu_sync-ed blocks. It will propagate upward when
4736 * spa_vdev_exit() calls vdev_dtl_reassess().
4738 dtl_max_txg
= txg
+ TXG_CONCURRENT_STATES
;
4740 vdev_dtl_dirty(newvd
, DTL_MISSING
, TXG_INITIAL
,
4741 dtl_max_txg
- TXG_INITIAL
);
4743 if (newvd
->vdev_isspare
) {
4744 spa_spare_activate(newvd
);
4745 spa_event_notify(spa
, newvd
, ESC_ZFS_VDEV_SPARE
);
4748 oldvdpath
= spa_strdup(oldvd
->vdev_path
);
4749 newvdpath
= spa_strdup(newvd
->vdev_path
);
4750 newvd_isspare
= newvd
->vdev_isspare
;
4753 * Mark newvd's DTL dirty in this txg.
4755 vdev_dirty(tvd
, VDD_DTL
, newvd
, txg
);
4758 * Schedule the resilver to restart in the future. We do this to
4759 * ensure that dmu_sync-ed blocks have been stitched into the
4760 * respective datasets.
4762 dsl_resilver_restart(spa
->spa_dsl_pool
, dtl_max_txg
);
4764 if (spa
->spa_bootfs
)
4765 spa_event_notify(spa
, newvd
, ESC_ZFS_BOOTFS_VDEV_ATTACH
);
4767 spa_event_notify(spa
, newvd
, ESC_ZFS_VDEV_ATTACH
);
4772 (void) spa_vdev_exit(spa
, newrootvd
, dtl_max_txg
, 0);
4774 spa_history_log_internal(spa
, "vdev attach", NULL
,
4775 "%s vdev=%s %s vdev=%s",
4776 replacing
&& newvd_isspare
? "spare in" :
4777 replacing
? "replace" : "attach", newvdpath
,
4778 replacing
? "for" : "to", oldvdpath
);
4780 spa_strfree(oldvdpath
);
4781 spa_strfree(newvdpath
);
4787 * Detach a device from a mirror or replacing vdev.
4789 * If 'replace_done' is specified, only detach if the parent
4790 * is a replacing vdev.
4793 spa_vdev_detach(spa_t
*spa
, uint64_t guid
, uint64_t pguid
, int replace_done
)
4797 vdev_t
*rvd
= spa
->spa_root_vdev
;
4798 vdev_t
*vd
, *pvd
, *cvd
, *tvd
;
4799 boolean_t unspare
= B_FALSE
;
4800 uint64_t unspare_guid
= 0;
4803 ASSERT(spa_writeable(spa
));
4805 txg
= spa_vdev_enter(spa
);
4807 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
4810 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
4812 if (!vd
->vdev_ops
->vdev_op_leaf
)
4813 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4815 pvd
= vd
->vdev_parent
;
4818 * If the parent/child relationship is not as expected, don't do it.
4819 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
4820 * vdev that's replacing B with C. The user's intent in replacing
4821 * is to go from M(A,B) to M(A,C). If the user decides to cancel
4822 * the replace by detaching C, the expected behavior is to end up
4823 * M(A,B). But suppose that right after deciding to detach C,
4824 * the replacement of B completes. We would have M(A,C), and then
4825 * ask to detach C, which would leave us with just A -- not what
4826 * the user wanted. To prevent this, we make sure that the
4827 * parent/child relationship hasn't changed -- in this example,
4828 * that C's parent is still the replacing vdev R.
4830 if (pvd
->vdev_guid
!= pguid
&& pguid
!= 0)
4831 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
4834 * Only 'replacing' or 'spare' vdevs can be replaced.
4836 if (replace_done
&& pvd
->vdev_ops
!= &vdev_replacing_ops
&&
4837 pvd
->vdev_ops
!= &vdev_spare_ops
)
4838 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4840 ASSERT(pvd
->vdev_ops
!= &vdev_spare_ops
||
4841 spa_version(spa
) >= SPA_VERSION_SPARES
);
4844 * Only mirror, replacing, and spare vdevs support detach.
4846 if (pvd
->vdev_ops
!= &vdev_replacing_ops
&&
4847 pvd
->vdev_ops
!= &vdev_mirror_ops
&&
4848 pvd
->vdev_ops
!= &vdev_spare_ops
)
4849 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4852 * If this device has the only valid copy of some data,
4853 * we cannot safely detach it.
4855 if (vdev_dtl_required(vd
))
4856 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
4858 ASSERT(pvd
->vdev_children
>= 2);
4861 * If we are detaching the second disk from a replacing vdev, then
4862 * check to see if we changed the original vdev's path to have "/old"
4863 * at the end in spa_vdev_attach(). If so, undo that change now.
4865 if (pvd
->vdev_ops
== &vdev_replacing_ops
&& vd
->vdev_id
> 0 &&
4866 vd
->vdev_path
!= NULL
) {
4867 size_t len
= strlen(vd
->vdev_path
);
4869 for (int c
= 0; c
< pvd
->vdev_children
; c
++) {
4870 cvd
= pvd
->vdev_child
[c
];
4872 if (cvd
== vd
|| cvd
->vdev_path
== NULL
)
4875 if (strncmp(cvd
->vdev_path
, vd
->vdev_path
, len
) == 0 &&
4876 strcmp(cvd
->vdev_path
+ len
, "/old") == 0) {
4877 spa_strfree(cvd
->vdev_path
);
4878 cvd
->vdev_path
= spa_strdup(vd
->vdev_path
);
4885 * If we are detaching the original disk from a spare, then it implies
4886 * that the spare should become a real disk, and be removed from the
4887 * active spare list for the pool.
4889 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
4891 pvd
->vdev_child
[pvd
->vdev_children
- 1]->vdev_isspare
)
4895 * Erase the disk labels so the disk can be used for other things.
4896 * This must be done after all other error cases are handled,
4897 * but before we disembowel vd (so we can still do I/O to it).
4898 * But if we can't do it, don't treat the error as fatal --
4899 * it may be that the unwritability of the disk is the reason
4900 * it's being detached!
4902 error
= vdev_label_init(vd
, 0, VDEV_LABEL_REMOVE
);
4905 * Remove vd from its parent and compact the parent's children.
4907 vdev_remove_child(pvd
, vd
);
4908 vdev_compact_children(pvd
);
4911 * Remember one of the remaining children so we can get tvd below.
4913 cvd
= pvd
->vdev_child
[pvd
->vdev_children
- 1];
4916 * If we need to remove the remaining child from the list of hot spares,
4917 * do it now, marking the vdev as no longer a spare in the process.
4918 * We must do this before vdev_remove_parent(), because that can
4919 * change the GUID if it creates a new toplevel GUID. For a similar
4920 * reason, we must remove the spare now, in the same txg as the detach;
4921 * otherwise someone could attach a new sibling, change the GUID, and
4922 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
4925 ASSERT(cvd
->vdev_isspare
);
4926 spa_spare_remove(cvd
);
4927 unspare_guid
= cvd
->vdev_guid
;
4928 (void) spa_vdev_remove(spa
, unspare_guid
, B_TRUE
);
4929 cvd
->vdev_unspare
= B_TRUE
;
4933 * If the parent mirror/replacing vdev only has one child,
4934 * the parent is no longer needed. Remove it from the tree.
4936 if (pvd
->vdev_children
== 1) {
4937 if (pvd
->vdev_ops
== &vdev_spare_ops
)
4938 cvd
->vdev_unspare
= B_FALSE
;
4939 vdev_remove_parent(cvd
);
4944 * We don't set tvd until now because the parent we just removed
4945 * may have been the previous top-level vdev.
4947 tvd
= cvd
->vdev_top
;
4948 ASSERT(tvd
->vdev_parent
== rvd
);
4951 * Reevaluate the parent vdev state.
4953 vdev_propagate_state(cvd
);
4956 * If the 'autoexpand' property is set on the pool then automatically
4957 * try to expand the size of the pool. For example if the device we
4958 * just detached was smaller than the others, it may be possible to
4959 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
4960 * first so that we can obtain the updated sizes of the leaf vdevs.
4962 if (spa
->spa_autoexpand
) {
4964 vdev_expand(tvd
, txg
);
4967 vdev_config_dirty(tvd
);
4970 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
4971 * vd->vdev_detached is set and free vd's DTL object in syncing context.
4972 * But first make sure we're not on any *other* txg's DTL list, to
4973 * prevent vd from being accessed after it's freed.
4975 vdpath
= spa_strdup(vd
->vdev_path
);
4976 for (int t
= 0; t
< TXG_SIZE
; t
++)
4977 (void) txg_list_remove_this(&tvd
->vdev_dtl_list
, vd
, t
);
4978 vd
->vdev_detached
= B_TRUE
;
4979 vdev_dirty(tvd
, VDD_DTL
, vd
, txg
);
4981 spa_event_notify(spa
, vd
, ESC_ZFS_VDEV_REMOVE
);
4983 /* hang on to the spa before we release the lock */
4984 spa_open_ref(spa
, FTAG
);
4986 error
= spa_vdev_exit(spa
, vd
, txg
, 0);
4988 spa_history_log_internal(spa
, "detach", NULL
,
4990 spa_strfree(vdpath
);
4993 * If this was the removal of the original device in a hot spare vdev,
4994 * then we want to go through and remove the device from the hot spare
4995 * list of every other pool.
4998 spa_t
*altspa
= NULL
;
5000 mutex_enter(&spa_namespace_lock
);
5001 while ((altspa
= spa_next(altspa
)) != NULL
) {
5002 if (altspa
->spa_state
!= POOL_STATE_ACTIVE
||
5006 spa_open_ref(altspa
, FTAG
);
5007 mutex_exit(&spa_namespace_lock
);
5008 (void) spa_vdev_remove(altspa
, unspare_guid
, B_TRUE
);
5009 mutex_enter(&spa_namespace_lock
);
5010 spa_close(altspa
, FTAG
);
5012 mutex_exit(&spa_namespace_lock
);
5014 /* search the rest of the vdevs for spares to remove */
5015 spa_vdev_resilver_done(spa
);
5018 /* all done with the spa; OK to release */
5019 mutex_enter(&spa_namespace_lock
);
5020 spa_close(spa
, FTAG
);
5021 mutex_exit(&spa_namespace_lock
);
5027 * Split a set of devices from their mirrors, and create a new pool from them.
5030 spa_vdev_split_mirror(spa_t
*spa
, char *newname
, nvlist_t
*config
,
5031 nvlist_t
*props
, boolean_t exp
)
5034 uint64_t txg
, *glist
;
5036 uint_t c
, children
, lastlog
;
5037 nvlist_t
**child
, *nvl
, *tmp
;
5039 char *altroot
= NULL
;
5040 vdev_t
*rvd
, **vml
= NULL
; /* vdev modify list */
5041 boolean_t activate_slog
;
5043 ASSERT(spa_writeable(spa
));
5045 txg
= spa_vdev_enter(spa
);
5047 /* clear the log and flush everything up to now */
5048 activate_slog
= spa_passivate_log(spa
);
5049 (void) spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
5050 error
= spa_offline_log(spa
);
5051 txg
= spa_vdev_config_enter(spa
);
5054 spa_activate_log(spa
);
5057 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5059 /* check new spa name before going any further */
5060 if (spa_lookup(newname
) != NULL
)
5061 return (spa_vdev_exit(spa
, NULL
, txg
, EEXIST
));
5064 * scan through all the children to ensure they're all mirrors
5066 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvl
) != 0 ||
5067 nvlist_lookup_nvlist_array(nvl
, ZPOOL_CONFIG_CHILDREN
, &child
,
5069 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
5071 /* first, check to ensure we've got the right child count */
5072 rvd
= spa
->spa_root_vdev
;
5074 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
5075 vdev_t
*vd
= rvd
->vdev_child
[c
];
5077 /* don't count the holes & logs as children */
5078 if (vd
->vdev_islog
|| vd
->vdev_ishole
) {
5086 if (children
!= (lastlog
!= 0 ? lastlog
: rvd
->vdev_children
))
5087 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
5089 /* next, ensure no spare or cache devices are part of the split */
5090 if (nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_SPARES
, &tmp
) == 0 ||
5091 nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_L2CACHE
, &tmp
) == 0)
5092 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
5094 vml
= kmem_zalloc(children
* sizeof (vdev_t
*), KM_SLEEP
);
5095 glist
= kmem_zalloc(children
* sizeof (uint64_t), KM_SLEEP
);
5097 /* then, loop over each vdev and validate it */
5098 for (c
= 0; c
< children
; c
++) {
5099 uint64_t is_hole
= 0;
5101 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
5105 if (spa
->spa_root_vdev
->vdev_child
[c
]->vdev_ishole
||
5106 spa
->spa_root_vdev
->vdev_child
[c
]->vdev_islog
) {
5109 error
= SET_ERROR(EINVAL
);
5114 /* which disk is going to be split? */
5115 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_GUID
,
5117 error
= SET_ERROR(EINVAL
);
5121 /* look it up in the spa */
5122 vml
[c
] = spa_lookup_by_guid(spa
, glist
[c
], B_FALSE
);
5123 if (vml
[c
] == NULL
) {
5124 error
= SET_ERROR(ENODEV
);
5128 /* make sure there's nothing stopping the split */
5129 if (vml
[c
]->vdev_parent
->vdev_ops
!= &vdev_mirror_ops
||
5130 vml
[c
]->vdev_islog
||
5131 vml
[c
]->vdev_ishole
||
5132 vml
[c
]->vdev_isspare
||
5133 vml
[c
]->vdev_isl2cache
||
5134 !vdev_writeable(vml
[c
]) ||
5135 vml
[c
]->vdev_children
!= 0 ||
5136 vml
[c
]->vdev_state
!= VDEV_STATE_HEALTHY
||
5137 c
!= spa
->spa_root_vdev
->vdev_child
[c
]->vdev_id
) {
5138 error
= SET_ERROR(EINVAL
);
5142 if (vdev_dtl_required(vml
[c
])) {
5143 error
= SET_ERROR(EBUSY
);
5147 /* we need certain info from the top level */
5148 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_ARRAY
,
5149 vml
[c
]->vdev_top
->vdev_ms_array
) == 0);
5150 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_SHIFT
,
5151 vml
[c
]->vdev_top
->vdev_ms_shift
) == 0);
5152 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASIZE
,
5153 vml
[c
]->vdev_top
->vdev_asize
) == 0);
5154 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASHIFT
,
5155 vml
[c
]->vdev_top
->vdev_ashift
) == 0);
5157 /* transfer per-vdev ZAPs */
5158 ASSERT3U(vml
[c
]->vdev_leaf_zap
, !=, 0);
5159 VERIFY0(nvlist_add_uint64(child
[c
],
5160 ZPOOL_CONFIG_VDEV_LEAF_ZAP
, vml
[c
]->vdev_leaf_zap
));
5162 ASSERT3U(vml
[c
]->vdev_top
->vdev_top_zap
, !=, 0);
5163 VERIFY0(nvlist_add_uint64(child
[c
],
5164 ZPOOL_CONFIG_VDEV_TOP_ZAP
,
5165 vml
[c
]->vdev_parent
->vdev_top_zap
));
5169 kmem_free(vml
, children
* sizeof (vdev_t
*));
5170 kmem_free(glist
, children
* sizeof (uint64_t));
5171 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5174 /* stop writers from using the disks */
5175 for (c
= 0; c
< children
; c
++) {
5177 vml
[c
]->vdev_offline
= B_TRUE
;
5179 vdev_reopen(spa
->spa_root_vdev
);
5182 * Temporarily record the splitting vdevs in the spa config. This
5183 * will disappear once the config is regenerated.
5185 VERIFY(nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
5186 VERIFY(nvlist_add_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
5187 glist
, children
) == 0);
5188 kmem_free(glist
, children
* sizeof (uint64_t));
5190 mutex_enter(&spa
->spa_props_lock
);
5191 VERIFY(nvlist_add_nvlist(spa
->spa_config
, ZPOOL_CONFIG_SPLIT
,
5193 mutex_exit(&spa
->spa_props_lock
);
5194 spa
->spa_config_splitting
= nvl
;
5195 vdev_config_dirty(spa
->spa_root_vdev
);
5197 /* configure and create the new pool */
5198 VERIFY(nvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
, newname
) == 0);
5199 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
5200 exp
? POOL_STATE_EXPORTED
: POOL_STATE_ACTIVE
) == 0);
5201 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
5202 spa_version(spa
)) == 0);
5203 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
5204 spa
->spa_config_txg
) == 0);
5205 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
5206 spa_generate_guid(NULL
)) == 0);
5207 VERIFY0(nvlist_add_boolean(config
, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
));
5208 (void) nvlist_lookup_string(props
,
5209 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
5211 /* add the new pool to the namespace */
5212 newspa
= spa_add(newname
, config
, altroot
);
5213 newspa
->spa_avz_action
= AVZ_ACTION_REBUILD
;
5214 newspa
->spa_config_txg
= spa
->spa_config_txg
;
5215 spa_set_log_state(newspa
, SPA_LOG_CLEAR
);
5217 /* release the spa config lock, retaining the namespace lock */
5218 spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
5220 if (zio_injection_enabled
)
5221 zio_handle_panic_injection(spa
, FTAG
, 1);
5223 spa_activate(newspa
, spa_mode_global
);
5224 spa_async_suspend(newspa
);
5226 /* create the new pool from the disks of the original pool */
5227 error
= spa_load(newspa
, SPA_LOAD_IMPORT
, SPA_IMPORT_ASSEMBLE
, B_TRUE
);
5231 /* if that worked, generate a real config for the new pool */
5232 if (newspa
->spa_root_vdev
!= NULL
) {
5233 VERIFY(nvlist_alloc(&newspa
->spa_config_splitting
,
5234 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
5235 VERIFY(nvlist_add_uint64(newspa
->spa_config_splitting
,
5236 ZPOOL_CONFIG_SPLIT_GUID
, spa_guid(spa
)) == 0);
5237 spa_config_set(newspa
, spa_config_generate(newspa
, NULL
, -1ULL,
5242 if (props
!= NULL
) {
5243 spa_configfile_set(newspa
, props
, B_FALSE
);
5244 error
= spa_prop_set(newspa
, props
);
5249 /* flush everything */
5250 txg
= spa_vdev_config_enter(newspa
);
5251 vdev_config_dirty(newspa
->spa_root_vdev
);
5252 (void) spa_vdev_config_exit(newspa
, NULL
, txg
, 0, FTAG
);
5254 if (zio_injection_enabled
)
5255 zio_handle_panic_injection(spa
, FTAG
, 2);
5257 spa_async_resume(newspa
);
5259 /* finally, update the original pool's config */
5260 txg
= spa_vdev_config_enter(spa
);
5261 tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
5262 error
= dmu_tx_assign(tx
, TXG_WAIT
);
5265 for (c
= 0; c
< children
; c
++) {
5266 if (vml
[c
] != NULL
) {
5269 spa_history_log_internal(spa
, "detach", tx
,
5270 "vdev=%s", vml
[c
]->vdev_path
);
5275 spa
->spa_avz_action
= AVZ_ACTION_REBUILD
;
5276 vdev_config_dirty(spa
->spa_root_vdev
);
5277 spa
->spa_config_splitting
= NULL
;
5281 (void) spa_vdev_exit(spa
, NULL
, txg
, 0);
5283 if (zio_injection_enabled
)
5284 zio_handle_panic_injection(spa
, FTAG
, 3);
5286 /* split is complete; log a history record */
5287 spa_history_log_internal(newspa
, "split", NULL
,
5288 "from pool %s", spa_name(spa
));
5290 kmem_free(vml
, children
* sizeof (vdev_t
*));
5292 /* if we're not going to mount the filesystems in userland, export */
5294 error
= spa_export_common(newname
, POOL_STATE_EXPORTED
, NULL
,
5301 spa_deactivate(newspa
);
5304 txg
= spa_vdev_config_enter(spa
);
5306 /* re-online all offlined disks */
5307 for (c
= 0; c
< children
; c
++) {
5309 vml
[c
]->vdev_offline
= B_FALSE
;
5311 vdev_reopen(spa
->spa_root_vdev
);
5313 nvlist_free(spa
->spa_config_splitting
);
5314 spa
->spa_config_splitting
= NULL
;
5315 (void) spa_vdev_exit(spa
, NULL
, txg
, error
);
5317 kmem_free(vml
, children
* sizeof (vdev_t
*));
5322 spa_nvlist_lookup_by_guid(nvlist_t
**nvpp
, int count
, uint64_t target_guid
)
5324 for (int i
= 0; i
< count
; i
++) {
5327 VERIFY(nvlist_lookup_uint64(nvpp
[i
], ZPOOL_CONFIG_GUID
,
5330 if (guid
== target_guid
)
5338 spa_vdev_remove_aux(nvlist_t
*config
, char *name
, nvlist_t
**dev
, int count
,
5339 nvlist_t
*dev_to_remove
)
5341 nvlist_t
**newdev
= NULL
;
5344 newdev
= kmem_alloc((count
- 1) * sizeof (void *), KM_SLEEP
);
5346 for (int i
= 0, j
= 0; i
< count
; i
++) {
5347 if (dev
[i
] == dev_to_remove
)
5349 VERIFY(nvlist_dup(dev
[i
], &newdev
[j
++], KM_SLEEP
) == 0);
5352 VERIFY(nvlist_remove(config
, name
, DATA_TYPE_NVLIST_ARRAY
) == 0);
5353 VERIFY(nvlist_add_nvlist_array(config
, name
, newdev
, count
- 1) == 0);
5355 for (int i
= 0; i
< count
- 1; i
++)
5356 nvlist_free(newdev
[i
]);
5359 kmem_free(newdev
, (count
- 1) * sizeof (void *));
5363 * Evacuate the device.
5366 spa_vdev_remove_evacuate(spa_t
*spa
, vdev_t
*vd
)
5371 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
5372 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
5373 ASSERT(vd
== vd
->vdev_top
);
5376 * Evacuate the device. We don't hold the config lock as writer
5377 * since we need to do I/O but we do keep the
5378 * spa_namespace_lock held. Once this completes the device
5379 * should no longer have any blocks allocated on it.
5381 if (vd
->vdev_islog
) {
5382 if (vd
->vdev_stat
.vs_alloc
!= 0)
5383 error
= spa_offline_log(spa
);
5385 error
= SET_ERROR(ENOTSUP
);
5392 * The evacuation succeeded. Remove any remaining MOS metadata
5393 * associated with this vdev, and wait for these changes to sync.
5395 ASSERT0(vd
->vdev_stat
.vs_alloc
);
5396 txg
= spa_vdev_config_enter(spa
);
5397 vd
->vdev_removing
= B_TRUE
;
5398 vdev_dirty_leaves(vd
, VDD_DTL
, txg
);
5399 vdev_config_dirty(vd
);
5400 spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
5406 * Complete the removal by cleaning up the namespace.
5409 spa_vdev_remove_from_namespace(spa_t
*spa
, vdev_t
*vd
)
5411 vdev_t
*rvd
= spa
->spa_root_vdev
;
5412 uint64_t id
= vd
->vdev_id
;
5413 boolean_t last_vdev
= (id
== (rvd
->vdev_children
- 1));
5415 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
5416 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
5417 ASSERT(vd
== vd
->vdev_top
);
5420 * Only remove any devices which are empty.
5422 if (vd
->vdev_stat
.vs_alloc
!= 0)
5425 (void) vdev_label_init(vd
, 0, VDEV_LABEL_REMOVE
);
5427 if (list_link_active(&vd
->vdev_state_dirty_node
))
5428 vdev_state_clean(vd
);
5429 if (list_link_active(&vd
->vdev_config_dirty_node
))
5430 vdev_config_clean(vd
);
5435 vdev_compact_children(rvd
);
5437 vd
= vdev_alloc_common(spa
, id
, 0, &vdev_hole_ops
);
5438 vdev_add_child(rvd
, vd
);
5440 vdev_config_dirty(rvd
);
5443 * Reassess the health of our root vdev.
5449 * Remove a device from the pool -
5451 * Removing a device from the vdev namespace requires several steps
5452 * and can take a significant amount of time. As a result we use
5453 * the spa_vdev_config_[enter/exit] functions which allow us to
5454 * grab and release the spa_config_lock while still holding the namespace
5455 * lock. During each step the configuration is synced out.
5457 * Currently, this supports removing only hot spares, slogs, and level 2 ARC
5461 spa_vdev_remove(spa_t
*spa
, uint64_t guid
, boolean_t unspare
)
5464 sysevent_t
*ev
= NULL
;
5465 metaslab_group_t
*mg
;
5466 nvlist_t
**spares
, **l2cache
, *nv
;
5468 uint_t nspares
, nl2cache
;
5470 boolean_t locked
= MUTEX_HELD(&spa_namespace_lock
);
5472 ASSERT(spa_writeable(spa
));
5475 txg
= spa_vdev_enter(spa
);
5477 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
5479 if (spa
->spa_spares
.sav_vdevs
!= NULL
&&
5480 nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
5481 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0 &&
5482 (nv
= spa_nvlist_lookup_by_guid(spares
, nspares
, guid
)) != NULL
) {
5484 * Only remove the hot spare if it's not currently in use
5487 if (vd
== NULL
|| unspare
) {
5489 vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
);
5490 ev
= spa_event_create(spa
, vd
, ESC_ZFS_VDEV_REMOVE_AUX
);
5491 spa_vdev_remove_aux(spa
->spa_spares
.sav_config
,
5492 ZPOOL_CONFIG_SPARES
, spares
, nspares
, nv
);
5493 spa_load_spares(spa
);
5494 spa
->spa_spares
.sav_sync
= B_TRUE
;
5496 error
= SET_ERROR(EBUSY
);
5498 } else if (spa
->spa_l2cache
.sav_vdevs
!= NULL
&&
5499 nvlist_lookup_nvlist_array(spa
->spa_l2cache
.sav_config
,
5500 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0 &&
5501 (nv
= spa_nvlist_lookup_by_guid(l2cache
, nl2cache
, guid
)) != NULL
) {
5503 * Cache devices can always be removed.
5505 vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
);
5506 ev
= spa_event_create(spa
, vd
, ESC_ZFS_VDEV_REMOVE_AUX
);
5507 spa_vdev_remove_aux(spa
->spa_l2cache
.sav_config
,
5508 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
, nv
);
5509 spa_load_l2cache(spa
);
5510 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
5511 } else if (vd
!= NULL
&& vd
->vdev_islog
) {
5513 ASSERT(vd
== vd
->vdev_top
);
5518 * Stop allocating from this vdev.
5520 metaslab_group_passivate(mg
);
5523 * Wait for the youngest allocations and frees to sync,
5524 * and then wait for the deferral of those frees to finish.
5526 spa_vdev_config_exit(spa
, NULL
,
5527 txg
+ TXG_CONCURRENT_STATES
+ TXG_DEFER_SIZE
, 0, FTAG
);
5530 * Attempt to evacuate the vdev.
5532 error
= spa_vdev_remove_evacuate(spa
, vd
);
5534 txg
= spa_vdev_config_enter(spa
);
5537 * If we couldn't evacuate the vdev, unwind.
5540 metaslab_group_activate(mg
);
5541 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5545 * Clean up the vdev namespace.
5547 ev
= spa_event_create(spa
, vd
, ESC_ZFS_VDEV_REMOVE_DEV
);
5548 spa_vdev_remove_from_namespace(spa
, vd
);
5550 } else if (vd
!= NULL
) {
5552 * Normal vdevs cannot be removed (yet).
5554 error
= SET_ERROR(ENOTSUP
);
5557 * There is no vdev of any kind with the specified guid.
5559 error
= SET_ERROR(ENOENT
);
5563 error
= spa_vdev_exit(spa
, NULL
, txg
, error
);
5572 * Find any device that's done replacing, or a vdev marked 'unspare' that's
5573 * currently spared, so we can detach it.
5576 spa_vdev_resilver_done_hunt(vdev_t
*vd
)
5578 vdev_t
*newvd
, *oldvd
;
5580 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
5581 oldvd
= spa_vdev_resilver_done_hunt(vd
->vdev_child
[c
]);
5587 * Check for a completed replacement. We always consider the first
5588 * vdev in the list to be the oldest vdev, and the last one to be
5589 * the newest (see spa_vdev_attach() for how that works). In
5590 * the case where the newest vdev is faulted, we will not automatically
5591 * remove it after a resilver completes. This is OK as it will require
5592 * user intervention to determine which disk the admin wishes to keep.
5594 if (vd
->vdev_ops
== &vdev_replacing_ops
) {
5595 ASSERT(vd
->vdev_children
> 1);
5597 newvd
= vd
->vdev_child
[vd
->vdev_children
- 1];
5598 oldvd
= vd
->vdev_child
[0];
5600 if (vdev_dtl_empty(newvd
, DTL_MISSING
) &&
5601 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
5602 !vdev_dtl_required(oldvd
))
5607 * Check for a completed resilver with the 'unspare' flag set.
5609 if (vd
->vdev_ops
== &vdev_spare_ops
) {
5610 vdev_t
*first
= vd
->vdev_child
[0];
5611 vdev_t
*last
= vd
->vdev_child
[vd
->vdev_children
- 1];
5613 if (last
->vdev_unspare
) {
5616 } else if (first
->vdev_unspare
) {
5623 if (oldvd
!= NULL
&&
5624 vdev_dtl_empty(newvd
, DTL_MISSING
) &&
5625 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
5626 !vdev_dtl_required(oldvd
))
5630 * If there are more than two spares attached to a disk,
5631 * and those spares are not required, then we want to
5632 * attempt to free them up now so that they can be used
5633 * by other pools. Once we're back down to a single
5634 * disk+spare, we stop removing them.
5636 if (vd
->vdev_children
> 2) {
5637 newvd
= vd
->vdev_child
[1];
5639 if (newvd
->vdev_isspare
&& last
->vdev_isspare
&&
5640 vdev_dtl_empty(last
, DTL_MISSING
) &&
5641 vdev_dtl_empty(last
, DTL_OUTAGE
) &&
5642 !vdev_dtl_required(newvd
))
5651 spa_vdev_resilver_done(spa_t
*spa
)
5653 vdev_t
*vd
, *pvd
, *ppvd
;
5654 uint64_t guid
, sguid
, pguid
, ppguid
;
5656 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5658 while ((vd
= spa_vdev_resilver_done_hunt(spa
->spa_root_vdev
)) != NULL
) {
5659 pvd
= vd
->vdev_parent
;
5660 ppvd
= pvd
->vdev_parent
;
5661 guid
= vd
->vdev_guid
;
5662 pguid
= pvd
->vdev_guid
;
5663 ppguid
= ppvd
->vdev_guid
;
5666 * If we have just finished replacing a hot spared device, then
5667 * we need to detach the parent's first child (the original hot
5670 if (ppvd
->vdev_ops
== &vdev_spare_ops
&& pvd
->vdev_id
== 0 &&
5671 ppvd
->vdev_children
== 2) {
5672 ASSERT(pvd
->vdev_ops
== &vdev_replacing_ops
);
5673 sguid
= ppvd
->vdev_child
[1]->vdev_guid
;
5675 ASSERT(vd
->vdev_resilver_txg
== 0 || !vdev_dtl_required(vd
));
5677 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5678 if (spa_vdev_detach(spa
, guid
, pguid
, B_TRUE
) != 0)
5680 if (sguid
&& spa_vdev_detach(spa
, sguid
, ppguid
, B_TRUE
) != 0)
5682 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5685 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5689 * Update the stored path or FRU for this vdev.
5692 spa_vdev_set_common(spa_t
*spa
, uint64_t guid
, const char *value
,
5696 boolean_t sync
= B_FALSE
;
5698 ASSERT(spa_writeable(spa
));
5700 spa_vdev_state_enter(spa
, SCL_ALL
);
5702 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
5703 return (spa_vdev_state_exit(spa
, NULL
, ENOENT
));
5705 if (!vd
->vdev_ops
->vdev_op_leaf
)
5706 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
5709 if (strcmp(value
, vd
->vdev_path
) != 0) {
5710 spa_strfree(vd
->vdev_path
);
5711 vd
->vdev_path
= spa_strdup(value
);
5715 if (vd
->vdev_fru
== NULL
) {
5716 vd
->vdev_fru
= spa_strdup(value
);
5718 } else if (strcmp(value
, vd
->vdev_fru
) != 0) {
5719 spa_strfree(vd
->vdev_fru
);
5720 vd
->vdev_fru
= spa_strdup(value
);
5725 return (spa_vdev_state_exit(spa
, sync
? vd
: NULL
, 0));
5729 spa_vdev_setpath(spa_t
*spa
, uint64_t guid
, const char *newpath
)
5731 return (spa_vdev_set_common(spa
, guid
, newpath
, B_TRUE
));
5735 spa_vdev_setfru(spa_t
*spa
, uint64_t guid
, const char *newfru
)
5737 return (spa_vdev_set_common(spa
, guid
, newfru
, B_FALSE
));
5741 * ==========================================================================
5743 * ==========================================================================
5747 spa_scan_stop(spa_t
*spa
)
5749 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
5750 if (dsl_scan_resilvering(spa
->spa_dsl_pool
))
5751 return (SET_ERROR(EBUSY
));
5752 return (dsl_scan_cancel(spa
->spa_dsl_pool
));
5756 spa_scan(spa_t
*spa
, pool_scan_func_t func
)
5758 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
5760 if (func
>= POOL_SCAN_FUNCS
|| func
== POOL_SCAN_NONE
)
5761 return (SET_ERROR(ENOTSUP
));
5764 * If a resilver was requested, but there is no DTL on a
5765 * writeable leaf device, we have nothing to do.
5767 if (func
== POOL_SCAN_RESILVER
&&
5768 !vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
)) {
5769 spa_async_request(spa
, SPA_ASYNC_RESILVER_DONE
);
5773 return (dsl_scan(spa
->spa_dsl_pool
, func
));
5777 * ==========================================================================
5778 * SPA async task processing
5779 * ==========================================================================
5783 spa_async_remove(spa_t
*spa
, vdev_t
*vd
)
5785 if (vd
->vdev_remove_wanted
) {
5786 vd
->vdev_remove_wanted
= B_FALSE
;
5787 vd
->vdev_delayed_close
= B_FALSE
;
5788 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_REMOVED
, VDEV_AUX_NONE
);
5791 * We want to clear the stats, but we don't want to do a full
5792 * vdev_clear() as that will cause us to throw away
5793 * degraded/faulted state as well as attempt to reopen the
5794 * device, all of which is a waste.
5796 vd
->vdev_stat
.vs_read_errors
= 0;
5797 vd
->vdev_stat
.vs_write_errors
= 0;
5798 vd
->vdev_stat
.vs_checksum_errors
= 0;
5800 vdev_state_dirty(vd
->vdev_top
);
5803 for (int c
= 0; c
< vd
->vdev_children
; c
++)
5804 spa_async_remove(spa
, vd
->vdev_child
[c
]);
5808 spa_async_probe(spa_t
*spa
, vdev_t
*vd
)
5810 if (vd
->vdev_probe_wanted
) {
5811 vd
->vdev_probe_wanted
= B_FALSE
;
5812 vdev_reopen(vd
); /* vdev_open() does the actual probe */
5815 for (int c
= 0; c
< vd
->vdev_children
; c
++)
5816 spa_async_probe(spa
, vd
->vdev_child
[c
]);
5820 spa_async_autoexpand(spa_t
*spa
, vdev_t
*vd
)
5826 if (!spa
->spa_autoexpand
)
5829 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
5830 vdev_t
*cvd
= vd
->vdev_child
[c
];
5831 spa_async_autoexpand(spa
, cvd
);
5834 if (!vd
->vdev_ops
->vdev_op_leaf
|| vd
->vdev_physpath
== NULL
)
5837 physpath
= kmem_zalloc(MAXPATHLEN
, KM_SLEEP
);
5838 (void) snprintf(physpath
, MAXPATHLEN
, "/devices%s", vd
->vdev_physpath
);
5840 VERIFY(nvlist_alloc(&attr
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
5841 VERIFY(nvlist_add_string(attr
, DEV_PHYS_PATH
, physpath
) == 0);
5843 (void) ddi_log_sysevent(zfs_dip
, SUNW_VENDOR
, EC_DEV_STATUS
,
5844 ESC_DEV_DLE
, attr
, &eid
, DDI_SLEEP
);
5847 kmem_free(physpath
, MAXPATHLEN
);
5851 spa_async_thread(spa_t
*spa
)
5855 ASSERT(spa
->spa_sync_on
);
5857 mutex_enter(&spa
->spa_async_lock
);
5858 tasks
= spa
->spa_async_tasks
;
5859 spa
->spa_async_tasks
= 0;
5860 mutex_exit(&spa
->spa_async_lock
);
5863 * See if the config needs to be updated.
5865 if (tasks
& SPA_ASYNC_CONFIG_UPDATE
) {
5866 uint64_t old_space
, new_space
;
5868 mutex_enter(&spa_namespace_lock
);
5869 old_space
= metaslab_class_get_space(spa_normal_class(spa
));
5870 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
5871 new_space
= metaslab_class_get_space(spa_normal_class(spa
));
5872 mutex_exit(&spa_namespace_lock
);
5875 * If the pool grew as a result of the config update,
5876 * then log an internal history event.
5878 if (new_space
!= old_space
) {
5879 spa_history_log_internal(spa
, "vdev online", NULL
,
5880 "pool '%s' size: %llu(+%llu)",
5881 spa_name(spa
), new_space
, new_space
- old_space
);
5886 * See if any devices need to be marked REMOVED.
5888 if (tasks
& SPA_ASYNC_REMOVE
) {
5889 spa_vdev_state_enter(spa
, SCL_NONE
);
5890 spa_async_remove(spa
, spa
->spa_root_vdev
);
5891 for (int i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++)
5892 spa_async_remove(spa
, spa
->spa_l2cache
.sav_vdevs
[i
]);
5893 for (int i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
5894 spa_async_remove(spa
, spa
->spa_spares
.sav_vdevs
[i
]);
5895 (void) spa_vdev_state_exit(spa
, NULL
, 0);
5898 if ((tasks
& SPA_ASYNC_AUTOEXPAND
) && !spa_suspended(spa
)) {
5899 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
5900 spa_async_autoexpand(spa
, spa
->spa_root_vdev
);
5901 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
5905 * See if any devices need to be probed.
5907 if (tasks
& SPA_ASYNC_PROBE
) {
5908 spa_vdev_state_enter(spa
, SCL_NONE
);
5909 spa_async_probe(spa
, spa
->spa_root_vdev
);
5910 (void) spa_vdev_state_exit(spa
, NULL
, 0);
5914 * If any devices are done replacing, detach them.
5916 if (tasks
& SPA_ASYNC_RESILVER_DONE
)
5917 spa_vdev_resilver_done(spa
);
5920 * Kick off a resilver.
5922 if (tasks
& SPA_ASYNC_RESILVER
)
5923 dsl_resilver_restart(spa
->spa_dsl_pool
, 0);
5926 * Let the world know that we're done.
5928 mutex_enter(&spa
->spa_async_lock
);
5929 spa
->spa_async_thread
= NULL
;
5930 cv_broadcast(&spa
->spa_async_cv
);
5931 mutex_exit(&spa
->spa_async_lock
);
5936 spa_async_suspend(spa_t
*spa
)
5938 mutex_enter(&spa
->spa_async_lock
);
5939 spa
->spa_async_suspended
++;
5940 while (spa
->spa_async_thread
!= NULL
)
5941 cv_wait(&spa
->spa_async_cv
, &spa
->spa_async_lock
);
5942 mutex_exit(&spa
->spa_async_lock
);
5946 spa_async_resume(spa_t
*spa
)
5948 mutex_enter(&spa
->spa_async_lock
);
5949 ASSERT(spa
->spa_async_suspended
!= 0);
5950 spa
->spa_async_suspended
--;
5951 mutex_exit(&spa
->spa_async_lock
);
5955 spa_async_tasks_pending(spa_t
*spa
)
5957 uint_t non_config_tasks
;
5959 boolean_t config_task_suspended
;
5961 non_config_tasks
= spa
->spa_async_tasks
& ~SPA_ASYNC_CONFIG_UPDATE
;
5962 config_task
= spa
->spa_async_tasks
& SPA_ASYNC_CONFIG_UPDATE
;
5963 if (spa
->spa_ccw_fail_time
== 0) {
5964 config_task_suspended
= B_FALSE
;
5966 config_task_suspended
=
5967 (gethrtime() - spa
->spa_ccw_fail_time
) <
5968 (zfs_ccw_retry_interval
* NANOSEC
);
5971 return (non_config_tasks
|| (config_task
&& !config_task_suspended
));
5975 spa_async_dispatch(spa_t
*spa
)
5977 mutex_enter(&spa
->spa_async_lock
);
5978 if (spa_async_tasks_pending(spa
) &&
5979 !spa
->spa_async_suspended
&&
5980 spa
->spa_async_thread
== NULL
&&
5982 spa
->spa_async_thread
= thread_create(NULL
, 0,
5983 spa_async_thread
, spa
, 0, &p0
, TS_RUN
, maxclsyspri
);
5984 mutex_exit(&spa
->spa_async_lock
);
5988 spa_async_request(spa_t
*spa
, int task
)
5990 zfs_dbgmsg("spa=%s async request task=%u", spa
->spa_name
, task
);
5991 mutex_enter(&spa
->spa_async_lock
);
5992 spa
->spa_async_tasks
|= task
;
5993 mutex_exit(&spa
->spa_async_lock
);
5997 * ==========================================================================
5998 * SPA syncing routines
5999 * ==========================================================================
6003 bpobj_enqueue_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
6006 bpobj_enqueue(bpo
, bp
, tx
);
6011 spa_free_sync_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
6015 zio_nowait(zio_free_sync(zio
, zio
->io_spa
, dmu_tx_get_txg(tx
), bp
,
6021 * Note: this simple function is not inlined to make it easier to dtrace the
6022 * amount of time spent syncing frees.
6025 spa_sync_frees(spa_t
*spa
, bplist_t
*bpl
, dmu_tx_t
*tx
)
6027 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
6028 bplist_iterate(bpl
, spa_free_sync_cb
, zio
, tx
);
6029 VERIFY(zio_wait(zio
) == 0);
6033 * Note: this simple function is not inlined to make it easier to dtrace the
6034 * amount of time spent syncing deferred frees.
6037 spa_sync_deferred_frees(spa_t
*spa
, dmu_tx_t
*tx
)
6039 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
6040 VERIFY3U(bpobj_iterate(&spa
->spa_deferred_bpobj
,
6041 spa_free_sync_cb
, zio
, tx
), ==, 0);
6042 VERIFY0(zio_wait(zio
));
6047 spa_sync_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
*nv
, dmu_tx_t
*tx
)
6049 char *packed
= NULL
;
6054 VERIFY(nvlist_size(nv
, &nvsize
, NV_ENCODE_XDR
) == 0);
6057 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
6058 * information. This avoids the dmu_buf_will_dirty() path and
6059 * saves us a pre-read to get data we don't actually care about.
6061 bufsize
= P2ROUNDUP((uint64_t)nvsize
, SPA_CONFIG_BLOCKSIZE
);
6062 packed
= kmem_alloc(bufsize
, KM_SLEEP
);
6064 VERIFY(nvlist_pack(nv
, &packed
, &nvsize
, NV_ENCODE_XDR
,
6066 bzero(packed
+ nvsize
, bufsize
- nvsize
);
6068 dmu_write(spa
->spa_meta_objset
, obj
, 0, bufsize
, packed
, tx
);
6070 kmem_free(packed
, bufsize
);
6072 VERIFY(0 == dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
));
6073 dmu_buf_will_dirty(db
, tx
);
6074 *(uint64_t *)db
->db_data
= nvsize
;
6075 dmu_buf_rele(db
, FTAG
);
6079 spa_sync_aux_dev(spa_t
*spa
, spa_aux_vdev_t
*sav
, dmu_tx_t
*tx
,
6080 const char *config
, const char *entry
)
6090 * Update the MOS nvlist describing the list of available devices.
6091 * spa_validate_aux() will have already made sure this nvlist is
6092 * valid and the vdevs are labeled appropriately.
6094 if (sav
->sav_object
== 0) {
6095 sav
->sav_object
= dmu_object_alloc(spa
->spa_meta_objset
,
6096 DMU_OT_PACKED_NVLIST
, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE
,
6097 sizeof (uint64_t), tx
);
6098 VERIFY(zap_update(spa
->spa_meta_objset
,
6099 DMU_POOL_DIRECTORY_OBJECT
, entry
, sizeof (uint64_t), 1,
6100 &sav
->sav_object
, tx
) == 0);
6103 VERIFY(nvlist_alloc(&nvroot
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
6104 if (sav
->sav_count
== 0) {
6105 VERIFY(nvlist_add_nvlist_array(nvroot
, config
, NULL
, 0) == 0);
6107 list
= kmem_alloc(sav
->sav_count
* sizeof (void *), KM_SLEEP
);
6108 for (i
= 0; i
< sav
->sav_count
; i
++)
6109 list
[i
] = vdev_config_generate(spa
, sav
->sav_vdevs
[i
],
6110 B_FALSE
, VDEV_CONFIG_L2CACHE
);
6111 VERIFY(nvlist_add_nvlist_array(nvroot
, config
, list
,
6112 sav
->sav_count
) == 0);
6113 for (i
= 0; i
< sav
->sav_count
; i
++)
6114 nvlist_free(list
[i
]);
6115 kmem_free(list
, sav
->sav_count
* sizeof (void *));
6118 spa_sync_nvlist(spa
, sav
->sav_object
, nvroot
, tx
);
6119 nvlist_free(nvroot
);
6121 sav
->sav_sync
= B_FALSE
;
6125 * Rebuild spa's all-vdev ZAP from the vdev ZAPs indicated in each vdev_t.
6126 * The all-vdev ZAP must be empty.
6129 spa_avz_build(vdev_t
*vd
, uint64_t avz
, dmu_tx_t
*tx
)
6131 spa_t
*spa
= vd
->vdev_spa
;
6132 if (vd
->vdev_top_zap
!= 0) {
6133 VERIFY0(zap_add_int(spa
->spa_meta_objset
, avz
,
6134 vd
->vdev_top_zap
, tx
));
6136 if (vd
->vdev_leaf_zap
!= 0) {
6137 VERIFY0(zap_add_int(spa
->spa_meta_objset
, avz
,
6138 vd
->vdev_leaf_zap
, tx
));
6140 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
6141 spa_avz_build(vd
->vdev_child
[i
], avz
, tx
);
6146 spa_sync_config_object(spa_t
*spa
, dmu_tx_t
*tx
)
6151 * If the pool is being imported from a pre-per-vdev-ZAP version of ZFS,
6152 * its config may not be dirty but we still need to build per-vdev ZAPs.
6153 * Similarly, if the pool is being assembled (e.g. after a split), we
6154 * need to rebuild the AVZ although the config may not be dirty.
6156 if (list_is_empty(&spa
->spa_config_dirty_list
) &&
6157 spa
->spa_avz_action
== AVZ_ACTION_NONE
)
6160 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
6162 ASSERT(spa
->spa_avz_action
== AVZ_ACTION_NONE
||
6163 spa
->spa_avz_action
== AVZ_ACTION_INITIALIZE
||
6164 spa
->spa_all_vdev_zaps
!= 0);
6166 if (spa
->spa_avz_action
== AVZ_ACTION_REBUILD
) {
6167 /* Make and build the new AVZ */
6168 uint64_t new_avz
= zap_create(spa
->spa_meta_objset
,
6169 DMU_OTN_ZAP_METADATA
, DMU_OT_NONE
, 0, tx
);
6170 spa_avz_build(spa
->spa_root_vdev
, new_avz
, tx
);
6172 /* Diff old AVZ with new one */
6176 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
6177 spa
->spa_all_vdev_zaps
);
6178 zap_cursor_retrieve(&zc
, &za
) == 0;
6179 zap_cursor_advance(&zc
)) {
6180 uint64_t vdzap
= za
.za_first_integer
;
6181 if (zap_lookup_int(spa
->spa_meta_objset
, new_avz
,
6184 * ZAP is listed in old AVZ but not in new one;
6187 VERIFY0(zap_destroy(spa
->spa_meta_objset
, vdzap
,
6192 zap_cursor_fini(&zc
);
6194 /* Destroy the old AVZ */
6195 VERIFY0(zap_destroy(spa
->spa_meta_objset
,
6196 spa
->spa_all_vdev_zaps
, tx
));
6198 /* Replace the old AVZ in the dir obj with the new one */
6199 VERIFY0(zap_update(spa
->spa_meta_objset
,
6200 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_VDEV_ZAP_MAP
,
6201 sizeof (new_avz
), 1, &new_avz
, tx
));
6203 spa
->spa_all_vdev_zaps
= new_avz
;
6204 } else if (spa
->spa_avz_action
== AVZ_ACTION_DESTROY
) {
6208 /* Walk through the AVZ and destroy all listed ZAPs */
6209 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
6210 spa
->spa_all_vdev_zaps
);
6211 zap_cursor_retrieve(&zc
, &za
) == 0;
6212 zap_cursor_advance(&zc
)) {
6213 uint64_t zap
= za
.za_first_integer
;
6214 VERIFY0(zap_destroy(spa
->spa_meta_objset
, zap
, tx
));
6217 zap_cursor_fini(&zc
);
6219 /* Destroy and unlink the AVZ itself */
6220 VERIFY0(zap_destroy(spa
->spa_meta_objset
,
6221 spa
->spa_all_vdev_zaps
, tx
));
6222 VERIFY0(zap_remove(spa
->spa_meta_objset
,
6223 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_VDEV_ZAP_MAP
, tx
));
6224 spa
->spa_all_vdev_zaps
= 0;
6227 if (spa
->spa_all_vdev_zaps
== 0) {
6228 spa
->spa_all_vdev_zaps
= zap_create_link(spa
->spa_meta_objset
,
6229 DMU_OTN_ZAP_METADATA
, DMU_POOL_DIRECTORY_OBJECT
,
6230 DMU_POOL_VDEV_ZAP_MAP
, tx
);
6232 spa
->spa_avz_action
= AVZ_ACTION_NONE
;
6234 /* Create ZAPs for vdevs that don't have them. */
6235 vdev_construct_zaps(spa
->spa_root_vdev
, tx
);
6237 config
= spa_config_generate(spa
, spa
->spa_root_vdev
,
6238 dmu_tx_get_txg(tx
), B_FALSE
);
6241 * If we're upgrading the spa version then make sure that
6242 * the config object gets updated with the correct version.
6244 if (spa
->spa_ubsync
.ub_version
< spa
->spa_uberblock
.ub_version
)
6245 fnvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
6246 spa
->spa_uberblock
.ub_version
);
6248 spa_config_exit(spa
, SCL_STATE
, FTAG
);
6250 nvlist_free(spa
->spa_config_syncing
);
6251 spa
->spa_config_syncing
= config
;
6253 spa_sync_nvlist(spa
, spa
->spa_config_object
, config
, tx
);
6257 spa_sync_version(void *arg
, dmu_tx_t
*tx
)
6259 uint64_t *versionp
= arg
;
6260 uint64_t version
= *versionp
;
6261 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
6264 * Setting the version is special cased when first creating the pool.
6266 ASSERT(tx
->tx_txg
!= TXG_INITIAL
);
6268 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
6269 ASSERT(version
>= spa_version(spa
));
6271 spa
->spa_uberblock
.ub_version
= version
;
6272 vdev_config_dirty(spa
->spa_root_vdev
);
6273 spa_history_log_internal(spa
, "set", tx
, "version=%lld", version
);
6277 * Set zpool properties.
6280 spa_sync_props(void *arg
, dmu_tx_t
*tx
)
6282 nvlist_t
*nvp
= arg
;
6283 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
6284 objset_t
*mos
= spa
->spa_meta_objset
;
6285 nvpair_t
*elem
= NULL
;
6287 mutex_enter(&spa
->spa_props_lock
);
6289 while ((elem
= nvlist_next_nvpair(nvp
, elem
))) {
6291 char *strval
, *fname
;
6293 const char *propname
;
6294 zprop_type_t proptype
;
6297 switch (prop
= zpool_name_to_prop(nvpair_name(elem
))) {
6300 * We checked this earlier in spa_prop_validate().
6302 ASSERT(zpool_prop_feature(nvpair_name(elem
)));
6304 fname
= strchr(nvpair_name(elem
), '@') + 1;
6305 VERIFY0(zfeature_lookup_name(fname
, &fid
));
6307 spa_feature_enable(spa
, fid
, tx
);
6308 spa_history_log_internal(spa
, "set", tx
,
6309 "%s=enabled", nvpair_name(elem
));
6312 case ZPOOL_PROP_VERSION
:
6313 intval
= fnvpair_value_uint64(elem
);
6315 * The version is synced seperatly before other
6316 * properties and should be correct by now.
6318 ASSERT3U(spa_version(spa
), >=, intval
);
6321 case ZPOOL_PROP_ALTROOT
:
6323 * 'altroot' is a non-persistent property. It should
6324 * have been set temporarily at creation or import time.
6326 ASSERT(spa
->spa_root
!= NULL
);
6329 case ZPOOL_PROP_READONLY
:
6330 case ZPOOL_PROP_CACHEFILE
:
6332 * 'readonly' and 'cachefile' are also non-persisitent
6336 case ZPOOL_PROP_COMMENT
:
6337 strval
= fnvpair_value_string(elem
);
6338 if (spa
->spa_comment
!= NULL
)
6339 spa_strfree(spa
->spa_comment
);
6340 spa
->spa_comment
= spa_strdup(strval
);
6342 * We need to dirty the configuration on all the vdevs
6343 * so that their labels get updated. It's unnecessary
6344 * to do this for pool creation since the vdev's
6345 * configuratoin has already been dirtied.
6347 if (tx
->tx_txg
!= TXG_INITIAL
)
6348 vdev_config_dirty(spa
->spa_root_vdev
);
6349 spa_history_log_internal(spa
, "set", tx
,
6350 "%s=%s", nvpair_name(elem
), strval
);
6354 * Set pool property values in the poolprops mos object.
6356 if (spa
->spa_pool_props_object
== 0) {
6357 spa
->spa_pool_props_object
=
6358 zap_create_link(mos
, DMU_OT_POOL_PROPS
,
6359 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_PROPS
,
6363 /* normalize the property name */
6364 propname
= zpool_prop_to_name(prop
);
6365 proptype
= zpool_prop_get_type(prop
);
6367 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
6368 ASSERT(proptype
== PROP_TYPE_STRING
);
6369 strval
= fnvpair_value_string(elem
);
6370 VERIFY0(zap_update(mos
,
6371 spa
->spa_pool_props_object
, propname
,
6372 1, strlen(strval
) + 1, strval
, tx
));
6373 spa_history_log_internal(spa
, "set", tx
,
6374 "%s=%s", nvpair_name(elem
), strval
);
6375 } else if (nvpair_type(elem
) == DATA_TYPE_UINT64
) {
6376 intval
= fnvpair_value_uint64(elem
);
6378 if (proptype
== PROP_TYPE_INDEX
) {
6380 VERIFY0(zpool_prop_index_to_string(
6381 prop
, intval
, &unused
));
6383 VERIFY0(zap_update(mos
,
6384 spa
->spa_pool_props_object
, propname
,
6385 8, 1, &intval
, tx
));
6386 spa_history_log_internal(spa
, "set", tx
,
6387 "%s=%lld", nvpair_name(elem
), intval
);
6389 ASSERT(0); /* not allowed */
6393 case ZPOOL_PROP_DELEGATION
:
6394 spa
->spa_delegation
= intval
;
6396 case ZPOOL_PROP_BOOTFS
:
6397 spa
->spa_bootfs
= intval
;
6399 case ZPOOL_PROP_FAILUREMODE
:
6400 spa
->spa_failmode
= intval
;
6402 case ZPOOL_PROP_AUTOEXPAND
:
6403 spa
->spa_autoexpand
= intval
;
6404 if (tx
->tx_txg
!= TXG_INITIAL
)
6405 spa_async_request(spa
,
6406 SPA_ASYNC_AUTOEXPAND
);
6408 case ZPOOL_PROP_DEDUPDITTO
:
6409 spa
->spa_dedup_ditto
= intval
;
6418 mutex_exit(&spa
->spa_props_lock
);
6422 * Perform one-time upgrade on-disk changes. spa_version() does not
6423 * reflect the new version this txg, so there must be no changes this
6424 * txg to anything that the upgrade code depends on after it executes.
6425 * Therefore this must be called after dsl_pool_sync() does the sync
6429 spa_sync_upgrades(spa_t
*spa
, dmu_tx_t
*tx
)
6431 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
6433 ASSERT(spa
->spa_sync_pass
== 1);
6435 rrw_enter(&dp
->dp_config_rwlock
, RW_WRITER
, FTAG
);
6437 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_ORIGIN
&&
6438 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_ORIGIN
) {
6439 dsl_pool_create_origin(dp
, tx
);
6441 /* Keeping the origin open increases spa_minref */
6442 spa
->spa_minref
+= 3;
6445 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_NEXT_CLONES
&&
6446 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_NEXT_CLONES
) {
6447 dsl_pool_upgrade_clones(dp
, tx
);
6450 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_DIR_CLONES
&&
6451 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_DIR_CLONES
) {
6452 dsl_pool_upgrade_dir_clones(dp
, tx
);
6454 /* Keeping the freedir open increases spa_minref */
6455 spa
->spa_minref
+= 3;
6458 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_FEATURES
&&
6459 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
6460 spa_feature_create_zap_objects(spa
, tx
);
6464 * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable
6465 * when possibility to use lz4 compression for metadata was added
6466 * Old pools that have this feature enabled must be upgraded to have
6467 * this feature active
6469 if (spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
6470 boolean_t lz4_en
= spa_feature_is_enabled(spa
,
6471 SPA_FEATURE_LZ4_COMPRESS
);
6472 boolean_t lz4_ac
= spa_feature_is_active(spa
,
6473 SPA_FEATURE_LZ4_COMPRESS
);
6475 if (lz4_en
&& !lz4_ac
)
6476 spa_feature_incr(spa
, SPA_FEATURE_LZ4_COMPRESS
, tx
);
6480 * If we haven't written the salt, do so now. Note that the
6481 * feature may not be activated yet, but that's fine since
6482 * the presence of this ZAP entry is backwards compatible.
6484 if (zap_contains(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
6485 DMU_POOL_CHECKSUM_SALT
) == ENOENT
) {
6486 VERIFY0(zap_add(spa
->spa_meta_objset
,
6487 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CHECKSUM_SALT
, 1,
6488 sizeof (spa
->spa_cksum_salt
.zcs_bytes
),
6489 spa
->spa_cksum_salt
.zcs_bytes
, tx
));
6492 rrw_exit(&dp
->dp_config_rwlock
, FTAG
);
6496 * Sync the specified transaction group. New blocks may be dirtied as
6497 * part of the process, so we iterate until it converges.
6500 spa_sync(spa_t
*spa
, uint64_t txg
)
6502 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
6503 objset_t
*mos
= spa
->spa_meta_objset
;
6504 bplist_t
*free_bpl
= &spa
->spa_free_bplist
[txg
& TXG_MASK
];
6505 vdev_t
*rvd
= spa
->spa_root_vdev
;
6509 uint32_t max_queue_depth
= zfs_vdev_async_write_max_active
*
6510 zfs_vdev_queue_depth_pct
/ 100;
6512 VERIFY(spa_writeable(spa
));
6515 * Lock out configuration changes.
6517 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
6519 spa
->spa_syncing_txg
= txg
;
6520 spa
->spa_sync_pass
= 0;
6522 mutex_enter(&spa
->spa_alloc_lock
);
6523 VERIFY0(avl_numnodes(&spa
->spa_alloc_tree
));
6524 mutex_exit(&spa
->spa_alloc_lock
);
6527 * If there are any pending vdev state changes, convert them
6528 * into config changes that go out with this transaction group.
6530 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
6531 while (list_head(&spa
->spa_state_dirty_list
) != NULL
) {
6533 * We need the write lock here because, for aux vdevs,
6534 * calling vdev_config_dirty() modifies sav_config.
6535 * This is ugly and will become unnecessary when we
6536 * eliminate the aux vdev wart by integrating all vdevs
6537 * into the root vdev tree.
6539 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
6540 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_WRITER
);
6541 while ((vd
= list_head(&spa
->spa_state_dirty_list
)) != NULL
) {
6542 vdev_state_clean(vd
);
6543 vdev_config_dirty(vd
);
6545 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
6546 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
6548 spa_config_exit(spa
, SCL_STATE
, FTAG
);
6550 tx
= dmu_tx_create_assigned(dp
, txg
);
6552 spa
->spa_sync_starttime
= gethrtime();
6553 VERIFY(cyclic_reprogram(spa
->spa_deadman_cycid
,
6554 spa
->spa_sync_starttime
+ spa
->spa_deadman_synctime
));
6557 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
6558 * set spa_deflate if we have no raid-z vdevs.
6560 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_RAIDZ_DEFLATE
&&
6561 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
6564 for (i
= 0; i
< rvd
->vdev_children
; i
++) {
6565 vd
= rvd
->vdev_child
[i
];
6566 if (vd
->vdev_deflate_ratio
!= SPA_MINBLOCKSIZE
)
6569 if (i
== rvd
->vdev_children
) {
6570 spa
->spa_deflate
= TRUE
;
6571 VERIFY(0 == zap_add(spa
->spa_meta_objset
,
6572 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
6573 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
));
6578 * Set the top-level vdev's max queue depth. Evaluate each
6579 * top-level's async write queue depth in case it changed.
6580 * The max queue depth will not change in the middle of syncing
6583 uint64_t queue_depth_total
= 0;
6584 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
6585 vdev_t
*tvd
= rvd
->vdev_child
[c
];
6586 metaslab_group_t
*mg
= tvd
->vdev_mg
;
6588 if (mg
== NULL
|| mg
->mg_class
!= spa_normal_class(spa
) ||
6589 !metaslab_group_initialized(mg
))
6593 * It is safe to do a lock-free check here because only async
6594 * allocations look at mg_max_alloc_queue_depth, and async
6595 * allocations all happen from spa_sync().
6597 ASSERT0(refcount_count(&mg
->mg_alloc_queue_depth
));
6598 mg
->mg_max_alloc_queue_depth
= max_queue_depth
;
6599 queue_depth_total
+= mg
->mg_max_alloc_queue_depth
;
6601 metaslab_class_t
*mc
= spa_normal_class(spa
);
6602 ASSERT0(refcount_count(&mc
->mc_alloc_slots
));
6603 mc
->mc_alloc_max_slots
= queue_depth_total
;
6604 mc
->mc_alloc_throttle_enabled
= zio_dva_throttle_enabled
;
6606 ASSERT3U(mc
->mc_alloc_max_slots
, <=,
6607 max_queue_depth
* rvd
->vdev_children
);
6610 * Iterate to convergence.
6613 int pass
= ++spa
->spa_sync_pass
;
6615 spa_sync_config_object(spa
, tx
);
6616 spa_sync_aux_dev(spa
, &spa
->spa_spares
, tx
,
6617 ZPOOL_CONFIG_SPARES
, DMU_POOL_SPARES
);
6618 spa_sync_aux_dev(spa
, &spa
->spa_l2cache
, tx
,
6619 ZPOOL_CONFIG_L2CACHE
, DMU_POOL_L2CACHE
);
6620 spa_errlog_sync(spa
, txg
);
6621 dsl_pool_sync(dp
, txg
);
6623 if (pass
< zfs_sync_pass_deferred_free
) {
6624 spa_sync_frees(spa
, free_bpl
, tx
);
6627 * We can not defer frees in pass 1, because
6628 * we sync the deferred frees later in pass 1.
6630 ASSERT3U(pass
, >, 1);
6631 bplist_iterate(free_bpl
, bpobj_enqueue_cb
,
6632 &spa
->spa_deferred_bpobj
, tx
);
6636 dsl_scan_sync(dp
, tx
);
6638 while (vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, txg
))
6642 spa_sync_upgrades(spa
, tx
);
6644 spa
->spa_uberblock
.ub_rootbp
.blk_birth
);
6646 * Note: We need to check if the MOS is dirty
6647 * because we could have marked the MOS dirty
6648 * without updating the uberblock (e.g. if we
6649 * have sync tasks but no dirty user data). We
6650 * need to check the uberblock's rootbp because
6651 * it is updated if we have synced out dirty
6652 * data (though in this case the MOS will most
6653 * likely also be dirty due to second order
6654 * effects, we don't want to rely on that here).
6656 if (spa
->spa_uberblock
.ub_rootbp
.blk_birth
< txg
&&
6657 !dmu_objset_is_dirty(mos
, txg
)) {
6659 * Nothing changed on the first pass,
6660 * therefore this TXG is a no-op. Avoid
6661 * syncing deferred frees, so that we
6662 * can keep this TXG as a no-op.
6664 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
,
6666 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
6667 ASSERT(txg_list_empty(&dp
->dp_sync_tasks
, txg
));
6670 spa_sync_deferred_frees(spa
, tx
);
6673 } while (dmu_objset_is_dirty(mos
, txg
));
6675 if (!list_is_empty(&spa
->spa_config_dirty_list
)) {
6677 * Make sure that the number of ZAPs for all the vdevs matches
6678 * the number of ZAPs in the per-vdev ZAP list. This only gets
6679 * called if the config is dirty; otherwise there may be
6680 * outstanding AVZ operations that weren't completed in
6681 * spa_sync_config_object.
6683 uint64_t all_vdev_zap_entry_count
;
6684 ASSERT0(zap_count(spa
->spa_meta_objset
,
6685 spa
->spa_all_vdev_zaps
, &all_vdev_zap_entry_count
));
6686 ASSERT3U(vdev_count_verify_zaps(spa
->spa_root_vdev
), ==,
6687 all_vdev_zap_entry_count
);
6691 * Rewrite the vdev configuration (which includes the uberblock)
6692 * to commit the transaction group.
6694 * If there are no dirty vdevs, we sync the uberblock to a few
6695 * random top-level vdevs that are known to be visible in the
6696 * config cache (see spa_vdev_add() for a complete description).
6697 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
6701 * We hold SCL_STATE to prevent vdev open/close/etc.
6702 * while we're attempting to write the vdev labels.
6704 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
6706 if (list_is_empty(&spa
->spa_config_dirty_list
)) {
6707 vdev_t
*svd
[SPA_DVAS_PER_BP
];
6709 int children
= rvd
->vdev_children
;
6710 int c0
= spa_get_random(children
);
6712 for (int c
= 0; c
< children
; c
++) {
6713 vd
= rvd
->vdev_child
[(c0
+ c
) % children
];
6714 if (vd
->vdev_ms_array
== 0 || vd
->vdev_islog
)
6716 svd
[svdcount
++] = vd
;
6717 if (svdcount
== SPA_DVAS_PER_BP
)
6720 error
= vdev_config_sync(svd
, svdcount
, txg
);
6722 error
= vdev_config_sync(rvd
->vdev_child
,
6723 rvd
->vdev_children
, txg
);
6727 spa
->spa_last_synced_guid
= rvd
->vdev_guid
;
6729 spa_config_exit(spa
, SCL_STATE
, FTAG
);
6733 zio_suspend(spa
, NULL
);
6734 zio_resume_wait(spa
);
6738 VERIFY(cyclic_reprogram(spa
->spa_deadman_cycid
, CY_INFINITY
));
6741 * Clear the dirty config list.
6743 while ((vd
= list_head(&spa
->spa_config_dirty_list
)) != NULL
)
6744 vdev_config_clean(vd
);
6747 * Now that the new config has synced transactionally,
6748 * let it become visible to the config cache.
6750 if (spa
->spa_config_syncing
!= NULL
) {
6751 spa_config_set(spa
, spa
->spa_config_syncing
);
6752 spa
->spa_config_txg
= txg
;
6753 spa
->spa_config_syncing
= NULL
;
6756 dsl_pool_sync_done(dp
, txg
);
6758 mutex_enter(&spa
->spa_alloc_lock
);
6759 VERIFY0(avl_numnodes(&spa
->spa_alloc_tree
));
6760 mutex_exit(&spa
->spa_alloc_lock
);
6763 * Update usable space statistics.
6765 while (vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, TXG_CLEAN(txg
)))
6766 vdev_sync_done(vd
, txg
);
6768 spa_update_dspace(spa
);
6771 * It had better be the case that we didn't dirty anything
6772 * since vdev_config_sync().
6774 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
, txg
));
6775 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
6776 ASSERT(txg_list_empty(&spa
->spa_vdev_txg_list
, txg
));
6778 spa
->spa_sync_pass
= 0;
6781 * Update the last synced uberblock here. We want to do this at
6782 * the end of spa_sync() so that consumers of spa_last_synced_txg()
6783 * will be guaranteed that all the processing associated with
6784 * that txg has been completed.
6786 spa
->spa_ubsync
= spa
->spa_uberblock
;
6787 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
6789 spa_handle_ignored_writes(spa
);
6792 * If any async tasks have been requested, kick them off.
6794 spa_async_dispatch(spa
);
6798 * Sync all pools. We don't want to hold the namespace lock across these
6799 * operations, so we take a reference on the spa_t and drop the lock during the
6803 spa_sync_allpools(void)
6806 mutex_enter(&spa_namespace_lock
);
6807 while ((spa
= spa_next(spa
)) != NULL
) {
6808 if (spa_state(spa
) != POOL_STATE_ACTIVE
||
6809 !spa_writeable(spa
) || spa_suspended(spa
))
6811 spa_open_ref(spa
, FTAG
);
6812 mutex_exit(&spa_namespace_lock
);
6813 txg_wait_synced(spa_get_dsl(spa
), 0);
6814 mutex_enter(&spa_namespace_lock
);
6815 spa_close(spa
, FTAG
);
6817 mutex_exit(&spa_namespace_lock
);
6821 * ==========================================================================
6822 * Miscellaneous routines
6823 * ==========================================================================
6827 * Remove all pools in the system.
6835 * Remove all cached state. All pools should be closed now,
6836 * so every spa in the AVL tree should be unreferenced.
6838 mutex_enter(&spa_namespace_lock
);
6839 while ((spa
= spa_next(NULL
)) != NULL
) {
6841 * Stop async tasks. The async thread may need to detach
6842 * a device that's been replaced, which requires grabbing
6843 * spa_namespace_lock, so we must drop it here.
6845 spa_open_ref(spa
, FTAG
);
6846 mutex_exit(&spa_namespace_lock
);
6847 spa_async_suspend(spa
);
6848 mutex_enter(&spa_namespace_lock
);
6849 spa_close(spa
, FTAG
);
6851 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
6853 spa_deactivate(spa
);
6857 mutex_exit(&spa_namespace_lock
);
6861 spa_lookup_by_guid(spa_t
*spa
, uint64_t guid
, boolean_t aux
)
6866 if ((vd
= vdev_lookup_by_guid(spa
->spa_root_vdev
, guid
)) != NULL
)
6870 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
6871 vd
= spa
->spa_l2cache
.sav_vdevs
[i
];
6872 if (vd
->vdev_guid
== guid
)
6876 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
6877 vd
= spa
->spa_spares
.sav_vdevs
[i
];
6878 if (vd
->vdev_guid
== guid
)
6887 spa_upgrade(spa_t
*spa
, uint64_t version
)
6889 ASSERT(spa_writeable(spa
));
6891 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6894 * This should only be called for a non-faulted pool, and since a
6895 * future version would result in an unopenable pool, this shouldn't be
6898 ASSERT(SPA_VERSION_IS_SUPPORTED(spa
->spa_uberblock
.ub_version
));
6899 ASSERT3U(version
, >=, spa
->spa_uberblock
.ub_version
);
6901 spa
->spa_uberblock
.ub_version
= version
;
6902 vdev_config_dirty(spa
->spa_root_vdev
);
6904 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6906 txg_wait_synced(spa_get_dsl(spa
), 0);
6910 spa_has_spare(spa_t
*spa
, uint64_t guid
)
6914 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
6916 for (i
= 0; i
< sav
->sav_count
; i
++)
6917 if (sav
->sav_vdevs
[i
]->vdev_guid
== guid
)
6920 for (i
= 0; i
< sav
->sav_npending
; i
++) {
6921 if (nvlist_lookup_uint64(sav
->sav_pending
[i
], ZPOOL_CONFIG_GUID
,
6922 &spareguid
) == 0 && spareguid
== guid
)
6930 * Check if a pool has an active shared spare device.
6931 * Note: reference count of an active spare is 2, as a spare and as a replace
6934 spa_has_active_shared_spare(spa_t
*spa
)
6938 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
6940 for (i
= 0; i
< sav
->sav_count
; i
++) {
6941 if (spa_spare_exists(sav
->sav_vdevs
[i
]->vdev_guid
, &pool
,
6942 &refcnt
) && pool
!= 0ULL && pool
== spa_guid(spa
) &&
6951 spa_event_create(spa_t
*spa
, vdev_t
*vd
, const char *name
)
6953 sysevent_t
*ev
= NULL
;
6955 sysevent_attr_list_t
*attr
= NULL
;
6956 sysevent_value_t value
;
6958 ev
= sysevent_alloc(EC_ZFS
, (char *)name
, SUNW_KERN_PUB
"zfs",
6962 value
.value_type
= SE_DATA_TYPE_STRING
;
6963 value
.value
.sv_string
= spa_name(spa
);
6964 if (sysevent_add_attr(&attr
, ZFS_EV_POOL_NAME
, &value
, SE_SLEEP
) != 0)
6967 value
.value_type
= SE_DATA_TYPE_UINT64
;
6968 value
.value
.sv_uint64
= spa_guid(spa
);
6969 if (sysevent_add_attr(&attr
, ZFS_EV_POOL_GUID
, &value
, SE_SLEEP
) != 0)
6973 value
.value_type
= SE_DATA_TYPE_UINT64
;
6974 value
.value
.sv_uint64
= vd
->vdev_guid
;
6975 if (sysevent_add_attr(&attr
, ZFS_EV_VDEV_GUID
, &value
,
6979 if (vd
->vdev_path
) {
6980 value
.value_type
= SE_DATA_TYPE_STRING
;
6981 value
.value
.sv_string
= vd
->vdev_path
;
6982 if (sysevent_add_attr(&attr
, ZFS_EV_VDEV_PATH
,
6983 &value
, SE_SLEEP
) != 0)
6988 if (sysevent_attach_attributes(ev
, attr
) != 0)
6994 sysevent_free_attr(attr
);
7001 spa_event_post(sysevent_t
*ev
)
7006 (void) log_sysevent(ev
, SE_SLEEP
, &eid
);
7012 * Post a sysevent corresponding to the given event. The 'name' must be one of
7013 * the event definitions in sys/sysevent/eventdefs.h. The payload will be
7014 * filled in from the spa and (optionally) the vdev. This doesn't do anything
7015 * in the userland libzpool, as we don't want consumers to misinterpret ztest
7016 * or zdb as real changes.
7019 spa_event_notify(spa_t
*spa
, vdev_t
*vd
, const char *name
)
7021 spa_event_post(spa_event_create(spa
, vd
, name
));