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) 2015, Nexenta Systems, Inc. All rights reserved.
26 * Copyright (c) 2013, 2014, Nexenta Systems, Inc. All rights reserved.
27 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
28 * Copyright 2013 Saso Kiselkov. All rights reserved.
29 * Copyright (c) 2014 Integros [integros.com]
30 * Copyright 2016 Toomas Soome <tsoome@me.com>
31 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
32 * Copyright (c) 2017 Datto Inc.
33 * Copyright 2017 Joyent, Inc.
37 * SPA: Storage Pool Allocator
39 * This file contains all the routines used when modifying on-disk SPA state.
40 * This includes opening, importing, destroying, exporting a pool, and syncing a
44 #include <sys/zfs_context.h>
45 #include <sys/fm/fs/zfs.h>
46 #include <sys/spa_impl.h>
48 #include <sys/zio_checksum.h>
50 #include <sys/dmu_tx.h>
54 #include <sys/vdev_impl.h>
55 #include <sys/vdev_removal.h>
56 #include <sys/vdev_indirect_mapping.h>
57 #include <sys/vdev_indirect_births.h>
58 #include <sys/vdev_disk.h>
59 #include <sys/metaslab.h>
60 #include <sys/metaslab_impl.h>
62 #include <sys/uberblock_impl.h>
65 #include <sys/bpobj.h>
66 #include <sys/dmu_traverse.h>
67 #include <sys/dmu_objset.h>
68 #include <sys/unique.h>
69 #include <sys/dsl_pool.h>
70 #include <sys/dsl_dataset.h>
71 #include <sys/dsl_dir.h>
72 #include <sys/dsl_prop.h>
73 #include <sys/dsl_synctask.h>
74 #include <sys/fs/zfs.h>
76 #include <sys/callb.h>
77 #include <sys/systeminfo.h>
78 #include <sys/spa_boot.h>
79 #include <sys/zfs_ioctl.h>
80 #include <sys/dsl_scan.h>
81 #include <sys/zfeature.h>
82 #include <sys/dsl_destroy.h>
86 #include <sys/fm/protocol.h>
87 #include <sys/fm/util.h>
88 #include <sys/bootprops.h>
89 #include <sys/callb.h>
90 #include <sys/cpupart.h>
92 #include <sys/sysdc.h>
97 #include "zfs_comutil.h"
100 * The interval, in seconds, at which failed configuration cache file writes
103 int zfs_ccw_retry_interval
= 300;
105 typedef enum zti_modes
{
106 ZTI_MODE_FIXED
, /* value is # of threads (min 1) */
107 ZTI_MODE_BATCH
, /* cpu-intensive; value is ignored */
108 ZTI_MODE_NULL
, /* don't create a taskq */
112 #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) }
113 #define ZTI_PCT(n) { ZTI_MODE_ONLINE_PERCENT, (n), 1 }
114 #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 }
115 #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 }
117 #define ZTI_N(n) ZTI_P(n, 1)
118 #define ZTI_ONE ZTI_N(1)
120 typedef struct zio_taskq_info
{
121 zti_modes_t zti_mode
;
126 static const char *const zio_taskq_types
[ZIO_TASKQ_TYPES
] = {
127 "iss", "iss_h", "int", "int_h"
131 * This table defines the taskq settings for each ZFS I/O type. When
132 * initializing a pool, we use this table to create an appropriately sized
133 * taskq. Some operations are low volume and therefore have a small, static
134 * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
135 * macros. Other operations process a large amount of data; the ZTI_BATCH
136 * macro causes us to create a taskq oriented for throughput. Some operations
137 * are so high frequency and short-lived that the taskq itself can become a a
138 * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
139 * additional degree of parallelism specified by the number of threads per-
140 * taskq and the number of taskqs; when dispatching an event in this case, the
141 * particular taskq is chosen at random.
143 * The different taskq priorities are to handle the different contexts (issue
144 * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that
145 * need to be handled with minimum delay.
147 const zio_taskq_info_t zio_taskqs
[ZIO_TYPES
][ZIO_TASKQ_TYPES
] = {
148 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
149 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* NULL */
150 { ZTI_N(8), ZTI_NULL
, ZTI_P(12, 8), ZTI_NULL
}, /* READ */
151 { ZTI_BATCH
, ZTI_N(5), ZTI_P(12, 8), ZTI_N(5) }, /* WRITE */
152 { ZTI_P(12, 8), ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* FREE */
153 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* CLAIM */
154 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* IOCTL */
157 static void spa_sync_version(void *arg
, dmu_tx_t
*tx
);
158 static void spa_sync_props(void *arg
, dmu_tx_t
*tx
);
159 static boolean_t
spa_has_active_shared_spare(spa_t
*spa
);
160 static int spa_load_impl(spa_t
*spa
, spa_import_type_t type
, char **ereport
,
161 boolean_t reloading
);
162 static void spa_vdev_resilver_done(spa_t
*spa
);
164 uint_t zio_taskq_batch_pct
= 75; /* 1 thread per cpu in pset */
165 id_t zio_taskq_psrset_bind
= PS_NONE
;
166 boolean_t zio_taskq_sysdc
= B_TRUE
; /* use SDC scheduling class */
167 uint_t zio_taskq_basedc
= 80; /* base duty cycle */
169 boolean_t spa_create_process
= B_TRUE
; /* no process ==> no sysdc */
172 * Report any spa_load_verify errors found, but do not fail spa_load.
173 * This is used by zdb to analyze non-idle pools.
175 boolean_t spa_load_verify_dryrun
= B_FALSE
;
178 * This (illegal) pool name is used when temporarily importing a spa_t in order
179 * to get the vdev stats associated with the imported devices.
181 #define TRYIMPORT_NAME "$import"
184 * For debugging purposes: print out vdev tree during pool import.
186 int spa_load_print_vdev_tree
= B_FALSE
;
189 * A non-zero value for zfs_max_missing_tvds means that we allow importing
190 * pools with missing top-level vdevs. This is strictly intended for advanced
191 * pool recovery cases since missing data is almost inevitable. Pools with
192 * missing devices can only be imported read-only for safety reasons, and their
193 * fail-mode will be automatically set to "continue".
195 * With 1 missing vdev we should be able to import the pool and mount all
196 * datasets. User data that was not modified after the missing device has been
197 * added should be recoverable. This means that snapshots created prior to the
198 * addition of that device should be completely intact.
200 * With 2 missing vdevs, some datasets may fail to mount since there are
201 * dataset statistics that are stored as regular metadata. Some data might be
202 * recoverable if those vdevs were added recently.
204 * With 3 or more missing vdevs, the pool is severely damaged and MOS entries
205 * may be missing entirely. Chances of data recovery are very low. Note that
206 * there are also risks of performing an inadvertent rewind as we might be
207 * missing all the vdevs with the latest uberblocks.
209 unsigned long zfs_max_missing_tvds
= 0;
212 * The parameters below are similar to zfs_max_missing_tvds but are only
213 * intended for a preliminary open of the pool with an untrusted config which
214 * might be incomplete or out-dated.
216 * We are more tolerant for pools opened from a cachefile since we could have
217 * an out-dated cachefile where a device removal was not registered.
218 * We could have set the limit arbitrarily high but in the case where devices
219 * are really missing we would want to return the proper error codes; we chose
220 * SPA_DVAS_PER_BP - 1 so that some copies of the MOS would still be available
221 * and we get a chance to retrieve the trusted config.
223 uint64_t zfs_max_missing_tvds_cachefile
= SPA_DVAS_PER_BP
- 1;
225 * In the case where config was assembled by scanning device paths (/dev/dsks
226 * by default) we are less tolerant since all the existing devices should have
227 * been detected and we want spa_load to return the right error codes.
229 uint64_t zfs_max_missing_tvds_scan
= 0;
232 * ==========================================================================
233 * SPA properties routines
234 * ==========================================================================
238 * Add a (source=src, propname=propval) list to an nvlist.
241 spa_prop_add_list(nvlist_t
*nvl
, zpool_prop_t prop
, char *strval
,
242 uint64_t intval
, zprop_source_t src
)
244 const char *propname
= zpool_prop_to_name(prop
);
247 VERIFY(nvlist_alloc(&propval
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
248 VERIFY(nvlist_add_uint64(propval
, ZPROP_SOURCE
, src
) == 0);
251 VERIFY(nvlist_add_string(propval
, ZPROP_VALUE
, strval
) == 0);
253 VERIFY(nvlist_add_uint64(propval
, ZPROP_VALUE
, intval
) == 0);
255 VERIFY(nvlist_add_nvlist(nvl
, propname
, propval
) == 0);
256 nvlist_free(propval
);
260 * Get property values from the spa configuration.
263 spa_prop_get_config(spa_t
*spa
, nvlist_t
**nvp
)
265 vdev_t
*rvd
= spa
->spa_root_vdev
;
266 dsl_pool_t
*pool
= spa
->spa_dsl_pool
;
267 uint64_t size
, alloc
, cap
, version
;
268 const zprop_source_t src
= ZPROP_SRC_NONE
;
269 spa_config_dirent_t
*dp
;
270 metaslab_class_t
*mc
= spa_normal_class(spa
);
272 ASSERT(MUTEX_HELD(&spa
->spa_props_lock
));
275 alloc
= metaslab_class_get_alloc(spa_normal_class(spa
));
276 size
= metaslab_class_get_space(spa_normal_class(spa
));
277 spa_prop_add_list(*nvp
, ZPOOL_PROP_NAME
, spa_name(spa
), 0, src
);
278 spa_prop_add_list(*nvp
, ZPOOL_PROP_SIZE
, NULL
, size
, src
);
279 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALLOCATED
, NULL
, alloc
, src
);
280 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREE
, NULL
,
283 spa_prop_add_list(*nvp
, ZPOOL_PROP_FRAGMENTATION
, NULL
,
284 metaslab_class_fragmentation(mc
), src
);
285 spa_prop_add_list(*nvp
, ZPOOL_PROP_EXPANDSZ
, NULL
,
286 metaslab_class_expandable_space(mc
), src
);
287 spa_prop_add_list(*nvp
, ZPOOL_PROP_READONLY
, NULL
,
288 (spa_mode(spa
) == FREAD
), src
);
290 cap
= (size
== 0) ? 0 : (alloc
* 100 / size
);
291 spa_prop_add_list(*nvp
, ZPOOL_PROP_CAPACITY
, NULL
, cap
, src
);
293 spa_prop_add_list(*nvp
, ZPOOL_PROP_DEDUPRATIO
, NULL
,
294 ddt_get_pool_dedup_ratio(spa
), src
);
296 spa_prop_add_list(*nvp
, ZPOOL_PROP_HEALTH
, NULL
,
297 rvd
->vdev_state
, src
);
299 version
= spa_version(spa
);
300 if (version
== zpool_prop_default_numeric(ZPOOL_PROP_VERSION
)) {
301 spa_prop_add_list(*nvp
, ZPOOL_PROP_VERSION
, NULL
,
302 version
, ZPROP_SRC_DEFAULT
);
304 spa_prop_add_list(*nvp
, ZPOOL_PROP_VERSION
, NULL
,
305 version
, ZPROP_SRC_LOCAL
);
311 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
312 * when opening pools before this version freedir will be NULL.
314 if (pool
->dp_free_dir
!= NULL
) {
315 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
, NULL
,
316 dsl_dir_phys(pool
->dp_free_dir
)->dd_used_bytes
,
319 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
,
323 if (pool
->dp_leak_dir
!= NULL
) {
324 spa_prop_add_list(*nvp
, ZPOOL_PROP_LEAKED
, NULL
,
325 dsl_dir_phys(pool
->dp_leak_dir
)->dd_used_bytes
,
328 spa_prop_add_list(*nvp
, ZPOOL_PROP_LEAKED
,
333 spa_prop_add_list(*nvp
, ZPOOL_PROP_GUID
, NULL
, spa_guid(spa
), src
);
335 if (spa
->spa_comment
!= NULL
) {
336 spa_prop_add_list(*nvp
, ZPOOL_PROP_COMMENT
, spa
->spa_comment
,
340 if (spa
->spa_root
!= NULL
)
341 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALTROOT
, spa
->spa_root
,
344 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_BLOCKS
)) {
345 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXBLOCKSIZE
, NULL
,
346 MIN(zfs_max_recordsize
, SPA_MAXBLOCKSIZE
), ZPROP_SRC_NONE
);
348 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXBLOCKSIZE
, NULL
,
349 SPA_OLD_MAXBLOCKSIZE
, ZPROP_SRC_NONE
);
352 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_DNODE
)) {
353 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXDNODESIZE
, NULL
,
354 DNODE_MAX_SIZE
, ZPROP_SRC_NONE
);
356 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXDNODESIZE
, NULL
,
357 DNODE_MIN_SIZE
, ZPROP_SRC_NONE
);
360 if ((dp
= list_head(&spa
->spa_config_list
)) != NULL
) {
361 if (dp
->scd_path
== NULL
) {
362 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
363 "none", 0, ZPROP_SRC_LOCAL
);
364 } else if (strcmp(dp
->scd_path
, spa_config_path
) != 0) {
365 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
366 dp
->scd_path
, 0, ZPROP_SRC_LOCAL
);
372 * Get zpool property values.
375 spa_prop_get(spa_t
*spa
, nvlist_t
**nvp
)
377 objset_t
*mos
= spa
->spa_meta_objset
;
382 err
= nvlist_alloc(nvp
, NV_UNIQUE_NAME
, KM_SLEEP
);
386 mutex_enter(&spa
->spa_props_lock
);
389 * Get properties from the spa config.
391 spa_prop_get_config(spa
, nvp
);
393 /* If no pool property object, no more prop to get. */
394 if (mos
== NULL
|| spa
->spa_pool_props_object
== 0) {
395 mutex_exit(&spa
->spa_props_lock
);
400 * Get properties from the MOS pool property object.
402 for (zap_cursor_init(&zc
, mos
, spa
->spa_pool_props_object
);
403 (err
= zap_cursor_retrieve(&zc
, &za
)) == 0;
404 zap_cursor_advance(&zc
)) {
407 zprop_source_t src
= ZPROP_SRC_DEFAULT
;
410 if ((prop
= zpool_name_to_prop(za
.za_name
)) == ZPOOL_PROP_INVAL
)
413 switch (za
.za_integer_length
) {
415 /* integer property */
416 if (za
.za_first_integer
!=
417 zpool_prop_default_numeric(prop
))
418 src
= ZPROP_SRC_LOCAL
;
420 if (prop
== ZPOOL_PROP_BOOTFS
) {
422 dsl_dataset_t
*ds
= NULL
;
424 dp
= spa_get_dsl(spa
);
425 dsl_pool_config_enter(dp
, FTAG
);
426 if ((err
= dsl_dataset_hold_obj(dp
,
427 za
.za_first_integer
, FTAG
, &ds
))) {
428 dsl_pool_config_exit(dp
, FTAG
);
432 strval
= kmem_alloc(ZFS_MAX_DATASET_NAME_LEN
,
434 dsl_dataset_name(ds
, strval
);
435 dsl_dataset_rele(ds
, FTAG
);
436 dsl_pool_config_exit(dp
, FTAG
);
439 intval
= za
.za_first_integer
;
442 spa_prop_add_list(*nvp
, prop
, strval
, intval
, src
);
445 kmem_free(strval
, ZFS_MAX_DATASET_NAME_LEN
);
450 /* string property */
451 strval
= kmem_alloc(za
.za_num_integers
, KM_SLEEP
);
452 err
= zap_lookup(mos
, spa
->spa_pool_props_object
,
453 za
.za_name
, 1, za
.za_num_integers
, strval
);
455 kmem_free(strval
, za
.za_num_integers
);
458 spa_prop_add_list(*nvp
, prop
, strval
, 0, src
);
459 kmem_free(strval
, za
.za_num_integers
);
466 zap_cursor_fini(&zc
);
467 mutex_exit(&spa
->spa_props_lock
);
469 if (err
&& err
!= ENOENT
) {
479 * Validate the given pool properties nvlist and modify the list
480 * for the property values to be set.
483 spa_prop_validate(spa_t
*spa
, nvlist_t
*props
)
486 int error
= 0, reset_bootfs
= 0;
488 boolean_t has_feature
= B_FALSE
;
491 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
493 char *strval
, *slash
, *check
, *fname
;
494 const char *propname
= nvpair_name(elem
);
495 zpool_prop_t prop
= zpool_name_to_prop(propname
);
498 case ZPOOL_PROP_INVAL
:
499 if (!zpool_prop_feature(propname
)) {
500 error
= SET_ERROR(EINVAL
);
505 * Sanitize the input.
507 if (nvpair_type(elem
) != DATA_TYPE_UINT64
) {
508 error
= SET_ERROR(EINVAL
);
512 if (nvpair_value_uint64(elem
, &intval
) != 0) {
513 error
= SET_ERROR(EINVAL
);
518 error
= SET_ERROR(EINVAL
);
522 fname
= strchr(propname
, '@') + 1;
523 if (zfeature_lookup_name(fname
, NULL
) != 0) {
524 error
= SET_ERROR(EINVAL
);
528 has_feature
= B_TRUE
;
531 case ZPOOL_PROP_VERSION
:
532 error
= nvpair_value_uint64(elem
, &intval
);
534 (intval
< spa_version(spa
) ||
535 intval
> SPA_VERSION_BEFORE_FEATURES
||
537 error
= SET_ERROR(EINVAL
);
540 case ZPOOL_PROP_DELEGATION
:
541 case ZPOOL_PROP_AUTOREPLACE
:
542 case ZPOOL_PROP_LISTSNAPS
:
543 case ZPOOL_PROP_AUTOEXPAND
:
544 error
= nvpair_value_uint64(elem
, &intval
);
545 if (!error
&& intval
> 1)
546 error
= SET_ERROR(EINVAL
);
549 case ZPOOL_PROP_MULTIHOST
:
550 error
= nvpair_value_uint64(elem
, &intval
);
551 if (!error
&& intval
> 1)
552 error
= SET_ERROR(EINVAL
);
554 if (!error
&& !spa_get_hostid())
555 error
= SET_ERROR(ENOTSUP
);
559 case ZPOOL_PROP_BOOTFS
:
561 * If the pool version is less than SPA_VERSION_BOOTFS,
562 * or the pool is still being created (version == 0),
563 * the bootfs property cannot be set.
565 if (spa_version(spa
) < SPA_VERSION_BOOTFS
) {
566 error
= SET_ERROR(ENOTSUP
);
571 * Make sure the vdev config is bootable
573 if (!vdev_is_bootable(spa
->spa_root_vdev
)) {
574 error
= SET_ERROR(ENOTSUP
);
580 error
= nvpair_value_string(elem
, &strval
);
586 if (strval
== NULL
|| strval
[0] == '\0') {
587 objnum
= zpool_prop_default_numeric(
592 error
= dmu_objset_hold(strval
, FTAG
, &os
);
597 * Must be ZPL, and its property settings
598 * must be supported by GRUB (compression
599 * is not gzip, and large blocks or large
600 * dnodes are not used).
603 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
604 error
= SET_ERROR(ENOTSUP
);
606 dsl_prop_get_int_ds(dmu_objset_ds(os
),
607 zfs_prop_to_name(ZFS_PROP_COMPRESSION
),
609 !BOOTFS_COMPRESS_VALID(propval
)) {
610 error
= SET_ERROR(ENOTSUP
);
612 dsl_prop_get_int_ds(dmu_objset_ds(os
),
613 zfs_prop_to_name(ZFS_PROP_DNODESIZE
),
615 propval
!= ZFS_DNSIZE_LEGACY
) {
616 error
= SET_ERROR(ENOTSUP
);
618 objnum
= dmu_objset_id(os
);
620 dmu_objset_rele(os
, FTAG
);
624 case ZPOOL_PROP_FAILUREMODE
:
625 error
= nvpair_value_uint64(elem
, &intval
);
626 if (!error
&& intval
> ZIO_FAILURE_MODE_PANIC
)
627 error
= SET_ERROR(EINVAL
);
630 * This is a special case which only occurs when
631 * the pool has completely failed. This allows
632 * the user to change the in-core failmode property
633 * without syncing it out to disk (I/Os might
634 * currently be blocked). We do this by returning
635 * EIO to the caller (spa_prop_set) to trick it
636 * into thinking we encountered a property validation
639 if (!error
&& spa_suspended(spa
)) {
640 spa
->spa_failmode
= intval
;
641 error
= SET_ERROR(EIO
);
645 case ZPOOL_PROP_CACHEFILE
:
646 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
649 if (strval
[0] == '\0')
652 if (strcmp(strval
, "none") == 0)
655 if (strval
[0] != '/') {
656 error
= SET_ERROR(EINVAL
);
660 slash
= strrchr(strval
, '/');
661 ASSERT(slash
!= NULL
);
663 if (slash
[1] == '\0' || strcmp(slash
, "/.") == 0 ||
664 strcmp(slash
, "/..") == 0)
665 error
= SET_ERROR(EINVAL
);
668 case ZPOOL_PROP_COMMENT
:
669 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
671 for (check
= strval
; *check
!= '\0'; check
++) {
672 if (!isprint(*check
)) {
673 error
= SET_ERROR(EINVAL
);
677 if (strlen(strval
) > ZPROP_MAX_COMMENT
)
678 error
= SET_ERROR(E2BIG
);
681 case ZPOOL_PROP_DEDUPDITTO
:
682 if (spa_version(spa
) < SPA_VERSION_DEDUP
)
683 error
= SET_ERROR(ENOTSUP
);
685 error
= nvpair_value_uint64(elem
, &intval
);
687 intval
!= 0 && intval
< ZIO_DEDUPDITTO_MIN
)
688 error
= SET_ERROR(EINVAL
);
699 if (!error
&& reset_bootfs
) {
700 error
= nvlist_remove(props
,
701 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), DATA_TYPE_STRING
);
704 error
= nvlist_add_uint64(props
,
705 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), objnum
);
713 spa_configfile_set(spa_t
*spa
, nvlist_t
*nvp
, boolean_t need_sync
)
716 spa_config_dirent_t
*dp
;
718 if (nvlist_lookup_string(nvp
, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
),
722 dp
= kmem_alloc(sizeof (spa_config_dirent_t
),
725 if (cachefile
[0] == '\0')
726 dp
->scd_path
= spa_strdup(spa_config_path
);
727 else if (strcmp(cachefile
, "none") == 0)
730 dp
->scd_path
= spa_strdup(cachefile
);
732 list_insert_head(&spa
->spa_config_list
, dp
);
734 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
738 spa_prop_set(spa_t
*spa
, nvlist_t
*nvp
)
741 nvpair_t
*elem
= NULL
;
742 boolean_t need_sync
= B_FALSE
;
744 if ((error
= spa_prop_validate(spa
, nvp
)) != 0)
747 while ((elem
= nvlist_next_nvpair(nvp
, elem
)) != NULL
) {
748 zpool_prop_t prop
= zpool_name_to_prop(nvpair_name(elem
));
750 if (prop
== ZPOOL_PROP_CACHEFILE
||
751 prop
== ZPOOL_PROP_ALTROOT
||
752 prop
== ZPOOL_PROP_READONLY
)
755 if (prop
== ZPOOL_PROP_VERSION
|| prop
== ZPOOL_PROP_INVAL
) {
758 if (prop
== ZPOOL_PROP_VERSION
) {
759 VERIFY(nvpair_value_uint64(elem
, &ver
) == 0);
761 ASSERT(zpool_prop_feature(nvpair_name(elem
)));
762 ver
= SPA_VERSION_FEATURES
;
766 /* Save time if the version is already set. */
767 if (ver
== spa_version(spa
))
771 * In addition to the pool directory object, we might
772 * create the pool properties object, the features for
773 * read object, the features for write object, or the
774 * feature descriptions object.
776 error
= dsl_sync_task(spa
->spa_name
, NULL
,
777 spa_sync_version
, &ver
,
778 6, ZFS_SPACE_CHECK_RESERVED
);
789 return (dsl_sync_task(spa
->spa_name
, NULL
, spa_sync_props
,
790 nvp
, 6, ZFS_SPACE_CHECK_RESERVED
));
797 * If the bootfs property value is dsobj, clear it.
800 spa_prop_clear_bootfs(spa_t
*spa
, uint64_t dsobj
, dmu_tx_t
*tx
)
802 if (spa
->spa_bootfs
== dsobj
&& spa
->spa_pool_props_object
!= 0) {
803 VERIFY(zap_remove(spa
->spa_meta_objset
,
804 spa
->spa_pool_props_object
,
805 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), tx
) == 0);
812 spa_change_guid_check(void *arg
, dmu_tx_t
*tx
)
814 ASSERTV(uint64_t *newguid
= arg
);
815 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
816 vdev_t
*rvd
= spa
->spa_root_vdev
;
819 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
820 vdev_state
= rvd
->vdev_state
;
821 spa_config_exit(spa
, SCL_STATE
, FTAG
);
823 if (vdev_state
!= VDEV_STATE_HEALTHY
)
824 return (SET_ERROR(ENXIO
));
826 ASSERT3U(spa_guid(spa
), !=, *newguid
);
832 spa_change_guid_sync(void *arg
, dmu_tx_t
*tx
)
834 uint64_t *newguid
= arg
;
835 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
837 vdev_t
*rvd
= spa
->spa_root_vdev
;
839 oldguid
= spa_guid(spa
);
841 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
842 rvd
->vdev_guid
= *newguid
;
843 rvd
->vdev_guid_sum
+= (*newguid
- oldguid
);
844 vdev_config_dirty(rvd
);
845 spa_config_exit(spa
, SCL_STATE
, FTAG
);
847 spa_history_log_internal(spa
, "guid change", tx
, "old=%llu new=%llu",
852 * Change the GUID for the pool. This is done so that we can later
853 * re-import a pool built from a clone of our own vdevs. We will modify
854 * the root vdev's guid, our own pool guid, and then mark all of our
855 * vdevs dirty. Note that we must make sure that all our vdevs are
856 * online when we do this, or else any vdevs that weren't present
857 * would be orphaned from our pool. We are also going to issue a
858 * sysevent to update any watchers.
861 spa_change_guid(spa_t
*spa
)
866 mutex_enter(&spa
->spa_vdev_top_lock
);
867 mutex_enter(&spa_namespace_lock
);
868 guid
= spa_generate_guid(NULL
);
870 error
= dsl_sync_task(spa
->spa_name
, spa_change_guid_check
,
871 spa_change_guid_sync
, &guid
, 5, ZFS_SPACE_CHECK_RESERVED
);
874 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
);
875 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_REGUID
);
878 mutex_exit(&spa_namespace_lock
);
879 mutex_exit(&spa
->spa_vdev_top_lock
);
885 * ==========================================================================
886 * SPA state manipulation (open/create/destroy/import/export)
887 * ==========================================================================
891 spa_error_entry_compare(const void *a
, const void *b
)
893 const spa_error_entry_t
*sa
= (const spa_error_entry_t
*)a
;
894 const spa_error_entry_t
*sb
= (const spa_error_entry_t
*)b
;
897 ret
= memcmp(&sa
->se_bookmark
, &sb
->se_bookmark
,
898 sizeof (zbookmark_phys_t
));
900 return (AVL_ISIGN(ret
));
904 * Utility function which retrieves copies of the current logs and
905 * re-initializes them in the process.
908 spa_get_errlists(spa_t
*spa
, avl_tree_t
*last
, avl_tree_t
*scrub
)
910 ASSERT(MUTEX_HELD(&spa
->spa_errlist_lock
));
912 bcopy(&spa
->spa_errlist_last
, last
, sizeof (avl_tree_t
));
913 bcopy(&spa
->spa_errlist_scrub
, scrub
, sizeof (avl_tree_t
));
915 avl_create(&spa
->spa_errlist_scrub
,
916 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
917 offsetof(spa_error_entry_t
, se_avl
));
918 avl_create(&spa
->spa_errlist_last
,
919 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
920 offsetof(spa_error_entry_t
, se_avl
));
924 spa_taskqs_init(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
926 const zio_taskq_info_t
*ztip
= &zio_taskqs
[t
][q
];
927 enum zti_modes mode
= ztip
->zti_mode
;
928 uint_t value
= ztip
->zti_value
;
929 uint_t count
= ztip
->zti_count
;
930 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
933 boolean_t batch
= B_FALSE
;
935 if (mode
== ZTI_MODE_NULL
) {
937 tqs
->stqs_taskq
= NULL
;
941 ASSERT3U(count
, >, 0);
943 tqs
->stqs_count
= count
;
944 tqs
->stqs_taskq
= kmem_alloc(count
* sizeof (taskq_t
*), KM_SLEEP
);
948 ASSERT3U(value
, >=, 1);
949 value
= MAX(value
, 1);
950 flags
|= TASKQ_DYNAMIC
;
955 flags
|= TASKQ_THREADS_CPU_PCT
;
956 value
= MIN(zio_taskq_batch_pct
, 100);
960 panic("unrecognized mode for %s_%s taskq (%u:%u) in "
962 zio_type_name
[t
], zio_taskq_types
[q
], mode
, value
);
966 for (uint_t i
= 0; i
< count
; i
++) {
970 (void) snprintf(name
, sizeof (name
), "%s_%s_%u",
971 zio_type_name
[t
], zio_taskq_types
[q
], i
);
973 (void) snprintf(name
, sizeof (name
), "%s_%s",
974 zio_type_name
[t
], zio_taskq_types
[q
]);
977 if (zio_taskq_sysdc
&& spa
->spa_proc
!= &p0
) {
979 flags
|= TASKQ_DC_BATCH
;
981 tq
= taskq_create_sysdc(name
, value
, 50, INT_MAX
,
982 spa
->spa_proc
, zio_taskq_basedc
, flags
);
984 pri_t pri
= maxclsyspri
;
986 * The write issue taskq can be extremely CPU
987 * intensive. Run it at slightly less important
988 * priority than the other taskqs. Under Linux this
989 * means incrementing the priority value on platforms
990 * like illumos it should be decremented.
992 if (t
== ZIO_TYPE_WRITE
&& q
== ZIO_TASKQ_ISSUE
)
995 tq
= taskq_create_proc(name
, value
, pri
, 50,
996 INT_MAX
, spa
->spa_proc
, flags
);
999 tqs
->stqs_taskq
[i
] = tq
;
1004 spa_taskqs_fini(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
1006 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1008 if (tqs
->stqs_taskq
== NULL
) {
1009 ASSERT3U(tqs
->stqs_count
, ==, 0);
1013 for (uint_t i
= 0; i
< tqs
->stqs_count
; i
++) {
1014 ASSERT3P(tqs
->stqs_taskq
[i
], !=, NULL
);
1015 taskq_destroy(tqs
->stqs_taskq
[i
]);
1018 kmem_free(tqs
->stqs_taskq
, tqs
->stqs_count
* sizeof (taskq_t
*));
1019 tqs
->stqs_taskq
= NULL
;
1023 * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority.
1024 * Note that a type may have multiple discrete taskqs to avoid lock contention
1025 * on the taskq itself. In that case we choose which taskq at random by using
1026 * the low bits of gethrtime().
1029 spa_taskq_dispatch_ent(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
1030 task_func_t
*func
, void *arg
, uint_t flags
, taskq_ent_t
*ent
)
1032 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1035 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
1036 ASSERT3U(tqs
->stqs_count
, !=, 0);
1038 if (tqs
->stqs_count
== 1) {
1039 tq
= tqs
->stqs_taskq
[0];
1041 tq
= tqs
->stqs_taskq
[((uint64_t)gethrtime()) % tqs
->stqs_count
];
1044 taskq_dispatch_ent(tq
, func
, arg
, flags
, ent
);
1048 * Same as spa_taskq_dispatch_ent() but block on the task until completion.
1051 spa_taskq_dispatch_sync(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
1052 task_func_t
*func
, void *arg
, uint_t flags
)
1054 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1058 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
1059 ASSERT3U(tqs
->stqs_count
, !=, 0);
1061 if (tqs
->stqs_count
== 1) {
1062 tq
= tqs
->stqs_taskq
[0];
1064 tq
= tqs
->stqs_taskq
[((uint64_t)gethrtime()) % tqs
->stqs_count
];
1067 id
= taskq_dispatch(tq
, func
, arg
, flags
);
1069 taskq_wait_id(tq
, id
);
1073 spa_create_zio_taskqs(spa_t
*spa
)
1075 for (int t
= 0; t
< ZIO_TYPES
; t
++) {
1076 for (int q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1077 spa_taskqs_init(spa
, t
, q
);
1083 * Disabled until spa_thread() can be adapted for Linux.
1085 #undef HAVE_SPA_THREAD
1087 #if defined(_KERNEL) && defined(HAVE_SPA_THREAD)
1089 spa_thread(void *arg
)
1091 callb_cpr_t cprinfo
;
1094 user_t
*pu
= PTOU(curproc
);
1096 CALLB_CPR_INIT(&cprinfo
, &spa
->spa_proc_lock
, callb_generic_cpr
,
1099 ASSERT(curproc
!= &p0
);
1100 (void) snprintf(pu
->u_psargs
, sizeof (pu
->u_psargs
),
1101 "zpool-%s", spa
->spa_name
);
1102 (void) strlcpy(pu
->u_comm
, pu
->u_psargs
, sizeof (pu
->u_comm
));
1104 /* bind this thread to the requested psrset */
1105 if (zio_taskq_psrset_bind
!= PS_NONE
) {
1107 mutex_enter(&cpu_lock
);
1108 mutex_enter(&pidlock
);
1109 mutex_enter(&curproc
->p_lock
);
1111 if (cpupart_bind_thread(curthread
, zio_taskq_psrset_bind
,
1112 0, NULL
, NULL
) == 0) {
1113 curthread
->t_bind_pset
= zio_taskq_psrset_bind
;
1116 "Couldn't bind process for zfs pool \"%s\" to "
1117 "pset %d\n", spa
->spa_name
, zio_taskq_psrset_bind
);
1120 mutex_exit(&curproc
->p_lock
);
1121 mutex_exit(&pidlock
);
1122 mutex_exit(&cpu_lock
);
1126 if (zio_taskq_sysdc
) {
1127 sysdc_thread_enter(curthread
, 100, 0);
1130 spa
->spa_proc
= curproc
;
1131 spa
->spa_did
= curthread
->t_did
;
1133 spa_create_zio_taskqs(spa
);
1135 mutex_enter(&spa
->spa_proc_lock
);
1136 ASSERT(spa
->spa_proc_state
== SPA_PROC_CREATED
);
1138 spa
->spa_proc_state
= SPA_PROC_ACTIVE
;
1139 cv_broadcast(&spa
->spa_proc_cv
);
1141 CALLB_CPR_SAFE_BEGIN(&cprinfo
);
1142 while (spa
->spa_proc_state
== SPA_PROC_ACTIVE
)
1143 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1144 CALLB_CPR_SAFE_END(&cprinfo
, &spa
->spa_proc_lock
);
1146 ASSERT(spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
);
1147 spa
->spa_proc_state
= SPA_PROC_GONE
;
1148 spa
->spa_proc
= &p0
;
1149 cv_broadcast(&spa
->spa_proc_cv
);
1150 CALLB_CPR_EXIT(&cprinfo
); /* drops spa_proc_lock */
1152 mutex_enter(&curproc
->p_lock
);
1158 * Activate an uninitialized pool.
1161 spa_activate(spa_t
*spa
, int mode
)
1163 ASSERT(spa
->spa_state
== POOL_STATE_UNINITIALIZED
);
1165 spa
->spa_state
= POOL_STATE_ACTIVE
;
1166 spa
->spa_mode
= mode
;
1168 spa
->spa_normal_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
1169 spa
->spa_log_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
1171 /* Try to create a covering process */
1172 mutex_enter(&spa
->spa_proc_lock
);
1173 ASSERT(spa
->spa_proc_state
== SPA_PROC_NONE
);
1174 ASSERT(spa
->spa_proc
== &p0
);
1177 #ifdef HAVE_SPA_THREAD
1178 /* Only create a process if we're going to be around a while. */
1179 if (spa_create_process
&& strcmp(spa
->spa_name
, TRYIMPORT_NAME
) != 0) {
1180 if (newproc(spa_thread
, (caddr_t
)spa
, syscid
, maxclsyspri
,
1182 spa
->spa_proc_state
= SPA_PROC_CREATED
;
1183 while (spa
->spa_proc_state
== SPA_PROC_CREATED
) {
1184 cv_wait(&spa
->spa_proc_cv
,
1185 &spa
->spa_proc_lock
);
1187 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1188 ASSERT(spa
->spa_proc
!= &p0
);
1189 ASSERT(spa
->spa_did
!= 0);
1193 "Couldn't create process for zfs pool \"%s\"\n",
1198 #endif /* HAVE_SPA_THREAD */
1199 mutex_exit(&spa
->spa_proc_lock
);
1201 /* If we didn't create a process, we need to create our taskqs. */
1202 if (spa
->spa_proc
== &p0
) {
1203 spa_create_zio_taskqs(spa
);
1206 for (size_t i
= 0; i
< TXG_SIZE
; i
++)
1207 spa
->spa_txg_zio
[i
] = zio_root(spa
, NULL
, NULL
, 0);
1209 list_create(&spa
->spa_config_dirty_list
, sizeof (vdev_t
),
1210 offsetof(vdev_t
, vdev_config_dirty_node
));
1211 list_create(&spa
->spa_evicting_os_list
, sizeof (objset_t
),
1212 offsetof(objset_t
, os_evicting_node
));
1213 list_create(&spa
->spa_state_dirty_list
, sizeof (vdev_t
),
1214 offsetof(vdev_t
, vdev_state_dirty_node
));
1216 txg_list_create(&spa
->spa_vdev_txg_list
, spa
,
1217 offsetof(struct vdev
, vdev_txg_node
));
1219 avl_create(&spa
->spa_errlist_scrub
,
1220 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1221 offsetof(spa_error_entry_t
, se_avl
));
1222 avl_create(&spa
->spa_errlist_last
,
1223 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1224 offsetof(spa_error_entry_t
, se_avl
));
1226 spa_keystore_init(&spa
->spa_keystore
);
1229 * This taskq is used to perform zvol-minor-related tasks
1230 * asynchronously. This has several advantages, including easy
1231 * resolution of various deadlocks (zfsonlinux bug #3681).
1233 * The taskq must be single threaded to ensure tasks are always
1234 * processed in the order in which they were dispatched.
1236 * A taskq per pool allows one to keep the pools independent.
1237 * This way if one pool is suspended, it will not impact another.
1239 * The preferred location to dispatch a zvol minor task is a sync
1240 * task. In this context, there is easy access to the spa_t and minimal
1241 * error handling is required because the sync task must succeed.
1243 spa
->spa_zvol_taskq
= taskq_create("z_zvol", 1, defclsyspri
,
1247 * Taskq dedicated to prefetcher threads: this is used to prevent the
1248 * pool traverse code from monopolizing the global (and limited)
1249 * system_taskq by inappropriately scheduling long running tasks on it.
1251 spa
->spa_prefetch_taskq
= taskq_create("z_prefetch", boot_ncpus
,
1252 defclsyspri
, 1, INT_MAX
, TASKQ_DYNAMIC
);
1255 * The taskq to upgrade datasets in this pool. Currently used by
1256 * feature SPA_FEATURE_USEROBJ_ACCOUNTING/SPA_FEATURE_PROJECT_QUOTA.
1258 spa
->spa_upgrade_taskq
= taskq_create("z_upgrade", boot_ncpus
,
1259 defclsyspri
, 1, INT_MAX
, TASKQ_DYNAMIC
);
1263 * Opposite of spa_activate().
1266 spa_deactivate(spa_t
*spa
)
1268 ASSERT(spa
->spa_sync_on
== B_FALSE
);
1269 ASSERT(spa
->spa_dsl_pool
== NULL
);
1270 ASSERT(spa
->spa_root_vdev
== NULL
);
1271 ASSERT(spa
->spa_async_zio_root
== NULL
);
1272 ASSERT(spa
->spa_state
!= POOL_STATE_UNINITIALIZED
);
1274 spa_evicting_os_wait(spa
);
1276 if (spa
->spa_zvol_taskq
) {
1277 taskq_destroy(spa
->spa_zvol_taskq
);
1278 spa
->spa_zvol_taskq
= NULL
;
1281 if (spa
->spa_prefetch_taskq
) {
1282 taskq_destroy(spa
->spa_prefetch_taskq
);
1283 spa
->spa_prefetch_taskq
= NULL
;
1286 if (spa
->spa_upgrade_taskq
) {
1287 taskq_destroy(spa
->spa_upgrade_taskq
);
1288 spa
->spa_upgrade_taskq
= NULL
;
1291 txg_list_destroy(&spa
->spa_vdev_txg_list
);
1293 list_destroy(&spa
->spa_config_dirty_list
);
1294 list_destroy(&spa
->spa_evicting_os_list
);
1295 list_destroy(&spa
->spa_state_dirty_list
);
1297 taskq_cancel_id(system_delay_taskq
, spa
->spa_deadman_tqid
);
1299 for (int t
= 0; t
< ZIO_TYPES
; t
++) {
1300 for (int q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1301 spa_taskqs_fini(spa
, t
, q
);
1305 for (size_t i
= 0; i
< TXG_SIZE
; i
++) {
1306 ASSERT3P(spa
->spa_txg_zio
[i
], !=, NULL
);
1307 VERIFY0(zio_wait(spa
->spa_txg_zio
[i
]));
1308 spa
->spa_txg_zio
[i
] = NULL
;
1311 metaslab_class_destroy(spa
->spa_normal_class
);
1312 spa
->spa_normal_class
= NULL
;
1314 metaslab_class_destroy(spa
->spa_log_class
);
1315 spa
->spa_log_class
= NULL
;
1318 * If this was part of an import or the open otherwise failed, we may
1319 * still have errors left in the queues. Empty them just in case.
1321 spa_errlog_drain(spa
);
1322 avl_destroy(&spa
->spa_errlist_scrub
);
1323 avl_destroy(&spa
->spa_errlist_last
);
1325 spa_keystore_fini(&spa
->spa_keystore
);
1327 spa
->spa_state
= POOL_STATE_UNINITIALIZED
;
1329 mutex_enter(&spa
->spa_proc_lock
);
1330 if (spa
->spa_proc_state
!= SPA_PROC_NONE
) {
1331 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1332 spa
->spa_proc_state
= SPA_PROC_DEACTIVATE
;
1333 cv_broadcast(&spa
->spa_proc_cv
);
1334 while (spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
) {
1335 ASSERT(spa
->spa_proc
!= &p0
);
1336 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1338 ASSERT(spa
->spa_proc_state
== SPA_PROC_GONE
);
1339 spa
->spa_proc_state
= SPA_PROC_NONE
;
1341 ASSERT(spa
->spa_proc
== &p0
);
1342 mutex_exit(&spa
->spa_proc_lock
);
1345 * We want to make sure spa_thread() has actually exited the ZFS
1346 * module, so that the module can't be unloaded out from underneath
1349 if (spa
->spa_did
!= 0) {
1350 thread_join(spa
->spa_did
);
1356 * Verify a pool configuration, and construct the vdev tree appropriately. This
1357 * will create all the necessary vdevs in the appropriate layout, with each vdev
1358 * in the CLOSED state. This will prep the pool before open/creation/import.
1359 * All vdev validation is done by the vdev_alloc() routine.
1362 spa_config_parse(spa_t
*spa
, vdev_t
**vdp
, nvlist_t
*nv
, vdev_t
*parent
,
1363 uint_t id
, int atype
)
1369 if ((error
= vdev_alloc(spa
, vdp
, nv
, parent
, id
, atype
)) != 0)
1372 if ((*vdp
)->vdev_ops
->vdev_op_leaf
)
1375 error
= nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1378 if (error
== ENOENT
)
1384 return (SET_ERROR(EINVAL
));
1387 for (int c
= 0; c
< children
; c
++) {
1389 if ((error
= spa_config_parse(spa
, &vd
, child
[c
], *vdp
, c
,
1397 ASSERT(*vdp
!= NULL
);
1403 * Opposite of spa_load().
1406 spa_unload(spa_t
*spa
)
1410 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1412 spa_load_note(spa
, "UNLOADING");
1417 spa_async_suspend(spa
);
1422 if (spa
->spa_sync_on
) {
1423 txg_sync_stop(spa
->spa_dsl_pool
);
1424 spa
->spa_sync_on
= B_FALSE
;
1428 * Even though vdev_free() also calls vdev_metaslab_fini, we need
1429 * to call it earlier, before we wait for async i/o to complete.
1430 * This ensures that there is no async metaslab prefetching, by
1431 * calling taskq_wait(mg_taskq).
1433 if (spa
->spa_root_vdev
!= NULL
) {
1434 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1435 for (int c
= 0; c
< spa
->spa_root_vdev
->vdev_children
; c
++)
1436 vdev_metaslab_fini(spa
->spa_root_vdev
->vdev_child
[c
]);
1437 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1440 if (spa
->spa_mmp
.mmp_thread
)
1441 mmp_thread_stop(spa
);
1444 * Wait for any outstanding async I/O to complete.
1446 if (spa
->spa_async_zio_root
!= NULL
) {
1447 for (int i
= 0; i
< max_ncpus
; i
++)
1448 (void) zio_wait(spa
->spa_async_zio_root
[i
]);
1449 kmem_free(spa
->spa_async_zio_root
, max_ncpus
* sizeof (void *));
1450 spa
->spa_async_zio_root
= NULL
;
1453 if (spa
->spa_vdev_removal
!= NULL
) {
1454 spa_vdev_removal_destroy(spa
->spa_vdev_removal
);
1455 spa
->spa_vdev_removal
= NULL
;
1458 if (spa
->spa_condense_zthr
!= NULL
) {
1459 ASSERT(!zthr_isrunning(spa
->spa_condense_zthr
));
1460 zthr_destroy(spa
->spa_condense_zthr
);
1461 spa
->spa_condense_zthr
= NULL
;
1464 spa_condense_fini(spa
);
1466 bpobj_close(&spa
->spa_deferred_bpobj
);
1468 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1473 if (spa
->spa_root_vdev
)
1474 vdev_free(spa
->spa_root_vdev
);
1475 ASSERT(spa
->spa_root_vdev
== NULL
);
1478 * Close the dsl pool.
1480 if (spa
->spa_dsl_pool
) {
1481 dsl_pool_close(spa
->spa_dsl_pool
);
1482 spa
->spa_dsl_pool
= NULL
;
1483 spa
->spa_meta_objset
= NULL
;
1489 * Drop and purge level 2 cache
1491 spa_l2cache_drop(spa
);
1493 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1494 vdev_free(spa
->spa_spares
.sav_vdevs
[i
]);
1495 if (spa
->spa_spares
.sav_vdevs
) {
1496 kmem_free(spa
->spa_spares
.sav_vdevs
,
1497 spa
->spa_spares
.sav_count
* sizeof (void *));
1498 spa
->spa_spares
.sav_vdevs
= NULL
;
1500 if (spa
->spa_spares
.sav_config
) {
1501 nvlist_free(spa
->spa_spares
.sav_config
);
1502 spa
->spa_spares
.sav_config
= NULL
;
1504 spa
->spa_spares
.sav_count
= 0;
1506 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
1507 vdev_clear_stats(spa
->spa_l2cache
.sav_vdevs
[i
]);
1508 vdev_free(spa
->spa_l2cache
.sav_vdevs
[i
]);
1510 if (spa
->spa_l2cache
.sav_vdevs
) {
1511 kmem_free(spa
->spa_l2cache
.sav_vdevs
,
1512 spa
->spa_l2cache
.sav_count
* sizeof (void *));
1513 spa
->spa_l2cache
.sav_vdevs
= NULL
;
1515 if (spa
->spa_l2cache
.sav_config
) {
1516 nvlist_free(spa
->spa_l2cache
.sav_config
);
1517 spa
->spa_l2cache
.sav_config
= NULL
;
1519 spa
->spa_l2cache
.sav_count
= 0;
1521 spa
->spa_async_suspended
= 0;
1523 spa
->spa_indirect_vdevs_loaded
= B_FALSE
;
1525 if (spa
->spa_comment
!= NULL
) {
1526 spa_strfree(spa
->spa_comment
);
1527 spa
->spa_comment
= NULL
;
1530 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1534 * Load (or re-load) the current list of vdevs describing the active spares for
1535 * this pool. When this is called, we have some form of basic information in
1536 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1537 * then re-generate a more complete list including status information.
1540 spa_load_spares(spa_t
*spa
)
1547 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1550 * First, close and free any existing spare vdevs.
1552 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1553 vd
= spa
->spa_spares
.sav_vdevs
[i
];
1555 /* Undo the call to spa_activate() below */
1556 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1557 B_FALSE
)) != NULL
&& tvd
->vdev_isspare
)
1558 spa_spare_remove(tvd
);
1563 if (spa
->spa_spares
.sav_vdevs
)
1564 kmem_free(spa
->spa_spares
.sav_vdevs
,
1565 spa
->spa_spares
.sav_count
* sizeof (void *));
1567 if (spa
->spa_spares
.sav_config
== NULL
)
1570 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
1571 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
1573 spa
->spa_spares
.sav_count
= (int)nspares
;
1574 spa
->spa_spares
.sav_vdevs
= NULL
;
1580 * Construct the array of vdevs, opening them to get status in the
1581 * process. For each spare, there is potentially two different vdev_t
1582 * structures associated with it: one in the list of spares (used only
1583 * for basic validation purposes) and one in the active vdev
1584 * configuration (if it's spared in). During this phase we open and
1585 * validate each vdev on the spare list. If the vdev also exists in the
1586 * active configuration, then we also mark this vdev as an active spare.
1588 spa
->spa_spares
.sav_vdevs
= kmem_zalloc(nspares
* sizeof (void *),
1590 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1591 VERIFY(spa_config_parse(spa
, &vd
, spares
[i
], NULL
, 0,
1592 VDEV_ALLOC_SPARE
) == 0);
1595 spa
->spa_spares
.sav_vdevs
[i
] = vd
;
1597 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1598 B_FALSE
)) != NULL
) {
1599 if (!tvd
->vdev_isspare
)
1603 * We only mark the spare active if we were successfully
1604 * able to load the vdev. Otherwise, importing a pool
1605 * with a bad active spare would result in strange
1606 * behavior, because multiple pool would think the spare
1607 * is actively in use.
1609 * There is a vulnerability here to an equally bizarre
1610 * circumstance, where a dead active spare is later
1611 * brought back to life (onlined or otherwise). Given
1612 * the rarity of this scenario, and the extra complexity
1613 * it adds, we ignore the possibility.
1615 if (!vdev_is_dead(tvd
))
1616 spa_spare_activate(tvd
);
1620 vd
->vdev_aux
= &spa
->spa_spares
;
1622 if (vdev_open(vd
) != 0)
1625 if (vdev_validate_aux(vd
) == 0)
1630 * Recompute the stashed list of spares, with status information
1633 VERIFY(nvlist_remove(spa
->spa_spares
.sav_config
, ZPOOL_CONFIG_SPARES
,
1634 DATA_TYPE_NVLIST_ARRAY
) == 0);
1636 spares
= kmem_alloc(spa
->spa_spares
.sav_count
* sizeof (void *),
1638 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1639 spares
[i
] = vdev_config_generate(spa
,
1640 spa
->spa_spares
.sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_SPARE
);
1641 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
1642 ZPOOL_CONFIG_SPARES
, spares
, spa
->spa_spares
.sav_count
) == 0);
1643 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1644 nvlist_free(spares
[i
]);
1645 kmem_free(spares
, spa
->spa_spares
.sav_count
* sizeof (void *));
1649 * Load (or re-load) the current list of vdevs describing the active l2cache for
1650 * this pool. When this is called, we have some form of basic information in
1651 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1652 * then re-generate a more complete list including status information.
1653 * Devices which are already active have their details maintained, and are
1657 spa_load_l2cache(spa_t
*spa
)
1659 nvlist_t
**l2cache
= NULL
;
1661 int i
, j
, oldnvdevs
;
1663 vdev_t
*vd
, **oldvdevs
, **newvdevs
;
1664 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
1666 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1668 oldvdevs
= sav
->sav_vdevs
;
1669 oldnvdevs
= sav
->sav_count
;
1670 sav
->sav_vdevs
= NULL
;
1673 if (sav
->sav_config
== NULL
) {
1679 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
,
1680 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
1681 newvdevs
= kmem_alloc(nl2cache
* sizeof (void *), KM_SLEEP
);
1684 * Process new nvlist of vdevs.
1686 for (i
= 0; i
< nl2cache
; i
++) {
1687 VERIFY(nvlist_lookup_uint64(l2cache
[i
], ZPOOL_CONFIG_GUID
,
1691 for (j
= 0; j
< oldnvdevs
; j
++) {
1693 if (vd
!= NULL
&& guid
== vd
->vdev_guid
) {
1695 * Retain previous vdev for add/remove ops.
1703 if (newvdevs
[i
] == NULL
) {
1707 VERIFY(spa_config_parse(spa
, &vd
, l2cache
[i
], NULL
, 0,
1708 VDEV_ALLOC_L2CACHE
) == 0);
1713 * Commit this vdev as an l2cache device,
1714 * even if it fails to open.
1716 spa_l2cache_add(vd
);
1721 spa_l2cache_activate(vd
);
1723 if (vdev_open(vd
) != 0)
1726 (void) vdev_validate_aux(vd
);
1728 if (!vdev_is_dead(vd
))
1729 l2arc_add_vdev(spa
, vd
);
1733 sav
->sav_vdevs
= newvdevs
;
1734 sav
->sav_count
= (int)nl2cache
;
1737 * Recompute the stashed list of l2cache devices, with status
1738 * information this time.
1740 VERIFY(nvlist_remove(sav
->sav_config
, ZPOOL_CONFIG_L2CACHE
,
1741 DATA_TYPE_NVLIST_ARRAY
) == 0);
1743 if (sav
->sav_count
> 0)
1744 l2cache
= kmem_alloc(sav
->sav_count
* sizeof (void *),
1746 for (i
= 0; i
< sav
->sav_count
; i
++)
1747 l2cache
[i
] = vdev_config_generate(spa
,
1748 sav
->sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_L2CACHE
);
1749 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
,
1750 ZPOOL_CONFIG_L2CACHE
, l2cache
, sav
->sav_count
) == 0);
1754 * Purge vdevs that were dropped
1756 for (i
= 0; i
< oldnvdevs
; i
++) {
1761 ASSERT(vd
->vdev_isl2cache
);
1763 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
1764 pool
!= 0ULL && l2arc_vdev_present(vd
))
1765 l2arc_remove_vdev(vd
);
1766 vdev_clear_stats(vd
);
1772 kmem_free(oldvdevs
, oldnvdevs
* sizeof (void *));
1774 for (i
= 0; i
< sav
->sav_count
; i
++)
1775 nvlist_free(l2cache
[i
]);
1777 kmem_free(l2cache
, sav
->sav_count
* sizeof (void *));
1781 load_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
**value
)
1784 char *packed
= NULL
;
1789 error
= dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
);
1793 nvsize
= *(uint64_t *)db
->db_data
;
1794 dmu_buf_rele(db
, FTAG
);
1796 packed
= vmem_alloc(nvsize
, KM_SLEEP
);
1797 error
= dmu_read(spa
->spa_meta_objset
, obj
, 0, nvsize
, packed
,
1800 error
= nvlist_unpack(packed
, nvsize
, value
, 0);
1801 vmem_free(packed
, nvsize
);
1807 * Concrete top-level vdevs that are not missing and are not logs. At every
1808 * spa_sync we write new uberblocks to at least SPA_SYNC_MIN_VDEVS core tvds.
1811 spa_healthy_core_tvds(spa_t
*spa
)
1813 vdev_t
*rvd
= spa
->spa_root_vdev
;
1816 for (uint64_t i
= 0; i
< rvd
->vdev_children
; i
++) {
1817 vdev_t
*vd
= rvd
->vdev_child
[i
];
1820 if (vdev_is_concrete(vd
) && !vdev_is_dead(vd
))
1828 * Checks to see if the given vdev could not be opened, in which case we post a
1829 * sysevent to notify the autoreplace code that the device has been removed.
1832 spa_check_removed(vdev_t
*vd
)
1834 for (uint64_t c
= 0; c
< vd
->vdev_children
; c
++)
1835 spa_check_removed(vd
->vdev_child
[c
]);
1837 if (vd
->vdev_ops
->vdev_op_leaf
&& vdev_is_dead(vd
) &&
1838 vdev_is_concrete(vd
)) {
1839 zfs_post_autoreplace(vd
->vdev_spa
, vd
);
1840 spa_event_notify(vd
->vdev_spa
, vd
, NULL
, ESC_ZFS_VDEV_CHECK
);
1845 spa_check_for_missing_logs(spa_t
*spa
)
1847 vdev_t
*rvd
= spa
->spa_root_vdev
;
1850 * If we're doing a normal import, then build up any additional
1851 * diagnostic information about missing log devices.
1852 * We'll pass this up to the user for further processing.
1854 if (!(spa
->spa_import_flags
& ZFS_IMPORT_MISSING_LOG
)) {
1855 nvlist_t
**child
, *nv
;
1858 child
= kmem_alloc(rvd
->vdev_children
* sizeof (nvlist_t
*),
1860 VERIFY(nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
1862 for (uint64_t c
= 0; c
< rvd
->vdev_children
; c
++) {
1863 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1866 * We consider a device as missing only if it failed
1867 * to open (i.e. offline or faulted is not considered
1870 if (tvd
->vdev_islog
&&
1871 tvd
->vdev_state
== VDEV_STATE_CANT_OPEN
) {
1872 child
[idx
++] = vdev_config_generate(spa
, tvd
,
1873 B_FALSE
, VDEV_CONFIG_MISSING
);
1878 fnvlist_add_nvlist_array(nv
,
1879 ZPOOL_CONFIG_CHILDREN
, child
, idx
);
1880 fnvlist_add_nvlist(spa
->spa_load_info
,
1881 ZPOOL_CONFIG_MISSING_DEVICES
, nv
);
1883 for (uint64_t i
= 0; i
< idx
; i
++)
1884 nvlist_free(child
[i
]);
1887 kmem_free(child
, rvd
->vdev_children
* sizeof (char **));
1890 spa_load_failed(spa
, "some log devices are missing");
1891 vdev_dbgmsg_print_tree(rvd
, 2);
1892 return (SET_ERROR(ENXIO
));
1895 for (uint64_t c
= 0; c
< rvd
->vdev_children
; c
++) {
1896 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1898 if (tvd
->vdev_islog
&&
1899 tvd
->vdev_state
== VDEV_STATE_CANT_OPEN
) {
1900 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
1901 spa_load_note(spa
, "some log devices are "
1902 "missing, ZIL is dropped.");
1903 vdev_dbgmsg_print_tree(rvd
, 2);
1913 * Check for missing log devices
1916 spa_check_logs(spa_t
*spa
)
1918 boolean_t rv
= B_FALSE
;
1919 dsl_pool_t
*dp
= spa_get_dsl(spa
);
1921 switch (spa
->spa_log_state
) {
1924 case SPA_LOG_MISSING
:
1925 /* need to recheck in case slog has been restored */
1926 case SPA_LOG_UNKNOWN
:
1927 rv
= (dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
1928 zil_check_log_chain
, NULL
, DS_FIND_CHILDREN
) != 0);
1930 spa_set_log_state(spa
, SPA_LOG_MISSING
);
1937 spa_passivate_log(spa_t
*spa
)
1939 vdev_t
*rvd
= spa
->spa_root_vdev
;
1940 boolean_t slog_found
= B_FALSE
;
1942 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
1944 if (!spa_has_slogs(spa
))
1947 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
1948 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1949 metaslab_group_t
*mg
= tvd
->vdev_mg
;
1951 if (tvd
->vdev_islog
) {
1952 metaslab_group_passivate(mg
);
1953 slog_found
= B_TRUE
;
1957 return (slog_found
);
1961 spa_activate_log(spa_t
*spa
)
1963 vdev_t
*rvd
= spa
->spa_root_vdev
;
1965 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
1967 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
1968 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1969 metaslab_group_t
*mg
= tvd
->vdev_mg
;
1971 if (tvd
->vdev_islog
)
1972 metaslab_group_activate(mg
);
1977 spa_reset_logs(spa_t
*spa
)
1981 error
= dmu_objset_find(spa_name(spa
), zil_reset
,
1982 NULL
, DS_FIND_CHILDREN
);
1985 * We successfully offlined the log device, sync out the
1986 * current txg so that the "stubby" block can be removed
1989 txg_wait_synced(spa
->spa_dsl_pool
, 0);
1995 spa_aux_check_removed(spa_aux_vdev_t
*sav
)
1997 for (int i
= 0; i
< sav
->sav_count
; i
++)
1998 spa_check_removed(sav
->sav_vdevs
[i
]);
2002 spa_claim_notify(zio_t
*zio
)
2004 spa_t
*spa
= zio
->io_spa
;
2009 mutex_enter(&spa
->spa_props_lock
); /* any mutex will do */
2010 if (spa
->spa_claim_max_txg
< zio
->io_bp
->blk_birth
)
2011 spa
->spa_claim_max_txg
= zio
->io_bp
->blk_birth
;
2012 mutex_exit(&spa
->spa_props_lock
);
2015 typedef struct spa_load_error
{
2016 uint64_t sle_meta_count
;
2017 uint64_t sle_data_count
;
2021 spa_load_verify_done(zio_t
*zio
)
2023 blkptr_t
*bp
= zio
->io_bp
;
2024 spa_load_error_t
*sle
= zio
->io_private
;
2025 dmu_object_type_t type
= BP_GET_TYPE(bp
);
2026 int error
= zio
->io_error
;
2027 spa_t
*spa
= zio
->io_spa
;
2029 abd_free(zio
->io_abd
);
2031 if ((BP_GET_LEVEL(bp
) != 0 || DMU_OT_IS_METADATA(type
)) &&
2032 type
!= DMU_OT_INTENT_LOG
)
2033 atomic_inc_64(&sle
->sle_meta_count
);
2035 atomic_inc_64(&sle
->sle_data_count
);
2038 mutex_enter(&spa
->spa_scrub_lock
);
2039 spa
->spa_load_verify_ios
--;
2040 cv_broadcast(&spa
->spa_scrub_io_cv
);
2041 mutex_exit(&spa
->spa_scrub_lock
);
2045 * Maximum number of concurrent scrub i/os to create while verifying
2046 * a pool while importing it.
2048 int spa_load_verify_maxinflight
= 10000;
2049 int spa_load_verify_metadata
= B_TRUE
;
2050 int spa_load_verify_data
= B_TRUE
;
2054 spa_load_verify_cb(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
2055 const zbookmark_phys_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
2057 if (bp
== NULL
|| BP_IS_HOLE(bp
) || BP_IS_EMBEDDED(bp
))
2060 * Note: normally this routine will not be called if
2061 * spa_load_verify_metadata is not set. However, it may be useful
2062 * to manually set the flag after the traversal has begun.
2064 if (!spa_load_verify_metadata
)
2066 if (!BP_IS_METADATA(bp
) && !spa_load_verify_data
)
2070 size_t size
= BP_GET_PSIZE(bp
);
2072 mutex_enter(&spa
->spa_scrub_lock
);
2073 while (spa
->spa_load_verify_ios
>= spa_load_verify_maxinflight
)
2074 cv_wait(&spa
->spa_scrub_io_cv
, &spa
->spa_scrub_lock
);
2075 spa
->spa_load_verify_ios
++;
2076 mutex_exit(&spa
->spa_scrub_lock
);
2078 zio_nowait(zio_read(rio
, spa
, bp
, abd_alloc_for_io(size
, B_FALSE
), size
,
2079 spa_load_verify_done
, rio
->io_private
, ZIO_PRIORITY_SCRUB
,
2080 ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_CANFAIL
|
2081 ZIO_FLAG_SCRUB
| ZIO_FLAG_RAW
, zb
));
2087 verify_dataset_name_len(dsl_pool_t
*dp
, dsl_dataset_t
*ds
, void *arg
)
2089 if (dsl_dataset_namelen(ds
) >= ZFS_MAX_DATASET_NAME_LEN
)
2090 return (SET_ERROR(ENAMETOOLONG
));
2096 spa_load_verify(spa_t
*spa
)
2099 spa_load_error_t sle
= { 0 };
2100 zpool_rewind_policy_t policy
;
2101 boolean_t verify_ok
= B_FALSE
;
2104 zpool_get_rewind_policy(spa
->spa_config
, &policy
);
2106 if (policy
.zrp_request
& ZPOOL_NEVER_REWIND
)
2109 dsl_pool_config_enter(spa
->spa_dsl_pool
, FTAG
);
2110 error
= dmu_objset_find_dp(spa
->spa_dsl_pool
,
2111 spa
->spa_dsl_pool
->dp_root_dir_obj
, verify_dataset_name_len
, NULL
,
2113 dsl_pool_config_exit(spa
->spa_dsl_pool
, FTAG
);
2117 rio
= zio_root(spa
, NULL
, &sle
,
2118 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
);
2120 if (spa_load_verify_metadata
) {
2121 if (spa
->spa_extreme_rewind
) {
2122 spa_load_note(spa
, "performing a complete scan of the "
2123 "pool since extreme rewind is on. This may take "
2124 "a very long time.\n (spa_load_verify_data=%u, "
2125 "spa_load_verify_metadata=%u)",
2126 spa_load_verify_data
, spa_load_verify_metadata
);
2128 error
= traverse_pool(spa
, spa
->spa_verify_min_txg
,
2129 TRAVERSE_PRE
| TRAVERSE_PREFETCH_METADATA
|
2130 TRAVERSE_NO_DECRYPT
, spa_load_verify_cb
, rio
);
2133 (void) zio_wait(rio
);
2135 spa
->spa_load_meta_errors
= sle
.sle_meta_count
;
2136 spa
->spa_load_data_errors
= sle
.sle_data_count
;
2138 if (sle
.sle_meta_count
!= 0 || sle
.sle_data_count
!= 0) {
2139 spa_load_note(spa
, "spa_load_verify found %llu metadata errors "
2140 "and %llu data errors", (u_longlong_t
)sle
.sle_meta_count
,
2141 (u_longlong_t
)sle
.sle_data_count
);
2144 if (spa_load_verify_dryrun
||
2145 (!error
&& sle
.sle_meta_count
<= policy
.zrp_maxmeta
&&
2146 sle
.sle_data_count
<= policy
.zrp_maxdata
)) {
2150 spa
->spa_load_txg
= spa
->spa_uberblock
.ub_txg
;
2151 spa
->spa_load_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
2153 loss
= spa
->spa_last_ubsync_txg_ts
- spa
->spa_load_txg_ts
;
2154 VERIFY(nvlist_add_uint64(spa
->spa_load_info
,
2155 ZPOOL_CONFIG_LOAD_TIME
, spa
->spa_load_txg_ts
) == 0);
2156 VERIFY(nvlist_add_int64(spa
->spa_load_info
,
2157 ZPOOL_CONFIG_REWIND_TIME
, loss
) == 0);
2158 VERIFY(nvlist_add_uint64(spa
->spa_load_info
,
2159 ZPOOL_CONFIG_LOAD_DATA_ERRORS
, sle
.sle_data_count
) == 0);
2161 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
;
2164 if (spa_load_verify_dryrun
)
2168 if (error
!= ENXIO
&& error
!= EIO
)
2169 error
= SET_ERROR(EIO
);
2173 return (verify_ok
? 0 : EIO
);
2177 * Find a value in the pool props object.
2180 spa_prop_find(spa_t
*spa
, zpool_prop_t prop
, uint64_t *val
)
2182 (void) zap_lookup(spa
->spa_meta_objset
, spa
->spa_pool_props_object
,
2183 zpool_prop_to_name(prop
), sizeof (uint64_t), 1, val
);
2187 * Find a value in the pool directory object.
2190 spa_dir_prop(spa_t
*spa
, const char *name
, uint64_t *val
, boolean_t log_enoent
)
2192 int error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
2193 name
, sizeof (uint64_t), 1, val
);
2195 if (error
!= 0 && (error
!= ENOENT
|| log_enoent
)) {
2196 spa_load_failed(spa
, "couldn't get '%s' value in MOS directory "
2197 "[error=%d]", name
, error
);
2204 spa_vdev_err(vdev_t
*vdev
, vdev_aux_t aux
, int err
)
2206 vdev_set_state(vdev
, B_TRUE
, VDEV_STATE_CANT_OPEN
, aux
);
2207 return (SET_ERROR(err
));
2211 spa_spawn_aux_threads(spa_t
*spa
)
2213 ASSERT(spa_writeable(spa
));
2215 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
2217 spa_start_indirect_condensing_thread(spa
);
2221 * Fix up config after a partly-completed split. This is done with the
2222 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
2223 * pool have that entry in their config, but only the splitting one contains
2224 * a list of all the guids of the vdevs that are being split off.
2226 * This function determines what to do with that list: either rejoin
2227 * all the disks to the pool, or complete the splitting process. To attempt
2228 * the rejoin, each disk that is offlined is marked online again, and
2229 * we do a reopen() call. If the vdev label for every disk that was
2230 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
2231 * then we call vdev_split() on each disk, and complete the split.
2233 * Otherwise we leave the config alone, with all the vdevs in place in
2234 * the original pool.
2237 spa_try_repair(spa_t
*spa
, nvlist_t
*config
)
2244 boolean_t attempt_reopen
;
2246 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
, &nvl
) != 0)
2249 /* check that the config is complete */
2250 if (nvlist_lookup_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
2251 &glist
, &gcount
) != 0)
2254 vd
= kmem_zalloc(gcount
* sizeof (vdev_t
*), KM_SLEEP
);
2256 /* attempt to online all the vdevs & validate */
2257 attempt_reopen
= B_TRUE
;
2258 for (i
= 0; i
< gcount
; i
++) {
2259 if (glist
[i
] == 0) /* vdev is hole */
2262 vd
[i
] = spa_lookup_by_guid(spa
, glist
[i
], B_FALSE
);
2263 if (vd
[i
] == NULL
) {
2265 * Don't bother attempting to reopen the disks;
2266 * just do the split.
2268 attempt_reopen
= B_FALSE
;
2270 /* attempt to re-online it */
2271 vd
[i
]->vdev_offline
= B_FALSE
;
2275 if (attempt_reopen
) {
2276 vdev_reopen(spa
->spa_root_vdev
);
2278 /* check each device to see what state it's in */
2279 for (extracted
= 0, i
= 0; i
< gcount
; i
++) {
2280 if (vd
[i
] != NULL
&&
2281 vd
[i
]->vdev_stat
.vs_aux
!= VDEV_AUX_SPLIT_POOL
)
2288 * If every disk has been moved to the new pool, or if we never
2289 * even attempted to look at them, then we split them off for
2292 if (!attempt_reopen
|| gcount
== extracted
) {
2293 for (i
= 0; i
< gcount
; i
++)
2296 vdev_reopen(spa
->spa_root_vdev
);
2299 kmem_free(vd
, gcount
* sizeof (vdev_t
*));
2303 spa_load(spa_t
*spa
, spa_load_state_t state
, spa_import_type_t type
)
2305 char *ereport
= FM_EREPORT_ZFS_POOL
;
2308 spa
->spa_load_state
= state
;
2310 gethrestime(&spa
->spa_loaded_ts
);
2311 error
= spa_load_impl(spa
, type
, &ereport
, B_FALSE
);
2314 * Don't count references from objsets that are already closed
2315 * and are making their way through the eviction process.
2317 spa_evicting_os_wait(spa
);
2318 spa
->spa_minref
= refcount_count(&spa
->spa_refcount
);
2320 if (error
!= EEXIST
) {
2321 spa
->spa_loaded_ts
.tv_sec
= 0;
2322 spa
->spa_loaded_ts
.tv_nsec
= 0;
2324 if (error
!= EBADF
) {
2325 zfs_ereport_post(ereport
, spa
, NULL
, NULL
, NULL
, 0, 0);
2328 spa
->spa_load_state
= error
? SPA_LOAD_ERROR
: SPA_LOAD_NONE
;
2336 * Count the number of per-vdev ZAPs associated with all of the vdevs in the
2337 * vdev tree rooted in the given vd, and ensure that each ZAP is present in the
2338 * spa's per-vdev ZAP list.
2341 vdev_count_verify_zaps(vdev_t
*vd
)
2343 spa_t
*spa
= vd
->vdev_spa
;
2346 if (vd
->vdev_top_zap
!= 0) {
2348 ASSERT0(zap_lookup_int(spa
->spa_meta_objset
,
2349 spa
->spa_all_vdev_zaps
, vd
->vdev_top_zap
));
2351 if (vd
->vdev_leaf_zap
!= 0) {
2353 ASSERT0(zap_lookup_int(spa
->spa_meta_objset
,
2354 spa
->spa_all_vdev_zaps
, vd
->vdev_leaf_zap
));
2357 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
2358 total
+= vdev_count_verify_zaps(vd
->vdev_child
[i
]);
2366 * Determine whether the activity check is required.
2369 spa_activity_check_required(spa_t
*spa
, uberblock_t
*ub
, nvlist_t
*label
,
2373 uint64_t hostid
= 0;
2374 uint64_t tryconfig_txg
= 0;
2375 uint64_t tryconfig_timestamp
= 0;
2378 if (nvlist_exists(config
, ZPOOL_CONFIG_LOAD_INFO
)) {
2379 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
2380 (void) nvlist_lookup_uint64(nvinfo
, ZPOOL_CONFIG_MMP_TXG
,
2382 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_TIMESTAMP
,
2383 &tryconfig_timestamp
);
2386 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
, &state
);
2389 * Disable the MMP activity check - This is used by zdb which
2390 * is intended to be used on potentially active pools.
2392 if (spa
->spa_import_flags
& ZFS_IMPORT_SKIP_MMP
)
2396 * Skip the activity check when the MMP feature is disabled.
2398 if (ub
->ub_mmp_magic
== MMP_MAGIC
&& ub
->ub_mmp_delay
== 0)
2401 * If the tryconfig_* values are nonzero, they are the results of an
2402 * earlier tryimport. If they match the uberblock we just found, then
2403 * the pool has not changed and we return false so we do not test a
2406 if (tryconfig_txg
&& tryconfig_txg
== ub
->ub_txg
&&
2407 tryconfig_timestamp
&& tryconfig_timestamp
== ub
->ub_timestamp
)
2411 * Allow the activity check to be skipped when importing the pool
2412 * on the same host which last imported it. Since the hostid from
2413 * configuration may be stale use the one read from the label.
2415 if (nvlist_exists(label
, ZPOOL_CONFIG_HOSTID
))
2416 hostid
= fnvlist_lookup_uint64(label
, ZPOOL_CONFIG_HOSTID
);
2418 if (hostid
== spa_get_hostid())
2422 * Skip the activity test when the pool was cleanly exported.
2424 if (state
!= POOL_STATE_ACTIVE
)
2431 * Perform the import activity check. If the user canceled the import or
2432 * we detected activity then fail.
2435 spa_activity_check(spa_t
*spa
, uberblock_t
*ub
, nvlist_t
*config
)
2437 uint64_t import_intervals
= MAX(zfs_multihost_import_intervals
, 1);
2438 uint64_t txg
= ub
->ub_txg
;
2439 uint64_t timestamp
= ub
->ub_timestamp
;
2440 uint64_t import_delay
= NANOSEC
;
2441 hrtime_t import_expire
;
2442 nvlist_t
*mmp_label
= NULL
;
2443 vdev_t
*rvd
= spa
->spa_root_vdev
;
2448 cv_init(&cv
, NULL
, CV_DEFAULT
, NULL
);
2449 mutex_init(&mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
2453 * If ZPOOL_CONFIG_MMP_TXG is present an activity check was performed
2454 * during the earlier tryimport. If the txg recorded there is 0 then
2455 * the pool is known to be active on another host.
2457 * Otherwise, the pool might be in use on another node. Check for
2458 * changes in the uberblocks on disk if necessary.
2460 if (nvlist_exists(config
, ZPOOL_CONFIG_LOAD_INFO
)) {
2461 nvlist_t
*nvinfo
= fnvlist_lookup_nvlist(config
,
2462 ZPOOL_CONFIG_LOAD_INFO
);
2464 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_TXG
) &&
2465 fnvlist_lookup_uint64(nvinfo
, ZPOOL_CONFIG_MMP_TXG
) == 0) {
2466 vdev_uberblock_load(rvd
, ub
, &mmp_label
);
2467 error
= SET_ERROR(EREMOTEIO
);
2473 * Preferentially use the zfs_multihost_interval from the node which
2474 * last imported the pool. This value is stored in an MMP uberblock as.
2476 * ub_mmp_delay * vdev_count_leaves() == zfs_multihost_interval
2478 if (ub
->ub_mmp_magic
== MMP_MAGIC
&& ub
->ub_mmp_delay
)
2479 import_delay
= MAX(import_delay
, import_intervals
*
2480 ub
->ub_mmp_delay
* MAX(vdev_count_leaves(spa
), 1));
2482 /* Apply a floor using the local default values. */
2483 import_delay
= MAX(import_delay
, import_intervals
*
2484 MSEC2NSEC(MAX(zfs_multihost_interval
, MMP_MIN_INTERVAL
)));
2486 zfs_dbgmsg("import_delay=%llu ub_mmp_delay=%llu import_intervals=%u "
2487 "leaves=%u", import_delay
, ub
->ub_mmp_delay
, import_intervals
,
2488 vdev_count_leaves(spa
));
2490 /* Add a small random factor in case of simultaneous imports (0-25%) */
2491 import_expire
= gethrtime() + import_delay
+
2492 (import_delay
* spa_get_random(250) / 1000);
2494 while (gethrtime() < import_expire
) {
2495 vdev_uberblock_load(rvd
, ub
, &mmp_label
);
2497 if (txg
!= ub
->ub_txg
|| timestamp
!= ub
->ub_timestamp
) {
2498 error
= SET_ERROR(EREMOTEIO
);
2503 nvlist_free(mmp_label
);
2507 error
= cv_timedwait_sig(&cv
, &mtx
, ddi_get_lbolt() + hz
);
2509 error
= SET_ERROR(EINTR
);
2517 mutex_destroy(&mtx
);
2521 * If the pool is determined to be active store the status in the
2522 * spa->spa_load_info nvlist. If the remote hostname or hostid are
2523 * available from configuration read from disk store them as well.
2524 * This allows 'zpool import' to generate a more useful message.
2526 * ZPOOL_CONFIG_MMP_STATE - observed pool status (mandatory)
2527 * ZPOOL_CONFIG_MMP_HOSTNAME - hostname from the active pool
2528 * ZPOOL_CONFIG_MMP_HOSTID - hostid from the active pool
2530 if (error
== EREMOTEIO
) {
2531 char *hostname
= "<unknown>";
2532 uint64_t hostid
= 0;
2535 if (nvlist_exists(mmp_label
, ZPOOL_CONFIG_HOSTNAME
)) {
2536 hostname
= fnvlist_lookup_string(mmp_label
,
2537 ZPOOL_CONFIG_HOSTNAME
);
2538 fnvlist_add_string(spa
->spa_load_info
,
2539 ZPOOL_CONFIG_MMP_HOSTNAME
, hostname
);
2542 if (nvlist_exists(mmp_label
, ZPOOL_CONFIG_HOSTID
)) {
2543 hostid
= fnvlist_lookup_uint64(mmp_label
,
2544 ZPOOL_CONFIG_HOSTID
);
2545 fnvlist_add_uint64(spa
->spa_load_info
,
2546 ZPOOL_CONFIG_MMP_HOSTID
, hostid
);
2550 fnvlist_add_uint64(spa
->spa_load_info
,
2551 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_ACTIVE
);
2552 fnvlist_add_uint64(spa
->spa_load_info
,
2553 ZPOOL_CONFIG_MMP_TXG
, 0);
2555 error
= spa_vdev_err(rvd
, VDEV_AUX_ACTIVE
, EREMOTEIO
);
2559 nvlist_free(mmp_label
);
2565 spa_verify_host(spa_t
*spa
, nvlist_t
*mos_config
)
2569 uint64_t myhostid
= 0;
2571 if (!spa_is_root(spa
) && nvlist_lookup_uint64(mos_config
,
2572 ZPOOL_CONFIG_HOSTID
, &hostid
) == 0) {
2573 hostname
= fnvlist_lookup_string(mos_config
,
2574 ZPOOL_CONFIG_HOSTNAME
);
2576 myhostid
= zone_get_hostid(NULL
);
2578 if (hostid
!= 0 && myhostid
!= 0 && hostid
!= myhostid
) {
2579 cmn_err(CE_WARN
, "pool '%s' could not be "
2580 "loaded as it was last accessed by "
2581 "another system (host: %s hostid: 0x%llx). "
2582 "See: http://illumos.org/msg/ZFS-8000-EY",
2583 spa_name(spa
), hostname
, (u_longlong_t
)hostid
);
2584 spa_load_failed(spa
, "hostid verification failed: pool "
2585 "last accessed by host: %s (hostid: 0x%llx)",
2586 hostname
, (u_longlong_t
)hostid
);
2587 return (SET_ERROR(EBADF
));
2595 spa_ld_parse_config(spa_t
*spa
, spa_import_type_t type
)
2598 nvlist_t
*nvtree
, *nvl
, *config
= spa
->spa_config
;
2605 * Versioning wasn't explicitly added to the label until later, so if
2606 * it's not present treat it as the initial version.
2608 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
2609 &spa
->spa_ubsync
.ub_version
) != 0)
2610 spa
->spa_ubsync
.ub_version
= SPA_VERSION_INITIAL
;
2612 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
, &pool_guid
)) {
2613 spa_load_failed(spa
, "invalid config provided: '%s' missing",
2614 ZPOOL_CONFIG_POOL_GUID
);
2615 return (SET_ERROR(EINVAL
));
2618 if ((spa
->spa_load_state
== SPA_LOAD_IMPORT
|| spa
->spa_load_state
==
2619 SPA_LOAD_TRYIMPORT
) && spa_guid_exists(pool_guid
, 0)) {
2620 spa_load_failed(spa
, "a pool with guid %llu is already open",
2621 (u_longlong_t
)pool_guid
);
2622 return (SET_ERROR(EEXIST
));
2625 spa
->spa_config_guid
= pool_guid
;
2627 nvlist_free(spa
->spa_load_info
);
2628 spa
->spa_load_info
= fnvlist_alloc();
2630 ASSERT(spa
->spa_comment
== NULL
);
2631 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
2632 spa
->spa_comment
= spa_strdup(comment
);
2634 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
2635 &spa
->spa_config_txg
);
2637 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
, &nvl
) == 0)
2638 spa
->spa_config_splitting
= fnvlist_dup(nvl
);
2640 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvtree
)) {
2641 spa_load_failed(spa
, "invalid config provided: '%s' missing",
2642 ZPOOL_CONFIG_VDEV_TREE
);
2643 return (SET_ERROR(EINVAL
));
2647 * Create "The Godfather" zio to hold all async IOs
2649 spa
->spa_async_zio_root
= kmem_alloc(max_ncpus
* sizeof (void *),
2651 for (int i
= 0; i
< max_ncpus
; i
++) {
2652 spa
->spa_async_zio_root
[i
] = zio_root(spa
, NULL
, NULL
,
2653 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
2654 ZIO_FLAG_GODFATHER
);
2658 * Parse the configuration into a vdev tree. We explicitly set the
2659 * value that will be returned by spa_version() since parsing the
2660 * configuration requires knowing the version number.
2662 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2663 parse
= (type
== SPA_IMPORT_EXISTING
?
2664 VDEV_ALLOC_LOAD
: VDEV_ALLOC_SPLIT
);
2665 error
= spa_config_parse(spa
, &rvd
, nvtree
, NULL
, 0, parse
);
2666 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2669 spa_load_failed(spa
, "unable to parse config [error=%d]",
2674 ASSERT(spa
->spa_root_vdev
== rvd
);
2675 ASSERT3U(spa
->spa_min_ashift
, >=, SPA_MINBLOCKSHIFT
);
2676 ASSERT3U(spa
->spa_max_ashift
, <=, SPA_MAXBLOCKSHIFT
);
2678 if (type
!= SPA_IMPORT_ASSEMBLE
) {
2679 ASSERT(spa_guid(spa
) == pool_guid
);
2686 * Recursively open all vdevs in the vdev tree. This function is called twice:
2687 * first with the untrusted config, then with the trusted config.
2690 spa_ld_open_vdevs(spa_t
*spa
)
2695 * spa_missing_tvds_allowed defines how many top-level vdevs can be
2696 * missing/unopenable for the root vdev to be still considered openable.
2698 if (spa
->spa_trust_config
) {
2699 spa
->spa_missing_tvds_allowed
= zfs_max_missing_tvds
;
2700 } else if (spa
->spa_config_source
== SPA_CONFIG_SRC_CACHEFILE
) {
2701 spa
->spa_missing_tvds_allowed
= zfs_max_missing_tvds_cachefile
;
2702 } else if (spa
->spa_config_source
== SPA_CONFIG_SRC_SCAN
) {
2703 spa
->spa_missing_tvds_allowed
= zfs_max_missing_tvds_scan
;
2705 spa
->spa_missing_tvds_allowed
= 0;
2708 spa
->spa_missing_tvds_allowed
=
2709 MAX(zfs_max_missing_tvds
, spa
->spa_missing_tvds_allowed
);
2711 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2712 error
= vdev_open(spa
->spa_root_vdev
);
2713 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2715 if (spa
->spa_missing_tvds
!= 0) {
2716 spa_load_note(spa
, "vdev tree has %lld missing top-level "
2717 "vdevs.", (u_longlong_t
)spa
->spa_missing_tvds
);
2718 if (spa
->spa_trust_config
&& (spa
->spa_mode
& FWRITE
)) {
2720 * Although theoretically we could allow users to open
2721 * incomplete pools in RW mode, we'd need to add a lot
2722 * of extra logic (e.g. adjust pool space to account
2723 * for missing vdevs).
2724 * This limitation also prevents users from accidentally
2725 * opening the pool in RW mode during data recovery and
2726 * damaging it further.
2728 spa_load_note(spa
, "pools with missing top-level "
2729 "vdevs can only be opened in read-only mode.");
2730 error
= SET_ERROR(ENXIO
);
2732 spa_load_note(spa
, "current settings allow for maximum "
2733 "%lld missing top-level vdevs at this stage.",
2734 (u_longlong_t
)spa
->spa_missing_tvds_allowed
);
2738 spa_load_failed(spa
, "unable to open vdev tree [error=%d]",
2741 if (spa
->spa_missing_tvds
!= 0 || error
!= 0)
2742 vdev_dbgmsg_print_tree(spa
->spa_root_vdev
, 2);
2748 * We need to validate the vdev labels against the configuration that
2749 * we have in hand. This function is called twice: first with an untrusted
2750 * config, then with a trusted config. The validation is more strict when the
2751 * config is trusted.
2754 spa_ld_validate_vdevs(spa_t
*spa
)
2757 vdev_t
*rvd
= spa
->spa_root_vdev
;
2759 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2760 error
= vdev_validate(rvd
);
2761 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2764 spa_load_failed(spa
, "vdev_validate failed [error=%d]", error
);
2768 if (rvd
->vdev_state
<= VDEV_STATE_CANT_OPEN
) {
2769 spa_load_failed(spa
, "cannot open vdev tree after invalidating "
2771 vdev_dbgmsg_print_tree(rvd
, 2);
2772 return (SET_ERROR(ENXIO
));
2779 spa_ld_select_uberblock(spa_t
*spa
, spa_import_type_t type
)
2781 vdev_t
*rvd
= spa
->spa_root_vdev
;
2783 uberblock_t
*ub
= &spa
->spa_uberblock
;
2784 boolean_t activity_check
= B_FALSE
;
2787 * Find the best uberblock.
2789 vdev_uberblock_load(rvd
, ub
, &label
);
2792 * If we weren't able to find a single valid uberblock, return failure.
2794 if (ub
->ub_txg
== 0) {
2796 spa_load_failed(spa
, "no valid uberblock found");
2797 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, ENXIO
));
2800 spa_load_note(spa
, "using uberblock with txg=%llu",
2801 (u_longlong_t
)ub
->ub_txg
);
2805 * For pools which have the multihost property on determine if the
2806 * pool is truly inactive and can be safely imported. Prevent
2807 * hosts which don't have a hostid set from importing the pool.
2809 activity_check
= spa_activity_check_required(spa
, ub
, label
,
2811 if (activity_check
) {
2812 if (ub
->ub_mmp_magic
== MMP_MAGIC
&& ub
->ub_mmp_delay
&&
2813 spa_get_hostid() == 0) {
2815 fnvlist_add_uint64(spa
->spa_load_info
,
2816 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_NO_HOSTID
);
2817 return (spa_vdev_err(rvd
, VDEV_AUX_ACTIVE
, EREMOTEIO
));
2820 int error
= spa_activity_check(spa
, ub
, spa
->spa_config
);
2826 fnvlist_add_uint64(spa
->spa_load_info
,
2827 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_INACTIVE
);
2828 fnvlist_add_uint64(spa
->spa_load_info
,
2829 ZPOOL_CONFIG_MMP_TXG
, ub
->ub_txg
);
2833 * If the pool has an unsupported version we can't open it.
2835 if (!SPA_VERSION_IS_SUPPORTED(ub
->ub_version
)) {
2837 spa_load_failed(spa
, "version %llu is not supported",
2838 (u_longlong_t
)ub
->ub_version
);
2839 return (spa_vdev_err(rvd
, VDEV_AUX_VERSION_NEWER
, ENOTSUP
));
2842 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
2846 * If we weren't able to find what's necessary for reading the
2847 * MOS in the label, return failure.
2849 if (label
== NULL
) {
2850 spa_load_failed(spa
, "label config unavailable");
2851 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
2855 if (nvlist_lookup_nvlist(label
, ZPOOL_CONFIG_FEATURES_FOR_READ
,
2858 spa_load_failed(spa
, "invalid label: '%s' missing",
2859 ZPOOL_CONFIG_FEATURES_FOR_READ
);
2860 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
2865 * Update our in-core representation with the definitive values
2868 nvlist_free(spa
->spa_label_features
);
2869 VERIFY(nvlist_dup(features
, &spa
->spa_label_features
, 0) == 0);
2875 * Look through entries in the label nvlist's features_for_read. If
2876 * there is a feature listed there which we don't understand then we
2877 * cannot open a pool.
2879 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
2880 nvlist_t
*unsup_feat
;
2882 VERIFY(nvlist_alloc(&unsup_feat
, NV_UNIQUE_NAME
, KM_SLEEP
) ==
2885 for (nvpair_t
*nvp
= nvlist_next_nvpair(spa
->spa_label_features
,
2887 nvp
= nvlist_next_nvpair(spa
->spa_label_features
, nvp
)) {
2888 if (!zfeature_is_supported(nvpair_name(nvp
))) {
2889 VERIFY(nvlist_add_string(unsup_feat
,
2890 nvpair_name(nvp
), "") == 0);
2894 if (!nvlist_empty(unsup_feat
)) {
2895 VERIFY(nvlist_add_nvlist(spa
->spa_load_info
,
2896 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
) == 0);
2897 nvlist_free(unsup_feat
);
2898 spa_load_failed(spa
, "some features are unsupported");
2899 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
2903 nvlist_free(unsup_feat
);
2906 if (type
!= SPA_IMPORT_ASSEMBLE
&& spa
->spa_config_splitting
) {
2907 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2908 spa_try_repair(spa
, spa
->spa_config
);
2909 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2910 nvlist_free(spa
->spa_config_splitting
);
2911 spa
->spa_config_splitting
= NULL
;
2915 * Initialize internal SPA structures.
2917 spa
->spa_state
= POOL_STATE_ACTIVE
;
2918 spa
->spa_ubsync
= spa
->spa_uberblock
;
2919 spa
->spa_verify_min_txg
= spa
->spa_extreme_rewind
?
2920 TXG_INITIAL
- 1 : spa_last_synced_txg(spa
) - TXG_DEFER_SIZE
- 1;
2921 spa
->spa_first_txg
= spa
->spa_last_ubsync_txg
?
2922 spa
->spa_last_ubsync_txg
: spa_last_synced_txg(spa
) + 1;
2923 spa
->spa_claim_max_txg
= spa
->spa_first_txg
;
2924 spa
->spa_prev_software_version
= ub
->ub_software_version
;
2930 spa_ld_open_rootbp(spa_t
*spa
)
2933 vdev_t
*rvd
= spa
->spa_root_vdev
;
2935 error
= dsl_pool_init(spa
, spa
->spa_first_txg
, &spa
->spa_dsl_pool
);
2937 spa_load_failed(spa
, "unable to open rootbp in dsl_pool_init "
2938 "[error=%d]", error
);
2939 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2941 spa
->spa_meta_objset
= spa
->spa_dsl_pool
->dp_meta_objset
;
2947 spa_ld_load_trusted_config(spa_t
*spa
, spa_import_type_t type
,
2948 boolean_t reloading
)
2950 vdev_t
*mrvd
, *rvd
= spa
->spa_root_vdev
;
2951 nvlist_t
*nv
, *mos_config
, *policy
;
2952 int error
= 0, copy_error
;
2953 uint64_t healthy_tvds
, healthy_tvds_mos
;
2954 uint64_t mos_config_txg
;
2956 if (spa_dir_prop(spa
, DMU_POOL_CONFIG
, &spa
->spa_config_object
, B_TRUE
)
2958 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2961 * If we're assembling a pool from a split, the config provided is
2962 * already trusted so there is nothing to do.
2964 if (type
== SPA_IMPORT_ASSEMBLE
)
2967 healthy_tvds
= spa_healthy_core_tvds(spa
);
2969 if (load_nvlist(spa
, spa
->spa_config_object
, &mos_config
)
2971 spa_load_failed(spa
, "unable to retrieve MOS config");
2972 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2976 * If we are doing an open, pool owner wasn't verified yet, thus do
2977 * the verification here.
2979 if (spa
->spa_load_state
== SPA_LOAD_OPEN
) {
2980 error
= spa_verify_host(spa
, mos_config
);
2982 nvlist_free(mos_config
);
2987 nv
= fnvlist_lookup_nvlist(mos_config
, ZPOOL_CONFIG_VDEV_TREE
);
2989 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2992 * Build a new vdev tree from the trusted config
2994 VERIFY(spa_config_parse(spa
, &mrvd
, nv
, NULL
, 0, VDEV_ALLOC_LOAD
) == 0);
2997 * Vdev paths in the MOS may be obsolete. If the untrusted config was
2998 * obtained by scanning /dev/dsk, then it will have the right vdev
2999 * paths. We update the trusted MOS config with this information.
3000 * We first try to copy the paths with vdev_copy_path_strict, which
3001 * succeeds only when both configs have exactly the same vdev tree.
3002 * If that fails, we fall back to a more flexible method that has a
3003 * best effort policy.
3005 copy_error
= vdev_copy_path_strict(rvd
, mrvd
);
3006 if (copy_error
!= 0 || spa_load_print_vdev_tree
) {
3007 spa_load_note(spa
, "provided vdev tree:");
3008 vdev_dbgmsg_print_tree(rvd
, 2);
3009 spa_load_note(spa
, "MOS vdev tree:");
3010 vdev_dbgmsg_print_tree(mrvd
, 2);
3012 if (copy_error
!= 0) {
3013 spa_load_note(spa
, "vdev_copy_path_strict failed, falling "
3014 "back to vdev_copy_path_relaxed");
3015 vdev_copy_path_relaxed(rvd
, mrvd
);
3020 spa
->spa_root_vdev
= mrvd
;
3022 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3025 * We will use spa_config if we decide to reload the spa or if spa_load
3026 * fails and we rewind. We must thus regenerate the config using the
3027 * MOS information with the updated paths. Rewind policy is an import
3028 * setting and is not in the MOS. We copy it over to our new, trusted
3031 mos_config_txg
= fnvlist_lookup_uint64(mos_config
,
3032 ZPOOL_CONFIG_POOL_TXG
);
3033 nvlist_free(mos_config
);
3034 mos_config
= spa_config_generate(spa
, NULL
, mos_config_txg
, B_FALSE
);
3035 if (nvlist_lookup_nvlist(spa
->spa_config
, ZPOOL_REWIND_POLICY
,
3037 fnvlist_add_nvlist(mos_config
, ZPOOL_REWIND_POLICY
, policy
);
3038 spa_config_set(spa
, mos_config
);
3039 spa
->spa_config_source
= SPA_CONFIG_SRC_MOS
;
3042 * Now that we got the config from the MOS, we should be more strict
3043 * in checking blkptrs and can make assumptions about the consistency
3044 * of the vdev tree. spa_trust_config must be set to true before opening
3045 * vdevs in order for them to be writeable.
3047 spa
->spa_trust_config
= B_TRUE
;
3050 * Open and validate the new vdev tree
3052 error
= spa_ld_open_vdevs(spa
);
3056 error
= spa_ld_validate_vdevs(spa
);
3060 if (copy_error
!= 0 || spa_load_print_vdev_tree
) {
3061 spa_load_note(spa
, "final vdev tree:");
3062 vdev_dbgmsg_print_tree(rvd
, 2);
3065 if (spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
&&
3066 !spa
->spa_extreme_rewind
&& zfs_max_missing_tvds
== 0) {
3068 * Sanity check to make sure that we are indeed loading the
3069 * latest uberblock. If we missed SPA_SYNC_MIN_VDEVS tvds
3070 * in the config provided and they happened to be the only ones
3071 * to have the latest uberblock, we could involuntarily perform
3072 * an extreme rewind.
3074 healthy_tvds_mos
= spa_healthy_core_tvds(spa
);
3075 if (healthy_tvds_mos
- healthy_tvds
>=
3076 SPA_SYNC_MIN_VDEVS
) {
3077 spa_load_note(spa
, "config provided misses too many "
3078 "top-level vdevs compared to MOS (%lld vs %lld). ",
3079 (u_longlong_t
)healthy_tvds
,
3080 (u_longlong_t
)healthy_tvds_mos
);
3081 spa_load_note(spa
, "vdev tree:");
3082 vdev_dbgmsg_print_tree(rvd
, 2);
3084 spa_load_failed(spa
, "config was already "
3085 "provided from MOS. Aborting.");
3086 return (spa_vdev_err(rvd
,
3087 VDEV_AUX_CORRUPT_DATA
, EIO
));
3089 spa_load_note(spa
, "spa must be reloaded using MOS "
3091 return (SET_ERROR(EAGAIN
));
3095 error
= spa_check_for_missing_logs(spa
);
3097 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
, ENXIO
));
3099 if (rvd
->vdev_guid_sum
!= spa
->spa_uberblock
.ub_guid_sum
) {
3100 spa_load_failed(spa
, "uberblock guid sum doesn't match MOS "
3101 "guid sum (%llu != %llu)",
3102 (u_longlong_t
)spa
->spa_uberblock
.ub_guid_sum
,
3103 (u_longlong_t
)rvd
->vdev_guid_sum
);
3104 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
,
3112 spa_ld_open_indirect_vdev_metadata(spa_t
*spa
)
3115 vdev_t
*rvd
= spa
->spa_root_vdev
;
3118 * Everything that we read before spa_remove_init() must be stored
3119 * on concreted vdevs. Therefore we do this as early as possible.
3121 error
= spa_remove_init(spa
);
3123 spa_load_failed(spa
, "spa_remove_init failed [error=%d]",
3125 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3129 * Retrieve information needed to condense indirect vdev mappings.
3131 error
= spa_condense_init(spa
);
3133 spa_load_failed(spa
, "spa_condense_init failed [error=%d]",
3135 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, error
));
3142 spa_ld_check_features(spa_t
*spa
, boolean_t
*missing_feat_writep
)
3145 vdev_t
*rvd
= spa
->spa_root_vdev
;
3147 if (spa_version(spa
) >= SPA_VERSION_FEATURES
) {
3148 boolean_t missing_feat_read
= B_FALSE
;
3149 nvlist_t
*unsup_feat
, *enabled_feat
;
3151 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_READ
,
3152 &spa
->spa_feat_for_read_obj
, B_TRUE
) != 0) {
3153 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3156 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_WRITE
,
3157 &spa
->spa_feat_for_write_obj
, B_TRUE
) != 0) {
3158 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3161 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_DESCRIPTIONS
,
3162 &spa
->spa_feat_desc_obj
, B_TRUE
) != 0) {
3163 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3166 enabled_feat
= fnvlist_alloc();
3167 unsup_feat
= fnvlist_alloc();
3169 if (!spa_features_check(spa
, B_FALSE
,
3170 unsup_feat
, enabled_feat
))
3171 missing_feat_read
= B_TRUE
;
3173 if (spa_writeable(spa
) ||
3174 spa
->spa_load_state
== SPA_LOAD_TRYIMPORT
) {
3175 if (!spa_features_check(spa
, B_TRUE
,
3176 unsup_feat
, enabled_feat
)) {
3177 *missing_feat_writep
= B_TRUE
;
3181 fnvlist_add_nvlist(spa
->spa_load_info
,
3182 ZPOOL_CONFIG_ENABLED_FEAT
, enabled_feat
);
3184 if (!nvlist_empty(unsup_feat
)) {
3185 fnvlist_add_nvlist(spa
->spa_load_info
,
3186 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
);
3189 fnvlist_free(enabled_feat
);
3190 fnvlist_free(unsup_feat
);
3192 if (!missing_feat_read
) {
3193 fnvlist_add_boolean(spa
->spa_load_info
,
3194 ZPOOL_CONFIG_CAN_RDONLY
);
3198 * If the state is SPA_LOAD_TRYIMPORT, our objective is
3199 * twofold: to determine whether the pool is available for
3200 * import in read-write mode and (if it is not) whether the
3201 * pool is available for import in read-only mode. If the pool
3202 * is available for import in read-write mode, it is displayed
3203 * as available in userland; if it is not available for import
3204 * in read-only mode, it is displayed as unavailable in
3205 * userland. If the pool is available for import in read-only
3206 * mode but not read-write mode, it is displayed as unavailable
3207 * in userland with a special note that the pool is actually
3208 * available for open in read-only mode.
3210 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
3211 * missing a feature for write, we must first determine whether
3212 * the pool can be opened read-only before returning to
3213 * userland in order to know whether to display the
3214 * abovementioned note.
3216 if (missing_feat_read
|| (*missing_feat_writep
&&
3217 spa_writeable(spa
))) {
3218 spa_load_failed(spa
, "pool uses unsupported features");
3219 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
3224 * Load refcounts for ZFS features from disk into an in-memory
3225 * cache during SPA initialization.
3227 for (spa_feature_t i
= 0; i
< SPA_FEATURES
; i
++) {
3230 error
= feature_get_refcount_from_disk(spa
,
3231 &spa_feature_table
[i
], &refcount
);
3233 spa
->spa_feat_refcount_cache
[i
] = refcount
;
3234 } else if (error
== ENOTSUP
) {
3235 spa
->spa_feat_refcount_cache
[i
] =
3236 SPA_FEATURE_DISABLED
;
3238 spa_load_failed(spa
, "error getting refcount "
3239 "for feature %s [error=%d]",
3240 spa_feature_table
[i
].fi_guid
, error
);
3241 return (spa_vdev_err(rvd
,
3242 VDEV_AUX_CORRUPT_DATA
, EIO
));
3247 if (spa_feature_is_active(spa
, SPA_FEATURE_ENABLED_TXG
)) {
3248 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_ENABLED_TXG
,
3249 &spa
->spa_feat_enabled_txg_obj
, B_TRUE
) != 0)
3250 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3257 spa_ld_load_special_directories(spa_t
*spa
)
3260 vdev_t
*rvd
= spa
->spa_root_vdev
;
3262 spa
->spa_is_initializing
= B_TRUE
;
3263 error
= dsl_pool_open(spa
->spa_dsl_pool
);
3264 spa
->spa_is_initializing
= B_FALSE
;
3266 spa_load_failed(spa
, "dsl_pool_open failed [error=%d]", error
);
3267 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3274 spa_ld_get_props(spa_t
*spa
)
3278 vdev_t
*rvd
= spa
->spa_root_vdev
;
3280 /* Grab the checksum salt from the MOS. */
3281 error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
3282 DMU_POOL_CHECKSUM_SALT
, 1,
3283 sizeof (spa
->spa_cksum_salt
.zcs_bytes
),
3284 spa
->spa_cksum_salt
.zcs_bytes
);
3285 if (error
== ENOENT
) {
3286 /* Generate a new salt for subsequent use */
3287 (void) random_get_pseudo_bytes(spa
->spa_cksum_salt
.zcs_bytes
,
3288 sizeof (spa
->spa_cksum_salt
.zcs_bytes
));
3289 } else if (error
!= 0) {
3290 spa_load_failed(spa
, "unable to retrieve checksum salt from "
3291 "MOS [error=%d]", error
);
3292 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3295 if (spa_dir_prop(spa
, DMU_POOL_SYNC_BPOBJ
, &obj
, B_TRUE
) != 0)
3296 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3297 error
= bpobj_open(&spa
->spa_deferred_bpobj
, spa
->spa_meta_objset
, obj
);
3299 spa_load_failed(spa
, "error opening deferred-frees bpobj "
3300 "[error=%d]", error
);
3301 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3305 * Load the bit that tells us to use the new accounting function
3306 * (raid-z deflation). If we have an older pool, this will not
3309 error
= spa_dir_prop(spa
, DMU_POOL_DEFLATE
, &spa
->spa_deflate
, B_FALSE
);
3310 if (error
!= 0 && error
!= ENOENT
)
3311 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3313 error
= spa_dir_prop(spa
, DMU_POOL_CREATION_VERSION
,
3314 &spa
->spa_creation_version
, B_FALSE
);
3315 if (error
!= 0 && error
!= ENOENT
)
3316 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3319 * Load the persistent error log. If we have an older pool, this will
3322 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_LAST
, &spa
->spa_errlog_last
,
3324 if (error
!= 0 && error
!= ENOENT
)
3325 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3327 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_SCRUB
,
3328 &spa
->spa_errlog_scrub
, B_FALSE
);
3329 if (error
!= 0 && error
!= ENOENT
)
3330 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3333 * Load the history object. If we have an older pool, this
3334 * will not be present.
3336 error
= spa_dir_prop(spa
, DMU_POOL_HISTORY
, &spa
->spa_history
, B_FALSE
);
3337 if (error
!= 0 && error
!= ENOENT
)
3338 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3341 * Load the per-vdev ZAP map. If we have an older pool, this will not
3342 * be present; in this case, defer its creation to a later time to
3343 * avoid dirtying the MOS this early / out of sync context. See
3344 * spa_sync_config_object.
3347 /* The sentinel is only available in the MOS config. */
3348 nvlist_t
*mos_config
;
3349 if (load_nvlist(spa
, spa
->spa_config_object
, &mos_config
) != 0) {
3350 spa_load_failed(spa
, "unable to retrieve MOS config");
3351 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3354 error
= spa_dir_prop(spa
, DMU_POOL_VDEV_ZAP_MAP
,
3355 &spa
->spa_all_vdev_zaps
, B_FALSE
);
3357 if (error
== ENOENT
) {
3358 VERIFY(!nvlist_exists(mos_config
,
3359 ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
));
3360 spa
->spa_avz_action
= AVZ_ACTION_INITIALIZE
;
3361 ASSERT0(vdev_count_verify_zaps(spa
->spa_root_vdev
));
3362 } else if (error
!= 0) {
3363 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3364 } else if (!nvlist_exists(mos_config
, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
)) {
3366 * An older version of ZFS overwrote the sentinel value, so
3367 * we have orphaned per-vdev ZAPs in the MOS. Defer their
3368 * destruction to later; see spa_sync_config_object.
3370 spa
->spa_avz_action
= AVZ_ACTION_DESTROY
;
3372 * We're assuming that no vdevs have had their ZAPs created
3373 * before this. Better be sure of it.
3375 ASSERT0(vdev_count_verify_zaps(spa
->spa_root_vdev
));
3377 nvlist_free(mos_config
);
3379 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
3381 error
= spa_dir_prop(spa
, DMU_POOL_PROPS
, &spa
->spa_pool_props_object
,
3383 if (error
&& error
!= ENOENT
)
3384 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3387 uint64_t autoreplace
;
3389 spa_prop_find(spa
, ZPOOL_PROP_BOOTFS
, &spa
->spa_bootfs
);
3390 spa_prop_find(spa
, ZPOOL_PROP_AUTOREPLACE
, &autoreplace
);
3391 spa_prop_find(spa
, ZPOOL_PROP_DELEGATION
, &spa
->spa_delegation
);
3392 spa_prop_find(spa
, ZPOOL_PROP_FAILUREMODE
, &spa
->spa_failmode
);
3393 spa_prop_find(spa
, ZPOOL_PROP_AUTOEXPAND
, &spa
->spa_autoexpand
);
3394 spa_prop_find(spa
, ZPOOL_PROP_MULTIHOST
, &spa
->spa_multihost
);
3395 spa_prop_find(spa
, ZPOOL_PROP_DEDUPDITTO
,
3396 &spa
->spa_dedup_ditto
);
3398 spa
->spa_autoreplace
= (autoreplace
!= 0);
3402 * If we are importing a pool with missing top-level vdevs,
3403 * we enforce that the pool doesn't panic or get suspended on
3404 * error since the likelihood of missing data is extremely high.
3406 if (spa
->spa_missing_tvds
> 0 &&
3407 spa
->spa_failmode
!= ZIO_FAILURE_MODE_CONTINUE
&&
3408 spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
) {
3409 spa_load_note(spa
, "forcing failmode to 'continue' "
3410 "as some top level vdevs are missing");
3411 spa
->spa_failmode
= ZIO_FAILURE_MODE_CONTINUE
;
3418 spa_ld_open_aux_vdevs(spa_t
*spa
, spa_import_type_t type
)
3421 vdev_t
*rvd
= spa
->spa_root_vdev
;
3424 * If we're assembling the pool from the split-off vdevs of
3425 * an existing pool, we don't want to attach the spares & cache
3430 * Load any hot spares for this pool.
3432 error
= spa_dir_prop(spa
, DMU_POOL_SPARES
, &spa
->spa_spares
.sav_object
,
3434 if (error
!= 0 && error
!= ENOENT
)
3435 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3436 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
3437 ASSERT(spa_version(spa
) >= SPA_VERSION_SPARES
);
3438 if (load_nvlist(spa
, spa
->spa_spares
.sav_object
,
3439 &spa
->spa_spares
.sav_config
) != 0) {
3440 spa_load_failed(spa
, "error loading spares nvlist");
3441 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3444 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3445 spa_load_spares(spa
);
3446 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3447 } else if (error
== 0) {
3448 spa
->spa_spares
.sav_sync
= B_TRUE
;
3452 * Load any level 2 ARC devices for this pool.
3454 error
= spa_dir_prop(spa
, DMU_POOL_L2CACHE
,
3455 &spa
->spa_l2cache
.sav_object
, B_FALSE
);
3456 if (error
!= 0 && error
!= ENOENT
)
3457 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3458 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
3459 ASSERT(spa_version(spa
) >= SPA_VERSION_L2CACHE
);
3460 if (load_nvlist(spa
, spa
->spa_l2cache
.sav_object
,
3461 &spa
->spa_l2cache
.sav_config
) != 0) {
3462 spa_load_failed(spa
, "error loading l2cache nvlist");
3463 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3466 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3467 spa_load_l2cache(spa
);
3468 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3469 } else if (error
== 0) {
3470 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
3477 spa_ld_load_vdev_metadata(spa_t
*spa
)
3480 vdev_t
*rvd
= spa
->spa_root_vdev
;
3483 * If the 'multihost' property is set, then never allow a pool to
3484 * be imported when the system hostid is zero. The exception to
3485 * this rule is zdb which is always allowed to access pools.
3487 if (spa_multihost(spa
) && spa_get_hostid() == 0 &&
3488 (spa
->spa_import_flags
& ZFS_IMPORT_SKIP_MMP
) == 0) {
3489 fnvlist_add_uint64(spa
->spa_load_info
,
3490 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_NO_HOSTID
);
3491 return (spa_vdev_err(rvd
, VDEV_AUX_ACTIVE
, EREMOTEIO
));
3495 * If the 'autoreplace' property is set, then post a resource notifying
3496 * the ZFS DE that it should not issue any faults for unopenable
3497 * devices. We also iterate over the vdevs, and post a sysevent for any
3498 * unopenable vdevs so that the normal autoreplace handler can take
3501 if (spa
->spa_autoreplace
&& spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
) {
3502 spa_check_removed(spa
->spa_root_vdev
);
3504 * For the import case, this is done in spa_import(), because
3505 * at this point we're using the spare definitions from
3506 * the MOS config, not necessarily from the userland config.
3508 if (spa
->spa_load_state
!= SPA_LOAD_IMPORT
) {
3509 spa_aux_check_removed(&spa
->spa_spares
);
3510 spa_aux_check_removed(&spa
->spa_l2cache
);
3515 * Load the vdev metadata such as metaslabs, DTLs, spacemap object, etc.
3517 error
= vdev_load(rvd
);
3519 spa_load_failed(spa
, "vdev_load failed [error=%d]", error
);
3520 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, error
));
3524 * Propagate the leaf DTLs we just loaded all the way up the vdev tree.
3526 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3527 vdev_dtl_reassess(rvd
, 0, 0, B_FALSE
);
3528 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3534 spa_ld_load_dedup_tables(spa_t
*spa
)
3537 vdev_t
*rvd
= spa
->spa_root_vdev
;
3539 error
= ddt_load(spa
);
3541 spa_load_failed(spa
, "ddt_load failed [error=%d]", error
);
3542 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3549 spa_ld_verify_logs(spa_t
*spa
, spa_import_type_t type
, char **ereport
)
3551 vdev_t
*rvd
= spa
->spa_root_vdev
;
3553 if (type
!= SPA_IMPORT_ASSEMBLE
&& spa_writeable(spa
)) {
3554 boolean_t missing
= spa_check_logs(spa
);
3556 if (spa
->spa_missing_tvds
!= 0) {
3557 spa_load_note(spa
, "spa_check_logs failed "
3558 "so dropping the logs");
3560 *ereport
= FM_EREPORT_ZFS_LOG_REPLAY
;
3561 spa_load_failed(spa
, "spa_check_logs failed");
3562 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_LOG
,
3572 spa_ld_verify_pool_data(spa_t
*spa
)
3575 vdev_t
*rvd
= spa
->spa_root_vdev
;
3578 * We've successfully opened the pool, verify that we're ready
3579 * to start pushing transactions.
3581 if (spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
) {
3582 error
= spa_load_verify(spa
);
3584 spa_load_failed(spa
, "spa_load_verify failed "
3585 "[error=%d]", error
);
3586 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
3595 spa_ld_claim_log_blocks(spa_t
*spa
)
3598 dsl_pool_t
*dp
= spa_get_dsl(spa
);
3601 * Claim log blocks that haven't been committed yet.
3602 * This must all happen in a single txg.
3603 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
3604 * invoked from zil_claim_log_block()'s i/o done callback.
3605 * Price of rollback is that we abandon the log.
3607 spa
->spa_claiming
= B_TRUE
;
3609 tx
= dmu_tx_create_assigned(dp
, spa_first_txg(spa
));
3610 (void) dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
3611 zil_claim
, tx
, DS_FIND_CHILDREN
);
3614 spa
->spa_claiming
= B_FALSE
;
3616 spa_set_log_state(spa
, SPA_LOG_GOOD
);
3620 spa_ld_check_for_config_update(spa_t
*spa
, uint64_t config_cache_txg
,
3621 boolean_t reloading
)
3623 vdev_t
*rvd
= spa
->spa_root_vdev
;
3624 int need_update
= B_FALSE
;
3627 * If the config cache is stale, or we have uninitialized
3628 * metaslabs (see spa_vdev_add()), then update the config.
3630 * If this is a verbatim import, trust the current
3631 * in-core spa_config and update the disk labels.
3633 if (reloading
|| config_cache_txg
!= spa
->spa_config_txg
||
3634 spa
->spa_load_state
== SPA_LOAD_IMPORT
||
3635 spa
->spa_load_state
== SPA_LOAD_RECOVER
||
3636 (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
))
3637 need_update
= B_TRUE
;
3639 for (int c
= 0; c
< rvd
->vdev_children
; c
++)
3640 if (rvd
->vdev_child
[c
]->vdev_ms_array
== 0)
3641 need_update
= B_TRUE
;
3644 * Update the config cache asychronously in case we're the
3645 * root pool, in which case the config cache isn't writable yet.
3648 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
3652 spa_ld_prepare_for_reload(spa_t
*spa
)
3654 int mode
= spa
->spa_mode
;
3655 int async_suspended
= spa
->spa_async_suspended
;
3658 spa_deactivate(spa
);
3659 spa_activate(spa
, mode
);
3662 * We save the value of spa_async_suspended as it gets reset to 0 by
3663 * spa_unload(). We want to restore it back to the original value before
3664 * returning as we might be calling spa_async_resume() later.
3666 spa
->spa_async_suspended
= async_suspended
;
3670 * Load an existing storage pool, using the config provided. This config
3671 * describes which vdevs are part of the pool and is later validated against
3672 * partial configs present in each vdev's label and an entire copy of the
3673 * config stored in the MOS.
3676 spa_load_impl(spa_t
*spa
, spa_import_type_t type
, char **ereport
,
3677 boolean_t reloading
)
3680 boolean_t missing_feat_write
= B_FALSE
;
3682 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
3683 ASSERT(spa
->spa_config_source
!= SPA_CONFIG_SRC_NONE
);
3686 * Never trust the config that is provided unless we are assembling
3687 * a pool following a split.
3688 * This means don't trust blkptrs and the vdev tree in general. This
3689 * also effectively puts the spa in read-only mode since
3690 * spa_writeable() checks for spa_trust_config to be true.
3691 * We will later load a trusted config from the MOS.
3693 if (type
!= SPA_IMPORT_ASSEMBLE
)
3694 spa
->spa_trust_config
= B_FALSE
;
3697 spa_load_note(spa
, "RELOADING");
3699 spa_load_note(spa
, "LOADING");
3702 * Parse the config provided to create a vdev tree.
3704 error
= spa_ld_parse_config(spa
, type
);
3709 * Now that we have the vdev tree, try to open each vdev. This involves
3710 * opening the underlying physical device, retrieving its geometry and
3711 * probing the vdev with a dummy I/O. The state of each vdev will be set
3712 * based on the success of those operations. After this we'll be ready
3713 * to read from the vdevs.
3715 error
= spa_ld_open_vdevs(spa
);
3720 * Read the label of each vdev and make sure that the GUIDs stored
3721 * there match the GUIDs in the config provided.
3722 * If we're assembling a new pool that's been split off from an
3723 * existing pool, the labels haven't yet been updated so we skip
3724 * validation for now.
3726 if (type
!= SPA_IMPORT_ASSEMBLE
) {
3727 error
= spa_ld_validate_vdevs(spa
);
3733 * Read vdev labels to find the best uberblock (i.e. latest, unless
3734 * spa_load_max_txg is set) and store it in spa_uberblock. We get the
3735 * list of features required to read blkptrs in the MOS from the vdev
3736 * label with the best uberblock and verify that our version of zfs
3737 * supports them all.
3739 error
= spa_ld_select_uberblock(spa
, type
);
3744 * Pass that uberblock to the dsl_pool layer which will open the root
3745 * blkptr. This blkptr points to the latest version of the MOS and will
3746 * allow us to read its contents.
3748 error
= spa_ld_open_rootbp(spa
);
3753 * Retrieve the trusted config stored in the MOS and use it to create
3754 * a new, exact version of the vdev tree, then reopen all vdevs.
3756 error
= spa_ld_load_trusted_config(spa
, type
, reloading
);
3757 if (error
== EAGAIN
) {
3760 * Redo the loading process with the trusted config if it is
3761 * too different from the untrusted config.
3763 spa_ld_prepare_for_reload(spa
);
3764 return (spa_load_impl(spa
, type
, ereport
, B_TRUE
));
3765 } else if (error
!= 0) {
3770 * Retrieve the mapping of indirect vdevs. Those vdevs were removed
3771 * from the pool and their contents were re-mapped to other vdevs. Note
3772 * that everything that we read before this step must have been
3773 * rewritten on concrete vdevs after the last device removal was
3774 * initiated. Otherwise we could be reading from indirect vdevs before
3775 * we have loaded their mappings.
3777 error
= spa_ld_open_indirect_vdev_metadata(spa
);
3782 * Retrieve the full list of active features from the MOS and check if
3783 * they are all supported.
3785 error
= spa_ld_check_features(spa
, &missing_feat_write
);
3790 * Load several special directories from the MOS needed by the dsl_pool
3793 error
= spa_ld_load_special_directories(spa
);
3798 * Retrieve pool properties from the MOS.
3800 error
= spa_ld_get_props(spa
);
3805 * Retrieve the list of auxiliary devices - cache devices and spares -
3808 error
= spa_ld_open_aux_vdevs(spa
, type
);
3813 * Load the metadata for all vdevs. Also check if unopenable devices
3814 * should be autoreplaced.
3816 error
= spa_ld_load_vdev_metadata(spa
);
3820 error
= spa_ld_load_dedup_tables(spa
);
3825 * Verify the logs now to make sure we don't have any unexpected errors
3826 * when we claim log blocks later.
3828 error
= spa_ld_verify_logs(spa
, type
, ereport
);
3832 if (missing_feat_write
) {
3833 ASSERT(spa
->spa_load_state
== SPA_LOAD_TRYIMPORT
);
3836 * At this point, we know that we can open the pool in
3837 * read-only mode but not read-write mode. We now have enough
3838 * information and can return to userland.
3840 return (spa_vdev_err(spa
->spa_root_vdev
, VDEV_AUX_UNSUP_FEAT
,
3845 * Traverse the last txgs to make sure the pool was left off in a safe
3846 * state. When performing an extreme rewind, we verify the whole pool,
3847 * which can take a very long time.
3849 error
= spa_ld_verify_pool_data(spa
);
3854 * Calculate the deflated space for the pool. This must be done before
3855 * we write anything to the pool because we'd need to update the space
3856 * accounting using the deflated sizes.
3858 spa_update_dspace(spa
);
3861 * We have now retrieved all the information we needed to open the
3862 * pool. If we are importing the pool in read-write mode, a few
3863 * additional steps must be performed to finish the import.
3865 if (spa_writeable(spa
) && (spa
->spa_load_state
== SPA_LOAD_RECOVER
||
3866 spa
->spa_load_max_txg
== UINT64_MAX
)) {
3867 uint64_t config_cache_txg
= spa
->spa_config_txg
;
3869 ASSERT(spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
);
3872 * Traverse the ZIL and claim all blocks.
3874 spa_ld_claim_log_blocks(spa
);
3877 * Kick-off the syncing thread.
3879 spa
->spa_sync_on
= B_TRUE
;
3880 txg_sync_start(spa
->spa_dsl_pool
);
3881 mmp_thread_start(spa
);
3884 * Wait for all claims to sync. We sync up to the highest
3885 * claimed log block birth time so that claimed log blocks
3886 * don't appear to be from the future. spa_claim_max_txg
3887 * will have been set for us by ZIL traversal operations
3890 txg_wait_synced(spa
->spa_dsl_pool
, spa
->spa_claim_max_txg
);
3893 * Check if we need to request an update of the config. On the
3894 * next sync, we would update the config stored in vdev labels
3895 * and the cachefile (by default /etc/zfs/zpool.cache).
3897 spa_ld_check_for_config_update(spa
, config_cache_txg
,
3901 * Check all DTLs to see if anything needs resilvering.
3903 if (!dsl_scan_resilvering(spa
->spa_dsl_pool
) &&
3904 vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
))
3905 spa_async_request(spa
, SPA_ASYNC_RESILVER
);
3908 * Log the fact that we booted up (so that we can detect if
3909 * we rebooted in the middle of an operation).
3911 spa_history_log_version(spa
, "open", NULL
);
3914 * Delete any inconsistent datasets.
3916 (void) dmu_objset_find(spa_name(spa
),
3917 dsl_destroy_inconsistent
, NULL
, DS_FIND_CHILDREN
);
3920 * Clean up any stale temporary dataset userrefs.
3922 dsl_pool_clean_tmp_userrefs(spa
->spa_dsl_pool
);
3924 spa_restart_removal(spa
);
3926 spa_spawn_aux_threads(spa
);
3929 spa_load_note(spa
, "LOADED");
3935 spa_load_retry(spa_t
*spa
, spa_load_state_t state
)
3937 int mode
= spa
->spa_mode
;
3940 spa_deactivate(spa
);
3942 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
- 1;
3944 spa_activate(spa
, mode
);
3945 spa_async_suspend(spa
);
3947 spa_load_note(spa
, "spa_load_retry: rewind, max txg: %llu",
3948 (u_longlong_t
)spa
->spa_load_max_txg
);
3950 return (spa_load(spa
, state
, SPA_IMPORT_EXISTING
));
3954 * If spa_load() fails this function will try loading prior txg's. If
3955 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
3956 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
3957 * function will not rewind the pool and will return the same error as
3961 spa_load_best(spa_t
*spa
, spa_load_state_t state
, uint64_t max_request
,
3964 nvlist_t
*loadinfo
= NULL
;
3965 nvlist_t
*config
= NULL
;
3966 int load_error
, rewind_error
;
3967 uint64_t safe_rewind_txg
;
3970 if (spa
->spa_load_txg
&& state
== SPA_LOAD_RECOVER
) {
3971 spa
->spa_load_max_txg
= spa
->spa_load_txg
;
3972 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
3974 spa
->spa_load_max_txg
= max_request
;
3975 if (max_request
!= UINT64_MAX
)
3976 spa
->spa_extreme_rewind
= B_TRUE
;
3979 load_error
= rewind_error
= spa_load(spa
, state
, SPA_IMPORT_EXISTING
);
3980 if (load_error
== 0)
3983 if (spa
->spa_root_vdev
!= NULL
)
3984 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
3986 spa
->spa_last_ubsync_txg
= spa
->spa_uberblock
.ub_txg
;
3987 spa
->spa_last_ubsync_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
3989 if (rewind_flags
& ZPOOL_NEVER_REWIND
) {
3990 nvlist_free(config
);
3991 return (load_error
);
3994 if (state
== SPA_LOAD_RECOVER
) {
3995 /* Price of rolling back is discarding txgs, including log */
3996 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
3999 * If we aren't rolling back save the load info from our first
4000 * import attempt so that we can restore it after attempting
4003 loadinfo
= spa
->spa_load_info
;
4004 spa
->spa_load_info
= fnvlist_alloc();
4007 spa
->spa_load_max_txg
= spa
->spa_last_ubsync_txg
;
4008 safe_rewind_txg
= spa
->spa_last_ubsync_txg
- TXG_DEFER_SIZE
;
4009 min_txg
= (rewind_flags
& ZPOOL_EXTREME_REWIND
) ?
4010 TXG_INITIAL
: safe_rewind_txg
;
4013 * Continue as long as we're finding errors, we're still within
4014 * the acceptable rewind range, and we're still finding uberblocks
4016 while (rewind_error
&& spa
->spa_uberblock
.ub_txg
>= min_txg
&&
4017 spa
->spa_uberblock
.ub_txg
<= spa
->spa_load_max_txg
) {
4018 if (spa
->spa_load_max_txg
< safe_rewind_txg
)
4019 spa
->spa_extreme_rewind
= B_TRUE
;
4020 rewind_error
= spa_load_retry(spa
, state
);
4023 spa
->spa_extreme_rewind
= B_FALSE
;
4024 spa
->spa_load_max_txg
= UINT64_MAX
;
4026 if (config
&& (rewind_error
|| state
!= SPA_LOAD_RECOVER
))
4027 spa_config_set(spa
, config
);
4029 nvlist_free(config
);
4031 if (state
== SPA_LOAD_RECOVER
) {
4032 ASSERT3P(loadinfo
, ==, NULL
);
4033 return (rewind_error
);
4035 /* Store the rewind info as part of the initial load info */
4036 fnvlist_add_nvlist(loadinfo
, ZPOOL_CONFIG_REWIND_INFO
,
4037 spa
->spa_load_info
);
4039 /* Restore the initial load info */
4040 fnvlist_free(spa
->spa_load_info
);
4041 spa
->spa_load_info
= loadinfo
;
4043 return (load_error
);
4050 * The import case is identical to an open except that the configuration is sent
4051 * down from userland, instead of grabbed from the configuration cache. For the
4052 * case of an open, the pool configuration will exist in the
4053 * POOL_STATE_UNINITIALIZED state.
4055 * The stats information (gen/count/ustats) is used to gather vdev statistics at
4056 * the same time open the pool, without having to keep around the spa_t in some
4060 spa_open_common(const char *pool
, spa_t
**spapp
, void *tag
, nvlist_t
*nvpolicy
,
4064 spa_load_state_t state
= SPA_LOAD_OPEN
;
4066 int locked
= B_FALSE
;
4067 int firstopen
= B_FALSE
;
4072 * As disgusting as this is, we need to support recursive calls to this
4073 * function because dsl_dir_open() is called during spa_load(), and ends
4074 * up calling spa_open() again. The real fix is to figure out how to
4075 * avoid dsl_dir_open() calling this in the first place.
4077 if (MUTEX_NOT_HELD(&spa_namespace_lock
)) {
4078 mutex_enter(&spa_namespace_lock
);
4082 if ((spa
= spa_lookup(pool
)) == NULL
) {
4084 mutex_exit(&spa_namespace_lock
);
4085 return (SET_ERROR(ENOENT
));
4088 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
) {
4089 zpool_rewind_policy_t policy
;
4093 zpool_get_rewind_policy(nvpolicy
? nvpolicy
: spa
->spa_config
,
4095 if (policy
.zrp_request
& ZPOOL_DO_REWIND
)
4096 state
= SPA_LOAD_RECOVER
;
4098 spa_activate(spa
, spa_mode_global
);
4100 if (state
!= SPA_LOAD_RECOVER
)
4101 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
4102 spa
->spa_config_source
= SPA_CONFIG_SRC_CACHEFILE
;
4104 zfs_dbgmsg("spa_open_common: opening %s", pool
);
4105 error
= spa_load_best(spa
, state
, policy
.zrp_txg
,
4106 policy
.zrp_request
);
4108 if (error
== EBADF
) {
4110 * If vdev_validate() returns failure (indicated by
4111 * EBADF), it indicates that one of the vdevs indicates
4112 * that the pool has been exported or destroyed. If
4113 * this is the case, the config cache is out of sync and
4114 * we should remove the pool from the namespace.
4117 spa_deactivate(spa
);
4118 spa_write_cachefile(spa
, B_TRUE
, B_TRUE
);
4121 mutex_exit(&spa_namespace_lock
);
4122 return (SET_ERROR(ENOENT
));
4127 * We can't open the pool, but we still have useful
4128 * information: the state of each vdev after the
4129 * attempted vdev_open(). Return this to the user.
4131 if (config
!= NULL
&& spa
->spa_config
) {
4132 VERIFY(nvlist_dup(spa
->spa_config
, config
,
4134 VERIFY(nvlist_add_nvlist(*config
,
4135 ZPOOL_CONFIG_LOAD_INFO
,
4136 spa
->spa_load_info
) == 0);
4139 spa_deactivate(spa
);
4140 spa
->spa_last_open_failed
= error
;
4142 mutex_exit(&spa_namespace_lock
);
4148 spa_open_ref(spa
, tag
);
4151 *config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
4154 * If we've recovered the pool, pass back any information we
4155 * gathered while doing the load.
4157 if (state
== SPA_LOAD_RECOVER
) {
4158 VERIFY(nvlist_add_nvlist(*config
, ZPOOL_CONFIG_LOAD_INFO
,
4159 spa
->spa_load_info
) == 0);
4163 spa
->spa_last_open_failed
= 0;
4164 spa
->spa_last_ubsync_txg
= 0;
4165 spa
->spa_load_txg
= 0;
4166 mutex_exit(&spa_namespace_lock
);
4170 zvol_create_minors(spa
, spa_name(spa
), B_TRUE
);
4178 spa_open_rewind(const char *name
, spa_t
**spapp
, void *tag
, nvlist_t
*policy
,
4181 return (spa_open_common(name
, spapp
, tag
, policy
, config
));
4185 spa_open(const char *name
, spa_t
**spapp
, void *tag
)
4187 return (spa_open_common(name
, spapp
, tag
, NULL
, NULL
));
4191 * Lookup the given spa_t, incrementing the inject count in the process,
4192 * preventing it from being exported or destroyed.
4195 spa_inject_addref(char *name
)
4199 mutex_enter(&spa_namespace_lock
);
4200 if ((spa
= spa_lookup(name
)) == NULL
) {
4201 mutex_exit(&spa_namespace_lock
);
4204 spa
->spa_inject_ref
++;
4205 mutex_exit(&spa_namespace_lock
);
4211 spa_inject_delref(spa_t
*spa
)
4213 mutex_enter(&spa_namespace_lock
);
4214 spa
->spa_inject_ref
--;
4215 mutex_exit(&spa_namespace_lock
);
4219 * Add spares device information to the nvlist.
4222 spa_add_spares(spa_t
*spa
, nvlist_t
*config
)
4232 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
4234 if (spa
->spa_spares
.sav_count
== 0)
4237 VERIFY(nvlist_lookup_nvlist(config
,
4238 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
4239 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
4240 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
4242 VERIFY(nvlist_add_nvlist_array(nvroot
,
4243 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
4244 VERIFY(nvlist_lookup_nvlist_array(nvroot
,
4245 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
4248 * Go through and find any spares which have since been
4249 * repurposed as an active spare. If this is the case, update
4250 * their status appropriately.
4252 for (i
= 0; i
< nspares
; i
++) {
4253 VERIFY(nvlist_lookup_uint64(spares
[i
],
4254 ZPOOL_CONFIG_GUID
, &guid
) == 0);
4255 if (spa_spare_exists(guid
, &pool
, NULL
) &&
4257 VERIFY(nvlist_lookup_uint64_array(
4258 spares
[i
], ZPOOL_CONFIG_VDEV_STATS
,
4259 (uint64_t **)&vs
, &vsc
) == 0);
4260 vs
->vs_state
= VDEV_STATE_CANT_OPEN
;
4261 vs
->vs_aux
= VDEV_AUX_SPARED
;
4268 * Add l2cache device information to the nvlist, including vdev stats.
4271 spa_add_l2cache(spa_t
*spa
, nvlist_t
*config
)
4274 uint_t i
, j
, nl2cache
;
4281 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
4283 if (spa
->spa_l2cache
.sav_count
== 0)
4286 VERIFY(nvlist_lookup_nvlist(config
,
4287 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
4288 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_l2cache
.sav_config
,
4289 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
4290 if (nl2cache
!= 0) {
4291 VERIFY(nvlist_add_nvlist_array(nvroot
,
4292 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
4293 VERIFY(nvlist_lookup_nvlist_array(nvroot
,
4294 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
4297 * Update level 2 cache device stats.
4300 for (i
= 0; i
< nl2cache
; i
++) {
4301 VERIFY(nvlist_lookup_uint64(l2cache
[i
],
4302 ZPOOL_CONFIG_GUID
, &guid
) == 0);
4305 for (j
= 0; j
< spa
->spa_l2cache
.sav_count
; j
++) {
4307 spa
->spa_l2cache
.sav_vdevs
[j
]->vdev_guid
) {
4308 vd
= spa
->spa_l2cache
.sav_vdevs
[j
];
4314 VERIFY(nvlist_lookup_uint64_array(l2cache
[i
],
4315 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
, &vsc
)
4317 vdev_get_stats(vd
, vs
);
4318 vdev_config_generate_stats(vd
, l2cache
[i
]);
4325 spa_feature_stats_from_disk(spa_t
*spa
, nvlist_t
*features
)
4330 if (spa
->spa_feat_for_read_obj
!= 0) {
4331 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
4332 spa
->spa_feat_for_read_obj
);
4333 zap_cursor_retrieve(&zc
, &za
) == 0;
4334 zap_cursor_advance(&zc
)) {
4335 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
4336 za
.za_num_integers
== 1);
4337 VERIFY0(nvlist_add_uint64(features
, za
.za_name
,
4338 za
.za_first_integer
));
4340 zap_cursor_fini(&zc
);
4343 if (spa
->spa_feat_for_write_obj
!= 0) {
4344 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
4345 spa
->spa_feat_for_write_obj
);
4346 zap_cursor_retrieve(&zc
, &za
) == 0;
4347 zap_cursor_advance(&zc
)) {
4348 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
4349 za
.za_num_integers
== 1);
4350 VERIFY0(nvlist_add_uint64(features
, za
.za_name
,
4351 za
.za_first_integer
));
4353 zap_cursor_fini(&zc
);
4358 spa_feature_stats_from_cache(spa_t
*spa
, nvlist_t
*features
)
4362 for (i
= 0; i
< SPA_FEATURES
; i
++) {
4363 zfeature_info_t feature
= spa_feature_table
[i
];
4366 if (feature_get_refcount(spa
, &feature
, &refcount
) != 0)
4369 VERIFY0(nvlist_add_uint64(features
, feature
.fi_guid
, refcount
));
4374 * Store a list of pool features and their reference counts in the
4377 * The first time this is called on a spa, allocate a new nvlist, fetch
4378 * the pool features and reference counts from disk, then save the list
4379 * in the spa. In subsequent calls on the same spa use the saved nvlist
4380 * and refresh its values from the cached reference counts. This
4381 * ensures we don't block here on I/O on a suspended pool so 'zpool
4382 * clear' can resume the pool.
4385 spa_add_feature_stats(spa_t
*spa
, nvlist_t
*config
)
4389 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
4391 mutex_enter(&spa
->spa_feat_stats_lock
);
4392 features
= spa
->spa_feat_stats
;
4394 if (features
!= NULL
) {
4395 spa_feature_stats_from_cache(spa
, features
);
4397 VERIFY0(nvlist_alloc(&features
, NV_UNIQUE_NAME
, KM_SLEEP
));
4398 spa
->spa_feat_stats
= features
;
4399 spa_feature_stats_from_disk(spa
, features
);
4402 VERIFY0(nvlist_add_nvlist(config
, ZPOOL_CONFIG_FEATURE_STATS
,
4405 mutex_exit(&spa
->spa_feat_stats_lock
);
4409 spa_get_stats(const char *name
, nvlist_t
**config
,
4410 char *altroot
, size_t buflen
)
4416 error
= spa_open_common(name
, &spa
, FTAG
, NULL
, config
);
4420 * This still leaves a window of inconsistency where the spares
4421 * or l2cache devices could change and the config would be
4422 * self-inconsistent.
4424 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
4426 if (*config
!= NULL
) {
4427 uint64_t loadtimes
[2];
4429 loadtimes
[0] = spa
->spa_loaded_ts
.tv_sec
;
4430 loadtimes
[1] = spa
->spa_loaded_ts
.tv_nsec
;
4431 VERIFY(nvlist_add_uint64_array(*config
,
4432 ZPOOL_CONFIG_LOADED_TIME
, loadtimes
, 2) == 0);
4434 VERIFY(nvlist_add_uint64(*config
,
4435 ZPOOL_CONFIG_ERRCOUNT
,
4436 spa_get_errlog_size(spa
)) == 0);
4438 if (spa_suspended(spa
)) {
4439 VERIFY(nvlist_add_uint64(*config
,
4440 ZPOOL_CONFIG_SUSPENDED
,
4441 spa
->spa_failmode
) == 0);
4442 VERIFY(nvlist_add_uint64(*config
,
4443 ZPOOL_CONFIG_SUSPENDED_REASON
,
4444 spa
->spa_suspended
) == 0);
4447 spa_add_spares(spa
, *config
);
4448 spa_add_l2cache(spa
, *config
);
4449 spa_add_feature_stats(spa
, *config
);
4454 * We want to get the alternate root even for faulted pools, so we cheat
4455 * and call spa_lookup() directly.
4459 mutex_enter(&spa_namespace_lock
);
4460 spa
= spa_lookup(name
);
4462 spa_altroot(spa
, altroot
, buflen
);
4466 mutex_exit(&spa_namespace_lock
);
4468 spa_altroot(spa
, altroot
, buflen
);
4473 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
4474 spa_close(spa
, FTAG
);
4481 * Validate that the auxiliary device array is well formed. We must have an
4482 * array of nvlists, each which describes a valid leaf vdev. If this is an
4483 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
4484 * specified, as long as they are well-formed.
4487 spa_validate_aux_devs(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
,
4488 spa_aux_vdev_t
*sav
, const char *config
, uint64_t version
,
4489 vdev_labeltype_t label
)
4496 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
4499 * It's acceptable to have no devs specified.
4501 if (nvlist_lookup_nvlist_array(nvroot
, config
, &dev
, &ndev
) != 0)
4505 return (SET_ERROR(EINVAL
));
4508 * Make sure the pool is formatted with a version that supports this
4511 if (spa_version(spa
) < version
)
4512 return (SET_ERROR(ENOTSUP
));
4515 * Set the pending device list so we correctly handle device in-use
4518 sav
->sav_pending
= dev
;
4519 sav
->sav_npending
= ndev
;
4521 for (i
= 0; i
< ndev
; i
++) {
4522 if ((error
= spa_config_parse(spa
, &vd
, dev
[i
], NULL
, 0,
4526 if (!vd
->vdev_ops
->vdev_op_leaf
) {
4528 error
= SET_ERROR(EINVAL
);
4534 if ((error
= vdev_open(vd
)) == 0 &&
4535 (error
= vdev_label_init(vd
, crtxg
, label
)) == 0) {
4536 VERIFY(nvlist_add_uint64(dev
[i
], ZPOOL_CONFIG_GUID
,
4537 vd
->vdev_guid
) == 0);
4543 (mode
!= VDEV_ALLOC_SPARE
&& mode
!= VDEV_ALLOC_L2CACHE
))
4550 sav
->sav_pending
= NULL
;
4551 sav
->sav_npending
= 0;
4556 spa_validate_aux(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
)
4560 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
4562 if ((error
= spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
4563 &spa
->spa_spares
, ZPOOL_CONFIG_SPARES
, SPA_VERSION_SPARES
,
4564 VDEV_LABEL_SPARE
)) != 0) {
4568 return (spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
4569 &spa
->spa_l2cache
, ZPOOL_CONFIG_L2CACHE
, SPA_VERSION_L2CACHE
,
4570 VDEV_LABEL_L2CACHE
));
4574 spa_set_aux_vdevs(spa_aux_vdev_t
*sav
, nvlist_t
**devs
, int ndevs
,
4579 if (sav
->sav_config
!= NULL
) {
4585 * Generate new dev list by concatenating with the
4588 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
, config
,
4589 &olddevs
, &oldndevs
) == 0);
4591 newdevs
= kmem_alloc(sizeof (void *) *
4592 (ndevs
+ oldndevs
), KM_SLEEP
);
4593 for (i
= 0; i
< oldndevs
; i
++)
4594 VERIFY(nvlist_dup(olddevs
[i
], &newdevs
[i
],
4596 for (i
= 0; i
< ndevs
; i
++)
4597 VERIFY(nvlist_dup(devs
[i
], &newdevs
[i
+ oldndevs
],
4600 VERIFY(nvlist_remove(sav
->sav_config
, config
,
4601 DATA_TYPE_NVLIST_ARRAY
) == 0);
4603 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
,
4604 config
, newdevs
, ndevs
+ oldndevs
) == 0);
4605 for (i
= 0; i
< oldndevs
+ ndevs
; i
++)
4606 nvlist_free(newdevs
[i
]);
4607 kmem_free(newdevs
, (oldndevs
+ ndevs
) * sizeof (void *));
4610 * Generate a new dev list.
4612 VERIFY(nvlist_alloc(&sav
->sav_config
, NV_UNIQUE_NAME
,
4614 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
, config
,
4620 * Stop and drop level 2 ARC devices
4623 spa_l2cache_drop(spa_t
*spa
)
4627 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
4629 for (i
= 0; i
< sav
->sav_count
; i
++) {
4632 vd
= sav
->sav_vdevs
[i
];
4635 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
4636 pool
!= 0ULL && l2arc_vdev_present(vd
))
4637 l2arc_remove_vdev(vd
);
4642 * Verify encryption parameters for spa creation. If we are encrypting, we must
4643 * have the encryption feature flag enabled.
4646 spa_create_check_encryption_params(dsl_crypto_params_t
*dcp
,
4647 boolean_t has_encryption
)
4649 if (dcp
->cp_crypt
!= ZIO_CRYPT_OFF
&&
4650 dcp
->cp_crypt
!= ZIO_CRYPT_INHERIT
&&
4652 return (SET_ERROR(ENOTSUP
));
4654 return (dmu_objset_create_crypt_check(NULL
, dcp
));
4661 spa_create(const char *pool
, nvlist_t
*nvroot
, nvlist_t
*props
,
4662 nvlist_t
*zplprops
, dsl_crypto_params_t
*dcp
)
4665 char *altroot
= NULL
;
4670 uint64_t txg
= TXG_INITIAL
;
4671 nvlist_t
**spares
, **l2cache
;
4672 uint_t nspares
, nl2cache
;
4673 uint64_t version
, obj
, root_dsobj
= 0;
4674 boolean_t has_features
;
4675 boolean_t has_encryption
;
4681 if (nvlist_lookup_string(props
, "tname", &poolname
) != 0)
4682 poolname
= (char *)pool
;
4685 * If this pool already exists, return failure.
4687 mutex_enter(&spa_namespace_lock
);
4688 if (spa_lookup(poolname
) != NULL
) {
4689 mutex_exit(&spa_namespace_lock
);
4690 return (SET_ERROR(EEXIST
));
4694 * Allocate a new spa_t structure.
4696 nvl
= fnvlist_alloc();
4697 fnvlist_add_string(nvl
, ZPOOL_CONFIG_POOL_NAME
, pool
);
4698 (void) nvlist_lookup_string(props
,
4699 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
4700 spa
= spa_add(poolname
, nvl
, altroot
);
4702 spa_activate(spa
, spa_mode_global
);
4704 if (props
&& (error
= spa_prop_validate(spa
, props
))) {
4705 spa_deactivate(spa
);
4707 mutex_exit(&spa_namespace_lock
);
4712 * Temporary pool names should never be written to disk.
4714 if (poolname
!= pool
)
4715 spa
->spa_import_flags
|= ZFS_IMPORT_TEMP_NAME
;
4717 has_features
= B_FALSE
;
4718 has_encryption
= B_FALSE
;
4719 for (nvpair_t
*elem
= nvlist_next_nvpair(props
, NULL
);
4720 elem
!= NULL
; elem
= nvlist_next_nvpair(props
, elem
)) {
4721 if (zpool_prop_feature(nvpair_name(elem
))) {
4722 has_features
= B_TRUE
;
4724 feat_name
= strchr(nvpair_name(elem
), '@') + 1;
4725 VERIFY0(zfeature_lookup_name(feat_name
, &feat
));
4726 if (feat
== SPA_FEATURE_ENCRYPTION
)
4727 has_encryption
= B_TRUE
;
4731 /* verify encryption params, if they were provided */
4733 error
= spa_create_check_encryption_params(dcp
, has_encryption
);
4735 spa_deactivate(spa
);
4737 mutex_exit(&spa_namespace_lock
);
4742 if (has_features
|| nvlist_lookup_uint64(props
,
4743 zpool_prop_to_name(ZPOOL_PROP_VERSION
), &version
) != 0) {
4744 version
= SPA_VERSION
;
4746 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
4748 spa
->spa_first_txg
= txg
;
4749 spa
->spa_uberblock
.ub_txg
= txg
- 1;
4750 spa
->spa_uberblock
.ub_version
= version
;
4751 spa
->spa_ubsync
= spa
->spa_uberblock
;
4752 spa
->spa_load_state
= SPA_LOAD_CREATE
;
4753 spa
->spa_removing_phys
.sr_state
= DSS_NONE
;
4754 spa
->spa_removing_phys
.sr_removing_vdev
= -1;
4755 spa
->spa_removing_phys
.sr_prev_indirect_vdev
= -1;
4758 * Create "The Godfather" zio to hold all async IOs
4760 spa
->spa_async_zio_root
= kmem_alloc(max_ncpus
* sizeof (void *),
4762 for (int i
= 0; i
< max_ncpus
; i
++) {
4763 spa
->spa_async_zio_root
[i
] = zio_root(spa
, NULL
, NULL
,
4764 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
4765 ZIO_FLAG_GODFATHER
);
4769 * Create the root vdev.
4771 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4773 error
= spa_config_parse(spa
, &rvd
, nvroot
, NULL
, 0, VDEV_ALLOC_ADD
);
4775 ASSERT(error
!= 0 || rvd
!= NULL
);
4776 ASSERT(error
!= 0 || spa
->spa_root_vdev
== rvd
);
4778 if (error
== 0 && !zfs_allocatable_devs(nvroot
))
4779 error
= SET_ERROR(EINVAL
);
4782 (error
= vdev_create(rvd
, txg
, B_FALSE
)) == 0 &&
4783 (error
= spa_validate_aux(spa
, nvroot
, txg
,
4784 VDEV_ALLOC_ADD
)) == 0) {
4785 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
4786 vdev_metaslab_set_size(rvd
->vdev_child
[c
]);
4787 vdev_expand(rvd
->vdev_child
[c
], txg
);
4791 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4795 spa_deactivate(spa
);
4797 mutex_exit(&spa_namespace_lock
);
4802 * Get the list of spares, if specified.
4804 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
4805 &spares
, &nspares
) == 0) {
4806 VERIFY(nvlist_alloc(&spa
->spa_spares
.sav_config
, NV_UNIQUE_NAME
,
4808 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
4809 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
4810 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4811 spa_load_spares(spa
);
4812 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4813 spa
->spa_spares
.sav_sync
= B_TRUE
;
4817 * Get the list of level 2 cache devices, if specified.
4819 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
4820 &l2cache
, &nl2cache
) == 0) {
4821 VERIFY(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
4822 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
4823 VERIFY(nvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
4824 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
4825 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4826 spa_load_l2cache(spa
);
4827 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4828 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
4831 spa
->spa_is_initializing
= B_TRUE
;
4832 spa
->spa_dsl_pool
= dp
= dsl_pool_create(spa
, zplprops
, dcp
, txg
);
4833 spa
->spa_is_initializing
= B_FALSE
;
4836 * Create DDTs (dedup tables).
4840 spa_update_dspace(spa
);
4842 tx
= dmu_tx_create_assigned(dp
, txg
);
4845 * Create the pool's history object.
4847 if (version
>= SPA_VERSION_ZPOOL_HISTORY
&& !spa
->spa_history
)
4848 spa_history_create_obj(spa
, tx
);
4850 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_CREATE
);
4851 spa_history_log_version(spa
, "create", tx
);
4854 * Create the pool config object.
4856 spa
->spa_config_object
= dmu_object_alloc(spa
->spa_meta_objset
,
4857 DMU_OT_PACKED_NVLIST
, SPA_CONFIG_BLOCKSIZE
,
4858 DMU_OT_PACKED_NVLIST_SIZE
, sizeof (uint64_t), tx
);
4860 if (zap_add(spa
->spa_meta_objset
,
4861 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CONFIG
,
4862 sizeof (uint64_t), 1, &spa
->spa_config_object
, tx
) != 0) {
4863 cmn_err(CE_PANIC
, "failed to add pool config");
4866 if (zap_add(spa
->spa_meta_objset
,
4867 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CREATION_VERSION
,
4868 sizeof (uint64_t), 1, &version
, tx
) != 0) {
4869 cmn_err(CE_PANIC
, "failed to add pool version");
4872 /* Newly created pools with the right version are always deflated. */
4873 if (version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
4874 spa
->spa_deflate
= TRUE
;
4875 if (zap_add(spa
->spa_meta_objset
,
4876 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
4877 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
) != 0) {
4878 cmn_err(CE_PANIC
, "failed to add deflate");
4883 * Create the deferred-free bpobj. Turn off compression
4884 * because sync-to-convergence takes longer if the blocksize
4887 obj
= bpobj_alloc(spa
->spa_meta_objset
, 1 << 14, tx
);
4888 dmu_object_set_compress(spa
->spa_meta_objset
, obj
,
4889 ZIO_COMPRESS_OFF
, tx
);
4890 if (zap_add(spa
->spa_meta_objset
,
4891 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_SYNC_BPOBJ
,
4892 sizeof (uint64_t), 1, &obj
, tx
) != 0) {
4893 cmn_err(CE_PANIC
, "failed to add bpobj");
4895 VERIFY3U(0, ==, bpobj_open(&spa
->spa_deferred_bpobj
,
4896 spa
->spa_meta_objset
, obj
));
4899 * Generate some random noise for salted checksums to operate on.
4901 (void) random_get_pseudo_bytes(spa
->spa_cksum_salt
.zcs_bytes
,
4902 sizeof (spa
->spa_cksum_salt
.zcs_bytes
));
4905 * Set pool properties.
4907 spa
->spa_bootfs
= zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS
);
4908 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
4909 spa
->spa_failmode
= zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE
);
4910 spa
->spa_autoexpand
= zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND
);
4911 spa
->spa_multihost
= zpool_prop_default_numeric(ZPOOL_PROP_MULTIHOST
);
4913 if (props
!= NULL
) {
4914 spa_configfile_set(spa
, props
, B_FALSE
);
4915 spa_sync_props(props
, tx
);
4921 * If the root dataset is encrypted we will need to create key mappings
4922 * for the zio layer before we start to write any data to disk and hold
4923 * them until after the first txg has been synced. Waiting for the first
4924 * transaction to complete also ensures that our bean counters are
4925 * appropriately updated.
4927 if (dp
->dp_root_dir
->dd_crypto_obj
!= 0) {
4928 root_dsobj
= dsl_dir_phys(dp
->dp_root_dir
)->dd_head_dataset_obj
;
4929 VERIFY0(spa_keystore_create_mapping_impl(spa
, root_dsobj
,
4930 dp
->dp_root_dir
, FTAG
));
4933 spa
->spa_sync_on
= B_TRUE
;
4935 mmp_thread_start(spa
);
4936 txg_wait_synced(dp
, txg
);
4938 if (dp
->dp_root_dir
->dd_crypto_obj
!= 0)
4939 VERIFY0(spa_keystore_remove_mapping(spa
, root_dsobj
, FTAG
));
4941 spa_spawn_aux_threads(spa
);
4943 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
);
4946 * Don't count references from objsets that are already closed
4947 * and are making their way through the eviction process.
4949 spa_evicting_os_wait(spa
);
4950 spa
->spa_minref
= refcount_count(&spa
->spa_refcount
);
4951 spa
->spa_load_state
= SPA_LOAD_NONE
;
4953 mutex_exit(&spa_namespace_lock
);
4959 * Import a non-root pool into the system.
4962 spa_import(char *pool
, nvlist_t
*config
, nvlist_t
*props
, uint64_t flags
)
4965 char *altroot
= NULL
;
4966 spa_load_state_t state
= SPA_LOAD_IMPORT
;
4967 zpool_rewind_policy_t policy
;
4968 uint64_t mode
= spa_mode_global
;
4969 uint64_t readonly
= B_FALSE
;
4972 nvlist_t
**spares
, **l2cache
;
4973 uint_t nspares
, nl2cache
;
4976 * If a pool with this name exists, return failure.
4978 mutex_enter(&spa_namespace_lock
);
4979 if (spa_lookup(pool
) != NULL
) {
4980 mutex_exit(&spa_namespace_lock
);
4981 return (SET_ERROR(EEXIST
));
4985 * Create and initialize the spa structure.
4987 (void) nvlist_lookup_string(props
,
4988 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
4989 (void) nvlist_lookup_uint64(props
,
4990 zpool_prop_to_name(ZPOOL_PROP_READONLY
), &readonly
);
4993 spa
= spa_add(pool
, config
, altroot
);
4994 spa
->spa_import_flags
= flags
;
4997 * Verbatim import - Take a pool and insert it into the namespace
4998 * as if it had been loaded at boot.
5000 if (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
) {
5002 spa_configfile_set(spa
, props
, B_FALSE
);
5004 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
);
5005 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_IMPORT
);
5006 zfs_dbgmsg("spa_import: verbatim import of %s", pool
);
5007 mutex_exit(&spa_namespace_lock
);
5011 spa_activate(spa
, mode
);
5014 * Don't start async tasks until we know everything is healthy.
5016 spa_async_suspend(spa
);
5018 zpool_get_rewind_policy(config
, &policy
);
5019 if (policy
.zrp_request
& ZPOOL_DO_REWIND
)
5020 state
= SPA_LOAD_RECOVER
;
5022 spa
->spa_config_source
= SPA_CONFIG_SRC_TRYIMPORT
;
5024 if (state
!= SPA_LOAD_RECOVER
) {
5025 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
5026 zfs_dbgmsg("spa_import: importing %s", pool
);
5028 zfs_dbgmsg("spa_import: importing %s, max_txg=%lld "
5029 "(RECOVERY MODE)", pool
, (longlong_t
)policy
.zrp_txg
);
5031 error
= spa_load_best(spa
, state
, policy
.zrp_txg
, policy
.zrp_request
);
5034 * Propagate anything learned while loading the pool and pass it
5035 * back to caller (i.e. rewind info, missing devices, etc).
5037 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
5038 spa
->spa_load_info
) == 0);
5040 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5042 * Toss any existing sparelist, as it doesn't have any validity
5043 * anymore, and conflicts with spa_has_spare().
5045 if (spa
->spa_spares
.sav_config
) {
5046 nvlist_free(spa
->spa_spares
.sav_config
);
5047 spa
->spa_spares
.sav_config
= NULL
;
5048 spa_load_spares(spa
);
5050 if (spa
->spa_l2cache
.sav_config
) {
5051 nvlist_free(spa
->spa_l2cache
.sav_config
);
5052 spa
->spa_l2cache
.sav_config
= NULL
;
5053 spa_load_l2cache(spa
);
5056 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
5058 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5061 spa_configfile_set(spa
, props
, B_FALSE
);
5063 if (error
!= 0 || (props
&& spa_writeable(spa
) &&
5064 (error
= spa_prop_set(spa
, props
)))) {
5066 spa_deactivate(spa
);
5068 mutex_exit(&spa_namespace_lock
);
5072 spa_async_resume(spa
);
5075 * Override any spares and level 2 cache devices as specified by
5076 * the user, as these may have correct device names/devids, etc.
5078 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
5079 &spares
, &nspares
) == 0) {
5080 if (spa
->spa_spares
.sav_config
)
5081 VERIFY(nvlist_remove(spa
->spa_spares
.sav_config
,
5082 ZPOOL_CONFIG_SPARES
, DATA_TYPE_NVLIST_ARRAY
) == 0);
5084 VERIFY(nvlist_alloc(&spa
->spa_spares
.sav_config
,
5085 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
5086 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
5087 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
5088 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5089 spa_load_spares(spa
);
5090 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5091 spa
->spa_spares
.sav_sync
= B_TRUE
;
5093 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
5094 &l2cache
, &nl2cache
) == 0) {
5095 if (spa
->spa_l2cache
.sav_config
)
5096 VERIFY(nvlist_remove(spa
->spa_l2cache
.sav_config
,
5097 ZPOOL_CONFIG_L2CACHE
, DATA_TYPE_NVLIST_ARRAY
) == 0);
5099 VERIFY(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
5100 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
5101 VERIFY(nvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
5102 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
5103 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5104 spa_load_l2cache(spa
);
5105 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5106 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
5110 * Check for any removed devices.
5112 if (spa
->spa_autoreplace
) {
5113 spa_aux_check_removed(&spa
->spa_spares
);
5114 spa_aux_check_removed(&spa
->spa_l2cache
);
5117 if (spa_writeable(spa
)) {
5119 * Update the config cache to include the newly-imported pool.
5121 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
5125 * It's possible that the pool was expanded while it was exported.
5126 * We kick off an async task to handle this for us.
5128 spa_async_request(spa
, SPA_ASYNC_AUTOEXPAND
);
5130 spa_history_log_version(spa
, "import", NULL
);
5132 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_IMPORT
);
5134 zvol_create_minors(spa
, pool
, B_TRUE
);
5136 mutex_exit(&spa_namespace_lock
);
5142 spa_tryimport(nvlist_t
*tryconfig
)
5144 nvlist_t
*config
= NULL
;
5145 char *poolname
, *cachefile
;
5149 zpool_rewind_policy_t policy
;
5151 if (nvlist_lookup_string(tryconfig
, ZPOOL_CONFIG_POOL_NAME
, &poolname
))
5154 if (nvlist_lookup_uint64(tryconfig
, ZPOOL_CONFIG_POOL_STATE
, &state
))
5158 * Create and initialize the spa structure.
5160 mutex_enter(&spa_namespace_lock
);
5161 spa
= spa_add(TRYIMPORT_NAME
, tryconfig
, NULL
);
5162 spa_activate(spa
, FREAD
);
5165 * Rewind pool if a max txg was provided. Note that even though we
5166 * retrieve the complete rewind policy, only the rewind txg is relevant
5169 zpool_get_rewind_policy(spa
->spa_config
, &policy
);
5170 if (policy
.zrp_txg
!= UINT64_MAX
) {
5171 spa
->spa_load_max_txg
= policy
.zrp_txg
;
5172 spa
->spa_extreme_rewind
= B_TRUE
;
5173 zfs_dbgmsg("spa_tryimport: importing %s, max_txg=%lld",
5174 poolname
, (longlong_t
)policy
.zrp_txg
);
5176 zfs_dbgmsg("spa_tryimport: importing %s", poolname
);
5179 if (nvlist_lookup_string(tryconfig
, ZPOOL_CONFIG_CACHEFILE
, &cachefile
)
5181 zfs_dbgmsg("spa_tryimport: using cachefile '%s'", cachefile
);
5182 spa
->spa_config_source
= SPA_CONFIG_SRC_CACHEFILE
;
5184 spa
->spa_config_source
= SPA_CONFIG_SRC_SCAN
;
5187 error
= spa_load(spa
, SPA_LOAD_TRYIMPORT
, SPA_IMPORT_EXISTING
);
5190 * If 'tryconfig' was at least parsable, return the current config.
5192 if (spa
->spa_root_vdev
!= NULL
) {
5193 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
5194 VERIFY(nvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
,
5196 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
5198 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_TIMESTAMP
,
5199 spa
->spa_uberblock
.ub_timestamp
) == 0);
5200 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
5201 spa
->spa_load_info
) == 0);
5202 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_ERRATA
,
5203 spa
->spa_errata
) == 0);
5206 * If the bootfs property exists on this pool then we
5207 * copy it out so that external consumers can tell which
5208 * pools are bootable.
5210 if ((!error
|| error
== EEXIST
) && spa
->spa_bootfs
) {
5211 char *tmpname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
5214 * We have to play games with the name since the
5215 * pool was opened as TRYIMPORT_NAME.
5217 if (dsl_dsobj_to_dsname(spa_name(spa
),
5218 spa
->spa_bootfs
, tmpname
) == 0) {
5222 dsname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
5224 cp
= strchr(tmpname
, '/');
5226 (void) strlcpy(dsname
, tmpname
,
5229 (void) snprintf(dsname
, MAXPATHLEN
,
5230 "%s/%s", poolname
, ++cp
);
5232 VERIFY(nvlist_add_string(config
,
5233 ZPOOL_CONFIG_BOOTFS
, dsname
) == 0);
5234 kmem_free(dsname
, MAXPATHLEN
);
5236 kmem_free(tmpname
, MAXPATHLEN
);
5240 * Add the list of hot spares and level 2 cache devices.
5242 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
5243 spa_add_spares(spa
, config
);
5244 spa_add_l2cache(spa
, config
);
5245 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
5249 spa_deactivate(spa
);
5251 mutex_exit(&spa_namespace_lock
);
5257 * Pool export/destroy
5259 * The act of destroying or exporting a pool is very simple. We make sure there
5260 * is no more pending I/O and any references to the pool are gone. Then, we
5261 * update the pool state and sync all the labels to disk, removing the
5262 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
5263 * we don't sync the labels or remove the configuration cache.
5266 spa_export_common(char *pool
, int new_state
, nvlist_t
**oldconfig
,
5267 boolean_t force
, boolean_t hardforce
)
5274 if (!(spa_mode_global
& FWRITE
))
5275 return (SET_ERROR(EROFS
));
5277 mutex_enter(&spa_namespace_lock
);
5278 if ((spa
= spa_lookup(pool
)) == NULL
) {
5279 mutex_exit(&spa_namespace_lock
);
5280 return (SET_ERROR(ENOENT
));
5284 * Put a hold on the pool, drop the namespace lock, stop async tasks,
5285 * reacquire the namespace lock, and see if we can export.
5287 spa_open_ref(spa
, FTAG
);
5288 mutex_exit(&spa_namespace_lock
);
5289 spa_async_suspend(spa
);
5290 if (spa
->spa_zvol_taskq
) {
5291 zvol_remove_minors(spa
, spa_name(spa
), B_TRUE
);
5292 taskq_wait(spa
->spa_zvol_taskq
);
5294 mutex_enter(&spa_namespace_lock
);
5295 spa_close(spa
, FTAG
);
5297 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
)
5300 * The pool will be in core if it's openable, in which case we can
5301 * modify its state. Objsets may be open only because they're dirty,
5302 * so we have to force it to sync before checking spa_refcnt.
5304 if (spa
->spa_sync_on
) {
5305 txg_wait_synced(spa
->spa_dsl_pool
, 0);
5306 spa_evicting_os_wait(spa
);
5310 * A pool cannot be exported or destroyed if there are active
5311 * references. If we are resetting a pool, allow references by
5312 * fault injection handlers.
5314 if (!spa_refcount_zero(spa
) ||
5315 (spa
->spa_inject_ref
!= 0 &&
5316 new_state
!= POOL_STATE_UNINITIALIZED
)) {
5317 spa_async_resume(spa
);
5318 mutex_exit(&spa_namespace_lock
);
5319 return (SET_ERROR(EBUSY
));
5322 if (spa
->spa_sync_on
) {
5324 * A pool cannot be exported if it has an active shared spare.
5325 * This is to prevent other pools stealing the active spare
5326 * from an exported pool. At user's own will, such pool can
5327 * be forcedly exported.
5329 if (!force
&& new_state
== POOL_STATE_EXPORTED
&&
5330 spa_has_active_shared_spare(spa
)) {
5331 spa_async_resume(spa
);
5332 mutex_exit(&spa_namespace_lock
);
5333 return (SET_ERROR(EXDEV
));
5337 * We want this to be reflected on every label,
5338 * so mark them all dirty. spa_unload() will do the
5339 * final sync that pushes these changes out.
5341 if (new_state
!= POOL_STATE_UNINITIALIZED
&& !hardforce
) {
5342 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5343 spa
->spa_state
= new_state
;
5344 spa
->spa_final_txg
= spa_last_synced_txg(spa
) +
5346 vdev_config_dirty(spa
->spa_root_vdev
);
5347 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5352 if (new_state
== POOL_STATE_DESTROYED
)
5353 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_DESTROY
);
5354 else if (new_state
== POOL_STATE_EXPORTED
)
5355 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_EXPORT
);
5357 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
5359 spa_deactivate(spa
);
5362 if (oldconfig
&& spa
->spa_config
)
5363 VERIFY(nvlist_dup(spa
->spa_config
, oldconfig
, 0) == 0);
5365 if (new_state
!= POOL_STATE_UNINITIALIZED
) {
5367 spa_write_cachefile(spa
, B_TRUE
, B_TRUE
);
5370 mutex_exit(&spa_namespace_lock
);
5376 * Destroy a storage pool.
5379 spa_destroy(char *pool
)
5381 return (spa_export_common(pool
, POOL_STATE_DESTROYED
, NULL
,
5386 * Export a storage pool.
5389 spa_export(char *pool
, nvlist_t
**oldconfig
, boolean_t force
,
5390 boolean_t hardforce
)
5392 return (spa_export_common(pool
, POOL_STATE_EXPORTED
, oldconfig
,
5397 * Similar to spa_export(), this unloads the spa_t without actually removing it
5398 * from the namespace in any way.
5401 spa_reset(char *pool
)
5403 return (spa_export_common(pool
, POOL_STATE_UNINITIALIZED
, NULL
,
5408 * ==========================================================================
5409 * Device manipulation
5410 * ==========================================================================
5414 * Add a device to a storage pool.
5417 spa_vdev_add(spa_t
*spa
, nvlist_t
*nvroot
)
5421 vdev_t
*rvd
= spa
->spa_root_vdev
;
5423 nvlist_t
**spares
, **l2cache
;
5424 uint_t nspares
, nl2cache
;
5426 ASSERT(spa_writeable(spa
));
5428 txg
= spa_vdev_enter(spa
);
5430 if ((error
= spa_config_parse(spa
, &vd
, nvroot
, NULL
, 0,
5431 VDEV_ALLOC_ADD
)) != 0)
5432 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5434 spa
->spa_pending_vdev
= vd
; /* spa_vdev_exit() will clear this */
5436 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
, &spares
,
5440 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
, &l2cache
,
5444 if (vd
->vdev_children
== 0 && nspares
== 0 && nl2cache
== 0)
5445 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
5447 if (vd
->vdev_children
!= 0 &&
5448 (error
= vdev_create(vd
, txg
, B_FALSE
)) != 0)
5449 return (spa_vdev_exit(spa
, vd
, txg
, error
));
5452 * We must validate the spares and l2cache devices after checking the
5453 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
5455 if ((error
= spa_validate_aux(spa
, nvroot
, txg
, VDEV_ALLOC_ADD
)) != 0)
5456 return (spa_vdev_exit(spa
, vd
, txg
, error
));
5459 * If we are in the middle of a device removal, we can only add
5460 * devices which match the existing devices in the pool.
5461 * If we are in the middle of a removal, or have some indirect
5462 * vdevs, we can not add raidz toplevels.
5464 if (spa
->spa_vdev_removal
!= NULL
||
5465 spa
->spa_removing_phys
.sr_prev_indirect_vdev
!= -1) {
5466 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
5467 tvd
= vd
->vdev_child
[c
];
5468 if (spa
->spa_vdev_removal
!= NULL
&&
5469 tvd
->vdev_ashift
!= spa
->spa_max_ashift
) {
5470 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
5472 /* Fail if top level vdev is raidz */
5473 if (tvd
->vdev_ops
== &vdev_raidz_ops
) {
5474 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
5477 * Need the top level mirror to be
5478 * a mirror of leaf vdevs only
5480 if (tvd
->vdev_ops
== &vdev_mirror_ops
) {
5481 for (uint64_t cid
= 0;
5482 cid
< tvd
->vdev_children
; cid
++) {
5483 vdev_t
*cvd
= tvd
->vdev_child
[cid
];
5484 if (!cvd
->vdev_ops
->vdev_op_leaf
) {
5485 return (spa_vdev_exit(spa
, vd
,
5493 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
5496 * Set the vdev id to the first hole, if one exists.
5498 for (id
= 0; id
< rvd
->vdev_children
; id
++) {
5499 if (rvd
->vdev_child
[id
]->vdev_ishole
) {
5500 vdev_free(rvd
->vdev_child
[id
]);
5504 tvd
= vd
->vdev_child
[c
];
5505 vdev_remove_child(vd
, tvd
);
5507 vdev_add_child(rvd
, tvd
);
5508 vdev_config_dirty(tvd
);
5512 spa_set_aux_vdevs(&spa
->spa_spares
, spares
, nspares
,
5513 ZPOOL_CONFIG_SPARES
);
5514 spa_load_spares(spa
);
5515 spa
->spa_spares
.sav_sync
= B_TRUE
;
5518 if (nl2cache
!= 0) {
5519 spa_set_aux_vdevs(&spa
->spa_l2cache
, l2cache
, nl2cache
,
5520 ZPOOL_CONFIG_L2CACHE
);
5521 spa_load_l2cache(spa
);
5522 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
5526 * We have to be careful when adding new vdevs to an existing pool.
5527 * If other threads start allocating from these vdevs before we
5528 * sync the config cache, and we lose power, then upon reboot we may
5529 * fail to open the pool because there are DVAs that the config cache
5530 * can't translate. Therefore, we first add the vdevs without
5531 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
5532 * and then let spa_config_update() initialize the new metaslabs.
5534 * spa_load() checks for added-but-not-initialized vdevs, so that
5535 * if we lose power at any point in this sequence, the remaining
5536 * steps will be completed the next time we load the pool.
5538 (void) spa_vdev_exit(spa
, vd
, txg
, 0);
5540 mutex_enter(&spa_namespace_lock
);
5541 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
5542 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_VDEV_ADD
);
5543 mutex_exit(&spa_namespace_lock
);
5549 * Attach a device to a mirror. The arguments are the path to any device
5550 * in the mirror, and the nvroot for the new device. If the path specifies
5551 * a device that is not mirrored, we automatically insert the mirror vdev.
5553 * If 'replacing' is specified, the new device is intended to replace the
5554 * existing device; in this case the two devices are made into their own
5555 * mirror using the 'replacing' vdev, which is functionally identical to
5556 * the mirror vdev (it actually reuses all the same ops) but has a few
5557 * extra rules: you can't attach to it after it's been created, and upon
5558 * completion of resilvering, the first disk (the one being replaced)
5559 * is automatically detached.
5562 spa_vdev_attach(spa_t
*spa
, uint64_t guid
, nvlist_t
*nvroot
, int replacing
)
5564 uint64_t txg
, dtl_max_txg
;
5565 ASSERTV(vdev_t
*rvd
= spa
->spa_root_vdev
);
5566 vdev_t
*oldvd
, *newvd
, *newrootvd
, *pvd
, *tvd
;
5568 char *oldvdpath
, *newvdpath
;
5572 ASSERT(spa_writeable(spa
));
5574 txg
= spa_vdev_enter(spa
);
5576 oldvd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
5578 if (spa
->spa_vdev_removal
!= NULL
)
5579 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
5582 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
5584 if (!oldvd
->vdev_ops
->vdev_op_leaf
)
5585 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
5587 pvd
= oldvd
->vdev_parent
;
5589 if ((error
= spa_config_parse(spa
, &newrootvd
, nvroot
, NULL
, 0,
5590 VDEV_ALLOC_ATTACH
)) != 0)
5591 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
5593 if (newrootvd
->vdev_children
!= 1)
5594 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
5596 newvd
= newrootvd
->vdev_child
[0];
5598 if (!newvd
->vdev_ops
->vdev_op_leaf
)
5599 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
5601 if ((error
= vdev_create(newrootvd
, txg
, replacing
)) != 0)
5602 return (spa_vdev_exit(spa
, newrootvd
, txg
, error
));
5605 * Spares can't replace logs
5607 if (oldvd
->vdev_top
->vdev_islog
&& newvd
->vdev_isspare
)
5608 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
5612 * For attach, the only allowable parent is a mirror or the root
5615 if (pvd
->vdev_ops
!= &vdev_mirror_ops
&&
5616 pvd
->vdev_ops
!= &vdev_root_ops
)
5617 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
5619 pvops
= &vdev_mirror_ops
;
5622 * Active hot spares can only be replaced by inactive hot
5625 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
5626 oldvd
->vdev_isspare
&&
5627 !spa_has_spare(spa
, newvd
->vdev_guid
))
5628 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
5631 * If the source is a hot spare, and the parent isn't already a
5632 * spare, then we want to create a new hot spare. Otherwise, we
5633 * want to create a replacing vdev. The user is not allowed to
5634 * attach to a spared vdev child unless the 'isspare' state is
5635 * the same (spare replaces spare, non-spare replaces
5638 if (pvd
->vdev_ops
== &vdev_replacing_ops
&&
5639 spa_version(spa
) < SPA_VERSION_MULTI_REPLACE
) {
5640 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
5641 } else if (pvd
->vdev_ops
== &vdev_spare_ops
&&
5642 newvd
->vdev_isspare
!= oldvd
->vdev_isspare
) {
5643 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
5646 if (newvd
->vdev_isspare
)
5647 pvops
= &vdev_spare_ops
;
5649 pvops
= &vdev_replacing_ops
;
5653 * Make sure the new device is big enough.
5655 if (newvd
->vdev_asize
< vdev_get_min_asize(oldvd
))
5656 return (spa_vdev_exit(spa
, newrootvd
, txg
, EOVERFLOW
));
5659 * The new device cannot have a higher alignment requirement
5660 * than the top-level vdev.
5662 if (newvd
->vdev_ashift
> oldvd
->vdev_top
->vdev_ashift
)
5663 return (spa_vdev_exit(spa
, newrootvd
, txg
, EDOM
));
5666 * If this is an in-place replacement, update oldvd's path and devid
5667 * to make it distinguishable from newvd, and unopenable from now on.
5669 if (strcmp(oldvd
->vdev_path
, newvd
->vdev_path
) == 0) {
5670 spa_strfree(oldvd
->vdev_path
);
5671 oldvd
->vdev_path
= kmem_alloc(strlen(newvd
->vdev_path
) + 5,
5673 (void) sprintf(oldvd
->vdev_path
, "%s/%s",
5674 newvd
->vdev_path
, "old");
5675 if (oldvd
->vdev_devid
!= NULL
) {
5676 spa_strfree(oldvd
->vdev_devid
);
5677 oldvd
->vdev_devid
= NULL
;
5681 /* mark the device being resilvered */
5682 newvd
->vdev_resilver_txg
= txg
;
5685 * If the parent is not a mirror, or if we're replacing, insert the new
5686 * mirror/replacing/spare vdev above oldvd.
5688 if (pvd
->vdev_ops
!= pvops
)
5689 pvd
= vdev_add_parent(oldvd
, pvops
);
5691 ASSERT(pvd
->vdev_top
->vdev_parent
== rvd
);
5692 ASSERT(pvd
->vdev_ops
== pvops
);
5693 ASSERT(oldvd
->vdev_parent
== pvd
);
5696 * Extract the new device from its root and add it to pvd.
5698 vdev_remove_child(newrootvd
, newvd
);
5699 newvd
->vdev_id
= pvd
->vdev_children
;
5700 newvd
->vdev_crtxg
= oldvd
->vdev_crtxg
;
5701 vdev_add_child(pvd
, newvd
);
5704 * Reevaluate the parent vdev state.
5706 vdev_propagate_state(pvd
);
5708 tvd
= newvd
->vdev_top
;
5709 ASSERT(pvd
->vdev_top
== tvd
);
5710 ASSERT(tvd
->vdev_parent
== rvd
);
5712 vdev_config_dirty(tvd
);
5715 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
5716 * for any dmu_sync-ed blocks. It will propagate upward when
5717 * spa_vdev_exit() calls vdev_dtl_reassess().
5719 dtl_max_txg
= txg
+ TXG_CONCURRENT_STATES
;
5721 vdev_dtl_dirty(newvd
, DTL_MISSING
, TXG_INITIAL
,
5722 dtl_max_txg
- TXG_INITIAL
);
5724 if (newvd
->vdev_isspare
) {
5725 spa_spare_activate(newvd
);
5726 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_VDEV_SPARE
);
5729 oldvdpath
= spa_strdup(oldvd
->vdev_path
);
5730 newvdpath
= spa_strdup(newvd
->vdev_path
);
5731 newvd_isspare
= newvd
->vdev_isspare
;
5734 * Mark newvd's DTL dirty in this txg.
5736 vdev_dirty(tvd
, VDD_DTL
, newvd
, txg
);
5739 * Schedule the resilver to restart in the future. We do this to
5740 * ensure that dmu_sync-ed blocks have been stitched into the
5741 * respective datasets.
5743 dsl_resilver_restart(spa
->spa_dsl_pool
, dtl_max_txg
);
5745 if (spa
->spa_bootfs
)
5746 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_BOOTFS_VDEV_ATTACH
);
5748 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_VDEV_ATTACH
);
5753 (void) spa_vdev_exit(spa
, newrootvd
, dtl_max_txg
, 0);
5755 spa_history_log_internal(spa
, "vdev attach", NULL
,
5756 "%s vdev=%s %s vdev=%s",
5757 replacing
&& newvd_isspare
? "spare in" :
5758 replacing
? "replace" : "attach", newvdpath
,
5759 replacing
? "for" : "to", oldvdpath
);
5761 spa_strfree(oldvdpath
);
5762 spa_strfree(newvdpath
);
5768 * Detach a device from a mirror or replacing vdev.
5770 * If 'replace_done' is specified, only detach if the parent
5771 * is a replacing vdev.
5774 spa_vdev_detach(spa_t
*spa
, uint64_t guid
, uint64_t pguid
, int replace_done
)
5778 ASSERTV(vdev_t
*rvd
= spa
->spa_root_vdev
);
5779 vdev_t
*vd
, *pvd
, *cvd
, *tvd
;
5780 boolean_t unspare
= B_FALSE
;
5781 uint64_t unspare_guid
= 0;
5784 ASSERT(spa_writeable(spa
));
5786 txg
= spa_vdev_enter(spa
);
5788 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
5791 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
5793 if (!vd
->vdev_ops
->vdev_op_leaf
)
5794 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
5796 pvd
= vd
->vdev_parent
;
5799 * If the parent/child relationship is not as expected, don't do it.
5800 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
5801 * vdev that's replacing B with C. The user's intent in replacing
5802 * is to go from M(A,B) to M(A,C). If the user decides to cancel
5803 * the replace by detaching C, the expected behavior is to end up
5804 * M(A,B). But suppose that right after deciding to detach C,
5805 * the replacement of B completes. We would have M(A,C), and then
5806 * ask to detach C, which would leave us with just A -- not what
5807 * the user wanted. To prevent this, we make sure that the
5808 * parent/child relationship hasn't changed -- in this example,
5809 * that C's parent is still the replacing vdev R.
5811 if (pvd
->vdev_guid
!= pguid
&& pguid
!= 0)
5812 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
5815 * Only 'replacing' or 'spare' vdevs can be replaced.
5817 if (replace_done
&& pvd
->vdev_ops
!= &vdev_replacing_ops
&&
5818 pvd
->vdev_ops
!= &vdev_spare_ops
)
5819 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
5821 ASSERT(pvd
->vdev_ops
!= &vdev_spare_ops
||
5822 spa_version(spa
) >= SPA_VERSION_SPARES
);
5825 * Only mirror, replacing, and spare vdevs support detach.
5827 if (pvd
->vdev_ops
!= &vdev_replacing_ops
&&
5828 pvd
->vdev_ops
!= &vdev_mirror_ops
&&
5829 pvd
->vdev_ops
!= &vdev_spare_ops
)
5830 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
5833 * If this device has the only valid copy of some data,
5834 * we cannot safely detach it.
5836 if (vdev_dtl_required(vd
))
5837 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
5839 ASSERT(pvd
->vdev_children
>= 2);
5842 * If we are detaching the second disk from a replacing vdev, then
5843 * check to see if we changed the original vdev's path to have "/old"
5844 * at the end in spa_vdev_attach(). If so, undo that change now.
5846 if (pvd
->vdev_ops
== &vdev_replacing_ops
&& vd
->vdev_id
> 0 &&
5847 vd
->vdev_path
!= NULL
) {
5848 size_t len
= strlen(vd
->vdev_path
);
5850 for (int c
= 0; c
< pvd
->vdev_children
; c
++) {
5851 cvd
= pvd
->vdev_child
[c
];
5853 if (cvd
== vd
|| cvd
->vdev_path
== NULL
)
5856 if (strncmp(cvd
->vdev_path
, vd
->vdev_path
, len
) == 0 &&
5857 strcmp(cvd
->vdev_path
+ len
, "/old") == 0) {
5858 spa_strfree(cvd
->vdev_path
);
5859 cvd
->vdev_path
= spa_strdup(vd
->vdev_path
);
5866 * If we are detaching the original disk from a spare, then it implies
5867 * that the spare should become a real disk, and be removed from the
5868 * active spare list for the pool.
5870 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
5872 pvd
->vdev_child
[pvd
->vdev_children
- 1]->vdev_isspare
)
5876 * Erase the disk labels so the disk can be used for other things.
5877 * This must be done after all other error cases are handled,
5878 * but before we disembowel vd (so we can still do I/O to it).
5879 * But if we can't do it, don't treat the error as fatal --
5880 * it may be that the unwritability of the disk is the reason
5881 * it's being detached!
5883 error
= vdev_label_init(vd
, 0, VDEV_LABEL_REMOVE
);
5886 * Remove vd from its parent and compact the parent's children.
5888 vdev_remove_child(pvd
, vd
);
5889 vdev_compact_children(pvd
);
5892 * Remember one of the remaining children so we can get tvd below.
5894 cvd
= pvd
->vdev_child
[pvd
->vdev_children
- 1];
5897 * If we need to remove the remaining child from the list of hot spares,
5898 * do it now, marking the vdev as no longer a spare in the process.
5899 * We must do this before vdev_remove_parent(), because that can
5900 * change the GUID if it creates a new toplevel GUID. For a similar
5901 * reason, we must remove the spare now, in the same txg as the detach;
5902 * otherwise someone could attach a new sibling, change the GUID, and
5903 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
5906 ASSERT(cvd
->vdev_isspare
);
5907 spa_spare_remove(cvd
);
5908 unspare_guid
= cvd
->vdev_guid
;
5909 (void) spa_vdev_remove(spa
, unspare_guid
, B_TRUE
);
5910 cvd
->vdev_unspare
= B_TRUE
;
5914 * If the parent mirror/replacing vdev only has one child,
5915 * the parent is no longer needed. Remove it from the tree.
5917 if (pvd
->vdev_children
== 1) {
5918 if (pvd
->vdev_ops
== &vdev_spare_ops
)
5919 cvd
->vdev_unspare
= B_FALSE
;
5920 vdev_remove_parent(cvd
);
5925 * We don't set tvd until now because the parent we just removed
5926 * may have been the previous top-level vdev.
5928 tvd
= cvd
->vdev_top
;
5929 ASSERT(tvd
->vdev_parent
== rvd
);
5932 * Reevaluate the parent vdev state.
5934 vdev_propagate_state(cvd
);
5937 * If the 'autoexpand' property is set on the pool then automatically
5938 * try to expand the size of the pool. For example if the device we
5939 * just detached was smaller than the others, it may be possible to
5940 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
5941 * first so that we can obtain the updated sizes of the leaf vdevs.
5943 if (spa
->spa_autoexpand
) {
5945 vdev_expand(tvd
, txg
);
5948 vdev_config_dirty(tvd
);
5951 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
5952 * vd->vdev_detached is set and free vd's DTL object in syncing context.
5953 * But first make sure we're not on any *other* txg's DTL list, to
5954 * prevent vd from being accessed after it's freed.
5956 vdpath
= spa_strdup(vd
->vdev_path
? vd
->vdev_path
: "none");
5957 for (int t
= 0; t
< TXG_SIZE
; t
++)
5958 (void) txg_list_remove_this(&tvd
->vdev_dtl_list
, vd
, t
);
5959 vd
->vdev_detached
= B_TRUE
;
5960 vdev_dirty(tvd
, VDD_DTL
, vd
, txg
);
5962 spa_event_notify(spa
, vd
, NULL
, ESC_ZFS_VDEV_REMOVE
);
5964 /* hang on to the spa before we release the lock */
5965 spa_open_ref(spa
, FTAG
);
5967 error
= spa_vdev_exit(spa
, vd
, txg
, 0);
5969 spa_history_log_internal(spa
, "detach", NULL
,
5971 spa_strfree(vdpath
);
5974 * If this was the removal of the original device in a hot spare vdev,
5975 * then we want to go through and remove the device from the hot spare
5976 * list of every other pool.
5979 spa_t
*altspa
= NULL
;
5981 mutex_enter(&spa_namespace_lock
);
5982 while ((altspa
= spa_next(altspa
)) != NULL
) {
5983 if (altspa
->spa_state
!= POOL_STATE_ACTIVE
||
5987 spa_open_ref(altspa
, FTAG
);
5988 mutex_exit(&spa_namespace_lock
);
5989 (void) spa_vdev_remove(altspa
, unspare_guid
, B_TRUE
);
5990 mutex_enter(&spa_namespace_lock
);
5991 spa_close(altspa
, FTAG
);
5993 mutex_exit(&spa_namespace_lock
);
5995 /* search the rest of the vdevs for spares to remove */
5996 spa_vdev_resilver_done(spa
);
5999 /* all done with the spa; OK to release */
6000 mutex_enter(&spa_namespace_lock
);
6001 spa_close(spa
, FTAG
);
6002 mutex_exit(&spa_namespace_lock
);
6008 * Split a set of devices from their mirrors, and create a new pool from them.
6011 spa_vdev_split_mirror(spa_t
*spa
, char *newname
, nvlist_t
*config
,
6012 nvlist_t
*props
, boolean_t exp
)
6015 uint64_t txg
, *glist
;
6017 uint_t c
, children
, lastlog
;
6018 nvlist_t
**child
, *nvl
, *tmp
;
6020 char *altroot
= NULL
;
6021 vdev_t
*rvd
, **vml
= NULL
; /* vdev modify list */
6022 boolean_t activate_slog
;
6024 ASSERT(spa_writeable(spa
));
6026 txg
= spa_vdev_enter(spa
);
6028 /* clear the log and flush everything up to now */
6029 activate_slog
= spa_passivate_log(spa
);
6030 (void) spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
6031 error
= spa_reset_logs(spa
);
6032 txg
= spa_vdev_config_enter(spa
);
6035 spa_activate_log(spa
);
6038 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
6040 /* check new spa name before going any further */
6041 if (spa_lookup(newname
) != NULL
)
6042 return (spa_vdev_exit(spa
, NULL
, txg
, EEXIST
));
6045 * scan through all the children to ensure they're all mirrors
6047 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvl
) != 0 ||
6048 nvlist_lookup_nvlist_array(nvl
, ZPOOL_CONFIG_CHILDREN
, &child
,
6050 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
6052 /* first, check to ensure we've got the right child count */
6053 rvd
= spa
->spa_root_vdev
;
6055 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
6056 vdev_t
*vd
= rvd
->vdev_child
[c
];
6058 /* don't count the holes & logs as children */
6059 if (vd
->vdev_islog
|| !vdev_is_concrete(vd
)) {
6067 if (children
!= (lastlog
!= 0 ? lastlog
: rvd
->vdev_children
))
6068 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
6070 /* next, ensure no spare or cache devices are part of the split */
6071 if (nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_SPARES
, &tmp
) == 0 ||
6072 nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_L2CACHE
, &tmp
) == 0)
6073 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
6075 vml
= kmem_zalloc(children
* sizeof (vdev_t
*), KM_SLEEP
);
6076 glist
= kmem_zalloc(children
* sizeof (uint64_t), KM_SLEEP
);
6078 /* then, loop over each vdev and validate it */
6079 for (c
= 0; c
< children
; c
++) {
6080 uint64_t is_hole
= 0;
6082 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
6086 if (spa
->spa_root_vdev
->vdev_child
[c
]->vdev_ishole
||
6087 spa
->spa_root_vdev
->vdev_child
[c
]->vdev_islog
) {
6090 error
= SET_ERROR(EINVAL
);
6095 /* which disk is going to be split? */
6096 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_GUID
,
6098 error
= SET_ERROR(EINVAL
);
6102 /* look it up in the spa */
6103 vml
[c
] = spa_lookup_by_guid(spa
, glist
[c
], B_FALSE
);
6104 if (vml
[c
] == NULL
) {
6105 error
= SET_ERROR(ENODEV
);
6109 /* make sure there's nothing stopping the split */
6110 if (vml
[c
]->vdev_parent
->vdev_ops
!= &vdev_mirror_ops
||
6111 vml
[c
]->vdev_islog
||
6112 !vdev_is_concrete(vml
[c
]) ||
6113 vml
[c
]->vdev_isspare
||
6114 vml
[c
]->vdev_isl2cache
||
6115 !vdev_writeable(vml
[c
]) ||
6116 vml
[c
]->vdev_children
!= 0 ||
6117 vml
[c
]->vdev_state
!= VDEV_STATE_HEALTHY
||
6118 c
!= spa
->spa_root_vdev
->vdev_child
[c
]->vdev_id
) {
6119 error
= SET_ERROR(EINVAL
);
6123 if (vdev_dtl_required(vml
[c
])) {
6124 error
= SET_ERROR(EBUSY
);
6128 /* we need certain info from the top level */
6129 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_ARRAY
,
6130 vml
[c
]->vdev_top
->vdev_ms_array
) == 0);
6131 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_SHIFT
,
6132 vml
[c
]->vdev_top
->vdev_ms_shift
) == 0);
6133 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASIZE
,
6134 vml
[c
]->vdev_top
->vdev_asize
) == 0);
6135 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASHIFT
,
6136 vml
[c
]->vdev_top
->vdev_ashift
) == 0);
6138 /* transfer per-vdev ZAPs */
6139 ASSERT3U(vml
[c
]->vdev_leaf_zap
, !=, 0);
6140 VERIFY0(nvlist_add_uint64(child
[c
],
6141 ZPOOL_CONFIG_VDEV_LEAF_ZAP
, vml
[c
]->vdev_leaf_zap
));
6143 ASSERT3U(vml
[c
]->vdev_top
->vdev_top_zap
, !=, 0);
6144 VERIFY0(nvlist_add_uint64(child
[c
],
6145 ZPOOL_CONFIG_VDEV_TOP_ZAP
,
6146 vml
[c
]->vdev_parent
->vdev_top_zap
));
6150 kmem_free(vml
, children
* sizeof (vdev_t
*));
6151 kmem_free(glist
, children
* sizeof (uint64_t));
6152 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
6155 /* stop writers from using the disks */
6156 for (c
= 0; c
< children
; c
++) {
6158 vml
[c
]->vdev_offline
= B_TRUE
;
6160 vdev_reopen(spa
->spa_root_vdev
);
6163 * Temporarily record the splitting vdevs in the spa config. This
6164 * will disappear once the config is regenerated.
6166 VERIFY(nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
6167 VERIFY(nvlist_add_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
6168 glist
, children
) == 0);
6169 kmem_free(glist
, children
* sizeof (uint64_t));
6171 mutex_enter(&spa
->spa_props_lock
);
6172 VERIFY(nvlist_add_nvlist(spa
->spa_config
, ZPOOL_CONFIG_SPLIT
,
6174 mutex_exit(&spa
->spa_props_lock
);
6175 spa
->spa_config_splitting
= nvl
;
6176 vdev_config_dirty(spa
->spa_root_vdev
);
6178 /* configure and create the new pool */
6179 VERIFY(nvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
, newname
) == 0);
6180 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
6181 exp
? POOL_STATE_EXPORTED
: POOL_STATE_ACTIVE
) == 0);
6182 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
6183 spa_version(spa
)) == 0);
6184 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
6185 spa
->spa_config_txg
) == 0);
6186 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
6187 spa_generate_guid(NULL
)) == 0);
6188 VERIFY0(nvlist_add_boolean(config
, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
));
6189 (void) nvlist_lookup_string(props
,
6190 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
6192 /* add the new pool to the namespace */
6193 newspa
= spa_add(newname
, config
, altroot
);
6194 newspa
->spa_avz_action
= AVZ_ACTION_REBUILD
;
6195 newspa
->spa_config_txg
= spa
->spa_config_txg
;
6196 spa_set_log_state(newspa
, SPA_LOG_CLEAR
);
6198 /* release the spa config lock, retaining the namespace lock */
6199 spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
6201 if (zio_injection_enabled
)
6202 zio_handle_panic_injection(spa
, FTAG
, 1);
6204 spa_activate(newspa
, spa_mode_global
);
6205 spa_async_suspend(newspa
);
6207 newspa
->spa_config_source
= SPA_CONFIG_SRC_SPLIT
;
6209 /* create the new pool from the disks of the original pool */
6210 error
= spa_load(newspa
, SPA_LOAD_IMPORT
, SPA_IMPORT_ASSEMBLE
);
6214 /* if that worked, generate a real config for the new pool */
6215 if (newspa
->spa_root_vdev
!= NULL
) {
6216 VERIFY(nvlist_alloc(&newspa
->spa_config_splitting
,
6217 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
6218 VERIFY(nvlist_add_uint64(newspa
->spa_config_splitting
,
6219 ZPOOL_CONFIG_SPLIT_GUID
, spa_guid(spa
)) == 0);
6220 spa_config_set(newspa
, spa_config_generate(newspa
, NULL
, -1ULL,
6225 if (props
!= NULL
) {
6226 spa_configfile_set(newspa
, props
, B_FALSE
);
6227 error
= spa_prop_set(newspa
, props
);
6232 /* flush everything */
6233 txg
= spa_vdev_config_enter(newspa
);
6234 vdev_config_dirty(newspa
->spa_root_vdev
);
6235 (void) spa_vdev_config_exit(newspa
, NULL
, txg
, 0, FTAG
);
6237 if (zio_injection_enabled
)
6238 zio_handle_panic_injection(spa
, FTAG
, 2);
6240 spa_async_resume(newspa
);
6242 /* finally, update the original pool's config */
6243 txg
= spa_vdev_config_enter(spa
);
6244 tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
6245 error
= dmu_tx_assign(tx
, TXG_WAIT
);
6248 for (c
= 0; c
< children
; c
++) {
6249 if (vml
[c
] != NULL
) {
6252 spa_history_log_internal(spa
, "detach", tx
,
6253 "vdev=%s", vml
[c
]->vdev_path
);
6258 spa
->spa_avz_action
= AVZ_ACTION_REBUILD
;
6259 vdev_config_dirty(spa
->spa_root_vdev
);
6260 spa
->spa_config_splitting
= NULL
;
6264 (void) spa_vdev_exit(spa
, NULL
, txg
, 0);
6266 if (zio_injection_enabled
)
6267 zio_handle_panic_injection(spa
, FTAG
, 3);
6269 /* split is complete; log a history record */
6270 spa_history_log_internal(newspa
, "split", NULL
,
6271 "from pool %s", spa_name(spa
));
6273 kmem_free(vml
, children
* sizeof (vdev_t
*));
6275 /* if we're not going to mount the filesystems in userland, export */
6277 error
= spa_export_common(newname
, POOL_STATE_EXPORTED
, NULL
,
6284 spa_deactivate(newspa
);
6287 txg
= spa_vdev_config_enter(spa
);
6289 /* re-online all offlined disks */
6290 for (c
= 0; c
< children
; c
++) {
6292 vml
[c
]->vdev_offline
= B_FALSE
;
6294 vdev_reopen(spa
->spa_root_vdev
);
6296 nvlist_free(spa
->spa_config_splitting
);
6297 spa
->spa_config_splitting
= NULL
;
6298 (void) spa_vdev_exit(spa
, NULL
, txg
, error
);
6300 kmem_free(vml
, children
* sizeof (vdev_t
*));
6305 * Find any device that's done replacing, or a vdev marked 'unspare' that's
6306 * currently spared, so we can detach it.
6309 spa_vdev_resilver_done_hunt(vdev_t
*vd
)
6311 vdev_t
*newvd
, *oldvd
;
6313 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
6314 oldvd
= spa_vdev_resilver_done_hunt(vd
->vdev_child
[c
]);
6320 * Check for a completed replacement. We always consider the first
6321 * vdev in the list to be the oldest vdev, and the last one to be
6322 * the newest (see spa_vdev_attach() for how that works). In
6323 * the case where the newest vdev is faulted, we will not automatically
6324 * remove it after a resilver completes. This is OK as it will require
6325 * user intervention to determine which disk the admin wishes to keep.
6327 if (vd
->vdev_ops
== &vdev_replacing_ops
) {
6328 ASSERT(vd
->vdev_children
> 1);
6330 newvd
= vd
->vdev_child
[vd
->vdev_children
- 1];
6331 oldvd
= vd
->vdev_child
[0];
6333 if (vdev_dtl_empty(newvd
, DTL_MISSING
) &&
6334 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
6335 !vdev_dtl_required(oldvd
))
6340 * Check for a completed resilver with the 'unspare' flag set.
6342 if (vd
->vdev_ops
== &vdev_spare_ops
) {
6343 vdev_t
*first
= vd
->vdev_child
[0];
6344 vdev_t
*last
= vd
->vdev_child
[vd
->vdev_children
- 1];
6346 if (last
->vdev_unspare
) {
6349 } else if (first
->vdev_unspare
) {
6356 if (oldvd
!= NULL
&&
6357 vdev_dtl_empty(newvd
, DTL_MISSING
) &&
6358 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
6359 !vdev_dtl_required(oldvd
))
6363 * If there are more than two spares attached to a disk,
6364 * and those spares are not required, then we want to
6365 * attempt to free them up now so that they can be used
6366 * by other pools. Once we're back down to a single
6367 * disk+spare, we stop removing them.
6369 if (vd
->vdev_children
> 2) {
6370 newvd
= vd
->vdev_child
[1];
6372 if (newvd
->vdev_isspare
&& last
->vdev_isspare
&&
6373 vdev_dtl_empty(last
, DTL_MISSING
) &&
6374 vdev_dtl_empty(last
, DTL_OUTAGE
) &&
6375 !vdev_dtl_required(newvd
))
6384 spa_vdev_resilver_done(spa_t
*spa
)
6386 vdev_t
*vd
, *pvd
, *ppvd
;
6387 uint64_t guid
, sguid
, pguid
, ppguid
;
6389 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6391 while ((vd
= spa_vdev_resilver_done_hunt(spa
->spa_root_vdev
)) != NULL
) {
6392 pvd
= vd
->vdev_parent
;
6393 ppvd
= pvd
->vdev_parent
;
6394 guid
= vd
->vdev_guid
;
6395 pguid
= pvd
->vdev_guid
;
6396 ppguid
= ppvd
->vdev_guid
;
6399 * If we have just finished replacing a hot spared device, then
6400 * we need to detach the parent's first child (the original hot
6403 if (ppvd
->vdev_ops
== &vdev_spare_ops
&& pvd
->vdev_id
== 0 &&
6404 ppvd
->vdev_children
== 2) {
6405 ASSERT(pvd
->vdev_ops
== &vdev_replacing_ops
);
6406 sguid
= ppvd
->vdev_child
[1]->vdev_guid
;
6408 ASSERT(vd
->vdev_resilver_txg
== 0 || !vdev_dtl_required(vd
));
6410 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6411 if (spa_vdev_detach(spa
, guid
, pguid
, B_TRUE
) != 0)
6413 if (sguid
&& spa_vdev_detach(spa
, sguid
, ppguid
, B_TRUE
) != 0)
6415 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6418 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6422 * Update the stored path or FRU for this vdev.
6425 spa_vdev_set_common(spa_t
*spa
, uint64_t guid
, const char *value
,
6429 boolean_t sync
= B_FALSE
;
6431 ASSERT(spa_writeable(spa
));
6433 spa_vdev_state_enter(spa
, SCL_ALL
);
6435 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
6436 return (spa_vdev_state_exit(spa
, NULL
, ENOENT
));
6438 if (!vd
->vdev_ops
->vdev_op_leaf
)
6439 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
6442 if (strcmp(value
, vd
->vdev_path
) != 0) {
6443 spa_strfree(vd
->vdev_path
);
6444 vd
->vdev_path
= spa_strdup(value
);
6448 if (vd
->vdev_fru
== NULL
) {
6449 vd
->vdev_fru
= spa_strdup(value
);
6451 } else if (strcmp(value
, vd
->vdev_fru
) != 0) {
6452 spa_strfree(vd
->vdev_fru
);
6453 vd
->vdev_fru
= spa_strdup(value
);
6458 return (spa_vdev_state_exit(spa
, sync
? vd
: NULL
, 0));
6462 spa_vdev_setpath(spa_t
*spa
, uint64_t guid
, const char *newpath
)
6464 return (spa_vdev_set_common(spa
, guid
, newpath
, B_TRUE
));
6468 spa_vdev_setfru(spa_t
*spa
, uint64_t guid
, const char *newfru
)
6470 return (spa_vdev_set_common(spa
, guid
, newfru
, B_FALSE
));
6474 * ==========================================================================
6476 * ==========================================================================
6479 spa_scrub_pause_resume(spa_t
*spa
, pool_scrub_cmd_t cmd
)
6481 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
6483 if (dsl_scan_resilvering(spa
->spa_dsl_pool
))
6484 return (SET_ERROR(EBUSY
));
6486 return (dsl_scrub_set_pause_resume(spa
->spa_dsl_pool
, cmd
));
6490 spa_scan_stop(spa_t
*spa
)
6492 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
6493 if (dsl_scan_resilvering(spa
->spa_dsl_pool
))
6494 return (SET_ERROR(EBUSY
));
6495 return (dsl_scan_cancel(spa
->spa_dsl_pool
));
6499 spa_scan(spa_t
*spa
, pool_scan_func_t func
)
6501 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
6503 if (func
>= POOL_SCAN_FUNCS
|| func
== POOL_SCAN_NONE
)
6504 return (SET_ERROR(ENOTSUP
));
6507 * If a resilver was requested, but there is no DTL on a
6508 * writeable leaf device, we have nothing to do.
6510 if (func
== POOL_SCAN_RESILVER
&&
6511 !vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
)) {
6512 spa_async_request(spa
, SPA_ASYNC_RESILVER_DONE
);
6516 return (dsl_scan(spa
->spa_dsl_pool
, func
));
6520 * ==========================================================================
6521 * SPA async task processing
6522 * ==========================================================================
6526 spa_async_remove(spa_t
*spa
, vdev_t
*vd
)
6528 if (vd
->vdev_remove_wanted
) {
6529 vd
->vdev_remove_wanted
= B_FALSE
;
6530 vd
->vdev_delayed_close
= B_FALSE
;
6531 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_REMOVED
, VDEV_AUX_NONE
);
6534 * We want to clear the stats, but we don't want to do a full
6535 * vdev_clear() as that will cause us to throw away
6536 * degraded/faulted state as well as attempt to reopen the
6537 * device, all of which is a waste.
6539 vd
->vdev_stat
.vs_read_errors
= 0;
6540 vd
->vdev_stat
.vs_write_errors
= 0;
6541 vd
->vdev_stat
.vs_checksum_errors
= 0;
6543 vdev_state_dirty(vd
->vdev_top
);
6546 for (int c
= 0; c
< vd
->vdev_children
; c
++)
6547 spa_async_remove(spa
, vd
->vdev_child
[c
]);
6551 spa_async_probe(spa_t
*spa
, vdev_t
*vd
)
6553 if (vd
->vdev_probe_wanted
) {
6554 vd
->vdev_probe_wanted
= B_FALSE
;
6555 vdev_reopen(vd
); /* vdev_open() does the actual probe */
6558 for (int c
= 0; c
< vd
->vdev_children
; c
++)
6559 spa_async_probe(spa
, vd
->vdev_child
[c
]);
6563 spa_async_autoexpand(spa_t
*spa
, vdev_t
*vd
)
6565 if (!spa
->spa_autoexpand
)
6568 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
6569 vdev_t
*cvd
= vd
->vdev_child
[c
];
6570 spa_async_autoexpand(spa
, cvd
);
6573 if (!vd
->vdev_ops
->vdev_op_leaf
|| vd
->vdev_physpath
== NULL
)
6576 spa_event_notify(vd
->vdev_spa
, vd
, NULL
, ESC_ZFS_VDEV_AUTOEXPAND
);
6580 spa_async_thread(void *arg
)
6582 spa_t
*spa
= (spa_t
*)arg
;
6585 ASSERT(spa
->spa_sync_on
);
6587 mutex_enter(&spa
->spa_async_lock
);
6588 tasks
= spa
->spa_async_tasks
;
6589 spa
->spa_async_tasks
= 0;
6590 mutex_exit(&spa
->spa_async_lock
);
6593 * See if the config needs to be updated.
6595 if (tasks
& SPA_ASYNC_CONFIG_UPDATE
) {
6596 uint64_t old_space
, new_space
;
6598 mutex_enter(&spa_namespace_lock
);
6599 old_space
= metaslab_class_get_space(spa_normal_class(spa
));
6600 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
6601 new_space
= metaslab_class_get_space(spa_normal_class(spa
));
6602 mutex_exit(&spa_namespace_lock
);
6605 * If the pool grew as a result of the config update,
6606 * then log an internal history event.
6608 if (new_space
!= old_space
) {
6609 spa_history_log_internal(spa
, "vdev online", NULL
,
6610 "pool '%s' size: %llu(+%llu)",
6611 spa_name(spa
), new_space
, new_space
- old_space
);
6616 * See if any devices need to be marked REMOVED.
6618 if (tasks
& SPA_ASYNC_REMOVE
) {
6619 spa_vdev_state_enter(spa
, SCL_NONE
);
6620 spa_async_remove(spa
, spa
->spa_root_vdev
);
6621 for (int i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++)
6622 spa_async_remove(spa
, spa
->spa_l2cache
.sav_vdevs
[i
]);
6623 for (int i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
6624 spa_async_remove(spa
, spa
->spa_spares
.sav_vdevs
[i
]);
6625 (void) spa_vdev_state_exit(spa
, NULL
, 0);
6628 if ((tasks
& SPA_ASYNC_AUTOEXPAND
) && !spa_suspended(spa
)) {
6629 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
6630 spa_async_autoexpand(spa
, spa
->spa_root_vdev
);
6631 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
6635 * See if any devices need to be probed.
6637 if (tasks
& SPA_ASYNC_PROBE
) {
6638 spa_vdev_state_enter(spa
, SCL_NONE
);
6639 spa_async_probe(spa
, spa
->spa_root_vdev
);
6640 (void) spa_vdev_state_exit(spa
, NULL
, 0);
6644 * If any devices are done replacing, detach them.
6646 if (tasks
& SPA_ASYNC_RESILVER_DONE
)
6647 spa_vdev_resilver_done(spa
);
6650 * Kick off a resilver.
6652 if (tasks
& SPA_ASYNC_RESILVER
)
6653 dsl_resilver_restart(spa
->spa_dsl_pool
, 0);
6656 * Let the world know that we're done.
6658 mutex_enter(&spa
->spa_async_lock
);
6659 spa
->spa_async_thread
= NULL
;
6660 cv_broadcast(&spa
->spa_async_cv
);
6661 mutex_exit(&spa
->spa_async_lock
);
6666 spa_async_suspend(spa_t
*spa
)
6668 mutex_enter(&spa
->spa_async_lock
);
6669 spa
->spa_async_suspended
++;
6670 while (spa
->spa_async_thread
!= NULL
)
6671 cv_wait(&spa
->spa_async_cv
, &spa
->spa_async_lock
);
6672 mutex_exit(&spa
->spa_async_lock
);
6674 spa_vdev_remove_suspend(spa
);
6676 zthr_t
*condense_thread
= spa
->spa_condense_zthr
;
6677 if (condense_thread
!= NULL
&& zthr_isrunning(condense_thread
))
6678 VERIFY0(zthr_cancel(condense_thread
));
6682 spa_async_resume(spa_t
*spa
)
6684 mutex_enter(&spa
->spa_async_lock
);
6685 ASSERT(spa
->spa_async_suspended
!= 0);
6686 spa
->spa_async_suspended
--;
6687 mutex_exit(&spa
->spa_async_lock
);
6688 spa_restart_removal(spa
);
6690 zthr_t
*condense_thread
= spa
->spa_condense_zthr
;
6691 if (condense_thread
!= NULL
&& !zthr_isrunning(condense_thread
))
6692 zthr_resume(condense_thread
);
6696 spa_async_tasks_pending(spa_t
*spa
)
6698 uint_t non_config_tasks
;
6700 boolean_t config_task_suspended
;
6702 non_config_tasks
= spa
->spa_async_tasks
& ~SPA_ASYNC_CONFIG_UPDATE
;
6703 config_task
= spa
->spa_async_tasks
& SPA_ASYNC_CONFIG_UPDATE
;
6704 if (spa
->spa_ccw_fail_time
== 0) {
6705 config_task_suspended
= B_FALSE
;
6707 config_task_suspended
=
6708 (gethrtime() - spa
->spa_ccw_fail_time
) <
6709 ((hrtime_t
)zfs_ccw_retry_interval
* NANOSEC
);
6712 return (non_config_tasks
|| (config_task
&& !config_task_suspended
));
6716 spa_async_dispatch(spa_t
*spa
)
6718 mutex_enter(&spa
->spa_async_lock
);
6719 if (spa_async_tasks_pending(spa
) &&
6720 !spa
->spa_async_suspended
&&
6721 spa
->spa_async_thread
== NULL
&&
6723 spa
->spa_async_thread
= thread_create(NULL
, 0,
6724 spa_async_thread
, spa
, 0, &p0
, TS_RUN
, maxclsyspri
);
6725 mutex_exit(&spa
->spa_async_lock
);
6729 spa_async_request(spa_t
*spa
, int task
)
6731 zfs_dbgmsg("spa=%s async request task=%u", spa
->spa_name
, task
);
6732 mutex_enter(&spa
->spa_async_lock
);
6733 spa
->spa_async_tasks
|= task
;
6734 mutex_exit(&spa
->spa_async_lock
);
6738 * ==========================================================================
6739 * SPA syncing routines
6740 * ==========================================================================
6744 bpobj_enqueue_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
6747 bpobj_enqueue(bpo
, bp
, tx
);
6752 spa_free_sync_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
6756 zio_nowait(zio_free_sync(zio
, zio
->io_spa
, dmu_tx_get_txg(tx
), bp
,
6762 * Note: this simple function is not inlined to make it easier to dtrace the
6763 * amount of time spent syncing frees.
6766 spa_sync_frees(spa_t
*spa
, bplist_t
*bpl
, dmu_tx_t
*tx
)
6768 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
6769 bplist_iterate(bpl
, spa_free_sync_cb
, zio
, tx
);
6770 VERIFY(zio_wait(zio
) == 0);
6774 * Note: this simple function is not inlined to make it easier to dtrace the
6775 * amount of time spent syncing deferred frees.
6778 spa_sync_deferred_frees(spa_t
*spa
, dmu_tx_t
*tx
)
6780 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
6781 VERIFY3U(bpobj_iterate(&spa
->spa_deferred_bpobj
,
6782 spa_free_sync_cb
, zio
, tx
), ==, 0);
6783 VERIFY0(zio_wait(zio
));
6787 spa_sync_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
*nv
, dmu_tx_t
*tx
)
6789 char *packed
= NULL
;
6794 VERIFY(nvlist_size(nv
, &nvsize
, NV_ENCODE_XDR
) == 0);
6797 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
6798 * information. This avoids the dmu_buf_will_dirty() path and
6799 * saves us a pre-read to get data we don't actually care about.
6801 bufsize
= P2ROUNDUP((uint64_t)nvsize
, SPA_CONFIG_BLOCKSIZE
);
6802 packed
= vmem_alloc(bufsize
, KM_SLEEP
);
6804 VERIFY(nvlist_pack(nv
, &packed
, &nvsize
, NV_ENCODE_XDR
,
6806 bzero(packed
+ nvsize
, bufsize
- nvsize
);
6808 dmu_write(spa
->spa_meta_objset
, obj
, 0, bufsize
, packed
, tx
);
6810 vmem_free(packed
, bufsize
);
6812 VERIFY(0 == dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
));
6813 dmu_buf_will_dirty(db
, tx
);
6814 *(uint64_t *)db
->db_data
= nvsize
;
6815 dmu_buf_rele(db
, FTAG
);
6819 spa_sync_aux_dev(spa_t
*spa
, spa_aux_vdev_t
*sav
, dmu_tx_t
*tx
,
6820 const char *config
, const char *entry
)
6830 * Update the MOS nvlist describing the list of available devices.
6831 * spa_validate_aux() will have already made sure this nvlist is
6832 * valid and the vdevs are labeled appropriately.
6834 if (sav
->sav_object
== 0) {
6835 sav
->sav_object
= dmu_object_alloc(spa
->spa_meta_objset
,
6836 DMU_OT_PACKED_NVLIST
, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE
,
6837 sizeof (uint64_t), tx
);
6838 VERIFY(zap_update(spa
->spa_meta_objset
,
6839 DMU_POOL_DIRECTORY_OBJECT
, entry
, sizeof (uint64_t), 1,
6840 &sav
->sav_object
, tx
) == 0);
6843 VERIFY(nvlist_alloc(&nvroot
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
6844 if (sav
->sav_count
== 0) {
6845 VERIFY(nvlist_add_nvlist_array(nvroot
, config
, NULL
, 0) == 0);
6847 list
= kmem_alloc(sav
->sav_count
*sizeof (void *), KM_SLEEP
);
6848 for (i
= 0; i
< sav
->sav_count
; i
++)
6849 list
[i
] = vdev_config_generate(spa
, sav
->sav_vdevs
[i
],
6850 B_FALSE
, VDEV_CONFIG_L2CACHE
);
6851 VERIFY(nvlist_add_nvlist_array(nvroot
, config
, list
,
6852 sav
->sav_count
) == 0);
6853 for (i
= 0; i
< sav
->sav_count
; i
++)
6854 nvlist_free(list
[i
]);
6855 kmem_free(list
, sav
->sav_count
* sizeof (void *));
6858 spa_sync_nvlist(spa
, sav
->sav_object
, nvroot
, tx
);
6859 nvlist_free(nvroot
);
6861 sav
->sav_sync
= B_FALSE
;
6865 * Rebuild spa's all-vdev ZAP from the vdev ZAPs indicated in each vdev_t.
6866 * The all-vdev ZAP must be empty.
6869 spa_avz_build(vdev_t
*vd
, uint64_t avz
, dmu_tx_t
*tx
)
6871 spa_t
*spa
= vd
->vdev_spa
;
6873 if (vd
->vdev_top_zap
!= 0) {
6874 VERIFY0(zap_add_int(spa
->spa_meta_objset
, avz
,
6875 vd
->vdev_top_zap
, tx
));
6877 if (vd
->vdev_leaf_zap
!= 0) {
6878 VERIFY0(zap_add_int(spa
->spa_meta_objset
, avz
,
6879 vd
->vdev_leaf_zap
, tx
));
6881 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
6882 spa_avz_build(vd
->vdev_child
[i
], avz
, tx
);
6887 spa_sync_config_object(spa_t
*spa
, dmu_tx_t
*tx
)
6892 * If the pool is being imported from a pre-per-vdev-ZAP version of ZFS,
6893 * its config may not be dirty but we still need to build per-vdev ZAPs.
6894 * Similarly, if the pool is being assembled (e.g. after a split), we
6895 * need to rebuild the AVZ although the config may not be dirty.
6897 if (list_is_empty(&spa
->spa_config_dirty_list
) &&
6898 spa
->spa_avz_action
== AVZ_ACTION_NONE
)
6901 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
6903 ASSERT(spa
->spa_avz_action
== AVZ_ACTION_NONE
||
6904 spa
->spa_avz_action
== AVZ_ACTION_INITIALIZE
||
6905 spa
->spa_all_vdev_zaps
!= 0);
6907 if (spa
->spa_avz_action
== AVZ_ACTION_REBUILD
) {
6908 /* Make and build the new AVZ */
6909 uint64_t new_avz
= zap_create(spa
->spa_meta_objset
,
6910 DMU_OTN_ZAP_METADATA
, DMU_OT_NONE
, 0, tx
);
6911 spa_avz_build(spa
->spa_root_vdev
, new_avz
, tx
);
6913 /* Diff old AVZ with new one */
6917 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
6918 spa
->spa_all_vdev_zaps
);
6919 zap_cursor_retrieve(&zc
, &za
) == 0;
6920 zap_cursor_advance(&zc
)) {
6921 uint64_t vdzap
= za
.za_first_integer
;
6922 if (zap_lookup_int(spa
->spa_meta_objset
, new_avz
,
6925 * ZAP is listed in old AVZ but not in new one;
6928 VERIFY0(zap_destroy(spa
->spa_meta_objset
, vdzap
,
6933 zap_cursor_fini(&zc
);
6935 /* Destroy the old AVZ */
6936 VERIFY0(zap_destroy(spa
->spa_meta_objset
,
6937 spa
->spa_all_vdev_zaps
, tx
));
6939 /* Replace the old AVZ in the dir obj with the new one */
6940 VERIFY0(zap_update(spa
->spa_meta_objset
,
6941 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_VDEV_ZAP_MAP
,
6942 sizeof (new_avz
), 1, &new_avz
, tx
));
6944 spa
->spa_all_vdev_zaps
= new_avz
;
6945 } else if (spa
->spa_avz_action
== AVZ_ACTION_DESTROY
) {
6949 /* Walk through the AVZ and destroy all listed ZAPs */
6950 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
6951 spa
->spa_all_vdev_zaps
);
6952 zap_cursor_retrieve(&zc
, &za
) == 0;
6953 zap_cursor_advance(&zc
)) {
6954 uint64_t zap
= za
.za_first_integer
;
6955 VERIFY0(zap_destroy(spa
->spa_meta_objset
, zap
, tx
));
6958 zap_cursor_fini(&zc
);
6960 /* Destroy and unlink the AVZ itself */
6961 VERIFY0(zap_destroy(spa
->spa_meta_objset
,
6962 spa
->spa_all_vdev_zaps
, tx
));
6963 VERIFY0(zap_remove(spa
->spa_meta_objset
,
6964 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_VDEV_ZAP_MAP
, tx
));
6965 spa
->spa_all_vdev_zaps
= 0;
6968 if (spa
->spa_all_vdev_zaps
== 0) {
6969 spa
->spa_all_vdev_zaps
= zap_create_link(spa
->spa_meta_objset
,
6970 DMU_OTN_ZAP_METADATA
, DMU_POOL_DIRECTORY_OBJECT
,
6971 DMU_POOL_VDEV_ZAP_MAP
, tx
);
6973 spa
->spa_avz_action
= AVZ_ACTION_NONE
;
6975 /* Create ZAPs for vdevs that don't have them. */
6976 vdev_construct_zaps(spa
->spa_root_vdev
, tx
);
6978 config
= spa_config_generate(spa
, spa
->spa_root_vdev
,
6979 dmu_tx_get_txg(tx
), B_FALSE
);
6982 * If we're upgrading the spa version then make sure that
6983 * the config object gets updated with the correct version.
6985 if (spa
->spa_ubsync
.ub_version
< spa
->spa_uberblock
.ub_version
)
6986 fnvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
6987 spa
->spa_uberblock
.ub_version
);
6989 spa_config_exit(spa
, SCL_STATE
, FTAG
);
6991 nvlist_free(spa
->spa_config_syncing
);
6992 spa
->spa_config_syncing
= config
;
6994 spa_sync_nvlist(spa
, spa
->spa_config_object
, config
, tx
);
6998 spa_sync_version(void *arg
, dmu_tx_t
*tx
)
7000 uint64_t *versionp
= arg
;
7001 uint64_t version
= *versionp
;
7002 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
7005 * Setting the version is special cased when first creating the pool.
7007 ASSERT(tx
->tx_txg
!= TXG_INITIAL
);
7009 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
7010 ASSERT(version
>= spa_version(spa
));
7012 spa
->spa_uberblock
.ub_version
= version
;
7013 vdev_config_dirty(spa
->spa_root_vdev
);
7014 spa_history_log_internal(spa
, "set", tx
, "version=%lld", version
);
7018 * Set zpool properties.
7021 spa_sync_props(void *arg
, dmu_tx_t
*tx
)
7023 nvlist_t
*nvp
= arg
;
7024 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
7025 objset_t
*mos
= spa
->spa_meta_objset
;
7026 nvpair_t
*elem
= NULL
;
7028 mutex_enter(&spa
->spa_props_lock
);
7030 while ((elem
= nvlist_next_nvpair(nvp
, elem
))) {
7032 char *strval
, *fname
;
7034 const char *propname
;
7035 zprop_type_t proptype
;
7038 switch (prop
= zpool_name_to_prop(nvpair_name(elem
))) {
7039 case ZPOOL_PROP_INVAL
:
7041 * We checked this earlier in spa_prop_validate().
7043 ASSERT(zpool_prop_feature(nvpair_name(elem
)));
7045 fname
= strchr(nvpair_name(elem
), '@') + 1;
7046 VERIFY0(zfeature_lookup_name(fname
, &fid
));
7048 spa_feature_enable(spa
, fid
, tx
);
7049 spa_history_log_internal(spa
, "set", tx
,
7050 "%s=enabled", nvpair_name(elem
));
7053 case ZPOOL_PROP_VERSION
:
7054 intval
= fnvpair_value_uint64(elem
);
7056 * The version is synced separately before other
7057 * properties and should be correct by now.
7059 ASSERT3U(spa_version(spa
), >=, intval
);
7062 case ZPOOL_PROP_ALTROOT
:
7064 * 'altroot' is a non-persistent property. It should
7065 * have been set temporarily at creation or import time.
7067 ASSERT(spa
->spa_root
!= NULL
);
7070 case ZPOOL_PROP_READONLY
:
7071 case ZPOOL_PROP_CACHEFILE
:
7073 * 'readonly' and 'cachefile' are also non-persisitent
7077 case ZPOOL_PROP_COMMENT
:
7078 strval
= fnvpair_value_string(elem
);
7079 if (spa
->spa_comment
!= NULL
)
7080 spa_strfree(spa
->spa_comment
);
7081 spa
->spa_comment
= spa_strdup(strval
);
7083 * We need to dirty the configuration on all the vdevs
7084 * so that their labels get updated. It's unnecessary
7085 * to do this for pool creation since the vdev's
7086 * configuration has already been dirtied.
7088 if (tx
->tx_txg
!= TXG_INITIAL
)
7089 vdev_config_dirty(spa
->spa_root_vdev
);
7090 spa_history_log_internal(spa
, "set", tx
,
7091 "%s=%s", nvpair_name(elem
), strval
);
7095 * Set pool property values in the poolprops mos object.
7097 if (spa
->spa_pool_props_object
== 0) {
7098 spa
->spa_pool_props_object
=
7099 zap_create_link(mos
, DMU_OT_POOL_PROPS
,
7100 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_PROPS
,
7104 /* normalize the property name */
7105 propname
= zpool_prop_to_name(prop
);
7106 proptype
= zpool_prop_get_type(prop
);
7108 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
7109 ASSERT(proptype
== PROP_TYPE_STRING
);
7110 strval
= fnvpair_value_string(elem
);
7111 VERIFY0(zap_update(mos
,
7112 spa
->spa_pool_props_object
, propname
,
7113 1, strlen(strval
) + 1, strval
, tx
));
7114 spa_history_log_internal(spa
, "set", tx
,
7115 "%s=%s", nvpair_name(elem
), strval
);
7116 } else if (nvpair_type(elem
) == DATA_TYPE_UINT64
) {
7117 intval
= fnvpair_value_uint64(elem
);
7119 if (proptype
== PROP_TYPE_INDEX
) {
7121 VERIFY0(zpool_prop_index_to_string(
7122 prop
, intval
, &unused
));
7124 VERIFY0(zap_update(mos
,
7125 spa
->spa_pool_props_object
, propname
,
7126 8, 1, &intval
, tx
));
7127 spa_history_log_internal(spa
, "set", tx
,
7128 "%s=%lld", nvpair_name(elem
), intval
);
7130 ASSERT(0); /* not allowed */
7134 case ZPOOL_PROP_DELEGATION
:
7135 spa
->spa_delegation
= intval
;
7137 case ZPOOL_PROP_BOOTFS
:
7138 spa
->spa_bootfs
= intval
;
7140 case ZPOOL_PROP_FAILUREMODE
:
7141 spa
->spa_failmode
= intval
;
7143 case ZPOOL_PROP_AUTOEXPAND
:
7144 spa
->spa_autoexpand
= intval
;
7145 if (tx
->tx_txg
!= TXG_INITIAL
)
7146 spa_async_request(spa
,
7147 SPA_ASYNC_AUTOEXPAND
);
7149 case ZPOOL_PROP_MULTIHOST
:
7150 spa
->spa_multihost
= intval
;
7152 case ZPOOL_PROP_DEDUPDITTO
:
7153 spa
->spa_dedup_ditto
= intval
;
7162 mutex_exit(&spa
->spa_props_lock
);
7166 * Perform one-time upgrade on-disk changes. spa_version() does not
7167 * reflect the new version this txg, so there must be no changes this
7168 * txg to anything that the upgrade code depends on after it executes.
7169 * Therefore this must be called after dsl_pool_sync() does the sync
7173 spa_sync_upgrades(spa_t
*spa
, dmu_tx_t
*tx
)
7175 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
7177 ASSERT(spa
->spa_sync_pass
== 1);
7179 rrw_enter(&dp
->dp_config_rwlock
, RW_WRITER
, FTAG
);
7181 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_ORIGIN
&&
7182 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_ORIGIN
) {
7183 dsl_pool_create_origin(dp
, tx
);
7185 /* Keeping the origin open increases spa_minref */
7186 spa
->spa_minref
+= 3;
7189 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_NEXT_CLONES
&&
7190 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_NEXT_CLONES
) {
7191 dsl_pool_upgrade_clones(dp
, tx
);
7194 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_DIR_CLONES
&&
7195 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_DIR_CLONES
) {
7196 dsl_pool_upgrade_dir_clones(dp
, tx
);
7198 /* Keeping the freedir open increases spa_minref */
7199 spa
->spa_minref
+= 3;
7202 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_FEATURES
&&
7203 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
7204 spa_feature_create_zap_objects(spa
, tx
);
7208 * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable
7209 * when possibility to use lz4 compression for metadata was added
7210 * Old pools that have this feature enabled must be upgraded to have
7211 * this feature active
7213 if (spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
7214 boolean_t lz4_en
= spa_feature_is_enabled(spa
,
7215 SPA_FEATURE_LZ4_COMPRESS
);
7216 boolean_t lz4_ac
= spa_feature_is_active(spa
,
7217 SPA_FEATURE_LZ4_COMPRESS
);
7219 if (lz4_en
&& !lz4_ac
)
7220 spa_feature_incr(spa
, SPA_FEATURE_LZ4_COMPRESS
, tx
);
7224 * If we haven't written the salt, do so now. Note that the
7225 * feature may not be activated yet, but that's fine since
7226 * the presence of this ZAP entry is backwards compatible.
7228 if (zap_contains(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
7229 DMU_POOL_CHECKSUM_SALT
) == ENOENT
) {
7230 VERIFY0(zap_add(spa
->spa_meta_objset
,
7231 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CHECKSUM_SALT
, 1,
7232 sizeof (spa
->spa_cksum_salt
.zcs_bytes
),
7233 spa
->spa_cksum_salt
.zcs_bytes
, tx
));
7236 rrw_exit(&dp
->dp_config_rwlock
, FTAG
);
7240 vdev_indirect_state_sync_verify(vdev_t
*vd
)
7242 ASSERTV(vdev_indirect_mapping_t
*vim
= vd
->vdev_indirect_mapping
);
7243 ASSERTV(vdev_indirect_births_t
*vib
= vd
->vdev_indirect_births
);
7245 if (vd
->vdev_ops
== &vdev_indirect_ops
) {
7246 ASSERT(vim
!= NULL
);
7247 ASSERT(vib
!= NULL
);
7250 if (vdev_obsolete_sm_object(vd
) != 0) {
7251 ASSERT(vd
->vdev_obsolete_sm
!= NULL
);
7252 ASSERT(vd
->vdev_removing
||
7253 vd
->vdev_ops
== &vdev_indirect_ops
);
7254 ASSERT(vdev_indirect_mapping_num_entries(vim
) > 0);
7255 ASSERT(vdev_indirect_mapping_bytes_mapped(vim
) > 0);
7257 ASSERT3U(vdev_obsolete_sm_object(vd
), ==,
7258 space_map_object(vd
->vdev_obsolete_sm
));
7259 ASSERT3U(vdev_indirect_mapping_bytes_mapped(vim
), >=,
7260 space_map_allocated(vd
->vdev_obsolete_sm
));
7262 ASSERT(vd
->vdev_obsolete_segments
!= NULL
);
7265 * Since frees / remaps to an indirect vdev can only
7266 * happen in syncing context, the obsolete segments
7267 * tree must be empty when we start syncing.
7269 ASSERT0(range_tree_space(vd
->vdev_obsolete_segments
));
7273 * Sync the specified transaction group. New blocks may be dirtied as
7274 * part of the process, so we iterate until it converges.
7277 spa_sync(spa_t
*spa
, uint64_t txg
)
7279 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
7280 objset_t
*mos
= spa
->spa_meta_objset
;
7281 bplist_t
*free_bpl
= &spa
->spa_free_bplist
[txg
& TXG_MASK
];
7282 vdev_t
*rvd
= spa
->spa_root_vdev
;
7286 uint32_t max_queue_depth
= zfs_vdev_async_write_max_active
*
7287 zfs_vdev_queue_depth_pct
/ 100;
7289 VERIFY(spa_writeable(spa
));
7292 * Wait for i/os issued in open context that need to complete
7293 * before this txg syncs.
7295 VERIFY0(zio_wait(spa
->spa_txg_zio
[txg
& TXG_MASK
]));
7296 spa
->spa_txg_zio
[txg
& TXG_MASK
] = zio_root(spa
, NULL
, NULL
, 0);
7299 * Lock out configuration changes.
7301 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
7303 spa
->spa_syncing_txg
= txg
;
7304 spa
->spa_sync_pass
= 0;
7306 mutex_enter(&spa
->spa_alloc_lock
);
7307 VERIFY0(avl_numnodes(&spa
->spa_alloc_tree
));
7308 mutex_exit(&spa
->spa_alloc_lock
);
7311 * If there are any pending vdev state changes, convert them
7312 * into config changes that go out with this transaction group.
7314 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
7315 while (list_head(&spa
->spa_state_dirty_list
) != NULL
) {
7317 * We need the write lock here because, for aux vdevs,
7318 * calling vdev_config_dirty() modifies sav_config.
7319 * This is ugly and will become unnecessary when we
7320 * eliminate the aux vdev wart by integrating all vdevs
7321 * into the root vdev tree.
7323 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7324 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_WRITER
);
7325 while ((vd
= list_head(&spa
->spa_state_dirty_list
)) != NULL
) {
7326 vdev_state_clean(vd
);
7327 vdev_config_dirty(vd
);
7329 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7330 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
7332 spa_config_exit(spa
, SCL_STATE
, FTAG
);
7334 tx
= dmu_tx_create_assigned(dp
, txg
);
7336 spa
->spa_sync_starttime
= gethrtime();
7337 taskq_cancel_id(system_delay_taskq
, spa
->spa_deadman_tqid
);
7338 spa
->spa_deadman_tqid
= taskq_dispatch_delay(system_delay_taskq
,
7339 spa_deadman
, spa
, TQ_SLEEP
, ddi_get_lbolt() +
7340 NSEC_TO_TICK(spa
->spa_deadman_synctime
));
7343 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
7344 * set spa_deflate if we have no raid-z vdevs.
7346 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_RAIDZ_DEFLATE
&&
7347 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
7350 for (i
= 0; i
< rvd
->vdev_children
; i
++) {
7351 vd
= rvd
->vdev_child
[i
];
7352 if (vd
->vdev_deflate_ratio
!= SPA_MINBLOCKSIZE
)
7355 if (i
== rvd
->vdev_children
) {
7356 spa
->spa_deflate
= TRUE
;
7357 VERIFY(0 == zap_add(spa
->spa_meta_objset
,
7358 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
7359 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
));
7364 * Set the top-level vdev's max queue depth. Evaluate each
7365 * top-level's async write queue depth in case it changed.
7366 * The max queue depth will not change in the middle of syncing
7369 uint64_t queue_depth_total
= 0;
7370 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
7371 vdev_t
*tvd
= rvd
->vdev_child
[c
];
7372 metaslab_group_t
*mg
= tvd
->vdev_mg
;
7374 if (mg
== NULL
|| mg
->mg_class
!= spa_normal_class(spa
) ||
7375 !metaslab_group_initialized(mg
))
7379 * It is safe to do a lock-free check here because only async
7380 * allocations look at mg_max_alloc_queue_depth, and async
7381 * allocations all happen from spa_sync().
7383 ASSERT0(refcount_count(&mg
->mg_alloc_queue_depth
));
7384 mg
->mg_max_alloc_queue_depth
= max_queue_depth
;
7385 queue_depth_total
+= mg
->mg_max_alloc_queue_depth
;
7387 metaslab_class_t
*mc
= spa_normal_class(spa
);
7388 ASSERT0(refcount_count(&mc
->mc_alloc_slots
));
7389 mc
->mc_alloc_max_slots
= queue_depth_total
;
7390 mc
->mc_alloc_throttle_enabled
= zio_dva_throttle_enabled
;
7392 ASSERT3U(mc
->mc_alloc_max_slots
, <=,
7393 max_queue_depth
* rvd
->vdev_children
);
7395 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
7396 vdev_t
*vd
= rvd
->vdev_child
[c
];
7397 vdev_indirect_state_sync_verify(vd
);
7399 if (vdev_indirect_should_condense(vd
)) {
7400 spa_condense_indirect_start_sync(vd
, tx
);
7406 * Iterate to convergence.
7409 int pass
= ++spa
->spa_sync_pass
;
7411 spa_sync_config_object(spa
, tx
);
7412 spa_sync_aux_dev(spa
, &spa
->spa_spares
, tx
,
7413 ZPOOL_CONFIG_SPARES
, DMU_POOL_SPARES
);
7414 spa_sync_aux_dev(spa
, &spa
->spa_l2cache
, tx
,
7415 ZPOOL_CONFIG_L2CACHE
, DMU_POOL_L2CACHE
);
7416 spa_errlog_sync(spa
, txg
);
7417 dsl_pool_sync(dp
, txg
);
7419 if (pass
< zfs_sync_pass_deferred_free
) {
7420 spa_sync_frees(spa
, free_bpl
, tx
);
7423 * We can not defer frees in pass 1, because
7424 * we sync the deferred frees later in pass 1.
7426 ASSERT3U(pass
, >, 1);
7427 bplist_iterate(free_bpl
, bpobj_enqueue_cb
,
7428 &spa
->spa_deferred_bpobj
, tx
);
7432 dsl_scan_sync(dp
, tx
);
7434 if (spa
->spa_vdev_removal
!= NULL
)
7437 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, txg
))
7442 spa_sync_upgrades(spa
, tx
);
7444 spa
->spa_uberblock
.ub_rootbp
.blk_birth
);
7446 * Note: We need to check if the MOS is dirty
7447 * because we could have marked the MOS dirty
7448 * without updating the uberblock (e.g. if we
7449 * have sync tasks but no dirty user data). We
7450 * need to check the uberblock's rootbp because
7451 * it is updated if we have synced out dirty
7452 * data (though in this case the MOS will most
7453 * likely also be dirty due to second order
7454 * effects, we don't want to rely on that here).
7456 if (spa
->spa_uberblock
.ub_rootbp
.blk_birth
< txg
&&
7457 !dmu_objset_is_dirty(mos
, txg
)) {
7459 * Nothing changed on the first pass,
7460 * therefore this TXG is a no-op. Avoid
7461 * syncing deferred frees, so that we
7462 * can keep this TXG as a no-op.
7464 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
,
7466 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
7467 ASSERT(txg_list_empty(&dp
->dp_sync_tasks
, txg
));
7470 spa_sync_deferred_frees(spa
, tx
);
7473 } while (dmu_objset_is_dirty(mos
, txg
));
7476 if (!list_is_empty(&spa
->spa_config_dirty_list
)) {
7478 * Make sure that the number of ZAPs for all the vdevs matches
7479 * the number of ZAPs in the per-vdev ZAP list. This only gets
7480 * called if the config is dirty; otherwise there may be
7481 * outstanding AVZ operations that weren't completed in
7482 * spa_sync_config_object.
7484 uint64_t all_vdev_zap_entry_count
;
7485 ASSERT0(zap_count(spa
->spa_meta_objset
,
7486 spa
->spa_all_vdev_zaps
, &all_vdev_zap_entry_count
));
7487 ASSERT3U(vdev_count_verify_zaps(spa
->spa_root_vdev
), ==,
7488 all_vdev_zap_entry_count
);
7492 if (spa
->spa_vdev_removal
!= NULL
) {
7493 ASSERT0(spa
->spa_vdev_removal
->svr_bytes_done
[txg
& TXG_MASK
]);
7497 * Rewrite the vdev configuration (which includes the uberblock)
7498 * to commit the transaction group.
7500 * If there are no dirty vdevs, we sync the uberblock to a few
7501 * random top-level vdevs that are known to be visible in the
7502 * config cache (see spa_vdev_add() for a complete description).
7503 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
7507 * We hold SCL_STATE to prevent vdev open/close/etc.
7508 * while we're attempting to write the vdev labels.
7510 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
7512 if (list_is_empty(&spa
->spa_config_dirty_list
)) {
7513 vdev_t
*svd
[SPA_SYNC_MIN_VDEVS
];
7515 int children
= rvd
->vdev_children
;
7516 int c0
= spa_get_random(children
);
7518 for (int c
= 0; c
< children
; c
++) {
7519 vd
= rvd
->vdev_child
[(c0
+ c
) % children
];
7520 if (vd
->vdev_ms_array
== 0 || vd
->vdev_islog
||
7521 !vdev_is_concrete(vd
))
7523 svd
[svdcount
++] = vd
;
7524 if (svdcount
== SPA_SYNC_MIN_VDEVS
)
7527 error
= vdev_config_sync(svd
, svdcount
, txg
);
7529 error
= vdev_config_sync(rvd
->vdev_child
,
7530 rvd
->vdev_children
, txg
);
7534 spa
->spa_last_synced_guid
= rvd
->vdev_guid
;
7536 spa_config_exit(spa
, SCL_STATE
, FTAG
);
7540 zio_suspend(spa
, NULL
, ZIO_SUSPEND_IOERR
);
7541 zio_resume_wait(spa
);
7545 taskq_cancel_id(system_delay_taskq
, spa
->spa_deadman_tqid
);
7546 spa
->spa_deadman_tqid
= 0;
7549 * Clear the dirty config list.
7551 while ((vd
= list_head(&spa
->spa_config_dirty_list
)) != NULL
)
7552 vdev_config_clean(vd
);
7555 * Now that the new config has synced transactionally,
7556 * let it become visible to the config cache.
7558 if (spa
->spa_config_syncing
!= NULL
) {
7559 spa_config_set(spa
, spa
->spa_config_syncing
);
7560 spa
->spa_config_txg
= txg
;
7561 spa
->spa_config_syncing
= NULL
;
7564 dsl_pool_sync_done(dp
, txg
);
7566 mutex_enter(&spa
->spa_alloc_lock
);
7567 VERIFY0(avl_numnodes(&spa
->spa_alloc_tree
));
7568 mutex_exit(&spa
->spa_alloc_lock
);
7571 * Update usable space statistics.
7573 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, TXG_CLEAN(txg
))))
7574 vdev_sync_done(vd
, txg
);
7576 spa_update_dspace(spa
);
7579 * It had better be the case that we didn't dirty anything
7580 * since vdev_config_sync().
7582 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
, txg
));
7583 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
7584 ASSERT(txg_list_empty(&spa
->spa_vdev_txg_list
, txg
));
7586 spa
->spa_sync_pass
= 0;
7589 * Update the last synced uberblock here. We want to do this at
7590 * the end of spa_sync() so that consumers of spa_last_synced_txg()
7591 * will be guaranteed that all the processing associated with
7592 * that txg has been completed.
7594 spa
->spa_ubsync
= spa
->spa_uberblock
;
7595 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
7597 spa_handle_ignored_writes(spa
);
7600 * If any async tasks have been requested, kick them off.
7602 spa_async_dispatch(spa
);
7606 * Sync all pools. We don't want to hold the namespace lock across these
7607 * operations, so we take a reference on the spa_t and drop the lock during the
7611 spa_sync_allpools(void)
7614 mutex_enter(&spa_namespace_lock
);
7615 while ((spa
= spa_next(spa
)) != NULL
) {
7616 if (spa_state(spa
) != POOL_STATE_ACTIVE
||
7617 !spa_writeable(spa
) || spa_suspended(spa
))
7619 spa_open_ref(spa
, FTAG
);
7620 mutex_exit(&spa_namespace_lock
);
7621 txg_wait_synced(spa_get_dsl(spa
), 0);
7622 mutex_enter(&spa_namespace_lock
);
7623 spa_close(spa
, FTAG
);
7625 mutex_exit(&spa_namespace_lock
);
7629 * ==========================================================================
7630 * Miscellaneous routines
7631 * ==========================================================================
7635 * Remove all pools in the system.
7643 * Remove all cached state. All pools should be closed now,
7644 * so every spa in the AVL tree should be unreferenced.
7646 mutex_enter(&spa_namespace_lock
);
7647 while ((spa
= spa_next(NULL
)) != NULL
) {
7649 * Stop async tasks. The async thread may need to detach
7650 * a device that's been replaced, which requires grabbing
7651 * spa_namespace_lock, so we must drop it here.
7653 spa_open_ref(spa
, FTAG
);
7654 mutex_exit(&spa_namespace_lock
);
7655 spa_async_suspend(spa
);
7656 mutex_enter(&spa_namespace_lock
);
7657 spa_close(spa
, FTAG
);
7659 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
7661 spa_deactivate(spa
);
7665 mutex_exit(&spa_namespace_lock
);
7669 spa_lookup_by_guid(spa_t
*spa
, uint64_t guid
, boolean_t aux
)
7674 if ((vd
= vdev_lookup_by_guid(spa
->spa_root_vdev
, guid
)) != NULL
)
7678 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
7679 vd
= spa
->spa_l2cache
.sav_vdevs
[i
];
7680 if (vd
->vdev_guid
== guid
)
7684 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
7685 vd
= spa
->spa_spares
.sav_vdevs
[i
];
7686 if (vd
->vdev_guid
== guid
)
7695 spa_upgrade(spa_t
*spa
, uint64_t version
)
7697 ASSERT(spa_writeable(spa
));
7699 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
7702 * This should only be called for a non-faulted pool, and since a
7703 * future version would result in an unopenable pool, this shouldn't be
7706 ASSERT(SPA_VERSION_IS_SUPPORTED(spa
->spa_uberblock
.ub_version
));
7707 ASSERT3U(version
, >=, spa
->spa_uberblock
.ub_version
);
7709 spa
->spa_uberblock
.ub_version
= version
;
7710 vdev_config_dirty(spa
->spa_root_vdev
);
7712 spa_config_exit(spa
, SCL_ALL
, FTAG
);
7714 txg_wait_synced(spa_get_dsl(spa
), 0);
7718 spa_has_spare(spa_t
*spa
, uint64_t guid
)
7722 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
7724 for (i
= 0; i
< sav
->sav_count
; i
++)
7725 if (sav
->sav_vdevs
[i
]->vdev_guid
== guid
)
7728 for (i
= 0; i
< sav
->sav_npending
; i
++) {
7729 if (nvlist_lookup_uint64(sav
->sav_pending
[i
], ZPOOL_CONFIG_GUID
,
7730 &spareguid
) == 0 && spareguid
== guid
)
7738 * Check if a pool has an active shared spare device.
7739 * Note: reference count of an active spare is 2, as a spare and as a replace
7742 spa_has_active_shared_spare(spa_t
*spa
)
7746 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
7748 for (i
= 0; i
< sav
->sav_count
; i
++) {
7749 if (spa_spare_exists(sav
->sav_vdevs
[i
]->vdev_guid
, &pool
,
7750 &refcnt
) && pool
!= 0ULL && pool
== spa_guid(spa
) &&
7759 spa_event_create(spa_t
*spa
, vdev_t
*vd
, nvlist_t
*hist_nvl
, const char *name
)
7761 sysevent_t
*ev
= NULL
;
7765 resource
= zfs_event_create(spa
, vd
, FM_SYSEVENT_CLASS
, name
, hist_nvl
);
7767 ev
= kmem_alloc(sizeof (sysevent_t
), KM_SLEEP
);
7768 ev
->resource
= resource
;
7775 spa_event_post(sysevent_t
*ev
)
7779 zfs_zevent_post(ev
->resource
, NULL
, zfs_zevent_post_cb
);
7780 kmem_free(ev
, sizeof (*ev
));
7786 * Post a zevent corresponding to the given sysevent. The 'name' must be one
7787 * of the event definitions in sys/sysevent/eventdefs.h. The payload will be
7788 * filled in from the spa and (optionally) the vdev. This doesn't do anything
7789 * in the userland libzpool, as we don't want consumers to misinterpret ztest
7790 * or zdb as real changes.
7793 spa_event_notify(spa_t
*spa
, vdev_t
*vd
, nvlist_t
*hist_nvl
, const char *name
)
7795 spa_event_post(spa_event_create(spa
, vd
, hist_nvl
, name
));
7798 #if defined(_KERNEL) && defined(HAVE_SPL)
7799 /* state manipulation functions */
7800 EXPORT_SYMBOL(spa_open
);
7801 EXPORT_SYMBOL(spa_open_rewind
);
7802 EXPORT_SYMBOL(spa_get_stats
);
7803 EXPORT_SYMBOL(spa_create
);
7804 EXPORT_SYMBOL(spa_import
);
7805 EXPORT_SYMBOL(spa_tryimport
);
7806 EXPORT_SYMBOL(spa_destroy
);
7807 EXPORT_SYMBOL(spa_export
);
7808 EXPORT_SYMBOL(spa_reset
);
7809 EXPORT_SYMBOL(spa_async_request
);
7810 EXPORT_SYMBOL(spa_async_suspend
);
7811 EXPORT_SYMBOL(spa_async_resume
);
7812 EXPORT_SYMBOL(spa_inject_addref
);
7813 EXPORT_SYMBOL(spa_inject_delref
);
7814 EXPORT_SYMBOL(spa_scan_stat_init
);
7815 EXPORT_SYMBOL(spa_scan_get_stats
);
7817 /* device maniion */
7818 EXPORT_SYMBOL(spa_vdev_add
);
7819 EXPORT_SYMBOL(spa_vdev_attach
);
7820 EXPORT_SYMBOL(spa_vdev_detach
);
7821 EXPORT_SYMBOL(spa_vdev_setpath
);
7822 EXPORT_SYMBOL(spa_vdev_setfru
);
7823 EXPORT_SYMBOL(spa_vdev_split_mirror
);
7825 /* spare statech is global across all pools) */
7826 EXPORT_SYMBOL(spa_spare_add
);
7827 EXPORT_SYMBOL(spa_spare_remove
);
7828 EXPORT_SYMBOL(spa_spare_exists
);
7829 EXPORT_SYMBOL(spa_spare_activate
);
7831 /* L2ARC statech is global across all pools) */
7832 EXPORT_SYMBOL(spa_l2cache_add
);
7833 EXPORT_SYMBOL(spa_l2cache_remove
);
7834 EXPORT_SYMBOL(spa_l2cache_exists
);
7835 EXPORT_SYMBOL(spa_l2cache_activate
);
7836 EXPORT_SYMBOL(spa_l2cache_drop
);
7839 EXPORT_SYMBOL(spa_scan
);
7840 EXPORT_SYMBOL(spa_scan_stop
);
7843 EXPORT_SYMBOL(spa_sync
); /* only for DMU use */
7844 EXPORT_SYMBOL(spa_sync_allpools
);
7847 EXPORT_SYMBOL(spa_prop_set
);
7848 EXPORT_SYMBOL(spa_prop_get
);
7849 EXPORT_SYMBOL(spa_prop_clear_bootfs
);
7851 /* asynchronous event notification */
7852 EXPORT_SYMBOL(spa_event_notify
);
7855 #if defined(_KERNEL) && defined(HAVE_SPL)
7856 module_param(spa_load_verify_maxinflight
, int, 0644);
7857 MODULE_PARM_DESC(spa_load_verify_maxinflight
,
7858 "Max concurrent traversal I/Os while verifying pool during import -X");
7860 module_param(spa_load_verify_metadata
, int, 0644);
7861 MODULE_PARM_DESC(spa_load_verify_metadata
,
7862 "Set to traverse metadata on pool import");
7864 module_param(spa_load_verify_data
, int, 0644);
7865 MODULE_PARM_DESC(spa_load_verify_data
,
7866 "Set to traverse data on pool import");
7868 module_param(spa_load_print_vdev_tree
, int, 0644);
7869 MODULE_PARM_DESC(spa_load_print_vdev_tree
,
7870 "Print vdev tree to zfs_dbgmsg during pool import");
7873 module_param(zio_taskq_batch_pct
, uint
, 0444);
7874 MODULE_PARM_DESC(zio_taskq_batch_pct
,
7875 "Percentage of CPUs to run an IO worker thread");
7878 module_param(zfs_max_missing_tvds
, ulong
, 0644);
7879 MODULE_PARM_DESC(zfs_max_missing_tvds
,
7880 "Allow importing pool with up to this number of missing top-level vdevs"
7881 " (in read-only mode)");