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
)) == ZPROP_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
);
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
== ZPROP_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 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
761 vdev_t
*rvd
= spa
->spa_root_vdev
;
763 ASSERTV(uint64_t *newguid
= arg
);
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 (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
];
955 if (tqs
->stqs_taskq
== NULL
) {
956 ASSERT3U(tqs
->stqs_count
, ==, 0);
960 for (i
= 0; i
< tqs
->stqs_count
; i
++) {
961 ASSERT3P(tqs
->stqs_taskq
[i
], !=, NULL
);
962 taskq_destroy(tqs
->stqs_taskq
[i
]);
965 kmem_free(tqs
->stqs_taskq
, tqs
->stqs_count
* sizeof (taskq_t
*));
966 tqs
->stqs_taskq
= NULL
;
970 * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority.
971 * Note that a type may have multiple discrete taskqs to avoid lock contention
972 * on the taskq itself. In that case we choose which taskq at random by using
973 * the low bits of gethrtime().
976 spa_taskq_dispatch_ent(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
977 task_func_t
*func
, void *arg
, uint_t flags
, taskq_ent_t
*ent
)
979 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
982 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
983 ASSERT3U(tqs
->stqs_count
, !=, 0);
985 if (tqs
->stqs_count
== 1) {
986 tq
= tqs
->stqs_taskq
[0];
988 tq
= tqs
->stqs_taskq
[((uint64_t)gethrtime()) % tqs
->stqs_count
];
991 taskq_dispatch_ent(tq
, func
, arg
, flags
, ent
);
995 * Same as spa_taskq_dispatch_ent() but block on the task until completion.
998 spa_taskq_dispatch_sync(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
999 task_func_t
*func
, void *arg
, uint_t flags
)
1001 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1005 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
1006 ASSERT3U(tqs
->stqs_count
, !=, 0);
1008 if (tqs
->stqs_count
== 1) {
1009 tq
= tqs
->stqs_taskq
[0];
1011 tq
= tqs
->stqs_taskq
[((uint64_t)gethrtime()) % tqs
->stqs_count
];
1014 id
= taskq_dispatch(tq
, func
, arg
, flags
);
1016 taskq_wait_id(tq
, id
);
1020 spa_create_zio_taskqs(spa_t
*spa
)
1024 for (t
= 0; t
< ZIO_TYPES
; t
++) {
1025 for (q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1026 spa_taskqs_init(spa
, t
, q
);
1031 #if defined(_KERNEL) && defined(HAVE_SPA_THREAD)
1033 spa_thread(void *arg
)
1035 callb_cpr_t cprinfo
;
1038 user_t
*pu
= PTOU(curproc
);
1040 CALLB_CPR_INIT(&cprinfo
, &spa
->spa_proc_lock
, callb_generic_cpr
,
1043 ASSERT(curproc
!= &p0
);
1044 (void) snprintf(pu
->u_psargs
, sizeof (pu
->u_psargs
),
1045 "zpool-%s", spa
->spa_name
);
1046 (void) strlcpy(pu
->u_comm
, pu
->u_psargs
, sizeof (pu
->u_comm
));
1048 /* bind this thread to the requested psrset */
1049 if (zio_taskq_psrset_bind
!= PS_NONE
) {
1051 mutex_enter(&cpu_lock
);
1052 mutex_enter(&pidlock
);
1053 mutex_enter(&curproc
->p_lock
);
1055 if (cpupart_bind_thread(curthread
, zio_taskq_psrset_bind
,
1056 0, NULL
, NULL
) == 0) {
1057 curthread
->t_bind_pset
= zio_taskq_psrset_bind
;
1060 "Couldn't bind process for zfs pool \"%s\" to "
1061 "pset %d\n", spa
->spa_name
, zio_taskq_psrset_bind
);
1064 mutex_exit(&curproc
->p_lock
);
1065 mutex_exit(&pidlock
);
1066 mutex_exit(&cpu_lock
);
1070 if (zio_taskq_sysdc
) {
1071 sysdc_thread_enter(curthread
, 100, 0);
1074 spa
->spa_proc
= curproc
;
1075 spa
->spa_did
= curthread
->t_did
;
1077 spa_create_zio_taskqs(spa
);
1079 mutex_enter(&spa
->spa_proc_lock
);
1080 ASSERT(spa
->spa_proc_state
== SPA_PROC_CREATED
);
1082 spa
->spa_proc_state
= SPA_PROC_ACTIVE
;
1083 cv_broadcast(&spa
->spa_proc_cv
);
1085 CALLB_CPR_SAFE_BEGIN(&cprinfo
);
1086 while (spa
->spa_proc_state
== SPA_PROC_ACTIVE
)
1087 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1088 CALLB_CPR_SAFE_END(&cprinfo
, &spa
->spa_proc_lock
);
1090 ASSERT(spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
);
1091 spa
->spa_proc_state
= SPA_PROC_GONE
;
1092 spa
->spa_proc
= &p0
;
1093 cv_broadcast(&spa
->spa_proc_cv
);
1094 CALLB_CPR_EXIT(&cprinfo
); /* drops spa_proc_lock */
1096 mutex_enter(&curproc
->p_lock
);
1102 * Activate an uninitialized pool.
1105 spa_activate(spa_t
*spa
, int mode
)
1107 ASSERT(spa
->spa_state
== POOL_STATE_UNINITIALIZED
);
1109 spa
->spa_state
= POOL_STATE_ACTIVE
;
1110 spa
->spa_mode
= mode
;
1112 spa
->spa_normal_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
1113 spa
->spa_log_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
1115 /* Try to create a covering process */
1116 mutex_enter(&spa
->spa_proc_lock
);
1117 ASSERT(spa
->spa_proc_state
== SPA_PROC_NONE
);
1118 ASSERT(spa
->spa_proc
== &p0
);
1121 #ifdef HAVE_SPA_THREAD
1122 /* Only create a process if we're going to be around a while. */
1123 if (spa_create_process
&& strcmp(spa
->spa_name
, TRYIMPORT_NAME
) != 0) {
1124 if (newproc(spa_thread
, (caddr_t
)spa
, syscid
, maxclsyspri
,
1126 spa
->spa_proc_state
= SPA_PROC_CREATED
;
1127 while (spa
->spa_proc_state
== SPA_PROC_CREATED
) {
1128 cv_wait(&spa
->spa_proc_cv
,
1129 &spa
->spa_proc_lock
);
1131 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1132 ASSERT(spa
->spa_proc
!= &p0
);
1133 ASSERT(spa
->spa_did
!= 0);
1137 "Couldn't create process for zfs pool \"%s\"\n",
1142 #endif /* HAVE_SPA_THREAD */
1143 mutex_exit(&spa
->spa_proc_lock
);
1145 /* If we didn't create a process, we need to create our taskqs. */
1146 if (spa
->spa_proc
== &p0
) {
1147 spa_create_zio_taskqs(spa
);
1150 list_create(&spa
->spa_config_dirty_list
, sizeof (vdev_t
),
1151 offsetof(vdev_t
, vdev_config_dirty_node
));
1152 list_create(&spa
->spa_evicting_os_list
, sizeof (objset_t
),
1153 offsetof(objset_t
, os_evicting_node
));
1154 list_create(&spa
->spa_state_dirty_list
, sizeof (vdev_t
),
1155 offsetof(vdev_t
, vdev_state_dirty_node
));
1157 txg_list_create(&spa
->spa_vdev_txg_list
, spa
,
1158 offsetof(struct vdev
, vdev_txg_node
));
1160 avl_create(&spa
->spa_errlist_scrub
,
1161 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1162 offsetof(spa_error_entry_t
, se_avl
));
1163 avl_create(&spa
->spa_errlist_last
,
1164 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1165 offsetof(spa_error_entry_t
, se_avl
));
1168 * This taskq is used to perform zvol-minor-related tasks
1169 * asynchronously. This has several advantages, including easy
1170 * resolution of various deadlocks (zfsonlinux bug #3681).
1172 * The taskq must be single threaded to ensure tasks are always
1173 * processed in the order in which they were dispatched.
1175 * A taskq per pool allows one to keep the pools independent.
1176 * This way if one pool is suspended, it will not impact another.
1178 * The preferred location to dispatch a zvol minor task is a sync
1179 * task. In this context, there is easy access to the spa_t and minimal
1180 * error handling is required because the sync task must succeed.
1182 spa
->spa_zvol_taskq
= taskq_create("z_zvol", 1, defclsyspri
,
1186 * Taskq dedicated to prefetcher threads: this is used to prevent the
1187 * pool traverse code from monopolizing the global (and limited)
1188 * system_taskq by inappropriately scheduling long running tasks on it.
1190 spa
->spa_prefetch_taskq
= taskq_create("z_prefetch", boot_ncpus
,
1191 defclsyspri
, 1, INT_MAX
, TASKQ_DYNAMIC
);
1194 * The taskq to upgrade datasets in this pool. Currently used by
1195 * feature SPA_FEATURE_USEROBJ_ACCOUNTING.
1197 spa
->spa_upgrade_taskq
= taskq_create("z_upgrade", boot_ncpus
,
1198 defclsyspri
, 1, INT_MAX
, TASKQ_DYNAMIC
);
1202 * Opposite of spa_activate().
1205 spa_deactivate(spa_t
*spa
)
1209 ASSERT(spa
->spa_sync_on
== B_FALSE
);
1210 ASSERT(spa
->spa_dsl_pool
== NULL
);
1211 ASSERT(spa
->spa_root_vdev
== NULL
);
1212 ASSERT(spa
->spa_async_zio_root
== NULL
);
1213 ASSERT(spa
->spa_state
!= POOL_STATE_UNINITIALIZED
);
1215 spa_evicting_os_wait(spa
);
1217 if (spa
->spa_zvol_taskq
) {
1218 taskq_destroy(spa
->spa_zvol_taskq
);
1219 spa
->spa_zvol_taskq
= NULL
;
1222 if (spa
->spa_prefetch_taskq
) {
1223 taskq_destroy(spa
->spa_prefetch_taskq
);
1224 spa
->spa_prefetch_taskq
= NULL
;
1227 if (spa
->spa_upgrade_taskq
) {
1228 taskq_destroy(spa
->spa_upgrade_taskq
);
1229 spa
->spa_upgrade_taskq
= NULL
;
1232 txg_list_destroy(&spa
->spa_vdev_txg_list
);
1234 list_destroy(&spa
->spa_config_dirty_list
);
1235 list_destroy(&spa
->spa_evicting_os_list
);
1236 list_destroy(&spa
->spa_state_dirty_list
);
1238 taskq_cancel_id(system_delay_taskq
, spa
->spa_deadman_tqid
);
1240 for (t
= 0; t
< ZIO_TYPES
; t
++) {
1241 for (q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1242 spa_taskqs_fini(spa
, t
, q
);
1246 metaslab_class_destroy(spa
->spa_normal_class
);
1247 spa
->spa_normal_class
= NULL
;
1249 metaslab_class_destroy(spa
->spa_log_class
);
1250 spa
->spa_log_class
= NULL
;
1253 * If this was part of an import or the open otherwise failed, we may
1254 * still have errors left in the queues. Empty them just in case.
1256 spa_errlog_drain(spa
);
1258 avl_destroy(&spa
->spa_errlist_scrub
);
1259 avl_destroy(&spa
->spa_errlist_last
);
1261 spa
->spa_state
= POOL_STATE_UNINITIALIZED
;
1263 mutex_enter(&spa
->spa_proc_lock
);
1264 if (spa
->spa_proc_state
!= SPA_PROC_NONE
) {
1265 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1266 spa
->spa_proc_state
= SPA_PROC_DEACTIVATE
;
1267 cv_broadcast(&spa
->spa_proc_cv
);
1268 while (spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
) {
1269 ASSERT(spa
->spa_proc
!= &p0
);
1270 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1272 ASSERT(spa
->spa_proc_state
== SPA_PROC_GONE
);
1273 spa
->spa_proc_state
= SPA_PROC_NONE
;
1275 ASSERT(spa
->spa_proc
== &p0
);
1276 mutex_exit(&spa
->spa_proc_lock
);
1279 * We want to make sure spa_thread() has actually exited the ZFS
1280 * module, so that the module can't be unloaded out from underneath
1283 if (spa
->spa_did
!= 0) {
1284 thread_join(spa
->spa_did
);
1290 * Verify a pool configuration, and construct the vdev tree appropriately. This
1291 * will create all the necessary vdevs in the appropriate layout, with each vdev
1292 * in the CLOSED state. This will prep the pool before open/creation/import.
1293 * All vdev validation is done by the vdev_alloc() routine.
1296 spa_config_parse(spa_t
*spa
, vdev_t
**vdp
, nvlist_t
*nv
, vdev_t
*parent
,
1297 uint_t id
, int atype
)
1304 if ((error
= vdev_alloc(spa
, vdp
, nv
, parent
, id
, atype
)) != 0)
1307 if ((*vdp
)->vdev_ops
->vdev_op_leaf
)
1310 error
= nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1313 if (error
== ENOENT
)
1319 return (SET_ERROR(EINVAL
));
1322 for (c
= 0; c
< children
; c
++) {
1324 if ((error
= spa_config_parse(spa
, &vd
, child
[c
], *vdp
, c
,
1332 ASSERT(*vdp
!= NULL
);
1338 * Opposite of spa_load().
1341 spa_unload(spa_t
*spa
)
1345 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1350 spa_async_suspend(spa
);
1355 if (spa
->spa_sync_on
) {
1356 txg_sync_stop(spa
->spa_dsl_pool
);
1357 spa
->spa_sync_on
= B_FALSE
;
1361 * Even though vdev_free() also calls vdev_metaslab_fini, we need
1362 * to call it earlier, before we wait for async i/o to complete.
1363 * This ensures that there is no async metaslab prefetching, by
1364 * calling taskq_wait(mg_taskq).
1366 if (spa
->spa_root_vdev
!= NULL
) {
1367 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1368 for (c
= 0; c
< spa
->spa_root_vdev
->vdev_children
; c
++)
1369 vdev_metaslab_fini(spa
->spa_root_vdev
->vdev_child
[c
]);
1370 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1373 if (spa
->spa_mmp
.mmp_thread
)
1374 mmp_thread_stop(spa
);
1377 * Wait for any outstanding async I/O to complete.
1379 if (spa
->spa_async_zio_root
!= NULL
) {
1380 for (i
= 0; i
< max_ncpus
; i
++)
1381 (void) zio_wait(spa
->spa_async_zio_root
[i
]);
1382 kmem_free(spa
->spa_async_zio_root
, max_ncpus
* sizeof (void *));
1383 spa
->spa_async_zio_root
= NULL
;
1386 bpobj_close(&spa
->spa_deferred_bpobj
);
1388 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1393 if (spa
->spa_root_vdev
)
1394 vdev_free(spa
->spa_root_vdev
);
1395 ASSERT(spa
->spa_root_vdev
== NULL
);
1398 * Close the dsl pool.
1400 if (spa
->spa_dsl_pool
) {
1401 dsl_pool_close(spa
->spa_dsl_pool
);
1402 spa
->spa_dsl_pool
= NULL
;
1403 spa
->spa_meta_objset
= NULL
;
1409 * Drop and purge level 2 cache
1411 spa_l2cache_drop(spa
);
1413 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1414 vdev_free(spa
->spa_spares
.sav_vdevs
[i
]);
1415 if (spa
->spa_spares
.sav_vdevs
) {
1416 kmem_free(spa
->spa_spares
.sav_vdevs
,
1417 spa
->spa_spares
.sav_count
* sizeof (void *));
1418 spa
->spa_spares
.sav_vdevs
= NULL
;
1420 if (spa
->spa_spares
.sav_config
) {
1421 nvlist_free(spa
->spa_spares
.sav_config
);
1422 spa
->spa_spares
.sav_config
= NULL
;
1424 spa
->spa_spares
.sav_count
= 0;
1426 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
1427 vdev_clear_stats(spa
->spa_l2cache
.sav_vdevs
[i
]);
1428 vdev_free(spa
->spa_l2cache
.sav_vdevs
[i
]);
1430 if (spa
->spa_l2cache
.sav_vdevs
) {
1431 kmem_free(spa
->spa_l2cache
.sav_vdevs
,
1432 spa
->spa_l2cache
.sav_count
* sizeof (void *));
1433 spa
->spa_l2cache
.sav_vdevs
= NULL
;
1435 if (spa
->spa_l2cache
.sav_config
) {
1436 nvlist_free(spa
->spa_l2cache
.sav_config
);
1437 spa
->spa_l2cache
.sav_config
= NULL
;
1439 spa
->spa_l2cache
.sav_count
= 0;
1441 spa
->spa_async_suspended
= 0;
1443 if (spa
->spa_comment
!= NULL
) {
1444 spa_strfree(spa
->spa_comment
);
1445 spa
->spa_comment
= NULL
;
1448 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1452 * Load (or re-load) the current list of vdevs describing the active spares for
1453 * this pool. When this is called, we have some form of basic information in
1454 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1455 * then re-generate a more complete list including status information.
1458 spa_load_spares(spa_t
*spa
)
1465 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1468 * First, close and free any existing spare vdevs.
1470 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1471 vd
= spa
->spa_spares
.sav_vdevs
[i
];
1473 /* Undo the call to spa_activate() below */
1474 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1475 B_FALSE
)) != NULL
&& tvd
->vdev_isspare
)
1476 spa_spare_remove(tvd
);
1481 if (spa
->spa_spares
.sav_vdevs
)
1482 kmem_free(spa
->spa_spares
.sav_vdevs
,
1483 spa
->spa_spares
.sav_count
* sizeof (void *));
1485 if (spa
->spa_spares
.sav_config
== NULL
)
1488 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
1489 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
1491 spa
->spa_spares
.sav_count
= (int)nspares
;
1492 spa
->spa_spares
.sav_vdevs
= NULL
;
1498 * Construct the array of vdevs, opening them to get status in the
1499 * process. For each spare, there is potentially two different vdev_t
1500 * structures associated with it: one in the list of spares (used only
1501 * for basic validation purposes) and one in the active vdev
1502 * configuration (if it's spared in). During this phase we open and
1503 * validate each vdev on the spare list. If the vdev also exists in the
1504 * active configuration, then we also mark this vdev as an active spare.
1506 spa
->spa_spares
.sav_vdevs
= kmem_zalloc(nspares
* sizeof (void *),
1508 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1509 VERIFY(spa_config_parse(spa
, &vd
, spares
[i
], NULL
, 0,
1510 VDEV_ALLOC_SPARE
) == 0);
1513 spa
->spa_spares
.sav_vdevs
[i
] = vd
;
1515 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1516 B_FALSE
)) != NULL
) {
1517 if (!tvd
->vdev_isspare
)
1521 * We only mark the spare active if we were successfully
1522 * able to load the vdev. Otherwise, importing a pool
1523 * with a bad active spare would result in strange
1524 * behavior, because multiple pool would think the spare
1525 * is actively in use.
1527 * There is a vulnerability here to an equally bizarre
1528 * circumstance, where a dead active spare is later
1529 * brought back to life (onlined or otherwise). Given
1530 * the rarity of this scenario, and the extra complexity
1531 * it adds, we ignore the possibility.
1533 if (!vdev_is_dead(tvd
))
1534 spa_spare_activate(tvd
);
1538 vd
->vdev_aux
= &spa
->spa_spares
;
1540 if (vdev_open(vd
) != 0)
1543 if (vdev_validate_aux(vd
) == 0)
1548 * Recompute the stashed list of spares, with status information
1551 VERIFY(nvlist_remove(spa
->spa_spares
.sav_config
, ZPOOL_CONFIG_SPARES
,
1552 DATA_TYPE_NVLIST_ARRAY
) == 0);
1554 spares
= kmem_alloc(spa
->spa_spares
.sav_count
* sizeof (void *),
1556 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1557 spares
[i
] = vdev_config_generate(spa
,
1558 spa
->spa_spares
.sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_SPARE
);
1559 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
1560 ZPOOL_CONFIG_SPARES
, spares
, spa
->spa_spares
.sav_count
) == 0);
1561 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1562 nvlist_free(spares
[i
]);
1563 kmem_free(spares
, spa
->spa_spares
.sav_count
* sizeof (void *));
1567 * Load (or re-load) the current list of vdevs describing the active l2cache for
1568 * this pool. When this is called, we have some form of basic information in
1569 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1570 * then re-generate a more complete list including status information.
1571 * Devices which are already active have their details maintained, and are
1575 spa_load_l2cache(spa_t
*spa
)
1577 nvlist_t
**l2cache
= NULL
;
1579 int i
, j
, oldnvdevs
;
1581 vdev_t
*vd
, **oldvdevs
, **newvdevs
;
1582 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
1584 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1586 oldvdevs
= sav
->sav_vdevs
;
1587 oldnvdevs
= sav
->sav_count
;
1588 sav
->sav_vdevs
= NULL
;
1591 if (sav
->sav_config
== NULL
) {
1597 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
,
1598 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
1599 newvdevs
= kmem_alloc(nl2cache
* sizeof (void *), KM_SLEEP
);
1602 * Process new nvlist of vdevs.
1604 for (i
= 0; i
< nl2cache
; i
++) {
1605 VERIFY(nvlist_lookup_uint64(l2cache
[i
], ZPOOL_CONFIG_GUID
,
1609 for (j
= 0; j
< oldnvdevs
; j
++) {
1611 if (vd
!= NULL
&& guid
== vd
->vdev_guid
) {
1613 * Retain previous vdev for add/remove ops.
1621 if (newvdevs
[i
] == NULL
) {
1625 VERIFY(spa_config_parse(spa
, &vd
, l2cache
[i
], NULL
, 0,
1626 VDEV_ALLOC_L2CACHE
) == 0);
1631 * Commit this vdev as an l2cache device,
1632 * even if it fails to open.
1634 spa_l2cache_add(vd
);
1639 spa_l2cache_activate(vd
);
1641 if (vdev_open(vd
) != 0)
1644 (void) vdev_validate_aux(vd
);
1646 if (!vdev_is_dead(vd
))
1647 l2arc_add_vdev(spa
, vd
);
1651 sav
->sav_vdevs
= newvdevs
;
1652 sav
->sav_count
= (int)nl2cache
;
1655 * Recompute the stashed list of l2cache devices, with status
1656 * information this time.
1658 VERIFY(nvlist_remove(sav
->sav_config
, ZPOOL_CONFIG_L2CACHE
,
1659 DATA_TYPE_NVLIST_ARRAY
) == 0);
1661 if (sav
->sav_count
> 0)
1662 l2cache
= kmem_alloc(sav
->sav_count
* sizeof (void *),
1664 for (i
= 0; i
< sav
->sav_count
; i
++)
1665 l2cache
[i
] = vdev_config_generate(spa
,
1666 sav
->sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_L2CACHE
);
1667 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
,
1668 ZPOOL_CONFIG_L2CACHE
, l2cache
, sav
->sav_count
) == 0);
1672 * Purge vdevs that were dropped
1674 for (i
= 0; i
< oldnvdevs
; i
++) {
1679 ASSERT(vd
->vdev_isl2cache
);
1681 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
1682 pool
!= 0ULL && l2arc_vdev_present(vd
))
1683 l2arc_remove_vdev(vd
);
1684 vdev_clear_stats(vd
);
1690 kmem_free(oldvdevs
, oldnvdevs
* sizeof (void *));
1692 for (i
= 0; i
< sav
->sav_count
; i
++)
1693 nvlist_free(l2cache
[i
]);
1695 kmem_free(l2cache
, sav
->sav_count
* sizeof (void *));
1699 load_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
**value
)
1702 char *packed
= NULL
;
1707 error
= dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
);
1711 nvsize
= *(uint64_t *)db
->db_data
;
1712 dmu_buf_rele(db
, FTAG
);
1714 packed
= vmem_alloc(nvsize
, KM_SLEEP
);
1715 error
= dmu_read(spa
->spa_meta_objset
, obj
, 0, nvsize
, packed
,
1718 error
= nvlist_unpack(packed
, nvsize
, value
, 0);
1719 vmem_free(packed
, nvsize
);
1725 * Checks to see if the given vdev could not be opened, in which case we post a
1726 * sysevent to notify the autoreplace code that the device has been removed.
1729 spa_check_removed(vdev_t
*vd
)
1733 for (c
= 0; c
< vd
->vdev_children
; c
++)
1734 spa_check_removed(vd
->vdev_child
[c
]);
1736 if (vd
->vdev_ops
->vdev_op_leaf
&& vdev_is_dead(vd
) &&
1738 zfs_post_autoreplace(vd
->vdev_spa
, vd
);
1739 spa_event_notify(vd
->vdev_spa
, vd
, NULL
, ESC_ZFS_VDEV_CHECK
);
1744 spa_config_valid_zaps(vdev_t
*vd
, vdev_t
*mvd
)
1748 ASSERT3U(vd
->vdev_children
, ==, mvd
->vdev_children
);
1750 vd
->vdev_top_zap
= mvd
->vdev_top_zap
;
1751 vd
->vdev_leaf_zap
= mvd
->vdev_leaf_zap
;
1753 for (i
= 0; i
< vd
->vdev_children
; i
++) {
1754 spa_config_valid_zaps(vd
->vdev_child
[i
], mvd
->vdev_child
[i
]);
1759 * Validate the current config against the MOS config
1762 spa_config_valid(spa_t
*spa
, nvlist_t
*config
)
1764 vdev_t
*mrvd
, *rvd
= spa
->spa_root_vdev
;
1768 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nv
) == 0);
1770 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1771 VERIFY(spa_config_parse(spa
, &mrvd
, nv
, NULL
, 0, VDEV_ALLOC_LOAD
) == 0);
1773 ASSERT3U(rvd
->vdev_children
, ==, mrvd
->vdev_children
);
1776 * If we're doing a normal import, then build up any additional
1777 * diagnostic information about missing devices in this config.
1778 * We'll pass this up to the user for further processing.
1780 if (!(spa
->spa_import_flags
& ZFS_IMPORT_MISSING_LOG
)) {
1781 nvlist_t
**child
, *nv
;
1784 child
= kmem_alloc(rvd
->vdev_children
* sizeof (nvlist_t
*),
1786 VERIFY(nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
1788 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1789 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1790 vdev_t
*mtvd
= mrvd
->vdev_child
[c
];
1792 if (tvd
->vdev_ops
== &vdev_missing_ops
&&
1793 mtvd
->vdev_ops
!= &vdev_missing_ops
&&
1795 child
[idx
++] = vdev_config_generate(spa
, mtvd
,
1800 VERIFY(nvlist_add_nvlist_array(nv
,
1801 ZPOOL_CONFIG_CHILDREN
, child
, idx
) == 0);
1802 VERIFY(nvlist_add_nvlist(spa
->spa_load_info
,
1803 ZPOOL_CONFIG_MISSING_DEVICES
, nv
) == 0);
1805 for (i
= 0; i
< idx
; i
++)
1806 nvlist_free(child
[i
]);
1809 kmem_free(child
, rvd
->vdev_children
* sizeof (char **));
1813 * Compare the root vdev tree with the information we have
1814 * from the MOS config (mrvd). Check each top-level vdev
1815 * with the corresponding MOS config top-level (mtvd).
1817 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1818 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1819 vdev_t
*mtvd
= mrvd
->vdev_child
[c
];
1822 * Resolve any "missing" vdevs in the current configuration.
1823 * If we find that the MOS config has more accurate information
1824 * about the top-level vdev then use that vdev instead.
1826 if (tvd
->vdev_ops
== &vdev_missing_ops
&&
1827 mtvd
->vdev_ops
!= &vdev_missing_ops
) {
1829 if (!(spa
->spa_import_flags
& ZFS_IMPORT_MISSING_LOG
))
1833 * Device specific actions.
1835 if (mtvd
->vdev_islog
) {
1836 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
1839 * XXX - once we have 'readonly' pool
1840 * support we should be able to handle
1841 * missing data devices by transitioning
1842 * the pool to readonly.
1848 * Swap the missing vdev with the data we were
1849 * able to obtain from the MOS config.
1851 vdev_remove_child(rvd
, tvd
);
1852 vdev_remove_child(mrvd
, mtvd
);
1854 vdev_add_child(rvd
, mtvd
);
1855 vdev_add_child(mrvd
, tvd
);
1857 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1859 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1863 if (mtvd
->vdev_islog
) {
1865 * Load the slog device's state from the MOS
1866 * config since it's possible that the label
1867 * does not contain the most up-to-date
1870 vdev_load_log_state(tvd
, mtvd
);
1875 * Per-vdev ZAP info is stored exclusively in the MOS.
1877 spa_config_valid_zaps(tvd
, mtvd
);
1882 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1885 * Ensure we were able to validate the config.
1887 return (rvd
->vdev_guid_sum
== spa
->spa_uberblock
.ub_guid_sum
);
1891 * Check for missing log devices
1894 spa_check_logs(spa_t
*spa
)
1896 boolean_t rv
= B_FALSE
;
1897 dsl_pool_t
*dp
= spa_get_dsl(spa
);
1899 switch (spa
->spa_log_state
) {
1902 case SPA_LOG_MISSING
:
1903 /* need to recheck in case slog has been restored */
1904 case SPA_LOG_UNKNOWN
:
1905 rv
= (dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
1906 zil_check_log_chain
, NULL
, DS_FIND_CHILDREN
) != 0);
1908 spa_set_log_state(spa
, SPA_LOG_MISSING
);
1915 spa_passivate_log(spa_t
*spa
)
1917 vdev_t
*rvd
= spa
->spa_root_vdev
;
1918 boolean_t slog_found
= B_FALSE
;
1921 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
1923 if (!spa_has_slogs(spa
))
1926 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1927 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1928 metaslab_group_t
*mg
= tvd
->vdev_mg
;
1930 if (tvd
->vdev_islog
) {
1931 metaslab_group_passivate(mg
);
1932 slog_found
= B_TRUE
;
1936 return (slog_found
);
1940 spa_activate_log(spa_t
*spa
)
1942 vdev_t
*rvd
= spa
->spa_root_vdev
;
1945 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
1947 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1948 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1949 metaslab_group_t
*mg
= tvd
->vdev_mg
;
1951 if (tvd
->vdev_islog
)
1952 metaslab_group_activate(mg
);
1957 spa_offline_log(spa_t
*spa
)
1961 error
= dmu_objset_find(spa_name(spa
), zil_vdev_offline
,
1962 NULL
, DS_FIND_CHILDREN
);
1965 * We successfully offlined the log device, sync out the
1966 * current txg so that the "stubby" block can be removed
1969 txg_wait_synced(spa
->spa_dsl_pool
, 0);
1975 spa_aux_check_removed(spa_aux_vdev_t
*sav
)
1979 for (i
= 0; i
< sav
->sav_count
; i
++)
1980 spa_check_removed(sav
->sav_vdevs
[i
]);
1984 spa_claim_notify(zio_t
*zio
)
1986 spa_t
*spa
= zio
->io_spa
;
1991 mutex_enter(&spa
->spa_props_lock
); /* any mutex will do */
1992 if (spa
->spa_claim_max_txg
< zio
->io_bp
->blk_birth
)
1993 spa
->spa_claim_max_txg
= zio
->io_bp
->blk_birth
;
1994 mutex_exit(&spa
->spa_props_lock
);
1997 typedef struct spa_load_error
{
1998 uint64_t sle_meta_count
;
1999 uint64_t sle_data_count
;
2003 spa_load_verify_done(zio_t
*zio
)
2005 blkptr_t
*bp
= zio
->io_bp
;
2006 spa_load_error_t
*sle
= zio
->io_private
;
2007 dmu_object_type_t type
= BP_GET_TYPE(bp
);
2008 int error
= zio
->io_error
;
2009 spa_t
*spa
= zio
->io_spa
;
2011 abd_free(zio
->io_abd
);
2013 if ((BP_GET_LEVEL(bp
) != 0 || DMU_OT_IS_METADATA(type
)) &&
2014 type
!= DMU_OT_INTENT_LOG
)
2015 atomic_inc_64(&sle
->sle_meta_count
);
2017 atomic_inc_64(&sle
->sle_data_count
);
2020 mutex_enter(&spa
->spa_scrub_lock
);
2021 spa
->spa_scrub_inflight
--;
2022 cv_broadcast(&spa
->spa_scrub_io_cv
);
2023 mutex_exit(&spa
->spa_scrub_lock
);
2027 * Maximum number of concurrent scrub i/os to create while verifying
2028 * a pool while importing it.
2030 int spa_load_verify_maxinflight
= 10000;
2031 int spa_load_verify_metadata
= B_TRUE
;
2032 int spa_load_verify_data
= B_TRUE
;
2036 spa_load_verify_cb(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
2037 const zbookmark_phys_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
2042 if (bp
== NULL
|| BP_IS_HOLE(bp
) || BP_IS_EMBEDDED(bp
))
2045 * Note: normally this routine will not be called if
2046 * spa_load_verify_metadata is not set. However, it may be useful
2047 * to manually set the flag after the traversal has begun.
2049 if (!spa_load_verify_metadata
)
2051 if (!BP_IS_METADATA(bp
) && !spa_load_verify_data
)
2055 size
= BP_GET_PSIZE(bp
);
2057 mutex_enter(&spa
->spa_scrub_lock
);
2058 while (spa
->spa_scrub_inflight
>= spa_load_verify_maxinflight
)
2059 cv_wait(&spa
->spa_scrub_io_cv
, &spa
->spa_scrub_lock
);
2060 spa
->spa_scrub_inflight
++;
2061 mutex_exit(&spa
->spa_scrub_lock
);
2063 zio_nowait(zio_read(rio
, spa
, bp
, abd_alloc_for_io(size
, B_FALSE
), size
,
2064 spa_load_verify_done
, rio
->io_private
, ZIO_PRIORITY_SCRUB
,
2065 ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_CANFAIL
|
2066 ZIO_FLAG_SCRUB
| ZIO_FLAG_RAW
, zb
));
2072 verify_dataset_name_len(dsl_pool_t
*dp
, dsl_dataset_t
*ds
, void *arg
)
2074 if (dsl_dataset_namelen(ds
) >= ZFS_MAX_DATASET_NAME_LEN
)
2075 return (SET_ERROR(ENAMETOOLONG
));
2081 spa_load_verify(spa_t
*spa
)
2084 spa_load_error_t sle
= { 0 };
2085 zpool_rewind_policy_t policy
;
2086 boolean_t verify_ok
= B_FALSE
;
2089 zpool_get_rewind_policy(spa
->spa_config
, &policy
);
2091 if (policy
.zrp_request
& ZPOOL_NEVER_REWIND
)
2094 dsl_pool_config_enter(spa
->spa_dsl_pool
, FTAG
);
2095 error
= dmu_objset_find_dp(spa
->spa_dsl_pool
,
2096 spa
->spa_dsl_pool
->dp_root_dir_obj
, verify_dataset_name_len
, NULL
,
2098 dsl_pool_config_exit(spa
->spa_dsl_pool
, FTAG
);
2102 rio
= zio_root(spa
, NULL
, &sle
,
2103 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
);
2105 if (spa_load_verify_metadata
) {
2106 error
= traverse_pool(spa
, spa
->spa_verify_min_txg
,
2107 TRAVERSE_PRE
| TRAVERSE_PREFETCH_METADATA
,
2108 spa_load_verify_cb
, rio
);
2111 (void) zio_wait(rio
);
2113 spa
->spa_load_meta_errors
= sle
.sle_meta_count
;
2114 spa
->spa_load_data_errors
= sle
.sle_data_count
;
2116 if (!error
&& sle
.sle_meta_count
<= policy
.zrp_maxmeta
&&
2117 sle
.sle_data_count
<= policy
.zrp_maxdata
) {
2121 spa
->spa_load_txg
= spa
->spa_uberblock
.ub_txg
;
2122 spa
->spa_load_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
2124 loss
= spa
->spa_last_ubsync_txg_ts
- spa
->spa_load_txg_ts
;
2125 VERIFY(nvlist_add_uint64(spa
->spa_load_info
,
2126 ZPOOL_CONFIG_LOAD_TIME
, spa
->spa_load_txg_ts
) == 0);
2127 VERIFY(nvlist_add_int64(spa
->spa_load_info
,
2128 ZPOOL_CONFIG_REWIND_TIME
, loss
) == 0);
2129 VERIFY(nvlist_add_uint64(spa
->spa_load_info
,
2130 ZPOOL_CONFIG_LOAD_DATA_ERRORS
, sle
.sle_data_count
) == 0);
2132 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
;
2136 if (error
!= ENXIO
&& error
!= EIO
)
2137 error
= SET_ERROR(EIO
);
2141 return (verify_ok
? 0 : EIO
);
2145 * Find a value in the pool props object.
2148 spa_prop_find(spa_t
*spa
, zpool_prop_t prop
, uint64_t *val
)
2150 (void) zap_lookup(spa
->spa_meta_objset
, spa
->spa_pool_props_object
,
2151 zpool_prop_to_name(prop
), sizeof (uint64_t), 1, val
);
2155 * Find a value in the pool directory object.
2158 spa_dir_prop(spa_t
*spa
, const char *name
, uint64_t *val
)
2160 return (zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
2161 name
, sizeof (uint64_t), 1, val
));
2165 spa_vdev_err(vdev_t
*vdev
, vdev_aux_t aux
, int err
)
2167 vdev_set_state(vdev
, B_TRUE
, VDEV_STATE_CANT_OPEN
, aux
);
2172 * Fix up config after a partly-completed split. This is done with the
2173 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
2174 * pool have that entry in their config, but only the splitting one contains
2175 * a list of all the guids of the vdevs that are being split off.
2177 * This function determines what to do with that list: either rejoin
2178 * all the disks to the pool, or complete the splitting process. To attempt
2179 * the rejoin, each disk that is offlined is marked online again, and
2180 * we do a reopen() call. If the vdev label for every disk that was
2181 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
2182 * then we call vdev_split() on each disk, and complete the split.
2184 * Otherwise we leave the config alone, with all the vdevs in place in
2185 * the original pool.
2188 spa_try_repair(spa_t
*spa
, nvlist_t
*config
)
2195 boolean_t attempt_reopen
;
2197 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
, &nvl
) != 0)
2200 /* check that the config is complete */
2201 if (nvlist_lookup_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
2202 &glist
, &gcount
) != 0)
2205 vd
= kmem_zalloc(gcount
* sizeof (vdev_t
*), KM_SLEEP
);
2207 /* attempt to online all the vdevs & validate */
2208 attempt_reopen
= B_TRUE
;
2209 for (i
= 0; i
< gcount
; i
++) {
2210 if (glist
[i
] == 0) /* vdev is hole */
2213 vd
[i
] = spa_lookup_by_guid(spa
, glist
[i
], B_FALSE
);
2214 if (vd
[i
] == NULL
) {
2216 * Don't bother attempting to reopen the disks;
2217 * just do the split.
2219 attempt_reopen
= B_FALSE
;
2221 /* attempt to re-online it */
2222 vd
[i
]->vdev_offline
= B_FALSE
;
2226 if (attempt_reopen
) {
2227 vdev_reopen(spa
->spa_root_vdev
);
2229 /* check each device to see what state it's in */
2230 for (extracted
= 0, i
= 0; i
< gcount
; i
++) {
2231 if (vd
[i
] != NULL
&&
2232 vd
[i
]->vdev_stat
.vs_aux
!= VDEV_AUX_SPLIT_POOL
)
2239 * If every disk has been moved to the new pool, or if we never
2240 * even attempted to look at them, then we split them off for
2243 if (!attempt_reopen
|| gcount
== extracted
) {
2244 for (i
= 0; i
< gcount
; i
++)
2247 vdev_reopen(spa
->spa_root_vdev
);
2250 kmem_free(vd
, gcount
* sizeof (vdev_t
*));
2254 spa_load(spa_t
*spa
, spa_load_state_t state
, spa_import_type_t type
,
2255 boolean_t mosconfig
)
2257 nvlist_t
*config
= spa
->spa_config
;
2258 char *ereport
= FM_EREPORT_ZFS_POOL
;
2264 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
, &pool_guid
))
2265 return (SET_ERROR(EINVAL
));
2267 ASSERT(spa
->spa_comment
== NULL
);
2268 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
2269 spa
->spa_comment
= spa_strdup(comment
);
2272 * Versioning wasn't explicitly added to the label until later, so if
2273 * it's not present treat it as the initial version.
2275 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
2276 &spa
->spa_ubsync
.ub_version
) != 0)
2277 spa
->spa_ubsync
.ub_version
= SPA_VERSION_INITIAL
;
2279 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
2280 &spa
->spa_config_txg
);
2282 if ((state
== SPA_LOAD_IMPORT
|| state
== SPA_LOAD_TRYIMPORT
) &&
2283 spa_guid_exists(pool_guid
, 0)) {
2284 error
= SET_ERROR(EEXIST
);
2286 spa
->spa_config_guid
= pool_guid
;
2288 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
,
2290 VERIFY(nvlist_dup(nvl
, &spa
->spa_config_splitting
,
2294 nvlist_free(spa
->spa_load_info
);
2295 spa
->spa_load_info
= fnvlist_alloc();
2297 gethrestime(&spa
->spa_loaded_ts
);
2298 error
= spa_load_impl(spa
, pool_guid
, config
, state
, type
,
2299 mosconfig
, &ereport
);
2303 * Don't count references from objsets that are already closed
2304 * and are making their way through the eviction process.
2306 spa_evicting_os_wait(spa
);
2307 spa
->spa_minref
= refcount_count(&spa
->spa_refcount
);
2309 if (error
!= EEXIST
) {
2310 spa
->spa_loaded_ts
.tv_sec
= 0;
2311 spa
->spa_loaded_ts
.tv_nsec
= 0;
2313 if (error
!= EBADF
) {
2314 zfs_ereport_post(ereport
, spa
, NULL
, NULL
, 0, 0);
2317 spa
->spa_load_state
= error
? SPA_LOAD_ERROR
: SPA_LOAD_NONE
;
2325 * Count the number of per-vdev ZAPs associated with all of the vdevs in the
2326 * vdev tree rooted in the given vd, and ensure that each ZAP is present in the
2327 * spa's per-vdev ZAP list.
2330 vdev_count_verify_zaps(vdev_t
*vd
)
2332 spa_t
*spa
= vd
->vdev_spa
;
2336 if (vd
->vdev_top_zap
!= 0) {
2338 ASSERT0(zap_lookup_int(spa
->spa_meta_objset
,
2339 spa
->spa_all_vdev_zaps
, vd
->vdev_top_zap
));
2341 if (vd
->vdev_leaf_zap
!= 0) {
2343 ASSERT0(zap_lookup_int(spa
->spa_meta_objset
,
2344 spa
->spa_all_vdev_zaps
, vd
->vdev_leaf_zap
));
2347 for (i
= 0; i
< vd
->vdev_children
; i
++) {
2348 total
+= vdev_count_verify_zaps(vd
->vdev_child
[i
]);
2356 * Determine whether the activity check is required.
2359 spa_activity_check_required(spa_t
*spa
, uberblock_t
*ub
, nvlist_t
*label
,
2363 uint64_t hostid
= 0;
2364 uint64_t tryconfig_txg
= 0;
2365 uint64_t tryconfig_timestamp
= 0;
2368 if (nvlist_exists(config
, ZPOOL_CONFIG_LOAD_INFO
)) {
2369 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
2370 (void) nvlist_lookup_uint64(nvinfo
, ZPOOL_CONFIG_MMP_TXG
,
2372 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_TIMESTAMP
,
2373 &tryconfig_timestamp
);
2376 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
, &state
);
2379 * Disable the MMP activity check - This is used by zdb which
2380 * is intended to be used on potentially active pools.
2382 if (spa
->spa_import_flags
& ZFS_IMPORT_SKIP_MMP
)
2386 * Skip the activity check when the MMP feature is disabled.
2388 if (ub
->ub_mmp_magic
== MMP_MAGIC
&& ub
->ub_mmp_delay
== 0)
2391 * If the tryconfig_* values are nonzero, they are the results of an
2392 * earlier tryimport. If they match the uberblock we just found, then
2393 * the pool has not changed and we return false so we do not test a
2396 if (tryconfig_txg
&& tryconfig_txg
== ub
->ub_txg
&&
2397 tryconfig_timestamp
&& tryconfig_timestamp
== ub
->ub_timestamp
)
2401 * Allow the activity check to be skipped when importing the pool
2402 * on the same host which last imported it. Since the hostid from
2403 * configuration may be stale use the one read from the label.
2405 if (nvlist_exists(label
, ZPOOL_CONFIG_HOSTID
))
2406 hostid
= fnvlist_lookup_uint64(label
, ZPOOL_CONFIG_HOSTID
);
2408 if (hostid
== spa_get_hostid())
2412 * Skip the activity test when the pool was cleanly exported.
2414 if (state
!= POOL_STATE_ACTIVE
)
2421 * Perform the import activity check. If the user canceled the import or
2422 * we detected activity then fail.
2425 spa_activity_check(spa_t
*spa
, uberblock_t
*ub
, nvlist_t
*config
)
2427 uint64_t import_intervals
= MAX(zfs_multihost_import_intervals
, 1);
2428 uint64_t txg
= ub
->ub_txg
;
2429 uint64_t timestamp
= ub
->ub_timestamp
;
2430 uint64_t import_delay
= NANOSEC
;
2431 hrtime_t import_expire
;
2432 nvlist_t
*mmp_label
= NULL
;
2433 vdev_t
*rvd
= spa
->spa_root_vdev
;
2438 cv_init(&cv
, NULL
, CV_DEFAULT
, NULL
);
2439 mutex_init(&mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
2443 * If ZPOOL_CONFIG_MMP_TXG is present an activity check was performed
2444 * during the earlier tryimport. If the txg recorded there is 0 then
2445 * the pool is known to be active on another host.
2447 * Otherwise, the pool might be in use on another node. Check for
2448 * changes in the uberblocks on disk if necessary.
2450 if (nvlist_exists(config
, ZPOOL_CONFIG_LOAD_INFO
)) {
2451 nvlist_t
*nvinfo
= fnvlist_lookup_nvlist(config
,
2452 ZPOOL_CONFIG_LOAD_INFO
);
2454 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_TXG
) &&
2455 fnvlist_lookup_uint64(nvinfo
, ZPOOL_CONFIG_MMP_TXG
) == 0) {
2456 vdev_uberblock_load(rvd
, ub
, &mmp_label
);
2457 error
= SET_ERROR(EREMOTEIO
);
2463 * Preferentially use the zfs_multihost_interval from the node which
2464 * last imported the pool. This value is stored in an MMP uberblock as.
2466 * ub_mmp_delay * vdev_count_leaves() == zfs_multihost_interval
2468 if (ub
->ub_mmp_magic
== MMP_MAGIC
&& ub
->ub_mmp_delay
)
2469 import_delay
= MAX(import_delay
, import_intervals
*
2470 ub
->ub_mmp_delay
* MAX(vdev_count_leaves(spa
), 1));
2472 /* Apply a floor using the local default values. */
2473 import_delay
= MAX(import_delay
, import_intervals
*
2474 MSEC2NSEC(MAX(zfs_multihost_interval
, MMP_MIN_INTERVAL
)));
2476 /* Add a small random factor in case of simultaneous imports (0-25%) */
2477 import_expire
= gethrtime() + import_delay
+
2478 (import_delay
* spa_get_random(250) / 1000);
2480 while (gethrtime() < import_expire
) {
2481 vdev_uberblock_load(rvd
, ub
, &mmp_label
);
2483 if (txg
!= ub
->ub_txg
|| timestamp
!= ub
->ub_timestamp
) {
2484 error
= SET_ERROR(EREMOTEIO
);
2489 nvlist_free(mmp_label
);
2493 error
= cv_timedwait_sig(&cv
, &mtx
, ddi_get_lbolt() + hz
);
2495 error
= SET_ERROR(EINTR
);
2503 mutex_destroy(&mtx
);
2507 * If the pool is determined to be active store the status in the
2508 * spa->spa_load_info nvlist. If the remote hostname or hostid are
2509 * available from configuration read from disk store them as well.
2510 * This allows 'zpool import' to generate a more useful message.
2512 * ZPOOL_CONFIG_MMP_STATE - observed pool status (mandatory)
2513 * ZPOOL_CONFIG_MMP_HOSTNAME - hostname from the active pool
2514 * ZPOOL_CONFIG_MMP_HOSTID - hostid from the active pool
2516 if (error
== EREMOTEIO
) {
2517 char *hostname
= "<unknown>";
2518 uint64_t hostid
= 0;
2521 if (nvlist_exists(mmp_label
, ZPOOL_CONFIG_HOSTNAME
)) {
2522 hostname
= fnvlist_lookup_string(mmp_label
,
2523 ZPOOL_CONFIG_HOSTNAME
);
2524 fnvlist_add_string(spa
->spa_load_info
,
2525 ZPOOL_CONFIG_MMP_HOSTNAME
, hostname
);
2528 if (nvlist_exists(mmp_label
, ZPOOL_CONFIG_HOSTID
)) {
2529 hostid
= fnvlist_lookup_uint64(mmp_label
,
2530 ZPOOL_CONFIG_HOSTID
);
2531 fnvlist_add_uint64(spa
->spa_load_info
,
2532 ZPOOL_CONFIG_MMP_HOSTID
, hostid
);
2536 fnvlist_add_uint64(spa
->spa_load_info
,
2537 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_ACTIVE
);
2538 fnvlist_add_uint64(spa
->spa_load_info
,
2539 ZPOOL_CONFIG_MMP_TXG
, 0);
2541 error
= spa_vdev_err(rvd
, VDEV_AUX_ACTIVE
, EREMOTEIO
);
2545 nvlist_free(mmp_label
);
2551 * Load an existing storage pool, using the pool's builtin spa_config as a
2552 * source of configuration information.
2554 __attribute__((always_inline
))
2556 spa_load_impl(spa_t
*spa
, uint64_t pool_guid
, nvlist_t
*config
,
2557 spa_load_state_t state
, spa_import_type_t type
, boolean_t mosconfig
,
2561 nvlist_t
*nvroot
= NULL
;
2564 uberblock_t
*ub
= &spa
->spa_uberblock
;
2565 uint64_t children
, config_cache_txg
= spa
->spa_config_txg
;
2566 int orig_mode
= spa
->spa_mode
;
2569 boolean_t missing_feat_write
= B_FALSE
;
2570 boolean_t activity_check
= B_FALSE
;
2571 nvlist_t
*mos_config
;
2574 * If this is an untrusted config, access the pool in read-only mode.
2575 * This prevents things like resilvering recently removed devices.
2578 spa
->spa_mode
= FREAD
;
2580 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
2582 spa
->spa_load_state
= state
;
2584 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvroot
))
2585 return (SET_ERROR(EINVAL
));
2587 parse
= (type
== SPA_IMPORT_EXISTING
?
2588 VDEV_ALLOC_LOAD
: VDEV_ALLOC_SPLIT
);
2591 * Create "The Godfather" zio to hold all async IOs
2593 spa
->spa_async_zio_root
= kmem_alloc(max_ncpus
* sizeof (void *),
2595 for (i
= 0; i
< max_ncpus
; i
++) {
2596 spa
->spa_async_zio_root
[i
] = zio_root(spa
, NULL
, NULL
,
2597 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
2598 ZIO_FLAG_GODFATHER
);
2602 * Parse the configuration into a vdev tree. We explicitly set the
2603 * value that will be returned by spa_version() since parsing the
2604 * configuration requires knowing the version number.
2606 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2607 error
= spa_config_parse(spa
, &rvd
, nvroot
, NULL
, 0, parse
);
2608 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2613 ASSERT(spa
->spa_root_vdev
== rvd
);
2614 ASSERT3U(spa
->spa_min_ashift
, >=, SPA_MINBLOCKSHIFT
);
2615 ASSERT3U(spa
->spa_max_ashift
, <=, SPA_MAXBLOCKSHIFT
);
2617 if (type
!= SPA_IMPORT_ASSEMBLE
) {
2618 ASSERT(spa_guid(spa
) == pool_guid
);
2622 * Try to open all vdevs, loading each label in the process.
2624 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2625 error
= vdev_open(rvd
);
2626 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2631 * We need to validate the vdev labels against the configuration that
2632 * we have in hand, which is dependent on the setting of mosconfig. If
2633 * mosconfig is true then we're validating the vdev labels based on
2634 * that config. Otherwise, we're validating against the cached config
2635 * (zpool.cache) that was read when we loaded the zfs module, and then
2636 * later we will recursively call spa_load() and validate against
2639 * If we're assembling a new pool that's been split off from an
2640 * existing pool, the labels haven't yet been updated so we skip
2641 * validation for now.
2643 if (type
!= SPA_IMPORT_ASSEMBLE
) {
2644 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2645 error
= vdev_validate(rvd
, mosconfig
);
2646 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2651 if (rvd
->vdev_state
<= VDEV_STATE_CANT_OPEN
)
2652 return (SET_ERROR(ENXIO
));
2656 * Find the best uberblock.
2658 vdev_uberblock_load(rvd
, ub
, &label
);
2661 * If we weren't able to find a single valid uberblock, return failure.
2663 if (ub
->ub_txg
== 0) {
2665 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, ENXIO
));
2669 * For pools which have the multihost property on determine if the
2670 * pool is truly inactive and can be safely imported. Prevent
2671 * hosts which don't have a hostid set from importing the pool.
2673 activity_check
= spa_activity_check_required(spa
, ub
, label
, config
);
2674 if (activity_check
) {
2675 if (ub
->ub_mmp_magic
== MMP_MAGIC
&& ub
->ub_mmp_delay
&&
2676 spa_get_hostid() == 0) {
2678 fnvlist_add_uint64(spa
->spa_load_info
,
2679 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_NO_HOSTID
);
2680 return (spa_vdev_err(rvd
, VDEV_AUX_ACTIVE
, EREMOTEIO
));
2683 error
= spa_activity_check(spa
, ub
, config
);
2689 fnvlist_add_uint64(spa
->spa_load_info
,
2690 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_INACTIVE
);
2691 fnvlist_add_uint64(spa
->spa_load_info
,
2692 ZPOOL_CONFIG_MMP_TXG
, ub
->ub_txg
);
2696 * If the pool has an unsupported version we can't open it.
2698 if (!SPA_VERSION_IS_SUPPORTED(ub
->ub_version
)) {
2700 return (spa_vdev_err(rvd
, VDEV_AUX_VERSION_NEWER
, ENOTSUP
));
2703 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
2707 * If we weren't able to find what's necessary for reading the
2708 * MOS in the label, return failure.
2710 if (label
== NULL
|| nvlist_lookup_nvlist(label
,
2711 ZPOOL_CONFIG_FEATURES_FOR_READ
, &features
) != 0) {
2713 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
2718 * Update our in-core representation with the definitive values
2721 nvlist_free(spa
->spa_label_features
);
2722 VERIFY(nvlist_dup(features
, &spa
->spa_label_features
, 0) == 0);
2728 * Look through entries in the label nvlist's features_for_read. If
2729 * there is a feature listed there which we don't understand then we
2730 * cannot open a pool.
2732 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
2733 nvlist_t
*unsup_feat
;
2736 VERIFY(nvlist_alloc(&unsup_feat
, NV_UNIQUE_NAME
, KM_SLEEP
) ==
2739 for (nvp
= nvlist_next_nvpair(spa
->spa_label_features
, NULL
);
2741 nvp
= nvlist_next_nvpair(spa
->spa_label_features
, nvp
)) {
2742 if (!zfeature_is_supported(nvpair_name(nvp
))) {
2743 VERIFY(nvlist_add_string(unsup_feat
,
2744 nvpair_name(nvp
), "") == 0);
2748 if (!nvlist_empty(unsup_feat
)) {
2749 VERIFY(nvlist_add_nvlist(spa
->spa_load_info
,
2750 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
) == 0);
2751 nvlist_free(unsup_feat
);
2752 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
2756 nvlist_free(unsup_feat
);
2760 * If the vdev guid sum doesn't match the uberblock, we have an
2761 * incomplete configuration. We first check to see if the pool
2762 * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN).
2763 * If it is, defer the vdev_guid_sum check till later so we
2764 * can handle missing vdevs.
2766 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VDEV_CHILDREN
,
2767 &children
) != 0 && mosconfig
&& type
!= SPA_IMPORT_ASSEMBLE
&&
2768 rvd
->vdev_guid_sum
!= ub
->ub_guid_sum
)
2769 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
, ENXIO
));
2771 if (type
!= SPA_IMPORT_ASSEMBLE
&& spa
->spa_config_splitting
) {
2772 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2773 spa_try_repair(spa
, config
);
2774 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2775 nvlist_free(spa
->spa_config_splitting
);
2776 spa
->spa_config_splitting
= NULL
;
2780 * Initialize internal SPA structures.
2782 spa
->spa_state
= POOL_STATE_ACTIVE
;
2783 spa
->spa_ubsync
= spa
->spa_uberblock
;
2784 spa
->spa_verify_min_txg
= spa
->spa_extreme_rewind
?
2785 TXG_INITIAL
- 1 : spa_last_synced_txg(spa
) - TXG_DEFER_SIZE
- 1;
2786 spa
->spa_first_txg
= spa
->spa_last_ubsync_txg
?
2787 spa
->spa_last_ubsync_txg
: spa_last_synced_txg(spa
) + 1;
2788 spa
->spa_claim_max_txg
= spa
->spa_first_txg
;
2789 spa
->spa_prev_software_version
= ub
->ub_software_version
;
2791 error
= dsl_pool_init(spa
, spa
->spa_first_txg
, &spa
->spa_dsl_pool
);
2793 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2794 spa
->spa_meta_objset
= spa
->spa_dsl_pool
->dp_meta_objset
;
2796 if (spa_dir_prop(spa
, DMU_POOL_CONFIG
, &spa
->spa_config_object
) != 0)
2797 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2799 if (spa_version(spa
) >= SPA_VERSION_FEATURES
) {
2800 boolean_t missing_feat_read
= B_FALSE
;
2801 nvlist_t
*unsup_feat
, *enabled_feat
;
2804 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_READ
,
2805 &spa
->spa_feat_for_read_obj
) != 0) {
2806 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2809 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_WRITE
,
2810 &spa
->spa_feat_for_write_obj
) != 0) {
2811 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2814 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_DESCRIPTIONS
,
2815 &spa
->spa_feat_desc_obj
) != 0) {
2816 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2819 enabled_feat
= fnvlist_alloc();
2820 unsup_feat
= fnvlist_alloc();
2822 if (!spa_features_check(spa
, B_FALSE
,
2823 unsup_feat
, enabled_feat
))
2824 missing_feat_read
= B_TRUE
;
2826 if (spa_writeable(spa
) || state
== SPA_LOAD_TRYIMPORT
) {
2827 if (!spa_features_check(spa
, B_TRUE
,
2828 unsup_feat
, enabled_feat
)) {
2829 missing_feat_write
= B_TRUE
;
2833 fnvlist_add_nvlist(spa
->spa_load_info
,
2834 ZPOOL_CONFIG_ENABLED_FEAT
, enabled_feat
);
2836 if (!nvlist_empty(unsup_feat
)) {
2837 fnvlist_add_nvlist(spa
->spa_load_info
,
2838 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
);
2841 fnvlist_free(enabled_feat
);
2842 fnvlist_free(unsup_feat
);
2844 if (!missing_feat_read
) {
2845 fnvlist_add_boolean(spa
->spa_load_info
,
2846 ZPOOL_CONFIG_CAN_RDONLY
);
2850 * If the state is SPA_LOAD_TRYIMPORT, our objective is
2851 * twofold: to determine whether the pool is available for
2852 * import in read-write mode and (if it is not) whether the
2853 * pool is available for import in read-only mode. If the pool
2854 * is available for import in read-write mode, it is displayed
2855 * as available in userland; if it is not available for import
2856 * in read-only mode, it is displayed as unavailable in
2857 * userland. If the pool is available for import in read-only
2858 * mode but not read-write mode, it is displayed as unavailable
2859 * in userland with a special note that the pool is actually
2860 * available for open in read-only mode.
2862 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
2863 * missing a feature for write, we must first determine whether
2864 * the pool can be opened read-only before returning to
2865 * userland in order to know whether to display the
2866 * abovementioned note.
2868 if (missing_feat_read
|| (missing_feat_write
&&
2869 spa_writeable(spa
))) {
2870 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
2875 * Load refcounts for ZFS features from disk into an in-memory
2876 * cache during SPA initialization.
2878 for (i
= 0; i
< SPA_FEATURES
; i
++) {
2881 error
= feature_get_refcount_from_disk(spa
,
2882 &spa_feature_table
[i
], &refcount
);
2884 spa
->spa_feat_refcount_cache
[i
] = refcount
;
2885 } else if (error
== ENOTSUP
) {
2886 spa
->spa_feat_refcount_cache
[i
] =
2887 SPA_FEATURE_DISABLED
;
2889 return (spa_vdev_err(rvd
,
2890 VDEV_AUX_CORRUPT_DATA
, EIO
));
2895 if (spa_feature_is_active(spa
, SPA_FEATURE_ENABLED_TXG
)) {
2896 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_ENABLED_TXG
,
2897 &spa
->spa_feat_enabled_txg_obj
) != 0)
2898 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2901 spa
->spa_is_initializing
= B_TRUE
;
2902 error
= dsl_pool_open(spa
->spa_dsl_pool
);
2903 spa
->spa_is_initializing
= B_FALSE
;
2905 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2909 nvlist_t
*policy
= NULL
, *nvconfig
;
2911 if (load_nvlist(spa
, spa
->spa_config_object
, &nvconfig
) != 0)
2912 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2914 if (!spa_is_root(spa
) && nvlist_lookup_uint64(nvconfig
,
2915 ZPOOL_CONFIG_HOSTID
, &hostid
) == 0) {
2917 unsigned long myhostid
= 0;
2919 VERIFY(nvlist_lookup_string(nvconfig
,
2920 ZPOOL_CONFIG_HOSTNAME
, &hostname
) == 0);
2922 myhostid
= spa_get_hostid();
2923 if (hostid
&& myhostid
&& hostid
!= myhostid
) {
2924 nvlist_free(nvconfig
);
2925 return (SET_ERROR(EBADF
));
2928 if (nvlist_lookup_nvlist(spa
->spa_config
,
2929 ZPOOL_REWIND_POLICY
, &policy
) == 0)
2930 VERIFY(nvlist_add_nvlist(nvconfig
,
2931 ZPOOL_REWIND_POLICY
, policy
) == 0);
2933 spa_config_set(spa
, nvconfig
);
2935 spa_deactivate(spa
);
2936 spa_activate(spa
, orig_mode
);
2938 return (spa_load(spa
, state
, SPA_IMPORT_EXISTING
, B_TRUE
));
2941 /* Grab the checksum salt from the MOS. */
2942 error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
2943 DMU_POOL_CHECKSUM_SALT
, 1,
2944 sizeof (spa
->spa_cksum_salt
.zcs_bytes
),
2945 spa
->spa_cksum_salt
.zcs_bytes
);
2946 if (error
== ENOENT
) {
2947 /* Generate a new salt for subsequent use */
2948 (void) random_get_pseudo_bytes(spa
->spa_cksum_salt
.zcs_bytes
,
2949 sizeof (spa
->spa_cksum_salt
.zcs_bytes
));
2950 } else if (error
!= 0) {
2951 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2954 if (spa_dir_prop(spa
, DMU_POOL_SYNC_BPOBJ
, &obj
) != 0)
2955 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2956 error
= bpobj_open(&spa
->spa_deferred_bpobj
, spa
->spa_meta_objset
, obj
);
2958 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2961 * Load the bit that tells us to use the new accounting function
2962 * (raid-z deflation). If we have an older pool, this will not
2965 error
= spa_dir_prop(spa
, DMU_POOL_DEFLATE
, &spa
->spa_deflate
);
2966 if (error
!= 0 && error
!= ENOENT
)
2967 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2969 error
= spa_dir_prop(spa
, DMU_POOL_CREATION_VERSION
,
2970 &spa
->spa_creation_version
);
2971 if (error
!= 0 && error
!= ENOENT
)
2972 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2975 * Load the persistent error log. If we have an older pool, this will
2978 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_LAST
, &spa
->spa_errlog_last
);
2979 if (error
!= 0 && error
!= ENOENT
)
2980 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2982 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_SCRUB
,
2983 &spa
->spa_errlog_scrub
);
2984 if (error
!= 0 && error
!= ENOENT
)
2985 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2988 * Load the history object. If we have an older pool, this
2989 * will not be present.
2991 error
= spa_dir_prop(spa
, DMU_POOL_HISTORY
, &spa
->spa_history
);
2992 if (error
!= 0 && error
!= ENOENT
)
2993 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2996 * Load the per-vdev ZAP map. If we have an older pool, this will not
2997 * be present; in this case, defer its creation to a later time to
2998 * avoid dirtying the MOS this early / out of sync context. See
2999 * spa_sync_config_object.
3002 /* The sentinel is only available in the MOS config. */
3003 if (load_nvlist(spa
, spa
->spa_config_object
, &mos_config
) != 0)
3004 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3006 error
= spa_dir_prop(spa
, DMU_POOL_VDEV_ZAP_MAP
,
3007 &spa
->spa_all_vdev_zaps
);
3009 if (error
== ENOENT
) {
3010 VERIFY(!nvlist_exists(mos_config
,
3011 ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
));
3012 spa
->spa_avz_action
= AVZ_ACTION_INITIALIZE
;
3013 ASSERT0(vdev_count_verify_zaps(spa
->spa_root_vdev
));
3014 } else if (error
!= 0) {
3015 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3016 } else if (!nvlist_exists(mos_config
, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
)) {
3018 * An older version of ZFS overwrote the sentinel value, so
3019 * we have orphaned per-vdev ZAPs in the MOS. Defer their
3020 * destruction to later; see spa_sync_config_object.
3022 spa
->spa_avz_action
= AVZ_ACTION_DESTROY
;
3024 * We're assuming that no vdevs have had their ZAPs created
3025 * before this. Better be sure of it.
3027 ASSERT0(vdev_count_verify_zaps(spa
->spa_root_vdev
));
3029 nvlist_free(mos_config
);
3032 * If we're assembling the pool from the split-off vdevs of
3033 * an existing pool, we don't want to attach the spares & cache
3038 * Load any hot spares for this pool.
3040 error
= spa_dir_prop(spa
, DMU_POOL_SPARES
, &spa
->spa_spares
.sav_object
);
3041 if (error
!= 0 && error
!= ENOENT
)
3042 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3043 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
3044 ASSERT(spa_version(spa
) >= SPA_VERSION_SPARES
);
3045 if (load_nvlist(spa
, spa
->spa_spares
.sav_object
,
3046 &spa
->spa_spares
.sav_config
) != 0)
3047 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3049 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3050 spa_load_spares(spa
);
3051 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3052 } else if (error
== 0) {
3053 spa
->spa_spares
.sav_sync
= B_TRUE
;
3057 * Load any level 2 ARC devices for this pool.
3059 error
= spa_dir_prop(spa
, DMU_POOL_L2CACHE
,
3060 &spa
->spa_l2cache
.sav_object
);
3061 if (error
!= 0 && error
!= ENOENT
)
3062 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3063 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
3064 ASSERT(spa_version(spa
) >= SPA_VERSION_L2CACHE
);
3065 if (load_nvlist(spa
, spa
->spa_l2cache
.sav_object
,
3066 &spa
->spa_l2cache
.sav_config
) != 0)
3067 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3069 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3070 spa_load_l2cache(spa
);
3071 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3072 } else if (error
== 0) {
3073 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
3076 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
3078 error
= spa_dir_prop(spa
, DMU_POOL_PROPS
, &spa
->spa_pool_props_object
);
3079 if (error
&& error
!= ENOENT
)
3080 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3083 uint64_t autoreplace
= 0;
3085 spa_prop_find(spa
, ZPOOL_PROP_BOOTFS
, &spa
->spa_bootfs
);
3086 spa_prop_find(spa
, ZPOOL_PROP_AUTOREPLACE
, &autoreplace
);
3087 spa_prop_find(spa
, ZPOOL_PROP_DELEGATION
, &spa
->spa_delegation
);
3088 spa_prop_find(spa
, ZPOOL_PROP_FAILUREMODE
, &spa
->spa_failmode
);
3089 spa_prop_find(spa
, ZPOOL_PROP_AUTOEXPAND
, &spa
->spa_autoexpand
);
3090 spa_prop_find(spa
, ZPOOL_PROP_MULTIHOST
, &spa
->spa_multihost
);
3091 spa_prop_find(spa
, ZPOOL_PROP_DEDUPDITTO
,
3092 &spa
->spa_dedup_ditto
);
3094 spa
->spa_autoreplace
= (autoreplace
!= 0);
3098 * If the 'multihost' property is set, then never allow a pool to
3099 * be imported when the system hostid is zero. The exception to
3100 * this rule is zdb which is always allowed to access pools.
3102 if (spa_multihost(spa
) && spa_get_hostid() == 0 &&
3103 (spa
->spa_import_flags
& ZFS_IMPORT_SKIP_MMP
) == 0) {
3104 fnvlist_add_uint64(spa
->spa_load_info
,
3105 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_NO_HOSTID
);
3106 return (spa_vdev_err(rvd
, VDEV_AUX_ACTIVE
, EREMOTEIO
));
3110 * If the 'autoreplace' property is set, then post a resource notifying
3111 * the ZFS DE that it should not issue any faults for unopenable
3112 * devices. We also iterate over the vdevs, and post a sysevent for any
3113 * unopenable vdevs so that the normal autoreplace handler can take
3116 if (spa
->spa_autoreplace
&& state
!= SPA_LOAD_TRYIMPORT
) {
3117 spa_check_removed(spa
->spa_root_vdev
);
3119 * For the import case, this is done in spa_import(), because
3120 * at this point we're using the spare definitions from
3121 * the MOS config, not necessarily from the userland config.
3123 if (state
!= SPA_LOAD_IMPORT
) {
3124 spa_aux_check_removed(&spa
->spa_spares
);
3125 spa_aux_check_removed(&spa
->spa_l2cache
);
3130 * Load the vdev state for all toplevel vdevs.
3135 * Propagate the leaf DTLs we just loaded all the way up the tree.
3137 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3138 vdev_dtl_reassess(rvd
, 0, 0, B_FALSE
);
3139 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3142 * Load the DDTs (dedup tables).
3144 error
= ddt_load(spa
);
3146 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3148 spa_update_dspace(spa
);
3151 * Validate the config, using the MOS config to fill in any
3152 * information which might be missing. If we fail to validate
3153 * the config then declare the pool unfit for use. If we're
3154 * assembling a pool from a split, the log is not transferred
3157 if (type
!= SPA_IMPORT_ASSEMBLE
) {
3160 if (load_nvlist(spa
, spa
->spa_config_object
, &nvconfig
) != 0)
3161 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3163 if (!spa_config_valid(spa
, nvconfig
)) {
3164 nvlist_free(nvconfig
);
3165 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
,
3168 nvlist_free(nvconfig
);
3171 * Now that we've validated the config, check the state of the
3172 * root vdev. If it can't be opened, it indicates one or
3173 * more toplevel vdevs are faulted.
3175 if (rvd
->vdev_state
<= VDEV_STATE_CANT_OPEN
)
3176 return (SET_ERROR(ENXIO
));
3178 if (spa_writeable(spa
) && spa_check_logs(spa
)) {
3179 *ereport
= FM_EREPORT_ZFS_LOG_REPLAY
;
3180 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_LOG
, ENXIO
));
3184 if (missing_feat_write
) {
3185 ASSERT(state
== SPA_LOAD_TRYIMPORT
);
3188 * At this point, we know that we can open the pool in
3189 * read-only mode but not read-write mode. We now have enough
3190 * information and can return to userland.
3192 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
, ENOTSUP
));
3196 * We've successfully opened the pool, verify that we're ready
3197 * to start pushing transactions.
3199 if (state
!= SPA_LOAD_TRYIMPORT
) {
3200 if ((error
= spa_load_verify(spa
)))
3201 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
3205 if (spa_writeable(spa
) && (state
== SPA_LOAD_RECOVER
||
3206 spa
->spa_load_max_txg
== UINT64_MAX
)) {
3208 int need_update
= B_FALSE
;
3209 dsl_pool_t
*dp
= spa_get_dsl(spa
);
3212 ASSERT(state
!= SPA_LOAD_TRYIMPORT
);
3215 * Claim log blocks that haven't been committed yet.
3216 * This must all happen in a single txg.
3217 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
3218 * invoked from zil_claim_log_block()'s i/o done callback.
3219 * Price of rollback is that we abandon the log.
3221 spa
->spa_claiming
= B_TRUE
;
3223 tx
= dmu_tx_create_assigned(dp
, spa_first_txg(spa
));
3224 (void) dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
3225 zil_claim
, tx
, DS_FIND_CHILDREN
);
3228 spa
->spa_claiming
= B_FALSE
;
3230 spa_set_log_state(spa
, SPA_LOG_GOOD
);
3231 spa
->spa_sync_on
= B_TRUE
;
3232 txg_sync_start(spa
->spa_dsl_pool
);
3233 mmp_thread_start(spa
);
3236 * Wait for all claims to sync. We sync up to the highest
3237 * claimed log block birth time so that claimed log blocks
3238 * don't appear to be from the future. spa_claim_max_txg
3239 * will have been set for us by either zil_check_log_chain()
3240 * (invoked from spa_check_logs()) or zil_claim() above.
3242 txg_wait_synced(spa
->spa_dsl_pool
, spa
->spa_claim_max_txg
);
3245 * If the config cache is stale, or we have uninitialized
3246 * metaslabs (see spa_vdev_add()), then update the config.
3248 * If this is a verbatim import, trust the current
3249 * in-core spa_config and update the disk labels.
3251 if (config_cache_txg
!= spa
->spa_config_txg
||
3252 state
== SPA_LOAD_IMPORT
||
3253 state
== SPA_LOAD_RECOVER
||
3254 (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
))
3255 need_update
= B_TRUE
;
3257 for (c
= 0; c
< rvd
->vdev_children
; c
++)
3258 if (rvd
->vdev_child
[c
]->vdev_ms_array
== 0)
3259 need_update
= B_TRUE
;
3262 * Update the config cache asychronously in case we're the
3263 * root pool, in which case the config cache isn't writable yet.
3266 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
3269 * Check all DTLs to see if anything needs resilvering.
3271 if (!dsl_scan_resilvering(spa
->spa_dsl_pool
) &&
3272 vdev_resilver_needed(rvd
, NULL
, NULL
))
3273 spa_async_request(spa
, SPA_ASYNC_RESILVER
);
3276 * Log the fact that we booted up (so that we can detect if
3277 * we rebooted in the middle of an operation).
3279 spa_history_log_version(spa
, "open", NULL
);
3282 * Delete any inconsistent datasets.
3284 (void) dmu_objset_find(spa_name(spa
),
3285 dsl_destroy_inconsistent
, NULL
, DS_FIND_CHILDREN
);
3288 * Clean up any stale temporary dataset userrefs.
3290 dsl_pool_clean_tmp_userrefs(spa
->spa_dsl_pool
);
3297 spa_load_retry(spa_t
*spa
, spa_load_state_t state
, int mosconfig
)
3299 int mode
= spa
->spa_mode
;
3302 spa_deactivate(spa
);
3304 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
- 1;
3306 spa_activate(spa
, mode
);
3307 spa_async_suspend(spa
);
3309 return (spa_load(spa
, state
, SPA_IMPORT_EXISTING
, mosconfig
));
3313 * If spa_load() fails this function will try loading prior txg's. If
3314 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
3315 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
3316 * function will not rewind the pool and will return the same error as
3320 spa_load_best(spa_t
*spa
, spa_load_state_t state
, int mosconfig
,
3321 uint64_t max_request
, int rewind_flags
)
3323 nvlist_t
*loadinfo
= NULL
;
3324 nvlist_t
*config
= NULL
;
3325 int load_error
, rewind_error
;
3326 uint64_t safe_rewind_txg
;
3329 if (spa
->spa_load_txg
&& state
== SPA_LOAD_RECOVER
) {
3330 spa
->spa_load_max_txg
= spa
->spa_load_txg
;
3331 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
3333 spa
->spa_load_max_txg
= max_request
;
3334 if (max_request
!= UINT64_MAX
)
3335 spa
->spa_extreme_rewind
= B_TRUE
;
3338 load_error
= rewind_error
= spa_load(spa
, state
, SPA_IMPORT_EXISTING
,
3340 if (load_error
== 0)
3343 if (spa
->spa_root_vdev
!= NULL
)
3344 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
3346 spa
->spa_last_ubsync_txg
= spa
->spa_uberblock
.ub_txg
;
3347 spa
->spa_last_ubsync_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
3349 if (rewind_flags
& ZPOOL_NEVER_REWIND
) {
3350 nvlist_free(config
);
3351 return (load_error
);
3354 if (state
== SPA_LOAD_RECOVER
) {
3355 /* Price of rolling back is discarding txgs, including log */
3356 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
3359 * If we aren't rolling back save the load info from our first
3360 * import attempt so that we can restore it after attempting
3363 loadinfo
= spa
->spa_load_info
;
3364 spa
->spa_load_info
= fnvlist_alloc();
3367 spa
->spa_load_max_txg
= spa
->spa_last_ubsync_txg
;
3368 safe_rewind_txg
= spa
->spa_last_ubsync_txg
- TXG_DEFER_SIZE
;
3369 min_txg
= (rewind_flags
& ZPOOL_EXTREME_REWIND
) ?
3370 TXG_INITIAL
: safe_rewind_txg
;
3373 * Continue as long as we're finding errors, we're still within
3374 * the acceptable rewind range, and we're still finding uberblocks
3376 while (rewind_error
&& spa
->spa_uberblock
.ub_txg
>= min_txg
&&
3377 spa
->spa_uberblock
.ub_txg
<= spa
->spa_load_max_txg
) {
3378 if (spa
->spa_load_max_txg
< safe_rewind_txg
)
3379 spa
->spa_extreme_rewind
= B_TRUE
;
3380 rewind_error
= spa_load_retry(spa
, state
, mosconfig
);
3383 spa
->spa_extreme_rewind
= B_FALSE
;
3384 spa
->spa_load_max_txg
= UINT64_MAX
;
3386 if (config
&& (rewind_error
|| state
!= SPA_LOAD_RECOVER
))
3387 spa_config_set(spa
, config
);
3389 nvlist_free(config
);
3391 if (state
== SPA_LOAD_RECOVER
) {
3392 ASSERT3P(loadinfo
, ==, NULL
);
3393 return (rewind_error
);
3395 /* Store the rewind info as part of the initial load info */
3396 fnvlist_add_nvlist(loadinfo
, ZPOOL_CONFIG_REWIND_INFO
,
3397 spa
->spa_load_info
);
3399 /* Restore the initial load info */
3400 fnvlist_free(spa
->spa_load_info
);
3401 spa
->spa_load_info
= loadinfo
;
3403 return (load_error
);
3410 * The import case is identical to an open except that the configuration is sent
3411 * down from userland, instead of grabbed from the configuration cache. For the
3412 * case of an open, the pool configuration will exist in the
3413 * POOL_STATE_UNINITIALIZED state.
3415 * The stats information (gen/count/ustats) is used to gather vdev statistics at
3416 * the same time open the pool, without having to keep around the spa_t in some
3420 spa_open_common(const char *pool
, spa_t
**spapp
, void *tag
, nvlist_t
*nvpolicy
,
3424 spa_load_state_t state
= SPA_LOAD_OPEN
;
3426 int locked
= B_FALSE
;
3427 int firstopen
= B_FALSE
;
3432 * As disgusting as this is, we need to support recursive calls to this
3433 * function because dsl_dir_open() is called during spa_load(), and ends
3434 * up calling spa_open() again. The real fix is to figure out how to
3435 * avoid dsl_dir_open() calling this in the first place.
3437 if (mutex_owner(&spa_namespace_lock
) != curthread
) {
3438 mutex_enter(&spa_namespace_lock
);
3442 if ((spa
= spa_lookup(pool
)) == NULL
) {
3444 mutex_exit(&spa_namespace_lock
);
3445 return (SET_ERROR(ENOENT
));
3448 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
) {
3449 zpool_rewind_policy_t policy
;
3453 zpool_get_rewind_policy(nvpolicy
? nvpolicy
: spa
->spa_config
,
3455 if (policy
.zrp_request
& ZPOOL_DO_REWIND
)
3456 state
= SPA_LOAD_RECOVER
;
3458 spa_activate(spa
, spa_mode_global
);
3460 if (state
!= SPA_LOAD_RECOVER
)
3461 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
3463 error
= spa_load_best(spa
, state
, B_FALSE
, policy
.zrp_txg
,
3464 policy
.zrp_request
);
3466 if (error
== EBADF
) {
3468 * If vdev_validate() returns failure (indicated by
3469 * EBADF), it indicates that one of the vdevs indicates
3470 * that the pool has been exported or destroyed. If
3471 * this is the case, the config cache is out of sync and
3472 * we should remove the pool from the namespace.
3475 spa_deactivate(spa
);
3476 spa_config_sync(spa
, B_TRUE
, B_TRUE
);
3479 mutex_exit(&spa_namespace_lock
);
3480 return (SET_ERROR(ENOENT
));
3485 * We can't open the pool, but we still have useful
3486 * information: the state of each vdev after the
3487 * attempted vdev_open(). Return this to the user.
3489 if (config
!= NULL
&& spa
->spa_config
) {
3490 VERIFY(nvlist_dup(spa
->spa_config
, config
,
3492 VERIFY(nvlist_add_nvlist(*config
,
3493 ZPOOL_CONFIG_LOAD_INFO
,
3494 spa
->spa_load_info
) == 0);
3497 spa_deactivate(spa
);
3498 spa
->spa_last_open_failed
= error
;
3500 mutex_exit(&spa_namespace_lock
);
3506 spa_open_ref(spa
, tag
);
3509 *config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
3512 * If we've recovered the pool, pass back any information we
3513 * gathered while doing the load.
3515 if (state
== SPA_LOAD_RECOVER
) {
3516 VERIFY(nvlist_add_nvlist(*config
, ZPOOL_CONFIG_LOAD_INFO
,
3517 spa
->spa_load_info
) == 0);
3521 spa
->spa_last_open_failed
= 0;
3522 spa
->spa_last_ubsync_txg
= 0;
3523 spa
->spa_load_txg
= 0;
3524 mutex_exit(&spa_namespace_lock
);
3528 zvol_create_minors(spa
, spa_name(spa
), B_TRUE
);
3536 spa_open_rewind(const char *name
, spa_t
**spapp
, void *tag
, nvlist_t
*policy
,
3539 return (spa_open_common(name
, spapp
, tag
, policy
, config
));
3543 spa_open(const char *name
, spa_t
**spapp
, void *tag
)
3545 return (spa_open_common(name
, spapp
, tag
, NULL
, NULL
));
3549 * Lookup the given spa_t, incrementing the inject count in the process,
3550 * preventing it from being exported or destroyed.
3553 spa_inject_addref(char *name
)
3557 mutex_enter(&spa_namespace_lock
);
3558 if ((spa
= spa_lookup(name
)) == NULL
) {
3559 mutex_exit(&spa_namespace_lock
);
3562 spa
->spa_inject_ref
++;
3563 mutex_exit(&spa_namespace_lock
);
3569 spa_inject_delref(spa_t
*spa
)
3571 mutex_enter(&spa_namespace_lock
);
3572 spa
->spa_inject_ref
--;
3573 mutex_exit(&spa_namespace_lock
);
3577 * Add spares device information to the nvlist.
3580 spa_add_spares(spa_t
*spa
, nvlist_t
*config
)
3590 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
3592 if (spa
->spa_spares
.sav_count
== 0)
3595 VERIFY(nvlist_lookup_nvlist(config
,
3596 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
3597 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
3598 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
3600 VERIFY(nvlist_add_nvlist_array(nvroot
,
3601 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
3602 VERIFY(nvlist_lookup_nvlist_array(nvroot
,
3603 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
3606 * Go through and find any spares which have since been
3607 * repurposed as an active spare. If this is the case, update
3608 * their status appropriately.
3610 for (i
= 0; i
< nspares
; i
++) {
3611 VERIFY(nvlist_lookup_uint64(spares
[i
],
3612 ZPOOL_CONFIG_GUID
, &guid
) == 0);
3613 if (spa_spare_exists(guid
, &pool
, NULL
) &&
3615 VERIFY(nvlist_lookup_uint64_array(
3616 spares
[i
], ZPOOL_CONFIG_VDEV_STATS
,
3617 (uint64_t **)&vs
, &vsc
) == 0);
3618 vs
->vs_state
= VDEV_STATE_CANT_OPEN
;
3619 vs
->vs_aux
= VDEV_AUX_SPARED
;
3626 * Add l2cache device information to the nvlist, including vdev stats.
3629 spa_add_l2cache(spa_t
*spa
, nvlist_t
*config
)
3632 uint_t i
, j
, nl2cache
;
3639 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
3641 if (spa
->spa_l2cache
.sav_count
== 0)
3644 VERIFY(nvlist_lookup_nvlist(config
,
3645 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
3646 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_l2cache
.sav_config
,
3647 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
3648 if (nl2cache
!= 0) {
3649 VERIFY(nvlist_add_nvlist_array(nvroot
,
3650 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
3651 VERIFY(nvlist_lookup_nvlist_array(nvroot
,
3652 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
3655 * Update level 2 cache device stats.
3658 for (i
= 0; i
< nl2cache
; i
++) {
3659 VERIFY(nvlist_lookup_uint64(l2cache
[i
],
3660 ZPOOL_CONFIG_GUID
, &guid
) == 0);
3663 for (j
= 0; j
< spa
->spa_l2cache
.sav_count
; j
++) {
3665 spa
->spa_l2cache
.sav_vdevs
[j
]->vdev_guid
) {
3666 vd
= spa
->spa_l2cache
.sav_vdevs
[j
];
3672 VERIFY(nvlist_lookup_uint64_array(l2cache
[i
],
3673 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
, &vsc
)
3675 vdev_get_stats(vd
, vs
);
3676 vdev_config_generate_stats(vd
, l2cache
[i
]);
3683 spa_feature_stats_from_disk(spa_t
*spa
, nvlist_t
*features
)
3688 if (spa
->spa_feat_for_read_obj
!= 0) {
3689 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
3690 spa
->spa_feat_for_read_obj
);
3691 zap_cursor_retrieve(&zc
, &za
) == 0;
3692 zap_cursor_advance(&zc
)) {
3693 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
3694 za
.za_num_integers
== 1);
3695 VERIFY0(nvlist_add_uint64(features
, za
.za_name
,
3696 za
.za_first_integer
));
3698 zap_cursor_fini(&zc
);
3701 if (spa
->spa_feat_for_write_obj
!= 0) {
3702 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
3703 spa
->spa_feat_for_write_obj
);
3704 zap_cursor_retrieve(&zc
, &za
) == 0;
3705 zap_cursor_advance(&zc
)) {
3706 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
3707 za
.za_num_integers
== 1);
3708 VERIFY0(nvlist_add_uint64(features
, za
.za_name
,
3709 za
.za_first_integer
));
3711 zap_cursor_fini(&zc
);
3716 spa_feature_stats_from_cache(spa_t
*spa
, nvlist_t
*features
)
3720 for (i
= 0; i
< SPA_FEATURES
; i
++) {
3721 zfeature_info_t feature
= spa_feature_table
[i
];
3724 if (feature_get_refcount(spa
, &feature
, &refcount
) != 0)
3727 VERIFY0(nvlist_add_uint64(features
, feature
.fi_guid
, refcount
));
3732 * Store a list of pool features and their reference counts in the
3735 * The first time this is called on a spa, allocate a new nvlist, fetch
3736 * the pool features and reference counts from disk, then save the list
3737 * in the spa. In subsequent calls on the same spa use the saved nvlist
3738 * and refresh its values from the cached reference counts. This
3739 * ensures we don't block here on I/O on a suspended pool so 'zpool
3740 * clear' can resume the pool.
3743 spa_add_feature_stats(spa_t
*spa
, nvlist_t
*config
)
3747 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
3749 mutex_enter(&spa
->spa_feat_stats_lock
);
3750 features
= spa
->spa_feat_stats
;
3752 if (features
!= NULL
) {
3753 spa_feature_stats_from_cache(spa
, features
);
3755 VERIFY0(nvlist_alloc(&features
, NV_UNIQUE_NAME
, KM_SLEEP
));
3756 spa
->spa_feat_stats
= features
;
3757 spa_feature_stats_from_disk(spa
, features
);
3760 VERIFY0(nvlist_add_nvlist(config
, ZPOOL_CONFIG_FEATURE_STATS
,
3763 mutex_exit(&spa
->spa_feat_stats_lock
);
3767 spa_get_stats(const char *name
, nvlist_t
**config
,
3768 char *altroot
, size_t buflen
)
3774 error
= spa_open_common(name
, &spa
, FTAG
, NULL
, config
);
3778 * This still leaves a window of inconsistency where the spares
3779 * or l2cache devices could change and the config would be
3780 * self-inconsistent.
3782 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
3784 if (*config
!= NULL
) {
3785 uint64_t loadtimes
[2];
3787 loadtimes
[0] = spa
->spa_loaded_ts
.tv_sec
;
3788 loadtimes
[1] = spa
->spa_loaded_ts
.tv_nsec
;
3789 VERIFY(nvlist_add_uint64_array(*config
,
3790 ZPOOL_CONFIG_LOADED_TIME
, loadtimes
, 2) == 0);
3792 VERIFY(nvlist_add_uint64(*config
,
3793 ZPOOL_CONFIG_ERRCOUNT
,
3794 spa_get_errlog_size(spa
)) == 0);
3796 if (spa_suspended(spa
))
3797 VERIFY(nvlist_add_uint64(*config
,
3798 ZPOOL_CONFIG_SUSPENDED
,
3799 spa
->spa_failmode
) == 0);
3801 spa_add_spares(spa
, *config
);
3802 spa_add_l2cache(spa
, *config
);
3803 spa_add_feature_stats(spa
, *config
);
3808 * We want to get the alternate root even for faulted pools, so we cheat
3809 * and call spa_lookup() directly.
3813 mutex_enter(&spa_namespace_lock
);
3814 spa
= spa_lookup(name
);
3816 spa_altroot(spa
, altroot
, buflen
);
3820 mutex_exit(&spa_namespace_lock
);
3822 spa_altroot(spa
, altroot
, buflen
);
3827 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
3828 spa_close(spa
, FTAG
);
3835 * Validate that the auxiliary device array is well formed. We must have an
3836 * array of nvlists, each which describes a valid leaf vdev. If this is an
3837 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
3838 * specified, as long as they are well-formed.
3841 spa_validate_aux_devs(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
,
3842 spa_aux_vdev_t
*sav
, const char *config
, uint64_t version
,
3843 vdev_labeltype_t label
)
3850 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
3853 * It's acceptable to have no devs specified.
3855 if (nvlist_lookup_nvlist_array(nvroot
, config
, &dev
, &ndev
) != 0)
3859 return (SET_ERROR(EINVAL
));
3862 * Make sure the pool is formatted with a version that supports this
3865 if (spa_version(spa
) < version
)
3866 return (SET_ERROR(ENOTSUP
));
3869 * Set the pending device list so we correctly handle device in-use
3872 sav
->sav_pending
= dev
;
3873 sav
->sav_npending
= ndev
;
3875 for (i
= 0; i
< ndev
; i
++) {
3876 if ((error
= spa_config_parse(spa
, &vd
, dev
[i
], NULL
, 0,
3880 if (!vd
->vdev_ops
->vdev_op_leaf
) {
3882 error
= SET_ERROR(EINVAL
);
3888 if ((error
= vdev_open(vd
)) == 0 &&
3889 (error
= vdev_label_init(vd
, crtxg
, label
)) == 0) {
3890 VERIFY(nvlist_add_uint64(dev
[i
], ZPOOL_CONFIG_GUID
,
3891 vd
->vdev_guid
) == 0);
3897 (mode
!= VDEV_ALLOC_SPARE
&& mode
!= VDEV_ALLOC_L2CACHE
))
3904 sav
->sav_pending
= NULL
;
3905 sav
->sav_npending
= 0;
3910 spa_validate_aux(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
)
3914 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
3916 if ((error
= spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
3917 &spa
->spa_spares
, ZPOOL_CONFIG_SPARES
, SPA_VERSION_SPARES
,
3918 VDEV_LABEL_SPARE
)) != 0) {
3922 return (spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
3923 &spa
->spa_l2cache
, ZPOOL_CONFIG_L2CACHE
, SPA_VERSION_L2CACHE
,
3924 VDEV_LABEL_L2CACHE
));
3928 spa_set_aux_vdevs(spa_aux_vdev_t
*sav
, nvlist_t
**devs
, int ndevs
,
3933 if (sav
->sav_config
!= NULL
) {
3939 * Generate new dev list by concatenating with the
3942 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
, config
,
3943 &olddevs
, &oldndevs
) == 0);
3945 newdevs
= kmem_alloc(sizeof (void *) *
3946 (ndevs
+ oldndevs
), KM_SLEEP
);
3947 for (i
= 0; i
< oldndevs
; i
++)
3948 VERIFY(nvlist_dup(olddevs
[i
], &newdevs
[i
],
3950 for (i
= 0; i
< ndevs
; i
++)
3951 VERIFY(nvlist_dup(devs
[i
], &newdevs
[i
+ oldndevs
],
3954 VERIFY(nvlist_remove(sav
->sav_config
, config
,
3955 DATA_TYPE_NVLIST_ARRAY
) == 0);
3957 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
,
3958 config
, newdevs
, ndevs
+ oldndevs
) == 0);
3959 for (i
= 0; i
< oldndevs
+ ndevs
; i
++)
3960 nvlist_free(newdevs
[i
]);
3961 kmem_free(newdevs
, (oldndevs
+ ndevs
) * sizeof (void *));
3964 * Generate a new dev list.
3966 VERIFY(nvlist_alloc(&sav
->sav_config
, NV_UNIQUE_NAME
,
3968 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
, config
,
3974 * Stop and drop level 2 ARC devices
3977 spa_l2cache_drop(spa_t
*spa
)
3981 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
3983 for (i
= 0; i
< sav
->sav_count
; i
++) {
3986 vd
= sav
->sav_vdevs
[i
];
3989 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
3990 pool
!= 0ULL && l2arc_vdev_present(vd
))
3991 l2arc_remove_vdev(vd
);
3999 spa_create(const char *pool
, nvlist_t
*nvroot
, nvlist_t
*props
,
4003 char *altroot
= NULL
;
4008 uint64_t txg
= TXG_INITIAL
;
4009 nvlist_t
**spares
, **l2cache
;
4010 uint_t nspares
, nl2cache
;
4011 uint64_t version
, obj
;
4012 boolean_t has_features
;
4018 if (nvlist_lookup_string(props
, "tname", &poolname
) != 0)
4019 poolname
= (char *)pool
;
4022 * If this pool already exists, return failure.
4024 mutex_enter(&spa_namespace_lock
);
4025 if (spa_lookup(poolname
) != NULL
) {
4026 mutex_exit(&spa_namespace_lock
);
4027 return (SET_ERROR(EEXIST
));
4031 * Allocate a new spa_t structure.
4033 nvl
= fnvlist_alloc();
4034 fnvlist_add_string(nvl
, ZPOOL_CONFIG_POOL_NAME
, pool
);
4035 (void) nvlist_lookup_string(props
,
4036 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
4037 spa
= spa_add(poolname
, nvl
, altroot
);
4039 spa_activate(spa
, spa_mode_global
);
4041 if (props
&& (error
= spa_prop_validate(spa
, props
))) {
4042 spa_deactivate(spa
);
4044 mutex_exit(&spa_namespace_lock
);
4049 * Temporary pool names should never be written to disk.
4051 if (poolname
!= pool
)
4052 spa
->spa_import_flags
|= ZFS_IMPORT_TEMP_NAME
;
4054 has_features
= B_FALSE
;
4055 for (elem
= nvlist_next_nvpair(props
, NULL
);
4056 elem
!= NULL
; elem
= nvlist_next_nvpair(props
, elem
)) {
4057 if (zpool_prop_feature(nvpair_name(elem
)))
4058 has_features
= B_TRUE
;
4061 if (has_features
|| nvlist_lookup_uint64(props
,
4062 zpool_prop_to_name(ZPOOL_PROP_VERSION
), &version
) != 0) {
4063 version
= SPA_VERSION
;
4065 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
4067 spa
->spa_first_txg
= txg
;
4068 spa
->spa_uberblock
.ub_txg
= txg
- 1;
4069 spa
->spa_uberblock
.ub_version
= version
;
4070 spa
->spa_ubsync
= spa
->spa_uberblock
;
4071 spa
->spa_load_state
= SPA_LOAD_CREATE
;
4074 * Create "The Godfather" zio to hold all async IOs
4076 spa
->spa_async_zio_root
= kmem_alloc(max_ncpus
* sizeof (void *),
4078 for (i
= 0; i
< max_ncpus
; i
++) {
4079 spa
->spa_async_zio_root
[i
] = zio_root(spa
, NULL
, NULL
,
4080 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
4081 ZIO_FLAG_GODFATHER
);
4085 * Create the root vdev.
4087 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4089 error
= spa_config_parse(spa
, &rvd
, nvroot
, NULL
, 0, VDEV_ALLOC_ADD
);
4091 ASSERT(error
!= 0 || rvd
!= NULL
);
4092 ASSERT(error
!= 0 || spa
->spa_root_vdev
== rvd
);
4094 if (error
== 0 && !zfs_allocatable_devs(nvroot
))
4095 error
= SET_ERROR(EINVAL
);
4098 (error
= vdev_create(rvd
, txg
, B_FALSE
)) == 0 &&
4099 (error
= spa_validate_aux(spa
, nvroot
, txg
,
4100 VDEV_ALLOC_ADD
)) == 0) {
4101 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
4102 vdev_metaslab_set_size(rvd
->vdev_child
[c
]);
4103 vdev_expand(rvd
->vdev_child
[c
], txg
);
4107 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4111 spa_deactivate(spa
);
4113 mutex_exit(&spa_namespace_lock
);
4118 * Get the list of spares, if specified.
4120 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
4121 &spares
, &nspares
) == 0) {
4122 VERIFY(nvlist_alloc(&spa
->spa_spares
.sav_config
, NV_UNIQUE_NAME
,
4124 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
4125 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
4126 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4127 spa_load_spares(spa
);
4128 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4129 spa
->spa_spares
.sav_sync
= B_TRUE
;
4133 * Get the list of level 2 cache devices, if specified.
4135 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
4136 &l2cache
, &nl2cache
) == 0) {
4137 VERIFY(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
4138 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
4139 VERIFY(nvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
4140 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
4141 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4142 spa_load_l2cache(spa
);
4143 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4144 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
4147 spa
->spa_is_initializing
= B_TRUE
;
4148 spa
->spa_dsl_pool
= dp
= dsl_pool_create(spa
, zplprops
, txg
);
4149 spa
->spa_meta_objset
= dp
->dp_meta_objset
;
4150 spa
->spa_is_initializing
= B_FALSE
;
4153 * Create DDTs (dedup tables).
4157 spa_update_dspace(spa
);
4159 tx
= dmu_tx_create_assigned(dp
, txg
);
4162 * Create the pool's history object.
4164 if (version
>= SPA_VERSION_ZPOOL_HISTORY
&& !spa
->spa_history
)
4165 spa_history_create_obj(spa
, tx
);
4167 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_CREATE
);
4168 spa_history_log_version(spa
, "create", tx
);
4171 * Create the pool config object.
4173 spa
->spa_config_object
= dmu_object_alloc(spa
->spa_meta_objset
,
4174 DMU_OT_PACKED_NVLIST
, SPA_CONFIG_BLOCKSIZE
,
4175 DMU_OT_PACKED_NVLIST_SIZE
, sizeof (uint64_t), tx
);
4177 if (zap_add(spa
->spa_meta_objset
,
4178 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CONFIG
,
4179 sizeof (uint64_t), 1, &spa
->spa_config_object
, tx
) != 0) {
4180 cmn_err(CE_PANIC
, "failed to add pool config");
4183 if (spa_version(spa
) >= SPA_VERSION_FEATURES
)
4184 spa_feature_create_zap_objects(spa
, tx
);
4186 if (zap_add(spa
->spa_meta_objset
,
4187 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CREATION_VERSION
,
4188 sizeof (uint64_t), 1, &version
, tx
) != 0) {
4189 cmn_err(CE_PANIC
, "failed to add pool version");
4192 /* Newly created pools with the right version are always deflated. */
4193 if (version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
4194 spa
->spa_deflate
= TRUE
;
4195 if (zap_add(spa
->spa_meta_objset
,
4196 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
4197 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
) != 0) {
4198 cmn_err(CE_PANIC
, "failed to add deflate");
4203 * Create the deferred-free bpobj. Turn off compression
4204 * because sync-to-convergence takes longer if the blocksize
4207 obj
= bpobj_alloc(spa
->spa_meta_objset
, 1 << 14, tx
);
4208 dmu_object_set_compress(spa
->spa_meta_objset
, obj
,
4209 ZIO_COMPRESS_OFF
, tx
);
4210 if (zap_add(spa
->spa_meta_objset
,
4211 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_SYNC_BPOBJ
,
4212 sizeof (uint64_t), 1, &obj
, tx
) != 0) {
4213 cmn_err(CE_PANIC
, "failed to add bpobj");
4215 VERIFY3U(0, ==, bpobj_open(&spa
->spa_deferred_bpobj
,
4216 spa
->spa_meta_objset
, obj
));
4219 * Generate some random noise for salted checksums to operate on.
4221 (void) random_get_pseudo_bytes(spa
->spa_cksum_salt
.zcs_bytes
,
4222 sizeof (spa
->spa_cksum_salt
.zcs_bytes
));
4225 * Set pool properties.
4227 spa
->spa_bootfs
= zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS
);
4228 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
4229 spa
->spa_failmode
= zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE
);
4230 spa
->spa_autoexpand
= zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND
);
4231 spa
->spa_multihost
= zpool_prop_default_numeric(ZPOOL_PROP_MULTIHOST
);
4233 if (props
!= NULL
) {
4234 spa_configfile_set(spa
, props
, B_FALSE
);
4235 spa_sync_props(props
, tx
);
4240 spa
->spa_sync_on
= B_TRUE
;
4241 txg_sync_start(spa
->spa_dsl_pool
);
4242 mmp_thread_start(spa
);
4245 * We explicitly wait for the first transaction to complete so that our
4246 * bean counters are appropriately updated.
4248 txg_wait_synced(spa
->spa_dsl_pool
, txg
);
4250 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
4253 * Don't count references from objsets that are already closed
4254 * and are making their way through the eviction process.
4256 spa_evicting_os_wait(spa
);
4257 spa
->spa_minref
= refcount_count(&spa
->spa_refcount
);
4258 spa
->spa_load_state
= SPA_LOAD_NONE
;
4260 mutex_exit(&spa_namespace_lock
);
4266 * Import a non-root pool into the system.
4269 spa_import(char *pool
, nvlist_t
*config
, nvlist_t
*props
, uint64_t flags
)
4272 char *altroot
= NULL
;
4273 spa_load_state_t state
= SPA_LOAD_IMPORT
;
4274 zpool_rewind_policy_t policy
;
4275 uint64_t mode
= spa_mode_global
;
4276 uint64_t readonly
= B_FALSE
;
4279 nvlist_t
**spares
, **l2cache
;
4280 uint_t nspares
, nl2cache
;
4283 * If a pool with this name exists, return failure.
4285 mutex_enter(&spa_namespace_lock
);
4286 if (spa_lookup(pool
) != NULL
) {
4287 mutex_exit(&spa_namespace_lock
);
4288 return (SET_ERROR(EEXIST
));
4292 * Create and initialize the spa structure.
4294 (void) nvlist_lookup_string(props
,
4295 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
4296 (void) nvlist_lookup_uint64(props
,
4297 zpool_prop_to_name(ZPOOL_PROP_READONLY
), &readonly
);
4300 spa
= spa_add(pool
, config
, altroot
);
4301 spa
->spa_import_flags
= flags
;
4304 * Verbatim import - Take a pool and insert it into the namespace
4305 * as if it had been loaded at boot.
4307 if (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
) {
4309 spa_configfile_set(spa
, props
, B_FALSE
);
4311 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
4312 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_IMPORT
);
4314 mutex_exit(&spa_namespace_lock
);
4318 spa_activate(spa
, mode
);
4321 * Don't start async tasks until we know everything is healthy.
4323 spa_async_suspend(spa
);
4325 zpool_get_rewind_policy(config
, &policy
);
4326 if (policy
.zrp_request
& ZPOOL_DO_REWIND
)
4327 state
= SPA_LOAD_RECOVER
;
4330 * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig
4331 * because the user-supplied config is actually the one to trust when
4334 if (state
!= SPA_LOAD_RECOVER
)
4335 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
4337 error
= spa_load_best(spa
, state
, B_TRUE
, policy
.zrp_txg
,
4338 policy
.zrp_request
);
4341 * Propagate anything learned while loading the pool and pass it
4342 * back to caller (i.e. rewind info, missing devices, etc).
4344 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
4345 spa
->spa_load_info
) == 0);
4347 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4349 * Toss any existing sparelist, as it doesn't have any validity
4350 * anymore, and conflicts with spa_has_spare().
4352 if (spa
->spa_spares
.sav_config
) {
4353 nvlist_free(spa
->spa_spares
.sav_config
);
4354 spa
->spa_spares
.sav_config
= NULL
;
4355 spa_load_spares(spa
);
4357 if (spa
->spa_l2cache
.sav_config
) {
4358 nvlist_free(spa
->spa_l2cache
.sav_config
);
4359 spa
->spa_l2cache
.sav_config
= NULL
;
4360 spa_load_l2cache(spa
);
4363 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
4365 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4368 spa_configfile_set(spa
, props
, B_FALSE
);
4370 if (error
!= 0 || (props
&& spa_writeable(spa
) &&
4371 (error
= spa_prop_set(spa
, props
)))) {
4373 spa_deactivate(spa
);
4375 mutex_exit(&spa_namespace_lock
);
4379 spa_async_resume(spa
);
4382 * Override any spares and level 2 cache devices as specified by
4383 * the user, as these may have correct device names/devids, etc.
4385 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
4386 &spares
, &nspares
) == 0) {
4387 if (spa
->spa_spares
.sav_config
)
4388 VERIFY(nvlist_remove(spa
->spa_spares
.sav_config
,
4389 ZPOOL_CONFIG_SPARES
, DATA_TYPE_NVLIST_ARRAY
) == 0);
4391 VERIFY(nvlist_alloc(&spa
->spa_spares
.sav_config
,
4392 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
4393 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
4394 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
4395 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4396 spa_load_spares(spa
);
4397 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4398 spa
->spa_spares
.sav_sync
= B_TRUE
;
4400 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
4401 &l2cache
, &nl2cache
) == 0) {
4402 if (spa
->spa_l2cache
.sav_config
)
4403 VERIFY(nvlist_remove(spa
->spa_l2cache
.sav_config
,
4404 ZPOOL_CONFIG_L2CACHE
, DATA_TYPE_NVLIST_ARRAY
) == 0);
4406 VERIFY(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
4407 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
4408 VERIFY(nvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
4409 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
4410 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4411 spa_load_l2cache(spa
);
4412 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4413 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
4417 * Check for any removed devices.
4419 if (spa
->spa_autoreplace
) {
4420 spa_aux_check_removed(&spa
->spa_spares
);
4421 spa_aux_check_removed(&spa
->spa_l2cache
);
4424 if (spa_writeable(spa
)) {
4426 * Update the config cache to include the newly-imported pool.
4428 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
4432 * It's possible that the pool was expanded while it was exported.
4433 * We kick off an async task to handle this for us.
4435 spa_async_request(spa
, SPA_ASYNC_AUTOEXPAND
);
4437 spa_history_log_version(spa
, "import", NULL
);
4439 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_IMPORT
);
4441 zvol_create_minors(spa
, pool
, B_TRUE
);
4443 mutex_exit(&spa_namespace_lock
);
4449 spa_tryimport(nvlist_t
*tryconfig
)
4451 nvlist_t
*config
= NULL
;
4457 if (nvlist_lookup_string(tryconfig
, ZPOOL_CONFIG_POOL_NAME
, &poolname
))
4460 if (nvlist_lookup_uint64(tryconfig
, ZPOOL_CONFIG_POOL_STATE
, &state
))
4464 * Create and initialize the spa structure.
4466 mutex_enter(&spa_namespace_lock
);
4467 spa
= spa_add(TRYIMPORT_NAME
, tryconfig
, NULL
);
4468 spa_activate(spa
, FREAD
);
4471 * Pass off the heavy lifting to spa_load().
4472 * Pass TRUE for mosconfig because the user-supplied config
4473 * is actually the one to trust when doing an import.
4475 error
= spa_load(spa
, SPA_LOAD_TRYIMPORT
, SPA_IMPORT_EXISTING
, B_TRUE
);
4478 * If 'tryconfig' was at least parsable, return the current config.
4480 if (spa
->spa_root_vdev
!= NULL
) {
4481 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
4482 VERIFY(nvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
,
4484 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
4486 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_TIMESTAMP
,
4487 spa
->spa_uberblock
.ub_timestamp
) == 0);
4488 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
4489 spa
->spa_load_info
) == 0);
4490 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_ERRATA
,
4491 spa
->spa_errata
) == 0);
4494 * If the bootfs property exists on this pool then we
4495 * copy it out so that external consumers can tell which
4496 * pools are bootable.
4498 if ((!error
|| error
== EEXIST
) && spa
->spa_bootfs
) {
4499 char *tmpname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
4502 * We have to play games with the name since the
4503 * pool was opened as TRYIMPORT_NAME.
4505 if (dsl_dsobj_to_dsname(spa_name(spa
),
4506 spa
->spa_bootfs
, tmpname
) == 0) {
4510 dsname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
4512 cp
= strchr(tmpname
, '/');
4514 (void) strlcpy(dsname
, tmpname
,
4517 (void) snprintf(dsname
, MAXPATHLEN
,
4518 "%s/%s", poolname
, ++cp
);
4520 VERIFY(nvlist_add_string(config
,
4521 ZPOOL_CONFIG_BOOTFS
, dsname
) == 0);
4522 kmem_free(dsname
, MAXPATHLEN
);
4524 kmem_free(tmpname
, MAXPATHLEN
);
4528 * Add the list of hot spares and level 2 cache devices.
4530 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
4531 spa_add_spares(spa
, config
);
4532 spa_add_l2cache(spa
, config
);
4533 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
4537 spa_deactivate(spa
);
4539 mutex_exit(&spa_namespace_lock
);
4545 * Pool export/destroy
4547 * The act of destroying or exporting a pool is very simple. We make sure there
4548 * is no more pending I/O and any references to the pool are gone. Then, we
4549 * update the pool state and sync all the labels to disk, removing the
4550 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
4551 * we don't sync the labels or remove the configuration cache.
4554 spa_export_common(char *pool
, int new_state
, nvlist_t
**oldconfig
,
4555 boolean_t force
, boolean_t hardforce
)
4562 if (!(spa_mode_global
& FWRITE
))
4563 return (SET_ERROR(EROFS
));
4565 mutex_enter(&spa_namespace_lock
);
4566 if ((spa
= spa_lookup(pool
)) == NULL
) {
4567 mutex_exit(&spa_namespace_lock
);
4568 return (SET_ERROR(ENOENT
));
4572 * Put a hold on the pool, drop the namespace lock, stop async tasks,
4573 * reacquire the namespace lock, and see if we can export.
4575 spa_open_ref(spa
, FTAG
);
4576 mutex_exit(&spa_namespace_lock
);
4577 spa_async_suspend(spa
);
4578 if (spa
->spa_zvol_taskq
) {
4579 zvol_remove_minors(spa
, spa_name(spa
), B_TRUE
);
4580 taskq_wait(spa
->spa_zvol_taskq
);
4582 mutex_enter(&spa_namespace_lock
);
4583 spa_close(spa
, FTAG
);
4585 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
)
4588 * The pool will be in core if it's openable, in which case we can
4589 * modify its state. Objsets may be open only because they're dirty,
4590 * so we have to force it to sync before checking spa_refcnt.
4592 if (spa
->spa_sync_on
) {
4593 txg_wait_synced(spa
->spa_dsl_pool
, 0);
4594 spa_evicting_os_wait(spa
);
4598 * A pool cannot be exported or destroyed if there are active
4599 * references. If we are resetting a pool, allow references by
4600 * fault injection handlers.
4602 if (!spa_refcount_zero(spa
) ||
4603 (spa
->spa_inject_ref
!= 0 &&
4604 new_state
!= POOL_STATE_UNINITIALIZED
)) {
4605 spa_async_resume(spa
);
4606 mutex_exit(&spa_namespace_lock
);
4607 return (SET_ERROR(EBUSY
));
4610 if (spa
->spa_sync_on
) {
4612 * A pool cannot be exported if it has an active shared spare.
4613 * This is to prevent other pools stealing the active spare
4614 * from an exported pool. At user's own will, such pool can
4615 * be forcedly exported.
4617 if (!force
&& new_state
== POOL_STATE_EXPORTED
&&
4618 spa_has_active_shared_spare(spa
)) {
4619 spa_async_resume(spa
);
4620 mutex_exit(&spa_namespace_lock
);
4621 return (SET_ERROR(EXDEV
));
4625 * We want this to be reflected on every label,
4626 * so mark them all dirty. spa_unload() will do the
4627 * final sync that pushes these changes out.
4629 if (new_state
!= POOL_STATE_UNINITIALIZED
&& !hardforce
) {
4630 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4631 spa
->spa_state
= new_state
;
4632 spa
->spa_final_txg
= spa_last_synced_txg(spa
) +
4634 vdev_config_dirty(spa
->spa_root_vdev
);
4635 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4640 if (new_state
== POOL_STATE_DESTROYED
)
4641 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_DESTROY
);
4642 else if (new_state
== POOL_STATE_EXPORTED
)
4643 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_EXPORT
);
4645 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
4647 spa_deactivate(spa
);
4650 if (oldconfig
&& spa
->spa_config
)
4651 VERIFY(nvlist_dup(spa
->spa_config
, oldconfig
, 0) == 0);
4653 if (new_state
!= POOL_STATE_UNINITIALIZED
) {
4655 spa_config_sync(spa
, B_TRUE
, B_TRUE
);
4658 mutex_exit(&spa_namespace_lock
);
4664 * Destroy a storage pool.
4667 spa_destroy(char *pool
)
4669 return (spa_export_common(pool
, POOL_STATE_DESTROYED
, NULL
,
4674 * Export a storage pool.
4677 spa_export(char *pool
, nvlist_t
**oldconfig
, boolean_t force
,
4678 boolean_t hardforce
)
4680 return (spa_export_common(pool
, POOL_STATE_EXPORTED
, oldconfig
,
4685 * Similar to spa_export(), this unloads the spa_t without actually removing it
4686 * from the namespace in any way.
4689 spa_reset(char *pool
)
4691 return (spa_export_common(pool
, POOL_STATE_UNINITIALIZED
, NULL
,
4696 * ==========================================================================
4697 * Device manipulation
4698 * ==========================================================================
4702 * Add a device to a storage pool.
4705 spa_vdev_add(spa_t
*spa
, nvlist_t
*nvroot
)
4709 vdev_t
*rvd
= spa
->spa_root_vdev
;
4711 nvlist_t
**spares
, **l2cache
;
4712 uint_t nspares
, nl2cache
;
4715 ASSERT(spa_writeable(spa
));
4717 txg
= spa_vdev_enter(spa
);
4719 if ((error
= spa_config_parse(spa
, &vd
, nvroot
, NULL
, 0,
4720 VDEV_ALLOC_ADD
)) != 0)
4721 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
4723 spa
->spa_pending_vdev
= vd
; /* spa_vdev_exit() will clear this */
4725 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
, &spares
,
4729 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
, &l2cache
,
4733 if (vd
->vdev_children
== 0 && nspares
== 0 && nl2cache
== 0)
4734 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
4736 if (vd
->vdev_children
!= 0 &&
4737 (error
= vdev_create(vd
, txg
, B_FALSE
)) != 0)
4738 return (spa_vdev_exit(spa
, vd
, txg
, error
));
4741 * We must validate the spares and l2cache devices after checking the
4742 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
4744 if ((error
= spa_validate_aux(spa
, nvroot
, txg
, VDEV_ALLOC_ADD
)) != 0)
4745 return (spa_vdev_exit(spa
, vd
, txg
, error
));
4748 * Transfer each new top-level vdev from vd to rvd.
4750 for (c
= 0; c
< vd
->vdev_children
; c
++) {
4753 * Set the vdev id to the first hole, if one exists.
4755 for (id
= 0; id
< rvd
->vdev_children
; id
++) {
4756 if (rvd
->vdev_child
[id
]->vdev_ishole
) {
4757 vdev_free(rvd
->vdev_child
[id
]);
4761 tvd
= vd
->vdev_child
[c
];
4762 vdev_remove_child(vd
, tvd
);
4764 vdev_add_child(rvd
, tvd
);
4765 vdev_config_dirty(tvd
);
4769 spa_set_aux_vdevs(&spa
->spa_spares
, spares
, nspares
,
4770 ZPOOL_CONFIG_SPARES
);
4771 spa_load_spares(spa
);
4772 spa
->spa_spares
.sav_sync
= B_TRUE
;
4775 if (nl2cache
!= 0) {
4776 spa_set_aux_vdevs(&spa
->spa_l2cache
, l2cache
, nl2cache
,
4777 ZPOOL_CONFIG_L2CACHE
);
4778 spa_load_l2cache(spa
);
4779 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
4783 * We have to be careful when adding new vdevs to an existing pool.
4784 * If other threads start allocating from these vdevs before we
4785 * sync the config cache, and we lose power, then upon reboot we may
4786 * fail to open the pool because there are DVAs that the config cache
4787 * can't translate. Therefore, we first add the vdevs without
4788 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
4789 * and then let spa_config_update() initialize the new metaslabs.
4791 * spa_load() checks for added-but-not-initialized vdevs, so that
4792 * if we lose power at any point in this sequence, the remaining
4793 * steps will be completed the next time we load the pool.
4795 (void) spa_vdev_exit(spa
, vd
, txg
, 0);
4797 mutex_enter(&spa_namespace_lock
);
4798 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
4799 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_VDEV_ADD
);
4800 mutex_exit(&spa_namespace_lock
);
4806 * Attach a device to a mirror. The arguments are the path to any device
4807 * in the mirror, and the nvroot for the new device. If the path specifies
4808 * a device that is not mirrored, we automatically insert the mirror vdev.
4810 * If 'replacing' is specified, the new device is intended to replace the
4811 * existing device; in this case the two devices are made into their own
4812 * mirror using the 'replacing' vdev, which is functionally identical to
4813 * the mirror vdev (it actually reuses all the same ops) but has a few
4814 * extra rules: you can't attach to it after it's been created, and upon
4815 * completion of resilvering, the first disk (the one being replaced)
4816 * is automatically detached.
4819 spa_vdev_attach(spa_t
*spa
, uint64_t guid
, nvlist_t
*nvroot
, int replacing
)
4821 uint64_t txg
, dtl_max_txg
;
4822 vdev_t
*oldvd
, *newvd
, *newrootvd
, *pvd
, *tvd
;
4824 char *oldvdpath
, *newvdpath
;
4827 ASSERTV(vdev_t
*rvd
= spa
->spa_root_vdev
);
4829 ASSERT(spa_writeable(spa
));
4831 txg
= spa_vdev_enter(spa
);
4833 oldvd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
4836 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
4838 if (!oldvd
->vdev_ops
->vdev_op_leaf
)
4839 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4841 pvd
= oldvd
->vdev_parent
;
4843 if ((error
= spa_config_parse(spa
, &newrootvd
, nvroot
, NULL
, 0,
4844 VDEV_ALLOC_ATTACH
)) != 0)
4845 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
4847 if (newrootvd
->vdev_children
!= 1)
4848 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
4850 newvd
= newrootvd
->vdev_child
[0];
4852 if (!newvd
->vdev_ops
->vdev_op_leaf
)
4853 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
4855 if ((error
= vdev_create(newrootvd
, txg
, replacing
)) != 0)
4856 return (spa_vdev_exit(spa
, newrootvd
, txg
, error
));
4859 * Spares can't replace logs
4861 if (oldvd
->vdev_top
->vdev_islog
&& newvd
->vdev_isspare
)
4862 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4866 * For attach, the only allowable parent is a mirror or the root
4869 if (pvd
->vdev_ops
!= &vdev_mirror_ops
&&
4870 pvd
->vdev_ops
!= &vdev_root_ops
)
4871 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4873 pvops
= &vdev_mirror_ops
;
4876 * Active hot spares can only be replaced by inactive hot
4879 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
4880 oldvd
->vdev_isspare
&&
4881 !spa_has_spare(spa
, newvd
->vdev_guid
))
4882 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4885 * If the source is a hot spare, and the parent isn't already a
4886 * spare, then we want to create a new hot spare. Otherwise, we
4887 * want to create a replacing vdev. The user is not allowed to
4888 * attach to a spared vdev child unless the 'isspare' state is
4889 * the same (spare replaces spare, non-spare replaces
4892 if (pvd
->vdev_ops
== &vdev_replacing_ops
&&
4893 spa_version(spa
) < SPA_VERSION_MULTI_REPLACE
) {
4894 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4895 } else if (pvd
->vdev_ops
== &vdev_spare_ops
&&
4896 newvd
->vdev_isspare
!= oldvd
->vdev_isspare
) {
4897 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4900 if (newvd
->vdev_isspare
)
4901 pvops
= &vdev_spare_ops
;
4903 pvops
= &vdev_replacing_ops
;
4907 * Make sure the new device is big enough.
4909 if (newvd
->vdev_asize
< vdev_get_min_asize(oldvd
))
4910 return (spa_vdev_exit(spa
, newrootvd
, txg
, EOVERFLOW
));
4913 * The new device cannot have a higher alignment requirement
4914 * than the top-level vdev.
4916 if (newvd
->vdev_ashift
> oldvd
->vdev_top
->vdev_ashift
)
4917 return (spa_vdev_exit(spa
, newrootvd
, txg
, EDOM
));
4920 * If this is an in-place replacement, update oldvd's path and devid
4921 * to make it distinguishable from newvd, and unopenable from now on.
4923 if (strcmp(oldvd
->vdev_path
, newvd
->vdev_path
) == 0) {
4924 spa_strfree(oldvd
->vdev_path
);
4925 oldvd
->vdev_path
= kmem_alloc(strlen(newvd
->vdev_path
) + 5,
4927 (void) sprintf(oldvd
->vdev_path
, "%s/%s",
4928 newvd
->vdev_path
, "old");
4929 if (oldvd
->vdev_devid
!= NULL
) {
4930 spa_strfree(oldvd
->vdev_devid
);
4931 oldvd
->vdev_devid
= NULL
;
4935 /* mark the device being resilvered */
4936 newvd
->vdev_resilver_txg
= txg
;
4939 * If the parent is not a mirror, or if we're replacing, insert the new
4940 * mirror/replacing/spare vdev above oldvd.
4942 if (pvd
->vdev_ops
!= pvops
)
4943 pvd
= vdev_add_parent(oldvd
, pvops
);
4945 ASSERT(pvd
->vdev_top
->vdev_parent
== rvd
);
4946 ASSERT(pvd
->vdev_ops
== pvops
);
4947 ASSERT(oldvd
->vdev_parent
== pvd
);
4950 * Extract the new device from its root and add it to pvd.
4952 vdev_remove_child(newrootvd
, newvd
);
4953 newvd
->vdev_id
= pvd
->vdev_children
;
4954 newvd
->vdev_crtxg
= oldvd
->vdev_crtxg
;
4955 vdev_add_child(pvd
, newvd
);
4958 * Reevaluate the parent vdev state.
4960 vdev_propagate_state(pvd
);
4962 tvd
= newvd
->vdev_top
;
4963 ASSERT(pvd
->vdev_top
== tvd
);
4964 ASSERT(tvd
->vdev_parent
== rvd
);
4966 vdev_config_dirty(tvd
);
4969 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
4970 * for any dmu_sync-ed blocks. It will propagate upward when
4971 * spa_vdev_exit() calls vdev_dtl_reassess().
4973 dtl_max_txg
= txg
+ TXG_CONCURRENT_STATES
;
4975 vdev_dtl_dirty(newvd
, DTL_MISSING
, TXG_INITIAL
,
4976 dtl_max_txg
- TXG_INITIAL
);
4978 if (newvd
->vdev_isspare
) {
4979 spa_spare_activate(newvd
);
4980 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_VDEV_SPARE
);
4983 oldvdpath
= spa_strdup(oldvd
->vdev_path
);
4984 newvdpath
= spa_strdup(newvd
->vdev_path
);
4985 newvd_isspare
= newvd
->vdev_isspare
;
4988 * Mark newvd's DTL dirty in this txg.
4990 vdev_dirty(tvd
, VDD_DTL
, newvd
, txg
);
4993 * Schedule the resilver to restart in the future. We do this to
4994 * ensure that dmu_sync-ed blocks have been stitched into the
4995 * respective datasets.
4997 dsl_resilver_restart(spa
->spa_dsl_pool
, dtl_max_txg
);
4999 if (spa
->spa_bootfs
)
5000 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_BOOTFS_VDEV_ATTACH
);
5002 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_VDEV_ATTACH
);
5007 (void) spa_vdev_exit(spa
, newrootvd
, dtl_max_txg
, 0);
5009 spa_history_log_internal(spa
, "vdev attach", NULL
,
5010 "%s vdev=%s %s vdev=%s",
5011 replacing
&& newvd_isspare
? "spare in" :
5012 replacing
? "replace" : "attach", newvdpath
,
5013 replacing
? "for" : "to", oldvdpath
);
5015 spa_strfree(oldvdpath
);
5016 spa_strfree(newvdpath
);
5022 * Detach a device from a mirror or replacing vdev.
5024 * If 'replace_done' is specified, only detach if the parent
5025 * is a replacing vdev.
5028 spa_vdev_detach(spa_t
*spa
, uint64_t guid
, uint64_t pguid
, int replace_done
)
5032 vdev_t
*vd
, *pvd
, *cvd
, *tvd
;
5033 boolean_t unspare
= B_FALSE
;
5034 uint64_t unspare_guid
= 0;
5037 ASSERTV(vdev_t
*rvd
= spa
->spa_root_vdev
);
5038 ASSERT(spa_writeable(spa
));
5040 txg
= spa_vdev_enter(spa
);
5042 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
5045 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
5047 if (!vd
->vdev_ops
->vdev_op_leaf
)
5048 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
5050 pvd
= vd
->vdev_parent
;
5053 * If the parent/child relationship is not as expected, don't do it.
5054 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
5055 * vdev that's replacing B with C. The user's intent in replacing
5056 * is to go from M(A,B) to M(A,C). If the user decides to cancel
5057 * the replace by detaching C, the expected behavior is to end up
5058 * M(A,B). But suppose that right after deciding to detach C,
5059 * the replacement of B completes. We would have M(A,C), and then
5060 * ask to detach C, which would leave us with just A -- not what
5061 * the user wanted. To prevent this, we make sure that the
5062 * parent/child relationship hasn't changed -- in this example,
5063 * that C's parent is still the replacing vdev R.
5065 if (pvd
->vdev_guid
!= pguid
&& pguid
!= 0)
5066 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
5069 * Only 'replacing' or 'spare' vdevs can be replaced.
5071 if (replace_done
&& pvd
->vdev_ops
!= &vdev_replacing_ops
&&
5072 pvd
->vdev_ops
!= &vdev_spare_ops
)
5073 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
5075 ASSERT(pvd
->vdev_ops
!= &vdev_spare_ops
||
5076 spa_version(spa
) >= SPA_VERSION_SPARES
);
5079 * Only mirror, replacing, and spare vdevs support detach.
5081 if (pvd
->vdev_ops
!= &vdev_replacing_ops
&&
5082 pvd
->vdev_ops
!= &vdev_mirror_ops
&&
5083 pvd
->vdev_ops
!= &vdev_spare_ops
)
5084 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
5087 * If this device has the only valid copy of some data,
5088 * we cannot safely detach it.
5090 if (vdev_dtl_required(vd
))
5091 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
5093 ASSERT(pvd
->vdev_children
>= 2);
5096 * If we are detaching the second disk from a replacing vdev, then
5097 * check to see if we changed the original vdev's path to have "/old"
5098 * at the end in spa_vdev_attach(). If so, undo that change now.
5100 if (pvd
->vdev_ops
== &vdev_replacing_ops
&& vd
->vdev_id
> 0 &&
5101 vd
->vdev_path
!= NULL
) {
5102 size_t len
= strlen(vd
->vdev_path
);
5104 for (c
= 0; c
< pvd
->vdev_children
; c
++) {
5105 cvd
= pvd
->vdev_child
[c
];
5107 if (cvd
== vd
|| cvd
->vdev_path
== NULL
)
5110 if (strncmp(cvd
->vdev_path
, vd
->vdev_path
, len
) == 0 &&
5111 strcmp(cvd
->vdev_path
+ len
, "/old") == 0) {
5112 spa_strfree(cvd
->vdev_path
);
5113 cvd
->vdev_path
= spa_strdup(vd
->vdev_path
);
5120 * If we are detaching the original disk from a spare, then it implies
5121 * that the spare should become a real disk, and be removed from the
5122 * active spare list for the pool.
5124 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
5126 pvd
->vdev_child
[pvd
->vdev_children
- 1]->vdev_isspare
)
5130 * Erase the disk labels so the disk can be used for other things.
5131 * This must be done after all other error cases are handled,
5132 * but before we disembowel vd (so we can still do I/O to it).
5133 * But if we can't do it, don't treat the error as fatal --
5134 * it may be that the unwritability of the disk is the reason
5135 * it's being detached!
5137 error
= vdev_label_init(vd
, 0, VDEV_LABEL_REMOVE
);
5140 * Remove vd from its parent and compact the parent's children.
5142 vdev_remove_child(pvd
, vd
);
5143 vdev_compact_children(pvd
);
5146 * Remember one of the remaining children so we can get tvd below.
5148 cvd
= pvd
->vdev_child
[pvd
->vdev_children
- 1];
5151 * If we need to remove the remaining child from the list of hot spares,
5152 * do it now, marking the vdev as no longer a spare in the process.
5153 * We must do this before vdev_remove_parent(), because that can
5154 * change the GUID if it creates a new toplevel GUID. For a similar
5155 * reason, we must remove the spare now, in the same txg as the detach;
5156 * otherwise someone could attach a new sibling, change the GUID, and
5157 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
5160 ASSERT(cvd
->vdev_isspare
);
5161 spa_spare_remove(cvd
);
5162 unspare_guid
= cvd
->vdev_guid
;
5163 (void) spa_vdev_remove(spa
, unspare_guid
, B_TRUE
);
5164 cvd
->vdev_unspare
= B_TRUE
;
5168 * If the parent mirror/replacing vdev only has one child,
5169 * the parent is no longer needed. Remove it from the tree.
5171 if (pvd
->vdev_children
== 1) {
5172 if (pvd
->vdev_ops
== &vdev_spare_ops
)
5173 cvd
->vdev_unspare
= B_FALSE
;
5174 vdev_remove_parent(cvd
);
5179 * We don't set tvd until now because the parent we just removed
5180 * may have been the previous top-level vdev.
5182 tvd
= cvd
->vdev_top
;
5183 ASSERT(tvd
->vdev_parent
== rvd
);
5186 * Reevaluate the parent vdev state.
5188 vdev_propagate_state(cvd
);
5191 * If the 'autoexpand' property is set on the pool then automatically
5192 * try to expand the size of the pool. For example if the device we
5193 * just detached was smaller than the others, it may be possible to
5194 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
5195 * first so that we can obtain the updated sizes of the leaf vdevs.
5197 if (spa
->spa_autoexpand
) {
5199 vdev_expand(tvd
, txg
);
5202 vdev_config_dirty(tvd
);
5205 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
5206 * vd->vdev_detached is set and free vd's DTL object in syncing context.
5207 * But first make sure we're not on any *other* txg's DTL list, to
5208 * prevent vd from being accessed after it's freed.
5210 vdpath
= spa_strdup(vd
->vdev_path
? vd
->vdev_path
: "none");
5211 for (t
= 0; t
< TXG_SIZE
; t
++)
5212 (void) txg_list_remove_this(&tvd
->vdev_dtl_list
, vd
, t
);
5213 vd
->vdev_detached
= B_TRUE
;
5214 vdev_dirty(tvd
, VDD_DTL
, vd
, txg
);
5216 spa_event_notify(spa
, vd
, NULL
, ESC_ZFS_VDEV_REMOVE
);
5218 /* hang on to the spa before we release the lock */
5219 spa_open_ref(spa
, FTAG
);
5221 error
= spa_vdev_exit(spa
, vd
, txg
, 0);
5223 spa_history_log_internal(spa
, "detach", NULL
,
5225 spa_strfree(vdpath
);
5228 * If this was the removal of the original device in a hot spare vdev,
5229 * then we want to go through and remove the device from the hot spare
5230 * list of every other pool.
5233 spa_t
*altspa
= NULL
;
5235 mutex_enter(&spa_namespace_lock
);
5236 while ((altspa
= spa_next(altspa
)) != NULL
) {
5237 if (altspa
->spa_state
!= POOL_STATE_ACTIVE
||
5241 spa_open_ref(altspa
, FTAG
);
5242 mutex_exit(&spa_namespace_lock
);
5243 (void) spa_vdev_remove(altspa
, unspare_guid
, B_TRUE
);
5244 mutex_enter(&spa_namespace_lock
);
5245 spa_close(altspa
, FTAG
);
5247 mutex_exit(&spa_namespace_lock
);
5249 /* search the rest of the vdevs for spares to remove */
5250 spa_vdev_resilver_done(spa
);
5253 /* all done with the spa; OK to release */
5254 mutex_enter(&spa_namespace_lock
);
5255 spa_close(spa
, FTAG
);
5256 mutex_exit(&spa_namespace_lock
);
5262 * Split a set of devices from their mirrors, and create a new pool from them.
5265 spa_vdev_split_mirror(spa_t
*spa
, char *newname
, nvlist_t
*config
,
5266 nvlist_t
*props
, boolean_t exp
)
5269 uint64_t txg
, *glist
;
5271 uint_t c
, children
, lastlog
;
5272 nvlist_t
**child
, *nvl
, *tmp
;
5274 char *altroot
= NULL
;
5275 vdev_t
*rvd
, **vml
= NULL
; /* vdev modify list */
5276 boolean_t activate_slog
;
5278 ASSERT(spa_writeable(spa
));
5280 txg
= spa_vdev_enter(spa
);
5282 /* clear the log and flush everything up to now */
5283 activate_slog
= spa_passivate_log(spa
);
5284 (void) spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
5285 error
= spa_offline_log(spa
);
5286 txg
= spa_vdev_config_enter(spa
);
5289 spa_activate_log(spa
);
5292 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5294 /* check new spa name before going any further */
5295 if (spa_lookup(newname
) != NULL
)
5296 return (spa_vdev_exit(spa
, NULL
, txg
, EEXIST
));
5299 * scan through all the children to ensure they're all mirrors
5301 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvl
) != 0 ||
5302 nvlist_lookup_nvlist_array(nvl
, ZPOOL_CONFIG_CHILDREN
, &child
,
5304 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
5306 /* first, check to ensure we've got the right child count */
5307 rvd
= spa
->spa_root_vdev
;
5309 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
5310 vdev_t
*vd
= rvd
->vdev_child
[c
];
5312 /* don't count the holes & logs as children */
5313 if (vd
->vdev_islog
|| vd
->vdev_ishole
) {
5321 if (children
!= (lastlog
!= 0 ? lastlog
: rvd
->vdev_children
))
5322 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
5324 /* next, ensure no spare or cache devices are part of the split */
5325 if (nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_SPARES
, &tmp
) == 0 ||
5326 nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_L2CACHE
, &tmp
) == 0)
5327 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
5329 vml
= kmem_zalloc(children
* sizeof (vdev_t
*), KM_SLEEP
);
5330 glist
= kmem_zalloc(children
* sizeof (uint64_t), KM_SLEEP
);
5332 /* then, loop over each vdev and validate it */
5333 for (c
= 0; c
< children
; c
++) {
5334 uint64_t is_hole
= 0;
5336 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
5340 if (spa
->spa_root_vdev
->vdev_child
[c
]->vdev_ishole
||
5341 spa
->spa_root_vdev
->vdev_child
[c
]->vdev_islog
) {
5344 error
= SET_ERROR(EINVAL
);
5349 /* which disk is going to be split? */
5350 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_GUID
,
5352 error
= SET_ERROR(EINVAL
);
5356 /* look it up in the spa */
5357 vml
[c
] = spa_lookup_by_guid(spa
, glist
[c
], B_FALSE
);
5358 if (vml
[c
] == NULL
) {
5359 error
= SET_ERROR(ENODEV
);
5363 /* make sure there's nothing stopping the split */
5364 if (vml
[c
]->vdev_parent
->vdev_ops
!= &vdev_mirror_ops
||
5365 vml
[c
]->vdev_islog
||
5366 vml
[c
]->vdev_ishole
||
5367 vml
[c
]->vdev_isspare
||
5368 vml
[c
]->vdev_isl2cache
||
5369 !vdev_writeable(vml
[c
]) ||
5370 vml
[c
]->vdev_children
!= 0 ||
5371 vml
[c
]->vdev_state
!= VDEV_STATE_HEALTHY
||
5372 c
!= spa
->spa_root_vdev
->vdev_child
[c
]->vdev_id
) {
5373 error
= SET_ERROR(EINVAL
);
5377 if (vdev_dtl_required(vml
[c
])) {
5378 error
= SET_ERROR(EBUSY
);
5382 /* we need certain info from the top level */
5383 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_ARRAY
,
5384 vml
[c
]->vdev_top
->vdev_ms_array
) == 0);
5385 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_SHIFT
,
5386 vml
[c
]->vdev_top
->vdev_ms_shift
) == 0);
5387 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASIZE
,
5388 vml
[c
]->vdev_top
->vdev_asize
) == 0);
5389 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASHIFT
,
5390 vml
[c
]->vdev_top
->vdev_ashift
) == 0);
5392 /* transfer per-vdev ZAPs */
5393 ASSERT3U(vml
[c
]->vdev_leaf_zap
, !=, 0);
5394 VERIFY0(nvlist_add_uint64(child
[c
],
5395 ZPOOL_CONFIG_VDEV_LEAF_ZAP
, vml
[c
]->vdev_leaf_zap
));
5397 ASSERT3U(vml
[c
]->vdev_top
->vdev_top_zap
, !=, 0);
5398 VERIFY0(nvlist_add_uint64(child
[c
],
5399 ZPOOL_CONFIG_VDEV_TOP_ZAP
,
5400 vml
[c
]->vdev_parent
->vdev_top_zap
));
5404 kmem_free(vml
, children
* sizeof (vdev_t
*));
5405 kmem_free(glist
, children
* sizeof (uint64_t));
5406 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5409 /* stop writers from using the disks */
5410 for (c
= 0; c
< children
; c
++) {
5412 vml
[c
]->vdev_offline
= B_TRUE
;
5414 vdev_reopen(spa
->spa_root_vdev
);
5417 * Temporarily record the splitting vdevs in the spa config. This
5418 * will disappear once the config is regenerated.
5420 VERIFY(nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
5421 VERIFY(nvlist_add_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
5422 glist
, children
) == 0);
5423 kmem_free(glist
, children
* sizeof (uint64_t));
5425 mutex_enter(&spa
->spa_props_lock
);
5426 VERIFY(nvlist_add_nvlist(spa
->spa_config
, ZPOOL_CONFIG_SPLIT
,
5428 mutex_exit(&spa
->spa_props_lock
);
5429 spa
->spa_config_splitting
= nvl
;
5430 vdev_config_dirty(spa
->spa_root_vdev
);
5432 /* configure and create the new pool */
5433 VERIFY(nvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
, newname
) == 0);
5434 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
5435 exp
? POOL_STATE_EXPORTED
: POOL_STATE_ACTIVE
) == 0);
5436 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
5437 spa_version(spa
)) == 0);
5438 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
5439 spa
->spa_config_txg
) == 0);
5440 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
5441 spa_generate_guid(NULL
)) == 0);
5442 VERIFY0(nvlist_add_boolean(config
, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
));
5443 (void) nvlist_lookup_string(props
,
5444 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
5446 /* add the new pool to the namespace */
5447 newspa
= spa_add(newname
, config
, altroot
);
5448 newspa
->spa_avz_action
= AVZ_ACTION_REBUILD
;
5449 newspa
->spa_config_txg
= spa
->spa_config_txg
;
5450 spa_set_log_state(newspa
, SPA_LOG_CLEAR
);
5452 /* release the spa config lock, retaining the namespace lock */
5453 spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
5455 if (zio_injection_enabled
)
5456 zio_handle_panic_injection(spa
, FTAG
, 1);
5458 spa_activate(newspa
, spa_mode_global
);
5459 spa_async_suspend(newspa
);
5461 /* create the new pool from the disks of the original pool */
5462 error
= spa_load(newspa
, SPA_LOAD_IMPORT
, SPA_IMPORT_ASSEMBLE
, B_TRUE
);
5466 /* if that worked, generate a real config for the new pool */
5467 if (newspa
->spa_root_vdev
!= NULL
) {
5468 VERIFY(nvlist_alloc(&newspa
->spa_config_splitting
,
5469 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
5470 VERIFY(nvlist_add_uint64(newspa
->spa_config_splitting
,
5471 ZPOOL_CONFIG_SPLIT_GUID
, spa_guid(spa
)) == 0);
5472 spa_config_set(newspa
, spa_config_generate(newspa
, NULL
, -1ULL,
5477 if (props
!= NULL
) {
5478 spa_configfile_set(newspa
, props
, B_FALSE
);
5479 error
= spa_prop_set(newspa
, props
);
5484 /* flush everything */
5485 txg
= spa_vdev_config_enter(newspa
);
5486 vdev_config_dirty(newspa
->spa_root_vdev
);
5487 (void) spa_vdev_config_exit(newspa
, NULL
, txg
, 0, FTAG
);
5489 if (zio_injection_enabled
)
5490 zio_handle_panic_injection(spa
, FTAG
, 2);
5492 spa_async_resume(newspa
);
5494 /* finally, update the original pool's config */
5495 txg
= spa_vdev_config_enter(spa
);
5496 tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
5497 error
= dmu_tx_assign(tx
, TXG_WAIT
);
5500 for (c
= 0; c
< children
; c
++) {
5501 if (vml
[c
] != NULL
) {
5504 spa_history_log_internal(spa
, "detach", tx
,
5505 "vdev=%s", vml
[c
]->vdev_path
);
5510 spa
->spa_avz_action
= AVZ_ACTION_REBUILD
;
5511 vdev_config_dirty(spa
->spa_root_vdev
);
5512 spa
->spa_config_splitting
= NULL
;
5516 (void) spa_vdev_exit(spa
, NULL
, txg
, 0);
5518 if (zio_injection_enabled
)
5519 zio_handle_panic_injection(spa
, FTAG
, 3);
5521 /* split is complete; log a history record */
5522 spa_history_log_internal(newspa
, "split", NULL
,
5523 "from pool %s", spa_name(spa
));
5525 kmem_free(vml
, children
* sizeof (vdev_t
*));
5527 /* if we're not going to mount the filesystems in userland, export */
5529 error
= spa_export_common(newname
, POOL_STATE_EXPORTED
, NULL
,
5536 spa_deactivate(newspa
);
5539 txg
= spa_vdev_config_enter(spa
);
5541 /* re-online all offlined disks */
5542 for (c
= 0; c
< children
; c
++) {
5544 vml
[c
]->vdev_offline
= B_FALSE
;
5546 vdev_reopen(spa
->spa_root_vdev
);
5548 nvlist_free(spa
->spa_config_splitting
);
5549 spa
->spa_config_splitting
= NULL
;
5550 (void) spa_vdev_exit(spa
, NULL
, txg
, error
);
5552 kmem_free(vml
, children
* sizeof (vdev_t
*));
5557 spa_nvlist_lookup_by_guid(nvlist_t
**nvpp
, int count
, uint64_t target_guid
)
5561 for (i
= 0; i
< count
; i
++) {
5564 VERIFY(nvlist_lookup_uint64(nvpp
[i
], ZPOOL_CONFIG_GUID
,
5567 if (guid
== target_guid
)
5575 spa_vdev_remove_aux(nvlist_t
*config
, char *name
, nvlist_t
**dev
, int count
,
5576 nvlist_t
*dev_to_remove
)
5578 nvlist_t
**newdev
= NULL
;
5582 newdev
= kmem_alloc((count
- 1) * sizeof (void *), KM_SLEEP
);
5584 for (i
= 0, j
= 0; i
< count
; i
++) {
5585 if (dev
[i
] == dev_to_remove
)
5587 VERIFY(nvlist_dup(dev
[i
], &newdev
[j
++], KM_SLEEP
) == 0);
5590 VERIFY(nvlist_remove(config
, name
, DATA_TYPE_NVLIST_ARRAY
) == 0);
5591 VERIFY(nvlist_add_nvlist_array(config
, name
, newdev
, count
- 1) == 0);
5593 for (i
= 0; i
< count
- 1; i
++)
5594 nvlist_free(newdev
[i
]);
5597 kmem_free(newdev
, (count
- 1) * sizeof (void *));
5601 * Evacuate the device.
5604 spa_vdev_remove_evacuate(spa_t
*spa
, vdev_t
*vd
)
5609 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
5610 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
5611 ASSERT(vd
== vd
->vdev_top
);
5614 * Evacuate the device. We don't hold the config lock as writer
5615 * since we need to do I/O but we do keep the
5616 * spa_namespace_lock held. Once this completes the device
5617 * should no longer have any blocks allocated on it.
5619 if (vd
->vdev_islog
) {
5620 if (vd
->vdev_stat
.vs_alloc
!= 0)
5621 error
= spa_offline_log(spa
);
5623 error
= SET_ERROR(ENOTSUP
);
5630 * The evacuation succeeded. Remove any remaining MOS metadata
5631 * associated with this vdev, and wait for these changes to sync.
5633 ASSERT0(vd
->vdev_stat
.vs_alloc
);
5634 txg
= spa_vdev_config_enter(spa
);
5635 vd
->vdev_removing
= B_TRUE
;
5636 vdev_dirty_leaves(vd
, VDD_DTL
, txg
);
5637 vdev_config_dirty(vd
);
5638 spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
5644 * Complete the removal by cleaning up the namespace.
5647 spa_vdev_remove_from_namespace(spa_t
*spa
, vdev_t
*vd
)
5649 vdev_t
*rvd
= spa
->spa_root_vdev
;
5650 uint64_t id
= vd
->vdev_id
;
5651 boolean_t last_vdev
= (id
== (rvd
->vdev_children
- 1));
5653 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
5654 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
5655 ASSERT(vd
== vd
->vdev_top
);
5658 * Only remove any devices which are empty.
5660 if (vd
->vdev_stat
.vs_alloc
!= 0)
5663 (void) vdev_label_init(vd
, 0, VDEV_LABEL_REMOVE
);
5665 if (list_link_active(&vd
->vdev_state_dirty_node
))
5666 vdev_state_clean(vd
);
5667 if (list_link_active(&vd
->vdev_config_dirty_node
))
5668 vdev_config_clean(vd
);
5673 vdev_compact_children(rvd
);
5675 vd
= vdev_alloc_common(spa
, id
, 0, &vdev_hole_ops
);
5676 vdev_add_child(rvd
, vd
);
5678 vdev_config_dirty(rvd
);
5681 * Reassess the health of our root vdev.
5687 * Remove a device from the pool -
5689 * Removing a device from the vdev namespace requires several steps
5690 * and can take a significant amount of time. As a result we use
5691 * the spa_vdev_config_[enter/exit] functions which allow us to
5692 * grab and release the spa_config_lock while still holding the namespace
5693 * lock. During each step the configuration is synced out.
5695 * Currently, this supports removing only hot spares, slogs, and level 2 ARC
5699 spa_vdev_remove(spa_t
*spa
, uint64_t guid
, boolean_t unspare
)
5702 sysevent_t
*ev
= NULL
;
5703 metaslab_group_t
*mg
;
5704 nvlist_t
**spares
, **l2cache
, *nv
;
5706 uint_t nspares
, nl2cache
;
5708 boolean_t locked
= MUTEX_HELD(&spa_namespace_lock
);
5710 ASSERT(spa_writeable(spa
));
5713 txg
= spa_vdev_enter(spa
);
5715 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
5717 if (spa
->spa_spares
.sav_vdevs
!= NULL
&&
5718 nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
5719 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0 &&
5720 (nv
= spa_nvlist_lookup_by_guid(spares
, nspares
, guid
)) != NULL
) {
5722 * Only remove the hot spare if it's not currently in use
5725 if (vd
== NULL
|| unspare
) {
5727 vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
);
5728 ev
= spa_event_create(spa
, vd
, NULL
,
5729 ESC_ZFS_VDEV_REMOVE_AUX
);
5730 spa_vdev_remove_aux(spa
->spa_spares
.sav_config
,
5731 ZPOOL_CONFIG_SPARES
, spares
, nspares
, nv
);
5732 spa_load_spares(spa
);
5733 spa
->spa_spares
.sav_sync
= B_TRUE
;
5735 error
= SET_ERROR(EBUSY
);
5737 } else if (spa
->spa_l2cache
.sav_vdevs
!= NULL
&&
5738 nvlist_lookup_nvlist_array(spa
->spa_l2cache
.sav_config
,
5739 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0 &&
5740 (nv
= spa_nvlist_lookup_by_guid(l2cache
, nl2cache
, guid
)) != NULL
) {
5742 * Cache devices can always be removed.
5744 vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
);
5745 ev
= spa_event_create(spa
, vd
, NULL
, ESC_ZFS_VDEV_REMOVE_AUX
);
5746 spa_vdev_remove_aux(spa
->spa_l2cache
.sav_config
,
5747 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
, nv
);
5748 spa_load_l2cache(spa
);
5749 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
5750 } else if (vd
!= NULL
&& vd
->vdev_islog
) {
5752 ASSERT(vd
== vd
->vdev_top
);
5757 * Stop allocating from this vdev.
5759 metaslab_group_passivate(mg
);
5762 * Wait for the youngest allocations and frees to sync,
5763 * and then wait for the deferral of those frees to finish.
5765 spa_vdev_config_exit(spa
, NULL
,
5766 txg
+ TXG_CONCURRENT_STATES
+ TXG_DEFER_SIZE
, 0, FTAG
);
5769 * Attempt to evacuate the vdev.
5771 error
= spa_vdev_remove_evacuate(spa
, vd
);
5773 txg
= spa_vdev_config_enter(spa
);
5776 * If we couldn't evacuate the vdev, unwind.
5779 metaslab_group_activate(mg
);
5780 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5784 * Clean up the vdev namespace.
5786 ev
= spa_event_create(spa
, vd
, NULL
, ESC_ZFS_VDEV_REMOVE_DEV
);
5787 spa_vdev_remove_from_namespace(spa
, vd
);
5789 } else if (vd
!= NULL
) {
5791 * Normal vdevs cannot be removed (yet).
5793 error
= SET_ERROR(ENOTSUP
);
5796 * There is no vdev of any kind with the specified guid.
5798 error
= SET_ERROR(ENOENT
);
5802 error
= spa_vdev_exit(spa
, NULL
, txg
, error
);
5811 * Find any device that's done replacing, or a vdev marked 'unspare' that's
5812 * currently spared, so we can detach it.
5815 spa_vdev_resilver_done_hunt(vdev_t
*vd
)
5817 vdev_t
*newvd
, *oldvd
;
5820 for (c
= 0; c
< vd
->vdev_children
; c
++) {
5821 oldvd
= spa_vdev_resilver_done_hunt(vd
->vdev_child
[c
]);
5827 * Check for a completed replacement. We always consider the first
5828 * vdev in the list to be the oldest vdev, and the last one to be
5829 * the newest (see spa_vdev_attach() for how that works). In
5830 * the case where the newest vdev is faulted, we will not automatically
5831 * remove it after a resilver completes. This is OK as it will require
5832 * user intervention to determine which disk the admin wishes to keep.
5834 if (vd
->vdev_ops
== &vdev_replacing_ops
) {
5835 ASSERT(vd
->vdev_children
> 1);
5837 newvd
= vd
->vdev_child
[vd
->vdev_children
- 1];
5838 oldvd
= vd
->vdev_child
[0];
5840 if (vdev_dtl_empty(newvd
, DTL_MISSING
) &&
5841 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
5842 !vdev_dtl_required(oldvd
))
5847 * Check for a completed resilver with the 'unspare' flag set.
5849 if (vd
->vdev_ops
== &vdev_spare_ops
) {
5850 vdev_t
*first
= vd
->vdev_child
[0];
5851 vdev_t
*last
= vd
->vdev_child
[vd
->vdev_children
- 1];
5853 if (last
->vdev_unspare
) {
5856 } else if (first
->vdev_unspare
) {
5863 if (oldvd
!= NULL
&&
5864 vdev_dtl_empty(newvd
, DTL_MISSING
) &&
5865 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
5866 !vdev_dtl_required(oldvd
))
5870 * If there are more than two spares attached to a disk,
5871 * and those spares are not required, then we want to
5872 * attempt to free them up now so that they can be used
5873 * by other pools. Once we're back down to a single
5874 * disk+spare, we stop removing them.
5876 if (vd
->vdev_children
> 2) {
5877 newvd
= vd
->vdev_child
[1];
5879 if (newvd
->vdev_isspare
&& last
->vdev_isspare
&&
5880 vdev_dtl_empty(last
, DTL_MISSING
) &&
5881 vdev_dtl_empty(last
, DTL_OUTAGE
) &&
5882 !vdev_dtl_required(newvd
))
5891 spa_vdev_resilver_done(spa_t
*spa
)
5893 vdev_t
*vd
, *pvd
, *ppvd
;
5894 uint64_t guid
, sguid
, pguid
, ppguid
;
5896 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5898 while ((vd
= spa_vdev_resilver_done_hunt(spa
->spa_root_vdev
)) != NULL
) {
5899 pvd
= vd
->vdev_parent
;
5900 ppvd
= pvd
->vdev_parent
;
5901 guid
= vd
->vdev_guid
;
5902 pguid
= pvd
->vdev_guid
;
5903 ppguid
= ppvd
->vdev_guid
;
5906 * If we have just finished replacing a hot spared device, then
5907 * we need to detach the parent's first child (the original hot
5910 if (ppvd
->vdev_ops
== &vdev_spare_ops
&& pvd
->vdev_id
== 0 &&
5911 ppvd
->vdev_children
== 2) {
5912 ASSERT(pvd
->vdev_ops
== &vdev_replacing_ops
);
5913 sguid
= ppvd
->vdev_child
[1]->vdev_guid
;
5915 ASSERT(vd
->vdev_resilver_txg
== 0 || !vdev_dtl_required(vd
));
5917 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5918 if (spa_vdev_detach(spa
, guid
, pguid
, B_TRUE
) != 0)
5920 if (sguid
&& spa_vdev_detach(spa
, sguid
, ppguid
, B_TRUE
) != 0)
5922 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5925 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5929 * Update the stored path or FRU for this vdev.
5932 spa_vdev_set_common(spa_t
*spa
, uint64_t guid
, const char *value
,
5936 boolean_t sync
= B_FALSE
;
5938 ASSERT(spa_writeable(spa
));
5940 spa_vdev_state_enter(spa
, SCL_ALL
);
5942 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
5943 return (spa_vdev_state_exit(spa
, NULL
, ENOENT
));
5945 if (!vd
->vdev_ops
->vdev_op_leaf
)
5946 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
5949 if (strcmp(value
, vd
->vdev_path
) != 0) {
5950 spa_strfree(vd
->vdev_path
);
5951 vd
->vdev_path
= spa_strdup(value
);
5955 if (vd
->vdev_fru
== NULL
) {
5956 vd
->vdev_fru
= spa_strdup(value
);
5958 } else if (strcmp(value
, vd
->vdev_fru
) != 0) {
5959 spa_strfree(vd
->vdev_fru
);
5960 vd
->vdev_fru
= spa_strdup(value
);
5965 return (spa_vdev_state_exit(spa
, sync
? vd
: NULL
, 0));
5969 spa_vdev_setpath(spa_t
*spa
, uint64_t guid
, const char *newpath
)
5971 return (spa_vdev_set_common(spa
, guid
, newpath
, B_TRUE
));
5975 spa_vdev_setfru(spa_t
*spa
, uint64_t guid
, const char *newfru
)
5977 return (spa_vdev_set_common(spa
, guid
, newfru
, B_FALSE
));
5981 * ==========================================================================
5983 * ==========================================================================
5986 spa_scrub_pause_resume(spa_t
*spa
, pool_scrub_cmd_t cmd
)
5988 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
5990 if (dsl_scan_resilvering(spa
->spa_dsl_pool
))
5991 return (SET_ERROR(EBUSY
));
5993 return (dsl_scrub_set_pause_resume(spa
->spa_dsl_pool
, cmd
));
5997 spa_scan_stop(spa_t
*spa
)
5999 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
6000 if (dsl_scan_resilvering(spa
->spa_dsl_pool
))
6001 return (SET_ERROR(EBUSY
));
6002 return (dsl_scan_cancel(spa
->spa_dsl_pool
));
6006 spa_scan(spa_t
*spa
, pool_scan_func_t func
)
6008 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
6010 if (func
>= POOL_SCAN_FUNCS
|| func
== POOL_SCAN_NONE
)
6011 return (SET_ERROR(ENOTSUP
));
6014 * If a resilver was requested, but there is no DTL on a
6015 * writeable leaf device, we have nothing to do.
6017 if (func
== POOL_SCAN_RESILVER
&&
6018 !vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
)) {
6019 spa_async_request(spa
, SPA_ASYNC_RESILVER_DONE
);
6023 return (dsl_scan(spa
->spa_dsl_pool
, func
));
6027 * ==========================================================================
6028 * SPA async task processing
6029 * ==========================================================================
6033 spa_async_remove(spa_t
*spa
, vdev_t
*vd
)
6037 if (vd
->vdev_remove_wanted
) {
6038 vd
->vdev_remove_wanted
= B_FALSE
;
6039 vd
->vdev_delayed_close
= B_FALSE
;
6040 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_REMOVED
, VDEV_AUX_NONE
);
6043 * We want to clear the stats, but we don't want to do a full
6044 * vdev_clear() as that will cause us to throw away
6045 * degraded/faulted state as well as attempt to reopen the
6046 * device, all of which is a waste.
6048 vd
->vdev_stat
.vs_read_errors
= 0;
6049 vd
->vdev_stat
.vs_write_errors
= 0;
6050 vd
->vdev_stat
.vs_checksum_errors
= 0;
6052 vdev_state_dirty(vd
->vdev_top
);
6055 for (c
= 0; c
< vd
->vdev_children
; c
++)
6056 spa_async_remove(spa
, vd
->vdev_child
[c
]);
6060 spa_async_probe(spa_t
*spa
, vdev_t
*vd
)
6064 if (vd
->vdev_probe_wanted
) {
6065 vd
->vdev_probe_wanted
= B_FALSE
;
6066 vdev_reopen(vd
); /* vdev_open() does the actual probe */
6069 for (c
= 0; c
< vd
->vdev_children
; c
++)
6070 spa_async_probe(spa
, vd
->vdev_child
[c
]);
6074 spa_async_autoexpand(spa_t
*spa
, vdev_t
*vd
)
6078 if (!spa
->spa_autoexpand
)
6081 for (c
= 0; c
< vd
->vdev_children
; c
++) {
6082 vdev_t
*cvd
= vd
->vdev_child
[c
];
6083 spa_async_autoexpand(spa
, cvd
);
6086 if (!vd
->vdev_ops
->vdev_op_leaf
|| vd
->vdev_physpath
== NULL
)
6089 spa_event_notify(vd
->vdev_spa
, vd
, NULL
, ESC_ZFS_VDEV_AUTOEXPAND
);
6093 spa_async_thread(spa_t
*spa
)
6097 ASSERT(spa
->spa_sync_on
);
6099 mutex_enter(&spa
->spa_async_lock
);
6100 tasks
= spa
->spa_async_tasks
;
6101 spa
->spa_async_tasks
= 0;
6102 mutex_exit(&spa
->spa_async_lock
);
6105 * See if the config needs to be updated.
6107 if (tasks
& SPA_ASYNC_CONFIG_UPDATE
) {
6108 uint64_t old_space
, new_space
;
6110 mutex_enter(&spa_namespace_lock
);
6111 old_space
= metaslab_class_get_space(spa_normal_class(spa
));
6112 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
6113 new_space
= metaslab_class_get_space(spa_normal_class(spa
));
6114 mutex_exit(&spa_namespace_lock
);
6117 * If the pool grew as a result of the config update,
6118 * then log an internal history event.
6120 if (new_space
!= old_space
) {
6121 spa_history_log_internal(spa
, "vdev online", NULL
,
6122 "pool '%s' size: %llu(+%llu)",
6123 spa_name(spa
), new_space
, new_space
- old_space
);
6128 * See if any devices need to be marked REMOVED.
6130 if (tasks
& SPA_ASYNC_REMOVE
) {
6131 spa_vdev_state_enter(spa
, SCL_NONE
);
6132 spa_async_remove(spa
, spa
->spa_root_vdev
);
6133 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++)
6134 spa_async_remove(spa
, spa
->spa_l2cache
.sav_vdevs
[i
]);
6135 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
6136 spa_async_remove(spa
, spa
->spa_spares
.sav_vdevs
[i
]);
6137 (void) spa_vdev_state_exit(spa
, NULL
, 0);
6140 if ((tasks
& SPA_ASYNC_AUTOEXPAND
) && !spa_suspended(spa
)) {
6141 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
6142 spa_async_autoexpand(spa
, spa
->spa_root_vdev
);
6143 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
6147 * See if any devices need to be probed.
6149 if (tasks
& SPA_ASYNC_PROBE
) {
6150 spa_vdev_state_enter(spa
, SCL_NONE
);
6151 spa_async_probe(spa
, spa
->spa_root_vdev
);
6152 (void) spa_vdev_state_exit(spa
, NULL
, 0);
6156 * If any devices are done replacing, detach them.
6158 if (tasks
& SPA_ASYNC_RESILVER_DONE
)
6159 spa_vdev_resilver_done(spa
);
6162 * Kick off a resilver.
6164 if (tasks
& SPA_ASYNC_RESILVER
)
6165 dsl_resilver_restart(spa
->spa_dsl_pool
, 0);
6168 * Let the world know that we're done.
6170 mutex_enter(&spa
->spa_async_lock
);
6171 spa
->spa_async_thread
= NULL
;
6172 cv_broadcast(&spa
->spa_async_cv
);
6173 mutex_exit(&spa
->spa_async_lock
);
6178 spa_async_suspend(spa_t
*spa
)
6180 mutex_enter(&spa
->spa_async_lock
);
6181 spa
->spa_async_suspended
++;
6182 while (spa
->spa_async_thread
!= NULL
)
6183 cv_wait(&spa
->spa_async_cv
, &spa
->spa_async_lock
);
6184 mutex_exit(&spa
->spa_async_lock
);
6188 spa_async_resume(spa_t
*spa
)
6190 mutex_enter(&spa
->spa_async_lock
);
6191 ASSERT(spa
->spa_async_suspended
!= 0);
6192 spa
->spa_async_suspended
--;
6193 mutex_exit(&spa
->spa_async_lock
);
6197 spa_async_tasks_pending(spa_t
*spa
)
6199 uint_t non_config_tasks
;
6201 boolean_t config_task_suspended
;
6203 non_config_tasks
= spa
->spa_async_tasks
& ~SPA_ASYNC_CONFIG_UPDATE
;
6204 config_task
= spa
->spa_async_tasks
& SPA_ASYNC_CONFIG_UPDATE
;
6205 if (spa
->spa_ccw_fail_time
== 0) {
6206 config_task_suspended
= B_FALSE
;
6208 config_task_suspended
=
6209 (gethrtime() - spa
->spa_ccw_fail_time
) <
6210 ((hrtime_t
)zfs_ccw_retry_interval
* NANOSEC
);
6213 return (non_config_tasks
|| (config_task
&& !config_task_suspended
));
6217 spa_async_dispatch(spa_t
*spa
)
6219 mutex_enter(&spa
->spa_async_lock
);
6220 if (spa_async_tasks_pending(spa
) &&
6221 !spa
->spa_async_suspended
&&
6222 spa
->spa_async_thread
== NULL
&&
6224 spa
->spa_async_thread
= thread_create(NULL
, 0,
6225 spa_async_thread
, spa
, 0, &p0
, TS_RUN
, maxclsyspri
);
6226 mutex_exit(&spa
->spa_async_lock
);
6230 spa_async_request(spa_t
*spa
, int task
)
6232 zfs_dbgmsg("spa=%s async request task=%u", spa
->spa_name
, task
);
6233 mutex_enter(&spa
->spa_async_lock
);
6234 spa
->spa_async_tasks
|= task
;
6235 mutex_exit(&spa
->spa_async_lock
);
6239 * ==========================================================================
6240 * SPA syncing routines
6241 * ==========================================================================
6245 bpobj_enqueue_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
6248 bpobj_enqueue(bpo
, bp
, tx
);
6253 spa_free_sync_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
6257 zio_nowait(zio_free_sync(zio
, zio
->io_spa
, dmu_tx_get_txg(tx
), bp
,
6263 * Note: this simple function is not inlined to make it easier to dtrace the
6264 * amount of time spent syncing frees.
6267 spa_sync_frees(spa_t
*spa
, bplist_t
*bpl
, dmu_tx_t
*tx
)
6269 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
6270 bplist_iterate(bpl
, spa_free_sync_cb
, zio
, tx
);
6271 VERIFY(zio_wait(zio
) == 0);
6275 * Note: this simple function is not inlined to make it easier to dtrace the
6276 * amount of time spent syncing deferred frees.
6279 spa_sync_deferred_frees(spa_t
*spa
, dmu_tx_t
*tx
)
6281 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
6282 VERIFY3U(bpobj_iterate(&spa
->spa_deferred_bpobj
,
6283 spa_free_sync_cb
, zio
, tx
), ==, 0);
6284 VERIFY0(zio_wait(zio
));
6288 spa_sync_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
*nv
, dmu_tx_t
*tx
)
6290 char *packed
= NULL
;
6295 VERIFY(nvlist_size(nv
, &nvsize
, NV_ENCODE_XDR
) == 0);
6298 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
6299 * information. This avoids the dmu_buf_will_dirty() path and
6300 * saves us a pre-read to get data we don't actually care about.
6302 bufsize
= P2ROUNDUP((uint64_t)nvsize
, SPA_CONFIG_BLOCKSIZE
);
6303 packed
= vmem_alloc(bufsize
, KM_SLEEP
);
6305 VERIFY(nvlist_pack(nv
, &packed
, &nvsize
, NV_ENCODE_XDR
,
6307 bzero(packed
+ nvsize
, bufsize
- nvsize
);
6309 dmu_write(spa
->spa_meta_objset
, obj
, 0, bufsize
, packed
, tx
);
6311 vmem_free(packed
, bufsize
);
6313 VERIFY(0 == dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
));
6314 dmu_buf_will_dirty(db
, tx
);
6315 *(uint64_t *)db
->db_data
= nvsize
;
6316 dmu_buf_rele(db
, FTAG
);
6320 spa_sync_aux_dev(spa_t
*spa
, spa_aux_vdev_t
*sav
, dmu_tx_t
*tx
,
6321 const char *config
, const char *entry
)
6331 * Update the MOS nvlist describing the list of available devices.
6332 * spa_validate_aux() will have already made sure this nvlist is
6333 * valid and the vdevs are labeled appropriately.
6335 if (sav
->sav_object
== 0) {
6336 sav
->sav_object
= dmu_object_alloc(spa
->spa_meta_objset
,
6337 DMU_OT_PACKED_NVLIST
, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE
,
6338 sizeof (uint64_t), tx
);
6339 VERIFY(zap_update(spa
->spa_meta_objset
,
6340 DMU_POOL_DIRECTORY_OBJECT
, entry
, sizeof (uint64_t), 1,
6341 &sav
->sav_object
, tx
) == 0);
6344 VERIFY(nvlist_alloc(&nvroot
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
6345 if (sav
->sav_count
== 0) {
6346 VERIFY(nvlist_add_nvlist_array(nvroot
, config
, NULL
, 0) == 0);
6348 list
= kmem_alloc(sav
->sav_count
*sizeof (void *), KM_SLEEP
);
6349 for (i
= 0; i
< sav
->sav_count
; i
++)
6350 list
[i
] = vdev_config_generate(spa
, sav
->sav_vdevs
[i
],
6351 B_FALSE
, VDEV_CONFIG_L2CACHE
);
6352 VERIFY(nvlist_add_nvlist_array(nvroot
, config
, list
,
6353 sav
->sav_count
) == 0);
6354 for (i
= 0; i
< sav
->sav_count
; i
++)
6355 nvlist_free(list
[i
]);
6356 kmem_free(list
, sav
->sav_count
* sizeof (void *));
6359 spa_sync_nvlist(spa
, sav
->sav_object
, nvroot
, tx
);
6360 nvlist_free(nvroot
);
6362 sav
->sav_sync
= B_FALSE
;
6366 * Rebuild spa's all-vdev ZAP from the vdev ZAPs indicated in each vdev_t.
6367 * The all-vdev ZAP must be empty.
6370 spa_avz_build(vdev_t
*vd
, uint64_t avz
, dmu_tx_t
*tx
)
6372 spa_t
*spa
= vd
->vdev_spa
;
6375 if (vd
->vdev_top_zap
!= 0) {
6376 VERIFY0(zap_add_int(spa
->spa_meta_objset
, avz
,
6377 vd
->vdev_top_zap
, tx
));
6379 if (vd
->vdev_leaf_zap
!= 0) {
6380 VERIFY0(zap_add_int(spa
->spa_meta_objset
, avz
,
6381 vd
->vdev_leaf_zap
, tx
));
6383 for (i
= 0; i
< vd
->vdev_children
; i
++) {
6384 spa_avz_build(vd
->vdev_child
[i
], avz
, tx
);
6389 spa_sync_config_object(spa_t
*spa
, dmu_tx_t
*tx
)
6394 * If the pool is being imported from a pre-per-vdev-ZAP version of ZFS,
6395 * its config may not be dirty but we still need to build per-vdev ZAPs.
6396 * Similarly, if the pool is being assembled (e.g. after a split), we
6397 * need to rebuild the AVZ although the config may not be dirty.
6399 if (list_is_empty(&spa
->spa_config_dirty_list
) &&
6400 spa
->spa_avz_action
== AVZ_ACTION_NONE
)
6403 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
6405 ASSERT(spa
->spa_avz_action
== AVZ_ACTION_NONE
||
6406 spa
->spa_avz_action
== AVZ_ACTION_INITIALIZE
||
6407 spa
->spa_all_vdev_zaps
!= 0);
6409 if (spa
->spa_avz_action
== AVZ_ACTION_REBUILD
) {
6413 /* Make and build the new AVZ */
6414 uint64_t new_avz
= zap_create(spa
->spa_meta_objset
,
6415 DMU_OTN_ZAP_METADATA
, DMU_OT_NONE
, 0, tx
);
6416 spa_avz_build(spa
->spa_root_vdev
, new_avz
, tx
);
6418 /* Diff old AVZ with new one */
6419 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
6420 spa
->spa_all_vdev_zaps
);
6421 zap_cursor_retrieve(&zc
, &za
) == 0;
6422 zap_cursor_advance(&zc
)) {
6423 uint64_t vdzap
= za
.za_first_integer
;
6424 if (zap_lookup_int(spa
->spa_meta_objset
, new_avz
,
6427 * ZAP is listed in old AVZ but not in new one;
6430 VERIFY0(zap_destroy(spa
->spa_meta_objset
, vdzap
,
6435 zap_cursor_fini(&zc
);
6437 /* Destroy the old AVZ */
6438 VERIFY0(zap_destroy(spa
->spa_meta_objset
,
6439 spa
->spa_all_vdev_zaps
, tx
));
6441 /* Replace the old AVZ in the dir obj with the new one */
6442 VERIFY0(zap_update(spa
->spa_meta_objset
,
6443 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_VDEV_ZAP_MAP
,
6444 sizeof (new_avz
), 1, &new_avz
, tx
));
6446 spa
->spa_all_vdev_zaps
= new_avz
;
6447 } else if (spa
->spa_avz_action
== AVZ_ACTION_DESTROY
) {
6451 /* Walk through the AVZ and destroy all listed ZAPs */
6452 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
6453 spa
->spa_all_vdev_zaps
);
6454 zap_cursor_retrieve(&zc
, &za
) == 0;
6455 zap_cursor_advance(&zc
)) {
6456 uint64_t zap
= za
.za_first_integer
;
6457 VERIFY0(zap_destroy(spa
->spa_meta_objset
, zap
, tx
));
6460 zap_cursor_fini(&zc
);
6462 /* Destroy and unlink the AVZ itself */
6463 VERIFY0(zap_destroy(spa
->spa_meta_objset
,
6464 spa
->spa_all_vdev_zaps
, tx
));
6465 VERIFY0(zap_remove(spa
->spa_meta_objset
,
6466 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_VDEV_ZAP_MAP
, tx
));
6467 spa
->spa_all_vdev_zaps
= 0;
6470 if (spa
->spa_all_vdev_zaps
== 0) {
6471 spa
->spa_all_vdev_zaps
= zap_create_link(spa
->spa_meta_objset
,
6472 DMU_OTN_ZAP_METADATA
, DMU_POOL_DIRECTORY_OBJECT
,
6473 DMU_POOL_VDEV_ZAP_MAP
, tx
);
6475 spa
->spa_avz_action
= AVZ_ACTION_NONE
;
6477 /* Create ZAPs for vdevs that don't have them. */
6478 vdev_construct_zaps(spa
->spa_root_vdev
, tx
);
6480 config
= spa_config_generate(spa
, spa
->spa_root_vdev
,
6481 dmu_tx_get_txg(tx
), B_FALSE
);
6484 * If we're upgrading the spa version then make sure that
6485 * the config object gets updated with the correct version.
6487 if (spa
->spa_ubsync
.ub_version
< spa
->spa_uberblock
.ub_version
)
6488 fnvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
6489 spa
->spa_uberblock
.ub_version
);
6491 spa_config_exit(spa
, SCL_STATE
, FTAG
);
6493 nvlist_free(spa
->spa_config_syncing
);
6494 spa
->spa_config_syncing
= config
;
6496 spa_sync_nvlist(spa
, spa
->spa_config_object
, config
, tx
);
6500 spa_sync_version(void *arg
, dmu_tx_t
*tx
)
6502 uint64_t *versionp
= arg
;
6503 uint64_t version
= *versionp
;
6504 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
6507 * Setting the version is special cased when first creating the pool.
6509 ASSERT(tx
->tx_txg
!= TXG_INITIAL
);
6511 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
6512 ASSERT(version
>= spa_version(spa
));
6514 spa
->spa_uberblock
.ub_version
= version
;
6515 vdev_config_dirty(spa
->spa_root_vdev
);
6516 spa_history_log_internal(spa
, "set", tx
, "version=%lld", version
);
6520 * Set zpool properties.
6523 spa_sync_props(void *arg
, dmu_tx_t
*tx
)
6525 nvlist_t
*nvp
= arg
;
6526 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
6527 objset_t
*mos
= spa
->spa_meta_objset
;
6528 nvpair_t
*elem
= NULL
;
6530 mutex_enter(&spa
->spa_props_lock
);
6532 while ((elem
= nvlist_next_nvpair(nvp
, elem
))) {
6534 char *strval
, *fname
;
6536 const char *propname
;
6537 zprop_type_t proptype
;
6540 prop
= zpool_name_to_prop(nvpair_name(elem
));
6541 switch ((int)prop
) {
6544 * We checked this earlier in spa_prop_validate().
6546 ASSERT(zpool_prop_feature(nvpair_name(elem
)));
6548 fname
= strchr(nvpair_name(elem
), '@') + 1;
6549 VERIFY0(zfeature_lookup_name(fname
, &fid
));
6551 spa_feature_enable(spa
, fid
, tx
);
6552 spa_history_log_internal(spa
, "set", tx
,
6553 "%s=enabled", nvpair_name(elem
));
6556 case ZPOOL_PROP_VERSION
:
6557 intval
= fnvpair_value_uint64(elem
);
6559 * The version is synced separately before other
6560 * properties and should be correct by now.
6562 ASSERT3U(spa_version(spa
), >=, intval
);
6565 case ZPOOL_PROP_ALTROOT
:
6567 * 'altroot' is a non-persistent property. It should
6568 * have been set temporarily at creation or import time.
6570 ASSERT(spa
->spa_root
!= NULL
);
6573 case ZPOOL_PROP_READONLY
:
6574 case ZPOOL_PROP_CACHEFILE
:
6576 * 'readonly' and 'cachefile' are also non-persisitent
6580 case ZPOOL_PROP_COMMENT
:
6581 strval
= fnvpair_value_string(elem
);
6582 if (spa
->spa_comment
!= NULL
)
6583 spa_strfree(spa
->spa_comment
);
6584 spa
->spa_comment
= spa_strdup(strval
);
6586 * We need to dirty the configuration on all the vdevs
6587 * so that their labels get updated. It's unnecessary
6588 * to do this for pool creation since the vdev's
6589 * configuration has already been dirtied.
6591 if (tx
->tx_txg
!= TXG_INITIAL
)
6592 vdev_config_dirty(spa
->spa_root_vdev
);
6593 spa_history_log_internal(spa
, "set", tx
,
6594 "%s=%s", nvpair_name(elem
), strval
);
6598 * Set pool property values in the poolprops mos object.
6600 if (spa
->spa_pool_props_object
== 0) {
6601 spa
->spa_pool_props_object
=
6602 zap_create_link(mos
, DMU_OT_POOL_PROPS
,
6603 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_PROPS
,
6607 /* normalize the property name */
6608 propname
= zpool_prop_to_name(prop
);
6609 proptype
= zpool_prop_get_type(prop
);
6611 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
6612 ASSERT(proptype
== PROP_TYPE_STRING
);
6613 strval
= fnvpair_value_string(elem
);
6614 VERIFY0(zap_update(mos
,
6615 spa
->spa_pool_props_object
, propname
,
6616 1, strlen(strval
) + 1, strval
, tx
));
6617 spa_history_log_internal(spa
, "set", tx
,
6618 "%s=%s", nvpair_name(elem
), strval
);
6619 } else if (nvpair_type(elem
) == DATA_TYPE_UINT64
) {
6620 intval
= fnvpair_value_uint64(elem
);
6622 if (proptype
== PROP_TYPE_INDEX
) {
6624 VERIFY0(zpool_prop_index_to_string(
6625 prop
, intval
, &unused
));
6627 VERIFY0(zap_update(mos
,
6628 spa
->spa_pool_props_object
, propname
,
6629 8, 1, &intval
, tx
));
6630 spa_history_log_internal(spa
, "set", tx
,
6631 "%s=%lld", nvpair_name(elem
), intval
);
6633 ASSERT(0); /* not allowed */
6637 case ZPOOL_PROP_DELEGATION
:
6638 spa
->spa_delegation
= intval
;
6640 case ZPOOL_PROP_BOOTFS
:
6641 spa
->spa_bootfs
= intval
;
6643 case ZPOOL_PROP_FAILUREMODE
:
6644 spa
->spa_failmode
= intval
;
6646 case ZPOOL_PROP_AUTOEXPAND
:
6647 spa
->spa_autoexpand
= intval
;
6648 if (tx
->tx_txg
!= TXG_INITIAL
)
6649 spa_async_request(spa
,
6650 SPA_ASYNC_AUTOEXPAND
);
6652 case ZPOOL_PROP_MULTIHOST
:
6653 spa
->spa_multihost
= intval
;
6655 case ZPOOL_PROP_DEDUPDITTO
:
6656 spa
->spa_dedup_ditto
= intval
;
6665 mutex_exit(&spa
->spa_props_lock
);
6669 * Perform one-time upgrade on-disk changes. spa_version() does not
6670 * reflect the new version this txg, so there must be no changes this
6671 * txg to anything that the upgrade code depends on after it executes.
6672 * Therefore this must be called after dsl_pool_sync() does the sync
6676 spa_sync_upgrades(spa_t
*spa
, dmu_tx_t
*tx
)
6678 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
6680 ASSERT(spa
->spa_sync_pass
== 1);
6682 rrw_enter(&dp
->dp_config_rwlock
, RW_WRITER
, FTAG
);
6684 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_ORIGIN
&&
6685 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_ORIGIN
) {
6686 dsl_pool_create_origin(dp
, tx
);
6688 /* Keeping the origin open increases spa_minref */
6689 spa
->spa_minref
+= 3;
6692 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_NEXT_CLONES
&&
6693 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_NEXT_CLONES
) {
6694 dsl_pool_upgrade_clones(dp
, tx
);
6697 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_DIR_CLONES
&&
6698 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_DIR_CLONES
) {
6699 dsl_pool_upgrade_dir_clones(dp
, tx
);
6701 /* Keeping the freedir open increases spa_minref */
6702 spa
->spa_minref
+= 3;
6705 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_FEATURES
&&
6706 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
6707 spa_feature_create_zap_objects(spa
, tx
);
6711 * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable
6712 * when possibility to use lz4 compression for metadata was added
6713 * Old pools that have this feature enabled must be upgraded to have
6714 * this feature active
6716 if (spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
6717 boolean_t lz4_en
= spa_feature_is_enabled(spa
,
6718 SPA_FEATURE_LZ4_COMPRESS
);
6719 boolean_t lz4_ac
= spa_feature_is_active(spa
,
6720 SPA_FEATURE_LZ4_COMPRESS
);
6722 if (lz4_en
&& !lz4_ac
)
6723 spa_feature_incr(spa
, SPA_FEATURE_LZ4_COMPRESS
, tx
);
6727 * If we haven't written the salt, do so now. Note that the
6728 * feature may not be activated yet, but that's fine since
6729 * the presence of this ZAP entry is backwards compatible.
6731 if (zap_contains(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
6732 DMU_POOL_CHECKSUM_SALT
) == ENOENT
) {
6733 VERIFY0(zap_add(spa
->spa_meta_objset
,
6734 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CHECKSUM_SALT
, 1,
6735 sizeof (spa
->spa_cksum_salt
.zcs_bytes
),
6736 spa
->spa_cksum_salt
.zcs_bytes
, tx
));
6739 rrw_exit(&dp
->dp_config_rwlock
, FTAG
);
6743 * Sync the specified transaction group. New blocks may be dirtied as
6744 * part of the process, so we iterate until it converges.
6747 spa_sync(spa_t
*spa
, uint64_t txg
)
6749 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
6750 objset_t
*mos
= spa
->spa_meta_objset
;
6751 bplist_t
*free_bpl
= &spa
->spa_free_bplist
[txg
& TXG_MASK
];
6752 metaslab_class_t
*mc
;
6753 vdev_t
*rvd
= spa
->spa_root_vdev
;
6757 uint32_t max_queue_depth
= zfs_vdev_async_write_max_active
*
6758 zfs_vdev_queue_depth_pct
/ 100;
6759 uint64_t queue_depth_total
;
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 queue_depth_total
= 0;
6836 for (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 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 (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");