4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2013 by Delphix. All rights reserved.
25 * Copyright (c) 2015, Nexenta Systems, Inc. All rights reserved.
26 * Copyright (c) 2013, 2014, Nexenta Systems, Inc. All rights reserved.
27 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
28 * Copyright 2013 Saso Kiselkov. All rights reserved.
29 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
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/vdev_disk.h>
52 #include <sys/metaslab.h>
53 #include <sys/metaslab_impl.h>
54 #include <sys/uberblock_impl.h>
57 #include <sys/dmu_traverse.h>
58 #include <sys/dmu_objset.h>
59 #include <sys/unique.h>
60 #include <sys/dsl_pool.h>
61 #include <sys/dsl_dataset.h>
62 #include <sys/dsl_dir.h>
63 #include <sys/dsl_prop.h>
64 #include <sys/dsl_synctask.h>
65 #include <sys/fs/zfs.h>
67 #include <sys/callb.h>
68 #include <sys/systeminfo.h>
69 #include <sys/spa_boot.h>
70 #include <sys/zfs_ioctl.h>
71 #include <sys/dsl_scan.h>
72 #include <sys/zfeature.h>
73 #include <sys/dsl_destroy.h>
77 #include <sys/bootprops.h>
78 #include <sys/callb.h>
79 #include <sys/cpupart.h>
81 #include <sys/sysdc.h>
86 #include "zfs_comutil.h"
89 * The interval, in seconds, at which failed configuration cache file writes
92 static int zfs_ccw_retry_interval
= 300;
94 typedef enum zti_modes
{
95 ZTI_MODE_FIXED
, /* value is # of threads (min 1) */
96 ZTI_MODE_BATCH
, /* cpu-intensive; value is ignored */
97 ZTI_MODE_NULL
, /* don't create a taskq */
101 #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) }
102 #define ZTI_PCT(n) { ZTI_MODE_ONLINE_PERCENT, (n), 1 }
103 #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 }
104 #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 }
106 #define ZTI_N(n) ZTI_P(n, 1)
107 #define ZTI_ONE ZTI_N(1)
109 typedef struct zio_taskq_info
{
110 zti_modes_t zti_mode
;
115 static const char *const zio_taskq_types
[ZIO_TASKQ_TYPES
] = {
116 "iss", "iss_h", "int", "int_h"
120 * This table defines the taskq settings for each ZFS I/O type. When
121 * initializing a pool, we use this table to create an appropriately sized
122 * taskq. Some operations are low volume and therefore have a small, static
123 * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
124 * macros. Other operations process a large amount of data; the ZTI_BATCH
125 * macro causes us to create a taskq oriented for throughput. Some operations
126 * are so high frequency and short-lived that the taskq itself can become a a
127 * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
128 * additional degree of parallelism specified by the number of threads per-
129 * taskq and the number of taskqs; when dispatching an event in this case, the
130 * particular taskq is chosen at random.
132 * The different taskq priorities are to handle the different contexts (issue
133 * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that
134 * need to be handled with minimum delay.
136 const zio_taskq_info_t zio_taskqs
[ZIO_TYPES
][ZIO_TASKQ_TYPES
] = {
137 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
138 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* NULL */
139 { ZTI_N(8), ZTI_NULL
, ZTI_P(12, 8), ZTI_NULL
}, /* READ */
140 { ZTI_BATCH
, ZTI_N(5), ZTI_P(12, 8), ZTI_N(5) }, /* WRITE */
141 { ZTI_P(12, 8), ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* FREE */
142 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* CLAIM */
143 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* IOCTL */
146 static void spa_sync_version(void *arg
, dmu_tx_t
*tx
);
147 static void spa_sync_props(void *arg
, dmu_tx_t
*tx
);
148 static boolean_t
spa_has_active_shared_spare(spa_t
*spa
);
149 static inline int spa_load_impl(spa_t
*spa
, uint64_t, nvlist_t
*config
,
150 spa_load_state_t state
, spa_import_type_t type
, boolean_t mosconfig
,
152 static void spa_vdev_resilver_done(spa_t
*spa
);
154 uint_t zio_taskq_batch_pct
= 75; /* 1 thread per cpu in pset */
155 id_t zio_taskq_psrset_bind
= PS_NONE
;
156 boolean_t zio_taskq_sysdc
= B_TRUE
; /* use SDC scheduling class */
157 uint_t zio_taskq_basedc
= 80; /* base duty cycle */
159 boolean_t spa_create_process
= B_TRUE
; /* no process ==> no sysdc */
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 const 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 spa_prop_add_list(*nvp
, ZPOOL_PROP_VERSION
, NULL
,
238 version
, ZPROP_SRC_DEFAULT
);
240 spa_prop_add_list(*nvp
, ZPOOL_PROP_VERSION
, NULL
,
241 version
, ZPROP_SRC_LOCAL
);
247 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
248 * when opening pools before this version freedir will be NULL.
250 if (pool
->dp_free_dir
!= NULL
) {
251 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
, NULL
,
252 dsl_dir_phys(pool
->dp_free_dir
)->dd_used_bytes
,
255 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
,
259 if (pool
->dp_leak_dir
!= NULL
) {
260 spa_prop_add_list(*nvp
, ZPOOL_PROP_LEAKED
, NULL
,
261 dsl_dir_phys(pool
->dp_leak_dir
)->dd_used_bytes
,
264 spa_prop_add_list(*nvp
, ZPOOL_PROP_LEAKED
,
269 spa_prop_add_list(*nvp
, ZPOOL_PROP_GUID
, NULL
, spa_guid(spa
), src
);
271 if (spa
->spa_comment
!= NULL
) {
272 spa_prop_add_list(*nvp
, ZPOOL_PROP_COMMENT
, spa
->spa_comment
,
276 if (spa
->spa_root
!= NULL
)
277 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALTROOT
, spa
->spa_root
,
280 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_BLOCKS
)) {
281 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXBLOCKSIZE
, NULL
,
282 MIN(zfs_max_recordsize
, SPA_MAXBLOCKSIZE
), ZPROP_SRC_NONE
);
284 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXBLOCKSIZE
, NULL
,
285 SPA_OLD_MAXBLOCKSIZE
, ZPROP_SRC_NONE
);
288 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_DNODE
)) {
289 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXDNODESIZE
, NULL
,
290 DNODE_MAX_SIZE
, ZPROP_SRC_NONE
);
292 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXDNODESIZE
, NULL
,
293 DNODE_MIN_SIZE
, ZPROP_SRC_NONE
);
296 if ((dp
= list_head(&spa
->spa_config_list
)) != NULL
) {
297 if (dp
->scd_path
== NULL
) {
298 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
299 "none", 0, ZPROP_SRC_LOCAL
);
300 } else if (strcmp(dp
->scd_path
, spa_config_path
) != 0) {
301 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
302 dp
->scd_path
, 0, ZPROP_SRC_LOCAL
);
308 * Get zpool property values.
311 spa_prop_get(spa_t
*spa
, nvlist_t
**nvp
)
313 objset_t
*mos
= spa
->spa_meta_objset
;
318 err
= nvlist_alloc(nvp
, NV_UNIQUE_NAME
, KM_SLEEP
);
322 mutex_enter(&spa
->spa_props_lock
);
325 * Get properties from the spa config.
327 spa_prop_get_config(spa
, nvp
);
329 /* If no pool property object, no more prop to get. */
330 if (mos
== NULL
|| spa
->spa_pool_props_object
== 0) {
331 mutex_exit(&spa
->spa_props_lock
);
336 * Get properties from the MOS pool property object.
338 for (zap_cursor_init(&zc
, mos
, spa
->spa_pool_props_object
);
339 (err
= zap_cursor_retrieve(&zc
, &za
)) == 0;
340 zap_cursor_advance(&zc
)) {
343 zprop_source_t src
= ZPROP_SRC_DEFAULT
;
346 if ((prop
= zpool_name_to_prop(za
.za_name
)) == ZPROP_INVAL
)
349 switch (za
.za_integer_length
) {
351 /* integer property */
352 if (za
.za_first_integer
!=
353 zpool_prop_default_numeric(prop
))
354 src
= ZPROP_SRC_LOCAL
;
356 if (prop
== ZPOOL_PROP_BOOTFS
) {
358 dsl_dataset_t
*ds
= NULL
;
360 dp
= spa_get_dsl(spa
);
361 dsl_pool_config_enter(dp
, FTAG
);
362 if ((err
= dsl_dataset_hold_obj(dp
,
363 za
.za_first_integer
, FTAG
, &ds
))) {
364 dsl_pool_config_exit(dp
, FTAG
);
368 strval
= kmem_alloc(ZFS_MAX_DATASET_NAME_LEN
,
370 dsl_dataset_name(ds
, strval
);
371 dsl_dataset_rele(ds
, FTAG
);
372 dsl_pool_config_exit(dp
, FTAG
);
375 intval
= za
.za_first_integer
;
378 spa_prop_add_list(*nvp
, prop
, strval
, intval
, src
);
381 kmem_free(strval
, ZFS_MAX_DATASET_NAME_LEN
);
386 /* string property */
387 strval
= kmem_alloc(za
.za_num_integers
, KM_SLEEP
);
388 err
= zap_lookup(mos
, spa
->spa_pool_props_object
,
389 za
.za_name
, 1, za
.za_num_integers
, strval
);
391 kmem_free(strval
, za
.za_num_integers
);
394 spa_prop_add_list(*nvp
, prop
, strval
, 0, src
);
395 kmem_free(strval
, za
.za_num_integers
);
402 zap_cursor_fini(&zc
);
403 mutex_exit(&spa
->spa_props_lock
);
405 if (err
&& err
!= ENOENT
) {
415 * Validate the given pool properties nvlist and modify the list
416 * for the property values to be set.
419 spa_prop_validate(spa_t
*spa
, nvlist_t
*props
)
422 int error
= 0, reset_bootfs
= 0;
424 boolean_t has_feature
= B_FALSE
;
427 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
429 char *strval
, *slash
, *check
, *fname
;
430 const char *propname
= nvpair_name(elem
);
431 zpool_prop_t prop
= zpool_name_to_prop(propname
);
435 if (!zpool_prop_feature(propname
)) {
436 error
= SET_ERROR(EINVAL
);
441 * Sanitize the input.
443 if (nvpair_type(elem
) != DATA_TYPE_UINT64
) {
444 error
= SET_ERROR(EINVAL
);
448 if (nvpair_value_uint64(elem
, &intval
) != 0) {
449 error
= SET_ERROR(EINVAL
);
454 error
= SET_ERROR(EINVAL
);
458 fname
= strchr(propname
, '@') + 1;
459 if (zfeature_lookup_name(fname
, NULL
) != 0) {
460 error
= SET_ERROR(EINVAL
);
464 has_feature
= B_TRUE
;
467 case ZPOOL_PROP_VERSION
:
468 error
= nvpair_value_uint64(elem
, &intval
);
470 (intval
< spa_version(spa
) ||
471 intval
> SPA_VERSION_BEFORE_FEATURES
||
473 error
= SET_ERROR(EINVAL
);
476 case ZPOOL_PROP_DELEGATION
:
477 case ZPOOL_PROP_AUTOREPLACE
:
478 case ZPOOL_PROP_LISTSNAPS
:
479 case ZPOOL_PROP_AUTOEXPAND
:
480 error
= nvpair_value_uint64(elem
, &intval
);
481 if (!error
&& intval
> 1)
482 error
= SET_ERROR(EINVAL
);
485 case ZPOOL_PROP_BOOTFS
:
487 * If the pool version is less than SPA_VERSION_BOOTFS,
488 * or the pool is still being created (version == 0),
489 * the bootfs property cannot be set.
491 if (spa_version(spa
) < SPA_VERSION_BOOTFS
) {
492 error
= SET_ERROR(ENOTSUP
);
497 * Make sure the vdev config is bootable
499 if (!vdev_is_bootable(spa
->spa_root_vdev
)) {
500 error
= SET_ERROR(ENOTSUP
);
506 error
= nvpair_value_string(elem
, &strval
);
512 if (strval
== NULL
|| strval
[0] == '\0') {
513 objnum
= zpool_prop_default_numeric(
518 error
= dmu_objset_hold(strval
, FTAG
, &os
);
523 * Must be ZPL, and its property settings
524 * must be supported by GRUB (compression
525 * is not gzip, and large blocks or large
526 * dnodes are not used).
529 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
530 error
= SET_ERROR(ENOTSUP
);
532 dsl_prop_get_int_ds(dmu_objset_ds(os
),
533 zfs_prop_to_name(ZFS_PROP_COMPRESSION
),
535 !BOOTFS_COMPRESS_VALID(propval
)) {
536 error
= SET_ERROR(ENOTSUP
);
538 dsl_prop_get_int_ds(dmu_objset_ds(os
),
539 zfs_prop_to_name(ZFS_PROP_RECORDSIZE
),
541 propval
> SPA_OLD_MAXBLOCKSIZE
) {
542 error
= SET_ERROR(ENOTSUP
);
544 dsl_prop_get_int_ds(dmu_objset_ds(os
),
545 zfs_prop_to_name(ZFS_PROP_DNODESIZE
),
547 propval
!= ZFS_DNSIZE_LEGACY
) {
548 error
= SET_ERROR(ENOTSUP
);
550 objnum
= dmu_objset_id(os
);
552 dmu_objset_rele(os
, FTAG
);
556 case ZPOOL_PROP_FAILUREMODE
:
557 error
= nvpair_value_uint64(elem
, &intval
);
558 if (!error
&& (intval
< ZIO_FAILURE_MODE_WAIT
||
559 intval
> ZIO_FAILURE_MODE_PANIC
))
560 error
= SET_ERROR(EINVAL
);
563 * This is a special case which only occurs when
564 * the pool has completely failed. This allows
565 * the user to change the in-core failmode property
566 * without syncing it out to disk (I/Os might
567 * currently be blocked). We do this by returning
568 * EIO to the caller (spa_prop_set) to trick it
569 * into thinking we encountered a property validation
572 if (!error
&& spa_suspended(spa
)) {
573 spa
->spa_failmode
= intval
;
574 error
= SET_ERROR(EIO
);
578 case ZPOOL_PROP_CACHEFILE
:
579 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
582 if (strval
[0] == '\0')
585 if (strcmp(strval
, "none") == 0)
588 if (strval
[0] != '/') {
589 error
= SET_ERROR(EINVAL
);
593 slash
= strrchr(strval
, '/');
594 ASSERT(slash
!= NULL
);
596 if (slash
[1] == '\0' || strcmp(slash
, "/.") == 0 ||
597 strcmp(slash
, "/..") == 0)
598 error
= SET_ERROR(EINVAL
);
601 case ZPOOL_PROP_COMMENT
:
602 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
604 for (check
= strval
; *check
!= '\0'; check
++) {
605 if (!isprint(*check
)) {
606 error
= SET_ERROR(EINVAL
);
610 if (strlen(strval
) > ZPROP_MAX_COMMENT
)
611 error
= SET_ERROR(E2BIG
);
614 case ZPOOL_PROP_DEDUPDITTO
:
615 if (spa_version(spa
) < SPA_VERSION_DEDUP
)
616 error
= SET_ERROR(ENOTSUP
);
618 error
= nvpair_value_uint64(elem
, &intval
);
620 intval
!= 0 && intval
< ZIO_DEDUPDITTO_MIN
)
621 error
= SET_ERROR(EINVAL
);
632 if (!error
&& reset_bootfs
) {
633 error
= nvlist_remove(props
,
634 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), DATA_TYPE_STRING
);
637 error
= nvlist_add_uint64(props
,
638 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), objnum
);
646 spa_configfile_set(spa_t
*spa
, nvlist_t
*nvp
, boolean_t need_sync
)
649 spa_config_dirent_t
*dp
;
651 if (nvlist_lookup_string(nvp
, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
),
655 dp
= kmem_alloc(sizeof (spa_config_dirent_t
),
658 if (cachefile
[0] == '\0')
659 dp
->scd_path
= spa_strdup(spa_config_path
);
660 else if (strcmp(cachefile
, "none") == 0)
663 dp
->scd_path
= spa_strdup(cachefile
);
665 list_insert_head(&spa
->spa_config_list
, dp
);
667 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
671 spa_prop_set(spa_t
*spa
, nvlist_t
*nvp
)
674 nvpair_t
*elem
= NULL
;
675 boolean_t need_sync
= B_FALSE
;
677 if ((error
= spa_prop_validate(spa
, nvp
)) != 0)
680 while ((elem
= nvlist_next_nvpair(nvp
, elem
)) != NULL
) {
681 zpool_prop_t prop
= zpool_name_to_prop(nvpair_name(elem
));
683 if (prop
== ZPOOL_PROP_CACHEFILE
||
684 prop
== ZPOOL_PROP_ALTROOT
||
685 prop
== ZPOOL_PROP_READONLY
)
688 if (prop
== ZPOOL_PROP_VERSION
|| prop
== ZPROP_INVAL
) {
691 if (prop
== ZPOOL_PROP_VERSION
) {
692 VERIFY(nvpair_value_uint64(elem
, &ver
) == 0);
694 ASSERT(zpool_prop_feature(nvpair_name(elem
)));
695 ver
= SPA_VERSION_FEATURES
;
699 /* Save time if the version is already set. */
700 if (ver
== spa_version(spa
))
704 * In addition to the pool directory object, we might
705 * create the pool properties object, the features for
706 * read object, the features for write object, or the
707 * feature descriptions object.
709 error
= dsl_sync_task(spa
->spa_name
, NULL
,
710 spa_sync_version
, &ver
,
711 6, ZFS_SPACE_CHECK_RESERVED
);
722 return (dsl_sync_task(spa
->spa_name
, NULL
, spa_sync_props
,
723 nvp
, 6, ZFS_SPACE_CHECK_RESERVED
));
730 * If the bootfs property value is dsobj, clear it.
733 spa_prop_clear_bootfs(spa_t
*spa
, uint64_t dsobj
, dmu_tx_t
*tx
)
735 if (spa
->spa_bootfs
== dsobj
&& spa
->spa_pool_props_object
!= 0) {
736 VERIFY(zap_remove(spa
->spa_meta_objset
,
737 spa
->spa_pool_props_object
,
738 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), tx
) == 0);
745 spa_change_guid_check(void *arg
, dmu_tx_t
*tx
)
747 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
748 vdev_t
*rvd
= spa
->spa_root_vdev
;
750 ASSERTV(uint64_t *newguid
= arg
);
752 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
753 vdev_state
= rvd
->vdev_state
;
754 spa_config_exit(spa
, SCL_STATE
, FTAG
);
756 if (vdev_state
!= VDEV_STATE_HEALTHY
)
757 return (SET_ERROR(ENXIO
));
759 ASSERT3U(spa_guid(spa
), !=, *newguid
);
765 spa_change_guid_sync(void *arg
, dmu_tx_t
*tx
)
767 uint64_t *newguid
= arg
;
768 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
770 vdev_t
*rvd
= spa
->spa_root_vdev
;
772 oldguid
= spa_guid(spa
);
774 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
775 rvd
->vdev_guid
= *newguid
;
776 rvd
->vdev_guid_sum
+= (*newguid
- oldguid
);
777 vdev_config_dirty(rvd
);
778 spa_config_exit(spa
, SCL_STATE
, FTAG
);
780 spa_history_log_internal(spa
, "guid change", tx
, "old=%llu new=%llu",
785 * Change the GUID for the pool. This is done so that we can later
786 * re-import a pool built from a clone of our own vdevs. We will modify
787 * the root vdev's guid, our own pool guid, and then mark all of our
788 * vdevs dirty. Note that we must make sure that all our vdevs are
789 * online when we do this, or else any vdevs that weren't present
790 * would be orphaned from our pool. We are also going to issue a
791 * sysevent to update any watchers.
794 spa_change_guid(spa_t
*spa
)
799 mutex_enter(&spa
->spa_vdev_top_lock
);
800 mutex_enter(&spa_namespace_lock
);
801 guid
= spa_generate_guid(NULL
);
803 error
= dsl_sync_task(spa
->spa_name
, spa_change_guid_check
,
804 spa_change_guid_sync
, &guid
, 5, ZFS_SPACE_CHECK_RESERVED
);
807 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
808 spa_event_notify(spa
, NULL
, ESC_ZFS_POOL_REGUID
);
811 mutex_exit(&spa_namespace_lock
);
812 mutex_exit(&spa
->spa_vdev_top_lock
);
818 * ==========================================================================
819 * SPA state manipulation (open/create/destroy/import/export)
820 * ==========================================================================
824 spa_error_entry_compare(const void *a
, const void *b
)
826 const spa_error_entry_t
*sa
= (const spa_error_entry_t
*)a
;
827 const spa_error_entry_t
*sb
= (const spa_error_entry_t
*)b
;
830 ret
= memcmp(&sa
->se_bookmark
, &sb
->se_bookmark
,
831 sizeof (zbookmark_phys_t
));
833 return (AVL_ISIGN(ret
));
837 * Utility function which retrieves copies of the current logs and
838 * re-initializes them in the process.
841 spa_get_errlists(spa_t
*spa
, avl_tree_t
*last
, avl_tree_t
*scrub
)
843 ASSERT(MUTEX_HELD(&spa
->spa_errlist_lock
));
845 bcopy(&spa
->spa_errlist_last
, last
, sizeof (avl_tree_t
));
846 bcopy(&spa
->spa_errlist_scrub
, scrub
, sizeof (avl_tree_t
));
848 avl_create(&spa
->spa_errlist_scrub
,
849 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
850 offsetof(spa_error_entry_t
, se_avl
));
851 avl_create(&spa
->spa_errlist_last
,
852 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
853 offsetof(spa_error_entry_t
, se_avl
));
857 spa_taskqs_init(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
859 const zio_taskq_info_t
*ztip
= &zio_taskqs
[t
][q
];
860 enum zti_modes mode
= ztip
->zti_mode
;
861 uint_t value
= ztip
->zti_value
;
862 uint_t count
= ztip
->zti_count
;
863 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
866 boolean_t batch
= B_FALSE
;
868 if (mode
== ZTI_MODE_NULL
) {
870 tqs
->stqs_taskq
= NULL
;
874 ASSERT3U(count
, >, 0);
876 tqs
->stqs_count
= count
;
877 tqs
->stqs_taskq
= kmem_alloc(count
* sizeof (taskq_t
*), KM_SLEEP
);
881 ASSERT3U(value
, >=, 1);
882 value
= MAX(value
, 1);
883 flags
|= TASKQ_DYNAMIC
;
888 flags
|= TASKQ_THREADS_CPU_PCT
;
889 value
= MIN(zio_taskq_batch_pct
, 100);
893 panic("unrecognized mode for %s_%s taskq (%u:%u) in "
895 zio_type_name
[t
], zio_taskq_types
[q
], mode
, value
);
899 for (i
= 0; i
< count
; i
++) {
903 (void) snprintf(name
, sizeof (name
), "%s_%s_%u",
904 zio_type_name
[t
], zio_taskq_types
[q
], i
);
906 (void) snprintf(name
, sizeof (name
), "%s_%s",
907 zio_type_name
[t
], zio_taskq_types
[q
]);
910 if (zio_taskq_sysdc
&& spa
->spa_proc
!= &p0
) {
912 flags
|= TASKQ_DC_BATCH
;
914 tq
= taskq_create_sysdc(name
, value
, 50, INT_MAX
,
915 spa
->spa_proc
, zio_taskq_basedc
, flags
);
917 pri_t pri
= maxclsyspri
;
919 * The write issue taskq can be extremely CPU
920 * intensive. Run it at slightly less important
921 * priority than the other taskqs. Under Linux this
922 * means incrementing the priority value on platforms
923 * like illumos it should be decremented.
925 if (t
== ZIO_TYPE_WRITE
&& q
== ZIO_TASKQ_ISSUE
)
928 tq
= taskq_create_proc(name
, value
, pri
, 50,
929 INT_MAX
, spa
->spa_proc
, flags
);
932 tqs
->stqs_taskq
[i
] = tq
;
937 spa_taskqs_fini(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
939 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
942 if (tqs
->stqs_taskq
== NULL
) {
943 ASSERT3U(tqs
->stqs_count
, ==, 0);
947 for (i
= 0; i
< tqs
->stqs_count
; i
++) {
948 ASSERT3P(tqs
->stqs_taskq
[i
], !=, NULL
);
949 taskq_destroy(tqs
->stqs_taskq
[i
]);
952 kmem_free(tqs
->stqs_taskq
, tqs
->stqs_count
* sizeof (taskq_t
*));
953 tqs
->stqs_taskq
= NULL
;
957 * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority.
958 * Note that a type may have multiple discrete taskqs to avoid lock contention
959 * on the taskq itself. In that case we choose which taskq at random by using
960 * the low bits of gethrtime().
963 spa_taskq_dispatch_ent(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
964 task_func_t
*func
, void *arg
, uint_t flags
, taskq_ent_t
*ent
)
966 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
969 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
970 ASSERT3U(tqs
->stqs_count
, !=, 0);
972 if (tqs
->stqs_count
== 1) {
973 tq
= tqs
->stqs_taskq
[0];
975 tq
= tqs
->stqs_taskq
[((uint64_t)gethrtime()) % tqs
->stqs_count
];
978 taskq_dispatch_ent(tq
, func
, arg
, flags
, ent
);
982 * Same as spa_taskq_dispatch_ent() but block on the task until completion.
985 spa_taskq_dispatch_sync(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
986 task_func_t
*func
, void *arg
, uint_t flags
)
988 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
992 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
993 ASSERT3U(tqs
->stqs_count
, !=, 0);
995 if (tqs
->stqs_count
== 1) {
996 tq
= tqs
->stqs_taskq
[0];
998 tq
= tqs
->stqs_taskq
[((uint64_t)gethrtime()) % tqs
->stqs_count
];
1001 id
= taskq_dispatch(tq
, func
, arg
, flags
);
1003 taskq_wait_id(tq
, id
);
1007 spa_create_zio_taskqs(spa_t
*spa
)
1011 for (t
= 0; t
< ZIO_TYPES
; t
++) {
1012 for (q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1013 spa_taskqs_init(spa
, t
, q
);
1018 #if defined(_KERNEL) && defined(HAVE_SPA_THREAD)
1020 spa_thread(void *arg
)
1022 callb_cpr_t cprinfo
;
1025 user_t
*pu
= PTOU(curproc
);
1027 CALLB_CPR_INIT(&cprinfo
, &spa
->spa_proc_lock
, callb_generic_cpr
,
1030 ASSERT(curproc
!= &p0
);
1031 (void) snprintf(pu
->u_psargs
, sizeof (pu
->u_psargs
),
1032 "zpool-%s", spa
->spa_name
);
1033 (void) strlcpy(pu
->u_comm
, pu
->u_psargs
, sizeof (pu
->u_comm
));
1035 /* bind this thread to the requested psrset */
1036 if (zio_taskq_psrset_bind
!= PS_NONE
) {
1038 mutex_enter(&cpu_lock
);
1039 mutex_enter(&pidlock
);
1040 mutex_enter(&curproc
->p_lock
);
1042 if (cpupart_bind_thread(curthread
, zio_taskq_psrset_bind
,
1043 0, NULL
, NULL
) == 0) {
1044 curthread
->t_bind_pset
= zio_taskq_psrset_bind
;
1047 "Couldn't bind process for zfs pool \"%s\" to "
1048 "pset %d\n", spa
->spa_name
, zio_taskq_psrset_bind
);
1051 mutex_exit(&curproc
->p_lock
);
1052 mutex_exit(&pidlock
);
1053 mutex_exit(&cpu_lock
);
1057 if (zio_taskq_sysdc
) {
1058 sysdc_thread_enter(curthread
, 100, 0);
1061 spa
->spa_proc
= curproc
;
1062 spa
->spa_did
= curthread
->t_did
;
1064 spa_create_zio_taskqs(spa
);
1066 mutex_enter(&spa
->spa_proc_lock
);
1067 ASSERT(spa
->spa_proc_state
== SPA_PROC_CREATED
);
1069 spa
->spa_proc_state
= SPA_PROC_ACTIVE
;
1070 cv_broadcast(&spa
->spa_proc_cv
);
1072 CALLB_CPR_SAFE_BEGIN(&cprinfo
);
1073 while (spa
->spa_proc_state
== SPA_PROC_ACTIVE
)
1074 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1075 CALLB_CPR_SAFE_END(&cprinfo
, &spa
->spa_proc_lock
);
1077 ASSERT(spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
);
1078 spa
->spa_proc_state
= SPA_PROC_GONE
;
1079 spa
->spa_proc
= &p0
;
1080 cv_broadcast(&spa
->spa_proc_cv
);
1081 CALLB_CPR_EXIT(&cprinfo
); /* drops spa_proc_lock */
1083 mutex_enter(&curproc
->p_lock
);
1089 * Activate an uninitialized pool.
1092 spa_activate(spa_t
*spa
, int mode
)
1094 ASSERT(spa
->spa_state
== POOL_STATE_UNINITIALIZED
);
1096 spa
->spa_state
= POOL_STATE_ACTIVE
;
1097 spa
->spa_mode
= mode
;
1099 spa
->spa_normal_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
1100 spa
->spa_log_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
1102 /* Try to create a covering process */
1103 mutex_enter(&spa
->spa_proc_lock
);
1104 ASSERT(spa
->spa_proc_state
== SPA_PROC_NONE
);
1105 ASSERT(spa
->spa_proc
== &p0
);
1108 #ifdef HAVE_SPA_THREAD
1109 /* Only create a process if we're going to be around a while. */
1110 if (spa_create_process
&& strcmp(spa
->spa_name
, TRYIMPORT_NAME
) != 0) {
1111 if (newproc(spa_thread
, (caddr_t
)spa
, syscid
, maxclsyspri
,
1113 spa
->spa_proc_state
= SPA_PROC_CREATED
;
1114 while (spa
->spa_proc_state
== SPA_PROC_CREATED
) {
1115 cv_wait(&spa
->spa_proc_cv
,
1116 &spa
->spa_proc_lock
);
1118 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1119 ASSERT(spa
->spa_proc
!= &p0
);
1120 ASSERT(spa
->spa_did
!= 0);
1124 "Couldn't create process for zfs pool \"%s\"\n",
1129 #endif /* HAVE_SPA_THREAD */
1130 mutex_exit(&spa
->spa_proc_lock
);
1132 /* If we didn't create a process, we need to create our taskqs. */
1133 if (spa
->spa_proc
== &p0
) {
1134 spa_create_zio_taskqs(spa
);
1137 list_create(&spa
->spa_config_dirty_list
, sizeof (vdev_t
),
1138 offsetof(vdev_t
, vdev_config_dirty_node
));
1139 list_create(&spa
->spa_evicting_os_list
, sizeof (objset_t
),
1140 offsetof(objset_t
, os_evicting_node
));
1141 list_create(&spa
->spa_state_dirty_list
, sizeof (vdev_t
),
1142 offsetof(vdev_t
, vdev_state_dirty_node
));
1144 txg_list_create(&spa
->spa_vdev_txg_list
,
1145 offsetof(struct vdev
, vdev_txg_node
));
1147 avl_create(&spa
->spa_errlist_scrub
,
1148 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1149 offsetof(spa_error_entry_t
, se_avl
));
1150 avl_create(&spa
->spa_errlist_last
,
1151 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1152 offsetof(spa_error_entry_t
, se_avl
));
1155 * This taskq is used to perform zvol-minor-related tasks
1156 * asynchronously. This has several advantages, including easy
1157 * resolution of various deadlocks (zfsonlinux bug #3681).
1159 * The taskq must be single threaded to ensure tasks are always
1160 * processed in the order in which they were dispatched.
1162 * A taskq per pool allows one to keep the pools independent.
1163 * This way if one pool is suspended, it will not impact another.
1165 * The preferred location to dispatch a zvol minor task is a sync
1166 * task. In this context, there is easy access to the spa_t and minimal
1167 * error handling is required because the sync task must succeed.
1169 spa
->spa_zvol_taskq
= taskq_create("z_zvol", 1, defclsyspri
,
1173 * The taskq to upgrade datasets in this pool. Currently used by
1174 * feature SPA_FEATURE_USEROBJ_ACCOUNTING.
1176 spa
->spa_upgrade_taskq
= taskq_create("z_upgrade", boot_ncpus
,
1177 defclsyspri
, 1, INT_MAX
, TASKQ_DYNAMIC
);
1181 * Opposite of spa_activate().
1184 spa_deactivate(spa_t
*spa
)
1188 ASSERT(spa
->spa_sync_on
== B_FALSE
);
1189 ASSERT(spa
->spa_dsl_pool
== NULL
);
1190 ASSERT(spa
->spa_root_vdev
== NULL
);
1191 ASSERT(spa
->spa_async_zio_root
== NULL
);
1192 ASSERT(spa
->spa_state
!= POOL_STATE_UNINITIALIZED
);
1194 spa_evicting_os_wait(spa
);
1196 if (spa
->spa_zvol_taskq
) {
1197 taskq_destroy(spa
->spa_zvol_taskq
);
1198 spa
->spa_zvol_taskq
= NULL
;
1201 if (spa
->spa_upgrade_taskq
) {
1202 taskq_destroy(spa
->spa_upgrade_taskq
);
1203 spa
->spa_upgrade_taskq
= NULL
;
1206 txg_list_destroy(&spa
->spa_vdev_txg_list
);
1208 list_destroy(&spa
->spa_config_dirty_list
);
1209 list_destroy(&spa
->spa_evicting_os_list
);
1210 list_destroy(&spa
->spa_state_dirty_list
);
1212 taskq_cancel_id(system_taskq
, spa
->spa_deadman_tqid
);
1214 for (t
= 0; t
< ZIO_TYPES
; t
++) {
1215 for (q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1216 spa_taskqs_fini(spa
, t
, q
);
1220 metaslab_class_destroy(spa
->spa_normal_class
);
1221 spa
->spa_normal_class
= NULL
;
1223 metaslab_class_destroy(spa
->spa_log_class
);
1224 spa
->spa_log_class
= NULL
;
1227 * If this was part of an import or the open otherwise failed, we may
1228 * still have errors left in the queues. Empty them just in case.
1230 spa_errlog_drain(spa
);
1232 avl_destroy(&spa
->spa_errlist_scrub
);
1233 avl_destroy(&spa
->spa_errlist_last
);
1235 spa
->spa_state
= POOL_STATE_UNINITIALIZED
;
1237 mutex_enter(&spa
->spa_proc_lock
);
1238 if (spa
->spa_proc_state
!= SPA_PROC_NONE
) {
1239 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1240 spa
->spa_proc_state
= SPA_PROC_DEACTIVATE
;
1241 cv_broadcast(&spa
->spa_proc_cv
);
1242 while (spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
) {
1243 ASSERT(spa
->spa_proc
!= &p0
);
1244 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1246 ASSERT(spa
->spa_proc_state
== SPA_PROC_GONE
);
1247 spa
->spa_proc_state
= SPA_PROC_NONE
;
1249 ASSERT(spa
->spa_proc
== &p0
);
1250 mutex_exit(&spa
->spa_proc_lock
);
1253 * We want to make sure spa_thread() has actually exited the ZFS
1254 * module, so that the module can't be unloaded out from underneath
1257 if (spa
->spa_did
!= 0) {
1258 thread_join(spa
->spa_did
);
1264 * Verify a pool configuration, and construct the vdev tree appropriately. This
1265 * will create all the necessary vdevs in the appropriate layout, with each vdev
1266 * in the CLOSED state. This will prep the pool before open/creation/import.
1267 * All vdev validation is done by the vdev_alloc() routine.
1270 spa_config_parse(spa_t
*spa
, vdev_t
**vdp
, nvlist_t
*nv
, vdev_t
*parent
,
1271 uint_t id
, int atype
)
1278 if ((error
= vdev_alloc(spa
, vdp
, nv
, parent
, id
, atype
)) != 0)
1281 if ((*vdp
)->vdev_ops
->vdev_op_leaf
)
1284 error
= nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1287 if (error
== ENOENT
)
1293 return (SET_ERROR(EINVAL
));
1296 for (c
= 0; c
< children
; c
++) {
1298 if ((error
= spa_config_parse(spa
, &vd
, child
[c
], *vdp
, c
,
1306 ASSERT(*vdp
!= NULL
);
1312 * Opposite of spa_load().
1315 spa_unload(spa_t
*spa
)
1319 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1324 spa_async_suspend(spa
);
1329 if (spa
->spa_sync_on
) {
1330 txg_sync_stop(spa
->spa_dsl_pool
);
1331 spa
->spa_sync_on
= B_FALSE
;
1335 * Wait for any outstanding async I/O to complete.
1337 if (spa
->spa_async_zio_root
!= NULL
) {
1338 for (i
= 0; i
< max_ncpus
; i
++)
1339 (void) zio_wait(spa
->spa_async_zio_root
[i
]);
1340 kmem_free(spa
->spa_async_zio_root
, max_ncpus
* sizeof (void *));
1341 spa
->spa_async_zio_root
= NULL
;
1344 bpobj_close(&spa
->spa_deferred_bpobj
);
1346 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1351 if (spa
->spa_root_vdev
)
1352 vdev_free(spa
->spa_root_vdev
);
1353 ASSERT(spa
->spa_root_vdev
== NULL
);
1356 * Close the dsl pool.
1358 if (spa
->spa_dsl_pool
) {
1359 dsl_pool_close(spa
->spa_dsl_pool
);
1360 spa
->spa_dsl_pool
= NULL
;
1361 spa
->spa_meta_objset
= NULL
;
1367 * Drop and purge level 2 cache
1369 spa_l2cache_drop(spa
);
1371 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1372 vdev_free(spa
->spa_spares
.sav_vdevs
[i
]);
1373 if (spa
->spa_spares
.sav_vdevs
) {
1374 kmem_free(spa
->spa_spares
.sav_vdevs
,
1375 spa
->spa_spares
.sav_count
* sizeof (void *));
1376 spa
->spa_spares
.sav_vdevs
= NULL
;
1378 if (spa
->spa_spares
.sav_config
) {
1379 nvlist_free(spa
->spa_spares
.sav_config
);
1380 spa
->spa_spares
.sav_config
= NULL
;
1382 spa
->spa_spares
.sav_count
= 0;
1384 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
1385 vdev_clear_stats(spa
->spa_l2cache
.sav_vdevs
[i
]);
1386 vdev_free(spa
->spa_l2cache
.sav_vdevs
[i
]);
1388 if (spa
->spa_l2cache
.sav_vdevs
) {
1389 kmem_free(spa
->spa_l2cache
.sav_vdevs
,
1390 spa
->spa_l2cache
.sav_count
* sizeof (void *));
1391 spa
->spa_l2cache
.sav_vdevs
= NULL
;
1393 if (spa
->spa_l2cache
.sav_config
) {
1394 nvlist_free(spa
->spa_l2cache
.sav_config
);
1395 spa
->spa_l2cache
.sav_config
= NULL
;
1397 spa
->spa_l2cache
.sav_count
= 0;
1399 spa
->spa_async_suspended
= 0;
1401 if (spa
->spa_comment
!= NULL
) {
1402 spa_strfree(spa
->spa_comment
);
1403 spa
->spa_comment
= NULL
;
1406 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1410 * Load (or re-load) the current list of vdevs describing the active spares for
1411 * this pool. When this is called, we have some form of basic information in
1412 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1413 * then re-generate a more complete list including status information.
1416 spa_load_spares(spa_t
*spa
)
1423 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1426 * First, close and free any existing spare vdevs.
1428 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1429 vd
= spa
->spa_spares
.sav_vdevs
[i
];
1431 /* Undo the call to spa_activate() below */
1432 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1433 B_FALSE
)) != NULL
&& tvd
->vdev_isspare
)
1434 spa_spare_remove(tvd
);
1439 if (spa
->spa_spares
.sav_vdevs
)
1440 kmem_free(spa
->spa_spares
.sav_vdevs
,
1441 spa
->spa_spares
.sav_count
* sizeof (void *));
1443 if (spa
->spa_spares
.sav_config
== NULL
)
1446 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
1447 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
1449 spa
->spa_spares
.sav_count
= (int)nspares
;
1450 spa
->spa_spares
.sav_vdevs
= NULL
;
1456 * Construct the array of vdevs, opening them to get status in the
1457 * process. For each spare, there is potentially two different vdev_t
1458 * structures associated with it: one in the list of spares (used only
1459 * for basic validation purposes) and one in the active vdev
1460 * configuration (if it's spared in). During this phase we open and
1461 * validate each vdev on the spare list. If the vdev also exists in the
1462 * active configuration, then we also mark this vdev as an active spare.
1464 spa
->spa_spares
.sav_vdevs
= kmem_zalloc(nspares
* sizeof (void *),
1466 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1467 VERIFY(spa_config_parse(spa
, &vd
, spares
[i
], NULL
, 0,
1468 VDEV_ALLOC_SPARE
) == 0);
1471 spa
->spa_spares
.sav_vdevs
[i
] = vd
;
1473 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1474 B_FALSE
)) != NULL
) {
1475 if (!tvd
->vdev_isspare
)
1479 * We only mark the spare active if we were successfully
1480 * able to load the vdev. Otherwise, importing a pool
1481 * with a bad active spare would result in strange
1482 * behavior, because multiple pool would think the spare
1483 * is actively in use.
1485 * There is a vulnerability here to an equally bizarre
1486 * circumstance, where a dead active spare is later
1487 * brought back to life (onlined or otherwise). Given
1488 * the rarity of this scenario, and the extra complexity
1489 * it adds, we ignore the possibility.
1491 if (!vdev_is_dead(tvd
))
1492 spa_spare_activate(tvd
);
1496 vd
->vdev_aux
= &spa
->spa_spares
;
1498 if (vdev_open(vd
) != 0)
1501 if (vdev_validate_aux(vd
) == 0)
1506 * Recompute the stashed list of spares, with status information
1509 VERIFY(nvlist_remove(spa
->spa_spares
.sav_config
, ZPOOL_CONFIG_SPARES
,
1510 DATA_TYPE_NVLIST_ARRAY
) == 0);
1512 spares
= kmem_alloc(spa
->spa_spares
.sav_count
* sizeof (void *),
1514 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1515 spares
[i
] = vdev_config_generate(spa
,
1516 spa
->spa_spares
.sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_SPARE
);
1517 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
1518 ZPOOL_CONFIG_SPARES
, spares
, spa
->spa_spares
.sav_count
) == 0);
1519 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1520 nvlist_free(spares
[i
]);
1521 kmem_free(spares
, spa
->spa_spares
.sav_count
* sizeof (void *));
1525 * Load (or re-load) the current list of vdevs describing the active l2cache for
1526 * this pool. When this is called, we have some form of basic information in
1527 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1528 * then re-generate a more complete list including status information.
1529 * Devices which are already active have their details maintained, and are
1533 spa_load_l2cache(spa_t
*spa
)
1537 int i
, j
, oldnvdevs
;
1539 vdev_t
*vd
, **oldvdevs
, **newvdevs
;
1540 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
1542 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1544 oldvdevs
= sav
->sav_vdevs
;
1545 oldnvdevs
= sav
->sav_count
;
1546 sav
->sav_vdevs
= NULL
;
1549 if (sav
->sav_config
== NULL
) {
1555 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
,
1556 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
1557 newvdevs
= kmem_alloc(nl2cache
* sizeof (void *), KM_SLEEP
);
1560 * Process new nvlist of vdevs.
1562 for (i
= 0; i
< nl2cache
; i
++) {
1563 VERIFY(nvlist_lookup_uint64(l2cache
[i
], ZPOOL_CONFIG_GUID
,
1567 for (j
= 0; j
< oldnvdevs
; j
++) {
1569 if (vd
!= NULL
&& guid
== vd
->vdev_guid
) {
1571 * Retain previous vdev for add/remove ops.
1579 if (newvdevs
[i
] == NULL
) {
1583 VERIFY(spa_config_parse(spa
, &vd
, l2cache
[i
], NULL
, 0,
1584 VDEV_ALLOC_L2CACHE
) == 0);
1589 * Commit this vdev as an l2cache device,
1590 * even if it fails to open.
1592 spa_l2cache_add(vd
);
1597 spa_l2cache_activate(vd
);
1599 if (vdev_open(vd
) != 0)
1602 (void) vdev_validate_aux(vd
);
1604 if (!vdev_is_dead(vd
))
1605 l2arc_add_vdev(spa
, vd
);
1609 sav
->sav_vdevs
= newvdevs
;
1610 sav
->sav_count
= (int)nl2cache
;
1613 * Recompute the stashed list of l2cache devices, with status
1614 * information this time.
1616 VERIFY(nvlist_remove(sav
->sav_config
, ZPOOL_CONFIG_L2CACHE
,
1617 DATA_TYPE_NVLIST_ARRAY
) == 0);
1619 l2cache
= kmem_alloc(sav
->sav_count
* sizeof (void *), KM_SLEEP
);
1620 for (i
= 0; i
< sav
->sav_count
; i
++)
1621 l2cache
[i
] = vdev_config_generate(spa
,
1622 sav
->sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_L2CACHE
);
1623 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
,
1624 ZPOOL_CONFIG_L2CACHE
, l2cache
, sav
->sav_count
) == 0);
1628 * Purge vdevs that were dropped
1630 for (i
= 0; i
< oldnvdevs
; i
++) {
1635 ASSERT(vd
->vdev_isl2cache
);
1637 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
1638 pool
!= 0ULL && l2arc_vdev_present(vd
))
1639 l2arc_remove_vdev(vd
);
1640 vdev_clear_stats(vd
);
1646 kmem_free(oldvdevs
, oldnvdevs
* sizeof (void *));
1648 for (i
= 0; i
< sav
->sav_count
; i
++)
1649 nvlist_free(l2cache
[i
]);
1651 kmem_free(l2cache
, sav
->sav_count
* sizeof (void *));
1655 load_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
**value
)
1658 char *packed
= NULL
;
1663 error
= dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
);
1667 nvsize
= *(uint64_t *)db
->db_data
;
1668 dmu_buf_rele(db
, FTAG
);
1670 packed
= vmem_alloc(nvsize
, KM_SLEEP
);
1671 error
= dmu_read(spa
->spa_meta_objset
, obj
, 0, nvsize
, packed
,
1674 error
= nvlist_unpack(packed
, nvsize
, value
, 0);
1675 vmem_free(packed
, nvsize
);
1681 * Checks to see if the given vdev could not be opened, in which case we post a
1682 * sysevent to notify the autoreplace code that the device has been removed.
1685 spa_check_removed(vdev_t
*vd
)
1689 for (c
= 0; c
< vd
->vdev_children
; c
++)
1690 spa_check_removed(vd
->vdev_child
[c
]);
1692 if (vd
->vdev_ops
->vdev_op_leaf
&& vdev_is_dead(vd
) &&
1694 zfs_post_autoreplace(vd
->vdev_spa
, vd
);
1695 spa_event_notify(vd
->vdev_spa
, vd
, ESC_ZFS_VDEV_CHECK
);
1700 spa_config_valid_zaps(vdev_t
*vd
, vdev_t
*mvd
)
1704 ASSERT3U(vd
->vdev_children
, ==, mvd
->vdev_children
);
1706 vd
->vdev_top_zap
= mvd
->vdev_top_zap
;
1707 vd
->vdev_leaf_zap
= mvd
->vdev_leaf_zap
;
1709 for (i
= 0; i
< vd
->vdev_children
; i
++) {
1710 spa_config_valid_zaps(vd
->vdev_child
[i
], mvd
->vdev_child
[i
]);
1715 * Validate the current config against the MOS config
1718 spa_config_valid(spa_t
*spa
, nvlist_t
*config
)
1720 vdev_t
*mrvd
, *rvd
= spa
->spa_root_vdev
;
1724 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nv
) == 0);
1726 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1727 VERIFY(spa_config_parse(spa
, &mrvd
, nv
, NULL
, 0, VDEV_ALLOC_LOAD
) == 0);
1729 ASSERT3U(rvd
->vdev_children
, ==, mrvd
->vdev_children
);
1732 * If we're doing a normal import, then build up any additional
1733 * diagnostic information about missing devices in this config.
1734 * We'll pass this up to the user for further processing.
1736 if (!(spa
->spa_import_flags
& ZFS_IMPORT_MISSING_LOG
)) {
1737 nvlist_t
**child
, *nv
;
1740 child
= kmem_alloc(rvd
->vdev_children
* sizeof (nvlist_t
*),
1742 VERIFY(nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
1744 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1745 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1746 vdev_t
*mtvd
= mrvd
->vdev_child
[c
];
1748 if (tvd
->vdev_ops
== &vdev_missing_ops
&&
1749 mtvd
->vdev_ops
!= &vdev_missing_ops
&&
1751 child
[idx
++] = vdev_config_generate(spa
, mtvd
,
1756 VERIFY(nvlist_add_nvlist_array(nv
,
1757 ZPOOL_CONFIG_CHILDREN
, child
, idx
) == 0);
1758 VERIFY(nvlist_add_nvlist(spa
->spa_load_info
,
1759 ZPOOL_CONFIG_MISSING_DEVICES
, nv
) == 0);
1761 for (i
= 0; i
< idx
; i
++)
1762 nvlist_free(child
[i
]);
1765 kmem_free(child
, rvd
->vdev_children
* sizeof (char **));
1769 * Compare the root vdev tree with the information we have
1770 * from the MOS config (mrvd). Check each top-level vdev
1771 * with the corresponding MOS config top-level (mtvd).
1773 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1774 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1775 vdev_t
*mtvd
= mrvd
->vdev_child
[c
];
1778 * Resolve any "missing" vdevs in the current configuration.
1779 * If we find that the MOS config has more accurate information
1780 * about the top-level vdev then use that vdev instead.
1782 if (tvd
->vdev_ops
== &vdev_missing_ops
&&
1783 mtvd
->vdev_ops
!= &vdev_missing_ops
) {
1785 if (!(spa
->spa_import_flags
& ZFS_IMPORT_MISSING_LOG
))
1789 * Device specific actions.
1791 if (mtvd
->vdev_islog
) {
1792 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
1795 * XXX - once we have 'readonly' pool
1796 * support we should be able to handle
1797 * missing data devices by transitioning
1798 * the pool to readonly.
1804 * Swap the missing vdev with the data we were
1805 * able to obtain from the MOS config.
1807 vdev_remove_child(rvd
, tvd
);
1808 vdev_remove_child(mrvd
, mtvd
);
1810 vdev_add_child(rvd
, mtvd
);
1811 vdev_add_child(mrvd
, tvd
);
1813 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1815 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1819 if (mtvd
->vdev_islog
) {
1821 * Load the slog device's state from the MOS
1822 * config since it's possible that the label
1823 * does not contain the most up-to-date
1826 vdev_load_log_state(tvd
, mtvd
);
1831 * Per-vdev ZAP info is stored exclusively in the MOS.
1833 spa_config_valid_zaps(tvd
, mtvd
);
1838 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1841 * Ensure we were able to validate the config.
1843 return (rvd
->vdev_guid_sum
== spa
->spa_uberblock
.ub_guid_sum
);
1847 * Check for missing log devices
1850 spa_check_logs(spa_t
*spa
)
1852 boolean_t rv
= B_FALSE
;
1853 dsl_pool_t
*dp
= spa_get_dsl(spa
);
1855 switch (spa
->spa_log_state
) {
1858 case SPA_LOG_MISSING
:
1859 /* need to recheck in case slog has been restored */
1860 case SPA_LOG_UNKNOWN
:
1861 rv
= (dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
1862 zil_check_log_chain
, NULL
, DS_FIND_CHILDREN
) != 0);
1864 spa_set_log_state(spa
, SPA_LOG_MISSING
);
1871 spa_passivate_log(spa_t
*spa
)
1873 vdev_t
*rvd
= spa
->spa_root_vdev
;
1874 boolean_t slog_found
= B_FALSE
;
1877 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
1879 if (!spa_has_slogs(spa
))
1882 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1883 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1884 metaslab_group_t
*mg
= tvd
->vdev_mg
;
1886 if (tvd
->vdev_islog
) {
1887 metaslab_group_passivate(mg
);
1888 slog_found
= B_TRUE
;
1892 return (slog_found
);
1896 spa_activate_log(spa_t
*spa
)
1898 vdev_t
*rvd
= spa
->spa_root_vdev
;
1901 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
1903 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1904 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1905 metaslab_group_t
*mg
= tvd
->vdev_mg
;
1907 if (tvd
->vdev_islog
)
1908 metaslab_group_activate(mg
);
1913 spa_offline_log(spa_t
*spa
)
1917 error
= dmu_objset_find(spa_name(spa
), zil_vdev_offline
,
1918 NULL
, DS_FIND_CHILDREN
);
1921 * We successfully offlined the log device, sync out the
1922 * current txg so that the "stubby" block can be removed
1925 txg_wait_synced(spa
->spa_dsl_pool
, 0);
1931 spa_aux_check_removed(spa_aux_vdev_t
*sav
)
1935 for (i
= 0; i
< sav
->sav_count
; i
++)
1936 spa_check_removed(sav
->sav_vdevs
[i
]);
1940 spa_claim_notify(zio_t
*zio
)
1942 spa_t
*spa
= zio
->io_spa
;
1947 mutex_enter(&spa
->spa_props_lock
); /* any mutex will do */
1948 if (spa
->spa_claim_max_txg
< zio
->io_bp
->blk_birth
)
1949 spa
->spa_claim_max_txg
= zio
->io_bp
->blk_birth
;
1950 mutex_exit(&spa
->spa_props_lock
);
1953 typedef struct spa_load_error
{
1954 uint64_t sle_meta_count
;
1955 uint64_t sle_data_count
;
1959 spa_load_verify_done(zio_t
*zio
)
1961 blkptr_t
*bp
= zio
->io_bp
;
1962 spa_load_error_t
*sle
= zio
->io_private
;
1963 dmu_object_type_t type
= BP_GET_TYPE(bp
);
1964 int error
= zio
->io_error
;
1965 spa_t
*spa
= zio
->io_spa
;
1968 if ((BP_GET_LEVEL(bp
) != 0 || DMU_OT_IS_METADATA(type
)) &&
1969 type
!= DMU_OT_INTENT_LOG
)
1970 atomic_inc_64(&sle
->sle_meta_count
);
1972 atomic_inc_64(&sle
->sle_data_count
);
1974 zio_data_buf_free(zio
->io_data
, zio
->io_size
);
1976 mutex_enter(&spa
->spa_scrub_lock
);
1977 spa
->spa_scrub_inflight
--;
1978 cv_broadcast(&spa
->spa_scrub_io_cv
);
1979 mutex_exit(&spa
->spa_scrub_lock
);
1983 * Maximum number of concurrent scrub i/os to create while verifying
1984 * a pool while importing it.
1986 int spa_load_verify_maxinflight
= 10000;
1987 int spa_load_verify_metadata
= B_TRUE
;
1988 int spa_load_verify_data
= B_TRUE
;
1992 spa_load_verify_cb(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
1993 const zbookmark_phys_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
1999 if (bp
== NULL
|| BP_IS_HOLE(bp
) || BP_IS_EMBEDDED(bp
))
2002 * Note: normally this routine will not be called if
2003 * spa_load_verify_metadata is not set. However, it may be useful
2004 * to manually set the flag after the traversal has begun.
2006 if (!spa_load_verify_metadata
)
2008 if (BP_GET_BUFC_TYPE(bp
) == ARC_BUFC_DATA
&& !spa_load_verify_data
)
2012 size
= BP_GET_PSIZE(bp
);
2013 data
= zio_data_buf_alloc(size
);
2015 mutex_enter(&spa
->spa_scrub_lock
);
2016 while (spa
->spa_scrub_inflight
>= spa_load_verify_maxinflight
)
2017 cv_wait(&spa
->spa_scrub_io_cv
, &spa
->spa_scrub_lock
);
2018 spa
->spa_scrub_inflight
++;
2019 mutex_exit(&spa
->spa_scrub_lock
);
2021 zio_nowait(zio_read(rio
, spa
, bp
, data
, size
,
2022 spa_load_verify_done
, rio
->io_private
, ZIO_PRIORITY_SCRUB
,
2023 ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_CANFAIL
|
2024 ZIO_FLAG_SCRUB
| ZIO_FLAG_RAW
, zb
));
2030 verify_dataset_name_len(dsl_pool_t
*dp
, dsl_dataset_t
*ds
, void *arg
)
2032 if (dsl_dataset_namelen(ds
) >= ZFS_MAX_DATASET_NAME_LEN
)
2033 return (SET_ERROR(ENAMETOOLONG
));
2039 spa_load_verify(spa_t
*spa
)
2042 spa_load_error_t sle
= { 0 };
2043 zpool_rewind_policy_t policy
;
2044 boolean_t verify_ok
= B_FALSE
;
2047 zpool_get_rewind_policy(spa
->spa_config
, &policy
);
2049 if (policy
.zrp_request
& ZPOOL_NEVER_REWIND
)
2052 dsl_pool_config_enter(spa
->spa_dsl_pool
, FTAG
);
2053 error
= dmu_objset_find_dp(spa
->spa_dsl_pool
,
2054 spa
->spa_dsl_pool
->dp_root_dir_obj
, verify_dataset_name_len
, NULL
,
2056 dsl_pool_config_exit(spa
->spa_dsl_pool
, FTAG
);
2060 rio
= zio_root(spa
, NULL
, &sle
,
2061 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
);
2063 if (spa_load_verify_metadata
) {
2064 error
= traverse_pool(spa
, spa
->spa_verify_min_txg
,
2065 TRAVERSE_PRE
| TRAVERSE_PREFETCH_METADATA
,
2066 spa_load_verify_cb
, rio
);
2069 (void) zio_wait(rio
);
2071 spa
->spa_load_meta_errors
= sle
.sle_meta_count
;
2072 spa
->spa_load_data_errors
= sle
.sle_data_count
;
2074 if (!error
&& sle
.sle_meta_count
<= policy
.zrp_maxmeta
&&
2075 sle
.sle_data_count
<= policy
.zrp_maxdata
) {
2079 spa
->spa_load_txg
= spa
->spa_uberblock
.ub_txg
;
2080 spa
->spa_load_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
2082 loss
= spa
->spa_last_ubsync_txg_ts
- spa
->spa_load_txg_ts
;
2083 VERIFY(nvlist_add_uint64(spa
->spa_load_info
,
2084 ZPOOL_CONFIG_LOAD_TIME
, spa
->spa_load_txg_ts
) == 0);
2085 VERIFY(nvlist_add_int64(spa
->spa_load_info
,
2086 ZPOOL_CONFIG_REWIND_TIME
, loss
) == 0);
2087 VERIFY(nvlist_add_uint64(spa
->spa_load_info
,
2088 ZPOOL_CONFIG_LOAD_DATA_ERRORS
, sle
.sle_data_count
) == 0);
2090 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
;
2094 if (error
!= ENXIO
&& error
!= EIO
)
2095 error
= SET_ERROR(EIO
);
2099 return (verify_ok
? 0 : EIO
);
2103 * Find a value in the pool props object.
2106 spa_prop_find(spa_t
*spa
, zpool_prop_t prop
, uint64_t *val
)
2108 (void) zap_lookup(spa
->spa_meta_objset
, spa
->spa_pool_props_object
,
2109 zpool_prop_to_name(prop
), sizeof (uint64_t), 1, val
);
2113 * Find a value in the pool directory object.
2116 spa_dir_prop(spa_t
*spa
, const char *name
, uint64_t *val
)
2118 return (zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
2119 name
, sizeof (uint64_t), 1, val
));
2123 spa_vdev_err(vdev_t
*vdev
, vdev_aux_t aux
, int err
)
2125 vdev_set_state(vdev
, B_TRUE
, VDEV_STATE_CANT_OPEN
, aux
);
2130 * Fix up config after a partly-completed split. This is done with the
2131 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
2132 * pool have that entry in their config, but only the splitting one contains
2133 * a list of all the guids of the vdevs that are being split off.
2135 * This function determines what to do with that list: either rejoin
2136 * all the disks to the pool, or complete the splitting process. To attempt
2137 * the rejoin, each disk that is offlined is marked online again, and
2138 * we do a reopen() call. If the vdev label for every disk that was
2139 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
2140 * then we call vdev_split() on each disk, and complete the split.
2142 * Otherwise we leave the config alone, with all the vdevs in place in
2143 * the original pool.
2146 spa_try_repair(spa_t
*spa
, nvlist_t
*config
)
2153 boolean_t attempt_reopen
;
2155 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
, &nvl
) != 0)
2158 /* check that the config is complete */
2159 if (nvlist_lookup_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
2160 &glist
, &gcount
) != 0)
2163 vd
= kmem_zalloc(gcount
* sizeof (vdev_t
*), KM_SLEEP
);
2165 /* attempt to online all the vdevs & validate */
2166 attempt_reopen
= B_TRUE
;
2167 for (i
= 0; i
< gcount
; i
++) {
2168 if (glist
[i
] == 0) /* vdev is hole */
2171 vd
[i
] = spa_lookup_by_guid(spa
, glist
[i
], B_FALSE
);
2172 if (vd
[i
] == NULL
) {
2174 * Don't bother attempting to reopen the disks;
2175 * just do the split.
2177 attempt_reopen
= B_FALSE
;
2179 /* attempt to re-online it */
2180 vd
[i
]->vdev_offline
= B_FALSE
;
2184 if (attempt_reopen
) {
2185 vdev_reopen(spa
->spa_root_vdev
);
2187 /* check each device to see what state it's in */
2188 for (extracted
= 0, i
= 0; i
< gcount
; i
++) {
2189 if (vd
[i
] != NULL
&&
2190 vd
[i
]->vdev_stat
.vs_aux
!= VDEV_AUX_SPLIT_POOL
)
2197 * If every disk has been moved to the new pool, or if we never
2198 * even attempted to look at them, then we split them off for
2201 if (!attempt_reopen
|| gcount
== extracted
) {
2202 for (i
= 0; i
< gcount
; i
++)
2205 vdev_reopen(spa
->spa_root_vdev
);
2208 kmem_free(vd
, gcount
* sizeof (vdev_t
*));
2212 spa_load(spa_t
*spa
, spa_load_state_t state
, spa_import_type_t type
,
2213 boolean_t mosconfig
)
2215 nvlist_t
*config
= spa
->spa_config
;
2216 char *ereport
= FM_EREPORT_ZFS_POOL
;
2222 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
, &pool_guid
))
2223 return (SET_ERROR(EINVAL
));
2225 ASSERT(spa
->spa_comment
== NULL
);
2226 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
2227 spa
->spa_comment
= spa_strdup(comment
);
2230 * Versioning wasn't explicitly added to the label until later, so if
2231 * it's not present treat it as the initial version.
2233 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
2234 &spa
->spa_ubsync
.ub_version
) != 0)
2235 spa
->spa_ubsync
.ub_version
= SPA_VERSION_INITIAL
;
2237 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
2238 &spa
->spa_config_txg
);
2240 if ((state
== SPA_LOAD_IMPORT
|| state
== SPA_LOAD_TRYIMPORT
) &&
2241 spa_guid_exists(pool_guid
, 0)) {
2242 error
= SET_ERROR(EEXIST
);
2244 spa
->spa_config_guid
= pool_guid
;
2246 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
,
2248 VERIFY(nvlist_dup(nvl
, &spa
->spa_config_splitting
,
2252 nvlist_free(spa
->spa_load_info
);
2253 spa
->spa_load_info
= fnvlist_alloc();
2255 gethrestime(&spa
->spa_loaded_ts
);
2256 error
= spa_load_impl(spa
, pool_guid
, config
, state
, type
,
2257 mosconfig
, &ereport
);
2261 * Don't count references from objsets that are already closed
2262 * and are making their way through the eviction process.
2264 spa_evicting_os_wait(spa
);
2265 spa
->spa_minref
= refcount_count(&spa
->spa_refcount
);
2267 if (error
!= EEXIST
) {
2268 spa
->spa_loaded_ts
.tv_sec
= 0;
2269 spa
->spa_loaded_ts
.tv_nsec
= 0;
2271 if (error
!= EBADF
) {
2272 zfs_ereport_post(ereport
, spa
, NULL
, NULL
, 0, 0);
2275 spa
->spa_load_state
= error
? SPA_LOAD_ERROR
: SPA_LOAD_NONE
;
2283 * Count the number of per-vdev ZAPs associated with all of the vdevs in the
2284 * vdev tree rooted in the given vd, and ensure that each ZAP is present in the
2285 * spa's per-vdev ZAP list.
2288 vdev_count_verify_zaps(vdev_t
*vd
)
2290 spa_t
*spa
= vd
->vdev_spa
;
2294 if (vd
->vdev_top_zap
!= 0) {
2296 ASSERT0(zap_lookup_int(spa
->spa_meta_objset
,
2297 spa
->spa_all_vdev_zaps
, vd
->vdev_top_zap
));
2299 if (vd
->vdev_leaf_zap
!= 0) {
2301 ASSERT0(zap_lookup_int(spa
->spa_meta_objset
,
2302 spa
->spa_all_vdev_zaps
, vd
->vdev_leaf_zap
));
2305 for (i
= 0; i
< vd
->vdev_children
; i
++) {
2306 total
+= vdev_count_verify_zaps(vd
->vdev_child
[i
]);
2314 * Load an existing storage pool, using the pool's builtin spa_config as a
2315 * source of configuration information.
2317 __attribute__((always_inline
))
2319 spa_load_impl(spa_t
*spa
, uint64_t pool_guid
, nvlist_t
*config
,
2320 spa_load_state_t state
, spa_import_type_t type
, boolean_t mosconfig
,
2324 nvlist_t
*nvroot
= NULL
;
2327 uberblock_t
*ub
= &spa
->spa_uberblock
;
2328 uint64_t children
, config_cache_txg
= spa
->spa_config_txg
;
2329 int orig_mode
= spa
->spa_mode
;
2332 boolean_t missing_feat_write
= B_FALSE
;
2333 nvlist_t
*mos_config
;
2336 * If this is an untrusted config, access the pool in read-only mode.
2337 * This prevents things like resilvering recently removed devices.
2340 spa
->spa_mode
= FREAD
;
2342 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
2344 spa
->spa_load_state
= state
;
2346 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvroot
))
2347 return (SET_ERROR(EINVAL
));
2349 parse
= (type
== SPA_IMPORT_EXISTING
?
2350 VDEV_ALLOC_LOAD
: VDEV_ALLOC_SPLIT
);
2353 * Create "The Godfather" zio to hold all async IOs
2355 spa
->spa_async_zio_root
= kmem_alloc(max_ncpus
* sizeof (void *),
2357 for (i
= 0; i
< max_ncpus
; i
++) {
2358 spa
->spa_async_zio_root
[i
] = zio_root(spa
, NULL
, NULL
,
2359 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
2360 ZIO_FLAG_GODFATHER
);
2364 * Parse the configuration into a vdev tree. We explicitly set the
2365 * value that will be returned by spa_version() since parsing the
2366 * configuration requires knowing the version number.
2368 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2369 error
= spa_config_parse(spa
, &rvd
, nvroot
, NULL
, 0, parse
);
2370 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2375 ASSERT(spa
->spa_root_vdev
== rvd
);
2376 ASSERT3U(spa
->spa_min_ashift
, >=, SPA_MINBLOCKSHIFT
);
2377 ASSERT3U(spa
->spa_max_ashift
, <=, SPA_MAXBLOCKSHIFT
);
2379 if (type
!= SPA_IMPORT_ASSEMBLE
) {
2380 ASSERT(spa_guid(spa
) == pool_guid
);
2384 * Try to open all vdevs, loading each label in the process.
2386 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2387 error
= vdev_open(rvd
);
2388 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2393 * We need to validate the vdev labels against the configuration that
2394 * we have in hand, which is dependent on the setting of mosconfig. If
2395 * mosconfig is true then we're validating the vdev labels based on
2396 * that config. Otherwise, we're validating against the cached config
2397 * (zpool.cache) that was read when we loaded the zfs module, and then
2398 * later we will recursively call spa_load() and validate against
2401 * If we're assembling a new pool that's been split off from an
2402 * existing pool, the labels haven't yet been updated so we skip
2403 * validation for now.
2405 if (type
!= SPA_IMPORT_ASSEMBLE
) {
2406 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2407 error
= vdev_validate(rvd
, mosconfig
);
2408 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2413 if (rvd
->vdev_state
<= VDEV_STATE_CANT_OPEN
)
2414 return (SET_ERROR(ENXIO
));
2418 * Find the best uberblock.
2420 vdev_uberblock_load(rvd
, ub
, &label
);
2423 * If we weren't able to find a single valid uberblock, return failure.
2425 if (ub
->ub_txg
== 0) {
2427 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, ENXIO
));
2431 * If the pool has an unsupported version we can't open it.
2433 if (!SPA_VERSION_IS_SUPPORTED(ub
->ub_version
)) {
2435 return (spa_vdev_err(rvd
, VDEV_AUX_VERSION_NEWER
, ENOTSUP
));
2438 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
2442 * If we weren't able to find what's necessary for reading the
2443 * MOS in the label, return failure.
2445 if (label
== NULL
|| nvlist_lookup_nvlist(label
,
2446 ZPOOL_CONFIG_FEATURES_FOR_READ
, &features
) != 0) {
2448 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
2453 * Update our in-core representation with the definitive values
2456 nvlist_free(spa
->spa_label_features
);
2457 VERIFY(nvlist_dup(features
, &spa
->spa_label_features
, 0) == 0);
2463 * Look through entries in the label nvlist's features_for_read. If
2464 * there is a feature listed there which we don't understand then we
2465 * cannot open a pool.
2467 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
2468 nvlist_t
*unsup_feat
;
2471 VERIFY(nvlist_alloc(&unsup_feat
, NV_UNIQUE_NAME
, KM_SLEEP
) ==
2474 for (nvp
= nvlist_next_nvpair(spa
->spa_label_features
, NULL
);
2476 nvp
= nvlist_next_nvpair(spa
->spa_label_features
, nvp
)) {
2477 if (!zfeature_is_supported(nvpair_name(nvp
))) {
2478 VERIFY(nvlist_add_string(unsup_feat
,
2479 nvpair_name(nvp
), "") == 0);
2483 if (!nvlist_empty(unsup_feat
)) {
2484 VERIFY(nvlist_add_nvlist(spa
->spa_load_info
,
2485 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
) == 0);
2486 nvlist_free(unsup_feat
);
2487 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
2491 nvlist_free(unsup_feat
);
2495 * If the vdev guid sum doesn't match the uberblock, we have an
2496 * incomplete configuration. We first check to see if the pool
2497 * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN).
2498 * If it is, defer the vdev_guid_sum check till later so we
2499 * can handle missing vdevs.
2501 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VDEV_CHILDREN
,
2502 &children
) != 0 && mosconfig
&& type
!= SPA_IMPORT_ASSEMBLE
&&
2503 rvd
->vdev_guid_sum
!= ub
->ub_guid_sum
)
2504 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
, ENXIO
));
2506 if (type
!= SPA_IMPORT_ASSEMBLE
&& spa
->spa_config_splitting
) {
2507 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2508 spa_try_repair(spa
, config
);
2509 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2510 nvlist_free(spa
->spa_config_splitting
);
2511 spa
->spa_config_splitting
= NULL
;
2515 * Initialize internal SPA structures.
2517 spa
->spa_state
= POOL_STATE_ACTIVE
;
2518 spa
->spa_ubsync
= spa
->spa_uberblock
;
2519 spa
->spa_verify_min_txg
= spa
->spa_extreme_rewind
?
2520 TXG_INITIAL
- 1 : spa_last_synced_txg(spa
) - TXG_DEFER_SIZE
- 1;
2521 spa
->spa_first_txg
= spa
->spa_last_ubsync_txg
?
2522 spa
->spa_last_ubsync_txg
: spa_last_synced_txg(spa
) + 1;
2523 spa
->spa_claim_max_txg
= spa
->spa_first_txg
;
2524 spa
->spa_prev_software_version
= ub
->ub_software_version
;
2526 error
= dsl_pool_init(spa
, spa
->spa_first_txg
, &spa
->spa_dsl_pool
);
2528 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2529 spa
->spa_meta_objset
= spa
->spa_dsl_pool
->dp_meta_objset
;
2531 if (spa_dir_prop(spa
, DMU_POOL_CONFIG
, &spa
->spa_config_object
) != 0)
2532 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2534 if (spa_version(spa
) >= SPA_VERSION_FEATURES
) {
2535 boolean_t missing_feat_read
= B_FALSE
;
2536 nvlist_t
*unsup_feat
, *enabled_feat
;
2539 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_READ
,
2540 &spa
->spa_feat_for_read_obj
) != 0) {
2541 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2544 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_WRITE
,
2545 &spa
->spa_feat_for_write_obj
) != 0) {
2546 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2549 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_DESCRIPTIONS
,
2550 &spa
->spa_feat_desc_obj
) != 0) {
2551 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2554 enabled_feat
= fnvlist_alloc();
2555 unsup_feat
= fnvlist_alloc();
2557 if (!spa_features_check(spa
, B_FALSE
,
2558 unsup_feat
, enabled_feat
))
2559 missing_feat_read
= B_TRUE
;
2561 if (spa_writeable(spa
) || state
== SPA_LOAD_TRYIMPORT
) {
2562 if (!spa_features_check(spa
, B_TRUE
,
2563 unsup_feat
, enabled_feat
)) {
2564 missing_feat_write
= B_TRUE
;
2568 fnvlist_add_nvlist(spa
->spa_load_info
,
2569 ZPOOL_CONFIG_ENABLED_FEAT
, enabled_feat
);
2571 if (!nvlist_empty(unsup_feat
)) {
2572 fnvlist_add_nvlist(spa
->spa_load_info
,
2573 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
);
2576 fnvlist_free(enabled_feat
);
2577 fnvlist_free(unsup_feat
);
2579 if (!missing_feat_read
) {
2580 fnvlist_add_boolean(spa
->spa_load_info
,
2581 ZPOOL_CONFIG_CAN_RDONLY
);
2585 * If the state is SPA_LOAD_TRYIMPORT, our objective is
2586 * twofold: to determine whether the pool is available for
2587 * import in read-write mode and (if it is not) whether the
2588 * pool is available for import in read-only mode. If the pool
2589 * is available for import in read-write mode, it is displayed
2590 * as available in userland; if it is not available for import
2591 * in read-only mode, it is displayed as unavailable in
2592 * userland. If the pool is available for import in read-only
2593 * mode but not read-write mode, it is displayed as unavailable
2594 * in userland with a special note that the pool is actually
2595 * available for open in read-only mode.
2597 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
2598 * missing a feature for write, we must first determine whether
2599 * the pool can be opened read-only before returning to
2600 * userland in order to know whether to display the
2601 * abovementioned note.
2603 if (missing_feat_read
|| (missing_feat_write
&&
2604 spa_writeable(spa
))) {
2605 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
2610 * Load refcounts for ZFS features from disk into an in-memory
2611 * cache during SPA initialization.
2613 for (i
= 0; i
< SPA_FEATURES
; i
++) {
2616 error
= feature_get_refcount_from_disk(spa
,
2617 &spa_feature_table
[i
], &refcount
);
2619 spa
->spa_feat_refcount_cache
[i
] = refcount
;
2620 } else if (error
== ENOTSUP
) {
2621 spa
->spa_feat_refcount_cache
[i
] =
2622 SPA_FEATURE_DISABLED
;
2624 return (spa_vdev_err(rvd
,
2625 VDEV_AUX_CORRUPT_DATA
, EIO
));
2630 if (spa_feature_is_active(spa
, SPA_FEATURE_ENABLED_TXG
)) {
2631 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_ENABLED_TXG
,
2632 &spa
->spa_feat_enabled_txg_obj
) != 0)
2633 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2636 spa
->spa_is_initializing
= B_TRUE
;
2637 error
= dsl_pool_open(spa
->spa_dsl_pool
);
2638 spa
->spa_is_initializing
= B_FALSE
;
2640 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2644 nvlist_t
*policy
= NULL
, *nvconfig
;
2646 if (load_nvlist(spa
, spa
->spa_config_object
, &nvconfig
) != 0)
2647 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2649 if (!spa_is_root(spa
) && nvlist_lookup_uint64(nvconfig
,
2650 ZPOOL_CONFIG_HOSTID
, &hostid
) == 0) {
2652 unsigned long myhostid
= 0;
2654 VERIFY(nvlist_lookup_string(nvconfig
,
2655 ZPOOL_CONFIG_HOSTNAME
, &hostname
) == 0);
2658 myhostid
= zone_get_hostid(NULL
);
2661 * We're emulating the system's hostid in userland, so
2662 * we can't use zone_get_hostid().
2664 (void) ddi_strtoul(hw_serial
, NULL
, 10, &myhostid
);
2665 #endif /* _KERNEL */
2666 if (hostid
!= 0 && myhostid
!= 0 &&
2667 hostid
!= myhostid
) {
2668 nvlist_free(nvconfig
);
2669 cmn_err(CE_WARN
, "pool '%s' could not be "
2670 "loaded as it was last accessed by another "
2671 "system (host: %s hostid: 0x%lx). See: "
2672 "http://zfsonlinux.org/msg/ZFS-8000-EY",
2673 spa_name(spa
), hostname
,
2674 (unsigned long)hostid
);
2675 return (SET_ERROR(EBADF
));
2678 if (nvlist_lookup_nvlist(spa
->spa_config
,
2679 ZPOOL_REWIND_POLICY
, &policy
) == 0)
2680 VERIFY(nvlist_add_nvlist(nvconfig
,
2681 ZPOOL_REWIND_POLICY
, policy
) == 0);
2683 spa_config_set(spa
, nvconfig
);
2685 spa_deactivate(spa
);
2686 spa_activate(spa
, orig_mode
);
2688 return (spa_load(spa
, state
, SPA_IMPORT_EXISTING
, B_TRUE
));
2691 /* Grab the checksum salt from the MOS. */
2692 error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
2693 DMU_POOL_CHECKSUM_SALT
, 1,
2694 sizeof (spa
->spa_cksum_salt
.zcs_bytes
),
2695 spa
->spa_cksum_salt
.zcs_bytes
);
2696 if (error
== ENOENT
) {
2697 /* Generate a new salt for subsequent use */
2698 (void) random_get_pseudo_bytes(spa
->spa_cksum_salt
.zcs_bytes
,
2699 sizeof (spa
->spa_cksum_salt
.zcs_bytes
));
2700 } else if (error
!= 0) {
2701 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2704 if (spa_dir_prop(spa
, DMU_POOL_SYNC_BPOBJ
, &obj
) != 0)
2705 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2706 error
= bpobj_open(&spa
->spa_deferred_bpobj
, spa
->spa_meta_objset
, obj
);
2708 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2711 * Load the bit that tells us to use the new accounting function
2712 * (raid-z deflation). If we have an older pool, this will not
2715 error
= spa_dir_prop(spa
, DMU_POOL_DEFLATE
, &spa
->spa_deflate
);
2716 if (error
!= 0 && error
!= ENOENT
)
2717 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2719 error
= spa_dir_prop(spa
, DMU_POOL_CREATION_VERSION
,
2720 &spa
->spa_creation_version
);
2721 if (error
!= 0 && error
!= ENOENT
)
2722 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2725 * Load the persistent error log. If we have an older pool, this will
2728 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_LAST
, &spa
->spa_errlog_last
);
2729 if (error
!= 0 && error
!= ENOENT
)
2730 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2732 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_SCRUB
,
2733 &spa
->spa_errlog_scrub
);
2734 if (error
!= 0 && error
!= ENOENT
)
2735 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2738 * Load the history object. If we have an older pool, this
2739 * will not be present.
2741 error
= spa_dir_prop(spa
, DMU_POOL_HISTORY
, &spa
->spa_history
);
2742 if (error
!= 0 && error
!= ENOENT
)
2743 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2746 * Load the per-vdev ZAP map. If we have an older pool, this will not
2747 * be present; in this case, defer its creation to a later time to
2748 * avoid dirtying the MOS this early / out of sync context. See
2749 * spa_sync_config_object.
2752 /* The sentinel is only available in the MOS config. */
2753 if (load_nvlist(spa
, spa
->spa_config_object
, &mos_config
) != 0)
2754 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2756 error
= spa_dir_prop(spa
, DMU_POOL_VDEV_ZAP_MAP
,
2757 &spa
->spa_all_vdev_zaps
);
2759 if (error
!= ENOENT
&& error
!= 0) {
2760 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2761 } else if (error
== 0 && !nvlist_exists(mos_config
,
2762 ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
)) {
2764 * An older version of ZFS overwrote the sentinel value, so
2765 * we have orphaned per-vdev ZAPs in the MOS. Defer their
2766 * destruction to later; see spa_sync_config_object.
2768 spa
->spa_avz_action
= AVZ_ACTION_DESTROY
;
2770 * We're assuming that no vdevs have had their ZAPs created
2771 * before this. Better be sure of it.
2773 ASSERT0(vdev_count_verify_zaps(spa
->spa_root_vdev
));
2775 nvlist_free(mos_config
);
2778 * If we're assembling the pool from the split-off vdevs of
2779 * an existing pool, we don't want to attach the spares & cache
2784 * Load any hot spares for this pool.
2786 error
= spa_dir_prop(spa
, DMU_POOL_SPARES
, &spa
->spa_spares
.sav_object
);
2787 if (error
!= 0 && error
!= ENOENT
)
2788 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2789 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
2790 ASSERT(spa_version(spa
) >= SPA_VERSION_SPARES
);
2791 if (load_nvlist(spa
, spa
->spa_spares
.sav_object
,
2792 &spa
->spa_spares
.sav_config
) != 0)
2793 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2795 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2796 spa_load_spares(spa
);
2797 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2798 } else if (error
== 0) {
2799 spa
->spa_spares
.sav_sync
= B_TRUE
;
2803 * Load any level 2 ARC devices for this pool.
2805 error
= spa_dir_prop(spa
, DMU_POOL_L2CACHE
,
2806 &spa
->spa_l2cache
.sav_object
);
2807 if (error
!= 0 && error
!= ENOENT
)
2808 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2809 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
2810 ASSERT(spa_version(spa
) >= SPA_VERSION_L2CACHE
);
2811 if (load_nvlist(spa
, spa
->spa_l2cache
.sav_object
,
2812 &spa
->spa_l2cache
.sav_config
) != 0)
2813 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2815 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2816 spa_load_l2cache(spa
);
2817 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2818 } else if (error
== 0) {
2819 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
2822 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
2824 error
= spa_dir_prop(spa
, DMU_POOL_PROPS
, &spa
->spa_pool_props_object
);
2825 if (error
&& error
!= ENOENT
)
2826 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2829 uint64_t autoreplace
= 0;
2831 spa_prop_find(spa
, ZPOOL_PROP_BOOTFS
, &spa
->spa_bootfs
);
2832 spa_prop_find(spa
, ZPOOL_PROP_AUTOREPLACE
, &autoreplace
);
2833 spa_prop_find(spa
, ZPOOL_PROP_DELEGATION
, &spa
->spa_delegation
);
2834 spa_prop_find(spa
, ZPOOL_PROP_FAILUREMODE
, &spa
->spa_failmode
);
2835 spa_prop_find(spa
, ZPOOL_PROP_AUTOEXPAND
, &spa
->spa_autoexpand
);
2836 spa_prop_find(spa
, ZPOOL_PROP_DEDUPDITTO
,
2837 &spa
->spa_dedup_ditto
);
2839 spa
->spa_autoreplace
= (autoreplace
!= 0);
2843 * If the 'autoreplace' property is set, then post a resource notifying
2844 * the ZFS DE that it should not issue any faults for unopenable
2845 * devices. We also iterate over the vdevs, and post a sysevent for any
2846 * unopenable vdevs so that the normal autoreplace handler can take
2849 if (spa
->spa_autoreplace
&& state
!= SPA_LOAD_TRYIMPORT
) {
2850 spa_check_removed(spa
->spa_root_vdev
);
2852 * For the import case, this is done in spa_import(), because
2853 * at this point we're using the spare definitions from
2854 * the MOS config, not necessarily from the userland config.
2856 if (state
!= SPA_LOAD_IMPORT
) {
2857 spa_aux_check_removed(&spa
->spa_spares
);
2858 spa_aux_check_removed(&spa
->spa_l2cache
);
2863 * Load the vdev state for all toplevel vdevs.
2868 * Propagate the leaf DTLs we just loaded all the way up the tree.
2870 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2871 vdev_dtl_reassess(rvd
, 0, 0, B_FALSE
);
2872 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2875 * Load the DDTs (dedup tables).
2877 error
= ddt_load(spa
);
2879 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2881 spa_update_dspace(spa
);
2884 * Validate the config, using the MOS config to fill in any
2885 * information which might be missing. If we fail to validate
2886 * the config then declare the pool unfit for use. If we're
2887 * assembling a pool from a split, the log is not transferred
2890 if (type
!= SPA_IMPORT_ASSEMBLE
) {
2893 if (load_nvlist(spa
, spa
->spa_config_object
, &nvconfig
) != 0)
2894 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2896 if (!spa_config_valid(spa
, nvconfig
)) {
2897 nvlist_free(nvconfig
);
2898 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
,
2901 nvlist_free(nvconfig
);
2904 * Now that we've validated the config, check the state of the
2905 * root vdev. If it can't be opened, it indicates one or
2906 * more toplevel vdevs are faulted.
2908 if (rvd
->vdev_state
<= VDEV_STATE_CANT_OPEN
)
2909 return (SET_ERROR(ENXIO
));
2911 if (spa_writeable(spa
) && spa_check_logs(spa
)) {
2912 *ereport
= FM_EREPORT_ZFS_LOG_REPLAY
;
2913 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_LOG
, ENXIO
));
2917 if (missing_feat_write
) {
2918 ASSERT(state
== SPA_LOAD_TRYIMPORT
);
2921 * At this point, we know that we can open the pool in
2922 * read-only mode but not read-write mode. We now have enough
2923 * information and can return to userland.
2925 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
, ENOTSUP
));
2929 * We've successfully opened the pool, verify that we're ready
2930 * to start pushing transactions.
2932 if (state
!= SPA_LOAD_TRYIMPORT
) {
2933 if ((error
= spa_load_verify(spa
)))
2934 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
2938 if (spa_writeable(spa
) && (state
== SPA_LOAD_RECOVER
||
2939 spa
->spa_load_max_txg
== UINT64_MAX
)) {
2941 int need_update
= B_FALSE
;
2942 dsl_pool_t
*dp
= spa_get_dsl(spa
);
2945 ASSERT(state
!= SPA_LOAD_TRYIMPORT
);
2948 * Claim log blocks that haven't been committed yet.
2949 * This must all happen in a single txg.
2950 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
2951 * invoked from zil_claim_log_block()'s i/o done callback.
2952 * Price of rollback is that we abandon the log.
2954 spa
->spa_claiming
= B_TRUE
;
2956 tx
= dmu_tx_create_assigned(dp
, spa_first_txg(spa
));
2957 (void) dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
2958 zil_claim
, tx
, DS_FIND_CHILDREN
);
2961 spa
->spa_claiming
= B_FALSE
;
2963 spa_set_log_state(spa
, SPA_LOG_GOOD
);
2964 spa
->spa_sync_on
= B_TRUE
;
2965 txg_sync_start(spa
->spa_dsl_pool
);
2968 * Wait for all claims to sync. We sync up to the highest
2969 * claimed log block birth time so that claimed log blocks
2970 * don't appear to be from the future. spa_claim_max_txg
2971 * will have been set for us by either zil_check_log_chain()
2972 * (invoked from spa_check_logs()) or zil_claim() above.
2974 txg_wait_synced(spa
->spa_dsl_pool
, spa
->spa_claim_max_txg
);
2977 * If the config cache is stale, or we have uninitialized
2978 * metaslabs (see spa_vdev_add()), then update the config.
2980 * If this is a verbatim import, trust the current
2981 * in-core spa_config and update the disk labels.
2983 if (config_cache_txg
!= spa
->spa_config_txg
||
2984 state
== SPA_LOAD_IMPORT
||
2985 state
== SPA_LOAD_RECOVER
||
2986 (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
))
2987 need_update
= B_TRUE
;
2989 for (c
= 0; c
< rvd
->vdev_children
; c
++)
2990 if (rvd
->vdev_child
[c
]->vdev_ms_array
== 0)
2991 need_update
= B_TRUE
;
2994 * Update the config cache asychronously in case we're the
2995 * root pool, in which case the config cache isn't writable yet.
2998 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
3001 * Check all DTLs to see if anything needs resilvering.
3003 if (!dsl_scan_resilvering(spa
->spa_dsl_pool
) &&
3004 vdev_resilver_needed(rvd
, NULL
, NULL
))
3005 spa_async_request(spa
, SPA_ASYNC_RESILVER
);
3008 * Log the fact that we booted up (so that we can detect if
3009 * we rebooted in the middle of an operation).
3011 spa_history_log_version(spa
, "open");
3014 * Delete any inconsistent datasets.
3016 (void) dmu_objset_find(spa_name(spa
),
3017 dsl_destroy_inconsistent
, NULL
, DS_FIND_CHILDREN
);
3020 * Clean up any stale temporary dataset userrefs.
3022 dsl_pool_clean_tmp_userrefs(spa
->spa_dsl_pool
);
3029 spa_load_retry(spa_t
*spa
, spa_load_state_t state
, int mosconfig
)
3031 int mode
= spa
->spa_mode
;
3034 spa_deactivate(spa
);
3036 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
- 1;
3038 spa_activate(spa
, mode
);
3039 spa_async_suspend(spa
);
3041 return (spa_load(spa
, state
, SPA_IMPORT_EXISTING
, mosconfig
));
3045 * If spa_load() fails this function will try loading prior txg's. If
3046 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
3047 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
3048 * function will not rewind the pool and will return the same error as
3052 spa_load_best(spa_t
*spa
, spa_load_state_t state
, int mosconfig
,
3053 uint64_t max_request
, int rewind_flags
)
3055 nvlist_t
*loadinfo
= NULL
;
3056 nvlist_t
*config
= NULL
;
3057 int load_error
, rewind_error
;
3058 uint64_t safe_rewind_txg
;
3061 if (spa
->spa_load_txg
&& state
== SPA_LOAD_RECOVER
) {
3062 spa
->spa_load_max_txg
= spa
->spa_load_txg
;
3063 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
3065 spa
->spa_load_max_txg
= max_request
;
3066 if (max_request
!= UINT64_MAX
)
3067 spa
->spa_extreme_rewind
= B_TRUE
;
3070 load_error
= rewind_error
= spa_load(spa
, state
, SPA_IMPORT_EXISTING
,
3072 if (load_error
== 0)
3075 if (spa
->spa_root_vdev
!= NULL
)
3076 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
3078 spa
->spa_last_ubsync_txg
= spa
->spa_uberblock
.ub_txg
;
3079 spa
->spa_last_ubsync_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
3081 if (rewind_flags
& ZPOOL_NEVER_REWIND
) {
3082 nvlist_free(config
);
3083 return (load_error
);
3086 if (state
== SPA_LOAD_RECOVER
) {
3087 /* Price of rolling back is discarding txgs, including log */
3088 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
3091 * If we aren't rolling back save the load info from our first
3092 * import attempt so that we can restore it after attempting
3095 loadinfo
= spa
->spa_load_info
;
3096 spa
->spa_load_info
= fnvlist_alloc();
3099 spa
->spa_load_max_txg
= spa
->spa_last_ubsync_txg
;
3100 safe_rewind_txg
= spa
->spa_last_ubsync_txg
- TXG_DEFER_SIZE
;
3101 min_txg
= (rewind_flags
& ZPOOL_EXTREME_REWIND
) ?
3102 TXG_INITIAL
: safe_rewind_txg
;
3105 * Continue as long as we're finding errors, we're still within
3106 * the acceptable rewind range, and we're still finding uberblocks
3108 while (rewind_error
&& spa
->spa_uberblock
.ub_txg
>= min_txg
&&
3109 spa
->spa_uberblock
.ub_txg
<= spa
->spa_load_max_txg
) {
3110 if (spa
->spa_load_max_txg
< safe_rewind_txg
)
3111 spa
->spa_extreme_rewind
= B_TRUE
;
3112 rewind_error
= spa_load_retry(spa
, state
, mosconfig
);
3115 spa
->spa_extreme_rewind
= B_FALSE
;
3116 spa
->spa_load_max_txg
= UINT64_MAX
;
3118 if (config
&& (rewind_error
|| state
!= SPA_LOAD_RECOVER
))
3119 spa_config_set(spa
, config
);
3121 nvlist_free(config
);
3123 if (state
== SPA_LOAD_RECOVER
) {
3124 ASSERT3P(loadinfo
, ==, NULL
);
3125 return (rewind_error
);
3127 /* Store the rewind info as part of the initial load info */
3128 fnvlist_add_nvlist(loadinfo
, ZPOOL_CONFIG_REWIND_INFO
,
3129 spa
->spa_load_info
);
3131 /* Restore the initial load info */
3132 fnvlist_free(spa
->spa_load_info
);
3133 spa
->spa_load_info
= loadinfo
;
3135 return (load_error
);
3142 * The import case is identical to an open except that the configuration is sent
3143 * down from userland, instead of grabbed from the configuration cache. For the
3144 * case of an open, the pool configuration will exist in the
3145 * POOL_STATE_UNINITIALIZED state.
3147 * The stats information (gen/count/ustats) is used to gather vdev statistics at
3148 * the same time open the pool, without having to keep around the spa_t in some
3152 spa_open_common(const char *pool
, spa_t
**spapp
, void *tag
, nvlist_t
*nvpolicy
,
3156 spa_load_state_t state
= SPA_LOAD_OPEN
;
3158 int locked
= B_FALSE
;
3159 int firstopen
= B_FALSE
;
3164 * As disgusting as this is, we need to support recursive calls to this
3165 * function because dsl_dir_open() is called during spa_load(), and ends
3166 * up calling spa_open() again. The real fix is to figure out how to
3167 * avoid dsl_dir_open() calling this in the first place.
3169 if (mutex_owner(&spa_namespace_lock
) != curthread
) {
3170 mutex_enter(&spa_namespace_lock
);
3174 if ((spa
= spa_lookup(pool
)) == NULL
) {
3176 mutex_exit(&spa_namespace_lock
);
3177 return (SET_ERROR(ENOENT
));
3180 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
) {
3181 zpool_rewind_policy_t policy
;
3185 zpool_get_rewind_policy(nvpolicy
? nvpolicy
: spa
->spa_config
,
3187 if (policy
.zrp_request
& ZPOOL_DO_REWIND
)
3188 state
= SPA_LOAD_RECOVER
;
3190 spa_activate(spa
, spa_mode_global
);
3192 if (state
!= SPA_LOAD_RECOVER
)
3193 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
3195 error
= spa_load_best(spa
, state
, B_FALSE
, policy
.zrp_txg
,
3196 policy
.zrp_request
);
3198 if (error
== EBADF
) {
3200 * If vdev_validate() returns failure (indicated by
3201 * EBADF), it indicates that one of the vdevs indicates
3202 * that the pool has been exported or destroyed. If
3203 * this is the case, the config cache is out of sync and
3204 * we should remove the pool from the namespace.
3207 spa_deactivate(spa
);
3208 spa_config_sync(spa
, B_TRUE
, B_TRUE
);
3211 mutex_exit(&spa_namespace_lock
);
3212 return (SET_ERROR(ENOENT
));
3217 * We can't open the pool, but we still have useful
3218 * information: the state of each vdev after the
3219 * attempted vdev_open(). Return this to the user.
3221 if (config
!= NULL
&& spa
->spa_config
) {
3222 VERIFY(nvlist_dup(spa
->spa_config
, config
,
3224 VERIFY(nvlist_add_nvlist(*config
,
3225 ZPOOL_CONFIG_LOAD_INFO
,
3226 spa
->spa_load_info
) == 0);
3229 spa_deactivate(spa
);
3230 spa
->spa_last_open_failed
= error
;
3232 mutex_exit(&spa_namespace_lock
);
3238 spa_open_ref(spa
, tag
);
3241 *config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
3244 * If we've recovered the pool, pass back any information we
3245 * gathered while doing the load.
3247 if (state
== SPA_LOAD_RECOVER
) {
3248 VERIFY(nvlist_add_nvlist(*config
, ZPOOL_CONFIG_LOAD_INFO
,
3249 spa
->spa_load_info
) == 0);
3253 spa
->spa_last_open_failed
= 0;
3254 spa
->spa_last_ubsync_txg
= 0;
3255 spa
->spa_load_txg
= 0;
3256 mutex_exit(&spa_namespace_lock
);
3260 zvol_create_minors(spa
, spa_name(spa
), B_TRUE
);
3268 spa_open_rewind(const char *name
, spa_t
**spapp
, void *tag
, nvlist_t
*policy
,
3271 return (spa_open_common(name
, spapp
, tag
, policy
, config
));
3275 spa_open(const char *name
, spa_t
**spapp
, void *tag
)
3277 return (spa_open_common(name
, spapp
, tag
, NULL
, NULL
));
3281 * Lookup the given spa_t, incrementing the inject count in the process,
3282 * preventing it from being exported or destroyed.
3285 spa_inject_addref(char *name
)
3289 mutex_enter(&spa_namespace_lock
);
3290 if ((spa
= spa_lookup(name
)) == NULL
) {
3291 mutex_exit(&spa_namespace_lock
);
3294 spa
->spa_inject_ref
++;
3295 mutex_exit(&spa_namespace_lock
);
3301 spa_inject_delref(spa_t
*spa
)
3303 mutex_enter(&spa_namespace_lock
);
3304 spa
->spa_inject_ref
--;
3305 mutex_exit(&spa_namespace_lock
);
3309 * Add spares device information to the nvlist.
3312 spa_add_spares(spa_t
*spa
, nvlist_t
*config
)
3322 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
3324 if (spa
->spa_spares
.sav_count
== 0)
3327 VERIFY(nvlist_lookup_nvlist(config
,
3328 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
3329 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
3330 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
3332 VERIFY(nvlist_add_nvlist_array(nvroot
,
3333 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
3334 VERIFY(nvlist_lookup_nvlist_array(nvroot
,
3335 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
3338 * Go through and find any spares which have since been
3339 * repurposed as an active spare. If this is the case, update
3340 * their status appropriately.
3342 for (i
= 0; i
< nspares
; i
++) {
3343 VERIFY(nvlist_lookup_uint64(spares
[i
],
3344 ZPOOL_CONFIG_GUID
, &guid
) == 0);
3345 if (spa_spare_exists(guid
, &pool
, NULL
) &&
3347 VERIFY(nvlist_lookup_uint64_array(
3348 spares
[i
], ZPOOL_CONFIG_VDEV_STATS
,
3349 (uint64_t **)&vs
, &vsc
) == 0);
3350 vs
->vs_state
= VDEV_STATE_CANT_OPEN
;
3351 vs
->vs_aux
= VDEV_AUX_SPARED
;
3358 * Add l2cache device information to the nvlist, including vdev stats.
3361 spa_add_l2cache(spa_t
*spa
, nvlist_t
*config
)
3364 uint_t i
, j
, nl2cache
;
3371 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
3373 if (spa
->spa_l2cache
.sav_count
== 0)
3376 VERIFY(nvlist_lookup_nvlist(config
,
3377 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
3378 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_l2cache
.sav_config
,
3379 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
3380 if (nl2cache
!= 0) {
3381 VERIFY(nvlist_add_nvlist_array(nvroot
,
3382 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
3383 VERIFY(nvlist_lookup_nvlist_array(nvroot
,
3384 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
3387 * Update level 2 cache device stats.
3390 for (i
= 0; i
< nl2cache
; i
++) {
3391 VERIFY(nvlist_lookup_uint64(l2cache
[i
],
3392 ZPOOL_CONFIG_GUID
, &guid
) == 0);
3395 for (j
= 0; j
< spa
->spa_l2cache
.sav_count
; j
++) {
3397 spa
->spa_l2cache
.sav_vdevs
[j
]->vdev_guid
) {
3398 vd
= spa
->spa_l2cache
.sav_vdevs
[j
];
3404 VERIFY(nvlist_lookup_uint64_array(l2cache
[i
],
3405 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
, &vsc
)
3407 vdev_get_stats(vd
, vs
);
3408 vdev_config_generate_stats(vd
, l2cache
[i
]);
3415 spa_feature_stats_from_disk(spa_t
*spa
, nvlist_t
*features
)
3420 if (spa
->spa_feat_for_read_obj
!= 0) {
3421 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
3422 spa
->spa_feat_for_read_obj
);
3423 zap_cursor_retrieve(&zc
, &za
) == 0;
3424 zap_cursor_advance(&zc
)) {
3425 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
3426 za
.za_num_integers
== 1);
3427 VERIFY0(nvlist_add_uint64(features
, za
.za_name
,
3428 za
.za_first_integer
));
3430 zap_cursor_fini(&zc
);
3433 if (spa
->spa_feat_for_write_obj
!= 0) {
3434 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
3435 spa
->spa_feat_for_write_obj
);
3436 zap_cursor_retrieve(&zc
, &za
) == 0;
3437 zap_cursor_advance(&zc
)) {
3438 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
3439 za
.za_num_integers
== 1);
3440 VERIFY0(nvlist_add_uint64(features
, za
.za_name
,
3441 za
.za_first_integer
));
3443 zap_cursor_fini(&zc
);
3448 spa_feature_stats_from_cache(spa_t
*spa
, nvlist_t
*features
)
3452 for (i
= 0; i
< SPA_FEATURES
; i
++) {
3453 zfeature_info_t feature
= spa_feature_table
[i
];
3456 if (feature_get_refcount(spa
, &feature
, &refcount
) != 0)
3459 VERIFY0(nvlist_add_uint64(features
, feature
.fi_guid
, refcount
));
3464 * Store a list of pool features and their reference counts in the
3467 * The first time this is called on a spa, allocate a new nvlist, fetch
3468 * the pool features and reference counts from disk, then save the list
3469 * in the spa. In subsequent calls on the same spa use the saved nvlist
3470 * and refresh its values from the cached reference counts. This
3471 * ensures we don't block here on I/O on a suspended pool so 'zpool
3472 * clear' can resume the pool.
3475 spa_add_feature_stats(spa_t
*spa
, nvlist_t
*config
)
3479 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
3481 mutex_enter(&spa
->spa_feat_stats_lock
);
3482 features
= spa
->spa_feat_stats
;
3484 if (features
!= NULL
) {
3485 spa_feature_stats_from_cache(spa
, features
);
3487 VERIFY0(nvlist_alloc(&features
, NV_UNIQUE_NAME
, KM_SLEEP
));
3488 spa
->spa_feat_stats
= features
;
3489 spa_feature_stats_from_disk(spa
, features
);
3492 VERIFY0(nvlist_add_nvlist(config
, ZPOOL_CONFIG_FEATURE_STATS
,
3495 mutex_exit(&spa
->spa_feat_stats_lock
);
3499 spa_get_stats(const char *name
, nvlist_t
**config
,
3500 char *altroot
, size_t buflen
)
3506 error
= spa_open_common(name
, &spa
, FTAG
, NULL
, config
);
3510 * This still leaves a window of inconsistency where the spares
3511 * or l2cache devices could change and the config would be
3512 * self-inconsistent.
3514 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
3516 if (*config
!= NULL
) {
3517 uint64_t loadtimes
[2];
3519 loadtimes
[0] = spa
->spa_loaded_ts
.tv_sec
;
3520 loadtimes
[1] = spa
->spa_loaded_ts
.tv_nsec
;
3521 VERIFY(nvlist_add_uint64_array(*config
,
3522 ZPOOL_CONFIG_LOADED_TIME
, loadtimes
, 2) == 0);
3524 VERIFY(nvlist_add_uint64(*config
,
3525 ZPOOL_CONFIG_ERRCOUNT
,
3526 spa_get_errlog_size(spa
)) == 0);
3528 if (spa_suspended(spa
))
3529 VERIFY(nvlist_add_uint64(*config
,
3530 ZPOOL_CONFIG_SUSPENDED
,
3531 spa
->spa_failmode
) == 0);
3533 spa_add_spares(spa
, *config
);
3534 spa_add_l2cache(spa
, *config
);
3535 spa_add_feature_stats(spa
, *config
);
3540 * We want to get the alternate root even for faulted pools, so we cheat
3541 * and call spa_lookup() directly.
3545 mutex_enter(&spa_namespace_lock
);
3546 spa
= spa_lookup(name
);
3548 spa_altroot(spa
, altroot
, buflen
);
3552 mutex_exit(&spa_namespace_lock
);
3554 spa_altroot(spa
, altroot
, buflen
);
3559 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
3560 spa_close(spa
, FTAG
);
3567 * Validate that the auxiliary device array is well formed. We must have an
3568 * array of nvlists, each which describes a valid leaf vdev. If this is an
3569 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
3570 * specified, as long as they are well-formed.
3573 spa_validate_aux_devs(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
,
3574 spa_aux_vdev_t
*sav
, const char *config
, uint64_t version
,
3575 vdev_labeltype_t label
)
3582 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
3585 * It's acceptable to have no devs specified.
3587 if (nvlist_lookup_nvlist_array(nvroot
, config
, &dev
, &ndev
) != 0)
3591 return (SET_ERROR(EINVAL
));
3594 * Make sure the pool is formatted with a version that supports this
3597 if (spa_version(spa
) < version
)
3598 return (SET_ERROR(ENOTSUP
));
3601 * Set the pending device list so we correctly handle device in-use
3604 sav
->sav_pending
= dev
;
3605 sav
->sav_npending
= ndev
;
3607 for (i
= 0; i
< ndev
; i
++) {
3608 if ((error
= spa_config_parse(spa
, &vd
, dev
[i
], NULL
, 0,
3612 if (!vd
->vdev_ops
->vdev_op_leaf
) {
3614 error
= SET_ERROR(EINVAL
);
3619 * The L2ARC currently only supports disk devices in
3620 * kernel context. For user-level testing, we allow it.
3623 if ((strcmp(config
, ZPOOL_CONFIG_L2CACHE
) == 0) &&
3624 strcmp(vd
->vdev_ops
->vdev_op_type
, VDEV_TYPE_DISK
) != 0) {
3625 error
= SET_ERROR(ENOTBLK
);
3632 if ((error
= vdev_open(vd
)) == 0 &&
3633 (error
= vdev_label_init(vd
, crtxg
, label
)) == 0) {
3634 VERIFY(nvlist_add_uint64(dev
[i
], ZPOOL_CONFIG_GUID
,
3635 vd
->vdev_guid
) == 0);
3641 (mode
!= VDEV_ALLOC_SPARE
&& mode
!= VDEV_ALLOC_L2CACHE
))
3648 sav
->sav_pending
= NULL
;
3649 sav
->sav_npending
= 0;
3654 spa_validate_aux(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
)
3658 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
3660 if ((error
= spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
3661 &spa
->spa_spares
, ZPOOL_CONFIG_SPARES
, SPA_VERSION_SPARES
,
3662 VDEV_LABEL_SPARE
)) != 0) {
3666 return (spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
3667 &spa
->spa_l2cache
, ZPOOL_CONFIG_L2CACHE
, SPA_VERSION_L2CACHE
,
3668 VDEV_LABEL_L2CACHE
));
3672 spa_set_aux_vdevs(spa_aux_vdev_t
*sav
, nvlist_t
**devs
, int ndevs
,
3677 if (sav
->sav_config
!= NULL
) {
3683 * Generate new dev list by concatentating with the
3686 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
, config
,
3687 &olddevs
, &oldndevs
) == 0);
3689 newdevs
= kmem_alloc(sizeof (void *) *
3690 (ndevs
+ oldndevs
), KM_SLEEP
);
3691 for (i
= 0; i
< oldndevs
; i
++)
3692 VERIFY(nvlist_dup(olddevs
[i
], &newdevs
[i
],
3694 for (i
= 0; i
< ndevs
; i
++)
3695 VERIFY(nvlist_dup(devs
[i
], &newdevs
[i
+ oldndevs
],
3698 VERIFY(nvlist_remove(sav
->sav_config
, config
,
3699 DATA_TYPE_NVLIST_ARRAY
) == 0);
3701 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
,
3702 config
, newdevs
, ndevs
+ oldndevs
) == 0);
3703 for (i
= 0; i
< oldndevs
+ ndevs
; i
++)
3704 nvlist_free(newdevs
[i
]);
3705 kmem_free(newdevs
, (oldndevs
+ ndevs
) * sizeof (void *));
3708 * Generate a new dev list.
3710 VERIFY(nvlist_alloc(&sav
->sav_config
, NV_UNIQUE_NAME
,
3712 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
, config
,
3718 * Stop and drop level 2 ARC devices
3721 spa_l2cache_drop(spa_t
*spa
)
3725 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
3727 for (i
= 0; i
< sav
->sav_count
; i
++) {
3730 vd
= sav
->sav_vdevs
[i
];
3733 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
3734 pool
!= 0ULL && l2arc_vdev_present(vd
))
3735 l2arc_remove_vdev(vd
);
3743 spa_create(const char *pool
, nvlist_t
*nvroot
, nvlist_t
*props
,
3747 char *altroot
= NULL
;
3752 uint64_t txg
= TXG_INITIAL
;
3753 nvlist_t
**spares
, **l2cache
;
3754 uint_t nspares
, nl2cache
;
3755 uint64_t version
, obj
;
3756 boolean_t has_features
;
3762 if (nvlist_lookup_string(props
, "tname", &poolname
) != 0)
3763 poolname
= (char *)pool
;
3766 * If this pool already exists, return failure.
3768 mutex_enter(&spa_namespace_lock
);
3769 if (spa_lookup(poolname
) != NULL
) {
3770 mutex_exit(&spa_namespace_lock
);
3771 return (SET_ERROR(EEXIST
));
3775 * Allocate a new spa_t structure.
3777 nvl
= fnvlist_alloc();
3778 fnvlist_add_string(nvl
, ZPOOL_CONFIG_POOL_NAME
, pool
);
3779 (void) nvlist_lookup_string(props
,
3780 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
3781 spa
= spa_add(poolname
, nvl
, altroot
);
3783 spa_activate(spa
, spa_mode_global
);
3785 if (props
&& (error
= spa_prop_validate(spa
, props
))) {
3786 spa_deactivate(spa
);
3788 mutex_exit(&spa_namespace_lock
);
3793 * Temporary pool names should never be written to disk.
3795 if (poolname
!= pool
)
3796 spa
->spa_import_flags
|= ZFS_IMPORT_TEMP_NAME
;
3798 has_features
= B_FALSE
;
3799 for (elem
= nvlist_next_nvpair(props
, NULL
);
3800 elem
!= NULL
; elem
= nvlist_next_nvpair(props
, elem
)) {
3801 if (zpool_prop_feature(nvpair_name(elem
)))
3802 has_features
= B_TRUE
;
3805 if (has_features
|| nvlist_lookup_uint64(props
,
3806 zpool_prop_to_name(ZPOOL_PROP_VERSION
), &version
) != 0) {
3807 version
= SPA_VERSION
;
3809 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
3811 spa
->spa_first_txg
= txg
;
3812 spa
->spa_uberblock
.ub_txg
= txg
- 1;
3813 spa
->spa_uberblock
.ub_version
= version
;
3814 spa
->spa_ubsync
= spa
->spa_uberblock
;
3815 spa
->spa_load_state
= SPA_LOAD_CREATE
;
3818 * Create "The Godfather" zio to hold all async IOs
3820 spa
->spa_async_zio_root
= kmem_alloc(max_ncpus
* sizeof (void *),
3822 for (i
= 0; i
< max_ncpus
; i
++) {
3823 spa
->spa_async_zio_root
[i
] = zio_root(spa
, NULL
, NULL
,
3824 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
3825 ZIO_FLAG_GODFATHER
);
3829 * Create the root vdev.
3831 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3833 error
= spa_config_parse(spa
, &rvd
, nvroot
, NULL
, 0, VDEV_ALLOC_ADD
);
3835 ASSERT(error
!= 0 || rvd
!= NULL
);
3836 ASSERT(error
!= 0 || spa
->spa_root_vdev
== rvd
);
3838 if (error
== 0 && !zfs_allocatable_devs(nvroot
))
3839 error
= SET_ERROR(EINVAL
);
3842 (error
= vdev_create(rvd
, txg
, B_FALSE
)) == 0 &&
3843 (error
= spa_validate_aux(spa
, nvroot
, txg
,
3844 VDEV_ALLOC_ADD
)) == 0) {
3845 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
3846 vdev_metaslab_set_size(rvd
->vdev_child
[c
]);
3847 vdev_expand(rvd
->vdev_child
[c
], txg
);
3851 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3855 spa_deactivate(spa
);
3857 mutex_exit(&spa_namespace_lock
);
3862 * Get the list of spares, if specified.
3864 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
3865 &spares
, &nspares
) == 0) {
3866 VERIFY(nvlist_alloc(&spa
->spa_spares
.sav_config
, NV_UNIQUE_NAME
,
3868 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
3869 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
3870 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3871 spa_load_spares(spa
);
3872 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3873 spa
->spa_spares
.sav_sync
= B_TRUE
;
3877 * Get the list of level 2 cache devices, if specified.
3879 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
3880 &l2cache
, &nl2cache
) == 0) {
3881 VERIFY(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
3882 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
3883 VERIFY(nvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
3884 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
3885 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3886 spa_load_l2cache(spa
);
3887 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3888 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
3891 spa
->spa_is_initializing
= B_TRUE
;
3892 spa
->spa_dsl_pool
= dp
= dsl_pool_create(spa
, zplprops
, txg
);
3893 spa
->spa_meta_objset
= dp
->dp_meta_objset
;
3894 spa
->spa_is_initializing
= B_FALSE
;
3897 * Create DDTs (dedup tables).
3901 spa_update_dspace(spa
);
3903 tx
= dmu_tx_create_assigned(dp
, txg
);
3906 * Create the pool config object.
3908 spa
->spa_config_object
= dmu_object_alloc(spa
->spa_meta_objset
,
3909 DMU_OT_PACKED_NVLIST
, SPA_CONFIG_BLOCKSIZE
,
3910 DMU_OT_PACKED_NVLIST_SIZE
, sizeof (uint64_t), tx
);
3912 if (zap_add(spa
->spa_meta_objset
,
3913 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CONFIG
,
3914 sizeof (uint64_t), 1, &spa
->spa_config_object
, tx
) != 0) {
3915 cmn_err(CE_PANIC
, "failed to add pool config");
3918 if (spa_version(spa
) >= SPA_VERSION_FEATURES
)
3919 spa_feature_create_zap_objects(spa
, tx
);
3921 if (zap_add(spa
->spa_meta_objset
,
3922 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CREATION_VERSION
,
3923 sizeof (uint64_t), 1, &version
, tx
) != 0) {
3924 cmn_err(CE_PANIC
, "failed to add pool version");
3927 /* Newly created pools with the right version are always deflated. */
3928 if (version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
3929 spa
->spa_deflate
= TRUE
;
3930 if (zap_add(spa
->spa_meta_objset
,
3931 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
3932 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
) != 0) {
3933 cmn_err(CE_PANIC
, "failed to add deflate");
3938 * Create the deferred-free bpobj. Turn off compression
3939 * because sync-to-convergence takes longer if the blocksize
3942 obj
= bpobj_alloc(spa
->spa_meta_objset
, 1 << 14, tx
);
3943 dmu_object_set_compress(spa
->spa_meta_objset
, obj
,
3944 ZIO_COMPRESS_OFF
, tx
);
3945 if (zap_add(spa
->spa_meta_objset
,
3946 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_SYNC_BPOBJ
,
3947 sizeof (uint64_t), 1, &obj
, tx
) != 0) {
3948 cmn_err(CE_PANIC
, "failed to add bpobj");
3950 VERIFY3U(0, ==, bpobj_open(&spa
->spa_deferred_bpobj
,
3951 spa
->spa_meta_objset
, obj
));
3954 * Create the pool's history object.
3956 if (version
>= SPA_VERSION_ZPOOL_HISTORY
)
3957 spa_history_create_obj(spa
, tx
);
3960 * Generate some random noise for salted checksums to operate on.
3962 (void) random_get_pseudo_bytes(spa
->spa_cksum_salt
.zcs_bytes
,
3963 sizeof (spa
->spa_cksum_salt
.zcs_bytes
));
3966 * Set pool properties.
3968 spa
->spa_bootfs
= zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS
);
3969 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
3970 spa
->spa_failmode
= zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE
);
3971 spa
->spa_autoexpand
= zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND
);
3973 if (props
!= NULL
) {
3974 spa_configfile_set(spa
, props
, B_FALSE
);
3975 spa_sync_props(props
, tx
);
3980 spa
->spa_sync_on
= B_TRUE
;
3981 txg_sync_start(spa
->spa_dsl_pool
);
3984 * We explicitly wait for the first transaction to complete so that our
3985 * bean counters are appropriately updated.
3987 txg_wait_synced(spa
->spa_dsl_pool
, txg
);
3989 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
3990 spa_event_notify(spa
, NULL
, ESC_ZFS_POOL_CREATE
);
3992 spa_history_log_version(spa
, "create");
3995 * Don't count references from objsets that are already closed
3996 * and are making their way through the eviction process.
3998 spa_evicting_os_wait(spa
);
3999 spa
->spa_minref
= refcount_count(&spa
->spa_refcount
);
4000 spa
->spa_load_state
= SPA_LOAD_NONE
;
4002 mutex_exit(&spa_namespace_lock
);
4008 * Import a non-root pool into the system.
4011 spa_import(char *pool
, nvlist_t
*config
, nvlist_t
*props
, uint64_t flags
)
4014 char *altroot
= NULL
;
4015 spa_load_state_t state
= SPA_LOAD_IMPORT
;
4016 zpool_rewind_policy_t policy
;
4017 uint64_t mode
= spa_mode_global
;
4018 uint64_t readonly
= B_FALSE
;
4021 nvlist_t
**spares
, **l2cache
;
4022 uint_t nspares
, nl2cache
;
4025 * If a pool with this name exists, return failure.
4027 mutex_enter(&spa_namespace_lock
);
4028 if (spa_lookup(pool
) != NULL
) {
4029 mutex_exit(&spa_namespace_lock
);
4030 return (SET_ERROR(EEXIST
));
4034 * Create and initialize the spa structure.
4036 (void) nvlist_lookup_string(props
,
4037 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
4038 (void) nvlist_lookup_uint64(props
,
4039 zpool_prop_to_name(ZPOOL_PROP_READONLY
), &readonly
);
4042 spa
= spa_add(pool
, config
, altroot
);
4043 spa
->spa_import_flags
= flags
;
4046 * Verbatim import - Take a pool and insert it into the namespace
4047 * as if it had been loaded at boot.
4049 if (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
) {
4051 spa_configfile_set(spa
, props
, B_FALSE
);
4053 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
4054 spa_event_notify(spa
, NULL
, ESC_ZFS_POOL_IMPORT
);
4056 mutex_exit(&spa_namespace_lock
);
4060 spa_activate(spa
, mode
);
4063 * Don't start async tasks until we know everything is healthy.
4065 spa_async_suspend(spa
);
4067 zpool_get_rewind_policy(config
, &policy
);
4068 if (policy
.zrp_request
& ZPOOL_DO_REWIND
)
4069 state
= SPA_LOAD_RECOVER
;
4072 * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig
4073 * because the user-supplied config is actually the one to trust when
4076 if (state
!= SPA_LOAD_RECOVER
)
4077 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
4079 error
= spa_load_best(spa
, state
, B_TRUE
, policy
.zrp_txg
,
4080 policy
.zrp_request
);
4083 * Propagate anything learned while loading the pool and pass it
4084 * back to caller (i.e. rewind info, missing devices, etc).
4086 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
4087 spa
->spa_load_info
) == 0);
4089 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4091 * Toss any existing sparelist, as it doesn't have any validity
4092 * anymore, and conflicts with spa_has_spare().
4094 if (spa
->spa_spares
.sav_config
) {
4095 nvlist_free(spa
->spa_spares
.sav_config
);
4096 spa
->spa_spares
.sav_config
= NULL
;
4097 spa_load_spares(spa
);
4099 if (spa
->spa_l2cache
.sav_config
) {
4100 nvlist_free(spa
->spa_l2cache
.sav_config
);
4101 spa
->spa_l2cache
.sav_config
= NULL
;
4102 spa_load_l2cache(spa
);
4105 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
4108 error
= spa_validate_aux(spa
, nvroot
, -1ULL,
4111 error
= spa_validate_aux(spa
, nvroot
, -1ULL,
4112 VDEV_ALLOC_L2CACHE
);
4113 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4116 spa_configfile_set(spa
, props
, B_FALSE
);
4118 if (error
!= 0 || (props
&& spa_writeable(spa
) &&
4119 (error
= spa_prop_set(spa
, props
)))) {
4121 spa_deactivate(spa
);
4123 mutex_exit(&spa_namespace_lock
);
4127 spa_async_resume(spa
);
4130 * Override any spares and level 2 cache devices as specified by
4131 * the user, as these may have correct device names/devids, etc.
4133 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
4134 &spares
, &nspares
) == 0) {
4135 if (spa
->spa_spares
.sav_config
)
4136 VERIFY(nvlist_remove(spa
->spa_spares
.sav_config
,
4137 ZPOOL_CONFIG_SPARES
, DATA_TYPE_NVLIST_ARRAY
) == 0);
4139 VERIFY(nvlist_alloc(&spa
->spa_spares
.sav_config
,
4140 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
4141 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
4142 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
4143 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4144 spa_load_spares(spa
);
4145 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4146 spa
->spa_spares
.sav_sync
= B_TRUE
;
4148 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
4149 &l2cache
, &nl2cache
) == 0) {
4150 if (spa
->spa_l2cache
.sav_config
)
4151 VERIFY(nvlist_remove(spa
->spa_l2cache
.sav_config
,
4152 ZPOOL_CONFIG_L2CACHE
, DATA_TYPE_NVLIST_ARRAY
) == 0);
4154 VERIFY(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
4155 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
4156 VERIFY(nvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
4157 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
4158 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4159 spa_load_l2cache(spa
);
4160 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4161 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
4165 * Check for any removed devices.
4167 if (spa
->spa_autoreplace
) {
4168 spa_aux_check_removed(&spa
->spa_spares
);
4169 spa_aux_check_removed(&spa
->spa_l2cache
);
4172 if (spa_writeable(spa
)) {
4174 * Update the config cache to include the newly-imported pool.
4176 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
4180 * It's possible that the pool was expanded while it was exported.
4181 * We kick off an async task to handle this for us.
4183 spa_async_request(spa
, SPA_ASYNC_AUTOEXPAND
);
4185 spa_history_log_version(spa
, "import");
4187 spa_event_notify(spa
, NULL
, ESC_ZFS_POOL_IMPORT
);
4189 zvol_create_minors(spa
, pool
, B_TRUE
);
4191 mutex_exit(&spa_namespace_lock
);
4197 spa_tryimport(nvlist_t
*tryconfig
)
4199 nvlist_t
*config
= NULL
;
4205 if (nvlist_lookup_string(tryconfig
, ZPOOL_CONFIG_POOL_NAME
, &poolname
))
4208 if (nvlist_lookup_uint64(tryconfig
, ZPOOL_CONFIG_POOL_STATE
, &state
))
4212 * Create and initialize the spa structure.
4214 mutex_enter(&spa_namespace_lock
);
4215 spa
= spa_add(TRYIMPORT_NAME
, tryconfig
, NULL
);
4216 spa_activate(spa
, FREAD
);
4219 * Pass off the heavy lifting to spa_load().
4220 * Pass TRUE for mosconfig because the user-supplied config
4221 * is actually the one to trust when doing an import.
4223 error
= spa_load(spa
, SPA_LOAD_TRYIMPORT
, SPA_IMPORT_EXISTING
, B_TRUE
);
4226 * If 'tryconfig' was at least parsable, return the current config.
4228 if (spa
->spa_root_vdev
!= NULL
) {
4229 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
4230 VERIFY(nvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
,
4232 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
4234 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_TIMESTAMP
,
4235 spa
->spa_uberblock
.ub_timestamp
) == 0);
4236 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
4237 spa
->spa_load_info
) == 0);
4238 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_ERRATA
,
4239 spa
->spa_errata
) == 0);
4242 * If the bootfs property exists on this pool then we
4243 * copy it out so that external consumers can tell which
4244 * pools are bootable.
4246 if ((!error
|| error
== EEXIST
) && spa
->spa_bootfs
) {
4247 char *tmpname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
4250 * We have to play games with the name since the
4251 * pool was opened as TRYIMPORT_NAME.
4253 if (dsl_dsobj_to_dsname(spa_name(spa
),
4254 spa
->spa_bootfs
, tmpname
) == 0) {
4258 dsname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
4260 cp
= strchr(tmpname
, '/');
4262 (void) strlcpy(dsname
, tmpname
,
4265 (void) snprintf(dsname
, MAXPATHLEN
,
4266 "%s/%s", poolname
, ++cp
);
4268 VERIFY(nvlist_add_string(config
,
4269 ZPOOL_CONFIG_BOOTFS
, dsname
) == 0);
4270 kmem_free(dsname
, MAXPATHLEN
);
4272 kmem_free(tmpname
, MAXPATHLEN
);
4276 * Add the list of hot spares and level 2 cache devices.
4278 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
4279 spa_add_spares(spa
, config
);
4280 spa_add_l2cache(spa
, config
);
4281 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
4285 spa_deactivate(spa
);
4287 mutex_exit(&spa_namespace_lock
);
4293 * Pool export/destroy
4295 * The act of destroying or exporting a pool is very simple. We make sure there
4296 * is no more pending I/O and any references to the pool are gone. Then, we
4297 * update the pool state and sync all the labels to disk, removing the
4298 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
4299 * we don't sync the labels or remove the configuration cache.
4302 spa_export_common(char *pool
, int new_state
, nvlist_t
**oldconfig
,
4303 boolean_t force
, boolean_t hardforce
)
4310 if (!(spa_mode_global
& FWRITE
))
4311 return (SET_ERROR(EROFS
));
4313 mutex_enter(&spa_namespace_lock
);
4314 if ((spa
= spa_lookup(pool
)) == NULL
) {
4315 mutex_exit(&spa_namespace_lock
);
4316 return (SET_ERROR(ENOENT
));
4320 * Put a hold on the pool, drop the namespace lock, stop async tasks,
4321 * reacquire the namespace lock, and see if we can export.
4323 spa_open_ref(spa
, FTAG
);
4324 mutex_exit(&spa_namespace_lock
);
4325 spa_async_suspend(spa
);
4326 if (spa
->spa_zvol_taskq
) {
4327 zvol_remove_minors(spa
, spa_name(spa
), B_TRUE
);
4328 taskq_wait(spa
->spa_zvol_taskq
);
4330 mutex_enter(&spa_namespace_lock
);
4331 spa_close(spa
, FTAG
);
4333 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
)
4336 * The pool will be in core if it's openable, in which case we can
4337 * modify its state. Objsets may be open only because they're dirty,
4338 * so we have to force it to sync before checking spa_refcnt.
4340 if (spa
->spa_sync_on
) {
4341 txg_wait_synced(spa
->spa_dsl_pool
, 0);
4342 spa_evicting_os_wait(spa
);
4346 * A pool cannot be exported or destroyed if there are active
4347 * references. If we are resetting a pool, allow references by
4348 * fault injection handlers.
4350 if (!spa_refcount_zero(spa
) ||
4351 (spa
->spa_inject_ref
!= 0 &&
4352 new_state
!= POOL_STATE_UNINITIALIZED
)) {
4353 spa_async_resume(spa
);
4354 mutex_exit(&spa_namespace_lock
);
4355 return (SET_ERROR(EBUSY
));
4358 if (spa
->spa_sync_on
) {
4360 * A pool cannot be exported if it has an active shared spare.
4361 * This is to prevent other pools stealing the active spare
4362 * from an exported pool. At user's own will, such pool can
4363 * be forcedly exported.
4365 if (!force
&& new_state
== POOL_STATE_EXPORTED
&&
4366 spa_has_active_shared_spare(spa
)) {
4367 spa_async_resume(spa
);
4368 mutex_exit(&spa_namespace_lock
);
4369 return (SET_ERROR(EXDEV
));
4373 * We want this to be reflected on every label,
4374 * so mark them all dirty. spa_unload() will do the
4375 * final sync that pushes these changes out.
4377 if (new_state
!= POOL_STATE_UNINITIALIZED
&& !hardforce
) {
4378 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4379 spa
->spa_state
= new_state
;
4380 spa
->spa_final_txg
= spa_last_synced_txg(spa
) +
4382 vdev_config_dirty(spa
->spa_root_vdev
);
4383 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4388 spa_event_notify(spa
, NULL
, ESC_ZFS_POOL_DESTROY
);
4390 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
4392 spa_deactivate(spa
);
4395 if (oldconfig
&& spa
->spa_config
)
4396 VERIFY(nvlist_dup(spa
->spa_config
, oldconfig
, 0) == 0);
4398 if (new_state
!= POOL_STATE_UNINITIALIZED
) {
4400 spa_config_sync(spa
, B_TRUE
, B_TRUE
);
4403 mutex_exit(&spa_namespace_lock
);
4409 * Destroy a storage pool.
4412 spa_destroy(char *pool
)
4414 return (spa_export_common(pool
, POOL_STATE_DESTROYED
, NULL
,
4419 * Export a storage pool.
4422 spa_export(char *pool
, nvlist_t
**oldconfig
, boolean_t force
,
4423 boolean_t hardforce
)
4425 return (spa_export_common(pool
, POOL_STATE_EXPORTED
, oldconfig
,
4430 * Similar to spa_export(), this unloads the spa_t without actually removing it
4431 * from the namespace in any way.
4434 spa_reset(char *pool
)
4436 return (spa_export_common(pool
, POOL_STATE_UNINITIALIZED
, NULL
,
4441 * ==========================================================================
4442 * Device manipulation
4443 * ==========================================================================
4447 * Add a device to a storage pool.
4450 spa_vdev_add(spa_t
*spa
, nvlist_t
*nvroot
)
4454 vdev_t
*rvd
= spa
->spa_root_vdev
;
4456 nvlist_t
**spares
, **l2cache
;
4457 uint_t nspares
, nl2cache
;
4460 ASSERT(spa_writeable(spa
));
4462 txg
= spa_vdev_enter(spa
);
4464 if ((error
= spa_config_parse(spa
, &vd
, nvroot
, NULL
, 0,
4465 VDEV_ALLOC_ADD
)) != 0)
4466 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
4468 spa
->spa_pending_vdev
= vd
; /* spa_vdev_exit() will clear this */
4470 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
, &spares
,
4474 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
, &l2cache
,
4478 if (vd
->vdev_children
== 0 && nspares
== 0 && nl2cache
== 0)
4479 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
4481 if (vd
->vdev_children
!= 0 &&
4482 (error
= vdev_create(vd
, txg
, B_FALSE
)) != 0)
4483 return (spa_vdev_exit(spa
, vd
, txg
, error
));
4486 * We must validate the spares and l2cache devices after checking the
4487 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
4489 if ((error
= spa_validate_aux(spa
, nvroot
, txg
, VDEV_ALLOC_ADD
)) != 0)
4490 return (spa_vdev_exit(spa
, vd
, txg
, error
));
4493 * Transfer each new top-level vdev from vd to rvd.
4495 for (c
= 0; c
< vd
->vdev_children
; c
++) {
4498 * Set the vdev id to the first hole, if one exists.
4500 for (id
= 0; id
< rvd
->vdev_children
; id
++) {
4501 if (rvd
->vdev_child
[id
]->vdev_ishole
) {
4502 vdev_free(rvd
->vdev_child
[id
]);
4506 tvd
= vd
->vdev_child
[c
];
4507 vdev_remove_child(vd
, tvd
);
4509 vdev_add_child(rvd
, tvd
);
4510 vdev_config_dirty(tvd
);
4514 spa_set_aux_vdevs(&spa
->spa_spares
, spares
, nspares
,
4515 ZPOOL_CONFIG_SPARES
);
4516 spa_load_spares(spa
);
4517 spa
->spa_spares
.sav_sync
= B_TRUE
;
4520 if (nl2cache
!= 0) {
4521 spa_set_aux_vdevs(&spa
->spa_l2cache
, l2cache
, nl2cache
,
4522 ZPOOL_CONFIG_L2CACHE
);
4523 spa_load_l2cache(spa
);
4524 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
4528 * We have to be careful when adding new vdevs to an existing pool.
4529 * If other threads start allocating from these vdevs before we
4530 * sync the config cache, and we lose power, then upon reboot we may
4531 * fail to open the pool because there are DVAs that the config cache
4532 * can't translate. Therefore, we first add the vdevs without
4533 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
4534 * and then let spa_config_update() initialize the new metaslabs.
4536 * spa_load() checks for added-but-not-initialized vdevs, so that
4537 * if we lose power at any point in this sequence, the remaining
4538 * steps will be completed the next time we load the pool.
4540 (void) spa_vdev_exit(spa
, vd
, txg
, 0);
4542 mutex_enter(&spa_namespace_lock
);
4543 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
4544 spa_event_notify(spa
, NULL
, ESC_ZFS_VDEV_ADD
);
4545 mutex_exit(&spa_namespace_lock
);
4551 * Attach a device to a mirror. The arguments are the path to any device
4552 * in the mirror, and the nvroot for the new device. If the path specifies
4553 * a device that is not mirrored, we automatically insert the mirror vdev.
4555 * If 'replacing' is specified, the new device is intended to replace the
4556 * existing device; in this case the two devices are made into their own
4557 * mirror using the 'replacing' vdev, which is functionally identical to
4558 * the mirror vdev (it actually reuses all the same ops) but has a few
4559 * extra rules: you can't attach to it after it's been created, and upon
4560 * completion of resilvering, the first disk (the one being replaced)
4561 * is automatically detached.
4564 spa_vdev_attach(spa_t
*spa
, uint64_t guid
, nvlist_t
*nvroot
, int replacing
)
4566 uint64_t txg
, dtl_max_txg
;
4567 vdev_t
*oldvd
, *newvd
, *newrootvd
, *pvd
, *tvd
;
4569 char *oldvdpath
, *newvdpath
;
4572 ASSERTV(vdev_t
*rvd
= spa
->spa_root_vdev
);
4574 ASSERT(spa_writeable(spa
));
4576 txg
= spa_vdev_enter(spa
);
4578 oldvd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
4581 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
4583 if (!oldvd
->vdev_ops
->vdev_op_leaf
)
4584 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4586 pvd
= oldvd
->vdev_parent
;
4588 if ((error
= spa_config_parse(spa
, &newrootvd
, nvroot
, NULL
, 0,
4589 VDEV_ALLOC_ATTACH
)) != 0)
4590 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
4592 if (newrootvd
->vdev_children
!= 1)
4593 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
4595 newvd
= newrootvd
->vdev_child
[0];
4597 if (!newvd
->vdev_ops
->vdev_op_leaf
)
4598 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
4600 if ((error
= vdev_create(newrootvd
, txg
, replacing
)) != 0)
4601 return (spa_vdev_exit(spa
, newrootvd
, txg
, error
));
4604 * Spares can't replace logs
4606 if (oldvd
->vdev_top
->vdev_islog
&& newvd
->vdev_isspare
)
4607 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4611 * For attach, the only allowable parent is a mirror or the root
4614 if (pvd
->vdev_ops
!= &vdev_mirror_ops
&&
4615 pvd
->vdev_ops
!= &vdev_root_ops
)
4616 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4618 pvops
= &vdev_mirror_ops
;
4621 * Active hot spares can only be replaced by inactive hot
4624 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
4625 oldvd
->vdev_isspare
&&
4626 !spa_has_spare(spa
, newvd
->vdev_guid
))
4627 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4630 * If the source is a hot spare, and the parent isn't already a
4631 * spare, then we want to create a new hot spare. Otherwise, we
4632 * want to create a replacing vdev. The user is not allowed to
4633 * attach to a spared vdev child unless the 'isspare' state is
4634 * the same (spare replaces spare, non-spare replaces
4637 if (pvd
->vdev_ops
== &vdev_replacing_ops
&&
4638 spa_version(spa
) < SPA_VERSION_MULTI_REPLACE
) {
4639 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4640 } else if (pvd
->vdev_ops
== &vdev_spare_ops
&&
4641 newvd
->vdev_isspare
!= oldvd
->vdev_isspare
) {
4642 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4645 if (newvd
->vdev_isspare
)
4646 pvops
= &vdev_spare_ops
;
4648 pvops
= &vdev_replacing_ops
;
4652 * Make sure the new device is big enough.
4654 if (newvd
->vdev_asize
< vdev_get_min_asize(oldvd
))
4655 return (spa_vdev_exit(spa
, newrootvd
, txg
, EOVERFLOW
));
4658 * The new device cannot have a higher alignment requirement
4659 * than the top-level vdev.
4661 if (newvd
->vdev_ashift
> oldvd
->vdev_top
->vdev_ashift
)
4662 return (spa_vdev_exit(spa
, newrootvd
, txg
, EDOM
));
4665 * If this is an in-place replacement, update oldvd's path and devid
4666 * to make it distinguishable from newvd, and unopenable from now on.
4668 if (strcmp(oldvd
->vdev_path
, newvd
->vdev_path
) == 0) {
4669 spa_strfree(oldvd
->vdev_path
);
4670 oldvd
->vdev_path
= kmem_alloc(strlen(newvd
->vdev_path
) + 5,
4672 (void) sprintf(oldvd
->vdev_path
, "%s/%s",
4673 newvd
->vdev_path
, "old");
4674 if (oldvd
->vdev_devid
!= NULL
) {
4675 spa_strfree(oldvd
->vdev_devid
);
4676 oldvd
->vdev_devid
= NULL
;
4680 /* mark the device being resilvered */
4681 newvd
->vdev_resilver_txg
= txg
;
4684 * If the parent is not a mirror, or if we're replacing, insert the new
4685 * mirror/replacing/spare vdev above oldvd.
4687 if (pvd
->vdev_ops
!= pvops
)
4688 pvd
= vdev_add_parent(oldvd
, pvops
);
4690 ASSERT(pvd
->vdev_top
->vdev_parent
== rvd
);
4691 ASSERT(pvd
->vdev_ops
== pvops
);
4692 ASSERT(oldvd
->vdev_parent
== pvd
);
4695 * Extract the new device from its root and add it to pvd.
4697 vdev_remove_child(newrootvd
, newvd
);
4698 newvd
->vdev_id
= pvd
->vdev_children
;
4699 newvd
->vdev_crtxg
= oldvd
->vdev_crtxg
;
4700 vdev_add_child(pvd
, newvd
);
4702 tvd
= newvd
->vdev_top
;
4703 ASSERT(pvd
->vdev_top
== tvd
);
4704 ASSERT(tvd
->vdev_parent
== rvd
);
4706 vdev_config_dirty(tvd
);
4709 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
4710 * for any dmu_sync-ed blocks. It will propagate upward when
4711 * spa_vdev_exit() calls vdev_dtl_reassess().
4713 dtl_max_txg
= txg
+ TXG_CONCURRENT_STATES
;
4715 vdev_dtl_dirty(newvd
, DTL_MISSING
, TXG_INITIAL
,
4716 dtl_max_txg
- TXG_INITIAL
);
4718 if (newvd
->vdev_isspare
) {
4719 spa_spare_activate(newvd
);
4720 spa_event_notify(spa
, newvd
, ESC_ZFS_VDEV_SPARE
);
4723 oldvdpath
= spa_strdup(oldvd
->vdev_path
);
4724 newvdpath
= spa_strdup(newvd
->vdev_path
);
4725 newvd_isspare
= newvd
->vdev_isspare
;
4728 * Mark newvd's DTL dirty in this txg.
4730 vdev_dirty(tvd
, VDD_DTL
, newvd
, txg
);
4733 * Schedule the resilver to restart in the future. We do this to
4734 * ensure that dmu_sync-ed blocks have been stitched into the
4735 * respective datasets.
4737 dsl_resilver_restart(spa
->spa_dsl_pool
, dtl_max_txg
);
4739 if (spa
->spa_bootfs
)
4740 spa_event_notify(spa
, newvd
, ESC_ZFS_BOOTFS_VDEV_ATTACH
);
4742 spa_event_notify(spa
, newvd
, ESC_ZFS_VDEV_ATTACH
);
4747 (void) spa_vdev_exit(spa
, newrootvd
, dtl_max_txg
, 0);
4749 spa_history_log_internal(spa
, "vdev attach", NULL
,
4750 "%s vdev=%s %s vdev=%s",
4751 replacing
&& newvd_isspare
? "spare in" :
4752 replacing
? "replace" : "attach", newvdpath
,
4753 replacing
? "for" : "to", oldvdpath
);
4755 spa_strfree(oldvdpath
);
4756 spa_strfree(newvdpath
);
4762 * Detach a device from a mirror or replacing vdev.
4764 * If 'replace_done' is specified, only detach if the parent
4765 * is a replacing vdev.
4768 spa_vdev_detach(spa_t
*spa
, uint64_t guid
, uint64_t pguid
, int replace_done
)
4772 vdev_t
*vd
, *pvd
, *cvd
, *tvd
;
4773 boolean_t unspare
= B_FALSE
;
4774 uint64_t unspare_guid
= 0;
4777 ASSERTV(vdev_t
*rvd
= spa
->spa_root_vdev
);
4778 ASSERT(spa_writeable(spa
));
4780 txg
= spa_vdev_enter(spa
);
4782 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
4785 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
4787 if (!vd
->vdev_ops
->vdev_op_leaf
)
4788 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4790 pvd
= vd
->vdev_parent
;
4793 * If the parent/child relationship is not as expected, don't do it.
4794 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
4795 * vdev that's replacing B with C. The user's intent in replacing
4796 * is to go from M(A,B) to M(A,C). If the user decides to cancel
4797 * the replace by detaching C, the expected behavior is to end up
4798 * M(A,B). But suppose that right after deciding to detach C,
4799 * the replacement of B completes. We would have M(A,C), and then
4800 * ask to detach C, which would leave us with just A -- not what
4801 * the user wanted. To prevent this, we make sure that the
4802 * parent/child relationship hasn't changed -- in this example,
4803 * that C's parent is still the replacing vdev R.
4805 if (pvd
->vdev_guid
!= pguid
&& pguid
!= 0)
4806 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
4809 * Only 'replacing' or 'spare' vdevs can be replaced.
4811 if (replace_done
&& pvd
->vdev_ops
!= &vdev_replacing_ops
&&
4812 pvd
->vdev_ops
!= &vdev_spare_ops
)
4813 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4815 ASSERT(pvd
->vdev_ops
!= &vdev_spare_ops
||
4816 spa_version(spa
) >= SPA_VERSION_SPARES
);
4819 * Only mirror, replacing, and spare vdevs support detach.
4821 if (pvd
->vdev_ops
!= &vdev_replacing_ops
&&
4822 pvd
->vdev_ops
!= &vdev_mirror_ops
&&
4823 pvd
->vdev_ops
!= &vdev_spare_ops
)
4824 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4827 * If this device has the only valid copy of some data,
4828 * we cannot safely detach it.
4830 if (vdev_dtl_required(vd
))
4831 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
4833 ASSERT(pvd
->vdev_children
>= 2);
4836 * If we are detaching the second disk from a replacing vdev, then
4837 * check to see if we changed the original vdev's path to have "/old"
4838 * at the end in spa_vdev_attach(). If so, undo that change now.
4840 if (pvd
->vdev_ops
== &vdev_replacing_ops
&& vd
->vdev_id
> 0 &&
4841 vd
->vdev_path
!= NULL
) {
4842 size_t len
= strlen(vd
->vdev_path
);
4844 for (c
= 0; c
< pvd
->vdev_children
; c
++) {
4845 cvd
= pvd
->vdev_child
[c
];
4847 if (cvd
== vd
|| cvd
->vdev_path
== NULL
)
4850 if (strncmp(cvd
->vdev_path
, vd
->vdev_path
, len
) == 0 &&
4851 strcmp(cvd
->vdev_path
+ len
, "/old") == 0) {
4852 spa_strfree(cvd
->vdev_path
);
4853 cvd
->vdev_path
= spa_strdup(vd
->vdev_path
);
4860 * If we are detaching the original disk from a spare, then it implies
4861 * that the spare should become a real disk, and be removed from the
4862 * active spare list for the pool.
4864 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
4866 pvd
->vdev_child
[pvd
->vdev_children
- 1]->vdev_isspare
)
4870 * Erase the disk labels so the disk can be used for other things.
4871 * This must be done after all other error cases are handled,
4872 * but before we disembowel vd (so we can still do I/O to it).
4873 * But if we can't do it, don't treat the error as fatal --
4874 * it may be that the unwritability of the disk is the reason
4875 * it's being detached!
4877 error
= vdev_label_init(vd
, 0, VDEV_LABEL_REMOVE
);
4880 * Remove vd from its parent and compact the parent's children.
4882 vdev_remove_child(pvd
, vd
);
4883 vdev_compact_children(pvd
);
4886 * Remember one of the remaining children so we can get tvd below.
4888 cvd
= pvd
->vdev_child
[pvd
->vdev_children
- 1];
4891 * If we need to remove the remaining child from the list of hot spares,
4892 * do it now, marking the vdev as no longer a spare in the process.
4893 * We must do this before vdev_remove_parent(), because that can
4894 * change the GUID if it creates a new toplevel GUID. For a similar
4895 * reason, we must remove the spare now, in the same txg as the detach;
4896 * otherwise someone could attach a new sibling, change the GUID, and
4897 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
4900 ASSERT(cvd
->vdev_isspare
);
4901 spa_spare_remove(cvd
);
4902 unspare_guid
= cvd
->vdev_guid
;
4903 (void) spa_vdev_remove(spa
, unspare_guid
, B_TRUE
);
4904 cvd
->vdev_unspare
= B_TRUE
;
4908 * If the parent mirror/replacing vdev only has one child,
4909 * the parent is no longer needed. Remove it from the tree.
4911 if (pvd
->vdev_children
== 1) {
4912 if (pvd
->vdev_ops
== &vdev_spare_ops
)
4913 cvd
->vdev_unspare
= B_FALSE
;
4914 vdev_remove_parent(cvd
);
4919 * We don't set tvd until now because the parent we just removed
4920 * may have been the previous top-level vdev.
4922 tvd
= cvd
->vdev_top
;
4923 ASSERT(tvd
->vdev_parent
== rvd
);
4926 * Reevaluate the parent vdev state.
4928 vdev_propagate_state(cvd
);
4931 * If the 'autoexpand' property is set on the pool then automatically
4932 * try to expand the size of the pool. For example if the device we
4933 * just detached was smaller than the others, it may be possible to
4934 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
4935 * first so that we can obtain the updated sizes of the leaf vdevs.
4937 if (spa
->spa_autoexpand
) {
4939 vdev_expand(tvd
, txg
);
4942 vdev_config_dirty(tvd
);
4945 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
4946 * vd->vdev_detached is set and free vd's DTL object in syncing context.
4947 * But first make sure we're not on any *other* txg's DTL list, to
4948 * prevent vd from being accessed after it's freed.
4950 vdpath
= spa_strdup(vd
->vdev_path
);
4951 for (t
= 0; t
< TXG_SIZE
; t
++)
4952 (void) txg_list_remove_this(&tvd
->vdev_dtl_list
, vd
, t
);
4953 vd
->vdev_detached
= B_TRUE
;
4954 vdev_dirty(tvd
, VDD_DTL
, vd
, txg
);
4956 spa_event_notify(spa
, vd
, ESC_ZFS_VDEV_REMOVE
);
4958 /* hang on to the spa before we release the lock */
4959 spa_open_ref(spa
, FTAG
);
4961 error
= spa_vdev_exit(spa
, vd
, txg
, 0);
4963 spa_history_log_internal(spa
, "detach", NULL
,
4965 spa_strfree(vdpath
);
4968 * If this was the removal of the original device in a hot spare vdev,
4969 * then we want to go through and remove the device from the hot spare
4970 * list of every other pool.
4973 spa_t
*altspa
= NULL
;
4975 mutex_enter(&spa_namespace_lock
);
4976 while ((altspa
= spa_next(altspa
)) != NULL
) {
4977 if (altspa
->spa_state
!= POOL_STATE_ACTIVE
||
4981 spa_open_ref(altspa
, FTAG
);
4982 mutex_exit(&spa_namespace_lock
);
4983 (void) spa_vdev_remove(altspa
, unspare_guid
, B_TRUE
);
4984 mutex_enter(&spa_namespace_lock
);
4985 spa_close(altspa
, FTAG
);
4987 mutex_exit(&spa_namespace_lock
);
4989 /* search the rest of the vdevs for spares to remove */
4990 spa_vdev_resilver_done(spa
);
4993 /* all done with the spa; OK to release */
4994 mutex_enter(&spa_namespace_lock
);
4995 spa_close(spa
, FTAG
);
4996 mutex_exit(&spa_namespace_lock
);
5002 * Split a set of devices from their mirrors, and create a new pool from them.
5005 spa_vdev_split_mirror(spa_t
*spa
, char *newname
, nvlist_t
*config
,
5006 nvlist_t
*props
, boolean_t exp
)
5009 uint64_t txg
, *glist
;
5011 uint_t c
, children
, lastlog
;
5012 nvlist_t
**child
, *nvl
, *tmp
;
5014 char *altroot
= NULL
;
5015 vdev_t
*rvd
, **vml
= NULL
; /* vdev modify list */
5016 boolean_t activate_slog
;
5018 ASSERT(spa_writeable(spa
));
5020 txg
= spa_vdev_enter(spa
);
5022 /* clear the log and flush everything up to now */
5023 activate_slog
= spa_passivate_log(spa
);
5024 (void) spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
5025 error
= spa_offline_log(spa
);
5026 txg
= spa_vdev_config_enter(spa
);
5029 spa_activate_log(spa
);
5032 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5034 /* check new spa name before going any further */
5035 if (spa_lookup(newname
) != NULL
)
5036 return (spa_vdev_exit(spa
, NULL
, txg
, EEXIST
));
5039 * scan through all the children to ensure they're all mirrors
5041 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvl
) != 0 ||
5042 nvlist_lookup_nvlist_array(nvl
, ZPOOL_CONFIG_CHILDREN
, &child
,
5044 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
5046 /* first, check to ensure we've got the right child count */
5047 rvd
= spa
->spa_root_vdev
;
5049 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
5050 vdev_t
*vd
= rvd
->vdev_child
[c
];
5052 /* don't count the holes & logs as children */
5053 if (vd
->vdev_islog
|| vd
->vdev_ishole
) {
5061 if (children
!= (lastlog
!= 0 ? lastlog
: rvd
->vdev_children
))
5062 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
5064 /* next, ensure no spare or cache devices are part of the split */
5065 if (nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_SPARES
, &tmp
) == 0 ||
5066 nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_L2CACHE
, &tmp
) == 0)
5067 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
5069 vml
= kmem_zalloc(children
* sizeof (vdev_t
*), KM_SLEEP
);
5070 glist
= kmem_zalloc(children
* sizeof (uint64_t), KM_SLEEP
);
5072 /* then, loop over each vdev and validate it */
5073 for (c
= 0; c
< children
; c
++) {
5074 uint64_t is_hole
= 0;
5076 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
5080 if (spa
->spa_root_vdev
->vdev_child
[c
]->vdev_ishole
||
5081 spa
->spa_root_vdev
->vdev_child
[c
]->vdev_islog
) {
5084 error
= SET_ERROR(EINVAL
);
5089 /* which disk is going to be split? */
5090 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_GUID
,
5092 error
= SET_ERROR(EINVAL
);
5096 /* look it up in the spa */
5097 vml
[c
] = spa_lookup_by_guid(spa
, glist
[c
], B_FALSE
);
5098 if (vml
[c
] == NULL
) {
5099 error
= SET_ERROR(ENODEV
);
5103 /* make sure there's nothing stopping the split */
5104 if (vml
[c
]->vdev_parent
->vdev_ops
!= &vdev_mirror_ops
||
5105 vml
[c
]->vdev_islog
||
5106 vml
[c
]->vdev_ishole
||
5107 vml
[c
]->vdev_isspare
||
5108 vml
[c
]->vdev_isl2cache
||
5109 !vdev_writeable(vml
[c
]) ||
5110 vml
[c
]->vdev_children
!= 0 ||
5111 vml
[c
]->vdev_state
!= VDEV_STATE_HEALTHY
||
5112 c
!= spa
->spa_root_vdev
->vdev_child
[c
]->vdev_id
) {
5113 error
= SET_ERROR(EINVAL
);
5117 if (vdev_dtl_required(vml
[c
])) {
5118 error
= SET_ERROR(EBUSY
);
5122 /* we need certain info from the top level */
5123 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_ARRAY
,
5124 vml
[c
]->vdev_top
->vdev_ms_array
) == 0);
5125 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_SHIFT
,
5126 vml
[c
]->vdev_top
->vdev_ms_shift
) == 0);
5127 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASIZE
,
5128 vml
[c
]->vdev_top
->vdev_asize
) == 0);
5129 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASHIFT
,
5130 vml
[c
]->vdev_top
->vdev_ashift
) == 0);
5132 /* transfer per-vdev ZAPs */
5133 ASSERT3U(vml
[c
]->vdev_leaf_zap
, !=, 0);
5134 VERIFY0(nvlist_add_uint64(child
[c
],
5135 ZPOOL_CONFIG_VDEV_LEAF_ZAP
, vml
[c
]->vdev_leaf_zap
));
5137 ASSERT3U(vml
[c
]->vdev_top
->vdev_top_zap
, !=, 0);
5138 VERIFY0(nvlist_add_uint64(child
[c
],
5139 ZPOOL_CONFIG_VDEV_TOP_ZAP
,
5140 vml
[c
]->vdev_parent
->vdev_top_zap
));
5144 kmem_free(vml
, children
* sizeof (vdev_t
*));
5145 kmem_free(glist
, children
* sizeof (uint64_t));
5146 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5149 /* stop writers from using the disks */
5150 for (c
= 0; c
< children
; c
++) {
5152 vml
[c
]->vdev_offline
= B_TRUE
;
5154 vdev_reopen(spa
->spa_root_vdev
);
5157 * Temporarily record the splitting vdevs in the spa config. This
5158 * will disappear once the config is regenerated.
5160 VERIFY(nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
5161 VERIFY(nvlist_add_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
5162 glist
, children
) == 0);
5163 kmem_free(glist
, children
* sizeof (uint64_t));
5165 mutex_enter(&spa
->spa_props_lock
);
5166 VERIFY(nvlist_add_nvlist(spa
->spa_config
, ZPOOL_CONFIG_SPLIT
,
5168 mutex_exit(&spa
->spa_props_lock
);
5169 spa
->spa_config_splitting
= nvl
;
5170 vdev_config_dirty(spa
->spa_root_vdev
);
5172 /* configure and create the new pool */
5173 VERIFY(nvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
, newname
) == 0);
5174 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
5175 exp
? POOL_STATE_EXPORTED
: POOL_STATE_ACTIVE
) == 0);
5176 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
5177 spa_version(spa
)) == 0);
5178 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
5179 spa
->spa_config_txg
) == 0);
5180 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
5181 spa_generate_guid(NULL
)) == 0);
5182 VERIFY0(nvlist_add_boolean(config
, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
));
5183 (void) nvlist_lookup_string(props
,
5184 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
5186 /* add the new pool to the namespace */
5187 newspa
= spa_add(newname
, config
, altroot
);
5188 newspa
->spa_avz_action
= AVZ_ACTION_REBUILD
;
5189 newspa
->spa_config_txg
= spa
->spa_config_txg
;
5190 spa_set_log_state(newspa
, SPA_LOG_CLEAR
);
5192 /* release the spa config lock, retaining the namespace lock */
5193 spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
5195 if (zio_injection_enabled
)
5196 zio_handle_panic_injection(spa
, FTAG
, 1);
5198 spa_activate(newspa
, spa_mode_global
);
5199 spa_async_suspend(newspa
);
5201 /* create the new pool from the disks of the original pool */
5202 error
= spa_load(newspa
, SPA_LOAD_IMPORT
, SPA_IMPORT_ASSEMBLE
, B_TRUE
);
5206 /* if that worked, generate a real config for the new pool */
5207 if (newspa
->spa_root_vdev
!= NULL
) {
5208 VERIFY(nvlist_alloc(&newspa
->spa_config_splitting
,
5209 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
5210 VERIFY(nvlist_add_uint64(newspa
->spa_config_splitting
,
5211 ZPOOL_CONFIG_SPLIT_GUID
, spa_guid(spa
)) == 0);
5212 spa_config_set(newspa
, spa_config_generate(newspa
, NULL
, -1ULL,
5217 if (props
!= NULL
) {
5218 spa_configfile_set(newspa
, props
, B_FALSE
);
5219 error
= spa_prop_set(newspa
, props
);
5224 /* flush everything */
5225 txg
= spa_vdev_config_enter(newspa
);
5226 vdev_config_dirty(newspa
->spa_root_vdev
);
5227 (void) spa_vdev_config_exit(newspa
, NULL
, txg
, 0, FTAG
);
5229 if (zio_injection_enabled
)
5230 zio_handle_panic_injection(spa
, FTAG
, 2);
5232 spa_async_resume(newspa
);
5234 /* finally, update the original pool's config */
5235 txg
= spa_vdev_config_enter(spa
);
5236 tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
5237 error
= dmu_tx_assign(tx
, TXG_WAIT
);
5240 for (c
= 0; c
< children
; c
++) {
5241 if (vml
[c
] != NULL
) {
5244 spa_history_log_internal(spa
, "detach", tx
,
5245 "vdev=%s", vml
[c
]->vdev_path
);
5250 spa
->spa_avz_action
= AVZ_ACTION_REBUILD
;
5251 vdev_config_dirty(spa
->spa_root_vdev
);
5252 spa
->spa_config_splitting
= NULL
;
5256 (void) spa_vdev_exit(spa
, NULL
, txg
, 0);
5258 if (zio_injection_enabled
)
5259 zio_handle_panic_injection(spa
, FTAG
, 3);
5261 /* split is complete; log a history record */
5262 spa_history_log_internal(newspa
, "split", NULL
,
5263 "from pool %s", spa_name(spa
));
5265 kmem_free(vml
, children
* sizeof (vdev_t
*));
5267 /* if we're not going to mount the filesystems in userland, export */
5269 error
= spa_export_common(newname
, POOL_STATE_EXPORTED
, NULL
,
5276 spa_deactivate(newspa
);
5279 txg
= spa_vdev_config_enter(spa
);
5281 /* re-online all offlined disks */
5282 for (c
= 0; c
< children
; c
++) {
5284 vml
[c
]->vdev_offline
= B_FALSE
;
5286 vdev_reopen(spa
->spa_root_vdev
);
5288 nvlist_free(spa
->spa_config_splitting
);
5289 spa
->spa_config_splitting
= NULL
;
5290 (void) spa_vdev_exit(spa
, NULL
, txg
, error
);
5292 kmem_free(vml
, children
* sizeof (vdev_t
*));
5297 spa_nvlist_lookup_by_guid(nvlist_t
**nvpp
, int count
, uint64_t target_guid
)
5301 for (i
= 0; i
< count
; i
++) {
5304 VERIFY(nvlist_lookup_uint64(nvpp
[i
], ZPOOL_CONFIG_GUID
,
5307 if (guid
== target_guid
)
5315 spa_vdev_remove_aux(nvlist_t
*config
, char *name
, nvlist_t
**dev
, int count
,
5316 nvlist_t
*dev_to_remove
)
5318 nvlist_t
**newdev
= NULL
;
5322 newdev
= kmem_alloc((count
- 1) * sizeof (void *), KM_SLEEP
);
5324 for (i
= 0, j
= 0; i
< count
; i
++) {
5325 if (dev
[i
] == dev_to_remove
)
5327 VERIFY(nvlist_dup(dev
[i
], &newdev
[j
++], KM_SLEEP
) == 0);
5330 VERIFY(nvlist_remove(config
, name
, DATA_TYPE_NVLIST_ARRAY
) == 0);
5331 VERIFY(nvlist_add_nvlist_array(config
, name
, newdev
, count
- 1) == 0);
5333 for (i
= 0; i
< count
- 1; i
++)
5334 nvlist_free(newdev
[i
]);
5337 kmem_free(newdev
, (count
- 1) * sizeof (void *));
5341 * Evacuate the device.
5344 spa_vdev_remove_evacuate(spa_t
*spa
, vdev_t
*vd
)
5349 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
5350 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
5351 ASSERT(vd
== vd
->vdev_top
);
5354 * Evacuate the device. We don't hold the config lock as writer
5355 * since we need to do I/O but we do keep the
5356 * spa_namespace_lock held. Once this completes the device
5357 * should no longer have any blocks allocated on it.
5359 if (vd
->vdev_islog
) {
5360 if (vd
->vdev_stat
.vs_alloc
!= 0)
5361 error
= spa_offline_log(spa
);
5363 error
= SET_ERROR(ENOTSUP
);
5370 * The evacuation succeeded. Remove any remaining MOS metadata
5371 * associated with this vdev, and wait for these changes to sync.
5373 ASSERT0(vd
->vdev_stat
.vs_alloc
);
5374 txg
= spa_vdev_config_enter(spa
);
5375 vd
->vdev_removing
= B_TRUE
;
5376 vdev_dirty_leaves(vd
, VDD_DTL
, txg
);
5377 vdev_config_dirty(vd
);
5378 spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
5384 * Complete the removal by cleaning up the namespace.
5387 spa_vdev_remove_from_namespace(spa_t
*spa
, vdev_t
*vd
)
5389 vdev_t
*rvd
= spa
->spa_root_vdev
;
5390 uint64_t id
= vd
->vdev_id
;
5391 boolean_t last_vdev
= (id
== (rvd
->vdev_children
- 1));
5393 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
5394 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
5395 ASSERT(vd
== vd
->vdev_top
);
5398 * Only remove any devices which are empty.
5400 if (vd
->vdev_stat
.vs_alloc
!= 0)
5403 (void) vdev_label_init(vd
, 0, VDEV_LABEL_REMOVE
);
5405 if (list_link_active(&vd
->vdev_state_dirty_node
))
5406 vdev_state_clean(vd
);
5407 if (list_link_active(&vd
->vdev_config_dirty_node
))
5408 vdev_config_clean(vd
);
5413 vdev_compact_children(rvd
);
5415 vd
= vdev_alloc_common(spa
, id
, 0, &vdev_hole_ops
);
5416 vdev_add_child(rvd
, vd
);
5418 vdev_config_dirty(rvd
);
5421 * Reassess the health of our root vdev.
5427 * Remove a device from the pool -
5429 * Removing a device from the vdev namespace requires several steps
5430 * and can take a significant amount of time. As a result we use
5431 * the spa_vdev_config_[enter/exit] functions which allow us to
5432 * grab and release the spa_config_lock while still holding the namespace
5433 * lock. During each step the configuration is synced out.
5435 * Currently, this supports removing only hot spares, slogs, and level 2 ARC
5439 spa_vdev_remove(spa_t
*spa
, uint64_t guid
, boolean_t unspare
)
5442 metaslab_group_t
*mg
;
5443 nvlist_t
**spares
, **l2cache
, *nv
;
5445 uint_t nspares
, nl2cache
;
5447 boolean_t locked
= MUTEX_HELD(&spa_namespace_lock
);
5449 ASSERT(spa_writeable(spa
));
5452 txg
= spa_vdev_enter(spa
);
5454 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
5456 if (spa
->spa_spares
.sav_vdevs
!= NULL
&&
5457 nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
5458 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0 &&
5459 (nv
= spa_nvlist_lookup_by_guid(spares
, nspares
, guid
)) != NULL
) {
5461 * Only remove the hot spare if it's not currently in use
5464 if (vd
== NULL
|| unspare
) {
5465 spa_vdev_remove_aux(spa
->spa_spares
.sav_config
,
5466 ZPOOL_CONFIG_SPARES
, spares
, nspares
, nv
);
5467 spa_load_spares(spa
);
5468 spa
->spa_spares
.sav_sync
= B_TRUE
;
5470 error
= SET_ERROR(EBUSY
);
5472 spa_event_notify(spa
, vd
, ESC_ZFS_VDEV_REMOVE_AUX
);
5473 } else if (spa
->spa_l2cache
.sav_vdevs
!= NULL
&&
5474 nvlist_lookup_nvlist_array(spa
->spa_l2cache
.sav_config
,
5475 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0 &&
5476 (nv
= spa_nvlist_lookup_by_guid(l2cache
, nl2cache
, guid
)) != NULL
) {
5478 * Cache devices can always be removed.
5480 spa_vdev_remove_aux(spa
->spa_l2cache
.sav_config
,
5481 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
, nv
);
5482 spa_load_l2cache(spa
);
5483 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
5484 spa_event_notify(spa
, vd
, ESC_ZFS_VDEV_REMOVE_AUX
);
5485 } else if (vd
!= NULL
&& vd
->vdev_islog
) {
5487 ASSERT(vd
== vd
->vdev_top
);
5492 * Stop allocating from this vdev.
5494 metaslab_group_passivate(mg
);
5497 * Wait for the youngest allocations and frees to sync,
5498 * and then wait for the deferral of those frees to finish.
5500 spa_vdev_config_exit(spa
, NULL
,
5501 txg
+ TXG_CONCURRENT_STATES
+ TXG_DEFER_SIZE
, 0, FTAG
);
5504 * Attempt to evacuate the vdev.
5506 error
= spa_vdev_remove_evacuate(spa
, vd
);
5508 txg
= spa_vdev_config_enter(spa
);
5511 * If we couldn't evacuate the vdev, unwind.
5514 metaslab_group_activate(mg
);
5515 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5519 * Clean up the vdev namespace.
5521 spa_vdev_remove_from_namespace(spa
, vd
);
5523 spa_event_notify(spa
, vd
, ESC_ZFS_VDEV_REMOVE_DEV
);
5524 } else if (vd
!= NULL
) {
5526 * Normal vdevs cannot be removed (yet).
5528 error
= SET_ERROR(ENOTSUP
);
5531 * There is no vdev of any kind with the specified guid.
5533 error
= SET_ERROR(ENOENT
);
5537 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5543 * Find any device that's done replacing, or a vdev marked 'unspare' that's
5544 * currently spared, so we can detach it.
5547 spa_vdev_resilver_done_hunt(vdev_t
*vd
)
5549 vdev_t
*newvd
, *oldvd
;
5552 for (c
= 0; c
< vd
->vdev_children
; c
++) {
5553 oldvd
= spa_vdev_resilver_done_hunt(vd
->vdev_child
[c
]);
5559 * Check for a completed replacement. We always consider the first
5560 * vdev in the list to be the oldest vdev, and the last one to be
5561 * the newest (see spa_vdev_attach() for how that works). In
5562 * the case where the newest vdev is faulted, we will not automatically
5563 * remove it after a resilver completes. This is OK as it will require
5564 * user intervention to determine which disk the admin wishes to keep.
5566 if (vd
->vdev_ops
== &vdev_replacing_ops
) {
5567 ASSERT(vd
->vdev_children
> 1);
5569 newvd
= vd
->vdev_child
[vd
->vdev_children
- 1];
5570 oldvd
= vd
->vdev_child
[0];
5572 if (vdev_dtl_empty(newvd
, DTL_MISSING
) &&
5573 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
5574 !vdev_dtl_required(oldvd
))
5579 * Check for a completed resilver with the 'unspare' flag set.
5581 if (vd
->vdev_ops
== &vdev_spare_ops
) {
5582 vdev_t
*first
= vd
->vdev_child
[0];
5583 vdev_t
*last
= vd
->vdev_child
[vd
->vdev_children
- 1];
5585 if (last
->vdev_unspare
) {
5588 } else if (first
->vdev_unspare
) {
5595 if (oldvd
!= NULL
&&
5596 vdev_dtl_empty(newvd
, DTL_MISSING
) &&
5597 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
5598 !vdev_dtl_required(oldvd
))
5602 * If there are more than two spares attached to a disk,
5603 * and those spares are not required, then we want to
5604 * attempt to free them up now so that they can be used
5605 * by other pools. Once we're back down to a single
5606 * disk+spare, we stop removing them.
5608 if (vd
->vdev_children
> 2) {
5609 newvd
= vd
->vdev_child
[1];
5611 if (newvd
->vdev_isspare
&& last
->vdev_isspare
&&
5612 vdev_dtl_empty(last
, DTL_MISSING
) &&
5613 vdev_dtl_empty(last
, DTL_OUTAGE
) &&
5614 !vdev_dtl_required(newvd
))
5623 spa_vdev_resilver_done(spa_t
*spa
)
5625 vdev_t
*vd
, *pvd
, *ppvd
;
5626 uint64_t guid
, sguid
, pguid
, ppguid
;
5628 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5630 while ((vd
= spa_vdev_resilver_done_hunt(spa
->spa_root_vdev
)) != NULL
) {
5631 pvd
= vd
->vdev_parent
;
5632 ppvd
= pvd
->vdev_parent
;
5633 guid
= vd
->vdev_guid
;
5634 pguid
= pvd
->vdev_guid
;
5635 ppguid
= ppvd
->vdev_guid
;
5638 * If we have just finished replacing a hot spared device, then
5639 * we need to detach the parent's first child (the original hot
5642 if (ppvd
->vdev_ops
== &vdev_spare_ops
&& pvd
->vdev_id
== 0 &&
5643 ppvd
->vdev_children
== 2) {
5644 ASSERT(pvd
->vdev_ops
== &vdev_replacing_ops
);
5645 sguid
= ppvd
->vdev_child
[1]->vdev_guid
;
5647 ASSERT(vd
->vdev_resilver_txg
== 0 || !vdev_dtl_required(vd
));
5649 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5650 if (spa_vdev_detach(spa
, guid
, pguid
, B_TRUE
) != 0)
5652 if (sguid
&& spa_vdev_detach(spa
, sguid
, ppguid
, B_TRUE
) != 0)
5654 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5657 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5661 * Update the stored path or FRU for this vdev.
5664 spa_vdev_set_common(spa_t
*spa
, uint64_t guid
, const char *value
,
5668 boolean_t sync
= B_FALSE
;
5670 ASSERT(spa_writeable(spa
));
5672 spa_vdev_state_enter(spa
, SCL_ALL
);
5674 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
5675 return (spa_vdev_state_exit(spa
, NULL
, ENOENT
));
5677 if (!vd
->vdev_ops
->vdev_op_leaf
)
5678 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
5681 if (strcmp(value
, vd
->vdev_path
) != 0) {
5682 spa_strfree(vd
->vdev_path
);
5683 vd
->vdev_path
= spa_strdup(value
);
5687 if (vd
->vdev_fru
== NULL
) {
5688 vd
->vdev_fru
= spa_strdup(value
);
5690 } else if (strcmp(value
, vd
->vdev_fru
) != 0) {
5691 spa_strfree(vd
->vdev_fru
);
5692 vd
->vdev_fru
= spa_strdup(value
);
5697 return (spa_vdev_state_exit(spa
, sync
? vd
: NULL
, 0));
5701 spa_vdev_setpath(spa_t
*spa
, uint64_t guid
, const char *newpath
)
5703 return (spa_vdev_set_common(spa
, guid
, newpath
, B_TRUE
));
5707 spa_vdev_setfru(spa_t
*spa
, uint64_t guid
, const char *newfru
)
5709 return (spa_vdev_set_common(spa
, guid
, newfru
, B_FALSE
));
5713 * ==========================================================================
5715 * ==========================================================================
5719 spa_scan_stop(spa_t
*spa
)
5721 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
5722 if (dsl_scan_resilvering(spa
->spa_dsl_pool
))
5723 return (SET_ERROR(EBUSY
));
5724 return (dsl_scan_cancel(spa
->spa_dsl_pool
));
5728 spa_scan(spa_t
*spa
, pool_scan_func_t func
)
5730 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
5732 if (func
>= POOL_SCAN_FUNCS
|| func
== POOL_SCAN_NONE
)
5733 return (SET_ERROR(ENOTSUP
));
5736 * If a resilver was requested, but there is no DTL on a
5737 * writeable leaf device, we have nothing to do.
5739 if (func
== POOL_SCAN_RESILVER
&&
5740 !vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
)) {
5741 spa_async_request(spa
, SPA_ASYNC_RESILVER_DONE
);
5745 return (dsl_scan(spa
->spa_dsl_pool
, func
));
5749 * ==========================================================================
5750 * SPA async task processing
5751 * ==========================================================================
5755 spa_async_remove(spa_t
*spa
, vdev_t
*vd
)
5759 if (vd
->vdev_remove_wanted
) {
5760 vd
->vdev_remove_wanted
= B_FALSE
;
5761 vd
->vdev_delayed_close
= B_FALSE
;
5762 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_REMOVED
, VDEV_AUX_NONE
);
5765 * We want to clear the stats, but we don't want to do a full
5766 * vdev_clear() as that will cause us to throw away
5767 * degraded/faulted state as well as attempt to reopen the
5768 * device, all of which is a waste.
5770 vd
->vdev_stat
.vs_read_errors
= 0;
5771 vd
->vdev_stat
.vs_write_errors
= 0;
5772 vd
->vdev_stat
.vs_checksum_errors
= 0;
5774 vdev_state_dirty(vd
->vdev_top
);
5777 for (c
= 0; c
< vd
->vdev_children
; c
++)
5778 spa_async_remove(spa
, vd
->vdev_child
[c
]);
5782 spa_async_probe(spa_t
*spa
, vdev_t
*vd
)
5786 if (vd
->vdev_probe_wanted
) {
5787 vd
->vdev_probe_wanted
= B_FALSE
;
5788 vdev_reopen(vd
); /* vdev_open() does the actual probe */
5791 for (c
= 0; c
< vd
->vdev_children
; c
++)
5792 spa_async_probe(spa
, vd
->vdev_child
[c
]);
5796 spa_async_autoexpand(spa_t
*spa
, vdev_t
*vd
)
5800 if (!spa
->spa_autoexpand
)
5803 for (c
= 0; c
< vd
->vdev_children
; c
++) {
5804 vdev_t
*cvd
= vd
->vdev_child
[c
];
5805 spa_async_autoexpand(spa
, cvd
);
5808 if (!vd
->vdev_ops
->vdev_op_leaf
|| vd
->vdev_physpath
== NULL
)
5811 spa_event_notify(vd
->vdev_spa
, vd
, ESC_ZFS_VDEV_AUTOEXPAND
);
5815 spa_async_thread(spa_t
*spa
)
5819 ASSERT(spa
->spa_sync_on
);
5821 mutex_enter(&spa
->spa_async_lock
);
5822 tasks
= spa
->spa_async_tasks
;
5823 spa
->spa_async_tasks
= 0;
5824 mutex_exit(&spa
->spa_async_lock
);
5827 * See if the config needs to be updated.
5829 if (tasks
& SPA_ASYNC_CONFIG_UPDATE
) {
5830 uint64_t old_space
, new_space
;
5832 mutex_enter(&spa_namespace_lock
);
5833 old_space
= metaslab_class_get_space(spa_normal_class(spa
));
5834 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
5835 new_space
= metaslab_class_get_space(spa_normal_class(spa
));
5836 mutex_exit(&spa_namespace_lock
);
5839 * If the pool grew as a result of the config update,
5840 * then log an internal history event.
5842 if (new_space
!= old_space
) {
5843 spa_history_log_internal(spa
, "vdev online", NULL
,
5844 "pool '%s' size: %llu(+%llu)",
5845 spa_name(spa
), new_space
, new_space
- old_space
);
5850 * See if any devices need to be marked REMOVED.
5852 if (tasks
& SPA_ASYNC_REMOVE
) {
5853 spa_vdev_state_enter(spa
, SCL_NONE
);
5854 spa_async_remove(spa
, spa
->spa_root_vdev
);
5855 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++)
5856 spa_async_remove(spa
, spa
->spa_l2cache
.sav_vdevs
[i
]);
5857 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
5858 spa_async_remove(spa
, spa
->spa_spares
.sav_vdevs
[i
]);
5859 (void) spa_vdev_state_exit(spa
, NULL
, 0);
5862 if ((tasks
& SPA_ASYNC_AUTOEXPAND
) && !spa_suspended(spa
)) {
5863 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
5864 spa_async_autoexpand(spa
, spa
->spa_root_vdev
);
5865 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
5869 * See if any devices need to be probed.
5871 if (tasks
& SPA_ASYNC_PROBE
) {
5872 spa_vdev_state_enter(spa
, SCL_NONE
);
5873 spa_async_probe(spa
, spa
->spa_root_vdev
);
5874 (void) spa_vdev_state_exit(spa
, NULL
, 0);
5878 * If any devices are done replacing, detach them.
5880 if (tasks
& SPA_ASYNC_RESILVER_DONE
)
5881 spa_vdev_resilver_done(spa
);
5884 * Kick off a resilver.
5886 if (tasks
& SPA_ASYNC_RESILVER
)
5887 dsl_resilver_restart(spa
->spa_dsl_pool
, 0);
5890 * Let the world know that we're done.
5892 mutex_enter(&spa
->spa_async_lock
);
5893 spa
->spa_async_thread
= NULL
;
5894 cv_broadcast(&spa
->spa_async_cv
);
5895 mutex_exit(&spa
->spa_async_lock
);
5900 spa_async_suspend(spa_t
*spa
)
5902 mutex_enter(&spa
->spa_async_lock
);
5903 spa
->spa_async_suspended
++;
5904 while (spa
->spa_async_thread
!= NULL
)
5905 cv_wait(&spa
->spa_async_cv
, &spa
->spa_async_lock
);
5906 mutex_exit(&spa
->spa_async_lock
);
5910 spa_async_resume(spa_t
*spa
)
5912 mutex_enter(&spa
->spa_async_lock
);
5913 ASSERT(spa
->spa_async_suspended
!= 0);
5914 spa
->spa_async_suspended
--;
5915 mutex_exit(&spa
->spa_async_lock
);
5919 spa_async_tasks_pending(spa_t
*spa
)
5921 uint_t non_config_tasks
;
5923 boolean_t config_task_suspended
;
5925 non_config_tasks
= spa
->spa_async_tasks
& ~SPA_ASYNC_CONFIG_UPDATE
;
5926 config_task
= spa
->spa_async_tasks
& SPA_ASYNC_CONFIG_UPDATE
;
5927 if (spa
->spa_ccw_fail_time
== 0) {
5928 config_task_suspended
= B_FALSE
;
5930 config_task_suspended
=
5931 (gethrtime() - spa
->spa_ccw_fail_time
) <
5932 ((hrtime_t
)zfs_ccw_retry_interval
* NANOSEC
);
5935 return (non_config_tasks
|| (config_task
&& !config_task_suspended
));
5939 spa_async_dispatch(spa_t
*spa
)
5941 mutex_enter(&spa
->spa_async_lock
);
5942 if (spa_async_tasks_pending(spa
) &&
5943 !spa
->spa_async_suspended
&&
5944 spa
->spa_async_thread
== NULL
&&
5946 spa
->spa_async_thread
= thread_create(NULL
, 0,
5947 spa_async_thread
, spa
, 0, &p0
, TS_RUN
, maxclsyspri
);
5948 mutex_exit(&spa
->spa_async_lock
);
5952 spa_async_request(spa_t
*spa
, int task
)
5954 zfs_dbgmsg("spa=%s async request task=%u", spa
->spa_name
, task
);
5955 mutex_enter(&spa
->spa_async_lock
);
5956 spa
->spa_async_tasks
|= task
;
5957 mutex_exit(&spa
->spa_async_lock
);
5961 * ==========================================================================
5962 * SPA syncing routines
5963 * ==========================================================================
5967 bpobj_enqueue_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
5970 bpobj_enqueue(bpo
, bp
, tx
);
5975 spa_free_sync_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
5979 zio_nowait(zio_free_sync(zio
, zio
->io_spa
, dmu_tx_get_txg(tx
), bp
,
5985 * Note: this simple function is not inlined to make it easier to dtrace the
5986 * amount of time spent syncing frees.
5989 spa_sync_frees(spa_t
*spa
, bplist_t
*bpl
, dmu_tx_t
*tx
)
5991 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
5992 bplist_iterate(bpl
, spa_free_sync_cb
, zio
, tx
);
5993 VERIFY(zio_wait(zio
) == 0);
5997 * Note: this simple function is not inlined to make it easier to dtrace the
5998 * amount of time spent syncing deferred frees.
6001 spa_sync_deferred_frees(spa_t
*spa
, dmu_tx_t
*tx
)
6003 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
6004 VERIFY3U(bpobj_iterate(&spa
->spa_deferred_bpobj
,
6005 spa_free_sync_cb
, zio
, tx
), ==, 0);
6006 VERIFY0(zio_wait(zio
));
6010 spa_sync_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
*nv
, dmu_tx_t
*tx
)
6012 char *packed
= NULL
;
6017 VERIFY(nvlist_size(nv
, &nvsize
, NV_ENCODE_XDR
) == 0);
6020 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
6021 * information. This avoids the dmu_buf_will_dirty() path and
6022 * saves us a pre-read to get data we don't actually care about.
6024 bufsize
= P2ROUNDUP((uint64_t)nvsize
, SPA_CONFIG_BLOCKSIZE
);
6025 packed
= vmem_alloc(bufsize
, KM_SLEEP
);
6027 VERIFY(nvlist_pack(nv
, &packed
, &nvsize
, NV_ENCODE_XDR
,
6029 bzero(packed
+ nvsize
, bufsize
- nvsize
);
6031 dmu_write(spa
->spa_meta_objset
, obj
, 0, bufsize
, packed
, tx
);
6033 vmem_free(packed
, bufsize
);
6035 VERIFY(0 == dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
));
6036 dmu_buf_will_dirty(db
, tx
);
6037 *(uint64_t *)db
->db_data
= nvsize
;
6038 dmu_buf_rele(db
, FTAG
);
6042 spa_sync_aux_dev(spa_t
*spa
, spa_aux_vdev_t
*sav
, dmu_tx_t
*tx
,
6043 const char *config
, const char *entry
)
6053 * Update the MOS nvlist describing the list of available devices.
6054 * spa_validate_aux() will have already made sure this nvlist is
6055 * valid and the vdevs are labeled appropriately.
6057 if (sav
->sav_object
== 0) {
6058 sav
->sav_object
= dmu_object_alloc(spa
->spa_meta_objset
,
6059 DMU_OT_PACKED_NVLIST
, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE
,
6060 sizeof (uint64_t), tx
);
6061 VERIFY(zap_update(spa
->spa_meta_objset
,
6062 DMU_POOL_DIRECTORY_OBJECT
, entry
, sizeof (uint64_t), 1,
6063 &sav
->sav_object
, tx
) == 0);
6066 VERIFY(nvlist_alloc(&nvroot
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
6067 if (sav
->sav_count
== 0) {
6068 VERIFY(nvlist_add_nvlist_array(nvroot
, config
, NULL
, 0) == 0);
6070 list
= kmem_alloc(sav
->sav_count
*sizeof (void *), KM_SLEEP
);
6071 for (i
= 0; i
< sav
->sav_count
; i
++)
6072 list
[i
] = vdev_config_generate(spa
, sav
->sav_vdevs
[i
],
6073 B_FALSE
, VDEV_CONFIG_L2CACHE
);
6074 VERIFY(nvlist_add_nvlist_array(nvroot
, config
, list
,
6075 sav
->sav_count
) == 0);
6076 for (i
= 0; i
< sav
->sav_count
; i
++)
6077 nvlist_free(list
[i
]);
6078 kmem_free(list
, sav
->sav_count
* sizeof (void *));
6081 spa_sync_nvlist(spa
, sav
->sav_object
, nvroot
, tx
);
6082 nvlist_free(nvroot
);
6084 sav
->sav_sync
= B_FALSE
;
6088 * Rebuild spa's all-vdev ZAP from the vdev ZAPs indicated in each vdev_t.
6089 * The all-vdev ZAP must be empty.
6092 spa_avz_build(vdev_t
*vd
, uint64_t avz
, dmu_tx_t
*tx
)
6094 spa_t
*spa
= vd
->vdev_spa
;
6097 if (vd
->vdev_top_zap
!= 0) {
6098 VERIFY0(zap_add_int(spa
->spa_meta_objset
, avz
,
6099 vd
->vdev_top_zap
, tx
));
6101 if (vd
->vdev_leaf_zap
!= 0) {
6102 VERIFY0(zap_add_int(spa
->spa_meta_objset
, avz
,
6103 vd
->vdev_leaf_zap
, tx
));
6105 for (i
= 0; i
< vd
->vdev_children
; i
++) {
6106 spa_avz_build(vd
->vdev_child
[i
], avz
, tx
);
6111 spa_sync_config_object(spa_t
*spa
, dmu_tx_t
*tx
)
6116 * If the pool is being imported from a pre-per-vdev-ZAP version of ZFS,
6117 * its config may not be dirty but we still need to build per-vdev ZAPs.
6118 * Similarly, if the pool is being assembled (e.g. after a split), we
6119 * need to rebuild the AVZ although the config may not be dirty.
6121 if (list_is_empty(&spa
->spa_config_dirty_list
) &&
6122 spa
->spa_avz_action
== AVZ_ACTION_NONE
)
6125 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
6127 ASSERT(spa
->spa_avz_action
== AVZ_ACTION_NONE
||
6128 spa
->spa_all_vdev_zaps
!= 0);
6130 if (spa
->spa_avz_action
== AVZ_ACTION_REBUILD
) {
6134 /* Make and build the new AVZ */
6135 uint64_t new_avz
= zap_create(spa
->spa_meta_objset
,
6136 DMU_OTN_ZAP_METADATA
, DMU_OT_NONE
, 0, tx
);
6137 spa_avz_build(spa
->spa_root_vdev
, new_avz
, tx
);
6139 /* Diff old AVZ with new one */
6140 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
6141 spa
->spa_all_vdev_zaps
);
6142 zap_cursor_retrieve(&zc
, &za
) == 0;
6143 zap_cursor_advance(&zc
)) {
6144 uint64_t vdzap
= za
.za_first_integer
;
6145 if (zap_lookup_int(spa
->spa_meta_objset
, new_avz
,
6148 * ZAP is listed in old AVZ but not in new one;
6151 VERIFY0(zap_destroy(spa
->spa_meta_objset
, vdzap
,
6156 zap_cursor_fini(&zc
);
6158 /* Destroy the old AVZ */
6159 VERIFY0(zap_destroy(spa
->spa_meta_objset
,
6160 spa
->spa_all_vdev_zaps
, tx
));
6162 /* Replace the old AVZ in the dir obj with the new one */
6163 VERIFY0(zap_update(spa
->spa_meta_objset
,
6164 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_VDEV_ZAP_MAP
,
6165 sizeof (new_avz
), 1, &new_avz
, tx
));
6167 spa
->spa_all_vdev_zaps
= new_avz
;
6168 } else if (spa
->spa_avz_action
== AVZ_ACTION_DESTROY
) {
6172 /* Walk through the AVZ and destroy all listed ZAPs */
6173 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
6174 spa
->spa_all_vdev_zaps
);
6175 zap_cursor_retrieve(&zc
, &za
) == 0;
6176 zap_cursor_advance(&zc
)) {
6177 uint64_t zap
= za
.za_first_integer
;
6178 VERIFY0(zap_destroy(spa
->spa_meta_objset
, zap
, tx
));
6181 zap_cursor_fini(&zc
);
6183 /* Destroy and unlink the AVZ itself */
6184 VERIFY0(zap_destroy(spa
->spa_meta_objset
,
6185 spa
->spa_all_vdev_zaps
, tx
));
6186 VERIFY0(zap_remove(spa
->spa_meta_objset
,
6187 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_VDEV_ZAP_MAP
, tx
));
6188 spa
->spa_all_vdev_zaps
= 0;
6191 if (spa
->spa_all_vdev_zaps
== 0) {
6192 spa
->spa_all_vdev_zaps
= zap_create_link(spa
->spa_meta_objset
,
6193 DMU_OTN_ZAP_METADATA
, DMU_POOL_DIRECTORY_OBJECT
,
6194 DMU_POOL_VDEV_ZAP_MAP
, tx
);
6196 spa
->spa_avz_action
= AVZ_ACTION_NONE
;
6198 /* Create ZAPs for vdevs that don't have them. */
6199 vdev_construct_zaps(spa
->spa_root_vdev
, tx
);
6201 config
= spa_config_generate(spa
, spa
->spa_root_vdev
,
6202 dmu_tx_get_txg(tx
), B_FALSE
);
6205 * If we're upgrading the spa version then make sure that
6206 * the config object gets updated with the correct version.
6208 if (spa
->spa_ubsync
.ub_version
< spa
->spa_uberblock
.ub_version
)
6209 fnvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
6210 spa
->spa_uberblock
.ub_version
);
6212 spa_config_exit(spa
, SCL_STATE
, FTAG
);
6214 nvlist_free(spa
->spa_config_syncing
);
6215 spa
->spa_config_syncing
= config
;
6217 spa_sync_nvlist(spa
, spa
->spa_config_object
, config
, tx
);
6221 spa_sync_version(void *arg
, dmu_tx_t
*tx
)
6223 uint64_t *versionp
= arg
;
6224 uint64_t version
= *versionp
;
6225 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
6228 * Setting the version is special cased when first creating the pool.
6230 ASSERT(tx
->tx_txg
!= TXG_INITIAL
);
6232 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
6233 ASSERT(version
>= spa_version(spa
));
6235 spa
->spa_uberblock
.ub_version
= version
;
6236 vdev_config_dirty(spa
->spa_root_vdev
);
6237 spa_history_log_internal(spa
, "set", tx
, "version=%lld", version
);
6241 * Set zpool properties.
6244 spa_sync_props(void *arg
, dmu_tx_t
*tx
)
6246 nvlist_t
*nvp
= arg
;
6247 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
6248 objset_t
*mos
= spa
->spa_meta_objset
;
6249 nvpair_t
*elem
= NULL
;
6251 mutex_enter(&spa
->spa_props_lock
);
6253 while ((elem
= nvlist_next_nvpair(nvp
, elem
))) {
6255 char *strval
, *fname
;
6257 const char *propname
;
6258 zprop_type_t proptype
;
6261 prop
= zpool_name_to_prop(nvpair_name(elem
));
6262 switch ((int)prop
) {
6265 * We checked this earlier in spa_prop_validate().
6267 ASSERT(zpool_prop_feature(nvpair_name(elem
)));
6269 fname
= strchr(nvpair_name(elem
), '@') + 1;
6270 VERIFY0(zfeature_lookup_name(fname
, &fid
));
6272 spa_feature_enable(spa
, fid
, tx
);
6273 spa_history_log_internal(spa
, "set", tx
,
6274 "%s=enabled", nvpair_name(elem
));
6277 case ZPOOL_PROP_VERSION
:
6278 intval
= fnvpair_value_uint64(elem
);
6280 * The version is synced seperatly before other
6281 * properties and should be correct by now.
6283 ASSERT3U(spa_version(spa
), >=, intval
);
6286 case ZPOOL_PROP_ALTROOT
:
6288 * 'altroot' is a non-persistent property. It should
6289 * have been set temporarily at creation or import time.
6291 ASSERT(spa
->spa_root
!= NULL
);
6294 case ZPOOL_PROP_READONLY
:
6295 case ZPOOL_PROP_CACHEFILE
:
6297 * 'readonly' and 'cachefile' are also non-persisitent
6301 case ZPOOL_PROP_COMMENT
:
6302 strval
= fnvpair_value_string(elem
);
6303 if (spa
->spa_comment
!= NULL
)
6304 spa_strfree(spa
->spa_comment
);
6305 spa
->spa_comment
= spa_strdup(strval
);
6307 * We need to dirty the configuration on all the vdevs
6308 * so that their labels get updated. It's unnecessary
6309 * to do this for pool creation since the vdev's
6310 * configuratoin has already been dirtied.
6312 if (tx
->tx_txg
!= TXG_INITIAL
)
6313 vdev_config_dirty(spa
->spa_root_vdev
);
6314 spa_history_log_internal(spa
, "set", tx
,
6315 "%s=%s", nvpair_name(elem
), strval
);
6319 * Set pool property values in the poolprops mos object.
6321 if (spa
->spa_pool_props_object
== 0) {
6322 spa
->spa_pool_props_object
=
6323 zap_create_link(mos
, DMU_OT_POOL_PROPS
,
6324 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_PROPS
,
6328 /* normalize the property name */
6329 propname
= zpool_prop_to_name(prop
);
6330 proptype
= zpool_prop_get_type(prop
);
6332 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
6333 ASSERT(proptype
== PROP_TYPE_STRING
);
6334 strval
= fnvpair_value_string(elem
);
6335 VERIFY0(zap_update(mos
,
6336 spa
->spa_pool_props_object
, propname
,
6337 1, strlen(strval
) + 1, strval
, tx
));
6338 spa_history_log_internal(spa
, "set", tx
,
6339 "%s=%s", nvpair_name(elem
), strval
);
6340 } else if (nvpair_type(elem
) == DATA_TYPE_UINT64
) {
6341 intval
= fnvpair_value_uint64(elem
);
6343 if (proptype
== PROP_TYPE_INDEX
) {
6345 VERIFY0(zpool_prop_index_to_string(
6346 prop
, intval
, &unused
));
6348 VERIFY0(zap_update(mos
,
6349 spa
->spa_pool_props_object
, propname
,
6350 8, 1, &intval
, tx
));
6351 spa_history_log_internal(spa
, "set", tx
,
6352 "%s=%lld", nvpair_name(elem
), intval
);
6354 ASSERT(0); /* not allowed */
6358 case ZPOOL_PROP_DELEGATION
:
6359 spa
->spa_delegation
= intval
;
6361 case ZPOOL_PROP_BOOTFS
:
6362 spa
->spa_bootfs
= intval
;
6364 case ZPOOL_PROP_FAILUREMODE
:
6365 spa
->spa_failmode
= intval
;
6367 case ZPOOL_PROP_AUTOEXPAND
:
6368 spa
->spa_autoexpand
= intval
;
6369 if (tx
->tx_txg
!= TXG_INITIAL
)
6370 spa_async_request(spa
,
6371 SPA_ASYNC_AUTOEXPAND
);
6373 case ZPOOL_PROP_DEDUPDITTO
:
6374 spa
->spa_dedup_ditto
= intval
;
6383 mutex_exit(&spa
->spa_props_lock
);
6387 * Perform one-time upgrade on-disk changes. spa_version() does not
6388 * reflect the new version this txg, so there must be no changes this
6389 * txg to anything that the upgrade code depends on after it executes.
6390 * Therefore this must be called after dsl_pool_sync() does the sync
6394 spa_sync_upgrades(spa_t
*spa
, dmu_tx_t
*tx
)
6396 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
6398 ASSERT(spa
->spa_sync_pass
== 1);
6400 rrw_enter(&dp
->dp_config_rwlock
, RW_WRITER
, FTAG
);
6402 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_ORIGIN
&&
6403 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_ORIGIN
) {
6404 dsl_pool_create_origin(dp
, tx
);
6406 /* Keeping the origin open increases spa_minref */
6407 spa
->spa_minref
+= 3;
6410 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_NEXT_CLONES
&&
6411 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_NEXT_CLONES
) {
6412 dsl_pool_upgrade_clones(dp
, tx
);
6415 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_DIR_CLONES
&&
6416 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_DIR_CLONES
) {
6417 dsl_pool_upgrade_dir_clones(dp
, tx
);
6419 /* Keeping the freedir open increases spa_minref */
6420 spa
->spa_minref
+= 3;
6423 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_FEATURES
&&
6424 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
6425 spa_feature_create_zap_objects(spa
, tx
);
6429 * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable
6430 * when possibility to use lz4 compression for metadata was added
6431 * Old pools that have this feature enabled must be upgraded to have
6432 * this feature active
6434 if (spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
6435 boolean_t lz4_en
= spa_feature_is_enabled(spa
,
6436 SPA_FEATURE_LZ4_COMPRESS
);
6437 boolean_t lz4_ac
= spa_feature_is_active(spa
,
6438 SPA_FEATURE_LZ4_COMPRESS
);
6440 if (lz4_en
&& !lz4_ac
)
6441 spa_feature_incr(spa
, SPA_FEATURE_LZ4_COMPRESS
, tx
);
6445 * If we haven't written the salt, do so now. Note that the
6446 * feature may not be activated yet, but that's fine since
6447 * the presence of this ZAP entry is backwards compatible.
6449 if (zap_contains(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
6450 DMU_POOL_CHECKSUM_SALT
) == ENOENT
) {
6451 VERIFY0(zap_add(spa
->spa_meta_objset
,
6452 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CHECKSUM_SALT
, 1,
6453 sizeof (spa
->spa_cksum_salt
.zcs_bytes
),
6454 spa
->spa_cksum_salt
.zcs_bytes
, tx
));
6457 rrw_exit(&dp
->dp_config_rwlock
, FTAG
);
6461 * Sync the specified transaction group. New blocks may be dirtied as
6462 * part of the process, so we iterate until it converges.
6465 spa_sync(spa_t
*spa
, uint64_t txg
)
6467 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
6468 objset_t
*mos
= spa
->spa_meta_objset
;
6469 bplist_t
*free_bpl
= &spa
->spa_free_bplist
[txg
& TXG_MASK
];
6470 metaslab_class_t
*mc
;
6471 vdev_t
*rvd
= spa
->spa_root_vdev
;
6475 uint32_t max_queue_depth
= zfs_vdev_async_write_max_active
*
6476 zfs_vdev_queue_depth_pct
/ 100;
6477 uint64_t queue_depth_total
;
6480 VERIFY(spa_writeable(spa
));
6483 * Lock out configuration changes.
6485 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
6487 spa
->spa_syncing_txg
= txg
;
6488 spa
->spa_sync_pass
= 0;
6490 mutex_enter(&spa
->spa_alloc_lock
);
6491 VERIFY0(avl_numnodes(&spa
->spa_alloc_tree
));
6492 mutex_exit(&spa
->spa_alloc_lock
);
6495 * If there are any pending vdev state changes, convert them
6496 * into config changes that go out with this transaction group.
6498 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
6499 while (list_head(&spa
->spa_state_dirty_list
) != NULL
) {
6501 * We need the write lock here because, for aux vdevs,
6502 * calling vdev_config_dirty() modifies sav_config.
6503 * This is ugly and will become unnecessary when we
6504 * eliminate the aux vdev wart by integrating all vdevs
6505 * into the root vdev tree.
6507 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
6508 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_WRITER
);
6509 while ((vd
= list_head(&spa
->spa_state_dirty_list
)) != NULL
) {
6510 vdev_state_clean(vd
);
6511 vdev_config_dirty(vd
);
6513 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
6514 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
6516 spa_config_exit(spa
, SCL_STATE
, FTAG
);
6518 tx
= dmu_tx_create_assigned(dp
, txg
);
6520 spa
->spa_sync_starttime
= gethrtime();
6521 taskq_cancel_id(system_taskq
, spa
->spa_deadman_tqid
);
6522 spa
->spa_deadman_tqid
= taskq_dispatch_delay(system_taskq
,
6523 spa_deadman
, spa
, TQ_SLEEP
, ddi_get_lbolt() +
6524 NSEC_TO_TICK(spa
->spa_deadman_synctime
));
6527 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
6528 * set spa_deflate if we have no raid-z vdevs.
6530 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_RAIDZ_DEFLATE
&&
6531 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
6534 for (i
= 0; i
< rvd
->vdev_children
; i
++) {
6535 vd
= rvd
->vdev_child
[i
];
6536 if (vd
->vdev_deflate_ratio
!= SPA_MINBLOCKSIZE
)
6539 if (i
== rvd
->vdev_children
) {
6540 spa
->spa_deflate
= TRUE
;
6541 VERIFY(0 == zap_add(spa
->spa_meta_objset
,
6542 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
6543 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
));
6548 * Set the top-level vdev's max queue depth. Evaluate each
6549 * top-level's async write queue depth in case it changed.
6550 * The max queue depth will not change in the middle of syncing
6553 queue_depth_total
= 0;
6554 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
6555 vdev_t
*tvd
= rvd
->vdev_child
[c
];
6556 metaslab_group_t
*mg
= tvd
->vdev_mg
;
6558 if (mg
== NULL
|| mg
->mg_class
!= spa_normal_class(spa
) ||
6559 !metaslab_group_initialized(mg
))
6563 * It is safe to do a lock-free check here because only async
6564 * allocations look at mg_max_alloc_queue_depth, and async
6565 * allocations all happen from spa_sync().
6567 ASSERT0(refcount_count(&mg
->mg_alloc_queue_depth
));
6568 mg
->mg_max_alloc_queue_depth
= max_queue_depth
;
6569 queue_depth_total
+= mg
->mg_max_alloc_queue_depth
;
6571 mc
= spa_normal_class(spa
);
6572 ASSERT0(refcount_count(&mc
->mc_alloc_slots
));
6573 mc
->mc_alloc_max_slots
= queue_depth_total
;
6574 mc
->mc_alloc_throttle_enabled
= zio_dva_throttle_enabled
;
6576 ASSERT3U(mc
->mc_alloc_max_slots
, <=,
6577 max_queue_depth
* rvd
->vdev_children
);
6580 * Iterate to convergence.
6583 int pass
= ++spa
->spa_sync_pass
;
6585 spa_sync_config_object(spa
, tx
);
6586 spa_sync_aux_dev(spa
, &spa
->spa_spares
, tx
,
6587 ZPOOL_CONFIG_SPARES
, DMU_POOL_SPARES
);
6588 spa_sync_aux_dev(spa
, &spa
->spa_l2cache
, tx
,
6589 ZPOOL_CONFIG_L2CACHE
, DMU_POOL_L2CACHE
);
6590 spa_errlog_sync(spa
, txg
);
6591 dsl_pool_sync(dp
, txg
);
6593 if (pass
< zfs_sync_pass_deferred_free
) {
6594 spa_sync_frees(spa
, free_bpl
, tx
);
6597 * We can not defer frees in pass 1, because
6598 * we sync the deferred frees later in pass 1.
6600 ASSERT3U(pass
, >, 1);
6601 bplist_iterate(free_bpl
, bpobj_enqueue_cb
,
6602 &spa
->spa_deferred_bpobj
, tx
);
6606 dsl_scan_sync(dp
, tx
);
6608 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, txg
)))
6612 spa_sync_upgrades(spa
, tx
);
6614 spa
->spa_uberblock
.ub_rootbp
.blk_birth
);
6616 * Note: We need to check if the MOS is dirty
6617 * because we could have marked the MOS dirty
6618 * without updating the uberblock (e.g. if we
6619 * have sync tasks but no dirty user data). We
6620 * need to check the uberblock's rootbp because
6621 * it is updated if we have synced out dirty
6622 * data (though in this case the MOS will most
6623 * likely also be dirty due to second order
6624 * effects, we don't want to rely on that here).
6626 if (spa
->spa_uberblock
.ub_rootbp
.blk_birth
< txg
&&
6627 !dmu_objset_is_dirty(mos
, txg
)) {
6629 * Nothing changed on the first pass,
6630 * therefore this TXG is a no-op. Avoid
6631 * syncing deferred frees, so that we
6632 * can keep this TXG as a no-op.
6634 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
,
6636 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
6637 ASSERT(txg_list_empty(&dp
->dp_sync_tasks
, txg
));
6640 spa_sync_deferred_frees(spa
, tx
);
6643 } while (dmu_objset_is_dirty(mos
, txg
));
6646 if (!list_is_empty(&spa
->spa_config_dirty_list
)) {
6648 * Make sure that the number of ZAPs for all the vdevs matches
6649 * the number of ZAPs in the per-vdev ZAP list. This only gets
6650 * called if the config is dirty; otherwise there may be
6651 * outstanding AVZ operations that weren't completed in
6652 * spa_sync_config_object.
6654 uint64_t all_vdev_zap_entry_count
;
6655 ASSERT0(zap_count(spa
->spa_meta_objset
,
6656 spa
->spa_all_vdev_zaps
, &all_vdev_zap_entry_count
));
6657 ASSERT3U(vdev_count_verify_zaps(spa
->spa_root_vdev
), ==,
6658 all_vdev_zap_entry_count
);
6663 * Rewrite the vdev configuration (which includes the uberblock)
6664 * to commit the transaction group.
6666 * If there are no dirty vdevs, we sync the uberblock to a few
6667 * random top-level vdevs that are known to be visible in the
6668 * config cache (see spa_vdev_add() for a complete description).
6669 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
6673 * We hold SCL_STATE to prevent vdev open/close/etc.
6674 * while we're attempting to write the vdev labels.
6676 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
6678 if (list_is_empty(&spa
->spa_config_dirty_list
)) {
6679 vdev_t
*svd
[SPA_DVAS_PER_BP
];
6681 int children
= rvd
->vdev_children
;
6682 int c0
= spa_get_random(children
);
6684 for (c
= 0; c
< children
; c
++) {
6685 vd
= rvd
->vdev_child
[(c0
+ c
) % children
];
6686 if (vd
->vdev_ms_array
== 0 || vd
->vdev_islog
)
6688 svd
[svdcount
++] = vd
;
6689 if (svdcount
== SPA_DVAS_PER_BP
)
6692 error
= vdev_config_sync(svd
, svdcount
, txg
);
6694 error
= vdev_config_sync(rvd
->vdev_child
,
6695 rvd
->vdev_children
, txg
);
6699 spa
->spa_last_synced_guid
= rvd
->vdev_guid
;
6701 spa_config_exit(spa
, SCL_STATE
, FTAG
);
6705 zio_suspend(spa
, NULL
);
6706 zio_resume_wait(spa
);
6710 taskq_cancel_id(system_taskq
, spa
->spa_deadman_tqid
);
6711 spa
->spa_deadman_tqid
= 0;
6714 * Clear the dirty config list.
6716 while ((vd
= list_head(&spa
->spa_config_dirty_list
)) != NULL
)
6717 vdev_config_clean(vd
);
6720 * Now that the new config has synced transactionally,
6721 * let it become visible to the config cache.
6723 if (spa
->spa_config_syncing
!= NULL
) {
6724 spa_config_set(spa
, spa
->spa_config_syncing
);
6725 spa
->spa_config_txg
= txg
;
6726 spa
->spa_config_syncing
= NULL
;
6729 spa
->spa_ubsync
= spa
->spa_uberblock
;
6731 dsl_pool_sync_done(dp
, txg
);
6733 mutex_enter(&spa
->spa_alloc_lock
);
6734 VERIFY0(avl_numnodes(&spa
->spa_alloc_tree
));
6735 mutex_exit(&spa
->spa_alloc_lock
);
6738 * Update usable space statistics.
6740 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, TXG_CLEAN(txg
))))
6741 vdev_sync_done(vd
, txg
);
6743 spa_update_dspace(spa
);
6746 * It had better be the case that we didn't dirty anything
6747 * since vdev_config_sync().
6749 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
, txg
));
6750 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
6751 ASSERT(txg_list_empty(&spa
->spa_vdev_txg_list
, txg
));
6753 spa
->spa_sync_pass
= 0;
6755 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
6757 spa_handle_ignored_writes(spa
);
6760 * If any async tasks have been requested, kick them off.
6762 spa_async_dispatch(spa
);
6766 * Sync all pools. We don't want to hold the namespace lock across these
6767 * operations, so we take a reference on the spa_t and drop the lock during the
6771 spa_sync_allpools(void)
6774 mutex_enter(&spa_namespace_lock
);
6775 while ((spa
= spa_next(spa
)) != NULL
) {
6776 if (spa_state(spa
) != POOL_STATE_ACTIVE
||
6777 !spa_writeable(spa
) || spa_suspended(spa
))
6779 spa_open_ref(spa
, FTAG
);
6780 mutex_exit(&spa_namespace_lock
);
6781 txg_wait_synced(spa_get_dsl(spa
), 0);
6782 mutex_enter(&spa_namespace_lock
);
6783 spa_close(spa
, FTAG
);
6785 mutex_exit(&spa_namespace_lock
);
6789 * ==========================================================================
6790 * Miscellaneous routines
6791 * ==========================================================================
6795 * Remove all pools in the system.
6803 * Remove all cached state. All pools should be closed now,
6804 * so every spa in the AVL tree should be unreferenced.
6806 mutex_enter(&spa_namespace_lock
);
6807 while ((spa
= spa_next(NULL
)) != NULL
) {
6809 * Stop async tasks. The async thread may need to detach
6810 * a device that's been replaced, which requires grabbing
6811 * spa_namespace_lock, so we must drop it here.
6813 spa_open_ref(spa
, FTAG
);
6814 mutex_exit(&spa_namespace_lock
);
6815 spa_async_suspend(spa
);
6816 mutex_enter(&spa_namespace_lock
);
6817 spa_close(spa
, FTAG
);
6819 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
6821 spa_deactivate(spa
);
6825 mutex_exit(&spa_namespace_lock
);
6829 spa_lookup_by_guid(spa_t
*spa
, uint64_t guid
, boolean_t aux
)
6834 if ((vd
= vdev_lookup_by_guid(spa
->spa_root_vdev
, guid
)) != NULL
)
6838 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
6839 vd
= spa
->spa_l2cache
.sav_vdevs
[i
];
6840 if (vd
->vdev_guid
== guid
)
6844 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
6845 vd
= spa
->spa_spares
.sav_vdevs
[i
];
6846 if (vd
->vdev_guid
== guid
)
6855 spa_upgrade(spa_t
*spa
, uint64_t version
)
6857 ASSERT(spa_writeable(spa
));
6859 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6862 * This should only be called for a non-faulted pool, and since a
6863 * future version would result in an unopenable pool, this shouldn't be
6866 ASSERT(SPA_VERSION_IS_SUPPORTED(spa
->spa_uberblock
.ub_version
));
6867 ASSERT3U(version
, >=, spa
->spa_uberblock
.ub_version
);
6869 spa
->spa_uberblock
.ub_version
= version
;
6870 vdev_config_dirty(spa
->spa_root_vdev
);
6872 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6874 txg_wait_synced(spa_get_dsl(spa
), 0);
6878 spa_has_spare(spa_t
*spa
, uint64_t guid
)
6882 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
6884 for (i
= 0; i
< sav
->sav_count
; i
++)
6885 if (sav
->sav_vdevs
[i
]->vdev_guid
== guid
)
6888 for (i
= 0; i
< sav
->sav_npending
; i
++) {
6889 if (nvlist_lookup_uint64(sav
->sav_pending
[i
], ZPOOL_CONFIG_GUID
,
6890 &spareguid
) == 0 && spareguid
== guid
)
6898 * Check if a pool has an active shared spare device.
6899 * Note: reference count of an active spare is 2, as a spare and as a replace
6902 spa_has_active_shared_spare(spa_t
*spa
)
6906 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
6908 for (i
= 0; i
< sav
->sav_count
; i
++) {
6909 if (spa_spare_exists(sav
->sav_vdevs
[i
]->vdev_guid
, &pool
,
6910 &refcnt
) && pool
!= 0ULL && pool
== spa_guid(spa
) &&
6919 * Post a zevent corresponding to the given sysevent. The 'name' must be one
6920 * of the event definitions in sys/sysevent/eventdefs.h. The payload will be
6921 * filled in from the spa and (optionally) the vdev. This doesn't do anything
6922 * in the userland libzpool, as we don't want consumers to misinterpret ztest
6923 * or zdb as real changes.
6926 spa_event_notify(spa_t
*spa
, vdev_t
*vd
, const char *name
)
6928 zfs_post_sysevent(spa
, vd
, name
);
6931 #if defined(_KERNEL) && defined(HAVE_SPL)
6932 /* state manipulation functions */
6933 EXPORT_SYMBOL(spa_open
);
6934 EXPORT_SYMBOL(spa_open_rewind
);
6935 EXPORT_SYMBOL(spa_get_stats
);
6936 EXPORT_SYMBOL(spa_create
);
6937 EXPORT_SYMBOL(spa_import
);
6938 EXPORT_SYMBOL(spa_tryimport
);
6939 EXPORT_SYMBOL(spa_destroy
);
6940 EXPORT_SYMBOL(spa_export
);
6941 EXPORT_SYMBOL(spa_reset
);
6942 EXPORT_SYMBOL(spa_async_request
);
6943 EXPORT_SYMBOL(spa_async_suspend
);
6944 EXPORT_SYMBOL(spa_async_resume
);
6945 EXPORT_SYMBOL(spa_inject_addref
);
6946 EXPORT_SYMBOL(spa_inject_delref
);
6947 EXPORT_SYMBOL(spa_scan_stat_init
);
6948 EXPORT_SYMBOL(spa_scan_get_stats
);
6950 /* device maniion */
6951 EXPORT_SYMBOL(spa_vdev_add
);
6952 EXPORT_SYMBOL(spa_vdev_attach
);
6953 EXPORT_SYMBOL(spa_vdev_detach
);
6954 EXPORT_SYMBOL(spa_vdev_remove
);
6955 EXPORT_SYMBOL(spa_vdev_setpath
);
6956 EXPORT_SYMBOL(spa_vdev_setfru
);
6957 EXPORT_SYMBOL(spa_vdev_split_mirror
);
6959 /* spare statech is global across all pools) */
6960 EXPORT_SYMBOL(spa_spare_add
);
6961 EXPORT_SYMBOL(spa_spare_remove
);
6962 EXPORT_SYMBOL(spa_spare_exists
);
6963 EXPORT_SYMBOL(spa_spare_activate
);
6965 /* L2ARC statech is global across all pools) */
6966 EXPORT_SYMBOL(spa_l2cache_add
);
6967 EXPORT_SYMBOL(spa_l2cache_remove
);
6968 EXPORT_SYMBOL(spa_l2cache_exists
);
6969 EXPORT_SYMBOL(spa_l2cache_activate
);
6970 EXPORT_SYMBOL(spa_l2cache_drop
);
6973 EXPORT_SYMBOL(spa_scan
);
6974 EXPORT_SYMBOL(spa_scan_stop
);
6977 EXPORT_SYMBOL(spa_sync
); /* only for DMU use */
6978 EXPORT_SYMBOL(spa_sync_allpools
);
6981 EXPORT_SYMBOL(spa_prop_set
);
6982 EXPORT_SYMBOL(spa_prop_get
);
6983 EXPORT_SYMBOL(spa_prop_clear_bootfs
);
6985 /* asynchronous event notification */
6986 EXPORT_SYMBOL(spa_event_notify
);
6989 #if defined(_KERNEL) && defined(HAVE_SPL)
6990 module_param(spa_load_verify_maxinflight
, int, 0644);
6991 MODULE_PARM_DESC(spa_load_verify_maxinflight
,
6992 "Max concurrent traversal I/Os while verifying pool during import -X");
6994 module_param(spa_load_verify_metadata
, int, 0644);
6995 MODULE_PARM_DESC(spa_load_verify_metadata
,
6996 "Set to traverse metadata on pool import");
6998 module_param(spa_load_verify_data
, int, 0644);
6999 MODULE_PARM_DESC(spa_load_verify_data
,
7000 "Set to traverse data on pool import");
7002 module_param(zio_taskq_batch_pct
, uint
, 0444);
7003 MODULE_PARM_DESC(zio_taskq_batch_pct
,
7004 "Percentage of CPUs to run an IO worker thread");