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.
26 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
30 * This file contains all the routines used when modifying on-disk SPA state.
31 * This includes opening, importing, destroying, exporting a pool, and syncing a
35 #include <sys/zfs_context.h>
36 #include <sys/fm/fs/zfs.h>
37 #include <sys/spa_impl.h>
39 #include <sys/zio_checksum.h>
41 #include <sys/dmu_tx.h>
45 #include <sys/vdev_impl.h>
46 #include <sys/vdev_disk.h>
47 #include <sys/metaslab.h>
48 #include <sys/metaslab_impl.h>
49 #include <sys/uberblock_impl.h>
52 #include <sys/dmu_traverse.h>
53 #include <sys/dmu_objset.h>
54 #include <sys/unique.h>
55 #include <sys/dsl_pool.h>
56 #include <sys/dsl_dataset.h>
57 #include <sys/dsl_dir.h>
58 #include <sys/dsl_prop.h>
59 #include <sys/dsl_synctask.h>
60 #include <sys/fs/zfs.h>
62 #include <sys/callb.h>
63 #include <sys/systeminfo.h>
64 #include <sys/spa_boot.h>
65 #include <sys/zfs_ioctl.h>
66 #include <sys/dsl_scan.h>
69 #include <sys/bootprops.h>
70 #include <sys/callb.h>
71 #include <sys/cpupart.h>
73 #include <sys/sysdc.h>
78 #include "zfs_comutil.h"
80 typedef enum zti_modes
{
81 zti_mode_fixed
, /* value is # of threads (min 1) */
82 zti_mode_online_percent
, /* value is % of online CPUs */
83 zti_mode_batch
, /* cpu-intensive; value is ignored */
84 zti_mode_null
, /* don't create a taskq */
88 #define ZTI_FIX(n) { zti_mode_fixed, (n) }
89 #define ZTI_PCT(n) { zti_mode_online_percent, (n) }
90 #define ZTI_BATCH { zti_mode_batch, 0 }
91 #define ZTI_NULL { zti_mode_null, 0 }
93 #define ZTI_ONE ZTI_FIX(1)
95 typedef struct zio_taskq_info
{
96 enum zti_modes zti_mode
;
100 static const char *const zio_taskq_types
[ZIO_TASKQ_TYPES
] = {
101 "iss", "iss_h", "int", "int_h"
105 * Define the taskq threads for the following I/O types:
106 * NULL, READ, WRITE, FREE, CLAIM, and IOCTL
108 const zio_taskq_info_t zio_taskqs
[ZIO_TYPES
][ZIO_TASKQ_TYPES
] = {
109 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
110 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
},
111 { ZTI_FIX(8), ZTI_NULL
, ZTI_BATCH
, ZTI_NULL
},
112 { ZTI_BATCH
, ZTI_FIX(5), ZTI_FIX(16), ZTI_FIX(5) },
113 { ZTI_FIX(100), ZTI_NULL
, ZTI_ONE
, ZTI_NULL
},
114 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
},
115 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
},
118 static dsl_syncfunc_t spa_sync_props
;
119 static boolean_t
spa_has_active_shared_spare(spa_t
*spa
);
120 static inline int spa_load_impl(spa_t
*spa
, uint64_t, nvlist_t
*config
,
121 spa_load_state_t state
, spa_import_type_t type
, boolean_t mosconfig
,
123 static void spa_vdev_resilver_done(spa_t
*spa
);
125 uint_t zio_taskq_batch_pct
= 100; /* 1 thread per cpu in pset */
126 id_t zio_taskq_psrset_bind
= PS_NONE
;
127 boolean_t zio_taskq_sysdc
= B_TRUE
; /* use SDC scheduling class */
128 uint_t zio_taskq_basedc
= 80; /* base duty cycle */
130 boolean_t spa_create_process
= B_TRUE
; /* no process ==> no sysdc */
133 * This (illegal) pool name is used when temporarily importing a spa_t in order
134 * to get the vdev stats associated with the imported devices.
136 #define TRYIMPORT_NAME "$import"
139 * ==========================================================================
140 * SPA properties routines
141 * ==========================================================================
145 * Add a (source=src, propname=propval) list to an nvlist.
148 spa_prop_add_list(nvlist_t
*nvl
, zpool_prop_t prop
, char *strval
,
149 uint64_t intval
, zprop_source_t src
)
151 const char *propname
= zpool_prop_to_name(prop
);
154 VERIFY(nvlist_alloc(&propval
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
155 VERIFY(nvlist_add_uint64(propval
, ZPROP_SOURCE
, src
) == 0);
158 VERIFY(nvlist_add_string(propval
, ZPROP_VALUE
, strval
) == 0);
160 VERIFY(nvlist_add_uint64(propval
, ZPROP_VALUE
, intval
) == 0);
162 VERIFY(nvlist_add_nvlist(nvl
, propname
, propval
) == 0);
163 nvlist_free(propval
);
167 * Get property values from the spa configuration.
170 spa_prop_get_config(spa_t
*spa
, nvlist_t
**nvp
)
174 uint64_t cap
, version
;
175 zprop_source_t src
= ZPROP_SRC_NONE
;
176 spa_config_dirent_t
*dp
;
178 ASSERT(MUTEX_HELD(&spa
->spa_props_lock
));
180 if (spa
->spa_root_vdev
!= NULL
) {
181 alloc
= metaslab_class_get_alloc(spa_normal_class(spa
));
182 size
= metaslab_class_get_space(spa_normal_class(spa
));
183 spa_prop_add_list(*nvp
, ZPOOL_PROP_NAME
, spa_name(spa
), 0, src
);
184 spa_prop_add_list(*nvp
, ZPOOL_PROP_SIZE
, NULL
, size
, src
);
185 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALLOCATED
, NULL
, alloc
, src
);
186 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREE
, NULL
,
188 spa_prop_add_list(*nvp
, ZPOOL_PROP_READONLY
, NULL
,
189 (spa_mode(spa
) == FREAD
), src
);
191 cap
= (size
== 0) ? 0 : (alloc
* 100 / size
);
192 spa_prop_add_list(*nvp
, ZPOOL_PROP_CAPACITY
, NULL
, cap
, src
);
194 spa_prop_add_list(*nvp
, ZPOOL_PROP_DEDUPRATIO
, NULL
,
195 ddt_get_pool_dedup_ratio(spa
), src
);
197 spa_prop_add_list(*nvp
, ZPOOL_PROP_HEALTH
, NULL
,
198 spa
->spa_root_vdev
->vdev_state
, src
);
200 version
= spa_version(spa
);
201 if (version
== zpool_prop_default_numeric(ZPOOL_PROP_VERSION
))
202 src
= ZPROP_SRC_DEFAULT
;
204 src
= ZPROP_SRC_LOCAL
;
205 spa_prop_add_list(*nvp
, ZPOOL_PROP_VERSION
, NULL
, version
, src
);
208 spa_prop_add_list(*nvp
, ZPOOL_PROP_GUID
, NULL
, spa_guid(spa
), src
);
210 if (spa
->spa_root
!= NULL
)
211 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALTROOT
, spa
->spa_root
,
214 if ((dp
= list_head(&spa
->spa_config_list
)) != NULL
) {
215 if (dp
->scd_path
== NULL
) {
216 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
217 "none", 0, ZPROP_SRC_LOCAL
);
218 } else if (strcmp(dp
->scd_path
, spa_config_path
) != 0) {
219 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
220 dp
->scd_path
, 0, ZPROP_SRC_LOCAL
);
226 * Get zpool property values.
229 spa_prop_get(spa_t
*spa
, nvlist_t
**nvp
)
231 objset_t
*mos
= spa
->spa_meta_objset
;
236 err
= nvlist_alloc(nvp
, NV_UNIQUE_NAME
, KM_SLEEP
);
240 mutex_enter(&spa
->spa_props_lock
);
243 * Get properties from the spa config.
245 spa_prop_get_config(spa
, nvp
);
247 /* If no pool property object, no more prop to get. */
248 if (mos
== NULL
|| spa
->spa_pool_props_object
== 0) {
249 mutex_exit(&spa
->spa_props_lock
);
254 * Get properties from the MOS pool property object.
256 for (zap_cursor_init(&zc
, mos
, spa
->spa_pool_props_object
);
257 (err
= zap_cursor_retrieve(&zc
, &za
)) == 0;
258 zap_cursor_advance(&zc
)) {
261 zprop_source_t src
= ZPROP_SRC_DEFAULT
;
264 if ((prop
= zpool_name_to_prop(za
.za_name
)) == ZPROP_INVAL
)
267 switch (za
.za_integer_length
) {
269 /* integer property */
270 if (za
.za_first_integer
!=
271 zpool_prop_default_numeric(prop
))
272 src
= ZPROP_SRC_LOCAL
;
274 if (prop
== ZPOOL_PROP_BOOTFS
) {
276 dsl_dataset_t
*ds
= NULL
;
278 dp
= spa_get_dsl(spa
);
279 rw_enter(&dp
->dp_config_rwlock
, RW_READER
);
280 if ((err
= dsl_dataset_hold_obj(dp
,
281 za
.za_first_integer
, FTAG
, &ds
))) {
282 rw_exit(&dp
->dp_config_rwlock
);
287 MAXNAMELEN
+ strlen(MOS_DIR_NAME
) + 1,
289 dsl_dataset_name(ds
, strval
);
290 dsl_dataset_rele(ds
, FTAG
);
291 rw_exit(&dp
->dp_config_rwlock
);
294 intval
= za
.za_first_integer
;
297 spa_prop_add_list(*nvp
, prop
, strval
, intval
, src
);
301 MAXNAMELEN
+ strlen(MOS_DIR_NAME
) + 1);
306 /* string property */
307 strval
= kmem_alloc(za
.za_num_integers
, KM_SLEEP
);
308 err
= zap_lookup(mos
, spa
->spa_pool_props_object
,
309 za
.za_name
, 1, za
.za_num_integers
, strval
);
311 kmem_free(strval
, za
.za_num_integers
);
314 spa_prop_add_list(*nvp
, prop
, strval
, 0, src
);
315 kmem_free(strval
, za
.za_num_integers
);
322 zap_cursor_fini(&zc
);
323 mutex_exit(&spa
->spa_props_lock
);
325 if (err
&& err
!= ENOENT
) {
335 * Validate the given pool properties nvlist and modify the list
336 * for the property values to be set.
339 spa_prop_validate(spa_t
*spa
, nvlist_t
*props
)
342 int error
= 0, reset_bootfs
= 0;
346 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
348 char *propname
, *strval
;
353 propname
= nvpair_name(elem
);
355 if ((prop
= zpool_name_to_prop(propname
)) == ZPROP_INVAL
)
359 case ZPOOL_PROP_VERSION
:
360 error
= nvpair_value_uint64(elem
, &intval
);
362 (intval
< spa_version(spa
) || intval
> SPA_VERSION
))
366 case ZPOOL_PROP_DELEGATION
:
367 case ZPOOL_PROP_AUTOREPLACE
:
368 case ZPOOL_PROP_LISTSNAPS
:
369 case ZPOOL_PROP_AUTOEXPAND
:
370 error
= nvpair_value_uint64(elem
, &intval
);
371 if (!error
&& intval
> 1)
375 case ZPOOL_PROP_BOOTFS
:
377 * If the pool version is less than SPA_VERSION_BOOTFS,
378 * or the pool is still being created (version == 0),
379 * the bootfs property cannot be set.
381 if (spa_version(spa
) < SPA_VERSION_BOOTFS
) {
387 * Make sure the vdev config is bootable
389 if (!vdev_is_bootable(spa
->spa_root_vdev
)) {
396 error
= nvpair_value_string(elem
, &strval
);
401 if (strval
== NULL
|| strval
[0] == '\0') {
402 objnum
= zpool_prop_default_numeric(
407 if ((error
= dmu_objset_hold(strval
,FTAG
,&os
)))
410 /* Must be ZPL and not gzip compressed. */
412 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
414 } else if ((error
= dsl_prop_get_integer(strval
,
415 zfs_prop_to_name(ZFS_PROP_COMPRESSION
),
416 &compress
, NULL
)) == 0 &&
417 !BOOTFS_COMPRESS_VALID(compress
)) {
420 objnum
= dmu_objset_id(os
);
422 dmu_objset_rele(os
, FTAG
);
426 case ZPOOL_PROP_FAILUREMODE
:
427 error
= nvpair_value_uint64(elem
, &intval
);
428 if (!error
&& (intval
< ZIO_FAILURE_MODE_WAIT
||
429 intval
> ZIO_FAILURE_MODE_PANIC
))
433 * This is a special case which only occurs when
434 * the pool has completely failed. This allows
435 * the user to change the in-core failmode property
436 * without syncing it out to disk (I/Os might
437 * currently be blocked). We do this by returning
438 * EIO to the caller (spa_prop_set) to trick it
439 * into thinking we encountered a property validation
442 if (!error
&& spa_suspended(spa
)) {
443 spa
->spa_failmode
= intval
;
448 case ZPOOL_PROP_CACHEFILE
:
449 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
452 if (strval
[0] == '\0')
455 if (strcmp(strval
, "none") == 0)
458 if (strval
[0] != '/') {
463 slash
= strrchr(strval
, '/');
464 ASSERT(slash
!= NULL
);
466 if (slash
[1] == '\0' || strcmp(slash
, "/.") == 0 ||
467 strcmp(slash
, "/..") == 0)
471 case ZPOOL_PROP_DEDUPDITTO
:
472 if (spa_version(spa
) < SPA_VERSION_DEDUP
)
475 error
= nvpair_value_uint64(elem
, &intval
);
477 intval
!= 0 && intval
< ZIO_DEDUPDITTO_MIN
)
489 if (!error
&& reset_bootfs
) {
490 error
= nvlist_remove(props
,
491 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), DATA_TYPE_STRING
);
494 error
= nvlist_add_uint64(props
,
495 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), objnum
);
503 spa_configfile_set(spa_t
*spa
, nvlist_t
*nvp
, boolean_t need_sync
)
506 spa_config_dirent_t
*dp
;
508 if (nvlist_lookup_string(nvp
, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
),
512 dp
= kmem_alloc(sizeof (spa_config_dirent_t
),
515 if (cachefile
[0] == '\0')
516 dp
->scd_path
= spa_strdup(spa_config_path
);
517 else if (strcmp(cachefile
, "none") == 0)
520 dp
->scd_path
= spa_strdup(cachefile
);
522 list_insert_head(&spa
->spa_config_list
, dp
);
524 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
528 spa_prop_set(spa_t
*spa
, nvlist_t
*nvp
)
532 boolean_t need_sync
= B_FALSE
;
535 if ((error
= spa_prop_validate(spa
, nvp
)) != 0)
539 while ((elem
= nvlist_next_nvpair(nvp
, elem
)) != NULL
) {
540 if ((prop
= zpool_name_to_prop(
541 nvpair_name(elem
))) == ZPROP_INVAL
)
544 if (prop
== ZPOOL_PROP_CACHEFILE
||
545 prop
== ZPOOL_PROP_ALTROOT
||
546 prop
== ZPOOL_PROP_READONLY
)
554 return (dsl_sync_task_do(spa_get_dsl(spa
), NULL
, spa_sync_props
,
561 * If the bootfs property value is dsobj, clear it.
564 spa_prop_clear_bootfs(spa_t
*spa
, uint64_t dsobj
, dmu_tx_t
*tx
)
566 if (spa
->spa_bootfs
== dsobj
&& spa
->spa_pool_props_object
!= 0) {
567 VERIFY(zap_remove(spa
->spa_meta_objset
,
568 spa
->spa_pool_props_object
,
569 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), tx
) == 0);
575 * ==========================================================================
576 * SPA state manipulation (open/create/destroy/import/export)
577 * ==========================================================================
581 spa_error_entry_compare(const void *a
, const void *b
)
583 spa_error_entry_t
*sa
= (spa_error_entry_t
*)a
;
584 spa_error_entry_t
*sb
= (spa_error_entry_t
*)b
;
587 ret
= bcmp(&sa
->se_bookmark
, &sb
->se_bookmark
,
588 sizeof (zbookmark_t
));
599 * Utility function which retrieves copies of the current logs and
600 * re-initializes them in the process.
603 spa_get_errlists(spa_t
*spa
, avl_tree_t
*last
, avl_tree_t
*scrub
)
605 ASSERT(MUTEX_HELD(&spa
->spa_errlist_lock
));
607 bcopy(&spa
->spa_errlist_last
, last
, sizeof (avl_tree_t
));
608 bcopy(&spa
->spa_errlist_scrub
, scrub
, sizeof (avl_tree_t
));
610 avl_create(&spa
->spa_errlist_scrub
,
611 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
612 offsetof(spa_error_entry_t
, se_avl
));
613 avl_create(&spa
->spa_errlist_last
,
614 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
615 offsetof(spa_error_entry_t
, se_avl
));
619 spa_taskq_create(spa_t
*spa
, const char *name
, enum zti_modes mode
,
620 uint_t value
, uint_t flags
)
622 boolean_t batch
= B_FALSE
;
626 return (NULL
); /* no taskq needed */
629 ASSERT3U(value
, >=, 1);
630 value
= MAX(value
, 1);
635 flags
|= TASKQ_THREADS_CPU_PCT
;
636 value
= zio_taskq_batch_pct
;
639 case zti_mode_online_percent
:
640 flags
|= TASKQ_THREADS_CPU_PCT
;
644 panic("unrecognized mode for %s taskq (%u:%u) in "
650 if (zio_taskq_sysdc
&& spa
->spa_proc
!= &p0
) {
652 flags
|= TASKQ_DC_BATCH
;
654 return (taskq_create_sysdc(name
, value
, 50, INT_MAX
,
655 spa
->spa_proc
, zio_taskq_basedc
, flags
));
657 return (taskq_create_proc(name
, value
, maxclsyspri
, 50, INT_MAX
,
658 spa
->spa_proc
, flags
));
662 spa_create_zio_taskqs(spa_t
*spa
)
666 for (t
= 0; t
< ZIO_TYPES
; t
++) {
667 for (q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
668 const zio_taskq_info_t
*ztip
= &zio_taskqs
[t
][q
];
669 enum zti_modes mode
= ztip
->zti_mode
;
670 uint_t value
= ztip
->zti_value
;
674 if (t
== ZIO_TYPE_WRITE
)
675 flags
|= TASKQ_NORECLAIM
;
677 (void) snprintf(name
, sizeof (name
),
678 "%s_%s", zio_type_name
[t
], zio_taskq_types
[q
]);
680 spa
->spa_zio_taskq
[t
][q
] =
681 spa_taskq_create(spa
, name
, mode
, value
, flags
);
686 #if defined(_KERNEL) && defined(HAVE_SPA_THREAD)
688 spa_thread(void *arg
)
693 user_t
*pu
= PTOU(curproc
);
695 CALLB_CPR_INIT(&cprinfo
, &spa
->spa_proc_lock
, callb_generic_cpr
,
698 ASSERT(curproc
!= &p0
);
699 (void) snprintf(pu
->u_psargs
, sizeof (pu
->u_psargs
),
700 "zpool-%s", spa
->spa_name
);
701 (void) strlcpy(pu
->u_comm
, pu
->u_psargs
, sizeof (pu
->u_comm
));
703 /* bind this thread to the requested psrset */
704 if (zio_taskq_psrset_bind
!= PS_NONE
) {
706 mutex_enter(&cpu_lock
);
707 mutex_enter(&pidlock
);
708 mutex_enter(&curproc
->p_lock
);
710 if (cpupart_bind_thread(curthread
, zio_taskq_psrset_bind
,
711 0, NULL
, NULL
) == 0) {
712 curthread
->t_bind_pset
= zio_taskq_psrset_bind
;
715 "Couldn't bind process for zfs pool \"%s\" to "
716 "pset %d\n", spa
->spa_name
, zio_taskq_psrset_bind
);
719 mutex_exit(&curproc
->p_lock
);
720 mutex_exit(&pidlock
);
721 mutex_exit(&cpu_lock
);
725 if (zio_taskq_sysdc
) {
726 sysdc_thread_enter(curthread
, 100, 0);
729 spa
->spa_proc
= curproc
;
730 spa
->spa_did
= curthread
->t_did
;
732 spa_create_zio_taskqs(spa
);
734 mutex_enter(&spa
->spa_proc_lock
);
735 ASSERT(spa
->spa_proc_state
== SPA_PROC_CREATED
);
737 spa
->spa_proc_state
= SPA_PROC_ACTIVE
;
738 cv_broadcast(&spa
->spa_proc_cv
);
740 CALLB_CPR_SAFE_BEGIN(&cprinfo
);
741 while (spa
->spa_proc_state
== SPA_PROC_ACTIVE
)
742 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
743 CALLB_CPR_SAFE_END(&cprinfo
, &spa
->spa_proc_lock
);
745 ASSERT(spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
);
746 spa
->spa_proc_state
= SPA_PROC_GONE
;
748 cv_broadcast(&spa
->spa_proc_cv
);
749 CALLB_CPR_EXIT(&cprinfo
); /* drops spa_proc_lock */
751 mutex_enter(&curproc
->p_lock
);
757 * Activate an uninitialized pool.
760 spa_activate(spa_t
*spa
, int mode
)
762 ASSERT(spa
->spa_state
== POOL_STATE_UNINITIALIZED
);
764 spa
->spa_state
= POOL_STATE_ACTIVE
;
765 spa
->spa_mode
= mode
;
767 spa
->spa_normal_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
768 spa
->spa_log_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
770 /* Try to create a covering process */
771 mutex_enter(&spa
->spa_proc_lock
);
772 ASSERT(spa
->spa_proc_state
== SPA_PROC_NONE
);
773 ASSERT(spa
->spa_proc
== &p0
);
776 #ifdef HAVE_SPA_THREAD
777 /* Only create a process if we're going to be around a while. */
778 if (spa_create_process
&& strcmp(spa
->spa_name
, TRYIMPORT_NAME
) != 0) {
779 if (newproc(spa_thread
, (caddr_t
)spa
, syscid
, maxclsyspri
,
781 spa
->spa_proc_state
= SPA_PROC_CREATED
;
782 while (spa
->spa_proc_state
== SPA_PROC_CREATED
) {
783 cv_wait(&spa
->spa_proc_cv
,
784 &spa
->spa_proc_lock
);
786 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
787 ASSERT(spa
->spa_proc
!= &p0
);
788 ASSERT(spa
->spa_did
!= 0);
792 "Couldn't create process for zfs pool \"%s\"\n",
797 #endif /* HAVE_SPA_THREAD */
798 mutex_exit(&spa
->spa_proc_lock
);
800 /* If we didn't create a process, we need to create our taskqs. */
801 if (spa
->spa_proc
== &p0
) {
802 spa_create_zio_taskqs(spa
);
805 list_create(&spa
->spa_config_dirty_list
, sizeof (vdev_t
),
806 offsetof(vdev_t
, vdev_config_dirty_node
));
807 list_create(&spa
->spa_state_dirty_list
, sizeof (vdev_t
),
808 offsetof(vdev_t
, vdev_state_dirty_node
));
810 txg_list_create(&spa
->spa_vdev_txg_list
,
811 offsetof(struct vdev
, vdev_txg_node
));
813 avl_create(&spa
->spa_errlist_scrub
,
814 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
815 offsetof(spa_error_entry_t
, se_avl
));
816 avl_create(&spa
->spa_errlist_last
,
817 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
818 offsetof(spa_error_entry_t
, se_avl
));
822 * Opposite of spa_activate().
825 spa_deactivate(spa_t
*spa
)
829 ASSERT(spa
->spa_sync_on
== B_FALSE
);
830 ASSERT(spa
->spa_dsl_pool
== NULL
);
831 ASSERT(spa
->spa_root_vdev
== NULL
);
832 ASSERT(spa
->spa_async_zio_root
== NULL
);
833 ASSERT(spa
->spa_state
!= POOL_STATE_UNINITIALIZED
);
835 txg_list_destroy(&spa
->spa_vdev_txg_list
);
837 list_destroy(&spa
->spa_config_dirty_list
);
838 list_destroy(&spa
->spa_state_dirty_list
);
840 for (t
= 0; t
< ZIO_TYPES
; t
++) {
841 for (q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
842 if (spa
->spa_zio_taskq
[t
][q
] != NULL
)
843 taskq_destroy(spa
->spa_zio_taskq
[t
][q
]);
844 spa
->spa_zio_taskq
[t
][q
] = NULL
;
848 metaslab_class_destroy(spa
->spa_normal_class
);
849 spa
->spa_normal_class
= NULL
;
851 metaslab_class_destroy(spa
->spa_log_class
);
852 spa
->spa_log_class
= NULL
;
855 * If this was part of an import or the open otherwise failed, we may
856 * still have errors left in the queues. Empty them just in case.
858 spa_errlog_drain(spa
);
860 avl_destroy(&spa
->spa_errlist_scrub
);
861 avl_destroy(&spa
->spa_errlist_last
);
863 spa
->spa_state
= POOL_STATE_UNINITIALIZED
;
865 mutex_enter(&spa
->spa_proc_lock
);
866 if (spa
->spa_proc_state
!= SPA_PROC_NONE
) {
867 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
868 spa
->spa_proc_state
= SPA_PROC_DEACTIVATE
;
869 cv_broadcast(&spa
->spa_proc_cv
);
870 while (spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
) {
871 ASSERT(spa
->spa_proc
!= &p0
);
872 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
874 ASSERT(spa
->spa_proc_state
== SPA_PROC_GONE
);
875 spa
->spa_proc_state
= SPA_PROC_NONE
;
877 ASSERT(spa
->spa_proc
== &p0
);
878 mutex_exit(&spa
->spa_proc_lock
);
881 * We want to make sure spa_thread() has actually exited the ZFS
882 * module, so that the module can't be unloaded out from underneath
885 if (spa
->spa_did
!= 0) {
886 thread_join(spa
->spa_did
);
892 * Verify a pool configuration, and construct the vdev tree appropriately. This
893 * will create all the necessary vdevs in the appropriate layout, with each vdev
894 * in the CLOSED state. This will prep the pool before open/creation/import.
895 * All vdev validation is done by the vdev_alloc() routine.
898 spa_config_parse(spa_t
*spa
, vdev_t
**vdp
, nvlist_t
*nv
, vdev_t
*parent
,
899 uint_t id
, int atype
)
906 if ((error
= vdev_alloc(spa
, vdp
, nv
, parent
, id
, atype
)) != 0)
909 if ((*vdp
)->vdev_ops
->vdev_op_leaf
)
912 error
= nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
924 for (c
= 0; c
< children
; c
++) {
926 if ((error
= spa_config_parse(spa
, &vd
, child
[c
], *vdp
, c
,
934 ASSERT(*vdp
!= NULL
);
940 * Opposite of spa_load().
943 spa_unload(spa_t
*spa
)
947 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
952 spa_async_suspend(spa
);
957 if (spa
->spa_sync_on
) {
958 txg_sync_stop(spa
->spa_dsl_pool
);
959 spa
->spa_sync_on
= B_FALSE
;
963 * Wait for any outstanding async I/O to complete.
965 if (spa
->spa_async_zio_root
!= NULL
) {
966 (void) zio_wait(spa
->spa_async_zio_root
);
967 spa
->spa_async_zio_root
= NULL
;
970 bpobj_close(&spa
->spa_deferred_bpobj
);
973 * Close the dsl pool.
975 if (spa
->spa_dsl_pool
) {
976 dsl_pool_close(spa
->spa_dsl_pool
);
977 spa
->spa_dsl_pool
= NULL
;
978 spa
->spa_meta_objset
= NULL
;
983 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
986 * Drop and purge level 2 cache
988 spa_l2cache_drop(spa
);
993 if (spa
->spa_root_vdev
)
994 vdev_free(spa
->spa_root_vdev
);
995 ASSERT(spa
->spa_root_vdev
== NULL
);
997 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
998 vdev_free(spa
->spa_spares
.sav_vdevs
[i
]);
999 if (spa
->spa_spares
.sav_vdevs
) {
1000 kmem_free(spa
->spa_spares
.sav_vdevs
,
1001 spa
->spa_spares
.sav_count
* sizeof (void *));
1002 spa
->spa_spares
.sav_vdevs
= NULL
;
1004 if (spa
->spa_spares
.sav_config
) {
1005 nvlist_free(spa
->spa_spares
.sav_config
);
1006 spa
->spa_spares
.sav_config
= NULL
;
1008 spa
->spa_spares
.sav_count
= 0;
1010 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++)
1011 vdev_free(spa
->spa_l2cache
.sav_vdevs
[i
]);
1012 if (spa
->spa_l2cache
.sav_vdevs
) {
1013 kmem_free(spa
->spa_l2cache
.sav_vdevs
,
1014 spa
->spa_l2cache
.sav_count
* sizeof (void *));
1015 spa
->spa_l2cache
.sav_vdevs
= NULL
;
1017 if (spa
->spa_l2cache
.sav_config
) {
1018 nvlist_free(spa
->spa_l2cache
.sav_config
);
1019 spa
->spa_l2cache
.sav_config
= NULL
;
1021 spa
->spa_l2cache
.sav_count
= 0;
1023 spa
->spa_async_suspended
= 0;
1025 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1029 * Load (or re-load) the current list of vdevs describing the active spares for
1030 * this pool. When this is called, we have some form of basic information in
1031 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1032 * then re-generate a more complete list including status information.
1035 spa_load_spares(spa_t
*spa
)
1042 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1045 * First, close and free any existing spare vdevs.
1047 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1048 vd
= spa
->spa_spares
.sav_vdevs
[i
];
1050 /* Undo the call to spa_activate() below */
1051 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1052 B_FALSE
)) != NULL
&& tvd
->vdev_isspare
)
1053 spa_spare_remove(tvd
);
1058 if (spa
->spa_spares
.sav_vdevs
)
1059 kmem_free(spa
->spa_spares
.sav_vdevs
,
1060 spa
->spa_spares
.sav_count
* sizeof (void *));
1062 if (spa
->spa_spares
.sav_config
== NULL
)
1065 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
1066 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
1068 spa
->spa_spares
.sav_count
= (int)nspares
;
1069 spa
->spa_spares
.sav_vdevs
= NULL
;
1075 * Construct the array of vdevs, opening them to get status in the
1076 * process. For each spare, there is potentially two different vdev_t
1077 * structures associated with it: one in the list of spares (used only
1078 * for basic validation purposes) and one in the active vdev
1079 * configuration (if it's spared in). During this phase we open and
1080 * validate each vdev on the spare list. If the vdev also exists in the
1081 * active configuration, then we also mark this vdev as an active spare.
1083 spa
->spa_spares
.sav_vdevs
= kmem_alloc(nspares
* sizeof (void *),
1085 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1086 VERIFY(spa_config_parse(spa
, &vd
, spares
[i
], NULL
, 0,
1087 VDEV_ALLOC_SPARE
) == 0);
1090 spa
->spa_spares
.sav_vdevs
[i
] = vd
;
1092 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1093 B_FALSE
)) != NULL
) {
1094 if (!tvd
->vdev_isspare
)
1098 * We only mark the spare active if we were successfully
1099 * able to load the vdev. Otherwise, importing a pool
1100 * with a bad active spare would result in strange
1101 * behavior, because multiple pool would think the spare
1102 * is actively in use.
1104 * There is a vulnerability here to an equally bizarre
1105 * circumstance, where a dead active spare is later
1106 * brought back to life (onlined or otherwise). Given
1107 * the rarity of this scenario, and the extra complexity
1108 * it adds, we ignore the possibility.
1110 if (!vdev_is_dead(tvd
))
1111 spa_spare_activate(tvd
);
1115 vd
->vdev_aux
= &spa
->spa_spares
;
1117 if (vdev_open(vd
) != 0)
1120 if (vdev_validate_aux(vd
) == 0)
1125 * Recompute the stashed list of spares, with status information
1128 VERIFY(nvlist_remove(spa
->spa_spares
.sav_config
, ZPOOL_CONFIG_SPARES
,
1129 DATA_TYPE_NVLIST_ARRAY
) == 0);
1131 spares
= kmem_alloc(spa
->spa_spares
.sav_count
* sizeof (void *),
1133 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1134 spares
[i
] = vdev_config_generate(spa
,
1135 spa
->spa_spares
.sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_SPARE
);
1136 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
1137 ZPOOL_CONFIG_SPARES
, spares
, spa
->spa_spares
.sav_count
) == 0);
1138 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1139 nvlist_free(spares
[i
]);
1140 kmem_free(spares
, spa
->spa_spares
.sav_count
* sizeof (void *));
1144 * Load (or re-load) the current list of vdevs describing the active l2cache for
1145 * this pool. When this is called, we have some form of basic information in
1146 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1147 * then re-generate a more complete list including status information.
1148 * Devices which are already active have their details maintained, and are
1152 spa_load_l2cache(spa_t
*spa
)
1156 int i
, j
, oldnvdevs
;
1158 vdev_t
*vd
, **oldvdevs
, **newvdevs
= NULL
;
1159 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
1161 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1163 if (sav
->sav_config
!= NULL
) {
1164 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
,
1165 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
1166 newvdevs
= kmem_alloc(nl2cache
* sizeof (void *), KM_SLEEP
);
1171 oldvdevs
= sav
->sav_vdevs
;
1172 oldnvdevs
= sav
->sav_count
;
1173 sav
->sav_vdevs
= NULL
;
1177 * Process new nvlist of vdevs.
1179 for (i
= 0; i
< nl2cache
; i
++) {
1180 VERIFY(nvlist_lookup_uint64(l2cache
[i
], ZPOOL_CONFIG_GUID
,
1184 for (j
= 0; j
< oldnvdevs
; j
++) {
1186 if (vd
!= NULL
&& guid
== vd
->vdev_guid
) {
1188 * Retain previous vdev for add/remove ops.
1196 if (newvdevs
[i
] == NULL
) {
1200 VERIFY(spa_config_parse(spa
, &vd
, l2cache
[i
], NULL
, 0,
1201 VDEV_ALLOC_L2CACHE
) == 0);
1206 * Commit this vdev as an l2cache device,
1207 * even if it fails to open.
1209 spa_l2cache_add(vd
);
1214 spa_l2cache_activate(vd
);
1216 if (vdev_open(vd
) != 0)
1219 (void) vdev_validate_aux(vd
);
1221 if (!vdev_is_dead(vd
))
1222 l2arc_add_vdev(spa
, vd
);
1227 * Purge vdevs that were dropped
1229 for (i
= 0; i
< oldnvdevs
; i
++) {
1234 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
1235 pool
!= 0ULL && l2arc_vdev_present(vd
))
1236 l2arc_remove_vdev(vd
);
1237 (void) vdev_close(vd
);
1238 spa_l2cache_remove(vd
);
1243 kmem_free(oldvdevs
, oldnvdevs
* sizeof (void *));
1245 if (sav
->sav_config
== NULL
)
1248 sav
->sav_vdevs
= newvdevs
;
1249 sav
->sav_count
= (int)nl2cache
;
1252 * Recompute the stashed list of l2cache devices, with status
1253 * information this time.
1255 VERIFY(nvlist_remove(sav
->sav_config
, ZPOOL_CONFIG_L2CACHE
,
1256 DATA_TYPE_NVLIST_ARRAY
) == 0);
1258 l2cache
= kmem_alloc(sav
->sav_count
* sizeof (void *), KM_SLEEP
);
1259 for (i
= 0; i
< sav
->sav_count
; i
++)
1260 l2cache
[i
] = vdev_config_generate(spa
,
1261 sav
->sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_L2CACHE
);
1262 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
,
1263 ZPOOL_CONFIG_L2CACHE
, l2cache
, sav
->sav_count
) == 0);
1265 for (i
= 0; i
< sav
->sav_count
; i
++)
1266 nvlist_free(l2cache
[i
]);
1268 kmem_free(l2cache
, sav
->sav_count
* sizeof (void *));
1272 load_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
**value
)
1275 char *packed
= NULL
;
1280 VERIFY(0 == dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
));
1281 nvsize
= *(uint64_t *)db
->db_data
;
1282 dmu_buf_rele(db
, FTAG
);
1284 packed
= kmem_alloc(nvsize
, KM_SLEEP
| KM_NODEBUG
);
1285 error
= dmu_read(spa
->spa_meta_objset
, obj
, 0, nvsize
, packed
,
1288 error
= nvlist_unpack(packed
, nvsize
, value
, 0);
1289 kmem_free(packed
, nvsize
);
1295 * Checks to see if the given vdev could not be opened, in which case we post a
1296 * sysevent to notify the autoreplace code that the device has been removed.
1299 spa_check_removed(vdev_t
*vd
)
1303 for (c
= 0; c
< vd
->vdev_children
; c
++)
1304 spa_check_removed(vd
->vdev_child
[c
]);
1306 if (vd
->vdev_ops
->vdev_op_leaf
&& vdev_is_dead(vd
)) {
1307 zfs_ereport_post(FM_EREPORT_RESOURCE_AUTOREPLACE
,
1308 vd
->vdev_spa
, vd
, NULL
, 0, 0);
1309 spa_event_notify(vd
->vdev_spa
, vd
, FM_EREPORT_ZFS_DEVICE_CHECK
);
1314 * Validate the current config against the MOS config
1317 spa_config_valid(spa_t
*spa
, nvlist_t
*config
)
1319 vdev_t
*mrvd
, *rvd
= spa
->spa_root_vdev
;
1323 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nv
) == 0);
1325 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1326 VERIFY(spa_config_parse(spa
, &mrvd
, nv
, NULL
, 0, VDEV_ALLOC_LOAD
) == 0);
1328 ASSERT3U(rvd
->vdev_children
, ==, mrvd
->vdev_children
);
1331 * If we're doing a normal import, then build up any additional
1332 * diagnostic information about missing devices in this config.
1333 * We'll pass this up to the user for further processing.
1335 if (!(spa
->spa_import_flags
& ZFS_IMPORT_MISSING_LOG
)) {
1336 nvlist_t
**child
, *nv
;
1339 child
= kmem_alloc(rvd
->vdev_children
* sizeof (nvlist_t
**),
1341 VERIFY(nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
1343 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1344 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1345 vdev_t
*mtvd
= mrvd
->vdev_child
[c
];
1347 if (tvd
->vdev_ops
== &vdev_missing_ops
&&
1348 mtvd
->vdev_ops
!= &vdev_missing_ops
&&
1350 child
[idx
++] = vdev_config_generate(spa
, mtvd
,
1355 VERIFY(nvlist_add_nvlist_array(nv
,
1356 ZPOOL_CONFIG_CHILDREN
, child
, idx
) == 0);
1357 VERIFY(nvlist_add_nvlist(spa
->spa_load_info
,
1358 ZPOOL_CONFIG_MISSING_DEVICES
, nv
) == 0);
1360 for (i
= 0; i
< idx
; i
++)
1361 nvlist_free(child
[i
]);
1364 kmem_free(child
, rvd
->vdev_children
* sizeof (char **));
1368 * Compare the root vdev tree with the information we have
1369 * from the MOS config (mrvd). Check each top-level vdev
1370 * with the corresponding MOS config top-level (mtvd).
1372 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1373 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1374 vdev_t
*mtvd
= mrvd
->vdev_child
[c
];
1377 * Resolve any "missing" vdevs in the current configuration.
1378 * If we find that the MOS config has more accurate information
1379 * about the top-level vdev then use that vdev instead.
1381 if (tvd
->vdev_ops
== &vdev_missing_ops
&&
1382 mtvd
->vdev_ops
!= &vdev_missing_ops
) {
1384 if (!(spa
->spa_import_flags
& ZFS_IMPORT_MISSING_LOG
))
1388 * Device specific actions.
1390 if (mtvd
->vdev_islog
) {
1391 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
1394 * XXX - once we have 'readonly' pool
1395 * support we should be able to handle
1396 * missing data devices by transitioning
1397 * the pool to readonly.
1403 * Swap the missing vdev with the data we were
1404 * able to obtain from the MOS config.
1406 vdev_remove_child(rvd
, tvd
);
1407 vdev_remove_child(mrvd
, mtvd
);
1409 vdev_add_child(rvd
, mtvd
);
1410 vdev_add_child(mrvd
, tvd
);
1412 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1414 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1417 } else if (mtvd
->vdev_islog
) {
1419 * Load the slog device's state from the MOS config
1420 * since it's possible that the label does not
1421 * contain the most up-to-date information.
1423 vdev_load_log_state(tvd
, mtvd
);
1428 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1431 * Ensure we were able to validate the config.
1433 return (rvd
->vdev_guid_sum
== spa
->spa_uberblock
.ub_guid_sum
);
1437 * Check for missing log devices
1440 spa_check_logs(spa_t
*spa
)
1442 switch (spa
->spa_log_state
) {
1445 case SPA_LOG_MISSING
:
1446 /* need to recheck in case slog has been restored */
1447 case SPA_LOG_UNKNOWN
:
1448 if (dmu_objset_find(spa
->spa_name
, zil_check_log_chain
, NULL
,
1449 DS_FIND_CHILDREN
)) {
1450 spa_set_log_state(spa
, SPA_LOG_MISSING
);
1459 spa_passivate_log(spa_t
*spa
)
1461 vdev_t
*rvd
= spa
->spa_root_vdev
;
1462 boolean_t slog_found
= B_FALSE
;
1465 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
1467 if (!spa_has_slogs(spa
))
1470 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1471 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1472 metaslab_group_t
*mg
= tvd
->vdev_mg
;
1474 if (tvd
->vdev_islog
) {
1475 metaslab_group_passivate(mg
);
1476 slog_found
= B_TRUE
;
1480 return (slog_found
);
1484 spa_activate_log(spa_t
*spa
)
1486 vdev_t
*rvd
= spa
->spa_root_vdev
;
1489 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
1491 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1492 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1493 metaslab_group_t
*mg
= tvd
->vdev_mg
;
1495 if (tvd
->vdev_islog
)
1496 metaslab_group_activate(mg
);
1501 spa_offline_log(spa_t
*spa
)
1505 if ((error
= dmu_objset_find(spa_name(spa
), zil_vdev_offline
,
1506 NULL
, DS_FIND_CHILDREN
)) == 0) {
1509 * We successfully offlined the log device, sync out the
1510 * current txg so that the "stubby" block can be removed
1513 txg_wait_synced(spa
->spa_dsl_pool
, 0);
1519 spa_aux_check_removed(spa_aux_vdev_t
*sav
)
1523 for (i
= 0; i
< sav
->sav_count
; i
++)
1524 spa_check_removed(sav
->sav_vdevs
[i
]);
1528 spa_claim_notify(zio_t
*zio
)
1530 spa_t
*spa
= zio
->io_spa
;
1535 mutex_enter(&spa
->spa_props_lock
); /* any mutex will do */
1536 if (spa
->spa_claim_max_txg
< zio
->io_bp
->blk_birth
)
1537 spa
->spa_claim_max_txg
= zio
->io_bp
->blk_birth
;
1538 mutex_exit(&spa
->spa_props_lock
);
1541 typedef struct spa_load_error
{
1542 uint64_t sle_meta_count
;
1543 uint64_t sle_data_count
;
1547 spa_load_verify_done(zio_t
*zio
)
1549 blkptr_t
*bp
= zio
->io_bp
;
1550 spa_load_error_t
*sle
= zio
->io_private
;
1551 dmu_object_type_t type
= BP_GET_TYPE(bp
);
1552 int error
= zio
->io_error
;
1555 if ((BP_GET_LEVEL(bp
) != 0 || dmu_ot
[type
].ot_metadata
) &&
1556 type
!= DMU_OT_INTENT_LOG
)
1557 atomic_add_64(&sle
->sle_meta_count
, 1);
1559 atomic_add_64(&sle
->sle_data_count
, 1);
1561 zio_data_buf_free(zio
->io_data
, zio
->io_size
);
1566 spa_load_verify_cb(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
1567 arc_buf_t
*pbuf
, const zbookmark_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
1571 size_t size
= BP_GET_PSIZE(bp
);
1572 void *data
= zio_data_buf_alloc(size
);
1574 zio_nowait(zio_read(rio
, spa
, bp
, data
, size
,
1575 spa_load_verify_done
, rio
->io_private
, ZIO_PRIORITY_SCRUB
,
1576 ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_CANFAIL
|
1577 ZIO_FLAG_SCRUB
| ZIO_FLAG_RAW
, zb
));
1583 spa_load_verify(spa_t
*spa
)
1586 spa_load_error_t sle
= { 0 };
1587 zpool_rewind_policy_t policy
;
1588 boolean_t verify_ok
= B_FALSE
;
1591 zpool_get_rewind_policy(spa
->spa_config
, &policy
);
1593 if (policy
.zrp_request
& ZPOOL_NEVER_REWIND
)
1596 rio
= zio_root(spa
, NULL
, &sle
,
1597 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
);
1599 error
= traverse_pool(spa
, spa
->spa_verify_min_txg
,
1600 TRAVERSE_PRE
| TRAVERSE_PREFETCH
, spa_load_verify_cb
, rio
);
1602 (void) zio_wait(rio
);
1604 spa
->spa_load_meta_errors
= sle
.sle_meta_count
;
1605 spa
->spa_load_data_errors
= sle
.sle_data_count
;
1607 if (!error
&& sle
.sle_meta_count
<= policy
.zrp_maxmeta
&&
1608 sle
.sle_data_count
<= policy
.zrp_maxdata
) {
1612 spa
->spa_load_txg
= spa
->spa_uberblock
.ub_txg
;
1613 spa
->spa_load_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
1615 loss
= spa
->spa_last_ubsync_txg_ts
- spa
->spa_load_txg_ts
;
1616 VERIFY(nvlist_add_uint64(spa
->spa_load_info
,
1617 ZPOOL_CONFIG_LOAD_TIME
, spa
->spa_load_txg_ts
) == 0);
1618 VERIFY(nvlist_add_int64(spa
->spa_load_info
,
1619 ZPOOL_CONFIG_REWIND_TIME
, loss
) == 0);
1620 VERIFY(nvlist_add_uint64(spa
->spa_load_info
,
1621 ZPOOL_CONFIG_LOAD_DATA_ERRORS
, sle
.sle_data_count
) == 0);
1623 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
;
1627 if (error
!= ENXIO
&& error
!= EIO
)
1632 return (verify_ok
? 0 : EIO
);
1636 * Find a value in the pool props object.
1639 spa_prop_find(spa_t
*spa
, zpool_prop_t prop
, uint64_t *val
)
1641 (void) zap_lookup(spa
->spa_meta_objset
, spa
->spa_pool_props_object
,
1642 zpool_prop_to_name(prop
), sizeof (uint64_t), 1, val
);
1646 * Find a value in the pool directory object.
1649 spa_dir_prop(spa_t
*spa
, const char *name
, uint64_t *val
)
1651 return (zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
1652 name
, sizeof (uint64_t), 1, val
));
1656 spa_vdev_err(vdev_t
*vdev
, vdev_aux_t aux
, int err
)
1658 vdev_set_state(vdev
, B_TRUE
, VDEV_STATE_CANT_OPEN
, aux
);
1663 * Fix up config after a partly-completed split. This is done with the
1664 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
1665 * pool have that entry in their config, but only the splitting one contains
1666 * a list of all the guids of the vdevs that are being split off.
1668 * This function determines what to do with that list: either rejoin
1669 * all the disks to the pool, or complete the splitting process. To attempt
1670 * the rejoin, each disk that is offlined is marked online again, and
1671 * we do a reopen() call. If the vdev label for every disk that was
1672 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
1673 * then we call vdev_split() on each disk, and complete the split.
1675 * Otherwise we leave the config alone, with all the vdevs in place in
1676 * the original pool.
1679 spa_try_repair(spa_t
*spa
, nvlist_t
*config
)
1686 boolean_t attempt_reopen
;
1688 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
, &nvl
) != 0)
1691 /* check that the config is complete */
1692 if (nvlist_lookup_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
1693 &glist
, &gcount
) != 0)
1696 vd
= kmem_zalloc(gcount
* sizeof (vdev_t
*), KM_SLEEP
);
1698 /* attempt to online all the vdevs & validate */
1699 attempt_reopen
= B_TRUE
;
1700 for (i
= 0; i
< gcount
; i
++) {
1701 if (glist
[i
] == 0) /* vdev is hole */
1704 vd
[i
] = spa_lookup_by_guid(spa
, glist
[i
], B_FALSE
);
1705 if (vd
[i
] == NULL
) {
1707 * Don't bother attempting to reopen the disks;
1708 * just do the split.
1710 attempt_reopen
= B_FALSE
;
1712 /* attempt to re-online it */
1713 vd
[i
]->vdev_offline
= B_FALSE
;
1717 if (attempt_reopen
) {
1718 vdev_reopen(spa
->spa_root_vdev
);
1720 /* check each device to see what state it's in */
1721 for (extracted
= 0, i
= 0; i
< gcount
; i
++) {
1722 if (vd
[i
] != NULL
&&
1723 vd
[i
]->vdev_stat
.vs_aux
!= VDEV_AUX_SPLIT_POOL
)
1730 * If every disk has been moved to the new pool, or if we never
1731 * even attempted to look at them, then we split them off for
1734 if (!attempt_reopen
|| gcount
== extracted
) {
1735 for (i
= 0; i
< gcount
; i
++)
1738 vdev_reopen(spa
->spa_root_vdev
);
1741 kmem_free(vd
, gcount
* sizeof (vdev_t
*));
1745 spa_load(spa_t
*spa
, spa_load_state_t state
, spa_import_type_t type
,
1746 boolean_t mosconfig
)
1748 nvlist_t
*config
= spa
->spa_config
;
1749 char *ereport
= FM_EREPORT_ZFS_POOL
;
1754 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
, &pool_guid
))
1758 * Versioning wasn't explicitly added to the label until later, so if
1759 * it's not present treat it as the initial version.
1761 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
1762 &spa
->spa_ubsync
.ub_version
) != 0)
1763 spa
->spa_ubsync
.ub_version
= SPA_VERSION_INITIAL
;
1765 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
1766 &spa
->spa_config_txg
);
1768 if ((state
== SPA_LOAD_IMPORT
|| state
== SPA_LOAD_TRYIMPORT
) &&
1769 spa_guid_exists(pool_guid
, 0)) {
1772 spa
->spa_load_guid
= pool_guid
;
1774 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
,
1776 VERIFY(nvlist_dup(nvl
, &spa
->spa_config_splitting
,
1780 gethrestime(&spa
->spa_loaded_ts
);
1781 error
= spa_load_impl(spa
, pool_guid
, config
, state
, type
,
1782 mosconfig
, &ereport
);
1785 spa
->spa_minref
= refcount_count(&spa
->spa_refcount
);
1787 if (error
!= EEXIST
) {
1788 spa
->spa_loaded_ts
.tv_sec
= 0;
1789 spa
->spa_loaded_ts
.tv_nsec
= 0;
1791 if (error
!= EBADF
) {
1792 zfs_ereport_post(ereport
, spa
, NULL
, NULL
, 0, 0);
1795 spa
->spa_load_state
= error
? SPA_LOAD_ERROR
: SPA_LOAD_NONE
;
1802 * Load an existing storage pool, using the pool's builtin spa_config as a
1803 * source of configuration information.
1805 __attribute__((always_inline
))
1807 spa_load_impl(spa_t
*spa
, uint64_t pool_guid
, nvlist_t
*config
,
1808 spa_load_state_t state
, spa_import_type_t type
, boolean_t mosconfig
,
1812 nvlist_t
*nvroot
= NULL
;
1814 uberblock_t
*ub
= &spa
->spa_uberblock
;
1815 uint64_t children
, config_cache_txg
= spa
->spa_config_txg
;
1816 int orig_mode
= spa
->spa_mode
;
1821 * If this is an untrusted config, access the pool in read-only mode.
1822 * This prevents things like resilvering recently removed devices.
1825 spa
->spa_mode
= FREAD
;
1827 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1829 spa
->spa_load_state
= state
;
1831 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvroot
))
1834 parse
= (type
== SPA_IMPORT_EXISTING
?
1835 VDEV_ALLOC_LOAD
: VDEV_ALLOC_SPLIT
);
1838 * Create "The Godfather" zio to hold all async IOs
1840 spa
->spa_async_zio_root
= zio_root(spa
, NULL
, NULL
,
1841 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_GODFATHER
);
1844 * Parse the configuration into a vdev tree. We explicitly set the
1845 * value that will be returned by spa_version() since parsing the
1846 * configuration requires knowing the version number.
1848 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1849 error
= spa_config_parse(spa
, &rvd
, nvroot
, NULL
, 0, parse
);
1850 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1855 ASSERT(spa
->spa_root_vdev
== rvd
);
1857 if (type
!= SPA_IMPORT_ASSEMBLE
) {
1858 ASSERT(spa_guid(spa
) == pool_guid
);
1862 * Try to open all vdevs, loading each label in the process.
1864 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1865 error
= vdev_open(rvd
);
1866 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1871 * We need to validate the vdev labels against the configuration that
1872 * we have in hand, which is dependent on the setting of mosconfig. If
1873 * mosconfig is true then we're validating the vdev labels based on
1874 * that config. Otherwise, we're validating against the cached config
1875 * (zpool.cache) that was read when we loaded the zfs module, and then
1876 * later we will recursively call spa_load() and validate against
1879 * If we're assembling a new pool that's been split off from an
1880 * existing pool, the labels haven't yet been updated so we skip
1881 * validation for now.
1883 if (type
!= SPA_IMPORT_ASSEMBLE
) {
1884 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1885 error
= vdev_validate(rvd
);
1886 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1891 if (rvd
->vdev_state
<= VDEV_STATE_CANT_OPEN
)
1896 * Find the best uberblock.
1898 vdev_uberblock_load(NULL
, rvd
, ub
);
1901 * If we weren't able to find a single valid uberblock, return failure.
1903 if (ub
->ub_txg
== 0)
1904 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, ENXIO
));
1907 * If the pool is newer than the code, we can't open it.
1909 if (ub
->ub_version
> SPA_VERSION
)
1910 return (spa_vdev_err(rvd
, VDEV_AUX_VERSION_NEWER
, ENOTSUP
));
1913 * If the vdev guid sum doesn't match the uberblock, we have an
1914 * incomplete configuration. We first check to see if the pool
1915 * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN).
1916 * If it is, defer the vdev_guid_sum check till later so we
1917 * can handle missing vdevs.
1919 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VDEV_CHILDREN
,
1920 &children
) != 0 && mosconfig
&& type
!= SPA_IMPORT_ASSEMBLE
&&
1921 rvd
->vdev_guid_sum
!= ub
->ub_guid_sum
)
1922 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
, ENXIO
));
1924 if (type
!= SPA_IMPORT_ASSEMBLE
&& spa
->spa_config_splitting
) {
1925 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1926 spa_try_repair(spa
, config
);
1927 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1928 nvlist_free(spa
->spa_config_splitting
);
1929 spa
->spa_config_splitting
= NULL
;
1933 * Initialize internal SPA structures.
1935 spa
->spa_state
= POOL_STATE_ACTIVE
;
1936 spa
->spa_ubsync
= spa
->spa_uberblock
;
1937 spa
->spa_verify_min_txg
= spa
->spa_extreme_rewind
?
1938 TXG_INITIAL
- 1 : spa_last_synced_txg(spa
) - TXG_DEFER_SIZE
- 1;
1939 spa
->spa_first_txg
= spa
->spa_last_ubsync_txg
?
1940 spa
->spa_last_ubsync_txg
: spa_last_synced_txg(spa
) + 1;
1941 spa
->spa_claim_max_txg
= spa
->spa_first_txg
;
1942 spa
->spa_prev_software_version
= ub
->ub_software_version
;
1944 error
= dsl_pool_open(spa
, spa
->spa_first_txg
, &spa
->spa_dsl_pool
);
1946 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
1947 spa
->spa_meta_objset
= spa
->spa_dsl_pool
->dp_meta_objset
;
1949 if (spa_dir_prop(spa
, DMU_POOL_CONFIG
, &spa
->spa_config_object
) != 0)
1950 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
1954 nvlist_t
*policy
= NULL
, *nvconfig
;
1956 if (load_nvlist(spa
, spa
->spa_config_object
, &nvconfig
) != 0)
1957 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
1959 if (!spa_is_root(spa
) && nvlist_lookup_uint64(nvconfig
,
1960 ZPOOL_CONFIG_HOSTID
, &hostid
) == 0) {
1962 unsigned long myhostid
= 0;
1964 VERIFY(nvlist_lookup_string(nvconfig
,
1965 ZPOOL_CONFIG_HOSTNAME
, &hostname
) == 0);
1968 myhostid
= zone_get_hostid(NULL
);
1971 * We're emulating the system's hostid in userland, so
1972 * we can't use zone_get_hostid().
1974 (void) ddi_strtoul(hw_serial
, NULL
, 10, &myhostid
);
1975 #endif /* _KERNEL */
1976 if (hostid
!= 0 && myhostid
!= 0 &&
1977 hostid
!= myhostid
) {
1978 nvlist_free(nvconfig
);
1979 cmn_err(CE_WARN
, "pool '%s' could not be "
1980 "loaded as it was last accessed by "
1981 "another system (host: %s hostid: 0x%lx). "
1982 "See: http://zfsonlinux.org/msg/ZFS-8000-EY",
1983 spa_name(spa
), hostname
,
1984 (unsigned long)hostid
);
1988 if (nvlist_lookup_nvlist(spa
->spa_config
,
1989 ZPOOL_REWIND_POLICY
, &policy
) == 0)
1990 VERIFY(nvlist_add_nvlist(nvconfig
,
1991 ZPOOL_REWIND_POLICY
, policy
) == 0);
1993 spa_config_set(spa
, nvconfig
);
1995 spa_deactivate(spa
);
1996 spa_activate(spa
, orig_mode
);
1998 return (spa_load(spa
, state
, SPA_IMPORT_EXISTING
, B_TRUE
));
2001 if (spa_dir_prop(spa
, DMU_POOL_SYNC_BPOBJ
, &obj
) != 0)
2002 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2003 error
= bpobj_open(&spa
->spa_deferred_bpobj
, spa
->spa_meta_objset
, obj
);
2005 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2008 * Load the bit that tells us to use the new accounting function
2009 * (raid-z deflation). If we have an older pool, this will not
2012 error
= spa_dir_prop(spa
, DMU_POOL_DEFLATE
, &spa
->spa_deflate
);
2013 if (error
!= 0 && error
!= ENOENT
)
2014 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2016 error
= spa_dir_prop(spa
, DMU_POOL_CREATION_VERSION
,
2017 &spa
->spa_creation_version
);
2018 if (error
!= 0 && error
!= ENOENT
)
2019 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2022 * Load the persistent error log. If we have an older pool, this will
2025 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_LAST
, &spa
->spa_errlog_last
);
2026 if (error
!= 0 && error
!= ENOENT
)
2027 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2029 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_SCRUB
,
2030 &spa
->spa_errlog_scrub
);
2031 if (error
!= 0 && error
!= ENOENT
)
2032 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2035 * Load the history object. If we have an older pool, this
2036 * will not be present.
2038 error
= spa_dir_prop(spa
, DMU_POOL_HISTORY
, &spa
->spa_history
);
2039 if (error
!= 0 && error
!= ENOENT
)
2040 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2043 * If we're assembling the pool from the split-off vdevs of
2044 * an existing pool, we don't want to attach the spares & cache
2049 * Load any hot spares for this pool.
2051 error
= spa_dir_prop(spa
, DMU_POOL_SPARES
, &spa
->spa_spares
.sav_object
);
2052 if (error
!= 0 && error
!= ENOENT
)
2053 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2054 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
2055 ASSERT(spa_version(spa
) >= SPA_VERSION_SPARES
);
2056 if (load_nvlist(spa
, spa
->spa_spares
.sav_object
,
2057 &spa
->spa_spares
.sav_config
) != 0)
2058 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2060 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2061 spa_load_spares(spa
);
2062 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2063 } else if (error
== 0) {
2064 spa
->spa_spares
.sav_sync
= B_TRUE
;
2068 * Load any level 2 ARC devices for this pool.
2070 error
= spa_dir_prop(spa
, DMU_POOL_L2CACHE
,
2071 &spa
->spa_l2cache
.sav_object
);
2072 if (error
!= 0 && error
!= ENOENT
)
2073 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2074 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
2075 ASSERT(spa_version(spa
) >= SPA_VERSION_L2CACHE
);
2076 if (load_nvlist(spa
, spa
->spa_l2cache
.sav_object
,
2077 &spa
->spa_l2cache
.sav_config
) != 0)
2078 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2080 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2081 spa_load_l2cache(spa
);
2082 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2083 } else if (error
== 0) {
2084 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
2087 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
2089 error
= spa_dir_prop(spa
, DMU_POOL_PROPS
, &spa
->spa_pool_props_object
);
2090 if (error
&& error
!= ENOENT
)
2091 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2094 uint64_t autoreplace
;
2096 spa_prop_find(spa
, ZPOOL_PROP_BOOTFS
, &spa
->spa_bootfs
);
2097 spa_prop_find(spa
, ZPOOL_PROP_AUTOREPLACE
, &autoreplace
);
2098 spa_prop_find(spa
, ZPOOL_PROP_DELEGATION
, &spa
->spa_delegation
);
2099 spa_prop_find(spa
, ZPOOL_PROP_FAILUREMODE
, &spa
->spa_failmode
);
2100 spa_prop_find(spa
, ZPOOL_PROP_AUTOEXPAND
, &spa
->spa_autoexpand
);
2101 spa_prop_find(spa
, ZPOOL_PROP_DEDUPDITTO
,
2102 &spa
->spa_dedup_ditto
);
2104 spa
->spa_autoreplace
= (autoreplace
!= 0);
2108 * If the 'autoreplace' property is set, then post a resource notifying
2109 * the ZFS DE that it should not issue any faults for unopenable
2110 * devices. We also iterate over the vdevs, and post a sysevent for any
2111 * unopenable vdevs so that the normal autoreplace handler can take
2114 if (spa
->spa_autoreplace
&& state
!= SPA_LOAD_TRYIMPORT
) {
2115 spa_check_removed(spa
->spa_root_vdev
);
2117 * For the import case, this is done in spa_import(), because
2118 * at this point we're using the spare definitions from
2119 * the MOS config, not necessarily from the userland config.
2121 if (state
!= SPA_LOAD_IMPORT
) {
2122 spa_aux_check_removed(&spa
->spa_spares
);
2123 spa_aux_check_removed(&spa
->spa_l2cache
);
2128 * Load the vdev state for all toplevel vdevs.
2133 * Propagate the leaf DTLs we just loaded all the way up the tree.
2135 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2136 vdev_dtl_reassess(rvd
, 0, 0, B_FALSE
);
2137 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2140 * Load the DDTs (dedup tables).
2142 error
= ddt_load(spa
);
2144 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2146 spa_update_dspace(spa
);
2149 * Validate the config, using the MOS config to fill in any
2150 * information which might be missing. If we fail to validate
2151 * the config then declare the pool unfit for use. If we're
2152 * assembling a pool from a split, the log is not transferred
2155 if (type
!= SPA_IMPORT_ASSEMBLE
) {
2158 if (load_nvlist(spa
, spa
->spa_config_object
, &nvconfig
) != 0)
2159 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2161 if (!spa_config_valid(spa
, nvconfig
)) {
2162 nvlist_free(nvconfig
);
2163 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
,
2166 nvlist_free(nvconfig
);
2169 * Now that we've validate the config, check the state of the
2170 * root vdev. If it can't be opened, it indicates one or
2171 * more toplevel vdevs are faulted.
2173 if (rvd
->vdev_state
<= VDEV_STATE_CANT_OPEN
)
2176 if (spa_check_logs(spa
)) {
2177 *ereport
= FM_EREPORT_ZFS_LOG_REPLAY
;
2178 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_LOG
, ENXIO
));
2183 * We've successfully opened the pool, verify that we're ready
2184 * to start pushing transactions.
2186 if (state
!= SPA_LOAD_TRYIMPORT
) {
2187 if ((error
= spa_load_verify(spa
)))
2188 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
2192 if (spa_writeable(spa
) && (state
== SPA_LOAD_RECOVER
||
2193 spa
->spa_load_max_txg
== UINT64_MAX
)) {
2195 int need_update
= B_FALSE
;
2198 ASSERT(state
!= SPA_LOAD_TRYIMPORT
);
2201 * Claim log blocks that haven't been committed yet.
2202 * This must all happen in a single txg.
2203 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
2204 * invoked from zil_claim_log_block()'s i/o done callback.
2205 * Price of rollback is that we abandon the log.
2207 spa
->spa_claiming
= B_TRUE
;
2209 tx
= dmu_tx_create_assigned(spa_get_dsl(spa
),
2210 spa_first_txg(spa
));
2211 (void) dmu_objset_find(spa_name(spa
),
2212 zil_claim
, tx
, DS_FIND_CHILDREN
);
2215 spa
->spa_claiming
= B_FALSE
;
2217 spa_set_log_state(spa
, SPA_LOG_GOOD
);
2218 spa
->spa_sync_on
= B_TRUE
;
2219 txg_sync_start(spa
->spa_dsl_pool
);
2222 * Wait for all claims to sync. We sync up to the highest
2223 * claimed log block birth time so that claimed log blocks
2224 * don't appear to be from the future. spa_claim_max_txg
2225 * will have been set for us by either zil_check_log_chain()
2226 * (invoked from spa_check_logs()) or zil_claim() above.
2228 txg_wait_synced(spa
->spa_dsl_pool
, spa
->spa_claim_max_txg
);
2231 * If the config cache is stale, or we have uninitialized
2232 * metaslabs (see spa_vdev_add()), then update the config.
2234 * If this is a verbatim import, trust the current
2235 * in-core spa_config and update the disk labels.
2237 if (config_cache_txg
!= spa
->spa_config_txg
||
2238 state
== SPA_LOAD_IMPORT
||
2239 state
== SPA_LOAD_RECOVER
||
2240 (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
))
2241 need_update
= B_TRUE
;
2243 for (c
= 0; c
< rvd
->vdev_children
; c
++)
2244 if (rvd
->vdev_child
[c
]->vdev_ms_array
== 0)
2245 need_update
= B_TRUE
;
2248 * Update the config cache asychronously in case we're the
2249 * root pool, in which case the config cache isn't writable yet.
2252 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
2255 * Check all DTLs to see if anything needs resilvering.
2257 if (!dsl_scan_resilvering(spa
->spa_dsl_pool
) &&
2258 vdev_resilver_needed(rvd
, NULL
, NULL
))
2259 spa_async_request(spa
, SPA_ASYNC_RESILVER
);
2262 * Delete any inconsistent datasets.
2264 (void) dmu_objset_find(spa_name(spa
),
2265 dsl_destroy_inconsistent
, NULL
, DS_FIND_CHILDREN
);
2268 * Clean up any stale temporary dataset userrefs.
2270 dsl_pool_clean_tmp_userrefs(spa
->spa_dsl_pool
);
2277 spa_load_retry(spa_t
*spa
, spa_load_state_t state
, int mosconfig
)
2279 int mode
= spa
->spa_mode
;
2282 spa_deactivate(spa
);
2284 spa
->spa_load_max_txg
--;
2286 spa_activate(spa
, mode
);
2287 spa_async_suspend(spa
);
2289 return (spa_load(spa
, state
, SPA_IMPORT_EXISTING
, mosconfig
));
2293 spa_load_best(spa_t
*spa
, spa_load_state_t state
, int mosconfig
,
2294 uint64_t max_request
, int rewind_flags
)
2296 nvlist_t
*config
= NULL
;
2297 int load_error
, rewind_error
;
2298 uint64_t safe_rewind_txg
;
2301 if (spa
->spa_load_txg
&& state
== SPA_LOAD_RECOVER
) {
2302 spa
->spa_load_max_txg
= spa
->spa_load_txg
;
2303 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
2305 spa
->spa_load_max_txg
= max_request
;
2308 load_error
= rewind_error
= spa_load(spa
, state
, SPA_IMPORT_EXISTING
,
2310 if (load_error
== 0)
2313 if (spa
->spa_root_vdev
!= NULL
)
2314 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
2316 spa
->spa_last_ubsync_txg
= spa
->spa_uberblock
.ub_txg
;
2317 spa
->spa_last_ubsync_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
2319 if (rewind_flags
& ZPOOL_NEVER_REWIND
) {
2320 nvlist_free(config
);
2321 return (load_error
);
2324 /* Price of rolling back is discarding txgs, including log */
2325 if (state
== SPA_LOAD_RECOVER
)
2326 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
2328 spa
->spa_load_max_txg
= spa
->spa_last_ubsync_txg
;
2329 safe_rewind_txg
= spa
->spa_last_ubsync_txg
- TXG_DEFER_SIZE
;
2330 min_txg
= (rewind_flags
& ZPOOL_EXTREME_REWIND
) ?
2331 TXG_INITIAL
: safe_rewind_txg
;
2334 * Continue as long as we're finding errors, we're still within
2335 * the acceptable rewind range, and we're still finding uberblocks
2337 while (rewind_error
&& spa
->spa_uberblock
.ub_txg
>= min_txg
&&
2338 spa
->spa_uberblock
.ub_txg
<= spa
->spa_load_max_txg
) {
2339 if (spa
->spa_load_max_txg
< safe_rewind_txg
)
2340 spa
->spa_extreme_rewind
= B_TRUE
;
2341 rewind_error
= spa_load_retry(spa
, state
, mosconfig
);
2344 spa
->spa_extreme_rewind
= B_FALSE
;
2345 spa
->spa_load_max_txg
= UINT64_MAX
;
2347 if (config
&& (rewind_error
|| state
!= SPA_LOAD_RECOVER
))
2348 spa_config_set(spa
, config
);
2350 return (state
== SPA_LOAD_RECOVER
? rewind_error
: load_error
);
2356 * The import case is identical to an open except that the configuration is sent
2357 * down from userland, instead of grabbed from the configuration cache. For the
2358 * case of an open, the pool configuration will exist in the
2359 * POOL_STATE_UNINITIALIZED state.
2361 * The stats information (gen/count/ustats) is used to gather vdev statistics at
2362 * the same time open the pool, without having to keep around the spa_t in some
2366 spa_open_common(const char *pool
, spa_t
**spapp
, void *tag
, nvlist_t
*nvpolicy
,
2370 spa_load_state_t state
= SPA_LOAD_OPEN
;
2372 int locked
= B_FALSE
;
2377 * As disgusting as this is, we need to support recursive calls to this
2378 * function because dsl_dir_open() is called during spa_load(), and ends
2379 * up calling spa_open() again. The real fix is to figure out how to
2380 * avoid dsl_dir_open() calling this in the first place.
2382 if (mutex_owner(&spa_namespace_lock
) != curthread
) {
2383 mutex_enter(&spa_namespace_lock
);
2387 if ((spa
= spa_lookup(pool
)) == NULL
) {
2389 mutex_exit(&spa_namespace_lock
);
2393 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
) {
2394 zpool_rewind_policy_t policy
;
2396 zpool_get_rewind_policy(nvpolicy
? nvpolicy
: spa
->spa_config
,
2398 if (policy
.zrp_request
& ZPOOL_DO_REWIND
)
2399 state
= SPA_LOAD_RECOVER
;
2401 spa_activate(spa
, spa_mode_global
);
2403 if (state
!= SPA_LOAD_RECOVER
)
2404 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
2406 error
= spa_load_best(spa
, state
, B_FALSE
, policy
.zrp_txg
,
2407 policy
.zrp_request
);
2409 if (error
== EBADF
) {
2411 * If vdev_validate() returns failure (indicated by
2412 * EBADF), it indicates that one of the vdevs indicates
2413 * that the pool has been exported or destroyed. If
2414 * this is the case, the config cache is out of sync and
2415 * we should remove the pool from the namespace.
2418 spa_deactivate(spa
);
2419 spa_config_sync(spa
, B_TRUE
, B_TRUE
);
2422 mutex_exit(&spa_namespace_lock
);
2428 * We can't open the pool, but we still have useful
2429 * information: the state of each vdev after the
2430 * attempted vdev_open(). Return this to the user.
2432 if (config
!= NULL
&& spa
->spa_config
) {
2433 VERIFY(nvlist_dup(spa
->spa_config
, config
,
2435 VERIFY(nvlist_add_nvlist(*config
,
2436 ZPOOL_CONFIG_LOAD_INFO
,
2437 spa
->spa_load_info
) == 0);
2440 spa_deactivate(spa
);
2441 spa
->spa_last_open_failed
= error
;
2443 mutex_exit(&spa_namespace_lock
);
2449 spa_open_ref(spa
, tag
);
2452 *config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
2455 * If we've recovered the pool, pass back any information we
2456 * gathered while doing the load.
2458 if (state
== SPA_LOAD_RECOVER
) {
2459 VERIFY(nvlist_add_nvlist(*config
, ZPOOL_CONFIG_LOAD_INFO
,
2460 spa
->spa_load_info
) == 0);
2464 spa
->spa_last_open_failed
= 0;
2465 spa
->spa_last_ubsync_txg
= 0;
2466 spa
->spa_load_txg
= 0;
2467 mutex_exit(&spa_namespace_lock
);
2476 spa_open_rewind(const char *name
, spa_t
**spapp
, void *tag
, nvlist_t
*policy
,
2479 return (spa_open_common(name
, spapp
, tag
, policy
, config
));
2483 spa_open(const char *name
, spa_t
**spapp
, void *tag
)
2485 return (spa_open_common(name
, spapp
, tag
, NULL
, NULL
));
2489 * Lookup the given spa_t, incrementing the inject count in the process,
2490 * preventing it from being exported or destroyed.
2493 spa_inject_addref(char *name
)
2497 mutex_enter(&spa_namespace_lock
);
2498 if ((spa
= spa_lookup(name
)) == NULL
) {
2499 mutex_exit(&spa_namespace_lock
);
2502 spa
->spa_inject_ref
++;
2503 mutex_exit(&spa_namespace_lock
);
2509 spa_inject_delref(spa_t
*spa
)
2511 mutex_enter(&spa_namespace_lock
);
2512 spa
->spa_inject_ref
--;
2513 mutex_exit(&spa_namespace_lock
);
2517 * Add spares device information to the nvlist.
2520 spa_add_spares(spa_t
*spa
, nvlist_t
*config
)
2530 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
2532 if (spa
->spa_spares
.sav_count
== 0)
2535 VERIFY(nvlist_lookup_nvlist(config
,
2536 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
2537 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
2538 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
2540 VERIFY(nvlist_add_nvlist_array(nvroot
,
2541 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
2542 VERIFY(nvlist_lookup_nvlist_array(nvroot
,
2543 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
2546 * Go through and find any spares which have since been
2547 * repurposed as an active spare. If this is the case, update
2548 * their status appropriately.
2550 for (i
= 0; i
< nspares
; i
++) {
2551 VERIFY(nvlist_lookup_uint64(spares
[i
],
2552 ZPOOL_CONFIG_GUID
, &guid
) == 0);
2553 if (spa_spare_exists(guid
, &pool
, NULL
) &&
2555 VERIFY(nvlist_lookup_uint64_array(
2556 spares
[i
], ZPOOL_CONFIG_VDEV_STATS
,
2557 (uint64_t **)&vs
, &vsc
) == 0);
2558 vs
->vs_state
= VDEV_STATE_CANT_OPEN
;
2559 vs
->vs_aux
= VDEV_AUX_SPARED
;
2566 * Add l2cache device information to the nvlist, including vdev stats.
2569 spa_add_l2cache(spa_t
*spa
, nvlist_t
*config
)
2572 uint_t i
, j
, nl2cache
;
2579 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
2581 if (spa
->spa_l2cache
.sav_count
== 0)
2584 VERIFY(nvlist_lookup_nvlist(config
,
2585 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
2586 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_l2cache
.sav_config
,
2587 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
2588 if (nl2cache
!= 0) {
2589 VERIFY(nvlist_add_nvlist_array(nvroot
,
2590 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
2591 VERIFY(nvlist_lookup_nvlist_array(nvroot
,
2592 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
2595 * Update level 2 cache device stats.
2598 for (i
= 0; i
< nl2cache
; i
++) {
2599 VERIFY(nvlist_lookup_uint64(l2cache
[i
],
2600 ZPOOL_CONFIG_GUID
, &guid
) == 0);
2603 for (j
= 0; j
< spa
->spa_l2cache
.sav_count
; j
++) {
2605 spa
->spa_l2cache
.sav_vdevs
[j
]->vdev_guid
) {
2606 vd
= spa
->spa_l2cache
.sav_vdevs
[j
];
2612 VERIFY(nvlist_lookup_uint64_array(l2cache
[i
],
2613 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
, &vsc
)
2615 vdev_get_stats(vd
, vs
);
2621 spa_get_stats(const char *name
, nvlist_t
**config
, char *altroot
, size_t buflen
)
2627 error
= spa_open_common(name
, &spa
, FTAG
, NULL
, config
);
2631 * This still leaves a window of inconsistency where the spares
2632 * or l2cache devices could change and the config would be
2633 * self-inconsistent.
2635 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
2637 if (*config
!= NULL
) {
2638 uint64_t loadtimes
[2];
2640 loadtimes
[0] = spa
->spa_loaded_ts
.tv_sec
;
2641 loadtimes
[1] = spa
->spa_loaded_ts
.tv_nsec
;
2642 VERIFY(nvlist_add_uint64_array(*config
,
2643 ZPOOL_CONFIG_LOADED_TIME
, loadtimes
, 2) == 0);
2645 VERIFY(nvlist_add_uint64(*config
,
2646 ZPOOL_CONFIG_ERRCOUNT
,
2647 spa_get_errlog_size(spa
)) == 0);
2649 if (spa_suspended(spa
))
2650 VERIFY(nvlist_add_uint64(*config
,
2651 ZPOOL_CONFIG_SUSPENDED
,
2652 spa
->spa_failmode
) == 0);
2654 spa_add_spares(spa
, *config
);
2655 spa_add_l2cache(spa
, *config
);
2660 * We want to get the alternate root even for faulted pools, so we cheat
2661 * and call spa_lookup() directly.
2665 mutex_enter(&spa_namespace_lock
);
2666 spa
= spa_lookup(name
);
2668 spa_altroot(spa
, altroot
, buflen
);
2672 mutex_exit(&spa_namespace_lock
);
2674 spa_altroot(spa
, altroot
, buflen
);
2679 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
2680 spa_close(spa
, FTAG
);
2687 * Validate that the auxiliary device array is well formed. We must have an
2688 * array of nvlists, each which describes a valid leaf vdev. If this is an
2689 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
2690 * specified, as long as they are well-formed.
2693 spa_validate_aux_devs(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
,
2694 spa_aux_vdev_t
*sav
, const char *config
, uint64_t version
,
2695 vdev_labeltype_t label
)
2702 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
2705 * It's acceptable to have no devs specified.
2707 if (nvlist_lookup_nvlist_array(nvroot
, config
, &dev
, &ndev
) != 0)
2714 * Make sure the pool is formatted with a version that supports this
2717 if (spa_version(spa
) < version
)
2721 * Set the pending device list so we correctly handle device in-use
2724 sav
->sav_pending
= dev
;
2725 sav
->sav_npending
= ndev
;
2727 for (i
= 0; i
< ndev
; i
++) {
2728 if ((error
= spa_config_parse(spa
, &vd
, dev
[i
], NULL
, 0,
2732 if (!vd
->vdev_ops
->vdev_op_leaf
) {
2739 * The L2ARC currently only supports disk devices in
2740 * kernel context. For user-level testing, we allow it.
2743 if ((strcmp(config
, ZPOOL_CONFIG_L2CACHE
) == 0) &&
2744 strcmp(vd
->vdev_ops
->vdev_op_type
, VDEV_TYPE_DISK
) != 0) {
2751 if ((error
= vdev_open(vd
)) == 0 &&
2752 (error
= vdev_label_init(vd
, crtxg
, label
)) == 0) {
2753 VERIFY(nvlist_add_uint64(dev
[i
], ZPOOL_CONFIG_GUID
,
2754 vd
->vdev_guid
) == 0);
2760 (mode
!= VDEV_ALLOC_SPARE
&& mode
!= VDEV_ALLOC_L2CACHE
))
2767 sav
->sav_pending
= NULL
;
2768 sav
->sav_npending
= 0;
2773 spa_validate_aux(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
)
2777 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
2779 if ((error
= spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
2780 &spa
->spa_spares
, ZPOOL_CONFIG_SPARES
, SPA_VERSION_SPARES
,
2781 VDEV_LABEL_SPARE
)) != 0) {
2785 return (spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
2786 &spa
->spa_l2cache
, ZPOOL_CONFIG_L2CACHE
, SPA_VERSION_L2CACHE
,
2787 VDEV_LABEL_L2CACHE
));
2791 spa_set_aux_vdevs(spa_aux_vdev_t
*sav
, nvlist_t
**devs
, int ndevs
,
2796 if (sav
->sav_config
!= NULL
) {
2802 * Generate new dev list by concatentating with the
2805 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
, config
,
2806 &olddevs
, &oldndevs
) == 0);
2808 newdevs
= kmem_alloc(sizeof (void *) *
2809 (ndevs
+ oldndevs
), KM_SLEEP
);
2810 for (i
= 0; i
< oldndevs
; i
++)
2811 VERIFY(nvlist_dup(olddevs
[i
], &newdevs
[i
],
2813 for (i
= 0; i
< ndevs
; i
++)
2814 VERIFY(nvlist_dup(devs
[i
], &newdevs
[i
+ oldndevs
],
2817 VERIFY(nvlist_remove(sav
->sav_config
, config
,
2818 DATA_TYPE_NVLIST_ARRAY
) == 0);
2820 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
,
2821 config
, newdevs
, ndevs
+ oldndevs
) == 0);
2822 for (i
= 0; i
< oldndevs
+ ndevs
; i
++)
2823 nvlist_free(newdevs
[i
]);
2824 kmem_free(newdevs
, (oldndevs
+ ndevs
) * sizeof (void *));
2827 * Generate a new dev list.
2829 VERIFY(nvlist_alloc(&sav
->sav_config
, NV_UNIQUE_NAME
,
2831 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
, config
,
2837 * Stop and drop level 2 ARC devices
2840 spa_l2cache_drop(spa_t
*spa
)
2844 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
2846 for (i
= 0; i
< sav
->sav_count
; i
++) {
2849 vd
= sav
->sav_vdevs
[i
];
2852 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
2853 pool
!= 0ULL && l2arc_vdev_present(vd
))
2854 l2arc_remove_vdev(vd
);
2855 if (vd
->vdev_isl2cache
)
2856 spa_l2cache_remove(vd
);
2857 vdev_clear_stats(vd
);
2858 (void) vdev_close(vd
);
2866 spa_create(const char *pool
, nvlist_t
*nvroot
, nvlist_t
*props
,
2867 const char *history_str
, nvlist_t
*zplprops
)
2870 char *altroot
= NULL
;
2875 uint64_t txg
= TXG_INITIAL
;
2876 nvlist_t
**spares
, **l2cache
;
2877 uint_t nspares
, nl2cache
;
2878 uint64_t version
, obj
;
2882 * If this pool already exists, return failure.
2884 mutex_enter(&spa_namespace_lock
);
2885 if (spa_lookup(pool
) != NULL
) {
2886 mutex_exit(&spa_namespace_lock
);
2891 * Allocate a new spa_t structure.
2893 (void) nvlist_lookup_string(props
,
2894 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
2895 spa
= spa_add(pool
, NULL
, altroot
);
2896 spa_activate(spa
, spa_mode_global
);
2898 if (props
&& (error
= spa_prop_validate(spa
, props
))) {
2899 spa_deactivate(spa
);
2901 mutex_exit(&spa_namespace_lock
);
2905 if (nvlist_lookup_uint64(props
, zpool_prop_to_name(ZPOOL_PROP_VERSION
),
2907 version
= SPA_VERSION
;
2908 ASSERT(version
<= SPA_VERSION
);
2910 spa
->spa_first_txg
= txg
;
2911 spa
->spa_uberblock
.ub_txg
= txg
- 1;
2912 spa
->spa_uberblock
.ub_version
= version
;
2913 spa
->spa_ubsync
= spa
->spa_uberblock
;
2916 * Create "The Godfather" zio to hold all async IOs
2918 spa
->spa_async_zio_root
= zio_root(spa
, NULL
, NULL
,
2919 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_GODFATHER
);
2922 * Create the root vdev.
2924 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2926 error
= spa_config_parse(spa
, &rvd
, nvroot
, NULL
, 0, VDEV_ALLOC_ADD
);
2928 ASSERT(error
!= 0 || rvd
!= NULL
);
2929 ASSERT(error
!= 0 || spa
->spa_root_vdev
== rvd
);
2931 if (error
== 0 && !zfs_allocatable_devs(nvroot
))
2935 (error
= vdev_create(rvd
, txg
, B_FALSE
)) == 0 &&
2936 (error
= spa_validate_aux(spa
, nvroot
, txg
,
2937 VDEV_ALLOC_ADD
)) == 0) {
2938 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
2939 vdev_metaslab_set_size(rvd
->vdev_child
[c
]);
2940 vdev_expand(rvd
->vdev_child
[c
], txg
);
2944 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2948 spa_deactivate(spa
);
2950 mutex_exit(&spa_namespace_lock
);
2955 * Get the list of spares, if specified.
2957 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
2958 &spares
, &nspares
) == 0) {
2959 VERIFY(nvlist_alloc(&spa
->spa_spares
.sav_config
, NV_UNIQUE_NAME
,
2961 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
2962 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
2963 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2964 spa_load_spares(spa
);
2965 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2966 spa
->spa_spares
.sav_sync
= B_TRUE
;
2970 * Get the list of level 2 cache devices, if specified.
2972 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
2973 &l2cache
, &nl2cache
) == 0) {
2974 VERIFY(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
2975 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2976 VERIFY(nvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
2977 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
2978 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2979 spa_load_l2cache(spa
);
2980 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2981 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
2984 spa
->spa_dsl_pool
= dp
= dsl_pool_create(spa
, zplprops
, txg
);
2985 spa
->spa_meta_objset
= dp
->dp_meta_objset
;
2988 * Create DDTs (dedup tables).
2992 spa_update_dspace(spa
);
2994 tx
= dmu_tx_create_assigned(dp
, txg
);
2997 * Create the pool config object.
2999 spa
->spa_config_object
= dmu_object_alloc(spa
->spa_meta_objset
,
3000 DMU_OT_PACKED_NVLIST
, SPA_CONFIG_BLOCKSIZE
,
3001 DMU_OT_PACKED_NVLIST_SIZE
, sizeof (uint64_t), tx
);
3003 if (zap_add(spa
->spa_meta_objset
,
3004 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CONFIG
,
3005 sizeof (uint64_t), 1, &spa
->spa_config_object
, tx
) != 0) {
3006 cmn_err(CE_PANIC
, "failed to add pool config");
3009 if (zap_add(spa
->spa_meta_objset
,
3010 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CREATION_VERSION
,
3011 sizeof (uint64_t), 1, &version
, tx
) != 0) {
3012 cmn_err(CE_PANIC
, "failed to add pool version");
3015 /* Newly created pools with the right version are always deflated. */
3016 if (version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
3017 spa
->spa_deflate
= TRUE
;
3018 if (zap_add(spa
->spa_meta_objset
,
3019 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
3020 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
) != 0) {
3021 cmn_err(CE_PANIC
, "failed to add deflate");
3026 * Create the deferred-free bpobj. Turn off compression
3027 * because sync-to-convergence takes longer if the blocksize
3030 obj
= bpobj_alloc(spa
->spa_meta_objset
, 1 << 14, tx
);
3031 dmu_object_set_compress(spa
->spa_meta_objset
, obj
,
3032 ZIO_COMPRESS_OFF
, tx
);
3033 if (zap_add(spa
->spa_meta_objset
,
3034 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_SYNC_BPOBJ
,
3035 sizeof (uint64_t), 1, &obj
, tx
) != 0) {
3036 cmn_err(CE_PANIC
, "failed to add bpobj");
3038 VERIFY3U(0, ==, bpobj_open(&spa
->spa_deferred_bpobj
,
3039 spa
->spa_meta_objset
, obj
));
3042 * Create the pool's history object.
3044 if (version
>= SPA_VERSION_ZPOOL_HISTORY
)
3045 spa_history_create_obj(spa
, tx
);
3048 * Set pool properties.
3050 spa
->spa_bootfs
= zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS
);
3051 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
3052 spa
->spa_failmode
= zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE
);
3053 spa
->spa_autoexpand
= zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND
);
3055 if (props
!= NULL
) {
3056 spa_configfile_set(spa
, props
, B_FALSE
);
3057 spa_sync_props(spa
, props
, tx
);
3062 spa
->spa_sync_on
= B_TRUE
;
3063 txg_sync_start(spa
->spa_dsl_pool
);
3066 * We explicitly wait for the first transaction to complete so that our
3067 * bean counters are appropriately updated.
3069 txg_wait_synced(spa
->spa_dsl_pool
, txg
);
3071 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
3073 if (version
>= SPA_VERSION_ZPOOL_HISTORY
&& history_str
!= NULL
)
3074 (void) spa_history_log(spa
, history_str
, LOG_CMD_POOL_CREATE
);
3075 spa_history_log_version(spa
, LOG_POOL_CREATE
);
3077 spa
->spa_minref
= refcount_count(&spa
->spa_refcount
);
3079 mutex_exit(&spa_namespace_lock
);
3086 * Get the root pool information from the root disk, then import the root pool
3087 * during the system boot up time.
3089 extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t
**);
3092 spa_generate_rootconf(char *devpath
, char *devid
, uint64_t *guid
)
3095 nvlist_t
*nvtop
, *nvroot
;
3098 if (vdev_disk_read_rootlabel(devpath
, devid
, &config
) != 0)
3102 * Add this top-level vdev to the child array.
3104 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
3106 VERIFY(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
3108 VERIFY(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_GUID
, guid
) == 0);
3111 * Put this pool's top-level vdevs into a root vdev.
3113 VERIFY(nvlist_alloc(&nvroot
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
3114 VERIFY(nvlist_add_string(nvroot
, ZPOOL_CONFIG_TYPE
,
3115 VDEV_TYPE_ROOT
) == 0);
3116 VERIFY(nvlist_add_uint64(nvroot
, ZPOOL_CONFIG_ID
, 0ULL) == 0);
3117 VERIFY(nvlist_add_uint64(nvroot
, ZPOOL_CONFIG_GUID
, pgid
) == 0);
3118 VERIFY(nvlist_add_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
3122 * Replace the existing vdev_tree with the new root vdev in
3123 * this pool's configuration (remove the old, add the new).
3125 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, nvroot
) == 0);
3126 nvlist_free(nvroot
);
3131 * Walk the vdev tree and see if we can find a device with "better"
3132 * configuration. A configuration is "better" if the label on that
3133 * device has a more recent txg.
3136 spa_alt_rootvdev(vdev_t
*vd
, vdev_t
**avd
, uint64_t *txg
)
3140 for (c
= 0; c
< vd
->vdev_children
; c
++)
3141 spa_alt_rootvdev(vd
->vdev_child
[c
], avd
, txg
);
3143 if (vd
->vdev_ops
->vdev_op_leaf
) {
3147 if (vdev_disk_read_rootlabel(vd
->vdev_physpath
, vd
->vdev_devid
,
3151 VERIFY(nvlist_lookup_uint64(label
, ZPOOL_CONFIG_POOL_TXG
,
3155 * Do we have a better boot device?
3157 if (label_txg
> *txg
) {
3166 * Import a root pool.
3168 * For x86. devpath_list will consist of devid and/or physpath name of
3169 * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
3170 * The GRUB "findroot" command will return the vdev we should boot.
3172 * For Sparc, devpath_list consists the physpath name of the booting device
3173 * no matter the rootpool is a single device pool or a mirrored pool.
3175 * "/pci@1f,0/ide@d/disk@0,0:a"
3178 spa_import_rootpool(char *devpath
, char *devid
)
3181 vdev_t
*rvd
, *bvd
, *avd
= NULL
;
3182 nvlist_t
*config
, *nvtop
;
3188 * Read the label from the boot device and generate a configuration.
3190 config
= spa_generate_rootconf(devpath
, devid
, &guid
);
3191 #if defined(_OBP) && defined(_KERNEL)
3192 if (config
== NULL
) {
3193 if (strstr(devpath
, "/iscsi/ssd") != NULL
) {
3195 get_iscsi_bootpath_phy(devpath
);
3196 config
= spa_generate_rootconf(devpath
, devid
, &guid
);
3200 if (config
== NULL
) {
3201 cmn_err(CE_NOTE
, "Can not read the pool label from '%s'",
3206 VERIFY(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
3208 VERIFY(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_TXG
, &txg
) == 0);
3210 mutex_enter(&spa_namespace_lock
);
3211 if ((spa
= spa_lookup(pname
)) != NULL
) {
3213 * Remove the existing root pool from the namespace so that we
3214 * can replace it with the correct config we just read in.
3219 spa
= spa_add(pname
, config
, NULL
);
3220 spa
->spa_is_root
= B_TRUE
;
3221 spa
->spa_import_flags
= ZFS_IMPORT_VERBATIM
;
3224 * Build up a vdev tree based on the boot device's label config.
3226 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
3228 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3229 error
= spa_config_parse(spa
, &rvd
, nvtop
, NULL
, 0,
3230 VDEV_ALLOC_ROOTPOOL
);
3231 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3233 mutex_exit(&spa_namespace_lock
);
3234 nvlist_free(config
);
3235 cmn_err(CE_NOTE
, "Can not parse the config for pool '%s'",
3241 * Get the boot vdev.
3243 if ((bvd
= vdev_lookup_by_guid(rvd
, guid
)) == NULL
) {
3244 cmn_err(CE_NOTE
, "Can not find the boot vdev for guid %llu",
3245 (u_longlong_t
)guid
);
3251 * Determine if there is a better boot device.
3254 spa_alt_rootvdev(rvd
, &avd
, &txg
);
3256 cmn_err(CE_NOTE
, "The boot device is 'degraded'. Please "
3257 "try booting from '%s'", avd
->vdev_path
);
3263 * If the boot device is part of a spare vdev then ensure that
3264 * we're booting off the active spare.
3266 if (bvd
->vdev_parent
->vdev_ops
== &vdev_spare_ops
&&
3267 !bvd
->vdev_isspare
) {
3268 cmn_err(CE_NOTE
, "The boot device is currently spared. Please "
3269 "try booting from '%s'",
3271 vdev_child
[bvd
->vdev_parent
->vdev_children
- 1]->vdev_path
);
3277 spa_history_log_version(spa
, LOG_POOL_IMPORT
);
3279 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3281 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3282 mutex_exit(&spa_namespace_lock
);
3284 nvlist_free(config
);
3291 * Import a non-root pool into the system.
3294 spa_import(const char *pool
, nvlist_t
*config
, nvlist_t
*props
, uint64_t flags
)
3297 char *altroot
= NULL
;
3298 spa_load_state_t state
= SPA_LOAD_IMPORT
;
3299 zpool_rewind_policy_t policy
;
3300 uint64_t mode
= spa_mode_global
;
3301 uint64_t readonly
= B_FALSE
;
3304 nvlist_t
**spares
, **l2cache
;
3305 uint_t nspares
, nl2cache
;
3308 * If a pool with this name exists, return failure.
3310 mutex_enter(&spa_namespace_lock
);
3311 if (spa_lookup(pool
) != NULL
) {
3312 mutex_exit(&spa_namespace_lock
);
3317 * Create and initialize the spa structure.
3319 (void) nvlist_lookup_string(props
,
3320 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
3321 (void) nvlist_lookup_uint64(props
,
3322 zpool_prop_to_name(ZPOOL_PROP_READONLY
), &readonly
);
3325 spa
= spa_add(pool
, config
, altroot
);
3326 spa
->spa_import_flags
= flags
;
3329 * Verbatim import - Take a pool and insert it into the namespace
3330 * as if it had been loaded at boot.
3332 if (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
) {
3334 spa_configfile_set(spa
, props
, B_FALSE
);
3336 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
3338 mutex_exit(&spa_namespace_lock
);
3339 spa_history_log_version(spa
, LOG_POOL_IMPORT
);
3344 spa_activate(spa
, mode
);
3347 * Don't start async tasks until we know everything is healthy.
3349 spa_async_suspend(spa
);
3351 zpool_get_rewind_policy(config
, &policy
);
3352 if (policy
.zrp_request
& ZPOOL_DO_REWIND
)
3353 state
= SPA_LOAD_RECOVER
;
3356 * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig
3357 * because the user-supplied config is actually the one to trust when
3360 if (state
!= SPA_LOAD_RECOVER
)
3361 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
3363 error
= spa_load_best(spa
, state
, B_TRUE
, policy
.zrp_txg
,
3364 policy
.zrp_request
);
3367 * Propagate anything learned while loading the pool and pass it
3368 * back to caller (i.e. rewind info, missing devices, etc).
3370 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
3371 spa
->spa_load_info
) == 0);
3373 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3375 * Toss any existing sparelist, as it doesn't have any validity
3376 * anymore, and conflicts with spa_has_spare().
3378 if (spa
->spa_spares
.sav_config
) {
3379 nvlist_free(spa
->spa_spares
.sav_config
);
3380 spa
->spa_spares
.sav_config
= NULL
;
3381 spa_load_spares(spa
);
3383 if (spa
->spa_l2cache
.sav_config
) {
3384 nvlist_free(spa
->spa_l2cache
.sav_config
);
3385 spa
->spa_l2cache
.sav_config
= NULL
;
3386 spa_load_l2cache(spa
);
3389 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
3392 error
= spa_validate_aux(spa
, nvroot
, -1ULL,
3395 error
= spa_validate_aux(spa
, nvroot
, -1ULL,
3396 VDEV_ALLOC_L2CACHE
);
3397 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3400 spa_configfile_set(spa
, props
, B_FALSE
);
3402 if (error
!= 0 || (props
&& spa_writeable(spa
) &&
3403 (error
= spa_prop_set(spa
, props
)))) {
3405 spa_deactivate(spa
);
3407 mutex_exit(&spa_namespace_lock
);
3411 spa_async_resume(spa
);
3414 * Override any spares and level 2 cache devices as specified by
3415 * the user, as these may have correct device names/devids, etc.
3417 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
3418 &spares
, &nspares
) == 0) {
3419 if (spa
->spa_spares
.sav_config
)
3420 VERIFY(nvlist_remove(spa
->spa_spares
.sav_config
,
3421 ZPOOL_CONFIG_SPARES
, DATA_TYPE_NVLIST_ARRAY
) == 0);
3423 VERIFY(nvlist_alloc(&spa
->spa_spares
.sav_config
,
3424 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
3425 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
3426 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
3427 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3428 spa_load_spares(spa
);
3429 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3430 spa
->spa_spares
.sav_sync
= B_TRUE
;
3432 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
3433 &l2cache
, &nl2cache
) == 0) {
3434 if (spa
->spa_l2cache
.sav_config
)
3435 VERIFY(nvlist_remove(spa
->spa_l2cache
.sav_config
,
3436 ZPOOL_CONFIG_L2CACHE
, DATA_TYPE_NVLIST_ARRAY
) == 0);
3438 VERIFY(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
3439 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
3440 VERIFY(nvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
3441 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
3442 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3443 spa_load_l2cache(spa
);
3444 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3445 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
3449 * Check for any removed devices.
3451 if (spa
->spa_autoreplace
) {
3452 spa_aux_check_removed(&spa
->spa_spares
);
3453 spa_aux_check_removed(&spa
->spa_l2cache
);
3456 if (spa_writeable(spa
)) {
3458 * Update the config cache to include the newly-imported pool.
3460 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
3464 * It's possible that the pool was expanded while it was exported.
3465 * We kick off an async task to handle this for us.
3467 spa_async_request(spa
, SPA_ASYNC_AUTOEXPAND
);
3469 mutex_exit(&spa_namespace_lock
);
3470 spa_history_log_version(spa
, LOG_POOL_IMPORT
);
3476 spa_tryimport(nvlist_t
*tryconfig
)
3478 nvlist_t
*config
= NULL
;
3484 if (nvlist_lookup_string(tryconfig
, ZPOOL_CONFIG_POOL_NAME
, &poolname
))
3487 if (nvlist_lookup_uint64(tryconfig
, ZPOOL_CONFIG_POOL_STATE
, &state
))
3491 * Create and initialize the spa structure.
3493 mutex_enter(&spa_namespace_lock
);
3494 spa
= spa_add(TRYIMPORT_NAME
, tryconfig
, NULL
);
3495 spa_activate(spa
, FREAD
);
3498 * Pass off the heavy lifting to spa_load().
3499 * Pass TRUE for mosconfig because the user-supplied config
3500 * is actually the one to trust when doing an import.
3502 error
= spa_load(spa
, SPA_LOAD_TRYIMPORT
, SPA_IMPORT_EXISTING
, B_TRUE
);
3505 * If 'tryconfig' was at least parsable, return the current config.
3507 if (spa
->spa_root_vdev
!= NULL
) {
3508 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
3509 VERIFY(nvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
,
3511 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
3513 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_TIMESTAMP
,
3514 spa
->spa_uberblock
.ub_timestamp
) == 0);
3517 * If the bootfs property exists on this pool then we
3518 * copy it out so that external consumers can tell which
3519 * pools are bootable.
3521 if ((!error
|| error
== EEXIST
) && spa
->spa_bootfs
) {
3522 char *tmpname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
3525 * We have to play games with the name since the
3526 * pool was opened as TRYIMPORT_NAME.
3528 if (dsl_dsobj_to_dsname(spa_name(spa
),
3529 spa
->spa_bootfs
, tmpname
) == 0) {
3531 char *dsname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
3533 cp
= strchr(tmpname
, '/');
3535 (void) strlcpy(dsname
, tmpname
,
3538 (void) snprintf(dsname
, MAXPATHLEN
,
3539 "%s/%s", poolname
, ++cp
);
3541 VERIFY(nvlist_add_string(config
,
3542 ZPOOL_CONFIG_BOOTFS
, dsname
) == 0);
3543 kmem_free(dsname
, MAXPATHLEN
);
3545 kmem_free(tmpname
, MAXPATHLEN
);
3549 * Add the list of hot spares and level 2 cache devices.
3551 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
3552 spa_add_spares(spa
, config
);
3553 spa_add_l2cache(spa
, config
);
3554 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
3558 spa_deactivate(spa
);
3560 mutex_exit(&spa_namespace_lock
);
3566 * Pool export/destroy
3568 * The act of destroying or exporting a pool is very simple. We make sure there
3569 * is no more pending I/O and any references to the pool are gone. Then, we
3570 * update the pool state and sync all the labels to disk, removing the
3571 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
3572 * we don't sync the labels or remove the configuration cache.
3575 spa_export_common(char *pool
, int new_state
, nvlist_t
**oldconfig
,
3576 boolean_t force
, boolean_t hardforce
)
3583 if (!(spa_mode_global
& FWRITE
))
3586 mutex_enter(&spa_namespace_lock
);
3587 if ((spa
= spa_lookup(pool
)) == NULL
) {
3588 mutex_exit(&spa_namespace_lock
);
3593 * Put a hold on the pool, drop the namespace lock, stop async tasks,
3594 * reacquire the namespace lock, and see if we can export.
3596 spa_open_ref(spa
, FTAG
);
3597 mutex_exit(&spa_namespace_lock
);
3598 spa_async_suspend(spa
);
3599 mutex_enter(&spa_namespace_lock
);
3600 spa_close(spa
, FTAG
);
3603 * The pool will be in core if it's openable,
3604 * in which case we can modify its state.
3606 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
&& spa
->spa_sync_on
) {
3608 * Objsets may be open only because they're dirty, so we
3609 * have to force it to sync before checking spa_refcnt.
3611 txg_wait_synced(spa
->spa_dsl_pool
, 0);
3614 * A pool cannot be exported or destroyed if there are active
3615 * references. If we are resetting a pool, allow references by
3616 * fault injection handlers.
3618 if (!spa_refcount_zero(spa
) ||
3619 (spa
->spa_inject_ref
!= 0 &&
3620 new_state
!= POOL_STATE_UNINITIALIZED
)) {
3621 spa_async_resume(spa
);
3622 mutex_exit(&spa_namespace_lock
);
3627 * A pool cannot be exported if it has an active shared spare.
3628 * This is to prevent other pools stealing the active spare
3629 * from an exported pool. At user's own will, such pool can
3630 * be forcedly exported.
3632 if (!force
&& new_state
== POOL_STATE_EXPORTED
&&
3633 spa_has_active_shared_spare(spa
)) {
3634 spa_async_resume(spa
);
3635 mutex_exit(&spa_namespace_lock
);
3640 * We want this to be reflected on every label,
3641 * so mark them all dirty. spa_unload() will do the
3642 * final sync that pushes these changes out.
3644 if (new_state
!= POOL_STATE_UNINITIALIZED
&& !hardforce
) {
3645 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3646 spa
->spa_state
= new_state
;
3647 spa
->spa_final_txg
= spa_last_synced_txg(spa
) +
3649 vdev_config_dirty(spa
->spa_root_vdev
);
3650 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3654 spa_event_notify(spa
, NULL
, FM_EREPORT_ZFS_POOL_DESTROY
);
3656 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
3658 spa_deactivate(spa
);
3661 if (oldconfig
&& spa
->spa_config
)
3662 VERIFY(nvlist_dup(spa
->spa_config
, oldconfig
, 0) == 0);
3664 if (new_state
!= POOL_STATE_UNINITIALIZED
) {
3666 spa_config_sync(spa
, B_TRUE
, B_TRUE
);
3669 mutex_exit(&spa_namespace_lock
);
3675 * Destroy a storage pool.
3678 spa_destroy(char *pool
)
3680 return (spa_export_common(pool
, POOL_STATE_DESTROYED
, NULL
,
3685 * Export a storage pool.
3688 spa_export(char *pool
, nvlist_t
**oldconfig
, boolean_t force
,
3689 boolean_t hardforce
)
3691 return (spa_export_common(pool
, POOL_STATE_EXPORTED
, oldconfig
,
3696 * Similar to spa_export(), this unloads the spa_t without actually removing it
3697 * from the namespace in any way.
3700 spa_reset(char *pool
)
3702 return (spa_export_common(pool
, POOL_STATE_UNINITIALIZED
, NULL
,
3707 * ==========================================================================
3708 * Device manipulation
3709 * ==========================================================================
3713 * Add a device to a storage pool.
3716 spa_vdev_add(spa_t
*spa
, nvlist_t
*nvroot
)
3720 vdev_t
*rvd
= spa
->spa_root_vdev
;
3722 nvlist_t
**spares
, **l2cache
;
3723 uint_t nspares
, nl2cache
;
3726 ASSERT(spa_writeable(spa
));
3728 txg
= spa_vdev_enter(spa
);
3730 if ((error
= spa_config_parse(spa
, &vd
, nvroot
, NULL
, 0,
3731 VDEV_ALLOC_ADD
)) != 0)
3732 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
3734 spa
->spa_pending_vdev
= vd
; /* spa_vdev_exit() will clear this */
3736 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
, &spares
,
3740 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
, &l2cache
,
3744 if (vd
->vdev_children
== 0 && nspares
== 0 && nl2cache
== 0)
3745 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
3747 if (vd
->vdev_children
!= 0 &&
3748 (error
= vdev_create(vd
, txg
, B_FALSE
)) != 0)
3749 return (spa_vdev_exit(spa
, vd
, txg
, error
));
3752 * We must validate the spares and l2cache devices after checking the
3753 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
3755 if ((error
= spa_validate_aux(spa
, nvroot
, txg
, VDEV_ALLOC_ADD
)) != 0)
3756 return (spa_vdev_exit(spa
, vd
, txg
, error
));
3759 * Transfer each new top-level vdev from vd to rvd.
3761 for (c
= 0; c
< vd
->vdev_children
; c
++) {
3764 * Set the vdev id to the first hole, if one exists.
3766 for (id
= 0; id
< rvd
->vdev_children
; id
++) {
3767 if (rvd
->vdev_child
[id
]->vdev_ishole
) {
3768 vdev_free(rvd
->vdev_child
[id
]);
3772 tvd
= vd
->vdev_child
[c
];
3773 vdev_remove_child(vd
, tvd
);
3775 vdev_add_child(rvd
, tvd
);
3776 vdev_config_dirty(tvd
);
3780 spa_set_aux_vdevs(&spa
->spa_spares
, spares
, nspares
,
3781 ZPOOL_CONFIG_SPARES
);
3782 spa_load_spares(spa
);
3783 spa
->spa_spares
.sav_sync
= B_TRUE
;
3786 if (nl2cache
!= 0) {
3787 spa_set_aux_vdevs(&spa
->spa_l2cache
, l2cache
, nl2cache
,
3788 ZPOOL_CONFIG_L2CACHE
);
3789 spa_load_l2cache(spa
);
3790 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
3794 * We have to be careful when adding new vdevs to an existing pool.
3795 * If other threads start allocating from these vdevs before we
3796 * sync the config cache, and we lose power, then upon reboot we may
3797 * fail to open the pool because there are DVAs that the config cache
3798 * can't translate. Therefore, we first add the vdevs without
3799 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
3800 * and then let spa_config_update() initialize the new metaslabs.
3802 * spa_load() checks for added-but-not-initialized vdevs, so that
3803 * if we lose power at any point in this sequence, the remaining
3804 * steps will be completed the next time we load the pool.
3806 (void) spa_vdev_exit(spa
, vd
, txg
, 0);
3808 mutex_enter(&spa_namespace_lock
);
3809 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
3810 mutex_exit(&spa_namespace_lock
);
3816 * Attach a device to a mirror. The arguments are the path to any device
3817 * in the mirror, and the nvroot for the new device. If the path specifies
3818 * a device that is not mirrored, we automatically insert the mirror vdev.
3820 * If 'replacing' is specified, the new device is intended to replace the
3821 * existing device; in this case the two devices are made into their own
3822 * mirror using the 'replacing' vdev, which is functionally identical to
3823 * the mirror vdev (it actually reuses all the same ops) but has a few
3824 * extra rules: you can't attach to it after it's been created, and upon
3825 * completion of resilvering, the first disk (the one being replaced)
3826 * is automatically detached.
3829 spa_vdev_attach(spa_t
*spa
, uint64_t guid
, nvlist_t
*nvroot
, int replacing
)
3831 uint64_t txg
, dtl_max_txg
;
3832 ASSERTV(vdev_t
*rvd
= spa
->spa_root_vdev
;)
3833 vdev_t
*oldvd
, *newvd
, *newrootvd
, *pvd
, *tvd
;
3835 char *oldvdpath
, *newvdpath
;
3839 ASSERT(spa_writeable(spa
));
3841 txg
= spa_vdev_enter(spa
);
3843 oldvd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
3846 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
3848 if (!oldvd
->vdev_ops
->vdev_op_leaf
)
3849 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
3851 pvd
= oldvd
->vdev_parent
;
3853 if ((error
= spa_config_parse(spa
, &newrootvd
, nvroot
, NULL
, 0,
3854 VDEV_ALLOC_ADD
)) != 0)
3855 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
3857 if (newrootvd
->vdev_children
!= 1)
3858 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
3860 newvd
= newrootvd
->vdev_child
[0];
3862 if (!newvd
->vdev_ops
->vdev_op_leaf
)
3863 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
3865 if ((error
= vdev_create(newrootvd
, txg
, replacing
)) != 0)
3866 return (spa_vdev_exit(spa
, newrootvd
, txg
, error
));
3869 * Spares can't replace logs
3871 if (oldvd
->vdev_top
->vdev_islog
&& newvd
->vdev_isspare
)
3872 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
3876 * For attach, the only allowable parent is a mirror or the root
3879 if (pvd
->vdev_ops
!= &vdev_mirror_ops
&&
3880 pvd
->vdev_ops
!= &vdev_root_ops
)
3881 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
3883 pvops
= &vdev_mirror_ops
;
3886 * Active hot spares can only be replaced by inactive hot
3889 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
3890 oldvd
->vdev_isspare
&&
3891 !spa_has_spare(spa
, newvd
->vdev_guid
))
3892 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
3895 * If the source is a hot spare, and the parent isn't already a
3896 * spare, then we want to create a new hot spare. Otherwise, we
3897 * want to create a replacing vdev. The user is not allowed to
3898 * attach to a spared vdev child unless the 'isspare' state is
3899 * the same (spare replaces spare, non-spare replaces
3902 if (pvd
->vdev_ops
== &vdev_replacing_ops
&&
3903 spa_version(spa
) < SPA_VERSION_MULTI_REPLACE
) {
3904 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
3905 } else if (pvd
->vdev_ops
== &vdev_spare_ops
&&
3906 newvd
->vdev_isspare
!= oldvd
->vdev_isspare
) {
3907 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
3910 if (newvd
->vdev_isspare
)
3911 pvops
= &vdev_spare_ops
;
3913 pvops
= &vdev_replacing_ops
;
3917 * Make sure the new device is big enough.
3919 if (newvd
->vdev_asize
< vdev_get_min_asize(oldvd
))
3920 return (spa_vdev_exit(spa
, newrootvd
, txg
, EOVERFLOW
));
3923 * The new device cannot have a higher alignment requirement
3924 * than the top-level vdev.
3926 if (newvd
->vdev_ashift
> oldvd
->vdev_top
->vdev_ashift
)
3927 return (spa_vdev_exit(spa
, newrootvd
, txg
, EDOM
));
3930 * If this is an in-place replacement, update oldvd's path and devid
3931 * to make it distinguishable from newvd, and unopenable from now on.
3933 if (strcmp(oldvd
->vdev_path
, newvd
->vdev_path
) == 0) {
3934 spa_strfree(oldvd
->vdev_path
);
3935 oldvd
->vdev_path
= kmem_alloc(strlen(newvd
->vdev_path
) + 5,
3937 (void) sprintf(oldvd
->vdev_path
, "%s/%s",
3938 newvd
->vdev_path
, "old");
3939 if (oldvd
->vdev_devid
!= NULL
) {
3940 spa_strfree(oldvd
->vdev_devid
);
3941 oldvd
->vdev_devid
= NULL
;
3945 /* mark the device being resilvered */
3946 newvd
->vdev_resilvering
= B_TRUE
;
3949 * If the parent is not a mirror, or if we're replacing, insert the new
3950 * mirror/replacing/spare vdev above oldvd.
3952 if (pvd
->vdev_ops
!= pvops
)
3953 pvd
= vdev_add_parent(oldvd
, pvops
);
3955 ASSERT(pvd
->vdev_top
->vdev_parent
== rvd
);
3956 ASSERT(pvd
->vdev_ops
== pvops
);
3957 ASSERT(oldvd
->vdev_parent
== pvd
);
3960 * Extract the new device from its root and add it to pvd.
3962 vdev_remove_child(newrootvd
, newvd
);
3963 newvd
->vdev_id
= pvd
->vdev_children
;
3964 newvd
->vdev_crtxg
= oldvd
->vdev_crtxg
;
3965 vdev_add_child(pvd
, newvd
);
3967 tvd
= newvd
->vdev_top
;
3968 ASSERT(pvd
->vdev_top
== tvd
);
3969 ASSERT(tvd
->vdev_parent
== rvd
);
3971 vdev_config_dirty(tvd
);
3974 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
3975 * for any dmu_sync-ed blocks. It will propagate upward when
3976 * spa_vdev_exit() calls vdev_dtl_reassess().
3978 dtl_max_txg
= txg
+ TXG_CONCURRENT_STATES
;
3980 vdev_dtl_dirty(newvd
, DTL_MISSING
, TXG_INITIAL
,
3981 dtl_max_txg
- TXG_INITIAL
);
3983 if (newvd
->vdev_isspare
) {
3984 spa_spare_activate(newvd
);
3985 spa_event_notify(spa
, newvd
, FM_EREPORT_ZFS_DEVICE_SPARE
);
3988 oldvdpath
= spa_strdup(oldvd
->vdev_path
);
3989 newvdpath
= spa_strdup(newvd
->vdev_path
);
3990 newvd_isspare
= newvd
->vdev_isspare
;
3993 * Mark newvd's DTL dirty in this txg.
3995 vdev_dirty(tvd
, VDD_DTL
, newvd
, txg
);
3998 * Restart the resilver
4000 dsl_resilver_restart(spa
->spa_dsl_pool
, dtl_max_txg
);
4005 (void) spa_vdev_exit(spa
, newrootvd
, dtl_max_txg
, 0);
4007 spa_history_log_internal(LOG_POOL_VDEV_ATTACH
, spa
, NULL
,
4008 "%s vdev=%s %s vdev=%s",
4009 replacing
&& newvd_isspare
? "spare in" :
4010 replacing
? "replace" : "attach", newvdpath
,
4011 replacing
? "for" : "to", oldvdpath
);
4013 spa_strfree(oldvdpath
);
4014 spa_strfree(newvdpath
);
4016 if (spa
->spa_bootfs
)
4017 spa_event_notify(spa
, newvd
, FM_EREPORT_ZFS_BOOTFS_VDEV_ATTACH
);
4023 * Detach a device from a mirror or replacing vdev.
4024 * If 'replace_done' is specified, only detach if the parent
4025 * is a replacing vdev.
4028 spa_vdev_detach(spa_t
*spa
, uint64_t guid
, uint64_t pguid
, int replace_done
)
4032 ASSERTV(vdev_t
*rvd
= spa
->spa_root_vdev
;)
4033 vdev_t
*vd
, *pvd
, *cvd
, *tvd
;
4034 boolean_t unspare
= B_FALSE
;
4035 uint64_t unspare_guid
= 0;
4039 ASSERT(spa_writeable(spa
));
4041 txg
= spa_vdev_enter(spa
);
4043 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
4046 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
4048 if (!vd
->vdev_ops
->vdev_op_leaf
)
4049 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4051 pvd
= vd
->vdev_parent
;
4054 * If the parent/child relationship is not as expected, don't do it.
4055 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
4056 * vdev that's replacing B with C. The user's intent in replacing
4057 * is to go from M(A,B) to M(A,C). If the user decides to cancel
4058 * the replace by detaching C, the expected behavior is to end up
4059 * M(A,B). But suppose that right after deciding to detach C,
4060 * the replacement of B completes. We would have M(A,C), and then
4061 * ask to detach C, which would leave us with just A -- not what
4062 * the user wanted. To prevent this, we make sure that the
4063 * parent/child relationship hasn't changed -- in this example,
4064 * that C's parent is still the replacing vdev R.
4066 if (pvd
->vdev_guid
!= pguid
&& pguid
!= 0)
4067 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
4070 * Only 'replacing' or 'spare' vdevs can be replaced.
4072 if (replace_done
&& pvd
->vdev_ops
!= &vdev_replacing_ops
&&
4073 pvd
->vdev_ops
!= &vdev_spare_ops
)
4074 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4076 ASSERT(pvd
->vdev_ops
!= &vdev_spare_ops
||
4077 spa_version(spa
) >= SPA_VERSION_SPARES
);
4080 * Only mirror, replacing, and spare vdevs support detach.
4082 if (pvd
->vdev_ops
!= &vdev_replacing_ops
&&
4083 pvd
->vdev_ops
!= &vdev_mirror_ops
&&
4084 pvd
->vdev_ops
!= &vdev_spare_ops
)
4085 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4088 * If this device has the only valid copy of some data,
4089 * we cannot safely detach it.
4091 if (vdev_dtl_required(vd
))
4092 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
4094 ASSERT(pvd
->vdev_children
>= 2);
4097 * If we are detaching the second disk from a replacing vdev, then
4098 * check to see if we changed the original vdev's path to have "/old"
4099 * at the end in spa_vdev_attach(). If so, undo that change now.
4101 if (pvd
->vdev_ops
== &vdev_replacing_ops
&& vd
->vdev_id
> 0 &&
4102 vd
->vdev_path
!= NULL
) {
4103 size_t len
= strlen(vd
->vdev_path
);
4105 for (c
= 0; c
< pvd
->vdev_children
; c
++) {
4106 cvd
= pvd
->vdev_child
[c
];
4108 if (cvd
== vd
|| cvd
->vdev_path
== NULL
)
4111 if (strncmp(cvd
->vdev_path
, vd
->vdev_path
, len
) == 0 &&
4112 strcmp(cvd
->vdev_path
+ len
, "/old") == 0) {
4113 spa_strfree(cvd
->vdev_path
);
4114 cvd
->vdev_path
= spa_strdup(vd
->vdev_path
);
4121 * If we are detaching the original disk from a spare, then it implies
4122 * that the spare should become a real disk, and be removed from the
4123 * active spare list for the pool.
4125 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
4127 pvd
->vdev_child
[pvd
->vdev_children
- 1]->vdev_isspare
)
4131 * Erase the disk labels so the disk can be used for other things.
4132 * This must be done after all other error cases are handled,
4133 * but before we disembowel vd (so we can still do I/O to it).
4134 * But if we can't do it, don't treat the error as fatal --
4135 * it may be that the unwritability of the disk is the reason
4136 * it's being detached!
4138 error
= vdev_label_init(vd
, 0, VDEV_LABEL_REMOVE
);
4141 * Remove vd from its parent and compact the parent's children.
4143 vdev_remove_child(pvd
, vd
);
4144 vdev_compact_children(pvd
);
4147 * Remember one of the remaining children so we can get tvd below.
4149 cvd
= pvd
->vdev_child
[pvd
->vdev_children
- 1];
4152 * If we need to remove the remaining child from the list of hot spares,
4153 * do it now, marking the vdev as no longer a spare in the process.
4154 * We must do this before vdev_remove_parent(), because that can
4155 * change the GUID if it creates a new toplevel GUID. For a similar
4156 * reason, we must remove the spare now, in the same txg as the detach;
4157 * otherwise someone could attach a new sibling, change the GUID, and
4158 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
4161 ASSERT(cvd
->vdev_isspare
);
4162 spa_spare_remove(cvd
);
4163 unspare_guid
= cvd
->vdev_guid
;
4164 (void) spa_vdev_remove(spa
, unspare_guid
, B_TRUE
);
4165 cvd
->vdev_unspare
= B_TRUE
;
4169 * If the parent mirror/replacing vdev only has one child,
4170 * the parent is no longer needed. Remove it from the tree.
4172 if (pvd
->vdev_children
== 1) {
4173 if (pvd
->vdev_ops
== &vdev_spare_ops
)
4174 cvd
->vdev_unspare
= B_FALSE
;
4175 vdev_remove_parent(cvd
);
4176 cvd
->vdev_resilvering
= B_FALSE
;
4181 * We don't set tvd until now because the parent we just removed
4182 * may have been the previous top-level vdev.
4184 tvd
= cvd
->vdev_top
;
4185 ASSERT(tvd
->vdev_parent
== rvd
);
4188 * Reevaluate the parent vdev state.
4190 vdev_propagate_state(cvd
);
4193 * If the 'autoexpand' property is set on the pool then automatically
4194 * try to expand the size of the pool. For example if the device we
4195 * just detached was smaller than the others, it may be possible to
4196 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
4197 * first so that we can obtain the updated sizes of the leaf vdevs.
4199 if (spa
->spa_autoexpand
) {
4201 vdev_expand(tvd
, txg
);
4204 vdev_config_dirty(tvd
);
4207 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
4208 * vd->vdev_detached is set and free vd's DTL object in syncing context.
4209 * But first make sure we're not on any *other* txg's DTL list, to
4210 * prevent vd from being accessed after it's freed.
4212 vdpath
= spa_strdup(vd
->vdev_path
);
4213 for (t
= 0; t
< TXG_SIZE
; t
++)
4214 (void) txg_list_remove_this(&tvd
->vdev_dtl_list
, vd
, t
);
4215 vd
->vdev_detached
= B_TRUE
;
4216 vdev_dirty(tvd
, VDD_DTL
, vd
, txg
);
4218 spa_event_notify(spa
, vd
, FM_EREPORT_ZFS_DEVICE_REMOVE
);
4220 /* hang on to the spa before we release the lock */
4221 spa_open_ref(spa
, FTAG
);
4223 error
= spa_vdev_exit(spa
, vd
, txg
, 0);
4225 spa_history_log_internal(LOG_POOL_VDEV_DETACH
, spa
, NULL
,
4227 spa_strfree(vdpath
);
4230 * If this was the removal of the original device in a hot spare vdev,
4231 * then we want to go through and remove the device from the hot spare
4232 * list of every other pool.
4235 spa_t
*altspa
= NULL
;
4237 mutex_enter(&spa_namespace_lock
);
4238 while ((altspa
= spa_next(altspa
)) != NULL
) {
4239 if (altspa
->spa_state
!= POOL_STATE_ACTIVE
||
4243 spa_open_ref(altspa
, FTAG
);
4244 mutex_exit(&spa_namespace_lock
);
4245 (void) spa_vdev_remove(altspa
, unspare_guid
, B_TRUE
);
4246 mutex_enter(&spa_namespace_lock
);
4247 spa_close(altspa
, FTAG
);
4249 mutex_exit(&spa_namespace_lock
);
4251 /* search the rest of the vdevs for spares to remove */
4252 spa_vdev_resilver_done(spa
);
4255 /* all done with the spa; OK to release */
4256 mutex_enter(&spa_namespace_lock
);
4257 spa_close(spa
, FTAG
);
4258 mutex_exit(&spa_namespace_lock
);
4264 * Split a set of devices from their mirrors, and create a new pool from them.
4267 spa_vdev_split_mirror(spa_t
*spa
, char *newname
, nvlist_t
*config
,
4268 nvlist_t
*props
, boolean_t exp
)
4271 uint64_t txg
, *glist
;
4273 uint_t c
, children
, lastlog
;
4274 nvlist_t
**child
, *nvl
, *tmp
;
4276 char *altroot
= NULL
;
4277 vdev_t
*rvd
, **vml
= NULL
; /* vdev modify list */
4278 boolean_t activate_slog
;
4280 ASSERT(spa_writeable(spa
));
4282 txg
= spa_vdev_enter(spa
);
4284 /* clear the log and flush everything up to now */
4285 activate_slog
= spa_passivate_log(spa
);
4286 (void) spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
4287 error
= spa_offline_log(spa
);
4288 txg
= spa_vdev_config_enter(spa
);
4291 spa_activate_log(spa
);
4294 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
4296 /* check new spa name before going any further */
4297 if (spa_lookup(newname
) != NULL
)
4298 return (spa_vdev_exit(spa
, NULL
, txg
, EEXIST
));
4301 * scan through all the children to ensure they're all mirrors
4303 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvl
) != 0 ||
4304 nvlist_lookup_nvlist_array(nvl
, ZPOOL_CONFIG_CHILDREN
, &child
,
4306 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
4308 /* first, check to ensure we've got the right child count */
4309 rvd
= spa
->spa_root_vdev
;
4311 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
4312 vdev_t
*vd
= rvd
->vdev_child
[c
];
4314 /* don't count the holes & logs as children */
4315 if (vd
->vdev_islog
|| vd
->vdev_ishole
) {
4323 if (children
!= (lastlog
!= 0 ? lastlog
: rvd
->vdev_children
))
4324 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
4326 /* next, ensure no spare or cache devices are part of the split */
4327 if (nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_SPARES
, &tmp
) == 0 ||
4328 nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_L2CACHE
, &tmp
) == 0)
4329 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
4331 vml
= kmem_zalloc(children
* sizeof (vdev_t
*), KM_SLEEP
);
4332 glist
= kmem_zalloc(children
* sizeof (uint64_t), KM_SLEEP
);
4334 /* then, loop over each vdev and validate it */
4335 for (c
= 0; c
< children
; c
++) {
4336 uint64_t is_hole
= 0;
4338 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
4342 if (spa
->spa_root_vdev
->vdev_child
[c
]->vdev_ishole
||
4343 spa
->spa_root_vdev
->vdev_child
[c
]->vdev_islog
) {
4351 /* which disk is going to be split? */
4352 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_GUID
,
4358 /* look it up in the spa */
4359 vml
[c
] = spa_lookup_by_guid(spa
, glist
[c
], B_FALSE
);
4360 if (vml
[c
] == NULL
) {
4365 /* make sure there's nothing stopping the split */
4366 if (vml
[c
]->vdev_parent
->vdev_ops
!= &vdev_mirror_ops
||
4367 vml
[c
]->vdev_islog
||
4368 vml
[c
]->vdev_ishole
||
4369 vml
[c
]->vdev_isspare
||
4370 vml
[c
]->vdev_isl2cache
||
4371 !vdev_writeable(vml
[c
]) ||
4372 vml
[c
]->vdev_children
!= 0 ||
4373 vml
[c
]->vdev_state
!= VDEV_STATE_HEALTHY
||
4374 c
!= spa
->spa_root_vdev
->vdev_child
[c
]->vdev_id
) {
4379 if (vdev_dtl_required(vml
[c
])) {
4384 /* we need certain info from the top level */
4385 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_ARRAY
,
4386 vml
[c
]->vdev_top
->vdev_ms_array
) == 0);
4387 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_SHIFT
,
4388 vml
[c
]->vdev_top
->vdev_ms_shift
) == 0);
4389 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASIZE
,
4390 vml
[c
]->vdev_top
->vdev_asize
) == 0);
4391 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASHIFT
,
4392 vml
[c
]->vdev_top
->vdev_ashift
) == 0);
4396 kmem_free(vml
, children
* sizeof (vdev_t
*));
4397 kmem_free(glist
, children
* sizeof (uint64_t));
4398 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
4401 /* stop writers from using the disks */
4402 for (c
= 0; c
< children
; c
++) {
4404 vml
[c
]->vdev_offline
= B_TRUE
;
4406 vdev_reopen(spa
->spa_root_vdev
);
4409 * Temporarily record the splitting vdevs in the spa config. This
4410 * will disappear once the config is regenerated.
4412 VERIFY(nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
4413 VERIFY(nvlist_add_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
4414 glist
, children
) == 0);
4415 kmem_free(glist
, children
* sizeof (uint64_t));
4417 mutex_enter(&spa
->spa_props_lock
);
4418 VERIFY(nvlist_add_nvlist(spa
->spa_config
, ZPOOL_CONFIG_SPLIT
,
4420 mutex_exit(&spa
->spa_props_lock
);
4421 spa
->spa_config_splitting
= nvl
;
4422 vdev_config_dirty(spa
->spa_root_vdev
);
4424 /* configure and create the new pool */
4425 VERIFY(nvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
, newname
) == 0);
4426 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
4427 exp
? POOL_STATE_EXPORTED
: POOL_STATE_ACTIVE
) == 0);
4428 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
4429 spa_version(spa
)) == 0);
4430 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
4431 spa
->spa_config_txg
) == 0);
4432 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
4433 spa_generate_guid(NULL
)) == 0);
4434 (void) nvlist_lookup_string(props
,
4435 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
4437 /* add the new pool to the namespace */
4438 newspa
= spa_add(newname
, config
, altroot
);
4439 newspa
->spa_config_txg
= spa
->spa_config_txg
;
4440 spa_set_log_state(newspa
, SPA_LOG_CLEAR
);
4442 /* release the spa config lock, retaining the namespace lock */
4443 spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
4445 if (zio_injection_enabled
)
4446 zio_handle_panic_injection(spa
, FTAG
, 1);
4448 spa_activate(newspa
, spa_mode_global
);
4449 spa_async_suspend(newspa
);
4451 /* create the new pool from the disks of the original pool */
4452 error
= spa_load(newspa
, SPA_LOAD_IMPORT
, SPA_IMPORT_ASSEMBLE
, B_TRUE
);
4456 /* if that worked, generate a real config for the new pool */
4457 if (newspa
->spa_root_vdev
!= NULL
) {
4458 VERIFY(nvlist_alloc(&newspa
->spa_config_splitting
,
4459 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
4460 VERIFY(nvlist_add_uint64(newspa
->spa_config_splitting
,
4461 ZPOOL_CONFIG_SPLIT_GUID
, spa_guid(spa
)) == 0);
4462 spa_config_set(newspa
, spa_config_generate(newspa
, NULL
, -1ULL,
4467 if (props
!= NULL
) {
4468 spa_configfile_set(newspa
, props
, B_FALSE
);
4469 error
= spa_prop_set(newspa
, props
);
4474 /* flush everything */
4475 txg
= spa_vdev_config_enter(newspa
);
4476 vdev_config_dirty(newspa
->spa_root_vdev
);
4477 (void) spa_vdev_config_exit(newspa
, NULL
, txg
, 0, FTAG
);
4479 if (zio_injection_enabled
)
4480 zio_handle_panic_injection(spa
, FTAG
, 2);
4482 spa_async_resume(newspa
);
4484 /* finally, update the original pool's config */
4485 txg
= spa_vdev_config_enter(spa
);
4486 tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
4487 error
= dmu_tx_assign(tx
, TXG_WAIT
);
4490 for (c
= 0; c
< children
; c
++) {
4491 if (vml
[c
] != NULL
) {
4494 spa_history_log_internal(LOG_POOL_VDEV_DETACH
,
4500 vdev_config_dirty(spa
->spa_root_vdev
);
4501 spa
->spa_config_splitting
= NULL
;
4505 (void) spa_vdev_exit(spa
, NULL
, txg
, 0);
4507 if (zio_injection_enabled
)
4508 zio_handle_panic_injection(spa
, FTAG
, 3);
4510 /* split is complete; log a history record */
4511 spa_history_log_internal(LOG_POOL_SPLIT
, newspa
, NULL
,
4512 "split new pool %s from pool %s", newname
, spa_name(spa
));
4514 kmem_free(vml
, children
* sizeof (vdev_t
*));
4516 /* if we're not going to mount the filesystems in userland, export */
4518 error
= spa_export_common(newname
, POOL_STATE_EXPORTED
, NULL
,
4525 spa_deactivate(newspa
);
4528 txg
= spa_vdev_config_enter(spa
);
4530 /* re-online all offlined disks */
4531 for (c
= 0; c
< children
; c
++) {
4533 vml
[c
]->vdev_offline
= B_FALSE
;
4535 vdev_reopen(spa
->spa_root_vdev
);
4537 nvlist_free(spa
->spa_config_splitting
);
4538 spa
->spa_config_splitting
= NULL
;
4539 (void) spa_vdev_exit(spa
, NULL
, txg
, error
);
4541 kmem_free(vml
, children
* sizeof (vdev_t
*));
4546 spa_nvlist_lookup_by_guid(nvlist_t
**nvpp
, int count
, uint64_t target_guid
)
4550 for (i
= 0; i
< count
; i
++) {
4553 VERIFY(nvlist_lookup_uint64(nvpp
[i
], ZPOOL_CONFIG_GUID
,
4556 if (guid
== target_guid
)
4564 spa_vdev_remove_aux(nvlist_t
*config
, char *name
, nvlist_t
**dev
, int count
,
4565 nvlist_t
*dev_to_remove
)
4567 nvlist_t
**newdev
= NULL
;
4571 newdev
= kmem_alloc((count
- 1) * sizeof (void *), KM_SLEEP
);
4573 for (i
= 0, j
= 0; i
< count
; i
++) {
4574 if (dev
[i
] == dev_to_remove
)
4576 VERIFY(nvlist_dup(dev
[i
], &newdev
[j
++], KM_SLEEP
) == 0);
4579 VERIFY(nvlist_remove(config
, name
, DATA_TYPE_NVLIST_ARRAY
) == 0);
4580 VERIFY(nvlist_add_nvlist_array(config
, name
, newdev
, count
- 1) == 0);
4582 for (i
= 0; i
< count
- 1; i
++)
4583 nvlist_free(newdev
[i
]);
4586 kmem_free(newdev
, (count
- 1) * sizeof (void *));
4590 * Evacuate the device.
4593 spa_vdev_remove_evacuate(spa_t
*spa
, vdev_t
*vd
)
4598 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
4599 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
4600 ASSERT(vd
== vd
->vdev_top
);
4603 * Evacuate the device. We don't hold the config lock as writer
4604 * since we need to do I/O but we do keep the
4605 * spa_namespace_lock held. Once this completes the device
4606 * should no longer have any blocks allocated on it.
4608 if (vd
->vdev_islog
) {
4609 if (vd
->vdev_stat
.vs_alloc
!= 0)
4610 error
= spa_offline_log(spa
);
4619 * The evacuation succeeded. Remove any remaining MOS metadata
4620 * associated with this vdev, and wait for these changes to sync.
4622 ASSERT3U(vd
->vdev_stat
.vs_alloc
, ==, 0);
4623 txg
= spa_vdev_config_enter(spa
);
4624 vd
->vdev_removing
= B_TRUE
;
4625 vdev_dirty(vd
, 0, NULL
, txg
);
4626 vdev_config_dirty(vd
);
4627 spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
4633 * Complete the removal by cleaning up the namespace.
4636 spa_vdev_remove_from_namespace(spa_t
*spa
, vdev_t
*vd
)
4638 vdev_t
*rvd
= spa
->spa_root_vdev
;
4639 uint64_t id
= vd
->vdev_id
;
4640 boolean_t last_vdev
= (id
== (rvd
->vdev_children
- 1));
4642 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
4643 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
4644 ASSERT(vd
== vd
->vdev_top
);
4647 * Only remove any devices which are empty.
4649 if (vd
->vdev_stat
.vs_alloc
!= 0)
4652 (void) vdev_label_init(vd
, 0, VDEV_LABEL_REMOVE
);
4654 if (list_link_active(&vd
->vdev_state_dirty_node
))
4655 vdev_state_clean(vd
);
4656 if (list_link_active(&vd
->vdev_config_dirty_node
))
4657 vdev_config_clean(vd
);
4662 vdev_compact_children(rvd
);
4664 vd
= vdev_alloc_common(spa
, id
, 0, &vdev_hole_ops
);
4665 vdev_add_child(rvd
, vd
);
4667 vdev_config_dirty(rvd
);
4670 * Reassess the health of our root vdev.
4676 * Remove a device from the pool -
4678 * Removing a device from the vdev namespace requires several steps
4679 * and can take a significant amount of time. As a result we use
4680 * the spa_vdev_config_[enter/exit] functions which allow us to
4681 * grab and release the spa_config_lock while still holding the namespace
4682 * lock. During each step the configuration is synced out.
4686 * Remove a device from the pool. Currently, this supports removing only hot
4687 * spares, slogs, and level 2 ARC devices.
4690 spa_vdev_remove(spa_t
*spa
, uint64_t guid
, boolean_t unspare
)
4693 metaslab_group_t
*mg
;
4694 nvlist_t
**spares
, **l2cache
, *nv
;
4696 uint_t nspares
, nl2cache
;
4698 boolean_t locked
= MUTEX_HELD(&spa_namespace_lock
);
4700 ASSERT(spa_writeable(spa
));
4703 txg
= spa_vdev_enter(spa
);
4705 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
4707 if (spa
->spa_spares
.sav_vdevs
!= NULL
&&
4708 nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
4709 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0 &&
4710 (nv
= spa_nvlist_lookup_by_guid(spares
, nspares
, guid
)) != NULL
) {
4712 * Only remove the hot spare if it's not currently in use
4715 if (vd
== NULL
|| unspare
) {
4716 spa_vdev_remove_aux(spa
->spa_spares
.sav_config
,
4717 ZPOOL_CONFIG_SPARES
, spares
, nspares
, nv
);
4718 spa_load_spares(spa
);
4719 spa
->spa_spares
.sav_sync
= B_TRUE
;
4723 } else if (spa
->spa_l2cache
.sav_vdevs
!= NULL
&&
4724 nvlist_lookup_nvlist_array(spa
->spa_l2cache
.sav_config
,
4725 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0 &&
4726 (nv
= spa_nvlist_lookup_by_guid(l2cache
, nl2cache
, guid
)) != NULL
) {
4728 * Cache devices can always be removed.
4730 spa_vdev_remove_aux(spa
->spa_l2cache
.sav_config
,
4731 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
, nv
);
4732 spa_load_l2cache(spa
);
4733 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
4734 } else if (vd
!= NULL
&& vd
->vdev_islog
) {
4736 ASSERT(vd
== vd
->vdev_top
);
4739 * XXX - Once we have bp-rewrite this should
4740 * become the common case.
4746 * Stop allocating from this vdev.
4748 metaslab_group_passivate(mg
);
4751 * Wait for the youngest allocations and frees to sync,
4752 * and then wait for the deferral of those frees to finish.
4754 spa_vdev_config_exit(spa
, NULL
,
4755 txg
+ TXG_CONCURRENT_STATES
+ TXG_DEFER_SIZE
, 0, FTAG
);
4758 * Attempt to evacuate the vdev.
4760 error
= spa_vdev_remove_evacuate(spa
, vd
);
4762 txg
= spa_vdev_config_enter(spa
);
4765 * If we couldn't evacuate the vdev, unwind.
4768 metaslab_group_activate(mg
);
4769 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
4773 * Clean up the vdev namespace.
4775 spa_vdev_remove_from_namespace(spa
, vd
);
4777 } else if (vd
!= NULL
) {
4779 * Normal vdevs cannot be removed (yet).
4784 * There is no vdev of any kind with the specified guid.
4790 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
4796 * Find any device that's done replacing, or a vdev marked 'unspare' that's
4797 * current spared, so we can detach it.
4800 spa_vdev_resilver_done_hunt(vdev_t
*vd
)
4802 vdev_t
*newvd
, *oldvd
;
4805 for (c
= 0; c
< vd
->vdev_children
; c
++) {
4806 oldvd
= spa_vdev_resilver_done_hunt(vd
->vdev_child
[c
]);
4812 * Check for a completed replacement. We always consider the first
4813 * vdev in the list to be the oldest vdev, and the last one to be
4814 * the newest (see spa_vdev_attach() for how that works). In
4815 * the case where the newest vdev is faulted, we will not automatically
4816 * remove it after a resilver completes. This is OK as it will require
4817 * user intervention to determine which disk the admin wishes to keep.
4819 if (vd
->vdev_ops
== &vdev_replacing_ops
) {
4820 ASSERT(vd
->vdev_children
> 1);
4822 newvd
= vd
->vdev_child
[vd
->vdev_children
- 1];
4823 oldvd
= vd
->vdev_child
[0];
4825 if (vdev_dtl_empty(newvd
, DTL_MISSING
) &&
4826 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
4827 !vdev_dtl_required(oldvd
))
4832 * Check for a completed resilver with the 'unspare' flag set.
4834 if (vd
->vdev_ops
== &vdev_spare_ops
) {
4835 vdev_t
*first
= vd
->vdev_child
[0];
4836 vdev_t
*last
= vd
->vdev_child
[vd
->vdev_children
- 1];
4838 if (last
->vdev_unspare
) {
4841 } else if (first
->vdev_unspare
) {
4848 if (oldvd
!= NULL
&&
4849 vdev_dtl_empty(newvd
, DTL_MISSING
) &&
4850 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
4851 !vdev_dtl_required(oldvd
))
4855 * If there are more than two spares attached to a disk,
4856 * and those spares are not required, then we want to
4857 * attempt to free them up now so that they can be used
4858 * by other pools. Once we're back down to a single
4859 * disk+spare, we stop removing them.
4861 if (vd
->vdev_children
> 2) {
4862 newvd
= vd
->vdev_child
[1];
4864 if (newvd
->vdev_isspare
&& last
->vdev_isspare
&&
4865 vdev_dtl_empty(last
, DTL_MISSING
) &&
4866 vdev_dtl_empty(last
, DTL_OUTAGE
) &&
4867 !vdev_dtl_required(newvd
))
4876 spa_vdev_resilver_done(spa_t
*spa
)
4878 vdev_t
*vd
, *pvd
, *ppvd
;
4879 uint64_t guid
, sguid
, pguid
, ppguid
;
4881 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4883 while ((vd
= spa_vdev_resilver_done_hunt(spa
->spa_root_vdev
)) != NULL
) {
4884 pvd
= vd
->vdev_parent
;
4885 ppvd
= pvd
->vdev_parent
;
4886 guid
= vd
->vdev_guid
;
4887 pguid
= pvd
->vdev_guid
;
4888 ppguid
= ppvd
->vdev_guid
;
4891 * If we have just finished replacing a hot spared device, then
4892 * we need to detach the parent's first child (the original hot
4895 if (ppvd
->vdev_ops
== &vdev_spare_ops
&& pvd
->vdev_id
== 0 &&
4896 ppvd
->vdev_children
== 2) {
4897 ASSERT(pvd
->vdev_ops
== &vdev_replacing_ops
);
4898 sguid
= ppvd
->vdev_child
[1]->vdev_guid
;
4900 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4901 if (spa_vdev_detach(spa
, guid
, pguid
, B_TRUE
) != 0)
4903 if (sguid
&& spa_vdev_detach(spa
, sguid
, ppguid
, B_TRUE
) != 0)
4905 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4908 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4912 * Update the stored path or FRU for this vdev.
4915 spa_vdev_set_common(spa_t
*spa
, uint64_t guid
, const char *value
,
4919 boolean_t sync
= B_FALSE
;
4921 ASSERT(spa_writeable(spa
));
4923 spa_vdev_state_enter(spa
, SCL_ALL
);
4925 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
4926 return (spa_vdev_state_exit(spa
, NULL
, ENOENT
));
4928 if (!vd
->vdev_ops
->vdev_op_leaf
)
4929 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
4932 if (strcmp(value
, vd
->vdev_path
) != 0) {
4933 spa_strfree(vd
->vdev_path
);
4934 vd
->vdev_path
= spa_strdup(value
);
4938 if (vd
->vdev_fru
== NULL
) {
4939 vd
->vdev_fru
= spa_strdup(value
);
4941 } else if (strcmp(value
, vd
->vdev_fru
) != 0) {
4942 spa_strfree(vd
->vdev_fru
);
4943 vd
->vdev_fru
= spa_strdup(value
);
4948 return (spa_vdev_state_exit(spa
, sync
? vd
: NULL
, 0));
4952 spa_vdev_setpath(spa_t
*spa
, uint64_t guid
, const char *newpath
)
4954 return (spa_vdev_set_common(spa
, guid
, newpath
, B_TRUE
));
4958 spa_vdev_setfru(spa_t
*spa
, uint64_t guid
, const char *newfru
)
4960 return (spa_vdev_set_common(spa
, guid
, newfru
, B_FALSE
));
4964 * ==========================================================================
4966 * ==========================================================================
4970 spa_scan_stop(spa_t
*spa
)
4972 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
4973 if (dsl_scan_resilvering(spa
->spa_dsl_pool
))
4975 return (dsl_scan_cancel(spa
->spa_dsl_pool
));
4979 spa_scan(spa_t
*spa
, pool_scan_func_t func
)
4981 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
4983 if (func
>= POOL_SCAN_FUNCS
|| func
== POOL_SCAN_NONE
)
4987 * If a resilver was requested, but there is no DTL on a
4988 * writeable leaf device, we have nothing to do.
4990 if (func
== POOL_SCAN_RESILVER
&&
4991 !vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
)) {
4992 spa_async_request(spa
, SPA_ASYNC_RESILVER_DONE
);
4996 return (dsl_scan(spa
->spa_dsl_pool
, func
));
5000 * ==========================================================================
5001 * SPA async task processing
5002 * ==========================================================================
5006 spa_async_remove(spa_t
*spa
, vdev_t
*vd
)
5010 if (vd
->vdev_remove_wanted
) {
5011 vd
->vdev_remove_wanted
= B_FALSE
;
5012 vd
->vdev_delayed_close
= B_FALSE
;
5013 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_REMOVED
, VDEV_AUX_NONE
);
5016 * We want to clear the stats, but we don't want to do a full
5017 * vdev_clear() as that will cause us to throw away
5018 * degraded/faulted state as well as attempt to reopen the
5019 * device, all of which is a waste.
5021 vd
->vdev_stat
.vs_read_errors
= 0;
5022 vd
->vdev_stat
.vs_write_errors
= 0;
5023 vd
->vdev_stat
.vs_checksum_errors
= 0;
5025 vdev_state_dirty(vd
->vdev_top
);
5028 for (c
= 0; c
< vd
->vdev_children
; c
++)
5029 spa_async_remove(spa
, vd
->vdev_child
[c
]);
5033 spa_async_probe(spa_t
*spa
, vdev_t
*vd
)
5037 if (vd
->vdev_probe_wanted
) {
5038 vd
->vdev_probe_wanted
= B_FALSE
;
5039 vdev_reopen(vd
); /* vdev_open() does the actual probe */
5042 for (c
= 0; c
< vd
->vdev_children
; c
++)
5043 spa_async_probe(spa
, vd
->vdev_child
[c
]);
5047 spa_async_autoexpand(spa_t
*spa
, vdev_t
*vd
)
5051 if (!spa
->spa_autoexpand
)
5054 for (c
= 0; c
< vd
->vdev_children
; c
++) {
5055 vdev_t
*cvd
= vd
->vdev_child
[c
];
5056 spa_async_autoexpand(spa
, cvd
);
5059 if (!vd
->vdev_ops
->vdev_op_leaf
|| vd
->vdev_physpath
== NULL
)
5062 spa_event_notify(vd
->vdev_spa
, vd
, FM_EREPORT_ZFS_DEVICE_AUTOEXPAND
);
5066 spa_async_thread(spa_t
*spa
)
5070 ASSERT(spa
->spa_sync_on
);
5072 mutex_enter(&spa
->spa_async_lock
);
5073 tasks
= spa
->spa_async_tasks
;
5074 spa
->spa_async_tasks
= 0;
5075 mutex_exit(&spa
->spa_async_lock
);
5078 * See if the config needs to be updated.
5080 if (tasks
& SPA_ASYNC_CONFIG_UPDATE
) {
5081 uint64_t old_space
, new_space
;
5083 mutex_enter(&spa_namespace_lock
);
5084 old_space
= metaslab_class_get_space(spa_normal_class(spa
));
5085 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
5086 new_space
= metaslab_class_get_space(spa_normal_class(spa
));
5087 mutex_exit(&spa_namespace_lock
);
5090 * If the pool grew as a result of the config update,
5091 * then log an internal history event.
5093 if (new_space
!= old_space
) {
5094 spa_history_log_internal(LOG_POOL_VDEV_ONLINE
,
5096 "pool '%s' size: %llu(+%llu)",
5097 spa_name(spa
), new_space
, new_space
- old_space
);
5102 * See if any devices need to be marked REMOVED.
5104 if (tasks
& SPA_ASYNC_REMOVE
) {
5105 spa_vdev_state_enter(spa
, SCL_NONE
);
5106 spa_async_remove(spa
, spa
->spa_root_vdev
);
5107 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++)
5108 spa_async_remove(spa
, spa
->spa_l2cache
.sav_vdevs
[i
]);
5109 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
5110 spa_async_remove(spa
, spa
->spa_spares
.sav_vdevs
[i
]);
5111 (void) spa_vdev_state_exit(spa
, NULL
, 0);
5114 if ((tasks
& SPA_ASYNC_AUTOEXPAND
) && !spa_suspended(spa
)) {
5115 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
5116 spa_async_autoexpand(spa
, spa
->spa_root_vdev
);
5117 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
5121 * See if any devices need to be probed.
5123 if (tasks
& SPA_ASYNC_PROBE
) {
5124 spa_vdev_state_enter(spa
, SCL_NONE
);
5125 spa_async_probe(spa
, spa
->spa_root_vdev
);
5126 (void) spa_vdev_state_exit(spa
, NULL
, 0);
5130 * If any devices are done replacing, detach them.
5132 if (tasks
& SPA_ASYNC_RESILVER_DONE
)
5133 spa_vdev_resilver_done(spa
);
5136 * Kick off a resilver.
5138 if (tasks
& SPA_ASYNC_RESILVER
)
5139 dsl_resilver_restart(spa
->spa_dsl_pool
, 0);
5142 * Let the world know that we're done.
5144 mutex_enter(&spa
->spa_async_lock
);
5145 spa
->spa_async_thread
= NULL
;
5146 cv_broadcast(&spa
->spa_async_cv
);
5147 mutex_exit(&spa
->spa_async_lock
);
5152 spa_async_suspend(spa_t
*spa
)
5154 mutex_enter(&spa
->spa_async_lock
);
5155 spa
->spa_async_suspended
++;
5156 while (spa
->spa_async_thread
!= NULL
)
5157 cv_wait(&spa
->spa_async_cv
, &spa
->spa_async_lock
);
5158 mutex_exit(&spa
->spa_async_lock
);
5162 spa_async_resume(spa_t
*spa
)
5164 mutex_enter(&spa
->spa_async_lock
);
5165 ASSERT(spa
->spa_async_suspended
!= 0);
5166 spa
->spa_async_suspended
--;
5167 mutex_exit(&spa
->spa_async_lock
);
5171 spa_async_dispatch(spa_t
*spa
)
5173 mutex_enter(&spa
->spa_async_lock
);
5174 if (spa
->spa_async_tasks
&& !spa
->spa_async_suspended
&&
5175 spa
->spa_async_thread
== NULL
&&
5176 rootdir
!= NULL
&& !vn_is_readonly(rootdir
))
5177 spa
->spa_async_thread
= thread_create(NULL
, 0,
5178 spa_async_thread
, spa
, 0, &p0
, TS_RUN
, maxclsyspri
);
5179 mutex_exit(&spa
->spa_async_lock
);
5183 spa_async_request(spa_t
*spa
, int task
)
5185 zfs_dbgmsg("spa=%s async request task=%u", spa
->spa_name
, task
);
5186 mutex_enter(&spa
->spa_async_lock
);
5187 spa
->spa_async_tasks
|= task
;
5188 mutex_exit(&spa
->spa_async_lock
);
5192 * ==========================================================================
5193 * SPA syncing routines
5194 * ==========================================================================
5198 bpobj_enqueue_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
5201 bpobj_enqueue(bpo
, bp
, tx
);
5206 spa_free_sync_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
5210 zio_nowait(zio_free_sync(zio
, zio
->io_spa
, dmu_tx_get_txg(tx
), bp
,
5216 spa_sync_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
*nv
, dmu_tx_t
*tx
)
5218 char *packed
= NULL
;
5223 VERIFY(nvlist_size(nv
, &nvsize
, NV_ENCODE_XDR
) == 0);
5226 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
5227 * information. This avoids the dbuf_will_dirty() path and
5228 * saves us a pre-read to get data we don't actually care about.
5230 bufsize
= P2ROUNDUP(nvsize
, SPA_CONFIG_BLOCKSIZE
);
5231 packed
= vmem_alloc(bufsize
, KM_SLEEP
);
5233 VERIFY(nvlist_pack(nv
, &packed
, &nvsize
, NV_ENCODE_XDR
,
5235 bzero(packed
+ nvsize
, bufsize
- nvsize
);
5237 dmu_write(spa
->spa_meta_objset
, obj
, 0, bufsize
, packed
, tx
);
5239 vmem_free(packed
, bufsize
);
5241 VERIFY(0 == dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
));
5242 dmu_buf_will_dirty(db
, tx
);
5243 *(uint64_t *)db
->db_data
= nvsize
;
5244 dmu_buf_rele(db
, FTAG
);
5248 spa_sync_aux_dev(spa_t
*spa
, spa_aux_vdev_t
*sav
, dmu_tx_t
*tx
,
5249 const char *config
, const char *entry
)
5259 * Update the MOS nvlist describing the list of available devices.
5260 * spa_validate_aux() will have already made sure this nvlist is
5261 * valid and the vdevs are labeled appropriately.
5263 if (sav
->sav_object
== 0) {
5264 sav
->sav_object
= dmu_object_alloc(spa
->spa_meta_objset
,
5265 DMU_OT_PACKED_NVLIST
, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE
,
5266 sizeof (uint64_t), tx
);
5267 VERIFY(zap_update(spa
->spa_meta_objset
,
5268 DMU_POOL_DIRECTORY_OBJECT
, entry
, sizeof (uint64_t), 1,
5269 &sav
->sav_object
, tx
) == 0);
5272 VERIFY(nvlist_alloc(&nvroot
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
5273 if (sav
->sav_count
== 0) {
5274 VERIFY(nvlist_add_nvlist_array(nvroot
, config
, NULL
, 0) == 0);
5276 list
= kmem_alloc(sav
->sav_count
* sizeof (void *), KM_SLEEP
);
5277 for (i
= 0; i
< sav
->sav_count
; i
++)
5278 list
[i
] = vdev_config_generate(spa
, sav
->sav_vdevs
[i
],
5279 B_FALSE
, VDEV_CONFIG_L2CACHE
);
5280 VERIFY(nvlist_add_nvlist_array(nvroot
, config
, list
,
5281 sav
->sav_count
) == 0);
5282 for (i
= 0; i
< sav
->sav_count
; i
++)
5283 nvlist_free(list
[i
]);
5284 kmem_free(list
, sav
->sav_count
* sizeof (void *));
5287 spa_sync_nvlist(spa
, sav
->sav_object
, nvroot
, tx
);
5288 nvlist_free(nvroot
);
5290 sav
->sav_sync
= B_FALSE
;
5294 spa_sync_config_object(spa_t
*spa
, dmu_tx_t
*tx
)
5298 if (list_is_empty(&spa
->spa_config_dirty_list
))
5301 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
5303 config
= spa_config_generate(spa
, spa
->spa_root_vdev
,
5304 dmu_tx_get_txg(tx
), B_FALSE
);
5306 spa_config_exit(spa
, SCL_STATE
, FTAG
);
5308 if (spa
->spa_config_syncing
)
5309 nvlist_free(spa
->spa_config_syncing
);
5310 spa
->spa_config_syncing
= config
;
5312 spa_sync_nvlist(spa
, spa
->spa_config_object
, config
, tx
);
5316 * Set zpool properties.
5319 spa_sync_props(void *arg1
, void *arg2
, dmu_tx_t
*tx
)
5322 objset_t
*mos
= spa
->spa_meta_objset
;
5323 nvlist_t
*nvp
= arg2
;
5328 const char *propname
;
5329 zprop_type_t proptype
;
5331 mutex_enter(&spa
->spa_props_lock
);
5334 while ((elem
= nvlist_next_nvpair(nvp
, elem
))) {
5335 switch (prop
= zpool_name_to_prop(nvpair_name(elem
))) {
5336 case ZPOOL_PROP_VERSION
:
5338 * Only set version for non-zpool-creation cases
5339 * (set/import). spa_create() needs special care
5340 * for version setting.
5342 if (tx
->tx_txg
!= TXG_INITIAL
) {
5343 VERIFY(nvpair_value_uint64(elem
,
5345 ASSERT(intval
<= SPA_VERSION
);
5346 ASSERT(intval
>= spa_version(spa
));
5347 spa
->spa_uberblock
.ub_version
= intval
;
5348 vdev_config_dirty(spa
->spa_root_vdev
);
5352 case ZPOOL_PROP_ALTROOT
:
5354 * 'altroot' is a non-persistent property. It should
5355 * have been set temporarily at creation or import time.
5357 ASSERT(spa
->spa_root
!= NULL
);
5360 case ZPOOL_PROP_READONLY
:
5361 case ZPOOL_PROP_CACHEFILE
:
5363 * 'readonly' and 'cachefile' are also non-persisitent
5369 * Set pool property values in the poolprops mos object.
5371 if (spa
->spa_pool_props_object
== 0) {
5372 VERIFY((spa
->spa_pool_props_object
=
5373 zap_create(mos
, DMU_OT_POOL_PROPS
,
5374 DMU_OT_NONE
, 0, tx
)) > 0);
5376 VERIFY(zap_update(mos
,
5377 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_PROPS
,
5378 8, 1, &spa
->spa_pool_props_object
, tx
)
5382 /* normalize the property name */
5383 propname
= zpool_prop_to_name(prop
);
5384 proptype
= zpool_prop_get_type(prop
);
5386 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
5387 ASSERT(proptype
== PROP_TYPE_STRING
);
5388 VERIFY(nvpair_value_string(elem
, &strval
) == 0);
5389 VERIFY(zap_update(mos
,
5390 spa
->spa_pool_props_object
, propname
,
5391 1, strlen(strval
) + 1, strval
, tx
) == 0);
5393 } else if (nvpair_type(elem
) == DATA_TYPE_UINT64
) {
5394 VERIFY(nvpair_value_uint64(elem
, &intval
) == 0);
5396 if (proptype
== PROP_TYPE_INDEX
) {
5398 VERIFY(zpool_prop_index_to_string(
5399 prop
, intval
, &unused
) == 0);
5401 VERIFY(zap_update(mos
,
5402 spa
->spa_pool_props_object
, propname
,
5403 8, 1, &intval
, tx
) == 0);
5405 ASSERT(0); /* not allowed */
5409 case ZPOOL_PROP_DELEGATION
:
5410 spa
->spa_delegation
= intval
;
5412 case ZPOOL_PROP_BOOTFS
:
5413 spa
->spa_bootfs
= intval
;
5415 case ZPOOL_PROP_FAILUREMODE
:
5416 spa
->spa_failmode
= intval
;
5418 case ZPOOL_PROP_AUTOEXPAND
:
5419 spa
->spa_autoexpand
= intval
;
5420 if (tx
->tx_txg
!= TXG_INITIAL
)
5421 spa_async_request(spa
,
5422 SPA_ASYNC_AUTOEXPAND
);
5424 case ZPOOL_PROP_DEDUPDITTO
:
5425 spa
->spa_dedup_ditto
= intval
;
5432 /* log internal history if this is not a zpool create */
5433 if (spa_version(spa
) >= SPA_VERSION_ZPOOL_HISTORY
&&
5434 tx
->tx_txg
!= TXG_INITIAL
) {
5435 spa_history_log_internal(LOG_POOL_PROPSET
,
5436 spa
, tx
, "%s %lld %s",
5437 nvpair_name(elem
), intval
, spa_name(spa
));
5441 mutex_exit(&spa
->spa_props_lock
);
5445 * Perform one-time upgrade on-disk changes. spa_version() does not
5446 * reflect the new version this txg, so there must be no changes this
5447 * txg to anything that the upgrade code depends on after it executes.
5448 * Therefore this must be called after dsl_pool_sync() does the sync
5452 spa_sync_upgrades(spa_t
*spa
, dmu_tx_t
*tx
)
5454 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
5456 ASSERT(spa
->spa_sync_pass
== 1);
5458 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_ORIGIN
&&
5459 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_ORIGIN
) {
5460 dsl_pool_create_origin(dp
, tx
);
5462 /* Keeping the origin open increases spa_minref */
5463 spa
->spa_minref
+= 3;
5466 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_NEXT_CLONES
&&
5467 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_NEXT_CLONES
) {
5468 dsl_pool_upgrade_clones(dp
, tx
);
5471 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_DIR_CLONES
&&
5472 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_DIR_CLONES
) {
5473 dsl_pool_upgrade_dir_clones(dp
, tx
);
5475 /* Keeping the freedir open increases spa_minref */
5476 spa
->spa_minref
+= 3;
5481 * Sync the specified transaction group. New blocks may be dirtied as
5482 * part of the process, so we iterate until it converges.
5485 spa_sync(spa_t
*spa
, uint64_t txg
)
5487 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
5488 objset_t
*mos
= spa
->spa_meta_objset
;
5489 bpobj_t
*defer_bpo
= &spa
->spa_deferred_bpobj
;
5490 bplist_t
*free_bpl
= &spa
->spa_free_bplist
[txg
& TXG_MASK
];
5491 vdev_t
*rvd
= spa
->spa_root_vdev
;
5497 VERIFY(spa_writeable(spa
));
5500 * Lock out configuration changes.
5502 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
5504 spa
->spa_syncing_txg
= txg
;
5505 spa
->spa_sync_pass
= 0;
5508 * If there are any pending vdev state changes, convert them
5509 * into config changes that go out with this transaction group.
5511 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
5512 while (list_head(&spa
->spa_state_dirty_list
) != NULL
) {
5514 * We need the write lock here because, for aux vdevs,
5515 * calling vdev_config_dirty() modifies sav_config.
5516 * This is ugly and will become unnecessary when we
5517 * eliminate the aux vdev wart by integrating all vdevs
5518 * into the root vdev tree.
5520 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
5521 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_WRITER
);
5522 while ((vd
= list_head(&spa
->spa_state_dirty_list
)) != NULL
) {
5523 vdev_state_clean(vd
);
5524 vdev_config_dirty(vd
);
5526 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
5527 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
5529 spa_config_exit(spa
, SCL_STATE
, FTAG
);
5531 tx
= dmu_tx_create_assigned(dp
, txg
);
5534 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
5535 * set spa_deflate if we have no raid-z vdevs.
5537 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_RAIDZ_DEFLATE
&&
5538 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
5541 for (i
= 0; i
< rvd
->vdev_children
; i
++) {
5542 vd
= rvd
->vdev_child
[i
];
5543 if (vd
->vdev_deflate_ratio
!= SPA_MINBLOCKSIZE
)
5546 if (i
== rvd
->vdev_children
) {
5547 spa
->spa_deflate
= TRUE
;
5548 VERIFY(0 == zap_add(spa
->spa_meta_objset
,
5549 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
5550 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
));
5555 * If anything has changed in this txg, or if someone is waiting
5556 * for this txg to sync (eg, spa_vdev_remove()), push the
5557 * deferred frees from the previous txg. If not, leave them
5558 * alone so that we don't generate work on an otherwise idle
5561 if (!txg_list_empty(&dp
->dp_dirty_datasets
, txg
) ||
5562 !txg_list_empty(&dp
->dp_dirty_dirs
, txg
) ||
5563 !txg_list_empty(&dp
->dp_sync_tasks
, txg
) ||
5564 ((dsl_scan_active(dp
->dp_scan
) ||
5565 txg_sync_waiting(dp
)) && !spa_shutting_down(spa
))) {
5566 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
5567 VERIFY3U(bpobj_iterate(defer_bpo
,
5568 spa_free_sync_cb
, zio
, tx
), ==, 0);
5569 VERIFY3U(zio_wait(zio
), ==, 0);
5573 * Iterate to convergence.
5576 int pass
= ++spa
->spa_sync_pass
;
5578 spa_sync_config_object(spa
, tx
);
5579 spa_sync_aux_dev(spa
, &spa
->spa_spares
, tx
,
5580 ZPOOL_CONFIG_SPARES
, DMU_POOL_SPARES
);
5581 spa_sync_aux_dev(spa
, &spa
->spa_l2cache
, tx
,
5582 ZPOOL_CONFIG_L2CACHE
, DMU_POOL_L2CACHE
);
5583 spa_errlog_sync(spa
, txg
);
5584 dsl_pool_sync(dp
, txg
);
5586 if (pass
<= SYNC_PASS_DEFERRED_FREE
) {
5587 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
5588 bplist_iterate(free_bpl
, spa_free_sync_cb
,
5590 VERIFY(zio_wait(zio
) == 0);
5592 bplist_iterate(free_bpl
, bpobj_enqueue_cb
,
5597 dsl_scan_sync(dp
, tx
);
5599 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, txg
)))
5603 spa_sync_upgrades(spa
, tx
);
5605 } while (dmu_objset_is_dirty(mos
, txg
));
5608 * Rewrite the vdev configuration (which includes the uberblock)
5609 * to commit the transaction group.
5611 * If there are no dirty vdevs, we sync the uberblock to a few
5612 * random top-level vdevs that are known to be visible in the
5613 * config cache (see spa_vdev_add() for a complete description).
5614 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
5618 * We hold SCL_STATE to prevent vdev open/close/etc.
5619 * while we're attempting to write the vdev labels.
5621 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
5623 if (list_is_empty(&spa
->spa_config_dirty_list
)) {
5624 vdev_t
*svd
[SPA_DVAS_PER_BP
];
5626 int children
= rvd
->vdev_children
;
5627 int c0
= spa_get_random(children
);
5629 for (c
= 0; c
< children
; c
++) {
5630 vd
= rvd
->vdev_child
[(c0
+ c
) % children
];
5631 if (vd
->vdev_ms_array
== 0 || vd
->vdev_islog
)
5633 svd
[svdcount
++] = vd
;
5634 if (svdcount
== SPA_DVAS_PER_BP
)
5637 error
= vdev_config_sync(svd
, svdcount
, txg
, B_FALSE
);
5639 error
= vdev_config_sync(svd
, svdcount
, txg
,
5642 error
= vdev_config_sync(rvd
->vdev_child
,
5643 rvd
->vdev_children
, txg
, B_FALSE
);
5645 error
= vdev_config_sync(rvd
->vdev_child
,
5646 rvd
->vdev_children
, txg
, B_TRUE
);
5649 spa_config_exit(spa
, SCL_STATE
, FTAG
);
5653 zio_suspend(spa
, NULL
);
5654 zio_resume_wait(spa
);
5659 * Clear the dirty config list.
5661 while ((vd
= list_head(&spa
->spa_config_dirty_list
)) != NULL
)
5662 vdev_config_clean(vd
);
5665 * Now that the new config has synced transactionally,
5666 * let it become visible to the config cache.
5668 if (spa
->spa_config_syncing
!= NULL
) {
5669 spa_config_set(spa
, spa
->spa_config_syncing
);
5670 spa
->spa_config_txg
= txg
;
5671 spa
->spa_config_syncing
= NULL
;
5674 spa
->spa_ubsync
= spa
->spa_uberblock
;
5676 dsl_pool_sync_done(dp
, txg
);
5679 * Update usable space statistics.
5681 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, TXG_CLEAN(txg
))))
5682 vdev_sync_done(vd
, txg
);
5684 spa_update_dspace(spa
);
5687 * It had better be the case that we didn't dirty anything
5688 * since vdev_config_sync().
5690 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
, txg
));
5691 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
5692 ASSERT(txg_list_empty(&spa
->spa_vdev_txg_list
, txg
));
5694 spa
->spa_sync_pass
= 0;
5696 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
5698 spa_handle_ignored_writes(spa
);
5701 * If any async tasks have been requested, kick them off.
5703 spa_async_dispatch(spa
);
5707 * Sync all pools. We don't want to hold the namespace lock across these
5708 * operations, so we take a reference on the spa_t and drop the lock during the
5712 spa_sync_allpools(void)
5715 mutex_enter(&spa_namespace_lock
);
5716 while ((spa
= spa_next(spa
)) != NULL
) {
5717 if (spa_state(spa
) != POOL_STATE_ACTIVE
||
5718 !spa_writeable(spa
) || spa_suspended(spa
))
5720 spa_open_ref(spa
, FTAG
);
5721 mutex_exit(&spa_namespace_lock
);
5722 txg_wait_synced(spa_get_dsl(spa
), 0);
5723 mutex_enter(&spa_namespace_lock
);
5724 spa_close(spa
, FTAG
);
5726 mutex_exit(&spa_namespace_lock
);
5730 * ==========================================================================
5731 * Miscellaneous routines
5732 * ==========================================================================
5736 * Remove all pools in the system.
5744 * Remove all cached state. All pools should be closed now,
5745 * so every spa in the AVL tree should be unreferenced.
5747 mutex_enter(&spa_namespace_lock
);
5748 while ((spa
= spa_next(NULL
)) != NULL
) {
5750 * Stop async tasks. The async thread may need to detach
5751 * a device that's been replaced, which requires grabbing
5752 * spa_namespace_lock, so we must drop it here.
5754 spa_open_ref(spa
, FTAG
);
5755 mutex_exit(&spa_namespace_lock
);
5756 spa_async_suspend(spa
);
5757 mutex_enter(&spa_namespace_lock
);
5758 spa_close(spa
, FTAG
);
5760 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
5762 spa_deactivate(spa
);
5766 mutex_exit(&spa_namespace_lock
);
5770 spa_lookup_by_guid(spa_t
*spa
, uint64_t guid
, boolean_t aux
)
5775 if ((vd
= vdev_lookup_by_guid(spa
->spa_root_vdev
, guid
)) != NULL
)
5779 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
5780 vd
= spa
->spa_l2cache
.sav_vdevs
[i
];
5781 if (vd
->vdev_guid
== guid
)
5785 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
5786 vd
= spa
->spa_spares
.sav_vdevs
[i
];
5787 if (vd
->vdev_guid
== guid
)
5796 spa_upgrade(spa_t
*spa
, uint64_t version
)
5798 ASSERT(spa_writeable(spa
));
5800 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5803 * This should only be called for a non-faulted pool, and since a
5804 * future version would result in an unopenable pool, this shouldn't be
5807 ASSERT(spa
->spa_uberblock
.ub_version
<= SPA_VERSION
);
5808 ASSERT(version
>= spa
->spa_uberblock
.ub_version
);
5810 spa
->spa_uberblock
.ub_version
= version
;
5811 vdev_config_dirty(spa
->spa_root_vdev
);
5813 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5815 txg_wait_synced(spa_get_dsl(spa
), 0);
5819 spa_has_spare(spa_t
*spa
, uint64_t guid
)
5823 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
5825 for (i
= 0; i
< sav
->sav_count
; i
++)
5826 if (sav
->sav_vdevs
[i
]->vdev_guid
== guid
)
5829 for (i
= 0; i
< sav
->sav_npending
; i
++) {
5830 if (nvlist_lookup_uint64(sav
->sav_pending
[i
], ZPOOL_CONFIG_GUID
,
5831 &spareguid
) == 0 && spareguid
== guid
)
5839 * Check if a pool has an active shared spare device.
5840 * Note: reference count of an active spare is 2, as a spare and as a replace
5843 spa_has_active_shared_spare(spa_t
*spa
)
5847 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
5849 for (i
= 0; i
< sav
->sav_count
; i
++) {
5850 if (spa_spare_exists(sav
->sav_vdevs
[i
]->vdev_guid
, &pool
,
5851 &refcnt
) && pool
!= 0ULL && pool
== spa_guid(spa
) &&
5860 * Post a FM_EREPORT_ZFS_* event from sys/fm/fs/zfs.h. The payload will be
5861 * filled in from the spa and (optionally) the vdev. This doesn't do anything
5862 * in the userland libzpool, as we don't want consumers to misinterpret ztest
5863 * or zdb as real changes.
5866 spa_event_notify(spa_t
*spa
, vdev_t
*vd
, const char *name
)
5869 zfs_ereport_post(name
, spa
, vd
, NULL
, 0, 0);
5873 #if defined(_KERNEL) && defined(HAVE_SPL)
5874 /* state manipulation functions */
5875 EXPORT_SYMBOL(spa_open
);
5876 EXPORT_SYMBOL(spa_open_rewind
);
5877 EXPORT_SYMBOL(spa_get_stats
);
5878 EXPORT_SYMBOL(spa_create
);
5879 EXPORT_SYMBOL(spa_import_rootpool
);
5880 EXPORT_SYMBOL(spa_import
);
5881 EXPORT_SYMBOL(spa_tryimport
);
5882 EXPORT_SYMBOL(spa_destroy
);
5883 EXPORT_SYMBOL(spa_export
);
5884 EXPORT_SYMBOL(spa_reset
);
5885 EXPORT_SYMBOL(spa_async_request
);
5886 EXPORT_SYMBOL(spa_async_suspend
);
5887 EXPORT_SYMBOL(spa_async_resume
);
5888 EXPORT_SYMBOL(spa_inject_addref
);
5889 EXPORT_SYMBOL(spa_inject_delref
);
5890 EXPORT_SYMBOL(spa_scan_stat_init
);
5891 EXPORT_SYMBOL(spa_scan_get_stats
);
5893 /* device maniion */
5894 EXPORT_SYMBOL(spa_vdev_add
);
5895 EXPORT_SYMBOL(spa_vdev_attach
);
5896 EXPORT_SYMBOL(spa_vdev_detach
);
5897 EXPORT_SYMBOL(spa_vdev_remove
);
5898 EXPORT_SYMBOL(spa_vdev_setpath
);
5899 EXPORT_SYMBOL(spa_vdev_setfru
);
5900 EXPORT_SYMBOL(spa_vdev_split_mirror
);
5902 /* spare statech is global across all pools) */
5903 EXPORT_SYMBOL(spa_spare_add
);
5904 EXPORT_SYMBOL(spa_spare_remove
);
5905 EXPORT_SYMBOL(spa_spare_exists
);
5906 EXPORT_SYMBOL(spa_spare_activate
);
5908 /* L2ARC statech is global across all pools) */
5909 EXPORT_SYMBOL(spa_l2cache_add
);
5910 EXPORT_SYMBOL(spa_l2cache_remove
);
5911 EXPORT_SYMBOL(spa_l2cache_exists
);
5912 EXPORT_SYMBOL(spa_l2cache_activate
);
5913 EXPORT_SYMBOL(spa_l2cache_drop
);
5916 EXPORT_SYMBOL(spa_scan
);
5917 EXPORT_SYMBOL(spa_scan_stop
);
5920 EXPORT_SYMBOL(spa_sync
); /* only for DMU use */
5921 EXPORT_SYMBOL(spa_sync_allpools
);
5924 EXPORT_SYMBOL(spa_prop_set
);
5925 EXPORT_SYMBOL(spa_prop_get
);
5926 EXPORT_SYMBOL(spa_prop_clear_bootfs
);
5928 /* asynchronous event notification */
5929 EXPORT_SYMBOL(spa_event_notify
);