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) 2013 by Delphix. All rights reserved.
25 * Copyright (c) 2013, 2014, Nexenta Systems, Inc. All rights reserved.
26 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
27 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
31 * SPA: Storage Pool Allocator
33 * This file contains all the routines used when modifying on-disk SPA state.
34 * This includes opening, importing, destroying, exporting a pool, and syncing a
38 #include <sys/zfs_context.h>
39 #include <sys/fm/fs/zfs.h>
40 #include <sys/spa_impl.h>
42 #include <sys/zio_checksum.h>
44 #include <sys/dmu_tx.h>
48 #include <sys/vdev_impl.h>
49 #include <sys/vdev_disk.h>
50 #include <sys/metaslab.h>
51 #include <sys/metaslab_impl.h>
52 #include <sys/uberblock_impl.h>
55 #include <sys/dmu_traverse.h>
56 #include <sys/dmu_objset.h>
57 #include <sys/unique.h>
58 #include <sys/dsl_pool.h>
59 #include <sys/dsl_dataset.h>
60 #include <sys/dsl_dir.h>
61 #include <sys/dsl_prop.h>
62 #include <sys/dsl_synctask.h>
63 #include <sys/fs/zfs.h>
65 #include <sys/callb.h>
66 #include <sys/systeminfo.h>
67 #include <sys/spa_boot.h>
68 #include <sys/zfs_ioctl.h>
69 #include <sys/dsl_scan.h>
70 #include <sys/zfeature.h>
71 #include <sys/dsl_destroy.h>
75 #include <sys/bootprops.h>
76 #include <sys/callb.h>
77 #include <sys/cpupart.h>
79 #include <sys/sysdc.h>
84 #include "zfs_comutil.h"
87 * The interval, in seconds, at which failed configuration cache file writes
90 static int zfs_ccw_retry_interval
= 300;
92 typedef enum zti_modes
{
93 ZTI_MODE_FIXED
, /* value is # of threads (min 1) */
94 ZTI_MODE_BATCH
, /* cpu-intensive; value is ignored */
95 ZTI_MODE_NULL
, /* don't create a taskq */
99 #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) }
100 #define ZTI_PCT(n) { ZTI_MODE_ONLINE_PERCENT, (n), 1 }
101 #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 }
102 #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 }
104 #define ZTI_N(n) ZTI_P(n, 1)
105 #define ZTI_ONE ZTI_N(1)
107 typedef struct zio_taskq_info
{
108 zti_modes_t zti_mode
;
113 static const char *const zio_taskq_types
[ZIO_TASKQ_TYPES
] = {
114 "iss", "iss_h", "int", "int_h"
118 * This table defines the taskq settings for each ZFS I/O type. When
119 * initializing a pool, we use this table to create an appropriately sized
120 * taskq. Some operations are low volume and therefore have a small, static
121 * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
122 * macros. Other operations process a large amount of data; the ZTI_BATCH
123 * macro causes us to create a taskq oriented for throughput. Some operations
124 * are so high frequency and short-lived that the taskq itself can become a a
125 * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
126 * additional degree of parallelism specified by the number of threads per-
127 * taskq and the number of taskqs; when dispatching an event in this case, the
128 * particular taskq is chosen at random.
130 * The different taskq priorities are to handle the different contexts (issue
131 * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that
132 * need to be handled with minimum delay.
134 const zio_taskq_info_t zio_taskqs
[ZIO_TYPES
][ZIO_TASKQ_TYPES
] = {
135 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
136 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* NULL */
137 { ZTI_N(8), ZTI_NULL
, ZTI_P(12, 8), ZTI_NULL
}, /* READ */
138 { ZTI_BATCH
, ZTI_N(5), ZTI_P(12, 8), ZTI_N(5) }, /* WRITE */
139 { ZTI_P(12, 8), ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* FREE */
140 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* CLAIM */
141 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* IOCTL */
144 static void spa_sync_version(void *arg
, dmu_tx_t
*tx
);
145 static void spa_sync_props(void *arg
, dmu_tx_t
*tx
);
146 static boolean_t
spa_has_active_shared_spare(spa_t
*spa
);
147 static inline int spa_load_impl(spa_t
*spa
, uint64_t, nvlist_t
*config
,
148 spa_load_state_t state
, spa_import_type_t type
, boolean_t mosconfig
,
150 static void spa_vdev_resilver_done(spa_t
*spa
);
152 uint_t zio_taskq_batch_pct
= 75; /* 1 thread per cpu in pset */
153 id_t zio_taskq_psrset_bind
= PS_NONE
;
154 boolean_t zio_taskq_sysdc
= B_TRUE
; /* use SDC scheduling class */
155 uint_t zio_taskq_basedc
= 80; /* base duty cycle */
157 boolean_t spa_create_process
= B_TRUE
; /* no process ==> no sysdc */
160 * This (illegal) pool name is used when temporarily importing a spa_t in order
161 * to get the vdev stats associated with the imported devices.
163 #define TRYIMPORT_NAME "$import"
166 * ==========================================================================
167 * SPA properties routines
168 * ==========================================================================
172 * Add a (source=src, propname=propval) list to an nvlist.
175 spa_prop_add_list(nvlist_t
*nvl
, zpool_prop_t prop
, char *strval
,
176 uint64_t intval
, zprop_source_t src
)
178 const char *propname
= zpool_prop_to_name(prop
);
181 VERIFY(nvlist_alloc(&propval
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
182 VERIFY(nvlist_add_uint64(propval
, ZPROP_SOURCE
, src
) == 0);
185 VERIFY(nvlist_add_string(propval
, ZPROP_VALUE
, strval
) == 0);
187 VERIFY(nvlist_add_uint64(propval
, ZPROP_VALUE
, intval
) == 0);
189 VERIFY(nvlist_add_nvlist(nvl
, propname
, propval
) == 0);
190 nvlist_free(propval
);
194 * Get property values from the spa configuration.
197 spa_prop_get_config(spa_t
*spa
, nvlist_t
**nvp
)
199 vdev_t
*rvd
= spa
->spa_root_vdev
;
200 dsl_pool_t
*pool
= spa
->spa_dsl_pool
;
201 uint64_t size
, alloc
, cap
, version
;
202 zprop_source_t src
= ZPROP_SRC_NONE
;
203 spa_config_dirent_t
*dp
;
204 metaslab_class_t
*mc
= spa_normal_class(spa
);
206 ASSERT(MUTEX_HELD(&spa
->spa_props_lock
));
209 alloc
= metaslab_class_get_alloc(spa_normal_class(spa
));
210 size
= metaslab_class_get_space(spa_normal_class(spa
));
211 spa_prop_add_list(*nvp
, ZPOOL_PROP_NAME
, spa_name(spa
), 0, src
);
212 spa_prop_add_list(*nvp
, ZPOOL_PROP_SIZE
, NULL
, size
, src
);
213 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALLOCATED
, NULL
, alloc
, src
);
214 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREE
, NULL
,
217 spa_prop_add_list(*nvp
, ZPOOL_PROP_FRAGMENTATION
, NULL
,
218 metaslab_class_fragmentation(mc
), src
);
219 spa_prop_add_list(*nvp
, ZPOOL_PROP_EXPANDSZ
, NULL
,
220 metaslab_class_expandable_space(mc
), src
);
221 spa_prop_add_list(*nvp
, ZPOOL_PROP_READONLY
, NULL
,
222 (spa_mode(spa
) == FREAD
), src
);
224 cap
= (size
== 0) ? 0 : (alloc
* 100 / size
);
225 spa_prop_add_list(*nvp
, ZPOOL_PROP_CAPACITY
, NULL
, cap
, src
);
227 spa_prop_add_list(*nvp
, ZPOOL_PROP_DEDUPRATIO
, NULL
,
228 ddt_get_pool_dedup_ratio(spa
), src
);
230 spa_prop_add_list(*nvp
, ZPOOL_PROP_HEALTH
, NULL
,
231 rvd
->vdev_state
, src
);
233 version
= spa_version(spa
);
234 if (version
== zpool_prop_default_numeric(ZPOOL_PROP_VERSION
))
235 src
= ZPROP_SRC_DEFAULT
;
237 src
= ZPROP_SRC_LOCAL
;
238 spa_prop_add_list(*nvp
, ZPOOL_PROP_VERSION
, NULL
, version
, src
);
243 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
244 * when opening pools before this version freedir will be NULL.
246 if (pool
->dp_free_dir
!= NULL
) {
247 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
, NULL
,
248 dsl_dir_phys(pool
->dp_free_dir
)->dd_used_bytes
,
251 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
,
255 if (pool
->dp_leak_dir
!= NULL
) {
256 spa_prop_add_list(*nvp
, ZPOOL_PROP_LEAKED
, NULL
,
257 dsl_dir_phys(pool
->dp_leak_dir
)->dd_used_bytes
,
260 spa_prop_add_list(*nvp
, ZPOOL_PROP_LEAKED
,
265 spa_prop_add_list(*nvp
, ZPOOL_PROP_GUID
, NULL
, spa_guid(spa
), src
);
267 if (spa
->spa_comment
!= NULL
) {
268 spa_prop_add_list(*nvp
, ZPOOL_PROP_COMMENT
, spa
->spa_comment
,
272 if (spa
->spa_root
!= NULL
)
273 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALTROOT
, spa
->spa_root
,
276 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_BLOCKS
)) {
277 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXBLOCKSIZE
, NULL
,
278 MIN(zfs_max_recordsize
, SPA_MAXBLOCKSIZE
), ZPROP_SRC_NONE
);
280 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXBLOCKSIZE
, NULL
,
281 SPA_OLD_MAXBLOCKSIZE
, ZPROP_SRC_NONE
);
284 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_DNODE
)) {
285 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXDNODESIZE
, NULL
,
286 DNODE_MAX_SIZE
, ZPROP_SRC_NONE
);
288 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXDNODESIZE
, NULL
,
289 DNODE_MIN_SIZE
, ZPROP_SRC_NONE
);
292 if ((dp
= list_head(&spa
->spa_config_list
)) != NULL
) {
293 if (dp
->scd_path
== NULL
) {
294 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
295 "none", 0, ZPROP_SRC_LOCAL
);
296 } else if (strcmp(dp
->scd_path
, spa_config_path
) != 0) {
297 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
298 dp
->scd_path
, 0, ZPROP_SRC_LOCAL
);
304 * Get zpool property values.
307 spa_prop_get(spa_t
*spa
, nvlist_t
**nvp
)
309 objset_t
*mos
= spa
->spa_meta_objset
;
314 err
= nvlist_alloc(nvp
, NV_UNIQUE_NAME
, KM_SLEEP
);
318 mutex_enter(&spa
->spa_props_lock
);
321 * Get properties from the spa config.
323 spa_prop_get_config(spa
, nvp
);
325 /* If no pool property object, no more prop to get. */
326 if (mos
== NULL
|| spa
->spa_pool_props_object
== 0) {
327 mutex_exit(&spa
->spa_props_lock
);
332 * Get properties from the MOS pool property object.
334 for (zap_cursor_init(&zc
, mos
, spa
->spa_pool_props_object
);
335 (err
= zap_cursor_retrieve(&zc
, &za
)) == 0;
336 zap_cursor_advance(&zc
)) {
339 zprop_source_t src
= ZPROP_SRC_DEFAULT
;
342 if ((prop
= zpool_name_to_prop(za
.za_name
)) == ZPROP_INVAL
)
345 switch (za
.za_integer_length
) {
347 /* integer property */
348 if (za
.za_first_integer
!=
349 zpool_prop_default_numeric(prop
))
350 src
= ZPROP_SRC_LOCAL
;
352 if (prop
== ZPOOL_PROP_BOOTFS
) {
354 dsl_dataset_t
*ds
= NULL
;
356 dp
= spa_get_dsl(spa
);
357 dsl_pool_config_enter(dp
, FTAG
);
358 if ((err
= dsl_dataset_hold_obj(dp
,
359 za
.za_first_integer
, FTAG
, &ds
))) {
360 dsl_pool_config_exit(dp
, FTAG
);
364 strval
= kmem_alloc(ZFS_MAX_DATASET_NAME_LEN
,
366 dsl_dataset_name(ds
, strval
);
367 dsl_dataset_rele(ds
, FTAG
);
368 dsl_pool_config_exit(dp
, FTAG
);
371 intval
= za
.za_first_integer
;
374 spa_prop_add_list(*nvp
, prop
, strval
, intval
, src
);
377 kmem_free(strval
, ZFS_MAX_DATASET_NAME_LEN
);
382 /* string property */
383 strval
= kmem_alloc(za
.za_num_integers
, KM_SLEEP
);
384 err
= zap_lookup(mos
, spa
->spa_pool_props_object
,
385 za
.za_name
, 1, za
.za_num_integers
, strval
);
387 kmem_free(strval
, za
.za_num_integers
);
390 spa_prop_add_list(*nvp
, prop
, strval
, 0, src
);
391 kmem_free(strval
, za
.za_num_integers
);
398 zap_cursor_fini(&zc
);
399 mutex_exit(&spa
->spa_props_lock
);
401 if (err
&& err
!= ENOENT
) {
411 * Validate the given pool properties nvlist and modify the list
412 * for the property values to be set.
415 spa_prop_validate(spa_t
*spa
, nvlist_t
*props
)
418 int error
= 0, reset_bootfs
= 0;
420 boolean_t has_feature
= B_FALSE
;
423 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
425 char *strval
, *slash
, *check
, *fname
;
426 const char *propname
= nvpair_name(elem
);
427 zpool_prop_t prop
= zpool_name_to_prop(propname
);
431 if (!zpool_prop_feature(propname
)) {
432 error
= SET_ERROR(EINVAL
);
437 * Sanitize the input.
439 if (nvpair_type(elem
) != DATA_TYPE_UINT64
) {
440 error
= SET_ERROR(EINVAL
);
444 if (nvpair_value_uint64(elem
, &intval
) != 0) {
445 error
= SET_ERROR(EINVAL
);
450 error
= SET_ERROR(EINVAL
);
454 fname
= strchr(propname
, '@') + 1;
455 if (zfeature_lookup_name(fname
, NULL
) != 0) {
456 error
= SET_ERROR(EINVAL
);
460 has_feature
= B_TRUE
;
463 case ZPOOL_PROP_VERSION
:
464 error
= nvpair_value_uint64(elem
, &intval
);
466 (intval
< spa_version(spa
) ||
467 intval
> SPA_VERSION_BEFORE_FEATURES
||
469 error
= SET_ERROR(EINVAL
);
472 case ZPOOL_PROP_DELEGATION
:
473 case ZPOOL_PROP_AUTOREPLACE
:
474 case ZPOOL_PROP_LISTSNAPS
:
475 case ZPOOL_PROP_AUTOEXPAND
:
476 error
= nvpair_value_uint64(elem
, &intval
);
477 if (!error
&& intval
> 1)
478 error
= SET_ERROR(EINVAL
);
481 case ZPOOL_PROP_BOOTFS
:
483 * If the pool version is less than SPA_VERSION_BOOTFS,
484 * or the pool is still being created (version == 0),
485 * the bootfs property cannot be set.
487 if (spa_version(spa
) < SPA_VERSION_BOOTFS
) {
488 error
= SET_ERROR(ENOTSUP
);
493 * Make sure the vdev config is bootable
495 if (!vdev_is_bootable(spa
->spa_root_vdev
)) {
496 error
= SET_ERROR(ENOTSUP
);
502 error
= nvpair_value_string(elem
, &strval
);
508 if (strval
== NULL
|| strval
[0] == '\0') {
509 objnum
= zpool_prop_default_numeric(
514 error
= dmu_objset_hold(strval
, FTAG
, &os
);
519 * Must be ZPL, and its property settings
520 * must be supported by GRUB (compression
521 * is not gzip, and large blocks or large
522 * dnodes are not used).
525 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
526 error
= SET_ERROR(ENOTSUP
);
528 dsl_prop_get_int_ds(dmu_objset_ds(os
),
529 zfs_prop_to_name(ZFS_PROP_COMPRESSION
),
531 !BOOTFS_COMPRESS_VALID(propval
)) {
532 error
= SET_ERROR(ENOTSUP
);
534 dsl_prop_get_int_ds(dmu_objset_ds(os
),
535 zfs_prop_to_name(ZFS_PROP_RECORDSIZE
),
537 propval
> SPA_OLD_MAXBLOCKSIZE
) {
538 error
= SET_ERROR(ENOTSUP
);
540 dsl_prop_get_int_ds(dmu_objset_ds(os
),
541 zfs_prop_to_name(ZFS_PROP_DNODESIZE
),
543 propval
!= ZFS_DNSIZE_LEGACY
) {
544 error
= SET_ERROR(ENOTSUP
);
546 objnum
= dmu_objset_id(os
);
548 dmu_objset_rele(os
, FTAG
);
552 case ZPOOL_PROP_FAILUREMODE
:
553 error
= nvpair_value_uint64(elem
, &intval
);
554 if (!error
&& (intval
< ZIO_FAILURE_MODE_WAIT
||
555 intval
> ZIO_FAILURE_MODE_PANIC
))
556 error
= SET_ERROR(EINVAL
);
559 * This is a special case which only occurs when
560 * the pool has completely failed. This allows
561 * the user to change the in-core failmode property
562 * without syncing it out to disk (I/Os might
563 * currently be blocked). We do this by returning
564 * EIO to the caller (spa_prop_set) to trick it
565 * into thinking we encountered a property validation
568 if (!error
&& spa_suspended(spa
)) {
569 spa
->spa_failmode
= intval
;
570 error
= SET_ERROR(EIO
);
574 case ZPOOL_PROP_CACHEFILE
:
575 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
578 if (strval
[0] == '\0')
581 if (strcmp(strval
, "none") == 0)
584 if (strval
[0] != '/') {
585 error
= SET_ERROR(EINVAL
);
589 slash
= strrchr(strval
, '/');
590 ASSERT(slash
!= NULL
);
592 if (slash
[1] == '\0' || strcmp(slash
, "/.") == 0 ||
593 strcmp(slash
, "/..") == 0)
594 error
= SET_ERROR(EINVAL
);
597 case ZPOOL_PROP_COMMENT
:
598 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
600 for (check
= strval
; *check
!= '\0'; check
++) {
601 if (!isprint(*check
)) {
602 error
= SET_ERROR(EINVAL
);
606 if (strlen(strval
) > ZPROP_MAX_COMMENT
)
607 error
= SET_ERROR(E2BIG
);
610 case ZPOOL_PROP_DEDUPDITTO
:
611 if (spa_version(spa
) < SPA_VERSION_DEDUP
)
612 error
= SET_ERROR(ENOTSUP
);
614 error
= nvpair_value_uint64(elem
, &intval
);
616 intval
!= 0 && intval
< ZIO_DEDUPDITTO_MIN
)
617 error
= SET_ERROR(EINVAL
);
628 if (!error
&& reset_bootfs
) {
629 error
= nvlist_remove(props
,
630 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), DATA_TYPE_STRING
);
633 error
= nvlist_add_uint64(props
,
634 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), objnum
);
642 spa_configfile_set(spa_t
*spa
, nvlist_t
*nvp
, boolean_t need_sync
)
645 spa_config_dirent_t
*dp
;
647 if (nvlist_lookup_string(nvp
, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
),
651 dp
= kmem_alloc(sizeof (spa_config_dirent_t
),
654 if (cachefile
[0] == '\0')
655 dp
->scd_path
= spa_strdup(spa_config_path
);
656 else if (strcmp(cachefile
, "none") == 0)
659 dp
->scd_path
= spa_strdup(cachefile
);
661 list_insert_head(&spa
->spa_config_list
, dp
);
663 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
667 spa_prop_set(spa_t
*spa
, nvlist_t
*nvp
)
670 nvpair_t
*elem
= NULL
;
671 boolean_t need_sync
= B_FALSE
;
673 if ((error
= spa_prop_validate(spa
, nvp
)) != 0)
676 while ((elem
= nvlist_next_nvpair(nvp
, elem
)) != NULL
) {
677 zpool_prop_t prop
= zpool_name_to_prop(nvpair_name(elem
));
679 if (prop
== ZPOOL_PROP_CACHEFILE
||
680 prop
== ZPOOL_PROP_ALTROOT
||
681 prop
== ZPOOL_PROP_READONLY
)
684 if (prop
== ZPOOL_PROP_VERSION
|| prop
== ZPROP_INVAL
) {
687 if (prop
== ZPOOL_PROP_VERSION
) {
688 VERIFY(nvpair_value_uint64(elem
, &ver
) == 0);
690 ASSERT(zpool_prop_feature(nvpair_name(elem
)));
691 ver
= SPA_VERSION_FEATURES
;
695 /* Save time if the version is already set. */
696 if (ver
== spa_version(spa
))
700 * In addition to the pool directory object, we might
701 * create the pool properties object, the features for
702 * read object, the features for write object, or the
703 * feature descriptions object.
705 error
= dsl_sync_task(spa
->spa_name
, NULL
,
706 spa_sync_version
, &ver
,
707 6, ZFS_SPACE_CHECK_RESERVED
);
718 return (dsl_sync_task(spa
->spa_name
, NULL
, spa_sync_props
,
719 nvp
, 6, ZFS_SPACE_CHECK_RESERVED
));
726 * If the bootfs property value is dsobj, clear it.
729 spa_prop_clear_bootfs(spa_t
*spa
, uint64_t dsobj
, dmu_tx_t
*tx
)
731 if (spa
->spa_bootfs
== dsobj
&& spa
->spa_pool_props_object
!= 0) {
732 VERIFY(zap_remove(spa
->spa_meta_objset
,
733 spa
->spa_pool_props_object
,
734 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), tx
) == 0);
741 spa_change_guid_check(void *arg
, dmu_tx_t
*tx
)
743 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
744 vdev_t
*rvd
= spa
->spa_root_vdev
;
746 ASSERTV(uint64_t *newguid
= arg
);
748 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
749 vdev_state
= rvd
->vdev_state
;
750 spa_config_exit(spa
, SCL_STATE
, FTAG
);
752 if (vdev_state
!= VDEV_STATE_HEALTHY
)
753 return (SET_ERROR(ENXIO
));
755 ASSERT3U(spa_guid(spa
), !=, *newguid
);
761 spa_change_guid_sync(void *arg
, dmu_tx_t
*tx
)
763 uint64_t *newguid
= arg
;
764 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
766 vdev_t
*rvd
= spa
->spa_root_vdev
;
768 oldguid
= spa_guid(spa
);
770 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
771 rvd
->vdev_guid
= *newguid
;
772 rvd
->vdev_guid_sum
+= (*newguid
- oldguid
);
773 vdev_config_dirty(rvd
);
774 spa_config_exit(spa
, SCL_STATE
, FTAG
);
776 spa_history_log_internal(spa
, "guid change", tx
, "old=%llu new=%llu",
781 * Change the GUID for the pool. This is done so that we can later
782 * re-import a pool built from a clone of our own vdevs. We will modify
783 * the root vdev's guid, our own pool guid, and then mark all of our
784 * vdevs dirty. Note that we must make sure that all our vdevs are
785 * online when we do this, or else any vdevs that weren't present
786 * would be orphaned from our pool. We are also going to issue a
787 * sysevent to update any watchers.
790 spa_change_guid(spa_t
*spa
)
795 mutex_enter(&spa
->spa_vdev_top_lock
);
796 mutex_enter(&spa_namespace_lock
);
797 guid
= spa_generate_guid(NULL
);
799 error
= dsl_sync_task(spa
->spa_name
, spa_change_guid_check
,
800 spa_change_guid_sync
, &guid
, 5, ZFS_SPACE_CHECK_RESERVED
);
803 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
804 spa_event_notify(spa
, NULL
, FM_EREPORT_ZFS_POOL_REGUID
);
807 mutex_exit(&spa_namespace_lock
);
808 mutex_exit(&spa
->spa_vdev_top_lock
);
814 * ==========================================================================
815 * SPA state manipulation (open/create/destroy/import/export)
816 * ==========================================================================
820 spa_error_entry_compare(const void *a
, const void *b
)
822 spa_error_entry_t
*sa
= (spa_error_entry_t
*)a
;
823 spa_error_entry_t
*sb
= (spa_error_entry_t
*)b
;
826 ret
= bcmp(&sa
->se_bookmark
, &sb
->se_bookmark
,
827 sizeof (zbookmark_phys_t
));
838 * Utility function which retrieves copies of the current logs and
839 * re-initializes them in the process.
842 spa_get_errlists(spa_t
*spa
, avl_tree_t
*last
, avl_tree_t
*scrub
)
844 ASSERT(MUTEX_HELD(&spa
->spa_errlist_lock
));
846 bcopy(&spa
->spa_errlist_last
, last
, sizeof (avl_tree_t
));
847 bcopy(&spa
->spa_errlist_scrub
, scrub
, sizeof (avl_tree_t
));
849 avl_create(&spa
->spa_errlist_scrub
,
850 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
851 offsetof(spa_error_entry_t
, se_avl
));
852 avl_create(&spa
->spa_errlist_last
,
853 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
854 offsetof(spa_error_entry_t
, se_avl
));
858 spa_taskqs_init(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
860 const zio_taskq_info_t
*ztip
= &zio_taskqs
[t
][q
];
861 enum zti_modes mode
= ztip
->zti_mode
;
862 uint_t value
= ztip
->zti_value
;
863 uint_t count
= ztip
->zti_count
;
864 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
866 uint_t i
, flags
= TASKQ_DYNAMIC
;
867 boolean_t batch
= B_FALSE
;
869 if (mode
== ZTI_MODE_NULL
) {
871 tqs
->stqs_taskq
= NULL
;
875 ASSERT3U(count
, >, 0);
877 tqs
->stqs_count
= count
;
878 tqs
->stqs_taskq
= kmem_alloc(count
* sizeof (taskq_t
*), KM_SLEEP
);
882 ASSERT3U(value
, >=, 1);
883 value
= MAX(value
, 1);
888 flags
|= TASKQ_THREADS_CPU_PCT
;
889 value
= MIN(zio_taskq_batch_pct
, 100);
893 panic("unrecognized mode for %s_%s taskq (%u:%u) in "
895 zio_type_name
[t
], zio_taskq_types
[q
], mode
, value
);
899 for (i
= 0; i
< count
; i
++) {
903 (void) snprintf(name
, sizeof (name
), "%s_%s_%u",
904 zio_type_name
[t
], zio_taskq_types
[q
], i
);
906 (void) snprintf(name
, sizeof (name
), "%s_%s",
907 zio_type_name
[t
], zio_taskq_types
[q
]);
910 if (zio_taskq_sysdc
&& spa
->spa_proc
!= &p0
) {
912 flags
|= TASKQ_DC_BATCH
;
914 tq
= taskq_create_sysdc(name
, value
, 50, INT_MAX
,
915 spa
->spa_proc
, zio_taskq_basedc
, flags
);
917 pri_t pri
= maxclsyspri
;
919 * The write issue taskq can be extremely CPU
920 * intensive. Run it at slightly less important
921 * priority than the other taskqs. Under Linux this
922 * means incrementing the priority value on platforms
923 * like illumos it should be decremented.
925 if (t
== ZIO_TYPE_WRITE
&& q
== ZIO_TASKQ_ISSUE
)
928 tq
= taskq_create_proc(name
, value
, pri
, 50,
929 INT_MAX
, spa
->spa_proc
, flags
);
932 tqs
->stqs_taskq
[i
] = tq
;
937 spa_taskqs_fini(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
939 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
942 if (tqs
->stqs_taskq
== NULL
) {
943 ASSERT3U(tqs
->stqs_count
, ==, 0);
947 for (i
= 0; i
< tqs
->stqs_count
; i
++) {
948 ASSERT3P(tqs
->stqs_taskq
[i
], !=, NULL
);
949 taskq_destroy(tqs
->stqs_taskq
[i
]);
952 kmem_free(tqs
->stqs_taskq
, tqs
->stqs_count
* sizeof (taskq_t
*));
953 tqs
->stqs_taskq
= NULL
;
957 * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority.
958 * Note that a type may have multiple discrete taskqs to avoid lock contention
959 * on the taskq itself. In that case we choose which taskq at random by using
960 * the low bits of gethrtime().
963 spa_taskq_dispatch_ent(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
964 task_func_t
*func
, void *arg
, uint_t flags
, taskq_ent_t
*ent
)
966 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
969 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
970 ASSERT3U(tqs
->stqs_count
, !=, 0);
972 if (tqs
->stqs_count
== 1) {
973 tq
= tqs
->stqs_taskq
[0];
975 tq
= tqs
->stqs_taskq
[((uint64_t)gethrtime()) % tqs
->stqs_count
];
978 taskq_dispatch_ent(tq
, func
, arg
, flags
, ent
);
982 * Same as spa_taskq_dispatch_ent() but block on the task until completion.
985 spa_taskq_dispatch_sync(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
986 task_func_t
*func
, void *arg
, uint_t flags
)
988 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
992 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
993 ASSERT3U(tqs
->stqs_count
, !=, 0);
995 if (tqs
->stqs_count
== 1) {
996 tq
= tqs
->stqs_taskq
[0];
998 tq
= tqs
->stqs_taskq
[((uint64_t)gethrtime()) % tqs
->stqs_count
];
1001 id
= taskq_dispatch(tq
, func
, arg
, flags
);
1003 taskq_wait_id(tq
, id
);
1007 spa_create_zio_taskqs(spa_t
*spa
)
1011 for (t
= 0; t
< ZIO_TYPES
; t
++) {
1012 for (q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1013 spa_taskqs_init(spa
, t
, q
);
1018 #if defined(_KERNEL) && defined(HAVE_SPA_THREAD)
1020 spa_thread(void *arg
)
1022 callb_cpr_t cprinfo
;
1025 user_t
*pu
= PTOU(curproc
);
1027 CALLB_CPR_INIT(&cprinfo
, &spa
->spa_proc_lock
, callb_generic_cpr
,
1030 ASSERT(curproc
!= &p0
);
1031 (void) snprintf(pu
->u_psargs
, sizeof (pu
->u_psargs
),
1032 "zpool-%s", spa
->spa_name
);
1033 (void) strlcpy(pu
->u_comm
, pu
->u_psargs
, sizeof (pu
->u_comm
));
1035 /* bind this thread to the requested psrset */
1036 if (zio_taskq_psrset_bind
!= PS_NONE
) {
1038 mutex_enter(&cpu_lock
);
1039 mutex_enter(&pidlock
);
1040 mutex_enter(&curproc
->p_lock
);
1042 if (cpupart_bind_thread(curthread
, zio_taskq_psrset_bind
,
1043 0, NULL
, NULL
) == 0) {
1044 curthread
->t_bind_pset
= zio_taskq_psrset_bind
;
1047 "Couldn't bind process for zfs pool \"%s\" to "
1048 "pset %d\n", spa
->spa_name
, zio_taskq_psrset_bind
);
1051 mutex_exit(&curproc
->p_lock
);
1052 mutex_exit(&pidlock
);
1053 mutex_exit(&cpu_lock
);
1057 if (zio_taskq_sysdc
) {
1058 sysdc_thread_enter(curthread
, 100, 0);
1061 spa
->spa_proc
= curproc
;
1062 spa
->spa_did
= curthread
->t_did
;
1064 spa_create_zio_taskqs(spa
);
1066 mutex_enter(&spa
->spa_proc_lock
);
1067 ASSERT(spa
->spa_proc_state
== SPA_PROC_CREATED
);
1069 spa
->spa_proc_state
= SPA_PROC_ACTIVE
;
1070 cv_broadcast(&spa
->spa_proc_cv
);
1072 CALLB_CPR_SAFE_BEGIN(&cprinfo
);
1073 while (spa
->spa_proc_state
== SPA_PROC_ACTIVE
)
1074 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1075 CALLB_CPR_SAFE_END(&cprinfo
, &spa
->spa_proc_lock
);
1077 ASSERT(spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
);
1078 spa
->spa_proc_state
= SPA_PROC_GONE
;
1079 spa
->spa_proc
= &p0
;
1080 cv_broadcast(&spa
->spa_proc_cv
);
1081 CALLB_CPR_EXIT(&cprinfo
); /* drops spa_proc_lock */
1083 mutex_enter(&curproc
->p_lock
);
1089 * Activate an uninitialized pool.
1092 spa_activate(spa_t
*spa
, int mode
)
1094 ASSERT(spa
->spa_state
== POOL_STATE_UNINITIALIZED
);
1096 spa
->spa_state
= POOL_STATE_ACTIVE
;
1097 spa
->spa_mode
= mode
;
1099 spa
->spa_normal_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
1100 spa
->spa_log_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
1102 /* Try to create a covering process */
1103 mutex_enter(&spa
->spa_proc_lock
);
1104 ASSERT(spa
->spa_proc_state
== SPA_PROC_NONE
);
1105 ASSERT(spa
->spa_proc
== &p0
);
1108 #ifdef HAVE_SPA_THREAD
1109 /* Only create a process if we're going to be around a while. */
1110 if (spa_create_process
&& strcmp(spa
->spa_name
, TRYIMPORT_NAME
) != 0) {
1111 if (newproc(spa_thread
, (caddr_t
)spa
, syscid
, maxclsyspri
,
1113 spa
->spa_proc_state
= SPA_PROC_CREATED
;
1114 while (spa
->spa_proc_state
== SPA_PROC_CREATED
) {
1115 cv_wait(&spa
->spa_proc_cv
,
1116 &spa
->spa_proc_lock
);
1118 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1119 ASSERT(spa
->spa_proc
!= &p0
);
1120 ASSERT(spa
->spa_did
!= 0);
1124 "Couldn't create process for zfs pool \"%s\"\n",
1129 #endif /* HAVE_SPA_THREAD */
1130 mutex_exit(&spa
->spa_proc_lock
);
1132 /* If we didn't create a process, we need to create our taskqs. */
1133 if (spa
->spa_proc
== &p0
) {
1134 spa_create_zio_taskqs(spa
);
1137 list_create(&spa
->spa_config_dirty_list
, sizeof (vdev_t
),
1138 offsetof(vdev_t
, vdev_config_dirty_node
));
1139 list_create(&spa
->spa_evicting_os_list
, sizeof (objset_t
),
1140 offsetof(objset_t
, os_evicting_node
));
1141 list_create(&spa
->spa_state_dirty_list
, sizeof (vdev_t
),
1142 offsetof(vdev_t
, vdev_state_dirty_node
));
1144 txg_list_create(&spa
->spa_vdev_txg_list
,
1145 offsetof(struct vdev
, vdev_txg_node
));
1147 avl_create(&spa
->spa_errlist_scrub
,
1148 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1149 offsetof(spa_error_entry_t
, se_avl
));
1150 avl_create(&spa
->spa_errlist_last
,
1151 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1152 offsetof(spa_error_entry_t
, se_avl
));
1155 * This taskq is used to perform zvol-minor-related tasks
1156 * asynchronously. This has several advantages, including easy
1157 * resolution of various deadlocks (zfsonlinux bug #3681).
1159 * The taskq must be single threaded to ensure tasks are always
1160 * processed in the order in which they were dispatched.
1162 * A taskq per pool allows one to keep the pools independent.
1163 * This way if one pool is suspended, it will not impact another.
1165 * The preferred location to dispatch a zvol minor task is a sync
1166 * task. In this context, there is easy access to the spa_t and minimal
1167 * error handling is required because the sync task must succeed.
1169 spa
->spa_zvol_taskq
= taskq_create("z_zvol", 1, defclsyspri
,
1174 * Opposite of spa_activate().
1177 spa_deactivate(spa_t
*spa
)
1181 ASSERT(spa
->spa_sync_on
== B_FALSE
);
1182 ASSERT(spa
->spa_dsl_pool
== NULL
);
1183 ASSERT(spa
->spa_root_vdev
== NULL
);
1184 ASSERT(spa
->spa_async_zio_root
== NULL
);
1185 ASSERT(spa
->spa_state
!= POOL_STATE_UNINITIALIZED
);
1187 spa_evicting_os_wait(spa
);
1189 if (spa
->spa_zvol_taskq
) {
1190 taskq_destroy(spa
->spa_zvol_taskq
);
1191 spa
->spa_zvol_taskq
= NULL
;
1194 txg_list_destroy(&spa
->spa_vdev_txg_list
);
1196 list_destroy(&spa
->spa_config_dirty_list
);
1197 list_destroy(&spa
->spa_evicting_os_list
);
1198 list_destroy(&spa
->spa_state_dirty_list
);
1200 taskq_cancel_id(system_taskq
, spa
->spa_deadman_tqid
);
1202 for (t
= 0; t
< ZIO_TYPES
; t
++) {
1203 for (q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1204 spa_taskqs_fini(spa
, t
, q
);
1208 metaslab_class_destroy(spa
->spa_normal_class
);
1209 spa
->spa_normal_class
= NULL
;
1211 metaslab_class_destroy(spa
->spa_log_class
);
1212 spa
->spa_log_class
= NULL
;
1215 * If this was part of an import or the open otherwise failed, we may
1216 * still have errors left in the queues. Empty them just in case.
1218 spa_errlog_drain(spa
);
1220 avl_destroy(&spa
->spa_errlist_scrub
);
1221 avl_destroy(&spa
->spa_errlist_last
);
1223 spa
->spa_state
= POOL_STATE_UNINITIALIZED
;
1225 mutex_enter(&spa
->spa_proc_lock
);
1226 if (spa
->spa_proc_state
!= SPA_PROC_NONE
) {
1227 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1228 spa
->spa_proc_state
= SPA_PROC_DEACTIVATE
;
1229 cv_broadcast(&spa
->spa_proc_cv
);
1230 while (spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
) {
1231 ASSERT(spa
->spa_proc
!= &p0
);
1232 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1234 ASSERT(spa
->spa_proc_state
== SPA_PROC_GONE
);
1235 spa
->spa_proc_state
= SPA_PROC_NONE
;
1237 ASSERT(spa
->spa_proc
== &p0
);
1238 mutex_exit(&spa
->spa_proc_lock
);
1241 * We want to make sure spa_thread() has actually exited the ZFS
1242 * module, so that the module can't be unloaded out from underneath
1245 if (spa
->spa_did
!= 0) {
1246 thread_join(spa
->spa_did
);
1252 * Verify a pool configuration, and construct the vdev tree appropriately. This
1253 * will create all the necessary vdevs in the appropriate layout, with each vdev
1254 * in the CLOSED state. This will prep the pool before open/creation/import.
1255 * All vdev validation is done by the vdev_alloc() routine.
1258 spa_config_parse(spa_t
*spa
, vdev_t
**vdp
, nvlist_t
*nv
, vdev_t
*parent
,
1259 uint_t id
, int atype
)
1266 if ((error
= vdev_alloc(spa
, vdp
, nv
, parent
, id
, atype
)) != 0)
1269 if ((*vdp
)->vdev_ops
->vdev_op_leaf
)
1272 error
= nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1275 if (error
== ENOENT
)
1281 return (SET_ERROR(EINVAL
));
1284 for (c
= 0; c
< children
; c
++) {
1286 if ((error
= spa_config_parse(spa
, &vd
, child
[c
], *vdp
, c
,
1294 ASSERT(*vdp
!= NULL
);
1300 * Opposite of spa_load().
1303 spa_unload(spa_t
*spa
)
1307 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1312 spa_async_suspend(spa
);
1317 if (spa
->spa_sync_on
) {
1318 txg_sync_stop(spa
->spa_dsl_pool
);
1319 spa
->spa_sync_on
= B_FALSE
;
1323 * Wait for any outstanding async I/O to complete.
1325 if (spa
->spa_async_zio_root
!= NULL
) {
1326 for (i
= 0; i
< max_ncpus
; i
++)
1327 (void) zio_wait(spa
->spa_async_zio_root
[i
]);
1328 kmem_free(spa
->spa_async_zio_root
, max_ncpus
* sizeof (void *));
1329 spa
->spa_async_zio_root
= NULL
;
1332 bpobj_close(&spa
->spa_deferred_bpobj
);
1334 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1339 if (spa
->spa_root_vdev
)
1340 vdev_free(spa
->spa_root_vdev
);
1341 ASSERT(spa
->spa_root_vdev
== NULL
);
1344 * Close the dsl pool.
1346 if (spa
->spa_dsl_pool
) {
1347 dsl_pool_close(spa
->spa_dsl_pool
);
1348 spa
->spa_dsl_pool
= NULL
;
1349 spa
->spa_meta_objset
= NULL
;
1356 * Drop and purge level 2 cache
1358 spa_l2cache_drop(spa
);
1360 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1361 vdev_free(spa
->spa_spares
.sav_vdevs
[i
]);
1362 if (spa
->spa_spares
.sav_vdevs
) {
1363 kmem_free(spa
->spa_spares
.sav_vdevs
,
1364 spa
->spa_spares
.sav_count
* sizeof (void *));
1365 spa
->spa_spares
.sav_vdevs
= NULL
;
1367 if (spa
->spa_spares
.sav_config
) {
1368 nvlist_free(spa
->spa_spares
.sav_config
);
1369 spa
->spa_spares
.sav_config
= NULL
;
1371 spa
->spa_spares
.sav_count
= 0;
1373 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
1374 vdev_clear_stats(spa
->spa_l2cache
.sav_vdevs
[i
]);
1375 vdev_free(spa
->spa_l2cache
.sav_vdevs
[i
]);
1377 if (spa
->spa_l2cache
.sav_vdevs
) {
1378 kmem_free(spa
->spa_l2cache
.sav_vdevs
,
1379 spa
->spa_l2cache
.sav_count
* sizeof (void *));
1380 spa
->spa_l2cache
.sav_vdevs
= NULL
;
1382 if (spa
->spa_l2cache
.sav_config
) {
1383 nvlist_free(spa
->spa_l2cache
.sav_config
);
1384 spa
->spa_l2cache
.sav_config
= NULL
;
1386 spa
->spa_l2cache
.sav_count
= 0;
1388 spa
->spa_async_suspended
= 0;
1390 if (spa
->spa_comment
!= NULL
) {
1391 spa_strfree(spa
->spa_comment
);
1392 spa
->spa_comment
= NULL
;
1395 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1399 * Load (or re-load) the current list of vdevs describing the active spares for
1400 * this pool. When this is called, we have some form of basic information in
1401 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1402 * then re-generate a more complete list including status information.
1405 spa_load_spares(spa_t
*spa
)
1412 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1415 * First, close and free any existing spare vdevs.
1417 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1418 vd
= spa
->spa_spares
.sav_vdevs
[i
];
1420 /* Undo the call to spa_activate() below */
1421 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1422 B_FALSE
)) != NULL
&& tvd
->vdev_isspare
)
1423 spa_spare_remove(tvd
);
1428 if (spa
->spa_spares
.sav_vdevs
)
1429 kmem_free(spa
->spa_spares
.sav_vdevs
,
1430 spa
->spa_spares
.sav_count
* sizeof (void *));
1432 if (spa
->spa_spares
.sav_config
== NULL
)
1435 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
1436 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
1438 spa
->spa_spares
.sav_count
= (int)nspares
;
1439 spa
->spa_spares
.sav_vdevs
= NULL
;
1445 * Construct the array of vdevs, opening them to get status in the
1446 * process. For each spare, there is potentially two different vdev_t
1447 * structures associated with it: one in the list of spares (used only
1448 * for basic validation purposes) and one in the active vdev
1449 * configuration (if it's spared in). During this phase we open and
1450 * validate each vdev on the spare list. If the vdev also exists in the
1451 * active configuration, then we also mark this vdev as an active spare.
1453 spa
->spa_spares
.sav_vdevs
= kmem_zalloc(nspares
* sizeof (void *),
1455 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1456 VERIFY(spa_config_parse(spa
, &vd
, spares
[i
], NULL
, 0,
1457 VDEV_ALLOC_SPARE
) == 0);
1460 spa
->spa_spares
.sav_vdevs
[i
] = vd
;
1462 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1463 B_FALSE
)) != NULL
) {
1464 if (!tvd
->vdev_isspare
)
1468 * We only mark the spare active if we were successfully
1469 * able to load the vdev. Otherwise, importing a pool
1470 * with a bad active spare would result in strange
1471 * behavior, because multiple pool would think the spare
1472 * is actively in use.
1474 * There is a vulnerability here to an equally bizarre
1475 * circumstance, where a dead active spare is later
1476 * brought back to life (onlined or otherwise). Given
1477 * the rarity of this scenario, and the extra complexity
1478 * it adds, we ignore the possibility.
1480 if (!vdev_is_dead(tvd
))
1481 spa_spare_activate(tvd
);
1485 vd
->vdev_aux
= &spa
->spa_spares
;
1487 if (vdev_open(vd
) != 0)
1490 if (vdev_validate_aux(vd
) == 0)
1495 * Recompute the stashed list of spares, with status information
1498 VERIFY(nvlist_remove(spa
->spa_spares
.sav_config
, ZPOOL_CONFIG_SPARES
,
1499 DATA_TYPE_NVLIST_ARRAY
) == 0);
1501 spares
= kmem_alloc(spa
->spa_spares
.sav_count
* sizeof (void *),
1503 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1504 spares
[i
] = vdev_config_generate(spa
,
1505 spa
->spa_spares
.sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_SPARE
);
1506 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
1507 ZPOOL_CONFIG_SPARES
, spares
, spa
->spa_spares
.sav_count
) == 0);
1508 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1509 nvlist_free(spares
[i
]);
1510 kmem_free(spares
, spa
->spa_spares
.sav_count
* sizeof (void *));
1514 * Load (or re-load) the current list of vdevs describing the active l2cache for
1515 * this pool. When this is called, we have some form of basic information in
1516 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1517 * then re-generate a more complete list including status information.
1518 * Devices which are already active have their details maintained, and are
1522 spa_load_l2cache(spa_t
*spa
)
1526 int i
, j
, oldnvdevs
;
1528 vdev_t
*vd
, **oldvdevs
, **newvdevs
;
1529 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
1531 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1533 if (sav
->sav_config
!= NULL
) {
1534 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
,
1535 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
1536 newvdevs
= kmem_alloc(nl2cache
* sizeof (void *), KM_SLEEP
);
1542 oldvdevs
= sav
->sav_vdevs
;
1543 oldnvdevs
= sav
->sav_count
;
1544 sav
->sav_vdevs
= NULL
;
1548 * Process new nvlist of vdevs.
1550 for (i
= 0; i
< nl2cache
; i
++) {
1551 VERIFY(nvlist_lookup_uint64(l2cache
[i
], ZPOOL_CONFIG_GUID
,
1555 for (j
= 0; j
< oldnvdevs
; j
++) {
1557 if (vd
!= NULL
&& guid
== vd
->vdev_guid
) {
1559 * Retain previous vdev for add/remove ops.
1567 if (newvdevs
[i
] == NULL
) {
1571 VERIFY(spa_config_parse(spa
, &vd
, l2cache
[i
], NULL
, 0,
1572 VDEV_ALLOC_L2CACHE
) == 0);
1577 * Commit this vdev as an l2cache device,
1578 * even if it fails to open.
1580 spa_l2cache_add(vd
);
1585 spa_l2cache_activate(vd
);
1587 if (vdev_open(vd
) != 0)
1590 (void) vdev_validate_aux(vd
);
1592 if (!vdev_is_dead(vd
))
1593 l2arc_add_vdev(spa
, vd
);
1598 * Purge vdevs that were dropped
1600 for (i
= 0; i
< oldnvdevs
; i
++) {
1605 ASSERT(vd
->vdev_isl2cache
);
1607 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
1608 pool
!= 0ULL && l2arc_vdev_present(vd
))
1609 l2arc_remove_vdev(vd
);
1610 vdev_clear_stats(vd
);
1616 kmem_free(oldvdevs
, oldnvdevs
* sizeof (void *));
1618 if (sav
->sav_config
== NULL
)
1621 sav
->sav_vdevs
= newvdevs
;
1622 sav
->sav_count
= (int)nl2cache
;
1625 * Recompute the stashed list of l2cache devices, with status
1626 * information this time.
1628 VERIFY(nvlist_remove(sav
->sav_config
, ZPOOL_CONFIG_L2CACHE
,
1629 DATA_TYPE_NVLIST_ARRAY
) == 0);
1631 l2cache
= kmem_alloc(sav
->sav_count
* sizeof (void *), KM_SLEEP
);
1632 for (i
= 0; i
< sav
->sav_count
; i
++)
1633 l2cache
[i
] = vdev_config_generate(spa
,
1634 sav
->sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_L2CACHE
);
1635 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
,
1636 ZPOOL_CONFIG_L2CACHE
, l2cache
, sav
->sav_count
) == 0);
1638 for (i
= 0; i
< sav
->sav_count
; i
++)
1639 nvlist_free(l2cache
[i
]);
1641 kmem_free(l2cache
, sav
->sav_count
* sizeof (void *));
1645 load_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
**value
)
1648 char *packed
= NULL
;
1653 error
= dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
);
1657 nvsize
= *(uint64_t *)db
->db_data
;
1658 dmu_buf_rele(db
, FTAG
);
1660 packed
= vmem_alloc(nvsize
, KM_SLEEP
);
1661 error
= dmu_read(spa
->spa_meta_objset
, obj
, 0, nvsize
, packed
,
1664 error
= nvlist_unpack(packed
, nvsize
, value
, 0);
1665 vmem_free(packed
, nvsize
);
1671 * Checks to see if the given vdev could not be opened, in which case we post a
1672 * sysevent to notify the autoreplace code that the device has been removed.
1675 spa_check_removed(vdev_t
*vd
)
1679 for (c
= 0; c
< vd
->vdev_children
; c
++)
1680 spa_check_removed(vd
->vdev_child
[c
]);
1682 if (vd
->vdev_ops
->vdev_op_leaf
&& vdev_is_dead(vd
) &&
1684 zfs_ereport_post(FM_EREPORT_RESOURCE_AUTOREPLACE
,
1685 vd
->vdev_spa
, vd
, NULL
, 0, 0);
1686 spa_event_notify(vd
->vdev_spa
, vd
, FM_EREPORT_ZFS_DEVICE_CHECK
);
1691 spa_config_valid_zaps(vdev_t
*vd
, vdev_t
*mvd
)
1695 ASSERT3U(vd
->vdev_children
, ==, mvd
->vdev_children
);
1697 vd
->vdev_top_zap
= mvd
->vdev_top_zap
;
1698 vd
->vdev_leaf_zap
= mvd
->vdev_leaf_zap
;
1700 for (i
= 0; i
< vd
->vdev_children
; i
++) {
1701 spa_config_valid_zaps(vd
->vdev_child
[i
], mvd
->vdev_child
[i
]);
1706 * Validate the current config against the MOS config
1709 spa_config_valid(spa_t
*spa
, nvlist_t
*config
)
1711 vdev_t
*mrvd
, *rvd
= spa
->spa_root_vdev
;
1715 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nv
) == 0);
1717 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1718 VERIFY(spa_config_parse(spa
, &mrvd
, nv
, NULL
, 0, VDEV_ALLOC_LOAD
) == 0);
1720 ASSERT3U(rvd
->vdev_children
, ==, mrvd
->vdev_children
);
1723 * If we're doing a normal import, then build up any additional
1724 * diagnostic information about missing devices in this config.
1725 * We'll pass this up to the user for further processing.
1727 if (!(spa
->spa_import_flags
& ZFS_IMPORT_MISSING_LOG
)) {
1728 nvlist_t
**child
, *nv
;
1731 child
= kmem_alloc(rvd
->vdev_children
* sizeof (nvlist_t
**),
1733 VERIFY(nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
1735 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1736 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1737 vdev_t
*mtvd
= mrvd
->vdev_child
[c
];
1739 if (tvd
->vdev_ops
== &vdev_missing_ops
&&
1740 mtvd
->vdev_ops
!= &vdev_missing_ops
&&
1742 child
[idx
++] = vdev_config_generate(spa
, mtvd
,
1747 VERIFY(nvlist_add_nvlist_array(nv
,
1748 ZPOOL_CONFIG_CHILDREN
, child
, idx
) == 0);
1749 VERIFY(nvlist_add_nvlist(spa
->spa_load_info
,
1750 ZPOOL_CONFIG_MISSING_DEVICES
, nv
) == 0);
1752 for (i
= 0; i
< idx
; i
++)
1753 nvlist_free(child
[i
]);
1756 kmem_free(child
, rvd
->vdev_children
* sizeof (char **));
1760 * Compare the root vdev tree with the information we have
1761 * from the MOS config (mrvd). Check each top-level vdev
1762 * with the corresponding MOS config top-level (mtvd).
1764 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1765 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1766 vdev_t
*mtvd
= mrvd
->vdev_child
[c
];
1769 * Resolve any "missing" vdevs in the current configuration.
1770 * If we find that the MOS config has more accurate information
1771 * about the top-level vdev then use that vdev instead.
1773 if (tvd
->vdev_ops
== &vdev_missing_ops
&&
1774 mtvd
->vdev_ops
!= &vdev_missing_ops
) {
1776 if (!(spa
->spa_import_flags
& ZFS_IMPORT_MISSING_LOG
))
1780 * Device specific actions.
1782 if (mtvd
->vdev_islog
) {
1783 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
1786 * XXX - once we have 'readonly' pool
1787 * support we should be able to handle
1788 * missing data devices by transitioning
1789 * the pool to readonly.
1795 * Swap the missing vdev with the data we were
1796 * able to obtain from the MOS config.
1798 vdev_remove_child(rvd
, tvd
);
1799 vdev_remove_child(mrvd
, mtvd
);
1801 vdev_add_child(rvd
, mtvd
);
1802 vdev_add_child(mrvd
, tvd
);
1804 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1806 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1810 if (mtvd
->vdev_islog
) {
1812 * Load the slog device's state from the MOS
1813 * config since it's possible that the label
1814 * does not contain the most up-to-date
1817 vdev_load_log_state(tvd
, mtvd
);
1822 * Per-vdev ZAP info is stored exclusively in the MOS.
1824 spa_config_valid_zaps(tvd
, mtvd
);
1829 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1832 * Ensure we were able to validate the config.
1834 return (rvd
->vdev_guid_sum
== spa
->spa_uberblock
.ub_guid_sum
);
1838 * Check for missing log devices
1841 spa_check_logs(spa_t
*spa
)
1843 boolean_t rv
= B_FALSE
;
1844 dsl_pool_t
*dp
= spa_get_dsl(spa
);
1846 switch (spa
->spa_log_state
) {
1849 case SPA_LOG_MISSING
:
1850 /* need to recheck in case slog has been restored */
1851 case SPA_LOG_UNKNOWN
:
1852 rv
= (dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
1853 zil_check_log_chain
, NULL
, DS_FIND_CHILDREN
) != 0);
1855 spa_set_log_state(spa
, SPA_LOG_MISSING
);
1862 spa_passivate_log(spa_t
*spa
)
1864 vdev_t
*rvd
= spa
->spa_root_vdev
;
1865 boolean_t slog_found
= B_FALSE
;
1868 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
1870 if (!spa_has_slogs(spa
))
1873 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1874 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1875 metaslab_group_t
*mg
= tvd
->vdev_mg
;
1877 if (tvd
->vdev_islog
) {
1878 metaslab_group_passivate(mg
);
1879 slog_found
= B_TRUE
;
1883 return (slog_found
);
1887 spa_activate_log(spa_t
*spa
)
1889 vdev_t
*rvd
= spa
->spa_root_vdev
;
1892 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
1894 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1895 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1896 metaslab_group_t
*mg
= tvd
->vdev_mg
;
1898 if (tvd
->vdev_islog
)
1899 metaslab_group_activate(mg
);
1904 spa_offline_log(spa_t
*spa
)
1908 error
= dmu_objset_find(spa_name(spa
), zil_vdev_offline
,
1909 NULL
, DS_FIND_CHILDREN
);
1912 * We successfully offlined the log device, sync out the
1913 * current txg so that the "stubby" block can be removed
1916 txg_wait_synced(spa
->spa_dsl_pool
, 0);
1922 spa_aux_check_removed(spa_aux_vdev_t
*sav
)
1926 for (i
= 0; i
< sav
->sav_count
; i
++)
1927 spa_check_removed(sav
->sav_vdevs
[i
]);
1931 spa_claim_notify(zio_t
*zio
)
1933 spa_t
*spa
= zio
->io_spa
;
1938 mutex_enter(&spa
->spa_props_lock
); /* any mutex will do */
1939 if (spa
->spa_claim_max_txg
< zio
->io_bp
->blk_birth
)
1940 spa
->spa_claim_max_txg
= zio
->io_bp
->blk_birth
;
1941 mutex_exit(&spa
->spa_props_lock
);
1944 typedef struct spa_load_error
{
1945 uint64_t sle_meta_count
;
1946 uint64_t sle_data_count
;
1950 spa_load_verify_done(zio_t
*zio
)
1952 blkptr_t
*bp
= zio
->io_bp
;
1953 spa_load_error_t
*sle
= zio
->io_private
;
1954 dmu_object_type_t type
= BP_GET_TYPE(bp
);
1955 int error
= zio
->io_error
;
1956 spa_t
*spa
= zio
->io_spa
;
1959 if ((BP_GET_LEVEL(bp
) != 0 || DMU_OT_IS_METADATA(type
)) &&
1960 type
!= DMU_OT_INTENT_LOG
)
1961 atomic_inc_64(&sle
->sle_meta_count
);
1963 atomic_inc_64(&sle
->sle_data_count
);
1965 zio_data_buf_free(zio
->io_data
, zio
->io_size
);
1967 mutex_enter(&spa
->spa_scrub_lock
);
1968 spa
->spa_scrub_inflight
--;
1969 cv_broadcast(&spa
->spa_scrub_io_cv
);
1970 mutex_exit(&spa
->spa_scrub_lock
);
1974 * Maximum number of concurrent scrub i/os to create while verifying
1975 * a pool while importing it.
1977 int spa_load_verify_maxinflight
= 10000;
1978 int spa_load_verify_metadata
= B_TRUE
;
1979 int spa_load_verify_data
= B_TRUE
;
1983 spa_load_verify_cb(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
1984 const zbookmark_phys_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
1990 if (bp
== NULL
|| BP_IS_HOLE(bp
) || BP_IS_EMBEDDED(bp
))
1993 * Note: normally this routine will not be called if
1994 * spa_load_verify_metadata is not set. However, it may be useful
1995 * to manually set the flag after the traversal has begun.
1997 if (!spa_load_verify_metadata
)
1999 if (BP_GET_BUFC_TYPE(bp
) == ARC_BUFC_DATA
&& !spa_load_verify_data
)
2003 size
= BP_GET_PSIZE(bp
);
2004 data
= zio_data_buf_alloc(size
);
2006 mutex_enter(&spa
->spa_scrub_lock
);
2007 while (spa
->spa_scrub_inflight
>= spa_load_verify_maxinflight
)
2008 cv_wait(&spa
->spa_scrub_io_cv
, &spa
->spa_scrub_lock
);
2009 spa
->spa_scrub_inflight
++;
2010 mutex_exit(&spa
->spa_scrub_lock
);
2012 zio_nowait(zio_read(rio
, spa
, bp
, data
, size
,
2013 spa_load_verify_done
, rio
->io_private
, ZIO_PRIORITY_SCRUB
,
2014 ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_CANFAIL
|
2015 ZIO_FLAG_SCRUB
| ZIO_FLAG_RAW
, zb
));
2021 verify_dataset_name_len(dsl_pool_t
*dp
, dsl_dataset_t
*ds
, void *arg
)
2023 if (dsl_dataset_namelen(ds
) >= ZFS_MAX_DATASET_NAME_LEN
)
2024 return (SET_ERROR(ENAMETOOLONG
));
2030 spa_load_verify(spa_t
*spa
)
2033 spa_load_error_t sle
= { 0 };
2034 zpool_rewind_policy_t policy
;
2035 boolean_t verify_ok
= B_FALSE
;
2038 zpool_get_rewind_policy(spa
->spa_config
, &policy
);
2040 if (policy
.zrp_request
& ZPOOL_NEVER_REWIND
)
2043 dsl_pool_config_enter(spa
->spa_dsl_pool
, FTAG
);
2044 error
= dmu_objset_find_dp(spa
->spa_dsl_pool
,
2045 spa
->spa_dsl_pool
->dp_root_dir_obj
, verify_dataset_name_len
, NULL
,
2047 dsl_pool_config_exit(spa
->spa_dsl_pool
, FTAG
);
2051 rio
= zio_root(spa
, NULL
, &sle
,
2052 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
);
2054 if (spa_load_verify_metadata
) {
2055 error
= traverse_pool(spa
, spa
->spa_verify_min_txg
,
2056 TRAVERSE_PRE
| TRAVERSE_PREFETCH_METADATA
,
2057 spa_load_verify_cb
, rio
);
2060 (void) zio_wait(rio
);
2062 spa
->spa_load_meta_errors
= sle
.sle_meta_count
;
2063 spa
->spa_load_data_errors
= sle
.sle_data_count
;
2065 if (!error
&& sle
.sle_meta_count
<= policy
.zrp_maxmeta
&&
2066 sle
.sle_data_count
<= policy
.zrp_maxdata
) {
2070 spa
->spa_load_txg
= spa
->spa_uberblock
.ub_txg
;
2071 spa
->spa_load_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
2073 loss
= spa
->spa_last_ubsync_txg_ts
- spa
->spa_load_txg_ts
;
2074 VERIFY(nvlist_add_uint64(spa
->spa_load_info
,
2075 ZPOOL_CONFIG_LOAD_TIME
, spa
->spa_load_txg_ts
) == 0);
2076 VERIFY(nvlist_add_int64(spa
->spa_load_info
,
2077 ZPOOL_CONFIG_REWIND_TIME
, loss
) == 0);
2078 VERIFY(nvlist_add_uint64(spa
->spa_load_info
,
2079 ZPOOL_CONFIG_LOAD_DATA_ERRORS
, sle
.sle_data_count
) == 0);
2081 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
;
2085 if (error
!= ENXIO
&& error
!= EIO
)
2086 error
= SET_ERROR(EIO
);
2090 return (verify_ok
? 0 : EIO
);
2094 * Find a value in the pool props object.
2097 spa_prop_find(spa_t
*spa
, zpool_prop_t prop
, uint64_t *val
)
2099 (void) zap_lookup(spa
->spa_meta_objset
, spa
->spa_pool_props_object
,
2100 zpool_prop_to_name(prop
), sizeof (uint64_t), 1, val
);
2104 * Find a value in the pool directory object.
2107 spa_dir_prop(spa_t
*spa
, const char *name
, uint64_t *val
)
2109 return (zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
2110 name
, sizeof (uint64_t), 1, val
));
2114 spa_vdev_err(vdev_t
*vdev
, vdev_aux_t aux
, int err
)
2116 vdev_set_state(vdev
, B_TRUE
, VDEV_STATE_CANT_OPEN
, aux
);
2121 * Fix up config after a partly-completed split. This is done with the
2122 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
2123 * pool have that entry in their config, but only the splitting one contains
2124 * a list of all the guids of the vdevs that are being split off.
2126 * This function determines what to do with that list: either rejoin
2127 * all the disks to the pool, or complete the splitting process. To attempt
2128 * the rejoin, each disk that is offlined is marked online again, and
2129 * we do a reopen() call. If the vdev label for every disk that was
2130 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
2131 * then we call vdev_split() on each disk, and complete the split.
2133 * Otherwise we leave the config alone, with all the vdevs in place in
2134 * the original pool.
2137 spa_try_repair(spa_t
*spa
, nvlist_t
*config
)
2144 boolean_t attempt_reopen
;
2146 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
, &nvl
) != 0)
2149 /* check that the config is complete */
2150 if (nvlist_lookup_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
2151 &glist
, &gcount
) != 0)
2154 vd
= kmem_zalloc(gcount
* sizeof (vdev_t
*), KM_SLEEP
);
2156 /* attempt to online all the vdevs & validate */
2157 attempt_reopen
= B_TRUE
;
2158 for (i
= 0; i
< gcount
; i
++) {
2159 if (glist
[i
] == 0) /* vdev is hole */
2162 vd
[i
] = spa_lookup_by_guid(spa
, glist
[i
], B_FALSE
);
2163 if (vd
[i
] == NULL
) {
2165 * Don't bother attempting to reopen the disks;
2166 * just do the split.
2168 attempt_reopen
= B_FALSE
;
2170 /* attempt to re-online it */
2171 vd
[i
]->vdev_offline
= B_FALSE
;
2175 if (attempt_reopen
) {
2176 vdev_reopen(spa
->spa_root_vdev
);
2178 /* check each device to see what state it's in */
2179 for (extracted
= 0, i
= 0; i
< gcount
; i
++) {
2180 if (vd
[i
] != NULL
&&
2181 vd
[i
]->vdev_stat
.vs_aux
!= VDEV_AUX_SPLIT_POOL
)
2188 * If every disk has been moved to the new pool, or if we never
2189 * even attempted to look at them, then we split them off for
2192 if (!attempt_reopen
|| gcount
== extracted
) {
2193 for (i
= 0; i
< gcount
; i
++)
2196 vdev_reopen(spa
->spa_root_vdev
);
2199 kmem_free(vd
, gcount
* sizeof (vdev_t
*));
2203 spa_load(spa_t
*spa
, spa_load_state_t state
, spa_import_type_t type
,
2204 boolean_t mosconfig
)
2206 nvlist_t
*config
= spa
->spa_config
;
2207 char *ereport
= FM_EREPORT_ZFS_POOL
;
2213 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
, &pool_guid
))
2214 return (SET_ERROR(EINVAL
));
2216 ASSERT(spa
->spa_comment
== NULL
);
2217 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
2218 spa
->spa_comment
= spa_strdup(comment
);
2221 * Versioning wasn't explicitly added to the label until later, so if
2222 * it's not present treat it as the initial version.
2224 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
2225 &spa
->spa_ubsync
.ub_version
) != 0)
2226 spa
->spa_ubsync
.ub_version
= SPA_VERSION_INITIAL
;
2228 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
2229 &spa
->spa_config_txg
);
2231 if ((state
== SPA_LOAD_IMPORT
|| state
== SPA_LOAD_TRYIMPORT
) &&
2232 spa_guid_exists(pool_guid
, 0)) {
2233 error
= SET_ERROR(EEXIST
);
2235 spa
->spa_config_guid
= pool_guid
;
2237 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
,
2239 VERIFY(nvlist_dup(nvl
, &spa
->spa_config_splitting
,
2243 nvlist_free(spa
->spa_load_info
);
2244 spa
->spa_load_info
= fnvlist_alloc();
2246 gethrestime(&spa
->spa_loaded_ts
);
2247 error
= spa_load_impl(spa
, pool_guid
, config
, state
, type
,
2248 mosconfig
, &ereport
);
2252 * Don't count references from objsets that are already closed
2253 * and are making their way through the eviction process.
2255 spa_evicting_os_wait(spa
);
2256 spa
->spa_minref
= refcount_count(&spa
->spa_refcount
);
2258 if (error
!= EEXIST
) {
2259 spa
->spa_loaded_ts
.tv_sec
= 0;
2260 spa
->spa_loaded_ts
.tv_nsec
= 0;
2262 if (error
!= EBADF
) {
2263 zfs_ereport_post(ereport
, spa
, NULL
, NULL
, 0, 0);
2266 spa
->spa_load_state
= error
? SPA_LOAD_ERROR
: SPA_LOAD_NONE
;
2274 * Count the number of per-vdev ZAPs associated with all of the vdevs in the
2275 * vdev tree rooted in the given vd, and ensure that each ZAP is present in the
2276 * spa's per-vdev ZAP list.
2279 vdev_count_verify_zaps(vdev_t
*vd
)
2281 spa_t
*spa
= vd
->vdev_spa
;
2285 if (vd
->vdev_top_zap
!= 0) {
2287 ASSERT0(zap_lookup_int(spa
->spa_meta_objset
,
2288 spa
->spa_all_vdev_zaps
, vd
->vdev_top_zap
));
2290 if (vd
->vdev_leaf_zap
!= 0) {
2292 ASSERT0(zap_lookup_int(spa
->spa_meta_objset
,
2293 spa
->spa_all_vdev_zaps
, vd
->vdev_leaf_zap
));
2296 for (i
= 0; i
< vd
->vdev_children
; i
++) {
2297 total
+= vdev_count_verify_zaps(vd
->vdev_child
[i
]);
2305 * Load an existing storage pool, using the pool's builtin spa_config as a
2306 * source of configuration information.
2308 __attribute__((always_inline
))
2310 spa_load_impl(spa_t
*spa
, uint64_t pool_guid
, nvlist_t
*config
,
2311 spa_load_state_t state
, spa_import_type_t type
, boolean_t mosconfig
,
2315 nvlist_t
*nvroot
= NULL
;
2318 uberblock_t
*ub
= &spa
->spa_uberblock
;
2319 uint64_t children
, config_cache_txg
= spa
->spa_config_txg
;
2320 int orig_mode
= spa
->spa_mode
;
2323 boolean_t missing_feat_write
= B_FALSE
;
2324 nvlist_t
*mos_config
;
2327 * If this is an untrusted config, access the pool in read-only mode.
2328 * This prevents things like resilvering recently removed devices.
2331 spa
->spa_mode
= FREAD
;
2333 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
2335 spa
->spa_load_state
= state
;
2337 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvroot
))
2338 return (SET_ERROR(EINVAL
));
2340 parse
= (type
== SPA_IMPORT_EXISTING
?
2341 VDEV_ALLOC_LOAD
: VDEV_ALLOC_SPLIT
);
2344 * Create "The Godfather" zio to hold all async IOs
2346 spa
->spa_async_zio_root
= kmem_alloc(max_ncpus
* sizeof (void *),
2348 for (i
= 0; i
< max_ncpus
; i
++) {
2349 spa
->spa_async_zio_root
[i
] = zio_root(spa
, NULL
, NULL
,
2350 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
2351 ZIO_FLAG_GODFATHER
);
2355 * Parse the configuration into a vdev tree. We explicitly set the
2356 * value that will be returned by spa_version() since parsing the
2357 * configuration requires knowing the version number.
2359 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2360 error
= spa_config_parse(spa
, &rvd
, nvroot
, NULL
, 0, parse
);
2361 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2366 ASSERT(spa
->spa_root_vdev
== rvd
);
2367 ASSERT3U(spa
->spa_min_ashift
, >=, SPA_MINBLOCKSHIFT
);
2368 ASSERT3U(spa
->spa_max_ashift
, <=, SPA_MAXBLOCKSHIFT
);
2370 if (type
!= SPA_IMPORT_ASSEMBLE
) {
2371 ASSERT(spa_guid(spa
) == pool_guid
);
2375 * Try to open all vdevs, loading each label in the process.
2377 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2378 error
= vdev_open(rvd
);
2379 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2384 * We need to validate the vdev labels against the configuration that
2385 * we have in hand, which is dependent on the setting of mosconfig. If
2386 * mosconfig is true then we're validating the vdev labels based on
2387 * that config. Otherwise, we're validating against the cached config
2388 * (zpool.cache) that was read when we loaded the zfs module, and then
2389 * later we will recursively call spa_load() and validate against
2392 * If we're assembling a new pool that's been split off from an
2393 * existing pool, the labels haven't yet been updated so we skip
2394 * validation for now.
2396 if (type
!= SPA_IMPORT_ASSEMBLE
) {
2397 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2398 error
= vdev_validate(rvd
, mosconfig
);
2399 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2404 if (rvd
->vdev_state
<= VDEV_STATE_CANT_OPEN
)
2405 return (SET_ERROR(ENXIO
));
2409 * Find the best uberblock.
2411 vdev_uberblock_load(rvd
, ub
, &label
);
2414 * If we weren't able to find a single valid uberblock, return failure.
2416 if (ub
->ub_txg
== 0) {
2418 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, ENXIO
));
2422 * If the pool has an unsupported version we can't open it.
2424 if (!SPA_VERSION_IS_SUPPORTED(ub
->ub_version
)) {
2426 return (spa_vdev_err(rvd
, VDEV_AUX_VERSION_NEWER
, ENOTSUP
));
2429 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
2433 * If we weren't able to find what's necessary for reading the
2434 * MOS in the label, return failure.
2436 if (label
== NULL
|| nvlist_lookup_nvlist(label
,
2437 ZPOOL_CONFIG_FEATURES_FOR_READ
, &features
) != 0) {
2439 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
2444 * Update our in-core representation with the definitive values
2447 nvlist_free(spa
->spa_label_features
);
2448 VERIFY(nvlist_dup(features
, &spa
->spa_label_features
, 0) == 0);
2454 * Look through entries in the label nvlist's features_for_read. If
2455 * there is a feature listed there which we don't understand then we
2456 * cannot open a pool.
2458 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
2459 nvlist_t
*unsup_feat
;
2462 VERIFY(nvlist_alloc(&unsup_feat
, NV_UNIQUE_NAME
, KM_SLEEP
) ==
2465 for (nvp
= nvlist_next_nvpair(spa
->spa_label_features
, NULL
);
2467 nvp
= nvlist_next_nvpair(spa
->spa_label_features
, nvp
)) {
2468 if (!zfeature_is_supported(nvpair_name(nvp
))) {
2469 VERIFY(nvlist_add_string(unsup_feat
,
2470 nvpair_name(nvp
), "") == 0);
2474 if (!nvlist_empty(unsup_feat
)) {
2475 VERIFY(nvlist_add_nvlist(spa
->spa_load_info
,
2476 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
) == 0);
2477 nvlist_free(unsup_feat
);
2478 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
2482 nvlist_free(unsup_feat
);
2486 * If the vdev guid sum doesn't match the uberblock, we have an
2487 * incomplete configuration. We first check to see if the pool
2488 * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN).
2489 * If it is, defer the vdev_guid_sum check till later so we
2490 * can handle missing vdevs.
2492 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VDEV_CHILDREN
,
2493 &children
) != 0 && mosconfig
&& type
!= SPA_IMPORT_ASSEMBLE
&&
2494 rvd
->vdev_guid_sum
!= ub
->ub_guid_sum
)
2495 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
, ENXIO
));
2497 if (type
!= SPA_IMPORT_ASSEMBLE
&& spa
->spa_config_splitting
) {
2498 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2499 spa_try_repair(spa
, config
);
2500 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2501 nvlist_free(spa
->spa_config_splitting
);
2502 spa
->spa_config_splitting
= NULL
;
2506 * Initialize internal SPA structures.
2508 spa
->spa_state
= POOL_STATE_ACTIVE
;
2509 spa
->spa_ubsync
= spa
->spa_uberblock
;
2510 spa
->spa_verify_min_txg
= spa
->spa_extreme_rewind
?
2511 TXG_INITIAL
- 1 : spa_last_synced_txg(spa
) - TXG_DEFER_SIZE
- 1;
2512 spa
->spa_first_txg
= spa
->spa_last_ubsync_txg
?
2513 spa
->spa_last_ubsync_txg
: spa_last_synced_txg(spa
) + 1;
2514 spa
->spa_claim_max_txg
= spa
->spa_first_txg
;
2515 spa
->spa_prev_software_version
= ub
->ub_software_version
;
2517 error
= dsl_pool_init(spa
, spa
->spa_first_txg
, &spa
->spa_dsl_pool
);
2519 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2520 spa
->spa_meta_objset
= spa
->spa_dsl_pool
->dp_meta_objset
;
2522 if (spa_dir_prop(spa
, DMU_POOL_CONFIG
, &spa
->spa_config_object
) != 0)
2523 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2525 if (spa_version(spa
) >= SPA_VERSION_FEATURES
) {
2526 boolean_t missing_feat_read
= B_FALSE
;
2527 nvlist_t
*unsup_feat
, *enabled_feat
;
2530 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_READ
,
2531 &spa
->spa_feat_for_read_obj
) != 0) {
2532 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2535 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_WRITE
,
2536 &spa
->spa_feat_for_write_obj
) != 0) {
2537 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2540 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_DESCRIPTIONS
,
2541 &spa
->spa_feat_desc_obj
) != 0) {
2542 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2545 enabled_feat
= fnvlist_alloc();
2546 unsup_feat
= fnvlist_alloc();
2548 if (!spa_features_check(spa
, B_FALSE
,
2549 unsup_feat
, enabled_feat
))
2550 missing_feat_read
= B_TRUE
;
2552 if (spa_writeable(spa
) || state
== SPA_LOAD_TRYIMPORT
) {
2553 if (!spa_features_check(spa
, B_TRUE
,
2554 unsup_feat
, enabled_feat
)) {
2555 missing_feat_write
= B_TRUE
;
2559 fnvlist_add_nvlist(spa
->spa_load_info
,
2560 ZPOOL_CONFIG_ENABLED_FEAT
, enabled_feat
);
2562 if (!nvlist_empty(unsup_feat
)) {
2563 fnvlist_add_nvlist(spa
->spa_load_info
,
2564 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
);
2567 fnvlist_free(enabled_feat
);
2568 fnvlist_free(unsup_feat
);
2570 if (!missing_feat_read
) {
2571 fnvlist_add_boolean(spa
->spa_load_info
,
2572 ZPOOL_CONFIG_CAN_RDONLY
);
2576 * If the state is SPA_LOAD_TRYIMPORT, our objective is
2577 * twofold: to determine whether the pool is available for
2578 * import in read-write mode and (if it is not) whether the
2579 * pool is available for import in read-only mode. If the pool
2580 * is available for import in read-write mode, it is displayed
2581 * as available in userland; if it is not available for import
2582 * in read-only mode, it is displayed as unavailable in
2583 * userland. If the pool is available for import in read-only
2584 * mode but not read-write mode, it is displayed as unavailable
2585 * in userland with a special note that the pool is actually
2586 * available for open in read-only mode.
2588 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
2589 * missing a feature for write, we must first determine whether
2590 * the pool can be opened read-only before returning to
2591 * userland in order to know whether to display the
2592 * abovementioned note.
2594 if (missing_feat_read
|| (missing_feat_write
&&
2595 spa_writeable(spa
))) {
2596 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
2601 * Load refcounts for ZFS features from disk into an in-memory
2602 * cache during SPA initialization.
2604 for (i
= 0; i
< SPA_FEATURES
; i
++) {
2607 error
= feature_get_refcount_from_disk(spa
,
2608 &spa_feature_table
[i
], &refcount
);
2610 spa
->spa_feat_refcount_cache
[i
] = refcount
;
2611 } else if (error
== ENOTSUP
) {
2612 spa
->spa_feat_refcount_cache
[i
] =
2613 SPA_FEATURE_DISABLED
;
2615 return (spa_vdev_err(rvd
,
2616 VDEV_AUX_CORRUPT_DATA
, EIO
));
2621 if (spa_feature_is_active(spa
, SPA_FEATURE_ENABLED_TXG
)) {
2622 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_ENABLED_TXG
,
2623 &spa
->spa_feat_enabled_txg_obj
) != 0)
2624 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2627 spa
->spa_is_initializing
= B_TRUE
;
2628 error
= dsl_pool_open(spa
->spa_dsl_pool
);
2629 spa
->spa_is_initializing
= B_FALSE
;
2631 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2635 nvlist_t
*policy
= NULL
, *nvconfig
;
2637 if (load_nvlist(spa
, spa
->spa_config_object
, &nvconfig
) != 0)
2638 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2640 if (!spa_is_root(spa
) && nvlist_lookup_uint64(nvconfig
,
2641 ZPOOL_CONFIG_HOSTID
, &hostid
) == 0) {
2643 unsigned long myhostid
= 0;
2645 VERIFY(nvlist_lookup_string(nvconfig
,
2646 ZPOOL_CONFIG_HOSTNAME
, &hostname
) == 0);
2649 myhostid
= zone_get_hostid(NULL
);
2652 * We're emulating the system's hostid in userland, so
2653 * we can't use zone_get_hostid().
2655 (void) ddi_strtoul(hw_serial
, NULL
, 10, &myhostid
);
2656 #endif /* _KERNEL */
2657 if (hostid
!= 0 && myhostid
!= 0 &&
2658 hostid
!= myhostid
) {
2659 nvlist_free(nvconfig
);
2660 cmn_err(CE_WARN
, "pool '%s' could not be "
2661 "loaded as it was last accessed by another "
2662 "system (host: %s hostid: 0x%lx). See: "
2663 "http://zfsonlinux.org/msg/ZFS-8000-EY",
2664 spa_name(spa
), hostname
,
2665 (unsigned long)hostid
);
2666 return (SET_ERROR(EBADF
));
2669 if (nvlist_lookup_nvlist(spa
->spa_config
,
2670 ZPOOL_REWIND_POLICY
, &policy
) == 0)
2671 VERIFY(nvlist_add_nvlist(nvconfig
,
2672 ZPOOL_REWIND_POLICY
, policy
) == 0);
2674 spa_config_set(spa
, nvconfig
);
2676 spa_deactivate(spa
);
2677 spa_activate(spa
, orig_mode
);
2679 return (spa_load(spa
, state
, SPA_IMPORT_EXISTING
, B_TRUE
));
2682 if (spa_dir_prop(spa
, DMU_POOL_SYNC_BPOBJ
, &obj
) != 0)
2683 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2684 error
= bpobj_open(&spa
->spa_deferred_bpobj
, spa
->spa_meta_objset
, obj
);
2686 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2689 * Load the bit that tells us to use the new accounting function
2690 * (raid-z deflation). If we have an older pool, this will not
2693 error
= spa_dir_prop(spa
, DMU_POOL_DEFLATE
, &spa
->spa_deflate
);
2694 if (error
!= 0 && error
!= ENOENT
)
2695 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2697 error
= spa_dir_prop(spa
, DMU_POOL_CREATION_VERSION
,
2698 &spa
->spa_creation_version
);
2699 if (error
!= 0 && error
!= ENOENT
)
2700 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2703 * Load the persistent error log. If we have an older pool, this will
2706 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_LAST
, &spa
->spa_errlog_last
);
2707 if (error
!= 0 && error
!= ENOENT
)
2708 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2710 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_SCRUB
,
2711 &spa
->spa_errlog_scrub
);
2712 if (error
!= 0 && error
!= ENOENT
)
2713 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2716 * Load the history object. If we have an older pool, this
2717 * will not be present.
2719 error
= spa_dir_prop(spa
, DMU_POOL_HISTORY
, &spa
->spa_history
);
2720 if (error
!= 0 && error
!= ENOENT
)
2721 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2724 * Load the per-vdev ZAP map. If we have an older pool, this will not
2725 * be present; in this case, defer its creation to a later time to
2726 * avoid dirtying the MOS this early / out of sync context. See
2727 * spa_sync_config_object.
2730 /* The sentinel is only available in the MOS config. */
2731 if (load_nvlist(spa
, spa
->spa_config_object
, &mos_config
) != 0)
2732 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2734 error
= spa_dir_prop(spa
, DMU_POOL_VDEV_ZAP_MAP
,
2735 &spa
->spa_all_vdev_zaps
);
2737 if (error
!= ENOENT
&& error
!= 0) {
2738 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2739 } else if (error
== 0 && !nvlist_exists(mos_config
,
2740 ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
)) {
2742 * An older version of ZFS overwrote the sentinel value, so
2743 * we have orphaned per-vdev ZAPs in the MOS. Defer their
2744 * destruction to later; see spa_sync_config_object.
2746 spa
->spa_avz_action
= AVZ_ACTION_DESTROY
;
2748 * We're assuming that no vdevs have had their ZAPs created
2749 * before this. Better be sure of it.
2751 ASSERT0(vdev_count_verify_zaps(spa
->spa_root_vdev
));
2753 nvlist_free(mos_config
);
2756 * If we're assembling the pool from the split-off vdevs of
2757 * an existing pool, we don't want to attach the spares & cache
2762 * Load any hot spares for this pool.
2764 error
= spa_dir_prop(spa
, DMU_POOL_SPARES
, &spa
->spa_spares
.sav_object
);
2765 if (error
!= 0 && error
!= ENOENT
)
2766 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2767 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
2768 ASSERT(spa_version(spa
) >= SPA_VERSION_SPARES
);
2769 if (load_nvlist(spa
, spa
->spa_spares
.sav_object
,
2770 &spa
->spa_spares
.sav_config
) != 0)
2771 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2773 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2774 spa_load_spares(spa
);
2775 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2776 } else if (error
== 0) {
2777 spa
->spa_spares
.sav_sync
= B_TRUE
;
2781 * Load any level 2 ARC devices for this pool.
2783 error
= spa_dir_prop(spa
, DMU_POOL_L2CACHE
,
2784 &spa
->spa_l2cache
.sav_object
);
2785 if (error
!= 0 && error
!= ENOENT
)
2786 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2787 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
2788 ASSERT(spa_version(spa
) >= SPA_VERSION_L2CACHE
);
2789 if (load_nvlist(spa
, spa
->spa_l2cache
.sav_object
,
2790 &spa
->spa_l2cache
.sav_config
) != 0)
2791 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2793 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2794 spa_load_l2cache(spa
);
2795 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2796 } else if (error
== 0) {
2797 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
2800 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
2802 error
= spa_dir_prop(spa
, DMU_POOL_PROPS
, &spa
->spa_pool_props_object
);
2803 if (error
&& error
!= ENOENT
)
2804 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2807 uint64_t autoreplace
= 0;
2809 spa_prop_find(spa
, ZPOOL_PROP_BOOTFS
, &spa
->spa_bootfs
);
2810 spa_prop_find(spa
, ZPOOL_PROP_AUTOREPLACE
, &autoreplace
);
2811 spa_prop_find(spa
, ZPOOL_PROP_DELEGATION
, &spa
->spa_delegation
);
2812 spa_prop_find(spa
, ZPOOL_PROP_FAILUREMODE
, &spa
->spa_failmode
);
2813 spa_prop_find(spa
, ZPOOL_PROP_AUTOEXPAND
, &spa
->spa_autoexpand
);
2814 spa_prop_find(spa
, ZPOOL_PROP_DEDUPDITTO
,
2815 &spa
->spa_dedup_ditto
);
2817 spa
->spa_autoreplace
= (autoreplace
!= 0);
2821 * If the 'autoreplace' property is set, then post a resource notifying
2822 * the ZFS DE that it should not issue any faults for unopenable
2823 * devices. We also iterate over the vdevs, and post a sysevent for any
2824 * unopenable vdevs so that the normal autoreplace handler can take
2827 if (spa
->spa_autoreplace
&& state
!= SPA_LOAD_TRYIMPORT
) {
2828 spa_check_removed(spa
->spa_root_vdev
);
2830 * For the import case, this is done in spa_import(), because
2831 * at this point we're using the spare definitions from
2832 * the MOS config, not necessarily from the userland config.
2834 if (state
!= SPA_LOAD_IMPORT
) {
2835 spa_aux_check_removed(&spa
->spa_spares
);
2836 spa_aux_check_removed(&spa
->spa_l2cache
);
2841 * Load the vdev state for all toplevel vdevs.
2846 * Propagate the leaf DTLs we just loaded all the way up the tree.
2848 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2849 vdev_dtl_reassess(rvd
, 0, 0, B_FALSE
);
2850 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2853 * Load the DDTs (dedup tables).
2855 error
= ddt_load(spa
);
2857 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2859 spa_update_dspace(spa
);
2862 * Validate the config, using the MOS config to fill in any
2863 * information which might be missing. If we fail to validate
2864 * the config then declare the pool unfit for use. If we're
2865 * assembling a pool from a split, the log is not transferred
2868 if (type
!= SPA_IMPORT_ASSEMBLE
) {
2871 if (load_nvlist(spa
, spa
->spa_config_object
, &nvconfig
) != 0)
2872 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2874 if (!spa_config_valid(spa
, nvconfig
)) {
2875 nvlist_free(nvconfig
);
2876 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
,
2879 nvlist_free(nvconfig
);
2882 * Now that we've validated the config, check the state of the
2883 * root vdev. If it can't be opened, it indicates one or
2884 * more toplevel vdevs are faulted.
2886 if (rvd
->vdev_state
<= VDEV_STATE_CANT_OPEN
)
2887 return (SET_ERROR(ENXIO
));
2889 if (spa_writeable(spa
) && spa_check_logs(spa
)) {
2890 *ereport
= FM_EREPORT_ZFS_LOG_REPLAY
;
2891 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_LOG
, ENXIO
));
2895 if (missing_feat_write
) {
2896 ASSERT(state
== SPA_LOAD_TRYIMPORT
);
2899 * At this point, we know that we can open the pool in
2900 * read-only mode but not read-write mode. We now have enough
2901 * information and can return to userland.
2903 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
, ENOTSUP
));
2907 * We've successfully opened the pool, verify that we're ready
2908 * to start pushing transactions.
2910 if (state
!= SPA_LOAD_TRYIMPORT
) {
2911 if ((error
= spa_load_verify(spa
)))
2912 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
2916 if (spa_writeable(spa
) && (state
== SPA_LOAD_RECOVER
||
2917 spa
->spa_load_max_txg
== UINT64_MAX
)) {
2919 int need_update
= B_FALSE
;
2920 dsl_pool_t
*dp
= spa_get_dsl(spa
);
2923 ASSERT(state
!= SPA_LOAD_TRYIMPORT
);
2926 * Claim log blocks that haven't been committed yet.
2927 * This must all happen in a single txg.
2928 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
2929 * invoked from zil_claim_log_block()'s i/o done callback.
2930 * Price of rollback is that we abandon the log.
2932 spa
->spa_claiming
= B_TRUE
;
2934 tx
= dmu_tx_create_assigned(dp
, spa_first_txg(spa
));
2935 (void) dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
2936 zil_claim
, tx
, DS_FIND_CHILDREN
);
2939 spa
->spa_claiming
= B_FALSE
;
2941 spa_set_log_state(spa
, SPA_LOG_GOOD
);
2942 spa
->spa_sync_on
= B_TRUE
;
2943 txg_sync_start(spa
->spa_dsl_pool
);
2946 * Wait for all claims to sync. We sync up to the highest
2947 * claimed log block birth time so that claimed log blocks
2948 * don't appear to be from the future. spa_claim_max_txg
2949 * will have been set for us by either zil_check_log_chain()
2950 * (invoked from spa_check_logs()) or zil_claim() above.
2952 txg_wait_synced(spa
->spa_dsl_pool
, spa
->spa_claim_max_txg
);
2955 * If the config cache is stale, or we have uninitialized
2956 * metaslabs (see spa_vdev_add()), then update the config.
2958 * If this is a verbatim import, trust the current
2959 * in-core spa_config and update the disk labels.
2961 if (config_cache_txg
!= spa
->spa_config_txg
||
2962 state
== SPA_LOAD_IMPORT
||
2963 state
== SPA_LOAD_RECOVER
||
2964 (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
))
2965 need_update
= B_TRUE
;
2967 for (c
= 0; c
< rvd
->vdev_children
; c
++)
2968 if (rvd
->vdev_child
[c
]->vdev_ms_array
== 0)
2969 need_update
= B_TRUE
;
2972 * Update the config cache asychronously in case we're the
2973 * root pool, in which case the config cache isn't writable yet.
2976 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
2979 * Check all DTLs to see if anything needs resilvering.
2981 if (!dsl_scan_resilvering(spa
->spa_dsl_pool
) &&
2982 vdev_resilver_needed(rvd
, NULL
, NULL
))
2983 spa_async_request(spa
, SPA_ASYNC_RESILVER
);
2986 * Log the fact that we booted up (so that we can detect if
2987 * we rebooted in the middle of an operation).
2989 spa_history_log_version(spa
, "open");
2992 * Delete any inconsistent datasets.
2994 (void) dmu_objset_find(spa_name(spa
),
2995 dsl_destroy_inconsistent
, NULL
, DS_FIND_CHILDREN
);
2998 * Clean up any stale temporary dataset userrefs.
3000 dsl_pool_clean_tmp_userrefs(spa
->spa_dsl_pool
);
3007 spa_load_retry(spa_t
*spa
, spa_load_state_t state
, int mosconfig
)
3009 int mode
= spa
->spa_mode
;
3012 spa_deactivate(spa
);
3014 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
- 1;
3016 spa_activate(spa
, mode
);
3017 spa_async_suspend(spa
);
3019 return (spa_load(spa
, state
, SPA_IMPORT_EXISTING
, mosconfig
));
3023 * If spa_load() fails this function will try loading prior txg's. If
3024 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
3025 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
3026 * function will not rewind the pool and will return the same error as
3030 spa_load_best(spa_t
*spa
, spa_load_state_t state
, int mosconfig
,
3031 uint64_t max_request
, int rewind_flags
)
3033 nvlist_t
*loadinfo
= NULL
;
3034 nvlist_t
*config
= NULL
;
3035 int load_error
, rewind_error
;
3036 uint64_t safe_rewind_txg
;
3039 if (spa
->spa_load_txg
&& state
== SPA_LOAD_RECOVER
) {
3040 spa
->spa_load_max_txg
= spa
->spa_load_txg
;
3041 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
3043 spa
->spa_load_max_txg
= max_request
;
3044 if (max_request
!= UINT64_MAX
)
3045 spa
->spa_extreme_rewind
= B_TRUE
;
3048 load_error
= rewind_error
= spa_load(spa
, state
, SPA_IMPORT_EXISTING
,
3050 if (load_error
== 0)
3053 if (spa
->spa_root_vdev
!= NULL
)
3054 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
3056 spa
->spa_last_ubsync_txg
= spa
->spa_uberblock
.ub_txg
;
3057 spa
->spa_last_ubsync_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
3059 if (rewind_flags
& ZPOOL_NEVER_REWIND
) {
3060 nvlist_free(config
);
3061 return (load_error
);
3064 if (state
== SPA_LOAD_RECOVER
) {
3065 /* Price of rolling back is discarding txgs, including log */
3066 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
3069 * If we aren't rolling back save the load info from our first
3070 * import attempt so that we can restore it after attempting
3073 loadinfo
= spa
->spa_load_info
;
3074 spa
->spa_load_info
= fnvlist_alloc();
3077 spa
->spa_load_max_txg
= spa
->spa_last_ubsync_txg
;
3078 safe_rewind_txg
= spa
->spa_last_ubsync_txg
- TXG_DEFER_SIZE
;
3079 min_txg
= (rewind_flags
& ZPOOL_EXTREME_REWIND
) ?
3080 TXG_INITIAL
: safe_rewind_txg
;
3083 * Continue as long as we're finding errors, we're still within
3084 * the acceptable rewind range, and we're still finding uberblocks
3086 while (rewind_error
&& spa
->spa_uberblock
.ub_txg
>= min_txg
&&
3087 spa
->spa_uberblock
.ub_txg
<= spa
->spa_load_max_txg
) {
3088 if (spa
->spa_load_max_txg
< safe_rewind_txg
)
3089 spa
->spa_extreme_rewind
= B_TRUE
;
3090 rewind_error
= spa_load_retry(spa
, state
, mosconfig
);
3093 spa
->spa_extreme_rewind
= B_FALSE
;
3094 spa
->spa_load_max_txg
= UINT64_MAX
;
3096 if (config
&& (rewind_error
|| state
!= SPA_LOAD_RECOVER
))
3097 spa_config_set(spa
, config
);
3099 if (state
== SPA_LOAD_RECOVER
) {
3100 ASSERT3P(loadinfo
, ==, NULL
);
3101 return (rewind_error
);
3103 /* Store the rewind info as part of the initial load info */
3104 fnvlist_add_nvlist(loadinfo
, ZPOOL_CONFIG_REWIND_INFO
,
3105 spa
->spa_load_info
);
3107 /* Restore the initial load info */
3108 fnvlist_free(spa
->spa_load_info
);
3109 spa
->spa_load_info
= loadinfo
;
3111 return (load_error
);
3118 * The import case is identical to an open except that the configuration is sent
3119 * down from userland, instead of grabbed from the configuration cache. For the
3120 * case of an open, the pool configuration will exist in the
3121 * POOL_STATE_UNINITIALIZED state.
3123 * The stats information (gen/count/ustats) is used to gather vdev statistics at
3124 * the same time open the pool, without having to keep around the spa_t in some
3128 spa_open_common(const char *pool
, spa_t
**spapp
, void *tag
, nvlist_t
*nvpolicy
,
3132 spa_load_state_t state
= SPA_LOAD_OPEN
;
3134 int locked
= B_FALSE
;
3135 int firstopen
= B_FALSE
;
3140 * As disgusting as this is, we need to support recursive calls to this
3141 * function because dsl_dir_open() is called during spa_load(), and ends
3142 * up calling spa_open() again. The real fix is to figure out how to
3143 * avoid dsl_dir_open() calling this in the first place.
3145 if (mutex_owner(&spa_namespace_lock
) != curthread
) {
3146 mutex_enter(&spa_namespace_lock
);
3150 if ((spa
= spa_lookup(pool
)) == NULL
) {
3152 mutex_exit(&spa_namespace_lock
);
3153 return (SET_ERROR(ENOENT
));
3156 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
) {
3157 zpool_rewind_policy_t policy
;
3161 zpool_get_rewind_policy(nvpolicy
? nvpolicy
: spa
->spa_config
,
3163 if (policy
.zrp_request
& ZPOOL_DO_REWIND
)
3164 state
= SPA_LOAD_RECOVER
;
3166 spa_activate(spa
, spa_mode_global
);
3168 if (state
!= SPA_LOAD_RECOVER
)
3169 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
3171 error
= spa_load_best(spa
, state
, B_FALSE
, policy
.zrp_txg
,
3172 policy
.zrp_request
);
3174 if (error
== EBADF
) {
3176 * If vdev_validate() returns failure (indicated by
3177 * EBADF), it indicates that one of the vdevs indicates
3178 * that the pool has been exported or destroyed. If
3179 * this is the case, the config cache is out of sync and
3180 * we should remove the pool from the namespace.
3183 spa_deactivate(spa
);
3184 spa_config_sync(spa
, B_TRUE
, B_TRUE
);
3187 mutex_exit(&spa_namespace_lock
);
3188 return (SET_ERROR(ENOENT
));
3193 * We can't open the pool, but we still have useful
3194 * information: the state of each vdev after the
3195 * attempted vdev_open(). Return this to the user.
3197 if (config
!= NULL
&& spa
->spa_config
) {
3198 VERIFY(nvlist_dup(spa
->spa_config
, config
,
3200 VERIFY(nvlist_add_nvlist(*config
,
3201 ZPOOL_CONFIG_LOAD_INFO
,
3202 spa
->spa_load_info
) == 0);
3205 spa_deactivate(spa
);
3206 spa
->spa_last_open_failed
= error
;
3208 mutex_exit(&spa_namespace_lock
);
3214 spa_open_ref(spa
, tag
);
3217 *config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
3220 * If we've recovered the pool, pass back any information we
3221 * gathered while doing the load.
3223 if (state
== SPA_LOAD_RECOVER
) {
3224 VERIFY(nvlist_add_nvlist(*config
, ZPOOL_CONFIG_LOAD_INFO
,
3225 spa
->spa_load_info
) == 0);
3229 spa
->spa_last_open_failed
= 0;
3230 spa
->spa_last_ubsync_txg
= 0;
3231 spa
->spa_load_txg
= 0;
3232 mutex_exit(&spa_namespace_lock
);
3236 zvol_create_minors(spa
, spa_name(spa
), B_TRUE
);
3244 spa_open_rewind(const char *name
, spa_t
**spapp
, void *tag
, nvlist_t
*policy
,
3247 return (spa_open_common(name
, spapp
, tag
, policy
, config
));
3251 spa_open(const char *name
, spa_t
**spapp
, void *tag
)
3253 return (spa_open_common(name
, spapp
, tag
, NULL
, NULL
));
3257 * Lookup the given spa_t, incrementing the inject count in the process,
3258 * preventing it from being exported or destroyed.
3261 spa_inject_addref(char *name
)
3265 mutex_enter(&spa_namespace_lock
);
3266 if ((spa
= spa_lookup(name
)) == NULL
) {
3267 mutex_exit(&spa_namespace_lock
);
3270 spa
->spa_inject_ref
++;
3271 mutex_exit(&spa_namespace_lock
);
3277 spa_inject_delref(spa_t
*spa
)
3279 mutex_enter(&spa_namespace_lock
);
3280 spa
->spa_inject_ref
--;
3281 mutex_exit(&spa_namespace_lock
);
3285 * Add spares device information to the nvlist.
3288 spa_add_spares(spa_t
*spa
, nvlist_t
*config
)
3298 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
3300 if (spa
->spa_spares
.sav_count
== 0)
3303 VERIFY(nvlist_lookup_nvlist(config
,
3304 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
3305 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
3306 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
3308 VERIFY(nvlist_add_nvlist_array(nvroot
,
3309 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
3310 VERIFY(nvlist_lookup_nvlist_array(nvroot
,
3311 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
3314 * Go through and find any spares which have since been
3315 * repurposed as an active spare. If this is the case, update
3316 * their status appropriately.
3318 for (i
= 0; i
< nspares
; i
++) {
3319 VERIFY(nvlist_lookup_uint64(spares
[i
],
3320 ZPOOL_CONFIG_GUID
, &guid
) == 0);
3321 if (spa_spare_exists(guid
, &pool
, NULL
) &&
3323 VERIFY(nvlist_lookup_uint64_array(
3324 spares
[i
], ZPOOL_CONFIG_VDEV_STATS
,
3325 (uint64_t **)&vs
, &vsc
) == 0);
3326 vs
->vs_state
= VDEV_STATE_CANT_OPEN
;
3327 vs
->vs_aux
= VDEV_AUX_SPARED
;
3334 * Add l2cache device information to the nvlist, including vdev stats.
3337 spa_add_l2cache(spa_t
*spa
, nvlist_t
*config
)
3340 uint_t i
, j
, nl2cache
;
3347 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
3349 if (spa
->spa_l2cache
.sav_count
== 0)
3352 VERIFY(nvlist_lookup_nvlist(config
,
3353 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
3354 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_l2cache
.sav_config
,
3355 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
3356 if (nl2cache
!= 0) {
3357 VERIFY(nvlist_add_nvlist_array(nvroot
,
3358 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
3359 VERIFY(nvlist_lookup_nvlist_array(nvroot
,
3360 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
3363 * Update level 2 cache device stats.
3366 for (i
= 0; i
< nl2cache
; i
++) {
3367 VERIFY(nvlist_lookup_uint64(l2cache
[i
],
3368 ZPOOL_CONFIG_GUID
, &guid
) == 0);
3371 for (j
= 0; j
< spa
->spa_l2cache
.sav_count
; j
++) {
3373 spa
->spa_l2cache
.sav_vdevs
[j
]->vdev_guid
) {
3374 vd
= spa
->spa_l2cache
.sav_vdevs
[j
];
3380 VERIFY(nvlist_lookup_uint64_array(l2cache
[i
],
3381 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
, &vsc
)
3383 vdev_get_stats(vd
, vs
);
3384 vdev_config_generate_stats(vd
, l2cache
[i
]);
3391 spa_feature_stats_from_disk(spa_t
*spa
, nvlist_t
*features
)
3396 if (spa
->spa_feat_for_read_obj
!= 0) {
3397 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
3398 spa
->spa_feat_for_read_obj
);
3399 zap_cursor_retrieve(&zc
, &za
) == 0;
3400 zap_cursor_advance(&zc
)) {
3401 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
3402 za
.za_num_integers
== 1);
3403 VERIFY0(nvlist_add_uint64(features
, za
.za_name
,
3404 za
.za_first_integer
));
3406 zap_cursor_fini(&zc
);
3409 if (spa
->spa_feat_for_write_obj
!= 0) {
3410 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
3411 spa
->spa_feat_for_write_obj
);
3412 zap_cursor_retrieve(&zc
, &za
) == 0;
3413 zap_cursor_advance(&zc
)) {
3414 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
3415 za
.za_num_integers
== 1);
3416 VERIFY0(nvlist_add_uint64(features
, za
.za_name
,
3417 za
.za_first_integer
));
3419 zap_cursor_fini(&zc
);
3424 spa_feature_stats_from_cache(spa_t
*spa
, nvlist_t
*features
)
3428 for (i
= 0; i
< SPA_FEATURES
; i
++) {
3429 zfeature_info_t feature
= spa_feature_table
[i
];
3432 if (feature_get_refcount(spa
, &feature
, &refcount
) != 0)
3435 VERIFY0(nvlist_add_uint64(features
, feature
.fi_guid
, refcount
));
3440 * Store a list of pool features and their reference counts in the
3443 * The first time this is called on a spa, allocate a new nvlist, fetch
3444 * the pool features and reference counts from disk, then save the list
3445 * in the spa. In subsequent calls on the same spa use the saved nvlist
3446 * and refresh its values from the cached reference counts. This
3447 * ensures we don't block here on I/O on a suspended pool so 'zpool
3448 * clear' can resume the pool.
3451 spa_add_feature_stats(spa_t
*spa
, nvlist_t
*config
)
3455 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
3457 mutex_enter(&spa
->spa_feat_stats_lock
);
3458 features
= spa
->spa_feat_stats
;
3460 if (features
!= NULL
) {
3461 spa_feature_stats_from_cache(spa
, features
);
3463 VERIFY0(nvlist_alloc(&features
, NV_UNIQUE_NAME
, KM_SLEEP
));
3464 spa
->spa_feat_stats
= features
;
3465 spa_feature_stats_from_disk(spa
, features
);
3468 VERIFY0(nvlist_add_nvlist(config
, ZPOOL_CONFIG_FEATURE_STATS
,
3471 mutex_exit(&spa
->spa_feat_stats_lock
);
3475 spa_get_stats(const char *name
, nvlist_t
**config
,
3476 char *altroot
, size_t buflen
)
3482 error
= spa_open_common(name
, &spa
, FTAG
, NULL
, config
);
3486 * This still leaves a window of inconsistency where the spares
3487 * or l2cache devices could change and the config would be
3488 * self-inconsistent.
3490 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
3492 if (*config
!= NULL
) {
3493 uint64_t loadtimes
[2];
3495 loadtimes
[0] = spa
->spa_loaded_ts
.tv_sec
;
3496 loadtimes
[1] = spa
->spa_loaded_ts
.tv_nsec
;
3497 VERIFY(nvlist_add_uint64_array(*config
,
3498 ZPOOL_CONFIG_LOADED_TIME
, loadtimes
, 2) == 0);
3500 VERIFY(nvlist_add_uint64(*config
,
3501 ZPOOL_CONFIG_ERRCOUNT
,
3502 spa_get_errlog_size(spa
)) == 0);
3504 if (spa_suspended(spa
))
3505 VERIFY(nvlist_add_uint64(*config
,
3506 ZPOOL_CONFIG_SUSPENDED
,
3507 spa
->spa_failmode
) == 0);
3509 spa_add_spares(spa
, *config
);
3510 spa_add_l2cache(spa
, *config
);
3511 spa_add_feature_stats(spa
, *config
);
3516 * We want to get the alternate root even for faulted pools, so we cheat
3517 * and call spa_lookup() directly.
3521 mutex_enter(&spa_namespace_lock
);
3522 spa
= spa_lookup(name
);
3524 spa_altroot(spa
, altroot
, buflen
);
3528 mutex_exit(&spa_namespace_lock
);
3530 spa_altroot(spa
, altroot
, buflen
);
3535 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
3536 spa_close(spa
, FTAG
);
3543 * Validate that the auxiliary device array is well formed. We must have an
3544 * array of nvlists, each which describes a valid leaf vdev. If this is an
3545 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
3546 * specified, as long as they are well-formed.
3549 spa_validate_aux_devs(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
,
3550 spa_aux_vdev_t
*sav
, const char *config
, uint64_t version
,
3551 vdev_labeltype_t label
)
3558 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
3561 * It's acceptable to have no devs specified.
3563 if (nvlist_lookup_nvlist_array(nvroot
, config
, &dev
, &ndev
) != 0)
3567 return (SET_ERROR(EINVAL
));
3570 * Make sure the pool is formatted with a version that supports this
3573 if (spa_version(spa
) < version
)
3574 return (SET_ERROR(ENOTSUP
));
3577 * Set the pending device list so we correctly handle device in-use
3580 sav
->sav_pending
= dev
;
3581 sav
->sav_npending
= ndev
;
3583 for (i
= 0; i
< ndev
; i
++) {
3584 if ((error
= spa_config_parse(spa
, &vd
, dev
[i
], NULL
, 0,
3588 if (!vd
->vdev_ops
->vdev_op_leaf
) {
3590 error
= SET_ERROR(EINVAL
);
3595 * The L2ARC currently only supports disk devices in
3596 * kernel context. For user-level testing, we allow it.
3599 if ((strcmp(config
, ZPOOL_CONFIG_L2CACHE
) == 0) &&
3600 strcmp(vd
->vdev_ops
->vdev_op_type
, VDEV_TYPE_DISK
) != 0) {
3601 error
= SET_ERROR(ENOTBLK
);
3608 if ((error
= vdev_open(vd
)) == 0 &&
3609 (error
= vdev_label_init(vd
, crtxg
, label
)) == 0) {
3610 VERIFY(nvlist_add_uint64(dev
[i
], ZPOOL_CONFIG_GUID
,
3611 vd
->vdev_guid
) == 0);
3617 (mode
!= VDEV_ALLOC_SPARE
&& mode
!= VDEV_ALLOC_L2CACHE
))
3624 sav
->sav_pending
= NULL
;
3625 sav
->sav_npending
= 0;
3630 spa_validate_aux(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
)
3634 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
3636 if ((error
= spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
3637 &spa
->spa_spares
, ZPOOL_CONFIG_SPARES
, SPA_VERSION_SPARES
,
3638 VDEV_LABEL_SPARE
)) != 0) {
3642 return (spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
3643 &spa
->spa_l2cache
, ZPOOL_CONFIG_L2CACHE
, SPA_VERSION_L2CACHE
,
3644 VDEV_LABEL_L2CACHE
));
3648 spa_set_aux_vdevs(spa_aux_vdev_t
*sav
, nvlist_t
**devs
, int ndevs
,
3653 if (sav
->sav_config
!= NULL
) {
3659 * Generate new dev list by concatentating with the
3662 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
, config
,
3663 &olddevs
, &oldndevs
) == 0);
3665 newdevs
= kmem_alloc(sizeof (void *) *
3666 (ndevs
+ oldndevs
), KM_SLEEP
);
3667 for (i
= 0; i
< oldndevs
; i
++)
3668 VERIFY(nvlist_dup(olddevs
[i
], &newdevs
[i
],
3670 for (i
= 0; i
< ndevs
; i
++)
3671 VERIFY(nvlist_dup(devs
[i
], &newdevs
[i
+ oldndevs
],
3674 VERIFY(nvlist_remove(sav
->sav_config
, config
,
3675 DATA_TYPE_NVLIST_ARRAY
) == 0);
3677 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
,
3678 config
, newdevs
, ndevs
+ oldndevs
) == 0);
3679 for (i
= 0; i
< oldndevs
+ ndevs
; i
++)
3680 nvlist_free(newdevs
[i
]);
3681 kmem_free(newdevs
, (oldndevs
+ ndevs
) * sizeof (void *));
3684 * Generate a new dev list.
3686 VERIFY(nvlist_alloc(&sav
->sav_config
, NV_UNIQUE_NAME
,
3688 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
, config
,
3694 * Stop and drop level 2 ARC devices
3697 spa_l2cache_drop(spa_t
*spa
)
3701 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
3703 for (i
= 0; i
< sav
->sav_count
; i
++) {
3706 vd
= sav
->sav_vdevs
[i
];
3709 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
3710 pool
!= 0ULL && l2arc_vdev_present(vd
))
3711 l2arc_remove_vdev(vd
);
3719 spa_create(const char *pool
, nvlist_t
*nvroot
, nvlist_t
*props
,
3723 char *altroot
= NULL
;
3728 uint64_t txg
= TXG_INITIAL
;
3729 nvlist_t
**spares
, **l2cache
;
3730 uint_t nspares
, nl2cache
;
3731 uint64_t version
, obj
;
3732 boolean_t has_features
;
3738 if (nvlist_lookup_string(props
, "tname", &poolname
) != 0)
3739 poolname
= (char *)pool
;
3742 * If this pool already exists, return failure.
3744 mutex_enter(&spa_namespace_lock
);
3745 if (spa_lookup(poolname
) != NULL
) {
3746 mutex_exit(&spa_namespace_lock
);
3747 return (SET_ERROR(EEXIST
));
3751 * Allocate a new spa_t structure.
3753 nvl
= fnvlist_alloc();
3754 fnvlist_add_string(nvl
, ZPOOL_CONFIG_POOL_NAME
, pool
);
3755 (void) nvlist_lookup_string(props
,
3756 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
3757 spa
= spa_add(poolname
, nvl
, altroot
);
3759 spa_activate(spa
, spa_mode_global
);
3761 if (props
&& (error
= spa_prop_validate(spa
, props
))) {
3762 spa_deactivate(spa
);
3764 mutex_exit(&spa_namespace_lock
);
3769 * Temporary pool names should never be written to disk.
3771 if (poolname
!= pool
)
3772 spa
->spa_import_flags
|= ZFS_IMPORT_TEMP_NAME
;
3774 has_features
= B_FALSE
;
3775 for (elem
= nvlist_next_nvpair(props
, NULL
);
3776 elem
!= NULL
; elem
= nvlist_next_nvpair(props
, elem
)) {
3777 if (zpool_prop_feature(nvpair_name(elem
)))
3778 has_features
= B_TRUE
;
3781 if (has_features
|| nvlist_lookup_uint64(props
,
3782 zpool_prop_to_name(ZPOOL_PROP_VERSION
), &version
) != 0) {
3783 version
= SPA_VERSION
;
3785 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
3787 spa
->spa_first_txg
= txg
;
3788 spa
->spa_uberblock
.ub_txg
= txg
- 1;
3789 spa
->spa_uberblock
.ub_version
= version
;
3790 spa
->spa_ubsync
= spa
->spa_uberblock
;
3793 * Create "The Godfather" zio to hold all async IOs
3795 spa
->spa_async_zio_root
= kmem_alloc(max_ncpus
* sizeof (void *),
3797 for (i
= 0; i
< max_ncpus
; i
++) {
3798 spa
->spa_async_zio_root
[i
] = zio_root(spa
, NULL
, NULL
,
3799 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
3800 ZIO_FLAG_GODFATHER
);
3804 * Create the root vdev.
3806 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3808 error
= spa_config_parse(spa
, &rvd
, nvroot
, NULL
, 0, VDEV_ALLOC_ADD
);
3810 ASSERT(error
!= 0 || rvd
!= NULL
);
3811 ASSERT(error
!= 0 || spa
->spa_root_vdev
== rvd
);
3813 if (error
== 0 && !zfs_allocatable_devs(nvroot
))
3814 error
= SET_ERROR(EINVAL
);
3817 (error
= vdev_create(rvd
, txg
, B_FALSE
)) == 0 &&
3818 (error
= spa_validate_aux(spa
, nvroot
, txg
,
3819 VDEV_ALLOC_ADD
)) == 0) {
3820 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
3821 vdev_metaslab_set_size(rvd
->vdev_child
[c
]);
3822 vdev_expand(rvd
->vdev_child
[c
], txg
);
3826 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3830 spa_deactivate(spa
);
3832 mutex_exit(&spa_namespace_lock
);
3837 * Get the list of spares, if specified.
3839 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
3840 &spares
, &nspares
) == 0) {
3841 VERIFY(nvlist_alloc(&spa
->spa_spares
.sav_config
, NV_UNIQUE_NAME
,
3843 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
3844 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
3845 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3846 spa_load_spares(spa
);
3847 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3848 spa
->spa_spares
.sav_sync
= B_TRUE
;
3852 * Get the list of level 2 cache devices, if specified.
3854 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
3855 &l2cache
, &nl2cache
) == 0) {
3856 VERIFY(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
3857 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
3858 VERIFY(nvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
3859 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
3860 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3861 spa_load_l2cache(spa
);
3862 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3863 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
3866 spa
->spa_is_initializing
= B_TRUE
;
3867 spa
->spa_dsl_pool
= dp
= dsl_pool_create(spa
, zplprops
, txg
);
3868 spa
->spa_meta_objset
= dp
->dp_meta_objset
;
3869 spa
->spa_is_initializing
= B_FALSE
;
3872 * Create DDTs (dedup tables).
3876 spa_update_dspace(spa
);
3878 tx
= dmu_tx_create_assigned(dp
, txg
);
3881 * Create the pool config object.
3883 spa
->spa_config_object
= dmu_object_alloc(spa
->spa_meta_objset
,
3884 DMU_OT_PACKED_NVLIST
, SPA_CONFIG_BLOCKSIZE
,
3885 DMU_OT_PACKED_NVLIST_SIZE
, sizeof (uint64_t), tx
);
3887 if (zap_add(spa
->spa_meta_objset
,
3888 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CONFIG
,
3889 sizeof (uint64_t), 1, &spa
->spa_config_object
, tx
) != 0) {
3890 cmn_err(CE_PANIC
, "failed to add pool config");
3893 if (spa_version(spa
) >= SPA_VERSION_FEATURES
)
3894 spa_feature_create_zap_objects(spa
, tx
);
3896 if (zap_add(spa
->spa_meta_objset
,
3897 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CREATION_VERSION
,
3898 sizeof (uint64_t), 1, &version
, tx
) != 0) {
3899 cmn_err(CE_PANIC
, "failed to add pool version");
3902 /* Newly created pools with the right version are always deflated. */
3903 if (version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
3904 spa
->spa_deflate
= TRUE
;
3905 if (zap_add(spa
->spa_meta_objset
,
3906 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
3907 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
) != 0) {
3908 cmn_err(CE_PANIC
, "failed to add deflate");
3913 * Create the deferred-free bpobj. Turn off compression
3914 * because sync-to-convergence takes longer if the blocksize
3917 obj
= bpobj_alloc(spa
->spa_meta_objset
, 1 << 14, tx
);
3918 dmu_object_set_compress(spa
->spa_meta_objset
, obj
,
3919 ZIO_COMPRESS_OFF
, tx
);
3920 if (zap_add(spa
->spa_meta_objset
,
3921 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_SYNC_BPOBJ
,
3922 sizeof (uint64_t), 1, &obj
, tx
) != 0) {
3923 cmn_err(CE_PANIC
, "failed to add bpobj");
3925 VERIFY3U(0, ==, bpobj_open(&spa
->spa_deferred_bpobj
,
3926 spa
->spa_meta_objset
, obj
));
3929 * Create the pool's history object.
3931 if (version
>= SPA_VERSION_ZPOOL_HISTORY
)
3932 spa_history_create_obj(spa
, tx
);
3935 * Set pool properties.
3937 spa
->spa_bootfs
= zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS
);
3938 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
3939 spa
->spa_failmode
= zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE
);
3940 spa
->spa_autoexpand
= zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND
);
3942 if (props
!= NULL
) {
3943 spa_configfile_set(spa
, props
, B_FALSE
);
3944 spa_sync_props(props
, tx
);
3949 spa
->spa_sync_on
= B_TRUE
;
3950 txg_sync_start(spa
->spa_dsl_pool
);
3953 * We explicitly wait for the first transaction to complete so that our
3954 * bean counters are appropriately updated.
3956 txg_wait_synced(spa
->spa_dsl_pool
, txg
);
3958 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
3960 spa_history_log_version(spa
, "create");
3963 * Don't count references from objsets that are already closed
3964 * and are making their way through the eviction process.
3966 spa_evicting_os_wait(spa
);
3967 spa
->spa_minref
= refcount_count(&spa
->spa_refcount
);
3969 mutex_exit(&spa_namespace_lock
);
3976 * Get the root pool information from the root disk, then import the root pool
3977 * during the system boot up time.
3979 extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t
**);
3982 spa_generate_rootconf(char *devpath
, char *devid
, uint64_t *guid
)
3985 nvlist_t
*nvtop
, *nvroot
;
3988 if (vdev_disk_read_rootlabel(devpath
, devid
, &config
) != 0)
3992 * Add this top-level vdev to the child array.
3994 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
3996 VERIFY(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
3998 VERIFY(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_GUID
, guid
) == 0);
4001 * Put this pool's top-level vdevs into a root vdev.
4003 VERIFY(nvlist_alloc(&nvroot
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
4004 VERIFY(nvlist_add_string(nvroot
, ZPOOL_CONFIG_TYPE
,
4005 VDEV_TYPE_ROOT
) == 0);
4006 VERIFY(nvlist_add_uint64(nvroot
, ZPOOL_CONFIG_ID
, 0ULL) == 0);
4007 VERIFY(nvlist_add_uint64(nvroot
, ZPOOL_CONFIG_GUID
, pgid
) == 0);
4008 VERIFY(nvlist_add_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
4012 * Replace the existing vdev_tree with the new root vdev in
4013 * this pool's configuration (remove the old, add the new).
4015 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, nvroot
) == 0);
4016 nvlist_free(nvroot
);
4021 * Walk the vdev tree and see if we can find a device with "better"
4022 * configuration. A configuration is "better" if the label on that
4023 * device has a more recent txg.
4026 spa_alt_rootvdev(vdev_t
*vd
, vdev_t
**avd
, uint64_t *txg
)
4030 for (c
= 0; c
< vd
->vdev_children
; c
++)
4031 spa_alt_rootvdev(vd
->vdev_child
[c
], avd
, txg
);
4033 if (vd
->vdev_ops
->vdev_op_leaf
) {
4037 if (vdev_disk_read_rootlabel(vd
->vdev_physpath
, vd
->vdev_devid
,
4041 VERIFY(nvlist_lookup_uint64(label
, ZPOOL_CONFIG_POOL_TXG
,
4045 * Do we have a better boot device?
4047 if (label_txg
> *txg
) {
4056 * Import a root pool.
4058 * For x86. devpath_list will consist of devid and/or physpath name of
4059 * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
4060 * The GRUB "findroot" command will return the vdev we should boot.
4062 * For Sparc, devpath_list consists the physpath name of the booting device
4063 * no matter the rootpool is a single device pool or a mirrored pool.
4065 * "/pci@1f,0/ide@d/disk@0,0:a"
4068 spa_import_rootpool(char *devpath
, char *devid
)
4071 vdev_t
*rvd
, *bvd
, *avd
= NULL
;
4072 nvlist_t
*config
, *nvtop
;
4078 * Read the label from the boot device and generate a configuration.
4080 config
= spa_generate_rootconf(devpath
, devid
, &guid
);
4081 #if defined(_OBP) && defined(_KERNEL)
4082 if (config
== NULL
) {
4083 if (strstr(devpath
, "/iscsi/ssd") != NULL
) {
4085 get_iscsi_bootpath_phy(devpath
);
4086 config
= spa_generate_rootconf(devpath
, devid
, &guid
);
4090 if (config
== NULL
) {
4091 cmn_err(CE_NOTE
, "Cannot read the pool label from '%s'",
4093 return (SET_ERROR(EIO
));
4096 VERIFY(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
4098 VERIFY(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_TXG
, &txg
) == 0);
4100 mutex_enter(&spa_namespace_lock
);
4101 if ((spa
= spa_lookup(pname
)) != NULL
) {
4103 * Remove the existing root pool from the namespace so that we
4104 * can replace it with the correct config we just read in.
4109 spa
= spa_add(pname
, config
, NULL
);
4110 spa
->spa_is_root
= B_TRUE
;
4111 spa
->spa_import_flags
= ZFS_IMPORT_VERBATIM
;
4114 * Build up a vdev tree based on the boot device's label config.
4116 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
4118 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4119 error
= spa_config_parse(spa
, &rvd
, nvtop
, NULL
, 0,
4120 VDEV_ALLOC_ROOTPOOL
);
4121 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4123 mutex_exit(&spa_namespace_lock
);
4124 nvlist_free(config
);
4125 cmn_err(CE_NOTE
, "Can not parse the config for pool '%s'",
4131 * Get the boot vdev.
4133 if ((bvd
= vdev_lookup_by_guid(rvd
, guid
)) == NULL
) {
4134 cmn_err(CE_NOTE
, "Can not find the boot vdev for guid %llu",
4135 (u_longlong_t
)guid
);
4136 error
= SET_ERROR(ENOENT
);
4141 * Determine if there is a better boot device.
4144 spa_alt_rootvdev(rvd
, &avd
, &txg
);
4146 cmn_err(CE_NOTE
, "The boot device is 'degraded'. Please "
4147 "try booting from '%s'", avd
->vdev_path
);
4148 error
= SET_ERROR(EINVAL
);
4153 * If the boot device is part of a spare vdev then ensure that
4154 * we're booting off the active spare.
4156 if (bvd
->vdev_parent
->vdev_ops
== &vdev_spare_ops
&&
4157 !bvd
->vdev_isspare
) {
4158 cmn_err(CE_NOTE
, "The boot device is currently spared. Please "
4159 "try booting from '%s'",
4161 vdev_child
[bvd
->vdev_parent
->vdev_children
- 1]->vdev_path
);
4162 error
= SET_ERROR(EINVAL
);
4168 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4170 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4171 mutex_exit(&spa_namespace_lock
);
4173 nvlist_free(config
);
4180 * Import a non-root pool into the system.
4183 spa_import(char *pool
, nvlist_t
*config
, nvlist_t
*props
, uint64_t flags
)
4186 char *altroot
= NULL
;
4187 spa_load_state_t state
= SPA_LOAD_IMPORT
;
4188 zpool_rewind_policy_t policy
;
4189 uint64_t mode
= spa_mode_global
;
4190 uint64_t readonly
= B_FALSE
;
4193 nvlist_t
**spares
, **l2cache
;
4194 uint_t nspares
, nl2cache
;
4197 * If a pool with this name exists, return failure.
4199 mutex_enter(&spa_namespace_lock
);
4200 if (spa_lookup(pool
) != NULL
) {
4201 mutex_exit(&spa_namespace_lock
);
4202 return (SET_ERROR(EEXIST
));
4206 * Create and initialize the spa structure.
4208 (void) nvlist_lookup_string(props
,
4209 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
4210 (void) nvlist_lookup_uint64(props
,
4211 zpool_prop_to_name(ZPOOL_PROP_READONLY
), &readonly
);
4214 spa
= spa_add(pool
, config
, altroot
);
4215 spa
->spa_import_flags
= flags
;
4218 * Verbatim import - Take a pool and insert it into the namespace
4219 * as if it had been loaded at boot.
4221 if (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
) {
4223 spa_configfile_set(spa
, props
, B_FALSE
);
4225 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
4227 mutex_exit(&spa_namespace_lock
);
4231 spa_activate(spa
, mode
);
4234 * Don't start async tasks until we know everything is healthy.
4236 spa_async_suspend(spa
);
4238 zpool_get_rewind_policy(config
, &policy
);
4239 if (policy
.zrp_request
& ZPOOL_DO_REWIND
)
4240 state
= SPA_LOAD_RECOVER
;
4243 * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig
4244 * because the user-supplied config is actually the one to trust when
4247 if (state
!= SPA_LOAD_RECOVER
)
4248 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
4250 error
= spa_load_best(spa
, state
, B_TRUE
, policy
.zrp_txg
,
4251 policy
.zrp_request
);
4254 * Propagate anything learned while loading the pool and pass it
4255 * back to caller (i.e. rewind info, missing devices, etc).
4257 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
4258 spa
->spa_load_info
) == 0);
4260 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4262 * Toss any existing sparelist, as it doesn't have any validity
4263 * anymore, and conflicts with spa_has_spare().
4265 if (spa
->spa_spares
.sav_config
) {
4266 nvlist_free(spa
->spa_spares
.sav_config
);
4267 spa
->spa_spares
.sav_config
= NULL
;
4268 spa_load_spares(spa
);
4270 if (spa
->spa_l2cache
.sav_config
) {
4271 nvlist_free(spa
->spa_l2cache
.sav_config
);
4272 spa
->spa_l2cache
.sav_config
= NULL
;
4273 spa_load_l2cache(spa
);
4276 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
4279 error
= spa_validate_aux(spa
, nvroot
, -1ULL,
4282 error
= spa_validate_aux(spa
, nvroot
, -1ULL,
4283 VDEV_ALLOC_L2CACHE
);
4284 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4287 spa_configfile_set(spa
, props
, B_FALSE
);
4289 if (error
!= 0 || (props
&& spa_writeable(spa
) &&
4290 (error
= spa_prop_set(spa
, props
)))) {
4292 spa_deactivate(spa
);
4294 mutex_exit(&spa_namespace_lock
);
4298 spa_async_resume(spa
);
4301 * Override any spares and level 2 cache devices as specified by
4302 * the user, as these may have correct device names/devids, etc.
4304 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
4305 &spares
, &nspares
) == 0) {
4306 if (spa
->spa_spares
.sav_config
)
4307 VERIFY(nvlist_remove(spa
->spa_spares
.sav_config
,
4308 ZPOOL_CONFIG_SPARES
, DATA_TYPE_NVLIST_ARRAY
) == 0);
4310 VERIFY(nvlist_alloc(&spa
->spa_spares
.sav_config
,
4311 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
4312 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
4313 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
4314 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4315 spa_load_spares(spa
);
4316 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4317 spa
->spa_spares
.sav_sync
= B_TRUE
;
4319 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
4320 &l2cache
, &nl2cache
) == 0) {
4321 if (spa
->spa_l2cache
.sav_config
)
4322 VERIFY(nvlist_remove(spa
->spa_l2cache
.sav_config
,
4323 ZPOOL_CONFIG_L2CACHE
, DATA_TYPE_NVLIST_ARRAY
) == 0);
4325 VERIFY(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
4326 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
4327 VERIFY(nvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
4328 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
4329 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4330 spa_load_l2cache(spa
);
4331 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4332 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
4336 * Check for any removed devices.
4338 if (spa
->spa_autoreplace
) {
4339 spa_aux_check_removed(&spa
->spa_spares
);
4340 spa_aux_check_removed(&spa
->spa_l2cache
);
4343 if (spa_writeable(spa
)) {
4345 * Update the config cache to include the newly-imported pool.
4347 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
4351 * It's possible that the pool was expanded while it was exported.
4352 * We kick off an async task to handle this for us.
4354 spa_async_request(spa
, SPA_ASYNC_AUTOEXPAND
);
4356 mutex_exit(&spa_namespace_lock
);
4357 spa_history_log_version(spa
, "import");
4358 zvol_create_minors(spa
, pool
, B_TRUE
);
4364 spa_tryimport(nvlist_t
*tryconfig
)
4366 nvlist_t
*config
= NULL
;
4372 if (nvlist_lookup_string(tryconfig
, ZPOOL_CONFIG_POOL_NAME
, &poolname
))
4375 if (nvlist_lookup_uint64(tryconfig
, ZPOOL_CONFIG_POOL_STATE
, &state
))
4379 * Create and initialize the spa structure.
4381 mutex_enter(&spa_namespace_lock
);
4382 spa
= spa_add(TRYIMPORT_NAME
, tryconfig
, NULL
);
4383 spa_activate(spa
, FREAD
);
4386 * Pass off the heavy lifting to spa_load().
4387 * Pass TRUE for mosconfig because the user-supplied config
4388 * is actually the one to trust when doing an import.
4390 error
= spa_load(spa
, SPA_LOAD_TRYIMPORT
, SPA_IMPORT_EXISTING
, B_TRUE
);
4393 * If 'tryconfig' was at least parsable, return the current config.
4395 if (spa
->spa_root_vdev
!= NULL
) {
4396 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
4397 VERIFY(nvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
,
4399 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
4401 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_TIMESTAMP
,
4402 spa
->spa_uberblock
.ub_timestamp
) == 0);
4403 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
4404 spa
->spa_load_info
) == 0);
4405 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_ERRATA
,
4406 spa
->spa_errata
) == 0);
4409 * If the bootfs property exists on this pool then we
4410 * copy it out so that external consumers can tell which
4411 * pools are bootable.
4413 if ((!error
|| error
== EEXIST
) && spa
->spa_bootfs
) {
4414 char *tmpname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
4417 * We have to play games with the name since the
4418 * pool was opened as TRYIMPORT_NAME.
4420 if (dsl_dsobj_to_dsname(spa_name(spa
),
4421 spa
->spa_bootfs
, tmpname
) == 0) {
4425 dsname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
4427 cp
= strchr(tmpname
, '/');
4429 (void) strlcpy(dsname
, tmpname
,
4432 (void) snprintf(dsname
, MAXPATHLEN
,
4433 "%s/%s", poolname
, ++cp
);
4435 VERIFY(nvlist_add_string(config
,
4436 ZPOOL_CONFIG_BOOTFS
, dsname
) == 0);
4437 kmem_free(dsname
, MAXPATHLEN
);
4439 kmem_free(tmpname
, MAXPATHLEN
);
4443 * Add the list of hot spares and level 2 cache devices.
4445 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
4446 spa_add_spares(spa
, config
);
4447 spa_add_l2cache(spa
, config
);
4448 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
4452 spa_deactivate(spa
);
4454 mutex_exit(&spa_namespace_lock
);
4460 * Pool export/destroy
4462 * The act of destroying or exporting a pool is very simple. We make sure there
4463 * is no more pending I/O and any references to the pool are gone. Then, we
4464 * update the pool state and sync all the labels to disk, removing the
4465 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
4466 * we don't sync the labels or remove the configuration cache.
4469 spa_export_common(char *pool
, int new_state
, nvlist_t
**oldconfig
,
4470 boolean_t force
, boolean_t hardforce
)
4477 if (!(spa_mode_global
& FWRITE
))
4478 return (SET_ERROR(EROFS
));
4480 mutex_enter(&spa_namespace_lock
);
4481 if ((spa
= spa_lookup(pool
)) == NULL
) {
4482 mutex_exit(&spa_namespace_lock
);
4483 return (SET_ERROR(ENOENT
));
4487 * Put a hold on the pool, drop the namespace lock, stop async tasks,
4488 * reacquire the namespace lock, and see if we can export.
4490 spa_open_ref(spa
, FTAG
);
4491 mutex_exit(&spa_namespace_lock
);
4492 spa_async_suspend(spa
);
4493 if (spa
->spa_zvol_taskq
) {
4494 zvol_remove_minors(spa
, spa_name(spa
), B_TRUE
);
4495 taskq_wait(spa
->spa_zvol_taskq
);
4497 mutex_enter(&spa_namespace_lock
);
4498 spa_close(spa
, FTAG
);
4500 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
)
4503 * The pool will be in core if it's openable, in which case we can
4504 * modify its state. Objsets may be open only because they're dirty,
4505 * so we have to force it to sync before checking spa_refcnt.
4507 if (spa
->spa_sync_on
) {
4508 txg_wait_synced(spa
->spa_dsl_pool
, 0);
4509 spa_evicting_os_wait(spa
);
4513 * A pool cannot be exported or destroyed if there are active
4514 * references. If we are resetting a pool, allow references by
4515 * fault injection handlers.
4517 if (!spa_refcount_zero(spa
) ||
4518 (spa
->spa_inject_ref
!= 0 &&
4519 new_state
!= POOL_STATE_UNINITIALIZED
)) {
4520 spa_async_resume(spa
);
4521 mutex_exit(&spa_namespace_lock
);
4522 return (SET_ERROR(EBUSY
));
4525 if (spa
->spa_sync_on
) {
4527 * A pool cannot be exported if it has an active shared spare.
4528 * This is to prevent other pools stealing the active spare
4529 * from an exported pool. At user's own will, such pool can
4530 * be forcedly exported.
4532 if (!force
&& new_state
== POOL_STATE_EXPORTED
&&
4533 spa_has_active_shared_spare(spa
)) {
4534 spa_async_resume(spa
);
4535 mutex_exit(&spa_namespace_lock
);
4536 return (SET_ERROR(EXDEV
));
4540 * We want this to be reflected on every label,
4541 * so mark them all dirty. spa_unload() will do the
4542 * final sync that pushes these changes out.
4544 if (new_state
!= POOL_STATE_UNINITIALIZED
&& !hardforce
) {
4545 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4546 spa
->spa_state
= new_state
;
4547 spa
->spa_final_txg
= spa_last_synced_txg(spa
) +
4549 vdev_config_dirty(spa
->spa_root_vdev
);
4550 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4555 spa_event_notify(spa
, NULL
, FM_EREPORT_ZFS_POOL_DESTROY
);
4557 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
4559 spa_deactivate(spa
);
4562 if (oldconfig
&& spa
->spa_config
)
4563 VERIFY(nvlist_dup(spa
->spa_config
, oldconfig
, 0) == 0);
4565 if (new_state
!= POOL_STATE_UNINITIALIZED
) {
4567 spa_config_sync(spa
, B_TRUE
, B_TRUE
);
4570 mutex_exit(&spa_namespace_lock
);
4576 * Destroy a storage pool.
4579 spa_destroy(char *pool
)
4581 return (spa_export_common(pool
, POOL_STATE_DESTROYED
, NULL
,
4586 * Export a storage pool.
4589 spa_export(char *pool
, nvlist_t
**oldconfig
, boolean_t force
,
4590 boolean_t hardforce
)
4592 return (spa_export_common(pool
, POOL_STATE_EXPORTED
, oldconfig
,
4597 * Similar to spa_export(), this unloads the spa_t without actually removing it
4598 * from the namespace in any way.
4601 spa_reset(char *pool
)
4603 return (spa_export_common(pool
, POOL_STATE_UNINITIALIZED
, NULL
,
4608 * ==========================================================================
4609 * Device manipulation
4610 * ==========================================================================
4614 * Add a device to a storage pool.
4617 spa_vdev_add(spa_t
*spa
, nvlist_t
*nvroot
)
4621 vdev_t
*rvd
= spa
->spa_root_vdev
;
4623 nvlist_t
**spares
, **l2cache
;
4624 uint_t nspares
, nl2cache
;
4627 ASSERT(spa_writeable(spa
));
4629 txg
= spa_vdev_enter(spa
);
4631 if ((error
= spa_config_parse(spa
, &vd
, nvroot
, NULL
, 0,
4632 VDEV_ALLOC_ADD
)) != 0)
4633 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
4635 spa
->spa_pending_vdev
= vd
; /* spa_vdev_exit() will clear this */
4637 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
, &spares
,
4641 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
, &l2cache
,
4645 if (vd
->vdev_children
== 0 && nspares
== 0 && nl2cache
== 0)
4646 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
4648 if (vd
->vdev_children
!= 0 &&
4649 (error
= vdev_create(vd
, txg
, B_FALSE
)) != 0)
4650 return (spa_vdev_exit(spa
, vd
, txg
, error
));
4653 * We must validate the spares and l2cache devices after checking the
4654 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
4656 if ((error
= spa_validate_aux(spa
, nvroot
, txg
, VDEV_ALLOC_ADD
)) != 0)
4657 return (spa_vdev_exit(spa
, vd
, txg
, error
));
4660 * Transfer each new top-level vdev from vd to rvd.
4662 for (c
= 0; c
< vd
->vdev_children
; c
++) {
4665 * Set the vdev id to the first hole, if one exists.
4667 for (id
= 0; id
< rvd
->vdev_children
; id
++) {
4668 if (rvd
->vdev_child
[id
]->vdev_ishole
) {
4669 vdev_free(rvd
->vdev_child
[id
]);
4673 tvd
= vd
->vdev_child
[c
];
4674 vdev_remove_child(vd
, tvd
);
4676 vdev_add_child(rvd
, tvd
);
4677 vdev_config_dirty(tvd
);
4681 spa_set_aux_vdevs(&spa
->spa_spares
, spares
, nspares
,
4682 ZPOOL_CONFIG_SPARES
);
4683 spa_load_spares(spa
);
4684 spa
->spa_spares
.sav_sync
= B_TRUE
;
4687 if (nl2cache
!= 0) {
4688 spa_set_aux_vdevs(&spa
->spa_l2cache
, l2cache
, nl2cache
,
4689 ZPOOL_CONFIG_L2CACHE
);
4690 spa_load_l2cache(spa
);
4691 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
4695 * We have to be careful when adding new vdevs to an existing pool.
4696 * If other threads start allocating from these vdevs before we
4697 * sync the config cache, and we lose power, then upon reboot we may
4698 * fail to open the pool because there are DVAs that the config cache
4699 * can't translate. Therefore, we first add the vdevs without
4700 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
4701 * and then let spa_config_update() initialize the new metaslabs.
4703 * spa_load() checks for added-but-not-initialized vdevs, so that
4704 * if we lose power at any point in this sequence, the remaining
4705 * steps will be completed the next time we load the pool.
4707 (void) spa_vdev_exit(spa
, vd
, txg
, 0);
4709 mutex_enter(&spa_namespace_lock
);
4710 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
4711 mutex_exit(&spa_namespace_lock
);
4717 * Attach a device to a mirror. The arguments are the path to any device
4718 * in the mirror, and the nvroot for the new device. If the path specifies
4719 * a device that is not mirrored, we automatically insert the mirror vdev.
4721 * If 'replacing' is specified, the new device is intended to replace the
4722 * existing device; in this case the two devices are made into their own
4723 * mirror using the 'replacing' vdev, which is functionally identical to
4724 * the mirror vdev (it actually reuses all the same ops) but has a few
4725 * extra rules: you can't attach to it after it's been created, and upon
4726 * completion of resilvering, the first disk (the one being replaced)
4727 * is automatically detached.
4730 spa_vdev_attach(spa_t
*spa
, uint64_t guid
, nvlist_t
*nvroot
, int replacing
)
4732 uint64_t txg
, dtl_max_txg
;
4733 vdev_t
*oldvd
, *newvd
, *newrootvd
, *pvd
, *tvd
;
4735 char *oldvdpath
, *newvdpath
;
4738 ASSERTV(vdev_t
*rvd
= spa
->spa_root_vdev
);
4740 ASSERT(spa_writeable(spa
));
4742 txg
= spa_vdev_enter(spa
);
4744 oldvd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
4747 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
4749 if (!oldvd
->vdev_ops
->vdev_op_leaf
)
4750 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4752 pvd
= oldvd
->vdev_parent
;
4754 if ((error
= spa_config_parse(spa
, &newrootvd
, nvroot
, NULL
, 0,
4755 VDEV_ALLOC_ATTACH
)) != 0)
4756 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
4758 if (newrootvd
->vdev_children
!= 1)
4759 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
4761 newvd
= newrootvd
->vdev_child
[0];
4763 if (!newvd
->vdev_ops
->vdev_op_leaf
)
4764 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
4766 if ((error
= vdev_create(newrootvd
, txg
, replacing
)) != 0)
4767 return (spa_vdev_exit(spa
, newrootvd
, txg
, error
));
4770 * Spares can't replace logs
4772 if (oldvd
->vdev_top
->vdev_islog
&& newvd
->vdev_isspare
)
4773 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4777 * For attach, the only allowable parent is a mirror or the root
4780 if (pvd
->vdev_ops
!= &vdev_mirror_ops
&&
4781 pvd
->vdev_ops
!= &vdev_root_ops
)
4782 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4784 pvops
= &vdev_mirror_ops
;
4787 * Active hot spares can only be replaced by inactive hot
4790 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
4791 oldvd
->vdev_isspare
&&
4792 !spa_has_spare(spa
, newvd
->vdev_guid
))
4793 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4796 * If the source is a hot spare, and the parent isn't already a
4797 * spare, then we want to create a new hot spare. Otherwise, we
4798 * want to create a replacing vdev. The user is not allowed to
4799 * attach to a spared vdev child unless the 'isspare' state is
4800 * the same (spare replaces spare, non-spare replaces
4803 if (pvd
->vdev_ops
== &vdev_replacing_ops
&&
4804 spa_version(spa
) < SPA_VERSION_MULTI_REPLACE
) {
4805 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4806 } else if (pvd
->vdev_ops
== &vdev_spare_ops
&&
4807 newvd
->vdev_isspare
!= oldvd
->vdev_isspare
) {
4808 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4811 if (newvd
->vdev_isspare
)
4812 pvops
= &vdev_spare_ops
;
4814 pvops
= &vdev_replacing_ops
;
4818 * Make sure the new device is big enough.
4820 if (newvd
->vdev_asize
< vdev_get_min_asize(oldvd
))
4821 return (spa_vdev_exit(spa
, newrootvd
, txg
, EOVERFLOW
));
4824 * The new device cannot have a higher alignment requirement
4825 * than the top-level vdev.
4827 if (newvd
->vdev_ashift
> oldvd
->vdev_top
->vdev_ashift
)
4828 return (spa_vdev_exit(spa
, newrootvd
, txg
, EDOM
));
4831 * If this is an in-place replacement, update oldvd's path and devid
4832 * to make it distinguishable from newvd, and unopenable from now on.
4834 if (strcmp(oldvd
->vdev_path
, newvd
->vdev_path
) == 0) {
4835 spa_strfree(oldvd
->vdev_path
);
4836 oldvd
->vdev_path
= kmem_alloc(strlen(newvd
->vdev_path
) + 5,
4838 (void) sprintf(oldvd
->vdev_path
, "%s/%s",
4839 newvd
->vdev_path
, "old");
4840 if (oldvd
->vdev_devid
!= NULL
) {
4841 spa_strfree(oldvd
->vdev_devid
);
4842 oldvd
->vdev_devid
= NULL
;
4846 /* mark the device being resilvered */
4847 newvd
->vdev_resilver_txg
= txg
;
4850 * If the parent is not a mirror, or if we're replacing, insert the new
4851 * mirror/replacing/spare vdev above oldvd.
4853 if (pvd
->vdev_ops
!= pvops
)
4854 pvd
= vdev_add_parent(oldvd
, pvops
);
4856 ASSERT(pvd
->vdev_top
->vdev_parent
== rvd
);
4857 ASSERT(pvd
->vdev_ops
== pvops
);
4858 ASSERT(oldvd
->vdev_parent
== pvd
);
4861 * Extract the new device from its root and add it to pvd.
4863 vdev_remove_child(newrootvd
, newvd
);
4864 newvd
->vdev_id
= pvd
->vdev_children
;
4865 newvd
->vdev_crtxg
= oldvd
->vdev_crtxg
;
4866 vdev_add_child(pvd
, newvd
);
4868 tvd
= newvd
->vdev_top
;
4869 ASSERT(pvd
->vdev_top
== tvd
);
4870 ASSERT(tvd
->vdev_parent
== rvd
);
4872 vdev_config_dirty(tvd
);
4875 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
4876 * for any dmu_sync-ed blocks. It will propagate upward when
4877 * spa_vdev_exit() calls vdev_dtl_reassess().
4879 dtl_max_txg
= txg
+ TXG_CONCURRENT_STATES
;
4881 vdev_dtl_dirty(newvd
, DTL_MISSING
, TXG_INITIAL
,
4882 dtl_max_txg
- TXG_INITIAL
);
4884 if (newvd
->vdev_isspare
) {
4885 spa_spare_activate(newvd
);
4886 spa_event_notify(spa
, newvd
, FM_EREPORT_ZFS_DEVICE_SPARE
);
4889 oldvdpath
= spa_strdup(oldvd
->vdev_path
);
4890 newvdpath
= spa_strdup(newvd
->vdev_path
);
4891 newvd_isspare
= newvd
->vdev_isspare
;
4894 * Mark newvd's DTL dirty in this txg.
4896 vdev_dirty(tvd
, VDD_DTL
, newvd
, txg
);
4899 * Schedule the resilver to restart in the future. We do this to
4900 * ensure that dmu_sync-ed blocks have been stitched into the
4901 * respective datasets.
4903 dsl_resilver_restart(spa
->spa_dsl_pool
, dtl_max_txg
);
4908 (void) spa_vdev_exit(spa
, newrootvd
, dtl_max_txg
, 0);
4910 spa_history_log_internal(spa
, "vdev attach", NULL
,
4911 "%s vdev=%s %s vdev=%s",
4912 replacing
&& newvd_isspare
? "spare in" :
4913 replacing
? "replace" : "attach", newvdpath
,
4914 replacing
? "for" : "to", oldvdpath
);
4916 spa_strfree(oldvdpath
);
4917 spa_strfree(newvdpath
);
4919 if (spa
->spa_bootfs
)
4920 spa_event_notify(spa
, newvd
, FM_EREPORT_ZFS_BOOTFS_VDEV_ATTACH
);
4926 * Detach a device from a mirror or replacing vdev.
4928 * If 'replace_done' is specified, only detach if the parent
4929 * is a replacing vdev.
4932 spa_vdev_detach(spa_t
*spa
, uint64_t guid
, uint64_t pguid
, int replace_done
)
4936 vdev_t
*vd
, *pvd
, *cvd
, *tvd
;
4937 boolean_t unspare
= B_FALSE
;
4938 uint64_t unspare_guid
= 0;
4941 ASSERTV(vdev_t
*rvd
= spa
->spa_root_vdev
);
4942 ASSERT(spa_writeable(spa
));
4944 txg
= spa_vdev_enter(spa
);
4946 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
4949 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
4951 if (!vd
->vdev_ops
->vdev_op_leaf
)
4952 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4954 pvd
= vd
->vdev_parent
;
4957 * If the parent/child relationship is not as expected, don't do it.
4958 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
4959 * vdev that's replacing B with C. The user's intent in replacing
4960 * is to go from M(A,B) to M(A,C). If the user decides to cancel
4961 * the replace by detaching C, the expected behavior is to end up
4962 * M(A,B). But suppose that right after deciding to detach C,
4963 * the replacement of B completes. We would have M(A,C), and then
4964 * ask to detach C, which would leave us with just A -- not what
4965 * the user wanted. To prevent this, we make sure that the
4966 * parent/child relationship hasn't changed -- in this example,
4967 * that C's parent is still the replacing vdev R.
4969 if (pvd
->vdev_guid
!= pguid
&& pguid
!= 0)
4970 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
4973 * Only 'replacing' or 'spare' vdevs can be replaced.
4975 if (replace_done
&& pvd
->vdev_ops
!= &vdev_replacing_ops
&&
4976 pvd
->vdev_ops
!= &vdev_spare_ops
)
4977 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4979 ASSERT(pvd
->vdev_ops
!= &vdev_spare_ops
||
4980 spa_version(spa
) >= SPA_VERSION_SPARES
);
4983 * Only mirror, replacing, and spare vdevs support detach.
4985 if (pvd
->vdev_ops
!= &vdev_replacing_ops
&&
4986 pvd
->vdev_ops
!= &vdev_mirror_ops
&&
4987 pvd
->vdev_ops
!= &vdev_spare_ops
)
4988 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4991 * If this device has the only valid copy of some data,
4992 * we cannot safely detach it.
4994 if (vdev_dtl_required(vd
))
4995 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
4997 ASSERT(pvd
->vdev_children
>= 2);
5000 * If we are detaching the second disk from a replacing vdev, then
5001 * check to see if we changed the original vdev's path to have "/old"
5002 * at the end in spa_vdev_attach(). If so, undo that change now.
5004 if (pvd
->vdev_ops
== &vdev_replacing_ops
&& vd
->vdev_id
> 0 &&
5005 vd
->vdev_path
!= NULL
) {
5006 size_t len
= strlen(vd
->vdev_path
);
5008 for (c
= 0; c
< pvd
->vdev_children
; c
++) {
5009 cvd
= pvd
->vdev_child
[c
];
5011 if (cvd
== vd
|| cvd
->vdev_path
== NULL
)
5014 if (strncmp(cvd
->vdev_path
, vd
->vdev_path
, len
) == 0 &&
5015 strcmp(cvd
->vdev_path
+ len
, "/old") == 0) {
5016 spa_strfree(cvd
->vdev_path
);
5017 cvd
->vdev_path
= spa_strdup(vd
->vdev_path
);
5024 * If we are detaching the original disk from a spare, then it implies
5025 * that the spare should become a real disk, and be removed from the
5026 * active spare list for the pool.
5028 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
5030 pvd
->vdev_child
[pvd
->vdev_children
- 1]->vdev_isspare
)
5034 * Erase the disk labels so the disk can be used for other things.
5035 * This must be done after all other error cases are handled,
5036 * but before we disembowel vd (so we can still do I/O to it).
5037 * But if we can't do it, don't treat the error as fatal --
5038 * it may be that the unwritability of the disk is the reason
5039 * it's being detached!
5041 error
= vdev_label_init(vd
, 0, VDEV_LABEL_REMOVE
);
5044 * Remove vd from its parent and compact the parent's children.
5046 vdev_remove_child(pvd
, vd
);
5047 vdev_compact_children(pvd
);
5050 * Remember one of the remaining children so we can get tvd below.
5052 cvd
= pvd
->vdev_child
[pvd
->vdev_children
- 1];
5055 * If we need to remove the remaining child from the list of hot spares,
5056 * do it now, marking the vdev as no longer a spare in the process.
5057 * We must do this before vdev_remove_parent(), because that can
5058 * change the GUID if it creates a new toplevel GUID. For a similar
5059 * reason, we must remove the spare now, in the same txg as the detach;
5060 * otherwise someone could attach a new sibling, change the GUID, and
5061 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
5064 ASSERT(cvd
->vdev_isspare
);
5065 spa_spare_remove(cvd
);
5066 unspare_guid
= cvd
->vdev_guid
;
5067 (void) spa_vdev_remove(spa
, unspare_guid
, B_TRUE
);
5068 cvd
->vdev_unspare
= B_TRUE
;
5072 * If the parent mirror/replacing vdev only has one child,
5073 * the parent is no longer needed. Remove it from the tree.
5075 if (pvd
->vdev_children
== 1) {
5076 if (pvd
->vdev_ops
== &vdev_spare_ops
)
5077 cvd
->vdev_unspare
= B_FALSE
;
5078 vdev_remove_parent(cvd
);
5083 * We don't set tvd until now because the parent we just removed
5084 * may have been the previous top-level vdev.
5086 tvd
= cvd
->vdev_top
;
5087 ASSERT(tvd
->vdev_parent
== rvd
);
5090 * Reevaluate the parent vdev state.
5092 vdev_propagate_state(cvd
);
5095 * If the 'autoexpand' property is set on the pool then automatically
5096 * try to expand the size of the pool. For example if the device we
5097 * just detached was smaller than the others, it may be possible to
5098 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
5099 * first so that we can obtain the updated sizes of the leaf vdevs.
5101 if (spa
->spa_autoexpand
) {
5103 vdev_expand(tvd
, txg
);
5106 vdev_config_dirty(tvd
);
5109 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
5110 * vd->vdev_detached is set and free vd's DTL object in syncing context.
5111 * But first make sure we're not on any *other* txg's DTL list, to
5112 * prevent vd from being accessed after it's freed.
5114 vdpath
= spa_strdup(vd
->vdev_path
);
5115 for (t
= 0; t
< TXG_SIZE
; t
++)
5116 (void) txg_list_remove_this(&tvd
->vdev_dtl_list
, vd
, t
);
5117 vd
->vdev_detached
= B_TRUE
;
5118 vdev_dirty(tvd
, VDD_DTL
, vd
, txg
);
5120 spa_event_notify(spa
, vd
, FM_EREPORT_ZFS_DEVICE_REMOVE
);
5122 /* hang on to the spa before we release the lock */
5123 spa_open_ref(spa
, FTAG
);
5125 error
= spa_vdev_exit(spa
, vd
, txg
, 0);
5127 spa_history_log_internal(spa
, "detach", NULL
,
5129 spa_strfree(vdpath
);
5132 * If this was the removal of the original device in a hot spare vdev,
5133 * then we want to go through and remove the device from the hot spare
5134 * list of every other pool.
5137 spa_t
*altspa
= NULL
;
5139 mutex_enter(&spa_namespace_lock
);
5140 while ((altspa
= spa_next(altspa
)) != NULL
) {
5141 if (altspa
->spa_state
!= POOL_STATE_ACTIVE
||
5145 spa_open_ref(altspa
, FTAG
);
5146 mutex_exit(&spa_namespace_lock
);
5147 (void) spa_vdev_remove(altspa
, unspare_guid
, B_TRUE
);
5148 mutex_enter(&spa_namespace_lock
);
5149 spa_close(altspa
, FTAG
);
5151 mutex_exit(&spa_namespace_lock
);
5153 /* search the rest of the vdevs for spares to remove */
5154 spa_vdev_resilver_done(spa
);
5157 /* all done with the spa; OK to release */
5158 mutex_enter(&spa_namespace_lock
);
5159 spa_close(spa
, FTAG
);
5160 mutex_exit(&spa_namespace_lock
);
5166 * Split a set of devices from their mirrors, and create a new pool from them.
5169 spa_vdev_split_mirror(spa_t
*spa
, char *newname
, nvlist_t
*config
,
5170 nvlist_t
*props
, boolean_t exp
)
5173 uint64_t txg
, *glist
;
5175 uint_t c
, children
, lastlog
;
5176 nvlist_t
**child
, *nvl
, *tmp
;
5178 char *altroot
= NULL
;
5179 vdev_t
*rvd
, **vml
= NULL
; /* vdev modify list */
5180 boolean_t activate_slog
;
5182 ASSERT(spa_writeable(spa
));
5184 txg
= spa_vdev_enter(spa
);
5186 /* clear the log and flush everything up to now */
5187 activate_slog
= spa_passivate_log(spa
);
5188 (void) spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
5189 error
= spa_offline_log(spa
);
5190 txg
= spa_vdev_config_enter(spa
);
5193 spa_activate_log(spa
);
5196 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5198 /* check new spa name before going any further */
5199 if (spa_lookup(newname
) != NULL
)
5200 return (spa_vdev_exit(spa
, NULL
, txg
, EEXIST
));
5203 * scan through all the children to ensure they're all mirrors
5205 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvl
) != 0 ||
5206 nvlist_lookup_nvlist_array(nvl
, ZPOOL_CONFIG_CHILDREN
, &child
,
5208 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
5210 /* first, check to ensure we've got the right child count */
5211 rvd
= spa
->spa_root_vdev
;
5213 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
5214 vdev_t
*vd
= rvd
->vdev_child
[c
];
5216 /* don't count the holes & logs as children */
5217 if (vd
->vdev_islog
|| vd
->vdev_ishole
) {
5225 if (children
!= (lastlog
!= 0 ? lastlog
: rvd
->vdev_children
))
5226 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
5228 /* next, ensure no spare or cache devices are part of the split */
5229 if (nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_SPARES
, &tmp
) == 0 ||
5230 nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_L2CACHE
, &tmp
) == 0)
5231 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
5233 vml
= kmem_zalloc(children
* sizeof (vdev_t
*), KM_SLEEP
);
5234 glist
= kmem_zalloc(children
* sizeof (uint64_t), KM_SLEEP
);
5236 /* then, loop over each vdev and validate it */
5237 for (c
= 0; c
< children
; c
++) {
5238 uint64_t is_hole
= 0;
5240 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
5244 if (spa
->spa_root_vdev
->vdev_child
[c
]->vdev_ishole
||
5245 spa
->spa_root_vdev
->vdev_child
[c
]->vdev_islog
) {
5248 error
= SET_ERROR(EINVAL
);
5253 /* which disk is going to be split? */
5254 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_GUID
,
5256 error
= SET_ERROR(EINVAL
);
5260 /* look it up in the spa */
5261 vml
[c
] = spa_lookup_by_guid(spa
, glist
[c
], B_FALSE
);
5262 if (vml
[c
] == NULL
) {
5263 error
= SET_ERROR(ENODEV
);
5267 /* make sure there's nothing stopping the split */
5268 if (vml
[c
]->vdev_parent
->vdev_ops
!= &vdev_mirror_ops
||
5269 vml
[c
]->vdev_islog
||
5270 vml
[c
]->vdev_ishole
||
5271 vml
[c
]->vdev_isspare
||
5272 vml
[c
]->vdev_isl2cache
||
5273 !vdev_writeable(vml
[c
]) ||
5274 vml
[c
]->vdev_children
!= 0 ||
5275 vml
[c
]->vdev_state
!= VDEV_STATE_HEALTHY
||
5276 c
!= spa
->spa_root_vdev
->vdev_child
[c
]->vdev_id
) {
5277 error
= SET_ERROR(EINVAL
);
5281 if (vdev_dtl_required(vml
[c
])) {
5282 error
= SET_ERROR(EBUSY
);
5286 /* we need certain info from the top level */
5287 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_ARRAY
,
5288 vml
[c
]->vdev_top
->vdev_ms_array
) == 0);
5289 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_SHIFT
,
5290 vml
[c
]->vdev_top
->vdev_ms_shift
) == 0);
5291 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASIZE
,
5292 vml
[c
]->vdev_top
->vdev_asize
) == 0);
5293 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASHIFT
,
5294 vml
[c
]->vdev_top
->vdev_ashift
) == 0);
5296 /* transfer per-vdev ZAPs */
5297 ASSERT3U(vml
[c
]->vdev_leaf_zap
, !=, 0);
5298 VERIFY0(nvlist_add_uint64(child
[c
],
5299 ZPOOL_CONFIG_VDEV_LEAF_ZAP
, vml
[c
]->vdev_leaf_zap
));
5301 ASSERT3U(vml
[c
]->vdev_top
->vdev_top_zap
, !=, 0);
5302 VERIFY0(nvlist_add_uint64(child
[c
],
5303 ZPOOL_CONFIG_VDEV_TOP_ZAP
,
5304 vml
[c
]->vdev_parent
->vdev_top_zap
));
5308 kmem_free(vml
, children
* sizeof (vdev_t
*));
5309 kmem_free(glist
, children
* sizeof (uint64_t));
5310 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5313 /* stop writers from using the disks */
5314 for (c
= 0; c
< children
; c
++) {
5316 vml
[c
]->vdev_offline
= B_TRUE
;
5318 vdev_reopen(spa
->spa_root_vdev
);
5321 * Temporarily record the splitting vdevs in the spa config. This
5322 * will disappear once the config is regenerated.
5324 VERIFY(nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
5325 VERIFY(nvlist_add_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
5326 glist
, children
) == 0);
5327 kmem_free(glist
, children
* sizeof (uint64_t));
5329 mutex_enter(&spa
->spa_props_lock
);
5330 VERIFY(nvlist_add_nvlist(spa
->spa_config
, ZPOOL_CONFIG_SPLIT
,
5332 mutex_exit(&spa
->spa_props_lock
);
5333 spa
->spa_config_splitting
= nvl
;
5334 vdev_config_dirty(spa
->spa_root_vdev
);
5336 /* configure and create the new pool */
5337 VERIFY(nvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
, newname
) == 0);
5338 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
5339 exp
? POOL_STATE_EXPORTED
: POOL_STATE_ACTIVE
) == 0);
5340 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
5341 spa_version(spa
)) == 0);
5342 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
5343 spa
->spa_config_txg
) == 0);
5344 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
5345 spa_generate_guid(NULL
)) == 0);
5346 VERIFY0(nvlist_add_boolean(config
, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
));
5347 (void) nvlist_lookup_string(props
,
5348 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
5350 /* add the new pool to the namespace */
5351 newspa
= spa_add(newname
, config
, altroot
);
5352 newspa
->spa_avz_action
= AVZ_ACTION_REBUILD
;
5353 newspa
->spa_config_txg
= spa
->spa_config_txg
;
5354 spa_set_log_state(newspa
, SPA_LOG_CLEAR
);
5356 /* release the spa config lock, retaining the namespace lock */
5357 spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
5359 if (zio_injection_enabled
)
5360 zio_handle_panic_injection(spa
, FTAG
, 1);
5362 spa_activate(newspa
, spa_mode_global
);
5363 spa_async_suspend(newspa
);
5365 /* create the new pool from the disks of the original pool */
5366 error
= spa_load(newspa
, SPA_LOAD_IMPORT
, SPA_IMPORT_ASSEMBLE
, B_TRUE
);
5370 /* if that worked, generate a real config for the new pool */
5371 if (newspa
->spa_root_vdev
!= NULL
) {
5372 VERIFY(nvlist_alloc(&newspa
->spa_config_splitting
,
5373 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
5374 VERIFY(nvlist_add_uint64(newspa
->spa_config_splitting
,
5375 ZPOOL_CONFIG_SPLIT_GUID
, spa_guid(spa
)) == 0);
5376 spa_config_set(newspa
, spa_config_generate(newspa
, NULL
, -1ULL,
5381 if (props
!= NULL
) {
5382 spa_configfile_set(newspa
, props
, B_FALSE
);
5383 error
= spa_prop_set(newspa
, props
);
5388 /* flush everything */
5389 txg
= spa_vdev_config_enter(newspa
);
5390 vdev_config_dirty(newspa
->spa_root_vdev
);
5391 (void) spa_vdev_config_exit(newspa
, NULL
, txg
, 0, FTAG
);
5393 if (zio_injection_enabled
)
5394 zio_handle_panic_injection(spa
, FTAG
, 2);
5396 spa_async_resume(newspa
);
5398 /* finally, update the original pool's config */
5399 txg
= spa_vdev_config_enter(spa
);
5400 tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
5401 error
= dmu_tx_assign(tx
, TXG_WAIT
);
5404 for (c
= 0; c
< children
; c
++) {
5405 if (vml
[c
] != NULL
) {
5408 spa_history_log_internal(spa
, "detach", tx
,
5409 "vdev=%s", vml
[c
]->vdev_path
);
5414 spa
->spa_avz_action
= AVZ_ACTION_REBUILD
;
5415 vdev_config_dirty(spa
->spa_root_vdev
);
5416 spa
->spa_config_splitting
= NULL
;
5420 (void) spa_vdev_exit(spa
, NULL
, txg
, 0);
5422 if (zio_injection_enabled
)
5423 zio_handle_panic_injection(spa
, FTAG
, 3);
5425 /* split is complete; log a history record */
5426 spa_history_log_internal(newspa
, "split", NULL
,
5427 "from pool %s", spa_name(spa
));
5429 kmem_free(vml
, children
* sizeof (vdev_t
*));
5431 /* if we're not going to mount the filesystems in userland, export */
5433 error
= spa_export_common(newname
, POOL_STATE_EXPORTED
, NULL
,
5440 spa_deactivate(newspa
);
5443 txg
= spa_vdev_config_enter(spa
);
5445 /* re-online all offlined disks */
5446 for (c
= 0; c
< children
; c
++) {
5448 vml
[c
]->vdev_offline
= B_FALSE
;
5450 vdev_reopen(spa
->spa_root_vdev
);
5452 nvlist_free(spa
->spa_config_splitting
);
5453 spa
->spa_config_splitting
= NULL
;
5454 (void) spa_vdev_exit(spa
, NULL
, txg
, error
);
5456 kmem_free(vml
, children
* sizeof (vdev_t
*));
5461 spa_nvlist_lookup_by_guid(nvlist_t
**nvpp
, int count
, uint64_t target_guid
)
5465 for (i
= 0; i
< count
; i
++) {
5468 VERIFY(nvlist_lookup_uint64(nvpp
[i
], ZPOOL_CONFIG_GUID
,
5471 if (guid
== target_guid
)
5479 spa_vdev_remove_aux(nvlist_t
*config
, char *name
, nvlist_t
**dev
, int count
,
5480 nvlist_t
*dev_to_remove
)
5482 nvlist_t
**newdev
= NULL
;
5486 newdev
= kmem_alloc((count
- 1) * sizeof (void *), KM_SLEEP
);
5488 for (i
= 0, j
= 0; i
< count
; i
++) {
5489 if (dev
[i
] == dev_to_remove
)
5491 VERIFY(nvlist_dup(dev
[i
], &newdev
[j
++], KM_SLEEP
) == 0);
5494 VERIFY(nvlist_remove(config
, name
, DATA_TYPE_NVLIST_ARRAY
) == 0);
5495 VERIFY(nvlist_add_nvlist_array(config
, name
, newdev
, count
- 1) == 0);
5497 for (i
= 0; i
< count
- 1; i
++)
5498 nvlist_free(newdev
[i
]);
5501 kmem_free(newdev
, (count
- 1) * sizeof (void *));
5505 * Evacuate the device.
5508 spa_vdev_remove_evacuate(spa_t
*spa
, vdev_t
*vd
)
5513 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
5514 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
5515 ASSERT(vd
== vd
->vdev_top
);
5518 * Evacuate the device. We don't hold the config lock as writer
5519 * since we need to do I/O but we do keep the
5520 * spa_namespace_lock held. Once this completes the device
5521 * should no longer have any blocks allocated on it.
5523 if (vd
->vdev_islog
) {
5524 if (vd
->vdev_stat
.vs_alloc
!= 0)
5525 error
= spa_offline_log(spa
);
5527 error
= SET_ERROR(ENOTSUP
);
5534 * The evacuation succeeded. Remove any remaining MOS metadata
5535 * associated with this vdev, and wait for these changes to sync.
5537 ASSERT0(vd
->vdev_stat
.vs_alloc
);
5538 txg
= spa_vdev_config_enter(spa
);
5539 vd
->vdev_removing
= B_TRUE
;
5540 vdev_dirty_leaves(vd
, VDD_DTL
, txg
);
5541 vdev_config_dirty(vd
);
5542 spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
5548 * Complete the removal by cleaning up the namespace.
5551 spa_vdev_remove_from_namespace(spa_t
*spa
, vdev_t
*vd
)
5553 vdev_t
*rvd
= spa
->spa_root_vdev
;
5554 uint64_t id
= vd
->vdev_id
;
5555 boolean_t last_vdev
= (id
== (rvd
->vdev_children
- 1));
5557 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
5558 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
5559 ASSERT(vd
== vd
->vdev_top
);
5562 * Only remove any devices which are empty.
5564 if (vd
->vdev_stat
.vs_alloc
!= 0)
5567 (void) vdev_label_init(vd
, 0, VDEV_LABEL_REMOVE
);
5569 if (list_link_active(&vd
->vdev_state_dirty_node
))
5570 vdev_state_clean(vd
);
5571 if (list_link_active(&vd
->vdev_config_dirty_node
))
5572 vdev_config_clean(vd
);
5577 vdev_compact_children(rvd
);
5579 vd
= vdev_alloc_common(spa
, id
, 0, &vdev_hole_ops
);
5580 vdev_add_child(rvd
, vd
);
5582 vdev_config_dirty(rvd
);
5585 * Reassess the health of our root vdev.
5591 * Remove a device from the pool -
5593 * Removing a device from the vdev namespace requires several steps
5594 * and can take a significant amount of time. As a result we use
5595 * the spa_vdev_config_[enter/exit] functions which allow us to
5596 * grab and release the spa_config_lock while still holding the namespace
5597 * lock. During each step the configuration is synced out.
5599 * Currently, this supports removing only hot spares, slogs, and level 2 ARC
5603 spa_vdev_remove(spa_t
*spa
, uint64_t guid
, boolean_t unspare
)
5606 metaslab_group_t
*mg
;
5607 nvlist_t
**spares
, **l2cache
, *nv
;
5609 uint_t nspares
, nl2cache
;
5611 boolean_t locked
= MUTEX_HELD(&spa_namespace_lock
);
5613 ASSERT(spa_writeable(spa
));
5616 txg
= spa_vdev_enter(spa
);
5618 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
5620 if (spa
->spa_spares
.sav_vdevs
!= NULL
&&
5621 nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
5622 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0 &&
5623 (nv
= spa_nvlist_lookup_by_guid(spares
, nspares
, guid
)) != NULL
) {
5625 * Only remove the hot spare if it's not currently in use
5628 if (vd
== NULL
|| unspare
) {
5629 spa_vdev_remove_aux(spa
->spa_spares
.sav_config
,
5630 ZPOOL_CONFIG_SPARES
, spares
, nspares
, nv
);
5631 spa_load_spares(spa
);
5632 spa
->spa_spares
.sav_sync
= B_TRUE
;
5634 error
= SET_ERROR(EBUSY
);
5636 } else if (spa
->spa_l2cache
.sav_vdevs
!= NULL
&&
5637 nvlist_lookup_nvlist_array(spa
->spa_l2cache
.sav_config
,
5638 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0 &&
5639 (nv
= spa_nvlist_lookup_by_guid(l2cache
, nl2cache
, guid
)) != NULL
) {
5641 * Cache devices can always be removed.
5643 spa_vdev_remove_aux(spa
->spa_l2cache
.sav_config
,
5644 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
, nv
);
5645 spa_load_l2cache(spa
);
5646 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
5647 } else if (vd
!= NULL
&& vd
->vdev_islog
) {
5649 ASSERT(vd
== vd
->vdev_top
);
5654 * Stop allocating from this vdev.
5656 metaslab_group_passivate(mg
);
5659 * Wait for the youngest allocations and frees to sync,
5660 * and then wait for the deferral of those frees to finish.
5662 spa_vdev_config_exit(spa
, NULL
,
5663 txg
+ TXG_CONCURRENT_STATES
+ TXG_DEFER_SIZE
, 0, FTAG
);
5666 * Attempt to evacuate the vdev.
5668 error
= spa_vdev_remove_evacuate(spa
, vd
);
5670 txg
= spa_vdev_config_enter(spa
);
5673 * If we couldn't evacuate the vdev, unwind.
5676 metaslab_group_activate(mg
);
5677 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5681 * Clean up the vdev namespace.
5683 spa_vdev_remove_from_namespace(spa
, vd
);
5685 } else if (vd
!= NULL
) {
5687 * Normal vdevs cannot be removed (yet).
5689 error
= SET_ERROR(ENOTSUP
);
5692 * There is no vdev of any kind with the specified guid.
5694 error
= SET_ERROR(ENOENT
);
5698 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5704 * Find any device that's done replacing, or a vdev marked 'unspare' that's
5705 * currently spared, so we can detach it.
5708 spa_vdev_resilver_done_hunt(vdev_t
*vd
)
5710 vdev_t
*newvd
, *oldvd
;
5713 for (c
= 0; c
< vd
->vdev_children
; c
++) {
5714 oldvd
= spa_vdev_resilver_done_hunt(vd
->vdev_child
[c
]);
5720 * Check for a completed replacement. We always consider the first
5721 * vdev in the list to be the oldest vdev, and the last one to be
5722 * the newest (see spa_vdev_attach() for how that works). In
5723 * the case where the newest vdev is faulted, we will not automatically
5724 * remove it after a resilver completes. This is OK as it will require
5725 * user intervention to determine which disk the admin wishes to keep.
5727 if (vd
->vdev_ops
== &vdev_replacing_ops
) {
5728 ASSERT(vd
->vdev_children
> 1);
5730 newvd
= vd
->vdev_child
[vd
->vdev_children
- 1];
5731 oldvd
= vd
->vdev_child
[0];
5733 if (vdev_dtl_empty(newvd
, DTL_MISSING
) &&
5734 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
5735 !vdev_dtl_required(oldvd
))
5740 * Check for a completed resilver with the 'unspare' flag set.
5742 if (vd
->vdev_ops
== &vdev_spare_ops
) {
5743 vdev_t
*first
= vd
->vdev_child
[0];
5744 vdev_t
*last
= vd
->vdev_child
[vd
->vdev_children
- 1];
5746 if (last
->vdev_unspare
) {
5749 } else if (first
->vdev_unspare
) {
5756 if (oldvd
!= NULL
&&
5757 vdev_dtl_empty(newvd
, DTL_MISSING
) &&
5758 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
5759 !vdev_dtl_required(oldvd
))
5763 * If there are more than two spares attached to a disk,
5764 * and those spares are not required, then we want to
5765 * attempt to free them up now so that they can be used
5766 * by other pools. Once we're back down to a single
5767 * disk+spare, we stop removing them.
5769 if (vd
->vdev_children
> 2) {
5770 newvd
= vd
->vdev_child
[1];
5772 if (newvd
->vdev_isspare
&& last
->vdev_isspare
&&
5773 vdev_dtl_empty(last
, DTL_MISSING
) &&
5774 vdev_dtl_empty(last
, DTL_OUTAGE
) &&
5775 !vdev_dtl_required(newvd
))
5784 spa_vdev_resilver_done(spa_t
*spa
)
5786 vdev_t
*vd
, *pvd
, *ppvd
;
5787 uint64_t guid
, sguid
, pguid
, ppguid
;
5789 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5791 while ((vd
= spa_vdev_resilver_done_hunt(spa
->spa_root_vdev
)) != NULL
) {
5792 pvd
= vd
->vdev_parent
;
5793 ppvd
= pvd
->vdev_parent
;
5794 guid
= vd
->vdev_guid
;
5795 pguid
= pvd
->vdev_guid
;
5796 ppguid
= ppvd
->vdev_guid
;
5799 * If we have just finished replacing a hot spared device, then
5800 * we need to detach the parent's first child (the original hot
5803 if (ppvd
->vdev_ops
== &vdev_spare_ops
&& pvd
->vdev_id
== 0 &&
5804 ppvd
->vdev_children
== 2) {
5805 ASSERT(pvd
->vdev_ops
== &vdev_replacing_ops
);
5806 sguid
= ppvd
->vdev_child
[1]->vdev_guid
;
5808 ASSERT(vd
->vdev_resilver_txg
== 0 || !vdev_dtl_required(vd
));
5810 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5811 if (spa_vdev_detach(spa
, guid
, pguid
, B_TRUE
) != 0)
5813 if (sguid
&& spa_vdev_detach(spa
, sguid
, ppguid
, B_TRUE
) != 0)
5815 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5818 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5822 * Update the stored path or FRU for this vdev.
5825 spa_vdev_set_common(spa_t
*spa
, uint64_t guid
, const char *value
,
5829 boolean_t sync
= B_FALSE
;
5831 ASSERT(spa_writeable(spa
));
5833 spa_vdev_state_enter(spa
, SCL_ALL
);
5835 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
5836 return (spa_vdev_state_exit(spa
, NULL
, ENOENT
));
5838 if (!vd
->vdev_ops
->vdev_op_leaf
)
5839 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
5842 if (strcmp(value
, vd
->vdev_path
) != 0) {
5843 spa_strfree(vd
->vdev_path
);
5844 vd
->vdev_path
= spa_strdup(value
);
5848 if (vd
->vdev_fru
== NULL
) {
5849 vd
->vdev_fru
= spa_strdup(value
);
5851 } else if (strcmp(value
, vd
->vdev_fru
) != 0) {
5852 spa_strfree(vd
->vdev_fru
);
5853 vd
->vdev_fru
= spa_strdup(value
);
5858 return (spa_vdev_state_exit(spa
, sync
? vd
: NULL
, 0));
5862 spa_vdev_setpath(spa_t
*spa
, uint64_t guid
, const char *newpath
)
5864 return (spa_vdev_set_common(spa
, guid
, newpath
, B_TRUE
));
5868 spa_vdev_setfru(spa_t
*spa
, uint64_t guid
, const char *newfru
)
5870 return (spa_vdev_set_common(spa
, guid
, newfru
, B_FALSE
));
5874 * ==========================================================================
5876 * ==========================================================================
5880 spa_scan_stop(spa_t
*spa
)
5882 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
5883 if (dsl_scan_resilvering(spa
->spa_dsl_pool
))
5884 return (SET_ERROR(EBUSY
));
5885 return (dsl_scan_cancel(spa
->spa_dsl_pool
));
5889 spa_scan(spa_t
*spa
, pool_scan_func_t func
)
5891 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
5893 if (func
>= POOL_SCAN_FUNCS
|| func
== POOL_SCAN_NONE
)
5894 return (SET_ERROR(ENOTSUP
));
5897 * If a resilver was requested, but there is no DTL on a
5898 * writeable leaf device, we have nothing to do.
5900 if (func
== POOL_SCAN_RESILVER
&&
5901 !vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
)) {
5902 spa_async_request(spa
, SPA_ASYNC_RESILVER_DONE
);
5906 return (dsl_scan(spa
->spa_dsl_pool
, func
));
5910 * ==========================================================================
5911 * SPA async task processing
5912 * ==========================================================================
5916 spa_async_remove(spa_t
*spa
, vdev_t
*vd
)
5920 if (vd
->vdev_remove_wanted
) {
5921 vd
->vdev_remove_wanted
= B_FALSE
;
5922 vd
->vdev_delayed_close
= B_FALSE
;
5923 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_REMOVED
, VDEV_AUX_NONE
);
5926 * We want to clear the stats, but we don't want to do a full
5927 * vdev_clear() as that will cause us to throw away
5928 * degraded/faulted state as well as attempt to reopen the
5929 * device, all of which is a waste.
5931 vd
->vdev_stat
.vs_read_errors
= 0;
5932 vd
->vdev_stat
.vs_write_errors
= 0;
5933 vd
->vdev_stat
.vs_checksum_errors
= 0;
5935 vdev_state_dirty(vd
->vdev_top
);
5938 for (c
= 0; c
< vd
->vdev_children
; c
++)
5939 spa_async_remove(spa
, vd
->vdev_child
[c
]);
5943 spa_async_probe(spa_t
*spa
, vdev_t
*vd
)
5947 if (vd
->vdev_probe_wanted
) {
5948 vd
->vdev_probe_wanted
= B_FALSE
;
5949 vdev_reopen(vd
); /* vdev_open() does the actual probe */
5952 for (c
= 0; c
< vd
->vdev_children
; c
++)
5953 spa_async_probe(spa
, vd
->vdev_child
[c
]);
5957 spa_async_autoexpand(spa_t
*spa
, vdev_t
*vd
)
5961 if (!spa
->spa_autoexpand
)
5964 for (c
= 0; c
< vd
->vdev_children
; c
++) {
5965 vdev_t
*cvd
= vd
->vdev_child
[c
];
5966 spa_async_autoexpand(spa
, cvd
);
5969 if (!vd
->vdev_ops
->vdev_op_leaf
|| vd
->vdev_physpath
== NULL
)
5972 spa_event_notify(vd
->vdev_spa
, vd
, FM_EREPORT_ZFS_DEVICE_AUTOEXPAND
);
5976 spa_async_thread(spa_t
*spa
)
5980 ASSERT(spa
->spa_sync_on
);
5982 mutex_enter(&spa
->spa_async_lock
);
5983 tasks
= spa
->spa_async_tasks
;
5984 spa
->spa_async_tasks
= 0;
5985 mutex_exit(&spa
->spa_async_lock
);
5988 * See if the config needs to be updated.
5990 if (tasks
& SPA_ASYNC_CONFIG_UPDATE
) {
5991 uint64_t old_space
, new_space
;
5993 mutex_enter(&spa_namespace_lock
);
5994 old_space
= metaslab_class_get_space(spa_normal_class(spa
));
5995 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
5996 new_space
= metaslab_class_get_space(spa_normal_class(spa
));
5997 mutex_exit(&spa_namespace_lock
);
6000 * If the pool grew as a result of the config update,
6001 * then log an internal history event.
6003 if (new_space
!= old_space
) {
6004 spa_history_log_internal(spa
, "vdev online", NULL
,
6005 "pool '%s' size: %llu(+%llu)",
6006 spa_name(spa
), new_space
, new_space
- old_space
);
6011 * See if any devices need to be marked REMOVED.
6013 if (tasks
& SPA_ASYNC_REMOVE
) {
6014 spa_vdev_state_enter(spa
, SCL_NONE
);
6015 spa_async_remove(spa
, spa
->spa_root_vdev
);
6016 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++)
6017 spa_async_remove(spa
, spa
->spa_l2cache
.sav_vdevs
[i
]);
6018 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
6019 spa_async_remove(spa
, spa
->spa_spares
.sav_vdevs
[i
]);
6020 (void) spa_vdev_state_exit(spa
, NULL
, 0);
6023 if ((tasks
& SPA_ASYNC_AUTOEXPAND
) && !spa_suspended(spa
)) {
6024 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
6025 spa_async_autoexpand(spa
, spa
->spa_root_vdev
);
6026 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
6030 * See if any devices need to be probed.
6032 if (tasks
& SPA_ASYNC_PROBE
) {
6033 spa_vdev_state_enter(spa
, SCL_NONE
);
6034 spa_async_probe(spa
, spa
->spa_root_vdev
);
6035 (void) spa_vdev_state_exit(spa
, NULL
, 0);
6039 * If any devices are done replacing, detach them.
6041 if (tasks
& SPA_ASYNC_RESILVER_DONE
)
6042 spa_vdev_resilver_done(spa
);
6045 * Kick off a resilver.
6047 if (tasks
& SPA_ASYNC_RESILVER
)
6048 dsl_resilver_restart(spa
->spa_dsl_pool
, 0);
6051 * Let the world know that we're done.
6053 mutex_enter(&spa
->spa_async_lock
);
6054 spa
->spa_async_thread
= NULL
;
6055 cv_broadcast(&spa
->spa_async_cv
);
6056 mutex_exit(&spa
->spa_async_lock
);
6061 spa_async_suspend(spa_t
*spa
)
6063 mutex_enter(&spa
->spa_async_lock
);
6064 spa
->spa_async_suspended
++;
6065 while (spa
->spa_async_thread
!= NULL
)
6066 cv_wait(&spa
->spa_async_cv
, &spa
->spa_async_lock
);
6067 mutex_exit(&spa
->spa_async_lock
);
6071 spa_async_resume(spa_t
*spa
)
6073 mutex_enter(&spa
->spa_async_lock
);
6074 ASSERT(spa
->spa_async_suspended
!= 0);
6075 spa
->spa_async_suspended
--;
6076 mutex_exit(&spa
->spa_async_lock
);
6080 spa_async_tasks_pending(spa_t
*spa
)
6082 uint_t non_config_tasks
;
6084 boolean_t config_task_suspended
;
6086 non_config_tasks
= spa
->spa_async_tasks
& ~SPA_ASYNC_CONFIG_UPDATE
;
6087 config_task
= spa
->spa_async_tasks
& SPA_ASYNC_CONFIG_UPDATE
;
6088 if (spa
->spa_ccw_fail_time
== 0) {
6089 config_task_suspended
= B_FALSE
;
6091 config_task_suspended
=
6092 (gethrtime() - spa
->spa_ccw_fail_time
) <
6093 (zfs_ccw_retry_interval
* NANOSEC
);
6096 return (non_config_tasks
|| (config_task
&& !config_task_suspended
));
6100 spa_async_dispatch(spa_t
*spa
)
6102 mutex_enter(&spa
->spa_async_lock
);
6103 if (spa_async_tasks_pending(spa
) &&
6104 !spa
->spa_async_suspended
&&
6105 spa
->spa_async_thread
== NULL
&&
6107 spa
->spa_async_thread
= thread_create(NULL
, 0,
6108 spa_async_thread
, spa
, 0, &p0
, TS_RUN
, maxclsyspri
);
6109 mutex_exit(&spa
->spa_async_lock
);
6113 spa_async_request(spa_t
*spa
, int task
)
6115 zfs_dbgmsg("spa=%s async request task=%u", spa
->spa_name
, task
);
6116 mutex_enter(&spa
->spa_async_lock
);
6117 spa
->spa_async_tasks
|= task
;
6118 mutex_exit(&spa
->spa_async_lock
);
6122 * ==========================================================================
6123 * SPA syncing routines
6124 * ==========================================================================
6128 bpobj_enqueue_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
6131 bpobj_enqueue(bpo
, bp
, tx
);
6136 spa_free_sync_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
6140 zio_nowait(zio_free_sync(zio
, zio
->io_spa
, dmu_tx_get_txg(tx
), bp
,
6146 * Note: this simple function is not inlined to make it easier to dtrace the
6147 * amount of time spent syncing frees.
6150 spa_sync_frees(spa_t
*spa
, bplist_t
*bpl
, dmu_tx_t
*tx
)
6152 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
6153 bplist_iterate(bpl
, spa_free_sync_cb
, zio
, tx
);
6154 VERIFY(zio_wait(zio
) == 0);
6158 * Note: this simple function is not inlined to make it easier to dtrace the
6159 * amount of time spent syncing deferred frees.
6162 spa_sync_deferred_frees(spa_t
*spa
, dmu_tx_t
*tx
)
6164 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
6165 VERIFY3U(bpobj_iterate(&spa
->spa_deferred_bpobj
,
6166 spa_free_sync_cb
, zio
, tx
), ==, 0);
6167 VERIFY0(zio_wait(zio
));
6171 spa_sync_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
*nv
, dmu_tx_t
*tx
)
6173 char *packed
= NULL
;
6178 VERIFY(nvlist_size(nv
, &nvsize
, NV_ENCODE_XDR
) == 0);
6181 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
6182 * information. This avoids the dmu_buf_will_dirty() path and
6183 * saves us a pre-read to get data we don't actually care about.
6185 bufsize
= P2ROUNDUP((uint64_t)nvsize
, SPA_CONFIG_BLOCKSIZE
);
6186 packed
= vmem_alloc(bufsize
, KM_SLEEP
);
6188 VERIFY(nvlist_pack(nv
, &packed
, &nvsize
, NV_ENCODE_XDR
,
6190 bzero(packed
+ nvsize
, bufsize
- nvsize
);
6192 dmu_write(spa
->spa_meta_objset
, obj
, 0, bufsize
, packed
, tx
);
6194 vmem_free(packed
, bufsize
);
6196 VERIFY(0 == dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
));
6197 dmu_buf_will_dirty(db
, tx
);
6198 *(uint64_t *)db
->db_data
= nvsize
;
6199 dmu_buf_rele(db
, FTAG
);
6203 spa_sync_aux_dev(spa_t
*spa
, spa_aux_vdev_t
*sav
, dmu_tx_t
*tx
,
6204 const char *config
, const char *entry
)
6214 * Update the MOS nvlist describing the list of available devices.
6215 * spa_validate_aux() will have already made sure this nvlist is
6216 * valid and the vdevs are labeled appropriately.
6218 if (sav
->sav_object
== 0) {
6219 sav
->sav_object
= dmu_object_alloc(spa
->spa_meta_objset
,
6220 DMU_OT_PACKED_NVLIST
, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE
,
6221 sizeof (uint64_t), tx
);
6222 VERIFY(zap_update(spa
->spa_meta_objset
,
6223 DMU_POOL_DIRECTORY_OBJECT
, entry
, sizeof (uint64_t), 1,
6224 &sav
->sav_object
, tx
) == 0);
6227 VERIFY(nvlist_alloc(&nvroot
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
6228 if (sav
->sav_count
== 0) {
6229 VERIFY(nvlist_add_nvlist_array(nvroot
, config
, NULL
, 0) == 0);
6231 list
= kmem_alloc(sav
->sav_count
*sizeof (void *), KM_SLEEP
);
6232 for (i
= 0; i
< sav
->sav_count
; i
++)
6233 list
[i
] = vdev_config_generate(spa
, sav
->sav_vdevs
[i
],
6234 B_FALSE
, VDEV_CONFIG_L2CACHE
);
6235 VERIFY(nvlist_add_nvlist_array(nvroot
, config
, list
,
6236 sav
->sav_count
) == 0);
6237 for (i
= 0; i
< sav
->sav_count
; i
++)
6238 nvlist_free(list
[i
]);
6239 kmem_free(list
, sav
->sav_count
* sizeof (void *));
6242 spa_sync_nvlist(spa
, sav
->sav_object
, nvroot
, tx
);
6243 nvlist_free(nvroot
);
6245 sav
->sav_sync
= B_FALSE
;
6249 * Rebuild spa's all-vdev ZAP from the vdev ZAPs indicated in each vdev_t.
6250 * The all-vdev ZAP must be empty.
6253 spa_avz_build(vdev_t
*vd
, uint64_t avz
, dmu_tx_t
*tx
)
6255 spa_t
*spa
= vd
->vdev_spa
;
6258 if (vd
->vdev_top_zap
!= 0) {
6259 VERIFY0(zap_add_int(spa
->spa_meta_objset
, avz
,
6260 vd
->vdev_top_zap
, tx
));
6262 if (vd
->vdev_leaf_zap
!= 0) {
6263 VERIFY0(zap_add_int(spa
->spa_meta_objset
, avz
,
6264 vd
->vdev_leaf_zap
, tx
));
6266 for (i
= 0; i
< vd
->vdev_children
; i
++) {
6267 spa_avz_build(vd
->vdev_child
[i
], avz
, tx
);
6272 spa_sync_config_object(spa_t
*spa
, dmu_tx_t
*tx
)
6277 * If the pool is being imported from a pre-per-vdev-ZAP version of ZFS,
6278 * its config may not be dirty but we still need to build per-vdev ZAPs.
6279 * Similarly, if the pool is being assembled (e.g. after a split), we
6280 * need to rebuild the AVZ although the config may not be dirty.
6282 if (list_is_empty(&spa
->spa_config_dirty_list
) &&
6283 spa
->spa_avz_action
== AVZ_ACTION_NONE
)
6286 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
6288 ASSERT(spa
->spa_avz_action
== AVZ_ACTION_NONE
||
6289 spa
->spa_all_vdev_zaps
!= 0);
6291 if (spa
->spa_avz_action
== AVZ_ACTION_REBUILD
) {
6295 /* Make and build the new AVZ */
6296 uint64_t new_avz
= zap_create(spa
->spa_meta_objset
,
6297 DMU_OTN_ZAP_METADATA
, DMU_OT_NONE
, 0, tx
);
6298 spa_avz_build(spa
->spa_root_vdev
, new_avz
, tx
);
6300 /* Diff old AVZ with new one */
6301 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
6302 spa
->spa_all_vdev_zaps
);
6303 zap_cursor_retrieve(&zc
, &za
) == 0;
6304 zap_cursor_advance(&zc
)) {
6305 uint64_t vdzap
= za
.za_first_integer
;
6306 if (zap_lookup_int(spa
->spa_meta_objset
, new_avz
,
6309 * ZAP is listed in old AVZ but not in new one;
6312 VERIFY0(zap_destroy(spa
->spa_meta_objset
, vdzap
,
6317 zap_cursor_fini(&zc
);
6319 /* Destroy the old AVZ */
6320 VERIFY0(zap_destroy(spa
->spa_meta_objset
,
6321 spa
->spa_all_vdev_zaps
, tx
));
6323 /* Replace the old AVZ in the dir obj with the new one */
6324 VERIFY0(zap_update(spa
->spa_meta_objset
,
6325 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_VDEV_ZAP_MAP
,
6326 sizeof (new_avz
), 1, &new_avz
, tx
));
6328 spa
->spa_all_vdev_zaps
= new_avz
;
6329 } else if (spa
->spa_avz_action
== AVZ_ACTION_DESTROY
) {
6333 /* Walk through the AVZ and destroy all listed ZAPs */
6334 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
6335 spa
->spa_all_vdev_zaps
);
6336 zap_cursor_retrieve(&zc
, &za
) == 0;
6337 zap_cursor_advance(&zc
)) {
6338 uint64_t zap
= za
.za_first_integer
;
6339 VERIFY0(zap_destroy(spa
->spa_meta_objset
, zap
, tx
));
6342 zap_cursor_fini(&zc
);
6344 /* Destroy and unlink the AVZ itself */
6345 VERIFY0(zap_destroy(spa
->spa_meta_objset
,
6346 spa
->spa_all_vdev_zaps
, tx
));
6347 VERIFY0(zap_remove(spa
->spa_meta_objset
,
6348 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_VDEV_ZAP_MAP
, tx
));
6349 spa
->spa_all_vdev_zaps
= 0;
6352 if (spa
->spa_all_vdev_zaps
== 0) {
6353 spa
->spa_all_vdev_zaps
= zap_create_link(spa
->spa_meta_objset
,
6354 DMU_OTN_ZAP_METADATA
, DMU_POOL_DIRECTORY_OBJECT
,
6355 DMU_POOL_VDEV_ZAP_MAP
, tx
);
6357 spa
->spa_avz_action
= AVZ_ACTION_NONE
;
6359 /* Create ZAPs for vdevs that don't have them. */
6360 vdev_construct_zaps(spa
->spa_root_vdev
, tx
);
6362 config
= spa_config_generate(spa
, spa
->spa_root_vdev
,
6363 dmu_tx_get_txg(tx
), B_FALSE
);
6366 * If we're upgrading the spa version then make sure that
6367 * the config object gets updated with the correct version.
6369 if (spa
->spa_ubsync
.ub_version
< spa
->spa_uberblock
.ub_version
)
6370 fnvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
6371 spa
->spa_uberblock
.ub_version
);
6373 spa_config_exit(spa
, SCL_STATE
, FTAG
);
6375 nvlist_free(spa
->spa_config_syncing
);
6376 spa
->spa_config_syncing
= config
;
6378 spa_sync_nvlist(spa
, spa
->spa_config_object
, config
, tx
);
6382 spa_sync_version(void *arg
, dmu_tx_t
*tx
)
6384 uint64_t *versionp
= arg
;
6385 uint64_t version
= *versionp
;
6386 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
6389 * Setting the version is special cased when first creating the pool.
6391 ASSERT(tx
->tx_txg
!= TXG_INITIAL
);
6393 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
6394 ASSERT(version
>= spa_version(spa
));
6396 spa
->spa_uberblock
.ub_version
= version
;
6397 vdev_config_dirty(spa
->spa_root_vdev
);
6398 spa_history_log_internal(spa
, "set", tx
, "version=%lld", version
);
6402 * Set zpool properties.
6405 spa_sync_props(void *arg
, dmu_tx_t
*tx
)
6407 nvlist_t
*nvp
= arg
;
6408 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
6409 objset_t
*mos
= spa
->spa_meta_objset
;
6410 nvpair_t
*elem
= NULL
;
6412 mutex_enter(&spa
->spa_props_lock
);
6414 while ((elem
= nvlist_next_nvpair(nvp
, elem
))) {
6416 char *strval
, *fname
;
6418 const char *propname
;
6419 zprop_type_t proptype
;
6422 prop
= zpool_name_to_prop(nvpair_name(elem
));
6423 switch ((int)prop
) {
6426 * We checked this earlier in spa_prop_validate().
6428 ASSERT(zpool_prop_feature(nvpair_name(elem
)));
6430 fname
= strchr(nvpair_name(elem
), '@') + 1;
6431 VERIFY0(zfeature_lookup_name(fname
, &fid
));
6433 spa_feature_enable(spa
, fid
, tx
);
6434 spa_history_log_internal(spa
, "set", tx
,
6435 "%s=enabled", nvpair_name(elem
));
6438 case ZPOOL_PROP_VERSION
:
6439 intval
= fnvpair_value_uint64(elem
);
6441 * The version is synced seperatly before other
6442 * properties and should be correct by now.
6444 ASSERT3U(spa_version(spa
), >=, intval
);
6447 case ZPOOL_PROP_ALTROOT
:
6449 * 'altroot' is a non-persistent property. It should
6450 * have been set temporarily at creation or import time.
6452 ASSERT(spa
->spa_root
!= NULL
);
6455 case ZPOOL_PROP_READONLY
:
6456 case ZPOOL_PROP_CACHEFILE
:
6458 * 'readonly' and 'cachefile' are also non-persisitent
6462 case ZPOOL_PROP_COMMENT
:
6463 strval
= fnvpair_value_string(elem
);
6464 if (spa
->spa_comment
!= NULL
)
6465 spa_strfree(spa
->spa_comment
);
6466 spa
->spa_comment
= spa_strdup(strval
);
6468 * We need to dirty the configuration on all the vdevs
6469 * so that their labels get updated. It's unnecessary
6470 * to do this for pool creation since the vdev's
6471 * configuratoin has already been dirtied.
6473 if (tx
->tx_txg
!= TXG_INITIAL
)
6474 vdev_config_dirty(spa
->spa_root_vdev
);
6475 spa_history_log_internal(spa
, "set", tx
,
6476 "%s=%s", nvpair_name(elem
), strval
);
6480 * Set pool property values in the poolprops mos object.
6482 if (spa
->spa_pool_props_object
== 0) {
6483 spa
->spa_pool_props_object
=
6484 zap_create_link(mos
, DMU_OT_POOL_PROPS
,
6485 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_PROPS
,
6489 /* normalize the property name */
6490 propname
= zpool_prop_to_name(prop
);
6491 proptype
= zpool_prop_get_type(prop
);
6493 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
6494 ASSERT(proptype
== PROP_TYPE_STRING
);
6495 strval
= fnvpair_value_string(elem
);
6496 VERIFY0(zap_update(mos
,
6497 spa
->spa_pool_props_object
, propname
,
6498 1, strlen(strval
) + 1, strval
, tx
));
6499 spa_history_log_internal(spa
, "set", tx
,
6500 "%s=%s", nvpair_name(elem
), strval
);
6501 } else if (nvpair_type(elem
) == DATA_TYPE_UINT64
) {
6502 intval
= fnvpair_value_uint64(elem
);
6504 if (proptype
== PROP_TYPE_INDEX
) {
6506 VERIFY0(zpool_prop_index_to_string(
6507 prop
, intval
, &unused
));
6509 VERIFY0(zap_update(mos
,
6510 spa
->spa_pool_props_object
, propname
,
6511 8, 1, &intval
, tx
));
6512 spa_history_log_internal(spa
, "set", tx
,
6513 "%s=%lld", nvpair_name(elem
), intval
);
6515 ASSERT(0); /* not allowed */
6519 case ZPOOL_PROP_DELEGATION
:
6520 spa
->spa_delegation
= intval
;
6522 case ZPOOL_PROP_BOOTFS
:
6523 spa
->spa_bootfs
= intval
;
6525 case ZPOOL_PROP_FAILUREMODE
:
6526 spa
->spa_failmode
= intval
;
6528 case ZPOOL_PROP_AUTOEXPAND
:
6529 spa
->spa_autoexpand
= intval
;
6530 if (tx
->tx_txg
!= TXG_INITIAL
)
6531 spa_async_request(spa
,
6532 SPA_ASYNC_AUTOEXPAND
);
6534 case ZPOOL_PROP_DEDUPDITTO
:
6535 spa
->spa_dedup_ditto
= intval
;
6544 mutex_exit(&spa
->spa_props_lock
);
6548 * Perform one-time upgrade on-disk changes. spa_version() does not
6549 * reflect the new version this txg, so there must be no changes this
6550 * txg to anything that the upgrade code depends on after it executes.
6551 * Therefore this must be called after dsl_pool_sync() does the sync
6555 spa_sync_upgrades(spa_t
*spa
, dmu_tx_t
*tx
)
6557 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
6559 ASSERT(spa
->spa_sync_pass
== 1);
6561 rrw_enter(&dp
->dp_config_rwlock
, RW_WRITER
, FTAG
);
6563 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_ORIGIN
&&
6564 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_ORIGIN
) {
6565 dsl_pool_create_origin(dp
, tx
);
6567 /* Keeping the origin open increases spa_minref */
6568 spa
->spa_minref
+= 3;
6571 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_NEXT_CLONES
&&
6572 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_NEXT_CLONES
) {
6573 dsl_pool_upgrade_clones(dp
, tx
);
6576 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_DIR_CLONES
&&
6577 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_DIR_CLONES
) {
6578 dsl_pool_upgrade_dir_clones(dp
, tx
);
6580 /* Keeping the freedir open increases spa_minref */
6581 spa
->spa_minref
+= 3;
6584 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_FEATURES
&&
6585 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
6586 spa_feature_create_zap_objects(spa
, tx
);
6590 * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable
6591 * when possibility to use lz4 compression for metadata was added
6592 * Old pools that have this feature enabled must be upgraded to have
6593 * this feature active
6595 if (spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
6596 boolean_t lz4_en
= spa_feature_is_enabled(spa
,
6597 SPA_FEATURE_LZ4_COMPRESS
);
6598 boolean_t lz4_ac
= spa_feature_is_active(spa
,
6599 SPA_FEATURE_LZ4_COMPRESS
);
6601 if (lz4_en
&& !lz4_ac
)
6602 spa_feature_incr(spa
, SPA_FEATURE_LZ4_COMPRESS
, tx
);
6604 rrw_exit(&dp
->dp_config_rwlock
, FTAG
);
6608 * Sync the specified transaction group. New blocks may be dirtied as
6609 * part of the process, so we iterate until it converges.
6612 spa_sync(spa_t
*spa
, uint64_t txg
)
6614 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
6615 objset_t
*mos
= spa
->spa_meta_objset
;
6616 bplist_t
*free_bpl
= &spa
->spa_free_bplist
[txg
& TXG_MASK
];
6617 vdev_t
*rvd
= spa
->spa_root_vdev
;
6623 VERIFY(spa_writeable(spa
));
6626 * Lock out configuration changes.
6628 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
6630 spa
->spa_syncing_txg
= txg
;
6631 spa
->spa_sync_pass
= 0;
6634 * If there are any pending vdev state changes, convert them
6635 * into config changes that go out with this transaction group.
6637 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
6638 while (list_head(&spa
->spa_state_dirty_list
) != NULL
) {
6640 * We need the write lock here because, for aux vdevs,
6641 * calling vdev_config_dirty() modifies sav_config.
6642 * This is ugly and will become unnecessary when we
6643 * eliminate the aux vdev wart by integrating all vdevs
6644 * into the root vdev tree.
6646 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
6647 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_WRITER
);
6648 while ((vd
= list_head(&spa
->spa_state_dirty_list
)) != NULL
) {
6649 vdev_state_clean(vd
);
6650 vdev_config_dirty(vd
);
6652 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
6653 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
6655 spa_config_exit(spa
, SCL_STATE
, FTAG
);
6657 tx
= dmu_tx_create_assigned(dp
, txg
);
6659 spa
->spa_sync_starttime
= gethrtime();
6660 taskq_cancel_id(system_taskq
, spa
->spa_deadman_tqid
);
6661 spa
->spa_deadman_tqid
= taskq_dispatch_delay(system_taskq
,
6662 spa_deadman
, spa
, TQ_SLEEP
, ddi_get_lbolt() +
6663 NSEC_TO_TICK(spa
->spa_deadman_synctime
));
6666 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
6667 * set spa_deflate if we have no raid-z vdevs.
6669 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_RAIDZ_DEFLATE
&&
6670 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
6673 for (i
= 0; i
< rvd
->vdev_children
; i
++) {
6674 vd
= rvd
->vdev_child
[i
];
6675 if (vd
->vdev_deflate_ratio
!= SPA_MINBLOCKSIZE
)
6678 if (i
== rvd
->vdev_children
) {
6679 spa
->spa_deflate
= TRUE
;
6680 VERIFY(0 == zap_add(spa
->spa_meta_objset
,
6681 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
6682 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
));
6687 * Iterate to convergence.
6690 int pass
= ++spa
->spa_sync_pass
;
6692 spa_sync_config_object(spa
, tx
);
6693 spa_sync_aux_dev(spa
, &spa
->spa_spares
, tx
,
6694 ZPOOL_CONFIG_SPARES
, DMU_POOL_SPARES
);
6695 spa_sync_aux_dev(spa
, &spa
->spa_l2cache
, tx
,
6696 ZPOOL_CONFIG_L2CACHE
, DMU_POOL_L2CACHE
);
6697 spa_errlog_sync(spa
, txg
);
6698 dsl_pool_sync(dp
, txg
);
6700 if (pass
< zfs_sync_pass_deferred_free
) {
6701 spa_sync_frees(spa
, free_bpl
, tx
);
6704 * We can not defer frees in pass 1, because
6705 * we sync the deferred frees later in pass 1.
6707 ASSERT3U(pass
, >, 1);
6708 bplist_iterate(free_bpl
, bpobj_enqueue_cb
,
6709 &spa
->spa_deferred_bpobj
, tx
);
6713 dsl_scan_sync(dp
, tx
);
6715 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, txg
)))
6719 spa_sync_upgrades(spa
, tx
);
6721 spa
->spa_uberblock
.ub_rootbp
.blk_birth
);
6723 * Note: We need to check if the MOS is dirty
6724 * because we could have marked the MOS dirty
6725 * without updating the uberblock (e.g. if we
6726 * have sync tasks but no dirty user data). We
6727 * need to check the uberblock's rootbp because
6728 * it is updated if we have synced out dirty
6729 * data (though in this case the MOS will most
6730 * likely also be dirty due to second order
6731 * effects, we don't want to rely on that here).
6733 if (spa
->spa_uberblock
.ub_rootbp
.blk_birth
< txg
&&
6734 !dmu_objset_is_dirty(mos
, txg
)) {
6736 * Nothing changed on the first pass,
6737 * therefore this TXG is a no-op. Avoid
6738 * syncing deferred frees, so that we
6739 * can keep this TXG as a no-op.
6741 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
,
6743 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
6744 ASSERT(txg_list_empty(&dp
->dp_sync_tasks
, txg
));
6747 spa_sync_deferred_frees(spa
, tx
);
6750 } while (dmu_objset_is_dirty(mos
, txg
));
6753 if (!list_is_empty(&spa
->spa_config_dirty_list
)) {
6755 * Make sure that the number of ZAPs for all the vdevs matches
6756 * the number of ZAPs in the per-vdev ZAP list. This only gets
6757 * called if the config is dirty; otherwise there may be
6758 * outstanding AVZ operations that weren't completed in
6759 * spa_sync_config_object.
6761 uint64_t all_vdev_zap_entry_count
;
6762 ASSERT0(zap_count(spa
->spa_meta_objset
,
6763 spa
->spa_all_vdev_zaps
, &all_vdev_zap_entry_count
));
6764 ASSERT3U(vdev_count_verify_zaps(spa
->spa_root_vdev
), ==,
6765 all_vdev_zap_entry_count
);
6770 * Rewrite the vdev configuration (which includes the uberblock)
6771 * to commit the transaction group.
6773 * If there are no dirty vdevs, we sync the uberblock to a few
6774 * random top-level vdevs that are known to be visible in the
6775 * config cache (see spa_vdev_add() for a complete description).
6776 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
6780 * We hold SCL_STATE to prevent vdev open/close/etc.
6781 * while we're attempting to write the vdev labels.
6783 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
6785 if (list_is_empty(&spa
->spa_config_dirty_list
)) {
6786 vdev_t
*svd
[SPA_DVAS_PER_BP
];
6788 int children
= rvd
->vdev_children
;
6789 int c0
= spa_get_random(children
);
6791 for (c
= 0; c
< children
; c
++) {
6792 vd
= rvd
->vdev_child
[(c0
+ c
) % children
];
6793 if (vd
->vdev_ms_array
== 0 || vd
->vdev_islog
)
6795 svd
[svdcount
++] = vd
;
6796 if (svdcount
== SPA_DVAS_PER_BP
)
6799 error
= vdev_config_sync(svd
, svdcount
, txg
);
6801 error
= vdev_config_sync(rvd
->vdev_child
,
6802 rvd
->vdev_children
, txg
);
6806 spa
->spa_last_synced_guid
= rvd
->vdev_guid
;
6808 spa_config_exit(spa
, SCL_STATE
, FTAG
);
6812 zio_suspend(spa
, NULL
);
6813 zio_resume_wait(spa
);
6817 taskq_cancel_id(system_taskq
, spa
->spa_deadman_tqid
);
6818 spa
->spa_deadman_tqid
= 0;
6821 * Clear the dirty config list.
6823 while ((vd
= list_head(&spa
->spa_config_dirty_list
)) != NULL
)
6824 vdev_config_clean(vd
);
6827 * Now that the new config has synced transactionally,
6828 * let it become visible to the config cache.
6830 if (spa
->spa_config_syncing
!= NULL
) {
6831 spa_config_set(spa
, spa
->spa_config_syncing
);
6832 spa
->spa_config_txg
= txg
;
6833 spa
->spa_config_syncing
= NULL
;
6836 spa
->spa_ubsync
= spa
->spa_uberblock
;
6838 dsl_pool_sync_done(dp
, txg
);
6841 * Update usable space statistics.
6843 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, TXG_CLEAN(txg
))))
6844 vdev_sync_done(vd
, txg
);
6846 spa_update_dspace(spa
);
6849 * It had better be the case that we didn't dirty anything
6850 * since vdev_config_sync().
6852 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
, txg
));
6853 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
6854 ASSERT(txg_list_empty(&spa
->spa_vdev_txg_list
, txg
));
6856 spa
->spa_sync_pass
= 0;
6858 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
6860 spa_handle_ignored_writes(spa
);
6863 * If any async tasks have been requested, kick them off.
6865 spa_async_dispatch(spa
);
6869 * Sync all pools. We don't want to hold the namespace lock across these
6870 * operations, so we take a reference on the spa_t and drop the lock during the
6874 spa_sync_allpools(void)
6877 mutex_enter(&spa_namespace_lock
);
6878 while ((spa
= spa_next(spa
)) != NULL
) {
6879 if (spa_state(spa
) != POOL_STATE_ACTIVE
||
6880 !spa_writeable(spa
) || spa_suspended(spa
))
6882 spa_open_ref(spa
, FTAG
);
6883 mutex_exit(&spa_namespace_lock
);
6884 txg_wait_synced(spa_get_dsl(spa
), 0);
6885 mutex_enter(&spa_namespace_lock
);
6886 spa_close(spa
, FTAG
);
6888 mutex_exit(&spa_namespace_lock
);
6892 * ==========================================================================
6893 * Miscellaneous routines
6894 * ==========================================================================
6898 * Remove all pools in the system.
6906 * Remove all cached state. All pools should be closed now,
6907 * so every spa in the AVL tree should be unreferenced.
6909 mutex_enter(&spa_namespace_lock
);
6910 while ((spa
= spa_next(NULL
)) != NULL
) {
6912 * Stop async tasks. The async thread may need to detach
6913 * a device that's been replaced, which requires grabbing
6914 * spa_namespace_lock, so we must drop it here.
6916 spa_open_ref(spa
, FTAG
);
6917 mutex_exit(&spa_namespace_lock
);
6918 spa_async_suspend(spa
);
6919 mutex_enter(&spa_namespace_lock
);
6920 spa_close(spa
, FTAG
);
6922 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
6924 spa_deactivate(spa
);
6928 mutex_exit(&spa_namespace_lock
);
6932 spa_lookup_by_guid(spa_t
*spa
, uint64_t guid
, boolean_t aux
)
6937 if ((vd
= vdev_lookup_by_guid(spa
->spa_root_vdev
, guid
)) != NULL
)
6941 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
6942 vd
= spa
->spa_l2cache
.sav_vdevs
[i
];
6943 if (vd
->vdev_guid
== guid
)
6947 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
6948 vd
= spa
->spa_spares
.sav_vdevs
[i
];
6949 if (vd
->vdev_guid
== guid
)
6958 spa_upgrade(spa_t
*spa
, uint64_t version
)
6960 ASSERT(spa_writeable(spa
));
6962 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6965 * This should only be called for a non-faulted pool, and since a
6966 * future version would result in an unopenable pool, this shouldn't be
6969 ASSERT(SPA_VERSION_IS_SUPPORTED(spa
->spa_uberblock
.ub_version
));
6970 ASSERT3U(version
, >=, spa
->spa_uberblock
.ub_version
);
6972 spa
->spa_uberblock
.ub_version
= version
;
6973 vdev_config_dirty(spa
->spa_root_vdev
);
6975 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6977 txg_wait_synced(spa_get_dsl(spa
), 0);
6981 spa_has_spare(spa_t
*spa
, uint64_t guid
)
6985 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
6987 for (i
= 0; i
< sav
->sav_count
; i
++)
6988 if (sav
->sav_vdevs
[i
]->vdev_guid
== guid
)
6991 for (i
= 0; i
< sav
->sav_npending
; i
++) {
6992 if (nvlist_lookup_uint64(sav
->sav_pending
[i
], ZPOOL_CONFIG_GUID
,
6993 &spareguid
) == 0 && spareguid
== guid
)
7001 * Check if a pool has an active shared spare device.
7002 * Note: reference count of an active spare is 2, as a spare and as a replace
7005 spa_has_active_shared_spare(spa_t
*spa
)
7009 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
7011 for (i
= 0; i
< sav
->sav_count
; i
++) {
7012 if (spa_spare_exists(sav
->sav_vdevs
[i
]->vdev_guid
, &pool
,
7013 &refcnt
) && pool
!= 0ULL && pool
== spa_guid(spa
) &&
7022 * Post a FM_EREPORT_ZFS_* event from sys/fm/fs/zfs.h. The payload will be
7023 * filled in from the spa and (optionally) the vdev. This doesn't do anything
7024 * in the userland libzpool, as we don't want consumers to misinterpret ztest
7025 * or zdb as real changes.
7028 spa_event_notify(spa_t
*spa
, vdev_t
*vd
, const char *name
)
7031 zfs_ereport_post(name
, spa
, vd
, NULL
, 0, 0);
7035 #if defined(_KERNEL) && defined(HAVE_SPL)
7036 /* state manipulation functions */
7037 EXPORT_SYMBOL(spa_open
);
7038 EXPORT_SYMBOL(spa_open_rewind
);
7039 EXPORT_SYMBOL(spa_get_stats
);
7040 EXPORT_SYMBOL(spa_create
);
7041 EXPORT_SYMBOL(spa_import_rootpool
);
7042 EXPORT_SYMBOL(spa_import
);
7043 EXPORT_SYMBOL(spa_tryimport
);
7044 EXPORT_SYMBOL(spa_destroy
);
7045 EXPORT_SYMBOL(spa_export
);
7046 EXPORT_SYMBOL(spa_reset
);
7047 EXPORT_SYMBOL(spa_async_request
);
7048 EXPORT_SYMBOL(spa_async_suspend
);
7049 EXPORT_SYMBOL(spa_async_resume
);
7050 EXPORT_SYMBOL(spa_inject_addref
);
7051 EXPORT_SYMBOL(spa_inject_delref
);
7052 EXPORT_SYMBOL(spa_scan_stat_init
);
7053 EXPORT_SYMBOL(spa_scan_get_stats
);
7055 /* device maniion */
7056 EXPORT_SYMBOL(spa_vdev_add
);
7057 EXPORT_SYMBOL(spa_vdev_attach
);
7058 EXPORT_SYMBOL(spa_vdev_detach
);
7059 EXPORT_SYMBOL(spa_vdev_remove
);
7060 EXPORT_SYMBOL(spa_vdev_setpath
);
7061 EXPORT_SYMBOL(spa_vdev_setfru
);
7062 EXPORT_SYMBOL(spa_vdev_split_mirror
);
7064 /* spare statech is global across all pools) */
7065 EXPORT_SYMBOL(spa_spare_add
);
7066 EXPORT_SYMBOL(spa_spare_remove
);
7067 EXPORT_SYMBOL(spa_spare_exists
);
7068 EXPORT_SYMBOL(spa_spare_activate
);
7070 /* L2ARC statech is global across all pools) */
7071 EXPORT_SYMBOL(spa_l2cache_add
);
7072 EXPORT_SYMBOL(spa_l2cache_remove
);
7073 EXPORT_SYMBOL(spa_l2cache_exists
);
7074 EXPORT_SYMBOL(spa_l2cache_activate
);
7075 EXPORT_SYMBOL(spa_l2cache_drop
);
7078 EXPORT_SYMBOL(spa_scan
);
7079 EXPORT_SYMBOL(spa_scan_stop
);
7082 EXPORT_SYMBOL(spa_sync
); /* only for DMU use */
7083 EXPORT_SYMBOL(spa_sync_allpools
);
7086 EXPORT_SYMBOL(spa_prop_set
);
7087 EXPORT_SYMBOL(spa_prop_get
);
7088 EXPORT_SYMBOL(spa_prop_clear_bootfs
);
7090 /* asynchronous event notification */
7091 EXPORT_SYMBOL(spa_event_notify
);
7094 #if defined(_KERNEL) && defined(HAVE_SPL)
7095 module_param(spa_load_verify_maxinflight
, int, 0644);
7096 MODULE_PARM_DESC(spa_load_verify_maxinflight
,
7097 "Max concurrent traversal I/Os while verifying pool during import -X");
7099 module_param(spa_load_verify_metadata
, int, 0644);
7100 MODULE_PARM_DESC(spa_load_verify_metadata
,
7101 "Set to traverse metadata on pool import");
7103 module_param(spa_load_verify_data
, int, 0644);
7104 MODULE_PARM_DESC(spa_load_verify_data
,
7105 "Set to traverse data on pool import");
7107 module_param(zio_taskq_batch_pct
, uint
, 0444);
7108 MODULE_PARM_DESC(zio_taskq_batch_pct
,
7109 "Percentage of CPUs to run an IO worker thread");