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.
30 * SPA: Storage Pool Allocator
32 * This file contains all the routines used when modifying on-disk SPA state.
33 * This includes opening, importing, destroying, exporting a pool, and syncing a
37 #include <sys/zfs_context.h>
38 #include <sys/fm/fs/zfs.h>
39 #include <sys/spa_impl.h>
41 #include <sys/zio_checksum.h>
43 #include <sys/dmu_tx.h>
47 #include <sys/vdev_impl.h>
48 #include <sys/vdev_disk.h>
49 #include <sys/metaslab.h>
50 #include <sys/metaslab_impl.h>
51 #include <sys/uberblock_impl.h>
54 #include <sys/dmu_traverse.h>
55 #include <sys/dmu_objset.h>
56 #include <sys/unique.h>
57 #include <sys/dsl_pool.h>
58 #include <sys/dsl_dataset.h>
59 #include <sys/dsl_dir.h>
60 #include <sys/dsl_prop.h>
61 #include <sys/dsl_synctask.h>
62 #include <sys/fs/zfs.h>
64 #include <sys/callb.h>
65 #include <sys/systeminfo.h>
66 #include <sys/spa_boot.h>
67 #include <sys/zfs_ioctl.h>
68 #include <sys/dsl_scan.h>
69 #include <sys/zfeature.h>
70 #include <sys/dsl_destroy.h>
74 #include <sys/bootprops.h>
75 #include <sys/callb.h>
76 #include <sys/cpupart.h>
78 #include <sys/sysdc.h>
83 #include "zfs_comutil.h"
86 * The interval, in seconds, at which failed configuration cache file writes
89 static int zfs_ccw_retry_interval
= 300;
91 typedef enum zti_modes
{
92 ZTI_MODE_FIXED
, /* value is # of threads (min 1) */
93 ZTI_MODE_BATCH
, /* cpu-intensive; value is ignored */
94 ZTI_MODE_NULL
, /* don't create a taskq */
98 #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) }
99 #define ZTI_PCT(n) { ZTI_MODE_ONLINE_PERCENT, (n), 1 }
100 #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 }
101 #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 }
103 #define ZTI_N(n) ZTI_P(n, 1)
104 #define ZTI_ONE ZTI_N(1)
106 typedef struct zio_taskq_info
{
107 zti_modes_t zti_mode
;
112 static const char *const zio_taskq_types
[ZIO_TASKQ_TYPES
] = {
113 "iss", "iss_h", "int", "int_h"
117 * This table defines the taskq settings for each ZFS I/O type. When
118 * initializing a pool, we use this table to create an appropriately sized
119 * taskq. Some operations are low volume and therefore have a small, static
120 * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
121 * macros. Other operations process a large amount of data; the ZTI_BATCH
122 * macro causes us to create a taskq oriented for throughput. Some operations
123 * are so high frequency and short-lived that the taskq itself can become a a
124 * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
125 * additional degree of parallelism specified by the number of threads per-
126 * taskq and the number of taskqs; when dispatching an event in this case, the
127 * particular taskq is chosen at random.
129 * The different taskq priorities are to handle the different contexts (issue
130 * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that
131 * need to be handled with minimum delay.
133 const zio_taskq_info_t zio_taskqs
[ZIO_TYPES
][ZIO_TASKQ_TYPES
] = {
134 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
135 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* NULL */
136 { ZTI_N(8), ZTI_NULL
, ZTI_P(12, 8), ZTI_NULL
}, /* READ */
137 { ZTI_BATCH
, ZTI_N(5), ZTI_P(12, 8), ZTI_N(5) }, /* WRITE */
138 { ZTI_P(12, 8), ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* FREE */
139 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* CLAIM */
140 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* IOCTL */
143 static void spa_sync_version(void *arg
, dmu_tx_t
*tx
);
144 static void spa_sync_props(void *arg
, dmu_tx_t
*tx
);
145 static boolean_t
spa_has_active_shared_spare(spa_t
*spa
);
146 static inline int spa_load_impl(spa_t
*spa
, uint64_t, nvlist_t
*config
,
147 spa_load_state_t state
, spa_import_type_t type
, boolean_t mosconfig
,
149 static void spa_vdev_resilver_done(spa_t
*spa
);
151 uint_t zio_taskq_batch_pct
= 75; /* 1 thread per cpu in pset */
152 id_t zio_taskq_psrset_bind
= PS_NONE
;
153 boolean_t zio_taskq_sysdc
= B_TRUE
; /* use SDC scheduling class */
154 uint_t zio_taskq_basedc
= 80; /* base duty cycle */
156 boolean_t spa_create_process
= B_TRUE
; /* no process ==> no sysdc */
159 * This (illegal) pool name is used when temporarily importing a spa_t in order
160 * to get the vdev stats associated with the imported devices.
162 #define TRYIMPORT_NAME "$import"
165 * ==========================================================================
166 * SPA properties routines
167 * ==========================================================================
171 * Add a (source=src, propname=propval) list to an nvlist.
174 spa_prop_add_list(nvlist_t
*nvl
, zpool_prop_t prop
, char *strval
,
175 uint64_t intval
, zprop_source_t src
)
177 const char *propname
= zpool_prop_to_name(prop
);
180 VERIFY(nvlist_alloc(&propval
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
181 VERIFY(nvlist_add_uint64(propval
, ZPROP_SOURCE
, src
) == 0);
184 VERIFY(nvlist_add_string(propval
, ZPROP_VALUE
, strval
) == 0);
186 VERIFY(nvlist_add_uint64(propval
, ZPROP_VALUE
, intval
) == 0);
188 VERIFY(nvlist_add_nvlist(nvl
, propname
, propval
) == 0);
189 nvlist_free(propval
);
193 * Get property values from the spa configuration.
196 spa_prop_get_config(spa_t
*spa
, nvlist_t
**nvp
)
198 vdev_t
*rvd
= spa
->spa_root_vdev
;
199 dsl_pool_t
*pool
= spa
->spa_dsl_pool
;
200 uint64_t size
, alloc
, cap
, version
;
201 zprop_source_t src
= ZPROP_SRC_NONE
;
202 spa_config_dirent_t
*dp
;
203 metaslab_class_t
*mc
= spa_normal_class(spa
);
205 ASSERT(MUTEX_HELD(&spa
->spa_props_lock
));
208 alloc
= metaslab_class_get_alloc(spa_normal_class(spa
));
209 size
= metaslab_class_get_space(spa_normal_class(spa
));
210 spa_prop_add_list(*nvp
, ZPOOL_PROP_NAME
, spa_name(spa
), 0, src
);
211 spa_prop_add_list(*nvp
, ZPOOL_PROP_SIZE
, NULL
, size
, src
);
212 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALLOCATED
, NULL
, alloc
, src
);
213 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREE
, NULL
,
216 spa_prop_add_list(*nvp
, ZPOOL_PROP_FRAGMENTATION
, NULL
,
217 metaslab_class_fragmentation(mc
), src
);
218 spa_prop_add_list(*nvp
, ZPOOL_PROP_EXPANDSZ
, NULL
,
219 metaslab_class_expandable_space(mc
), src
);
220 spa_prop_add_list(*nvp
, ZPOOL_PROP_READONLY
, NULL
,
221 (spa_mode(spa
) == FREAD
), src
);
223 cap
= (size
== 0) ? 0 : (alloc
* 100 / size
);
224 spa_prop_add_list(*nvp
, ZPOOL_PROP_CAPACITY
, NULL
, cap
, src
);
226 spa_prop_add_list(*nvp
, ZPOOL_PROP_DEDUPRATIO
, NULL
,
227 ddt_get_pool_dedup_ratio(spa
), src
);
229 spa_prop_add_list(*nvp
, ZPOOL_PROP_HEALTH
, NULL
,
230 rvd
->vdev_state
, src
);
232 version
= spa_version(spa
);
233 if (version
== zpool_prop_default_numeric(ZPOOL_PROP_VERSION
))
234 src
= ZPROP_SRC_DEFAULT
;
236 src
= ZPROP_SRC_LOCAL
;
237 spa_prop_add_list(*nvp
, ZPOOL_PROP_VERSION
, NULL
, version
, src
);
242 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
243 * when opening pools before this version freedir will be NULL.
245 if (pool
->dp_free_dir
!= NULL
) {
246 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
, NULL
,
247 dsl_dir_phys(pool
->dp_free_dir
)->dd_used_bytes
,
250 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
,
254 if (pool
->dp_leak_dir
!= NULL
) {
255 spa_prop_add_list(*nvp
, ZPOOL_PROP_LEAKED
, NULL
,
256 dsl_dir_phys(pool
->dp_leak_dir
)->dd_used_bytes
,
259 spa_prop_add_list(*nvp
, ZPOOL_PROP_LEAKED
,
264 spa_prop_add_list(*nvp
, ZPOOL_PROP_GUID
, NULL
, spa_guid(spa
), src
);
266 if (spa
->spa_comment
!= NULL
) {
267 spa_prop_add_list(*nvp
, ZPOOL_PROP_COMMENT
, spa
->spa_comment
,
271 if (spa
->spa_root
!= NULL
)
272 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALTROOT
, spa
->spa_root
,
275 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_BLOCKS
)) {
276 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXBLOCKSIZE
, NULL
,
277 MIN(zfs_max_recordsize
, SPA_MAXBLOCKSIZE
), ZPROP_SRC_NONE
);
279 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXBLOCKSIZE
, NULL
,
280 SPA_OLD_MAXBLOCKSIZE
, ZPROP_SRC_NONE
);
283 if ((dp
= list_head(&spa
->spa_config_list
)) != NULL
) {
284 if (dp
->scd_path
== NULL
) {
285 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
286 "none", 0, ZPROP_SRC_LOCAL
);
287 } else if (strcmp(dp
->scd_path
, spa_config_path
) != 0) {
288 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
289 dp
->scd_path
, 0, ZPROP_SRC_LOCAL
);
295 * Get zpool property values.
298 spa_prop_get(spa_t
*spa
, nvlist_t
**nvp
)
300 objset_t
*mos
= spa
->spa_meta_objset
;
305 err
= nvlist_alloc(nvp
, NV_UNIQUE_NAME
, KM_SLEEP
);
309 mutex_enter(&spa
->spa_props_lock
);
312 * Get properties from the spa config.
314 spa_prop_get_config(spa
, nvp
);
316 /* If no pool property object, no more prop to get. */
317 if (mos
== NULL
|| spa
->spa_pool_props_object
== 0) {
318 mutex_exit(&spa
->spa_props_lock
);
323 * Get properties from the MOS pool property object.
325 for (zap_cursor_init(&zc
, mos
, spa
->spa_pool_props_object
);
326 (err
= zap_cursor_retrieve(&zc
, &za
)) == 0;
327 zap_cursor_advance(&zc
)) {
330 zprop_source_t src
= ZPROP_SRC_DEFAULT
;
333 if ((prop
= zpool_name_to_prop(za
.za_name
)) == ZPROP_INVAL
)
336 switch (za
.za_integer_length
) {
338 /* integer property */
339 if (za
.za_first_integer
!=
340 zpool_prop_default_numeric(prop
))
341 src
= ZPROP_SRC_LOCAL
;
343 if (prop
== ZPOOL_PROP_BOOTFS
) {
345 dsl_dataset_t
*ds
= NULL
;
347 dp
= spa_get_dsl(spa
);
348 dsl_pool_config_enter(dp
, FTAG
);
349 if ((err
= dsl_dataset_hold_obj(dp
,
350 za
.za_first_integer
, FTAG
, &ds
))) {
351 dsl_pool_config_exit(dp
, FTAG
);
356 MAXNAMELEN
+ strlen(MOS_DIR_NAME
) + 1,
358 dsl_dataset_name(ds
, strval
);
359 dsl_dataset_rele(ds
, FTAG
);
360 dsl_pool_config_exit(dp
, FTAG
);
363 intval
= za
.za_first_integer
;
366 spa_prop_add_list(*nvp
, prop
, strval
, intval
, src
);
370 MAXNAMELEN
+ strlen(MOS_DIR_NAME
) + 1);
375 /* string property */
376 strval
= kmem_alloc(za
.za_num_integers
, KM_SLEEP
);
377 err
= zap_lookup(mos
, spa
->spa_pool_props_object
,
378 za
.za_name
, 1, za
.za_num_integers
, strval
);
380 kmem_free(strval
, za
.za_num_integers
);
383 spa_prop_add_list(*nvp
, prop
, strval
, 0, src
);
384 kmem_free(strval
, za
.za_num_integers
);
391 zap_cursor_fini(&zc
);
392 mutex_exit(&spa
->spa_props_lock
);
394 if (err
&& err
!= ENOENT
) {
404 * Validate the given pool properties nvlist and modify the list
405 * for the property values to be set.
408 spa_prop_validate(spa_t
*spa
, nvlist_t
*props
)
411 int error
= 0, reset_bootfs
= 0;
413 boolean_t has_feature
= B_FALSE
;
416 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
418 char *strval
, *slash
, *check
, *fname
;
419 const char *propname
= nvpair_name(elem
);
420 zpool_prop_t prop
= zpool_name_to_prop(propname
);
424 if (!zpool_prop_feature(propname
)) {
425 error
= SET_ERROR(EINVAL
);
430 * Sanitize the input.
432 if (nvpair_type(elem
) != DATA_TYPE_UINT64
) {
433 error
= SET_ERROR(EINVAL
);
437 if (nvpair_value_uint64(elem
, &intval
) != 0) {
438 error
= SET_ERROR(EINVAL
);
443 error
= SET_ERROR(EINVAL
);
447 fname
= strchr(propname
, '@') + 1;
448 if (zfeature_lookup_name(fname
, NULL
) != 0) {
449 error
= SET_ERROR(EINVAL
);
453 has_feature
= B_TRUE
;
456 case ZPOOL_PROP_VERSION
:
457 error
= nvpair_value_uint64(elem
, &intval
);
459 (intval
< spa_version(spa
) ||
460 intval
> SPA_VERSION_BEFORE_FEATURES
||
462 error
= SET_ERROR(EINVAL
);
465 case ZPOOL_PROP_DELEGATION
:
466 case ZPOOL_PROP_AUTOREPLACE
:
467 case ZPOOL_PROP_LISTSNAPS
:
468 case ZPOOL_PROP_AUTOEXPAND
:
469 error
= nvpair_value_uint64(elem
, &intval
);
470 if (!error
&& intval
> 1)
471 error
= SET_ERROR(EINVAL
);
474 case ZPOOL_PROP_BOOTFS
:
476 * If the pool version is less than SPA_VERSION_BOOTFS,
477 * or the pool is still being created (version == 0),
478 * the bootfs property cannot be set.
480 if (spa_version(spa
) < SPA_VERSION_BOOTFS
) {
481 error
= SET_ERROR(ENOTSUP
);
486 * Make sure the vdev config is bootable
488 if (!vdev_is_bootable(spa
->spa_root_vdev
)) {
489 error
= SET_ERROR(ENOTSUP
);
495 error
= nvpair_value_string(elem
, &strval
);
501 if (strval
== NULL
|| strval
[0] == '\0') {
502 objnum
= zpool_prop_default_numeric(
507 error
= dmu_objset_hold(strval
, FTAG
, &os
);
512 * Must be ZPL, and its property settings
513 * must be supported by GRUB (compression
514 * is not gzip, and large blocks are not used).
517 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
518 error
= SET_ERROR(ENOTSUP
);
520 dsl_prop_get_int_ds(dmu_objset_ds(os
),
521 zfs_prop_to_name(ZFS_PROP_COMPRESSION
),
523 !BOOTFS_COMPRESS_VALID(propval
)) {
524 error
= SET_ERROR(ENOTSUP
);
526 dsl_prop_get_int_ds(dmu_objset_ds(os
),
527 zfs_prop_to_name(ZFS_PROP_RECORDSIZE
),
529 propval
> SPA_OLD_MAXBLOCKSIZE
) {
530 error
= SET_ERROR(ENOTSUP
);
532 objnum
= dmu_objset_id(os
);
534 dmu_objset_rele(os
, FTAG
);
538 case ZPOOL_PROP_FAILUREMODE
:
539 error
= nvpair_value_uint64(elem
, &intval
);
540 if (!error
&& (intval
< ZIO_FAILURE_MODE_WAIT
||
541 intval
> ZIO_FAILURE_MODE_PANIC
))
542 error
= SET_ERROR(EINVAL
);
545 * This is a special case which only occurs when
546 * the pool has completely failed. This allows
547 * the user to change the in-core failmode property
548 * without syncing it out to disk (I/Os might
549 * currently be blocked). We do this by returning
550 * EIO to the caller (spa_prop_set) to trick it
551 * into thinking we encountered a property validation
554 if (!error
&& spa_suspended(spa
)) {
555 spa
->spa_failmode
= intval
;
556 error
= SET_ERROR(EIO
);
560 case ZPOOL_PROP_CACHEFILE
:
561 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
564 if (strval
[0] == '\0')
567 if (strcmp(strval
, "none") == 0)
570 if (strval
[0] != '/') {
571 error
= SET_ERROR(EINVAL
);
575 slash
= strrchr(strval
, '/');
576 ASSERT(slash
!= NULL
);
578 if (slash
[1] == '\0' || strcmp(slash
, "/.") == 0 ||
579 strcmp(slash
, "/..") == 0)
580 error
= SET_ERROR(EINVAL
);
583 case ZPOOL_PROP_COMMENT
:
584 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
586 for (check
= strval
; *check
!= '\0'; check
++) {
587 if (!isprint(*check
)) {
588 error
= SET_ERROR(EINVAL
);
593 if (strlen(strval
) > ZPROP_MAX_COMMENT
)
594 error
= SET_ERROR(E2BIG
);
597 case ZPOOL_PROP_DEDUPDITTO
:
598 if (spa_version(spa
) < SPA_VERSION_DEDUP
)
599 error
= SET_ERROR(ENOTSUP
);
601 error
= nvpair_value_uint64(elem
, &intval
);
603 intval
!= 0 && intval
< ZIO_DEDUPDITTO_MIN
)
604 error
= SET_ERROR(EINVAL
);
615 if (!error
&& reset_bootfs
) {
616 error
= nvlist_remove(props
,
617 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), DATA_TYPE_STRING
);
620 error
= nvlist_add_uint64(props
,
621 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), objnum
);
629 spa_configfile_set(spa_t
*spa
, nvlist_t
*nvp
, boolean_t need_sync
)
632 spa_config_dirent_t
*dp
;
634 if (nvlist_lookup_string(nvp
, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
),
638 dp
= kmem_alloc(sizeof (spa_config_dirent_t
),
641 if (cachefile
[0] == '\0')
642 dp
->scd_path
= spa_strdup(spa_config_path
);
643 else if (strcmp(cachefile
, "none") == 0)
646 dp
->scd_path
= spa_strdup(cachefile
);
648 list_insert_head(&spa
->spa_config_list
, dp
);
650 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
654 spa_prop_set(spa_t
*spa
, nvlist_t
*nvp
)
657 nvpair_t
*elem
= NULL
;
658 boolean_t need_sync
= B_FALSE
;
660 if ((error
= spa_prop_validate(spa
, nvp
)) != 0)
663 while ((elem
= nvlist_next_nvpair(nvp
, elem
)) != NULL
) {
664 zpool_prop_t prop
= zpool_name_to_prop(nvpair_name(elem
));
666 if (prop
== ZPOOL_PROP_CACHEFILE
||
667 prop
== ZPOOL_PROP_ALTROOT
||
668 prop
== ZPOOL_PROP_READONLY
)
671 if (prop
== ZPOOL_PROP_VERSION
|| prop
== ZPROP_INVAL
) {
674 if (prop
== ZPOOL_PROP_VERSION
) {
675 VERIFY(nvpair_value_uint64(elem
, &ver
) == 0);
677 ASSERT(zpool_prop_feature(nvpair_name(elem
)));
678 ver
= SPA_VERSION_FEATURES
;
682 /* Save time if the version is already set. */
683 if (ver
== spa_version(spa
))
687 * In addition to the pool directory object, we might
688 * create the pool properties object, the features for
689 * read object, the features for write object, or the
690 * feature descriptions object.
692 error
= dsl_sync_task(spa
->spa_name
, NULL
,
693 spa_sync_version
, &ver
,
694 6, ZFS_SPACE_CHECK_RESERVED
);
705 return (dsl_sync_task(spa
->spa_name
, NULL
, spa_sync_props
,
706 nvp
, 6, ZFS_SPACE_CHECK_RESERVED
));
713 * If the bootfs property value is dsobj, clear it.
716 spa_prop_clear_bootfs(spa_t
*spa
, uint64_t dsobj
, dmu_tx_t
*tx
)
718 if (spa
->spa_bootfs
== dsobj
&& spa
->spa_pool_props_object
!= 0) {
719 VERIFY(zap_remove(spa
->spa_meta_objset
,
720 spa
->spa_pool_props_object
,
721 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), tx
) == 0);
728 spa_change_guid_check(void *arg
, dmu_tx_t
*tx
)
730 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
731 vdev_t
*rvd
= spa
->spa_root_vdev
;
733 ASSERTV(uint64_t *newguid
= arg
);
735 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
736 vdev_state
= rvd
->vdev_state
;
737 spa_config_exit(spa
, SCL_STATE
, FTAG
);
739 if (vdev_state
!= VDEV_STATE_HEALTHY
)
740 return (SET_ERROR(ENXIO
));
742 ASSERT3U(spa_guid(spa
), !=, *newguid
);
748 spa_change_guid_sync(void *arg
, dmu_tx_t
*tx
)
750 uint64_t *newguid
= arg
;
751 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
753 vdev_t
*rvd
= spa
->spa_root_vdev
;
755 oldguid
= spa_guid(spa
);
757 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
758 rvd
->vdev_guid
= *newguid
;
759 rvd
->vdev_guid_sum
+= (*newguid
- oldguid
);
760 vdev_config_dirty(rvd
);
761 spa_config_exit(spa
, SCL_STATE
, FTAG
);
763 spa_history_log_internal(spa
, "guid change", tx
, "old=%llu new=%llu",
768 * Change the GUID for the pool. This is done so that we can later
769 * re-import a pool built from a clone of our own vdevs. We will modify
770 * the root vdev's guid, our own pool guid, and then mark all of our
771 * vdevs dirty. Note that we must make sure that all our vdevs are
772 * online when we do this, or else any vdevs that weren't present
773 * would be orphaned from our pool. We are also going to issue a
774 * sysevent to update any watchers.
777 spa_change_guid(spa_t
*spa
)
782 mutex_enter(&spa
->spa_vdev_top_lock
);
783 mutex_enter(&spa_namespace_lock
);
784 guid
= spa_generate_guid(NULL
);
786 error
= dsl_sync_task(spa
->spa_name
, spa_change_guid_check
,
787 spa_change_guid_sync
, &guid
, 5, ZFS_SPACE_CHECK_RESERVED
);
790 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
791 spa_event_notify(spa
, NULL
, FM_EREPORT_ZFS_POOL_REGUID
);
794 mutex_exit(&spa_namespace_lock
);
795 mutex_exit(&spa
->spa_vdev_top_lock
);
801 * ==========================================================================
802 * SPA state manipulation (open/create/destroy/import/export)
803 * ==========================================================================
807 spa_error_entry_compare(const void *a
, const void *b
)
809 spa_error_entry_t
*sa
= (spa_error_entry_t
*)a
;
810 spa_error_entry_t
*sb
= (spa_error_entry_t
*)b
;
813 ret
= bcmp(&sa
->se_bookmark
, &sb
->se_bookmark
,
814 sizeof (zbookmark_phys_t
));
825 * Utility function which retrieves copies of the current logs and
826 * re-initializes them in the process.
829 spa_get_errlists(spa_t
*spa
, avl_tree_t
*last
, avl_tree_t
*scrub
)
831 ASSERT(MUTEX_HELD(&spa
->spa_errlist_lock
));
833 bcopy(&spa
->spa_errlist_last
, last
, sizeof (avl_tree_t
));
834 bcopy(&spa
->spa_errlist_scrub
, scrub
, sizeof (avl_tree_t
));
836 avl_create(&spa
->spa_errlist_scrub
,
837 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
838 offsetof(spa_error_entry_t
, se_avl
));
839 avl_create(&spa
->spa_errlist_last
,
840 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
841 offsetof(spa_error_entry_t
, se_avl
));
845 spa_taskqs_init(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
847 const zio_taskq_info_t
*ztip
= &zio_taskqs
[t
][q
];
848 enum zti_modes mode
= ztip
->zti_mode
;
849 uint_t value
= ztip
->zti_value
;
850 uint_t count
= ztip
->zti_count
;
851 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
853 uint_t i
, flags
= TASKQ_DYNAMIC
;
854 boolean_t batch
= B_FALSE
;
856 if (mode
== ZTI_MODE_NULL
) {
858 tqs
->stqs_taskq
= NULL
;
862 ASSERT3U(count
, >, 0);
864 tqs
->stqs_count
= count
;
865 tqs
->stqs_taskq
= kmem_alloc(count
* sizeof (taskq_t
*), KM_SLEEP
);
869 ASSERT3U(value
, >=, 1);
870 value
= MAX(value
, 1);
875 flags
|= TASKQ_THREADS_CPU_PCT
;
876 value
= MIN(zio_taskq_batch_pct
, 100);
880 panic("unrecognized mode for %s_%s taskq (%u:%u) in "
882 zio_type_name
[t
], zio_taskq_types
[q
], mode
, value
);
886 for (i
= 0; i
< count
; i
++) {
890 (void) snprintf(name
, sizeof (name
), "%s_%s_%u",
891 zio_type_name
[t
], zio_taskq_types
[q
], i
);
893 (void) snprintf(name
, sizeof (name
), "%s_%s",
894 zio_type_name
[t
], zio_taskq_types
[q
]);
897 if (zio_taskq_sysdc
&& spa
->spa_proc
!= &p0
) {
899 flags
|= TASKQ_DC_BATCH
;
901 tq
= taskq_create_sysdc(name
, value
, 50, INT_MAX
,
902 spa
->spa_proc
, zio_taskq_basedc
, flags
);
904 pri_t pri
= maxclsyspri
;
906 * The write issue taskq can be extremely CPU
907 * intensive. Run it at slightly less important
908 * priority than the other taskqs. Under Linux this
909 * means incrementing the priority value on platforms
910 * like illumos it should be decremented.
912 if (t
== ZIO_TYPE_WRITE
&& q
== ZIO_TASKQ_ISSUE
)
915 tq
= taskq_create_proc(name
, value
, pri
, 50,
916 INT_MAX
, spa
->spa_proc
, flags
);
919 tqs
->stqs_taskq
[i
] = tq
;
924 spa_taskqs_fini(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
926 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
929 if (tqs
->stqs_taskq
== NULL
) {
930 ASSERT3U(tqs
->stqs_count
, ==, 0);
934 for (i
= 0; i
< tqs
->stqs_count
; i
++) {
935 ASSERT3P(tqs
->stqs_taskq
[i
], !=, NULL
);
936 taskq_destroy(tqs
->stqs_taskq
[i
]);
939 kmem_free(tqs
->stqs_taskq
, tqs
->stqs_count
* sizeof (taskq_t
*));
940 tqs
->stqs_taskq
= NULL
;
944 * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority.
945 * Note that a type may have multiple discrete taskqs to avoid lock contention
946 * on the taskq itself. In that case we choose which taskq at random by using
947 * the low bits of gethrtime().
950 spa_taskq_dispatch_ent(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
951 task_func_t
*func
, void *arg
, uint_t flags
, taskq_ent_t
*ent
)
953 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
956 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
957 ASSERT3U(tqs
->stqs_count
, !=, 0);
959 if (tqs
->stqs_count
== 1) {
960 tq
= tqs
->stqs_taskq
[0];
962 tq
= tqs
->stqs_taskq
[((uint64_t)gethrtime()) % tqs
->stqs_count
];
965 taskq_dispatch_ent(tq
, func
, arg
, flags
, ent
);
969 * Same as spa_taskq_dispatch_ent() but block on the task until completion.
972 spa_taskq_dispatch_sync(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
973 task_func_t
*func
, void *arg
, uint_t flags
)
975 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
979 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
980 ASSERT3U(tqs
->stqs_count
, !=, 0);
982 if (tqs
->stqs_count
== 1) {
983 tq
= tqs
->stqs_taskq
[0];
985 tq
= tqs
->stqs_taskq
[((uint64_t)gethrtime()) % tqs
->stqs_count
];
988 id
= taskq_dispatch(tq
, func
, arg
, flags
);
990 taskq_wait_id(tq
, id
);
994 spa_create_zio_taskqs(spa_t
*spa
)
998 for (t
= 0; t
< ZIO_TYPES
; t
++) {
999 for (q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1000 spa_taskqs_init(spa
, t
, q
);
1005 #if defined(_KERNEL) && defined(HAVE_SPA_THREAD)
1007 spa_thread(void *arg
)
1009 callb_cpr_t cprinfo
;
1012 user_t
*pu
= PTOU(curproc
);
1014 CALLB_CPR_INIT(&cprinfo
, &spa
->spa_proc_lock
, callb_generic_cpr
,
1017 ASSERT(curproc
!= &p0
);
1018 (void) snprintf(pu
->u_psargs
, sizeof (pu
->u_psargs
),
1019 "zpool-%s", spa
->spa_name
);
1020 (void) strlcpy(pu
->u_comm
, pu
->u_psargs
, sizeof (pu
->u_comm
));
1022 /* bind this thread to the requested psrset */
1023 if (zio_taskq_psrset_bind
!= PS_NONE
) {
1025 mutex_enter(&cpu_lock
);
1026 mutex_enter(&pidlock
);
1027 mutex_enter(&curproc
->p_lock
);
1029 if (cpupart_bind_thread(curthread
, zio_taskq_psrset_bind
,
1030 0, NULL
, NULL
) == 0) {
1031 curthread
->t_bind_pset
= zio_taskq_psrset_bind
;
1034 "Couldn't bind process for zfs pool \"%s\" to "
1035 "pset %d\n", spa
->spa_name
, zio_taskq_psrset_bind
);
1038 mutex_exit(&curproc
->p_lock
);
1039 mutex_exit(&pidlock
);
1040 mutex_exit(&cpu_lock
);
1044 if (zio_taskq_sysdc
) {
1045 sysdc_thread_enter(curthread
, 100, 0);
1048 spa
->spa_proc
= curproc
;
1049 spa
->spa_did
= curthread
->t_did
;
1051 spa_create_zio_taskqs(spa
);
1053 mutex_enter(&spa
->spa_proc_lock
);
1054 ASSERT(spa
->spa_proc_state
== SPA_PROC_CREATED
);
1056 spa
->spa_proc_state
= SPA_PROC_ACTIVE
;
1057 cv_broadcast(&spa
->spa_proc_cv
);
1059 CALLB_CPR_SAFE_BEGIN(&cprinfo
);
1060 while (spa
->spa_proc_state
== SPA_PROC_ACTIVE
)
1061 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1062 CALLB_CPR_SAFE_END(&cprinfo
, &spa
->spa_proc_lock
);
1064 ASSERT(spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
);
1065 spa
->spa_proc_state
= SPA_PROC_GONE
;
1066 spa
->spa_proc
= &p0
;
1067 cv_broadcast(&spa
->spa_proc_cv
);
1068 CALLB_CPR_EXIT(&cprinfo
); /* drops spa_proc_lock */
1070 mutex_enter(&curproc
->p_lock
);
1076 * Activate an uninitialized pool.
1079 spa_activate(spa_t
*spa
, int mode
)
1081 ASSERT(spa
->spa_state
== POOL_STATE_UNINITIALIZED
);
1083 spa
->spa_state
= POOL_STATE_ACTIVE
;
1084 spa
->spa_mode
= mode
;
1086 spa
->spa_normal_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
1087 spa
->spa_log_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
1089 /* Try to create a covering process */
1090 mutex_enter(&spa
->spa_proc_lock
);
1091 ASSERT(spa
->spa_proc_state
== SPA_PROC_NONE
);
1092 ASSERT(spa
->spa_proc
== &p0
);
1095 #ifdef HAVE_SPA_THREAD
1096 /* Only create a process if we're going to be around a while. */
1097 if (spa_create_process
&& strcmp(spa
->spa_name
, TRYIMPORT_NAME
) != 0) {
1098 if (newproc(spa_thread
, (caddr_t
)spa
, syscid
, maxclsyspri
,
1100 spa
->spa_proc_state
= SPA_PROC_CREATED
;
1101 while (spa
->spa_proc_state
== SPA_PROC_CREATED
) {
1102 cv_wait(&spa
->spa_proc_cv
,
1103 &spa
->spa_proc_lock
);
1105 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1106 ASSERT(spa
->spa_proc
!= &p0
);
1107 ASSERT(spa
->spa_did
!= 0);
1111 "Couldn't create process for zfs pool \"%s\"\n",
1116 #endif /* HAVE_SPA_THREAD */
1117 mutex_exit(&spa
->spa_proc_lock
);
1119 /* If we didn't create a process, we need to create our taskqs. */
1120 if (spa
->spa_proc
== &p0
) {
1121 spa_create_zio_taskqs(spa
);
1124 list_create(&spa
->spa_config_dirty_list
, sizeof (vdev_t
),
1125 offsetof(vdev_t
, vdev_config_dirty_node
));
1126 list_create(&spa
->spa_evicting_os_list
, sizeof (objset_t
),
1127 offsetof(objset_t
, os_evicting_node
));
1128 list_create(&spa
->spa_state_dirty_list
, sizeof (vdev_t
),
1129 offsetof(vdev_t
, vdev_state_dirty_node
));
1131 txg_list_create(&spa
->spa_vdev_txg_list
,
1132 offsetof(struct vdev
, vdev_txg_node
));
1134 avl_create(&spa
->spa_errlist_scrub
,
1135 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1136 offsetof(spa_error_entry_t
, se_avl
));
1137 avl_create(&spa
->spa_errlist_last
,
1138 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1139 offsetof(spa_error_entry_t
, se_avl
));
1143 * Opposite of spa_activate().
1146 spa_deactivate(spa_t
*spa
)
1150 ASSERT(spa
->spa_sync_on
== B_FALSE
);
1151 ASSERT(spa
->spa_dsl_pool
== NULL
);
1152 ASSERT(spa
->spa_root_vdev
== NULL
);
1153 ASSERT(spa
->spa_async_zio_root
== NULL
);
1154 ASSERT(spa
->spa_state
!= POOL_STATE_UNINITIALIZED
);
1156 spa_evicting_os_wait(spa
);
1158 txg_list_destroy(&spa
->spa_vdev_txg_list
);
1160 list_destroy(&spa
->spa_config_dirty_list
);
1161 list_destroy(&spa
->spa_evicting_os_list
);
1162 list_destroy(&spa
->spa_state_dirty_list
);
1164 taskq_cancel_id(system_taskq
, spa
->spa_deadman_tqid
);
1166 for (t
= 0; t
< ZIO_TYPES
; t
++) {
1167 for (q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1168 spa_taskqs_fini(spa
, t
, q
);
1172 metaslab_class_destroy(spa
->spa_normal_class
);
1173 spa
->spa_normal_class
= NULL
;
1175 metaslab_class_destroy(spa
->spa_log_class
);
1176 spa
->spa_log_class
= NULL
;
1179 * If this was part of an import or the open otherwise failed, we may
1180 * still have errors left in the queues. Empty them just in case.
1182 spa_errlog_drain(spa
);
1184 avl_destroy(&spa
->spa_errlist_scrub
);
1185 avl_destroy(&spa
->spa_errlist_last
);
1187 spa
->spa_state
= POOL_STATE_UNINITIALIZED
;
1189 mutex_enter(&spa
->spa_proc_lock
);
1190 if (spa
->spa_proc_state
!= SPA_PROC_NONE
) {
1191 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1192 spa
->spa_proc_state
= SPA_PROC_DEACTIVATE
;
1193 cv_broadcast(&spa
->spa_proc_cv
);
1194 while (spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
) {
1195 ASSERT(spa
->spa_proc
!= &p0
);
1196 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1198 ASSERT(spa
->spa_proc_state
== SPA_PROC_GONE
);
1199 spa
->spa_proc_state
= SPA_PROC_NONE
;
1201 ASSERT(spa
->spa_proc
== &p0
);
1202 mutex_exit(&spa
->spa_proc_lock
);
1205 * We want to make sure spa_thread() has actually exited the ZFS
1206 * module, so that the module can't be unloaded out from underneath
1209 if (spa
->spa_did
!= 0) {
1210 thread_join(spa
->spa_did
);
1216 * Verify a pool configuration, and construct the vdev tree appropriately. This
1217 * will create all the necessary vdevs in the appropriate layout, with each vdev
1218 * in the CLOSED state. This will prep the pool before open/creation/import.
1219 * All vdev validation is done by the vdev_alloc() routine.
1222 spa_config_parse(spa_t
*spa
, vdev_t
**vdp
, nvlist_t
*nv
, vdev_t
*parent
,
1223 uint_t id
, int atype
)
1230 if ((error
= vdev_alloc(spa
, vdp
, nv
, parent
, id
, atype
)) != 0)
1233 if ((*vdp
)->vdev_ops
->vdev_op_leaf
)
1236 error
= nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1239 if (error
== ENOENT
)
1245 return (SET_ERROR(EINVAL
));
1248 for (c
= 0; c
< children
; c
++) {
1250 if ((error
= spa_config_parse(spa
, &vd
, child
[c
], *vdp
, c
,
1258 ASSERT(*vdp
!= NULL
);
1264 * Opposite of spa_load().
1267 spa_unload(spa_t
*spa
)
1271 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1276 spa_async_suspend(spa
);
1281 if (spa
->spa_sync_on
) {
1282 txg_sync_stop(spa
->spa_dsl_pool
);
1283 spa
->spa_sync_on
= B_FALSE
;
1287 * Wait for any outstanding async I/O to complete.
1289 if (spa
->spa_async_zio_root
!= NULL
) {
1290 for (i
= 0; i
< max_ncpus
; i
++)
1291 (void) zio_wait(spa
->spa_async_zio_root
[i
]);
1292 kmem_free(spa
->spa_async_zio_root
, max_ncpus
* sizeof (void *));
1293 spa
->spa_async_zio_root
= NULL
;
1296 bpobj_close(&spa
->spa_deferred_bpobj
);
1298 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1303 if (spa
->spa_root_vdev
)
1304 vdev_free(spa
->spa_root_vdev
);
1305 ASSERT(spa
->spa_root_vdev
== NULL
);
1308 * Close the dsl pool.
1310 if (spa
->spa_dsl_pool
) {
1311 dsl_pool_close(spa
->spa_dsl_pool
);
1312 spa
->spa_dsl_pool
= NULL
;
1313 spa
->spa_meta_objset
= NULL
;
1320 * Drop and purge level 2 cache
1322 spa_l2cache_drop(spa
);
1324 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1325 vdev_free(spa
->spa_spares
.sav_vdevs
[i
]);
1326 if (spa
->spa_spares
.sav_vdevs
) {
1327 kmem_free(spa
->spa_spares
.sav_vdevs
,
1328 spa
->spa_spares
.sav_count
* sizeof (void *));
1329 spa
->spa_spares
.sav_vdevs
= NULL
;
1331 if (spa
->spa_spares
.sav_config
) {
1332 nvlist_free(spa
->spa_spares
.sav_config
);
1333 spa
->spa_spares
.sav_config
= NULL
;
1335 spa
->spa_spares
.sav_count
= 0;
1337 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
1338 vdev_clear_stats(spa
->spa_l2cache
.sav_vdevs
[i
]);
1339 vdev_free(spa
->spa_l2cache
.sav_vdevs
[i
]);
1341 if (spa
->spa_l2cache
.sav_vdevs
) {
1342 kmem_free(spa
->spa_l2cache
.sav_vdevs
,
1343 spa
->spa_l2cache
.sav_count
* sizeof (void *));
1344 spa
->spa_l2cache
.sav_vdevs
= NULL
;
1346 if (spa
->spa_l2cache
.sav_config
) {
1347 nvlist_free(spa
->spa_l2cache
.sav_config
);
1348 spa
->spa_l2cache
.sav_config
= NULL
;
1350 spa
->spa_l2cache
.sav_count
= 0;
1352 spa
->spa_async_suspended
= 0;
1354 if (spa
->spa_comment
!= NULL
) {
1355 spa_strfree(spa
->spa_comment
);
1356 spa
->spa_comment
= NULL
;
1359 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1363 * Load (or re-load) the current list of vdevs describing the active spares for
1364 * this pool. When this is called, we have some form of basic information in
1365 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1366 * then re-generate a more complete list including status information.
1369 spa_load_spares(spa_t
*spa
)
1376 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1379 * First, close and free any existing spare vdevs.
1381 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1382 vd
= spa
->spa_spares
.sav_vdevs
[i
];
1384 /* Undo the call to spa_activate() below */
1385 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1386 B_FALSE
)) != NULL
&& tvd
->vdev_isspare
)
1387 spa_spare_remove(tvd
);
1392 if (spa
->spa_spares
.sav_vdevs
)
1393 kmem_free(spa
->spa_spares
.sav_vdevs
,
1394 spa
->spa_spares
.sav_count
* sizeof (void *));
1396 if (spa
->spa_spares
.sav_config
== NULL
)
1399 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
1400 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
1402 spa
->spa_spares
.sav_count
= (int)nspares
;
1403 spa
->spa_spares
.sav_vdevs
= NULL
;
1409 * Construct the array of vdevs, opening them to get status in the
1410 * process. For each spare, there is potentially two different vdev_t
1411 * structures associated with it: one in the list of spares (used only
1412 * for basic validation purposes) and one in the active vdev
1413 * configuration (if it's spared in). During this phase we open and
1414 * validate each vdev on the spare list. If the vdev also exists in the
1415 * active configuration, then we also mark this vdev as an active spare.
1417 spa
->spa_spares
.sav_vdevs
= kmem_zalloc(nspares
* sizeof (void *),
1419 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1420 VERIFY(spa_config_parse(spa
, &vd
, spares
[i
], NULL
, 0,
1421 VDEV_ALLOC_SPARE
) == 0);
1424 spa
->spa_spares
.sav_vdevs
[i
] = vd
;
1426 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1427 B_FALSE
)) != NULL
) {
1428 if (!tvd
->vdev_isspare
)
1432 * We only mark the spare active if we were successfully
1433 * able to load the vdev. Otherwise, importing a pool
1434 * with a bad active spare would result in strange
1435 * behavior, because multiple pool would think the spare
1436 * is actively in use.
1438 * There is a vulnerability here to an equally bizarre
1439 * circumstance, where a dead active spare is later
1440 * brought back to life (onlined or otherwise). Given
1441 * the rarity of this scenario, and the extra complexity
1442 * it adds, we ignore the possibility.
1444 if (!vdev_is_dead(tvd
))
1445 spa_spare_activate(tvd
);
1449 vd
->vdev_aux
= &spa
->spa_spares
;
1451 if (vdev_open(vd
) != 0)
1454 if (vdev_validate_aux(vd
) == 0)
1459 * Recompute the stashed list of spares, with status information
1462 VERIFY(nvlist_remove(spa
->spa_spares
.sav_config
, ZPOOL_CONFIG_SPARES
,
1463 DATA_TYPE_NVLIST_ARRAY
) == 0);
1465 spares
= kmem_alloc(spa
->spa_spares
.sav_count
* sizeof (void *),
1467 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1468 spares
[i
] = vdev_config_generate(spa
,
1469 spa
->spa_spares
.sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_SPARE
);
1470 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
1471 ZPOOL_CONFIG_SPARES
, spares
, spa
->spa_spares
.sav_count
) == 0);
1472 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1473 nvlist_free(spares
[i
]);
1474 kmem_free(spares
, spa
->spa_spares
.sav_count
* sizeof (void *));
1478 * Load (or re-load) the current list of vdevs describing the active l2cache for
1479 * this pool. When this is called, we have some form of basic information in
1480 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1481 * then re-generate a more complete list including status information.
1482 * Devices which are already active have their details maintained, and are
1486 spa_load_l2cache(spa_t
*spa
)
1490 int i
, j
, oldnvdevs
;
1492 vdev_t
*vd
, **oldvdevs
, **newvdevs
;
1493 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
1495 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1497 if (sav
->sav_config
!= NULL
) {
1498 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
,
1499 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
1500 newvdevs
= kmem_alloc(nl2cache
* sizeof (void *), KM_SLEEP
);
1506 oldvdevs
= sav
->sav_vdevs
;
1507 oldnvdevs
= sav
->sav_count
;
1508 sav
->sav_vdevs
= NULL
;
1512 * Process new nvlist of vdevs.
1514 for (i
= 0; i
< nl2cache
; i
++) {
1515 VERIFY(nvlist_lookup_uint64(l2cache
[i
], ZPOOL_CONFIG_GUID
,
1519 for (j
= 0; j
< oldnvdevs
; j
++) {
1521 if (vd
!= NULL
&& guid
== vd
->vdev_guid
) {
1523 * Retain previous vdev for add/remove ops.
1531 if (newvdevs
[i
] == NULL
) {
1535 VERIFY(spa_config_parse(spa
, &vd
, l2cache
[i
], NULL
, 0,
1536 VDEV_ALLOC_L2CACHE
) == 0);
1541 * Commit this vdev as an l2cache device,
1542 * even if it fails to open.
1544 spa_l2cache_add(vd
);
1549 spa_l2cache_activate(vd
);
1551 if (vdev_open(vd
) != 0)
1554 (void) vdev_validate_aux(vd
);
1556 if (!vdev_is_dead(vd
))
1557 l2arc_add_vdev(spa
, vd
);
1562 * Purge vdevs that were dropped
1564 for (i
= 0; i
< oldnvdevs
; i
++) {
1569 ASSERT(vd
->vdev_isl2cache
);
1571 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
1572 pool
!= 0ULL && l2arc_vdev_present(vd
))
1573 l2arc_remove_vdev(vd
);
1574 vdev_clear_stats(vd
);
1580 kmem_free(oldvdevs
, oldnvdevs
* sizeof (void *));
1582 if (sav
->sav_config
== NULL
)
1585 sav
->sav_vdevs
= newvdevs
;
1586 sav
->sav_count
= (int)nl2cache
;
1589 * Recompute the stashed list of l2cache devices, with status
1590 * information this time.
1592 VERIFY(nvlist_remove(sav
->sav_config
, ZPOOL_CONFIG_L2CACHE
,
1593 DATA_TYPE_NVLIST_ARRAY
) == 0);
1595 l2cache
= kmem_alloc(sav
->sav_count
* sizeof (void *), KM_SLEEP
);
1596 for (i
= 0; i
< sav
->sav_count
; i
++)
1597 l2cache
[i
] = vdev_config_generate(spa
,
1598 sav
->sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_L2CACHE
);
1599 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
,
1600 ZPOOL_CONFIG_L2CACHE
, l2cache
, sav
->sav_count
) == 0);
1602 for (i
= 0; i
< sav
->sav_count
; i
++)
1603 nvlist_free(l2cache
[i
]);
1605 kmem_free(l2cache
, sav
->sav_count
* sizeof (void *));
1609 load_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
**value
)
1612 char *packed
= NULL
;
1617 error
= dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
);
1621 nvsize
= *(uint64_t *)db
->db_data
;
1622 dmu_buf_rele(db
, FTAG
);
1624 packed
= vmem_alloc(nvsize
, KM_SLEEP
);
1625 error
= dmu_read(spa
->spa_meta_objset
, obj
, 0, nvsize
, packed
,
1628 error
= nvlist_unpack(packed
, nvsize
, value
, 0);
1629 vmem_free(packed
, nvsize
);
1635 * Checks to see if the given vdev could not be opened, in which case we post a
1636 * sysevent to notify the autoreplace code that the device has been removed.
1639 spa_check_removed(vdev_t
*vd
)
1643 for (c
= 0; c
< vd
->vdev_children
; c
++)
1644 spa_check_removed(vd
->vdev_child
[c
]);
1646 if (vd
->vdev_ops
->vdev_op_leaf
&& vdev_is_dead(vd
) &&
1648 zfs_ereport_post(FM_EREPORT_RESOURCE_AUTOREPLACE
,
1649 vd
->vdev_spa
, vd
, NULL
, 0, 0);
1650 spa_event_notify(vd
->vdev_spa
, vd
, FM_EREPORT_ZFS_DEVICE_CHECK
);
1655 * Validate the current config against the MOS config
1658 spa_config_valid(spa_t
*spa
, nvlist_t
*config
)
1660 vdev_t
*mrvd
, *rvd
= spa
->spa_root_vdev
;
1664 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nv
) == 0);
1666 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1667 VERIFY(spa_config_parse(spa
, &mrvd
, nv
, NULL
, 0, VDEV_ALLOC_LOAD
) == 0);
1669 ASSERT3U(rvd
->vdev_children
, ==, mrvd
->vdev_children
);
1672 * If we're doing a normal import, then build up any additional
1673 * diagnostic information about missing devices in this config.
1674 * We'll pass this up to the user for further processing.
1676 if (!(spa
->spa_import_flags
& ZFS_IMPORT_MISSING_LOG
)) {
1677 nvlist_t
**child
, *nv
;
1680 child
= kmem_alloc(rvd
->vdev_children
* sizeof (nvlist_t
**),
1682 VERIFY(nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
1684 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1685 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1686 vdev_t
*mtvd
= mrvd
->vdev_child
[c
];
1688 if (tvd
->vdev_ops
== &vdev_missing_ops
&&
1689 mtvd
->vdev_ops
!= &vdev_missing_ops
&&
1691 child
[idx
++] = vdev_config_generate(spa
, mtvd
,
1696 VERIFY(nvlist_add_nvlist_array(nv
,
1697 ZPOOL_CONFIG_CHILDREN
, child
, idx
) == 0);
1698 VERIFY(nvlist_add_nvlist(spa
->spa_load_info
,
1699 ZPOOL_CONFIG_MISSING_DEVICES
, nv
) == 0);
1701 for (i
= 0; i
< idx
; i
++)
1702 nvlist_free(child
[i
]);
1705 kmem_free(child
, rvd
->vdev_children
* sizeof (char **));
1709 * Compare the root vdev tree with the information we have
1710 * from the MOS config (mrvd). Check each top-level vdev
1711 * with the corresponding MOS config top-level (mtvd).
1713 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1714 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1715 vdev_t
*mtvd
= mrvd
->vdev_child
[c
];
1718 * Resolve any "missing" vdevs in the current configuration.
1719 * If we find that the MOS config has more accurate information
1720 * about the top-level vdev then use that vdev instead.
1722 if (tvd
->vdev_ops
== &vdev_missing_ops
&&
1723 mtvd
->vdev_ops
!= &vdev_missing_ops
) {
1725 if (!(spa
->spa_import_flags
& ZFS_IMPORT_MISSING_LOG
))
1729 * Device specific actions.
1731 if (mtvd
->vdev_islog
) {
1732 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
1735 * XXX - once we have 'readonly' pool
1736 * support we should be able to handle
1737 * missing data devices by transitioning
1738 * the pool to readonly.
1744 * Swap the missing vdev with the data we were
1745 * able to obtain from the MOS config.
1747 vdev_remove_child(rvd
, tvd
);
1748 vdev_remove_child(mrvd
, mtvd
);
1750 vdev_add_child(rvd
, mtvd
);
1751 vdev_add_child(mrvd
, tvd
);
1753 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1755 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1758 } else if (mtvd
->vdev_islog
) {
1760 * Load the slog device's state from the MOS config
1761 * since it's possible that the label does not
1762 * contain the most up-to-date information.
1764 vdev_load_log_state(tvd
, mtvd
);
1769 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1772 * Ensure we were able to validate the config.
1774 return (rvd
->vdev_guid_sum
== spa
->spa_uberblock
.ub_guid_sum
);
1778 * Check for missing log devices
1781 spa_check_logs(spa_t
*spa
)
1783 boolean_t rv
= B_FALSE
;
1784 dsl_pool_t
*dp
= spa_get_dsl(spa
);
1786 switch (spa
->spa_log_state
) {
1789 case SPA_LOG_MISSING
:
1790 /* need to recheck in case slog has been restored */
1791 case SPA_LOG_UNKNOWN
:
1792 rv
= (dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
1793 zil_check_log_chain
, NULL
, DS_FIND_CHILDREN
) != 0);
1795 spa_set_log_state(spa
, SPA_LOG_MISSING
);
1802 spa_passivate_log(spa_t
*spa
)
1804 vdev_t
*rvd
= spa
->spa_root_vdev
;
1805 boolean_t slog_found
= B_FALSE
;
1808 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
1810 if (!spa_has_slogs(spa
))
1813 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1814 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1815 metaslab_group_t
*mg
= tvd
->vdev_mg
;
1817 if (tvd
->vdev_islog
) {
1818 metaslab_group_passivate(mg
);
1819 slog_found
= B_TRUE
;
1823 return (slog_found
);
1827 spa_activate_log(spa_t
*spa
)
1829 vdev_t
*rvd
= spa
->spa_root_vdev
;
1832 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
1834 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1835 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1836 metaslab_group_t
*mg
= tvd
->vdev_mg
;
1838 if (tvd
->vdev_islog
)
1839 metaslab_group_activate(mg
);
1844 spa_offline_log(spa_t
*spa
)
1848 error
= dmu_objset_find(spa_name(spa
), zil_vdev_offline
,
1849 NULL
, DS_FIND_CHILDREN
);
1852 * We successfully offlined the log device, sync out the
1853 * current txg so that the "stubby" block can be removed
1856 txg_wait_synced(spa
->spa_dsl_pool
, 0);
1862 spa_aux_check_removed(spa_aux_vdev_t
*sav
)
1866 for (i
= 0; i
< sav
->sav_count
; i
++)
1867 spa_check_removed(sav
->sav_vdevs
[i
]);
1871 spa_claim_notify(zio_t
*zio
)
1873 spa_t
*spa
= zio
->io_spa
;
1878 mutex_enter(&spa
->spa_props_lock
); /* any mutex will do */
1879 if (spa
->spa_claim_max_txg
< zio
->io_bp
->blk_birth
)
1880 spa
->spa_claim_max_txg
= zio
->io_bp
->blk_birth
;
1881 mutex_exit(&spa
->spa_props_lock
);
1884 typedef struct spa_load_error
{
1885 uint64_t sle_meta_count
;
1886 uint64_t sle_data_count
;
1890 spa_load_verify_done(zio_t
*zio
)
1892 blkptr_t
*bp
= zio
->io_bp
;
1893 spa_load_error_t
*sle
= zio
->io_private
;
1894 dmu_object_type_t type
= BP_GET_TYPE(bp
);
1895 int error
= zio
->io_error
;
1896 spa_t
*spa
= zio
->io_spa
;
1899 if ((BP_GET_LEVEL(bp
) != 0 || DMU_OT_IS_METADATA(type
)) &&
1900 type
!= DMU_OT_INTENT_LOG
)
1901 atomic_inc_64(&sle
->sle_meta_count
);
1903 atomic_inc_64(&sle
->sle_data_count
);
1905 zio_data_buf_free(zio
->io_data
, zio
->io_size
);
1907 mutex_enter(&spa
->spa_scrub_lock
);
1908 spa
->spa_scrub_inflight
--;
1909 cv_broadcast(&spa
->spa_scrub_io_cv
);
1910 mutex_exit(&spa
->spa_scrub_lock
);
1914 * Maximum number of concurrent scrub i/os to create while verifying
1915 * a pool while importing it.
1917 int spa_load_verify_maxinflight
= 10000;
1918 int spa_load_verify_metadata
= B_TRUE
;
1919 int spa_load_verify_data
= B_TRUE
;
1923 spa_load_verify_cb(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
1924 const zbookmark_phys_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
1930 if (bp
== NULL
|| BP_IS_HOLE(bp
) || BP_IS_EMBEDDED(bp
))
1933 * Note: normally this routine will not be called if
1934 * spa_load_verify_metadata is not set. However, it may be useful
1935 * to manually set the flag after the traversal has begun.
1937 if (!spa_load_verify_metadata
)
1939 if (BP_GET_BUFC_TYPE(bp
) == ARC_BUFC_DATA
&& !spa_load_verify_data
)
1943 size
= BP_GET_PSIZE(bp
);
1944 data
= zio_data_buf_alloc(size
);
1946 mutex_enter(&spa
->spa_scrub_lock
);
1947 while (spa
->spa_scrub_inflight
>= spa_load_verify_maxinflight
)
1948 cv_wait(&spa
->spa_scrub_io_cv
, &spa
->spa_scrub_lock
);
1949 spa
->spa_scrub_inflight
++;
1950 mutex_exit(&spa
->spa_scrub_lock
);
1952 zio_nowait(zio_read(rio
, spa
, bp
, data
, size
,
1953 spa_load_verify_done
, rio
->io_private
, ZIO_PRIORITY_SCRUB
,
1954 ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_CANFAIL
|
1955 ZIO_FLAG_SCRUB
| ZIO_FLAG_RAW
, zb
));
1960 spa_load_verify(spa_t
*spa
)
1963 spa_load_error_t sle
= { 0 };
1964 zpool_rewind_policy_t policy
;
1965 boolean_t verify_ok
= B_FALSE
;
1968 zpool_get_rewind_policy(spa
->spa_config
, &policy
);
1970 if (policy
.zrp_request
& ZPOOL_NEVER_REWIND
)
1973 rio
= zio_root(spa
, NULL
, &sle
,
1974 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
);
1976 if (spa_load_verify_metadata
) {
1977 error
= traverse_pool(spa
, spa
->spa_verify_min_txg
,
1978 TRAVERSE_PRE
| TRAVERSE_PREFETCH_METADATA
,
1979 spa_load_verify_cb
, rio
);
1982 (void) zio_wait(rio
);
1984 spa
->spa_load_meta_errors
= sle
.sle_meta_count
;
1985 spa
->spa_load_data_errors
= sle
.sle_data_count
;
1987 if (!error
&& sle
.sle_meta_count
<= policy
.zrp_maxmeta
&&
1988 sle
.sle_data_count
<= policy
.zrp_maxdata
) {
1992 spa
->spa_load_txg
= spa
->spa_uberblock
.ub_txg
;
1993 spa
->spa_load_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
1995 loss
= spa
->spa_last_ubsync_txg_ts
- spa
->spa_load_txg_ts
;
1996 VERIFY(nvlist_add_uint64(spa
->spa_load_info
,
1997 ZPOOL_CONFIG_LOAD_TIME
, spa
->spa_load_txg_ts
) == 0);
1998 VERIFY(nvlist_add_int64(spa
->spa_load_info
,
1999 ZPOOL_CONFIG_REWIND_TIME
, loss
) == 0);
2000 VERIFY(nvlist_add_uint64(spa
->spa_load_info
,
2001 ZPOOL_CONFIG_LOAD_DATA_ERRORS
, sle
.sle_data_count
) == 0);
2003 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
;
2007 if (error
!= ENXIO
&& error
!= EIO
)
2008 error
= SET_ERROR(EIO
);
2012 return (verify_ok
? 0 : EIO
);
2016 * Find a value in the pool props object.
2019 spa_prop_find(spa_t
*spa
, zpool_prop_t prop
, uint64_t *val
)
2021 (void) zap_lookup(spa
->spa_meta_objset
, spa
->spa_pool_props_object
,
2022 zpool_prop_to_name(prop
), sizeof (uint64_t), 1, val
);
2026 * Find a value in the pool directory object.
2029 spa_dir_prop(spa_t
*spa
, const char *name
, uint64_t *val
)
2031 return (zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
2032 name
, sizeof (uint64_t), 1, val
));
2036 spa_vdev_err(vdev_t
*vdev
, vdev_aux_t aux
, int err
)
2038 vdev_set_state(vdev
, B_TRUE
, VDEV_STATE_CANT_OPEN
, aux
);
2043 * Fix up config after a partly-completed split. This is done with the
2044 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
2045 * pool have that entry in their config, but only the splitting one contains
2046 * a list of all the guids of the vdevs that are being split off.
2048 * This function determines what to do with that list: either rejoin
2049 * all the disks to the pool, or complete the splitting process. To attempt
2050 * the rejoin, each disk that is offlined is marked online again, and
2051 * we do a reopen() call. If the vdev label for every disk that was
2052 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
2053 * then we call vdev_split() on each disk, and complete the split.
2055 * Otherwise we leave the config alone, with all the vdevs in place in
2056 * the original pool.
2059 spa_try_repair(spa_t
*spa
, nvlist_t
*config
)
2066 boolean_t attempt_reopen
;
2068 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
, &nvl
) != 0)
2071 /* check that the config is complete */
2072 if (nvlist_lookup_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
2073 &glist
, &gcount
) != 0)
2076 vd
= kmem_zalloc(gcount
* sizeof (vdev_t
*), KM_SLEEP
);
2078 /* attempt to online all the vdevs & validate */
2079 attempt_reopen
= B_TRUE
;
2080 for (i
= 0; i
< gcount
; i
++) {
2081 if (glist
[i
] == 0) /* vdev is hole */
2084 vd
[i
] = spa_lookup_by_guid(spa
, glist
[i
], B_FALSE
);
2085 if (vd
[i
] == NULL
) {
2087 * Don't bother attempting to reopen the disks;
2088 * just do the split.
2090 attempt_reopen
= B_FALSE
;
2092 /* attempt to re-online it */
2093 vd
[i
]->vdev_offline
= B_FALSE
;
2097 if (attempt_reopen
) {
2098 vdev_reopen(spa
->spa_root_vdev
);
2100 /* check each device to see what state it's in */
2101 for (extracted
= 0, i
= 0; i
< gcount
; i
++) {
2102 if (vd
[i
] != NULL
&&
2103 vd
[i
]->vdev_stat
.vs_aux
!= VDEV_AUX_SPLIT_POOL
)
2110 * If every disk has been moved to the new pool, or if we never
2111 * even attempted to look at them, then we split them off for
2114 if (!attempt_reopen
|| gcount
== extracted
) {
2115 for (i
= 0; i
< gcount
; i
++)
2118 vdev_reopen(spa
->spa_root_vdev
);
2121 kmem_free(vd
, gcount
* sizeof (vdev_t
*));
2125 spa_load(spa_t
*spa
, spa_load_state_t state
, spa_import_type_t type
,
2126 boolean_t mosconfig
)
2128 nvlist_t
*config
= spa
->spa_config
;
2129 char *ereport
= FM_EREPORT_ZFS_POOL
;
2135 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
, &pool_guid
))
2136 return (SET_ERROR(EINVAL
));
2138 ASSERT(spa
->spa_comment
== NULL
);
2139 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
2140 spa
->spa_comment
= spa_strdup(comment
);
2143 * Versioning wasn't explicitly added to the label until later, so if
2144 * it's not present treat it as the initial version.
2146 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
2147 &spa
->spa_ubsync
.ub_version
) != 0)
2148 spa
->spa_ubsync
.ub_version
= SPA_VERSION_INITIAL
;
2150 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
2151 &spa
->spa_config_txg
);
2153 if ((state
== SPA_LOAD_IMPORT
|| state
== SPA_LOAD_TRYIMPORT
) &&
2154 spa_guid_exists(pool_guid
, 0)) {
2155 error
= SET_ERROR(EEXIST
);
2157 spa
->spa_config_guid
= pool_guid
;
2159 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
,
2161 VERIFY(nvlist_dup(nvl
, &spa
->spa_config_splitting
,
2165 nvlist_free(spa
->spa_load_info
);
2166 spa
->spa_load_info
= fnvlist_alloc();
2168 gethrestime(&spa
->spa_loaded_ts
);
2169 error
= spa_load_impl(spa
, pool_guid
, config
, state
, type
,
2170 mosconfig
, &ereport
);
2174 * Don't count references from objsets that are already closed
2175 * and are making their way through the eviction process.
2177 spa_evicting_os_wait(spa
);
2178 spa
->spa_minref
= refcount_count(&spa
->spa_refcount
);
2180 if (error
!= EEXIST
) {
2181 spa
->spa_loaded_ts
.tv_sec
= 0;
2182 spa
->spa_loaded_ts
.tv_nsec
= 0;
2184 if (error
!= EBADF
) {
2185 zfs_ereport_post(ereport
, spa
, NULL
, NULL
, 0, 0);
2188 spa
->spa_load_state
= error
? SPA_LOAD_ERROR
: SPA_LOAD_NONE
;
2195 * Load an existing storage pool, using the pool's builtin spa_config as a
2196 * source of configuration information.
2198 __attribute__((always_inline
))
2200 spa_load_impl(spa_t
*spa
, uint64_t pool_guid
, nvlist_t
*config
,
2201 spa_load_state_t state
, spa_import_type_t type
, boolean_t mosconfig
,
2205 nvlist_t
*nvroot
= NULL
;
2208 uberblock_t
*ub
= &spa
->spa_uberblock
;
2209 uint64_t children
, config_cache_txg
= spa
->spa_config_txg
;
2210 int orig_mode
= spa
->spa_mode
;
2213 boolean_t missing_feat_write
= B_FALSE
;
2216 * If this is an untrusted config, access the pool in read-only mode.
2217 * This prevents things like resilvering recently removed devices.
2220 spa
->spa_mode
= FREAD
;
2222 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
2224 spa
->spa_load_state
= state
;
2226 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvroot
))
2227 return (SET_ERROR(EINVAL
));
2229 parse
= (type
== SPA_IMPORT_EXISTING
?
2230 VDEV_ALLOC_LOAD
: VDEV_ALLOC_SPLIT
);
2233 * Create "The Godfather" zio to hold all async IOs
2235 spa
->spa_async_zio_root
= kmem_alloc(max_ncpus
* sizeof (void *),
2237 for (i
= 0; i
< max_ncpus
; i
++) {
2238 spa
->spa_async_zio_root
[i
] = zio_root(spa
, NULL
, NULL
,
2239 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
2240 ZIO_FLAG_GODFATHER
);
2244 * Parse the configuration into a vdev tree. We explicitly set the
2245 * value that will be returned by spa_version() since parsing the
2246 * configuration requires knowing the version number.
2248 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2249 error
= spa_config_parse(spa
, &rvd
, nvroot
, NULL
, 0, parse
);
2250 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2255 ASSERT(spa
->spa_root_vdev
== rvd
);
2256 ASSERT3U(spa
->spa_min_ashift
, >=, SPA_MINBLOCKSHIFT
);
2257 ASSERT3U(spa
->spa_max_ashift
, <=, SPA_MAXBLOCKSHIFT
);
2259 if (type
!= SPA_IMPORT_ASSEMBLE
) {
2260 ASSERT(spa_guid(spa
) == pool_guid
);
2264 * Try to open all vdevs, loading each label in the process.
2266 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2267 error
= vdev_open(rvd
);
2268 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2273 * We need to validate the vdev labels against the configuration that
2274 * we have in hand, which is dependent on the setting of mosconfig. If
2275 * mosconfig is true then we're validating the vdev labels based on
2276 * that config. Otherwise, we're validating against the cached config
2277 * (zpool.cache) that was read when we loaded the zfs module, and then
2278 * later we will recursively call spa_load() and validate against
2281 * If we're assembling a new pool that's been split off from an
2282 * existing pool, the labels haven't yet been updated so we skip
2283 * validation for now.
2285 if (type
!= SPA_IMPORT_ASSEMBLE
) {
2286 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2287 error
= vdev_validate(rvd
, mosconfig
);
2288 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2293 if (rvd
->vdev_state
<= VDEV_STATE_CANT_OPEN
)
2294 return (SET_ERROR(ENXIO
));
2298 * Find the best uberblock.
2300 vdev_uberblock_load(rvd
, ub
, &label
);
2303 * If we weren't able to find a single valid uberblock, return failure.
2305 if (ub
->ub_txg
== 0) {
2307 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, ENXIO
));
2311 * If the pool has an unsupported version we can't open it.
2313 if (!SPA_VERSION_IS_SUPPORTED(ub
->ub_version
)) {
2315 return (spa_vdev_err(rvd
, VDEV_AUX_VERSION_NEWER
, ENOTSUP
));
2318 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
2322 * If we weren't able to find what's necessary for reading the
2323 * MOS in the label, return failure.
2325 if (label
== NULL
|| nvlist_lookup_nvlist(label
,
2326 ZPOOL_CONFIG_FEATURES_FOR_READ
, &features
) != 0) {
2328 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
2333 * Update our in-core representation with the definitive values
2336 nvlist_free(spa
->spa_label_features
);
2337 VERIFY(nvlist_dup(features
, &spa
->spa_label_features
, 0) == 0);
2343 * Look through entries in the label nvlist's features_for_read. If
2344 * there is a feature listed there which we don't understand then we
2345 * cannot open a pool.
2347 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
2348 nvlist_t
*unsup_feat
;
2351 VERIFY(nvlist_alloc(&unsup_feat
, NV_UNIQUE_NAME
, KM_SLEEP
) ==
2354 for (nvp
= nvlist_next_nvpair(spa
->spa_label_features
, NULL
);
2356 nvp
= nvlist_next_nvpair(spa
->spa_label_features
, nvp
)) {
2357 if (!zfeature_is_supported(nvpair_name(nvp
))) {
2358 VERIFY(nvlist_add_string(unsup_feat
,
2359 nvpair_name(nvp
), "") == 0);
2363 if (!nvlist_empty(unsup_feat
)) {
2364 VERIFY(nvlist_add_nvlist(spa
->spa_load_info
,
2365 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
) == 0);
2366 nvlist_free(unsup_feat
);
2367 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
2371 nvlist_free(unsup_feat
);
2375 * If the vdev guid sum doesn't match the uberblock, we have an
2376 * incomplete configuration. We first check to see if the pool
2377 * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN).
2378 * If it is, defer the vdev_guid_sum check till later so we
2379 * can handle missing vdevs.
2381 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VDEV_CHILDREN
,
2382 &children
) != 0 && mosconfig
&& type
!= SPA_IMPORT_ASSEMBLE
&&
2383 rvd
->vdev_guid_sum
!= ub
->ub_guid_sum
)
2384 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
, ENXIO
));
2386 if (type
!= SPA_IMPORT_ASSEMBLE
&& spa
->spa_config_splitting
) {
2387 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2388 spa_try_repair(spa
, config
);
2389 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2390 nvlist_free(spa
->spa_config_splitting
);
2391 spa
->spa_config_splitting
= NULL
;
2395 * Initialize internal SPA structures.
2397 spa
->spa_state
= POOL_STATE_ACTIVE
;
2398 spa
->spa_ubsync
= spa
->spa_uberblock
;
2399 spa
->spa_verify_min_txg
= spa
->spa_extreme_rewind
?
2400 TXG_INITIAL
- 1 : spa_last_synced_txg(spa
) - TXG_DEFER_SIZE
- 1;
2401 spa
->spa_first_txg
= spa
->spa_last_ubsync_txg
?
2402 spa
->spa_last_ubsync_txg
: spa_last_synced_txg(spa
) + 1;
2403 spa
->spa_claim_max_txg
= spa
->spa_first_txg
;
2404 spa
->spa_prev_software_version
= ub
->ub_software_version
;
2406 error
= dsl_pool_init(spa
, spa
->spa_first_txg
, &spa
->spa_dsl_pool
);
2408 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2409 spa
->spa_meta_objset
= spa
->spa_dsl_pool
->dp_meta_objset
;
2411 if (spa_dir_prop(spa
, DMU_POOL_CONFIG
, &spa
->spa_config_object
) != 0)
2412 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2414 if (spa_version(spa
) >= SPA_VERSION_FEATURES
) {
2415 boolean_t missing_feat_read
= B_FALSE
;
2416 nvlist_t
*unsup_feat
, *enabled_feat
;
2419 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_READ
,
2420 &spa
->spa_feat_for_read_obj
) != 0) {
2421 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2424 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_WRITE
,
2425 &spa
->spa_feat_for_write_obj
) != 0) {
2426 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2429 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_DESCRIPTIONS
,
2430 &spa
->spa_feat_desc_obj
) != 0) {
2431 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2434 enabled_feat
= fnvlist_alloc();
2435 unsup_feat
= fnvlist_alloc();
2437 if (!spa_features_check(spa
, B_FALSE
,
2438 unsup_feat
, enabled_feat
))
2439 missing_feat_read
= B_TRUE
;
2441 if (spa_writeable(spa
) || state
== SPA_LOAD_TRYIMPORT
) {
2442 if (!spa_features_check(spa
, B_TRUE
,
2443 unsup_feat
, enabled_feat
)) {
2444 missing_feat_write
= B_TRUE
;
2448 fnvlist_add_nvlist(spa
->spa_load_info
,
2449 ZPOOL_CONFIG_ENABLED_FEAT
, enabled_feat
);
2451 if (!nvlist_empty(unsup_feat
)) {
2452 fnvlist_add_nvlist(spa
->spa_load_info
,
2453 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
);
2456 fnvlist_free(enabled_feat
);
2457 fnvlist_free(unsup_feat
);
2459 if (!missing_feat_read
) {
2460 fnvlist_add_boolean(spa
->spa_load_info
,
2461 ZPOOL_CONFIG_CAN_RDONLY
);
2465 * If the state is SPA_LOAD_TRYIMPORT, our objective is
2466 * twofold: to determine whether the pool is available for
2467 * import in read-write mode and (if it is not) whether the
2468 * pool is available for import in read-only mode. If the pool
2469 * is available for import in read-write mode, it is displayed
2470 * as available in userland; if it is not available for import
2471 * in read-only mode, it is displayed as unavailable in
2472 * userland. If the pool is available for import in read-only
2473 * mode but not read-write mode, it is displayed as unavailable
2474 * in userland with a special note that the pool is actually
2475 * available for open in read-only mode.
2477 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
2478 * missing a feature for write, we must first determine whether
2479 * the pool can be opened read-only before returning to
2480 * userland in order to know whether to display the
2481 * abovementioned note.
2483 if (missing_feat_read
|| (missing_feat_write
&&
2484 spa_writeable(spa
))) {
2485 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
2490 * Load refcounts for ZFS features from disk into an in-memory
2491 * cache during SPA initialization.
2493 for (i
= 0; i
< SPA_FEATURES
; i
++) {
2496 error
= feature_get_refcount_from_disk(spa
,
2497 &spa_feature_table
[i
], &refcount
);
2499 spa
->spa_feat_refcount_cache
[i
] = refcount
;
2500 } else if (error
== ENOTSUP
) {
2501 spa
->spa_feat_refcount_cache
[i
] =
2502 SPA_FEATURE_DISABLED
;
2504 return (spa_vdev_err(rvd
,
2505 VDEV_AUX_CORRUPT_DATA
, EIO
));
2510 if (spa_feature_is_active(spa
, SPA_FEATURE_ENABLED_TXG
)) {
2511 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_ENABLED_TXG
,
2512 &spa
->spa_feat_enabled_txg_obj
) != 0)
2513 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2516 spa
->spa_is_initializing
= B_TRUE
;
2517 error
= dsl_pool_open(spa
->spa_dsl_pool
);
2518 spa
->spa_is_initializing
= B_FALSE
;
2520 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2524 nvlist_t
*policy
= NULL
, *nvconfig
;
2526 if (load_nvlist(spa
, spa
->spa_config_object
, &nvconfig
) != 0)
2527 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2529 if (!spa_is_root(spa
) && nvlist_lookup_uint64(nvconfig
,
2530 ZPOOL_CONFIG_HOSTID
, &hostid
) == 0) {
2532 unsigned long myhostid
= 0;
2534 VERIFY(nvlist_lookup_string(nvconfig
,
2535 ZPOOL_CONFIG_HOSTNAME
, &hostname
) == 0);
2538 myhostid
= zone_get_hostid(NULL
);
2541 * We're emulating the system's hostid in userland, so
2542 * we can't use zone_get_hostid().
2544 (void) ddi_strtoul(hw_serial
, NULL
, 10, &myhostid
);
2545 #endif /* _KERNEL */
2546 if (hostid
!= 0 && myhostid
!= 0 &&
2547 hostid
!= myhostid
) {
2548 nvlist_free(nvconfig
);
2549 cmn_err(CE_WARN
, "pool '%s' could not be "
2550 "loaded as it was last accessed by another "
2551 "system (host: %s hostid: 0x%lx). See: "
2552 "http://zfsonlinux.org/msg/ZFS-8000-EY",
2553 spa_name(spa
), hostname
,
2554 (unsigned long)hostid
);
2555 return (SET_ERROR(EBADF
));
2558 if (nvlist_lookup_nvlist(spa
->spa_config
,
2559 ZPOOL_REWIND_POLICY
, &policy
) == 0)
2560 VERIFY(nvlist_add_nvlist(nvconfig
,
2561 ZPOOL_REWIND_POLICY
, policy
) == 0);
2563 spa_config_set(spa
, nvconfig
);
2565 spa_deactivate(spa
);
2566 spa_activate(spa
, orig_mode
);
2568 return (spa_load(spa
, state
, SPA_IMPORT_EXISTING
, B_TRUE
));
2571 if (spa_dir_prop(spa
, DMU_POOL_SYNC_BPOBJ
, &obj
) != 0)
2572 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2573 error
= bpobj_open(&spa
->spa_deferred_bpobj
, spa
->spa_meta_objset
, obj
);
2575 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2578 * Load the bit that tells us to use the new accounting function
2579 * (raid-z deflation). If we have an older pool, this will not
2582 error
= spa_dir_prop(spa
, DMU_POOL_DEFLATE
, &spa
->spa_deflate
);
2583 if (error
!= 0 && error
!= ENOENT
)
2584 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2586 error
= spa_dir_prop(spa
, DMU_POOL_CREATION_VERSION
,
2587 &spa
->spa_creation_version
);
2588 if (error
!= 0 && error
!= ENOENT
)
2589 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2592 * Load the persistent error log. If we have an older pool, this will
2595 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_LAST
, &spa
->spa_errlog_last
);
2596 if (error
!= 0 && error
!= ENOENT
)
2597 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2599 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_SCRUB
,
2600 &spa
->spa_errlog_scrub
);
2601 if (error
!= 0 && error
!= ENOENT
)
2602 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2605 * Load the history object. If we have an older pool, this
2606 * will not be present.
2608 error
= spa_dir_prop(spa
, DMU_POOL_HISTORY
, &spa
->spa_history
);
2609 if (error
!= 0 && error
!= ENOENT
)
2610 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2613 * If we're assembling the pool from the split-off vdevs of
2614 * an existing pool, we don't want to attach the spares & cache
2619 * Load any hot spares for this pool.
2621 error
= spa_dir_prop(spa
, DMU_POOL_SPARES
, &spa
->spa_spares
.sav_object
);
2622 if (error
!= 0 && error
!= ENOENT
)
2623 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2624 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
2625 ASSERT(spa_version(spa
) >= SPA_VERSION_SPARES
);
2626 if (load_nvlist(spa
, spa
->spa_spares
.sav_object
,
2627 &spa
->spa_spares
.sav_config
) != 0)
2628 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2630 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2631 spa_load_spares(spa
);
2632 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2633 } else if (error
== 0) {
2634 spa
->spa_spares
.sav_sync
= B_TRUE
;
2638 * Load any level 2 ARC devices for this pool.
2640 error
= spa_dir_prop(spa
, DMU_POOL_L2CACHE
,
2641 &spa
->spa_l2cache
.sav_object
);
2642 if (error
!= 0 && error
!= ENOENT
)
2643 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2644 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
2645 ASSERT(spa_version(spa
) >= SPA_VERSION_L2CACHE
);
2646 if (load_nvlist(spa
, spa
->spa_l2cache
.sav_object
,
2647 &spa
->spa_l2cache
.sav_config
) != 0)
2648 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2650 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2651 spa_load_l2cache(spa
);
2652 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2653 } else if (error
== 0) {
2654 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
2657 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
2659 error
= spa_dir_prop(spa
, DMU_POOL_PROPS
, &spa
->spa_pool_props_object
);
2660 if (error
&& error
!= ENOENT
)
2661 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2664 uint64_t autoreplace
= 0;
2666 spa_prop_find(spa
, ZPOOL_PROP_BOOTFS
, &spa
->spa_bootfs
);
2667 spa_prop_find(spa
, ZPOOL_PROP_AUTOREPLACE
, &autoreplace
);
2668 spa_prop_find(spa
, ZPOOL_PROP_DELEGATION
, &spa
->spa_delegation
);
2669 spa_prop_find(spa
, ZPOOL_PROP_FAILUREMODE
, &spa
->spa_failmode
);
2670 spa_prop_find(spa
, ZPOOL_PROP_AUTOEXPAND
, &spa
->spa_autoexpand
);
2671 spa_prop_find(spa
, ZPOOL_PROP_DEDUPDITTO
,
2672 &spa
->spa_dedup_ditto
);
2674 spa
->spa_autoreplace
= (autoreplace
!= 0);
2678 * If the 'autoreplace' property is set, then post a resource notifying
2679 * the ZFS DE that it should not issue any faults for unopenable
2680 * devices. We also iterate over the vdevs, and post a sysevent for any
2681 * unopenable vdevs so that the normal autoreplace handler can take
2684 if (spa
->spa_autoreplace
&& state
!= SPA_LOAD_TRYIMPORT
) {
2685 spa_check_removed(spa
->spa_root_vdev
);
2687 * For the import case, this is done in spa_import(), because
2688 * at this point we're using the spare definitions from
2689 * the MOS config, not necessarily from the userland config.
2691 if (state
!= SPA_LOAD_IMPORT
) {
2692 spa_aux_check_removed(&spa
->spa_spares
);
2693 spa_aux_check_removed(&spa
->spa_l2cache
);
2698 * Load the vdev state for all toplevel vdevs.
2703 * Propagate the leaf DTLs we just loaded all the way up the tree.
2705 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2706 vdev_dtl_reassess(rvd
, 0, 0, B_FALSE
);
2707 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2710 * Load the DDTs (dedup tables).
2712 error
= ddt_load(spa
);
2714 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2716 spa_update_dspace(spa
);
2719 * Validate the config, using the MOS config to fill in any
2720 * information which might be missing. If we fail to validate
2721 * the config then declare the pool unfit for use. If we're
2722 * assembling a pool from a split, the log is not transferred
2725 if (type
!= SPA_IMPORT_ASSEMBLE
) {
2728 if (load_nvlist(spa
, spa
->spa_config_object
, &nvconfig
) != 0)
2729 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2731 if (!spa_config_valid(spa
, nvconfig
)) {
2732 nvlist_free(nvconfig
);
2733 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
,
2736 nvlist_free(nvconfig
);
2739 * Now that we've validated the config, check the state of the
2740 * root vdev. If it can't be opened, it indicates one or
2741 * more toplevel vdevs are faulted.
2743 if (rvd
->vdev_state
<= VDEV_STATE_CANT_OPEN
)
2744 return (SET_ERROR(ENXIO
));
2746 if (spa_writeable(spa
) && spa_check_logs(spa
)) {
2747 *ereport
= FM_EREPORT_ZFS_LOG_REPLAY
;
2748 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_LOG
, ENXIO
));
2752 if (missing_feat_write
) {
2753 ASSERT(state
== SPA_LOAD_TRYIMPORT
);
2756 * At this point, we know that we can open the pool in
2757 * read-only mode but not read-write mode. We now have enough
2758 * information and can return to userland.
2760 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
, ENOTSUP
));
2764 * We've successfully opened the pool, verify that we're ready
2765 * to start pushing transactions.
2767 if (state
!= SPA_LOAD_TRYIMPORT
) {
2768 if ((error
= spa_load_verify(spa
)))
2769 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
2773 if (spa_writeable(spa
) && (state
== SPA_LOAD_RECOVER
||
2774 spa
->spa_load_max_txg
== UINT64_MAX
)) {
2776 int need_update
= B_FALSE
;
2777 dsl_pool_t
*dp
= spa_get_dsl(spa
);
2780 ASSERT(state
!= SPA_LOAD_TRYIMPORT
);
2783 * Claim log blocks that haven't been committed yet.
2784 * This must all happen in a single txg.
2785 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
2786 * invoked from zil_claim_log_block()'s i/o done callback.
2787 * Price of rollback is that we abandon the log.
2789 spa
->spa_claiming
= B_TRUE
;
2791 tx
= dmu_tx_create_assigned(dp
, spa_first_txg(spa
));
2792 (void) dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
2793 zil_claim
, tx
, DS_FIND_CHILDREN
);
2796 spa
->spa_claiming
= B_FALSE
;
2798 spa_set_log_state(spa
, SPA_LOG_GOOD
);
2799 spa
->spa_sync_on
= B_TRUE
;
2800 txg_sync_start(spa
->spa_dsl_pool
);
2803 * Wait for all claims to sync. We sync up to the highest
2804 * claimed log block birth time so that claimed log blocks
2805 * don't appear to be from the future. spa_claim_max_txg
2806 * will have been set for us by either zil_check_log_chain()
2807 * (invoked from spa_check_logs()) or zil_claim() above.
2809 txg_wait_synced(spa
->spa_dsl_pool
, spa
->spa_claim_max_txg
);
2812 * If the config cache is stale, or we have uninitialized
2813 * metaslabs (see spa_vdev_add()), then update the config.
2815 * If this is a verbatim import, trust the current
2816 * in-core spa_config and update the disk labels.
2818 if (config_cache_txg
!= spa
->spa_config_txg
||
2819 state
== SPA_LOAD_IMPORT
||
2820 state
== SPA_LOAD_RECOVER
||
2821 (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
))
2822 need_update
= B_TRUE
;
2824 for (c
= 0; c
< rvd
->vdev_children
; c
++)
2825 if (rvd
->vdev_child
[c
]->vdev_ms_array
== 0)
2826 need_update
= B_TRUE
;
2829 * Update the config cache asychronously in case we're the
2830 * root pool, in which case the config cache isn't writable yet.
2833 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
2836 * Check all DTLs to see if anything needs resilvering.
2838 if (!dsl_scan_resilvering(spa
->spa_dsl_pool
) &&
2839 vdev_resilver_needed(rvd
, NULL
, NULL
))
2840 spa_async_request(spa
, SPA_ASYNC_RESILVER
);
2843 * Log the fact that we booted up (so that we can detect if
2844 * we rebooted in the middle of an operation).
2846 spa_history_log_version(spa
, "open");
2849 * Delete any inconsistent datasets.
2851 (void) dmu_objset_find(spa_name(spa
),
2852 dsl_destroy_inconsistent
, NULL
, DS_FIND_CHILDREN
);
2855 * Clean up any stale temporary dataset userrefs.
2857 dsl_pool_clean_tmp_userrefs(spa
->spa_dsl_pool
);
2864 spa_load_retry(spa_t
*spa
, spa_load_state_t state
, int mosconfig
)
2866 int mode
= spa
->spa_mode
;
2869 spa_deactivate(spa
);
2871 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
- 1;
2873 spa_activate(spa
, mode
);
2874 spa_async_suspend(spa
);
2876 return (spa_load(spa
, state
, SPA_IMPORT_EXISTING
, mosconfig
));
2880 * If spa_load() fails this function will try loading prior txg's. If
2881 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
2882 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
2883 * function will not rewind the pool and will return the same error as
2887 spa_load_best(spa_t
*spa
, spa_load_state_t state
, int mosconfig
,
2888 uint64_t max_request
, int rewind_flags
)
2890 nvlist_t
*loadinfo
= NULL
;
2891 nvlist_t
*config
= NULL
;
2892 int load_error
, rewind_error
;
2893 uint64_t safe_rewind_txg
;
2896 if (spa
->spa_load_txg
&& state
== SPA_LOAD_RECOVER
) {
2897 spa
->spa_load_max_txg
= spa
->spa_load_txg
;
2898 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
2900 spa
->spa_load_max_txg
= max_request
;
2901 if (max_request
!= UINT64_MAX
)
2902 spa
->spa_extreme_rewind
= B_TRUE
;
2905 load_error
= rewind_error
= spa_load(spa
, state
, SPA_IMPORT_EXISTING
,
2907 if (load_error
== 0)
2910 if (spa
->spa_root_vdev
!= NULL
)
2911 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
2913 spa
->spa_last_ubsync_txg
= spa
->spa_uberblock
.ub_txg
;
2914 spa
->spa_last_ubsync_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
2916 if (rewind_flags
& ZPOOL_NEVER_REWIND
) {
2917 nvlist_free(config
);
2918 return (load_error
);
2921 if (state
== SPA_LOAD_RECOVER
) {
2922 /* Price of rolling back is discarding txgs, including log */
2923 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
2926 * If we aren't rolling back save the load info from our first
2927 * import attempt so that we can restore it after attempting
2930 loadinfo
= spa
->spa_load_info
;
2931 spa
->spa_load_info
= fnvlist_alloc();
2934 spa
->spa_load_max_txg
= spa
->spa_last_ubsync_txg
;
2935 safe_rewind_txg
= spa
->spa_last_ubsync_txg
- TXG_DEFER_SIZE
;
2936 min_txg
= (rewind_flags
& ZPOOL_EXTREME_REWIND
) ?
2937 TXG_INITIAL
: safe_rewind_txg
;
2940 * Continue as long as we're finding errors, we're still within
2941 * the acceptable rewind range, and we're still finding uberblocks
2943 while (rewind_error
&& spa
->spa_uberblock
.ub_txg
>= min_txg
&&
2944 spa
->spa_uberblock
.ub_txg
<= spa
->spa_load_max_txg
) {
2945 if (spa
->spa_load_max_txg
< safe_rewind_txg
)
2946 spa
->spa_extreme_rewind
= B_TRUE
;
2947 rewind_error
= spa_load_retry(spa
, state
, mosconfig
);
2950 spa
->spa_extreme_rewind
= B_FALSE
;
2951 spa
->spa_load_max_txg
= UINT64_MAX
;
2953 if (config
&& (rewind_error
|| state
!= SPA_LOAD_RECOVER
))
2954 spa_config_set(spa
, config
);
2956 if (state
== SPA_LOAD_RECOVER
) {
2957 ASSERT3P(loadinfo
, ==, NULL
);
2958 return (rewind_error
);
2960 /* Store the rewind info as part of the initial load info */
2961 fnvlist_add_nvlist(loadinfo
, ZPOOL_CONFIG_REWIND_INFO
,
2962 spa
->spa_load_info
);
2964 /* Restore the initial load info */
2965 fnvlist_free(spa
->spa_load_info
);
2966 spa
->spa_load_info
= loadinfo
;
2968 return (load_error
);
2975 * The import case is identical to an open except that the configuration is sent
2976 * down from userland, instead of grabbed from the configuration cache. For the
2977 * case of an open, the pool configuration will exist in the
2978 * POOL_STATE_UNINITIALIZED state.
2980 * The stats information (gen/count/ustats) is used to gather vdev statistics at
2981 * the same time open the pool, without having to keep around the spa_t in some
2985 spa_open_common(const char *pool
, spa_t
**spapp
, void *tag
, nvlist_t
*nvpolicy
,
2989 spa_load_state_t state
= SPA_LOAD_OPEN
;
2991 int locked
= B_FALSE
;
2992 int firstopen
= B_FALSE
;
2997 * As disgusting as this is, we need to support recursive calls to this
2998 * function because dsl_dir_open() is called during spa_load(), and ends
2999 * up calling spa_open() again. The real fix is to figure out how to
3000 * avoid dsl_dir_open() calling this in the first place.
3002 if (mutex_owner(&spa_namespace_lock
) != curthread
) {
3003 mutex_enter(&spa_namespace_lock
);
3007 if ((spa
= spa_lookup(pool
)) == NULL
) {
3009 mutex_exit(&spa_namespace_lock
);
3010 return (SET_ERROR(ENOENT
));
3013 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
) {
3014 zpool_rewind_policy_t policy
;
3018 zpool_get_rewind_policy(nvpolicy
? nvpolicy
: spa
->spa_config
,
3020 if (policy
.zrp_request
& ZPOOL_DO_REWIND
)
3021 state
= SPA_LOAD_RECOVER
;
3023 spa_activate(spa
, spa_mode_global
);
3025 if (state
!= SPA_LOAD_RECOVER
)
3026 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
3028 error
= spa_load_best(spa
, state
, B_FALSE
, policy
.zrp_txg
,
3029 policy
.zrp_request
);
3031 if (error
== EBADF
) {
3033 * If vdev_validate() returns failure (indicated by
3034 * EBADF), it indicates that one of the vdevs indicates
3035 * that the pool has been exported or destroyed. If
3036 * this is the case, the config cache is out of sync and
3037 * we should remove the pool from the namespace.
3040 spa_deactivate(spa
);
3041 spa_config_sync(spa
, B_TRUE
, B_TRUE
);
3044 mutex_exit(&spa_namespace_lock
);
3045 return (SET_ERROR(ENOENT
));
3050 * We can't open the pool, but we still have useful
3051 * information: the state of each vdev after the
3052 * attempted vdev_open(). Return this to the user.
3054 if (config
!= NULL
&& spa
->spa_config
) {
3055 VERIFY(nvlist_dup(spa
->spa_config
, config
,
3057 VERIFY(nvlist_add_nvlist(*config
,
3058 ZPOOL_CONFIG_LOAD_INFO
,
3059 spa
->spa_load_info
) == 0);
3062 spa_deactivate(spa
);
3063 spa
->spa_last_open_failed
= error
;
3065 mutex_exit(&spa_namespace_lock
);
3071 spa_open_ref(spa
, tag
);
3074 *config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
3077 * If we've recovered the pool, pass back any information we
3078 * gathered while doing the load.
3080 if (state
== SPA_LOAD_RECOVER
) {
3081 VERIFY(nvlist_add_nvlist(*config
, ZPOOL_CONFIG_LOAD_INFO
,
3082 spa
->spa_load_info
) == 0);
3086 spa
->spa_last_open_failed
= 0;
3087 spa
->spa_last_ubsync_txg
= 0;
3088 spa
->spa_load_txg
= 0;
3089 mutex_exit(&spa_namespace_lock
);
3094 zvol_create_minors(spa
->spa_name
);
3103 spa_open_rewind(const char *name
, spa_t
**spapp
, void *tag
, nvlist_t
*policy
,
3106 return (spa_open_common(name
, spapp
, tag
, policy
, config
));
3110 spa_open(const char *name
, spa_t
**spapp
, void *tag
)
3112 return (spa_open_common(name
, spapp
, tag
, NULL
, NULL
));
3116 * Lookup the given spa_t, incrementing the inject count in the process,
3117 * preventing it from being exported or destroyed.
3120 spa_inject_addref(char *name
)
3124 mutex_enter(&spa_namespace_lock
);
3125 if ((spa
= spa_lookup(name
)) == NULL
) {
3126 mutex_exit(&spa_namespace_lock
);
3129 spa
->spa_inject_ref
++;
3130 mutex_exit(&spa_namespace_lock
);
3136 spa_inject_delref(spa_t
*spa
)
3138 mutex_enter(&spa_namespace_lock
);
3139 spa
->spa_inject_ref
--;
3140 mutex_exit(&spa_namespace_lock
);
3144 * Add spares device information to the nvlist.
3147 spa_add_spares(spa_t
*spa
, nvlist_t
*config
)
3157 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
3159 if (spa
->spa_spares
.sav_count
== 0)
3162 VERIFY(nvlist_lookup_nvlist(config
,
3163 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
3164 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
3165 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
3167 VERIFY(nvlist_add_nvlist_array(nvroot
,
3168 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
3169 VERIFY(nvlist_lookup_nvlist_array(nvroot
,
3170 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
3173 * Go through and find any spares which have since been
3174 * repurposed as an active spare. If this is the case, update
3175 * their status appropriately.
3177 for (i
= 0; i
< nspares
; i
++) {
3178 VERIFY(nvlist_lookup_uint64(spares
[i
],
3179 ZPOOL_CONFIG_GUID
, &guid
) == 0);
3180 if (spa_spare_exists(guid
, &pool
, NULL
) &&
3182 VERIFY(nvlist_lookup_uint64_array(
3183 spares
[i
], ZPOOL_CONFIG_VDEV_STATS
,
3184 (uint64_t **)&vs
, &vsc
) == 0);
3185 vs
->vs_state
= VDEV_STATE_CANT_OPEN
;
3186 vs
->vs_aux
= VDEV_AUX_SPARED
;
3193 * Add l2cache device information to the nvlist, including vdev stats.
3196 spa_add_l2cache(spa_t
*spa
, nvlist_t
*config
)
3199 uint_t i
, j
, nl2cache
;
3206 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
3208 if (spa
->spa_l2cache
.sav_count
== 0)
3211 VERIFY(nvlist_lookup_nvlist(config
,
3212 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
3213 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_l2cache
.sav_config
,
3214 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
3215 if (nl2cache
!= 0) {
3216 VERIFY(nvlist_add_nvlist_array(nvroot
,
3217 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
3218 VERIFY(nvlist_lookup_nvlist_array(nvroot
,
3219 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
3222 * Update level 2 cache device stats.
3225 for (i
= 0; i
< nl2cache
; i
++) {
3226 VERIFY(nvlist_lookup_uint64(l2cache
[i
],
3227 ZPOOL_CONFIG_GUID
, &guid
) == 0);
3230 for (j
= 0; j
< spa
->spa_l2cache
.sav_count
; j
++) {
3232 spa
->spa_l2cache
.sav_vdevs
[j
]->vdev_guid
) {
3233 vd
= spa
->spa_l2cache
.sav_vdevs
[j
];
3239 VERIFY(nvlist_lookup_uint64_array(l2cache
[i
],
3240 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
, &vsc
)
3242 vdev_get_stats(vd
, vs
);
3248 spa_feature_stats_from_disk(spa_t
*spa
, nvlist_t
*features
)
3253 if (spa
->spa_feat_for_read_obj
!= 0) {
3254 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
3255 spa
->spa_feat_for_read_obj
);
3256 zap_cursor_retrieve(&zc
, &za
) == 0;
3257 zap_cursor_advance(&zc
)) {
3258 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
3259 za
.za_num_integers
== 1);
3260 VERIFY0(nvlist_add_uint64(features
, za
.za_name
,
3261 za
.za_first_integer
));
3263 zap_cursor_fini(&zc
);
3266 if (spa
->spa_feat_for_write_obj
!= 0) {
3267 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
3268 spa
->spa_feat_for_write_obj
);
3269 zap_cursor_retrieve(&zc
, &za
) == 0;
3270 zap_cursor_advance(&zc
)) {
3271 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
3272 za
.za_num_integers
== 1);
3273 VERIFY0(nvlist_add_uint64(features
, za
.za_name
,
3274 za
.za_first_integer
));
3276 zap_cursor_fini(&zc
);
3281 spa_feature_stats_from_cache(spa_t
*spa
, nvlist_t
*features
)
3285 for (i
= 0; i
< SPA_FEATURES
; i
++) {
3286 zfeature_info_t feature
= spa_feature_table
[i
];
3289 if (feature_get_refcount(spa
, &feature
, &refcount
) != 0)
3292 VERIFY0(nvlist_add_uint64(features
, feature
.fi_guid
, refcount
));
3297 * Store a list of pool features and their reference counts in the
3300 * The first time this is called on a spa, allocate a new nvlist, fetch
3301 * the pool features and reference counts from disk, then save the list
3302 * in the spa. In subsequent calls on the same spa use the saved nvlist
3303 * and refresh its values from the cached reference counts. This
3304 * ensures we don't block here on I/O on a suspended pool so 'zpool
3305 * clear' can resume the pool.
3308 spa_add_feature_stats(spa_t
*spa
, nvlist_t
*config
)
3312 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
3314 mutex_enter(&spa
->spa_feat_stats_lock
);
3315 features
= spa
->spa_feat_stats
;
3317 if (features
!= NULL
) {
3318 spa_feature_stats_from_cache(spa
, features
);
3320 VERIFY0(nvlist_alloc(&features
, NV_UNIQUE_NAME
, KM_SLEEP
));
3321 spa
->spa_feat_stats
= features
;
3322 spa_feature_stats_from_disk(spa
, features
);
3325 VERIFY0(nvlist_add_nvlist(config
, ZPOOL_CONFIG_FEATURE_STATS
,
3328 mutex_exit(&spa
->spa_feat_stats_lock
);
3332 spa_get_stats(const char *name
, nvlist_t
**config
,
3333 char *altroot
, size_t buflen
)
3339 error
= spa_open_common(name
, &spa
, FTAG
, NULL
, config
);
3343 * This still leaves a window of inconsistency where the spares
3344 * or l2cache devices could change and the config would be
3345 * self-inconsistent.
3347 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
3349 if (*config
!= NULL
) {
3350 uint64_t loadtimes
[2];
3352 loadtimes
[0] = spa
->spa_loaded_ts
.tv_sec
;
3353 loadtimes
[1] = spa
->spa_loaded_ts
.tv_nsec
;
3354 VERIFY(nvlist_add_uint64_array(*config
,
3355 ZPOOL_CONFIG_LOADED_TIME
, loadtimes
, 2) == 0);
3357 VERIFY(nvlist_add_uint64(*config
,
3358 ZPOOL_CONFIG_ERRCOUNT
,
3359 spa_get_errlog_size(spa
)) == 0);
3361 if (spa_suspended(spa
))
3362 VERIFY(nvlist_add_uint64(*config
,
3363 ZPOOL_CONFIG_SUSPENDED
,
3364 spa
->spa_failmode
) == 0);
3366 spa_add_spares(spa
, *config
);
3367 spa_add_l2cache(spa
, *config
);
3368 spa_add_feature_stats(spa
, *config
);
3373 * We want to get the alternate root even for faulted pools, so we cheat
3374 * and call spa_lookup() directly.
3378 mutex_enter(&spa_namespace_lock
);
3379 spa
= spa_lookup(name
);
3381 spa_altroot(spa
, altroot
, buflen
);
3385 mutex_exit(&spa_namespace_lock
);
3387 spa_altroot(spa
, altroot
, buflen
);
3392 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
3393 spa_close(spa
, FTAG
);
3400 * Validate that the auxiliary device array is well formed. We must have an
3401 * array of nvlists, each which describes a valid leaf vdev. If this is an
3402 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
3403 * specified, as long as they are well-formed.
3406 spa_validate_aux_devs(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
,
3407 spa_aux_vdev_t
*sav
, const char *config
, uint64_t version
,
3408 vdev_labeltype_t label
)
3415 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
3418 * It's acceptable to have no devs specified.
3420 if (nvlist_lookup_nvlist_array(nvroot
, config
, &dev
, &ndev
) != 0)
3424 return (SET_ERROR(EINVAL
));
3427 * Make sure the pool is formatted with a version that supports this
3430 if (spa_version(spa
) < version
)
3431 return (SET_ERROR(ENOTSUP
));
3434 * Set the pending device list so we correctly handle device in-use
3437 sav
->sav_pending
= dev
;
3438 sav
->sav_npending
= ndev
;
3440 for (i
= 0; i
< ndev
; i
++) {
3441 if ((error
= spa_config_parse(spa
, &vd
, dev
[i
], NULL
, 0,
3445 if (!vd
->vdev_ops
->vdev_op_leaf
) {
3447 error
= SET_ERROR(EINVAL
);
3452 * The L2ARC currently only supports disk devices in
3453 * kernel context. For user-level testing, we allow it.
3456 if ((strcmp(config
, ZPOOL_CONFIG_L2CACHE
) == 0) &&
3457 strcmp(vd
->vdev_ops
->vdev_op_type
, VDEV_TYPE_DISK
) != 0) {
3458 error
= SET_ERROR(ENOTBLK
);
3465 if ((error
= vdev_open(vd
)) == 0 &&
3466 (error
= vdev_label_init(vd
, crtxg
, label
)) == 0) {
3467 VERIFY(nvlist_add_uint64(dev
[i
], ZPOOL_CONFIG_GUID
,
3468 vd
->vdev_guid
) == 0);
3474 (mode
!= VDEV_ALLOC_SPARE
&& mode
!= VDEV_ALLOC_L2CACHE
))
3481 sav
->sav_pending
= NULL
;
3482 sav
->sav_npending
= 0;
3487 spa_validate_aux(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
)
3491 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
3493 if ((error
= spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
3494 &spa
->spa_spares
, ZPOOL_CONFIG_SPARES
, SPA_VERSION_SPARES
,
3495 VDEV_LABEL_SPARE
)) != 0) {
3499 return (spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
3500 &spa
->spa_l2cache
, ZPOOL_CONFIG_L2CACHE
, SPA_VERSION_L2CACHE
,
3501 VDEV_LABEL_L2CACHE
));
3505 spa_set_aux_vdevs(spa_aux_vdev_t
*sav
, nvlist_t
**devs
, int ndevs
,
3510 if (sav
->sav_config
!= NULL
) {
3516 * Generate new dev list by concatentating with the
3519 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
, config
,
3520 &olddevs
, &oldndevs
) == 0);
3522 newdevs
= kmem_alloc(sizeof (void *) *
3523 (ndevs
+ oldndevs
), KM_SLEEP
);
3524 for (i
= 0; i
< oldndevs
; i
++)
3525 VERIFY(nvlist_dup(olddevs
[i
], &newdevs
[i
],
3527 for (i
= 0; i
< ndevs
; i
++)
3528 VERIFY(nvlist_dup(devs
[i
], &newdevs
[i
+ oldndevs
],
3531 VERIFY(nvlist_remove(sav
->sav_config
, config
,
3532 DATA_TYPE_NVLIST_ARRAY
) == 0);
3534 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
,
3535 config
, newdevs
, ndevs
+ oldndevs
) == 0);
3536 for (i
= 0; i
< oldndevs
+ ndevs
; i
++)
3537 nvlist_free(newdevs
[i
]);
3538 kmem_free(newdevs
, (oldndevs
+ ndevs
) * sizeof (void *));
3541 * Generate a new dev list.
3543 VERIFY(nvlist_alloc(&sav
->sav_config
, NV_UNIQUE_NAME
,
3545 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
, config
,
3551 * Stop and drop level 2 ARC devices
3554 spa_l2cache_drop(spa_t
*spa
)
3558 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
3560 for (i
= 0; i
< sav
->sav_count
; i
++) {
3563 vd
= sav
->sav_vdevs
[i
];
3566 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
3567 pool
!= 0ULL && l2arc_vdev_present(vd
))
3568 l2arc_remove_vdev(vd
);
3576 spa_create(const char *pool
, nvlist_t
*nvroot
, nvlist_t
*props
,
3580 char *altroot
= NULL
;
3585 uint64_t txg
= TXG_INITIAL
;
3586 nvlist_t
**spares
, **l2cache
;
3587 uint_t nspares
, nl2cache
;
3588 uint64_t version
, obj
;
3589 boolean_t has_features
;
3595 if (nvlist_lookup_string(props
, "tname", &poolname
) != 0)
3596 poolname
= (char *)pool
;
3599 * If this pool already exists, return failure.
3601 mutex_enter(&spa_namespace_lock
);
3602 if (spa_lookup(poolname
) != NULL
) {
3603 mutex_exit(&spa_namespace_lock
);
3604 return (SET_ERROR(EEXIST
));
3608 * Allocate a new spa_t structure.
3610 nvl
= fnvlist_alloc();
3611 fnvlist_add_string(nvl
, ZPOOL_CONFIG_POOL_NAME
, pool
);
3612 (void) nvlist_lookup_string(props
,
3613 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
3614 spa
= spa_add(poolname
, nvl
, altroot
);
3616 spa_activate(spa
, spa_mode_global
);
3618 if (props
&& (error
= spa_prop_validate(spa
, props
))) {
3619 spa_deactivate(spa
);
3621 mutex_exit(&spa_namespace_lock
);
3626 * Temporary pool names should never be written to disk.
3628 if (poolname
!= pool
)
3629 spa
->spa_import_flags
|= ZFS_IMPORT_TEMP_NAME
;
3631 has_features
= B_FALSE
;
3632 for (elem
= nvlist_next_nvpair(props
, NULL
);
3633 elem
!= NULL
; elem
= nvlist_next_nvpair(props
, elem
)) {
3634 if (zpool_prop_feature(nvpair_name(elem
)))
3635 has_features
= B_TRUE
;
3638 if (has_features
|| nvlist_lookup_uint64(props
,
3639 zpool_prop_to_name(ZPOOL_PROP_VERSION
), &version
) != 0) {
3640 version
= SPA_VERSION
;
3642 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
3644 spa
->spa_first_txg
= txg
;
3645 spa
->spa_uberblock
.ub_txg
= txg
- 1;
3646 spa
->spa_uberblock
.ub_version
= version
;
3647 spa
->spa_ubsync
= spa
->spa_uberblock
;
3650 * Create "The Godfather" zio to hold all async IOs
3652 spa
->spa_async_zio_root
= kmem_alloc(max_ncpus
* sizeof (void *),
3654 for (i
= 0; i
< max_ncpus
; i
++) {
3655 spa
->spa_async_zio_root
[i
] = zio_root(spa
, NULL
, NULL
,
3656 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
3657 ZIO_FLAG_GODFATHER
);
3661 * Create the root vdev.
3663 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3665 error
= spa_config_parse(spa
, &rvd
, nvroot
, NULL
, 0, VDEV_ALLOC_ADD
);
3667 ASSERT(error
!= 0 || rvd
!= NULL
);
3668 ASSERT(error
!= 0 || spa
->spa_root_vdev
== rvd
);
3670 if (error
== 0 && !zfs_allocatable_devs(nvroot
))
3671 error
= SET_ERROR(EINVAL
);
3674 (error
= vdev_create(rvd
, txg
, B_FALSE
)) == 0 &&
3675 (error
= spa_validate_aux(spa
, nvroot
, txg
,
3676 VDEV_ALLOC_ADD
)) == 0) {
3677 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
3678 vdev_metaslab_set_size(rvd
->vdev_child
[c
]);
3679 vdev_expand(rvd
->vdev_child
[c
], txg
);
3683 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3687 spa_deactivate(spa
);
3689 mutex_exit(&spa_namespace_lock
);
3694 * Get the list of spares, if specified.
3696 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
3697 &spares
, &nspares
) == 0) {
3698 VERIFY(nvlist_alloc(&spa
->spa_spares
.sav_config
, NV_UNIQUE_NAME
,
3700 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
3701 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
3702 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3703 spa_load_spares(spa
);
3704 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3705 spa
->spa_spares
.sav_sync
= B_TRUE
;
3709 * Get the list of level 2 cache devices, if specified.
3711 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
3712 &l2cache
, &nl2cache
) == 0) {
3713 VERIFY(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
3714 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
3715 VERIFY(nvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
3716 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
3717 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3718 spa_load_l2cache(spa
);
3719 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3720 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
3723 spa
->spa_is_initializing
= B_TRUE
;
3724 spa
->spa_dsl_pool
= dp
= dsl_pool_create(spa
, zplprops
, txg
);
3725 spa
->spa_meta_objset
= dp
->dp_meta_objset
;
3726 spa
->spa_is_initializing
= B_FALSE
;
3729 * Create DDTs (dedup tables).
3733 spa_update_dspace(spa
);
3735 tx
= dmu_tx_create_assigned(dp
, txg
);
3738 * Create the pool config object.
3740 spa
->spa_config_object
= dmu_object_alloc(spa
->spa_meta_objset
,
3741 DMU_OT_PACKED_NVLIST
, SPA_CONFIG_BLOCKSIZE
,
3742 DMU_OT_PACKED_NVLIST_SIZE
, sizeof (uint64_t), tx
);
3744 if (zap_add(spa
->spa_meta_objset
,
3745 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CONFIG
,
3746 sizeof (uint64_t), 1, &spa
->spa_config_object
, tx
) != 0) {
3747 cmn_err(CE_PANIC
, "failed to add pool config");
3750 if (spa_version(spa
) >= SPA_VERSION_FEATURES
)
3751 spa_feature_create_zap_objects(spa
, tx
);
3753 if (zap_add(spa
->spa_meta_objset
,
3754 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CREATION_VERSION
,
3755 sizeof (uint64_t), 1, &version
, tx
) != 0) {
3756 cmn_err(CE_PANIC
, "failed to add pool version");
3759 /* Newly created pools with the right version are always deflated. */
3760 if (version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
3761 spa
->spa_deflate
= TRUE
;
3762 if (zap_add(spa
->spa_meta_objset
,
3763 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
3764 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
) != 0) {
3765 cmn_err(CE_PANIC
, "failed to add deflate");
3770 * Create the deferred-free bpobj. Turn off compression
3771 * because sync-to-convergence takes longer if the blocksize
3774 obj
= bpobj_alloc(spa
->spa_meta_objset
, 1 << 14, tx
);
3775 dmu_object_set_compress(spa
->spa_meta_objset
, obj
,
3776 ZIO_COMPRESS_OFF
, tx
);
3777 if (zap_add(spa
->spa_meta_objset
,
3778 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_SYNC_BPOBJ
,
3779 sizeof (uint64_t), 1, &obj
, tx
) != 0) {
3780 cmn_err(CE_PANIC
, "failed to add bpobj");
3782 VERIFY3U(0, ==, bpobj_open(&spa
->spa_deferred_bpobj
,
3783 spa
->spa_meta_objset
, obj
));
3786 * Create the pool's history object.
3788 if (version
>= SPA_VERSION_ZPOOL_HISTORY
)
3789 spa_history_create_obj(spa
, tx
);
3792 * Set pool properties.
3794 spa
->spa_bootfs
= zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS
);
3795 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
3796 spa
->spa_failmode
= zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE
);
3797 spa
->spa_autoexpand
= zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND
);
3799 if (props
!= NULL
) {
3800 spa_configfile_set(spa
, props
, B_FALSE
);
3801 spa_sync_props(props
, tx
);
3806 spa
->spa_sync_on
= B_TRUE
;
3807 txg_sync_start(spa
->spa_dsl_pool
);
3810 * We explicitly wait for the first transaction to complete so that our
3811 * bean counters are appropriately updated.
3813 txg_wait_synced(spa
->spa_dsl_pool
, txg
);
3815 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
3817 spa_history_log_version(spa
, "create");
3820 * Don't count references from objsets that are already closed
3821 * and are making their way through the eviction process.
3823 spa_evicting_os_wait(spa
);
3824 spa
->spa_minref
= refcount_count(&spa
->spa_refcount
);
3826 mutex_exit(&spa_namespace_lock
);
3833 * Get the root pool information from the root disk, then import the root pool
3834 * during the system boot up time.
3836 extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t
**);
3839 spa_generate_rootconf(char *devpath
, char *devid
, uint64_t *guid
)
3842 nvlist_t
*nvtop
, *nvroot
;
3845 if (vdev_disk_read_rootlabel(devpath
, devid
, &config
) != 0)
3849 * Add this top-level vdev to the child array.
3851 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
3853 VERIFY(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
3855 VERIFY(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_GUID
, guid
) == 0);
3858 * Put this pool's top-level vdevs into a root vdev.
3860 VERIFY(nvlist_alloc(&nvroot
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
3861 VERIFY(nvlist_add_string(nvroot
, ZPOOL_CONFIG_TYPE
,
3862 VDEV_TYPE_ROOT
) == 0);
3863 VERIFY(nvlist_add_uint64(nvroot
, ZPOOL_CONFIG_ID
, 0ULL) == 0);
3864 VERIFY(nvlist_add_uint64(nvroot
, ZPOOL_CONFIG_GUID
, pgid
) == 0);
3865 VERIFY(nvlist_add_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
3869 * Replace the existing vdev_tree with the new root vdev in
3870 * this pool's configuration (remove the old, add the new).
3872 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, nvroot
) == 0);
3873 nvlist_free(nvroot
);
3878 * Walk the vdev tree and see if we can find a device with "better"
3879 * configuration. A configuration is "better" if the label on that
3880 * device has a more recent txg.
3883 spa_alt_rootvdev(vdev_t
*vd
, vdev_t
**avd
, uint64_t *txg
)
3887 for (c
= 0; c
< vd
->vdev_children
; c
++)
3888 spa_alt_rootvdev(vd
->vdev_child
[c
], avd
, txg
);
3890 if (vd
->vdev_ops
->vdev_op_leaf
) {
3894 if (vdev_disk_read_rootlabel(vd
->vdev_physpath
, vd
->vdev_devid
,
3898 VERIFY(nvlist_lookup_uint64(label
, ZPOOL_CONFIG_POOL_TXG
,
3902 * Do we have a better boot device?
3904 if (label_txg
> *txg
) {
3913 * Import a root pool.
3915 * For x86. devpath_list will consist of devid and/or physpath name of
3916 * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
3917 * The GRUB "findroot" command will return the vdev we should boot.
3919 * For Sparc, devpath_list consists the physpath name of the booting device
3920 * no matter the rootpool is a single device pool or a mirrored pool.
3922 * "/pci@1f,0/ide@d/disk@0,0:a"
3925 spa_import_rootpool(char *devpath
, char *devid
)
3928 vdev_t
*rvd
, *bvd
, *avd
= NULL
;
3929 nvlist_t
*config
, *nvtop
;
3935 * Read the label from the boot device and generate a configuration.
3937 config
= spa_generate_rootconf(devpath
, devid
, &guid
);
3938 #if defined(_OBP) && defined(_KERNEL)
3939 if (config
== NULL
) {
3940 if (strstr(devpath
, "/iscsi/ssd") != NULL
) {
3942 get_iscsi_bootpath_phy(devpath
);
3943 config
= spa_generate_rootconf(devpath
, devid
, &guid
);
3947 if (config
== NULL
) {
3948 cmn_err(CE_NOTE
, "Cannot read the pool label from '%s'",
3950 return (SET_ERROR(EIO
));
3953 VERIFY(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
3955 VERIFY(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_TXG
, &txg
) == 0);
3957 mutex_enter(&spa_namespace_lock
);
3958 if ((spa
= spa_lookup(pname
)) != NULL
) {
3960 * Remove the existing root pool from the namespace so that we
3961 * can replace it with the correct config we just read in.
3966 spa
= spa_add(pname
, config
, NULL
);
3967 spa
->spa_is_root
= B_TRUE
;
3968 spa
->spa_import_flags
= ZFS_IMPORT_VERBATIM
;
3971 * Build up a vdev tree based on the boot device's label config.
3973 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
3975 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3976 error
= spa_config_parse(spa
, &rvd
, nvtop
, NULL
, 0,
3977 VDEV_ALLOC_ROOTPOOL
);
3978 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3980 mutex_exit(&spa_namespace_lock
);
3981 nvlist_free(config
);
3982 cmn_err(CE_NOTE
, "Can not parse the config for pool '%s'",
3988 * Get the boot vdev.
3990 if ((bvd
= vdev_lookup_by_guid(rvd
, guid
)) == NULL
) {
3991 cmn_err(CE_NOTE
, "Can not find the boot vdev for guid %llu",
3992 (u_longlong_t
)guid
);
3993 error
= SET_ERROR(ENOENT
);
3998 * Determine if there is a better boot device.
4001 spa_alt_rootvdev(rvd
, &avd
, &txg
);
4003 cmn_err(CE_NOTE
, "The boot device is 'degraded'. Please "
4004 "try booting from '%s'", avd
->vdev_path
);
4005 error
= SET_ERROR(EINVAL
);
4010 * If the boot device is part of a spare vdev then ensure that
4011 * we're booting off the active spare.
4013 if (bvd
->vdev_parent
->vdev_ops
== &vdev_spare_ops
&&
4014 !bvd
->vdev_isspare
) {
4015 cmn_err(CE_NOTE
, "The boot device is currently spared. Please "
4016 "try booting from '%s'",
4018 vdev_child
[bvd
->vdev_parent
->vdev_children
- 1]->vdev_path
);
4019 error
= SET_ERROR(EINVAL
);
4025 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4027 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4028 mutex_exit(&spa_namespace_lock
);
4030 nvlist_free(config
);
4037 * Import a non-root pool into the system.
4040 spa_import(char *pool
, nvlist_t
*config
, nvlist_t
*props
, uint64_t flags
)
4043 char *altroot
= NULL
;
4044 spa_load_state_t state
= SPA_LOAD_IMPORT
;
4045 zpool_rewind_policy_t policy
;
4046 uint64_t mode
= spa_mode_global
;
4047 uint64_t readonly
= B_FALSE
;
4050 nvlist_t
**spares
, **l2cache
;
4051 uint_t nspares
, nl2cache
;
4054 * If a pool with this name exists, return failure.
4056 mutex_enter(&spa_namespace_lock
);
4057 if (spa_lookup(pool
) != NULL
) {
4058 mutex_exit(&spa_namespace_lock
);
4059 return (SET_ERROR(EEXIST
));
4063 * Create and initialize the spa structure.
4065 (void) nvlist_lookup_string(props
,
4066 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
4067 (void) nvlist_lookup_uint64(props
,
4068 zpool_prop_to_name(ZPOOL_PROP_READONLY
), &readonly
);
4071 spa
= spa_add(pool
, config
, altroot
);
4072 spa
->spa_import_flags
= flags
;
4075 * Verbatim import - Take a pool and insert it into the namespace
4076 * as if it had been loaded at boot.
4078 if (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
) {
4080 spa_configfile_set(spa
, props
, B_FALSE
);
4082 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
4084 mutex_exit(&spa_namespace_lock
);
4088 spa_activate(spa
, mode
);
4091 * Don't start async tasks until we know everything is healthy.
4093 spa_async_suspend(spa
);
4095 zpool_get_rewind_policy(config
, &policy
);
4096 if (policy
.zrp_request
& ZPOOL_DO_REWIND
)
4097 state
= SPA_LOAD_RECOVER
;
4100 * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig
4101 * because the user-supplied config is actually the one to trust when
4104 if (state
!= SPA_LOAD_RECOVER
)
4105 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
4107 error
= spa_load_best(spa
, state
, B_TRUE
, policy
.zrp_txg
,
4108 policy
.zrp_request
);
4111 * Propagate anything learned while loading the pool and pass it
4112 * back to caller (i.e. rewind info, missing devices, etc).
4114 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
4115 spa
->spa_load_info
) == 0);
4117 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4119 * Toss any existing sparelist, as it doesn't have any validity
4120 * anymore, and conflicts with spa_has_spare().
4122 if (spa
->spa_spares
.sav_config
) {
4123 nvlist_free(spa
->spa_spares
.sav_config
);
4124 spa
->spa_spares
.sav_config
= NULL
;
4125 spa_load_spares(spa
);
4127 if (spa
->spa_l2cache
.sav_config
) {
4128 nvlist_free(spa
->spa_l2cache
.sav_config
);
4129 spa
->spa_l2cache
.sav_config
= NULL
;
4130 spa_load_l2cache(spa
);
4133 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
4136 error
= spa_validate_aux(spa
, nvroot
, -1ULL,
4139 error
= spa_validate_aux(spa
, nvroot
, -1ULL,
4140 VDEV_ALLOC_L2CACHE
);
4141 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4144 spa_configfile_set(spa
, props
, B_FALSE
);
4146 if (error
!= 0 || (props
&& spa_writeable(spa
) &&
4147 (error
= spa_prop_set(spa
, props
)))) {
4149 spa_deactivate(spa
);
4151 mutex_exit(&spa_namespace_lock
);
4155 spa_async_resume(spa
);
4158 * Override any spares and level 2 cache devices as specified by
4159 * the user, as these may have correct device names/devids, etc.
4161 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
4162 &spares
, &nspares
) == 0) {
4163 if (spa
->spa_spares
.sav_config
)
4164 VERIFY(nvlist_remove(spa
->spa_spares
.sav_config
,
4165 ZPOOL_CONFIG_SPARES
, DATA_TYPE_NVLIST_ARRAY
) == 0);
4167 VERIFY(nvlist_alloc(&spa
->spa_spares
.sav_config
,
4168 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
4169 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
4170 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
4171 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4172 spa_load_spares(spa
);
4173 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4174 spa
->spa_spares
.sav_sync
= B_TRUE
;
4176 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
4177 &l2cache
, &nl2cache
) == 0) {
4178 if (spa
->spa_l2cache
.sav_config
)
4179 VERIFY(nvlist_remove(spa
->spa_l2cache
.sav_config
,
4180 ZPOOL_CONFIG_L2CACHE
, DATA_TYPE_NVLIST_ARRAY
) == 0);
4182 VERIFY(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
4183 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
4184 VERIFY(nvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
4185 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
4186 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4187 spa_load_l2cache(spa
);
4188 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4189 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
4193 * Check for any removed devices.
4195 if (spa
->spa_autoreplace
) {
4196 spa_aux_check_removed(&spa
->spa_spares
);
4197 spa_aux_check_removed(&spa
->spa_l2cache
);
4200 if (spa_writeable(spa
)) {
4202 * Update the config cache to include the newly-imported pool.
4204 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
4208 * It's possible that the pool was expanded while it was exported.
4209 * We kick off an async task to handle this for us.
4211 spa_async_request(spa
, SPA_ASYNC_AUTOEXPAND
);
4213 mutex_exit(&spa_namespace_lock
);
4214 spa_history_log_version(spa
, "import");
4217 zvol_create_minors(pool
);
4224 spa_tryimport(nvlist_t
*tryconfig
)
4226 nvlist_t
*config
= NULL
;
4232 if (nvlist_lookup_string(tryconfig
, ZPOOL_CONFIG_POOL_NAME
, &poolname
))
4235 if (nvlist_lookup_uint64(tryconfig
, ZPOOL_CONFIG_POOL_STATE
, &state
))
4239 * Create and initialize the spa structure.
4241 mutex_enter(&spa_namespace_lock
);
4242 spa
= spa_add(TRYIMPORT_NAME
, tryconfig
, NULL
);
4243 spa_activate(spa
, FREAD
);
4246 * Pass off the heavy lifting to spa_load().
4247 * Pass TRUE for mosconfig because the user-supplied config
4248 * is actually the one to trust when doing an import.
4250 error
= spa_load(spa
, SPA_LOAD_TRYIMPORT
, SPA_IMPORT_EXISTING
, B_TRUE
);
4253 * If 'tryconfig' was at least parsable, return the current config.
4255 if (spa
->spa_root_vdev
!= NULL
) {
4256 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
4257 VERIFY(nvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
,
4259 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
4261 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_TIMESTAMP
,
4262 spa
->spa_uberblock
.ub_timestamp
) == 0);
4263 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
4264 spa
->spa_load_info
) == 0);
4265 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_ERRATA
,
4266 spa
->spa_errata
) == 0);
4269 * If the bootfs property exists on this pool then we
4270 * copy it out so that external consumers can tell which
4271 * pools are bootable.
4273 if ((!error
|| error
== EEXIST
) && spa
->spa_bootfs
) {
4274 char *tmpname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
4277 * We have to play games with the name since the
4278 * pool was opened as TRYIMPORT_NAME.
4280 if (dsl_dsobj_to_dsname(spa_name(spa
),
4281 spa
->spa_bootfs
, tmpname
) == 0) {
4285 dsname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
4287 cp
= strchr(tmpname
, '/');
4289 (void) strlcpy(dsname
, tmpname
,
4292 (void) snprintf(dsname
, MAXPATHLEN
,
4293 "%s/%s", poolname
, ++cp
);
4295 VERIFY(nvlist_add_string(config
,
4296 ZPOOL_CONFIG_BOOTFS
, dsname
) == 0);
4297 kmem_free(dsname
, MAXPATHLEN
);
4299 kmem_free(tmpname
, MAXPATHLEN
);
4303 * Add the list of hot spares and level 2 cache devices.
4305 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
4306 spa_add_spares(spa
, config
);
4307 spa_add_l2cache(spa
, config
);
4308 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
4312 spa_deactivate(spa
);
4314 mutex_exit(&spa_namespace_lock
);
4320 * Pool export/destroy
4322 * The act of destroying or exporting a pool is very simple. We make sure there
4323 * is no more pending I/O and any references to the pool are gone. Then, we
4324 * update the pool state and sync all the labels to disk, removing the
4325 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
4326 * we don't sync the labels or remove the configuration cache.
4329 spa_export_common(char *pool
, int new_state
, nvlist_t
**oldconfig
,
4330 boolean_t force
, boolean_t hardforce
)
4337 if (!(spa_mode_global
& FWRITE
))
4338 return (SET_ERROR(EROFS
));
4340 mutex_enter(&spa_namespace_lock
);
4341 if ((spa
= spa_lookup(pool
)) == NULL
) {
4342 mutex_exit(&spa_namespace_lock
);
4343 return (SET_ERROR(ENOENT
));
4347 * Put a hold on the pool, drop the namespace lock, stop async tasks,
4348 * reacquire the namespace lock, and see if we can export.
4350 spa_open_ref(spa
, FTAG
);
4351 mutex_exit(&spa_namespace_lock
);
4352 spa_async_suspend(spa
);
4353 mutex_enter(&spa_namespace_lock
);
4354 spa_close(spa
, FTAG
);
4356 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
)
4359 * The pool will be in core if it's openable, in which case we can
4360 * modify its state. Objsets may be open only because they're dirty,
4361 * so we have to force it to sync before checking spa_refcnt.
4363 if (spa
->spa_sync_on
) {
4364 txg_wait_synced(spa
->spa_dsl_pool
, 0);
4365 spa_evicting_os_wait(spa
);
4369 * A pool cannot be exported or destroyed if there are active
4370 * references. If we are resetting a pool, allow references by
4371 * fault injection handlers.
4373 if (!spa_refcount_zero(spa
) ||
4374 (spa
->spa_inject_ref
!= 0 &&
4375 new_state
!= POOL_STATE_UNINITIALIZED
)) {
4376 spa_async_resume(spa
);
4377 mutex_exit(&spa_namespace_lock
);
4378 return (SET_ERROR(EBUSY
));
4381 if (spa
->spa_sync_on
) {
4383 * A pool cannot be exported if it has an active shared spare.
4384 * This is to prevent other pools stealing the active spare
4385 * from an exported pool. At user's own will, such pool can
4386 * be forcedly exported.
4388 if (!force
&& new_state
== POOL_STATE_EXPORTED
&&
4389 spa_has_active_shared_spare(spa
)) {
4390 spa_async_resume(spa
);
4391 mutex_exit(&spa_namespace_lock
);
4392 return (SET_ERROR(EXDEV
));
4396 * We want this to be reflected on every label,
4397 * so mark them all dirty. spa_unload() will do the
4398 * final sync that pushes these changes out.
4400 if (new_state
!= POOL_STATE_UNINITIALIZED
&& !hardforce
) {
4401 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4402 spa
->spa_state
= new_state
;
4403 spa
->spa_final_txg
= spa_last_synced_txg(spa
) +
4405 vdev_config_dirty(spa
->spa_root_vdev
);
4406 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4411 spa_event_notify(spa
, NULL
, FM_EREPORT_ZFS_POOL_DESTROY
);
4413 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
4415 spa_deactivate(spa
);
4418 if (oldconfig
&& spa
->spa_config
)
4419 VERIFY(nvlist_dup(spa
->spa_config
, oldconfig
, 0) == 0);
4421 if (new_state
!= POOL_STATE_UNINITIALIZED
) {
4423 spa_config_sync(spa
, B_TRUE
, B_TRUE
);
4426 mutex_exit(&spa_namespace_lock
);
4432 * Destroy a storage pool.
4435 spa_destroy(char *pool
)
4437 return (spa_export_common(pool
, POOL_STATE_DESTROYED
, NULL
,
4442 * Export a storage pool.
4445 spa_export(char *pool
, nvlist_t
**oldconfig
, boolean_t force
,
4446 boolean_t hardforce
)
4448 return (spa_export_common(pool
, POOL_STATE_EXPORTED
, oldconfig
,
4453 * Similar to spa_export(), this unloads the spa_t without actually removing it
4454 * from the namespace in any way.
4457 spa_reset(char *pool
)
4459 return (spa_export_common(pool
, POOL_STATE_UNINITIALIZED
, NULL
,
4464 * ==========================================================================
4465 * Device manipulation
4466 * ==========================================================================
4470 * Add a device to a storage pool.
4473 spa_vdev_add(spa_t
*spa
, nvlist_t
*nvroot
)
4477 vdev_t
*rvd
= spa
->spa_root_vdev
;
4479 nvlist_t
**spares
, **l2cache
;
4480 uint_t nspares
, nl2cache
;
4483 ASSERT(spa_writeable(spa
));
4485 txg
= spa_vdev_enter(spa
);
4487 if ((error
= spa_config_parse(spa
, &vd
, nvroot
, NULL
, 0,
4488 VDEV_ALLOC_ADD
)) != 0)
4489 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
4491 spa
->spa_pending_vdev
= vd
; /* spa_vdev_exit() will clear this */
4493 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
, &spares
,
4497 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
, &l2cache
,
4501 if (vd
->vdev_children
== 0 && nspares
== 0 && nl2cache
== 0)
4502 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
4504 if (vd
->vdev_children
!= 0 &&
4505 (error
= vdev_create(vd
, txg
, B_FALSE
)) != 0)
4506 return (spa_vdev_exit(spa
, vd
, txg
, error
));
4509 * We must validate the spares and l2cache devices after checking the
4510 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
4512 if ((error
= spa_validate_aux(spa
, nvroot
, txg
, VDEV_ALLOC_ADD
)) != 0)
4513 return (spa_vdev_exit(spa
, vd
, txg
, error
));
4516 * Transfer each new top-level vdev from vd to rvd.
4518 for (c
= 0; c
< vd
->vdev_children
; c
++) {
4521 * Set the vdev id to the first hole, if one exists.
4523 for (id
= 0; id
< rvd
->vdev_children
; id
++) {
4524 if (rvd
->vdev_child
[id
]->vdev_ishole
) {
4525 vdev_free(rvd
->vdev_child
[id
]);
4529 tvd
= vd
->vdev_child
[c
];
4530 vdev_remove_child(vd
, tvd
);
4532 vdev_add_child(rvd
, tvd
);
4533 vdev_config_dirty(tvd
);
4537 spa_set_aux_vdevs(&spa
->spa_spares
, spares
, nspares
,
4538 ZPOOL_CONFIG_SPARES
);
4539 spa_load_spares(spa
);
4540 spa
->spa_spares
.sav_sync
= B_TRUE
;
4543 if (nl2cache
!= 0) {
4544 spa_set_aux_vdevs(&spa
->spa_l2cache
, l2cache
, nl2cache
,
4545 ZPOOL_CONFIG_L2CACHE
);
4546 spa_load_l2cache(spa
);
4547 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
4551 * We have to be careful when adding new vdevs to an existing pool.
4552 * If other threads start allocating from these vdevs before we
4553 * sync the config cache, and we lose power, then upon reboot we may
4554 * fail to open the pool because there are DVAs that the config cache
4555 * can't translate. Therefore, we first add the vdevs without
4556 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
4557 * and then let spa_config_update() initialize the new metaslabs.
4559 * spa_load() checks for added-but-not-initialized vdevs, so that
4560 * if we lose power at any point in this sequence, the remaining
4561 * steps will be completed the next time we load the pool.
4563 (void) spa_vdev_exit(spa
, vd
, txg
, 0);
4565 mutex_enter(&spa_namespace_lock
);
4566 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
4567 mutex_exit(&spa_namespace_lock
);
4573 * Attach a device to a mirror. The arguments are the path to any device
4574 * in the mirror, and the nvroot for the new device. If the path specifies
4575 * a device that is not mirrored, we automatically insert the mirror vdev.
4577 * If 'replacing' is specified, the new device is intended to replace the
4578 * existing device; in this case the two devices are made into their own
4579 * mirror using the 'replacing' vdev, which is functionally identical to
4580 * the mirror vdev (it actually reuses all the same ops) but has a few
4581 * extra rules: you can't attach to it after it's been created, and upon
4582 * completion of resilvering, the first disk (the one being replaced)
4583 * is automatically detached.
4586 spa_vdev_attach(spa_t
*spa
, uint64_t guid
, nvlist_t
*nvroot
, int replacing
)
4588 uint64_t txg
, dtl_max_txg
;
4589 vdev_t
*oldvd
, *newvd
, *newrootvd
, *pvd
, *tvd
;
4591 char *oldvdpath
, *newvdpath
;
4594 ASSERTV(vdev_t
*rvd
= spa
->spa_root_vdev
);
4596 ASSERT(spa_writeable(spa
));
4598 txg
= spa_vdev_enter(spa
);
4600 oldvd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
4603 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
4605 if (!oldvd
->vdev_ops
->vdev_op_leaf
)
4606 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4608 pvd
= oldvd
->vdev_parent
;
4610 if ((error
= spa_config_parse(spa
, &newrootvd
, nvroot
, NULL
, 0,
4611 VDEV_ALLOC_ATTACH
)) != 0)
4612 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
4614 if (newrootvd
->vdev_children
!= 1)
4615 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
4617 newvd
= newrootvd
->vdev_child
[0];
4619 if (!newvd
->vdev_ops
->vdev_op_leaf
)
4620 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
4622 if ((error
= vdev_create(newrootvd
, txg
, replacing
)) != 0)
4623 return (spa_vdev_exit(spa
, newrootvd
, txg
, error
));
4626 * Spares can't replace logs
4628 if (oldvd
->vdev_top
->vdev_islog
&& newvd
->vdev_isspare
)
4629 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4633 * For attach, the only allowable parent is a mirror or the root
4636 if (pvd
->vdev_ops
!= &vdev_mirror_ops
&&
4637 pvd
->vdev_ops
!= &vdev_root_ops
)
4638 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4640 pvops
= &vdev_mirror_ops
;
4643 * Active hot spares can only be replaced by inactive hot
4646 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
4647 oldvd
->vdev_isspare
&&
4648 !spa_has_spare(spa
, newvd
->vdev_guid
))
4649 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4652 * If the source is a hot spare, and the parent isn't already a
4653 * spare, then we want to create a new hot spare. Otherwise, we
4654 * want to create a replacing vdev. The user is not allowed to
4655 * attach to a spared vdev child unless the 'isspare' state is
4656 * the same (spare replaces spare, non-spare replaces
4659 if (pvd
->vdev_ops
== &vdev_replacing_ops
&&
4660 spa_version(spa
) < SPA_VERSION_MULTI_REPLACE
) {
4661 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4662 } else if (pvd
->vdev_ops
== &vdev_spare_ops
&&
4663 newvd
->vdev_isspare
!= oldvd
->vdev_isspare
) {
4664 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4667 if (newvd
->vdev_isspare
)
4668 pvops
= &vdev_spare_ops
;
4670 pvops
= &vdev_replacing_ops
;
4674 * Make sure the new device is big enough.
4676 if (newvd
->vdev_asize
< vdev_get_min_asize(oldvd
))
4677 return (spa_vdev_exit(spa
, newrootvd
, txg
, EOVERFLOW
));
4680 * The new device cannot have a higher alignment requirement
4681 * than the top-level vdev.
4683 if (newvd
->vdev_ashift
> oldvd
->vdev_top
->vdev_ashift
)
4684 return (spa_vdev_exit(spa
, newrootvd
, txg
, EDOM
));
4687 * If this is an in-place replacement, update oldvd's path and devid
4688 * to make it distinguishable from newvd, and unopenable from now on.
4690 if (strcmp(oldvd
->vdev_path
, newvd
->vdev_path
) == 0) {
4691 spa_strfree(oldvd
->vdev_path
);
4692 oldvd
->vdev_path
= kmem_alloc(strlen(newvd
->vdev_path
) + 5,
4694 (void) sprintf(oldvd
->vdev_path
, "%s/%s",
4695 newvd
->vdev_path
, "old");
4696 if (oldvd
->vdev_devid
!= NULL
) {
4697 spa_strfree(oldvd
->vdev_devid
);
4698 oldvd
->vdev_devid
= NULL
;
4702 /* mark the device being resilvered */
4703 newvd
->vdev_resilver_txg
= txg
;
4706 * If the parent is not a mirror, or if we're replacing, insert the new
4707 * mirror/replacing/spare vdev above oldvd.
4709 if (pvd
->vdev_ops
!= pvops
)
4710 pvd
= vdev_add_parent(oldvd
, pvops
);
4712 ASSERT(pvd
->vdev_top
->vdev_parent
== rvd
);
4713 ASSERT(pvd
->vdev_ops
== pvops
);
4714 ASSERT(oldvd
->vdev_parent
== pvd
);
4717 * Extract the new device from its root and add it to pvd.
4719 vdev_remove_child(newrootvd
, newvd
);
4720 newvd
->vdev_id
= pvd
->vdev_children
;
4721 newvd
->vdev_crtxg
= oldvd
->vdev_crtxg
;
4722 vdev_add_child(pvd
, newvd
);
4724 tvd
= newvd
->vdev_top
;
4725 ASSERT(pvd
->vdev_top
== tvd
);
4726 ASSERT(tvd
->vdev_parent
== rvd
);
4728 vdev_config_dirty(tvd
);
4731 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
4732 * for any dmu_sync-ed blocks. It will propagate upward when
4733 * spa_vdev_exit() calls vdev_dtl_reassess().
4735 dtl_max_txg
= txg
+ TXG_CONCURRENT_STATES
;
4737 vdev_dtl_dirty(newvd
, DTL_MISSING
, TXG_INITIAL
,
4738 dtl_max_txg
- TXG_INITIAL
);
4740 if (newvd
->vdev_isspare
) {
4741 spa_spare_activate(newvd
);
4742 spa_event_notify(spa
, newvd
, FM_EREPORT_ZFS_DEVICE_SPARE
);
4745 oldvdpath
= spa_strdup(oldvd
->vdev_path
);
4746 newvdpath
= spa_strdup(newvd
->vdev_path
);
4747 newvd_isspare
= newvd
->vdev_isspare
;
4750 * Mark newvd's DTL dirty in this txg.
4752 vdev_dirty(tvd
, VDD_DTL
, newvd
, txg
);
4755 * Schedule the resilver to restart in the future. We do this to
4756 * ensure that dmu_sync-ed blocks have been stitched into the
4757 * respective datasets.
4759 dsl_resilver_restart(spa
->spa_dsl_pool
, dtl_max_txg
);
4764 (void) spa_vdev_exit(spa
, newrootvd
, dtl_max_txg
, 0);
4766 spa_history_log_internal(spa
, "vdev attach", NULL
,
4767 "%s vdev=%s %s vdev=%s",
4768 replacing
&& newvd_isspare
? "spare in" :
4769 replacing
? "replace" : "attach", newvdpath
,
4770 replacing
? "for" : "to", oldvdpath
);
4772 spa_strfree(oldvdpath
);
4773 spa_strfree(newvdpath
);
4775 if (spa
->spa_bootfs
)
4776 spa_event_notify(spa
, newvd
, FM_EREPORT_ZFS_BOOTFS_VDEV_ATTACH
);
4782 * Detach a device from a mirror or replacing vdev.
4784 * If 'replace_done' is specified, only detach if the parent
4785 * is a replacing vdev.
4788 spa_vdev_detach(spa_t
*spa
, uint64_t guid
, uint64_t pguid
, int replace_done
)
4792 vdev_t
*vd
, *pvd
, *cvd
, *tvd
;
4793 boolean_t unspare
= B_FALSE
;
4794 uint64_t unspare_guid
= 0;
4797 ASSERTV(vdev_t
*rvd
= spa
->spa_root_vdev
);
4798 ASSERT(spa_writeable(spa
));
4800 txg
= spa_vdev_enter(spa
);
4802 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
4805 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
4807 if (!vd
->vdev_ops
->vdev_op_leaf
)
4808 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4810 pvd
= vd
->vdev_parent
;
4813 * If the parent/child relationship is not as expected, don't do it.
4814 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
4815 * vdev that's replacing B with C. The user's intent in replacing
4816 * is to go from M(A,B) to M(A,C). If the user decides to cancel
4817 * the replace by detaching C, the expected behavior is to end up
4818 * M(A,B). But suppose that right after deciding to detach C,
4819 * the replacement of B completes. We would have M(A,C), and then
4820 * ask to detach C, which would leave us with just A -- not what
4821 * the user wanted. To prevent this, we make sure that the
4822 * parent/child relationship hasn't changed -- in this example,
4823 * that C's parent is still the replacing vdev R.
4825 if (pvd
->vdev_guid
!= pguid
&& pguid
!= 0)
4826 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
4829 * Only 'replacing' or 'spare' vdevs can be replaced.
4831 if (replace_done
&& pvd
->vdev_ops
!= &vdev_replacing_ops
&&
4832 pvd
->vdev_ops
!= &vdev_spare_ops
)
4833 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4835 ASSERT(pvd
->vdev_ops
!= &vdev_spare_ops
||
4836 spa_version(spa
) >= SPA_VERSION_SPARES
);
4839 * Only mirror, replacing, and spare vdevs support detach.
4841 if (pvd
->vdev_ops
!= &vdev_replacing_ops
&&
4842 pvd
->vdev_ops
!= &vdev_mirror_ops
&&
4843 pvd
->vdev_ops
!= &vdev_spare_ops
)
4844 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4847 * If this device has the only valid copy of some data,
4848 * we cannot safely detach it.
4850 if (vdev_dtl_required(vd
))
4851 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
4853 ASSERT(pvd
->vdev_children
>= 2);
4856 * If we are detaching the second disk from a replacing vdev, then
4857 * check to see if we changed the original vdev's path to have "/old"
4858 * at the end in spa_vdev_attach(). If so, undo that change now.
4860 if (pvd
->vdev_ops
== &vdev_replacing_ops
&& vd
->vdev_id
> 0 &&
4861 vd
->vdev_path
!= NULL
) {
4862 size_t len
= strlen(vd
->vdev_path
);
4864 for (c
= 0; c
< pvd
->vdev_children
; c
++) {
4865 cvd
= pvd
->vdev_child
[c
];
4867 if (cvd
== vd
|| cvd
->vdev_path
== NULL
)
4870 if (strncmp(cvd
->vdev_path
, vd
->vdev_path
, len
) == 0 &&
4871 strcmp(cvd
->vdev_path
+ len
, "/old") == 0) {
4872 spa_strfree(cvd
->vdev_path
);
4873 cvd
->vdev_path
= spa_strdup(vd
->vdev_path
);
4880 * If we are detaching the original disk from a spare, then it implies
4881 * that the spare should become a real disk, and be removed from the
4882 * active spare list for the pool.
4884 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
4886 pvd
->vdev_child
[pvd
->vdev_children
- 1]->vdev_isspare
)
4890 * Erase the disk labels so the disk can be used for other things.
4891 * This must be done after all other error cases are handled,
4892 * but before we disembowel vd (so we can still do I/O to it).
4893 * But if we can't do it, don't treat the error as fatal --
4894 * it may be that the unwritability of the disk is the reason
4895 * it's being detached!
4897 error
= vdev_label_init(vd
, 0, VDEV_LABEL_REMOVE
);
4900 * Remove vd from its parent and compact the parent's children.
4902 vdev_remove_child(pvd
, vd
);
4903 vdev_compact_children(pvd
);
4906 * Remember one of the remaining children so we can get tvd below.
4908 cvd
= pvd
->vdev_child
[pvd
->vdev_children
- 1];
4911 * If we need to remove the remaining child from the list of hot spares,
4912 * do it now, marking the vdev as no longer a spare in the process.
4913 * We must do this before vdev_remove_parent(), because that can
4914 * change the GUID if it creates a new toplevel GUID. For a similar
4915 * reason, we must remove the spare now, in the same txg as the detach;
4916 * otherwise someone could attach a new sibling, change the GUID, and
4917 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
4920 ASSERT(cvd
->vdev_isspare
);
4921 spa_spare_remove(cvd
);
4922 unspare_guid
= cvd
->vdev_guid
;
4923 (void) spa_vdev_remove(spa
, unspare_guid
, B_TRUE
);
4924 cvd
->vdev_unspare
= B_TRUE
;
4928 * If the parent mirror/replacing vdev only has one child,
4929 * the parent is no longer needed. Remove it from the tree.
4931 if (pvd
->vdev_children
== 1) {
4932 if (pvd
->vdev_ops
== &vdev_spare_ops
)
4933 cvd
->vdev_unspare
= B_FALSE
;
4934 vdev_remove_parent(cvd
);
4939 * We don't set tvd until now because the parent we just removed
4940 * may have been the previous top-level vdev.
4942 tvd
= cvd
->vdev_top
;
4943 ASSERT(tvd
->vdev_parent
== rvd
);
4946 * Reevaluate the parent vdev state.
4948 vdev_propagate_state(cvd
);
4951 * If the 'autoexpand' property is set on the pool then automatically
4952 * try to expand the size of the pool. For example if the device we
4953 * just detached was smaller than the others, it may be possible to
4954 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
4955 * first so that we can obtain the updated sizes of the leaf vdevs.
4957 if (spa
->spa_autoexpand
) {
4959 vdev_expand(tvd
, txg
);
4962 vdev_config_dirty(tvd
);
4965 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
4966 * vd->vdev_detached is set and free vd's DTL object in syncing context.
4967 * But first make sure we're not on any *other* txg's DTL list, to
4968 * prevent vd from being accessed after it's freed.
4970 vdpath
= spa_strdup(vd
->vdev_path
);
4971 for (t
= 0; t
< TXG_SIZE
; t
++)
4972 (void) txg_list_remove_this(&tvd
->vdev_dtl_list
, vd
, t
);
4973 vd
->vdev_detached
= B_TRUE
;
4974 vdev_dirty(tvd
, VDD_DTL
, vd
, txg
);
4976 spa_event_notify(spa
, vd
, FM_EREPORT_ZFS_DEVICE_REMOVE
);
4978 /* hang on to the spa before we release the lock */
4979 spa_open_ref(spa
, FTAG
);
4981 error
= spa_vdev_exit(spa
, vd
, txg
, 0);
4983 spa_history_log_internal(spa
, "detach", NULL
,
4985 spa_strfree(vdpath
);
4988 * If this was the removal of the original device in a hot spare vdev,
4989 * then we want to go through and remove the device from the hot spare
4990 * list of every other pool.
4993 spa_t
*altspa
= NULL
;
4995 mutex_enter(&spa_namespace_lock
);
4996 while ((altspa
= spa_next(altspa
)) != NULL
) {
4997 if (altspa
->spa_state
!= POOL_STATE_ACTIVE
||
5001 spa_open_ref(altspa
, FTAG
);
5002 mutex_exit(&spa_namespace_lock
);
5003 (void) spa_vdev_remove(altspa
, unspare_guid
, B_TRUE
);
5004 mutex_enter(&spa_namespace_lock
);
5005 spa_close(altspa
, FTAG
);
5007 mutex_exit(&spa_namespace_lock
);
5009 /* search the rest of the vdevs for spares to remove */
5010 spa_vdev_resilver_done(spa
);
5013 /* all done with the spa; OK to release */
5014 mutex_enter(&spa_namespace_lock
);
5015 spa_close(spa
, FTAG
);
5016 mutex_exit(&spa_namespace_lock
);
5022 * Split a set of devices from their mirrors, and create a new pool from them.
5025 spa_vdev_split_mirror(spa_t
*spa
, char *newname
, nvlist_t
*config
,
5026 nvlist_t
*props
, boolean_t exp
)
5029 uint64_t txg
, *glist
;
5031 uint_t c
, children
, lastlog
;
5032 nvlist_t
**child
, *nvl
, *tmp
;
5034 char *altroot
= NULL
;
5035 vdev_t
*rvd
, **vml
= NULL
; /* vdev modify list */
5036 boolean_t activate_slog
;
5038 ASSERT(spa_writeable(spa
));
5040 txg
= spa_vdev_enter(spa
);
5042 /* clear the log and flush everything up to now */
5043 activate_slog
= spa_passivate_log(spa
);
5044 (void) spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
5045 error
= spa_offline_log(spa
);
5046 txg
= spa_vdev_config_enter(spa
);
5049 spa_activate_log(spa
);
5052 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5054 /* check new spa name before going any further */
5055 if (spa_lookup(newname
) != NULL
)
5056 return (spa_vdev_exit(spa
, NULL
, txg
, EEXIST
));
5059 * scan through all the children to ensure they're all mirrors
5061 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvl
) != 0 ||
5062 nvlist_lookup_nvlist_array(nvl
, ZPOOL_CONFIG_CHILDREN
, &child
,
5064 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
5066 /* first, check to ensure we've got the right child count */
5067 rvd
= spa
->spa_root_vdev
;
5069 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
5070 vdev_t
*vd
= rvd
->vdev_child
[c
];
5072 /* don't count the holes & logs as children */
5073 if (vd
->vdev_islog
|| vd
->vdev_ishole
) {
5081 if (children
!= (lastlog
!= 0 ? lastlog
: rvd
->vdev_children
))
5082 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
5084 /* next, ensure no spare or cache devices are part of the split */
5085 if (nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_SPARES
, &tmp
) == 0 ||
5086 nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_L2CACHE
, &tmp
) == 0)
5087 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
5089 vml
= kmem_zalloc(children
* sizeof (vdev_t
*), KM_SLEEP
);
5090 glist
= kmem_zalloc(children
* sizeof (uint64_t), KM_SLEEP
);
5092 /* then, loop over each vdev and validate it */
5093 for (c
= 0; c
< children
; c
++) {
5094 uint64_t is_hole
= 0;
5096 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
5100 if (spa
->spa_root_vdev
->vdev_child
[c
]->vdev_ishole
||
5101 spa
->spa_root_vdev
->vdev_child
[c
]->vdev_islog
) {
5104 error
= SET_ERROR(EINVAL
);
5109 /* which disk is going to be split? */
5110 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_GUID
,
5112 error
= SET_ERROR(EINVAL
);
5116 /* look it up in the spa */
5117 vml
[c
] = spa_lookup_by_guid(spa
, glist
[c
], B_FALSE
);
5118 if (vml
[c
] == NULL
) {
5119 error
= SET_ERROR(ENODEV
);
5123 /* make sure there's nothing stopping the split */
5124 if (vml
[c
]->vdev_parent
->vdev_ops
!= &vdev_mirror_ops
||
5125 vml
[c
]->vdev_islog
||
5126 vml
[c
]->vdev_ishole
||
5127 vml
[c
]->vdev_isspare
||
5128 vml
[c
]->vdev_isl2cache
||
5129 !vdev_writeable(vml
[c
]) ||
5130 vml
[c
]->vdev_children
!= 0 ||
5131 vml
[c
]->vdev_state
!= VDEV_STATE_HEALTHY
||
5132 c
!= spa
->spa_root_vdev
->vdev_child
[c
]->vdev_id
) {
5133 error
= SET_ERROR(EINVAL
);
5137 if (vdev_dtl_required(vml
[c
])) {
5138 error
= SET_ERROR(EBUSY
);
5142 /* we need certain info from the top level */
5143 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_ARRAY
,
5144 vml
[c
]->vdev_top
->vdev_ms_array
) == 0);
5145 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_SHIFT
,
5146 vml
[c
]->vdev_top
->vdev_ms_shift
) == 0);
5147 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASIZE
,
5148 vml
[c
]->vdev_top
->vdev_asize
) == 0);
5149 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASHIFT
,
5150 vml
[c
]->vdev_top
->vdev_ashift
) == 0);
5154 kmem_free(vml
, children
* sizeof (vdev_t
*));
5155 kmem_free(glist
, children
* sizeof (uint64_t));
5156 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5159 /* stop writers from using the disks */
5160 for (c
= 0; c
< children
; c
++) {
5162 vml
[c
]->vdev_offline
= B_TRUE
;
5164 vdev_reopen(spa
->spa_root_vdev
);
5167 * Temporarily record the splitting vdevs in the spa config. This
5168 * will disappear once the config is regenerated.
5170 VERIFY(nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
5171 VERIFY(nvlist_add_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
5172 glist
, children
) == 0);
5173 kmem_free(glist
, children
* sizeof (uint64_t));
5175 mutex_enter(&spa
->spa_props_lock
);
5176 VERIFY(nvlist_add_nvlist(spa
->spa_config
, ZPOOL_CONFIG_SPLIT
,
5178 mutex_exit(&spa
->spa_props_lock
);
5179 spa
->spa_config_splitting
= nvl
;
5180 vdev_config_dirty(spa
->spa_root_vdev
);
5182 /* configure and create the new pool */
5183 VERIFY(nvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
, newname
) == 0);
5184 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
5185 exp
? POOL_STATE_EXPORTED
: POOL_STATE_ACTIVE
) == 0);
5186 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
5187 spa_version(spa
)) == 0);
5188 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
5189 spa
->spa_config_txg
) == 0);
5190 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
5191 spa_generate_guid(NULL
)) == 0);
5192 (void) nvlist_lookup_string(props
,
5193 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
5195 /* add the new pool to the namespace */
5196 newspa
= spa_add(newname
, config
, altroot
);
5197 newspa
->spa_config_txg
= spa
->spa_config_txg
;
5198 spa_set_log_state(newspa
, SPA_LOG_CLEAR
);
5200 /* release the spa config lock, retaining the namespace lock */
5201 spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
5203 if (zio_injection_enabled
)
5204 zio_handle_panic_injection(spa
, FTAG
, 1);
5206 spa_activate(newspa
, spa_mode_global
);
5207 spa_async_suspend(newspa
);
5209 /* create the new pool from the disks of the original pool */
5210 error
= spa_load(newspa
, SPA_LOAD_IMPORT
, SPA_IMPORT_ASSEMBLE
, B_TRUE
);
5214 /* if that worked, generate a real config for the new pool */
5215 if (newspa
->spa_root_vdev
!= NULL
) {
5216 VERIFY(nvlist_alloc(&newspa
->spa_config_splitting
,
5217 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
5218 VERIFY(nvlist_add_uint64(newspa
->spa_config_splitting
,
5219 ZPOOL_CONFIG_SPLIT_GUID
, spa_guid(spa
)) == 0);
5220 spa_config_set(newspa
, spa_config_generate(newspa
, NULL
, -1ULL,
5225 if (props
!= NULL
) {
5226 spa_configfile_set(newspa
, props
, B_FALSE
);
5227 error
= spa_prop_set(newspa
, props
);
5232 /* flush everything */
5233 txg
= spa_vdev_config_enter(newspa
);
5234 vdev_config_dirty(newspa
->spa_root_vdev
);
5235 (void) spa_vdev_config_exit(newspa
, NULL
, txg
, 0, FTAG
);
5237 if (zio_injection_enabled
)
5238 zio_handle_panic_injection(spa
, FTAG
, 2);
5240 spa_async_resume(newspa
);
5242 /* finally, update the original pool's config */
5243 txg
= spa_vdev_config_enter(spa
);
5244 tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
5245 error
= dmu_tx_assign(tx
, TXG_WAIT
);
5248 for (c
= 0; c
< children
; c
++) {
5249 if (vml
[c
] != NULL
) {
5252 spa_history_log_internal(spa
, "detach", tx
,
5253 "vdev=%s", vml
[c
]->vdev_path
);
5257 vdev_config_dirty(spa
->spa_root_vdev
);
5258 spa
->spa_config_splitting
= NULL
;
5262 (void) spa_vdev_exit(spa
, NULL
, txg
, 0);
5264 if (zio_injection_enabled
)
5265 zio_handle_panic_injection(spa
, FTAG
, 3);
5267 /* split is complete; log a history record */
5268 spa_history_log_internal(newspa
, "split", NULL
,
5269 "from pool %s", spa_name(spa
));
5271 kmem_free(vml
, children
* sizeof (vdev_t
*));
5273 /* if we're not going to mount the filesystems in userland, export */
5275 error
= spa_export_common(newname
, POOL_STATE_EXPORTED
, NULL
,
5282 spa_deactivate(newspa
);
5285 txg
= spa_vdev_config_enter(spa
);
5287 /* re-online all offlined disks */
5288 for (c
= 0; c
< children
; c
++) {
5290 vml
[c
]->vdev_offline
= B_FALSE
;
5292 vdev_reopen(spa
->spa_root_vdev
);
5294 nvlist_free(spa
->spa_config_splitting
);
5295 spa
->spa_config_splitting
= NULL
;
5296 (void) spa_vdev_exit(spa
, NULL
, txg
, error
);
5298 kmem_free(vml
, children
* sizeof (vdev_t
*));
5303 spa_nvlist_lookup_by_guid(nvlist_t
**nvpp
, int count
, uint64_t target_guid
)
5307 for (i
= 0; i
< count
; i
++) {
5310 VERIFY(nvlist_lookup_uint64(nvpp
[i
], ZPOOL_CONFIG_GUID
,
5313 if (guid
== target_guid
)
5321 spa_vdev_remove_aux(nvlist_t
*config
, char *name
, nvlist_t
**dev
, int count
,
5322 nvlist_t
*dev_to_remove
)
5324 nvlist_t
**newdev
= NULL
;
5328 newdev
= kmem_alloc((count
- 1) * sizeof (void *), KM_SLEEP
);
5330 for (i
= 0, j
= 0; i
< count
; i
++) {
5331 if (dev
[i
] == dev_to_remove
)
5333 VERIFY(nvlist_dup(dev
[i
], &newdev
[j
++], KM_SLEEP
) == 0);
5336 VERIFY(nvlist_remove(config
, name
, DATA_TYPE_NVLIST_ARRAY
) == 0);
5337 VERIFY(nvlist_add_nvlist_array(config
, name
, newdev
, count
- 1) == 0);
5339 for (i
= 0; i
< count
- 1; i
++)
5340 nvlist_free(newdev
[i
]);
5343 kmem_free(newdev
, (count
- 1) * sizeof (void *));
5347 * Evacuate the device.
5350 spa_vdev_remove_evacuate(spa_t
*spa
, vdev_t
*vd
)
5355 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
5356 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
5357 ASSERT(vd
== vd
->vdev_top
);
5360 * Evacuate the device. We don't hold the config lock as writer
5361 * since we need to do I/O but we do keep the
5362 * spa_namespace_lock held. Once this completes the device
5363 * should no longer have any blocks allocated on it.
5365 if (vd
->vdev_islog
) {
5366 if (vd
->vdev_stat
.vs_alloc
!= 0)
5367 error
= spa_offline_log(spa
);
5369 error
= SET_ERROR(ENOTSUP
);
5376 * The evacuation succeeded. Remove any remaining MOS metadata
5377 * associated with this vdev, and wait for these changes to sync.
5379 ASSERT0(vd
->vdev_stat
.vs_alloc
);
5380 txg
= spa_vdev_config_enter(spa
);
5381 vd
->vdev_removing
= B_TRUE
;
5382 vdev_dirty_leaves(vd
, VDD_DTL
, txg
);
5383 vdev_config_dirty(vd
);
5384 spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
5390 * Complete the removal by cleaning up the namespace.
5393 spa_vdev_remove_from_namespace(spa_t
*spa
, vdev_t
*vd
)
5395 vdev_t
*rvd
= spa
->spa_root_vdev
;
5396 uint64_t id
= vd
->vdev_id
;
5397 boolean_t last_vdev
= (id
== (rvd
->vdev_children
- 1));
5399 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
5400 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
5401 ASSERT(vd
== vd
->vdev_top
);
5404 * Only remove any devices which are empty.
5406 if (vd
->vdev_stat
.vs_alloc
!= 0)
5409 (void) vdev_label_init(vd
, 0, VDEV_LABEL_REMOVE
);
5411 if (list_link_active(&vd
->vdev_state_dirty_node
))
5412 vdev_state_clean(vd
);
5413 if (list_link_active(&vd
->vdev_config_dirty_node
))
5414 vdev_config_clean(vd
);
5419 vdev_compact_children(rvd
);
5421 vd
= vdev_alloc_common(spa
, id
, 0, &vdev_hole_ops
);
5422 vdev_add_child(rvd
, vd
);
5424 vdev_config_dirty(rvd
);
5427 * Reassess the health of our root vdev.
5433 * Remove a device from the pool -
5435 * Removing a device from the vdev namespace requires several steps
5436 * and can take a significant amount of time. As a result we use
5437 * the spa_vdev_config_[enter/exit] functions which allow us to
5438 * grab and release the spa_config_lock while still holding the namespace
5439 * lock. During each step the configuration is synced out.
5441 * Currently, this supports removing only hot spares, slogs, and level 2 ARC
5445 spa_vdev_remove(spa_t
*spa
, uint64_t guid
, boolean_t unspare
)
5448 metaslab_group_t
*mg
;
5449 nvlist_t
**spares
, **l2cache
, *nv
;
5451 uint_t nspares
, nl2cache
;
5453 boolean_t locked
= MUTEX_HELD(&spa_namespace_lock
);
5455 ASSERT(spa_writeable(spa
));
5458 txg
= spa_vdev_enter(spa
);
5460 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
5462 if (spa
->spa_spares
.sav_vdevs
!= NULL
&&
5463 nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
5464 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0 &&
5465 (nv
= spa_nvlist_lookup_by_guid(spares
, nspares
, guid
)) != NULL
) {
5467 * Only remove the hot spare if it's not currently in use
5470 if (vd
== NULL
|| unspare
) {
5471 spa_vdev_remove_aux(spa
->spa_spares
.sav_config
,
5472 ZPOOL_CONFIG_SPARES
, spares
, nspares
, nv
);
5473 spa_load_spares(spa
);
5474 spa
->spa_spares
.sav_sync
= B_TRUE
;
5476 error
= SET_ERROR(EBUSY
);
5478 } else if (spa
->spa_l2cache
.sav_vdevs
!= NULL
&&
5479 nvlist_lookup_nvlist_array(spa
->spa_l2cache
.sav_config
,
5480 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0 &&
5481 (nv
= spa_nvlist_lookup_by_guid(l2cache
, nl2cache
, guid
)) != NULL
) {
5483 * Cache devices can always be removed.
5485 spa_vdev_remove_aux(spa
->spa_l2cache
.sav_config
,
5486 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
, nv
);
5487 spa_load_l2cache(spa
);
5488 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
5489 } else if (vd
!= NULL
&& vd
->vdev_islog
) {
5491 ASSERT(vd
== vd
->vdev_top
);
5496 * Stop allocating from this vdev.
5498 metaslab_group_passivate(mg
);
5501 * Wait for the youngest allocations and frees to sync,
5502 * and then wait for the deferral of those frees to finish.
5504 spa_vdev_config_exit(spa
, NULL
,
5505 txg
+ TXG_CONCURRENT_STATES
+ TXG_DEFER_SIZE
, 0, FTAG
);
5508 * Attempt to evacuate the vdev.
5510 error
= spa_vdev_remove_evacuate(spa
, vd
);
5512 txg
= spa_vdev_config_enter(spa
);
5515 * If we couldn't evacuate the vdev, unwind.
5518 metaslab_group_activate(mg
);
5519 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5523 * Clean up the vdev namespace.
5525 spa_vdev_remove_from_namespace(spa
, vd
);
5527 } else if (vd
!= NULL
) {
5529 * Normal vdevs cannot be removed (yet).
5531 error
= SET_ERROR(ENOTSUP
);
5534 * There is no vdev of any kind with the specified guid.
5536 error
= SET_ERROR(ENOENT
);
5540 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5546 * Find any device that's done replacing, or a vdev marked 'unspare' that's
5547 * currently spared, so we can detach it.
5550 spa_vdev_resilver_done_hunt(vdev_t
*vd
)
5552 vdev_t
*newvd
, *oldvd
;
5555 for (c
= 0; c
< vd
->vdev_children
; c
++) {
5556 oldvd
= spa_vdev_resilver_done_hunt(vd
->vdev_child
[c
]);
5562 * Check for a completed replacement. We always consider the first
5563 * vdev in the list to be the oldest vdev, and the last one to be
5564 * the newest (see spa_vdev_attach() for how that works). In
5565 * the case where the newest vdev is faulted, we will not automatically
5566 * remove it after a resilver completes. This is OK as it will require
5567 * user intervention to determine which disk the admin wishes to keep.
5569 if (vd
->vdev_ops
== &vdev_replacing_ops
) {
5570 ASSERT(vd
->vdev_children
> 1);
5572 newvd
= vd
->vdev_child
[vd
->vdev_children
- 1];
5573 oldvd
= vd
->vdev_child
[0];
5575 if (vdev_dtl_empty(newvd
, DTL_MISSING
) &&
5576 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
5577 !vdev_dtl_required(oldvd
))
5582 * Check for a completed resilver with the 'unspare' flag set.
5584 if (vd
->vdev_ops
== &vdev_spare_ops
) {
5585 vdev_t
*first
= vd
->vdev_child
[0];
5586 vdev_t
*last
= vd
->vdev_child
[vd
->vdev_children
- 1];
5588 if (last
->vdev_unspare
) {
5591 } else if (first
->vdev_unspare
) {
5598 if (oldvd
!= NULL
&&
5599 vdev_dtl_empty(newvd
, DTL_MISSING
) &&
5600 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
5601 !vdev_dtl_required(oldvd
))
5605 * If there are more than two spares attached to a disk,
5606 * and those spares are not required, then we want to
5607 * attempt to free them up now so that they can be used
5608 * by other pools. Once we're back down to a single
5609 * disk+spare, we stop removing them.
5611 if (vd
->vdev_children
> 2) {
5612 newvd
= vd
->vdev_child
[1];
5614 if (newvd
->vdev_isspare
&& last
->vdev_isspare
&&
5615 vdev_dtl_empty(last
, DTL_MISSING
) &&
5616 vdev_dtl_empty(last
, DTL_OUTAGE
) &&
5617 !vdev_dtl_required(newvd
))
5626 spa_vdev_resilver_done(spa_t
*spa
)
5628 vdev_t
*vd
, *pvd
, *ppvd
;
5629 uint64_t guid
, sguid
, pguid
, ppguid
;
5631 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5633 while ((vd
= spa_vdev_resilver_done_hunt(spa
->spa_root_vdev
)) != NULL
) {
5634 pvd
= vd
->vdev_parent
;
5635 ppvd
= pvd
->vdev_parent
;
5636 guid
= vd
->vdev_guid
;
5637 pguid
= pvd
->vdev_guid
;
5638 ppguid
= ppvd
->vdev_guid
;
5641 * If we have just finished replacing a hot spared device, then
5642 * we need to detach the parent's first child (the original hot
5645 if (ppvd
->vdev_ops
== &vdev_spare_ops
&& pvd
->vdev_id
== 0 &&
5646 ppvd
->vdev_children
== 2) {
5647 ASSERT(pvd
->vdev_ops
== &vdev_replacing_ops
);
5648 sguid
= ppvd
->vdev_child
[1]->vdev_guid
;
5650 ASSERT(vd
->vdev_resilver_txg
== 0 || !vdev_dtl_required(vd
));
5652 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5653 if (spa_vdev_detach(spa
, guid
, pguid
, B_TRUE
) != 0)
5655 if (sguid
&& spa_vdev_detach(spa
, sguid
, ppguid
, B_TRUE
) != 0)
5657 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5660 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5664 * Update the stored path or FRU for this vdev.
5667 spa_vdev_set_common(spa_t
*spa
, uint64_t guid
, const char *value
,
5671 boolean_t sync
= B_FALSE
;
5673 ASSERT(spa_writeable(spa
));
5675 spa_vdev_state_enter(spa
, SCL_ALL
);
5677 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
5678 return (spa_vdev_state_exit(spa
, NULL
, ENOENT
));
5680 if (!vd
->vdev_ops
->vdev_op_leaf
)
5681 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
5684 if (strcmp(value
, vd
->vdev_path
) != 0) {
5685 spa_strfree(vd
->vdev_path
);
5686 vd
->vdev_path
= spa_strdup(value
);
5690 if (vd
->vdev_fru
== NULL
) {
5691 vd
->vdev_fru
= spa_strdup(value
);
5693 } else if (strcmp(value
, vd
->vdev_fru
) != 0) {
5694 spa_strfree(vd
->vdev_fru
);
5695 vd
->vdev_fru
= spa_strdup(value
);
5700 return (spa_vdev_state_exit(spa
, sync
? vd
: NULL
, 0));
5704 spa_vdev_setpath(spa_t
*spa
, uint64_t guid
, const char *newpath
)
5706 return (spa_vdev_set_common(spa
, guid
, newpath
, B_TRUE
));
5710 spa_vdev_setfru(spa_t
*spa
, uint64_t guid
, const char *newfru
)
5712 return (spa_vdev_set_common(spa
, guid
, newfru
, B_FALSE
));
5716 * ==========================================================================
5718 * ==========================================================================
5722 spa_scan_stop(spa_t
*spa
)
5724 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
5725 if (dsl_scan_resilvering(spa
->spa_dsl_pool
))
5726 return (SET_ERROR(EBUSY
));
5727 return (dsl_scan_cancel(spa
->spa_dsl_pool
));
5731 spa_scan(spa_t
*spa
, pool_scan_func_t func
)
5733 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
5735 if (func
>= POOL_SCAN_FUNCS
|| func
== POOL_SCAN_NONE
)
5736 return (SET_ERROR(ENOTSUP
));
5739 * If a resilver was requested, but there is no DTL on a
5740 * writeable leaf device, we have nothing to do.
5742 if (func
== POOL_SCAN_RESILVER
&&
5743 !vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
)) {
5744 spa_async_request(spa
, SPA_ASYNC_RESILVER_DONE
);
5748 return (dsl_scan(spa
->spa_dsl_pool
, func
));
5752 * ==========================================================================
5753 * SPA async task processing
5754 * ==========================================================================
5758 spa_async_remove(spa_t
*spa
, vdev_t
*vd
)
5762 if (vd
->vdev_remove_wanted
) {
5763 vd
->vdev_remove_wanted
= B_FALSE
;
5764 vd
->vdev_delayed_close
= B_FALSE
;
5765 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_REMOVED
, VDEV_AUX_NONE
);
5768 * We want to clear the stats, but we don't want to do a full
5769 * vdev_clear() as that will cause us to throw away
5770 * degraded/faulted state as well as attempt to reopen the
5771 * device, all of which is a waste.
5773 vd
->vdev_stat
.vs_read_errors
= 0;
5774 vd
->vdev_stat
.vs_write_errors
= 0;
5775 vd
->vdev_stat
.vs_checksum_errors
= 0;
5777 vdev_state_dirty(vd
->vdev_top
);
5780 for (c
= 0; c
< vd
->vdev_children
; c
++)
5781 spa_async_remove(spa
, vd
->vdev_child
[c
]);
5785 spa_async_probe(spa_t
*spa
, vdev_t
*vd
)
5789 if (vd
->vdev_probe_wanted
) {
5790 vd
->vdev_probe_wanted
= B_FALSE
;
5791 vdev_reopen(vd
); /* vdev_open() does the actual probe */
5794 for (c
= 0; c
< vd
->vdev_children
; c
++)
5795 spa_async_probe(spa
, vd
->vdev_child
[c
]);
5799 spa_async_autoexpand(spa_t
*spa
, vdev_t
*vd
)
5803 if (!spa
->spa_autoexpand
)
5806 for (c
= 0; c
< vd
->vdev_children
; c
++) {
5807 vdev_t
*cvd
= vd
->vdev_child
[c
];
5808 spa_async_autoexpand(spa
, cvd
);
5811 if (!vd
->vdev_ops
->vdev_op_leaf
|| vd
->vdev_physpath
== NULL
)
5814 spa_event_notify(vd
->vdev_spa
, vd
, FM_EREPORT_ZFS_DEVICE_AUTOEXPAND
);
5818 spa_async_thread(spa_t
*spa
)
5822 ASSERT(spa
->spa_sync_on
);
5824 mutex_enter(&spa
->spa_async_lock
);
5825 tasks
= spa
->spa_async_tasks
;
5826 spa
->spa_async_tasks
= 0;
5827 mutex_exit(&spa
->spa_async_lock
);
5830 * See if the config needs to be updated.
5832 if (tasks
& SPA_ASYNC_CONFIG_UPDATE
) {
5833 uint64_t old_space
, new_space
;
5835 mutex_enter(&spa_namespace_lock
);
5836 old_space
= metaslab_class_get_space(spa_normal_class(spa
));
5837 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
5838 new_space
= metaslab_class_get_space(spa_normal_class(spa
));
5839 mutex_exit(&spa_namespace_lock
);
5842 * If the pool grew as a result of the config update,
5843 * then log an internal history event.
5845 if (new_space
!= old_space
) {
5846 spa_history_log_internal(spa
, "vdev online", NULL
,
5847 "pool '%s' size: %llu(+%llu)",
5848 spa_name(spa
), new_space
, new_space
- old_space
);
5853 * See if any devices need to be marked REMOVED.
5855 if (tasks
& SPA_ASYNC_REMOVE
) {
5856 spa_vdev_state_enter(spa
, SCL_NONE
);
5857 spa_async_remove(spa
, spa
->spa_root_vdev
);
5858 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++)
5859 spa_async_remove(spa
, spa
->spa_l2cache
.sav_vdevs
[i
]);
5860 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
5861 spa_async_remove(spa
, spa
->spa_spares
.sav_vdevs
[i
]);
5862 (void) spa_vdev_state_exit(spa
, NULL
, 0);
5865 if ((tasks
& SPA_ASYNC_AUTOEXPAND
) && !spa_suspended(spa
)) {
5866 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
5867 spa_async_autoexpand(spa
, spa
->spa_root_vdev
);
5868 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
5872 * See if any devices need to be probed.
5874 if (tasks
& SPA_ASYNC_PROBE
) {
5875 spa_vdev_state_enter(spa
, SCL_NONE
);
5876 spa_async_probe(spa
, spa
->spa_root_vdev
);
5877 (void) spa_vdev_state_exit(spa
, NULL
, 0);
5881 * If any devices are done replacing, detach them.
5883 if (tasks
& SPA_ASYNC_RESILVER_DONE
)
5884 spa_vdev_resilver_done(spa
);
5887 * Kick off a resilver.
5889 if (tasks
& SPA_ASYNC_RESILVER
)
5890 dsl_resilver_restart(spa
->spa_dsl_pool
, 0);
5893 * Let the world know that we're done.
5895 mutex_enter(&spa
->spa_async_lock
);
5896 spa
->spa_async_thread
= NULL
;
5897 cv_broadcast(&spa
->spa_async_cv
);
5898 mutex_exit(&spa
->spa_async_lock
);
5903 spa_async_suspend(spa_t
*spa
)
5905 mutex_enter(&spa
->spa_async_lock
);
5906 spa
->spa_async_suspended
++;
5907 while (spa
->spa_async_thread
!= NULL
)
5908 cv_wait(&spa
->spa_async_cv
, &spa
->spa_async_lock
);
5909 mutex_exit(&spa
->spa_async_lock
);
5913 spa_async_resume(spa_t
*spa
)
5915 mutex_enter(&spa
->spa_async_lock
);
5916 ASSERT(spa
->spa_async_suspended
!= 0);
5917 spa
->spa_async_suspended
--;
5918 mutex_exit(&spa
->spa_async_lock
);
5922 spa_async_tasks_pending(spa_t
*spa
)
5924 uint_t non_config_tasks
;
5926 boolean_t config_task_suspended
;
5928 non_config_tasks
= spa
->spa_async_tasks
& ~SPA_ASYNC_CONFIG_UPDATE
;
5929 config_task
= spa
->spa_async_tasks
& SPA_ASYNC_CONFIG_UPDATE
;
5930 if (spa
->spa_ccw_fail_time
== 0) {
5931 config_task_suspended
= B_FALSE
;
5933 config_task_suspended
=
5934 (gethrtime() - spa
->spa_ccw_fail_time
) <
5935 (zfs_ccw_retry_interval
* NANOSEC
);
5938 return (non_config_tasks
|| (config_task
&& !config_task_suspended
));
5942 spa_async_dispatch(spa_t
*spa
)
5944 mutex_enter(&spa
->spa_async_lock
);
5945 if (spa_async_tasks_pending(spa
) &&
5946 !spa
->spa_async_suspended
&&
5947 spa
->spa_async_thread
== NULL
&&
5949 spa
->spa_async_thread
= thread_create(NULL
, 0,
5950 spa_async_thread
, spa
, 0, &p0
, TS_RUN
, maxclsyspri
);
5951 mutex_exit(&spa
->spa_async_lock
);
5955 spa_async_request(spa_t
*spa
, int task
)
5957 zfs_dbgmsg("spa=%s async request task=%u", spa
->spa_name
, task
);
5958 mutex_enter(&spa
->spa_async_lock
);
5959 spa
->spa_async_tasks
|= task
;
5960 mutex_exit(&spa
->spa_async_lock
);
5964 * ==========================================================================
5965 * SPA syncing routines
5966 * ==========================================================================
5970 bpobj_enqueue_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
5973 bpobj_enqueue(bpo
, bp
, tx
);
5978 spa_free_sync_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
5982 zio_nowait(zio_free_sync(zio
, zio
->io_spa
, dmu_tx_get_txg(tx
), bp
,
5988 * Note: this simple function is not inlined to make it easier to dtrace the
5989 * amount of time spent syncing frees.
5992 spa_sync_frees(spa_t
*spa
, bplist_t
*bpl
, dmu_tx_t
*tx
)
5994 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
5995 bplist_iterate(bpl
, spa_free_sync_cb
, zio
, tx
);
5996 VERIFY(zio_wait(zio
) == 0);
6000 * Note: this simple function is not inlined to make it easier to dtrace the
6001 * amount of time spent syncing deferred frees.
6004 spa_sync_deferred_frees(spa_t
*spa
, dmu_tx_t
*tx
)
6006 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
6007 VERIFY3U(bpobj_iterate(&spa
->spa_deferred_bpobj
,
6008 spa_free_sync_cb
, zio
, tx
), ==, 0);
6009 VERIFY0(zio_wait(zio
));
6013 spa_sync_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
*nv
, dmu_tx_t
*tx
)
6015 char *packed
= NULL
;
6020 VERIFY(nvlist_size(nv
, &nvsize
, NV_ENCODE_XDR
) == 0);
6023 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
6024 * information. This avoids the dmu_buf_will_dirty() path and
6025 * saves us a pre-read to get data we don't actually care about.
6027 bufsize
= P2ROUNDUP((uint64_t)nvsize
, SPA_CONFIG_BLOCKSIZE
);
6028 packed
= vmem_alloc(bufsize
, KM_SLEEP
);
6030 VERIFY(nvlist_pack(nv
, &packed
, &nvsize
, NV_ENCODE_XDR
,
6032 bzero(packed
+ nvsize
, bufsize
- nvsize
);
6034 dmu_write(spa
->spa_meta_objset
, obj
, 0, bufsize
, packed
, tx
);
6036 vmem_free(packed
, bufsize
);
6038 VERIFY(0 == dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
));
6039 dmu_buf_will_dirty(db
, tx
);
6040 *(uint64_t *)db
->db_data
= nvsize
;
6041 dmu_buf_rele(db
, FTAG
);
6045 spa_sync_aux_dev(spa_t
*spa
, spa_aux_vdev_t
*sav
, dmu_tx_t
*tx
,
6046 const char *config
, const char *entry
)
6056 * Update the MOS nvlist describing the list of available devices.
6057 * spa_validate_aux() will have already made sure this nvlist is
6058 * valid and the vdevs are labeled appropriately.
6060 if (sav
->sav_object
== 0) {
6061 sav
->sav_object
= dmu_object_alloc(spa
->spa_meta_objset
,
6062 DMU_OT_PACKED_NVLIST
, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE
,
6063 sizeof (uint64_t), tx
);
6064 VERIFY(zap_update(spa
->spa_meta_objset
,
6065 DMU_POOL_DIRECTORY_OBJECT
, entry
, sizeof (uint64_t), 1,
6066 &sav
->sav_object
, tx
) == 0);
6069 VERIFY(nvlist_alloc(&nvroot
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
6070 if (sav
->sav_count
== 0) {
6071 VERIFY(nvlist_add_nvlist_array(nvroot
, config
, NULL
, 0) == 0);
6073 list
= kmem_alloc(sav
->sav_count
*sizeof (void *), KM_SLEEP
);
6074 for (i
= 0; i
< sav
->sav_count
; i
++)
6075 list
[i
] = vdev_config_generate(spa
, sav
->sav_vdevs
[i
],
6076 B_FALSE
, VDEV_CONFIG_L2CACHE
);
6077 VERIFY(nvlist_add_nvlist_array(nvroot
, config
, list
,
6078 sav
->sav_count
) == 0);
6079 for (i
= 0; i
< sav
->sav_count
; i
++)
6080 nvlist_free(list
[i
]);
6081 kmem_free(list
, sav
->sav_count
* sizeof (void *));
6084 spa_sync_nvlist(spa
, sav
->sav_object
, nvroot
, tx
);
6085 nvlist_free(nvroot
);
6087 sav
->sav_sync
= B_FALSE
;
6091 spa_sync_config_object(spa_t
*spa
, dmu_tx_t
*tx
)
6095 if (list_is_empty(&spa
->spa_config_dirty_list
))
6098 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
6100 config
= spa_config_generate(spa
, spa
->spa_root_vdev
,
6101 dmu_tx_get_txg(tx
), B_FALSE
);
6104 * If we're upgrading the spa version then make sure that
6105 * the config object gets updated with the correct version.
6107 if (spa
->spa_ubsync
.ub_version
< spa
->spa_uberblock
.ub_version
)
6108 fnvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
6109 spa
->spa_uberblock
.ub_version
);
6111 spa_config_exit(spa
, SCL_STATE
, FTAG
);
6113 if (spa
->spa_config_syncing
)
6114 nvlist_free(spa
->spa_config_syncing
);
6115 spa
->spa_config_syncing
= config
;
6117 spa_sync_nvlist(spa
, spa
->spa_config_object
, config
, tx
);
6121 spa_sync_version(void *arg
, dmu_tx_t
*tx
)
6123 uint64_t *versionp
= arg
;
6124 uint64_t version
= *versionp
;
6125 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
6128 * Setting the version is special cased when first creating the pool.
6130 ASSERT(tx
->tx_txg
!= TXG_INITIAL
);
6132 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
6133 ASSERT(version
>= spa_version(spa
));
6135 spa
->spa_uberblock
.ub_version
= version
;
6136 vdev_config_dirty(spa
->spa_root_vdev
);
6137 spa_history_log_internal(spa
, "set", tx
, "version=%lld", version
);
6141 * Set zpool properties.
6144 spa_sync_props(void *arg
, dmu_tx_t
*tx
)
6146 nvlist_t
*nvp
= arg
;
6147 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
6148 objset_t
*mos
= spa
->spa_meta_objset
;
6149 nvpair_t
*elem
= NULL
;
6151 mutex_enter(&spa
->spa_props_lock
);
6153 while ((elem
= nvlist_next_nvpair(nvp
, elem
))) {
6155 char *strval
, *fname
;
6157 const char *propname
;
6158 zprop_type_t proptype
;
6161 prop
= zpool_name_to_prop(nvpair_name(elem
));
6162 switch ((int)prop
) {
6165 * We checked this earlier in spa_prop_validate().
6167 ASSERT(zpool_prop_feature(nvpair_name(elem
)));
6169 fname
= strchr(nvpair_name(elem
), '@') + 1;
6170 VERIFY0(zfeature_lookup_name(fname
, &fid
));
6172 spa_feature_enable(spa
, fid
, tx
);
6173 spa_history_log_internal(spa
, "set", tx
,
6174 "%s=enabled", nvpair_name(elem
));
6177 case ZPOOL_PROP_VERSION
:
6178 intval
= fnvpair_value_uint64(elem
);
6180 * The version is synced seperatly before other
6181 * properties and should be correct by now.
6183 ASSERT3U(spa_version(spa
), >=, intval
);
6186 case ZPOOL_PROP_ALTROOT
:
6188 * 'altroot' is a non-persistent property. It should
6189 * have been set temporarily at creation or import time.
6191 ASSERT(spa
->spa_root
!= NULL
);
6194 case ZPOOL_PROP_READONLY
:
6195 case ZPOOL_PROP_CACHEFILE
:
6197 * 'readonly' and 'cachefile' are also non-persisitent
6201 case ZPOOL_PROP_COMMENT
:
6202 strval
= fnvpair_value_string(elem
);
6203 if (spa
->spa_comment
!= NULL
)
6204 spa_strfree(spa
->spa_comment
);
6205 spa
->spa_comment
= spa_strdup(strval
);
6207 * We need to dirty the configuration on all the vdevs
6208 * so that their labels get updated. It's unnecessary
6209 * to do this for pool creation since the vdev's
6210 * configuratoin has already been dirtied.
6212 if (tx
->tx_txg
!= TXG_INITIAL
)
6213 vdev_config_dirty(spa
->spa_root_vdev
);
6214 spa_history_log_internal(spa
, "set", tx
,
6215 "%s=%s", nvpair_name(elem
), strval
);
6219 * Set pool property values in the poolprops mos object.
6221 if (spa
->spa_pool_props_object
== 0) {
6222 spa
->spa_pool_props_object
=
6223 zap_create_link(mos
, DMU_OT_POOL_PROPS
,
6224 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_PROPS
,
6228 /* normalize the property name */
6229 propname
= zpool_prop_to_name(prop
);
6230 proptype
= zpool_prop_get_type(prop
);
6232 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
6233 ASSERT(proptype
== PROP_TYPE_STRING
);
6234 strval
= fnvpair_value_string(elem
);
6235 VERIFY0(zap_update(mos
,
6236 spa
->spa_pool_props_object
, propname
,
6237 1, strlen(strval
) + 1, strval
, tx
));
6238 spa_history_log_internal(spa
, "set", tx
,
6239 "%s=%s", nvpair_name(elem
), strval
);
6240 } else if (nvpair_type(elem
) == DATA_TYPE_UINT64
) {
6241 intval
= fnvpair_value_uint64(elem
);
6243 if (proptype
== PROP_TYPE_INDEX
) {
6245 VERIFY0(zpool_prop_index_to_string(
6246 prop
, intval
, &unused
));
6248 VERIFY0(zap_update(mos
,
6249 spa
->spa_pool_props_object
, propname
,
6250 8, 1, &intval
, tx
));
6251 spa_history_log_internal(spa
, "set", tx
,
6252 "%s=%lld", nvpair_name(elem
), intval
);
6254 ASSERT(0); /* not allowed */
6258 case ZPOOL_PROP_DELEGATION
:
6259 spa
->spa_delegation
= intval
;
6261 case ZPOOL_PROP_BOOTFS
:
6262 spa
->spa_bootfs
= intval
;
6264 case ZPOOL_PROP_FAILUREMODE
:
6265 spa
->spa_failmode
= intval
;
6267 case ZPOOL_PROP_AUTOEXPAND
:
6268 spa
->spa_autoexpand
= intval
;
6269 if (tx
->tx_txg
!= TXG_INITIAL
)
6270 spa_async_request(spa
,
6271 SPA_ASYNC_AUTOEXPAND
);
6273 case ZPOOL_PROP_DEDUPDITTO
:
6274 spa
->spa_dedup_ditto
= intval
;
6283 mutex_exit(&spa
->spa_props_lock
);
6287 * Perform one-time upgrade on-disk changes. spa_version() does not
6288 * reflect the new version this txg, so there must be no changes this
6289 * txg to anything that the upgrade code depends on after it executes.
6290 * Therefore this must be called after dsl_pool_sync() does the sync
6294 spa_sync_upgrades(spa_t
*spa
, dmu_tx_t
*tx
)
6296 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
6298 ASSERT(spa
->spa_sync_pass
== 1);
6300 rrw_enter(&dp
->dp_config_rwlock
, RW_WRITER
, FTAG
);
6302 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_ORIGIN
&&
6303 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_ORIGIN
) {
6304 dsl_pool_create_origin(dp
, tx
);
6306 /* Keeping the origin open increases spa_minref */
6307 spa
->spa_minref
+= 3;
6310 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_NEXT_CLONES
&&
6311 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_NEXT_CLONES
) {
6312 dsl_pool_upgrade_clones(dp
, tx
);
6315 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_DIR_CLONES
&&
6316 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_DIR_CLONES
) {
6317 dsl_pool_upgrade_dir_clones(dp
, tx
);
6319 /* Keeping the freedir open increases spa_minref */
6320 spa
->spa_minref
+= 3;
6323 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_FEATURES
&&
6324 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
6325 spa_feature_create_zap_objects(spa
, tx
);
6329 * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable
6330 * when possibility to use lz4 compression for metadata was added
6331 * Old pools that have this feature enabled must be upgraded to have
6332 * this feature active
6334 if (spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
6335 boolean_t lz4_en
= spa_feature_is_enabled(spa
,
6336 SPA_FEATURE_LZ4_COMPRESS
);
6337 boolean_t lz4_ac
= spa_feature_is_active(spa
,
6338 SPA_FEATURE_LZ4_COMPRESS
);
6340 if (lz4_en
&& !lz4_ac
)
6341 spa_feature_incr(spa
, SPA_FEATURE_LZ4_COMPRESS
, tx
);
6343 rrw_exit(&dp
->dp_config_rwlock
, FTAG
);
6347 * Sync the specified transaction group. New blocks may be dirtied as
6348 * part of the process, so we iterate until it converges.
6351 spa_sync(spa_t
*spa
, uint64_t txg
)
6353 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
6354 objset_t
*mos
= spa
->spa_meta_objset
;
6355 bplist_t
*free_bpl
= &spa
->spa_free_bplist
[txg
& TXG_MASK
];
6356 vdev_t
*rvd
= spa
->spa_root_vdev
;
6362 VERIFY(spa_writeable(spa
));
6365 * Lock out configuration changes.
6367 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
6369 spa
->spa_syncing_txg
= txg
;
6370 spa
->spa_sync_pass
= 0;
6373 * If there are any pending vdev state changes, convert them
6374 * into config changes that go out with this transaction group.
6376 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
6377 while (list_head(&spa
->spa_state_dirty_list
) != NULL
) {
6379 * We need the write lock here because, for aux vdevs,
6380 * calling vdev_config_dirty() modifies sav_config.
6381 * This is ugly and will become unnecessary when we
6382 * eliminate the aux vdev wart by integrating all vdevs
6383 * into the root vdev tree.
6385 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
6386 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_WRITER
);
6387 while ((vd
= list_head(&spa
->spa_state_dirty_list
)) != NULL
) {
6388 vdev_state_clean(vd
);
6389 vdev_config_dirty(vd
);
6391 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
6392 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
6394 spa_config_exit(spa
, SCL_STATE
, FTAG
);
6396 tx
= dmu_tx_create_assigned(dp
, txg
);
6398 spa
->spa_sync_starttime
= gethrtime();
6399 taskq_cancel_id(system_taskq
, spa
->spa_deadman_tqid
);
6400 spa
->spa_deadman_tqid
= taskq_dispatch_delay(system_taskq
,
6401 spa_deadman
, spa
, TQ_SLEEP
, ddi_get_lbolt() +
6402 NSEC_TO_TICK(spa
->spa_deadman_synctime
));
6405 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
6406 * set spa_deflate if we have no raid-z vdevs.
6408 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_RAIDZ_DEFLATE
&&
6409 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
6412 for (i
= 0; i
< rvd
->vdev_children
; i
++) {
6413 vd
= rvd
->vdev_child
[i
];
6414 if (vd
->vdev_deflate_ratio
!= SPA_MINBLOCKSIZE
)
6417 if (i
== rvd
->vdev_children
) {
6418 spa
->spa_deflate
= TRUE
;
6419 VERIFY(0 == zap_add(spa
->spa_meta_objset
,
6420 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
6421 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
));
6426 * Iterate to convergence.
6429 int pass
= ++spa
->spa_sync_pass
;
6431 spa_sync_config_object(spa
, tx
);
6432 spa_sync_aux_dev(spa
, &spa
->spa_spares
, tx
,
6433 ZPOOL_CONFIG_SPARES
, DMU_POOL_SPARES
);
6434 spa_sync_aux_dev(spa
, &spa
->spa_l2cache
, tx
,
6435 ZPOOL_CONFIG_L2CACHE
, DMU_POOL_L2CACHE
);
6436 spa_errlog_sync(spa
, txg
);
6437 dsl_pool_sync(dp
, txg
);
6439 if (pass
< zfs_sync_pass_deferred_free
) {
6440 spa_sync_frees(spa
, free_bpl
, tx
);
6443 * We can not defer frees in pass 1, because
6444 * we sync the deferred frees later in pass 1.
6446 ASSERT3U(pass
, >, 1);
6447 bplist_iterate(free_bpl
, bpobj_enqueue_cb
,
6448 &spa
->spa_deferred_bpobj
, tx
);
6452 dsl_scan_sync(dp
, tx
);
6454 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, txg
)))
6458 spa_sync_upgrades(spa
, tx
);
6460 spa
->spa_uberblock
.ub_rootbp
.blk_birth
);
6462 * Note: We need to check if the MOS is dirty
6463 * because we could have marked the MOS dirty
6464 * without updating the uberblock (e.g. if we
6465 * have sync tasks but no dirty user data). We
6466 * need to check the uberblock's rootbp because
6467 * it is updated if we have synced out dirty
6468 * data (though in this case the MOS will most
6469 * likely also be dirty due to second order
6470 * effects, we don't want to rely on that here).
6472 if (spa
->spa_uberblock
.ub_rootbp
.blk_birth
< txg
&&
6473 !dmu_objset_is_dirty(mos
, txg
)) {
6475 * Nothing changed on the first pass,
6476 * therefore this TXG is a no-op. Avoid
6477 * syncing deferred frees, so that we
6478 * can keep this TXG as a no-op.
6480 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
,
6482 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
6483 ASSERT(txg_list_empty(&dp
->dp_sync_tasks
, txg
));
6486 spa_sync_deferred_frees(spa
, tx
);
6489 } while (dmu_objset_is_dirty(mos
, txg
));
6492 * Rewrite the vdev configuration (which includes the uberblock)
6493 * to commit the transaction group.
6495 * If there are no dirty vdevs, we sync the uberblock to a few
6496 * random top-level vdevs that are known to be visible in the
6497 * config cache (see spa_vdev_add() for a complete description).
6498 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
6502 * We hold SCL_STATE to prevent vdev open/close/etc.
6503 * while we're attempting to write the vdev labels.
6505 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
6507 if (list_is_empty(&spa
->spa_config_dirty_list
)) {
6508 vdev_t
*svd
[SPA_DVAS_PER_BP
];
6510 int children
= rvd
->vdev_children
;
6511 int c0
= spa_get_random(children
);
6513 for (c
= 0; c
< children
; c
++) {
6514 vd
= rvd
->vdev_child
[(c0
+ c
) % children
];
6515 if (vd
->vdev_ms_array
== 0 || vd
->vdev_islog
)
6517 svd
[svdcount
++] = vd
;
6518 if (svdcount
== SPA_DVAS_PER_BP
)
6521 error
= vdev_config_sync(svd
, svdcount
, txg
);
6523 error
= vdev_config_sync(rvd
->vdev_child
,
6524 rvd
->vdev_children
, txg
);
6528 spa
->spa_last_synced_guid
= rvd
->vdev_guid
;
6530 spa_config_exit(spa
, SCL_STATE
, FTAG
);
6534 zio_suspend(spa
, NULL
);
6535 zio_resume_wait(spa
);
6539 taskq_cancel_id(system_taskq
, spa
->spa_deadman_tqid
);
6540 spa
->spa_deadman_tqid
= 0;
6543 * Clear the dirty config list.
6545 while ((vd
= list_head(&spa
->spa_config_dirty_list
)) != NULL
)
6546 vdev_config_clean(vd
);
6549 * Now that the new config has synced transactionally,
6550 * let it become visible to the config cache.
6552 if (spa
->spa_config_syncing
!= NULL
) {
6553 spa_config_set(spa
, spa
->spa_config_syncing
);
6554 spa
->spa_config_txg
= txg
;
6555 spa
->spa_config_syncing
= NULL
;
6558 spa
->spa_ubsync
= spa
->spa_uberblock
;
6560 dsl_pool_sync_done(dp
, txg
);
6563 * Update usable space statistics.
6565 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, TXG_CLEAN(txg
))))
6566 vdev_sync_done(vd
, txg
);
6568 spa_update_dspace(spa
);
6571 * It had better be the case that we didn't dirty anything
6572 * since vdev_config_sync().
6574 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
, txg
));
6575 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
6576 ASSERT(txg_list_empty(&spa
->spa_vdev_txg_list
, txg
));
6578 spa
->spa_sync_pass
= 0;
6580 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
6582 spa_handle_ignored_writes(spa
);
6585 * If any async tasks have been requested, kick them off.
6587 spa_async_dispatch(spa
);
6591 * Sync all pools. We don't want to hold the namespace lock across these
6592 * operations, so we take a reference on the spa_t and drop the lock during the
6596 spa_sync_allpools(void)
6599 mutex_enter(&spa_namespace_lock
);
6600 while ((spa
= spa_next(spa
)) != NULL
) {
6601 if (spa_state(spa
) != POOL_STATE_ACTIVE
||
6602 !spa_writeable(spa
) || spa_suspended(spa
))
6604 spa_open_ref(spa
, FTAG
);
6605 mutex_exit(&spa_namespace_lock
);
6606 txg_wait_synced(spa_get_dsl(spa
), 0);
6607 mutex_enter(&spa_namespace_lock
);
6608 spa_close(spa
, FTAG
);
6610 mutex_exit(&spa_namespace_lock
);
6614 * ==========================================================================
6615 * Miscellaneous routines
6616 * ==========================================================================
6620 * Remove all pools in the system.
6628 * Remove all cached state. All pools should be closed now,
6629 * so every spa in the AVL tree should be unreferenced.
6631 mutex_enter(&spa_namespace_lock
);
6632 while ((spa
= spa_next(NULL
)) != NULL
) {
6634 * Stop async tasks. The async thread may need to detach
6635 * a device that's been replaced, which requires grabbing
6636 * spa_namespace_lock, so we must drop it here.
6638 spa_open_ref(spa
, FTAG
);
6639 mutex_exit(&spa_namespace_lock
);
6640 spa_async_suspend(spa
);
6641 mutex_enter(&spa_namespace_lock
);
6642 spa_close(spa
, FTAG
);
6644 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
6646 spa_deactivate(spa
);
6650 mutex_exit(&spa_namespace_lock
);
6654 spa_lookup_by_guid(spa_t
*spa
, uint64_t guid
, boolean_t aux
)
6659 if ((vd
= vdev_lookup_by_guid(spa
->spa_root_vdev
, guid
)) != NULL
)
6663 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
6664 vd
= spa
->spa_l2cache
.sav_vdevs
[i
];
6665 if (vd
->vdev_guid
== guid
)
6669 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
6670 vd
= spa
->spa_spares
.sav_vdevs
[i
];
6671 if (vd
->vdev_guid
== guid
)
6680 spa_upgrade(spa_t
*spa
, uint64_t version
)
6682 ASSERT(spa_writeable(spa
));
6684 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6687 * This should only be called for a non-faulted pool, and since a
6688 * future version would result in an unopenable pool, this shouldn't be
6691 ASSERT(SPA_VERSION_IS_SUPPORTED(spa
->spa_uberblock
.ub_version
));
6692 ASSERT3U(version
, >=, spa
->spa_uberblock
.ub_version
);
6694 spa
->spa_uberblock
.ub_version
= version
;
6695 vdev_config_dirty(spa
->spa_root_vdev
);
6697 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6699 txg_wait_synced(spa_get_dsl(spa
), 0);
6703 spa_has_spare(spa_t
*spa
, uint64_t guid
)
6707 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
6709 for (i
= 0; i
< sav
->sav_count
; i
++)
6710 if (sav
->sav_vdevs
[i
]->vdev_guid
== guid
)
6713 for (i
= 0; i
< sav
->sav_npending
; i
++) {
6714 if (nvlist_lookup_uint64(sav
->sav_pending
[i
], ZPOOL_CONFIG_GUID
,
6715 &spareguid
) == 0 && spareguid
== guid
)
6723 * Check if a pool has an active shared spare device.
6724 * Note: reference count of an active spare is 2, as a spare and as a replace
6727 spa_has_active_shared_spare(spa_t
*spa
)
6731 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
6733 for (i
= 0; i
< sav
->sav_count
; i
++) {
6734 if (spa_spare_exists(sav
->sav_vdevs
[i
]->vdev_guid
, &pool
,
6735 &refcnt
) && pool
!= 0ULL && pool
== spa_guid(spa
) &&
6744 * Post a FM_EREPORT_ZFS_* event from sys/fm/fs/zfs.h. The payload will be
6745 * filled in from the spa and (optionally) the vdev. This doesn't do anything
6746 * in the userland libzpool, as we don't want consumers to misinterpret ztest
6747 * or zdb as real changes.
6750 spa_event_notify(spa_t
*spa
, vdev_t
*vd
, const char *name
)
6753 zfs_ereport_post(name
, spa
, vd
, NULL
, 0, 0);
6757 #if defined(_KERNEL) && defined(HAVE_SPL)
6758 /* state manipulation functions */
6759 EXPORT_SYMBOL(spa_open
);
6760 EXPORT_SYMBOL(spa_open_rewind
);
6761 EXPORT_SYMBOL(spa_get_stats
);
6762 EXPORT_SYMBOL(spa_create
);
6763 EXPORT_SYMBOL(spa_import_rootpool
);
6764 EXPORT_SYMBOL(spa_import
);
6765 EXPORT_SYMBOL(spa_tryimport
);
6766 EXPORT_SYMBOL(spa_destroy
);
6767 EXPORT_SYMBOL(spa_export
);
6768 EXPORT_SYMBOL(spa_reset
);
6769 EXPORT_SYMBOL(spa_async_request
);
6770 EXPORT_SYMBOL(spa_async_suspend
);
6771 EXPORT_SYMBOL(spa_async_resume
);
6772 EXPORT_SYMBOL(spa_inject_addref
);
6773 EXPORT_SYMBOL(spa_inject_delref
);
6774 EXPORT_SYMBOL(spa_scan_stat_init
);
6775 EXPORT_SYMBOL(spa_scan_get_stats
);
6777 /* device maniion */
6778 EXPORT_SYMBOL(spa_vdev_add
);
6779 EXPORT_SYMBOL(spa_vdev_attach
);
6780 EXPORT_SYMBOL(spa_vdev_detach
);
6781 EXPORT_SYMBOL(spa_vdev_remove
);
6782 EXPORT_SYMBOL(spa_vdev_setpath
);
6783 EXPORT_SYMBOL(spa_vdev_setfru
);
6784 EXPORT_SYMBOL(spa_vdev_split_mirror
);
6786 /* spare statech is global across all pools) */
6787 EXPORT_SYMBOL(spa_spare_add
);
6788 EXPORT_SYMBOL(spa_spare_remove
);
6789 EXPORT_SYMBOL(spa_spare_exists
);
6790 EXPORT_SYMBOL(spa_spare_activate
);
6792 /* L2ARC statech is global across all pools) */
6793 EXPORT_SYMBOL(spa_l2cache_add
);
6794 EXPORT_SYMBOL(spa_l2cache_remove
);
6795 EXPORT_SYMBOL(spa_l2cache_exists
);
6796 EXPORT_SYMBOL(spa_l2cache_activate
);
6797 EXPORT_SYMBOL(spa_l2cache_drop
);
6800 EXPORT_SYMBOL(spa_scan
);
6801 EXPORT_SYMBOL(spa_scan_stop
);
6804 EXPORT_SYMBOL(spa_sync
); /* only for DMU use */
6805 EXPORT_SYMBOL(spa_sync_allpools
);
6808 EXPORT_SYMBOL(spa_prop_set
);
6809 EXPORT_SYMBOL(spa_prop_get
);
6810 EXPORT_SYMBOL(spa_prop_clear_bootfs
);
6812 /* asynchronous event notification */
6813 EXPORT_SYMBOL(spa_event_notify
);
6816 #if defined(_KERNEL) && defined(HAVE_SPL)
6817 module_param(spa_load_verify_maxinflight
, int, 0644);
6818 MODULE_PARM_DESC(spa_load_verify_maxinflight
,
6819 "Max concurrent traversal I/Os while verifying pool during import -X");
6821 module_param(spa_load_verify_metadata
, int, 0644);
6822 MODULE_PARM_DESC(spa_load_verify_metadata
,
6823 "Set to traverse metadata on pool import");
6825 module_param(spa_load_verify_data
, int, 0644);
6826 MODULE_PARM_DESC(spa_load_verify_data
,
6827 "Set to traverse data on pool import");
6829 module_param(zio_taskq_batch_pct
, uint
, 0444);
6830 MODULE_PARM_DESC(zio_taskq_batch_pct
,
6831 "Percentage of CPUs to run an IO worker thread");