4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2012 by Delphix. All rights reserved.
29 * This file contains all the routines used when modifying on-disk SPA state.
30 * This includes opening, importing, destroying, exporting a pool, and syncing a
34 #include <sys/zfs_context.h>
35 #include <sys/fm/fs/zfs.h>
36 #include <sys/spa_impl.h>
38 #include <sys/zio_checksum.h>
40 #include <sys/dmu_tx.h>
44 #include <sys/vdev_impl.h>
45 #include <sys/vdev_disk.h>
46 #include <sys/metaslab.h>
47 #include <sys/metaslab_impl.h>
48 #include <sys/uberblock_impl.h>
51 #include <sys/dmu_traverse.h>
52 #include <sys/dmu_objset.h>
53 #include <sys/unique.h>
54 #include <sys/dsl_pool.h>
55 #include <sys/dsl_dataset.h>
56 #include <sys/dsl_dir.h>
57 #include <sys/dsl_prop.h>
58 #include <sys/dsl_synctask.h>
59 #include <sys/fs/zfs.h>
61 #include <sys/callb.h>
62 #include <sys/systeminfo.h>
63 #include <sys/spa_boot.h>
64 #include <sys/zfs_ioctl.h>
65 #include <sys/dsl_scan.h>
68 #include <sys/bootprops.h>
69 #include <sys/callb.h>
70 #include <sys/cpupart.h>
72 #include <sys/sysdc.h>
77 #include "zfs_comutil.h"
79 typedef enum zti_modes
{
80 zti_mode_fixed
, /* value is # of threads (min 1) */
81 zti_mode_online_percent
, /* value is % of online CPUs */
82 zti_mode_batch
, /* cpu-intensive; value is ignored */
83 zti_mode_null
, /* don't create a taskq */
87 #define ZTI_FIX(n) { zti_mode_fixed, (n) }
88 #define ZTI_PCT(n) { zti_mode_online_percent, (n) }
89 #define ZTI_BATCH { zti_mode_batch, 0 }
90 #define ZTI_NULL { zti_mode_null, 0 }
92 #define ZTI_ONE ZTI_FIX(1)
94 typedef struct zio_taskq_info
{
95 enum zti_modes zti_mode
;
99 static const char *const zio_taskq_types
[ZIO_TASKQ_TYPES
] = {
100 "iss", "iss_h", "int", "int_h"
104 * Define the taskq threads for the following I/O types:
105 * NULL, READ, WRITE, FREE, CLAIM, and IOCTL
107 const zio_taskq_info_t zio_taskqs
[ZIO_TYPES
][ZIO_TASKQ_TYPES
] = {
108 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
109 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
},
110 { ZTI_FIX(8), ZTI_NULL
, ZTI_BATCH
, ZTI_NULL
},
111 { ZTI_BATCH
, ZTI_FIX(5), ZTI_FIX(16), ZTI_FIX(5) },
112 { ZTI_PCT(100), ZTI_NULL
, ZTI_ONE
, ZTI_NULL
},
113 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
},
114 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
},
117 static dsl_syncfunc_t spa_sync_props
;
118 static boolean_t
spa_has_active_shared_spare(spa_t
*spa
);
119 static inline int spa_load_impl(spa_t
*spa
, uint64_t, nvlist_t
*config
,
120 spa_load_state_t state
, spa_import_type_t type
, boolean_t mosconfig
,
122 static void spa_vdev_resilver_done(spa_t
*spa
);
124 uint_t zio_taskq_batch_pct
= 100; /* 1 thread per cpu in pset */
125 id_t zio_taskq_psrset_bind
= PS_NONE
;
126 boolean_t zio_taskq_sysdc
= B_TRUE
; /* use SDC scheduling class */
127 uint_t zio_taskq_basedc
= 80; /* base duty cycle */
129 boolean_t spa_create_process
= B_TRUE
; /* no process ==> no sysdc */
132 * This (illegal) pool name is used when temporarily importing a spa_t in order
133 * to get the vdev stats associated with the imported devices.
135 #define TRYIMPORT_NAME "$import"
138 * ==========================================================================
139 * SPA properties routines
140 * ==========================================================================
144 * Add a (source=src, propname=propval) list to an nvlist.
147 spa_prop_add_list(nvlist_t
*nvl
, zpool_prop_t prop
, char *strval
,
148 uint64_t intval
, zprop_source_t src
)
150 const char *propname
= zpool_prop_to_name(prop
);
153 VERIFY(nvlist_alloc(&propval
, NV_UNIQUE_NAME
, KM_PUSHPAGE
) == 0);
154 VERIFY(nvlist_add_uint64(propval
, ZPROP_SOURCE
, src
) == 0);
157 VERIFY(nvlist_add_string(propval
, ZPROP_VALUE
, strval
) == 0);
159 VERIFY(nvlist_add_uint64(propval
, ZPROP_VALUE
, intval
) == 0);
161 VERIFY(nvlist_add_nvlist(nvl
, propname
, propval
) == 0);
162 nvlist_free(propval
);
166 * Get property values from the spa configuration.
169 spa_prop_get_config(spa_t
*spa
, nvlist_t
**nvp
)
171 vdev_t
*rvd
= spa
->spa_root_vdev
;
175 uint64_t cap
, version
;
176 zprop_source_t src
= ZPROP_SRC_NONE
;
177 spa_config_dirent_t
*dp
;
180 ASSERT(MUTEX_HELD(&spa
->spa_props_lock
));
183 alloc
= metaslab_class_get_alloc(spa_normal_class(spa
));
184 size
= metaslab_class_get_space(spa_normal_class(spa
));
185 spa_prop_add_list(*nvp
, ZPOOL_PROP_NAME
, spa_name(spa
), 0, src
);
186 spa_prop_add_list(*nvp
, ZPOOL_PROP_SIZE
, NULL
, size
, src
);
187 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALLOCATED
, NULL
, alloc
, src
);
188 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREE
, NULL
,
192 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
193 vdev_t
*tvd
= rvd
->vdev_child
[c
];
194 space
+= tvd
->vdev_max_asize
- tvd
->vdev_asize
;
196 spa_prop_add_list(*nvp
, ZPOOL_PROP_EXPANDSZ
, NULL
, space
,
199 spa_prop_add_list(*nvp
, ZPOOL_PROP_READONLY
, NULL
,
200 (spa_mode(spa
) == FREAD
), src
);
202 cap
= (size
== 0) ? 0 : (alloc
* 100 / size
);
203 spa_prop_add_list(*nvp
, ZPOOL_PROP_CAPACITY
, NULL
, cap
, src
);
205 spa_prop_add_list(*nvp
, ZPOOL_PROP_DEDUPRATIO
, NULL
,
206 ddt_get_pool_dedup_ratio(spa
), src
);
208 spa_prop_add_list(*nvp
, ZPOOL_PROP_HEALTH
, NULL
,
209 rvd
->vdev_state
, src
);
211 version
= spa_version(spa
);
212 if (version
== zpool_prop_default_numeric(ZPOOL_PROP_VERSION
))
213 src
= ZPROP_SRC_DEFAULT
;
215 src
= ZPROP_SRC_LOCAL
;
216 spa_prop_add_list(*nvp
, ZPOOL_PROP_VERSION
, NULL
, version
, src
);
219 spa_prop_add_list(*nvp
, ZPOOL_PROP_GUID
, NULL
, spa_guid(spa
), src
);
221 if (spa
->spa_comment
!= NULL
) {
222 spa_prop_add_list(*nvp
, ZPOOL_PROP_COMMENT
, spa
->spa_comment
,
226 if (spa
->spa_root
!= NULL
)
227 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALTROOT
, spa
->spa_root
,
230 if ((dp
= list_head(&spa
->spa_config_list
)) != NULL
) {
231 if (dp
->scd_path
== NULL
) {
232 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
233 "none", 0, ZPROP_SRC_LOCAL
);
234 } else if (strcmp(dp
->scd_path
, spa_config_path
) != 0) {
235 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
236 dp
->scd_path
, 0, ZPROP_SRC_LOCAL
);
242 * Get zpool property values.
245 spa_prop_get(spa_t
*spa
, nvlist_t
**nvp
)
247 objset_t
*mos
= spa
->spa_meta_objset
;
252 err
= nvlist_alloc(nvp
, NV_UNIQUE_NAME
, KM_PUSHPAGE
);
256 mutex_enter(&spa
->spa_props_lock
);
259 * Get properties from the spa config.
261 spa_prop_get_config(spa
, nvp
);
263 /* If no pool property object, no more prop to get. */
264 if (mos
== NULL
|| spa
->spa_pool_props_object
== 0) {
265 mutex_exit(&spa
->spa_props_lock
);
270 * Get properties from the MOS pool property object.
272 for (zap_cursor_init(&zc
, mos
, spa
->spa_pool_props_object
);
273 (err
= zap_cursor_retrieve(&zc
, &za
)) == 0;
274 zap_cursor_advance(&zc
)) {
277 zprop_source_t src
= ZPROP_SRC_DEFAULT
;
280 if ((prop
= zpool_name_to_prop(za
.za_name
)) == ZPROP_INVAL
)
283 switch (za
.za_integer_length
) {
285 /* integer property */
286 if (za
.za_first_integer
!=
287 zpool_prop_default_numeric(prop
))
288 src
= ZPROP_SRC_LOCAL
;
290 if (prop
== ZPOOL_PROP_BOOTFS
) {
292 dsl_dataset_t
*ds
= NULL
;
294 dp
= spa_get_dsl(spa
);
295 rw_enter(&dp
->dp_config_rwlock
, RW_READER
);
296 if ((err
= dsl_dataset_hold_obj(dp
,
297 za
.za_first_integer
, FTAG
, &ds
))) {
298 rw_exit(&dp
->dp_config_rwlock
);
303 MAXNAMELEN
+ strlen(MOS_DIR_NAME
) + 1,
305 dsl_dataset_name(ds
, strval
);
306 dsl_dataset_rele(ds
, FTAG
);
307 rw_exit(&dp
->dp_config_rwlock
);
310 intval
= za
.za_first_integer
;
313 spa_prop_add_list(*nvp
, prop
, strval
, intval
, src
);
317 MAXNAMELEN
+ strlen(MOS_DIR_NAME
) + 1);
322 /* string property */
323 strval
= kmem_alloc(za
.za_num_integers
, KM_PUSHPAGE
);
324 err
= zap_lookup(mos
, spa
->spa_pool_props_object
,
325 za
.za_name
, 1, za
.za_num_integers
, strval
);
327 kmem_free(strval
, za
.za_num_integers
);
330 spa_prop_add_list(*nvp
, prop
, strval
, 0, src
);
331 kmem_free(strval
, za
.za_num_integers
);
338 zap_cursor_fini(&zc
);
339 mutex_exit(&spa
->spa_props_lock
);
341 if (err
&& err
!= ENOENT
) {
351 * Validate the given pool properties nvlist and modify the list
352 * for the property values to be set.
355 spa_prop_validate(spa_t
*spa
, nvlist_t
*props
)
358 int error
= 0, reset_bootfs
= 0;
362 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
364 char *propname
, *strval
;
369 propname
= nvpair_name(elem
);
371 if ((prop
= zpool_name_to_prop(propname
)) == ZPROP_INVAL
)
375 case ZPOOL_PROP_VERSION
:
376 error
= nvpair_value_uint64(elem
, &intval
);
378 (intval
< spa_version(spa
) || intval
> SPA_VERSION
))
382 case ZPOOL_PROP_DELEGATION
:
383 case ZPOOL_PROP_AUTOREPLACE
:
384 case ZPOOL_PROP_LISTSNAPS
:
385 case ZPOOL_PROP_AUTOEXPAND
:
386 error
= nvpair_value_uint64(elem
, &intval
);
387 if (!error
&& intval
> 1)
391 case ZPOOL_PROP_BOOTFS
:
393 * If the pool version is less than SPA_VERSION_BOOTFS,
394 * or the pool is still being created (version == 0),
395 * the bootfs property cannot be set.
397 if (spa_version(spa
) < SPA_VERSION_BOOTFS
) {
403 * Make sure the vdev config is bootable
405 if (!vdev_is_bootable(spa
->spa_root_vdev
)) {
412 error
= nvpair_value_string(elem
, &strval
);
417 if (strval
== NULL
|| strval
[0] == '\0') {
418 objnum
= zpool_prop_default_numeric(
423 if ((error
= dmu_objset_hold(strval
,FTAG
,&os
)))
426 /* Must be ZPL and not gzip compressed. */
428 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
430 } else if ((error
= dsl_prop_get_integer(strval
,
431 zfs_prop_to_name(ZFS_PROP_COMPRESSION
),
432 &compress
, NULL
)) == 0 &&
433 !BOOTFS_COMPRESS_VALID(compress
)) {
436 objnum
= dmu_objset_id(os
);
438 dmu_objset_rele(os
, FTAG
);
442 case ZPOOL_PROP_FAILUREMODE
:
443 error
= nvpair_value_uint64(elem
, &intval
);
444 if (!error
&& (intval
< ZIO_FAILURE_MODE_WAIT
||
445 intval
> ZIO_FAILURE_MODE_PANIC
))
449 * This is a special case which only occurs when
450 * the pool has completely failed. This allows
451 * the user to change the in-core failmode property
452 * without syncing it out to disk (I/Os might
453 * currently be blocked). We do this by returning
454 * EIO to the caller (spa_prop_set) to trick it
455 * into thinking we encountered a property validation
458 if (!error
&& spa_suspended(spa
)) {
459 spa
->spa_failmode
= intval
;
464 case ZPOOL_PROP_CACHEFILE
:
465 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
468 if (strval
[0] == '\0')
471 if (strcmp(strval
, "none") == 0)
474 if (strval
[0] != '/') {
479 slash
= strrchr(strval
, '/');
480 ASSERT(slash
!= NULL
);
482 if (slash
[1] == '\0' || strcmp(slash
, "/.") == 0 ||
483 strcmp(slash
, "/..") == 0)
487 case ZPOOL_PROP_COMMENT
:
488 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
490 for (check
= strval
; *check
!= '\0'; check
++) {
491 if (!isprint(*check
)) {
497 if (strlen(strval
) > ZPROP_MAX_COMMENT
)
501 case ZPOOL_PROP_DEDUPDITTO
:
502 if (spa_version(spa
) < SPA_VERSION_DEDUP
)
505 error
= nvpair_value_uint64(elem
, &intval
);
507 intval
!= 0 && intval
< ZIO_DEDUPDITTO_MIN
)
519 if (!error
&& reset_bootfs
) {
520 error
= nvlist_remove(props
,
521 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), DATA_TYPE_STRING
);
524 error
= nvlist_add_uint64(props
,
525 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), objnum
);
533 spa_configfile_set(spa_t
*spa
, nvlist_t
*nvp
, boolean_t need_sync
)
536 spa_config_dirent_t
*dp
;
538 if (nvlist_lookup_string(nvp
, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
),
542 dp
= kmem_alloc(sizeof (spa_config_dirent_t
),
545 if (cachefile
[0] == '\0')
546 dp
->scd_path
= spa_strdup(spa_config_path
);
547 else if (strcmp(cachefile
, "none") == 0)
550 dp
->scd_path
= spa_strdup(cachefile
);
552 list_insert_head(&spa
->spa_config_list
, dp
);
554 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
558 spa_prop_set(spa_t
*spa
, nvlist_t
*nvp
)
562 boolean_t need_sync
= B_FALSE
;
565 if ((error
= spa_prop_validate(spa
, nvp
)) != 0)
569 while ((elem
= nvlist_next_nvpair(nvp
, elem
)) != NULL
) {
570 if ((prop
= zpool_name_to_prop(
571 nvpair_name(elem
))) == ZPROP_INVAL
)
574 if (prop
== ZPOOL_PROP_CACHEFILE
||
575 prop
== ZPOOL_PROP_ALTROOT
||
576 prop
== ZPOOL_PROP_READONLY
)
584 return (dsl_sync_task_do(spa_get_dsl(spa
), NULL
, spa_sync_props
,
591 * If the bootfs property value is dsobj, clear it.
594 spa_prop_clear_bootfs(spa_t
*spa
, uint64_t dsobj
, dmu_tx_t
*tx
)
596 if (spa
->spa_bootfs
== dsobj
&& spa
->spa_pool_props_object
!= 0) {
597 VERIFY(zap_remove(spa
->spa_meta_objset
,
598 spa
->spa_pool_props_object
,
599 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), tx
) == 0);
605 * Change the GUID for the pool. This is done so that we can later
606 * re-import a pool built from a clone of our own vdevs. We will modify
607 * the root vdev's guid, our own pool guid, and then mark all of our
608 * vdevs dirty. Note that we must make sure that all our vdevs are
609 * online when we do this, or else any vdevs that weren't present
610 * would be orphaned from our pool. We are also going to issue a
611 * sysevent to update any watchers.
614 spa_change_guid(spa_t
*spa
)
616 uint64_t oldguid
, newguid
;
619 if (!(spa_mode_global
& FWRITE
))
622 txg
= spa_vdev_enter(spa
);
624 if (spa
->spa_root_vdev
->vdev_state
!= VDEV_STATE_HEALTHY
)
625 return (spa_vdev_exit(spa
, NULL
, txg
, ENXIO
));
627 oldguid
= spa_guid(spa
);
628 newguid
= spa_generate_guid(NULL
);
629 ASSERT3U(oldguid
, !=, newguid
);
631 spa
->spa_root_vdev
->vdev_guid
= newguid
;
632 spa
->spa_root_vdev
->vdev_guid_sum
+= (newguid
- oldguid
);
634 vdev_config_dirty(spa
->spa_root_vdev
);
636 spa_event_notify(spa
, NULL
, FM_EREPORT_ZFS_POOL_REGUID
);
638 return (spa_vdev_exit(spa
, NULL
, txg
, 0));
642 * ==========================================================================
643 * SPA state manipulation (open/create/destroy/import/export)
644 * ==========================================================================
648 spa_error_entry_compare(const void *a
, const void *b
)
650 spa_error_entry_t
*sa
= (spa_error_entry_t
*)a
;
651 spa_error_entry_t
*sb
= (spa_error_entry_t
*)b
;
654 ret
= bcmp(&sa
->se_bookmark
, &sb
->se_bookmark
,
655 sizeof (zbookmark_t
));
666 * Utility function which retrieves copies of the current logs and
667 * re-initializes them in the process.
670 spa_get_errlists(spa_t
*spa
, avl_tree_t
*last
, avl_tree_t
*scrub
)
672 ASSERT(MUTEX_HELD(&spa
->spa_errlist_lock
));
674 bcopy(&spa
->spa_errlist_last
, last
, sizeof (avl_tree_t
));
675 bcopy(&spa
->spa_errlist_scrub
, scrub
, sizeof (avl_tree_t
));
677 avl_create(&spa
->spa_errlist_scrub
,
678 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
679 offsetof(spa_error_entry_t
, se_avl
));
680 avl_create(&spa
->spa_errlist_last
,
681 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
682 offsetof(spa_error_entry_t
, se_avl
));
686 spa_taskq_create(spa_t
*spa
, const char *name
, enum zti_modes mode
,
689 uint_t flags
= TASKQ_PREPOPULATE
;
690 boolean_t batch
= B_FALSE
;
694 return (NULL
); /* no taskq needed */
697 ASSERT3U(value
, >=, 1);
698 value
= MAX(value
, 1);
703 flags
|= TASKQ_THREADS_CPU_PCT
;
704 value
= zio_taskq_batch_pct
;
707 case zti_mode_online_percent
:
708 flags
|= TASKQ_THREADS_CPU_PCT
;
712 panic("unrecognized mode for %s taskq (%u:%u) in "
718 if (zio_taskq_sysdc
&& spa
->spa_proc
!= &p0
) {
720 flags
|= TASKQ_DC_BATCH
;
722 return (taskq_create_sysdc(name
, value
, 50, INT_MAX
,
723 spa
->spa_proc
, zio_taskq_basedc
, flags
));
725 return (taskq_create_proc(name
, value
, maxclsyspri
, 50, INT_MAX
,
726 spa
->spa_proc
, flags
));
730 spa_create_zio_taskqs(spa_t
*spa
)
734 for (t
= 0; t
< ZIO_TYPES
; t
++) {
735 for (q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
736 const zio_taskq_info_t
*ztip
= &zio_taskqs
[t
][q
];
737 enum zti_modes mode
= ztip
->zti_mode
;
738 uint_t value
= ztip
->zti_value
;
741 (void) snprintf(name
, sizeof (name
),
742 "%s_%s", zio_type_name
[t
], zio_taskq_types
[q
]);
744 spa
->spa_zio_taskq
[t
][q
] =
745 spa_taskq_create(spa
, name
, mode
, value
);
750 #if defined(_KERNEL) && defined(HAVE_SPA_THREAD)
752 spa_thread(void *arg
)
757 user_t
*pu
= PTOU(curproc
);
759 CALLB_CPR_INIT(&cprinfo
, &spa
->spa_proc_lock
, callb_generic_cpr
,
762 ASSERT(curproc
!= &p0
);
763 (void) snprintf(pu
->u_psargs
, sizeof (pu
->u_psargs
),
764 "zpool-%s", spa
->spa_name
);
765 (void) strlcpy(pu
->u_comm
, pu
->u_psargs
, sizeof (pu
->u_comm
));
767 /* bind this thread to the requested psrset */
768 if (zio_taskq_psrset_bind
!= PS_NONE
) {
770 mutex_enter(&cpu_lock
);
771 mutex_enter(&pidlock
);
772 mutex_enter(&curproc
->p_lock
);
774 if (cpupart_bind_thread(curthread
, zio_taskq_psrset_bind
,
775 0, NULL
, NULL
) == 0) {
776 curthread
->t_bind_pset
= zio_taskq_psrset_bind
;
779 "Couldn't bind process for zfs pool \"%s\" to "
780 "pset %d\n", spa
->spa_name
, zio_taskq_psrset_bind
);
783 mutex_exit(&curproc
->p_lock
);
784 mutex_exit(&pidlock
);
785 mutex_exit(&cpu_lock
);
789 if (zio_taskq_sysdc
) {
790 sysdc_thread_enter(curthread
, 100, 0);
793 spa
->spa_proc
= curproc
;
794 spa
->spa_did
= curthread
->t_did
;
796 spa_create_zio_taskqs(spa
);
798 mutex_enter(&spa
->spa_proc_lock
);
799 ASSERT(spa
->spa_proc_state
== SPA_PROC_CREATED
);
801 spa
->spa_proc_state
= SPA_PROC_ACTIVE
;
802 cv_broadcast(&spa
->spa_proc_cv
);
804 CALLB_CPR_SAFE_BEGIN(&cprinfo
);
805 while (spa
->spa_proc_state
== SPA_PROC_ACTIVE
)
806 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
807 CALLB_CPR_SAFE_END(&cprinfo
, &spa
->spa_proc_lock
);
809 ASSERT(spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
);
810 spa
->spa_proc_state
= SPA_PROC_GONE
;
812 cv_broadcast(&spa
->spa_proc_cv
);
813 CALLB_CPR_EXIT(&cprinfo
); /* drops spa_proc_lock */
815 mutex_enter(&curproc
->p_lock
);
821 * Activate an uninitialized pool.
824 spa_activate(spa_t
*spa
, int mode
)
826 ASSERT(spa
->spa_state
== POOL_STATE_UNINITIALIZED
);
828 spa
->spa_state
= POOL_STATE_ACTIVE
;
829 spa
->spa_mode
= mode
;
831 spa
->spa_normal_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
832 spa
->spa_log_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
834 /* Try to create a covering process */
835 mutex_enter(&spa
->spa_proc_lock
);
836 ASSERT(spa
->spa_proc_state
== SPA_PROC_NONE
);
837 ASSERT(spa
->spa_proc
== &p0
);
840 #ifdef HAVE_SPA_THREAD
841 /* Only create a process if we're going to be around a while. */
842 if (spa_create_process
&& strcmp(spa
->spa_name
, TRYIMPORT_NAME
) != 0) {
843 if (newproc(spa_thread
, (caddr_t
)spa
, syscid
, maxclsyspri
,
845 spa
->spa_proc_state
= SPA_PROC_CREATED
;
846 while (spa
->spa_proc_state
== SPA_PROC_CREATED
) {
847 cv_wait(&spa
->spa_proc_cv
,
848 &spa
->spa_proc_lock
);
850 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
851 ASSERT(spa
->spa_proc
!= &p0
);
852 ASSERT(spa
->spa_did
!= 0);
856 "Couldn't create process for zfs pool \"%s\"\n",
861 #endif /* HAVE_SPA_THREAD */
862 mutex_exit(&spa
->spa_proc_lock
);
864 /* If we didn't create a process, we need to create our taskqs. */
865 if (spa
->spa_proc
== &p0
) {
866 spa_create_zio_taskqs(spa
);
869 list_create(&spa
->spa_config_dirty_list
, sizeof (vdev_t
),
870 offsetof(vdev_t
, vdev_config_dirty_node
));
871 list_create(&spa
->spa_state_dirty_list
, sizeof (vdev_t
),
872 offsetof(vdev_t
, vdev_state_dirty_node
));
874 txg_list_create(&spa
->spa_vdev_txg_list
,
875 offsetof(struct vdev
, vdev_txg_node
));
877 avl_create(&spa
->spa_errlist_scrub
,
878 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
879 offsetof(spa_error_entry_t
, se_avl
));
880 avl_create(&spa
->spa_errlist_last
,
881 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
882 offsetof(spa_error_entry_t
, se_avl
));
886 * Opposite of spa_activate().
889 spa_deactivate(spa_t
*spa
)
893 ASSERT(spa
->spa_sync_on
== B_FALSE
);
894 ASSERT(spa
->spa_dsl_pool
== NULL
);
895 ASSERT(spa
->spa_root_vdev
== NULL
);
896 ASSERT(spa
->spa_async_zio_root
== NULL
);
897 ASSERT(spa
->spa_state
!= POOL_STATE_UNINITIALIZED
);
899 txg_list_destroy(&spa
->spa_vdev_txg_list
);
901 list_destroy(&spa
->spa_config_dirty_list
);
902 list_destroy(&spa
->spa_state_dirty_list
);
904 for (t
= 0; t
< ZIO_TYPES
; t
++) {
905 for (q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
906 if (spa
->spa_zio_taskq
[t
][q
] != NULL
)
907 taskq_destroy(spa
->spa_zio_taskq
[t
][q
]);
908 spa
->spa_zio_taskq
[t
][q
] = NULL
;
912 metaslab_class_destroy(spa
->spa_normal_class
);
913 spa
->spa_normal_class
= NULL
;
915 metaslab_class_destroy(spa
->spa_log_class
);
916 spa
->spa_log_class
= NULL
;
919 * If this was part of an import or the open otherwise failed, we may
920 * still have errors left in the queues. Empty them just in case.
922 spa_errlog_drain(spa
);
924 avl_destroy(&spa
->spa_errlist_scrub
);
925 avl_destroy(&spa
->spa_errlist_last
);
927 spa
->spa_state
= POOL_STATE_UNINITIALIZED
;
929 mutex_enter(&spa
->spa_proc_lock
);
930 if (spa
->spa_proc_state
!= SPA_PROC_NONE
) {
931 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
932 spa
->spa_proc_state
= SPA_PROC_DEACTIVATE
;
933 cv_broadcast(&spa
->spa_proc_cv
);
934 while (spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
) {
935 ASSERT(spa
->spa_proc
!= &p0
);
936 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
938 ASSERT(spa
->spa_proc_state
== SPA_PROC_GONE
);
939 spa
->spa_proc_state
= SPA_PROC_NONE
;
941 ASSERT(spa
->spa_proc
== &p0
);
942 mutex_exit(&spa
->spa_proc_lock
);
945 * We want to make sure spa_thread() has actually exited the ZFS
946 * module, so that the module can't be unloaded out from underneath
949 if (spa
->spa_did
!= 0) {
950 thread_join(spa
->spa_did
);
956 * Verify a pool configuration, and construct the vdev tree appropriately. This
957 * will create all the necessary vdevs in the appropriate layout, with each vdev
958 * in the CLOSED state. This will prep the pool before open/creation/import.
959 * All vdev validation is done by the vdev_alloc() routine.
962 spa_config_parse(spa_t
*spa
, vdev_t
**vdp
, nvlist_t
*nv
, vdev_t
*parent
,
963 uint_t id
, int atype
)
970 if ((error
= vdev_alloc(spa
, vdp
, nv
, parent
, id
, atype
)) != 0)
973 if ((*vdp
)->vdev_ops
->vdev_op_leaf
)
976 error
= nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
988 for (c
= 0; c
< children
; c
++) {
990 if ((error
= spa_config_parse(spa
, &vd
, child
[c
], *vdp
, c
,
998 ASSERT(*vdp
!= NULL
);
1004 * Opposite of spa_load().
1007 spa_unload(spa_t
*spa
)
1011 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1016 spa_async_suspend(spa
);
1021 if (spa
->spa_sync_on
) {
1022 txg_sync_stop(spa
->spa_dsl_pool
);
1023 spa
->spa_sync_on
= B_FALSE
;
1027 * Wait for any outstanding async I/O to complete.
1029 if (spa
->spa_async_zio_root
!= NULL
) {
1030 (void) zio_wait(spa
->spa_async_zio_root
);
1031 spa
->spa_async_zio_root
= NULL
;
1034 bpobj_close(&spa
->spa_deferred_bpobj
);
1037 * Close the dsl pool.
1039 if (spa
->spa_dsl_pool
) {
1040 dsl_pool_close(spa
->spa_dsl_pool
);
1041 spa
->spa_dsl_pool
= NULL
;
1042 spa
->spa_meta_objset
= NULL
;
1047 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1050 * Drop and purge level 2 cache
1052 spa_l2cache_drop(spa
);
1057 if (spa
->spa_root_vdev
)
1058 vdev_free(spa
->spa_root_vdev
);
1059 ASSERT(spa
->spa_root_vdev
== NULL
);
1061 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1062 vdev_free(spa
->spa_spares
.sav_vdevs
[i
]);
1063 if (spa
->spa_spares
.sav_vdevs
) {
1064 kmem_free(spa
->spa_spares
.sav_vdevs
,
1065 spa
->spa_spares
.sav_count
* sizeof (void *));
1066 spa
->spa_spares
.sav_vdevs
= NULL
;
1068 if (spa
->spa_spares
.sav_config
) {
1069 nvlist_free(spa
->spa_spares
.sav_config
);
1070 spa
->spa_spares
.sav_config
= NULL
;
1072 spa
->spa_spares
.sav_count
= 0;
1074 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
1075 vdev_clear_stats(spa
->spa_l2cache
.sav_vdevs
[i
]);
1076 vdev_free(spa
->spa_l2cache
.sav_vdevs
[i
]);
1078 if (spa
->spa_l2cache
.sav_vdevs
) {
1079 kmem_free(spa
->spa_l2cache
.sav_vdevs
,
1080 spa
->spa_l2cache
.sav_count
* sizeof (void *));
1081 spa
->spa_l2cache
.sav_vdevs
= NULL
;
1083 if (spa
->spa_l2cache
.sav_config
) {
1084 nvlist_free(spa
->spa_l2cache
.sav_config
);
1085 spa
->spa_l2cache
.sav_config
= NULL
;
1087 spa
->spa_l2cache
.sav_count
= 0;
1089 spa
->spa_async_suspended
= 0;
1091 if (spa
->spa_comment
!= NULL
) {
1092 spa_strfree(spa
->spa_comment
);
1093 spa
->spa_comment
= NULL
;
1096 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1100 * Load (or re-load) the current list of vdevs describing the active spares for
1101 * this pool. When this is called, we have some form of basic information in
1102 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1103 * then re-generate a more complete list including status information.
1106 spa_load_spares(spa_t
*spa
)
1113 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1116 * First, close and free any existing spare vdevs.
1118 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1119 vd
= spa
->spa_spares
.sav_vdevs
[i
];
1121 /* Undo the call to spa_activate() below */
1122 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1123 B_FALSE
)) != NULL
&& tvd
->vdev_isspare
)
1124 spa_spare_remove(tvd
);
1129 if (spa
->spa_spares
.sav_vdevs
)
1130 kmem_free(spa
->spa_spares
.sav_vdevs
,
1131 spa
->spa_spares
.sav_count
* sizeof (void *));
1133 if (spa
->spa_spares
.sav_config
== NULL
)
1136 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
1137 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
1139 spa
->spa_spares
.sav_count
= (int)nspares
;
1140 spa
->spa_spares
.sav_vdevs
= NULL
;
1146 * Construct the array of vdevs, opening them to get status in the
1147 * process. For each spare, there is potentially two different vdev_t
1148 * structures associated with it: one in the list of spares (used only
1149 * for basic validation purposes) and one in the active vdev
1150 * configuration (if it's spared in). During this phase we open and
1151 * validate each vdev on the spare list. If the vdev also exists in the
1152 * active configuration, then we also mark this vdev as an active spare.
1154 spa
->spa_spares
.sav_vdevs
= kmem_alloc(nspares
* sizeof (void *),
1156 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1157 VERIFY(spa_config_parse(spa
, &vd
, spares
[i
], NULL
, 0,
1158 VDEV_ALLOC_SPARE
) == 0);
1161 spa
->spa_spares
.sav_vdevs
[i
] = vd
;
1163 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1164 B_FALSE
)) != NULL
) {
1165 if (!tvd
->vdev_isspare
)
1169 * We only mark the spare active if we were successfully
1170 * able to load the vdev. Otherwise, importing a pool
1171 * with a bad active spare would result in strange
1172 * behavior, because multiple pool would think the spare
1173 * is actively in use.
1175 * There is a vulnerability here to an equally bizarre
1176 * circumstance, where a dead active spare is later
1177 * brought back to life (onlined or otherwise). Given
1178 * the rarity of this scenario, and the extra complexity
1179 * it adds, we ignore the possibility.
1181 if (!vdev_is_dead(tvd
))
1182 spa_spare_activate(tvd
);
1186 vd
->vdev_aux
= &spa
->spa_spares
;
1188 if (vdev_open(vd
) != 0)
1191 if (vdev_validate_aux(vd
) == 0)
1196 * Recompute the stashed list of spares, with status information
1199 VERIFY(nvlist_remove(spa
->spa_spares
.sav_config
, ZPOOL_CONFIG_SPARES
,
1200 DATA_TYPE_NVLIST_ARRAY
) == 0);
1202 spares
= kmem_alloc(spa
->spa_spares
.sav_count
* sizeof (void *),
1204 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1205 spares
[i
] = vdev_config_generate(spa
,
1206 spa
->spa_spares
.sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_SPARE
);
1207 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
1208 ZPOOL_CONFIG_SPARES
, spares
, spa
->spa_spares
.sav_count
) == 0);
1209 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1210 nvlist_free(spares
[i
]);
1211 kmem_free(spares
, spa
->spa_spares
.sav_count
* sizeof (void *));
1215 * Load (or re-load) the current list of vdevs describing the active l2cache for
1216 * this pool. When this is called, we have some form of basic information in
1217 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1218 * then re-generate a more complete list including status information.
1219 * Devices which are already active have their details maintained, and are
1223 spa_load_l2cache(spa_t
*spa
)
1227 int i
, j
, oldnvdevs
;
1229 vdev_t
*vd
, **oldvdevs
, **newvdevs
= NULL
;
1230 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
1232 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1234 if (sav
->sav_config
!= NULL
) {
1235 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
,
1236 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
1237 newvdevs
= kmem_alloc(nl2cache
* sizeof (void *), KM_PUSHPAGE
);
1242 oldvdevs
= sav
->sav_vdevs
;
1243 oldnvdevs
= sav
->sav_count
;
1244 sav
->sav_vdevs
= NULL
;
1248 * Process new nvlist of vdevs.
1250 for (i
= 0; i
< nl2cache
; i
++) {
1251 VERIFY(nvlist_lookup_uint64(l2cache
[i
], ZPOOL_CONFIG_GUID
,
1255 for (j
= 0; j
< oldnvdevs
; j
++) {
1257 if (vd
!= NULL
&& guid
== vd
->vdev_guid
) {
1259 * Retain previous vdev for add/remove ops.
1267 if (newvdevs
[i
] == NULL
) {
1271 VERIFY(spa_config_parse(spa
, &vd
, l2cache
[i
], NULL
, 0,
1272 VDEV_ALLOC_L2CACHE
) == 0);
1277 * Commit this vdev as an l2cache device,
1278 * even if it fails to open.
1280 spa_l2cache_add(vd
);
1285 spa_l2cache_activate(vd
);
1287 if (vdev_open(vd
) != 0)
1290 (void) vdev_validate_aux(vd
);
1292 if (!vdev_is_dead(vd
))
1293 l2arc_add_vdev(spa
, vd
);
1298 * Purge vdevs that were dropped
1300 for (i
= 0; i
< oldnvdevs
; i
++) {
1305 ASSERT(vd
->vdev_isl2cache
);
1307 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
1308 pool
!= 0ULL && l2arc_vdev_present(vd
))
1309 l2arc_remove_vdev(vd
);
1310 vdev_clear_stats(vd
);
1316 kmem_free(oldvdevs
, oldnvdevs
* sizeof (void *));
1318 if (sav
->sav_config
== NULL
)
1321 sav
->sav_vdevs
= newvdevs
;
1322 sav
->sav_count
= (int)nl2cache
;
1325 * Recompute the stashed list of l2cache devices, with status
1326 * information this time.
1328 VERIFY(nvlist_remove(sav
->sav_config
, ZPOOL_CONFIG_L2CACHE
,
1329 DATA_TYPE_NVLIST_ARRAY
) == 0);
1331 l2cache
= kmem_alloc(sav
->sav_count
* sizeof (void *), KM_PUSHPAGE
);
1332 for (i
= 0; i
< sav
->sav_count
; i
++)
1333 l2cache
[i
] = vdev_config_generate(spa
,
1334 sav
->sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_L2CACHE
);
1335 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
,
1336 ZPOOL_CONFIG_L2CACHE
, l2cache
, sav
->sav_count
) == 0);
1338 for (i
= 0; i
< sav
->sav_count
; i
++)
1339 nvlist_free(l2cache
[i
]);
1341 kmem_free(l2cache
, sav
->sav_count
* sizeof (void *));
1345 load_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
**value
)
1348 char *packed
= NULL
;
1353 VERIFY(0 == dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
));
1354 nvsize
= *(uint64_t *)db
->db_data
;
1355 dmu_buf_rele(db
, FTAG
);
1357 packed
= kmem_alloc(nvsize
, KM_PUSHPAGE
| KM_NODEBUG
);
1358 error
= dmu_read(spa
->spa_meta_objset
, obj
, 0, nvsize
, packed
,
1361 error
= nvlist_unpack(packed
, nvsize
, value
, 0);
1362 kmem_free(packed
, nvsize
);
1368 * Checks to see if the given vdev could not be opened, in which case we post a
1369 * sysevent to notify the autoreplace code that the device has been removed.
1372 spa_check_removed(vdev_t
*vd
)
1376 for (c
= 0; c
< vd
->vdev_children
; c
++)
1377 spa_check_removed(vd
->vdev_child
[c
]);
1379 if (vd
->vdev_ops
->vdev_op_leaf
&& vdev_is_dead(vd
)) {
1380 zfs_ereport_post(FM_EREPORT_RESOURCE_AUTOREPLACE
,
1381 vd
->vdev_spa
, vd
, NULL
, 0, 0);
1382 spa_event_notify(vd
->vdev_spa
, vd
, FM_EREPORT_ZFS_DEVICE_CHECK
);
1387 * Validate the current config against the MOS config
1390 spa_config_valid(spa_t
*spa
, nvlist_t
*config
)
1392 vdev_t
*mrvd
, *rvd
= spa
->spa_root_vdev
;
1396 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nv
) == 0);
1398 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1399 VERIFY(spa_config_parse(spa
, &mrvd
, nv
, NULL
, 0, VDEV_ALLOC_LOAD
) == 0);
1401 ASSERT3U(rvd
->vdev_children
, ==, mrvd
->vdev_children
);
1404 * If we're doing a normal import, then build up any additional
1405 * diagnostic information about missing devices in this config.
1406 * We'll pass this up to the user for further processing.
1408 if (!(spa
->spa_import_flags
& ZFS_IMPORT_MISSING_LOG
)) {
1409 nvlist_t
**child
, *nv
;
1412 child
= kmem_alloc(rvd
->vdev_children
* sizeof (nvlist_t
**),
1414 VERIFY(nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_PUSHPAGE
) == 0);
1416 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1417 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1418 vdev_t
*mtvd
= mrvd
->vdev_child
[c
];
1420 if (tvd
->vdev_ops
== &vdev_missing_ops
&&
1421 mtvd
->vdev_ops
!= &vdev_missing_ops
&&
1423 child
[idx
++] = vdev_config_generate(spa
, mtvd
,
1428 VERIFY(nvlist_add_nvlist_array(nv
,
1429 ZPOOL_CONFIG_CHILDREN
, child
, idx
) == 0);
1430 VERIFY(nvlist_add_nvlist(spa
->spa_load_info
,
1431 ZPOOL_CONFIG_MISSING_DEVICES
, nv
) == 0);
1433 for (i
= 0; i
< idx
; i
++)
1434 nvlist_free(child
[i
]);
1437 kmem_free(child
, rvd
->vdev_children
* sizeof (char **));
1441 * Compare the root vdev tree with the information we have
1442 * from the MOS config (mrvd). Check each top-level vdev
1443 * with the corresponding MOS config top-level (mtvd).
1445 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1446 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1447 vdev_t
*mtvd
= mrvd
->vdev_child
[c
];
1450 * Resolve any "missing" vdevs in the current configuration.
1451 * If we find that the MOS config has more accurate information
1452 * about the top-level vdev then use that vdev instead.
1454 if (tvd
->vdev_ops
== &vdev_missing_ops
&&
1455 mtvd
->vdev_ops
!= &vdev_missing_ops
) {
1457 if (!(spa
->spa_import_flags
& ZFS_IMPORT_MISSING_LOG
))
1461 * Device specific actions.
1463 if (mtvd
->vdev_islog
) {
1464 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
1467 * XXX - once we have 'readonly' pool
1468 * support we should be able to handle
1469 * missing data devices by transitioning
1470 * the pool to readonly.
1476 * Swap the missing vdev with the data we were
1477 * able to obtain from the MOS config.
1479 vdev_remove_child(rvd
, tvd
);
1480 vdev_remove_child(mrvd
, mtvd
);
1482 vdev_add_child(rvd
, mtvd
);
1483 vdev_add_child(mrvd
, tvd
);
1485 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1487 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1490 } else if (mtvd
->vdev_islog
) {
1492 * Load the slog device's state from the MOS config
1493 * since it's possible that the label does not
1494 * contain the most up-to-date information.
1496 vdev_load_log_state(tvd
, mtvd
);
1501 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1504 * Ensure we were able to validate the config.
1506 return (rvd
->vdev_guid_sum
== spa
->spa_uberblock
.ub_guid_sum
);
1510 * Check for missing log devices
1513 spa_check_logs(spa_t
*spa
)
1515 switch (spa
->spa_log_state
) {
1518 case SPA_LOG_MISSING
:
1519 /* need to recheck in case slog has been restored */
1520 case SPA_LOG_UNKNOWN
:
1521 if (dmu_objset_find(spa
->spa_name
, zil_check_log_chain
, NULL
,
1522 DS_FIND_CHILDREN
)) {
1523 spa_set_log_state(spa
, SPA_LOG_MISSING
);
1532 spa_passivate_log(spa_t
*spa
)
1534 vdev_t
*rvd
= spa
->spa_root_vdev
;
1535 boolean_t slog_found
= B_FALSE
;
1538 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
1540 if (!spa_has_slogs(spa
))
1543 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1544 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1545 metaslab_group_t
*mg
= tvd
->vdev_mg
;
1547 if (tvd
->vdev_islog
) {
1548 metaslab_group_passivate(mg
);
1549 slog_found
= B_TRUE
;
1553 return (slog_found
);
1557 spa_activate_log(spa_t
*spa
)
1559 vdev_t
*rvd
= spa
->spa_root_vdev
;
1562 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
1564 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1565 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1566 metaslab_group_t
*mg
= tvd
->vdev_mg
;
1568 if (tvd
->vdev_islog
)
1569 metaslab_group_activate(mg
);
1574 spa_offline_log(spa_t
*spa
)
1578 if ((error
= dmu_objset_find(spa_name(spa
), zil_vdev_offline
,
1579 NULL
, DS_FIND_CHILDREN
)) == 0) {
1582 * We successfully offlined the log device, sync out the
1583 * current txg so that the "stubby" block can be removed
1586 txg_wait_synced(spa
->spa_dsl_pool
, 0);
1592 spa_aux_check_removed(spa_aux_vdev_t
*sav
)
1596 for (i
= 0; i
< sav
->sav_count
; i
++)
1597 spa_check_removed(sav
->sav_vdevs
[i
]);
1601 spa_claim_notify(zio_t
*zio
)
1603 spa_t
*spa
= zio
->io_spa
;
1608 mutex_enter(&spa
->spa_props_lock
); /* any mutex will do */
1609 if (spa
->spa_claim_max_txg
< zio
->io_bp
->blk_birth
)
1610 spa
->spa_claim_max_txg
= zio
->io_bp
->blk_birth
;
1611 mutex_exit(&spa
->spa_props_lock
);
1614 typedef struct spa_load_error
{
1615 uint64_t sle_meta_count
;
1616 uint64_t sle_data_count
;
1620 spa_load_verify_done(zio_t
*zio
)
1622 blkptr_t
*bp
= zio
->io_bp
;
1623 spa_load_error_t
*sle
= zio
->io_private
;
1624 dmu_object_type_t type
= BP_GET_TYPE(bp
);
1625 int error
= zio
->io_error
;
1628 if ((BP_GET_LEVEL(bp
) != 0 || dmu_ot
[type
].ot_metadata
) &&
1629 type
!= DMU_OT_INTENT_LOG
)
1630 atomic_add_64(&sle
->sle_meta_count
, 1);
1632 atomic_add_64(&sle
->sle_data_count
, 1);
1634 zio_data_buf_free(zio
->io_data
, zio
->io_size
);
1639 spa_load_verify_cb(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
1640 arc_buf_t
*pbuf
, const zbookmark_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
1644 size_t size
= BP_GET_PSIZE(bp
);
1645 void *data
= zio_data_buf_alloc(size
);
1647 zio_nowait(zio_read(rio
, spa
, bp
, data
, size
,
1648 spa_load_verify_done
, rio
->io_private
, ZIO_PRIORITY_SCRUB
,
1649 ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_CANFAIL
|
1650 ZIO_FLAG_SCRUB
| ZIO_FLAG_RAW
, zb
));
1656 spa_load_verify(spa_t
*spa
)
1659 spa_load_error_t sle
= { 0 };
1660 zpool_rewind_policy_t policy
;
1661 boolean_t verify_ok
= B_FALSE
;
1664 zpool_get_rewind_policy(spa
->spa_config
, &policy
);
1666 if (policy
.zrp_request
& ZPOOL_NEVER_REWIND
)
1669 rio
= zio_root(spa
, NULL
, &sle
,
1670 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
);
1672 error
= traverse_pool(spa
, spa
->spa_verify_min_txg
,
1673 TRAVERSE_PRE
| TRAVERSE_PREFETCH
, spa_load_verify_cb
, rio
);
1675 (void) zio_wait(rio
);
1677 spa
->spa_load_meta_errors
= sle
.sle_meta_count
;
1678 spa
->spa_load_data_errors
= sle
.sle_data_count
;
1680 if (!error
&& sle
.sle_meta_count
<= policy
.zrp_maxmeta
&&
1681 sle
.sle_data_count
<= policy
.zrp_maxdata
) {
1685 spa
->spa_load_txg
= spa
->spa_uberblock
.ub_txg
;
1686 spa
->spa_load_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
1688 loss
= spa
->spa_last_ubsync_txg_ts
- spa
->spa_load_txg_ts
;
1689 VERIFY(nvlist_add_uint64(spa
->spa_load_info
,
1690 ZPOOL_CONFIG_LOAD_TIME
, spa
->spa_load_txg_ts
) == 0);
1691 VERIFY(nvlist_add_int64(spa
->spa_load_info
,
1692 ZPOOL_CONFIG_REWIND_TIME
, loss
) == 0);
1693 VERIFY(nvlist_add_uint64(spa
->spa_load_info
,
1694 ZPOOL_CONFIG_LOAD_DATA_ERRORS
, sle
.sle_data_count
) == 0);
1696 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
;
1700 if (error
!= ENXIO
&& error
!= EIO
)
1705 return (verify_ok
? 0 : EIO
);
1709 * Find a value in the pool props object.
1712 spa_prop_find(spa_t
*spa
, zpool_prop_t prop
, uint64_t *val
)
1714 (void) zap_lookup(spa
->spa_meta_objset
, spa
->spa_pool_props_object
,
1715 zpool_prop_to_name(prop
), sizeof (uint64_t), 1, val
);
1719 * Find a value in the pool directory object.
1722 spa_dir_prop(spa_t
*spa
, const char *name
, uint64_t *val
)
1724 return (zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
1725 name
, sizeof (uint64_t), 1, val
));
1729 spa_vdev_err(vdev_t
*vdev
, vdev_aux_t aux
, int err
)
1731 vdev_set_state(vdev
, B_TRUE
, VDEV_STATE_CANT_OPEN
, aux
);
1736 * Fix up config after a partly-completed split. This is done with the
1737 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
1738 * pool have that entry in their config, but only the splitting one contains
1739 * a list of all the guids of the vdevs that are being split off.
1741 * This function determines what to do with that list: either rejoin
1742 * all the disks to the pool, or complete the splitting process. To attempt
1743 * the rejoin, each disk that is offlined is marked online again, and
1744 * we do a reopen() call. If the vdev label for every disk that was
1745 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
1746 * then we call vdev_split() on each disk, and complete the split.
1748 * Otherwise we leave the config alone, with all the vdevs in place in
1749 * the original pool.
1752 spa_try_repair(spa_t
*spa
, nvlist_t
*config
)
1759 boolean_t attempt_reopen
;
1761 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
, &nvl
) != 0)
1764 /* check that the config is complete */
1765 if (nvlist_lookup_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
1766 &glist
, &gcount
) != 0)
1769 vd
= kmem_zalloc(gcount
* sizeof (vdev_t
*), KM_PUSHPAGE
);
1771 /* attempt to online all the vdevs & validate */
1772 attempt_reopen
= B_TRUE
;
1773 for (i
= 0; i
< gcount
; i
++) {
1774 if (glist
[i
] == 0) /* vdev is hole */
1777 vd
[i
] = spa_lookup_by_guid(spa
, glist
[i
], B_FALSE
);
1778 if (vd
[i
] == NULL
) {
1780 * Don't bother attempting to reopen the disks;
1781 * just do the split.
1783 attempt_reopen
= B_FALSE
;
1785 /* attempt to re-online it */
1786 vd
[i
]->vdev_offline
= B_FALSE
;
1790 if (attempt_reopen
) {
1791 vdev_reopen(spa
->spa_root_vdev
);
1793 /* check each device to see what state it's in */
1794 for (extracted
= 0, i
= 0; i
< gcount
; i
++) {
1795 if (vd
[i
] != NULL
&&
1796 vd
[i
]->vdev_stat
.vs_aux
!= VDEV_AUX_SPLIT_POOL
)
1803 * If every disk has been moved to the new pool, or if we never
1804 * even attempted to look at them, then we split them off for
1807 if (!attempt_reopen
|| gcount
== extracted
) {
1808 for (i
= 0; i
< gcount
; i
++)
1811 vdev_reopen(spa
->spa_root_vdev
);
1814 kmem_free(vd
, gcount
* sizeof (vdev_t
*));
1818 spa_load(spa_t
*spa
, spa_load_state_t state
, spa_import_type_t type
,
1819 boolean_t mosconfig
)
1821 nvlist_t
*config
= spa
->spa_config
;
1822 char *ereport
= FM_EREPORT_ZFS_POOL
;
1828 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
, &pool_guid
))
1831 ASSERT(spa
->spa_comment
== NULL
);
1832 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
1833 spa
->spa_comment
= spa_strdup(comment
);
1836 * Versioning wasn't explicitly added to the label until later, so if
1837 * it's not present treat it as the initial version.
1839 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
1840 &spa
->spa_ubsync
.ub_version
) != 0)
1841 spa
->spa_ubsync
.ub_version
= SPA_VERSION_INITIAL
;
1843 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
1844 &spa
->spa_config_txg
);
1846 if ((state
== SPA_LOAD_IMPORT
|| state
== SPA_LOAD_TRYIMPORT
) &&
1847 spa_guid_exists(pool_guid
, 0)) {
1850 spa
->spa_config_guid
= pool_guid
;
1852 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
,
1854 VERIFY(nvlist_dup(nvl
, &spa
->spa_config_splitting
,
1858 gethrestime(&spa
->spa_loaded_ts
);
1859 error
= spa_load_impl(spa
, pool_guid
, config
, state
, type
,
1860 mosconfig
, &ereport
);
1863 spa
->spa_minref
= refcount_count(&spa
->spa_refcount
);
1865 if (error
!= EEXIST
) {
1866 spa
->spa_loaded_ts
.tv_sec
= 0;
1867 spa
->spa_loaded_ts
.tv_nsec
= 0;
1869 if (error
!= EBADF
) {
1870 zfs_ereport_post(ereport
, spa
, NULL
, NULL
, 0, 0);
1873 spa
->spa_load_state
= error
? SPA_LOAD_ERROR
: SPA_LOAD_NONE
;
1880 * Load an existing storage pool, using the pool's builtin spa_config as a
1881 * source of configuration information.
1883 __attribute__((always_inline
))
1885 spa_load_impl(spa_t
*spa
, uint64_t pool_guid
, nvlist_t
*config
,
1886 spa_load_state_t state
, spa_import_type_t type
, boolean_t mosconfig
,
1890 nvlist_t
*nvroot
= NULL
;
1892 uberblock_t
*ub
= &spa
->spa_uberblock
;
1893 uint64_t children
, config_cache_txg
= spa
->spa_config_txg
;
1894 int orig_mode
= spa
->spa_mode
;
1899 * If this is an untrusted config, access the pool in read-only mode.
1900 * This prevents things like resilvering recently removed devices.
1903 spa
->spa_mode
= FREAD
;
1905 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1907 spa
->spa_load_state
= state
;
1909 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvroot
))
1912 parse
= (type
== SPA_IMPORT_EXISTING
?
1913 VDEV_ALLOC_LOAD
: VDEV_ALLOC_SPLIT
);
1916 * Create "The Godfather" zio to hold all async IOs
1918 spa
->spa_async_zio_root
= zio_root(spa
, NULL
, NULL
,
1919 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_GODFATHER
);
1922 * Parse the configuration into a vdev tree. We explicitly set the
1923 * value that will be returned by spa_version() since parsing the
1924 * configuration requires knowing the version number.
1926 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1927 error
= spa_config_parse(spa
, &rvd
, nvroot
, NULL
, 0, parse
);
1928 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1933 ASSERT(spa
->spa_root_vdev
== rvd
);
1935 if (type
!= SPA_IMPORT_ASSEMBLE
) {
1936 ASSERT(spa_guid(spa
) == pool_guid
);
1940 * Try to open all vdevs, loading each label in the process.
1942 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1943 error
= vdev_open(rvd
);
1944 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1949 * We need to validate the vdev labels against the configuration that
1950 * we have in hand, which is dependent on the setting of mosconfig. If
1951 * mosconfig is true then we're validating the vdev labels based on
1952 * that config. Otherwise, we're validating against the cached config
1953 * (zpool.cache) that was read when we loaded the zfs module, and then
1954 * later we will recursively call spa_load() and validate against
1957 * If we're assembling a new pool that's been split off from an
1958 * existing pool, the labels haven't yet been updated so we skip
1959 * validation for now.
1961 if (type
!= SPA_IMPORT_ASSEMBLE
) {
1962 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1963 error
= vdev_validate(rvd
, mosconfig
);
1964 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1969 if (rvd
->vdev_state
<= VDEV_STATE_CANT_OPEN
)
1974 * Find the best uberblock.
1976 vdev_uberblock_load(NULL
, rvd
, ub
);
1979 * If we weren't able to find a single valid uberblock, return failure.
1981 if (ub
->ub_txg
== 0)
1982 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, ENXIO
));
1985 * If the pool is newer than the code, we can't open it.
1987 if (ub
->ub_version
> SPA_VERSION
)
1988 return (spa_vdev_err(rvd
, VDEV_AUX_VERSION_NEWER
, ENOTSUP
));
1991 * If the vdev guid sum doesn't match the uberblock, we have an
1992 * incomplete configuration. We first check to see if the pool
1993 * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN).
1994 * If it is, defer the vdev_guid_sum check till later so we
1995 * can handle missing vdevs.
1997 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VDEV_CHILDREN
,
1998 &children
) != 0 && mosconfig
&& type
!= SPA_IMPORT_ASSEMBLE
&&
1999 rvd
->vdev_guid_sum
!= ub
->ub_guid_sum
)
2000 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
, ENXIO
));
2002 if (type
!= SPA_IMPORT_ASSEMBLE
&& spa
->spa_config_splitting
) {
2003 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2004 spa_try_repair(spa
, config
);
2005 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2006 nvlist_free(spa
->spa_config_splitting
);
2007 spa
->spa_config_splitting
= NULL
;
2011 * Initialize internal SPA structures.
2013 spa
->spa_state
= POOL_STATE_ACTIVE
;
2014 spa
->spa_ubsync
= spa
->spa_uberblock
;
2015 spa
->spa_verify_min_txg
= spa
->spa_extreme_rewind
?
2016 TXG_INITIAL
- 1 : spa_last_synced_txg(spa
) - TXG_DEFER_SIZE
- 1;
2017 spa
->spa_first_txg
= spa
->spa_last_ubsync_txg
?
2018 spa
->spa_last_ubsync_txg
: spa_last_synced_txg(spa
) + 1;
2019 spa
->spa_claim_max_txg
= spa
->spa_first_txg
;
2020 spa
->spa_prev_software_version
= ub
->ub_software_version
;
2022 error
= dsl_pool_open(spa
, spa
->spa_first_txg
, &spa
->spa_dsl_pool
);
2024 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2025 spa
->spa_meta_objset
= spa
->spa_dsl_pool
->dp_meta_objset
;
2027 if (spa_dir_prop(spa
, DMU_POOL_CONFIG
, &spa
->spa_config_object
) != 0)
2028 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2032 nvlist_t
*policy
= NULL
, *nvconfig
;
2034 if (load_nvlist(spa
, spa
->spa_config_object
, &nvconfig
) != 0)
2035 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2037 if (!spa_is_root(spa
) && nvlist_lookup_uint64(nvconfig
,
2038 ZPOOL_CONFIG_HOSTID
, &hostid
) == 0) {
2040 unsigned long myhostid
= 0;
2042 VERIFY(nvlist_lookup_string(nvconfig
,
2043 ZPOOL_CONFIG_HOSTNAME
, &hostname
) == 0);
2046 myhostid
= zone_get_hostid(NULL
);
2049 * We're emulating the system's hostid in userland, so
2050 * we can't use zone_get_hostid().
2052 (void) ddi_strtoul(hw_serial
, NULL
, 10, &myhostid
);
2053 #endif /* _KERNEL */
2054 if (hostid
!= 0 && myhostid
!= 0 &&
2055 hostid
!= myhostid
) {
2056 nvlist_free(nvconfig
);
2057 cmn_err(CE_WARN
, "pool '%s' could not be "
2058 "loaded as it was last accessed by "
2059 "another system (host: %s hostid: 0x%lx). "
2060 "See: http://zfsonlinux.org/msg/ZFS-8000-EY",
2061 spa_name(spa
), hostname
,
2062 (unsigned long)hostid
);
2066 if (nvlist_lookup_nvlist(spa
->spa_config
,
2067 ZPOOL_REWIND_POLICY
, &policy
) == 0)
2068 VERIFY(nvlist_add_nvlist(nvconfig
,
2069 ZPOOL_REWIND_POLICY
, policy
) == 0);
2071 spa_config_set(spa
, nvconfig
);
2073 spa_deactivate(spa
);
2074 spa_activate(spa
, orig_mode
);
2076 return (spa_load(spa
, state
, SPA_IMPORT_EXISTING
, B_TRUE
));
2079 if (spa_dir_prop(spa
, DMU_POOL_SYNC_BPOBJ
, &obj
) != 0)
2080 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2081 error
= bpobj_open(&spa
->spa_deferred_bpobj
, spa
->spa_meta_objset
, obj
);
2083 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2086 * Load the bit that tells us to use the new accounting function
2087 * (raid-z deflation). If we have an older pool, this will not
2090 error
= spa_dir_prop(spa
, DMU_POOL_DEFLATE
, &spa
->spa_deflate
);
2091 if (error
!= 0 && error
!= ENOENT
)
2092 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2094 error
= spa_dir_prop(spa
, DMU_POOL_CREATION_VERSION
,
2095 &spa
->spa_creation_version
);
2096 if (error
!= 0 && error
!= ENOENT
)
2097 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2100 * Load the persistent error log. If we have an older pool, this will
2103 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_LAST
, &spa
->spa_errlog_last
);
2104 if (error
!= 0 && error
!= ENOENT
)
2105 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2107 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_SCRUB
,
2108 &spa
->spa_errlog_scrub
);
2109 if (error
!= 0 && error
!= ENOENT
)
2110 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2113 * Load the history object. If we have an older pool, this
2114 * will not be present.
2116 error
= spa_dir_prop(spa
, DMU_POOL_HISTORY
, &spa
->spa_history
);
2117 if (error
!= 0 && error
!= ENOENT
)
2118 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2121 * If we're assembling the pool from the split-off vdevs of
2122 * an existing pool, we don't want to attach the spares & cache
2127 * Load any hot spares for this pool.
2129 error
= spa_dir_prop(spa
, DMU_POOL_SPARES
, &spa
->spa_spares
.sav_object
);
2130 if (error
!= 0 && error
!= ENOENT
)
2131 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2132 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
2133 ASSERT(spa_version(spa
) >= SPA_VERSION_SPARES
);
2134 if (load_nvlist(spa
, spa
->spa_spares
.sav_object
,
2135 &spa
->spa_spares
.sav_config
) != 0)
2136 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2138 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2139 spa_load_spares(spa
);
2140 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2141 } else if (error
== 0) {
2142 spa
->spa_spares
.sav_sync
= B_TRUE
;
2146 * Load any level 2 ARC devices for this pool.
2148 error
= spa_dir_prop(spa
, DMU_POOL_L2CACHE
,
2149 &spa
->spa_l2cache
.sav_object
);
2150 if (error
!= 0 && error
!= ENOENT
)
2151 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2152 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
2153 ASSERT(spa_version(spa
) >= SPA_VERSION_L2CACHE
);
2154 if (load_nvlist(spa
, spa
->spa_l2cache
.sav_object
,
2155 &spa
->spa_l2cache
.sav_config
) != 0)
2156 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2158 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2159 spa_load_l2cache(spa
);
2160 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2161 } else if (error
== 0) {
2162 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
2165 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
2167 error
= spa_dir_prop(spa
, DMU_POOL_PROPS
, &spa
->spa_pool_props_object
);
2168 if (error
&& error
!= ENOENT
)
2169 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2172 uint64_t autoreplace
;
2174 spa_prop_find(spa
, ZPOOL_PROP_BOOTFS
, &spa
->spa_bootfs
);
2175 spa_prop_find(spa
, ZPOOL_PROP_AUTOREPLACE
, &autoreplace
);
2176 spa_prop_find(spa
, ZPOOL_PROP_DELEGATION
, &spa
->spa_delegation
);
2177 spa_prop_find(spa
, ZPOOL_PROP_FAILUREMODE
, &spa
->spa_failmode
);
2178 spa_prop_find(spa
, ZPOOL_PROP_AUTOEXPAND
, &spa
->spa_autoexpand
);
2179 spa_prop_find(spa
, ZPOOL_PROP_DEDUPDITTO
,
2180 &spa
->spa_dedup_ditto
);
2182 spa
->spa_autoreplace
= (autoreplace
!= 0);
2186 * If the 'autoreplace' property is set, then post a resource notifying
2187 * the ZFS DE that it should not issue any faults for unopenable
2188 * devices. We also iterate over the vdevs, and post a sysevent for any
2189 * unopenable vdevs so that the normal autoreplace handler can take
2192 if (spa
->spa_autoreplace
&& state
!= SPA_LOAD_TRYIMPORT
) {
2193 spa_check_removed(spa
->spa_root_vdev
);
2195 * For the import case, this is done in spa_import(), because
2196 * at this point we're using the spare definitions from
2197 * the MOS config, not necessarily from the userland config.
2199 if (state
!= SPA_LOAD_IMPORT
) {
2200 spa_aux_check_removed(&spa
->spa_spares
);
2201 spa_aux_check_removed(&spa
->spa_l2cache
);
2206 * Load the vdev state for all toplevel vdevs.
2211 * Propagate the leaf DTLs we just loaded all the way up the tree.
2213 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2214 vdev_dtl_reassess(rvd
, 0, 0, B_FALSE
);
2215 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2218 * Load the DDTs (dedup tables).
2220 error
= ddt_load(spa
);
2222 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2224 spa_update_dspace(spa
);
2227 * Validate the config, using the MOS config to fill in any
2228 * information which might be missing. If we fail to validate
2229 * the config then declare the pool unfit for use. If we're
2230 * assembling a pool from a split, the log is not transferred
2233 if (type
!= SPA_IMPORT_ASSEMBLE
) {
2236 if (load_nvlist(spa
, spa
->spa_config_object
, &nvconfig
) != 0)
2237 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2239 if (!spa_config_valid(spa
, nvconfig
)) {
2240 nvlist_free(nvconfig
);
2241 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
,
2244 nvlist_free(nvconfig
);
2247 * Now that we've validate the config, check the state of the
2248 * root vdev. If it can't be opened, it indicates one or
2249 * more toplevel vdevs are faulted.
2251 if (rvd
->vdev_state
<= VDEV_STATE_CANT_OPEN
)
2254 if (spa_check_logs(spa
)) {
2255 *ereport
= FM_EREPORT_ZFS_LOG_REPLAY
;
2256 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_LOG
, ENXIO
));
2261 * We've successfully opened the pool, verify that we're ready
2262 * to start pushing transactions.
2264 if (state
!= SPA_LOAD_TRYIMPORT
) {
2265 if ((error
= spa_load_verify(spa
)))
2266 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
2270 if (spa_writeable(spa
) && (state
== SPA_LOAD_RECOVER
||
2271 spa
->spa_load_max_txg
== UINT64_MAX
)) {
2273 int need_update
= B_FALSE
;
2276 ASSERT(state
!= SPA_LOAD_TRYIMPORT
);
2279 * Claim log blocks that haven't been committed yet.
2280 * This must all happen in a single txg.
2281 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
2282 * invoked from zil_claim_log_block()'s i/o done callback.
2283 * Price of rollback is that we abandon the log.
2285 spa
->spa_claiming
= B_TRUE
;
2287 tx
= dmu_tx_create_assigned(spa_get_dsl(spa
),
2288 spa_first_txg(spa
));
2289 (void) dmu_objset_find(spa_name(spa
),
2290 zil_claim
, tx
, DS_FIND_CHILDREN
);
2293 spa
->spa_claiming
= B_FALSE
;
2295 spa_set_log_state(spa
, SPA_LOG_GOOD
);
2296 spa
->spa_sync_on
= B_TRUE
;
2297 txg_sync_start(spa
->spa_dsl_pool
);
2300 * Wait for all claims to sync. We sync up to the highest
2301 * claimed log block birth time so that claimed log blocks
2302 * don't appear to be from the future. spa_claim_max_txg
2303 * will have been set for us by either zil_check_log_chain()
2304 * (invoked from spa_check_logs()) or zil_claim() above.
2306 txg_wait_synced(spa
->spa_dsl_pool
, spa
->spa_claim_max_txg
);
2309 * If the config cache is stale, or we have uninitialized
2310 * metaslabs (see spa_vdev_add()), then update the config.
2312 * If this is a verbatim import, trust the current
2313 * in-core spa_config and update the disk labels.
2315 if (config_cache_txg
!= spa
->spa_config_txg
||
2316 state
== SPA_LOAD_IMPORT
||
2317 state
== SPA_LOAD_RECOVER
||
2318 (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
))
2319 need_update
= B_TRUE
;
2321 for (c
= 0; c
< rvd
->vdev_children
; c
++)
2322 if (rvd
->vdev_child
[c
]->vdev_ms_array
== 0)
2323 need_update
= B_TRUE
;
2326 * Update the config cache asychronously in case we're the
2327 * root pool, in which case the config cache isn't writable yet.
2330 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
2333 * Check all DTLs to see if anything needs resilvering.
2335 if (!dsl_scan_resilvering(spa
->spa_dsl_pool
) &&
2336 vdev_resilver_needed(rvd
, NULL
, NULL
))
2337 spa_async_request(spa
, SPA_ASYNC_RESILVER
);
2340 * Delete any inconsistent datasets.
2342 (void) dmu_objset_find(spa_name(spa
),
2343 dsl_destroy_inconsistent
, NULL
, DS_FIND_CHILDREN
);
2346 * Clean up any stale temporary dataset userrefs.
2348 dsl_pool_clean_tmp_userrefs(spa
->spa_dsl_pool
);
2355 spa_load_retry(spa_t
*spa
, spa_load_state_t state
, int mosconfig
)
2357 int mode
= spa
->spa_mode
;
2360 spa_deactivate(spa
);
2362 spa
->spa_load_max_txg
--;
2364 spa_activate(spa
, mode
);
2365 spa_async_suspend(spa
);
2367 return (spa_load(spa
, state
, SPA_IMPORT_EXISTING
, mosconfig
));
2371 spa_load_best(spa_t
*spa
, spa_load_state_t state
, int mosconfig
,
2372 uint64_t max_request
, int rewind_flags
)
2374 nvlist_t
*config
= NULL
;
2375 int load_error
, rewind_error
;
2376 uint64_t safe_rewind_txg
;
2379 if (spa
->spa_load_txg
&& state
== SPA_LOAD_RECOVER
) {
2380 spa
->spa_load_max_txg
= spa
->spa_load_txg
;
2381 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
2383 spa
->spa_load_max_txg
= max_request
;
2386 load_error
= rewind_error
= spa_load(spa
, state
, SPA_IMPORT_EXISTING
,
2388 if (load_error
== 0)
2391 if (spa
->spa_root_vdev
!= NULL
)
2392 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
2394 spa
->spa_last_ubsync_txg
= spa
->spa_uberblock
.ub_txg
;
2395 spa
->spa_last_ubsync_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
2397 if (rewind_flags
& ZPOOL_NEVER_REWIND
) {
2398 nvlist_free(config
);
2399 return (load_error
);
2402 /* Price of rolling back is discarding txgs, including log */
2403 if (state
== SPA_LOAD_RECOVER
)
2404 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
2406 spa
->spa_load_max_txg
= spa
->spa_last_ubsync_txg
;
2407 safe_rewind_txg
= spa
->spa_last_ubsync_txg
- TXG_DEFER_SIZE
;
2408 min_txg
= (rewind_flags
& ZPOOL_EXTREME_REWIND
) ?
2409 TXG_INITIAL
: safe_rewind_txg
;
2412 * Continue as long as we're finding errors, we're still within
2413 * the acceptable rewind range, and we're still finding uberblocks
2415 while (rewind_error
&& spa
->spa_uberblock
.ub_txg
>= min_txg
&&
2416 spa
->spa_uberblock
.ub_txg
<= spa
->spa_load_max_txg
) {
2417 if (spa
->spa_load_max_txg
< safe_rewind_txg
)
2418 spa
->spa_extreme_rewind
= B_TRUE
;
2419 rewind_error
= spa_load_retry(spa
, state
, mosconfig
);
2422 spa
->spa_extreme_rewind
= B_FALSE
;
2423 spa
->spa_load_max_txg
= UINT64_MAX
;
2425 if (config
&& (rewind_error
|| state
!= SPA_LOAD_RECOVER
))
2426 spa_config_set(spa
, config
);
2428 return (state
== SPA_LOAD_RECOVER
? rewind_error
: load_error
);
2434 * The import case is identical to an open except that the configuration is sent
2435 * down from userland, instead of grabbed from the configuration cache. For the
2436 * case of an open, the pool configuration will exist in the
2437 * POOL_STATE_UNINITIALIZED state.
2439 * The stats information (gen/count/ustats) is used to gather vdev statistics at
2440 * the same time open the pool, without having to keep around the spa_t in some
2444 spa_open_common(const char *pool
, spa_t
**spapp
, void *tag
, nvlist_t
*nvpolicy
,
2448 spa_load_state_t state
= SPA_LOAD_OPEN
;
2450 int locked
= B_FALSE
;
2455 * As disgusting as this is, we need to support recursive calls to this
2456 * function because dsl_dir_open() is called during spa_load(), and ends
2457 * up calling spa_open() again. The real fix is to figure out how to
2458 * avoid dsl_dir_open() calling this in the first place.
2460 if (mutex_owner(&spa_namespace_lock
) != curthread
) {
2461 mutex_enter(&spa_namespace_lock
);
2465 if ((spa
= spa_lookup(pool
)) == NULL
) {
2467 mutex_exit(&spa_namespace_lock
);
2471 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
) {
2472 zpool_rewind_policy_t policy
;
2474 zpool_get_rewind_policy(nvpolicy
? nvpolicy
: spa
->spa_config
,
2476 if (policy
.zrp_request
& ZPOOL_DO_REWIND
)
2477 state
= SPA_LOAD_RECOVER
;
2479 spa_activate(spa
, spa_mode_global
);
2481 if (state
!= SPA_LOAD_RECOVER
)
2482 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
2484 error
= spa_load_best(spa
, state
, B_FALSE
, policy
.zrp_txg
,
2485 policy
.zrp_request
);
2487 if (error
== EBADF
) {
2489 * If vdev_validate() returns failure (indicated by
2490 * EBADF), it indicates that one of the vdevs indicates
2491 * that the pool has been exported or destroyed. If
2492 * this is the case, the config cache is out of sync and
2493 * we should remove the pool from the namespace.
2496 spa_deactivate(spa
);
2497 spa_config_sync(spa
, B_TRUE
, B_TRUE
);
2500 mutex_exit(&spa_namespace_lock
);
2506 * We can't open the pool, but we still have useful
2507 * information: the state of each vdev after the
2508 * attempted vdev_open(). Return this to the user.
2510 if (config
!= NULL
&& spa
->spa_config
) {
2511 VERIFY(nvlist_dup(spa
->spa_config
, config
,
2513 VERIFY(nvlist_add_nvlist(*config
,
2514 ZPOOL_CONFIG_LOAD_INFO
,
2515 spa
->spa_load_info
) == 0);
2518 spa_deactivate(spa
);
2519 spa
->spa_last_open_failed
= error
;
2521 mutex_exit(&spa_namespace_lock
);
2527 spa_open_ref(spa
, tag
);
2530 *config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
2533 * If we've recovered the pool, pass back any information we
2534 * gathered while doing the load.
2536 if (state
== SPA_LOAD_RECOVER
) {
2537 VERIFY(nvlist_add_nvlist(*config
, ZPOOL_CONFIG_LOAD_INFO
,
2538 spa
->spa_load_info
) == 0);
2542 spa
->spa_last_open_failed
= 0;
2543 spa
->spa_last_ubsync_txg
= 0;
2544 spa
->spa_load_txg
= 0;
2545 mutex_exit(&spa_namespace_lock
);
2554 spa_open_rewind(const char *name
, spa_t
**spapp
, void *tag
, nvlist_t
*policy
,
2557 return (spa_open_common(name
, spapp
, tag
, policy
, config
));
2561 spa_open(const char *name
, spa_t
**spapp
, void *tag
)
2563 return (spa_open_common(name
, spapp
, tag
, NULL
, NULL
));
2567 * Lookup the given spa_t, incrementing the inject count in the process,
2568 * preventing it from being exported or destroyed.
2571 spa_inject_addref(char *name
)
2575 mutex_enter(&spa_namespace_lock
);
2576 if ((spa
= spa_lookup(name
)) == NULL
) {
2577 mutex_exit(&spa_namespace_lock
);
2580 spa
->spa_inject_ref
++;
2581 mutex_exit(&spa_namespace_lock
);
2587 spa_inject_delref(spa_t
*spa
)
2589 mutex_enter(&spa_namespace_lock
);
2590 spa
->spa_inject_ref
--;
2591 mutex_exit(&spa_namespace_lock
);
2595 * Add spares device information to the nvlist.
2598 spa_add_spares(spa_t
*spa
, nvlist_t
*config
)
2608 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
2610 if (spa
->spa_spares
.sav_count
== 0)
2613 VERIFY(nvlist_lookup_nvlist(config
,
2614 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
2615 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
2616 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
2618 VERIFY(nvlist_add_nvlist_array(nvroot
,
2619 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
2620 VERIFY(nvlist_lookup_nvlist_array(nvroot
,
2621 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
2624 * Go through and find any spares which have since been
2625 * repurposed as an active spare. If this is the case, update
2626 * their status appropriately.
2628 for (i
= 0; i
< nspares
; i
++) {
2629 VERIFY(nvlist_lookup_uint64(spares
[i
],
2630 ZPOOL_CONFIG_GUID
, &guid
) == 0);
2631 if (spa_spare_exists(guid
, &pool
, NULL
) &&
2633 VERIFY(nvlist_lookup_uint64_array(
2634 spares
[i
], ZPOOL_CONFIG_VDEV_STATS
,
2635 (uint64_t **)&vs
, &vsc
) == 0);
2636 vs
->vs_state
= VDEV_STATE_CANT_OPEN
;
2637 vs
->vs_aux
= VDEV_AUX_SPARED
;
2644 * Add l2cache device information to the nvlist, including vdev stats.
2647 spa_add_l2cache(spa_t
*spa
, nvlist_t
*config
)
2650 uint_t i
, j
, nl2cache
;
2657 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
2659 if (spa
->spa_l2cache
.sav_count
== 0)
2662 VERIFY(nvlist_lookup_nvlist(config
,
2663 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
2664 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_l2cache
.sav_config
,
2665 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
2666 if (nl2cache
!= 0) {
2667 VERIFY(nvlist_add_nvlist_array(nvroot
,
2668 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
2669 VERIFY(nvlist_lookup_nvlist_array(nvroot
,
2670 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
2673 * Update level 2 cache device stats.
2676 for (i
= 0; i
< nl2cache
; i
++) {
2677 VERIFY(nvlist_lookup_uint64(l2cache
[i
],
2678 ZPOOL_CONFIG_GUID
, &guid
) == 0);
2681 for (j
= 0; j
< spa
->spa_l2cache
.sav_count
; j
++) {
2683 spa
->spa_l2cache
.sav_vdevs
[j
]->vdev_guid
) {
2684 vd
= spa
->spa_l2cache
.sav_vdevs
[j
];
2690 VERIFY(nvlist_lookup_uint64_array(l2cache
[i
],
2691 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
, &vsc
)
2693 vdev_get_stats(vd
, vs
);
2699 spa_get_stats(const char *name
, nvlist_t
**config
, char *altroot
, size_t buflen
)
2705 error
= spa_open_common(name
, &spa
, FTAG
, NULL
, config
);
2709 * This still leaves a window of inconsistency where the spares
2710 * or l2cache devices could change and the config would be
2711 * self-inconsistent.
2713 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
2715 if (*config
!= NULL
) {
2716 uint64_t loadtimes
[2];
2718 loadtimes
[0] = spa
->spa_loaded_ts
.tv_sec
;
2719 loadtimes
[1] = spa
->spa_loaded_ts
.tv_nsec
;
2720 VERIFY(nvlist_add_uint64_array(*config
,
2721 ZPOOL_CONFIG_LOADED_TIME
, loadtimes
, 2) == 0);
2723 VERIFY(nvlist_add_uint64(*config
,
2724 ZPOOL_CONFIG_ERRCOUNT
,
2725 spa_get_errlog_size(spa
)) == 0);
2727 if (spa_suspended(spa
))
2728 VERIFY(nvlist_add_uint64(*config
,
2729 ZPOOL_CONFIG_SUSPENDED
,
2730 spa
->spa_failmode
) == 0);
2732 spa_add_spares(spa
, *config
);
2733 spa_add_l2cache(spa
, *config
);
2738 * We want to get the alternate root even for faulted pools, so we cheat
2739 * and call spa_lookup() directly.
2743 mutex_enter(&spa_namespace_lock
);
2744 spa
= spa_lookup(name
);
2746 spa_altroot(spa
, altroot
, buflen
);
2750 mutex_exit(&spa_namespace_lock
);
2752 spa_altroot(spa
, altroot
, buflen
);
2757 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
2758 spa_close(spa
, FTAG
);
2765 * Validate that the auxiliary device array is well formed. We must have an
2766 * array of nvlists, each which describes a valid leaf vdev. If this is an
2767 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
2768 * specified, as long as they are well-formed.
2771 spa_validate_aux_devs(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
,
2772 spa_aux_vdev_t
*sav
, const char *config
, uint64_t version
,
2773 vdev_labeltype_t label
)
2780 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
2783 * It's acceptable to have no devs specified.
2785 if (nvlist_lookup_nvlist_array(nvroot
, config
, &dev
, &ndev
) != 0)
2792 * Make sure the pool is formatted with a version that supports this
2795 if (spa_version(spa
) < version
)
2799 * Set the pending device list so we correctly handle device in-use
2802 sav
->sav_pending
= dev
;
2803 sav
->sav_npending
= ndev
;
2805 for (i
= 0; i
< ndev
; i
++) {
2806 if ((error
= spa_config_parse(spa
, &vd
, dev
[i
], NULL
, 0,
2810 if (!vd
->vdev_ops
->vdev_op_leaf
) {
2817 * The L2ARC currently only supports disk devices in
2818 * kernel context. For user-level testing, we allow it.
2821 if ((strcmp(config
, ZPOOL_CONFIG_L2CACHE
) == 0) &&
2822 strcmp(vd
->vdev_ops
->vdev_op_type
, VDEV_TYPE_DISK
) != 0) {
2830 if ((error
= vdev_open(vd
)) == 0 &&
2831 (error
= vdev_label_init(vd
, crtxg
, label
)) == 0) {
2832 VERIFY(nvlist_add_uint64(dev
[i
], ZPOOL_CONFIG_GUID
,
2833 vd
->vdev_guid
) == 0);
2839 (mode
!= VDEV_ALLOC_SPARE
&& mode
!= VDEV_ALLOC_L2CACHE
))
2846 sav
->sav_pending
= NULL
;
2847 sav
->sav_npending
= 0;
2852 spa_validate_aux(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
)
2856 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
2858 if ((error
= spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
2859 &spa
->spa_spares
, ZPOOL_CONFIG_SPARES
, SPA_VERSION_SPARES
,
2860 VDEV_LABEL_SPARE
)) != 0) {
2864 return (spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
2865 &spa
->spa_l2cache
, ZPOOL_CONFIG_L2CACHE
, SPA_VERSION_L2CACHE
,
2866 VDEV_LABEL_L2CACHE
));
2870 spa_set_aux_vdevs(spa_aux_vdev_t
*sav
, nvlist_t
**devs
, int ndevs
,
2875 if (sav
->sav_config
!= NULL
) {
2881 * Generate new dev list by concatentating with the
2884 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
, config
,
2885 &olddevs
, &oldndevs
) == 0);
2887 newdevs
= kmem_alloc(sizeof (void *) *
2888 (ndevs
+ oldndevs
), KM_PUSHPAGE
);
2889 for (i
= 0; i
< oldndevs
; i
++)
2890 VERIFY(nvlist_dup(olddevs
[i
], &newdevs
[i
],
2892 for (i
= 0; i
< ndevs
; i
++)
2893 VERIFY(nvlist_dup(devs
[i
], &newdevs
[i
+ oldndevs
],
2896 VERIFY(nvlist_remove(sav
->sav_config
, config
,
2897 DATA_TYPE_NVLIST_ARRAY
) == 0);
2899 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
,
2900 config
, newdevs
, ndevs
+ oldndevs
) == 0);
2901 for (i
= 0; i
< oldndevs
+ ndevs
; i
++)
2902 nvlist_free(newdevs
[i
]);
2903 kmem_free(newdevs
, (oldndevs
+ ndevs
) * sizeof (void *));
2906 * Generate a new dev list.
2908 VERIFY(nvlist_alloc(&sav
->sav_config
, NV_UNIQUE_NAME
,
2910 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
, config
,
2916 * Stop and drop level 2 ARC devices
2919 spa_l2cache_drop(spa_t
*spa
)
2923 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
2925 for (i
= 0; i
< sav
->sav_count
; i
++) {
2928 vd
= sav
->sav_vdevs
[i
];
2931 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
2932 pool
!= 0ULL && l2arc_vdev_present(vd
))
2933 l2arc_remove_vdev(vd
);
2941 spa_create(const char *pool
, nvlist_t
*nvroot
, nvlist_t
*props
,
2942 const char *history_str
, nvlist_t
*zplprops
)
2945 char *altroot
= NULL
;
2950 uint64_t txg
= TXG_INITIAL
;
2951 nvlist_t
**spares
, **l2cache
;
2952 uint_t nspares
, nl2cache
;
2953 uint64_t version
, obj
;
2957 * If this pool already exists, return failure.
2959 mutex_enter(&spa_namespace_lock
);
2960 if (spa_lookup(pool
) != NULL
) {
2961 mutex_exit(&spa_namespace_lock
);
2966 * Allocate a new spa_t structure.
2968 (void) nvlist_lookup_string(props
,
2969 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
2970 spa
= spa_add(pool
, NULL
, altroot
);
2971 spa_activate(spa
, spa_mode_global
);
2973 if (props
&& (error
= spa_prop_validate(spa
, props
))) {
2974 spa_deactivate(spa
);
2976 mutex_exit(&spa_namespace_lock
);
2980 if (nvlist_lookup_uint64(props
, zpool_prop_to_name(ZPOOL_PROP_VERSION
),
2982 version
= SPA_VERSION
;
2983 ASSERT(version
<= SPA_VERSION
);
2985 spa
->spa_first_txg
= txg
;
2986 spa
->spa_uberblock
.ub_txg
= txg
- 1;
2987 spa
->spa_uberblock
.ub_version
= version
;
2988 spa
->spa_ubsync
= spa
->spa_uberblock
;
2991 * Create "The Godfather" zio to hold all async IOs
2993 spa
->spa_async_zio_root
= zio_root(spa
, NULL
, NULL
,
2994 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_GODFATHER
);
2997 * Create the root vdev.
2999 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3001 error
= spa_config_parse(spa
, &rvd
, nvroot
, NULL
, 0, VDEV_ALLOC_ADD
);
3003 ASSERT(error
!= 0 || rvd
!= NULL
);
3004 ASSERT(error
!= 0 || spa
->spa_root_vdev
== rvd
);
3006 if (error
== 0 && !zfs_allocatable_devs(nvroot
))
3010 (error
= vdev_create(rvd
, txg
, B_FALSE
)) == 0 &&
3011 (error
= spa_validate_aux(spa
, nvroot
, txg
,
3012 VDEV_ALLOC_ADD
)) == 0) {
3013 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
3014 vdev_metaslab_set_size(rvd
->vdev_child
[c
]);
3015 vdev_expand(rvd
->vdev_child
[c
], txg
);
3019 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3023 spa_deactivate(spa
);
3025 mutex_exit(&spa_namespace_lock
);
3030 * Get the list of spares, if specified.
3032 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
3033 &spares
, &nspares
) == 0) {
3034 VERIFY(nvlist_alloc(&spa
->spa_spares
.sav_config
, NV_UNIQUE_NAME
,
3036 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
3037 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
3038 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3039 spa_load_spares(spa
);
3040 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3041 spa
->spa_spares
.sav_sync
= B_TRUE
;
3045 * Get the list of level 2 cache devices, if specified.
3047 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
3048 &l2cache
, &nl2cache
) == 0) {
3049 VERIFY(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
3050 NV_UNIQUE_NAME
, KM_PUSHPAGE
) == 0);
3051 VERIFY(nvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
3052 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
3053 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3054 spa_load_l2cache(spa
);
3055 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3056 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
3059 spa
->spa_dsl_pool
= dp
= dsl_pool_create(spa
, zplprops
, txg
);
3060 spa
->spa_meta_objset
= dp
->dp_meta_objset
;
3063 * Create DDTs (dedup tables).
3067 spa_update_dspace(spa
);
3069 tx
= dmu_tx_create_assigned(dp
, txg
);
3072 * Create the pool config object.
3074 spa
->spa_config_object
= dmu_object_alloc(spa
->spa_meta_objset
,
3075 DMU_OT_PACKED_NVLIST
, SPA_CONFIG_BLOCKSIZE
,
3076 DMU_OT_PACKED_NVLIST_SIZE
, sizeof (uint64_t), tx
);
3078 if (zap_add(spa
->spa_meta_objset
,
3079 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CONFIG
,
3080 sizeof (uint64_t), 1, &spa
->spa_config_object
, tx
) != 0) {
3081 cmn_err(CE_PANIC
, "failed to add pool config");
3084 if (zap_add(spa
->spa_meta_objset
,
3085 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CREATION_VERSION
,
3086 sizeof (uint64_t), 1, &version
, tx
) != 0) {
3087 cmn_err(CE_PANIC
, "failed to add pool version");
3090 /* Newly created pools with the right version are always deflated. */
3091 if (version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
3092 spa
->spa_deflate
= TRUE
;
3093 if (zap_add(spa
->spa_meta_objset
,
3094 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
3095 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
) != 0) {
3096 cmn_err(CE_PANIC
, "failed to add deflate");
3101 * Create the deferred-free bpobj. Turn off compression
3102 * because sync-to-convergence takes longer if the blocksize
3105 obj
= bpobj_alloc(spa
->spa_meta_objset
, 1 << 14, tx
);
3106 dmu_object_set_compress(spa
->spa_meta_objset
, obj
,
3107 ZIO_COMPRESS_OFF
, tx
);
3108 if (zap_add(spa
->spa_meta_objset
,
3109 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_SYNC_BPOBJ
,
3110 sizeof (uint64_t), 1, &obj
, tx
) != 0) {
3111 cmn_err(CE_PANIC
, "failed to add bpobj");
3113 VERIFY3U(0, ==, bpobj_open(&spa
->spa_deferred_bpobj
,
3114 spa
->spa_meta_objset
, obj
));
3117 * Create the pool's history object.
3119 if (version
>= SPA_VERSION_ZPOOL_HISTORY
)
3120 spa_history_create_obj(spa
, tx
);
3123 * Set pool properties.
3125 spa
->spa_bootfs
= zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS
);
3126 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
3127 spa
->spa_failmode
= zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE
);
3128 spa
->spa_autoexpand
= zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND
);
3130 if (props
!= NULL
) {
3131 spa_configfile_set(spa
, props
, B_FALSE
);
3132 spa_sync_props(spa
, props
, tx
);
3137 spa
->spa_sync_on
= B_TRUE
;
3138 txg_sync_start(spa
->spa_dsl_pool
);
3141 * We explicitly wait for the first transaction to complete so that our
3142 * bean counters are appropriately updated.
3144 txg_wait_synced(spa
->spa_dsl_pool
, txg
);
3146 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
3148 if (version
>= SPA_VERSION_ZPOOL_HISTORY
&& history_str
!= NULL
)
3149 (void) spa_history_log(spa
, history_str
, LOG_CMD_POOL_CREATE
);
3150 spa_history_log_version(spa
, LOG_POOL_CREATE
);
3152 spa
->spa_minref
= refcount_count(&spa
->spa_refcount
);
3154 mutex_exit(&spa_namespace_lock
);
3161 * Get the root pool information from the root disk, then import the root pool
3162 * during the system boot up time.
3164 extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t
**);
3167 spa_generate_rootconf(char *devpath
, char *devid
, uint64_t *guid
)
3170 nvlist_t
*nvtop
, *nvroot
;
3173 if (vdev_disk_read_rootlabel(devpath
, devid
, &config
) != 0)
3177 * Add this top-level vdev to the child array.
3179 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
3181 VERIFY(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
3183 VERIFY(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_GUID
, guid
) == 0);
3186 * Put this pool's top-level vdevs into a root vdev.
3188 VERIFY(nvlist_alloc(&nvroot
, NV_UNIQUE_NAME
, KM_PUSHPAGE
) == 0);
3189 VERIFY(nvlist_add_string(nvroot
, ZPOOL_CONFIG_TYPE
,
3190 VDEV_TYPE_ROOT
) == 0);
3191 VERIFY(nvlist_add_uint64(nvroot
, ZPOOL_CONFIG_ID
, 0ULL) == 0);
3192 VERIFY(nvlist_add_uint64(nvroot
, ZPOOL_CONFIG_GUID
, pgid
) == 0);
3193 VERIFY(nvlist_add_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
3197 * Replace the existing vdev_tree with the new root vdev in
3198 * this pool's configuration (remove the old, add the new).
3200 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, nvroot
) == 0);
3201 nvlist_free(nvroot
);
3206 * Walk the vdev tree and see if we can find a device with "better"
3207 * configuration. A configuration is "better" if the label on that
3208 * device has a more recent txg.
3211 spa_alt_rootvdev(vdev_t
*vd
, vdev_t
**avd
, uint64_t *txg
)
3215 for (c
= 0; c
< vd
->vdev_children
; c
++)
3216 spa_alt_rootvdev(vd
->vdev_child
[c
], avd
, txg
);
3218 if (vd
->vdev_ops
->vdev_op_leaf
) {
3222 if (vdev_disk_read_rootlabel(vd
->vdev_physpath
, vd
->vdev_devid
,
3226 VERIFY(nvlist_lookup_uint64(label
, ZPOOL_CONFIG_POOL_TXG
,
3230 * Do we have a better boot device?
3232 if (label_txg
> *txg
) {
3241 * Import a root pool.
3243 * For x86. devpath_list will consist of devid and/or physpath name of
3244 * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
3245 * The GRUB "findroot" command will return the vdev we should boot.
3247 * For Sparc, devpath_list consists the physpath name of the booting device
3248 * no matter the rootpool is a single device pool or a mirrored pool.
3250 * "/pci@1f,0/ide@d/disk@0,0:a"
3253 spa_import_rootpool(char *devpath
, char *devid
)
3256 vdev_t
*rvd
, *bvd
, *avd
= NULL
;
3257 nvlist_t
*config
, *nvtop
;
3263 * Read the label from the boot device and generate a configuration.
3265 config
= spa_generate_rootconf(devpath
, devid
, &guid
);
3266 #if defined(_OBP) && defined(_KERNEL)
3267 if (config
== NULL
) {
3268 if (strstr(devpath
, "/iscsi/ssd") != NULL
) {
3270 get_iscsi_bootpath_phy(devpath
);
3271 config
= spa_generate_rootconf(devpath
, devid
, &guid
);
3275 if (config
== NULL
) {
3276 cmn_err(CE_NOTE
, "Can not read the pool label from '%s'",
3281 VERIFY(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
3283 VERIFY(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_TXG
, &txg
) == 0);
3285 mutex_enter(&spa_namespace_lock
);
3286 if ((spa
= spa_lookup(pname
)) != NULL
) {
3288 * Remove the existing root pool from the namespace so that we
3289 * can replace it with the correct config we just read in.
3294 spa
= spa_add(pname
, config
, NULL
);
3295 spa
->spa_is_root
= B_TRUE
;
3296 spa
->spa_import_flags
= ZFS_IMPORT_VERBATIM
;
3299 * Build up a vdev tree based on the boot device's label config.
3301 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
3303 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3304 error
= spa_config_parse(spa
, &rvd
, nvtop
, NULL
, 0,
3305 VDEV_ALLOC_ROOTPOOL
);
3306 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3308 mutex_exit(&spa_namespace_lock
);
3309 nvlist_free(config
);
3310 cmn_err(CE_NOTE
, "Can not parse the config for pool '%s'",
3316 * Get the boot vdev.
3318 if ((bvd
= vdev_lookup_by_guid(rvd
, guid
)) == NULL
) {
3319 cmn_err(CE_NOTE
, "Can not find the boot vdev for guid %llu",
3320 (u_longlong_t
)guid
);
3326 * Determine if there is a better boot device.
3329 spa_alt_rootvdev(rvd
, &avd
, &txg
);
3331 cmn_err(CE_NOTE
, "The boot device is 'degraded'. Please "
3332 "try booting from '%s'", avd
->vdev_path
);
3338 * If the boot device is part of a spare vdev then ensure that
3339 * we're booting off the active spare.
3341 if (bvd
->vdev_parent
->vdev_ops
== &vdev_spare_ops
&&
3342 !bvd
->vdev_isspare
) {
3343 cmn_err(CE_NOTE
, "The boot device is currently spared. Please "
3344 "try booting from '%s'",
3346 vdev_child
[bvd
->vdev_parent
->vdev_children
- 1]->vdev_path
);
3352 spa_history_log_version(spa
, LOG_POOL_IMPORT
);
3354 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3356 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3357 mutex_exit(&spa_namespace_lock
);
3359 nvlist_free(config
);
3366 * Import a non-root pool into the system.
3369 spa_import(const char *pool
, nvlist_t
*config
, nvlist_t
*props
, uint64_t flags
)
3372 char *altroot
= NULL
;
3373 spa_load_state_t state
= SPA_LOAD_IMPORT
;
3374 zpool_rewind_policy_t policy
;
3375 uint64_t mode
= spa_mode_global
;
3376 uint64_t readonly
= B_FALSE
;
3379 nvlist_t
**spares
, **l2cache
;
3380 uint_t nspares
, nl2cache
;
3383 * If a pool with this name exists, return failure.
3385 mutex_enter(&spa_namespace_lock
);
3386 if (spa_lookup(pool
) != NULL
) {
3387 mutex_exit(&spa_namespace_lock
);
3392 * Create and initialize the spa structure.
3394 (void) nvlist_lookup_string(props
,
3395 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
3396 (void) nvlist_lookup_uint64(props
,
3397 zpool_prop_to_name(ZPOOL_PROP_READONLY
), &readonly
);
3400 spa
= spa_add(pool
, config
, altroot
);
3401 spa
->spa_import_flags
= flags
;
3404 * Verbatim import - Take a pool and insert it into the namespace
3405 * as if it had been loaded at boot.
3407 if (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
) {
3409 spa_configfile_set(spa
, props
, B_FALSE
);
3411 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
3413 mutex_exit(&spa_namespace_lock
);
3414 spa_history_log_version(spa
, LOG_POOL_IMPORT
);
3419 spa_activate(spa
, mode
);
3422 * Don't start async tasks until we know everything is healthy.
3424 spa_async_suspend(spa
);
3426 zpool_get_rewind_policy(config
, &policy
);
3427 if (policy
.zrp_request
& ZPOOL_DO_REWIND
)
3428 state
= SPA_LOAD_RECOVER
;
3431 * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig
3432 * because the user-supplied config is actually the one to trust when
3435 if (state
!= SPA_LOAD_RECOVER
)
3436 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
3438 error
= spa_load_best(spa
, state
, B_TRUE
, policy
.zrp_txg
,
3439 policy
.zrp_request
);
3442 * Propagate anything learned while loading the pool and pass it
3443 * back to caller (i.e. rewind info, missing devices, etc).
3445 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
3446 spa
->spa_load_info
) == 0);
3448 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3450 * Toss any existing sparelist, as it doesn't have any validity
3451 * anymore, and conflicts with spa_has_spare().
3453 if (spa
->spa_spares
.sav_config
) {
3454 nvlist_free(spa
->spa_spares
.sav_config
);
3455 spa
->spa_spares
.sav_config
= NULL
;
3456 spa_load_spares(spa
);
3458 if (spa
->spa_l2cache
.sav_config
) {
3459 nvlist_free(spa
->spa_l2cache
.sav_config
);
3460 spa
->spa_l2cache
.sav_config
= NULL
;
3461 spa_load_l2cache(spa
);
3464 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
3467 error
= spa_validate_aux(spa
, nvroot
, -1ULL,
3470 error
= spa_validate_aux(spa
, nvroot
, -1ULL,
3471 VDEV_ALLOC_L2CACHE
);
3472 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3475 spa_configfile_set(spa
, props
, B_FALSE
);
3477 if (error
!= 0 || (props
&& spa_writeable(spa
) &&
3478 (error
= spa_prop_set(spa
, props
)))) {
3480 spa_deactivate(spa
);
3482 mutex_exit(&spa_namespace_lock
);
3486 spa_async_resume(spa
);
3489 * Override any spares and level 2 cache devices as specified by
3490 * the user, as these may have correct device names/devids, etc.
3492 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
3493 &spares
, &nspares
) == 0) {
3494 if (spa
->spa_spares
.sav_config
)
3495 VERIFY(nvlist_remove(spa
->spa_spares
.sav_config
,
3496 ZPOOL_CONFIG_SPARES
, DATA_TYPE_NVLIST_ARRAY
) == 0);
3498 VERIFY(nvlist_alloc(&spa
->spa_spares
.sav_config
,
3499 NV_UNIQUE_NAME
, KM_PUSHPAGE
) == 0);
3500 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
3501 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
3502 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3503 spa_load_spares(spa
);
3504 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3505 spa
->spa_spares
.sav_sync
= B_TRUE
;
3507 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
3508 &l2cache
, &nl2cache
) == 0) {
3509 if (spa
->spa_l2cache
.sav_config
)
3510 VERIFY(nvlist_remove(spa
->spa_l2cache
.sav_config
,
3511 ZPOOL_CONFIG_L2CACHE
, DATA_TYPE_NVLIST_ARRAY
) == 0);
3513 VERIFY(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
3514 NV_UNIQUE_NAME
, KM_PUSHPAGE
) == 0);
3515 VERIFY(nvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
3516 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
3517 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3518 spa_load_l2cache(spa
);
3519 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3520 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
3524 * Check for any removed devices.
3526 if (spa
->spa_autoreplace
) {
3527 spa_aux_check_removed(&spa
->spa_spares
);
3528 spa_aux_check_removed(&spa
->spa_l2cache
);
3531 if (spa_writeable(spa
)) {
3533 * Update the config cache to include the newly-imported pool.
3535 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
3539 * It's possible that the pool was expanded while it was exported.
3540 * We kick off an async task to handle this for us.
3542 spa_async_request(spa
, SPA_ASYNC_AUTOEXPAND
);
3544 mutex_exit(&spa_namespace_lock
);
3545 spa_history_log_version(spa
, LOG_POOL_IMPORT
);
3551 spa_tryimport(nvlist_t
*tryconfig
)
3553 nvlist_t
*config
= NULL
;
3559 if (nvlist_lookup_string(tryconfig
, ZPOOL_CONFIG_POOL_NAME
, &poolname
))
3562 if (nvlist_lookup_uint64(tryconfig
, ZPOOL_CONFIG_POOL_STATE
, &state
))
3566 * Create and initialize the spa structure.
3568 mutex_enter(&spa_namespace_lock
);
3569 spa
= spa_add(TRYIMPORT_NAME
, tryconfig
, NULL
);
3570 spa_activate(spa
, FREAD
);
3573 * Pass off the heavy lifting to spa_load().
3574 * Pass TRUE for mosconfig because the user-supplied config
3575 * is actually the one to trust when doing an import.
3577 error
= spa_load(spa
, SPA_LOAD_TRYIMPORT
, SPA_IMPORT_EXISTING
, B_TRUE
);
3580 * If 'tryconfig' was at least parsable, return the current config.
3582 if (spa
->spa_root_vdev
!= NULL
) {
3583 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
3584 VERIFY(nvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
,
3586 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
3588 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_TIMESTAMP
,
3589 spa
->spa_uberblock
.ub_timestamp
) == 0);
3592 * If the bootfs property exists on this pool then we
3593 * copy it out so that external consumers can tell which
3594 * pools are bootable.
3596 if ((!error
|| error
== EEXIST
) && spa
->spa_bootfs
) {
3597 char *tmpname
= kmem_alloc(MAXPATHLEN
, KM_PUSHPAGE
);
3600 * We have to play games with the name since the
3601 * pool was opened as TRYIMPORT_NAME.
3603 if (dsl_dsobj_to_dsname(spa_name(spa
),
3604 spa
->spa_bootfs
, tmpname
) == 0) {
3606 char *dsname
= kmem_alloc(MAXPATHLEN
, KM_PUSHPAGE
);
3608 cp
= strchr(tmpname
, '/');
3610 (void) strlcpy(dsname
, tmpname
,
3613 (void) snprintf(dsname
, MAXPATHLEN
,
3614 "%s/%s", poolname
, ++cp
);
3616 VERIFY(nvlist_add_string(config
,
3617 ZPOOL_CONFIG_BOOTFS
, dsname
) == 0);
3618 kmem_free(dsname
, MAXPATHLEN
);
3620 kmem_free(tmpname
, MAXPATHLEN
);
3624 * Add the list of hot spares and level 2 cache devices.
3626 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
3627 spa_add_spares(spa
, config
);
3628 spa_add_l2cache(spa
, config
);
3629 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
3633 spa_deactivate(spa
);
3635 mutex_exit(&spa_namespace_lock
);
3641 * Pool export/destroy
3643 * The act of destroying or exporting a pool is very simple. We make sure there
3644 * is no more pending I/O and any references to the pool are gone. Then, we
3645 * update the pool state and sync all the labels to disk, removing the
3646 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
3647 * we don't sync the labels or remove the configuration cache.
3650 spa_export_common(char *pool
, int new_state
, nvlist_t
**oldconfig
,
3651 boolean_t force
, boolean_t hardforce
)
3658 if (!(spa_mode_global
& FWRITE
))
3661 mutex_enter(&spa_namespace_lock
);
3662 if ((spa
= spa_lookup(pool
)) == NULL
) {
3663 mutex_exit(&spa_namespace_lock
);
3668 * Put a hold on the pool, drop the namespace lock, stop async tasks,
3669 * reacquire the namespace lock, and see if we can export.
3671 spa_open_ref(spa
, FTAG
);
3672 mutex_exit(&spa_namespace_lock
);
3673 spa_async_suspend(spa
);
3674 mutex_enter(&spa_namespace_lock
);
3675 spa_close(spa
, FTAG
);
3678 * The pool will be in core if it's openable,
3679 * in which case we can modify its state.
3681 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
&& spa
->spa_sync_on
) {
3683 * Objsets may be open only because they're dirty, so we
3684 * have to force it to sync before checking spa_refcnt.
3686 txg_wait_synced(spa
->spa_dsl_pool
, 0);
3689 * A pool cannot be exported or destroyed if there are active
3690 * references. If we are resetting a pool, allow references by
3691 * fault injection handlers.
3693 if (!spa_refcount_zero(spa
) ||
3694 (spa
->spa_inject_ref
!= 0 &&
3695 new_state
!= POOL_STATE_UNINITIALIZED
)) {
3696 spa_async_resume(spa
);
3697 mutex_exit(&spa_namespace_lock
);
3702 * A pool cannot be exported if it has an active shared spare.
3703 * This is to prevent other pools stealing the active spare
3704 * from an exported pool. At user's own will, such pool can
3705 * be forcedly exported.
3707 if (!force
&& new_state
== POOL_STATE_EXPORTED
&&
3708 spa_has_active_shared_spare(spa
)) {
3709 spa_async_resume(spa
);
3710 mutex_exit(&spa_namespace_lock
);
3715 * We want this to be reflected on every label,
3716 * so mark them all dirty. spa_unload() will do the
3717 * final sync that pushes these changes out.
3719 if (new_state
!= POOL_STATE_UNINITIALIZED
&& !hardforce
) {
3720 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3721 spa
->spa_state
= new_state
;
3722 spa
->spa_final_txg
= spa_last_synced_txg(spa
) +
3724 vdev_config_dirty(spa
->spa_root_vdev
);
3725 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3729 spa_event_notify(spa
, NULL
, FM_EREPORT_ZFS_POOL_DESTROY
);
3731 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
3733 spa_deactivate(spa
);
3736 if (oldconfig
&& spa
->spa_config
)
3737 VERIFY(nvlist_dup(spa
->spa_config
, oldconfig
, 0) == 0);
3739 if (new_state
!= POOL_STATE_UNINITIALIZED
) {
3741 spa_config_sync(spa
, B_TRUE
, B_TRUE
);
3744 mutex_exit(&spa_namespace_lock
);
3750 * Destroy a storage pool.
3753 spa_destroy(char *pool
)
3755 return (spa_export_common(pool
, POOL_STATE_DESTROYED
, NULL
,
3760 * Export a storage pool.
3763 spa_export(char *pool
, nvlist_t
**oldconfig
, boolean_t force
,
3764 boolean_t hardforce
)
3766 return (spa_export_common(pool
, POOL_STATE_EXPORTED
, oldconfig
,
3771 * Similar to spa_export(), this unloads the spa_t without actually removing it
3772 * from the namespace in any way.
3775 spa_reset(char *pool
)
3777 return (spa_export_common(pool
, POOL_STATE_UNINITIALIZED
, NULL
,
3782 * ==========================================================================
3783 * Device manipulation
3784 * ==========================================================================
3788 * Add a device to a storage pool.
3791 spa_vdev_add(spa_t
*spa
, nvlist_t
*nvroot
)
3795 vdev_t
*rvd
= spa
->spa_root_vdev
;
3797 nvlist_t
**spares
, **l2cache
;
3798 uint_t nspares
, nl2cache
;
3801 ASSERT(spa_writeable(spa
));
3803 txg
= spa_vdev_enter(spa
);
3805 if ((error
= spa_config_parse(spa
, &vd
, nvroot
, NULL
, 0,
3806 VDEV_ALLOC_ADD
)) != 0)
3807 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
3809 spa
->spa_pending_vdev
= vd
; /* spa_vdev_exit() will clear this */
3811 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
, &spares
,
3815 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
, &l2cache
,
3819 if (vd
->vdev_children
== 0 && nspares
== 0 && nl2cache
== 0)
3820 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
3822 if (vd
->vdev_children
!= 0 &&
3823 (error
= vdev_create(vd
, txg
, B_FALSE
)) != 0)
3824 return (spa_vdev_exit(spa
, vd
, txg
, error
));
3827 * We must validate the spares and l2cache devices after checking the
3828 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
3830 if ((error
= spa_validate_aux(spa
, nvroot
, txg
, VDEV_ALLOC_ADD
)) != 0)
3831 return (spa_vdev_exit(spa
, vd
, txg
, error
));
3834 * Transfer each new top-level vdev from vd to rvd.
3836 for (c
= 0; c
< vd
->vdev_children
; c
++) {
3839 * Set the vdev id to the first hole, if one exists.
3841 for (id
= 0; id
< rvd
->vdev_children
; id
++) {
3842 if (rvd
->vdev_child
[id
]->vdev_ishole
) {
3843 vdev_free(rvd
->vdev_child
[id
]);
3847 tvd
= vd
->vdev_child
[c
];
3848 vdev_remove_child(vd
, tvd
);
3850 vdev_add_child(rvd
, tvd
);
3851 vdev_config_dirty(tvd
);
3855 spa_set_aux_vdevs(&spa
->spa_spares
, spares
, nspares
,
3856 ZPOOL_CONFIG_SPARES
);
3857 spa_load_spares(spa
);
3858 spa
->spa_spares
.sav_sync
= B_TRUE
;
3861 if (nl2cache
!= 0) {
3862 spa_set_aux_vdevs(&spa
->spa_l2cache
, l2cache
, nl2cache
,
3863 ZPOOL_CONFIG_L2CACHE
);
3864 spa_load_l2cache(spa
);
3865 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
3869 * We have to be careful when adding new vdevs to an existing pool.
3870 * If other threads start allocating from these vdevs before we
3871 * sync the config cache, and we lose power, then upon reboot we may
3872 * fail to open the pool because there are DVAs that the config cache
3873 * can't translate. Therefore, we first add the vdevs without
3874 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
3875 * and then let spa_config_update() initialize the new metaslabs.
3877 * spa_load() checks for added-but-not-initialized vdevs, so that
3878 * if we lose power at any point in this sequence, the remaining
3879 * steps will be completed the next time we load the pool.
3881 (void) spa_vdev_exit(spa
, vd
, txg
, 0);
3883 mutex_enter(&spa_namespace_lock
);
3884 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
3885 mutex_exit(&spa_namespace_lock
);
3891 * Attach a device to a mirror. The arguments are the path to any device
3892 * in the mirror, and the nvroot for the new device. If the path specifies
3893 * a device that is not mirrored, we automatically insert the mirror vdev.
3895 * If 'replacing' is specified, the new device is intended to replace the
3896 * existing device; in this case the two devices are made into their own
3897 * mirror using the 'replacing' vdev, which is functionally identical to
3898 * the mirror vdev (it actually reuses all the same ops) but has a few
3899 * extra rules: you can't attach to it after it's been created, and upon
3900 * completion of resilvering, the first disk (the one being replaced)
3901 * is automatically detached.
3904 spa_vdev_attach(spa_t
*spa
, uint64_t guid
, nvlist_t
*nvroot
, int replacing
)
3906 uint64_t txg
, dtl_max_txg
;
3907 ASSERTV(vdev_t
*rvd
= spa
->spa_root_vdev
;)
3908 vdev_t
*oldvd
, *newvd
, *newrootvd
, *pvd
, *tvd
;
3910 char *oldvdpath
, *newvdpath
;
3914 ASSERT(spa_writeable(spa
));
3916 txg
= spa_vdev_enter(spa
);
3918 oldvd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
3921 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
3923 if (!oldvd
->vdev_ops
->vdev_op_leaf
)
3924 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
3926 pvd
= oldvd
->vdev_parent
;
3928 if ((error
= spa_config_parse(spa
, &newrootvd
, nvroot
, NULL
, 0,
3929 VDEV_ALLOC_ATTACH
)) != 0)
3930 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
3932 if (newrootvd
->vdev_children
!= 1)
3933 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
3935 newvd
= newrootvd
->vdev_child
[0];
3937 if (!newvd
->vdev_ops
->vdev_op_leaf
)
3938 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
3940 if ((error
= vdev_create(newrootvd
, txg
, replacing
)) != 0)
3941 return (spa_vdev_exit(spa
, newrootvd
, txg
, error
));
3944 * Spares can't replace logs
3946 if (oldvd
->vdev_top
->vdev_islog
&& newvd
->vdev_isspare
)
3947 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
3951 * For attach, the only allowable parent is a mirror or the root
3954 if (pvd
->vdev_ops
!= &vdev_mirror_ops
&&
3955 pvd
->vdev_ops
!= &vdev_root_ops
)
3956 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
3958 pvops
= &vdev_mirror_ops
;
3961 * Active hot spares can only be replaced by inactive hot
3964 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
3965 oldvd
->vdev_isspare
&&
3966 !spa_has_spare(spa
, newvd
->vdev_guid
))
3967 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
3970 * If the source is a hot spare, and the parent isn't already a
3971 * spare, then we want to create a new hot spare. Otherwise, we
3972 * want to create a replacing vdev. The user is not allowed to
3973 * attach to a spared vdev child unless the 'isspare' state is
3974 * the same (spare replaces spare, non-spare replaces
3977 if (pvd
->vdev_ops
== &vdev_replacing_ops
&&
3978 spa_version(spa
) < SPA_VERSION_MULTI_REPLACE
) {
3979 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
3980 } else if (pvd
->vdev_ops
== &vdev_spare_ops
&&
3981 newvd
->vdev_isspare
!= oldvd
->vdev_isspare
) {
3982 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
3985 if (newvd
->vdev_isspare
)
3986 pvops
= &vdev_spare_ops
;
3988 pvops
= &vdev_replacing_ops
;
3992 * Make sure the new device is big enough.
3994 if (newvd
->vdev_asize
< vdev_get_min_asize(oldvd
))
3995 return (spa_vdev_exit(spa
, newrootvd
, txg
, EOVERFLOW
));
3998 * The new device cannot have a higher alignment requirement
3999 * than the top-level vdev.
4001 if (newvd
->vdev_ashift
> oldvd
->vdev_top
->vdev_ashift
)
4002 return (spa_vdev_exit(spa
, newrootvd
, txg
, EDOM
));
4005 * If this is an in-place replacement, update oldvd's path and devid
4006 * to make it distinguishable from newvd, and unopenable from now on.
4008 if (strcmp(oldvd
->vdev_path
, newvd
->vdev_path
) == 0) {
4009 spa_strfree(oldvd
->vdev_path
);
4010 oldvd
->vdev_path
= kmem_alloc(strlen(newvd
->vdev_path
) + 5,
4012 (void) sprintf(oldvd
->vdev_path
, "%s/%s",
4013 newvd
->vdev_path
, "old");
4014 if (oldvd
->vdev_devid
!= NULL
) {
4015 spa_strfree(oldvd
->vdev_devid
);
4016 oldvd
->vdev_devid
= NULL
;
4020 /* mark the device being resilvered */
4021 newvd
->vdev_resilvering
= B_TRUE
;
4024 * If the parent is not a mirror, or if we're replacing, insert the new
4025 * mirror/replacing/spare vdev above oldvd.
4027 if (pvd
->vdev_ops
!= pvops
)
4028 pvd
= vdev_add_parent(oldvd
, pvops
);
4030 ASSERT(pvd
->vdev_top
->vdev_parent
== rvd
);
4031 ASSERT(pvd
->vdev_ops
== pvops
);
4032 ASSERT(oldvd
->vdev_parent
== pvd
);
4035 * Extract the new device from its root and add it to pvd.
4037 vdev_remove_child(newrootvd
, newvd
);
4038 newvd
->vdev_id
= pvd
->vdev_children
;
4039 newvd
->vdev_crtxg
= oldvd
->vdev_crtxg
;
4040 vdev_add_child(pvd
, newvd
);
4042 tvd
= newvd
->vdev_top
;
4043 ASSERT(pvd
->vdev_top
== tvd
);
4044 ASSERT(tvd
->vdev_parent
== rvd
);
4046 vdev_config_dirty(tvd
);
4049 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
4050 * for any dmu_sync-ed blocks. It will propagate upward when
4051 * spa_vdev_exit() calls vdev_dtl_reassess().
4053 dtl_max_txg
= txg
+ TXG_CONCURRENT_STATES
;
4055 vdev_dtl_dirty(newvd
, DTL_MISSING
, TXG_INITIAL
,
4056 dtl_max_txg
- TXG_INITIAL
);
4058 if (newvd
->vdev_isspare
) {
4059 spa_spare_activate(newvd
);
4060 spa_event_notify(spa
, newvd
, FM_EREPORT_ZFS_DEVICE_SPARE
);
4063 oldvdpath
= spa_strdup(oldvd
->vdev_path
);
4064 newvdpath
= spa_strdup(newvd
->vdev_path
);
4065 newvd_isspare
= newvd
->vdev_isspare
;
4068 * Mark newvd's DTL dirty in this txg.
4070 vdev_dirty(tvd
, VDD_DTL
, newvd
, txg
);
4073 * Restart the resilver
4075 dsl_resilver_restart(spa
->spa_dsl_pool
, dtl_max_txg
);
4080 (void) spa_vdev_exit(spa
, newrootvd
, dtl_max_txg
, 0);
4082 spa_history_log_internal(LOG_POOL_VDEV_ATTACH
, spa
, NULL
,
4083 "%s vdev=%s %s vdev=%s",
4084 replacing
&& newvd_isspare
? "spare in" :
4085 replacing
? "replace" : "attach", newvdpath
,
4086 replacing
? "for" : "to", oldvdpath
);
4088 spa_strfree(oldvdpath
);
4089 spa_strfree(newvdpath
);
4091 if (spa
->spa_bootfs
)
4092 spa_event_notify(spa
, newvd
, FM_EREPORT_ZFS_BOOTFS_VDEV_ATTACH
);
4098 * Detach a device from a mirror or replacing vdev.
4099 * If 'replace_done' is specified, only detach if the parent
4100 * is a replacing vdev.
4103 spa_vdev_detach(spa_t
*spa
, uint64_t guid
, uint64_t pguid
, int replace_done
)
4107 ASSERTV(vdev_t
*rvd
= spa
->spa_root_vdev
;)
4108 vdev_t
*vd
, *pvd
, *cvd
, *tvd
;
4109 boolean_t unspare
= B_FALSE
;
4110 uint64_t unspare_guid
= 0;
4114 ASSERT(spa_writeable(spa
));
4116 txg
= spa_vdev_enter(spa
);
4118 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
4121 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
4123 if (!vd
->vdev_ops
->vdev_op_leaf
)
4124 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4126 pvd
= vd
->vdev_parent
;
4129 * If the parent/child relationship is not as expected, don't do it.
4130 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
4131 * vdev that's replacing B with C. The user's intent in replacing
4132 * is to go from M(A,B) to M(A,C). If the user decides to cancel
4133 * the replace by detaching C, the expected behavior is to end up
4134 * M(A,B). But suppose that right after deciding to detach C,
4135 * the replacement of B completes. We would have M(A,C), and then
4136 * ask to detach C, which would leave us with just A -- not what
4137 * the user wanted. To prevent this, we make sure that the
4138 * parent/child relationship hasn't changed -- in this example,
4139 * that C's parent is still the replacing vdev R.
4141 if (pvd
->vdev_guid
!= pguid
&& pguid
!= 0)
4142 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
4145 * Only 'replacing' or 'spare' vdevs can be replaced.
4147 if (replace_done
&& pvd
->vdev_ops
!= &vdev_replacing_ops
&&
4148 pvd
->vdev_ops
!= &vdev_spare_ops
)
4149 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4151 ASSERT(pvd
->vdev_ops
!= &vdev_spare_ops
||
4152 spa_version(spa
) >= SPA_VERSION_SPARES
);
4155 * Only mirror, replacing, and spare vdevs support detach.
4157 if (pvd
->vdev_ops
!= &vdev_replacing_ops
&&
4158 pvd
->vdev_ops
!= &vdev_mirror_ops
&&
4159 pvd
->vdev_ops
!= &vdev_spare_ops
)
4160 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4163 * If this device has the only valid copy of some data,
4164 * we cannot safely detach it.
4166 if (vdev_dtl_required(vd
))
4167 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
4169 ASSERT(pvd
->vdev_children
>= 2);
4172 * If we are detaching the second disk from a replacing vdev, then
4173 * check to see if we changed the original vdev's path to have "/old"
4174 * at the end in spa_vdev_attach(). If so, undo that change now.
4176 if (pvd
->vdev_ops
== &vdev_replacing_ops
&& vd
->vdev_id
> 0 &&
4177 vd
->vdev_path
!= NULL
) {
4178 size_t len
= strlen(vd
->vdev_path
);
4180 for (c
= 0; c
< pvd
->vdev_children
; c
++) {
4181 cvd
= pvd
->vdev_child
[c
];
4183 if (cvd
== vd
|| cvd
->vdev_path
== NULL
)
4186 if (strncmp(cvd
->vdev_path
, vd
->vdev_path
, len
) == 0 &&
4187 strcmp(cvd
->vdev_path
+ len
, "/old") == 0) {
4188 spa_strfree(cvd
->vdev_path
);
4189 cvd
->vdev_path
= spa_strdup(vd
->vdev_path
);
4196 * If we are detaching the original disk from a spare, then it implies
4197 * that the spare should become a real disk, and be removed from the
4198 * active spare list for the pool.
4200 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
4202 pvd
->vdev_child
[pvd
->vdev_children
- 1]->vdev_isspare
)
4206 * Erase the disk labels so the disk can be used for other things.
4207 * This must be done after all other error cases are handled,
4208 * but before we disembowel vd (so we can still do I/O to it).
4209 * But if we can't do it, don't treat the error as fatal --
4210 * it may be that the unwritability of the disk is the reason
4211 * it's being detached!
4213 error
= vdev_label_init(vd
, 0, VDEV_LABEL_REMOVE
);
4216 * Remove vd from its parent and compact the parent's children.
4218 vdev_remove_child(pvd
, vd
);
4219 vdev_compact_children(pvd
);
4222 * Remember one of the remaining children so we can get tvd below.
4224 cvd
= pvd
->vdev_child
[pvd
->vdev_children
- 1];
4227 * If we need to remove the remaining child from the list of hot spares,
4228 * do it now, marking the vdev as no longer a spare in the process.
4229 * We must do this before vdev_remove_parent(), because that can
4230 * change the GUID if it creates a new toplevel GUID. For a similar
4231 * reason, we must remove the spare now, in the same txg as the detach;
4232 * otherwise someone could attach a new sibling, change the GUID, and
4233 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
4236 ASSERT(cvd
->vdev_isspare
);
4237 spa_spare_remove(cvd
);
4238 unspare_guid
= cvd
->vdev_guid
;
4239 (void) spa_vdev_remove(spa
, unspare_guid
, B_TRUE
);
4240 cvd
->vdev_unspare
= B_TRUE
;
4244 * If the parent mirror/replacing vdev only has one child,
4245 * the parent is no longer needed. Remove it from the tree.
4247 if (pvd
->vdev_children
== 1) {
4248 if (pvd
->vdev_ops
== &vdev_spare_ops
)
4249 cvd
->vdev_unspare
= B_FALSE
;
4250 vdev_remove_parent(cvd
);
4251 cvd
->vdev_resilvering
= B_FALSE
;
4256 * We don't set tvd until now because the parent we just removed
4257 * may have been the previous top-level vdev.
4259 tvd
= cvd
->vdev_top
;
4260 ASSERT(tvd
->vdev_parent
== rvd
);
4263 * Reevaluate the parent vdev state.
4265 vdev_propagate_state(cvd
);
4268 * If the 'autoexpand' property is set on the pool then automatically
4269 * try to expand the size of the pool. For example if the device we
4270 * just detached was smaller than the others, it may be possible to
4271 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
4272 * first so that we can obtain the updated sizes of the leaf vdevs.
4274 if (spa
->spa_autoexpand
) {
4276 vdev_expand(tvd
, txg
);
4279 vdev_config_dirty(tvd
);
4282 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
4283 * vd->vdev_detached is set and free vd's DTL object in syncing context.
4284 * But first make sure we're not on any *other* txg's DTL list, to
4285 * prevent vd from being accessed after it's freed.
4287 vdpath
= spa_strdup(vd
->vdev_path
);
4288 for (t
= 0; t
< TXG_SIZE
; t
++)
4289 (void) txg_list_remove_this(&tvd
->vdev_dtl_list
, vd
, t
);
4290 vd
->vdev_detached
= B_TRUE
;
4291 vdev_dirty(tvd
, VDD_DTL
, vd
, txg
);
4293 spa_event_notify(spa
, vd
, FM_EREPORT_ZFS_DEVICE_REMOVE
);
4295 /* hang on to the spa before we release the lock */
4296 spa_open_ref(spa
, FTAG
);
4298 error
= spa_vdev_exit(spa
, vd
, txg
, 0);
4300 spa_history_log_internal(LOG_POOL_VDEV_DETACH
, spa
, NULL
,
4302 spa_strfree(vdpath
);
4305 * If this was the removal of the original device in a hot spare vdev,
4306 * then we want to go through and remove the device from the hot spare
4307 * list of every other pool.
4310 spa_t
*altspa
= NULL
;
4312 mutex_enter(&spa_namespace_lock
);
4313 while ((altspa
= spa_next(altspa
)) != NULL
) {
4314 if (altspa
->spa_state
!= POOL_STATE_ACTIVE
||
4318 spa_open_ref(altspa
, FTAG
);
4319 mutex_exit(&spa_namespace_lock
);
4320 (void) spa_vdev_remove(altspa
, unspare_guid
, B_TRUE
);
4321 mutex_enter(&spa_namespace_lock
);
4322 spa_close(altspa
, FTAG
);
4324 mutex_exit(&spa_namespace_lock
);
4326 /* search the rest of the vdevs for spares to remove */
4327 spa_vdev_resilver_done(spa
);
4330 /* all done with the spa; OK to release */
4331 mutex_enter(&spa_namespace_lock
);
4332 spa_close(spa
, FTAG
);
4333 mutex_exit(&spa_namespace_lock
);
4339 * Split a set of devices from their mirrors, and create a new pool from them.
4342 spa_vdev_split_mirror(spa_t
*spa
, char *newname
, nvlist_t
*config
,
4343 nvlist_t
*props
, boolean_t exp
)
4346 uint64_t txg
, *glist
;
4348 uint_t c
, children
, lastlog
;
4349 nvlist_t
**child
, *nvl
, *tmp
;
4351 char *altroot
= NULL
;
4352 vdev_t
*rvd
, **vml
= NULL
; /* vdev modify list */
4353 boolean_t activate_slog
;
4355 ASSERT(spa_writeable(spa
));
4357 txg
= spa_vdev_enter(spa
);
4359 /* clear the log and flush everything up to now */
4360 activate_slog
= spa_passivate_log(spa
);
4361 (void) spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
4362 error
= spa_offline_log(spa
);
4363 txg
= spa_vdev_config_enter(spa
);
4366 spa_activate_log(spa
);
4369 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
4371 /* check new spa name before going any further */
4372 if (spa_lookup(newname
) != NULL
)
4373 return (spa_vdev_exit(spa
, NULL
, txg
, EEXIST
));
4376 * scan through all the children to ensure they're all mirrors
4378 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvl
) != 0 ||
4379 nvlist_lookup_nvlist_array(nvl
, ZPOOL_CONFIG_CHILDREN
, &child
,
4381 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
4383 /* first, check to ensure we've got the right child count */
4384 rvd
= spa
->spa_root_vdev
;
4386 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
4387 vdev_t
*vd
= rvd
->vdev_child
[c
];
4389 /* don't count the holes & logs as children */
4390 if (vd
->vdev_islog
|| vd
->vdev_ishole
) {
4398 if (children
!= (lastlog
!= 0 ? lastlog
: rvd
->vdev_children
))
4399 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
4401 /* next, ensure no spare or cache devices are part of the split */
4402 if (nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_SPARES
, &tmp
) == 0 ||
4403 nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_L2CACHE
, &tmp
) == 0)
4404 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
4406 vml
= kmem_zalloc(children
* sizeof (vdev_t
*), KM_PUSHPAGE
);
4407 glist
= kmem_zalloc(children
* sizeof (uint64_t), KM_PUSHPAGE
);
4409 /* then, loop over each vdev and validate it */
4410 for (c
= 0; c
< children
; c
++) {
4411 uint64_t is_hole
= 0;
4413 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
4417 if (spa
->spa_root_vdev
->vdev_child
[c
]->vdev_ishole
||
4418 spa
->spa_root_vdev
->vdev_child
[c
]->vdev_islog
) {
4426 /* which disk is going to be split? */
4427 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_GUID
,
4433 /* look it up in the spa */
4434 vml
[c
] = spa_lookup_by_guid(spa
, glist
[c
], B_FALSE
);
4435 if (vml
[c
] == NULL
) {
4440 /* make sure there's nothing stopping the split */
4441 if (vml
[c
]->vdev_parent
->vdev_ops
!= &vdev_mirror_ops
||
4442 vml
[c
]->vdev_islog
||
4443 vml
[c
]->vdev_ishole
||
4444 vml
[c
]->vdev_isspare
||
4445 vml
[c
]->vdev_isl2cache
||
4446 !vdev_writeable(vml
[c
]) ||
4447 vml
[c
]->vdev_children
!= 0 ||
4448 vml
[c
]->vdev_state
!= VDEV_STATE_HEALTHY
||
4449 c
!= spa
->spa_root_vdev
->vdev_child
[c
]->vdev_id
) {
4454 if (vdev_dtl_required(vml
[c
])) {
4459 /* we need certain info from the top level */
4460 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_ARRAY
,
4461 vml
[c
]->vdev_top
->vdev_ms_array
) == 0);
4462 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_SHIFT
,
4463 vml
[c
]->vdev_top
->vdev_ms_shift
) == 0);
4464 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASIZE
,
4465 vml
[c
]->vdev_top
->vdev_asize
) == 0);
4466 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASHIFT
,
4467 vml
[c
]->vdev_top
->vdev_ashift
) == 0);
4471 kmem_free(vml
, children
* sizeof (vdev_t
*));
4472 kmem_free(glist
, children
* sizeof (uint64_t));
4473 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
4476 /* stop writers from using the disks */
4477 for (c
= 0; c
< children
; c
++) {
4479 vml
[c
]->vdev_offline
= B_TRUE
;
4481 vdev_reopen(spa
->spa_root_vdev
);
4484 * Temporarily record the splitting vdevs in the spa config. This
4485 * will disappear once the config is regenerated.
4487 VERIFY(nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, KM_PUSHPAGE
) == 0);
4488 VERIFY(nvlist_add_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
4489 glist
, children
) == 0);
4490 kmem_free(glist
, children
* sizeof (uint64_t));
4492 mutex_enter(&spa
->spa_props_lock
);
4493 VERIFY(nvlist_add_nvlist(spa
->spa_config
, ZPOOL_CONFIG_SPLIT
,
4495 mutex_exit(&spa
->spa_props_lock
);
4496 spa
->spa_config_splitting
= nvl
;
4497 vdev_config_dirty(spa
->spa_root_vdev
);
4499 /* configure and create the new pool */
4500 VERIFY(nvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
, newname
) == 0);
4501 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
4502 exp
? POOL_STATE_EXPORTED
: POOL_STATE_ACTIVE
) == 0);
4503 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
4504 spa_version(spa
)) == 0);
4505 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
4506 spa
->spa_config_txg
) == 0);
4507 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
4508 spa_generate_guid(NULL
)) == 0);
4509 (void) nvlist_lookup_string(props
,
4510 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
4512 /* add the new pool to the namespace */
4513 newspa
= spa_add(newname
, config
, altroot
);
4514 newspa
->spa_config_txg
= spa
->spa_config_txg
;
4515 spa_set_log_state(newspa
, SPA_LOG_CLEAR
);
4517 /* release the spa config lock, retaining the namespace lock */
4518 spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
4520 if (zio_injection_enabled
)
4521 zio_handle_panic_injection(spa
, FTAG
, 1);
4523 spa_activate(newspa
, spa_mode_global
);
4524 spa_async_suspend(newspa
);
4526 /* create the new pool from the disks of the original pool */
4527 error
= spa_load(newspa
, SPA_LOAD_IMPORT
, SPA_IMPORT_ASSEMBLE
, B_TRUE
);
4531 /* if that worked, generate a real config for the new pool */
4532 if (newspa
->spa_root_vdev
!= NULL
) {
4533 VERIFY(nvlist_alloc(&newspa
->spa_config_splitting
,
4534 NV_UNIQUE_NAME
, KM_PUSHPAGE
) == 0);
4535 VERIFY(nvlist_add_uint64(newspa
->spa_config_splitting
,
4536 ZPOOL_CONFIG_SPLIT_GUID
, spa_guid(spa
)) == 0);
4537 spa_config_set(newspa
, spa_config_generate(newspa
, NULL
, -1ULL,
4542 if (props
!= NULL
) {
4543 spa_configfile_set(newspa
, props
, B_FALSE
);
4544 error
= spa_prop_set(newspa
, props
);
4549 /* flush everything */
4550 txg
= spa_vdev_config_enter(newspa
);
4551 vdev_config_dirty(newspa
->spa_root_vdev
);
4552 (void) spa_vdev_config_exit(newspa
, NULL
, txg
, 0, FTAG
);
4554 if (zio_injection_enabled
)
4555 zio_handle_panic_injection(spa
, FTAG
, 2);
4557 spa_async_resume(newspa
);
4559 /* finally, update the original pool's config */
4560 txg
= spa_vdev_config_enter(spa
);
4561 tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
4562 error
= dmu_tx_assign(tx
, TXG_WAIT
);
4565 for (c
= 0; c
< children
; c
++) {
4566 if (vml
[c
] != NULL
) {
4569 spa_history_log_internal(LOG_POOL_VDEV_DETACH
,
4575 vdev_config_dirty(spa
->spa_root_vdev
);
4576 spa
->spa_config_splitting
= NULL
;
4580 (void) spa_vdev_exit(spa
, NULL
, txg
, 0);
4582 if (zio_injection_enabled
)
4583 zio_handle_panic_injection(spa
, FTAG
, 3);
4585 /* split is complete; log a history record */
4586 spa_history_log_internal(LOG_POOL_SPLIT
, newspa
, NULL
,
4587 "split new pool %s from pool %s", newname
, spa_name(spa
));
4589 kmem_free(vml
, children
* sizeof (vdev_t
*));
4591 /* if we're not going to mount the filesystems in userland, export */
4593 error
= spa_export_common(newname
, POOL_STATE_EXPORTED
, NULL
,
4600 spa_deactivate(newspa
);
4603 txg
= spa_vdev_config_enter(spa
);
4605 /* re-online all offlined disks */
4606 for (c
= 0; c
< children
; c
++) {
4608 vml
[c
]->vdev_offline
= B_FALSE
;
4610 vdev_reopen(spa
->spa_root_vdev
);
4612 nvlist_free(spa
->spa_config_splitting
);
4613 spa
->spa_config_splitting
= NULL
;
4614 (void) spa_vdev_exit(spa
, NULL
, txg
, error
);
4616 kmem_free(vml
, children
* sizeof (vdev_t
*));
4621 spa_nvlist_lookup_by_guid(nvlist_t
**nvpp
, int count
, uint64_t target_guid
)
4625 for (i
= 0; i
< count
; i
++) {
4628 VERIFY(nvlist_lookup_uint64(nvpp
[i
], ZPOOL_CONFIG_GUID
,
4631 if (guid
== target_guid
)
4639 spa_vdev_remove_aux(nvlist_t
*config
, char *name
, nvlist_t
**dev
, int count
,
4640 nvlist_t
*dev_to_remove
)
4642 nvlist_t
**newdev
= NULL
;
4646 newdev
= kmem_alloc((count
- 1) * sizeof (void *), KM_PUSHPAGE
);
4648 for (i
= 0, j
= 0; i
< count
; i
++) {
4649 if (dev
[i
] == dev_to_remove
)
4651 VERIFY(nvlist_dup(dev
[i
], &newdev
[j
++], KM_PUSHPAGE
) == 0);
4654 VERIFY(nvlist_remove(config
, name
, DATA_TYPE_NVLIST_ARRAY
) == 0);
4655 VERIFY(nvlist_add_nvlist_array(config
, name
, newdev
, count
- 1) == 0);
4657 for (i
= 0; i
< count
- 1; i
++)
4658 nvlist_free(newdev
[i
]);
4661 kmem_free(newdev
, (count
- 1) * sizeof (void *));
4665 * Evacuate the device.
4668 spa_vdev_remove_evacuate(spa_t
*spa
, vdev_t
*vd
)
4673 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
4674 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
4675 ASSERT(vd
== vd
->vdev_top
);
4678 * Evacuate the device. We don't hold the config lock as writer
4679 * since we need to do I/O but we do keep the
4680 * spa_namespace_lock held. Once this completes the device
4681 * should no longer have any blocks allocated on it.
4683 if (vd
->vdev_islog
) {
4684 if (vd
->vdev_stat
.vs_alloc
!= 0)
4685 error
= spa_offline_log(spa
);
4694 * The evacuation succeeded. Remove any remaining MOS metadata
4695 * associated with this vdev, and wait for these changes to sync.
4697 ASSERT3U(vd
->vdev_stat
.vs_alloc
, ==, 0);
4698 txg
= spa_vdev_config_enter(spa
);
4699 vd
->vdev_removing
= B_TRUE
;
4700 vdev_dirty(vd
, 0, NULL
, txg
);
4701 vdev_config_dirty(vd
);
4702 spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
4708 * Complete the removal by cleaning up the namespace.
4711 spa_vdev_remove_from_namespace(spa_t
*spa
, vdev_t
*vd
)
4713 vdev_t
*rvd
= spa
->spa_root_vdev
;
4714 uint64_t id
= vd
->vdev_id
;
4715 boolean_t last_vdev
= (id
== (rvd
->vdev_children
- 1));
4717 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
4718 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
4719 ASSERT(vd
== vd
->vdev_top
);
4722 * Only remove any devices which are empty.
4724 if (vd
->vdev_stat
.vs_alloc
!= 0)
4727 (void) vdev_label_init(vd
, 0, VDEV_LABEL_REMOVE
);
4729 if (list_link_active(&vd
->vdev_state_dirty_node
))
4730 vdev_state_clean(vd
);
4731 if (list_link_active(&vd
->vdev_config_dirty_node
))
4732 vdev_config_clean(vd
);
4737 vdev_compact_children(rvd
);
4739 vd
= vdev_alloc_common(spa
, id
, 0, &vdev_hole_ops
);
4740 vdev_add_child(rvd
, vd
);
4742 vdev_config_dirty(rvd
);
4745 * Reassess the health of our root vdev.
4751 * Remove a device from the pool -
4753 * Removing a device from the vdev namespace requires several steps
4754 * and can take a significant amount of time. As a result we use
4755 * the spa_vdev_config_[enter/exit] functions which allow us to
4756 * grab and release the spa_config_lock while still holding the namespace
4757 * lock. During each step the configuration is synced out.
4761 * Remove a device from the pool. Currently, this supports removing only hot
4762 * spares, slogs, and level 2 ARC devices.
4765 spa_vdev_remove(spa_t
*spa
, uint64_t guid
, boolean_t unspare
)
4768 metaslab_group_t
*mg
;
4769 nvlist_t
**spares
, **l2cache
, *nv
;
4771 uint_t nspares
, nl2cache
;
4773 boolean_t locked
= MUTEX_HELD(&spa_namespace_lock
);
4775 ASSERT(spa_writeable(spa
));
4778 txg
= spa_vdev_enter(spa
);
4780 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
4782 if (spa
->spa_spares
.sav_vdevs
!= NULL
&&
4783 nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
4784 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0 &&
4785 (nv
= spa_nvlist_lookup_by_guid(spares
, nspares
, guid
)) != NULL
) {
4787 * Only remove the hot spare if it's not currently in use
4790 if (vd
== NULL
|| unspare
) {
4791 spa_vdev_remove_aux(spa
->spa_spares
.sav_config
,
4792 ZPOOL_CONFIG_SPARES
, spares
, nspares
, nv
);
4793 spa_load_spares(spa
);
4794 spa
->spa_spares
.sav_sync
= B_TRUE
;
4798 } else if (spa
->spa_l2cache
.sav_vdevs
!= NULL
&&
4799 nvlist_lookup_nvlist_array(spa
->spa_l2cache
.sav_config
,
4800 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0 &&
4801 (nv
= spa_nvlist_lookup_by_guid(l2cache
, nl2cache
, guid
)) != NULL
) {
4803 * Cache devices can always be removed.
4805 spa_vdev_remove_aux(spa
->spa_l2cache
.sav_config
,
4806 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
, nv
);
4807 spa_load_l2cache(spa
);
4808 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
4809 } else if (vd
!= NULL
&& vd
->vdev_islog
) {
4811 ASSERT(vd
== vd
->vdev_top
);
4814 * XXX - Once we have bp-rewrite this should
4815 * become the common case.
4821 * Stop allocating from this vdev.
4823 metaslab_group_passivate(mg
);
4826 * Wait for the youngest allocations and frees to sync,
4827 * and then wait for the deferral of those frees to finish.
4829 spa_vdev_config_exit(spa
, NULL
,
4830 txg
+ TXG_CONCURRENT_STATES
+ TXG_DEFER_SIZE
, 0, FTAG
);
4833 * Attempt to evacuate the vdev.
4835 error
= spa_vdev_remove_evacuate(spa
, vd
);
4837 txg
= spa_vdev_config_enter(spa
);
4840 * If we couldn't evacuate the vdev, unwind.
4843 metaslab_group_activate(mg
);
4844 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
4848 * Clean up the vdev namespace.
4850 spa_vdev_remove_from_namespace(spa
, vd
);
4852 } else if (vd
!= NULL
) {
4854 * Normal vdevs cannot be removed (yet).
4859 * There is no vdev of any kind with the specified guid.
4865 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
4871 * Find any device that's done replacing, or a vdev marked 'unspare' that's
4872 * current spared, so we can detach it.
4875 spa_vdev_resilver_done_hunt(vdev_t
*vd
)
4877 vdev_t
*newvd
, *oldvd
;
4880 for (c
= 0; c
< vd
->vdev_children
; c
++) {
4881 oldvd
= spa_vdev_resilver_done_hunt(vd
->vdev_child
[c
]);
4887 * Check for a completed replacement. We always consider the first
4888 * vdev in the list to be the oldest vdev, and the last one to be
4889 * the newest (see spa_vdev_attach() for how that works). In
4890 * the case where the newest vdev is faulted, we will not automatically
4891 * remove it after a resilver completes. This is OK as it will require
4892 * user intervention to determine which disk the admin wishes to keep.
4894 if (vd
->vdev_ops
== &vdev_replacing_ops
) {
4895 ASSERT(vd
->vdev_children
> 1);
4897 newvd
= vd
->vdev_child
[vd
->vdev_children
- 1];
4898 oldvd
= vd
->vdev_child
[0];
4900 if (vdev_dtl_empty(newvd
, DTL_MISSING
) &&
4901 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
4902 !vdev_dtl_required(oldvd
))
4907 * Check for a completed resilver with the 'unspare' flag set.
4909 if (vd
->vdev_ops
== &vdev_spare_ops
) {
4910 vdev_t
*first
= vd
->vdev_child
[0];
4911 vdev_t
*last
= vd
->vdev_child
[vd
->vdev_children
- 1];
4913 if (last
->vdev_unspare
) {
4916 } else if (first
->vdev_unspare
) {
4923 if (oldvd
!= NULL
&&
4924 vdev_dtl_empty(newvd
, DTL_MISSING
) &&
4925 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
4926 !vdev_dtl_required(oldvd
))
4930 * If there are more than two spares attached to a disk,
4931 * and those spares are not required, then we want to
4932 * attempt to free them up now so that they can be used
4933 * by other pools. Once we're back down to a single
4934 * disk+spare, we stop removing them.
4936 if (vd
->vdev_children
> 2) {
4937 newvd
= vd
->vdev_child
[1];
4939 if (newvd
->vdev_isspare
&& last
->vdev_isspare
&&
4940 vdev_dtl_empty(last
, DTL_MISSING
) &&
4941 vdev_dtl_empty(last
, DTL_OUTAGE
) &&
4942 !vdev_dtl_required(newvd
))
4951 spa_vdev_resilver_done(spa_t
*spa
)
4953 vdev_t
*vd
, *pvd
, *ppvd
;
4954 uint64_t guid
, sguid
, pguid
, ppguid
;
4956 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4958 while ((vd
= spa_vdev_resilver_done_hunt(spa
->spa_root_vdev
)) != NULL
) {
4959 pvd
= vd
->vdev_parent
;
4960 ppvd
= pvd
->vdev_parent
;
4961 guid
= vd
->vdev_guid
;
4962 pguid
= pvd
->vdev_guid
;
4963 ppguid
= ppvd
->vdev_guid
;
4966 * If we have just finished replacing a hot spared device, then
4967 * we need to detach the parent's first child (the original hot
4970 if (ppvd
->vdev_ops
== &vdev_spare_ops
&& pvd
->vdev_id
== 0 &&
4971 ppvd
->vdev_children
== 2) {
4972 ASSERT(pvd
->vdev_ops
== &vdev_replacing_ops
);
4973 sguid
= ppvd
->vdev_child
[1]->vdev_guid
;
4975 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4976 if (spa_vdev_detach(spa
, guid
, pguid
, B_TRUE
) != 0)
4978 if (sguid
&& spa_vdev_detach(spa
, sguid
, ppguid
, B_TRUE
) != 0)
4980 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4983 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4987 * Update the stored path or FRU for this vdev.
4990 spa_vdev_set_common(spa_t
*spa
, uint64_t guid
, const char *value
,
4994 boolean_t sync
= B_FALSE
;
4996 ASSERT(spa_writeable(spa
));
4998 spa_vdev_state_enter(spa
, SCL_ALL
);
5000 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
5001 return (spa_vdev_state_exit(spa
, NULL
, ENOENT
));
5003 if (!vd
->vdev_ops
->vdev_op_leaf
)
5004 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
5007 if (strcmp(value
, vd
->vdev_path
) != 0) {
5008 spa_strfree(vd
->vdev_path
);
5009 vd
->vdev_path
= spa_strdup(value
);
5013 if (vd
->vdev_fru
== NULL
) {
5014 vd
->vdev_fru
= spa_strdup(value
);
5016 } else if (strcmp(value
, vd
->vdev_fru
) != 0) {
5017 spa_strfree(vd
->vdev_fru
);
5018 vd
->vdev_fru
= spa_strdup(value
);
5023 return (spa_vdev_state_exit(spa
, sync
? vd
: NULL
, 0));
5027 spa_vdev_setpath(spa_t
*spa
, uint64_t guid
, const char *newpath
)
5029 return (spa_vdev_set_common(spa
, guid
, newpath
, B_TRUE
));
5033 spa_vdev_setfru(spa_t
*spa
, uint64_t guid
, const char *newfru
)
5035 return (spa_vdev_set_common(spa
, guid
, newfru
, B_FALSE
));
5039 * ==========================================================================
5041 * ==========================================================================
5045 spa_scan_stop(spa_t
*spa
)
5047 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
5048 if (dsl_scan_resilvering(spa
->spa_dsl_pool
))
5050 return (dsl_scan_cancel(spa
->spa_dsl_pool
));
5054 spa_scan(spa_t
*spa
, pool_scan_func_t func
)
5056 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
5058 if (func
>= POOL_SCAN_FUNCS
|| func
== POOL_SCAN_NONE
)
5062 * If a resilver was requested, but there is no DTL on a
5063 * writeable leaf device, we have nothing to do.
5065 if (func
== POOL_SCAN_RESILVER
&&
5066 !vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
)) {
5067 spa_async_request(spa
, SPA_ASYNC_RESILVER_DONE
);
5071 return (dsl_scan(spa
->spa_dsl_pool
, func
));
5075 * ==========================================================================
5076 * SPA async task processing
5077 * ==========================================================================
5081 spa_async_remove(spa_t
*spa
, vdev_t
*vd
)
5085 if (vd
->vdev_remove_wanted
) {
5086 vd
->vdev_remove_wanted
= B_FALSE
;
5087 vd
->vdev_delayed_close
= B_FALSE
;
5088 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_REMOVED
, VDEV_AUX_NONE
);
5091 * We want to clear the stats, but we don't want to do a full
5092 * vdev_clear() as that will cause us to throw away
5093 * degraded/faulted state as well as attempt to reopen the
5094 * device, all of which is a waste.
5096 vd
->vdev_stat
.vs_read_errors
= 0;
5097 vd
->vdev_stat
.vs_write_errors
= 0;
5098 vd
->vdev_stat
.vs_checksum_errors
= 0;
5100 vdev_state_dirty(vd
->vdev_top
);
5103 for (c
= 0; c
< vd
->vdev_children
; c
++)
5104 spa_async_remove(spa
, vd
->vdev_child
[c
]);
5108 spa_async_probe(spa_t
*spa
, vdev_t
*vd
)
5112 if (vd
->vdev_probe_wanted
) {
5113 vd
->vdev_probe_wanted
= B_FALSE
;
5114 vdev_reopen(vd
); /* vdev_open() does the actual probe */
5117 for (c
= 0; c
< vd
->vdev_children
; c
++)
5118 spa_async_probe(spa
, vd
->vdev_child
[c
]);
5122 spa_async_autoexpand(spa_t
*spa
, vdev_t
*vd
)
5126 if (!spa
->spa_autoexpand
)
5129 for (c
= 0; c
< vd
->vdev_children
; c
++) {
5130 vdev_t
*cvd
= vd
->vdev_child
[c
];
5131 spa_async_autoexpand(spa
, cvd
);
5134 if (!vd
->vdev_ops
->vdev_op_leaf
|| vd
->vdev_physpath
== NULL
)
5137 spa_event_notify(vd
->vdev_spa
, vd
, FM_EREPORT_ZFS_DEVICE_AUTOEXPAND
);
5141 spa_async_thread(spa_t
*spa
)
5145 ASSERT(spa
->spa_sync_on
);
5147 mutex_enter(&spa
->spa_async_lock
);
5148 tasks
= spa
->spa_async_tasks
;
5149 spa
->spa_async_tasks
= 0;
5150 mutex_exit(&spa
->spa_async_lock
);
5153 * See if the config needs to be updated.
5155 if (tasks
& SPA_ASYNC_CONFIG_UPDATE
) {
5156 uint64_t old_space
, new_space
;
5158 mutex_enter(&spa_namespace_lock
);
5159 old_space
= metaslab_class_get_space(spa_normal_class(spa
));
5160 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
5161 new_space
= metaslab_class_get_space(spa_normal_class(spa
));
5162 mutex_exit(&spa_namespace_lock
);
5165 * If the pool grew as a result of the config update,
5166 * then log an internal history event.
5168 if (new_space
!= old_space
) {
5169 spa_history_log_internal(LOG_POOL_VDEV_ONLINE
,
5171 "pool '%s' size: %llu(+%llu)",
5172 spa_name(spa
), new_space
, new_space
- old_space
);
5177 * See if any devices need to be marked REMOVED.
5179 if (tasks
& SPA_ASYNC_REMOVE
) {
5180 spa_vdev_state_enter(spa
, SCL_NONE
);
5181 spa_async_remove(spa
, spa
->spa_root_vdev
);
5182 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++)
5183 spa_async_remove(spa
, spa
->spa_l2cache
.sav_vdevs
[i
]);
5184 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
5185 spa_async_remove(spa
, spa
->spa_spares
.sav_vdevs
[i
]);
5186 (void) spa_vdev_state_exit(spa
, NULL
, 0);
5189 if ((tasks
& SPA_ASYNC_AUTOEXPAND
) && !spa_suspended(spa
)) {
5190 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
5191 spa_async_autoexpand(spa
, spa
->spa_root_vdev
);
5192 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
5196 * See if any devices need to be probed.
5198 if (tasks
& SPA_ASYNC_PROBE
) {
5199 spa_vdev_state_enter(spa
, SCL_NONE
);
5200 spa_async_probe(spa
, spa
->spa_root_vdev
);
5201 (void) spa_vdev_state_exit(spa
, NULL
, 0);
5205 * If any devices are done replacing, detach them.
5207 if (tasks
& SPA_ASYNC_RESILVER_DONE
)
5208 spa_vdev_resilver_done(spa
);
5211 * Kick off a resilver.
5213 if (tasks
& SPA_ASYNC_RESILVER
)
5214 dsl_resilver_restart(spa
->spa_dsl_pool
, 0);
5217 * Let the world know that we're done.
5219 mutex_enter(&spa
->spa_async_lock
);
5220 spa
->spa_async_thread
= NULL
;
5221 cv_broadcast(&spa
->spa_async_cv
);
5222 mutex_exit(&spa
->spa_async_lock
);
5227 spa_async_suspend(spa_t
*spa
)
5229 mutex_enter(&spa
->spa_async_lock
);
5230 spa
->spa_async_suspended
++;
5231 while (spa
->spa_async_thread
!= NULL
)
5232 cv_wait(&spa
->spa_async_cv
, &spa
->spa_async_lock
);
5233 mutex_exit(&spa
->spa_async_lock
);
5237 spa_async_resume(spa_t
*spa
)
5239 mutex_enter(&spa
->spa_async_lock
);
5240 ASSERT(spa
->spa_async_suspended
!= 0);
5241 spa
->spa_async_suspended
--;
5242 mutex_exit(&spa
->spa_async_lock
);
5246 spa_async_dispatch(spa_t
*spa
)
5248 mutex_enter(&spa
->spa_async_lock
);
5249 if (spa
->spa_async_tasks
&& !spa
->spa_async_suspended
&&
5250 spa
->spa_async_thread
== NULL
&&
5251 rootdir
!= NULL
&& !vn_is_readonly(rootdir
))
5252 spa
->spa_async_thread
= thread_create(NULL
, 0,
5253 spa_async_thread
, spa
, 0, &p0
, TS_RUN
, maxclsyspri
);
5254 mutex_exit(&spa
->spa_async_lock
);
5258 spa_async_request(spa_t
*spa
, int task
)
5260 zfs_dbgmsg("spa=%s async request task=%u", spa
->spa_name
, task
);
5261 mutex_enter(&spa
->spa_async_lock
);
5262 spa
->spa_async_tasks
|= task
;
5263 mutex_exit(&spa
->spa_async_lock
);
5267 * ==========================================================================
5268 * SPA syncing routines
5269 * ==========================================================================
5273 bpobj_enqueue_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
5276 bpobj_enqueue(bpo
, bp
, tx
);
5281 spa_free_sync_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
5285 zio_nowait(zio_free_sync(zio
, zio
->io_spa
, dmu_tx_get_txg(tx
), bp
,
5291 spa_sync_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
*nv
, dmu_tx_t
*tx
)
5293 char *packed
= NULL
;
5298 VERIFY(nvlist_size(nv
, &nvsize
, NV_ENCODE_XDR
) == 0);
5301 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
5302 * information. This avoids the dbuf_will_dirty() path and
5303 * saves us a pre-read to get data we don't actually care about.
5305 bufsize
= P2ROUNDUP(nvsize
, SPA_CONFIG_BLOCKSIZE
);
5306 packed
= vmem_alloc(bufsize
, KM_PUSHPAGE
);
5308 VERIFY(nvlist_pack(nv
, &packed
, &nvsize
, NV_ENCODE_XDR
,
5310 bzero(packed
+ nvsize
, bufsize
- nvsize
);
5312 dmu_write(spa
->spa_meta_objset
, obj
, 0, bufsize
, packed
, tx
);
5314 vmem_free(packed
, bufsize
);
5316 VERIFY(0 == dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
));
5317 dmu_buf_will_dirty(db
, tx
);
5318 *(uint64_t *)db
->db_data
= nvsize
;
5319 dmu_buf_rele(db
, FTAG
);
5323 spa_sync_aux_dev(spa_t
*spa
, spa_aux_vdev_t
*sav
, dmu_tx_t
*tx
,
5324 const char *config
, const char *entry
)
5334 * Update the MOS nvlist describing the list of available devices.
5335 * spa_validate_aux() will have already made sure this nvlist is
5336 * valid and the vdevs are labeled appropriately.
5338 if (sav
->sav_object
== 0) {
5339 sav
->sav_object
= dmu_object_alloc(spa
->spa_meta_objset
,
5340 DMU_OT_PACKED_NVLIST
, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE
,
5341 sizeof (uint64_t), tx
);
5342 VERIFY(zap_update(spa
->spa_meta_objset
,
5343 DMU_POOL_DIRECTORY_OBJECT
, entry
, sizeof (uint64_t), 1,
5344 &sav
->sav_object
, tx
) == 0);
5347 VERIFY(nvlist_alloc(&nvroot
, NV_UNIQUE_NAME
, KM_PUSHPAGE
) == 0);
5348 if (sav
->sav_count
== 0) {
5349 VERIFY(nvlist_add_nvlist_array(nvroot
, config
, NULL
, 0) == 0);
5351 list
= kmem_alloc(sav
->sav_count
* sizeof (void *), KM_PUSHPAGE
);
5352 for (i
= 0; i
< sav
->sav_count
; i
++)
5353 list
[i
] = vdev_config_generate(spa
, sav
->sav_vdevs
[i
],
5354 B_FALSE
, VDEV_CONFIG_L2CACHE
);
5355 VERIFY(nvlist_add_nvlist_array(nvroot
, config
, list
,
5356 sav
->sav_count
) == 0);
5357 for (i
= 0; i
< sav
->sav_count
; i
++)
5358 nvlist_free(list
[i
]);
5359 kmem_free(list
, sav
->sav_count
* sizeof (void *));
5362 spa_sync_nvlist(spa
, sav
->sav_object
, nvroot
, tx
);
5363 nvlist_free(nvroot
);
5365 sav
->sav_sync
= B_FALSE
;
5369 spa_sync_config_object(spa_t
*spa
, dmu_tx_t
*tx
)
5373 if (list_is_empty(&spa
->spa_config_dirty_list
))
5376 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
5378 config
= spa_config_generate(spa
, spa
->spa_root_vdev
,
5379 dmu_tx_get_txg(tx
), B_FALSE
);
5381 spa_config_exit(spa
, SCL_STATE
, FTAG
);
5383 if (spa
->spa_config_syncing
)
5384 nvlist_free(spa
->spa_config_syncing
);
5385 spa
->spa_config_syncing
= config
;
5387 spa_sync_nvlist(spa
, spa
->spa_config_object
, config
, tx
);
5391 * Set zpool properties.
5394 spa_sync_props(void *arg1
, void *arg2
, dmu_tx_t
*tx
)
5397 objset_t
*mos
= spa
->spa_meta_objset
;
5398 nvlist_t
*nvp
= arg2
;
5403 const char *propname
;
5404 zprop_type_t proptype
;
5406 mutex_enter(&spa
->spa_props_lock
);
5409 while ((elem
= nvlist_next_nvpair(nvp
, elem
))) {
5410 switch (prop
= zpool_name_to_prop(nvpair_name(elem
))) {
5411 case ZPOOL_PROP_VERSION
:
5413 * Only set version for non-zpool-creation cases
5414 * (set/import). spa_create() needs special care
5415 * for version setting.
5417 if (tx
->tx_txg
!= TXG_INITIAL
) {
5418 VERIFY(nvpair_value_uint64(elem
,
5420 ASSERT(intval
<= SPA_VERSION
);
5421 ASSERT(intval
>= spa_version(spa
));
5422 spa
->spa_uberblock
.ub_version
= intval
;
5423 vdev_config_dirty(spa
->spa_root_vdev
);
5427 case ZPOOL_PROP_ALTROOT
:
5429 * 'altroot' is a non-persistent property. It should
5430 * have been set temporarily at creation or import time.
5432 ASSERT(spa
->spa_root
!= NULL
);
5435 case ZPOOL_PROP_READONLY
:
5436 case ZPOOL_PROP_CACHEFILE
:
5438 * 'readonly' and 'cachefile' are also non-persisitent
5442 case ZPOOL_PROP_COMMENT
:
5443 VERIFY(nvpair_value_string(elem
, &strval
) == 0);
5444 if (spa
->spa_comment
!= NULL
)
5445 spa_strfree(spa
->spa_comment
);
5446 spa
->spa_comment
= spa_strdup(strval
);
5448 * We need to dirty the configuration on all the vdevs
5449 * so that their labels get updated. It's unnecessary
5450 * to do this for pool creation since the vdev's
5451 * configuratoin has already been dirtied.
5453 if (tx
->tx_txg
!= TXG_INITIAL
)
5454 vdev_config_dirty(spa
->spa_root_vdev
);
5458 * Set pool property values in the poolprops mos object.
5460 if (spa
->spa_pool_props_object
== 0) {
5461 VERIFY((spa
->spa_pool_props_object
=
5462 zap_create(mos
, DMU_OT_POOL_PROPS
,
5463 DMU_OT_NONE
, 0, tx
)) > 0);
5465 VERIFY(zap_update(mos
,
5466 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_PROPS
,
5467 8, 1, &spa
->spa_pool_props_object
, tx
)
5471 /* normalize the property name */
5472 propname
= zpool_prop_to_name(prop
);
5473 proptype
= zpool_prop_get_type(prop
);
5475 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
5476 ASSERT(proptype
== PROP_TYPE_STRING
);
5477 VERIFY(nvpair_value_string(elem
, &strval
) == 0);
5478 VERIFY(zap_update(mos
,
5479 spa
->spa_pool_props_object
, propname
,
5480 1, strlen(strval
) + 1, strval
, tx
) == 0);
5482 } else if (nvpair_type(elem
) == DATA_TYPE_UINT64
) {
5483 VERIFY(nvpair_value_uint64(elem
, &intval
) == 0);
5485 if (proptype
== PROP_TYPE_INDEX
) {
5487 VERIFY(zpool_prop_index_to_string(
5488 prop
, intval
, &unused
) == 0);
5490 VERIFY(zap_update(mos
,
5491 spa
->spa_pool_props_object
, propname
,
5492 8, 1, &intval
, tx
) == 0);
5494 ASSERT(0); /* not allowed */
5498 case ZPOOL_PROP_DELEGATION
:
5499 spa
->spa_delegation
= intval
;
5501 case ZPOOL_PROP_BOOTFS
:
5502 spa
->spa_bootfs
= intval
;
5504 case ZPOOL_PROP_FAILUREMODE
:
5505 spa
->spa_failmode
= intval
;
5507 case ZPOOL_PROP_AUTOEXPAND
:
5508 spa
->spa_autoexpand
= intval
;
5509 if (tx
->tx_txg
!= TXG_INITIAL
)
5510 spa_async_request(spa
,
5511 SPA_ASYNC_AUTOEXPAND
);
5513 case ZPOOL_PROP_DEDUPDITTO
:
5514 spa
->spa_dedup_ditto
= intval
;
5521 /* log internal history if this is not a zpool create */
5522 if (spa_version(spa
) >= SPA_VERSION_ZPOOL_HISTORY
&&
5523 tx
->tx_txg
!= TXG_INITIAL
) {
5524 spa_history_log_internal(LOG_POOL_PROPSET
,
5525 spa
, tx
, "%s %lld %s",
5526 nvpair_name(elem
), intval
, spa_name(spa
));
5530 mutex_exit(&spa
->spa_props_lock
);
5534 * Perform one-time upgrade on-disk changes. spa_version() does not
5535 * reflect the new version this txg, so there must be no changes this
5536 * txg to anything that the upgrade code depends on after it executes.
5537 * Therefore this must be called after dsl_pool_sync() does the sync
5541 spa_sync_upgrades(spa_t
*spa
, dmu_tx_t
*tx
)
5543 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
5545 ASSERT(spa
->spa_sync_pass
== 1);
5547 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_ORIGIN
&&
5548 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_ORIGIN
) {
5549 dsl_pool_create_origin(dp
, tx
);
5551 /* Keeping the origin open increases spa_minref */
5552 spa
->spa_minref
+= 3;
5555 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_NEXT_CLONES
&&
5556 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_NEXT_CLONES
) {
5557 dsl_pool_upgrade_clones(dp
, tx
);
5560 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_DIR_CLONES
&&
5561 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_DIR_CLONES
) {
5562 dsl_pool_upgrade_dir_clones(dp
, tx
);
5564 /* Keeping the freedir open increases spa_minref */
5565 spa
->spa_minref
+= 3;
5570 * Sync the specified transaction group. New blocks may be dirtied as
5571 * part of the process, so we iterate until it converges.
5574 spa_sync(spa_t
*spa
, uint64_t txg
)
5576 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
5577 objset_t
*mos
= spa
->spa_meta_objset
;
5578 bpobj_t
*defer_bpo
= &spa
->spa_deferred_bpobj
;
5579 bplist_t
*free_bpl
= &spa
->spa_free_bplist
[txg
& TXG_MASK
];
5580 vdev_t
*rvd
= spa
->spa_root_vdev
;
5586 VERIFY(spa_writeable(spa
));
5589 * Lock out configuration changes.
5591 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
5593 spa
->spa_syncing_txg
= txg
;
5594 spa
->spa_sync_pass
= 0;
5597 * If there are any pending vdev state changes, convert them
5598 * into config changes that go out with this transaction group.
5600 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
5601 while (list_head(&spa
->spa_state_dirty_list
) != NULL
) {
5603 * We need the write lock here because, for aux vdevs,
5604 * calling vdev_config_dirty() modifies sav_config.
5605 * This is ugly and will become unnecessary when we
5606 * eliminate the aux vdev wart by integrating all vdevs
5607 * into the root vdev tree.
5609 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
5610 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_WRITER
);
5611 while ((vd
= list_head(&spa
->spa_state_dirty_list
)) != NULL
) {
5612 vdev_state_clean(vd
);
5613 vdev_config_dirty(vd
);
5615 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
5616 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
5618 spa_config_exit(spa
, SCL_STATE
, FTAG
);
5620 tx
= dmu_tx_create_assigned(dp
, txg
);
5623 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
5624 * set spa_deflate if we have no raid-z vdevs.
5626 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_RAIDZ_DEFLATE
&&
5627 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
5630 for (i
= 0; i
< rvd
->vdev_children
; i
++) {
5631 vd
= rvd
->vdev_child
[i
];
5632 if (vd
->vdev_deflate_ratio
!= SPA_MINBLOCKSIZE
)
5635 if (i
== rvd
->vdev_children
) {
5636 spa
->spa_deflate
= TRUE
;
5637 VERIFY(0 == zap_add(spa
->spa_meta_objset
,
5638 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
5639 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
));
5644 * If anything has changed in this txg, or if someone is waiting
5645 * for this txg to sync (eg, spa_vdev_remove()), push the
5646 * deferred frees from the previous txg. If not, leave them
5647 * alone so that we don't generate work on an otherwise idle
5650 if (!txg_list_empty(&dp
->dp_dirty_datasets
, txg
) ||
5651 !txg_list_empty(&dp
->dp_dirty_dirs
, txg
) ||
5652 !txg_list_empty(&dp
->dp_sync_tasks
, txg
) ||
5653 ((dsl_scan_active(dp
->dp_scan
) ||
5654 txg_sync_waiting(dp
)) && !spa_shutting_down(spa
))) {
5655 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
5656 VERIFY3U(bpobj_iterate(defer_bpo
,
5657 spa_free_sync_cb
, zio
, tx
), ==, 0);
5658 VERIFY3U(zio_wait(zio
), ==, 0);
5662 * Iterate to convergence.
5665 int pass
= ++spa
->spa_sync_pass
;
5667 spa_sync_config_object(spa
, tx
);
5668 spa_sync_aux_dev(spa
, &spa
->spa_spares
, tx
,
5669 ZPOOL_CONFIG_SPARES
, DMU_POOL_SPARES
);
5670 spa_sync_aux_dev(spa
, &spa
->spa_l2cache
, tx
,
5671 ZPOOL_CONFIG_L2CACHE
, DMU_POOL_L2CACHE
);
5672 spa_errlog_sync(spa
, txg
);
5673 dsl_pool_sync(dp
, txg
);
5675 if (pass
<= SYNC_PASS_DEFERRED_FREE
) {
5676 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
5677 bplist_iterate(free_bpl
, spa_free_sync_cb
,
5679 VERIFY(zio_wait(zio
) == 0);
5681 bplist_iterate(free_bpl
, bpobj_enqueue_cb
,
5686 dsl_scan_sync(dp
, tx
);
5688 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, txg
)))
5692 spa_sync_upgrades(spa
, tx
);
5694 } while (dmu_objset_is_dirty(mos
, txg
));
5697 * Rewrite the vdev configuration (which includes the uberblock)
5698 * to commit the transaction group.
5700 * If there are no dirty vdevs, we sync the uberblock to a few
5701 * random top-level vdevs that are known to be visible in the
5702 * config cache (see spa_vdev_add() for a complete description).
5703 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
5707 * We hold SCL_STATE to prevent vdev open/close/etc.
5708 * while we're attempting to write the vdev labels.
5710 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
5712 if (list_is_empty(&spa
->spa_config_dirty_list
)) {
5713 vdev_t
*svd
[SPA_DVAS_PER_BP
];
5715 int children
= rvd
->vdev_children
;
5716 int c0
= spa_get_random(children
);
5718 for (c
= 0; c
< children
; c
++) {
5719 vd
= rvd
->vdev_child
[(c0
+ c
) % children
];
5720 if (vd
->vdev_ms_array
== 0 || vd
->vdev_islog
)
5722 svd
[svdcount
++] = vd
;
5723 if (svdcount
== SPA_DVAS_PER_BP
)
5726 error
= vdev_config_sync(svd
, svdcount
, txg
, B_FALSE
);
5728 error
= vdev_config_sync(svd
, svdcount
, txg
,
5731 error
= vdev_config_sync(rvd
->vdev_child
,
5732 rvd
->vdev_children
, txg
, B_FALSE
);
5734 error
= vdev_config_sync(rvd
->vdev_child
,
5735 rvd
->vdev_children
, txg
, B_TRUE
);
5738 spa_config_exit(spa
, SCL_STATE
, FTAG
);
5742 zio_suspend(spa
, NULL
);
5743 zio_resume_wait(spa
);
5748 * Clear the dirty config list.
5750 while ((vd
= list_head(&spa
->spa_config_dirty_list
)) != NULL
)
5751 vdev_config_clean(vd
);
5754 * Now that the new config has synced transactionally,
5755 * let it become visible to the config cache.
5757 if (spa
->spa_config_syncing
!= NULL
) {
5758 spa_config_set(spa
, spa
->spa_config_syncing
);
5759 spa
->spa_config_txg
= txg
;
5760 spa
->spa_config_syncing
= NULL
;
5763 spa
->spa_ubsync
= spa
->spa_uberblock
;
5765 dsl_pool_sync_done(dp
, txg
);
5768 * Update usable space statistics.
5770 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, TXG_CLEAN(txg
))))
5771 vdev_sync_done(vd
, txg
);
5773 spa_update_dspace(spa
);
5776 * It had better be the case that we didn't dirty anything
5777 * since vdev_config_sync().
5779 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
, txg
));
5780 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
5781 ASSERT(txg_list_empty(&spa
->spa_vdev_txg_list
, txg
));
5783 spa
->spa_sync_pass
= 0;
5785 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
5787 spa_handle_ignored_writes(spa
);
5790 * If any async tasks have been requested, kick them off.
5792 spa_async_dispatch(spa
);
5796 * Sync all pools. We don't want to hold the namespace lock across these
5797 * operations, so we take a reference on the spa_t and drop the lock during the
5801 spa_sync_allpools(void)
5804 mutex_enter(&spa_namespace_lock
);
5805 while ((spa
= spa_next(spa
)) != NULL
) {
5806 if (spa_state(spa
) != POOL_STATE_ACTIVE
||
5807 !spa_writeable(spa
) || spa_suspended(spa
))
5809 spa_open_ref(spa
, FTAG
);
5810 mutex_exit(&spa_namespace_lock
);
5811 txg_wait_synced(spa_get_dsl(spa
), 0);
5812 mutex_enter(&spa_namespace_lock
);
5813 spa_close(spa
, FTAG
);
5815 mutex_exit(&spa_namespace_lock
);
5819 * ==========================================================================
5820 * Miscellaneous routines
5821 * ==========================================================================
5825 * Remove all pools in the system.
5833 * Remove all cached state. All pools should be closed now,
5834 * so every spa in the AVL tree should be unreferenced.
5836 mutex_enter(&spa_namespace_lock
);
5837 while ((spa
= spa_next(NULL
)) != NULL
) {
5839 * Stop async tasks. The async thread may need to detach
5840 * a device that's been replaced, which requires grabbing
5841 * spa_namespace_lock, so we must drop it here.
5843 spa_open_ref(spa
, FTAG
);
5844 mutex_exit(&spa_namespace_lock
);
5845 spa_async_suspend(spa
);
5846 mutex_enter(&spa_namespace_lock
);
5847 spa_close(spa
, FTAG
);
5849 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
5851 spa_deactivate(spa
);
5855 mutex_exit(&spa_namespace_lock
);
5859 spa_lookup_by_guid(spa_t
*spa
, uint64_t guid
, boolean_t aux
)
5864 if ((vd
= vdev_lookup_by_guid(spa
->spa_root_vdev
, guid
)) != NULL
)
5868 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
5869 vd
= spa
->spa_l2cache
.sav_vdevs
[i
];
5870 if (vd
->vdev_guid
== guid
)
5874 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
5875 vd
= spa
->spa_spares
.sav_vdevs
[i
];
5876 if (vd
->vdev_guid
== guid
)
5885 spa_upgrade(spa_t
*spa
, uint64_t version
)
5887 ASSERT(spa_writeable(spa
));
5889 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5892 * This should only be called for a non-faulted pool, and since a
5893 * future version would result in an unopenable pool, this shouldn't be
5896 ASSERT(spa
->spa_uberblock
.ub_version
<= SPA_VERSION
);
5897 ASSERT(version
>= spa
->spa_uberblock
.ub_version
);
5899 spa
->spa_uberblock
.ub_version
= version
;
5900 vdev_config_dirty(spa
->spa_root_vdev
);
5902 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5904 txg_wait_synced(spa_get_dsl(spa
), 0);
5908 spa_has_spare(spa_t
*spa
, uint64_t guid
)
5912 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
5914 for (i
= 0; i
< sav
->sav_count
; i
++)
5915 if (sav
->sav_vdevs
[i
]->vdev_guid
== guid
)
5918 for (i
= 0; i
< sav
->sav_npending
; i
++) {
5919 if (nvlist_lookup_uint64(sav
->sav_pending
[i
], ZPOOL_CONFIG_GUID
,
5920 &spareguid
) == 0 && spareguid
== guid
)
5928 * Check if a pool has an active shared spare device.
5929 * Note: reference count of an active spare is 2, as a spare and as a replace
5932 spa_has_active_shared_spare(spa_t
*spa
)
5936 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
5938 for (i
= 0; i
< sav
->sav_count
; i
++) {
5939 if (spa_spare_exists(sav
->sav_vdevs
[i
]->vdev_guid
, &pool
,
5940 &refcnt
) && pool
!= 0ULL && pool
== spa_guid(spa
) &&
5949 * Post a FM_EREPORT_ZFS_* event from sys/fm/fs/zfs.h. The payload will be
5950 * filled in from the spa and (optionally) the vdev. This doesn't do anything
5951 * in the userland libzpool, as we don't want consumers to misinterpret ztest
5952 * or zdb as real changes.
5955 spa_event_notify(spa_t
*spa
, vdev_t
*vd
, const char *name
)
5958 zfs_ereport_post(name
, spa
, vd
, NULL
, 0, 0);
5962 #if defined(_KERNEL) && defined(HAVE_SPL)
5963 /* state manipulation functions */
5964 EXPORT_SYMBOL(spa_open
);
5965 EXPORT_SYMBOL(spa_open_rewind
);
5966 EXPORT_SYMBOL(spa_get_stats
);
5967 EXPORT_SYMBOL(spa_create
);
5968 EXPORT_SYMBOL(spa_import_rootpool
);
5969 EXPORT_SYMBOL(spa_import
);
5970 EXPORT_SYMBOL(spa_tryimport
);
5971 EXPORT_SYMBOL(spa_destroy
);
5972 EXPORT_SYMBOL(spa_export
);
5973 EXPORT_SYMBOL(spa_reset
);
5974 EXPORT_SYMBOL(spa_async_request
);
5975 EXPORT_SYMBOL(spa_async_suspend
);
5976 EXPORT_SYMBOL(spa_async_resume
);
5977 EXPORT_SYMBOL(spa_inject_addref
);
5978 EXPORT_SYMBOL(spa_inject_delref
);
5979 EXPORT_SYMBOL(spa_scan_stat_init
);
5980 EXPORT_SYMBOL(spa_scan_get_stats
);
5982 /* device maniion */
5983 EXPORT_SYMBOL(spa_vdev_add
);
5984 EXPORT_SYMBOL(spa_vdev_attach
);
5985 EXPORT_SYMBOL(spa_vdev_detach
);
5986 EXPORT_SYMBOL(spa_vdev_remove
);
5987 EXPORT_SYMBOL(spa_vdev_setpath
);
5988 EXPORT_SYMBOL(spa_vdev_setfru
);
5989 EXPORT_SYMBOL(spa_vdev_split_mirror
);
5991 /* spare statech is global across all pools) */
5992 EXPORT_SYMBOL(spa_spare_add
);
5993 EXPORT_SYMBOL(spa_spare_remove
);
5994 EXPORT_SYMBOL(spa_spare_exists
);
5995 EXPORT_SYMBOL(spa_spare_activate
);
5997 /* L2ARC statech is global across all pools) */
5998 EXPORT_SYMBOL(spa_l2cache_add
);
5999 EXPORT_SYMBOL(spa_l2cache_remove
);
6000 EXPORT_SYMBOL(spa_l2cache_exists
);
6001 EXPORT_SYMBOL(spa_l2cache_activate
);
6002 EXPORT_SYMBOL(spa_l2cache_drop
);
6005 EXPORT_SYMBOL(spa_scan
);
6006 EXPORT_SYMBOL(spa_scan_stop
);
6009 EXPORT_SYMBOL(spa_sync
); /* only for DMU use */
6010 EXPORT_SYMBOL(spa_sync_allpools
);
6013 EXPORT_SYMBOL(spa_prop_set
);
6014 EXPORT_SYMBOL(spa_prop_get
);
6015 EXPORT_SYMBOL(spa_prop_clear_bootfs
);
6017 /* asynchronous event notification */
6018 EXPORT_SYMBOL(spa_event_notify
);