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_trim.h>
61 #include <sys/vdev_disk.h>
62 #include <sys/metaslab.h>
63 #include <sys/metaslab_impl.h>
65 #include <sys/uberblock_impl.h>
68 #include <sys/bpobj.h>
69 #include <sys/dmu_traverse.h>
70 #include <sys/dmu_objset.h>
71 #include <sys/unique.h>
72 #include <sys/dsl_pool.h>
73 #include <sys/dsl_dataset.h>
74 #include <sys/dsl_dir.h>
75 #include <sys/dsl_prop.h>
76 #include <sys/dsl_synctask.h>
77 #include <sys/fs/zfs.h>
79 #include <sys/callb.h>
80 #include <sys/systeminfo.h>
81 #include <sys/spa_boot.h>
82 #include <sys/zfs_ioctl.h>
83 #include <sys/dsl_scan.h>
84 #include <sys/zfeature.h>
85 #include <sys/dsl_destroy.h>
89 #include <sys/fm/protocol.h>
90 #include <sys/fm/util.h>
91 #include <sys/callb.h>
96 #include "zfs_comutil.h"
99 * The interval, in seconds, at which failed configuration cache file writes
102 int zfs_ccw_retry_interval
= 300;
104 typedef enum zti_modes
{
105 ZTI_MODE_FIXED
, /* value is # of threads (min 1) */
106 ZTI_MODE_BATCH
, /* cpu-intensive; value is ignored */
107 ZTI_MODE_NULL
, /* don't create a taskq */
111 #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) }
112 #define ZTI_PCT(n) { ZTI_MODE_ONLINE_PERCENT, (n), 1 }
113 #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 }
114 #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 }
116 #define ZTI_N(n) ZTI_P(n, 1)
117 #define ZTI_ONE ZTI_N(1)
119 typedef struct zio_taskq_info
{
120 zti_modes_t zti_mode
;
125 static const char *const zio_taskq_types
[ZIO_TASKQ_TYPES
] = {
126 "iss", "iss_h", "int", "int_h"
130 * This table defines the taskq settings for each ZFS I/O type. When
131 * initializing a pool, we use this table to create an appropriately sized
132 * taskq. Some operations are low volume and therefore have a small, static
133 * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
134 * macros. Other operations process a large amount of data; the ZTI_BATCH
135 * macro causes us to create a taskq oriented for throughput. Some operations
136 * are so high frequency and short-lived that the taskq itself can become a
137 * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
138 * additional degree of parallelism specified by the number of threads per-
139 * taskq and the number of taskqs; when dispatching an event in this case, the
140 * particular taskq is chosen at random.
142 * The different taskq priorities are to handle the different contexts (issue
143 * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that
144 * need to be handled with minimum delay.
146 const zio_taskq_info_t zio_taskqs
[ZIO_TYPES
][ZIO_TASKQ_TYPES
] = {
147 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
148 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* NULL */
149 { ZTI_N(8), ZTI_NULL
, ZTI_P(12, 8), ZTI_NULL
}, /* READ */
150 { ZTI_BATCH
, ZTI_N(5), ZTI_P(12, 8), ZTI_N(5) }, /* WRITE */
151 { ZTI_P(12, 8), ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* FREE */
152 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* CLAIM */
153 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* IOCTL */
154 { ZTI_N(4), ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* TRIM */
157 static void spa_sync_version(void *arg
, dmu_tx_t
*tx
);
158 static void spa_sync_props(void *arg
, dmu_tx_t
*tx
);
159 static boolean_t
spa_has_active_shared_spare(spa_t
*spa
);
160 static int spa_load_impl(spa_t
*spa
, spa_import_type_t type
, char **ereport
);
161 static void spa_vdev_resilver_done(spa_t
*spa
);
163 uint_t zio_taskq_batch_pct
= 75; /* 1 thread per cpu in pset */
164 boolean_t zio_taskq_sysdc
= B_TRUE
; /* use SDC scheduling class */
165 uint_t zio_taskq_basedc
= 80; /* base duty cycle */
167 boolean_t spa_create_process
= B_TRUE
; /* no process ==> no sysdc */
170 * Report any spa_load_verify errors found, but do not fail spa_load.
171 * This is used by zdb to analyze non-idle pools.
173 boolean_t spa_load_verify_dryrun
= B_FALSE
;
176 * This (illegal) pool name is used when temporarily importing a spa_t in order
177 * to get the vdev stats associated with the imported devices.
179 #define TRYIMPORT_NAME "$import"
182 * For debugging purposes: print out vdev tree during pool import.
184 int spa_load_print_vdev_tree
= B_FALSE
;
187 * A non-zero value for zfs_max_missing_tvds means that we allow importing
188 * pools with missing top-level vdevs. This is strictly intended for advanced
189 * pool recovery cases since missing data is almost inevitable. Pools with
190 * missing devices can only be imported read-only for safety reasons, and their
191 * fail-mode will be automatically set to "continue".
193 * With 1 missing vdev we should be able to import the pool and mount all
194 * datasets. User data that was not modified after the missing device has been
195 * added should be recoverable. This means that snapshots created prior to the
196 * addition of that device should be completely intact.
198 * With 2 missing vdevs, some datasets may fail to mount since there are
199 * dataset statistics that are stored as regular metadata. Some data might be
200 * recoverable if those vdevs were added recently.
202 * With 3 or more missing vdevs, the pool is severely damaged and MOS entries
203 * may be missing entirely. Chances of data recovery are very low. Note that
204 * there are also risks of performing an inadvertent rewind as we might be
205 * missing all the vdevs with the latest uberblocks.
207 unsigned long zfs_max_missing_tvds
= 0;
210 * The parameters below are similar to zfs_max_missing_tvds but are only
211 * intended for a preliminary open of the pool with an untrusted config which
212 * might be incomplete or out-dated.
214 * We are more tolerant for pools opened from a cachefile since we could have
215 * an out-dated cachefile where a device removal was not registered.
216 * We could have set the limit arbitrarily high but in the case where devices
217 * are really missing we would want to return the proper error codes; we chose
218 * SPA_DVAS_PER_BP - 1 so that some copies of the MOS would still be available
219 * and we get a chance to retrieve the trusted config.
221 uint64_t zfs_max_missing_tvds_cachefile
= SPA_DVAS_PER_BP
- 1;
224 * In the case where config was assembled by scanning device paths (/dev/dsks
225 * by default) we are less tolerant since all the existing devices should have
226 * been detected and we want spa_load to return the right error codes.
228 uint64_t zfs_max_missing_tvds_scan
= 0;
231 * Debugging aid that pauses spa_sync() towards the end.
233 boolean_t zfs_pause_spa_sync
= B_FALSE
;
236 * ==========================================================================
237 * SPA properties routines
238 * ==========================================================================
242 * Add a (source=src, propname=propval) list to an nvlist.
245 spa_prop_add_list(nvlist_t
*nvl
, zpool_prop_t prop
, char *strval
,
246 uint64_t intval
, zprop_source_t src
)
248 const char *propname
= zpool_prop_to_name(prop
);
251 VERIFY(nvlist_alloc(&propval
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
252 VERIFY(nvlist_add_uint64(propval
, ZPROP_SOURCE
, src
) == 0);
255 VERIFY(nvlist_add_string(propval
, ZPROP_VALUE
, strval
) == 0);
257 VERIFY(nvlist_add_uint64(propval
, ZPROP_VALUE
, intval
) == 0);
259 VERIFY(nvlist_add_nvlist(nvl
, propname
, propval
) == 0);
260 nvlist_free(propval
);
264 * Get property values from the spa configuration.
267 spa_prop_get_config(spa_t
*spa
, nvlist_t
**nvp
)
269 vdev_t
*rvd
= spa
->spa_root_vdev
;
270 dsl_pool_t
*pool
= spa
->spa_dsl_pool
;
271 uint64_t size
, alloc
, cap
, version
;
272 const zprop_source_t src
= ZPROP_SRC_NONE
;
273 spa_config_dirent_t
*dp
;
274 metaslab_class_t
*mc
= spa_normal_class(spa
);
276 ASSERT(MUTEX_HELD(&spa
->spa_props_lock
));
279 alloc
= metaslab_class_get_alloc(mc
);
280 alloc
+= metaslab_class_get_alloc(spa_special_class(spa
));
281 alloc
+= metaslab_class_get_alloc(spa_dedup_class(spa
));
283 size
= metaslab_class_get_space(mc
);
284 size
+= metaslab_class_get_space(spa_special_class(spa
));
285 size
+= metaslab_class_get_space(spa_dedup_class(spa
));
287 spa_prop_add_list(*nvp
, ZPOOL_PROP_NAME
, spa_name(spa
), 0, src
);
288 spa_prop_add_list(*nvp
, ZPOOL_PROP_SIZE
, NULL
, size
, src
);
289 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALLOCATED
, NULL
, alloc
, src
);
290 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREE
, NULL
,
292 spa_prop_add_list(*nvp
, ZPOOL_PROP_CHECKPOINT
, NULL
,
293 spa
->spa_checkpoint_info
.sci_dspace
, src
);
295 spa_prop_add_list(*nvp
, ZPOOL_PROP_FRAGMENTATION
, NULL
,
296 metaslab_class_fragmentation(mc
), src
);
297 spa_prop_add_list(*nvp
, ZPOOL_PROP_EXPANDSZ
, NULL
,
298 metaslab_class_expandable_space(mc
), src
);
299 spa_prop_add_list(*nvp
, ZPOOL_PROP_READONLY
, NULL
,
300 (spa_mode(spa
) == FREAD
), src
);
302 cap
= (size
== 0) ? 0 : (alloc
* 100 / size
);
303 spa_prop_add_list(*nvp
, ZPOOL_PROP_CAPACITY
, NULL
, cap
, src
);
305 spa_prop_add_list(*nvp
, ZPOOL_PROP_DEDUPRATIO
, NULL
,
306 ddt_get_pool_dedup_ratio(spa
), src
);
308 spa_prop_add_list(*nvp
, ZPOOL_PROP_HEALTH
, NULL
,
309 rvd
->vdev_state
, src
);
311 version
= spa_version(spa
);
312 if (version
== zpool_prop_default_numeric(ZPOOL_PROP_VERSION
)) {
313 spa_prop_add_list(*nvp
, ZPOOL_PROP_VERSION
, NULL
,
314 version
, ZPROP_SRC_DEFAULT
);
316 spa_prop_add_list(*nvp
, ZPOOL_PROP_VERSION
, NULL
,
317 version
, ZPROP_SRC_LOCAL
);
319 spa_prop_add_list(*nvp
, ZPOOL_PROP_LOAD_GUID
,
320 NULL
, spa_load_guid(spa
), src
);
325 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
326 * when opening pools before this version freedir will be NULL.
328 if (pool
->dp_free_dir
!= NULL
) {
329 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
, NULL
,
330 dsl_dir_phys(pool
->dp_free_dir
)->dd_used_bytes
,
333 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
,
337 if (pool
->dp_leak_dir
!= NULL
) {
338 spa_prop_add_list(*nvp
, ZPOOL_PROP_LEAKED
, NULL
,
339 dsl_dir_phys(pool
->dp_leak_dir
)->dd_used_bytes
,
342 spa_prop_add_list(*nvp
, ZPOOL_PROP_LEAKED
,
347 spa_prop_add_list(*nvp
, ZPOOL_PROP_GUID
, NULL
, spa_guid(spa
), src
);
349 if (spa
->spa_comment
!= NULL
) {
350 spa_prop_add_list(*nvp
, ZPOOL_PROP_COMMENT
, spa
->spa_comment
,
354 if (spa
->spa_root
!= NULL
)
355 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALTROOT
, spa
->spa_root
,
358 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_BLOCKS
)) {
359 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXBLOCKSIZE
, NULL
,
360 MIN(zfs_max_recordsize
, SPA_MAXBLOCKSIZE
), ZPROP_SRC_NONE
);
362 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXBLOCKSIZE
, NULL
,
363 SPA_OLD_MAXBLOCKSIZE
, ZPROP_SRC_NONE
);
366 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_DNODE
)) {
367 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXDNODESIZE
, NULL
,
368 DNODE_MAX_SIZE
, ZPROP_SRC_NONE
);
370 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXDNODESIZE
, NULL
,
371 DNODE_MIN_SIZE
, ZPROP_SRC_NONE
);
374 if ((dp
= list_head(&spa
->spa_config_list
)) != NULL
) {
375 if (dp
->scd_path
== NULL
) {
376 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
377 "none", 0, ZPROP_SRC_LOCAL
);
378 } else if (strcmp(dp
->scd_path
, spa_config_path
) != 0) {
379 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
380 dp
->scd_path
, 0, ZPROP_SRC_LOCAL
);
386 * Get zpool property values.
389 spa_prop_get(spa_t
*spa
, nvlist_t
**nvp
)
391 objset_t
*mos
= spa
->spa_meta_objset
;
396 err
= nvlist_alloc(nvp
, NV_UNIQUE_NAME
, KM_SLEEP
);
400 mutex_enter(&spa
->spa_props_lock
);
403 * Get properties from the spa config.
405 spa_prop_get_config(spa
, nvp
);
407 /* If no pool property object, no more prop to get. */
408 if (mos
== NULL
|| spa
->spa_pool_props_object
== 0) {
409 mutex_exit(&spa
->spa_props_lock
);
414 * Get properties from the MOS pool property object.
416 for (zap_cursor_init(&zc
, mos
, spa
->spa_pool_props_object
);
417 (err
= zap_cursor_retrieve(&zc
, &za
)) == 0;
418 zap_cursor_advance(&zc
)) {
421 zprop_source_t src
= ZPROP_SRC_DEFAULT
;
424 if ((prop
= zpool_name_to_prop(za
.za_name
)) == ZPOOL_PROP_INVAL
)
427 switch (za
.za_integer_length
) {
429 /* integer property */
430 if (za
.za_first_integer
!=
431 zpool_prop_default_numeric(prop
))
432 src
= ZPROP_SRC_LOCAL
;
434 if (prop
== ZPOOL_PROP_BOOTFS
) {
436 dsl_dataset_t
*ds
= NULL
;
438 dp
= spa_get_dsl(spa
);
439 dsl_pool_config_enter(dp
, FTAG
);
440 err
= dsl_dataset_hold_obj(dp
,
441 za
.za_first_integer
, FTAG
, &ds
);
443 dsl_pool_config_exit(dp
, FTAG
);
447 strval
= kmem_alloc(ZFS_MAX_DATASET_NAME_LEN
,
449 dsl_dataset_name(ds
, strval
);
450 dsl_dataset_rele(ds
, FTAG
);
451 dsl_pool_config_exit(dp
, FTAG
);
454 intval
= za
.za_first_integer
;
457 spa_prop_add_list(*nvp
, prop
, strval
, intval
, src
);
460 kmem_free(strval
, ZFS_MAX_DATASET_NAME_LEN
);
465 /* string property */
466 strval
= kmem_alloc(za
.za_num_integers
, KM_SLEEP
);
467 err
= zap_lookup(mos
, spa
->spa_pool_props_object
,
468 za
.za_name
, 1, za
.za_num_integers
, strval
);
470 kmem_free(strval
, za
.za_num_integers
);
473 spa_prop_add_list(*nvp
, prop
, strval
, 0, src
);
474 kmem_free(strval
, za
.za_num_integers
);
481 zap_cursor_fini(&zc
);
482 mutex_exit(&spa
->spa_props_lock
);
484 if (err
&& err
!= ENOENT
) {
494 * Validate the given pool properties nvlist and modify the list
495 * for the property values to be set.
498 spa_prop_validate(spa_t
*spa
, nvlist_t
*props
)
501 int error
= 0, reset_bootfs
= 0;
503 boolean_t has_feature
= B_FALSE
;
506 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
508 char *strval
, *slash
, *check
, *fname
;
509 const char *propname
= nvpair_name(elem
);
510 zpool_prop_t prop
= zpool_name_to_prop(propname
);
513 case ZPOOL_PROP_INVAL
:
514 if (!zpool_prop_feature(propname
)) {
515 error
= SET_ERROR(EINVAL
);
520 * Sanitize the input.
522 if (nvpair_type(elem
) != DATA_TYPE_UINT64
) {
523 error
= SET_ERROR(EINVAL
);
527 if (nvpair_value_uint64(elem
, &intval
) != 0) {
528 error
= SET_ERROR(EINVAL
);
533 error
= SET_ERROR(EINVAL
);
537 fname
= strchr(propname
, '@') + 1;
538 if (zfeature_lookup_name(fname
, NULL
) != 0) {
539 error
= SET_ERROR(EINVAL
);
543 has_feature
= B_TRUE
;
546 case ZPOOL_PROP_VERSION
:
547 error
= nvpair_value_uint64(elem
, &intval
);
549 (intval
< spa_version(spa
) ||
550 intval
> SPA_VERSION_BEFORE_FEATURES
||
552 error
= SET_ERROR(EINVAL
);
555 case ZPOOL_PROP_DELEGATION
:
556 case ZPOOL_PROP_AUTOREPLACE
:
557 case ZPOOL_PROP_LISTSNAPS
:
558 case ZPOOL_PROP_AUTOEXPAND
:
559 case ZPOOL_PROP_AUTOTRIM
:
560 error
= nvpair_value_uint64(elem
, &intval
);
561 if (!error
&& intval
> 1)
562 error
= SET_ERROR(EINVAL
);
565 case ZPOOL_PROP_MULTIHOST
:
566 error
= nvpair_value_uint64(elem
, &intval
);
567 if (!error
&& intval
> 1)
568 error
= SET_ERROR(EINVAL
);
570 if (!error
&& !spa_get_hostid())
571 error
= SET_ERROR(ENOTSUP
);
575 case ZPOOL_PROP_BOOTFS
:
577 * If the pool version is less than SPA_VERSION_BOOTFS,
578 * or the pool is still being created (version == 0),
579 * the bootfs property cannot be set.
581 if (spa_version(spa
) < SPA_VERSION_BOOTFS
) {
582 error
= SET_ERROR(ENOTSUP
);
587 * Make sure the vdev config is bootable
589 if (!vdev_is_bootable(spa
->spa_root_vdev
)) {
590 error
= SET_ERROR(ENOTSUP
);
596 error
= nvpair_value_string(elem
, &strval
);
602 if (strval
== NULL
|| strval
[0] == '\0') {
603 objnum
= zpool_prop_default_numeric(
608 error
= dmu_objset_hold(strval
, FTAG
, &os
);
613 * Must be ZPL, and its property settings
614 * must be supported by GRUB (compression
615 * is not gzip, and large blocks or large
616 * dnodes are not used).
619 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
620 error
= SET_ERROR(ENOTSUP
);
622 dsl_prop_get_int_ds(dmu_objset_ds(os
),
623 zfs_prop_to_name(ZFS_PROP_COMPRESSION
),
625 !BOOTFS_COMPRESS_VALID(propval
)) {
626 error
= SET_ERROR(ENOTSUP
);
628 dsl_prop_get_int_ds(dmu_objset_ds(os
),
629 zfs_prop_to_name(ZFS_PROP_DNODESIZE
),
631 propval
!= ZFS_DNSIZE_LEGACY
) {
632 error
= SET_ERROR(ENOTSUP
);
634 objnum
= dmu_objset_id(os
);
636 dmu_objset_rele(os
, FTAG
);
640 case ZPOOL_PROP_FAILUREMODE
:
641 error
= nvpair_value_uint64(elem
, &intval
);
642 if (!error
&& intval
> ZIO_FAILURE_MODE_PANIC
)
643 error
= SET_ERROR(EINVAL
);
646 * This is a special case which only occurs when
647 * the pool has completely failed. This allows
648 * the user to change the in-core failmode property
649 * without syncing it out to disk (I/Os might
650 * currently be blocked). We do this by returning
651 * EIO to the caller (spa_prop_set) to trick it
652 * into thinking we encountered a property validation
655 if (!error
&& spa_suspended(spa
)) {
656 spa
->spa_failmode
= intval
;
657 error
= SET_ERROR(EIO
);
661 case ZPOOL_PROP_CACHEFILE
:
662 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
665 if (strval
[0] == '\0')
668 if (strcmp(strval
, "none") == 0)
671 if (strval
[0] != '/') {
672 error
= SET_ERROR(EINVAL
);
676 slash
= strrchr(strval
, '/');
677 ASSERT(slash
!= NULL
);
679 if (slash
[1] == '\0' || strcmp(slash
, "/.") == 0 ||
680 strcmp(slash
, "/..") == 0)
681 error
= SET_ERROR(EINVAL
);
684 case ZPOOL_PROP_COMMENT
:
685 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
687 for (check
= strval
; *check
!= '\0'; check
++) {
688 if (!isprint(*check
)) {
689 error
= SET_ERROR(EINVAL
);
693 if (strlen(strval
) > ZPROP_MAX_COMMENT
)
694 error
= SET_ERROR(E2BIG
);
697 case ZPOOL_PROP_DEDUPDITTO
:
698 if (spa_version(spa
) < SPA_VERSION_DEDUP
)
699 error
= SET_ERROR(ENOTSUP
);
701 error
= nvpair_value_uint64(elem
, &intval
);
703 intval
!= 0 && intval
< ZIO_DEDUPDITTO_MIN
)
704 error
= SET_ERROR(EINVAL
);
715 if (!error
&& reset_bootfs
) {
716 error
= nvlist_remove(props
,
717 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), DATA_TYPE_STRING
);
720 error
= nvlist_add_uint64(props
,
721 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), objnum
);
729 spa_configfile_set(spa_t
*spa
, nvlist_t
*nvp
, boolean_t need_sync
)
732 spa_config_dirent_t
*dp
;
734 if (nvlist_lookup_string(nvp
, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
),
738 dp
= kmem_alloc(sizeof (spa_config_dirent_t
),
741 if (cachefile
[0] == '\0')
742 dp
->scd_path
= spa_strdup(spa_config_path
);
743 else if (strcmp(cachefile
, "none") == 0)
746 dp
->scd_path
= spa_strdup(cachefile
);
748 list_insert_head(&spa
->spa_config_list
, dp
);
750 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
754 spa_prop_set(spa_t
*spa
, nvlist_t
*nvp
)
757 nvpair_t
*elem
= NULL
;
758 boolean_t need_sync
= B_FALSE
;
760 if ((error
= spa_prop_validate(spa
, nvp
)) != 0)
763 while ((elem
= nvlist_next_nvpair(nvp
, elem
)) != NULL
) {
764 zpool_prop_t prop
= zpool_name_to_prop(nvpair_name(elem
));
766 if (prop
== ZPOOL_PROP_CACHEFILE
||
767 prop
== ZPOOL_PROP_ALTROOT
||
768 prop
== ZPOOL_PROP_READONLY
)
771 if (prop
== ZPOOL_PROP_VERSION
|| prop
== ZPOOL_PROP_INVAL
) {
774 if (prop
== ZPOOL_PROP_VERSION
) {
775 VERIFY(nvpair_value_uint64(elem
, &ver
) == 0);
777 ASSERT(zpool_prop_feature(nvpair_name(elem
)));
778 ver
= SPA_VERSION_FEATURES
;
782 /* Save time if the version is already set. */
783 if (ver
== spa_version(spa
))
787 * In addition to the pool directory object, we might
788 * create the pool properties object, the features for
789 * read object, the features for write object, or the
790 * feature descriptions object.
792 error
= dsl_sync_task(spa
->spa_name
, NULL
,
793 spa_sync_version
, &ver
,
794 6, ZFS_SPACE_CHECK_RESERVED
);
805 return (dsl_sync_task(spa
->spa_name
, NULL
, spa_sync_props
,
806 nvp
, 6, ZFS_SPACE_CHECK_RESERVED
));
813 * If the bootfs property value is dsobj, clear it.
816 spa_prop_clear_bootfs(spa_t
*spa
, uint64_t dsobj
, dmu_tx_t
*tx
)
818 if (spa
->spa_bootfs
== dsobj
&& spa
->spa_pool_props_object
!= 0) {
819 VERIFY(zap_remove(spa
->spa_meta_objset
,
820 spa
->spa_pool_props_object
,
821 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), tx
) == 0);
828 spa_change_guid_check(void *arg
, dmu_tx_t
*tx
)
830 ASSERTV(uint64_t *newguid
= arg
);
831 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
832 vdev_t
*rvd
= spa
->spa_root_vdev
;
835 if (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
)) {
836 int error
= (spa_has_checkpoint(spa
)) ?
837 ZFS_ERR_CHECKPOINT_EXISTS
: ZFS_ERR_DISCARDING_CHECKPOINT
;
838 return (SET_ERROR(error
));
841 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
842 vdev_state
= rvd
->vdev_state
;
843 spa_config_exit(spa
, SCL_STATE
, FTAG
);
845 if (vdev_state
!= VDEV_STATE_HEALTHY
)
846 return (SET_ERROR(ENXIO
));
848 ASSERT3U(spa_guid(spa
), !=, *newguid
);
854 spa_change_guid_sync(void *arg
, dmu_tx_t
*tx
)
856 uint64_t *newguid
= arg
;
857 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
859 vdev_t
*rvd
= spa
->spa_root_vdev
;
861 oldguid
= spa_guid(spa
);
863 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
864 rvd
->vdev_guid
= *newguid
;
865 rvd
->vdev_guid_sum
+= (*newguid
- oldguid
);
866 vdev_config_dirty(rvd
);
867 spa_config_exit(spa
, SCL_STATE
, FTAG
);
869 spa_history_log_internal(spa
, "guid change", tx
, "old=%llu new=%llu",
874 * Change the GUID for the pool. This is done so that we can later
875 * re-import a pool built from a clone of our own vdevs. We will modify
876 * the root vdev's guid, our own pool guid, and then mark all of our
877 * vdevs dirty. Note that we must make sure that all our vdevs are
878 * online when we do this, or else any vdevs that weren't present
879 * would be orphaned from our pool. We are also going to issue a
880 * sysevent to update any watchers.
883 spa_change_guid(spa_t
*spa
)
888 mutex_enter(&spa
->spa_vdev_top_lock
);
889 mutex_enter(&spa_namespace_lock
);
890 guid
= spa_generate_guid(NULL
);
892 error
= dsl_sync_task(spa
->spa_name
, spa_change_guid_check
,
893 spa_change_guid_sync
, &guid
, 5, ZFS_SPACE_CHECK_RESERVED
);
896 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
);
897 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_REGUID
);
900 mutex_exit(&spa_namespace_lock
);
901 mutex_exit(&spa
->spa_vdev_top_lock
);
907 * ==========================================================================
908 * SPA state manipulation (open/create/destroy/import/export)
909 * ==========================================================================
913 spa_error_entry_compare(const void *a
, const void *b
)
915 const spa_error_entry_t
*sa
= (const spa_error_entry_t
*)a
;
916 const spa_error_entry_t
*sb
= (const spa_error_entry_t
*)b
;
919 ret
= memcmp(&sa
->se_bookmark
, &sb
->se_bookmark
,
920 sizeof (zbookmark_phys_t
));
922 return (AVL_ISIGN(ret
));
926 * Utility function which retrieves copies of the current logs and
927 * re-initializes them in the process.
930 spa_get_errlists(spa_t
*spa
, avl_tree_t
*last
, avl_tree_t
*scrub
)
932 ASSERT(MUTEX_HELD(&spa
->spa_errlist_lock
));
934 bcopy(&spa
->spa_errlist_last
, last
, sizeof (avl_tree_t
));
935 bcopy(&spa
->spa_errlist_scrub
, scrub
, sizeof (avl_tree_t
));
937 avl_create(&spa
->spa_errlist_scrub
,
938 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
939 offsetof(spa_error_entry_t
, se_avl
));
940 avl_create(&spa
->spa_errlist_last
,
941 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
942 offsetof(spa_error_entry_t
, se_avl
));
946 spa_taskqs_init(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
948 const zio_taskq_info_t
*ztip
= &zio_taskqs
[t
][q
];
949 enum zti_modes mode
= ztip
->zti_mode
;
950 uint_t value
= ztip
->zti_value
;
951 uint_t count
= ztip
->zti_count
;
952 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
954 boolean_t batch
= B_FALSE
;
956 if (mode
== ZTI_MODE_NULL
) {
958 tqs
->stqs_taskq
= NULL
;
962 ASSERT3U(count
, >, 0);
964 tqs
->stqs_count
= count
;
965 tqs
->stqs_taskq
= kmem_alloc(count
* sizeof (taskq_t
*), KM_SLEEP
);
969 ASSERT3U(value
, >=, 1);
970 value
= MAX(value
, 1);
971 flags
|= TASKQ_DYNAMIC
;
976 flags
|= TASKQ_THREADS_CPU_PCT
;
977 value
= MIN(zio_taskq_batch_pct
, 100);
981 panic("unrecognized mode for %s_%s taskq (%u:%u) in "
983 zio_type_name
[t
], zio_taskq_types
[q
], mode
, value
);
987 for (uint_t i
= 0; i
< count
; i
++) {
991 (void) snprintf(name
, sizeof (name
), "%s_%s",
992 zio_type_name
[t
], zio_taskq_types
[q
]);
994 if (zio_taskq_sysdc
&& spa
->spa_proc
!= &p0
) {
996 flags
|= TASKQ_DC_BATCH
;
998 tq
= taskq_create_sysdc(name
, value
, 50, INT_MAX
,
999 spa
->spa_proc
, zio_taskq_basedc
, flags
);
1001 pri_t pri
= maxclsyspri
;
1003 * The write issue taskq can be extremely CPU
1004 * intensive. Run it at slightly less important
1005 * priority than the other taskqs. Under Linux this
1006 * means incrementing the priority value on platforms
1007 * like illumos it should be decremented.
1009 if (t
== ZIO_TYPE_WRITE
&& q
== ZIO_TASKQ_ISSUE
)
1012 tq
= taskq_create_proc(name
, value
, pri
, 50,
1013 INT_MAX
, spa
->spa_proc
, flags
);
1016 tqs
->stqs_taskq
[i
] = tq
;
1021 spa_taskqs_fini(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
1023 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1025 if (tqs
->stqs_taskq
== NULL
) {
1026 ASSERT3U(tqs
->stqs_count
, ==, 0);
1030 for (uint_t i
= 0; i
< tqs
->stqs_count
; i
++) {
1031 ASSERT3P(tqs
->stqs_taskq
[i
], !=, NULL
);
1032 taskq_destroy(tqs
->stqs_taskq
[i
]);
1035 kmem_free(tqs
->stqs_taskq
, tqs
->stqs_count
* sizeof (taskq_t
*));
1036 tqs
->stqs_taskq
= NULL
;
1040 * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority.
1041 * Note that a type may have multiple discrete taskqs to avoid lock contention
1042 * on the taskq itself. In that case we choose which taskq at random by using
1043 * the low bits of gethrtime().
1046 spa_taskq_dispatch_ent(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
1047 task_func_t
*func
, void *arg
, uint_t flags
, taskq_ent_t
*ent
)
1049 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1052 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
1053 ASSERT3U(tqs
->stqs_count
, !=, 0);
1055 if (tqs
->stqs_count
== 1) {
1056 tq
= tqs
->stqs_taskq
[0];
1058 tq
= tqs
->stqs_taskq
[((uint64_t)gethrtime()) % tqs
->stqs_count
];
1061 taskq_dispatch_ent(tq
, func
, arg
, flags
, ent
);
1065 * Same as spa_taskq_dispatch_ent() but block on the task until completion.
1068 spa_taskq_dispatch_sync(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
1069 task_func_t
*func
, void *arg
, uint_t flags
)
1071 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1075 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
1076 ASSERT3U(tqs
->stqs_count
, !=, 0);
1078 if (tqs
->stqs_count
== 1) {
1079 tq
= tqs
->stqs_taskq
[0];
1081 tq
= tqs
->stqs_taskq
[((uint64_t)gethrtime()) % tqs
->stqs_count
];
1084 id
= taskq_dispatch(tq
, func
, arg
, flags
);
1086 taskq_wait_id(tq
, id
);
1090 spa_create_zio_taskqs(spa_t
*spa
)
1092 for (int t
= 0; t
< ZIO_TYPES
; t
++) {
1093 for (int q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1094 spa_taskqs_init(spa
, t
, q
);
1100 * Disabled until spa_thread() can be adapted for Linux.
1102 #undef HAVE_SPA_THREAD
1104 #if defined(_KERNEL) && defined(HAVE_SPA_THREAD)
1106 spa_thread(void *arg
)
1108 psetid_t zio_taskq_psrset_bind
= PS_NONE
;
1109 callb_cpr_t cprinfo
;
1112 user_t
*pu
= PTOU(curproc
);
1114 CALLB_CPR_INIT(&cprinfo
, &spa
->spa_proc_lock
, callb_generic_cpr
,
1117 ASSERT(curproc
!= &p0
);
1118 (void) snprintf(pu
->u_psargs
, sizeof (pu
->u_psargs
),
1119 "zpool-%s", spa
->spa_name
);
1120 (void) strlcpy(pu
->u_comm
, pu
->u_psargs
, sizeof (pu
->u_comm
));
1122 /* bind this thread to the requested psrset */
1123 if (zio_taskq_psrset_bind
!= PS_NONE
) {
1125 mutex_enter(&cpu_lock
);
1126 mutex_enter(&pidlock
);
1127 mutex_enter(&curproc
->p_lock
);
1129 if (cpupart_bind_thread(curthread
, zio_taskq_psrset_bind
,
1130 0, NULL
, NULL
) == 0) {
1131 curthread
->t_bind_pset
= zio_taskq_psrset_bind
;
1134 "Couldn't bind process for zfs pool \"%s\" to "
1135 "pset %d\n", spa
->spa_name
, zio_taskq_psrset_bind
);
1138 mutex_exit(&curproc
->p_lock
);
1139 mutex_exit(&pidlock
);
1140 mutex_exit(&cpu_lock
);
1144 if (zio_taskq_sysdc
) {
1145 sysdc_thread_enter(curthread
, 100, 0);
1148 spa
->spa_proc
= curproc
;
1149 spa
->spa_did
= curthread
->t_did
;
1151 spa_create_zio_taskqs(spa
);
1153 mutex_enter(&spa
->spa_proc_lock
);
1154 ASSERT(spa
->spa_proc_state
== SPA_PROC_CREATED
);
1156 spa
->spa_proc_state
= SPA_PROC_ACTIVE
;
1157 cv_broadcast(&spa
->spa_proc_cv
);
1159 CALLB_CPR_SAFE_BEGIN(&cprinfo
);
1160 while (spa
->spa_proc_state
== SPA_PROC_ACTIVE
)
1161 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1162 CALLB_CPR_SAFE_END(&cprinfo
, &spa
->spa_proc_lock
);
1164 ASSERT(spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
);
1165 spa
->spa_proc_state
= SPA_PROC_GONE
;
1166 spa
->spa_proc
= &p0
;
1167 cv_broadcast(&spa
->spa_proc_cv
);
1168 CALLB_CPR_EXIT(&cprinfo
); /* drops spa_proc_lock */
1170 mutex_enter(&curproc
->p_lock
);
1176 * Activate an uninitialized pool.
1179 spa_activate(spa_t
*spa
, int mode
)
1181 ASSERT(spa
->spa_state
== POOL_STATE_UNINITIALIZED
);
1183 spa
->spa_state
= POOL_STATE_ACTIVE
;
1184 spa
->spa_mode
= mode
;
1186 spa
->spa_normal_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
1187 spa
->spa_log_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
1188 spa
->spa_special_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
1189 spa
->spa_dedup_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
1191 /* Try to create a covering process */
1192 mutex_enter(&spa
->spa_proc_lock
);
1193 ASSERT(spa
->spa_proc_state
== SPA_PROC_NONE
);
1194 ASSERT(spa
->spa_proc
== &p0
);
1197 #ifdef HAVE_SPA_THREAD
1198 /* Only create a process if we're going to be around a while. */
1199 if (spa_create_process
&& strcmp(spa
->spa_name
, TRYIMPORT_NAME
) != 0) {
1200 if (newproc(spa_thread
, (caddr_t
)spa
, syscid
, maxclsyspri
,
1202 spa
->spa_proc_state
= SPA_PROC_CREATED
;
1203 while (spa
->spa_proc_state
== SPA_PROC_CREATED
) {
1204 cv_wait(&spa
->spa_proc_cv
,
1205 &spa
->spa_proc_lock
);
1207 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1208 ASSERT(spa
->spa_proc
!= &p0
);
1209 ASSERT(spa
->spa_did
!= 0);
1213 "Couldn't create process for zfs pool \"%s\"\n",
1218 #endif /* HAVE_SPA_THREAD */
1219 mutex_exit(&spa
->spa_proc_lock
);
1221 /* If we didn't create a process, we need to create our taskqs. */
1222 if (spa
->spa_proc
== &p0
) {
1223 spa_create_zio_taskqs(spa
);
1226 for (size_t i
= 0; i
< TXG_SIZE
; i
++) {
1227 spa
->spa_txg_zio
[i
] = zio_root(spa
, NULL
, NULL
,
1231 list_create(&spa
->spa_config_dirty_list
, sizeof (vdev_t
),
1232 offsetof(vdev_t
, vdev_config_dirty_node
));
1233 list_create(&spa
->spa_evicting_os_list
, sizeof (objset_t
),
1234 offsetof(objset_t
, os_evicting_node
));
1235 list_create(&spa
->spa_state_dirty_list
, sizeof (vdev_t
),
1236 offsetof(vdev_t
, vdev_state_dirty_node
));
1238 txg_list_create(&spa
->spa_vdev_txg_list
, spa
,
1239 offsetof(struct vdev
, vdev_txg_node
));
1241 avl_create(&spa
->spa_errlist_scrub
,
1242 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1243 offsetof(spa_error_entry_t
, se_avl
));
1244 avl_create(&spa
->spa_errlist_last
,
1245 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1246 offsetof(spa_error_entry_t
, se_avl
));
1248 spa_keystore_init(&spa
->spa_keystore
);
1251 * This taskq is used to perform zvol-minor-related tasks
1252 * asynchronously. This has several advantages, including easy
1253 * resolution of various deadlocks (zfsonlinux bug #3681).
1255 * The taskq must be single threaded to ensure tasks are always
1256 * processed in the order in which they were dispatched.
1258 * A taskq per pool allows one to keep the pools independent.
1259 * This way if one pool is suspended, it will not impact another.
1261 * The preferred location to dispatch a zvol minor task is a sync
1262 * task. In this context, there is easy access to the spa_t and minimal
1263 * error handling is required because the sync task must succeed.
1265 spa
->spa_zvol_taskq
= taskq_create("z_zvol", 1, defclsyspri
,
1269 * Taskq dedicated to prefetcher threads: this is used to prevent the
1270 * pool traverse code from monopolizing the global (and limited)
1271 * system_taskq by inappropriately scheduling long running tasks on it.
1273 spa
->spa_prefetch_taskq
= taskq_create("z_prefetch", boot_ncpus
,
1274 defclsyspri
, 1, INT_MAX
, TASKQ_DYNAMIC
);
1277 * The taskq to upgrade datasets in this pool. Currently used by
1278 * feature SPA_FEATURE_USEROBJ_ACCOUNTING/SPA_FEATURE_PROJECT_QUOTA.
1280 spa
->spa_upgrade_taskq
= taskq_create("z_upgrade", boot_ncpus
,
1281 defclsyspri
, 1, INT_MAX
, TASKQ_DYNAMIC
);
1285 * Opposite of spa_activate().
1288 spa_deactivate(spa_t
*spa
)
1290 ASSERT(spa
->spa_sync_on
== B_FALSE
);
1291 ASSERT(spa
->spa_dsl_pool
== NULL
);
1292 ASSERT(spa
->spa_root_vdev
== NULL
);
1293 ASSERT(spa
->spa_async_zio_root
== NULL
);
1294 ASSERT(spa
->spa_state
!= POOL_STATE_UNINITIALIZED
);
1296 spa_evicting_os_wait(spa
);
1298 if (spa
->spa_zvol_taskq
) {
1299 taskq_destroy(spa
->spa_zvol_taskq
);
1300 spa
->spa_zvol_taskq
= NULL
;
1303 if (spa
->spa_prefetch_taskq
) {
1304 taskq_destroy(spa
->spa_prefetch_taskq
);
1305 spa
->spa_prefetch_taskq
= NULL
;
1308 if (spa
->spa_upgrade_taskq
) {
1309 taskq_destroy(spa
->spa_upgrade_taskq
);
1310 spa
->spa_upgrade_taskq
= NULL
;
1313 txg_list_destroy(&spa
->spa_vdev_txg_list
);
1315 list_destroy(&spa
->spa_config_dirty_list
);
1316 list_destroy(&spa
->spa_evicting_os_list
);
1317 list_destroy(&spa
->spa_state_dirty_list
);
1319 taskq_cancel_id(system_delay_taskq
, spa
->spa_deadman_tqid
);
1321 for (int t
= 0; t
< ZIO_TYPES
; t
++) {
1322 for (int q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1323 spa_taskqs_fini(spa
, t
, q
);
1327 for (size_t i
= 0; i
< TXG_SIZE
; i
++) {
1328 ASSERT3P(spa
->spa_txg_zio
[i
], !=, NULL
);
1329 VERIFY0(zio_wait(spa
->spa_txg_zio
[i
]));
1330 spa
->spa_txg_zio
[i
] = NULL
;
1333 metaslab_class_destroy(spa
->spa_normal_class
);
1334 spa
->spa_normal_class
= NULL
;
1336 metaslab_class_destroy(spa
->spa_log_class
);
1337 spa
->spa_log_class
= NULL
;
1339 metaslab_class_destroy(spa
->spa_special_class
);
1340 spa
->spa_special_class
= NULL
;
1342 metaslab_class_destroy(spa
->spa_dedup_class
);
1343 spa
->spa_dedup_class
= NULL
;
1346 * If this was part of an import or the open otherwise failed, we may
1347 * still have errors left in the queues. Empty them just in case.
1349 spa_errlog_drain(spa
);
1350 avl_destroy(&spa
->spa_errlist_scrub
);
1351 avl_destroy(&spa
->spa_errlist_last
);
1353 spa_keystore_fini(&spa
->spa_keystore
);
1355 spa
->spa_state
= POOL_STATE_UNINITIALIZED
;
1357 mutex_enter(&spa
->spa_proc_lock
);
1358 if (spa
->spa_proc_state
!= SPA_PROC_NONE
) {
1359 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1360 spa
->spa_proc_state
= SPA_PROC_DEACTIVATE
;
1361 cv_broadcast(&spa
->spa_proc_cv
);
1362 while (spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
) {
1363 ASSERT(spa
->spa_proc
!= &p0
);
1364 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1366 ASSERT(spa
->spa_proc_state
== SPA_PROC_GONE
);
1367 spa
->spa_proc_state
= SPA_PROC_NONE
;
1369 ASSERT(spa
->spa_proc
== &p0
);
1370 mutex_exit(&spa
->spa_proc_lock
);
1373 * We want to make sure spa_thread() has actually exited the ZFS
1374 * module, so that the module can't be unloaded out from underneath
1377 if (spa
->spa_did
!= 0) {
1378 thread_join(spa
->spa_did
);
1384 * Verify a pool configuration, and construct the vdev tree appropriately. This
1385 * will create all the necessary vdevs in the appropriate layout, with each vdev
1386 * in the CLOSED state. This will prep the pool before open/creation/import.
1387 * All vdev validation is done by the vdev_alloc() routine.
1390 spa_config_parse(spa_t
*spa
, vdev_t
**vdp
, nvlist_t
*nv
, vdev_t
*parent
,
1391 uint_t id
, int atype
)
1397 if ((error
= vdev_alloc(spa
, vdp
, nv
, parent
, id
, atype
)) != 0)
1400 if ((*vdp
)->vdev_ops
->vdev_op_leaf
)
1403 error
= nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1406 if (error
== ENOENT
)
1412 return (SET_ERROR(EINVAL
));
1415 for (int c
= 0; c
< children
; c
++) {
1417 if ((error
= spa_config_parse(spa
, &vd
, child
[c
], *vdp
, c
,
1425 ASSERT(*vdp
!= NULL
);
1431 * Opposite of spa_load().
1434 spa_unload(spa_t
*spa
)
1438 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1440 spa_load_note(spa
, "UNLOADING");
1445 spa_async_suspend(spa
);
1447 if (spa
->spa_root_vdev
) {
1448 vdev_t
*root_vdev
= spa
->spa_root_vdev
;
1449 vdev_initialize_stop_all(root_vdev
, VDEV_INITIALIZE_ACTIVE
);
1450 vdev_trim_stop_all(root_vdev
, VDEV_TRIM_ACTIVE
);
1451 vdev_autotrim_stop_all(spa
);
1457 if (spa
->spa_sync_on
) {
1458 txg_sync_stop(spa
->spa_dsl_pool
);
1459 spa
->spa_sync_on
= B_FALSE
;
1463 * Even though vdev_free() also calls vdev_metaslab_fini, we need
1464 * to call it earlier, before we wait for async i/o to complete.
1465 * This ensures that there is no async metaslab prefetching, by
1466 * calling taskq_wait(mg_taskq).
1468 if (spa
->spa_root_vdev
!= NULL
) {
1469 spa_config_enter(spa
, SCL_ALL
, spa
, RW_WRITER
);
1470 for (int c
= 0; c
< spa
->spa_root_vdev
->vdev_children
; c
++)
1471 vdev_metaslab_fini(spa
->spa_root_vdev
->vdev_child
[c
]);
1472 spa_config_exit(spa
, SCL_ALL
, spa
);
1475 if (spa
->spa_mmp
.mmp_thread
)
1476 mmp_thread_stop(spa
);
1479 * Wait for any outstanding async I/O to complete.
1481 if (spa
->spa_async_zio_root
!= NULL
) {
1482 for (int i
= 0; i
< max_ncpus
; i
++)
1483 (void) zio_wait(spa
->spa_async_zio_root
[i
]);
1484 kmem_free(spa
->spa_async_zio_root
, max_ncpus
* sizeof (void *));
1485 spa
->spa_async_zio_root
= NULL
;
1488 if (spa
->spa_vdev_removal
!= NULL
) {
1489 spa_vdev_removal_destroy(spa
->spa_vdev_removal
);
1490 spa
->spa_vdev_removal
= NULL
;
1493 if (spa
->spa_condense_zthr
!= NULL
) {
1494 zthr_destroy(spa
->spa_condense_zthr
);
1495 spa
->spa_condense_zthr
= NULL
;
1498 if (spa
->spa_checkpoint_discard_zthr
!= NULL
) {
1499 zthr_destroy(spa
->spa_checkpoint_discard_zthr
);
1500 spa
->spa_checkpoint_discard_zthr
= NULL
;
1503 spa_condense_fini(spa
);
1505 bpobj_close(&spa
->spa_deferred_bpobj
);
1507 spa_config_enter(spa
, SCL_ALL
, spa
, RW_WRITER
);
1512 if (spa
->spa_root_vdev
)
1513 vdev_free(spa
->spa_root_vdev
);
1514 ASSERT(spa
->spa_root_vdev
== NULL
);
1517 * Close the dsl pool.
1519 if (spa
->spa_dsl_pool
) {
1520 dsl_pool_close(spa
->spa_dsl_pool
);
1521 spa
->spa_dsl_pool
= NULL
;
1522 spa
->spa_meta_objset
= NULL
;
1528 * Drop and purge level 2 cache
1530 spa_l2cache_drop(spa
);
1532 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1533 vdev_free(spa
->spa_spares
.sav_vdevs
[i
]);
1534 if (spa
->spa_spares
.sav_vdevs
) {
1535 kmem_free(spa
->spa_spares
.sav_vdevs
,
1536 spa
->spa_spares
.sav_count
* sizeof (void *));
1537 spa
->spa_spares
.sav_vdevs
= NULL
;
1539 if (spa
->spa_spares
.sav_config
) {
1540 nvlist_free(spa
->spa_spares
.sav_config
);
1541 spa
->spa_spares
.sav_config
= NULL
;
1543 spa
->spa_spares
.sav_count
= 0;
1545 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
1546 vdev_clear_stats(spa
->spa_l2cache
.sav_vdevs
[i
]);
1547 vdev_free(spa
->spa_l2cache
.sav_vdevs
[i
]);
1549 if (spa
->spa_l2cache
.sav_vdevs
) {
1550 kmem_free(spa
->spa_l2cache
.sav_vdevs
,
1551 spa
->spa_l2cache
.sav_count
* sizeof (void *));
1552 spa
->spa_l2cache
.sav_vdevs
= NULL
;
1554 if (spa
->spa_l2cache
.sav_config
) {
1555 nvlist_free(spa
->spa_l2cache
.sav_config
);
1556 spa
->spa_l2cache
.sav_config
= NULL
;
1558 spa
->spa_l2cache
.sav_count
= 0;
1560 spa
->spa_async_suspended
= 0;
1562 spa
->spa_indirect_vdevs_loaded
= B_FALSE
;
1564 if (spa
->spa_comment
!= NULL
) {
1565 spa_strfree(spa
->spa_comment
);
1566 spa
->spa_comment
= NULL
;
1569 spa_config_exit(spa
, SCL_ALL
, spa
);
1573 * Load (or re-load) the current list of vdevs describing the active spares for
1574 * this pool. When this is called, we have some form of basic information in
1575 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1576 * then re-generate a more complete list including status information.
1579 spa_load_spares(spa_t
*spa
)
1588 * zdb opens both the current state of the pool and the
1589 * checkpointed state (if present), with a different spa_t.
1591 * As spare vdevs are shared among open pools, we skip loading
1592 * them when we load the checkpointed state of the pool.
1594 if (!spa_writeable(spa
))
1598 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1601 * First, close and free any existing spare vdevs.
1603 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1604 vd
= spa
->spa_spares
.sav_vdevs
[i
];
1606 /* Undo the call to spa_activate() below */
1607 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1608 B_FALSE
)) != NULL
&& tvd
->vdev_isspare
)
1609 spa_spare_remove(tvd
);
1614 if (spa
->spa_spares
.sav_vdevs
)
1615 kmem_free(spa
->spa_spares
.sav_vdevs
,
1616 spa
->spa_spares
.sav_count
* sizeof (void *));
1618 if (spa
->spa_spares
.sav_config
== NULL
)
1621 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
1622 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
1624 spa
->spa_spares
.sav_count
= (int)nspares
;
1625 spa
->spa_spares
.sav_vdevs
= NULL
;
1631 * Construct the array of vdevs, opening them to get status in the
1632 * process. For each spare, there is potentially two different vdev_t
1633 * structures associated with it: one in the list of spares (used only
1634 * for basic validation purposes) and one in the active vdev
1635 * configuration (if it's spared in). During this phase we open and
1636 * validate each vdev on the spare list. If the vdev also exists in the
1637 * active configuration, then we also mark this vdev as an active spare.
1639 spa
->spa_spares
.sav_vdevs
= kmem_zalloc(nspares
* sizeof (void *),
1641 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1642 VERIFY(spa_config_parse(spa
, &vd
, spares
[i
], NULL
, 0,
1643 VDEV_ALLOC_SPARE
) == 0);
1646 spa
->spa_spares
.sav_vdevs
[i
] = vd
;
1648 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1649 B_FALSE
)) != NULL
) {
1650 if (!tvd
->vdev_isspare
)
1654 * We only mark the spare active if we were successfully
1655 * able to load the vdev. Otherwise, importing a pool
1656 * with a bad active spare would result in strange
1657 * behavior, because multiple pool would think the spare
1658 * is actively in use.
1660 * There is a vulnerability here to an equally bizarre
1661 * circumstance, where a dead active spare is later
1662 * brought back to life (onlined or otherwise). Given
1663 * the rarity of this scenario, and the extra complexity
1664 * it adds, we ignore the possibility.
1666 if (!vdev_is_dead(tvd
))
1667 spa_spare_activate(tvd
);
1671 vd
->vdev_aux
= &spa
->spa_spares
;
1673 if (vdev_open(vd
) != 0)
1676 if (vdev_validate_aux(vd
) == 0)
1681 * Recompute the stashed list of spares, with status information
1684 VERIFY(nvlist_remove(spa
->spa_spares
.sav_config
, ZPOOL_CONFIG_SPARES
,
1685 DATA_TYPE_NVLIST_ARRAY
) == 0);
1687 spares
= kmem_alloc(spa
->spa_spares
.sav_count
* sizeof (void *),
1689 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1690 spares
[i
] = vdev_config_generate(spa
,
1691 spa
->spa_spares
.sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_SPARE
);
1692 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
1693 ZPOOL_CONFIG_SPARES
, spares
, spa
->spa_spares
.sav_count
) == 0);
1694 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1695 nvlist_free(spares
[i
]);
1696 kmem_free(spares
, spa
->spa_spares
.sav_count
* sizeof (void *));
1700 * Load (or re-load) the current list of vdevs describing the active l2cache for
1701 * this pool. When this is called, we have some form of basic information in
1702 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1703 * then re-generate a more complete list including status information.
1704 * Devices which are already active have their details maintained, and are
1708 spa_load_l2cache(spa_t
*spa
)
1710 nvlist_t
**l2cache
= NULL
;
1712 int i
, j
, oldnvdevs
;
1714 vdev_t
*vd
, **oldvdevs
, **newvdevs
;
1715 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
1719 * zdb opens both the current state of the pool and the
1720 * checkpointed state (if present), with a different spa_t.
1722 * As L2 caches are part of the ARC which is shared among open
1723 * pools, we skip loading them when we load the checkpointed
1724 * state of the pool.
1726 if (!spa_writeable(spa
))
1730 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1732 oldvdevs
= sav
->sav_vdevs
;
1733 oldnvdevs
= sav
->sav_count
;
1734 sav
->sav_vdevs
= NULL
;
1737 if (sav
->sav_config
== NULL
) {
1743 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
,
1744 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
1745 newvdevs
= kmem_alloc(nl2cache
* sizeof (void *), KM_SLEEP
);
1748 * Process new nvlist of vdevs.
1750 for (i
= 0; i
< nl2cache
; i
++) {
1751 VERIFY(nvlist_lookup_uint64(l2cache
[i
], ZPOOL_CONFIG_GUID
,
1755 for (j
= 0; j
< oldnvdevs
; j
++) {
1757 if (vd
!= NULL
&& guid
== vd
->vdev_guid
) {
1759 * Retain previous vdev for add/remove ops.
1767 if (newvdevs
[i
] == NULL
) {
1771 VERIFY(spa_config_parse(spa
, &vd
, l2cache
[i
], NULL
, 0,
1772 VDEV_ALLOC_L2CACHE
) == 0);
1777 * Commit this vdev as an l2cache device,
1778 * even if it fails to open.
1780 spa_l2cache_add(vd
);
1785 spa_l2cache_activate(vd
);
1787 if (vdev_open(vd
) != 0)
1790 (void) vdev_validate_aux(vd
);
1792 if (!vdev_is_dead(vd
))
1793 l2arc_add_vdev(spa
, vd
);
1797 sav
->sav_vdevs
= newvdevs
;
1798 sav
->sav_count
= (int)nl2cache
;
1801 * Recompute the stashed list of l2cache devices, with status
1802 * information this time.
1804 VERIFY(nvlist_remove(sav
->sav_config
, ZPOOL_CONFIG_L2CACHE
,
1805 DATA_TYPE_NVLIST_ARRAY
) == 0);
1807 if (sav
->sav_count
> 0)
1808 l2cache
= kmem_alloc(sav
->sav_count
* sizeof (void *),
1810 for (i
= 0; i
< sav
->sav_count
; i
++)
1811 l2cache
[i
] = vdev_config_generate(spa
,
1812 sav
->sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_L2CACHE
);
1813 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
,
1814 ZPOOL_CONFIG_L2CACHE
, l2cache
, sav
->sav_count
) == 0);
1818 * Purge vdevs that were dropped
1820 for (i
= 0; i
< oldnvdevs
; i
++) {
1825 ASSERT(vd
->vdev_isl2cache
);
1827 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
1828 pool
!= 0ULL && l2arc_vdev_present(vd
))
1829 l2arc_remove_vdev(vd
);
1830 vdev_clear_stats(vd
);
1836 kmem_free(oldvdevs
, oldnvdevs
* sizeof (void *));
1838 for (i
= 0; i
< sav
->sav_count
; i
++)
1839 nvlist_free(l2cache
[i
]);
1841 kmem_free(l2cache
, sav
->sav_count
* sizeof (void *));
1845 load_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
**value
)
1848 char *packed
= NULL
;
1853 error
= dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
);
1857 nvsize
= *(uint64_t *)db
->db_data
;
1858 dmu_buf_rele(db
, FTAG
);
1860 packed
= vmem_alloc(nvsize
, KM_SLEEP
);
1861 error
= dmu_read(spa
->spa_meta_objset
, obj
, 0, nvsize
, packed
,
1864 error
= nvlist_unpack(packed
, nvsize
, value
, 0);
1865 vmem_free(packed
, nvsize
);
1871 * Concrete top-level vdevs that are not missing and are not logs. At every
1872 * spa_sync we write new uberblocks to at least SPA_SYNC_MIN_VDEVS core tvds.
1875 spa_healthy_core_tvds(spa_t
*spa
)
1877 vdev_t
*rvd
= spa
->spa_root_vdev
;
1880 for (uint64_t i
= 0; i
< rvd
->vdev_children
; i
++) {
1881 vdev_t
*vd
= rvd
->vdev_child
[i
];
1884 if (vdev_is_concrete(vd
) && !vdev_is_dead(vd
))
1892 * Checks to see if the given vdev could not be opened, in which case we post a
1893 * sysevent to notify the autoreplace code that the device has been removed.
1896 spa_check_removed(vdev_t
*vd
)
1898 for (uint64_t c
= 0; c
< vd
->vdev_children
; c
++)
1899 spa_check_removed(vd
->vdev_child
[c
]);
1901 if (vd
->vdev_ops
->vdev_op_leaf
&& vdev_is_dead(vd
) &&
1902 vdev_is_concrete(vd
)) {
1903 zfs_post_autoreplace(vd
->vdev_spa
, vd
);
1904 spa_event_notify(vd
->vdev_spa
, vd
, NULL
, ESC_ZFS_VDEV_CHECK
);
1909 spa_check_for_missing_logs(spa_t
*spa
)
1911 vdev_t
*rvd
= spa
->spa_root_vdev
;
1914 * If we're doing a normal import, then build up any additional
1915 * diagnostic information about missing log devices.
1916 * We'll pass this up to the user for further processing.
1918 if (!(spa
->spa_import_flags
& ZFS_IMPORT_MISSING_LOG
)) {
1919 nvlist_t
**child
, *nv
;
1922 child
= kmem_alloc(rvd
->vdev_children
* sizeof (nvlist_t
*),
1924 VERIFY(nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
1926 for (uint64_t c
= 0; c
< rvd
->vdev_children
; c
++) {
1927 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1930 * We consider a device as missing only if it failed
1931 * to open (i.e. offline or faulted is not considered
1934 if (tvd
->vdev_islog
&&
1935 tvd
->vdev_state
== VDEV_STATE_CANT_OPEN
) {
1936 child
[idx
++] = vdev_config_generate(spa
, tvd
,
1937 B_FALSE
, VDEV_CONFIG_MISSING
);
1942 fnvlist_add_nvlist_array(nv
,
1943 ZPOOL_CONFIG_CHILDREN
, child
, idx
);
1944 fnvlist_add_nvlist(spa
->spa_load_info
,
1945 ZPOOL_CONFIG_MISSING_DEVICES
, nv
);
1947 for (uint64_t i
= 0; i
< idx
; i
++)
1948 nvlist_free(child
[i
]);
1951 kmem_free(child
, rvd
->vdev_children
* sizeof (char **));
1954 spa_load_failed(spa
, "some log devices are missing");
1955 vdev_dbgmsg_print_tree(rvd
, 2);
1956 return (SET_ERROR(ENXIO
));
1959 for (uint64_t c
= 0; c
< rvd
->vdev_children
; c
++) {
1960 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1962 if (tvd
->vdev_islog
&&
1963 tvd
->vdev_state
== VDEV_STATE_CANT_OPEN
) {
1964 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
1965 spa_load_note(spa
, "some log devices are "
1966 "missing, ZIL is dropped.");
1967 vdev_dbgmsg_print_tree(rvd
, 2);
1977 * Check for missing log devices
1980 spa_check_logs(spa_t
*spa
)
1982 boolean_t rv
= B_FALSE
;
1983 dsl_pool_t
*dp
= spa_get_dsl(spa
);
1985 switch (spa
->spa_log_state
) {
1988 case SPA_LOG_MISSING
:
1989 /* need to recheck in case slog has been restored */
1990 case SPA_LOG_UNKNOWN
:
1991 rv
= (dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
1992 zil_check_log_chain
, NULL
, DS_FIND_CHILDREN
) != 0);
1994 spa_set_log_state(spa
, SPA_LOG_MISSING
);
2001 spa_passivate_log(spa_t
*spa
)
2003 vdev_t
*rvd
= spa
->spa_root_vdev
;
2004 boolean_t slog_found
= B_FALSE
;
2006 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
2008 if (!spa_has_slogs(spa
))
2011 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
2012 vdev_t
*tvd
= rvd
->vdev_child
[c
];
2013 metaslab_group_t
*mg
= tvd
->vdev_mg
;
2015 if (tvd
->vdev_islog
) {
2016 metaslab_group_passivate(mg
);
2017 slog_found
= B_TRUE
;
2021 return (slog_found
);
2025 spa_activate_log(spa_t
*spa
)
2027 vdev_t
*rvd
= spa
->spa_root_vdev
;
2029 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
2031 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
2032 vdev_t
*tvd
= rvd
->vdev_child
[c
];
2033 metaslab_group_t
*mg
= tvd
->vdev_mg
;
2035 if (tvd
->vdev_islog
)
2036 metaslab_group_activate(mg
);
2041 spa_reset_logs(spa_t
*spa
)
2045 error
= dmu_objset_find(spa_name(spa
), zil_reset
,
2046 NULL
, DS_FIND_CHILDREN
);
2049 * We successfully offlined the log device, sync out the
2050 * current txg so that the "stubby" block can be removed
2053 txg_wait_synced(spa
->spa_dsl_pool
, 0);
2059 spa_aux_check_removed(spa_aux_vdev_t
*sav
)
2061 for (int i
= 0; i
< sav
->sav_count
; i
++)
2062 spa_check_removed(sav
->sav_vdevs
[i
]);
2066 spa_claim_notify(zio_t
*zio
)
2068 spa_t
*spa
= zio
->io_spa
;
2073 mutex_enter(&spa
->spa_props_lock
); /* any mutex will do */
2074 if (spa
->spa_claim_max_txg
< zio
->io_bp
->blk_birth
)
2075 spa
->spa_claim_max_txg
= zio
->io_bp
->blk_birth
;
2076 mutex_exit(&spa
->spa_props_lock
);
2079 typedef struct spa_load_error
{
2080 uint64_t sle_meta_count
;
2081 uint64_t sle_data_count
;
2085 spa_load_verify_done(zio_t
*zio
)
2087 blkptr_t
*bp
= zio
->io_bp
;
2088 spa_load_error_t
*sle
= zio
->io_private
;
2089 dmu_object_type_t type
= BP_GET_TYPE(bp
);
2090 int error
= zio
->io_error
;
2091 spa_t
*spa
= zio
->io_spa
;
2093 abd_free(zio
->io_abd
);
2095 if ((BP_GET_LEVEL(bp
) != 0 || DMU_OT_IS_METADATA(type
)) &&
2096 type
!= DMU_OT_INTENT_LOG
)
2097 atomic_inc_64(&sle
->sle_meta_count
);
2099 atomic_inc_64(&sle
->sle_data_count
);
2102 mutex_enter(&spa
->spa_scrub_lock
);
2103 spa
->spa_load_verify_ios
--;
2104 cv_broadcast(&spa
->spa_scrub_io_cv
);
2105 mutex_exit(&spa
->spa_scrub_lock
);
2109 * Maximum number of concurrent scrub i/os to create while verifying
2110 * a pool while importing it.
2112 int spa_load_verify_maxinflight
= 10000;
2113 int spa_load_verify_metadata
= B_TRUE
;
2114 int spa_load_verify_data
= B_TRUE
;
2118 spa_load_verify_cb(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
2119 const zbookmark_phys_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
2121 if (bp
== NULL
|| BP_IS_HOLE(bp
) || BP_IS_EMBEDDED(bp
))
2124 * Note: normally this routine will not be called if
2125 * spa_load_verify_metadata is not set. However, it may be useful
2126 * to manually set the flag after the traversal has begun.
2128 if (!spa_load_verify_metadata
)
2130 if (!BP_IS_METADATA(bp
) && !spa_load_verify_data
)
2134 size_t size
= BP_GET_PSIZE(bp
);
2136 mutex_enter(&spa
->spa_scrub_lock
);
2137 while (spa
->spa_load_verify_ios
>= spa_load_verify_maxinflight
)
2138 cv_wait(&spa
->spa_scrub_io_cv
, &spa
->spa_scrub_lock
);
2139 spa
->spa_load_verify_ios
++;
2140 mutex_exit(&spa
->spa_scrub_lock
);
2142 zio_nowait(zio_read(rio
, spa
, bp
, abd_alloc_for_io(size
, B_FALSE
), size
,
2143 spa_load_verify_done
, rio
->io_private
, ZIO_PRIORITY_SCRUB
,
2144 ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_CANFAIL
|
2145 ZIO_FLAG_SCRUB
| ZIO_FLAG_RAW
, zb
));
2151 verify_dataset_name_len(dsl_pool_t
*dp
, dsl_dataset_t
*ds
, void *arg
)
2153 if (dsl_dataset_namelen(ds
) >= ZFS_MAX_DATASET_NAME_LEN
)
2154 return (SET_ERROR(ENAMETOOLONG
));
2160 spa_load_verify(spa_t
*spa
)
2163 spa_load_error_t sle
= { 0 };
2164 zpool_load_policy_t policy
;
2165 boolean_t verify_ok
= B_FALSE
;
2168 zpool_get_load_policy(spa
->spa_config
, &policy
);
2170 if (policy
.zlp_rewind
& ZPOOL_NEVER_REWIND
)
2173 dsl_pool_config_enter(spa
->spa_dsl_pool
, FTAG
);
2174 error
= dmu_objset_find_dp(spa
->spa_dsl_pool
,
2175 spa
->spa_dsl_pool
->dp_root_dir_obj
, verify_dataset_name_len
, NULL
,
2177 dsl_pool_config_exit(spa
->spa_dsl_pool
, FTAG
);
2181 rio
= zio_root(spa
, NULL
, &sle
,
2182 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
);
2184 if (spa_load_verify_metadata
) {
2185 if (spa
->spa_extreme_rewind
) {
2186 spa_load_note(spa
, "performing a complete scan of the "
2187 "pool since extreme rewind is on. This may take "
2188 "a very long time.\n (spa_load_verify_data=%u, "
2189 "spa_load_verify_metadata=%u)",
2190 spa_load_verify_data
, spa_load_verify_metadata
);
2192 error
= traverse_pool(spa
, spa
->spa_verify_min_txg
,
2193 TRAVERSE_PRE
| TRAVERSE_PREFETCH_METADATA
|
2194 TRAVERSE_NO_DECRYPT
, spa_load_verify_cb
, rio
);
2197 (void) zio_wait(rio
);
2199 spa
->spa_load_meta_errors
= sle
.sle_meta_count
;
2200 spa
->spa_load_data_errors
= sle
.sle_data_count
;
2202 if (sle
.sle_meta_count
!= 0 || sle
.sle_data_count
!= 0) {
2203 spa_load_note(spa
, "spa_load_verify found %llu metadata errors "
2204 "and %llu data errors", (u_longlong_t
)sle
.sle_meta_count
,
2205 (u_longlong_t
)sle
.sle_data_count
);
2208 if (spa_load_verify_dryrun
||
2209 (!error
&& sle
.sle_meta_count
<= policy
.zlp_maxmeta
&&
2210 sle
.sle_data_count
<= policy
.zlp_maxdata
)) {
2214 spa
->spa_load_txg
= spa
->spa_uberblock
.ub_txg
;
2215 spa
->spa_load_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
2217 loss
= spa
->spa_last_ubsync_txg_ts
- spa
->spa_load_txg_ts
;
2218 VERIFY(nvlist_add_uint64(spa
->spa_load_info
,
2219 ZPOOL_CONFIG_LOAD_TIME
, spa
->spa_load_txg_ts
) == 0);
2220 VERIFY(nvlist_add_int64(spa
->spa_load_info
,
2221 ZPOOL_CONFIG_REWIND_TIME
, loss
) == 0);
2222 VERIFY(nvlist_add_uint64(spa
->spa_load_info
,
2223 ZPOOL_CONFIG_LOAD_DATA_ERRORS
, sle
.sle_data_count
) == 0);
2225 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
;
2228 if (spa_load_verify_dryrun
)
2232 if (error
!= ENXIO
&& error
!= EIO
)
2233 error
= SET_ERROR(EIO
);
2237 return (verify_ok
? 0 : EIO
);
2241 * Find a value in the pool props object.
2244 spa_prop_find(spa_t
*spa
, zpool_prop_t prop
, uint64_t *val
)
2246 (void) zap_lookup(spa
->spa_meta_objset
, spa
->spa_pool_props_object
,
2247 zpool_prop_to_name(prop
), sizeof (uint64_t), 1, val
);
2251 * Find a value in the pool directory object.
2254 spa_dir_prop(spa_t
*spa
, const char *name
, uint64_t *val
, boolean_t log_enoent
)
2256 int error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
2257 name
, sizeof (uint64_t), 1, val
);
2259 if (error
!= 0 && (error
!= ENOENT
|| log_enoent
)) {
2260 spa_load_failed(spa
, "couldn't get '%s' value in MOS directory "
2261 "[error=%d]", name
, error
);
2268 spa_vdev_err(vdev_t
*vdev
, vdev_aux_t aux
, int err
)
2270 vdev_set_state(vdev
, B_TRUE
, VDEV_STATE_CANT_OPEN
, aux
);
2271 return (SET_ERROR(err
));
2275 spa_spawn_aux_threads(spa_t
*spa
)
2277 ASSERT(spa_writeable(spa
));
2279 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
2281 spa_start_indirect_condensing_thread(spa
);
2283 ASSERT3P(spa
->spa_checkpoint_discard_zthr
, ==, NULL
);
2284 spa
->spa_checkpoint_discard_zthr
=
2285 zthr_create(spa_checkpoint_discard_thread_check
,
2286 spa_checkpoint_discard_thread
, spa
);
2290 * Fix up config after a partly-completed split. This is done with the
2291 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
2292 * pool have that entry in their config, but only the splitting one contains
2293 * a list of all the guids of the vdevs that are being split off.
2295 * This function determines what to do with that list: either rejoin
2296 * all the disks to the pool, or complete the splitting process. To attempt
2297 * the rejoin, each disk that is offlined is marked online again, and
2298 * we do a reopen() call. If the vdev label for every disk that was
2299 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
2300 * then we call vdev_split() on each disk, and complete the split.
2302 * Otherwise we leave the config alone, with all the vdevs in place in
2303 * the original pool.
2306 spa_try_repair(spa_t
*spa
, nvlist_t
*config
)
2313 boolean_t attempt_reopen
;
2315 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
, &nvl
) != 0)
2318 /* check that the config is complete */
2319 if (nvlist_lookup_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
2320 &glist
, &gcount
) != 0)
2323 vd
= kmem_zalloc(gcount
* sizeof (vdev_t
*), KM_SLEEP
);
2325 /* attempt to online all the vdevs & validate */
2326 attempt_reopen
= B_TRUE
;
2327 for (i
= 0; i
< gcount
; i
++) {
2328 if (glist
[i
] == 0) /* vdev is hole */
2331 vd
[i
] = spa_lookup_by_guid(spa
, glist
[i
], B_FALSE
);
2332 if (vd
[i
] == NULL
) {
2334 * Don't bother attempting to reopen the disks;
2335 * just do the split.
2337 attempt_reopen
= B_FALSE
;
2339 /* attempt to re-online it */
2340 vd
[i
]->vdev_offline
= B_FALSE
;
2344 if (attempt_reopen
) {
2345 vdev_reopen(spa
->spa_root_vdev
);
2347 /* check each device to see what state it's in */
2348 for (extracted
= 0, i
= 0; i
< gcount
; i
++) {
2349 if (vd
[i
] != NULL
&&
2350 vd
[i
]->vdev_stat
.vs_aux
!= VDEV_AUX_SPLIT_POOL
)
2357 * If every disk has been moved to the new pool, or if we never
2358 * even attempted to look at them, then we split them off for
2361 if (!attempt_reopen
|| gcount
== extracted
) {
2362 for (i
= 0; i
< gcount
; i
++)
2365 vdev_reopen(spa
->spa_root_vdev
);
2368 kmem_free(vd
, gcount
* sizeof (vdev_t
*));
2372 spa_load(spa_t
*spa
, spa_load_state_t state
, spa_import_type_t type
)
2374 char *ereport
= FM_EREPORT_ZFS_POOL
;
2377 spa
->spa_load_state
= state
;
2379 gethrestime(&spa
->spa_loaded_ts
);
2380 error
= spa_load_impl(spa
, type
, &ereport
);
2383 * Don't count references from objsets that are already closed
2384 * and are making their way through the eviction process.
2386 spa_evicting_os_wait(spa
);
2387 spa
->spa_minref
= zfs_refcount_count(&spa
->spa_refcount
);
2389 if (error
!= EEXIST
) {
2390 spa
->spa_loaded_ts
.tv_sec
= 0;
2391 spa
->spa_loaded_ts
.tv_nsec
= 0;
2393 if (error
!= EBADF
) {
2394 zfs_ereport_post(ereport
, spa
, NULL
, NULL
, NULL
, 0, 0);
2397 spa
->spa_load_state
= error
? SPA_LOAD_ERROR
: SPA_LOAD_NONE
;
2405 * Count the number of per-vdev ZAPs associated with all of the vdevs in the
2406 * vdev tree rooted in the given vd, and ensure that each ZAP is present in the
2407 * spa's per-vdev ZAP list.
2410 vdev_count_verify_zaps(vdev_t
*vd
)
2412 spa_t
*spa
= vd
->vdev_spa
;
2415 if (vd
->vdev_top_zap
!= 0) {
2417 ASSERT0(zap_lookup_int(spa
->spa_meta_objset
,
2418 spa
->spa_all_vdev_zaps
, vd
->vdev_top_zap
));
2420 if (vd
->vdev_leaf_zap
!= 0) {
2422 ASSERT0(zap_lookup_int(spa
->spa_meta_objset
,
2423 spa
->spa_all_vdev_zaps
, vd
->vdev_leaf_zap
));
2426 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
2427 total
+= vdev_count_verify_zaps(vd
->vdev_child
[i
]);
2435 * Determine whether the activity check is required.
2438 spa_activity_check_required(spa_t
*spa
, uberblock_t
*ub
, nvlist_t
*label
,
2442 uint64_t hostid
= 0;
2443 uint64_t tryconfig_txg
= 0;
2444 uint64_t tryconfig_timestamp
= 0;
2445 uint16_t tryconfig_mmp_seq
= 0;
2448 if (nvlist_exists(config
, ZPOOL_CONFIG_LOAD_INFO
)) {
2449 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
2450 (void) nvlist_lookup_uint64(nvinfo
, ZPOOL_CONFIG_MMP_TXG
,
2452 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_TIMESTAMP
,
2453 &tryconfig_timestamp
);
2454 (void) nvlist_lookup_uint16(nvinfo
, ZPOOL_CONFIG_MMP_SEQ
,
2455 &tryconfig_mmp_seq
);
2458 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
, &state
);
2461 * Disable the MMP activity check - This is used by zdb which
2462 * is intended to be used on potentially active pools.
2464 if (spa
->spa_import_flags
& ZFS_IMPORT_SKIP_MMP
)
2468 * Skip the activity check when the MMP feature is disabled.
2470 if (ub
->ub_mmp_magic
== MMP_MAGIC
&& ub
->ub_mmp_delay
== 0)
2473 * If the tryconfig_ values are nonzero, they are the results of an
2474 * earlier tryimport. If they all match the uberblock we just found,
2475 * then the pool has not changed and we return false so we do not test
2478 if (tryconfig_txg
&& tryconfig_txg
== ub
->ub_txg
&&
2479 tryconfig_timestamp
&& tryconfig_timestamp
== ub
->ub_timestamp
&&
2480 tryconfig_mmp_seq
&& tryconfig_mmp_seq
==
2481 (MMP_SEQ_VALID(ub
) ? MMP_SEQ(ub
) : 0))
2485 * Allow the activity check to be skipped when importing the pool
2486 * on the same host which last imported it. Since the hostid from
2487 * configuration may be stale use the one read from the label.
2489 if (nvlist_exists(label
, ZPOOL_CONFIG_HOSTID
))
2490 hostid
= fnvlist_lookup_uint64(label
, ZPOOL_CONFIG_HOSTID
);
2492 if (hostid
== spa_get_hostid())
2496 * Skip the activity test when the pool was cleanly exported.
2498 if (state
!= POOL_STATE_ACTIVE
)
2505 * Nanoseconds the activity check must watch for changes on-disk.
2508 spa_activity_check_duration(spa_t
*spa
, uberblock_t
*ub
)
2510 uint64_t import_intervals
= MAX(zfs_multihost_import_intervals
, 1);
2511 uint64_t multihost_interval
= MSEC2NSEC(
2512 MMP_INTERVAL_OK(zfs_multihost_interval
));
2513 uint64_t import_delay
= MAX(NANOSEC
, import_intervals
*
2514 multihost_interval
);
2517 * Local tunables determine a minimum duration except for the case
2518 * where we know when the remote host will suspend the pool if MMP
2519 * writes do not land.
2521 * See Big Theory comment at the top of mmp.c for the reasoning behind
2522 * these cases and times.
2525 ASSERT(MMP_IMPORT_SAFETY_FACTOR
>= 100);
2527 if (MMP_INTERVAL_VALID(ub
) && MMP_FAIL_INT_VALID(ub
) &&
2528 MMP_FAIL_INT(ub
) > 0) {
2530 /* MMP on remote host will suspend pool after failed writes */
2531 import_delay
= MMP_FAIL_INT(ub
) * MSEC2NSEC(MMP_INTERVAL(ub
)) *
2532 MMP_IMPORT_SAFETY_FACTOR
/ 100;
2534 zfs_dbgmsg("fail_intvals>0 import_delay=%llu ub_mmp "
2535 "mmp_fails=%llu ub_mmp mmp_interval=%llu "
2536 "import_intervals=%u", import_delay
, MMP_FAIL_INT(ub
),
2537 MMP_INTERVAL(ub
), import_intervals
);
2539 } else if (MMP_INTERVAL_VALID(ub
) && MMP_FAIL_INT_VALID(ub
) &&
2540 MMP_FAIL_INT(ub
) == 0) {
2542 /* MMP on remote host will never suspend pool */
2543 import_delay
= MAX(import_delay
, (MSEC2NSEC(MMP_INTERVAL(ub
)) +
2544 ub
->ub_mmp_delay
) * import_intervals
);
2546 zfs_dbgmsg("fail_intvals=0 import_delay=%llu ub_mmp "
2547 "mmp_interval=%llu ub_mmp_delay=%llu "
2548 "import_intervals=%u", import_delay
, MMP_INTERVAL(ub
),
2549 ub
->ub_mmp_delay
, import_intervals
);
2551 } else if (MMP_VALID(ub
)) {
2553 * zfs-0.7 compatability case
2556 import_delay
= MAX(import_delay
, (multihost_interval
+
2557 ub
->ub_mmp_delay
) * import_intervals
);
2559 zfs_dbgmsg("import_delay=%llu ub_mmp_delay=%llu "
2560 "import_intervals=%u leaves=%u", import_delay
,
2561 ub
->ub_mmp_delay
, import_intervals
,
2562 vdev_count_leaves(spa
));
2564 /* Using local tunings is the only reasonable option */
2565 zfs_dbgmsg("pool last imported on non-MMP aware "
2566 "host using import_delay=%llu multihost_interval=%llu "
2567 "import_intervals=%u", import_delay
, multihost_interval
,
2571 return (import_delay
);
2575 * Perform the import activity check. If the user canceled the import or
2576 * we detected activity then fail.
2579 spa_activity_check(spa_t
*spa
, uberblock_t
*ub
, nvlist_t
*config
)
2581 uint64_t txg
= ub
->ub_txg
;
2582 uint64_t timestamp
= ub
->ub_timestamp
;
2583 uint64_t mmp_config
= ub
->ub_mmp_config
;
2584 uint16_t mmp_seq
= MMP_SEQ_VALID(ub
) ? MMP_SEQ(ub
) : 0;
2585 uint64_t import_delay
;
2586 hrtime_t import_expire
;
2587 nvlist_t
*mmp_label
= NULL
;
2588 vdev_t
*rvd
= spa
->spa_root_vdev
;
2593 cv_init(&cv
, NULL
, CV_DEFAULT
, NULL
);
2594 mutex_init(&mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
2598 * If ZPOOL_CONFIG_MMP_TXG is present an activity check was performed
2599 * during the earlier tryimport. If the txg recorded there is 0 then
2600 * the pool is known to be active on another host.
2602 * Otherwise, the pool might be in use on another host. Check for
2603 * changes in the uberblocks on disk if necessary.
2605 if (nvlist_exists(config
, ZPOOL_CONFIG_LOAD_INFO
)) {
2606 nvlist_t
*nvinfo
= fnvlist_lookup_nvlist(config
,
2607 ZPOOL_CONFIG_LOAD_INFO
);
2609 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_TXG
) &&
2610 fnvlist_lookup_uint64(nvinfo
, ZPOOL_CONFIG_MMP_TXG
) == 0) {
2611 vdev_uberblock_load(rvd
, ub
, &mmp_label
);
2612 error
= SET_ERROR(EREMOTEIO
);
2617 import_delay
= spa_activity_check_duration(spa
, ub
);
2619 /* Add a small random factor in case of simultaneous imports (0-25%) */
2620 import_expire
= gethrtime() + import_delay
+
2621 (import_delay
* spa_get_random(250) / 1000);
2623 while (gethrtime() < import_expire
) {
2624 vdev_uberblock_load(rvd
, ub
, &mmp_label
);
2626 if (txg
!= ub
->ub_txg
|| timestamp
!= ub
->ub_timestamp
||
2627 mmp_seq
!= (MMP_SEQ_VALID(ub
) ? MMP_SEQ(ub
) : 0)) {
2628 zfs_dbgmsg("multihost activity detected "
2629 "txg %llu ub_txg %llu "
2630 "timestamp %llu ub_timestamp %llu "
2631 "mmp_config %#llx ub_mmp_config %#llx",
2632 txg
, ub
->ub_txg
, timestamp
, ub
->ub_timestamp
,
2633 mmp_config
, ub
->ub_mmp_config
);
2635 error
= SET_ERROR(EREMOTEIO
);
2640 nvlist_free(mmp_label
);
2644 error
= cv_timedwait_sig(&cv
, &mtx
, ddi_get_lbolt() + hz
);
2646 error
= SET_ERROR(EINTR
);
2654 mutex_destroy(&mtx
);
2658 * If the pool is determined to be active store the status in the
2659 * spa->spa_load_info nvlist. If the remote hostname or hostid are
2660 * available from configuration read from disk store them as well.
2661 * This allows 'zpool import' to generate a more useful message.
2663 * ZPOOL_CONFIG_MMP_STATE - observed pool status (mandatory)
2664 * ZPOOL_CONFIG_MMP_HOSTNAME - hostname from the active pool
2665 * ZPOOL_CONFIG_MMP_HOSTID - hostid from the active pool
2667 if (error
== EREMOTEIO
) {
2668 char *hostname
= "<unknown>";
2669 uint64_t hostid
= 0;
2672 if (nvlist_exists(mmp_label
, ZPOOL_CONFIG_HOSTNAME
)) {
2673 hostname
= fnvlist_lookup_string(mmp_label
,
2674 ZPOOL_CONFIG_HOSTNAME
);
2675 fnvlist_add_string(spa
->spa_load_info
,
2676 ZPOOL_CONFIG_MMP_HOSTNAME
, hostname
);
2679 if (nvlist_exists(mmp_label
, ZPOOL_CONFIG_HOSTID
)) {
2680 hostid
= fnvlist_lookup_uint64(mmp_label
,
2681 ZPOOL_CONFIG_HOSTID
);
2682 fnvlist_add_uint64(spa
->spa_load_info
,
2683 ZPOOL_CONFIG_MMP_HOSTID
, hostid
);
2687 fnvlist_add_uint64(spa
->spa_load_info
,
2688 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_ACTIVE
);
2689 fnvlist_add_uint64(spa
->spa_load_info
,
2690 ZPOOL_CONFIG_MMP_TXG
, 0);
2692 error
= spa_vdev_err(rvd
, VDEV_AUX_ACTIVE
, EREMOTEIO
);
2696 nvlist_free(mmp_label
);
2702 spa_verify_host(spa_t
*spa
, nvlist_t
*mos_config
)
2706 uint64_t myhostid
= 0;
2708 if (!spa_is_root(spa
) && nvlist_lookup_uint64(mos_config
,
2709 ZPOOL_CONFIG_HOSTID
, &hostid
) == 0) {
2710 hostname
= fnvlist_lookup_string(mos_config
,
2711 ZPOOL_CONFIG_HOSTNAME
);
2713 myhostid
= zone_get_hostid(NULL
);
2715 if (hostid
!= 0 && myhostid
!= 0 && hostid
!= myhostid
) {
2716 cmn_err(CE_WARN
, "pool '%s' could not be "
2717 "loaded as it was last accessed by "
2718 "another system (host: %s hostid: 0x%llx). "
2719 "See: http://illumos.org/msg/ZFS-8000-EY",
2720 spa_name(spa
), hostname
, (u_longlong_t
)hostid
);
2721 spa_load_failed(spa
, "hostid verification failed: pool "
2722 "last accessed by host: %s (hostid: 0x%llx)",
2723 hostname
, (u_longlong_t
)hostid
);
2724 return (SET_ERROR(EBADF
));
2732 spa_ld_parse_config(spa_t
*spa
, spa_import_type_t type
)
2735 nvlist_t
*nvtree
, *nvl
, *config
= spa
->spa_config
;
2742 * Versioning wasn't explicitly added to the label until later, so if
2743 * it's not present treat it as the initial version.
2745 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
2746 &spa
->spa_ubsync
.ub_version
) != 0)
2747 spa
->spa_ubsync
.ub_version
= SPA_VERSION_INITIAL
;
2749 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
, &pool_guid
)) {
2750 spa_load_failed(spa
, "invalid config provided: '%s' missing",
2751 ZPOOL_CONFIG_POOL_GUID
);
2752 return (SET_ERROR(EINVAL
));
2756 * If we are doing an import, ensure that the pool is not already
2757 * imported by checking if its pool guid already exists in the
2760 * The only case that we allow an already imported pool to be
2761 * imported again, is when the pool is checkpointed and we want to
2762 * look at its checkpointed state from userland tools like zdb.
2765 if ((spa
->spa_load_state
== SPA_LOAD_IMPORT
||
2766 spa
->spa_load_state
== SPA_LOAD_TRYIMPORT
) &&
2767 spa_guid_exists(pool_guid
, 0)) {
2769 if ((spa
->spa_load_state
== SPA_LOAD_IMPORT
||
2770 spa
->spa_load_state
== SPA_LOAD_TRYIMPORT
) &&
2771 spa_guid_exists(pool_guid
, 0) &&
2772 !spa_importing_readonly_checkpoint(spa
)) {
2774 spa_load_failed(spa
, "a pool with guid %llu is already open",
2775 (u_longlong_t
)pool_guid
);
2776 return (SET_ERROR(EEXIST
));
2779 spa
->spa_config_guid
= pool_guid
;
2781 nvlist_free(spa
->spa_load_info
);
2782 spa
->spa_load_info
= fnvlist_alloc();
2784 ASSERT(spa
->spa_comment
== NULL
);
2785 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
2786 spa
->spa_comment
= spa_strdup(comment
);
2788 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
2789 &spa
->spa_config_txg
);
2791 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
, &nvl
) == 0)
2792 spa
->spa_config_splitting
= fnvlist_dup(nvl
);
2794 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvtree
)) {
2795 spa_load_failed(spa
, "invalid config provided: '%s' missing",
2796 ZPOOL_CONFIG_VDEV_TREE
);
2797 return (SET_ERROR(EINVAL
));
2801 * Create "The Godfather" zio to hold all async IOs
2803 spa
->spa_async_zio_root
= kmem_alloc(max_ncpus
* sizeof (void *),
2805 for (int i
= 0; i
< max_ncpus
; i
++) {
2806 spa
->spa_async_zio_root
[i
] = zio_root(spa
, NULL
, NULL
,
2807 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
2808 ZIO_FLAG_GODFATHER
);
2812 * Parse the configuration into a vdev tree. We explicitly set the
2813 * value that will be returned by spa_version() since parsing the
2814 * configuration requires knowing the version number.
2816 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2817 parse
= (type
== SPA_IMPORT_EXISTING
?
2818 VDEV_ALLOC_LOAD
: VDEV_ALLOC_SPLIT
);
2819 error
= spa_config_parse(spa
, &rvd
, nvtree
, NULL
, 0, parse
);
2820 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2823 spa_load_failed(spa
, "unable to parse config [error=%d]",
2828 ASSERT(spa
->spa_root_vdev
== rvd
);
2829 ASSERT3U(spa
->spa_min_ashift
, >=, SPA_MINBLOCKSHIFT
);
2830 ASSERT3U(spa
->spa_max_ashift
, <=, SPA_MAXBLOCKSHIFT
);
2832 if (type
!= SPA_IMPORT_ASSEMBLE
) {
2833 ASSERT(spa_guid(spa
) == pool_guid
);
2840 * Recursively open all vdevs in the vdev tree. This function is called twice:
2841 * first with the untrusted config, then with the trusted config.
2844 spa_ld_open_vdevs(spa_t
*spa
)
2849 * spa_missing_tvds_allowed defines how many top-level vdevs can be
2850 * missing/unopenable for the root vdev to be still considered openable.
2852 if (spa
->spa_trust_config
) {
2853 spa
->spa_missing_tvds_allowed
= zfs_max_missing_tvds
;
2854 } else if (spa
->spa_config_source
== SPA_CONFIG_SRC_CACHEFILE
) {
2855 spa
->spa_missing_tvds_allowed
= zfs_max_missing_tvds_cachefile
;
2856 } else if (spa
->spa_config_source
== SPA_CONFIG_SRC_SCAN
) {
2857 spa
->spa_missing_tvds_allowed
= zfs_max_missing_tvds_scan
;
2859 spa
->spa_missing_tvds_allowed
= 0;
2862 spa
->spa_missing_tvds_allowed
=
2863 MAX(zfs_max_missing_tvds
, spa
->spa_missing_tvds_allowed
);
2865 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2866 error
= vdev_open(spa
->spa_root_vdev
);
2867 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2869 if (spa
->spa_missing_tvds
!= 0) {
2870 spa_load_note(spa
, "vdev tree has %lld missing top-level "
2871 "vdevs.", (u_longlong_t
)spa
->spa_missing_tvds
);
2872 if (spa
->spa_trust_config
&& (spa
->spa_mode
& FWRITE
)) {
2874 * Although theoretically we could allow users to open
2875 * incomplete pools in RW mode, we'd need to add a lot
2876 * of extra logic (e.g. adjust pool space to account
2877 * for missing vdevs).
2878 * This limitation also prevents users from accidentally
2879 * opening the pool in RW mode during data recovery and
2880 * damaging it further.
2882 spa_load_note(spa
, "pools with missing top-level "
2883 "vdevs can only be opened in read-only mode.");
2884 error
= SET_ERROR(ENXIO
);
2886 spa_load_note(spa
, "current settings allow for maximum "
2887 "%lld missing top-level vdevs at this stage.",
2888 (u_longlong_t
)spa
->spa_missing_tvds_allowed
);
2892 spa_load_failed(spa
, "unable to open vdev tree [error=%d]",
2895 if (spa
->spa_missing_tvds
!= 0 || error
!= 0)
2896 vdev_dbgmsg_print_tree(spa
->spa_root_vdev
, 2);
2902 * We need to validate the vdev labels against the configuration that
2903 * we have in hand. This function is called twice: first with an untrusted
2904 * config, then with a trusted config. The validation is more strict when the
2905 * config is trusted.
2908 spa_ld_validate_vdevs(spa_t
*spa
)
2911 vdev_t
*rvd
= spa
->spa_root_vdev
;
2913 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2914 error
= vdev_validate(rvd
);
2915 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2918 spa_load_failed(spa
, "vdev_validate failed [error=%d]", error
);
2922 if (rvd
->vdev_state
<= VDEV_STATE_CANT_OPEN
) {
2923 spa_load_failed(spa
, "cannot open vdev tree after invalidating "
2925 vdev_dbgmsg_print_tree(rvd
, 2);
2926 return (SET_ERROR(ENXIO
));
2933 spa_ld_select_uberblock_done(spa_t
*spa
, uberblock_t
*ub
)
2935 spa
->spa_state
= POOL_STATE_ACTIVE
;
2936 spa
->spa_ubsync
= spa
->spa_uberblock
;
2937 spa
->spa_verify_min_txg
= spa
->spa_extreme_rewind
?
2938 TXG_INITIAL
- 1 : spa_last_synced_txg(spa
) - TXG_DEFER_SIZE
- 1;
2939 spa
->spa_first_txg
= spa
->spa_last_ubsync_txg
?
2940 spa
->spa_last_ubsync_txg
: spa_last_synced_txg(spa
) + 1;
2941 spa
->spa_claim_max_txg
= spa
->spa_first_txg
;
2942 spa
->spa_prev_software_version
= ub
->ub_software_version
;
2946 spa_ld_select_uberblock(spa_t
*spa
, spa_import_type_t type
)
2948 vdev_t
*rvd
= spa
->spa_root_vdev
;
2950 uberblock_t
*ub
= &spa
->spa_uberblock
;
2951 boolean_t activity_check
= B_FALSE
;
2954 * If we are opening the checkpointed state of the pool by
2955 * rewinding to it, at this point we will have written the
2956 * checkpointed uberblock to the vdev labels, so searching
2957 * the labels will find the right uberblock. However, if
2958 * we are opening the checkpointed state read-only, we have
2959 * not modified the labels. Therefore, we must ignore the
2960 * labels and continue using the spa_uberblock that was set
2961 * by spa_ld_checkpoint_rewind.
2963 * Note that it would be fine to ignore the labels when
2964 * rewinding (opening writeable) as well. However, if we
2965 * crash just after writing the labels, we will end up
2966 * searching the labels. Doing so in the common case means
2967 * that this code path gets exercised normally, rather than
2968 * just in the edge case.
2970 if (ub
->ub_checkpoint_txg
!= 0 &&
2971 spa_importing_readonly_checkpoint(spa
)) {
2972 spa_ld_select_uberblock_done(spa
, ub
);
2977 * Find the best uberblock.
2979 vdev_uberblock_load(rvd
, ub
, &label
);
2982 * If we weren't able to find a single valid uberblock, return failure.
2984 if (ub
->ub_txg
== 0) {
2986 spa_load_failed(spa
, "no valid uberblock found");
2987 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, ENXIO
));
2990 spa_load_note(spa
, "using uberblock with txg=%llu",
2991 (u_longlong_t
)ub
->ub_txg
);
2995 * For pools which have the multihost property on determine if the
2996 * pool is truly inactive and can be safely imported. Prevent
2997 * hosts which don't have a hostid set from importing the pool.
2999 activity_check
= spa_activity_check_required(spa
, ub
, label
,
3001 if (activity_check
) {
3002 if (ub
->ub_mmp_magic
== MMP_MAGIC
&& ub
->ub_mmp_delay
&&
3003 spa_get_hostid() == 0) {
3005 fnvlist_add_uint64(spa
->spa_load_info
,
3006 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_NO_HOSTID
);
3007 return (spa_vdev_err(rvd
, VDEV_AUX_ACTIVE
, EREMOTEIO
));
3010 int error
= spa_activity_check(spa
, ub
, spa
->spa_config
);
3016 fnvlist_add_uint64(spa
->spa_load_info
,
3017 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_INACTIVE
);
3018 fnvlist_add_uint64(spa
->spa_load_info
,
3019 ZPOOL_CONFIG_MMP_TXG
, ub
->ub_txg
);
3020 fnvlist_add_uint16(spa
->spa_load_info
,
3021 ZPOOL_CONFIG_MMP_SEQ
,
3022 (MMP_SEQ_VALID(ub
) ? MMP_SEQ(ub
) : 0));
3026 * If the pool has an unsupported version we can't open it.
3028 if (!SPA_VERSION_IS_SUPPORTED(ub
->ub_version
)) {
3030 spa_load_failed(spa
, "version %llu is not supported",
3031 (u_longlong_t
)ub
->ub_version
);
3032 return (spa_vdev_err(rvd
, VDEV_AUX_VERSION_NEWER
, ENOTSUP
));
3035 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
3039 * If we weren't able to find what's necessary for reading the
3040 * MOS in the label, return failure.
3042 if (label
== NULL
) {
3043 spa_load_failed(spa
, "label config unavailable");
3044 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
3048 if (nvlist_lookup_nvlist(label
, ZPOOL_CONFIG_FEATURES_FOR_READ
,
3051 spa_load_failed(spa
, "invalid label: '%s' missing",
3052 ZPOOL_CONFIG_FEATURES_FOR_READ
);
3053 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
3058 * Update our in-core representation with the definitive values
3061 nvlist_free(spa
->spa_label_features
);
3062 VERIFY(nvlist_dup(features
, &spa
->spa_label_features
, 0) == 0);
3068 * Look through entries in the label nvlist's features_for_read. If
3069 * there is a feature listed there which we don't understand then we
3070 * cannot open a pool.
3072 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
3073 nvlist_t
*unsup_feat
;
3075 VERIFY(nvlist_alloc(&unsup_feat
, NV_UNIQUE_NAME
, KM_SLEEP
) ==
3078 for (nvpair_t
*nvp
= nvlist_next_nvpair(spa
->spa_label_features
,
3080 nvp
= nvlist_next_nvpair(spa
->spa_label_features
, nvp
)) {
3081 if (!zfeature_is_supported(nvpair_name(nvp
))) {
3082 VERIFY(nvlist_add_string(unsup_feat
,
3083 nvpair_name(nvp
), "") == 0);
3087 if (!nvlist_empty(unsup_feat
)) {
3088 VERIFY(nvlist_add_nvlist(spa
->spa_load_info
,
3089 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
) == 0);
3090 nvlist_free(unsup_feat
);
3091 spa_load_failed(spa
, "some features are unsupported");
3092 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
3096 nvlist_free(unsup_feat
);
3099 if (type
!= SPA_IMPORT_ASSEMBLE
&& spa
->spa_config_splitting
) {
3100 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3101 spa_try_repair(spa
, spa
->spa_config
);
3102 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3103 nvlist_free(spa
->spa_config_splitting
);
3104 spa
->spa_config_splitting
= NULL
;
3108 * Initialize internal SPA structures.
3110 spa_ld_select_uberblock_done(spa
, ub
);
3116 spa_ld_open_rootbp(spa_t
*spa
)
3119 vdev_t
*rvd
= spa
->spa_root_vdev
;
3121 error
= dsl_pool_init(spa
, spa
->spa_first_txg
, &spa
->spa_dsl_pool
);
3123 spa_load_failed(spa
, "unable to open rootbp in dsl_pool_init "
3124 "[error=%d]", error
);
3125 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3127 spa
->spa_meta_objset
= spa
->spa_dsl_pool
->dp_meta_objset
;
3133 spa_ld_trusted_config(spa_t
*spa
, spa_import_type_t type
,
3134 boolean_t reloading
)
3136 vdev_t
*mrvd
, *rvd
= spa
->spa_root_vdev
;
3137 nvlist_t
*nv
, *mos_config
, *policy
;
3138 int error
= 0, copy_error
;
3139 uint64_t healthy_tvds
, healthy_tvds_mos
;
3140 uint64_t mos_config_txg
;
3142 if (spa_dir_prop(spa
, DMU_POOL_CONFIG
, &spa
->spa_config_object
, B_TRUE
)
3144 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3147 * If we're assembling a pool from a split, the config provided is
3148 * already trusted so there is nothing to do.
3150 if (type
== SPA_IMPORT_ASSEMBLE
)
3153 healthy_tvds
= spa_healthy_core_tvds(spa
);
3155 if (load_nvlist(spa
, spa
->spa_config_object
, &mos_config
)
3157 spa_load_failed(spa
, "unable to retrieve MOS config");
3158 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3162 * If we are doing an open, pool owner wasn't verified yet, thus do
3163 * the verification here.
3165 if (spa
->spa_load_state
== SPA_LOAD_OPEN
) {
3166 error
= spa_verify_host(spa
, mos_config
);
3168 nvlist_free(mos_config
);
3173 nv
= fnvlist_lookup_nvlist(mos_config
, ZPOOL_CONFIG_VDEV_TREE
);
3175 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3178 * Build a new vdev tree from the trusted config
3180 VERIFY(spa_config_parse(spa
, &mrvd
, nv
, NULL
, 0, VDEV_ALLOC_LOAD
) == 0);
3183 * Vdev paths in the MOS may be obsolete. If the untrusted config was
3184 * obtained by scanning /dev/dsk, then it will have the right vdev
3185 * paths. We update the trusted MOS config with this information.
3186 * We first try to copy the paths with vdev_copy_path_strict, which
3187 * succeeds only when both configs have exactly the same vdev tree.
3188 * If that fails, we fall back to a more flexible method that has a
3189 * best effort policy.
3191 copy_error
= vdev_copy_path_strict(rvd
, mrvd
);
3192 if (copy_error
!= 0 || spa_load_print_vdev_tree
) {
3193 spa_load_note(spa
, "provided vdev tree:");
3194 vdev_dbgmsg_print_tree(rvd
, 2);
3195 spa_load_note(spa
, "MOS vdev tree:");
3196 vdev_dbgmsg_print_tree(mrvd
, 2);
3198 if (copy_error
!= 0) {
3199 spa_load_note(spa
, "vdev_copy_path_strict failed, falling "
3200 "back to vdev_copy_path_relaxed");
3201 vdev_copy_path_relaxed(rvd
, mrvd
);
3206 spa
->spa_root_vdev
= mrvd
;
3208 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3211 * We will use spa_config if we decide to reload the spa or if spa_load
3212 * fails and we rewind. We must thus regenerate the config using the
3213 * MOS information with the updated paths. ZPOOL_LOAD_POLICY is used to
3214 * pass settings on how to load the pool and is not stored in the MOS.
3215 * We copy it over to our new, trusted config.
3217 mos_config_txg
= fnvlist_lookup_uint64(mos_config
,
3218 ZPOOL_CONFIG_POOL_TXG
);
3219 nvlist_free(mos_config
);
3220 mos_config
= spa_config_generate(spa
, NULL
, mos_config_txg
, B_FALSE
);
3221 if (nvlist_lookup_nvlist(spa
->spa_config
, ZPOOL_LOAD_POLICY
,
3223 fnvlist_add_nvlist(mos_config
, ZPOOL_LOAD_POLICY
, policy
);
3224 spa_config_set(spa
, mos_config
);
3225 spa
->spa_config_source
= SPA_CONFIG_SRC_MOS
;
3228 * Now that we got the config from the MOS, we should be more strict
3229 * in checking blkptrs and can make assumptions about the consistency
3230 * of the vdev tree. spa_trust_config must be set to true before opening
3231 * vdevs in order for them to be writeable.
3233 spa
->spa_trust_config
= B_TRUE
;
3236 * Open and validate the new vdev tree
3238 error
= spa_ld_open_vdevs(spa
);
3242 error
= spa_ld_validate_vdevs(spa
);
3246 if (copy_error
!= 0 || spa_load_print_vdev_tree
) {
3247 spa_load_note(spa
, "final vdev tree:");
3248 vdev_dbgmsg_print_tree(rvd
, 2);
3251 if (spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
&&
3252 !spa
->spa_extreme_rewind
&& zfs_max_missing_tvds
== 0) {
3254 * Sanity check to make sure that we are indeed loading the
3255 * latest uberblock. If we missed SPA_SYNC_MIN_VDEVS tvds
3256 * in the config provided and they happened to be the only ones
3257 * to have the latest uberblock, we could involuntarily perform
3258 * an extreme rewind.
3260 healthy_tvds_mos
= spa_healthy_core_tvds(spa
);
3261 if (healthy_tvds_mos
- healthy_tvds
>=
3262 SPA_SYNC_MIN_VDEVS
) {
3263 spa_load_note(spa
, "config provided misses too many "
3264 "top-level vdevs compared to MOS (%lld vs %lld). ",
3265 (u_longlong_t
)healthy_tvds
,
3266 (u_longlong_t
)healthy_tvds_mos
);
3267 spa_load_note(spa
, "vdev tree:");
3268 vdev_dbgmsg_print_tree(rvd
, 2);
3270 spa_load_failed(spa
, "config was already "
3271 "provided from MOS. Aborting.");
3272 return (spa_vdev_err(rvd
,
3273 VDEV_AUX_CORRUPT_DATA
, EIO
));
3275 spa_load_note(spa
, "spa must be reloaded using MOS "
3277 return (SET_ERROR(EAGAIN
));
3281 error
= spa_check_for_missing_logs(spa
);
3283 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
, ENXIO
));
3285 if (rvd
->vdev_guid_sum
!= spa
->spa_uberblock
.ub_guid_sum
) {
3286 spa_load_failed(spa
, "uberblock guid sum doesn't match MOS "
3287 "guid sum (%llu != %llu)",
3288 (u_longlong_t
)spa
->spa_uberblock
.ub_guid_sum
,
3289 (u_longlong_t
)rvd
->vdev_guid_sum
);
3290 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
,
3298 spa_ld_open_indirect_vdev_metadata(spa_t
*spa
)
3301 vdev_t
*rvd
= spa
->spa_root_vdev
;
3304 * Everything that we read before spa_remove_init() must be stored
3305 * on concreted vdevs. Therefore we do this as early as possible.
3307 error
= spa_remove_init(spa
);
3309 spa_load_failed(spa
, "spa_remove_init failed [error=%d]",
3311 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3315 * Retrieve information needed to condense indirect vdev mappings.
3317 error
= spa_condense_init(spa
);
3319 spa_load_failed(spa
, "spa_condense_init failed [error=%d]",
3321 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, error
));
3328 spa_ld_check_features(spa_t
*spa
, boolean_t
*missing_feat_writep
)
3331 vdev_t
*rvd
= spa
->spa_root_vdev
;
3333 if (spa_version(spa
) >= SPA_VERSION_FEATURES
) {
3334 boolean_t missing_feat_read
= B_FALSE
;
3335 nvlist_t
*unsup_feat
, *enabled_feat
;
3337 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_READ
,
3338 &spa
->spa_feat_for_read_obj
, B_TRUE
) != 0) {
3339 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3342 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_WRITE
,
3343 &spa
->spa_feat_for_write_obj
, B_TRUE
) != 0) {
3344 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3347 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_DESCRIPTIONS
,
3348 &spa
->spa_feat_desc_obj
, B_TRUE
) != 0) {
3349 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3352 enabled_feat
= fnvlist_alloc();
3353 unsup_feat
= fnvlist_alloc();
3355 if (!spa_features_check(spa
, B_FALSE
,
3356 unsup_feat
, enabled_feat
))
3357 missing_feat_read
= B_TRUE
;
3359 if (spa_writeable(spa
) ||
3360 spa
->spa_load_state
== SPA_LOAD_TRYIMPORT
) {
3361 if (!spa_features_check(spa
, B_TRUE
,
3362 unsup_feat
, enabled_feat
)) {
3363 *missing_feat_writep
= B_TRUE
;
3367 fnvlist_add_nvlist(spa
->spa_load_info
,
3368 ZPOOL_CONFIG_ENABLED_FEAT
, enabled_feat
);
3370 if (!nvlist_empty(unsup_feat
)) {
3371 fnvlist_add_nvlist(spa
->spa_load_info
,
3372 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
);
3375 fnvlist_free(enabled_feat
);
3376 fnvlist_free(unsup_feat
);
3378 if (!missing_feat_read
) {
3379 fnvlist_add_boolean(spa
->spa_load_info
,
3380 ZPOOL_CONFIG_CAN_RDONLY
);
3384 * If the state is SPA_LOAD_TRYIMPORT, our objective is
3385 * twofold: to determine whether the pool is available for
3386 * import in read-write mode and (if it is not) whether the
3387 * pool is available for import in read-only mode. If the pool
3388 * is available for import in read-write mode, it is displayed
3389 * as available in userland; if it is not available for import
3390 * in read-only mode, it is displayed as unavailable in
3391 * userland. If the pool is available for import in read-only
3392 * mode but not read-write mode, it is displayed as unavailable
3393 * in userland with a special note that the pool is actually
3394 * available for open in read-only mode.
3396 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
3397 * missing a feature for write, we must first determine whether
3398 * the pool can be opened read-only before returning to
3399 * userland in order to know whether to display the
3400 * abovementioned note.
3402 if (missing_feat_read
|| (*missing_feat_writep
&&
3403 spa_writeable(spa
))) {
3404 spa_load_failed(spa
, "pool uses unsupported features");
3405 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
3410 * Load refcounts for ZFS features from disk into an in-memory
3411 * cache during SPA initialization.
3413 for (spa_feature_t i
= 0; i
< SPA_FEATURES
; i
++) {
3416 error
= feature_get_refcount_from_disk(spa
,
3417 &spa_feature_table
[i
], &refcount
);
3419 spa
->spa_feat_refcount_cache
[i
] = refcount
;
3420 } else if (error
== ENOTSUP
) {
3421 spa
->spa_feat_refcount_cache
[i
] =
3422 SPA_FEATURE_DISABLED
;
3424 spa_load_failed(spa
, "error getting refcount "
3425 "for feature %s [error=%d]",
3426 spa_feature_table
[i
].fi_guid
, error
);
3427 return (spa_vdev_err(rvd
,
3428 VDEV_AUX_CORRUPT_DATA
, EIO
));
3433 if (spa_feature_is_active(spa
, SPA_FEATURE_ENABLED_TXG
)) {
3434 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_ENABLED_TXG
,
3435 &spa
->spa_feat_enabled_txg_obj
, B_TRUE
) != 0)
3436 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3440 * Encryption was added before bookmark_v2, even though bookmark_v2
3441 * is now a dependency. If this pool has encryption enabled without
3442 * bookmark_v2, trigger an errata message.
3444 if (spa_feature_is_enabled(spa
, SPA_FEATURE_ENCRYPTION
) &&
3445 !spa_feature_is_enabled(spa
, SPA_FEATURE_BOOKMARK_V2
)) {
3446 spa
->spa_errata
= ZPOOL_ERRATA_ZOL_8308_ENCRYPTION
;
3453 spa_ld_load_special_directories(spa_t
*spa
)
3456 vdev_t
*rvd
= spa
->spa_root_vdev
;
3458 spa
->spa_is_initializing
= B_TRUE
;
3459 error
= dsl_pool_open(spa
->spa_dsl_pool
);
3460 spa
->spa_is_initializing
= B_FALSE
;
3462 spa_load_failed(spa
, "dsl_pool_open failed [error=%d]", error
);
3463 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3470 spa_ld_get_props(spa_t
*spa
)
3474 vdev_t
*rvd
= spa
->spa_root_vdev
;
3476 /* Grab the checksum salt from the MOS. */
3477 error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
3478 DMU_POOL_CHECKSUM_SALT
, 1,
3479 sizeof (spa
->spa_cksum_salt
.zcs_bytes
),
3480 spa
->spa_cksum_salt
.zcs_bytes
);
3481 if (error
== ENOENT
) {
3482 /* Generate a new salt for subsequent use */
3483 (void) random_get_pseudo_bytes(spa
->spa_cksum_salt
.zcs_bytes
,
3484 sizeof (spa
->spa_cksum_salt
.zcs_bytes
));
3485 } else if (error
!= 0) {
3486 spa_load_failed(spa
, "unable to retrieve checksum salt from "
3487 "MOS [error=%d]", error
);
3488 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3491 if (spa_dir_prop(spa
, DMU_POOL_SYNC_BPOBJ
, &obj
, B_TRUE
) != 0)
3492 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3493 error
= bpobj_open(&spa
->spa_deferred_bpobj
, spa
->spa_meta_objset
, obj
);
3495 spa_load_failed(spa
, "error opening deferred-frees bpobj "
3496 "[error=%d]", error
);
3497 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3501 * Load the bit that tells us to use the new accounting function
3502 * (raid-z deflation). If we have an older pool, this will not
3505 error
= spa_dir_prop(spa
, DMU_POOL_DEFLATE
, &spa
->spa_deflate
, B_FALSE
);
3506 if (error
!= 0 && error
!= ENOENT
)
3507 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3509 error
= spa_dir_prop(spa
, DMU_POOL_CREATION_VERSION
,
3510 &spa
->spa_creation_version
, B_FALSE
);
3511 if (error
!= 0 && error
!= ENOENT
)
3512 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3515 * Load the persistent error log. If we have an older pool, this will
3518 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_LAST
, &spa
->spa_errlog_last
,
3520 if (error
!= 0 && error
!= ENOENT
)
3521 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3523 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_SCRUB
,
3524 &spa
->spa_errlog_scrub
, B_FALSE
);
3525 if (error
!= 0 && error
!= ENOENT
)
3526 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3529 * Load the history object. If we have an older pool, this
3530 * will not be present.
3532 error
= spa_dir_prop(spa
, DMU_POOL_HISTORY
, &spa
->spa_history
, B_FALSE
);
3533 if (error
!= 0 && error
!= ENOENT
)
3534 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3537 * Load the per-vdev ZAP map. If we have an older pool, this will not
3538 * be present; in this case, defer its creation to a later time to
3539 * avoid dirtying the MOS this early / out of sync context. See
3540 * spa_sync_config_object.
3543 /* The sentinel is only available in the MOS config. */
3544 nvlist_t
*mos_config
;
3545 if (load_nvlist(spa
, spa
->spa_config_object
, &mos_config
) != 0) {
3546 spa_load_failed(spa
, "unable to retrieve MOS config");
3547 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3550 error
= spa_dir_prop(spa
, DMU_POOL_VDEV_ZAP_MAP
,
3551 &spa
->spa_all_vdev_zaps
, B_FALSE
);
3553 if (error
== ENOENT
) {
3554 VERIFY(!nvlist_exists(mos_config
,
3555 ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
));
3556 spa
->spa_avz_action
= AVZ_ACTION_INITIALIZE
;
3557 ASSERT0(vdev_count_verify_zaps(spa
->spa_root_vdev
));
3558 } else if (error
!= 0) {
3559 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3560 } else if (!nvlist_exists(mos_config
, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
)) {
3562 * An older version of ZFS overwrote the sentinel value, so
3563 * we have orphaned per-vdev ZAPs in the MOS. Defer their
3564 * destruction to later; see spa_sync_config_object.
3566 spa
->spa_avz_action
= AVZ_ACTION_DESTROY
;
3568 * We're assuming that no vdevs have had their ZAPs created
3569 * before this. Better be sure of it.
3571 ASSERT0(vdev_count_verify_zaps(spa
->spa_root_vdev
));
3573 nvlist_free(mos_config
);
3575 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
3577 error
= spa_dir_prop(spa
, DMU_POOL_PROPS
, &spa
->spa_pool_props_object
,
3579 if (error
&& error
!= ENOENT
)
3580 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3583 uint64_t autoreplace
;
3585 spa_prop_find(spa
, ZPOOL_PROP_BOOTFS
, &spa
->spa_bootfs
);
3586 spa_prop_find(spa
, ZPOOL_PROP_AUTOREPLACE
, &autoreplace
);
3587 spa_prop_find(spa
, ZPOOL_PROP_DELEGATION
, &spa
->spa_delegation
);
3588 spa_prop_find(spa
, ZPOOL_PROP_FAILUREMODE
, &spa
->spa_failmode
);
3589 spa_prop_find(spa
, ZPOOL_PROP_AUTOEXPAND
, &spa
->spa_autoexpand
);
3590 spa_prop_find(spa
, ZPOOL_PROP_MULTIHOST
, &spa
->spa_multihost
);
3591 spa_prop_find(spa
, ZPOOL_PROP_DEDUPDITTO
,
3592 &spa
->spa_dedup_ditto
);
3593 spa_prop_find(spa
, ZPOOL_PROP_AUTOTRIM
, &spa
->spa_autotrim
);
3594 spa
->spa_autoreplace
= (autoreplace
!= 0);
3598 * If we are importing a pool with missing top-level vdevs,
3599 * we enforce that the pool doesn't panic or get suspended on
3600 * error since the likelihood of missing data is extremely high.
3602 if (spa
->spa_missing_tvds
> 0 &&
3603 spa
->spa_failmode
!= ZIO_FAILURE_MODE_CONTINUE
&&
3604 spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
) {
3605 spa_load_note(spa
, "forcing failmode to 'continue' "
3606 "as some top level vdevs are missing");
3607 spa
->spa_failmode
= ZIO_FAILURE_MODE_CONTINUE
;
3614 spa_ld_open_aux_vdevs(spa_t
*spa
, spa_import_type_t type
)
3617 vdev_t
*rvd
= spa
->spa_root_vdev
;
3620 * If we're assembling the pool from the split-off vdevs of
3621 * an existing pool, we don't want to attach the spares & cache
3626 * Load any hot spares for this pool.
3628 error
= spa_dir_prop(spa
, DMU_POOL_SPARES
, &spa
->spa_spares
.sav_object
,
3630 if (error
!= 0 && error
!= ENOENT
)
3631 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3632 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
3633 ASSERT(spa_version(spa
) >= SPA_VERSION_SPARES
);
3634 if (load_nvlist(spa
, spa
->spa_spares
.sav_object
,
3635 &spa
->spa_spares
.sav_config
) != 0) {
3636 spa_load_failed(spa
, "error loading spares nvlist");
3637 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3640 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3641 spa_load_spares(spa
);
3642 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3643 } else if (error
== 0) {
3644 spa
->spa_spares
.sav_sync
= B_TRUE
;
3648 * Load any level 2 ARC devices for this pool.
3650 error
= spa_dir_prop(spa
, DMU_POOL_L2CACHE
,
3651 &spa
->spa_l2cache
.sav_object
, B_FALSE
);
3652 if (error
!= 0 && error
!= ENOENT
)
3653 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3654 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
3655 ASSERT(spa_version(spa
) >= SPA_VERSION_L2CACHE
);
3656 if (load_nvlist(spa
, spa
->spa_l2cache
.sav_object
,
3657 &spa
->spa_l2cache
.sav_config
) != 0) {
3658 spa_load_failed(spa
, "error loading l2cache nvlist");
3659 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3662 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3663 spa_load_l2cache(spa
);
3664 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3665 } else if (error
== 0) {
3666 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
3673 spa_ld_load_vdev_metadata(spa_t
*spa
)
3676 vdev_t
*rvd
= spa
->spa_root_vdev
;
3679 * If the 'multihost' property is set, then never allow a pool to
3680 * be imported when the system hostid is zero. The exception to
3681 * this rule is zdb which is always allowed to access pools.
3683 if (spa_multihost(spa
) && spa_get_hostid() == 0 &&
3684 (spa
->spa_import_flags
& ZFS_IMPORT_SKIP_MMP
) == 0) {
3685 fnvlist_add_uint64(spa
->spa_load_info
,
3686 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_NO_HOSTID
);
3687 return (spa_vdev_err(rvd
, VDEV_AUX_ACTIVE
, EREMOTEIO
));
3691 * If the 'autoreplace' property is set, then post a resource notifying
3692 * the ZFS DE that it should not issue any faults for unopenable
3693 * devices. We also iterate over the vdevs, and post a sysevent for any
3694 * unopenable vdevs so that the normal autoreplace handler can take
3697 if (spa
->spa_autoreplace
&& spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
) {
3698 spa_check_removed(spa
->spa_root_vdev
);
3700 * For the import case, this is done in spa_import(), because
3701 * at this point we're using the spare definitions from
3702 * the MOS config, not necessarily from the userland config.
3704 if (spa
->spa_load_state
!= SPA_LOAD_IMPORT
) {
3705 spa_aux_check_removed(&spa
->spa_spares
);
3706 spa_aux_check_removed(&spa
->spa_l2cache
);
3711 * Load the vdev metadata such as metaslabs, DTLs, spacemap object, etc.
3713 error
= vdev_load(rvd
);
3715 spa_load_failed(spa
, "vdev_load failed [error=%d]", error
);
3716 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, error
));
3720 * Propagate the leaf DTLs we just loaded all the way up the vdev tree.
3722 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3723 vdev_dtl_reassess(rvd
, 0, 0, B_FALSE
);
3724 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3730 spa_ld_load_dedup_tables(spa_t
*spa
)
3733 vdev_t
*rvd
= spa
->spa_root_vdev
;
3735 error
= ddt_load(spa
);
3737 spa_load_failed(spa
, "ddt_load failed [error=%d]", error
);
3738 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3745 spa_ld_verify_logs(spa_t
*spa
, spa_import_type_t type
, char **ereport
)
3747 vdev_t
*rvd
= spa
->spa_root_vdev
;
3749 if (type
!= SPA_IMPORT_ASSEMBLE
&& spa_writeable(spa
)) {
3750 boolean_t missing
= spa_check_logs(spa
);
3752 if (spa
->spa_missing_tvds
!= 0) {
3753 spa_load_note(spa
, "spa_check_logs failed "
3754 "so dropping the logs");
3756 *ereport
= FM_EREPORT_ZFS_LOG_REPLAY
;
3757 spa_load_failed(spa
, "spa_check_logs failed");
3758 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_LOG
,
3768 spa_ld_verify_pool_data(spa_t
*spa
)
3771 vdev_t
*rvd
= spa
->spa_root_vdev
;
3774 * We've successfully opened the pool, verify that we're ready
3775 * to start pushing transactions.
3777 if (spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
) {
3778 error
= spa_load_verify(spa
);
3780 spa_load_failed(spa
, "spa_load_verify failed "
3781 "[error=%d]", error
);
3782 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
3791 spa_ld_claim_log_blocks(spa_t
*spa
)
3794 dsl_pool_t
*dp
= spa_get_dsl(spa
);
3797 * Claim log blocks that haven't been committed yet.
3798 * This must all happen in a single txg.
3799 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
3800 * invoked from zil_claim_log_block()'s i/o done callback.
3801 * Price of rollback is that we abandon the log.
3803 spa
->spa_claiming
= B_TRUE
;
3805 tx
= dmu_tx_create_assigned(dp
, spa_first_txg(spa
));
3806 (void) dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
3807 zil_claim
, tx
, DS_FIND_CHILDREN
);
3810 spa
->spa_claiming
= B_FALSE
;
3812 spa_set_log_state(spa
, SPA_LOG_GOOD
);
3816 spa_ld_check_for_config_update(spa_t
*spa
, uint64_t config_cache_txg
,
3817 boolean_t update_config_cache
)
3819 vdev_t
*rvd
= spa
->spa_root_vdev
;
3820 int need_update
= B_FALSE
;
3823 * If the config cache is stale, or we have uninitialized
3824 * metaslabs (see spa_vdev_add()), then update the config.
3826 * If this is a verbatim import, trust the current
3827 * in-core spa_config and update the disk labels.
3829 if (update_config_cache
|| config_cache_txg
!= spa
->spa_config_txg
||
3830 spa
->spa_load_state
== SPA_LOAD_IMPORT
||
3831 spa
->spa_load_state
== SPA_LOAD_RECOVER
||
3832 (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
))
3833 need_update
= B_TRUE
;
3835 for (int c
= 0; c
< rvd
->vdev_children
; c
++)
3836 if (rvd
->vdev_child
[c
]->vdev_ms_array
== 0)
3837 need_update
= B_TRUE
;
3840 * Update the config cache asychronously in case we're the
3841 * root pool, in which case the config cache isn't writable yet.
3844 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
3848 spa_ld_prepare_for_reload(spa_t
*spa
)
3850 int mode
= spa
->spa_mode
;
3851 int async_suspended
= spa
->spa_async_suspended
;
3854 spa_deactivate(spa
);
3855 spa_activate(spa
, mode
);
3858 * We save the value of spa_async_suspended as it gets reset to 0 by
3859 * spa_unload(). We want to restore it back to the original value before
3860 * returning as we might be calling spa_async_resume() later.
3862 spa
->spa_async_suspended
= async_suspended
;
3866 spa_ld_read_checkpoint_txg(spa_t
*spa
)
3868 uberblock_t checkpoint
;
3871 ASSERT0(spa
->spa_checkpoint_txg
);
3872 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
3874 error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
3875 DMU_POOL_ZPOOL_CHECKPOINT
, sizeof (uint64_t),
3876 sizeof (uberblock_t
) / sizeof (uint64_t), &checkpoint
);
3878 if (error
== ENOENT
)
3884 ASSERT3U(checkpoint
.ub_txg
, !=, 0);
3885 ASSERT3U(checkpoint
.ub_checkpoint_txg
, !=, 0);
3886 ASSERT3U(checkpoint
.ub_timestamp
, !=, 0);
3887 spa
->spa_checkpoint_txg
= checkpoint
.ub_txg
;
3888 spa
->spa_checkpoint_info
.sci_timestamp
= checkpoint
.ub_timestamp
;
3894 spa_ld_mos_init(spa_t
*spa
, spa_import_type_t type
)
3898 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
3899 ASSERT(spa
->spa_config_source
!= SPA_CONFIG_SRC_NONE
);
3902 * Never trust the config that is provided unless we are assembling
3903 * a pool following a split.
3904 * This means don't trust blkptrs and the vdev tree in general. This
3905 * also effectively puts the spa in read-only mode since
3906 * spa_writeable() checks for spa_trust_config to be true.
3907 * We will later load a trusted config from the MOS.
3909 if (type
!= SPA_IMPORT_ASSEMBLE
)
3910 spa
->spa_trust_config
= B_FALSE
;
3913 * Parse the config provided to create a vdev tree.
3915 error
= spa_ld_parse_config(spa
, type
);
3920 * Now that we have the vdev tree, try to open each vdev. This involves
3921 * opening the underlying physical device, retrieving its geometry and
3922 * probing the vdev with a dummy I/O. The state of each vdev will be set
3923 * based on the success of those operations. After this we'll be ready
3924 * to read from the vdevs.
3926 error
= spa_ld_open_vdevs(spa
);
3931 * Read the label of each vdev and make sure that the GUIDs stored
3932 * there match the GUIDs in the config provided.
3933 * If we're assembling a new pool that's been split off from an
3934 * existing pool, the labels haven't yet been updated so we skip
3935 * validation for now.
3937 if (type
!= SPA_IMPORT_ASSEMBLE
) {
3938 error
= spa_ld_validate_vdevs(spa
);
3944 * Read all vdev labels to find the best uberblock (i.e. latest,
3945 * unless spa_load_max_txg is set) and store it in spa_uberblock. We
3946 * get the list of features required to read blkptrs in the MOS from
3947 * the vdev label with the best uberblock and verify that our version
3948 * of zfs supports them all.
3950 error
= spa_ld_select_uberblock(spa
, type
);
3955 * Pass that uberblock to the dsl_pool layer which will open the root
3956 * blkptr. This blkptr points to the latest version of the MOS and will
3957 * allow us to read its contents.
3959 error
= spa_ld_open_rootbp(spa
);
3967 spa_ld_checkpoint_rewind(spa_t
*spa
)
3969 uberblock_t checkpoint
;
3972 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
3973 ASSERT(spa
->spa_import_flags
& ZFS_IMPORT_CHECKPOINT
);
3975 error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
3976 DMU_POOL_ZPOOL_CHECKPOINT
, sizeof (uint64_t),
3977 sizeof (uberblock_t
) / sizeof (uint64_t), &checkpoint
);
3980 spa_load_failed(spa
, "unable to retrieve checkpointed "
3981 "uberblock from the MOS config [error=%d]", error
);
3983 if (error
== ENOENT
)
3984 error
= ZFS_ERR_NO_CHECKPOINT
;
3989 ASSERT3U(checkpoint
.ub_txg
, <, spa
->spa_uberblock
.ub_txg
);
3990 ASSERT3U(checkpoint
.ub_txg
, ==, checkpoint
.ub_checkpoint_txg
);
3993 * We need to update the txg and timestamp of the checkpointed
3994 * uberblock to be higher than the latest one. This ensures that
3995 * the checkpointed uberblock is selected if we were to close and
3996 * reopen the pool right after we've written it in the vdev labels.
3997 * (also see block comment in vdev_uberblock_compare)
3999 checkpoint
.ub_txg
= spa
->spa_uberblock
.ub_txg
+ 1;
4000 checkpoint
.ub_timestamp
= gethrestime_sec();
4003 * Set current uberblock to be the checkpointed uberblock.
4005 spa
->spa_uberblock
= checkpoint
;
4008 * If we are doing a normal rewind, then the pool is open for
4009 * writing and we sync the "updated" checkpointed uberblock to
4010 * disk. Once this is done, we've basically rewound the whole
4011 * pool and there is no way back.
4013 * There are cases when we don't want to attempt and sync the
4014 * checkpointed uberblock to disk because we are opening a
4015 * pool as read-only. Specifically, verifying the checkpointed
4016 * state with zdb, and importing the checkpointed state to get
4017 * a "preview" of its content.
4019 if (spa_writeable(spa
)) {
4020 vdev_t
*rvd
= spa
->spa_root_vdev
;
4022 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4023 vdev_t
*svd
[SPA_SYNC_MIN_VDEVS
] = { NULL
};
4025 int children
= rvd
->vdev_children
;
4026 int c0
= spa_get_random(children
);
4028 for (int c
= 0; c
< children
; c
++) {
4029 vdev_t
*vd
= rvd
->vdev_child
[(c0
+ c
) % children
];
4031 /* Stop when revisiting the first vdev */
4032 if (c
> 0 && svd
[0] == vd
)
4035 if (vd
->vdev_ms_array
== 0 || vd
->vdev_islog
||
4036 !vdev_is_concrete(vd
))
4039 svd
[svdcount
++] = vd
;
4040 if (svdcount
== SPA_SYNC_MIN_VDEVS
)
4043 error
= vdev_config_sync(svd
, svdcount
, spa
->spa_first_txg
);
4045 spa
->spa_last_synced_guid
= rvd
->vdev_guid
;
4046 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4049 spa_load_failed(spa
, "failed to write checkpointed "
4050 "uberblock to the vdev labels [error=%d]", error
);
4059 spa_ld_mos_with_trusted_config(spa_t
*spa
, spa_import_type_t type
,
4060 boolean_t
*update_config_cache
)
4065 * Parse the config for pool, open and validate vdevs,
4066 * select an uberblock, and use that uberblock to open
4069 error
= spa_ld_mos_init(spa
, type
);
4074 * Retrieve the trusted config stored in the MOS and use it to create
4075 * a new, exact version of the vdev tree, then reopen all vdevs.
4077 error
= spa_ld_trusted_config(spa
, type
, B_FALSE
);
4078 if (error
== EAGAIN
) {
4079 if (update_config_cache
!= NULL
)
4080 *update_config_cache
= B_TRUE
;
4083 * Redo the loading process with the trusted config if it is
4084 * too different from the untrusted config.
4086 spa_ld_prepare_for_reload(spa
);
4087 spa_load_note(spa
, "RELOADING");
4088 error
= spa_ld_mos_init(spa
, type
);
4092 error
= spa_ld_trusted_config(spa
, type
, B_TRUE
);
4096 } else if (error
!= 0) {
4104 * Load an existing storage pool, using the config provided. This config
4105 * describes which vdevs are part of the pool and is later validated against
4106 * partial configs present in each vdev's label and an entire copy of the
4107 * config stored in the MOS.
4110 spa_load_impl(spa_t
*spa
, spa_import_type_t type
, char **ereport
)
4113 boolean_t missing_feat_write
= B_FALSE
;
4114 boolean_t checkpoint_rewind
=
4115 (spa
->spa_import_flags
& ZFS_IMPORT_CHECKPOINT
);
4116 boolean_t update_config_cache
= B_FALSE
;
4118 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
4119 ASSERT(spa
->spa_config_source
!= SPA_CONFIG_SRC_NONE
);
4121 spa_load_note(spa
, "LOADING");
4123 error
= spa_ld_mos_with_trusted_config(spa
, type
, &update_config_cache
);
4128 * If we are rewinding to the checkpoint then we need to repeat
4129 * everything we've done so far in this function but this time
4130 * selecting the checkpointed uberblock and using that to open
4133 if (checkpoint_rewind
) {
4135 * If we are rewinding to the checkpoint update config cache
4138 update_config_cache
= B_TRUE
;
4141 * Extract the checkpointed uberblock from the current MOS
4142 * and use this as the pool's uberblock from now on. If the
4143 * pool is imported as writeable we also write the checkpoint
4144 * uberblock to the labels, making the rewind permanent.
4146 error
= spa_ld_checkpoint_rewind(spa
);
4151 * Redo the loading process process again with the
4152 * checkpointed uberblock.
4154 spa_ld_prepare_for_reload(spa
);
4155 spa_load_note(spa
, "LOADING checkpointed uberblock");
4156 error
= spa_ld_mos_with_trusted_config(spa
, type
, NULL
);
4162 * Retrieve the checkpoint txg if the pool has a checkpoint.
4164 error
= spa_ld_read_checkpoint_txg(spa
);
4169 * Retrieve the mapping of indirect vdevs. Those vdevs were removed
4170 * from the pool and their contents were re-mapped to other vdevs. Note
4171 * that everything that we read before this step must have been
4172 * rewritten on concrete vdevs after the last device removal was
4173 * initiated. Otherwise we could be reading from indirect vdevs before
4174 * we have loaded their mappings.
4176 error
= spa_ld_open_indirect_vdev_metadata(spa
);
4181 * Retrieve the full list of active features from the MOS and check if
4182 * they are all supported.
4184 error
= spa_ld_check_features(spa
, &missing_feat_write
);
4189 * Load several special directories from the MOS needed by the dsl_pool
4192 error
= spa_ld_load_special_directories(spa
);
4197 * Retrieve pool properties from the MOS.
4199 error
= spa_ld_get_props(spa
);
4204 * Retrieve the list of auxiliary devices - cache devices and spares -
4207 error
= spa_ld_open_aux_vdevs(spa
, type
);
4212 * Load the metadata for all vdevs. Also check if unopenable devices
4213 * should be autoreplaced.
4215 error
= spa_ld_load_vdev_metadata(spa
);
4219 error
= spa_ld_load_dedup_tables(spa
);
4224 * Verify the logs now to make sure we don't have any unexpected errors
4225 * when we claim log blocks later.
4227 error
= spa_ld_verify_logs(spa
, type
, ereport
);
4231 if (missing_feat_write
) {
4232 ASSERT(spa
->spa_load_state
== SPA_LOAD_TRYIMPORT
);
4235 * At this point, we know that we can open the pool in
4236 * read-only mode but not read-write mode. We now have enough
4237 * information and can return to userland.
4239 return (spa_vdev_err(spa
->spa_root_vdev
, VDEV_AUX_UNSUP_FEAT
,
4244 * Traverse the last txgs to make sure the pool was left off in a safe
4245 * state. When performing an extreme rewind, we verify the whole pool,
4246 * which can take a very long time.
4248 error
= spa_ld_verify_pool_data(spa
);
4253 * Calculate the deflated space for the pool. This must be done before
4254 * we write anything to the pool because we'd need to update the space
4255 * accounting using the deflated sizes.
4257 spa_update_dspace(spa
);
4260 * We have now retrieved all the information we needed to open the
4261 * pool. If we are importing the pool in read-write mode, a few
4262 * additional steps must be performed to finish the import.
4264 if (spa_writeable(spa
) && (spa
->spa_load_state
== SPA_LOAD_RECOVER
||
4265 spa
->spa_load_max_txg
== UINT64_MAX
)) {
4266 uint64_t config_cache_txg
= spa
->spa_config_txg
;
4268 ASSERT(spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
);
4271 * In case of a checkpoint rewind, log the original txg
4272 * of the checkpointed uberblock.
4274 if (checkpoint_rewind
) {
4275 spa_history_log_internal(spa
, "checkpoint rewind",
4276 NULL
, "rewound state to txg=%llu",
4277 (u_longlong_t
)spa
->spa_uberblock
.ub_checkpoint_txg
);
4281 * Traverse the ZIL and claim all blocks.
4283 spa_ld_claim_log_blocks(spa
);
4286 * Kick-off the syncing thread.
4288 spa
->spa_sync_on
= B_TRUE
;
4289 txg_sync_start(spa
->spa_dsl_pool
);
4290 mmp_thread_start(spa
);
4293 * Wait for all claims to sync. We sync up to the highest
4294 * claimed log block birth time so that claimed log blocks
4295 * don't appear to be from the future. spa_claim_max_txg
4296 * will have been set for us by ZIL traversal operations
4299 txg_wait_synced(spa
->spa_dsl_pool
, spa
->spa_claim_max_txg
);
4302 * Check if we need to request an update of the config. On the
4303 * next sync, we would update the config stored in vdev labels
4304 * and the cachefile (by default /etc/zfs/zpool.cache).
4306 spa_ld_check_for_config_update(spa
, config_cache_txg
,
4307 update_config_cache
);
4310 * Check all DTLs to see if anything needs resilvering.
4312 if (!dsl_scan_resilvering(spa
->spa_dsl_pool
) &&
4313 vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
))
4314 spa_async_request(spa
, SPA_ASYNC_RESILVER
);
4317 * Log the fact that we booted up (so that we can detect if
4318 * we rebooted in the middle of an operation).
4320 spa_history_log_version(spa
, "open", NULL
);
4322 spa_restart_removal(spa
);
4323 spa_spawn_aux_threads(spa
);
4326 * Delete any inconsistent datasets.
4329 * Since we may be issuing deletes for clones here,
4330 * we make sure to do so after we've spawned all the
4331 * auxiliary threads above (from which the livelist
4332 * deletion zthr is part of).
4334 (void) dmu_objset_find(spa_name(spa
),
4335 dsl_destroy_inconsistent
, NULL
, DS_FIND_CHILDREN
);
4338 * Clean up any stale temporary dataset userrefs.
4340 dsl_pool_clean_tmp_userrefs(spa
->spa_dsl_pool
);
4342 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
4343 vdev_initialize_restart(spa
->spa_root_vdev
);
4344 vdev_trim_restart(spa
->spa_root_vdev
);
4345 vdev_autotrim_restart(spa
);
4346 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
4349 spa_load_note(spa
, "LOADED");
4355 spa_load_retry(spa_t
*spa
, spa_load_state_t state
)
4357 int mode
= spa
->spa_mode
;
4360 spa_deactivate(spa
);
4362 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
- 1;
4364 spa_activate(spa
, mode
);
4365 spa_async_suspend(spa
);
4367 spa_load_note(spa
, "spa_load_retry: rewind, max txg: %llu",
4368 (u_longlong_t
)spa
->spa_load_max_txg
);
4370 return (spa_load(spa
, state
, SPA_IMPORT_EXISTING
));
4374 * If spa_load() fails this function will try loading prior txg's. If
4375 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
4376 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
4377 * function will not rewind the pool and will return the same error as
4381 spa_load_best(spa_t
*spa
, spa_load_state_t state
, uint64_t max_request
,
4384 nvlist_t
*loadinfo
= NULL
;
4385 nvlist_t
*config
= NULL
;
4386 int load_error
, rewind_error
;
4387 uint64_t safe_rewind_txg
;
4390 if (spa
->spa_load_txg
&& state
== SPA_LOAD_RECOVER
) {
4391 spa
->spa_load_max_txg
= spa
->spa_load_txg
;
4392 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
4394 spa
->spa_load_max_txg
= max_request
;
4395 if (max_request
!= UINT64_MAX
)
4396 spa
->spa_extreme_rewind
= B_TRUE
;
4399 load_error
= rewind_error
= spa_load(spa
, state
, SPA_IMPORT_EXISTING
);
4400 if (load_error
== 0)
4402 if (load_error
== ZFS_ERR_NO_CHECKPOINT
) {
4404 * When attempting checkpoint-rewind on a pool with no
4405 * checkpoint, we should not attempt to load uberblocks
4406 * from previous txgs when spa_load fails.
4408 ASSERT(spa
->spa_import_flags
& ZFS_IMPORT_CHECKPOINT
);
4409 return (load_error
);
4412 if (spa
->spa_root_vdev
!= NULL
)
4413 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
4415 spa
->spa_last_ubsync_txg
= spa
->spa_uberblock
.ub_txg
;
4416 spa
->spa_last_ubsync_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
4418 if (rewind_flags
& ZPOOL_NEVER_REWIND
) {
4419 nvlist_free(config
);
4420 return (load_error
);
4423 if (state
== SPA_LOAD_RECOVER
) {
4424 /* Price of rolling back is discarding txgs, including log */
4425 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
4428 * If we aren't rolling back save the load info from our first
4429 * import attempt so that we can restore it after attempting
4432 loadinfo
= spa
->spa_load_info
;
4433 spa
->spa_load_info
= fnvlist_alloc();
4436 spa
->spa_load_max_txg
= spa
->spa_last_ubsync_txg
;
4437 safe_rewind_txg
= spa
->spa_last_ubsync_txg
- TXG_DEFER_SIZE
;
4438 min_txg
= (rewind_flags
& ZPOOL_EXTREME_REWIND
) ?
4439 TXG_INITIAL
: safe_rewind_txg
;
4442 * Continue as long as we're finding errors, we're still within
4443 * the acceptable rewind range, and we're still finding uberblocks
4445 while (rewind_error
&& spa
->spa_uberblock
.ub_txg
>= min_txg
&&
4446 spa
->spa_uberblock
.ub_txg
<= spa
->spa_load_max_txg
) {
4447 if (spa
->spa_load_max_txg
< safe_rewind_txg
)
4448 spa
->spa_extreme_rewind
= B_TRUE
;
4449 rewind_error
= spa_load_retry(spa
, state
);
4452 spa
->spa_extreme_rewind
= B_FALSE
;
4453 spa
->spa_load_max_txg
= UINT64_MAX
;
4455 if (config
&& (rewind_error
|| state
!= SPA_LOAD_RECOVER
))
4456 spa_config_set(spa
, config
);
4458 nvlist_free(config
);
4460 if (state
== SPA_LOAD_RECOVER
) {
4461 ASSERT3P(loadinfo
, ==, NULL
);
4462 return (rewind_error
);
4464 /* Store the rewind info as part of the initial load info */
4465 fnvlist_add_nvlist(loadinfo
, ZPOOL_CONFIG_REWIND_INFO
,
4466 spa
->spa_load_info
);
4468 /* Restore the initial load info */
4469 fnvlist_free(spa
->spa_load_info
);
4470 spa
->spa_load_info
= loadinfo
;
4472 return (load_error
);
4479 * The import case is identical to an open except that the configuration is sent
4480 * down from userland, instead of grabbed from the configuration cache. For the
4481 * case of an open, the pool configuration will exist in the
4482 * POOL_STATE_UNINITIALIZED state.
4484 * The stats information (gen/count/ustats) is used to gather vdev statistics at
4485 * the same time open the pool, without having to keep around the spa_t in some
4489 spa_open_common(const char *pool
, spa_t
**spapp
, void *tag
, nvlist_t
*nvpolicy
,
4493 spa_load_state_t state
= SPA_LOAD_OPEN
;
4495 int locked
= B_FALSE
;
4496 int firstopen
= B_FALSE
;
4501 * As disgusting as this is, we need to support recursive calls to this
4502 * function because dsl_dir_open() is called during spa_load(), and ends
4503 * up calling spa_open() again. The real fix is to figure out how to
4504 * avoid dsl_dir_open() calling this in the first place.
4506 if (MUTEX_NOT_HELD(&spa_namespace_lock
)) {
4507 mutex_enter(&spa_namespace_lock
);
4511 if ((spa
= spa_lookup(pool
)) == NULL
) {
4513 mutex_exit(&spa_namespace_lock
);
4514 return (SET_ERROR(ENOENT
));
4517 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
) {
4518 zpool_load_policy_t policy
;
4522 zpool_get_load_policy(nvpolicy
? nvpolicy
: spa
->spa_config
,
4524 if (policy
.zlp_rewind
& ZPOOL_DO_REWIND
)
4525 state
= SPA_LOAD_RECOVER
;
4527 spa_activate(spa
, spa_mode_global
);
4529 if (state
!= SPA_LOAD_RECOVER
)
4530 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
4531 spa
->spa_config_source
= SPA_CONFIG_SRC_CACHEFILE
;
4533 zfs_dbgmsg("spa_open_common: opening %s", pool
);
4534 error
= spa_load_best(spa
, state
, policy
.zlp_txg
,
4537 if (error
== EBADF
) {
4539 * If vdev_validate() returns failure (indicated by
4540 * EBADF), it indicates that one of the vdevs indicates
4541 * that the pool has been exported or destroyed. If
4542 * this is the case, the config cache is out of sync and
4543 * we should remove the pool from the namespace.
4546 spa_deactivate(spa
);
4547 spa_write_cachefile(spa
, B_TRUE
, B_TRUE
);
4550 mutex_exit(&spa_namespace_lock
);
4551 return (SET_ERROR(ENOENT
));
4556 * We can't open the pool, but we still have useful
4557 * information: the state of each vdev after the
4558 * attempted vdev_open(). Return this to the user.
4560 if (config
!= NULL
&& spa
->spa_config
) {
4561 VERIFY(nvlist_dup(spa
->spa_config
, config
,
4563 VERIFY(nvlist_add_nvlist(*config
,
4564 ZPOOL_CONFIG_LOAD_INFO
,
4565 spa
->spa_load_info
) == 0);
4568 spa_deactivate(spa
);
4569 spa
->spa_last_open_failed
= error
;
4571 mutex_exit(&spa_namespace_lock
);
4577 spa_open_ref(spa
, tag
);
4580 *config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
4583 * If we've recovered the pool, pass back any information we
4584 * gathered while doing the load.
4586 if (state
== SPA_LOAD_RECOVER
) {
4587 VERIFY(nvlist_add_nvlist(*config
, ZPOOL_CONFIG_LOAD_INFO
,
4588 spa
->spa_load_info
) == 0);
4592 spa
->spa_last_open_failed
= 0;
4593 spa
->spa_last_ubsync_txg
= 0;
4594 spa
->spa_load_txg
= 0;
4595 mutex_exit(&spa_namespace_lock
);
4599 zvol_create_minors(spa
, spa_name(spa
), B_TRUE
);
4607 spa_open_rewind(const char *name
, spa_t
**spapp
, void *tag
, nvlist_t
*policy
,
4610 return (spa_open_common(name
, spapp
, tag
, policy
, config
));
4614 spa_open(const char *name
, spa_t
**spapp
, void *tag
)
4616 return (spa_open_common(name
, spapp
, tag
, NULL
, NULL
));
4620 * Lookup the given spa_t, incrementing the inject count in the process,
4621 * preventing it from being exported or destroyed.
4624 spa_inject_addref(char *name
)
4628 mutex_enter(&spa_namespace_lock
);
4629 if ((spa
= spa_lookup(name
)) == NULL
) {
4630 mutex_exit(&spa_namespace_lock
);
4633 spa
->spa_inject_ref
++;
4634 mutex_exit(&spa_namespace_lock
);
4640 spa_inject_delref(spa_t
*spa
)
4642 mutex_enter(&spa_namespace_lock
);
4643 spa
->spa_inject_ref
--;
4644 mutex_exit(&spa_namespace_lock
);
4648 * Add spares device information to the nvlist.
4651 spa_add_spares(spa_t
*spa
, nvlist_t
*config
)
4661 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
4663 if (spa
->spa_spares
.sav_count
== 0)
4666 VERIFY(nvlist_lookup_nvlist(config
,
4667 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
4668 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
4669 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
4671 VERIFY(nvlist_add_nvlist_array(nvroot
,
4672 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
4673 VERIFY(nvlist_lookup_nvlist_array(nvroot
,
4674 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
4677 * Go through and find any spares which have since been
4678 * repurposed as an active spare. If this is the case, update
4679 * their status appropriately.
4681 for (i
= 0; i
< nspares
; i
++) {
4682 VERIFY(nvlist_lookup_uint64(spares
[i
],
4683 ZPOOL_CONFIG_GUID
, &guid
) == 0);
4684 if (spa_spare_exists(guid
, &pool
, NULL
) &&
4686 VERIFY(nvlist_lookup_uint64_array(
4687 spares
[i
], ZPOOL_CONFIG_VDEV_STATS
,
4688 (uint64_t **)&vs
, &vsc
) == 0);
4689 vs
->vs_state
= VDEV_STATE_CANT_OPEN
;
4690 vs
->vs_aux
= VDEV_AUX_SPARED
;
4697 * Add l2cache device information to the nvlist, including vdev stats.
4700 spa_add_l2cache(spa_t
*spa
, nvlist_t
*config
)
4703 uint_t i
, j
, nl2cache
;
4710 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
4712 if (spa
->spa_l2cache
.sav_count
== 0)
4715 VERIFY(nvlist_lookup_nvlist(config
,
4716 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
4717 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_l2cache
.sav_config
,
4718 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
4719 if (nl2cache
!= 0) {
4720 VERIFY(nvlist_add_nvlist_array(nvroot
,
4721 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
4722 VERIFY(nvlist_lookup_nvlist_array(nvroot
,
4723 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
4726 * Update level 2 cache device stats.
4729 for (i
= 0; i
< nl2cache
; i
++) {
4730 VERIFY(nvlist_lookup_uint64(l2cache
[i
],
4731 ZPOOL_CONFIG_GUID
, &guid
) == 0);
4734 for (j
= 0; j
< spa
->spa_l2cache
.sav_count
; j
++) {
4736 spa
->spa_l2cache
.sav_vdevs
[j
]->vdev_guid
) {
4737 vd
= spa
->spa_l2cache
.sav_vdevs
[j
];
4743 VERIFY(nvlist_lookup_uint64_array(l2cache
[i
],
4744 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
, &vsc
)
4746 vdev_get_stats(vd
, vs
);
4747 vdev_config_generate_stats(vd
, l2cache
[i
]);
4754 spa_feature_stats_from_disk(spa_t
*spa
, nvlist_t
*features
)
4759 if (spa
->spa_feat_for_read_obj
!= 0) {
4760 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
4761 spa
->spa_feat_for_read_obj
);
4762 zap_cursor_retrieve(&zc
, &za
) == 0;
4763 zap_cursor_advance(&zc
)) {
4764 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
4765 za
.za_num_integers
== 1);
4766 VERIFY0(nvlist_add_uint64(features
, za
.za_name
,
4767 za
.za_first_integer
));
4769 zap_cursor_fini(&zc
);
4772 if (spa
->spa_feat_for_write_obj
!= 0) {
4773 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
4774 spa
->spa_feat_for_write_obj
);
4775 zap_cursor_retrieve(&zc
, &za
) == 0;
4776 zap_cursor_advance(&zc
)) {
4777 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
4778 za
.za_num_integers
== 1);
4779 VERIFY0(nvlist_add_uint64(features
, za
.za_name
,
4780 za
.za_first_integer
));
4782 zap_cursor_fini(&zc
);
4787 spa_feature_stats_from_cache(spa_t
*spa
, nvlist_t
*features
)
4791 for (i
= 0; i
< SPA_FEATURES
; i
++) {
4792 zfeature_info_t feature
= spa_feature_table
[i
];
4795 if (feature_get_refcount(spa
, &feature
, &refcount
) != 0)
4798 VERIFY0(nvlist_add_uint64(features
, feature
.fi_guid
, refcount
));
4803 * Store a list of pool features and their reference counts in the
4806 * The first time this is called on a spa, allocate a new nvlist, fetch
4807 * the pool features and reference counts from disk, then save the list
4808 * in the spa. In subsequent calls on the same spa use the saved nvlist
4809 * and refresh its values from the cached reference counts. This
4810 * ensures we don't block here on I/O on a suspended pool so 'zpool
4811 * clear' can resume the pool.
4814 spa_add_feature_stats(spa_t
*spa
, nvlist_t
*config
)
4818 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
4820 mutex_enter(&spa
->spa_feat_stats_lock
);
4821 features
= spa
->spa_feat_stats
;
4823 if (features
!= NULL
) {
4824 spa_feature_stats_from_cache(spa
, features
);
4826 VERIFY0(nvlist_alloc(&features
, NV_UNIQUE_NAME
, KM_SLEEP
));
4827 spa
->spa_feat_stats
= features
;
4828 spa_feature_stats_from_disk(spa
, features
);
4831 VERIFY0(nvlist_add_nvlist(config
, ZPOOL_CONFIG_FEATURE_STATS
,
4834 mutex_exit(&spa
->spa_feat_stats_lock
);
4838 spa_get_stats(const char *name
, nvlist_t
**config
,
4839 char *altroot
, size_t buflen
)
4845 error
= spa_open_common(name
, &spa
, FTAG
, NULL
, config
);
4849 * This still leaves a window of inconsistency where the spares
4850 * or l2cache devices could change and the config would be
4851 * self-inconsistent.
4853 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
4855 if (*config
!= NULL
) {
4856 uint64_t loadtimes
[2];
4858 loadtimes
[0] = spa
->spa_loaded_ts
.tv_sec
;
4859 loadtimes
[1] = spa
->spa_loaded_ts
.tv_nsec
;
4860 VERIFY(nvlist_add_uint64_array(*config
,
4861 ZPOOL_CONFIG_LOADED_TIME
, loadtimes
, 2) == 0);
4863 VERIFY(nvlist_add_uint64(*config
,
4864 ZPOOL_CONFIG_ERRCOUNT
,
4865 spa_get_errlog_size(spa
)) == 0);
4867 if (spa_suspended(spa
)) {
4868 VERIFY(nvlist_add_uint64(*config
,
4869 ZPOOL_CONFIG_SUSPENDED
,
4870 spa
->spa_failmode
) == 0);
4871 VERIFY(nvlist_add_uint64(*config
,
4872 ZPOOL_CONFIG_SUSPENDED_REASON
,
4873 spa
->spa_suspended
) == 0);
4876 spa_add_spares(spa
, *config
);
4877 spa_add_l2cache(spa
, *config
);
4878 spa_add_feature_stats(spa
, *config
);
4883 * We want to get the alternate root even for faulted pools, so we cheat
4884 * and call spa_lookup() directly.
4888 mutex_enter(&spa_namespace_lock
);
4889 spa
= spa_lookup(name
);
4891 spa_altroot(spa
, altroot
, buflen
);
4895 mutex_exit(&spa_namespace_lock
);
4897 spa_altroot(spa
, altroot
, buflen
);
4902 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
4903 spa_close(spa
, FTAG
);
4910 * Validate that the auxiliary device array is well formed. We must have an
4911 * array of nvlists, each which describes a valid leaf vdev. If this is an
4912 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
4913 * specified, as long as they are well-formed.
4916 spa_validate_aux_devs(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
,
4917 spa_aux_vdev_t
*sav
, const char *config
, uint64_t version
,
4918 vdev_labeltype_t label
)
4925 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
4928 * It's acceptable to have no devs specified.
4930 if (nvlist_lookup_nvlist_array(nvroot
, config
, &dev
, &ndev
) != 0)
4934 return (SET_ERROR(EINVAL
));
4937 * Make sure the pool is formatted with a version that supports this
4940 if (spa_version(spa
) < version
)
4941 return (SET_ERROR(ENOTSUP
));
4944 * Set the pending device list so we correctly handle device in-use
4947 sav
->sav_pending
= dev
;
4948 sav
->sav_npending
= ndev
;
4950 for (i
= 0; i
< ndev
; i
++) {
4951 if ((error
= spa_config_parse(spa
, &vd
, dev
[i
], NULL
, 0,
4955 if (!vd
->vdev_ops
->vdev_op_leaf
) {
4957 error
= SET_ERROR(EINVAL
);
4963 if ((error
= vdev_open(vd
)) == 0 &&
4964 (error
= vdev_label_init(vd
, crtxg
, label
)) == 0) {
4965 VERIFY(nvlist_add_uint64(dev
[i
], ZPOOL_CONFIG_GUID
,
4966 vd
->vdev_guid
) == 0);
4972 (mode
!= VDEV_ALLOC_SPARE
&& mode
!= VDEV_ALLOC_L2CACHE
))
4979 sav
->sav_pending
= NULL
;
4980 sav
->sav_npending
= 0;
4985 spa_validate_aux(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
)
4989 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
4991 if ((error
= spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
4992 &spa
->spa_spares
, ZPOOL_CONFIG_SPARES
, SPA_VERSION_SPARES
,
4993 VDEV_LABEL_SPARE
)) != 0) {
4997 return (spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
4998 &spa
->spa_l2cache
, ZPOOL_CONFIG_L2CACHE
, SPA_VERSION_L2CACHE
,
4999 VDEV_LABEL_L2CACHE
));
5003 spa_set_aux_vdevs(spa_aux_vdev_t
*sav
, nvlist_t
**devs
, int ndevs
,
5008 if (sav
->sav_config
!= NULL
) {
5014 * Generate new dev list by concatenating with the
5017 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
, config
,
5018 &olddevs
, &oldndevs
) == 0);
5020 newdevs
= kmem_alloc(sizeof (void *) *
5021 (ndevs
+ oldndevs
), KM_SLEEP
);
5022 for (i
= 0; i
< oldndevs
; i
++)
5023 VERIFY(nvlist_dup(olddevs
[i
], &newdevs
[i
],
5025 for (i
= 0; i
< ndevs
; i
++)
5026 VERIFY(nvlist_dup(devs
[i
], &newdevs
[i
+ oldndevs
],
5029 VERIFY(nvlist_remove(sav
->sav_config
, config
,
5030 DATA_TYPE_NVLIST_ARRAY
) == 0);
5032 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
,
5033 config
, newdevs
, ndevs
+ oldndevs
) == 0);
5034 for (i
= 0; i
< oldndevs
+ ndevs
; i
++)
5035 nvlist_free(newdevs
[i
]);
5036 kmem_free(newdevs
, (oldndevs
+ ndevs
) * sizeof (void *));
5039 * Generate a new dev list.
5041 VERIFY(nvlist_alloc(&sav
->sav_config
, NV_UNIQUE_NAME
,
5043 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
, config
,
5049 * Stop and drop level 2 ARC devices
5052 spa_l2cache_drop(spa_t
*spa
)
5056 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
5058 for (i
= 0; i
< sav
->sav_count
; i
++) {
5061 vd
= sav
->sav_vdevs
[i
];
5064 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
5065 pool
!= 0ULL && l2arc_vdev_present(vd
))
5066 l2arc_remove_vdev(vd
);
5071 * Verify encryption parameters for spa creation. If we are encrypting, we must
5072 * have the encryption feature flag enabled.
5075 spa_create_check_encryption_params(dsl_crypto_params_t
*dcp
,
5076 boolean_t has_encryption
)
5078 if (dcp
->cp_crypt
!= ZIO_CRYPT_OFF
&&
5079 dcp
->cp_crypt
!= ZIO_CRYPT_INHERIT
&&
5081 return (SET_ERROR(ENOTSUP
));
5083 return (dmu_objset_create_crypt_check(NULL
, dcp
, NULL
));
5090 spa_create(const char *pool
, nvlist_t
*nvroot
, nvlist_t
*props
,
5091 nvlist_t
*zplprops
, dsl_crypto_params_t
*dcp
)
5094 char *altroot
= NULL
;
5099 uint64_t txg
= TXG_INITIAL
;
5100 nvlist_t
**spares
, **l2cache
;
5101 uint_t nspares
, nl2cache
;
5102 uint64_t version
, obj
;
5103 boolean_t has_features
;
5104 boolean_t has_encryption
;
5110 if (props
== NULL
||
5111 nvlist_lookup_string(props
, "tname", &poolname
) != 0)
5112 poolname
= (char *)pool
;
5115 * If this pool already exists, return failure.
5117 mutex_enter(&spa_namespace_lock
);
5118 if (spa_lookup(poolname
) != NULL
) {
5119 mutex_exit(&spa_namespace_lock
);
5120 return (SET_ERROR(EEXIST
));
5124 * Allocate a new spa_t structure.
5126 nvl
= fnvlist_alloc();
5127 fnvlist_add_string(nvl
, ZPOOL_CONFIG_POOL_NAME
, pool
);
5128 (void) nvlist_lookup_string(props
,
5129 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
5130 spa
= spa_add(poolname
, nvl
, altroot
);
5132 spa_activate(spa
, spa_mode_global
);
5134 if (props
&& (error
= spa_prop_validate(spa
, props
))) {
5135 spa_deactivate(spa
);
5137 mutex_exit(&spa_namespace_lock
);
5142 * Temporary pool names should never be written to disk.
5144 if (poolname
!= pool
)
5145 spa
->spa_import_flags
|= ZFS_IMPORT_TEMP_NAME
;
5147 has_features
= B_FALSE
;
5148 has_encryption
= B_FALSE
;
5149 for (nvpair_t
*elem
= nvlist_next_nvpair(props
, NULL
);
5150 elem
!= NULL
; elem
= nvlist_next_nvpair(props
, elem
)) {
5151 if (zpool_prop_feature(nvpair_name(elem
))) {
5152 has_features
= B_TRUE
;
5154 feat_name
= strchr(nvpair_name(elem
), '@') + 1;
5155 VERIFY0(zfeature_lookup_name(feat_name
, &feat
));
5156 if (feat
== SPA_FEATURE_ENCRYPTION
)
5157 has_encryption
= B_TRUE
;
5161 /* verify encryption params, if they were provided */
5163 error
= spa_create_check_encryption_params(dcp
, has_encryption
);
5165 spa_deactivate(spa
);
5167 mutex_exit(&spa_namespace_lock
);
5172 if (has_features
|| nvlist_lookup_uint64(props
,
5173 zpool_prop_to_name(ZPOOL_PROP_VERSION
), &version
) != 0) {
5174 version
= SPA_VERSION
;
5176 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
5178 spa
->spa_first_txg
= txg
;
5179 spa
->spa_uberblock
.ub_txg
= txg
- 1;
5180 spa
->spa_uberblock
.ub_version
= version
;
5181 spa
->spa_ubsync
= spa
->spa_uberblock
;
5182 spa
->spa_load_state
= SPA_LOAD_CREATE
;
5183 spa
->spa_removing_phys
.sr_state
= DSS_NONE
;
5184 spa
->spa_removing_phys
.sr_removing_vdev
= -1;
5185 spa
->spa_removing_phys
.sr_prev_indirect_vdev
= -1;
5188 * Create "The Godfather" zio to hold all async IOs
5190 spa
->spa_async_zio_root
= kmem_alloc(max_ncpus
* sizeof (void *),
5192 for (int i
= 0; i
< max_ncpus
; i
++) {
5193 spa
->spa_async_zio_root
[i
] = zio_root(spa
, NULL
, NULL
,
5194 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
5195 ZIO_FLAG_GODFATHER
);
5199 * Create the root vdev.
5201 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5203 error
= spa_config_parse(spa
, &rvd
, nvroot
, NULL
, 0, VDEV_ALLOC_ADD
);
5205 ASSERT(error
!= 0 || rvd
!= NULL
);
5206 ASSERT(error
!= 0 || spa
->spa_root_vdev
== rvd
);
5208 if (error
== 0 && !zfs_allocatable_devs(nvroot
))
5209 error
= SET_ERROR(EINVAL
);
5212 (error
= vdev_create(rvd
, txg
, B_FALSE
)) == 0 &&
5213 (error
= spa_validate_aux(spa
, nvroot
, txg
,
5214 VDEV_ALLOC_ADD
)) == 0) {
5216 * instantiate the metaslab groups (this will dirty the vdevs)
5217 * we can no longer error exit past this point
5219 for (int c
= 0; error
== 0 && c
< rvd
->vdev_children
; c
++) {
5220 vdev_t
*vd
= rvd
->vdev_child
[c
];
5222 vdev_metaslab_set_size(vd
);
5223 vdev_expand(vd
, txg
);
5227 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5231 spa_deactivate(spa
);
5233 mutex_exit(&spa_namespace_lock
);
5238 * Get the list of spares, if specified.
5240 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
5241 &spares
, &nspares
) == 0) {
5242 VERIFY(nvlist_alloc(&spa
->spa_spares
.sav_config
, NV_UNIQUE_NAME
,
5244 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
5245 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
5246 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5247 spa_load_spares(spa
);
5248 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5249 spa
->spa_spares
.sav_sync
= B_TRUE
;
5253 * Get the list of level 2 cache devices, if specified.
5255 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
5256 &l2cache
, &nl2cache
) == 0) {
5257 VERIFY(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
5258 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
5259 VERIFY(nvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
5260 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
5261 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5262 spa_load_l2cache(spa
);
5263 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5264 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
5267 spa
->spa_is_initializing
= B_TRUE
;
5268 spa
->spa_dsl_pool
= dp
= dsl_pool_create(spa
, zplprops
, dcp
, txg
);
5269 spa
->spa_is_initializing
= B_FALSE
;
5272 * Create DDTs (dedup tables).
5276 spa_update_dspace(spa
);
5278 tx
= dmu_tx_create_assigned(dp
, txg
);
5281 * Create the pool's history object.
5283 if (version
>= SPA_VERSION_ZPOOL_HISTORY
&& !spa
->spa_history
)
5284 spa_history_create_obj(spa
, tx
);
5286 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_CREATE
);
5287 spa_history_log_version(spa
, "create", tx
);
5290 * Create the pool config object.
5292 spa
->spa_config_object
= dmu_object_alloc(spa
->spa_meta_objset
,
5293 DMU_OT_PACKED_NVLIST
, SPA_CONFIG_BLOCKSIZE
,
5294 DMU_OT_PACKED_NVLIST_SIZE
, sizeof (uint64_t), tx
);
5296 if (zap_add(spa
->spa_meta_objset
,
5297 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CONFIG
,
5298 sizeof (uint64_t), 1, &spa
->spa_config_object
, tx
) != 0) {
5299 cmn_err(CE_PANIC
, "failed to add pool config");
5302 if (zap_add(spa
->spa_meta_objset
,
5303 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CREATION_VERSION
,
5304 sizeof (uint64_t), 1, &version
, tx
) != 0) {
5305 cmn_err(CE_PANIC
, "failed to add pool version");
5308 /* Newly created pools with the right version are always deflated. */
5309 if (version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
5310 spa
->spa_deflate
= TRUE
;
5311 if (zap_add(spa
->spa_meta_objset
,
5312 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
5313 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
) != 0) {
5314 cmn_err(CE_PANIC
, "failed to add deflate");
5319 * Create the deferred-free bpobj. Turn off compression
5320 * because sync-to-convergence takes longer if the blocksize
5323 obj
= bpobj_alloc(spa
->spa_meta_objset
, 1 << 14, tx
);
5324 dmu_object_set_compress(spa
->spa_meta_objset
, obj
,
5325 ZIO_COMPRESS_OFF
, tx
);
5326 if (zap_add(spa
->spa_meta_objset
,
5327 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_SYNC_BPOBJ
,
5328 sizeof (uint64_t), 1, &obj
, tx
) != 0) {
5329 cmn_err(CE_PANIC
, "failed to add bpobj");
5331 VERIFY3U(0, ==, bpobj_open(&spa
->spa_deferred_bpobj
,
5332 spa
->spa_meta_objset
, obj
));
5335 * Generate some random noise for salted checksums to operate on.
5337 (void) random_get_pseudo_bytes(spa
->spa_cksum_salt
.zcs_bytes
,
5338 sizeof (spa
->spa_cksum_salt
.zcs_bytes
));
5341 * Set pool properties.
5343 spa
->spa_bootfs
= zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS
);
5344 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
5345 spa
->spa_failmode
= zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE
);
5346 spa
->spa_autoexpand
= zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND
);
5347 spa
->spa_multihost
= zpool_prop_default_numeric(ZPOOL_PROP_MULTIHOST
);
5348 spa
->spa_autotrim
= zpool_prop_default_numeric(ZPOOL_PROP_AUTOTRIM
);
5350 if (props
!= NULL
) {
5351 spa_configfile_set(spa
, props
, B_FALSE
);
5352 spa_sync_props(props
, tx
);
5357 spa
->spa_sync_on
= B_TRUE
;
5359 mmp_thread_start(spa
);
5360 txg_wait_synced(dp
, txg
);
5362 spa_spawn_aux_threads(spa
);
5364 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
);
5367 * Don't count references from objsets that are already closed
5368 * and are making their way through the eviction process.
5370 spa_evicting_os_wait(spa
);
5371 spa
->spa_minref
= zfs_refcount_count(&spa
->spa_refcount
);
5372 spa
->spa_load_state
= SPA_LOAD_NONE
;
5374 mutex_exit(&spa_namespace_lock
);
5380 * Import a non-root pool into the system.
5383 spa_import(char *pool
, nvlist_t
*config
, nvlist_t
*props
, uint64_t flags
)
5386 char *altroot
= NULL
;
5387 spa_load_state_t state
= SPA_LOAD_IMPORT
;
5388 zpool_load_policy_t policy
;
5389 uint64_t mode
= spa_mode_global
;
5390 uint64_t readonly
= B_FALSE
;
5393 nvlist_t
**spares
, **l2cache
;
5394 uint_t nspares
, nl2cache
;
5397 * If a pool with this name exists, return failure.
5399 mutex_enter(&spa_namespace_lock
);
5400 if (spa_lookup(pool
) != NULL
) {
5401 mutex_exit(&spa_namespace_lock
);
5402 return (SET_ERROR(EEXIST
));
5406 * Create and initialize the spa structure.
5408 (void) nvlist_lookup_string(props
,
5409 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
5410 (void) nvlist_lookup_uint64(props
,
5411 zpool_prop_to_name(ZPOOL_PROP_READONLY
), &readonly
);
5414 spa
= spa_add(pool
, config
, altroot
);
5415 spa
->spa_import_flags
= flags
;
5418 * Verbatim import - Take a pool and insert it into the namespace
5419 * as if it had been loaded at boot.
5421 if (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
) {
5423 spa_configfile_set(spa
, props
, B_FALSE
);
5425 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
);
5426 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_IMPORT
);
5427 zfs_dbgmsg("spa_import: verbatim import of %s", pool
);
5428 mutex_exit(&spa_namespace_lock
);
5432 spa_activate(spa
, mode
);
5435 * Don't start async tasks until we know everything is healthy.
5437 spa_async_suspend(spa
);
5439 zpool_get_load_policy(config
, &policy
);
5440 if (policy
.zlp_rewind
& ZPOOL_DO_REWIND
)
5441 state
= SPA_LOAD_RECOVER
;
5443 spa
->spa_config_source
= SPA_CONFIG_SRC_TRYIMPORT
;
5445 if (state
!= SPA_LOAD_RECOVER
) {
5446 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
5447 zfs_dbgmsg("spa_import: importing %s", pool
);
5449 zfs_dbgmsg("spa_import: importing %s, max_txg=%lld "
5450 "(RECOVERY MODE)", pool
, (longlong_t
)policy
.zlp_txg
);
5452 error
= spa_load_best(spa
, state
, policy
.zlp_txg
, policy
.zlp_rewind
);
5455 * Propagate anything learned while loading the pool and pass it
5456 * back to caller (i.e. rewind info, missing devices, etc).
5458 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
5459 spa
->spa_load_info
) == 0);
5461 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5463 * Toss any existing sparelist, as it doesn't have any validity
5464 * anymore, and conflicts with spa_has_spare().
5466 if (spa
->spa_spares
.sav_config
) {
5467 nvlist_free(spa
->spa_spares
.sav_config
);
5468 spa
->spa_spares
.sav_config
= NULL
;
5469 spa_load_spares(spa
);
5471 if (spa
->spa_l2cache
.sav_config
) {
5472 nvlist_free(spa
->spa_l2cache
.sav_config
);
5473 spa
->spa_l2cache
.sav_config
= NULL
;
5474 spa_load_l2cache(spa
);
5477 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
5479 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5482 spa_configfile_set(spa
, props
, B_FALSE
);
5484 if (error
!= 0 || (props
&& spa_writeable(spa
) &&
5485 (error
= spa_prop_set(spa
, props
)))) {
5487 spa_deactivate(spa
);
5489 mutex_exit(&spa_namespace_lock
);
5493 spa_async_resume(spa
);
5496 * Override any spares and level 2 cache devices as specified by
5497 * the user, as these may have correct device names/devids, etc.
5499 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
5500 &spares
, &nspares
) == 0) {
5501 if (spa
->spa_spares
.sav_config
)
5502 VERIFY(nvlist_remove(spa
->spa_spares
.sav_config
,
5503 ZPOOL_CONFIG_SPARES
, DATA_TYPE_NVLIST_ARRAY
) == 0);
5505 VERIFY(nvlist_alloc(&spa
->spa_spares
.sav_config
,
5506 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
5507 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
5508 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
5509 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5510 spa_load_spares(spa
);
5511 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5512 spa
->spa_spares
.sav_sync
= B_TRUE
;
5514 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
5515 &l2cache
, &nl2cache
) == 0) {
5516 if (spa
->spa_l2cache
.sav_config
)
5517 VERIFY(nvlist_remove(spa
->spa_l2cache
.sav_config
,
5518 ZPOOL_CONFIG_L2CACHE
, DATA_TYPE_NVLIST_ARRAY
) == 0);
5520 VERIFY(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
5521 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
5522 VERIFY(nvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
5523 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
5524 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5525 spa_load_l2cache(spa
);
5526 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5527 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
5531 * Check for any removed devices.
5533 if (spa
->spa_autoreplace
) {
5534 spa_aux_check_removed(&spa
->spa_spares
);
5535 spa_aux_check_removed(&spa
->spa_l2cache
);
5538 if (spa_writeable(spa
)) {
5540 * Update the config cache to include the newly-imported pool.
5542 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
5546 * It's possible that the pool was expanded while it was exported.
5547 * We kick off an async task to handle this for us.
5549 spa_async_request(spa
, SPA_ASYNC_AUTOEXPAND
);
5551 spa_history_log_version(spa
, "import", NULL
);
5553 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_IMPORT
);
5555 zvol_create_minors(spa
, pool
, B_TRUE
);
5557 mutex_exit(&spa_namespace_lock
);
5563 spa_tryimport(nvlist_t
*tryconfig
)
5565 nvlist_t
*config
= NULL
;
5566 char *poolname
, *cachefile
;
5570 zpool_load_policy_t policy
;
5572 if (nvlist_lookup_string(tryconfig
, ZPOOL_CONFIG_POOL_NAME
, &poolname
))
5575 if (nvlist_lookup_uint64(tryconfig
, ZPOOL_CONFIG_POOL_STATE
, &state
))
5579 * Create and initialize the spa structure.
5581 mutex_enter(&spa_namespace_lock
);
5582 spa
= spa_add(TRYIMPORT_NAME
, tryconfig
, NULL
);
5583 spa_activate(spa
, FREAD
);
5586 * Rewind pool if a max txg was provided.
5588 zpool_get_load_policy(spa
->spa_config
, &policy
);
5589 if (policy
.zlp_txg
!= UINT64_MAX
) {
5590 spa
->spa_load_max_txg
= policy
.zlp_txg
;
5591 spa
->spa_extreme_rewind
= B_TRUE
;
5592 zfs_dbgmsg("spa_tryimport: importing %s, max_txg=%lld",
5593 poolname
, (longlong_t
)policy
.zlp_txg
);
5595 zfs_dbgmsg("spa_tryimport: importing %s", poolname
);
5598 if (nvlist_lookup_string(tryconfig
, ZPOOL_CONFIG_CACHEFILE
, &cachefile
)
5600 zfs_dbgmsg("spa_tryimport: using cachefile '%s'", cachefile
);
5601 spa
->spa_config_source
= SPA_CONFIG_SRC_CACHEFILE
;
5603 spa
->spa_config_source
= SPA_CONFIG_SRC_SCAN
;
5606 error
= spa_load(spa
, SPA_LOAD_TRYIMPORT
, SPA_IMPORT_EXISTING
);
5609 * If 'tryconfig' was at least parsable, return the current config.
5611 if (spa
->spa_root_vdev
!= NULL
) {
5612 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
5613 VERIFY(nvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
,
5615 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
5617 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_TIMESTAMP
,
5618 spa
->spa_uberblock
.ub_timestamp
) == 0);
5619 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
5620 spa
->spa_load_info
) == 0);
5621 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_ERRATA
,
5622 spa
->spa_errata
) == 0);
5625 * If the bootfs property exists on this pool then we
5626 * copy it out so that external consumers can tell which
5627 * pools are bootable.
5629 if ((!error
|| error
== EEXIST
) && spa
->spa_bootfs
) {
5630 char *tmpname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
5633 * We have to play games with the name since the
5634 * pool was opened as TRYIMPORT_NAME.
5636 if (dsl_dsobj_to_dsname(spa_name(spa
),
5637 spa
->spa_bootfs
, tmpname
) == 0) {
5641 dsname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
5643 cp
= strchr(tmpname
, '/');
5645 (void) strlcpy(dsname
, tmpname
,
5648 (void) snprintf(dsname
, MAXPATHLEN
,
5649 "%s/%s", poolname
, ++cp
);
5651 VERIFY(nvlist_add_string(config
,
5652 ZPOOL_CONFIG_BOOTFS
, dsname
) == 0);
5653 kmem_free(dsname
, MAXPATHLEN
);
5655 kmem_free(tmpname
, MAXPATHLEN
);
5659 * Add the list of hot spares and level 2 cache devices.
5661 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
5662 spa_add_spares(spa
, config
);
5663 spa_add_l2cache(spa
, config
);
5664 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
5668 spa_deactivate(spa
);
5670 mutex_exit(&spa_namespace_lock
);
5676 * Pool export/destroy
5678 * The act of destroying or exporting a pool is very simple. We make sure there
5679 * is no more pending I/O and any references to the pool are gone. Then, we
5680 * update the pool state and sync all the labels to disk, removing the
5681 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
5682 * we don't sync the labels or remove the configuration cache.
5685 spa_export_common(char *pool
, int new_state
, nvlist_t
**oldconfig
,
5686 boolean_t force
, boolean_t hardforce
)
5693 if (!(spa_mode_global
& FWRITE
))
5694 return (SET_ERROR(EROFS
));
5696 mutex_enter(&spa_namespace_lock
);
5697 if ((spa
= spa_lookup(pool
)) == NULL
) {
5698 mutex_exit(&spa_namespace_lock
);
5699 return (SET_ERROR(ENOENT
));
5703 * Put a hold on the pool, drop the namespace lock, stop async tasks,
5704 * reacquire the namespace lock, and see if we can export.
5706 spa_open_ref(spa
, FTAG
);
5707 mutex_exit(&spa_namespace_lock
);
5708 spa_async_suspend(spa
);
5709 if (spa
->spa_zvol_taskq
) {
5710 zvol_remove_minors(spa
, spa_name(spa
), B_TRUE
);
5711 taskq_wait(spa
->spa_zvol_taskq
);
5713 mutex_enter(&spa_namespace_lock
);
5714 spa_close(spa
, FTAG
);
5716 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
)
5719 * The pool will be in core if it's openable, in which case we can
5720 * modify its state. Objsets may be open only because they're dirty,
5721 * so we have to force it to sync before checking spa_refcnt.
5723 if (spa
->spa_sync_on
) {
5724 txg_wait_synced(spa
->spa_dsl_pool
, 0);
5725 spa_evicting_os_wait(spa
);
5729 * A pool cannot be exported or destroyed if there are active
5730 * references. If we are resetting a pool, allow references by
5731 * fault injection handlers.
5733 if (!spa_refcount_zero(spa
) ||
5734 (spa
->spa_inject_ref
!= 0 &&
5735 new_state
!= POOL_STATE_UNINITIALIZED
)) {
5736 spa_async_resume(spa
);
5737 mutex_exit(&spa_namespace_lock
);
5738 return (SET_ERROR(EBUSY
));
5741 if (spa
->spa_sync_on
) {
5743 * A pool cannot be exported if it has an active shared spare.
5744 * This is to prevent other pools stealing the active spare
5745 * from an exported pool. At user's own will, such pool can
5746 * be forcedly exported.
5748 if (!force
&& new_state
== POOL_STATE_EXPORTED
&&
5749 spa_has_active_shared_spare(spa
)) {
5750 spa_async_resume(spa
);
5751 mutex_exit(&spa_namespace_lock
);
5752 return (SET_ERROR(EXDEV
));
5756 * We're about to export or destroy this pool. Make sure
5757 * we stop all initialization and trim activity here before
5758 * we set the spa_final_txg. This will ensure that all
5759 * dirty data resulting from the initialization is
5760 * committed to disk before we unload the pool.
5762 if (spa
->spa_root_vdev
!= NULL
) {
5763 vdev_t
*rvd
= spa
->spa_root_vdev
;
5764 vdev_initialize_stop_all(rvd
, VDEV_INITIALIZE_ACTIVE
);
5765 vdev_trim_stop_all(rvd
, VDEV_TRIM_ACTIVE
);
5766 vdev_autotrim_stop_all(spa
);
5770 * We want this to be reflected on every label,
5771 * so mark them all dirty. spa_unload() will do the
5772 * final sync that pushes these changes out.
5774 if (new_state
!= POOL_STATE_UNINITIALIZED
&& !hardforce
) {
5775 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5776 spa
->spa_state
= new_state
;
5777 spa
->spa_final_txg
= spa_last_synced_txg(spa
) +
5779 vdev_config_dirty(spa
->spa_root_vdev
);
5780 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5785 if (new_state
== POOL_STATE_DESTROYED
)
5786 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_DESTROY
);
5787 else if (new_state
== POOL_STATE_EXPORTED
)
5788 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_EXPORT
);
5790 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
5792 spa_deactivate(spa
);
5795 if (oldconfig
&& spa
->spa_config
)
5796 VERIFY(nvlist_dup(spa
->spa_config
, oldconfig
, 0) == 0);
5798 if (new_state
!= POOL_STATE_UNINITIALIZED
) {
5800 spa_write_cachefile(spa
, B_TRUE
, B_TRUE
);
5803 mutex_exit(&spa_namespace_lock
);
5809 * Destroy a storage pool.
5812 spa_destroy(char *pool
)
5814 return (spa_export_common(pool
, POOL_STATE_DESTROYED
, NULL
,
5819 * Export a storage pool.
5822 spa_export(char *pool
, nvlist_t
**oldconfig
, boolean_t force
,
5823 boolean_t hardforce
)
5825 return (spa_export_common(pool
, POOL_STATE_EXPORTED
, oldconfig
,
5830 * Similar to spa_export(), this unloads the spa_t without actually removing it
5831 * from the namespace in any way.
5834 spa_reset(char *pool
)
5836 return (spa_export_common(pool
, POOL_STATE_UNINITIALIZED
, NULL
,
5841 * ==========================================================================
5842 * Device manipulation
5843 * ==========================================================================
5847 * Add a device to a storage pool.
5850 spa_vdev_add(spa_t
*spa
, nvlist_t
*nvroot
)
5854 vdev_t
*rvd
= spa
->spa_root_vdev
;
5856 nvlist_t
**spares
, **l2cache
;
5857 uint_t nspares
, nl2cache
;
5859 ASSERT(spa_writeable(spa
));
5861 txg
= spa_vdev_enter(spa
);
5863 if ((error
= spa_config_parse(spa
, &vd
, nvroot
, NULL
, 0,
5864 VDEV_ALLOC_ADD
)) != 0)
5865 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5867 spa
->spa_pending_vdev
= vd
; /* spa_vdev_exit() will clear this */
5869 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
, &spares
,
5873 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
, &l2cache
,
5877 if (vd
->vdev_children
== 0 && nspares
== 0 && nl2cache
== 0)
5878 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
5880 if (vd
->vdev_children
!= 0 &&
5881 (error
= vdev_create(vd
, txg
, B_FALSE
)) != 0)
5882 return (spa_vdev_exit(spa
, vd
, txg
, error
));
5885 * We must validate the spares and l2cache devices after checking the
5886 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
5888 if ((error
= spa_validate_aux(spa
, nvroot
, txg
, VDEV_ALLOC_ADD
)) != 0)
5889 return (spa_vdev_exit(spa
, vd
, txg
, error
));
5892 * If we are in the middle of a device removal, we can only add
5893 * devices which match the existing devices in the pool.
5894 * If we are in the middle of a removal, or have some indirect
5895 * vdevs, we can not add raidz toplevels.
5897 if (spa
->spa_vdev_removal
!= NULL
||
5898 spa
->spa_removing_phys
.sr_prev_indirect_vdev
!= -1) {
5899 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
5900 tvd
= vd
->vdev_child
[c
];
5901 if (spa
->spa_vdev_removal
!= NULL
&&
5902 tvd
->vdev_ashift
!= spa
->spa_max_ashift
) {
5903 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
5905 /* Fail if top level vdev is raidz */
5906 if (tvd
->vdev_ops
== &vdev_raidz_ops
) {
5907 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
5910 * Need the top level mirror to be
5911 * a mirror of leaf vdevs only
5913 if (tvd
->vdev_ops
== &vdev_mirror_ops
) {
5914 for (uint64_t cid
= 0;
5915 cid
< tvd
->vdev_children
; cid
++) {
5916 vdev_t
*cvd
= tvd
->vdev_child
[cid
];
5917 if (!cvd
->vdev_ops
->vdev_op_leaf
) {
5918 return (spa_vdev_exit(spa
, vd
,
5926 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
5929 * Set the vdev id to the first hole, if one exists.
5931 for (id
= 0; id
< rvd
->vdev_children
; id
++) {
5932 if (rvd
->vdev_child
[id
]->vdev_ishole
) {
5933 vdev_free(rvd
->vdev_child
[id
]);
5937 tvd
= vd
->vdev_child
[c
];
5938 vdev_remove_child(vd
, tvd
);
5940 vdev_add_child(rvd
, tvd
);
5941 vdev_config_dirty(tvd
);
5945 spa_set_aux_vdevs(&spa
->spa_spares
, spares
, nspares
,
5946 ZPOOL_CONFIG_SPARES
);
5947 spa_load_spares(spa
);
5948 spa
->spa_spares
.sav_sync
= B_TRUE
;
5951 if (nl2cache
!= 0) {
5952 spa_set_aux_vdevs(&spa
->spa_l2cache
, l2cache
, nl2cache
,
5953 ZPOOL_CONFIG_L2CACHE
);
5954 spa_load_l2cache(spa
);
5955 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
5959 * We have to be careful when adding new vdevs to an existing pool.
5960 * If other threads start allocating from these vdevs before we
5961 * sync the config cache, and we lose power, then upon reboot we may
5962 * fail to open the pool because there are DVAs that the config cache
5963 * can't translate. Therefore, we first add the vdevs without
5964 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
5965 * and then let spa_config_update() initialize the new metaslabs.
5967 * spa_load() checks for added-but-not-initialized vdevs, so that
5968 * if we lose power at any point in this sequence, the remaining
5969 * steps will be completed the next time we load the pool.
5971 (void) spa_vdev_exit(spa
, vd
, txg
, 0);
5973 mutex_enter(&spa_namespace_lock
);
5974 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
5975 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_VDEV_ADD
);
5976 mutex_exit(&spa_namespace_lock
);
5982 * Attach a device to a mirror. The arguments are the path to any device
5983 * in the mirror, and the nvroot for the new device. If the path specifies
5984 * a device that is not mirrored, we automatically insert the mirror vdev.
5986 * If 'replacing' is specified, the new device is intended to replace the
5987 * existing device; in this case the two devices are made into their own
5988 * mirror using the 'replacing' vdev, which is functionally identical to
5989 * the mirror vdev (it actually reuses all the same ops) but has a few
5990 * extra rules: you can't attach to it after it's been created, and upon
5991 * completion of resilvering, the first disk (the one being replaced)
5992 * is automatically detached.
5995 spa_vdev_attach(spa_t
*spa
, uint64_t guid
, nvlist_t
*nvroot
, int replacing
)
5997 uint64_t txg
, dtl_max_txg
;
5998 ASSERTV(vdev_t
*rvd
= spa
->spa_root_vdev
);
5999 vdev_t
*oldvd
, *newvd
, *newrootvd
, *pvd
, *tvd
;
6001 char *oldvdpath
, *newvdpath
;
6005 ASSERT(spa_writeable(spa
));
6007 txg
= spa_vdev_enter(spa
);
6009 oldvd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
6011 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
6012 if (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
)) {
6013 error
= (spa_has_checkpoint(spa
)) ?
6014 ZFS_ERR_CHECKPOINT_EXISTS
: ZFS_ERR_DISCARDING_CHECKPOINT
;
6015 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
6018 if (spa
->spa_vdev_removal
!= NULL
)
6019 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
6022 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
6024 if (!oldvd
->vdev_ops
->vdev_op_leaf
)
6025 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
6027 pvd
= oldvd
->vdev_parent
;
6029 if ((error
= spa_config_parse(spa
, &newrootvd
, nvroot
, NULL
, 0,
6030 VDEV_ALLOC_ATTACH
)) != 0)
6031 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
6033 if (newrootvd
->vdev_children
!= 1)
6034 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
6036 newvd
= newrootvd
->vdev_child
[0];
6038 if (!newvd
->vdev_ops
->vdev_op_leaf
)
6039 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
6041 if ((error
= vdev_create(newrootvd
, txg
, replacing
)) != 0)
6042 return (spa_vdev_exit(spa
, newrootvd
, txg
, error
));
6045 * Spares can't replace logs
6047 if (oldvd
->vdev_top
->vdev_islog
&& newvd
->vdev_isspare
)
6048 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
6052 * For attach, the only allowable parent is a mirror or the root
6055 if (pvd
->vdev_ops
!= &vdev_mirror_ops
&&
6056 pvd
->vdev_ops
!= &vdev_root_ops
)
6057 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
6059 pvops
= &vdev_mirror_ops
;
6062 * Active hot spares can only be replaced by inactive hot
6065 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
6066 oldvd
->vdev_isspare
&&
6067 !spa_has_spare(spa
, newvd
->vdev_guid
))
6068 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
6071 * If the source is a hot spare, and the parent isn't already a
6072 * spare, then we want to create a new hot spare. Otherwise, we
6073 * want to create a replacing vdev. The user is not allowed to
6074 * attach to a spared vdev child unless the 'isspare' state is
6075 * the same (spare replaces spare, non-spare replaces
6078 if (pvd
->vdev_ops
== &vdev_replacing_ops
&&
6079 spa_version(spa
) < SPA_VERSION_MULTI_REPLACE
) {
6080 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
6081 } else if (pvd
->vdev_ops
== &vdev_spare_ops
&&
6082 newvd
->vdev_isspare
!= oldvd
->vdev_isspare
) {
6083 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
6086 if (newvd
->vdev_isspare
)
6087 pvops
= &vdev_spare_ops
;
6089 pvops
= &vdev_replacing_ops
;
6093 * Make sure the new device is big enough.
6095 if (newvd
->vdev_asize
< vdev_get_min_asize(oldvd
))
6096 return (spa_vdev_exit(spa
, newrootvd
, txg
, EOVERFLOW
));
6099 * The new device cannot have a higher alignment requirement
6100 * than the top-level vdev.
6102 if (newvd
->vdev_ashift
> oldvd
->vdev_top
->vdev_ashift
)
6103 return (spa_vdev_exit(spa
, newrootvd
, txg
, EDOM
));
6106 * If this is an in-place replacement, update oldvd's path and devid
6107 * to make it distinguishable from newvd, and unopenable from now on.
6109 if (strcmp(oldvd
->vdev_path
, newvd
->vdev_path
) == 0) {
6110 spa_strfree(oldvd
->vdev_path
);
6111 oldvd
->vdev_path
= kmem_alloc(strlen(newvd
->vdev_path
) + 5,
6113 (void) sprintf(oldvd
->vdev_path
, "%s/%s",
6114 newvd
->vdev_path
, "old");
6115 if (oldvd
->vdev_devid
!= NULL
) {
6116 spa_strfree(oldvd
->vdev_devid
);
6117 oldvd
->vdev_devid
= NULL
;
6121 /* mark the device being resilvered */
6122 newvd
->vdev_resilver_txg
= txg
;
6125 * If the parent is not a mirror, or if we're replacing, insert the new
6126 * mirror/replacing/spare vdev above oldvd.
6128 if (pvd
->vdev_ops
!= pvops
)
6129 pvd
= vdev_add_parent(oldvd
, pvops
);
6131 ASSERT(pvd
->vdev_top
->vdev_parent
== rvd
);
6132 ASSERT(pvd
->vdev_ops
== pvops
);
6133 ASSERT(oldvd
->vdev_parent
== pvd
);
6136 * Extract the new device from its root and add it to pvd.
6138 vdev_remove_child(newrootvd
, newvd
);
6139 newvd
->vdev_id
= pvd
->vdev_children
;
6140 newvd
->vdev_crtxg
= oldvd
->vdev_crtxg
;
6141 vdev_add_child(pvd
, newvd
);
6144 * Reevaluate the parent vdev state.
6146 vdev_propagate_state(pvd
);
6148 tvd
= newvd
->vdev_top
;
6149 ASSERT(pvd
->vdev_top
== tvd
);
6150 ASSERT(tvd
->vdev_parent
== rvd
);
6152 vdev_config_dirty(tvd
);
6155 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
6156 * for any dmu_sync-ed blocks. It will propagate upward when
6157 * spa_vdev_exit() calls vdev_dtl_reassess().
6159 dtl_max_txg
= txg
+ TXG_CONCURRENT_STATES
;
6161 vdev_dtl_dirty(newvd
, DTL_MISSING
, TXG_INITIAL
,
6162 dtl_max_txg
- TXG_INITIAL
);
6164 if (newvd
->vdev_isspare
) {
6165 spa_spare_activate(newvd
);
6166 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_VDEV_SPARE
);
6169 oldvdpath
= spa_strdup(oldvd
->vdev_path
);
6170 newvdpath
= spa_strdup(newvd
->vdev_path
);
6171 newvd_isspare
= newvd
->vdev_isspare
;
6174 * Mark newvd's DTL dirty in this txg.
6176 vdev_dirty(tvd
, VDD_DTL
, newvd
, txg
);
6179 * Schedule the resilver to restart in the future. We do this to
6180 * ensure that dmu_sync-ed blocks have been stitched into the
6181 * respective datasets. We do not do this if resilvers have been
6184 if (dsl_scan_resilvering(spa_get_dsl(spa
)) &&
6185 spa_feature_is_enabled(spa
, SPA_FEATURE_RESILVER_DEFER
))
6186 vdev_set_deferred_resilver(spa
, newvd
);
6188 dsl_resilver_restart(spa
->spa_dsl_pool
, dtl_max_txg
);
6190 if (spa
->spa_bootfs
)
6191 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_BOOTFS_VDEV_ATTACH
);
6193 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_VDEV_ATTACH
);
6198 (void) spa_vdev_exit(spa
, newrootvd
, dtl_max_txg
, 0);
6200 spa_history_log_internal(spa
, "vdev attach", NULL
,
6201 "%s vdev=%s %s vdev=%s",
6202 replacing
&& newvd_isspare
? "spare in" :
6203 replacing
? "replace" : "attach", newvdpath
,
6204 replacing
? "for" : "to", oldvdpath
);
6206 spa_strfree(oldvdpath
);
6207 spa_strfree(newvdpath
);
6213 * Detach a device from a mirror or replacing vdev.
6215 * If 'replace_done' is specified, only detach if the parent
6216 * is a replacing vdev.
6219 spa_vdev_detach(spa_t
*spa
, uint64_t guid
, uint64_t pguid
, int replace_done
)
6223 ASSERTV(vdev_t
*rvd
= spa
->spa_root_vdev
);
6224 vdev_t
*vd
, *pvd
, *cvd
, *tvd
;
6225 boolean_t unspare
= B_FALSE
;
6226 uint64_t unspare_guid
= 0;
6229 ASSERT(spa_writeable(spa
));
6231 txg
= spa_vdev_enter(spa
);
6233 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
6236 * Besides being called directly from the userland through the
6237 * ioctl interface, spa_vdev_detach() can be potentially called
6238 * at the end of spa_vdev_resilver_done().
6240 * In the regular case, when we have a checkpoint this shouldn't
6241 * happen as we never empty the DTLs of a vdev during the scrub
6242 * [see comment in dsl_scan_done()]. Thus spa_vdev_resilvering_done()
6243 * should never get here when we have a checkpoint.
6245 * That said, even in a case when we checkpoint the pool exactly
6246 * as spa_vdev_resilver_done() calls this function everything
6247 * should be fine as the resilver will return right away.
6249 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
6250 if (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
)) {
6251 error
= (spa_has_checkpoint(spa
)) ?
6252 ZFS_ERR_CHECKPOINT_EXISTS
: ZFS_ERR_DISCARDING_CHECKPOINT
;
6253 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
6257 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
6259 if (!vd
->vdev_ops
->vdev_op_leaf
)
6260 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
6262 pvd
= vd
->vdev_parent
;
6265 * If the parent/child relationship is not as expected, don't do it.
6266 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
6267 * vdev that's replacing B with C. The user's intent in replacing
6268 * is to go from M(A,B) to M(A,C). If the user decides to cancel
6269 * the replace by detaching C, the expected behavior is to end up
6270 * M(A,B). But suppose that right after deciding to detach C,
6271 * the replacement of B completes. We would have M(A,C), and then
6272 * ask to detach C, which would leave us with just A -- not what
6273 * the user wanted. To prevent this, we make sure that the
6274 * parent/child relationship hasn't changed -- in this example,
6275 * that C's parent is still the replacing vdev R.
6277 if (pvd
->vdev_guid
!= pguid
&& pguid
!= 0)
6278 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
6281 * Only 'replacing' or 'spare' vdevs can be replaced.
6283 if (replace_done
&& pvd
->vdev_ops
!= &vdev_replacing_ops
&&
6284 pvd
->vdev_ops
!= &vdev_spare_ops
)
6285 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
6287 ASSERT(pvd
->vdev_ops
!= &vdev_spare_ops
||
6288 spa_version(spa
) >= SPA_VERSION_SPARES
);
6291 * Only mirror, replacing, and spare vdevs support detach.
6293 if (pvd
->vdev_ops
!= &vdev_replacing_ops
&&
6294 pvd
->vdev_ops
!= &vdev_mirror_ops
&&
6295 pvd
->vdev_ops
!= &vdev_spare_ops
)
6296 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
6299 * If this device has the only valid copy of some data,
6300 * we cannot safely detach it.
6302 if (vdev_dtl_required(vd
))
6303 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
6305 ASSERT(pvd
->vdev_children
>= 2);
6308 * If we are detaching the second disk from a replacing vdev, then
6309 * check to see if we changed the original vdev's path to have "/old"
6310 * at the end in spa_vdev_attach(). If so, undo that change now.
6312 if (pvd
->vdev_ops
== &vdev_replacing_ops
&& vd
->vdev_id
> 0 &&
6313 vd
->vdev_path
!= NULL
) {
6314 size_t len
= strlen(vd
->vdev_path
);
6316 for (int c
= 0; c
< pvd
->vdev_children
; c
++) {
6317 cvd
= pvd
->vdev_child
[c
];
6319 if (cvd
== vd
|| cvd
->vdev_path
== NULL
)
6322 if (strncmp(cvd
->vdev_path
, vd
->vdev_path
, len
) == 0 &&
6323 strcmp(cvd
->vdev_path
+ len
, "/old") == 0) {
6324 spa_strfree(cvd
->vdev_path
);
6325 cvd
->vdev_path
= spa_strdup(vd
->vdev_path
);
6332 * If we are detaching the original disk from a spare, then it implies
6333 * that the spare should become a real disk, and be removed from the
6334 * active spare list for the pool.
6336 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
6338 pvd
->vdev_child
[pvd
->vdev_children
- 1]->vdev_isspare
)
6342 * Erase the disk labels so the disk can be used for other things.
6343 * This must be done after all other error cases are handled,
6344 * but before we disembowel vd (so we can still do I/O to it).
6345 * But if we can't do it, don't treat the error as fatal --
6346 * it may be that the unwritability of the disk is the reason
6347 * it's being detached!
6349 error
= vdev_label_init(vd
, 0, VDEV_LABEL_REMOVE
);
6352 * Remove vd from its parent and compact the parent's children.
6354 vdev_remove_child(pvd
, vd
);
6355 vdev_compact_children(pvd
);
6358 * Remember one of the remaining children so we can get tvd below.
6360 cvd
= pvd
->vdev_child
[pvd
->vdev_children
- 1];
6363 * If we need to remove the remaining child from the list of hot spares,
6364 * do it now, marking the vdev as no longer a spare in the process.
6365 * We must do this before vdev_remove_parent(), because that can
6366 * change the GUID if it creates a new toplevel GUID. For a similar
6367 * reason, we must remove the spare now, in the same txg as the detach;
6368 * otherwise someone could attach a new sibling, change the GUID, and
6369 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
6372 ASSERT(cvd
->vdev_isspare
);
6373 spa_spare_remove(cvd
);
6374 unspare_guid
= cvd
->vdev_guid
;
6375 (void) spa_vdev_remove(spa
, unspare_guid
, B_TRUE
);
6376 cvd
->vdev_unspare
= B_TRUE
;
6380 * If the parent mirror/replacing vdev only has one child,
6381 * the parent is no longer needed. Remove it from the tree.
6383 if (pvd
->vdev_children
== 1) {
6384 if (pvd
->vdev_ops
== &vdev_spare_ops
)
6385 cvd
->vdev_unspare
= B_FALSE
;
6386 vdev_remove_parent(cvd
);
6390 * We don't set tvd until now because the parent we just removed
6391 * may have been the previous top-level vdev.
6393 tvd
= cvd
->vdev_top
;
6394 ASSERT(tvd
->vdev_parent
== rvd
);
6397 * Reevaluate the parent vdev state.
6399 vdev_propagate_state(cvd
);
6402 * If the 'autoexpand' property is set on the pool then automatically
6403 * try to expand the size of the pool. For example if the device we
6404 * just detached was smaller than the others, it may be possible to
6405 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
6406 * first so that we can obtain the updated sizes of the leaf vdevs.
6408 if (spa
->spa_autoexpand
) {
6410 vdev_expand(tvd
, txg
);
6413 vdev_config_dirty(tvd
);
6416 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
6417 * vd->vdev_detached is set and free vd's DTL object in syncing context.
6418 * But first make sure we're not on any *other* txg's DTL list, to
6419 * prevent vd from being accessed after it's freed.
6421 vdpath
= spa_strdup(vd
->vdev_path
? vd
->vdev_path
: "none");
6422 for (int t
= 0; t
< TXG_SIZE
; t
++)
6423 (void) txg_list_remove_this(&tvd
->vdev_dtl_list
, vd
, t
);
6424 vd
->vdev_detached
= B_TRUE
;
6425 vdev_dirty(tvd
, VDD_DTL
, vd
, txg
);
6427 spa_event_notify(spa
, vd
, NULL
, ESC_ZFS_VDEV_REMOVE
);
6429 /* hang on to the spa before we release the lock */
6430 spa_open_ref(spa
, FTAG
);
6432 error
= spa_vdev_exit(spa
, vd
, txg
, 0);
6434 spa_history_log_internal(spa
, "detach", NULL
,
6436 spa_strfree(vdpath
);
6439 * If this was the removal of the original device in a hot spare vdev,
6440 * then we want to go through and remove the device from the hot spare
6441 * list of every other pool.
6444 spa_t
*altspa
= NULL
;
6446 mutex_enter(&spa_namespace_lock
);
6447 while ((altspa
= spa_next(altspa
)) != NULL
) {
6448 if (altspa
->spa_state
!= POOL_STATE_ACTIVE
||
6452 spa_open_ref(altspa
, FTAG
);
6453 mutex_exit(&spa_namespace_lock
);
6454 (void) spa_vdev_remove(altspa
, unspare_guid
, B_TRUE
);
6455 mutex_enter(&spa_namespace_lock
);
6456 spa_close(altspa
, FTAG
);
6458 mutex_exit(&spa_namespace_lock
);
6460 /* search the rest of the vdevs for spares to remove */
6461 spa_vdev_resilver_done(spa
);
6464 /* all done with the spa; OK to release */
6465 mutex_enter(&spa_namespace_lock
);
6466 spa_close(spa
, FTAG
);
6467 mutex_exit(&spa_namespace_lock
);
6473 spa_vdev_initialize_impl(spa_t
*spa
, uint64_t guid
, uint64_t cmd_type
,
6476 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
6478 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
6480 /* Look up vdev and ensure it's a leaf. */
6481 vdev_t
*vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
6482 if (vd
== NULL
|| vd
->vdev_detached
) {
6483 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
6484 return (SET_ERROR(ENODEV
));
6485 } else if (!vd
->vdev_ops
->vdev_op_leaf
|| !vdev_is_concrete(vd
)) {
6486 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
6487 return (SET_ERROR(EINVAL
));
6488 } else if (!vdev_writeable(vd
)) {
6489 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
6490 return (SET_ERROR(EROFS
));
6492 mutex_enter(&vd
->vdev_initialize_lock
);
6493 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
6496 * When we activate an initialize action we check to see
6497 * if the vdev_initialize_thread is NULL. We do this instead
6498 * of using the vdev_initialize_state since there might be
6499 * a previous initialization process which has completed but
6500 * the thread is not exited.
6502 if (cmd_type
== POOL_INITIALIZE_START
&&
6503 (vd
->vdev_initialize_thread
!= NULL
||
6504 vd
->vdev_top
->vdev_removing
)) {
6505 mutex_exit(&vd
->vdev_initialize_lock
);
6506 return (SET_ERROR(EBUSY
));
6507 } else if (cmd_type
== POOL_INITIALIZE_CANCEL
&&
6508 (vd
->vdev_initialize_state
!= VDEV_INITIALIZE_ACTIVE
&&
6509 vd
->vdev_initialize_state
!= VDEV_INITIALIZE_SUSPENDED
)) {
6510 mutex_exit(&vd
->vdev_initialize_lock
);
6511 return (SET_ERROR(ESRCH
));
6512 } else if (cmd_type
== POOL_INITIALIZE_SUSPEND
&&
6513 vd
->vdev_initialize_state
!= VDEV_INITIALIZE_ACTIVE
) {
6514 mutex_exit(&vd
->vdev_initialize_lock
);
6515 return (SET_ERROR(ESRCH
));
6519 case POOL_INITIALIZE_START
:
6520 vdev_initialize(vd
);
6522 case POOL_INITIALIZE_CANCEL
:
6523 vdev_initialize_stop(vd
, VDEV_INITIALIZE_CANCELED
, vd_list
);
6525 case POOL_INITIALIZE_SUSPEND
:
6526 vdev_initialize_stop(vd
, VDEV_INITIALIZE_SUSPENDED
, vd_list
);
6529 panic("invalid cmd_type %llu", (unsigned long long)cmd_type
);
6531 mutex_exit(&vd
->vdev_initialize_lock
);
6537 spa_vdev_initialize(spa_t
*spa
, nvlist_t
*nv
, uint64_t cmd_type
,
6538 nvlist_t
*vdev_errlist
)
6540 int total_errors
= 0;
6543 list_create(&vd_list
, sizeof (vdev_t
),
6544 offsetof(vdev_t
, vdev_initialize_node
));
6547 * We hold the namespace lock through the whole function
6548 * to prevent any changes to the pool while we're starting or
6549 * stopping initialization. The config and state locks are held so that
6550 * we can properly assess the vdev state before we commit to
6551 * the initializing operation.
6553 mutex_enter(&spa_namespace_lock
);
6555 for (nvpair_t
*pair
= nvlist_next_nvpair(nv
, NULL
);
6556 pair
!= NULL
; pair
= nvlist_next_nvpair(nv
, pair
)) {
6557 uint64_t vdev_guid
= fnvpair_value_uint64(pair
);
6559 int error
= spa_vdev_initialize_impl(spa
, vdev_guid
, cmd_type
,
6562 char guid_as_str
[MAXNAMELEN
];
6564 (void) snprintf(guid_as_str
, sizeof (guid_as_str
),
6565 "%llu", (unsigned long long)vdev_guid
);
6566 fnvlist_add_int64(vdev_errlist
, guid_as_str
, error
);
6571 /* Wait for all initialize threads to stop. */
6572 vdev_initialize_stop_wait(spa
, &vd_list
);
6574 /* Sync out the initializing state */
6575 txg_wait_synced(spa
->spa_dsl_pool
, 0);
6576 mutex_exit(&spa_namespace_lock
);
6578 list_destroy(&vd_list
);
6580 return (total_errors
);
6584 spa_vdev_trim_impl(spa_t
*spa
, uint64_t guid
, uint64_t cmd_type
,
6585 uint64_t rate
, boolean_t partial
, boolean_t secure
, list_t
*vd_list
)
6587 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
6589 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
6591 /* Look up vdev and ensure it's a leaf. */
6592 vdev_t
*vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
6593 if (vd
== NULL
|| vd
->vdev_detached
) {
6594 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
6595 return (SET_ERROR(ENODEV
));
6596 } else if (!vd
->vdev_ops
->vdev_op_leaf
|| !vdev_is_concrete(vd
)) {
6597 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
6598 return (SET_ERROR(EINVAL
));
6599 } else if (!vdev_writeable(vd
)) {
6600 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
6601 return (SET_ERROR(EROFS
));
6602 } else if (!vd
->vdev_has_trim
) {
6603 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
6604 return (SET_ERROR(EOPNOTSUPP
));
6605 } else if (secure
&& !vd
->vdev_has_securetrim
) {
6606 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
6607 return (SET_ERROR(EOPNOTSUPP
));
6609 mutex_enter(&vd
->vdev_trim_lock
);
6610 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
6613 * When we activate a TRIM action we check to see if the
6614 * vdev_trim_thread is NULL. We do this instead of using the
6615 * vdev_trim_state since there might be a previous TRIM process
6616 * which has completed but the thread is not exited.
6618 if (cmd_type
== POOL_TRIM_START
&&
6619 (vd
->vdev_trim_thread
!= NULL
|| vd
->vdev_top
->vdev_removing
)) {
6620 mutex_exit(&vd
->vdev_trim_lock
);
6621 return (SET_ERROR(EBUSY
));
6622 } else if (cmd_type
== POOL_TRIM_CANCEL
&&
6623 (vd
->vdev_trim_state
!= VDEV_TRIM_ACTIVE
&&
6624 vd
->vdev_trim_state
!= VDEV_TRIM_SUSPENDED
)) {
6625 mutex_exit(&vd
->vdev_trim_lock
);
6626 return (SET_ERROR(ESRCH
));
6627 } else if (cmd_type
== POOL_TRIM_SUSPEND
&&
6628 vd
->vdev_trim_state
!= VDEV_TRIM_ACTIVE
) {
6629 mutex_exit(&vd
->vdev_trim_lock
);
6630 return (SET_ERROR(ESRCH
));
6634 case POOL_TRIM_START
:
6635 vdev_trim(vd
, rate
, partial
, secure
);
6637 case POOL_TRIM_CANCEL
:
6638 vdev_trim_stop(vd
, VDEV_TRIM_CANCELED
, vd_list
);
6640 case POOL_TRIM_SUSPEND
:
6641 vdev_trim_stop(vd
, VDEV_TRIM_SUSPENDED
, vd_list
);
6644 panic("invalid cmd_type %llu", (unsigned long long)cmd_type
);
6646 mutex_exit(&vd
->vdev_trim_lock
);
6652 * Initiates a manual TRIM for the requested vdevs. This kicks off individual
6653 * TRIM threads for each child vdev. These threads pass over all of the free
6654 * space in the vdev's metaslabs and issues TRIM commands for that space.
6657 spa_vdev_trim(spa_t
*spa
, nvlist_t
*nv
, uint64_t cmd_type
, uint64_t rate
,
6658 boolean_t partial
, boolean_t secure
, nvlist_t
*vdev_errlist
)
6660 int total_errors
= 0;
6663 list_create(&vd_list
, sizeof (vdev_t
),
6664 offsetof(vdev_t
, vdev_trim_node
));
6667 * We hold the namespace lock through the whole function
6668 * to prevent any changes to the pool while we're starting or
6669 * stopping TRIM. The config and state locks are held so that
6670 * we can properly assess the vdev state before we commit to
6671 * the TRIM operation.
6673 mutex_enter(&spa_namespace_lock
);
6675 for (nvpair_t
*pair
= nvlist_next_nvpair(nv
, NULL
);
6676 pair
!= NULL
; pair
= nvlist_next_nvpair(nv
, pair
)) {
6677 uint64_t vdev_guid
= fnvpair_value_uint64(pair
);
6679 int error
= spa_vdev_trim_impl(spa
, vdev_guid
, cmd_type
,
6680 rate
, partial
, secure
, &vd_list
);
6682 char guid_as_str
[MAXNAMELEN
];
6684 (void) snprintf(guid_as_str
, sizeof (guid_as_str
),
6685 "%llu", (unsigned long long)vdev_guid
);
6686 fnvlist_add_int64(vdev_errlist
, guid_as_str
, error
);
6691 /* Wait for all TRIM threads to stop. */
6692 vdev_trim_stop_wait(spa
, &vd_list
);
6694 /* Sync out the TRIM state */
6695 txg_wait_synced(spa
->spa_dsl_pool
, 0);
6696 mutex_exit(&spa_namespace_lock
);
6698 list_destroy(&vd_list
);
6700 return (total_errors
);
6704 * Split a set of devices from their mirrors, and create a new pool from them.
6707 spa_vdev_split_mirror(spa_t
*spa
, char *newname
, nvlist_t
*config
,
6708 nvlist_t
*props
, boolean_t exp
)
6711 uint64_t txg
, *glist
;
6713 uint_t c
, children
, lastlog
;
6714 nvlist_t
**child
, *nvl
, *tmp
;
6716 char *altroot
= NULL
;
6717 vdev_t
*rvd
, **vml
= NULL
; /* vdev modify list */
6718 boolean_t activate_slog
;
6720 ASSERT(spa_writeable(spa
));
6722 txg
= spa_vdev_enter(spa
);
6724 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
6725 if (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
)) {
6726 error
= (spa_has_checkpoint(spa
)) ?
6727 ZFS_ERR_CHECKPOINT_EXISTS
: ZFS_ERR_DISCARDING_CHECKPOINT
;
6728 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
6731 /* clear the log and flush everything up to now */
6732 activate_slog
= spa_passivate_log(spa
);
6733 (void) spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
6734 error
= spa_reset_logs(spa
);
6735 txg
= spa_vdev_config_enter(spa
);
6738 spa_activate_log(spa
);
6741 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
6743 /* check new spa name before going any further */
6744 if (spa_lookup(newname
) != NULL
)
6745 return (spa_vdev_exit(spa
, NULL
, txg
, EEXIST
));
6748 * scan through all the children to ensure they're all mirrors
6750 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvl
) != 0 ||
6751 nvlist_lookup_nvlist_array(nvl
, ZPOOL_CONFIG_CHILDREN
, &child
,
6753 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
6755 /* first, check to ensure we've got the right child count */
6756 rvd
= spa
->spa_root_vdev
;
6758 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
6759 vdev_t
*vd
= rvd
->vdev_child
[c
];
6761 /* don't count the holes & logs as children */
6762 if (vd
->vdev_islog
|| !vdev_is_concrete(vd
)) {
6770 if (children
!= (lastlog
!= 0 ? lastlog
: rvd
->vdev_children
))
6771 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
6773 /* next, ensure no spare or cache devices are part of the split */
6774 if (nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_SPARES
, &tmp
) == 0 ||
6775 nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_L2CACHE
, &tmp
) == 0)
6776 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
6778 vml
= kmem_zalloc(children
* sizeof (vdev_t
*), KM_SLEEP
);
6779 glist
= kmem_zalloc(children
* sizeof (uint64_t), KM_SLEEP
);
6781 /* then, loop over each vdev and validate it */
6782 for (c
= 0; c
< children
; c
++) {
6783 uint64_t is_hole
= 0;
6785 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
6789 if (spa
->spa_root_vdev
->vdev_child
[c
]->vdev_ishole
||
6790 spa
->spa_root_vdev
->vdev_child
[c
]->vdev_islog
) {
6793 error
= SET_ERROR(EINVAL
);
6798 /* which disk is going to be split? */
6799 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_GUID
,
6801 error
= SET_ERROR(EINVAL
);
6805 /* look it up in the spa */
6806 vml
[c
] = spa_lookup_by_guid(spa
, glist
[c
], B_FALSE
);
6807 if (vml
[c
] == NULL
) {
6808 error
= SET_ERROR(ENODEV
);
6812 /* make sure there's nothing stopping the split */
6813 if (vml
[c
]->vdev_parent
->vdev_ops
!= &vdev_mirror_ops
||
6814 vml
[c
]->vdev_islog
||
6815 !vdev_is_concrete(vml
[c
]) ||
6816 vml
[c
]->vdev_isspare
||
6817 vml
[c
]->vdev_isl2cache
||
6818 !vdev_writeable(vml
[c
]) ||
6819 vml
[c
]->vdev_children
!= 0 ||
6820 vml
[c
]->vdev_state
!= VDEV_STATE_HEALTHY
||
6821 c
!= spa
->spa_root_vdev
->vdev_child
[c
]->vdev_id
) {
6822 error
= SET_ERROR(EINVAL
);
6826 if (vdev_dtl_required(vml
[c
]) ||
6827 vdev_resilver_needed(vml
[c
], NULL
, NULL
)) {
6828 error
= SET_ERROR(EBUSY
);
6832 /* we need certain info from the top level */
6833 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_ARRAY
,
6834 vml
[c
]->vdev_top
->vdev_ms_array
) == 0);
6835 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_SHIFT
,
6836 vml
[c
]->vdev_top
->vdev_ms_shift
) == 0);
6837 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASIZE
,
6838 vml
[c
]->vdev_top
->vdev_asize
) == 0);
6839 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASHIFT
,
6840 vml
[c
]->vdev_top
->vdev_ashift
) == 0);
6842 /* transfer per-vdev ZAPs */
6843 ASSERT3U(vml
[c
]->vdev_leaf_zap
, !=, 0);
6844 VERIFY0(nvlist_add_uint64(child
[c
],
6845 ZPOOL_CONFIG_VDEV_LEAF_ZAP
, vml
[c
]->vdev_leaf_zap
));
6847 ASSERT3U(vml
[c
]->vdev_top
->vdev_top_zap
, !=, 0);
6848 VERIFY0(nvlist_add_uint64(child
[c
],
6849 ZPOOL_CONFIG_VDEV_TOP_ZAP
,
6850 vml
[c
]->vdev_parent
->vdev_top_zap
));
6854 kmem_free(vml
, children
* sizeof (vdev_t
*));
6855 kmem_free(glist
, children
* sizeof (uint64_t));
6856 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
6859 /* stop writers from using the disks */
6860 for (c
= 0; c
< children
; c
++) {
6862 vml
[c
]->vdev_offline
= B_TRUE
;
6864 vdev_reopen(spa
->spa_root_vdev
);
6867 * Temporarily record the splitting vdevs in the spa config. This
6868 * will disappear once the config is regenerated.
6870 VERIFY(nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
6871 VERIFY(nvlist_add_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
6872 glist
, children
) == 0);
6873 kmem_free(glist
, children
* sizeof (uint64_t));
6875 mutex_enter(&spa
->spa_props_lock
);
6876 VERIFY(nvlist_add_nvlist(spa
->spa_config
, ZPOOL_CONFIG_SPLIT
,
6878 mutex_exit(&spa
->spa_props_lock
);
6879 spa
->spa_config_splitting
= nvl
;
6880 vdev_config_dirty(spa
->spa_root_vdev
);
6882 /* configure and create the new pool */
6883 VERIFY(nvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
, newname
) == 0);
6884 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
6885 exp
? POOL_STATE_EXPORTED
: POOL_STATE_ACTIVE
) == 0);
6886 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
6887 spa_version(spa
)) == 0);
6888 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
6889 spa
->spa_config_txg
) == 0);
6890 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
6891 spa_generate_guid(NULL
)) == 0);
6892 VERIFY0(nvlist_add_boolean(config
, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
));
6893 (void) nvlist_lookup_string(props
,
6894 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
6896 /* add the new pool to the namespace */
6897 newspa
= spa_add(newname
, config
, altroot
);
6898 newspa
->spa_avz_action
= AVZ_ACTION_REBUILD
;
6899 newspa
->spa_config_txg
= spa
->spa_config_txg
;
6900 spa_set_log_state(newspa
, SPA_LOG_CLEAR
);
6902 /* release the spa config lock, retaining the namespace lock */
6903 spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
6905 if (zio_injection_enabled
)
6906 zio_handle_panic_injection(spa
, FTAG
, 1);
6908 spa_activate(newspa
, spa_mode_global
);
6909 spa_async_suspend(newspa
);
6912 * Temporarily stop the initializing and TRIM activity. We set the
6913 * state to ACTIVE so that we know to resume initializing or TRIM
6914 * once the split has completed.
6916 list_t vd_initialize_list
;
6917 list_create(&vd_initialize_list
, sizeof (vdev_t
),
6918 offsetof(vdev_t
, vdev_initialize_node
));
6920 list_t vd_trim_list
;
6921 list_create(&vd_trim_list
, sizeof (vdev_t
),
6922 offsetof(vdev_t
, vdev_trim_node
));
6924 for (c
= 0; c
< children
; c
++) {
6925 if (vml
[c
] != NULL
) {
6926 mutex_enter(&vml
[c
]->vdev_initialize_lock
);
6927 vdev_initialize_stop(vml
[c
],
6928 VDEV_INITIALIZE_ACTIVE
, &vd_initialize_list
);
6929 mutex_exit(&vml
[c
]->vdev_initialize_lock
);
6931 mutex_enter(&vml
[c
]->vdev_trim_lock
);
6932 vdev_trim_stop(vml
[c
], VDEV_TRIM_ACTIVE
, &vd_trim_list
);
6933 mutex_exit(&vml
[c
]->vdev_trim_lock
);
6937 vdev_initialize_stop_wait(spa
, &vd_initialize_list
);
6938 vdev_trim_stop_wait(spa
, &vd_trim_list
);
6940 list_destroy(&vd_initialize_list
);
6941 list_destroy(&vd_trim_list
);
6943 newspa
->spa_config_source
= SPA_CONFIG_SRC_SPLIT
;
6945 /* create the new pool from the disks of the original pool */
6946 error
= spa_load(newspa
, SPA_LOAD_IMPORT
, SPA_IMPORT_ASSEMBLE
);
6950 /* if that worked, generate a real config for the new pool */
6951 if (newspa
->spa_root_vdev
!= NULL
) {
6952 VERIFY(nvlist_alloc(&newspa
->spa_config_splitting
,
6953 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
6954 VERIFY(nvlist_add_uint64(newspa
->spa_config_splitting
,
6955 ZPOOL_CONFIG_SPLIT_GUID
, spa_guid(spa
)) == 0);
6956 spa_config_set(newspa
, spa_config_generate(newspa
, NULL
, -1ULL,
6961 if (props
!= NULL
) {
6962 spa_configfile_set(newspa
, props
, B_FALSE
);
6963 error
= spa_prop_set(newspa
, props
);
6968 /* flush everything */
6969 txg
= spa_vdev_config_enter(newspa
);
6970 vdev_config_dirty(newspa
->spa_root_vdev
);
6971 (void) spa_vdev_config_exit(newspa
, NULL
, txg
, 0, FTAG
);
6973 if (zio_injection_enabled
)
6974 zio_handle_panic_injection(spa
, FTAG
, 2);
6976 spa_async_resume(newspa
);
6978 /* finally, update the original pool's config */
6979 txg
= spa_vdev_config_enter(spa
);
6980 tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
6981 error
= dmu_tx_assign(tx
, TXG_WAIT
);
6984 for (c
= 0; c
< children
; c
++) {
6985 if (vml
[c
] != NULL
) {
6986 vdev_t
*tvd
= vml
[c
]->vdev_top
;
6989 * Need to be sure the detachable VDEV is not
6990 * on any *other* txg's DTL list to prevent it
6991 * from being accessed after it's freed.
6993 for (int t
= 0; t
< TXG_SIZE
; t
++) {
6994 (void) txg_list_remove_this(
6995 &tvd
->vdev_dtl_list
, vml
[c
], t
);
7000 spa_history_log_internal(spa
, "detach", tx
,
7001 "vdev=%s", vml
[c
]->vdev_path
);
7006 spa
->spa_avz_action
= AVZ_ACTION_REBUILD
;
7007 vdev_config_dirty(spa
->spa_root_vdev
);
7008 spa
->spa_config_splitting
= NULL
;
7012 (void) spa_vdev_exit(spa
, NULL
, txg
, 0);
7014 if (zio_injection_enabled
)
7015 zio_handle_panic_injection(spa
, FTAG
, 3);
7017 /* split is complete; log a history record */
7018 spa_history_log_internal(newspa
, "split", NULL
,
7019 "from pool %s", spa_name(spa
));
7021 kmem_free(vml
, children
* sizeof (vdev_t
*));
7023 /* if we're not going to mount the filesystems in userland, export */
7025 error
= spa_export_common(newname
, POOL_STATE_EXPORTED
, NULL
,
7032 spa_deactivate(newspa
);
7035 txg
= spa_vdev_config_enter(spa
);
7037 /* re-online all offlined disks */
7038 for (c
= 0; c
< children
; c
++) {
7040 vml
[c
]->vdev_offline
= B_FALSE
;
7043 /* restart initializing or trimming disks as necessary */
7044 spa_async_request(spa
, SPA_ASYNC_INITIALIZE_RESTART
);
7045 spa_async_request(spa
, SPA_ASYNC_TRIM_RESTART
);
7046 spa_async_request(spa
, SPA_ASYNC_AUTOTRIM_RESTART
);
7048 vdev_reopen(spa
->spa_root_vdev
);
7050 nvlist_free(spa
->spa_config_splitting
);
7051 spa
->spa_config_splitting
= NULL
;
7052 (void) spa_vdev_exit(spa
, NULL
, txg
, error
);
7054 kmem_free(vml
, children
* sizeof (vdev_t
*));
7059 * Find any device that's done replacing, or a vdev marked 'unspare' that's
7060 * currently spared, so we can detach it.
7063 spa_vdev_resilver_done_hunt(vdev_t
*vd
)
7065 vdev_t
*newvd
, *oldvd
;
7067 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
7068 oldvd
= spa_vdev_resilver_done_hunt(vd
->vdev_child
[c
]);
7074 * Check for a completed replacement. We always consider the first
7075 * vdev in the list to be the oldest vdev, and the last one to be
7076 * the newest (see spa_vdev_attach() for how that works). In
7077 * the case where the newest vdev is faulted, we will not automatically
7078 * remove it after a resilver completes. This is OK as it will require
7079 * user intervention to determine which disk the admin wishes to keep.
7081 if (vd
->vdev_ops
== &vdev_replacing_ops
) {
7082 ASSERT(vd
->vdev_children
> 1);
7084 newvd
= vd
->vdev_child
[vd
->vdev_children
- 1];
7085 oldvd
= vd
->vdev_child
[0];
7087 if (vdev_dtl_empty(newvd
, DTL_MISSING
) &&
7088 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
7089 !vdev_dtl_required(oldvd
))
7094 * Check for a completed resilver with the 'unspare' flag set.
7095 * Also potentially update faulted state.
7097 if (vd
->vdev_ops
== &vdev_spare_ops
) {
7098 vdev_t
*first
= vd
->vdev_child
[0];
7099 vdev_t
*last
= vd
->vdev_child
[vd
->vdev_children
- 1];
7101 if (last
->vdev_unspare
) {
7104 } else if (first
->vdev_unspare
) {
7111 if (oldvd
!= NULL
&&
7112 vdev_dtl_empty(newvd
, DTL_MISSING
) &&
7113 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
7114 !vdev_dtl_required(oldvd
))
7117 vdev_propagate_state(vd
);
7120 * If there are more than two spares attached to a disk,
7121 * and those spares are not required, then we want to
7122 * attempt to free them up now so that they can be used
7123 * by other pools. Once we're back down to a single
7124 * disk+spare, we stop removing them.
7126 if (vd
->vdev_children
> 2) {
7127 newvd
= vd
->vdev_child
[1];
7129 if (newvd
->vdev_isspare
&& last
->vdev_isspare
&&
7130 vdev_dtl_empty(last
, DTL_MISSING
) &&
7131 vdev_dtl_empty(last
, DTL_OUTAGE
) &&
7132 !vdev_dtl_required(newvd
))
7141 spa_vdev_resilver_done(spa_t
*spa
)
7143 vdev_t
*vd
, *pvd
, *ppvd
;
7144 uint64_t guid
, sguid
, pguid
, ppguid
;
7146 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
7148 while ((vd
= spa_vdev_resilver_done_hunt(spa
->spa_root_vdev
)) != NULL
) {
7149 pvd
= vd
->vdev_parent
;
7150 ppvd
= pvd
->vdev_parent
;
7151 guid
= vd
->vdev_guid
;
7152 pguid
= pvd
->vdev_guid
;
7153 ppguid
= ppvd
->vdev_guid
;
7156 * If we have just finished replacing a hot spared device, then
7157 * we need to detach the parent's first child (the original hot
7160 if (ppvd
->vdev_ops
== &vdev_spare_ops
&& pvd
->vdev_id
== 0 &&
7161 ppvd
->vdev_children
== 2) {
7162 ASSERT(pvd
->vdev_ops
== &vdev_replacing_ops
);
7163 sguid
= ppvd
->vdev_child
[1]->vdev_guid
;
7165 ASSERT(vd
->vdev_resilver_txg
== 0 || !vdev_dtl_required(vd
));
7167 spa_config_exit(spa
, SCL_ALL
, FTAG
);
7168 if (spa_vdev_detach(spa
, guid
, pguid
, B_TRUE
) != 0)
7170 if (sguid
&& spa_vdev_detach(spa
, sguid
, ppguid
, B_TRUE
) != 0)
7172 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
7175 spa_config_exit(spa
, SCL_ALL
, FTAG
);
7179 * Update the stored path or FRU for this vdev.
7182 spa_vdev_set_common(spa_t
*spa
, uint64_t guid
, const char *value
,
7186 boolean_t sync
= B_FALSE
;
7188 ASSERT(spa_writeable(spa
));
7190 spa_vdev_state_enter(spa
, SCL_ALL
);
7192 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
7193 return (spa_vdev_state_exit(spa
, NULL
, ENOENT
));
7195 if (!vd
->vdev_ops
->vdev_op_leaf
)
7196 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
7199 if (strcmp(value
, vd
->vdev_path
) != 0) {
7200 spa_strfree(vd
->vdev_path
);
7201 vd
->vdev_path
= spa_strdup(value
);
7205 if (vd
->vdev_fru
== NULL
) {
7206 vd
->vdev_fru
= spa_strdup(value
);
7208 } else if (strcmp(value
, vd
->vdev_fru
) != 0) {
7209 spa_strfree(vd
->vdev_fru
);
7210 vd
->vdev_fru
= spa_strdup(value
);
7215 return (spa_vdev_state_exit(spa
, sync
? vd
: NULL
, 0));
7219 spa_vdev_setpath(spa_t
*spa
, uint64_t guid
, const char *newpath
)
7221 return (spa_vdev_set_common(spa
, guid
, newpath
, B_TRUE
));
7225 spa_vdev_setfru(spa_t
*spa
, uint64_t guid
, const char *newfru
)
7227 return (spa_vdev_set_common(spa
, guid
, newfru
, B_FALSE
));
7231 * ==========================================================================
7233 * ==========================================================================
7236 spa_scrub_pause_resume(spa_t
*spa
, pool_scrub_cmd_t cmd
)
7238 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
7240 if (dsl_scan_resilvering(spa
->spa_dsl_pool
))
7241 return (SET_ERROR(EBUSY
));
7243 return (dsl_scrub_set_pause_resume(spa
->spa_dsl_pool
, cmd
));
7247 spa_scan_stop(spa_t
*spa
)
7249 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
7250 if (dsl_scan_resilvering(spa
->spa_dsl_pool
))
7251 return (SET_ERROR(EBUSY
));
7252 return (dsl_scan_cancel(spa
->spa_dsl_pool
));
7256 spa_scan(spa_t
*spa
, pool_scan_func_t func
)
7258 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
7260 if (func
>= POOL_SCAN_FUNCS
|| func
== POOL_SCAN_NONE
)
7261 return (SET_ERROR(ENOTSUP
));
7264 * If a resilver was requested, but there is no DTL on a
7265 * writeable leaf device, we have nothing to do.
7267 if (func
== POOL_SCAN_RESILVER
&&
7268 !vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
)) {
7269 spa_async_request(spa
, SPA_ASYNC_RESILVER_DONE
);
7273 return (dsl_scan(spa
->spa_dsl_pool
, func
));
7277 * ==========================================================================
7278 * SPA async task processing
7279 * ==========================================================================
7283 spa_async_remove(spa_t
*spa
, vdev_t
*vd
)
7285 if (vd
->vdev_remove_wanted
) {
7286 vd
->vdev_remove_wanted
= B_FALSE
;
7287 vd
->vdev_delayed_close
= B_FALSE
;
7288 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_REMOVED
, VDEV_AUX_NONE
);
7291 * We want to clear the stats, but we don't want to do a full
7292 * vdev_clear() as that will cause us to throw away
7293 * degraded/faulted state as well as attempt to reopen the
7294 * device, all of which is a waste.
7296 vd
->vdev_stat
.vs_read_errors
= 0;
7297 vd
->vdev_stat
.vs_write_errors
= 0;
7298 vd
->vdev_stat
.vs_checksum_errors
= 0;
7300 vdev_state_dirty(vd
->vdev_top
);
7303 for (int c
= 0; c
< vd
->vdev_children
; c
++)
7304 spa_async_remove(spa
, vd
->vdev_child
[c
]);
7308 spa_async_probe(spa_t
*spa
, vdev_t
*vd
)
7310 if (vd
->vdev_probe_wanted
) {
7311 vd
->vdev_probe_wanted
= B_FALSE
;
7312 vdev_reopen(vd
); /* vdev_open() does the actual probe */
7315 for (int c
= 0; c
< vd
->vdev_children
; c
++)
7316 spa_async_probe(spa
, vd
->vdev_child
[c
]);
7320 spa_async_autoexpand(spa_t
*spa
, vdev_t
*vd
)
7322 if (!spa
->spa_autoexpand
)
7325 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
7326 vdev_t
*cvd
= vd
->vdev_child
[c
];
7327 spa_async_autoexpand(spa
, cvd
);
7330 if (!vd
->vdev_ops
->vdev_op_leaf
|| vd
->vdev_physpath
== NULL
)
7333 spa_event_notify(vd
->vdev_spa
, vd
, NULL
, ESC_ZFS_VDEV_AUTOEXPAND
);
7337 spa_async_thread(void *arg
)
7339 spa_t
*spa
= (spa_t
*)arg
;
7340 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
7343 ASSERT(spa
->spa_sync_on
);
7345 mutex_enter(&spa
->spa_async_lock
);
7346 tasks
= spa
->spa_async_tasks
;
7347 spa
->spa_async_tasks
= 0;
7348 mutex_exit(&spa
->spa_async_lock
);
7351 * See if the config needs to be updated.
7353 if (tasks
& SPA_ASYNC_CONFIG_UPDATE
) {
7354 uint64_t old_space
, new_space
;
7356 mutex_enter(&spa_namespace_lock
);
7357 old_space
= metaslab_class_get_space(spa_normal_class(spa
));
7358 old_space
+= metaslab_class_get_space(spa_special_class(spa
));
7359 old_space
+= metaslab_class_get_space(spa_dedup_class(spa
));
7361 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
7363 new_space
= metaslab_class_get_space(spa_normal_class(spa
));
7364 new_space
+= metaslab_class_get_space(spa_special_class(spa
));
7365 new_space
+= metaslab_class_get_space(spa_dedup_class(spa
));
7366 mutex_exit(&spa_namespace_lock
);
7369 * If the pool grew as a result of the config update,
7370 * then log an internal history event.
7372 if (new_space
!= old_space
) {
7373 spa_history_log_internal(spa
, "vdev online", NULL
,
7374 "pool '%s' size: %llu(+%llu)",
7375 spa_name(spa
), new_space
, new_space
- old_space
);
7380 * See if any devices need to be marked REMOVED.
7382 if (tasks
& SPA_ASYNC_REMOVE
) {
7383 spa_vdev_state_enter(spa
, SCL_NONE
);
7384 spa_async_remove(spa
, spa
->spa_root_vdev
);
7385 for (int i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++)
7386 spa_async_remove(spa
, spa
->spa_l2cache
.sav_vdevs
[i
]);
7387 for (int i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
7388 spa_async_remove(spa
, spa
->spa_spares
.sav_vdevs
[i
]);
7389 (void) spa_vdev_state_exit(spa
, NULL
, 0);
7392 if ((tasks
& SPA_ASYNC_AUTOEXPAND
) && !spa_suspended(spa
)) {
7393 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
7394 spa_async_autoexpand(spa
, spa
->spa_root_vdev
);
7395 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
7399 * See if any devices need to be probed.
7401 if (tasks
& SPA_ASYNC_PROBE
) {
7402 spa_vdev_state_enter(spa
, SCL_NONE
);
7403 spa_async_probe(spa
, spa
->spa_root_vdev
);
7404 (void) spa_vdev_state_exit(spa
, NULL
, 0);
7408 * If any devices are done replacing, detach them.
7410 if (tasks
& SPA_ASYNC_RESILVER_DONE
)
7411 spa_vdev_resilver_done(spa
);
7414 * Kick off a resilver.
7416 if (tasks
& SPA_ASYNC_RESILVER
&&
7417 (!dsl_scan_resilvering(dp
) ||
7418 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_RESILVER_DEFER
)))
7419 dsl_resilver_restart(dp
, 0);
7421 if (tasks
& SPA_ASYNC_INITIALIZE_RESTART
) {
7422 mutex_enter(&spa_namespace_lock
);
7423 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
7424 vdev_initialize_restart(spa
->spa_root_vdev
);
7425 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
7426 mutex_exit(&spa_namespace_lock
);
7429 if (tasks
& SPA_ASYNC_TRIM_RESTART
) {
7430 mutex_enter(&spa_namespace_lock
);
7431 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
7432 vdev_trim_restart(spa
->spa_root_vdev
);
7433 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
7434 mutex_exit(&spa_namespace_lock
);
7437 if (tasks
& SPA_ASYNC_AUTOTRIM_RESTART
) {
7438 mutex_enter(&spa_namespace_lock
);
7439 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
7440 vdev_autotrim_restart(spa
);
7441 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
7442 mutex_exit(&spa_namespace_lock
);
7446 * Let the world know that we're done.
7448 mutex_enter(&spa
->spa_async_lock
);
7449 spa
->spa_async_thread
= NULL
;
7450 cv_broadcast(&spa
->spa_async_cv
);
7451 mutex_exit(&spa
->spa_async_lock
);
7456 spa_async_suspend(spa_t
*spa
)
7458 mutex_enter(&spa
->spa_async_lock
);
7459 spa
->spa_async_suspended
++;
7460 while (spa
->spa_async_thread
!= NULL
)
7461 cv_wait(&spa
->spa_async_cv
, &spa
->spa_async_lock
);
7462 mutex_exit(&spa
->spa_async_lock
);
7464 spa_vdev_remove_suspend(spa
);
7466 zthr_t
*condense_thread
= spa
->spa_condense_zthr
;
7467 if (condense_thread
!= NULL
)
7468 zthr_cancel(condense_thread
);
7470 zthr_t
*discard_thread
= spa
->spa_checkpoint_discard_zthr
;
7471 if (discard_thread
!= NULL
)
7472 zthr_cancel(discard_thread
);
7476 spa_async_resume(spa_t
*spa
)
7478 mutex_enter(&spa
->spa_async_lock
);
7479 ASSERT(spa
->spa_async_suspended
!= 0);
7480 spa
->spa_async_suspended
--;
7481 mutex_exit(&spa
->spa_async_lock
);
7482 spa_restart_removal(spa
);
7484 zthr_t
*condense_thread
= spa
->spa_condense_zthr
;
7485 if (condense_thread
!= NULL
)
7486 zthr_resume(condense_thread
);
7488 zthr_t
*discard_thread
= spa
->spa_checkpoint_discard_zthr
;
7489 if (discard_thread
!= NULL
)
7490 zthr_resume(discard_thread
);
7494 spa_async_tasks_pending(spa_t
*spa
)
7496 uint_t non_config_tasks
;
7498 boolean_t config_task_suspended
;
7500 non_config_tasks
= spa
->spa_async_tasks
& ~SPA_ASYNC_CONFIG_UPDATE
;
7501 config_task
= spa
->spa_async_tasks
& SPA_ASYNC_CONFIG_UPDATE
;
7502 if (spa
->spa_ccw_fail_time
== 0) {
7503 config_task_suspended
= B_FALSE
;
7505 config_task_suspended
=
7506 (gethrtime() - spa
->spa_ccw_fail_time
) <
7507 ((hrtime_t
)zfs_ccw_retry_interval
* NANOSEC
);
7510 return (non_config_tasks
|| (config_task
&& !config_task_suspended
));
7514 spa_async_dispatch(spa_t
*spa
)
7516 mutex_enter(&spa
->spa_async_lock
);
7517 if (spa_async_tasks_pending(spa
) &&
7518 !spa
->spa_async_suspended
&&
7519 spa
->spa_async_thread
== NULL
&&
7521 spa
->spa_async_thread
= thread_create(NULL
, 0,
7522 spa_async_thread
, spa
, 0, &p0
, TS_RUN
, maxclsyspri
);
7523 mutex_exit(&spa
->spa_async_lock
);
7527 spa_async_request(spa_t
*spa
, int task
)
7529 zfs_dbgmsg("spa=%s async request task=%u", spa
->spa_name
, task
);
7530 mutex_enter(&spa
->spa_async_lock
);
7531 spa
->spa_async_tasks
|= task
;
7532 mutex_exit(&spa
->spa_async_lock
);
7536 * ==========================================================================
7537 * SPA syncing routines
7538 * ==========================================================================
7542 bpobj_enqueue_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
7545 bpobj_enqueue(bpo
, bp
, tx
);
7550 spa_free_sync_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
7554 zio_nowait(zio_free_sync(zio
, zio
->io_spa
, dmu_tx_get_txg(tx
), bp
,
7560 * Note: this simple function is not inlined to make it easier to dtrace the
7561 * amount of time spent syncing frees.
7564 spa_sync_frees(spa_t
*spa
, bplist_t
*bpl
, dmu_tx_t
*tx
)
7566 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
7567 bplist_iterate(bpl
, spa_free_sync_cb
, zio
, tx
);
7568 VERIFY(zio_wait(zio
) == 0);
7572 * Note: this simple function is not inlined to make it easier to dtrace the
7573 * amount of time spent syncing deferred frees.
7576 spa_sync_deferred_frees(spa_t
*spa
, dmu_tx_t
*tx
)
7578 if (spa_sync_pass(spa
) != 1)
7581 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
7582 VERIFY3U(bpobj_iterate(&spa
->spa_deferred_bpobj
,
7583 spa_free_sync_cb
, zio
, tx
), ==, 0);
7584 VERIFY0(zio_wait(zio
));
7588 spa_sync_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
*nv
, dmu_tx_t
*tx
)
7590 char *packed
= NULL
;
7595 VERIFY(nvlist_size(nv
, &nvsize
, NV_ENCODE_XDR
) == 0);
7598 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
7599 * information. This avoids the dmu_buf_will_dirty() path and
7600 * saves us a pre-read to get data we don't actually care about.
7602 bufsize
= P2ROUNDUP((uint64_t)nvsize
, SPA_CONFIG_BLOCKSIZE
);
7603 packed
= vmem_alloc(bufsize
, KM_SLEEP
);
7605 VERIFY(nvlist_pack(nv
, &packed
, &nvsize
, NV_ENCODE_XDR
,
7607 bzero(packed
+ nvsize
, bufsize
- nvsize
);
7609 dmu_write(spa
->spa_meta_objset
, obj
, 0, bufsize
, packed
, tx
);
7611 vmem_free(packed
, bufsize
);
7613 VERIFY(0 == dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
));
7614 dmu_buf_will_dirty(db
, tx
);
7615 *(uint64_t *)db
->db_data
= nvsize
;
7616 dmu_buf_rele(db
, FTAG
);
7620 spa_sync_aux_dev(spa_t
*spa
, spa_aux_vdev_t
*sav
, dmu_tx_t
*tx
,
7621 const char *config
, const char *entry
)
7631 * Update the MOS nvlist describing the list of available devices.
7632 * spa_validate_aux() will have already made sure this nvlist is
7633 * valid and the vdevs are labeled appropriately.
7635 if (sav
->sav_object
== 0) {
7636 sav
->sav_object
= dmu_object_alloc(spa
->spa_meta_objset
,
7637 DMU_OT_PACKED_NVLIST
, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE
,
7638 sizeof (uint64_t), tx
);
7639 VERIFY(zap_update(spa
->spa_meta_objset
,
7640 DMU_POOL_DIRECTORY_OBJECT
, entry
, sizeof (uint64_t), 1,
7641 &sav
->sav_object
, tx
) == 0);
7644 VERIFY(nvlist_alloc(&nvroot
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
7645 if (sav
->sav_count
== 0) {
7646 VERIFY(nvlist_add_nvlist_array(nvroot
, config
, NULL
, 0) == 0);
7648 list
= kmem_alloc(sav
->sav_count
*sizeof (void *), KM_SLEEP
);
7649 for (i
= 0; i
< sav
->sav_count
; i
++)
7650 list
[i
] = vdev_config_generate(spa
, sav
->sav_vdevs
[i
],
7651 B_FALSE
, VDEV_CONFIG_L2CACHE
);
7652 VERIFY(nvlist_add_nvlist_array(nvroot
, config
, list
,
7653 sav
->sav_count
) == 0);
7654 for (i
= 0; i
< sav
->sav_count
; i
++)
7655 nvlist_free(list
[i
]);
7656 kmem_free(list
, sav
->sav_count
* sizeof (void *));
7659 spa_sync_nvlist(spa
, sav
->sav_object
, nvroot
, tx
);
7660 nvlist_free(nvroot
);
7662 sav
->sav_sync
= B_FALSE
;
7666 * Rebuild spa's all-vdev ZAP from the vdev ZAPs indicated in each vdev_t.
7667 * The all-vdev ZAP must be empty.
7670 spa_avz_build(vdev_t
*vd
, uint64_t avz
, dmu_tx_t
*tx
)
7672 spa_t
*spa
= vd
->vdev_spa
;
7674 if (vd
->vdev_top_zap
!= 0) {
7675 VERIFY0(zap_add_int(spa
->spa_meta_objset
, avz
,
7676 vd
->vdev_top_zap
, tx
));
7678 if (vd
->vdev_leaf_zap
!= 0) {
7679 VERIFY0(zap_add_int(spa
->spa_meta_objset
, avz
,
7680 vd
->vdev_leaf_zap
, tx
));
7682 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
7683 spa_avz_build(vd
->vdev_child
[i
], avz
, tx
);
7688 spa_sync_config_object(spa_t
*spa
, dmu_tx_t
*tx
)
7693 * If the pool is being imported from a pre-per-vdev-ZAP version of ZFS,
7694 * its config may not be dirty but we still need to build per-vdev ZAPs.
7695 * Similarly, if the pool is being assembled (e.g. after a split), we
7696 * need to rebuild the AVZ although the config may not be dirty.
7698 if (list_is_empty(&spa
->spa_config_dirty_list
) &&
7699 spa
->spa_avz_action
== AVZ_ACTION_NONE
)
7702 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
7704 ASSERT(spa
->spa_avz_action
== AVZ_ACTION_NONE
||
7705 spa
->spa_avz_action
== AVZ_ACTION_INITIALIZE
||
7706 spa
->spa_all_vdev_zaps
!= 0);
7708 if (spa
->spa_avz_action
== AVZ_ACTION_REBUILD
) {
7709 /* Make and build the new AVZ */
7710 uint64_t new_avz
= zap_create(spa
->spa_meta_objset
,
7711 DMU_OTN_ZAP_METADATA
, DMU_OT_NONE
, 0, tx
);
7712 spa_avz_build(spa
->spa_root_vdev
, new_avz
, tx
);
7714 /* Diff old AVZ with new one */
7718 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
7719 spa
->spa_all_vdev_zaps
);
7720 zap_cursor_retrieve(&zc
, &za
) == 0;
7721 zap_cursor_advance(&zc
)) {
7722 uint64_t vdzap
= za
.za_first_integer
;
7723 if (zap_lookup_int(spa
->spa_meta_objset
, new_avz
,
7726 * ZAP is listed in old AVZ but not in new one;
7729 VERIFY0(zap_destroy(spa
->spa_meta_objset
, vdzap
,
7734 zap_cursor_fini(&zc
);
7736 /* Destroy the old AVZ */
7737 VERIFY0(zap_destroy(spa
->spa_meta_objset
,
7738 spa
->spa_all_vdev_zaps
, tx
));
7740 /* Replace the old AVZ in the dir obj with the new one */
7741 VERIFY0(zap_update(spa
->spa_meta_objset
,
7742 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_VDEV_ZAP_MAP
,
7743 sizeof (new_avz
), 1, &new_avz
, tx
));
7745 spa
->spa_all_vdev_zaps
= new_avz
;
7746 } else if (spa
->spa_avz_action
== AVZ_ACTION_DESTROY
) {
7750 /* Walk through the AVZ and destroy all listed ZAPs */
7751 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
7752 spa
->spa_all_vdev_zaps
);
7753 zap_cursor_retrieve(&zc
, &za
) == 0;
7754 zap_cursor_advance(&zc
)) {
7755 uint64_t zap
= za
.za_first_integer
;
7756 VERIFY0(zap_destroy(spa
->spa_meta_objset
, zap
, tx
));
7759 zap_cursor_fini(&zc
);
7761 /* Destroy and unlink the AVZ itself */
7762 VERIFY0(zap_destroy(spa
->spa_meta_objset
,
7763 spa
->spa_all_vdev_zaps
, tx
));
7764 VERIFY0(zap_remove(spa
->spa_meta_objset
,
7765 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_VDEV_ZAP_MAP
, tx
));
7766 spa
->spa_all_vdev_zaps
= 0;
7769 if (spa
->spa_all_vdev_zaps
== 0) {
7770 spa
->spa_all_vdev_zaps
= zap_create_link(spa
->spa_meta_objset
,
7771 DMU_OTN_ZAP_METADATA
, DMU_POOL_DIRECTORY_OBJECT
,
7772 DMU_POOL_VDEV_ZAP_MAP
, tx
);
7774 spa
->spa_avz_action
= AVZ_ACTION_NONE
;
7776 /* Create ZAPs for vdevs that don't have them. */
7777 vdev_construct_zaps(spa
->spa_root_vdev
, tx
);
7779 config
= spa_config_generate(spa
, spa
->spa_root_vdev
,
7780 dmu_tx_get_txg(tx
), B_FALSE
);
7783 * If we're upgrading the spa version then make sure that
7784 * the config object gets updated with the correct version.
7786 if (spa
->spa_ubsync
.ub_version
< spa
->spa_uberblock
.ub_version
)
7787 fnvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
7788 spa
->spa_uberblock
.ub_version
);
7790 spa_config_exit(spa
, SCL_STATE
, FTAG
);
7792 nvlist_free(spa
->spa_config_syncing
);
7793 spa
->spa_config_syncing
= config
;
7795 spa_sync_nvlist(spa
, spa
->spa_config_object
, config
, tx
);
7799 spa_sync_version(void *arg
, dmu_tx_t
*tx
)
7801 uint64_t *versionp
= arg
;
7802 uint64_t version
= *versionp
;
7803 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
7806 * Setting the version is special cased when first creating the pool.
7808 ASSERT(tx
->tx_txg
!= TXG_INITIAL
);
7810 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
7811 ASSERT(version
>= spa_version(spa
));
7813 spa
->spa_uberblock
.ub_version
= version
;
7814 vdev_config_dirty(spa
->spa_root_vdev
);
7815 spa_history_log_internal(spa
, "set", tx
, "version=%lld", version
);
7819 * Set zpool properties.
7822 spa_sync_props(void *arg
, dmu_tx_t
*tx
)
7824 nvlist_t
*nvp
= arg
;
7825 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
7826 objset_t
*mos
= spa
->spa_meta_objset
;
7827 nvpair_t
*elem
= NULL
;
7829 mutex_enter(&spa
->spa_props_lock
);
7831 while ((elem
= nvlist_next_nvpair(nvp
, elem
))) {
7833 char *strval
, *fname
;
7835 const char *propname
;
7836 zprop_type_t proptype
;
7839 switch (prop
= zpool_name_to_prop(nvpair_name(elem
))) {
7840 case ZPOOL_PROP_INVAL
:
7842 * We checked this earlier in spa_prop_validate().
7844 ASSERT(zpool_prop_feature(nvpair_name(elem
)));
7846 fname
= strchr(nvpair_name(elem
), '@') + 1;
7847 VERIFY0(zfeature_lookup_name(fname
, &fid
));
7849 spa_feature_enable(spa
, fid
, tx
);
7850 spa_history_log_internal(spa
, "set", tx
,
7851 "%s=enabled", nvpair_name(elem
));
7854 case ZPOOL_PROP_VERSION
:
7855 intval
= fnvpair_value_uint64(elem
);
7857 * The version is synced separately before other
7858 * properties and should be correct by now.
7860 ASSERT3U(spa_version(spa
), >=, intval
);
7863 case ZPOOL_PROP_ALTROOT
:
7865 * 'altroot' is a non-persistent property. It should
7866 * have been set temporarily at creation or import time.
7868 ASSERT(spa
->spa_root
!= NULL
);
7871 case ZPOOL_PROP_READONLY
:
7872 case ZPOOL_PROP_CACHEFILE
:
7874 * 'readonly' and 'cachefile' are also non-persisitent
7878 case ZPOOL_PROP_COMMENT
:
7879 strval
= fnvpair_value_string(elem
);
7880 if (spa
->spa_comment
!= NULL
)
7881 spa_strfree(spa
->spa_comment
);
7882 spa
->spa_comment
= spa_strdup(strval
);
7884 * We need to dirty the configuration on all the vdevs
7885 * so that their labels get updated. It's unnecessary
7886 * to do this for pool creation since the vdev's
7887 * configuration has already been dirtied.
7889 if (tx
->tx_txg
!= TXG_INITIAL
)
7890 vdev_config_dirty(spa
->spa_root_vdev
);
7891 spa_history_log_internal(spa
, "set", tx
,
7892 "%s=%s", nvpair_name(elem
), strval
);
7896 * Set pool property values in the poolprops mos object.
7898 if (spa
->spa_pool_props_object
== 0) {
7899 spa
->spa_pool_props_object
=
7900 zap_create_link(mos
, DMU_OT_POOL_PROPS
,
7901 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_PROPS
,
7905 /* normalize the property name */
7906 propname
= zpool_prop_to_name(prop
);
7907 proptype
= zpool_prop_get_type(prop
);
7909 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
7910 ASSERT(proptype
== PROP_TYPE_STRING
);
7911 strval
= fnvpair_value_string(elem
);
7912 VERIFY0(zap_update(mos
,
7913 spa
->spa_pool_props_object
, propname
,
7914 1, strlen(strval
) + 1, strval
, tx
));
7915 spa_history_log_internal(spa
, "set", tx
,
7916 "%s=%s", nvpair_name(elem
), strval
);
7917 } else if (nvpair_type(elem
) == DATA_TYPE_UINT64
) {
7918 intval
= fnvpair_value_uint64(elem
);
7920 if (proptype
== PROP_TYPE_INDEX
) {
7922 VERIFY0(zpool_prop_index_to_string(
7923 prop
, intval
, &unused
));
7925 VERIFY0(zap_update(mos
,
7926 spa
->spa_pool_props_object
, propname
,
7927 8, 1, &intval
, tx
));
7928 spa_history_log_internal(spa
, "set", tx
,
7929 "%s=%lld", nvpair_name(elem
), intval
);
7931 ASSERT(0); /* not allowed */
7935 case ZPOOL_PROP_DELEGATION
:
7936 spa
->spa_delegation
= intval
;
7938 case ZPOOL_PROP_BOOTFS
:
7939 spa
->spa_bootfs
= intval
;
7941 case ZPOOL_PROP_FAILUREMODE
:
7942 spa
->spa_failmode
= intval
;
7944 case ZPOOL_PROP_AUTOTRIM
:
7945 spa
->spa_autotrim
= intval
;
7946 spa_async_request(spa
,
7947 SPA_ASYNC_AUTOTRIM_RESTART
);
7949 case ZPOOL_PROP_AUTOEXPAND
:
7950 spa
->spa_autoexpand
= intval
;
7951 if (tx
->tx_txg
!= TXG_INITIAL
)
7952 spa_async_request(spa
,
7953 SPA_ASYNC_AUTOEXPAND
);
7955 case ZPOOL_PROP_MULTIHOST
:
7956 spa
->spa_multihost
= intval
;
7958 case ZPOOL_PROP_DEDUPDITTO
:
7959 spa
->spa_dedup_ditto
= intval
;
7968 mutex_exit(&spa
->spa_props_lock
);
7972 * Perform one-time upgrade on-disk changes. spa_version() does not
7973 * reflect the new version this txg, so there must be no changes this
7974 * txg to anything that the upgrade code depends on after it executes.
7975 * Therefore this must be called after dsl_pool_sync() does the sync
7979 spa_sync_upgrades(spa_t
*spa
, dmu_tx_t
*tx
)
7981 if (spa_sync_pass(spa
) != 1)
7984 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
7985 rrw_enter(&dp
->dp_config_rwlock
, RW_WRITER
, FTAG
);
7987 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_ORIGIN
&&
7988 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_ORIGIN
) {
7989 dsl_pool_create_origin(dp
, tx
);
7991 /* Keeping the origin open increases spa_minref */
7992 spa
->spa_minref
+= 3;
7995 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_NEXT_CLONES
&&
7996 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_NEXT_CLONES
) {
7997 dsl_pool_upgrade_clones(dp
, tx
);
8000 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_DIR_CLONES
&&
8001 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_DIR_CLONES
) {
8002 dsl_pool_upgrade_dir_clones(dp
, tx
);
8004 /* Keeping the freedir open increases spa_minref */
8005 spa
->spa_minref
+= 3;
8008 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_FEATURES
&&
8009 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
8010 spa_feature_create_zap_objects(spa
, tx
);
8014 * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable
8015 * when possibility to use lz4 compression for metadata was added
8016 * Old pools that have this feature enabled must be upgraded to have
8017 * this feature active
8019 if (spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
8020 boolean_t lz4_en
= spa_feature_is_enabled(spa
,
8021 SPA_FEATURE_LZ4_COMPRESS
);
8022 boolean_t lz4_ac
= spa_feature_is_active(spa
,
8023 SPA_FEATURE_LZ4_COMPRESS
);
8025 if (lz4_en
&& !lz4_ac
)
8026 spa_feature_incr(spa
, SPA_FEATURE_LZ4_COMPRESS
, tx
);
8030 * If we haven't written the salt, do so now. Note that the
8031 * feature may not be activated yet, but that's fine since
8032 * the presence of this ZAP entry is backwards compatible.
8034 if (zap_contains(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
8035 DMU_POOL_CHECKSUM_SALT
) == ENOENT
) {
8036 VERIFY0(zap_add(spa
->spa_meta_objset
,
8037 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CHECKSUM_SALT
, 1,
8038 sizeof (spa
->spa_cksum_salt
.zcs_bytes
),
8039 spa
->spa_cksum_salt
.zcs_bytes
, tx
));
8042 rrw_exit(&dp
->dp_config_rwlock
, FTAG
);
8046 vdev_indirect_state_sync_verify(vdev_t
*vd
)
8048 ASSERTV(vdev_indirect_mapping_t
*vim
= vd
->vdev_indirect_mapping
);
8049 ASSERTV(vdev_indirect_births_t
*vib
= vd
->vdev_indirect_births
);
8051 if (vd
->vdev_ops
== &vdev_indirect_ops
) {
8052 ASSERT(vim
!= NULL
);
8053 ASSERT(vib
!= NULL
);
8056 uint64_t obsolete_sm_object
= 0;
8057 ASSERT0(vdev_obsolete_sm_object(vd
, &obsolete_sm_object
));
8058 if (obsolete_sm_object
!= 0) {
8059 ASSERT(vd
->vdev_obsolete_sm
!= NULL
);
8060 ASSERT(vd
->vdev_removing
||
8061 vd
->vdev_ops
== &vdev_indirect_ops
);
8062 ASSERT(vdev_indirect_mapping_num_entries(vim
) > 0);
8063 ASSERT(vdev_indirect_mapping_bytes_mapped(vim
) > 0);
8064 ASSERT3U(obsolete_sm_object
, ==,
8065 space_map_object(vd
->vdev_obsolete_sm
));
8066 ASSERT3U(vdev_indirect_mapping_bytes_mapped(vim
), >=,
8067 space_map_allocated(vd
->vdev_obsolete_sm
));
8069 ASSERT(vd
->vdev_obsolete_segments
!= NULL
);
8072 * Since frees / remaps to an indirect vdev can only
8073 * happen in syncing context, the obsolete segments
8074 * tree must be empty when we start syncing.
8076 ASSERT0(range_tree_space(vd
->vdev_obsolete_segments
));
8080 * Set the top-level vdev's max queue depth. Evaluate each top-level's
8081 * async write queue depth in case it changed. The max queue depth will
8082 * not change in the middle of syncing out this txg.
8085 spa_sync_adjust_vdev_max_queue_depth(spa_t
*spa
)
8087 ASSERT(spa_writeable(spa
));
8089 vdev_t
*rvd
= spa
->spa_root_vdev
;
8090 uint32_t max_queue_depth
= zfs_vdev_async_write_max_active
*
8091 zfs_vdev_queue_depth_pct
/ 100;
8092 metaslab_class_t
*normal
= spa_normal_class(spa
);
8093 metaslab_class_t
*special
= spa_special_class(spa
);
8094 metaslab_class_t
*dedup
= spa_dedup_class(spa
);
8096 uint64_t slots_per_allocator
= 0;
8097 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
8098 vdev_t
*tvd
= rvd
->vdev_child
[c
];
8100 metaslab_group_t
*mg
= tvd
->vdev_mg
;
8101 if (mg
== NULL
|| !metaslab_group_initialized(mg
))
8104 metaslab_class_t
*mc
= mg
->mg_class
;
8105 if (mc
!= normal
&& mc
!= special
&& mc
!= dedup
)
8109 * It is safe to do a lock-free check here because only async
8110 * allocations look at mg_max_alloc_queue_depth, and async
8111 * allocations all happen from spa_sync().
8113 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++)
8114 ASSERT0(zfs_refcount_count(
8115 &(mg
->mg_alloc_queue_depth
[i
])));
8116 mg
->mg_max_alloc_queue_depth
= max_queue_depth
;
8118 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
8119 mg
->mg_cur_max_alloc_queue_depth
[i
] =
8120 zfs_vdev_def_queue_depth
;
8122 slots_per_allocator
+= zfs_vdev_def_queue_depth
;
8125 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
8126 ASSERT0(zfs_refcount_count(&normal
->mc_alloc_slots
[i
]));
8127 ASSERT0(zfs_refcount_count(&special
->mc_alloc_slots
[i
]));
8128 ASSERT0(zfs_refcount_count(&dedup
->mc_alloc_slots
[i
]));
8129 normal
->mc_alloc_max_slots
[i
] = slots_per_allocator
;
8130 special
->mc_alloc_max_slots
[i
] = slots_per_allocator
;
8131 dedup
->mc_alloc_max_slots
[i
] = slots_per_allocator
;
8133 normal
->mc_alloc_throttle_enabled
= zio_dva_throttle_enabled
;
8134 special
->mc_alloc_throttle_enabled
= zio_dva_throttle_enabled
;
8135 dedup
->mc_alloc_throttle_enabled
= zio_dva_throttle_enabled
;
8139 spa_sync_condense_indirect(spa_t
*spa
, dmu_tx_t
*tx
)
8141 ASSERT(spa_writeable(spa
));
8143 vdev_t
*rvd
= spa
->spa_root_vdev
;
8144 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
8145 vdev_t
*vd
= rvd
->vdev_child
[c
];
8146 vdev_indirect_state_sync_verify(vd
);
8148 if (vdev_indirect_should_condense(vd
)) {
8149 spa_condense_indirect_start_sync(vd
, tx
);
8156 spa_sync_iterate_to_convergence(spa_t
*spa
, dmu_tx_t
*tx
)
8158 objset_t
*mos
= spa
->spa_meta_objset
;
8159 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
8160 uint64_t txg
= tx
->tx_txg
;
8161 bplist_t
*free_bpl
= &spa
->spa_free_bplist
[txg
& TXG_MASK
];
8164 int pass
= ++spa
->spa_sync_pass
;
8166 spa_sync_config_object(spa
, tx
);
8167 spa_sync_aux_dev(spa
, &spa
->spa_spares
, tx
,
8168 ZPOOL_CONFIG_SPARES
, DMU_POOL_SPARES
);
8169 spa_sync_aux_dev(spa
, &spa
->spa_l2cache
, tx
,
8170 ZPOOL_CONFIG_L2CACHE
, DMU_POOL_L2CACHE
);
8171 spa_errlog_sync(spa
, txg
);
8172 dsl_pool_sync(dp
, txg
);
8174 if (pass
< zfs_sync_pass_deferred_free
) {
8175 spa_sync_frees(spa
, free_bpl
, tx
);
8178 * We can not defer frees in pass 1, because
8179 * we sync the deferred frees later in pass 1.
8181 ASSERT3U(pass
, >, 1);
8182 bplist_iterate(free_bpl
, bpobj_enqueue_cb
,
8183 &spa
->spa_deferred_bpobj
, tx
);
8187 dsl_scan_sync(dp
, tx
);
8189 spa_sync_upgrades(spa
, tx
);
8192 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, txg
))
8197 * Note: We need to check if the MOS is dirty because we could
8198 * have marked the MOS dirty without updating the uberblock
8199 * (e.g. if we have sync tasks but no dirty user data). We need
8200 * to check the uberblock's rootbp because it is updated if we
8201 * have synced out dirty data (though in this case the MOS will
8202 * most likely also be dirty due to second order effects, we
8203 * don't want to rely on that here).
8206 spa
->spa_uberblock
.ub_rootbp
.blk_birth
< txg
&&
8207 !dmu_objset_is_dirty(mos
, txg
)) {
8209 * Nothing changed on the first pass, therefore this
8210 * TXG is a no-op. Avoid syncing deferred frees, so
8211 * that we can keep this TXG as a no-op.
8213 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
, txg
));
8214 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
8215 ASSERT(txg_list_empty(&dp
->dp_sync_tasks
, txg
));
8216 ASSERT(txg_list_empty(&dp
->dp_early_sync_tasks
, txg
));
8220 spa_sync_deferred_frees(spa
, tx
);
8221 } while (dmu_objset_is_dirty(mos
, txg
));
8225 * Rewrite the vdev configuration (which includes the uberblock) to
8226 * commit the transaction group.
8228 * If there are no dirty vdevs, we sync the uberblock to a few random
8229 * top-level vdevs that are known to be visible in the config cache
8230 * (see spa_vdev_add() for a complete description). If there *are* dirty
8231 * vdevs, sync the uberblock to all vdevs.
8234 spa_sync_rewrite_vdev_config(spa_t
*spa
, dmu_tx_t
*tx
)
8236 vdev_t
*rvd
= spa
->spa_root_vdev
;
8237 uint64_t txg
= tx
->tx_txg
;
8243 * We hold SCL_STATE to prevent vdev open/close/etc.
8244 * while we're attempting to write the vdev labels.
8246 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
8248 if (list_is_empty(&spa
->spa_config_dirty_list
)) {
8249 vdev_t
*svd
[SPA_SYNC_MIN_VDEVS
] = { NULL
};
8251 int children
= rvd
->vdev_children
;
8252 int c0
= spa_get_random(children
);
8254 for (int c
= 0; c
< children
; c
++) {
8256 rvd
->vdev_child
[(c0
+ c
) % children
];
8258 /* Stop when revisiting the first vdev */
8259 if (c
> 0 && svd
[0] == vd
)
8262 if (vd
->vdev_ms_array
== 0 ||
8264 !vdev_is_concrete(vd
))
8267 svd
[svdcount
++] = vd
;
8268 if (svdcount
== SPA_SYNC_MIN_VDEVS
)
8271 error
= vdev_config_sync(svd
, svdcount
, txg
);
8273 error
= vdev_config_sync(rvd
->vdev_child
,
8274 rvd
->vdev_children
, txg
);
8278 spa
->spa_last_synced_guid
= rvd
->vdev_guid
;
8280 spa_config_exit(spa
, SCL_STATE
, FTAG
);
8284 zio_suspend(spa
, NULL
, ZIO_SUSPEND_IOERR
);
8285 zio_resume_wait(spa
);
8290 * Sync the specified transaction group. New blocks may be dirtied as
8291 * part of the process, so we iterate until it converges.
8294 spa_sync(spa_t
*spa
, uint64_t txg
)
8298 VERIFY(spa_writeable(spa
));
8301 * Wait for i/os issued in open context that need to complete
8302 * before this txg syncs.
8304 (void) zio_wait(spa
->spa_txg_zio
[txg
& TXG_MASK
]);
8305 spa
->spa_txg_zio
[txg
& TXG_MASK
] = zio_root(spa
, NULL
, NULL
,
8309 * Lock out configuration changes.
8311 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
8313 spa
->spa_syncing_txg
= txg
;
8314 spa
->spa_sync_pass
= 0;
8316 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
8317 mutex_enter(&spa
->spa_alloc_locks
[i
]);
8318 VERIFY0(avl_numnodes(&spa
->spa_alloc_trees
[i
]));
8319 mutex_exit(&spa
->spa_alloc_locks
[i
]);
8323 * If there are any pending vdev state changes, convert them
8324 * into config changes that go out with this transaction group.
8326 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
8327 while (list_head(&spa
->spa_state_dirty_list
) != NULL
) {
8329 * We need the write lock here because, for aux vdevs,
8330 * calling vdev_config_dirty() modifies sav_config.
8331 * This is ugly and will become unnecessary when we
8332 * eliminate the aux vdev wart by integrating all vdevs
8333 * into the root vdev tree.
8335 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
8336 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_WRITER
);
8337 while ((vd
= list_head(&spa
->spa_state_dirty_list
)) != NULL
) {
8338 vdev_state_clean(vd
);
8339 vdev_config_dirty(vd
);
8341 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
8342 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
8344 spa_config_exit(spa
, SCL_STATE
, FTAG
);
8346 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
8347 dmu_tx_t
*tx
= dmu_tx_create_assigned(dp
, txg
);
8349 spa
->spa_sync_starttime
= gethrtime();
8350 taskq_cancel_id(system_delay_taskq
, spa
->spa_deadman_tqid
);
8351 spa
->spa_deadman_tqid
= taskq_dispatch_delay(system_delay_taskq
,
8352 spa_deadman
, spa
, TQ_SLEEP
, ddi_get_lbolt() +
8353 NSEC_TO_TICK(spa
->spa_deadman_synctime
));
8356 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
8357 * set spa_deflate if we have no raid-z vdevs.
8359 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_RAIDZ_DEFLATE
&&
8360 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
8361 vdev_t
*rvd
= spa
->spa_root_vdev
;
8364 for (i
= 0; i
< rvd
->vdev_children
; i
++) {
8365 vd
= rvd
->vdev_child
[i
];
8366 if (vd
->vdev_deflate_ratio
!= SPA_MINBLOCKSIZE
)
8369 if (i
== rvd
->vdev_children
) {
8370 spa
->spa_deflate
= TRUE
;
8371 VERIFY0(zap_add(spa
->spa_meta_objset
,
8372 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
8373 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
));
8377 spa_sync_adjust_vdev_max_queue_depth(spa
);
8379 spa_sync_condense_indirect(spa
, tx
);
8381 spa_sync_iterate_to_convergence(spa
, tx
);
8384 if (!list_is_empty(&spa
->spa_config_dirty_list
)) {
8386 * Make sure that the number of ZAPs for all the vdevs matches
8387 * the number of ZAPs in the per-vdev ZAP list. This only gets
8388 * called if the config is dirty; otherwise there may be
8389 * outstanding AVZ operations that weren't completed in
8390 * spa_sync_config_object.
8392 uint64_t all_vdev_zap_entry_count
;
8393 ASSERT0(zap_count(spa
->spa_meta_objset
,
8394 spa
->spa_all_vdev_zaps
, &all_vdev_zap_entry_count
));
8395 ASSERT3U(vdev_count_verify_zaps(spa
->spa_root_vdev
), ==,
8396 all_vdev_zap_entry_count
);
8400 if (spa
->spa_vdev_removal
!= NULL
) {
8401 ASSERT0(spa
->spa_vdev_removal
->svr_bytes_done
[txg
& TXG_MASK
]);
8404 spa_sync_rewrite_vdev_config(spa
, tx
);
8407 taskq_cancel_id(system_delay_taskq
, spa
->spa_deadman_tqid
);
8408 spa
->spa_deadman_tqid
= 0;
8411 * Clear the dirty config list.
8413 while ((vd
= list_head(&spa
->spa_config_dirty_list
)) != NULL
)
8414 vdev_config_clean(vd
);
8417 * Now that the new config has synced transactionally,
8418 * let it become visible to the config cache.
8420 if (spa
->spa_config_syncing
!= NULL
) {
8421 spa_config_set(spa
, spa
->spa_config_syncing
);
8422 spa
->spa_config_txg
= txg
;
8423 spa
->spa_config_syncing
= NULL
;
8426 dsl_pool_sync_done(dp
, txg
);
8428 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
8429 mutex_enter(&spa
->spa_alloc_locks
[i
]);
8430 VERIFY0(avl_numnodes(&spa
->spa_alloc_trees
[i
]));
8431 mutex_exit(&spa
->spa_alloc_locks
[i
]);
8435 * Update usable space statistics.
8437 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, TXG_CLEAN(txg
)))
8439 vdev_sync_done(vd
, txg
);
8441 spa_update_dspace(spa
);
8444 * It had better be the case that we didn't dirty anything
8445 * since vdev_config_sync().
8447 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
, txg
));
8448 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
8449 ASSERT(txg_list_empty(&spa
->spa_vdev_txg_list
, txg
));
8451 while (zfs_pause_spa_sync
)
8454 spa
->spa_sync_pass
= 0;
8457 * Update the last synced uberblock here. We want to do this at
8458 * the end of spa_sync() so that consumers of spa_last_synced_txg()
8459 * will be guaranteed that all the processing associated with
8460 * that txg has been completed.
8462 spa
->spa_ubsync
= spa
->spa_uberblock
;
8463 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
8465 spa_handle_ignored_writes(spa
);
8468 * If any async tasks have been requested, kick them off.
8470 spa_async_dispatch(spa
);
8474 * Sync all pools. We don't want to hold the namespace lock across these
8475 * operations, so we take a reference on the spa_t and drop the lock during the
8479 spa_sync_allpools(void)
8482 mutex_enter(&spa_namespace_lock
);
8483 while ((spa
= spa_next(spa
)) != NULL
) {
8484 if (spa_state(spa
) != POOL_STATE_ACTIVE
||
8485 !spa_writeable(spa
) || spa_suspended(spa
))
8487 spa_open_ref(spa
, FTAG
);
8488 mutex_exit(&spa_namespace_lock
);
8489 txg_wait_synced(spa_get_dsl(spa
), 0);
8490 mutex_enter(&spa_namespace_lock
);
8491 spa_close(spa
, FTAG
);
8493 mutex_exit(&spa_namespace_lock
);
8497 * ==========================================================================
8498 * Miscellaneous routines
8499 * ==========================================================================
8503 * Remove all pools in the system.
8511 * Remove all cached state. All pools should be closed now,
8512 * so every spa in the AVL tree should be unreferenced.
8514 mutex_enter(&spa_namespace_lock
);
8515 while ((spa
= spa_next(NULL
)) != NULL
) {
8517 * Stop async tasks. The async thread may need to detach
8518 * a device that's been replaced, which requires grabbing
8519 * spa_namespace_lock, so we must drop it here.
8521 spa_open_ref(spa
, FTAG
);
8522 mutex_exit(&spa_namespace_lock
);
8523 spa_async_suspend(spa
);
8524 mutex_enter(&spa_namespace_lock
);
8525 spa_close(spa
, FTAG
);
8527 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
8529 spa_deactivate(spa
);
8533 mutex_exit(&spa_namespace_lock
);
8537 spa_lookup_by_guid(spa_t
*spa
, uint64_t guid
, boolean_t aux
)
8542 if ((vd
= vdev_lookup_by_guid(spa
->spa_root_vdev
, guid
)) != NULL
)
8546 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
8547 vd
= spa
->spa_l2cache
.sav_vdevs
[i
];
8548 if (vd
->vdev_guid
== guid
)
8552 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
8553 vd
= spa
->spa_spares
.sav_vdevs
[i
];
8554 if (vd
->vdev_guid
== guid
)
8563 spa_upgrade(spa_t
*spa
, uint64_t version
)
8565 ASSERT(spa_writeable(spa
));
8567 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
8570 * This should only be called for a non-faulted pool, and since a
8571 * future version would result in an unopenable pool, this shouldn't be
8574 ASSERT(SPA_VERSION_IS_SUPPORTED(spa
->spa_uberblock
.ub_version
));
8575 ASSERT3U(version
, >=, spa
->spa_uberblock
.ub_version
);
8577 spa
->spa_uberblock
.ub_version
= version
;
8578 vdev_config_dirty(spa
->spa_root_vdev
);
8580 spa_config_exit(spa
, SCL_ALL
, FTAG
);
8582 txg_wait_synced(spa_get_dsl(spa
), 0);
8586 spa_has_spare(spa_t
*spa
, uint64_t guid
)
8590 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
8592 for (i
= 0; i
< sav
->sav_count
; i
++)
8593 if (sav
->sav_vdevs
[i
]->vdev_guid
== guid
)
8596 for (i
= 0; i
< sav
->sav_npending
; i
++) {
8597 if (nvlist_lookup_uint64(sav
->sav_pending
[i
], ZPOOL_CONFIG_GUID
,
8598 &spareguid
) == 0 && spareguid
== guid
)
8606 * Check if a pool has an active shared spare device.
8607 * Note: reference count of an active spare is 2, as a spare and as a replace
8610 spa_has_active_shared_spare(spa_t
*spa
)
8614 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
8616 for (i
= 0; i
< sav
->sav_count
; i
++) {
8617 if (spa_spare_exists(sav
->sav_vdevs
[i
]->vdev_guid
, &pool
,
8618 &refcnt
) && pool
!= 0ULL && pool
== spa_guid(spa
) &&
8627 spa_event_create(spa_t
*spa
, vdev_t
*vd
, nvlist_t
*hist_nvl
, const char *name
)
8629 sysevent_t
*ev
= NULL
;
8633 resource
= zfs_event_create(spa
, vd
, FM_SYSEVENT_CLASS
, name
, hist_nvl
);
8635 ev
= kmem_alloc(sizeof (sysevent_t
), KM_SLEEP
);
8636 ev
->resource
= resource
;
8643 spa_event_post(sysevent_t
*ev
)
8647 zfs_zevent_post(ev
->resource
, NULL
, zfs_zevent_post_cb
);
8648 kmem_free(ev
, sizeof (*ev
));
8654 * Post a zevent corresponding to the given sysevent. The 'name' must be one
8655 * of the event definitions in sys/sysevent/eventdefs.h. The payload will be
8656 * filled in from the spa and (optionally) the vdev. This doesn't do anything
8657 * in the userland libzpool, as we don't want consumers to misinterpret ztest
8658 * or zdb as real changes.
8661 spa_event_notify(spa_t
*spa
, vdev_t
*vd
, nvlist_t
*hist_nvl
, const char *name
)
8663 spa_event_post(spa_event_create(spa
, vd
, hist_nvl
, name
));
8666 #if defined(_KERNEL)
8667 /* state manipulation functions */
8668 EXPORT_SYMBOL(spa_open
);
8669 EXPORT_SYMBOL(spa_open_rewind
);
8670 EXPORT_SYMBOL(spa_get_stats
);
8671 EXPORT_SYMBOL(spa_create
);
8672 EXPORT_SYMBOL(spa_import
);
8673 EXPORT_SYMBOL(spa_tryimport
);
8674 EXPORT_SYMBOL(spa_destroy
);
8675 EXPORT_SYMBOL(spa_export
);
8676 EXPORT_SYMBOL(spa_reset
);
8677 EXPORT_SYMBOL(spa_async_request
);
8678 EXPORT_SYMBOL(spa_async_suspend
);
8679 EXPORT_SYMBOL(spa_async_resume
);
8680 EXPORT_SYMBOL(spa_inject_addref
);
8681 EXPORT_SYMBOL(spa_inject_delref
);
8682 EXPORT_SYMBOL(spa_scan_stat_init
);
8683 EXPORT_SYMBOL(spa_scan_get_stats
);
8685 /* device maniion */
8686 EXPORT_SYMBOL(spa_vdev_add
);
8687 EXPORT_SYMBOL(spa_vdev_attach
);
8688 EXPORT_SYMBOL(spa_vdev_detach
);
8689 EXPORT_SYMBOL(spa_vdev_setpath
);
8690 EXPORT_SYMBOL(spa_vdev_setfru
);
8691 EXPORT_SYMBOL(spa_vdev_split_mirror
);
8693 /* spare statech is global across all pools) */
8694 EXPORT_SYMBOL(spa_spare_add
);
8695 EXPORT_SYMBOL(spa_spare_remove
);
8696 EXPORT_SYMBOL(spa_spare_exists
);
8697 EXPORT_SYMBOL(spa_spare_activate
);
8699 /* L2ARC statech is global across all pools) */
8700 EXPORT_SYMBOL(spa_l2cache_add
);
8701 EXPORT_SYMBOL(spa_l2cache_remove
);
8702 EXPORT_SYMBOL(spa_l2cache_exists
);
8703 EXPORT_SYMBOL(spa_l2cache_activate
);
8704 EXPORT_SYMBOL(spa_l2cache_drop
);
8707 EXPORT_SYMBOL(spa_scan
);
8708 EXPORT_SYMBOL(spa_scan_stop
);
8711 EXPORT_SYMBOL(spa_sync
); /* only for DMU use */
8712 EXPORT_SYMBOL(spa_sync_allpools
);
8715 EXPORT_SYMBOL(spa_prop_set
);
8716 EXPORT_SYMBOL(spa_prop_get
);
8717 EXPORT_SYMBOL(spa_prop_clear_bootfs
);
8719 /* asynchronous event notification */
8720 EXPORT_SYMBOL(spa_event_notify
);
8723 #if defined(_KERNEL)
8724 module_param(spa_load_verify_maxinflight
, int, 0644);
8725 MODULE_PARM_DESC(spa_load_verify_maxinflight
,
8726 "Max concurrent traversal I/Os while verifying pool during import -X");
8728 module_param(spa_load_verify_metadata
, int, 0644);
8729 MODULE_PARM_DESC(spa_load_verify_metadata
,
8730 "Set to traverse metadata on pool import");
8732 module_param(spa_load_verify_data
, int, 0644);
8733 MODULE_PARM_DESC(spa_load_verify_data
,
8734 "Set to traverse data on pool import");
8736 module_param(spa_load_print_vdev_tree
, int, 0644);
8737 MODULE_PARM_DESC(spa_load_print_vdev_tree
,
8738 "Print vdev tree to zfs_dbgmsg during pool import");
8741 module_param(zio_taskq_batch_pct
, uint
, 0444);
8742 MODULE_PARM_DESC(zio_taskq_batch_pct
,
8743 "Percentage of CPUs to run an IO worker thread");
8746 module_param(zfs_max_missing_tvds
, ulong
, 0644);
8747 MODULE_PARM_DESC(zfs_max_missing_tvds
,
8748 "Allow importing pool with up to this number of missing top-level vdevs"
8749 " (in read-only mode)");