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_disk.h>
56 #include <sys/metaslab.h>
57 #include <sys/metaslab_impl.h>
59 #include <sys/uberblock_impl.h>
62 #include <sys/dmu_traverse.h>
63 #include <sys/dmu_objset.h>
64 #include <sys/unique.h>
65 #include <sys/dsl_pool.h>
66 #include <sys/dsl_dataset.h>
67 #include <sys/dsl_dir.h>
68 #include <sys/dsl_prop.h>
69 #include <sys/dsl_synctask.h>
70 #include <sys/fs/zfs.h>
72 #include <sys/callb.h>
73 #include <sys/systeminfo.h>
74 #include <sys/spa_boot.h>
75 #include <sys/zfs_ioctl.h>
76 #include <sys/dsl_scan.h>
77 #include <sys/zfeature.h>
78 #include <sys/dsl_destroy.h>
82 #include <sys/fm/protocol.h>
83 #include <sys/fm/util.h>
84 #include <sys/bootprops.h>
85 #include <sys/callb.h>
86 #include <sys/cpupart.h>
88 #include <sys/sysdc.h>
93 #include "zfs_comutil.h"
96 * The interval, in seconds, at which failed configuration cache file writes
99 static int zfs_ccw_retry_interval
= 300;
101 typedef enum zti_modes
{
102 ZTI_MODE_FIXED
, /* value is # of threads (min 1) */
103 ZTI_MODE_BATCH
, /* cpu-intensive; value is ignored */
104 ZTI_MODE_NULL
, /* don't create a taskq */
108 #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) }
109 #define ZTI_PCT(n) { ZTI_MODE_ONLINE_PERCENT, (n), 1 }
110 #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 }
111 #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 }
113 #define ZTI_N(n) ZTI_P(n, 1)
114 #define ZTI_ONE ZTI_N(1)
116 typedef struct zio_taskq_info
{
117 zti_modes_t zti_mode
;
122 static const char *const zio_taskq_types
[ZIO_TASKQ_TYPES
] = {
123 "iss", "iss_h", "int", "int_h"
127 * This table defines the taskq settings for each ZFS I/O type. When
128 * initializing a pool, we use this table to create an appropriately sized
129 * taskq. Some operations are low volume and therefore have a small, static
130 * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
131 * macros. Other operations process a large amount of data; the ZTI_BATCH
132 * macro causes us to create a taskq oriented for throughput. Some operations
133 * are so high frequency and short-lived that the taskq itself can become a a
134 * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
135 * additional degree of parallelism specified by the number of threads per-
136 * taskq and the number of taskqs; when dispatching an event in this case, the
137 * particular taskq is chosen at random.
139 * The different taskq priorities are to handle the different contexts (issue
140 * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that
141 * need to be handled with minimum delay.
143 const zio_taskq_info_t zio_taskqs
[ZIO_TYPES
][ZIO_TASKQ_TYPES
] = {
144 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
145 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* NULL */
146 { ZTI_N(8), ZTI_NULL
, ZTI_P(12, 8), ZTI_NULL
}, /* READ */
147 { ZTI_BATCH
, ZTI_N(5), ZTI_P(12, 8), ZTI_N(5) }, /* WRITE */
148 { ZTI_P(12, 8), ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* FREE */
149 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* CLAIM */
150 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* IOCTL */
153 static sysevent_t
*spa_event_create(spa_t
*spa
, vdev_t
*vd
, nvlist_t
*hist_nvl
,
155 static void spa_event_post(sysevent_t
*ev
);
156 static void spa_sync_version(void *arg
, dmu_tx_t
*tx
);
157 static void spa_sync_props(void *arg
, dmu_tx_t
*tx
);
158 static boolean_t
spa_has_active_shared_spare(spa_t
*spa
);
159 static inline int spa_load_impl(spa_t
*spa
, uint64_t, nvlist_t
*config
,
160 spa_load_state_t state
, spa_import_type_t type
, boolean_t mosconfig
,
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 * This (illegal) pool name is used when temporarily importing a spa_t in order
173 * to get the vdev stats associated with the imported devices.
175 #define TRYIMPORT_NAME "$import"
178 * ==========================================================================
179 * SPA properties routines
180 * ==========================================================================
184 * Add a (source=src, propname=propval) list to an nvlist.
187 spa_prop_add_list(nvlist_t
*nvl
, zpool_prop_t prop
, char *strval
,
188 uint64_t intval
, zprop_source_t src
)
190 const char *propname
= zpool_prop_to_name(prop
);
193 VERIFY(nvlist_alloc(&propval
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
194 VERIFY(nvlist_add_uint64(propval
, ZPROP_SOURCE
, src
) == 0);
197 VERIFY(nvlist_add_string(propval
, ZPROP_VALUE
, strval
) == 0);
199 VERIFY(nvlist_add_uint64(propval
, ZPROP_VALUE
, intval
) == 0);
201 VERIFY(nvlist_add_nvlist(nvl
, propname
, propval
) == 0);
202 nvlist_free(propval
);
206 * Get property values from the spa configuration.
209 spa_prop_get_config(spa_t
*spa
, nvlist_t
**nvp
)
211 vdev_t
*rvd
= spa
->spa_root_vdev
;
212 dsl_pool_t
*pool
= spa
->spa_dsl_pool
;
213 uint64_t size
, alloc
, cap
, version
;
214 const zprop_source_t src
= ZPROP_SRC_NONE
;
215 spa_config_dirent_t
*dp
;
216 metaslab_class_t
*mc
= spa_normal_class(spa
);
218 ASSERT(MUTEX_HELD(&spa
->spa_props_lock
));
221 alloc
= metaslab_class_get_alloc(spa_normal_class(spa
));
222 size
= metaslab_class_get_space(spa_normal_class(spa
));
223 spa_prop_add_list(*nvp
, ZPOOL_PROP_NAME
, spa_name(spa
), 0, src
);
224 spa_prop_add_list(*nvp
, ZPOOL_PROP_SIZE
, NULL
, size
, src
);
225 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALLOCATED
, NULL
, alloc
, src
);
226 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREE
, NULL
,
229 spa_prop_add_list(*nvp
, ZPOOL_PROP_FRAGMENTATION
, NULL
,
230 metaslab_class_fragmentation(mc
), src
);
231 spa_prop_add_list(*nvp
, ZPOOL_PROP_EXPANDSZ
, NULL
,
232 metaslab_class_expandable_space(mc
), src
);
233 spa_prop_add_list(*nvp
, ZPOOL_PROP_READONLY
, NULL
,
234 (spa_mode(spa
) == FREAD
), src
);
236 cap
= (size
== 0) ? 0 : (alloc
* 100 / size
);
237 spa_prop_add_list(*nvp
, ZPOOL_PROP_CAPACITY
, NULL
, cap
, src
);
239 spa_prop_add_list(*nvp
, ZPOOL_PROP_DEDUPRATIO
, NULL
,
240 ddt_get_pool_dedup_ratio(spa
), src
);
242 spa_prop_add_list(*nvp
, ZPOOL_PROP_HEALTH
, NULL
,
243 rvd
->vdev_state
, src
);
245 version
= spa_version(spa
);
246 if (version
== zpool_prop_default_numeric(ZPOOL_PROP_VERSION
)) {
247 spa_prop_add_list(*nvp
, ZPOOL_PROP_VERSION
, NULL
,
248 version
, ZPROP_SRC_DEFAULT
);
250 spa_prop_add_list(*nvp
, ZPOOL_PROP_VERSION
, NULL
,
251 version
, ZPROP_SRC_LOCAL
);
257 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
258 * when opening pools before this version freedir will be NULL.
260 if (pool
->dp_free_dir
!= NULL
) {
261 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
, NULL
,
262 dsl_dir_phys(pool
->dp_free_dir
)->dd_used_bytes
,
265 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
,
269 if (pool
->dp_leak_dir
!= NULL
) {
270 spa_prop_add_list(*nvp
, ZPOOL_PROP_LEAKED
, NULL
,
271 dsl_dir_phys(pool
->dp_leak_dir
)->dd_used_bytes
,
274 spa_prop_add_list(*nvp
, ZPOOL_PROP_LEAKED
,
279 spa_prop_add_list(*nvp
, ZPOOL_PROP_GUID
, NULL
, spa_guid(spa
), src
);
281 if (spa
->spa_comment
!= NULL
) {
282 spa_prop_add_list(*nvp
, ZPOOL_PROP_COMMENT
, spa
->spa_comment
,
286 if (spa
->spa_root
!= NULL
)
287 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALTROOT
, spa
->spa_root
,
290 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_BLOCKS
)) {
291 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXBLOCKSIZE
, NULL
,
292 MIN(zfs_max_recordsize
, SPA_MAXBLOCKSIZE
), ZPROP_SRC_NONE
);
294 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXBLOCKSIZE
, NULL
,
295 SPA_OLD_MAXBLOCKSIZE
, ZPROP_SRC_NONE
);
298 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_DNODE
)) {
299 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXDNODESIZE
, NULL
,
300 DNODE_MAX_SIZE
, ZPROP_SRC_NONE
);
302 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXDNODESIZE
, NULL
,
303 DNODE_MIN_SIZE
, ZPROP_SRC_NONE
);
306 if ((dp
= list_head(&spa
->spa_config_list
)) != NULL
) {
307 if (dp
->scd_path
== NULL
) {
308 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
309 "none", 0, ZPROP_SRC_LOCAL
);
310 } else if (strcmp(dp
->scd_path
, spa_config_path
) != 0) {
311 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
312 dp
->scd_path
, 0, ZPROP_SRC_LOCAL
);
318 * Get zpool property values.
321 spa_prop_get(spa_t
*spa
, nvlist_t
**nvp
)
323 objset_t
*mos
= spa
->spa_meta_objset
;
328 err
= nvlist_alloc(nvp
, NV_UNIQUE_NAME
, KM_SLEEP
);
332 mutex_enter(&spa
->spa_props_lock
);
335 * Get properties from the spa config.
337 spa_prop_get_config(spa
, nvp
);
339 /* If no pool property object, no more prop to get. */
340 if (mos
== NULL
|| spa
->spa_pool_props_object
== 0) {
341 mutex_exit(&spa
->spa_props_lock
);
346 * Get properties from the MOS pool property object.
348 for (zap_cursor_init(&zc
, mos
, spa
->spa_pool_props_object
);
349 (err
= zap_cursor_retrieve(&zc
, &za
)) == 0;
350 zap_cursor_advance(&zc
)) {
353 zprop_source_t src
= ZPROP_SRC_DEFAULT
;
356 if ((prop
= zpool_name_to_prop(za
.za_name
)) == ZPOOL_PROP_INVAL
)
359 switch (za
.za_integer_length
) {
361 /* integer property */
362 if (za
.za_first_integer
!=
363 zpool_prop_default_numeric(prop
))
364 src
= ZPROP_SRC_LOCAL
;
366 if (prop
== ZPOOL_PROP_BOOTFS
) {
368 dsl_dataset_t
*ds
= NULL
;
370 dp
= spa_get_dsl(spa
);
371 dsl_pool_config_enter(dp
, FTAG
);
372 if ((err
= dsl_dataset_hold_obj(dp
,
373 za
.za_first_integer
, FTAG
, &ds
))) {
374 dsl_pool_config_exit(dp
, FTAG
);
378 strval
= kmem_alloc(ZFS_MAX_DATASET_NAME_LEN
,
380 dsl_dataset_name(ds
, strval
);
381 dsl_dataset_rele(ds
, FTAG
);
382 dsl_pool_config_exit(dp
, FTAG
);
385 intval
= za
.za_first_integer
;
388 spa_prop_add_list(*nvp
, prop
, strval
, intval
, src
);
391 kmem_free(strval
, ZFS_MAX_DATASET_NAME_LEN
);
396 /* string property */
397 strval
= kmem_alloc(za
.za_num_integers
, KM_SLEEP
);
398 err
= zap_lookup(mos
, spa
->spa_pool_props_object
,
399 za
.za_name
, 1, za
.za_num_integers
, strval
);
401 kmem_free(strval
, za
.za_num_integers
);
404 spa_prop_add_list(*nvp
, prop
, strval
, 0, src
);
405 kmem_free(strval
, za
.za_num_integers
);
412 zap_cursor_fini(&zc
);
413 mutex_exit(&spa
->spa_props_lock
);
415 if (err
&& err
!= ENOENT
) {
425 * Validate the given pool properties nvlist and modify the list
426 * for the property values to be set.
429 spa_prop_validate(spa_t
*spa
, nvlist_t
*props
)
432 int error
= 0, reset_bootfs
= 0;
434 boolean_t has_feature
= B_FALSE
;
437 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
439 char *strval
, *slash
, *check
, *fname
;
440 const char *propname
= nvpair_name(elem
);
441 zpool_prop_t prop
= zpool_name_to_prop(propname
);
444 case ZPOOL_PROP_INVAL
:
445 if (!zpool_prop_feature(propname
)) {
446 error
= SET_ERROR(EINVAL
);
451 * Sanitize the input.
453 if (nvpair_type(elem
) != DATA_TYPE_UINT64
) {
454 error
= SET_ERROR(EINVAL
);
458 if (nvpair_value_uint64(elem
, &intval
) != 0) {
459 error
= SET_ERROR(EINVAL
);
464 error
= SET_ERROR(EINVAL
);
468 fname
= strchr(propname
, '@') + 1;
469 if (zfeature_lookup_name(fname
, NULL
) != 0) {
470 error
= SET_ERROR(EINVAL
);
474 has_feature
= B_TRUE
;
477 case ZPOOL_PROP_VERSION
:
478 error
= nvpair_value_uint64(elem
, &intval
);
480 (intval
< spa_version(spa
) ||
481 intval
> SPA_VERSION_BEFORE_FEATURES
||
483 error
= SET_ERROR(EINVAL
);
486 case ZPOOL_PROP_DELEGATION
:
487 case ZPOOL_PROP_AUTOREPLACE
:
488 case ZPOOL_PROP_LISTSNAPS
:
489 case ZPOOL_PROP_AUTOEXPAND
:
490 error
= nvpair_value_uint64(elem
, &intval
);
491 if (!error
&& intval
> 1)
492 error
= SET_ERROR(EINVAL
);
495 case ZPOOL_PROP_MULTIHOST
:
496 error
= nvpair_value_uint64(elem
, &intval
);
497 if (!error
&& intval
> 1)
498 error
= SET_ERROR(EINVAL
);
500 if (!error
&& !spa_get_hostid())
501 error
= SET_ERROR(ENOTSUP
);
505 case ZPOOL_PROP_BOOTFS
:
507 * If the pool version is less than SPA_VERSION_BOOTFS,
508 * or the pool is still being created (version == 0),
509 * the bootfs property cannot be set.
511 if (spa_version(spa
) < SPA_VERSION_BOOTFS
) {
512 error
= SET_ERROR(ENOTSUP
);
517 * Make sure the vdev config is bootable
519 if (!vdev_is_bootable(spa
->spa_root_vdev
)) {
520 error
= SET_ERROR(ENOTSUP
);
526 error
= nvpair_value_string(elem
, &strval
);
532 if (strval
== NULL
|| strval
[0] == '\0') {
533 objnum
= zpool_prop_default_numeric(
538 error
= dmu_objset_hold(strval
, FTAG
, &os
);
543 * Must be ZPL, and its property settings
544 * must be supported by GRUB (compression
545 * is not gzip, and large blocks or large
546 * dnodes are not used).
549 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
550 error
= SET_ERROR(ENOTSUP
);
552 dsl_prop_get_int_ds(dmu_objset_ds(os
),
553 zfs_prop_to_name(ZFS_PROP_COMPRESSION
),
555 !BOOTFS_COMPRESS_VALID(propval
)) {
556 error
= SET_ERROR(ENOTSUP
);
558 dsl_prop_get_int_ds(dmu_objset_ds(os
),
559 zfs_prop_to_name(ZFS_PROP_DNODESIZE
),
561 propval
!= ZFS_DNSIZE_LEGACY
) {
562 error
= SET_ERROR(ENOTSUP
);
564 objnum
= dmu_objset_id(os
);
566 dmu_objset_rele(os
, FTAG
);
570 case ZPOOL_PROP_FAILUREMODE
:
571 error
= nvpair_value_uint64(elem
, &intval
);
572 if (!error
&& intval
> ZIO_FAILURE_MODE_PANIC
)
573 error
= SET_ERROR(EINVAL
);
576 * This is a special case which only occurs when
577 * the pool has completely failed. This allows
578 * the user to change the in-core failmode property
579 * without syncing it out to disk (I/Os might
580 * currently be blocked). We do this by returning
581 * EIO to the caller (spa_prop_set) to trick it
582 * into thinking we encountered a property validation
585 if (!error
&& spa_suspended(spa
)) {
586 spa
->spa_failmode
= intval
;
587 error
= SET_ERROR(EIO
);
591 case ZPOOL_PROP_CACHEFILE
:
592 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
595 if (strval
[0] == '\0')
598 if (strcmp(strval
, "none") == 0)
601 if (strval
[0] != '/') {
602 error
= SET_ERROR(EINVAL
);
606 slash
= strrchr(strval
, '/');
607 ASSERT(slash
!= NULL
);
609 if (slash
[1] == '\0' || strcmp(slash
, "/.") == 0 ||
610 strcmp(slash
, "/..") == 0)
611 error
= SET_ERROR(EINVAL
);
614 case ZPOOL_PROP_COMMENT
:
615 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
617 for (check
= strval
; *check
!= '\0'; check
++) {
618 if (!isprint(*check
)) {
619 error
= SET_ERROR(EINVAL
);
623 if (strlen(strval
) > ZPROP_MAX_COMMENT
)
624 error
= SET_ERROR(E2BIG
);
627 case ZPOOL_PROP_DEDUPDITTO
:
628 if (spa_version(spa
) < SPA_VERSION_DEDUP
)
629 error
= SET_ERROR(ENOTSUP
);
631 error
= nvpair_value_uint64(elem
, &intval
);
633 intval
!= 0 && intval
< ZIO_DEDUPDITTO_MIN
)
634 error
= SET_ERROR(EINVAL
);
645 if (!error
&& reset_bootfs
) {
646 error
= nvlist_remove(props
,
647 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), DATA_TYPE_STRING
);
650 error
= nvlist_add_uint64(props
,
651 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), objnum
);
659 spa_configfile_set(spa_t
*spa
, nvlist_t
*nvp
, boolean_t need_sync
)
662 spa_config_dirent_t
*dp
;
664 if (nvlist_lookup_string(nvp
, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
),
668 dp
= kmem_alloc(sizeof (spa_config_dirent_t
),
671 if (cachefile
[0] == '\0')
672 dp
->scd_path
= spa_strdup(spa_config_path
);
673 else if (strcmp(cachefile
, "none") == 0)
676 dp
->scd_path
= spa_strdup(cachefile
);
678 list_insert_head(&spa
->spa_config_list
, dp
);
680 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
684 spa_prop_set(spa_t
*spa
, nvlist_t
*nvp
)
687 nvpair_t
*elem
= NULL
;
688 boolean_t need_sync
= B_FALSE
;
690 if ((error
= spa_prop_validate(spa
, nvp
)) != 0)
693 while ((elem
= nvlist_next_nvpair(nvp
, elem
)) != NULL
) {
694 zpool_prop_t prop
= zpool_name_to_prop(nvpair_name(elem
));
696 if (prop
== ZPOOL_PROP_CACHEFILE
||
697 prop
== ZPOOL_PROP_ALTROOT
||
698 prop
== ZPOOL_PROP_READONLY
)
701 if (prop
== ZPOOL_PROP_VERSION
|| prop
== ZPOOL_PROP_INVAL
) {
704 if (prop
== ZPOOL_PROP_VERSION
) {
705 VERIFY(nvpair_value_uint64(elem
, &ver
) == 0);
707 ASSERT(zpool_prop_feature(nvpair_name(elem
)));
708 ver
= SPA_VERSION_FEATURES
;
712 /* Save time if the version is already set. */
713 if (ver
== spa_version(spa
))
717 * In addition to the pool directory object, we might
718 * create the pool properties object, the features for
719 * read object, the features for write object, or the
720 * feature descriptions object.
722 error
= dsl_sync_task(spa
->spa_name
, NULL
,
723 spa_sync_version
, &ver
,
724 6, ZFS_SPACE_CHECK_RESERVED
);
735 return (dsl_sync_task(spa
->spa_name
, NULL
, spa_sync_props
,
736 nvp
, 6, ZFS_SPACE_CHECK_RESERVED
));
743 * If the bootfs property value is dsobj, clear it.
746 spa_prop_clear_bootfs(spa_t
*spa
, uint64_t dsobj
, dmu_tx_t
*tx
)
748 if (spa
->spa_bootfs
== dsobj
&& spa
->spa_pool_props_object
!= 0) {
749 VERIFY(zap_remove(spa
->spa_meta_objset
,
750 spa
->spa_pool_props_object
,
751 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), tx
) == 0);
758 spa_change_guid_check(void *arg
, dmu_tx_t
*tx
)
760 ASSERTV(uint64_t *newguid
= arg
);
761 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
762 vdev_t
*rvd
= spa
->spa_root_vdev
;
765 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
766 vdev_state
= rvd
->vdev_state
;
767 spa_config_exit(spa
, SCL_STATE
, FTAG
);
769 if (vdev_state
!= VDEV_STATE_HEALTHY
)
770 return (SET_ERROR(ENXIO
));
772 ASSERT3U(spa_guid(spa
), !=, *newguid
);
778 spa_change_guid_sync(void *arg
, dmu_tx_t
*tx
)
780 uint64_t *newguid
= arg
;
781 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
783 vdev_t
*rvd
= spa
->spa_root_vdev
;
785 oldguid
= spa_guid(spa
);
787 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
788 rvd
->vdev_guid
= *newguid
;
789 rvd
->vdev_guid_sum
+= (*newguid
- oldguid
);
790 vdev_config_dirty(rvd
);
791 spa_config_exit(spa
, SCL_STATE
, FTAG
);
793 spa_history_log_internal(spa
, "guid change", tx
, "old=%llu new=%llu",
798 * Change the GUID for the pool. This is done so that we can later
799 * re-import a pool built from a clone of our own vdevs. We will modify
800 * the root vdev's guid, our own pool guid, and then mark all of our
801 * vdevs dirty. Note that we must make sure that all our vdevs are
802 * online when we do this, or else any vdevs that weren't present
803 * would be orphaned from our pool. We are also going to issue a
804 * sysevent to update any watchers.
807 spa_change_guid(spa_t
*spa
)
812 mutex_enter(&spa
->spa_vdev_top_lock
);
813 mutex_enter(&spa_namespace_lock
);
814 guid
= spa_generate_guid(NULL
);
816 error
= dsl_sync_task(spa
->spa_name
, spa_change_guid_check
,
817 spa_change_guid_sync
, &guid
, 5, ZFS_SPACE_CHECK_RESERVED
);
820 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
821 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_REGUID
);
824 mutex_exit(&spa_namespace_lock
);
825 mutex_exit(&spa
->spa_vdev_top_lock
);
831 * ==========================================================================
832 * SPA state manipulation (open/create/destroy/import/export)
833 * ==========================================================================
837 spa_error_entry_compare(const void *a
, const void *b
)
839 const spa_error_entry_t
*sa
= (const spa_error_entry_t
*)a
;
840 const spa_error_entry_t
*sb
= (const spa_error_entry_t
*)b
;
843 ret
= memcmp(&sa
->se_bookmark
, &sb
->se_bookmark
,
844 sizeof (zbookmark_phys_t
));
846 return (AVL_ISIGN(ret
));
850 * Utility function which retrieves copies of the current logs and
851 * re-initializes them in the process.
854 spa_get_errlists(spa_t
*spa
, avl_tree_t
*last
, avl_tree_t
*scrub
)
856 ASSERT(MUTEX_HELD(&spa
->spa_errlist_lock
));
858 bcopy(&spa
->spa_errlist_last
, last
, sizeof (avl_tree_t
));
859 bcopy(&spa
->spa_errlist_scrub
, scrub
, sizeof (avl_tree_t
));
861 avl_create(&spa
->spa_errlist_scrub
,
862 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
863 offsetof(spa_error_entry_t
, se_avl
));
864 avl_create(&spa
->spa_errlist_last
,
865 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
866 offsetof(spa_error_entry_t
, se_avl
));
870 spa_taskqs_init(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
872 const zio_taskq_info_t
*ztip
= &zio_taskqs
[t
][q
];
873 enum zti_modes mode
= ztip
->zti_mode
;
874 uint_t value
= ztip
->zti_value
;
875 uint_t count
= ztip
->zti_count
;
876 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
879 boolean_t batch
= B_FALSE
;
881 if (mode
== ZTI_MODE_NULL
) {
883 tqs
->stqs_taskq
= NULL
;
887 ASSERT3U(count
, >, 0);
889 tqs
->stqs_count
= count
;
890 tqs
->stqs_taskq
= kmem_alloc(count
* sizeof (taskq_t
*), KM_SLEEP
);
894 ASSERT3U(value
, >=, 1);
895 value
= MAX(value
, 1);
896 flags
|= TASKQ_DYNAMIC
;
901 flags
|= TASKQ_THREADS_CPU_PCT
;
902 value
= MIN(zio_taskq_batch_pct
, 100);
906 panic("unrecognized mode for %s_%s taskq (%u:%u) in "
908 zio_type_name
[t
], zio_taskq_types
[q
], mode
, value
);
912 for (uint_t i
= 0; i
< count
; i
++) {
916 (void) snprintf(name
, sizeof (name
), "%s_%s_%u",
917 zio_type_name
[t
], zio_taskq_types
[q
], i
);
919 (void) snprintf(name
, sizeof (name
), "%s_%s",
920 zio_type_name
[t
], zio_taskq_types
[q
]);
923 if (zio_taskq_sysdc
&& spa
->spa_proc
!= &p0
) {
925 flags
|= TASKQ_DC_BATCH
;
927 tq
= taskq_create_sysdc(name
, value
, 50, INT_MAX
,
928 spa
->spa_proc
, zio_taskq_basedc
, flags
);
930 pri_t pri
= maxclsyspri
;
932 * The write issue taskq can be extremely CPU
933 * intensive. Run it at slightly less important
934 * priority than the other taskqs. Under Linux this
935 * means incrementing the priority value on platforms
936 * like illumos it should be decremented.
938 if (t
== ZIO_TYPE_WRITE
&& q
== ZIO_TASKQ_ISSUE
)
941 tq
= taskq_create_proc(name
, value
, pri
, 50,
942 INT_MAX
, spa
->spa_proc
, flags
);
945 tqs
->stqs_taskq
[i
] = tq
;
950 spa_taskqs_fini(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
952 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
954 if (tqs
->stqs_taskq
== NULL
) {
955 ASSERT3U(tqs
->stqs_count
, ==, 0);
959 for (uint_t i
= 0; i
< tqs
->stqs_count
; i
++) {
960 ASSERT3P(tqs
->stqs_taskq
[i
], !=, NULL
);
961 taskq_destroy(tqs
->stqs_taskq
[i
]);
964 kmem_free(tqs
->stqs_taskq
, tqs
->stqs_count
* sizeof (taskq_t
*));
965 tqs
->stqs_taskq
= NULL
;
969 * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority.
970 * Note that a type may have multiple discrete taskqs to avoid lock contention
971 * on the taskq itself. In that case we choose which taskq at random by using
972 * the low bits of gethrtime().
975 spa_taskq_dispatch_ent(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
976 task_func_t
*func
, void *arg
, uint_t flags
, taskq_ent_t
*ent
)
978 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
981 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
982 ASSERT3U(tqs
->stqs_count
, !=, 0);
984 if (tqs
->stqs_count
== 1) {
985 tq
= tqs
->stqs_taskq
[0];
987 tq
= tqs
->stqs_taskq
[((uint64_t)gethrtime()) % tqs
->stqs_count
];
990 taskq_dispatch_ent(tq
, func
, arg
, flags
, ent
);
994 * Same as spa_taskq_dispatch_ent() but block on the task until completion.
997 spa_taskq_dispatch_sync(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
998 task_func_t
*func
, void *arg
, uint_t flags
)
1000 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1004 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
1005 ASSERT3U(tqs
->stqs_count
, !=, 0);
1007 if (tqs
->stqs_count
== 1) {
1008 tq
= tqs
->stqs_taskq
[0];
1010 tq
= tqs
->stqs_taskq
[((uint64_t)gethrtime()) % tqs
->stqs_count
];
1013 id
= taskq_dispatch(tq
, func
, arg
, flags
);
1015 taskq_wait_id(tq
, id
);
1019 spa_create_zio_taskqs(spa_t
*spa
)
1021 for (int t
= 0; t
< ZIO_TYPES
; t
++) {
1022 for (int q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1023 spa_taskqs_init(spa
, t
, q
);
1029 * Disabled until spa_thread() can be adapted for Linux.
1031 #undef HAVE_SPA_THREAD
1033 #if defined(_KERNEL) && defined(HAVE_SPA_THREAD)
1035 spa_thread(void *arg
)
1037 callb_cpr_t cprinfo
;
1040 user_t
*pu
= PTOU(curproc
);
1042 CALLB_CPR_INIT(&cprinfo
, &spa
->spa_proc_lock
, callb_generic_cpr
,
1045 ASSERT(curproc
!= &p0
);
1046 (void) snprintf(pu
->u_psargs
, sizeof (pu
->u_psargs
),
1047 "zpool-%s", spa
->spa_name
);
1048 (void) strlcpy(pu
->u_comm
, pu
->u_psargs
, sizeof (pu
->u_comm
));
1050 /* bind this thread to the requested psrset */
1051 if (zio_taskq_psrset_bind
!= PS_NONE
) {
1053 mutex_enter(&cpu_lock
);
1054 mutex_enter(&pidlock
);
1055 mutex_enter(&curproc
->p_lock
);
1057 if (cpupart_bind_thread(curthread
, zio_taskq_psrset_bind
,
1058 0, NULL
, NULL
) == 0) {
1059 curthread
->t_bind_pset
= zio_taskq_psrset_bind
;
1062 "Couldn't bind process for zfs pool \"%s\" to "
1063 "pset %d\n", spa
->spa_name
, zio_taskq_psrset_bind
);
1066 mutex_exit(&curproc
->p_lock
);
1067 mutex_exit(&pidlock
);
1068 mutex_exit(&cpu_lock
);
1072 if (zio_taskq_sysdc
) {
1073 sysdc_thread_enter(curthread
, 100, 0);
1076 spa
->spa_proc
= curproc
;
1077 spa
->spa_did
= curthread
->t_did
;
1079 spa_create_zio_taskqs(spa
);
1081 mutex_enter(&spa
->spa_proc_lock
);
1082 ASSERT(spa
->spa_proc_state
== SPA_PROC_CREATED
);
1084 spa
->spa_proc_state
= SPA_PROC_ACTIVE
;
1085 cv_broadcast(&spa
->spa_proc_cv
);
1087 CALLB_CPR_SAFE_BEGIN(&cprinfo
);
1088 while (spa
->spa_proc_state
== SPA_PROC_ACTIVE
)
1089 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1090 CALLB_CPR_SAFE_END(&cprinfo
, &spa
->spa_proc_lock
);
1092 ASSERT(spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
);
1093 spa
->spa_proc_state
= SPA_PROC_GONE
;
1094 spa
->spa_proc
= &p0
;
1095 cv_broadcast(&spa
->spa_proc_cv
);
1096 CALLB_CPR_EXIT(&cprinfo
); /* drops spa_proc_lock */
1098 mutex_enter(&curproc
->p_lock
);
1104 * Activate an uninitialized pool.
1107 spa_activate(spa_t
*spa
, int mode
)
1109 ASSERT(spa
->spa_state
== POOL_STATE_UNINITIALIZED
);
1111 spa
->spa_state
= POOL_STATE_ACTIVE
;
1112 spa
->spa_mode
= mode
;
1114 spa
->spa_normal_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
1115 spa
->spa_log_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
1117 /* Try to create a covering process */
1118 mutex_enter(&spa
->spa_proc_lock
);
1119 ASSERT(spa
->spa_proc_state
== SPA_PROC_NONE
);
1120 ASSERT(spa
->spa_proc
== &p0
);
1123 #ifdef HAVE_SPA_THREAD
1124 /* Only create a process if we're going to be around a while. */
1125 if (spa_create_process
&& strcmp(spa
->spa_name
, TRYIMPORT_NAME
) != 0) {
1126 if (newproc(spa_thread
, (caddr_t
)spa
, syscid
, maxclsyspri
,
1128 spa
->spa_proc_state
= SPA_PROC_CREATED
;
1129 while (spa
->spa_proc_state
== SPA_PROC_CREATED
) {
1130 cv_wait(&spa
->spa_proc_cv
,
1131 &spa
->spa_proc_lock
);
1133 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1134 ASSERT(spa
->spa_proc
!= &p0
);
1135 ASSERT(spa
->spa_did
!= 0);
1139 "Couldn't create process for zfs pool \"%s\"\n",
1144 #endif /* HAVE_SPA_THREAD */
1145 mutex_exit(&spa
->spa_proc_lock
);
1147 /* If we didn't create a process, we need to create our taskqs. */
1148 if (spa
->spa_proc
== &p0
) {
1149 spa_create_zio_taskqs(spa
);
1152 list_create(&spa
->spa_config_dirty_list
, sizeof (vdev_t
),
1153 offsetof(vdev_t
, vdev_config_dirty_node
));
1154 list_create(&spa
->spa_evicting_os_list
, sizeof (objset_t
),
1155 offsetof(objset_t
, os_evicting_node
));
1156 list_create(&spa
->spa_state_dirty_list
, sizeof (vdev_t
),
1157 offsetof(vdev_t
, vdev_state_dirty_node
));
1159 txg_list_create(&spa
->spa_vdev_txg_list
, spa
,
1160 offsetof(struct vdev
, vdev_txg_node
));
1162 avl_create(&spa
->spa_errlist_scrub
,
1163 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1164 offsetof(spa_error_entry_t
, se_avl
));
1165 avl_create(&spa
->spa_errlist_last
,
1166 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1167 offsetof(spa_error_entry_t
, se_avl
));
1169 spa_keystore_init(&spa
->spa_keystore
);
1172 * This taskq is used to perform zvol-minor-related tasks
1173 * asynchronously. This has several advantages, including easy
1174 * resolution of various deadlocks (zfsonlinux bug #3681).
1176 * The taskq must be single threaded to ensure tasks are always
1177 * processed in the order in which they were dispatched.
1179 * A taskq per pool allows one to keep the pools independent.
1180 * This way if one pool is suspended, it will not impact another.
1182 * The preferred location to dispatch a zvol minor task is a sync
1183 * task. In this context, there is easy access to the spa_t and minimal
1184 * error handling is required because the sync task must succeed.
1186 spa
->spa_zvol_taskq
= taskq_create("z_zvol", 1, defclsyspri
,
1190 * The taskq to upgrade datasets in this pool. Currently used by
1191 * feature SPA_FEATURE_USEROBJ_ACCOUNTING/SPA_FEATURE_PROJECT_QUOTA.
1193 spa
->spa_upgrade_taskq
= taskq_create("z_upgrade", boot_ncpus
,
1194 defclsyspri
, 1, INT_MAX
, TASKQ_DYNAMIC
);
1198 * Opposite of spa_activate().
1201 spa_deactivate(spa_t
*spa
)
1203 ASSERT(spa
->spa_sync_on
== B_FALSE
);
1204 ASSERT(spa
->spa_dsl_pool
== NULL
);
1205 ASSERT(spa
->spa_root_vdev
== NULL
);
1206 ASSERT(spa
->spa_async_zio_root
== NULL
);
1207 ASSERT(spa
->spa_state
!= POOL_STATE_UNINITIALIZED
);
1209 spa_evicting_os_wait(spa
);
1211 if (spa
->spa_zvol_taskq
) {
1212 taskq_destroy(spa
->spa_zvol_taskq
);
1213 spa
->spa_zvol_taskq
= NULL
;
1216 if (spa
->spa_upgrade_taskq
) {
1217 taskq_destroy(spa
->spa_upgrade_taskq
);
1218 spa
->spa_upgrade_taskq
= NULL
;
1221 txg_list_destroy(&spa
->spa_vdev_txg_list
);
1223 list_destroy(&spa
->spa_config_dirty_list
);
1224 list_destroy(&spa
->spa_evicting_os_list
);
1225 list_destroy(&spa
->spa_state_dirty_list
);
1227 taskq_cancel_id(system_delay_taskq
, spa
->spa_deadman_tqid
);
1229 for (int t
= 0; t
< ZIO_TYPES
; t
++) {
1230 for (int q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1231 spa_taskqs_fini(spa
, t
, q
);
1235 metaslab_class_destroy(spa
->spa_normal_class
);
1236 spa
->spa_normal_class
= NULL
;
1238 metaslab_class_destroy(spa
->spa_log_class
);
1239 spa
->spa_log_class
= NULL
;
1242 * If this was part of an import or the open otherwise failed, we may
1243 * still have errors left in the queues. Empty them just in case.
1245 spa_errlog_drain(spa
);
1246 avl_destroy(&spa
->spa_errlist_scrub
);
1247 avl_destroy(&spa
->spa_errlist_last
);
1249 spa_keystore_fini(&spa
->spa_keystore
);
1251 spa
->spa_state
= POOL_STATE_UNINITIALIZED
;
1253 mutex_enter(&spa
->spa_proc_lock
);
1254 if (spa
->spa_proc_state
!= SPA_PROC_NONE
) {
1255 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1256 spa
->spa_proc_state
= SPA_PROC_DEACTIVATE
;
1257 cv_broadcast(&spa
->spa_proc_cv
);
1258 while (spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
) {
1259 ASSERT(spa
->spa_proc
!= &p0
);
1260 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1262 ASSERT(spa
->spa_proc_state
== SPA_PROC_GONE
);
1263 spa
->spa_proc_state
= SPA_PROC_NONE
;
1265 ASSERT(spa
->spa_proc
== &p0
);
1266 mutex_exit(&spa
->spa_proc_lock
);
1269 * We want to make sure spa_thread() has actually exited the ZFS
1270 * module, so that the module can't be unloaded out from underneath
1273 if (spa
->spa_did
!= 0) {
1274 thread_join(spa
->spa_did
);
1280 * Verify a pool configuration, and construct the vdev tree appropriately. This
1281 * will create all the necessary vdevs in the appropriate layout, with each vdev
1282 * in the CLOSED state. This will prep the pool before open/creation/import.
1283 * All vdev validation is done by the vdev_alloc() routine.
1286 spa_config_parse(spa_t
*spa
, vdev_t
**vdp
, nvlist_t
*nv
, vdev_t
*parent
,
1287 uint_t id
, int atype
)
1293 if ((error
= vdev_alloc(spa
, vdp
, nv
, parent
, id
, atype
)) != 0)
1296 if ((*vdp
)->vdev_ops
->vdev_op_leaf
)
1299 error
= nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1302 if (error
== ENOENT
)
1308 return (SET_ERROR(EINVAL
));
1311 for (int c
= 0; c
< children
; c
++) {
1313 if ((error
= spa_config_parse(spa
, &vd
, child
[c
], *vdp
, c
,
1321 ASSERT(*vdp
!= NULL
);
1327 * Opposite of spa_load().
1330 spa_unload(spa_t
*spa
)
1334 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1339 spa_async_suspend(spa
);
1344 if (spa
->spa_sync_on
) {
1345 txg_sync_stop(spa
->spa_dsl_pool
);
1346 spa
->spa_sync_on
= B_FALSE
;
1350 * Even though vdev_free() also calls vdev_metaslab_fini, we need
1351 * to call it earlier, before we wait for async i/o to complete.
1352 * This ensures that there is no async metaslab prefetching, by
1353 * calling taskq_wait(mg_taskq).
1355 if (spa
->spa_root_vdev
!= NULL
) {
1356 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1357 for (int c
= 0; c
< spa
->spa_root_vdev
->vdev_children
; c
++)
1358 vdev_metaslab_fini(spa
->spa_root_vdev
->vdev_child
[c
]);
1359 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1362 if (spa
->spa_mmp
.mmp_thread
)
1363 mmp_thread_stop(spa
);
1366 * Wait for any outstanding async I/O to complete.
1368 if (spa
->spa_async_zio_root
!= NULL
) {
1369 for (int i
= 0; i
< max_ncpus
; i
++)
1370 (void) zio_wait(spa
->spa_async_zio_root
[i
]);
1371 kmem_free(spa
->spa_async_zio_root
, max_ncpus
* sizeof (void *));
1372 spa
->spa_async_zio_root
= NULL
;
1375 bpobj_close(&spa
->spa_deferred_bpobj
);
1377 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1382 if (spa
->spa_root_vdev
)
1383 vdev_free(spa
->spa_root_vdev
);
1384 ASSERT(spa
->spa_root_vdev
== NULL
);
1387 * Close the dsl pool.
1389 if (spa
->spa_dsl_pool
) {
1390 dsl_pool_close(spa
->spa_dsl_pool
);
1391 spa
->spa_dsl_pool
= NULL
;
1392 spa
->spa_meta_objset
= NULL
;
1398 * Drop and purge level 2 cache
1400 spa_l2cache_drop(spa
);
1402 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1403 vdev_free(spa
->spa_spares
.sav_vdevs
[i
]);
1404 if (spa
->spa_spares
.sav_vdevs
) {
1405 kmem_free(spa
->spa_spares
.sav_vdevs
,
1406 spa
->spa_spares
.sav_count
* sizeof (void *));
1407 spa
->spa_spares
.sav_vdevs
= NULL
;
1409 if (spa
->spa_spares
.sav_config
) {
1410 nvlist_free(spa
->spa_spares
.sav_config
);
1411 spa
->spa_spares
.sav_config
= NULL
;
1413 spa
->spa_spares
.sav_count
= 0;
1415 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
1416 vdev_clear_stats(spa
->spa_l2cache
.sav_vdevs
[i
]);
1417 vdev_free(spa
->spa_l2cache
.sav_vdevs
[i
]);
1419 if (spa
->spa_l2cache
.sav_vdevs
) {
1420 kmem_free(spa
->spa_l2cache
.sav_vdevs
,
1421 spa
->spa_l2cache
.sav_count
* sizeof (void *));
1422 spa
->spa_l2cache
.sav_vdevs
= NULL
;
1424 if (spa
->spa_l2cache
.sav_config
) {
1425 nvlist_free(spa
->spa_l2cache
.sav_config
);
1426 spa
->spa_l2cache
.sav_config
= NULL
;
1428 spa
->spa_l2cache
.sav_count
= 0;
1430 spa
->spa_async_suspended
= 0;
1432 if (spa
->spa_comment
!= NULL
) {
1433 spa_strfree(spa
->spa_comment
);
1434 spa
->spa_comment
= NULL
;
1437 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1441 * Load (or re-load) the current list of vdevs describing the active spares for
1442 * this pool. When this is called, we have some form of basic information in
1443 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1444 * then re-generate a more complete list including status information.
1447 spa_load_spares(spa_t
*spa
)
1454 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1457 * First, close and free any existing spare vdevs.
1459 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1460 vd
= spa
->spa_spares
.sav_vdevs
[i
];
1462 /* Undo the call to spa_activate() below */
1463 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1464 B_FALSE
)) != NULL
&& tvd
->vdev_isspare
)
1465 spa_spare_remove(tvd
);
1470 if (spa
->spa_spares
.sav_vdevs
)
1471 kmem_free(spa
->spa_spares
.sav_vdevs
,
1472 spa
->spa_spares
.sav_count
* sizeof (void *));
1474 if (spa
->spa_spares
.sav_config
== NULL
)
1477 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
1478 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
1480 spa
->spa_spares
.sav_count
= (int)nspares
;
1481 spa
->spa_spares
.sav_vdevs
= NULL
;
1487 * Construct the array of vdevs, opening them to get status in the
1488 * process. For each spare, there is potentially two different vdev_t
1489 * structures associated with it: one in the list of spares (used only
1490 * for basic validation purposes) and one in the active vdev
1491 * configuration (if it's spared in). During this phase we open and
1492 * validate each vdev on the spare list. If the vdev also exists in the
1493 * active configuration, then we also mark this vdev as an active spare.
1495 spa
->spa_spares
.sav_vdevs
= kmem_zalloc(nspares
* sizeof (void *),
1497 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1498 VERIFY(spa_config_parse(spa
, &vd
, spares
[i
], NULL
, 0,
1499 VDEV_ALLOC_SPARE
) == 0);
1502 spa
->spa_spares
.sav_vdevs
[i
] = vd
;
1504 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1505 B_FALSE
)) != NULL
) {
1506 if (!tvd
->vdev_isspare
)
1510 * We only mark the spare active if we were successfully
1511 * able to load the vdev. Otherwise, importing a pool
1512 * with a bad active spare would result in strange
1513 * behavior, because multiple pool would think the spare
1514 * is actively in use.
1516 * There is a vulnerability here to an equally bizarre
1517 * circumstance, where a dead active spare is later
1518 * brought back to life (onlined or otherwise). Given
1519 * the rarity of this scenario, and the extra complexity
1520 * it adds, we ignore the possibility.
1522 if (!vdev_is_dead(tvd
))
1523 spa_spare_activate(tvd
);
1527 vd
->vdev_aux
= &spa
->spa_spares
;
1529 if (vdev_open(vd
) != 0)
1532 if (vdev_validate_aux(vd
) == 0)
1537 * Recompute the stashed list of spares, with status information
1540 VERIFY(nvlist_remove(spa
->spa_spares
.sav_config
, ZPOOL_CONFIG_SPARES
,
1541 DATA_TYPE_NVLIST_ARRAY
) == 0);
1543 spares
= kmem_alloc(spa
->spa_spares
.sav_count
* sizeof (void *),
1545 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1546 spares
[i
] = vdev_config_generate(spa
,
1547 spa
->spa_spares
.sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_SPARE
);
1548 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
1549 ZPOOL_CONFIG_SPARES
, spares
, spa
->spa_spares
.sav_count
) == 0);
1550 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1551 nvlist_free(spares
[i
]);
1552 kmem_free(spares
, spa
->spa_spares
.sav_count
* sizeof (void *));
1556 * Load (or re-load) the current list of vdevs describing the active l2cache for
1557 * this pool. When this is called, we have some form of basic information in
1558 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1559 * then re-generate a more complete list including status information.
1560 * Devices which are already active have their details maintained, and are
1564 spa_load_l2cache(spa_t
*spa
)
1566 nvlist_t
**l2cache
= NULL
;
1568 int i
, j
, oldnvdevs
;
1570 vdev_t
*vd
, **oldvdevs
, **newvdevs
;
1571 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
1573 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1575 oldvdevs
= sav
->sav_vdevs
;
1576 oldnvdevs
= sav
->sav_count
;
1577 sav
->sav_vdevs
= NULL
;
1580 if (sav
->sav_config
== NULL
) {
1586 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
,
1587 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
1588 newvdevs
= kmem_alloc(nl2cache
* sizeof (void *), KM_SLEEP
);
1591 * Process new nvlist of vdevs.
1593 for (i
= 0; i
< nl2cache
; i
++) {
1594 VERIFY(nvlist_lookup_uint64(l2cache
[i
], ZPOOL_CONFIG_GUID
,
1598 for (j
= 0; j
< oldnvdevs
; j
++) {
1600 if (vd
!= NULL
&& guid
== vd
->vdev_guid
) {
1602 * Retain previous vdev for add/remove ops.
1610 if (newvdevs
[i
] == NULL
) {
1614 VERIFY(spa_config_parse(spa
, &vd
, l2cache
[i
], NULL
, 0,
1615 VDEV_ALLOC_L2CACHE
) == 0);
1620 * Commit this vdev as an l2cache device,
1621 * even if it fails to open.
1623 spa_l2cache_add(vd
);
1628 spa_l2cache_activate(vd
);
1630 if (vdev_open(vd
) != 0)
1633 (void) vdev_validate_aux(vd
);
1635 if (!vdev_is_dead(vd
))
1636 l2arc_add_vdev(spa
, vd
);
1640 sav
->sav_vdevs
= newvdevs
;
1641 sav
->sav_count
= (int)nl2cache
;
1644 * Recompute the stashed list of l2cache devices, with status
1645 * information this time.
1647 VERIFY(nvlist_remove(sav
->sav_config
, ZPOOL_CONFIG_L2CACHE
,
1648 DATA_TYPE_NVLIST_ARRAY
) == 0);
1650 if (sav
->sav_count
> 0)
1651 l2cache
= kmem_alloc(sav
->sav_count
* sizeof (void *),
1653 for (i
= 0; i
< sav
->sav_count
; i
++)
1654 l2cache
[i
] = vdev_config_generate(spa
,
1655 sav
->sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_L2CACHE
);
1656 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
,
1657 ZPOOL_CONFIG_L2CACHE
, l2cache
, sav
->sav_count
) == 0);
1661 * Purge vdevs that were dropped
1663 for (i
= 0; i
< oldnvdevs
; i
++) {
1668 ASSERT(vd
->vdev_isl2cache
);
1670 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
1671 pool
!= 0ULL && l2arc_vdev_present(vd
))
1672 l2arc_remove_vdev(vd
);
1673 vdev_clear_stats(vd
);
1679 kmem_free(oldvdevs
, oldnvdevs
* sizeof (void *));
1681 for (i
= 0; i
< sav
->sav_count
; i
++)
1682 nvlist_free(l2cache
[i
]);
1684 kmem_free(l2cache
, sav
->sav_count
* sizeof (void *));
1688 load_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
**value
)
1691 char *packed
= NULL
;
1696 error
= dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
);
1700 nvsize
= *(uint64_t *)db
->db_data
;
1701 dmu_buf_rele(db
, FTAG
);
1703 packed
= vmem_alloc(nvsize
, KM_SLEEP
);
1704 error
= dmu_read(spa
->spa_meta_objset
, obj
, 0, nvsize
, packed
,
1707 error
= nvlist_unpack(packed
, nvsize
, value
, 0);
1708 vmem_free(packed
, nvsize
);
1714 * Checks to see if the given vdev could not be opened, in which case we post a
1715 * sysevent to notify the autoreplace code that the device has been removed.
1718 spa_check_removed(vdev_t
*vd
)
1720 for (int c
= 0; c
< vd
->vdev_children
; c
++)
1721 spa_check_removed(vd
->vdev_child
[c
]);
1723 if (vd
->vdev_ops
->vdev_op_leaf
&& vdev_is_dead(vd
) &&
1725 zfs_post_autoreplace(vd
->vdev_spa
, vd
);
1726 spa_event_notify(vd
->vdev_spa
, vd
, NULL
, ESC_ZFS_VDEV_CHECK
);
1731 spa_config_valid_zaps(vdev_t
*vd
, vdev_t
*mvd
)
1733 ASSERT3U(vd
->vdev_children
, ==, mvd
->vdev_children
);
1735 vd
->vdev_top_zap
= mvd
->vdev_top_zap
;
1736 vd
->vdev_leaf_zap
= mvd
->vdev_leaf_zap
;
1738 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
1739 spa_config_valid_zaps(vd
->vdev_child
[i
], mvd
->vdev_child
[i
]);
1744 * Validate the current config against the MOS config
1747 spa_config_valid(spa_t
*spa
, nvlist_t
*config
)
1749 vdev_t
*mrvd
, *rvd
= spa
->spa_root_vdev
;
1752 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nv
) == 0);
1754 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1755 VERIFY(spa_config_parse(spa
, &mrvd
, nv
, NULL
, 0, VDEV_ALLOC_LOAD
) == 0);
1757 ASSERT3U(rvd
->vdev_children
, ==, mrvd
->vdev_children
);
1760 * If we're doing a normal import, then build up any additional
1761 * diagnostic information about missing devices in this config.
1762 * We'll pass this up to the user for further processing.
1764 if (!(spa
->spa_import_flags
& ZFS_IMPORT_MISSING_LOG
)) {
1765 nvlist_t
**child
, *nv
;
1768 child
= kmem_alloc(rvd
->vdev_children
* sizeof (nvlist_t
*),
1770 VERIFY(nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
1772 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
1773 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1774 vdev_t
*mtvd
= mrvd
->vdev_child
[c
];
1776 if (tvd
->vdev_ops
== &vdev_missing_ops
&&
1777 mtvd
->vdev_ops
!= &vdev_missing_ops
&&
1779 child
[idx
++] = vdev_config_generate(spa
, mtvd
,
1784 VERIFY(nvlist_add_nvlist_array(nv
,
1785 ZPOOL_CONFIG_CHILDREN
, child
, idx
) == 0);
1786 VERIFY(nvlist_add_nvlist(spa
->spa_load_info
,
1787 ZPOOL_CONFIG_MISSING_DEVICES
, nv
) == 0);
1789 for (int i
= 0; i
< idx
; i
++)
1790 nvlist_free(child
[i
]);
1793 kmem_free(child
, rvd
->vdev_children
* sizeof (char **));
1797 * Compare the root vdev tree with the information we have
1798 * from the MOS config (mrvd). Check each top-level vdev
1799 * with the corresponding MOS config top-level (mtvd).
1801 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
1802 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1803 vdev_t
*mtvd
= mrvd
->vdev_child
[c
];
1806 * Resolve any "missing" vdevs in the current configuration.
1807 * If we find that the MOS config has more accurate information
1808 * about the top-level vdev then use that vdev instead.
1810 if (tvd
->vdev_ops
== &vdev_missing_ops
&&
1811 mtvd
->vdev_ops
!= &vdev_missing_ops
) {
1813 if (!(spa
->spa_import_flags
& ZFS_IMPORT_MISSING_LOG
))
1817 * Device specific actions.
1819 if (mtvd
->vdev_islog
) {
1820 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
1823 * XXX - once we have 'readonly' pool
1824 * support we should be able to handle
1825 * missing data devices by transitioning
1826 * the pool to readonly.
1832 * Swap the missing vdev with the data we were
1833 * able to obtain from the MOS config.
1835 vdev_remove_child(rvd
, tvd
);
1836 vdev_remove_child(mrvd
, mtvd
);
1838 vdev_add_child(rvd
, mtvd
);
1839 vdev_add_child(mrvd
, tvd
);
1841 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1843 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1847 if (mtvd
->vdev_islog
) {
1849 * Load the slog device's state from the MOS
1850 * config since it's possible that the label
1851 * does not contain the most up-to-date
1854 vdev_load_log_state(tvd
, mtvd
);
1859 * Per-vdev ZAP info is stored exclusively in the MOS.
1861 spa_config_valid_zaps(tvd
, mtvd
);
1866 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1869 * Ensure we were able to validate the config.
1871 return (rvd
->vdev_guid_sum
== spa
->spa_uberblock
.ub_guid_sum
);
1875 * Check for missing log devices
1878 spa_check_logs(spa_t
*spa
)
1880 boolean_t rv
= B_FALSE
;
1881 dsl_pool_t
*dp
= spa_get_dsl(spa
);
1883 switch (spa
->spa_log_state
) {
1886 case SPA_LOG_MISSING
:
1887 /* need to recheck in case slog has been restored */
1888 case SPA_LOG_UNKNOWN
:
1889 rv
= (dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
1890 zil_check_log_chain
, NULL
, DS_FIND_CHILDREN
) != 0);
1892 spa_set_log_state(spa
, SPA_LOG_MISSING
);
1899 spa_passivate_log(spa_t
*spa
)
1901 vdev_t
*rvd
= spa
->spa_root_vdev
;
1902 boolean_t slog_found
= B_FALSE
;
1904 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
1906 if (!spa_has_slogs(spa
))
1909 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
1910 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1911 metaslab_group_t
*mg
= tvd
->vdev_mg
;
1913 if (tvd
->vdev_islog
) {
1914 metaslab_group_passivate(mg
);
1915 slog_found
= B_TRUE
;
1919 return (slog_found
);
1923 spa_activate_log(spa_t
*spa
)
1925 vdev_t
*rvd
= spa
->spa_root_vdev
;
1927 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
1929 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
1930 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1931 metaslab_group_t
*mg
= tvd
->vdev_mg
;
1933 if (tvd
->vdev_islog
)
1934 metaslab_group_activate(mg
);
1939 spa_offline_log(spa_t
*spa
)
1943 error
= dmu_objset_find(spa_name(spa
), zil_vdev_offline
,
1944 NULL
, DS_FIND_CHILDREN
);
1947 * We successfully offlined the log device, sync out the
1948 * current txg so that the "stubby" block can be removed
1951 txg_wait_synced(spa
->spa_dsl_pool
, 0);
1957 spa_aux_check_removed(spa_aux_vdev_t
*sav
)
1959 for (int i
= 0; i
< sav
->sav_count
; i
++)
1960 spa_check_removed(sav
->sav_vdevs
[i
]);
1964 spa_claim_notify(zio_t
*zio
)
1966 spa_t
*spa
= zio
->io_spa
;
1971 mutex_enter(&spa
->spa_props_lock
); /* any mutex will do */
1972 if (spa
->spa_claim_max_txg
< zio
->io_bp
->blk_birth
)
1973 spa
->spa_claim_max_txg
= zio
->io_bp
->blk_birth
;
1974 mutex_exit(&spa
->spa_props_lock
);
1977 typedef struct spa_load_error
{
1978 uint64_t sle_meta_count
;
1979 uint64_t sle_data_count
;
1983 spa_load_verify_done(zio_t
*zio
)
1985 blkptr_t
*bp
= zio
->io_bp
;
1986 spa_load_error_t
*sle
= zio
->io_private
;
1987 dmu_object_type_t type
= BP_GET_TYPE(bp
);
1988 int error
= zio
->io_error
;
1989 spa_t
*spa
= zio
->io_spa
;
1991 abd_free(zio
->io_abd
);
1993 if ((BP_GET_LEVEL(bp
) != 0 || DMU_OT_IS_METADATA(type
)) &&
1994 type
!= DMU_OT_INTENT_LOG
)
1995 atomic_inc_64(&sle
->sle_meta_count
);
1997 atomic_inc_64(&sle
->sle_data_count
);
2000 mutex_enter(&spa
->spa_scrub_lock
);
2001 spa
->spa_load_verify_ios
--;
2002 cv_broadcast(&spa
->spa_scrub_io_cv
);
2003 mutex_exit(&spa
->spa_scrub_lock
);
2007 * Maximum number of concurrent scrub i/os to create while verifying
2008 * a pool while importing it.
2010 int spa_load_verify_maxinflight
= 10000;
2011 int spa_load_verify_metadata
= B_TRUE
;
2012 int spa_load_verify_data
= B_TRUE
;
2016 spa_load_verify_cb(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
2017 const zbookmark_phys_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
2019 if (bp
== NULL
|| BP_IS_HOLE(bp
) || BP_IS_EMBEDDED(bp
))
2022 * Note: normally this routine will not be called if
2023 * spa_load_verify_metadata is not set. However, it may be useful
2024 * to manually set the flag after the traversal has begun.
2026 if (!spa_load_verify_metadata
)
2028 if (!BP_IS_METADATA(bp
) && !spa_load_verify_data
)
2032 size_t size
= BP_GET_PSIZE(bp
);
2034 mutex_enter(&spa
->spa_scrub_lock
);
2035 while (spa
->spa_load_verify_ios
>= spa_load_verify_maxinflight
)
2036 cv_wait(&spa
->spa_scrub_io_cv
, &spa
->spa_scrub_lock
);
2037 spa
->spa_load_verify_ios
++;
2038 mutex_exit(&spa
->spa_scrub_lock
);
2040 zio_nowait(zio_read(rio
, spa
, bp
, abd_alloc_for_io(size
, B_FALSE
), size
,
2041 spa_load_verify_done
, rio
->io_private
, ZIO_PRIORITY_SCRUB
,
2042 ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_CANFAIL
|
2043 ZIO_FLAG_SCRUB
| ZIO_FLAG_RAW
, zb
));
2049 verify_dataset_name_len(dsl_pool_t
*dp
, dsl_dataset_t
*ds
, void *arg
)
2051 if (dsl_dataset_namelen(ds
) >= ZFS_MAX_DATASET_NAME_LEN
)
2052 return (SET_ERROR(ENAMETOOLONG
));
2058 spa_load_verify(spa_t
*spa
)
2061 spa_load_error_t sle
= { 0 };
2062 zpool_rewind_policy_t policy
;
2063 boolean_t verify_ok
= B_FALSE
;
2066 zpool_get_rewind_policy(spa
->spa_config
, &policy
);
2068 if (policy
.zrp_request
& ZPOOL_NEVER_REWIND
)
2071 dsl_pool_config_enter(spa
->spa_dsl_pool
, FTAG
);
2072 error
= dmu_objset_find_dp(spa
->spa_dsl_pool
,
2073 spa
->spa_dsl_pool
->dp_root_dir_obj
, verify_dataset_name_len
, NULL
,
2075 dsl_pool_config_exit(spa
->spa_dsl_pool
, FTAG
);
2079 rio
= zio_root(spa
, NULL
, &sle
,
2080 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
);
2082 if (spa_load_verify_metadata
) {
2083 error
= traverse_pool(spa
, spa
->spa_verify_min_txg
,
2084 TRAVERSE_PRE
| TRAVERSE_PREFETCH_METADATA
|
2085 TRAVERSE_NO_DECRYPT
, spa_load_verify_cb
, rio
);
2088 (void) zio_wait(rio
);
2090 spa
->spa_load_meta_errors
= sle
.sle_meta_count
;
2091 spa
->spa_load_data_errors
= sle
.sle_data_count
;
2093 if (!error
&& sle
.sle_meta_count
<= policy
.zrp_maxmeta
&&
2094 sle
.sle_data_count
<= policy
.zrp_maxdata
) {
2098 spa
->spa_load_txg
= spa
->spa_uberblock
.ub_txg
;
2099 spa
->spa_load_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
2101 loss
= spa
->spa_last_ubsync_txg_ts
- spa
->spa_load_txg_ts
;
2102 VERIFY(nvlist_add_uint64(spa
->spa_load_info
,
2103 ZPOOL_CONFIG_LOAD_TIME
, spa
->spa_load_txg_ts
) == 0);
2104 VERIFY(nvlist_add_int64(spa
->spa_load_info
,
2105 ZPOOL_CONFIG_REWIND_TIME
, loss
) == 0);
2106 VERIFY(nvlist_add_uint64(spa
->spa_load_info
,
2107 ZPOOL_CONFIG_LOAD_DATA_ERRORS
, sle
.sle_data_count
) == 0);
2109 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
;
2113 if (error
!= ENXIO
&& error
!= EIO
)
2114 error
= SET_ERROR(EIO
);
2118 return (verify_ok
? 0 : EIO
);
2122 * Find a value in the pool props object.
2125 spa_prop_find(spa_t
*spa
, zpool_prop_t prop
, uint64_t *val
)
2127 (void) zap_lookup(spa
->spa_meta_objset
, spa
->spa_pool_props_object
,
2128 zpool_prop_to_name(prop
), sizeof (uint64_t), 1, val
);
2132 * Find a value in the pool directory object.
2135 spa_dir_prop(spa_t
*spa
, const char *name
, uint64_t *val
)
2137 return (zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
2138 name
, sizeof (uint64_t), 1, val
));
2142 spa_vdev_err(vdev_t
*vdev
, vdev_aux_t aux
, int err
)
2144 vdev_set_state(vdev
, B_TRUE
, VDEV_STATE_CANT_OPEN
, aux
);
2149 * Fix up config after a partly-completed split. This is done with the
2150 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
2151 * pool have that entry in their config, but only the splitting one contains
2152 * a list of all the guids of the vdevs that are being split off.
2154 * This function determines what to do with that list: either rejoin
2155 * all the disks to the pool, or complete the splitting process. To attempt
2156 * the rejoin, each disk that is offlined is marked online again, and
2157 * we do a reopen() call. If the vdev label for every disk that was
2158 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
2159 * then we call vdev_split() on each disk, and complete the split.
2161 * Otherwise we leave the config alone, with all the vdevs in place in
2162 * the original pool.
2165 spa_try_repair(spa_t
*spa
, nvlist_t
*config
)
2172 boolean_t attempt_reopen
;
2174 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
, &nvl
) != 0)
2177 /* check that the config is complete */
2178 if (nvlist_lookup_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
2179 &glist
, &gcount
) != 0)
2182 vd
= kmem_zalloc(gcount
* sizeof (vdev_t
*), KM_SLEEP
);
2184 /* attempt to online all the vdevs & validate */
2185 attempt_reopen
= B_TRUE
;
2186 for (i
= 0; i
< gcount
; i
++) {
2187 if (glist
[i
] == 0) /* vdev is hole */
2190 vd
[i
] = spa_lookup_by_guid(spa
, glist
[i
], B_FALSE
);
2191 if (vd
[i
] == NULL
) {
2193 * Don't bother attempting to reopen the disks;
2194 * just do the split.
2196 attempt_reopen
= B_FALSE
;
2198 /* attempt to re-online it */
2199 vd
[i
]->vdev_offline
= B_FALSE
;
2203 if (attempt_reopen
) {
2204 vdev_reopen(spa
->spa_root_vdev
);
2206 /* check each device to see what state it's in */
2207 for (extracted
= 0, i
= 0; i
< gcount
; i
++) {
2208 if (vd
[i
] != NULL
&&
2209 vd
[i
]->vdev_stat
.vs_aux
!= VDEV_AUX_SPLIT_POOL
)
2216 * If every disk has been moved to the new pool, or if we never
2217 * even attempted to look at them, then we split them off for
2220 if (!attempt_reopen
|| gcount
== extracted
) {
2221 for (i
= 0; i
< gcount
; i
++)
2224 vdev_reopen(spa
->spa_root_vdev
);
2227 kmem_free(vd
, gcount
* sizeof (vdev_t
*));
2231 spa_load(spa_t
*spa
, spa_load_state_t state
, spa_import_type_t type
,
2232 boolean_t mosconfig
)
2234 nvlist_t
*config
= spa
->spa_config
;
2235 char *ereport
= FM_EREPORT_ZFS_POOL
;
2241 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
, &pool_guid
))
2242 return (SET_ERROR(EINVAL
));
2244 ASSERT(spa
->spa_comment
== NULL
);
2245 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
2246 spa
->spa_comment
= spa_strdup(comment
);
2249 * Versioning wasn't explicitly added to the label until later, so if
2250 * it's not present treat it as the initial version.
2252 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
2253 &spa
->spa_ubsync
.ub_version
) != 0)
2254 spa
->spa_ubsync
.ub_version
= SPA_VERSION_INITIAL
;
2256 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
2257 &spa
->spa_config_txg
);
2259 if ((state
== SPA_LOAD_IMPORT
|| state
== SPA_LOAD_TRYIMPORT
) &&
2260 spa_guid_exists(pool_guid
, 0)) {
2261 error
= SET_ERROR(EEXIST
);
2263 spa
->spa_config_guid
= pool_guid
;
2265 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
,
2267 VERIFY(nvlist_dup(nvl
, &spa
->spa_config_splitting
,
2271 nvlist_free(spa
->spa_load_info
);
2272 spa
->spa_load_info
= fnvlist_alloc();
2274 gethrestime(&spa
->spa_loaded_ts
);
2275 error
= spa_load_impl(spa
, pool_guid
, config
, state
, type
,
2276 mosconfig
, &ereport
);
2280 * Don't count references from objsets that are already closed
2281 * and are making their way through the eviction process.
2283 spa_evicting_os_wait(spa
);
2284 spa
->spa_minref
= refcount_count(&spa
->spa_refcount
);
2286 if (error
!= EEXIST
) {
2287 spa
->spa_loaded_ts
.tv_sec
= 0;
2288 spa
->spa_loaded_ts
.tv_nsec
= 0;
2290 if (error
!= EBADF
) {
2291 zfs_ereport_post(ereport
, spa
, NULL
, NULL
, NULL
, 0, 0);
2294 spa
->spa_load_state
= error
? SPA_LOAD_ERROR
: SPA_LOAD_NONE
;
2302 * Count the number of per-vdev ZAPs associated with all of the vdevs in the
2303 * vdev tree rooted in the given vd, and ensure that each ZAP is present in the
2304 * spa's per-vdev ZAP list.
2307 vdev_count_verify_zaps(vdev_t
*vd
)
2309 spa_t
*spa
= vd
->vdev_spa
;
2312 if (vd
->vdev_top_zap
!= 0) {
2314 ASSERT0(zap_lookup_int(spa
->spa_meta_objset
,
2315 spa
->spa_all_vdev_zaps
, vd
->vdev_top_zap
));
2317 if (vd
->vdev_leaf_zap
!= 0) {
2319 ASSERT0(zap_lookup_int(spa
->spa_meta_objset
,
2320 spa
->spa_all_vdev_zaps
, vd
->vdev_leaf_zap
));
2323 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
2324 total
+= vdev_count_verify_zaps(vd
->vdev_child
[i
]);
2332 * Determine whether the activity check is required.
2335 spa_activity_check_required(spa_t
*spa
, uberblock_t
*ub
, nvlist_t
*label
,
2339 uint64_t hostid
= 0;
2340 uint64_t tryconfig_txg
= 0;
2341 uint64_t tryconfig_timestamp
= 0;
2344 if (nvlist_exists(config
, ZPOOL_CONFIG_LOAD_INFO
)) {
2345 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
2346 (void) nvlist_lookup_uint64(nvinfo
, ZPOOL_CONFIG_MMP_TXG
,
2348 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_TIMESTAMP
,
2349 &tryconfig_timestamp
);
2352 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
, &state
);
2355 * Disable the MMP activity check - This is used by zdb which
2356 * is intended to be used on potentially active pools.
2358 if (spa
->spa_import_flags
& ZFS_IMPORT_SKIP_MMP
)
2362 * Skip the activity check when the MMP feature is disabled.
2364 if (ub
->ub_mmp_magic
== MMP_MAGIC
&& ub
->ub_mmp_delay
== 0)
2367 * If the tryconfig_* values are nonzero, they are the results of an
2368 * earlier tryimport. If they match the uberblock we just found, then
2369 * the pool has not changed and we return false so we do not test a
2372 if (tryconfig_txg
&& tryconfig_txg
== ub
->ub_txg
&&
2373 tryconfig_timestamp
&& tryconfig_timestamp
== ub
->ub_timestamp
)
2377 * Allow the activity check to be skipped when importing the pool
2378 * on the same host which last imported it. Since the hostid from
2379 * configuration may be stale use the one read from the label.
2381 if (nvlist_exists(label
, ZPOOL_CONFIG_HOSTID
))
2382 hostid
= fnvlist_lookup_uint64(label
, ZPOOL_CONFIG_HOSTID
);
2384 if (hostid
== spa_get_hostid())
2388 * Skip the activity test when the pool was cleanly exported.
2390 if (state
!= POOL_STATE_ACTIVE
)
2397 * Perform the import activity check. If the user canceled the import or
2398 * we detected activity then fail.
2401 spa_activity_check(spa_t
*spa
, uberblock_t
*ub
, nvlist_t
*config
)
2403 uint64_t import_intervals
= MAX(zfs_multihost_import_intervals
, 1);
2404 uint64_t txg
= ub
->ub_txg
;
2405 uint64_t timestamp
= ub
->ub_timestamp
;
2406 uint64_t import_delay
= NANOSEC
;
2407 hrtime_t import_expire
;
2408 nvlist_t
*mmp_label
= NULL
;
2409 vdev_t
*rvd
= spa
->spa_root_vdev
;
2414 cv_init(&cv
, NULL
, CV_DEFAULT
, NULL
);
2415 mutex_init(&mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
2419 * If ZPOOL_CONFIG_MMP_TXG is present an activity check was performed
2420 * during the earlier tryimport. If the txg recorded there is 0 then
2421 * the pool is known to be active on another host.
2423 * Otherwise, the pool might be in use on another node. Check for
2424 * changes in the uberblocks on disk if necessary.
2426 if (nvlist_exists(config
, ZPOOL_CONFIG_LOAD_INFO
)) {
2427 nvlist_t
*nvinfo
= fnvlist_lookup_nvlist(config
,
2428 ZPOOL_CONFIG_LOAD_INFO
);
2430 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_TXG
) &&
2431 fnvlist_lookup_uint64(nvinfo
, ZPOOL_CONFIG_MMP_TXG
) == 0) {
2432 vdev_uberblock_load(rvd
, ub
, &mmp_label
);
2433 error
= SET_ERROR(EREMOTEIO
);
2439 * Preferentially use the zfs_multihost_interval from the node which
2440 * last imported the pool. This value is stored in an MMP uberblock as.
2442 * ub_mmp_delay * vdev_count_leaves() == zfs_multihost_interval
2444 if (ub
->ub_mmp_magic
== MMP_MAGIC
&& ub
->ub_mmp_delay
)
2445 import_delay
= MAX(import_delay
, import_intervals
*
2446 ub
->ub_mmp_delay
* MAX(vdev_count_leaves(spa
), 1));
2448 /* Apply a floor using the local default values. */
2449 import_delay
= MAX(import_delay
, import_intervals
*
2450 MSEC2NSEC(MAX(zfs_multihost_interval
, MMP_MIN_INTERVAL
)));
2452 /* Add a small random factor in case of simultaneous imports (0-25%) */
2453 import_expire
= gethrtime() + import_delay
+
2454 (import_delay
* spa_get_random(250) / 1000);
2456 while (gethrtime() < import_expire
) {
2457 vdev_uberblock_load(rvd
, ub
, &mmp_label
);
2459 if (txg
!= ub
->ub_txg
|| timestamp
!= ub
->ub_timestamp
) {
2460 error
= SET_ERROR(EREMOTEIO
);
2465 nvlist_free(mmp_label
);
2469 error
= cv_timedwait_sig(&cv
, &mtx
, ddi_get_lbolt() + hz
);
2471 error
= SET_ERROR(EINTR
);
2479 mutex_destroy(&mtx
);
2483 * If the pool is determined to be active store the status in the
2484 * spa->spa_load_info nvlist. If the remote hostname or hostid are
2485 * available from configuration read from disk store them as well.
2486 * This allows 'zpool import' to generate a more useful message.
2488 * ZPOOL_CONFIG_MMP_STATE - observed pool status (mandatory)
2489 * ZPOOL_CONFIG_MMP_HOSTNAME - hostname from the active pool
2490 * ZPOOL_CONFIG_MMP_HOSTID - hostid from the active pool
2492 if (error
== EREMOTEIO
) {
2493 char *hostname
= "<unknown>";
2494 uint64_t hostid
= 0;
2497 if (nvlist_exists(mmp_label
, ZPOOL_CONFIG_HOSTNAME
)) {
2498 hostname
= fnvlist_lookup_string(mmp_label
,
2499 ZPOOL_CONFIG_HOSTNAME
);
2500 fnvlist_add_string(spa
->spa_load_info
,
2501 ZPOOL_CONFIG_MMP_HOSTNAME
, hostname
);
2504 if (nvlist_exists(mmp_label
, ZPOOL_CONFIG_HOSTID
)) {
2505 hostid
= fnvlist_lookup_uint64(mmp_label
,
2506 ZPOOL_CONFIG_HOSTID
);
2507 fnvlist_add_uint64(spa
->spa_load_info
,
2508 ZPOOL_CONFIG_MMP_HOSTID
, hostid
);
2512 fnvlist_add_uint64(spa
->spa_load_info
,
2513 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_ACTIVE
);
2514 fnvlist_add_uint64(spa
->spa_load_info
,
2515 ZPOOL_CONFIG_MMP_TXG
, 0);
2517 error
= spa_vdev_err(rvd
, VDEV_AUX_ACTIVE
, EREMOTEIO
);
2521 nvlist_free(mmp_label
);
2527 * Load an existing storage pool, using the pool's builtin spa_config as a
2528 * source of configuration information.
2530 __attribute__((always_inline
))
2532 spa_load_impl(spa_t
*spa
, uint64_t pool_guid
, nvlist_t
*config
,
2533 spa_load_state_t state
, spa_import_type_t type
, boolean_t mosconfig
,
2537 nvlist_t
*nvroot
= NULL
;
2540 uberblock_t
*ub
= &spa
->spa_uberblock
;
2541 uint64_t children
, config_cache_txg
= spa
->spa_config_txg
;
2542 int orig_mode
= spa
->spa_mode
;
2545 boolean_t missing_feat_write
= B_FALSE
;
2546 boolean_t activity_check
= B_FALSE
;
2549 * If this is an untrusted config, access the pool in read-only mode.
2550 * This prevents things like resilvering recently removed devices.
2553 spa
->spa_mode
= FREAD
;
2555 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
2557 spa
->spa_load_state
= state
;
2559 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvroot
))
2560 return (SET_ERROR(EINVAL
));
2562 parse
= (type
== SPA_IMPORT_EXISTING
?
2563 VDEV_ALLOC_LOAD
: VDEV_ALLOC_SPLIT
);
2566 * Create "The Godfather" zio to hold all async IOs
2568 spa
->spa_async_zio_root
= kmem_alloc(max_ncpus
* sizeof (void *),
2570 for (int i
= 0; i
< max_ncpus
; i
++) {
2571 spa
->spa_async_zio_root
[i
] = zio_root(spa
, NULL
, NULL
,
2572 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
2573 ZIO_FLAG_GODFATHER
);
2577 * Parse the configuration into a vdev tree. We explicitly set the
2578 * value that will be returned by spa_version() since parsing the
2579 * configuration requires knowing the version number.
2581 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2582 error
= spa_config_parse(spa
, &rvd
, nvroot
, NULL
, 0, parse
);
2583 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2588 ASSERT(spa
->spa_root_vdev
== rvd
);
2589 ASSERT3U(spa
->spa_min_ashift
, >=, SPA_MINBLOCKSHIFT
);
2590 ASSERT3U(spa
->spa_max_ashift
, <=, SPA_MAXBLOCKSHIFT
);
2592 if (type
!= SPA_IMPORT_ASSEMBLE
) {
2593 ASSERT(spa_guid(spa
) == pool_guid
);
2597 * Try to open all vdevs, loading each label in the process.
2599 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2600 error
= vdev_open(rvd
);
2601 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2606 * We need to validate the vdev labels against the configuration that
2607 * we have in hand, which is dependent on the setting of mosconfig. If
2608 * mosconfig is true then we're validating the vdev labels based on
2609 * that config. Otherwise, we're validating against the cached config
2610 * (zpool.cache) that was read when we loaded the zfs module, and then
2611 * later we will recursively call spa_load() and validate against
2614 * If we're assembling a new pool that's been split off from an
2615 * existing pool, the labels haven't yet been updated so we skip
2616 * validation for now.
2618 if (type
!= SPA_IMPORT_ASSEMBLE
) {
2619 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2620 error
= vdev_validate(rvd
, mosconfig
);
2621 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2626 if (rvd
->vdev_state
<= VDEV_STATE_CANT_OPEN
)
2627 return (SET_ERROR(ENXIO
));
2631 * Find the best uberblock.
2633 vdev_uberblock_load(rvd
, ub
, &label
);
2636 * If we weren't able to find a single valid uberblock, return failure.
2638 if (ub
->ub_txg
== 0) {
2640 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, ENXIO
));
2644 * For pools which have the multihost property on determine if the
2645 * pool is truly inactive and can be safely imported. Prevent
2646 * hosts which don't have a hostid set from importing the pool.
2648 activity_check
= spa_activity_check_required(spa
, ub
, label
, config
);
2649 if (activity_check
) {
2650 if (ub
->ub_mmp_magic
== MMP_MAGIC
&& ub
->ub_mmp_delay
&&
2651 spa_get_hostid() == 0) {
2653 fnvlist_add_uint64(spa
->spa_load_info
,
2654 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_NO_HOSTID
);
2655 return (spa_vdev_err(rvd
, VDEV_AUX_ACTIVE
, EREMOTEIO
));
2658 error
= spa_activity_check(spa
, ub
, config
);
2664 fnvlist_add_uint64(spa
->spa_load_info
,
2665 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_INACTIVE
);
2666 fnvlist_add_uint64(spa
->spa_load_info
,
2667 ZPOOL_CONFIG_MMP_TXG
, ub
->ub_txg
);
2671 * If the pool has an unsupported version we can't open it.
2673 if (!SPA_VERSION_IS_SUPPORTED(ub
->ub_version
)) {
2675 return (spa_vdev_err(rvd
, VDEV_AUX_VERSION_NEWER
, ENOTSUP
));
2678 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
2682 * If we weren't able to find what's necessary for reading the
2683 * MOS in the label, return failure.
2685 if (label
== NULL
|| nvlist_lookup_nvlist(label
,
2686 ZPOOL_CONFIG_FEATURES_FOR_READ
, &features
) != 0) {
2688 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
2693 * Update our in-core representation with the definitive values
2696 nvlist_free(spa
->spa_label_features
);
2697 VERIFY(nvlist_dup(features
, &spa
->spa_label_features
, 0) == 0);
2703 * Look through entries in the label nvlist's features_for_read. If
2704 * there is a feature listed there which we don't understand then we
2705 * cannot open a pool.
2707 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
2708 nvlist_t
*unsup_feat
;
2710 VERIFY(nvlist_alloc(&unsup_feat
, NV_UNIQUE_NAME
, KM_SLEEP
) ==
2713 for (nvpair_t
*nvp
= nvlist_next_nvpair(spa
->spa_label_features
,
2715 nvp
= nvlist_next_nvpair(spa
->spa_label_features
, nvp
)) {
2716 if (!zfeature_is_supported(nvpair_name(nvp
))) {
2717 VERIFY(nvlist_add_string(unsup_feat
,
2718 nvpair_name(nvp
), "") == 0);
2722 if (!nvlist_empty(unsup_feat
)) {
2723 VERIFY(nvlist_add_nvlist(spa
->spa_load_info
,
2724 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
) == 0);
2725 nvlist_free(unsup_feat
);
2726 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
2730 nvlist_free(unsup_feat
);
2734 * If the vdev guid sum doesn't match the uberblock, we have an
2735 * incomplete configuration. We first check to see if the pool
2736 * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN).
2737 * If it is, defer the vdev_guid_sum check till later so we
2738 * can handle missing vdevs.
2740 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VDEV_CHILDREN
,
2741 &children
) != 0 && mosconfig
&& type
!= SPA_IMPORT_ASSEMBLE
&&
2742 rvd
->vdev_guid_sum
!= ub
->ub_guid_sum
)
2743 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
, ENXIO
));
2745 if (type
!= SPA_IMPORT_ASSEMBLE
&& spa
->spa_config_splitting
) {
2746 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2747 spa_try_repair(spa
, config
);
2748 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2749 nvlist_free(spa
->spa_config_splitting
);
2750 spa
->spa_config_splitting
= NULL
;
2754 * Initialize internal SPA structures.
2756 spa
->spa_state
= POOL_STATE_ACTIVE
;
2757 spa
->spa_ubsync
= spa
->spa_uberblock
;
2758 spa
->spa_verify_min_txg
= spa
->spa_extreme_rewind
?
2759 TXG_INITIAL
- 1 : spa_last_synced_txg(spa
) - TXG_DEFER_SIZE
- 1;
2760 spa
->spa_first_txg
= spa
->spa_last_ubsync_txg
?
2761 spa
->spa_last_ubsync_txg
: spa_last_synced_txg(spa
) + 1;
2762 spa
->spa_claim_max_txg
= spa
->spa_first_txg
;
2763 spa
->spa_prev_software_version
= ub
->ub_software_version
;
2765 error
= dsl_pool_init(spa
, spa
->spa_first_txg
, &spa
->spa_dsl_pool
);
2767 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2768 spa
->spa_meta_objset
= spa
->spa_dsl_pool
->dp_meta_objset
;
2770 if (spa_dir_prop(spa
, DMU_POOL_CONFIG
, &spa
->spa_config_object
) != 0)
2771 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2773 if (spa_version(spa
) >= SPA_VERSION_FEATURES
) {
2774 boolean_t missing_feat_read
= B_FALSE
;
2775 nvlist_t
*unsup_feat
, *enabled_feat
;
2777 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_READ
,
2778 &spa
->spa_feat_for_read_obj
) != 0) {
2779 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2782 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_WRITE
,
2783 &spa
->spa_feat_for_write_obj
) != 0) {
2784 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2787 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_DESCRIPTIONS
,
2788 &spa
->spa_feat_desc_obj
) != 0) {
2789 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2792 enabled_feat
= fnvlist_alloc();
2793 unsup_feat
= fnvlist_alloc();
2795 if (!spa_features_check(spa
, B_FALSE
,
2796 unsup_feat
, enabled_feat
))
2797 missing_feat_read
= B_TRUE
;
2799 if (spa_writeable(spa
) || state
== SPA_LOAD_TRYIMPORT
) {
2800 if (!spa_features_check(spa
, B_TRUE
,
2801 unsup_feat
, enabled_feat
)) {
2802 missing_feat_write
= B_TRUE
;
2806 fnvlist_add_nvlist(spa
->spa_load_info
,
2807 ZPOOL_CONFIG_ENABLED_FEAT
, enabled_feat
);
2809 if (!nvlist_empty(unsup_feat
)) {
2810 fnvlist_add_nvlist(spa
->spa_load_info
,
2811 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
);
2814 fnvlist_free(enabled_feat
);
2815 fnvlist_free(unsup_feat
);
2817 if (!missing_feat_read
) {
2818 fnvlist_add_boolean(spa
->spa_load_info
,
2819 ZPOOL_CONFIG_CAN_RDONLY
);
2823 * If the state is SPA_LOAD_TRYIMPORT, our objective is
2824 * twofold: to determine whether the pool is available for
2825 * import in read-write mode and (if it is not) whether the
2826 * pool is available for import in read-only mode. If the pool
2827 * is available for import in read-write mode, it is displayed
2828 * as available in userland; if it is not available for import
2829 * in read-only mode, it is displayed as unavailable in
2830 * userland. If the pool is available for import in read-only
2831 * mode but not read-write mode, it is displayed as unavailable
2832 * in userland with a special note that the pool is actually
2833 * available for open in read-only mode.
2835 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
2836 * missing a feature for write, we must first determine whether
2837 * the pool can be opened read-only before returning to
2838 * userland in order to know whether to display the
2839 * abovementioned note.
2841 if (missing_feat_read
|| (missing_feat_write
&&
2842 spa_writeable(spa
))) {
2843 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
2848 * Load refcounts for ZFS features from disk into an in-memory
2849 * cache during SPA initialization.
2851 for (spa_feature_t i
= 0; i
< SPA_FEATURES
; i
++) {
2854 error
= feature_get_refcount_from_disk(spa
,
2855 &spa_feature_table
[i
], &refcount
);
2857 spa
->spa_feat_refcount_cache
[i
] = refcount
;
2858 } else if (error
== ENOTSUP
) {
2859 spa
->spa_feat_refcount_cache
[i
] =
2860 SPA_FEATURE_DISABLED
;
2862 return (spa_vdev_err(rvd
,
2863 VDEV_AUX_CORRUPT_DATA
, EIO
));
2868 if (spa_feature_is_active(spa
, SPA_FEATURE_ENABLED_TXG
)) {
2869 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_ENABLED_TXG
,
2870 &spa
->spa_feat_enabled_txg_obj
) != 0)
2871 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2874 spa
->spa_is_initializing
= B_TRUE
;
2875 error
= dsl_pool_open(spa
->spa_dsl_pool
);
2876 spa
->spa_is_initializing
= B_FALSE
;
2878 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2882 nvlist_t
*policy
= NULL
, *nvconfig
;
2884 if (load_nvlist(spa
, spa
->spa_config_object
, &nvconfig
) != 0)
2885 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2887 if (!spa_is_root(spa
) && nvlist_lookup_uint64(nvconfig
,
2888 ZPOOL_CONFIG_HOSTID
, &hostid
) == 0) {
2890 unsigned long myhostid
= 0;
2892 VERIFY(nvlist_lookup_string(nvconfig
,
2893 ZPOOL_CONFIG_HOSTNAME
, &hostname
) == 0);
2895 myhostid
= spa_get_hostid();
2896 if (hostid
&& myhostid
&& hostid
!= myhostid
) {
2897 nvlist_free(nvconfig
);
2898 return (SET_ERROR(EBADF
));
2901 if (nvlist_lookup_nvlist(spa
->spa_config
,
2902 ZPOOL_REWIND_POLICY
, &policy
) == 0)
2903 VERIFY(nvlist_add_nvlist(nvconfig
,
2904 ZPOOL_REWIND_POLICY
, policy
) == 0);
2906 spa_config_set(spa
, nvconfig
);
2908 spa_deactivate(spa
);
2909 spa_activate(spa
, orig_mode
);
2911 return (spa_load(spa
, state
, SPA_IMPORT_EXISTING
, B_TRUE
));
2914 /* Grab the checksum salt from the MOS. */
2915 error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
2916 DMU_POOL_CHECKSUM_SALT
, 1,
2917 sizeof (spa
->spa_cksum_salt
.zcs_bytes
),
2918 spa
->spa_cksum_salt
.zcs_bytes
);
2919 if (error
== ENOENT
) {
2920 /* Generate a new salt for subsequent use */
2921 (void) random_get_pseudo_bytes(spa
->spa_cksum_salt
.zcs_bytes
,
2922 sizeof (spa
->spa_cksum_salt
.zcs_bytes
));
2923 } else if (error
!= 0) {
2924 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2927 if (spa_dir_prop(spa
, DMU_POOL_SYNC_BPOBJ
, &obj
) != 0)
2928 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2929 error
= bpobj_open(&spa
->spa_deferred_bpobj
, spa
->spa_meta_objset
, obj
);
2931 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2934 * Load the bit that tells us to use the new accounting function
2935 * (raid-z deflation). If we have an older pool, this will not
2938 error
= spa_dir_prop(spa
, DMU_POOL_DEFLATE
, &spa
->spa_deflate
);
2939 if (error
!= 0 && error
!= ENOENT
)
2940 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2942 error
= spa_dir_prop(spa
, DMU_POOL_CREATION_VERSION
,
2943 &spa
->spa_creation_version
);
2944 if (error
!= 0 && error
!= ENOENT
)
2945 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2948 * Load the persistent error log. If we have an older pool, this will
2951 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_LAST
, &spa
->spa_errlog_last
);
2952 if (error
!= 0 && error
!= ENOENT
)
2953 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2955 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_SCRUB
,
2956 &spa
->spa_errlog_scrub
);
2957 if (error
!= 0 && error
!= ENOENT
)
2958 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2961 * Load the history object. If we have an older pool, this
2962 * will not be present.
2964 error
= spa_dir_prop(spa
, DMU_POOL_HISTORY
, &spa
->spa_history
);
2965 if (error
!= 0 && error
!= ENOENT
)
2966 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2969 * Load the per-vdev ZAP map. If we have an older pool, this will not
2970 * be present; in this case, defer its creation to a later time to
2971 * avoid dirtying the MOS this early / out of sync context. See
2972 * spa_sync_config_object.
2975 /* The sentinel is only available in the MOS config. */
2976 nvlist_t
*mos_config
;
2977 if (load_nvlist(spa
, spa
->spa_config_object
, &mos_config
) != 0)
2978 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2980 error
= spa_dir_prop(spa
, DMU_POOL_VDEV_ZAP_MAP
,
2981 &spa
->spa_all_vdev_zaps
);
2983 if (error
== ENOENT
) {
2984 VERIFY(!nvlist_exists(mos_config
,
2985 ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
));
2986 spa
->spa_avz_action
= AVZ_ACTION_INITIALIZE
;
2987 ASSERT0(vdev_count_verify_zaps(spa
->spa_root_vdev
));
2988 } else if (error
!= 0) {
2989 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2990 } else if (!nvlist_exists(mos_config
, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
)) {
2992 * An older version of ZFS overwrote the sentinel value, so
2993 * we have orphaned per-vdev ZAPs in the MOS. Defer their
2994 * destruction to later; see spa_sync_config_object.
2996 spa
->spa_avz_action
= AVZ_ACTION_DESTROY
;
2998 * We're assuming that no vdevs have had their ZAPs created
2999 * before this. Better be sure of it.
3001 ASSERT0(vdev_count_verify_zaps(spa
->spa_root_vdev
));
3003 nvlist_free(mos_config
);
3006 * If we're assembling the pool from the split-off vdevs of
3007 * an existing pool, we don't want to attach the spares & cache
3012 * Load any hot spares for this pool.
3014 error
= spa_dir_prop(spa
, DMU_POOL_SPARES
, &spa
->spa_spares
.sav_object
);
3015 if (error
!= 0 && error
!= ENOENT
)
3016 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3017 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
3018 ASSERT(spa_version(spa
) >= SPA_VERSION_SPARES
);
3019 if (load_nvlist(spa
, spa
->spa_spares
.sav_object
,
3020 &spa
->spa_spares
.sav_config
) != 0)
3021 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3023 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3024 spa_load_spares(spa
);
3025 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3026 } else if (error
== 0) {
3027 spa
->spa_spares
.sav_sync
= B_TRUE
;
3031 * Load any level 2 ARC devices for this pool.
3033 error
= spa_dir_prop(spa
, DMU_POOL_L2CACHE
,
3034 &spa
->spa_l2cache
.sav_object
);
3035 if (error
!= 0 && error
!= ENOENT
)
3036 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3037 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
3038 ASSERT(spa_version(spa
) >= SPA_VERSION_L2CACHE
);
3039 if (load_nvlist(spa
, spa
->spa_l2cache
.sav_object
,
3040 &spa
->spa_l2cache
.sav_config
) != 0)
3041 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3043 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3044 spa_load_l2cache(spa
);
3045 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3046 } else if (error
== 0) {
3047 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
3050 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
3052 error
= spa_dir_prop(spa
, DMU_POOL_PROPS
, &spa
->spa_pool_props_object
);
3053 if (error
&& error
!= ENOENT
)
3054 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3057 uint64_t autoreplace
= 0;
3059 spa_prop_find(spa
, ZPOOL_PROP_BOOTFS
, &spa
->spa_bootfs
);
3060 spa_prop_find(spa
, ZPOOL_PROP_AUTOREPLACE
, &autoreplace
);
3061 spa_prop_find(spa
, ZPOOL_PROP_DELEGATION
, &spa
->spa_delegation
);
3062 spa_prop_find(spa
, ZPOOL_PROP_FAILUREMODE
, &spa
->spa_failmode
);
3063 spa_prop_find(spa
, ZPOOL_PROP_AUTOEXPAND
, &spa
->spa_autoexpand
);
3064 spa_prop_find(spa
, ZPOOL_PROP_MULTIHOST
, &spa
->spa_multihost
);
3065 spa_prop_find(spa
, ZPOOL_PROP_DEDUPDITTO
,
3066 &spa
->spa_dedup_ditto
);
3068 spa
->spa_autoreplace
= (autoreplace
!= 0);
3072 * If the 'multihost' property is set, then never allow a pool to
3073 * be imported when the system hostid is zero. The exception to
3074 * this rule is zdb which is always allowed to access pools.
3076 if (spa_multihost(spa
) && spa_get_hostid() == 0 &&
3077 (spa
->spa_import_flags
& ZFS_IMPORT_SKIP_MMP
) == 0) {
3078 fnvlist_add_uint64(spa
->spa_load_info
,
3079 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_NO_HOSTID
);
3080 return (spa_vdev_err(rvd
, VDEV_AUX_ACTIVE
, EREMOTEIO
));
3084 * If the 'autoreplace' property is set, then post a resource notifying
3085 * the ZFS DE that it should not issue any faults for unopenable
3086 * devices. We also iterate over the vdevs, and post a sysevent for any
3087 * unopenable vdevs so that the normal autoreplace handler can take
3090 if (spa
->spa_autoreplace
&& state
!= SPA_LOAD_TRYIMPORT
) {
3091 spa_check_removed(spa
->spa_root_vdev
);
3093 * For the import case, this is done in spa_import(), because
3094 * at this point we're using the spare definitions from
3095 * the MOS config, not necessarily from the userland config.
3097 if (state
!= SPA_LOAD_IMPORT
) {
3098 spa_aux_check_removed(&spa
->spa_spares
);
3099 spa_aux_check_removed(&spa
->spa_l2cache
);
3104 * Load the vdev state for all toplevel vdevs.
3109 * Propagate the leaf DTLs we just loaded all the way up the tree.
3111 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3112 vdev_dtl_reassess(rvd
, 0, 0, B_FALSE
);
3113 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3116 * Load the DDTs (dedup tables).
3118 error
= ddt_load(spa
);
3120 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3122 spa_update_dspace(spa
);
3125 * Validate the config, using the MOS config to fill in any
3126 * information which might be missing. If we fail to validate
3127 * the config then declare the pool unfit for use. If we're
3128 * assembling a pool from a split, the log is not transferred
3131 if (type
!= SPA_IMPORT_ASSEMBLE
) {
3134 if (load_nvlist(spa
, spa
->spa_config_object
, &nvconfig
) != 0)
3135 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3137 if (!spa_config_valid(spa
, nvconfig
)) {
3138 nvlist_free(nvconfig
);
3139 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
,
3142 nvlist_free(nvconfig
);
3145 * Now that we've validated the config, check the state of the
3146 * root vdev. If it can't be opened, it indicates one or
3147 * more toplevel vdevs are faulted.
3149 if (rvd
->vdev_state
<= VDEV_STATE_CANT_OPEN
)
3150 return (SET_ERROR(ENXIO
));
3152 if (spa_writeable(spa
) && spa_check_logs(spa
)) {
3153 *ereport
= FM_EREPORT_ZFS_LOG_REPLAY
;
3154 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_LOG
, ENXIO
));
3158 if (missing_feat_write
) {
3159 ASSERT(state
== SPA_LOAD_TRYIMPORT
);
3162 * At this point, we know that we can open the pool in
3163 * read-only mode but not read-write mode. We now have enough
3164 * information and can return to userland.
3166 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
, ENOTSUP
));
3170 * We've successfully opened the pool, verify that we're ready
3171 * to start pushing transactions.
3173 if (state
!= SPA_LOAD_TRYIMPORT
) {
3174 if ((error
= spa_load_verify(spa
)))
3175 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
3179 if (spa_writeable(spa
) && (state
== SPA_LOAD_RECOVER
||
3180 spa
->spa_load_max_txg
== UINT64_MAX
)) {
3182 int need_update
= B_FALSE
;
3183 dsl_pool_t
*dp
= spa_get_dsl(spa
);
3185 ASSERT(state
!= SPA_LOAD_TRYIMPORT
);
3188 * Claim log blocks that haven't been committed yet.
3189 * This must all happen in a single txg.
3190 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
3191 * invoked from zil_claim_log_block()'s i/o done callback.
3192 * Price of rollback is that we abandon the log.
3194 spa
->spa_claiming
= B_TRUE
;
3196 tx
= dmu_tx_create_assigned(dp
, spa_first_txg(spa
));
3197 (void) dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
3198 zil_claim
, tx
, DS_FIND_CHILDREN
);
3201 spa
->spa_claiming
= B_FALSE
;
3203 spa_set_log_state(spa
, SPA_LOG_GOOD
);
3204 spa
->spa_sync_on
= B_TRUE
;
3205 txg_sync_start(spa
->spa_dsl_pool
);
3206 mmp_thread_start(spa
);
3209 * Wait for all claims to sync. We sync up to the highest
3210 * claimed log block birth time so that claimed log blocks
3211 * don't appear to be from the future. spa_claim_max_txg
3212 * will have been set for us by either zil_check_log_chain()
3213 * (invoked from spa_check_logs()) or zil_claim() above.
3215 txg_wait_synced(spa
->spa_dsl_pool
, spa
->spa_claim_max_txg
);
3218 * If the config cache is stale, or we have uninitialized
3219 * metaslabs (see spa_vdev_add()), then update the config.
3221 * If this is a verbatim import, trust the current
3222 * in-core spa_config and update the disk labels.
3224 if (config_cache_txg
!= spa
->spa_config_txg
||
3225 state
== SPA_LOAD_IMPORT
||
3226 state
== SPA_LOAD_RECOVER
||
3227 (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
))
3228 need_update
= B_TRUE
;
3230 for (int c
= 0; c
< rvd
->vdev_children
; c
++)
3231 if (rvd
->vdev_child
[c
]->vdev_ms_array
== 0)
3232 need_update
= B_TRUE
;
3235 * Update the config cache asychronously in case we're the
3236 * root pool, in which case the config cache isn't writable yet.
3239 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
3242 * Check all DTLs to see if anything needs resilvering.
3244 if (!dsl_scan_resilvering(spa
->spa_dsl_pool
) &&
3245 vdev_resilver_needed(rvd
, NULL
, NULL
))
3246 spa_async_request(spa
, SPA_ASYNC_RESILVER
);
3249 * Log the fact that we booted up (so that we can detect if
3250 * we rebooted in the middle of an operation).
3252 spa_history_log_version(spa
, "open", NULL
);
3255 * Delete any inconsistent datasets.
3257 (void) dmu_objset_find(spa_name(spa
),
3258 dsl_destroy_inconsistent
, NULL
, DS_FIND_CHILDREN
);
3261 * Clean up any stale temporary dataset userrefs.
3263 dsl_pool_clean_tmp_userrefs(spa
->spa_dsl_pool
);
3270 spa_load_retry(spa_t
*spa
, spa_load_state_t state
, int mosconfig
)
3272 int mode
= spa
->spa_mode
;
3275 spa_deactivate(spa
);
3277 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
- 1;
3279 spa_activate(spa
, mode
);
3280 spa_async_suspend(spa
);
3282 return (spa_load(spa
, state
, SPA_IMPORT_EXISTING
, mosconfig
));
3286 * If spa_load() fails this function will try loading prior txg's. If
3287 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
3288 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
3289 * function will not rewind the pool and will return the same error as
3293 spa_load_best(spa_t
*spa
, spa_load_state_t state
, int mosconfig
,
3294 uint64_t max_request
, int rewind_flags
)
3296 nvlist_t
*loadinfo
= NULL
;
3297 nvlist_t
*config
= NULL
;
3298 int load_error
, rewind_error
;
3299 uint64_t safe_rewind_txg
;
3302 if (spa
->spa_load_txg
&& state
== SPA_LOAD_RECOVER
) {
3303 spa
->spa_load_max_txg
= spa
->spa_load_txg
;
3304 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
3306 spa
->spa_load_max_txg
= max_request
;
3307 if (max_request
!= UINT64_MAX
)
3308 spa
->spa_extreme_rewind
= B_TRUE
;
3311 load_error
= rewind_error
= spa_load(spa
, state
, SPA_IMPORT_EXISTING
,
3313 if (load_error
== 0)
3316 if (spa
->spa_root_vdev
!= NULL
)
3317 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
3319 spa
->spa_last_ubsync_txg
= spa
->spa_uberblock
.ub_txg
;
3320 spa
->spa_last_ubsync_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
3322 if (rewind_flags
& ZPOOL_NEVER_REWIND
) {
3323 nvlist_free(config
);
3324 return (load_error
);
3327 if (state
== SPA_LOAD_RECOVER
) {
3328 /* Price of rolling back is discarding txgs, including log */
3329 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
3332 * If we aren't rolling back save the load info from our first
3333 * import attempt so that we can restore it after attempting
3336 loadinfo
= spa
->spa_load_info
;
3337 spa
->spa_load_info
= fnvlist_alloc();
3340 spa
->spa_load_max_txg
= spa
->spa_last_ubsync_txg
;
3341 safe_rewind_txg
= spa
->spa_last_ubsync_txg
- TXG_DEFER_SIZE
;
3342 min_txg
= (rewind_flags
& ZPOOL_EXTREME_REWIND
) ?
3343 TXG_INITIAL
: safe_rewind_txg
;
3346 * Continue as long as we're finding errors, we're still within
3347 * the acceptable rewind range, and we're still finding uberblocks
3349 while (rewind_error
&& spa
->spa_uberblock
.ub_txg
>= min_txg
&&
3350 spa
->spa_uberblock
.ub_txg
<= spa
->spa_load_max_txg
) {
3351 if (spa
->spa_load_max_txg
< safe_rewind_txg
)
3352 spa
->spa_extreme_rewind
= B_TRUE
;
3353 rewind_error
= spa_load_retry(spa
, state
, mosconfig
);
3356 spa
->spa_extreme_rewind
= B_FALSE
;
3357 spa
->spa_load_max_txg
= UINT64_MAX
;
3359 if (config
&& (rewind_error
|| state
!= SPA_LOAD_RECOVER
))
3360 spa_config_set(spa
, config
);
3362 nvlist_free(config
);
3364 if (state
== SPA_LOAD_RECOVER
) {
3365 ASSERT3P(loadinfo
, ==, NULL
);
3366 return (rewind_error
);
3368 /* Store the rewind info as part of the initial load info */
3369 fnvlist_add_nvlist(loadinfo
, ZPOOL_CONFIG_REWIND_INFO
,
3370 spa
->spa_load_info
);
3372 /* Restore the initial load info */
3373 fnvlist_free(spa
->spa_load_info
);
3374 spa
->spa_load_info
= loadinfo
;
3376 return (load_error
);
3383 * The import case is identical to an open except that the configuration is sent
3384 * down from userland, instead of grabbed from the configuration cache. For the
3385 * case of an open, the pool configuration will exist in the
3386 * POOL_STATE_UNINITIALIZED state.
3388 * The stats information (gen/count/ustats) is used to gather vdev statistics at
3389 * the same time open the pool, without having to keep around the spa_t in some
3393 spa_open_common(const char *pool
, spa_t
**spapp
, void *tag
, nvlist_t
*nvpolicy
,
3397 spa_load_state_t state
= SPA_LOAD_OPEN
;
3399 int locked
= B_FALSE
;
3400 int firstopen
= B_FALSE
;
3405 * As disgusting as this is, we need to support recursive calls to this
3406 * function because dsl_dir_open() is called during spa_load(), and ends
3407 * up calling spa_open() again. The real fix is to figure out how to
3408 * avoid dsl_dir_open() calling this in the first place.
3410 if (MUTEX_NOT_HELD(&spa_namespace_lock
)) {
3411 mutex_enter(&spa_namespace_lock
);
3415 if ((spa
= spa_lookup(pool
)) == NULL
) {
3417 mutex_exit(&spa_namespace_lock
);
3418 return (SET_ERROR(ENOENT
));
3421 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
) {
3422 zpool_rewind_policy_t policy
;
3426 zpool_get_rewind_policy(nvpolicy
? nvpolicy
: spa
->spa_config
,
3428 if (policy
.zrp_request
& ZPOOL_DO_REWIND
)
3429 state
= SPA_LOAD_RECOVER
;
3431 spa_activate(spa
, spa_mode_global
);
3433 if (state
!= SPA_LOAD_RECOVER
)
3434 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
3436 error
= spa_load_best(spa
, state
, B_FALSE
, policy
.zrp_txg
,
3437 policy
.zrp_request
);
3439 if (error
== EBADF
) {
3441 * If vdev_validate() returns failure (indicated by
3442 * EBADF), it indicates that one of the vdevs indicates
3443 * that the pool has been exported or destroyed. If
3444 * this is the case, the config cache is out of sync and
3445 * we should remove the pool from the namespace.
3448 spa_deactivate(spa
);
3449 spa_config_sync(spa
, B_TRUE
, B_TRUE
);
3452 mutex_exit(&spa_namespace_lock
);
3453 return (SET_ERROR(ENOENT
));
3458 * We can't open the pool, but we still have useful
3459 * information: the state of each vdev after the
3460 * attempted vdev_open(). Return this to the user.
3462 if (config
!= NULL
&& spa
->spa_config
) {
3463 VERIFY(nvlist_dup(spa
->spa_config
, config
,
3465 VERIFY(nvlist_add_nvlist(*config
,
3466 ZPOOL_CONFIG_LOAD_INFO
,
3467 spa
->spa_load_info
) == 0);
3470 spa_deactivate(spa
);
3471 spa
->spa_last_open_failed
= error
;
3473 mutex_exit(&spa_namespace_lock
);
3479 spa_open_ref(spa
, tag
);
3482 *config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
3485 * If we've recovered the pool, pass back any information we
3486 * gathered while doing the load.
3488 if (state
== SPA_LOAD_RECOVER
) {
3489 VERIFY(nvlist_add_nvlist(*config
, ZPOOL_CONFIG_LOAD_INFO
,
3490 spa
->spa_load_info
) == 0);
3494 spa
->spa_last_open_failed
= 0;
3495 spa
->spa_last_ubsync_txg
= 0;
3496 spa
->spa_load_txg
= 0;
3497 mutex_exit(&spa_namespace_lock
);
3501 zvol_create_minors(spa
, spa_name(spa
), B_TRUE
);
3509 spa_open_rewind(const char *name
, spa_t
**spapp
, void *tag
, nvlist_t
*policy
,
3512 return (spa_open_common(name
, spapp
, tag
, policy
, config
));
3516 spa_open(const char *name
, spa_t
**spapp
, void *tag
)
3518 return (spa_open_common(name
, spapp
, tag
, NULL
, NULL
));
3522 * Lookup the given spa_t, incrementing the inject count in the process,
3523 * preventing it from being exported or destroyed.
3526 spa_inject_addref(char *name
)
3530 mutex_enter(&spa_namespace_lock
);
3531 if ((spa
= spa_lookup(name
)) == NULL
) {
3532 mutex_exit(&spa_namespace_lock
);
3535 spa
->spa_inject_ref
++;
3536 mutex_exit(&spa_namespace_lock
);
3542 spa_inject_delref(spa_t
*spa
)
3544 mutex_enter(&spa_namespace_lock
);
3545 spa
->spa_inject_ref
--;
3546 mutex_exit(&spa_namespace_lock
);
3550 * Add spares device information to the nvlist.
3553 spa_add_spares(spa_t
*spa
, nvlist_t
*config
)
3563 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
3565 if (spa
->spa_spares
.sav_count
== 0)
3568 VERIFY(nvlist_lookup_nvlist(config
,
3569 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
3570 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
3571 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
3573 VERIFY(nvlist_add_nvlist_array(nvroot
,
3574 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
3575 VERIFY(nvlist_lookup_nvlist_array(nvroot
,
3576 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
3579 * Go through and find any spares which have since been
3580 * repurposed as an active spare. If this is the case, update
3581 * their status appropriately.
3583 for (i
= 0; i
< nspares
; i
++) {
3584 VERIFY(nvlist_lookup_uint64(spares
[i
],
3585 ZPOOL_CONFIG_GUID
, &guid
) == 0);
3586 if (spa_spare_exists(guid
, &pool
, NULL
) &&
3588 VERIFY(nvlist_lookup_uint64_array(
3589 spares
[i
], ZPOOL_CONFIG_VDEV_STATS
,
3590 (uint64_t **)&vs
, &vsc
) == 0);
3591 vs
->vs_state
= VDEV_STATE_CANT_OPEN
;
3592 vs
->vs_aux
= VDEV_AUX_SPARED
;
3599 * Add l2cache device information to the nvlist, including vdev stats.
3602 spa_add_l2cache(spa_t
*spa
, nvlist_t
*config
)
3605 uint_t i
, j
, nl2cache
;
3612 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
3614 if (spa
->spa_l2cache
.sav_count
== 0)
3617 VERIFY(nvlist_lookup_nvlist(config
,
3618 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
3619 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_l2cache
.sav_config
,
3620 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
3621 if (nl2cache
!= 0) {
3622 VERIFY(nvlist_add_nvlist_array(nvroot
,
3623 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
3624 VERIFY(nvlist_lookup_nvlist_array(nvroot
,
3625 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
3628 * Update level 2 cache device stats.
3631 for (i
= 0; i
< nl2cache
; i
++) {
3632 VERIFY(nvlist_lookup_uint64(l2cache
[i
],
3633 ZPOOL_CONFIG_GUID
, &guid
) == 0);
3636 for (j
= 0; j
< spa
->spa_l2cache
.sav_count
; j
++) {
3638 spa
->spa_l2cache
.sav_vdevs
[j
]->vdev_guid
) {
3639 vd
= spa
->spa_l2cache
.sav_vdevs
[j
];
3645 VERIFY(nvlist_lookup_uint64_array(l2cache
[i
],
3646 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
, &vsc
)
3648 vdev_get_stats(vd
, vs
);
3649 vdev_config_generate_stats(vd
, l2cache
[i
]);
3656 spa_feature_stats_from_disk(spa_t
*spa
, nvlist_t
*features
)
3661 if (spa
->spa_feat_for_read_obj
!= 0) {
3662 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
3663 spa
->spa_feat_for_read_obj
);
3664 zap_cursor_retrieve(&zc
, &za
) == 0;
3665 zap_cursor_advance(&zc
)) {
3666 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
3667 za
.za_num_integers
== 1);
3668 VERIFY0(nvlist_add_uint64(features
, za
.za_name
,
3669 za
.za_first_integer
));
3671 zap_cursor_fini(&zc
);
3674 if (spa
->spa_feat_for_write_obj
!= 0) {
3675 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
3676 spa
->spa_feat_for_write_obj
);
3677 zap_cursor_retrieve(&zc
, &za
) == 0;
3678 zap_cursor_advance(&zc
)) {
3679 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
3680 za
.za_num_integers
== 1);
3681 VERIFY0(nvlist_add_uint64(features
, za
.za_name
,
3682 za
.za_first_integer
));
3684 zap_cursor_fini(&zc
);
3689 spa_feature_stats_from_cache(spa_t
*spa
, nvlist_t
*features
)
3693 for (i
= 0; i
< SPA_FEATURES
; i
++) {
3694 zfeature_info_t feature
= spa_feature_table
[i
];
3697 if (feature_get_refcount(spa
, &feature
, &refcount
) != 0)
3700 VERIFY0(nvlist_add_uint64(features
, feature
.fi_guid
, refcount
));
3705 * Store a list of pool features and their reference counts in the
3708 * The first time this is called on a spa, allocate a new nvlist, fetch
3709 * the pool features and reference counts from disk, then save the list
3710 * in the spa. In subsequent calls on the same spa use the saved nvlist
3711 * and refresh its values from the cached reference counts. This
3712 * ensures we don't block here on I/O on a suspended pool so 'zpool
3713 * clear' can resume the pool.
3716 spa_add_feature_stats(spa_t
*spa
, nvlist_t
*config
)
3720 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
3722 mutex_enter(&spa
->spa_feat_stats_lock
);
3723 features
= spa
->spa_feat_stats
;
3725 if (features
!= NULL
) {
3726 spa_feature_stats_from_cache(spa
, features
);
3728 VERIFY0(nvlist_alloc(&features
, NV_UNIQUE_NAME
, KM_SLEEP
));
3729 spa
->spa_feat_stats
= features
;
3730 spa_feature_stats_from_disk(spa
, features
);
3733 VERIFY0(nvlist_add_nvlist(config
, ZPOOL_CONFIG_FEATURE_STATS
,
3736 mutex_exit(&spa
->spa_feat_stats_lock
);
3740 spa_get_stats(const char *name
, nvlist_t
**config
,
3741 char *altroot
, size_t buflen
)
3747 error
= spa_open_common(name
, &spa
, FTAG
, NULL
, config
);
3751 * This still leaves a window of inconsistency where the spares
3752 * or l2cache devices could change and the config would be
3753 * self-inconsistent.
3755 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
3757 if (*config
!= NULL
) {
3758 uint64_t loadtimes
[2];
3760 loadtimes
[0] = spa
->spa_loaded_ts
.tv_sec
;
3761 loadtimes
[1] = spa
->spa_loaded_ts
.tv_nsec
;
3762 VERIFY(nvlist_add_uint64_array(*config
,
3763 ZPOOL_CONFIG_LOADED_TIME
, loadtimes
, 2) == 0);
3765 VERIFY(nvlist_add_uint64(*config
,
3766 ZPOOL_CONFIG_ERRCOUNT
,
3767 spa_get_errlog_size(spa
)) == 0);
3769 if (spa_suspended(spa
))
3770 VERIFY(nvlist_add_uint64(*config
,
3771 ZPOOL_CONFIG_SUSPENDED
,
3772 spa
->spa_failmode
) == 0);
3774 spa_add_spares(spa
, *config
);
3775 spa_add_l2cache(spa
, *config
);
3776 spa_add_feature_stats(spa
, *config
);
3781 * We want to get the alternate root even for faulted pools, so we cheat
3782 * and call spa_lookup() directly.
3786 mutex_enter(&spa_namespace_lock
);
3787 spa
= spa_lookup(name
);
3789 spa_altroot(spa
, altroot
, buflen
);
3793 mutex_exit(&spa_namespace_lock
);
3795 spa_altroot(spa
, altroot
, buflen
);
3800 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
3801 spa_close(spa
, FTAG
);
3808 * Validate that the auxiliary device array is well formed. We must have an
3809 * array of nvlists, each which describes a valid leaf vdev. If this is an
3810 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
3811 * specified, as long as they are well-formed.
3814 spa_validate_aux_devs(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
,
3815 spa_aux_vdev_t
*sav
, const char *config
, uint64_t version
,
3816 vdev_labeltype_t label
)
3823 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
3826 * It's acceptable to have no devs specified.
3828 if (nvlist_lookup_nvlist_array(nvroot
, config
, &dev
, &ndev
) != 0)
3832 return (SET_ERROR(EINVAL
));
3835 * Make sure the pool is formatted with a version that supports this
3838 if (spa_version(spa
) < version
)
3839 return (SET_ERROR(ENOTSUP
));
3842 * Set the pending device list so we correctly handle device in-use
3845 sav
->sav_pending
= dev
;
3846 sav
->sav_npending
= ndev
;
3848 for (i
= 0; i
< ndev
; i
++) {
3849 if ((error
= spa_config_parse(spa
, &vd
, dev
[i
], NULL
, 0,
3853 if (!vd
->vdev_ops
->vdev_op_leaf
) {
3855 error
= SET_ERROR(EINVAL
);
3861 if ((error
= vdev_open(vd
)) == 0 &&
3862 (error
= vdev_label_init(vd
, crtxg
, label
)) == 0) {
3863 VERIFY(nvlist_add_uint64(dev
[i
], ZPOOL_CONFIG_GUID
,
3864 vd
->vdev_guid
) == 0);
3870 (mode
!= VDEV_ALLOC_SPARE
&& mode
!= VDEV_ALLOC_L2CACHE
))
3877 sav
->sav_pending
= NULL
;
3878 sav
->sav_npending
= 0;
3883 spa_validate_aux(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
)
3887 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
3889 if ((error
= spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
3890 &spa
->spa_spares
, ZPOOL_CONFIG_SPARES
, SPA_VERSION_SPARES
,
3891 VDEV_LABEL_SPARE
)) != 0) {
3895 return (spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
3896 &spa
->spa_l2cache
, ZPOOL_CONFIG_L2CACHE
, SPA_VERSION_L2CACHE
,
3897 VDEV_LABEL_L2CACHE
));
3901 spa_set_aux_vdevs(spa_aux_vdev_t
*sav
, nvlist_t
**devs
, int ndevs
,
3906 if (sav
->sav_config
!= NULL
) {
3912 * Generate new dev list by concatenating with the
3915 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
, config
,
3916 &olddevs
, &oldndevs
) == 0);
3918 newdevs
= kmem_alloc(sizeof (void *) *
3919 (ndevs
+ oldndevs
), KM_SLEEP
);
3920 for (i
= 0; i
< oldndevs
; i
++)
3921 VERIFY(nvlist_dup(olddevs
[i
], &newdevs
[i
],
3923 for (i
= 0; i
< ndevs
; i
++)
3924 VERIFY(nvlist_dup(devs
[i
], &newdevs
[i
+ oldndevs
],
3927 VERIFY(nvlist_remove(sav
->sav_config
, config
,
3928 DATA_TYPE_NVLIST_ARRAY
) == 0);
3930 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
,
3931 config
, newdevs
, ndevs
+ oldndevs
) == 0);
3932 for (i
= 0; i
< oldndevs
+ ndevs
; i
++)
3933 nvlist_free(newdevs
[i
]);
3934 kmem_free(newdevs
, (oldndevs
+ ndevs
) * sizeof (void *));
3937 * Generate a new dev list.
3939 VERIFY(nvlist_alloc(&sav
->sav_config
, NV_UNIQUE_NAME
,
3941 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
, config
,
3947 * Stop and drop level 2 ARC devices
3950 spa_l2cache_drop(spa_t
*spa
)
3954 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
3956 for (i
= 0; i
< sav
->sav_count
; i
++) {
3959 vd
= sav
->sav_vdevs
[i
];
3962 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
3963 pool
!= 0ULL && l2arc_vdev_present(vd
))
3964 l2arc_remove_vdev(vd
);
3969 * Verify encryption parameters for spa creation. If we are encrypting, we must
3970 * have the encryption feature flag enabled.
3973 spa_create_check_encryption_params(dsl_crypto_params_t
*dcp
,
3974 boolean_t has_encryption
)
3976 if (dcp
->cp_crypt
!= ZIO_CRYPT_OFF
&&
3977 dcp
->cp_crypt
!= ZIO_CRYPT_INHERIT
&&
3979 return (SET_ERROR(ENOTSUP
));
3981 return (dmu_objset_create_crypt_check(NULL
, dcp
));
3988 spa_create(const char *pool
, nvlist_t
*nvroot
, nvlist_t
*props
,
3989 nvlist_t
*zplprops
, dsl_crypto_params_t
*dcp
)
3992 char *altroot
= NULL
;
3997 uint64_t txg
= TXG_INITIAL
;
3998 nvlist_t
**spares
, **l2cache
;
3999 uint_t nspares
, nl2cache
;
4000 uint64_t version
, obj
, root_dsobj
= 0;
4001 boolean_t has_features
;
4002 boolean_t has_encryption
;
4008 if (nvlist_lookup_string(props
, "tname", &poolname
) != 0)
4009 poolname
= (char *)pool
;
4012 * If this pool already exists, return failure.
4014 mutex_enter(&spa_namespace_lock
);
4015 if (spa_lookup(poolname
) != NULL
) {
4016 mutex_exit(&spa_namespace_lock
);
4017 return (SET_ERROR(EEXIST
));
4021 * Allocate a new spa_t structure.
4023 nvl
= fnvlist_alloc();
4024 fnvlist_add_string(nvl
, ZPOOL_CONFIG_POOL_NAME
, pool
);
4025 (void) nvlist_lookup_string(props
,
4026 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
4027 spa
= spa_add(poolname
, nvl
, altroot
);
4029 spa_activate(spa
, spa_mode_global
);
4031 if (props
&& (error
= spa_prop_validate(spa
, props
))) {
4032 spa_deactivate(spa
);
4034 mutex_exit(&spa_namespace_lock
);
4039 * Temporary pool names should never be written to disk.
4041 if (poolname
!= pool
)
4042 spa
->spa_import_flags
|= ZFS_IMPORT_TEMP_NAME
;
4044 has_features
= B_FALSE
;
4045 has_encryption
= B_FALSE
;
4046 for (nvpair_t
*elem
= nvlist_next_nvpair(props
, NULL
);
4047 elem
!= NULL
; elem
= nvlist_next_nvpair(props
, elem
)) {
4048 if (zpool_prop_feature(nvpair_name(elem
))) {
4049 has_features
= B_TRUE
;
4051 feat_name
= strchr(nvpair_name(elem
), '@') + 1;
4052 VERIFY0(zfeature_lookup_name(feat_name
, &feat
));
4053 if (feat
== SPA_FEATURE_ENCRYPTION
)
4054 has_encryption
= B_TRUE
;
4058 /* verify encryption params, if they were provided */
4060 error
= spa_create_check_encryption_params(dcp
, has_encryption
);
4062 spa_deactivate(spa
);
4064 mutex_exit(&spa_namespace_lock
);
4069 if (has_features
|| nvlist_lookup_uint64(props
,
4070 zpool_prop_to_name(ZPOOL_PROP_VERSION
), &version
) != 0) {
4071 version
= SPA_VERSION
;
4073 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
4075 spa
->spa_first_txg
= txg
;
4076 spa
->spa_uberblock
.ub_txg
= txg
- 1;
4077 spa
->spa_uberblock
.ub_version
= version
;
4078 spa
->spa_ubsync
= spa
->spa_uberblock
;
4079 spa
->spa_load_state
= SPA_LOAD_CREATE
;
4082 * Create "The Godfather" zio to hold all async IOs
4084 spa
->spa_async_zio_root
= kmem_alloc(max_ncpus
* sizeof (void *),
4086 for (int i
= 0; i
< max_ncpus
; i
++) {
4087 spa
->spa_async_zio_root
[i
] = zio_root(spa
, NULL
, NULL
,
4088 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
4089 ZIO_FLAG_GODFATHER
);
4093 * Create the root vdev.
4095 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4097 error
= spa_config_parse(spa
, &rvd
, nvroot
, NULL
, 0, VDEV_ALLOC_ADD
);
4099 ASSERT(error
!= 0 || rvd
!= NULL
);
4100 ASSERT(error
!= 0 || spa
->spa_root_vdev
== rvd
);
4102 if (error
== 0 && !zfs_allocatable_devs(nvroot
))
4103 error
= SET_ERROR(EINVAL
);
4106 (error
= vdev_create(rvd
, txg
, B_FALSE
)) == 0 &&
4107 (error
= spa_validate_aux(spa
, nvroot
, txg
,
4108 VDEV_ALLOC_ADD
)) == 0) {
4109 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
4110 vdev_metaslab_set_size(rvd
->vdev_child
[c
]);
4111 vdev_expand(rvd
->vdev_child
[c
], txg
);
4115 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4119 spa_deactivate(spa
);
4121 mutex_exit(&spa_namespace_lock
);
4126 * Get the list of spares, if specified.
4128 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
4129 &spares
, &nspares
) == 0) {
4130 VERIFY(nvlist_alloc(&spa
->spa_spares
.sav_config
, NV_UNIQUE_NAME
,
4132 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
4133 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
4134 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4135 spa_load_spares(spa
);
4136 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4137 spa
->spa_spares
.sav_sync
= B_TRUE
;
4141 * Get the list of level 2 cache devices, if specified.
4143 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
4144 &l2cache
, &nl2cache
) == 0) {
4145 VERIFY(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
4146 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
4147 VERIFY(nvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
4148 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
4149 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4150 spa_load_l2cache(spa
);
4151 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4152 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
4155 spa
->spa_is_initializing
= B_TRUE
;
4156 spa
->spa_dsl_pool
= dp
= dsl_pool_create(spa
, zplprops
, dcp
, txg
);
4157 spa
->spa_is_initializing
= B_FALSE
;
4160 * Create DDTs (dedup tables).
4164 spa_update_dspace(spa
);
4166 tx
= dmu_tx_create_assigned(dp
, txg
);
4169 * Create the pool's history object.
4171 if (version
>= SPA_VERSION_ZPOOL_HISTORY
&& !spa
->spa_history
)
4172 spa_history_create_obj(spa
, tx
);
4174 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_CREATE
);
4175 spa_history_log_version(spa
, "create", tx
);
4178 * Create the pool config object.
4180 spa
->spa_config_object
= dmu_object_alloc(spa
->spa_meta_objset
,
4181 DMU_OT_PACKED_NVLIST
, SPA_CONFIG_BLOCKSIZE
,
4182 DMU_OT_PACKED_NVLIST_SIZE
, sizeof (uint64_t), tx
);
4184 if (zap_add(spa
->spa_meta_objset
,
4185 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CONFIG
,
4186 sizeof (uint64_t), 1, &spa
->spa_config_object
, tx
) != 0) {
4187 cmn_err(CE_PANIC
, "failed to add pool config");
4190 if (zap_add(spa
->spa_meta_objset
,
4191 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CREATION_VERSION
,
4192 sizeof (uint64_t), 1, &version
, tx
) != 0) {
4193 cmn_err(CE_PANIC
, "failed to add pool version");
4196 /* Newly created pools with the right version are always deflated. */
4197 if (version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
4198 spa
->spa_deflate
= TRUE
;
4199 if (zap_add(spa
->spa_meta_objset
,
4200 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
4201 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
) != 0) {
4202 cmn_err(CE_PANIC
, "failed to add deflate");
4207 * Create the deferred-free bpobj. Turn off compression
4208 * because sync-to-convergence takes longer if the blocksize
4211 obj
= bpobj_alloc(spa
->spa_meta_objset
, 1 << 14, tx
);
4212 dmu_object_set_compress(spa
->spa_meta_objset
, obj
,
4213 ZIO_COMPRESS_OFF
, tx
);
4214 if (zap_add(spa
->spa_meta_objset
,
4215 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_SYNC_BPOBJ
,
4216 sizeof (uint64_t), 1, &obj
, tx
) != 0) {
4217 cmn_err(CE_PANIC
, "failed to add bpobj");
4219 VERIFY3U(0, ==, bpobj_open(&spa
->spa_deferred_bpobj
,
4220 spa
->spa_meta_objset
, obj
));
4223 * Generate some random noise for salted checksums to operate on.
4225 (void) random_get_pseudo_bytes(spa
->spa_cksum_salt
.zcs_bytes
,
4226 sizeof (spa
->spa_cksum_salt
.zcs_bytes
));
4229 * Set pool properties.
4231 spa
->spa_bootfs
= zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS
);
4232 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
4233 spa
->spa_failmode
= zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE
);
4234 spa
->spa_autoexpand
= zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND
);
4235 spa
->spa_multihost
= zpool_prop_default_numeric(ZPOOL_PROP_MULTIHOST
);
4237 if (props
!= NULL
) {
4238 spa_configfile_set(spa
, props
, B_FALSE
);
4239 spa_sync_props(props
, tx
);
4245 * If the root dataset is encrypted we will need to create key mappings
4246 * for the zio layer before we start to write any data to disk and hold
4247 * them until after the first txg has been synced. Waiting for the first
4248 * transaction to complete also ensures that our bean counters are
4249 * appropriately updated.
4251 if (dp
->dp_root_dir
->dd_crypto_obj
!= 0) {
4252 root_dsobj
= dsl_dir_phys(dp
->dp_root_dir
)->dd_head_dataset_obj
;
4253 VERIFY0(spa_keystore_create_mapping_impl(spa
, root_dsobj
,
4254 dp
->dp_root_dir
, FTAG
));
4257 spa
->spa_sync_on
= B_TRUE
;
4259 mmp_thread_start(spa
);
4260 txg_wait_synced(dp
, txg
);
4262 if (dp
->dp_root_dir
->dd_crypto_obj
!= 0)
4263 VERIFY0(spa_keystore_remove_mapping(spa
, root_dsobj
, FTAG
));
4265 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
4268 * Don't count references from objsets that are already closed
4269 * and are making their way through the eviction process.
4271 spa_evicting_os_wait(spa
);
4272 spa
->spa_minref
= refcount_count(&spa
->spa_refcount
);
4273 spa
->spa_load_state
= SPA_LOAD_NONE
;
4275 mutex_exit(&spa_namespace_lock
);
4281 * Import a non-root pool into the system.
4284 spa_import(char *pool
, nvlist_t
*config
, nvlist_t
*props
, uint64_t flags
)
4287 char *altroot
= NULL
;
4288 spa_load_state_t state
= SPA_LOAD_IMPORT
;
4289 zpool_rewind_policy_t policy
;
4290 uint64_t mode
= spa_mode_global
;
4291 uint64_t readonly
= B_FALSE
;
4294 nvlist_t
**spares
, **l2cache
;
4295 uint_t nspares
, nl2cache
;
4298 * If a pool with this name exists, return failure.
4300 mutex_enter(&spa_namespace_lock
);
4301 if (spa_lookup(pool
) != NULL
) {
4302 mutex_exit(&spa_namespace_lock
);
4303 return (SET_ERROR(EEXIST
));
4307 * Create and initialize the spa structure.
4309 (void) nvlist_lookup_string(props
,
4310 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
4311 (void) nvlist_lookup_uint64(props
,
4312 zpool_prop_to_name(ZPOOL_PROP_READONLY
), &readonly
);
4315 spa
= spa_add(pool
, config
, altroot
);
4316 spa
->spa_import_flags
= flags
;
4319 * Verbatim import - Take a pool and insert it into the namespace
4320 * as if it had been loaded at boot.
4322 if (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
) {
4324 spa_configfile_set(spa
, props
, B_FALSE
);
4326 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
4327 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_IMPORT
);
4329 mutex_exit(&spa_namespace_lock
);
4333 spa_activate(spa
, mode
);
4336 * Don't start async tasks until we know everything is healthy.
4338 spa_async_suspend(spa
);
4340 zpool_get_rewind_policy(config
, &policy
);
4341 if (policy
.zrp_request
& ZPOOL_DO_REWIND
)
4342 state
= SPA_LOAD_RECOVER
;
4345 * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig
4346 * because the user-supplied config is actually the one to trust when
4349 if (state
!= SPA_LOAD_RECOVER
)
4350 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
4352 error
= spa_load_best(spa
, state
, B_TRUE
, policy
.zrp_txg
,
4353 policy
.zrp_request
);
4356 * Propagate anything learned while loading the pool and pass it
4357 * back to caller (i.e. rewind info, missing devices, etc).
4359 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
4360 spa
->spa_load_info
) == 0);
4362 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4364 * Toss any existing sparelist, as it doesn't have any validity
4365 * anymore, and conflicts with spa_has_spare().
4367 if (spa
->spa_spares
.sav_config
) {
4368 nvlist_free(spa
->spa_spares
.sav_config
);
4369 spa
->spa_spares
.sav_config
= NULL
;
4370 spa_load_spares(spa
);
4372 if (spa
->spa_l2cache
.sav_config
) {
4373 nvlist_free(spa
->spa_l2cache
.sav_config
);
4374 spa
->spa_l2cache
.sav_config
= NULL
;
4375 spa_load_l2cache(spa
);
4378 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
4380 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4383 spa_configfile_set(spa
, props
, B_FALSE
);
4385 if (error
!= 0 || (props
&& spa_writeable(spa
) &&
4386 (error
= spa_prop_set(spa
, props
)))) {
4388 spa_deactivate(spa
);
4390 mutex_exit(&spa_namespace_lock
);
4394 spa_async_resume(spa
);
4397 * Override any spares and level 2 cache devices as specified by
4398 * the user, as these may have correct device names/devids, etc.
4400 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
4401 &spares
, &nspares
) == 0) {
4402 if (spa
->spa_spares
.sav_config
)
4403 VERIFY(nvlist_remove(spa
->spa_spares
.sav_config
,
4404 ZPOOL_CONFIG_SPARES
, DATA_TYPE_NVLIST_ARRAY
) == 0);
4406 VERIFY(nvlist_alloc(&spa
->spa_spares
.sav_config
,
4407 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
4408 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
4409 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
4410 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4411 spa_load_spares(spa
);
4412 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4413 spa
->spa_spares
.sav_sync
= B_TRUE
;
4415 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
4416 &l2cache
, &nl2cache
) == 0) {
4417 if (spa
->spa_l2cache
.sav_config
)
4418 VERIFY(nvlist_remove(spa
->spa_l2cache
.sav_config
,
4419 ZPOOL_CONFIG_L2CACHE
, DATA_TYPE_NVLIST_ARRAY
) == 0);
4421 VERIFY(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
4422 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
4423 VERIFY(nvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
4424 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
4425 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4426 spa_load_l2cache(spa
);
4427 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4428 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
4432 * Check for any removed devices.
4434 if (spa
->spa_autoreplace
) {
4435 spa_aux_check_removed(&spa
->spa_spares
);
4436 spa_aux_check_removed(&spa
->spa_l2cache
);
4439 if (spa_writeable(spa
)) {
4441 * Update the config cache to include the newly-imported pool.
4443 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
4447 * It's possible that the pool was expanded while it was exported.
4448 * We kick off an async task to handle this for us.
4450 spa_async_request(spa
, SPA_ASYNC_AUTOEXPAND
);
4452 spa_history_log_version(spa
, "import", NULL
);
4454 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_IMPORT
);
4456 zvol_create_minors(spa
, pool
, B_TRUE
);
4458 mutex_exit(&spa_namespace_lock
);
4464 spa_tryimport(nvlist_t
*tryconfig
)
4466 nvlist_t
*config
= NULL
;
4472 if (nvlist_lookup_string(tryconfig
, ZPOOL_CONFIG_POOL_NAME
, &poolname
))
4475 if (nvlist_lookup_uint64(tryconfig
, ZPOOL_CONFIG_POOL_STATE
, &state
))
4479 * Create and initialize the spa structure.
4481 mutex_enter(&spa_namespace_lock
);
4482 spa
= spa_add(TRYIMPORT_NAME
, tryconfig
, NULL
);
4483 spa_activate(spa
, FREAD
);
4486 * Pass off the heavy lifting to spa_load().
4487 * Pass TRUE for mosconfig because the user-supplied config
4488 * is actually the one to trust when doing an import.
4490 error
= spa_load(spa
, SPA_LOAD_TRYIMPORT
, SPA_IMPORT_EXISTING
, B_TRUE
);
4493 * If 'tryconfig' was at least parsable, return the current config.
4495 if (spa
->spa_root_vdev
!= NULL
) {
4496 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
4497 VERIFY(nvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
,
4499 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
4501 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_TIMESTAMP
,
4502 spa
->spa_uberblock
.ub_timestamp
) == 0);
4503 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
4504 spa
->spa_load_info
) == 0);
4505 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_ERRATA
,
4506 spa
->spa_errata
) == 0);
4509 * If the bootfs property exists on this pool then we
4510 * copy it out so that external consumers can tell which
4511 * pools are bootable.
4513 if ((!error
|| error
== EEXIST
) && spa
->spa_bootfs
) {
4514 char *tmpname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
4517 * We have to play games with the name since the
4518 * pool was opened as TRYIMPORT_NAME.
4520 if (dsl_dsobj_to_dsname(spa_name(spa
),
4521 spa
->spa_bootfs
, tmpname
) == 0) {
4525 dsname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
4527 cp
= strchr(tmpname
, '/');
4529 (void) strlcpy(dsname
, tmpname
,
4532 (void) snprintf(dsname
, MAXPATHLEN
,
4533 "%s/%s", poolname
, ++cp
);
4535 VERIFY(nvlist_add_string(config
,
4536 ZPOOL_CONFIG_BOOTFS
, dsname
) == 0);
4537 kmem_free(dsname
, MAXPATHLEN
);
4539 kmem_free(tmpname
, MAXPATHLEN
);
4543 * Add the list of hot spares and level 2 cache devices.
4545 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
4546 spa_add_spares(spa
, config
);
4547 spa_add_l2cache(spa
, config
);
4548 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
4552 spa_deactivate(spa
);
4554 mutex_exit(&spa_namespace_lock
);
4560 * Pool export/destroy
4562 * The act of destroying or exporting a pool is very simple. We make sure there
4563 * is no more pending I/O and any references to the pool are gone. Then, we
4564 * update the pool state and sync all the labels to disk, removing the
4565 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
4566 * we don't sync the labels or remove the configuration cache.
4569 spa_export_common(char *pool
, int new_state
, nvlist_t
**oldconfig
,
4570 boolean_t force
, boolean_t hardforce
)
4577 if (!(spa_mode_global
& FWRITE
))
4578 return (SET_ERROR(EROFS
));
4580 mutex_enter(&spa_namespace_lock
);
4581 if ((spa
= spa_lookup(pool
)) == NULL
) {
4582 mutex_exit(&spa_namespace_lock
);
4583 return (SET_ERROR(ENOENT
));
4587 * Put a hold on the pool, drop the namespace lock, stop async tasks,
4588 * reacquire the namespace lock, and see if we can export.
4590 spa_open_ref(spa
, FTAG
);
4591 mutex_exit(&spa_namespace_lock
);
4592 spa_async_suspend(spa
);
4593 if (spa
->spa_zvol_taskq
) {
4594 zvol_remove_minors(spa
, spa_name(spa
), B_TRUE
);
4595 taskq_wait(spa
->spa_zvol_taskq
);
4597 mutex_enter(&spa_namespace_lock
);
4598 spa_close(spa
, FTAG
);
4600 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
)
4603 * The pool will be in core if it's openable, in which case we can
4604 * modify its state. Objsets may be open only because they're dirty,
4605 * so we have to force it to sync before checking spa_refcnt.
4607 if (spa
->spa_sync_on
) {
4608 txg_wait_synced(spa
->spa_dsl_pool
, 0);
4609 spa_evicting_os_wait(spa
);
4613 * A pool cannot be exported or destroyed if there are active
4614 * references. If we are resetting a pool, allow references by
4615 * fault injection handlers.
4617 if (!spa_refcount_zero(spa
) ||
4618 (spa
->spa_inject_ref
!= 0 &&
4619 new_state
!= POOL_STATE_UNINITIALIZED
)) {
4620 spa_async_resume(spa
);
4621 mutex_exit(&spa_namespace_lock
);
4622 return (SET_ERROR(EBUSY
));
4625 if (spa
->spa_sync_on
) {
4627 * A pool cannot be exported if it has an active shared spare.
4628 * This is to prevent other pools stealing the active spare
4629 * from an exported pool. At user's own will, such pool can
4630 * be forcedly exported.
4632 if (!force
&& new_state
== POOL_STATE_EXPORTED
&&
4633 spa_has_active_shared_spare(spa
)) {
4634 spa_async_resume(spa
);
4635 mutex_exit(&spa_namespace_lock
);
4636 return (SET_ERROR(EXDEV
));
4640 * We want this to be reflected on every label,
4641 * so mark them all dirty. spa_unload() will do the
4642 * final sync that pushes these changes out.
4644 if (new_state
!= POOL_STATE_UNINITIALIZED
&& !hardforce
) {
4645 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4646 spa
->spa_state
= new_state
;
4647 spa
->spa_final_txg
= spa_last_synced_txg(spa
) +
4649 vdev_config_dirty(spa
->spa_root_vdev
);
4650 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4655 if (new_state
== POOL_STATE_DESTROYED
)
4656 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_DESTROY
);
4657 else if (new_state
== POOL_STATE_EXPORTED
)
4658 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_EXPORT
);
4660 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
4662 spa_deactivate(spa
);
4665 if (oldconfig
&& spa
->spa_config
)
4666 VERIFY(nvlist_dup(spa
->spa_config
, oldconfig
, 0) == 0);
4668 if (new_state
!= POOL_STATE_UNINITIALIZED
) {
4670 spa_config_sync(spa
, B_TRUE
, B_TRUE
);
4673 mutex_exit(&spa_namespace_lock
);
4679 * Destroy a storage pool.
4682 spa_destroy(char *pool
)
4684 return (spa_export_common(pool
, POOL_STATE_DESTROYED
, NULL
,
4689 * Export a storage pool.
4692 spa_export(char *pool
, nvlist_t
**oldconfig
, boolean_t force
,
4693 boolean_t hardforce
)
4695 return (spa_export_common(pool
, POOL_STATE_EXPORTED
, oldconfig
,
4700 * Similar to spa_export(), this unloads the spa_t without actually removing it
4701 * from the namespace in any way.
4704 spa_reset(char *pool
)
4706 return (spa_export_common(pool
, POOL_STATE_UNINITIALIZED
, NULL
,
4711 * ==========================================================================
4712 * Device manipulation
4713 * ==========================================================================
4717 * Add a device to a storage pool.
4720 spa_vdev_add(spa_t
*spa
, nvlist_t
*nvroot
)
4724 vdev_t
*rvd
= spa
->spa_root_vdev
;
4726 nvlist_t
**spares
, **l2cache
;
4727 uint_t nspares
, nl2cache
;
4729 ASSERT(spa_writeable(spa
));
4731 txg
= spa_vdev_enter(spa
);
4733 if ((error
= spa_config_parse(spa
, &vd
, nvroot
, NULL
, 0,
4734 VDEV_ALLOC_ADD
)) != 0)
4735 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
4737 spa
->spa_pending_vdev
= vd
; /* spa_vdev_exit() will clear this */
4739 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
, &spares
,
4743 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
, &l2cache
,
4747 if (vd
->vdev_children
== 0 && nspares
== 0 && nl2cache
== 0)
4748 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
4750 if (vd
->vdev_children
!= 0 &&
4751 (error
= vdev_create(vd
, txg
, B_FALSE
)) != 0)
4752 return (spa_vdev_exit(spa
, vd
, txg
, error
));
4755 * We must validate the spares and l2cache devices after checking the
4756 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
4758 if ((error
= spa_validate_aux(spa
, nvroot
, txg
, VDEV_ALLOC_ADD
)) != 0)
4759 return (spa_vdev_exit(spa
, vd
, txg
, error
));
4762 * Transfer each new top-level vdev from vd to rvd.
4764 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
4767 * Set the vdev id to the first hole, if one exists.
4769 for (id
= 0; id
< rvd
->vdev_children
; id
++) {
4770 if (rvd
->vdev_child
[id
]->vdev_ishole
) {
4771 vdev_free(rvd
->vdev_child
[id
]);
4775 tvd
= vd
->vdev_child
[c
];
4776 vdev_remove_child(vd
, tvd
);
4778 vdev_add_child(rvd
, tvd
);
4779 vdev_config_dirty(tvd
);
4783 spa_set_aux_vdevs(&spa
->spa_spares
, spares
, nspares
,
4784 ZPOOL_CONFIG_SPARES
);
4785 spa_load_spares(spa
);
4786 spa
->spa_spares
.sav_sync
= B_TRUE
;
4789 if (nl2cache
!= 0) {
4790 spa_set_aux_vdevs(&spa
->spa_l2cache
, l2cache
, nl2cache
,
4791 ZPOOL_CONFIG_L2CACHE
);
4792 spa_load_l2cache(spa
);
4793 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
4797 * We have to be careful when adding new vdevs to an existing pool.
4798 * If other threads start allocating from these vdevs before we
4799 * sync the config cache, and we lose power, then upon reboot we may
4800 * fail to open the pool because there are DVAs that the config cache
4801 * can't translate. Therefore, we first add the vdevs without
4802 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
4803 * and then let spa_config_update() initialize the new metaslabs.
4805 * spa_load() checks for added-but-not-initialized vdevs, so that
4806 * if we lose power at any point in this sequence, the remaining
4807 * steps will be completed the next time we load the pool.
4809 (void) spa_vdev_exit(spa
, vd
, txg
, 0);
4811 mutex_enter(&spa_namespace_lock
);
4812 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
4813 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_VDEV_ADD
);
4814 mutex_exit(&spa_namespace_lock
);
4820 * Attach a device to a mirror. The arguments are the path to any device
4821 * in the mirror, and the nvroot for the new device. If the path specifies
4822 * a device that is not mirrored, we automatically insert the mirror vdev.
4824 * If 'replacing' is specified, the new device is intended to replace the
4825 * existing device; in this case the two devices are made into their own
4826 * mirror using the 'replacing' vdev, which is functionally identical to
4827 * the mirror vdev (it actually reuses all the same ops) but has a few
4828 * extra rules: you can't attach to it after it's been created, and upon
4829 * completion of resilvering, the first disk (the one being replaced)
4830 * is automatically detached.
4833 spa_vdev_attach(spa_t
*spa
, uint64_t guid
, nvlist_t
*nvroot
, int replacing
)
4835 uint64_t txg
, dtl_max_txg
;
4836 ASSERTV(vdev_t
*rvd
= spa
->spa_root_vdev
);
4837 vdev_t
*oldvd
, *newvd
, *newrootvd
, *pvd
, *tvd
;
4839 char *oldvdpath
, *newvdpath
;
4843 ASSERT(spa_writeable(spa
));
4845 txg
= spa_vdev_enter(spa
);
4847 oldvd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
4850 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
4852 if (!oldvd
->vdev_ops
->vdev_op_leaf
)
4853 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4855 pvd
= oldvd
->vdev_parent
;
4857 if ((error
= spa_config_parse(spa
, &newrootvd
, nvroot
, NULL
, 0,
4858 VDEV_ALLOC_ATTACH
)) != 0)
4859 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
4861 if (newrootvd
->vdev_children
!= 1)
4862 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
4864 newvd
= newrootvd
->vdev_child
[0];
4866 if (!newvd
->vdev_ops
->vdev_op_leaf
)
4867 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
4869 if ((error
= vdev_create(newrootvd
, txg
, replacing
)) != 0)
4870 return (spa_vdev_exit(spa
, newrootvd
, txg
, error
));
4873 * Spares can't replace logs
4875 if (oldvd
->vdev_top
->vdev_islog
&& newvd
->vdev_isspare
)
4876 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4880 * For attach, the only allowable parent is a mirror or the root
4883 if (pvd
->vdev_ops
!= &vdev_mirror_ops
&&
4884 pvd
->vdev_ops
!= &vdev_root_ops
)
4885 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4887 pvops
= &vdev_mirror_ops
;
4890 * Active hot spares can only be replaced by inactive hot
4893 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
4894 oldvd
->vdev_isspare
&&
4895 !spa_has_spare(spa
, newvd
->vdev_guid
))
4896 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4899 * If the source is a hot spare, and the parent isn't already a
4900 * spare, then we want to create a new hot spare. Otherwise, we
4901 * want to create a replacing vdev. The user is not allowed to
4902 * attach to a spared vdev child unless the 'isspare' state is
4903 * the same (spare replaces spare, non-spare replaces
4906 if (pvd
->vdev_ops
== &vdev_replacing_ops
&&
4907 spa_version(spa
) < SPA_VERSION_MULTI_REPLACE
) {
4908 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4909 } else if (pvd
->vdev_ops
== &vdev_spare_ops
&&
4910 newvd
->vdev_isspare
!= oldvd
->vdev_isspare
) {
4911 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4914 if (newvd
->vdev_isspare
)
4915 pvops
= &vdev_spare_ops
;
4917 pvops
= &vdev_replacing_ops
;
4921 * Make sure the new device is big enough.
4923 if (newvd
->vdev_asize
< vdev_get_min_asize(oldvd
))
4924 return (spa_vdev_exit(spa
, newrootvd
, txg
, EOVERFLOW
));
4927 * The new device cannot have a higher alignment requirement
4928 * than the top-level vdev.
4930 if (newvd
->vdev_ashift
> oldvd
->vdev_top
->vdev_ashift
)
4931 return (spa_vdev_exit(spa
, newrootvd
, txg
, EDOM
));
4934 * If this is an in-place replacement, update oldvd's path and devid
4935 * to make it distinguishable from newvd, and unopenable from now on.
4937 if (strcmp(oldvd
->vdev_path
, newvd
->vdev_path
) == 0) {
4938 spa_strfree(oldvd
->vdev_path
);
4939 oldvd
->vdev_path
= kmem_alloc(strlen(newvd
->vdev_path
) + 5,
4941 (void) sprintf(oldvd
->vdev_path
, "%s/%s",
4942 newvd
->vdev_path
, "old");
4943 if (oldvd
->vdev_devid
!= NULL
) {
4944 spa_strfree(oldvd
->vdev_devid
);
4945 oldvd
->vdev_devid
= NULL
;
4949 /* mark the device being resilvered */
4950 newvd
->vdev_resilver_txg
= txg
;
4953 * If the parent is not a mirror, or if we're replacing, insert the new
4954 * mirror/replacing/spare vdev above oldvd.
4956 if (pvd
->vdev_ops
!= pvops
)
4957 pvd
= vdev_add_parent(oldvd
, pvops
);
4959 ASSERT(pvd
->vdev_top
->vdev_parent
== rvd
);
4960 ASSERT(pvd
->vdev_ops
== pvops
);
4961 ASSERT(oldvd
->vdev_parent
== pvd
);
4964 * Extract the new device from its root and add it to pvd.
4966 vdev_remove_child(newrootvd
, newvd
);
4967 newvd
->vdev_id
= pvd
->vdev_children
;
4968 newvd
->vdev_crtxg
= oldvd
->vdev_crtxg
;
4969 vdev_add_child(pvd
, newvd
);
4972 * Reevaluate the parent vdev state.
4974 vdev_propagate_state(pvd
);
4976 tvd
= newvd
->vdev_top
;
4977 ASSERT(pvd
->vdev_top
== tvd
);
4978 ASSERT(tvd
->vdev_parent
== rvd
);
4980 vdev_config_dirty(tvd
);
4983 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
4984 * for any dmu_sync-ed blocks. It will propagate upward when
4985 * spa_vdev_exit() calls vdev_dtl_reassess().
4987 dtl_max_txg
= txg
+ TXG_CONCURRENT_STATES
;
4989 vdev_dtl_dirty(newvd
, DTL_MISSING
, TXG_INITIAL
,
4990 dtl_max_txg
- TXG_INITIAL
);
4992 if (newvd
->vdev_isspare
) {
4993 spa_spare_activate(newvd
);
4994 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_VDEV_SPARE
);
4997 oldvdpath
= spa_strdup(oldvd
->vdev_path
);
4998 newvdpath
= spa_strdup(newvd
->vdev_path
);
4999 newvd_isspare
= newvd
->vdev_isspare
;
5002 * Mark newvd's DTL dirty in this txg.
5004 vdev_dirty(tvd
, VDD_DTL
, newvd
, txg
);
5007 * Schedule the resilver to restart in the future. We do this to
5008 * ensure that dmu_sync-ed blocks have been stitched into the
5009 * respective datasets.
5011 dsl_resilver_restart(spa
->spa_dsl_pool
, dtl_max_txg
);
5013 if (spa
->spa_bootfs
)
5014 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_BOOTFS_VDEV_ATTACH
);
5016 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_VDEV_ATTACH
);
5021 (void) spa_vdev_exit(spa
, newrootvd
, dtl_max_txg
, 0);
5023 spa_history_log_internal(spa
, "vdev attach", NULL
,
5024 "%s vdev=%s %s vdev=%s",
5025 replacing
&& newvd_isspare
? "spare in" :
5026 replacing
? "replace" : "attach", newvdpath
,
5027 replacing
? "for" : "to", oldvdpath
);
5029 spa_strfree(oldvdpath
);
5030 spa_strfree(newvdpath
);
5036 * Detach a device from a mirror or replacing vdev.
5038 * If 'replace_done' is specified, only detach if the parent
5039 * is a replacing vdev.
5042 spa_vdev_detach(spa_t
*spa
, uint64_t guid
, uint64_t pguid
, int replace_done
)
5046 ASSERTV(vdev_t
*rvd
= spa
->spa_root_vdev
);
5047 vdev_t
*vd
, *pvd
, *cvd
, *tvd
;
5048 boolean_t unspare
= B_FALSE
;
5049 uint64_t unspare_guid
= 0;
5052 ASSERT(spa_writeable(spa
));
5054 txg
= spa_vdev_enter(spa
);
5056 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
5059 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
5061 if (!vd
->vdev_ops
->vdev_op_leaf
)
5062 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
5064 pvd
= vd
->vdev_parent
;
5067 * If the parent/child relationship is not as expected, don't do it.
5068 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
5069 * vdev that's replacing B with C. The user's intent in replacing
5070 * is to go from M(A,B) to M(A,C). If the user decides to cancel
5071 * the replace by detaching C, the expected behavior is to end up
5072 * M(A,B). But suppose that right after deciding to detach C,
5073 * the replacement of B completes. We would have M(A,C), and then
5074 * ask to detach C, which would leave us with just A -- not what
5075 * the user wanted. To prevent this, we make sure that the
5076 * parent/child relationship hasn't changed -- in this example,
5077 * that C's parent is still the replacing vdev R.
5079 if (pvd
->vdev_guid
!= pguid
&& pguid
!= 0)
5080 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
5083 * Only 'replacing' or 'spare' vdevs can be replaced.
5085 if (replace_done
&& pvd
->vdev_ops
!= &vdev_replacing_ops
&&
5086 pvd
->vdev_ops
!= &vdev_spare_ops
)
5087 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
5089 ASSERT(pvd
->vdev_ops
!= &vdev_spare_ops
||
5090 spa_version(spa
) >= SPA_VERSION_SPARES
);
5093 * Only mirror, replacing, and spare vdevs support detach.
5095 if (pvd
->vdev_ops
!= &vdev_replacing_ops
&&
5096 pvd
->vdev_ops
!= &vdev_mirror_ops
&&
5097 pvd
->vdev_ops
!= &vdev_spare_ops
)
5098 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
5101 * If this device has the only valid copy of some data,
5102 * we cannot safely detach it.
5104 if (vdev_dtl_required(vd
))
5105 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
5107 ASSERT(pvd
->vdev_children
>= 2);
5110 * If we are detaching the second disk from a replacing vdev, then
5111 * check to see if we changed the original vdev's path to have "/old"
5112 * at the end in spa_vdev_attach(). If so, undo that change now.
5114 if (pvd
->vdev_ops
== &vdev_replacing_ops
&& vd
->vdev_id
> 0 &&
5115 vd
->vdev_path
!= NULL
) {
5116 size_t len
= strlen(vd
->vdev_path
);
5118 for (int c
= 0; c
< pvd
->vdev_children
; c
++) {
5119 cvd
= pvd
->vdev_child
[c
];
5121 if (cvd
== vd
|| cvd
->vdev_path
== NULL
)
5124 if (strncmp(cvd
->vdev_path
, vd
->vdev_path
, len
) == 0 &&
5125 strcmp(cvd
->vdev_path
+ len
, "/old") == 0) {
5126 spa_strfree(cvd
->vdev_path
);
5127 cvd
->vdev_path
= spa_strdup(vd
->vdev_path
);
5134 * If we are detaching the original disk from a spare, then it implies
5135 * that the spare should become a real disk, and be removed from the
5136 * active spare list for the pool.
5138 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
5140 pvd
->vdev_child
[pvd
->vdev_children
- 1]->vdev_isspare
)
5144 * Erase the disk labels so the disk can be used for other things.
5145 * This must be done after all other error cases are handled,
5146 * but before we disembowel vd (so we can still do I/O to it).
5147 * But if we can't do it, don't treat the error as fatal --
5148 * it may be that the unwritability of the disk is the reason
5149 * it's being detached!
5151 error
= vdev_label_init(vd
, 0, VDEV_LABEL_REMOVE
);
5154 * Remove vd from its parent and compact the parent's children.
5156 vdev_remove_child(pvd
, vd
);
5157 vdev_compact_children(pvd
);
5160 * Remember one of the remaining children so we can get tvd below.
5162 cvd
= pvd
->vdev_child
[pvd
->vdev_children
- 1];
5165 * If we need to remove the remaining child from the list of hot spares,
5166 * do it now, marking the vdev as no longer a spare in the process.
5167 * We must do this before vdev_remove_parent(), because that can
5168 * change the GUID if it creates a new toplevel GUID. For a similar
5169 * reason, we must remove the spare now, in the same txg as the detach;
5170 * otherwise someone could attach a new sibling, change the GUID, and
5171 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
5174 ASSERT(cvd
->vdev_isspare
);
5175 spa_spare_remove(cvd
);
5176 unspare_guid
= cvd
->vdev_guid
;
5177 (void) spa_vdev_remove(spa
, unspare_guid
, B_TRUE
);
5178 cvd
->vdev_unspare
= B_TRUE
;
5182 * If the parent mirror/replacing vdev only has one child,
5183 * the parent is no longer needed. Remove it from the tree.
5185 if (pvd
->vdev_children
== 1) {
5186 if (pvd
->vdev_ops
== &vdev_spare_ops
)
5187 cvd
->vdev_unspare
= B_FALSE
;
5188 vdev_remove_parent(cvd
);
5193 * We don't set tvd until now because the parent we just removed
5194 * may have been the previous top-level vdev.
5196 tvd
= cvd
->vdev_top
;
5197 ASSERT(tvd
->vdev_parent
== rvd
);
5200 * Reevaluate the parent vdev state.
5202 vdev_propagate_state(cvd
);
5205 * If the 'autoexpand' property is set on the pool then automatically
5206 * try to expand the size of the pool. For example if the device we
5207 * just detached was smaller than the others, it may be possible to
5208 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
5209 * first so that we can obtain the updated sizes of the leaf vdevs.
5211 if (spa
->spa_autoexpand
) {
5213 vdev_expand(tvd
, txg
);
5216 vdev_config_dirty(tvd
);
5219 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
5220 * vd->vdev_detached is set and free vd's DTL object in syncing context.
5221 * But first make sure we're not on any *other* txg's DTL list, to
5222 * prevent vd from being accessed after it's freed.
5224 vdpath
= spa_strdup(vd
->vdev_path
? vd
->vdev_path
: "none");
5225 for (int t
= 0; t
< TXG_SIZE
; t
++)
5226 (void) txg_list_remove_this(&tvd
->vdev_dtl_list
, vd
, t
);
5227 vd
->vdev_detached
= B_TRUE
;
5228 vdev_dirty(tvd
, VDD_DTL
, vd
, txg
);
5230 spa_event_notify(spa
, vd
, NULL
, ESC_ZFS_VDEV_REMOVE
);
5232 /* hang on to the spa before we release the lock */
5233 spa_open_ref(spa
, FTAG
);
5235 error
= spa_vdev_exit(spa
, vd
, txg
, 0);
5237 spa_history_log_internal(spa
, "detach", NULL
,
5239 spa_strfree(vdpath
);
5242 * If this was the removal of the original device in a hot spare vdev,
5243 * then we want to go through and remove the device from the hot spare
5244 * list of every other pool.
5247 spa_t
*altspa
= NULL
;
5249 mutex_enter(&spa_namespace_lock
);
5250 while ((altspa
= spa_next(altspa
)) != NULL
) {
5251 if (altspa
->spa_state
!= POOL_STATE_ACTIVE
||
5255 spa_open_ref(altspa
, FTAG
);
5256 mutex_exit(&spa_namespace_lock
);
5257 (void) spa_vdev_remove(altspa
, unspare_guid
, B_TRUE
);
5258 mutex_enter(&spa_namespace_lock
);
5259 spa_close(altspa
, FTAG
);
5261 mutex_exit(&spa_namespace_lock
);
5263 /* search the rest of the vdevs for spares to remove */
5264 spa_vdev_resilver_done(spa
);
5267 /* all done with the spa; OK to release */
5268 mutex_enter(&spa_namespace_lock
);
5269 spa_close(spa
, FTAG
);
5270 mutex_exit(&spa_namespace_lock
);
5276 * Split a set of devices from their mirrors, and create a new pool from them.
5279 spa_vdev_split_mirror(spa_t
*spa
, char *newname
, nvlist_t
*config
,
5280 nvlist_t
*props
, boolean_t exp
)
5283 uint64_t txg
, *glist
;
5285 uint_t c
, children
, lastlog
;
5286 nvlist_t
**child
, *nvl
, *tmp
;
5288 char *altroot
= NULL
;
5289 vdev_t
*rvd
, **vml
= NULL
; /* vdev modify list */
5290 boolean_t activate_slog
;
5292 ASSERT(spa_writeable(spa
));
5294 txg
= spa_vdev_enter(spa
);
5296 /* clear the log and flush everything up to now */
5297 activate_slog
= spa_passivate_log(spa
);
5298 (void) spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
5299 error
= spa_offline_log(spa
);
5300 txg
= spa_vdev_config_enter(spa
);
5303 spa_activate_log(spa
);
5306 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5308 /* check new spa name before going any further */
5309 if (spa_lookup(newname
) != NULL
)
5310 return (spa_vdev_exit(spa
, NULL
, txg
, EEXIST
));
5313 * scan through all the children to ensure they're all mirrors
5315 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvl
) != 0 ||
5316 nvlist_lookup_nvlist_array(nvl
, ZPOOL_CONFIG_CHILDREN
, &child
,
5318 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
5320 /* first, check to ensure we've got the right child count */
5321 rvd
= spa
->spa_root_vdev
;
5323 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
5324 vdev_t
*vd
= rvd
->vdev_child
[c
];
5326 /* don't count the holes & logs as children */
5327 if (vd
->vdev_islog
|| vd
->vdev_ishole
) {
5335 if (children
!= (lastlog
!= 0 ? lastlog
: rvd
->vdev_children
))
5336 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
5338 /* next, ensure no spare or cache devices are part of the split */
5339 if (nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_SPARES
, &tmp
) == 0 ||
5340 nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_L2CACHE
, &tmp
) == 0)
5341 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
5343 vml
= kmem_zalloc(children
* sizeof (vdev_t
*), KM_SLEEP
);
5344 glist
= kmem_zalloc(children
* sizeof (uint64_t), KM_SLEEP
);
5346 /* then, loop over each vdev and validate it */
5347 for (c
= 0; c
< children
; c
++) {
5348 uint64_t is_hole
= 0;
5350 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
5354 if (spa
->spa_root_vdev
->vdev_child
[c
]->vdev_ishole
||
5355 spa
->spa_root_vdev
->vdev_child
[c
]->vdev_islog
) {
5358 error
= SET_ERROR(EINVAL
);
5363 /* which disk is going to be split? */
5364 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_GUID
,
5366 error
= SET_ERROR(EINVAL
);
5370 /* look it up in the spa */
5371 vml
[c
] = spa_lookup_by_guid(spa
, glist
[c
], B_FALSE
);
5372 if (vml
[c
] == NULL
) {
5373 error
= SET_ERROR(ENODEV
);
5377 /* make sure there's nothing stopping the split */
5378 if (vml
[c
]->vdev_parent
->vdev_ops
!= &vdev_mirror_ops
||
5379 vml
[c
]->vdev_islog
||
5380 vml
[c
]->vdev_ishole
||
5381 vml
[c
]->vdev_isspare
||
5382 vml
[c
]->vdev_isl2cache
||
5383 !vdev_writeable(vml
[c
]) ||
5384 vml
[c
]->vdev_children
!= 0 ||
5385 vml
[c
]->vdev_state
!= VDEV_STATE_HEALTHY
||
5386 c
!= spa
->spa_root_vdev
->vdev_child
[c
]->vdev_id
) {
5387 error
= SET_ERROR(EINVAL
);
5391 if (vdev_dtl_required(vml
[c
])) {
5392 error
= SET_ERROR(EBUSY
);
5396 /* we need certain info from the top level */
5397 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_ARRAY
,
5398 vml
[c
]->vdev_top
->vdev_ms_array
) == 0);
5399 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_SHIFT
,
5400 vml
[c
]->vdev_top
->vdev_ms_shift
) == 0);
5401 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASIZE
,
5402 vml
[c
]->vdev_top
->vdev_asize
) == 0);
5403 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASHIFT
,
5404 vml
[c
]->vdev_top
->vdev_ashift
) == 0);
5406 /* transfer per-vdev ZAPs */
5407 ASSERT3U(vml
[c
]->vdev_leaf_zap
, !=, 0);
5408 VERIFY0(nvlist_add_uint64(child
[c
],
5409 ZPOOL_CONFIG_VDEV_LEAF_ZAP
, vml
[c
]->vdev_leaf_zap
));
5411 ASSERT3U(vml
[c
]->vdev_top
->vdev_top_zap
, !=, 0);
5412 VERIFY0(nvlist_add_uint64(child
[c
],
5413 ZPOOL_CONFIG_VDEV_TOP_ZAP
,
5414 vml
[c
]->vdev_parent
->vdev_top_zap
));
5418 kmem_free(vml
, children
* sizeof (vdev_t
*));
5419 kmem_free(glist
, children
* sizeof (uint64_t));
5420 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5423 /* stop writers from using the disks */
5424 for (c
= 0; c
< children
; c
++) {
5426 vml
[c
]->vdev_offline
= B_TRUE
;
5428 vdev_reopen(spa
->spa_root_vdev
);
5431 * Temporarily record the splitting vdevs in the spa config. This
5432 * will disappear once the config is regenerated.
5434 VERIFY(nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
5435 VERIFY(nvlist_add_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
5436 glist
, children
) == 0);
5437 kmem_free(glist
, children
* sizeof (uint64_t));
5439 mutex_enter(&spa
->spa_props_lock
);
5440 VERIFY(nvlist_add_nvlist(spa
->spa_config
, ZPOOL_CONFIG_SPLIT
,
5442 mutex_exit(&spa
->spa_props_lock
);
5443 spa
->spa_config_splitting
= nvl
;
5444 vdev_config_dirty(spa
->spa_root_vdev
);
5446 /* configure and create the new pool */
5447 VERIFY(nvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
, newname
) == 0);
5448 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
5449 exp
? POOL_STATE_EXPORTED
: POOL_STATE_ACTIVE
) == 0);
5450 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
5451 spa_version(spa
)) == 0);
5452 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
5453 spa
->spa_config_txg
) == 0);
5454 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
5455 spa_generate_guid(NULL
)) == 0);
5456 VERIFY0(nvlist_add_boolean(config
, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
));
5457 (void) nvlist_lookup_string(props
,
5458 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
5460 /* add the new pool to the namespace */
5461 newspa
= spa_add(newname
, config
, altroot
);
5462 newspa
->spa_avz_action
= AVZ_ACTION_REBUILD
;
5463 newspa
->spa_config_txg
= spa
->spa_config_txg
;
5464 spa_set_log_state(newspa
, SPA_LOG_CLEAR
);
5466 /* release the spa config lock, retaining the namespace lock */
5467 spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
5469 if (zio_injection_enabled
)
5470 zio_handle_panic_injection(spa
, FTAG
, 1);
5472 spa_activate(newspa
, spa_mode_global
);
5473 spa_async_suspend(newspa
);
5475 /* create the new pool from the disks of the original pool */
5476 error
= spa_load(newspa
, SPA_LOAD_IMPORT
, SPA_IMPORT_ASSEMBLE
, B_TRUE
);
5480 /* if that worked, generate a real config for the new pool */
5481 if (newspa
->spa_root_vdev
!= NULL
) {
5482 VERIFY(nvlist_alloc(&newspa
->spa_config_splitting
,
5483 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
5484 VERIFY(nvlist_add_uint64(newspa
->spa_config_splitting
,
5485 ZPOOL_CONFIG_SPLIT_GUID
, spa_guid(spa
)) == 0);
5486 spa_config_set(newspa
, spa_config_generate(newspa
, NULL
, -1ULL,
5491 if (props
!= NULL
) {
5492 spa_configfile_set(newspa
, props
, B_FALSE
);
5493 error
= spa_prop_set(newspa
, props
);
5498 /* flush everything */
5499 txg
= spa_vdev_config_enter(newspa
);
5500 vdev_config_dirty(newspa
->spa_root_vdev
);
5501 (void) spa_vdev_config_exit(newspa
, NULL
, txg
, 0, FTAG
);
5503 if (zio_injection_enabled
)
5504 zio_handle_panic_injection(spa
, FTAG
, 2);
5506 spa_async_resume(newspa
);
5508 /* finally, update the original pool's config */
5509 txg
= spa_vdev_config_enter(spa
);
5510 tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
5511 error
= dmu_tx_assign(tx
, TXG_WAIT
);
5514 for (c
= 0; c
< children
; c
++) {
5515 if (vml
[c
] != NULL
) {
5518 spa_history_log_internal(spa
, "detach", tx
,
5519 "vdev=%s", vml
[c
]->vdev_path
);
5524 spa
->spa_avz_action
= AVZ_ACTION_REBUILD
;
5525 vdev_config_dirty(spa
->spa_root_vdev
);
5526 spa
->spa_config_splitting
= NULL
;
5530 (void) spa_vdev_exit(spa
, NULL
, txg
, 0);
5532 if (zio_injection_enabled
)
5533 zio_handle_panic_injection(spa
, FTAG
, 3);
5535 /* split is complete; log a history record */
5536 spa_history_log_internal(newspa
, "split", NULL
,
5537 "from pool %s", spa_name(spa
));
5539 kmem_free(vml
, children
* sizeof (vdev_t
*));
5541 /* if we're not going to mount the filesystems in userland, export */
5543 error
= spa_export_common(newname
, POOL_STATE_EXPORTED
, NULL
,
5550 spa_deactivate(newspa
);
5553 txg
= spa_vdev_config_enter(spa
);
5555 /* re-online all offlined disks */
5556 for (c
= 0; c
< children
; c
++) {
5558 vml
[c
]->vdev_offline
= B_FALSE
;
5560 vdev_reopen(spa
->spa_root_vdev
);
5562 nvlist_free(spa
->spa_config_splitting
);
5563 spa
->spa_config_splitting
= NULL
;
5564 (void) spa_vdev_exit(spa
, NULL
, txg
, error
);
5566 kmem_free(vml
, children
* sizeof (vdev_t
*));
5571 spa_nvlist_lookup_by_guid(nvlist_t
**nvpp
, int count
, uint64_t target_guid
)
5573 for (int i
= 0; i
< count
; i
++) {
5576 VERIFY(nvlist_lookup_uint64(nvpp
[i
], ZPOOL_CONFIG_GUID
,
5579 if (guid
== target_guid
)
5587 spa_vdev_remove_aux(nvlist_t
*config
, char *name
, nvlist_t
**dev
, int count
,
5588 nvlist_t
*dev_to_remove
)
5590 nvlist_t
**newdev
= NULL
;
5593 newdev
= kmem_alloc((count
- 1) * sizeof (void *), KM_SLEEP
);
5595 for (int i
= 0, j
= 0; i
< count
; i
++) {
5596 if (dev
[i
] == dev_to_remove
)
5598 VERIFY(nvlist_dup(dev
[i
], &newdev
[j
++], KM_SLEEP
) == 0);
5601 VERIFY(nvlist_remove(config
, name
, DATA_TYPE_NVLIST_ARRAY
) == 0);
5602 VERIFY(nvlist_add_nvlist_array(config
, name
, newdev
, count
- 1) == 0);
5604 for (int i
= 0; i
< count
- 1; i
++)
5605 nvlist_free(newdev
[i
]);
5608 kmem_free(newdev
, (count
- 1) * sizeof (void *));
5612 * Evacuate the device.
5615 spa_vdev_remove_evacuate(spa_t
*spa
, vdev_t
*vd
)
5620 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
5621 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
5622 ASSERT(vd
== vd
->vdev_top
);
5625 * Evacuate the device. We don't hold the config lock as writer
5626 * since we need to do I/O but we do keep the
5627 * spa_namespace_lock held. Once this completes the device
5628 * should no longer have any blocks allocated on it.
5630 if (vd
->vdev_islog
) {
5631 if (vd
->vdev_stat
.vs_alloc
!= 0)
5632 error
= spa_offline_log(spa
);
5634 error
= SET_ERROR(ENOTSUP
);
5641 * The evacuation succeeded. Remove any remaining MOS metadata
5642 * associated with this vdev, and wait for these changes to sync.
5644 ASSERT0(vd
->vdev_stat
.vs_alloc
);
5645 txg
= spa_vdev_config_enter(spa
);
5646 vd
->vdev_removing
= B_TRUE
;
5647 vdev_dirty_leaves(vd
, VDD_DTL
, txg
);
5648 vdev_config_dirty(vd
);
5649 spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
5655 * Complete the removal by cleaning up the namespace.
5658 spa_vdev_remove_from_namespace(spa_t
*spa
, vdev_t
*vd
)
5660 vdev_t
*rvd
= spa
->spa_root_vdev
;
5661 uint64_t id
= vd
->vdev_id
;
5662 boolean_t last_vdev
= (id
== (rvd
->vdev_children
- 1));
5664 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
5665 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
5666 ASSERT(vd
== vd
->vdev_top
);
5669 * Only remove any devices which are empty.
5671 if (vd
->vdev_stat
.vs_alloc
!= 0)
5674 (void) vdev_label_init(vd
, 0, VDEV_LABEL_REMOVE
);
5676 if (list_link_active(&vd
->vdev_state_dirty_node
))
5677 vdev_state_clean(vd
);
5678 if (list_link_active(&vd
->vdev_config_dirty_node
))
5679 vdev_config_clean(vd
);
5684 vdev_compact_children(rvd
);
5686 vd
= vdev_alloc_common(spa
, id
, 0, &vdev_hole_ops
);
5687 vdev_add_child(rvd
, vd
);
5689 vdev_config_dirty(rvd
);
5692 * Reassess the health of our root vdev.
5698 * Remove a device from the pool -
5700 * Removing a device from the vdev namespace requires several steps
5701 * and can take a significant amount of time. As a result we use
5702 * the spa_vdev_config_[enter/exit] functions which allow us to
5703 * grab and release the spa_config_lock while still holding the namespace
5704 * lock. During each step the configuration is synced out.
5706 * Currently, this supports removing only hot spares, slogs, and level 2 ARC
5710 spa_vdev_remove(spa_t
*spa
, uint64_t guid
, boolean_t unspare
)
5713 sysevent_t
*ev
= NULL
;
5714 metaslab_group_t
*mg
;
5715 nvlist_t
**spares
, **l2cache
, *nv
;
5717 uint_t nspares
, nl2cache
;
5719 boolean_t locked
= MUTEX_HELD(&spa_namespace_lock
);
5721 ASSERT(spa_writeable(spa
));
5724 txg
= spa_vdev_enter(spa
);
5726 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
5728 if (spa
->spa_spares
.sav_vdevs
!= NULL
&&
5729 nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
5730 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0 &&
5731 (nv
= spa_nvlist_lookup_by_guid(spares
, nspares
, guid
)) != NULL
) {
5733 * Only remove the hot spare if it's not currently in use
5736 if (vd
== NULL
|| unspare
) {
5738 vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
);
5739 ev
= spa_event_create(spa
, vd
, NULL
,
5740 ESC_ZFS_VDEV_REMOVE_AUX
);
5741 spa_vdev_remove_aux(spa
->spa_spares
.sav_config
,
5742 ZPOOL_CONFIG_SPARES
, spares
, nspares
, nv
);
5743 spa_load_spares(spa
);
5744 spa
->spa_spares
.sav_sync
= B_TRUE
;
5746 error
= SET_ERROR(EBUSY
);
5748 } else if (spa
->spa_l2cache
.sav_vdevs
!= NULL
&&
5749 nvlist_lookup_nvlist_array(spa
->spa_l2cache
.sav_config
,
5750 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0 &&
5751 (nv
= spa_nvlist_lookup_by_guid(l2cache
, nl2cache
, guid
)) != NULL
) {
5753 * Cache devices can always be removed.
5755 vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
);
5756 ev
= spa_event_create(spa
, vd
, NULL
, ESC_ZFS_VDEV_REMOVE_AUX
);
5757 spa_vdev_remove_aux(spa
->spa_l2cache
.sav_config
,
5758 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
, nv
);
5759 spa_load_l2cache(spa
);
5760 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
5761 } else if (vd
!= NULL
&& vd
->vdev_islog
) {
5763 ASSERT(vd
== vd
->vdev_top
);
5768 * Stop allocating from this vdev.
5770 metaslab_group_passivate(mg
);
5773 * Wait for the youngest allocations and frees to sync,
5774 * and then wait for the deferral of those frees to finish.
5776 spa_vdev_config_exit(spa
, NULL
,
5777 txg
+ TXG_CONCURRENT_STATES
+ TXG_DEFER_SIZE
, 0, FTAG
);
5780 * Attempt to evacuate the vdev.
5782 error
= spa_vdev_remove_evacuate(spa
, vd
);
5784 txg
= spa_vdev_config_enter(spa
);
5787 * If we couldn't evacuate the vdev, unwind.
5790 metaslab_group_activate(mg
);
5791 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5795 * Clean up the vdev namespace.
5797 ev
= spa_event_create(spa
, vd
, NULL
, ESC_ZFS_VDEV_REMOVE_DEV
);
5798 spa_vdev_remove_from_namespace(spa
, vd
);
5800 } else if (vd
!= NULL
) {
5802 * Normal vdevs cannot be removed (yet).
5804 error
= SET_ERROR(ENOTSUP
);
5807 * There is no vdev of any kind with the specified guid.
5809 error
= SET_ERROR(ENOENT
);
5813 error
= spa_vdev_exit(spa
, NULL
, txg
, error
);
5822 * Find any device that's done replacing, or a vdev marked 'unspare' that's
5823 * currently spared, so we can detach it.
5826 spa_vdev_resilver_done_hunt(vdev_t
*vd
)
5828 vdev_t
*newvd
, *oldvd
;
5830 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
5831 oldvd
= spa_vdev_resilver_done_hunt(vd
->vdev_child
[c
]);
5837 * Check for a completed replacement. We always consider the first
5838 * vdev in the list to be the oldest vdev, and the last one to be
5839 * the newest (see spa_vdev_attach() for how that works). In
5840 * the case where the newest vdev is faulted, we will not automatically
5841 * remove it after a resilver completes. This is OK as it will require
5842 * user intervention to determine which disk the admin wishes to keep.
5844 if (vd
->vdev_ops
== &vdev_replacing_ops
) {
5845 ASSERT(vd
->vdev_children
> 1);
5847 newvd
= vd
->vdev_child
[vd
->vdev_children
- 1];
5848 oldvd
= vd
->vdev_child
[0];
5850 if (vdev_dtl_empty(newvd
, DTL_MISSING
) &&
5851 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
5852 !vdev_dtl_required(oldvd
))
5857 * Check for a completed resilver with the 'unspare' flag set.
5859 if (vd
->vdev_ops
== &vdev_spare_ops
) {
5860 vdev_t
*first
= vd
->vdev_child
[0];
5861 vdev_t
*last
= vd
->vdev_child
[vd
->vdev_children
- 1];
5863 if (last
->vdev_unspare
) {
5866 } else if (first
->vdev_unspare
) {
5873 if (oldvd
!= NULL
&&
5874 vdev_dtl_empty(newvd
, DTL_MISSING
) &&
5875 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
5876 !vdev_dtl_required(oldvd
))
5880 * If there are more than two spares attached to a disk,
5881 * and those spares are not required, then we want to
5882 * attempt to free them up now so that they can be used
5883 * by other pools. Once we're back down to a single
5884 * disk+spare, we stop removing them.
5886 if (vd
->vdev_children
> 2) {
5887 newvd
= vd
->vdev_child
[1];
5889 if (newvd
->vdev_isspare
&& last
->vdev_isspare
&&
5890 vdev_dtl_empty(last
, DTL_MISSING
) &&
5891 vdev_dtl_empty(last
, DTL_OUTAGE
) &&
5892 !vdev_dtl_required(newvd
))
5901 spa_vdev_resilver_done(spa_t
*spa
)
5903 vdev_t
*vd
, *pvd
, *ppvd
;
5904 uint64_t guid
, sguid
, pguid
, ppguid
;
5906 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5908 while ((vd
= spa_vdev_resilver_done_hunt(spa
->spa_root_vdev
)) != NULL
) {
5909 pvd
= vd
->vdev_parent
;
5910 ppvd
= pvd
->vdev_parent
;
5911 guid
= vd
->vdev_guid
;
5912 pguid
= pvd
->vdev_guid
;
5913 ppguid
= ppvd
->vdev_guid
;
5916 * If we have just finished replacing a hot spared device, then
5917 * we need to detach the parent's first child (the original hot
5920 if (ppvd
->vdev_ops
== &vdev_spare_ops
&& pvd
->vdev_id
== 0 &&
5921 ppvd
->vdev_children
== 2) {
5922 ASSERT(pvd
->vdev_ops
== &vdev_replacing_ops
);
5923 sguid
= ppvd
->vdev_child
[1]->vdev_guid
;
5925 ASSERT(vd
->vdev_resilver_txg
== 0 || !vdev_dtl_required(vd
));
5927 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5928 if (spa_vdev_detach(spa
, guid
, pguid
, B_TRUE
) != 0)
5930 if (sguid
&& spa_vdev_detach(spa
, sguid
, ppguid
, B_TRUE
) != 0)
5932 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5935 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5939 * Update the stored path or FRU for this vdev.
5942 spa_vdev_set_common(spa_t
*spa
, uint64_t guid
, const char *value
,
5946 boolean_t sync
= B_FALSE
;
5948 ASSERT(spa_writeable(spa
));
5950 spa_vdev_state_enter(spa
, SCL_ALL
);
5952 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
5953 return (spa_vdev_state_exit(spa
, NULL
, ENOENT
));
5955 if (!vd
->vdev_ops
->vdev_op_leaf
)
5956 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
5959 if (strcmp(value
, vd
->vdev_path
) != 0) {
5960 spa_strfree(vd
->vdev_path
);
5961 vd
->vdev_path
= spa_strdup(value
);
5965 if (vd
->vdev_fru
== NULL
) {
5966 vd
->vdev_fru
= spa_strdup(value
);
5968 } else if (strcmp(value
, vd
->vdev_fru
) != 0) {
5969 spa_strfree(vd
->vdev_fru
);
5970 vd
->vdev_fru
= spa_strdup(value
);
5975 return (spa_vdev_state_exit(spa
, sync
? vd
: NULL
, 0));
5979 spa_vdev_setpath(spa_t
*spa
, uint64_t guid
, const char *newpath
)
5981 return (spa_vdev_set_common(spa
, guid
, newpath
, B_TRUE
));
5985 spa_vdev_setfru(spa_t
*spa
, uint64_t guid
, const char *newfru
)
5987 return (spa_vdev_set_common(spa
, guid
, newfru
, B_FALSE
));
5991 * ==========================================================================
5993 * ==========================================================================
5996 spa_scrub_pause_resume(spa_t
*spa
, pool_scrub_cmd_t cmd
)
5998 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
6000 if (dsl_scan_resilvering(spa
->spa_dsl_pool
))
6001 return (SET_ERROR(EBUSY
));
6003 return (dsl_scrub_set_pause_resume(spa
->spa_dsl_pool
, cmd
));
6007 spa_scan_stop(spa_t
*spa
)
6009 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
6010 if (dsl_scan_resilvering(spa
->spa_dsl_pool
))
6011 return (SET_ERROR(EBUSY
));
6012 return (dsl_scan_cancel(spa
->spa_dsl_pool
));
6016 spa_scan(spa_t
*spa
, pool_scan_func_t func
)
6018 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
6020 if (func
>= POOL_SCAN_FUNCS
|| func
== POOL_SCAN_NONE
)
6021 return (SET_ERROR(ENOTSUP
));
6024 * If a resilver was requested, but there is no DTL on a
6025 * writeable leaf device, we have nothing to do.
6027 if (func
== POOL_SCAN_RESILVER
&&
6028 !vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
)) {
6029 spa_async_request(spa
, SPA_ASYNC_RESILVER_DONE
);
6033 return (dsl_scan(spa
->spa_dsl_pool
, func
));
6037 * ==========================================================================
6038 * SPA async task processing
6039 * ==========================================================================
6043 spa_async_remove(spa_t
*spa
, vdev_t
*vd
)
6045 if (vd
->vdev_remove_wanted
) {
6046 vd
->vdev_remove_wanted
= B_FALSE
;
6047 vd
->vdev_delayed_close
= B_FALSE
;
6048 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_REMOVED
, VDEV_AUX_NONE
);
6051 * We want to clear the stats, but we don't want to do a full
6052 * vdev_clear() as that will cause us to throw away
6053 * degraded/faulted state as well as attempt to reopen the
6054 * device, all of which is a waste.
6056 vd
->vdev_stat
.vs_read_errors
= 0;
6057 vd
->vdev_stat
.vs_write_errors
= 0;
6058 vd
->vdev_stat
.vs_checksum_errors
= 0;
6060 vdev_state_dirty(vd
->vdev_top
);
6063 for (int c
= 0; c
< vd
->vdev_children
; c
++)
6064 spa_async_remove(spa
, vd
->vdev_child
[c
]);
6068 spa_async_probe(spa_t
*spa
, vdev_t
*vd
)
6070 if (vd
->vdev_probe_wanted
) {
6071 vd
->vdev_probe_wanted
= B_FALSE
;
6072 vdev_reopen(vd
); /* vdev_open() does the actual probe */
6075 for (int c
= 0; c
< vd
->vdev_children
; c
++)
6076 spa_async_probe(spa
, vd
->vdev_child
[c
]);
6080 spa_async_autoexpand(spa_t
*spa
, vdev_t
*vd
)
6082 if (!spa
->spa_autoexpand
)
6085 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
6086 vdev_t
*cvd
= vd
->vdev_child
[c
];
6087 spa_async_autoexpand(spa
, cvd
);
6090 if (!vd
->vdev_ops
->vdev_op_leaf
|| vd
->vdev_physpath
== NULL
)
6093 spa_event_notify(vd
->vdev_spa
, vd
, NULL
, ESC_ZFS_VDEV_AUTOEXPAND
);
6097 spa_async_thread(void *arg
)
6099 spa_t
*spa
= (spa_t
*)arg
;
6102 ASSERT(spa
->spa_sync_on
);
6104 mutex_enter(&spa
->spa_async_lock
);
6105 tasks
= spa
->spa_async_tasks
;
6106 spa
->spa_async_tasks
= 0;
6107 mutex_exit(&spa
->spa_async_lock
);
6110 * See if the config needs to be updated.
6112 if (tasks
& SPA_ASYNC_CONFIG_UPDATE
) {
6113 uint64_t old_space
, new_space
;
6115 mutex_enter(&spa_namespace_lock
);
6116 old_space
= metaslab_class_get_space(spa_normal_class(spa
));
6117 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
6118 new_space
= metaslab_class_get_space(spa_normal_class(spa
));
6119 mutex_exit(&spa_namespace_lock
);
6122 * If the pool grew as a result of the config update,
6123 * then log an internal history event.
6125 if (new_space
!= old_space
) {
6126 spa_history_log_internal(spa
, "vdev online", NULL
,
6127 "pool '%s' size: %llu(+%llu)",
6128 spa_name(spa
), new_space
, new_space
- old_space
);
6133 * See if any devices need to be marked REMOVED.
6135 if (tasks
& SPA_ASYNC_REMOVE
) {
6136 spa_vdev_state_enter(spa
, SCL_NONE
);
6137 spa_async_remove(spa
, spa
->spa_root_vdev
);
6138 for (int i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++)
6139 spa_async_remove(spa
, spa
->spa_l2cache
.sav_vdevs
[i
]);
6140 for (int i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
6141 spa_async_remove(spa
, spa
->spa_spares
.sav_vdevs
[i
]);
6142 (void) spa_vdev_state_exit(spa
, NULL
, 0);
6145 if ((tasks
& SPA_ASYNC_AUTOEXPAND
) && !spa_suspended(spa
)) {
6146 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
6147 spa_async_autoexpand(spa
, spa
->spa_root_vdev
);
6148 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
6152 * See if any devices need to be probed.
6154 if (tasks
& SPA_ASYNC_PROBE
) {
6155 spa_vdev_state_enter(spa
, SCL_NONE
);
6156 spa_async_probe(spa
, spa
->spa_root_vdev
);
6157 (void) spa_vdev_state_exit(spa
, NULL
, 0);
6161 * If any devices are done replacing, detach them.
6163 if (tasks
& SPA_ASYNC_RESILVER_DONE
)
6164 spa_vdev_resilver_done(spa
);
6167 * Kick off a resilver.
6169 if (tasks
& SPA_ASYNC_RESILVER
)
6170 dsl_resilver_restart(spa
->spa_dsl_pool
, 0);
6173 * Let the world know that we're done.
6175 mutex_enter(&spa
->spa_async_lock
);
6176 spa
->spa_async_thread
= NULL
;
6177 cv_broadcast(&spa
->spa_async_cv
);
6178 mutex_exit(&spa
->spa_async_lock
);
6183 spa_async_suspend(spa_t
*spa
)
6185 mutex_enter(&spa
->spa_async_lock
);
6186 spa
->spa_async_suspended
++;
6187 while (spa
->spa_async_thread
!= NULL
)
6188 cv_wait(&spa
->spa_async_cv
, &spa
->spa_async_lock
);
6189 mutex_exit(&spa
->spa_async_lock
);
6193 spa_async_resume(spa_t
*spa
)
6195 mutex_enter(&spa
->spa_async_lock
);
6196 ASSERT(spa
->spa_async_suspended
!= 0);
6197 spa
->spa_async_suspended
--;
6198 mutex_exit(&spa
->spa_async_lock
);
6202 spa_async_tasks_pending(spa_t
*spa
)
6204 uint_t non_config_tasks
;
6206 boolean_t config_task_suspended
;
6208 non_config_tasks
= spa
->spa_async_tasks
& ~SPA_ASYNC_CONFIG_UPDATE
;
6209 config_task
= spa
->spa_async_tasks
& SPA_ASYNC_CONFIG_UPDATE
;
6210 if (spa
->spa_ccw_fail_time
== 0) {
6211 config_task_suspended
= B_FALSE
;
6213 config_task_suspended
=
6214 (gethrtime() - spa
->spa_ccw_fail_time
) <
6215 ((hrtime_t
)zfs_ccw_retry_interval
* NANOSEC
);
6218 return (non_config_tasks
|| (config_task
&& !config_task_suspended
));
6222 spa_async_dispatch(spa_t
*spa
)
6224 mutex_enter(&spa
->spa_async_lock
);
6225 if (spa_async_tasks_pending(spa
) &&
6226 !spa
->spa_async_suspended
&&
6227 spa
->spa_async_thread
== NULL
&&
6229 spa
->spa_async_thread
= thread_create(NULL
, 0,
6230 spa_async_thread
, spa
, 0, &p0
, TS_RUN
, maxclsyspri
);
6231 mutex_exit(&spa
->spa_async_lock
);
6235 spa_async_request(spa_t
*spa
, int task
)
6237 zfs_dbgmsg("spa=%s async request task=%u", spa
->spa_name
, task
);
6238 mutex_enter(&spa
->spa_async_lock
);
6239 spa
->spa_async_tasks
|= task
;
6240 mutex_exit(&spa
->spa_async_lock
);
6244 * ==========================================================================
6245 * SPA syncing routines
6246 * ==========================================================================
6250 bpobj_enqueue_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
6253 bpobj_enqueue(bpo
, bp
, tx
);
6258 spa_free_sync_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
6262 zio_nowait(zio_free_sync(zio
, zio
->io_spa
, dmu_tx_get_txg(tx
), bp
,
6268 * Note: this simple function is not inlined to make it easier to dtrace the
6269 * amount of time spent syncing frees.
6272 spa_sync_frees(spa_t
*spa
, bplist_t
*bpl
, dmu_tx_t
*tx
)
6274 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
6275 bplist_iterate(bpl
, spa_free_sync_cb
, zio
, tx
);
6276 VERIFY(zio_wait(zio
) == 0);
6280 * Note: this simple function is not inlined to make it easier to dtrace the
6281 * amount of time spent syncing deferred frees.
6284 spa_sync_deferred_frees(spa_t
*spa
, dmu_tx_t
*tx
)
6286 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
6287 VERIFY3U(bpobj_iterate(&spa
->spa_deferred_bpobj
,
6288 spa_free_sync_cb
, zio
, tx
), ==, 0);
6289 VERIFY0(zio_wait(zio
));
6293 spa_sync_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
*nv
, dmu_tx_t
*tx
)
6295 char *packed
= NULL
;
6300 VERIFY(nvlist_size(nv
, &nvsize
, NV_ENCODE_XDR
) == 0);
6303 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
6304 * information. This avoids the dmu_buf_will_dirty() path and
6305 * saves us a pre-read to get data we don't actually care about.
6307 bufsize
= P2ROUNDUP((uint64_t)nvsize
, SPA_CONFIG_BLOCKSIZE
);
6308 packed
= vmem_alloc(bufsize
, KM_SLEEP
);
6310 VERIFY(nvlist_pack(nv
, &packed
, &nvsize
, NV_ENCODE_XDR
,
6312 bzero(packed
+ nvsize
, bufsize
- nvsize
);
6314 dmu_write(spa
->spa_meta_objset
, obj
, 0, bufsize
, packed
, tx
);
6316 vmem_free(packed
, bufsize
);
6318 VERIFY(0 == dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
));
6319 dmu_buf_will_dirty(db
, tx
);
6320 *(uint64_t *)db
->db_data
= nvsize
;
6321 dmu_buf_rele(db
, FTAG
);
6325 spa_sync_aux_dev(spa_t
*spa
, spa_aux_vdev_t
*sav
, dmu_tx_t
*tx
,
6326 const char *config
, const char *entry
)
6336 * Update the MOS nvlist describing the list of available devices.
6337 * spa_validate_aux() will have already made sure this nvlist is
6338 * valid and the vdevs are labeled appropriately.
6340 if (sav
->sav_object
== 0) {
6341 sav
->sav_object
= dmu_object_alloc(spa
->spa_meta_objset
,
6342 DMU_OT_PACKED_NVLIST
, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE
,
6343 sizeof (uint64_t), tx
);
6344 VERIFY(zap_update(spa
->spa_meta_objset
,
6345 DMU_POOL_DIRECTORY_OBJECT
, entry
, sizeof (uint64_t), 1,
6346 &sav
->sav_object
, tx
) == 0);
6349 VERIFY(nvlist_alloc(&nvroot
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
6350 if (sav
->sav_count
== 0) {
6351 VERIFY(nvlist_add_nvlist_array(nvroot
, config
, NULL
, 0) == 0);
6353 list
= kmem_alloc(sav
->sav_count
*sizeof (void *), KM_SLEEP
);
6354 for (i
= 0; i
< sav
->sav_count
; i
++)
6355 list
[i
] = vdev_config_generate(spa
, sav
->sav_vdevs
[i
],
6356 B_FALSE
, VDEV_CONFIG_L2CACHE
);
6357 VERIFY(nvlist_add_nvlist_array(nvroot
, config
, list
,
6358 sav
->sav_count
) == 0);
6359 for (i
= 0; i
< sav
->sav_count
; i
++)
6360 nvlist_free(list
[i
]);
6361 kmem_free(list
, sav
->sav_count
* sizeof (void *));
6364 spa_sync_nvlist(spa
, sav
->sav_object
, nvroot
, tx
);
6365 nvlist_free(nvroot
);
6367 sav
->sav_sync
= B_FALSE
;
6371 * Rebuild spa's all-vdev ZAP from the vdev ZAPs indicated in each vdev_t.
6372 * The all-vdev ZAP must be empty.
6375 spa_avz_build(vdev_t
*vd
, uint64_t avz
, dmu_tx_t
*tx
)
6377 spa_t
*spa
= vd
->vdev_spa
;
6379 if (vd
->vdev_top_zap
!= 0) {
6380 VERIFY0(zap_add_int(spa
->spa_meta_objset
, avz
,
6381 vd
->vdev_top_zap
, tx
));
6383 if (vd
->vdev_leaf_zap
!= 0) {
6384 VERIFY0(zap_add_int(spa
->spa_meta_objset
, avz
,
6385 vd
->vdev_leaf_zap
, tx
));
6387 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
6388 spa_avz_build(vd
->vdev_child
[i
], avz
, tx
);
6393 spa_sync_config_object(spa_t
*spa
, dmu_tx_t
*tx
)
6398 * If the pool is being imported from a pre-per-vdev-ZAP version of ZFS,
6399 * its config may not be dirty but we still need to build per-vdev ZAPs.
6400 * Similarly, if the pool is being assembled (e.g. after a split), we
6401 * need to rebuild the AVZ although the config may not be dirty.
6403 if (list_is_empty(&spa
->spa_config_dirty_list
) &&
6404 spa
->spa_avz_action
== AVZ_ACTION_NONE
)
6407 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
6409 ASSERT(spa
->spa_avz_action
== AVZ_ACTION_NONE
||
6410 spa
->spa_avz_action
== AVZ_ACTION_INITIALIZE
||
6411 spa
->spa_all_vdev_zaps
!= 0);
6413 if (spa
->spa_avz_action
== AVZ_ACTION_REBUILD
) {
6414 /* Make and build the new AVZ */
6415 uint64_t new_avz
= zap_create(spa
->spa_meta_objset
,
6416 DMU_OTN_ZAP_METADATA
, DMU_OT_NONE
, 0, tx
);
6417 spa_avz_build(spa
->spa_root_vdev
, new_avz
, tx
);
6419 /* Diff old AVZ with new one */
6423 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
6424 spa
->spa_all_vdev_zaps
);
6425 zap_cursor_retrieve(&zc
, &za
) == 0;
6426 zap_cursor_advance(&zc
)) {
6427 uint64_t vdzap
= za
.za_first_integer
;
6428 if (zap_lookup_int(spa
->spa_meta_objset
, new_avz
,
6431 * ZAP is listed in old AVZ but not in new one;
6434 VERIFY0(zap_destroy(spa
->spa_meta_objset
, vdzap
,
6439 zap_cursor_fini(&zc
);
6441 /* Destroy the old AVZ */
6442 VERIFY0(zap_destroy(spa
->spa_meta_objset
,
6443 spa
->spa_all_vdev_zaps
, tx
));
6445 /* Replace the old AVZ in the dir obj with the new one */
6446 VERIFY0(zap_update(spa
->spa_meta_objset
,
6447 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_VDEV_ZAP_MAP
,
6448 sizeof (new_avz
), 1, &new_avz
, tx
));
6450 spa
->spa_all_vdev_zaps
= new_avz
;
6451 } else if (spa
->spa_avz_action
== AVZ_ACTION_DESTROY
) {
6455 /* Walk through the AVZ and destroy all listed ZAPs */
6456 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
6457 spa
->spa_all_vdev_zaps
);
6458 zap_cursor_retrieve(&zc
, &za
) == 0;
6459 zap_cursor_advance(&zc
)) {
6460 uint64_t zap
= za
.za_first_integer
;
6461 VERIFY0(zap_destroy(spa
->spa_meta_objset
, zap
, tx
));
6464 zap_cursor_fini(&zc
);
6466 /* Destroy and unlink the AVZ itself */
6467 VERIFY0(zap_destroy(spa
->spa_meta_objset
,
6468 spa
->spa_all_vdev_zaps
, tx
));
6469 VERIFY0(zap_remove(spa
->spa_meta_objset
,
6470 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_VDEV_ZAP_MAP
, tx
));
6471 spa
->spa_all_vdev_zaps
= 0;
6474 if (spa
->spa_all_vdev_zaps
== 0) {
6475 spa
->spa_all_vdev_zaps
= zap_create_link(spa
->spa_meta_objset
,
6476 DMU_OTN_ZAP_METADATA
, DMU_POOL_DIRECTORY_OBJECT
,
6477 DMU_POOL_VDEV_ZAP_MAP
, tx
);
6479 spa
->spa_avz_action
= AVZ_ACTION_NONE
;
6481 /* Create ZAPs for vdevs that don't have them. */
6482 vdev_construct_zaps(spa
->spa_root_vdev
, tx
);
6484 config
= spa_config_generate(spa
, spa
->spa_root_vdev
,
6485 dmu_tx_get_txg(tx
), B_FALSE
);
6488 * If we're upgrading the spa version then make sure that
6489 * the config object gets updated with the correct version.
6491 if (spa
->spa_ubsync
.ub_version
< spa
->spa_uberblock
.ub_version
)
6492 fnvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
6493 spa
->spa_uberblock
.ub_version
);
6495 spa_config_exit(spa
, SCL_STATE
, FTAG
);
6497 nvlist_free(spa
->spa_config_syncing
);
6498 spa
->spa_config_syncing
= config
;
6500 spa_sync_nvlist(spa
, spa
->spa_config_object
, config
, tx
);
6504 spa_sync_version(void *arg
, dmu_tx_t
*tx
)
6506 uint64_t *versionp
= arg
;
6507 uint64_t version
= *versionp
;
6508 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
6511 * Setting the version is special cased when first creating the pool.
6513 ASSERT(tx
->tx_txg
!= TXG_INITIAL
);
6515 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
6516 ASSERT(version
>= spa_version(spa
));
6518 spa
->spa_uberblock
.ub_version
= version
;
6519 vdev_config_dirty(spa
->spa_root_vdev
);
6520 spa_history_log_internal(spa
, "set", tx
, "version=%lld", version
);
6524 * Set zpool properties.
6527 spa_sync_props(void *arg
, dmu_tx_t
*tx
)
6529 nvlist_t
*nvp
= arg
;
6530 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
6531 objset_t
*mos
= spa
->spa_meta_objset
;
6532 nvpair_t
*elem
= NULL
;
6534 mutex_enter(&spa
->spa_props_lock
);
6536 while ((elem
= nvlist_next_nvpair(nvp
, elem
))) {
6538 char *strval
, *fname
;
6540 const char *propname
;
6541 zprop_type_t proptype
;
6544 switch (prop
= zpool_name_to_prop(nvpair_name(elem
))) {
6545 case ZPOOL_PROP_INVAL
:
6547 * We checked this earlier in spa_prop_validate().
6549 ASSERT(zpool_prop_feature(nvpair_name(elem
)));
6551 fname
= strchr(nvpair_name(elem
), '@') + 1;
6552 VERIFY0(zfeature_lookup_name(fname
, &fid
));
6554 spa_feature_enable(spa
, fid
, tx
);
6555 spa_history_log_internal(spa
, "set", tx
,
6556 "%s=enabled", nvpair_name(elem
));
6559 case ZPOOL_PROP_VERSION
:
6560 intval
= fnvpair_value_uint64(elem
);
6562 * The version is synced separately before other
6563 * properties and should be correct by now.
6565 ASSERT3U(spa_version(spa
), >=, intval
);
6568 case ZPOOL_PROP_ALTROOT
:
6570 * 'altroot' is a non-persistent property. It should
6571 * have been set temporarily at creation or import time.
6573 ASSERT(spa
->spa_root
!= NULL
);
6576 case ZPOOL_PROP_READONLY
:
6577 case ZPOOL_PROP_CACHEFILE
:
6579 * 'readonly' and 'cachefile' are also non-persisitent
6583 case ZPOOL_PROP_COMMENT
:
6584 strval
= fnvpair_value_string(elem
);
6585 if (spa
->spa_comment
!= NULL
)
6586 spa_strfree(spa
->spa_comment
);
6587 spa
->spa_comment
= spa_strdup(strval
);
6589 * We need to dirty the configuration on all the vdevs
6590 * so that their labels get updated. It's unnecessary
6591 * to do this for pool creation since the vdev's
6592 * configuration has already been dirtied.
6594 if (tx
->tx_txg
!= TXG_INITIAL
)
6595 vdev_config_dirty(spa
->spa_root_vdev
);
6596 spa_history_log_internal(spa
, "set", tx
,
6597 "%s=%s", nvpair_name(elem
), strval
);
6601 * Set pool property values in the poolprops mos object.
6603 if (spa
->spa_pool_props_object
== 0) {
6604 spa
->spa_pool_props_object
=
6605 zap_create_link(mos
, DMU_OT_POOL_PROPS
,
6606 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_PROPS
,
6610 /* normalize the property name */
6611 propname
= zpool_prop_to_name(prop
);
6612 proptype
= zpool_prop_get_type(prop
);
6614 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
6615 ASSERT(proptype
== PROP_TYPE_STRING
);
6616 strval
= fnvpair_value_string(elem
);
6617 VERIFY0(zap_update(mos
,
6618 spa
->spa_pool_props_object
, propname
,
6619 1, strlen(strval
) + 1, strval
, tx
));
6620 spa_history_log_internal(spa
, "set", tx
,
6621 "%s=%s", nvpair_name(elem
), strval
);
6622 } else if (nvpair_type(elem
) == DATA_TYPE_UINT64
) {
6623 intval
= fnvpair_value_uint64(elem
);
6625 if (proptype
== PROP_TYPE_INDEX
) {
6627 VERIFY0(zpool_prop_index_to_string(
6628 prop
, intval
, &unused
));
6630 VERIFY0(zap_update(mos
,
6631 spa
->spa_pool_props_object
, propname
,
6632 8, 1, &intval
, tx
));
6633 spa_history_log_internal(spa
, "set", tx
,
6634 "%s=%lld", nvpair_name(elem
), intval
);
6636 ASSERT(0); /* not allowed */
6640 case ZPOOL_PROP_DELEGATION
:
6641 spa
->spa_delegation
= intval
;
6643 case ZPOOL_PROP_BOOTFS
:
6644 spa
->spa_bootfs
= intval
;
6646 case ZPOOL_PROP_FAILUREMODE
:
6647 spa
->spa_failmode
= intval
;
6649 case ZPOOL_PROP_AUTOEXPAND
:
6650 spa
->spa_autoexpand
= intval
;
6651 if (tx
->tx_txg
!= TXG_INITIAL
)
6652 spa_async_request(spa
,
6653 SPA_ASYNC_AUTOEXPAND
);
6655 case ZPOOL_PROP_MULTIHOST
:
6656 spa
->spa_multihost
= intval
;
6658 case ZPOOL_PROP_DEDUPDITTO
:
6659 spa
->spa_dedup_ditto
= intval
;
6668 mutex_exit(&spa
->spa_props_lock
);
6672 * Perform one-time upgrade on-disk changes. spa_version() does not
6673 * reflect the new version this txg, so there must be no changes this
6674 * txg to anything that the upgrade code depends on after it executes.
6675 * Therefore this must be called after dsl_pool_sync() does the sync
6679 spa_sync_upgrades(spa_t
*spa
, dmu_tx_t
*tx
)
6681 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
6683 ASSERT(spa
->spa_sync_pass
== 1);
6685 rrw_enter(&dp
->dp_config_rwlock
, RW_WRITER
, FTAG
);
6687 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_ORIGIN
&&
6688 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_ORIGIN
) {
6689 dsl_pool_create_origin(dp
, tx
);
6691 /* Keeping the origin open increases spa_minref */
6692 spa
->spa_minref
+= 3;
6695 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_NEXT_CLONES
&&
6696 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_NEXT_CLONES
) {
6697 dsl_pool_upgrade_clones(dp
, tx
);
6700 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_DIR_CLONES
&&
6701 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_DIR_CLONES
) {
6702 dsl_pool_upgrade_dir_clones(dp
, tx
);
6704 /* Keeping the freedir open increases spa_minref */
6705 spa
->spa_minref
+= 3;
6708 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_FEATURES
&&
6709 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
6710 spa_feature_create_zap_objects(spa
, tx
);
6714 * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable
6715 * when possibility to use lz4 compression for metadata was added
6716 * Old pools that have this feature enabled must be upgraded to have
6717 * this feature active
6719 if (spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
6720 boolean_t lz4_en
= spa_feature_is_enabled(spa
,
6721 SPA_FEATURE_LZ4_COMPRESS
);
6722 boolean_t lz4_ac
= spa_feature_is_active(spa
,
6723 SPA_FEATURE_LZ4_COMPRESS
);
6725 if (lz4_en
&& !lz4_ac
)
6726 spa_feature_incr(spa
, SPA_FEATURE_LZ4_COMPRESS
, tx
);
6730 * If we haven't written the salt, do so now. Note that the
6731 * feature may not be activated yet, but that's fine since
6732 * the presence of this ZAP entry is backwards compatible.
6734 if (zap_contains(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
6735 DMU_POOL_CHECKSUM_SALT
) == ENOENT
) {
6736 VERIFY0(zap_add(spa
->spa_meta_objset
,
6737 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CHECKSUM_SALT
, 1,
6738 sizeof (spa
->spa_cksum_salt
.zcs_bytes
),
6739 spa
->spa_cksum_salt
.zcs_bytes
, tx
));
6742 rrw_exit(&dp
->dp_config_rwlock
, FTAG
);
6746 * Sync the specified transaction group. New blocks may be dirtied as
6747 * part of the process, so we iterate until it converges.
6750 spa_sync(spa_t
*spa
, uint64_t txg
)
6752 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
6753 objset_t
*mos
= spa
->spa_meta_objset
;
6754 bplist_t
*free_bpl
= &spa
->spa_free_bplist
[txg
& TXG_MASK
];
6755 vdev_t
*rvd
= spa
->spa_root_vdev
;
6759 uint32_t max_queue_depth
= zfs_vdev_async_write_max_active
*
6760 zfs_vdev_queue_depth_pct
/ 100;
6762 VERIFY(spa_writeable(spa
));
6765 * Lock out configuration changes.
6767 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
6769 spa
->spa_syncing_txg
= txg
;
6770 spa
->spa_sync_pass
= 0;
6772 mutex_enter(&spa
->spa_alloc_lock
);
6773 VERIFY0(avl_numnodes(&spa
->spa_alloc_tree
));
6774 mutex_exit(&spa
->spa_alloc_lock
);
6777 * If there are any pending vdev state changes, convert them
6778 * into config changes that go out with this transaction group.
6780 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
6781 while (list_head(&spa
->spa_state_dirty_list
) != NULL
) {
6783 * We need the write lock here because, for aux vdevs,
6784 * calling vdev_config_dirty() modifies sav_config.
6785 * This is ugly and will become unnecessary when we
6786 * eliminate the aux vdev wart by integrating all vdevs
6787 * into the root vdev tree.
6789 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
6790 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_WRITER
);
6791 while ((vd
= list_head(&spa
->spa_state_dirty_list
)) != NULL
) {
6792 vdev_state_clean(vd
);
6793 vdev_config_dirty(vd
);
6795 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
6796 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
6798 spa_config_exit(spa
, SCL_STATE
, FTAG
);
6800 tx
= dmu_tx_create_assigned(dp
, txg
);
6802 spa
->spa_sync_starttime
= gethrtime();
6803 taskq_cancel_id(system_delay_taskq
, spa
->spa_deadman_tqid
);
6804 spa
->spa_deadman_tqid
= taskq_dispatch_delay(system_delay_taskq
,
6805 spa_deadman
, spa
, TQ_SLEEP
, ddi_get_lbolt() +
6806 NSEC_TO_TICK(spa
->spa_deadman_synctime
));
6809 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
6810 * set spa_deflate if we have no raid-z vdevs.
6812 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_RAIDZ_DEFLATE
&&
6813 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
6816 for (i
= 0; i
< rvd
->vdev_children
; i
++) {
6817 vd
= rvd
->vdev_child
[i
];
6818 if (vd
->vdev_deflate_ratio
!= SPA_MINBLOCKSIZE
)
6821 if (i
== rvd
->vdev_children
) {
6822 spa
->spa_deflate
= TRUE
;
6823 VERIFY(0 == zap_add(spa
->spa_meta_objset
,
6824 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
6825 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
));
6830 * Set the top-level vdev's max queue depth. Evaluate each
6831 * top-level's async write queue depth in case it changed.
6832 * The max queue depth will not change in the middle of syncing
6835 uint64_t queue_depth_total
= 0;
6836 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
6837 vdev_t
*tvd
= rvd
->vdev_child
[c
];
6838 metaslab_group_t
*mg
= tvd
->vdev_mg
;
6840 if (mg
== NULL
|| mg
->mg_class
!= spa_normal_class(spa
) ||
6841 !metaslab_group_initialized(mg
))
6845 * It is safe to do a lock-free check here because only async
6846 * allocations look at mg_max_alloc_queue_depth, and async
6847 * allocations all happen from spa_sync().
6849 ASSERT0(refcount_count(&mg
->mg_alloc_queue_depth
));
6850 mg
->mg_max_alloc_queue_depth
= max_queue_depth
;
6851 queue_depth_total
+= mg
->mg_max_alloc_queue_depth
;
6853 metaslab_class_t
*mc
= spa_normal_class(spa
);
6854 ASSERT0(refcount_count(&mc
->mc_alloc_slots
));
6855 mc
->mc_alloc_max_slots
= queue_depth_total
;
6856 mc
->mc_alloc_throttle_enabled
= zio_dva_throttle_enabled
;
6858 ASSERT3U(mc
->mc_alloc_max_slots
, <=,
6859 max_queue_depth
* rvd
->vdev_children
);
6862 * Iterate to convergence.
6865 int pass
= ++spa
->spa_sync_pass
;
6867 spa_sync_config_object(spa
, tx
);
6868 spa_sync_aux_dev(spa
, &spa
->spa_spares
, tx
,
6869 ZPOOL_CONFIG_SPARES
, DMU_POOL_SPARES
);
6870 spa_sync_aux_dev(spa
, &spa
->spa_l2cache
, tx
,
6871 ZPOOL_CONFIG_L2CACHE
, DMU_POOL_L2CACHE
);
6872 spa_errlog_sync(spa
, txg
);
6873 dsl_pool_sync(dp
, txg
);
6875 if (pass
< zfs_sync_pass_deferred_free
) {
6876 spa_sync_frees(spa
, free_bpl
, tx
);
6879 * We can not defer frees in pass 1, because
6880 * we sync the deferred frees later in pass 1.
6882 ASSERT3U(pass
, >, 1);
6883 bplist_iterate(free_bpl
, bpobj_enqueue_cb
,
6884 &spa
->spa_deferred_bpobj
, tx
);
6888 dsl_scan_sync(dp
, tx
);
6890 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, txg
)))
6894 spa_sync_upgrades(spa
, tx
);
6896 spa
->spa_uberblock
.ub_rootbp
.blk_birth
);
6898 * Note: We need to check if the MOS is dirty
6899 * because we could have marked the MOS dirty
6900 * without updating the uberblock (e.g. if we
6901 * have sync tasks but no dirty user data). We
6902 * need to check the uberblock's rootbp because
6903 * it is updated if we have synced out dirty
6904 * data (though in this case the MOS will most
6905 * likely also be dirty due to second order
6906 * effects, we don't want to rely on that here).
6908 if (spa
->spa_uberblock
.ub_rootbp
.blk_birth
< txg
&&
6909 !dmu_objset_is_dirty(mos
, txg
)) {
6911 * Nothing changed on the first pass,
6912 * therefore this TXG is a no-op. Avoid
6913 * syncing deferred frees, so that we
6914 * can keep this TXG as a no-op.
6916 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
,
6918 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
6919 ASSERT(txg_list_empty(&dp
->dp_sync_tasks
, txg
));
6922 spa_sync_deferred_frees(spa
, tx
);
6925 } while (dmu_objset_is_dirty(mos
, txg
));
6928 if (!list_is_empty(&spa
->spa_config_dirty_list
)) {
6930 * Make sure that the number of ZAPs for all the vdevs matches
6931 * the number of ZAPs in the per-vdev ZAP list. This only gets
6932 * called if the config is dirty; otherwise there may be
6933 * outstanding AVZ operations that weren't completed in
6934 * spa_sync_config_object.
6936 uint64_t all_vdev_zap_entry_count
;
6937 ASSERT0(zap_count(spa
->spa_meta_objset
,
6938 spa
->spa_all_vdev_zaps
, &all_vdev_zap_entry_count
));
6939 ASSERT3U(vdev_count_verify_zaps(spa
->spa_root_vdev
), ==,
6940 all_vdev_zap_entry_count
);
6945 * Rewrite the vdev configuration (which includes the uberblock)
6946 * to commit the transaction group.
6948 * If there are no dirty vdevs, we sync the uberblock to a few
6949 * random top-level vdevs that are known to be visible in the
6950 * config cache (see spa_vdev_add() for a complete description).
6951 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
6955 * We hold SCL_STATE to prevent vdev open/close/etc.
6956 * while we're attempting to write the vdev labels.
6958 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
6960 if (list_is_empty(&spa
->spa_config_dirty_list
)) {
6961 vdev_t
*svd
[SPA_DVAS_PER_BP
];
6963 int children
= rvd
->vdev_children
;
6964 int c0
= spa_get_random(children
);
6966 for (int c
= 0; c
< children
; c
++) {
6967 vd
= rvd
->vdev_child
[(c0
+ c
) % children
];
6968 if (vd
->vdev_ms_array
== 0 || vd
->vdev_islog
)
6970 svd
[svdcount
++] = vd
;
6971 if (svdcount
== SPA_DVAS_PER_BP
)
6974 error
= vdev_config_sync(svd
, svdcount
, txg
);
6976 error
= vdev_config_sync(rvd
->vdev_child
,
6977 rvd
->vdev_children
, txg
);
6981 spa
->spa_last_synced_guid
= rvd
->vdev_guid
;
6983 spa_config_exit(spa
, SCL_STATE
, FTAG
);
6987 zio_suspend(spa
, NULL
);
6988 zio_resume_wait(spa
);
6992 taskq_cancel_id(system_delay_taskq
, spa
->spa_deadman_tqid
);
6993 spa
->spa_deadman_tqid
= 0;
6996 * Clear the dirty config list.
6998 while ((vd
= list_head(&spa
->spa_config_dirty_list
)) != NULL
)
6999 vdev_config_clean(vd
);
7002 * Now that the new config has synced transactionally,
7003 * let it become visible to the config cache.
7005 if (spa
->spa_config_syncing
!= NULL
) {
7006 spa_config_set(spa
, spa
->spa_config_syncing
);
7007 spa
->spa_config_txg
= txg
;
7008 spa
->spa_config_syncing
= NULL
;
7011 dsl_pool_sync_done(dp
, txg
);
7013 mutex_enter(&spa
->spa_alloc_lock
);
7014 VERIFY0(avl_numnodes(&spa
->spa_alloc_tree
));
7015 mutex_exit(&spa
->spa_alloc_lock
);
7018 * Update usable space statistics.
7020 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, TXG_CLEAN(txg
))))
7021 vdev_sync_done(vd
, txg
);
7023 spa_update_dspace(spa
);
7026 * It had better be the case that we didn't dirty anything
7027 * since vdev_config_sync().
7029 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
, txg
));
7030 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
7031 ASSERT(txg_list_empty(&spa
->spa_vdev_txg_list
, txg
));
7033 spa
->spa_sync_pass
= 0;
7036 * Update the last synced uberblock here. We want to do this at
7037 * the end of spa_sync() so that consumers of spa_last_synced_txg()
7038 * will be guaranteed that all the processing associated with
7039 * that txg has been completed.
7041 spa
->spa_ubsync
= spa
->spa_uberblock
;
7042 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
7044 spa_handle_ignored_writes(spa
);
7047 * If any async tasks have been requested, kick them off.
7049 spa_async_dispatch(spa
);
7053 * Sync all pools. We don't want to hold the namespace lock across these
7054 * operations, so we take a reference on the spa_t and drop the lock during the
7058 spa_sync_allpools(void)
7061 mutex_enter(&spa_namespace_lock
);
7062 while ((spa
= spa_next(spa
)) != NULL
) {
7063 if (spa_state(spa
) != POOL_STATE_ACTIVE
||
7064 !spa_writeable(spa
) || spa_suspended(spa
))
7066 spa_open_ref(spa
, FTAG
);
7067 mutex_exit(&spa_namespace_lock
);
7068 txg_wait_synced(spa_get_dsl(spa
), 0);
7069 mutex_enter(&spa_namespace_lock
);
7070 spa_close(spa
, FTAG
);
7072 mutex_exit(&spa_namespace_lock
);
7076 * ==========================================================================
7077 * Miscellaneous routines
7078 * ==========================================================================
7082 * Remove all pools in the system.
7090 * Remove all cached state. All pools should be closed now,
7091 * so every spa in the AVL tree should be unreferenced.
7093 mutex_enter(&spa_namespace_lock
);
7094 while ((spa
= spa_next(NULL
)) != NULL
) {
7096 * Stop async tasks. The async thread may need to detach
7097 * a device that's been replaced, which requires grabbing
7098 * spa_namespace_lock, so we must drop it here.
7100 spa_open_ref(spa
, FTAG
);
7101 mutex_exit(&spa_namespace_lock
);
7102 spa_async_suspend(spa
);
7103 mutex_enter(&spa_namespace_lock
);
7104 spa_close(spa
, FTAG
);
7106 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
7108 spa_deactivate(spa
);
7112 mutex_exit(&spa_namespace_lock
);
7116 spa_lookup_by_guid(spa_t
*spa
, uint64_t guid
, boolean_t aux
)
7121 if ((vd
= vdev_lookup_by_guid(spa
->spa_root_vdev
, guid
)) != NULL
)
7125 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
7126 vd
= spa
->spa_l2cache
.sav_vdevs
[i
];
7127 if (vd
->vdev_guid
== guid
)
7131 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
7132 vd
= spa
->spa_spares
.sav_vdevs
[i
];
7133 if (vd
->vdev_guid
== guid
)
7142 spa_upgrade(spa_t
*spa
, uint64_t version
)
7144 ASSERT(spa_writeable(spa
));
7146 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
7149 * This should only be called for a non-faulted pool, and since a
7150 * future version would result in an unopenable pool, this shouldn't be
7153 ASSERT(SPA_VERSION_IS_SUPPORTED(spa
->spa_uberblock
.ub_version
));
7154 ASSERT3U(version
, >=, spa
->spa_uberblock
.ub_version
);
7156 spa
->spa_uberblock
.ub_version
= version
;
7157 vdev_config_dirty(spa
->spa_root_vdev
);
7159 spa_config_exit(spa
, SCL_ALL
, FTAG
);
7161 txg_wait_synced(spa_get_dsl(spa
), 0);
7165 spa_has_spare(spa_t
*spa
, uint64_t guid
)
7169 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
7171 for (i
= 0; i
< sav
->sav_count
; i
++)
7172 if (sav
->sav_vdevs
[i
]->vdev_guid
== guid
)
7175 for (i
= 0; i
< sav
->sav_npending
; i
++) {
7176 if (nvlist_lookup_uint64(sav
->sav_pending
[i
], ZPOOL_CONFIG_GUID
,
7177 &spareguid
) == 0 && spareguid
== guid
)
7185 * Check if a pool has an active shared spare device.
7186 * Note: reference count of an active spare is 2, as a spare and as a replace
7189 spa_has_active_shared_spare(spa_t
*spa
)
7193 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
7195 for (i
= 0; i
< sav
->sav_count
; i
++) {
7196 if (spa_spare_exists(sav
->sav_vdevs
[i
]->vdev_guid
, &pool
,
7197 &refcnt
) && pool
!= 0ULL && pool
== spa_guid(spa
) &&
7206 spa_event_create(spa_t
*spa
, vdev_t
*vd
, nvlist_t
*hist_nvl
, const char *name
)
7208 sysevent_t
*ev
= NULL
;
7212 resource
= zfs_event_create(spa
, vd
, FM_SYSEVENT_CLASS
, name
, hist_nvl
);
7214 ev
= kmem_alloc(sizeof (sysevent_t
), KM_SLEEP
);
7215 ev
->resource
= resource
;
7222 spa_event_post(sysevent_t
*ev
)
7226 zfs_zevent_post(ev
->resource
, NULL
, zfs_zevent_post_cb
);
7227 kmem_free(ev
, sizeof (*ev
));
7233 * Post a zevent corresponding to the given sysevent. The 'name' must be one
7234 * of the event definitions in sys/sysevent/eventdefs.h. The payload will be
7235 * filled in from the spa and (optionally) the vdev. This doesn't do anything
7236 * in the userland libzpool, as we don't want consumers to misinterpret ztest
7237 * or zdb as real changes.
7240 spa_event_notify(spa_t
*spa
, vdev_t
*vd
, nvlist_t
*hist_nvl
, const char *name
)
7242 spa_event_post(spa_event_create(spa
, vd
, hist_nvl
, name
));
7245 #if defined(_KERNEL) && defined(HAVE_SPL)
7246 /* state manipulation functions */
7247 EXPORT_SYMBOL(spa_open
);
7248 EXPORT_SYMBOL(spa_open_rewind
);
7249 EXPORT_SYMBOL(spa_get_stats
);
7250 EXPORT_SYMBOL(spa_create
);
7251 EXPORT_SYMBOL(spa_import
);
7252 EXPORT_SYMBOL(spa_tryimport
);
7253 EXPORT_SYMBOL(spa_destroy
);
7254 EXPORT_SYMBOL(spa_export
);
7255 EXPORT_SYMBOL(spa_reset
);
7256 EXPORT_SYMBOL(spa_async_request
);
7257 EXPORT_SYMBOL(spa_async_suspend
);
7258 EXPORT_SYMBOL(spa_async_resume
);
7259 EXPORT_SYMBOL(spa_inject_addref
);
7260 EXPORT_SYMBOL(spa_inject_delref
);
7261 EXPORT_SYMBOL(spa_scan_stat_init
);
7262 EXPORT_SYMBOL(spa_scan_get_stats
);
7264 /* device maniion */
7265 EXPORT_SYMBOL(spa_vdev_add
);
7266 EXPORT_SYMBOL(spa_vdev_attach
);
7267 EXPORT_SYMBOL(spa_vdev_detach
);
7268 EXPORT_SYMBOL(spa_vdev_remove
);
7269 EXPORT_SYMBOL(spa_vdev_setpath
);
7270 EXPORT_SYMBOL(spa_vdev_setfru
);
7271 EXPORT_SYMBOL(spa_vdev_split_mirror
);
7273 /* spare statech is global across all pools) */
7274 EXPORT_SYMBOL(spa_spare_add
);
7275 EXPORT_SYMBOL(spa_spare_remove
);
7276 EXPORT_SYMBOL(spa_spare_exists
);
7277 EXPORT_SYMBOL(spa_spare_activate
);
7279 /* L2ARC statech is global across all pools) */
7280 EXPORT_SYMBOL(spa_l2cache_add
);
7281 EXPORT_SYMBOL(spa_l2cache_remove
);
7282 EXPORT_SYMBOL(spa_l2cache_exists
);
7283 EXPORT_SYMBOL(spa_l2cache_activate
);
7284 EXPORT_SYMBOL(spa_l2cache_drop
);
7287 EXPORT_SYMBOL(spa_scan
);
7288 EXPORT_SYMBOL(spa_scan_stop
);
7291 EXPORT_SYMBOL(spa_sync
); /* only for DMU use */
7292 EXPORT_SYMBOL(spa_sync_allpools
);
7295 EXPORT_SYMBOL(spa_prop_set
);
7296 EXPORT_SYMBOL(spa_prop_get
);
7297 EXPORT_SYMBOL(spa_prop_clear_bootfs
);
7299 /* asynchronous event notification */
7300 EXPORT_SYMBOL(spa_event_notify
);
7303 #if defined(_KERNEL) && defined(HAVE_SPL)
7304 module_param(spa_load_verify_maxinflight
, int, 0644);
7305 MODULE_PARM_DESC(spa_load_verify_maxinflight
,
7306 "Max concurrent traversal I/Os while verifying pool during import -X");
7308 module_param(spa_load_verify_metadata
, int, 0644);
7309 MODULE_PARM_DESC(spa_load_verify_metadata
,
7310 "Set to traverse metadata on pool import");
7312 module_param(spa_load_verify_data
, int, 0644);
7313 MODULE_PARM_DESC(spa_load_verify_data
,
7314 "Set to traverse data on pool import");
7317 module_param(zio_taskq_batch_pct
, uint
, 0444);
7318 MODULE_PARM_DESC(zio_taskq_batch_pct
,
7319 "Percentage of CPUs to run an IO worker thread");