4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2017 by Delphix. All rights reserved.
25 * Copyright (c) 2018, Nexenta Systems, Inc. All rights reserved.
26 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
27 * Copyright 2013 Saso Kiselkov. All rights reserved.
28 * Copyright (c) 2014 Integros [integros.com]
29 * Copyright 2016 Toomas Soome <tsoome@me.com>
30 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
31 * Copyright 2018 Joyent, Inc.
32 * Copyright (c) 2017 Datto Inc.
33 * Copyright 2017 Joyent, Inc.
34 * Copyright (c) 2017, Intel Corporation.
38 * SPA: Storage Pool Allocator
40 * This file contains all the routines used when modifying on-disk SPA state.
41 * This includes opening, importing, destroying, exporting a pool, and syncing a
45 #include <sys/zfs_context.h>
46 #include <sys/fm/fs/zfs.h>
47 #include <sys/spa_impl.h>
49 #include <sys/zio_checksum.h>
51 #include <sys/dmu_tx.h>
55 #include <sys/vdev_impl.h>
56 #include <sys/vdev_removal.h>
57 #include <sys/vdev_indirect_mapping.h>
58 #include <sys/vdev_indirect_births.h>
59 #include <sys/vdev_initialize.h>
60 #include <sys/vdev_disk.h>
61 #include <sys/metaslab.h>
62 #include <sys/metaslab_impl.h>
64 #include <sys/uberblock_impl.h>
67 #include <sys/bpobj.h>
68 #include <sys/dmu_traverse.h>
69 #include <sys/dmu_objset.h>
70 #include <sys/unique.h>
71 #include <sys/dsl_pool.h>
72 #include <sys/dsl_dataset.h>
73 #include <sys/dsl_dir.h>
74 #include <sys/dsl_prop.h>
75 #include <sys/dsl_synctask.h>
76 #include <sys/fs/zfs.h>
78 #include <sys/callb.h>
79 #include <sys/systeminfo.h>
80 #include <sys/spa_boot.h>
81 #include <sys/zfs_ioctl.h>
82 #include <sys/dsl_scan.h>
83 #include <sys/zfeature.h>
84 #include <sys/dsl_destroy.h>
88 #include <sys/fm/protocol.h>
89 #include <sys/fm/util.h>
90 #include <sys/callb.h>
95 #include "zfs_comutil.h"
98 * The interval, in seconds, at which failed configuration cache file writes
101 int zfs_ccw_retry_interval
= 300;
103 typedef enum zti_modes
{
104 ZTI_MODE_FIXED
, /* value is # of threads (min 1) */
105 ZTI_MODE_BATCH
, /* cpu-intensive; value is ignored */
106 ZTI_MODE_NULL
, /* don't create a taskq */
110 #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) }
111 #define ZTI_PCT(n) { ZTI_MODE_ONLINE_PERCENT, (n), 1 }
112 #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 }
113 #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 }
115 #define ZTI_N(n) ZTI_P(n, 1)
116 #define ZTI_ONE ZTI_N(1)
118 typedef struct zio_taskq_info
{
119 zti_modes_t zti_mode
;
124 static const char *const zio_taskq_types
[ZIO_TASKQ_TYPES
] = {
125 "iss", "iss_h", "int", "int_h"
129 * This table defines the taskq settings for each ZFS I/O type. When
130 * initializing a pool, we use this table to create an appropriately sized
131 * taskq. Some operations are low volume and therefore have a small, static
132 * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
133 * macros. Other operations process a large amount of data; the ZTI_BATCH
134 * macro causes us to create a taskq oriented for throughput. Some operations
135 * are so high frequency and short-lived that the taskq itself can become a a
136 * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
137 * additional degree of parallelism specified by the number of threads per-
138 * taskq and the number of taskqs; when dispatching an event in this case, the
139 * particular taskq is chosen at random.
141 * The different taskq priorities are to handle the different contexts (issue
142 * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that
143 * need to be handled with minimum delay.
145 const zio_taskq_info_t zio_taskqs
[ZIO_TYPES
][ZIO_TASKQ_TYPES
] = {
146 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
147 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* NULL */
148 { ZTI_N(8), ZTI_NULL
, ZTI_P(12, 8), ZTI_NULL
}, /* READ */
149 { ZTI_BATCH
, ZTI_N(5), ZTI_P(12, 8), ZTI_N(5) }, /* WRITE */
150 { ZTI_P(12, 8), ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* FREE */
151 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* CLAIM */
152 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* IOCTL */
155 static void spa_sync_version(void *arg
, dmu_tx_t
*tx
);
156 static void spa_sync_props(void *arg
, dmu_tx_t
*tx
);
157 static boolean_t
spa_has_active_shared_spare(spa_t
*spa
);
158 static int spa_load_impl(spa_t
*spa
, spa_import_type_t type
, char **ereport
);
159 static void spa_vdev_resilver_done(spa_t
*spa
);
161 uint_t zio_taskq_batch_pct
= 75; /* 1 thread per cpu in pset */
162 boolean_t zio_taskq_sysdc
= B_TRUE
; /* use SDC scheduling class */
163 uint_t zio_taskq_basedc
= 80; /* base duty cycle */
165 boolean_t spa_create_process
= B_TRUE
; /* no process ==> no sysdc */
168 * Report any spa_load_verify errors found, but do not fail spa_load.
169 * This is used by zdb to analyze non-idle pools.
171 boolean_t spa_load_verify_dryrun
= B_FALSE
;
174 * This (illegal) pool name is used when temporarily importing a spa_t in order
175 * to get the vdev stats associated with the imported devices.
177 #define TRYIMPORT_NAME "$import"
180 * For debugging purposes: print out vdev tree during pool import.
182 int spa_load_print_vdev_tree
= B_FALSE
;
185 * A non-zero value for zfs_max_missing_tvds means that we allow importing
186 * pools with missing top-level vdevs. This is strictly intended for advanced
187 * pool recovery cases since missing data is almost inevitable. Pools with
188 * missing devices can only be imported read-only for safety reasons, and their
189 * fail-mode will be automatically set to "continue".
191 * With 1 missing vdev we should be able to import the pool and mount all
192 * datasets. User data that was not modified after the missing device has been
193 * added should be recoverable. This means that snapshots created prior to the
194 * addition of that device should be completely intact.
196 * With 2 missing vdevs, some datasets may fail to mount since there are
197 * dataset statistics that are stored as regular metadata. Some data might be
198 * recoverable if those vdevs were added recently.
200 * With 3 or more missing vdevs, the pool is severely damaged and MOS entries
201 * may be missing entirely. Chances of data recovery are very low. Note that
202 * there are also risks of performing an inadvertent rewind as we might be
203 * missing all the vdevs with the latest uberblocks.
205 unsigned long zfs_max_missing_tvds
= 0;
208 * The parameters below are similar to zfs_max_missing_tvds but are only
209 * intended for a preliminary open of the pool with an untrusted config which
210 * might be incomplete or out-dated.
212 * We are more tolerant for pools opened from a cachefile since we could have
213 * an out-dated cachefile where a device removal was not registered.
214 * We could have set the limit arbitrarily high but in the case where devices
215 * are really missing we would want to return the proper error codes; we chose
216 * SPA_DVAS_PER_BP - 1 so that some copies of the MOS would still be available
217 * and we get a chance to retrieve the trusted config.
219 uint64_t zfs_max_missing_tvds_cachefile
= SPA_DVAS_PER_BP
- 1;
222 * In the case where config was assembled by scanning device paths (/dev/dsks
223 * by default) we are less tolerant since all the existing devices should have
224 * been detected and we want spa_load to return the right error codes.
226 uint64_t zfs_max_missing_tvds_scan
= 0;
229 * Debugging aid that pauses spa_sync() towards the end.
231 boolean_t zfs_pause_spa_sync
= B_FALSE
;
234 * ==========================================================================
235 * SPA properties routines
236 * ==========================================================================
240 * Add a (source=src, propname=propval) list to an nvlist.
243 spa_prop_add_list(nvlist_t
*nvl
, zpool_prop_t prop
, char *strval
,
244 uint64_t intval
, zprop_source_t src
)
246 const char *propname
= zpool_prop_to_name(prop
);
249 VERIFY(nvlist_alloc(&propval
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
250 VERIFY(nvlist_add_uint64(propval
, ZPROP_SOURCE
, src
) == 0);
253 VERIFY(nvlist_add_string(propval
, ZPROP_VALUE
, strval
) == 0);
255 VERIFY(nvlist_add_uint64(propval
, ZPROP_VALUE
, intval
) == 0);
257 VERIFY(nvlist_add_nvlist(nvl
, propname
, propval
) == 0);
258 nvlist_free(propval
);
262 * Get property values from the spa configuration.
265 spa_prop_get_config(spa_t
*spa
, nvlist_t
**nvp
)
267 vdev_t
*rvd
= spa
->spa_root_vdev
;
268 dsl_pool_t
*pool
= spa
->spa_dsl_pool
;
269 uint64_t size
, alloc
, cap
, version
;
270 const zprop_source_t src
= ZPROP_SRC_NONE
;
271 spa_config_dirent_t
*dp
;
272 metaslab_class_t
*mc
= spa_normal_class(spa
);
274 ASSERT(MUTEX_HELD(&spa
->spa_props_lock
));
277 alloc
= metaslab_class_get_alloc(mc
);
278 alloc
+= metaslab_class_get_alloc(spa_special_class(spa
));
279 alloc
+= metaslab_class_get_alloc(spa_dedup_class(spa
));
281 size
= metaslab_class_get_space(mc
);
282 size
+= metaslab_class_get_space(spa_special_class(spa
));
283 size
+= metaslab_class_get_space(spa_dedup_class(spa
));
285 spa_prop_add_list(*nvp
, ZPOOL_PROP_NAME
, spa_name(spa
), 0, src
);
286 spa_prop_add_list(*nvp
, ZPOOL_PROP_SIZE
, NULL
, size
, src
);
287 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALLOCATED
, NULL
, alloc
, src
);
288 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREE
, NULL
,
290 spa_prop_add_list(*nvp
, ZPOOL_PROP_CHECKPOINT
, NULL
,
291 spa
->spa_checkpoint_info
.sci_dspace
, src
);
293 spa_prop_add_list(*nvp
, ZPOOL_PROP_FRAGMENTATION
, NULL
,
294 metaslab_class_fragmentation(mc
), src
);
295 spa_prop_add_list(*nvp
, ZPOOL_PROP_EXPANDSZ
, NULL
,
296 metaslab_class_expandable_space(mc
), src
);
297 spa_prop_add_list(*nvp
, ZPOOL_PROP_READONLY
, NULL
,
298 (spa_mode(spa
) == FREAD
), src
);
300 cap
= (size
== 0) ? 0 : (alloc
* 100 / size
);
301 spa_prop_add_list(*nvp
, ZPOOL_PROP_CAPACITY
, NULL
, cap
, src
);
303 spa_prop_add_list(*nvp
, ZPOOL_PROP_DEDUPRATIO
, NULL
,
304 ddt_get_pool_dedup_ratio(spa
), src
);
306 spa_prop_add_list(*nvp
, ZPOOL_PROP_HEALTH
, NULL
,
307 rvd
->vdev_state
, src
);
309 version
= spa_version(spa
);
310 if (version
== zpool_prop_default_numeric(ZPOOL_PROP_VERSION
)) {
311 spa_prop_add_list(*nvp
, ZPOOL_PROP_VERSION
, NULL
,
312 version
, ZPROP_SRC_DEFAULT
);
314 spa_prop_add_list(*nvp
, ZPOOL_PROP_VERSION
, NULL
,
315 version
, ZPROP_SRC_LOCAL
);
317 spa_prop_add_list(*nvp
, ZPOOL_PROP_LOAD_GUID
,
318 NULL
, spa_load_guid(spa
), src
);
323 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
324 * when opening pools before this version freedir will be NULL.
326 if (pool
->dp_free_dir
!= NULL
) {
327 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
, NULL
,
328 dsl_dir_phys(pool
->dp_free_dir
)->dd_used_bytes
,
331 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
,
335 if (pool
->dp_leak_dir
!= NULL
) {
336 spa_prop_add_list(*nvp
, ZPOOL_PROP_LEAKED
, NULL
,
337 dsl_dir_phys(pool
->dp_leak_dir
)->dd_used_bytes
,
340 spa_prop_add_list(*nvp
, ZPOOL_PROP_LEAKED
,
345 spa_prop_add_list(*nvp
, ZPOOL_PROP_GUID
, NULL
, spa_guid(spa
), src
);
347 if (spa
->spa_comment
!= NULL
) {
348 spa_prop_add_list(*nvp
, ZPOOL_PROP_COMMENT
, spa
->spa_comment
,
352 if (spa
->spa_root
!= NULL
)
353 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALTROOT
, spa
->spa_root
,
356 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_BLOCKS
)) {
357 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXBLOCKSIZE
, NULL
,
358 MIN(zfs_max_recordsize
, SPA_MAXBLOCKSIZE
), ZPROP_SRC_NONE
);
360 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXBLOCKSIZE
, NULL
,
361 SPA_OLD_MAXBLOCKSIZE
, ZPROP_SRC_NONE
);
364 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_DNODE
)) {
365 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXDNODESIZE
, NULL
,
366 DNODE_MAX_SIZE
, ZPROP_SRC_NONE
);
368 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXDNODESIZE
, NULL
,
369 DNODE_MIN_SIZE
, ZPROP_SRC_NONE
);
372 if ((dp
= list_head(&spa
->spa_config_list
)) != NULL
) {
373 if (dp
->scd_path
== NULL
) {
374 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
375 "none", 0, ZPROP_SRC_LOCAL
);
376 } else if (strcmp(dp
->scd_path
, spa_config_path
) != 0) {
377 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
378 dp
->scd_path
, 0, ZPROP_SRC_LOCAL
);
384 * Get zpool property values.
387 spa_prop_get(spa_t
*spa
, nvlist_t
**nvp
)
389 objset_t
*mos
= spa
->spa_meta_objset
;
394 err
= nvlist_alloc(nvp
, NV_UNIQUE_NAME
, KM_SLEEP
);
398 mutex_enter(&spa
->spa_props_lock
);
401 * Get properties from the spa config.
403 spa_prop_get_config(spa
, nvp
);
405 /* If no pool property object, no more prop to get. */
406 if (mos
== NULL
|| spa
->spa_pool_props_object
== 0) {
407 mutex_exit(&spa
->spa_props_lock
);
412 * Get properties from the MOS pool property object.
414 for (zap_cursor_init(&zc
, mos
, spa
->spa_pool_props_object
);
415 (err
= zap_cursor_retrieve(&zc
, &za
)) == 0;
416 zap_cursor_advance(&zc
)) {
419 zprop_source_t src
= ZPROP_SRC_DEFAULT
;
422 if ((prop
= zpool_name_to_prop(za
.za_name
)) == ZPOOL_PROP_INVAL
)
425 switch (za
.za_integer_length
) {
427 /* integer property */
428 if (za
.za_first_integer
!=
429 zpool_prop_default_numeric(prop
))
430 src
= ZPROP_SRC_LOCAL
;
432 if (prop
== ZPOOL_PROP_BOOTFS
) {
434 dsl_dataset_t
*ds
= NULL
;
436 dp
= spa_get_dsl(spa
);
437 dsl_pool_config_enter(dp
, FTAG
);
438 err
= dsl_dataset_hold_obj(dp
,
439 za
.za_first_integer
, FTAG
, &ds
);
441 dsl_pool_config_exit(dp
, FTAG
);
445 strval
= kmem_alloc(ZFS_MAX_DATASET_NAME_LEN
,
447 dsl_dataset_name(ds
, strval
);
448 dsl_dataset_rele(ds
, FTAG
);
449 dsl_pool_config_exit(dp
, FTAG
);
452 intval
= za
.za_first_integer
;
455 spa_prop_add_list(*nvp
, prop
, strval
, intval
, src
);
458 kmem_free(strval
, ZFS_MAX_DATASET_NAME_LEN
);
463 /* string property */
464 strval
= kmem_alloc(za
.za_num_integers
, KM_SLEEP
);
465 err
= zap_lookup(mos
, spa
->spa_pool_props_object
,
466 za
.za_name
, 1, za
.za_num_integers
, strval
);
468 kmem_free(strval
, za
.za_num_integers
);
471 spa_prop_add_list(*nvp
, prop
, strval
, 0, src
);
472 kmem_free(strval
, za
.za_num_integers
);
479 zap_cursor_fini(&zc
);
480 mutex_exit(&spa
->spa_props_lock
);
482 if (err
&& err
!= ENOENT
) {
492 * Validate the given pool properties nvlist and modify the list
493 * for the property values to be set.
496 spa_prop_validate(spa_t
*spa
, nvlist_t
*props
)
499 int error
= 0, reset_bootfs
= 0;
501 boolean_t has_feature
= B_FALSE
;
504 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
506 char *strval
, *slash
, *check
, *fname
;
507 const char *propname
= nvpair_name(elem
);
508 zpool_prop_t prop
= zpool_name_to_prop(propname
);
511 case ZPOOL_PROP_INVAL
:
512 if (!zpool_prop_feature(propname
)) {
513 error
= SET_ERROR(EINVAL
);
518 * Sanitize the input.
520 if (nvpair_type(elem
) != DATA_TYPE_UINT64
) {
521 error
= SET_ERROR(EINVAL
);
525 if (nvpair_value_uint64(elem
, &intval
) != 0) {
526 error
= SET_ERROR(EINVAL
);
531 error
= SET_ERROR(EINVAL
);
535 fname
= strchr(propname
, '@') + 1;
536 if (zfeature_lookup_name(fname
, NULL
) != 0) {
537 error
= SET_ERROR(EINVAL
);
541 has_feature
= B_TRUE
;
544 case ZPOOL_PROP_VERSION
:
545 error
= nvpair_value_uint64(elem
, &intval
);
547 (intval
< spa_version(spa
) ||
548 intval
> SPA_VERSION_BEFORE_FEATURES
||
550 error
= SET_ERROR(EINVAL
);
553 case ZPOOL_PROP_DELEGATION
:
554 case ZPOOL_PROP_AUTOREPLACE
:
555 case ZPOOL_PROP_LISTSNAPS
:
556 case ZPOOL_PROP_AUTOEXPAND
:
557 error
= nvpair_value_uint64(elem
, &intval
);
558 if (!error
&& intval
> 1)
559 error
= SET_ERROR(EINVAL
);
562 case ZPOOL_PROP_MULTIHOST
:
563 error
= nvpair_value_uint64(elem
, &intval
);
564 if (!error
&& intval
> 1)
565 error
= SET_ERROR(EINVAL
);
567 if (!error
&& !spa_get_hostid())
568 error
= SET_ERROR(ENOTSUP
);
572 case ZPOOL_PROP_BOOTFS
:
574 * If the pool version is less than SPA_VERSION_BOOTFS,
575 * or the pool is still being created (version == 0),
576 * the bootfs property cannot be set.
578 if (spa_version(spa
) < SPA_VERSION_BOOTFS
) {
579 error
= SET_ERROR(ENOTSUP
);
584 * Make sure the vdev config is bootable
586 if (!vdev_is_bootable(spa
->spa_root_vdev
)) {
587 error
= SET_ERROR(ENOTSUP
);
593 error
= nvpair_value_string(elem
, &strval
);
599 if (strval
== NULL
|| strval
[0] == '\0') {
600 objnum
= zpool_prop_default_numeric(
605 error
= dmu_objset_hold(strval
, FTAG
, &os
);
610 * Must be ZPL, and its property settings
611 * must be supported by GRUB (compression
612 * is not gzip, and large blocks or large
613 * dnodes are not used).
616 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
617 error
= SET_ERROR(ENOTSUP
);
619 dsl_prop_get_int_ds(dmu_objset_ds(os
),
620 zfs_prop_to_name(ZFS_PROP_COMPRESSION
),
622 !BOOTFS_COMPRESS_VALID(propval
)) {
623 error
= SET_ERROR(ENOTSUP
);
625 dsl_prop_get_int_ds(dmu_objset_ds(os
),
626 zfs_prop_to_name(ZFS_PROP_DNODESIZE
),
628 propval
!= ZFS_DNSIZE_LEGACY
) {
629 error
= SET_ERROR(ENOTSUP
);
631 objnum
= dmu_objset_id(os
);
633 dmu_objset_rele(os
, FTAG
);
637 case ZPOOL_PROP_FAILUREMODE
:
638 error
= nvpair_value_uint64(elem
, &intval
);
639 if (!error
&& intval
> ZIO_FAILURE_MODE_PANIC
)
640 error
= SET_ERROR(EINVAL
);
643 * This is a special case which only occurs when
644 * the pool has completely failed. This allows
645 * the user to change the in-core failmode property
646 * without syncing it out to disk (I/Os might
647 * currently be blocked). We do this by returning
648 * EIO to the caller (spa_prop_set) to trick it
649 * into thinking we encountered a property validation
652 if (!error
&& spa_suspended(spa
)) {
653 spa
->spa_failmode
= intval
;
654 error
= SET_ERROR(EIO
);
658 case ZPOOL_PROP_CACHEFILE
:
659 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
662 if (strval
[0] == '\0')
665 if (strcmp(strval
, "none") == 0)
668 if (strval
[0] != '/') {
669 error
= SET_ERROR(EINVAL
);
673 slash
= strrchr(strval
, '/');
674 ASSERT(slash
!= NULL
);
676 if (slash
[1] == '\0' || strcmp(slash
, "/.") == 0 ||
677 strcmp(slash
, "/..") == 0)
678 error
= SET_ERROR(EINVAL
);
681 case ZPOOL_PROP_COMMENT
:
682 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
684 for (check
= strval
; *check
!= '\0'; check
++) {
685 if (!isprint(*check
)) {
686 error
= SET_ERROR(EINVAL
);
690 if (strlen(strval
) > ZPROP_MAX_COMMENT
)
691 error
= SET_ERROR(E2BIG
);
694 case ZPOOL_PROP_DEDUPDITTO
:
695 if (spa_version(spa
) < SPA_VERSION_DEDUP
)
696 error
= SET_ERROR(ENOTSUP
);
698 error
= nvpair_value_uint64(elem
, &intval
);
700 intval
!= 0 && intval
< ZIO_DEDUPDITTO_MIN
)
701 error
= SET_ERROR(EINVAL
);
712 if (!error
&& reset_bootfs
) {
713 error
= nvlist_remove(props
,
714 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), DATA_TYPE_STRING
);
717 error
= nvlist_add_uint64(props
,
718 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), objnum
);
726 spa_configfile_set(spa_t
*spa
, nvlist_t
*nvp
, boolean_t need_sync
)
729 spa_config_dirent_t
*dp
;
731 if (nvlist_lookup_string(nvp
, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
),
735 dp
= kmem_alloc(sizeof (spa_config_dirent_t
),
738 if (cachefile
[0] == '\0')
739 dp
->scd_path
= spa_strdup(spa_config_path
);
740 else if (strcmp(cachefile
, "none") == 0)
743 dp
->scd_path
= spa_strdup(cachefile
);
745 list_insert_head(&spa
->spa_config_list
, dp
);
747 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
751 spa_prop_set(spa_t
*spa
, nvlist_t
*nvp
)
754 nvpair_t
*elem
= NULL
;
755 boolean_t need_sync
= B_FALSE
;
757 if ((error
= spa_prop_validate(spa
, nvp
)) != 0)
760 while ((elem
= nvlist_next_nvpair(nvp
, elem
)) != NULL
) {
761 zpool_prop_t prop
= zpool_name_to_prop(nvpair_name(elem
));
763 if (prop
== ZPOOL_PROP_CACHEFILE
||
764 prop
== ZPOOL_PROP_ALTROOT
||
765 prop
== ZPOOL_PROP_READONLY
)
768 if (prop
== ZPOOL_PROP_VERSION
|| prop
== ZPOOL_PROP_INVAL
) {
771 if (prop
== ZPOOL_PROP_VERSION
) {
772 VERIFY(nvpair_value_uint64(elem
, &ver
) == 0);
774 ASSERT(zpool_prop_feature(nvpair_name(elem
)));
775 ver
= SPA_VERSION_FEATURES
;
779 /* Save time if the version is already set. */
780 if (ver
== spa_version(spa
))
784 * In addition to the pool directory object, we might
785 * create the pool properties object, the features for
786 * read object, the features for write object, or the
787 * feature descriptions object.
789 error
= dsl_sync_task(spa
->spa_name
, NULL
,
790 spa_sync_version
, &ver
,
791 6, ZFS_SPACE_CHECK_RESERVED
);
802 return (dsl_sync_task(spa
->spa_name
, NULL
, spa_sync_props
,
803 nvp
, 6, ZFS_SPACE_CHECK_RESERVED
));
810 * If the bootfs property value is dsobj, clear it.
813 spa_prop_clear_bootfs(spa_t
*spa
, uint64_t dsobj
, dmu_tx_t
*tx
)
815 if (spa
->spa_bootfs
== dsobj
&& spa
->spa_pool_props_object
!= 0) {
816 VERIFY(zap_remove(spa
->spa_meta_objset
,
817 spa
->spa_pool_props_object
,
818 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), tx
) == 0);
825 spa_change_guid_check(void *arg
, dmu_tx_t
*tx
)
827 ASSERTV(uint64_t *newguid
= arg
);
828 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
829 vdev_t
*rvd
= spa
->spa_root_vdev
;
832 if (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
)) {
833 int error
= (spa_has_checkpoint(spa
)) ?
834 ZFS_ERR_CHECKPOINT_EXISTS
: ZFS_ERR_DISCARDING_CHECKPOINT
;
835 return (SET_ERROR(error
));
838 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
839 vdev_state
= rvd
->vdev_state
;
840 spa_config_exit(spa
, SCL_STATE
, FTAG
);
842 if (vdev_state
!= VDEV_STATE_HEALTHY
)
843 return (SET_ERROR(ENXIO
));
845 ASSERT3U(spa_guid(spa
), !=, *newguid
);
851 spa_change_guid_sync(void *arg
, dmu_tx_t
*tx
)
853 uint64_t *newguid
= arg
;
854 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
856 vdev_t
*rvd
= spa
->spa_root_vdev
;
858 oldguid
= spa_guid(spa
);
860 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
861 rvd
->vdev_guid
= *newguid
;
862 rvd
->vdev_guid_sum
+= (*newguid
- oldguid
);
863 vdev_config_dirty(rvd
);
864 spa_config_exit(spa
, SCL_STATE
, FTAG
);
866 spa_history_log_internal(spa
, "guid change", tx
, "old=%llu new=%llu",
871 * Change the GUID for the pool. This is done so that we can later
872 * re-import a pool built from a clone of our own vdevs. We will modify
873 * the root vdev's guid, our own pool guid, and then mark all of our
874 * vdevs dirty. Note that we must make sure that all our vdevs are
875 * online when we do this, or else any vdevs that weren't present
876 * would be orphaned from our pool. We are also going to issue a
877 * sysevent to update any watchers.
880 spa_change_guid(spa_t
*spa
)
885 mutex_enter(&spa
->spa_vdev_top_lock
);
886 mutex_enter(&spa_namespace_lock
);
887 guid
= spa_generate_guid(NULL
);
889 error
= dsl_sync_task(spa
->spa_name
, spa_change_guid_check
,
890 spa_change_guid_sync
, &guid
, 5, ZFS_SPACE_CHECK_RESERVED
);
893 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
);
894 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_REGUID
);
897 mutex_exit(&spa_namespace_lock
);
898 mutex_exit(&spa
->spa_vdev_top_lock
);
904 * ==========================================================================
905 * SPA state manipulation (open/create/destroy/import/export)
906 * ==========================================================================
910 spa_error_entry_compare(const void *a
, const void *b
)
912 const spa_error_entry_t
*sa
= (const spa_error_entry_t
*)a
;
913 const spa_error_entry_t
*sb
= (const spa_error_entry_t
*)b
;
916 ret
= memcmp(&sa
->se_bookmark
, &sb
->se_bookmark
,
917 sizeof (zbookmark_phys_t
));
919 return (AVL_ISIGN(ret
));
923 * Utility function which retrieves copies of the current logs and
924 * re-initializes them in the process.
927 spa_get_errlists(spa_t
*spa
, avl_tree_t
*last
, avl_tree_t
*scrub
)
929 ASSERT(MUTEX_HELD(&spa
->spa_errlist_lock
));
931 bcopy(&spa
->spa_errlist_last
, last
, sizeof (avl_tree_t
));
932 bcopy(&spa
->spa_errlist_scrub
, scrub
, sizeof (avl_tree_t
));
934 avl_create(&spa
->spa_errlist_scrub
,
935 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
936 offsetof(spa_error_entry_t
, se_avl
));
937 avl_create(&spa
->spa_errlist_last
,
938 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
939 offsetof(spa_error_entry_t
, se_avl
));
943 spa_taskqs_init(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
945 const zio_taskq_info_t
*ztip
= &zio_taskqs
[t
][q
];
946 enum zti_modes mode
= ztip
->zti_mode
;
947 uint_t value
= ztip
->zti_value
;
948 uint_t count
= ztip
->zti_count
;
949 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
951 boolean_t batch
= B_FALSE
;
953 if (mode
== ZTI_MODE_NULL
) {
955 tqs
->stqs_taskq
= NULL
;
959 ASSERT3U(count
, >, 0);
961 tqs
->stqs_count
= count
;
962 tqs
->stqs_taskq
= kmem_alloc(count
* sizeof (taskq_t
*), KM_SLEEP
);
966 ASSERT3U(value
, >=, 1);
967 value
= MAX(value
, 1);
968 flags
|= TASKQ_DYNAMIC
;
973 flags
|= TASKQ_THREADS_CPU_PCT
;
974 value
= MIN(zio_taskq_batch_pct
, 100);
978 panic("unrecognized mode for %s_%s taskq (%u:%u) in "
980 zio_type_name
[t
], zio_taskq_types
[q
], mode
, value
);
984 for (uint_t i
= 0; i
< count
; i
++) {
988 (void) snprintf(name
, sizeof (name
), "%s_%s",
989 zio_type_name
[t
], zio_taskq_types
[q
]);
991 if (zio_taskq_sysdc
&& spa
->spa_proc
!= &p0
) {
993 flags
|= TASKQ_DC_BATCH
;
995 tq
= taskq_create_sysdc(name
, value
, 50, INT_MAX
,
996 spa
->spa_proc
, zio_taskq_basedc
, flags
);
998 pri_t pri
= maxclsyspri
;
1000 * The write issue taskq can be extremely CPU
1001 * intensive. Run it at slightly less important
1002 * priority than the other taskqs. Under Linux this
1003 * means incrementing the priority value on platforms
1004 * like illumos it should be decremented.
1006 if (t
== ZIO_TYPE_WRITE
&& q
== ZIO_TASKQ_ISSUE
)
1009 tq
= taskq_create_proc(name
, value
, pri
, 50,
1010 INT_MAX
, spa
->spa_proc
, flags
);
1013 tqs
->stqs_taskq
[i
] = tq
;
1018 spa_taskqs_fini(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
1020 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1022 if (tqs
->stqs_taskq
== NULL
) {
1023 ASSERT3U(tqs
->stqs_count
, ==, 0);
1027 for (uint_t i
= 0; i
< tqs
->stqs_count
; i
++) {
1028 ASSERT3P(tqs
->stqs_taskq
[i
], !=, NULL
);
1029 taskq_destroy(tqs
->stqs_taskq
[i
]);
1032 kmem_free(tqs
->stqs_taskq
, tqs
->stqs_count
* sizeof (taskq_t
*));
1033 tqs
->stqs_taskq
= NULL
;
1037 * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority.
1038 * Note that a type may have multiple discrete taskqs to avoid lock contention
1039 * on the taskq itself. In that case we choose which taskq at random by using
1040 * the low bits of gethrtime().
1043 spa_taskq_dispatch_ent(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
1044 task_func_t
*func
, void *arg
, uint_t flags
, taskq_ent_t
*ent
)
1046 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1049 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
1050 ASSERT3U(tqs
->stqs_count
, !=, 0);
1052 if (tqs
->stqs_count
== 1) {
1053 tq
= tqs
->stqs_taskq
[0];
1055 tq
= tqs
->stqs_taskq
[((uint64_t)gethrtime()) % tqs
->stqs_count
];
1058 taskq_dispatch_ent(tq
, func
, arg
, flags
, ent
);
1062 * Same as spa_taskq_dispatch_ent() but block on the task until completion.
1065 spa_taskq_dispatch_sync(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
1066 task_func_t
*func
, void *arg
, uint_t flags
)
1068 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1072 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
1073 ASSERT3U(tqs
->stqs_count
, !=, 0);
1075 if (tqs
->stqs_count
== 1) {
1076 tq
= tqs
->stqs_taskq
[0];
1078 tq
= tqs
->stqs_taskq
[((uint64_t)gethrtime()) % tqs
->stqs_count
];
1081 id
= taskq_dispatch(tq
, func
, arg
, flags
);
1083 taskq_wait_id(tq
, id
);
1087 spa_create_zio_taskqs(spa_t
*spa
)
1089 for (int t
= 0; t
< ZIO_TYPES
; t
++) {
1090 for (int q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1091 spa_taskqs_init(spa
, t
, q
);
1097 * Disabled until spa_thread() can be adapted for Linux.
1099 #undef HAVE_SPA_THREAD
1101 #if defined(_KERNEL) && defined(HAVE_SPA_THREAD)
1103 spa_thread(void *arg
)
1105 psetid_t zio_taskq_psrset_bind
= PS_NONE
;
1106 callb_cpr_t cprinfo
;
1109 user_t
*pu
= PTOU(curproc
);
1111 CALLB_CPR_INIT(&cprinfo
, &spa
->spa_proc_lock
, callb_generic_cpr
,
1114 ASSERT(curproc
!= &p0
);
1115 (void) snprintf(pu
->u_psargs
, sizeof (pu
->u_psargs
),
1116 "zpool-%s", spa
->spa_name
);
1117 (void) strlcpy(pu
->u_comm
, pu
->u_psargs
, sizeof (pu
->u_comm
));
1119 /* bind this thread to the requested psrset */
1120 if (zio_taskq_psrset_bind
!= PS_NONE
) {
1122 mutex_enter(&cpu_lock
);
1123 mutex_enter(&pidlock
);
1124 mutex_enter(&curproc
->p_lock
);
1126 if (cpupart_bind_thread(curthread
, zio_taskq_psrset_bind
,
1127 0, NULL
, NULL
) == 0) {
1128 curthread
->t_bind_pset
= zio_taskq_psrset_bind
;
1131 "Couldn't bind process for zfs pool \"%s\" to "
1132 "pset %d\n", spa
->spa_name
, zio_taskq_psrset_bind
);
1135 mutex_exit(&curproc
->p_lock
);
1136 mutex_exit(&pidlock
);
1137 mutex_exit(&cpu_lock
);
1141 if (zio_taskq_sysdc
) {
1142 sysdc_thread_enter(curthread
, 100, 0);
1145 spa
->spa_proc
= curproc
;
1146 spa
->spa_did
= curthread
->t_did
;
1148 spa_create_zio_taskqs(spa
);
1150 mutex_enter(&spa
->spa_proc_lock
);
1151 ASSERT(spa
->spa_proc_state
== SPA_PROC_CREATED
);
1153 spa
->spa_proc_state
= SPA_PROC_ACTIVE
;
1154 cv_broadcast(&spa
->spa_proc_cv
);
1156 CALLB_CPR_SAFE_BEGIN(&cprinfo
);
1157 while (spa
->spa_proc_state
== SPA_PROC_ACTIVE
)
1158 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1159 CALLB_CPR_SAFE_END(&cprinfo
, &spa
->spa_proc_lock
);
1161 ASSERT(spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
);
1162 spa
->spa_proc_state
= SPA_PROC_GONE
;
1163 spa
->spa_proc
= &p0
;
1164 cv_broadcast(&spa
->spa_proc_cv
);
1165 CALLB_CPR_EXIT(&cprinfo
); /* drops spa_proc_lock */
1167 mutex_enter(&curproc
->p_lock
);
1173 * Activate an uninitialized pool.
1176 spa_activate(spa_t
*spa
, int mode
)
1178 ASSERT(spa
->spa_state
== POOL_STATE_UNINITIALIZED
);
1180 spa
->spa_state
= POOL_STATE_ACTIVE
;
1181 spa
->spa_mode
= mode
;
1183 spa
->spa_normal_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
1184 spa
->spa_log_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
1185 spa
->spa_special_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
1186 spa
->spa_dedup_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
1188 /* Try to create a covering process */
1189 mutex_enter(&spa
->spa_proc_lock
);
1190 ASSERT(spa
->spa_proc_state
== SPA_PROC_NONE
);
1191 ASSERT(spa
->spa_proc
== &p0
);
1194 #ifdef HAVE_SPA_THREAD
1195 /* Only create a process if we're going to be around a while. */
1196 if (spa_create_process
&& strcmp(spa
->spa_name
, TRYIMPORT_NAME
) != 0) {
1197 if (newproc(spa_thread
, (caddr_t
)spa
, syscid
, maxclsyspri
,
1199 spa
->spa_proc_state
= SPA_PROC_CREATED
;
1200 while (spa
->spa_proc_state
== SPA_PROC_CREATED
) {
1201 cv_wait(&spa
->spa_proc_cv
,
1202 &spa
->spa_proc_lock
);
1204 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1205 ASSERT(spa
->spa_proc
!= &p0
);
1206 ASSERT(spa
->spa_did
!= 0);
1210 "Couldn't create process for zfs pool \"%s\"\n",
1215 #endif /* HAVE_SPA_THREAD */
1216 mutex_exit(&spa
->spa_proc_lock
);
1218 /* If we didn't create a process, we need to create our taskqs. */
1219 if (spa
->spa_proc
== &p0
) {
1220 spa_create_zio_taskqs(spa
);
1223 for (size_t i
= 0; i
< TXG_SIZE
; i
++) {
1224 spa
->spa_txg_zio
[i
] = zio_root(spa
, NULL
, NULL
,
1228 list_create(&spa
->spa_config_dirty_list
, sizeof (vdev_t
),
1229 offsetof(vdev_t
, vdev_config_dirty_node
));
1230 list_create(&spa
->spa_evicting_os_list
, sizeof (objset_t
),
1231 offsetof(objset_t
, os_evicting_node
));
1232 list_create(&spa
->spa_state_dirty_list
, sizeof (vdev_t
),
1233 offsetof(vdev_t
, vdev_state_dirty_node
));
1235 txg_list_create(&spa
->spa_vdev_txg_list
, spa
,
1236 offsetof(struct vdev
, vdev_txg_node
));
1238 avl_create(&spa
->spa_errlist_scrub
,
1239 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1240 offsetof(spa_error_entry_t
, se_avl
));
1241 avl_create(&spa
->spa_errlist_last
,
1242 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1243 offsetof(spa_error_entry_t
, se_avl
));
1245 spa_keystore_init(&spa
->spa_keystore
);
1248 * This taskq is used to perform zvol-minor-related tasks
1249 * asynchronously. This has several advantages, including easy
1250 * resolution of various deadlocks (zfsonlinux bug #3681).
1252 * The taskq must be single threaded to ensure tasks are always
1253 * processed in the order in which they were dispatched.
1255 * A taskq per pool allows one to keep the pools independent.
1256 * This way if one pool is suspended, it will not impact another.
1258 * The preferred location to dispatch a zvol minor task is a sync
1259 * task. In this context, there is easy access to the spa_t and minimal
1260 * error handling is required because the sync task must succeed.
1262 spa
->spa_zvol_taskq
= taskq_create("z_zvol", 1, defclsyspri
,
1266 * Taskq dedicated to prefetcher threads: this is used to prevent the
1267 * pool traverse code from monopolizing the global (and limited)
1268 * system_taskq by inappropriately scheduling long running tasks on it.
1270 spa
->spa_prefetch_taskq
= taskq_create("z_prefetch", boot_ncpus
,
1271 defclsyspri
, 1, INT_MAX
, TASKQ_DYNAMIC
);
1274 * The taskq to upgrade datasets in this pool. Currently used by
1275 * feature SPA_FEATURE_USEROBJ_ACCOUNTING/SPA_FEATURE_PROJECT_QUOTA.
1277 spa
->spa_upgrade_taskq
= taskq_create("z_upgrade", boot_ncpus
,
1278 defclsyspri
, 1, INT_MAX
, TASKQ_DYNAMIC
);
1282 * Opposite of spa_activate().
1285 spa_deactivate(spa_t
*spa
)
1287 ASSERT(spa
->spa_sync_on
== B_FALSE
);
1288 ASSERT(spa
->spa_dsl_pool
== NULL
);
1289 ASSERT(spa
->spa_root_vdev
== NULL
);
1290 ASSERT(spa
->spa_async_zio_root
== NULL
);
1291 ASSERT(spa
->spa_state
!= POOL_STATE_UNINITIALIZED
);
1293 spa_evicting_os_wait(spa
);
1295 if (spa
->spa_zvol_taskq
) {
1296 taskq_destroy(spa
->spa_zvol_taskq
);
1297 spa
->spa_zvol_taskq
= NULL
;
1300 if (spa
->spa_prefetch_taskq
) {
1301 taskq_destroy(spa
->spa_prefetch_taskq
);
1302 spa
->spa_prefetch_taskq
= NULL
;
1305 if (spa
->spa_upgrade_taskq
) {
1306 taskq_destroy(spa
->spa_upgrade_taskq
);
1307 spa
->spa_upgrade_taskq
= NULL
;
1310 txg_list_destroy(&spa
->spa_vdev_txg_list
);
1312 list_destroy(&spa
->spa_config_dirty_list
);
1313 list_destroy(&spa
->spa_evicting_os_list
);
1314 list_destroy(&spa
->spa_state_dirty_list
);
1316 taskq_cancel_id(system_delay_taskq
, spa
->spa_deadman_tqid
);
1318 for (int t
= 0; t
< ZIO_TYPES
; t
++) {
1319 for (int q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1320 spa_taskqs_fini(spa
, t
, q
);
1324 for (size_t i
= 0; i
< TXG_SIZE
; i
++) {
1325 ASSERT3P(spa
->spa_txg_zio
[i
], !=, NULL
);
1326 VERIFY0(zio_wait(spa
->spa_txg_zio
[i
]));
1327 spa
->spa_txg_zio
[i
] = NULL
;
1330 metaslab_class_destroy(spa
->spa_normal_class
);
1331 spa
->spa_normal_class
= NULL
;
1333 metaslab_class_destroy(spa
->spa_log_class
);
1334 spa
->spa_log_class
= NULL
;
1336 metaslab_class_destroy(spa
->spa_special_class
);
1337 spa
->spa_special_class
= NULL
;
1339 metaslab_class_destroy(spa
->spa_dedup_class
);
1340 spa
->spa_dedup_class
= NULL
;
1343 * If this was part of an import or the open otherwise failed, we may
1344 * still have errors left in the queues. Empty them just in case.
1346 spa_errlog_drain(spa
);
1347 avl_destroy(&spa
->spa_errlist_scrub
);
1348 avl_destroy(&spa
->spa_errlist_last
);
1350 spa_keystore_fini(&spa
->spa_keystore
);
1352 spa
->spa_state
= POOL_STATE_UNINITIALIZED
;
1354 mutex_enter(&spa
->spa_proc_lock
);
1355 if (spa
->spa_proc_state
!= SPA_PROC_NONE
) {
1356 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1357 spa
->spa_proc_state
= SPA_PROC_DEACTIVATE
;
1358 cv_broadcast(&spa
->spa_proc_cv
);
1359 while (spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
) {
1360 ASSERT(spa
->spa_proc
!= &p0
);
1361 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1363 ASSERT(spa
->spa_proc_state
== SPA_PROC_GONE
);
1364 spa
->spa_proc_state
= SPA_PROC_NONE
;
1366 ASSERT(spa
->spa_proc
== &p0
);
1367 mutex_exit(&spa
->spa_proc_lock
);
1370 * We want to make sure spa_thread() has actually exited the ZFS
1371 * module, so that the module can't be unloaded out from underneath
1374 if (spa
->spa_did
!= 0) {
1375 thread_join(spa
->spa_did
);
1381 * Verify a pool configuration, and construct the vdev tree appropriately. This
1382 * will create all the necessary vdevs in the appropriate layout, with each vdev
1383 * in the CLOSED state. This will prep the pool before open/creation/import.
1384 * All vdev validation is done by the vdev_alloc() routine.
1387 spa_config_parse(spa_t
*spa
, vdev_t
**vdp
, nvlist_t
*nv
, vdev_t
*parent
,
1388 uint_t id
, int atype
)
1394 if ((error
= vdev_alloc(spa
, vdp
, nv
, parent
, id
, atype
)) != 0)
1397 if ((*vdp
)->vdev_ops
->vdev_op_leaf
)
1400 error
= nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1403 if (error
== ENOENT
)
1409 return (SET_ERROR(EINVAL
));
1412 for (int c
= 0; c
< children
; c
++) {
1414 if ((error
= spa_config_parse(spa
, &vd
, child
[c
], *vdp
, c
,
1422 ASSERT(*vdp
!= NULL
);
1428 * Opposite of spa_load().
1431 spa_unload(spa_t
*spa
)
1435 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1437 spa_load_note(spa
, "UNLOADING");
1442 spa_async_suspend(spa
);
1444 if (spa
->spa_root_vdev
) {
1445 vdev_initialize_stop_all(spa
->spa_root_vdev
,
1446 VDEV_INITIALIZE_ACTIVE
);
1452 if (spa
->spa_sync_on
) {
1453 txg_sync_stop(spa
->spa_dsl_pool
);
1454 spa
->spa_sync_on
= B_FALSE
;
1458 * Even though vdev_free() also calls vdev_metaslab_fini, we need
1459 * to call it earlier, before we wait for async i/o to complete.
1460 * This ensures that there is no async metaslab prefetching, by
1461 * calling taskq_wait(mg_taskq).
1463 if (spa
->spa_root_vdev
!= NULL
) {
1464 spa_config_enter(spa
, SCL_ALL
, spa
, RW_WRITER
);
1465 for (int c
= 0; c
< spa
->spa_root_vdev
->vdev_children
; c
++)
1466 vdev_metaslab_fini(spa
->spa_root_vdev
->vdev_child
[c
]);
1467 spa_config_exit(spa
, SCL_ALL
, spa
);
1470 if (spa
->spa_mmp
.mmp_thread
)
1471 mmp_thread_stop(spa
);
1474 * Wait for any outstanding async I/O to complete.
1476 if (spa
->spa_async_zio_root
!= NULL
) {
1477 for (int i
= 0; i
< max_ncpus
; i
++)
1478 (void) zio_wait(spa
->spa_async_zio_root
[i
]);
1479 kmem_free(spa
->spa_async_zio_root
, max_ncpus
* sizeof (void *));
1480 spa
->spa_async_zio_root
= NULL
;
1483 if (spa
->spa_vdev_removal
!= NULL
) {
1484 spa_vdev_removal_destroy(spa
->spa_vdev_removal
);
1485 spa
->spa_vdev_removal
= NULL
;
1488 if (spa
->spa_condense_zthr
!= NULL
) {
1489 ASSERT(!zthr_isrunning(spa
->spa_condense_zthr
));
1490 zthr_destroy(spa
->spa_condense_zthr
);
1491 spa
->spa_condense_zthr
= NULL
;
1494 if (spa
->spa_checkpoint_discard_zthr
!= NULL
) {
1495 ASSERT(!zthr_isrunning(spa
->spa_checkpoint_discard_zthr
));
1496 zthr_destroy(spa
->spa_checkpoint_discard_zthr
);
1497 spa
->spa_checkpoint_discard_zthr
= NULL
;
1500 spa_condense_fini(spa
);
1502 bpobj_close(&spa
->spa_deferred_bpobj
);
1504 spa_config_enter(spa
, SCL_ALL
, spa
, RW_WRITER
);
1509 if (spa
->spa_root_vdev
)
1510 vdev_free(spa
->spa_root_vdev
);
1511 ASSERT(spa
->spa_root_vdev
== NULL
);
1514 * Close the dsl pool.
1516 if (spa
->spa_dsl_pool
) {
1517 dsl_pool_close(spa
->spa_dsl_pool
);
1518 spa
->spa_dsl_pool
= NULL
;
1519 spa
->spa_meta_objset
= NULL
;
1525 * Drop and purge level 2 cache
1527 spa_l2cache_drop(spa
);
1529 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1530 vdev_free(spa
->spa_spares
.sav_vdevs
[i
]);
1531 if (spa
->spa_spares
.sav_vdevs
) {
1532 kmem_free(spa
->spa_spares
.sav_vdevs
,
1533 spa
->spa_spares
.sav_count
* sizeof (void *));
1534 spa
->spa_spares
.sav_vdevs
= NULL
;
1536 if (spa
->spa_spares
.sav_config
) {
1537 nvlist_free(spa
->spa_spares
.sav_config
);
1538 spa
->spa_spares
.sav_config
= NULL
;
1540 spa
->spa_spares
.sav_count
= 0;
1542 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
1543 vdev_clear_stats(spa
->spa_l2cache
.sav_vdevs
[i
]);
1544 vdev_free(spa
->spa_l2cache
.sav_vdevs
[i
]);
1546 if (spa
->spa_l2cache
.sav_vdevs
) {
1547 kmem_free(spa
->spa_l2cache
.sav_vdevs
,
1548 spa
->spa_l2cache
.sav_count
* sizeof (void *));
1549 spa
->spa_l2cache
.sav_vdevs
= NULL
;
1551 if (spa
->spa_l2cache
.sav_config
) {
1552 nvlist_free(spa
->spa_l2cache
.sav_config
);
1553 spa
->spa_l2cache
.sav_config
= NULL
;
1555 spa
->spa_l2cache
.sav_count
= 0;
1557 spa
->spa_async_suspended
= 0;
1559 spa
->spa_indirect_vdevs_loaded
= B_FALSE
;
1561 if (spa
->spa_comment
!= NULL
) {
1562 spa_strfree(spa
->spa_comment
);
1563 spa
->spa_comment
= NULL
;
1566 spa_config_exit(spa
, SCL_ALL
, spa
);
1570 * Load (or re-load) the current list of vdevs describing the active spares for
1571 * this pool. When this is called, we have some form of basic information in
1572 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1573 * then re-generate a more complete list including status information.
1576 spa_load_spares(spa_t
*spa
)
1585 * zdb opens both the current state of the pool and the
1586 * checkpointed state (if present), with a different spa_t.
1588 * As spare vdevs are shared among open pools, we skip loading
1589 * them when we load the checkpointed state of the pool.
1591 if (!spa_writeable(spa
))
1595 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1598 * First, close and free any existing spare vdevs.
1600 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1601 vd
= spa
->spa_spares
.sav_vdevs
[i
];
1603 /* Undo the call to spa_activate() below */
1604 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1605 B_FALSE
)) != NULL
&& tvd
->vdev_isspare
)
1606 spa_spare_remove(tvd
);
1611 if (spa
->spa_spares
.sav_vdevs
)
1612 kmem_free(spa
->spa_spares
.sav_vdevs
,
1613 spa
->spa_spares
.sav_count
* sizeof (void *));
1615 if (spa
->spa_spares
.sav_config
== NULL
)
1618 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
1619 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
1621 spa
->spa_spares
.sav_count
= (int)nspares
;
1622 spa
->spa_spares
.sav_vdevs
= NULL
;
1628 * Construct the array of vdevs, opening them to get status in the
1629 * process. For each spare, there is potentially two different vdev_t
1630 * structures associated with it: one in the list of spares (used only
1631 * for basic validation purposes) and one in the active vdev
1632 * configuration (if it's spared in). During this phase we open and
1633 * validate each vdev on the spare list. If the vdev also exists in the
1634 * active configuration, then we also mark this vdev as an active spare.
1636 spa
->spa_spares
.sav_vdevs
= kmem_zalloc(nspares
* sizeof (void *),
1638 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1639 VERIFY(spa_config_parse(spa
, &vd
, spares
[i
], NULL
, 0,
1640 VDEV_ALLOC_SPARE
) == 0);
1643 spa
->spa_spares
.sav_vdevs
[i
] = vd
;
1645 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1646 B_FALSE
)) != NULL
) {
1647 if (!tvd
->vdev_isspare
)
1651 * We only mark the spare active if we were successfully
1652 * able to load the vdev. Otherwise, importing a pool
1653 * with a bad active spare would result in strange
1654 * behavior, because multiple pool would think the spare
1655 * is actively in use.
1657 * There is a vulnerability here to an equally bizarre
1658 * circumstance, where a dead active spare is later
1659 * brought back to life (onlined or otherwise). Given
1660 * the rarity of this scenario, and the extra complexity
1661 * it adds, we ignore the possibility.
1663 if (!vdev_is_dead(tvd
))
1664 spa_spare_activate(tvd
);
1668 vd
->vdev_aux
= &spa
->spa_spares
;
1670 if (vdev_open(vd
) != 0)
1673 if (vdev_validate_aux(vd
) == 0)
1678 * Recompute the stashed list of spares, with status information
1681 VERIFY(nvlist_remove(spa
->spa_spares
.sav_config
, ZPOOL_CONFIG_SPARES
,
1682 DATA_TYPE_NVLIST_ARRAY
) == 0);
1684 spares
= kmem_alloc(spa
->spa_spares
.sav_count
* sizeof (void *),
1686 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1687 spares
[i
] = vdev_config_generate(spa
,
1688 spa
->spa_spares
.sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_SPARE
);
1689 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
1690 ZPOOL_CONFIG_SPARES
, spares
, spa
->spa_spares
.sav_count
) == 0);
1691 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1692 nvlist_free(spares
[i
]);
1693 kmem_free(spares
, spa
->spa_spares
.sav_count
* sizeof (void *));
1697 * Load (or re-load) the current list of vdevs describing the active l2cache for
1698 * this pool. When this is called, we have some form of basic information in
1699 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1700 * then re-generate a more complete list including status information.
1701 * Devices which are already active have their details maintained, and are
1705 spa_load_l2cache(spa_t
*spa
)
1707 nvlist_t
**l2cache
= NULL
;
1709 int i
, j
, oldnvdevs
;
1711 vdev_t
*vd
, **oldvdevs
, **newvdevs
;
1712 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
1716 * zdb opens both the current state of the pool and the
1717 * checkpointed state (if present), with a different spa_t.
1719 * As L2 caches are part of the ARC which is shared among open
1720 * pools, we skip loading them when we load the checkpointed
1721 * state of the pool.
1723 if (!spa_writeable(spa
))
1727 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1729 oldvdevs
= sav
->sav_vdevs
;
1730 oldnvdevs
= sav
->sav_count
;
1731 sav
->sav_vdevs
= NULL
;
1734 if (sav
->sav_config
== NULL
) {
1740 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
,
1741 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
1742 newvdevs
= kmem_alloc(nl2cache
* sizeof (void *), KM_SLEEP
);
1745 * Process new nvlist of vdevs.
1747 for (i
= 0; i
< nl2cache
; i
++) {
1748 VERIFY(nvlist_lookup_uint64(l2cache
[i
], ZPOOL_CONFIG_GUID
,
1752 for (j
= 0; j
< oldnvdevs
; j
++) {
1754 if (vd
!= NULL
&& guid
== vd
->vdev_guid
) {
1756 * Retain previous vdev for add/remove ops.
1764 if (newvdevs
[i
] == NULL
) {
1768 VERIFY(spa_config_parse(spa
, &vd
, l2cache
[i
], NULL
, 0,
1769 VDEV_ALLOC_L2CACHE
) == 0);
1774 * Commit this vdev as an l2cache device,
1775 * even if it fails to open.
1777 spa_l2cache_add(vd
);
1782 spa_l2cache_activate(vd
);
1784 if (vdev_open(vd
) != 0)
1787 (void) vdev_validate_aux(vd
);
1789 if (!vdev_is_dead(vd
))
1790 l2arc_add_vdev(spa
, vd
);
1794 sav
->sav_vdevs
= newvdevs
;
1795 sav
->sav_count
= (int)nl2cache
;
1798 * Recompute the stashed list of l2cache devices, with status
1799 * information this time.
1801 VERIFY(nvlist_remove(sav
->sav_config
, ZPOOL_CONFIG_L2CACHE
,
1802 DATA_TYPE_NVLIST_ARRAY
) == 0);
1804 if (sav
->sav_count
> 0)
1805 l2cache
= kmem_alloc(sav
->sav_count
* sizeof (void *),
1807 for (i
= 0; i
< sav
->sav_count
; i
++)
1808 l2cache
[i
] = vdev_config_generate(spa
,
1809 sav
->sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_L2CACHE
);
1810 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
,
1811 ZPOOL_CONFIG_L2CACHE
, l2cache
, sav
->sav_count
) == 0);
1815 * Purge vdevs that were dropped
1817 for (i
= 0; i
< oldnvdevs
; i
++) {
1822 ASSERT(vd
->vdev_isl2cache
);
1824 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
1825 pool
!= 0ULL && l2arc_vdev_present(vd
))
1826 l2arc_remove_vdev(vd
);
1827 vdev_clear_stats(vd
);
1833 kmem_free(oldvdevs
, oldnvdevs
* sizeof (void *));
1835 for (i
= 0; i
< sav
->sav_count
; i
++)
1836 nvlist_free(l2cache
[i
]);
1838 kmem_free(l2cache
, sav
->sav_count
* sizeof (void *));
1842 load_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
**value
)
1845 char *packed
= NULL
;
1850 error
= dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
);
1854 nvsize
= *(uint64_t *)db
->db_data
;
1855 dmu_buf_rele(db
, FTAG
);
1857 packed
= vmem_alloc(nvsize
, KM_SLEEP
);
1858 error
= dmu_read(spa
->spa_meta_objset
, obj
, 0, nvsize
, packed
,
1861 error
= nvlist_unpack(packed
, nvsize
, value
, 0);
1862 vmem_free(packed
, nvsize
);
1868 * Concrete top-level vdevs that are not missing and are not logs. At every
1869 * spa_sync we write new uberblocks to at least SPA_SYNC_MIN_VDEVS core tvds.
1872 spa_healthy_core_tvds(spa_t
*spa
)
1874 vdev_t
*rvd
= spa
->spa_root_vdev
;
1877 for (uint64_t i
= 0; i
< rvd
->vdev_children
; i
++) {
1878 vdev_t
*vd
= rvd
->vdev_child
[i
];
1881 if (vdev_is_concrete(vd
) && !vdev_is_dead(vd
))
1889 * Checks to see if the given vdev could not be opened, in which case we post a
1890 * sysevent to notify the autoreplace code that the device has been removed.
1893 spa_check_removed(vdev_t
*vd
)
1895 for (uint64_t c
= 0; c
< vd
->vdev_children
; c
++)
1896 spa_check_removed(vd
->vdev_child
[c
]);
1898 if (vd
->vdev_ops
->vdev_op_leaf
&& vdev_is_dead(vd
) &&
1899 vdev_is_concrete(vd
)) {
1900 zfs_post_autoreplace(vd
->vdev_spa
, vd
);
1901 spa_event_notify(vd
->vdev_spa
, vd
, NULL
, ESC_ZFS_VDEV_CHECK
);
1906 spa_check_for_missing_logs(spa_t
*spa
)
1908 vdev_t
*rvd
= spa
->spa_root_vdev
;
1911 * If we're doing a normal import, then build up any additional
1912 * diagnostic information about missing log devices.
1913 * We'll pass this up to the user for further processing.
1915 if (!(spa
->spa_import_flags
& ZFS_IMPORT_MISSING_LOG
)) {
1916 nvlist_t
**child
, *nv
;
1919 child
= kmem_alloc(rvd
->vdev_children
* sizeof (nvlist_t
*),
1921 VERIFY(nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
1923 for (uint64_t c
= 0; c
< rvd
->vdev_children
; c
++) {
1924 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1927 * We consider a device as missing only if it failed
1928 * to open (i.e. offline or faulted is not considered
1931 if (tvd
->vdev_islog
&&
1932 tvd
->vdev_state
== VDEV_STATE_CANT_OPEN
) {
1933 child
[idx
++] = vdev_config_generate(spa
, tvd
,
1934 B_FALSE
, VDEV_CONFIG_MISSING
);
1939 fnvlist_add_nvlist_array(nv
,
1940 ZPOOL_CONFIG_CHILDREN
, child
, idx
);
1941 fnvlist_add_nvlist(spa
->spa_load_info
,
1942 ZPOOL_CONFIG_MISSING_DEVICES
, nv
);
1944 for (uint64_t i
= 0; i
< idx
; i
++)
1945 nvlist_free(child
[i
]);
1948 kmem_free(child
, rvd
->vdev_children
* sizeof (char **));
1951 spa_load_failed(spa
, "some log devices are missing");
1952 vdev_dbgmsg_print_tree(rvd
, 2);
1953 return (SET_ERROR(ENXIO
));
1956 for (uint64_t c
= 0; c
< rvd
->vdev_children
; c
++) {
1957 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1959 if (tvd
->vdev_islog
&&
1960 tvd
->vdev_state
== VDEV_STATE_CANT_OPEN
) {
1961 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
1962 spa_load_note(spa
, "some log devices are "
1963 "missing, ZIL is dropped.");
1964 vdev_dbgmsg_print_tree(rvd
, 2);
1974 * Check for missing log devices
1977 spa_check_logs(spa_t
*spa
)
1979 boolean_t rv
= B_FALSE
;
1980 dsl_pool_t
*dp
= spa_get_dsl(spa
);
1982 switch (spa
->spa_log_state
) {
1985 case SPA_LOG_MISSING
:
1986 /* need to recheck in case slog has been restored */
1987 case SPA_LOG_UNKNOWN
:
1988 rv
= (dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
1989 zil_check_log_chain
, NULL
, DS_FIND_CHILDREN
) != 0);
1991 spa_set_log_state(spa
, SPA_LOG_MISSING
);
1998 spa_passivate_log(spa_t
*spa
)
2000 vdev_t
*rvd
= spa
->spa_root_vdev
;
2001 boolean_t slog_found
= B_FALSE
;
2003 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
2005 if (!spa_has_slogs(spa
))
2008 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
2009 vdev_t
*tvd
= rvd
->vdev_child
[c
];
2010 metaslab_group_t
*mg
= tvd
->vdev_mg
;
2012 if (tvd
->vdev_islog
) {
2013 metaslab_group_passivate(mg
);
2014 slog_found
= B_TRUE
;
2018 return (slog_found
);
2022 spa_activate_log(spa_t
*spa
)
2024 vdev_t
*rvd
= spa
->spa_root_vdev
;
2026 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
2028 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
2029 vdev_t
*tvd
= rvd
->vdev_child
[c
];
2030 metaslab_group_t
*mg
= tvd
->vdev_mg
;
2032 if (tvd
->vdev_islog
)
2033 metaslab_group_activate(mg
);
2038 spa_reset_logs(spa_t
*spa
)
2042 error
= dmu_objset_find(spa_name(spa
), zil_reset
,
2043 NULL
, DS_FIND_CHILDREN
);
2046 * We successfully offlined the log device, sync out the
2047 * current txg so that the "stubby" block can be removed
2050 txg_wait_synced(spa
->spa_dsl_pool
, 0);
2056 spa_aux_check_removed(spa_aux_vdev_t
*sav
)
2058 for (int i
= 0; i
< sav
->sav_count
; i
++)
2059 spa_check_removed(sav
->sav_vdevs
[i
]);
2063 spa_claim_notify(zio_t
*zio
)
2065 spa_t
*spa
= zio
->io_spa
;
2070 mutex_enter(&spa
->spa_props_lock
); /* any mutex will do */
2071 if (spa
->spa_claim_max_txg
< zio
->io_bp
->blk_birth
)
2072 spa
->spa_claim_max_txg
= zio
->io_bp
->blk_birth
;
2073 mutex_exit(&spa
->spa_props_lock
);
2076 typedef struct spa_load_error
{
2077 uint64_t sle_meta_count
;
2078 uint64_t sle_data_count
;
2082 spa_load_verify_done(zio_t
*zio
)
2084 blkptr_t
*bp
= zio
->io_bp
;
2085 spa_load_error_t
*sle
= zio
->io_private
;
2086 dmu_object_type_t type
= BP_GET_TYPE(bp
);
2087 int error
= zio
->io_error
;
2088 spa_t
*spa
= zio
->io_spa
;
2090 abd_free(zio
->io_abd
);
2092 if ((BP_GET_LEVEL(bp
) != 0 || DMU_OT_IS_METADATA(type
)) &&
2093 type
!= DMU_OT_INTENT_LOG
)
2094 atomic_inc_64(&sle
->sle_meta_count
);
2096 atomic_inc_64(&sle
->sle_data_count
);
2099 mutex_enter(&spa
->spa_scrub_lock
);
2100 spa
->spa_load_verify_ios
--;
2101 cv_broadcast(&spa
->spa_scrub_io_cv
);
2102 mutex_exit(&spa
->spa_scrub_lock
);
2106 * Maximum number of concurrent scrub i/os to create while verifying
2107 * a pool while importing it.
2109 int spa_load_verify_maxinflight
= 10000;
2110 int spa_load_verify_metadata
= B_TRUE
;
2111 int spa_load_verify_data
= B_TRUE
;
2115 spa_load_verify_cb(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
2116 const zbookmark_phys_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
2118 if (bp
== NULL
|| BP_IS_HOLE(bp
) || BP_IS_EMBEDDED(bp
))
2121 * Note: normally this routine will not be called if
2122 * spa_load_verify_metadata is not set. However, it may be useful
2123 * to manually set the flag after the traversal has begun.
2125 if (!spa_load_verify_metadata
)
2127 if (!BP_IS_METADATA(bp
) && !spa_load_verify_data
)
2131 size_t size
= BP_GET_PSIZE(bp
);
2133 mutex_enter(&spa
->spa_scrub_lock
);
2134 while (spa
->spa_load_verify_ios
>= spa_load_verify_maxinflight
)
2135 cv_wait(&spa
->spa_scrub_io_cv
, &spa
->spa_scrub_lock
);
2136 spa
->spa_load_verify_ios
++;
2137 mutex_exit(&spa
->spa_scrub_lock
);
2139 zio_nowait(zio_read(rio
, spa
, bp
, abd_alloc_for_io(size
, B_FALSE
), size
,
2140 spa_load_verify_done
, rio
->io_private
, ZIO_PRIORITY_SCRUB
,
2141 ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_CANFAIL
|
2142 ZIO_FLAG_SCRUB
| ZIO_FLAG_RAW
, zb
));
2148 verify_dataset_name_len(dsl_pool_t
*dp
, dsl_dataset_t
*ds
, void *arg
)
2150 if (dsl_dataset_namelen(ds
) >= ZFS_MAX_DATASET_NAME_LEN
)
2151 return (SET_ERROR(ENAMETOOLONG
));
2157 spa_load_verify(spa_t
*spa
)
2160 spa_load_error_t sle
= { 0 };
2161 zpool_load_policy_t policy
;
2162 boolean_t verify_ok
= B_FALSE
;
2165 zpool_get_load_policy(spa
->spa_config
, &policy
);
2167 if (policy
.zlp_rewind
& ZPOOL_NEVER_REWIND
)
2170 dsl_pool_config_enter(spa
->spa_dsl_pool
, FTAG
);
2171 error
= dmu_objset_find_dp(spa
->spa_dsl_pool
,
2172 spa
->spa_dsl_pool
->dp_root_dir_obj
, verify_dataset_name_len
, NULL
,
2174 dsl_pool_config_exit(spa
->spa_dsl_pool
, FTAG
);
2178 rio
= zio_root(spa
, NULL
, &sle
,
2179 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
);
2181 if (spa_load_verify_metadata
) {
2182 if (spa
->spa_extreme_rewind
) {
2183 spa_load_note(spa
, "performing a complete scan of the "
2184 "pool since extreme rewind is on. This may take "
2185 "a very long time.\n (spa_load_verify_data=%u, "
2186 "spa_load_verify_metadata=%u)",
2187 spa_load_verify_data
, spa_load_verify_metadata
);
2189 error
= traverse_pool(spa
, spa
->spa_verify_min_txg
,
2190 TRAVERSE_PRE
| TRAVERSE_PREFETCH_METADATA
|
2191 TRAVERSE_NO_DECRYPT
, spa_load_verify_cb
, rio
);
2194 (void) zio_wait(rio
);
2196 spa
->spa_load_meta_errors
= sle
.sle_meta_count
;
2197 spa
->spa_load_data_errors
= sle
.sle_data_count
;
2199 if (sle
.sle_meta_count
!= 0 || sle
.sle_data_count
!= 0) {
2200 spa_load_note(spa
, "spa_load_verify found %llu metadata errors "
2201 "and %llu data errors", (u_longlong_t
)sle
.sle_meta_count
,
2202 (u_longlong_t
)sle
.sle_data_count
);
2205 if (spa_load_verify_dryrun
||
2206 (!error
&& sle
.sle_meta_count
<= policy
.zlp_maxmeta
&&
2207 sle
.sle_data_count
<= policy
.zlp_maxdata
)) {
2211 spa
->spa_load_txg
= spa
->spa_uberblock
.ub_txg
;
2212 spa
->spa_load_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
2214 loss
= spa
->spa_last_ubsync_txg_ts
- spa
->spa_load_txg_ts
;
2215 VERIFY(nvlist_add_uint64(spa
->spa_load_info
,
2216 ZPOOL_CONFIG_LOAD_TIME
, spa
->spa_load_txg_ts
) == 0);
2217 VERIFY(nvlist_add_int64(spa
->spa_load_info
,
2218 ZPOOL_CONFIG_REWIND_TIME
, loss
) == 0);
2219 VERIFY(nvlist_add_uint64(spa
->spa_load_info
,
2220 ZPOOL_CONFIG_LOAD_DATA_ERRORS
, sle
.sle_data_count
) == 0);
2222 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
;
2225 if (spa_load_verify_dryrun
)
2229 if (error
!= ENXIO
&& error
!= EIO
)
2230 error
= SET_ERROR(EIO
);
2234 return (verify_ok
? 0 : EIO
);
2238 * Find a value in the pool props object.
2241 spa_prop_find(spa_t
*spa
, zpool_prop_t prop
, uint64_t *val
)
2243 (void) zap_lookup(spa
->spa_meta_objset
, spa
->spa_pool_props_object
,
2244 zpool_prop_to_name(prop
), sizeof (uint64_t), 1, val
);
2248 * Find a value in the pool directory object.
2251 spa_dir_prop(spa_t
*spa
, const char *name
, uint64_t *val
, boolean_t log_enoent
)
2253 int error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
2254 name
, sizeof (uint64_t), 1, val
);
2256 if (error
!= 0 && (error
!= ENOENT
|| log_enoent
)) {
2257 spa_load_failed(spa
, "couldn't get '%s' value in MOS directory "
2258 "[error=%d]", name
, error
);
2265 spa_vdev_err(vdev_t
*vdev
, vdev_aux_t aux
, int err
)
2267 vdev_set_state(vdev
, B_TRUE
, VDEV_STATE_CANT_OPEN
, aux
);
2268 return (SET_ERROR(err
));
2272 spa_spawn_aux_threads(spa_t
*spa
)
2274 ASSERT(spa_writeable(spa
));
2276 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
2278 spa_start_indirect_condensing_thread(spa
);
2280 ASSERT3P(spa
->spa_checkpoint_discard_zthr
, ==, NULL
);
2281 spa
->spa_checkpoint_discard_zthr
=
2282 zthr_create(spa_checkpoint_discard_thread_check
,
2283 spa_checkpoint_discard_thread
, spa
);
2287 * Fix up config after a partly-completed split. This is done with the
2288 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
2289 * pool have that entry in their config, but only the splitting one contains
2290 * a list of all the guids of the vdevs that are being split off.
2292 * This function determines what to do with that list: either rejoin
2293 * all the disks to the pool, or complete the splitting process. To attempt
2294 * the rejoin, each disk that is offlined is marked online again, and
2295 * we do a reopen() call. If the vdev label for every disk that was
2296 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
2297 * then we call vdev_split() on each disk, and complete the split.
2299 * Otherwise we leave the config alone, with all the vdevs in place in
2300 * the original pool.
2303 spa_try_repair(spa_t
*spa
, nvlist_t
*config
)
2310 boolean_t attempt_reopen
;
2312 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
, &nvl
) != 0)
2315 /* check that the config is complete */
2316 if (nvlist_lookup_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
2317 &glist
, &gcount
) != 0)
2320 vd
= kmem_zalloc(gcount
* sizeof (vdev_t
*), KM_SLEEP
);
2322 /* attempt to online all the vdevs & validate */
2323 attempt_reopen
= B_TRUE
;
2324 for (i
= 0; i
< gcount
; i
++) {
2325 if (glist
[i
] == 0) /* vdev is hole */
2328 vd
[i
] = spa_lookup_by_guid(spa
, glist
[i
], B_FALSE
);
2329 if (vd
[i
] == NULL
) {
2331 * Don't bother attempting to reopen the disks;
2332 * just do the split.
2334 attempt_reopen
= B_FALSE
;
2336 /* attempt to re-online it */
2337 vd
[i
]->vdev_offline
= B_FALSE
;
2341 if (attempt_reopen
) {
2342 vdev_reopen(spa
->spa_root_vdev
);
2344 /* check each device to see what state it's in */
2345 for (extracted
= 0, i
= 0; i
< gcount
; i
++) {
2346 if (vd
[i
] != NULL
&&
2347 vd
[i
]->vdev_stat
.vs_aux
!= VDEV_AUX_SPLIT_POOL
)
2354 * If every disk has been moved to the new pool, or if we never
2355 * even attempted to look at them, then we split them off for
2358 if (!attempt_reopen
|| gcount
== extracted
) {
2359 for (i
= 0; i
< gcount
; i
++)
2362 vdev_reopen(spa
->spa_root_vdev
);
2365 kmem_free(vd
, gcount
* sizeof (vdev_t
*));
2369 spa_load(spa_t
*spa
, spa_load_state_t state
, spa_import_type_t type
)
2371 char *ereport
= FM_EREPORT_ZFS_POOL
;
2374 spa
->spa_load_state
= state
;
2376 gethrestime(&spa
->spa_loaded_ts
);
2377 error
= spa_load_impl(spa
, type
, &ereport
);
2380 * Don't count references from objsets that are already closed
2381 * and are making their way through the eviction process.
2383 spa_evicting_os_wait(spa
);
2384 spa
->spa_minref
= zfs_refcount_count(&spa
->spa_refcount
);
2386 if (error
!= EEXIST
) {
2387 spa
->spa_loaded_ts
.tv_sec
= 0;
2388 spa
->spa_loaded_ts
.tv_nsec
= 0;
2390 if (error
!= EBADF
) {
2391 zfs_ereport_post(ereport
, spa
, NULL
, NULL
, NULL
, 0, 0);
2394 spa
->spa_load_state
= error
? SPA_LOAD_ERROR
: SPA_LOAD_NONE
;
2402 * Count the number of per-vdev ZAPs associated with all of the vdevs in the
2403 * vdev tree rooted in the given vd, and ensure that each ZAP is present in the
2404 * spa's per-vdev ZAP list.
2407 vdev_count_verify_zaps(vdev_t
*vd
)
2409 spa_t
*spa
= vd
->vdev_spa
;
2412 if (vd
->vdev_top_zap
!= 0) {
2414 ASSERT0(zap_lookup_int(spa
->spa_meta_objset
,
2415 spa
->spa_all_vdev_zaps
, vd
->vdev_top_zap
));
2417 if (vd
->vdev_leaf_zap
!= 0) {
2419 ASSERT0(zap_lookup_int(spa
->spa_meta_objset
,
2420 spa
->spa_all_vdev_zaps
, vd
->vdev_leaf_zap
));
2423 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
2424 total
+= vdev_count_verify_zaps(vd
->vdev_child
[i
]);
2432 * Determine whether the activity check is required.
2435 spa_activity_check_required(spa_t
*spa
, uberblock_t
*ub
, nvlist_t
*label
,
2439 uint64_t hostid
= 0;
2440 uint64_t tryconfig_txg
= 0;
2441 uint64_t tryconfig_timestamp
= 0;
2444 if (nvlist_exists(config
, ZPOOL_CONFIG_LOAD_INFO
)) {
2445 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
2446 (void) nvlist_lookup_uint64(nvinfo
, ZPOOL_CONFIG_MMP_TXG
,
2448 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_TIMESTAMP
,
2449 &tryconfig_timestamp
);
2452 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
, &state
);
2455 * Disable the MMP activity check - This is used by zdb which
2456 * is intended to be used on potentially active pools.
2458 if (spa
->spa_import_flags
& ZFS_IMPORT_SKIP_MMP
)
2462 * Skip the activity check when the MMP feature is disabled.
2464 if (ub
->ub_mmp_magic
== MMP_MAGIC
&& ub
->ub_mmp_delay
== 0)
2467 * If the tryconfig_* values are nonzero, they are the results of an
2468 * earlier tryimport. If they match the uberblock we just found, then
2469 * the pool has not changed and we return false so we do not test a
2472 if (tryconfig_txg
&& tryconfig_txg
== ub
->ub_txg
&&
2473 tryconfig_timestamp
&& tryconfig_timestamp
== ub
->ub_timestamp
)
2477 * Allow the activity check to be skipped when importing the pool
2478 * on the same host which last imported it. Since the hostid from
2479 * configuration may be stale use the one read from the label.
2481 if (nvlist_exists(label
, ZPOOL_CONFIG_HOSTID
))
2482 hostid
= fnvlist_lookup_uint64(label
, ZPOOL_CONFIG_HOSTID
);
2484 if (hostid
== spa_get_hostid())
2488 * Skip the activity test when the pool was cleanly exported.
2490 if (state
!= POOL_STATE_ACTIVE
)
2497 * Perform the import activity check. If the user canceled the import or
2498 * we detected activity then fail.
2501 spa_activity_check(spa_t
*spa
, uberblock_t
*ub
, nvlist_t
*config
)
2503 uint64_t import_intervals
= MAX(zfs_multihost_import_intervals
, 1);
2504 uint64_t txg
= ub
->ub_txg
;
2505 uint64_t timestamp
= ub
->ub_timestamp
;
2506 uint64_t import_delay
= NANOSEC
;
2507 hrtime_t import_expire
;
2508 nvlist_t
*mmp_label
= NULL
;
2509 vdev_t
*rvd
= spa
->spa_root_vdev
;
2514 cv_init(&cv
, NULL
, CV_DEFAULT
, NULL
);
2515 mutex_init(&mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
2519 * If ZPOOL_CONFIG_MMP_TXG is present an activity check was performed
2520 * during the earlier tryimport. If the txg recorded there is 0 then
2521 * the pool is known to be active on another host.
2523 * Otherwise, the pool might be in use on another node. Check for
2524 * changes in the uberblocks on disk if necessary.
2526 if (nvlist_exists(config
, ZPOOL_CONFIG_LOAD_INFO
)) {
2527 nvlist_t
*nvinfo
= fnvlist_lookup_nvlist(config
,
2528 ZPOOL_CONFIG_LOAD_INFO
);
2530 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_TXG
) &&
2531 fnvlist_lookup_uint64(nvinfo
, ZPOOL_CONFIG_MMP_TXG
) == 0) {
2532 vdev_uberblock_load(rvd
, ub
, &mmp_label
);
2533 error
= SET_ERROR(EREMOTEIO
);
2539 * Preferentially use the zfs_multihost_interval from the node which
2540 * last imported the pool. This value is stored in an MMP uberblock as.
2542 * ub_mmp_delay * vdev_count_leaves() == zfs_multihost_interval
2544 if (ub
->ub_mmp_magic
== MMP_MAGIC
&& ub
->ub_mmp_delay
)
2545 import_delay
= MAX(import_delay
, import_intervals
*
2546 ub
->ub_mmp_delay
* MAX(vdev_count_leaves(spa
), 1));
2548 /* Apply a floor using the local default values. */
2549 import_delay
= MAX(import_delay
, import_intervals
*
2550 MSEC2NSEC(MAX(zfs_multihost_interval
, MMP_MIN_INTERVAL
)));
2552 zfs_dbgmsg("import_delay=%llu ub_mmp_delay=%llu import_intervals=%u "
2553 "leaves=%u", import_delay
, ub
->ub_mmp_delay
, import_intervals
,
2554 vdev_count_leaves(spa
));
2556 /* Add a small random factor in case of simultaneous imports (0-25%) */
2557 import_expire
= gethrtime() + import_delay
+
2558 (import_delay
* spa_get_random(250) / 1000);
2560 while (gethrtime() < import_expire
) {
2561 vdev_uberblock_load(rvd
, ub
, &mmp_label
);
2563 if (txg
!= ub
->ub_txg
|| timestamp
!= ub
->ub_timestamp
) {
2564 error
= SET_ERROR(EREMOTEIO
);
2569 nvlist_free(mmp_label
);
2573 error
= cv_timedwait_sig(&cv
, &mtx
, ddi_get_lbolt() + hz
);
2575 error
= SET_ERROR(EINTR
);
2583 mutex_destroy(&mtx
);
2587 * If the pool is determined to be active store the status in the
2588 * spa->spa_load_info nvlist. If the remote hostname or hostid are
2589 * available from configuration read from disk store them as well.
2590 * This allows 'zpool import' to generate a more useful message.
2592 * ZPOOL_CONFIG_MMP_STATE - observed pool status (mandatory)
2593 * ZPOOL_CONFIG_MMP_HOSTNAME - hostname from the active pool
2594 * ZPOOL_CONFIG_MMP_HOSTID - hostid from the active pool
2596 if (error
== EREMOTEIO
) {
2597 char *hostname
= "<unknown>";
2598 uint64_t hostid
= 0;
2601 if (nvlist_exists(mmp_label
, ZPOOL_CONFIG_HOSTNAME
)) {
2602 hostname
= fnvlist_lookup_string(mmp_label
,
2603 ZPOOL_CONFIG_HOSTNAME
);
2604 fnvlist_add_string(spa
->spa_load_info
,
2605 ZPOOL_CONFIG_MMP_HOSTNAME
, hostname
);
2608 if (nvlist_exists(mmp_label
, ZPOOL_CONFIG_HOSTID
)) {
2609 hostid
= fnvlist_lookup_uint64(mmp_label
,
2610 ZPOOL_CONFIG_HOSTID
);
2611 fnvlist_add_uint64(spa
->spa_load_info
,
2612 ZPOOL_CONFIG_MMP_HOSTID
, hostid
);
2616 fnvlist_add_uint64(spa
->spa_load_info
,
2617 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_ACTIVE
);
2618 fnvlist_add_uint64(spa
->spa_load_info
,
2619 ZPOOL_CONFIG_MMP_TXG
, 0);
2621 error
= spa_vdev_err(rvd
, VDEV_AUX_ACTIVE
, EREMOTEIO
);
2625 nvlist_free(mmp_label
);
2631 spa_verify_host(spa_t
*spa
, nvlist_t
*mos_config
)
2635 uint64_t myhostid
= 0;
2637 if (!spa_is_root(spa
) && nvlist_lookup_uint64(mos_config
,
2638 ZPOOL_CONFIG_HOSTID
, &hostid
) == 0) {
2639 hostname
= fnvlist_lookup_string(mos_config
,
2640 ZPOOL_CONFIG_HOSTNAME
);
2642 myhostid
= zone_get_hostid(NULL
);
2644 if (hostid
!= 0 && myhostid
!= 0 && hostid
!= myhostid
) {
2645 cmn_err(CE_WARN
, "pool '%s' could not be "
2646 "loaded as it was last accessed by "
2647 "another system (host: %s hostid: 0x%llx). "
2648 "See: http://illumos.org/msg/ZFS-8000-EY",
2649 spa_name(spa
), hostname
, (u_longlong_t
)hostid
);
2650 spa_load_failed(spa
, "hostid verification failed: pool "
2651 "last accessed by host: %s (hostid: 0x%llx)",
2652 hostname
, (u_longlong_t
)hostid
);
2653 return (SET_ERROR(EBADF
));
2661 spa_ld_parse_config(spa_t
*spa
, spa_import_type_t type
)
2664 nvlist_t
*nvtree
, *nvl
, *config
= spa
->spa_config
;
2671 * Versioning wasn't explicitly added to the label until later, so if
2672 * it's not present treat it as the initial version.
2674 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
2675 &spa
->spa_ubsync
.ub_version
) != 0)
2676 spa
->spa_ubsync
.ub_version
= SPA_VERSION_INITIAL
;
2678 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
, &pool_guid
)) {
2679 spa_load_failed(spa
, "invalid config provided: '%s' missing",
2680 ZPOOL_CONFIG_POOL_GUID
);
2681 return (SET_ERROR(EINVAL
));
2685 * If we are doing an import, ensure that the pool is not already
2686 * imported by checking if its pool guid already exists in the
2689 * The only case that we allow an already imported pool to be
2690 * imported again, is when the pool is checkpointed and we want to
2691 * look at its checkpointed state from userland tools like zdb.
2694 if ((spa
->spa_load_state
== SPA_LOAD_IMPORT
||
2695 spa
->spa_load_state
== SPA_LOAD_TRYIMPORT
) &&
2696 spa_guid_exists(pool_guid
, 0)) {
2698 if ((spa
->spa_load_state
== SPA_LOAD_IMPORT
||
2699 spa
->spa_load_state
== SPA_LOAD_TRYIMPORT
) &&
2700 spa_guid_exists(pool_guid
, 0) &&
2701 !spa_importing_readonly_checkpoint(spa
)) {
2703 spa_load_failed(spa
, "a pool with guid %llu is already open",
2704 (u_longlong_t
)pool_guid
);
2705 return (SET_ERROR(EEXIST
));
2708 spa
->spa_config_guid
= pool_guid
;
2710 nvlist_free(spa
->spa_load_info
);
2711 spa
->spa_load_info
= fnvlist_alloc();
2713 ASSERT(spa
->spa_comment
== NULL
);
2714 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
2715 spa
->spa_comment
= spa_strdup(comment
);
2717 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
2718 &spa
->spa_config_txg
);
2720 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
, &nvl
) == 0)
2721 spa
->spa_config_splitting
= fnvlist_dup(nvl
);
2723 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvtree
)) {
2724 spa_load_failed(spa
, "invalid config provided: '%s' missing",
2725 ZPOOL_CONFIG_VDEV_TREE
);
2726 return (SET_ERROR(EINVAL
));
2730 * Create "The Godfather" zio to hold all async IOs
2732 spa
->spa_async_zio_root
= kmem_alloc(max_ncpus
* sizeof (void *),
2734 for (int i
= 0; i
< max_ncpus
; i
++) {
2735 spa
->spa_async_zio_root
[i
] = zio_root(spa
, NULL
, NULL
,
2736 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
2737 ZIO_FLAG_GODFATHER
);
2741 * Parse the configuration into a vdev tree. We explicitly set the
2742 * value that will be returned by spa_version() since parsing the
2743 * configuration requires knowing the version number.
2745 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2746 parse
= (type
== SPA_IMPORT_EXISTING
?
2747 VDEV_ALLOC_LOAD
: VDEV_ALLOC_SPLIT
);
2748 error
= spa_config_parse(spa
, &rvd
, nvtree
, NULL
, 0, parse
);
2749 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2752 spa_load_failed(spa
, "unable to parse config [error=%d]",
2757 ASSERT(spa
->spa_root_vdev
== rvd
);
2758 ASSERT3U(spa
->spa_min_ashift
, >=, SPA_MINBLOCKSHIFT
);
2759 ASSERT3U(spa
->spa_max_ashift
, <=, SPA_MAXBLOCKSHIFT
);
2761 if (type
!= SPA_IMPORT_ASSEMBLE
) {
2762 ASSERT(spa_guid(spa
) == pool_guid
);
2769 * Recursively open all vdevs in the vdev tree. This function is called twice:
2770 * first with the untrusted config, then with the trusted config.
2773 spa_ld_open_vdevs(spa_t
*spa
)
2778 * spa_missing_tvds_allowed defines how many top-level vdevs can be
2779 * missing/unopenable for the root vdev to be still considered openable.
2781 if (spa
->spa_trust_config
) {
2782 spa
->spa_missing_tvds_allowed
= zfs_max_missing_tvds
;
2783 } else if (spa
->spa_config_source
== SPA_CONFIG_SRC_CACHEFILE
) {
2784 spa
->spa_missing_tvds_allowed
= zfs_max_missing_tvds_cachefile
;
2785 } else if (spa
->spa_config_source
== SPA_CONFIG_SRC_SCAN
) {
2786 spa
->spa_missing_tvds_allowed
= zfs_max_missing_tvds_scan
;
2788 spa
->spa_missing_tvds_allowed
= 0;
2791 spa
->spa_missing_tvds_allowed
=
2792 MAX(zfs_max_missing_tvds
, spa
->spa_missing_tvds_allowed
);
2794 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2795 error
= vdev_open(spa
->spa_root_vdev
);
2796 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2798 if (spa
->spa_missing_tvds
!= 0) {
2799 spa_load_note(spa
, "vdev tree has %lld missing top-level "
2800 "vdevs.", (u_longlong_t
)spa
->spa_missing_tvds
);
2801 if (spa
->spa_trust_config
&& (spa
->spa_mode
& FWRITE
)) {
2803 * Although theoretically we could allow users to open
2804 * incomplete pools in RW mode, we'd need to add a lot
2805 * of extra logic (e.g. adjust pool space to account
2806 * for missing vdevs).
2807 * This limitation also prevents users from accidentally
2808 * opening the pool in RW mode during data recovery and
2809 * damaging it further.
2811 spa_load_note(spa
, "pools with missing top-level "
2812 "vdevs can only be opened in read-only mode.");
2813 error
= SET_ERROR(ENXIO
);
2815 spa_load_note(spa
, "current settings allow for maximum "
2816 "%lld missing top-level vdevs at this stage.",
2817 (u_longlong_t
)spa
->spa_missing_tvds_allowed
);
2821 spa_load_failed(spa
, "unable to open vdev tree [error=%d]",
2824 if (spa
->spa_missing_tvds
!= 0 || error
!= 0)
2825 vdev_dbgmsg_print_tree(spa
->spa_root_vdev
, 2);
2831 * We need to validate the vdev labels against the configuration that
2832 * we have in hand. This function is called twice: first with an untrusted
2833 * config, then with a trusted config. The validation is more strict when the
2834 * config is trusted.
2837 spa_ld_validate_vdevs(spa_t
*spa
)
2840 vdev_t
*rvd
= spa
->spa_root_vdev
;
2842 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2843 error
= vdev_validate(rvd
);
2844 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2847 spa_load_failed(spa
, "vdev_validate failed [error=%d]", error
);
2851 if (rvd
->vdev_state
<= VDEV_STATE_CANT_OPEN
) {
2852 spa_load_failed(spa
, "cannot open vdev tree after invalidating "
2854 vdev_dbgmsg_print_tree(rvd
, 2);
2855 return (SET_ERROR(ENXIO
));
2862 spa_ld_select_uberblock_done(spa_t
*spa
, uberblock_t
*ub
)
2864 spa
->spa_state
= POOL_STATE_ACTIVE
;
2865 spa
->spa_ubsync
= spa
->spa_uberblock
;
2866 spa
->spa_verify_min_txg
= spa
->spa_extreme_rewind
?
2867 TXG_INITIAL
- 1 : spa_last_synced_txg(spa
) - TXG_DEFER_SIZE
- 1;
2868 spa
->spa_first_txg
= spa
->spa_last_ubsync_txg
?
2869 spa
->spa_last_ubsync_txg
: spa_last_synced_txg(spa
) + 1;
2870 spa
->spa_claim_max_txg
= spa
->spa_first_txg
;
2871 spa
->spa_prev_software_version
= ub
->ub_software_version
;
2875 spa_ld_select_uberblock(spa_t
*spa
, spa_import_type_t type
)
2877 vdev_t
*rvd
= spa
->spa_root_vdev
;
2879 uberblock_t
*ub
= &spa
->spa_uberblock
;
2880 boolean_t activity_check
= B_FALSE
;
2883 * If we are opening the checkpointed state of the pool by
2884 * rewinding to it, at this point we will have written the
2885 * checkpointed uberblock to the vdev labels, so searching
2886 * the labels will find the right uberblock. However, if
2887 * we are opening the checkpointed state read-only, we have
2888 * not modified the labels. Therefore, we must ignore the
2889 * labels and continue using the spa_uberblock that was set
2890 * by spa_ld_checkpoint_rewind.
2892 * Note that it would be fine to ignore the labels when
2893 * rewinding (opening writeable) as well. However, if we
2894 * crash just after writing the labels, we will end up
2895 * searching the labels. Doing so in the common case means
2896 * that this code path gets exercised normally, rather than
2897 * just in the edge case.
2899 if (ub
->ub_checkpoint_txg
!= 0 &&
2900 spa_importing_readonly_checkpoint(spa
)) {
2901 spa_ld_select_uberblock_done(spa
, ub
);
2906 * Find the best uberblock.
2908 vdev_uberblock_load(rvd
, ub
, &label
);
2911 * If we weren't able to find a single valid uberblock, return failure.
2913 if (ub
->ub_txg
== 0) {
2915 spa_load_failed(spa
, "no valid uberblock found");
2916 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, ENXIO
));
2919 spa_load_note(spa
, "using uberblock with txg=%llu",
2920 (u_longlong_t
)ub
->ub_txg
);
2924 * For pools which have the multihost property on determine if the
2925 * pool is truly inactive and can be safely imported. Prevent
2926 * hosts which don't have a hostid set from importing the pool.
2928 activity_check
= spa_activity_check_required(spa
, ub
, label
,
2930 if (activity_check
) {
2931 if (ub
->ub_mmp_magic
== MMP_MAGIC
&& ub
->ub_mmp_delay
&&
2932 spa_get_hostid() == 0) {
2934 fnvlist_add_uint64(spa
->spa_load_info
,
2935 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_NO_HOSTID
);
2936 return (spa_vdev_err(rvd
, VDEV_AUX_ACTIVE
, EREMOTEIO
));
2939 int error
= spa_activity_check(spa
, ub
, spa
->spa_config
);
2945 fnvlist_add_uint64(spa
->spa_load_info
,
2946 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_INACTIVE
);
2947 fnvlist_add_uint64(spa
->spa_load_info
,
2948 ZPOOL_CONFIG_MMP_TXG
, ub
->ub_txg
);
2952 * If the pool has an unsupported version we can't open it.
2954 if (!SPA_VERSION_IS_SUPPORTED(ub
->ub_version
)) {
2956 spa_load_failed(spa
, "version %llu is not supported",
2957 (u_longlong_t
)ub
->ub_version
);
2958 return (spa_vdev_err(rvd
, VDEV_AUX_VERSION_NEWER
, ENOTSUP
));
2961 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
2965 * If we weren't able to find what's necessary for reading the
2966 * MOS in the label, return failure.
2968 if (label
== NULL
) {
2969 spa_load_failed(spa
, "label config unavailable");
2970 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
2974 if (nvlist_lookup_nvlist(label
, ZPOOL_CONFIG_FEATURES_FOR_READ
,
2977 spa_load_failed(spa
, "invalid label: '%s' missing",
2978 ZPOOL_CONFIG_FEATURES_FOR_READ
);
2979 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
2984 * Update our in-core representation with the definitive values
2987 nvlist_free(spa
->spa_label_features
);
2988 VERIFY(nvlist_dup(features
, &spa
->spa_label_features
, 0) == 0);
2994 * Look through entries in the label nvlist's features_for_read. If
2995 * there is a feature listed there which we don't understand then we
2996 * cannot open a pool.
2998 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
2999 nvlist_t
*unsup_feat
;
3001 VERIFY(nvlist_alloc(&unsup_feat
, NV_UNIQUE_NAME
, KM_SLEEP
) ==
3004 for (nvpair_t
*nvp
= nvlist_next_nvpair(spa
->spa_label_features
,
3006 nvp
= nvlist_next_nvpair(spa
->spa_label_features
, nvp
)) {
3007 if (!zfeature_is_supported(nvpair_name(nvp
))) {
3008 VERIFY(nvlist_add_string(unsup_feat
,
3009 nvpair_name(nvp
), "") == 0);
3013 if (!nvlist_empty(unsup_feat
)) {
3014 VERIFY(nvlist_add_nvlist(spa
->spa_load_info
,
3015 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
) == 0);
3016 nvlist_free(unsup_feat
);
3017 spa_load_failed(spa
, "some features are unsupported");
3018 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
3022 nvlist_free(unsup_feat
);
3025 if (type
!= SPA_IMPORT_ASSEMBLE
&& spa
->spa_config_splitting
) {
3026 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3027 spa_try_repair(spa
, spa
->spa_config
);
3028 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3029 nvlist_free(spa
->spa_config_splitting
);
3030 spa
->spa_config_splitting
= NULL
;
3034 * Initialize internal SPA structures.
3036 spa_ld_select_uberblock_done(spa
, ub
);
3042 spa_ld_open_rootbp(spa_t
*spa
)
3045 vdev_t
*rvd
= spa
->spa_root_vdev
;
3047 error
= dsl_pool_init(spa
, spa
->spa_first_txg
, &spa
->spa_dsl_pool
);
3049 spa_load_failed(spa
, "unable to open rootbp in dsl_pool_init "
3050 "[error=%d]", error
);
3051 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3053 spa
->spa_meta_objset
= spa
->spa_dsl_pool
->dp_meta_objset
;
3059 spa_ld_trusted_config(spa_t
*spa
, spa_import_type_t type
,
3060 boolean_t reloading
)
3062 vdev_t
*mrvd
, *rvd
= spa
->spa_root_vdev
;
3063 nvlist_t
*nv
, *mos_config
, *policy
;
3064 int error
= 0, copy_error
;
3065 uint64_t healthy_tvds
, healthy_tvds_mos
;
3066 uint64_t mos_config_txg
;
3068 if (spa_dir_prop(spa
, DMU_POOL_CONFIG
, &spa
->spa_config_object
, B_TRUE
)
3070 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3073 * If we're assembling a pool from a split, the config provided is
3074 * already trusted so there is nothing to do.
3076 if (type
== SPA_IMPORT_ASSEMBLE
)
3079 healthy_tvds
= spa_healthy_core_tvds(spa
);
3081 if (load_nvlist(spa
, spa
->spa_config_object
, &mos_config
)
3083 spa_load_failed(spa
, "unable to retrieve MOS config");
3084 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3088 * If we are doing an open, pool owner wasn't verified yet, thus do
3089 * the verification here.
3091 if (spa
->spa_load_state
== SPA_LOAD_OPEN
) {
3092 error
= spa_verify_host(spa
, mos_config
);
3094 nvlist_free(mos_config
);
3099 nv
= fnvlist_lookup_nvlist(mos_config
, ZPOOL_CONFIG_VDEV_TREE
);
3101 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3104 * Build a new vdev tree from the trusted config
3106 VERIFY(spa_config_parse(spa
, &mrvd
, nv
, NULL
, 0, VDEV_ALLOC_LOAD
) == 0);
3109 * Vdev paths in the MOS may be obsolete. If the untrusted config was
3110 * obtained by scanning /dev/dsk, then it will have the right vdev
3111 * paths. We update the trusted MOS config with this information.
3112 * We first try to copy the paths with vdev_copy_path_strict, which
3113 * succeeds only when both configs have exactly the same vdev tree.
3114 * If that fails, we fall back to a more flexible method that has a
3115 * best effort policy.
3117 copy_error
= vdev_copy_path_strict(rvd
, mrvd
);
3118 if (copy_error
!= 0 || spa_load_print_vdev_tree
) {
3119 spa_load_note(spa
, "provided vdev tree:");
3120 vdev_dbgmsg_print_tree(rvd
, 2);
3121 spa_load_note(spa
, "MOS vdev tree:");
3122 vdev_dbgmsg_print_tree(mrvd
, 2);
3124 if (copy_error
!= 0) {
3125 spa_load_note(spa
, "vdev_copy_path_strict failed, falling "
3126 "back to vdev_copy_path_relaxed");
3127 vdev_copy_path_relaxed(rvd
, mrvd
);
3132 spa
->spa_root_vdev
= mrvd
;
3134 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3137 * We will use spa_config if we decide to reload the spa or if spa_load
3138 * fails and we rewind. We must thus regenerate the config using the
3139 * MOS information with the updated paths. ZPOOL_LOAD_POLICY is used to
3140 * pass settings on how to load the pool and is not stored in the MOS.
3141 * We copy it over to our new, trusted config.
3143 mos_config_txg
= fnvlist_lookup_uint64(mos_config
,
3144 ZPOOL_CONFIG_POOL_TXG
);
3145 nvlist_free(mos_config
);
3146 mos_config
= spa_config_generate(spa
, NULL
, mos_config_txg
, B_FALSE
);
3147 if (nvlist_lookup_nvlist(spa
->spa_config
, ZPOOL_LOAD_POLICY
,
3149 fnvlist_add_nvlist(mos_config
, ZPOOL_LOAD_POLICY
, policy
);
3150 spa_config_set(spa
, mos_config
);
3151 spa
->spa_config_source
= SPA_CONFIG_SRC_MOS
;
3154 * Now that we got the config from the MOS, we should be more strict
3155 * in checking blkptrs and can make assumptions about the consistency
3156 * of the vdev tree. spa_trust_config must be set to true before opening
3157 * vdevs in order for them to be writeable.
3159 spa
->spa_trust_config
= B_TRUE
;
3162 * Open and validate the new vdev tree
3164 error
= spa_ld_open_vdevs(spa
);
3168 error
= spa_ld_validate_vdevs(spa
);
3172 if (copy_error
!= 0 || spa_load_print_vdev_tree
) {
3173 spa_load_note(spa
, "final vdev tree:");
3174 vdev_dbgmsg_print_tree(rvd
, 2);
3177 if (spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
&&
3178 !spa
->spa_extreme_rewind
&& zfs_max_missing_tvds
== 0) {
3180 * Sanity check to make sure that we are indeed loading the
3181 * latest uberblock. If we missed SPA_SYNC_MIN_VDEVS tvds
3182 * in the config provided and they happened to be the only ones
3183 * to have the latest uberblock, we could involuntarily perform
3184 * an extreme rewind.
3186 healthy_tvds_mos
= spa_healthy_core_tvds(spa
);
3187 if (healthy_tvds_mos
- healthy_tvds
>=
3188 SPA_SYNC_MIN_VDEVS
) {
3189 spa_load_note(spa
, "config provided misses too many "
3190 "top-level vdevs compared to MOS (%lld vs %lld). ",
3191 (u_longlong_t
)healthy_tvds
,
3192 (u_longlong_t
)healthy_tvds_mos
);
3193 spa_load_note(spa
, "vdev tree:");
3194 vdev_dbgmsg_print_tree(rvd
, 2);
3196 spa_load_failed(spa
, "config was already "
3197 "provided from MOS. Aborting.");
3198 return (spa_vdev_err(rvd
,
3199 VDEV_AUX_CORRUPT_DATA
, EIO
));
3201 spa_load_note(spa
, "spa must be reloaded using MOS "
3203 return (SET_ERROR(EAGAIN
));
3207 error
= spa_check_for_missing_logs(spa
);
3209 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
, ENXIO
));
3211 if (rvd
->vdev_guid_sum
!= spa
->spa_uberblock
.ub_guid_sum
) {
3212 spa_load_failed(spa
, "uberblock guid sum doesn't match MOS "
3213 "guid sum (%llu != %llu)",
3214 (u_longlong_t
)spa
->spa_uberblock
.ub_guid_sum
,
3215 (u_longlong_t
)rvd
->vdev_guid_sum
);
3216 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
,
3224 spa_ld_open_indirect_vdev_metadata(spa_t
*spa
)
3227 vdev_t
*rvd
= spa
->spa_root_vdev
;
3230 * Everything that we read before spa_remove_init() must be stored
3231 * on concreted vdevs. Therefore we do this as early as possible.
3233 error
= spa_remove_init(spa
);
3235 spa_load_failed(spa
, "spa_remove_init failed [error=%d]",
3237 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3241 * Retrieve information needed to condense indirect vdev mappings.
3243 error
= spa_condense_init(spa
);
3245 spa_load_failed(spa
, "spa_condense_init failed [error=%d]",
3247 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, error
));
3254 spa_ld_check_features(spa_t
*spa
, boolean_t
*missing_feat_writep
)
3257 vdev_t
*rvd
= spa
->spa_root_vdev
;
3259 if (spa_version(spa
) >= SPA_VERSION_FEATURES
) {
3260 boolean_t missing_feat_read
= B_FALSE
;
3261 nvlist_t
*unsup_feat
, *enabled_feat
;
3263 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_READ
,
3264 &spa
->spa_feat_for_read_obj
, B_TRUE
) != 0) {
3265 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3268 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_WRITE
,
3269 &spa
->spa_feat_for_write_obj
, B_TRUE
) != 0) {
3270 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3273 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_DESCRIPTIONS
,
3274 &spa
->spa_feat_desc_obj
, B_TRUE
) != 0) {
3275 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3278 enabled_feat
= fnvlist_alloc();
3279 unsup_feat
= fnvlist_alloc();
3281 if (!spa_features_check(spa
, B_FALSE
,
3282 unsup_feat
, enabled_feat
))
3283 missing_feat_read
= B_TRUE
;
3285 if (spa_writeable(spa
) ||
3286 spa
->spa_load_state
== SPA_LOAD_TRYIMPORT
) {
3287 if (!spa_features_check(spa
, B_TRUE
,
3288 unsup_feat
, enabled_feat
)) {
3289 *missing_feat_writep
= B_TRUE
;
3293 fnvlist_add_nvlist(spa
->spa_load_info
,
3294 ZPOOL_CONFIG_ENABLED_FEAT
, enabled_feat
);
3296 if (!nvlist_empty(unsup_feat
)) {
3297 fnvlist_add_nvlist(spa
->spa_load_info
,
3298 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
);
3301 fnvlist_free(enabled_feat
);
3302 fnvlist_free(unsup_feat
);
3304 if (!missing_feat_read
) {
3305 fnvlist_add_boolean(spa
->spa_load_info
,
3306 ZPOOL_CONFIG_CAN_RDONLY
);
3310 * If the state is SPA_LOAD_TRYIMPORT, our objective is
3311 * twofold: to determine whether the pool is available for
3312 * import in read-write mode and (if it is not) whether the
3313 * pool is available for import in read-only mode. If the pool
3314 * is available for import in read-write mode, it is displayed
3315 * as available in userland; if it is not available for import
3316 * in read-only mode, it is displayed as unavailable in
3317 * userland. If the pool is available for import in read-only
3318 * mode but not read-write mode, it is displayed as unavailable
3319 * in userland with a special note that the pool is actually
3320 * available for open in read-only mode.
3322 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
3323 * missing a feature for write, we must first determine whether
3324 * the pool can be opened read-only before returning to
3325 * userland in order to know whether to display the
3326 * abovementioned note.
3328 if (missing_feat_read
|| (*missing_feat_writep
&&
3329 spa_writeable(spa
))) {
3330 spa_load_failed(spa
, "pool uses unsupported features");
3331 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
3336 * Load refcounts for ZFS features from disk into an in-memory
3337 * cache during SPA initialization.
3339 for (spa_feature_t i
= 0; i
< SPA_FEATURES
; i
++) {
3342 error
= feature_get_refcount_from_disk(spa
,
3343 &spa_feature_table
[i
], &refcount
);
3345 spa
->spa_feat_refcount_cache
[i
] = refcount
;
3346 } else if (error
== ENOTSUP
) {
3347 spa
->spa_feat_refcount_cache
[i
] =
3348 SPA_FEATURE_DISABLED
;
3350 spa_load_failed(spa
, "error getting refcount "
3351 "for feature %s [error=%d]",
3352 spa_feature_table
[i
].fi_guid
, error
);
3353 return (spa_vdev_err(rvd
,
3354 VDEV_AUX_CORRUPT_DATA
, EIO
));
3359 if (spa_feature_is_active(spa
, SPA_FEATURE_ENABLED_TXG
)) {
3360 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_ENABLED_TXG
,
3361 &spa
->spa_feat_enabled_txg_obj
, B_TRUE
) != 0)
3362 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3369 spa_ld_load_special_directories(spa_t
*spa
)
3372 vdev_t
*rvd
= spa
->spa_root_vdev
;
3374 spa
->spa_is_initializing
= B_TRUE
;
3375 error
= dsl_pool_open(spa
->spa_dsl_pool
);
3376 spa
->spa_is_initializing
= B_FALSE
;
3378 spa_load_failed(spa
, "dsl_pool_open failed [error=%d]", error
);
3379 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3386 spa_ld_get_props(spa_t
*spa
)
3390 vdev_t
*rvd
= spa
->spa_root_vdev
;
3392 /* Grab the checksum salt from the MOS. */
3393 error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
3394 DMU_POOL_CHECKSUM_SALT
, 1,
3395 sizeof (spa
->spa_cksum_salt
.zcs_bytes
),
3396 spa
->spa_cksum_salt
.zcs_bytes
);
3397 if (error
== ENOENT
) {
3398 /* Generate a new salt for subsequent use */
3399 (void) random_get_pseudo_bytes(spa
->spa_cksum_salt
.zcs_bytes
,
3400 sizeof (spa
->spa_cksum_salt
.zcs_bytes
));
3401 } else if (error
!= 0) {
3402 spa_load_failed(spa
, "unable to retrieve checksum salt from "
3403 "MOS [error=%d]", error
);
3404 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3407 if (spa_dir_prop(spa
, DMU_POOL_SYNC_BPOBJ
, &obj
, B_TRUE
) != 0)
3408 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3409 error
= bpobj_open(&spa
->spa_deferred_bpobj
, spa
->spa_meta_objset
, obj
);
3411 spa_load_failed(spa
, "error opening deferred-frees bpobj "
3412 "[error=%d]", error
);
3413 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3417 * Load the bit that tells us to use the new accounting function
3418 * (raid-z deflation). If we have an older pool, this will not
3421 error
= spa_dir_prop(spa
, DMU_POOL_DEFLATE
, &spa
->spa_deflate
, B_FALSE
);
3422 if (error
!= 0 && error
!= ENOENT
)
3423 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3425 error
= spa_dir_prop(spa
, DMU_POOL_CREATION_VERSION
,
3426 &spa
->spa_creation_version
, B_FALSE
);
3427 if (error
!= 0 && error
!= ENOENT
)
3428 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3431 * Load the persistent error log. If we have an older pool, this will
3434 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_LAST
, &spa
->spa_errlog_last
,
3436 if (error
!= 0 && error
!= ENOENT
)
3437 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3439 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_SCRUB
,
3440 &spa
->spa_errlog_scrub
, B_FALSE
);
3441 if (error
!= 0 && error
!= ENOENT
)
3442 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3445 * Load the history object. If we have an older pool, this
3446 * will not be present.
3448 error
= spa_dir_prop(spa
, DMU_POOL_HISTORY
, &spa
->spa_history
, B_FALSE
);
3449 if (error
!= 0 && error
!= ENOENT
)
3450 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3453 * Load the per-vdev ZAP map. If we have an older pool, this will not
3454 * be present; in this case, defer its creation to a later time to
3455 * avoid dirtying the MOS this early / out of sync context. See
3456 * spa_sync_config_object.
3459 /* The sentinel is only available in the MOS config. */
3460 nvlist_t
*mos_config
;
3461 if (load_nvlist(spa
, spa
->spa_config_object
, &mos_config
) != 0) {
3462 spa_load_failed(spa
, "unable to retrieve MOS config");
3463 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3466 error
= spa_dir_prop(spa
, DMU_POOL_VDEV_ZAP_MAP
,
3467 &spa
->spa_all_vdev_zaps
, B_FALSE
);
3469 if (error
== ENOENT
) {
3470 VERIFY(!nvlist_exists(mos_config
,
3471 ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
));
3472 spa
->spa_avz_action
= AVZ_ACTION_INITIALIZE
;
3473 ASSERT0(vdev_count_verify_zaps(spa
->spa_root_vdev
));
3474 } else if (error
!= 0) {
3475 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3476 } else if (!nvlist_exists(mos_config
, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
)) {
3478 * An older version of ZFS overwrote the sentinel value, so
3479 * we have orphaned per-vdev ZAPs in the MOS. Defer their
3480 * destruction to later; see spa_sync_config_object.
3482 spa
->spa_avz_action
= AVZ_ACTION_DESTROY
;
3484 * We're assuming that no vdevs have had their ZAPs created
3485 * before this. Better be sure of it.
3487 ASSERT0(vdev_count_verify_zaps(spa
->spa_root_vdev
));
3489 nvlist_free(mos_config
);
3491 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
3493 error
= spa_dir_prop(spa
, DMU_POOL_PROPS
, &spa
->spa_pool_props_object
,
3495 if (error
&& error
!= ENOENT
)
3496 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3499 uint64_t autoreplace
;
3501 spa_prop_find(spa
, ZPOOL_PROP_BOOTFS
, &spa
->spa_bootfs
);
3502 spa_prop_find(spa
, ZPOOL_PROP_AUTOREPLACE
, &autoreplace
);
3503 spa_prop_find(spa
, ZPOOL_PROP_DELEGATION
, &spa
->spa_delegation
);
3504 spa_prop_find(spa
, ZPOOL_PROP_FAILUREMODE
, &spa
->spa_failmode
);
3505 spa_prop_find(spa
, ZPOOL_PROP_AUTOEXPAND
, &spa
->spa_autoexpand
);
3506 spa_prop_find(spa
, ZPOOL_PROP_MULTIHOST
, &spa
->spa_multihost
);
3507 spa_prop_find(spa
, ZPOOL_PROP_DEDUPDITTO
,
3508 &spa
->spa_dedup_ditto
);
3510 spa
->spa_autoreplace
= (autoreplace
!= 0);
3514 * If we are importing a pool with missing top-level vdevs,
3515 * we enforce that the pool doesn't panic or get suspended on
3516 * error since the likelihood of missing data is extremely high.
3518 if (spa
->spa_missing_tvds
> 0 &&
3519 spa
->spa_failmode
!= ZIO_FAILURE_MODE_CONTINUE
&&
3520 spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
) {
3521 spa_load_note(spa
, "forcing failmode to 'continue' "
3522 "as some top level vdevs are missing");
3523 spa
->spa_failmode
= ZIO_FAILURE_MODE_CONTINUE
;
3530 spa_ld_open_aux_vdevs(spa_t
*spa
, spa_import_type_t type
)
3533 vdev_t
*rvd
= spa
->spa_root_vdev
;
3536 * If we're assembling the pool from the split-off vdevs of
3537 * an existing pool, we don't want to attach the spares & cache
3542 * Load any hot spares for this pool.
3544 error
= spa_dir_prop(spa
, DMU_POOL_SPARES
, &spa
->spa_spares
.sav_object
,
3546 if (error
!= 0 && error
!= ENOENT
)
3547 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3548 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
3549 ASSERT(spa_version(spa
) >= SPA_VERSION_SPARES
);
3550 if (load_nvlist(spa
, spa
->spa_spares
.sav_object
,
3551 &spa
->spa_spares
.sav_config
) != 0) {
3552 spa_load_failed(spa
, "error loading spares nvlist");
3553 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3556 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3557 spa_load_spares(spa
);
3558 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3559 } else if (error
== 0) {
3560 spa
->spa_spares
.sav_sync
= B_TRUE
;
3564 * Load any level 2 ARC devices for this pool.
3566 error
= spa_dir_prop(spa
, DMU_POOL_L2CACHE
,
3567 &spa
->spa_l2cache
.sav_object
, B_FALSE
);
3568 if (error
!= 0 && error
!= ENOENT
)
3569 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3570 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
3571 ASSERT(spa_version(spa
) >= SPA_VERSION_L2CACHE
);
3572 if (load_nvlist(spa
, spa
->spa_l2cache
.sav_object
,
3573 &spa
->spa_l2cache
.sav_config
) != 0) {
3574 spa_load_failed(spa
, "error loading l2cache nvlist");
3575 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3578 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3579 spa_load_l2cache(spa
);
3580 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3581 } else if (error
== 0) {
3582 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
3589 spa_ld_load_vdev_metadata(spa_t
*spa
)
3592 vdev_t
*rvd
= spa
->spa_root_vdev
;
3595 * If the 'multihost' property is set, then never allow a pool to
3596 * be imported when the system hostid is zero. The exception to
3597 * this rule is zdb which is always allowed to access pools.
3599 if (spa_multihost(spa
) && spa_get_hostid() == 0 &&
3600 (spa
->spa_import_flags
& ZFS_IMPORT_SKIP_MMP
) == 0) {
3601 fnvlist_add_uint64(spa
->spa_load_info
,
3602 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_NO_HOSTID
);
3603 return (spa_vdev_err(rvd
, VDEV_AUX_ACTIVE
, EREMOTEIO
));
3607 * If the 'autoreplace' property is set, then post a resource notifying
3608 * the ZFS DE that it should not issue any faults for unopenable
3609 * devices. We also iterate over the vdevs, and post a sysevent for any
3610 * unopenable vdevs so that the normal autoreplace handler can take
3613 if (spa
->spa_autoreplace
&& spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
) {
3614 spa_check_removed(spa
->spa_root_vdev
);
3616 * For the import case, this is done in spa_import(), because
3617 * at this point we're using the spare definitions from
3618 * the MOS config, not necessarily from the userland config.
3620 if (spa
->spa_load_state
!= SPA_LOAD_IMPORT
) {
3621 spa_aux_check_removed(&spa
->spa_spares
);
3622 spa_aux_check_removed(&spa
->spa_l2cache
);
3627 * Load the vdev metadata such as metaslabs, DTLs, spacemap object, etc.
3629 error
= vdev_load(rvd
);
3631 spa_load_failed(spa
, "vdev_load failed [error=%d]", error
);
3632 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, error
));
3636 * Propagate the leaf DTLs we just loaded all the way up the vdev tree.
3638 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3639 vdev_dtl_reassess(rvd
, 0, 0, B_FALSE
);
3640 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3646 spa_ld_load_dedup_tables(spa_t
*spa
)
3649 vdev_t
*rvd
= spa
->spa_root_vdev
;
3651 error
= ddt_load(spa
);
3653 spa_load_failed(spa
, "ddt_load failed [error=%d]", error
);
3654 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3661 spa_ld_verify_logs(spa_t
*spa
, spa_import_type_t type
, char **ereport
)
3663 vdev_t
*rvd
= spa
->spa_root_vdev
;
3665 if (type
!= SPA_IMPORT_ASSEMBLE
&& spa_writeable(spa
)) {
3666 boolean_t missing
= spa_check_logs(spa
);
3668 if (spa
->spa_missing_tvds
!= 0) {
3669 spa_load_note(spa
, "spa_check_logs failed "
3670 "so dropping the logs");
3672 *ereport
= FM_EREPORT_ZFS_LOG_REPLAY
;
3673 spa_load_failed(spa
, "spa_check_logs failed");
3674 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_LOG
,
3684 spa_ld_verify_pool_data(spa_t
*spa
)
3687 vdev_t
*rvd
= spa
->spa_root_vdev
;
3690 * We've successfully opened the pool, verify that we're ready
3691 * to start pushing transactions.
3693 if (spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
) {
3694 error
= spa_load_verify(spa
);
3696 spa_load_failed(spa
, "spa_load_verify failed "
3697 "[error=%d]", error
);
3698 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
3707 spa_ld_claim_log_blocks(spa_t
*spa
)
3710 dsl_pool_t
*dp
= spa_get_dsl(spa
);
3713 * Claim log blocks that haven't been committed yet.
3714 * This must all happen in a single txg.
3715 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
3716 * invoked from zil_claim_log_block()'s i/o done callback.
3717 * Price of rollback is that we abandon the log.
3719 spa
->spa_claiming
= B_TRUE
;
3721 tx
= dmu_tx_create_assigned(dp
, spa_first_txg(spa
));
3722 (void) dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
3723 zil_claim
, tx
, DS_FIND_CHILDREN
);
3726 spa
->spa_claiming
= B_FALSE
;
3728 spa_set_log_state(spa
, SPA_LOG_GOOD
);
3732 spa_ld_check_for_config_update(spa_t
*spa
, uint64_t config_cache_txg
,
3733 boolean_t update_config_cache
)
3735 vdev_t
*rvd
= spa
->spa_root_vdev
;
3736 int need_update
= B_FALSE
;
3739 * If the config cache is stale, or we have uninitialized
3740 * metaslabs (see spa_vdev_add()), then update the config.
3742 * If this is a verbatim import, trust the current
3743 * in-core spa_config and update the disk labels.
3745 if (update_config_cache
|| config_cache_txg
!= spa
->spa_config_txg
||
3746 spa
->spa_load_state
== SPA_LOAD_IMPORT
||
3747 spa
->spa_load_state
== SPA_LOAD_RECOVER
||
3748 (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
))
3749 need_update
= B_TRUE
;
3751 for (int c
= 0; c
< rvd
->vdev_children
; c
++)
3752 if (rvd
->vdev_child
[c
]->vdev_ms_array
== 0)
3753 need_update
= B_TRUE
;
3756 * Update the config cache asychronously in case we're the
3757 * root pool, in which case the config cache isn't writable yet.
3760 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
3764 spa_ld_prepare_for_reload(spa_t
*spa
)
3766 int mode
= spa
->spa_mode
;
3767 int async_suspended
= spa
->spa_async_suspended
;
3770 spa_deactivate(spa
);
3771 spa_activate(spa
, mode
);
3774 * We save the value of spa_async_suspended as it gets reset to 0 by
3775 * spa_unload(). We want to restore it back to the original value before
3776 * returning as we might be calling spa_async_resume() later.
3778 spa
->spa_async_suspended
= async_suspended
;
3782 spa_ld_read_checkpoint_txg(spa_t
*spa
)
3784 uberblock_t checkpoint
;
3787 ASSERT0(spa
->spa_checkpoint_txg
);
3788 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
3790 error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
3791 DMU_POOL_ZPOOL_CHECKPOINT
, sizeof (uint64_t),
3792 sizeof (uberblock_t
) / sizeof (uint64_t), &checkpoint
);
3794 if (error
== ENOENT
)
3800 ASSERT3U(checkpoint
.ub_txg
, !=, 0);
3801 ASSERT3U(checkpoint
.ub_checkpoint_txg
, !=, 0);
3802 ASSERT3U(checkpoint
.ub_timestamp
, !=, 0);
3803 spa
->spa_checkpoint_txg
= checkpoint
.ub_txg
;
3804 spa
->spa_checkpoint_info
.sci_timestamp
= checkpoint
.ub_timestamp
;
3810 spa_ld_mos_init(spa_t
*spa
, spa_import_type_t type
)
3814 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
3815 ASSERT(spa
->spa_config_source
!= SPA_CONFIG_SRC_NONE
);
3818 * Never trust the config that is provided unless we are assembling
3819 * a pool following a split.
3820 * This means don't trust blkptrs and the vdev tree in general. This
3821 * also effectively puts the spa in read-only mode since
3822 * spa_writeable() checks for spa_trust_config to be true.
3823 * We will later load a trusted config from the MOS.
3825 if (type
!= SPA_IMPORT_ASSEMBLE
)
3826 spa
->spa_trust_config
= B_FALSE
;
3829 * Parse the config provided to create a vdev tree.
3831 error
= spa_ld_parse_config(spa
, type
);
3836 * Now that we have the vdev tree, try to open each vdev. This involves
3837 * opening the underlying physical device, retrieving its geometry and
3838 * probing the vdev with a dummy I/O. The state of each vdev will be set
3839 * based on the success of those operations. After this we'll be ready
3840 * to read from the vdevs.
3842 error
= spa_ld_open_vdevs(spa
);
3847 * Read the label of each vdev and make sure that the GUIDs stored
3848 * there match the GUIDs in the config provided.
3849 * If we're assembling a new pool that's been split off from an
3850 * existing pool, the labels haven't yet been updated so we skip
3851 * validation for now.
3853 if (type
!= SPA_IMPORT_ASSEMBLE
) {
3854 error
= spa_ld_validate_vdevs(spa
);
3860 * Read all vdev labels to find the best uberblock (i.e. latest,
3861 * unless spa_load_max_txg is set) and store it in spa_uberblock. We
3862 * get the list of features required to read blkptrs in the MOS from
3863 * the vdev label with the best uberblock and verify that our version
3864 * of zfs supports them all.
3866 error
= spa_ld_select_uberblock(spa
, type
);
3871 * Pass that uberblock to the dsl_pool layer which will open the root
3872 * blkptr. This blkptr points to the latest version of the MOS and will
3873 * allow us to read its contents.
3875 error
= spa_ld_open_rootbp(spa
);
3883 spa_ld_checkpoint_rewind(spa_t
*spa
)
3885 uberblock_t checkpoint
;
3888 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
3889 ASSERT(spa
->spa_import_flags
& ZFS_IMPORT_CHECKPOINT
);
3891 error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
3892 DMU_POOL_ZPOOL_CHECKPOINT
, sizeof (uint64_t),
3893 sizeof (uberblock_t
) / sizeof (uint64_t), &checkpoint
);
3896 spa_load_failed(spa
, "unable to retrieve checkpointed "
3897 "uberblock from the MOS config [error=%d]", error
);
3899 if (error
== ENOENT
)
3900 error
= ZFS_ERR_NO_CHECKPOINT
;
3905 ASSERT3U(checkpoint
.ub_txg
, <, spa
->spa_uberblock
.ub_txg
);
3906 ASSERT3U(checkpoint
.ub_txg
, ==, checkpoint
.ub_checkpoint_txg
);
3909 * We need to update the txg and timestamp of the checkpointed
3910 * uberblock to be higher than the latest one. This ensures that
3911 * the checkpointed uberblock is selected if we were to close and
3912 * reopen the pool right after we've written it in the vdev labels.
3913 * (also see block comment in vdev_uberblock_compare)
3915 checkpoint
.ub_txg
= spa
->spa_uberblock
.ub_txg
+ 1;
3916 checkpoint
.ub_timestamp
= gethrestime_sec();
3919 * Set current uberblock to be the checkpointed uberblock.
3921 spa
->spa_uberblock
= checkpoint
;
3924 * If we are doing a normal rewind, then the pool is open for
3925 * writing and we sync the "updated" checkpointed uberblock to
3926 * disk. Once this is done, we've basically rewound the whole
3927 * pool and there is no way back.
3929 * There are cases when we don't want to attempt and sync the
3930 * checkpointed uberblock to disk because we are opening a
3931 * pool as read-only. Specifically, verifying the checkpointed
3932 * state with zdb, and importing the checkpointed state to get
3933 * a "preview" of its content.
3935 if (spa_writeable(spa
)) {
3936 vdev_t
*rvd
= spa
->spa_root_vdev
;
3938 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3939 vdev_t
*svd
[SPA_SYNC_MIN_VDEVS
] = { NULL
};
3941 int children
= rvd
->vdev_children
;
3942 int c0
= spa_get_random(children
);
3944 for (int c
= 0; c
< children
; c
++) {
3945 vdev_t
*vd
= rvd
->vdev_child
[(c0
+ c
) % children
];
3947 /* Stop when revisiting the first vdev */
3948 if (c
> 0 && svd
[0] == vd
)
3951 if (vd
->vdev_ms_array
== 0 || vd
->vdev_islog
||
3952 !vdev_is_concrete(vd
))
3955 svd
[svdcount
++] = vd
;
3956 if (svdcount
== SPA_SYNC_MIN_VDEVS
)
3959 error
= vdev_config_sync(svd
, svdcount
, spa
->spa_first_txg
);
3961 spa
->spa_last_synced_guid
= rvd
->vdev_guid
;
3962 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3965 spa_load_failed(spa
, "failed to write checkpointed "
3966 "uberblock to the vdev labels [error=%d]", error
);
3975 spa_ld_mos_with_trusted_config(spa_t
*spa
, spa_import_type_t type
,
3976 boolean_t
*update_config_cache
)
3981 * Parse the config for pool, open and validate vdevs,
3982 * select an uberblock, and use that uberblock to open
3985 error
= spa_ld_mos_init(spa
, type
);
3990 * Retrieve the trusted config stored in the MOS and use it to create
3991 * a new, exact version of the vdev tree, then reopen all vdevs.
3993 error
= spa_ld_trusted_config(spa
, type
, B_FALSE
);
3994 if (error
== EAGAIN
) {
3995 if (update_config_cache
!= NULL
)
3996 *update_config_cache
= B_TRUE
;
3999 * Redo the loading process with the trusted config if it is
4000 * too different from the untrusted config.
4002 spa_ld_prepare_for_reload(spa
);
4003 spa_load_note(spa
, "RELOADING");
4004 error
= spa_ld_mos_init(spa
, type
);
4008 error
= spa_ld_trusted_config(spa
, type
, B_TRUE
);
4012 } else if (error
!= 0) {
4020 * Load an existing storage pool, using the config provided. This config
4021 * describes which vdevs are part of the pool and is later validated against
4022 * partial configs present in each vdev's label and an entire copy of the
4023 * config stored in the MOS.
4026 spa_load_impl(spa_t
*spa
, spa_import_type_t type
, char **ereport
)
4029 boolean_t missing_feat_write
= B_FALSE
;
4030 boolean_t checkpoint_rewind
=
4031 (spa
->spa_import_flags
& ZFS_IMPORT_CHECKPOINT
);
4032 boolean_t update_config_cache
= B_FALSE
;
4034 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
4035 ASSERT(spa
->spa_config_source
!= SPA_CONFIG_SRC_NONE
);
4037 spa_load_note(spa
, "LOADING");
4039 error
= spa_ld_mos_with_trusted_config(spa
, type
, &update_config_cache
);
4044 * If we are rewinding to the checkpoint then we need to repeat
4045 * everything we've done so far in this function but this time
4046 * selecting the checkpointed uberblock and using that to open
4049 if (checkpoint_rewind
) {
4051 * If we are rewinding to the checkpoint update config cache
4054 update_config_cache
= B_TRUE
;
4057 * Extract the checkpointed uberblock from the current MOS
4058 * and use this as the pool's uberblock from now on. If the
4059 * pool is imported as writeable we also write the checkpoint
4060 * uberblock to the labels, making the rewind permanent.
4062 error
= spa_ld_checkpoint_rewind(spa
);
4067 * Redo the loading process process again with the
4068 * checkpointed uberblock.
4070 spa_ld_prepare_for_reload(spa
);
4071 spa_load_note(spa
, "LOADING checkpointed uberblock");
4072 error
= spa_ld_mos_with_trusted_config(spa
, type
, NULL
);
4078 * Retrieve the checkpoint txg if the pool has a checkpoint.
4080 error
= spa_ld_read_checkpoint_txg(spa
);
4085 * Retrieve the mapping of indirect vdevs. Those vdevs were removed
4086 * from the pool and their contents were re-mapped to other vdevs. Note
4087 * that everything that we read before this step must have been
4088 * rewritten on concrete vdevs after the last device removal was
4089 * initiated. Otherwise we could be reading from indirect vdevs before
4090 * we have loaded their mappings.
4092 error
= spa_ld_open_indirect_vdev_metadata(spa
);
4097 * Retrieve the full list of active features from the MOS and check if
4098 * they are all supported.
4100 error
= spa_ld_check_features(spa
, &missing_feat_write
);
4105 * Load several special directories from the MOS needed by the dsl_pool
4108 error
= spa_ld_load_special_directories(spa
);
4113 * Retrieve pool properties from the MOS.
4115 error
= spa_ld_get_props(spa
);
4120 * Retrieve the list of auxiliary devices - cache devices and spares -
4123 error
= spa_ld_open_aux_vdevs(spa
, type
);
4128 * Load the metadata for all vdevs. Also check if unopenable devices
4129 * should be autoreplaced.
4131 error
= spa_ld_load_vdev_metadata(spa
);
4135 error
= spa_ld_load_dedup_tables(spa
);
4140 * Verify the logs now to make sure we don't have any unexpected errors
4141 * when we claim log blocks later.
4143 error
= spa_ld_verify_logs(spa
, type
, ereport
);
4147 if (missing_feat_write
) {
4148 ASSERT(spa
->spa_load_state
== SPA_LOAD_TRYIMPORT
);
4151 * At this point, we know that we can open the pool in
4152 * read-only mode but not read-write mode. We now have enough
4153 * information and can return to userland.
4155 return (spa_vdev_err(spa
->spa_root_vdev
, VDEV_AUX_UNSUP_FEAT
,
4160 * Traverse the last txgs to make sure the pool was left off in a safe
4161 * state. When performing an extreme rewind, we verify the whole pool,
4162 * which can take a very long time.
4164 error
= spa_ld_verify_pool_data(spa
);
4169 * Calculate the deflated space for the pool. This must be done before
4170 * we write anything to the pool because we'd need to update the space
4171 * accounting using the deflated sizes.
4173 spa_update_dspace(spa
);
4176 * We have now retrieved all the information we needed to open the
4177 * pool. If we are importing the pool in read-write mode, a few
4178 * additional steps must be performed to finish the import.
4180 if (spa_writeable(spa
) && (spa
->spa_load_state
== SPA_LOAD_RECOVER
||
4181 spa
->spa_load_max_txg
== UINT64_MAX
)) {
4182 uint64_t config_cache_txg
= spa
->spa_config_txg
;
4184 ASSERT(spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
);
4187 * In case of a checkpoint rewind, log the original txg
4188 * of the checkpointed uberblock.
4190 if (checkpoint_rewind
) {
4191 spa_history_log_internal(spa
, "checkpoint rewind",
4192 NULL
, "rewound state to txg=%llu",
4193 (u_longlong_t
)spa
->spa_uberblock
.ub_checkpoint_txg
);
4197 * Traverse the ZIL and claim all blocks.
4199 spa_ld_claim_log_blocks(spa
);
4202 * Kick-off the syncing thread.
4204 spa
->spa_sync_on
= B_TRUE
;
4205 txg_sync_start(spa
->spa_dsl_pool
);
4206 mmp_thread_start(spa
);
4209 * Wait for all claims to sync. We sync up to the highest
4210 * claimed log block birth time so that claimed log blocks
4211 * don't appear to be from the future. spa_claim_max_txg
4212 * will have been set for us by ZIL traversal operations
4215 txg_wait_synced(spa
->spa_dsl_pool
, spa
->spa_claim_max_txg
);
4218 * Check if we need to request an update of the config. On the
4219 * next sync, we would update the config stored in vdev labels
4220 * and the cachefile (by default /etc/zfs/zpool.cache).
4222 spa_ld_check_for_config_update(spa
, config_cache_txg
,
4223 update_config_cache
);
4226 * Check all DTLs to see if anything needs resilvering.
4228 if (!dsl_scan_resilvering(spa
->spa_dsl_pool
) &&
4229 vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
))
4230 spa_async_request(spa
, SPA_ASYNC_RESILVER
);
4233 * Log the fact that we booted up (so that we can detect if
4234 * we rebooted in the middle of an operation).
4236 spa_history_log_version(spa
, "open", NULL
);
4238 spa_restart_removal(spa
);
4239 spa_spawn_aux_threads(spa
);
4242 * Delete any inconsistent datasets.
4245 * Since we may be issuing deletes for clones here,
4246 * we make sure to do so after we've spawned all the
4247 * auxiliary threads above (from which the livelist
4248 * deletion zthr is part of).
4250 (void) dmu_objset_find(spa_name(spa
),
4251 dsl_destroy_inconsistent
, NULL
, DS_FIND_CHILDREN
);
4254 * Clean up any stale temporary dataset userrefs.
4256 dsl_pool_clean_tmp_userrefs(spa
->spa_dsl_pool
);
4258 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
4259 vdev_initialize_restart(spa
->spa_root_vdev
);
4260 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
4263 spa_load_note(spa
, "LOADED");
4269 spa_load_retry(spa_t
*spa
, spa_load_state_t state
)
4271 int mode
= spa
->spa_mode
;
4274 spa_deactivate(spa
);
4276 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
- 1;
4278 spa_activate(spa
, mode
);
4279 spa_async_suspend(spa
);
4281 spa_load_note(spa
, "spa_load_retry: rewind, max txg: %llu",
4282 (u_longlong_t
)spa
->spa_load_max_txg
);
4284 return (spa_load(spa
, state
, SPA_IMPORT_EXISTING
));
4288 * If spa_load() fails this function will try loading prior txg's. If
4289 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
4290 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
4291 * function will not rewind the pool and will return the same error as
4295 spa_load_best(spa_t
*spa
, spa_load_state_t state
, uint64_t max_request
,
4298 nvlist_t
*loadinfo
= NULL
;
4299 nvlist_t
*config
= NULL
;
4300 int load_error
, rewind_error
;
4301 uint64_t safe_rewind_txg
;
4304 if (spa
->spa_load_txg
&& state
== SPA_LOAD_RECOVER
) {
4305 spa
->spa_load_max_txg
= spa
->spa_load_txg
;
4306 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
4308 spa
->spa_load_max_txg
= max_request
;
4309 if (max_request
!= UINT64_MAX
)
4310 spa
->spa_extreme_rewind
= B_TRUE
;
4313 load_error
= rewind_error
= spa_load(spa
, state
, SPA_IMPORT_EXISTING
);
4314 if (load_error
== 0)
4316 if (load_error
== ZFS_ERR_NO_CHECKPOINT
) {
4318 * When attempting checkpoint-rewind on a pool with no
4319 * checkpoint, we should not attempt to load uberblocks
4320 * from previous txgs when spa_load fails.
4322 ASSERT(spa
->spa_import_flags
& ZFS_IMPORT_CHECKPOINT
);
4323 return (load_error
);
4326 if (spa
->spa_root_vdev
!= NULL
)
4327 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
4329 spa
->spa_last_ubsync_txg
= spa
->spa_uberblock
.ub_txg
;
4330 spa
->spa_last_ubsync_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
4332 if (rewind_flags
& ZPOOL_NEVER_REWIND
) {
4333 nvlist_free(config
);
4334 return (load_error
);
4337 if (state
== SPA_LOAD_RECOVER
) {
4338 /* Price of rolling back is discarding txgs, including log */
4339 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
4342 * If we aren't rolling back save the load info from our first
4343 * import attempt so that we can restore it after attempting
4346 loadinfo
= spa
->spa_load_info
;
4347 spa
->spa_load_info
= fnvlist_alloc();
4350 spa
->spa_load_max_txg
= spa
->spa_last_ubsync_txg
;
4351 safe_rewind_txg
= spa
->spa_last_ubsync_txg
- TXG_DEFER_SIZE
;
4352 min_txg
= (rewind_flags
& ZPOOL_EXTREME_REWIND
) ?
4353 TXG_INITIAL
: safe_rewind_txg
;
4356 * Continue as long as we're finding errors, we're still within
4357 * the acceptable rewind range, and we're still finding uberblocks
4359 while (rewind_error
&& spa
->spa_uberblock
.ub_txg
>= min_txg
&&
4360 spa
->spa_uberblock
.ub_txg
<= spa
->spa_load_max_txg
) {
4361 if (spa
->spa_load_max_txg
< safe_rewind_txg
)
4362 spa
->spa_extreme_rewind
= B_TRUE
;
4363 rewind_error
= spa_load_retry(spa
, state
);
4366 spa
->spa_extreme_rewind
= B_FALSE
;
4367 spa
->spa_load_max_txg
= UINT64_MAX
;
4369 if (config
&& (rewind_error
|| state
!= SPA_LOAD_RECOVER
))
4370 spa_config_set(spa
, config
);
4372 nvlist_free(config
);
4374 if (state
== SPA_LOAD_RECOVER
) {
4375 ASSERT3P(loadinfo
, ==, NULL
);
4376 return (rewind_error
);
4378 /* Store the rewind info as part of the initial load info */
4379 fnvlist_add_nvlist(loadinfo
, ZPOOL_CONFIG_REWIND_INFO
,
4380 spa
->spa_load_info
);
4382 /* Restore the initial load info */
4383 fnvlist_free(spa
->spa_load_info
);
4384 spa
->spa_load_info
= loadinfo
;
4386 return (load_error
);
4393 * The import case is identical to an open except that the configuration is sent
4394 * down from userland, instead of grabbed from the configuration cache. For the
4395 * case of an open, the pool configuration will exist in the
4396 * POOL_STATE_UNINITIALIZED state.
4398 * The stats information (gen/count/ustats) is used to gather vdev statistics at
4399 * the same time open the pool, without having to keep around the spa_t in some
4403 spa_open_common(const char *pool
, spa_t
**spapp
, void *tag
, nvlist_t
*nvpolicy
,
4407 spa_load_state_t state
= SPA_LOAD_OPEN
;
4409 int locked
= B_FALSE
;
4410 int firstopen
= B_FALSE
;
4415 * As disgusting as this is, we need to support recursive calls to this
4416 * function because dsl_dir_open() is called during spa_load(), and ends
4417 * up calling spa_open() again. The real fix is to figure out how to
4418 * avoid dsl_dir_open() calling this in the first place.
4420 if (MUTEX_NOT_HELD(&spa_namespace_lock
)) {
4421 mutex_enter(&spa_namespace_lock
);
4425 if ((spa
= spa_lookup(pool
)) == NULL
) {
4427 mutex_exit(&spa_namespace_lock
);
4428 return (SET_ERROR(ENOENT
));
4431 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
) {
4432 zpool_load_policy_t policy
;
4436 zpool_get_load_policy(nvpolicy
? nvpolicy
: spa
->spa_config
,
4438 if (policy
.zlp_rewind
& ZPOOL_DO_REWIND
)
4439 state
= SPA_LOAD_RECOVER
;
4441 spa_activate(spa
, spa_mode_global
);
4443 if (state
!= SPA_LOAD_RECOVER
)
4444 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
4445 spa
->spa_config_source
= SPA_CONFIG_SRC_CACHEFILE
;
4447 zfs_dbgmsg("spa_open_common: opening %s", pool
);
4448 error
= spa_load_best(spa
, state
, policy
.zlp_txg
,
4451 if (error
== EBADF
) {
4453 * If vdev_validate() returns failure (indicated by
4454 * EBADF), it indicates that one of the vdevs indicates
4455 * that the pool has been exported or destroyed. If
4456 * this is the case, the config cache is out of sync and
4457 * we should remove the pool from the namespace.
4460 spa_deactivate(spa
);
4461 spa_write_cachefile(spa
, B_TRUE
, B_TRUE
);
4464 mutex_exit(&spa_namespace_lock
);
4465 return (SET_ERROR(ENOENT
));
4470 * We can't open the pool, but we still have useful
4471 * information: the state of each vdev after the
4472 * attempted vdev_open(). Return this to the user.
4474 if (config
!= NULL
&& spa
->spa_config
) {
4475 VERIFY(nvlist_dup(spa
->spa_config
, config
,
4477 VERIFY(nvlist_add_nvlist(*config
,
4478 ZPOOL_CONFIG_LOAD_INFO
,
4479 spa
->spa_load_info
) == 0);
4482 spa_deactivate(spa
);
4483 spa
->spa_last_open_failed
= error
;
4485 mutex_exit(&spa_namespace_lock
);
4491 spa_open_ref(spa
, tag
);
4494 *config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
4497 * If we've recovered the pool, pass back any information we
4498 * gathered while doing the load.
4500 if (state
== SPA_LOAD_RECOVER
) {
4501 VERIFY(nvlist_add_nvlist(*config
, ZPOOL_CONFIG_LOAD_INFO
,
4502 spa
->spa_load_info
) == 0);
4506 spa
->spa_last_open_failed
= 0;
4507 spa
->spa_last_ubsync_txg
= 0;
4508 spa
->spa_load_txg
= 0;
4509 mutex_exit(&spa_namespace_lock
);
4513 zvol_create_minors(spa
, spa_name(spa
), B_TRUE
);
4521 spa_open_rewind(const char *name
, spa_t
**spapp
, void *tag
, nvlist_t
*policy
,
4524 return (spa_open_common(name
, spapp
, tag
, policy
, config
));
4528 spa_open(const char *name
, spa_t
**spapp
, void *tag
)
4530 return (spa_open_common(name
, spapp
, tag
, NULL
, NULL
));
4534 * Lookup the given spa_t, incrementing the inject count in the process,
4535 * preventing it from being exported or destroyed.
4538 spa_inject_addref(char *name
)
4542 mutex_enter(&spa_namespace_lock
);
4543 if ((spa
= spa_lookup(name
)) == NULL
) {
4544 mutex_exit(&spa_namespace_lock
);
4547 spa
->spa_inject_ref
++;
4548 mutex_exit(&spa_namespace_lock
);
4554 spa_inject_delref(spa_t
*spa
)
4556 mutex_enter(&spa_namespace_lock
);
4557 spa
->spa_inject_ref
--;
4558 mutex_exit(&spa_namespace_lock
);
4562 * Add spares device information to the nvlist.
4565 spa_add_spares(spa_t
*spa
, nvlist_t
*config
)
4575 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
4577 if (spa
->spa_spares
.sav_count
== 0)
4580 VERIFY(nvlist_lookup_nvlist(config
,
4581 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
4582 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
4583 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
4585 VERIFY(nvlist_add_nvlist_array(nvroot
,
4586 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
4587 VERIFY(nvlist_lookup_nvlist_array(nvroot
,
4588 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
4591 * Go through and find any spares which have since been
4592 * repurposed as an active spare. If this is the case, update
4593 * their status appropriately.
4595 for (i
= 0; i
< nspares
; i
++) {
4596 VERIFY(nvlist_lookup_uint64(spares
[i
],
4597 ZPOOL_CONFIG_GUID
, &guid
) == 0);
4598 if (spa_spare_exists(guid
, &pool
, NULL
) &&
4600 VERIFY(nvlist_lookup_uint64_array(
4601 spares
[i
], ZPOOL_CONFIG_VDEV_STATS
,
4602 (uint64_t **)&vs
, &vsc
) == 0);
4603 vs
->vs_state
= VDEV_STATE_CANT_OPEN
;
4604 vs
->vs_aux
= VDEV_AUX_SPARED
;
4611 * Add l2cache device information to the nvlist, including vdev stats.
4614 spa_add_l2cache(spa_t
*spa
, nvlist_t
*config
)
4617 uint_t i
, j
, nl2cache
;
4624 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
4626 if (spa
->spa_l2cache
.sav_count
== 0)
4629 VERIFY(nvlist_lookup_nvlist(config
,
4630 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
4631 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_l2cache
.sav_config
,
4632 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
4633 if (nl2cache
!= 0) {
4634 VERIFY(nvlist_add_nvlist_array(nvroot
,
4635 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
4636 VERIFY(nvlist_lookup_nvlist_array(nvroot
,
4637 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
4640 * Update level 2 cache device stats.
4643 for (i
= 0; i
< nl2cache
; i
++) {
4644 VERIFY(nvlist_lookup_uint64(l2cache
[i
],
4645 ZPOOL_CONFIG_GUID
, &guid
) == 0);
4648 for (j
= 0; j
< spa
->spa_l2cache
.sav_count
; j
++) {
4650 spa
->spa_l2cache
.sav_vdevs
[j
]->vdev_guid
) {
4651 vd
= spa
->spa_l2cache
.sav_vdevs
[j
];
4657 VERIFY(nvlist_lookup_uint64_array(l2cache
[i
],
4658 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
, &vsc
)
4660 vdev_get_stats(vd
, vs
);
4661 vdev_config_generate_stats(vd
, l2cache
[i
]);
4668 spa_feature_stats_from_disk(spa_t
*spa
, nvlist_t
*features
)
4673 if (spa
->spa_feat_for_read_obj
!= 0) {
4674 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
4675 spa
->spa_feat_for_read_obj
);
4676 zap_cursor_retrieve(&zc
, &za
) == 0;
4677 zap_cursor_advance(&zc
)) {
4678 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
4679 za
.za_num_integers
== 1);
4680 VERIFY0(nvlist_add_uint64(features
, za
.za_name
,
4681 za
.za_first_integer
));
4683 zap_cursor_fini(&zc
);
4686 if (spa
->spa_feat_for_write_obj
!= 0) {
4687 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
4688 spa
->spa_feat_for_write_obj
);
4689 zap_cursor_retrieve(&zc
, &za
) == 0;
4690 zap_cursor_advance(&zc
)) {
4691 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
4692 za
.za_num_integers
== 1);
4693 VERIFY0(nvlist_add_uint64(features
, za
.za_name
,
4694 za
.za_first_integer
));
4696 zap_cursor_fini(&zc
);
4701 spa_feature_stats_from_cache(spa_t
*spa
, nvlist_t
*features
)
4705 for (i
= 0; i
< SPA_FEATURES
; i
++) {
4706 zfeature_info_t feature
= spa_feature_table
[i
];
4709 if (feature_get_refcount(spa
, &feature
, &refcount
) != 0)
4712 VERIFY0(nvlist_add_uint64(features
, feature
.fi_guid
, refcount
));
4717 * Store a list of pool features and their reference counts in the
4720 * The first time this is called on a spa, allocate a new nvlist, fetch
4721 * the pool features and reference counts from disk, then save the list
4722 * in the spa. In subsequent calls on the same spa use the saved nvlist
4723 * and refresh its values from the cached reference counts. This
4724 * ensures we don't block here on I/O on a suspended pool so 'zpool
4725 * clear' can resume the pool.
4728 spa_add_feature_stats(spa_t
*spa
, nvlist_t
*config
)
4732 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
4734 mutex_enter(&spa
->spa_feat_stats_lock
);
4735 features
= spa
->spa_feat_stats
;
4737 if (features
!= NULL
) {
4738 spa_feature_stats_from_cache(spa
, features
);
4740 VERIFY0(nvlist_alloc(&features
, NV_UNIQUE_NAME
, KM_SLEEP
));
4741 spa
->spa_feat_stats
= features
;
4742 spa_feature_stats_from_disk(spa
, features
);
4745 VERIFY0(nvlist_add_nvlist(config
, ZPOOL_CONFIG_FEATURE_STATS
,
4748 mutex_exit(&spa
->spa_feat_stats_lock
);
4752 spa_get_stats(const char *name
, nvlist_t
**config
,
4753 char *altroot
, size_t buflen
)
4759 error
= spa_open_common(name
, &spa
, FTAG
, NULL
, config
);
4763 * This still leaves a window of inconsistency where the spares
4764 * or l2cache devices could change and the config would be
4765 * self-inconsistent.
4767 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
4769 if (*config
!= NULL
) {
4770 uint64_t loadtimes
[2];
4772 loadtimes
[0] = spa
->spa_loaded_ts
.tv_sec
;
4773 loadtimes
[1] = spa
->spa_loaded_ts
.tv_nsec
;
4774 VERIFY(nvlist_add_uint64_array(*config
,
4775 ZPOOL_CONFIG_LOADED_TIME
, loadtimes
, 2) == 0);
4777 VERIFY(nvlist_add_uint64(*config
,
4778 ZPOOL_CONFIG_ERRCOUNT
,
4779 spa_get_errlog_size(spa
)) == 0);
4781 if (spa_suspended(spa
)) {
4782 VERIFY(nvlist_add_uint64(*config
,
4783 ZPOOL_CONFIG_SUSPENDED
,
4784 spa
->spa_failmode
) == 0);
4785 VERIFY(nvlist_add_uint64(*config
,
4786 ZPOOL_CONFIG_SUSPENDED_REASON
,
4787 spa
->spa_suspended
) == 0);
4790 spa_add_spares(spa
, *config
);
4791 spa_add_l2cache(spa
, *config
);
4792 spa_add_feature_stats(spa
, *config
);
4797 * We want to get the alternate root even for faulted pools, so we cheat
4798 * and call spa_lookup() directly.
4802 mutex_enter(&spa_namespace_lock
);
4803 spa
= spa_lookup(name
);
4805 spa_altroot(spa
, altroot
, buflen
);
4809 mutex_exit(&spa_namespace_lock
);
4811 spa_altroot(spa
, altroot
, buflen
);
4816 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
4817 spa_close(spa
, FTAG
);
4824 * Validate that the auxiliary device array is well formed. We must have an
4825 * array of nvlists, each which describes a valid leaf vdev. If this is an
4826 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
4827 * specified, as long as they are well-formed.
4830 spa_validate_aux_devs(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
,
4831 spa_aux_vdev_t
*sav
, const char *config
, uint64_t version
,
4832 vdev_labeltype_t label
)
4839 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
4842 * It's acceptable to have no devs specified.
4844 if (nvlist_lookup_nvlist_array(nvroot
, config
, &dev
, &ndev
) != 0)
4848 return (SET_ERROR(EINVAL
));
4851 * Make sure the pool is formatted with a version that supports this
4854 if (spa_version(spa
) < version
)
4855 return (SET_ERROR(ENOTSUP
));
4858 * Set the pending device list so we correctly handle device in-use
4861 sav
->sav_pending
= dev
;
4862 sav
->sav_npending
= ndev
;
4864 for (i
= 0; i
< ndev
; i
++) {
4865 if ((error
= spa_config_parse(spa
, &vd
, dev
[i
], NULL
, 0,
4869 if (!vd
->vdev_ops
->vdev_op_leaf
) {
4871 error
= SET_ERROR(EINVAL
);
4877 if ((error
= vdev_open(vd
)) == 0 &&
4878 (error
= vdev_label_init(vd
, crtxg
, label
)) == 0) {
4879 VERIFY(nvlist_add_uint64(dev
[i
], ZPOOL_CONFIG_GUID
,
4880 vd
->vdev_guid
) == 0);
4886 (mode
!= VDEV_ALLOC_SPARE
&& mode
!= VDEV_ALLOC_L2CACHE
))
4893 sav
->sav_pending
= NULL
;
4894 sav
->sav_npending
= 0;
4899 spa_validate_aux(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
)
4903 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
4905 if ((error
= spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
4906 &spa
->spa_spares
, ZPOOL_CONFIG_SPARES
, SPA_VERSION_SPARES
,
4907 VDEV_LABEL_SPARE
)) != 0) {
4911 return (spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
4912 &spa
->spa_l2cache
, ZPOOL_CONFIG_L2CACHE
, SPA_VERSION_L2CACHE
,
4913 VDEV_LABEL_L2CACHE
));
4917 spa_set_aux_vdevs(spa_aux_vdev_t
*sav
, nvlist_t
**devs
, int ndevs
,
4922 if (sav
->sav_config
!= NULL
) {
4928 * Generate new dev list by concatenating with the
4931 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
, config
,
4932 &olddevs
, &oldndevs
) == 0);
4934 newdevs
= kmem_alloc(sizeof (void *) *
4935 (ndevs
+ oldndevs
), KM_SLEEP
);
4936 for (i
= 0; i
< oldndevs
; i
++)
4937 VERIFY(nvlist_dup(olddevs
[i
], &newdevs
[i
],
4939 for (i
= 0; i
< ndevs
; i
++)
4940 VERIFY(nvlist_dup(devs
[i
], &newdevs
[i
+ oldndevs
],
4943 VERIFY(nvlist_remove(sav
->sav_config
, config
,
4944 DATA_TYPE_NVLIST_ARRAY
) == 0);
4946 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
,
4947 config
, newdevs
, ndevs
+ oldndevs
) == 0);
4948 for (i
= 0; i
< oldndevs
+ ndevs
; i
++)
4949 nvlist_free(newdevs
[i
]);
4950 kmem_free(newdevs
, (oldndevs
+ ndevs
) * sizeof (void *));
4953 * Generate a new dev list.
4955 VERIFY(nvlist_alloc(&sav
->sav_config
, NV_UNIQUE_NAME
,
4957 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
, config
,
4963 * Stop and drop level 2 ARC devices
4966 spa_l2cache_drop(spa_t
*spa
)
4970 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
4972 for (i
= 0; i
< sav
->sav_count
; i
++) {
4975 vd
= sav
->sav_vdevs
[i
];
4978 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
4979 pool
!= 0ULL && l2arc_vdev_present(vd
))
4980 l2arc_remove_vdev(vd
);
4985 * Verify encryption parameters for spa creation. If we are encrypting, we must
4986 * have the encryption feature flag enabled.
4989 spa_create_check_encryption_params(dsl_crypto_params_t
*dcp
,
4990 boolean_t has_encryption
)
4992 if (dcp
->cp_crypt
!= ZIO_CRYPT_OFF
&&
4993 dcp
->cp_crypt
!= ZIO_CRYPT_INHERIT
&&
4995 return (SET_ERROR(ENOTSUP
));
4997 return (dmu_objset_create_crypt_check(NULL
, dcp
, NULL
));
5004 spa_create(const char *pool
, nvlist_t
*nvroot
, nvlist_t
*props
,
5005 nvlist_t
*zplprops
, dsl_crypto_params_t
*dcp
)
5008 char *altroot
= NULL
;
5013 uint64_t txg
= TXG_INITIAL
;
5014 nvlist_t
**spares
, **l2cache
;
5015 uint_t nspares
, nl2cache
;
5016 uint64_t version
, obj
;
5017 boolean_t has_features
;
5018 boolean_t has_encryption
;
5024 if (props
== NULL
||
5025 nvlist_lookup_string(props
, "tname", &poolname
) != 0)
5026 poolname
= (char *)pool
;
5029 * If this pool already exists, return failure.
5031 mutex_enter(&spa_namespace_lock
);
5032 if (spa_lookup(poolname
) != NULL
) {
5033 mutex_exit(&spa_namespace_lock
);
5034 return (SET_ERROR(EEXIST
));
5038 * Allocate a new spa_t structure.
5040 nvl
= fnvlist_alloc();
5041 fnvlist_add_string(nvl
, ZPOOL_CONFIG_POOL_NAME
, pool
);
5042 (void) nvlist_lookup_string(props
,
5043 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
5044 spa
= spa_add(poolname
, nvl
, altroot
);
5046 spa_activate(spa
, spa_mode_global
);
5048 if (props
&& (error
= spa_prop_validate(spa
, props
))) {
5049 spa_deactivate(spa
);
5051 mutex_exit(&spa_namespace_lock
);
5056 * Temporary pool names should never be written to disk.
5058 if (poolname
!= pool
)
5059 spa
->spa_import_flags
|= ZFS_IMPORT_TEMP_NAME
;
5061 has_features
= B_FALSE
;
5062 has_encryption
= B_FALSE
;
5063 for (nvpair_t
*elem
= nvlist_next_nvpair(props
, NULL
);
5064 elem
!= NULL
; elem
= nvlist_next_nvpair(props
, elem
)) {
5065 if (zpool_prop_feature(nvpair_name(elem
))) {
5066 has_features
= B_TRUE
;
5068 feat_name
= strchr(nvpair_name(elem
), '@') + 1;
5069 VERIFY0(zfeature_lookup_name(feat_name
, &feat
));
5070 if (feat
== SPA_FEATURE_ENCRYPTION
)
5071 has_encryption
= B_TRUE
;
5075 /* verify encryption params, if they were provided */
5077 error
= spa_create_check_encryption_params(dcp
, has_encryption
);
5079 spa_deactivate(spa
);
5081 mutex_exit(&spa_namespace_lock
);
5086 if (has_features
|| nvlist_lookup_uint64(props
,
5087 zpool_prop_to_name(ZPOOL_PROP_VERSION
), &version
) != 0) {
5088 version
= SPA_VERSION
;
5090 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
5092 spa
->spa_first_txg
= txg
;
5093 spa
->spa_uberblock
.ub_txg
= txg
- 1;
5094 spa
->spa_uberblock
.ub_version
= version
;
5095 spa
->spa_ubsync
= spa
->spa_uberblock
;
5096 spa
->spa_load_state
= SPA_LOAD_CREATE
;
5097 spa
->spa_removing_phys
.sr_state
= DSS_NONE
;
5098 spa
->spa_removing_phys
.sr_removing_vdev
= -1;
5099 spa
->spa_removing_phys
.sr_prev_indirect_vdev
= -1;
5102 * Create "The Godfather" zio to hold all async IOs
5104 spa
->spa_async_zio_root
= kmem_alloc(max_ncpus
* sizeof (void *),
5106 for (int i
= 0; i
< max_ncpus
; i
++) {
5107 spa
->spa_async_zio_root
[i
] = zio_root(spa
, NULL
, NULL
,
5108 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
5109 ZIO_FLAG_GODFATHER
);
5113 * Create the root vdev.
5115 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5117 error
= spa_config_parse(spa
, &rvd
, nvroot
, NULL
, 0, VDEV_ALLOC_ADD
);
5119 ASSERT(error
!= 0 || rvd
!= NULL
);
5120 ASSERT(error
!= 0 || spa
->spa_root_vdev
== rvd
);
5122 if (error
== 0 && !zfs_allocatable_devs(nvroot
))
5123 error
= SET_ERROR(EINVAL
);
5126 (error
= vdev_create(rvd
, txg
, B_FALSE
)) == 0 &&
5127 (error
= spa_validate_aux(spa
, nvroot
, txg
,
5128 VDEV_ALLOC_ADD
)) == 0) {
5130 * instantiate the metaslab groups (this will dirty the vdevs)
5131 * we can no longer error exit past this point
5133 for (int c
= 0; error
== 0 && c
< rvd
->vdev_children
; c
++) {
5134 vdev_t
*vd
= rvd
->vdev_child
[c
];
5136 vdev_metaslab_set_size(vd
);
5137 vdev_expand(vd
, txg
);
5141 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5145 spa_deactivate(spa
);
5147 mutex_exit(&spa_namespace_lock
);
5152 * Get the list of spares, if specified.
5154 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
5155 &spares
, &nspares
) == 0) {
5156 VERIFY(nvlist_alloc(&spa
->spa_spares
.sav_config
, NV_UNIQUE_NAME
,
5158 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
5159 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
5160 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5161 spa_load_spares(spa
);
5162 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5163 spa
->spa_spares
.sav_sync
= B_TRUE
;
5167 * Get the list of level 2 cache devices, if specified.
5169 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
5170 &l2cache
, &nl2cache
) == 0) {
5171 VERIFY(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
5172 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
5173 VERIFY(nvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
5174 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
5175 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5176 spa_load_l2cache(spa
);
5177 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5178 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
5181 spa
->spa_is_initializing
= B_TRUE
;
5182 spa
->spa_dsl_pool
= dp
= dsl_pool_create(spa
, zplprops
, dcp
, txg
);
5183 spa
->spa_is_initializing
= B_FALSE
;
5186 * Create DDTs (dedup tables).
5190 spa_update_dspace(spa
);
5192 tx
= dmu_tx_create_assigned(dp
, txg
);
5195 * Create the pool's history object.
5197 if (version
>= SPA_VERSION_ZPOOL_HISTORY
&& !spa
->spa_history
)
5198 spa_history_create_obj(spa
, tx
);
5200 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_CREATE
);
5201 spa_history_log_version(spa
, "create", tx
);
5204 * Create the pool config object.
5206 spa
->spa_config_object
= dmu_object_alloc(spa
->spa_meta_objset
,
5207 DMU_OT_PACKED_NVLIST
, SPA_CONFIG_BLOCKSIZE
,
5208 DMU_OT_PACKED_NVLIST_SIZE
, sizeof (uint64_t), tx
);
5210 if (zap_add(spa
->spa_meta_objset
,
5211 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CONFIG
,
5212 sizeof (uint64_t), 1, &spa
->spa_config_object
, tx
) != 0) {
5213 cmn_err(CE_PANIC
, "failed to add pool config");
5216 if (zap_add(spa
->spa_meta_objset
,
5217 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CREATION_VERSION
,
5218 sizeof (uint64_t), 1, &version
, tx
) != 0) {
5219 cmn_err(CE_PANIC
, "failed to add pool version");
5222 /* Newly created pools with the right version are always deflated. */
5223 if (version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
5224 spa
->spa_deflate
= TRUE
;
5225 if (zap_add(spa
->spa_meta_objset
,
5226 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
5227 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
) != 0) {
5228 cmn_err(CE_PANIC
, "failed to add deflate");
5233 * Create the deferred-free bpobj. Turn off compression
5234 * because sync-to-convergence takes longer if the blocksize
5237 obj
= bpobj_alloc(spa
->spa_meta_objset
, 1 << 14, tx
);
5238 dmu_object_set_compress(spa
->spa_meta_objset
, obj
,
5239 ZIO_COMPRESS_OFF
, tx
);
5240 if (zap_add(spa
->spa_meta_objset
,
5241 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_SYNC_BPOBJ
,
5242 sizeof (uint64_t), 1, &obj
, tx
) != 0) {
5243 cmn_err(CE_PANIC
, "failed to add bpobj");
5245 VERIFY3U(0, ==, bpobj_open(&spa
->spa_deferred_bpobj
,
5246 spa
->spa_meta_objset
, obj
));
5249 * Generate some random noise for salted checksums to operate on.
5251 (void) random_get_pseudo_bytes(spa
->spa_cksum_salt
.zcs_bytes
,
5252 sizeof (spa
->spa_cksum_salt
.zcs_bytes
));
5255 * Set pool properties.
5257 spa
->spa_bootfs
= zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS
);
5258 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
5259 spa
->spa_failmode
= zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE
);
5260 spa
->spa_autoexpand
= zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND
);
5261 spa
->spa_multihost
= zpool_prop_default_numeric(ZPOOL_PROP_MULTIHOST
);
5263 if (props
!= NULL
) {
5264 spa_configfile_set(spa
, props
, B_FALSE
);
5265 spa_sync_props(props
, tx
);
5270 spa
->spa_sync_on
= B_TRUE
;
5272 mmp_thread_start(spa
);
5273 txg_wait_synced(dp
, txg
);
5275 spa_spawn_aux_threads(spa
);
5277 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
);
5280 * Don't count references from objsets that are already closed
5281 * and are making their way through the eviction process.
5283 spa_evicting_os_wait(spa
);
5284 spa
->spa_minref
= zfs_refcount_count(&spa
->spa_refcount
);
5285 spa
->spa_load_state
= SPA_LOAD_NONE
;
5287 mutex_exit(&spa_namespace_lock
);
5293 * Import a non-root pool into the system.
5296 spa_import(char *pool
, nvlist_t
*config
, nvlist_t
*props
, uint64_t flags
)
5299 char *altroot
= NULL
;
5300 spa_load_state_t state
= SPA_LOAD_IMPORT
;
5301 zpool_load_policy_t policy
;
5302 uint64_t mode
= spa_mode_global
;
5303 uint64_t readonly
= B_FALSE
;
5306 nvlist_t
**spares
, **l2cache
;
5307 uint_t nspares
, nl2cache
;
5310 * If a pool with this name exists, return failure.
5312 mutex_enter(&spa_namespace_lock
);
5313 if (spa_lookup(pool
) != NULL
) {
5314 mutex_exit(&spa_namespace_lock
);
5315 return (SET_ERROR(EEXIST
));
5319 * Create and initialize the spa structure.
5321 (void) nvlist_lookup_string(props
,
5322 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
5323 (void) nvlist_lookup_uint64(props
,
5324 zpool_prop_to_name(ZPOOL_PROP_READONLY
), &readonly
);
5327 spa
= spa_add(pool
, config
, altroot
);
5328 spa
->spa_import_flags
= flags
;
5331 * Verbatim import - Take a pool and insert it into the namespace
5332 * as if it had been loaded at boot.
5334 if (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
) {
5336 spa_configfile_set(spa
, props
, B_FALSE
);
5338 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
);
5339 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_IMPORT
);
5340 zfs_dbgmsg("spa_import: verbatim import of %s", pool
);
5341 mutex_exit(&spa_namespace_lock
);
5345 spa_activate(spa
, mode
);
5348 * Don't start async tasks until we know everything is healthy.
5350 spa_async_suspend(spa
);
5352 zpool_get_load_policy(config
, &policy
);
5353 if (policy
.zlp_rewind
& ZPOOL_DO_REWIND
)
5354 state
= SPA_LOAD_RECOVER
;
5356 spa
->spa_config_source
= SPA_CONFIG_SRC_TRYIMPORT
;
5358 if (state
!= SPA_LOAD_RECOVER
) {
5359 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
5360 zfs_dbgmsg("spa_import: importing %s", pool
);
5362 zfs_dbgmsg("spa_import: importing %s, max_txg=%lld "
5363 "(RECOVERY MODE)", pool
, (longlong_t
)policy
.zlp_txg
);
5365 error
= spa_load_best(spa
, state
, policy
.zlp_txg
, policy
.zlp_rewind
);
5368 * Propagate anything learned while loading the pool and pass it
5369 * back to caller (i.e. rewind info, missing devices, etc).
5371 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
5372 spa
->spa_load_info
) == 0);
5374 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5376 * Toss any existing sparelist, as it doesn't have any validity
5377 * anymore, and conflicts with spa_has_spare().
5379 if (spa
->spa_spares
.sav_config
) {
5380 nvlist_free(spa
->spa_spares
.sav_config
);
5381 spa
->spa_spares
.sav_config
= NULL
;
5382 spa_load_spares(spa
);
5384 if (spa
->spa_l2cache
.sav_config
) {
5385 nvlist_free(spa
->spa_l2cache
.sav_config
);
5386 spa
->spa_l2cache
.sav_config
= NULL
;
5387 spa_load_l2cache(spa
);
5390 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
5392 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5395 spa_configfile_set(spa
, props
, B_FALSE
);
5397 if (error
!= 0 || (props
&& spa_writeable(spa
) &&
5398 (error
= spa_prop_set(spa
, props
)))) {
5400 spa_deactivate(spa
);
5402 mutex_exit(&spa_namespace_lock
);
5406 spa_async_resume(spa
);
5409 * Override any spares and level 2 cache devices as specified by
5410 * the user, as these may have correct device names/devids, etc.
5412 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
5413 &spares
, &nspares
) == 0) {
5414 if (spa
->spa_spares
.sav_config
)
5415 VERIFY(nvlist_remove(spa
->spa_spares
.sav_config
,
5416 ZPOOL_CONFIG_SPARES
, DATA_TYPE_NVLIST_ARRAY
) == 0);
5418 VERIFY(nvlist_alloc(&spa
->spa_spares
.sav_config
,
5419 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
5420 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
5421 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
5422 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5423 spa_load_spares(spa
);
5424 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5425 spa
->spa_spares
.sav_sync
= B_TRUE
;
5427 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
5428 &l2cache
, &nl2cache
) == 0) {
5429 if (spa
->spa_l2cache
.sav_config
)
5430 VERIFY(nvlist_remove(spa
->spa_l2cache
.sav_config
,
5431 ZPOOL_CONFIG_L2CACHE
, DATA_TYPE_NVLIST_ARRAY
) == 0);
5433 VERIFY(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
5434 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
5435 VERIFY(nvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
5436 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
5437 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5438 spa_load_l2cache(spa
);
5439 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5440 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
5444 * Check for any removed devices.
5446 if (spa
->spa_autoreplace
) {
5447 spa_aux_check_removed(&spa
->spa_spares
);
5448 spa_aux_check_removed(&spa
->spa_l2cache
);
5451 if (spa_writeable(spa
)) {
5453 * Update the config cache to include the newly-imported pool.
5455 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
5459 * It's possible that the pool was expanded while it was exported.
5460 * We kick off an async task to handle this for us.
5462 spa_async_request(spa
, SPA_ASYNC_AUTOEXPAND
);
5464 spa_history_log_version(spa
, "import", NULL
);
5466 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_IMPORT
);
5468 zvol_create_minors(spa
, pool
, B_TRUE
);
5470 mutex_exit(&spa_namespace_lock
);
5476 spa_tryimport(nvlist_t
*tryconfig
)
5478 nvlist_t
*config
= NULL
;
5479 char *poolname
, *cachefile
;
5483 zpool_load_policy_t policy
;
5485 if (nvlist_lookup_string(tryconfig
, ZPOOL_CONFIG_POOL_NAME
, &poolname
))
5488 if (nvlist_lookup_uint64(tryconfig
, ZPOOL_CONFIG_POOL_STATE
, &state
))
5492 * Create and initialize the spa structure.
5494 mutex_enter(&spa_namespace_lock
);
5495 spa
= spa_add(TRYIMPORT_NAME
, tryconfig
, NULL
);
5496 spa_activate(spa
, FREAD
);
5499 * Rewind pool if a max txg was provided.
5501 zpool_get_load_policy(spa
->spa_config
, &policy
);
5502 if (policy
.zlp_txg
!= UINT64_MAX
) {
5503 spa
->spa_load_max_txg
= policy
.zlp_txg
;
5504 spa
->spa_extreme_rewind
= B_TRUE
;
5505 zfs_dbgmsg("spa_tryimport: importing %s, max_txg=%lld",
5506 poolname
, (longlong_t
)policy
.zlp_txg
);
5508 zfs_dbgmsg("spa_tryimport: importing %s", poolname
);
5511 if (nvlist_lookup_string(tryconfig
, ZPOOL_CONFIG_CACHEFILE
, &cachefile
)
5513 zfs_dbgmsg("spa_tryimport: using cachefile '%s'", cachefile
);
5514 spa
->spa_config_source
= SPA_CONFIG_SRC_CACHEFILE
;
5516 spa
->spa_config_source
= SPA_CONFIG_SRC_SCAN
;
5519 error
= spa_load(spa
, SPA_LOAD_TRYIMPORT
, SPA_IMPORT_EXISTING
);
5522 * If 'tryconfig' was at least parsable, return the current config.
5524 if (spa
->spa_root_vdev
!= NULL
) {
5525 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
5526 VERIFY(nvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
,
5528 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
5530 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_TIMESTAMP
,
5531 spa
->spa_uberblock
.ub_timestamp
) == 0);
5532 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
5533 spa
->spa_load_info
) == 0);
5534 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_ERRATA
,
5535 spa
->spa_errata
) == 0);
5538 * If the bootfs property exists on this pool then we
5539 * copy it out so that external consumers can tell which
5540 * pools are bootable.
5542 if ((!error
|| error
== EEXIST
) && spa
->spa_bootfs
) {
5543 char *tmpname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
5546 * We have to play games with the name since the
5547 * pool was opened as TRYIMPORT_NAME.
5549 if (dsl_dsobj_to_dsname(spa_name(spa
),
5550 spa
->spa_bootfs
, tmpname
) == 0) {
5554 dsname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
5556 cp
= strchr(tmpname
, '/');
5558 (void) strlcpy(dsname
, tmpname
,
5561 (void) snprintf(dsname
, MAXPATHLEN
,
5562 "%s/%s", poolname
, ++cp
);
5564 VERIFY(nvlist_add_string(config
,
5565 ZPOOL_CONFIG_BOOTFS
, dsname
) == 0);
5566 kmem_free(dsname
, MAXPATHLEN
);
5568 kmem_free(tmpname
, MAXPATHLEN
);
5572 * Add the list of hot spares and level 2 cache devices.
5574 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
5575 spa_add_spares(spa
, config
);
5576 spa_add_l2cache(spa
, config
);
5577 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
5581 spa_deactivate(spa
);
5583 mutex_exit(&spa_namespace_lock
);
5589 * Pool export/destroy
5591 * The act of destroying or exporting a pool is very simple. We make sure there
5592 * is no more pending I/O and any references to the pool are gone. Then, we
5593 * update the pool state and sync all the labels to disk, removing the
5594 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
5595 * we don't sync the labels or remove the configuration cache.
5598 spa_export_common(char *pool
, int new_state
, nvlist_t
**oldconfig
,
5599 boolean_t force
, boolean_t hardforce
)
5606 if (!(spa_mode_global
& FWRITE
))
5607 return (SET_ERROR(EROFS
));
5609 mutex_enter(&spa_namespace_lock
);
5610 if ((spa
= spa_lookup(pool
)) == NULL
) {
5611 mutex_exit(&spa_namespace_lock
);
5612 return (SET_ERROR(ENOENT
));
5616 * Put a hold on the pool, drop the namespace lock, stop async tasks,
5617 * reacquire the namespace lock, and see if we can export.
5619 spa_open_ref(spa
, FTAG
);
5620 mutex_exit(&spa_namespace_lock
);
5621 spa_async_suspend(spa
);
5622 if (spa
->spa_zvol_taskq
) {
5623 zvol_remove_minors(spa
, spa_name(spa
), B_TRUE
);
5624 taskq_wait(spa
->spa_zvol_taskq
);
5626 mutex_enter(&spa_namespace_lock
);
5627 spa_close(spa
, FTAG
);
5629 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
)
5632 * The pool will be in core if it's openable, in which case we can
5633 * modify its state. Objsets may be open only because they're dirty,
5634 * so we have to force it to sync before checking spa_refcnt.
5636 if (spa
->spa_sync_on
) {
5637 txg_wait_synced(spa
->spa_dsl_pool
, 0);
5638 spa_evicting_os_wait(spa
);
5642 * A pool cannot be exported or destroyed if there are active
5643 * references. If we are resetting a pool, allow references by
5644 * fault injection handlers.
5646 if (!spa_refcount_zero(spa
) ||
5647 (spa
->spa_inject_ref
!= 0 &&
5648 new_state
!= POOL_STATE_UNINITIALIZED
)) {
5649 spa_async_resume(spa
);
5650 mutex_exit(&spa_namespace_lock
);
5651 return (SET_ERROR(EBUSY
));
5654 if (spa
->spa_sync_on
) {
5656 * A pool cannot be exported if it has an active shared spare.
5657 * This is to prevent other pools stealing the active spare
5658 * from an exported pool. At user's own will, such pool can
5659 * be forcedly exported.
5661 if (!force
&& new_state
== POOL_STATE_EXPORTED
&&
5662 spa_has_active_shared_spare(spa
)) {
5663 spa_async_resume(spa
);
5664 mutex_exit(&spa_namespace_lock
);
5665 return (SET_ERROR(EXDEV
));
5669 * We're about to export or destroy this pool. Make sure
5670 * we stop all initializtion activity here before we
5671 * set the spa_final_txg. This will ensure that all
5672 * dirty data resulting from the initialization is
5673 * committed to disk before we unload the pool.
5675 if (spa
->spa_root_vdev
!= NULL
) {
5676 vdev_initialize_stop_all(spa
->spa_root_vdev
,
5677 VDEV_INITIALIZE_ACTIVE
);
5681 * We want this to be reflected on every label,
5682 * so mark them all dirty. spa_unload() will do the
5683 * final sync that pushes these changes out.
5685 if (new_state
!= POOL_STATE_UNINITIALIZED
&& !hardforce
) {
5686 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5687 spa
->spa_state
= new_state
;
5688 spa
->spa_final_txg
= spa_last_synced_txg(spa
) +
5690 vdev_config_dirty(spa
->spa_root_vdev
);
5691 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5696 if (new_state
== POOL_STATE_DESTROYED
)
5697 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_DESTROY
);
5698 else if (new_state
== POOL_STATE_EXPORTED
)
5699 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_EXPORT
);
5701 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
5703 spa_deactivate(spa
);
5706 if (oldconfig
&& spa
->spa_config
)
5707 VERIFY(nvlist_dup(spa
->spa_config
, oldconfig
, 0) == 0);
5709 if (new_state
!= POOL_STATE_UNINITIALIZED
) {
5711 spa_write_cachefile(spa
, B_TRUE
, B_TRUE
);
5714 mutex_exit(&spa_namespace_lock
);
5720 * Destroy a storage pool.
5723 spa_destroy(char *pool
)
5725 return (spa_export_common(pool
, POOL_STATE_DESTROYED
, NULL
,
5730 * Export a storage pool.
5733 spa_export(char *pool
, nvlist_t
**oldconfig
, boolean_t force
,
5734 boolean_t hardforce
)
5736 return (spa_export_common(pool
, POOL_STATE_EXPORTED
, oldconfig
,
5741 * Similar to spa_export(), this unloads the spa_t without actually removing it
5742 * from the namespace in any way.
5745 spa_reset(char *pool
)
5747 return (spa_export_common(pool
, POOL_STATE_UNINITIALIZED
, NULL
,
5752 * ==========================================================================
5753 * Device manipulation
5754 * ==========================================================================
5758 * Add a device to a storage pool.
5761 spa_vdev_add(spa_t
*spa
, nvlist_t
*nvroot
)
5765 vdev_t
*rvd
= spa
->spa_root_vdev
;
5767 nvlist_t
**spares
, **l2cache
;
5768 uint_t nspares
, nl2cache
;
5770 ASSERT(spa_writeable(spa
));
5772 txg
= spa_vdev_enter(spa
);
5774 if ((error
= spa_config_parse(spa
, &vd
, nvroot
, NULL
, 0,
5775 VDEV_ALLOC_ADD
)) != 0)
5776 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5778 spa
->spa_pending_vdev
= vd
; /* spa_vdev_exit() will clear this */
5780 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
, &spares
,
5784 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
, &l2cache
,
5788 if (vd
->vdev_children
== 0 && nspares
== 0 && nl2cache
== 0)
5789 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
5791 if (vd
->vdev_children
!= 0 &&
5792 (error
= vdev_create(vd
, txg
, B_FALSE
)) != 0)
5793 return (spa_vdev_exit(spa
, vd
, txg
, error
));
5796 * We must validate the spares and l2cache devices after checking the
5797 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
5799 if ((error
= spa_validate_aux(spa
, nvroot
, txg
, VDEV_ALLOC_ADD
)) != 0)
5800 return (spa_vdev_exit(spa
, vd
, txg
, error
));
5803 * If we are in the middle of a device removal, we can only add
5804 * devices which match the existing devices in the pool.
5805 * If we are in the middle of a removal, or have some indirect
5806 * vdevs, we can not add raidz toplevels.
5808 if (spa
->spa_vdev_removal
!= NULL
||
5809 spa
->spa_removing_phys
.sr_prev_indirect_vdev
!= -1) {
5810 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
5811 tvd
= vd
->vdev_child
[c
];
5812 if (spa
->spa_vdev_removal
!= NULL
&&
5813 tvd
->vdev_ashift
!= spa
->spa_max_ashift
) {
5814 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
5816 /* Fail if top level vdev is raidz */
5817 if (tvd
->vdev_ops
== &vdev_raidz_ops
) {
5818 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
5821 * Need the top level mirror to be
5822 * a mirror of leaf vdevs only
5824 if (tvd
->vdev_ops
== &vdev_mirror_ops
) {
5825 for (uint64_t cid
= 0;
5826 cid
< tvd
->vdev_children
; cid
++) {
5827 vdev_t
*cvd
= tvd
->vdev_child
[cid
];
5828 if (!cvd
->vdev_ops
->vdev_op_leaf
) {
5829 return (spa_vdev_exit(spa
, vd
,
5837 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
5840 * Set the vdev id to the first hole, if one exists.
5842 for (id
= 0; id
< rvd
->vdev_children
; id
++) {
5843 if (rvd
->vdev_child
[id
]->vdev_ishole
) {
5844 vdev_free(rvd
->vdev_child
[id
]);
5848 tvd
= vd
->vdev_child
[c
];
5849 vdev_remove_child(vd
, tvd
);
5851 vdev_add_child(rvd
, tvd
);
5852 vdev_config_dirty(tvd
);
5856 spa_set_aux_vdevs(&spa
->spa_spares
, spares
, nspares
,
5857 ZPOOL_CONFIG_SPARES
);
5858 spa_load_spares(spa
);
5859 spa
->spa_spares
.sav_sync
= B_TRUE
;
5862 if (nl2cache
!= 0) {
5863 spa_set_aux_vdevs(&spa
->spa_l2cache
, l2cache
, nl2cache
,
5864 ZPOOL_CONFIG_L2CACHE
);
5865 spa_load_l2cache(spa
);
5866 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
5870 * We have to be careful when adding new vdevs to an existing pool.
5871 * If other threads start allocating from these vdevs before we
5872 * sync the config cache, and we lose power, then upon reboot we may
5873 * fail to open the pool because there are DVAs that the config cache
5874 * can't translate. Therefore, we first add the vdevs without
5875 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
5876 * and then let spa_config_update() initialize the new metaslabs.
5878 * spa_load() checks for added-but-not-initialized vdevs, so that
5879 * if we lose power at any point in this sequence, the remaining
5880 * steps will be completed the next time we load the pool.
5882 (void) spa_vdev_exit(spa
, vd
, txg
, 0);
5884 mutex_enter(&spa_namespace_lock
);
5885 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
5886 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_VDEV_ADD
);
5887 mutex_exit(&spa_namespace_lock
);
5893 * Attach a device to a mirror. The arguments are the path to any device
5894 * in the mirror, and the nvroot for the new device. If the path specifies
5895 * a device that is not mirrored, we automatically insert the mirror vdev.
5897 * If 'replacing' is specified, the new device is intended to replace the
5898 * existing device; in this case the two devices are made into their own
5899 * mirror using the 'replacing' vdev, which is functionally identical to
5900 * the mirror vdev (it actually reuses all the same ops) but has a few
5901 * extra rules: you can't attach to it after it's been created, and upon
5902 * completion of resilvering, the first disk (the one being replaced)
5903 * is automatically detached.
5906 spa_vdev_attach(spa_t
*spa
, uint64_t guid
, nvlist_t
*nvroot
, int replacing
)
5908 uint64_t txg
, dtl_max_txg
;
5909 ASSERTV(vdev_t
*rvd
= spa
->spa_root_vdev
);
5910 vdev_t
*oldvd
, *newvd
, *newrootvd
, *pvd
, *tvd
;
5912 char *oldvdpath
, *newvdpath
;
5916 ASSERT(spa_writeable(spa
));
5918 txg
= spa_vdev_enter(spa
);
5920 oldvd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
5922 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
5923 if (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
)) {
5924 error
= (spa_has_checkpoint(spa
)) ?
5925 ZFS_ERR_CHECKPOINT_EXISTS
: ZFS_ERR_DISCARDING_CHECKPOINT
;
5926 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5929 if (spa
->spa_vdev_removal
!= NULL
)
5930 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
5933 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
5935 if (!oldvd
->vdev_ops
->vdev_op_leaf
)
5936 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
5938 pvd
= oldvd
->vdev_parent
;
5940 if ((error
= spa_config_parse(spa
, &newrootvd
, nvroot
, NULL
, 0,
5941 VDEV_ALLOC_ATTACH
)) != 0)
5942 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
5944 if (newrootvd
->vdev_children
!= 1)
5945 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
5947 newvd
= newrootvd
->vdev_child
[0];
5949 if (!newvd
->vdev_ops
->vdev_op_leaf
)
5950 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
5952 if ((error
= vdev_create(newrootvd
, txg
, replacing
)) != 0)
5953 return (spa_vdev_exit(spa
, newrootvd
, txg
, error
));
5956 * Spares can't replace logs
5958 if (oldvd
->vdev_top
->vdev_islog
&& newvd
->vdev_isspare
)
5959 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
5963 * For attach, the only allowable parent is a mirror or the root
5966 if (pvd
->vdev_ops
!= &vdev_mirror_ops
&&
5967 pvd
->vdev_ops
!= &vdev_root_ops
)
5968 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
5970 pvops
= &vdev_mirror_ops
;
5973 * Active hot spares can only be replaced by inactive hot
5976 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
5977 oldvd
->vdev_isspare
&&
5978 !spa_has_spare(spa
, newvd
->vdev_guid
))
5979 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
5982 * If the source is a hot spare, and the parent isn't already a
5983 * spare, then we want to create a new hot spare. Otherwise, we
5984 * want to create a replacing vdev. The user is not allowed to
5985 * attach to a spared vdev child unless the 'isspare' state is
5986 * the same (spare replaces spare, non-spare replaces
5989 if (pvd
->vdev_ops
== &vdev_replacing_ops
&&
5990 spa_version(spa
) < SPA_VERSION_MULTI_REPLACE
) {
5991 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
5992 } else if (pvd
->vdev_ops
== &vdev_spare_ops
&&
5993 newvd
->vdev_isspare
!= oldvd
->vdev_isspare
) {
5994 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
5997 if (newvd
->vdev_isspare
)
5998 pvops
= &vdev_spare_ops
;
6000 pvops
= &vdev_replacing_ops
;
6004 * Make sure the new device is big enough.
6006 if (newvd
->vdev_asize
< vdev_get_min_asize(oldvd
))
6007 return (spa_vdev_exit(spa
, newrootvd
, txg
, EOVERFLOW
));
6010 * The new device cannot have a higher alignment requirement
6011 * than the top-level vdev.
6013 if (newvd
->vdev_ashift
> oldvd
->vdev_top
->vdev_ashift
)
6014 return (spa_vdev_exit(spa
, newrootvd
, txg
, EDOM
));
6017 * If this is an in-place replacement, update oldvd's path and devid
6018 * to make it distinguishable from newvd, and unopenable from now on.
6020 if (strcmp(oldvd
->vdev_path
, newvd
->vdev_path
) == 0) {
6021 spa_strfree(oldvd
->vdev_path
);
6022 oldvd
->vdev_path
= kmem_alloc(strlen(newvd
->vdev_path
) + 5,
6024 (void) sprintf(oldvd
->vdev_path
, "%s/%s",
6025 newvd
->vdev_path
, "old");
6026 if (oldvd
->vdev_devid
!= NULL
) {
6027 spa_strfree(oldvd
->vdev_devid
);
6028 oldvd
->vdev_devid
= NULL
;
6032 /* mark the device being resilvered */
6033 newvd
->vdev_resilver_txg
= txg
;
6036 * If the parent is not a mirror, or if we're replacing, insert the new
6037 * mirror/replacing/spare vdev above oldvd.
6039 if (pvd
->vdev_ops
!= pvops
)
6040 pvd
= vdev_add_parent(oldvd
, pvops
);
6042 ASSERT(pvd
->vdev_top
->vdev_parent
== rvd
);
6043 ASSERT(pvd
->vdev_ops
== pvops
);
6044 ASSERT(oldvd
->vdev_parent
== pvd
);
6047 * Extract the new device from its root and add it to pvd.
6049 vdev_remove_child(newrootvd
, newvd
);
6050 newvd
->vdev_id
= pvd
->vdev_children
;
6051 newvd
->vdev_crtxg
= oldvd
->vdev_crtxg
;
6052 vdev_add_child(pvd
, newvd
);
6055 * Reevaluate the parent vdev state.
6057 vdev_propagate_state(pvd
);
6059 tvd
= newvd
->vdev_top
;
6060 ASSERT(pvd
->vdev_top
== tvd
);
6061 ASSERT(tvd
->vdev_parent
== rvd
);
6063 vdev_config_dirty(tvd
);
6066 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
6067 * for any dmu_sync-ed blocks. It will propagate upward when
6068 * spa_vdev_exit() calls vdev_dtl_reassess().
6070 dtl_max_txg
= txg
+ TXG_CONCURRENT_STATES
;
6072 vdev_dtl_dirty(newvd
, DTL_MISSING
, TXG_INITIAL
,
6073 dtl_max_txg
- TXG_INITIAL
);
6075 if (newvd
->vdev_isspare
) {
6076 spa_spare_activate(newvd
);
6077 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_VDEV_SPARE
);
6080 oldvdpath
= spa_strdup(oldvd
->vdev_path
);
6081 newvdpath
= spa_strdup(newvd
->vdev_path
);
6082 newvd_isspare
= newvd
->vdev_isspare
;
6085 * Mark newvd's DTL dirty in this txg.
6087 vdev_dirty(tvd
, VDD_DTL
, newvd
, txg
);
6090 * Schedule the resilver to restart in the future. We do this to
6091 * ensure that dmu_sync-ed blocks have been stitched into the
6092 * respective datasets. We do not do this if resilvers have been
6095 if (dsl_scan_resilvering(spa_get_dsl(spa
)) &&
6096 spa_feature_is_enabled(spa
, SPA_FEATURE_RESILVER_DEFER
))
6097 vdev_set_deferred_resilver(spa
, newvd
);
6099 dsl_resilver_restart(spa
->spa_dsl_pool
, dtl_max_txg
);
6101 if (spa
->spa_bootfs
)
6102 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_BOOTFS_VDEV_ATTACH
);
6104 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_VDEV_ATTACH
);
6109 (void) spa_vdev_exit(spa
, newrootvd
, dtl_max_txg
, 0);
6111 spa_history_log_internal(spa
, "vdev attach", NULL
,
6112 "%s vdev=%s %s vdev=%s",
6113 replacing
&& newvd_isspare
? "spare in" :
6114 replacing
? "replace" : "attach", newvdpath
,
6115 replacing
? "for" : "to", oldvdpath
);
6117 spa_strfree(oldvdpath
);
6118 spa_strfree(newvdpath
);
6124 * Detach a device from a mirror or replacing vdev.
6126 * If 'replace_done' is specified, only detach if the parent
6127 * is a replacing vdev.
6130 spa_vdev_detach(spa_t
*spa
, uint64_t guid
, uint64_t pguid
, int replace_done
)
6134 ASSERTV(vdev_t
*rvd
= spa
->spa_root_vdev
);
6135 vdev_t
*vd
, *pvd
, *cvd
, *tvd
;
6136 boolean_t unspare
= B_FALSE
;
6137 uint64_t unspare_guid
= 0;
6140 ASSERT(spa_writeable(spa
));
6142 txg
= spa_vdev_enter(spa
);
6144 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
6147 * Besides being called directly from the userland through the
6148 * ioctl interface, spa_vdev_detach() can be potentially called
6149 * at the end of spa_vdev_resilver_done().
6151 * In the regular case, when we have a checkpoint this shouldn't
6152 * happen as we never empty the DTLs of a vdev during the scrub
6153 * [see comment in dsl_scan_done()]. Thus spa_vdev_resilvering_done()
6154 * should never get here when we have a checkpoint.
6156 * That said, even in a case when we checkpoint the pool exactly
6157 * as spa_vdev_resilver_done() calls this function everything
6158 * should be fine as the resilver will return right away.
6160 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
6161 if (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
)) {
6162 error
= (spa_has_checkpoint(spa
)) ?
6163 ZFS_ERR_CHECKPOINT_EXISTS
: ZFS_ERR_DISCARDING_CHECKPOINT
;
6164 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
6168 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
6170 if (!vd
->vdev_ops
->vdev_op_leaf
)
6171 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
6173 pvd
= vd
->vdev_parent
;
6176 * If the parent/child relationship is not as expected, don't do it.
6177 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
6178 * vdev that's replacing B with C. The user's intent in replacing
6179 * is to go from M(A,B) to M(A,C). If the user decides to cancel
6180 * the replace by detaching C, the expected behavior is to end up
6181 * M(A,B). But suppose that right after deciding to detach C,
6182 * the replacement of B completes. We would have M(A,C), and then
6183 * ask to detach C, which would leave us with just A -- not what
6184 * the user wanted. To prevent this, we make sure that the
6185 * parent/child relationship hasn't changed -- in this example,
6186 * that C's parent is still the replacing vdev R.
6188 if (pvd
->vdev_guid
!= pguid
&& pguid
!= 0)
6189 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
6192 * Only 'replacing' or 'spare' vdevs can be replaced.
6194 if (replace_done
&& pvd
->vdev_ops
!= &vdev_replacing_ops
&&
6195 pvd
->vdev_ops
!= &vdev_spare_ops
)
6196 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
6198 ASSERT(pvd
->vdev_ops
!= &vdev_spare_ops
||
6199 spa_version(spa
) >= SPA_VERSION_SPARES
);
6202 * Only mirror, replacing, and spare vdevs support detach.
6204 if (pvd
->vdev_ops
!= &vdev_replacing_ops
&&
6205 pvd
->vdev_ops
!= &vdev_mirror_ops
&&
6206 pvd
->vdev_ops
!= &vdev_spare_ops
)
6207 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
6210 * If this device has the only valid copy of some data,
6211 * we cannot safely detach it.
6213 if (vdev_dtl_required(vd
))
6214 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
6216 ASSERT(pvd
->vdev_children
>= 2);
6219 * If we are detaching the second disk from a replacing vdev, then
6220 * check to see if we changed the original vdev's path to have "/old"
6221 * at the end in spa_vdev_attach(). If so, undo that change now.
6223 if (pvd
->vdev_ops
== &vdev_replacing_ops
&& vd
->vdev_id
> 0 &&
6224 vd
->vdev_path
!= NULL
) {
6225 size_t len
= strlen(vd
->vdev_path
);
6227 for (int c
= 0; c
< pvd
->vdev_children
; c
++) {
6228 cvd
= pvd
->vdev_child
[c
];
6230 if (cvd
== vd
|| cvd
->vdev_path
== NULL
)
6233 if (strncmp(cvd
->vdev_path
, vd
->vdev_path
, len
) == 0 &&
6234 strcmp(cvd
->vdev_path
+ len
, "/old") == 0) {
6235 spa_strfree(cvd
->vdev_path
);
6236 cvd
->vdev_path
= spa_strdup(vd
->vdev_path
);
6243 * If we are detaching the original disk from a spare, then it implies
6244 * that the spare should become a real disk, and be removed from the
6245 * active spare list for the pool.
6247 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
6249 pvd
->vdev_child
[pvd
->vdev_children
- 1]->vdev_isspare
)
6253 * Erase the disk labels so the disk can be used for other things.
6254 * This must be done after all other error cases are handled,
6255 * but before we disembowel vd (so we can still do I/O to it).
6256 * But if we can't do it, don't treat the error as fatal --
6257 * it may be that the unwritability of the disk is the reason
6258 * it's being detached!
6260 error
= vdev_label_init(vd
, 0, VDEV_LABEL_REMOVE
);
6263 * Remove vd from its parent and compact the parent's children.
6265 vdev_remove_child(pvd
, vd
);
6266 vdev_compact_children(pvd
);
6269 * Remember one of the remaining children so we can get tvd below.
6271 cvd
= pvd
->vdev_child
[pvd
->vdev_children
- 1];
6274 * If we need to remove the remaining child from the list of hot spares,
6275 * do it now, marking the vdev as no longer a spare in the process.
6276 * We must do this before vdev_remove_parent(), because that can
6277 * change the GUID if it creates a new toplevel GUID. For a similar
6278 * reason, we must remove the spare now, in the same txg as the detach;
6279 * otherwise someone could attach a new sibling, change the GUID, and
6280 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
6283 ASSERT(cvd
->vdev_isspare
);
6284 spa_spare_remove(cvd
);
6285 unspare_guid
= cvd
->vdev_guid
;
6286 (void) spa_vdev_remove(spa
, unspare_guid
, B_TRUE
);
6287 cvd
->vdev_unspare
= B_TRUE
;
6291 * If the parent mirror/replacing vdev only has one child,
6292 * the parent is no longer needed. Remove it from the tree.
6294 if (pvd
->vdev_children
== 1) {
6295 if (pvd
->vdev_ops
== &vdev_spare_ops
)
6296 cvd
->vdev_unspare
= B_FALSE
;
6297 vdev_remove_parent(cvd
);
6302 * We don't set tvd until now because the parent we just removed
6303 * may have been the previous top-level vdev.
6305 tvd
= cvd
->vdev_top
;
6306 ASSERT(tvd
->vdev_parent
== rvd
);
6309 * Reevaluate the parent vdev state.
6311 vdev_propagate_state(cvd
);
6314 * If the 'autoexpand' property is set on the pool then automatically
6315 * try to expand the size of the pool. For example if the device we
6316 * just detached was smaller than the others, it may be possible to
6317 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
6318 * first so that we can obtain the updated sizes of the leaf vdevs.
6320 if (spa
->spa_autoexpand
) {
6322 vdev_expand(tvd
, txg
);
6325 vdev_config_dirty(tvd
);
6328 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
6329 * vd->vdev_detached is set and free vd's DTL object in syncing context.
6330 * But first make sure we're not on any *other* txg's DTL list, to
6331 * prevent vd from being accessed after it's freed.
6333 vdpath
= spa_strdup(vd
->vdev_path
? vd
->vdev_path
: "none");
6334 for (int t
= 0; t
< TXG_SIZE
; t
++)
6335 (void) txg_list_remove_this(&tvd
->vdev_dtl_list
, vd
, t
);
6336 vd
->vdev_detached
= B_TRUE
;
6337 vdev_dirty(tvd
, VDD_DTL
, vd
, txg
);
6339 spa_event_notify(spa
, vd
, NULL
, ESC_ZFS_VDEV_REMOVE
);
6341 /* hang on to the spa before we release the lock */
6342 spa_open_ref(spa
, FTAG
);
6344 error
= spa_vdev_exit(spa
, vd
, txg
, 0);
6346 spa_history_log_internal(spa
, "detach", NULL
,
6348 spa_strfree(vdpath
);
6351 * If this was the removal of the original device in a hot spare vdev,
6352 * then we want to go through and remove the device from the hot spare
6353 * list of every other pool.
6356 spa_t
*altspa
= NULL
;
6358 mutex_enter(&spa_namespace_lock
);
6359 while ((altspa
= spa_next(altspa
)) != NULL
) {
6360 if (altspa
->spa_state
!= POOL_STATE_ACTIVE
||
6364 spa_open_ref(altspa
, FTAG
);
6365 mutex_exit(&spa_namespace_lock
);
6366 (void) spa_vdev_remove(altspa
, unspare_guid
, B_TRUE
);
6367 mutex_enter(&spa_namespace_lock
);
6368 spa_close(altspa
, FTAG
);
6370 mutex_exit(&spa_namespace_lock
);
6372 /* search the rest of the vdevs for spares to remove */
6373 spa_vdev_resilver_done(spa
);
6376 /* all done with the spa; OK to release */
6377 mutex_enter(&spa_namespace_lock
);
6378 spa_close(spa
, FTAG
);
6379 mutex_exit(&spa_namespace_lock
);
6385 spa_vdev_initialize_impl(spa_t
*spa
, uint64_t guid
, uint64_t cmd_type
,
6388 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
6390 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
6392 /* Look up vdev and ensure it's a leaf. */
6393 vdev_t
*vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
6394 if (vd
== NULL
|| vd
->vdev_detached
) {
6395 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
6396 return (SET_ERROR(ENODEV
));
6397 } else if (!vd
->vdev_ops
->vdev_op_leaf
|| !vdev_is_concrete(vd
)) {
6398 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
6399 return (SET_ERROR(EINVAL
));
6400 } else if (!vdev_writeable(vd
)) {
6401 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
6402 return (SET_ERROR(EROFS
));
6404 mutex_enter(&vd
->vdev_initialize_lock
);
6405 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
6408 * When we activate an initialize action we check to see
6409 * if the vdev_initialize_thread is NULL. We do this instead
6410 * of using the vdev_initialize_state since there might be
6411 * a previous initialization process which has completed but
6412 * the thread is not exited.
6414 if (cmd_type
== POOL_INITIALIZE_DO
&&
6415 (vd
->vdev_initialize_thread
!= NULL
||
6416 vd
->vdev_top
->vdev_removing
)) {
6417 mutex_exit(&vd
->vdev_initialize_lock
);
6418 return (SET_ERROR(EBUSY
));
6419 } else if (cmd_type
== POOL_INITIALIZE_CANCEL
&&
6420 (vd
->vdev_initialize_state
!= VDEV_INITIALIZE_ACTIVE
&&
6421 vd
->vdev_initialize_state
!= VDEV_INITIALIZE_SUSPENDED
)) {
6422 mutex_exit(&vd
->vdev_initialize_lock
);
6423 return (SET_ERROR(ESRCH
));
6424 } else if (cmd_type
== POOL_INITIALIZE_SUSPEND
&&
6425 vd
->vdev_initialize_state
!= VDEV_INITIALIZE_ACTIVE
) {
6426 mutex_exit(&vd
->vdev_initialize_lock
);
6427 return (SET_ERROR(ESRCH
));
6431 case POOL_INITIALIZE_DO
:
6432 vdev_initialize(vd
);
6434 case POOL_INITIALIZE_CANCEL
:
6435 vdev_initialize_stop(vd
, VDEV_INITIALIZE_CANCELED
, vd_list
);
6437 case POOL_INITIALIZE_SUSPEND
:
6438 vdev_initialize_stop(vd
, VDEV_INITIALIZE_SUSPENDED
, vd_list
);
6441 panic("invalid cmd_type %llu", (unsigned long long)cmd_type
);
6443 mutex_exit(&vd
->vdev_initialize_lock
);
6449 spa_vdev_initialize(spa_t
*spa
, nvlist_t
*nv
, uint64_t cmd_type
,
6450 nvlist_t
*vdev_errlist
)
6452 int total_errors
= 0;
6455 list_create(&vd_list
, sizeof (vdev_t
),
6456 offsetof(vdev_t
, vdev_initialize_node
));
6459 * We hold the namespace lock through the whole function
6460 * to prevent any changes to the pool while we're starting or
6461 * stopping initialization. The config and state locks are held so that
6462 * we can properly assess the vdev state before we commit to
6463 * the initializing operation.
6465 mutex_enter(&spa_namespace_lock
);
6467 for (nvpair_t
*pair
= nvlist_next_nvpair(nv
, NULL
);
6468 pair
!= NULL
; pair
= nvlist_next_nvpair(nv
, pair
)) {
6469 uint64_t vdev_guid
= fnvpair_value_uint64(pair
);
6471 int error
= spa_vdev_initialize_impl(spa
, vdev_guid
, cmd_type
,
6474 char guid_as_str
[MAXNAMELEN
];
6476 (void) snprintf(guid_as_str
, sizeof (guid_as_str
),
6477 "%llu", (unsigned long long)vdev_guid
);
6478 fnvlist_add_int64(vdev_errlist
, guid_as_str
, error
);
6483 /* Wait for all initialize threads to stop. */
6484 vdev_initialize_stop_wait(spa
, &vd_list
);
6486 /* Sync out the initializing state */
6487 txg_wait_synced(spa
->spa_dsl_pool
, 0);
6488 mutex_exit(&spa_namespace_lock
);
6490 list_destroy(&vd_list
);
6492 return (total_errors
);
6496 * Split a set of devices from their mirrors, and create a new pool from them.
6499 spa_vdev_split_mirror(spa_t
*spa
, char *newname
, nvlist_t
*config
,
6500 nvlist_t
*props
, boolean_t exp
)
6503 uint64_t txg
, *glist
;
6505 uint_t c
, children
, lastlog
;
6506 nvlist_t
**child
, *nvl
, *tmp
;
6508 char *altroot
= NULL
;
6509 vdev_t
*rvd
, **vml
= NULL
; /* vdev modify list */
6510 boolean_t activate_slog
;
6512 ASSERT(spa_writeable(spa
));
6514 txg
= spa_vdev_enter(spa
);
6516 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
6517 if (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
)) {
6518 error
= (spa_has_checkpoint(spa
)) ?
6519 ZFS_ERR_CHECKPOINT_EXISTS
: ZFS_ERR_DISCARDING_CHECKPOINT
;
6520 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
6523 /* clear the log and flush everything up to now */
6524 activate_slog
= spa_passivate_log(spa
);
6525 (void) spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
6526 error
= spa_reset_logs(spa
);
6527 txg
= spa_vdev_config_enter(spa
);
6530 spa_activate_log(spa
);
6533 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
6535 /* check new spa name before going any further */
6536 if (spa_lookup(newname
) != NULL
)
6537 return (spa_vdev_exit(spa
, NULL
, txg
, EEXIST
));
6540 * scan through all the children to ensure they're all mirrors
6542 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvl
) != 0 ||
6543 nvlist_lookup_nvlist_array(nvl
, ZPOOL_CONFIG_CHILDREN
, &child
,
6545 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
6547 /* first, check to ensure we've got the right child count */
6548 rvd
= spa
->spa_root_vdev
;
6550 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
6551 vdev_t
*vd
= rvd
->vdev_child
[c
];
6553 /* don't count the holes & logs as children */
6554 if (vd
->vdev_islog
|| !vdev_is_concrete(vd
)) {
6562 if (children
!= (lastlog
!= 0 ? lastlog
: rvd
->vdev_children
))
6563 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
6565 /* next, ensure no spare or cache devices are part of the split */
6566 if (nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_SPARES
, &tmp
) == 0 ||
6567 nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_L2CACHE
, &tmp
) == 0)
6568 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
6570 vml
= kmem_zalloc(children
* sizeof (vdev_t
*), KM_SLEEP
);
6571 glist
= kmem_zalloc(children
* sizeof (uint64_t), KM_SLEEP
);
6573 /* then, loop over each vdev and validate it */
6574 for (c
= 0; c
< children
; c
++) {
6575 uint64_t is_hole
= 0;
6577 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
6581 if (spa
->spa_root_vdev
->vdev_child
[c
]->vdev_ishole
||
6582 spa
->spa_root_vdev
->vdev_child
[c
]->vdev_islog
) {
6585 error
= SET_ERROR(EINVAL
);
6590 /* which disk is going to be split? */
6591 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_GUID
,
6593 error
= SET_ERROR(EINVAL
);
6597 /* look it up in the spa */
6598 vml
[c
] = spa_lookup_by_guid(spa
, glist
[c
], B_FALSE
);
6599 if (vml
[c
] == NULL
) {
6600 error
= SET_ERROR(ENODEV
);
6604 /* make sure there's nothing stopping the split */
6605 if (vml
[c
]->vdev_parent
->vdev_ops
!= &vdev_mirror_ops
||
6606 vml
[c
]->vdev_islog
||
6607 !vdev_is_concrete(vml
[c
]) ||
6608 vml
[c
]->vdev_isspare
||
6609 vml
[c
]->vdev_isl2cache
||
6610 !vdev_writeable(vml
[c
]) ||
6611 vml
[c
]->vdev_children
!= 0 ||
6612 vml
[c
]->vdev_state
!= VDEV_STATE_HEALTHY
||
6613 c
!= spa
->spa_root_vdev
->vdev_child
[c
]->vdev_id
) {
6614 error
= SET_ERROR(EINVAL
);
6618 if (vdev_dtl_required(vml
[c
]) ||
6619 vdev_resilver_needed(vml
[c
], NULL
, NULL
)) {
6620 error
= SET_ERROR(EBUSY
);
6624 /* we need certain info from the top level */
6625 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_ARRAY
,
6626 vml
[c
]->vdev_top
->vdev_ms_array
) == 0);
6627 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_SHIFT
,
6628 vml
[c
]->vdev_top
->vdev_ms_shift
) == 0);
6629 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASIZE
,
6630 vml
[c
]->vdev_top
->vdev_asize
) == 0);
6631 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASHIFT
,
6632 vml
[c
]->vdev_top
->vdev_ashift
) == 0);
6634 /* transfer per-vdev ZAPs */
6635 ASSERT3U(vml
[c
]->vdev_leaf_zap
, !=, 0);
6636 VERIFY0(nvlist_add_uint64(child
[c
],
6637 ZPOOL_CONFIG_VDEV_LEAF_ZAP
, vml
[c
]->vdev_leaf_zap
));
6639 ASSERT3U(vml
[c
]->vdev_top
->vdev_top_zap
, !=, 0);
6640 VERIFY0(nvlist_add_uint64(child
[c
],
6641 ZPOOL_CONFIG_VDEV_TOP_ZAP
,
6642 vml
[c
]->vdev_parent
->vdev_top_zap
));
6646 kmem_free(vml
, children
* sizeof (vdev_t
*));
6647 kmem_free(glist
, children
* sizeof (uint64_t));
6648 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
6651 /* stop writers from using the disks */
6652 for (c
= 0; c
< children
; c
++) {
6654 vml
[c
]->vdev_offline
= B_TRUE
;
6656 vdev_reopen(spa
->spa_root_vdev
);
6659 * Temporarily record the splitting vdevs in the spa config. This
6660 * will disappear once the config is regenerated.
6662 VERIFY(nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
6663 VERIFY(nvlist_add_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
6664 glist
, children
) == 0);
6665 kmem_free(glist
, children
* sizeof (uint64_t));
6667 mutex_enter(&spa
->spa_props_lock
);
6668 VERIFY(nvlist_add_nvlist(spa
->spa_config
, ZPOOL_CONFIG_SPLIT
,
6670 mutex_exit(&spa
->spa_props_lock
);
6671 spa
->spa_config_splitting
= nvl
;
6672 vdev_config_dirty(spa
->spa_root_vdev
);
6674 /* configure and create the new pool */
6675 VERIFY(nvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
, newname
) == 0);
6676 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
6677 exp
? POOL_STATE_EXPORTED
: POOL_STATE_ACTIVE
) == 0);
6678 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
6679 spa_version(spa
)) == 0);
6680 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
6681 spa
->spa_config_txg
) == 0);
6682 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
6683 spa_generate_guid(NULL
)) == 0);
6684 VERIFY0(nvlist_add_boolean(config
, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
));
6685 (void) nvlist_lookup_string(props
,
6686 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
6688 /* add the new pool to the namespace */
6689 newspa
= spa_add(newname
, config
, altroot
);
6690 newspa
->spa_avz_action
= AVZ_ACTION_REBUILD
;
6691 newspa
->spa_config_txg
= spa
->spa_config_txg
;
6692 spa_set_log_state(newspa
, SPA_LOG_CLEAR
);
6694 /* release the spa config lock, retaining the namespace lock */
6695 spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
6697 if (zio_injection_enabled
)
6698 zio_handle_panic_injection(spa
, FTAG
, 1);
6700 spa_activate(newspa
, spa_mode_global
);
6701 spa_async_suspend(newspa
);
6704 * Temporarily stop the initializing activity. We set the state to
6705 * ACTIVE so that we know to resume the initializing once the split
6709 list_create(&vd_list
, sizeof (vdev_t
),
6710 offsetof(vdev_t
, vdev_initialize_node
));
6712 for (c
= 0; c
< children
; c
++) {
6713 if (vml
[c
] != NULL
) {
6714 mutex_enter(&vml
[c
]->vdev_initialize_lock
);
6715 vdev_initialize_stop(vml
[c
], VDEV_INITIALIZE_ACTIVE
,
6717 mutex_exit(&vml
[c
]->vdev_initialize_lock
);
6720 vdev_initialize_stop_wait(spa
, &vd_list
);
6721 list_destroy(&vd_list
);
6723 newspa
->spa_config_source
= SPA_CONFIG_SRC_SPLIT
;
6725 /* create the new pool from the disks of the original pool */
6726 error
= spa_load(newspa
, SPA_LOAD_IMPORT
, SPA_IMPORT_ASSEMBLE
);
6730 /* if that worked, generate a real config for the new pool */
6731 if (newspa
->spa_root_vdev
!= NULL
) {
6732 VERIFY(nvlist_alloc(&newspa
->spa_config_splitting
,
6733 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
6734 VERIFY(nvlist_add_uint64(newspa
->spa_config_splitting
,
6735 ZPOOL_CONFIG_SPLIT_GUID
, spa_guid(spa
)) == 0);
6736 spa_config_set(newspa
, spa_config_generate(newspa
, NULL
, -1ULL,
6741 if (props
!= NULL
) {
6742 spa_configfile_set(newspa
, props
, B_FALSE
);
6743 error
= spa_prop_set(newspa
, props
);
6748 /* flush everything */
6749 txg
= spa_vdev_config_enter(newspa
);
6750 vdev_config_dirty(newspa
->spa_root_vdev
);
6751 (void) spa_vdev_config_exit(newspa
, NULL
, txg
, 0, FTAG
);
6753 if (zio_injection_enabled
)
6754 zio_handle_panic_injection(spa
, FTAG
, 2);
6756 spa_async_resume(newspa
);
6758 /* finally, update the original pool's config */
6759 txg
= spa_vdev_config_enter(spa
);
6760 tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
6761 error
= dmu_tx_assign(tx
, TXG_WAIT
);
6764 for (c
= 0; c
< children
; c
++) {
6765 if (vml
[c
] != NULL
) {
6768 spa_history_log_internal(spa
, "detach", tx
,
6769 "vdev=%s", vml
[c
]->vdev_path
);
6774 spa
->spa_avz_action
= AVZ_ACTION_REBUILD
;
6775 vdev_config_dirty(spa
->spa_root_vdev
);
6776 spa
->spa_config_splitting
= NULL
;
6780 (void) spa_vdev_exit(spa
, NULL
, txg
, 0);
6782 if (zio_injection_enabled
)
6783 zio_handle_panic_injection(spa
, FTAG
, 3);
6785 /* split is complete; log a history record */
6786 spa_history_log_internal(newspa
, "split", NULL
,
6787 "from pool %s", spa_name(spa
));
6789 kmem_free(vml
, children
* sizeof (vdev_t
*));
6791 /* if we're not going to mount the filesystems in userland, export */
6793 error
= spa_export_common(newname
, POOL_STATE_EXPORTED
, NULL
,
6800 spa_deactivate(newspa
);
6803 txg
= spa_vdev_config_enter(spa
);
6805 /* re-online all offlined disks */
6806 for (c
= 0; c
< children
; c
++) {
6808 vml
[c
]->vdev_offline
= B_FALSE
;
6811 /* restart initializing disks as necessary */
6812 spa_async_request(spa
, SPA_ASYNC_INITIALIZE_RESTART
);
6814 vdev_reopen(spa
->spa_root_vdev
);
6816 nvlist_free(spa
->spa_config_splitting
);
6817 spa
->spa_config_splitting
= NULL
;
6818 (void) spa_vdev_exit(spa
, NULL
, txg
, error
);
6820 kmem_free(vml
, children
* sizeof (vdev_t
*));
6825 * Find any device that's done replacing, or a vdev marked 'unspare' that's
6826 * currently spared, so we can detach it.
6829 spa_vdev_resilver_done_hunt(vdev_t
*vd
)
6831 vdev_t
*newvd
, *oldvd
;
6833 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
6834 oldvd
= spa_vdev_resilver_done_hunt(vd
->vdev_child
[c
]);
6840 * Check for a completed replacement. We always consider the first
6841 * vdev in the list to be the oldest vdev, and the last one to be
6842 * the newest (see spa_vdev_attach() for how that works). In
6843 * the case where the newest vdev is faulted, we will not automatically
6844 * remove it after a resilver completes. This is OK as it will require
6845 * user intervention to determine which disk the admin wishes to keep.
6847 if (vd
->vdev_ops
== &vdev_replacing_ops
) {
6848 ASSERT(vd
->vdev_children
> 1);
6850 newvd
= vd
->vdev_child
[vd
->vdev_children
- 1];
6851 oldvd
= vd
->vdev_child
[0];
6853 if (vdev_dtl_empty(newvd
, DTL_MISSING
) &&
6854 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
6855 !vdev_dtl_required(oldvd
))
6860 * Check for a completed resilver with the 'unspare' flag set.
6861 * Also potentially update faulted state.
6863 if (vd
->vdev_ops
== &vdev_spare_ops
) {
6864 vdev_t
*first
= vd
->vdev_child
[0];
6865 vdev_t
*last
= vd
->vdev_child
[vd
->vdev_children
- 1];
6867 if (last
->vdev_unspare
) {
6870 } else if (first
->vdev_unspare
) {
6877 if (oldvd
!= NULL
&&
6878 vdev_dtl_empty(newvd
, DTL_MISSING
) &&
6879 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
6880 !vdev_dtl_required(oldvd
))
6883 vdev_propagate_state(vd
);
6886 * If there are more than two spares attached to a disk,
6887 * and those spares are not required, then we want to
6888 * attempt to free them up now so that they can be used
6889 * by other pools. Once we're back down to a single
6890 * disk+spare, we stop removing them.
6892 if (vd
->vdev_children
> 2) {
6893 newvd
= vd
->vdev_child
[1];
6895 if (newvd
->vdev_isspare
&& last
->vdev_isspare
&&
6896 vdev_dtl_empty(last
, DTL_MISSING
) &&
6897 vdev_dtl_empty(last
, DTL_OUTAGE
) &&
6898 !vdev_dtl_required(newvd
))
6907 spa_vdev_resilver_done(spa_t
*spa
)
6909 vdev_t
*vd
, *pvd
, *ppvd
;
6910 uint64_t guid
, sguid
, pguid
, ppguid
;
6912 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6914 while ((vd
= spa_vdev_resilver_done_hunt(spa
->spa_root_vdev
)) != NULL
) {
6915 pvd
= vd
->vdev_parent
;
6916 ppvd
= pvd
->vdev_parent
;
6917 guid
= vd
->vdev_guid
;
6918 pguid
= pvd
->vdev_guid
;
6919 ppguid
= ppvd
->vdev_guid
;
6922 * If we have just finished replacing a hot spared device, then
6923 * we need to detach the parent's first child (the original hot
6926 if (ppvd
->vdev_ops
== &vdev_spare_ops
&& pvd
->vdev_id
== 0 &&
6927 ppvd
->vdev_children
== 2) {
6928 ASSERT(pvd
->vdev_ops
== &vdev_replacing_ops
);
6929 sguid
= ppvd
->vdev_child
[1]->vdev_guid
;
6931 ASSERT(vd
->vdev_resilver_txg
== 0 || !vdev_dtl_required(vd
));
6933 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6934 if (spa_vdev_detach(spa
, guid
, pguid
, B_TRUE
) != 0)
6936 if (sguid
&& spa_vdev_detach(spa
, sguid
, ppguid
, B_TRUE
) != 0)
6938 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6941 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6945 * Update the stored path or FRU for this vdev.
6948 spa_vdev_set_common(spa_t
*spa
, uint64_t guid
, const char *value
,
6952 boolean_t sync
= B_FALSE
;
6954 ASSERT(spa_writeable(spa
));
6956 spa_vdev_state_enter(spa
, SCL_ALL
);
6958 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
6959 return (spa_vdev_state_exit(spa
, NULL
, ENOENT
));
6961 if (!vd
->vdev_ops
->vdev_op_leaf
)
6962 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
6965 if (strcmp(value
, vd
->vdev_path
) != 0) {
6966 spa_strfree(vd
->vdev_path
);
6967 vd
->vdev_path
= spa_strdup(value
);
6971 if (vd
->vdev_fru
== NULL
) {
6972 vd
->vdev_fru
= spa_strdup(value
);
6974 } else if (strcmp(value
, vd
->vdev_fru
) != 0) {
6975 spa_strfree(vd
->vdev_fru
);
6976 vd
->vdev_fru
= spa_strdup(value
);
6981 return (spa_vdev_state_exit(spa
, sync
? vd
: NULL
, 0));
6985 spa_vdev_setpath(spa_t
*spa
, uint64_t guid
, const char *newpath
)
6987 return (spa_vdev_set_common(spa
, guid
, newpath
, B_TRUE
));
6991 spa_vdev_setfru(spa_t
*spa
, uint64_t guid
, const char *newfru
)
6993 return (spa_vdev_set_common(spa
, guid
, newfru
, B_FALSE
));
6997 * ==========================================================================
6999 * ==========================================================================
7002 spa_scrub_pause_resume(spa_t
*spa
, pool_scrub_cmd_t cmd
)
7004 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
7006 if (dsl_scan_resilvering(spa
->spa_dsl_pool
))
7007 return (SET_ERROR(EBUSY
));
7009 return (dsl_scrub_set_pause_resume(spa
->spa_dsl_pool
, cmd
));
7013 spa_scan_stop(spa_t
*spa
)
7015 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
7016 if (dsl_scan_resilvering(spa
->spa_dsl_pool
))
7017 return (SET_ERROR(EBUSY
));
7018 return (dsl_scan_cancel(spa
->spa_dsl_pool
));
7022 spa_scan(spa_t
*spa
, pool_scan_func_t func
)
7024 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
7026 if (func
>= POOL_SCAN_FUNCS
|| func
== POOL_SCAN_NONE
)
7027 return (SET_ERROR(ENOTSUP
));
7030 * If a resilver was requested, but there is no DTL on a
7031 * writeable leaf device, we have nothing to do.
7033 if (func
== POOL_SCAN_RESILVER
&&
7034 !vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
)) {
7035 spa_async_request(spa
, SPA_ASYNC_RESILVER_DONE
);
7039 return (dsl_scan(spa
->spa_dsl_pool
, func
));
7043 * ==========================================================================
7044 * SPA async task processing
7045 * ==========================================================================
7049 spa_async_remove(spa_t
*spa
, vdev_t
*vd
)
7051 if (vd
->vdev_remove_wanted
) {
7052 vd
->vdev_remove_wanted
= B_FALSE
;
7053 vd
->vdev_delayed_close
= B_FALSE
;
7054 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_REMOVED
, VDEV_AUX_NONE
);
7057 * We want to clear the stats, but we don't want to do a full
7058 * vdev_clear() as that will cause us to throw away
7059 * degraded/faulted state as well as attempt to reopen the
7060 * device, all of which is a waste.
7062 vd
->vdev_stat
.vs_read_errors
= 0;
7063 vd
->vdev_stat
.vs_write_errors
= 0;
7064 vd
->vdev_stat
.vs_checksum_errors
= 0;
7066 vdev_state_dirty(vd
->vdev_top
);
7069 for (int c
= 0; c
< vd
->vdev_children
; c
++)
7070 spa_async_remove(spa
, vd
->vdev_child
[c
]);
7074 spa_async_probe(spa_t
*spa
, vdev_t
*vd
)
7076 if (vd
->vdev_probe_wanted
) {
7077 vd
->vdev_probe_wanted
= B_FALSE
;
7078 vdev_reopen(vd
); /* vdev_open() does the actual probe */
7081 for (int c
= 0; c
< vd
->vdev_children
; c
++)
7082 spa_async_probe(spa
, vd
->vdev_child
[c
]);
7086 spa_async_autoexpand(spa_t
*spa
, vdev_t
*vd
)
7088 if (!spa
->spa_autoexpand
)
7091 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
7092 vdev_t
*cvd
= vd
->vdev_child
[c
];
7093 spa_async_autoexpand(spa
, cvd
);
7096 if (!vd
->vdev_ops
->vdev_op_leaf
|| vd
->vdev_physpath
== NULL
)
7099 spa_event_notify(vd
->vdev_spa
, vd
, NULL
, ESC_ZFS_VDEV_AUTOEXPAND
);
7103 spa_async_thread(void *arg
)
7105 spa_t
*spa
= (spa_t
*)arg
;
7106 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
7109 ASSERT(spa
->spa_sync_on
);
7111 mutex_enter(&spa
->spa_async_lock
);
7112 tasks
= spa
->spa_async_tasks
;
7113 spa
->spa_async_tasks
= 0;
7114 mutex_exit(&spa
->spa_async_lock
);
7117 * See if the config needs to be updated.
7119 if (tasks
& SPA_ASYNC_CONFIG_UPDATE
) {
7120 uint64_t old_space
, new_space
;
7122 mutex_enter(&spa_namespace_lock
);
7123 old_space
= metaslab_class_get_space(spa_normal_class(spa
));
7124 old_space
+= metaslab_class_get_space(spa_special_class(spa
));
7125 old_space
+= metaslab_class_get_space(spa_dedup_class(spa
));
7127 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
7129 new_space
= metaslab_class_get_space(spa_normal_class(spa
));
7130 new_space
+= metaslab_class_get_space(spa_special_class(spa
));
7131 new_space
+= metaslab_class_get_space(spa_dedup_class(spa
));
7132 mutex_exit(&spa_namespace_lock
);
7135 * If the pool grew as a result of the config update,
7136 * then log an internal history event.
7138 if (new_space
!= old_space
) {
7139 spa_history_log_internal(spa
, "vdev online", NULL
,
7140 "pool '%s' size: %llu(+%llu)",
7141 spa_name(spa
), new_space
, new_space
- old_space
);
7146 * See if any devices need to be marked REMOVED.
7148 if (tasks
& SPA_ASYNC_REMOVE
) {
7149 spa_vdev_state_enter(spa
, SCL_NONE
);
7150 spa_async_remove(spa
, spa
->spa_root_vdev
);
7151 for (int i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++)
7152 spa_async_remove(spa
, spa
->spa_l2cache
.sav_vdevs
[i
]);
7153 for (int i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
7154 spa_async_remove(spa
, spa
->spa_spares
.sav_vdevs
[i
]);
7155 (void) spa_vdev_state_exit(spa
, NULL
, 0);
7158 if ((tasks
& SPA_ASYNC_AUTOEXPAND
) && !spa_suspended(spa
)) {
7159 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
7160 spa_async_autoexpand(spa
, spa
->spa_root_vdev
);
7161 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
7165 * See if any devices need to be probed.
7167 if (tasks
& SPA_ASYNC_PROBE
) {
7168 spa_vdev_state_enter(spa
, SCL_NONE
);
7169 spa_async_probe(spa
, spa
->spa_root_vdev
);
7170 (void) spa_vdev_state_exit(spa
, NULL
, 0);
7174 * If any devices are done replacing, detach them.
7176 if (tasks
& SPA_ASYNC_RESILVER_DONE
)
7177 spa_vdev_resilver_done(spa
);
7180 * Kick off a resilver.
7182 if (tasks
& SPA_ASYNC_RESILVER
&&
7183 (!dsl_scan_resilvering(dp
) ||
7184 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_RESILVER_DEFER
)))
7185 dsl_resilver_restart(dp
, 0);
7187 if (tasks
& SPA_ASYNC_INITIALIZE_RESTART
) {
7188 mutex_enter(&spa_namespace_lock
);
7189 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
7190 vdev_initialize_restart(spa
->spa_root_vdev
);
7191 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
7192 mutex_exit(&spa_namespace_lock
);
7196 * Let the world know that we're done.
7198 mutex_enter(&spa
->spa_async_lock
);
7199 spa
->spa_async_thread
= NULL
;
7200 cv_broadcast(&spa
->spa_async_cv
);
7201 mutex_exit(&spa
->spa_async_lock
);
7206 spa_async_suspend(spa_t
*spa
)
7208 mutex_enter(&spa
->spa_async_lock
);
7209 spa
->spa_async_suspended
++;
7210 while (spa
->spa_async_thread
!= NULL
)
7211 cv_wait(&spa
->spa_async_cv
, &spa
->spa_async_lock
);
7212 mutex_exit(&spa
->spa_async_lock
);
7214 spa_vdev_remove_suspend(spa
);
7216 zthr_t
*condense_thread
= spa
->spa_condense_zthr
;
7217 if (condense_thread
!= NULL
&& zthr_isrunning(condense_thread
))
7218 VERIFY0(zthr_cancel(condense_thread
));
7220 zthr_t
*discard_thread
= spa
->spa_checkpoint_discard_zthr
;
7221 if (discard_thread
!= NULL
&& zthr_isrunning(discard_thread
))
7222 VERIFY0(zthr_cancel(discard_thread
));
7226 spa_async_resume(spa_t
*spa
)
7228 mutex_enter(&spa
->spa_async_lock
);
7229 ASSERT(spa
->spa_async_suspended
!= 0);
7230 spa
->spa_async_suspended
--;
7231 mutex_exit(&spa
->spa_async_lock
);
7232 spa_restart_removal(spa
);
7234 zthr_t
*condense_thread
= spa
->spa_condense_zthr
;
7235 if (condense_thread
!= NULL
&& !zthr_isrunning(condense_thread
))
7236 zthr_resume(condense_thread
);
7238 zthr_t
*discard_thread
= spa
->spa_checkpoint_discard_zthr
;
7239 if (discard_thread
!= NULL
&& !zthr_isrunning(discard_thread
))
7240 zthr_resume(discard_thread
);
7244 spa_async_tasks_pending(spa_t
*spa
)
7246 uint_t non_config_tasks
;
7248 boolean_t config_task_suspended
;
7250 non_config_tasks
= spa
->spa_async_tasks
& ~SPA_ASYNC_CONFIG_UPDATE
;
7251 config_task
= spa
->spa_async_tasks
& SPA_ASYNC_CONFIG_UPDATE
;
7252 if (spa
->spa_ccw_fail_time
== 0) {
7253 config_task_suspended
= B_FALSE
;
7255 config_task_suspended
=
7256 (gethrtime() - spa
->spa_ccw_fail_time
) <
7257 ((hrtime_t
)zfs_ccw_retry_interval
* NANOSEC
);
7260 return (non_config_tasks
|| (config_task
&& !config_task_suspended
));
7264 spa_async_dispatch(spa_t
*spa
)
7266 mutex_enter(&spa
->spa_async_lock
);
7267 if (spa_async_tasks_pending(spa
) &&
7268 !spa
->spa_async_suspended
&&
7269 spa
->spa_async_thread
== NULL
&&
7271 spa
->spa_async_thread
= thread_create(NULL
, 0,
7272 spa_async_thread
, spa
, 0, &p0
, TS_RUN
, maxclsyspri
);
7273 mutex_exit(&spa
->spa_async_lock
);
7277 spa_async_request(spa_t
*spa
, int task
)
7279 zfs_dbgmsg("spa=%s async request task=%u", spa
->spa_name
, task
);
7280 mutex_enter(&spa
->spa_async_lock
);
7281 spa
->spa_async_tasks
|= task
;
7282 mutex_exit(&spa
->spa_async_lock
);
7286 * ==========================================================================
7287 * SPA syncing routines
7288 * ==========================================================================
7292 bpobj_enqueue_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
7295 bpobj_enqueue(bpo
, bp
, tx
);
7300 spa_free_sync_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
7304 zio_nowait(zio_free_sync(zio
, zio
->io_spa
, dmu_tx_get_txg(tx
), bp
,
7310 * Note: this simple function is not inlined to make it easier to dtrace the
7311 * amount of time spent syncing frees.
7314 spa_sync_frees(spa_t
*spa
, bplist_t
*bpl
, dmu_tx_t
*tx
)
7316 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
7317 bplist_iterate(bpl
, spa_free_sync_cb
, zio
, tx
);
7318 VERIFY(zio_wait(zio
) == 0);
7322 * Note: this simple function is not inlined to make it easier to dtrace the
7323 * amount of time spent syncing deferred frees.
7326 spa_sync_deferred_frees(spa_t
*spa
, dmu_tx_t
*tx
)
7328 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
7329 VERIFY3U(bpobj_iterate(&spa
->spa_deferred_bpobj
,
7330 spa_free_sync_cb
, zio
, tx
), ==, 0);
7331 VERIFY0(zio_wait(zio
));
7335 spa_sync_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
*nv
, dmu_tx_t
*tx
)
7337 char *packed
= NULL
;
7342 VERIFY(nvlist_size(nv
, &nvsize
, NV_ENCODE_XDR
) == 0);
7345 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
7346 * information. This avoids the dmu_buf_will_dirty() path and
7347 * saves us a pre-read to get data we don't actually care about.
7349 bufsize
= P2ROUNDUP((uint64_t)nvsize
, SPA_CONFIG_BLOCKSIZE
);
7350 packed
= vmem_alloc(bufsize
, KM_SLEEP
);
7352 VERIFY(nvlist_pack(nv
, &packed
, &nvsize
, NV_ENCODE_XDR
,
7354 bzero(packed
+ nvsize
, bufsize
- nvsize
);
7356 dmu_write(spa
->spa_meta_objset
, obj
, 0, bufsize
, packed
, tx
);
7358 vmem_free(packed
, bufsize
);
7360 VERIFY(0 == dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
));
7361 dmu_buf_will_dirty(db
, tx
);
7362 *(uint64_t *)db
->db_data
= nvsize
;
7363 dmu_buf_rele(db
, FTAG
);
7367 spa_sync_aux_dev(spa_t
*spa
, spa_aux_vdev_t
*sav
, dmu_tx_t
*tx
,
7368 const char *config
, const char *entry
)
7378 * Update the MOS nvlist describing the list of available devices.
7379 * spa_validate_aux() will have already made sure this nvlist is
7380 * valid and the vdevs are labeled appropriately.
7382 if (sav
->sav_object
== 0) {
7383 sav
->sav_object
= dmu_object_alloc(spa
->spa_meta_objset
,
7384 DMU_OT_PACKED_NVLIST
, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE
,
7385 sizeof (uint64_t), tx
);
7386 VERIFY(zap_update(spa
->spa_meta_objset
,
7387 DMU_POOL_DIRECTORY_OBJECT
, entry
, sizeof (uint64_t), 1,
7388 &sav
->sav_object
, tx
) == 0);
7391 VERIFY(nvlist_alloc(&nvroot
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
7392 if (sav
->sav_count
== 0) {
7393 VERIFY(nvlist_add_nvlist_array(nvroot
, config
, NULL
, 0) == 0);
7395 list
= kmem_alloc(sav
->sav_count
*sizeof (void *), KM_SLEEP
);
7396 for (i
= 0; i
< sav
->sav_count
; i
++)
7397 list
[i
] = vdev_config_generate(spa
, sav
->sav_vdevs
[i
],
7398 B_FALSE
, VDEV_CONFIG_L2CACHE
);
7399 VERIFY(nvlist_add_nvlist_array(nvroot
, config
, list
,
7400 sav
->sav_count
) == 0);
7401 for (i
= 0; i
< sav
->sav_count
; i
++)
7402 nvlist_free(list
[i
]);
7403 kmem_free(list
, sav
->sav_count
* sizeof (void *));
7406 spa_sync_nvlist(spa
, sav
->sav_object
, nvroot
, tx
);
7407 nvlist_free(nvroot
);
7409 sav
->sav_sync
= B_FALSE
;
7413 * Rebuild spa's all-vdev ZAP from the vdev ZAPs indicated in each vdev_t.
7414 * The all-vdev ZAP must be empty.
7417 spa_avz_build(vdev_t
*vd
, uint64_t avz
, dmu_tx_t
*tx
)
7419 spa_t
*spa
= vd
->vdev_spa
;
7421 if (vd
->vdev_top_zap
!= 0) {
7422 VERIFY0(zap_add_int(spa
->spa_meta_objset
, avz
,
7423 vd
->vdev_top_zap
, tx
));
7425 if (vd
->vdev_leaf_zap
!= 0) {
7426 VERIFY0(zap_add_int(spa
->spa_meta_objset
, avz
,
7427 vd
->vdev_leaf_zap
, tx
));
7429 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
7430 spa_avz_build(vd
->vdev_child
[i
], avz
, tx
);
7435 spa_sync_config_object(spa_t
*spa
, dmu_tx_t
*tx
)
7440 * If the pool is being imported from a pre-per-vdev-ZAP version of ZFS,
7441 * its config may not be dirty but we still need to build per-vdev ZAPs.
7442 * Similarly, if the pool is being assembled (e.g. after a split), we
7443 * need to rebuild the AVZ although the config may not be dirty.
7445 if (list_is_empty(&spa
->spa_config_dirty_list
) &&
7446 spa
->spa_avz_action
== AVZ_ACTION_NONE
)
7449 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
7451 ASSERT(spa
->spa_avz_action
== AVZ_ACTION_NONE
||
7452 spa
->spa_avz_action
== AVZ_ACTION_INITIALIZE
||
7453 spa
->spa_all_vdev_zaps
!= 0);
7455 if (spa
->spa_avz_action
== AVZ_ACTION_REBUILD
) {
7456 /* Make and build the new AVZ */
7457 uint64_t new_avz
= zap_create(spa
->spa_meta_objset
,
7458 DMU_OTN_ZAP_METADATA
, DMU_OT_NONE
, 0, tx
);
7459 spa_avz_build(spa
->spa_root_vdev
, new_avz
, tx
);
7461 /* Diff old AVZ with new one */
7465 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
7466 spa
->spa_all_vdev_zaps
);
7467 zap_cursor_retrieve(&zc
, &za
) == 0;
7468 zap_cursor_advance(&zc
)) {
7469 uint64_t vdzap
= za
.za_first_integer
;
7470 if (zap_lookup_int(spa
->spa_meta_objset
, new_avz
,
7473 * ZAP is listed in old AVZ but not in new one;
7476 VERIFY0(zap_destroy(spa
->spa_meta_objset
, vdzap
,
7481 zap_cursor_fini(&zc
);
7483 /* Destroy the old AVZ */
7484 VERIFY0(zap_destroy(spa
->spa_meta_objset
,
7485 spa
->spa_all_vdev_zaps
, tx
));
7487 /* Replace the old AVZ in the dir obj with the new one */
7488 VERIFY0(zap_update(spa
->spa_meta_objset
,
7489 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_VDEV_ZAP_MAP
,
7490 sizeof (new_avz
), 1, &new_avz
, tx
));
7492 spa
->spa_all_vdev_zaps
= new_avz
;
7493 } else if (spa
->spa_avz_action
== AVZ_ACTION_DESTROY
) {
7497 /* Walk through the AVZ and destroy all listed ZAPs */
7498 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
7499 spa
->spa_all_vdev_zaps
);
7500 zap_cursor_retrieve(&zc
, &za
) == 0;
7501 zap_cursor_advance(&zc
)) {
7502 uint64_t zap
= za
.za_first_integer
;
7503 VERIFY0(zap_destroy(spa
->spa_meta_objset
, zap
, tx
));
7506 zap_cursor_fini(&zc
);
7508 /* Destroy and unlink the AVZ itself */
7509 VERIFY0(zap_destroy(spa
->spa_meta_objset
,
7510 spa
->spa_all_vdev_zaps
, tx
));
7511 VERIFY0(zap_remove(spa
->spa_meta_objset
,
7512 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_VDEV_ZAP_MAP
, tx
));
7513 spa
->spa_all_vdev_zaps
= 0;
7516 if (spa
->spa_all_vdev_zaps
== 0) {
7517 spa
->spa_all_vdev_zaps
= zap_create_link(spa
->spa_meta_objset
,
7518 DMU_OTN_ZAP_METADATA
, DMU_POOL_DIRECTORY_OBJECT
,
7519 DMU_POOL_VDEV_ZAP_MAP
, tx
);
7521 spa
->spa_avz_action
= AVZ_ACTION_NONE
;
7523 /* Create ZAPs for vdevs that don't have them. */
7524 vdev_construct_zaps(spa
->spa_root_vdev
, tx
);
7526 config
= spa_config_generate(spa
, spa
->spa_root_vdev
,
7527 dmu_tx_get_txg(tx
), B_FALSE
);
7530 * If we're upgrading the spa version then make sure that
7531 * the config object gets updated with the correct version.
7533 if (spa
->spa_ubsync
.ub_version
< spa
->spa_uberblock
.ub_version
)
7534 fnvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
7535 spa
->spa_uberblock
.ub_version
);
7537 spa_config_exit(spa
, SCL_STATE
, FTAG
);
7539 nvlist_free(spa
->spa_config_syncing
);
7540 spa
->spa_config_syncing
= config
;
7542 spa_sync_nvlist(spa
, spa
->spa_config_object
, config
, tx
);
7546 spa_sync_version(void *arg
, dmu_tx_t
*tx
)
7548 uint64_t *versionp
= arg
;
7549 uint64_t version
= *versionp
;
7550 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
7553 * Setting the version is special cased when first creating the pool.
7555 ASSERT(tx
->tx_txg
!= TXG_INITIAL
);
7557 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
7558 ASSERT(version
>= spa_version(spa
));
7560 spa
->spa_uberblock
.ub_version
= version
;
7561 vdev_config_dirty(spa
->spa_root_vdev
);
7562 spa_history_log_internal(spa
, "set", tx
, "version=%lld", version
);
7566 * Set zpool properties.
7569 spa_sync_props(void *arg
, dmu_tx_t
*tx
)
7571 nvlist_t
*nvp
= arg
;
7572 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
7573 objset_t
*mos
= spa
->spa_meta_objset
;
7574 nvpair_t
*elem
= NULL
;
7576 mutex_enter(&spa
->spa_props_lock
);
7578 while ((elem
= nvlist_next_nvpair(nvp
, elem
))) {
7580 char *strval
, *fname
;
7582 const char *propname
;
7583 zprop_type_t proptype
;
7586 switch (prop
= zpool_name_to_prop(nvpair_name(elem
))) {
7587 case ZPOOL_PROP_INVAL
:
7589 * We checked this earlier in spa_prop_validate().
7591 ASSERT(zpool_prop_feature(nvpair_name(elem
)));
7593 fname
= strchr(nvpair_name(elem
), '@') + 1;
7594 VERIFY0(zfeature_lookup_name(fname
, &fid
));
7596 spa_feature_enable(spa
, fid
, tx
);
7597 spa_history_log_internal(spa
, "set", tx
,
7598 "%s=enabled", nvpair_name(elem
));
7601 case ZPOOL_PROP_VERSION
:
7602 intval
= fnvpair_value_uint64(elem
);
7604 * The version is synced separately before other
7605 * properties and should be correct by now.
7607 ASSERT3U(spa_version(spa
), >=, intval
);
7610 case ZPOOL_PROP_ALTROOT
:
7612 * 'altroot' is a non-persistent property. It should
7613 * have been set temporarily at creation or import time.
7615 ASSERT(spa
->spa_root
!= NULL
);
7618 case ZPOOL_PROP_READONLY
:
7619 case ZPOOL_PROP_CACHEFILE
:
7621 * 'readonly' and 'cachefile' are also non-persisitent
7625 case ZPOOL_PROP_COMMENT
:
7626 strval
= fnvpair_value_string(elem
);
7627 if (spa
->spa_comment
!= NULL
)
7628 spa_strfree(spa
->spa_comment
);
7629 spa
->spa_comment
= spa_strdup(strval
);
7631 * We need to dirty the configuration on all the vdevs
7632 * so that their labels get updated. It's unnecessary
7633 * to do this for pool creation since the vdev's
7634 * configuration has already been dirtied.
7636 if (tx
->tx_txg
!= TXG_INITIAL
)
7637 vdev_config_dirty(spa
->spa_root_vdev
);
7638 spa_history_log_internal(spa
, "set", tx
,
7639 "%s=%s", nvpair_name(elem
), strval
);
7643 * Set pool property values in the poolprops mos object.
7645 if (spa
->spa_pool_props_object
== 0) {
7646 spa
->spa_pool_props_object
=
7647 zap_create_link(mos
, DMU_OT_POOL_PROPS
,
7648 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_PROPS
,
7652 /* normalize the property name */
7653 propname
= zpool_prop_to_name(prop
);
7654 proptype
= zpool_prop_get_type(prop
);
7656 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
7657 ASSERT(proptype
== PROP_TYPE_STRING
);
7658 strval
= fnvpair_value_string(elem
);
7659 VERIFY0(zap_update(mos
,
7660 spa
->spa_pool_props_object
, propname
,
7661 1, strlen(strval
) + 1, strval
, tx
));
7662 spa_history_log_internal(spa
, "set", tx
,
7663 "%s=%s", nvpair_name(elem
), strval
);
7664 } else if (nvpair_type(elem
) == DATA_TYPE_UINT64
) {
7665 intval
= fnvpair_value_uint64(elem
);
7667 if (proptype
== PROP_TYPE_INDEX
) {
7669 VERIFY0(zpool_prop_index_to_string(
7670 prop
, intval
, &unused
));
7672 VERIFY0(zap_update(mos
,
7673 spa
->spa_pool_props_object
, propname
,
7674 8, 1, &intval
, tx
));
7675 spa_history_log_internal(spa
, "set", tx
,
7676 "%s=%lld", nvpair_name(elem
), intval
);
7678 ASSERT(0); /* not allowed */
7682 case ZPOOL_PROP_DELEGATION
:
7683 spa
->spa_delegation
= intval
;
7685 case ZPOOL_PROP_BOOTFS
:
7686 spa
->spa_bootfs
= intval
;
7688 case ZPOOL_PROP_FAILUREMODE
:
7689 spa
->spa_failmode
= intval
;
7691 case ZPOOL_PROP_AUTOEXPAND
:
7692 spa
->spa_autoexpand
= intval
;
7693 if (tx
->tx_txg
!= TXG_INITIAL
)
7694 spa_async_request(spa
,
7695 SPA_ASYNC_AUTOEXPAND
);
7697 case ZPOOL_PROP_MULTIHOST
:
7698 spa
->spa_multihost
= intval
;
7700 case ZPOOL_PROP_DEDUPDITTO
:
7701 spa
->spa_dedup_ditto
= intval
;
7710 mutex_exit(&spa
->spa_props_lock
);
7714 * Perform one-time upgrade on-disk changes. spa_version() does not
7715 * reflect the new version this txg, so there must be no changes this
7716 * txg to anything that the upgrade code depends on after it executes.
7717 * Therefore this must be called after dsl_pool_sync() does the sync
7721 spa_sync_upgrades(spa_t
*spa
, dmu_tx_t
*tx
)
7723 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
7725 ASSERT(spa
->spa_sync_pass
== 1);
7727 rrw_enter(&dp
->dp_config_rwlock
, RW_WRITER
, FTAG
);
7729 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_ORIGIN
&&
7730 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_ORIGIN
) {
7731 dsl_pool_create_origin(dp
, tx
);
7733 /* Keeping the origin open increases spa_minref */
7734 spa
->spa_minref
+= 3;
7737 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_NEXT_CLONES
&&
7738 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_NEXT_CLONES
) {
7739 dsl_pool_upgrade_clones(dp
, tx
);
7742 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_DIR_CLONES
&&
7743 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_DIR_CLONES
) {
7744 dsl_pool_upgrade_dir_clones(dp
, tx
);
7746 /* Keeping the freedir open increases spa_minref */
7747 spa
->spa_minref
+= 3;
7750 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_FEATURES
&&
7751 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
7752 spa_feature_create_zap_objects(spa
, tx
);
7756 * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable
7757 * when possibility to use lz4 compression for metadata was added
7758 * Old pools that have this feature enabled must be upgraded to have
7759 * this feature active
7761 if (spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
7762 boolean_t lz4_en
= spa_feature_is_enabled(spa
,
7763 SPA_FEATURE_LZ4_COMPRESS
);
7764 boolean_t lz4_ac
= spa_feature_is_active(spa
,
7765 SPA_FEATURE_LZ4_COMPRESS
);
7767 if (lz4_en
&& !lz4_ac
)
7768 spa_feature_incr(spa
, SPA_FEATURE_LZ4_COMPRESS
, tx
);
7772 * If we haven't written the salt, do so now. Note that the
7773 * feature may not be activated yet, but that's fine since
7774 * the presence of this ZAP entry is backwards compatible.
7776 if (zap_contains(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
7777 DMU_POOL_CHECKSUM_SALT
) == ENOENT
) {
7778 VERIFY0(zap_add(spa
->spa_meta_objset
,
7779 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CHECKSUM_SALT
, 1,
7780 sizeof (spa
->spa_cksum_salt
.zcs_bytes
),
7781 spa
->spa_cksum_salt
.zcs_bytes
, tx
));
7784 rrw_exit(&dp
->dp_config_rwlock
, FTAG
);
7788 vdev_indirect_state_sync_verify(vdev_t
*vd
)
7790 ASSERTV(vdev_indirect_mapping_t
*vim
= vd
->vdev_indirect_mapping
);
7791 ASSERTV(vdev_indirect_births_t
*vib
= vd
->vdev_indirect_births
);
7793 if (vd
->vdev_ops
== &vdev_indirect_ops
) {
7794 ASSERT(vim
!= NULL
);
7795 ASSERT(vib
!= NULL
);
7798 uint64_t obsolete_sm_object
= 0;
7799 ASSERT0(vdev_obsolete_sm_object(vd
, &obsolete_sm_object
));
7800 if (obsolete_sm_object
!= 0) {
7801 ASSERT(vd
->vdev_obsolete_sm
!= NULL
);
7802 ASSERT(vd
->vdev_removing
||
7803 vd
->vdev_ops
== &vdev_indirect_ops
);
7804 ASSERT(vdev_indirect_mapping_num_entries(vim
) > 0);
7805 ASSERT(vdev_indirect_mapping_bytes_mapped(vim
) > 0);
7806 ASSERT3U(obsolete_sm_object
, ==,
7807 space_map_object(vd
->vdev_obsolete_sm
));
7808 ASSERT3U(vdev_indirect_mapping_bytes_mapped(vim
), >=,
7809 space_map_allocated(vd
->vdev_obsolete_sm
));
7811 ASSERT(vd
->vdev_obsolete_segments
!= NULL
);
7814 * Since frees / remaps to an indirect vdev can only
7815 * happen in syncing context, the obsolete segments
7816 * tree must be empty when we start syncing.
7818 ASSERT0(range_tree_space(vd
->vdev_obsolete_segments
));
7822 * Sync the specified transaction group. New blocks may be dirtied as
7823 * part of the process, so we iterate until it converges.
7826 spa_sync(spa_t
*spa
, uint64_t txg
)
7828 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
7829 objset_t
*mos
= spa
->spa_meta_objset
;
7830 bplist_t
*free_bpl
= &spa
->spa_free_bplist
[txg
& TXG_MASK
];
7831 metaslab_class_t
*normal
= spa_normal_class(spa
);
7832 metaslab_class_t
*special
= spa_special_class(spa
);
7833 metaslab_class_t
*dedup
= spa_dedup_class(spa
);
7834 vdev_t
*rvd
= spa
->spa_root_vdev
;
7838 uint32_t max_queue_depth
= zfs_vdev_async_write_max_active
*
7839 zfs_vdev_queue_depth_pct
/ 100;
7841 VERIFY(spa_writeable(spa
));
7844 * Wait for i/os issued in open context that need to complete
7845 * before this txg syncs.
7847 (void) zio_wait(spa
->spa_txg_zio
[txg
& TXG_MASK
]);
7848 spa
->spa_txg_zio
[txg
& TXG_MASK
] = zio_root(spa
, NULL
, NULL
,
7852 * Lock out configuration changes.
7854 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
7856 spa
->spa_syncing_txg
= txg
;
7857 spa
->spa_sync_pass
= 0;
7859 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
7860 mutex_enter(&spa
->spa_alloc_locks
[i
]);
7861 VERIFY0(avl_numnodes(&spa
->spa_alloc_trees
[i
]));
7862 mutex_exit(&spa
->spa_alloc_locks
[i
]);
7866 * If there are any pending vdev state changes, convert them
7867 * into config changes that go out with this transaction group.
7869 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
7870 while (list_head(&spa
->spa_state_dirty_list
) != NULL
) {
7872 * We need the write lock here because, for aux vdevs,
7873 * calling vdev_config_dirty() modifies sav_config.
7874 * This is ugly and will become unnecessary when we
7875 * eliminate the aux vdev wart by integrating all vdevs
7876 * into the root vdev tree.
7878 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7879 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_WRITER
);
7880 while ((vd
= list_head(&spa
->spa_state_dirty_list
)) != NULL
) {
7881 vdev_state_clean(vd
);
7882 vdev_config_dirty(vd
);
7884 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7885 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
7887 spa_config_exit(spa
, SCL_STATE
, FTAG
);
7889 tx
= dmu_tx_create_assigned(dp
, txg
);
7891 spa
->spa_sync_starttime
= gethrtime();
7892 taskq_cancel_id(system_delay_taskq
, spa
->spa_deadman_tqid
);
7893 spa
->spa_deadman_tqid
= taskq_dispatch_delay(system_delay_taskq
,
7894 spa_deadman
, spa
, TQ_SLEEP
, ddi_get_lbolt() +
7895 NSEC_TO_TICK(spa
->spa_deadman_synctime
));
7898 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
7899 * set spa_deflate if we have no raid-z vdevs.
7901 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_RAIDZ_DEFLATE
&&
7902 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
7905 for (i
= 0; i
< rvd
->vdev_children
; i
++) {
7906 vd
= rvd
->vdev_child
[i
];
7907 if (vd
->vdev_deflate_ratio
!= SPA_MINBLOCKSIZE
)
7910 if (i
== rvd
->vdev_children
) {
7911 spa
->spa_deflate
= TRUE
;
7912 VERIFY(0 == zap_add(spa
->spa_meta_objset
,
7913 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
7914 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
));
7919 * Set the top-level vdev's max queue depth. Evaluate each
7920 * top-level's async write queue depth in case it changed.
7921 * The max queue depth will not change in the middle of syncing
7924 uint64_t slots_per_allocator
= 0;
7925 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
7926 vdev_t
*tvd
= rvd
->vdev_child
[c
];
7927 metaslab_group_t
*mg
= tvd
->vdev_mg
;
7928 metaslab_class_t
*mc
;
7930 if (mg
== NULL
|| !metaslab_group_initialized(mg
))
7934 if (mc
!= normal
&& mc
!= special
&& mc
!= dedup
)
7938 * It is safe to do a lock-free check here because only async
7939 * allocations look at mg_max_alloc_queue_depth, and async
7940 * allocations all happen from spa_sync().
7942 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++)
7943 ASSERT0(zfs_refcount_count(
7944 &(mg
->mg_alloc_queue_depth
[i
])));
7945 mg
->mg_max_alloc_queue_depth
= max_queue_depth
;
7947 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
7948 mg
->mg_cur_max_alloc_queue_depth
[i
] =
7949 zfs_vdev_def_queue_depth
;
7951 slots_per_allocator
+= zfs_vdev_def_queue_depth
;
7954 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
7955 ASSERT0(zfs_refcount_count(&normal
->mc_alloc_slots
[i
]));
7956 ASSERT0(zfs_refcount_count(&special
->mc_alloc_slots
[i
]));
7957 ASSERT0(zfs_refcount_count(&dedup
->mc_alloc_slots
[i
]));
7958 normal
->mc_alloc_max_slots
[i
] = slots_per_allocator
;
7959 special
->mc_alloc_max_slots
[i
] = slots_per_allocator
;
7960 dedup
->mc_alloc_max_slots
[i
] = slots_per_allocator
;
7962 normal
->mc_alloc_throttle_enabled
= zio_dva_throttle_enabled
;
7963 special
->mc_alloc_throttle_enabled
= zio_dva_throttle_enabled
;
7964 dedup
->mc_alloc_throttle_enabled
= zio_dva_throttle_enabled
;
7966 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
7967 vdev_t
*vd
= rvd
->vdev_child
[c
];
7968 vdev_indirect_state_sync_verify(vd
);
7970 if (vdev_indirect_should_condense(vd
)) {
7971 spa_condense_indirect_start_sync(vd
, tx
);
7977 * Iterate to convergence.
7980 int pass
= ++spa
->spa_sync_pass
;
7982 spa_sync_config_object(spa
, tx
);
7983 spa_sync_aux_dev(spa
, &spa
->spa_spares
, tx
,
7984 ZPOOL_CONFIG_SPARES
, DMU_POOL_SPARES
);
7985 spa_sync_aux_dev(spa
, &spa
->spa_l2cache
, tx
,
7986 ZPOOL_CONFIG_L2CACHE
, DMU_POOL_L2CACHE
);
7987 spa_errlog_sync(spa
, txg
);
7988 dsl_pool_sync(dp
, txg
);
7990 if (pass
< zfs_sync_pass_deferred_free
) {
7991 spa_sync_frees(spa
, free_bpl
, tx
);
7994 * We can not defer frees in pass 1, because
7995 * we sync the deferred frees later in pass 1.
7997 ASSERT3U(pass
, >, 1);
7998 bplist_iterate(free_bpl
, bpobj_enqueue_cb
,
7999 &spa
->spa_deferred_bpobj
, tx
);
8003 dsl_scan_sync(dp
, tx
);
8005 if (spa
->spa_vdev_removal
!= NULL
)
8008 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, txg
))
8013 spa_sync_upgrades(spa
, tx
);
8015 spa
->spa_uberblock
.ub_rootbp
.blk_birth
);
8017 * Note: We need to check if the MOS is dirty
8018 * because we could have marked the MOS dirty
8019 * without updating the uberblock (e.g. if we
8020 * have sync tasks but no dirty user data). We
8021 * need to check the uberblock's rootbp because
8022 * it is updated if we have synced out dirty
8023 * data (though in this case the MOS will most
8024 * likely also be dirty due to second order
8025 * effects, we don't want to rely on that here).
8027 if (spa
->spa_uberblock
.ub_rootbp
.blk_birth
< txg
&&
8028 !dmu_objset_is_dirty(mos
, txg
)) {
8030 * Nothing changed on the first pass,
8031 * therefore this TXG is a no-op. Avoid
8032 * syncing deferred frees, so that we
8033 * can keep this TXG as a no-op.
8035 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
,
8037 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
8038 ASSERT(txg_list_empty(&dp
->dp_sync_tasks
, txg
));
8039 ASSERT(txg_list_empty(&dp
->dp_early_sync_tasks
,
8043 spa_sync_deferred_frees(spa
, tx
);
8046 } while (dmu_objset_is_dirty(mos
, txg
));
8049 if (!list_is_empty(&spa
->spa_config_dirty_list
)) {
8051 * Make sure that the number of ZAPs for all the vdevs matches
8052 * the number of ZAPs in the per-vdev ZAP list. This only gets
8053 * called if the config is dirty; otherwise there may be
8054 * outstanding AVZ operations that weren't completed in
8055 * spa_sync_config_object.
8057 uint64_t all_vdev_zap_entry_count
;
8058 ASSERT0(zap_count(spa
->spa_meta_objset
,
8059 spa
->spa_all_vdev_zaps
, &all_vdev_zap_entry_count
));
8060 ASSERT3U(vdev_count_verify_zaps(spa
->spa_root_vdev
), ==,
8061 all_vdev_zap_entry_count
);
8065 if (spa
->spa_vdev_removal
!= NULL
) {
8066 ASSERT0(spa
->spa_vdev_removal
->svr_bytes_done
[txg
& TXG_MASK
]);
8070 * Rewrite the vdev configuration (which includes the uberblock)
8071 * to commit the transaction group.
8073 * If there are no dirty vdevs, we sync the uberblock to a few
8074 * random top-level vdevs that are known to be visible in the
8075 * config cache (see spa_vdev_add() for a complete description).
8076 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
8080 * We hold SCL_STATE to prevent vdev open/close/etc.
8081 * while we're attempting to write the vdev labels.
8083 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
8085 if (list_is_empty(&spa
->spa_config_dirty_list
)) {
8086 vdev_t
*svd
[SPA_SYNC_MIN_VDEVS
] = { NULL
};
8088 int children
= rvd
->vdev_children
;
8089 int c0
= spa_get_random(children
);
8091 for (int c
= 0; c
< children
; c
++) {
8092 vd
= rvd
->vdev_child
[(c0
+ c
) % children
];
8094 /* Stop when revisiting the first vdev */
8095 if (c
> 0 && svd
[0] == vd
)
8098 if (vd
->vdev_ms_array
== 0 || vd
->vdev_islog
||
8099 !vdev_is_concrete(vd
))
8102 svd
[svdcount
++] = vd
;
8103 if (svdcount
== SPA_SYNC_MIN_VDEVS
)
8106 error
= vdev_config_sync(svd
, svdcount
, txg
);
8108 error
= vdev_config_sync(rvd
->vdev_child
,
8109 rvd
->vdev_children
, txg
);
8113 spa
->spa_last_synced_guid
= rvd
->vdev_guid
;
8115 spa_config_exit(spa
, SCL_STATE
, FTAG
);
8119 zio_suspend(spa
, NULL
, ZIO_SUSPEND_IOERR
);
8120 zio_resume_wait(spa
);
8124 taskq_cancel_id(system_delay_taskq
, spa
->spa_deadman_tqid
);
8125 spa
->spa_deadman_tqid
= 0;
8128 * Clear the dirty config list.
8130 while ((vd
= list_head(&spa
->spa_config_dirty_list
)) != NULL
)
8131 vdev_config_clean(vd
);
8134 * Now that the new config has synced transactionally,
8135 * let it become visible to the config cache.
8137 if (spa
->spa_config_syncing
!= NULL
) {
8138 spa_config_set(spa
, spa
->spa_config_syncing
);
8139 spa
->spa_config_txg
= txg
;
8140 spa
->spa_config_syncing
= NULL
;
8143 dsl_pool_sync_done(dp
, txg
);
8145 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
8146 mutex_enter(&spa
->spa_alloc_locks
[i
]);
8147 VERIFY0(avl_numnodes(&spa
->spa_alloc_trees
[i
]));
8148 mutex_exit(&spa
->spa_alloc_locks
[i
]);
8152 * Update usable space statistics.
8154 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, TXG_CLEAN(txg
)))
8156 vdev_sync_done(vd
, txg
);
8158 spa_update_dspace(spa
);
8161 * It had better be the case that we didn't dirty anything
8162 * since vdev_config_sync().
8164 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
, txg
));
8165 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
8166 ASSERT(txg_list_empty(&spa
->spa_vdev_txg_list
, txg
));
8168 while (zfs_pause_spa_sync
)
8171 spa
->spa_sync_pass
= 0;
8174 * Update the last synced uberblock here. We want to do this at
8175 * the end of spa_sync() so that consumers of spa_last_synced_txg()
8176 * will be guaranteed that all the processing associated with
8177 * that txg has been completed.
8179 spa
->spa_ubsync
= spa
->spa_uberblock
;
8180 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
8182 spa_handle_ignored_writes(spa
);
8185 * If any async tasks have been requested, kick them off.
8187 spa_async_dispatch(spa
);
8191 * Sync all pools. We don't want to hold the namespace lock across these
8192 * operations, so we take a reference on the spa_t and drop the lock during the
8196 spa_sync_allpools(void)
8199 mutex_enter(&spa_namespace_lock
);
8200 while ((spa
= spa_next(spa
)) != NULL
) {
8201 if (spa_state(spa
) != POOL_STATE_ACTIVE
||
8202 !spa_writeable(spa
) || spa_suspended(spa
))
8204 spa_open_ref(spa
, FTAG
);
8205 mutex_exit(&spa_namespace_lock
);
8206 txg_wait_synced(spa_get_dsl(spa
), 0);
8207 mutex_enter(&spa_namespace_lock
);
8208 spa_close(spa
, FTAG
);
8210 mutex_exit(&spa_namespace_lock
);
8214 * ==========================================================================
8215 * Miscellaneous routines
8216 * ==========================================================================
8220 * Remove all pools in the system.
8228 * Remove all cached state. All pools should be closed now,
8229 * so every spa in the AVL tree should be unreferenced.
8231 mutex_enter(&spa_namespace_lock
);
8232 while ((spa
= spa_next(NULL
)) != NULL
) {
8234 * Stop async tasks. The async thread may need to detach
8235 * a device that's been replaced, which requires grabbing
8236 * spa_namespace_lock, so we must drop it here.
8238 spa_open_ref(spa
, FTAG
);
8239 mutex_exit(&spa_namespace_lock
);
8240 spa_async_suspend(spa
);
8241 mutex_enter(&spa_namespace_lock
);
8242 spa_close(spa
, FTAG
);
8244 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
8246 spa_deactivate(spa
);
8250 mutex_exit(&spa_namespace_lock
);
8254 spa_lookup_by_guid(spa_t
*spa
, uint64_t guid
, boolean_t aux
)
8259 if ((vd
= vdev_lookup_by_guid(spa
->spa_root_vdev
, guid
)) != NULL
)
8263 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
8264 vd
= spa
->spa_l2cache
.sav_vdevs
[i
];
8265 if (vd
->vdev_guid
== guid
)
8269 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
8270 vd
= spa
->spa_spares
.sav_vdevs
[i
];
8271 if (vd
->vdev_guid
== guid
)
8280 spa_upgrade(spa_t
*spa
, uint64_t version
)
8282 ASSERT(spa_writeable(spa
));
8284 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
8287 * This should only be called for a non-faulted pool, and since a
8288 * future version would result in an unopenable pool, this shouldn't be
8291 ASSERT(SPA_VERSION_IS_SUPPORTED(spa
->spa_uberblock
.ub_version
));
8292 ASSERT3U(version
, >=, spa
->spa_uberblock
.ub_version
);
8294 spa
->spa_uberblock
.ub_version
= version
;
8295 vdev_config_dirty(spa
->spa_root_vdev
);
8297 spa_config_exit(spa
, SCL_ALL
, FTAG
);
8299 txg_wait_synced(spa_get_dsl(spa
), 0);
8303 spa_has_spare(spa_t
*spa
, uint64_t guid
)
8307 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
8309 for (i
= 0; i
< sav
->sav_count
; i
++)
8310 if (sav
->sav_vdevs
[i
]->vdev_guid
== guid
)
8313 for (i
= 0; i
< sav
->sav_npending
; i
++) {
8314 if (nvlist_lookup_uint64(sav
->sav_pending
[i
], ZPOOL_CONFIG_GUID
,
8315 &spareguid
) == 0 && spareguid
== guid
)
8323 * Check if a pool has an active shared spare device.
8324 * Note: reference count of an active spare is 2, as a spare and as a replace
8327 spa_has_active_shared_spare(spa_t
*spa
)
8331 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
8333 for (i
= 0; i
< sav
->sav_count
; i
++) {
8334 if (spa_spare_exists(sav
->sav_vdevs
[i
]->vdev_guid
, &pool
,
8335 &refcnt
) && pool
!= 0ULL && pool
== spa_guid(spa
) &&
8344 spa_event_create(spa_t
*spa
, vdev_t
*vd
, nvlist_t
*hist_nvl
, const char *name
)
8346 sysevent_t
*ev
= NULL
;
8350 resource
= zfs_event_create(spa
, vd
, FM_SYSEVENT_CLASS
, name
, hist_nvl
);
8352 ev
= kmem_alloc(sizeof (sysevent_t
), KM_SLEEP
);
8353 ev
->resource
= resource
;
8360 spa_event_post(sysevent_t
*ev
)
8364 zfs_zevent_post(ev
->resource
, NULL
, zfs_zevent_post_cb
);
8365 kmem_free(ev
, sizeof (*ev
));
8371 * Post a zevent corresponding to the given sysevent. The 'name' must be one
8372 * of the event definitions in sys/sysevent/eventdefs.h. The payload will be
8373 * filled in from the spa and (optionally) the vdev. This doesn't do anything
8374 * in the userland libzpool, as we don't want consumers to misinterpret ztest
8375 * or zdb as real changes.
8378 spa_event_notify(spa_t
*spa
, vdev_t
*vd
, nvlist_t
*hist_nvl
, const char *name
)
8380 spa_event_post(spa_event_create(spa
, vd
, hist_nvl
, name
));
8383 #if defined(_KERNEL)
8384 /* state manipulation functions */
8385 EXPORT_SYMBOL(spa_open
);
8386 EXPORT_SYMBOL(spa_open_rewind
);
8387 EXPORT_SYMBOL(spa_get_stats
);
8388 EXPORT_SYMBOL(spa_create
);
8389 EXPORT_SYMBOL(spa_import
);
8390 EXPORT_SYMBOL(spa_tryimport
);
8391 EXPORT_SYMBOL(spa_destroy
);
8392 EXPORT_SYMBOL(spa_export
);
8393 EXPORT_SYMBOL(spa_reset
);
8394 EXPORT_SYMBOL(spa_async_request
);
8395 EXPORT_SYMBOL(spa_async_suspend
);
8396 EXPORT_SYMBOL(spa_async_resume
);
8397 EXPORT_SYMBOL(spa_inject_addref
);
8398 EXPORT_SYMBOL(spa_inject_delref
);
8399 EXPORT_SYMBOL(spa_scan_stat_init
);
8400 EXPORT_SYMBOL(spa_scan_get_stats
);
8402 /* device maniion */
8403 EXPORT_SYMBOL(spa_vdev_add
);
8404 EXPORT_SYMBOL(spa_vdev_attach
);
8405 EXPORT_SYMBOL(spa_vdev_detach
);
8406 EXPORT_SYMBOL(spa_vdev_setpath
);
8407 EXPORT_SYMBOL(spa_vdev_setfru
);
8408 EXPORT_SYMBOL(spa_vdev_split_mirror
);
8410 /* spare statech is global across all pools) */
8411 EXPORT_SYMBOL(spa_spare_add
);
8412 EXPORT_SYMBOL(spa_spare_remove
);
8413 EXPORT_SYMBOL(spa_spare_exists
);
8414 EXPORT_SYMBOL(spa_spare_activate
);
8416 /* L2ARC statech is global across all pools) */
8417 EXPORT_SYMBOL(spa_l2cache_add
);
8418 EXPORT_SYMBOL(spa_l2cache_remove
);
8419 EXPORT_SYMBOL(spa_l2cache_exists
);
8420 EXPORT_SYMBOL(spa_l2cache_activate
);
8421 EXPORT_SYMBOL(spa_l2cache_drop
);
8424 EXPORT_SYMBOL(spa_scan
);
8425 EXPORT_SYMBOL(spa_scan_stop
);
8428 EXPORT_SYMBOL(spa_sync
); /* only for DMU use */
8429 EXPORT_SYMBOL(spa_sync_allpools
);
8432 EXPORT_SYMBOL(spa_prop_set
);
8433 EXPORT_SYMBOL(spa_prop_get
);
8434 EXPORT_SYMBOL(spa_prop_clear_bootfs
);
8436 /* asynchronous event notification */
8437 EXPORT_SYMBOL(spa_event_notify
);
8440 #if defined(_KERNEL)
8441 module_param(spa_load_verify_maxinflight
, int, 0644);
8442 MODULE_PARM_DESC(spa_load_verify_maxinflight
,
8443 "Max concurrent traversal I/Os while verifying pool during import -X");
8445 module_param(spa_load_verify_metadata
, int, 0644);
8446 MODULE_PARM_DESC(spa_load_verify_metadata
,
8447 "Set to traverse metadata on pool import");
8449 module_param(spa_load_verify_data
, int, 0644);
8450 MODULE_PARM_DESC(spa_load_verify_data
,
8451 "Set to traverse data on pool import");
8453 module_param(spa_load_print_vdev_tree
, int, 0644);
8454 MODULE_PARM_DESC(spa_load_print_vdev_tree
,
8455 "Print vdev tree to zfs_dbgmsg during pool import");
8458 module_param(zio_taskq_batch_pct
, uint
, 0444);
8459 MODULE_PARM_DESC(zio_taskq_batch_pct
,
8460 "Percentage of CPUs to run an IO worker thread");
8463 module_param(zfs_max_missing_tvds
, ulong
, 0644);
8464 MODULE_PARM_DESC(zfs_max_missing_tvds
,
8465 "Allow importing pool with up to this number of missing top-level vdevs"
8466 " (in read-only mode)");