4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2017 by Delphix. All rights reserved.
25 * Copyright (c) 2018, Nexenta Systems, Inc. All rights reserved.
26 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
27 * Copyright 2013 Saso Kiselkov. All rights reserved.
28 * Copyright (c) 2014 Integros [integros.com]
29 * Copyright 2016 Toomas Soome <tsoome@me.com>
30 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
31 * Copyright 2018 Joyent, Inc.
32 * Copyright (c) 2017 Datto Inc.
33 * Copyright 2017 Joyent, Inc.
34 * Copyright (c) 2017, Intel Corporation.
38 * SPA: Storage Pool Allocator
40 * This file contains all the routines used when modifying on-disk SPA state.
41 * This includes opening, importing, destroying, exporting a pool, and syncing a
45 #include <sys/zfs_context.h>
46 #include <sys/fm/fs/zfs.h>
47 #include <sys/spa_impl.h>
49 #include <sys/zio_checksum.h>
51 #include <sys/dmu_tx.h>
55 #include <sys/vdev_impl.h>
56 #include <sys/vdev_removal.h>
57 #include <sys/vdev_indirect_mapping.h>
58 #include <sys/vdev_indirect_births.h>
59 #include <sys/vdev_initialize.h>
60 #include <sys/vdev_disk.h>
61 #include <sys/metaslab.h>
62 #include <sys/metaslab_impl.h>
64 #include <sys/uberblock_impl.h>
67 #include <sys/bpobj.h>
68 #include <sys/dmu_traverse.h>
69 #include <sys/dmu_objset.h>
70 #include <sys/unique.h>
71 #include <sys/dsl_pool.h>
72 #include <sys/dsl_dataset.h>
73 #include <sys/dsl_dir.h>
74 #include <sys/dsl_prop.h>
75 #include <sys/dsl_synctask.h>
76 #include <sys/fs/zfs.h>
78 #include <sys/callb.h>
79 #include <sys/systeminfo.h>
80 #include <sys/spa_boot.h>
81 #include <sys/zfs_ioctl.h>
82 #include <sys/dsl_scan.h>
83 #include <sys/zfeature.h>
84 #include <sys/dsl_destroy.h>
88 #include <sys/fm/protocol.h>
89 #include <sys/fm/util.h>
90 #include <sys/callb.h>
95 #include "zfs_comutil.h"
98 * The interval, in seconds, at which failed configuration cache file writes
101 int zfs_ccw_retry_interval
= 300;
103 typedef enum zti_modes
{
104 ZTI_MODE_FIXED
, /* value is # of threads (min 1) */
105 ZTI_MODE_BATCH
, /* cpu-intensive; value is ignored */
106 ZTI_MODE_NULL
, /* don't create a taskq */
110 #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) }
111 #define ZTI_PCT(n) { ZTI_MODE_ONLINE_PERCENT, (n), 1 }
112 #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 }
113 #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 }
115 #define ZTI_N(n) ZTI_P(n, 1)
116 #define ZTI_ONE ZTI_N(1)
118 typedef struct zio_taskq_info
{
119 zti_modes_t zti_mode
;
124 static const char *const zio_taskq_types
[ZIO_TASKQ_TYPES
] = {
125 "iss", "iss_h", "int", "int_h"
129 * This table defines the taskq settings for each ZFS I/O type. When
130 * initializing a pool, we use this table to create an appropriately sized
131 * taskq. Some operations are low volume and therefore have a small, static
132 * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
133 * macros. Other operations process a large amount of data; the ZTI_BATCH
134 * macro causes us to create a taskq oriented for throughput. Some operations
135 * are so high frequency and short-lived that the taskq itself can become a a
136 * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
137 * additional degree of parallelism specified by the number of threads per-
138 * taskq and the number of taskqs; when dispatching an event in this case, the
139 * particular taskq is chosen at random.
141 * The different taskq priorities are to handle the different contexts (issue
142 * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that
143 * need to be handled with minimum delay.
145 const zio_taskq_info_t zio_taskqs
[ZIO_TYPES
][ZIO_TASKQ_TYPES
] = {
146 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
147 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* NULL */
148 { ZTI_N(8), ZTI_NULL
, ZTI_P(12, 8), ZTI_NULL
}, /* READ */
149 { ZTI_BATCH
, ZTI_N(5), ZTI_P(12, 8), ZTI_N(5) }, /* WRITE */
150 { ZTI_P(12, 8), ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* FREE */
151 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* CLAIM */
152 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* IOCTL */
155 static void spa_sync_version(void *arg
, dmu_tx_t
*tx
);
156 static void spa_sync_props(void *arg
, dmu_tx_t
*tx
);
157 static boolean_t
spa_has_active_shared_spare(spa_t
*spa
);
158 static int spa_load_impl(spa_t
*spa
, spa_import_type_t type
, char **ereport
);
159 static void spa_vdev_resilver_done(spa_t
*spa
);
161 uint_t zio_taskq_batch_pct
= 75; /* 1 thread per cpu in pset */
162 boolean_t zio_taskq_sysdc
= B_TRUE
; /* use SDC scheduling class */
163 uint_t zio_taskq_basedc
= 80; /* base duty cycle */
165 boolean_t spa_create_process
= B_TRUE
; /* no process ==> no sysdc */
168 * Report any spa_load_verify errors found, but do not fail spa_load.
169 * This is used by zdb to analyze non-idle pools.
171 boolean_t spa_load_verify_dryrun
= B_FALSE
;
174 * This (illegal) pool name is used when temporarily importing a spa_t in order
175 * to get the vdev stats associated with the imported devices.
177 #define TRYIMPORT_NAME "$import"
180 * For debugging purposes: print out vdev tree during pool import.
182 int spa_load_print_vdev_tree
= B_FALSE
;
185 * A non-zero value for zfs_max_missing_tvds means that we allow importing
186 * pools with missing top-level vdevs. This is strictly intended for advanced
187 * pool recovery cases since missing data is almost inevitable. Pools with
188 * missing devices can only be imported read-only for safety reasons, and their
189 * fail-mode will be automatically set to "continue".
191 * With 1 missing vdev we should be able to import the pool and mount all
192 * datasets. User data that was not modified after the missing device has been
193 * added should be recoverable. This means that snapshots created prior to the
194 * addition of that device should be completely intact.
196 * With 2 missing vdevs, some datasets may fail to mount since there are
197 * dataset statistics that are stored as regular metadata. Some data might be
198 * recoverable if those vdevs were added recently.
200 * With 3 or more missing vdevs, the pool is severely damaged and MOS entries
201 * may be missing entirely. Chances of data recovery are very low. Note that
202 * there are also risks of performing an inadvertent rewind as we might be
203 * missing all the vdevs with the latest uberblocks.
205 unsigned long zfs_max_missing_tvds
= 0;
208 * The parameters below are similar to zfs_max_missing_tvds but are only
209 * intended for a preliminary open of the pool with an untrusted config which
210 * might be incomplete or out-dated.
212 * We are more tolerant for pools opened from a cachefile since we could have
213 * an out-dated cachefile where a device removal was not registered.
214 * We could have set the limit arbitrarily high but in the case where devices
215 * are really missing we would want to return the proper error codes; we chose
216 * SPA_DVAS_PER_BP - 1 so that some copies of the MOS would still be available
217 * and we get a chance to retrieve the trusted config.
219 uint64_t zfs_max_missing_tvds_cachefile
= SPA_DVAS_PER_BP
- 1;
222 * In the case where config was assembled by scanning device paths (/dev/dsks
223 * by default) we are less tolerant since all the existing devices should have
224 * been detected and we want spa_load to return the right error codes.
226 uint64_t zfs_max_missing_tvds_scan
= 0;
229 * Debugging aid that pauses spa_sync() towards the end.
231 boolean_t zfs_pause_spa_sync
= B_FALSE
;
234 * ==========================================================================
235 * SPA properties routines
236 * ==========================================================================
240 * Add a (source=src, propname=propval) list to an nvlist.
243 spa_prop_add_list(nvlist_t
*nvl
, zpool_prop_t prop
, char *strval
,
244 uint64_t intval
, zprop_source_t src
)
246 const char *propname
= zpool_prop_to_name(prop
);
249 VERIFY(nvlist_alloc(&propval
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
250 VERIFY(nvlist_add_uint64(propval
, ZPROP_SOURCE
, src
) == 0);
253 VERIFY(nvlist_add_string(propval
, ZPROP_VALUE
, strval
) == 0);
255 VERIFY(nvlist_add_uint64(propval
, ZPROP_VALUE
, intval
) == 0);
257 VERIFY(nvlist_add_nvlist(nvl
, propname
, propval
) == 0);
258 nvlist_free(propval
);
262 * Get property values from the spa configuration.
265 spa_prop_get_config(spa_t
*spa
, nvlist_t
**nvp
)
267 vdev_t
*rvd
= spa
->spa_root_vdev
;
268 dsl_pool_t
*pool
= spa
->spa_dsl_pool
;
269 uint64_t size
, alloc
, cap
, version
;
270 const zprop_source_t src
= ZPROP_SRC_NONE
;
271 spa_config_dirent_t
*dp
;
272 metaslab_class_t
*mc
= spa_normal_class(spa
);
274 ASSERT(MUTEX_HELD(&spa
->spa_props_lock
));
277 alloc
= metaslab_class_get_alloc(mc
);
278 alloc
+= metaslab_class_get_alloc(spa_special_class(spa
));
279 alloc
+= metaslab_class_get_alloc(spa_dedup_class(spa
));
281 size
= metaslab_class_get_space(mc
);
282 size
+= metaslab_class_get_space(spa_special_class(spa
));
283 size
+= metaslab_class_get_space(spa_dedup_class(spa
));
285 spa_prop_add_list(*nvp
, ZPOOL_PROP_NAME
, spa_name(spa
), 0, src
);
286 spa_prop_add_list(*nvp
, ZPOOL_PROP_SIZE
, NULL
, size
, src
);
287 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALLOCATED
, NULL
, alloc
, src
);
288 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREE
, NULL
,
290 spa_prop_add_list(*nvp
, ZPOOL_PROP_CHECKPOINT
, NULL
,
291 spa
->spa_checkpoint_info
.sci_dspace
, src
);
293 spa_prop_add_list(*nvp
, ZPOOL_PROP_FRAGMENTATION
, NULL
,
294 metaslab_class_fragmentation(mc
), src
);
295 spa_prop_add_list(*nvp
, ZPOOL_PROP_EXPANDSZ
, NULL
,
296 metaslab_class_expandable_space(mc
), src
);
297 spa_prop_add_list(*nvp
, ZPOOL_PROP_READONLY
, NULL
,
298 (spa_mode(spa
) == FREAD
), src
);
300 cap
= (size
== 0) ? 0 : (alloc
* 100 / size
);
301 spa_prop_add_list(*nvp
, ZPOOL_PROP_CAPACITY
, NULL
, cap
, src
);
303 spa_prop_add_list(*nvp
, ZPOOL_PROP_DEDUPRATIO
, NULL
,
304 ddt_get_pool_dedup_ratio(spa
), src
);
306 spa_prop_add_list(*nvp
, ZPOOL_PROP_HEALTH
, NULL
,
307 rvd
->vdev_state
, src
);
309 version
= spa_version(spa
);
310 if (version
== zpool_prop_default_numeric(ZPOOL_PROP_VERSION
)) {
311 spa_prop_add_list(*nvp
, ZPOOL_PROP_VERSION
, NULL
,
312 version
, ZPROP_SRC_DEFAULT
);
314 spa_prop_add_list(*nvp
, ZPOOL_PROP_VERSION
, NULL
,
315 version
, ZPROP_SRC_LOCAL
);
317 spa_prop_add_list(*nvp
, ZPOOL_PROP_LOAD_GUID
,
318 NULL
, spa_load_guid(spa
), src
);
323 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
324 * when opening pools before this version freedir will be NULL.
326 if (pool
->dp_free_dir
!= NULL
) {
327 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
, NULL
,
328 dsl_dir_phys(pool
->dp_free_dir
)->dd_used_bytes
,
331 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
,
335 if (pool
->dp_leak_dir
!= NULL
) {
336 spa_prop_add_list(*nvp
, ZPOOL_PROP_LEAKED
, NULL
,
337 dsl_dir_phys(pool
->dp_leak_dir
)->dd_used_bytes
,
340 spa_prop_add_list(*nvp
, ZPOOL_PROP_LEAKED
,
345 spa_prop_add_list(*nvp
, ZPOOL_PROP_GUID
, NULL
, spa_guid(spa
), src
);
347 if (spa
->spa_comment
!= NULL
) {
348 spa_prop_add_list(*nvp
, ZPOOL_PROP_COMMENT
, spa
->spa_comment
,
352 if (spa
->spa_root
!= NULL
)
353 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALTROOT
, spa
->spa_root
,
356 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_BLOCKS
)) {
357 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXBLOCKSIZE
, NULL
,
358 MIN(zfs_max_recordsize
, SPA_MAXBLOCKSIZE
), ZPROP_SRC_NONE
);
360 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXBLOCKSIZE
, NULL
,
361 SPA_OLD_MAXBLOCKSIZE
, ZPROP_SRC_NONE
);
364 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_DNODE
)) {
365 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXDNODESIZE
, NULL
,
366 DNODE_MAX_SIZE
, ZPROP_SRC_NONE
);
368 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXDNODESIZE
, NULL
,
369 DNODE_MIN_SIZE
, ZPROP_SRC_NONE
);
372 if ((dp
= list_head(&spa
->spa_config_list
)) != NULL
) {
373 if (dp
->scd_path
== NULL
) {
374 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
375 "none", 0, ZPROP_SRC_LOCAL
);
376 } else if (strcmp(dp
->scd_path
, spa_config_path
) != 0) {
377 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
378 dp
->scd_path
, 0, ZPROP_SRC_LOCAL
);
384 * Get zpool property values.
387 spa_prop_get(spa_t
*spa
, nvlist_t
**nvp
)
389 objset_t
*mos
= spa
->spa_meta_objset
;
394 err
= nvlist_alloc(nvp
, NV_UNIQUE_NAME
, KM_SLEEP
);
398 mutex_enter(&spa
->spa_props_lock
);
401 * Get properties from the spa config.
403 spa_prop_get_config(spa
, nvp
);
405 /* If no pool property object, no more prop to get. */
406 if (mos
== NULL
|| spa
->spa_pool_props_object
== 0) {
407 mutex_exit(&spa
->spa_props_lock
);
412 * Get properties from the MOS pool property object.
414 for (zap_cursor_init(&zc
, mos
, spa
->spa_pool_props_object
);
415 (err
= zap_cursor_retrieve(&zc
, &za
)) == 0;
416 zap_cursor_advance(&zc
)) {
419 zprop_source_t src
= ZPROP_SRC_DEFAULT
;
422 if ((prop
= zpool_name_to_prop(za
.za_name
)) == ZPOOL_PROP_INVAL
)
425 switch (za
.za_integer_length
) {
427 /* integer property */
428 if (za
.za_first_integer
!=
429 zpool_prop_default_numeric(prop
))
430 src
= ZPROP_SRC_LOCAL
;
432 if (prop
== ZPOOL_PROP_BOOTFS
) {
434 dsl_dataset_t
*ds
= NULL
;
436 dp
= spa_get_dsl(spa
);
437 dsl_pool_config_enter(dp
, FTAG
);
438 err
= dsl_dataset_hold_obj(dp
,
439 za
.za_first_integer
, FTAG
, &ds
);
441 dsl_pool_config_exit(dp
, FTAG
);
445 strval
= kmem_alloc(ZFS_MAX_DATASET_NAME_LEN
,
447 dsl_dataset_name(ds
, strval
);
448 dsl_dataset_rele(ds
, FTAG
);
449 dsl_pool_config_exit(dp
, FTAG
);
452 intval
= za
.za_first_integer
;
455 spa_prop_add_list(*nvp
, prop
, strval
, intval
, src
);
458 kmem_free(strval
, ZFS_MAX_DATASET_NAME_LEN
);
463 /* string property */
464 strval
= kmem_alloc(za
.za_num_integers
, KM_SLEEP
);
465 err
= zap_lookup(mos
, spa
->spa_pool_props_object
,
466 za
.za_name
, 1, za
.za_num_integers
, strval
);
468 kmem_free(strval
, za
.za_num_integers
);
471 spa_prop_add_list(*nvp
, prop
, strval
, 0, src
);
472 kmem_free(strval
, za
.za_num_integers
);
479 zap_cursor_fini(&zc
);
480 mutex_exit(&spa
->spa_props_lock
);
482 if (err
&& err
!= ENOENT
) {
492 * Validate the given pool properties nvlist and modify the list
493 * for the property values to be set.
496 spa_prop_validate(spa_t
*spa
, nvlist_t
*props
)
499 int error
= 0, reset_bootfs
= 0;
501 boolean_t has_feature
= B_FALSE
;
504 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
506 char *strval
, *slash
, *check
, *fname
;
507 const char *propname
= nvpair_name(elem
);
508 zpool_prop_t prop
= zpool_name_to_prop(propname
);
511 case ZPOOL_PROP_INVAL
:
512 if (!zpool_prop_feature(propname
)) {
513 error
= SET_ERROR(EINVAL
);
518 * Sanitize the input.
520 if (nvpair_type(elem
) != DATA_TYPE_UINT64
) {
521 error
= SET_ERROR(EINVAL
);
525 if (nvpair_value_uint64(elem
, &intval
) != 0) {
526 error
= SET_ERROR(EINVAL
);
531 error
= SET_ERROR(EINVAL
);
535 fname
= strchr(propname
, '@') + 1;
536 if (zfeature_lookup_name(fname
, NULL
) != 0) {
537 error
= SET_ERROR(EINVAL
);
541 has_feature
= B_TRUE
;
544 case ZPOOL_PROP_VERSION
:
545 error
= nvpair_value_uint64(elem
, &intval
);
547 (intval
< spa_version(spa
) ||
548 intval
> SPA_VERSION_BEFORE_FEATURES
||
550 error
= SET_ERROR(EINVAL
);
553 case ZPOOL_PROP_DELEGATION
:
554 case ZPOOL_PROP_AUTOREPLACE
:
555 case ZPOOL_PROP_LISTSNAPS
:
556 case ZPOOL_PROP_AUTOEXPAND
:
557 error
= nvpair_value_uint64(elem
, &intval
);
558 if (!error
&& intval
> 1)
559 error
= SET_ERROR(EINVAL
);
562 case ZPOOL_PROP_MULTIHOST
:
563 error
= nvpair_value_uint64(elem
, &intval
);
564 if (!error
&& intval
> 1)
565 error
= SET_ERROR(EINVAL
);
567 if (!error
&& !spa_get_hostid())
568 error
= SET_ERROR(ENOTSUP
);
572 case ZPOOL_PROP_BOOTFS
:
574 * If the pool version is less than SPA_VERSION_BOOTFS,
575 * or the pool is still being created (version == 0),
576 * the bootfs property cannot be set.
578 if (spa_version(spa
) < SPA_VERSION_BOOTFS
) {
579 error
= SET_ERROR(ENOTSUP
);
584 * Make sure the vdev config is bootable
586 if (!vdev_is_bootable(spa
->spa_root_vdev
)) {
587 error
= SET_ERROR(ENOTSUP
);
593 error
= nvpair_value_string(elem
, &strval
);
599 if (strval
== NULL
|| strval
[0] == '\0') {
600 objnum
= zpool_prop_default_numeric(
605 error
= dmu_objset_hold(strval
, FTAG
, &os
);
610 * Must be ZPL, and its property settings
611 * must be supported by GRUB (compression
612 * is not gzip, and large blocks or large
613 * dnodes are not used).
616 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
617 error
= SET_ERROR(ENOTSUP
);
619 dsl_prop_get_int_ds(dmu_objset_ds(os
),
620 zfs_prop_to_name(ZFS_PROP_COMPRESSION
),
622 !BOOTFS_COMPRESS_VALID(propval
)) {
623 error
= SET_ERROR(ENOTSUP
);
625 dsl_prop_get_int_ds(dmu_objset_ds(os
),
626 zfs_prop_to_name(ZFS_PROP_DNODESIZE
),
628 propval
!= ZFS_DNSIZE_LEGACY
) {
629 error
= SET_ERROR(ENOTSUP
);
631 objnum
= dmu_objset_id(os
);
633 dmu_objset_rele(os
, FTAG
);
637 case ZPOOL_PROP_FAILUREMODE
:
638 error
= nvpair_value_uint64(elem
, &intval
);
639 if (!error
&& intval
> ZIO_FAILURE_MODE_PANIC
)
640 error
= SET_ERROR(EINVAL
);
643 * This is a special case which only occurs when
644 * the pool has completely failed. This allows
645 * the user to change the in-core failmode property
646 * without syncing it out to disk (I/Os might
647 * currently be blocked). We do this by returning
648 * EIO to the caller (spa_prop_set) to trick it
649 * into thinking we encountered a property validation
652 if (!error
&& spa_suspended(spa
)) {
653 spa
->spa_failmode
= intval
;
654 error
= SET_ERROR(EIO
);
658 case ZPOOL_PROP_CACHEFILE
:
659 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
662 if (strval
[0] == '\0')
665 if (strcmp(strval
, "none") == 0)
668 if (strval
[0] != '/') {
669 error
= SET_ERROR(EINVAL
);
673 slash
= strrchr(strval
, '/');
674 ASSERT(slash
!= NULL
);
676 if (slash
[1] == '\0' || strcmp(slash
, "/.") == 0 ||
677 strcmp(slash
, "/..") == 0)
678 error
= SET_ERROR(EINVAL
);
681 case ZPOOL_PROP_COMMENT
:
682 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
684 for (check
= strval
; *check
!= '\0'; check
++) {
685 if (!isprint(*check
)) {
686 error
= SET_ERROR(EINVAL
);
690 if (strlen(strval
) > ZPROP_MAX_COMMENT
)
691 error
= SET_ERROR(E2BIG
);
694 case ZPOOL_PROP_DEDUPDITTO
:
695 if (spa_version(spa
) < SPA_VERSION_DEDUP
)
696 error
= SET_ERROR(ENOTSUP
);
698 error
= nvpair_value_uint64(elem
, &intval
);
700 intval
!= 0 && intval
< ZIO_DEDUPDITTO_MIN
)
701 error
= SET_ERROR(EINVAL
);
712 if (!error
&& reset_bootfs
) {
713 error
= nvlist_remove(props
,
714 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), DATA_TYPE_STRING
);
717 error
= nvlist_add_uint64(props
,
718 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), objnum
);
726 spa_configfile_set(spa_t
*spa
, nvlist_t
*nvp
, boolean_t need_sync
)
729 spa_config_dirent_t
*dp
;
731 if (nvlist_lookup_string(nvp
, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
),
735 dp
= kmem_alloc(sizeof (spa_config_dirent_t
),
738 if (cachefile
[0] == '\0')
739 dp
->scd_path
= spa_strdup(spa_config_path
);
740 else if (strcmp(cachefile
, "none") == 0)
743 dp
->scd_path
= spa_strdup(cachefile
);
745 list_insert_head(&spa
->spa_config_list
, dp
);
747 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
751 spa_prop_set(spa_t
*spa
, nvlist_t
*nvp
)
754 nvpair_t
*elem
= NULL
;
755 boolean_t need_sync
= B_FALSE
;
757 if ((error
= spa_prop_validate(spa
, nvp
)) != 0)
760 while ((elem
= nvlist_next_nvpair(nvp
, elem
)) != NULL
) {
761 zpool_prop_t prop
= zpool_name_to_prop(nvpair_name(elem
));
763 if (prop
== ZPOOL_PROP_CACHEFILE
||
764 prop
== ZPOOL_PROP_ALTROOT
||
765 prop
== ZPOOL_PROP_READONLY
)
768 if (prop
== ZPOOL_PROP_VERSION
|| prop
== ZPOOL_PROP_INVAL
) {
771 if (prop
== ZPOOL_PROP_VERSION
) {
772 VERIFY(nvpair_value_uint64(elem
, &ver
) == 0);
774 ASSERT(zpool_prop_feature(nvpair_name(elem
)));
775 ver
= SPA_VERSION_FEATURES
;
779 /* Save time if the version is already set. */
780 if (ver
== spa_version(spa
))
784 * In addition to the pool directory object, we might
785 * create the pool properties object, the features for
786 * read object, the features for write object, or the
787 * feature descriptions object.
789 error
= dsl_sync_task(spa
->spa_name
, NULL
,
790 spa_sync_version
, &ver
,
791 6, ZFS_SPACE_CHECK_RESERVED
);
802 return (dsl_sync_task(spa
->spa_name
, NULL
, spa_sync_props
,
803 nvp
, 6, ZFS_SPACE_CHECK_RESERVED
));
810 * If the bootfs property value is dsobj, clear it.
813 spa_prop_clear_bootfs(spa_t
*spa
, uint64_t dsobj
, dmu_tx_t
*tx
)
815 if (spa
->spa_bootfs
== dsobj
&& spa
->spa_pool_props_object
!= 0) {
816 VERIFY(zap_remove(spa
->spa_meta_objset
,
817 spa
->spa_pool_props_object
,
818 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), tx
) == 0);
825 spa_change_guid_check(void *arg
, dmu_tx_t
*tx
)
827 ASSERTV(uint64_t *newguid
= arg
);
828 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
829 vdev_t
*rvd
= spa
->spa_root_vdev
;
832 if (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
)) {
833 int error
= (spa_has_checkpoint(spa
)) ?
834 ZFS_ERR_CHECKPOINT_EXISTS
: ZFS_ERR_DISCARDING_CHECKPOINT
;
835 return (SET_ERROR(error
));
838 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
839 vdev_state
= rvd
->vdev_state
;
840 spa_config_exit(spa
, SCL_STATE
, FTAG
);
842 if (vdev_state
!= VDEV_STATE_HEALTHY
)
843 return (SET_ERROR(ENXIO
));
845 ASSERT3U(spa_guid(spa
), !=, *newguid
);
851 spa_change_guid_sync(void *arg
, dmu_tx_t
*tx
)
853 uint64_t *newguid
= arg
;
854 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
856 vdev_t
*rvd
= spa
->spa_root_vdev
;
858 oldguid
= spa_guid(spa
);
860 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
861 rvd
->vdev_guid
= *newguid
;
862 rvd
->vdev_guid_sum
+= (*newguid
- oldguid
);
863 vdev_config_dirty(rvd
);
864 spa_config_exit(spa
, SCL_STATE
, FTAG
);
866 spa_history_log_internal(spa
, "guid change", tx
, "old=%llu new=%llu",
871 * Change the GUID for the pool. This is done so that we can later
872 * re-import a pool built from a clone of our own vdevs. We will modify
873 * the root vdev's guid, our own pool guid, and then mark all of our
874 * vdevs dirty. Note that we must make sure that all our vdevs are
875 * online when we do this, or else any vdevs that weren't present
876 * would be orphaned from our pool. We are also going to issue a
877 * sysevent to update any watchers.
880 spa_change_guid(spa_t
*spa
)
885 mutex_enter(&spa
->spa_vdev_top_lock
);
886 mutex_enter(&spa_namespace_lock
);
887 guid
= spa_generate_guid(NULL
);
889 error
= dsl_sync_task(spa
->spa_name
, spa_change_guid_check
,
890 spa_change_guid_sync
, &guid
, 5, ZFS_SPACE_CHECK_RESERVED
);
893 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
);
894 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_REGUID
);
897 mutex_exit(&spa_namespace_lock
);
898 mutex_exit(&spa
->spa_vdev_top_lock
);
904 * ==========================================================================
905 * SPA state manipulation (open/create/destroy/import/export)
906 * ==========================================================================
910 spa_error_entry_compare(const void *a
, const void *b
)
912 const spa_error_entry_t
*sa
= (const spa_error_entry_t
*)a
;
913 const spa_error_entry_t
*sb
= (const spa_error_entry_t
*)b
;
916 ret
= memcmp(&sa
->se_bookmark
, &sb
->se_bookmark
,
917 sizeof (zbookmark_phys_t
));
919 return (AVL_ISIGN(ret
));
923 * Utility function which retrieves copies of the current logs and
924 * re-initializes them in the process.
927 spa_get_errlists(spa_t
*spa
, avl_tree_t
*last
, avl_tree_t
*scrub
)
929 ASSERT(MUTEX_HELD(&spa
->spa_errlist_lock
));
931 bcopy(&spa
->spa_errlist_last
, last
, sizeof (avl_tree_t
));
932 bcopy(&spa
->spa_errlist_scrub
, scrub
, sizeof (avl_tree_t
));
934 avl_create(&spa
->spa_errlist_scrub
,
935 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
936 offsetof(spa_error_entry_t
, se_avl
));
937 avl_create(&spa
->spa_errlist_last
,
938 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
939 offsetof(spa_error_entry_t
, se_avl
));
943 spa_taskqs_init(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
945 const zio_taskq_info_t
*ztip
= &zio_taskqs
[t
][q
];
946 enum zti_modes mode
= ztip
->zti_mode
;
947 uint_t value
= ztip
->zti_value
;
948 uint_t count
= ztip
->zti_count
;
949 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
951 boolean_t batch
= B_FALSE
;
953 if (mode
== ZTI_MODE_NULL
) {
955 tqs
->stqs_taskq
= NULL
;
959 ASSERT3U(count
, >, 0);
961 tqs
->stqs_count
= count
;
962 tqs
->stqs_taskq
= kmem_alloc(count
* sizeof (taskq_t
*), KM_SLEEP
);
966 ASSERT3U(value
, >=, 1);
967 value
= MAX(value
, 1);
968 flags
|= TASKQ_DYNAMIC
;
973 flags
|= TASKQ_THREADS_CPU_PCT
;
974 value
= MIN(zio_taskq_batch_pct
, 100);
978 panic("unrecognized mode for %s_%s taskq (%u:%u) in "
980 zio_type_name
[t
], zio_taskq_types
[q
], mode
, value
);
984 for (uint_t i
= 0; i
< count
; i
++) {
988 (void) snprintf(name
, sizeof (name
), "%s_%s",
989 zio_type_name
[t
], zio_taskq_types
[q
]);
991 if (zio_taskq_sysdc
&& spa
->spa_proc
!= &p0
) {
993 flags
|= TASKQ_DC_BATCH
;
995 tq
= taskq_create_sysdc(name
, value
, 50, INT_MAX
,
996 spa
->spa_proc
, zio_taskq_basedc
, flags
);
998 pri_t pri
= maxclsyspri
;
1000 * The write issue taskq can be extremely CPU
1001 * intensive. Run it at slightly less important
1002 * priority than the other taskqs. Under Linux this
1003 * means incrementing the priority value on platforms
1004 * like illumos it should be decremented.
1006 if (t
== ZIO_TYPE_WRITE
&& q
== ZIO_TASKQ_ISSUE
)
1009 tq
= taskq_create_proc(name
, value
, pri
, 50,
1010 INT_MAX
, spa
->spa_proc
, flags
);
1013 tqs
->stqs_taskq
[i
] = tq
;
1018 spa_taskqs_fini(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
1020 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1022 if (tqs
->stqs_taskq
== NULL
) {
1023 ASSERT3U(tqs
->stqs_count
, ==, 0);
1027 for (uint_t i
= 0; i
< tqs
->stqs_count
; i
++) {
1028 ASSERT3P(tqs
->stqs_taskq
[i
], !=, NULL
);
1029 taskq_destroy(tqs
->stqs_taskq
[i
]);
1032 kmem_free(tqs
->stqs_taskq
, tqs
->stqs_count
* sizeof (taskq_t
*));
1033 tqs
->stqs_taskq
= NULL
;
1037 * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority.
1038 * Note that a type may have multiple discrete taskqs to avoid lock contention
1039 * on the taskq itself. In that case we choose which taskq at random by using
1040 * the low bits of gethrtime().
1043 spa_taskq_dispatch_ent(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
1044 task_func_t
*func
, void *arg
, uint_t flags
, taskq_ent_t
*ent
)
1046 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1049 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
1050 ASSERT3U(tqs
->stqs_count
, !=, 0);
1052 if (tqs
->stqs_count
== 1) {
1053 tq
= tqs
->stqs_taskq
[0];
1055 tq
= tqs
->stqs_taskq
[((uint64_t)gethrtime()) % tqs
->stqs_count
];
1058 taskq_dispatch_ent(tq
, func
, arg
, flags
, ent
);
1062 * Same as spa_taskq_dispatch_ent() but block on the task until completion.
1065 spa_taskq_dispatch_sync(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
1066 task_func_t
*func
, void *arg
, uint_t flags
)
1068 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1072 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
1073 ASSERT3U(tqs
->stqs_count
, !=, 0);
1075 if (tqs
->stqs_count
== 1) {
1076 tq
= tqs
->stqs_taskq
[0];
1078 tq
= tqs
->stqs_taskq
[((uint64_t)gethrtime()) % tqs
->stqs_count
];
1081 id
= taskq_dispatch(tq
, func
, arg
, flags
);
1083 taskq_wait_id(tq
, id
);
1087 spa_create_zio_taskqs(spa_t
*spa
)
1089 for (int t
= 0; t
< ZIO_TYPES
; t
++) {
1090 for (int q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1091 spa_taskqs_init(spa
, t
, q
);
1097 * Disabled until spa_thread() can be adapted for Linux.
1099 #undef HAVE_SPA_THREAD
1101 #if defined(_KERNEL) && defined(HAVE_SPA_THREAD)
1103 spa_thread(void *arg
)
1105 psetid_t zio_taskq_psrset_bind
= PS_NONE
;
1106 callb_cpr_t cprinfo
;
1109 user_t
*pu
= PTOU(curproc
);
1111 CALLB_CPR_INIT(&cprinfo
, &spa
->spa_proc_lock
, callb_generic_cpr
,
1114 ASSERT(curproc
!= &p0
);
1115 (void) snprintf(pu
->u_psargs
, sizeof (pu
->u_psargs
),
1116 "zpool-%s", spa
->spa_name
);
1117 (void) strlcpy(pu
->u_comm
, pu
->u_psargs
, sizeof (pu
->u_comm
));
1119 /* bind this thread to the requested psrset */
1120 if (zio_taskq_psrset_bind
!= PS_NONE
) {
1122 mutex_enter(&cpu_lock
);
1123 mutex_enter(&pidlock
);
1124 mutex_enter(&curproc
->p_lock
);
1126 if (cpupart_bind_thread(curthread
, zio_taskq_psrset_bind
,
1127 0, NULL
, NULL
) == 0) {
1128 curthread
->t_bind_pset
= zio_taskq_psrset_bind
;
1131 "Couldn't bind process for zfs pool \"%s\" to "
1132 "pset %d\n", spa
->spa_name
, zio_taskq_psrset_bind
);
1135 mutex_exit(&curproc
->p_lock
);
1136 mutex_exit(&pidlock
);
1137 mutex_exit(&cpu_lock
);
1141 if (zio_taskq_sysdc
) {
1142 sysdc_thread_enter(curthread
, 100, 0);
1145 spa
->spa_proc
= curproc
;
1146 spa
->spa_did
= curthread
->t_did
;
1148 spa_create_zio_taskqs(spa
);
1150 mutex_enter(&spa
->spa_proc_lock
);
1151 ASSERT(spa
->spa_proc_state
== SPA_PROC_CREATED
);
1153 spa
->spa_proc_state
= SPA_PROC_ACTIVE
;
1154 cv_broadcast(&spa
->spa_proc_cv
);
1156 CALLB_CPR_SAFE_BEGIN(&cprinfo
);
1157 while (spa
->spa_proc_state
== SPA_PROC_ACTIVE
)
1158 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1159 CALLB_CPR_SAFE_END(&cprinfo
, &spa
->spa_proc_lock
);
1161 ASSERT(spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
);
1162 spa
->spa_proc_state
= SPA_PROC_GONE
;
1163 spa
->spa_proc
= &p0
;
1164 cv_broadcast(&spa
->spa_proc_cv
);
1165 CALLB_CPR_EXIT(&cprinfo
); /* drops spa_proc_lock */
1167 mutex_enter(&curproc
->p_lock
);
1173 * Activate an uninitialized pool.
1176 spa_activate(spa_t
*spa
, int mode
)
1178 ASSERT(spa
->spa_state
== POOL_STATE_UNINITIALIZED
);
1180 spa
->spa_state
= POOL_STATE_ACTIVE
;
1181 spa
->spa_mode
= mode
;
1183 spa
->spa_normal_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
1184 spa
->spa_log_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
1185 spa
->spa_special_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
1186 spa
->spa_dedup_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
1188 /* Try to create a covering process */
1189 mutex_enter(&spa
->spa_proc_lock
);
1190 ASSERT(spa
->spa_proc_state
== SPA_PROC_NONE
);
1191 ASSERT(spa
->spa_proc
== &p0
);
1194 #ifdef HAVE_SPA_THREAD
1195 /* Only create a process if we're going to be around a while. */
1196 if (spa_create_process
&& strcmp(spa
->spa_name
, TRYIMPORT_NAME
) != 0) {
1197 if (newproc(spa_thread
, (caddr_t
)spa
, syscid
, maxclsyspri
,
1199 spa
->spa_proc_state
= SPA_PROC_CREATED
;
1200 while (spa
->spa_proc_state
== SPA_PROC_CREATED
) {
1201 cv_wait(&spa
->spa_proc_cv
,
1202 &spa
->spa_proc_lock
);
1204 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1205 ASSERT(spa
->spa_proc
!= &p0
);
1206 ASSERT(spa
->spa_did
!= 0);
1210 "Couldn't create process for zfs pool \"%s\"\n",
1215 #endif /* HAVE_SPA_THREAD */
1216 mutex_exit(&spa
->spa_proc_lock
);
1218 /* If we didn't create a process, we need to create our taskqs. */
1219 if (spa
->spa_proc
== &p0
) {
1220 spa_create_zio_taskqs(spa
);
1223 for (size_t i
= 0; i
< TXG_SIZE
; i
++) {
1224 spa
->spa_txg_zio
[i
] = zio_root(spa
, NULL
, NULL
,
1228 list_create(&spa
->spa_config_dirty_list
, sizeof (vdev_t
),
1229 offsetof(vdev_t
, vdev_config_dirty_node
));
1230 list_create(&spa
->spa_evicting_os_list
, sizeof (objset_t
),
1231 offsetof(objset_t
, os_evicting_node
));
1232 list_create(&spa
->spa_state_dirty_list
, sizeof (vdev_t
),
1233 offsetof(vdev_t
, vdev_state_dirty_node
));
1235 txg_list_create(&spa
->spa_vdev_txg_list
, spa
,
1236 offsetof(struct vdev
, vdev_txg_node
));
1238 avl_create(&spa
->spa_errlist_scrub
,
1239 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1240 offsetof(spa_error_entry_t
, se_avl
));
1241 avl_create(&spa
->spa_errlist_last
,
1242 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1243 offsetof(spa_error_entry_t
, se_avl
));
1245 spa_keystore_init(&spa
->spa_keystore
);
1248 * This taskq is used to perform zvol-minor-related tasks
1249 * asynchronously. This has several advantages, including easy
1250 * resolution of various deadlocks (zfsonlinux bug #3681).
1252 * The taskq must be single threaded to ensure tasks are always
1253 * processed in the order in which they were dispatched.
1255 * A taskq per pool allows one to keep the pools independent.
1256 * This way if one pool is suspended, it will not impact another.
1258 * The preferred location to dispatch a zvol minor task is a sync
1259 * task. In this context, there is easy access to the spa_t and minimal
1260 * error handling is required because the sync task must succeed.
1262 spa
->spa_zvol_taskq
= taskq_create("z_zvol", 1, defclsyspri
,
1266 * Taskq dedicated to prefetcher threads: this is used to prevent the
1267 * pool traverse code from monopolizing the global (and limited)
1268 * system_taskq by inappropriately scheduling long running tasks on it.
1270 spa
->spa_prefetch_taskq
= taskq_create("z_prefetch", boot_ncpus
,
1271 defclsyspri
, 1, INT_MAX
, TASKQ_DYNAMIC
);
1274 * The taskq to upgrade datasets in this pool. Currently used by
1275 * feature SPA_FEATURE_USEROBJ_ACCOUNTING/SPA_FEATURE_PROJECT_QUOTA.
1277 spa
->spa_upgrade_taskq
= taskq_create("z_upgrade", boot_ncpus
,
1278 defclsyspri
, 1, INT_MAX
, TASKQ_DYNAMIC
);
1282 * Opposite of spa_activate().
1285 spa_deactivate(spa_t
*spa
)
1287 ASSERT(spa
->spa_sync_on
== B_FALSE
);
1288 ASSERT(spa
->spa_dsl_pool
== NULL
);
1289 ASSERT(spa
->spa_root_vdev
== NULL
);
1290 ASSERT(spa
->spa_async_zio_root
== NULL
);
1291 ASSERT(spa
->spa_state
!= POOL_STATE_UNINITIALIZED
);
1293 spa_evicting_os_wait(spa
);
1295 if (spa
->spa_zvol_taskq
) {
1296 taskq_destroy(spa
->spa_zvol_taskq
);
1297 spa
->spa_zvol_taskq
= NULL
;
1300 if (spa
->spa_prefetch_taskq
) {
1301 taskq_destroy(spa
->spa_prefetch_taskq
);
1302 spa
->spa_prefetch_taskq
= NULL
;
1305 if (spa
->spa_upgrade_taskq
) {
1306 taskq_destroy(spa
->spa_upgrade_taskq
);
1307 spa
->spa_upgrade_taskq
= NULL
;
1310 txg_list_destroy(&spa
->spa_vdev_txg_list
);
1312 list_destroy(&spa
->spa_config_dirty_list
);
1313 list_destroy(&spa
->spa_evicting_os_list
);
1314 list_destroy(&spa
->spa_state_dirty_list
);
1316 taskq_cancel_id(system_delay_taskq
, spa
->spa_deadman_tqid
);
1318 for (int t
= 0; t
< ZIO_TYPES
; t
++) {
1319 for (int q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1320 spa_taskqs_fini(spa
, t
, q
);
1324 for (size_t i
= 0; i
< TXG_SIZE
; i
++) {
1325 ASSERT3P(spa
->spa_txg_zio
[i
], !=, NULL
);
1326 VERIFY0(zio_wait(spa
->spa_txg_zio
[i
]));
1327 spa
->spa_txg_zio
[i
] = NULL
;
1330 metaslab_class_destroy(spa
->spa_normal_class
);
1331 spa
->spa_normal_class
= NULL
;
1333 metaslab_class_destroy(spa
->spa_log_class
);
1334 spa
->spa_log_class
= NULL
;
1336 metaslab_class_destroy(spa
->spa_special_class
);
1337 spa
->spa_special_class
= NULL
;
1339 metaslab_class_destroy(spa
->spa_dedup_class
);
1340 spa
->spa_dedup_class
= NULL
;
1343 * If this was part of an import or the open otherwise failed, we may
1344 * still have errors left in the queues. Empty them just in case.
1346 spa_errlog_drain(spa
);
1347 avl_destroy(&spa
->spa_errlist_scrub
);
1348 avl_destroy(&spa
->spa_errlist_last
);
1350 spa_keystore_fini(&spa
->spa_keystore
);
1352 spa
->spa_state
= POOL_STATE_UNINITIALIZED
;
1354 mutex_enter(&spa
->spa_proc_lock
);
1355 if (spa
->spa_proc_state
!= SPA_PROC_NONE
) {
1356 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1357 spa
->spa_proc_state
= SPA_PROC_DEACTIVATE
;
1358 cv_broadcast(&spa
->spa_proc_cv
);
1359 while (spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
) {
1360 ASSERT(spa
->spa_proc
!= &p0
);
1361 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1363 ASSERT(spa
->spa_proc_state
== SPA_PROC_GONE
);
1364 spa
->spa_proc_state
= SPA_PROC_NONE
;
1366 ASSERT(spa
->spa_proc
== &p0
);
1367 mutex_exit(&spa
->spa_proc_lock
);
1370 * We want to make sure spa_thread() has actually exited the ZFS
1371 * module, so that the module can't be unloaded out from underneath
1374 if (spa
->spa_did
!= 0) {
1375 thread_join(spa
->spa_did
);
1381 * Verify a pool configuration, and construct the vdev tree appropriately. This
1382 * will create all the necessary vdevs in the appropriate layout, with each vdev
1383 * in the CLOSED state. This will prep the pool before open/creation/import.
1384 * All vdev validation is done by the vdev_alloc() routine.
1387 spa_config_parse(spa_t
*spa
, vdev_t
**vdp
, nvlist_t
*nv
, vdev_t
*parent
,
1388 uint_t id
, int atype
)
1394 if ((error
= vdev_alloc(spa
, vdp
, nv
, parent
, id
, atype
)) != 0)
1397 if ((*vdp
)->vdev_ops
->vdev_op_leaf
)
1400 error
= nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1403 if (error
== ENOENT
)
1409 return (SET_ERROR(EINVAL
));
1412 for (int c
= 0; c
< children
; c
++) {
1414 if ((error
= spa_config_parse(spa
, &vd
, child
[c
], *vdp
, c
,
1422 ASSERT(*vdp
!= NULL
);
1428 * Opposite of spa_load().
1431 spa_unload(spa_t
*spa
)
1435 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1437 spa_load_note(spa
, "UNLOADING");
1442 spa_async_suspend(spa
);
1444 if (spa
->spa_root_vdev
) {
1445 vdev_initialize_stop_all(spa
->spa_root_vdev
,
1446 VDEV_INITIALIZE_ACTIVE
);
1452 if (spa
->spa_sync_on
) {
1453 txg_sync_stop(spa
->spa_dsl_pool
);
1454 spa
->spa_sync_on
= B_FALSE
;
1458 * Even though vdev_free() also calls vdev_metaslab_fini, we need
1459 * to call it earlier, before we wait for async i/o to complete.
1460 * This ensures that there is no async metaslab prefetching, by
1461 * calling taskq_wait(mg_taskq).
1463 if (spa
->spa_root_vdev
!= NULL
) {
1464 spa_config_enter(spa
, SCL_ALL
, spa
, RW_WRITER
);
1465 for (int c
= 0; c
< spa
->spa_root_vdev
->vdev_children
; c
++)
1466 vdev_metaslab_fini(spa
->spa_root_vdev
->vdev_child
[c
]);
1467 spa_config_exit(spa
, SCL_ALL
, spa
);
1470 if (spa
->spa_mmp
.mmp_thread
)
1471 mmp_thread_stop(spa
);
1474 * Wait for any outstanding async I/O to complete.
1476 if (spa
->spa_async_zio_root
!= NULL
) {
1477 for (int i
= 0; i
< max_ncpus
; i
++)
1478 (void) zio_wait(spa
->spa_async_zio_root
[i
]);
1479 kmem_free(spa
->spa_async_zio_root
, max_ncpus
* sizeof (void *));
1480 spa
->spa_async_zio_root
= NULL
;
1483 if (spa
->spa_vdev_removal
!= NULL
) {
1484 spa_vdev_removal_destroy(spa
->spa_vdev_removal
);
1485 spa
->spa_vdev_removal
= NULL
;
1488 if (spa
->spa_condense_zthr
!= NULL
) {
1489 zthr_destroy(spa
->spa_condense_zthr
);
1490 spa
->spa_condense_zthr
= NULL
;
1493 if (spa
->spa_checkpoint_discard_zthr
!= NULL
) {
1494 zthr_destroy(spa
->spa_checkpoint_discard_zthr
);
1495 spa
->spa_checkpoint_discard_zthr
= NULL
;
1498 spa_condense_fini(spa
);
1500 bpobj_close(&spa
->spa_deferred_bpobj
);
1502 spa_config_enter(spa
, SCL_ALL
, spa
, RW_WRITER
);
1507 if (spa
->spa_root_vdev
)
1508 vdev_free(spa
->spa_root_vdev
);
1509 ASSERT(spa
->spa_root_vdev
== NULL
);
1512 * Close the dsl pool.
1514 if (spa
->spa_dsl_pool
) {
1515 dsl_pool_close(spa
->spa_dsl_pool
);
1516 spa
->spa_dsl_pool
= NULL
;
1517 spa
->spa_meta_objset
= NULL
;
1523 * Drop and purge level 2 cache
1525 spa_l2cache_drop(spa
);
1527 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1528 vdev_free(spa
->spa_spares
.sav_vdevs
[i
]);
1529 if (spa
->spa_spares
.sav_vdevs
) {
1530 kmem_free(spa
->spa_spares
.sav_vdevs
,
1531 spa
->spa_spares
.sav_count
* sizeof (void *));
1532 spa
->spa_spares
.sav_vdevs
= NULL
;
1534 if (spa
->spa_spares
.sav_config
) {
1535 nvlist_free(spa
->spa_spares
.sav_config
);
1536 spa
->spa_spares
.sav_config
= NULL
;
1538 spa
->spa_spares
.sav_count
= 0;
1540 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
1541 vdev_clear_stats(spa
->spa_l2cache
.sav_vdevs
[i
]);
1542 vdev_free(spa
->spa_l2cache
.sav_vdevs
[i
]);
1544 if (spa
->spa_l2cache
.sav_vdevs
) {
1545 kmem_free(spa
->spa_l2cache
.sav_vdevs
,
1546 spa
->spa_l2cache
.sav_count
* sizeof (void *));
1547 spa
->spa_l2cache
.sav_vdevs
= NULL
;
1549 if (spa
->spa_l2cache
.sav_config
) {
1550 nvlist_free(spa
->spa_l2cache
.sav_config
);
1551 spa
->spa_l2cache
.sav_config
= NULL
;
1553 spa
->spa_l2cache
.sav_count
= 0;
1555 spa
->spa_async_suspended
= 0;
1557 spa
->spa_indirect_vdevs_loaded
= B_FALSE
;
1559 if (spa
->spa_comment
!= NULL
) {
1560 spa_strfree(spa
->spa_comment
);
1561 spa
->spa_comment
= NULL
;
1564 spa_config_exit(spa
, SCL_ALL
, spa
);
1568 * Load (or re-load) the current list of vdevs describing the active spares for
1569 * this pool. When this is called, we have some form of basic information in
1570 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1571 * then re-generate a more complete list including status information.
1574 spa_load_spares(spa_t
*spa
)
1583 * zdb opens both the current state of the pool and the
1584 * checkpointed state (if present), with a different spa_t.
1586 * As spare vdevs are shared among open pools, we skip loading
1587 * them when we load the checkpointed state of the pool.
1589 if (!spa_writeable(spa
))
1593 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1596 * First, close and free any existing spare vdevs.
1598 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1599 vd
= spa
->spa_spares
.sav_vdevs
[i
];
1601 /* Undo the call to spa_activate() below */
1602 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1603 B_FALSE
)) != NULL
&& tvd
->vdev_isspare
)
1604 spa_spare_remove(tvd
);
1609 if (spa
->spa_spares
.sav_vdevs
)
1610 kmem_free(spa
->spa_spares
.sav_vdevs
,
1611 spa
->spa_spares
.sav_count
* sizeof (void *));
1613 if (spa
->spa_spares
.sav_config
== NULL
)
1616 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
1617 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
1619 spa
->spa_spares
.sav_count
= (int)nspares
;
1620 spa
->spa_spares
.sav_vdevs
= NULL
;
1626 * Construct the array of vdevs, opening them to get status in the
1627 * process. For each spare, there is potentially two different vdev_t
1628 * structures associated with it: one in the list of spares (used only
1629 * for basic validation purposes) and one in the active vdev
1630 * configuration (if it's spared in). During this phase we open and
1631 * validate each vdev on the spare list. If the vdev also exists in the
1632 * active configuration, then we also mark this vdev as an active spare.
1634 spa
->spa_spares
.sav_vdevs
= kmem_zalloc(nspares
* sizeof (void *),
1636 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1637 VERIFY(spa_config_parse(spa
, &vd
, spares
[i
], NULL
, 0,
1638 VDEV_ALLOC_SPARE
) == 0);
1641 spa
->spa_spares
.sav_vdevs
[i
] = vd
;
1643 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1644 B_FALSE
)) != NULL
) {
1645 if (!tvd
->vdev_isspare
)
1649 * We only mark the spare active if we were successfully
1650 * able to load the vdev. Otherwise, importing a pool
1651 * with a bad active spare would result in strange
1652 * behavior, because multiple pool would think the spare
1653 * is actively in use.
1655 * There is a vulnerability here to an equally bizarre
1656 * circumstance, where a dead active spare is later
1657 * brought back to life (onlined or otherwise). Given
1658 * the rarity of this scenario, and the extra complexity
1659 * it adds, we ignore the possibility.
1661 if (!vdev_is_dead(tvd
))
1662 spa_spare_activate(tvd
);
1666 vd
->vdev_aux
= &spa
->spa_spares
;
1668 if (vdev_open(vd
) != 0)
1671 if (vdev_validate_aux(vd
) == 0)
1676 * Recompute the stashed list of spares, with status information
1679 VERIFY(nvlist_remove(spa
->spa_spares
.sav_config
, ZPOOL_CONFIG_SPARES
,
1680 DATA_TYPE_NVLIST_ARRAY
) == 0);
1682 spares
= kmem_alloc(spa
->spa_spares
.sav_count
* sizeof (void *),
1684 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1685 spares
[i
] = vdev_config_generate(spa
,
1686 spa
->spa_spares
.sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_SPARE
);
1687 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
1688 ZPOOL_CONFIG_SPARES
, spares
, spa
->spa_spares
.sav_count
) == 0);
1689 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1690 nvlist_free(spares
[i
]);
1691 kmem_free(spares
, spa
->spa_spares
.sav_count
* sizeof (void *));
1695 * Load (or re-load) the current list of vdevs describing the active l2cache for
1696 * this pool. When this is called, we have some form of basic information in
1697 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1698 * then re-generate a more complete list including status information.
1699 * Devices which are already active have their details maintained, and are
1703 spa_load_l2cache(spa_t
*spa
)
1705 nvlist_t
**l2cache
= NULL
;
1707 int i
, j
, oldnvdevs
;
1709 vdev_t
*vd
, **oldvdevs
, **newvdevs
;
1710 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
1714 * zdb opens both the current state of the pool and the
1715 * checkpointed state (if present), with a different spa_t.
1717 * As L2 caches are part of the ARC which is shared among open
1718 * pools, we skip loading them when we load the checkpointed
1719 * state of the pool.
1721 if (!spa_writeable(spa
))
1725 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1727 oldvdevs
= sav
->sav_vdevs
;
1728 oldnvdevs
= sav
->sav_count
;
1729 sav
->sav_vdevs
= NULL
;
1732 if (sav
->sav_config
== NULL
) {
1738 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
,
1739 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
1740 newvdevs
= kmem_alloc(nl2cache
* sizeof (void *), KM_SLEEP
);
1743 * Process new nvlist of vdevs.
1745 for (i
= 0; i
< nl2cache
; i
++) {
1746 VERIFY(nvlist_lookup_uint64(l2cache
[i
], ZPOOL_CONFIG_GUID
,
1750 for (j
= 0; j
< oldnvdevs
; j
++) {
1752 if (vd
!= NULL
&& guid
== vd
->vdev_guid
) {
1754 * Retain previous vdev for add/remove ops.
1762 if (newvdevs
[i
] == NULL
) {
1766 VERIFY(spa_config_parse(spa
, &vd
, l2cache
[i
], NULL
, 0,
1767 VDEV_ALLOC_L2CACHE
) == 0);
1772 * Commit this vdev as an l2cache device,
1773 * even if it fails to open.
1775 spa_l2cache_add(vd
);
1780 spa_l2cache_activate(vd
);
1782 if (vdev_open(vd
) != 0)
1785 (void) vdev_validate_aux(vd
);
1787 if (!vdev_is_dead(vd
))
1788 l2arc_add_vdev(spa
, vd
);
1792 sav
->sav_vdevs
= newvdevs
;
1793 sav
->sav_count
= (int)nl2cache
;
1796 * Recompute the stashed list of l2cache devices, with status
1797 * information this time.
1799 VERIFY(nvlist_remove(sav
->sav_config
, ZPOOL_CONFIG_L2CACHE
,
1800 DATA_TYPE_NVLIST_ARRAY
) == 0);
1802 if (sav
->sav_count
> 0)
1803 l2cache
= kmem_alloc(sav
->sav_count
* sizeof (void *),
1805 for (i
= 0; i
< sav
->sav_count
; i
++)
1806 l2cache
[i
] = vdev_config_generate(spa
,
1807 sav
->sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_L2CACHE
);
1808 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
,
1809 ZPOOL_CONFIG_L2CACHE
, l2cache
, sav
->sav_count
) == 0);
1813 * Purge vdevs that were dropped
1815 for (i
= 0; i
< oldnvdevs
; i
++) {
1820 ASSERT(vd
->vdev_isl2cache
);
1822 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
1823 pool
!= 0ULL && l2arc_vdev_present(vd
))
1824 l2arc_remove_vdev(vd
);
1825 vdev_clear_stats(vd
);
1831 kmem_free(oldvdevs
, oldnvdevs
* sizeof (void *));
1833 for (i
= 0; i
< sav
->sav_count
; i
++)
1834 nvlist_free(l2cache
[i
]);
1836 kmem_free(l2cache
, sav
->sav_count
* sizeof (void *));
1840 load_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
**value
)
1843 char *packed
= NULL
;
1848 error
= dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
);
1852 nvsize
= *(uint64_t *)db
->db_data
;
1853 dmu_buf_rele(db
, FTAG
);
1855 packed
= vmem_alloc(nvsize
, KM_SLEEP
);
1856 error
= dmu_read(spa
->spa_meta_objset
, obj
, 0, nvsize
, packed
,
1859 error
= nvlist_unpack(packed
, nvsize
, value
, 0);
1860 vmem_free(packed
, nvsize
);
1866 * Concrete top-level vdevs that are not missing and are not logs. At every
1867 * spa_sync we write new uberblocks to at least SPA_SYNC_MIN_VDEVS core tvds.
1870 spa_healthy_core_tvds(spa_t
*spa
)
1872 vdev_t
*rvd
= spa
->spa_root_vdev
;
1875 for (uint64_t i
= 0; i
< rvd
->vdev_children
; i
++) {
1876 vdev_t
*vd
= rvd
->vdev_child
[i
];
1879 if (vdev_is_concrete(vd
) && !vdev_is_dead(vd
))
1887 * Checks to see if the given vdev could not be opened, in which case we post a
1888 * sysevent to notify the autoreplace code that the device has been removed.
1891 spa_check_removed(vdev_t
*vd
)
1893 for (uint64_t c
= 0; c
< vd
->vdev_children
; c
++)
1894 spa_check_removed(vd
->vdev_child
[c
]);
1896 if (vd
->vdev_ops
->vdev_op_leaf
&& vdev_is_dead(vd
) &&
1897 vdev_is_concrete(vd
)) {
1898 zfs_post_autoreplace(vd
->vdev_spa
, vd
);
1899 spa_event_notify(vd
->vdev_spa
, vd
, NULL
, ESC_ZFS_VDEV_CHECK
);
1904 spa_check_for_missing_logs(spa_t
*spa
)
1906 vdev_t
*rvd
= spa
->spa_root_vdev
;
1909 * If we're doing a normal import, then build up any additional
1910 * diagnostic information about missing log devices.
1911 * We'll pass this up to the user for further processing.
1913 if (!(spa
->spa_import_flags
& ZFS_IMPORT_MISSING_LOG
)) {
1914 nvlist_t
**child
, *nv
;
1917 child
= kmem_alloc(rvd
->vdev_children
* sizeof (nvlist_t
*),
1919 VERIFY(nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
1921 for (uint64_t c
= 0; c
< rvd
->vdev_children
; c
++) {
1922 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1925 * We consider a device as missing only if it failed
1926 * to open (i.e. offline or faulted is not considered
1929 if (tvd
->vdev_islog
&&
1930 tvd
->vdev_state
== VDEV_STATE_CANT_OPEN
) {
1931 child
[idx
++] = vdev_config_generate(spa
, tvd
,
1932 B_FALSE
, VDEV_CONFIG_MISSING
);
1937 fnvlist_add_nvlist_array(nv
,
1938 ZPOOL_CONFIG_CHILDREN
, child
, idx
);
1939 fnvlist_add_nvlist(spa
->spa_load_info
,
1940 ZPOOL_CONFIG_MISSING_DEVICES
, nv
);
1942 for (uint64_t i
= 0; i
< idx
; i
++)
1943 nvlist_free(child
[i
]);
1946 kmem_free(child
, rvd
->vdev_children
* sizeof (char **));
1949 spa_load_failed(spa
, "some log devices are missing");
1950 vdev_dbgmsg_print_tree(rvd
, 2);
1951 return (SET_ERROR(ENXIO
));
1954 for (uint64_t c
= 0; c
< rvd
->vdev_children
; c
++) {
1955 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1957 if (tvd
->vdev_islog
&&
1958 tvd
->vdev_state
== VDEV_STATE_CANT_OPEN
) {
1959 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
1960 spa_load_note(spa
, "some log devices are "
1961 "missing, ZIL is dropped.");
1962 vdev_dbgmsg_print_tree(rvd
, 2);
1972 * Check for missing log devices
1975 spa_check_logs(spa_t
*spa
)
1977 boolean_t rv
= B_FALSE
;
1978 dsl_pool_t
*dp
= spa_get_dsl(spa
);
1980 switch (spa
->spa_log_state
) {
1983 case SPA_LOG_MISSING
:
1984 /* need to recheck in case slog has been restored */
1985 case SPA_LOG_UNKNOWN
:
1986 rv
= (dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
1987 zil_check_log_chain
, NULL
, DS_FIND_CHILDREN
) != 0);
1989 spa_set_log_state(spa
, SPA_LOG_MISSING
);
1996 spa_passivate_log(spa_t
*spa
)
1998 vdev_t
*rvd
= spa
->spa_root_vdev
;
1999 boolean_t slog_found
= B_FALSE
;
2001 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
2003 if (!spa_has_slogs(spa
))
2006 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
2007 vdev_t
*tvd
= rvd
->vdev_child
[c
];
2008 metaslab_group_t
*mg
= tvd
->vdev_mg
;
2010 if (tvd
->vdev_islog
) {
2011 metaslab_group_passivate(mg
);
2012 slog_found
= B_TRUE
;
2016 return (slog_found
);
2020 spa_activate_log(spa_t
*spa
)
2022 vdev_t
*rvd
= spa
->spa_root_vdev
;
2024 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
2026 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
2027 vdev_t
*tvd
= rvd
->vdev_child
[c
];
2028 metaslab_group_t
*mg
= tvd
->vdev_mg
;
2030 if (tvd
->vdev_islog
)
2031 metaslab_group_activate(mg
);
2036 spa_reset_logs(spa_t
*spa
)
2040 error
= dmu_objset_find(spa_name(spa
), zil_reset
,
2041 NULL
, DS_FIND_CHILDREN
);
2044 * We successfully offlined the log device, sync out the
2045 * current txg so that the "stubby" block can be removed
2048 txg_wait_synced(spa
->spa_dsl_pool
, 0);
2054 spa_aux_check_removed(spa_aux_vdev_t
*sav
)
2056 for (int i
= 0; i
< sav
->sav_count
; i
++)
2057 spa_check_removed(sav
->sav_vdevs
[i
]);
2061 spa_claim_notify(zio_t
*zio
)
2063 spa_t
*spa
= zio
->io_spa
;
2068 mutex_enter(&spa
->spa_props_lock
); /* any mutex will do */
2069 if (spa
->spa_claim_max_txg
< zio
->io_bp
->blk_birth
)
2070 spa
->spa_claim_max_txg
= zio
->io_bp
->blk_birth
;
2071 mutex_exit(&spa
->spa_props_lock
);
2074 typedef struct spa_load_error
{
2075 uint64_t sle_meta_count
;
2076 uint64_t sle_data_count
;
2080 spa_load_verify_done(zio_t
*zio
)
2082 blkptr_t
*bp
= zio
->io_bp
;
2083 spa_load_error_t
*sle
= zio
->io_private
;
2084 dmu_object_type_t type
= BP_GET_TYPE(bp
);
2085 int error
= zio
->io_error
;
2086 spa_t
*spa
= zio
->io_spa
;
2088 abd_free(zio
->io_abd
);
2090 if ((BP_GET_LEVEL(bp
) != 0 || DMU_OT_IS_METADATA(type
)) &&
2091 type
!= DMU_OT_INTENT_LOG
)
2092 atomic_inc_64(&sle
->sle_meta_count
);
2094 atomic_inc_64(&sle
->sle_data_count
);
2097 mutex_enter(&spa
->spa_scrub_lock
);
2098 spa
->spa_load_verify_ios
--;
2099 cv_broadcast(&spa
->spa_scrub_io_cv
);
2100 mutex_exit(&spa
->spa_scrub_lock
);
2104 * Maximum number of concurrent scrub i/os to create while verifying
2105 * a pool while importing it.
2107 int spa_load_verify_maxinflight
= 10000;
2108 int spa_load_verify_metadata
= B_TRUE
;
2109 int spa_load_verify_data
= B_TRUE
;
2113 spa_load_verify_cb(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
2114 const zbookmark_phys_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
2116 if (bp
== NULL
|| BP_IS_HOLE(bp
) || BP_IS_EMBEDDED(bp
))
2119 * Note: normally this routine will not be called if
2120 * spa_load_verify_metadata is not set. However, it may be useful
2121 * to manually set the flag after the traversal has begun.
2123 if (!spa_load_verify_metadata
)
2125 if (!BP_IS_METADATA(bp
) && !spa_load_verify_data
)
2129 size_t size
= BP_GET_PSIZE(bp
);
2131 mutex_enter(&spa
->spa_scrub_lock
);
2132 while (spa
->spa_load_verify_ios
>= spa_load_verify_maxinflight
)
2133 cv_wait(&spa
->spa_scrub_io_cv
, &spa
->spa_scrub_lock
);
2134 spa
->spa_load_verify_ios
++;
2135 mutex_exit(&spa
->spa_scrub_lock
);
2137 zio_nowait(zio_read(rio
, spa
, bp
, abd_alloc_for_io(size
, B_FALSE
), size
,
2138 spa_load_verify_done
, rio
->io_private
, ZIO_PRIORITY_SCRUB
,
2139 ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_CANFAIL
|
2140 ZIO_FLAG_SCRUB
| ZIO_FLAG_RAW
, zb
));
2146 verify_dataset_name_len(dsl_pool_t
*dp
, dsl_dataset_t
*ds
, void *arg
)
2148 if (dsl_dataset_namelen(ds
) >= ZFS_MAX_DATASET_NAME_LEN
)
2149 return (SET_ERROR(ENAMETOOLONG
));
2155 spa_load_verify(spa_t
*spa
)
2158 spa_load_error_t sle
= { 0 };
2159 zpool_load_policy_t policy
;
2160 boolean_t verify_ok
= B_FALSE
;
2163 zpool_get_load_policy(spa
->spa_config
, &policy
);
2165 if (policy
.zlp_rewind
& ZPOOL_NEVER_REWIND
)
2168 dsl_pool_config_enter(spa
->spa_dsl_pool
, FTAG
);
2169 error
= dmu_objset_find_dp(spa
->spa_dsl_pool
,
2170 spa
->spa_dsl_pool
->dp_root_dir_obj
, verify_dataset_name_len
, NULL
,
2172 dsl_pool_config_exit(spa
->spa_dsl_pool
, FTAG
);
2176 rio
= zio_root(spa
, NULL
, &sle
,
2177 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
);
2179 if (spa_load_verify_metadata
) {
2180 if (spa
->spa_extreme_rewind
) {
2181 spa_load_note(spa
, "performing a complete scan of the "
2182 "pool since extreme rewind is on. This may take "
2183 "a very long time.\n (spa_load_verify_data=%u, "
2184 "spa_load_verify_metadata=%u)",
2185 spa_load_verify_data
, spa_load_verify_metadata
);
2187 error
= traverse_pool(spa
, spa
->spa_verify_min_txg
,
2188 TRAVERSE_PRE
| TRAVERSE_PREFETCH_METADATA
|
2189 TRAVERSE_NO_DECRYPT
, spa_load_verify_cb
, rio
);
2192 (void) zio_wait(rio
);
2194 spa
->spa_load_meta_errors
= sle
.sle_meta_count
;
2195 spa
->spa_load_data_errors
= sle
.sle_data_count
;
2197 if (sle
.sle_meta_count
!= 0 || sle
.sle_data_count
!= 0) {
2198 spa_load_note(spa
, "spa_load_verify found %llu metadata errors "
2199 "and %llu data errors", (u_longlong_t
)sle
.sle_meta_count
,
2200 (u_longlong_t
)sle
.sle_data_count
);
2203 if (spa_load_verify_dryrun
||
2204 (!error
&& sle
.sle_meta_count
<= policy
.zlp_maxmeta
&&
2205 sle
.sle_data_count
<= policy
.zlp_maxdata
)) {
2209 spa
->spa_load_txg
= spa
->spa_uberblock
.ub_txg
;
2210 spa
->spa_load_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
2212 loss
= spa
->spa_last_ubsync_txg_ts
- spa
->spa_load_txg_ts
;
2213 VERIFY(nvlist_add_uint64(spa
->spa_load_info
,
2214 ZPOOL_CONFIG_LOAD_TIME
, spa
->spa_load_txg_ts
) == 0);
2215 VERIFY(nvlist_add_int64(spa
->spa_load_info
,
2216 ZPOOL_CONFIG_REWIND_TIME
, loss
) == 0);
2217 VERIFY(nvlist_add_uint64(spa
->spa_load_info
,
2218 ZPOOL_CONFIG_LOAD_DATA_ERRORS
, sle
.sle_data_count
) == 0);
2220 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
;
2223 if (spa_load_verify_dryrun
)
2227 if (error
!= ENXIO
&& error
!= EIO
)
2228 error
= SET_ERROR(EIO
);
2232 return (verify_ok
? 0 : EIO
);
2236 * Find a value in the pool props object.
2239 spa_prop_find(spa_t
*spa
, zpool_prop_t prop
, uint64_t *val
)
2241 (void) zap_lookup(spa
->spa_meta_objset
, spa
->spa_pool_props_object
,
2242 zpool_prop_to_name(prop
), sizeof (uint64_t), 1, val
);
2246 * Find a value in the pool directory object.
2249 spa_dir_prop(spa_t
*spa
, const char *name
, uint64_t *val
, boolean_t log_enoent
)
2251 int error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
2252 name
, sizeof (uint64_t), 1, val
);
2254 if (error
!= 0 && (error
!= ENOENT
|| log_enoent
)) {
2255 spa_load_failed(spa
, "couldn't get '%s' value in MOS directory "
2256 "[error=%d]", name
, error
);
2263 spa_vdev_err(vdev_t
*vdev
, vdev_aux_t aux
, int err
)
2265 vdev_set_state(vdev
, B_TRUE
, VDEV_STATE_CANT_OPEN
, aux
);
2266 return (SET_ERROR(err
));
2270 spa_spawn_aux_threads(spa_t
*spa
)
2272 ASSERT(spa_writeable(spa
));
2274 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
2276 spa_start_indirect_condensing_thread(spa
);
2278 ASSERT3P(spa
->spa_checkpoint_discard_zthr
, ==, NULL
);
2279 spa
->spa_checkpoint_discard_zthr
=
2280 zthr_create(spa_checkpoint_discard_thread_check
,
2281 spa_checkpoint_discard_thread
, spa
);
2285 * Fix up config after a partly-completed split. This is done with the
2286 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
2287 * pool have that entry in their config, but only the splitting one contains
2288 * a list of all the guids of the vdevs that are being split off.
2290 * This function determines what to do with that list: either rejoin
2291 * all the disks to the pool, or complete the splitting process. To attempt
2292 * the rejoin, each disk that is offlined is marked online again, and
2293 * we do a reopen() call. If the vdev label for every disk that was
2294 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
2295 * then we call vdev_split() on each disk, and complete the split.
2297 * Otherwise we leave the config alone, with all the vdevs in place in
2298 * the original pool.
2301 spa_try_repair(spa_t
*spa
, nvlist_t
*config
)
2308 boolean_t attempt_reopen
;
2310 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
, &nvl
) != 0)
2313 /* check that the config is complete */
2314 if (nvlist_lookup_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
2315 &glist
, &gcount
) != 0)
2318 vd
= kmem_zalloc(gcount
* sizeof (vdev_t
*), KM_SLEEP
);
2320 /* attempt to online all the vdevs & validate */
2321 attempt_reopen
= B_TRUE
;
2322 for (i
= 0; i
< gcount
; i
++) {
2323 if (glist
[i
] == 0) /* vdev is hole */
2326 vd
[i
] = spa_lookup_by_guid(spa
, glist
[i
], B_FALSE
);
2327 if (vd
[i
] == NULL
) {
2329 * Don't bother attempting to reopen the disks;
2330 * just do the split.
2332 attempt_reopen
= B_FALSE
;
2334 /* attempt to re-online it */
2335 vd
[i
]->vdev_offline
= B_FALSE
;
2339 if (attempt_reopen
) {
2340 vdev_reopen(spa
->spa_root_vdev
);
2342 /* check each device to see what state it's in */
2343 for (extracted
= 0, i
= 0; i
< gcount
; i
++) {
2344 if (vd
[i
] != NULL
&&
2345 vd
[i
]->vdev_stat
.vs_aux
!= VDEV_AUX_SPLIT_POOL
)
2352 * If every disk has been moved to the new pool, or if we never
2353 * even attempted to look at them, then we split them off for
2356 if (!attempt_reopen
|| gcount
== extracted
) {
2357 for (i
= 0; i
< gcount
; i
++)
2360 vdev_reopen(spa
->spa_root_vdev
);
2363 kmem_free(vd
, gcount
* sizeof (vdev_t
*));
2367 spa_load(spa_t
*spa
, spa_load_state_t state
, spa_import_type_t type
)
2369 char *ereport
= FM_EREPORT_ZFS_POOL
;
2372 spa
->spa_load_state
= state
;
2374 gethrestime(&spa
->spa_loaded_ts
);
2375 error
= spa_load_impl(spa
, type
, &ereport
);
2378 * Don't count references from objsets that are already closed
2379 * and are making their way through the eviction process.
2381 spa_evicting_os_wait(spa
);
2382 spa
->spa_minref
= zfs_refcount_count(&spa
->spa_refcount
);
2384 if (error
!= EEXIST
) {
2385 spa
->spa_loaded_ts
.tv_sec
= 0;
2386 spa
->spa_loaded_ts
.tv_nsec
= 0;
2388 if (error
!= EBADF
) {
2389 zfs_ereport_post(ereport
, spa
, NULL
, NULL
, NULL
, 0, 0);
2392 spa
->spa_load_state
= error
? SPA_LOAD_ERROR
: SPA_LOAD_NONE
;
2400 * Count the number of per-vdev ZAPs associated with all of the vdevs in the
2401 * vdev tree rooted in the given vd, and ensure that each ZAP is present in the
2402 * spa's per-vdev ZAP list.
2405 vdev_count_verify_zaps(vdev_t
*vd
)
2407 spa_t
*spa
= vd
->vdev_spa
;
2410 if (vd
->vdev_top_zap
!= 0) {
2412 ASSERT0(zap_lookup_int(spa
->spa_meta_objset
,
2413 spa
->spa_all_vdev_zaps
, vd
->vdev_top_zap
));
2415 if (vd
->vdev_leaf_zap
!= 0) {
2417 ASSERT0(zap_lookup_int(spa
->spa_meta_objset
,
2418 spa
->spa_all_vdev_zaps
, vd
->vdev_leaf_zap
));
2421 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
2422 total
+= vdev_count_verify_zaps(vd
->vdev_child
[i
]);
2430 * Determine whether the activity check is required.
2433 spa_activity_check_required(spa_t
*spa
, uberblock_t
*ub
, nvlist_t
*label
,
2437 uint64_t hostid
= 0;
2438 uint64_t tryconfig_txg
= 0;
2439 uint64_t tryconfig_timestamp
= 0;
2442 if (nvlist_exists(config
, ZPOOL_CONFIG_LOAD_INFO
)) {
2443 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
2444 (void) nvlist_lookup_uint64(nvinfo
, ZPOOL_CONFIG_MMP_TXG
,
2446 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_TIMESTAMP
,
2447 &tryconfig_timestamp
);
2450 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
, &state
);
2453 * Disable the MMP activity check - This is used by zdb which
2454 * is intended to be used on potentially active pools.
2456 if (spa
->spa_import_flags
& ZFS_IMPORT_SKIP_MMP
)
2460 * Skip the activity check when the MMP feature is disabled.
2462 if (ub
->ub_mmp_magic
== MMP_MAGIC
&& ub
->ub_mmp_delay
== 0)
2465 * If the tryconfig_* values are nonzero, they are the results of an
2466 * earlier tryimport. If they match the uberblock we just found, then
2467 * the pool has not changed and we return false so we do not test a
2470 if (tryconfig_txg
&& tryconfig_txg
== ub
->ub_txg
&&
2471 tryconfig_timestamp
&& tryconfig_timestamp
== ub
->ub_timestamp
)
2475 * Allow the activity check to be skipped when importing the pool
2476 * on the same host which last imported it. Since the hostid from
2477 * configuration may be stale use the one read from the label.
2479 if (nvlist_exists(label
, ZPOOL_CONFIG_HOSTID
))
2480 hostid
= fnvlist_lookup_uint64(label
, ZPOOL_CONFIG_HOSTID
);
2482 if (hostid
== spa_get_hostid())
2486 * Skip the activity test when the pool was cleanly exported.
2488 if (state
!= POOL_STATE_ACTIVE
)
2495 * Perform the import activity check. If the user canceled the import or
2496 * we detected activity then fail.
2499 spa_activity_check(spa_t
*spa
, uberblock_t
*ub
, nvlist_t
*config
)
2501 uint64_t import_intervals
= MAX(zfs_multihost_import_intervals
, 1);
2502 uint64_t txg
= ub
->ub_txg
;
2503 uint64_t timestamp
= ub
->ub_timestamp
;
2504 uint64_t import_delay
= NANOSEC
;
2505 hrtime_t import_expire
;
2506 nvlist_t
*mmp_label
= NULL
;
2507 vdev_t
*rvd
= spa
->spa_root_vdev
;
2512 cv_init(&cv
, NULL
, CV_DEFAULT
, NULL
);
2513 mutex_init(&mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
2517 * If ZPOOL_CONFIG_MMP_TXG is present an activity check was performed
2518 * during the earlier tryimport. If the txg recorded there is 0 then
2519 * the pool is known to be active on another host.
2521 * Otherwise, the pool might be in use on another node. Check for
2522 * changes in the uberblocks on disk if necessary.
2524 if (nvlist_exists(config
, ZPOOL_CONFIG_LOAD_INFO
)) {
2525 nvlist_t
*nvinfo
= fnvlist_lookup_nvlist(config
,
2526 ZPOOL_CONFIG_LOAD_INFO
);
2528 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_TXG
) &&
2529 fnvlist_lookup_uint64(nvinfo
, ZPOOL_CONFIG_MMP_TXG
) == 0) {
2530 vdev_uberblock_load(rvd
, ub
, &mmp_label
);
2531 error
= SET_ERROR(EREMOTEIO
);
2537 * Preferentially use the zfs_multihost_interval from the node which
2538 * last imported the pool. This value is stored in an MMP uberblock as.
2540 * ub_mmp_delay * vdev_count_leaves() == zfs_multihost_interval
2542 if (ub
->ub_mmp_magic
== MMP_MAGIC
&& ub
->ub_mmp_delay
)
2543 import_delay
= MAX(import_delay
, import_intervals
*
2544 ub
->ub_mmp_delay
* MAX(vdev_count_leaves(spa
), 1));
2546 /* Apply a floor using the local default values. */
2547 import_delay
= MAX(import_delay
, import_intervals
*
2548 MSEC2NSEC(MAX(zfs_multihost_interval
, MMP_MIN_INTERVAL
)));
2550 zfs_dbgmsg("import_delay=%llu ub_mmp_delay=%llu import_intervals=%u "
2551 "leaves=%u", import_delay
, ub
->ub_mmp_delay
, import_intervals
,
2552 vdev_count_leaves(spa
));
2554 /* Add a small random factor in case of simultaneous imports (0-25%) */
2555 import_expire
= gethrtime() + import_delay
+
2556 (import_delay
* spa_get_random(250) / 1000);
2558 while (gethrtime() < import_expire
) {
2559 vdev_uberblock_load(rvd
, ub
, &mmp_label
);
2561 if (txg
!= ub
->ub_txg
|| timestamp
!= ub
->ub_timestamp
) {
2562 error
= SET_ERROR(EREMOTEIO
);
2567 nvlist_free(mmp_label
);
2571 error
= cv_timedwait_sig(&cv
, &mtx
, ddi_get_lbolt() + hz
);
2573 error
= SET_ERROR(EINTR
);
2581 mutex_destroy(&mtx
);
2585 * If the pool is determined to be active store the status in the
2586 * spa->spa_load_info nvlist. If the remote hostname or hostid are
2587 * available from configuration read from disk store them as well.
2588 * This allows 'zpool import' to generate a more useful message.
2590 * ZPOOL_CONFIG_MMP_STATE - observed pool status (mandatory)
2591 * ZPOOL_CONFIG_MMP_HOSTNAME - hostname from the active pool
2592 * ZPOOL_CONFIG_MMP_HOSTID - hostid from the active pool
2594 if (error
== EREMOTEIO
) {
2595 char *hostname
= "<unknown>";
2596 uint64_t hostid
= 0;
2599 if (nvlist_exists(mmp_label
, ZPOOL_CONFIG_HOSTNAME
)) {
2600 hostname
= fnvlist_lookup_string(mmp_label
,
2601 ZPOOL_CONFIG_HOSTNAME
);
2602 fnvlist_add_string(spa
->spa_load_info
,
2603 ZPOOL_CONFIG_MMP_HOSTNAME
, hostname
);
2606 if (nvlist_exists(mmp_label
, ZPOOL_CONFIG_HOSTID
)) {
2607 hostid
= fnvlist_lookup_uint64(mmp_label
,
2608 ZPOOL_CONFIG_HOSTID
);
2609 fnvlist_add_uint64(spa
->spa_load_info
,
2610 ZPOOL_CONFIG_MMP_HOSTID
, hostid
);
2614 fnvlist_add_uint64(spa
->spa_load_info
,
2615 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_ACTIVE
);
2616 fnvlist_add_uint64(spa
->spa_load_info
,
2617 ZPOOL_CONFIG_MMP_TXG
, 0);
2619 error
= spa_vdev_err(rvd
, VDEV_AUX_ACTIVE
, EREMOTEIO
);
2623 nvlist_free(mmp_label
);
2629 spa_verify_host(spa_t
*spa
, nvlist_t
*mos_config
)
2633 uint64_t myhostid
= 0;
2635 if (!spa_is_root(spa
) && nvlist_lookup_uint64(mos_config
,
2636 ZPOOL_CONFIG_HOSTID
, &hostid
) == 0) {
2637 hostname
= fnvlist_lookup_string(mos_config
,
2638 ZPOOL_CONFIG_HOSTNAME
);
2640 myhostid
= zone_get_hostid(NULL
);
2642 if (hostid
!= 0 && myhostid
!= 0 && hostid
!= myhostid
) {
2643 cmn_err(CE_WARN
, "pool '%s' could not be "
2644 "loaded as it was last accessed by "
2645 "another system (host: %s hostid: 0x%llx). "
2646 "See: http://illumos.org/msg/ZFS-8000-EY",
2647 spa_name(spa
), hostname
, (u_longlong_t
)hostid
);
2648 spa_load_failed(spa
, "hostid verification failed: pool "
2649 "last accessed by host: %s (hostid: 0x%llx)",
2650 hostname
, (u_longlong_t
)hostid
);
2651 return (SET_ERROR(EBADF
));
2659 spa_ld_parse_config(spa_t
*spa
, spa_import_type_t type
)
2662 nvlist_t
*nvtree
, *nvl
, *config
= spa
->spa_config
;
2669 * Versioning wasn't explicitly added to the label until later, so if
2670 * it's not present treat it as the initial version.
2672 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
2673 &spa
->spa_ubsync
.ub_version
) != 0)
2674 spa
->spa_ubsync
.ub_version
= SPA_VERSION_INITIAL
;
2676 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
, &pool_guid
)) {
2677 spa_load_failed(spa
, "invalid config provided: '%s' missing",
2678 ZPOOL_CONFIG_POOL_GUID
);
2679 return (SET_ERROR(EINVAL
));
2683 * If we are doing an import, ensure that the pool is not already
2684 * imported by checking if its pool guid already exists in the
2687 * The only case that we allow an already imported pool to be
2688 * imported again, is when the pool is checkpointed and we want to
2689 * look at its checkpointed state from userland tools like zdb.
2692 if ((spa
->spa_load_state
== SPA_LOAD_IMPORT
||
2693 spa
->spa_load_state
== SPA_LOAD_TRYIMPORT
) &&
2694 spa_guid_exists(pool_guid
, 0)) {
2696 if ((spa
->spa_load_state
== SPA_LOAD_IMPORT
||
2697 spa
->spa_load_state
== SPA_LOAD_TRYIMPORT
) &&
2698 spa_guid_exists(pool_guid
, 0) &&
2699 !spa_importing_readonly_checkpoint(spa
)) {
2701 spa_load_failed(spa
, "a pool with guid %llu is already open",
2702 (u_longlong_t
)pool_guid
);
2703 return (SET_ERROR(EEXIST
));
2706 spa
->spa_config_guid
= pool_guid
;
2708 nvlist_free(spa
->spa_load_info
);
2709 spa
->spa_load_info
= fnvlist_alloc();
2711 ASSERT(spa
->spa_comment
== NULL
);
2712 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
2713 spa
->spa_comment
= spa_strdup(comment
);
2715 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
2716 &spa
->spa_config_txg
);
2718 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
, &nvl
) == 0)
2719 spa
->spa_config_splitting
= fnvlist_dup(nvl
);
2721 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvtree
)) {
2722 spa_load_failed(spa
, "invalid config provided: '%s' missing",
2723 ZPOOL_CONFIG_VDEV_TREE
);
2724 return (SET_ERROR(EINVAL
));
2728 * Create "The Godfather" zio to hold all async IOs
2730 spa
->spa_async_zio_root
= kmem_alloc(max_ncpus
* sizeof (void *),
2732 for (int i
= 0; i
< max_ncpus
; i
++) {
2733 spa
->spa_async_zio_root
[i
] = zio_root(spa
, NULL
, NULL
,
2734 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
2735 ZIO_FLAG_GODFATHER
);
2739 * Parse the configuration into a vdev tree. We explicitly set the
2740 * value that will be returned by spa_version() since parsing the
2741 * configuration requires knowing the version number.
2743 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2744 parse
= (type
== SPA_IMPORT_EXISTING
?
2745 VDEV_ALLOC_LOAD
: VDEV_ALLOC_SPLIT
);
2746 error
= spa_config_parse(spa
, &rvd
, nvtree
, NULL
, 0, parse
);
2747 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2750 spa_load_failed(spa
, "unable to parse config [error=%d]",
2755 ASSERT(spa
->spa_root_vdev
== rvd
);
2756 ASSERT3U(spa
->spa_min_ashift
, >=, SPA_MINBLOCKSHIFT
);
2757 ASSERT3U(spa
->spa_max_ashift
, <=, SPA_MAXBLOCKSHIFT
);
2759 if (type
!= SPA_IMPORT_ASSEMBLE
) {
2760 ASSERT(spa_guid(spa
) == pool_guid
);
2767 * Recursively open all vdevs in the vdev tree. This function is called twice:
2768 * first with the untrusted config, then with the trusted config.
2771 spa_ld_open_vdevs(spa_t
*spa
)
2776 * spa_missing_tvds_allowed defines how many top-level vdevs can be
2777 * missing/unopenable for the root vdev to be still considered openable.
2779 if (spa
->spa_trust_config
) {
2780 spa
->spa_missing_tvds_allowed
= zfs_max_missing_tvds
;
2781 } else if (spa
->spa_config_source
== SPA_CONFIG_SRC_CACHEFILE
) {
2782 spa
->spa_missing_tvds_allowed
= zfs_max_missing_tvds_cachefile
;
2783 } else if (spa
->spa_config_source
== SPA_CONFIG_SRC_SCAN
) {
2784 spa
->spa_missing_tvds_allowed
= zfs_max_missing_tvds_scan
;
2786 spa
->spa_missing_tvds_allowed
= 0;
2789 spa
->spa_missing_tvds_allowed
=
2790 MAX(zfs_max_missing_tvds
, spa
->spa_missing_tvds_allowed
);
2792 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2793 error
= vdev_open(spa
->spa_root_vdev
);
2794 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2796 if (spa
->spa_missing_tvds
!= 0) {
2797 spa_load_note(spa
, "vdev tree has %lld missing top-level "
2798 "vdevs.", (u_longlong_t
)spa
->spa_missing_tvds
);
2799 if (spa
->spa_trust_config
&& (spa
->spa_mode
& FWRITE
)) {
2801 * Although theoretically we could allow users to open
2802 * incomplete pools in RW mode, we'd need to add a lot
2803 * of extra logic (e.g. adjust pool space to account
2804 * for missing vdevs).
2805 * This limitation also prevents users from accidentally
2806 * opening the pool in RW mode during data recovery and
2807 * damaging it further.
2809 spa_load_note(spa
, "pools with missing top-level "
2810 "vdevs can only be opened in read-only mode.");
2811 error
= SET_ERROR(ENXIO
);
2813 spa_load_note(spa
, "current settings allow for maximum "
2814 "%lld missing top-level vdevs at this stage.",
2815 (u_longlong_t
)spa
->spa_missing_tvds_allowed
);
2819 spa_load_failed(spa
, "unable to open vdev tree [error=%d]",
2822 if (spa
->spa_missing_tvds
!= 0 || error
!= 0)
2823 vdev_dbgmsg_print_tree(spa
->spa_root_vdev
, 2);
2829 * We need to validate the vdev labels against the configuration that
2830 * we have in hand. This function is called twice: first with an untrusted
2831 * config, then with a trusted config. The validation is more strict when the
2832 * config is trusted.
2835 spa_ld_validate_vdevs(spa_t
*spa
)
2838 vdev_t
*rvd
= spa
->spa_root_vdev
;
2840 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2841 error
= vdev_validate(rvd
);
2842 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2845 spa_load_failed(spa
, "vdev_validate failed [error=%d]", error
);
2849 if (rvd
->vdev_state
<= VDEV_STATE_CANT_OPEN
) {
2850 spa_load_failed(spa
, "cannot open vdev tree after invalidating "
2852 vdev_dbgmsg_print_tree(rvd
, 2);
2853 return (SET_ERROR(ENXIO
));
2860 spa_ld_select_uberblock_done(spa_t
*spa
, uberblock_t
*ub
)
2862 spa
->spa_state
= POOL_STATE_ACTIVE
;
2863 spa
->spa_ubsync
= spa
->spa_uberblock
;
2864 spa
->spa_verify_min_txg
= spa
->spa_extreme_rewind
?
2865 TXG_INITIAL
- 1 : spa_last_synced_txg(spa
) - TXG_DEFER_SIZE
- 1;
2866 spa
->spa_first_txg
= spa
->spa_last_ubsync_txg
?
2867 spa
->spa_last_ubsync_txg
: spa_last_synced_txg(spa
) + 1;
2868 spa
->spa_claim_max_txg
= spa
->spa_first_txg
;
2869 spa
->spa_prev_software_version
= ub
->ub_software_version
;
2873 spa_ld_select_uberblock(spa_t
*spa
, spa_import_type_t type
)
2875 vdev_t
*rvd
= spa
->spa_root_vdev
;
2877 uberblock_t
*ub
= &spa
->spa_uberblock
;
2878 boolean_t activity_check
= B_FALSE
;
2881 * If we are opening the checkpointed state of the pool by
2882 * rewinding to it, at this point we will have written the
2883 * checkpointed uberblock to the vdev labels, so searching
2884 * the labels will find the right uberblock. However, if
2885 * we are opening the checkpointed state read-only, we have
2886 * not modified the labels. Therefore, we must ignore the
2887 * labels and continue using the spa_uberblock that was set
2888 * by spa_ld_checkpoint_rewind.
2890 * Note that it would be fine to ignore the labels when
2891 * rewinding (opening writeable) as well. However, if we
2892 * crash just after writing the labels, we will end up
2893 * searching the labels. Doing so in the common case means
2894 * that this code path gets exercised normally, rather than
2895 * just in the edge case.
2897 if (ub
->ub_checkpoint_txg
!= 0 &&
2898 spa_importing_readonly_checkpoint(spa
)) {
2899 spa_ld_select_uberblock_done(spa
, ub
);
2904 * Find the best uberblock.
2906 vdev_uberblock_load(rvd
, ub
, &label
);
2909 * If we weren't able to find a single valid uberblock, return failure.
2911 if (ub
->ub_txg
== 0) {
2913 spa_load_failed(spa
, "no valid uberblock found");
2914 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, ENXIO
));
2917 spa_load_note(spa
, "using uberblock with txg=%llu",
2918 (u_longlong_t
)ub
->ub_txg
);
2922 * For pools which have the multihost property on determine if the
2923 * pool is truly inactive and can be safely imported. Prevent
2924 * hosts which don't have a hostid set from importing the pool.
2926 activity_check
= spa_activity_check_required(spa
, ub
, label
,
2928 if (activity_check
) {
2929 if (ub
->ub_mmp_magic
== MMP_MAGIC
&& ub
->ub_mmp_delay
&&
2930 spa_get_hostid() == 0) {
2932 fnvlist_add_uint64(spa
->spa_load_info
,
2933 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_NO_HOSTID
);
2934 return (spa_vdev_err(rvd
, VDEV_AUX_ACTIVE
, EREMOTEIO
));
2937 int error
= spa_activity_check(spa
, ub
, spa
->spa_config
);
2943 fnvlist_add_uint64(spa
->spa_load_info
,
2944 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_INACTIVE
);
2945 fnvlist_add_uint64(spa
->spa_load_info
,
2946 ZPOOL_CONFIG_MMP_TXG
, ub
->ub_txg
);
2950 * If the pool has an unsupported version we can't open it.
2952 if (!SPA_VERSION_IS_SUPPORTED(ub
->ub_version
)) {
2954 spa_load_failed(spa
, "version %llu is not supported",
2955 (u_longlong_t
)ub
->ub_version
);
2956 return (spa_vdev_err(rvd
, VDEV_AUX_VERSION_NEWER
, ENOTSUP
));
2959 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
2963 * If we weren't able to find what's necessary for reading the
2964 * MOS in the label, return failure.
2966 if (label
== NULL
) {
2967 spa_load_failed(spa
, "label config unavailable");
2968 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
2972 if (nvlist_lookup_nvlist(label
, ZPOOL_CONFIG_FEATURES_FOR_READ
,
2975 spa_load_failed(spa
, "invalid label: '%s' missing",
2976 ZPOOL_CONFIG_FEATURES_FOR_READ
);
2977 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
2982 * Update our in-core representation with the definitive values
2985 nvlist_free(spa
->spa_label_features
);
2986 VERIFY(nvlist_dup(features
, &spa
->spa_label_features
, 0) == 0);
2992 * Look through entries in the label nvlist's features_for_read. If
2993 * there is a feature listed there which we don't understand then we
2994 * cannot open a pool.
2996 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
2997 nvlist_t
*unsup_feat
;
2999 VERIFY(nvlist_alloc(&unsup_feat
, NV_UNIQUE_NAME
, KM_SLEEP
) ==
3002 for (nvpair_t
*nvp
= nvlist_next_nvpair(spa
->spa_label_features
,
3004 nvp
= nvlist_next_nvpair(spa
->spa_label_features
, nvp
)) {
3005 if (!zfeature_is_supported(nvpair_name(nvp
))) {
3006 VERIFY(nvlist_add_string(unsup_feat
,
3007 nvpair_name(nvp
), "") == 0);
3011 if (!nvlist_empty(unsup_feat
)) {
3012 VERIFY(nvlist_add_nvlist(spa
->spa_load_info
,
3013 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
) == 0);
3014 nvlist_free(unsup_feat
);
3015 spa_load_failed(spa
, "some features are unsupported");
3016 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
3020 nvlist_free(unsup_feat
);
3023 if (type
!= SPA_IMPORT_ASSEMBLE
&& spa
->spa_config_splitting
) {
3024 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3025 spa_try_repair(spa
, spa
->spa_config
);
3026 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3027 nvlist_free(spa
->spa_config_splitting
);
3028 spa
->spa_config_splitting
= NULL
;
3032 * Initialize internal SPA structures.
3034 spa_ld_select_uberblock_done(spa
, ub
);
3040 spa_ld_open_rootbp(spa_t
*spa
)
3043 vdev_t
*rvd
= spa
->spa_root_vdev
;
3045 error
= dsl_pool_init(spa
, spa
->spa_first_txg
, &spa
->spa_dsl_pool
);
3047 spa_load_failed(spa
, "unable to open rootbp in dsl_pool_init "
3048 "[error=%d]", error
);
3049 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3051 spa
->spa_meta_objset
= spa
->spa_dsl_pool
->dp_meta_objset
;
3057 spa_ld_trusted_config(spa_t
*spa
, spa_import_type_t type
,
3058 boolean_t reloading
)
3060 vdev_t
*mrvd
, *rvd
= spa
->spa_root_vdev
;
3061 nvlist_t
*nv
, *mos_config
, *policy
;
3062 int error
= 0, copy_error
;
3063 uint64_t healthy_tvds
, healthy_tvds_mos
;
3064 uint64_t mos_config_txg
;
3066 if (spa_dir_prop(spa
, DMU_POOL_CONFIG
, &spa
->spa_config_object
, B_TRUE
)
3068 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3071 * If we're assembling a pool from a split, the config provided is
3072 * already trusted so there is nothing to do.
3074 if (type
== SPA_IMPORT_ASSEMBLE
)
3077 healthy_tvds
= spa_healthy_core_tvds(spa
);
3079 if (load_nvlist(spa
, spa
->spa_config_object
, &mos_config
)
3081 spa_load_failed(spa
, "unable to retrieve MOS config");
3082 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3086 * If we are doing an open, pool owner wasn't verified yet, thus do
3087 * the verification here.
3089 if (spa
->spa_load_state
== SPA_LOAD_OPEN
) {
3090 error
= spa_verify_host(spa
, mos_config
);
3092 nvlist_free(mos_config
);
3097 nv
= fnvlist_lookup_nvlist(mos_config
, ZPOOL_CONFIG_VDEV_TREE
);
3099 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3102 * Build a new vdev tree from the trusted config
3104 VERIFY(spa_config_parse(spa
, &mrvd
, nv
, NULL
, 0, VDEV_ALLOC_LOAD
) == 0);
3107 * Vdev paths in the MOS may be obsolete. If the untrusted config was
3108 * obtained by scanning /dev/dsk, then it will have the right vdev
3109 * paths. We update the trusted MOS config with this information.
3110 * We first try to copy the paths with vdev_copy_path_strict, which
3111 * succeeds only when both configs have exactly the same vdev tree.
3112 * If that fails, we fall back to a more flexible method that has a
3113 * best effort policy.
3115 copy_error
= vdev_copy_path_strict(rvd
, mrvd
);
3116 if (copy_error
!= 0 || spa_load_print_vdev_tree
) {
3117 spa_load_note(spa
, "provided vdev tree:");
3118 vdev_dbgmsg_print_tree(rvd
, 2);
3119 spa_load_note(spa
, "MOS vdev tree:");
3120 vdev_dbgmsg_print_tree(mrvd
, 2);
3122 if (copy_error
!= 0) {
3123 spa_load_note(spa
, "vdev_copy_path_strict failed, falling "
3124 "back to vdev_copy_path_relaxed");
3125 vdev_copy_path_relaxed(rvd
, mrvd
);
3130 spa
->spa_root_vdev
= mrvd
;
3132 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3135 * We will use spa_config if we decide to reload the spa or if spa_load
3136 * fails and we rewind. We must thus regenerate the config using the
3137 * MOS information with the updated paths. ZPOOL_LOAD_POLICY is used to
3138 * pass settings on how to load the pool and is not stored in the MOS.
3139 * We copy it over to our new, trusted config.
3141 mos_config_txg
= fnvlist_lookup_uint64(mos_config
,
3142 ZPOOL_CONFIG_POOL_TXG
);
3143 nvlist_free(mos_config
);
3144 mos_config
= spa_config_generate(spa
, NULL
, mos_config_txg
, B_FALSE
);
3145 if (nvlist_lookup_nvlist(spa
->spa_config
, ZPOOL_LOAD_POLICY
,
3147 fnvlist_add_nvlist(mos_config
, ZPOOL_LOAD_POLICY
, policy
);
3148 spa_config_set(spa
, mos_config
);
3149 spa
->spa_config_source
= SPA_CONFIG_SRC_MOS
;
3152 * Now that we got the config from the MOS, we should be more strict
3153 * in checking blkptrs and can make assumptions about the consistency
3154 * of the vdev tree. spa_trust_config must be set to true before opening
3155 * vdevs in order for them to be writeable.
3157 spa
->spa_trust_config
= B_TRUE
;
3160 * Open and validate the new vdev tree
3162 error
= spa_ld_open_vdevs(spa
);
3166 error
= spa_ld_validate_vdevs(spa
);
3170 if (copy_error
!= 0 || spa_load_print_vdev_tree
) {
3171 spa_load_note(spa
, "final vdev tree:");
3172 vdev_dbgmsg_print_tree(rvd
, 2);
3175 if (spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
&&
3176 !spa
->spa_extreme_rewind
&& zfs_max_missing_tvds
== 0) {
3178 * Sanity check to make sure that we are indeed loading the
3179 * latest uberblock. If we missed SPA_SYNC_MIN_VDEVS tvds
3180 * in the config provided and they happened to be the only ones
3181 * to have the latest uberblock, we could involuntarily perform
3182 * an extreme rewind.
3184 healthy_tvds_mos
= spa_healthy_core_tvds(spa
);
3185 if (healthy_tvds_mos
- healthy_tvds
>=
3186 SPA_SYNC_MIN_VDEVS
) {
3187 spa_load_note(spa
, "config provided misses too many "
3188 "top-level vdevs compared to MOS (%lld vs %lld). ",
3189 (u_longlong_t
)healthy_tvds
,
3190 (u_longlong_t
)healthy_tvds_mos
);
3191 spa_load_note(spa
, "vdev tree:");
3192 vdev_dbgmsg_print_tree(rvd
, 2);
3194 spa_load_failed(spa
, "config was already "
3195 "provided from MOS. Aborting.");
3196 return (spa_vdev_err(rvd
,
3197 VDEV_AUX_CORRUPT_DATA
, EIO
));
3199 spa_load_note(spa
, "spa must be reloaded using MOS "
3201 return (SET_ERROR(EAGAIN
));
3205 error
= spa_check_for_missing_logs(spa
);
3207 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
, ENXIO
));
3209 if (rvd
->vdev_guid_sum
!= spa
->spa_uberblock
.ub_guid_sum
) {
3210 spa_load_failed(spa
, "uberblock guid sum doesn't match MOS "
3211 "guid sum (%llu != %llu)",
3212 (u_longlong_t
)spa
->spa_uberblock
.ub_guid_sum
,
3213 (u_longlong_t
)rvd
->vdev_guid_sum
);
3214 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
,
3222 spa_ld_open_indirect_vdev_metadata(spa_t
*spa
)
3225 vdev_t
*rvd
= spa
->spa_root_vdev
;
3228 * Everything that we read before spa_remove_init() must be stored
3229 * on concreted vdevs. Therefore we do this as early as possible.
3231 error
= spa_remove_init(spa
);
3233 spa_load_failed(spa
, "spa_remove_init failed [error=%d]",
3235 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3239 * Retrieve information needed to condense indirect vdev mappings.
3241 error
= spa_condense_init(spa
);
3243 spa_load_failed(spa
, "spa_condense_init failed [error=%d]",
3245 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, error
));
3252 spa_ld_check_features(spa_t
*spa
, boolean_t
*missing_feat_writep
)
3255 vdev_t
*rvd
= spa
->spa_root_vdev
;
3257 if (spa_version(spa
) >= SPA_VERSION_FEATURES
) {
3258 boolean_t missing_feat_read
= B_FALSE
;
3259 nvlist_t
*unsup_feat
, *enabled_feat
;
3261 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_READ
,
3262 &spa
->spa_feat_for_read_obj
, B_TRUE
) != 0) {
3263 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3266 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_WRITE
,
3267 &spa
->spa_feat_for_write_obj
, B_TRUE
) != 0) {
3268 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3271 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_DESCRIPTIONS
,
3272 &spa
->spa_feat_desc_obj
, B_TRUE
) != 0) {
3273 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3276 enabled_feat
= fnvlist_alloc();
3277 unsup_feat
= fnvlist_alloc();
3279 if (!spa_features_check(spa
, B_FALSE
,
3280 unsup_feat
, enabled_feat
))
3281 missing_feat_read
= B_TRUE
;
3283 if (spa_writeable(spa
) ||
3284 spa
->spa_load_state
== SPA_LOAD_TRYIMPORT
) {
3285 if (!spa_features_check(spa
, B_TRUE
,
3286 unsup_feat
, enabled_feat
)) {
3287 *missing_feat_writep
= B_TRUE
;
3291 fnvlist_add_nvlist(spa
->spa_load_info
,
3292 ZPOOL_CONFIG_ENABLED_FEAT
, enabled_feat
);
3294 if (!nvlist_empty(unsup_feat
)) {
3295 fnvlist_add_nvlist(spa
->spa_load_info
,
3296 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
);
3299 fnvlist_free(enabled_feat
);
3300 fnvlist_free(unsup_feat
);
3302 if (!missing_feat_read
) {
3303 fnvlist_add_boolean(spa
->spa_load_info
,
3304 ZPOOL_CONFIG_CAN_RDONLY
);
3308 * If the state is SPA_LOAD_TRYIMPORT, our objective is
3309 * twofold: to determine whether the pool is available for
3310 * import in read-write mode and (if it is not) whether the
3311 * pool is available for import in read-only mode. If the pool
3312 * is available for import in read-write mode, it is displayed
3313 * as available in userland; if it is not available for import
3314 * in read-only mode, it is displayed as unavailable in
3315 * userland. If the pool is available for import in read-only
3316 * mode but not read-write mode, it is displayed as unavailable
3317 * in userland with a special note that the pool is actually
3318 * available for open in read-only mode.
3320 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
3321 * missing a feature for write, we must first determine whether
3322 * the pool can be opened read-only before returning to
3323 * userland in order to know whether to display the
3324 * abovementioned note.
3326 if (missing_feat_read
|| (*missing_feat_writep
&&
3327 spa_writeable(spa
))) {
3328 spa_load_failed(spa
, "pool uses unsupported features");
3329 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
3334 * Load refcounts for ZFS features from disk into an in-memory
3335 * cache during SPA initialization.
3337 for (spa_feature_t i
= 0; i
< SPA_FEATURES
; i
++) {
3340 error
= feature_get_refcount_from_disk(spa
,
3341 &spa_feature_table
[i
], &refcount
);
3343 spa
->spa_feat_refcount_cache
[i
] = refcount
;
3344 } else if (error
== ENOTSUP
) {
3345 spa
->spa_feat_refcount_cache
[i
] =
3346 SPA_FEATURE_DISABLED
;
3348 spa_load_failed(spa
, "error getting refcount "
3349 "for feature %s [error=%d]",
3350 spa_feature_table
[i
].fi_guid
, error
);
3351 return (spa_vdev_err(rvd
,
3352 VDEV_AUX_CORRUPT_DATA
, EIO
));
3357 if (spa_feature_is_active(spa
, SPA_FEATURE_ENABLED_TXG
)) {
3358 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_ENABLED_TXG
,
3359 &spa
->spa_feat_enabled_txg_obj
, B_TRUE
) != 0)
3360 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3367 spa_ld_load_special_directories(spa_t
*spa
)
3370 vdev_t
*rvd
= spa
->spa_root_vdev
;
3372 spa
->spa_is_initializing
= B_TRUE
;
3373 error
= dsl_pool_open(spa
->spa_dsl_pool
);
3374 spa
->spa_is_initializing
= B_FALSE
;
3376 spa_load_failed(spa
, "dsl_pool_open failed [error=%d]", error
);
3377 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3384 spa_ld_get_props(spa_t
*spa
)
3388 vdev_t
*rvd
= spa
->spa_root_vdev
;
3390 /* Grab the checksum salt from the MOS. */
3391 error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
3392 DMU_POOL_CHECKSUM_SALT
, 1,
3393 sizeof (spa
->spa_cksum_salt
.zcs_bytes
),
3394 spa
->spa_cksum_salt
.zcs_bytes
);
3395 if (error
== ENOENT
) {
3396 /* Generate a new salt for subsequent use */
3397 (void) random_get_pseudo_bytes(spa
->spa_cksum_salt
.zcs_bytes
,
3398 sizeof (spa
->spa_cksum_salt
.zcs_bytes
));
3399 } else if (error
!= 0) {
3400 spa_load_failed(spa
, "unable to retrieve checksum salt from "
3401 "MOS [error=%d]", error
);
3402 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3405 if (spa_dir_prop(spa
, DMU_POOL_SYNC_BPOBJ
, &obj
, B_TRUE
) != 0)
3406 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3407 error
= bpobj_open(&spa
->spa_deferred_bpobj
, spa
->spa_meta_objset
, obj
);
3409 spa_load_failed(spa
, "error opening deferred-frees bpobj "
3410 "[error=%d]", error
);
3411 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3415 * Load the bit that tells us to use the new accounting function
3416 * (raid-z deflation). If we have an older pool, this will not
3419 error
= spa_dir_prop(spa
, DMU_POOL_DEFLATE
, &spa
->spa_deflate
, B_FALSE
);
3420 if (error
!= 0 && error
!= ENOENT
)
3421 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3423 error
= spa_dir_prop(spa
, DMU_POOL_CREATION_VERSION
,
3424 &spa
->spa_creation_version
, B_FALSE
);
3425 if (error
!= 0 && error
!= ENOENT
)
3426 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3429 * Load the persistent error log. If we have an older pool, this will
3432 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_LAST
, &spa
->spa_errlog_last
,
3434 if (error
!= 0 && error
!= ENOENT
)
3435 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3437 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_SCRUB
,
3438 &spa
->spa_errlog_scrub
, B_FALSE
);
3439 if (error
!= 0 && error
!= ENOENT
)
3440 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3443 * Load the history object. If we have an older pool, this
3444 * will not be present.
3446 error
= spa_dir_prop(spa
, DMU_POOL_HISTORY
, &spa
->spa_history
, B_FALSE
);
3447 if (error
!= 0 && error
!= ENOENT
)
3448 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3451 * Load the per-vdev ZAP map. If we have an older pool, this will not
3452 * be present; in this case, defer its creation to a later time to
3453 * avoid dirtying the MOS this early / out of sync context. See
3454 * spa_sync_config_object.
3457 /* The sentinel is only available in the MOS config. */
3458 nvlist_t
*mos_config
;
3459 if (load_nvlist(spa
, spa
->spa_config_object
, &mos_config
) != 0) {
3460 spa_load_failed(spa
, "unable to retrieve MOS config");
3461 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3464 error
= spa_dir_prop(spa
, DMU_POOL_VDEV_ZAP_MAP
,
3465 &spa
->spa_all_vdev_zaps
, B_FALSE
);
3467 if (error
== ENOENT
) {
3468 VERIFY(!nvlist_exists(mos_config
,
3469 ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
));
3470 spa
->spa_avz_action
= AVZ_ACTION_INITIALIZE
;
3471 ASSERT0(vdev_count_verify_zaps(spa
->spa_root_vdev
));
3472 } else if (error
!= 0) {
3473 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3474 } else if (!nvlist_exists(mos_config
, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
)) {
3476 * An older version of ZFS overwrote the sentinel value, so
3477 * we have orphaned per-vdev ZAPs in the MOS. Defer their
3478 * destruction to later; see spa_sync_config_object.
3480 spa
->spa_avz_action
= AVZ_ACTION_DESTROY
;
3482 * We're assuming that no vdevs have had their ZAPs created
3483 * before this. Better be sure of it.
3485 ASSERT0(vdev_count_verify_zaps(spa
->spa_root_vdev
));
3487 nvlist_free(mos_config
);
3489 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
3491 error
= spa_dir_prop(spa
, DMU_POOL_PROPS
, &spa
->spa_pool_props_object
,
3493 if (error
&& error
!= ENOENT
)
3494 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3497 uint64_t autoreplace
;
3499 spa_prop_find(spa
, ZPOOL_PROP_BOOTFS
, &spa
->spa_bootfs
);
3500 spa_prop_find(spa
, ZPOOL_PROP_AUTOREPLACE
, &autoreplace
);
3501 spa_prop_find(spa
, ZPOOL_PROP_DELEGATION
, &spa
->spa_delegation
);
3502 spa_prop_find(spa
, ZPOOL_PROP_FAILUREMODE
, &spa
->spa_failmode
);
3503 spa_prop_find(spa
, ZPOOL_PROP_AUTOEXPAND
, &spa
->spa_autoexpand
);
3504 spa_prop_find(spa
, ZPOOL_PROP_MULTIHOST
, &spa
->spa_multihost
);
3505 spa_prop_find(spa
, ZPOOL_PROP_DEDUPDITTO
,
3506 &spa
->spa_dedup_ditto
);
3508 spa
->spa_autoreplace
= (autoreplace
!= 0);
3512 * If we are importing a pool with missing top-level vdevs,
3513 * we enforce that the pool doesn't panic or get suspended on
3514 * error since the likelihood of missing data is extremely high.
3516 if (spa
->spa_missing_tvds
> 0 &&
3517 spa
->spa_failmode
!= ZIO_FAILURE_MODE_CONTINUE
&&
3518 spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
) {
3519 spa_load_note(spa
, "forcing failmode to 'continue' "
3520 "as some top level vdevs are missing");
3521 spa
->spa_failmode
= ZIO_FAILURE_MODE_CONTINUE
;
3528 spa_ld_open_aux_vdevs(spa_t
*spa
, spa_import_type_t type
)
3531 vdev_t
*rvd
= spa
->spa_root_vdev
;
3534 * If we're assembling the pool from the split-off vdevs of
3535 * an existing pool, we don't want to attach the spares & cache
3540 * Load any hot spares for this pool.
3542 error
= spa_dir_prop(spa
, DMU_POOL_SPARES
, &spa
->spa_spares
.sav_object
,
3544 if (error
!= 0 && error
!= ENOENT
)
3545 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3546 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
3547 ASSERT(spa_version(spa
) >= SPA_VERSION_SPARES
);
3548 if (load_nvlist(spa
, spa
->spa_spares
.sav_object
,
3549 &spa
->spa_spares
.sav_config
) != 0) {
3550 spa_load_failed(spa
, "error loading spares nvlist");
3551 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3554 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3555 spa_load_spares(spa
);
3556 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3557 } else if (error
== 0) {
3558 spa
->spa_spares
.sav_sync
= B_TRUE
;
3562 * Load any level 2 ARC devices for this pool.
3564 error
= spa_dir_prop(spa
, DMU_POOL_L2CACHE
,
3565 &spa
->spa_l2cache
.sav_object
, B_FALSE
);
3566 if (error
!= 0 && error
!= ENOENT
)
3567 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3568 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
3569 ASSERT(spa_version(spa
) >= SPA_VERSION_L2CACHE
);
3570 if (load_nvlist(spa
, spa
->spa_l2cache
.sav_object
,
3571 &spa
->spa_l2cache
.sav_config
) != 0) {
3572 spa_load_failed(spa
, "error loading l2cache nvlist");
3573 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3576 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3577 spa_load_l2cache(spa
);
3578 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3579 } else if (error
== 0) {
3580 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
3587 spa_ld_load_vdev_metadata(spa_t
*spa
)
3590 vdev_t
*rvd
= spa
->spa_root_vdev
;
3593 * If the 'multihost' property is set, then never allow a pool to
3594 * be imported when the system hostid is zero. The exception to
3595 * this rule is zdb which is always allowed to access pools.
3597 if (spa_multihost(spa
) && spa_get_hostid() == 0 &&
3598 (spa
->spa_import_flags
& ZFS_IMPORT_SKIP_MMP
) == 0) {
3599 fnvlist_add_uint64(spa
->spa_load_info
,
3600 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_NO_HOSTID
);
3601 return (spa_vdev_err(rvd
, VDEV_AUX_ACTIVE
, EREMOTEIO
));
3605 * If the 'autoreplace' property is set, then post a resource notifying
3606 * the ZFS DE that it should not issue any faults for unopenable
3607 * devices. We also iterate over the vdevs, and post a sysevent for any
3608 * unopenable vdevs so that the normal autoreplace handler can take
3611 if (spa
->spa_autoreplace
&& spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
) {
3612 spa_check_removed(spa
->spa_root_vdev
);
3614 * For the import case, this is done in spa_import(), because
3615 * at this point we're using the spare definitions from
3616 * the MOS config, not necessarily from the userland config.
3618 if (spa
->spa_load_state
!= SPA_LOAD_IMPORT
) {
3619 spa_aux_check_removed(&spa
->spa_spares
);
3620 spa_aux_check_removed(&spa
->spa_l2cache
);
3625 * Load the vdev metadata such as metaslabs, DTLs, spacemap object, etc.
3627 error
= vdev_load(rvd
);
3629 spa_load_failed(spa
, "vdev_load failed [error=%d]", error
);
3630 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, error
));
3634 * Propagate the leaf DTLs we just loaded all the way up the vdev tree.
3636 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3637 vdev_dtl_reassess(rvd
, 0, 0, B_FALSE
);
3638 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3644 spa_ld_load_dedup_tables(spa_t
*spa
)
3647 vdev_t
*rvd
= spa
->spa_root_vdev
;
3649 error
= ddt_load(spa
);
3651 spa_load_failed(spa
, "ddt_load failed [error=%d]", error
);
3652 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3659 spa_ld_verify_logs(spa_t
*spa
, spa_import_type_t type
, char **ereport
)
3661 vdev_t
*rvd
= spa
->spa_root_vdev
;
3663 if (type
!= SPA_IMPORT_ASSEMBLE
&& spa_writeable(spa
)) {
3664 boolean_t missing
= spa_check_logs(spa
);
3666 if (spa
->spa_missing_tvds
!= 0) {
3667 spa_load_note(spa
, "spa_check_logs failed "
3668 "so dropping the logs");
3670 *ereport
= FM_EREPORT_ZFS_LOG_REPLAY
;
3671 spa_load_failed(spa
, "spa_check_logs failed");
3672 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_LOG
,
3682 spa_ld_verify_pool_data(spa_t
*spa
)
3685 vdev_t
*rvd
= spa
->spa_root_vdev
;
3688 * We've successfully opened the pool, verify that we're ready
3689 * to start pushing transactions.
3691 if (spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
) {
3692 error
= spa_load_verify(spa
);
3694 spa_load_failed(spa
, "spa_load_verify failed "
3695 "[error=%d]", error
);
3696 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
3705 spa_ld_claim_log_blocks(spa_t
*spa
)
3708 dsl_pool_t
*dp
= spa_get_dsl(spa
);
3711 * Claim log blocks that haven't been committed yet.
3712 * This must all happen in a single txg.
3713 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
3714 * invoked from zil_claim_log_block()'s i/o done callback.
3715 * Price of rollback is that we abandon the log.
3717 spa
->spa_claiming
= B_TRUE
;
3719 tx
= dmu_tx_create_assigned(dp
, spa_first_txg(spa
));
3720 (void) dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
3721 zil_claim
, tx
, DS_FIND_CHILDREN
);
3724 spa
->spa_claiming
= B_FALSE
;
3726 spa_set_log_state(spa
, SPA_LOG_GOOD
);
3730 spa_ld_check_for_config_update(spa_t
*spa
, uint64_t config_cache_txg
,
3731 boolean_t update_config_cache
)
3733 vdev_t
*rvd
= spa
->spa_root_vdev
;
3734 int need_update
= B_FALSE
;
3737 * If the config cache is stale, or we have uninitialized
3738 * metaslabs (see spa_vdev_add()), then update the config.
3740 * If this is a verbatim import, trust the current
3741 * in-core spa_config and update the disk labels.
3743 if (update_config_cache
|| config_cache_txg
!= spa
->spa_config_txg
||
3744 spa
->spa_load_state
== SPA_LOAD_IMPORT
||
3745 spa
->spa_load_state
== SPA_LOAD_RECOVER
||
3746 (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
))
3747 need_update
= B_TRUE
;
3749 for (int c
= 0; c
< rvd
->vdev_children
; c
++)
3750 if (rvd
->vdev_child
[c
]->vdev_ms_array
== 0)
3751 need_update
= B_TRUE
;
3754 * Update the config cache asychronously in case we're the
3755 * root pool, in which case the config cache isn't writable yet.
3758 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
3762 spa_ld_prepare_for_reload(spa_t
*spa
)
3764 int mode
= spa
->spa_mode
;
3765 int async_suspended
= spa
->spa_async_suspended
;
3768 spa_deactivate(spa
);
3769 spa_activate(spa
, mode
);
3772 * We save the value of spa_async_suspended as it gets reset to 0 by
3773 * spa_unload(). We want to restore it back to the original value before
3774 * returning as we might be calling spa_async_resume() later.
3776 spa
->spa_async_suspended
= async_suspended
;
3780 spa_ld_read_checkpoint_txg(spa_t
*spa
)
3782 uberblock_t checkpoint
;
3785 ASSERT0(spa
->spa_checkpoint_txg
);
3786 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
3788 error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
3789 DMU_POOL_ZPOOL_CHECKPOINT
, sizeof (uint64_t),
3790 sizeof (uberblock_t
) / sizeof (uint64_t), &checkpoint
);
3792 if (error
== ENOENT
)
3798 ASSERT3U(checkpoint
.ub_txg
, !=, 0);
3799 ASSERT3U(checkpoint
.ub_checkpoint_txg
, !=, 0);
3800 ASSERT3U(checkpoint
.ub_timestamp
, !=, 0);
3801 spa
->spa_checkpoint_txg
= checkpoint
.ub_txg
;
3802 spa
->spa_checkpoint_info
.sci_timestamp
= checkpoint
.ub_timestamp
;
3808 spa_ld_mos_init(spa_t
*spa
, spa_import_type_t type
)
3812 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
3813 ASSERT(spa
->spa_config_source
!= SPA_CONFIG_SRC_NONE
);
3816 * Never trust the config that is provided unless we are assembling
3817 * a pool following a split.
3818 * This means don't trust blkptrs and the vdev tree in general. This
3819 * also effectively puts the spa in read-only mode since
3820 * spa_writeable() checks for spa_trust_config to be true.
3821 * We will later load a trusted config from the MOS.
3823 if (type
!= SPA_IMPORT_ASSEMBLE
)
3824 spa
->spa_trust_config
= B_FALSE
;
3827 * Parse the config provided to create a vdev tree.
3829 error
= spa_ld_parse_config(spa
, type
);
3834 * Now that we have the vdev tree, try to open each vdev. This involves
3835 * opening the underlying physical device, retrieving its geometry and
3836 * probing the vdev with a dummy I/O. The state of each vdev will be set
3837 * based on the success of those operations. After this we'll be ready
3838 * to read from the vdevs.
3840 error
= spa_ld_open_vdevs(spa
);
3845 * Read the label of each vdev and make sure that the GUIDs stored
3846 * there match the GUIDs in the config provided.
3847 * If we're assembling a new pool that's been split off from an
3848 * existing pool, the labels haven't yet been updated so we skip
3849 * validation for now.
3851 if (type
!= SPA_IMPORT_ASSEMBLE
) {
3852 error
= spa_ld_validate_vdevs(spa
);
3858 * Read all vdev labels to find the best uberblock (i.e. latest,
3859 * unless spa_load_max_txg is set) and store it in spa_uberblock. We
3860 * get the list of features required to read blkptrs in the MOS from
3861 * the vdev label with the best uberblock and verify that our version
3862 * of zfs supports them all.
3864 error
= spa_ld_select_uberblock(spa
, type
);
3869 * Pass that uberblock to the dsl_pool layer which will open the root
3870 * blkptr. This blkptr points to the latest version of the MOS and will
3871 * allow us to read its contents.
3873 error
= spa_ld_open_rootbp(spa
);
3881 spa_ld_checkpoint_rewind(spa_t
*spa
)
3883 uberblock_t checkpoint
;
3886 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
3887 ASSERT(spa
->spa_import_flags
& ZFS_IMPORT_CHECKPOINT
);
3889 error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
3890 DMU_POOL_ZPOOL_CHECKPOINT
, sizeof (uint64_t),
3891 sizeof (uberblock_t
) / sizeof (uint64_t), &checkpoint
);
3894 spa_load_failed(spa
, "unable to retrieve checkpointed "
3895 "uberblock from the MOS config [error=%d]", error
);
3897 if (error
== ENOENT
)
3898 error
= ZFS_ERR_NO_CHECKPOINT
;
3903 ASSERT3U(checkpoint
.ub_txg
, <, spa
->spa_uberblock
.ub_txg
);
3904 ASSERT3U(checkpoint
.ub_txg
, ==, checkpoint
.ub_checkpoint_txg
);
3907 * We need to update the txg and timestamp of the checkpointed
3908 * uberblock to be higher than the latest one. This ensures that
3909 * the checkpointed uberblock is selected if we were to close and
3910 * reopen the pool right after we've written it in the vdev labels.
3911 * (also see block comment in vdev_uberblock_compare)
3913 checkpoint
.ub_txg
= spa
->spa_uberblock
.ub_txg
+ 1;
3914 checkpoint
.ub_timestamp
= gethrestime_sec();
3917 * Set current uberblock to be the checkpointed uberblock.
3919 spa
->spa_uberblock
= checkpoint
;
3922 * If we are doing a normal rewind, then the pool is open for
3923 * writing and we sync the "updated" checkpointed uberblock to
3924 * disk. Once this is done, we've basically rewound the whole
3925 * pool and there is no way back.
3927 * There are cases when we don't want to attempt and sync the
3928 * checkpointed uberblock to disk because we are opening a
3929 * pool as read-only. Specifically, verifying the checkpointed
3930 * state with zdb, and importing the checkpointed state to get
3931 * a "preview" of its content.
3933 if (spa_writeable(spa
)) {
3934 vdev_t
*rvd
= spa
->spa_root_vdev
;
3936 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3937 vdev_t
*svd
[SPA_SYNC_MIN_VDEVS
] = { NULL
};
3939 int children
= rvd
->vdev_children
;
3940 int c0
= spa_get_random(children
);
3942 for (int c
= 0; c
< children
; c
++) {
3943 vdev_t
*vd
= rvd
->vdev_child
[(c0
+ c
) % children
];
3945 /* Stop when revisiting the first vdev */
3946 if (c
> 0 && svd
[0] == vd
)
3949 if (vd
->vdev_ms_array
== 0 || vd
->vdev_islog
||
3950 !vdev_is_concrete(vd
))
3953 svd
[svdcount
++] = vd
;
3954 if (svdcount
== SPA_SYNC_MIN_VDEVS
)
3957 error
= vdev_config_sync(svd
, svdcount
, spa
->spa_first_txg
);
3959 spa
->spa_last_synced_guid
= rvd
->vdev_guid
;
3960 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3963 spa_load_failed(spa
, "failed to write checkpointed "
3964 "uberblock to the vdev labels [error=%d]", error
);
3973 spa_ld_mos_with_trusted_config(spa_t
*spa
, spa_import_type_t type
,
3974 boolean_t
*update_config_cache
)
3979 * Parse the config for pool, open and validate vdevs,
3980 * select an uberblock, and use that uberblock to open
3983 error
= spa_ld_mos_init(spa
, type
);
3988 * Retrieve the trusted config stored in the MOS and use it to create
3989 * a new, exact version of the vdev tree, then reopen all vdevs.
3991 error
= spa_ld_trusted_config(spa
, type
, B_FALSE
);
3992 if (error
== EAGAIN
) {
3993 if (update_config_cache
!= NULL
)
3994 *update_config_cache
= B_TRUE
;
3997 * Redo the loading process with the trusted config if it is
3998 * too different from the untrusted config.
4000 spa_ld_prepare_for_reload(spa
);
4001 spa_load_note(spa
, "RELOADING");
4002 error
= spa_ld_mos_init(spa
, type
);
4006 error
= spa_ld_trusted_config(spa
, type
, B_TRUE
);
4010 } else if (error
!= 0) {
4018 * Load an existing storage pool, using the config provided. This config
4019 * describes which vdevs are part of the pool and is later validated against
4020 * partial configs present in each vdev's label and an entire copy of the
4021 * config stored in the MOS.
4024 spa_load_impl(spa_t
*spa
, spa_import_type_t type
, char **ereport
)
4027 boolean_t missing_feat_write
= B_FALSE
;
4028 boolean_t checkpoint_rewind
=
4029 (spa
->spa_import_flags
& ZFS_IMPORT_CHECKPOINT
);
4030 boolean_t update_config_cache
= B_FALSE
;
4032 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
4033 ASSERT(spa
->spa_config_source
!= SPA_CONFIG_SRC_NONE
);
4035 spa_load_note(spa
, "LOADING");
4037 error
= spa_ld_mos_with_trusted_config(spa
, type
, &update_config_cache
);
4042 * If we are rewinding to the checkpoint then we need to repeat
4043 * everything we've done so far in this function but this time
4044 * selecting the checkpointed uberblock and using that to open
4047 if (checkpoint_rewind
) {
4049 * If we are rewinding to the checkpoint update config cache
4052 update_config_cache
= B_TRUE
;
4055 * Extract the checkpointed uberblock from the current MOS
4056 * and use this as the pool's uberblock from now on. If the
4057 * pool is imported as writeable we also write the checkpoint
4058 * uberblock to the labels, making the rewind permanent.
4060 error
= spa_ld_checkpoint_rewind(spa
);
4065 * Redo the loading process process again with the
4066 * checkpointed uberblock.
4068 spa_ld_prepare_for_reload(spa
);
4069 spa_load_note(spa
, "LOADING checkpointed uberblock");
4070 error
= spa_ld_mos_with_trusted_config(spa
, type
, NULL
);
4076 * Retrieve the checkpoint txg if the pool has a checkpoint.
4078 error
= spa_ld_read_checkpoint_txg(spa
);
4083 * Retrieve the mapping of indirect vdevs. Those vdevs were removed
4084 * from the pool and their contents were re-mapped to other vdevs. Note
4085 * that everything that we read before this step must have been
4086 * rewritten on concrete vdevs after the last device removal was
4087 * initiated. Otherwise we could be reading from indirect vdevs before
4088 * we have loaded their mappings.
4090 error
= spa_ld_open_indirect_vdev_metadata(spa
);
4095 * Retrieve the full list of active features from the MOS and check if
4096 * they are all supported.
4098 error
= spa_ld_check_features(spa
, &missing_feat_write
);
4103 * Load several special directories from the MOS needed by the dsl_pool
4106 error
= spa_ld_load_special_directories(spa
);
4111 * Retrieve pool properties from the MOS.
4113 error
= spa_ld_get_props(spa
);
4118 * Retrieve the list of auxiliary devices - cache devices and spares -
4121 error
= spa_ld_open_aux_vdevs(spa
, type
);
4126 * Load the metadata for all vdevs. Also check if unopenable devices
4127 * should be autoreplaced.
4129 error
= spa_ld_load_vdev_metadata(spa
);
4133 error
= spa_ld_load_dedup_tables(spa
);
4138 * Verify the logs now to make sure we don't have any unexpected errors
4139 * when we claim log blocks later.
4141 error
= spa_ld_verify_logs(spa
, type
, ereport
);
4145 if (missing_feat_write
) {
4146 ASSERT(spa
->spa_load_state
== SPA_LOAD_TRYIMPORT
);
4149 * At this point, we know that we can open the pool in
4150 * read-only mode but not read-write mode. We now have enough
4151 * information and can return to userland.
4153 return (spa_vdev_err(spa
->spa_root_vdev
, VDEV_AUX_UNSUP_FEAT
,
4158 * Traverse the last txgs to make sure the pool was left off in a safe
4159 * state. When performing an extreme rewind, we verify the whole pool,
4160 * which can take a very long time.
4162 error
= spa_ld_verify_pool_data(spa
);
4167 * Calculate the deflated space for the pool. This must be done before
4168 * we write anything to the pool because we'd need to update the space
4169 * accounting using the deflated sizes.
4171 spa_update_dspace(spa
);
4174 * We have now retrieved all the information we needed to open the
4175 * pool. If we are importing the pool in read-write mode, a few
4176 * additional steps must be performed to finish the import.
4178 if (spa_writeable(spa
) && (spa
->spa_load_state
== SPA_LOAD_RECOVER
||
4179 spa
->spa_load_max_txg
== UINT64_MAX
)) {
4180 uint64_t config_cache_txg
= spa
->spa_config_txg
;
4182 ASSERT(spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
);
4185 * In case of a checkpoint rewind, log the original txg
4186 * of the checkpointed uberblock.
4188 if (checkpoint_rewind
) {
4189 spa_history_log_internal(spa
, "checkpoint rewind",
4190 NULL
, "rewound state to txg=%llu",
4191 (u_longlong_t
)spa
->spa_uberblock
.ub_checkpoint_txg
);
4195 * Traverse the ZIL and claim all blocks.
4197 spa_ld_claim_log_blocks(spa
);
4200 * Kick-off the syncing thread.
4202 spa
->spa_sync_on
= B_TRUE
;
4203 txg_sync_start(spa
->spa_dsl_pool
);
4204 mmp_thread_start(spa
);
4207 * Wait for all claims to sync. We sync up to the highest
4208 * claimed log block birth time so that claimed log blocks
4209 * don't appear to be from the future. spa_claim_max_txg
4210 * will have been set for us by ZIL traversal operations
4213 txg_wait_synced(spa
->spa_dsl_pool
, spa
->spa_claim_max_txg
);
4216 * Check if we need to request an update of the config. On the
4217 * next sync, we would update the config stored in vdev labels
4218 * and the cachefile (by default /etc/zfs/zpool.cache).
4220 spa_ld_check_for_config_update(spa
, config_cache_txg
,
4221 update_config_cache
);
4224 * Check all DTLs to see if anything needs resilvering.
4226 if (!dsl_scan_resilvering(spa
->spa_dsl_pool
) &&
4227 vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
))
4228 spa_async_request(spa
, SPA_ASYNC_RESILVER
);
4231 * Log the fact that we booted up (so that we can detect if
4232 * we rebooted in the middle of an operation).
4234 spa_history_log_version(spa
, "open", NULL
);
4236 spa_restart_removal(spa
);
4237 spa_spawn_aux_threads(spa
);
4240 * Delete any inconsistent datasets.
4243 * Since we may be issuing deletes for clones here,
4244 * we make sure to do so after we've spawned all the
4245 * auxiliary threads above (from which the livelist
4246 * deletion zthr is part of).
4248 (void) dmu_objset_find(spa_name(spa
),
4249 dsl_destroy_inconsistent
, NULL
, DS_FIND_CHILDREN
);
4252 * Clean up any stale temporary dataset userrefs.
4254 dsl_pool_clean_tmp_userrefs(spa
->spa_dsl_pool
);
4256 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
4257 vdev_initialize_restart(spa
->spa_root_vdev
);
4258 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
4261 spa_load_note(spa
, "LOADED");
4267 spa_load_retry(spa_t
*spa
, spa_load_state_t state
)
4269 int mode
= spa
->spa_mode
;
4272 spa_deactivate(spa
);
4274 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
- 1;
4276 spa_activate(spa
, mode
);
4277 spa_async_suspend(spa
);
4279 spa_load_note(spa
, "spa_load_retry: rewind, max txg: %llu",
4280 (u_longlong_t
)spa
->spa_load_max_txg
);
4282 return (spa_load(spa
, state
, SPA_IMPORT_EXISTING
));
4286 * If spa_load() fails this function will try loading prior txg's. If
4287 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
4288 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
4289 * function will not rewind the pool and will return the same error as
4293 spa_load_best(spa_t
*spa
, spa_load_state_t state
, uint64_t max_request
,
4296 nvlist_t
*loadinfo
= NULL
;
4297 nvlist_t
*config
= NULL
;
4298 int load_error
, rewind_error
;
4299 uint64_t safe_rewind_txg
;
4302 if (spa
->spa_load_txg
&& state
== SPA_LOAD_RECOVER
) {
4303 spa
->spa_load_max_txg
= spa
->spa_load_txg
;
4304 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
4306 spa
->spa_load_max_txg
= max_request
;
4307 if (max_request
!= UINT64_MAX
)
4308 spa
->spa_extreme_rewind
= B_TRUE
;
4311 load_error
= rewind_error
= spa_load(spa
, state
, SPA_IMPORT_EXISTING
);
4312 if (load_error
== 0)
4314 if (load_error
== ZFS_ERR_NO_CHECKPOINT
) {
4316 * When attempting checkpoint-rewind on a pool with no
4317 * checkpoint, we should not attempt to load uberblocks
4318 * from previous txgs when spa_load fails.
4320 ASSERT(spa
->spa_import_flags
& ZFS_IMPORT_CHECKPOINT
);
4321 return (load_error
);
4324 if (spa
->spa_root_vdev
!= NULL
)
4325 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
4327 spa
->spa_last_ubsync_txg
= spa
->spa_uberblock
.ub_txg
;
4328 spa
->spa_last_ubsync_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
4330 if (rewind_flags
& ZPOOL_NEVER_REWIND
) {
4331 nvlist_free(config
);
4332 return (load_error
);
4335 if (state
== SPA_LOAD_RECOVER
) {
4336 /* Price of rolling back is discarding txgs, including log */
4337 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
4340 * If we aren't rolling back save the load info from our first
4341 * import attempt so that we can restore it after attempting
4344 loadinfo
= spa
->spa_load_info
;
4345 spa
->spa_load_info
= fnvlist_alloc();
4348 spa
->spa_load_max_txg
= spa
->spa_last_ubsync_txg
;
4349 safe_rewind_txg
= spa
->spa_last_ubsync_txg
- TXG_DEFER_SIZE
;
4350 min_txg
= (rewind_flags
& ZPOOL_EXTREME_REWIND
) ?
4351 TXG_INITIAL
: safe_rewind_txg
;
4354 * Continue as long as we're finding errors, we're still within
4355 * the acceptable rewind range, and we're still finding uberblocks
4357 while (rewind_error
&& spa
->spa_uberblock
.ub_txg
>= min_txg
&&
4358 spa
->spa_uberblock
.ub_txg
<= spa
->spa_load_max_txg
) {
4359 if (spa
->spa_load_max_txg
< safe_rewind_txg
)
4360 spa
->spa_extreme_rewind
= B_TRUE
;
4361 rewind_error
= spa_load_retry(spa
, state
);
4364 spa
->spa_extreme_rewind
= B_FALSE
;
4365 spa
->spa_load_max_txg
= UINT64_MAX
;
4367 if (config
&& (rewind_error
|| state
!= SPA_LOAD_RECOVER
))
4368 spa_config_set(spa
, config
);
4370 nvlist_free(config
);
4372 if (state
== SPA_LOAD_RECOVER
) {
4373 ASSERT3P(loadinfo
, ==, NULL
);
4374 return (rewind_error
);
4376 /* Store the rewind info as part of the initial load info */
4377 fnvlist_add_nvlist(loadinfo
, ZPOOL_CONFIG_REWIND_INFO
,
4378 spa
->spa_load_info
);
4380 /* Restore the initial load info */
4381 fnvlist_free(spa
->spa_load_info
);
4382 spa
->spa_load_info
= loadinfo
;
4384 return (load_error
);
4391 * The import case is identical to an open except that the configuration is sent
4392 * down from userland, instead of grabbed from the configuration cache. For the
4393 * case of an open, the pool configuration will exist in the
4394 * POOL_STATE_UNINITIALIZED state.
4396 * The stats information (gen/count/ustats) is used to gather vdev statistics at
4397 * the same time open the pool, without having to keep around the spa_t in some
4401 spa_open_common(const char *pool
, spa_t
**spapp
, void *tag
, nvlist_t
*nvpolicy
,
4405 spa_load_state_t state
= SPA_LOAD_OPEN
;
4407 int locked
= B_FALSE
;
4408 int firstopen
= B_FALSE
;
4413 * As disgusting as this is, we need to support recursive calls to this
4414 * function because dsl_dir_open() is called during spa_load(), and ends
4415 * up calling spa_open() again. The real fix is to figure out how to
4416 * avoid dsl_dir_open() calling this in the first place.
4418 if (MUTEX_NOT_HELD(&spa_namespace_lock
)) {
4419 mutex_enter(&spa_namespace_lock
);
4423 if ((spa
= spa_lookup(pool
)) == NULL
) {
4425 mutex_exit(&spa_namespace_lock
);
4426 return (SET_ERROR(ENOENT
));
4429 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
) {
4430 zpool_load_policy_t policy
;
4434 zpool_get_load_policy(nvpolicy
? nvpolicy
: spa
->spa_config
,
4436 if (policy
.zlp_rewind
& ZPOOL_DO_REWIND
)
4437 state
= SPA_LOAD_RECOVER
;
4439 spa_activate(spa
, spa_mode_global
);
4441 if (state
!= SPA_LOAD_RECOVER
)
4442 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
4443 spa
->spa_config_source
= SPA_CONFIG_SRC_CACHEFILE
;
4445 zfs_dbgmsg("spa_open_common: opening %s", pool
);
4446 error
= spa_load_best(spa
, state
, policy
.zlp_txg
,
4449 if (error
== EBADF
) {
4451 * If vdev_validate() returns failure (indicated by
4452 * EBADF), it indicates that one of the vdevs indicates
4453 * that the pool has been exported or destroyed. If
4454 * this is the case, the config cache is out of sync and
4455 * we should remove the pool from the namespace.
4458 spa_deactivate(spa
);
4459 spa_write_cachefile(spa
, B_TRUE
, B_TRUE
);
4462 mutex_exit(&spa_namespace_lock
);
4463 return (SET_ERROR(ENOENT
));
4468 * We can't open the pool, but we still have useful
4469 * information: the state of each vdev after the
4470 * attempted vdev_open(). Return this to the user.
4472 if (config
!= NULL
&& spa
->spa_config
) {
4473 VERIFY(nvlist_dup(spa
->spa_config
, config
,
4475 VERIFY(nvlist_add_nvlist(*config
,
4476 ZPOOL_CONFIG_LOAD_INFO
,
4477 spa
->spa_load_info
) == 0);
4480 spa_deactivate(spa
);
4481 spa
->spa_last_open_failed
= error
;
4483 mutex_exit(&spa_namespace_lock
);
4489 spa_open_ref(spa
, tag
);
4492 *config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
4495 * If we've recovered the pool, pass back any information we
4496 * gathered while doing the load.
4498 if (state
== SPA_LOAD_RECOVER
) {
4499 VERIFY(nvlist_add_nvlist(*config
, ZPOOL_CONFIG_LOAD_INFO
,
4500 spa
->spa_load_info
) == 0);
4504 spa
->spa_last_open_failed
= 0;
4505 spa
->spa_last_ubsync_txg
= 0;
4506 spa
->spa_load_txg
= 0;
4507 mutex_exit(&spa_namespace_lock
);
4511 zvol_create_minors(spa
, spa_name(spa
), B_TRUE
);
4519 spa_open_rewind(const char *name
, spa_t
**spapp
, void *tag
, nvlist_t
*policy
,
4522 return (spa_open_common(name
, spapp
, tag
, policy
, config
));
4526 spa_open(const char *name
, spa_t
**spapp
, void *tag
)
4528 return (spa_open_common(name
, spapp
, tag
, NULL
, NULL
));
4532 * Lookup the given spa_t, incrementing the inject count in the process,
4533 * preventing it from being exported or destroyed.
4536 spa_inject_addref(char *name
)
4540 mutex_enter(&spa_namespace_lock
);
4541 if ((spa
= spa_lookup(name
)) == NULL
) {
4542 mutex_exit(&spa_namespace_lock
);
4545 spa
->spa_inject_ref
++;
4546 mutex_exit(&spa_namespace_lock
);
4552 spa_inject_delref(spa_t
*spa
)
4554 mutex_enter(&spa_namespace_lock
);
4555 spa
->spa_inject_ref
--;
4556 mutex_exit(&spa_namespace_lock
);
4560 * Add spares device information to the nvlist.
4563 spa_add_spares(spa_t
*spa
, nvlist_t
*config
)
4573 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
4575 if (spa
->spa_spares
.sav_count
== 0)
4578 VERIFY(nvlist_lookup_nvlist(config
,
4579 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
4580 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
4581 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
4583 VERIFY(nvlist_add_nvlist_array(nvroot
,
4584 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
4585 VERIFY(nvlist_lookup_nvlist_array(nvroot
,
4586 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
4589 * Go through and find any spares which have since been
4590 * repurposed as an active spare. If this is the case, update
4591 * their status appropriately.
4593 for (i
= 0; i
< nspares
; i
++) {
4594 VERIFY(nvlist_lookup_uint64(spares
[i
],
4595 ZPOOL_CONFIG_GUID
, &guid
) == 0);
4596 if (spa_spare_exists(guid
, &pool
, NULL
) &&
4598 VERIFY(nvlist_lookup_uint64_array(
4599 spares
[i
], ZPOOL_CONFIG_VDEV_STATS
,
4600 (uint64_t **)&vs
, &vsc
) == 0);
4601 vs
->vs_state
= VDEV_STATE_CANT_OPEN
;
4602 vs
->vs_aux
= VDEV_AUX_SPARED
;
4609 * Add l2cache device information to the nvlist, including vdev stats.
4612 spa_add_l2cache(spa_t
*spa
, nvlist_t
*config
)
4615 uint_t i
, j
, nl2cache
;
4622 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
4624 if (spa
->spa_l2cache
.sav_count
== 0)
4627 VERIFY(nvlist_lookup_nvlist(config
,
4628 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
4629 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_l2cache
.sav_config
,
4630 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
4631 if (nl2cache
!= 0) {
4632 VERIFY(nvlist_add_nvlist_array(nvroot
,
4633 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
4634 VERIFY(nvlist_lookup_nvlist_array(nvroot
,
4635 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
4638 * Update level 2 cache device stats.
4641 for (i
= 0; i
< nl2cache
; i
++) {
4642 VERIFY(nvlist_lookup_uint64(l2cache
[i
],
4643 ZPOOL_CONFIG_GUID
, &guid
) == 0);
4646 for (j
= 0; j
< spa
->spa_l2cache
.sav_count
; j
++) {
4648 spa
->spa_l2cache
.sav_vdevs
[j
]->vdev_guid
) {
4649 vd
= spa
->spa_l2cache
.sav_vdevs
[j
];
4655 VERIFY(nvlist_lookup_uint64_array(l2cache
[i
],
4656 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
, &vsc
)
4658 vdev_get_stats(vd
, vs
);
4659 vdev_config_generate_stats(vd
, l2cache
[i
]);
4666 spa_feature_stats_from_disk(spa_t
*spa
, nvlist_t
*features
)
4671 if (spa
->spa_feat_for_read_obj
!= 0) {
4672 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
4673 spa
->spa_feat_for_read_obj
);
4674 zap_cursor_retrieve(&zc
, &za
) == 0;
4675 zap_cursor_advance(&zc
)) {
4676 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
4677 za
.za_num_integers
== 1);
4678 VERIFY0(nvlist_add_uint64(features
, za
.za_name
,
4679 za
.za_first_integer
));
4681 zap_cursor_fini(&zc
);
4684 if (spa
->spa_feat_for_write_obj
!= 0) {
4685 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
4686 spa
->spa_feat_for_write_obj
);
4687 zap_cursor_retrieve(&zc
, &za
) == 0;
4688 zap_cursor_advance(&zc
)) {
4689 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
4690 za
.za_num_integers
== 1);
4691 VERIFY0(nvlist_add_uint64(features
, za
.za_name
,
4692 za
.za_first_integer
));
4694 zap_cursor_fini(&zc
);
4699 spa_feature_stats_from_cache(spa_t
*spa
, nvlist_t
*features
)
4703 for (i
= 0; i
< SPA_FEATURES
; i
++) {
4704 zfeature_info_t feature
= spa_feature_table
[i
];
4707 if (feature_get_refcount(spa
, &feature
, &refcount
) != 0)
4710 VERIFY0(nvlist_add_uint64(features
, feature
.fi_guid
, refcount
));
4715 * Store a list of pool features and their reference counts in the
4718 * The first time this is called on a spa, allocate a new nvlist, fetch
4719 * the pool features and reference counts from disk, then save the list
4720 * in the spa. In subsequent calls on the same spa use the saved nvlist
4721 * and refresh its values from the cached reference counts. This
4722 * ensures we don't block here on I/O on a suspended pool so 'zpool
4723 * clear' can resume the pool.
4726 spa_add_feature_stats(spa_t
*spa
, nvlist_t
*config
)
4730 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
4732 mutex_enter(&spa
->spa_feat_stats_lock
);
4733 features
= spa
->spa_feat_stats
;
4735 if (features
!= NULL
) {
4736 spa_feature_stats_from_cache(spa
, features
);
4738 VERIFY0(nvlist_alloc(&features
, NV_UNIQUE_NAME
, KM_SLEEP
));
4739 spa
->spa_feat_stats
= features
;
4740 spa_feature_stats_from_disk(spa
, features
);
4743 VERIFY0(nvlist_add_nvlist(config
, ZPOOL_CONFIG_FEATURE_STATS
,
4746 mutex_exit(&spa
->spa_feat_stats_lock
);
4750 spa_get_stats(const char *name
, nvlist_t
**config
,
4751 char *altroot
, size_t buflen
)
4757 error
= spa_open_common(name
, &spa
, FTAG
, NULL
, config
);
4761 * This still leaves a window of inconsistency where the spares
4762 * or l2cache devices could change and the config would be
4763 * self-inconsistent.
4765 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
4767 if (*config
!= NULL
) {
4768 uint64_t loadtimes
[2];
4770 loadtimes
[0] = spa
->spa_loaded_ts
.tv_sec
;
4771 loadtimes
[1] = spa
->spa_loaded_ts
.tv_nsec
;
4772 VERIFY(nvlist_add_uint64_array(*config
,
4773 ZPOOL_CONFIG_LOADED_TIME
, loadtimes
, 2) == 0);
4775 VERIFY(nvlist_add_uint64(*config
,
4776 ZPOOL_CONFIG_ERRCOUNT
,
4777 spa_get_errlog_size(spa
)) == 0);
4779 if (spa_suspended(spa
)) {
4780 VERIFY(nvlist_add_uint64(*config
,
4781 ZPOOL_CONFIG_SUSPENDED
,
4782 spa
->spa_failmode
) == 0);
4783 VERIFY(nvlist_add_uint64(*config
,
4784 ZPOOL_CONFIG_SUSPENDED_REASON
,
4785 spa
->spa_suspended
) == 0);
4788 spa_add_spares(spa
, *config
);
4789 spa_add_l2cache(spa
, *config
);
4790 spa_add_feature_stats(spa
, *config
);
4795 * We want to get the alternate root even for faulted pools, so we cheat
4796 * and call spa_lookup() directly.
4800 mutex_enter(&spa_namespace_lock
);
4801 spa
= spa_lookup(name
);
4803 spa_altroot(spa
, altroot
, buflen
);
4807 mutex_exit(&spa_namespace_lock
);
4809 spa_altroot(spa
, altroot
, buflen
);
4814 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
4815 spa_close(spa
, FTAG
);
4822 * Validate that the auxiliary device array is well formed. We must have an
4823 * array of nvlists, each which describes a valid leaf vdev. If this is an
4824 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
4825 * specified, as long as they are well-formed.
4828 spa_validate_aux_devs(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
,
4829 spa_aux_vdev_t
*sav
, const char *config
, uint64_t version
,
4830 vdev_labeltype_t label
)
4837 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
4840 * It's acceptable to have no devs specified.
4842 if (nvlist_lookup_nvlist_array(nvroot
, config
, &dev
, &ndev
) != 0)
4846 return (SET_ERROR(EINVAL
));
4849 * Make sure the pool is formatted with a version that supports this
4852 if (spa_version(spa
) < version
)
4853 return (SET_ERROR(ENOTSUP
));
4856 * Set the pending device list so we correctly handle device in-use
4859 sav
->sav_pending
= dev
;
4860 sav
->sav_npending
= ndev
;
4862 for (i
= 0; i
< ndev
; i
++) {
4863 if ((error
= spa_config_parse(spa
, &vd
, dev
[i
], NULL
, 0,
4867 if (!vd
->vdev_ops
->vdev_op_leaf
) {
4869 error
= SET_ERROR(EINVAL
);
4875 if ((error
= vdev_open(vd
)) == 0 &&
4876 (error
= vdev_label_init(vd
, crtxg
, label
)) == 0) {
4877 VERIFY(nvlist_add_uint64(dev
[i
], ZPOOL_CONFIG_GUID
,
4878 vd
->vdev_guid
) == 0);
4884 (mode
!= VDEV_ALLOC_SPARE
&& mode
!= VDEV_ALLOC_L2CACHE
))
4891 sav
->sav_pending
= NULL
;
4892 sav
->sav_npending
= 0;
4897 spa_validate_aux(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
)
4901 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
4903 if ((error
= spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
4904 &spa
->spa_spares
, ZPOOL_CONFIG_SPARES
, SPA_VERSION_SPARES
,
4905 VDEV_LABEL_SPARE
)) != 0) {
4909 return (spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
4910 &spa
->spa_l2cache
, ZPOOL_CONFIG_L2CACHE
, SPA_VERSION_L2CACHE
,
4911 VDEV_LABEL_L2CACHE
));
4915 spa_set_aux_vdevs(spa_aux_vdev_t
*sav
, nvlist_t
**devs
, int ndevs
,
4920 if (sav
->sav_config
!= NULL
) {
4926 * Generate new dev list by concatenating with the
4929 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
, config
,
4930 &olddevs
, &oldndevs
) == 0);
4932 newdevs
= kmem_alloc(sizeof (void *) *
4933 (ndevs
+ oldndevs
), KM_SLEEP
);
4934 for (i
= 0; i
< oldndevs
; i
++)
4935 VERIFY(nvlist_dup(olddevs
[i
], &newdevs
[i
],
4937 for (i
= 0; i
< ndevs
; i
++)
4938 VERIFY(nvlist_dup(devs
[i
], &newdevs
[i
+ oldndevs
],
4941 VERIFY(nvlist_remove(sav
->sav_config
, config
,
4942 DATA_TYPE_NVLIST_ARRAY
) == 0);
4944 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
,
4945 config
, newdevs
, ndevs
+ oldndevs
) == 0);
4946 for (i
= 0; i
< oldndevs
+ ndevs
; i
++)
4947 nvlist_free(newdevs
[i
]);
4948 kmem_free(newdevs
, (oldndevs
+ ndevs
) * sizeof (void *));
4951 * Generate a new dev list.
4953 VERIFY(nvlist_alloc(&sav
->sav_config
, NV_UNIQUE_NAME
,
4955 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
, config
,
4961 * Stop and drop level 2 ARC devices
4964 spa_l2cache_drop(spa_t
*spa
)
4968 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
4970 for (i
= 0; i
< sav
->sav_count
; i
++) {
4973 vd
= sav
->sav_vdevs
[i
];
4976 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
4977 pool
!= 0ULL && l2arc_vdev_present(vd
))
4978 l2arc_remove_vdev(vd
);
4983 * Verify encryption parameters for spa creation. If we are encrypting, we must
4984 * have the encryption feature flag enabled.
4987 spa_create_check_encryption_params(dsl_crypto_params_t
*dcp
,
4988 boolean_t has_encryption
)
4990 if (dcp
->cp_crypt
!= ZIO_CRYPT_OFF
&&
4991 dcp
->cp_crypt
!= ZIO_CRYPT_INHERIT
&&
4993 return (SET_ERROR(ENOTSUP
));
4995 return (dmu_objset_create_crypt_check(NULL
, dcp
, NULL
));
5002 spa_create(const char *pool
, nvlist_t
*nvroot
, nvlist_t
*props
,
5003 nvlist_t
*zplprops
, dsl_crypto_params_t
*dcp
)
5006 char *altroot
= NULL
;
5011 uint64_t txg
= TXG_INITIAL
;
5012 nvlist_t
**spares
, **l2cache
;
5013 uint_t nspares
, nl2cache
;
5014 uint64_t version
, obj
;
5015 boolean_t has_features
;
5016 boolean_t has_encryption
;
5022 if (props
== NULL
||
5023 nvlist_lookup_string(props
, "tname", &poolname
) != 0)
5024 poolname
= (char *)pool
;
5027 * If this pool already exists, return failure.
5029 mutex_enter(&spa_namespace_lock
);
5030 if (spa_lookup(poolname
) != NULL
) {
5031 mutex_exit(&spa_namespace_lock
);
5032 return (SET_ERROR(EEXIST
));
5036 * Allocate a new spa_t structure.
5038 nvl
= fnvlist_alloc();
5039 fnvlist_add_string(nvl
, ZPOOL_CONFIG_POOL_NAME
, pool
);
5040 (void) nvlist_lookup_string(props
,
5041 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
5042 spa
= spa_add(poolname
, nvl
, altroot
);
5044 spa_activate(spa
, spa_mode_global
);
5046 if (props
&& (error
= spa_prop_validate(spa
, props
))) {
5047 spa_deactivate(spa
);
5049 mutex_exit(&spa_namespace_lock
);
5054 * Temporary pool names should never be written to disk.
5056 if (poolname
!= pool
)
5057 spa
->spa_import_flags
|= ZFS_IMPORT_TEMP_NAME
;
5059 has_features
= B_FALSE
;
5060 has_encryption
= B_FALSE
;
5061 for (nvpair_t
*elem
= nvlist_next_nvpair(props
, NULL
);
5062 elem
!= NULL
; elem
= nvlist_next_nvpair(props
, elem
)) {
5063 if (zpool_prop_feature(nvpair_name(elem
))) {
5064 has_features
= B_TRUE
;
5066 feat_name
= strchr(nvpair_name(elem
), '@') + 1;
5067 VERIFY0(zfeature_lookup_name(feat_name
, &feat
));
5068 if (feat
== SPA_FEATURE_ENCRYPTION
)
5069 has_encryption
= B_TRUE
;
5073 /* verify encryption params, if they were provided */
5075 error
= spa_create_check_encryption_params(dcp
, has_encryption
);
5077 spa_deactivate(spa
);
5079 mutex_exit(&spa_namespace_lock
);
5084 if (has_features
|| nvlist_lookup_uint64(props
,
5085 zpool_prop_to_name(ZPOOL_PROP_VERSION
), &version
) != 0) {
5086 version
= SPA_VERSION
;
5088 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
5090 spa
->spa_first_txg
= txg
;
5091 spa
->spa_uberblock
.ub_txg
= txg
- 1;
5092 spa
->spa_uberblock
.ub_version
= version
;
5093 spa
->spa_ubsync
= spa
->spa_uberblock
;
5094 spa
->spa_load_state
= SPA_LOAD_CREATE
;
5095 spa
->spa_removing_phys
.sr_state
= DSS_NONE
;
5096 spa
->spa_removing_phys
.sr_removing_vdev
= -1;
5097 spa
->spa_removing_phys
.sr_prev_indirect_vdev
= -1;
5100 * Create "The Godfather" zio to hold all async IOs
5102 spa
->spa_async_zio_root
= kmem_alloc(max_ncpus
* sizeof (void *),
5104 for (int i
= 0; i
< max_ncpus
; i
++) {
5105 spa
->spa_async_zio_root
[i
] = zio_root(spa
, NULL
, NULL
,
5106 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
5107 ZIO_FLAG_GODFATHER
);
5111 * Create the root vdev.
5113 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5115 error
= spa_config_parse(spa
, &rvd
, nvroot
, NULL
, 0, VDEV_ALLOC_ADD
);
5117 ASSERT(error
!= 0 || rvd
!= NULL
);
5118 ASSERT(error
!= 0 || spa
->spa_root_vdev
== rvd
);
5120 if (error
== 0 && !zfs_allocatable_devs(nvroot
))
5121 error
= SET_ERROR(EINVAL
);
5124 (error
= vdev_create(rvd
, txg
, B_FALSE
)) == 0 &&
5125 (error
= spa_validate_aux(spa
, nvroot
, txg
,
5126 VDEV_ALLOC_ADD
)) == 0) {
5128 * instantiate the metaslab groups (this will dirty the vdevs)
5129 * we can no longer error exit past this point
5131 for (int c
= 0; error
== 0 && c
< rvd
->vdev_children
; c
++) {
5132 vdev_t
*vd
= rvd
->vdev_child
[c
];
5134 vdev_metaslab_set_size(vd
);
5135 vdev_expand(vd
, txg
);
5139 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5143 spa_deactivate(spa
);
5145 mutex_exit(&spa_namespace_lock
);
5150 * Get the list of spares, if specified.
5152 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
5153 &spares
, &nspares
) == 0) {
5154 VERIFY(nvlist_alloc(&spa
->spa_spares
.sav_config
, NV_UNIQUE_NAME
,
5156 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
5157 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
5158 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5159 spa_load_spares(spa
);
5160 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5161 spa
->spa_spares
.sav_sync
= B_TRUE
;
5165 * Get the list of level 2 cache devices, if specified.
5167 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
5168 &l2cache
, &nl2cache
) == 0) {
5169 VERIFY(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
5170 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
5171 VERIFY(nvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
5172 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
5173 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5174 spa_load_l2cache(spa
);
5175 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5176 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
5179 spa
->spa_is_initializing
= B_TRUE
;
5180 spa
->spa_dsl_pool
= dp
= dsl_pool_create(spa
, zplprops
, dcp
, txg
);
5181 spa
->spa_is_initializing
= B_FALSE
;
5184 * Create DDTs (dedup tables).
5188 spa_update_dspace(spa
);
5190 tx
= dmu_tx_create_assigned(dp
, txg
);
5193 * Create the pool's history object.
5195 if (version
>= SPA_VERSION_ZPOOL_HISTORY
&& !spa
->spa_history
)
5196 spa_history_create_obj(spa
, tx
);
5198 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_CREATE
);
5199 spa_history_log_version(spa
, "create", tx
);
5202 * Create the pool config object.
5204 spa
->spa_config_object
= dmu_object_alloc(spa
->spa_meta_objset
,
5205 DMU_OT_PACKED_NVLIST
, SPA_CONFIG_BLOCKSIZE
,
5206 DMU_OT_PACKED_NVLIST_SIZE
, sizeof (uint64_t), tx
);
5208 if (zap_add(spa
->spa_meta_objset
,
5209 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CONFIG
,
5210 sizeof (uint64_t), 1, &spa
->spa_config_object
, tx
) != 0) {
5211 cmn_err(CE_PANIC
, "failed to add pool config");
5214 if (zap_add(spa
->spa_meta_objset
,
5215 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CREATION_VERSION
,
5216 sizeof (uint64_t), 1, &version
, tx
) != 0) {
5217 cmn_err(CE_PANIC
, "failed to add pool version");
5220 /* Newly created pools with the right version are always deflated. */
5221 if (version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
5222 spa
->spa_deflate
= TRUE
;
5223 if (zap_add(spa
->spa_meta_objset
,
5224 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
5225 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
) != 0) {
5226 cmn_err(CE_PANIC
, "failed to add deflate");
5231 * Create the deferred-free bpobj. Turn off compression
5232 * because sync-to-convergence takes longer if the blocksize
5235 obj
= bpobj_alloc(spa
->spa_meta_objset
, 1 << 14, tx
);
5236 dmu_object_set_compress(spa
->spa_meta_objset
, obj
,
5237 ZIO_COMPRESS_OFF
, tx
);
5238 if (zap_add(spa
->spa_meta_objset
,
5239 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_SYNC_BPOBJ
,
5240 sizeof (uint64_t), 1, &obj
, tx
) != 0) {
5241 cmn_err(CE_PANIC
, "failed to add bpobj");
5243 VERIFY3U(0, ==, bpobj_open(&spa
->spa_deferred_bpobj
,
5244 spa
->spa_meta_objset
, obj
));
5247 * Generate some random noise for salted checksums to operate on.
5249 (void) random_get_pseudo_bytes(spa
->spa_cksum_salt
.zcs_bytes
,
5250 sizeof (spa
->spa_cksum_salt
.zcs_bytes
));
5253 * Set pool properties.
5255 spa
->spa_bootfs
= zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS
);
5256 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
5257 spa
->spa_failmode
= zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE
);
5258 spa
->spa_autoexpand
= zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND
);
5259 spa
->spa_multihost
= zpool_prop_default_numeric(ZPOOL_PROP_MULTIHOST
);
5261 if (props
!= NULL
) {
5262 spa_configfile_set(spa
, props
, B_FALSE
);
5263 spa_sync_props(props
, tx
);
5268 spa
->spa_sync_on
= B_TRUE
;
5270 mmp_thread_start(spa
);
5271 txg_wait_synced(dp
, txg
);
5273 spa_spawn_aux_threads(spa
);
5275 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
);
5278 * Don't count references from objsets that are already closed
5279 * and are making their way through the eviction process.
5281 spa_evicting_os_wait(spa
);
5282 spa
->spa_minref
= zfs_refcount_count(&spa
->spa_refcount
);
5283 spa
->spa_load_state
= SPA_LOAD_NONE
;
5285 mutex_exit(&spa_namespace_lock
);
5291 * Import a non-root pool into the system.
5294 spa_import(char *pool
, nvlist_t
*config
, nvlist_t
*props
, uint64_t flags
)
5297 char *altroot
= NULL
;
5298 spa_load_state_t state
= SPA_LOAD_IMPORT
;
5299 zpool_load_policy_t policy
;
5300 uint64_t mode
= spa_mode_global
;
5301 uint64_t readonly
= B_FALSE
;
5304 nvlist_t
**spares
, **l2cache
;
5305 uint_t nspares
, nl2cache
;
5308 * If a pool with this name exists, return failure.
5310 mutex_enter(&spa_namespace_lock
);
5311 if (spa_lookup(pool
) != NULL
) {
5312 mutex_exit(&spa_namespace_lock
);
5313 return (SET_ERROR(EEXIST
));
5317 * Create and initialize the spa structure.
5319 (void) nvlist_lookup_string(props
,
5320 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
5321 (void) nvlist_lookup_uint64(props
,
5322 zpool_prop_to_name(ZPOOL_PROP_READONLY
), &readonly
);
5325 spa
= spa_add(pool
, config
, altroot
);
5326 spa
->spa_import_flags
= flags
;
5329 * Verbatim import - Take a pool and insert it into the namespace
5330 * as if it had been loaded at boot.
5332 if (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
) {
5334 spa_configfile_set(spa
, props
, B_FALSE
);
5336 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
);
5337 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_IMPORT
);
5338 zfs_dbgmsg("spa_import: verbatim import of %s", pool
);
5339 mutex_exit(&spa_namespace_lock
);
5343 spa_activate(spa
, mode
);
5346 * Don't start async tasks until we know everything is healthy.
5348 spa_async_suspend(spa
);
5350 zpool_get_load_policy(config
, &policy
);
5351 if (policy
.zlp_rewind
& ZPOOL_DO_REWIND
)
5352 state
= SPA_LOAD_RECOVER
;
5354 spa
->spa_config_source
= SPA_CONFIG_SRC_TRYIMPORT
;
5356 if (state
!= SPA_LOAD_RECOVER
) {
5357 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
5358 zfs_dbgmsg("spa_import: importing %s", pool
);
5360 zfs_dbgmsg("spa_import: importing %s, max_txg=%lld "
5361 "(RECOVERY MODE)", pool
, (longlong_t
)policy
.zlp_txg
);
5363 error
= spa_load_best(spa
, state
, policy
.zlp_txg
, policy
.zlp_rewind
);
5366 * Propagate anything learned while loading the pool and pass it
5367 * back to caller (i.e. rewind info, missing devices, etc).
5369 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
5370 spa
->spa_load_info
) == 0);
5372 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5374 * Toss any existing sparelist, as it doesn't have any validity
5375 * anymore, and conflicts with spa_has_spare().
5377 if (spa
->spa_spares
.sav_config
) {
5378 nvlist_free(spa
->spa_spares
.sav_config
);
5379 spa
->spa_spares
.sav_config
= NULL
;
5380 spa_load_spares(spa
);
5382 if (spa
->spa_l2cache
.sav_config
) {
5383 nvlist_free(spa
->spa_l2cache
.sav_config
);
5384 spa
->spa_l2cache
.sav_config
= NULL
;
5385 spa_load_l2cache(spa
);
5388 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
5390 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5393 spa_configfile_set(spa
, props
, B_FALSE
);
5395 if (error
!= 0 || (props
&& spa_writeable(spa
) &&
5396 (error
= spa_prop_set(spa
, props
)))) {
5398 spa_deactivate(spa
);
5400 mutex_exit(&spa_namespace_lock
);
5404 spa_async_resume(spa
);
5407 * Override any spares and level 2 cache devices as specified by
5408 * the user, as these may have correct device names/devids, etc.
5410 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
5411 &spares
, &nspares
) == 0) {
5412 if (spa
->spa_spares
.sav_config
)
5413 VERIFY(nvlist_remove(spa
->spa_spares
.sav_config
,
5414 ZPOOL_CONFIG_SPARES
, DATA_TYPE_NVLIST_ARRAY
) == 0);
5416 VERIFY(nvlist_alloc(&spa
->spa_spares
.sav_config
,
5417 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
5418 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
5419 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
5420 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5421 spa_load_spares(spa
);
5422 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5423 spa
->spa_spares
.sav_sync
= B_TRUE
;
5425 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
5426 &l2cache
, &nl2cache
) == 0) {
5427 if (spa
->spa_l2cache
.sav_config
)
5428 VERIFY(nvlist_remove(spa
->spa_l2cache
.sav_config
,
5429 ZPOOL_CONFIG_L2CACHE
, DATA_TYPE_NVLIST_ARRAY
) == 0);
5431 VERIFY(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
5432 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
5433 VERIFY(nvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
5434 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
5435 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5436 spa_load_l2cache(spa
);
5437 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5438 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
5442 * Check for any removed devices.
5444 if (spa
->spa_autoreplace
) {
5445 spa_aux_check_removed(&spa
->spa_spares
);
5446 spa_aux_check_removed(&spa
->spa_l2cache
);
5449 if (spa_writeable(spa
)) {
5451 * Update the config cache to include the newly-imported pool.
5453 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
5457 * It's possible that the pool was expanded while it was exported.
5458 * We kick off an async task to handle this for us.
5460 spa_async_request(spa
, SPA_ASYNC_AUTOEXPAND
);
5462 spa_history_log_version(spa
, "import", NULL
);
5464 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_IMPORT
);
5466 zvol_create_minors(spa
, pool
, B_TRUE
);
5468 mutex_exit(&spa_namespace_lock
);
5474 spa_tryimport(nvlist_t
*tryconfig
)
5476 nvlist_t
*config
= NULL
;
5477 char *poolname
, *cachefile
;
5481 zpool_load_policy_t policy
;
5483 if (nvlist_lookup_string(tryconfig
, ZPOOL_CONFIG_POOL_NAME
, &poolname
))
5486 if (nvlist_lookup_uint64(tryconfig
, ZPOOL_CONFIG_POOL_STATE
, &state
))
5490 * Create and initialize the spa structure.
5492 mutex_enter(&spa_namespace_lock
);
5493 spa
= spa_add(TRYIMPORT_NAME
, tryconfig
, NULL
);
5494 spa_activate(spa
, FREAD
);
5497 * Rewind pool if a max txg was provided.
5499 zpool_get_load_policy(spa
->spa_config
, &policy
);
5500 if (policy
.zlp_txg
!= UINT64_MAX
) {
5501 spa
->spa_load_max_txg
= policy
.zlp_txg
;
5502 spa
->spa_extreme_rewind
= B_TRUE
;
5503 zfs_dbgmsg("spa_tryimport: importing %s, max_txg=%lld",
5504 poolname
, (longlong_t
)policy
.zlp_txg
);
5506 zfs_dbgmsg("spa_tryimport: importing %s", poolname
);
5509 if (nvlist_lookup_string(tryconfig
, ZPOOL_CONFIG_CACHEFILE
, &cachefile
)
5511 zfs_dbgmsg("spa_tryimport: using cachefile '%s'", cachefile
);
5512 spa
->spa_config_source
= SPA_CONFIG_SRC_CACHEFILE
;
5514 spa
->spa_config_source
= SPA_CONFIG_SRC_SCAN
;
5517 error
= spa_load(spa
, SPA_LOAD_TRYIMPORT
, SPA_IMPORT_EXISTING
);
5520 * If 'tryconfig' was at least parsable, return the current config.
5522 if (spa
->spa_root_vdev
!= NULL
) {
5523 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
5524 VERIFY(nvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
,
5526 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
5528 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_TIMESTAMP
,
5529 spa
->spa_uberblock
.ub_timestamp
) == 0);
5530 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
5531 spa
->spa_load_info
) == 0);
5532 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_ERRATA
,
5533 spa
->spa_errata
) == 0);
5536 * If the bootfs property exists on this pool then we
5537 * copy it out so that external consumers can tell which
5538 * pools are bootable.
5540 if ((!error
|| error
== EEXIST
) && spa
->spa_bootfs
) {
5541 char *tmpname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
5544 * We have to play games with the name since the
5545 * pool was opened as TRYIMPORT_NAME.
5547 if (dsl_dsobj_to_dsname(spa_name(spa
),
5548 spa
->spa_bootfs
, tmpname
) == 0) {
5552 dsname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
5554 cp
= strchr(tmpname
, '/');
5556 (void) strlcpy(dsname
, tmpname
,
5559 (void) snprintf(dsname
, MAXPATHLEN
,
5560 "%s/%s", poolname
, ++cp
);
5562 VERIFY(nvlist_add_string(config
,
5563 ZPOOL_CONFIG_BOOTFS
, dsname
) == 0);
5564 kmem_free(dsname
, MAXPATHLEN
);
5566 kmem_free(tmpname
, MAXPATHLEN
);
5570 * Add the list of hot spares and level 2 cache devices.
5572 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
5573 spa_add_spares(spa
, config
);
5574 spa_add_l2cache(spa
, config
);
5575 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
5579 spa_deactivate(spa
);
5581 mutex_exit(&spa_namespace_lock
);
5587 * Pool export/destroy
5589 * The act of destroying or exporting a pool is very simple. We make sure there
5590 * is no more pending I/O and any references to the pool are gone. Then, we
5591 * update the pool state and sync all the labels to disk, removing the
5592 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
5593 * we don't sync the labels or remove the configuration cache.
5596 spa_export_common(char *pool
, int new_state
, nvlist_t
**oldconfig
,
5597 boolean_t force
, boolean_t hardforce
)
5604 if (!(spa_mode_global
& FWRITE
))
5605 return (SET_ERROR(EROFS
));
5607 mutex_enter(&spa_namespace_lock
);
5608 if ((spa
= spa_lookup(pool
)) == NULL
) {
5609 mutex_exit(&spa_namespace_lock
);
5610 return (SET_ERROR(ENOENT
));
5614 * Put a hold on the pool, drop the namespace lock, stop async tasks,
5615 * reacquire the namespace lock, and see if we can export.
5617 spa_open_ref(spa
, FTAG
);
5618 mutex_exit(&spa_namespace_lock
);
5619 spa_async_suspend(spa
);
5620 if (spa
->spa_zvol_taskq
) {
5621 zvol_remove_minors(spa
, spa_name(spa
), B_TRUE
);
5622 taskq_wait(spa
->spa_zvol_taskq
);
5624 mutex_enter(&spa_namespace_lock
);
5625 spa_close(spa
, FTAG
);
5627 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
)
5630 * The pool will be in core if it's openable, in which case we can
5631 * modify its state. Objsets may be open only because they're dirty,
5632 * so we have to force it to sync before checking spa_refcnt.
5634 if (spa
->spa_sync_on
) {
5635 txg_wait_synced(spa
->spa_dsl_pool
, 0);
5636 spa_evicting_os_wait(spa
);
5640 * A pool cannot be exported or destroyed if there are active
5641 * references. If we are resetting a pool, allow references by
5642 * fault injection handlers.
5644 if (!spa_refcount_zero(spa
) ||
5645 (spa
->spa_inject_ref
!= 0 &&
5646 new_state
!= POOL_STATE_UNINITIALIZED
)) {
5647 spa_async_resume(spa
);
5648 mutex_exit(&spa_namespace_lock
);
5649 return (SET_ERROR(EBUSY
));
5652 if (spa
->spa_sync_on
) {
5654 * A pool cannot be exported if it has an active shared spare.
5655 * This is to prevent other pools stealing the active spare
5656 * from an exported pool. At user's own will, such pool can
5657 * be forcedly exported.
5659 if (!force
&& new_state
== POOL_STATE_EXPORTED
&&
5660 spa_has_active_shared_spare(spa
)) {
5661 spa_async_resume(spa
);
5662 mutex_exit(&spa_namespace_lock
);
5663 return (SET_ERROR(EXDEV
));
5667 * We're about to export or destroy this pool. Make sure
5668 * we stop all initializtion activity here before we
5669 * set the spa_final_txg. This will ensure that all
5670 * dirty data resulting from the initialization is
5671 * committed to disk before we unload the pool.
5673 if (spa
->spa_root_vdev
!= NULL
) {
5674 vdev_initialize_stop_all(spa
->spa_root_vdev
,
5675 VDEV_INITIALIZE_ACTIVE
);
5679 * We want this to be reflected on every label,
5680 * so mark them all dirty. spa_unload() will do the
5681 * final sync that pushes these changes out.
5683 if (new_state
!= POOL_STATE_UNINITIALIZED
&& !hardforce
) {
5684 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5685 spa
->spa_state
= new_state
;
5686 spa
->spa_final_txg
= spa_last_synced_txg(spa
) +
5688 vdev_config_dirty(spa
->spa_root_vdev
);
5689 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5694 if (new_state
== POOL_STATE_DESTROYED
)
5695 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_DESTROY
);
5696 else if (new_state
== POOL_STATE_EXPORTED
)
5697 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_EXPORT
);
5699 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
5701 spa_deactivate(spa
);
5704 if (oldconfig
&& spa
->spa_config
)
5705 VERIFY(nvlist_dup(spa
->spa_config
, oldconfig
, 0) == 0);
5707 if (new_state
!= POOL_STATE_UNINITIALIZED
) {
5709 spa_write_cachefile(spa
, B_TRUE
, B_TRUE
);
5712 mutex_exit(&spa_namespace_lock
);
5718 * Destroy a storage pool.
5721 spa_destroy(char *pool
)
5723 return (spa_export_common(pool
, POOL_STATE_DESTROYED
, NULL
,
5728 * Export a storage pool.
5731 spa_export(char *pool
, nvlist_t
**oldconfig
, boolean_t force
,
5732 boolean_t hardforce
)
5734 return (spa_export_common(pool
, POOL_STATE_EXPORTED
, oldconfig
,
5739 * Similar to spa_export(), this unloads the spa_t without actually removing it
5740 * from the namespace in any way.
5743 spa_reset(char *pool
)
5745 return (spa_export_common(pool
, POOL_STATE_UNINITIALIZED
, NULL
,
5750 * ==========================================================================
5751 * Device manipulation
5752 * ==========================================================================
5756 * Add a device to a storage pool.
5759 spa_vdev_add(spa_t
*spa
, nvlist_t
*nvroot
)
5763 vdev_t
*rvd
= spa
->spa_root_vdev
;
5765 nvlist_t
**spares
, **l2cache
;
5766 uint_t nspares
, nl2cache
;
5768 ASSERT(spa_writeable(spa
));
5770 txg
= spa_vdev_enter(spa
);
5772 if ((error
= spa_config_parse(spa
, &vd
, nvroot
, NULL
, 0,
5773 VDEV_ALLOC_ADD
)) != 0)
5774 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5776 spa
->spa_pending_vdev
= vd
; /* spa_vdev_exit() will clear this */
5778 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
, &spares
,
5782 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
, &l2cache
,
5786 if (vd
->vdev_children
== 0 && nspares
== 0 && nl2cache
== 0)
5787 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
5789 if (vd
->vdev_children
!= 0 &&
5790 (error
= vdev_create(vd
, txg
, B_FALSE
)) != 0)
5791 return (spa_vdev_exit(spa
, vd
, txg
, error
));
5794 * We must validate the spares and l2cache devices after checking the
5795 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
5797 if ((error
= spa_validate_aux(spa
, nvroot
, txg
, VDEV_ALLOC_ADD
)) != 0)
5798 return (spa_vdev_exit(spa
, vd
, txg
, error
));
5801 * If we are in the middle of a device removal, we can only add
5802 * devices which match the existing devices in the pool.
5803 * If we are in the middle of a removal, or have some indirect
5804 * vdevs, we can not add raidz toplevels.
5806 if (spa
->spa_vdev_removal
!= NULL
||
5807 spa
->spa_removing_phys
.sr_prev_indirect_vdev
!= -1) {
5808 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
5809 tvd
= vd
->vdev_child
[c
];
5810 if (spa
->spa_vdev_removal
!= NULL
&&
5811 tvd
->vdev_ashift
!= spa
->spa_max_ashift
) {
5812 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
5814 /* Fail if top level vdev is raidz */
5815 if (tvd
->vdev_ops
== &vdev_raidz_ops
) {
5816 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
5819 * Need the top level mirror to be
5820 * a mirror of leaf vdevs only
5822 if (tvd
->vdev_ops
== &vdev_mirror_ops
) {
5823 for (uint64_t cid
= 0;
5824 cid
< tvd
->vdev_children
; cid
++) {
5825 vdev_t
*cvd
= tvd
->vdev_child
[cid
];
5826 if (!cvd
->vdev_ops
->vdev_op_leaf
) {
5827 return (spa_vdev_exit(spa
, vd
,
5835 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
5838 * Set the vdev id to the first hole, if one exists.
5840 for (id
= 0; id
< rvd
->vdev_children
; id
++) {
5841 if (rvd
->vdev_child
[id
]->vdev_ishole
) {
5842 vdev_free(rvd
->vdev_child
[id
]);
5846 tvd
= vd
->vdev_child
[c
];
5847 vdev_remove_child(vd
, tvd
);
5849 vdev_add_child(rvd
, tvd
);
5850 vdev_config_dirty(tvd
);
5854 spa_set_aux_vdevs(&spa
->spa_spares
, spares
, nspares
,
5855 ZPOOL_CONFIG_SPARES
);
5856 spa_load_spares(spa
);
5857 spa
->spa_spares
.sav_sync
= B_TRUE
;
5860 if (nl2cache
!= 0) {
5861 spa_set_aux_vdevs(&spa
->spa_l2cache
, l2cache
, nl2cache
,
5862 ZPOOL_CONFIG_L2CACHE
);
5863 spa_load_l2cache(spa
);
5864 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
5868 * We have to be careful when adding new vdevs to an existing pool.
5869 * If other threads start allocating from these vdevs before we
5870 * sync the config cache, and we lose power, then upon reboot we may
5871 * fail to open the pool because there are DVAs that the config cache
5872 * can't translate. Therefore, we first add the vdevs without
5873 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
5874 * and then let spa_config_update() initialize the new metaslabs.
5876 * spa_load() checks for added-but-not-initialized vdevs, so that
5877 * if we lose power at any point in this sequence, the remaining
5878 * steps will be completed the next time we load the pool.
5880 (void) spa_vdev_exit(spa
, vd
, txg
, 0);
5882 mutex_enter(&spa_namespace_lock
);
5883 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
5884 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_VDEV_ADD
);
5885 mutex_exit(&spa_namespace_lock
);
5891 * Attach a device to a mirror. The arguments are the path to any device
5892 * in the mirror, and the nvroot for the new device. If the path specifies
5893 * a device that is not mirrored, we automatically insert the mirror vdev.
5895 * If 'replacing' is specified, the new device is intended to replace the
5896 * existing device; in this case the two devices are made into their own
5897 * mirror using the 'replacing' vdev, which is functionally identical to
5898 * the mirror vdev (it actually reuses all the same ops) but has a few
5899 * extra rules: you can't attach to it after it's been created, and upon
5900 * completion of resilvering, the first disk (the one being replaced)
5901 * is automatically detached.
5904 spa_vdev_attach(spa_t
*spa
, uint64_t guid
, nvlist_t
*nvroot
, int replacing
)
5906 uint64_t txg
, dtl_max_txg
;
5907 ASSERTV(vdev_t
*rvd
= spa
->spa_root_vdev
);
5908 vdev_t
*oldvd
, *newvd
, *newrootvd
, *pvd
, *tvd
;
5910 char *oldvdpath
, *newvdpath
;
5914 ASSERT(spa_writeable(spa
));
5916 txg
= spa_vdev_enter(spa
);
5918 oldvd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
5920 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
5921 if (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
)) {
5922 error
= (spa_has_checkpoint(spa
)) ?
5923 ZFS_ERR_CHECKPOINT_EXISTS
: ZFS_ERR_DISCARDING_CHECKPOINT
;
5924 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5927 if (spa
->spa_vdev_removal
!= NULL
)
5928 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
5931 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
5933 if (!oldvd
->vdev_ops
->vdev_op_leaf
)
5934 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
5936 pvd
= oldvd
->vdev_parent
;
5938 if ((error
= spa_config_parse(spa
, &newrootvd
, nvroot
, NULL
, 0,
5939 VDEV_ALLOC_ATTACH
)) != 0)
5940 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
5942 if (newrootvd
->vdev_children
!= 1)
5943 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
5945 newvd
= newrootvd
->vdev_child
[0];
5947 if (!newvd
->vdev_ops
->vdev_op_leaf
)
5948 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
5950 if ((error
= vdev_create(newrootvd
, txg
, replacing
)) != 0)
5951 return (spa_vdev_exit(spa
, newrootvd
, txg
, error
));
5954 * Spares can't replace logs
5956 if (oldvd
->vdev_top
->vdev_islog
&& newvd
->vdev_isspare
)
5957 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
5961 * For attach, the only allowable parent is a mirror or the root
5964 if (pvd
->vdev_ops
!= &vdev_mirror_ops
&&
5965 pvd
->vdev_ops
!= &vdev_root_ops
)
5966 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
5968 pvops
= &vdev_mirror_ops
;
5971 * Active hot spares can only be replaced by inactive hot
5974 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
5975 oldvd
->vdev_isspare
&&
5976 !spa_has_spare(spa
, newvd
->vdev_guid
))
5977 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
5980 * If the source is a hot spare, and the parent isn't already a
5981 * spare, then we want to create a new hot spare. Otherwise, we
5982 * want to create a replacing vdev. The user is not allowed to
5983 * attach to a spared vdev child unless the 'isspare' state is
5984 * the same (spare replaces spare, non-spare replaces
5987 if (pvd
->vdev_ops
== &vdev_replacing_ops
&&
5988 spa_version(spa
) < SPA_VERSION_MULTI_REPLACE
) {
5989 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
5990 } else if (pvd
->vdev_ops
== &vdev_spare_ops
&&
5991 newvd
->vdev_isspare
!= oldvd
->vdev_isspare
) {
5992 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
5995 if (newvd
->vdev_isspare
)
5996 pvops
= &vdev_spare_ops
;
5998 pvops
= &vdev_replacing_ops
;
6002 * Make sure the new device is big enough.
6004 if (newvd
->vdev_asize
< vdev_get_min_asize(oldvd
))
6005 return (spa_vdev_exit(spa
, newrootvd
, txg
, EOVERFLOW
));
6008 * The new device cannot have a higher alignment requirement
6009 * than the top-level vdev.
6011 if (newvd
->vdev_ashift
> oldvd
->vdev_top
->vdev_ashift
)
6012 return (spa_vdev_exit(spa
, newrootvd
, txg
, EDOM
));
6015 * If this is an in-place replacement, update oldvd's path and devid
6016 * to make it distinguishable from newvd, and unopenable from now on.
6018 if (strcmp(oldvd
->vdev_path
, newvd
->vdev_path
) == 0) {
6019 spa_strfree(oldvd
->vdev_path
);
6020 oldvd
->vdev_path
= kmem_alloc(strlen(newvd
->vdev_path
) + 5,
6022 (void) sprintf(oldvd
->vdev_path
, "%s/%s",
6023 newvd
->vdev_path
, "old");
6024 if (oldvd
->vdev_devid
!= NULL
) {
6025 spa_strfree(oldvd
->vdev_devid
);
6026 oldvd
->vdev_devid
= NULL
;
6030 /* mark the device being resilvered */
6031 newvd
->vdev_resilver_txg
= txg
;
6034 * If the parent is not a mirror, or if we're replacing, insert the new
6035 * mirror/replacing/spare vdev above oldvd.
6037 if (pvd
->vdev_ops
!= pvops
)
6038 pvd
= vdev_add_parent(oldvd
, pvops
);
6040 ASSERT(pvd
->vdev_top
->vdev_parent
== rvd
);
6041 ASSERT(pvd
->vdev_ops
== pvops
);
6042 ASSERT(oldvd
->vdev_parent
== pvd
);
6045 * Extract the new device from its root and add it to pvd.
6047 vdev_remove_child(newrootvd
, newvd
);
6048 newvd
->vdev_id
= pvd
->vdev_children
;
6049 newvd
->vdev_crtxg
= oldvd
->vdev_crtxg
;
6050 vdev_add_child(pvd
, newvd
);
6053 * Reevaluate the parent vdev state.
6055 vdev_propagate_state(pvd
);
6057 tvd
= newvd
->vdev_top
;
6058 ASSERT(pvd
->vdev_top
== tvd
);
6059 ASSERT(tvd
->vdev_parent
== rvd
);
6061 vdev_config_dirty(tvd
);
6064 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
6065 * for any dmu_sync-ed blocks. It will propagate upward when
6066 * spa_vdev_exit() calls vdev_dtl_reassess().
6068 dtl_max_txg
= txg
+ TXG_CONCURRENT_STATES
;
6070 vdev_dtl_dirty(newvd
, DTL_MISSING
, TXG_INITIAL
,
6071 dtl_max_txg
- TXG_INITIAL
);
6073 if (newvd
->vdev_isspare
) {
6074 spa_spare_activate(newvd
);
6075 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_VDEV_SPARE
);
6078 oldvdpath
= spa_strdup(oldvd
->vdev_path
);
6079 newvdpath
= spa_strdup(newvd
->vdev_path
);
6080 newvd_isspare
= newvd
->vdev_isspare
;
6083 * Mark newvd's DTL dirty in this txg.
6085 vdev_dirty(tvd
, VDD_DTL
, newvd
, txg
);
6088 * Schedule the resilver to restart in the future. We do this to
6089 * ensure that dmu_sync-ed blocks have been stitched into the
6090 * respective datasets. We do not do this if resilvers have been
6093 if (dsl_scan_resilvering(spa_get_dsl(spa
)) &&
6094 spa_feature_is_enabled(spa
, SPA_FEATURE_RESILVER_DEFER
))
6095 vdev_set_deferred_resilver(spa
, newvd
);
6097 dsl_resilver_restart(spa
->spa_dsl_pool
, dtl_max_txg
);
6099 if (spa
->spa_bootfs
)
6100 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_BOOTFS_VDEV_ATTACH
);
6102 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_VDEV_ATTACH
);
6107 (void) spa_vdev_exit(spa
, newrootvd
, dtl_max_txg
, 0);
6109 spa_history_log_internal(spa
, "vdev attach", NULL
,
6110 "%s vdev=%s %s vdev=%s",
6111 replacing
&& newvd_isspare
? "spare in" :
6112 replacing
? "replace" : "attach", newvdpath
,
6113 replacing
? "for" : "to", oldvdpath
);
6115 spa_strfree(oldvdpath
);
6116 spa_strfree(newvdpath
);
6122 * Detach a device from a mirror or replacing vdev.
6124 * If 'replace_done' is specified, only detach if the parent
6125 * is a replacing vdev.
6128 spa_vdev_detach(spa_t
*spa
, uint64_t guid
, uint64_t pguid
, int replace_done
)
6132 ASSERTV(vdev_t
*rvd
= spa
->spa_root_vdev
);
6133 vdev_t
*vd
, *pvd
, *cvd
, *tvd
;
6134 boolean_t unspare
= B_FALSE
;
6135 uint64_t unspare_guid
= 0;
6138 ASSERT(spa_writeable(spa
));
6140 txg
= spa_vdev_enter(spa
);
6142 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
6145 * Besides being called directly from the userland through the
6146 * ioctl interface, spa_vdev_detach() can be potentially called
6147 * at the end of spa_vdev_resilver_done().
6149 * In the regular case, when we have a checkpoint this shouldn't
6150 * happen as we never empty the DTLs of a vdev during the scrub
6151 * [see comment in dsl_scan_done()]. Thus spa_vdev_resilvering_done()
6152 * should never get here when we have a checkpoint.
6154 * That said, even in a case when we checkpoint the pool exactly
6155 * as spa_vdev_resilver_done() calls this function everything
6156 * should be fine as the resilver will return right away.
6158 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
6159 if (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
)) {
6160 error
= (spa_has_checkpoint(spa
)) ?
6161 ZFS_ERR_CHECKPOINT_EXISTS
: ZFS_ERR_DISCARDING_CHECKPOINT
;
6162 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
6166 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
6168 if (!vd
->vdev_ops
->vdev_op_leaf
)
6169 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
6171 pvd
= vd
->vdev_parent
;
6174 * If the parent/child relationship is not as expected, don't do it.
6175 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
6176 * vdev that's replacing B with C. The user's intent in replacing
6177 * is to go from M(A,B) to M(A,C). If the user decides to cancel
6178 * the replace by detaching C, the expected behavior is to end up
6179 * M(A,B). But suppose that right after deciding to detach C,
6180 * the replacement of B completes. We would have M(A,C), and then
6181 * ask to detach C, which would leave us with just A -- not what
6182 * the user wanted. To prevent this, we make sure that the
6183 * parent/child relationship hasn't changed -- in this example,
6184 * that C's parent is still the replacing vdev R.
6186 if (pvd
->vdev_guid
!= pguid
&& pguid
!= 0)
6187 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
6190 * Only 'replacing' or 'spare' vdevs can be replaced.
6192 if (replace_done
&& pvd
->vdev_ops
!= &vdev_replacing_ops
&&
6193 pvd
->vdev_ops
!= &vdev_spare_ops
)
6194 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
6196 ASSERT(pvd
->vdev_ops
!= &vdev_spare_ops
||
6197 spa_version(spa
) >= SPA_VERSION_SPARES
);
6200 * Only mirror, replacing, and spare vdevs support detach.
6202 if (pvd
->vdev_ops
!= &vdev_replacing_ops
&&
6203 pvd
->vdev_ops
!= &vdev_mirror_ops
&&
6204 pvd
->vdev_ops
!= &vdev_spare_ops
)
6205 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
6208 * If this device has the only valid copy of some data,
6209 * we cannot safely detach it.
6211 if (vdev_dtl_required(vd
))
6212 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
6214 ASSERT(pvd
->vdev_children
>= 2);
6217 * If we are detaching the second disk from a replacing vdev, then
6218 * check to see if we changed the original vdev's path to have "/old"
6219 * at the end in spa_vdev_attach(). If so, undo that change now.
6221 if (pvd
->vdev_ops
== &vdev_replacing_ops
&& vd
->vdev_id
> 0 &&
6222 vd
->vdev_path
!= NULL
) {
6223 size_t len
= strlen(vd
->vdev_path
);
6225 for (int c
= 0; c
< pvd
->vdev_children
; c
++) {
6226 cvd
= pvd
->vdev_child
[c
];
6228 if (cvd
== vd
|| cvd
->vdev_path
== NULL
)
6231 if (strncmp(cvd
->vdev_path
, vd
->vdev_path
, len
) == 0 &&
6232 strcmp(cvd
->vdev_path
+ len
, "/old") == 0) {
6233 spa_strfree(cvd
->vdev_path
);
6234 cvd
->vdev_path
= spa_strdup(vd
->vdev_path
);
6241 * If we are detaching the original disk from a spare, then it implies
6242 * that the spare should become a real disk, and be removed from the
6243 * active spare list for the pool.
6245 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
6247 pvd
->vdev_child
[pvd
->vdev_children
- 1]->vdev_isspare
)
6251 * Erase the disk labels so the disk can be used for other things.
6252 * This must be done after all other error cases are handled,
6253 * but before we disembowel vd (so we can still do I/O to it).
6254 * But if we can't do it, don't treat the error as fatal --
6255 * it may be that the unwritability of the disk is the reason
6256 * it's being detached!
6258 error
= vdev_label_init(vd
, 0, VDEV_LABEL_REMOVE
);
6261 * Remove vd from its parent and compact the parent's children.
6263 vdev_remove_child(pvd
, vd
);
6264 vdev_compact_children(pvd
);
6267 * Remember one of the remaining children so we can get tvd below.
6269 cvd
= pvd
->vdev_child
[pvd
->vdev_children
- 1];
6272 * If we need to remove the remaining child from the list of hot spares,
6273 * do it now, marking the vdev as no longer a spare in the process.
6274 * We must do this before vdev_remove_parent(), because that can
6275 * change the GUID if it creates a new toplevel GUID. For a similar
6276 * reason, we must remove the spare now, in the same txg as the detach;
6277 * otherwise someone could attach a new sibling, change the GUID, and
6278 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
6281 ASSERT(cvd
->vdev_isspare
);
6282 spa_spare_remove(cvd
);
6283 unspare_guid
= cvd
->vdev_guid
;
6284 (void) spa_vdev_remove(spa
, unspare_guid
, B_TRUE
);
6285 cvd
->vdev_unspare
= B_TRUE
;
6289 * If the parent mirror/replacing vdev only has one child,
6290 * the parent is no longer needed. Remove it from the tree.
6292 if (pvd
->vdev_children
== 1) {
6293 if (pvd
->vdev_ops
== &vdev_spare_ops
)
6294 cvd
->vdev_unspare
= B_FALSE
;
6295 vdev_remove_parent(cvd
);
6300 * We don't set tvd until now because the parent we just removed
6301 * may have been the previous top-level vdev.
6303 tvd
= cvd
->vdev_top
;
6304 ASSERT(tvd
->vdev_parent
== rvd
);
6307 * Reevaluate the parent vdev state.
6309 vdev_propagate_state(cvd
);
6312 * If the 'autoexpand' property is set on the pool then automatically
6313 * try to expand the size of the pool. For example if the device we
6314 * just detached was smaller than the others, it may be possible to
6315 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
6316 * first so that we can obtain the updated sizes of the leaf vdevs.
6318 if (spa
->spa_autoexpand
) {
6320 vdev_expand(tvd
, txg
);
6323 vdev_config_dirty(tvd
);
6326 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
6327 * vd->vdev_detached is set and free vd's DTL object in syncing context.
6328 * But first make sure we're not on any *other* txg's DTL list, to
6329 * prevent vd from being accessed after it's freed.
6331 vdpath
= spa_strdup(vd
->vdev_path
? vd
->vdev_path
: "none");
6332 for (int t
= 0; t
< TXG_SIZE
; t
++)
6333 (void) txg_list_remove_this(&tvd
->vdev_dtl_list
, vd
, t
);
6334 vd
->vdev_detached
= B_TRUE
;
6335 vdev_dirty(tvd
, VDD_DTL
, vd
, txg
);
6337 spa_event_notify(spa
, vd
, NULL
, ESC_ZFS_VDEV_REMOVE
);
6339 /* hang on to the spa before we release the lock */
6340 spa_open_ref(spa
, FTAG
);
6342 error
= spa_vdev_exit(spa
, vd
, txg
, 0);
6344 spa_history_log_internal(spa
, "detach", NULL
,
6346 spa_strfree(vdpath
);
6349 * If this was the removal of the original device in a hot spare vdev,
6350 * then we want to go through and remove the device from the hot spare
6351 * list of every other pool.
6354 spa_t
*altspa
= NULL
;
6356 mutex_enter(&spa_namespace_lock
);
6357 while ((altspa
= spa_next(altspa
)) != NULL
) {
6358 if (altspa
->spa_state
!= POOL_STATE_ACTIVE
||
6362 spa_open_ref(altspa
, FTAG
);
6363 mutex_exit(&spa_namespace_lock
);
6364 (void) spa_vdev_remove(altspa
, unspare_guid
, B_TRUE
);
6365 mutex_enter(&spa_namespace_lock
);
6366 spa_close(altspa
, FTAG
);
6368 mutex_exit(&spa_namespace_lock
);
6370 /* search the rest of the vdevs for spares to remove */
6371 spa_vdev_resilver_done(spa
);
6374 /* all done with the spa; OK to release */
6375 mutex_enter(&spa_namespace_lock
);
6376 spa_close(spa
, FTAG
);
6377 mutex_exit(&spa_namespace_lock
);
6383 spa_vdev_initialize_impl(spa_t
*spa
, uint64_t guid
, uint64_t cmd_type
,
6386 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
6388 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
6390 /* Look up vdev and ensure it's a leaf. */
6391 vdev_t
*vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
6392 if (vd
== NULL
|| vd
->vdev_detached
) {
6393 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
6394 return (SET_ERROR(ENODEV
));
6395 } else if (!vd
->vdev_ops
->vdev_op_leaf
|| !vdev_is_concrete(vd
)) {
6396 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
6397 return (SET_ERROR(EINVAL
));
6398 } else if (!vdev_writeable(vd
)) {
6399 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
6400 return (SET_ERROR(EROFS
));
6402 mutex_enter(&vd
->vdev_initialize_lock
);
6403 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
6406 * When we activate an initialize action we check to see
6407 * if the vdev_initialize_thread is NULL. We do this instead
6408 * of using the vdev_initialize_state since there might be
6409 * a previous initialization process which has completed but
6410 * the thread is not exited.
6412 if (cmd_type
== POOL_INITIALIZE_DO
&&
6413 (vd
->vdev_initialize_thread
!= NULL
||
6414 vd
->vdev_top
->vdev_removing
)) {
6415 mutex_exit(&vd
->vdev_initialize_lock
);
6416 return (SET_ERROR(EBUSY
));
6417 } else if (cmd_type
== POOL_INITIALIZE_CANCEL
&&
6418 (vd
->vdev_initialize_state
!= VDEV_INITIALIZE_ACTIVE
&&
6419 vd
->vdev_initialize_state
!= VDEV_INITIALIZE_SUSPENDED
)) {
6420 mutex_exit(&vd
->vdev_initialize_lock
);
6421 return (SET_ERROR(ESRCH
));
6422 } else if (cmd_type
== POOL_INITIALIZE_SUSPEND
&&
6423 vd
->vdev_initialize_state
!= VDEV_INITIALIZE_ACTIVE
) {
6424 mutex_exit(&vd
->vdev_initialize_lock
);
6425 return (SET_ERROR(ESRCH
));
6429 case POOL_INITIALIZE_DO
:
6430 vdev_initialize(vd
);
6432 case POOL_INITIALIZE_CANCEL
:
6433 vdev_initialize_stop(vd
, VDEV_INITIALIZE_CANCELED
, vd_list
);
6435 case POOL_INITIALIZE_SUSPEND
:
6436 vdev_initialize_stop(vd
, VDEV_INITIALIZE_SUSPENDED
, vd_list
);
6439 panic("invalid cmd_type %llu", (unsigned long long)cmd_type
);
6441 mutex_exit(&vd
->vdev_initialize_lock
);
6447 spa_vdev_initialize(spa_t
*spa
, nvlist_t
*nv
, uint64_t cmd_type
,
6448 nvlist_t
*vdev_errlist
)
6450 int total_errors
= 0;
6453 list_create(&vd_list
, sizeof (vdev_t
),
6454 offsetof(vdev_t
, vdev_initialize_node
));
6457 * We hold the namespace lock through the whole function
6458 * to prevent any changes to the pool while we're starting or
6459 * stopping initialization. The config and state locks are held so that
6460 * we can properly assess the vdev state before we commit to
6461 * the initializing operation.
6463 mutex_enter(&spa_namespace_lock
);
6465 for (nvpair_t
*pair
= nvlist_next_nvpair(nv
, NULL
);
6466 pair
!= NULL
; pair
= nvlist_next_nvpair(nv
, pair
)) {
6467 uint64_t vdev_guid
= fnvpair_value_uint64(pair
);
6469 int error
= spa_vdev_initialize_impl(spa
, vdev_guid
, cmd_type
,
6472 char guid_as_str
[MAXNAMELEN
];
6474 (void) snprintf(guid_as_str
, sizeof (guid_as_str
),
6475 "%llu", (unsigned long long)vdev_guid
);
6476 fnvlist_add_int64(vdev_errlist
, guid_as_str
, error
);
6481 /* Wait for all initialize threads to stop. */
6482 vdev_initialize_stop_wait(spa
, &vd_list
);
6484 /* Sync out the initializing state */
6485 txg_wait_synced(spa
->spa_dsl_pool
, 0);
6486 mutex_exit(&spa_namespace_lock
);
6488 list_destroy(&vd_list
);
6490 return (total_errors
);
6494 * Split a set of devices from their mirrors, and create a new pool from them.
6497 spa_vdev_split_mirror(spa_t
*spa
, char *newname
, nvlist_t
*config
,
6498 nvlist_t
*props
, boolean_t exp
)
6501 uint64_t txg
, *glist
;
6503 uint_t c
, children
, lastlog
;
6504 nvlist_t
**child
, *nvl
, *tmp
;
6506 char *altroot
= NULL
;
6507 vdev_t
*rvd
, **vml
= NULL
; /* vdev modify list */
6508 boolean_t activate_slog
;
6510 ASSERT(spa_writeable(spa
));
6512 txg
= spa_vdev_enter(spa
);
6514 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
6515 if (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
)) {
6516 error
= (spa_has_checkpoint(spa
)) ?
6517 ZFS_ERR_CHECKPOINT_EXISTS
: ZFS_ERR_DISCARDING_CHECKPOINT
;
6518 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
6521 /* clear the log and flush everything up to now */
6522 activate_slog
= spa_passivate_log(spa
);
6523 (void) spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
6524 error
= spa_reset_logs(spa
);
6525 txg
= spa_vdev_config_enter(spa
);
6528 spa_activate_log(spa
);
6531 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
6533 /* check new spa name before going any further */
6534 if (spa_lookup(newname
) != NULL
)
6535 return (spa_vdev_exit(spa
, NULL
, txg
, EEXIST
));
6538 * scan through all the children to ensure they're all mirrors
6540 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvl
) != 0 ||
6541 nvlist_lookup_nvlist_array(nvl
, ZPOOL_CONFIG_CHILDREN
, &child
,
6543 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
6545 /* first, check to ensure we've got the right child count */
6546 rvd
= spa
->spa_root_vdev
;
6548 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
6549 vdev_t
*vd
= rvd
->vdev_child
[c
];
6551 /* don't count the holes & logs as children */
6552 if (vd
->vdev_islog
|| !vdev_is_concrete(vd
)) {
6560 if (children
!= (lastlog
!= 0 ? lastlog
: rvd
->vdev_children
))
6561 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
6563 /* next, ensure no spare or cache devices are part of the split */
6564 if (nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_SPARES
, &tmp
) == 0 ||
6565 nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_L2CACHE
, &tmp
) == 0)
6566 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
6568 vml
= kmem_zalloc(children
* sizeof (vdev_t
*), KM_SLEEP
);
6569 glist
= kmem_zalloc(children
* sizeof (uint64_t), KM_SLEEP
);
6571 /* then, loop over each vdev and validate it */
6572 for (c
= 0; c
< children
; c
++) {
6573 uint64_t is_hole
= 0;
6575 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
6579 if (spa
->spa_root_vdev
->vdev_child
[c
]->vdev_ishole
||
6580 spa
->spa_root_vdev
->vdev_child
[c
]->vdev_islog
) {
6583 error
= SET_ERROR(EINVAL
);
6588 /* which disk is going to be split? */
6589 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_GUID
,
6591 error
= SET_ERROR(EINVAL
);
6595 /* look it up in the spa */
6596 vml
[c
] = spa_lookup_by_guid(spa
, glist
[c
], B_FALSE
);
6597 if (vml
[c
] == NULL
) {
6598 error
= SET_ERROR(ENODEV
);
6602 /* make sure there's nothing stopping the split */
6603 if (vml
[c
]->vdev_parent
->vdev_ops
!= &vdev_mirror_ops
||
6604 vml
[c
]->vdev_islog
||
6605 !vdev_is_concrete(vml
[c
]) ||
6606 vml
[c
]->vdev_isspare
||
6607 vml
[c
]->vdev_isl2cache
||
6608 !vdev_writeable(vml
[c
]) ||
6609 vml
[c
]->vdev_children
!= 0 ||
6610 vml
[c
]->vdev_state
!= VDEV_STATE_HEALTHY
||
6611 c
!= spa
->spa_root_vdev
->vdev_child
[c
]->vdev_id
) {
6612 error
= SET_ERROR(EINVAL
);
6616 if (vdev_dtl_required(vml
[c
]) ||
6617 vdev_resilver_needed(vml
[c
], NULL
, NULL
)) {
6618 error
= SET_ERROR(EBUSY
);
6622 /* we need certain info from the top level */
6623 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_ARRAY
,
6624 vml
[c
]->vdev_top
->vdev_ms_array
) == 0);
6625 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_SHIFT
,
6626 vml
[c
]->vdev_top
->vdev_ms_shift
) == 0);
6627 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASIZE
,
6628 vml
[c
]->vdev_top
->vdev_asize
) == 0);
6629 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASHIFT
,
6630 vml
[c
]->vdev_top
->vdev_ashift
) == 0);
6632 /* transfer per-vdev ZAPs */
6633 ASSERT3U(vml
[c
]->vdev_leaf_zap
, !=, 0);
6634 VERIFY0(nvlist_add_uint64(child
[c
],
6635 ZPOOL_CONFIG_VDEV_LEAF_ZAP
, vml
[c
]->vdev_leaf_zap
));
6637 ASSERT3U(vml
[c
]->vdev_top
->vdev_top_zap
, !=, 0);
6638 VERIFY0(nvlist_add_uint64(child
[c
],
6639 ZPOOL_CONFIG_VDEV_TOP_ZAP
,
6640 vml
[c
]->vdev_parent
->vdev_top_zap
));
6644 kmem_free(vml
, children
* sizeof (vdev_t
*));
6645 kmem_free(glist
, children
* sizeof (uint64_t));
6646 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
6649 /* stop writers from using the disks */
6650 for (c
= 0; c
< children
; c
++) {
6652 vml
[c
]->vdev_offline
= B_TRUE
;
6654 vdev_reopen(spa
->spa_root_vdev
);
6657 * Temporarily record the splitting vdevs in the spa config. This
6658 * will disappear once the config is regenerated.
6660 VERIFY(nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
6661 VERIFY(nvlist_add_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
6662 glist
, children
) == 0);
6663 kmem_free(glist
, children
* sizeof (uint64_t));
6665 mutex_enter(&spa
->spa_props_lock
);
6666 VERIFY(nvlist_add_nvlist(spa
->spa_config
, ZPOOL_CONFIG_SPLIT
,
6668 mutex_exit(&spa
->spa_props_lock
);
6669 spa
->spa_config_splitting
= nvl
;
6670 vdev_config_dirty(spa
->spa_root_vdev
);
6672 /* configure and create the new pool */
6673 VERIFY(nvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
, newname
) == 0);
6674 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
6675 exp
? POOL_STATE_EXPORTED
: POOL_STATE_ACTIVE
) == 0);
6676 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
6677 spa_version(spa
)) == 0);
6678 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
6679 spa
->spa_config_txg
) == 0);
6680 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
6681 spa_generate_guid(NULL
)) == 0);
6682 VERIFY0(nvlist_add_boolean(config
, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
));
6683 (void) nvlist_lookup_string(props
,
6684 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
6686 /* add the new pool to the namespace */
6687 newspa
= spa_add(newname
, config
, altroot
);
6688 newspa
->spa_avz_action
= AVZ_ACTION_REBUILD
;
6689 newspa
->spa_config_txg
= spa
->spa_config_txg
;
6690 spa_set_log_state(newspa
, SPA_LOG_CLEAR
);
6692 /* release the spa config lock, retaining the namespace lock */
6693 spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
6695 if (zio_injection_enabled
)
6696 zio_handle_panic_injection(spa
, FTAG
, 1);
6698 spa_activate(newspa
, spa_mode_global
);
6699 spa_async_suspend(newspa
);
6702 * Temporarily stop the initializing activity. We set the state to
6703 * ACTIVE so that we know to resume the initializing once the split
6707 list_create(&vd_list
, sizeof (vdev_t
),
6708 offsetof(vdev_t
, vdev_initialize_node
));
6710 for (c
= 0; c
< children
; c
++) {
6711 if (vml
[c
] != NULL
) {
6712 mutex_enter(&vml
[c
]->vdev_initialize_lock
);
6713 vdev_initialize_stop(vml
[c
], VDEV_INITIALIZE_ACTIVE
,
6715 mutex_exit(&vml
[c
]->vdev_initialize_lock
);
6718 vdev_initialize_stop_wait(spa
, &vd_list
);
6719 list_destroy(&vd_list
);
6721 newspa
->spa_config_source
= SPA_CONFIG_SRC_SPLIT
;
6723 /* create the new pool from the disks of the original pool */
6724 error
= spa_load(newspa
, SPA_LOAD_IMPORT
, SPA_IMPORT_ASSEMBLE
);
6728 /* if that worked, generate a real config for the new pool */
6729 if (newspa
->spa_root_vdev
!= NULL
) {
6730 VERIFY(nvlist_alloc(&newspa
->spa_config_splitting
,
6731 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
6732 VERIFY(nvlist_add_uint64(newspa
->spa_config_splitting
,
6733 ZPOOL_CONFIG_SPLIT_GUID
, spa_guid(spa
)) == 0);
6734 spa_config_set(newspa
, spa_config_generate(newspa
, NULL
, -1ULL,
6739 if (props
!= NULL
) {
6740 spa_configfile_set(newspa
, props
, B_FALSE
);
6741 error
= spa_prop_set(newspa
, props
);
6746 /* flush everything */
6747 txg
= spa_vdev_config_enter(newspa
);
6748 vdev_config_dirty(newspa
->spa_root_vdev
);
6749 (void) spa_vdev_config_exit(newspa
, NULL
, txg
, 0, FTAG
);
6751 if (zio_injection_enabled
)
6752 zio_handle_panic_injection(spa
, FTAG
, 2);
6754 spa_async_resume(newspa
);
6756 /* finally, update the original pool's config */
6757 txg
= spa_vdev_config_enter(spa
);
6758 tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
6759 error
= dmu_tx_assign(tx
, TXG_WAIT
);
6762 for (c
= 0; c
< children
; c
++) {
6763 if (vml
[c
] != NULL
) {
6766 spa_history_log_internal(spa
, "detach", tx
,
6767 "vdev=%s", vml
[c
]->vdev_path
);
6772 spa
->spa_avz_action
= AVZ_ACTION_REBUILD
;
6773 vdev_config_dirty(spa
->spa_root_vdev
);
6774 spa
->spa_config_splitting
= NULL
;
6778 (void) spa_vdev_exit(spa
, NULL
, txg
, 0);
6780 if (zio_injection_enabled
)
6781 zio_handle_panic_injection(spa
, FTAG
, 3);
6783 /* split is complete; log a history record */
6784 spa_history_log_internal(newspa
, "split", NULL
,
6785 "from pool %s", spa_name(spa
));
6787 kmem_free(vml
, children
* sizeof (vdev_t
*));
6789 /* if we're not going to mount the filesystems in userland, export */
6791 error
= spa_export_common(newname
, POOL_STATE_EXPORTED
, NULL
,
6798 spa_deactivate(newspa
);
6801 txg
= spa_vdev_config_enter(spa
);
6803 /* re-online all offlined disks */
6804 for (c
= 0; c
< children
; c
++) {
6806 vml
[c
]->vdev_offline
= B_FALSE
;
6809 /* restart initializing disks as necessary */
6810 spa_async_request(spa
, SPA_ASYNC_INITIALIZE_RESTART
);
6812 vdev_reopen(spa
->spa_root_vdev
);
6814 nvlist_free(spa
->spa_config_splitting
);
6815 spa
->spa_config_splitting
= NULL
;
6816 (void) spa_vdev_exit(spa
, NULL
, txg
, error
);
6818 kmem_free(vml
, children
* sizeof (vdev_t
*));
6823 * Find any device that's done replacing, or a vdev marked 'unspare' that's
6824 * currently spared, so we can detach it.
6827 spa_vdev_resilver_done_hunt(vdev_t
*vd
)
6829 vdev_t
*newvd
, *oldvd
;
6831 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
6832 oldvd
= spa_vdev_resilver_done_hunt(vd
->vdev_child
[c
]);
6838 * Check for a completed replacement. We always consider the first
6839 * vdev in the list to be the oldest vdev, and the last one to be
6840 * the newest (see spa_vdev_attach() for how that works). In
6841 * the case where the newest vdev is faulted, we will not automatically
6842 * remove it after a resilver completes. This is OK as it will require
6843 * user intervention to determine which disk the admin wishes to keep.
6845 if (vd
->vdev_ops
== &vdev_replacing_ops
) {
6846 ASSERT(vd
->vdev_children
> 1);
6848 newvd
= vd
->vdev_child
[vd
->vdev_children
- 1];
6849 oldvd
= vd
->vdev_child
[0];
6851 if (vdev_dtl_empty(newvd
, DTL_MISSING
) &&
6852 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
6853 !vdev_dtl_required(oldvd
))
6858 * Check for a completed resilver with the 'unspare' flag set.
6859 * Also potentially update faulted state.
6861 if (vd
->vdev_ops
== &vdev_spare_ops
) {
6862 vdev_t
*first
= vd
->vdev_child
[0];
6863 vdev_t
*last
= vd
->vdev_child
[vd
->vdev_children
- 1];
6865 if (last
->vdev_unspare
) {
6868 } else if (first
->vdev_unspare
) {
6875 if (oldvd
!= NULL
&&
6876 vdev_dtl_empty(newvd
, DTL_MISSING
) &&
6877 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
6878 !vdev_dtl_required(oldvd
))
6881 vdev_propagate_state(vd
);
6884 * If there are more than two spares attached to a disk,
6885 * and those spares are not required, then we want to
6886 * attempt to free them up now so that they can be used
6887 * by other pools. Once we're back down to a single
6888 * disk+spare, we stop removing them.
6890 if (vd
->vdev_children
> 2) {
6891 newvd
= vd
->vdev_child
[1];
6893 if (newvd
->vdev_isspare
&& last
->vdev_isspare
&&
6894 vdev_dtl_empty(last
, DTL_MISSING
) &&
6895 vdev_dtl_empty(last
, DTL_OUTAGE
) &&
6896 !vdev_dtl_required(newvd
))
6905 spa_vdev_resilver_done(spa_t
*spa
)
6907 vdev_t
*vd
, *pvd
, *ppvd
;
6908 uint64_t guid
, sguid
, pguid
, ppguid
;
6910 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6912 while ((vd
= spa_vdev_resilver_done_hunt(spa
->spa_root_vdev
)) != NULL
) {
6913 pvd
= vd
->vdev_parent
;
6914 ppvd
= pvd
->vdev_parent
;
6915 guid
= vd
->vdev_guid
;
6916 pguid
= pvd
->vdev_guid
;
6917 ppguid
= ppvd
->vdev_guid
;
6920 * If we have just finished replacing a hot spared device, then
6921 * we need to detach the parent's first child (the original hot
6924 if (ppvd
->vdev_ops
== &vdev_spare_ops
&& pvd
->vdev_id
== 0 &&
6925 ppvd
->vdev_children
== 2) {
6926 ASSERT(pvd
->vdev_ops
== &vdev_replacing_ops
);
6927 sguid
= ppvd
->vdev_child
[1]->vdev_guid
;
6929 ASSERT(vd
->vdev_resilver_txg
== 0 || !vdev_dtl_required(vd
));
6931 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6932 if (spa_vdev_detach(spa
, guid
, pguid
, B_TRUE
) != 0)
6934 if (sguid
&& spa_vdev_detach(spa
, sguid
, ppguid
, B_TRUE
) != 0)
6936 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6939 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6943 * Update the stored path or FRU for this vdev.
6946 spa_vdev_set_common(spa_t
*spa
, uint64_t guid
, const char *value
,
6950 boolean_t sync
= B_FALSE
;
6952 ASSERT(spa_writeable(spa
));
6954 spa_vdev_state_enter(spa
, SCL_ALL
);
6956 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
6957 return (spa_vdev_state_exit(spa
, NULL
, ENOENT
));
6959 if (!vd
->vdev_ops
->vdev_op_leaf
)
6960 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
6963 if (strcmp(value
, vd
->vdev_path
) != 0) {
6964 spa_strfree(vd
->vdev_path
);
6965 vd
->vdev_path
= spa_strdup(value
);
6969 if (vd
->vdev_fru
== NULL
) {
6970 vd
->vdev_fru
= spa_strdup(value
);
6972 } else if (strcmp(value
, vd
->vdev_fru
) != 0) {
6973 spa_strfree(vd
->vdev_fru
);
6974 vd
->vdev_fru
= spa_strdup(value
);
6979 return (spa_vdev_state_exit(spa
, sync
? vd
: NULL
, 0));
6983 spa_vdev_setpath(spa_t
*spa
, uint64_t guid
, const char *newpath
)
6985 return (spa_vdev_set_common(spa
, guid
, newpath
, B_TRUE
));
6989 spa_vdev_setfru(spa_t
*spa
, uint64_t guid
, const char *newfru
)
6991 return (spa_vdev_set_common(spa
, guid
, newfru
, B_FALSE
));
6995 * ==========================================================================
6997 * ==========================================================================
7000 spa_scrub_pause_resume(spa_t
*spa
, pool_scrub_cmd_t cmd
)
7002 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
7004 if (dsl_scan_resilvering(spa
->spa_dsl_pool
))
7005 return (SET_ERROR(EBUSY
));
7007 return (dsl_scrub_set_pause_resume(spa
->spa_dsl_pool
, cmd
));
7011 spa_scan_stop(spa_t
*spa
)
7013 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
7014 if (dsl_scan_resilvering(spa
->spa_dsl_pool
))
7015 return (SET_ERROR(EBUSY
));
7016 return (dsl_scan_cancel(spa
->spa_dsl_pool
));
7020 spa_scan(spa_t
*spa
, pool_scan_func_t func
)
7022 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
7024 if (func
>= POOL_SCAN_FUNCS
|| func
== POOL_SCAN_NONE
)
7025 return (SET_ERROR(ENOTSUP
));
7028 * If a resilver was requested, but there is no DTL on a
7029 * writeable leaf device, we have nothing to do.
7031 if (func
== POOL_SCAN_RESILVER
&&
7032 !vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
)) {
7033 spa_async_request(spa
, SPA_ASYNC_RESILVER_DONE
);
7037 return (dsl_scan(spa
->spa_dsl_pool
, func
));
7041 * ==========================================================================
7042 * SPA async task processing
7043 * ==========================================================================
7047 spa_async_remove(spa_t
*spa
, vdev_t
*vd
)
7049 if (vd
->vdev_remove_wanted
) {
7050 vd
->vdev_remove_wanted
= B_FALSE
;
7051 vd
->vdev_delayed_close
= B_FALSE
;
7052 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_REMOVED
, VDEV_AUX_NONE
);
7055 * We want to clear the stats, but we don't want to do a full
7056 * vdev_clear() as that will cause us to throw away
7057 * degraded/faulted state as well as attempt to reopen the
7058 * device, all of which is a waste.
7060 vd
->vdev_stat
.vs_read_errors
= 0;
7061 vd
->vdev_stat
.vs_write_errors
= 0;
7062 vd
->vdev_stat
.vs_checksum_errors
= 0;
7064 vdev_state_dirty(vd
->vdev_top
);
7067 for (int c
= 0; c
< vd
->vdev_children
; c
++)
7068 spa_async_remove(spa
, vd
->vdev_child
[c
]);
7072 spa_async_probe(spa_t
*spa
, vdev_t
*vd
)
7074 if (vd
->vdev_probe_wanted
) {
7075 vd
->vdev_probe_wanted
= B_FALSE
;
7076 vdev_reopen(vd
); /* vdev_open() does the actual probe */
7079 for (int c
= 0; c
< vd
->vdev_children
; c
++)
7080 spa_async_probe(spa
, vd
->vdev_child
[c
]);
7084 spa_async_autoexpand(spa_t
*spa
, vdev_t
*vd
)
7086 if (!spa
->spa_autoexpand
)
7089 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
7090 vdev_t
*cvd
= vd
->vdev_child
[c
];
7091 spa_async_autoexpand(spa
, cvd
);
7094 if (!vd
->vdev_ops
->vdev_op_leaf
|| vd
->vdev_physpath
== NULL
)
7097 spa_event_notify(vd
->vdev_spa
, vd
, NULL
, ESC_ZFS_VDEV_AUTOEXPAND
);
7101 spa_async_thread(void *arg
)
7103 spa_t
*spa
= (spa_t
*)arg
;
7104 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
7107 ASSERT(spa
->spa_sync_on
);
7109 mutex_enter(&spa
->spa_async_lock
);
7110 tasks
= spa
->spa_async_tasks
;
7111 spa
->spa_async_tasks
= 0;
7112 mutex_exit(&spa
->spa_async_lock
);
7115 * See if the config needs to be updated.
7117 if (tasks
& SPA_ASYNC_CONFIG_UPDATE
) {
7118 uint64_t old_space
, new_space
;
7120 mutex_enter(&spa_namespace_lock
);
7121 old_space
= metaslab_class_get_space(spa_normal_class(spa
));
7122 old_space
+= metaslab_class_get_space(spa_special_class(spa
));
7123 old_space
+= metaslab_class_get_space(spa_dedup_class(spa
));
7125 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
7127 new_space
= metaslab_class_get_space(spa_normal_class(spa
));
7128 new_space
+= metaslab_class_get_space(spa_special_class(spa
));
7129 new_space
+= metaslab_class_get_space(spa_dedup_class(spa
));
7130 mutex_exit(&spa_namespace_lock
);
7133 * If the pool grew as a result of the config update,
7134 * then log an internal history event.
7136 if (new_space
!= old_space
) {
7137 spa_history_log_internal(spa
, "vdev online", NULL
,
7138 "pool '%s' size: %llu(+%llu)",
7139 spa_name(spa
), new_space
, new_space
- old_space
);
7144 * See if any devices need to be marked REMOVED.
7146 if (tasks
& SPA_ASYNC_REMOVE
) {
7147 spa_vdev_state_enter(spa
, SCL_NONE
);
7148 spa_async_remove(spa
, spa
->spa_root_vdev
);
7149 for (int i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++)
7150 spa_async_remove(spa
, spa
->spa_l2cache
.sav_vdevs
[i
]);
7151 for (int i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
7152 spa_async_remove(spa
, spa
->spa_spares
.sav_vdevs
[i
]);
7153 (void) spa_vdev_state_exit(spa
, NULL
, 0);
7156 if ((tasks
& SPA_ASYNC_AUTOEXPAND
) && !spa_suspended(spa
)) {
7157 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
7158 spa_async_autoexpand(spa
, spa
->spa_root_vdev
);
7159 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
7163 * See if any devices need to be probed.
7165 if (tasks
& SPA_ASYNC_PROBE
) {
7166 spa_vdev_state_enter(spa
, SCL_NONE
);
7167 spa_async_probe(spa
, spa
->spa_root_vdev
);
7168 (void) spa_vdev_state_exit(spa
, NULL
, 0);
7172 * If any devices are done replacing, detach them.
7174 if (tasks
& SPA_ASYNC_RESILVER_DONE
)
7175 spa_vdev_resilver_done(spa
);
7178 * Kick off a resilver.
7180 if (tasks
& SPA_ASYNC_RESILVER
&&
7181 (!dsl_scan_resilvering(dp
) ||
7182 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_RESILVER_DEFER
)))
7183 dsl_resilver_restart(dp
, 0);
7185 if (tasks
& SPA_ASYNC_INITIALIZE_RESTART
) {
7186 mutex_enter(&spa_namespace_lock
);
7187 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
7188 vdev_initialize_restart(spa
->spa_root_vdev
);
7189 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
7190 mutex_exit(&spa_namespace_lock
);
7194 * Let the world know that we're done.
7196 mutex_enter(&spa
->spa_async_lock
);
7197 spa
->spa_async_thread
= NULL
;
7198 cv_broadcast(&spa
->spa_async_cv
);
7199 mutex_exit(&spa
->spa_async_lock
);
7204 spa_async_suspend(spa_t
*spa
)
7206 mutex_enter(&spa
->spa_async_lock
);
7207 spa
->spa_async_suspended
++;
7208 while (spa
->spa_async_thread
!= NULL
)
7209 cv_wait(&spa
->spa_async_cv
, &spa
->spa_async_lock
);
7210 mutex_exit(&spa
->spa_async_lock
);
7212 spa_vdev_remove_suspend(spa
);
7214 zthr_t
*condense_thread
= spa
->spa_condense_zthr
;
7215 if (condense_thread
!= NULL
)
7216 zthr_cancel(condense_thread
);
7218 zthr_t
*discard_thread
= spa
->spa_checkpoint_discard_zthr
;
7219 if (discard_thread
!= NULL
)
7220 zthr_cancel(discard_thread
);
7224 spa_async_resume(spa_t
*spa
)
7226 mutex_enter(&spa
->spa_async_lock
);
7227 ASSERT(spa
->spa_async_suspended
!= 0);
7228 spa
->spa_async_suspended
--;
7229 mutex_exit(&spa
->spa_async_lock
);
7230 spa_restart_removal(spa
);
7232 zthr_t
*condense_thread
= spa
->spa_condense_zthr
;
7233 if (condense_thread
!= NULL
)
7234 zthr_resume(condense_thread
);
7236 zthr_t
*discard_thread
= spa
->spa_checkpoint_discard_zthr
;
7237 if (discard_thread
!= NULL
)
7238 zthr_resume(discard_thread
);
7242 spa_async_tasks_pending(spa_t
*spa
)
7244 uint_t non_config_tasks
;
7246 boolean_t config_task_suspended
;
7248 non_config_tasks
= spa
->spa_async_tasks
& ~SPA_ASYNC_CONFIG_UPDATE
;
7249 config_task
= spa
->spa_async_tasks
& SPA_ASYNC_CONFIG_UPDATE
;
7250 if (spa
->spa_ccw_fail_time
== 0) {
7251 config_task_suspended
= B_FALSE
;
7253 config_task_suspended
=
7254 (gethrtime() - spa
->spa_ccw_fail_time
) <
7255 ((hrtime_t
)zfs_ccw_retry_interval
* NANOSEC
);
7258 return (non_config_tasks
|| (config_task
&& !config_task_suspended
));
7262 spa_async_dispatch(spa_t
*spa
)
7264 mutex_enter(&spa
->spa_async_lock
);
7265 if (spa_async_tasks_pending(spa
) &&
7266 !spa
->spa_async_suspended
&&
7267 spa
->spa_async_thread
== NULL
&&
7269 spa
->spa_async_thread
= thread_create(NULL
, 0,
7270 spa_async_thread
, spa
, 0, &p0
, TS_RUN
, maxclsyspri
);
7271 mutex_exit(&spa
->spa_async_lock
);
7275 spa_async_request(spa_t
*spa
, int task
)
7277 zfs_dbgmsg("spa=%s async request task=%u", spa
->spa_name
, task
);
7278 mutex_enter(&spa
->spa_async_lock
);
7279 spa
->spa_async_tasks
|= task
;
7280 mutex_exit(&spa
->spa_async_lock
);
7284 * ==========================================================================
7285 * SPA syncing routines
7286 * ==========================================================================
7290 bpobj_enqueue_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
7293 bpobj_enqueue(bpo
, bp
, tx
);
7298 spa_free_sync_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
7302 zio_nowait(zio_free_sync(zio
, zio
->io_spa
, dmu_tx_get_txg(tx
), bp
,
7308 * Note: this simple function is not inlined to make it easier to dtrace the
7309 * amount of time spent syncing frees.
7312 spa_sync_frees(spa_t
*spa
, bplist_t
*bpl
, dmu_tx_t
*tx
)
7314 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
7315 bplist_iterate(bpl
, spa_free_sync_cb
, zio
, tx
);
7316 VERIFY(zio_wait(zio
) == 0);
7320 * Note: this simple function is not inlined to make it easier to dtrace the
7321 * amount of time spent syncing deferred frees.
7324 spa_sync_deferred_frees(spa_t
*spa
, dmu_tx_t
*tx
)
7326 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
7327 VERIFY3U(bpobj_iterate(&spa
->spa_deferred_bpobj
,
7328 spa_free_sync_cb
, zio
, tx
), ==, 0);
7329 VERIFY0(zio_wait(zio
));
7333 spa_sync_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
*nv
, dmu_tx_t
*tx
)
7335 char *packed
= NULL
;
7340 VERIFY(nvlist_size(nv
, &nvsize
, NV_ENCODE_XDR
) == 0);
7343 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
7344 * information. This avoids the dmu_buf_will_dirty() path and
7345 * saves us a pre-read to get data we don't actually care about.
7347 bufsize
= P2ROUNDUP((uint64_t)nvsize
, SPA_CONFIG_BLOCKSIZE
);
7348 packed
= vmem_alloc(bufsize
, KM_SLEEP
);
7350 VERIFY(nvlist_pack(nv
, &packed
, &nvsize
, NV_ENCODE_XDR
,
7352 bzero(packed
+ nvsize
, bufsize
- nvsize
);
7354 dmu_write(spa
->spa_meta_objset
, obj
, 0, bufsize
, packed
, tx
);
7356 vmem_free(packed
, bufsize
);
7358 VERIFY(0 == dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
));
7359 dmu_buf_will_dirty(db
, tx
);
7360 *(uint64_t *)db
->db_data
= nvsize
;
7361 dmu_buf_rele(db
, FTAG
);
7365 spa_sync_aux_dev(spa_t
*spa
, spa_aux_vdev_t
*sav
, dmu_tx_t
*tx
,
7366 const char *config
, const char *entry
)
7376 * Update the MOS nvlist describing the list of available devices.
7377 * spa_validate_aux() will have already made sure this nvlist is
7378 * valid and the vdevs are labeled appropriately.
7380 if (sav
->sav_object
== 0) {
7381 sav
->sav_object
= dmu_object_alloc(spa
->spa_meta_objset
,
7382 DMU_OT_PACKED_NVLIST
, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE
,
7383 sizeof (uint64_t), tx
);
7384 VERIFY(zap_update(spa
->spa_meta_objset
,
7385 DMU_POOL_DIRECTORY_OBJECT
, entry
, sizeof (uint64_t), 1,
7386 &sav
->sav_object
, tx
) == 0);
7389 VERIFY(nvlist_alloc(&nvroot
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
7390 if (sav
->sav_count
== 0) {
7391 VERIFY(nvlist_add_nvlist_array(nvroot
, config
, NULL
, 0) == 0);
7393 list
= kmem_alloc(sav
->sav_count
*sizeof (void *), KM_SLEEP
);
7394 for (i
= 0; i
< sav
->sav_count
; i
++)
7395 list
[i
] = vdev_config_generate(spa
, sav
->sav_vdevs
[i
],
7396 B_FALSE
, VDEV_CONFIG_L2CACHE
);
7397 VERIFY(nvlist_add_nvlist_array(nvroot
, config
, list
,
7398 sav
->sav_count
) == 0);
7399 for (i
= 0; i
< sav
->sav_count
; i
++)
7400 nvlist_free(list
[i
]);
7401 kmem_free(list
, sav
->sav_count
* sizeof (void *));
7404 spa_sync_nvlist(spa
, sav
->sav_object
, nvroot
, tx
);
7405 nvlist_free(nvroot
);
7407 sav
->sav_sync
= B_FALSE
;
7411 * Rebuild spa's all-vdev ZAP from the vdev ZAPs indicated in each vdev_t.
7412 * The all-vdev ZAP must be empty.
7415 spa_avz_build(vdev_t
*vd
, uint64_t avz
, dmu_tx_t
*tx
)
7417 spa_t
*spa
= vd
->vdev_spa
;
7419 if (vd
->vdev_top_zap
!= 0) {
7420 VERIFY0(zap_add_int(spa
->spa_meta_objset
, avz
,
7421 vd
->vdev_top_zap
, tx
));
7423 if (vd
->vdev_leaf_zap
!= 0) {
7424 VERIFY0(zap_add_int(spa
->spa_meta_objset
, avz
,
7425 vd
->vdev_leaf_zap
, tx
));
7427 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
7428 spa_avz_build(vd
->vdev_child
[i
], avz
, tx
);
7433 spa_sync_config_object(spa_t
*spa
, dmu_tx_t
*tx
)
7438 * If the pool is being imported from a pre-per-vdev-ZAP version of ZFS,
7439 * its config may not be dirty but we still need to build per-vdev ZAPs.
7440 * Similarly, if the pool is being assembled (e.g. after a split), we
7441 * need to rebuild the AVZ although the config may not be dirty.
7443 if (list_is_empty(&spa
->spa_config_dirty_list
) &&
7444 spa
->spa_avz_action
== AVZ_ACTION_NONE
)
7447 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
7449 ASSERT(spa
->spa_avz_action
== AVZ_ACTION_NONE
||
7450 spa
->spa_avz_action
== AVZ_ACTION_INITIALIZE
||
7451 spa
->spa_all_vdev_zaps
!= 0);
7453 if (spa
->spa_avz_action
== AVZ_ACTION_REBUILD
) {
7454 /* Make and build the new AVZ */
7455 uint64_t new_avz
= zap_create(spa
->spa_meta_objset
,
7456 DMU_OTN_ZAP_METADATA
, DMU_OT_NONE
, 0, tx
);
7457 spa_avz_build(spa
->spa_root_vdev
, new_avz
, tx
);
7459 /* Diff old AVZ with new one */
7463 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
7464 spa
->spa_all_vdev_zaps
);
7465 zap_cursor_retrieve(&zc
, &za
) == 0;
7466 zap_cursor_advance(&zc
)) {
7467 uint64_t vdzap
= za
.za_first_integer
;
7468 if (zap_lookup_int(spa
->spa_meta_objset
, new_avz
,
7471 * ZAP is listed in old AVZ but not in new one;
7474 VERIFY0(zap_destroy(spa
->spa_meta_objset
, vdzap
,
7479 zap_cursor_fini(&zc
);
7481 /* Destroy the old AVZ */
7482 VERIFY0(zap_destroy(spa
->spa_meta_objset
,
7483 spa
->spa_all_vdev_zaps
, tx
));
7485 /* Replace the old AVZ in the dir obj with the new one */
7486 VERIFY0(zap_update(spa
->spa_meta_objset
,
7487 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_VDEV_ZAP_MAP
,
7488 sizeof (new_avz
), 1, &new_avz
, tx
));
7490 spa
->spa_all_vdev_zaps
= new_avz
;
7491 } else if (spa
->spa_avz_action
== AVZ_ACTION_DESTROY
) {
7495 /* Walk through the AVZ and destroy all listed ZAPs */
7496 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
7497 spa
->spa_all_vdev_zaps
);
7498 zap_cursor_retrieve(&zc
, &za
) == 0;
7499 zap_cursor_advance(&zc
)) {
7500 uint64_t zap
= za
.za_first_integer
;
7501 VERIFY0(zap_destroy(spa
->spa_meta_objset
, zap
, tx
));
7504 zap_cursor_fini(&zc
);
7506 /* Destroy and unlink the AVZ itself */
7507 VERIFY0(zap_destroy(spa
->spa_meta_objset
,
7508 spa
->spa_all_vdev_zaps
, tx
));
7509 VERIFY0(zap_remove(spa
->spa_meta_objset
,
7510 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_VDEV_ZAP_MAP
, tx
));
7511 spa
->spa_all_vdev_zaps
= 0;
7514 if (spa
->spa_all_vdev_zaps
== 0) {
7515 spa
->spa_all_vdev_zaps
= zap_create_link(spa
->spa_meta_objset
,
7516 DMU_OTN_ZAP_METADATA
, DMU_POOL_DIRECTORY_OBJECT
,
7517 DMU_POOL_VDEV_ZAP_MAP
, tx
);
7519 spa
->spa_avz_action
= AVZ_ACTION_NONE
;
7521 /* Create ZAPs for vdevs that don't have them. */
7522 vdev_construct_zaps(spa
->spa_root_vdev
, tx
);
7524 config
= spa_config_generate(spa
, spa
->spa_root_vdev
,
7525 dmu_tx_get_txg(tx
), B_FALSE
);
7528 * If we're upgrading the spa version then make sure that
7529 * the config object gets updated with the correct version.
7531 if (spa
->spa_ubsync
.ub_version
< spa
->spa_uberblock
.ub_version
)
7532 fnvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
7533 spa
->spa_uberblock
.ub_version
);
7535 spa_config_exit(spa
, SCL_STATE
, FTAG
);
7537 nvlist_free(spa
->spa_config_syncing
);
7538 spa
->spa_config_syncing
= config
;
7540 spa_sync_nvlist(spa
, spa
->spa_config_object
, config
, tx
);
7544 spa_sync_version(void *arg
, dmu_tx_t
*tx
)
7546 uint64_t *versionp
= arg
;
7547 uint64_t version
= *versionp
;
7548 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
7551 * Setting the version is special cased when first creating the pool.
7553 ASSERT(tx
->tx_txg
!= TXG_INITIAL
);
7555 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
7556 ASSERT(version
>= spa_version(spa
));
7558 spa
->spa_uberblock
.ub_version
= version
;
7559 vdev_config_dirty(spa
->spa_root_vdev
);
7560 spa_history_log_internal(spa
, "set", tx
, "version=%lld", version
);
7564 * Set zpool properties.
7567 spa_sync_props(void *arg
, dmu_tx_t
*tx
)
7569 nvlist_t
*nvp
= arg
;
7570 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
7571 objset_t
*mos
= spa
->spa_meta_objset
;
7572 nvpair_t
*elem
= NULL
;
7574 mutex_enter(&spa
->spa_props_lock
);
7576 while ((elem
= nvlist_next_nvpair(nvp
, elem
))) {
7578 char *strval
, *fname
;
7580 const char *propname
;
7581 zprop_type_t proptype
;
7584 switch (prop
= zpool_name_to_prop(nvpair_name(elem
))) {
7585 case ZPOOL_PROP_INVAL
:
7587 * We checked this earlier in spa_prop_validate().
7589 ASSERT(zpool_prop_feature(nvpair_name(elem
)));
7591 fname
= strchr(nvpair_name(elem
), '@') + 1;
7592 VERIFY0(zfeature_lookup_name(fname
, &fid
));
7594 spa_feature_enable(spa
, fid
, tx
);
7595 spa_history_log_internal(spa
, "set", tx
,
7596 "%s=enabled", nvpair_name(elem
));
7599 case ZPOOL_PROP_VERSION
:
7600 intval
= fnvpair_value_uint64(elem
);
7602 * The version is synced separately before other
7603 * properties and should be correct by now.
7605 ASSERT3U(spa_version(spa
), >=, intval
);
7608 case ZPOOL_PROP_ALTROOT
:
7610 * 'altroot' is a non-persistent property. It should
7611 * have been set temporarily at creation or import time.
7613 ASSERT(spa
->spa_root
!= NULL
);
7616 case ZPOOL_PROP_READONLY
:
7617 case ZPOOL_PROP_CACHEFILE
:
7619 * 'readonly' and 'cachefile' are also non-persisitent
7623 case ZPOOL_PROP_COMMENT
:
7624 strval
= fnvpair_value_string(elem
);
7625 if (spa
->spa_comment
!= NULL
)
7626 spa_strfree(spa
->spa_comment
);
7627 spa
->spa_comment
= spa_strdup(strval
);
7629 * We need to dirty the configuration on all the vdevs
7630 * so that their labels get updated. It's unnecessary
7631 * to do this for pool creation since the vdev's
7632 * configuration has already been dirtied.
7634 if (tx
->tx_txg
!= TXG_INITIAL
)
7635 vdev_config_dirty(spa
->spa_root_vdev
);
7636 spa_history_log_internal(spa
, "set", tx
,
7637 "%s=%s", nvpair_name(elem
), strval
);
7641 * Set pool property values in the poolprops mos object.
7643 if (spa
->spa_pool_props_object
== 0) {
7644 spa
->spa_pool_props_object
=
7645 zap_create_link(mos
, DMU_OT_POOL_PROPS
,
7646 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_PROPS
,
7650 /* normalize the property name */
7651 propname
= zpool_prop_to_name(prop
);
7652 proptype
= zpool_prop_get_type(prop
);
7654 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
7655 ASSERT(proptype
== PROP_TYPE_STRING
);
7656 strval
= fnvpair_value_string(elem
);
7657 VERIFY0(zap_update(mos
,
7658 spa
->spa_pool_props_object
, propname
,
7659 1, strlen(strval
) + 1, strval
, tx
));
7660 spa_history_log_internal(spa
, "set", tx
,
7661 "%s=%s", nvpair_name(elem
), strval
);
7662 } else if (nvpair_type(elem
) == DATA_TYPE_UINT64
) {
7663 intval
= fnvpair_value_uint64(elem
);
7665 if (proptype
== PROP_TYPE_INDEX
) {
7667 VERIFY0(zpool_prop_index_to_string(
7668 prop
, intval
, &unused
));
7670 VERIFY0(zap_update(mos
,
7671 spa
->spa_pool_props_object
, propname
,
7672 8, 1, &intval
, tx
));
7673 spa_history_log_internal(spa
, "set", tx
,
7674 "%s=%lld", nvpair_name(elem
), intval
);
7676 ASSERT(0); /* not allowed */
7680 case ZPOOL_PROP_DELEGATION
:
7681 spa
->spa_delegation
= intval
;
7683 case ZPOOL_PROP_BOOTFS
:
7684 spa
->spa_bootfs
= intval
;
7686 case ZPOOL_PROP_FAILUREMODE
:
7687 spa
->spa_failmode
= intval
;
7689 case ZPOOL_PROP_AUTOEXPAND
:
7690 spa
->spa_autoexpand
= intval
;
7691 if (tx
->tx_txg
!= TXG_INITIAL
)
7692 spa_async_request(spa
,
7693 SPA_ASYNC_AUTOEXPAND
);
7695 case ZPOOL_PROP_MULTIHOST
:
7696 spa
->spa_multihost
= intval
;
7698 case ZPOOL_PROP_DEDUPDITTO
:
7699 spa
->spa_dedup_ditto
= intval
;
7708 mutex_exit(&spa
->spa_props_lock
);
7712 * Perform one-time upgrade on-disk changes. spa_version() does not
7713 * reflect the new version this txg, so there must be no changes this
7714 * txg to anything that the upgrade code depends on after it executes.
7715 * Therefore this must be called after dsl_pool_sync() does the sync
7719 spa_sync_upgrades(spa_t
*spa
, dmu_tx_t
*tx
)
7721 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
7723 ASSERT(spa
->spa_sync_pass
== 1);
7725 rrw_enter(&dp
->dp_config_rwlock
, RW_WRITER
, FTAG
);
7727 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_ORIGIN
&&
7728 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_ORIGIN
) {
7729 dsl_pool_create_origin(dp
, tx
);
7731 /* Keeping the origin open increases spa_minref */
7732 spa
->spa_minref
+= 3;
7735 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_NEXT_CLONES
&&
7736 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_NEXT_CLONES
) {
7737 dsl_pool_upgrade_clones(dp
, tx
);
7740 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_DIR_CLONES
&&
7741 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_DIR_CLONES
) {
7742 dsl_pool_upgrade_dir_clones(dp
, tx
);
7744 /* Keeping the freedir open increases spa_minref */
7745 spa
->spa_minref
+= 3;
7748 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_FEATURES
&&
7749 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
7750 spa_feature_create_zap_objects(spa
, tx
);
7754 * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable
7755 * when possibility to use lz4 compression for metadata was added
7756 * Old pools that have this feature enabled must be upgraded to have
7757 * this feature active
7759 if (spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
7760 boolean_t lz4_en
= spa_feature_is_enabled(spa
,
7761 SPA_FEATURE_LZ4_COMPRESS
);
7762 boolean_t lz4_ac
= spa_feature_is_active(spa
,
7763 SPA_FEATURE_LZ4_COMPRESS
);
7765 if (lz4_en
&& !lz4_ac
)
7766 spa_feature_incr(spa
, SPA_FEATURE_LZ4_COMPRESS
, tx
);
7770 * If we haven't written the salt, do so now. Note that the
7771 * feature may not be activated yet, but that's fine since
7772 * the presence of this ZAP entry is backwards compatible.
7774 if (zap_contains(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
7775 DMU_POOL_CHECKSUM_SALT
) == ENOENT
) {
7776 VERIFY0(zap_add(spa
->spa_meta_objset
,
7777 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CHECKSUM_SALT
, 1,
7778 sizeof (spa
->spa_cksum_salt
.zcs_bytes
),
7779 spa
->spa_cksum_salt
.zcs_bytes
, tx
));
7782 rrw_exit(&dp
->dp_config_rwlock
, FTAG
);
7786 vdev_indirect_state_sync_verify(vdev_t
*vd
)
7788 ASSERTV(vdev_indirect_mapping_t
*vim
= vd
->vdev_indirect_mapping
);
7789 ASSERTV(vdev_indirect_births_t
*vib
= vd
->vdev_indirect_births
);
7791 if (vd
->vdev_ops
== &vdev_indirect_ops
) {
7792 ASSERT(vim
!= NULL
);
7793 ASSERT(vib
!= NULL
);
7796 uint64_t obsolete_sm_object
= 0;
7797 ASSERT0(vdev_obsolete_sm_object(vd
, &obsolete_sm_object
));
7798 if (obsolete_sm_object
!= 0) {
7799 ASSERT(vd
->vdev_obsolete_sm
!= NULL
);
7800 ASSERT(vd
->vdev_removing
||
7801 vd
->vdev_ops
== &vdev_indirect_ops
);
7802 ASSERT(vdev_indirect_mapping_num_entries(vim
) > 0);
7803 ASSERT(vdev_indirect_mapping_bytes_mapped(vim
) > 0);
7804 ASSERT3U(obsolete_sm_object
, ==,
7805 space_map_object(vd
->vdev_obsolete_sm
));
7806 ASSERT3U(vdev_indirect_mapping_bytes_mapped(vim
), >=,
7807 space_map_allocated(vd
->vdev_obsolete_sm
));
7809 ASSERT(vd
->vdev_obsolete_segments
!= NULL
);
7812 * Since frees / remaps to an indirect vdev can only
7813 * happen in syncing context, the obsolete segments
7814 * tree must be empty when we start syncing.
7816 ASSERT0(range_tree_space(vd
->vdev_obsolete_segments
));
7820 * Sync the specified transaction group. New blocks may be dirtied as
7821 * part of the process, so we iterate until it converges.
7824 spa_sync(spa_t
*spa
, uint64_t txg
)
7826 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
7827 objset_t
*mos
= spa
->spa_meta_objset
;
7828 bplist_t
*free_bpl
= &spa
->spa_free_bplist
[txg
& TXG_MASK
];
7829 metaslab_class_t
*normal
= spa_normal_class(spa
);
7830 metaslab_class_t
*special
= spa_special_class(spa
);
7831 metaslab_class_t
*dedup
= spa_dedup_class(spa
);
7832 vdev_t
*rvd
= spa
->spa_root_vdev
;
7836 uint32_t max_queue_depth
= zfs_vdev_async_write_max_active
*
7837 zfs_vdev_queue_depth_pct
/ 100;
7839 VERIFY(spa_writeable(spa
));
7842 * Wait for i/os issued in open context that need to complete
7843 * before this txg syncs.
7845 (void) zio_wait(spa
->spa_txg_zio
[txg
& TXG_MASK
]);
7846 spa
->spa_txg_zio
[txg
& TXG_MASK
] = zio_root(spa
, NULL
, NULL
,
7850 * Lock out configuration changes.
7852 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
7854 spa
->spa_syncing_txg
= txg
;
7855 spa
->spa_sync_pass
= 0;
7857 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
7858 mutex_enter(&spa
->spa_alloc_locks
[i
]);
7859 VERIFY0(avl_numnodes(&spa
->spa_alloc_trees
[i
]));
7860 mutex_exit(&spa
->spa_alloc_locks
[i
]);
7864 * If there are any pending vdev state changes, convert them
7865 * into config changes that go out with this transaction group.
7867 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
7868 while (list_head(&spa
->spa_state_dirty_list
) != NULL
) {
7870 * We need the write lock here because, for aux vdevs,
7871 * calling vdev_config_dirty() modifies sav_config.
7872 * This is ugly and will become unnecessary when we
7873 * eliminate the aux vdev wart by integrating all vdevs
7874 * into the root vdev tree.
7876 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7877 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_WRITER
);
7878 while ((vd
= list_head(&spa
->spa_state_dirty_list
)) != NULL
) {
7879 vdev_state_clean(vd
);
7880 vdev_config_dirty(vd
);
7882 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7883 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
7885 spa_config_exit(spa
, SCL_STATE
, FTAG
);
7887 tx
= dmu_tx_create_assigned(dp
, txg
);
7889 spa
->spa_sync_starttime
= gethrtime();
7890 taskq_cancel_id(system_delay_taskq
, spa
->spa_deadman_tqid
);
7891 spa
->spa_deadman_tqid
= taskq_dispatch_delay(system_delay_taskq
,
7892 spa_deadman
, spa
, TQ_SLEEP
, ddi_get_lbolt() +
7893 NSEC_TO_TICK(spa
->spa_deadman_synctime
));
7896 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
7897 * set spa_deflate if we have no raid-z vdevs.
7899 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_RAIDZ_DEFLATE
&&
7900 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
7903 for (i
= 0; i
< rvd
->vdev_children
; i
++) {
7904 vd
= rvd
->vdev_child
[i
];
7905 if (vd
->vdev_deflate_ratio
!= SPA_MINBLOCKSIZE
)
7908 if (i
== rvd
->vdev_children
) {
7909 spa
->spa_deflate
= TRUE
;
7910 VERIFY(0 == zap_add(spa
->spa_meta_objset
,
7911 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
7912 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
));
7917 * Set the top-level vdev's max queue depth. Evaluate each
7918 * top-level's async write queue depth in case it changed.
7919 * The max queue depth will not change in the middle of syncing
7922 uint64_t slots_per_allocator
= 0;
7923 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
7924 vdev_t
*tvd
= rvd
->vdev_child
[c
];
7925 metaslab_group_t
*mg
= tvd
->vdev_mg
;
7926 metaslab_class_t
*mc
;
7928 if (mg
== NULL
|| !metaslab_group_initialized(mg
))
7932 if (mc
!= normal
&& mc
!= special
&& mc
!= dedup
)
7936 * It is safe to do a lock-free check here because only async
7937 * allocations look at mg_max_alloc_queue_depth, and async
7938 * allocations all happen from spa_sync().
7940 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++)
7941 ASSERT0(zfs_refcount_count(
7942 &(mg
->mg_alloc_queue_depth
[i
])));
7943 mg
->mg_max_alloc_queue_depth
= max_queue_depth
;
7945 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
7946 mg
->mg_cur_max_alloc_queue_depth
[i
] =
7947 zfs_vdev_def_queue_depth
;
7949 slots_per_allocator
+= zfs_vdev_def_queue_depth
;
7952 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
7953 ASSERT0(zfs_refcount_count(&normal
->mc_alloc_slots
[i
]));
7954 ASSERT0(zfs_refcount_count(&special
->mc_alloc_slots
[i
]));
7955 ASSERT0(zfs_refcount_count(&dedup
->mc_alloc_slots
[i
]));
7956 normal
->mc_alloc_max_slots
[i
] = slots_per_allocator
;
7957 special
->mc_alloc_max_slots
[i
] = slots_per_allocator
;
7958 dedup
->mc_alloc_max_slots
[i
] = slots_per_allocator
;
7960 normal
->mc_alloc_throttle_enabled
= zio_dva_throttle_enabled
;
7961 special
->mc_alloc_throttle_enabled
= zio_dva_throttle_enabled
;
7962 dedup
->mc_alloc_throttle_enabled
= zio_dva_throttle_enabled
;
7964 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
7965 vdev_t
*vd
= rvd
->vdev_child
[c
];
7966 vdev_indirect_state_sync_verify(vd
);
7968 if (vdev_indirect_should_condense(vd
)) {
7969 spa_condense_indirect_start_sync(vd
, tx
);
7975 * Iterate to convergence.
7978 int pass
= ++spa
->spa_sync_pass
;
7980 spa_sync_config_object(spa
, tx
);
7981 spa_sync_aux_dev(spa
, &spa
->spa_spares
, tx
,
7982 ZPOOL_CONFIG_SPARES
, DMU_POOL_SPARES
);
7983 spa_sync_aux_dev(spa
, &spa
->spa_l2cache
, tx
,
7984 ZPOOL_CONFIG_L2CACHE
, DMU_POOL_L2CACHE
);
7985 spa_errlog_sync(spa
, txg
);
7986 dsl_pool_sync(dp
, txg
);
7988 if (pass
< zfs_sync_pass_deferred_free
) {
7989 spa_sync_frees(spa
, free_bpl
, tx
);
7992 * We can not defer frees in pass 1, because
7993 * we sync the deferred frees later in pass 1.
7995 ASSERT3U(pass
, >, 1);
7996 bplist_iterate(free_bpl
, bpobj_enqueue_cb
,
7997 &spa
->spa_deferred_bpobj
, tx
);
8001 dsl_scan_sync(dp
, tx
);
8003 if (spa
->spa_vdev_removal
!= NULL
)
8006 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, txg
))
8011 spa_sync_upgrades(spa
, tx
);
8013 spa
->spa_uberblock
.ub_rootbp
.blk_birth
);
8015 * Note: We need to check if the MOS is dirty
8016 * because we could have marked the MOS dirty
8017 * without updating the uberblock (e.g. if we
8018 * have sync tasks but no dirty user data). We
8019 * need to check the uberblock's rootbp because
8020 * it is updated if we have synced out dirty
8021 * data (though in this case the MOS will most
8022 * likely also be dirty due to second order
8023 * effects, we don't want to rely on that here).
8025 if (spa
->spa_uberblock
.ub_rootbp
.blk_birth
< txg
&&
8026 !dmu_objset_is_dirty(mos
, txg
)) {
8028 * Nothing changed on the first pass,
8029 * therefore this TXG is a no-op. Avoid
8030 * syncing deferred frees, so that we
8031 * can keep this TXG as a no-op.
8033 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
,
8035 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
8036 ASSERT(txg_list_empty(&dp
->dp_sync_tasks
, txg
));
8037 ASSERT(txg_list_empty(&dp
->dp_early_sync_tasks
,
8041 spa_sync_deferred_frees(spa
, tx
);
8044 } while (dmu_objset_is_dirty(mos
, txg
));
8047 if (!list_is_empty(&spa
->spa_config_dirty_list
)) {
8049 * Make sure that the number of ZAPs for all the vdevs matches
8050 * the number of ZAPs in the per-vdev ZAP list. This only gets
8051 * called if the config is dirty; otherwise there may be
8052 * outstanding AVZ operations that weren't completed in
8053 * spa_sync_config_object.
8055 uint64_t all_vdev_zap_entry_count
;
8056 ASSERT0(zap_count(spa
->spa_meta_objset
,
8057 spa
->spa_all_vdev_zaps
, &all_vdev_zap_entry_count
));
8058 ASSERT3U(vdev_count_verify_zaps(spa
->spa_root_vdev
), ==,
8059 all_vdev_zap_entry_count
);
8063 if (spa
->spa_vdev_removal
!= NULL
) {
8064 ASSERT0(spa
->spa_vdev_removal
->svr_bytes_done
[txg
& TXG_MASK
]);
8068 * Rewrite the vdev configuration (which includes the uberblock)
8069 * to commit the transaction group.
8071 * If there are no dirty vdevs, we sync the uberblock to a few
8072 * random top-level vdevs that are known to be visible in the
8073 * config cache (see spa_vdev_add() for a complete description).
8074 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
8078 * We hold SCL_STATE to prevent vdev open/close/etc.
8079 * while we're attempting to write the vdev labels.
8081 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
8083 if (list_is_empty(&spa
->spa_config_dirty_list
)) {
8084 vdev_t
*svd
[SPA_SYNC_MIN_VDEVS
] = { NULL
};
8086 int children
= rvd
->vdev_children
;
8087 int c0
= spa_get_random(children
);
8089 for (int c
= 0; c
< children
; c
++) {
8090 vd
= rvd
->vdev_child
[(c0
+ c
) % children
];
8092 /* Stop when revisiting the first vdev */
8093 if (c
> 0 && svd
[0] == vd
)
8096 if (vd
->vdev_ms_array
== 0 || vd
->vdev_islog
||
8097 !vdev_is_concrete(vd
))
8100 svd
[svdcount
++] = vd
;
8101 if (svdcount
== SPA_SYNC_MIN_VDEVS
)
8104 error
= vdev_config_sync(svd
, svdcount
, txg
);
8106 error
= vdev_config_sync(rvd
->vdev_child
,
8107 rvd
->vdev_children
, txg
);
8111 spa
->spa_last_synced_guid
= rvd
->vdev_guid
;
8113 spa_config_exit(spa
, SCL_STATE
, FTAG
);
8117 zio_suspend(spa
, NULL
, ZIO_SUSPEND_IOERR
);
8118 zio_resume_wait(spa
);
8122 taskq_cancel_id(system_delay_taskq
, spa
->spa_deadman_tqid
);
8123 spa
->spa_deadman_tqid
= 0;
8126 * Clear the dirty config list.
8128 while ((vd
= list_head(&spa
->spa_config_dirty_list
)) != NULL
)
8129 vdev_config_clean(vd
);
8132 * Now that the new config has synced transactionally,
8133 * let it become visible to the config cache.
8135 if (spa
->spa_config_syncing
!= NULL
) {
8136 spa_config_set(spa
, spa
->spa_config_syncing
);
8137 spa
->spa_config_txg
= txg
;
8138 spa
->spa_config_syncing
= NULL
;
8141 dsl_pool_sync_done(dp
, txg
);
8143 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
8144 mutex_enter(&spa
->spa_alloc_locks
[i
]);
8145 VERIFY0(avl_numnodes(&spa
->spa_alloc_trees
[i
]));
8146 mutex_exit(&spa
->spa_alloc_locks
[i
]);
8150 * Update usable space statistics.
8152 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, TXG_CLEAN(txg
)))
8154 vdev_sync_done(vd
, txg
);
8156 spa_update_dspace(spa
);
8159 * It had better be the case that we didn't dirty anything
8160 * since vdev_config_sync().
8162 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
, txg
));
8163 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
8164 ASSERT(txg_list_empty(&spa
->spa_vdev_txg_list
, txg
));
8166 while (zfs_pause_spa_sync
)
8169 spa
->spa_sync_pass
= 0;
8172 * Update the last synced uberblock here. We want to do this at
8173 * the end of spa_sync() so that consumers of spa_last_synced_txg()
8174 * will be guaranteed that all the processing associated with
8175 * that txg has been completed.
8177 spa
->spa_ubsync
= spa
->spa_uberblock
;
8178 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
8180 spa_handle_ignored_writes(spa
);
8183 * If any async tasks have been requested, kick them off.
8185 spa_async_dispatch(spa
);
8189 * Sync all pools. We don't want to hold the namespace lock across these
8190 * operations, so we take a reference on the spa_t and drop the lock during the
8194 spa_sync_allpools(void)
8197 mutex_enter(&spa_namespace_lock
);
8198 while ((spa
= spa_next(spa
)) != NULL
) {
8199 if (spa_state(spa
) != POOL_STATE_ACTIVE
||
8200 !spa_writeable(spa
) || spa_suspended(spa
))
8202 spa_open_ref(spa
, FTAG
);
8203 mutex_exit(&spa_namespace_lock
);
8204 txg_wait_synced(spa_get_dsl(spa
), 0);
8205 mutex_enter(&spa_namespace_lock
);
8206 spa_close(spa
, FTAG
);
8208 mutex_exit(&spa_namespace_lock
);
8212 * ==========================================================================
8213 * Miscellaneous routines
8214 * ==========================================================================
8218 * Remove all pools in the system.
8226 * Remove all cached state. All pools should be closed now,
8227 * so every spa in the AVL tree should be unreferenced.
8229 mutex_enter(&spa_namespace_lock
);
8230 while ((spa
= spa_next(NULL
)) != NULL
) {
8232 * Stop async tasks. The async thread may need to detach
8233 * a device that's been replaced, which requires grabbing
8234 * spa_namespace_lock, so we must drop it here.
8236 spa_open_ref(spa
, FTAG
);
8237 mutex_exit(&spa_namespace_lock
);
8238 spa_async_suspend(spa
);
8239 mutex_enter(&spa_namespace_lock
);
8240 spa_close(spa
, FTAG
);
8242 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
8244 spa_deactivate(spa
);
8248 mutex_exit(&spa_namespace_lock
);
8252 spa_lookup_by_guid(spa_t
*spa
, uint64_t guid
, boolean_t aux
)
8257 if ((vd
= vdev_lookup_by_guid(spa
->spa_root_vdev
, guid
)) != NULL
)
8261 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
8262 vd
= spa
->spa_l2cache
.sav_vdevs
[i
];
8263 if (vd
->vdev_guid
== guid
)
8267 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
8268 vd
= spa
->spa_spares
.sav_vdevs
[i
];
8269 if (vd
->vdev_guid
== guid
)
8278 spa_upgrade(spa_t
*spa
, uint64_t version
)
8280 ASSERT(spa_writeable(spa
));
8282 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
8285 * This should only be called for a non-faulted pool, and since a
8286 * future version would result in an unopenable pool, this shouldn't be
8289 ASSERT(SPA_VERSION_IS_SUPPORTED(spa
->spa_uberblock
.ub_version
));
8290 ASSERT3U(version
, >=, spa
->spa_uberblock
.ub_version
);
8292 spa
->spa_uberblock
.ub_version
= version
;
8293 vdev_config_dirty(spa
->spa_root_vdev
);
8295 spa_config_exit(spa
, SCL_ALL
, FTAG
);
8297 txg_wait_synced(spa_get_dsl(spa
), 0);
8301 spa_has_spare(spa_t
*spa
, uint64_t guid
)
8305 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
8307 for (i
= 0; i
< sav
->sav_count
; i
++)
8308 if (sav
->sav_vdevs
[i
]->vdev_guid
== guid
)
8311 for (i
= 0; i
< sav
->sav_npending
; i
++) {
8312 if (nvlist_lookup_uint64(sav
->sav_pending
[i
], ZPOOL_CONFIG_GUID
,
8313 &spareguid
) == 0 && spareguid
== guid
)
8321 * Check if a pool has an active shared spare device.
8322 * Note: reference count of an active spare is 2, as a spare and as a replace
8325 spa_has_active_shared_spare(spa_t
*spa
)
8329 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
8331 for (i
= 0; i
< sav
->sav_count
; i
++) {
8332 if (spa_spare_exists(sav
->sav_vdevs
[i
]->vdev_guid
, &pool
,
8333 &refcnt
) && pool
!= 0ULL && pool
== spa_guid(spa
) &&
8342 spa_event_create(spa_t
*spa
, vdev_t
*vd
, nvlist_t
*hist_nvl
, const char *name
)
8344 sysevent_t
*ev
= NULL
;
8348 resource
= zfs_event_create(spa
, vd
, FM_SYSEVENT_CLASS
, name
, hist_nvl
);
8350 ev
= kmem_alloc(sizeof (sysevent_t
), KM_SLEEP
);
8351 ev
->resource
= resource
;
8358 spa_event_post(sysevent_t
*ev
)
8362 zfs_zevent_post(ev
->resource
, NULL
, zfs_zevent_post_cb
);
8363 kmem_free(ev
, sizeof (*ev
));
8369 * Post a zevent corresponding to the given sysevent. The 'name' must be one
8370 * of the event definitions in sys/sysevent/eventdefs.h. The payload will be
8371 * filled in from the spa and (optionally) the vdev. This doesn't do anything
8372 * in the userland libzpool, as we don't want consumers to misinterpret ztest
8373 * or zdb as real changes.
8376 spa_event_notify(spa_t
*spa
, vdev_t
*vd
, nvlist_t
*hist_nvl
, const char *name
)
8378 spa_event_post(spa_event_create(spa
, vd
, hist_nvl
, name
));
8381 #if defined(_KERNEL)
8382 /* state manipulation functions */
8383 EXPORT_SYMBOL(spa_open
);
8384 EXPORT_SYMBOL(spa_open_rewind
);
8385 EXPORT_SYMBOL(spa_get_stats
);
8386 EXPORT_SYMBOL(spa_create
);
8387 EXPORT_SYMBOL(spa_import
);
8388 EXPORT_SYMBOL(spa_tryimport
);
8389 EXPORT_SYMBOL(spa_destroy
);
8390 EXPORT_SYMBOL(spa_export
);
8391 EXPORT_SYMBOL(spa_reset
);
8392 EXPORT_SYMBOL(spa_async_request
);
8393 EXPORT_SYMBOL(spa_async_suspend
);
8394 EXPORT_SYMBOL(spa_async_resume
);
8395 EXPORT_SYMBOL(spa_inject_addref
);
8396 EXPORT_SYMBOL(spa_inject_delref
);
8397 EXPORT_SYMBOL(spa_scan_stat_init
);
8398 EXPORT_SYMBOL(spa_scan_get_stats
);
8400 /* device maniion */
8401 EXPORT_SYMBOL(spa_vdev_add
);
8402 EXPORT_SYMBOL(spa_vdev_attach
);
8403 EXPORT_SYMBOL(spa_vdev_detach
);
8404 EXPORT_SYMBOL(spa_vdev_setpath
);
8405 EXPORT_SYMBOL(spa_vdev_setfru
);
8406 EXPORT_SYMBOL(spa_vdev_split_mirror
);
8408 /* spare statech is global across all pools) */
8409 EXPORT_SYMBOL(spa_spare_add
);
8410 EXPORT_SYMBOL(spa_spare_remove
);
8411 EXPORT_SYMBOL(spa_spare_exists
);
8412 EXPORT_SYMBOL(spa_spare_activate
);
8414 /* L2ARC statech is global across all pools) */
8415 EXPORT_SYMBOL(spa_l2cache_add
);
8416 EXPORT_SYMBOL(spa_l2cache_remove
);
8417 EXPORT_SYMBOL(spa_l2cache_exists
);
8418 EXPORT_SYMBOL(spa_l2cache_activate
);
8419 EXPORT_SYMBOL(spa_l2cache_drop
);
8422 EXPORT_SYMBOL(spa_scan
);
8423 EXPORT_SYMBOL(spa_scan_stop
);
8426 EXPORT_SYMBOL(spa_sync
); /* only for DMU use */
8427 EXPORT_SYMBOL(spa_sync_allpools
);
8430 EXPORT_SYMBOL(spa_prop_set
);
8431 EXPORT_SYMBOL(spa_prop_get
);
8432 EXPORT_SYMBOL(spa_prop_clear_bootfs
);
8434 /* asynchronous event notification */
8435 EXPORT_SYMBOL(spa_event_notify
);
8438 #if defined(_KERNEL)
8439 module_param(spa_load_verify_maxinflight
, int, 0644);
8440 MODULE_PARM_DESC(spa_load_verify_maxinflight
,
8441 "Max concurrent traversal I/Os while verifying pool during import -X");
8443 module_param(spa_load_verify_metadata
, int, 0644);
8444 MODULE_PARM_DESC(spa_load_verify_metadata
,
8445 "Set to traverse metadata on pool import");
8447 module_param(spa_load_verify_data
, int, 0644);
8448 MODULE_PARM_DESC(spa_load_verify_data
,
8449 "Set to traverse data on pool import");
8451 module_param(spa_load_print_vdev_tree
, int, 0644);
8452 MODULE_PARM_DESC(spa_load_print_vdev_tree
,
8453 "Print vdev tree to zfs_dbgmsg during pool import");
8456 module_param(zio_taskq_batch_pct
, uint
, 0444);
8457 MODULE_PARM_DESC(zio_taskq_batch_pct
,
8458 "Percentage of CPUs to run an IO worker thread");
8461 module_param(zfs_max_missing_tvds
, ulong
, 0644);
8462 MODULE_PARM_DESC(zfs_max_missing_tvds
,
8463 "Allow importing pool with up to this number of missing top-level vdevs"
8464 " (in read-only mode)");