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>
58 #include <sys/uberblock_impl.h>
61 #include <sys/dmu_traverse.h>
62 #include <sys/dmu_objset.h>
63 #include <sys/unique.h>
64 #include <sys/dsl_pool.h>
65 #include <sys/dsl_dataset.h>
66 #include <sys/dsl_dir.h>
67 #include <sys/dsl_prop.h>
68 #include <sys/dsl_synctask.h>
69 #include <sys/fs/zfs.h>
71 #include <sys/callb.h>
72 #include <sys/systeminfo.h>
73 #include <sys/spa_boot.h>
74 #include <sys/zfs_ioctl.h>
75 #include <sys/dsl_scan.h>
76 #include <sys/zfeature.h>
77 #include <sys/dsl_destroy.h>
81 #include <sys/fm/protocol.h>
82 #include <sys/fm/util.h>
83 #include <sys/bootprops.h>
84 #include <sys/callb.h>
85 #include <sys/cpupart.h>
87 #include <sys/sysdc.h>
92 #include "zfs_comutil.h"
95 * The interval, in seconds, at which failed configuration cache file writes
98 static int zfs_ccw_retry_interval
= 300;
100 typedef enum zti_modes
{
101 ZTI_MODE_FIXED
, /* value is # of threads (min 1) */
102 ZTI_MODE_BATCH
, /* cpu-intensive; value is ignored */
103 ZTI_MODE_NULL
, /* don't create a taskq */
107 #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) }
108 #define ZTI_PCT(n) { ZTI_MODE_ONLINE_PERCENT, (n), 1 }
109 #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 }
110 #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 }
112 #define ZTI_N(n) ZTI_P(n, 1)
113 #define ZTI_ONE ZTI_N(1)
115 typedef struct zio_taskq_info
{
116 zti_modes_t zti_mode
;
121 static const char *const zio_taskq_types
[ZIO_TASKQ_TYPES
] = {
122 "iss", "iss_h", "int", "int_h"
126 * This table defines the taskq settings for each ZFS I/O type. When
127 * initializing a pool, we use this table to create an appropriately sized
128 * taskq. Some operations are low volume and therefore have a small, static
129 * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
130 * macros. Other operations process a large amount of data; the ZTI_BATCH
131 * macro causes us to create a taskq oriented for throughput. Some operations
132 * are so high frequency and short-lived that the taskq itself can become a a
133 * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
134 * additional degree of parallelism specified by the number of threads per-
135 * taskq and the number of taskqs; when dispatching an event in this case, the
136 * particular taskq is chosen at random.
138 * The different taskq priorities are to handle the different contexts (issue
139 * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that
140 * need to be handled with minimum delay.
142 const zio_taskq_info_t zio_taskqs
[ZIO_TYPES
][ZIO_TASKQ_TYPES
] = {
143 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
144 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* NULL */
145 { ZTI_N(8), ZTI_NULL
, ZTI_P(12, 8), ZTI_NULL
}, /* READ */
146 { ZTI_BATCH
, ZTI_N(5), ZTI_P(12, 8), ZTI_N(5) }, /* WRITE */
147 { ZTI_P(12, 8), ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* FREE */
148 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* CLAIM */
149 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* IOCTL */
152 static sysevent_t
*spa_event_create(spa_t
*spa
, vdev_t
*vd
, nvlist_t
*hist_nvl
,
154 static void spa_event_post(sysevent_t
*ev
);
155 static void spa_sync_version(void *arg
, dmu_tx_t
*tx
);
156 static void spa_sync_props(void *arg
, dmu_tx_t
*tx
);
157 static boolean_t
spa_has_active_shared_spare(spa_t
*spa
);
158 static inline int spa_load_impl(spa_t
*spa
, uint64_t, nvlist_t
*config
,
159 spa_load_state_t state
, spa_import_type_t type
, boolean_t mosconfig
,
161 static void spa_vdev_resilver_done(spa_t
*spa
);
163 uint_t zio_taskq_batch_pct
= 75; /* 1 thread per cpu in pset */
164 id_t zio_taskq_psrset_bind
= PS_NONE
;
165 boolean_t zio_taskq_sysdc
= B_TRUE
; /* use SDC scheduling class */
166 uint_t zio_taskq_basedc
= 80; /* base duty cycle */
168 boolean_t spa_create_process
= B_TRUE
; /* no process ==> no sysdc */
171 * This (illegal) pool name is used when temporarily importing a spa_t in order
172 * to get the vdev stats associated with the imported devices.
174 #define TRYIMPORT_NAME "$import"
177 * ==========================================================================
178 * SPA properties routines
179 * ==========================================================================
183 * Add a (source=src, propname=propval) list to an nvlist.
186 spa_prop_add_list(nvlist_t
*nvl
, zpool_prop_t prop
, char *strval
,
187 uint64_t intval
, zprop_source_t src
)
189 const char *propname
= zpool_prop_to_name(prop
);
192 VERIFY(nvlist_alloc(&propval
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
193 VERIFY(nvlist_add_uint64(propval
, ZPROP_SOURCE
, src
) == 0);
196 VERIFY(nvlist_add_string(propval
, ZPROP_VALUE
, strval
) == 0);
198 VERIFY(nvlist_add_uint64(propval
, ZPROP_VALUE
, intval
) == 0);
200 VERIFY(nvlist_add_nvlist(nvl
, propname
, propval
) == 0);
201 nvlist_free(propval
);
205 * Get property values from the spa configuration.
208 spa_prop_get_config(spa_t
*spa
, nvlist_t
**nvp
)
210 vdev_t
*rvd
= spa
->spa_root_vdev
;
211 dsl_pool_t
*pool
= spa
->spa_dsl_pool
;
212 uint64_t size
, alloc
, cap
, version
;
213 const zprop_source_t src
= ZPROP_SRC_NONE
;
214 spa_config_dirent_t
*dp
;
215 metaslab_class_t
*mc
= spa_normal_class(spa
);
217 ASSERT(MUTEX_HELD(&spa
->spa_props_lock
));
220 alloc
= metaslab_class_get_alloc(spa_normal_class(spa
));
221 size
= metaslab_class_get_space(spa_normal_class(spa
));
222 spa_prop_add_list(*nvp
, ZPOOL_PROP_NAME
, spa_name(spa
), 0, src
);
223 spa_prop_add_list(*nvp
, ZPOOL_PROP_SIZE
, NULL
, size
, src
);
224 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALLOCATED
, NULL
, alloc
, src
);
225 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREE
, NULL
,
228 spa_prop_add_list(*nvp
, ZPOOL_PROP_FRAGMENTATION
, NULL
,
229 metaslab_class_fragmentation(mc
), src
);
230 spa_prop_add_list(*nvp
, ZPOOL_PROP_EXPANDSZ
, NULL
,
231 metaslab_class_expandable_space(mc
), src
);
232 spa_prop_add_list(*nvp
, ZPOOL_PROP_READONLY
, NULL
,
233 (spa_mode(spa
) == FREAD
), src
);
235 cap
= (size
== 0) ? 0 : (alloc
* 100 / size
);
236 spa_prop_add_list(*nvp
, ZPOOL_PROP_CAPACITY
, NULL
, cap
, src
);
238 spa_prop_add_list(*nvp
, ZPOOL_PROP_DEDUPRATIO
, NULL
,
239 ddt_get_pool_dedup_ratio(spa
), src
);
241 spa_prop_add_list(*nvp
, ZPOOL_PROP_HEALTH
, NULL
,
242 rvd
->vdev_state
, src
);
244 version
= spa_version(spa
);
245 if (version
== zpool_prop_default_numeric(ZPOOL_PROP_VERSION
)) {
246 spa_prop_add_list(*nvp
, ZPOOL_PROP_VERSION
, NULL
,
247 version
, ZPROP_SRC_DEFAULT
);
249 spa_prop_add_list(*nvp
, ZPOOL_PROP_VERSION
, NULL
,
250 version
, ZPROP_SRC_LOCAL
);
256 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
257 * when opening pools before this version freedir will be NULL.
259 if (pool
->dp_free_dir
!= NULL
) {
260 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
, NULL
,
261 dsl_dir_phys(pool
->dp_free_dir
)->dd_used_bytes
,
264 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
,
268 if (pool
->dp_leak_dir
!= NULL
) {
269 spa_prop_add_list(*nvp
, ZPOOL_PROP_LEAKED
, NULL
,
270 dsl_dir_phys(pool
->dp_leak_dir
)->dd_used_bytes
,
273 spa_prop_add_list(*nvp
, ZPOOL_PROP_LEAKED
,
278 spa_prop_add_list(*nvp
, ZPOOL_PROP_GUID
, NULL
, spa_guid(spa
), src
);
280 if (spa
->spa_comment
!= NULL
) {
281 spa_prop_add_list(*nvp
, ZPOOL_PROP_COMMENT
, spa
->spa_comment
,
285 if (spa
->spa_root
!= NULL
)
286 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALTROOT
, spa
->spa_root
,
289 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_BLOCKS
)) {
290 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXBLOCKSIZE
, NULL
,
291 MIN(zfs_max_recordsize
, SPA_MAXBLOCKSIZE
), ZPROP_SRC_NONE
);
293 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXBLOCKSIZE
, NULL
,
294 SPA_OLD_MAXBLOCKSIZE
, ZPROP_SRC_NONE
);
297 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_DNODE
)) {
298 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXDNODESIZE
, NULL
,
299 DNODE_MAX_SIZE
, ZPROP_SRC_NONE
);
301 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXDNODESIZE
, NULL
,
302 DNODE_MIN_SIZE
, ZPROP_SRC_NONE
);
305 if ((dp
= list_head(&spa
->spa_config_list
)) != NULL
) {
306 if (dp
->scd_path
== NULL
) {
307 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
308 "none", 0, ZPROP_SRC_LOCAL
);
309 } else if (strcmp(dp
->scd_path
, spa_config_path
) != 0) {
310 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
311 dp
->scd_path
, 0, ZPROP_SRC_LOCAL
);
317 * Get zpool property values.
320 spa_prop_get(spa_t
*spa
, nvlist_t
**nvp
)
322 objset_t
*mos
= spa
->spa_meta_objset
;
327 err
= nvlist_alloc(nvp
, NV_UNIQUE_NAME
, KM_SLEEP
);
331 mutex_enter(&spa
->spa_props_lock
);
334 * Get properties from the spa config.
336 spa_prop_get_config(spa
, nvp
);
338 /* If no pool property object, no more prop to get. */
339 if (mos
== NULL
|| spa
->spa_pool_props_object
== 0) {
340 mutex_exit(&spa
->spa_props_lock
);
345 * Get properties from the MOS pool property object.
347 for (zap_cursor_init(&zc
, mos
, spa
->spa_pool_props_object
);
348 (err
= zap_cursor_retrieve(&zc
, &za
)) == 0;
349 zap_cursor_advance(&zc
)) {
352 zprop_source_t src
= ZPROP_SRC_DEFAULT
;
355 if ((prop
= zpool_name_to_prop(za
.za_name
)) == ZPROP_INVAL
)
358 switch (za
.za_integer_length
) {
360 /* integer property */
361 if (za
.za_first_integer
!=
362 zpool_prop_default_numeric(prop
))
363 src
= ZPROP_SRC_LOCAL
;
365 if (prop
== ZPOOL_PROP_BOOTFS
) {
367 dsl_dataset_t
*ds
= NULL
;
369 dp
= spa_get_dsl(spa
);
370 dsl_pool_config_enter(dp
, FTAG
);
371 if ((err
= dsl_dataset_hold_obj(dp
,
372 za
.za_first_integer
, FTAG
, &ds
))) {
373 dsl_pool_config_exit(dp
, FTAG
);
377 strval
= kmem_alloc(ZFS_MAX_DATASET_NAME_LEN
,
379 dsl_dataset_name(ds
, strval
);
380 dsl_dataset_rele(ds
, FTAG
);
381 dsl_pool_config_exit(dp
, FTAG
);
384 intval
= za
.za_first_integer
;
387 spa_prop_add_list(*nvp
, prop
, strval
, intval
, src
);
390 kmem_free(strval
, ZFS_MAX_DATASET_NAME_LEN
);
395 /* string property */
396 strval
= kmem_alloc(za
.za_num_integers
, KM_SLEEP
);
397 err
= zap_lookup(mos
, spa
->spa_pool_props_object
,
398 za
.za_name
, 1, za
.za_num_integers
, strval
);
400 kmem_free(strval
, za
.za_num_integers
);
403 spa_prop_add_list(*nvp
, prop
, strval
, 0, src
);
404 kmem_free(strval
, za
.za_num_integers
);
411 zap_cursor_fini(&zc
);
412 mutex_exit(&spa
->spa_props_lock
);
414 if (err
&& err
!= ENOENT
) {
424 * Validate the given pool properties nvlist and modify the list
425 * for the property values to be set.
428 spa_prop_validate(spa_t
*spa
, nvlist_t
*props
)
431 int error
= 0, reset_bootfs
= 0;
433 boolean_t has_feature
= B_FALSE
;
436 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
438 char *strval
, *slash
, *check
, *fname
;
439 const char *propname
= nvpair_name(elem
);
440 zpool_prop_t prop
= zpool_name_to_prop(propname
);
444 if (!zpool_prop_feature(propname
)) {
445 error
= SET_ERROR(EINVAL
);
450 * Sanitize the input.
452 if (nvpair_type(elem
) != DATA_TYPE_UINT64
) {
453 error
= SET_ERROR(EINVAL
);
457 if (nvpair_value_uint64(elem
, &intval
) != 0) {
458 error
= SET_ERROR(EINVAL
);
463 error
= SET_ERROR(EINVAL
);
467 fname
= strchr(propname
, '@') + 1;
468 if (zfeature_lookup_name(fname
, NULL
) != 0) {
469 error
= SET_ERROR(EINVAL
);
473 has_feature
= B_TRUE
;
476 case ZPOOL_PROP_VERSION
:
477 error
= nvpair_value_uint64(elem
, &intval
);
479 (intval
< spa_version(spa
) ||
480 intval
> SPA_VERSION_BEFORE_FEATURES
||
482 error
= SET_ERROR(EINVAL
);
485 case ZPOOL_PROP_DELEGATION
:
486 case ZPOOL_PROP_AUTOREPLACE
:
487 case ZPOOL_PROP_LISTSNAPS
:
488 case ZPOOL_PROP_AUTOEXPAND
:
489 error
= nvpair_value_uint64(elem
, &intval
);
490 if (!error
&& intval
> 1)
491 error
= SET_ERROR(EINVAL
);
494 case ZPOOL_PROP_BOOTFS
:
496 * If the pool version is less than SPA_VERSION_BOOTFS,
497 * or the pool is still being created (version == 0),
498 * the bootfs property cannot be set.
500 if (spa_version(spa
) < SPA_VERSION_BOOTFS
) {
501 error
= SET_ERROR(ENOTSUP
);
506 * Make sure the vdev config is bootable
508 if (!vdev_is_bootable(spa
->spa_root_vdev
)) {
509 error
= SET_ERROR(ENOTSUP
);
515 error
= nvpair_value_string(elem
, &strval
);
521 if (strval
== NULL
|| strval
[0] == '\0') {
522 objnum
= zpool_prop_default_numeric(
527 error
= dmu_objset_hold(strval
, FTAG
, &os
);
532 * Must be ZPL, and its property settings
533 * must be supported by GRUB (compression
534 * is not gzip, and large blocks or large
535 * dnodes are not used).
538 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
539 error
= SET_ERROR(ENOTSUP
);
541 dsl_prop_get_int_ds(dmu_objset_ds(os
),
542 zfs_prop_to_name(ZFS_PROP_COMPRESSION
),
544 !BOOTFS_COMPRESS_VALID(propval
)) {
545 error
= SET_ERROR(ENOTSUP
);
547 dsl_prop_get_int_ds(dmu_objset_ds(os
),
548 zfs_prop_to_name(ZFS_PROP_DNODESIZE
),
550 propval
!= ZFS_DNSIZE_LEGACY
) {
551 error
= SET_ERROR(ENOTSUP
);
553 objnum
= dmu_objset_id(os
);
555 dmu_objset_rele(os
, FTAG
);
559 case ZPOOL_PROP_FAILUREMODE
:
560 error
= nvpair_value_uint64(elem
, &intval
);
561 if (!error
&& intval
> ZIO_FAILURE_MODE_PANIC
)
562 error
= SET_ERROR(EINVAL
);
565 * This is a special case which only occurs when
566 * the pool has completely failed. This allows
567 * the user to change the in-core failmode property
568 * without syncing it out to disk (I/Os might
569 * currently be blocked). We do this by returning
570 * EIO to the caller (spa_prop_set) to trick it
571 * into thinking we encountered a property validation
574 if (!error
&& spa_suspended(spa
)) {
575 spa
->spa_failmode
= intval
;
576 error
= SET_ERROR(EIO
);
580 case ZPOOL_PROP_CACHEFILE
:
581 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
584 if (strval
[0] == '\0')
587 if (strcmp(strval
, "none") == 0)
590 if (strval
[0] != '/') {
591 error
= SET_ERROR(EINVAL
);
595 slash
= strrchr(strval
, '/');
596 ASSERT(slash
!= NULL
);
598 if (slash
[1] == '\0' || strcmp(slash
, "/.") == 0 ||
599 strcmp(slash
, "/..") == 0)
600 error
= SET_ERROR(EINVAL
);
603 case ZPOOL_PROP_COMMENT
:
604 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
606 for (check
= strval
; *check
!= '\0'; check
++) {
607 if (!isprint(*check
)) {
608 error
= SET_ERROR(EINVAL
);
612 if (strlen(strval
) > ZPROP_MAX_COMMENT
)
613 error
= SET_ERROR(E2BIG
);
616 case ZPOOL_PROP_DEDUPDITTO
:
617 if (spa_version(spa
) < SPA_VERSION_DEDUP
)
618 error
= SET_ERROR(ENOTSUP
);
620 error
= nvpair_value_uint64(elem
, &intval
);
622 intval
!= 0 && intval
< ZIO_DEDUPDITTO_MIN
)
623 error
= SET_ERROR(EINVAL
);
634 if (!error
&& reset_bootfs
) {
635 error
= nvlist_remove(props
,
636 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), DATA_TYPE_STRING
);
639 error
= nvlist_add_uint64(props
,
640 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), objnum
);
648 spa_configfile_set(spa_t
*spa
, nvlist_t
*nvp
, boolean_t need_sync
)
651 spa_config_dirent_t
*dp
;
653 if (nvlist_lookup_string(nvp
, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
),
657 dp
= kmem_alloc(sizeof (spa_config_dirent_t
),
660 if (cachefile
[0] == '\0')
661 dp
->scd_path
= spa_strdup(spa_config_path
);
662 else if (strcmp(cachefile
, "none") == 0)
665 dp
->scd_path
= spa_strdup(cachefile
);
667 list_insert_head(&spa
->spa_config_list
, dp
);
669 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
673 spa_prop_set(spa_t
*spa
, nvlist_t
*nvp
)
676 nvpair_t
*elem
= NULL
;
677 boolean_t need_sync
= B_FALSE
;
679 if ((error
= spa_prop_validate(spa
, nvp
)) != 0)
682 while ((elem
= nvlist_next_nvpair(nvp
, elem
)) != NULL
) {
683 zpool_prop_t prop
= zpool_name_to_prop(nvpair_name(elem
));
685 if (prop
== ZPOOL_PROP_CACHEFILE
||
686 prop
== ZPOOL_PROP_ALTROOT
||
687 prop
== ZPOOL_PROP_READONLY
)
690 if (prop
== ZPOOL_PROP_VERSION
|| prop
== ZPROP_INVAL
) {
693 if (prop
== ZPOOL_PROP_VERSION
) {
694 VERIFY(nvpair_value_uint64(elem
, &ver
) == 0);
696 ASSERT(zpool_prop_feature(nvpair_name(elem
)));
697 ver
= SPA_VERSION_FEATURES
;
701 /* Save time if the version is already set. */
702 if (ver
== spa_version(spa
))
706 * In addition to the pool directory object, we might
707 * create the pool properties object, the features for
708 * read object, the features for write object, or the
709 * feature descriptions object.
711 error
= dsl_sync_task(spa
->spa_name
, NULL
,
712 spa_sync_version
, &ver
,
713 6, ZFS_SPACE_CHECK_RESERVED
);
724 return (dsl_sync_task(spa
->spa_name
, NULL
, spa_sync_props
,
725 nvp
, 6, ZFS_SPACE_CHECK_RESERVED
));
732 * If the bootfs property value is dsobj, clear it.
735 spa_prop_clear_bootfs(spa_t
*spa
, uint64_t dsobj
, dmu_tx_t
*tx
)
737 if (spa
->spa_bootfs
== dsobj
&& spa
->spa_pool_props_object
!= 0) {
738 VERIFY(zap_remove(spa
->spa_meta_objset
,
739 spa
->spa_pool_props_object
,
740 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), tx
) == 0);
747 spa_change_guid_check(void *arg
, dmu_tx_t
*tx
)
749 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
750 vdev_t
*rvd
= spa
->spa_root_vdev
;
752 ASSERTV(uint64_t *newguid
= arg
);
754 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
755 vdev_state
= rvd
->vdev_state
;
756 spa_config_exit(spa
, SCL_STATE
, FTAG
);
758 if (vdev_state
!= VDEV_STATE_HEALTHY
)
759 return (SET_ERROR(ENXIO
));
761 ASSERT3U(spa_guid(spa
), !=, *newguid
);
767 spa_change_guid_sync(void *arg
, dmu_tx_t
*tx
)
769 uint64_t *newguid
= arg
;
770 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
772 vdev_t
*rvd
= spa
->spa_root_vdev
;
774 oldguid
= spa_guid(spa
);
776 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
777 rvd
->vdev_guid
= *newguid
;
778 rvd
->vdev_guid_sum
+= (*newguid
- oldguid
);
779 vdev_config_dirty(rvd
);
780 spa_config_exit(spa
, SCL_STATE
, FTAG
);
782 spa_history_log_internal(spa
, "guid change", tx
, "old=%llu new=%llu",
787 * Change the GUID for the pool. This is done so that we can later
788 * re-import a pool built from a clone of our own vdevs. We will modify
789 * the root vdev's guid, our own pool guid, and then mark all of our
790 * vdevs dirty. Note that we must make sure that all our vdevs are
791 * online when we do this, or else any vdevs that weren't present
792 * would be orphaned from our pool. We are also going to issue a
793 * sysevent to update any watchers.
796 spa_change_guid(spa_t
*spa
)
801 mutex_enter(&spa
->spa_vdev_top_lock
);
802 mutex_enter(&spa_namespace_lock
);
803 guid
= spa_generate_guid(NULL
);
805 error
= dsl_sync_task(spa
->spa_name
, spa_change_guid_check
,
806 spa_change_guid_sync
, &guid
, 5, ZFS_SPACE_CHECK_RESERVED
);
809 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
810 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_REGUID
);
813 mutex_exit(&spa_namespace_lock
);
814 mutex_exit(&spa
->spa_vdev_top_lock
);
820 * ==========================================================================
821 * SPA state manipulation (open/create/destroy/import/export)
822 * ==========================================================================
826 spa_error_entry_compare(const void *a
, const void *b
)
828 const spa_error_entry_t
*sa
= (const spa_error_entry_t
*)a
;
829 const spa_error_entry_t
*sb
= (const spa_error_entry_t
*)b
;
832 ret
= memcmp(&sa
->se_bookmark
, &sb
->se_bookmark
,
833 sizeof (zbookmark_phys_t
));
835 return (AVL_ISIGN(ret
));
839 * Utility function which retrieves copies of the current logs and
840 * re-initializes them in the process.
843 spa_get_errlists(spa_t
*spa
, avl_tree_t
*last
, avl_tree_t
*scrub
)
845 ASSERT(MUTEX_HELD(&spa
->spa_errlist_lock
));
847 bcopy(&spa
->spa_errlist_last
, last
, sizeof (avl_tree_t
));
848 bcopy(&spa
->spa_errlist_scrub
, scrub
, sizeof (avl_tree_t
));
850 avl_create(&spa
->spa_errlist_scrub
,
851 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
852 offsetof(spa_error_entry_t
, se_avl
));
853 avl_create(&spa
->spa_errlist_last
,
854 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
855 offsetof(spa_error_entry_t
, se_avl
));
859 spa_taskqs_init(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
861 const zio_taskq_info_t
*ztip
= &zio_taskqs
[t
][q
];
862 enum zti_modes mode
= ztip
->zti_mode
;
863 uint_t value
= ztip
->zti_value
;
864 uint_t count
= ztip
->zti_count
;
865 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
868 boolean_t batch
= B_FALSE
;
870 if (mode
== ZTI_MODE_NULL
) {
872 tqs
->stqs_taskq
= NULL
;
876 ASSERT3U(count
, >, 0);
878 tqs
->stqs_count
= count
;
879 tqs
->stqs_taskq
= kmem_alloc(count
* sizeof (taskq_t
*), KM_SLEEP
);
883 ASSERT3U(value
, >=, 1);
884 value
= MAX(value
, 1);
885 flags
|= TASKQ_DYNAMIC
;
890 flags
|= TASKQ_THREADS_CPU_PCT
;
891 value
= MIN(zio_taskq_batch_pct
, 100);
895 panic("unrecognized mode for %s_%s taskq (%u:%u) in "
897 zio_type_name
[t
], zio_taskq_types
[q
], mode
, value
);
901 for (i
= 0; i
< count
; i
++) {
905 (void) snprintf(name
, sizeof (name
), "%s_%s_%u",
906 zio_type_name
[t
], zio_taskq_types
[q
], i
);
908 (void) snprintf(name
, sizeof (name
), "%s_%s",
909 zio_type_name
[t
], zio_taskq_types
[q
]);
912 if (zio_taskq_sysdc
&& spa
->spa_proc
!= &p0
) {
914 flags
|= TASKQ_DC_BATCH
;
916 tq
= taskq_create_sysdc(name
, value
, 50, INT_MAX
,
917 spa
->spa_proc
, zio_taskq_basedc
, flags
);
919 pri_t pri
= maxclsyspri
;
921 * The write issue taskq can be extremely CPU
922 * intensive. Run it at slightly less important
923 * priority than the other taskqs. Under Linux this
924 * means incrementing the priority value on platforms
925 * like illumos it should be decremented.
927 if (t
== ZIO_TYPE_WRITE
&& q
== ZIO_TASKQ_ISSUE
)
930 tq
= taskq_create_proc(name
, value
, pri
, 50,
931 INT_MAX
, spa
->spa_proc
, flags
);
934 tqs
->stqs_taskq
[i
] = tq
;
939 spa_taskqs_fini(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
941 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
944 if (tqs
->stqs_taskq
== NULL
) {
945 ASSERT3U(tqs
->stqs_count
, ==, 0);
949 for (i
= 0; i
< tqs
->stqs_count
; i
++) {
950 ASSERT3P(tqs
->stqs_taskq
[i
], !=, NULL
);
951 taskq_destroy(tqs
->stqs_taskq
[i
]);
954 kmem_free(tqs
->stqs_taskq
, tqs
->stqs_count
* sizeof (taskq_t
*));
955 tqs
->stqs_taskq
= NULL
;
959 * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority.
960 * Note that a type may have multiple discrete taskqs to avoid lock contention
961 * on the taskq itself. In that case we choose which taskq at random by using
962 * the low bits of gethrtime().
965 spa_taskq_dispatch_ent(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
966 task_func_t
*func
, void *arg
, uint_t flags
, taskq_ent_t
*ent
)
968 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
971 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
972 ASSERT3U(tqs
->stqs_count
, !=, 0);
974 if (tqs
->stqs_count
== 1) {
975 tq
= tqs
->stqs_taskq
[0];
977 tq
= tqs
->stqs_taskq
[((uint64_t)gethrtime()) % tqs
->stqs_count
];
980 taskq_dispatch_ent(tq
, func
, arg
, flags
, ent
);
984 * Same as spa_taskq_dispatch_ent() but block on the task until completion.
987 spa_taskq_dispatch_sync(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
988 task_func_t
*func
, void *arg
, uint_t flags
)
990 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
994 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
995 ASSERT3U(tqs
->stqs_count
, !=, 0);
997 if (tqs
->stqs_count
== 1) {
998 tq
= tqs
->stqs_taskq
[0];
1000 tq
= tqs
->stqs_taskq
[((uint64_t)gethrtime()) % tqs
->stqs_count
];
1003 id
= taskq_dispatch(tq
, func
, arg
, flags
);
1005 taskq_wait_id(tq
, id
);
1009 spa_create_zio_taskqs(spa_t
*spa
)
1013 for (t
= 0; t
< ZIO_TYPES
; t
++) {
1014 for (q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1015 spa_taskqs_init(spa
, t
, q
);
1020 #if defined(_KERNEL) && defined(HAVE_SPA_THREAD)
1022 spa_thread(void *arg
)
1024 callb_cpr_t cprinfo
;
1027 user_t
*pu
= PTOU(curproc
);
1029 CALLB_CPR_INIT(&cprinfo
, &spa
->spa_proc_lock
, callb_generic_cpr
,
1032 ASSERT(curproc
!= &p0
);
1033 (void) snprintf(pu
->u_psargs
, sizeof (pu
->u_psargs
),
1034 "zpool-%s", spa
->spa_name
);
1035 (void) strlcpy(pu
->u_comm
, pu
->u_psargs
, sizeof (pu
->u_comm
));
1037 /* bind this thread to the requested psrset */
1038 if (zio_taskq_psrset_bind
!= PS_NONE
) {
1040 mutex_enter(&cpu_lock
);
1041 mutex_enter(&pidlock
);
1042 mutex_enter(&curproc
->p_lock
);
1044 if (cpupart_bind_thread(curthread
, zio_taskq_psrset_bind
,
1045 0, NULL
, NULL
) == 0) {
1046 curthread
->t_bind_pset
= zio_taskq_psrset_bind
;
1049 "Couldn't bind process for zfs pool \"%s\" to "
1050 "pset %d\n", spa
->spa_name
, zio_taskq_psrset_bind
);
1053 mutex_exit(&curproc
->p_lock
);
1054 mutex_exit(&pidlock
);
1055 mutex_exit(&cpu_lock
);
1059 if (zio_taskq_sysdc
) {
1060 sysdc_thread_enter(curthread
, 100, 0);
1063 spa
->spa_proc
= curproc
;
1064 spa
->spa_did
= curthread
->t_did
;
1066 spa_create_zio_taskqs(spa
);
1068 mutex_enter(&spa
->spa_proc_lock
);
1069 ASSERT(spa
->spa_proc_state
== SPA_PROC_CREATED
);
1071 spa
->spa_proc_state
= SPA_PROC_ACTIVE
;
1072 cv_broadcast(&spa
->spa_proc_cv
);
1074 CALLB_CPR_SAFE_BEGIN(&cprinfo
);
1075 while (spa
->spa_proc_state
== SPA_PROC_ACTIVE
)
1076 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1077 CALLB_CPR_SAFE_END(&cprinfo
, &spa
->spa_proc_lock
);
1079 ASSERT(spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
);
1080 spa
->spa_proc_state
= SPA_PROC_GONE
;
1081 spa
->spa_proc
= &p0
;
1082 cv_broadcast(&spa
->spa_proc_cv
);
1083 CALLB_CPR_EXIT(&cprinfo
); /* drops spa_proc_lock */
1085 mutex_enter(&curproc
->p_lock
);
1091 * Activate an uninitialized pool.
1094 spa_activate(spa_t
*spa
, int mode
)
1096 ASSERT(spa
->spa_state
== POOL_STATE_UNINITIALIZED
);
1098 spa
->spa_state
= POOL_STATE_ACTIVE
;
1099 spa
->spa_mode
= mode
;
1101 spa
->spa_normal_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
1102 spa
->spa_log_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
1104 /* Try to create a covering process */
1105 mutex_enter(&spa
->spa_proc_lock
);
1106 ASSERT(spa
->spa_proc_state
== SPA_PROC_NONE
);
1107 ASSERT(spa
->spa_proc
== &p0
);
1110 #ifdef HAVE_SPA_THREAD
1111 /* Only create a process if we're going to be around a while. */
1112 if (spa_create_process
&& strcmp(spa
->spa_name
, TRYIMPORT_NAME
) != 0) {
1113 if (newproc(spa_thread
, (caddr_t
)spa
, syscid
, maxclsyspri
,
1115 spa
->spa_proc_state
= SPA_PROC_CREATED
;
1116 while (spa
->spa_proc_state
== SPA_PROC_CREATED
) {
1117 cv_wait(&spa
->spa_proc_cv
,
1118 &spa
->spa_proc_lock
);
1120 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1121 ASSERT(spa
->spa_proc
!= &p0
);
1122 ASSERT(spa
->spa_did
!= 0);
1126 "Couldn't create process for zfs pool \"%s\"\n",
1131 #endif /* HAVE_SPA_THREAD */
1132 mutex_exit(&spa
->spa_proc_lock
);
1134 /* If we didn't create a process, we need to create our taskqs. */
1135 if (spa
->spa_proc
== &p0
) {
1136 spa_create_zio_taskqs(spa
);
1139 list_create(&spa
->spa_config_dirty_list
, sizeof (vdev_t
),
1140 offsetof(vdev_t
, vdev_config_dirty_node
));
1141 list_create(&spa
->spa_evicting_os_list
, sizeof (objset_t
),
1142 offsetof(objset_t
, os_evicting_node
));
1143 list_create(&spa
->spa_state_dirty_list
, sizeof (vdev_t
),
1144 offsetof(vdev_t
, vdev_state_dirty_node
));
1146 txg_list_create(&spa
->spa_vdev_txg_list
, spa
,
1147 offsetof(struct vdev
, vdev_txg_node
));
1149 avl_create(&spa
->spa_errlist_scrub
,
1150 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1151 offsetof(spa_error_entry_t
, se_avl
));
1152 avl_create(&spa
->spa_errlist_last
,
1153 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1154 offsetof(spa_error_entry_t
, se_avl
));
1157 * This taskq is used to perform zvol-minor-related tasks
1158 * asynchronously. This has several advantages, including easy
1159 * resolution of various deadlocks (zfsonlinux bug #3681).
1161 * The taskq must be single threaded to ensure tasks are always
1162 * processed in the order in which they were dispatched.
1164 * A taskq per pool allows one to keep the pools independent.
1165 * This way if one pool is suspended, it will not impact another.
1167 * The preferred location to dispatch a zvol minor task is a sync
1168 * task. In this context, there is easy access to the spa_t and minimal
1169 * error handling is required because the sync task must succeed.
1171 spa
->spa_zvol_taskq
= taskq_create("z_zvol", 1, defclsyspri
,
1175 * The taskq to upgrade datasets in this pool. Currently used by
1176 * feature SPA_FEATURE_USEROBJ_ACCOUNTING.
1178 spa
->spa_upgrade_taskq
= taskq_create("z_upgrade", boot_ncpus
,
1179 defclsyspri
, 1, INT_MAX
, TASKQ_DYNAMIC
);
1183 * Opposite of spa_activate().
1186 spa_deactivate(spa_t
*spa
)
1190 ASSERT(spa
->spa_sync_on
== B_FALSE
);
1191 ASSERT(spa
->spa_dsl_pool
== NULL
);
1192 ASSERT(spa
->spa_root_vdev
== NULL
);
1193 ASSERT(spa
->spa_async_zio_root
== NULL
);
1194 ASSERT(spa
->spa_state
!= POOL_STATE_UNINITIALIZED
);
1196 spa_evicting_os_wait(spa
);
1198 if (spa
->spa_zvol_taskq
) {
1199 taskq_destroy(spa
->spa_zvol_taskq
);
1200 spa
->spa_zvol_taskq
= NULL
;
1203 if (spa
->spa_upgrade_taskq
) {
1204 taskq_destroy(spa
->spa_upgrade_taskq
);
1205 spa
->spa_upgrade_taskq
= NULL
;
1208 txg_list_destroy(&spa
->spa_vdev_txg_list
);
1210 list_destroy(&spa
->spa_config_dirty_list
);
1211 list_destroy(&spa
->spa_evicting_os_list
);
1212 list_destroy(&spa
->spa_state_dirty_list
);
1214 taskq_cancel_id(system_delay_taskq
, spa
->spa_deadman_tqid
);
1216 for (t
= 0; t
< ZIO_TYPES
; t
++) {
1217 for (q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1218 spa_taskqs_fini(spa
, t
, q
);
1222 metaslab_class_destroy(spa
->spa_normal_class
);
1223 spa
->spa_normal_class
= NULL
;
1225 metaslab_class_destroy(spa
->spa_log_class
);
1226 spa
->spa_log_class
= NULL
;
1229 * If this was part of an import or the open otherwise failed, we may
1230 * still have errors left in the queues. Empty them just in case.
1232 spa_errlog_drain(spa
);
1234 avl_destroy(&spa
->spa_errlist_scrub
);
1235 avl_destroy(&spa
->spa_errlist_last
);
1237 spa
->spa_state
= POOL_STATE_UNINITIALIZED
;
1239 mutex_enter(&spa
->spa_proc_lock
);
1240 if (spa
->spa_proc_state
!= SPA_PROC_NONE
) {
1241 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1242 spa
->spa_proc_state
= SPA_PROC_DEACTIVATE
;
1243 cv_broadcast(&spa
->spa_proc_cv
);
1244 while (spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
) {
1245 ASSERT(spa
->spa_proc
!= &p0
);
1246 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1248 ASSERT(spa
->spa_proc_state
== SPA_PROC_GONE
);
1249 spa
->spa_proc_state
= SPA_PROC_NONE
;
1251 ASSERT(spa
->spa_proc
== &p0
);
1252 mutex_exit(&spa
->spa_proc_lock
);
1255 * We want to make sure spa_thread() has actually exited the ZFS
1256 * module, so that the module can't be unloaded out from underneath
1259 if (spa
->spa_did
!= 0) {
1260 thread_join(spa
->spa_did
);
1266 * Verify a pool configuration, and construct the vdev tree appropriately. This
1267 * will create all the necessary vdevs in the appropriate layout, with each vdev
1268 * in the CLOSED state. This will prep the pool before open/creation/import.
1269 * All vdev validation is done by the vdev_alloc() routine.
1272 spa_config_parse(spa_t
*spa
, vdev_t
**vdp
, nvlist_t
*nv
, vdev_t
*parent
,
1273 uint_t id
, int atype
)
1280 if ((error
= vdev_alloc(spa
, vdp
, nv
, parent
, id
, atype
)) != 0)
1283 if ((*vdp
)->vdev_ops
->vdev_op_leaf
)
1286 error
= nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1289 if (error
== ENOENT
)
1295 return (SET_ERROR(EINVAL
));
1298 for (c
= 0; c
< children
; c
++) {
1300 if ((error
= spa_config_parse(spa
, &vd
, child
[c
], *vdp
, c
,
1308 ASSERT(*vdp
!= NULL
);
1314 * Opposite of spa_load().
1317 spa_unload(spa_t
*spa
)
1321 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1326 spa_async_suspend(spa
);
1331 if (spa
->spa_sync_on
) {
1332 txg_sync_stop(spa
->spa_dsl_pool
);
1333 spa
->spa_sync_on
= B_FALSE
;
1337 * Even though vdev_free() also calls vdev_metaslab_fini, we need
1338 * to call it earlier, before we wait for async i/o to complete.
1339 * This ensures that there is no async metaslab prefetching, by
1340 * calling taskq_wait(mg_taskq).
1342 if (spa
->spa_root_vdev
!= NULL
) {
1343 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1344 for (c
= 0; c
< spa
->spa_root_vdev
->vdev_children
; c
++)
1345 vdev_metaslab_fini(spa
->spa_root_vdev
->vdev_child
[c
]);
1346 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1350 * Wait for any outstanding async I/O to complete.
1352 if (spa
->spa_async_zio_root
!= NULL
) {
1353 for (i
= 0; i
< max_ncpus
; i
++)
1354 (void) zio_wait(spa
->spa_async_zio_root
[i
]);
1355 kmem_free(spa
->spa_async_zio_root
, max_ncpus
* sizeof (void *));
1356 spa
->spa_async_zio_root
= NULL
;
1359 bpobj_close(&spa
->spa_deferred_bpobj
);
1361 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1366 if (spa
->spa_root_vdev
)
1367 vdev_free(spa
->spa_root_vdev
);
1368 ASSERT(spa
->spa_root_vdev
== NULL
);
1371 * Close the dsl pool.
1373 if (spa
->spa_dsl_pool
) {
1374 dsl_pool_close(spa
->spa_dsl_pool
);
1375 spa
->spa_dsl_pool
= NULL
;
1376 spa
->spa_meta_objset
= NULL
;
1382 * Drop and purge level 2 cache
1384 spa_l2cache_drop(spa
);
1386 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1387 vdev_free(spa
->spa_spares
.sav_vdevs
[i
]);
1388 if (spa
->spa_spares
.sav_vdevs
) {
1389 kmem_free(spa
->spa_spares
.sav_vdevs
,
1390 spa
->spa_spares
.sav_count
* sizeof (void *));
1391 spa
->spa_spares
.sav_vdevs
= NULL
;
1393 if (spa
->spa_spares
.sav_config
) {
1394 nvlist_free(spa
->spa_spares
.sav_config
);
1395 spa
->spa_spares
.sav_config
= NULL
;
1397 spa
->spa_spares
.sav_count
= 0;
1399 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
1400 vdev_clear_stats(spa
->spa_l2cache
.sav_vdevs
[i
]);
1401 vdev_free(spa
->spa_l2cache
.sav_vdevs
[i
]);
1403 if (spa
->spa_l2cache
.sav_vdevs
) {
1404 kmem_free(spa
->spa_l2cache
.sav_vdevs
,
1405 spa
->spa_l2cache
.sav_count
* sizeof (void *));
1406 spa
->spa_l2cache
.sav_vdevs
= NULL
;
1408 if (spa
->spa_l2cache
.sav_config
) {
1409 nvlist_free(spa
->spa_l2cache
.sav_config
);
1410 spa
->spa_l2cache
.sav_config
= NULL
;
1412 spa
->spa_l2cache
.sav_count
= 0;
1414 spa
->spa_async_suspended
= 0;
1416 if (spa
->spa_comment
!= NULL
) {
1417 spa_strfree(spa
->spa_comment
);
1418 spa
->spa_comment
= NULL
;
1421 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1425 * Load (or re-load) the current list of vdevs describing the active spares for
1426 * this pool. When this is called, we have some form of basic information in
1427 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1428 * then re-generate a more complete list including status information.
1431 spa_load_spares(spa_t
*spa
)
1438 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1441 * First, close and free any existing spare vdevs.
1443 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1444 vd
= spa
->spa_spares
.sav_vdevs
[i
];
1446 /* Undo the call to spa_activate() below */
1447 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1448 B_FALSE
)) != NULL
&& tvd
->vdev_isspare
)
1449 spa_spare_remove(tvd
);
1454 if (spa
->spa_spares
.sav_vdevs
)
1455 kmem_free(spa
->spa_spares
.sav_vdevs
,
1456 spa
->spa_spares
.sav_count
* sizeof (void *));
1458 if (spa
->spa_spares
.sav_config
== NULL
)
1461 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
1462 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
1464 spa
->spa_spares
.sav_count
= (int)nspares
;
1465 spa
->spa_spares
.sav_vdevs
= NULL
;
1471 * Construct the array of vdevs, opening them to get status in the
1472 * process. For each spare, there is potentially two different vdev_t
1473 * structures associated with it: one in the list of spares (used only
1474 * for basic validation purposes) and one in the active vdev
1475 * configuration (if it's spared in). During this phase we open and
1476 * validate each vdev on the spare list. If the vdev also exists in the
1477 * active configuration, then we also mark this vdev as an active spare.
1479 spa
->spa_spares
.sav_vdevs
= kmem_zalloc(nspares
* sizeof (void *),
1481 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1482 VERIFY(spa_config_parse(spa
, &vd
, spares
[i
], NULL
, 0,
1483 VDEV_ALLOC_SPARE
) == 0);
1486 spa
->spa_spares
.sav_vdevs
[i
] = vd
;
1488 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1489 B_FALSE
)) != NULL
) {
1490 if (!tvd
->vdev_isspare
)
1494 * We only mark the spare active if we were successfully
1495 * able to load the vdev. Otherwise, importing a pool
1496 * with a bad active spare would result in strange
1497 * behavior, because multiple pool would think the spare
1498 * is actively in use.
1500 * There is a vulnerability here to an equally bizarre
1501 * circumstance, where a dead active spare is later
1502 * brought back to life (onlined or otherwise). Given
1503 * the rarity of this scenario, and the extra complexity
1504 * it adds, we ignore the possibility.
1506 if (!vdev_is_dead(tvd
))
1507 spa_spare_activate(tvd
);
1511 vd
->vdev_aux
= &spa
->spa_spares
;
1513 if (vdev_open(vd
) != 0)
1516 if (vdev_validate_aux(vd
) == 0)
1521 * Recompute the stashed list of spares, with status information
1524 VERIFY(nvlist_remove(spa
->spa_spares
.sav_config
, ZPOOL_CONFIG_SPARES
,
1525 DATA_TYPE_NVLIST_ARRAY
) == 0);
1527 spares
= kmem_alloc(spa
->spa_spares
.sav_count
* sizeof (void *),
1529 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1530 spares
[i
] = vdev_config_generate(spa
,
1531 spa
->spa_spares
.sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_SPARE
);
1532 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
1533 ZPOOL_CONFIG_SPARES
, spares
, spa
->spa_spares
.sav_count
) == 0);
1534 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1535 nvlist_free(spares
[i
]);
1536 kmem_free(spares
, spa
->spa_spares
.sav_count
* sizeof (void *));
1540 * Load (or re-load) the current list of vdevs describing the active l2cache for
1541 * this pool. When this is called, we have some form of basic information in
1542 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1543 * then re-generate a more complete list including status information.
1544 * Devices which are already active have their details maintained, and are
1548 spa_load_l2cache(spa_t
*spa
)
1552 int i
, j
, oldnvdevs
;
1554 vdev_t
*vd
, **oldvdevs
, **newvdevs
;
1555 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
1557 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1559 oldvdevs
= sav
->sav_vdevs
;
1560 oldnvdevs
= sav
->sav_count
;
1561 sav
->sav_vdevs
= NULL
;
1564 if (sav
->sav_config
== NULL
) {
1570 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
,
1571 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
1572 newvdevs
= kmem_alloc(nl2cache
* sizeof (void *), KM_SLEEP
);
1575 * Process new nvlist of vdevs.
1577 for (i
= 0; i
< nl2cache
; i
++) {
1578 VERIFY(nvlist_lookup_uint64(l2cache
[i
], ZPOOL_CONFIG_GUID
,
1582 for (j
= 0; j
< oldnvdevs
; j
++) {
1584 if (vd
!= NULL
&& guid
== vd
->vdev_guid
) {
1586 * Retain previous vdev for add/remove ops.
1594 if (newvdevs
[i
] == NULL
) {
1598 VERIFY(spa_config_parse(spa
, &vd
, l2cache
[i
], NULL
, 0,
1599 VDEV_ALLOC_L2CACHE
) == 0);
1604 * Commit this vdev as an l2cache device,
1605 * even if it fails to open.
1607 spa_l2cache_add(vd
);
1612 spa_l2cache_activate(vd
);
1614 if (vdev_open(vd
) != 0)
1617 (void) vdev_validate_aux(vd
);
1619 if (!vdev_is_dead(vd
))
1620 l2arc_add_vdev(spa
, vd
);
1624 sav
->sav_vdevs
= newvdevs
;
1625 sav
->sav_count
= (int)nl2cache
;
1628 * Recompute the stashed list of l2cache devices, with status
1629 * information this time.
1631 VERIFY(nvlist_remove(sav
->sav_config
, ZPOOL_CONFIG_L2CACHE
,
1632 DATA_TYPE_NVLIST_ARRAY
) == 0);
1634 l2cache
= kmem_alloc(sav
->sav_count
* sizeof (void *), KM_SLEEP
);
1635 for (i
= 0; i
< sav
->sav_count
; i
++)
1636 l2cache
[i
] = vdev_config_generate(spa
,
1637 sav
->sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_L2CACHE
);
1638 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
,
1639 ZPOOL_CONFIG_L2CACHE
, l2cache
, sav
->sav_count
) == 0);
1643 * Purge vdevs that were dropped
1645 for (i
= 0; i
< oldnvdevs
; i
++) {
1650 ASSERT(vd
->vdev_isl2cache
);
1652 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
1653 pool
!= 0ULL && l2arc_vdev_present(vd
))
1654 l2arc_remove_vdev(vd
);
1655 vdev_clear_stats(vd
);
1661 kmem_free(oldvdevs
, oldnvdevs
* sizeof (void *));
1663 for (i
= 0; i
< sav
->sav_count
; i
++)
1664 nvlist_free(l2cache
[i
]);
1666 kmem_free(l2cache
, sav
->sav_count
* sizeof (void *));
1670 load_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
**value
)
1673 char *packed
= NULL
;
1678 error
= dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
);
1682 nvsize
= *(uint64_t *)db
->db_data
;
1683 dmu_buf_rele(db
, FTAG
);
1685 packed
= vmem_alloc(nvsize
, KM_SLEEP
);
1686 error
= dmu_read(spa
->spa_meta_objset
, obj
, 0, nvsize
, packed
,
1689 error
= nvlist_unpack(packed
, nvsize
, value
, 0);
1690 vmem_free(packed
, nvsize
);
1696 * Checks to see if the given vdev could not be opened, in which case we post a
1697 * sysevent to notify the autoreplace code that the device has been removed.
1700 spa_check_removed(vdev_t
*vd
)
1704 for (c
= 0; c
< vd
->vdev_children
; c
++)
1705 spa_check_removed(vd
->vdev_child
[c
]);
1707 if (vd
->vdev_ops
->vdev_op_leaf
&& vdev_is_dead(vd
) &&
1709 zfs_post_autoreplace(vd
->vdev_spa
, vd
);
1710 spa_event_notify(vd
->vdev_spa
, vd
, NULL
, ESC_ZFS_VDEV_CHECK
);
1715 spa_config_valid_zaps(vdev_t
*vd
, vdev_t
*mvd
)
1719 ASSERT3U(vd
->vdev_children
, ==, mvd
->vdev_children
);
1721 vd
->vdev_top_zap
= mvd
->vdev_top_zap
;
1722 vd
->vdev_leaf_zap
= mvd
->vdev_leaf_zap
;
1724 for (i
= 0; i
< vd
->vdev_children
; i
++) {
1725 spa_config_valid_zaps(vd
->vdev_child
[i
], mvd
->vdev_child
[i
]);
1730 * Validate the current config against the MOS config
1733 spa_config_valid(spa_t
*spa
, nvlist_t
*config
)
1735 vdev_t
*mrvd
, *rvd
= spa
->spa_root_vdev
;
1739 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nv
) == 0);
1741 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1742 VERIFY(spa_config_parse(spa
, &mrvd
, nv
, NULL
, 0, VDEV_ALLOC_LOAD
) == 0);
1744 ASSERT3U(rvd
->vdev_children
, ==, mrvd
->vdev_children
);
1747 * If we're doing a normal import, then build up any additional
1748 * diagnostic information about missing devices in this config.
1749 * We'll pass this up to the user for further processing.
1751 if (!(spa
->spa_import_flags
& ZFS_IMPORT_MISSING_LOG
)) {
1752 nvlist_t
**child
, *nv
;
1755 child
= kmem_alloc(rvd
->vdev_children
* sizeof (nvlist_t
*),
1757 VERIFY(nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
1759 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1760 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1761 vdev_t
*mtvd
= mrvd
->vdev_child
[c
];
1763 if (tvd
->vdev_ops
== &vdev_missing_ops
&&
1764 mtvd
->vdev_ops
!= &vdev_missing_ops
&&
1766 child
[idx
++] = vdev_config_generate(spa
, mtvd
,
1771 VERIFY(nvlist_add_nvlist_array(nv
,
1772 ZPOOL_CONFIG_CHILDREN
, child
, idx
) == 0);
1773 VERIFY(nvlist_add_nvlist(spa
->spa_load_info
,
1774 ZPOOL_CONFIG_MISSING_DEVICES
, nv
) == 0);
1776 for (i
= 0; i
< idx
; i
++)
1777 nvlist_free(child
[i
]);
1780 kmem_free(child
, rvd
->vdev_children
* sizeof (char **));
1784 * Compare the root vdev tree with the information we have
1785 * from the MOS config (mrvd). Check each top-level vdev
1786 * with the corresponding MOS config top-level (mtvd).
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
];
1793 * Resolve any "missing" vdevs in the current configuration.
1794 * If we find that the MOS config has more accurate information
1795 * about the top-level vdev then use that vdev instead.
1797 if (tvd
->vdev_ops
== &vdev_missing_ops
&&
1798 mtvd
->vdev_ops
!= &vdev_missing_ops
) {
1800 if (!(spa
->spa_import_flags
& ZFS_IMPORT_MISSING_LOG
))
1804 * Device specific actions.
1806 if (mtvd
->vdev_islog
) {
1807 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
1810 * XXX - once we have 'readonly' pool
1811 * support we should be able to handle
1812 * missing data devices by transitioning
1813 * the pool to readonly.
1819 * Swap the missing vdev with the data we were
1820 * able to obtain from the MOS config.
1822 vdev_remove_child(rvd
, tvd
);
1823 vdev_remove_child(mrvd
, mtvd
);
1825 vdev_add_child(rvd
, mtvd
);
1826 vdev_add_child(mrvd
, tvd
);
1828 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1830 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1834 if (mtvd
->vdev_islog
) {
1836 * Load the slog device's state from the MOS
1837 * config since it's possible that the label
1838 * does not contain the most up-to-date
1841 vdev_load_log_state(tvd
, mtvd
);
1846 * Per-vdev ZAP info is stored exclusively in the MOS.
1848 spa_config_valid_zaps(tvd
, mtvd
);
1853 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1856 * Ensure we were able to validate the config.
1858 return (rvd
->vdev_guid_sum
== spa
->spa_uberblock
.ub_guid_sum
);
1862 * Check for missing log devices
1865 spa_check_logs(spa_t
*spa
)
1867 boolean_t rv
= B_FALSE
;
1868 dsl_pool_t
*dp
= spa_get_dsl(spa
);
1870 switch (spa
->spa_log_state
) {
1873 case SPA_LOG_MISSING
:
1874 /* need to recheck in case slog has been restored */
1875 case SPA_LOG_UNKNOWN
:
1876 rv
= (dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
1877 zil_check_log_chain
, NULL
, DS_FIND_CHILDREN
) != 0);
1879 spa_set_log_state(spa
, SPA_LOG_MISSING
);
1886 spa_passivate_log(spa_t
*spa
)
1888 vdev_t
*rvd
= spa
->spa_root_vdev
;
1889 boolean_t slog_found
= B_FALSE
;
1892 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
1894 if (!spa_has_slogs(spa
))
1897 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1898 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1899 metaslab_group_t
*mg
= tvd
->vdev_mg
;
1901 if (tvd
->vdev_islog
) {
1902 metaslab_group_passivate(mg
);
1903 slog_found
= B_TRUE
;
1907 return (slog_found
);
1911 spa_activate_log(spa_t
*spa
)
1913 vdev_t
*rvd
= spa
->spa_root_vdev
;
1916 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
1918 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1919 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1920 metaslab_group_t
*mg
= tvd
->vdev_mg
;
1922 if (tvd
->vdev_islog
)
1923 metaslab_group_activate(mg
);
1928 spa_offline_log(spa_t
*spa
)
1932 error
= dmu_objset_find(spa_name(spa
), zil_vdev_offline
,
1933 NULL
, DS_FIND_CHILDREN
);
1936 * We successfully offlined the log device, sync out the
1937 * current txg so that the "stubby" block can be removed
1940 txg_wait_synced(spa
->spa_dsl_pool
, 0);
1946 spa_aux_check_removed(spa_aux_vdev_t
*sav
)
1950 for (i
= 0; i
< sav
->sav_count
; i
++)
1951 spa_check_removed(sav
->sav_vdevs
[i
]);
1955 spa_claim_notify(zio_t
*zio
)
1957 spa_t
*spa
= zio
->io_spa
;
1962 mutex_enter(&spa
->spa_props_lock
); /* any mutex will do */
1963 if (spa
->spa_claim_max_txg
< zio
->io_bp
->blk_birth
)
1964 spa
->spa_claim_max_txg
= zio
->io_bp
->blk_birth
;
1965 mutex_exit(&spa
->spa_props_lock
);
1968 typedef struct spa_load_error
{
1969 uint64_t sle_meta_count
;
1970 uint64_t sle_data_count
;
1974 spa_load_verify_done(zio_t
*zio
)
1976 blkptr_t
*bp
= zio
->io_bp
;
1977 spa_load_error_t
*sle
= zio
->io_private
;
1978 dmu_object_type_t type
= BP_GET_TYPE(bp
);
1979 int error
= zio
->io_error
;
1980 spa_t
*spa
= zio
->io_spa
;
1982 abd_free(zio
->io_abd
);
1984 if ((BP_GET_LEVEL(bp
) != 0 || DMU_OT_IS_METADATA(type
)) &&
1985 type
!= DMU_OT_INTENT_LOG
)
1986 atomic_inc_64(&sle
->sle_meta_count
);
1988 atomic_inc_64(&sle
->sle_data_count
);
1991 mutex_enter(&spa
->spa_scrub_lock
);
1992 spa
->spa_scrub_inflight
--;
1993 cv_broadcast(&spa
->spa_scrub_io_cv
);
1994 mutex_exit(&spa
->spa_scrub_lock
);
1998 * Maximum number of concurrent scrub i/os to create while verifying
1999 * a pool while importing it.
2001 int spa_load_verify_maxinflight
= 10000;
2002 int spa_load_verify_metadata
= B_TRUE
;
2003 int spa_load_verify_data
= B_TRUE
;
2007 spa_load_verify_cb(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
2008 const zbookmark_phys_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
2013 if (bp
== NULL
|| BP_IS_HOLE(bp
) || BP_IS_EMBEDDED(bp
))
2016 * Note: normally this routine will not be called if
2017 * spa_load_verify_metadata is not set. However, it may be useful
2018 * to manually set the flag after the traversal has begun.
2020 if (!spa_load_verify_metadata
)
2022 if (!BP_IS_METADATA(bp
) && !spa_load_verify_data
)
2026 size
= BP_GET_PSIZE(bp
);
2028 mutex_enter(&spa
->spa_scrub_lock
);
2029 while (spa
->spa_scrub_inflight
>= spa_load_verify_maxinflight
)
2030 cv_wait(&spa
->spa_scrub_io_cv
, &spa
->spa_scrub_lock
);
2031 spa
->spa_scrub_inflight
++;
2032 mutex_exit(&spa
->spa_scrub_lock
);
2034 zio_nowait(zio_read(rio
, spa
, bp
, abd_alloc_for_io(size
, B_FALSE
), size
,
2035 spa_load_verify_done
, rio
->io_private
, ZIO_PRIORITY_SCRUB
,
2036 ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_CANFAIL
|
2037 ZIO_FLAG_SCRUB
| ZIO_FLAG_RAW
, zb
));
2043 verify_dataset_name_len(dsl_pool_t
*dp
, dsl_dataset_t
*ds
, void *arg
)
2045 if (dsl_dataset_namelen(ds
) >= ZFS_MAX_DATASET_NAME_LEN
)
2046 return (SET_ERROR(ENAMETOOLONG
));
2052 spa_load_verify(spa_t
*spa
)
2055 spa_load_error_t sle
= { 0 };
2056 zpool_rewind_policy_t policy
;
2057 boolean_t verify_ok
= B_FALSE
;
2060 zpool_get_rewind_policy(spa
->spa_config
, &policy
);
2062 if (policy
.zrp_request
& ZPOOL_NEVER_REWIND
)
2065 dsl_pool_config_enter(spa
->spa_dsl_pool
, FTAG
);
2066 error
= dmu_objset_find_dp(spa
->spa_dsl_pool
,
2067 spa
->spa_dsl_pool
->dp_root_dir_obj
, verify_dataset_name_len
, NULL
,
2069 dsl_pool_config_exit(spa
->spa_dsl_pool
, FTAG
);
2073 rio
= zio_root(spa
, NULL
, &sle
,
2074 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
);
2076 if (spa_load_verify_metadata
) {
2077 error
= traverse_pool(spa
, spa
->spa_verify_min_txg
,
2078 TRAVERSE_PRE
| TRAVERSE_PREFETCH_METADATA
,
2079 spa_load_verify_cb
, rio
);
2082 (void) zio_wait(rio
);
2084 spa
->spa_load_meta_errors
= sle
.sle_meta_count
;
2085 spa
->spa_load_data_errors
= sle
.sle_data_count
;
2087 if (!error
&& sle
.sle_meta_count
<= policy
.zrp_maxmeta
&&
2088 sle
.sle_data_count
<= policy
.zrp_maxdata
) {
2092 spa
->spa_load_txg
= spa
->spa_uberblock
.ub_txg
;
2093 spa
->spa_load_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
2095 loss
= spa
->spa_last_ubsync_txg_ts
- spa
->spa_load_txg_ts
;
2096 VERIFY(nvlist_add_uint64(spa
->spa_load_info
,
2097 ZPOOL_CONFIG_LOAD_TIME
, spa
->spa_load_txg_ts
) == 0);
2098 VERIFY(nvlist_add_int64(spa
->spa_load_info
,
2099 ZPOOL_CONFIG_REWIND_TIME
, loss
) == 0);
2100 VERIFY(nvlist_add_uint64(spa
->spa_load_info
,
2101 ZPOOL_CONFIG_LOAD_DATA_ERRORS
, sle
.sle_data_count
) == 0);
2103 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
;
2107 if (error
!= ENXIO
&& error
!= EIO
)
2108 error
= SET_ERROR(EIO
);
2112 return (verify_ok
? 0 : EIO
);
2116 * Find a value in the pool props object.
2119 spa_prop_find(spa_t
*spa
, zpool_prop_t prop
, uint64_t *val
)
2121 (void) zap_lookup(spa
->spa_meta_objset
, spa
->spa_pool_props_object
,
2122 zpool_prop_to_name(prop
), sizeof (uint64_t), 1, val
);
2126 * Find a value in the pool directory object.
2129 spa_dir_prop(spa_t
*spa
, const char *name
, uint64_t *val
)
2131 return (zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
2132 name
, sizeof (uint64_t), 1, val
));
2136 spa_vdev_err(vdev_t
*vdev
, vdev_aux_t aux
, int err
)
2138 vdev_set_state(vdev
, B_TRUE
, VDEV_STATE_CANT_OPEN
, aux
);
2143 * Fix up config after a partly-completed split. This is done with the
2144 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
2145 * pool have that entry in their config, but only the splitting one contains
2146 * a list of all the guids of the vdevs that are being split off.
2148 * This function determines what to do with that list: either rejoin
2149 * all the disks to the pool, or complete the splitting process. To attempt
2150 * the rejoin, each disk that is offlined is marked online again, and
2151 * we do a reopen() call. If the vdev label for every disk that was
2152 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
2153 * then we call vdev_split() on each disk, and complete the split.
2155 * Otherwise we leave the config alone, with all the vdevs in place in
2156 * the original pool.
2159 spa_try_repair(spa_t
*spa
, nvlist_t
*config
)
2166 boolean_t attempt_reopen
;
2168 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
, &nvl
) != 0)
2171 /* check that the config is complete */
2172 if (nvlist_lookup_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
2173 &glist
, &gcount
) != 0)
2176 vd
= kmem_zalloc(gcount
* sizeof (vdev_t
*), KM_SLEEP
);
2178 /* attempt to online all the vdevs & validate */
2179 attempt_reopen
= B_TRUE
;
2180 for (i
= 0; i
< gcount
; i
++) {
2181 if (glist
[i
] == 0) /* vdev is hole */
2184 vd
[i
] = spa_lookup_by_guid(spa
, glist
[i
], B_FALSE
);
2185 if (vd
[i
] == NULL
) {
2187 * Don't bother attempting to reopen the disks;
2188 * just do the split.
2190 attempt_reopen
= B_FALSE
;
2192 /* attempt to re-online it */
2193 vd
[i
]->vdev_offline
= B_FALSE
;
2197 if (attempt_reopen
) {
2198 vdev_reopen(spa
->spa_root_vdev
);
2200 /* check each device to see what state it's in */
2201 for (extracted
= 0, i
= 0; i
< gcount
; i
++) {
2202 if (vd
[i
] != NULL
&&
2203 vd
[i
]->vdev_stat
.vs_aux
!= VDEV_AUX_SPLIT_POOL
)
2210 * If every disk has been moved to the new pool, or if we never
2211 * even attempted to look at them, then we split them off for
2214 if (!attempt_reopen
|| gcount
== extracted
) {
2215 for (i
= 0; i
< gcount
; i
++)
2218 vdev_reopen(spa
->spa_root_vdev
);
2221 kmem_free(vd
, gcount
* sizeof (vdev_t
*));
2225 spa_load(spa_t
*spa
, spa_load_state_t state
, spa_import_type_t type
,
2226 boolean_t mosconfig
)
2228 nvlist_t
*config
= spa
->spa_config
;
2229 char *ereport
= FM_EREPORT_ZFS_POOL
;
2235 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
, &pool_guid
))
2236 return (SET_ERROR(EINVAL
));
2238 ASSERT(spa
->spa_comment
== NULL
);
2239 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
2240 spa
->spa_comment
= spa_strdup(comment
);
2243 * Versioning wasn't explicitly added to the label until later, so if
2244 * it's not present treat it as the initial version.
2246 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
2247 &spa
->spa_ubsync
.ub_version
) != 0)
2248 spa
->spa_ubsync
.ub_version
= SPA_VERSION_INITIAL
;
2250 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
2251 &spa
->spa_config_txg
);
2253 if ((state
== SPA_LOAD_IMPORT
|| state
== SPA_LOAD_TRYIMPORT
) &&
2254 spa_guid_exists(pool_guid
, 0)) {
2255 error
= SET_ERROR(EEXIST
);
2257 spa
->spa_config_guid
= pool_guid
;
2259 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
,
2261 VERIFY(nvlist_dup(nvl
, &spa
->spa_config_splitting
,
2265 nvlist_free(spa
->spa_load_info
);
2266 spa
->spa_load_info
= fnvlist_alloc();
2268 gethrestime(&spa
->spa_loaded_ts
);
2269 error
= spa_load_impl(spa
, pool_guid
, config
, state
, type
,
2270 mosconfig
, &ereport
);
2274 * Don't count references from objsets that are already closed
2275 * and are making their way through the eviction process.
2277 spa_evicting_os_wait(spa
);
2278 spa
->spa_minref
= refcount_count(&spa
->spa_refcount
);
2280 if (error
!= EEXIST
) {
2281 spa
->spa_loaded_ts
.tv_sec
= 0;
2282 spa
->spa_loaded_ts
.tv_nsec
= 0;
2284 if (error
!= EBADF
) {
2285 zfs_ereport_post(ereport
, spa
, NULL
, NULL
, 0, 0);
2288 spa
->spa_load_state
= error
? SPA_LOAD_ERROR
: SPA_LOAD_NONE
;
2296 * Count the number of per-vdev ZAPs associated with all of the vdevs in the
2297 * vdev tree rooted in the given vd, and ensure that each ZAP is present in the
2298 * spa's per-vdev ZAP list.
2301 vdev_count_verify_zaps(vdev_t
*vd
)
2303 spa_t
*spa
= vd
->vdev_spa
;
2307 if (vd
->vdev_top_zap
!= 0) {
2309 ASSERT0(zap_lookup_int(spa
->spa_meta_objset
,
2310 spa
->spa_all_vdev_zaps
, vd
->vdev_top_zap
));
2312 if (vd
->vdev_leaf_zap
!= 0) {
2314 ASSERT0(zap_lookup_int(spa
->spa_meta_objset
,
2315 spa
->spa_all_vdev_zaps
, vd
->vdev_leaf_zap
));
2318 for (i
= 0; i
< vd
->vdev_children
; i
++) {
2319 total
+= vdev_count_verify_zaps(vd
->vdev_child
[i
]);
2327 * Load an existing storage pool, using the pool's builtin spa_config as a
2328 * source of configuration information.
2330 __attribute__((always_inline
))
2332 spa_load_impl(spa_t
*spa
, uint64_t pool_guid
, nvlist_t
*config
,
2333 spa_load_state_t state
, spa_import_type_t type
, boolean_t mosconfig
,
2337 nvlist_t
*nvroot
= NULL
;
2340 uberblock_t
*ub
= &spa
->spa_uberblock
;
2341 uint64_t children
, config_cache_txg
= spa
->spa_config_txg
;
2342 int orig_mode
= spa
->spa_mode
;
2345 boolean_t missing_feat_write
= B_FALSE
;
2346 nvlist_t
*mos_config
;
2349 * If this is an untrusted config, access the pool in read-only mode.
2350 * This prevents things like resilvering recently removed devices.
2353 spa
->spa_mode
= FREAD
;
2355 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
2357 spa
->spa_load_state
= state
;
2359 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvroot
))
2360 return (SET_ERROR(EINVAL
));
2362 parse
= (type
== SPA_IMPORT_EXISTING
?
2363 VDEV_ALLOC_LOAD
: VDEV_ALLOC_SPLIT
);
2366 * Create "The Godfather" zio to hold all async IOs
2368 spa
->spa_async_zio_root
= kmem_alloc(max_ncpus
* sizeof (void *),
2370 for (i
= 0; i
< max_ncpus
; i
++) {
2371 spa
->spa_async_zio_root
[i
] = zio_root(spa
, NULL
, NULL
,
2372 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
2373 ZIO_FLAG_GODFATHER
);
2377 * Parse the configuration into a vdev tree. We explicitly set the
2378 * value that will be returned by spa_version() since parsing the
2379 * configuration requires knowing the version number.
2381 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2382 error
= spa_config_parse(spa
, &rvd
, nvroot
, NULL
, 0, parse
);
2383 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2388 ASSERT(spa
->spa_root_vdev
== rvd
);
2389 ASSERT3U(spa
->spa_min_ashift
, >=, SPA_MINBLOCKSHIFT
);
2390 ASSERT3U(spa
->spa_max_ashift
, <=, SPA_MAXBLOCKSHIFT
);
2392 if (type
!= SPA_IMPORT_ASSEMBLE
) {
2393 ASSERT(spa_guid(spa
) == pool_guid
);
2397 * Try to open all vdevs, loading each label in the process.
2399 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2400 error
= vdev_open(rvd
);
2401 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2406 * We need to validate the vdev labels against the configuration that
2407 * we have in hand, which is dependent on the setting of mosconfig. If
2408 * mosconfig is true then we're validating the vdev labels based on
2409 * that config. Otherwise, we're validating against the cached config
2410 * (zpool.cache) that was read when we loaded the zfs module, and then
2411 * later we will recursively call spa_load() and validate against
2414 * If we're assembling a new pool that's been split off from an
2415 * existing pool, the labels haven't yet been updated so we skip
2416 * validation for now.
2418 if (type
!= SPA_IMPORT_ASSEMBLE
) {
2419 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2420 error
= vdev_validate(rvd
, mosconfig
);
2421 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2426 if (rvd
->vdev_state
<= VDEV_STATE_CANT_OPEN
)
2427 return (SET_ERROR(ENXIO
));
2431 * Find the best uberblock.
2433 vdev_uberblock_load(rvd
, ub
, &label
);
2436 * If we weren't able to find a single valid uberblock, return failure.
2438 if (ub
->ub_txg
== 0) {
2440 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, ENXIO
));
2444 * If the pool has an unsupported version we can't open it.
2446 if (!SPA_VERSION_IS_SUPPORTED(ub
->ub_version
)) {
2448 return (spa_vdev_err(rvd
, VDEV_AUX_VERSION_NEWER
, ENOTSUP
));
2451 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
2455 * If we weren't able to find what's necessary for reading the
2456 * MOS in the label, return failure.
2458 if (label
== NULL
|| nvlist_lookup_nvlist(label
,
2459 ZPOOL_CONFIG_FEATURES_FOR_READ
, &features
) != 0) {
2461 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
2466 * Update our in-core representation with the definitive values
2469 nvlist_free(spa
->spa_label_features
);
2470 VERIFY(nvlist_dup(features
, &spa
->spa_label_features
, 0) == 0);
2476 * Look through entries in the label nvlist's features_for_read. If
2477 * there is a feature listed there which we don't understand then we
2478 * cannot open a pool.
2480 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
2481 nvlist_t
*unsup_feat
;
2484 VERIFY(nvlist_alloc(&unsup_feat
, NV_UNIQUE_NAME
, KM_SLEEP
) ==
2487 for (nvp
= nvlist_next_nvpair(spa
->spa_label_features
, NULL
);
2489 nvp
= nvlist_next_nvpair(spa
->spa_label_features
, nvp
)) {
2490 if (!zfeature_is_supported(nvpair_name(nvp
))) {
2491 VERIFY(nvlist_add_string(unsup_feat
,
2492 nvpair_name(nvp
), "") == 0);
2496 if (!nvlist_empty(unsup_feat
)) {
2497 VERIFY(nvlist_add_nvlist(spa
->spa_load_info
,
2498 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
) == 0);
2499 nvlist_free(unsup_feat
);
2500 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
2504 nvlist_free(unsup_feat
);
2508 * If the vdev guid sum doesn't match the uberblock, we have an
2509 * incomplete configuration. We first check to see if the pool
2510 * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN).
2511 * If it is, defer the vdev_guid_sum check till later so we
2512 * can handle missing vdevs.
2514 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VDEV_CHILDREN
,
2515 &children
) != 0 && mosconfig
&& type
!= SPA_IMPORT_ASSEMBLE
&&
2516 rvd
->vdev_guid_sum
!= ub
->ub_guid_sum
)
2517 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
, ENXIO
));
2519 if (type
!= SPA_IMPORT_ASSEMBLE
&& spa
->spa_config_splitting
) {
2520 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2521 spa_try_repair(spa
, config
);
2522 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2523 nvlist_free(spa
->spa_config_splitting
);
2524 spa
->spa_config_splitting
= NULL
;
2528 * Initialize internal SPA structures.
2530 spa
->spa_state
= POOL_STATE_ACTIVE
;
2531 spa
->spa_ubsync
= spa
->spa_uberblock
;
2532 spa
->spa_verify_min_txg
= spa
->spa_extreme_rewind
?
2533 TXG_INITIAL
- 1 : spa_last_synced_txg(spa
) - TXG_DEFER_SIZE
- 1;
2534 spa
->spa_first_txg
= spa
->spa_last_ubsync_txg
?
2535 spa
->spa_last_ubsync_txg
: spa_last_synced_txg(spa
) + 1;
2536 spa
->spa_claim_max_txg
= spa
->spa_first_txg
;
2537 spa
->spa_prev_software_version
= ub
->ub_software_version
;
2539 error
= dsl_pool_init(spa
, spa
->spa_first_txg
, &spa
->spa_dsl_pool
);
2541 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2542 spa
->spa_meta_objset
= spa
->spa_dsl_pool
->dp_meta_objset
;
2544 if (spa_dir_prop(spa
, DMU_POOL_CONFIG
, &spa
->spa_config_object
) != 0)
2545 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2547 if (spa_version(spa
) >= SPA_VERSION_FEATURES
) {
2548 boolean_t missing_feat_read
= B_FALSE
;
2549 nvlist_t
*unsup_feat
, *enabled_feat
;
2552 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_READ
,
2553 &spa
->spa_feat_for_read_obj
) != 0) {
2554 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2557 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_WRITE
,
2558 &spa
->spa_feat_for_write_obj
) != 0) {
2559 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2562 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_DESCRIPTIONS
,
2563 &spa
->spa_feat_desc_obj
) != 0) {
2564 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2567 enabled_feat
= fnvlist_alloc();
2568 unsup_feat
= fnvlist_alloc();
2570 if (!spa_features_check(spa
, B_FALSE
,
2571 unsup_feat
, enabled_feat
))
2572 missing_feat_read
= B_TRUE
;
2574 if (spa_writeable(spa
) || state
== SPA_LOAD_TRYIMPORT
) {
2575 if (!spa_features_check(spa
, B_TRUE
,
2576 unsup_feat
, enabled_feat
)) {
2577 missing_feat_write
= B_TRUE
;
2581 fnvlist_add_nvlist(spa
->spa_load_info
,
2582 ZPOOL_CONFIG_ENABLED_FEAT
, enabled_feat
);
2584 if (!nvlist_empty(unsup_feat
)) {
2585 fnvlist_add_nvlist(spa
->spa_load_info
,
2586 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
);
2589 fnvlist_free(enabled_feat
);
2590 fnvlist_free(unsup_feat
);
2592 if (!missing_feat_read
) {
2593 fnvlist_add_boolean(spa
->spa_load_info
,
2594 ZPOOL_CONFIG_CAN_RDONLY
);
2598 * If the state is SPA_LOAD_TRYIMPORT, our objective is
2599 * twofold: to determine whether the pool is available for
2600 * import in read-write mode and (if it is not) whether the
2601 * pool is available for import in read-only mode. If the pool
2602 * is available for import in read-write mode, it is displayed
2603 * as available in userland; if it is not available for import
2604 * in read-only mode, it is displayed as unavailable in
2605 * userland. If the pool is available for import in read-only
2606 * mode but not read-write mode, it is displayed as unavailable
2607 * in userland with a special note that the pool is actually
2608 * available for open in read-only mode.
2610 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
2611 * missing a feature for write, we must first determine whether
2612 * the pool can be opened read-only before returning to
2613 * userland in order to know whether to display the
2614 * abovementioned note.
2616 if (missing_feat_read
|| (missing_feat_write
&&
2617 spa_writeable(spa
))) {
2618 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
2623 * Load refcounts for ZFS features from disk into an in-memory
2624 * cache during SPA initialization.
2626 for (i
= 0; i
< SPA_FEATURES
; i
++) {
2629 error
= feature_get_refcount_from_disk(spa
,
2630 &spa_feature_table
[i
], &refcount
);
2632 spa
->spa_feat_refcount_cache
[i
] = refcount
;
2633 } else if (error
== ENOTSUP
) {
2634 spa
->spa_feat_refcount_cache
[i
] =
2635 SPA_FEATURE_DISABLED
;
2637 return (spa_vdev_err(rvd
,
2638 VDEV_AUX_CORRUPT_DATA
, EIO
));
2643 if (spa_feature_is_active(spa
, SPA_FEATURE_ENABLED_TXG
)) {
2644 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_ENABLED_TXG
,
2645 &spa
->spa_feat_enabled_txg_obj
) != 0)
2646 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2649 spa
->spa_is_initializing
= B_TRUE
;
2650 error
= dsl_pool_open(spa
->spa_dsl_pool
);
2651 spa
->spa_is_initializing
= B_FALSE
;
2653 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2657 nvlist_t
*policy
= NULL
, *nvconfig
;
2659 if (load_nvlist(spa
, spa
->spa_config_object
, &nvconfig
) != 0)
2660 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2662 if (!spa_is_root(spa
) && nvlist_lookup_uint64(nvconfig
,
2663 ZPOOL_CONFIG_HOSTID
, &hostid
) == 0) {
2665 unsigned long myhostid
= 0;
2667 VERIFY(nvlist_lookup_string(nvconfig
,
2668 ZPOOL_CONFIG_HOSTNAME
, &hostname
) == 0);
2671 myhostid
= zone_get_hostid(NULL
);
2674 * We're emulating the system's hostid in userland, so
2675 * we can't use zone_get_hostid().
2677 (void) ddi_strtoul(hw_serial
, NULL
, 10, &myhostid
);
2678 #endif /* _KERNEL */
2679 if (hostid
!= 0 && myhostid
!= 0 &&
2680 hostid
!= myhostid
) {
2681 nvlist_free(nvconfig
);
2682 cmn_err(CE_WARN
, "pool '%s' could not be "
2683 "loaded as it was last accessed by another "
2684 "system (host: %s hostid: 0x%lx). See: "
2685 "http://zfsonlinux.org/msg/ZFS-8000-EY",
2686 spa_name(spa
), hostname
,
2687 (unsigned long)hostid
);
2688 return (SET_ERROR(EBADF
));
2691 if (nvlist_lookup_nvlist(spa
->spa_config
,
2692 ZPOOL_REWIND_POLICY
, &policy
) == 0)
2693 VERIFY(nvlist_add_nvlist(nvconfig
,
2694 ZPOOL_REWIND_POLICY
, policy
) == 0);
2696 spa_config_set(spa
, nvconfig
);
2698 spa_deactivate(spa
);
2699 spa_activate(spa
, orig_mode
);
2701 return (spa_load(spa
, state
, SPA_IMPORT_EXISTING
, B_TRUE
));
2704 /* Grab the checksum salt from the MOS. */
2705 error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
2706 DMU_POOL_CHECKSUM_SALT
, 1,
2707 sizeof (spa
->spa_cksum_salt
.zcs_bytes
),
2708 spa
->spa_cksum_salt
.zcs_bytes
);
2709 if (error
== ENOENT
) {
2710 /* Generate a new salt for subsequent use */
2711 (void) random_get_pseudo_bytes(spa
->spa_cksum_salt
.zcs_bytes
,
2712 sizeof (spa
->spa_cksum_salt
.zcs_bytes
));
2713 } else if (error
!= 0) {
2714 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2717 if (spa_dir_prop(spa
, DMU_POOL_SYNC_BPOBJ
, &obj
) != 0)
2718 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2719 error
= bpobj_open(&spa
->spa_deferred_bpobj
, spa
->spa_meta_objset
, obj
);
2721 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2724 * Load the bit that tells us to use the new accounting function
2725 * (raid-z deflation). If we have an older pool, this will not
2728 error
= spa_dir_prop(spa
, DMU_POOL_DEFLATE
, &spa
->spa_deflate
);
2729 if (error
!= 0 && error
!= ENOENT
)
2730 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2732 error
= spa_dir_prop(spa
, DMU_POOL_CREATION_VERSION
,
2733 &spa
->spa_creation_version
);
2734 if (error
!= 0 && error
!= ENOENT
)
2735 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2738 * Load the persistent error log. If we have an older pool, this will
2741 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_LAST
, &spa
->spa_errlog_last
);
2742 if (error
!= 0 && error
!= ENOENT
)
2743 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2745 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_SCRUB
,
2746 &spa
->spa_errlog_scrub
);
2747 if (error
!= 0 && error
!= ENOENT
)
2748 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2751 * Load the history object. If we have an older pool, this
2752 * will not be present.
2754 error
= spa_dir_prop(spa
, DMU_POOL_HISTORY
, &spa
->spa_history
);
2755 if (error
!= 0 && error
!= ENOENT
)
2756 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2759 * Load the per-vdev ZAP map. If we have an older pool, this will not
2760 * be present; in this case, defer its creation to a later time to
2761 * avoid dirtying the MOS this early / out of sync context. See
2762 * spa_sync_config_object.
2765 /* The sentinel is only available in the MOS config. */
2766 if (load_nvlist(spa
, spa
->spa_config_object
, &mos_config
) != 0)
2767 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2769 error
= spa_dir_prop(spa
, DMU_POOL_VDEV_ZAP_MAP
,
2770 &spa
->spa_all_vdev_zaps
);
2772 if (error
== ENOENT
) {
2773 VERIFY(!nvlist_exists(mos_config
,
2774 ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
));
2775 spa
->spa_avz_action
= AVZ_ACTION_INITIALIZE
;
2776 ASSERT0(vdev_count_verify_zaps(spa
->spa_root_vdev
));
2777 } else if (error
!= 0) {
2778 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2779 } else if (!nvlist_exists(mos_config
, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
)) {
2781 * An older version of ZFS overwrote the sentinel value, so
2782 * we have orphaned per-vdev ZAPs in the MOS. Defer their
2783 * destruction to later; see spa_sync_config_object.
2785 spa
->spa_avz_action
= AVZ_ACTION_DESTROY
;
2787 * We're assuming that no vdevs have had their ZAPs created
2788 * before this. Better be sure of it.
2790 ASSERT0(vdev_count_verify_zaps(spa
->spa_root_vdev
));
2792 nvlist_free(mos_config
);
2795 * If we're assembling the pool from the split-off vdevs of
2796 * an existing pool, we don't want to attach the spares & cache
2801 * Load any hot spares for this pool.
2803 error
= spa_dir_prop(spa
, DMU_POOL_SPARES
, &spa
->spa_spares
.sav_object
);
2804 if (error
!= 0 && error
!= ENOENT
)
2805 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2806 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
2807 ASSERT(spa_version(spa
) >= SPA_VERSION_SPARES
);
2808 if (load_nvlist(spa
, spa
->spa_spares
.sav_object
,
2809 &spa
->spa_spares
.sav_config
) != 0)
2810 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2812 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2813 spa_load_spares(spa
);
2814 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2815 } else if (error
== 0) {
2816 spa
->spa_spares
.sav_sync
= B_TRUE
;
2820 * Load any level 2 ARC devices for this pool.
2822 error
= spa_dir_prop(spa
, DMU_POOL_L2CACHE
,
2823 &spa
->spa_l2cache
.sav_object
);
2824 if (error
!= 0 && error
!= ENOENT
)
2825 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2826 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
2827 ASSERT(spa_version(spa
) >= SPA_VERSION_L2CACHE
);
2828 if (load_nvlist(spa
, spa
->spa_l2cache
.sav_object
,
2829 &spa
->spa_l2cache
.sav_config
) != 0)
2830 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2832 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2833 spa_load_l2cache(spa
);
2834 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2835 } else if (error
== 0) {
2836 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
2839 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
2841 error
= spa_dir_prop(spa
, DMU_POOL_PROPS
, &spa
->spa_pool_props_object
);
2842 if (error
&& error
!= ENOENT
)
2843 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2846 uint64_t autoreplace
= 0;
2848 spa_prop_find(spa
, ZPOOL_PROP_BOOTFS
, &spa
->spa_bootfs
);
2849 spa_prop_find(spa
, ZPOOL_PROP_AUTOREPLACE
, &autoreplace
);
2850 spa_prop_find(spa
, ZPOOL_PROP_DELEGATION
, &spa
->spa_delegation
);
2851 spa_prop_find(spa
, ZPOOL_PROP_FAILUREMODE
, &spa
->spa_failmode
);
2852 spa_prop_find(spa
, ZPOOL_PROP_AUTOEXPAND
, &spa
->spa_autoexpand
);
2853 spa_prop_find(spa
, ZPOOL_PROP_DEDUPDITTO
,
2854 &spa
->spa_dedup_ditto
);
2856 spa
->spa_autoreplace
= (autoreplace
!= 0);
2860 * If the 'autoreplace' property is set, then post a resource notifying
2861 * the ZFS DE that it should not issue any faults for unopenable
2862 * devices. We also iterate over the vdevs, and post a sysevent for any
2863 * unopenable vdevs so that the normal autoreplace handler can take
2866 if (spa
->spa_autoreplace
&& state
!= SPA_LOAD_TRYIMPORT
) {
2867 spa_check_removed(spa
->spa_root_vdev
);
2869 * For the import case, this is done in spa_import(), because
2870 * at this point we're using the spare definitions from
2871 * the MOS config, not necessarily from the userland config.
2873 if (state
!= SPA_LOAD_IMPORT
) {
2874 spa_aux_check_removed(&spa
->spa_spares
);
2875 spa_aux_check_removed(&spa
->spa_l2cache
);
2880 * Load the vdev state for all toplevel vdevs.
2885 * Propagate the leaf DTLs we just loaded all the way up the tree.
2887 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2888 vdev_dtl_reassess(rvd
, 0, 0, B_FALSE
);
2889 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2892 * Load the DDTs (dedup tables).
2894 error
= ddt_load(spa
);
2896 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2898 spa_update_dspace(spa
);
2901 * Validate the config, using the MOS config to fill in any
2902 * information which might be missing. If we fail to validate
2903 * the config then declare the pool unfit for use. If we're
2904 * assembling a pool from a split, the log is not transferred
2907 if (type
!= SPA_IMPORT_ASSEMBLE
) {
2910 if (load_nvlist(spa
, spa
->spa_config_object
, &nvconfig
) != 0)
2911 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2913 if (!spa_config_valid(spa
, nvconfig
)) {
2914 nvlist_free(nvconfig
);
2915 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
,
2918 nvlist_free(nvconfig
);
2921 * Now that we've validated the config, check the state of the
2922 * root vdev. If it can't be opened, it indicates one or
2923 * more toplevel vdevs are faulted.
2925 if (rvd
->vdev_state
<= VDEV_STATE_CANT_OPEN
)
2926 return (SET_ERROR(ENXIO
));
2928 if (spa_writeable(spa
) && spa_check_logs(spa
)) {
2929 *ereport
= FM_EREPORT_ZFS_LOG_REPLAY
;
2930 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_LOG
, ENXIO
));
2934 if (missing_feat_write
) {
2935 ASSERT(state
== SPA_LOAD_TRYIMPORT
);
2938 * At this point, we know that we can open the pool in
2939 * read-only mode but not read-write mode. We now have enough
2940 * information and can return to userland.
2942 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
, ENOTSUP
));
2946 * We've successfully opened the pool, verify that we're ready
2947 * to start pushing transactions.
2949 if (state
!= SPA_LOAD_TRYIMPORT
) {
2950 if ((error
= spa_load_verify(spa
)))
2951 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
2955 if (spa_writeable(spa
) && (state
== SPA_LOAD_RECOVER
||
2956 spa
->spa_load_max_txg
== UINT64_MAX
)) {
2958 int need_update
= B_FALSE
;
2959 dsl_pool_t
*dp
= spa_get_dsl(spa
);
2962 ASSERT(state
!= SPA_LOAD_TRYIMPORT
);
2965 * Claim log blocks that haven't been committed yet.
2966 * This must all happen in a single txg.
2967 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
2968 * invoked from zil_claim_log_block()'s i/o done callback.
2969 * Price of rollback is that we abandon the log.
2971 spa
->spa_claiming
= B_TRUE
;
2973 tx
= dmu_tx_create_assigned(dp
, spa_first_txg(spa
));
2974 (void) dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
2975 zil_claim
, tx
, DS_FIND_CHILDREN
);
2978 spa
->spa_claiming
= B_FALSE
;
2980 spa_set_log_state(spa
, SPA_LOG_GOOD
);
2981 spa
->spa_sync_on
= B_TRUE
;
2982 txg_sync_start(spa
->spa_dsl_pool
);
2985 * Wait for all claims to sync. We sync up to the highest
2986 * claimed log block birth time so that claimed log blocks
2987 * don't appear to be from the future. spa_claim_max_txg
2988 * will have been set for us by either zil_check_log_chain()
2989 * (invoked from spa_check_logs()) or zil_claim() above.
2991 txg_wait_synced(spa
->spa_dsl_pool
, spa
->spa_claim_max_txg
);
2994 * If the config cache is stale, or we have uninitialized
2995 * metaslabs (see spa_vdev_add()), then update the config.
2997 * If this is a verbatim import, trust the current
2998 * in-core spa_config and update the disk labels.
3000 if (config_cache_txg
!= spa
->spa_config_txg
||
3001 state
== SPA_LOAD_IMPORT
||
3002 state
== SPA_LOAD_RECOVER
||
3003 (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
))
3004 need_update
= B_TRUE
;
3006 for (c
= 0; c
< rvd
->vdev_children
; c
++)
3007 if (rvd
->vdev_child
[c
]->vdev_ms_array
== 0)
3008 need_update
= B_TRUE
;
3011 * Update the config cache asychronously in case we're the
3012 * root pool, in which case the config cache isn't writable yet.
3015 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
3018 * Check all DTLs to see if anything needs resilvering.
3020 if (!dsl_scan_resilvering(spa
->spa_dsl_pool
) &&
3021 vdev_resilver_needed(rvd
, NULL
, NULL
))
3022 spa_async_request(spa
, SPA_ASYNC_RESILVER
);
3025 * Log the fact that we booted up (so that we can detect if
3026 * we rebooted in the middle of an operation).
3028 spa_history_log_version(spa
, "open");
3031 * Delete any inconsistent datasets.
3033 (void) dmu_objset_find(spa_name(spa
),
3034 dsl_destroy_inconsistent
, NULL
, DS_FIND_CHILDREN
);
3037 * Clean up any stale temporary dataset userrefs.
3039 dsl_pool_clean_tmp_userrefs(spa
->spa_dsl_pool
);
3046 spa_load_retry(spa_t
*spa
, spa_load_state_t state
, int mosconfig
)
3048 int mode
= spa
->spa_mode
;
3051 spa_deactivate(spa
);
3053 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
- 1;
3055 spa_activate(spa
, mode
);
3056 spa_async_suspend(spa
);
3058 return (spa_load(spa
, state
, SPA_IMPORT_EXISTING
, mosconfig
));
3062 * If spa_load() fails this function will try loading prior txg's. If
3063 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
3064 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
3065 * function will not rewind the pool and will return the same error as
3069 spa_load_best(spa_t
*spa
, spa_load_state_t state
, int mosconfig
,
3070 uint64_t max_request
, int rewind_flags
)
3072 nvlist_t
*loadinfo
= NULL
;
3073 nvlist_t
*config
= NULL
;
3074 int load_error
, rewind_error
;
3075 uint64_t safe_rewind_txg
;
3078 if (spa
->spa_load_txg
&& state
== SPA_LOAD_RECOVER
) {
3079 spa
->spa_load_max_txg
= spa
->spa_load_txg
;
3080 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
3082 spa
->spa_load_max_txg
= max_request
;
3083 if (max_request
!= UINT64_MAX
)
3084 spa
->spa_extreme_rewind
= B_TRUE
;
3087 load_error
= rewind_error
= spa_load(spa
, state
, SPA_IMPORT_EXISTING
,
3089 if (load_error
== 0)
3092 if (spa
->spa_root_vdev
!= NULL
)
3093 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
3095 spa
->spa_last_ubsync_txg
= spa
->spa_uberblock
.ub_txg
;
3096 spa
->spa_last_ubsync_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
3098 if (rewind_flags
& ZPOOL_NEVER_REWIND
) {
3099 nvlist_free(config
);
3100 return (load_error
);
3103 if (state
== SPA_LOAD_RECOVER
) {
3104 /* Price of rolling back is discarding txgs, including log */
3105 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
3108 * If we aren't rolling back save the load info from our first
3109 * import attempt so that we can restore it after attempting
3112 loadinfo
= spa
->spa_load_info
;
3113 spa
->spa_load_info
= fnvlist_alloc();
3116 spa
->spa_load_max_txg
= spa
->spa_last_ubsync_txg
;
3117 safe_rewind_txg
= spa
->spa_last_ubsync_txg
- TXG_DEFER_SIZE
;
3118 min_txg
= (rewind_flags
& ZPOOL_EXTREME_REWIND
) ?
3119 TXG_INITIAL
: safe_rewind_txg
;
3122 * Continue as long as we're finding errors, we're still within
3123 * the acceptable rewind range, and we're still finding uberblocks
3125 while (rewind_error
&& spa
->spa_uberblock
.ub_txg
>= min_txg
&&
3126 spa
->spa_uberblock
.ub_txg
<= spa
->spa_load_max_txg
) {
3127 if (spa
->spa_load_max_txg
< safe_rewind_txg
)
3128 spa
->spa_extreme_rewind
= B_TRUE
;
3129 rewind_error
= spa_load_retry(spa
, state
, mosconfig
);
3132 spa
->spa_extreme_rewind
= B_FALSE
;
3133 spa
->spa_load_max_txg
= UINT64_MAX
;
3135 if (config
&& (rewind_error
|| state
!= SPA_LOAD_RECOVER
))
3136 spa_config_set(spa
, config
);
3138 nvlist_free(config
);
3140 if (state
== SPA_LOAD_RECOVER
) {
3141 ASSERT3P(loadinfo
, ==, NULL
);
3142 return (rewind_error
);
3144 /* Store the rewind info as part of the initial load info */
3145 fnvlist_add_nvlist(loadinfo
, ZPOOL_CONFIG_REWIND_INFO
,
3146 spa
->spa_load_info
);
3148 /* Restore the initial load info */
3149 fnvlist_free(spa
->spa_load_info
);
3150 spa
->spa_load_info
= loadinfo
;
3152 return (load_error
);
3159 * The import case is identical to an open except that the configuration is sent
3160 * down from userland, instead of grabbed from the configuration cache. For the
3161 * case of an open, the pool configuration will exist in the
3162 * POOL_STATE_UNINITIALIZED state.
3164 * The stats information (gen/count/ustats) is used to gather vdev statistics at
3165 * the same time open the pool, without having to keep around the spa_t in some
3169 spa_open_common(const char *pool
, spa_t
**spapp
, void *tag
, nvlist_t
*nvpolicy
,
3173 spa_load_state_t state
= SPA_LOAD_OPEN
;
3175 int locked
= B_FALSE
;
3176 int firstopen
= B_FALSE
;
3181 * As disgusting as this is, we need to support recursive calls to this
3182 * function because dsl_dir_open() is called during spa_load(), and ends
3183 * up calling spa_open() again. The real fix is to figure out how to
3184 * avoid dsl_dir_open() calling this in the first place.
3186 if (mutex_owner(&spa_namespace_lock
) != curthread
) {
3187 mutex_enter(&spa_namespace_lock
);
3191 if ((spa
= spa_lookup(pool
)) == NULL
) {
3193 mutex_exit(&spa_namespace_lock
);
3194 return (SET_ERROR(ENOENT
));
3197 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
) {
3198 zpool_rewind_policy_t policy
;
3202 zpool_get_rewind_policy(nvpolicy
? nvpolicy
: spa
->spa_config
,
3204 if (policy
.zrp_request
& ZPOOL_DO_REWIND
)
3205 state
= SPA_LOAD_RECOVER
;
3207 spa_activate(spa
, spa_mode_global
);
3209 if (state
!= SPA_LOAD_RECOVER
)
3210 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
3212 error
= spa_load_best(spa
, state
, B_FALSE
, policy
.zrp_txg
,
3213 policy
.zrp_request
);
3215 if (error
== EBADF
) {
3217 * If vdev_validate() returns failure (indicated by
3218 * EBADF), it indicates that one of the vdevs indicates
3219 * that the pool has been exported or destroyed. If
3220 * this is the case, the config cache is out of sync and
3221 * we should remove the pool from the namespace.
3224 spa_deactivate(spa
);
3225 spa_config_sync(spa
, B_TRUE
, B_TRUE
);
3228 mutex_exit(&spa_namespace_lock
);
3229 return (SET_ERROR(ENOENT
));
3234 * We can't open the pool, but we still have useful
3235 * information: the state of each vdev after the
3236 * attempted vdev_open(). Return this to the user.
3238 if (config
!= NULL
&& spa
->spa_config
) {
3239 VERIFY(nvlist_dup(spa
->spa_config
, config
,
3241 VERIFY(nvlist_add_nvlist(*config
,
3242 ZPOOL_CONFIG_LOAD_INFO
,
3243 spa
->spa_load_info
) == 0);
3246 spa_deactivate(spa
);
3247 spa
->spa_last_open_failed
= error
;
3249 mutex_exit(&spa_namespace_lock
);
3255 spa_open_ref(spa
, tag
);
3258 *config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
3261 * If we've recovered the pool, pass back any information we
3262 * gathered while doing the load.
3264 if (state
== SPA_LOAD_RECOVER
) {
3265 VERIFY(nvlist_add_nvlist(*config
, ZPOOL_CONFIG_LOAD_INFO
,
3266 spa
->spa_load_info
) == 0);
3270 spa
->spa_last_open_failed
= 0;
3271 spa
->spa_last_ubsync_txg
= 0;
3272 spa
->spa_load_txg
= 0;
3273 mutex_exit(&spa_namespace_lock
);
3277 zvol_create_minors(spa
, spa_name(spa
), B_TRUE
);
3285 spa_open_rewind(const char *name
, spa_t
**spapp
, void *tag
, nvlist_t
*policy
,
3288 return (spa_open_common(name
, spapp
, tag
, policy
, config
));
3292 spa_open(const char *name
, spa_t
**spapp
, void *tag
)
3294 return (spa_open_common(name
, spapp
, tag
, NULL
, NULL
));
3298 * Lookup the given spa_t, incrementing the inject count in the process,
3299 * preventing it from being exported or destroyed.
3302 spa_inject_addref(char *name
)
3306 mutex_enter(&spa_namespace_lock
);
3307 if ((spa
= spa_lookup(name
)) == NULL
) {
3308 mutex_exit(&spa_namespace_lock
);
3311 spa
->spa_inject_ref
++;
3312 mutex_exit(&spa_namespace_lock
);
3318 spa_inject_delref(spa_t
*spa
)
3320 mutex_enter(&spa_namespace_lock
);
3321 spa
->spa_inject_ref
--;
3322 mutex_exit(&spa_namespace_lock
);
3326 * Add spares device information to the nvlist.
3329 spa_add_spares(spa_t
*spa
, nvlist_t
*config
)
3339 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
3341 if (spa
->spa_spares
.sav_count
== 0)
3344 VERIFY(nvlist_lookup_nvlist(config
,
3345 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
3346 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
3347 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
3349 VERIFY(nvlist_add_nvlist_array(nvroot
,
3350 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
3351 VERIFY(nvlist_lookup_nvlist_array(nvroot
,
3352 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
3355 * Go through and find any spares which have since been
3356 * repurposed as an active spare. If this is the case, update
3357 * their status appropriately.
3359 for (i
= 0; i
< nspares
; i
++) {
3360 VERIFY(nvlist_lookup_uint64(spares
[i
],
3361 ZPOOL_CONFIG_GUID
, &guid
) == 0);
3362 if (spa_spare_exists(guid
, &pool
, NULL
) &&
3364 VERIFY(nvlist_lookup_uint64_array(
3365 spares
[i
], ZPOOL_CONFIG_VDEV_STATS
,
3366 (uint64_t **)&vs
, &vsc
) == 0);
3367 vs
->vs_state
= VDEV_STATE_CANT_OPEN
;
3368 vs
->vs_aux
= VDEV_AUX_SPARED
;
3375 * Add l2cache device information to the nvlist, including vdev stats.
3378 spa_add_l2cache(spa_t
*spa
, nvlist_t
*config
)
3381 uint_t i
, j
, nl2cache
;
3388 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
3390 if (spa
->spa_l2cache
.sav_count
== 0)
3393 VERIFY(nvlist_lookup_nvlist(config
,
3394 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
3395 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_l2cache
.sav_config
,
3396 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
3397 if (nl2cache
!= 0) {
3398 VERIFY(nvlist_add_nvlist_array(nvroot
,
3399 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
3400 VERIFY(nvlist_lookup_nvlist_array(nvroot
,
3401 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
3404 * Update level 2 cache device stats.
3407 for (i
= 0; i
< nl2cache
; i
++) {
3408 VERIFY(nvlist_lookup_uint64(l2cache
[i
],
3409 ZPOOL_CONFIG_GUID
, &guid
) == 0);
3412 for (j
= 0; j
< spa
->spa_l2cache
.sav_count
; j
++) {
3414 spa
->spa_l2cache
.sav_vdevs
[j
]->vdev_guid
) {
3415 vd
= spa
->spa_l2cache
.sav_vdevs
[j
];
3421 VERIFY(nvlist_lookup_uint64_array(l2cache
[i
],
3422 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
, &vsc
)
3424 vdev_get_stats(vd
, vs
);
3425 vdev_config_generate_stats(vd
, l2cache
[i
]);
3432 spa_feature_stats_from_disk(spa_t
*spa
, nvlist_t
*features
)
3437 if (spa
->spa_feat_for_read_obj
!= 0) {
3438 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
3439 spa
->spa_feat_for_read_obj
);
3440 zap_cursor_retrieve(&zc
, &za
) == 0;
3441 zap_cursor_advance(&zc
)) {
3442 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
3443 za
.za_num_integers
== 1);
3444 VERIFY0(nvlist_add_uint64(features
, za
.za_name
,
3445 za
.za_first_integer
));
3447 zap_cursor_fini(&zc
);
3450 if (spa
->spa_feat_for_write_obj
!= 0) {
3451 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
3452 spa
->spa_feat_for_write_obj
);
3453 zap_cursor_retrieve(&zc
, &za
) == 0;
3454 zap_cursor_advance(&zc
)) {
3455 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
3456 za
.za_num_integers
== 1);
3457 VERIFY0(nvlist_add_uint64(features
, za
.za_name
,
3458 za
.za_first_integer
));
3460 zap_cursor_fini(&zc
);
3465 spa_feature_stats_from_cache(spa_t
*spa
, nvlist_t
*features
)
3469 for (i
= 0; i
< SPA_FEATURES
; i
++) {
3470 zfeature_info_t feature
= spa_feature_table
[i
];
3473 if (feature_get_refcount(spa
, &feature
, &refcount
) != 0)
3476 VERIFY0(nvlist_add_uint64(features
, feature
.fi_guid
, refcount
));
3481 * Store a list of pool features and their reference counts in the
3484 * The first time this is called on a spa, allocate a new nvlist, fetch
3485 * the pool features and reference counts from disk, then save the list
3486 * in the spa. In subsequent calls on the same spa use the saved nvlist
3487 * and refresh its values from the cached reference counts. This
3488 * ensures we don't block here on I/O on a suspended pool so 'zpool
3489 * clear' can resume the pool.
3492 spa_add_feature_stats(spa_t
*spa
, nvlist_t
*config
)
3496 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
3498 mutex_enter(&spa
->spa_feat_stats_lock
);
3499 features
= spa
->spa_feat_stats
;
3501 if (features
!= NULL
) {
3502 spa_feature_stats_from_cache(spa
, features
);
3504 VERIFY0(nvlist_alloc(&features
, NV_UNIQUE_NAME
, KM_SLEEP
));
3505 spa
->spa_feat_stats
= features
;
3506 spa_feature_stats_from_disk(spa
, features
);
3509 VERIFY0(nvlist_add_nvlist(config
, ZPOOL_CONFIG_FEATURE_STATS
,
3512 mutex_exit(&spa
->spa_feat_stats_lock
);
3516 spa_get_stats(const char *name
, nvlist_t
**config
,
3517 char *altroot
, size_t buflen
)
3523 error
= spa_open_common(name
, &spa
, FTAG
, NULL
, config
);
3527 * This still leaves a window of inconsistency where the spares
3528 * or l2cache devices could change and the config would be
3529 * self-inconsistent.
3531 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
3533 if (*config
!= NULL
) {
3534 uint64_t loadtimes
[2];
3536 loadtimes
[0] = spa
->spa_loaded_ts
.tv_sec
;
3537 loadtimes
[1] = spa
->spa_loaded_ts
.tv_nsec
;
3538 VERIFY(nvlist_add_uint64_array(*config
,
3539 ZPOOL_CONFIG_LOADED_TIME
, loadtimes
, 2) == 0);
3541 VERIFY(nvlist_add_uint64(*config
,
3542 ZPOOL_CONFIG_ERRCOUNT
,
3543 spa_get_errlog_size(spa
)) == 0);
3545 if (spa_suspended(spa
))
3546 VERIFY(nvlist_add_uint64(*config
,
3547 ZPOOL_CONFIG_SUSPENDED
,
3548 spa
->spa_failmode
) == 0);
3550 spa_add_spares(spa
, *config
);
3551 spa_add_l2cache(spa
, *config
);
3552 spa_add_feature_stats(spa
, *config
);
3557 * We want to get the alternate root even for faulted pools, so we cheat
3558 * and call spa_lookup() directly.
3562 mutex_enter(&spa_namespace_lock
);
3563 spa
= spa_lookup(name
);
3565 spa_altroot(spa
, altroot
, buflen
);
3569 mutex_exit(&spa_namespace_lock
);
3571 spa_altroot(spa
, altroot
, buflen
);
3576 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
3577 spa_close(spa
, FTAG
);
3584 * Validate that the auxiliary device array is well formed. We must have an
3585 * array of nvlists, each which describes a valid leaf vdev. If this is an
3586 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
3587 * specified, as long as they are well-formed.
3590 spa_validate_aux_devs(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
,
3591 spa_aux_vdev_t
*sav
, const char *config
, uint64_t version
,
3592 vdev_labeltype_t label
)
3599 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
3602 * It's acceptable to have no devs specified.
3604 if (nvlist_lookup_nvlist_array(nvroot
, config
, &dev
, &ndev
) != 0)
3608 return (SET_ERROR(EINVAL
));
3611 * Make sure the pool is formatted with a version that supports this
3614 if (spa_version(spa
) < version
)
3615 return (SET_ERROR(ENOTSUP
));
3618 * Set the pending device list so we correctly handle device in-use
3621 sav
->sav_pending
= dev
;
3622 sav
->sav_npending
= ndev
;
3624 for (i
= 0; i
< ndev
; i
++) {
3625 if ((error
= spa_config_parse(spa
, &vd
, dev
[i
], NULL
, 0,
3629 if (!vd
->vdev_ops
->vdev_op_leaf
) {
3631 error
= SET_ERROR(EINVAL
);
3636 * The L2ARC currently only supports disk devices in
3637 * kernel context. For user-level testing, we allow it.
3640 if ((strcmp(config
, ZPOOL_CONFIG_L2CACHE
) == 0) &&
3641 strcmp(vd
->vdev_ops
->vdev_op_type
, VDEV_TYPE_DISK
) != 0) {
3642 error
= SET_ERROR(ENOTBLK
);
3649 if ((error
= vdev_open(vd
)) == 0 &&
3650 (error
= vdev_label_init(vd
, crtxg
, label
)) == 0) {
3651 VERIFY(nvlist_add_uint64(dev
[i
], ZPOOL_CONFIG_GUID
,
3652 vd
->vdev_guid
) == 0);
3658 (mode
!= VDEV_ALLOC_SPARE
&& mode
!= VDEV_ALLOC_L2CACHE
))
3665 sav
->sav_pending
= NULL
;
3666 sav
->sav_npending
= 0;
3671 spa_validate_aux(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
)
3675 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
3677 if ((error
= spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
3678 &spa
->spa_spares
, ZPOOL_CONFIG_SPARES
, SPA_VERSION_SPARES
,
3679 VDEV_LABEL_SPARE
)) != 0) {
3683 return (spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
3684 &spa
->spa_l2cache
, ZPOOL_CONFIG_L2CACHE
, SPA_VERSION_L2CACHE
,
3685 VDEV_LABEL_L2CACHE
));
3689 spa_set_aux_vdevs(spa_aux_vdev_t
*sav
, nvlist_t
**devs
, int ndevs
,
3694 if (sav
->sav_config
!= NULL
) {
3700 * Generate new dev list by concatenating with the
3703 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
, config
,
3704 &olddevs
, &oldndevs
) == 0);
3706 newdevs
= kmem_alloc(sizeof (void *) *
3707 (ndevs
+ oldndevs
), KM_SLEEP
);
3708 for (i
= 0; i
< oldndevs
; i
++)
3709 VERIFY(nvlist_dup(olddevs
[i
], &newdevs
[i
],
3711 for (i
= 0; i
< ndevs
; i
++)
3712 VERIFY(nvlist_dup(devs
[i
], &newdevs
[i
+ oldndevs
],
3715 VERIFY(nvlist_remove(sav
->sav_config
, config
,
3716 DATA_TYPE_NVLIST_ARRAY
) == 0);
3718 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
,
3719 config
, newdevs
, ndevs
+ oldndevs
) == 0);
3720 for (i
= 0; i
< oldndevs
+ ndevs
; i
++)
3721 nvlist_free(newdevs
[i
]);
3722 kmem_free(newdevs
, (oldndevs
+ ndevs
) * sizeof (void *));
3725 * Generate a new dev list.
3727 VERIFY(nvlist_alloc(&sav
->sav_config
, NV_UNIQUE_NAME
,
3729 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
, config
,
3735 * Stop and drop level 2 ARC devices
3738 spa_l2cache_drop(spa_t
*spa
)
3742 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
3744 for (i
= 0; i
< sav
->sav_count
; i
++) {
3747 vd
= sav
->sav_vdevs
[i
];
3750 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
3751 pool
!= 0ULL && l2arc_vdev_present(vd
))
3752 l2arc_remove_vdev(vd
);
3760 spa_create(const char *pool
, nvlist_t
*nvroot
, nvlist_t
*props
,
3764 char *altroot
= NULL
;
3769 uint64_t txg
= TXG_INITIAL
;
3770 nvlist_t
**spares
, **l2cache
;
3771 uint_t nspares
, nl2cache
;
3772 uint64_t version
, obj
;
3773 boolean_t has_features
;
3779 if (nvlist_lookup_string(props
, "tname", &poolname
) != 0)
3780 poolname
= (char *)pool
;
3783 * If this pool already exists, return failure.
3785 mutex_enter(&spa_namespace_lock
);
3786 if (spa_lookup(poolname
) != NULL
) {
3787 mutex_exit(&spa_namespace_lock
);
3788 return (SET_ERROR(EEXIST
));
3792 * Allocate a new spa_t structure.
3794 nvl
= fnvlist_alloc();
3795 fnvlist_add_string(nvl
, ZPOOL_CONFIG_POOL_NAME
, pool
);
3796 (void) nvlist_lookup_string(props
,
3797 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
3798 spa
= spa_add(poolname
, nvl
, altroot
);
3800 spa_activate(spa
, spa_mode_global
);
3802 if (props
&& (error
= spa_prop_validate(spa
, props
))) {
3803 spa_deactivate(spa
);
3805 mutex_exit(&spa_namespace_lock
);
3810 * Temporary pool names should never be written to disk.
3812 if (poolname
!= pool
)
3813 spa
->spa_import_flags
|= ZFS_IMPORT_TEMP_NAME
;
3815 has_features
= B_FALSE
;
3816 for (elem
= nvlist_next_nvpair(props
, NULL
);
3817 elem
!= NULL
; elem
= nvlist_next_nvpair(props
, elem
)) {
3818 if (zpool_prop_feature(nvpair_name(elem
)))
3819 has_features
= B_TRUE
;
3822 if (has_features
|| nvlist_lookup_uint64(props
,
3823 zpool_prop_to_name(ZPOOL_PROP_VERSION
), &version
) != 0) {
3824 version
= SPA_VERSION
;
3826 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
3828 spa
->spa_first_txg
= txg
;
3829 spa
->spa_uberblock
.ub_txg
= txg
- 1;
3830 spa
->spa_uberblock
.ub_version
= version
;
3831 spa
->spa_ubsync
= spa
->spa_uberblock
;
3832 spa
->spa_load_state
= SPA_LOAD_CREATE
;
3835 * Create "The Godfather" zio to hold all async IOs
3837 spa
->spa_async_zio_root
= kmem_alloc(max_ncpus
* sizeof (void *),
3839 for (i
= 0; i
< max_ncpus
; i
++) {
3840 spa
->spa_async_zio_root
[i
] = zio_root(spa
, NULL
, NULL
,
3841 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
3842 ZIO_FLAG_GODFATHER
);
3846 * Create the root vdev.
3848 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3850 error
= spa_config_parse(spa
, &rvd
, nvroot
, NULL
, 0, VDEV_ALLOC_ADD
);
3852 ASSERT(error
!= 0 || rvd
!= NULL
);
3853 ASSERT(error
!= 0 || spa
->spa_root_vdev
== rvd
);
3855 if (error
== 0 && !zfs_allocatable_devs(nvroot
))
3856 error
= SET_ERROR(EINVAL
);
3859 (error
= vdev_create(rvd
, txg
, B_FALSE
)) == 0 &&
3860 (error
= spa_validate_aux(spa
, nvroot
, txg
,
3861 VDEV_ALLOC_ADD
)) == 0) {
3862 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
3863 vdev_metaslab_set_size(rvd
->vdev_child
[c
]);
3864 vdev_expand(rvd
->vdev_child
[c
], txg
);
3868 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3872 spa_deactivate(spa
);
3874 mutex_exit(&spa_namespace_lock
);
3879 * Get the list of spares, if specified.
3881 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
3882 &spares
, &nspares
) == 0) {
3883 VERIFY(nvlist_alloc(&spa
->spa_spares
.sav_config
, NV_UNIQUE_NAME
,
3885 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
3886 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
3887 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3888 spa_load_spares(spa
);
3889 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3890 spa
->spa_spares
.sav_sync
= B_TRUE
;
3894 * Get the list of level 2 cache devices, if specified.
3896 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
3897 &l2cache
, &nl2cache
) == 0) {
3898 VERIFY(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
3899 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
3900 VERIFY(nvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
3901 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
3902 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3903 spa_load_l2cache(spa
);
3904 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3905 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
3908 spa
->spa_is_initializing
= B_TRUE
;
3909 spa
->spa_dsl_pool
= dp
= dsl_pool_create(spa
, zplprops
, txg
);
3910 spa
->spa_meta_objset
= dp
->dp_meta_objset
;
3911 spa
->spa_is_initializing
= B_FALSE
;
3914 * Create DDTs (dedup tables).
3918 spa_update_dspace(spa
);
3920 tx
= dmu_tx_create_assigned(dp
, txg
);
3923 * Create the pool config object.
3925 spa
->spa_config_object
= dmu_object_alloc(spa
->spa_meta_objset
,
3926 DMU_OT_PACKED_NVLIST
, SPA_CONFIG_BLOCKSIZE
,
3927 DMU_OT_PACKED_NVLIST_SIZE
, sizeof (uint64_t), tx
);
3929 if (zap_add(spa
->spa_meta_objset
,
3930 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CONFIG
,
3931 sizeof (uint64_t), 1, &spa
->spa_config_object
, tx
) != 0) {
3932 cmn_err(CE_PANIC
, "failed to add pool config");
3935 if (spa_version(spa
) >= SPA_VERSION_FEATURES
)
3936 spa_feature_create_zap_objects(spa
, tx
);
3938 if (zap_add(spa
->spa_meta_objset
,
3939 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CREATION_VERSION
,
3940 sizeof (uint64_t), 1, &version
, tx
) != 0) {
3941 cmn_err(CE_PANIC
, "failed to add pool version");
3944 /* Newly created pools with the right version are always deflated. */
3945 if (version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
3946 spa
->spa_deflate
= TRUE
;
3947 if (zap_add(spa
->spa_meta_objset
,
3948 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
3949 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
) != 0) {
3950 cmn_err(CE_PANIC
, "failed to add deflate");
3955 * Create the deferred-free bpobj. Turn off compression
3956 * because sync-to-convergence takes longer if the blocksize
3959 obj
= bpobj_alloc(spa
->spa_meta_objset
, 1 << 14, tx
);
3960 dmu_object_set_compress(spa
->spa_meta_objset
, obj
,
3961 ZIO_COMPRESS_OFF
, tx
);
3962 if (zap_add(spa
->spa_meta_objset
,
3963 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_SYNC_BPOBJ
,
3964 sizeof (uint64_t), 1, &obj
, tx
) != 0) {
3965 cmn_err(CE_PANIC
, "failed to add bpobj");
3967 VERIFY3U(0, ==, bpobj_open(&spa
->spa_deferred_bpobj
,
3968 spa
->spa_meta_objset
, obj
));
3971 * Create the pool's history object.
3973 if (version
>= SPA_VERSION_ZPOOL_HISTORY
)
3974 spa_history_create_obj(spa
, tx
);
3977 * Generate some random noise for salted checksums to operate on.
3979 (void) random_get_pseudo_bytes(spa
->spa_cksum_salt
.zcs_bytes
,
3980 sizeof (spa
->spa_cksum_salt
.zcs_bytes
));
3983 * Set pool properties.
3985 spa
->spa_bootfs
= zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS
);
3986 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
3987 spa
->spa_failmode
= zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE
);
3988 spa
->spa_autoexpand
= zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND
);
3990 if (props
!= NULL
) {
3991 spa_configfile_set(spa
, props
, B_FALSE
);
3992 spa_sync_props(props
, tx
);
3997 spa
->spa_sync_on
= B_TRUE
;
3998 txg_sync_start(spa
->spa_dsl_pool
);
4001 * We explicitly wait for the first transaction to complete so that our
4002 * bean counters are appropriately updated.
4004 txg_wait_synced(spa
->spa_dsl_pool
, txg
);
4006 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
4007 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_CREATE
);
4009 spa_history_log_version(spa
, "create");
4012 * Don't count references from objsets that are already closed
4013 * and are making their way through the eviction process.
4015 spa_evicting_os_wait(spa
);
4016 spa
->spa_minref
= refcount_count(&spa
->spa_refcount
);
4017 spa
->spa_load_state
= SPA_LOAD_NONE
;
4019 mutex_exit(&spa_namespace_lock
);
4025 * Import a non-root pool into the system.
4028 spa_import(char *pool
, nvlist_t
*config
, nvlist_t
*props
, uint64_t flags
)
4031 char *altroot
= NULL
;
4032 spa_load_state_t state
= SPA_LOAD_IMPORT
;
4033 zpool_rewind_policy_t policy
;
4034 uint64_t mode
= spa_mode_global
;
4035 uint64_t readonly
= B_FALSE
;
4038 nvlist_t
**spares
, **l2cache
;
4039 uint_t nspares
, nl2cache
;
4042 * If a pool with this name exists, return failure.
4044 mutex_enter(&spa_namespace_lock
);
4045 if (spa_lookup(pool
) != NULL
) {
4046 mutex_exit(&spa_namespace_lock
);
4047 return (SET_ERROR(EEXIST
));
4051 * Create and initialize the spa structure.
4053 (void) nvlist_lookup_string(props
,
4054 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
4055 (void) nvlist_lookup_uint64(props
,
4056 zpool_prop_to_name(ZPOOL_PROP_READONLY
), &readonly
);
4059 spa
= spa_add(pool
, config
, altroot
);
4060 spa
->spa_import_flags
= flags
;
4063 * Verbatim import - Take a pool and insert it into the namespace
4064 * as if it had been loaded at boot.
4066 if (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
) {
4068 spa_configfile_set(spa
, props
, B_FALSE
);
4070 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
4071 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_IMPORT
);
4073 mutex_exit(&spa_namespace_lock
);
4077 spa_activate(spa
, mode
);
4080 * Don't start async tasks until we know everything is healthy.
4082 spa_async_suspend(spa
);
4084 zpool_get_rewind_policy(config
, &policy
);
4085 if (policy
.zrp_request
& ZPOOL_DO_REWIND
)
4086 state
= SPA_LOAD_RECOVER
;
4089 * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig
4090 * because the user-supplied config is actually the one to trust when
4093 if (state
!= SPA_LOAD_RECOVER
)
4094 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
4096 error
= spa_load_best(spa
, state
, B_TRUE
, policy
.zrp_txg
,
4097 policy
.zrp_request
);
4100 * Propagate anything learned while loading the pool and pass it
4101 * back to caller (i.e. rewind info, missing devices, etc).
4103 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
4104 spa
->spa_load_info
) == 0);
4106 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4108 * Toss any existing sparelist, as it doesn't have any validity
4109 * anymore, and conflicts with spa_has_spare().
4111 if (spa
->spa_spares
.sav_config
) {
4112 nvlist_free(spa
->spa_spares
.sav_config
);
4113 spa
->spa_spares
.sav_config
= NULL
;
4114 spa_load_spares(spa
);
4116 if (spa
->spa_l2cache
.sav_config
) {
4117 nvlist_free(spa
->spa_l2cache
.sav_config
);
4118 spa
->spa_l2cache
.sav_config
= NULL
;
4119 spa_load_l2cache(spa
);
4122 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
4124 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4127 spa_configfile_set(spa
, props
, B_FALSE
);
4129 if (error
!= 0 || (props
&& spa_writeable(spa
) &&
4130 (error
= spa_prop_set(spa
, props
)))) {
4132 spa_deactivate(spa
);
4134 mutex_exit(&spa_namespace_lock
);
4138 spa_async_resume(spa
);
4141 * Override any spares and level 2 cache devices as specified by
4142 * the user, as these may have correct device names/devids, etc.
4144 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
4145 &spares
, &nspares
) == 0) {
4146 if (spa
->spa_spares
.sav_config
)
4147 VERIFY(nvlist_remove(spa
->spa_spares
.sav_config
,
4148 ZPOOL_CONFIG_SPARES
, DATA_TYPE_NVLIST_ARRAY
) == 0);
4150 VERIFY(nvlist_alloc(&spa
->spa_spares
.sav_config
,
4151 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
4152 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
4153 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
4154 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4155 spa_load_spares(spa
);
4156 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4157 spa
->spa_spares
.sav_sync
= B_TRUE
;
4159 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
4160 &l2cache
, &nl2cache
) == 0) {
4161 if (spa
->spa_l2cache
.sav_config
)
4162 VERIFY(nvlist_remove(spa
->spa_l2cache
.sav_config
,
4163 ZPOOL_CONFIG_L2CACHE
, DATA_TYPE_NVLIST_ARRAY
) == 0);
4165 VERIFY(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
4166 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
4167 VERIFY(nvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
4168 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
4169 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4170 spa_load_l2cache(spa
);
4171 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4172 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
4176 * Check for any removed devices.
4178 if (spa
->spa_autoreplace
) {
4179 spa_aux_check_removed(&spa
->spa_spares
);
4180 spa_aux_check_removed(&spa
->spa_l2cache
);
4183 if (spa_writeable(spa
)) {
4185 * Update the config cache to include the newly-imported pool.
4187 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
4191 * It's possible that the pool was expanded while it was exported.
4192 * We kick off an async task to handle this for us.
4194 spa_async_request(spa
, SPA_ASYNC_AUTOEXPAND
);
4196 spa_history_log_version(spa
, "import");
4198 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_IMPORT
);
4200 zvol_create_minors(spa
, pool
, B_TRUE
);
4202 mutex_exit(&spa_namespace_lock
);
4208 spa_tryimport(nvlist_t
*tryconfig
)
4210 nvlist_t
*config
= NULL
;
4216 if (nvlist_lookup_string(tryconfig
, ZPOOL_CONFIG_POOL_NAME
, &poolname
))
4219 if (nvlist_lookup_uint64(tryconfig
, ZPOOL_CONFIG_POOL_STATE
, &state
))
4223 * Create and initialize the spa structure.
4225 mutex_enter(&spa_namespace_lock
);
4226 spa
= spa_add(TRYIMPORT_NAME
, tryconfig
, NULL
);
4227 spa_activate(spa
, FREAD
);
4230 * Pass off the heavy lifting to spa_load().
4231 * Pass TRUE for mosconfig because the user-supplied config
4232 * is actually the one to trust when doing an import.
4234 error
= spa_load(spa
, SPA_LOAD_TRYIMPORT
, SPA_IMPORT_EXISTING
, B_TRUE
);
4237 * If 'tryconfig' was at least parsable, return the current config.
4239 if (spa
->spa_root_vdev
!= NULL
) {
4240 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
4241 VERIFY(nvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
,
4243 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
4245 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_TIMESTAMP
,
4246 spa
->spa_uberblock
.ub_timestamp
) == 0);
4247 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
4248 spa
->spa_load_info
) == 0);
4249 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_ERRATA
,
4250 spa
->spa_errata
) == 0);
4253 * If the bootfs property exists on this pool then we
4254 * copy it out so that external consumers can tell which
4255 * pools are bootable.
4257 if ((!error
|| error
== EEXIST
) && spa
->spa_bootfs
) {
4258 char *tmpname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
4261 * We have to play games with the name since the
4262 * pool was opened as TRYIMPORT_NAME.
4264 if (dsl_dsobj_to_dsname(spa_name(spa
),
4265 spa
->spa_bootfs
, tmpname
) == 0) {
4269 dsname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
4271 cp
= strchr(tmpname
, '/');
4273 (void) strlcpy(dsname
, tmpname
,
4276 (void) snprintf(dsname
, MAXPATHLEN
,
4277 "%s/%s", poolname
, ++cp
);
4279 VERIFY(nvlist_add_string(config
,
4280 ZPOOL_CONFIG_BOOTFS
, dsname
) == 0);
4281 kmem_free(dsname
, MAXPATHLEN
);
4283 kmem_free(tmpname
, MAXPATHLEN
);
4287 * Add the list of hot spares and level 2 cache devices.
4289 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
4290 spa_add_spares(spa
, config
);
4291 spa_add_l2cache(spa
, config
);
4292 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
4296 spa_deactivate(spa
);
4298 mutex_exit(&spa_namespace_lock
);
4304 * Pool export/destroy
4306 * The act of destroying or exporting a pool is very simple. We make sure there
4307 * is no more pending I/O and any references to the pool are gone. Then, we
4308 * update the pool state and sync all the labels to disk, removing the
4309 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
4310 * we don't sync the labels or remove the configuration cache.
4313 spa_export_common(char *pool
, int new_state
, nvlist_t
**oldconfig
,
4314 boolean_t force
, boolean_t hardforce
)
4321 if (!(spa_mode_global
& FWRITE
))
4322 return (SET_ERROR(EROFS
));
4324 mutex_enter(&spa_namespace_lock
);
4325 if ((spa
= spa_lookup(pool
)) == NULL
) {
4326 mutex_exit(&spa_namespace_lock
);
4327 return (SET_ERROR(ENOENT
));
4331 * Put a hold on the pool, drop the namespace lock, stop async tasks,
4332 * reacquire the namespace lock, and see if we can export.
4334 spa_open_ref(spa
, FTAG
);
4335 mutex_exit(&spa_namespace_lock
);
4336 spa_async_suspend(spa
);
4337 if (spa
->spa_zvol_taskq
) {
4338 zvol_remove_minors(spa
, spa_name(spa
), B_TRUE
);
4339 taskq_wait(spa
->spa_zvol_taskq
);
4341 mutex_enter(&spa_namespace_lock
);
4342 spa_close(spa
, FTAG
);
4344 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
)
4347 * The pool will be in core if it's openable, in which case we can
4348 * modify its state. Objsets may be open only because they're dirty,
4349 * so we have to force it to sync before checking spa_refcnt.
4351 if (spa
->spa_sync_on
) {
4352 txg_wait_synced(spa
->spa_dsl_pool
, 0);
4353 spa_evicting_os_wait(spa
);
4357 * A pool cannot be exported or destroyed if there are active
4358 * references. If we are resetting a pool, allow references by
4359 * fault injection handlers.
4361 if (!spa_refcount_zero(spa
) ||
4362 (spa
->spa_inject_ref
!= 0 &&
4363 new_state
!= POOL_STATE_UNINITIALIZED
)) {
4364 spa_async_resume(spa
);
4365 mutex_exit(&spa_namespace_lock
);
4366 return (SET_ERROR(EBUSY
));
4369 if (spa
->spa_sync_on
) {
4371 * A pool cannot be exported if it has an active shared spare.
4372 * This is to prevent other pools stealing the active spare
4373 * from an exported pool. At user's own will, such pool can
4374 * be forcedly exported.
4376 if (!force
&& new_state
== POOL_STATE_EXPORTED
&&
4377 spa_has_active_shared_spare(spa
)) {
4378 spa_async_resume(spa
);
4379 mutex_exit(&spa_namespace_lock
);
4380 return (SET_ERROR(EXDEV
));
4384 * We want this to be reflected on every label,
4385 * so mark them all dirty. spa_unload() will do the
4386 * final sync that pushes these changes out.
4388 if (new_state
!= POOL_STATE_UNINITIALIZED
&& !hardforce
) {
4389 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4390 spa
->spa_state
= new_state
;
4391 spa
->spa_final_txg
= spa_last_synced_txg(spa
) +
4393 vdev_config_dirty(spa
->spa_root_vdev
);
4394 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4399 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_DESTROY
);
4401 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
4403 spa_deactivate(spa
);
4406 if (oldconfig
&& spa
->spa_config
)
4407 VERIFY(nvlist_dup(spa
->spa_config
, oldconfig
, 0) == 0);
4409 if (new_state
!= POOL_STATE_UNINITIALIZED
) {
4411 spa_config_sync(spa
, B_TRUE
, B_TRUE
);
4414 mutex_exit(&spa_namespace_lock
);
4420 * Destroy a storage pool.
4423 spa_destroy(char *pool
)
4425 return (spa_export_common(pool
, POOL_STATE_DESTROYED
, NULL
,
4430 * Export a storage pool.
4433 spa_export(char *pool
, nvlist_t
**oldconfig
, boolean_t force
,
4434 boolean_t hardforce
)
4436 return (spa_export_common(pool
, POOL_STATE_EXPORTED
, oldconfig
,
4441 * Similar to spa_export(), this unloads the spa_t without actually removing it
4442 * from the namespace in any way.
4445 spa_reset(char *pool
)
4447 return (spa_export_common(pool
, POOL_STATE_UNINITIALIZED
, NULL
,
4452 * ==========================================================================
4453 * Device manipulation
4454 * ==========================================================================
4458 * Add a device to a storage pool.
4461 spa_vdev_add(spa_t
*spa
, nvlist_t
*nvroot
)
4465 vdev_t
*rvd
= spa
->spa_root_vdev
;
4467 nvlist_t
**spares
, **l2cache
;
4468 uint_t nspares
, nl2cache
;
4471 ASSERT(spa_writeable(spa
));
4473 txg
= spa_vdev_enter(spa
);
4475 if ((error
= spa_config_parse(spa
, &vd
, nvroot
, NULL
, 0,
4476 VDEV_ALLOC_ADD
)) != 0)
4477 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
4479 spa
->spa_pending_vdev
= vd
; /* spa_vdev_exit() will clear this */
4481 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
, &spares
,
4485 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
, &l2cache
,
4489 if (vd
->vdev_children
== 0 && nspares
== 0 && nl2cache
== 0)
4490 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
4492 if (vd
->vdev_children
!= 0 &&
4493 (error
= vdev_create(vd
, txg
, B_FALSE
)) != 0)
4494 return (spa_vdev_exit(spa
, vd
, txg
, error
));
4497 * We must validate the spares and l2cache devices after checking the
4498 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
4500 if ((error
= spa_validate_aux(spa
, nvroot
, txg
, VDEV_ALLOC_ADD
)) != 0)
4501 return (spa_vdev_exit(spa
, vd
, txg
, error
));
4504 * Transfer each new top-level vdev from vd to rvd.
4506 for (c
= 0; c
< vd
->vdev_children
; c
++) {
4509 * Set the vdev id to the first hole, if one exists.
4511 for (id
= 0; id
< rvd
->vdev_children
; id
++) {
4512 if (rvd
->vdev_child
[id
]->vdev_ishole
) {
4513 vdev_free(rvd
->vdev_child
[id
]);
4517 tvd
= vd
->vdev_child
[c
];
4518 vdev_remove_child(vd
, tvd
);
4520 vdev_add_child(rvd
, tvd
);
4521 vdev_config_dirty(tvd
);
4525 spa_set_aux_vdevs(&spa
->spa_spares
, spares
, nspares
,
4526 ZPOOL_CONFIG_SPARES
);
4527 spa_load_spares(spa
);
4528 spa
->spa_spares
.sav_sync
= B_TRUE
;
4531 if (nl2cache
!= 0) {
4532 spa_set_aux_vdevs(&spa
->spa_l2cache
, l2cache
, nl2cache
,
4533 ZPOOL_CONFIG_L2CACHE
);
4534 spa_load_l2cache(spa
);
4535 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
4539 * We have to be careful when adding new vdevs to an existing pool.
4540 * If other threads start allocating from these vdevs before we
4541 * sync the config cache, and we lose power, then upon reboot we may
4542 * fail to open the pool because there are DVAs that the config cache
4543 * can't translate. Therefore, we first add the vdevs without
4544 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
4545 * and then let spa_config_update() initialize the new metaslabs.
4547 * spa_load() checks for added-but-not-initialized vdevs, so that
4548 * if we lose power at any point in this sequence, the remaining
4549 * steps will be completed the next time we load the pool.
4551 (void) spa_vdev_exit(spa
, vd
, txg
, 0);
4553 mutex_enter(&spa_namespace_lock
);
4554 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
4555 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_VDEV_ADD
);
4556 mutex_exit(&spa_namespace_lock
);
4562 * Attach a device to a mirror. The arguments are the path to any device
4563 * in the mirror, and the nvroot for the new device. If the path specifies
4564 * a device that is not mirrored, we automatically insert the mirror vdev.
4566 * If 'replacing' is specified, the new device is intended to replace the
4567 * existing device; in this case the two devices are made into their own
4568 * mirror using the 'replacing' vdev, which is functionally identical to
4569 * the mirror vdev (it actually reuses all the same ops) but has a few
4570 * extra rules: you can't attach to it after it's been created, and upon
4571 * completion of resilvering, the first disk (the one being replaced)
4572 * is automatically detached.
4575 spa_vdev_attach(spa_t
*spa
, uint64_t guid
, nvlist_t
*nvroot
, int replacing
)
4577 uint64_t txg
, dtl_max_txg
;
4578 vdev_t
*oldvd
, *newvd
, *newrootvd
, *pvd
, *tvd
;
4580 char *oldvdpath
, *newvdpath
;
4583 ASSERTV(vdev_t
*rvd
= spa
->spa_root_vdev
);
4585 ASSERT(spa_writeable(spa
));
4587 txg
= spa_vdev_enter(spa
);
4589 oldvd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
4592 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
4594 if (!oldvd
->vdev_ops
->vdev_op_leaf
)
4595 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4597 pvd
= oldvd
->vdev_parent
;
4599 if ((error
= spa_config_parse(spa
, &newrootvd
, nvroot
, NULL
, 0,
4600 VDEV_ALLOC_ATTACH
)) != 0)
4601 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
4603 if (newrootvd
->vdev_children
!= 1)
4604 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
4606 newvd
= newrootvd
->vdev_child
[0];
4608 if (!newvd
->vdev_ops
->vdev_op_leaf
)
4609 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
4611 if ((error
= vdev_create(newrootvd
, txg
, replacing
)) != 0)
4612 return (spa_vdev_exit(spa
, newrootvd
, txg
, error
));
4615 * Spares can't replace logs
4617 if (oldvd
->vdev_top
->vdev_islog
&& newvd
->vdev_isspare
)
4618 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4622 * For attach, the only allowable parent is a mirror or the root
4625 if (pvd
->vdev_ops
!= &vdev_mirror_ops
&&
4626 pvd
->vdev_ops
!= &vdev_root_ops
)
4627 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4629 pvops
= &vdev_mirror_ops
;
4632 * Active hot spares can only be replaced by inactive hot
4635 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
4636 oldvd
->vdev_isspare
&&
4637 !spa_has_spare(spa
, newvd
->vdev_guid
))
4638 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4641 * If the source is a hot spare, and the parent isn't already a
4642 * spare, then we want to create a new hot spare. Otherwise, we
4643 * want to create a replacing vdev. The user is not allowed to
4644 * attach to a spared vdev child unless the 'isspare' state is
4645 * the same (spare replaces spare, non-spare replaces
4648 if (pvd
->vdev_ops
== &vdev_replacing_ops
&&
4649 spa_version(spa
) < SPA_VERSION_MULTI_REPLACE
) {
4650 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4651 } else if (pvd
->vdev_ops
== &vdev_spare_ops
&&
4652 newvd
->vdev_isspare
!= oldvd
->vdev_isspare
) {
4653 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4656 if (newvd
->vdev_isspare
)
4657 pvops
= &vdev_spare_ops
;
4659 pvops
= &vdev_replacing_ops
;
4663 * Make sure the new device is big enough.
4665 if (newvd
->vdev_asize
< vdev_get_min_asize(oldvd
))
4666 return (spa_vdev_exit(spa
, newrootvd
, txg
, EOVERFLOW
));
4669 * The new device cannot have a higher alignment requirement
4670 * than the top-level vdev.
4672 if (newvd
->vdev_ashift
> oldvd
->vdev_top
->vdev_ashift
)
4673 return (spa_vdev_exit(spa
, newrootvd
, txg
, EDOM
));
4676 * If this is an in-place replacement, update oldvd's path and devid
4677 * to make it distinguishable from newvd, and unopenable from now on.
4679 if (strcmp(oldvd
->vdev_path
, newvd
->vdev_path
) == 0) {
4680 spa_strfree(oldvd
->vdev_path
);
4681 oldvd
->vdev_path
= kmem_alloc(strlen(newvd
->vdev_path
) + 5,
4683 (void) sprintf(oldvd
->vdev_path
, "%s/%s",
4684 newvd
->vdev_path
, "old");
4685 if (oldvd
->vdev_devid
!= NULL
) {
4686 spa_strfree(oldvd
->vdev_devid
);
4687 oldvd
->vdev_devid
= NULL
;
4691 /* mark the device being resilvered */
4692 newvd
->vdev_resilver_txg
= txg
;
4695 * If the parent is not a mirror, or if we're replacing, insert the new
4696 * mirror/replacing/spare vdev above oldvd.
4698 if (pvd
->vdev_ops
!= pvops
)
4699 pvd
= vdev_add_parent(oldvd
, pvops
);
4701 ASSERT(pvd
->vdev_top
->vdev_parent
== rvd
);
4702 ASSERT(pvd
->vdev_ops
== pvops
);
4703 ASSERT(oldvd
->vdev_parent
== pvd
);
4706 * Extract the new device from its root and add it to pvd.
4708 vdev_remove_child(newrootvd
, newvd
);
4709 newvd
->vdev_id
= pvd
->vdev_children
;
4710 newvd
->vdev_crtxg
= oldvd
->vdev_crtxg
;
4711 vdev_add_child(pvd
, newvd
);
4714 * Reevaluate the parent vdev state.
4716 vdev_propagate_state(pvd
);
4718 tvd
= newvd
->vdev_top
;
4719 ASSERT(pvd
->vdev_top
== tvd
);
4720 ASSERT(tvd
->vdev_parent
== rvd
);
4722 vdev_config_dirty(tvd
);
4725 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
4726 * for any dmu_sync-ed blocks. It will propagate upward when
4727 * spa_vdev_exit() calls vdev_dtl_reassess().
4729 dtl_max_txg
= txg
+ TXG_CONCURRENT_STATES
;
4731 vdev_dtl_dirty(newvd
, DTL_MISSING
, TXG_INITIAL
,
4732 dtl_max_txg
- TXG_INITIAL
);
4734 if (newvd
->vdev_isspare
) {
4735 spa_spare_activate(newvd
);
4736 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_VDEV_SPARE
);
4739 oldvdpath
= spa_strdup(oldvd
->vdev_path
);
4740 newvdpath
= spa_strdup(newvd
->vdev_path
);
4741 newvd_isspare
= newvd
->vdev_isspare
;
4744 * Mark newvd's DTL dirty in this txg.
4746 vdev_dirty(tvd
, VDD_DTL
, newvd
, txg
);
4749 * Schedule the resilver to restart in the future. We do this to
4750 * ensure that dmu_sync-ed blocks have been stitched into the
4751 * respective datasets.
4753 dsl_resilver_restart(spa
->spa_dsl_pool
, dtl_max_txg
);
4755 if (spa
->spa_bootfs
)
4756 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_BOOTFS_VDEV_ATTACH
);
4758 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_VDEV_ATTACH
);
4763 (void) spa_vdev_exit(spa
, newrootvd
, dtl_max_txg
, 0);
4765 spa_history_log_internal(spa
, "vdev attach", NULL
,
4766 "%s vdev=%s %s vdev=%s",
4767 replacing
&& newvd_isspare
? "spare in" :
4768 replacing
? "replace" : "attach", newvdpath
,
4769 replacing
? "for" : "to", oldvdpath
);
4771 spa_strfree(oldvdpath
);
4772 spa_strfree(newvdpath
);
4778 * Detach a device from a mirror or replacing vdev.
4780 * If 'replace_done' is specified, only detach if the parent
4781 * is a replacing vdev.
4784 spa_vdev_detach(spa_t
*spa
, uint64_t guid
, uint64_t pguid
, int replace_done
)
4788 vdev_t
*vd
, *pvd
, *cvd
, *tvd
;
4789 boolean_t unspare
= B_FALSE
;
4790 uint64_t unspare_guid
= 0;
4793 ASSERTV(vdev_t
*rvd
= spa
->spa_root_vdev
);
4794 ASSERT(spa_writeable(spa
));
4796 txg
= spa_vdev_enter(spa
);
4798 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
4801 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
4803 if (!vd
->vdev_ops
->vdev_op_leaf
)
4804 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4806 pvd
= vd
->vdev_parent
;
4809 * If the parent/child relationship is not as expected, don't do it.
4810 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
4811 * vdev that's replacing B with C. The user's intent in replacing
4812 * is to go from M(A,B) to M(A,C). If the user decides to cancel
4813 * the replace by detaching C, the expected behavior is to end up
4814 * M(A,B). But suppose that right after deciding to detach C,
4815 * the replacement of B completes. We would have M(A,C), and then
4816 * ask to detach C, which would leave us with just A -- not what
4817 * the user wanted. To prevent this, we make sure that the
4818 * parent/child relationship hasn't changed -- in this example,
4819 * that C's parent is still the replacing vdev R.
4821 if (pvd
->vdev_guid
!= pguid
&& pguid
!= 0)
4822 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
4825 * Only 'replacing' or 'spare' vdevs can be replaced.
4827 if (replace_done
&& pvd
->vdev_ops
!= &vdev_replacing_ops
&&
4828 pvd
->vdev_ops
!= &vdev_spare_ops
)
4829 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4831 ASSERT(pvd
->vdev_ops
!= &vdev_spare_ops
||
4832 spa_version(spa
) >= SPA_VERSION_SPARES
);
4835 * Only mirror, replacing, and spare vdevs support detach.
4837 if (pvd
->vdev_ops
!= &vdev_replacing_ops
&&
4838 pvd
->vdev_ops
!= &vdev_mirror_ops
&&
4839 pvd
->vdev_ops
!= &vdev_spare_ops
)
4840 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4843 * If this device has the only valid copy of some data,
4844 * we cannot safely detach it.
4846 if (vdev_dtl_required(vd
))
4847 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
4849 ASSERT(pvd
->vdev_children
>= 2);
4852 * If we are detaching the second disk from a replacing vdev, then
4853 * check to see if we changed the original vdev's path to have "/old"
4854 * at the end in spa_vdev_attach(). If so, undo that change now.
4856 if (pvd
->vdev_ops
== &vdev_replacing_ops
&& vd
->vdev_id
> 0 &&
4857 vd
->vdev_path
!= NULL
) {
4858 size_t len
= strlen(vd
->vdev_path
);
4860 for (c
= 0; c
< pvd
->vdev_children
; c
++) {
4861 cvd
= pvd
->vdev_child
[c
];
4863 if (cvd
== vd
|| cvd
->vdev_path
== NULL
)
4866 if (strncmp(cvd
->vdev_path
, vd
->vdev_path
, len
) == 0 &&
4867 strcmp(cvd
->vdev_path
+ len
, "/old") == 0) {
4868 spa_strfree(cvd
->vdev_path
);
4869 cvd
->vdev_path
= spa_strdup(vd
->vdev_path
);
4876 * If we are detaching the original disk from a spare, then it implies
4877 * that the spare should become a real disk, and be removed from the
4878 * active spare list for the pool.
4880 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
4882 pvd
->vdev_child
[pvd
->vdev_children
- 1]->vdev_isspare
)
4886 * Erase the disk labels so the disk can be used for other things.
4887 * This must be done after all other error cases are handled,
4888 * but before we disembowel vd (so we can still do I/O to it).
4889 * But if we can't do it, don't treat the error as fatal --
4890 * it may be that the unwritability of the disk is the reason
4891 * it's being detached!
4893 error
= vdev_label_init(vd
, 0, VDEV_LABEL_REMOVE
);
4896 * Remove vd from its parent and compact the parent's children.
4898 vdev_remove_child(pvd
, vd
);
4899 vdev_compact_children(pvd
);
4902 * Remember one of the remaining children so we can get tvd below.
4904 cvd
= pvd
->vdev_child
[pvd
->vdev_children
- 1];
4907 * If we need to remove the remaining child from the list of hot spares,
4908 * do it now, marking the vdev as no longer a spare in the process.
4909 * We must do this before vdev_remove_parent(), because that can
4910 * change the GUID if it creates a new toplevel GUID. For a similar
4911 * reason, we must remove the spare now, in the same txg as the detach;
4912 * otherwise someone could attach a new sibling, change the GUID, and
4913 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
4916 ASSERT(cvd
->vdev_isspare
);
4917 spa_spare_remove(cvd
);
4918 unspare_guid
= cvd
->vdev_guid
;
4919 (void) spa_vdev_remove(spa
, unspare_guid
, B_TRUE
);
4920 cvd
->vdev_unspare
= B_TRUE
;
4924 * If the parent mirror/replacing vdev only has one child,
4925 * the parent is no longer needed. Remove it from the tree.
4927 if (pvd
->vdev_children
== 1) {
4928 if (pvd
->vdev_ops
== &vdev_spare_ops
)
4929 cvd
->vdev_unspare
= B_FALSE
;
4930 vdev_remove_parent(cvd
);
4935 * We don't set tvd until now because the parent we just removed
4936 * may have been the previous top-level vdev.
4938 tvd
= cvd
->vdev_top
;
4939 ASSERT(tvd
->vdev_parent
== rvd
);
4942 * Reevaluate the parent vdev state.
4944 vdev_propagate_state(cvd
);
4947 * If the 'autoexpand' property is set on the pool then automatically
4948 * try to expand the size of the pool. For example if the device we
4949 * just detached was smaller than the others, it may be possible to
4950 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
4951 * first so that we can obtain the updated sizes of the leaf vdevs.
4953 if (spa
->spa_autoexpand
) {
4955 vdev_expand(tvd
, txg
);
4958 vdev_config_dirty(tvd
);
4961 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
4962 * vd->vdev_detached is set and free vd's DTL object in syncing context.
4963 * But first make sure we're not on any *other* txg's DTL list, to
4964 * prevent vd from being accessed after it's freed.
4966 vdpath
= spa_strdup(vd
->vdev_path
? vd
->vdev_path
: "none");
4967 for (t
= 0; t
< TXG_SIZE
; t
++)
4968 (void) txg_list_remove_this(&tvd
->vdev_dtl_list
, vd
, t
);
4969 vd
->vdev_detached
= B_TRUE
;
4970 vdev_dirty(tvd
, VDD_DTL
, vd
, txg
);
4972 spa_event_notify(spa
, vd
, NULL
, ESC_ZFS_VDEV_REMOVE
);
4974 /* hang on to the spa before we release the lock */
4975 spa_open_ref(spa
, FTAG
);
4977 error
= spa_vdev_exit(spa
, vd
, txg
, 0);
4979 spa_history_log_internal(spa
, "detach", NULL
,
4981 spa_strfree(vdpath
);
4984 * If this was the removal of the original device in a hot spare vdev,
4985 * then we want to go through and remove the device from the hot spare
4986 * list of every other pool.
4989 spa_t
*altspa
= NULL
;
4991 mutex_enter(&spa_namespace_lock
);
4992 while ((altspa
= spa_next(altspa
)) != NULL
) {
4993 if (altspa
->spa_state
!= POOL_STATE_ACTIVE
||
4997 spa_open_ref(altspa
, FTAG
);
4998 mutex_exit(&spa_namespace_lock
);
4999 (void) spa_vdev_remove(altspa
, unspare_guid
, B_TRUE
);
5000 mutex_enter(&spa_namespace_lock
);
5001 spa_close(altspa
, FTAG
);
5003 mutex_exit(&spa_namespace_lock
);
5005 /* search the rest of the vdevs for spares to remove */
5006 spa_vdev_resilver_done(spa
);
5009 /* all done with the spa; OK to release */
5010 mutex_enter(&spa_namespace_lock
);
5011 spa_close(spa
, FTAG
);
5012 mutex_exit(&spa_namespace_lock
);
5018 * Split a set of devices from their mirrors, and create a new pool from them.
5021 spa_vdev_split_mirror(spa_t
*spa
, char *newname
, nvlist_t
*config
,
5022 nvlist_t
*props
, boolean_t exp
)
5025 uint64_t txg
, *glist
;
5027 uint_t c
, children
, lastlog
;
5028 nvlist_t
**child
, *nvl
, *tmp
;
5030 char *altroot
= NULL
;
5031 vdev_t
*rvd
, **vml
= NULL
; /* vdev modify list */
5032 boolean_t activate_slog
;
5034 ASSERT(spa_writeable(spa
));
5036 txg
= spa_vdev_enter(spa
);
5038 /* clear the log and flush everything up to now */
5039 activate_slog
= spa_passivate_log(spa
);
5040 (void) spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
5041 error
= spa_offline_log(spa
);
5042 txg
= spa_vdev_config_enter(spa
);
5045 spa_activate_log(spa
);
5048 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5050 /* check new spa name before going any further */
5051 if (spa_lookup(newname
) != NULL
)
5052 return (spa_vdev_exit(spa
, NULL
, txg
, EEXIST
));
5055 * scan through all the children to ensure they're all mirrors
5057 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvl
) != 0 ||
5058 nvlist_lookup_nvlist_array(nvl
, ZPOOL_CONFIG_CHILDREN
, &child
,
5060 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
5062 /* first, check to ensure we've got the right child count */
5063 rvd
= spa
->spa_root_vdev
;
5065 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
5066 vdev_t
*vd
= rvd
->vdev_child
[c
];
5068 /* don't count the holes & logs as children */
5069 if (vd
->vdev_islog
|| vd
->vdev_ishole
) {
5077 if (children
!= (lastlog
!= 0 ? lastlog
: rvd
->vdev_children
))
5078 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
5080 /* next, ensure no spare or cache devices are part of the split */
5081 if (nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_SPARES
, &tmp
) == 0 ||
5082 nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_L2CACHE
, &tmp
) == 0)
5083 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
5085 vml
= kmem_zalloc(children
* sizeof (vdev_t
*), KM_SLEEP
);
5086 glist
= kmem_zalloc(children
* sizeof (uint64_t), KM_SLEEP
);
5088 /* then, loop over each vdev and validate it */
5089 for (c
= 0; c
< children
; c
++) {
5090 uint64_t is_hole
= 0;
5092 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
5096 if (spa
->spa_root_vdev
->vdev_child
[c
]->vdev_ishole
||
5097 spa
->spa_root_vdev
->vdev_child
[c
]->vdev_islog
) {
5100 error
= SET_ERROR(EINVAL
);
5105 /* which disk is going to be split? */
5106 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_GUID
,
5108 error
= SET_ERROR(EINVAL
);
5112 /* look it up in the spa */
5113 vml
[c
] = spa_lookup_by_guid(spa
, glist
[c
], B_FALSE
);
5114 if (vml
[c
] == NULL
) {
5115 error
= SET_ERROR(ENODEV
);
5119 /* make sure there's nothing stopping the split */
5120 if (vml
[c
]->vdev_parent
->vdev_ops
!= &vdev_mirror_ops
||
5121 vml
[c
]->vdev_islog
||
5122 vml
[c
]->vdev_ishole
||
5123 vml
[c
]->vdev_isspare
||
5124 vml
[c
]->vdev_isl2cache
||
5125 !vdev_writeable(vml
[c
]) ||
5126 vml
[c
]->vdev_children
!= 0 ||
5127 vml
[c
]->vdev_state
!= VDEV_STATE_HEALTHY
||
5128 c
!= spa
->spa_root_vdev
->vdev_child
[c
]->vdev_id
) {
5129 error
= SET_ERROR(EINVAL
);
5133 if (vdev_dtl_required(vml
[c
])) {
5134 error
= SET_ERROR(EBUSY
);
5138 /* we need certain info from the top level */
5139 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_ARRAY
,
5140 vml
[c
]->vdev_top
->vdev_ms_array
) == 0);
5141 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_SHIFT
,
5142 vml
[c
]->vdev_top
->vdev_ms_shift
) == 0);
5143 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASIZE
,
5144 vml
[c
]->vdev_top
->vdev_asize
) == 0);
5145 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASHIFT
,
5146 vml
[c
]->vdev_top
->vdev_ashift
) == 0);
5148 /* transfer per-vdev ZAPs */
5149 ASSERT3U(vml
[c
]->vdev_leaf_zap
, !=, 0);
5150 VERIFY0(nvlist_add_uint64(child
[c
],
5151 ZPOOL_CONFIG_VDEV_LEAF_ZAP
, vml
[c
]->vdev_leaf_zap
));
5153 ASSERT3U(vml
[c
]->vdev_top
->vdev_top_zap
, !=, 0);
5154 VERIFY0(nvlist_add_uint64(child
[c
],
5155 ZPOOL_CONFIG_VDEV_TOP_ZAP
,
5156 vml
[c
]->vdev_parent
->vdev_top_zap
));
5160 kmem_free(vml
, children
* sizeof (vdev_t
*));
5161 kmem_free(glist
, children
* sizeof (uint64_t));
5162 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5165 /* stop writers from using the disks */
5166 for (c
= 0; c
< children
; c
++) {
5168 vml
[c
]->vdev_offline
= B_TRUE
;
5170 vdev_reopen(spa
->spa_root_vdev
);
5173 * Temporarily record the splitting vdevs in the spa config. This
5174 * will disappear once the config is regenerated.
5176 VERIFY(nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
5177 VERIFY(nvlist_add_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
5178 glist
, children
) == 0);
5179 kmem_free(glist
, children
* sizeof (uint64_t));
5181 mutex_enter(&spa
->spa_props_lock
);
5182 VERIFY(nvlist_add_nvlist(spa
->spa_config
, ZPOOL_CONFIG_SPLIT
,
5184 mutex_exit(&spa
->spa_props_lock
);
5185 spa
->spa_config_splitting
= nvl
;
5186 vdev_config_dirty(spa
->spa_root_vdev
);
5188 /* configure and create the new pool */
5189 VERIFY(nvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
, newname
) == 0);
5190 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
5191 exp
? POOL_STATE_EXPORTED
: POOL_STATE_ACTIVE
) == 0);
5192 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
5193 spa_version(spa
)) == 0);
5194 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
5195 spa
->spa_config_txg
) == 0);
5196 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
5197 spa_generate_guid(NULL
)) == 0);
5198 VERIFY0(nvlist_add_boolean(config
, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
));
5199 (void) nvlist_lookup_string(props
,
5200 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
5202 /* add the new pool to the namespace */
5203 newspa
= spa_add(newname
, config
, altroot
);
5204 newspa
->spa_avz_action
= AVZ_ACTION_REBUILD
;
5205 newspa
->spa_config_txg
= spa
->spa_config_txg
;
5206 spa_set_log_state(newspa
, SPA_LOG_CLEAR
);
5208 /* release the spa config lock, retaining the namespace lock */
5209 spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
5211 if (zio_injection_enabled
)
5212 zio_handle_panic_injection(spa
, FTAG
, 1);
5214 spa_activate(newspa
, spa_mode_global
);
5215 spa_async_suspend(newspa
);
5217 /* create the new pool from the disks of the original pool */
5218 error
= spa_load(newspa
, SPA_LOAD_IMPORT
, SPA_IMPORT_ASSEMBLE
, B_TRUE
);
5222 /* if that worked, generate a real config for the new pool */
5223 if (newspa
->spa_root_vdev
!= NULL
) {
5224 VERIFY(nvlist_alloc(&newspa
->spa_config_splitting
,
5225 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
5226 VERIFY(nvlist_add_uint64(newspa
->spa_config_splitting
,
5227 ZPOOL_CONFIG_SPLIT_GUID
, spa_guid(spa
)) == 0);
5228 spa_config_set(newspa
, spa_config_generate(newspa
, NULL
, -1ULL,
5233 if (props
!= NULL
) {
5234 spa_configfile_set(newspa
, props
, B_FALSE
);
5235 error
= spa_prop_set(newspa
, props
);
5240 /* flush everything */
5241 txg
= spa_vdev_config_enter(newspa
);
5242 vdev_config_dirty(newspa
->spa_root_vdev
);
5243 (void) spa_vdev_config_exit(newspa
, NULL
, txg
, 0, FTAG
);
5245 if (zio_injection_enabled
)
5246 zio_handle_panic_injection(spa
, FTAG
, 2);
5248 spa_async_resume(newspa
);
5250 /* finally, update the original pool's config */
5251 txg
= spa_vdev_config_enter(spa
);
5252 tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
5253 error
= dmu_tx_assign(tx
, TXG_WAIT
);
5256 for (c
= 0; c
< children
; c
++) {
5257 if (vml
[c
] != NULL
) {
5260 spa_history_log_internal(spa
, "detach", tx
,
5261 "vdev=%s", vml
[c
]->vdev_path
);
5266 spa
->spa_avz_action
= AVZ_ACTION_REBUILD
;
5267 vdev_config_dirty(spa
->spa_root_vdev
);
5268 spa
->spa_config_splitting
= NULL
;
5272 (void) spa_vdev_exit(spa
, NULL
, txg
, 0);
5274 if (zio_injection_enabled
)
5275 zio_handle_panic_injection(spa
, FTAG
, 3);
5277 /* split is complete; log a history record */
5278 spa_history_log_internal(newspa
, "split", NULL
,
5279 "from pool %s", spa_name(spa
));
5281 kmem_free(vml
, children
* sizeof (vdev_t
*));
5283 /* if we're not going to mount the filesystems in userland, export */
5285 error
= spa_export_common(newname
, POOL_STATE_EXPORTED
, NULL
,
5292 spa_deactivate(newspa
);
5295 txg
= spa_vdev_config_enter(spa
);
5297 /* re-online all offlined disks */
5298 for (c
= 0; c
< children
; c
++) {
5300 vml
[c
]->vdev_offline
= B_FALSE
;
5302 vdev_reopen(spa
->spa_root_vdev
);
5304 nvlist_free(spa
->spa_config_splitting
);
5305 spa
->spa_config_splitting
= NULL
;
5306 (void) spa_vdev_exit(spa
, NULL
, txg
, error
);
5308 kmem_free(vml
, children
* sizeof (vdev_t
*));
5313 spa_nvlist_lookup_by_guid(nvlist_t
**nvpp
, int count
, uint64_t target_guid
)
5317 for (i
= 0; i
< count
; i
++) {
5320 VERIFY(nvlist_lookup_uint64(nvpp
[i
], ZPOOL_CONFIG_GUID
,
5323 if (guid
== target_guid
)
5331 spa_vdev_remove_aux(nvlist_t
*config
, char *name
, nvlist_t
**dev
, int count
,
5332 nvlist_t
*dev_to_remove
)
5334 nvlist_t
**newdev
= NULL
;
5338 newdev
= kmem_alloc((count
- 1) * sizeof (void *), KM_SLEEP
);
5340 for (i
= 0, j
= 0; i
< count
; i
++) {
5341 if (dev
[i
] == dev_to_remove
)
5343 VERIFY(nvlist_dup(dev
[i
], &newdev
[j
++], KM_SLEEP
) == 0);
5346 VERIFY(nvlist_remove(config
, name
, DATA_TYPE_NVLIST_ARRAY
) == 0);
5347 VERIFY(nvlist_add_nvlist_array(config
, name
, newdev
, count
- 1) == 0);
5349 for (i
= 0; i
< count
- 1; i
++)
5350 nvlist_free(newdev
[i
]);
5353 kmem_free(newdev
, (count
- 1) * sizeof (void *));
5357 * Evacuate the device.
5360 spa_vdev_remove_evacuate(spa_t
*spa
, vdev_t
*vd
)
5365 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
5366 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
5367 ASSERT(vd
== vd
->vdev_top
);
5370 * Evacuate the device. We don't hold the config lock as writer
5371 * since we need to do I/O but we do keep the
5372 * spa_namespace_lock held. Once this completes the device
5373 * should no longer have any blocks allocated on it.
5375 if (vd
->vdev_islog
) {
5376 if (vd
->vdev_stat
.vs_alloc
!= 0)
5377 error
= spa_offline_log(spa
);
5379 error
= SET_ERROR(ENOTSUP
);
5386 * The evacuation succeeded. Remove any remaining MOS metadata
5387 * associated with this vdev, and wait for these changes to sync.
5389 ASSERT0(vd
->vdev_stat
.vs_alloc
);
5390 txg
= spa_vdev_config_enter(spa
);
5391 vd
->vdev_removing
= B_TRUE
;
5392 vdev_dirty_leaves(vd
, VDD_DTL
, txg
);
5393 vdev_config_dirty(vd
);
5394 spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
5400 * Complete the removal by cleaning up the namespace.
5403 spa_vdev_remove_from_namespace(spa_t
*spa
, vdev_t
*vd
)
5405 vdev_t
*rvd
= spa
->spa_root_vdev
;
5406 uint64_t id
= vd
->vdev_id
;
5407 boolean_t last_vdev
= (id
== (rvd
->vdev_children
- 1));
5409 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
5410 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
5411 ASSERT(vd
== vd
->vdev_top
);
5414 * Only remove any devices which are empty.
5416 if (vd
->vdev_stat
.vs_alloc
!= 0)
5419 (void) vdev_label_init(vd
, 0, VDEV_LABEL_REMOVE
);
5421 if (list_link_active(&vd
->vdev_state_dirty_node
))
5422 vdev_state_clean(vd
);
5423 if (list_link_active(&vd
->vdev_config_dirty_node
))
5424 vdev_config_clean(vd
);
5429 vdev_compact_children(rvd
);
5431 vd
= vdev_alloc_common(spa
, id
, 0, &vdev_hole_ops
);
5432 vdev_add_child(rvd
, vd
);
5434 vdev_config_dirty(rvd
);
5437 * Reassess the health of our root vdev.
5443 * Remove a device from the pool -
5445 * Removing a device from the vdev namespace requires several steps
5446 * and can take a significant amount of time. As a result we use
5447 * the spa_vdev_config_[enter/exit] functions which allow us to
5448 * grab and release the spa_config_lock while still holding the namespace
5449 * lock. During each step the configuration is synced out.
5451 * Currently, this supports removing only hot spares, slogs, and level 2 ARC
5455 spa_vdev_remove(spa_t
*spa
, uint64_t guid
, boolean_t unspare
)
5458 sysevent_t
*ev
= NULL
;
5459 metaslab_group_t
*mg
;
5460 nvlist_t
**spares
, **l2cache
, *nv
;
5462 uint_t nspares
, nl2cache
;
5464 boolean_t locked
= MUTEX_HELD(&spa_namespace_lock
);
5466 ASSERT(spa_writeable(spa
));
5469 txg
= spa_vdev_enter(spa
);
5471 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
5473 if (spa
->spa_spares
.sav_vdevs
!= NULL
&&
5474 nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
5475 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0 &&
5476 (nv
= spa_nvlist_lookup_by_guid(spares
, nspares
, guid
)) != NULL
) {
5478 * Only remove the hot spare if it's not currently in use
5481 if (vd
== NULL
|| unspare
) {
5483 vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
);
5484 ev
= spa_event_create(spa
, vd
, NULL
,
5485 ESC_ZFS_VDEV_REMOVE_AUX
);
5486 spa_vdev_remove_aux(spa
->spa_spares
.sav_config
,
5487 ZPOOL_CONFIG_SPARES
, spares
, nspares
, nv
);
5488 spa_load_spares(spa
);
5489 spa
->spa_spares
.sav_sync
= B_TRUE
;
5491 error
= SET_ERROR(EBUSY
);
5493 } else if (spa
->spa_l2cache
.sav_vdevs
!= NULL
&&
5494 nvlist_lookup_nvlist_array(spa
->spa_l2cache
.sav_config
,
5495 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0 &&
5496 (nv
= spa_nvlist_lookup_by_guid(l2cache
, nl2cache
, guid
)) != NULL
) {
5498 * Cache devices can always be removed.
5500 vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
);
5501 ev
= spa_event_create(spa
, vd
, NULL
, ESC_ZFS_VDEV_REMOVE_AUX
);
5502 spa_vdev_remove_aux(spa
->spa_l2cache
.sav_config
,
5503 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
, nv
);
5504 spa_load_l2cache(spa
);
5505 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
5506 } else if (vd
!= NULL
&& vd
->vdev_islog
) {
5508 ASSERT(vd
== vd
->vdev_top
);
5513 * Stop allocating from this vdev.
5515 metaslab_group_passivate(mg
);
5518 * Wait for the youngest allocations and frees to sync,
5519 * and then wait for the deferral of those frees to finish.
5521 spa_vdev_config_exit(spa
, NULL
,
5522 txg
+ TXG_CONCURRENT_STATES
+ TXG_DEFER_SIZE
, 0, FTAG
);
5525 * Attempt to evacuate the vdev.
5527 error
= spa_vdev_remove_evacuate(spa
, vd
);
5529 txg
= spa_vdev_config_enter(spa
);
5532 * If we couldn't evacuate the vdev, unwind.
5535 metaslab_group_activate(mg
);
5536 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5540 * Clean up the vdev namespace.
5542 ev
= spa_event_create(spa
, vd
, NULL
, ESC_ZFS_VDEV_REMOVE_DEV
);
5543 spa_vdev_remove_from_namespace(spa
, vd
);
5545 } else if (vd
!= NULL
) {
5547 * Normal vdevs cannot be removed (yet).
5549 error
= SET_ERROR(ENOTSUP
);
5552 * There is no vdev of any kind with the specified guid.
5554 error
= SET_ERROR(ENOENT
);
5558 error
= spa_vdev_exit(spa
, NULL
, txg
, error
);
5567 * Find any device that's done replacing, or a vdev marked 'unspare' that's
5568 * currently spared, so we can detach it.
5571 spa_vdev_resilver_done_hunt(vdev_t
*vd
)
5573 vdev_t
*newvd
, *oldvd
;
5576 for (c
= 0; c
< vd
->vdev_children
; c
++) {
5577 oldvd
= spa_vdev_resilver_done_hunt(vd
->vdev_child
[c
]);
5583 * Check for a completed replacement. We always consider the first
5584 * vdev in the list to be the oldest vdev, and the last one to be
5585 * the newest (see spa_vdev_attach() for how that works). In
5586 * the case where the newest vdev is faulted, we will not automatically
5587 * remove it after a resilver completes. This is OK as it will require
5588 * user intervention to determine which disk the admin wishes to keep.
5590 if (vd
->vdev_ops
== &vdev_replacing_ops
) {
5591 ASSERT(vd
->vdev_children
> 1);
5593 newvd
= vd
->vdev_child
[vd
->vdev_children
- 1];
5594 oldvd
= vd
->vdev_child
[0];
5596 if (vdev_dtl_empty(newvd
, DTL_MISSING
) &&
5597 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
5598 !vdev_dtl_required(oldvd
))
5603 * Check for a completed resilver with the 'unspare' flag set.
5605 if (vd
->vdev_ops
== &vdev_spare_ops
) {
5606 vdev_t
*first
= vd
->vdev_child
[0];
5607 vdev_t
*last
= vd
->vdev_child
[vd
->vdev_children
- 1];
5609 if (last
->vdev_unspare
) {
5612 } else if (first
->vdev_unspare
) {
5619 if (oldvd
!= NULL
&&
5620 vdev_dtl_empty(newvd
, DTL_MISSING
) &&
5621 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
5622 !vdev_dtl_required(oldvd
))
5626 * If there are more than two spares attached to a disk,
5627 * and those spares are not required, then we want to
5628 * attempt to free them up now so that they can be used
5629 * by other pools. Once we're back down to a single
5630 * disk+spare, we stop removing them.
5632 if (vd
->vdev_children
> 2) {
5633 newvd
= vd
->vdev_child
[1];
5635 if (newvd
->vdev_isspare
&& last
->vdev_isspare
&&
5636 vdev_dtl_empty(last
, DTL_MISSING
) &&
5637 vdev_dtl_empty(last
, DTL_OUTAGE
) &&
5638 !vdev_dtl_required(newvd
))
5647 spa_vdev_resilver_done(spa_t
*spa
)
5649 vdev_t
*vd
, *pvd
, *ppvd
;
5650 uint64_t guid
, sguid
, pguid
, ppguid
;
5652 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5654 while ((vd
= spa_vdev_resilver_done_hunt(spa
->spa_root_vdev
)) != NULL
) {
5655 pvd
= vd
->vdev_parent
;
5656 ppvd
= pvd
->vdev_parent
;
5657 guid
= vd
->vdev_guid
;
5658 pguid
= pvd
->vdev_guid
;
5659 ppguid
= ppvd
->vdev_guid
;
5662 * If we have just finished replacing a hot spared device, then
5663 * we need to detach the parent's first child (the original hot
5666 if (ppvd
->vdev_ops
== &vdev_spare_ops
&& pvd
->vdev_id
== 0 &&
5667 ppvd
->vdev_children
== 2) {
5668 ASSERT(pvd
->vdev_ops
== &vdev_replacing_ops
);
5669 sguid
= ppvd
->vdev_child
[1]->vdev_guid
;
5671 ASSERT(vd
->vdev_resilver_txg
== 0 || !vdev_dtl_required(vd
));
5673 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5674 if (spa_vdev_detach(spa
, guid
, pguid
, B_TRUE
) != 0)
5676 if (sguid
&& spa_vdev_detach(spa
, sguid
, ppguid
, B_TRUE
) != 0)
5678 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5681 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5685 * Update the stored path or FRU for this vdev.
5688 spa_vdev_set_common(spa_t
*spa
, uint64_t guid
, const char *value
,
5692 boolean_t sync
= B_FALSE
;
5694 ASSERT(spa_writeable(spa
));
5696 spa_vdev_state_enter(spa
, SCL_ALL
);
5698 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
5699 return (spa_vdev_state_exit(spa
, NULL
, ENOENT
));
5701 if (!vd
->vdev_ops
->vdev_op_leaf
)
5702 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
5705 if (strcmp(value
, vd
->vdev_path
) != 0) {
5706 spa_strfree(vd
->vdev_path
);
5707 vd
->vdev_path
= spa_strdup(value
);
5711 if (vd
->vdev_fru
== NULL
) {
5712 vd
->vdev_fru
= spa_strdup(value
);
5714 } else if (strcmp(value
, vd
->vdev_fru
) != 0) {
5715 spa_strfree(vd
->vdev_fru
);
5716 vd
->vdev_fru
= spa_strdup(value
);
5721 return (spa_vdev_state_exit(spa
, sync
? vd
: NULL
, 0));
5725 spa_vdev_setpath(spa_t
*spa
, uint64_t guid
, const char *newpath
)
5727 return (spa_vdev_set_common(spa
, guid
, newpath
, B_TRUE
));
5731 spa_vdev_setfru(spa_t
*spa
, uint64_t guid
, const char *newfru
)
5733 return (spa_vdev_set_common(spa
, guid
, newfru
, B_FALSE
));
5737 * ==========================================================================
5739 * ==========================================================================
5742 spa_scrub_pause_resume(spa_t
*spa
, pool_scrub_cmd_t cmd
)
5744 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
5746 if (dsl_scan_resilvering(spa
->spa_dsl_pool
))
5747 return (SET_ERROR(EBUSY
));
5749 return (dsl_scrub_set_pause_resume(spa
->spa_dsl_pool
, cmd
));
5753 spa_scan_stop(spa_t
*spa
)
5755 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
5756 if (dsl_scan_resilvering(spa
->spa_dsl_pool
))
5757 return (SET_ERROR(EBUSY
));
5758 return (dsl_scan_cancel(spa
->spa_dsl_pool
));
5762 spa_scan(spa_t
*spa
, pool_scan_func_t func
)
5764 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
5766 if (func
>= POOL_SCAN_FUNCS
|| func
== POOL_SCAN_NONE
)
5767 return (SET_ERROR(ENOTSUP
));
5770 * If a resilver was requested, but there is no DTL on a
5771 * writeable leaf device, we have nothing to do.
5773 if (func
== POOL_SCAN_RESILVER
&&
5774 !vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
)) {
5775 spa_async_request(spa
, SPA_ASYNC_RESILVER_DONE
);
5779 return (dsl_scan(spa
->spa_dsl_pool
, func
));
5783 * ==========================================================================
5784 * SPA async task processing
5785 * ==========================================================================
5789 spa_async_remove(spa_t
*spa
, vdev_t
*vd
)
5793 if (vd
->vdev_remove_wanted
) {
5794 vd
->vdev_remove_wanted
= B_FALSE
;
5795 vd
->vdev_delayed_close
= B_FALSE
;
5796 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_REMOVED
, VDEV_AUX_NONE
);
5799 * We want to clear the stats, but we don't want to do a full
5800 * vdev_clear() as that will cause us to throw away
5801 * degraded/faulted state as well as attempt to reopen the
5802 * device, all of which is a waste.
5804 vd
->vdev_stat
.vs_read_errors
= 0;
5805 vd
->vdev_stat
.vs_write_errors
= 0;
5806 vd
->vdev_stat
.vs_checksum_errors
= 0;
5808 vdev_state_dirty(vd
->vdev_top
);
5811 for (c
= 0; c
< vd
->vdev_children
; c
++)
5812 spa_async_remove(spa
, vd
->vdev_child
[c
]);
5816 spa_async_probe(spa_t
*spa
, vdev_t
*vd
)
5820 if (vd
->vdev_probe_wanted
) {
5821 vd
->vdev_probe_wanted
= B_FALSE
;
5822 vdev_reopen(vd
); /* vdev_open() does the actual probe */
5825 for (c
= 0; c
< vd
->vdev_children
; c
++)
5826 spa_async_probe(spa
, vd
->vdev_child
[c
]);
5830 spa_async_autoexpand(spa_t
*spa
, vdev_t
*vd
)
5834 if (!spa
->spa_autoexpand
)
5837 for (c
= 0; c
< vd
->vdev_children
; c
++) {
5838 vdev_t
*cvd
= vd
->vdev_child
[c
];
5839 spa_async_autoexpand(spa
, cvd
);
5842 if (!vd
->vdev_ops
->vdev_op_leaf
|| vd
->vdev_physpath
== NULL
)
5845 spa_event_notify(vd
->vdev_spa
, vd
, NULL
, ESC_ZFS_VDEV_AUTOEXPAND
);
5849 spa_async_thread(spa_t
*spa
)
5853 ASSERT(spa
->spa_sync_on
);
5855 mutex_enter(&spa
->spa_async_lock
);
5856 tasks
= spa
->spa_async_tasks
;
5857 spa
->spa_async_tasks
= 0;
5858 mutex_exit(&spa
->spa_async_lock
);
5861 * See if the config needs to be updated.
5863 if (tasks
& SPA_ASYNC_CONFIG_UPDATE
) {
5864 uint64_t old_space
, new_space
;
5866 mutex_enter(&spa_namespace_lock
);
5867 old_space
= metaslab_class_get_space(spa_normal_class(spa
));
5868 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
5869 new_space
= metaslab_class_get_space(spa_normal_class(spa
));
5870 mutex_exit(&spa_namespace_lock
);
5873 * If the pool grew as a result of the config update,
5874 * then log an internal history event.
5876 if (new_space
!= old_space
) {
5877 spa_history_log_internal(spa
, "vdev online", NULL
,
5878 "pool '%s' size: %llu(+%llu)",
5879 spa_name(spa
), new_space
, new_space
- old_space
);
5884 * See if any devices need to be marked REMOVED.
5886 if (tasks
& SPA_ASYNC_REMOVE
) {
5887 spa_vdev_state_enter(spa
, SCL_NONE
);
5888 spa_async_remove(spa
, spa
->spa_root_vdev
);
5889 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++)
5890 spa_async_remove(spa
, spa
->spa_l2cache
.sav_vdevs
[i
]);
5891 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
5892 spa_async_remove(spa
, spa
->spa_spares
.sav_vdevs
[i
]);
5893 (void) spa_vdev_state_exit(spa
, NULL
, 0);
5896 if ((tasks
& SPA_ASYNC_AUTOEXPAND
) && !spa_suspended(spa
)) {
5897 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
5898 spa_async_autoexpand(spa
, spa
->spa_root_vdev
);
5899 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
5903 * See if any devices need to be probed.
5905 if (tasks
& SPA_ASYNC_PROBE
) {
5906 spa_vdev_state_enter(spa
, SCL_NONE
);
5907 spa_async_probe(spa
, spa
->spa_root_vdev
);
5908 (void) spa_vdev_state_exit(spa
, NULL
, 0);
5912 * If any devices are done replacing, detach them.
5914 if (tasks
& SPA_ASYNC_RESILVER_DONE
)
5915 spa_vdev_resilver_done(spa
);
5918 * Kick off a resilver.
5920 if (tasks
& SPA_ASYNC_RESILVER
)
5921 dsl_resilver_restart(spa
->spa_dsl_pool
, 0);
5924 * Let the world know that we're done.
5926 mutex_enter(&spa
->spa_async_lock
);
5927 spa
->spa_async_thread
= NULL
;
5928 cv_broadcast(&spa
->spa_async_cv
);
5929 mutex_exit(&spa
->spa_async_lock
);
5934 spa_async_suspend(spa_t
*spa
)
5936 mutex_enter(&spa
->spa_async_lock
);
5937 spa
->spa_async_suspended
++;
5938 while (spa
->spa_async_thread
!= NULL
)
5939 cv_wait(&spa
->spa_async_cv
, &spa
->spa_async_lock
);
5940 mutex_exit(&spa
->spa_async_lock
);
5944 spa_async_resume(spa_t
*spa
)
5946 mutex_enter(&spa
->spa_async_lock
);
5947 ASSERT(spa
->spa_async_suspended
!= 0);
5948 spa
->spa_async_suspended
--;
5949 mutex_exit(&spa
->spa_async_lock
);
5953 spa_async_tasks_pending(spa_t
*spa
)
5955 uint_t non_config_tasks
;
5957 boolean_t config_task_suspended
;
5959 non_config_tasks
= spa
->spa_async_tasks
& ~SPA_ASYNC_CONFIG_UPDATE
;
5960 config_task
= spa
->spa_async_tasks
& SPA_ASYNC_CONFIG_UPDATE
;
5961 if (spa
->spa_ccw_fail_time
== 0) {
5962 config_task_suspended
= B_FALSE
;
5964 config_task_suspended
=
5965 (gethrtime() - spa
->spa_ccw_fail_time
) <
5966 ((hrtime_t
)zfs_ccw_retry_interval
* NANOSEC
);
5969 return (non_config_tasks
|| (config_task
&& !config_task_suspended
));
5973 spa_async_dispatch(spa_t
*spa
)
5975 mutex_enter(&spa
->spa_async_lock
);
5976 if (spa_async_tasks_pending(spa
) &&
5977 !spa
->spa_async_suspended
&&
5978 spa
->spa_async_thread
== NULL
&&
5980 spa
->spa_async_thread
= thread_create(NULL
, 0,
5981 spa_async_thread
, spa
, 0, &p0
, TS_RUN
, maxclsyspri
);
5982 mutex_exit(&spa
->spa_async_lock
);
5986 spa_async_request(spa_t
*spa
, int task
)
5988 zfs_dbgmsg("spa=%s async request task=%u", spa
->spa_name
, task
);
5989 mutex_enter(&spa
->spa_async_lock
);
5990 spa
->spa_async_tasks
|= task
;
5991 mutex_exit(&spa
->spa_async_lock
);
5995 * ==========================================================================
5996 * SPA syncing routines
5997 * ==========================================================================
6001 bpobj_enqueue_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
6004 bpobj_enqueue(bpo
, bp
, tx
);
6009 spa_free_sync_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
6013 zio_nowait(zio_free_sync(zio
, zio
->io_spa
, dmu_tx_get_txg(tx
), bp
,
6019 * Note: this simple function is not inlined to make it easier to dtrace the
6020 * amount of time spent syncing frees.
6023 spa_sync_frees(spa_t
*spa
, bplist_t
*bpl
, dmu_tx_t
*tx
)
6025 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
6026 bplist_iterate(bpl
, spa_free_sync_cb
, zio
, tx
);
6027 VERIFY(zio_wait(zio
) == 0);
6031 * Note: this simple function is not inlined to make it easier to dtrace the
6032 * amount of time spent syncing deferred frees.
6035 spa_sync_deferred_frees(spa_t
*spa
, dmu_tx_t
*tx
)
6037 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
6038 VERIFY3U(bpobj_iterate(&spa
->spa_deferred_bpobj
,
6039 spa_free_sync_cb
, zio
, tx
), ==, 0);
6040 VERIFY0(zio_wait(zio
));
6044 spa_sync_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
*nv
, dmu_tx_t
*tx
)
6046 char *packed
= NULL
;
6051 VERIFY(nvlist_size(nv
, &nvsize
, NV_ENCODE_XDR
) == 0);
6054 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
6055 * information. This avoids the dmu_buf_will_dirty() path and
6056 * saves us a pre-read to get data we don't actually care about.
6058 bufsize
= P2ROUNDUP((uint64_t)nvsize
, SPA_CONFIG_BLOCKSIZE
);
6059 packed
= vmem_alloc(bufsize
, KM_SLEEP
);
6061 VERIFY(nvlist_pack(nv
, &packed
, &nvsize
, NV_ENCODE_XDR
,
6063 bzero(packed
+ nvsize
, bufsize
- nvsize
);
6065 dmu_write(spa
->spa_meta_objset
, obj
, 0, bufsize
, packed
, tx
);
6067 vmem_free(packed
, bufsize
);
6069 VERIFY(0 == dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
));
6070 dmu_buf_will_dirty(db
, tx
);
6071 *(uint64_t *)db
->db_data
= nvsize
;
6072 dmu_buf_rele(db
, FTAG
);
6076 spa_sync_aux_dev(spa_t
*spa
, spa_aux_vdev_t
*sav
, dmu_tx_t
*tx
,
6077 const char *config
, const char *entry
)
6087 * Update the MOS nvlist describing the list of available devices.
6088 * spa_validate_aux() will have already made sure this nvlist is
6089 * valid and the vdevs are labeled appropriately.
6091 if (sav
->sav_object
== 0) {
6092 sav
->sav_object
= dmu_object_alloc(spa
->spa_meta_objset
,
6093 DMU_OT_PACKED_NVLIST
, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE
,
6094 sizeof (uint64_t), tx
);
6095 VERIFY(zap_update(spa
->spa_meta_objset
,
6096 DMU_POOL_DIRECTORY_OBJECT
, entry
, sizeof (uint64_t), 1,
6097 &sav
->sav_object
, tx
) == 0);
6100 VERIFY(nvlist_alloc(&nvroot
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
6101 if (sav
->sav_count
== 0) {
6102 VERIFY(nvlist_add_nvlist_array(nvroot
, config
, NULL
, 0) == 0);
6104 list
= kmem_alloc(sav
->sav_count
*sizeof (void *), KM_SLEEP
);
6105 for (i
= 0; i
< sav
->sav_count
; i
++)
6106 list
[i
] = vdev_config_generate(spa
, sav
->sav_vdevs
[i
],
6107 B_FALSE
, VDEV_CONFIG_L2CACHE
);
6108 VERIFY(nvlist_add_nvlist_array(nvroot
, config
, list
,
6109 sav
->sav_count
) == 0);
6110 for (i
= 0; i
< sav
->sav_count
; i
++)
6111 nvlist_free(list
[i
]);
6112 kmem_free(list
, sav
->sav_count
* sizeof (void *));
6115 spa_sync_nvlist(spa
, sav
->sav_object
, nvroot
, tx
);
6116 nvlist_free(nvroot
);
6118 sav
->sav_sync
= B_FALSE
;
6122 * Rebuild spa's all-vdev ZAP from the vdev ZAPs indicated in each vdev_t.
6123 * The all-vdev ZAP must be empty.
6126 spa_avz_build(vdev_t
*vd
, uint64_t avz
, dmu_tx_t
*tx
)
6128 spa_t
*spa
= vd
->vdev_spa
;
6131 if (vd
->vdev_top_zap
!= 0) {
6132 VERIFY0(zap_add_int(spa
->spa_meta_objset
, avz
,
6133 vd
->vdev_top_zap
, tx
));
6135 if (vd
->vdev_leaf_zap
!= 0) {
6136 VERIFY0(zap_add_int(spa
->spa_meta_objset
, avz
,
6137 vd
->vdev_leaf_zap
, tx
));
6139 for (i
= 0; i
< vd
->vdev_children
; i
++) {
6140 spa_avz_build(vd
->vdev_child
[i
], avz
, tx
);
6145 spa_sync_config_object(spa_t
*spa
, dmu_tx_t
*tx
)
6150 * If the pool is being imported from a pre-per-vdev-ZAP version of ZFS,
6151 * its config may not be dirty but we still need to build per-vdev ZAPs.
6152 * Similarly, if the pool is being assembled (e.g. after a split), we
6153 * need to rebuild the AVZ although the config may not be dirty.
6155 if (list_is_empty(&spa
->spa_config_dirty_list
) &&
6156 spa
->spa_avz_action
== AVZ_ACTION_NONE
)
6159 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
6161 ASSERT(spa
->spa_avz_action
== AVZ_ACTION_NONE
||
6162 spa
->spa_avz_action
== AVZ_ACTION_INITIALIZE
||
6163 spa
->spa_all_vdev_zaps
!= 0);
6165 if (spa
->spa_avz_action
== AVZ_ACTION_REBUILD
) {
6169 /* Make and build the new AVZ */
6170 uint64_t new_avz
= zap_create(spa
->spa_meta_objset
,
6171 DMU_OTN_ZAP_METADATA
, DMU_OT_NONE
, 0, tx
);
6172 spa_avz_build(spa
->spa_root_vdev
, new_avz
, tx
);
6174 /* Diff old AVZ with new one */
6175 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
6176 spa
->spa_all_vdev_zaps
);
6177 zap_cursor_retrieve(&zc
, &za
) == 0;
6178 zap_cursor_advance(&zc
)) {
6179 uint64_t vdzap
= za
.za_first_integer
;
6180 if (zap_lookup_int(spa
->spa_meta_objset
, new_avz
,
6183 * ZAP is listed in old AVZ but not in new one;
6186 VERIFY0(zap_destroy(spa
->spa_meta_objset
, vdzap
,
6191 zap_cursor_fini(&zc
);
6193 /* Destroy the old AVZ */
6194 VERIFY0(zap_destroy(spa
->spa_meta_objset
,
6195 spa
->spa_all_vdev_zaps
, tx
));
6197 /* Replace the old AVZ in the dir obj with the new one */
6198 VERIFY0(zap_update(spa
->spa_meta_objset
,
6199 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_VDEV_ZAP_MAP
,
6200 sizeof (new_avz
), 1, &new_avz
, tx
));
6202 spa
->spa_all_vdev_zaps
= new_avz
;
6203 } else if (spa
->spa_avz_action
== AVZ_ACTION_DESTROY
) {
6207 /* Walk through the AVZ and destroy all listed ZAPs */
6208 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
6209 spa
->spa_all_vdev_zaps
);
6210 zap_cursor_retrieve(&zc
, &za
) == 0;
6211 zap_cursor_advance(&zc
)) {
6212 uint64_t zap
= za
.za_first_integer
;
6213 VERIFY0(zap_destroy(spa
->spa_meta_objset
, zap
, tx
));
6216 zap_cursor_fini(&zc
);
6218 /* Destroy and unlink the AVZ itself */
6219 VERIFY0(zap_destroy(spa
->spa_meta_objset
,
6220 spa
->spa_all_vdev_zaps
, tx
));
6221 VERIFY0(zap_remove(spa
->spa_meta_objset
,
6222 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_VDEV_ZAP_MAP
, tx
));
6223 spa
->spa_all_vdev_zaps
= 0;
6226 if (spa
->spa_all_vdev_zaps
== 0) {
6227 spa
->spa_all_vdev_zaps
= zap_create_link(spa
->spa_meta_objset
,
6228 DMU_OTN_ZAP_METADATA
, DMU_POOL_DIRECTORY_OBJECT
,
6229 DMU_POOL_VDEV_ZAP_MAP
, tx
);
6231 spa
->spa_avz_action
= AVZ_ACTION_NONE
;
6233 /* Create ZAPs for vdevs that don't have them. */
6234 vdev_construct_zaps(spa
->spa_root_vdev
, tx
);
6236 config
= spa_config_generate(spa
, spa
->spa_root_vdev
,
6237 dmu_tx_get_txg(tx
), B_FALSE
);
6240 * If we're upgrading the spa version then make sure that
6241 * the config object gets updated with the correct version.
6243 if (spa
->spa_ubsync
.ub_version
< spa
->spa_uberblock
.ub_version
)
6244 fnvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
6245 spa
->spa_uberblock
.ub_version
);
6247 spa_config_exit(spa
, SCL_STATE
, FTAG
);
6249 nvlist_free(spa
->spa_config_syncing
);
6250 spa
->spa_config_syncing
= config
;
6252 spa_sync_nvlist(spa
, spa
->spa_config_object
, config
, tx
);
6256 spa_sync_version(void *arg
, dmu_tx_t
*tx
)
6258 uint64_t *versionp
= arg
;
6259 uint64_t version
= *versionp
;
6260 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
6263 * Setting the version is special cased when first creating the pool.
6265 ASSERT(tx
->tx_txg
!= TXG_INITIAL
);
6267 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
6268 ASSERT(version
>= spa_version(spa
));
6270 spa
->spa_uberblock
.ub_version
= version
;
6271 vdev_config_dirty(spa
->spa_root_vdev
);
6272 spa_history_log_internal(spa
, "set", tx
, "version=%lld", version
);
6276 * Set zpool properties.
6279 spa_sync_props(void *arg
, dmu_tx_t
*tx
)
6281 nvlist_t
*nvp
= arg
;
6282 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
6283 objset_t
*mos
= spa
->spa_meta_objset
;
6284 nvpair_t
*elem
= NULL
;
6286 mutex_enter(&spa
->spa_props_lock
);
6288 while ((elem
= nvlist_next_nvpair(nvp
, elem
))) {
6290 char *strval
, *fname
;
6292 const char *propname
;
6293 zprop_type_t proptype
;
6296 prop
= zpool_name_to_prop(nvpair_name(elem
));
6297 switch ((int)prop
) {
6300 * We checked this earlier in spa_prop_validate().
6302 ASSERT(zpool_prop_feature(nvpair_name(elem
)));
6304 fname
= strchr(nvpair_name(elem
), '@') + 1;
6305 VERIFY0(zfeature_lookup_name(fname
, &fid
));
6307 spa_feature_enable(spa
, fid
, tx
);
6308 spa_history_log_internal(spa
, "set", tx
,
6309 "%s=enabled", nvpair_name(elem
));
6312 case ZPOOL_PROP_VERSION
:
6313 intval
= fnvpair_value_uint64(elem
);
6315 * The version is synced separately before other
6316 * properties and should be correct by now.
6318 ASSERT3U(spa_version(spa
), >=, intval
);
6321 case ZPOOL_PROP_ALTROOT
:
6323 * 'altroot' is a non-persistent property. It should
6324 * have been set temporarily at creation or import time.
6326 ASSERT(spa
->spa_root
!= NULL
);
6329 case ZPOOL_PROP_READONLY
:
6330 case ZPOOL_PROP_CACHEFILE
:
6332 * 'readonly' and 'cachefile' are also non-persisitent
6336 case ZPOOL_PROP_COMMENT
:
6337 strval
= fnvpair_value_string(elem
);
6338 if (spa
->spa_comment
!= NULL
)
6339 spa_strfree(spa
->spa_comment
);
6340 spa
->spa_comment
= spa_strdup(strval
);
6342 * We need to dirty the configuration on all the vdevs
6343 * so that their labels get updated. It's unnecessary
6344 * to do this for pool creation since the vdev's
6345 * configuration has already been dirtied.
6347 if (tx
->tx_txg
!= TXG_INITIAL
)
6348 vdev_config_dirty(spa
->spa_root_vdev
);
6349 spa_history_log_internal(spa
, "set", tx
,
6350 "%s=%s", nvpair_name(elem
), strval
);
6354 * Set pool property values in the poolprops mos object.
6356 if (spa
->spa_pool_props_object
== 0) {
6357 spa
->spa_pool_props_object
=
6358 zap_create_link(mos
, DMU_OT_POOL_PROPS
,
6359 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_PROPS
,
6363 /* normalize the property name */
6364 propname
= zpool_prop_to_name(prop
);
6365 proptype
= zpool_prop_get_type(prop
);
6367 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
6368 ASSERT(proptype
== PROP_TYPE_STRING
);
6369 strval
= fnvpair_value_string(elem
);
6370 VERIFY0(zap_update(mos
,
6371 spa
->spa_pool_props_object
, propname
,
6372 1, strlen(strval
) + 1, strval
, tx
));
6373 spa_history_log_internal(spa
, "set", tx
,
6374 "%s=%s", nvpair_name(elem
), strval
);
6375 } else if (nvpair_type(elem
) == DATA_TYPE_UINT64
) {
6376 intval
= fnvpair_value_uint64(elem
);
6378 if (proptype
== PROP_TYPE_INDEX
) {
6380 VERIFY0(zpool_prop_index_to_string(
6381 prop
, intval
, &unused
));
6383 VERIFY0(zap_update(mos
,
6384 spa
->spa_pool_props_object
, propname
,
6385 8, 1, &intval
, tx
));
6386 spa_history_log_internal(spa
, "set", tx
,
6387 "%s=%lld", nvpair_name(elem
), intval
);
6389 ASSERT(0); /* not allowed */
6393 case ZPOOL_PROP_DELEGATION
:
6394 spa
->spa_delegation
= intval
;
6396 case ZPOOL_PROP_BOOTFS
:
6397 spa
->spa_bootfs
= intval
;
6399 case ZPOOL_PROP_FAILUREMODE
:
6400 spa
->spa_failmode
= intval
;
6402 case ZPOOL_PROP_AUTOEXPAND
:
6403 spa
->spa_autoexpand
= intval
;
6404 if (tx
->tx_txg
!= TXG_INITIAL
)
6405 spa_async_request(spa
,
6406 SPA_ASYNC_AUTOEXPAND
);
6408 case ZPOOL_PROP_DEDUPDITTO
:
6409 spa
->spa_dedup_ditto
= intval
;
6418 mutex_exit(&spa
->spa_props_lock
);
6422 * Perform one-time upgrade on-disk changes. spa_version() does not
6423 * reflect the new version this txg, so there must be no changes this
6424 * txg to anything that the upgrade code depends on after it executes.
6425 * Therefore this must be called after dsl_pool_sync() does the sync
6429 spa_sync_upgrades(spa_t
*spa
, dmu_tx_t
*tx
)
6431 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
6433 ASSERT(spa
->spa_sync_pass
== 1);
6435 rrw_enter(&dp
->dp_config_rwlock
, RW_WRITER
, FTAG
);
6437 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_ORIGIN
&&
6438 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_ORIGIN
) {
6439 dsl_pool_create_origin(dp
, tx
);
6441 /* Keeping the origin open increases spa_minref */
6442 spa
->spa_minref
+= 3;
6445 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_NEXT_CLONES
&&
6446 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_NEXT_CLONES
) {
6447 dsl_pool_upgrade_clones(dp
, tx
);
6450 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_DIR_CLONES
&&
6451 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_DIR_CLONES
) {
6452 dsl_pool_upgrade_dir_clones(dp
, tx
);
6454 /* Keeping the freedir open increases spa_minref */
6455 spa
->spa_minref
+= 3;
6458 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_FEATURES
&&
6459 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
6460 spa_feature_create_zap_objects(spa
, tx
);
6464 * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable
6465 * when possibility to use lz4 compression for metadata was added
6466 * Old pools that have this feature enabled must be upgraded to have
6467 * this feature active
6469 if (spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
6470 boolean_t lz4_en
= spa_feature_is_enabled(spa
,
6471 SPA_FEATURE_LZ4_COMPRESS
);
6472 boolean_t lz4_ac
= spa_feature_is_active(spa
,
6473 SPA_FEATURE_LZ4_COMPRESS
);
6475 if (lz4_en
&& !lz4_ac
)
6476 spa_feature_incr(spa
, SPA_FEATURE_LZ4_COMPRESS
, tx
);
6480 * If we haven't written the salt, do so now. Note that the
6481 * feature may not be activated yet, but that's fine since
6482 * the presence of this ZAP entry is backwards compatible.
6484 if (zap_contains(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
6485 DMU_POOL_CHECKSUM_SALT
) == ENOENT
) {
6486 VERIFY0(zap_add(spa
->spa_meta_objset
,
6487 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CHECKSUM_SALT
, 1,
6488 sizeof (spa
->spa_cksum_salt
.zcs_bytes
),
6489 spa
->spa_cksum_salt
.zcs_bytes
, tx
));
6492 rrw_exit(&dp
->dp_config_rwlock
, FTAG
);
6496 * Sync the specified transaction group. New blocks may be dirtied as
6497 * part of the process, so we iterate until it converges.
6500 spa_sync(spa_t
*spa
, uint64_t txg
)
6502 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
6503 objset_t
*mos
= spa
->spa_meta_objset
;
6504 bplist_t
*free_bpl
= &spa
->spa_free_bplist
[txg
& TXG_MASK
];
6505 metaslab_class_t
*mc
;
6506 vdev_t
*rvd
= spa
->spa_root_vdev
;
6510 uint32_t max_queue_depth
= zfs_vdev_async_write_max_active
*
6511 zfs_vdev_queue_depth_pct
/ 100;
6512 uint64_t queue_depth_total
;
6515 VERIFY(spa_writeable(spa
));
6518 * Lock out configuration changes.
6520 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
6522 spa
->spa_syncing_txg
= txg
;
6523 spa
->spa_sync_pass
= 0;
6525 mutex_enter(&spa
->spa_alloc_lock
);
6526 VERIFY0(avl_numnodes(&spa
->spa_alloc_tree
));
6527 mutex_exit(&spa
->spa_alloc_lock
);
6530 * If there are any pending vdev state changes, convert them
6531 * into config changes that go out with this transaction group.
6533 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
6534 while (list_head(&spa
->spa_state_dirty_list
) != NULL
) {
6536 * We need the write lock here because, for aux vdevs,
6537 * calling vdev_config_dirty() modifies sav_config.
6538 * This is ugly and will become unnecessary when we
6539 * eliminate the aux vdev wart by integrating all vdevs
6540 * into the root vdev tree.
6542 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
6543 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_WRITER
);
6544 while ((vd
= list_head(&spa
->spa_state_dirty_list
)) != NULL
) {
6545 vdev_state_clean(vd
);
6546 vdev_config_dirty(vd
);
6548 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
6549 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
6551 spa_config_exit(spa
, SCL_STATE
, FTAG
);
6553 tx
= dmu_tx_create_assigned(dp
, txg
);
6555 spa
->spa_sync_starttime
= gethrtime();
6556 taskq_cancel_id(system_delay_taskq
, spa
->spa_deadman_tqid
);
6557 spa
->spa_deadman_tqid
= taskq_dispatch_delay(system_delay_taskq
,
6558 spa_deadman
, spa
, TQ_SLEEP
, ddi_get_lbolt() +
6559 NSEC_TO_TICK(spa
->spa_deadman_synctime
));
6562 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
6563 * set spa_deflate if we have no raid-z vdevs.
6565 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_RAIDZ_DEFLATE
&&
6566 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
6569 for (i
= 0; i
< rvd
->vdev_children
; i
++) {
6570 vd
= rvd
->vdev_child
[i
];
6571 if (vd
->vdev_deflate_ratio
!= SPA_MINBLOCKSIZE
)
6574 if (i
== rvd
->vdev_children
) {
6575 spa
->spa_deflate
= TRUE
;
6576 VERIFY(0 == zap_add(spa
->spa_meta_objset
,
6577 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
6578 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
));
6583 * Set the top-level vdev's max queue depth. Evaluate each
6584 * top-level's async write queue depth in case it changed.
6585 * The max queue depth will not change in the middle of syncing
6588 queue_depth_total
= 0;
6589 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
6590 vdev_t
*tvd
= rvd
->vdev_child
[c
];
6591 metaslab_group_t
*mg
= tvd
->vdev_mg
;
6593 if (mg
== NULL
|| mg
->mg_class
!= spa_normal_class(spa
) ||
6594 !metaslab_group_initialized(mg
))
6598 * It is safe to do a lock-free check here because only async
6599 * allocations look at mg_max_alloc_queue_depth, and async
6600 * allocations all happen from spa_sync().
6602 ASSERT0(refcount_count(&mg
->mg_alloc_queue_depth
));
6603 mg
->mg_max_alloc_queue_depth
= max_queue_depth
;
6604 queue_depth_total
+= mg
->mg_max_alloc_queue_depth
;
6606 mc
= spa_normal_class(spa
);
6607 ASSERT0(refcount_count(&mc
->mc_alloc_slots
));
6608 mc
->mc_alloc_max_slots
= queue_depth_total
;
6609 mc
->mc_alloc_throttle_enabled
= zio_dva_throttle_enabled
;
6611 ASSERT3U(mc
->mc_alloc_max_slots
, <=,
6612 max_queue_depth
* rvd
->vdev_children
);
6615 * Iterate to convergence.
6618 int pass
= ++spa
->spa_sync_pass
;
6620 spa_sync_config_object(spa
, tx
);
6621 spa_sync_aux_dev(spa
, &spa
->spa_spares
, tx
,
6622 ZPOOL_CONFIG_SPARES
, DMU_POOL_SPARES
);
6623 spa_sync_aux_dev(spa
, &spa
->spa_l2cache
, tx
,
6624 ZPOOL_CONFIG_L2CACHE
, DMU_POOL_L2CACHE
);
6625 spa_errlog_sync(spa
, txg
);
6626 dsl_pool_sync(dp
, txg
);
6628 if (pass
< zfs_sync_pass_deferred_free
) {
6629 spa_sync_frees(spa
, free_bpl
, tx
);
6632 * We can not defer frees in pass 1, because
6633 * we sync the deferred frees later in pass 1.
6635 ASSERT3U(pass
, >, 1);
6636 bplist_iterate(free_bpl
, bpobj_enqueue_cb
,
6637 &spa
->spa_deferred_bpobj
, tx
);
6641 dsl_scan_sync(dp
, tx
);
6643 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, txg
)))
6647 spa_sync_upgrades(spa
, tx
);
6649 spa
->spa_uberblock
.ub_rootbp
.blk_birth
);
6651 * Note: We need to check if the MOS is dirty
6652 * because we could have marked the MOS dirty
6653 * without updating the uberblock (e.g. if we
6654 * have sync tasks but no dirty user data). We
6655 * need to check the uberblock's rootbp because
6656 * it is updated if we have synced out dirty
6657 * data (though in this case the MOS will most
6658 * likely also be dirty due to second order
6659 * effects, we don't want to rely on that here).
6661 if (spa
->spa_uberblock
.ub_rootbp
.blk_birth
< txg
&&
6662 !dmu_objset_is_dirty(mos
, txg
)) {
6664 * Nothing changed on the first pass,
6665 * therefore this TXG is a no-op. Avoid
6666 * syncing deferred frees, so that we
6667 * can keep this TXG as a no-op.
6669 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
,
6671 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
6672 ASSERT(txg_list_empty(&dp
->dp_sync_tasks
, txg
));
6675 spa_sync_deferred_frees(spa
, tx
);
6678 } while (dmu_objset_is_dirty(mos
, txg
));
6681 if (!list_is_empty(&spa
->spa_config_dirty_list
)) {
6683 * Make sure that the number of ZAPs for all the vdevs matches
6684 * the number of ZAPs in the per-vdev ZAP list. This only gets
6685 * called if the config is dirty; otherwise there may be
6686 * outstanding AVZ operations that weren't completed in
6687 * spa_sync_config_object.
6689 uint64_t all_vdev_zap_entry_count
;
6690 ASSERT0(zap_count(spa
->spa_meta_objset
,
6691 spa
->spa_all_vdev_zaps
, &all_vdev_zap_entry_count
));
6692 ASSERT3U(vdev_count_verify_zaps(spa
->spa_root_vdev
), ==,
6693 all_vdev_zap_entry_count
);
6698 * Rewrite the vdev configuration (which includes the uberblock)
6699 * to commit the transaction group.
6701 * If there are no dirty vdevs, we sync the uberblock to a few
6702 * random top-level vdevs that are known to be visible in the
6703 * config cache (see spa_vdev_add() for a complete description).
6704 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
6708 * We hold SCL_STATE to prevent vdev open/close/etc.
6709 * while we're attempting to write the vdev labels.
6711 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
6713 if (list_is_empty(&spa
->spa_config_dirty_list
)) {
6714 vdev_t
*svd
[SPA_DVAS_PER_BP
];
6716 int children
= rvd
->vdev_children
;
6717 int c0
= spa_get_random(children
);
6719 for (c
= 0; c
< children
; c
++) {
6720 vd
= rvd
->vdev_child
[(c0
+ c
) % children
];
6721 if (vd
->vdev_ms_array
== 0 || vd
->vdev_islog
)
6723 svd
[svdcount
++] = vd
;
6724 if (svdcount
== SPA_DVAS_PER_BP
)
6727 error
= vdev_config_sync(svd
, svdcount
, txg
);
6729 error
= vdev_config_sync(rvd
->vdev_child
,
6730 rvd
->vdev_children
, txg
);
6734 spa
->spa_last_synced_guid
= rvd
->vdev_guid
;
6736 spa_config_exit(spa
, SCL_STATE
, FTAG
);
6740 zio_suspend(spa
, NULL
);
6741 zio_resume_wait(spa
);
6745 taskq_cancel_id(system_delay_taskq
, spa
->spa_deadman_tqid
);
6746 spa
->spa_deadman_tqid
= 0;
6749 * Clear the dirty config list.
6751 while ((vd
= list_head(&spa
->spa_config_dirty_list
)) != NULL
)
6752 vdev_config_clean(vd
);
6755 * Now that the new config has synced transactionally,
6756 * let it become visible to the config cache.
6758 if (spa
->spa_config_syncing
!= NULL
) {
6759 spa_config_set(spa
, spa
->spa_config_syncing
);
6760 spa
->spa_config_txg
= txg
;
6761 spa
->spa_config_syncing
= NULL
;
6764 dsl_pool_sync_done(dp
, txg
);
6766 mutex_enter(&spa
->spa_alloc_lock
);
6767 VERIFY0(avl_numnodes(&spa
->spa_alloc_tree
));
6768 mutex_exit(&spa
->spa_alloc_lock
);
6771 * Update usable space statistics.
6773 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, TXG_CLEAN(txg
))))
6774 vdev_sync_done(vd
, txg
);
6776 spa_update_dspace(spa
);
6779 * It had better be the case that we didn't dirty anything
6780 * since vdev_config_sync().
6782 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
, txg
));
6783 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
6784 ASSERT(txg_list_empty(&spa
->spa_vdev_txg_list
, txg
));
6786 spa
->spa_sync_pass
= 0;
6789 * Update the last synced uberblock here. We want to do this at
6790 * the end of spa_sync() so that consumers of spa_last_synced_txg()
6791 * will be guaranteed that all the processing associated with
6792 * that txg has been completed.
6794 spa
->spa_ubsync
= spa
->spa_uberblock
;
6795 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
6797 spa_handle_ignored_writes(spa
);
6800 * If any async tasks have been requested, kick them off.
6802 spa_async_dispatch(spa
);
6806 * Sync all pools. We don't want to hold the namespace lock across these
6807 * operations, so we take a reference on the spa_t and drop the lock during the
6811 spa_sync_allpools(void)
6814 mutex_enter(&spa_namespace_lock
);
6815 while ((spa
= spa_next(spa
)) != NULL
) {
6816 if (spa_state(spa
) != POOL_STATE_ACTIVE
||
6817 !spa_writeable(spa
) || spa_suspended(spa
))
6819 spa_open_ref(spa
, FTAG
);
6820 mutex_exit(&spa_namespace_lock
);
6821 txg_wait_synced(spa_get_dsl(spa
), 0);
6822 mutex_enter(&spa_namespace_lock
);
6823 spa_close(spa
, FTAG
);
6825 mutex_exit(&spa_namespace_lock
);
6829 * ==========================================================================
6830 * Miscellaneous routines
6831 * ==========================================================================
6835 * Remove all pools in the system.
6843 * Remove all cached state. All pools should be closed now,
6844 * so every spa in the AVL tree should be unreferenced.
6846 mutex_enter(&spa_namespace_lock
);
6847 while ((spa
= spa_next(NULL
)) != NULL
) {
6849 * Stop async tasks. The async thread may need to detach
6850 * a device that's been replaced, which requires grabbing
6851 * spa_namespace_lock, so we must drop it here.
6853 spa_open_ref(spa
, FTAG
);
6854 mutex_exit(&spa_namespace_lock
);
6855 spa_async_suspend(spa
);
6856 mutex_enter(&spa_namespace_lock
);
6857 spa_close(spa
, FTAG
);
6859 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
6861 spa_deactivate(spa
);
6865 mutex_exit(&spa_namespace_lock
);
6869 spa_lookup_by_guid(spa_t
*spa
, uint64_t guid
, boolean_t aux
)
6874 if ((vd
= vdev_lookup_by_guid(spa
->spa_root_vdev
, guid
)) != NULL
)
6878 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
6879 vd
= spa
->spa_l2cache
.sav_vdevs
[i
];
6880 if (vd
->vdev_guid
== guid
)
6884 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
6885 vd
= spa
->spa_spares
.sav_vdevs
[i
];
6886 if (vd
->vdev_guid
== guid
)
6895 spa_upgrade(spa_t
*spa
, uint64_t version
)
6897 ASSERT(spa_writeable(spa
));
6899 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6902 * This should only be called for a non-faulted pool, and since a
6903 * future version would result in an unopenable pool, this shouldn't be
6906 ASSERT(SPA_VERSION_IS_SUPPORTED(spa
->spa_uberblock
.ub_version
));
6907 ASSERT3U(version
, >=, spa
->spa_uberblock
.ub_version
);
6909 spa
->spa_uberblock
.ub_version
= version
;
6910 vdev_config_dirty(spa
->spa_root_vdev
);
6912 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6914 txg_wait_synced(spa_get_dsl(spa
), 0);
6918 spa_has_spare(spa_t
*spa
, uint64_t guid
)
6922 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
6924 for (i
= 0; i
< sav
->sav_count
; i
++)
6925 if (sav
->sav_vdevs
[i
]->vdev_guid
== guid
)
6928 for (i
= 0; i
< sav
->sav_npending
; i
++) {
6929 if (nvlist_lookup_uint64(sav
->sav_pending
[i
], ZPOOL_CONFIG_GUID
,
6930 &spareguid
) == 0 && spareguid
== guid
)
6938 * Check if a pool has an active shared spare device.
6939 * Note: reference count of an active spare is 2, as a spare and as a replace
6942 spa_has_active_shared_spare(spa_t
*spa
)
6946 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
6948 for (i
= 0; i
< sav
->sav_count
; i
++) {
6949 if (spa_spare_exists(sav
->sav_vdevs
[i
]->vdev_guid
, &pool
,
6950 &refcnt
) && pool
!= 0ULL && pool
== spa_guid(spa
) &&
6959 spa_event_create(spa_t
*spa
, vdev_t
*vd
, nvlist_t
*hist_nvl
, const char *name
)
6961 sysevent_t
*ev
= NULL
;
6965 resource
= zfs_event_create(spa
, vd
, FM_SYSEVENT_CLASS
, name
, hist_nvl
);
6967 ev
= kmem_alloc(sizeof (sysevent_t
), KM_SLEEP
);
6968 ev
->resource
= resource
;
6975 spa_event_post(sysevent_t
*ev
)
6979 zfs_zevent_post(ev
->resource
, NULL
, zfs_zevent_post_cb
);
6980 kmem_free(ev
, sizeof (*ev
));
6986 * Post a zevent corresponding to the given sysevent. The 'name' must be one
6987 * of the event definitions in sys/sysevent/eventdefs.h. The payload will be
6988 * filled in from the spa and (optionally) the vdev. This doesn't do anything
6989 * in the userland libzpool, as we don't want consumers to misinterpret ztest
6990 * or zdb as real changes.
6993 spa_event_notify(spa_t
*spa
, vdev_t
*vd
, nvlist_t
*hist_nvl
, const char *name
)
6995 spa_event_post(spa_event_create(spa
, vd
, hist_nvl
, name
));
6998 #if defined(_KERNEL) && defined(HAVE_SPL)
6999 /* state manipulation functions */
7000 EXPORT_SYMBOL(spa_open
);
7001 EXPORT_SYMBOL(spa_open_rewind
);
7002 EXPORT_SYMBOL(spa_get_stats
);
7003 EXPORT_SYMBOL(spa_create
);
7004 EXPORT_SYMBOL(spa_import
);
7005 EXPORT_SYMBOL(spa_tryimport
);
7006 EXPORT_SYMBOL(spa_destroy
);
7007 EXPORT_SYMBOL(spa_export
);
7008 EXPORT_SYMBOL(spa_reset
);
7009 EXPORT_SYMBOL(spa_async_request
);
7010 EXPORT_SYMBOL(spa_async_suspend
);
7011 EXPORT_SYMBOL(spa_async_resume
);
7012 EXPORT_SYMBOL(spa_inject_addref
);
7013 EXPORT_SYMBOL(spa_inject_delref
);
7014 EXPORT_SYMBOL(spa_scan_stat_init
);
7015 EXPORT_SYMBOL(spa_scan_get_stats
);
7017 /* device maniion */
7018 EXPORT_SYMBOL(spa_vdev_add
);
7019 EXPORT_SYMBOL(spa_vdev_attach
);
7020 EXPORT_SYMBOL(spa_vdev_detach
);
7021 EXPORT_SYMBOL(spa_vdev_remove
);
7022 EXPORT_SYMBOL(spa_vdev_setpath
);
7023 EXPORT_SYMBOL(spa_vdev_setfru
);
7024 EXPORT_SYMBOL(spa_vdev_split_mirror
);
7026 /* spare statech is global across all pools) */
7027 EXPORT_SYMBOL(spa_spare_add
);
7028 EXPORT_SYMBOL(spa_spare_remove
);
7029 EXPORT_SYMBOL(spa_spare_exists
);
7030 EXPORT_SYMBOL(spa_spare_activate
);
7032 /* L2ARC statech is global across all pools) */
7033 EXPORT_SYMBOL(spa_l2cache_add
);
7034 EXPORT_SYMBOL(spa_l2cache_remove
);
7035 EXPORT_SYMBOL(spa_l2cache_exists
);
7036 EXPORT_SYMBOL(spa_l2cache_activate
);
7037 EXPORT_SYMBOL(spa_l2cache_drop
);
7040 EXPORT_SYMBOL(spa_scan
);
7041 EXPORT_SYMBOL(spa_scan_stop
);
7044 EXPORT_SYMBOL(spa_sync
); /* only for DMU use */
7045 EXPORT_SYMBOL(spa_sync_allpools
);
7048 EXPORT_SYMBOL(spa_prop_set
);
7049 EXPORT_SYMBOL(spa_prop_get
);
7050 EXPORT_SYMBOL(spa_prop_clear_bootfs
);
7052 /* asynchronous event notification */
7053 EXPORT_SYMBOL(spa_event_notify
);
7056 #if defined(_KERNEL) && defined(HAVE_SPL)
7057 module_param(spa_load_verify_maxinflight
, int, 0644);
7058 MODULE_PARM_DESC(spa_load_verify_maxinflight
,
7059 "Max concurrent traversal I/Os while verifying pool during import -X");
7061 module_param(spa_load_verify_metadata
, int, 0644);
7062 MODULE_PARM_DESC(spa_load_verify_metadata
,
7063 "Set to traverse metadata on pool import");
7065 module_param(spa_load_verify_data
, int, 0644);
7066 MODULE_PARM_DESC(spa_load_verify_data
,
7067 "Set to traverse data on pool import");
7070 module_param(zio_taskq_batch_pct
, uint
, 0444);
7071 MODULE_PARM_DESC(zio_taskq_batch_pct
,
7072 "Percentage of CPUs to run an IO worker thread");