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]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <sys/zfs_context.h>
27 #include <sys/spa_impl.h>
29 #include <sys/zio_checksum.h>
30 #include <sys/zio_compress.h>
32 #include <sys/dmu_tx.h>
35 #include <sys/vdev_impl.h>
36 #include <sys/metaslab.h>
37 #include <sys/uberblock_impl.h>
40 #include <sys/unique.h>
41 #include <sys/dsl_pool.h>
42 #include <sys/dsl_dir.h>
43 #include <sys/dsl_prop.h>
44 #include <sys/fs/zfs.h>
45 #include <sys/metaslab_impl.h>
46 #include <sys/sunddi.h>
54 * There are four basic locks for managing spa_t structures:
56 * spa_namespace_lock (global mutex)
58 * This lock must be acquired to do any of the following:
60 * - Lookup a spa_t by name
61 * - Add or remove a spa_t from the namespace
62 * - Increase spa_refcount from non-zero
63 * - Check if spa_refcount is zero
65 * - add/remove/attach/detach devices
66 * - Held for the duration of create/destroy/import/export
68 * It does not need to handle recursion. A create or destroy may
69 * reference objects (files or zvols) in other pools, but by
70 * definition they must have an existing reference, and will never need
71 * to lookup a spa_t by name.
73 * spa_refcount (per-spa refcount_t protected by mutex)
75 * This reference count keep track of any active users of the spa_t. The
76 * spa_t cannot be destroyed or freed while this is non-zero. Internally,
77 * the refcount is never really 'zero' - opening a pool implicitly keeps
78 * some references in the DMU. Internally we check against spa_minref, but
79 * present the image of a zero/non-zero value to consumers.
81 * spa_config_lock[] (per-spa array of rwlocks)
83 * This protects the spa_t from config changes, and must be held in
84 * the following circumstances:
86 * - RW_READER to perform I/O to the spa
87 * - RW_WRITER to change the vdev config
89 * The locking order is fairly straightforward:
91 * spa_namespace_lock -> spa_refcount
93 * The namespace lock must be acquired to increase the refcount from 0
94 * or to check if it is zero.
96 * spa_refcount -> spa_config_lock[]
98 * There must be at least one valid reference on the spa_t to acquire
101 * spa_namespace_lock -> spa_config_lock[]
103 * The namespace lock must always be taken before the config lock.
106 * The spa_namespace_lock can be acquired directly and is globally visible.
108 * The namespace is manipulated using the following functions, all of which
109 * require the spa_namespace_lock to be held.
111 * spa_lookup() Lookup a spa_t by name.
113 * spa_add() Create a new spa_t in the namespace.
115 * spa_remove() Remove a spa_t from the namespace. This also
116 * frees up any memory associated with the spa_t.
118 * spa_next() Returns the next spa_t in the system, or the
119 * first if NULL is passed.
121 * spa_evict_all() Shutdown and remove all spa_t structures in
124 * spa_guid_exists() Determine whether a pool/device guid exists.
126 * The spa_refcount is manipulated using the following functions:
128 * spa_open_ref() Adds a reference to the given spa_t. Must be
129 * called with spa_namespace_lock held if the
130 * refcount is currently zero.
132 * spa_close() Remove a reference from the spa_t. This will
133 * not free the spa_t or remove it from the
134 * namespace. No locking is required.
136 * spa_refcount_zero() Returns true if the refcount is currently
137 * zero. Must be called with spa_namespace_lock
140 * The spa_config_lock[] is an array of rwlocks, ordered as follows:
141 * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV.
142 * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}().
144 * To read the configuration, it suffices to hold one of these locks as reader.
145 * To modify the configuration, you must hold all locks as writer. To modify
146 * vdev state without altering the vdev tree's topology (e.g. online/offline),
147 * you must hold SCL_STATE and SCL_ZIO as writer.
149 * We use these distinct config locks to avoid recursive lock entry.
150 * For example, spa_sync() (which holds SCL_CONFIG as reader) induces
151 * block allocations (SCL_ALLOC), which may require reading space maps
152 * from disk (dmu_read() -> zio_read() -> SCL_ZIO).
154 * The spa config locks cannot be normal rwlocks because we need the
155 * ability to hand off ownership. For example, SCL_ZIO is acquired
156 * by the issuing thread and later released by an interrupt thread.
157 * They do, however, obey the usual write-wanted semantics to prevent
158 * writer (i.e. system administrator) starvation.
160 * The lock acquisition rules are as follows:
163 * Protects changes to the vdev tree topology, such as vdev
164 * add/remove/attach/detach. Protects the dirty config list
165 * (spa_config_dirty_list) and the set of spares and l2arc devices.
168 * Protects changes to pool state and vdev state, such as vdev
169 * online/offline/fault/degrade/clear. Protects the dirty state list
170 * (spa_state_dirty_list) and global pool state (spa_state).
173 * Protects changes to metaslab groups and classes.
174 * Held as reader by metaslab_alloc() and metaslab_claim().
177 * Held by bp-level zios (those which have no io_vd upon entry)
178 * to prevent changes to the vdev tree. The bp-level zio implicitly
179 * protects all of its vdev child zios, which do not hold SCL_ZIO.
182 * Protects changes to metaslab groups and classes.
183 * Held as reader by metaslab_free(). SCL_FREE is distinct from
184 * SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free
185 * blocks in zio_done() while another i/o that holds either
186 * SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete.
189 * Held as reader to prevent changes to the vdev tree during trivial
190 * inquiries such as bp_get_dsize(). SCL_VDEV is distinct from the
191 * other locks, and lower than all of them, to ensure that it's safe
192 * to acquire regardless of caller context.
194 * In addition, the following rules apply:
196 * (a) spa_props_lock protects pool properties, spa_config and spa_config_list.
197 * The lock ordering is SCL_CONFIG > spa_props_lock.
199 * (b) I/O operations on leaf vdevs. For any zio operation that takes
200 * an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(),
201 * or zio_write_phys() -- the caller must ensure that the config cannot
202 * cannot change in the interim, and that the vdev cannot be reopened.
203 * SCL_STATE as reader suffices for both.
205 * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
207 * spa_vdev_enter() Acquire the namespace lock and the config lock
210 * spa_vdev_exit() Release the config lock, wait for all I/O
211 * to complete, sync the updated configs to the
212 * cache, and release the namespace lock.
214 * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit().
215 * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual
216 * locking is, always, based on spa_namespace_lock and spa_config_lock[].
218 * spa_rename() is also implemented within this file since is requires
219 * manipulation of the namespace.
222 static avl_tree_t spa_namespace_avl
;
223 kmutex_t spa_namespace_lock
;
224 static kcondvar_t spa_namespace_cv
;
225 static int spa_active_count
;
226 int spa_max_replication_override
= SPA_DVAS_PER_BP
;
228 static kmutex_t spa_spare_lock
;
229 static avl_tree_t spa_spare_avl
;
230 static kmutex_t spa_l2cache_lock
;
231 static avl_tree_t spa_l2cache_avl
;
233 kmem_cache_t
*spa_buffer_pool
;
237 /* Everything except dprintf is on by default in debug builds */
238 int zfs_flags
= ~ZFS_DEBUG_DPRINTF
;
244 * zfs_recover can be set to nonzero to attempt to recover from
245 * otherwise-fatal errors, typically caused by on-disk corruption. When
246 * set, calls to zfs_panic_recover() will turn into warning messages.
252 * ==========================================================================
254 * ==========================================================================
257 spa_config_lock_init(spa_t
*spa
)
259 for (int i
= 0; i
< SCL_LOCKS
; i
++) {
260 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
261 mutex_init(&scl
->scl_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
262 cv_init(&scl
->scl_cv
, NULL
, CV_DEFAULT
, NULL
);
263 refcount_create(&scl
->scl_count
);
264 scl
->scl_writer
= NULL
;
265 scl
->scl_write_wanted
= 0;
270 spa_config_lock_destroy(spa_t
*spa
)
272 for (int i
= 0; i
< SCL_LOCKS
; i
++) {
273 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
274 mutex_destroy(&scl
->scl_lock
);
275 cv_destroy(&scl
->scl_cv
);
276 refcount_destroy(&scl
->scl_count
);
277 ASSERT(scl
->scl_writer
== NULL
);
278 ASSERT(scl
->scl_write_wanted
== 0);
283 spa_config_tryenter(spa_t
*spa
, int locks
, void *tag
, krw_t rw
)
285 for (int i
= 0; i
< SCL_LOCKS
; i
++) {
286 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
287 if (!(locks
& (1 << i
)))
289 mutex_enter(&scl
->scl_lock
);
290 if (rw
== RW_READER
) {
291 if (scl
->scl_writer
|| scl
->scl_write_wanted
) {
292 mutex_exit(&scl
->scl_lock
);
293 spa_config_exit(spa
, locks
^ (1 << i
), tag
);
297 ASSERT(scl
->scl_writer
!= curthread
);
298 if (!refcount_is_zero(&scl
->scl_count
)) {
299 mutex_exit(&scl
->scl_lock
);
300 spa_config_exit(spa
, locks
^ (1 << i
), tag
);
303 scl
->scl_writer
= curthread
;
305 (void) refcount_add(&scl
->scl_count
, tag
);
306 mutex_exit(&scl
->scl_lock
);
312 spa_config_enter(spa_t
*spa
, int locks
, void *tag
, krw_t rw
)
316 for (int i
= 0; i
< SCL_LOCKS
; i
++) {
317 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
318 if (scl
->scl_writer
== curthread
)
319 wlocks_held
|= (1 << i
);
320 if (!(locks
& (1 << i
)))
322 mutex_enter(&scl
->scl_lock
);
323 if (rw
== RW_READER
) {
324 while (scl
->scl_writer
|| scl
->scl_write_wanted
) {
325 cv_wait(&scl
->scl_cv
, &scl
->scl_lock
);
328 ASSERT(scl
->scl_writer
!= curthread
);
329 while (!refcount_is_zero(&scl
->scl_count
)) {
330 scl
->scl_write_wanted
++;
331 cv_wait(&scl
->scl_cv
, &scl
->scl_lock
);
332 scl
->scl_write_wanted
--;
334 scl
->scl_writer
= curthread
;
336 (void) refcount_add(&scl
->scl_count
, tag
);
337 mutex_exit(&scl
->scl_lock
);
339 ASSERT(wlocks_held
<= locks
);
343 spa_config_exit(spa_t
*spa
, int locks
, void *tag
)
345 for (int i
= SCL_LOCKS
- 1; i
>= 0; i
--) {
346 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
347 if (!(locks
& (1 << i
)))
349 mutex_enter(&scl
->scl_lock
);
350 ASSERT(!refcount_is_zero(&scl
->scl_count
));
351 if (refcount_remove(&scl
->scl_count
, tag
) == 0) {
352 ASSERT(scl
->scl_writer
== NULL
||
353 scl
->scl_writer
== curthread
);
354 scl
->scl_writer
= NULL
; /* OK in either case */
355 cv_broadcast(&scl
->scl_cv
);
357 mutex_exit(&scl
->scl_lock
);
362 spa_config_held(spa_t
*spa
, int locks
, krw_t rw
)
366 for (int i
= 0; i
< SCL_LOCKS
; i
++) {
367 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
368 if (!(locks
& (1 << i
)))
370 if ((rw
== RW_READER
&& !refcount_is_zero(&scl
->scl_count
)) ||
371 (rw
== RW_WRITER
&& scl
->scl_writer
== curthread
))
372 locks_held
|= 1 << i
;
379 * ==========================================================================
380 * SPA namespace functions
381 * ==========================================================================
385 * Lookup the named spa_t in the AVL tree. The spa_namespace_lock must be held.
386 * Returns NULL if no matching spa_t is found.
389 spa_lookup(const char *name
)
391 static spa_t search
; /* spa_t is large; don't allocate on stack */
397 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
400 * If it's a full dataset name, figure out the pool name and
403 cp
= strpbrk(name
, "/@");
409 (void) strlcpy(search
.spa_name
, name
, sizeof (search
.spa_name
));
410 spa
= avl_find(&spa_namespace_avl
, &search
, &where
);
419 * Create an uninitialized spa_t with the given name. Requires
420 * spa_namespace_lock. The caller must ensure that the spa_t doesn't already
421 * exist by calling spa_lookup() first.
424 spa_add(const char *name
, nvlist_t
*config
, const char *altroot
)
427 spa_config_dirent_t
*dp
;
429 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
431 spa
= kmem_zalloc(sizeof (spa_t
), KM_SLEEP
);
433 mutex_init(&spa
->spa_async_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
434 mutex_init(&spa
->spa_scrub_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
435 mutex_init(&spa
->spa_errlog_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
436 mutex_init(&spa
->spa_errlist_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
437 mutex_init(&spa
->spa_history_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
438 mutex_init(&spa
->spa_props_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
440 cv_init(&spa
->spa_async_cv
, NULL
, CV_DEFAULT
, NULL
);
441 cv_init(&spa
->spa_scrub_io_cv
, NULL
, CV_DEFAULT
, NULL
);
442 cv_init(&spa
->spa_suspend_cv
, NULL
, CV_DEFAULT
, NULL
);
444 for (int t
= 0; t
< TXG_SIZE
; t
++)
445 bplist_init(&spa
->spa_free_bplist
[t
]);
446 bplist_init(&spa
->spa_deferred_bplist
);
448 (void) strlcpy(spa
->spa_name
, name
, sizeof (spa
->spa_name
));
449 spa
->spa_state
= POOL_STATE_UNINITIALIZED
;
450 spa
->spa_freeze_txg
= UINT64_MAX
;
451 spa
->spa_final_txg
= UINT64_MAX
;
452 spa
->spa_load_max_txg
= UINT64_MAX
;
454 refcount_create(&spa
->spa_refcount
);
455 spa_config_lock_init(spa
);
457 avl_add(&spa_namespace_avl
, spa
);
459 mutex_init(&spa
->spa_suspend_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
462 * Set the alternate root, if there is one.
465 spa
->spa_root
= spa_strdup(altroot
);
470 * Every pool starts with the default cachefile
472 list_create(&spa
->spa_config_list
, sizeof (spa_config_dirent_t
),
473 offsetof(spa_config_dirent_t
, scd_link
));
475 dp
= kmem_zalloc(sizeof (spa_config_dirent_t
), KM_SLEEP
);
476 dp
->scd_path
= spa_strdup(spa_config_path
);
477 list_insert_head(&spa
->spa_config_list
, dp
);
480 VERIFY(nvlist_dup(config
, &spa
->spa_config
, 0) == 0);
486 * Removes a spa_t from the namespace, freeing up any memory used. Requires
487 * spa_namespace_lock. This is called only after the spa_t has been closed and
491 spa_remove(spa_t
*spa
)
493 spa_config_dirent_t
*dp
;
495 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
496 ASSERT(spa
->spa_state
== POOL_STATE_UNINITIALIZED
);
498 avl_remove(&spa_namespace_avl
, spa
);
499 cv_broadcast(&spa_namespace_cv
);
502 spa_strfree(spa
->spa_root
);
506 while ((dp
= list_head(&spa
->spa_config_list
)) != NULL
) {
507 list_remove(&spa
->spa_config_list
, dp
);
508 if (dp
->scd_path
!= NULL
)
509 spa_strfree(dp
->scd_path
);
510 kmem_free(dp
, sizeof (spa_config_dirent_t
));
513 list_destroy(&spa
->spa_config_list
);
515 spa_config_set(spa
, NULL
);
517 refcount_destroy(&spa
->spa_refcount
);
519 spa_config_lock_destroy(spa
);
521 for (int t
= 0; t
< TXG_SIZE
; t
++)
522 bplist_fini(&spa
->spa_free_bplist
[t
]);
523 bplist_fini(&spa
->spa_deferred_bplist
);
525 cv_destroy(&spa
->spa_async_cv
);
526 cv_destroy(&spa
->spa_scrub_io_cv
);
527 cv_destroy(&spa
->spa_suspend_cv
);
529 mutex_destroy(&spa
->spa_async_lock
);
530 mutex_destroy(&spa
->spa_scrub_lock
);
531 mutex_destroy(&spa
->spa_errlog_lock
);
532 mutex_destroy(&spa
->spa_errlist_lock
);
533 mutex_destroy(&spa
->spa_history_lock
);
534 mutex_destroy(&spa
->spa_props_lock
);
535 mutex_destroy(&spa
->spa_suspend_lock
);
537 kmem_free(spa
, sizeof (spa_t
));
541 * Given a pool, return the next pool in the namespace, or NULL if there is
542 * none. If 'prev' is NULL, return the first pool.
545 spa_next(spa_t
*prev
)
547 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
550 return (AVL_NEXT(&spa_namespace_avl
, prev
));
552 return (avl_first(&spa_namespace_avl
));
556 * ==========================================================================
557 * SPA refcount functions
558 * ==========================================================================
562 * Add a reference to the given spa_t. Must have at least one reference, or
563 * have the namespace lock held.
566 spa_open_ref(spa_t
*spa
, void *tag
)
568 ASSERT(refcount_count(&spa
->spa_refcount
) >= spa
->spa_minref
||
569 MUTEX_HELD(&spa_namespace_lock
));
570 (void) refcount_add(&spa
->spa_refcount
, tag
);
574 * Remove a reference to the given spa_t. Must have at least one reference, or
575 * have the namespace lock held.
578 spa_close(spa_t
*spa
, void *tag
)
580 ASSERT(refcount_count(&spa
->spa_refcount
) > spa
->spa_minref
||
581 MUTEX_HELD(&spa_namespace_lock
));
582 (void) refcount_remove(&spa
->spa_refcount
, tag
);
586 * Check to see if the spa refcount is zero. Must be called with
587 * spa_namespace_lock held. We really compare against spa_minref, which is the
588 * number of references acquired when opening a pool
591 spa_refcount_zero(spa_t
*spa
)
593 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
595 return (refcount_count(&spa
->spa_refcount
) == spa
->spa_minref
);
599 * ==========================================================================
600 * SPA spare and l2cache tracking
601 * ==========================================================================
605 * Hot spares and cache devices are tracked using the same code below,
606 * for 'auxiliary' devices.
609 typedef struct spa_aux
{
617 spa_aux_compare(const void *a
, const void *b
)
619 const spa_aux_t
*sa
= a
;
620 const spa_aux_t
*sb
= b
;
622 if (sa
->aux_guid
< sb
->aux_guid
)
624 else if (sa
->aux_guid
> sb
->aux_guid
)
631 spa_aux_add(vdev_t
*vd
, avl_tree_t
*avl
)
637 search
.aux_guid
= vd
->vdev_guid
;
638 if ((aux
= avl_find(avl
, &search
, &where
)) != NULL
) {
641 aux
= kmem_zalloc(sizeof (spa_aux_t
), KM_SLEEP
);
642 aux
->aux_guid
= vd
->vdev_guid
;
644 avl_insert(avl
, aux
, where
);
649 spa_aux_remove(vdev_t
*vd
, avl_tree_t
*avl
)
655 search
.aux_guid
= vd
->vdev_guid
;
656 aux
= avl_find(avl
, &search
, &where
);
660 if (--aux
->aux_count
== 0) {
661 avl_remove(avl
, aux
);
662 kmem_free(aux
, sizeof (spa_aux_t
));
663 } else if (aux
->aux_pool
== spa_guid(vd
->vdev_spa
)) {
664 aux
->aux_pool
= 0ULL;
669 spa_aux_exists(uint64_t guid
, uint64_t *pool
, int *refcnt
, avl_tree_t
*avl
)
671 spa_aux_t search
, *found
;
673 search
.aux_guid
= guid
;
674 found
= avl_find(avl
, &search
, NULL
);
678 *pool
= found
->aux_pool
;
685 *refcnt
= found
->aux_count
;
690 return (found
!= NULL
);
694 spa_aux_activate(vdev_t
*vd
, avl_tree_t
*avl
)
696 spa_aux_t search
, *found
;
699 search
.aux_guid
= vd
->vdev_guid
;
700 found
= avl_find(avl
, &search
, &where
);
701 ASSERT(found
!= NULL
);
702 ASSERT(found
->aux_pool
== 0ULL);
704 found
->aux_pool
= spa_guid(vd
->vdev_spa
);
708 * Spares are tracked globally due to the following constraints:
710 * - A spare may be part of multiple pools.
711 * - A spare may be added to a pool even if it's actively in use within
713 * - A spare in use in any pool can only be the source of a replacement if
714 * the target is a spare in the same pool.
716 * We keep track of all spares on the system through the use of a reference
717 * counted AVL tree. When a vdev is added as a spare, or used as a replacement
718 * spare, then we bump the reference count in the AVL tree. In addition, we set
719 * the 'vdev_isspare' member to indicate that the device is a spare (active or
720 * inactive). When a spare is made active (used to replace a device in the
721 * pool), we also keep track of which pool its been made a part of.
723 * The 'spa_spare_lock' protects the AVL tree. These functions are normally
724 * called under the spa_namespace lock as part of vdev reconfiguration. The
725 * separate spare lock exists for the status query path, which does not need to
726 * be completely consistent with respect to other vdev configuration changes.
730 spa_spare_compare(const void *a
, const void *b
)
732 return (spa_aux_compare(a
, b
));
736 spa_spare_add(vdev_t
*vd
)
738 mutex_enter(&spa_spare_lock
);
739 ASSERT(!vd
->vdev_isspare
);
740 spa_aux_add(vd
, &spa_spare_avl
);
741 vd
->vdev_isspare
= B_TRUE
;
742 mutex_exit(&spa_spare_lock
);
746 spa_spare_remove(vdev_t
*vd
)
748 mutex_enter(&spa_spare_lock
);
749 ASSERT(vd
->vdev_isspare
);
750 spa_aux_remove(vd
, &spa_spare_avl
);
751 vd
->vdev_isspare
= B_FALSE
;
752 mutex_exit(&spa_spare_lock
);
756 spa_spare_exists(uint64_t guid
, uint64_t *pool
, int *refcnt
)
760 mutex_enter(&spa_spare_lock
);
761 found
= spa_aux_exists(guid
, pool
, refcnt
, &spa_spare_avl
);
762 mutex_exit(&spa_spare_lock
);
768 spa_spare_activate(vdev_t
*vd
)
770 mutex_enter(&spa_spare_lock
);
771 ASSERT(vd
->vdev_isspare
);
772 spa_aux_activate(vd
, &spa_spare_avl
);
773 mutex_exit(&spa_spare_lock
);
777 * Level 2 ARC devices are tracked globally for the same reasons as spares.
778 * Cache devices currently only support one pool per cache device, and so
779 * for these devices the aux reference count is currently unused beyond 1.
783 spa_l2cache_compare(const void *a
, const void *b
)
785 return (spa_aux_compare(a
, b
));
789 spa_l2cache_add(vdev_t
*vd
)
791 mutex_enter(&spa_l2cache_lock
);
792 ASSERT(!vd
->vdev_isl2cache
);
793 spa_aux_add(vd
, &spa_l2cache_avl
);
794 vd
->vdev_isl2cache
= B_TRUE
;
795 mutex_exit(&spa_l2cache_lock
);
799 spa_l2cache_remove(vdev_t
*vd
)
801 mutex_enter(&spa_l2cache_lock
);
802 ASSERT(vd
->vdev_isl2cache
);
803 spa_aux_remove(vd
, &spa_l2cache_avl
);
804 vd
->vdev_isl2cache
= B_FALSE
;
805 mutex_exit(&spa_l2cache_lock
);
809 spa_l2cache_exists(uint64_t guid
, uint64_t *pool
)
813 mutex_enter(&spa_l2cache_lock
);
814 found
= spa_aux_exists(guid
, pool
, NULL
, &spa_l2cache_avl
);
815 mutex_exit(&spa_l2cache_lock
);
821 spa_l2cache_activate(vdev_t
*vd
)
823 mutex_enter(&spa_l2cache_lock
);
824 ASSERT(vd
->vdev_isl2cache
);
825 spa_aux_activate(vd
, &spa_l2cache_avl
);
826 mutex_exit(&spa_l2cache_lock
);
830 * ==========================================================================
832 * ==========================================================================
836 * Lock the given spa_t for the purpose of adding or removing a vdev.
837 * Grabs the global spa_namespace_lock plus the spa config lock for writing.
838 * It returns the next transaction group for the spa_t.
841 spa_vdev_enter(spa_t
*spa
)
843 mutex_enter(&spa_namespace_lock
);
844 return (spa_vdev_config_enter(spa
));
848 * Internal implementation for spa_vdev_enter(). Used when a vdev
849 * operation requires multiple syncs (i.e. removing a device) while
850 * keeping the spa_namespace_lock held.
853 spa_vdev_config_enter(spa_t
*spa
)
855 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
857 spa_config_enter(spa
, SCL_ALL
, spa
, RW_WRITER
);
859 return (spa_last_synced_txg(spa
) + 1);
863 * Used in combination with spa_vdev_config_enter() to allow the syncing
864 * of multiple transactions without releasing the spa_namespace_lock.
867 spa_vdev_config_exit(spa_t
*spa
, vdev_t
*vd
, uint64_t txg
, int error
, char *tag
)
869 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
871 int config_changed
= B_FALSE
;
873 ASSERT(txg
> spa_last_synced_txg(spa
));
875 spa
->spa_pending_vdev
= NULL
;
880 vdev_dtl_reassess(spa
->spa_root_vdev
, 0, 0, B_FALSE
);
883 * If the config changed, notify the scrub thread that it must restart.
885 if (error
== 0 && !list_is_empty(&spa
->spa_config_dirty_list
)) {
886 dsl_pool_scrub_restart(spa
->spa_dsl_pool
);
887 config_changed
= B_TRUE
;
888 spa
->spa_config_generation
++;
892 * Verify the metaslab classes.
894 ASSERT(metaslab_class_validate(spa_normal_class(spa
)) == 0);
895 ASSERT(metaslab_class_validate(spa_log_class(spa
)) == 0);
897 spa_config_exit(spa
, SCL_ALL
, spa
);
900 * Panic the system if the specified tag requires it. This
901 * is useful for ensuring that configurations are updated
904 if (zio_injection_enabled
)
905 zio_handle_panic_injection(spa
, tag
);
908 * Note: this txg_wait_synced() is important because it ensures
909 * that there won't be more than one config change per txg.
910 * This allows us to use the txg as the generation number.
913 txg_wait_synced(spa
->spa_dsl_pool
, txg
);
916 ASSERT(!vd
->vdev_detached
|| vd
->vdev_dtl_smo
.smo_object
== 0);
917 spa_config_enter(spa
, SCL_ALL
, spa
, RW_WRITER
);
919 spa_config_exit(spa
, SCL_ALL
, spa
);
923 * If the config changed, update the config cache.
926 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
930 * Unlock the spa_t after adding or removing a vdev. Besides undoing the
931 * locking of spa_vdev_enter(), we also want make sure the transactions have
932 * synced to disk, and then update the global configuration cache with the new
936 spa_vdev_exit(spa_t
*spa
, vdev_t
*vd
, uint64_t txg
, int error
)
938 spa_vdev_config_exit(spa
, vd
, txg
, error
, FTAG
);
939 mutex_exit(&spa_namespace_lock
);
945 * Lock the given spa_t for the purpose of changing vdev state.
948 spa_vdev_state_enter(spa_t
*spa
, int oplocks
)
950 int locks
= SCL_STATE_ALL
| oplocks
;
952 spa_config_enter(spa
, locks
, spa
, RW_WRITER
);
953 spa
->spa_vdev_locks
= locks
;
957 spa_vdev_state_exit(spa_t
*spa
, vdev_t
*vd
, int error
)
959 if (vd
!= NULL
|| error
== 0)
960 vdev_dtl_reassess(vd
? vd
->vdev_top
: spa
->spa_root_vdev
,
964 vdev_state_dirty(vd
->vdev_top
);
965 spa
->spa_config_generation
++;
968 ASSERT3U(spa
->spa_vdev_locks
, >=, SCL_STATE_ALL
);
969 spa_config_exit(spa
, spa
->spa_vdev_locks
, spa
);
972 * If anything changed, wait for it to sync. This ensures that,
973 * from the system administrator's perspective, zpool(1M) commands
974 * are synchronous. This is important for things like zpool offline:
975 * when the command completes, you expect no further I/O from ZFS.
978 txg_wait_synced(spa
->spa_dsl_pool
, 0);
984 * ==========================================================================
985 * Miscellaneous functions
986 * ==========================================================================
993 spa_rename(const char *name
, const char *newname
)
999 * Lookup the spa_t and grab the config lock for writing. We need to
1000 * actually open the pool so that we can sync out the necessary labels.
1001 * It's OK to call spa_open() with the namespace lock held because we
1002 * allow recursive calls for other reasons.
1004 mutex_enter(&spa_namespace_lock
);
1005 if ((err
= spa_open(name
, &spa
, FTAG
)) != 0) {
1006 mutex_exit(&spa_namespace_lock
);
1010 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1012 avl_remove(&spa_namespace_avl
, spa
);
1013 (void) strlcpy(spa
->spa_name
, newname
, sizeof (spa
->spa_name
));
1014 avl_add(&spa_namespace_avl
, spa
);
1017 * Sync all labels to disk with the new names by marking the root vdev
1018 * dirty and waiting for it to sync. It will pick up the new pool name
1021 vdev_config_dirty(spa
->spa_root_vdev
);
1023 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1025 txg_wait_synced(spa
->spa_dsl_pool
, 0);
1028 * Sync the updated config cache.
1030 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
1032 spa_close(spa
, FTAG
);
1034 mutex_exit(&spa_namespace_lock
);
1041 * Determine whether a pool with given pool_guid exists. If device_guid is
1042 * non-zero, determine whether the pool exists *and* contains a device with the
1043 * specified device_guid.
1046 spa_guid_exists(uint64_t pool_guid
, uint64_t device_guid
)
1049 avl_tree_t
*t
= &spa_namespace_avl
;
1051 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1053 for (spa
= avl_first(t
); spa
!= NULL
; spa
= AVL_NEXT(t
, spa
)) {
1054 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
)
1056 if (spa
->spa_root_vdev
== NULL
)
1058 if (spa_guid(spa
) == pool_guid
) {
1059 if (device_guid
== 0)
1062 if (vdev_lookup_by_guid(spa
->spa_root_vdev
,
1063 device_guid
) != NULL
)
1067 * Check any devices we may be in the process of adding.
1069 if (spa
->spa_pending_vdev
) {
1070 if (vdev_lookup_by_guid(spa
->spa_pending_vdev
,
1071 device_guid
) != NULL
)
1077 return (spa
!= NULL
);
1081 spa_strdup(const char *s
)
1087 new = kmem_alloc(len
+ 1, KM_SLEEP
);
1095 spa_strfree(char *s
)
1097 kmem_free(s
, strlen(s
) + 1);
1101 spa_get_random(uint64_t range
)
1107 (void) random_get_pseudo_bytes((void *)&r
, sizeof (uint64_t));
1113 sprintf_blkptr(char *buf
, const blkptr_t
*bp
)
1115 char *type
= dmu_ot
[BP_GET_TYPE(bp
)].ot_name
;
1116 char *checksum
= zio_checksum_table
[BP_GET_CHECKSUM(bp
)].ci_name
;
1117 char *compress
= zio_compress_table
[BP_GET_COMPRESS(bp
)].ci_name
;
1119 SPRINTF_BLKPTR(snprintf
, ' ', buf
, bp
, type
, checksum
, compress
);
1123 spa_freeze(spa_t
*spa
)
1125 uint64_t freeze_txg
= 0;
1127 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1128 if (spa
->spa_freeze_txg
== UINT64_MAX
) {
1129 freeze_txg
= spa_last_synced_txg(spa
) + TXG_SIZE
;
1130 spa
->spa_freeze_txg
= freeze_txg
;
1132 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1133 if (freeze_txg
!= 0)
1134 txg_wait_synced(spa_get_dsl(spa
), freeze_txg
);
1138 zfs_panic_recover(const char *fmt
, ...)
1143 vcmn_err(zfs_recover
? CE_WARN
: CE_PANIC
, fmt
, adx
);
1148 * ==========================================================================
1149 * Accessor functions
1150 * ==========================================================================
1154 spa_shutting_down(spa_t
*spa
)
1156 return (spa
->spa_async_suspended
);
1160 spa_get_dsl(spa_t
*spa
)
1162 return (spa
->spa_dsl_pool
);
1166 spa_get_rootblkptr(spa_t
*spa
)
1168 return (&spa
->spa_ubsync
.ub_rootbp
);
1172 spa_set_rootblkptr(spa_t
*spa
, const blkptr_t
*bp
)
1174 spa
->spa_uberblock
.ub_rootbp
= *bp
;
1178 spa_altroot(spa_t
*spa
, char *buf
, size_t buflen
)
1180 if (spa
->spa_root
== NULL
)
1183 (void) strncpy(buf
, spa
->spa_root
, buflen
);
1187 spa_sync_pass(spa_t
*spa
)
1189 return (spa
->spa_sync_pass
);
1193 spa_name(spa_t
*spa
)
1195 return (spa
->spa_name
);
1199 spa_guid(spa_t
*spa
)
1202 * If we fail to parse the config during spa_load(), we can go through
1203 * the error path (which posts an ereport) and end up here with no root
1204 * vdev. We stash the original pool guid in 'spa_load_guid' to handle
1207 if (spa
->spa_root_vdev
!= NULL
)
1208 return (spa
->spa_root_vdev
->vdev_guid
);
1210 return (spa
->spa_load_guid
);
1214 spa_last_synced_txg(spa_t
*spa
)
1216 return (spa
->spa_ubsync
.ub_txg
);
1220 spa_first_txg(spa_t
*spa
)
1222 return (spa
->spa_first_txg
);
1226 spa_syncing_txg(spa_t
*spa
)
1228 return (spa
->spa_syncing_txg
);
1232 spa_state(spa_t
*spa
)
1234 return (spa
->spa_state
);
1238 spa_freeze_txg(spa_t
*spa
)
1240 return (spa
->spa_freeze_txg
);
1245 spa_get_asize(spa_t
*spa
, uint64_t lsize
)
1248 * The worst case is single-sector max-parity RAID-Z blocks, in which
1249 * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1)
1250 * times the size; so just assume that. Add to this the fact that
1251 * we can have up to 3 DVAs per bp, and one more factor of 2 because
1252 * the block may be dittoed with up to 3 DVAs by ddt_sync().
1254 return (lsize
* (VDEV_RAIDZ_MAXPARITY
+ 1) * SPA_DVAS_PER_BP
* 2);
1258 spa_get_dspace(spa_t
*spa
)
1260 return (spa
->spa_dspace
);
1264 spa_update_dspace(spa_t
*spa
)
1266 spa
->spa_dspace
= metaslab_class_get_dspace(spa_normal_class(spa
)) +
1267 ddt_get_dedup_dspace(spa
);
1271 * Return the failure mode that has been set to this pool. The default
1272 * behavior will be to block all I/Os when a complete failure occurs.
1275 spa_get_failmode(spa_t
*spa
)
1277 return (spa
->spa_failmode
);
1281 spa_suspended(spa_t
*spa
)
1283 return (spa
->spa_suspended
);
1287 spa_version(spa_t
*spa
)
1289 return (spa
->spa_ubsync
.ub_version
);
1293 spa_deflate(spa_t
*spa
)
1295 return (spa
->spa_deflate
);
1299 spa_normal_class(spa_t
*spa
)
1301 return (spa
->spa_normal_class
);
1305 spa_log_class(spa_t
*spa
)
1307 return (spa
->spa_log_class
);
1311 spa_max_replication(spa_t
*spa
)
1314 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
1315 * handle BPs with more than one DVA allocated. Set our max
1316 * replication level accordingly.
1318 if (spa_version(spa
) < SPA_VERSION_DITTO_BLOCKS
)
1320 return (MIN(SPA_DVAS_PER_BP
, spa_max_replication_override
));
1324 dva_get_dsize_sync(spa_t
*spa
, const dva_t
*dva
)
1326 uint64_t asize
= DVA_GET_ASIZE(dva
);
1327 uint64_t dsize
= asize
;
1329 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_READER
) != 0);
1331 if (asize
!= 0 && spa
->spa_deflate
) {
1332 vdev_t
*vd
= vdev_lookup_top(spa
, DVA_GET_VDEV(dva
));
1333 dsize
= (asize
>> SPA_MINBLOCKSHIFT
) * vd
->vdev_deflate_ratio
;
1340 bp_get_dsize_sync(spa_t
*spa
, const blkptr_t
*bp
)
1344 for (int d
= 0; d
< SPA_DVAS_PER_BP
; d
++)
1345 dsize
+= dva_get_dsize_sync(spa
, &bp
->blk_dva
[d
]);
1351 bp_get_dsize(spa_t
*spa
, const blkptr_t
*bp
)
1355 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
1357 for (int d
= 0; d
< SPA_DVAS_PER_BP
; d
++)
1358 dsize
+= dva_get_dsize_sync(spa
, &bp
->blk_dva
[d
]);
1360 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
1366 * ==========================================================================
1367 * Initialization and Termination
1368 * ==========================================================================
1372 spa_name_compare(const void *a1
, const void *a2
)
1374 const spa_t
*s1
= a1
;
1375 const spa_t
*s2
= a2
;
1378 s
= strcmp(s1
->spa_name
, s2
->spa_name
);
1389 return (spa_active_count
);
1401 mutex_init(&spa_namespace_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1402 mutex_init(&spa_spare_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1403 mutex_init(&spa_l2cache_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1404 cv_init(&spa_namespace_cv
, NULL
, CV_DEFAULT
, NULL
);
1406 avl_create(&spa_namespace_avl
, spa_name_compare
, sizeof (spa_t
),
1407 offsetof(spa_t
, spa_avl
));
1409 avl_create(&spa_spare_avl
, spa_spare_compare
, sizeof (spa_aux_t
),
1410 offsetof(spa_aux_t
, aux_avl
));
1412 avl_create(&spa_l2cache_avl
, spa_l2cache_compare
, sizeof (spa_aux_t
),
1413 offsetof(spa_aux_t
, aux_avl
));
1415 spa_mode_global
= mode
;
1422 vdev_cache_stat_init();
1436 vdev_cache_stat_fini();
1443 avl_destroy(&spa_namespace_avl
);
1444 avl_destroy(&spa_spare_avl
);
1445 avl_destroy(&spa_l2cache_avl
);
1447 cv_destroy(&spa_namespace_cv
);
1448 mutex_destroy(&spa_namespace_lock
);
1449 mutex_destroy(&spa_spare_lock
);
1450 mutex_destroy(&spa_l2cache_lock
);
1454 * Return whether this pool has slogs. No locking needed.
1455 * It's not a problem if the wrong answer is returned as it's only for
1456 * performance and not correctness
1459 spa_has_slogs(spa_t
*spa
)
1461 return (spa
->spa_log_class
->mc_rotor
!= NULL
);
1465 spa_get_log_state(spa_t
*spa
)
1467 return (spa
->spa_log_state
);
1471 spa_set_log_state(spa_t
*spa
, spa_log_state_t state
)
1473 spa
->spa_log_state
= state
;
1477 spa_is_root(spa_t
*spa
)
1479 return (spa
->spa_is_root
);
1483 spa_writeable(spa_t
*spa
)
1485 return (!!(spa
->spa_mode
& FWRITE
));
1489 spa_mode(spa_t
*spa
)
1491 return (spa
->spa_mode
);
1495 spa_bootfs(spa_t
*spa
)
1497 return (spa
->spa_bootfs
);
1501 spa_delegation(spa_t
*spa
)
1503 return (spa
->spa_delegation
);
1507 spa_meta_objset(spa_t
*spa
)
1509 return (spa
->spa_meta_objset
);
1513 spa_dedup_checksum(spa_t
*spa
)
1515 return (spa
->spa_dedup_checksum
);