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_disk.h>
60 #include <sys/metaslab.h>
61 #include <sys/metaslab_impl.h>
63 #include <sys/uberblock_impl.h>
66 #include <sys/bpobj.h>
67 #include <sys/dmu_traverse.h>
68 #include <sys/dmu_objset.h>
69 #include <sys/unique.h>
70 #include <sys/dsl_pool.h>
71 #include <sys/dsl_dataset.h>
72 #include <sys/dsl_dir.h>
73 #include <sys/dsl_prop.h>
74 #include <sys/dsl_synctask.h>
75 #include <sys/fs/zfs.h>
77 #include <sys/callb.h>
78 #include <sys/systeminfo.h>
79 #include <sys/spa_boot.h>
80 #include <sys/zfs_ioctl.h>
81 #include <sys/dsl_scan.h>
82 #include <sys/zfeature.h>
83 #include <sys/dsl_destroy.h>
87 #include <sys/fm/protocol.h>
88 #include <sys/fm/util.h>
89 #include <sys/callb.h>
94 #include "zfs_comutil.h"
97 * The interval, in seconds, at which failed configuration cache file writes
100 int zfs_ccw_retry_interval
= 300;
102 typedef enum zti_modes
{
103 ZTI_MODE_FIXED
, /* value is # of threads (min 1) */
104 ZTI_MODE_BATCH
, /* cpu-intensive; value is ignored */
105 ZTI_MODE_NULL
, /* don't create a taskq */
109 #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) }
110 #define ZTI_PCT(n) { ZTI_MODE_ONLINE_PERCENT, (n), 1 }
111 #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 }
112 #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 }
114 #define ZTI_N(n) ZTI_P(n, 1)
115 #define ZTI_ONE ZTI_N(1)
117 typedef struct zio_taskq_info
{
118 zti_modes_t zti_mode
;
123 static const char *const zio_taskq_types
[ZIO_TASKQ_TYPES
] = {
124 "iss", "iss_h", "int", "int_h"
128 * This table defines the taskq settings for each ZFS I/O type. When
129 * initializing a pool, we use this table to create an appropriately sized
130 * taskq. Some operations are low volume and therefore have a small, static
131 * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
132 * macros. Other operations process a large amount of data; the ZTI_BATCH
133 * macro causes us to create a taskq oriented for throughput. Some operations
134 * are so high frequency and short-lived that the taskq itself can become a a
135 * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
136 * additional degree of parallelism specified by the number of threads per-
137 * taskq and the number of taskqs; when dispatching an event in this case, the
138 * particular taskq is chosen at random.
140 * The different taskq priorities are to handle the different contexts (issue
141 * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that
142 * need to be handled with minimum delay.
144 const zio_taskq_info_t zio_taskqs
[ZIO_TYPES
][ZIO_TASKQ_TYPES
] = {
145 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
146 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* NULL */
147 { ZTI_N(8), ZTI_NULL
, ZTI_P(12, 8), ZTI_NULL
}, /* READ */
148 { ZTI_BATCH
, ZTI_N(5), ZTI_P(12, 8), ZTI_N(5) }, /* WRITE */
149 { ZTI_P(12, 8), ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* FREE */
150 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* CLAIM */
151 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* IOCTL */
154 static void spa_sync_version(void *arg
, dmu_tx_t
*tx
);
155 static void spa_sync_props(void *arg
, dmu_tx_t
*tx
);
156 static boolean_t
spa_has_active_shared_spare(spa_t
*spa
);
157 static int spa_load_impl(spa_t
*spa
, spa_import_type_t type
, char **ereport
);
158 static void spa_vdev_resilver_done(spa_t
*spa
);
160 uint_t zio_taskq_batch_pct
= 75; /* 1 thread per cpu in pset */
161 boolean_t zio_taskq_sysdc
= B_TRUE
; /* use SDC scheduling class */
162 uint_t zio_taskq_basedc
= 80; /* base duty cycle */
164 boolean_t spa_create_process
= B_TRUE
; /* no process ==> no sysdc */
167 * Report any spa_load_verify errors found, but do not fail spa_load.
168 * This is used by zdb to analyze non-idle pools.
170 boolean_t spa_load_verify_dryrun
= B_FALSE
;
173 * This (illegal) pool name is used when temporarily importing a spa_t in order
174 * to get the vdev stats associated with the imported devices.
176 #define TRYIMPORT_NAME "$import"
179 * For debugging purposes: print out vdev tree during pool import.
181 int spa_load_print_vdev_tree
= B_FALSE
;
184 * A non-zero value for zfs_max_missing_tvds means that we allow importing
185 * pools with missing top-level vdevs. This is strictly intended for advanced
186 * pool recovery cases since missing data is almost inevitable. Pools with
187 * missing devices can only be imported read-only for safety reasons, and their
188 * fail-mode will be automatically set to "continue".
190 * With 1 missing vdev we should be able to import the pool and mount all
191 * datasets. User data that was not modified after the missing device has been
192 * added should be recoverable. This means that snapshots created prior to the
193 * addition of that device should be completely intact.
195 * With 2 missing vdevs, some datasets may fail to mount since there are
196 * dataset statistics that are stored as regular metadata. Some data might be
197 * recoverable if those vdevs were added recently.
199 * With 3 or more missing vdevs, the pool is severely damaged and MOS entries
200 * may be missing entirely. Chances of data recovery are very low. Note that
201 * there are also risks of performing an inadvertent rewind as we might be
202 * missing all the vdevs with the latest uberblocks.
204 unsigned long zfs_max_missing_tvds
= 0;
207 * The parameters below are similar to zfs_max_missing_tvds but are only
208 * intended for a preliminary open of the pool with an untrusted config which
209 * might be incomplete or out-dated.
211 * We are more tolerant for pools opened from a cachefile since we could have
212 * an out-dated cachefile where a device removal was not registered.
213 * We could have set the limit arbitrarily high but in the case where devices
214 * are really missing we would want to return the proper error codes; we chose
215 * SPA_DVAS_PER_BP - 1 so that some copies of the MOS would still be available
216 * and we get a chance to retrieve the trusted config.
218 uint64_t zfs_max_missing_tvds_cachefile
= SPA_DVAS_PER_BP
- 1;
221 * In the case where config was assembled by scanning device paths (/dev/dsks
222 * by default) we are less tolerant since all the existing devices should have
223 * been detected and we want spa_load to return the right error codes.
225 uint64_t zfs_max_missing_tvds_scan
= 0;
228 * Debugging aid that pauses spa_sync() towards the end.
230 boolean_t zfs_pause_spa_sync
= B_FALSE
;
233 * ==========================================================================
234 * SPA properties routines
235 * ==========================================================================
239 * Add a (source=src, propname=propval) list to an nvlist.
242 spa_prop_add_list(nvlist_t
*nvl
, zpool_prop_t prop
, char *strval
,
243 uint64_t intval
, zprop_source_t src
)
245 const char *propname
= zpool_prop_to_name(prop
);
248 VERIFY(nvlist_alloc(&propval
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
249 VERIFY(nvlist_add_uint64(propval
, ZPROP_SOURCE
, src
) == 0);
252 VERIFY(nvlist_add_string(propval
, ZPROP_VALUE
, strval
) == 0);
254 VERIFY(nvlist_add_uint64(propval
, ZPROP_VALUE
, intval
) == 0);
256 VERIFY(nvlist_add_nvlist(nvl
, propname
, propval
) == 0);
257 nvlist_free(propval
);
261 * Get property values from the spa configuration.
264 spa_prop_get_config(spa_t
*spa
, nvlist_t
**nvp
)
266 vdev_t
*rvd
= spa
->spa_root_vdev
;
267 dsl_pool_t
*pool
= spa
->spa_dsl_pool
;
268 uint64_t size
, alloc
, cap
, version
;
269 const zprop_source_t src
= ZPROP_SRC_NONE
;
270 spa_config_dirent_t
*dp
;
271 metaslab_class_t
*mc
= spa_normal_class(spa
);
273 ASSERT(MUTEX_HELD(&spa
->spa_props_lock
));
276 alloc
= metaslab_class_get_alloc(mc
);
277 alloc
+= metaslab_class_get_alloc(spa_special_class(spa
));
278 alloc
+= metaslab_class_get_alloc(spa_dedup_class(spa
));
280 size
= metaslab_class_get_space(mc
);
281 size
+= metaslab_class_get_space(spa_special_class(spa
));
282 size
+= metaslab_class_get_space(spa_dedup_class(spa
));
284 spa_prop_add_list(*nvp
, ZPOOL_PROP_NAME
, spa_name(spa
), 0, src
);
285 spa_prop_add_list(*nvp
, ZPOOL_PROP_SIZE
, NULL
, size
, src
);
286 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALLOCATED
, NULL
, alloc
, src
);
287 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREE
, NULL
,
289 spa_prop_add_list(*nvp
, ZPOOL_PROP_CHECKPOINT
, NULL
,
290 spa
->spa_checkpoint_info
.sci_dspace
, src
);
292 spa_prop_add_list(*nvp
, ZPOOL_PROP_FRAGMENTATION
, NULL
,
293 metaslab_class_fragmentation(mc
), src
);
294 spa_prop_add_list(*nvp
, ZPOOL_PROP_EXPANDSZ
, NULL
,
295 metaslab_class_expandable_space(mc
), src
);
296 spa_prop_add_list(*nvp
, ZPOOL_PROP_READONLY
, NULL
,
297 (spa_mode(spa
) == FREAD
), src
);
299 cap
= (size
== 0) ? 0 : (alloc
* 100 / size
);
300 spa_prop_add_list(*nvp
, ZPOOL_PROP_CAPACITY
, NULL
, cap
, src
);
302 spa_prop_add_list(*nvp
, ZPOOL_PROP_DEDUPRATIO
, NULL
,
303 ddt_get_pool_dedup_ratio(spa
), src
);
305 spa_prop_add_list(*nvp
, ZPOOL_PROP_HEALTH
, NULL
,
306 rvd
->vdev_state
, src
);
308 version
= spa_version(spa
);
309 if (version
== zpool_prop_default_numeric(ZPOOL_PROP_VERSION
)) {
310 spa_prop_add_list(*nvp
, ZPOOL_PROP_VERSION
, NULL
,
311 version
, ZPROP_SRC_DEFAULT
);
313 spa_prop_add_list(*nvp
, ZPOOL_PROP_VERSION
, NULL
,
314 version
, ZPROP_SRC_LOCAL
);
316 spa_prop_add_list(*nvp
, ZPOOL_PROP_LOAD_GUID
,
317 NULL
, spa_load_guid(spa
), src
);
322 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
323 * when opening pools before this version freedir will be NULL.
325 if (pool
->dp_free_dir
!= NULL
) {
326 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
, NULL
,
327 dsl_dir_phys(pool
->dp_free_dir
)->dd_used_bytes
,
330 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
,
334 if (pool
->dp_leak_dir
!= NULL
) {
335 spa_prop_add_list(*nvp
, ZPOOL_PROP_LEAKED
, NULL
,
336 dsl_dir_phys(pool
->dp_leak_dir
)->dd_used_bytes
,
339 spa_prop_add_list(*nvp
, ZPOOL_PROP_LEAKED
,
344 spa_prop_add_list(*nvp
, ZPOOL_PROP_GUID
, NULL
, spa_guid(spa
), src
);
346 if (spa
->spa_comment
!= NULL
) {
347 spa_prop_add_list(*nvp
, ZPOOL_PROP_COMMENT
, spa
->spa_comment
,
351 if (spa
->spa_root
!= NULL
)
352 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALTROOT
, spa
->spa_root
,
355 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_BLOCKS
)) {
356 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXBLOCKSIZE
, NULL
,
357 MIN(zfs_max_recordsize
, SPA_MAXBLOCKSIZE
), ZPROP_SRC_NONE
);
359 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXBLOCKSIZE
, NULL
,
360 SPA_OLD_MAXBLOCKSIZE
, ZPROP_SRC_NONE
);
363 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_DNODE
)) {
364 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXDNODESIZE
, NULL
,
365 DNODE_MAX_SIZE
, ZPROP_SRC_NONE
);
367 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXDNODESIZE
, NULL
,
368 DNODE_MIN_SIZE
, ZPROP_SRC_NONE
);
371 if ((dp
= list_head(&spa
->spa_config_list
)) != NULL
) {
372 if (dp
->scd_path
== NULL
) {
373 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
374 "none", 0, ZPROP_SRC_LOCAL
);
375 } else if (strcmp(dp
->scd_path
, spa_config_path
) != 0) {
376 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
377 dp
->scd_path
, 0, ZPROP_SRC_LOCAL
);
383 * Get zpool property values.
386 spa_prop_get(spa_t
*spa
, nvlist_t
**nvp
)
388 objset_t
*mos
= spa
->spa_meta_objset
;
393 err
= nvlist_alloc(nvp
, NV_UNIQUE_NAME
, KM_SLEEP
);
397 mutex_enter(&spa
->spa_props_lock
);
400 * Get properties from the spa config.
402 spa_prop_get_config(spa
, nvp
);
404 /* If no pool property object, no more prop to get. */
405 if (mos
== NULL
|| spa
->spa_pool_props_object
== 0) {
406 mutex_exit(&spa
->spa_props_lock
);
411 * Get properties from the MOS pool property object.
413 for (zap_cursor_init(&zc
, mos
, spa
->spa_pool_props_object
);
414 (err
= zap_cursor_retrieve(&zc
, &za
)) == 0;
415 zap_cursor_advance(&zc
)) {
418 zprop_source_t src
= ZPROP_SRC_DEFAULT
;
421 if ((prop
= zpool_name_to_prop(za
.za_name
)) == ZPOOL_PROP_INVAL
)
424 switch (za
.za_integer_length
) {
426 /* integer property */
427 if (za
.za_first_integer
!=
428 zpool_prop_default_numeric(prop
))
429 src
= ZPROP_SRC_LOCAL
;
431 if (prop
== ZPOOL_PROP_BOOTFS
) {
433 dsl_dataset_t
*ds
= NULL
;
435 dp
= spa_get_dsl(spa
);
436 dsl_pool_config_enter(dp
, FTAG
);
437 if ((err
= dsl_dataset_hold_obj(dp
,
438 za
.za_first_integer
, FTAG
, &ds
))) {
439 dsl_pool_config_exit(dp
, FTAG
);
443 strval
= kmem_alloc(ZFS_MAX_DATASET_NAME_LEN
,
445 dsl_dataset_name(ds
, strval
);
446 dsl_dataset_rele(ds
, FTAG
);
447 dsl_pool_config_exit(dp
, FTAG
);
450 intval
= za
.za_first_integer
;
453 spa_prop_add_list(*nvp
, prop
, strval
, intval
, src
);
456 kmem_free(strval
, ZFS_MAX_DATASET_NAME_LEN
);
461 /* string property */
462 strval
= kmem_alloc(za
.za_num_integers
, KM_SLEEP
);
463 err
= zap_lookup(mos
, spa
->spa_pool_props_object
,
464 za
.za_name
, 1, za
.za_num_integers
, strval
);
466 kmem_free(strval
, za
.za_num_integers
);
469 spa_prop_add_list(*nvp
, prop
, strval
, 0, src
);
470 kmem_free(strval
, za
.za_num_integers
);
477 zap_cursor_fini(&zc
);
478 mutex_exit(&spa
->spa_props_lock
);
480 if (err
&& err
!= ENOENT
) {
490 * Validate the given pool properties nvlist and modify the list
491 * for the property values to be set.
494 spa_prop_validate(spa_t
*spa
, nvlist_t
*props
)
497 int error
= 0, reset_bootfs
= 0;
499 boolean_t has_feature
= B_FALSE
;
502 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
504 char *strval
, *slash
, *check
, *fname
;
505 const char *propname
= nvpair_name(elem
);
506 zpool_prop_t prop
= zpool_name_to_prop(propname
);
509 case ZPOOL_PROP_INVAL
:
510 if (!zpool_prop_feature(propname
)) {
511 error
= SET_ERROR(EINVAL
);
516 * Sanitize the input.
518 if (nvpair_type(elem
) != DATA_TYPE_UINT64
) {
519 error
= SET_ERROR(EINVAL
);
523 if (nvpair_value_uint64(elem
, &intval
) != 0) {
524 error
= SET_ERROR(EINVAL
);
529 error
= SET_ERROR(EINVAL
);
533 fname
= strchr(propname
, '@') + 1;
534 if (zfeature_lookup_name(fname
, NULL
) != 0) {
535 error
= SET_ERROR(EINVAL
);
539 has_feature
= B_TRUE
;
542 case ZPOOL_PROP_VERSION
:
543 error
= nvpair_value_uint64(elem
, &intval
);
545 (intval
< spa_version(spa
) ||
546 intval
> SPA_VERSION_BEFORE_FEATURES
||
548 error
= SET_ERROR(EINVAL
);
551 case ZPOOL_PROP_DELEGATION
:
552 case ZPOOL_PROP_AUTOREPLACE
:
553 case ZPOOL_PROP_LISTSNAPS
:
554 case ZPOOL_PROP_AUTOEXPAND
:
555 error
= nvpair_value_uint64(elem
, &intval
);
556 if (!error
&& intval
> 1)
557 error
= SET_ERROR(EINVAL
);
560 case ZPOOL_PROP_MULTIHOST
:
561 error
= nvpair_value_uint64(elem
, &intval
);
562 if (!error
&& intval
> 1)
563 error
= SET_ERROR(EINVAL
);
565 if (!error
&& !spa_get_hostid())
566 error
= SET_ERROR(ENOTSUP
);
570 case ZPOOL_PROP_BOOTFS
:
572 * If the pool version is less than SPA_VERSION_BOOTFS,
573 * or the pool is still being created (version == 0),
574 * the bootfs property cannot be set.
576 if (spa_version(spa
) < SPA_VERSION_BOOTFS
) {
577 error
= SET_ERROR(ENOTSUP
);
582 * Make sure the vdev config is bootable
584 if (!vdev_is_bootable(spa
->spa_root_vdev
)) {
585 error
= SET_ERROR(ENOTSUP
);
591 error
= nvpair_value_string(elem
, &strval
);
597 if (strval
== NULL
|| strval
[0] == '\0') {
598 objnum
= zpool_prop_default_numeric(
603 error
= dmu_objset_hold(strval
, FTAG
, &os
);
608 * Must be ZPL, and its property settings
609 * must be supported by GRUB (compression
610 * is not gzip, and large blocks or large
611 * dnodes are not used).
614 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
615 error
= SET_ERROR(ENOTSUP
);
617 dsl_prop_get_int_ds(dmu_objset_ds(os
),
618 zfs_prop_to_name(ZFS_PROP_COMPRESSION
),
620 !BOOTFS_COMPRESS_VALID(propval
)) {
621 error
= SET_ERROR(ENOTSUP
);
623 dsl_prop_get_int_ds(dmu_objset_ds(os
),
624 zfs_prop_to_name(ZFS_PROP_DNODESIZE
),
626 propval
!= ZFS_DNSIZE_LEGACY
) {
627 error
= SET_ERROR(ENOTSUP
);
629 objnum
= dmu_objset_id(os
);
631 dmu_objset_rele(os
, FTAG
);
635 case ZPOOL_PROP_FAILUREMODE
:
636 error
= nvpair_value_uint64(elem
, &intval
);
637 if (!error
&& intval
> ZIO_FAILURE_MODE_PANIC
)
638 error
= SET_ERROR(EINVAL
);
641 * This is a special case which only occurs when
642 * the pool has completely failed. This allows
643 * the user to change the in-core failmode property
644 * without syncing it out to disk (I/Os might
645 * currently be blocked). We do this by returning
646 * EIO to the caller (spa_prop_set) to trick it
647 * into thinking we encountered a property validation
650 if (!error
&& spa_suspended(spa
)) {
651 spa
->spa_failmode
= intval
;
652 error
= SET_ERROR(EIO
);
656 case ZPOOL_PROP_CACHEFILE
:
657 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
660 if (strval
[0] == '\0')
663 if (strcmp(strval
, "none") == 0)
666 if (strval
[0] != '/') {
667 error
= SET_ERROR(EINVAL
);
671 slash
= strrchr(strval
, '/');
672 ASSERT(slash
!= NULL
);
674 if (slash
[1] == '\0' || strcmp(slash
, "/.") == 0 ||
675 strcmp(slash
, "/..") == 0)
676 error
= SET_ERROR(EINVAL
);
679 case ZPOOL_PROP_COMMENT
:
680 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
682 for (check
= strval
; *check
!= '\0'; check
++) {
683 if (!isprint(*check
)) {
684 error
= SET_ERROR(EINVAL
);
688 if (strlen(strval
) > ZPROP_MAX_COMMENT
)
689 error
= SET_ERROR(E2BIG
);
692 case ZPOOL_PROP_DEDUPDITTO
:
693 if (spa_version(spa
) < SPA_VERSION_DEDUP
)
694 error
= SET_ERROR(ENOTSUP
);
696 error
= nvpair_value_uint64(elem
, &intval
);
698 intval
!= 0 && intval
< ZIO_DEDUPDITTO_MIN
)
699 error
= SET_ERROR(EINVAL
);
710 if (!error
&& reset_bootfs
) {
711 error
= nvlist_remove(props
,
712 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), DATA_TYPE_STRING
);
715 error
= nvlist_add_uint64(props
,
716 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), objnum
);
724 spa_configfile_set(spa_t
*spa
, nvlist_t
*nvp
, boolean_t need_sync
)
727 spa_config_dirent_t
*dp
;
729 if (nvlist_lookup_string(nvp
, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
),
733 dp
= kmem_alloc(sizeof (spa_config_dirent_t
),
736 if (cachefile
[0] == '\0')
737 dp
->scd_path
= spa_strdup(spa_config_path
);
738 else if (strcmp(cachefile
, "none") == 0)
741 dp
->scd_path
= spa_strdup(cachefile
);
743 list_insert_head(&spa
->spa_config_list
, dp
);
745 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
749 spa_prop_set(spa_t
*spa
, nvlist_t
*nvp
)
752 nvpair_t
*elem
= NULL
;
753 boolean_t need_sync
= B_FALSE
;
755 if ((error
= spa_prop_validate(spa
, nvp
)) != 0)
758 while ((elem
= nvlist_next_nvpair(nvp
, elem
)) != NULL
) {
759 zpool_prop_t prop
= zpool_name_to_prop(nvpair_name(elem
));
761 if (prop
== ZPOOL_PROP_CACHEFILE
||
762 prop
== ZPOOL_PROP_ALTROOT
||
763 prop
== ZPOOL_PROP_READONLY
)
766 if (prop
== ZPOOL_PROP_VERSION
|| prop
== ZPOOL_PROP_INVAL
) {
769 if (prop
== ZPOOL_PROP_VERSION
) {
770 VERIFY(nvpair_value_uint64(elem
, &ver
) == 0);
772 ASSERT(zpool_prop_feature(nvpair_name(elem
)));
773 ver
= SPA_VERSION_FEATURES
;
777 /* Save time if the version is already set. */
778 if (ver
== spa_version(spa
))
782 * In addition to the pool directory object, we might
783 * create the pool properties object, the features for
784 * read object, the features for write object, or the
785 * feature descriptions object.
787 error
= dsl_sync_task(spa
->spa_name
, NULL
,
788 spa_sync_version
, &ver
,
789 6, ZFS_SPACE_CHECK_RESERVED
);
800 return (dsl_sync_task(spa
->spa_name
, NULL
, spa_sync_props
,
801 nvp
, 6, ZFS_SPACE_CHECK_RESERVED
));
808 * If the bootfs property value is dsobj, clear it.
811 spa_prop_clear_bootfs(spa_t
*spa
, uint64_t dsobj
, dmu_tx_t
*tx
)
813 if (spa
->spa_bootfs
== dsobj
&& spa
->spa_pool_props_object
!= 0) {
814 VERIFY(zap_remove(spa
->spa_meta_objset
,
815 spa
->spa_pool_props_object
,
816 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), tx
) == 0);
823 spa_change_guid_check(void *arg
, dmu_tx_t
*tx
)
825 ASSERTV(uint64_t *newguid
= arg
);
826 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
827 vdev_t
*rvd
= spa
->spa_root_vdev
;
830 if (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
)) {
831 int error
= (spa_has_checkpoint(spa
)) ?
832 ZFS_ERR_CHECKPOINT_EXISTS
: ZFS_ERR_DISCARDING_CHECKPOINT
;
833 return (SET_ERROR(error
));
836 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
837 vdev_state
= rvd
->vdev_state
;
838 spa_config_exit(spa
, SCL_STATE
, FTAG
);
840 if (vdev_state
!= VDEV_STATE_HEALTHY
)
841 return (SET_ERROR(ENXIO
));
843 ASSERT3U(spa_guid(spa
), !=, *newguid
);
849 spa_change_guid_sync(void *arg
, dmu_tx_t
*tx
)
851 uint64_t *newguid
= arg
;
852 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
854 vdev_t
*rvd
= spa
->spa_root_vdev
;
856 oldguid
= spa_guid(spa
);
858 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
859 rvd
->vdev_guid
= *newguid
;
860 rvd
->vdev_guid_sum
+= (*newguid
- oldguid
);
861 vdev_config_dirty(rvd
);
862 spa_config_exit(spa
, SCL_STATE
, FTAG
);
864 spa_history_log_internal(spa
, "guid change", tx
, "old=%llu new=%llu",
869 * Change the GUID for the pool. This is done so that we can later
870 * re-import a pool built from a clone of our own vdevs. We will modify
871 * the root vdev's guid, our own pool guid, and then mark all of our
872 * vdevs dirty. Note that we must make sure that all our vdevs are
873 * online when we do this, or else any vdevs that weren't present
874 * would be orphaned from our pool. We are also going to issue a
875 * sysevent to update any watchers.
878 spa_change_guid(spa_t
*spa
)
883 mutex_enter(&spa
->spa_vdev_top_lock
);
884 mutex_enter(&spa_namespace_lock
);
885 guid
= spa_generate_guid(NULL
);
887 error
= dsl_sync_task(spa
->spa_name
, spa_change_guid_check
,
888 spa_change_guid_sync
, &guid
, 5, ZFS_SPACE_CHECK_RESERVED
);
891 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
);
892 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_REGUID
);
895 mutex_exit(&spa_namespace_lock
);
896 mutex_exit(&spa
->spa_vdev_top_lock
);
902 * ==========================================================================
903 * SPA state manipulation (open/create/destroy/import/export)
904 * ==========================================================================
908 spa_error_entry_compare(const void *a
, const void *b
)
910 const spa_error_entry_t
*sa
= (const spa_error_entry_t
*)a
;
911 const spa_error_entry_t
*sb
= (const spa_error_entry_t
*)b
;
914 ret
= memcmp(&sa
->se_bookmark
, &sb
->se_bookmark
,
915 sizeof (zbookmark_phys_t
));
917 return (AVL_ISIGN(ret
));
921 * Utility function which retrieves copies of the current logs and
922 * re-initializes them in the process.
925 spa_get_errlists(spa_t
*spa
, avl_tree_t
*last
, avl_tree_t
*scrub
)
927 ASSERT(MUTEX_HELD(&spa
->spa_errlist_lock
));
929 bcopy(&spa
->spa_errlist_last
, last
, sizeof (avl_tree_t
));
930 bcopy(&spa
->spa_errlist_scrub
, scrub
, sizeof (avl_tree_t
));
932 avl_create(&spa
->spa_errlist_scrub
,
933 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
934 offsetof(spa_error_entry_t
, se_avl
));
935 avl_create(&spa
->spa_errlist_last
,
936 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
937 offsetof(spa_error_entry_t
, se_avl
));
941 spa_taskqs_init(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
943 const zio_taskq_info_t
*ztip
= &zio_taskqs
[t
][q
];
944 enum zti_modes mode
= ztip
->zti_mode
;
945 uint_t value
= ztip
->zti_value
;
946 uint_t count
= ztip
->zti_count
;
947 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
949 boolean_t batch
= B_FALSE
;
951 if (mode
== ZTI_MODE_NULL
) {
953 tqs
->stqs_taskq
= NULL
;
957 ASSERT3U(count
, >, 0);
959 tqs
->stqs_count
= count
;
960 tqs
->stqs_taskq
= kmem_alloc(count
* sizeof (taskq_t
*), KM_SLEEP
);
964 ASSERT3U(value
, >=, 1);
965 value
= MAX(value
, 1);
966 flags
|= TASKQ_DYNAMIC
;
971 flags
|= TASKQ_THREADS_CPU_PCT
;
972 value
= MIN(zio_taskq_batch_pct
, 100);
976 panic("unrecognized mode for %s_%s taskq (%u:%u) in "
978 zio_type_name
[t
], zio_taskq_types
[q
], mode
, value
);
982 for (uint_t i
= 0; i
< count
; i
++) {
986 (void) snprintf(name
, sizeof (name
), "%s_%s",
987 zio_type_name
[t
], zio_taskq_types
[q
]);
989 if (zio_taskq_sysdc
&& spa
->spa_proc
!= &p0
) {
991 flags
|= TASKQ_DC_BATCH
;
993 tq
= taskq_create_sysdc(name
, value
, 50, INT_MAX
,
994 spa
->spa_proc
, zio_taskq_basedc
, flags
);
996 pri_t pri
= maxclsyspri
;
998 * The write issue taskq can be extremely CPU
999 * intensive. Run it at slightly less important
1000 * priority than the other taskqs. Under Linux this
1001 * means incrementing the priority value on platforms
1002 * like illumos it should be decremented.
1004 if (t
== ZIO_TYPE_WRITE
&& q
== ZIO_TASKQ_ISSUE
)
1007 tq
= taskq_create_proc(name
, value
, pri
, 50,
1008 INT_MAX
, spa
->spa_proc
, flags
);
1011 tqs
->stqs_taskq
[i
] = tq
;
1016 spa_taskqs_fini(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
1018 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1020 if (tqs
->stqs_taskq
== NULL
) {
1021 ASSERT3U(tqs
->stqs_count
, ==, 0);
1025 for (uint_t i
= 0; i
< tqs
->stqs_count
; i
++) {
1026 ASSERT3P(tqs
->stqs_taskq
[i
], !=, NULL
);
1027 taskq_destroy(tqs
->stqs_taskq
[i
]);
1030 kmem_free(tqs
->stqs_taskq
, tqs
->stqs_count
* sizeof (taskq_t
*));
1031 tqs
->stqs_taskq
= NULL
;
1035 * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority.
1036 * Note that a type may have multiple discrete taskqs to avoid lock contention
1037 * on the taskq itself. In that case we choose which taskq at random by using
1038 * the low bits of gethrtime().
1041 spa_taskq_dispatch_ent(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
1042 task_func_t
*func
, void *arg
, uint_t flags
, taskq_ent_t
*ent
)
1044 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1047 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
1048 ASSERT3U(tqs
->stqs_count
, !=, 0);
1050 if (tqs
->stqs_count
== 1) {
1051 tq
= tqs
->stqs_taskq
[0];
1053 tq
= tqs
->stqs_taskq
[((uint64_t)gethrtime()) % tqs
->stqs_count
];
1056 taskq_dispatch_ent(tq
, func
, arg
, flags
, ent
);
1060 * Same as spa_taskq_dispatch_ent() but block on the task until completion.
1063 spa_taskq_dispatch_sync(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
1064 task_func_t
*func
, void *arg
, uint_t flags
)
1066 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1070 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
1071 ASSERT3U(tqs
->stqs_count
, !=, 0);
1073 if (tqs
->stqs_count
== 1) {
1074 tq
= tqs
->stqs_taskq
[0];
1076 tq
= tqs
->stqs_taskq
[((uint64_t)gethrtime()) % tqs
->stqs_count
];
1079 id
= taskq_dispatch(tq
, func
, arg
, flags
);
1081 taskq_wait_id(tq
, id
);
1085 spa_create_zio_taskqs(spa_t
*spa
)
1087 for (int t
= 0; t
< ZIO_TYPES
; t
++) {
1088 for (int q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1089 spa_taskqs_init(spa
, t
, q
);
1095 * Disabled until spa_thread() can be adapted for Linux.
1097 #undef HAVE_SPA_THREAD
1099 #if defined(_KERNEL) && defined(HAVE_SPA_THREAD)
1101 spa_thread(void *arg
)
1103 psetid_t zio_taskq_psrset_bind
= PS_NONE
;
1104 callb_cpr_t cprinfo
;
1107 user_t
*pu
= PTOU(curproc
);
1109 CALLB_CPR_INIT(&cprinfo
, &spa
->spa_proc_lock
, callb_generic_cpr
,
1112 ASSERT(curproc
!= &p0
);
1113 (void) snprintf(pu
->u_psargs
, sizeof (pu
->u_psargs
),
1114 "zpool-%s", spa
->spa_name
);
1115 (void) strlcpy(pu
->u_comm
, pu
->u_psargs
, sizeof (pu
->u_comm
));
1117 /* bind this thread to the requested psrset */
1118 if (zio_taskq_psrset_bind
!= PS_NONE
) {
1120 mutex_enter(&cpu_lock
);
1121 mutex_enter(&pidlock
);
1122 mutex_enter(&curproc
->p_lock
);
1124 if (cpupart_bind_thread(curthread
, zio_taskq_psrset_bind
,
1125 0, NULL
, NULL
) == 0) {
1126 curthread
->t_bind_pset
= zio_taskq_psrset_bind
;
1129 "Couldn't bind process for zfs pool \"%s\" to "
1130 "pset %d\n", spa
->spa_name
, zio_taskq_psrset_bind
);
1133 mutex_exit(&curproc
->p_lock
);
1134 mutex_exit(&pidlock
);
1135 mutex_exit(&cpu_lock
);
1139 if (zio_taskq_sysdc
) {
1140 sysdc_thread_enter(curthread
, 100, 0);
1143 spa
->spa_proc
= curproc
;
1144 spa
->spa_did
= curthread
->t_did
;
1146 spa_create_zio_taskqs(spa
);
1148 mutex_enter(&spa
->spa_proc_lock
);
1149 ASSERT(spa
->spa_proc_state
== SPA_PROC_CREATED
);
1151 spa
->spa_proc_state
= SPA_PROC_ACTIVE
;
1152 cv_broadcast(&spa
->spa_proc_cv
);
1154 CALLB_CPR_SAFE_BEGIN(&cprinfo
);
1155 while (spa
->spa_proc_state
== SPA_PROC_ACTIVE
)
1156 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1157 CALLB_CPR_SAFE_END(&cprinfo
, &spa
->spa_proc_lock
);
1159 ASSERT(spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
);
1160 spa
->spa_proc_state
= SPA_PROC_GONE
;
1161 spa
->spa_proc
= &p0
;
1162 cv_broadcast(&spa
->spa_proc_cv
);
1163 CALLB_CPR_EXIT(&cprinfo
); /* drops spa_proc_lock */
1165 mutex_enter(&curproc
->p_lock
);
1171 * Activate an uninitialized pool.
1174 spa_activate(spa_t
*spa
, int mode
)
1176 ASSERT(spa
->spa_state
== POOL_STATE_UNINITIALIZED
);
1178 spa
->spa_state
= POOL_STATE_ACTIVE
;
1179 spa
->spa_mode
= mode
;
1181 spa
->spa_normal_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
1182 spa
->spa_log_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
1183 spa
->spa_special_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
1184 spa
->spa_dedup_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
1186 /* Try to create a covering process */
1187 mutex_enter(&spa
->spa_proc_lock
);
1188 ASSERT(spa
->spa_proc_state
== SPA_PROC_NONE
);
1189 ASSERT(spa
->spa_proc
== &p0
);
1192 #ifdef HAVE_SPA_THREAD
1193 /* Only create a process if we're going to be around a while. */
1194 if (spa_create_process
&& strcmp(spa
->spa_name
, TRYIMPORT_NAME
) != 0) {
1195 if (newproc(spa_thread
, (caddr_t
)spa
, syscid
, maxclsyspri
,
1197 spa
->spa_proc_state
= SPA_PROC_CREATED
;
1198 while (spa
->spa_proc_state
== SPA_PROC_CREATED
) {
1199 cv_wait(&spa
->spa_proc_cv
,
1200 &spa
->spa_proc_lock
);
1202 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1203 ASSERT(spa
->spa_proc
!= &p0
);
1204 ASSERT(spa
->spa_did
!= 0);
1208 "Couldn't create process for zfs pool \"%s\"\n",
1213 #endif /* HAVE_SPA_THREAD */
1214 mutex_exit(&spa
->spa_proc_lock
);
1216 /* If we didn't create a process, we need to create our taskqs. */
1217 if (spa
->spa_proc
== &p0
) {
1218 spa_create_zio_taskqs(spa
);
1221 for (size_t i
= 0; i
< TXG_SIZE
; i
++)
1222 spa
->spa_txg_zio
[i
] = zio_root(spa
, NULL
, NULL
, 0);
1224 list_create(&spa
->spa_config_dirty_list
, sizeof (vdev_t
),
1225 offsetof(vdev_t
, vdev_config_dirty_node
));
1226 list_create(&spa
->spa_evicting_os_list
, sizeof (objset_t
),
1227 offsetof(objset_t
, os_evicting_node
));
1228 list_create(&spa
->spa_state_dirty_list
, sizeof (vdev_t
),
1229 offsetof(vdev_t
, vdev_state_dirty_node
));
1231 txg_list_create(&spa
->spa_vdev_txg_list
, spa
,
1232 offsetof(struct vdev
, vdev_txg_node
));
1234 avl_create(&spa
->spa_errlist_scrub
,
1235 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1236 offsetof(spa_error_entry_t
, se_avl
));
1237 avl_create(&spa
->spa_errlist_last
,
1238 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1239 offsetof(spa_error_entry_t
, se_avl
));
1241 spa_keystore_init(&spa
->spa_keystore
);
1244 * This taskq is used to perform zvol-minor-related tasks
1245 * asynchronously. This has several advantages, including easy
1246 * resolution of various deadlocks (zfsonlinux bug #3681).
1248 * The taskq must be single threaded to ensure tasks are always
1249 * processed in the order in which they were dispatched.
1251 * A taskq per pool allows one to keep the pools independent.
1252 * This way if one pool is suspended, it will not impact another.
1254 * The preferred location to dispatch a zvol minor task is a sync
1255 * task. In this context, there is easy access to the spa_t and minimal
1256 * error handling is required because the sync task must succeed.
1258 spa
->spa_zvol_taskq
= taskq_create("z_zvol", 1, defclsyspri
,
1262 * Taskq dedicated to prefetcher threads: this is used to prevent the
1263 * pool traverse code from monopolizing the global (and limited)
1264 * system_taskq by inappropriately scheduling long running tasks on it.
1266 spa
->spa_prefetch_taskq
= taskq_create("z_prefetch", boot_ncpus
,
1267 defclsyspri
, 1, INT_MAX
, TASKQ_DYNAMIC
);
1270 * The taskq to upgrade datasets in this pool. Currently used by
1271 * feature SPA_FEATURE_USEROBJ_ACCOUNTING/SPA_FEATURE_PROJECT_QUOTA.
1273 spa
->spa_upgrade_taskq
= taskq_create("z_upgrade", boot_ncpus
,
1274 defclsyspri
, 1, INT_MAX
, TASKQ_DYNAMIC
);
1278 * Opposite of spa_activate().
1281 spa_deactivate(spa_t
*spa
)
1283 ASSERT(spa
->spa_sync_on
== B_FALSE
);
1284 ASSERT(spa
->spa_dsl_pool
== NULL
);
1285 ASSERT(spa
->spa_root_vdev
== NULL
);
1286 ASSERT(spa
->spa_async_zio_root
== NULL
);
1287 ASSERT(spa
->spa_state
!= POOL_STATE_UNINITIALIZED
);
1289 spa_evicting_os_wait(spa
);
1291 if (spa
->spa_zvol_taskq
) {
1292 taskq_destroy(spa
->spa_zvol_taskq
);
1293 spa
->spa_zvol_taskq
= NULL
;
1296 if (spa
->spa_prefetch_taskq
) {
1297 taskq_destroy(spa
->spa_prefetch_taskq
);
1298 spa
->spa_prefetch_taskq
= NULL
;
1301 if (spa
->spa_upgrade_taskq
) {
1302 taskq_destroy(spa
->spa_upgrade_taskq
);
1303 spa
->spa_upgrade_taskq
= NULL
;
1306 txg_list_destroy(&spa
->spa_vdev_txg_list
);
1308 list_destroy(&spa
->spa_config_dirty_list
);
1309 list_destroy(&spa
->spa_evicting_os_list
);
1310 list_destroy(&spa
->spa_state_dirty_list
);
1312 taskq_cancel_id(system_delay_taskq
, spa
->spa_deadman_tqid
);
1314 for (int t
= 0; t
< ZIO_TYPES
; t
++) {
1315 for (int q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1316 spa_taskqs_fini(spa
, t
, q
);
1320 for (size_t i
= 0; i
< TXG_SIZE
; i
++) {
1321 ASSERT3P(spa
->spa_txg_zio
[i
], !=, NULL
);
1322 VERIFY0(zio_wait(spa
->spa_txg_zio
[i
]));
1323 spa
->spa_txg_zio
[i
] = NULL
;
1326 metaslab_class_destroy(spa
->spa_normal_class
);
1327 spa
->spa_normal_class
= NULL
;
1329 metaslab_class_destroy(spa
->spa_log_class
);
1330 spa
->spa_log_class
= NULL
;
1332 metaslab_class_destroy(spa
->spa_special_class
);
1333 spa
->spa_special_class
= NULL
;
1335 metaslab_class_destroy(spa
->spa_dedup_class
);
1336 spa
->spa_dedup_class
= NULL
;
1339 * If this was part of an import or the open otherwise failed, we may
1340 * still have errors left in the queues. Empty them just in case.
1342 spa_errlog_drain(spa
);
1343 avl_destroy(&spa
->spa_errlist_scrub
);
1344 avl_destroy(&spa
->spa_errlist_last
);
1346 spa_keystore_fini(&spa
->spa_keystore
);
1348 spa
->spa_state
= POOL_STATE_UNINITIALIZED
;
1350 mutex_enter(&spa
->spa_proc_lock
);
1351 if (spa
->spa_proc_state
!= SPA_PROC_NONE
) {
1352 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1353 spa
->spa_proc_state
= SPA_PROC_DEACTIVATE
;
1354 cv_broadcast(&spa
->spa_proc_cv
);
1355 while (spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
) {
1356 ASSERT(spa
->spa_proc
!= &p0
);
1357 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1359 ASSERT(spa
->spa_proc_state
== SPA_PROC_GONE
);
1360 spa
->spa_proc_state
= SPA_PROC_NONE
;
1362 ASSERT(spa
->spa_proc
== &p0
);
1363 mutex_exit(&spa
->spa_proc_lock
);
1366 * We want to make sure spa_thread() has actually exited the ZFS
1367 * module, so that the module can't be unloaded out from underneath
1370 if (spa
->spa_did
!= 0) {
1371 thread_join(spa
->spa_did
);
1377 * Verify a pool configuration, and construct the vdev tree appropriately. This
1378 * will create all the necessary vdevs in the appropriate layout, with each vdev
1379 * in the CLOSED state. This will prep the pool before open/creation/import.
1380 * All vdev validation is done by the vdev_alloc() routine.
1383 spa_config_parse(spa_t
*spa
, vdev_t
**vdp
, nvlist_t
*nv
, vdev_t
*parent
,
1384 uint_t id
, int atype
)
1390 if ((error
= vdev_alloc(spa
, vdp
, nv
, parent
, id
, atype
)) != 0)
1393 if ((*vdp
)->vdev_ops
->vdev_op_leaf
)
1396 error
= nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1399 if (error
== ENOENT
)
1405 return (SET_ERROR(EINVAL
));
1408 for (int c
= 0; c
< children
; c
++) {
1410 if ((error
= spa_config_parse(spa
, &vd
, child
[c
], *vdp
, c
,
1418 ASSERT(*vdp
!= NULL
);
1424 * Opposite of spa_load().
1427 spa_unload(spa_t
*spa
)
1431 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1433 spa_load_note(spa
, "UNLOADING");
1438 spa_async_suspend(spa
);
1443 if (spa
->spa_sync_on
) {
1444 txg_sync_stop(spa
->spa_dsl_pool
);
1445 spa
->spa_sync_on
= B_FALSE
;
1449 * Even though vdev_free() also calls vdev_metaslab_fini, we need
1450 * to call it earlier, before we wait for async i/o to complete.
1451 * This ensures that there is no async metaslab prefetching, by
1452 * calling taskq_wait(mg_taskq).
1454 if (spa
->spa_root_vdev
!= NULL
) {
1455 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1456 for (int c
= 0; c
< spa
->spa_root_vdev
->vdev_children
; c
++)
1457 vdev_metaslab_fini(spa
->spa_root_vdev
->vdev_child
[c
]);
1458 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1461 if (spa
->spa_mmp
.mmp_thread
)
1462 mmp_thread_stop(spa
);
1465 * Wait for any outstanding async I/O to complete.
1467 if (spa
->spa_async_zio_root
!= NULL
) {
1468 for (int i
= 0; i
< max_ncpus
; i
++)
1469 (void) zio_wait(spa
->spa_async_zio_root
[i
]);
1470 kmem_free(spa
->spa_async_zio_root
, max_ncpus
* sizeof (void *));
1471 spa
->spa_async_zio_root
= NULL
;
1474 if (spa
->spa_vdev_removal
!= NULL
) {
1475 spa_vdev_removal_destroy(spa
->spa_vdev_removal
);
1476 spa
->spa_vdev_removal
= NULL
;
1479 if (spa
->spa_condense_zthr
!= NULL
) {
1480 ASSERT(!zthr_isrunning(spa
->spa_condense_zthr
));
1481 zthr_destroy(spa
->spa_condense_zthr
);
1482 spa
->spa_condense_zthr
= NULL
;
1485 if (spa
->spa_checkpoint_discard_zthr
!= NULL
) {
1486 ASSERT(!zthr_isrunning(spa
->spa_checkpoint_discard_zthr
));
1487 zthr_destroy(spa
->spa_checkpoint_discard_zthr
);
1488 spa
->spa_checkpoint_discard_zthr
= NULL
;
1491 spa_condense_fini(spa
);
1493 bpobj_close(&spa
->spa_deferred_bpobj
);
1495 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1500 if (spa
->spa_root_vdev
)
1501 vdev_free(spa
->spa_root_vdev
);
1502 ASSERT(spa
->spa_root_vdev
== NULL
);
1505 * Close the dsl pool.
1507 if (spa
->spa_dsl_pool
) {
1508 dsl_pool_close(spa
->spa_dsl_pool
);
1509 spa
->spa_dsl_pool
= NULL
;
1510 spa
->spa_meta_objset
= NULL
;
1516 * Drop and purge level 2 cache
1518 spa_l2cache_drop(spa
);
1520 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1521 vdev_free(spa
->spa_spares
.sav_vdevs
[i
]);
1522 if (spa
->spa_spares
.sav_vdevs
) {
1523 kmem_free(spa
->spa_spares
.sav_vdevs
,
1524 spa
->spa_spares
.sav_count
* sizeof (void *));
1525 spa
->spa_spares
.sav_vdevs
= NULL
;
1527 if (spa
->spa_spares
.sav_config
) {
1528 nvlist_free(spa
->spa_spares
.sav_config
);
1529 spa
->spa_spares
.sav_config
= NULL
;
1531 spa
->spa_spares
.sav_count
= 0;
1533 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
1534 vdev_clear_stats(spa
->spa_l2cache
.sav_vdevs
[i
]);
1535 vdev_free(spa
->spa_l2cache
.sav_vdevs
[i
]);
1537 if (spa
->spa_l2cache
.sav_vdevs
) {
1538 kmem_free(spa
->spa_l2cache
.sav_vdevs
,
1539 spa
->spa_l2cache
.sav_count
* sizeof (void *));
1540 spa
->spa_l2cache
.sav_vdevs
= NULL
;
1542 if (spa
->spa_l2cache
.sav_config
) {
1543 nvlist_free(spa
->spa_l2cache
.sav_config
);
1544 spa
->spa_l2cache
.sav_config
= NULL
;
1546 spa
->spa_l2cache
.sav_count
= 0;
1548 spa
->spa_async_suspended
= 0;
1550 spa
->spa_indirect_vdevs_loaded
= B_FALSE
;
1552 if (spa
->spa_comment
!= NULL
) {
1553 spa_strfree(spa
->spa_comment
);
1554 spa
->spa_comment
= NULL
;
1557 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1561 * Load (or re-load) the current list of vdevs describing the active spares for
1562 * this pool. When this is called, we have some form of basic information in
1563 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1564 * then re-generate a more complete list including status information.
1567 spa_load_spares(spa_t
*spa
)
1576 * zdb opens both the current state of the pool and the
1577 * checkpointed state (if present), with a different spa_t.
1579 * As spare vdevs are shared among open pools, we skip loading
1580 * them when we load the checkpointed state of the pool.
1582 if (!spa_writeable(spa
))
1586 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1589 * First, close and free any existing spare vdevs.
1591 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1592 vd
= spa
->spa_spares
.sav_vdevs
[i
];
1594 /* Undo the call to spa_activate() below */
1595 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1596 B_FALSE
)) != NULL
&& tvd
->vdev_isspare
)
1597 spa_spare_remove(tvd
);
1602 if (spa
->spa_spares
.sav_vdevs
)
1603 kmem_free(spa
->spa_spares
.sav_vdevs
,
1604 spa
->spa_spares
.sav_count
* sizeof (void *));
1606 if (spa
->spa_spares
.sav_config
== NULL
)
1609 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
1610 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
1612 spa
->spa_spares
.sav_count
= (int)nspares
;
1613 spa
->spa_spares
.sav_vdevs
= NULL
;
1619 * Construct the array of vdevs, opening them to get status in the
1620 * process. For each spare, there is potentially two different vdev_t
1621 * structures associated with it: one in the list of spares (used only
1622 * for basic validation purposes) and one in the active vdev
1623 * configuration (if it's spared in). During this phase we open and
1624 * validate each vdev on the spare list. If the vdev also exists in the
1625 * active configuration, then we also mark this vdev as an active spare.
1627 spa
->spa_spares
.sav_vdevs
= kmem_zalloc(nspares
* sizeof (void *),
1629 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1630 VERIFY(spa_config_parse(spa
, &vd
, spares
[i
], NULL
, 0,
1631 VDEV_ALLOC_SPARE
) == 0);
1634 spa
->spa_spares
.sav_vdevs
[i
] = vd
;
1636 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1637 B_FALSE
)) != NULL
) {
1638 if (!tvd
->vdev_isspare
)
1642 * We only mark the spare active if we were successfully
1643 * able to load the vdev. Otherwise, importing a pool
1644 * with a bad active spare would result in strange
1645 * behavior, because multiple pool would think the spare
1646 * is actively in use.
1648 * There is a vulnerability here to an equally bizarre
1649 * circumstance, where a dead active spare is later
1650 * brought back to life (onlined or otherwise). Given
1651 * the rarity of this scenario, and the extra complexity
1652 * it adds, we ignore the possibility.
1654 if (!vdev_is_dead(tvd
))
1655 spa_spare_activate(tvd
);
1659 vd
->vdev_aux
= &spa
->spa_spares
;
1661 if (vdev_open(vd
) != 0)
1664 if (vdev_validate_aux(vd
) == 0)
1669 * Recompute the stashed list of spares, with status information
1672 VERIFY(nvlist_remove(spa
->spa_spares
.sav_config
, ZPOOL_CONFIG_SPARES
,
1673 DATA_TYPE_NVLIST_ARRAY
) == 0);
1675 spares
= kmem_alloc(spa
->spa_spares
.sav_count
* sizeof (void *),
1677 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1678 spares
[i
] = vdev_config_generate(spa
,
1679 spa
->spa_spares
.sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_SPARE
);
1680 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
1681 ZPOOL_CONFIG_SPARES
, spares
, spa
->spa_spares
.sav_count
) == 0);
1682 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1683 nvlist_free(spares
[i
]);
1684 kmem_free(spares
, spa
->spa_spares
.sav_count
* sizeof (void *));
1688 * Load (or re-load) the current list of vdevs describing the active l2cache for
1689 * this pool. When this is called, we have some form of basic information in
1690 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1691 * then re-generate a more complete list including status information.
1692 * Devices which are already active have their details maintained, and are
1696 spa_load_l2cache(spa_t
*spa
)
1698 nvlist_t
**l2cache
= NULL
;
1700 int i
, j
, oldnvdevs
;
1702 vdev_t
*vd
, **oldvdevs
, **newvdevs
;
1703 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
1707 * zdb opens both the current state of the pool and the
1708 * checkpointed state (if present), with a different spa_t.
1710 * As L2 caches are part of the ARC which is shared among open
1711 * pools, we skip loading them when we load the checkpointed
1712 * state of the pool.
1714 if (!spa_writeable(spa
))
1718 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1720 oldvdevs
= sav
->sav_vdevs
;
1721 oldnvdevs
= sav
->sav_count
;
1722 sav
->sav_vdevs
= NULL
;
1725 if (sav
->sav_config
== NULL
) {
1731 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
,
1732 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
1733 newvdevs
= kmem_alloc(nl2cache
* sizeof (void *), KM_SLEEP
);
1736 * Process new nvlist of vdevs.
1738 for (i
= 0; i
< nl2cache
; i
++) {
1739 VERIFY(nvlist_lookup_uint64(l2cache
[i
], ZPOOL_CONFIG_GUID
,
1743 for (j
= 0; j
< oldnvdevs
; j
++) {
1745 if (vd
!= NULL
&& guid
== vd
->vdev_guid
) {
1747 * Retain previous vdev for add/remove ops.
1755 if (newvdevs
[i
] == NULL
) {
1759 VERIFY(spa_config_parse(spa
, &vd
, l2cache
[i
], NULL
, 0,
1760 VDEV_ALLOC_L2CACHE
) == 0);
1765 * Commit this vdev as an l2cache device,
1766 * even if it fails to open.
1768 spa_l2cache_add(vd
);
1773 spa_l2cache_activate(vd
);
1775 if (vdev_open(vd
) != 0)
1778 (void) vdev_validate_aux(vd
);
1780 if (!vdev_is_dead(vd
))
1781 l2arc_add_vdev(spa
, vd
);
1785 sav
->sav_vdevs
= newvdevs
;
1786 sav
->sav_count
= (int)nl2cache
;
1789 * Recompute the stashed list of l2cache devices, with status
1790 * information this time.
1792 VERIFY(nvlist_remove(sav
->sav_config
, ZPOOL_CONFIG_L2CACHE
,
1793 DATA_TYPE_NVLIST_ARRAY
) == 0);
1795 if (sav
->sav_count
> 0)
1796 l2cache
= kmem_alloc(sav
->sav_count
* sizeof (void *),
1798 for (i
= 0; i
< sav
->sav_count
; i
++)
1799 l2cache
[i
] = vdev_config_generate(spa
,
1800 sav
->sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_L2CACHE
);
1801 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
,
1802 ZPOOL_CONFIG_L2CACHE
, l2cache
, sav
->sav_count
) == 0);
1806 * Purge vdevs that were dropped
1808 for (i
= 0; i
< oldnvdevs
; i
++) {
1813 ASSERT(vd
->vdev_isl2cache
);
1815 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
1816 pool
!= 0ULL && l2arc_vdev_present(vd
))
1817 l2arc_remove_vdev(vd
);
1818 vdev_clear_stats(vd
);
1824 kmem_free(oldvdevs
, oldnvdevs
* sizeof (void *));
1826 for (i
= 0; i
< sav
->sav_count
; i
++)
1827 nvlist_free(l2cache
[i
]);
1829 kmem_free(l2cache
, sav
->sav_count
* sizeof (void *));
1833 load_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
**value
)
1836 char *packed
= NULL
;
1841 error
= dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
);
1845 nvsize
= *(uint64_t *)db
->db_data
;
1846 dmu_buf_rele(db
, FTAG
);
1848 packed
= vmem_alloc(nvsize
, KM_SLEEP
);
1849 error
= dmu_read(spa
->spa_meta_objset
, obj
, 0, nvsize
, packed
,
1852 error
= nvlist_unpack(packed
, nvsize
, value
, 0);
1853 vmem_free(packed
, nvsize
);
1859 * Concrete top-level vdevs that are not missing and are not logs. At every
1860 * spa_sync we write new uberblocks to at least SPA_SYNC_MIN_VDEVS core tvds.
1863 spa_healthy_core_tvds(spa_t
*spa
)
1865 vdev_t
*rvd
= spa
->spa_root_vdev
;
1868 for (uint64_t i
= 0; i
< rvd
->vdev_children
; i
++) {
1869 vdev_t
*vd
= rvd
->vdev_child
[i
];
1872 if (vdev_is_concrete(vd
) && !vdev_is_dead(vd
))
1880 * Checks to see if the given vdev could not be opened, in which case we post a
1881 * sysevent to notify the autoreplace code that the device has been removed.
1884 spa_check_removed(vdev_t
*vd
)
1886 for (uint64_t c
= 0; c
< vd
->vdev_children
; c
++)
1887 spa_check_removed(vd
->vdev_child
[c
]);
1889 if (vd
->vdev_ops
->vdev_op_leaf
&& vdev_is_dead(vd
) &&
1890 vdev_is_concrete(vd
)) {
1891 zfs_post_autoreplace(vd
->vdev_spa
, vd
);
1892 spa_event_notify(vd
->vdev_spa
, vd
, NULL
, ESC_ZFS_VDEV_CHECK
);
1897 spa_check_for_missing_logs(spa_t
*spa
)
1899 vdev_t
*rvd
= spa
->spa_root_vdev
;
1902 * If we're doing a normal import, then build up any additional
1903 * diagnostic information about missing log devices.
1904 * We'll pass this up to the user for further processing.
1906 if (!(spa
->spa_import_flags
& ZFS_IMPORT_MISSING_LOG
)) {
1907 nvlist_t
**child
, *nv
;
1910 child
= kmem_alloc(rvd
->vdev_children
* sizeof (nvlist_t
*),
1912 VERIFY(nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
1914 for (uint64_t c
= 0; c
< rvd
->vdev_children
; c
++) {
1915 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1918 * We consider a device as missing only if it failed
1919 * to open (i.e. offline or faulted is not considered
1922 if (tvd
->vdev_islog
&&
1923 tvd
->vdev_state
== VDEV_STATE_CANT_OPEN
) {
1924 child
[idx
++] = vdev_config_generate(spa
, tvd
,
1925 B_FALSE
, VDEV_CONFIG_MISSING
);
1930 fnvlist_add_nvlist_array(nv
,
1931 ZPOOL_CONFIG_CHILDREN
, child
, idx
);
1932 fnvlist_add_nvlist(spa
->spa_load_info
,
1933 ZPOOL_CONFIG_MISSING_DEVICES
, nv
);
1935 for (uint64_t i
= 0; i
< idx
; i
++)
1936 nvlist_free(child
[i
]);
1939 kmem_free(child
, rvd
->vdev_children
* sizeof (char **));
1942 spa_load_failed(spa
, "some log devices are missing");
1943 vdev_dbgmsg_print_tree(rvd
, 2);
1944 return (SET_ERROR(ENXIO
));
1947 for (uint64_t c
= 0; c
< rvd
->vdev_children
; c
++) {
1948 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1950 if (tvd
->vdev_islog
&&
1951 tvd
->vdev_state
== VDEV_STATE_CANT_OPEN
) {
1952 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
1953 spa_load_note(spa
, "some log devices are "
1954 "missing, ZIL is dropped.");
1955 vdev_dbgmsg_print_tree(rvd
, 2);
1965 * Check for missing log devices
1968 spa_check_logs(spa_t
*spa
)
1970 boolean_t rv
= B_FALSE
;
1971 dsl_pool_t
*dp
= spa_get_dsl(spa
);
1973 switch (spa
->spa_log_state
) {
1976 case SPA_LOG_MISSING
:
1977 /* need to recheck in case slog has been restored */
1978 case SPA_LOG_UNKNOWN
:
1979 rv
= (dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
1980 zil_check_log_chain
, NULL
, DS_FIND_CHILDREN
) != 0);
1982 spa_set_log_state(spa
, SPA_LOG_MISSING
);
1989 spa_passivate_log(spa_t
*spa
)
1991 vdev_t
*rvd
= spa
->spa_root_vdev
;
1992 boolean_t slog_found
= B_FALSE
;
1994 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
1996 if (!spa_has_slogs(spa
))
1999 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
2000 vdev_t
*tvd
= rvd
->vdev_child
[c
];
2001 metaslab_group_t
*mg
= tvd
->vdev_mg
;
2003 if (tvd
->vdev_islog
) {
2004 metaslab_group_passivate(mg
);
2005 slog_found
= B_TRUE
;
2009 return (slog_found
);
2013 spa_activate_log(spa_t
*spa
)
2015 vdev_t
*rvd
= spa
->spa_root_vdev
;
2017 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
2019 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
2020 vdev_t
*tvd
= rvd
->vdev_child
[c
];
2021 metaslab_group_t
*mg
= tvd
->vdev_mg
;
2023 if (tvd
->vdev_islog
)
2024 metaslab_group_activate(mg
);
2029 spa_reset_logs(spa_t
*spa
)
2033 error
= dmu_objset_find(spa_name(spa
), zil_reset
,
2034 NULL
, DS_FIND_CHILDREN
);
2037 * We successfully offlined the log device, sync out the
2038 * current txg so that the "stubby" block can be removed
2041 txg_wait_synced(spa
->spa_dsl_pool
, 0);
2047 spa_aux_check_removed(spa_aux_vdev_t
*sav
)
2049 for (int i
= 0; i
< sav
->sav_count
; i
++)
2050 spa_check_removed(sav
->sav_vdevs
[i
]);
2054 spa_claim_notify(zio_t
*zio
)
2056 spa_t
*spa
= zio
->io_spa
;
2061 mutex_enter(&spa
->spa_props_lock
); /* any mutex will do */
2062 if (spa
->spa_claim_max_txg
< zio
->io_bp
->blk_birth
)
2063 spa
->spa_claim_max_txg
= zio
->io_bp
->blk_birth
;
2064 mutex_exit(&spa
->spa_props_lock
);
2067 typedef struct spa_load_error
{
2068 uint64_t sle_meta_count
;
2069 uint64_t sle_data_count
;
2073 spa_load_verify_done(zio_t
*zio
)
2075 blkptr_t
*bp
= zio
->io_bp
;
2076 spa_load_error_t
*sle
= zio
->io_private
;
2077 dmu_object_type_t type
= BP_GET_TYPE(bp
);
2078 int error
= zio
->io_error
;
2079 spa_t
*spa
= zio
->io_spa
;
2081 abd_free(zio
->io_abd
);
2083 if ((BP_GET_LEVEL(bp
) != 0 || DMU_OT_IS_METADATA(type
)) &&
2084 type
!= DMU_OT_INTENT_LOG
)
2085 atomic_inc_64(&sle
->sle_meta_count
);
2087 atomic_inc_64(&sle
->sle_data_count
);
2090 mutex_enter(&spa
->spa_scrub_lock
);
2091 spa
->spa_load_verify_ios
--;
2092 cv_broadcast(&spa
->spa_scrub_io_cv
);
2093 mutex_exit(&spa
->spa_scrub_lock
);
2097 * Maximum number of concurrent scrub i/os to create while verifying
2098 * a pool while importing it.
2100 int spa_load_verify_maxinflight
= 10000;
2101 int spa_load_verify_metadata
= B_TRUE
;
2102 int spa_load_verify_data
= B_TRUE
;
2106 spa_load_verify_cb(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
2107 const zbookmark_phys_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
2109 if (bp
== NULL
|| BP_IS_HOLE(bp
) || BP_IS_EMBEDDED(bp
))
2112 * Note: normally this routine will not be called if
2113 * spa_load_verify_metadata is not set. However, it may be useful
2114 * to manually set the flag after the traversal has begun.
2116 if (!spa_load_verify_metadata
)
2118 if (!BP_IS_METADATA(bp
) && !spa_load_verify_data
)
2122 size_t size
= BP_GET_PSIZE(bp
);
2124 mutex_enter(&spa
->spa_scrub_lock
);
2125 while (spa
->spa_load_verify_ios
>= spa_load_verify_maxinflight
)
2126 cv_wait(&spa
->spa_scrub_io_cv
, &spa
->spa_scrub_lock
);
2127 spa
->spa_load_verify_ios
++;
2128 mutex_exit(&spa
->spa_scrub_lock
);
2130 zio_nowait(zio_read(rio
, spa
, bp
, abd_alloc_for_io(size
, B_FALSE
), size
,
2131 spa_load_verify_done
, rio
->io_private
, ZIO_PRIORITY_SCRUB
,
2132 ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_CANFAIL
|
2133 ZIO_FLAG_SCRUB
| ZIO_FLAG_RAW
, zb
));
2139 verify_dataset_name_len(dsl_pool_t
*dp
, dsl_dataset_t
*ds
, void *arg
)
2141 if (dsl_dataset_namelen(ds
) >= ZFS_MAX_DATASET_NAME_LEN
)
2142 return (SET_ERROR(ENAMETOOLONG
));
2148 spa_load_verify(spa_t
*spa
)
2151 spa_load_error_t sle
= { 0 };
2152 zpool_load_policy_t policy
;
2153 boolean_t verify_ok
= B_FALSE
;
2156 zpool_get_load_policy(spa
->spa_config
, &policy
);
2158 if (policy
.zlp_rewind
& ZPOOL_NEVER_REWIND
)
2161 dsl_pool_config_enter(spa
->spa_dsl_pool
, FTAG
);
2162 error
= dmu_objset_find_dp(spa
->spa_dsl_pool
,
2163 spa
->spa_dsl_pool
->dp_root_dir_obj
, verify_dataset_name_len
, NULL
,
2165 dsl_pool_config_exit(spa
->spa_dsl_pool
, FTAG
);
2169 rio
= zio_root(spa
, NULL
, &sle
,
2170 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
);
2172 if (spa_load_verify_metadata
) {
2173 if (spa
->spa_extreme_rewind
) {
2174 spa_load_note(spa
, "performing a complete scan of the "
2175 "pool since extreme rewind is on. This may take "
2176 "a very long time.\n (spa_load_verify_data=%u, "
2177 "spa_load_verify_metadata=%u)",
2178 spa_load_verify_data
, spa_load_verify_metadata
);
2180 error
= traverse_pool(spa
, spa
->spa_verify_min_txg
,
2181 TRAVERSE_PRE
| TRAVERSE_PREFETCH_METADATA
|
2182 TRAVERSE_NO_DECRYPT
, spa_load_verify_cb
, rio
);
2185 (void) zio_wait(rio
);
2187 spa
->spa_load_meta_errors
= sle
.sle_meta_count
;
2188 spa
->spa_load_data_errors
= sle
.sle_data_count
;
2190 if (sle
.sle_meta_count
!= 0 || sle
.sle_data_count
!= 0) {
2191 spa_load_note(spa
, "spa_load_verify found %llu metadata errors "
2192 "and %llu data errors", (u_longlong_t
)sle
.sle_meta_count
,
2193 (u_longlong_t
)sle
.sle_data_count
);
2196 if (spa_load_verify_dryrun
||
2197 (!error
&& sle
.sle_meta_count
<= policy
.zlp_maxmeta
&&
2198 sle
.sle_data_count
<= policy
.zlp_maxdata
)) {
2202 spa
->spa_load_txg
= spa
->spa_uberblock
.ub_txg
;
2203 spa
->spa_load_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
2205 loss
= spa
->spa_last_ubsync_txg_ts
- spa
->spa_load_txg_ts
;
2206 VERIFY(nvlist_add_uint64(spa
->spa_load_info
,
2207 ZPOOL_CONFIG_LOAD_TIME
, spa
->spa_load_txg_ts
) == 0);
2208 VERIFY(nvlist_add_int64(spa
->spa_load_info
,
2209 ZPOOL_CONFIG_REWIND_TIME
, loss
) == 0);
2210 VERIFY(nvlist_add_uint64(spa
->spa_load_info
,
2211 ZPOOL_CONFIG_LOAD_DATA_ERRORS
, sle
.sle_data_count
) == 0);
2213 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
;
2216 if (spa_load_verify_dryrun
)
2220 if (error
!= ENXIO
&& error
!= EIO
)
2221 error
= SET_ERROR(EIO
);
2225 return (verify_ok
? 0 : EIO
);
2229 * Find a value in the pool props object.
2232 spa_prop_find(spa_t
*spa
, zpool_prop_t prop
, uint64_t *val
)
2234 (void) zap_lookup(spa
->spa_meta_objset
, spa
->spa_pool_props_object
,
2235 zpool_prop_to_name(prop
), sizeof (uint64_t), 1, val
);
2239 * Find a value in the pool directory object.
2242 spa_dir_prop(spa_t
*spa
, const char *name
, uint64_t *val
, boolean_t log_enoent
)
2244 int error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
2245 name
, sizeof (uint64_t), 1, val
);
2247 if (error
!= 0 && (error
!= ENOENT
|| log_enoent
)) {
2248 spa_load_failed(spa
, "couldn't get '%s' value in MOS directory "
2249 "[error=%d]", name
, error
);
2256 spa_vdev_err(vdev_t
*vdev
, vdev_aux_t aux
, int err
)
2258 vdev_set_state(vdev
, B_TRUE
, VDEV_STATE_CANT_OPEN
, aux
);
2259 return (SET_ERROR(err
));
2263 spa_spawn_aux_threads(spa_t
*spa
)
2265 ASSERT(spa_writeable(spa
));
2267 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
2269 spa_start_indirect_condensing_thread(spa
);
2271 ASSERT3P(spa
->spa_checkpoint_discard_zthr
, ==, NULL
);
2272 spa
->spa_checkpoint_discard_zthr
=
2273 zthr_create(spa_checkpoint_discard_thread_check
,
2274 spa_checkpoint_discard_thread
, spa
);
2278 * Fix up config after a partly-completed split. This is done with the
2279 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
2280 * pool have that entry in their config, but only the splitting one contains
2281 * a list of all the guids of the vdevs that are being split off.
2283 * This function determines what to do with that list: either rejoin
2284 * all the disks to the pool, or complete the splitting process. To attempt
2285 * the rejoin, each disk that is offlined is marked online again, and
2286 * we do a reopen() call. If the vdev label for every disk that was
2287 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
2288 * then we call vdev_split() on each disk, and complete the split.
2290 * Otherwise we leave the config alone, with all the vdevs in place in
2291 * the original pool.
2294 spa_try_repair(spa_t
*spa
, nvlist_t
*config
)
2301 boolean_t attempt_reopen
;
2303 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
, &nvl
) != 0)
2306 /* check that the config is complete */
2307 if (nvlist_lookup_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
2308 &glist
, &gcount
) != 0)
2311 vd
= kmem_zalloc(gcount
* sizeof (vdev_t
*), KM_SLEEP
);
2313 /* attempt to online all the vdevs & validate */
2314 attempt_reopen
= B_TRUE
;
2315 for (i
= 0; i
< gcount
; i
++) {
2316 if (glist
[i
] == 0) /* vdev is hole */
2319 vd
[i
] = spa_lookup_by_guid(spa
, glist
[i
], B_FALSE
);
2320 if (vd
[i
] == NULL
) {
2322 * Don't bother attempting to reopen the disks;
2323 * just do the split.
2325 attempt_reopen
= B_FALSE
;
2327 /* attempt to re-online it */
2328 vd
[i
]->vdev_offline
= B_FALSE
;
2332 if (attempt_reopen
) {
2333 vdev_reopen(spa
->spa_root_vdev
);
2335 /* check each device to see what state it's in */
2336 for (extracted
= 0, i
= 0; i
< gcount
; i
++) {
2337 if (vd
[i
] != NULL
&&
2338 vd
[i
]->vdev_stat
.vs_aux
!= VDEV_AUX_SPLIT_POOL
)
2345 * If every disk has been moved to the new pool, or if we never
2346 * even attempted to look at them, then we split them off for
2349 if (!attempt_reopen
|| gcount
== extracted
) {
2350 for (i
= 0; i
< gcount
; i
++)
2353 vdev_reopen(spa
->spa_root_vdev
);
2356 kmem_free(vd
, gcount
* sizeof (vdev_t
*));
2360 spa_load(spa_t
*spa
, spa_load_state_t state
, spa_import_type_t type
)
2362 char *ereport
= FM_EREPORT_ZFS_POOL
;
2365 spa
->spa_load_state
= state
;
2367 gethrestime(&spa
->spa_loaded_ts
);
2368 error
= spa_load_impl(spa
, type
, &ereport
);
2371 * Don't count references from objsets that are already closed
2372 * and are making their way through the eviction process.
2374 spa_evicting_os_wait(spa
);
2375 spa
->spa_minref
= zfs_refcount_count(&spa
->spa_refcount
);
2377 if (error
!= EEXIST
) {
2378 spa
->spa_loaded_ts
.tv_sec
= 0;
2379 spa
->spa_loaded_ts
.tv_nsec
= 0;
2381 if (error
!= EBADF
) {
2382 zfs_ereport_post(ereport
, spa
, NULL
, NULL
, NULL
, 0, 0);
2385 spa
->spa_load_state
= error
? SPA_LOAD_ERROR
: SPA_LOAD_NONE
;
2393 * Count the number of per-vdev ZAPs associated with all of the vdevs in the
2394 * vdev tree rooted in the given vd, and ensure that each ZAP is present in the
2395 * spa's per-vdev ZAP list.
2398 vdev_count_verify_zaps(vdev_t
*vd
)
2400 spa_t
*spa
= vd
->vdev_spa
;
2403 if (vd
->vdev_top_zap
!= 0) {
2405 ASSERT0(zap_lookup_int(spa
->spa_meta_objset
,
2406 spa
->spa_all_vdev_zaps
, vd
->vdev_top_zap
));
2408 if (vd
->vdev_leaf_zap
!= 0) {
2410 ASSERT0(zap_lookup_int(spa
->spa_meta_objset
,
2411 spa
->spa_all_vdev_zaps
, vd
->vdev_leaf_zap
));
2414 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
2415 total
+= vdev_count_verify_zaps(vd
->vdev_child
[i
]);
2423 * Determine whether the activity check is required.
2426 spa_activity_check_required(spa_t
*spa
, uberblock_t
*ub
, nvlist_t
*label
,
2430 uint64_t hostid
= 0;
2431 uint64_t tryconfig_txg
= 0;
2432 uint64_t tryconfig_timestamp
= 0;
2435 if (nvlist_exists(config
, ZPOOL_CONFIG_LOAD_INFO
)) {
2436 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
2437 (void) nvlist_lookup_uint64(nvinfo
, ZPOOL_CONFIG_MMP_TXG
,
2439 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_TIMESTAMP
,
2440 &tryconfig_timestamp
);
2443 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
, &state
);
2446 * Disable the MMP activity check - This is used by zdb which
2447 * is intended to be used on potentially active pools.
2449 if (spa
->spa_import_flags
& ZFS_IMPORT_SKIP_MMP
)
2453 * Skip the activity check when the MMP feature is disabled.
2455 if (ub
->ub_mmp_magic
== MMP_MAGIC
&& ub
->ub_mmp_delay
== 0)
2458 * If the tryconfig_* values are nonzero, they are the results of an
2459 * earlier tryimport. If they match the uberblock we just found, then
2460 * the pool has not changed and we return false so we do not test a
2463 if (tryconfig_txg
&& tryconfig_txg
== ub
->ub_txg
&&
2464 tryconfig_timestamp
&& tryconfig_timestamp
== ub
->ub_timestamp
)
2468 * Allow the activity check to be skipped when importing the pool
2469 * on the same host which last imported it. Since the hostid from
2470 * configuration may be stale use the one read from the label.
2472 if (nvlist_exists(label
, ZPOOL_CONFIG_HOSTID
))
2473 hostid
= fnvlist_lookup_uint64(label
, ZPOOL_CONFIG_HOSTID
);
2475 if (hostid
== spa_get_hostid())
2479 * Skip the activity test when the pool was cleanly exported.
2481 if (state
!= POOL_STATE_ACTIVE
)
2488 * Perform the import activity check. If the user canceled the import or
2489 * we detected activity then fail.
2492 spa_activity_check(spa_t
*spa
, uberblock_t
*ub
, nvlist_t
*config
)
2494 uint64_t import_intervals
= MAX(zfs_multihost_import_intervals
, 1);
2495 uint64_t txg
= ub
->ub_txg
;
2496 uint64_t timestamp
= ub
->ub_timestamp
;
2497 uint64_t import_delay
= NANOSEC
;
2498 hrtime_t import_expire
;
2499 nvlist_t
*mmp_label
= NULL
;
2500 vdev_t
*rvd
= spa
->spa_root_vdev
;
2505 cv_init(&cv
, NULL
, CV_DEFAULT
, NULL
);
2506 mutex_init(&mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
2510 * If ZPOOL_CONFIG_MMP_TXG is present an activity check was performed
2511 * during the earlier tryimport. If the txg recorded there is 0 then
2512 * the pool is known to be active on another host.
2514 * Otherwise, the pool might be in use on another node. Check for
2515 * changes in the uberblocks on disk if necessary.
2517 if (nvlist_exists(config
, ZPOOL_CONFIG_LOAD_INFO
)) {
2518 nvlist_t
*nvinfo
= fnvlist_lookup_nvlist(config
,
2519 ZPOOL_CONFIG_LOAD_INFO
);
2521 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_TXG
) &&
2522 fnvlist_lookup_uint64(nvinfo
, ZPOOL_CONFIG_MMP_TXG
) == 0) {
2523 vdev_uberblock_load(rvd
, ub
, &mmp_label
);
2524 error
= SET_ERROR(EREMOTEIO
);
2530 * Preferentially use the zfs_multihost_interval from the node which
2531 * last imported the pool. This value is stored in an MMP uberblock as.
2533 * ub_mmp_delay * vdev_count_leaves() == zfs_multihost_interval
2535 if (ub
->ub_mmp_magic
== MMP_MAGIC
&& ub
->ub_mmp_delay
)
2536 import_delay
= MAX(import_delay
, import_intervals
*
2537 ub
->ub_mmp_delay
* MAX(vdev_count_leaves(spa
), 1));
2539 /* Apply a floor using the local default values. */
2540 import_delay
= MAX(import_delay
, import_intervals
*
2541 MSEC2NSEC(MAX(zfs_multihost_interval
, MMP_MIN_INTERVAL
)));
2543 zfs_dbgmsg("import_delay=%llu ub_mmp_delay=%llu import_intervals=%u "
2544 "leaves=%u", import_delay
, ub
->ub_mmp_delay
, import_intervals
,
2545 vdev_count_leaves(spa
));
2547 /* Add a small random factor in case of simultaneous imports (0-25%) */
2548 import_expire
= gethrtime() + import_delay
+
2549 (import_delay
* spa_get_random(250) / 1000);
2551 while (gethrtime() < import_expire
) {
2552 vdev_uberblock_load(rvd
, ub
, &mmp_label
);
2554 if (txg
!= ub
->ub_txg
|| timestamp
!= ub
->ub_timestamp
) {
2555 error
= SET_ERROR(EREMOTEIO
);
2560 nvlist_free(mmp_label
);
2564 error
= cv_timedwait_sig(&cv
, &mtx
, ddi_get_lbolt() + hz
);
2566 error
= SET_ERROR(EINTR
);
2574 mutex_destroy(&mtx
);
2578 * If the pool is determined to be active store the status in the
2579 * spa->spa_load_info nvlist. If the remote hostname or hostid are
2580 * available from configuration read from disk store them as well.
2581 * This allows 'zpool import' to generate a more useful message.
2583 * ZPOOL_CONFIG_MMP_STATE - observed pool status (mandatory)
2584 * ZPOOL_CONFIG_MMP_HOSTNAME - hostname from the active pool
2585 * ZPOOL_CONFIG_MMP_HOSTID - hostid from the active pool
2587 if (error
== EREMOTEIO
) {
2588 char *hostname
= "<unknown>";
2589 uint64_t hostid
= 0;
2592 if (nvlist_exists(mmp_label
, ZPOOL_CONFIG_HOSTNAME
)) {
2593 hostname
= fnvlist_lookup_string(mmp_label
,
2594 ZPOOL_CONFIG_HOSTNAME
);
2595 fnvlist_add_string(spa
->spa_load_info
,
2596 ZPOOL_CONFIG_MMP_HOSTNAME
, hostname
);
2599 if (nvlist_exists(mmp_label
, ZPOOL_CONFIG_HOSTID
)) {
2600 hostid
= fnvlist_lookup_uint64(mmp_label
,
2601 ZPOOL_CONFIG_HOSTID
);
2602 fnvlist_add_uint64(spa
->spa_load_info
,
2603 ZPOOL_CONFIG_MMP_HOSTID
, hostid
);
2607 fnvlist_add_uint64(spa
->spa_load_info
,
2608 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_ACTIVE
);
2609 fnvlist_add_uint64(spa
->spa_load_info
,
2610 ZPOOL_CONFIG_MMP_TXG
, 0);
2612 error
= spa_vdev_err(rvd
, VDEV_AUX_ACTIVE
, EREMOTEIO
);
2616 nvlist_free(mmp_label
);
2622 spa_verify_host(spa_t
*spa
, nvlist_t
*mos_config
)
2626 uint64_t myhostid
= 0;
2628 if (!spa_is_root(spa
) && nvlist_lookup_uint64(mos_config
,
2629 ZPOOL_CONFIG_HOSTID
, &hostid
) == 0) {
2630 hostname
= fnvlist_lookup_string(mos_config
,
2631 ZPOOL_CONFIG_HOSTNAME
);
2633 myhostid
= zone_get_hostid(NULL
);
2635 if (hostid
!= 0 && myhostid
!= 0 && hostid
!= myhostid
) {
2636 cmn_err(CE_WARN
, "pool '%s' could not be "
2637 "loaded as it was last accessed by "
2638 "another system (host: %s hostid: 0x%llx). "
2639 "See: http://illumos.org/msg/ZFS-8000-EY",
2640 spa_name(spa
), hostname
, (u_longlong_t
)hostid
);
2641 spa_load_failed(spa
, "hostid verification failed: pool "
2642 "last accessed by host: %s (hostid: 0x%llx)",
2643 hostname
, (u_longlong_t
)hostid
);
2644 return (SET_ERROR(EBADF
));
2652 spa_ld_parse_config(spa_t
*spa
, spa_import_type_t type
)
2655 nvlist_t
*nvtree
, *nvl
, *config
= spa
->spa_config
;
2662 * Versioning wasn't explicitly added to the label until later, so if
2663 * it's not present treat it as the initial version.
2665 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
2666 &spa
->spa_ubsync
.ub_version
) != 0)
2667 spa
->spa_ubsync
.ub_version
= SPA_VERSION_INITIAL
;
2669 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
, &pool_guid
)) {
2670 spa_load_failed(spa
, "invalid config provided: '%s' missing",
2671 ZPOOL_CONFIG_POOL_GUID
);
2672 return (SET_ERROR(EINVAL
));
2676 * If we are doing an import, ensure that the pool is not already
2677 * imported by checking if its pool guid already exists in the
2680 * The only case that we allow an already imported pool to be
2681 * imported again, is when the pool is checkpointed and we want to
2682 * look at its checkpointed state from userland tools like zdb.
2685 if ((spa
->spa_load_state
== SPA_LOAD_IMPORT
||
2686 spa
->spa_load_state
== SPA_LOAD_TRYIMPORT
) &&
2687 spa_guid_exists(pool_guid
, 0)) {
2689 if ((spa
->spa_load_state
== SPA_LOAD_IMPORT
||
2690 spa
->spa_load_state
== SPA_LOAD_TRYIMPORT
) &&
2691 spa_guid_exists(pool_guid
, 0) &&
2692 !spa_importing_readonly_checkpoint(spa
)) {
2694 spa_load_failed(spa
, "a pool with guid %llu is already open",
2695 (u_longlong_t
)pool_guid
);
2696 return (SET_ERROR(EEXIST
));
2699 spa
->spa_config_guid
= pool_guid
;
2701 nvlist_free(spa
->spa_load_info
);
2702 spa
->spa_load_info
= fnvlist_alloc();
2704 ASSERT(spa
->spa_comment
== NULL
);
2705 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
2706 spa
->spa_comment
= spa_strdup(comment
);
2708 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
2709 &spa
->spa_config_txg
);
2711 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
, &nvl
) == 0)
2712 spa
->spa_config_splitting
= fnvlist_dup(nvl
);
2714 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvtree
)) {
2715 spa_load_failed(spa
, "invalid config provided: '%s' missing",
2716 ZPOOL_CONFIG_VDEV_TREE
);
2717 return (SET_ERROR(EINVAL
));
2721 * Create "The Godfather" zio to hold all async IOs
2723 spa
->spa_async_zio_root
= kmem_alloc(max_ncpus
* sizeof (void *),
2725 for (int i
= 0; i
< max_ncpus
; i
++) {
2726 spa
->spa_async_zio_root
[i
] = zio_root(spa
, NULL
, NULL
,
2727 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
2728 ZIO_FLAG_GODFATHER
);
2732 * Parse the configuration into a vdev tree. We explicitly set the
2733 * value that will be returned by spa_version() since parsing the
2734 * configuration requires knowing the version number.
2736 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2737 parse
= (type
== SPA_IMPORT_EXISTING
?
2738 VDEV_ALLOC_LOAD
: VDEV_ALLOC_SPLIT
);
2739 error
= spa_config_parse(spa
, &rvd
, nvtree
, NULL
, 0, parse
);
2740 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2743 spa_load_failed(spa
, "unable to parse config [error=%d]",
2748 ASSERT(spa
->spa_root_vdev
== rvd
);
2749 ASSERT3U(spa
->spa_min_ashift
, >=, SPA_MINBLOCKSHIFT
);
2750 ASSERT3U(spa
->spa_max_ashift
, <=, SPA_MAXBLOCKSHIFT
);
2752 if (type
!= SPA_IMPORT_ASSEMBLE
) {
2753 ASSERT(spa_guid(spa
) == pool_guid
);
2760 * Recursively open all vdevs in the vdev tree. This function is called twice:
2761 * first with the untrusted config, then with the trusted config.
2764 spa_ld_open_vdevs(spa_t
*spa
)
2769 * spa_missing_tvds_allowed defines how many top-level vdevs can be
2770 * missing/unopenable for the root vdev to be still considered openable.
2772 if (spa
->spa_trust_config
) {
2773 spa
->spa_missing_tvds_allowed
= zfs_max_missing_tvds
;
2774 } else if (spa
->spa_config_source
== SPA_CONFIG_SRC_CACHEFILE
) {
2775 spa
->spa_missing_tvds_allowed
= zfs_max_missing_tvds_cachefile
;
2776 } else if (spa
->spa_config_source
== SPA_CONFIG_SRC_SCAN
) {
2777 spa
->spa_missing_tvds_allowed
= zfs_max_missing_tvds_scan
;
2779 spa
->spa_missing_tvds_allowed
= 0;
2782 spa
->spa_missing_tvds_allowed
=
2783 MAX(zfs_max_missing_tvds
, spa
->spa_missing_tvds_allowed
);
2785 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2786 error
= vdev_open(spa
->spa_root_vdev
);
2787 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2789 if (spa
->spa_missing_tvds
!= 0) {
2790 spa_load_note(spa
, "vdev tree has %lld missing top-level "
2791 "vdevs.", (u_longlong_t
)spa
->spa_missing_tvds
);
2792 if (spa
->spa_trust_config
&& (spa
->spa_mode
& FWRITE
)) {
2794 * Although theoretically we could allow users to open
2795 * incomplete pools in RW mode, we'd need to add a lot
2796 * of extra logic (e.g. adjust pool space to account
2797 * for missing vdevs).
2798 * This limitation also prevents users from accidentally
2799 * opening the pool in RW mode during data recovery and
2800 * damaging it further.
2802 spa_load_note(spa
, "pools with missing top-level "
2803 "vdevs can only be opened in read-only mode.");
2804 error
= SET_ERROR(ENXIO
);
2806 spa_load_note(spa
, "current settings allow for maximum "
2807 "%lld missing top-level vdevs at this stage.",
2808 (u_longlong_t
)spa
->spa_missing_tvds_allowed
);
2812 spa_load_failed(spa
, "unable to open vdev tree [error=%d]",
2815 if (spa
->spa_missing_tvds
!= 0 || error
!= 0)
2816 vdev_dbgmsg_print_tree(spa
->spa_root_vdev
, 2);
2822 * We need to validate the vdev labels against the configuration that
2823 * we have in hand. This function is called twice: first with an untrusted
2824 * config, then with a trusted config. The validation is more strict when the
2825 * config is trusted.
2828 spa_ld_validate_vdevs(spa_t
*spa
)
2831 vdev_t
*rvd
= spa
->spa_root_vdev
;
2833 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2834 error
= vdev_validate(rvd
);
2835 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2838 spa_load_failed(spa
, "vdev_validate failed [error=%d]", error
);
2842 if (rvd
->vdev_state
<= VDEV_STATE_CANT_OPEN
) {
2843 spa_load_failed(spa
, "cannot open vdev tree after invalidating "
2845 vdev_dbgmsg_print_tree(rvd
, 2);
2846 return (SET_ERROR(ENXIO
));
2853 spa_ld_select_uberblock_done(spa_t
*spa
, uberblock_t
*ub
)
2855 spa
->spa_state
= POOL_STATE_ACTIVE
;
2856 spa
->spa_ubsync
= spa
->spa_uberblock
;
2857 spa
->spa_verify_min_txg
= spa
->spa_extreme_rewind
?
2858 TXG_INITIAL
- 1 : spa_last_synced_txg(spa
) - TXG_DEFER_SIZE
- 1;
2859 spa
->spa_first_txg
= spa
->spa_last_ubsync_txg
?
2860 spa
->spa_last_ubsync_txg
: spa_last_synced_txg(spa
) + 1;
2861 spa
->spa_claim_max_txg
= spa
->spa_first_txg
;
2862 spa
->spa_prev_software_version
= ub
->ub_software_version
;
2866 spa_ld_select_uberblock(spa_t
*spa
, spa_import_type_t type
)
2868 vdev_t
*rvd
= spa
->spa_root_vdev
;
2870 uberblock_t
*ub
= &spa
->spa_uberblock
;
2871 boolean_t activity_check
= B_FALSE
;
2874 * If we are opening the checkpointed state of the pool by
2875 * rewinding to it, at this point we will have written the
2876 * checkpointed uberblock to the vdev labels, so searching
2877 * the labels will find the right uberblock. However, if
2878 * we are opening the checkpointed state read-only, we have
2879 * not modified the labels. Therefore, we must ignore the
2880 * labels and continue using the spa_uberblock that was set
2881 * by spa_ld_checkpoint_rewind.
2883 * Note that it would be fine to ignore the labels when
2884 * rewinding (opening writeable) as well. However, if we
2885 * crash just after writing the labels, we will end up
2886 * searching the labels. Doing so in the common case means
2887 * that this code path gets exercised normally, rather than
2888 * just in the edge case.
2890 if (ub
->ub_checkpoint_txg
!= 0 &&
2891 spa_importing_readonly_checkpoint(spa
)) {
2892 spa_ld_select_uberblock_done(spa
, ub
);
2897 * Find the best uberblock.
2899 vdev_uberblock_load(rvd
, ub
, &label
);
2902 * If we weren't able to find a single valid uberblock, return failure.
2904 if (ub
->ub_txg
== 0) {
2906 spa_load_failed(spa
, "no valid uberblock found");
2907 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, ENXIO
));
2910 spa_load_note(spa
, "using uberblock with txg=%llu",
2911 (u_longlong_t
)ub
->ub_txg
);
2915 * For pools which have the multihost property on determine if the
2916 * pool is truly inactive and can be safely imported. Prevent
2917 * hosts which don't have a hostid set from importing the pool.
2919 activity_check
= spa_activity_check_required(spa
, ub
, label
,
2921 if (activity_check
) {
2922 if (ub
->ub_mmp_magic
== MMP_MAGIC
&& ub
->ub_mmp_delay
&&
2923 spa_get_hostid() == 0) {
2925 fnvlist_add_uint64(spa
->spa_load_info
,
2926 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_NO_HOSTID
);
2927 return (spa_vdev_err(rvd
, VDEV_AUX_ACTIVE
, EREMOTEIO
));
2930 int error
= spa_activity_check(spa
, ub
, spa
->spa_config
);
2936 fnvlist_add_uint64(spa
->spa_load_info
,
2937 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_INACTIVE
);
2938 fnvlist_add_uint64(spa
->spa_load_info
,
2939 ZPOOL_CONFIG_MMP_TXG
, ub
->ub_txg
);
2943 * If the pool has an unsupported version we can't open it.
2945 if (!SPA_VERSION_IS_SUPPORTED(ub
->ub_version
)) {
2947 spa_load_failed(spa
, "version %llu is not supported",
2948 (u_longlong_t
)ub
->ub_version
);
2949 return (spa_vdev_err(rvd
, VDEV_AUX_VERSION_NEWER
, ENOTSUP
));
2952 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
2956 * If we weren't able to find what's necessary for reading the
2957 * MOS in the label, return failure.
2959 if (label
== NULL
) {
2960 spa_load_failed(spa
, "label config unavailable");
2961 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
2965 if (nvlist_lookup_nvlist(label
, ZPOOL_CONFIG_FEATURES_FOR_READ
,
2968 spa_load_failed(spa
, "invalid label: '%s' missing",
2969 ZPOOL_CONFIG_FEATURES_FOR_READ
);
2970 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
2975 * Update our in-core representation with the definitive values
2978 nvlist_free(spa
->spa_label_features
);
2979 VERIFY(nvlist_dup(features
, &spa
->spa_label_features
, 0) == 0);
2985 * Look through entries in the label nvlist's features_for_read. If
2986 * there is a feature listed there which we don't understand then we
2987 * cannot open a pool.
2989 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
2990 nvlist_t
*unsup_feat
;
2992 VERIFY(nvlist_alloc(&unsup_feat
, NV_UNIQUE_NAME
, KM_SLEEP
) ==
2995 for (nvpair_t
*nvp
= nvlist_next_nvpair(spa
->spa_label_features
,
2997 nvp
= nvlist_next_nvpair(spa
->spa_label_features
, nvp
)) {
2998 if (!zfeature_is_supported(nvpair_name(nvp
))) {
2999 VERIFY(nvlist_add_string(unsup_feat
,
3000 nvpair_name(nvp
), "") == 0);
3004 if (!nvlist_empty(unsup_feat
)) {
3005 VERIFY(nvlist_add_nvlist(spa
->spa_load_info
,
3006 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
) == 0);
3007 nvlist_free(unsup_feat
);
3008 spa_load_failed(spa
, "some features are unsupported");
3009 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
3013 nvlist_free(unsup_feat
);
3016 if (type
!= SPA_IMPORT_ASSEMBLE
&& spa
->spa_config_splitting
) {
3017 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3018 spa_try_repair(spa
, spa
->spa_config
);
3019 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3020 nvlist_free(spa
->spa_config_splitting
);
3021 spa
->spa_config_splitting
= NULL
;
3025 * Initialize internal SPA structures.
3027 spa_ld_select_uberblock_done(spa
, ub
);
3033 spa_ld_open_rootbp(spa_t
*spa
)
3036 vdev_t
*rvd
= spa
->spa_root_vdev
;
3038 error
= dsl_pool_init(spa
, spa
->spa_first_txg
, &spa
->spa_dsl_pool
);
3040 spa_load_failed(spa
, "unable to open rootbp in dsl_pool_init "
3041 "[error=%d]", error
);
3042 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3044 spa
->spa_meta_objset
= spa
->spa_dsl_pool
->dp_meta_objset
;
3050 spa_ld_trusted_config(spa_t
*spa
, spa_import_type_t type
,
3051 boolean_t reloading
)
3053 vdev_t
*mrvd
, *rvd
= spa
->spa_root_vdev
;
3054 nvlist_t
*nv
, *mos_config
, *policy
;
3055 int error
= 0, copy_error
;
3056 uint64_t healthy_tvds
, healthy_tvds_mos
;
3057 uint64_t mos_config_txg
;
3059 if (spa_dir_prop(spa
, DMU_POOL_CONFIG
, &spa
->spa_config_object
, B_TRUE
)
3061 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3064 * If we're assembling a pool from a split, the config provided is
3065 * already trusted so there is nothing to do.
3067 if (type
== SPA_IMPORT_ASSEMBLE
)
3070 healthy_tvds
= spa_healthy_core_tvds(spa
);
3072 if (load_nvlist(spa
, spa
->spa_config_object
, &mos_config
)
3074 spa_load_failed(spa
, "unable to retrieve MOS config");
3075 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3079 * If we are doing an open, pool owner wasn't verified yet, thus do
3080 * the verification here.
3082 if (spa
->spa_load_state
== SPA_LOAD_OPEN
) {
3083 error
= spa_verify_host(spa
, mos_config
);
3085 nvlist_free(mos_config
);
3090 nv
= fnvlist_lookup_nvlist(mos_config
, ZPOOL_CONFIG_VDEV_TREE
);
3092 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3095 * Build a new vdev tree from the trusted config
3097 VERIFY(spa_config_parse(spa
, &mrvd
, nv
, NULL
, 0, VDEV_ALLOC_LOAD
) == 0);
3100 * Vdev paths in the MOS may be obsolete. If the untrusted config was
3101 * obtained by scanning /dev/dsk, then it will have the right vdev
3102 * paths. We update the trusted MOS config with this information.
3103 * We first try to copy the paths with vdev_copy_path_strict, which
3104 * succeeds only when both configs have exactly the same vdev tree.
3105 * If that fails, we fall back to a more flexible method that has a
3106 * best effort policy.
3108 copy_error
= vdev_copy_path_strict(rvd
, mrvd
);
3109 if (copy_error
!= 0 || spa_load_print_vdev_tree
) {
3110 spa_load_note(spa
, "provided vdev tree:");
3111 vdev_dbgmsg_print_tree(rvd
, 2);
3112 spa_load_note(spa
, "MOS vdev tree:");
3113 vdev_dbgmsg_print_tree(mrvd
, 2);
3115 if (copy_error
!= 0) {
3116 spa_load_note(spa
, "vdev_copy_path_strict failed, falling "
3117 "back to vdev_copy_path_relaxed");
3118 vdev_copy_path_relaxed(rvd
, mrvd
);
3123 spa
->spa_root_vdev
= mrvd
;
3125 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3128 * We will use spa_config if we decide to reload the spa or if spa_load
3129 * fails and we rewind. We must thus regenerate the config using the
3130 * MOS information with the updated paths. ZPOOL_LOAD_POLICY is used to
3131 * pass settings on how to load the pool and is not stored in the MOS.
3132 * We copy it over to our new, trusted config.
3134 mos_config_txg
= fnvlist_lookup_uint64(mos_config
,
3135 ZPOOL_CONFIG_POOL_TXG
);
3136 nvlist_free(mos_config
);
3137 mos_config
= spa_config_generate(spa
, NULL
, mos_config_txg
, B_FALSE
);
3138 if (nvlist_lookup_nvlist(spa
->spa_config
, ZPOOL_LOAD_POLICY
,
3140 fnvlist_add_nvlist(mos_config
, ZPOOL_LOAD_POLICY
, policy
);
3141 spa_config_set(spa
, mos_config
);
3142 spa
->spa_config_source
= SPA_CONFIG_SRC_MOS
;
3145 * Now that we got the config from the MOS, we should be more strict
3146 * in checking blkptrs and can make assumptions about the consistency
3147 * of the vdev tree. spa_trust_config must be set to true before opening
3148 * vdevs in order for them to be writeable.
3150 spa
->spa_trust_config
= B_TRUE
;
3153 * Open and validate the new vdev tree
3155 error
= spa_ld_open_vdevs(spa
);
3159 error
= spa_ld_validate_vdevs(spa
);
3163 if (copy_error
!= 0 || spa_load_print_vdev_tree
) {
3164 spa_load_note(spa
, "final vdev tree:");
3165 vdev_dbgmsg_print_tree(rvd
, 2);
3168 if (spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
&&
3169 !spa
->spa_extreme_rewind
&& zfs_max_missing_tvds
== 0) {
3171 * Sanity check to make sure that we are indeed loading the
3172 * latest uberblock. If we missed SPA_SYNC_MIN_VDEVS tvds
3173 * in the config provided and they happened to be the only ones
3174 * to have the latest uberblock, we could involuntarily perform
3175 * an extreme rewind.
3177 healthy_tvds_mos
= spa_healthy_core_tvds(spa
);
3178 if (healthy_tvds_mos
- healthy_tvds
>=
3179 SPA_SYNC_MIN_VDEVS
) {
3180 spa_load_note(spa
, "config provided misses too many "
3181 "top-level vdevs compared to MOS (%lld vs %lld). ",
3182 (u_longlong_t
)healthy_tvds
,
3183 (u_longlong_t
)healthy_tvds_mos
);
3184 spa_load_note(spa
, "vdev tree:");
3185 vdev_dbgmsg_print_tree(rvd
, 2);
3187 spa_load_failed(spa
, "config was already "
3188 "provided from MOS. Aborting.");
3189 return (spa_vdev_err(rvd
,
3190 VDEV_AUX_CORRUPT_DATA
, EIO
));
3192 spa_load_note(spa
, "spa must be reloaded using MOS "
3194 return (SET_ERROR(EAGAIN
));
3198 error
= spa_check_for_missing_logs(spa
);
3200 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
, ENXIO
));
3202 if (rvd
->vdev_guid_sum
!= spa
->spa_uberblock
.ub_guid_sum
) {
3203 spa_load_failed(spa
, "uberblock guid sum doesn't match MOS "
3204 "guid sum (%llu != %llu)",
3205 (u_longlong_t
)spa
->spa_uberblock
.ub_guid_sum
,
3206 (u_longlong_t
)rvd
->vdev_guid_sum
);
3207 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
,
3215 spa_ld_open_indirect_vdev_metadata(spa_t
*spa
)
3218 vdev_t
*rvd
= spa
->spa_root_vdev
;
3221 * Everything that we read before spa_remove_init() must be stored
3222 * on concreted vdevs. Therefore we do this as early as possible.
3224 error
= spa_remove_init(spa
);
3226 spa_load_failed(spa
, "spa_remove_init failed [error=%d]",
3228 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3232 * Retrieve information needed to condense indirect vdev mappings.
3234 error
= spa_condense_init(spa
);
3236 spa_load_failed(spa
, "spa_condense_init failed [error=%d]",
3238 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, error
));
3245 spa_ld_check_features(spa_t
*spa
, boolean_t
*missing_feat_writep
)
3248 vdev_t
*rvd
= spa
->spa_root_vdev
;
3250 if (spa_version(spa
) >= SPA_VERSION_FEATURES
) {
3251 boolean_t missing_feat_read
= B_FALSE
;
3252 nvlist_t
*unsup_feat
, *enabled_feat
;
3254 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_READ
,
3255 &spa
->spa_feat_for_read_obj
, B_TRUE
) != 0) {
3256 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3259 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_WRITE
,
3260 &spa
->spa_feat_for_write_obj
, B_TRUE
) != 0) {
3261 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3264 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_DESCRIPTIONS
,
3265 &spa
->spa_feat_desc_obj
, B_TRUE
) != 0) {
3266 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3269 enabled_feat
= fnvlist_alloc();
3270 unsup_feat
= fnvlist_alloc();
3272 if (!spa_features_check(spa
, B_FALSE
,
3273 unsup_feat
, enabled_feat
))
3274 missing_feat_read
= B_TRUE
;
3276 if (spa_writeable(spa
) ||
3277 spa
->spa_load_state
== SPA_LOAD_TRYIMPORT
) {
3278 if (!spa_features_check(spa
, B_TRUE
,
3279 unsup_feat
, enabled_feat
)) {
3280 *missing_feat_writep
= B_TRUE
;
3284 fnvlist_add_nvlist(spa
->spa_load_info
,
3285 ZPOOL_CONFIG_ENABLED_FEAT
, enabled_feat
);
3287 if (!nvlist_empty(unsup_feat
)) {
3288 fnvlist_add_nvlist(spa
->spa_load_info
,
3289 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
);
3292 fnvlist_free(enabled_feat
);
3293 fnvlist_free(unsup_feat
);
3295 if (!missing_feat_read
) {
3296 fnvlist_add_boolean(spa
->spa_load_info
,
3297 ZPOOL_CONFIG_CAN_RDONLY
);
3301 * If the state is SPA_LOAD_TRYIMPORT, our objective is
3302 * twofold: to determine whether the pool is available for
3303 * import in read-write mode and (if it is not) whether the
3304 * pool is available for import in read-only mode. If the pool
3305 * is available for import in read-write mode, it is displayed
3306 * as available in userland; if it is not available for import
3307 * in read-only mode, it is displayed as unavailable in
3308 * userland. If the pool is available for import in read-only
3309 * mode but not read-write mode, it is displayed as unavailable
3310 * in userland with a special note that the pool is actually
3311 * available for open in read-only mode.
3313 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
3314 * missing a feature for write, we must first determine whether
3315 * the pool can be opened read-only before returning to
3316 * userland in order to know whether to display the
3317 * abovementioned note.
3319 if (missing_feat_read
|| (*missing_feat_writep
&&
3320 spa_writeable(spa
))) {
3321 spa_load_failed(spa
, "pool uses unsupported features");
3322 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
3327 * Load refcounts for ZFS features from disk into an in-memory
3328 * cache during SPA initialization.
3330 for (spa_feature_t i
= 0; i
< SPA_FEATURES
; i
++) {
3333 error
= feature_get_refcount_from_disk(spa
,
3334 &spa_feature_table
[i
], &refcount
);
3336 spa
->spa_feat_refcount_cache
[i
] = refcount
;
3337 } else if (error
== ENOTSUP
) {
3338 spa
->spa_feat_refcount_cache
[i
] =
3339 SPA_FEATURE_DISABLED
;
3341 spa_load_failed(spa
, "error getting refcount "
3342 "for feature %s [error=%d]",
3343 spa_feature_table
[i
].fi_guid
, error
);
3344 return (spa_vdev_err(rvd
,
3345 VDEV_AUX_CORRUPT_DATA
, EIO
));
3350 if (spa_feature_is_active(spa
, SPA_FEATURE_ENABLED_TXG
)) {
3351 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_ENABLED_TXG
,
3352 &spa
->spa_feat_enabled_txg_obj
, B_TRUE
) != 0)
3353 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3360 spa_ld_load_special_directories(spa_t
*spa
)
3363 vdev_t
*rvd
= spa
->spa_root_vdev
;
3365 spa
->spa_is_initializing
= B_TRUE
;
3366 error
= dsl_pool_open(spa
->spa_dsl_pool
);
3367 spa
->spa_is_initializing
= B_FALSE
;
3369 spa_load_failed(spa
, "dsl_pool_open failed [error=%d]", error
);
3370 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3377 spa_ld_get_props(spa_t
*spa
)
3381 vdev_t
*rvd
= spa
->spa_root_vdev
;
3383 /* Grab the checksum salt from the MOS. */
3384 error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
3385 DMU_POOL_CHECKSUM_SALT
, 1,
3386 sizeof (spa
->spa_cksum_salt
.zcs_bytes
),
3387 spa
->spa_cksum_salt
.zcs_bytes
);
3388 if (error
== ENOENT
) {
3389 /* Generate a new salt for subsequent use */
3390 (void) random_get_pseudo_bytes(spa
->spa_cksum_salt
.zcs_bytes
,
3391 sizeof (spa
->spa_cksum_salt
.zcs_bytes
));
3392 } else if (error
!= 0) {
3393 spa_load_failed(spa
, "unable to retrieve checksum salt from "
3394 "MOS [error=%d]", error
);
3395 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3398 if (spa_dir_prop(spa
, DMU_POOL_SYNC_BPOBJ
, &obj
, B_TRUE
) != 0)
3399 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3400 error
= bpobj_open(&spa
->spa_deferred_bpobj
, spa
->spa_meta_objset
, obj
);
3402 spa_load_failed(spa
, "error opening deferred-frees bpobj "
3403 "[error=%d]", error
);
3404 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3408 * Load the bit that tells us to use the new accounting function
3409 * (raid-z deflation). If we have an older pool, this will not
3412 error
= spa_dir_prop(spa
, DMU_POOL_DEFLATE
, &spa
->spa_deflate
, B_FALSE
);
3413 if (error
!= 0 && error
!= ENOENT
)
3414 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3416 error
= spa_dir_prop(spa
, DMU_POOL_CREATION_VERSION
,
3417 &spa
->spa_creation_version
, B_FALSE
);
3418 if (error
!= 0 && error
!= ENOENT
)
3419 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3422 * Load the persistent error log. If we have an older pool, this will
3425 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_LAST
, &spa
->spa_errlog_last
,
3427 if (error
!= 0 && error
!= ENOENT
)
3428 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3430 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_SCRUB
,
3431 &spa
->spa_errlog_scrub
, B_FALSE
);
3432 if (error
!= 0 && error
!= ENOENT
)
3433 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3436 * Load the history object. If we have an older pool, this
3437 * will not be present.
3439 error
= spa_dir_prop(spa
, DMU_POOL_HISTORY
, &spa
->spa_history
, B_FALSE
);
3440 if (error
!= 0 && error
!= ENOENT
)
3441 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3444 * Load the per-vdev ZAP map. If we have an older pool, this will not
3445 * be present; in this case, defer its creation to a later time to
3446 * avoid dirtying the MOS this early / out of sync context. See
3447 * spa_sync_config_object.
3450 /* The sentinel is only available in the MOS config. */
3451 nvlist_t
*mos_config
;
3452 if (load_nvlist(spa
, spa
->spa_config_object
, &mos_config
) != 0) {
3453 spa_load_failed(spa
, "unable to retrieve MOS config");
3454 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3457 error
= spa_dir_prop(spa
, DMU_POOL_VDEV_ZAP_MAP
,
3458 &spa
->spa_all_vdev_zaps
, B_FALSE
);
3460 if (error
== ENOENT
) {
3461 VERIFY(!nvlist_exists(mos_config
,
3462 ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
));
3463 spa
->spa_avz_action
= AVZ_ACTION_INITIALIZE
;
3464 ASSERT0(vdev_count_verify_zaps(spa
->spa_root_vdev
));
3465 } else if (error
!= 0) {
3466 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3467 } else if (!nvlist_exists(mos_config
, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
)) {
3469 * An older version of ZFS overwrote the sentinel value, so
3470 * we have orphaned per-vdev ZAPs in the MOS. Defer their
3471 * destruction to later; see spa_sync_config_object.
3473 spa
->spa_avz_action
= AVZ_ACTION_DESTROY
;
3475 * We're assuming that no vdevs have had their ZAPs created
3476 * before this. Better be sure of it.
3478 ASSERT0(vdev_count_verify_zaps(spa
->spa_root_vdev
));
3480 nvlist_free(mos_config
);
3482 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
3484 error
= spa_dir_prop(spa
, DMU_POOL_PROPS
, &spa
->spa_pool_props_object
,
3486 if (error
&& error
!= ENOENT
)
3487 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3490 uint64_t autoreplace
;
3492 spa_prop_find(spa
, ZPOOL_PROP_BOOTFS
, &spa
->spa_bootfs
);
3493 spa_prop_find(spa
, ZPOOL_PROP_AUTOREPLACE
, &autoreplace
);
3494 spa_prop_find(spa
, ZPOOL_PROP_DELEGATION
, &spa
->spa_delegation
);
3495 spa_prop_find(spa
, ZPOOL_PROP_FAILUREMODE
, &spa
->spa_failmode
);
3496 spa_prop_find(spa
, ZPOOL_PROP_AUTOEXPAND
, &spa
->spa_autoexpand
);
3497 spa_prop_find(spa
, ZPOOL_PROP_MULTIHOST
, &spa
->spa_multihost
);
3498 spa_prop_find(spa
, ZPOOL_PROP_DEDUPDITTO
,
3499 &spa
->spa_dedup_ditto
);
3501 spa
->spa_autoreplace
= (autoreplace
!= 0);
3505 * If we are importing a pool with missing top-level vdevs,
3506 * we enforce that the pool doesn't panic or get suspended on
3507 * error since the likelihood of missing data is extremely high.
3509 if (spa
->spa_missing_tvds
> 0 &&
3510 spa
->spa_failmode
!= ZIO_FAILURE_MODE_CONTINUE
&&
3511 spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
) {
3512 spa_load_note(spa
, "forcing failmode to 'continue' "
3513 "as some top level vdevs are missing");
3514 spa
->spa_failmode
= ZIO_FAILURE_MODE_CONTINUE
;
3521 spa_ld_open_aux_vdevs(spa_t
*spa
, spa_import_type_t type
)
3524 vdev_t
*rvd
= spa
->spa_root_vdev
;
3527 * If we're assembling the pool from the split-off vdevs of
3528 * an existing pool, we don't want to attach the spares & cache
3533 * Load any hot spares for this pool.
3535 error
= spa_dir_prop(spa
, DMU_POOL_SPARES
, &spa
->spa_spares
.sav_object
,
3537 if (error
!= 0 && error
!= ENOENT
)
3538 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3539 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
3540 ASSERT(spa_version(spa
) >= SPA_VERSION_SPARES
);
3541 if (load_nvlist(spa
, spa
->spa_spares
.sav_object
,
3542 &spa
->spa_spares
.sav_config
) != 0) {
3543 spa_load_failed(spa
, "error loading spares nvlist");
3544 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3547 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3548 spa_load_spares(spa
);
3549 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3550 } else if (error
== 0) {
3551 spa
->spa_spares
.sav_sync
= B_TRUE
;
3555 * Load any level 2 ARC devices for this pool.
3557 error
= spa_dir_prop(spa
, DMU_POOL_L2CACHE
,
3558 &spa
->spa_l2cache
.sav_object
, B_FALSE
);
3559 if (error
!= 0 && error
!= ENOENT
)
3560 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3561 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
3562 ASSERT(spa_version(spa
) >= SPA_VERSION_L2CACHE
);
3563 if (load_nvlist(spa
, spa
->spa_l2cache
.sav_object
,
3564 &spa
->spa_l2cache
.sav_config
) != 0) {
3565 spa_load_failed(spa
, "error loading l2cache nvlist");
3566 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3569 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3570 spa_load_l2cache(spa
);
3571 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3572 } else if (error
== 0) {
3573 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
3580 spa_ld_load_vdev_metadata(spa_t
*spa
)
3583 vdev_t
*rvd
= spa
->spa_root_vdev
;
3586 * If the 'multihost' property is set, then never allow a pool to
3587 * be imported when the system hostid is zero. The exception to
3588 * this rule is zdb which is always allowed to access pools.
3590 if (spa_multihost(spa
) && spa_get_hostid() == 0 &&
3591 (spa
->spa_import_flags
& ZFS_IMPORT_SKIP_MMP
) == 0) {
3592 fnvlist_add_uint64(spa
->spa_load_info
,
3593 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_NO_HOSTID
);
3594 return (spa_vdev_err(rvd
, VDEV_AUX_ACTIVE
, EREMOTEIO
));
3598 * If the 'autoreplace' property is set, then post a resource notifying
3599 * the ZFS DE that it should not issue any faults for unopenable
3600 * devices. We also iterate over the vdevs, and post a sysevent for any
3601 * unopenable vdevs so that the normal autoreplace handler can take
3604 if (spa
->spa_autoreplace
&& spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
) {
3605 spa_check_removed(spa
->spa_root_vdev
);
3607 * For the import case, this is done in spa_import(), because
3608 * at this point we're using the spare definitions from
3609 * the MOS config, not necessarily from the userland config.
3611 if (spa
->spa_load_state
!= SPA_LOAD_IMPORT
) {
3612 spa_aux_check_removed(&spa
->spa_spares
);
3613 spa_aux_check_removed(&spa
->spa_l2cache
);
3618 * Load the vdev metadata such as metaslabs, DTLs, spacemap object, etc.
3620 error
= vdev_load(rvd
);
3622 spa_load_failed(spa
, "vdev_load failed [error=%d]", error
);
3623 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, error
));
3627 * Propagate the leaf DTLs we just loaded all the way up the vdev tree.
3629 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3630 vdev_dtl_reassess(rvd
, 0, 0, B_FALSE
);
3631 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3637 spa_ld_load_dedup_tables(spa_t
*spa
)
3640 vdev_t
*rvd
= spa
->spa_root_vdev
;
3642 error
= ddt_load(spa
);
3644 spa_load_failed(spa
, "ddt_load failed [error=%d]", error
);
3645 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3652 spa_ld_verify_logs(spa_t
*spa
, spa_import_type_t type
, char **ereport
)
3654 vdev_t
*rvd
= spa
->spa_root_vdev
;
3656 if (type
!= SPA_IMPORT_ASSEMBLE
&& spa_writeable(spa
)) {
3657 boolean_t missing
= spa_check_logs(spa
);
3659 if (spa
->spa_missing_tvds
!= 0) {
3660 spa_load_note(spa
, "spa_check_logs failed "
3661 "so dropping the logs");
3663 *ereport
= FM_EREPORT_ZFS_LOG_REPLAY
;
3664 spa_load_failed(spa
, "spa_check_logs failed");
3665 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_LOG
,
3675 spa_ld_verify_pool_data(spa_t
*spa
)
3678 vdev_t
*rvd
= spa
->spa_root_vdev
;
3681 * We've successfully opened the pool, verify that we're ready
3682 * to start pushing transactions.
3684 if (spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
) {
3685 error
= spa_load_verify(spa
);
3687 spa_load_failed(spa
, "spa_load_verify failed "
3688 "[error=%d]", error
);
3689 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
3698 spa_ld_claim_log_blocks(spa_t
*spa
)
3701 dsl_pool_t
*dp
= spa_get_dsl(spa
);
3704 * Claim log blocks that haven't been committed yet.
3705 * This must all happen in a single txg.
3706 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
3707 * invoked from zil_claim_log_block()'s i/o done callback.
3708 * Price of rollback is that we abandon the log.
3710 spa
->spa_claiming
= B_TRUE
;
3712 tx
= dmu_tx_create_assigned(dp
, spa_first_txg(spa
));
3713 (void) dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
3714 zil_claim
, tx
, DS_FIND_CHILDREN
);
3717 spa
->spa_claiming
= B_FALSE
;
3719 spa_set_log_state(spa
, SPA_LOG_GOOD
);
3723 spa_ld_check_for_config_update(spa_t
*spa
, uint64_t config_cache_txg
,
3724 boolean_t update_config_cache
)
3726 vdev_t
*rvd
= spa
->spa_root_vdev
;
3727 int need_update
= B_FALSE
;
3730 * If the config cache is stale, or we have uninitialized
3731 * metaslabs (see spa_vdev_add()), then update the config.
3733 * If this is a verbatim import, trust the current
3734 * in-core spa_config and update the disk labels.
3736 if (update_config_cache
|| config_cache_txg
!= spa
->spa_config_txg
||
3737 spa
->spa_load_state
== SPA_LOAD_IMPORT
||
3738 spa
->spa_load_state
== SPA_LOAD_RECOVER
||
3739 (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
))
3740 need_update
= B_TRUE
;
3742 for (int c
= 0; c
< rvd
->vdev_children
; c
++)
3743 if (rvd
->vdev_child
[c
]->vdev_ms_array
== 0)
3744 need_update
= B_TRUE
;
3747 * Update the config cache asychronously in case we're the
3748 * root pool, in which case the config cache isn't writable yet.
3751 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
3755 spa_ld_prepare_for_reload(spa_t
*spa
)
3757 int mode
= spa
->spa_mode
;
3758 int async_suspended
= spa
->spa_async_suspended
;
3761 spa_deactivate(spa
);
3762 spa_activate(spa
, mode
);
3765 * We save the value of spa_async_suspended as it gets reset to 0 by
3766 * spa_unload(). We want to restore it back to the original value before
3767 * returning as we might be calling spa_async_resume() later.
3769 spa
->spa_async_suspended
= async_suspended
;
3773 spa_ld_read_checkpoint_txg(spa_t
*spa
)
3775 uberblock_t checkpoint
;
3778 ASSERT0(spa
->spa_checkpoint_txg
);
3779 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
3781 error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
3782 DMU_POOL_ZPOOL_CHECKPOINT
, sizeof (uint64_t),
3783 sizeof (uberblock_t
) / sizeof (uint64_t), &checkpoint
);
3785 if (error
== ENOENT
)
3791 ASSERT3U(checkpoint
.ub_txg
, !=, 0);
3792 ASSERT3U(checkpoint
.ub_checkpoint_txg
, !=, 0);
3793 ASSERT3U(checkpoint
.ub_timestamp
, !=, 0);
3794 spa
->spa_checkpoint_txg
= checkpoint
.ub_txg
;
3795 spa
->spa_checkpoint_info
.sci_timestamp
= checkpoint
.ub_timestamp
;
3801 spa_ld_mos_init(spa_t
*spa
, spa_import_type_t type
)
3805 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
3806 ASSERT(spa
->spa_config_source
!= SPA_CONFIG_SRC_NONE
);
3809 * Never trust the config that is provided unless we are assembling
3810 * a pool following a split.
3811 * This means don't trust blkptrs and the vdev tree in general. This
3812 * also effectively puts the spa in read-only mode since
3813 * spa_writeable() checks for spa_trust_config to be true.
3814 * We will later load a trusted config from the MOS.
3816 if (type
!= SPA_IMPORT_ASSEMBLE
)
3817 spa
->spa_trust_config
= B_FALSE
;
3820 * Parse the config provided to create a vdev tree.
3822 error
= spa_ld_parse_config(spa
, type
);
3827 * Now that we have the vdev tree, try to open each vdev. This involves
3828 * opening the underlying physical device, retrieving its geometry and
3829 * probing the vdev with a dummy I/O. The state of each vdev will be set
3830 * based on the success of those operations. After this we'll be ready
3831 * to read from the vdevs.
3833 error
= spa_ld_open_vdevs(spa
);
3838 * Read the label of each vdev and make sure that the GUIDs stored
3839 * there match the GUIDs in the config provided.
3840 * If we're assembling a new pool that's been split off from an
3841 * existing pool, the labels haven't yet been updated so we skip
3842 * validation for now.
3844 if (type
!= SPA_IMPORT_ASSEMBLE
) {
3845 error
= spa_ld_validate_vdevs(spa
);
3851 * Read all vdev labels to find the best uberblock (i.e. latest,
3852 * unless spa_load_max_txg is set) and store it in spa_uberblock. We
3853 * get the list of features required to read blkptrs in the MOS from
3854 * the vdev label with the best uberblock and verify that our version
3855 * of zfs supports them all.
3857 error
= spa_ld_select_uberblock(spa
, type
);
3862 * Pass that uberblock to the dsl_pool layer which will open the root
3863 * blkptr. This blkptr points to the latest version of the MOS and will
3864 * allow us to read its contents.
3866 error
= spa_ld_open_rootbp(spa
);
3874 spa_ld_checkpoint_rewind(spa_t
*spa
)
3876 uberblock_t checkpoint
;
3879 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
3880 ASSERT(spa
->spa_import_flags
& ZFS_IMPORT_CHECKPOINT
);
3882 error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
3883 DMU_POOL_ZPOOL_CHECKPOINT
, sizeof (uint64_t),
3884 sizeof (uberblock_t
) / sizeof (uint64_t), &checkpoint
);
3887 spa_load_failed(spa
, "unable to retrieve checkpointed "
3888 "uberblock from the MOS config [error=%d]", error
);
3890 if (error
== ENOENT
)
3891 error
= ZFS_ERR_NO_CHECKPOINT
;
3896 ASSERT3U(checkpoint
.ub_txg
, <, spa
->spa_uberblock
.ub_txg
);
3897 ASSERT3U(checkpoint
.ub_txg
, ==, checkpoint
.ub_checkpoint_txg
);
3900 * We need to update the txg and timestamp of the checkpointed
3901 * uberblock to be higher than the latest one. This ensures that
3902 * the checkpointed uberblock is selected if we were to close and
3903 * reopen the pool right after we've written it in the vdev labels.
3904 * (also see block comment in vdev_uberblock_compare)
3906 checkpoint
.ub_txg
= spa
->spa_uberblock
.ub_txg
+ 1;
3907 checkpoint
.ub_timestamp
= gethrestime_sec();
3910 * Set current uberblock to be the checkpointed uberblock.
3912 spa
->spa_uberblock
= checkpoint
;
3915 * If we are doing a normal rewind, then the pool is open for
3916 * writing and we sync the "updated" checkpointed uberblock to
3917 * disk. Once this is done, we've basically rewound the whole
3918 * pool and there is no way back.
3920 * There are cases when we don't want to attempt and sync the
3921 * checkpointed uberblock to disk because we are opening a
3922 * pool as read-only. Specifically, verifying the checkpointed
3923 * state with zdb, and importing the checkpointed state to get
3924 * a "preview" of its content.
3926 if (spa_writeable(spa
)) {
3927 vdev_t
*rvd
= spa
->spa_root_vdev
;
3929 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3930 vdev_t
*svd
[SPA_SYNC_MIN_VDEVS
] = { NULL
};
3932 int children
= rvd
->vdev_children
;
3933 int c0
= spa_get_random(children
);
3935 for (int c
= 0; c
< children
; c
++) {
3936 vdev_t
*vd
= rvd
->vdev_child
[(c0
+ c
) % children
];
3938 /* Stop when revisiting the first vdev */
3939 if (c
> 0 && svd
[0] == vd
)
3942 if (vd
->vdev_ms_array
== 0 || vd
->vdev_islog
||
3943 !vdev_is_concrete(vd
))
3946 svd
[svdcount
++] = vd
;
3947 if (svdcount
== SPA_SYNC_MIN_VDEVS
)
3950 error
= vdev_config_sync(svd
, svdcount
, spa
->spa_first_txg
);
3952 spa
->spa_last_synced_guid
= rvd
->vdev_guid
;
3953 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3956 spa_load_failed(spa
, "failed to write checkpointed "
3957 "uberblock to the vdev labels [error=%d]", error
);
3966 spa_ld_mos_with_trusted_config(spa_t
*spa
, spa_import_type_t type
,
3967 boolean_t
*update_config_cache
)
3972 * Parse the config for pool, open and validate vdevs,
3973 * select an uberblock, and use that uberblock to open
3976 error
= spa_ld_mos_init(spa
, type
);
3981 * Retrieve the trusted config stored in the MOS and use it to create
3982 * a new, exact version of the vdev tree, then reopen all vdevs.
3984 error
= spa_ld_trusted_config(spa
, type
, B_FALSE
);
3985 if (error
== EAGAIN
) {
3986 if (update_config_cache
!= NULL
)
3987 *update_config_cache
= B_TRUE
;
3990 * Redo the loading process with the trusted config if it is
3991 * too different from the untrusted config.
3993 spa_ld_prepare_for_reload(spa
);
3994 spa_load_note(spa
, "RELOADING");
3995 error
= spa_ld_mos_init(spa
, type
);
3999 error
= spa_ld_trusted_config(spa
, type
, B_TRUE
);
4003 } else if (error
!= 0) {
4011 * Load an existing storage pool, using the config provided. This config
4012 * describes which vdevs are part of the pool and is later validated against
4013 * partial configs present in each vdev's label and an entire copy of the
4014 * config stored in the MOS.
4017 spa_load_impl(spa_t
*spa
, spa_import_type_t type
, char **ereport
)
4020 boolean_t missing_feat_write
= B_FALSE
;
4021 boolean_t checkpoint_rewind
=
4022 (spa
->spa_import_flags
& ZFS_IMPORT_CHECKPOINT
);
4023 boolean_t update_config_cache
= B_FALSE
;
4025 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
4026 ASSERT(spa
->spa_config_source
!= SPA_CONFIG_SRC_NONE
);
4028 spa_load_note(spa
, "LOADING");
4030 error
= spa_ld_mos_with_trusted_config(spa
, type
, &update_config_cache
);
4035 * If we are rewinding to the checkpoint then we need to repeat
4036 * everything we've done so far in this function but this time
4037 * selecting the checkpointed uberblock and using that to open
4040 if (checkpoint_rewind
) {
4042 * If we are rewinding to the checkpoint update config cache
4045 update_config_cache
= B_TRUE
;
4048 * Extract the checkpointed uberblock from the current MOS
4049 * and use this as the pool's uberblock from now on. If the
4050 * pool is imported as writeable we also write the checkpoint
4051 * uberblock to the labels, making the rewind permanent.
4053 error
= spa_ld_checkpoint_rewind(spa
);
4058 * Redo the loading process process again with the
4059 * checkpointed uberblock.
4061 spa_ld_prepare_for_reload(spa
);
4062 spa_load_note(spa
, "LOADING checkpointed uberblock");
4063 error
= spa_ld_mos_with_trusted_config(spa
, type
, NULL
);
4069 * Retrieve the checkpoint txg if the pool has a checkpoint.
4071 error
= spa_ld_read_checkpoint_txg(spa
);
4076 * Retrieve the mapping of indirect vdevs. Those vdevs were removed
4077 * from the pool and their contents were re-mapped to other vdevs. Note
4078 * that everything that we read before this step must have been
4079 * rewritten on concrete vdevs after the last device removal was
4080 * initiated. Otherwise we could be reading from indirect vdevs before
4081 * we have loaded their mappings.
4083 error
= spa_ld_open_indirect_vdev_metadata(spa
);
4088 * Retrieve the full list of active features from the MOS and check if
4089 * they are all supported.
4091 error
= spa_ld_check_features(spa
, &missing_feat_write
);
4096 * Load several special directories from the MOS needed by the dsl_pool
4099 error
= spa_ld_load_special_directories(spa
);
4104 * Retrieve pool properties from the MOS.
4106 error
= spa_ld_get_props(spa
);
4111 * Retrieve the list of auxiliary devices - cache devices and spares -
4114 error
= spa_ld_open_aux_vdevs(spa
, type
);
4119 * Load the metadata for all vdevs. Also check if unopenable devices
4120 * should be autoreplaced.
4122 error
= spa_ld_load_vdev_metadata(spa
);
4126 error
= spa_ld_load_dedup_tables(spa
);
4131 * Verify the logs now to make sure we don't have any unexpected errors
4132 * when we claim log blocks later.
4134 error
= spa_ld_verify_logs(spa
, type
, ereport
);
4138 if (missing_feat_write
) {
4139 ASSERT(spa
->spa_load_state
== SPA_LOAD_TRYIMPORT
);
4142 * At this point, we know that we can open the pool in
4143 * read-only mode but not read-write mode. We now have enough
4144 * information and can return to userland.
4146 return (spa_vdev_err(spa
->spa_root_vdev
, VDEV_AUX_UNSUP_FEAT
,
4151 * Traverse the last txgs to make sure the pool was left off in a safe
4152 * state. When performing an extreme rewind, we verify the whole pool,
4153 * which can take a very long time.
4155 error
= spa_ld_verify_pool_data(spa
);
4160 * Calculate the deflated space for the pool. This must be done before
4161 * we write anything to the pool because we'd need to update the space
4162 * accounting using the deflated sizes.
4164 spa_update_dspace(spa
);
4167 * We have now retrieved all the information we needed to open the
4168 * pool. If we are importing the pool in read-write mode, a few
4169 * additional steps must be performed to finish the import.
4171 if (spa_writeable(spa
) && (spa
->spa_load_state
== SPA_LOAD_RECOVER
||
4172 spa
->spa_load_max_txg
== UINT64_MAX
)) {
4173 uint64_t config_cache_txg
= spa
->spa_config_txg
;
4175 ASSERT(spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
);
4178 * In case of a checkpoint rewind, log the original txg
4179 * of the checkpointed uberblock.
4181 if (checkpoint_rewind
) {
4182 spa_history_log_internal(spa
, "checkpoint rewind",
4183 NULL
, "rewound state to txg=%llu",
4184 (u_longlong_t
)spa
->spa_uberblock
.ub_checkpoint_txg
);
4188 * Traverse the ZIL and claim all blocks.
4190 spa_ld_claim_log_blocks(spa
);
4193 * Kick-off the syncing thread.
4195 spa
->spa_sync_on
= B_TRUE
;
4196 txg_sync_start(spa
->spa_dsl_pool
);
4197 mmp_thread_start(spa
);
4200 * Wait for all claims to sync. We sync up to the highest
4201 * claimed log block birth time so that claimed log blocks
4202 * don't appear to be from the future. spa_claim_max_txg
4203 * will have been set for us by ZIL traversal operations
4206 txg_wait_synced(spa
->spa_dsl_pool
, spa
->spa_claim_max_txg
);
4209 * Check if we need to request an update of the config. On the
4210 * next sync, we would update the config stored in vdev labels
4211 * and the cachefile (by default /etc/zfs/zpool.cache).
4213 spa_ld_check_for_config_update(spa
, config_cache_txg
,
4214 update_config_cache
);
4217 * Check all DTLs to see if anything needs resilvering.
4219 if (!dsl_scan_resilvering(spa
->spa_dsl_pool
) &&
4220 vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
))
4221 spa_async_request(spa
, SPA_ASYNC_RESILVER
);
4224 * Log the fact that we booted up (so that we can detect if
4225 * we rebooted in the middle of an operation).
4227 spa_history_log_version(spa
, "open", NULL
);
4230 * Delete any inconsistent datasets.
4232 (void) dmu_objset_find(spa_name(spa
),
4233 dsl_destroy_inconsistent
, NULL
, DS_FIND_CHILDREN
);
4236 * Clean up any stale temporary dataset userrefs.
4238 dsl_pool_clean_tmp_userrefs(spa
->spa_dsl_pool
);
4240 spa_restart_removal(spa
);
4242 spa_spawn_aux_threads(spa
);
4245 spa_load_note(spa
, "LOADED");
4251 spa_load_retry(spa_t
*spa
, spa_load_state_t state
)
4253 int mode
= spa
->spa_mode
;
4256 spa_deactivate(spa
);
4258 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
- 1;
4260 spa_activate(spa
, mode
);
4261 spa_async_suspend(spa
);
4263 spa_load_note(spa
, "spa_load_retry: rewind, max txg: %llu",
4264 (u_longlong_t
)spa
->spa_load_max_txg
);
4266 return (spa_load(spa
, state
, SPA_IMPORT_EXISTING
));
4270 * If spa_load() fails this function will try loading prior txg's. If
4271 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
4272 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
4273 * function will not rewind the pool and will return the same error as
4277 spa_load_best(spa_t
*spa
, spa_load_state_t state
, uint64_t max_request
,
4280 nvlist_t
*loadinfo
= NULL
;
4281 nvlist_t
*config
= NULL
;
4282 int load_error
, rewind_error
;
4283 uint64_t safe_rewind_txg
;
4286 if (spa
->spa_load_txg
&& state
== SPA_LOAD_RECOVER
) {
4287 spa
->spa_load_max_txg
= spa
->spa_load_txg
;
4288 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
4290 spa
->spa_load_max_txg
= max_request
;
4291 if (max_request
!= UINT64_MAX
)
4292 spa
->spa_extreme_rewind
= B_TRUE
;
4295 load_error
= rewind_error
= spa_load(spa
, state
, SPA_IMPORT_EXISTING
);
4296 if (load_error
== 0)
4298 if (load_error
== ZFS_ERR_NO_CHECKPOINT
) {
4300 * When attempting checkpoint-rewind on a pool with no
4301 * checkpoint, we should not attempt to load uberblocks
4302 * from previous txgs when spa_load fails.
4304 ASSERT(spa
->spa_import_flags
& ZFS_IMPORT_CHECKPOINT
);
4305 return (load_error
);
4308 if (spa
->spa_root_vdev
!= NULL
)
4309 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
4311 spa
->spa_last_ubsync_txg
= spa
->spa_uberblock
.ub_txg
;
4312 spa
->spa_last_ubsync_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
4314 if (rewind_flags
& ZPOOL_NEVER_REWIND
) {
4315 nvlist_free(config
);
4316 return (load_error
);
4319 if (state
== SPA_LOAD_RECOVER
) {
4320 /* Price of rolling back is discarding txgs, including log */
4321 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
4324 * If we aren't rolling back save the load info from our first
4325 * import attempt so that we can restore it after attempting
4328 loadinfo
= spa
->spa_load_info
;
4329 spa
->spa_load_info
= fnvlist_alloc();
4332 spa
->spa_load_max_txg
= spa
->spa_last_ubsync_txg
;
4333 safe_rewind_txg
= spa
->spa_last_ubsync_txg
- TXG_DEFER_SIZE
;
4334 min_txg
= (rewind_flags
& ZPOOL_EXTREME_REWIND
) ?
4335 TXG_INITIAL
: safe_rewind_txg
;
4338 * Continue as long as we're finding errors, we're still within
4339 * the acceptable rewind range, and we're still finding uberblocks
4341 while (rewind_error
&& spa
->spa_uberblock
.ub_txg
>= min_txg
&&
4342 spa
->spa_uberblock
.ub_txg
<= spa
->spa_load_max_txg
) {
4343 if (spa
->spa_load_max_txg
< safe_rewind_txg
)
4344 spa
->spa_extreme_rewind
= B_TRUE
;
4345 rewind_error
= spa_load_retry(spa
, state
);
4348 spa
->spa_extreme_rewind
= B_FALSE
;
4349 spa
->spa_load_max_txg
= UINT64_MAX
;
4351 if (config
&& (rewind_error
|| state
!= SPA_LOAD_RECOVER
))
4352 spa_config_set(spa
, config
);
4354 nvlist_free(config
);
4356 if (state
== SPA_LOAD_RECOVER
) {
4357 ASSERT3P(loadinfo
, ==, NULL
);
4358 return (rewind_error
);
4360 /* Store the rewind info as part of the initial load info */
4361 fnvlist_add_nvlist(loadinfo
, ZPOOL_CONFIG_REWIND_INFO
,
4362 spa
->spa_load_info
);
4364 /* Restore the initial load info */
4365 fnvlist_free(spa
->spa_load_info
);
4366 spa
->spa_load_info
= loadinfo
;
4368 return (load_error
);
4375 * The import case is identical to an open except that the configuration is sent
4376 * down from userland, instead of grabbed from the configuration cache. For the
4377 * case of an open, the pool configuration will exist in the
4378 * POOL_STATE_UNINITIALIZED state.
4380 * The stats information (gen/count/ustats) is used to gather vdev statistics at
4381 * the same time open the pool, without having to keep around the spa_t in some
4385 spa_open_common(const char *pool
, spa_t
**spapp
, void *tag
, nvlist_t
*nvpolicy
,
4389 spa_load_state_t state
= SPA_LOAD_OPEN
;
4391 int locked
= B_FALSE
;
4392 int firstopen
= B_FALSE
;
4397 * As disgusting as this is, we need to support recursive calls to this
4398 * function because dsl_dir_open() is called during spa_load(), and ends
4399 * up calling spa_open() again. The real fix is to figure out how to
4400 * avoid dsl_dir_open() calling this in the first place.
4402 if (MUTEX_NOT_HELD(&spa_namespace_lock
)) {
4403 mutex_enter(&spa_namespace_lock
);
4407 if ((spa
= spa_lookup(pool
)) == NULL
) {
4409 mutex_exit(&spa_namespace_lock
);
4410 return (SET_ERROR(ENOENT
));
4413 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
) {
4414 zpool_load_policy_t policy
;
4418 zpool_get_load_policy(nvpolicy
? nvpolicy
: spa
->spa_config
,
4420 if (policy
.zlp_rewind
& ZPOOL_DO_REWIND
)
4421 state
= SPA_LOAD_RECOVER
;
4423 spa_activate(spa
, spa_mode_global
);
4425 if (state
!= SPA_LOAD_RECOVER
)
4426 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
4427 spa
->spa_config_source
= SPA_CONFIG_SRC_CACHEFILE
;
4429 zfs_dbgmsg("spa_open_common: opening %s", pool
);
4430 error
= spa_load_best(spa
, state
, policy
.zlp_txg
,
4433 if (error
== EBADF
) {
4435 * If vdev_validate() returns failure (indicated by
4436 * EBADF), it indicates that one of the vdevs indicates
4437 * that the pool has been exported or destroyed. If
4438 * this is the case, the config cache is out of sync and
4439 * we should remove the pool from the namespace.
4442 spa_deactivate(spa
);
4443 spa_write_cachefile(spa
, B_TRUE
, B_TRUE
);
4446 mutex_exit(&spa_namespace_lock
);
4447 return (SET_ERROR(ENOENT
));
4452 * We can't open the pool, but we still have useful
4453 * information: the state of each vdev after the
4454 * attempted vdev_open(). Return this to the user.
4456 if (config
!= NULL
&& spa
->spa_config
) {
4457 VERIFY(nvlist_dup(spa
->spa_config
, config
,
4459 VERIFY(nvlist_add_nvlist(*config
,
4460 ZPOOL_CONFIG_LOAD_INFO
,
4461 spa
->spa_load_info
) == 0);
4464 spa_deactivate(spa
);
4465 spa
->spa_last_open_failed
= error
;
4467 mutex_exit(&spa_namespace_lock
);
4473 spa_open_ref(spa
, tag
);
4476 *config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
4479 * If we've recovered the pool, pass back any information we
4480 * gathered while doing the load.
4482 if (state
== SPA_LOAD_RECOVER
) {
4483 VERIFY(nvlist_add_nvlist(*config
, ZPOOL_CONFIG_LOAD_INFO
,
4484 spa
->spa_load_info
) == 0);
4488 spa
->spa_last_open_failed
= 0;
4489 spa
->spa_last_ubsync_txg
= 0;
4490 spa
->spa_load_txg
= 0;
4491 mutex_exit(&spa_namespace_lock
);
4495 zvol_create_minors(spa
, spa_name(spa
), B_TRUE
);
4503 spa_open_rewind(const char *name
, spa_t
**spapp
, void *tag
, nvlist_t
*policy
,
4506 return (spa_open_common(name
, spapp
, tag
, policy
, config
));
4510 spa_open(const char *name
, spa_t
**spapp
, void *tag
)
4512 return (spa_open_common(name
, spapp
, tag
, NULL
, NULL
));
4516 * Lookup the given spa_t, incrementing the inject count in the process,
4517 * preventing it from being exported or destroyed.
4520 spa_inject_addref(char *name
)
4524 mutex_enter(&spa_namespace_lock
);
4525 if ((spa
= spa_lookup(name
)) == NULL
) {
4526 mutex_exit(&spa_namespace_lock
);
4529 spa
->spa_inject_ref
++;
4530 mutex_exit(&spa_namespace_lock
);
4536 spa_inject_delref(spa_t
*spa
)
4538 mutex_enter(&spa_namespace_lock
);
4539 spa
->spa_inject_ref
--;
4540 mutex_exit(&spa_namespace_lock
);
4544 * Add spares device information to the nvlist.
4547 spa_add_spares(spa_t
*spa
, nvlist_t
*config
)
4557 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
4559 if (spa
->spa_spares
.sav_count
== 0)
4562 VERIFY(nvlist_lookup_nvlist(config
,
4563 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
4564 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
4565 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
4567 VERIFY(nvlist_add_nvlist_array(nvroot
,
4568 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
4569 VERIFY(nvlist_lookup_nvlist_array(nvroot
,
4570 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
4573 * Go through and find any spares which have since been
4574 * repurposed as an active spare. If this is the case, update
4575 * their status appropriately.
4577 for (i
= 0; i
< nspares
; i
++) {
4578 VERIFY(nvlist_lookup_uint64(spares
[i
],
4579 ZPOOL_CONFIG_GUID
, &guid
) == 0);
4580 if (spa_spare_exists(guid
, &pool
, NULL
) &&
4582 VERIFY(nvlist_lookup_uint64_array(
4583 spares
[i
], ZPOOL_CONFIG_VDEV_STATS
,
4584 (uint64_t **)&vs
, &vsc
) == 0);
4585 vs
->vs_state
= VDEV_STATE_CANT_OPEN
;
4586 vs
->vs_aux
= VDEV_AUX_SPARED
;
4593 * Add l2cache device information to the nvlist, including vdev stats.
4596 spa_add_l2cache(spa_t
*spa
, nvlist_t
*config
)
4599 uint_t i
, j
, nl2cache
;
4606 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
4608 if (spa
->spa_l2cache
.sav_count
== 0)
4611 VERIFY(nvlist_lookup_nvlist(config
,
4612 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
4613 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_l2cache
.sav_config
,
4614 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
4615 if (nl2cache
!= 0) {
4616 VERIFY(nvlist_add_nvlist_array(nvroot
,
4617 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
4618 VERIFY(nvlist_lookup_nvlist_array(nvroot
,
4619 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
4622 * Update level 2 cache device stats.
4625 for (i
= 0; i
< nl2cache
; i
++) {
4626 VERIFY(nvlist_lookup_uint64(l2cache
[i
],
4627 ZPOOL_CONFIG_GUID
, &guid
) == 0);
4630 for (j
= 0; j
< spa
->spa_l2cache
.sav_count
; j
++) {
4632 spa
->spa_l2cache
.sav_vdevs
[j
]->vdev_guid
) {
4633 vd
= spa
->spa_l2cache
.sav_vdevs
[j
];
4639 VERIFY(nvlist_lookup_uint64_array(l2cache
[i
],
4640 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
, &vsc
)
4642 vdev_get_stats(vd
, vs
);
4643 vdev_config_generate_stats(vd
, l2cache
[i
]);
4650 spa_feature_stats_from_disk(spa_t
*spa
, nvlist_t
*features
)
4655 if (spa
->spa_feat_for_read_obj
!= 0) {
4656 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
4657 spa
->spa_feat_for_read_obj
);
4658 zap_cursor_retrieve(&zc
, &za
) == 0;
4659 zap_cursor_advance(&zc
)) {
4660 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
4661 za
.za_num_integers
== 1);
4662 VERIFY0(nvlist_add_uint64(features
, za
.za_name
,
4663 za
.za_first_integer
));
4665 zap_cursor_fini(&zc
);
4668 if (spa
->spa_feat_for_write_obj
!= 0) {
4669 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
4670 spa
->spa_feat_for_write_obj
);
4671 zap_cursor_retrieve(&zc
, &za
) == 0;
4672 zap_cursor_advance(&zc
)) {
4673 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
4674 za
.za_num_integers
== 1);
4675 VERIFY0(nvlist_add_uint64(features
, za
.za_name
,
4676 za
.za_first_integer
));
4678 zap_cursor_fini(&zc
);
4683 spa_feature_stats_from_cache(spa_t
*spa
, nvlist_t
*features
)
4687 for (i
= 0; i
< SPA_FEATURES
; i
++) {
4688 zfeature_info_t feature
= spa_feature_table
[i
];
4691 if (feature_get_refcount(spa
, &feature
, &refcount
) != 0)
4694 VERIFY0(nvlist_add_uint64(features
, feature
.fi_guid
, refcount
));
4699 * Store a list of pool features and their reference counts in the
4702 * The first time this is called on a spa, allocate a new nvlist, fetch
4703 * the pool features and reference counts from disk, then save the list
4704 * in the spa. In subsequent calls on the same spa use the saved nvlist
4705 * and refresh its values from the cached reference counts. This
4706 * ensures we don't block here on I/O on a suspended pool so 'zpool
4707 * clear' can resume the pool.
4710 spa_add_feature_stats(spa_t
*spa
, nvlist_t
*config
)
4714 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
4716 mutex_enter(&spa
->spa_feat_stats_lock
);
4717 features
= spa
->spa_feat_stats
;
4719 if (features
!= NULL
) {
4720 spa_feature_stats_from_cache(spa
, features
);
4722 VERIFY0(nvlist_alloc(&features
, NV_UNIQUE_NAME
, KM_SLEEP
));
4723 spa
->spa_feat_stats
= features
;
4724 spa_feature_stats_from_disk(spa
, features
);
4727 VERIFY0(nvlist_add_nvlist(config
, ZPOOL_CONFIG_FEATURE_STATS
,
4730 mutex_exit(&spa
->spa_feat_stats_lock
);
4734 spa_get_stats(const char *name
, nvlist_t
**config
,
4735 char *altroot
, size_t buflen
)
4741 error
= spa_open_common(name
, &spa
, FTAG
, NULL
, config
);
4745 * This still leaves a window of inconsistency where the spares
4746 * or l2cache devices could change and the config would be
4747 * self-inconsistent.
4749 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
4751 if (*config
!= NULL
) {
4752 uint64_t loadtimes
[2];
4754 loadtimes
[0] = spa
->spa_loaded_ts
.tv_sec
;
4755 loadtimes
[1] = spa
->spa_loaded_ts
.tv_nsec
;
4756 VERIFY(nvlist_add_uint64_array(*config
,
4757 ZPOOL_CONFIG_LOADED_TIME
, loadtimes
, 2) == 0);
4759 VERIFY(nvlist_add_uint64(*config
,
4760 ZPOOL_CONFIG_ERRCOUNT
,
4761 spa_get_errlog_size(spa
)) == 0);
4763 if (spa_suspended(spa
)) {
4764 VERIFY(nvlist_add_uint64(*config
,
4765 ZPOOL_CONFIG_SUSPENDED
,
4766 spa
->spa_failmode
) == 0);
4767 VERIFY(nvlist_add_uint64(*config
,
4768 ZPOOL_CONFIG_SUSPENDED_REASON
,
4769 spa
->spa_suspended
) == 0);
4772 spa_add_spares(spa
, *config
);
4773 spa_add_l2cache(spa
, *config
);
4774 spa_add_feature_stats(spa
, *config
);
4779 * We want to get the alternate root even for faulted pools, so we cheat
4780 * and call spa_lookup() directly.
4784 mutex_enter(&spa_namespace_lock
);
4785 spa
= spa_lookup(name
);
4787 spa_altroot(spa
, altroot
, buflen
);
4791 mutex_exit(&spa_namespace_lock
);
4793 spa_altroot(spa
, altroot
, buflen
);
4798 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
4799 spa_close(spa
, FTAG
);
4806 * Validate that the auxiliary device array is well formed. We must have an
4807 * array of nvlists, each which describes a valid leaf vdev. If this is an
4808 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
4809 * specified, as long as they are well-formed.
4812 spa_validate_aux_devs(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
,
4813 spa_aux_vdev_t
*sav
, const char *config
, uint64_t version
,
4814 vdev_labeltype_t label
)
4821 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
4824 * It's acceptable to have no devs specified.
4826 if (nvlist_lookup_nvlist_array(nvroot
, config
, &dev
, &ndev
) != 0)
4830 return (SET_ERROR(EINVAL
));
4833 * Make sure the pool is formatted with a version that supports this
4836 if (spa_version(spa
) < version
)
4837 return (SET_ERROR(ENOTSUP
));
4840 * Set the pending device list so we correctly handle device in-use
4843 sav
->sav_pending
= dev
;
4844 sav
->sav_npending
= ndev
;
4846 for (i
= 0; i
< ndev
; i
++) {
4847 if ((error
= spa_config_parse(spa
, &vd
, dev
[i
], NULL
, 0,
4851 if (!vd
->vdev_ops
->vdev_op_leaf
) {
4853 error
= SET_ERROR(EINVAL
);
4859 if ((error
= vdev_open(vd
)) == 0 &&
4860 (error
= vdev_label_init(vd
, crtxg
, label
)) == 0) {
4861 VERIFY(nvlist_add_uint64(dev
[i
], ZPOOL_CONFIG_GUID
,
4862 vd
->vdev_guid
) == 0);
4868 (mode
!= VDEV_ALLOC_SPARE
&& mode
!= VDEV_ALLOC_L2CACHE
))
4875 sav
->sav_pending
= NULL
;
4876 sav
->sav_npending
= 0;
4881 spa_validate_aux(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
)
4885 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
4887 if ((error
= spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
4888 &spa
->spa_spares
, ZPOOL_CONFIG_SPARES
, SPA_VERSION_SPARES
,
4889 VDEV_LABEL_SPARE
)) != 0) {
4893 return (spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
4894 &spa
->spa_l2cache
, ZPOOL_CONFIG_L2CACHE
, SPA_VERSION_L2CACHE
,
4895 VDEV_LABEL_L2CACHE
));
4899 spa_set_aux_vdevs(spa_aux_vdev_t
*sav
, nvlist_t
**devs
, int ndevs
,
4904 if (sav
->sav_config
!= NULL
) {
4910 * Generate new dev list by concatenating with the
4913 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
, config
,
4914 &olddevs
, &oldndevs
) == 0);
4916 newdevs
= kmem_alloc(sizeof (void *) *
4917 (ndevs
+ oldndevs
), KM_SLEEP
);
4918 for (i
= 0; i
< oldndevs
; i
++)
4919 VERIFY(nvlist_dup(olddevs
[i
], &newdevs
[i
],
4921 for (i
= 0; i
< ndevs
; i
++)
4922 VERIFY(nvlist_dup(devs
[i
], &newdevs
[i
+ oldndevs
],
4925 VERIFY(nvlist_remove(sav
->sav_config
, config
,
4926 DATA_TYPE_NVLIST_ARRAY
) == 0);
4928 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
,
4929 config
, newdevs
, ndevs
+ oldndevs
) == 0);
4930 for (i
= 0; i
< oldndevs
+ ndevs
; i
++)
4931 nvlist_free(newdevs
[i
]);
4932 kmem_free(newdevs
, (oldndevs
+ ndevs
) * sizeof (void *));
4935 * Generate a new dev list.
4937 VERIFY(nvlist_alloc(&sav
->sav_config
, NV_UNIQUE_NAME
,
4939 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
, config
,
4945 * Stop and drop level 2 ARC devices
4948 spa_l2cache_drop(spa_t
*spa
)
4952 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
4954 for (i
= 0; i
< sav
->sav_count
; i
++) {
4957 vd
= sav
->sav_vdevs
[i
];
4960 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
4961 pool
!= 0ULL && l2arc_vdev_present(vd
))
4962 l2arc_remove_vdev(vd
);
4967 * Verify encryption parameters for spa creation. If we are encrypting, we must
4968 * have the encryption feature flag enabled.
4971 spa_create_check_encryption_params(dsl_crypto_params_t
*dcp
,
4972 boolean_t has_encryption
)
4974 if (dcp
->cp_crypt
!= ZIO_CRYPT_OFF
&&
4975 dcp
->cp_crypt
!= ZIO_CRYPT_INHERIT
&&
4977 return (SET_ERROR(ENOTSUP
));
4979 return (dmu_objset_create_crypt_check(NULL
, dcp
, NULL
));
4986 spa_create(const char *pool
, nvlist_t
*nvroot
, nvlist_t
*props
,
4987 nvlist_t
*zplprops
, dsl_crypto_params_t
*dcp
)
4990 char *altroot
= NULL
;
4995 uint64_t txg
= TXG_INITIAL
;
4996 nvlist_t
**spares
, **l2cache
;
4997 uint_t nspares
, nl2cache
;
4998 uint64_t version
, obj
;
4999 boolean_t has_features
;
5000 boolean_t has_encryption
;
5006 if (props
== NULL
||
5007 nvlist_lookup_string(props
, "tname", &poolname
) != 0)
5008 poolname
= (char *)pool
;
5011 * If this pool already exists, return failure.
5013 mutex_enter(&spa_namespace_lock
);
5014 if (spa_lookup(poolname
) != NULL
) {
5015 mutex_exit(&spa_namespace_lock
);
5016 return (SET_ERROR(EEXIST
));
5020 * Allocate a new spa_t structure.
5022 nvl
= fnvlist_alloc();
5023 fnvlist_add_string(nvl
, ZPOOL_CONFIG_POOL_NAME
, pool
);
5024 (void) nvlist_lookup_string(props
,
5025 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
5026 spa
= spa_add(poolname
, nvl
, altroot
);
5028 spa_activate(spa
, spa_mode_global
);
5030 if (props
&& (error
= spa_prop_validate(spa
, props
))) {
5031 spa_deactivate(spa
);
5033 mutex_exit(&spa_namespace_lock
);
5038 * Temporary pool names should never be written to disk.
5040 if (poolname
!= pool
)
5041 spa
->spa_import_flags
|= ZFS_IMPORT_TEMP_NAME
;
5043 has_features
= B_FALSE
;
5044 has_encryption
= B_FALSE
;
5045 for (nvpair_t
*elem
= nvlist_next_nvpair(props
, NULL
);
5046 elem
!= NULL
; elem
= nvlist_next_nvpair(props
, elem
)) {
5047 if (zpool_prop_feature(nvpair_name(elem
))) {
5048 has_features
= B_TRUE
;
5050 feat_name
= strchr(nvpair_name(elem
), '@') + 1;
5051 VERIFY0(zfeature_lookup_name(feat_name
, &feat
));
5052 if (feat
== SPA_FEATURE_ENCRYPTION
)
5053 has_encryption
= B_TRUE
;
5057 /* verify encryption params, if they were provided */
5059 error
= spa_create_check_encryption_params(dcp
, has_encryption
);
5061 spa_deactivate(spa
);
5063 mutex_exit(&spa_namespace_lock
);
5068 if (has_features
|| nvlist_lookup_uint64(props
,
5069 zpool_prop_to_name(ZPOOL_PROP_VERSION
), &version
) != 0) {
5070 version
= SPA_VERSION
;
5072 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
5074 spa
->spa_first_txg
= txg
;
5075 spa
->spa_uberblock
.ub_txg
= txg
- 1;
5076 spa
->spa_uberblock
.ub_version
= version
;
5077 spa
->spa_ubsync
= spa
->spa_uberblock
;
5078 spa
->spa_load_state
= SPA_LOAD_CREATE
;
5079 spa
->spa_removing_phys
.sr_state
= DSS_NONE
;
5080 spa
->spa_removing_phys
.sr_removing_vdev
= -1;
5081 spa
->spa_removing_phys
.sr_prev_indirect_vdev
= -1;
5084 * Create "The Godfather" zio to hold all async IOs
5086 spa
->spa_async_zio_root
= kmem_alloc(max_ncpus
* sizeof (void *),
5088 for (int i
= 0; i
< max_ncpus
; i
++) {
5089 spa
->spa_async_zio_root
[i
] = zio_root(spa
, NULL
, NULL
,
5090 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
5091 ZIO_FLAG_GODFATHER
);
5095 * Create the root vdev.
5097 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5099 error
= spa_config_parse(spa
, &rvd
, nvroot
, NULL
, 0, VDEV_ALLOC_ADD
);
5101 ASSERT(error
!= 0 || rvd
!= NULL
);
5102 ASSERT(error
!= 0 || spa
->spa_root_vdev
== rvd
);
5104 if (error
== 0 && !zfs_allocatable_devs(nvroot
))
5105 error
= SET_ERROR(EINVAL
);
5108 (error
= vdev_create(rvd
, txg
, B_FALSE
)) == 0 &&
5109 (error
= spa_validate_aux(spa
, nvroot
, txg
,
5110 VDEV_ALLOC_ADD
)) == 0) {
5112 * instantiate the metaslab groups (this will dirty the vdevs)
5113 * we can no longer error exit past this point
5115 for (int c
= 0; error
== 0 && c
< rvd
->vdev_children
; c
++) {
5116 vdev_t
*vd
= rvd
->vdev_child
[c
];
5118 vdev_metaslab_set_size(vd
);
5119 vdev_expand(vd
, txg
);
5123 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5127 spa_deactivate(spa
);
5129 mutex_exit(&spa_namespace_lock
);
5134 * Get the list of spares, if specified.
5136 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
5137 &spares
, &nspares
) == 0) {
5138 VERIFY(nvlist_alloc(&spa
->spa_spares
.sav_config
, NV_UNIQUE_NAME
,
5140 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
5141 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
5142 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5143 spa_load_spares(spa
);
5144 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5145 spa
->spa_spares
.sav_sync
= B_TRUE
;
5149 * Get the list of level 2 cache devices, if specified.
5151 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
5152 &l2cache
, &nl2cache
) == 0) {
5153 VERIFY(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
5154 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
5155 VERIFY(nvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
5156 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
5157 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5158 spa_load_l2cache(spa
);
5159 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5160 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
5163 spa
->spa_is_initializing
= B_TRUE
;
5164 spa
->spa_dsl_pool
= dp
= dsl_pool_create(spa
, zplprops
, dcp
, txg
);
5165 spa
->spa_is_initializing
= B_FALSE
;
5168 * Create DDTs (dedup tables).
5172 spa_update_dspace(spa
);
5174 tx
= dmu_tx_create_assigned(dp
, txg
);
5177 * Create the pool's history object.
5179 if (version
>= SPA_VERSION_ZPOOL_HISTORY
&& !spa
->spa_history
)
5180 spa_history_create_obj(spa
, tx
);
5182 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_CREATE
);
5183 spa_history_log_version(spa
, "create", tx
);
5186 * Create the pool config object.
5188 spa
->spa_config_object
= dmu_object_alloc(spa
->spa_meta_objset
,
5189 DMU_OT_PACKED_NVLIST
, SPA_CONFIG_BLOCKSIZE
,
5190 DMU_OT_PACKED_NVLIST_SIZE
, sizeof (uint64_t), tx
);
5192 if (zap_add(spa
->spa_meta_objset
,
5193 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CONFIG
,
5194 sizeof (uint64_t), 1, &spa
->spa_config_object
, tx
) != 0) {
5195 cmn_err(CE_PANIC
, "failed to add pool config");
5198 if (zap_add(spa
->spa_meta_objset
,
5199 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CREATION_VERSION
,
5200 sizeof (uint64_t), 1, &version
, tx
) != 0) {
5201 cmn_err(CE_PANIC
, "failed to add pool version");
5204 /* Newly created pools with the right version are always deflated. */
5205 if (version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
5206 spa
->spa_deflate
= TRUE
;
5207 if (zap_add(spa
->spa_meta_objset
,
5208 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
5209 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
) != 0) {
5210 cmn_err(CE_PANIC
, "failed to add deflate");
5215 * Create the deferred-free bpobj. Turn off compression
5216 * because sync-to-convergence takes longer if the blocksize
5219 obj
= bpobj_alloc(spa
->spa_meta_objset
, 1 << 14, tx
);
5220 dmu_object_set_compress(spa
->spa_meta_objset
, obj
,
5221 ZIO_COMPRESS_OFF
, tx
);
5222 if (zap_add(spa
->spa_meta_objset
,
5223 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_SYNC_BPOBJ
,
5224 sizeof (uint64_t), 1, &obj
, tx
) != 0) {
5225 cmn_err(CE_PANIC
, "failed to add bpobj");
5227 VERIFY3U(0, ==, bpobj_open(&spa
->spa_deferred_bpobj
,
5228 spa
->spa_meta_objset
, obj
));
5231 * Generate some random noise for salted checksums to operate on.
5233 (void) random_get_pseudo_bytes(spa
->spa_cksum_salt
.zcs_bytes
,
5234 sizeof (spa
->spa_cksum_salt
.zcs_bytes
));
5237 * Set pool properties.
5239 spa
->spa_bootfs
= zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS
);
5240 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
5241 spa
->spa_failmode
= zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE
);
5242 spa
->spa_autoexpand
= zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND
);
5243 spa
->spa_multihost
= zpool_prop_default_numeric(ZPOOL_PROP_MULTIHOST
);
5245 if (props
!= NULL
) {
5246 spa_configfile_set(spa
, props
, B_FALSE
);
5247 spa_sync_props(props
, tx
);
5252 spa
->spa_sync_on
= B_TRUE
;
5254 mmp_thread_start(spa
);
5255 txg_wait_synced(dp
, txg
);
5257 spa_spawn_aux_threads(spa
);
5259 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
);
5262 * Don't count references from objsets that are already closed
5263 * and are making their way through the eviction process.
5265 spa_evicting_os_wait(spa
);
5266 spa
->spa_minref
= zfs_refcount_count(&spa
->spa_refcount
);
5267 spa
->spa_load_state
= SPA_LOAD_NONE
;
5269 mutex_exit(&spa_namespace_lock
);
5275 * Import a non-root pool into the system.
5278 spa_import(char *pool
, nvlist_t
*config
, nvlist_t
*props
, uint64_t flags
)
5281 char *altroot
= NULL
;
5282 spa_load_state_t state
= SPA_LOAD_IMPORT
;
5283 zpool_load_policy_t policy
;
5284 uint64_t mode
= spa_mode_global
;
5285 uint64_t readonly
= B_FALSE
;
5288 nvlist_t
**spares
, **l2cache
;
5289 uint_t nspares
, nl2cache
;
5292 * If a pool with this name exists, return failure.
5294 mutex_enter(&spa_namespace_lock
);
5295 if (spa_lookup(pool
) != NULL
) {
5296 mutex_exit(&spa_namespace_lock
);
5297 return (SET_ERROR(EEXIST
));
5301 * Create and initialize the spa structure.
5303 (void) nvlist_lookup_string(props
,
5304 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
5305 (void) nvlist_lookup_uint64(props
,
5306 zpool_prop_to_name(ZPOOL_PROP_READONLY
), &readonly
);
5309 spa
= spa_add(pool
, config
, altroot
);
5310 spa
->spa_import_flags
= flags
;
5313 * Verbatim import - Take a pool and insert it into the namespace
5314 * as if it had been loaded at boot.
5316 if (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
) {
5318 spa_configfile_set(spa
, props
, B_FALSE
);
5320 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
);
5321 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_IMPORT
);
5322 zfs_dbgmsg("spa_import: verbatim import of %s", pool
);
5323 mutex_exit(&spa_namespace_lock
);
5327 spa_activate(spa
, mode
);
5330 * Don't start async tasks until we know everything is healthy.
5332 spa_async_suspend(spa
);
5334 zpool_get_load_policy(config
, &policy
);
5335 if (policy
.zlp_rewind
& ZPOOL_DO_REWIND
)
5336 state
= SPA_LOAD_RECOVER
;
5338 spa
->spa_config_source
= SPA_CONFIG_SRC_TRYIMPORT
;
5340 if (state
!= SPA_LOAD_RECOVER
) {
5341 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
5342 zfs_dbgmsg("spa_import: importing %s", pool
);
5344 zfs_dbgmsg("spa_import: importing %s, max_txg=%lld "
5345 "(RECOVERY MODE)", pool
, (longlong_t
)policy
.zlp_txg
);
5347 error
= spa_load_best(spa
, state
, policy
.zlp_txg
, policy
.zlp_rewind
);
5350 * Propagate anything learned while loading the pool and pass it
5351 * back to caller (i.e. rewind info, missing devices, etc).
5353 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
5354 spa
->spa_load_info
) == 0);
5356 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5358 * Toss any existing sparelist, as it doesn't have any validity
5359 * anymore, and conflicts with spa_has_spare().
5361 if (spa
->spa_spares
.sav_config
) {
5362 nvlist_free(spa
->spa_spares
.sav_config
);
5363 spa
->spa_spares
.sav_config
= NULL
;
5364 spa_load_spares(spa
);
5366 if (spa
->spa_l2cache
.sav_config
) {
5367 nvlist_free(spa
->spa_l2cache
.sav_config
);
5368 spa
->spa_l2cache
.sav_config
= NULL
;
5369 spa_load_l2cache(spa
);
5372 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
5374 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5377 spa_configfile_set(spa
, props
, B_FALSE
);
5379 if (error
!= 0 || (props
&& spa_writeable(spa
) &&
5380 (error
= spa_prop_set(spa
, props
)))) {
5382 spa_deactivate(spa
);
5384 mutex_exit(&spa_namespace_lock
);
5388 spa_async_resume(spa
);
5391 * Override any spares and level 2 cache devices as specified by
5392 * the user, as these may have correct device names/devids, etc.
5394 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
5395 &spares
, &nspares
) == 0) {
5396 if (spa
->spa_spares
.sav_config
)
5397 VERIFY(nvlist_remove(spa
->spa_spares
.sav_config
,
5398 ZPOOL_CONFIG_SPARES
, DATA_TYPE_NVLIST_ARRAY
) == 0);
5400 VERIFY(nvlist_alloc(&spa
->spa_spares
.sav_config
,
5401 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
5402 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
5403 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
5404 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5405 spa_load_spares(spa
);
5406 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5407 spa
->spa_spares
.sav_sync
= B_TRUE
;
5409 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
5410 &l2cache
, &nl2cache
) == 0) {
5411 if (spa
->spa_l2cache
.sav_config
)
5412 VERIFY(nvlist_remove(spa
->spa_l2cache
.sav_config
,
5413 ZPOOL_CONFIG_L2CACHE
, DATA_TYPE_NVLIST_ARRAY
) == 0);
5415 VERIFY(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
5416 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
5417 VERIFY(nvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
5418 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
5419 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5420 spa_load_l2cache(spa
);
5421 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5422 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
5426 * Check for any removed devices.
5428 if (spa
->spa_autoreplace
) {
5429 spa_aux_check_removed(&spa
->spa_spares
);
5430 spa_aux_check_removed(&spa
->spa_l2cache
);
5433 if (spa_writeable(spa
)) {
5435 * Update the config cache to include the newly-imported pool.
5437 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
5441 * It's possible that the pool was expanded while it was exported.
5442 * We kick off an async task to handle this for us.
5444 spa_async_request(spa
, SPA_ASYNC_AUTOEXPAND
);
5446 spa_history_log_version(spa
, "import", NULL
);
5448 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_IMPORT
);
5450 zvol_create_minors(spa
, pool
, B_TRUE
);
5452 mutex_exit(&spa_namespace_lock
);
5458 spa_tryimport(nvlist_t
*tryconfig
)
5460 nvlist_t
*config
= NULL
;
5461 char *poolname
, *cachefile
;
5465 zpool_load_policy_t policy
;
5467 if (nvlist_lookup_string(tryconfig
, ZPOOL_CONFIG_POOL_NAME
, &poolname
))
5470 if (nvlist_lookup_uint64(tryconfig
, ZPOOL_CONFIG_POOL_STATE
, &state
))
5474 * Create and initialize the spa structure.
5476 mutex_enter(&spa_namespace_lock
);
5477 spa
= spa_add(TRYIMPORT_NAME
, tryconfig
, NULL
);
5478 spa_activate(spa
, FREAD
);
5481 * Rewind pool if a max txg was provided.
5483 zpool_get_load_policy(spa
->spa_config
, &policy
);
5484 if (policy
.zlp_txg
!= UINT64_MAX
) {
5485 spa
->spa_load_max_txg
= policy
.zlp_txg
;
5486 spa
->spa_extreme_rewind
= B_TRUE
;
5487 zfs_dbgmsg("spa_tryimport: importing %s, max_txg=%lld",
5488 poolname
, (longlong_t
)policy
.zlp_txg
);
5490 zfs_dbgmsg("spa_tryimport: importing %s", poolname
);
5493 if (nvlist_lookup_string(tryconfig
, ZPOOL_CONFIG_CACHEFILE
, &cachefile
)
5495 zfs_dbgmsg("spa_tryimport: using cachefile '%s'", cachefile
);
5496 spa
->spa_config_source
= SPA_CONFIG_SRC_CACHEFILE
;
5498 spa
->spa_config_source
= SPA_CONFIG_SRC_SCAN
;
5501 error
= spa_load(spa
, SPA_LOAD_TRYIMPORT
, SPA_IMPORT_EXISTING
);
5504 * If 'tryconfig' was at least parsable, return the current config.
5506 if (spa
->spa_root_vdev
!= NULL
) {
5507 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
5508 VERIFY(nvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
,
5510 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
5512 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_TIMESTAMP
,
5513 spa
->spa_uberblock
.ub_timestamp
) == 0);
5514 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
5515 spa
->spa_load_info
) == 0);
5516 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_ERRATA
,
5517 spa
->spa_errata
) == 0);
5520 * If the bootfs property exists on this pool then we
5521 * copy it out so that external consumers can tell which
5522 * pools are bootable.
5524 if ((!error
|| error
== EEXIST
) && spa
->spa_bootfs
) {
5525 char *tmpname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
5528 * We have to play games with the name since the
5529 * pool was opened as TRYIMPORT_NAME.
5531 if (dsl_dsobj_to_dsname(spa_name(spa
),
5532 spa
->spa_bootfs
, tmpname
) == 0) {
5536 dsname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
5538 cp
= strchr(tmpname
, '/');
5540 (void) strlcpy(dsname
, tmpname
,
5543 (void) snprintf(dsname
, MAXPATHLEN
,
5544 "%s/%s", poolname
, ++cp
);
5546 VERIFY(nvlist_add_string(config
,
5547 ZPOOL_CONFIG_BOOTFS
, dsname
) == 0);
5548 kmem_free(dsname
, MAXPATHLEN
);
5550 kmem_free(tmpname
, MAXPATHLEN
);
5554 * Add the list of hot spares and level 2 cache devices.
5556 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
5557 spa_add_spares(spa
, config
);
5558 spa_add_l2cache(spa
, config
);
5559 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
5563 spa_deactivate(spa
);
5565 mutex_exit(&spa_namespace_lock
);
5571 * Pool export/destroy
5573 * The act of destroying or exporting a pool is very simple. We make sure there
5574 * is no more pending I/O and any references to the pool are gone. Then, we
5575 * update the pool state and sync all the labels to disk, removing the
5576 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
5577 * we don't sync the labels or remove the configuration cache.
5580 spa_export_common(char *pool
, int new_state
, nvlist_t
**oldconfig
,
5581 boolean_t force
, boolean_t hardforce
)
5588 if (!(spa_mode_global
& FWRITE
))
5589 return (SET_ERROR(EROFS
));
5591 mutex_enter(&spa_namespace_lock
);
5592 if ((spa
= spa_lookup(pool
)) == NULL
) {
5593 mutex_exit(&spa_namespace_lock
);
5594 return (SET_ERROR(ENOENT
));
5598 * Put a hold on the pool, drop the namespace lock, stop async tasks,
5599 * reacquire the namespace lock, and see if we can export.
5601 spa_open_ref(spa
, FTAG
);
5602 mutex_exit(&spa_namespace_lock
);
5603 spa_async_suspend(spa
);
5604 if (spa
->spa_zvol_taskq
) {
5605 zvol_remove_minors(spa
, spa_name(spa
), B_TRUE
);
5606 taskq_wait(spa
->spa_zvol_taskq
);
5608 mutex_enter(&spa_namespace_lock
);
5609 spa_close(spa
, FTAG
);
5611 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
)
5614 * The pool will be in core if it's openable, in which case we can
5615 * modify its state. Objsets may be open only because they're dirty,
5616 * so we have to force it to sync before checking spa_refcnt.
5618 if (spa
->spa_sync_on
) {
5619 txg_wait_synced(spa
->spa_dsl_pool
, 0);
5620 spa_evicting_os_wait(spa
);
5624 * A pool cannot be exported or destroyed if there are active
5625 * references. If we are resetting a pool, allow references by
5626 * fault injection handlers.
5628 if (!spa_refcount_zero(spa
) ||
5629 (spa
->spa_inject_ref
!= 0 &&
5630 new_state
!= POOL_STATE_UNINITIALIZED
)) {
5631 spa_async_resume(spa
);
5632 mutex_exit(&spa_namespace_lock
);
5633 return (SET_ERROR(EBUSY
));
5636 if (spa
->spa_sync_on
) {
5638 * A pool cannot be exported if it has an active shared spare.
5639 * This is to prevent other pools stealing the active spare
5640 * from an exported pool. At user's own will, such pool can
5641 * be forcedly exported.
5643 if (!force
&& new_state
== POOL_STATE_EXPORTED
&&
5644 spa_has_active_shared_spare(spa
)) {
5645 spa_async_resume(spa
);
5646 mutex_exit(&spa_namespace_lock
);
5647 return (SET_ERROR(EXDEV
));
5651 * We want this to be reflected on every label,
5652 * so mark them all dirty. spa_unload() will do the
5653 * final sync that pushes these changes out.
5655 if (new_state
!= POOL_STATE_UNINITIALIZED
&& !hardforce
) {
5656 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5657 spa
->spa_state
= new_state
;
5658 spa
->spa_final_txg
= spa_last_synced_txg(spa
) +
5660 vdev_config_dirty(spa
->spa_root_vdev
);
5661 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5666 if (new_state
== POOL_STATE_DESTROYED
)
5667 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_DESTROY
);
5668 else if (new_state
== POOL_STATE_EXPORTED
)
5669 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_EXPORT
);
5671 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
5673 spa_deactivate(spa
);
5676 if (oldconfig
&& spa
->spa_config
)
5677 VERIFY(nvlist_dup(spa
->spa_config
, oldconfig
, 0) == 0);
5679 if (new_state
!= POOL_STATE_UNINITIALIZED
) {
5681 spa_write_cachefile(spa
, B_TRUE
, B_TRUE
);
5684 mutex_exit(&spa_namespace_lock
);
5690 * Destroy a storage pool.
5693 spa_destroy(char *pool
)
5695 return (spa_export_common(pool
, POOL_STATE_DESTROYED
, NULL
,
5700 * Export a storage pool.
5703 spa_export(char *pool
, nvlist_t
**oldconfig
, boolean_t force
,
5704 boolean_t hardforce
)
5706 return (spa_export_common(pool
, POOL_STATE_EXPORTED
, oldconfig
,
5711 * Similar to spa_export(), this unloads the spa_t without actually removing it
5712 * from the namespace in any way.
5715 spa_reset(char *pool
)
5717 return (spa_export_common(pool
, POOL_STATE_UNINITIALIZED
, NULL
,
5722 * ==========================================================================
5723 * Device manipulation
5724 * ==========================================================================
5728 * Add a device to a storage pool.
5731 spa_vdev_add(spa_t
*spa
, nvlist_t
*nvroot
)
5735 vdev_t
*rvd
= spa
->spa_root_vdev
;
5737 nvlist_t
**spares
, **l2cache
;
5738 uint_t nspares
, nl2cache
;
5740 ASSERT(spa_writeable(spa
));
5742 txg
= spa_vdev_enter(spa
);
5744 if ((error
= spa_config_parse(spa
, &vd
, nvroot
, NULL
, 0,
5745 VDEV_ALLOC_ADD
)) != 0)
5746 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5748 spa
->spa_pending_vdev
= vd
; /* spa_vdev_exit() will clear this */
5750 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
, &spares
,
5754 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
, &l2cache
,
5758 if (vd
->vdev_children
== 0 && nspares
== 0 && nl2cache
== 0)
5759 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
5761 if (vd
->vdev_children
!= 0 &&
5762 (error
= vdev_create(vd
, txg
, B_FALSE
)) != 0)
5763 return (spa_vdev_exit(spa
, vd
, txg
, error
));
5766 * We must validate the spares and l2cache devices after checking the
5767 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
5769 if ((error
= spa_validate_aux(spa
, nvroot
, txg
, VDEV_ALLOC_ADD
)) != 0)
5770 return (spa_vdev_exit(spa
, vd
, txg
, error
));
5773 * If we are in the middle of a device removal, we can only add
5774 * devices which match the existing devices in the pool.
5775 * If we are in the middle of a removal, or have some indirect
5776 * vdevs, we can not add raidz toplevels.
5778 if (spa
->spa_vdev_removal
!= NULL
||
5779 spa
->spa_removing_phys
.sr_prev_indirect_vdev
!= -1) {
5780 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
5781 tvd
= vd
->vdev_child
[c
];
5782 if (spa
->spa_vdev_removal
!= NULL
&&
5783 tvd
->vdev_ashift
!= spa
->spa_max_ashift
) {
5784 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
5786 /* Fail if top level vdev is raidz */
5787 if (tvd
->vdev_ops
== &vdev_raidz_ops
) {
5788 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
5791 * Need the top level mirror to be
5792 * a mirror of leaf vdevs only
5794 if (tvd
->vdev_ops
== &vdev_mirror_ops
) {
5795 for (uint64_t cid
= 0;
5796 cid
< tvd
->vdev_children
; cid
++) {
5797 vdev_t
*cvd
= tvd
->vdev_child
[cid
];
5798 if (!cvd
->vdev_ops
->vdev_op_leaf
) {
5799 return (spa_vdev_exit(spa
, vd
,
5807 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
5810 * Set the vdev id to the first hole, if one exists.
5812 for (id
= 0; id
< rvd
->vdev_children
; id
++) {
5813 if (rvd
->vdev_child
[id
]->vdev_ishole
) {
5814 vdev_free(rvd
->vdev_child
[id
]);
5818 tvd
= vd
->vdev_child
[c
];
5819 vdev_remove_child(vd
, tvd
);
5821 vdev_add_child(rvd
, tvd
);
5822 vdev_config_dirty(tvd
);
5826 spa_set_aux_vdevs(&spa
->spa_spares
, spares
, nspares
,
5827 ZPOOL_CONFIG_SPARES
);
5828 spa_load_spares(spa
);
5829 spa
->spa_spares
.sav_sync
= B_TRUE
;
5832 if (nl2cache
!= 0) {
5833 spa_set_aux_vdevs(&spa
->spa_l2cache
, l2cache
, nl2cache
,
5834 ZPOOL_CONFIG_L2CACHE
);
5835 spa_load_l2cache(spa
);
5836 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
5840 * We have to be careful when adding new vdevs to an existing pool.
5841 * If other threads start allocating from these vdevs before we
5842 * sync the config cache, and we lose power, then upon reboot we may
5843 * fail to open the pool because there are DVAs that the config cache
5844 * can't translate. Therefore, we first add the vdevs without
5845 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
5846 * and then let spa_config_update() initialize the new metaslabs.
5848 * spa_load() checks for added-but-not-initialized vdevs, so that
5849 * if we lose power at any point in this sequence, the remaining
5850 * steps will be completed the next time we load the pool.
5852 (void) spa_vdev_exit(spa
, vd
, txg
, 0);
5854 mutex_enter(&spa_namespace_lock
);
5855 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
5856 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_VDEV_ADD
);
5857 mutex_exit(&spa_namespace_lock
);
5863 * Attach a device to a mirror. The arguments are the path to any device
5864 * in the mirror, and the nvroot for the new device. If the path specifies
5865 * a device that is not mirrored, we automatically insert the mirror vdev.
5867 * If 'replacing' is specified, the new device is intended to replace the
5868 * existing device; in this case the two devices are made into their own
5869 * mirror using the 'replacing' vdev, which is functionally identical to
5870 * the mirror vdev (it actually reuses all the same ops) but has a few
5871 * extra rules: you can't attach to it after it's been created, and upon
5872 * completion of resilvering, the first disk (the one being replaced)
5873 * is automatically detached.
5876 spa_vdev_attach(spa_t
*spa
, uint64_t guid
, nvlist_t
*nvroot
, int replacing
)
5878 uint64_t txg
, dtl_max_txg
;
5879 ASSERTV(vdev_t
*rvd
= spa
->spa_root_vdev
);
5880 vdev_t
*oldvd
, *newvd
, *newrootvd
, *pvd
, *tvd
;
5882 char *oldvdpath
, *newvdpath
;
5886 ASSERT(spa_writeable(spa
));
5888 txg
= spa_vdev_enter(spa
);
5890 oldvd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
5892 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
5893 if (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
)) {
5894 error
= (spa_has_checkpoint(spa
)) ?
5895 ZFS_ERR_CHECKPOINT_EXISTS
: ZFS_ERR_DISCARDING_CHECKPOINT
;
5896 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5899 if (spa
->spa_vdev_removal
!= NULL
)
5900 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
5903 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
5905 if (!oldvd
->vdev_ops
->vdev_op_leaf
)
5906 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
5908 pvd
= oldvd
->vdev_parent
;
5910 if ((error
= spa_config_parse(spa
, &newrootvd
, nvroot
, NULL
, 0,
5911 VDEV_ALLOC_ATTACH
)) != 0)
5912 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
5914 if (newrootvd
->vdev_children
!= 1)
5915 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
5917 newvd
= newrootvd
->vdev_child
[0];
5919 if (!newvd
->vdev_ops
->vdev_op_leaf
)
5920 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
5922 if ((error
= vdev_create(newrootvd
, txg
, replacing
)) != 0)
5923 return (spa_vdev_exit(spa
, newrootvd
, txg
, error
));
5926 * Spares can't replace logs
5928 if (oldvd
->vdev_top
->vdev_islog
&& newvd
->vdev_isspare
)
5929 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
5933 * For attach, the only allowable parent is a mirror or the root
5936 if (pvd
->vdev_ops
!= &vdev_mirror_ops
&&
5937 pvd
->vdev_ops
!= &vdev_root_ops
)
5938 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
5940 pvops
= &vdev_mirror_ops
;
5943 * Active hot spares can only be replaced by inactive hot
5946 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
5947 oldvd
->vdev_isspare
&&
5948 !spa_has_spare(spa
, newvd
->vdev_guid
))
5949 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
5952 * If the source is a hot spare, and the parent isn't already a
5953 * spare, then we want to create a new hot spare. Otherwise, we
5954 * want to create a replacing vdev. The user is not allowed to
5955 * attach to a spared vdev child unless the 'isspare' state is
5956 * the same (spare replaces spare, non-spare replaces
5959 if (pvd
->vdev_ops
== &vdev_replacing_ops
&&
5960 spa_version(spa
) < SPA_VERSION_MULTI_REPLACE
) {
5961 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
5962 } else if (pvd
->vdev_ops
== &vdev_spare_ops
&&
5963 newvd
->vdev_isspare
!= oldvd
->vdev_isspare
) {
5964 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
5967 if (newvd
->vdev_isspare
)
5968 pvops
= &vdev_spare_ops
;
5970 pvops
= &vdev_replacing_ops
;
5974 * Make sure the new device is big enough.
5976 if (newvd
->vdev_asize
< vdev_get_min_asize(oldvd
))
5977 return (spa_vdev_exit(spa
, newrootvd
, txg
, EOVERFLOW
));
5980 * The new device cannot have a higher alignment requirement
5981 * than the top-level vdev.
5983 if (newvd
->vdev_ashift
> oldvd
->vdev_top
->vdev_ashift
)
5984 return (spa_vdev_exit(spa
, newrootvd
, txg
, EDOM
));
5987 * If this is an in-place replacement, update oldvd's path and devid
5988 * to make it distinguishable from newvd, and unopenable from now on.
5990 if (strcmp(oldvd
->vdev_path
, newvd
->vdev_path
) == 0) {
5991 spa_strfree(oldvd
->vdev_path
);
5992 oldvd
->vdev_path
= kmem_alloc(strlen(newvd
->vdev_path
) + 5,
5994 (void) sprintf(oldvd
->vdev_path
, "%s/%s",
5995 newvd
->vdev_path
, "old");
5996 if (oldvd
->vdev_devid
!= NULL
) {
5997 spa_strfree(oldvd
->vdev_devid
);
5998 oldvd
->vdev_devid
= NULL
;
6002 /* mark the device being resilvered */
6003 newvd
->vdev_resilver_txg
= txg
;
6006 * If the parent is not a mirror, or if we're replacing, insert the new
6007 * mirror/replacing/spare vdev above oldvd.
6009 if (pvd
->vdev_ops
!= pvops
)
6010 pvd
= vdev_add_parent(oldvd
, pvops
);
6012 ASSERT(pvd
->vdev_top
->vdev_parent
== rvd
);
6013 ASSERT(pvd
->vdev_ops
== pvops
);
6014 ASSERT(oldvd
->vdev_parent
== pvd
);
6017 * Extract the new device from its root and add it to pvd.
6019 vdev_remove_child(newrootvd
, newvd
);
6020 newvd
->vdev_id
= pvd
->vdev_children
;
6021 newvd
->vdev_crtxg
= oldvd
->vdev_crtxg
;
6022 vdev_add_child(pvd
, newvd
);
6025 * Reevaluate the parent vdev state.
6027 vdev_propagate_state(pvd
);
6029 tvd
= newvd
->vdev_top
;
6030 ASSERT(pvd
->vdev_top
== tvd
);
6031 ASSERT(tvd
->vdev_parent
== rvd
);
6033 vdev_config_dirty(tvd
);
6036 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
6037 * for any dmu_sync-ed blocks. It will propagate upward when
6038 * spa_vdev_exit() calls vdev_dtl_reassess().
6040 dtl_max_txg
= txg
+ TXG_CONCURRENT_STATES
;
6042 vdev_dtl_dirty(newvd
, DTL_MISSING
, TXG_INITIAL
,
6043 dtl_max_txg
- TXG_INITIAL
);
6045 if (newvd
->vdev_isspare
) {
6046 spa_spare_activate(newvd
);
6047 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_VDEV_SPARE
);
6050 oldvdpath
= spa_strdup(oldvd
->vdev_path
);
6051 newvdpath
= spa_strdup(newvd
->vdev_path
);
6052 newvd_isspare
= newvd
->vdev_isspare
;
6055 * Mark newvd's DTL dirty in this txg.
6057 vdev_dirty(tvd
, VDD_DTL
, newvd
, txg
);
6060 * Schedule the resilver to restart in the future. We do this to
6061 * ensure that dmu_sync-ed blocks have been stitched into the
6062 * respective datasets.
6064 dsl_resilver_restart(spa
->spa_dsl_pool
, dtl_max_txg
);
6066 if (spa
->spa_bootfs
)
6067 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_BOOTFS_VDEV_ATTACH
);
6069 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_VDEV_ATTACH
);
6074 (void) spa_vdev_exit(spa
, newrootvd
, dtl_max_txg
, 0);
6076 spa_history_log_internal(spa
, "vdev attach", NULL
,
6077 "%s vdev=%s %s vdev=%s",
6078 replacing
&& newvd_isspare
? "spare in" :
6079 replacing
? "replace" : "attach", newvdpath
,
6080 replacing
? "for" : "to", oldvdpath
);
6082 spa_strfree(oldvdpath
);
6083 spa_strfree(newvdpath
);
6089 * Detach a device from a mirror or replacing vdev.
6091 * If 'replace_done' is specified, only detach if the parent
6092 * is a replacing vdev.
6095 spa_vdev_detach(spa_t
*spa
, uint64_t guid
, uint64_t pguid
, int replace_done
)
6099 ASSERTV(vdev_t
*rvd
= spa
->spa_root_vdev
);
6100 vdev_t
*vd
, *pvd
, *cvd
, *tvd
;
6101 boolean_t unspare
= B_FALSE
;
6102 uint64_t unspare_guid
= 0;
6105 ASSERT(spa_writeable(spa
));
6107 txg
= spa_vdev_enter(spa
);
6109 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
6112 * Besides being called directly from the userland through the
6113 * ioctl interface, spa_vdev_detach() can be potentially called
6114 * at the end of spa_vdev_resilver_done().
6116 * In the regular case, when we have a checkpoint this shouldn't
6117 * happen as we never empty the DTLs of a vdev during the scrub
6118 * [see comment in dsl_scan_done()]. Thus spa_vdev_resilvering_done()
6119 * should never get here when we have a checkpoint.
6121 * That said, even in a case when we checkpoint the pool exactly
6122 * as spa_vdev_resilver_done() calls this function everything
6123 * should be fine as the resilver will return right away.
6125 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
6126 if (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
)) {
6127 error
= (spa_has_checkpoint(spa
)) ?
6128 ZFS_ERR_CHECKPOINT_EXISTS
: ZFS_ERR_DISCARDING_CHECKPOINT
;
6129 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
6133 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
6135 if (!vd
->vdev_ops
->vdev_op_leaf
)
6136 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
6138 pvd
= vd
->vdev_parent
;
6141 * If the parent/child relationship is not as expected, don't do it.
6142 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
6143 * vdev that's replacing B with C. The user's intent in replacing
6144 * is to go from M(A,B) to M(A,C). If the user decides to cancel
6145 * the replace by detaching C, the expected behavior is to end up
6146 * M(A,B). But suppose that right after deciding to detach C,
6147 * the replacement of B completes. We would have M(A,C), and then
6148 * ask to detach C, which would leave us with just A -- not what
6149 * the user wanted. To prevent this, we make sure that the
6150 * parent/child relationship hasn't changed -- in this example,
6151 * that C's parent is still the replacing vdev R.
6153 if (pvd
->vdev_guid
!= pguid
&& pguid
!= 0)
6154 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
6157 * Only 'replacing' or 'spare' vdevs can be replaced.
6159 if (replace_done
&& pvd
->vdev_ops
!= &vdev_replacing_ops
&&
6160 pvd
->vdev_ops
!= &vdev_spare_ops
)
6161 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
6163 ASSERT(pvd
->vdev_ops
!= &vdev_spare_ops
||
6164 spa_version(spa
) >= SPA_VERSION_SPARES
);
6167 * Only mirror, replacing, and spare vdevs support detach.
6169 if (pvd
->vdev_ops
!= &vdev_replacing_ops
&&
6170 pvd
->vdev_ops
!= &vdev_mirror_ops
&&
6171 pvd
->vdev_ops
!= &vdev_spare_ops
)
6172 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
6175 * If this device has the only valid copy of some data,
6176 * we cannot safely detach it.
6178 if (vdev_dtl_required(vd
))
6179 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
6181 ASSERT(pvd
->vdev_children
>= 2);
6184 * If we are detaching the second disk from a replacing vdev, then
6185 * check to see if we changed the original vdev's path to have "/old"
6186 * at the end in spa_vdev_attach(). If so, undo that change now.
6188 if (pvd
->vdev_ops
== &vdev_replacing_ops
&& vd
->vdev_id
> 0 &&
6189 vd
->vdev_path
!= NULL
) {
6190 size_t len
= strlen(vd
->vdev_path
);
6192 for (int c
= 0; c
< pvd
->vdev_children
; c
++) {
6193 cvd
= pvd
->vdev_child
[c
];
6195 if (cvd
== vd
|| cvd
->vdev_path
== NULL
)
6198 if (strncmp(cvd
->vdev_path
, vd
->vdev_path
, len
) == 0 &&
6199 strcmp(cvd
->vdev_path
+ len
, "/old") == 0) {
6200 spa_strfree(cvd
->vdev_path
);
6201 cvd
->vdev_path
= spa_strdup(vd
->vdev_path
);
6208 * If we are detaching the original disk from a spare, then it implies
6209 * that the spare should become a real disk, and be removed from the
6210 * active spare list for the pool.
6212 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
6214 pvd
->vdev_child
[pvd
->vdev_children
- 1]->vdev_isspare
)
6218 * Erase the disk labels so the disk can be used for other things.
6219 * This must be done after all other error cases are handled,
6220 * but before we disembowel vd (so we can still do I/O to it).
6221 * But if we can't do it, don't treat the error as fatal --
6222 * it may be that the unwritability of the disk is the reason
6223 * it's being detached!
6225 error
= vdev_label_init(vd
, 0, VDEV_LABEL_REMOVE
);
6228 * Remove vd from its parent and compact the parent's children.
6230 vdev_remove_child(pvd
, vd
);
6231 vdev_compact_children(pvd
);
6234 * Remember one of the remaining children so we can get tvd below.
6236 cvd
= pvd
->vdev_child
[pvd
->vdev_children
- 1];
6239 * If we need to remove the remaining child from the list of hot spares,
6240 * do it now, marking the vdev as no longer a spare in the process.
6241 * We must do this before vdev_remove_parent(), because that can
6242 * change the GUID if it creates a new toplevel GUID. For a similar
6243 * reason, we must remove the spare now, in the same txg as the detach;
6244 * otherwise someone could attach a new sibling, change the GUID, and
6245 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
6248 ASSERT(cvd
->vdev_isspare
);
6249 spa_spare_remove(cvd
);
6250 unspare_guid
= cvd
->vdev_guid
;
6251 (void) spa_vdev_remove(spa
, unspare_guid
, B_TRUE
);
6252 cvd
->vdev_unspare
= B_TRUE
;
6256 * If the parent mirror/replacing vdev only has one child,
6257 * the parent is no longer needed. Remove it from the tree.
6259 if (pvd
->vdev_children
== 1) {
6260 if (pvd
->vdev_ops
== &vdev_spare_ops
)
6261 cvd
->vdev_unspare
= B_FALSE
;
6262 vdev_remove_parent(cvd
);
6267 * We don't set tvd until now because the parent we just removed
6268 * may have been the previous top-level vdev.
6270 tvd
= cvd
->vdev_top
;
6271 ASSERT(tvd
->vdev_parent
== rvd
);
6274 * Reevaluate the parent vdev state.
6276 vdev_propagate_state(cvd
);
6279 * If the 'autoexpand' property is set on the pool then automatically
6280 * try to expand the size of the pool. For example if the device we
6281 * just detached was smaller than the others, it may be possible to
6282 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
6283 * first so that we can obtain the updated sizes of the leaf vdevs.
6285 if (spa
->spa_autoexpand
) {
6287 vdev_expand(tvd
, txg
);
6290 vdev_config_dirty(tvd
);
6293 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
6294 * vd->vdev_detached is set and free vd's DTL object in syncing context.
6295 * But first make sure we're not on any *other* txg's DTL list, to
6296 * prevent vd from being accessed after it's freed.
6298 vdpath
= spa_strdup(vd
->vdev_path
? vd
->vdev_path
: "none");
6299 for (int t
= 0; t
< TXG_SIZE
; t
++)
6300 (void) txg_list_remove_this(&tvd
->vdev_dtl_list
, vd
, t
);
6301 vd
->vdev_detached
= B_TRUE
;
6302 vdev_dirty(tvd
, VDD_DTL
, vd
, txg
);
6304 spa_event_notify(spa
, vd
, NULL
, ESC_ZFS_VDEV_REMOVE
);
6306 /* hang on to the spa before we release the lock */
6307 spa_open_ref(spa
, FTAG
);
6309 error
= spa_vdev_exit(spa
, vd
, txg
, 0);
6311 spa_history_log_internal(spa
, "detach", NULL
,
6313 spa_strfree(vdpath
);
6316 * If this was the removal of the original device in a hot spare vdev,
6317 * then we want to go through and remove the device from the hot spare
6318 * list of every other pool.
6321 spa_t
*altspa
= NULL
;
6323 mutex_enter(&spa_namespace_lock
);
6324 while ((altspa
= spa_next(altspa
)) != NULL
) {
6325 if (altspa
->spa_state
!= POOL_STATE_ACTIVE
||
6329 spa_open_ref(altspa
, FTAG
);
6330 mutex_exit(&spa_namespace_lock
);
6331 (void) spa_vdev_remove(altspa
, unspare_guid
, B_TRUE
);
6332 mutex_enter(&spa_namespace_lock
);
6333 spa_close(altspa
, FTAG
);
6335 mutex_exit(&spa_namespace_lock
);
6337 /* search the rest of the vdevs for spares to remove */
6338 spa_vdev_resilver_done(spa
);
6341 /* all done with the spa; OK to release */
6342 mutex_enter(&spa_namespace_lock
);
6343 spa_close(spa
, FTAG
);
6344 mutex_exit(&spa_namespace_lock
);
6350 * Split a set of devices from their mirrors, and create a new pool from them.
6353 spa_vdev_split_mirror(spa_t
*spa
, char *newname
, nvlist_t
*config
,
6354 nvlist_t
*props
, boolean_t exp
)
6357 uint64_t txg
, *glist
;
6359 uint_t c
, children
, lastlog
;
6360 nvlist_t
**child
, *nvl
, *tmp
;
6362 char *altroot
= NULL
;
6363 vdev_t
*rvd
, **vml
= NULL
; /* vdev modify list */
6364 boolean_t activate_slog
;
6366 ASSERT(spa_writeable(spa
));
6368 txg
= spa_vdev_enter(spa
);
6370 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
6371 if (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
)) {
6372 error
= (spa_has_checkpoint(spa
)) ?
6373 ZFS_ERR_CHECKPOINT_EXISTS
: ZFS_ERR_DISCARDING_CHECKPOINT
;
6374 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
6377 /* clear the log and flush everything up to now */
6378 activate_slog
= spa_passivate_log(spa
);
6379 (void) spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
6380 error
= spa_reset_logs(spa
);
6381 txg
= spa_vdev_config_enter(spa
);
6384 spa_activate_log(spa
);
6387 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
6389 /* check new spa name before going any further */
6390 if (spa_lookup(newname
) != NULL
)
6391 return (spa_vdev_exit(spa
, NULL
, txg
, EEXIST
));
6394 * scan through all the children to ensure they're all mirrors
6396 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvl
) != 0 ||
6397 nvlist_lookup_nvlist_array(nvl
, ZPOOL_CONFIG_CHILDREN
, &child
,
6399 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
6401 /* first, check to ensure we've got the right child count */
6402 rvd
= spa
->spa_root_vdev
;
6404 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
6405 vdev_t
*vd
= rvd
->vdev_child
[c
];
6407 /* don't count the holes & logs as children */
6408 if (vd
->vdev_islog
|| !vdev_is_concrete(vd
)) {
6416 if (children
!= (lastlog
!= 0 ? lastlog
: rvd
->vdev_children
))
6417 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
6419 /* next, ensure no spare or cache devices are part of the split */
6420 if (nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_SPARES
, &tmp
) == 0 ||
6421 nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_L2CACHE
, &tmp
) == 0)
6422 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
6424 vml
= kmem_zalloc(children
* sizeof (vdev_t
*), KM_SLEEP
);
6425 glist
= kmem_zalloc(children
* sizeof (uint64_t), KM_SLEEP
);
6427 /* then, loop over each vdev and validate it */
6428 for (c
= 0; c
< children
; c
++) {
6429 uint64_t is_hole
= 0;
6431 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
6435 if (spa
->spa_root_vdev
->vdev_child
[c
]->vdev_ishole
||
6436 spa
->spa_root_vdev
->vdev_child
[c
]->vdev_islog
) {
6439 error
= SET_ERROR(EINVAL
);
6444 /* which disk is going to be split? */
6445 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_GUID
,
6447 error
= SET_ERROR(EINVAL
);
6451 /* look it up in the spa */
6452 vml
[c
] = spa_lookup_by_guid(spa
, glist
[c
], B_FALSE
);
6453 if (vml
[c
] == NULL
) {
6454 error
= SET_ERROR(ENODEV
);
6458 /* make sure there's nothing stopping the split */
6459 if (vml
[c
]->vdev_parent
->vdev_ops
!= &vdev_mirror_ops
||
6460 vml
[c
]->vdev_islog
||
6461 !vdev_is_concrete(vml
[c
]) ||
6462 vml
[c
]->vdev_isspare
||
6463 vml
[c
]->vdev_isl2cache
||
6464 !vdev_writeable(vml
[c
]) ||
6465 vml
[c
]->vdev_children
!= 0 ||
6466 vml
[c
]->vdev_state
!= VDEV_STATE_HEALTHY
||
6467 c
!= spa
->spa_root_vdev
->vdev_child
[c
]->vdev_id
) {
6468 error
= SET_ERROR(EINVAL
);
6472 if (vdev_dtl_required(vml
[c
]) ||
6473 vdev_resilver_needed(vml
[c
], NULL
, NULL
)) {
6474 error
= SET_ERROR(EBUSY
);
6478 /* we need certain info from the top level */
6479 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_ARRAY
,
6480 vml
[c
]->vdev_top
->vdev_ms_array
) == 0);
6481 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_SHIFT
,
6482 vml
[c
]->vdev_top
->vdev_ms_shift
) == 0);
6483 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASIZE
,
6484 vml
[c
]->vdev_top
->vdev_asize
) == 0);
6485 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASHIFT
,
6486 vml
[c
]->vdev_top
->vdev_ashift
) == 0);
6488 /* transfer per-vdev ZAPs */
6489 ASSERT3U(vml
[c
]->vdev_leaf_zap
, !=, 0);
6490 VERIFY0(nvlist_add_uint64(child
[c
],
6491 ZPOOL_CONFIG_VDEV_LEAF_ZAP
, vml
[c
]->vdev_leaf_zap
));
6493 ASSERT3U(vml
[c
]->vdev_top
->vdev_top_zap
, !=, 0);
6494 VERIFY0(nvlist_add_uint64(child
[c
],
6495 ZPOOL_CONFIG_VDEV_TOP_ZAP
,
6496 vml
[c
]->vdev_parent
->vdev_top_zap
));
6500 kmem_free(vml
, children
* sizeof (vdev_t
*));
6501 kmem_free(glist
, children
* sizeof (uint64_t));
6502 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
6505 /* stop writers from using the disks */
6506 for (c
= 0; c
< children
; c
++) {
6508 vml
[c
]->vdev_offline
= B_TRUE
;
6510 vdev_reopen(spa
->spa_root_vdev
);
6513 * Temporarily record the splitting vdevs in the spa config. This
6514 * will disappear once the config is regenerated.
6516 VERIFY(nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
6517 VERIFY(nvlist_add_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
6518 glist
, children
) == 0);
6519 kmem_free(glist
, children
* sizeof (uint64_t));
6521 mutex_enter(&spa
->spa_props_lock
);
6522 VERIFY(nvlist_add_nvlist(spa
->spa_config
, ZPOOL_CONFIG_SPLIT
,
6524 mutex_exit(&spa
->spa_props_lock
);
6525 spa
->spa_config_splitting
= nvl
;
6526 vdev_config_dirty(spa
->spa_root_vdev
);
6528 /* configure and create the new pool */
6529 VERIFY(nvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
, newname
) == 0);
6530 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
6531 exp
? POOL_STATE_EXPORTED
: POOL_STATE_ACTIVE
) == 0);
6532 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
6533 spa_version(spa
)) == 0);
6534 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
6535 spa
->spa_config_txg
) == 0);
6536 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
6537 spa_generate_guid(NULL
)) == 0);
6538 VERIFY0(nvlist_add_boolean(config
, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
));
6539 (void) nvlist_lookup_string(props
,
6540 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
6542 /* add the new pool to the namespace */
6543 newspa
= spa_add(newname
, config
, altroot
);
6544 newspa
->spa_avz_action
= AVZ_ACTION_REBUILD
;
6545 newspa
->spa_config_txg
= spa
->spa_config_txg
;
6546 spa_set_log_state(newspa
, SPA_LOG_CLEAR
);
6548 /* release the spa config lock, retaining the namespace lock */
6549 spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
6551 if (zio_injection_enabled
)
6552 zio_handle_panic_injection(spa
, FTAG
, 1);
6554 spa_activate(newspa
, spa_mode_global
);
6555 spa_async_suspend(newspa
);
6557 newspa
->spa_config_source
= SPA_CONFIG_SRC_SPLIT
;
6559 /* create the new pool from the disks of the original pool */
6560 error
= spa_load(newspa
, SPA_LOAD_IMPORT
, SPA_IMPORT_ASSEMBLE
);
6564 /* if that worked, generate a real config for the new pool */
6565 if (newspa
->spa_root_vdev
!= NULL
) {
6566 VERIFY(nvlist_alloc(&newspa
->spa_config_splitting
,
6567 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
6568 VERIFY(nvlist_add_uint64(newspa
->spa_config_splitting
,
6569 ZPOOL_CONFIG_SPLIT_GUID
, spa_guid(spa
)) == 0);
6570 spa_config_set(newspa
, spa_config_generate(newspa
, NULL
, -1ULL,
6575 if (props
!= NULL
) {
6576 spa_configfile_set(newspa
, props
, B_FALSE
);
6577 error
= spa_prop_set(newspa
, props
);
6582 /* flush everything */
6583 txg
= spa_vdev_config_enter(newspa
);
6584 vdev_config_dirty(newspa
->spa_root_vdev
);
6585 (void) spa_vdev_config_exit(newspa
, NULL
, txg
, 0, FTAG
);
6587 if (zio_injection_enabled
)
6588 zio_handle_panic_injection(spa
, FTAG
, 2);
6590 spa_async_resume(newspa
);
6592 /* finally, update the original pool's config */
6593 txg
= spa_vdev_config_enter(spa
);
6594 tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
6595 error
= dmu_tx_assign(tx
, TXG_WAIT
);
6598 for (c
= 0; c
< children
; c
++) {
6599 if (vml
[c
] != NULL
) {
6602 spa_history_log_internal(spa
, "detach", tx
,
6603 "vdev=%s", vml
[c
]->vdev_path
);
6608 spa
->spa_avz_action
= AVZ_ACTION_REBUILD
;
6609 vdev_config_dirty(spa
->spa_root_vdev
);
6610 spa
->spa_config_splitting
= NULL
;
6614 (void) spa_vdev_exit(spa
, NULL
, txg
, 0);
6616 if (zio_injection_enabled
)
6617 zio_handle_panic_injection(spa
, FTAG
, 3);
6619 /* split is complete; log a history record */
6620 spa_history_log_internal(newspa
, "split", NULL
,
6621 "from pool %s", spa_name(spa
));
6623 kmem_free(vml
, children
* sizeof (vdev_t
*));
6625 /* if we're not going to mount the filesystems in userland, export */
6627 error
= spa_export_common(newname
, POOL_STATE_EXPORTED
, NULL
,
6634 spa_deactivate(newspa
);
6637 txg
= spa_vdev_config_enter(spa
);
6639 /* re-online all offlined disks */
6640 for (c
= 0; c
< children
; c
++) {
6642 vml
[c
]->vdev_offline
= B_FALSE
;
6644 vdev_reopen(spa
->spa_root_vdev
);
6646 nvlist_free(spa
->spa_config_splitting
);
6647 spa
->spa_config_splitting
= NULL
;
6648 (void) spa_vdev_exit(spa
, NULL
, txg
, error
);
6650 kmem_free(vml
, children
* sizeof (vdev_t
*));
6655 * Find any device that's done replacing, or a vdev marked 'unspare' that's
6656 * currently spared, so we can detach it.
6659 spa_vdev_resilver_done_hunt(vdev_t
*vd
)
6661 vdev_t
*newvd
, *oldvd
;
6663 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
6664 oldvd
= spa_vdev_resilver_done_hunt(vd
->vdev_child
[c
]);
6670 * Check for a completed replacement. We always consider the first
6671 * vdev in the list to be the oldest vdev, and the last one to be
6672 * the newest (see spa_vdev_attach() for how that works). In
6673 * the case where the newest vdev is faulted, we will not automatically
6674 * remove it after a resilver completes. This is OK as it will require
6675 * user intervention to determine which disk the admin wishes to keep.
6677 if (vd
->vdev_ops
== &vdev_replacing_ops
) {
6678 ASSERT(vd
->vdev_children
> 1);
6680 newvd
= vd
->vdev_child
[vd
->vdev_children
- 1];
6681 oldvd
= vd
->vdev_child
[0];
6683 if (vdev_dtl_empty(newvd
, DTL_MISSING
) &&
6684 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
6685 !vdev_dtl_required(oldvd
))
6690 * Check for a completed resilver with the 'unspare' flag set.
6691 * Also potentially update faulted state.
6693 if (vd
->vdev_ops
== &vdev_spare_ops
) {
6694 vdev_t
*first
= vd
->vdev_child
[0];
6695 vdev_t
*last
= vd
->vdev_child
[vd
->vdev_children
- 1];
6697 if (last
->vdev_unspare
) {
6700 } else if (first
->vdev_unspare
) {
6707 if (oldvd
!= NULL
&&
6708 vdev_dtl_empty(newvd
, DTL_MISSING
) &&
6709 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
6710 !vdev_dtl_required(oldvd
))
6713 vdev_propagate_state(vd
);
6716 * If there are more than two spares attached to a disk,
6717 * and those spares are not required, then we want to
6718 * attempt to free them up now so that they can be used
6719 * by other pools. Once we're back down to a single
6720 * disk+spare, we stop removing them.
6722 if (vd
->vdev_children
> 2) {
6723 newvd
= vd
->vdev_child
[1];
6725 if (newvd
->vdev_isspare
&& last
->vdev_isspare
&&
6726 vdev_dtl_empty(last
, DTL_MISSING
) &&
6727 vdev_dtl_empty(last
, DTL_OUTAGE
) &&
6728 !vdev_dtl_required(newvd
))
6737 spa_vdev_resilver_done(spa_t
*spa
)
6739 vdev_t
*vd
, *pvd
, *ppvd
;
6740 uint64_t guid
, sguid
, pguid
, ppguid
;
6742 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6744 while ((vd
= spa_vdev_resilver_done_hunt(spa
->spa_root_vdev
)) != NULL
) {
6745 pvd
= vd
->vdev_parent
;
6746 ppvd
= pvd
->vdev_parent
;
6747 guid
= vd
->vdev_guid
;
6748 pguid
= pvd
->vdev_guid
;
6749 ppguid
= ppvd
->vdev_guid
;
6752 * If we have just finished replacing a hot spared device, then
6753 * we need to detach the parent's first child (the original hot
6756 if (ppvd
->vdev_ops
== &vdev_spare_ops
&& pvd
->vdev_id
== 0 &&
6757 ppvd
->vdev_children
== 2) {
6758 ASSERT(pvd
->vdev_ops
== &vdev_replacing_ops
);
6759 sguid
= ppvd
->vdev_child
[1]->vdev_guid
;
6761 ASSERT(vd
->vdev_resilver_txg
== 0 || !vdev_dtl_required(vd
));
6763 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6764 if (spa_vdev_detach(spa
, guid
, pguid
, B_TRUE
) != 0)
6766 if (sguid
&& spa_vdev_detach(spa
, sguid
, ppguid
, B_TRUE
) != 0)
6768 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6771 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6775 * Update the stored path or FRU for this vdev.
6778 spa_vdev_set_common(spa_t
*spa
, uint64_t guid
, const char *value
,
6782 boolean_t sync
= B_FALSE
;
6784 ASSERT(spa_writeable(spa
));
6786 spa_vdev_state_enter(spa
, SCL_ALL
);
6788 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
6789 return (spa_vdev_state_exit(spa
, NULL
, ENOENT
));
6791 if (!vd
->vdev_ops
->vdev_op_leaf
)
6792 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
6795 if (strcmp(value
, vd
->vdev_path
) != 0) {
6796 spa_strfree(vd
->vdev_path
);
6797 vd
->vdev_path
= spa_strdup(value
);
6801 if (vd
->vdev_fru
== NULL
) {
6802 vd
->vdev_fru
= spa_strdup(value
);
6804 } else if (strcmp(value
, vd
->vdev_fru
) != 0) {
6805 spa_strfree(vd
->vdev_fru
);
6806 vd
->vdev_fru
= spa_strdup(value
);
6811 return (spa_vdev_state_exit(spa
, sync
? vd
: NULL
, 0));
6815 spa_vdev_setpath(spa_t
*spa
, uint64_t guid
, const char *newpath
)
6817 return (spa_vdev_set_common(spa
, guid
, newpath
, B_TRUE
));
6821 spa_vdev_setfru(spa_t
*spa
, uint64_t guid
, const char *newfru
)
6823 return (spa_vdev_set_common(spa
, guid
, newfru
, B_FALSE
));
6827 * ==========================================================================
6829 * ==========================================================================
6832 spa_scrub_pause_resume(spa_t
*spa
, pool_scrub_cmd_t cmd
)
6834 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
6836 if (dsl_scan_resilvering(spa
->spa_dsl_pool
))
6837 return (SET_ERROR(EBUSY
));
6839 return (dsl_scrub_set_pause_resume(spa
->spa_dsl_pool
, cmd
));
6843 spa_scan_stop(spa_t
*spa
)
6845 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
6846 if (dsl_scan_resilvering(spa
->spa_dsl_pool
))
6847 return (SET_ERROR(EBUSY
));
6848 return (dsl_scan_cancel(spa
->spa_dsl_pool
));
6852 spa_scan(spa_t
*spa
, pool_scan_func_t func
)
6854 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
6856 if (func
>= POOL_SCAN_FUNCS
|| func
== POOL_SCAN_NONE
)
6857 return (SET_ERROR(ENOTSUP
));
6860 * If a resilver was requested, but there is no DTL on a
6861 * writeable leaf device, we have nothing to do.
6863 if (func
== POOL_SCAN_RESILVER
&&
6864 !vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
)) {
6865 spa_async_request(spa
, SPA_ASYNC_RESILVER_DONE
);
6869 return (dsl_scan(spa
->spa_dsl_pool
, func
));
6873 * ==========================================================================
6874 * SPA async task processing
6875 * ==========================================================================
6879 spa_async_remove(spa_t
*spa
, vdev_t
*vd
)
6881 if (vd
->vdev_remove_wanted
) {
6882 vd
->vdev_remove_wanted
= B_FALSE
;
6883 vd
->vdev_delayed_close
= B_FALSE
;
6884 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_REMOVED
, VDEV_AUX_NONE
);
6887 * We want to clear the stats, but we don't want to do a full
6888 * vdev_clear() as that will cause us to throw away
6889 * degraded/faulted state as well as attempt to reopen the
6890 * device, all of which is a waste.
6892 vd
->vdev_stat
.vs_read_errors
= 0;
6893 vd
->vdev_stat
.vs_write_errors
= 0;
6894 vd
->vdev_stat
.vs_checksum_errors
= 0;
6896 vdev_state_dirty(vd
->vdev_top
);
6899 for (int c
= 0; c
< vd
->vdev_children
; c
++)
6900 spa_async_remove(spa
, vd
->vdev_child
[c
]);
6904 spa_async_probe(spa_t
*spa
, vdev_t
*vd
)
6906 if (vd
->vdev_probe_wanted
) {
6907 vd
->vdev_probe_wanted
= B_FALSE
;
6908 vdev_reopen(vd
); /* vdev_open() does the actual probe */
6911 for (int c
= 0; c
< vd
->vdev_children
; c
++)
6912 spa_async_probe(spa
, vd
->vdev_child
[c
]);
6916 spa_async_autoexpand(spa_t
*spa
, vdev_t
*vd
)
6918 if (!spa
->spa_autoexpand
)
6921 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
6922 vdev_t
*cvd
= vd
->vdev_child
[c
];
6923 spa_async_autoexpand(spa
, cvd
);
6926 if (!vd
->vdev_ops
->vdev_op_leaf
|| vd
->vdev_physpath
== NULL
)
6929 spa_event_notify(vd
->vdev_spa
, vd
, NULL
, ESC_ZFS_VDEV_AUTOEXPAND
);
6933 spa_async_thread(void *arg
)
6935 spa_t
*spa
= (spa_t
*)arg
;
6938 ASSERT(spa
->spa_sync_on
);
6940 mutex_enter(&spa
->spa_async_lock
);
6941 tasks
= spa
->spa_async_tasks
;
6942 spa
->spa_async_tasks
= 0;
6943 mutex_exit(&spa
->spa_async_lock
);
6946 * See if the config needs to be updated.
6948 if (tasks
& SPA_ASYNC_CONFIG_UPDATE
) {
6949 uint64_t old_space
, new_space
;
6951 mutex_enter(&spa_namespace_lock
);
6952 old_space
= metaslab_class_get_space(spa_normal_class(spa
));
6953 old_space
+= metaslab_class_get_space(spa_special_class(spa
));
6954 old_space
+= metaslab_class_get_space(spa_dedup_class(spa
));
6956 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
6958 new_space
= metaslab_class_get_space(spa_normal_class(spa
));
6959 new_space
+= metaslab_class_get_space(spa_special_class(spa
));
6960 new_space
+= metaslab_class_get_space(spa_dedup_class(spa
));
6961 mutex_exit(&spa_namespace_lock
);
6964 * If the pool grew as a result of the config update,
6965 * then log an internal history event.
6967 if (new_space
!= old_space
) {
6968 spa_history_log_internal(spa
, "vdev online", NULL
,
6969 "pool '%s' size: %llu(+%llu)",
6970 spa_name(spa
), new_space
, new_space
- old_space
);
6975 * See if any devices need to be marked REMOVED.
6977 if (tasks
& SPA_ASYNC_REMOVE
) {
6978 spa_vdev_state_enter(spa
, SCL_NONE
);
6979 spa_async_remove(spa
, spa
->spa_root_vdev
);
6980 for (int i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++)
6981 spa_async_remove(spa
, spa
->spa_l2cache
.sav_vdevs
[i
]);
6982 for (int i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
6983 spa_async_remove(spa
, spa
->spa_spares
.sav_vdevs
[i
]);
6984 (void) spa_vdev_state_exit(spa
, NULL
, 0);
6987 if ((tasks
& SPA_ASYNC_AUTOEXPAND
) && !spa_suspended(spa
)) {
6988 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
6989 spa_async_autoexpand(spa
, spa
->spa_root_vdev
);
6990 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
6994 * See if any devices need to be probed.
6996 if (tasks
& SPA_ASYNC_PROBE
) {
6997 spa_vdev_state_enter(spa
, SCL_NONE
);
6998 spa_async_probe(spa
, spa
->spa_root_vdev
);
6999 (void) spa_vdev_state_exit(spa
, NULL
, 0);
7003 * If any devices are done replacing, detach them.
7005 if (tasks
& SPA_ASYNC_RESILVER_DONE
)
7006 spa_vdev_resilver_done(spa
);
7009 * Kick off a resilver.
7011 if (tasks
& SPA_ASYNC_RESILVER
)
7012 dsl_resilver_restart(spa
->spa_dsl_pool
, 0);
7015 * Let the world know that we're done.
7017 mutex_enter(&spa
->spa_async_lock
);
7018 spa
->spa_async_thread
= NULL
;
7019 cv_broadcast(&spa
->spa_async_cv
);
7020 mutex_exit(&spa
->spa_async_lock
);
7025 spa_async_suspend(spa_t
*spa
)
7027 mutex_enter(&spa
->spa_async_lock
);
7028 spa
->spa_async_suspended
++;
7029 while (spa
->spa_async_thread
!= NULL
)
7030 cv_wait(&spa
->spa_async_cv
, &spa
->spa_async_lock
);
7031 mutex_exit(&spa
->spa_async_lock
);
7033 spa_vdev_remove_suspend(spa
);
7035 zthr_t
*condense_thread
= spa
->spa_condense_zthr
;
7036 if (condense_thread
!= NULL
&& zthr_isrunning(condense_thread
))
7037 VERIFY0(zthr_cancel(condense_thread
));
7039 zthr_t
*discard_thread
= spa
->spa_checkpoint_discard_zthr
;
7040 if (discard_thread
!= NULL
&& zthr_isrunning(discard_thread
))
7041 VERIFY0(zthr_cancel(discard_thread
));
7045 spa_async_resume(spa_t
*spa
)
7047 mutex_enter(&spa
->spa_async_lock
);
7048 ASSERT(spa
->spa_async_suspended
!= 0);
7049 spa
->spa_async_suspended
--;
7050 mutex_exit(&spa
->spa_async_lock
);
7051 spa_restart_removal(spa
);
7053 zthr_t
*condense_thread
= spa
->spa_condense_zthr
;
7054 if (condense_thread
!= NULL
&& !zthr_isrunning(condense_thread
))
7055 zthr_resume(condense_thread
);
7057 zthr_t
*discard_thread
= spa
->spa_checkpoint_discard_zthr
;
7058 if (discard_thread
!= NULL
&& !zthr_isrunning(discard_thread
))
7059 zthr_resume(discard_thread
);
7063 spa_async_tasks_pending(spa_t
*spa
)
7065 uint_t non_config_tasks
;
7067 boolean_t config_task_suspended
;
7069 non_config_tasks
= spa
->spa_async_tasks
& ~SPA_ASYNC_CONFIG_UPDATE
;
7070 config_task
= spa
->spa_async_tasks
& SPA_ASYNC_CONFIG_UPDATE
;
7071 if (spa
->spa_ccw_fail_time
== 0) {
7072 config_task_suspended
= B_FALSE
;
7074 config_task_suspended
=
7075 (gethrtime() - spa
->spa_ccw_fail_time
) <
7076 ((hrtime_t
)zfs_ccw_retry_interval
* NANOSEC
);
7079 return (non_config_tasks
|| (config_task
&& !config_task_suspended
));
7083 spa_async_dispatch(spa_t
*spa
)
7085 mutex_enter(&spa
->spa_async_lock
);
7086 if (spa_async_tasks_pending(spa
) &&
7087 !spa
->spa_async_suspended
&&
7088 spa
->spa_async_thread
== NULL
&&
7090 spa
->spa_async_thread
= thread_create(NULL
, 0,
7091 spa_async_thread
, spa
, 0, &p0
, TS_RUN
, maxclsyspri
);
7092 mutex_exit(&spa
->spa_async_lock
);
7096 spa_async_request(spa_t
*spa
, int task
)
7098 zfs_dbgmsg("spa=%s async request task=%u", spa
->spa_name
, task
);
7099 mutex_enter(&spa
->spa_async_lock
);
7100 spa
->spa_async_tasks
|= task
;
7101 mutex_exit(&spa
->spa_async_lock
);
7105 * ==========================================================================
7106 * SPA syncing routines
7107 * ==========================================================================
7111 bpobj_enqueue_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
7114 bpobj_enqueue(bpo
, bp
, tx
);
7119 spa_free_sync_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
7123 zio_nowait(zio_free_sync(zio
, zio
->io_spa
, dmu_tx_get_txg(tx
), bp
,
7129 * Note: this simple function is not inlined to make it easier to dtrace the
7130 * amount of time spent syncing frees.
7133 spa_sync_frees(spa_t
*spa
, bplist_t
*bpl
, dmu_tx_t
*tx
)
7135 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
7136 bplist_iterate(bpl
, spa_free_sync_cb
, zio
, tx
);
7137 VERIFY(zio_wait(zio
) == 0);
7141 * Note: this simple function is not inlined to make it easier to dtrace the
7142 * amount of time spent syncing deferred frees.
7145 spa_sync_deferred_frees(spa_t
*spa
, dmu_tx_t
*tx
)
7147 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
7148 VERIFY3U(bpobj_iterate(&spa
->spa_deferred_bpobj
,
7149 spa_free_sync_cb
, zio
, tx
), ==, 0);
7150 VERIFY0(zio_wait(zio
));
7154 spa_sync_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
*nv
, dmu_tx_t
*tx
)
7156 char *packed
= NULL
;
7161 VERIFY(nvlist_size(nv
, &nvsize
, NV_ENCODE_XDR
) == 0);
7164 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
7165 * information. This avoids the dmu_buf_will_dirty() path and
7166 * saves us a pre-read to get data we don't actually care about.
7168 bufsize
= P2ROUNDUP((uint64_t)nvsize
, SPA_CONFIG_BLOCKSIZE
);
7169 packed
= vmem_alloc(bufsize
, KM_SLEEP
);
7171 VERIFY(nvlist_pack(nv
, &packed
, &nvsize
, NV_ENCODE_XDR
,
7173 bzero(packed
+ nvsize
, bufsize
- nvsize
);
7175 dmu_write(spa
->spa_meta_objset
, obj
, 0, bufsize
, packed
, tx
);
7177 vmem_free(packed
, bufsize
);
7179 VERIFY(0 == dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
));
7180 dmu_buf_will_dirty(db
, tx
);
7181 *(uint64_t *)db
->db_data
= nvsize
;
7182 dmu_buf_rele(db
, FTAG
);
7186 spa_sync_aux_dev(spa_t
*spa
, spa_aux_vdev_t
*sav
, dmu_tx_t
*tx
,
7187 const char *config
, const char *entry
)
7197 * Update the MOS nvlist describing the list of available devices.
7198 * spa_validate_aux() will have already made sure this nvlist is
7199 * valid and the vdevs are labeled appropriately.
7201 if (sav
->sav_object
== 0) {
7202 sav
->sav_object
= dmu_object_alloc(spa
->spa_meta_objset
,
7203 DMU_OT_PACKED_NVLIST
, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE
,
7204 sizeof (uint64_t), tx
);
7205 VERIFY(zap_update(spa
->spa_meta_objset
,
7206 DMU_POOL_DIRECTORY_OBJECT
, entry
, sizeof (uint64_t), 1,
7207 &sav
->sav_object
, tx
) == 0);
7210 VERIFY(nvlist_alloc(&nvroot
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
7211 if (sav
->sav_count
== 0) {
7212 VERIFY(nvlist_add_nvlist_array(nvroot
, config
, NULL
, 0) == 0);
7214 list
= kmem_alloc(sav
->sav_count
*sizeof (void *), KM_SLEEP
);
7215 for (i
= 0; i
< sav
->sav_count
; i
++)
7216 list
[i
] = vdev_config_generate(spa
, sav
->sav_vdevs
[i
],
7217 B_FALSE
, VDEV_CONFIG_L2CACHE
);
7218 VERIFY(nvlist_add_nvlist_array(nvroot
, config
, list
,
7219 sav
->sav_count
) == 0);
7220 for (i
= 0; i
< sav
->sav_count
; i
++)
7221 nvlist_free(list
[i
]);
7222 kmem_free(list
, sav
->sav_count
* sizeof (void *));
7225 spa_sync_nvlist(spa
, sav
->sav_object
, nvroot
, tx
);
7226 nvlist_free(nvroot
);
7228 sav
->sav_sync
= B_FALSE
;
7232 * Rebuild spa's all-vdev ZAP from the vdev ZAPs indicated in each vdev_t.
7233 * The all-vdev ZAP must be empty.
7236 spa_avz_build(vdev_t
*vd
, uint64_t avz
, dmu_tx_t
*tx
)
7238 spa_t
*spa
= vd
->vdev_spa
;
7240 if (vd
->vdev_top_zap
!= 0) {
7241 VERIFY0(zap_add_int(spa
->spa_meta_objset
, avz
,
7242 vd
->vdev_top_zap
, tx
));
7244 if (vd
->vdev_leaf_zap
!= 0) {
7245 VERIFY0(zap_add_int(spa
->spa_meta_objset
, avz
,
7246 vd
->vdev_leaf_zap
, tx
));
7248 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
7249 spa_avz_build(vd
->vdev_child
[i
], avz
, tx
);
7254 spa_sync_config_object(spa_t
*spa
, dmu_tx_t
*tx
)
7259 * If the pool is being imported from a pre-per-vdev-ZAP version of ZFS,
7260 * its config may not be dirty but we still need to build per-vdev ZAPs.
7261 * Similarly, if the pool is being assembled (e.g. after a split), we
7262 * need to rebuild the AVZ although the config may not be dirty.
7264 if (list_is_empty(&spa
->spa_config_dirty_list
) &&
7265 spa
->spa_avz_action
== AVZ_ACTION_NONE
)
7268 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
7270 ASSERT(spa
->spa_avz_action
== AVZ_ACTION_NONE
||
7271 spa
->spa_avz_action
== AVZ_ACTION_INITIALIZE
||
7272 spa
->spa_all_vdev_zaps
!= 0);
7274 if (spa
->spa_avz_action
== AVZ_ACTION_REBUILD
) {
7275 /* Make and build the new AVZ */
7276 uint64_t new_avz
= zap_create(spa
->spa_meta_objset
,
7277 DMU_OTN_ZAP_METADATA
, DMU_OT_NONE
, 0, tx
);
7278 spa_avz_build(spa
->spa_root_vdev
, new_avz
, tx
);
7280 /* Diff old AVZ with new one */
7284 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
7285 spa
->spa_all_vdev_zaps
);
7286 zap_cursor_retrieve(&zc
, &za
) == 0;
7287 zap_cursor_advance(&zc
)) {
7288 uint64_t vdzap
= za
.za_first_integer
;
7289 if (zap_lookup_int(spa
->spa_meta_objset
, new_avz
,
7292 * ZAP is listed in old AVZ but not in new one;
7295 VERIFY0(zap_destroy(spa
->spa_meta_objset
, vdzap
,
7300 zap_cursor_fini(&zc
);
7302 /* Destroy the old AVZ */
7303 VERIFY0(zap_destroy(spa
->spa_meta_objset
,
7304 spa
->spa_all_vdev_zaps
, tx
));
7306 /* Replace the old AVZ in the dir obj with the new one */
7307 VERIFY0(zap_update(spa
->spa_meta_objset
,
7308 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_VDEV_ZAP_MAP
,
7309 sizeof (new_avz
), 1, &new_avz
, tx
));
7311 spa
->spa_all_vdev_zaps
= new_avz
;
7312 } else if (spa
->spa_avz_action
== AVZ_ACTION_DESTROY
) {
7316 /* Walk through the AVZ and destroy all listed ZAPs */
7317 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
7318 spa
->spa_all_vdev_zaps
);
7319 zap_cursor_retrieve(&zc
, &za
) == 0;
7320 zap_cursor_advance(&zc
)) {
7321 uint64_t zap
= za
.za_first_integer
;
7322 VERIFY0(zap_destroy(spa
->spa_meta_objset
, zap
, tx
));
7325 zap_cursor_fini(&zc
);
7327 /* Destroy and unlink the AVZ itself */
7328 VERIFY0(zap_destroy(spa
->spa_meta_objset
,
7329 spa
->spa_all_vdev_zaps
, tx
));
7330 VERIFY0(zap_remove(spa
->spa_meta_objset
,
7331 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_VDEV_ZAP_MAP
, tx
));
7332 spa
->spa_all_vdev_zaps
= 0;
7335 if (spa
->spa_all_vdev_zaps
== 0) {
7336 spa
->spa_all_vdev_zaps
= zap_create_link(spa
->spa_meta_objset
,
7337 DMU_OTN_ZAP_METADATA
, DMU_POOL_DIRECTORY_OBJECT
,
7338 DMU_POOL_VDEV_ZAP_MAP
, tx
);
7340 spa
->spa_avz_action
= AVZ_ACTION_NONE
;
7342 /* Create ZAPs for vdevs that don't have them. */
7343 vdev_construct_zaps(spa
->spa_root_vdev
, tx
);
7345 config
= spa_config_generate(spa
, spa
->spa_root_vdev
,
7346 dmu_tx_get_txg(tx
), B_FALSE
);
7349 * If we're upgrading the spa version then make sure that
7350 * the config object gets updated with the correct version.
7352 if (spa
->spa_ubsync
.ub_version
< spa
->spa_uberblock
.ub_version
)
7353 fnvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
7354 spa
->spa_uberblock
.ub_version
);
7356 spa_config_exit(spa
, SCL_STATE
, FTAG
);
7358 nvlist_free(spa
->spa_config_syncing
);
7359 spa
->spa_config_syncing
= config
;
7361 spa_sync_nvlist(spa
, spa
->spa_config_object
, config
, tx
);
7365 spa_sync_version(void *arg
, dmu_tx_t
*tx
)
7367 uint64_t *versionp
= arg
;
7368 uint64_t version
= *versionp
;
7369 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
7372 * Setting the version is special cased when first creating the pool.
7374 ASSERT(tx
->tx_txg
!= TXG_INITIAL
);
7376 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
7377 ASSERT(version
>= spa_version(spa
));
7379 spa
->spa_uberblock
.ub_version
= version
;
7380 vdev_config_dirty(spa
->spa_root_vdev
);
7381 spa_history_log_internal(spa
, "set", tx
, "version=%lld", version
);
7385 * Set zpool properties.
7388 spa_sync_props(void *arg
, dmu_tx_t
*tx
)
7390 nvlist_t
*nvp
= arg
;
7391 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
7392 objset_t
*mos
= spa
->spa_meta_objset
;
7393 nvpair_t
*elem
= NULL
;
7395 mutex_enter(&spa
->spa_props_lock
);
7397 while ((elem
= nvlist_next_nvpair(nvp
, elem
))) {
7399 char *strval
, *fname
;
7401 const char *propname
;
7402 zprop_type_t proptype
;
7405 switch (prop
= zpool_name_to_prop(nvpair_name(elem
))) {
7406 case ZPOOL_PROP_INVAL
:
7408 * We checked this earlier in spa_prop_validate().
7410 ASSERT(zpool_prop_feature(nvpair_name(elem
)));
7412 fname
= strchr(nvpair_name(elem
), '@') + 1;
7413 VERIFY0(zfeature_lookup_name(fname
, &fid
));
7415 spa_feature_enable(spa
, fid
, tx
);
7416 spa_history_log_internal(spa
, "set", tx
,
7417 "%s=enabled", nvpair_name(elem
));
7420 case ZPOOL_PROP_VERSION
:
7421 intval
= fnvpair_value_uint64(elem
);
7423 * The version is synced separately before other
7424 * properties and should be correct by now.
7426 ASSERT3U(spa_version(spa
), >=, intval
);
7429 case ZPOOL_PROP_ALTROOT
:
7431 * 'altroot' is a non-persistent property. It should
7432 * have been set temporarily at creation or import time.
7434 ASSERT(spa
->spa_root
!= NULL
);
7437 case ZPOOL_PROP_READONLY
:
7438 case ZPOOL_PROP_CACHEFILE
:
7440 * 'readonly' and 'cachefile' are also non-persisitent
7444 case ZPOOL_PROP_COMMENT
:
7445 strval
= fnvpair_value_string(elem
);
7446 if (spa
->spa_comment
!= NULL
)
7447 spa_strfree(spa
->spa_comment
);
7448 spa
->spa_comment
= spa_strdup(strval
);
7450 * We need to dirty the configuration on all the vdevs
7451 * so that their labels get updated. It's unnecessary
7452 * to do this for pool creation since the vdev's
7453 * configuration has already been dirtied.
7455 if (tx
->tx_txg
!= TXG_INITIAL
)
7456 vdev_config_dirty(spa
->spa_root_vdev
);
7457 spa_history_log_internal(spa
, "set", tx
,
7458 "%s=%s", nvpair_name(elem
), strval
);
7462 * Set pool property values in the poolprops mos object.
7464 if (spa
->spa_pool_props_object
== 0) {
7465 spa
->spa_pool_props_object
=
7466 zap_create_link(mos
, DMU_OT_POOL_PROPS
,
7467 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_PROPS
,
7471 /* normalize the property name */
7472 propname
= zpool_prop_to_name(prop
);
7473 proptype
= zpool_prop_get_type(prop
);
7475 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
7476 ASSERT(proptype
== PROP_TYPE_STRING
);
7477 strval
= fnvpair_value_string(elem
);
7478 VERIFY0(zap_update(mos
,
7479 spa
->spa_pool_props_object
, propname
,
7480 1, strlen(strval
) + 1, strval
, tx
));
7481 spa_history_log_internal(spa
, "set", tx
,
7482 "%s=%s", nvpair_name(elem
), strval
);
7483 } else if (nvpair_type(elem
) == DATA_TYPE_UINT64
) {
7484 intval
= fnvpair_value_uint64(elem
);
7486 if (proptype
== PROP_TYPE_INDEX
) {
7488 VERIFY0(zpool_prop_index_to_string(
7489 prop
, intval
, &unused
));
7491 VERIFY0(zap_update(mos
,
7492 spa
->spa_pool_props_object
, propname
,
7493 8, 1, &intval
, tx
));
7494 spa_history_log_internal(spa
, "set", tx
,
7495 "%s=%lld", nvpair_name(elem
), intval
);
7497 ASSERT(0); /* not allowed */
7501 case ZPOOL_PROP_DELEGATION
:
7502 spa
->spa_delegation
= intval
;
7504 case ZPOOL_PROP_BOOTFS
:
7505 spa
->spa_bootfs
= intval
;
7507 case ZPOOL_PROP_FAILUREMODE
:
7508 spa
->spa_failmode
= intval
;
7510 case ZPOOL_PROP_AUTOEXPAND
:
7511 spa
->spa_autoexpand
= intval
;
7512 if (tx
->tx_txg
!= TXG_INITIAL
)
7513 spa_async_request(spa
,
7514 SPA_ASYNC_AUTOEXPAND
);
7516 case ZPOOL_PROP_MULTIHOST
:
7517 spa
->spa_multihost
= intval
;
7519 case ZPOOL_PROP_DEDUPDITTO
:
7520 spa
->spa_dedup_ditto
= intval
;
7529 mutex_exit(&spa
->spa_props_lock
);
7533 * Perform one-time upgrade on-disk changes. spa_version() does not
7534 * reflect the new version this txg, so there must be no changes this
7535 * txg to anything that the upgrade code depends on after it executes.
7536 * Therefore this must be called after dsl_pool_sync() does the sync
7540 spa_sync_upgrades(spa_t
*spa
, dmu_tx_t
*tx
)
7542 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
7544 ASSERT(spa
->spa_sync_pass
== 1);
7546 rrw_enter(&dp
->dp_config_rwlock
, RW_WRITER
, FTAG
);
7548 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_ORIGIN
&&
7549 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_ORIGIN
) {
7550 dsl_pool_create_origin(dp
, tx
);
7552 /* Keeping the origin open increases spa_minref */
7553 spa
->spa_minref
+= 3;
7556 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_NEXT_CLONES
&&
7557 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_NEXT_CLONES
) {
7558 dsl_pool_upgrade_clones(dp
, tx
);
7561 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_DIR_CLONES
&&
7562 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_DIR_CLONES
) {
7563 dsl_pool_upgrade_dir_clones(dp
, tx
);
7565 /* Keeping the freedir open increases spa_minref */
7566 spa
->spa_minref
+= 3;
7569 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_FEATURES
&&
7570 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
7571 spa_feature_create_zap_objects(spa
, tx
);
7575 * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable
7576 * when possibility to use lz4 compression for metadata was added
7577 * Old pools that have this feature enabled must be upgraded to have
7578 * this feature active
7580 if (spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
7581 boolean_t lz4_en
= spa_feature_is_enabled(spa
,
7582 SPA_FEATURE_LZ4_COMPRESS
);
7583 boolean_t lz4_ac
= spa_feature_is_active(spa
,
7584 SPA_FEATURE_LZ4_COMPRESS
);
7586 if (lz4_en
&& !lz4_ac
)
7587 spa_feature_incr(spa
, SPA_FEATURE_LZ4_COMPRESS
, tx
);
7591 * If we haven't written the salt, do so now. Note that the
7592 * feature may not be activated yet, but that's fine since
7593 * the presence of this ZAP entry is backwards compatible.
7595 if (zap_contains(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
7596 DMU_POOL_CHECKSUM_SALT
) == ENOENT
) {
7597 VERIFY0(zap_add(spa
->spa_meta_objset
,
7598 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CHECKSUM_SALT
, 1,
7599 sizeof (spa
->spa_cksum_salt
.zcs_bytes
),
7600 spa
->spa_cksum_salt
.zcs_bytes
, tx
));
7603 rrw_exit(&dp
->dp_config_rwlock
, FTAG
);
7607 vdev_indirect_state_sync_verify(vdev_t
*vd
)
7609 ASSERTV(vdev_indirect_mapping_t
*vim
= vd
->vdev_indirect_mapping
);
7610 ASSERTV(vdev_indirect_births_t
*vib
= vd
->vdev_indirect_births
);
7612 if (vd
->vdev_ops
== &vdev_indirect_ops
) {
7613 ASSERT(vim
!= NULL
);
7614 ASSERT(vib
!= NULL
);
7617 uint64_t obsolete_sm_object
= 0;
7618 ASSERT0(vdev_obsolete_sm_object(vd
, &obsolete_sm_object
));
7619 if (obsolete_sm_object
!= 0) {
7620 ASSERT(vd
->vdev_obsolete_sm
!= NULL
);
7621 ASSERT(vd
->vdev_removing
||
7622 vd
->vdev_ops
== &vdev_indirect_ops
);
7623 ASSERT(vdev_indirect_mapping_num_entries(vim
) > 0);
7624 ASSERT(vdev_indirect_mapping_bytes_mapped(vim
) > 0);
7625 ASSERT3U(obsolete_sm_object
, ==,
7626 space_map_object(vd
->vdev_obsolete_sm
));
7627 ASSERT3U(vdev_indirect_mapping_bytes_mapped(vim
), >=,
7628 space_map_allocated(vd
->vdev_obsolete_sm
));
7630 ASSERT(vd
->vdev_obsolete_segments
!= NULL
);
7633 * Since frees / remaps to an indirect vdev can only
7634 * happen in syncing context, the obsolete segments
7635 * tree must be empty when we start syncing.
7637 ASSERT0(range_tree_space(vd
->vdev_obsolete_segments
));
7641 * Sync the specified transaction group. New blocks may be dirtied as
7642 * part of the process, so we iterate until it converges.
7645 spa_sync(spa_t
*spa
, uint64_t txg
)
7647 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
7648 objset_t
*mos
= spa
->spa_meta_objset
;
7649 bplist_t
*free_bpl
= &spa
->spa_free_bplist
[txg
& TXG_MASK
];
7650 metaslab_class_t
*normal
= spa_normal_class(spa
);
7651 metaslab_class_t
*special
= spa_special_class(spa
);
7652 metaslab_class_t
*dedup
= spa_dedup_class(spa
);
7653 vdev_t
*rvd
= spa
->spa_root_vdev
;
7657 uint32_t max_queue_depth
= zfs_vdev_async_write_max_active
*
7658 zfs_vdev_queue_depth_pct
/ 100;
7660 VERIFY(spa_writeable(spa
));
7663 * Wait for i/os issued in open context that need to complete
7664 * before this txg syncs.
7666 VERIFY0(zio_wait(spa
->spa_txg_zio
[txg
& TXG_MASK
]));
7667 spa
->spa_txg_zio
[txg
& TXG_MASK
] = zio_root(spa
, NULL
, NULL
, 0);
7670 * Lock out configuration changes.
7672 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
7674 spa
->spa_syncing_txg
= txg
;
7675 spa
->spa_sync_pass
= 0;
7677 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
7678 mutex_enter(&spa
->spa_alloc_locks
[i
]);
7679 VERIFY0(avl_numnodes(&spa
->spa_alloc_trees
[i
]));
7680 mutex_exit(&spa
->spa_alloc_locks
[i
]);
7684 * If there are any pending vdev state changes, convert them
7685 * into config changes that go out with this transaction group.
7687 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
7688 while (list_head(&spa
->spa_state_dirty_list
) != NULL
) {
7690 * We need the write lock here because, for aux vdevs,
7691 * calling vdev_config_dirty() modifies sav_config.
7692 * This is ugly and will become unnecessary when we
7693 * eliminate the aux vdev wart by integrating all vdevs
7694 * into the root vdev tree.
7696 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7697 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_WRITER
);
7698 while ((vd
= list_head(&spa
->spa_state_dirty_list
)) != NULL
) {
7699 vdev_state_clean(vd
);
7700 vdev_config_dirty(vd
);
7702 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7703 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
7705 spa_config_exit(spa
, SCL_STATE
, FTAG
);
7707 tx
= dmu_tx_create_assigned(dp
, txg
);
7709 spa
->spa_sync_starttime
= gethrtime();
7710 taskq_cancel_id(system_delay_taskq
, spa
->spa_deadman_tqid
);
7711 spa
->spa_deadman_tqid
= taskq_dispatch_delay(system_delay_taskq
,
7712 spa_deadman
, spa
, TQ_SLEEP
, ddi_get_lbolt() +
7713 NSEC_TO_TICK(spa
->spa_deadman_synctime
));
7716 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
7717 * set spa_deflate if we have no raid-z vdevs.
7719 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_RAIDZ_DEFLATE
&&
7720 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
7723 for (i
= 0; i
< rvd
->vdev_children
; i
++) {
7724 vd
= rvd
->vdev_child
[i
];
7725 if (vd
->vdev_deflate_ratio
!= SPA_MINBLOCKSIZE
)
7728 if (i
== rvd
->vdev_children
) {
7729 spa
->spa_deflate
= TRUE
;
7730 VERIFY(0 == zap_add(spa
->spa_meta_objset
,
7731 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
7732 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
));
7737 * Set the top-level vdev's max queue depth. Evaluate each
7738 * top-level's async write queue depth in case it changed.
7739 * The max queue depth will not change in the middle of syncing
7742 uint64_t slots_per_allocator
= 0;
7743 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
7744 vdev_t
*tvd
= rvd
->vdev_child
[c
];
7745 metaslab_group_t
*mg
= tvd
->vdev_mg
;
7746 metaslab_class_t
*mc
;
7748 if (mg
== NULL
|| !metaslab_group_initialized(mg
))
7752 if (mc
!= normal
&& mc
!= special
&& mc
!= dedup
)
7756 * It is safe to do a lock-free check here because only async
7757 * allocations look at mg_max_alloc_queue_depth, and async
7758 * allocations all happen from spa_sync().
7760 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++)
7761 ASSERT0(zfs_refcount_count(
7762 &(mg
->mg_alloc_queue_depth
[i
])));
7763 mg
->mg_max_alloc_queue_depth
= max_queue_depth
;
7765 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
7766 mg
->mg_cur_max_alloc_queue_depth
[i
] =
7767 zfs_vdev_def_queue_depth
;
7769 slots_per_allocator
+= zfs_vdev_def_queue_depth
;
7772 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
7773 ASSERT0(zfs_refcount_count(&normal
->mc_alloc_slots
[i
]));
7774 ASSERT0(zfs_refcount_count(&special
->mc_alloc_slots
[i
]));
7775 ASSERT0(zfs_refcount_count(&dedup
->mc_alloc_slots
[i
]));
7776 normal
->mc_alloc_max_slots
[i
] = slots_per_allocator
;
7777 special
->mc_alloc_max_slots
[i
] = slots_per_allocator
;
7778 dedup
->mc_alloc_max_slots
[i
] = slots_per_allocator
;
7780 normal
->mc_alloc_throttle_enabled
= zio_dva_throttle_enabled
;
7781 special
->mc_alloc_throttle_enabled
= zio_dva_throttle_enabled
;
7782 dedup
->mc_alloc_throttle_enabled
= zio_dva_throttle_enabled
;
7784 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
7785 vdev_t
*vd
= rvd
->vdev_child
[c
];
7786 vdev_indirect_state_sync_verify(vd
);
7788 if (vdev_indirect_should_condense(vd
)) {
7789 spa_condense_indirect_start_sync(vd
, tx
);
7795 * Iterate to convergence.
7798 int pass
= ++spa
->spa_sync_pass
;
7800 spa_sync_config_object(spa
, tx
);
7801 spa_sync_aux_dev(spa
, &spa
->spa_spares
, tx
,
7802 ZPOOL_CONFIG_SPARES
, DMU_POOL_SPARES
);
7803 spa_sync_aux_dev(spa
, &spa
->spa_l2cache
, tx
,
7804 ZPOOL_CONFIG_L2CACHE
, DMU_POOL_L2CACHE
);
7805 spa_errlog_sync(spa
, txg
);
7806 dsl_pool_sync(dp
, txg
);
7808 if (pass
< zfs_sync_pass_deferred_free
) {
7809 spa_sync_frees(spa
, free_bpl
, tx
);
7812 * We can not defer frees in pass 1, because
7813 * we sync the deferred frees later in pass 1.
7815 ASSERT3U(pass
, >, 1);
7816 bplist_iterate(free_bpl
, bpobj_enqueue_cb
,
7817 &spa
->spa_deferred_bpobj
, tx
);
7821 dsl_scan_sync(dp
, tx
);
7823 if (spa
->spa_vdev_removal
!= NULL
)
7826 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, txg
))
7831 spa_sync_upgrades(spa
, tx
);
7833 spa
->spa_uberblock
.ub_rootbp
.blk_birth
);
7835 * Note: We need to check if the MOS is dirty
7836 * because we could have marked the MOS dirty
7837 * without updating the uberblock (e.g. if we
7838 * have sync tasks but no dirty user data). We
7839 * need to check the uberblock's rootbp because
7840 * it is updated if we have synced out dirty
7841 * data (though in this case the MOS will most
7842 * likely also be dirty due to second order
7843 * effects, we don't want to rely on that here).
7845 if (spa
->spa_uberblock
.ub_rootbp
.blk_birth
< txg
&&
7846 !dmu_objset_is_dirty(mos
, txg
)) {
7848 * Nothing changed on the first pass,
7849 * therefore this TXG is a no-op. Avoid
7850 * syncing deferred frees, so that we
7851 * can keep this TXG as a no-op.
7853 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
,
7855 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
7856 ASSERT(txg_list_empty(&dp
->dp_sync_tasks
, txg
));
7857 ASSERT(txg_list_empty(&dp
->dp_early_sync_tasks
,
7861 spa_sync_deferred_frees(spa
, tx
);
7864 } while (dmu_objset_is_dirty(mos
, txg
));
7867 if (!list_is_empty(&spa
->spa_config_dirty_list
)) {
7869 * Make sure that the number of ZAPs for all the vdevs matches
7870 * the number of ZAPs in the per-vdev ZAP list. This only gets
7871 * called if the config is dirty; otherwise there may be
7872 * outstanding AVZ operations that weren't completed in
7873 * spa_sync_config_object.
7875 uint64_t all_vdev_zap_entry_count
;
7876 ASSERT0(zap_count(spa
->spa_meta_objset
,
7877 spa
->spa_all_vdev_zaps
, &all_vdev_zap_entry_count
));
7878 ASSERT3U(vdev_count_verify_zaps(spa
->spa_root_vdev
), ==,
7879 all_vdev_zap_entry_count
);
7883 if (spa
->spa_vdev_removal
!= NULL
) {
7884 ASSERT0(spa
->spa_vdev_removal
->svr_bytes_done
[txg
& TXG_MASK
]);
7888 * Rewrite the vdev configuration (which includes the uberblock)
7889 * to commit the transaction group.
7891 * If there are no dirty vdevs, we sync the uberblock to a few
7892 * random top-level vdevs that are known to be visible in the
7893 * config cache (see spa_vdev_add() for a complete description).
7894 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
7898 * We hold SCL_STATE to prevent vdev open/close/etc.
7899 * while we're attempting to write the vdev labels.
7901 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
7903 if (list_is_empty(&spa
->spa_config_dirty_list
)) {
7904 vdev_t
*svd
[SPA_SYNC_MIN_VDEVS
] = { NULL
};
7906 int children
= rvd
->vdev_children
;
7907 int c0
= spa_get_random(children
);
7909 for (int c
= 0; c
< children
; c
++) {
7910 vd
= rvd
->vdev_child
[(c0
+ c
) % children
];
7912 /* Stop when revisiting the first vdev */
7913 if (c
> 0 && svd
[0] == vd
)
7916 if (vd
->vdev_ms_array
== 0 || vd
->vdev_islog
||
7917 !vdev_is_concrete(vd
))
7920 svd
[svdcount
++] = vd
;
7921 if (svdcount
== SPA_SYNC_MIN_VDEVS
)
7924 error
= vdev_config_sync(svd
, svdcount
, txg
);
7926 error
= vdev_config_sync(rvd
->vdev_child
,
7927 rvd
->vdev_children
, txg
);
7931 spa
->spa_last_synced_guid
= rvd
->vdev_guid
;
7933 spa_config_exit(spa
, SCL_STATE
, FTAG
);
7937 zio_suspend(spa
, NULL
, ZIO_SUSPEND_IOERR
);
7938 zio_resume_wait(spa
);
7942 taskq_cancel_id(system_delay_taskq
, spa
->spa_deadman_tqid
);
7943 spa
->spa_deadman_tqid
= 0;
7946 * Clear the dirty config list.
7948 while ((vd
= list_head(&spa
->spa_config_dirty_list
)) != NULL
)
7949 vdev_config_clean(vd
);
7952 * Now that the new config has synced transactionally,
7953 * let it become visible to the config cache.
7955 if (spa
->spa_config_syncing
!= NULL
) {
7956 spa_config_set(spa
, spa
->spa_config_syncing
);
7957 spa
->spa_config_txg
= txg
;
7958 spa
->spa_config_syncing
= NULL
;
7961 dsl_pool_sync_done(dp
, txg
);
7963 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
7964 mutex_enter(&spa
->spa_alloc_locks
[i
]);
7965 VERIFY0(avl_numnodes(&spa
->spa_alloc_trees
[i
]));
7966 mutex_exit(&spa
->spa_alloc_locks
[i
]);
7970 * Update usable space statistics.
7972 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, TXG_CLEAN(txg
))))
7973 vdev_sync_done(vd
, txg
);
7975 spa_update_dspace(spa
);
7978 * It had better be the case that we didn't dirty anything
7979 * since vdev_config_sync().
7981 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
, txg
));
7982 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
7983 ASSERT(txg_list_empty(&spa
->spa_vdev_txg_list
, txg
));
7985 while (zfs_pause_spa_sync
)
7988 spa
->spa_sync_pass
= 0;
7991 * Update the last synced uberblock here. We want to do this at
7992 * the end of spa_sync() so that consumers of spa_last_synced_txg()
7993 * will be guaranteed that all the processing associated with
7994 * that txg has been completed.
7996 spa
->spa_ubsync
= spa
->spa_uberblock
;
7997 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
7999 spa_handle_ignored_writes(spa
);
8002 * If any async tasks have been requested, kick them off.
8004 spa_async_dispatch(spa
);
8008 * Sync all pools. We don't want to hold the namespace lock across these
8009 * operations, so we take a reference on the spa_t and drop the lock during the
8013 spa_sync_allpools(void)
8016 mutex_enter(&spa_namespace_lock
);
8017 while ((spa
= spa_next(spa
)) != NULL
) {
8018 if (spa_state(spa
) != POOL_STATE_ACTIVE
||
8019 !spa_writeable(spa
) || spa_suspended(spa
))
8021 spa_open_ref(spa
, FTAG
);
8022 mutex_exit(&spa_namespace_lock
);
8023 txg_wait_synced(spa_get_dsl(spa
), 0);
8024 mutex_enter(&spa_namespace_lock
);
8025 spa_close(spa
, FTAG
);
8027 mutex_exit(&spa_namespace_lock
);
8031 * ==========================================================================
8032 * Miscellaneous routines
8033 * ==========================================================================
8037 * Remove all pools in the system.
8045 * Remove all cached state. All pools should be closed now,
8046 * so every spa in the AVL tree should be unreferenced.
8048 mutex_enter(&spa_namespace_lock
);
8049 while ((spa
= spa_next(NULL
)) != NULL
) {
8051 * Stop async tasks. The async thread may need to detach
8052 * a device that's been replaced, which requires grabbing
8053 * spa_namespace_lock, so we must drop it here.
8055 spa_open_ref(spa
, FTAG
);
8056 mutex_exit(&spa_namespace_lock
);
8057 spa_async_suspend(spa
);
8058 mutex_enter(&spa_namespace_lock
);
8059 spa_close(spa
, FTAG
);
8061 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
8063 spa_deactivate(spa
);
8067 mutex_exit(&spa_namespace_lock
);
8071 spa_lookup_by_guid(spa_t
*spa
, uint64_t guid
, boolean_t aux
)
8076 if ((vd
= vdev_lookup_by_guid(spa
->spa_root_vdev
, guid
)) != NULL
)
8080 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
8081 vd
= spa
->spa_l2cache
.sav_vdevs
[i
];
8082 if (vd
->vdev_guid
== guid
)
8086 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
8087 vd
= spa
->spa_spares
.sav_vdevs
[i
];
8088 if (vd
->vdev_guid
== guid
)
8097 spa_upgrade(spa_t
*spa
, uint64_t version
)
8099 ASSERT(spa_writeable(spa
));
8101 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
8104 * This should only be called for a non-faulted pool, and since a
8105 * future version would result in an unopenable pool, this shouldn't be
8108 ASSERT(SPA_VERSION_IS_SUPPORTED(spa
->spa_uberblock
.ub_version
));
8109 ASSERT3U(version
, >=, spa
->spa_uberblock
.ub_version
);
8111 spa
->spa_uberblock
.ub_version
= version
;
8112 vdev_config_dirty(spa
->spa_root_vdev
);
8114 spa_config_exit(spa
, SCL_ALL
, FTAG
);
8116 txg_wait_synced(spa_get_dsl(spa
), 0);
8120 spa_has_spare(spa_t
*spa
, uint64_t guid
)
8124 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
8126 for (i
= 0; i
< sav
->sav_count
; i
++)
8127 if (sav
->sav_vdevs
[i
]->vdev_guid
== guid
)
8130 for (i
= 0; i
< sav
->sav_npending
; i
++) {
8131 if (nvlist_lookup_uint64(sav
->sav_pending
[i
], ZPOOL_CONFIG_GUID
,
8132 &spareguid
) == 0 && spareguid
== guid
)
8140 * Check if a pool has an active shared spare device.
8141 * Note: reference count of an active spare is 2, as a spare and as a replace
8144 spa_has_active_shared_spare(spa_t
*spa
)
8148 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
8150 for (i
= 0; i
< sav
->sav_count
; i
++) {
8151 if (spa_spare_exists(sav
->sav_vdevs
[i
]->vdev_guid
, &pool
,
8152 &refcnt
) && pool
!= 0ULL && pool
== spa_guid(spa
) &&
8161 spa_event_create(spa_t
*spa
, vdev_t
*vd
, nvlist_t
*hist_nvl
, const char *name
)
8163 sysevent_t
*ev
= NULL
;
8167 resource
= zfs_event_create(spa
, vd
, FM_SYSEVENT_CLASS
, name
, hist_nvl
);
8169 ev
= kmem_alloc(sizeof (sysevent_t
), KM_SLEEP
);
8170 ev
->resource
= resource
;
8177 spa_event_post(sysevent_t
*ev
)
8181 zfs_zevent_post(ev
->resource
, NULL
, zfs_zevent_post_cb
);
8182 kmem_free(ev
, sizeof (*ev
));
8188 * Post a zevent corresponding to the given sysevent. The 'name' must be one
8189 * of the event definitions in sys/sysevent/eventdefs.h. The payload will be
8190 * filled in from the spa and (optionally) the vdev. This doesn't do anything
8191 * in the userland libzpool, as we don't want consumers to misinterpret ztest
8192 * or zdb as real changes.
8195 spa_event_notify(spa_t
*spa
, vdev_t
*vd
, nvlist_t
*hist_nvl
, const char *name
)
8197 spa_event_post(spa_event_create(spa
, vd
, hist_nvl
, name
));
8200 #if defined(_KERNEL)
8201 /* state manipulation functions */
8202 EXPORT_SYMBOL(spa_open
);
8203 EXPORT_SYMBOL(spa_open_rewind
);
8204 EXPORT_SYMBOL(spa_get_stats
);
8205 EXPORT_SYMBOL(spa_create
);
8206 EXPORT_SYMBOL(spa_import
);
8207 EXPORT_SYMBOL(spa_tryimport
);
8208 EXPORT_SYMBOL(spa_destroy
);
8209 EXPORT_SYMBOL(spa_export
);
8210 EXPORT_SYMBOL(spa_reset
);
8211 EXPORT_SYMBOL(spa_async_request
);
8212 EXPORT_SYMBOL(spa_async_suspend
);
8213 EXPORT_SYMBOL(spa_async_resume
);
8214 EXPORT_SYMBOL(spa_inject_addref
);
8215 EXPORT_SYMBOL(spa_inject_delref
);
8216 EXPORT_SYMBOL(spa_scan_stat_init
);
8217 EXPORT_SYMBOL(spa_scan_get_stats
);
8219 /* device maniion */
8220 EXPORT_SYMBOL(spa_vdev_add
);
8221 EXPORT_SYMBOL(spa_vdev_attach
);
8222 EXPORT_SYMBOL(spa_vdev_detach
);
8223 EXPORT_SYMBOL(spa_vdev_setpath
);
8224 EXPORT_SYMBOL(spa_vdev_setfru
);
8225 EXPORT_SYMBOL(spa_vdev_split_mirror
);
8227 /* spare statech is global across all pools) */
8228 EXPORT_SYMBOL(spa_spare_add
);
8229 EXPORT_SYMBOL(spa_spare_remove
);
8230 EXPORT_SYMBOL(spa_spare_exists
);
8231 EXPORT_SYMBOL(spa_spare_activate
);
8233 /* L2ARC statech is global across all pools) */
8234 EXPORT_SYMBOL(spa_l2cache_add
);
8235 EXPORT_SYMBOL(spa_l2cache_remove
);
8236 EXPORT_SYMBOL(spa_l2cache_exists
);
8237 EXPORT_SYMBOL(spa_l2cache_activate
);
8238 EXPORT_SYMBOL(spa_l2cache_drop
);
8241 EXPORT_SYMBOL(spa_scan
);
8242 EXPORT_SYMBOL(spa_scan_stop
);
8245 EXPORT_SYMBOL(spa_sync
); /* only for DMU use */
8246 EXPORT_SYMBOL(spa_sync_allpools
);
8249 EXPORT_SYMBOL(spa_prop_set
);
8250 EXPORT_SYMBOL(spa_prop_get
);
8251 EXPORT_SYMBOL(spa_prop_clear_bootfs
);
8253 /* asynchronous event notification */
8254 EXPORT_SYMBOL(spa_event_notify
);
8257 #if defined(_KERNEL)
8258 module_param(spa_load_verify_maxinflight
, int, 0644);
8259 MODULE_PARM_DESC(spa_load_verify_maxinflight
,
8260 "Max concurrent traversal I/Os while verifying pool during import -X");
8262 module_param(spa_load_verify_metadata
, int, 0644);
8263 MODULE_PARM_DESC(spa_load_verify_metadata
,
8264 "Set to traverse metadata on pool import");
8266 module_param(spa_load_verify_data
, int, 0644);
8267 MODULE_PARM_DESC(spa_load_verify_data
,
8268 "Set to traverse data on pool import");
8270 module_param(spa_load_print_vdev_tree
, int, 0644);
8271 MODULE_PARM_DESC(spa_load_print_vdev_tree
,
8272 "Print vdev tree to zfs_dbgmsg during pool import");
8275 module_param(zio_taskq_batch_pct
, uint
, 0444);
8276 MODULE_PARM_DESC(zio_taskq_batch_pct
,
8277 "Percentage of CPUs to run an IO worker thread");
8280 module_param(zfs_max_missing_tvds
, ulong
, 0644);
8281 MODULE_PARM_DESC(zfs_max_missing_tvds
,
8282 "Allow importing pool with up to this number of missing top-level vdevs"
8283 " (in read-only mode)");