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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012 by Delphix. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
27 #include <sys/zfs_context.h>
28 #include <sys/spa_impl.h>
30 #include <sys/zio_checksum.h>
31 #include <sys/zio_compress.h>
33 #include <sys/dmu_tx.h>
36 #include <sys/vdev_impl.h>
37 #include <sys/metaslab.h>
38 #include <sys/uberblock_impl.h>
41 #include <sys/unique.h>
42 #include <sys/dsl_pool.h>
43 #include <sys/dsl_dir.h>
44 #include <sys/dsl_prop.h>
45 #include <sys/fm/util.h>
46 #include <sys/dsl_scan.h>
47 #include <sys/fs/zfs.h>
48 #include <sys/metaslab_impl.h>
52 #include "zfeature_common.h"
57 * There are four basic locks for managing spa_t structures:
59 * spa_namespace_lock (global mutex)
61 * This lock must be acquired to do any of the following:
63 * - Lookup a spa_t by name
64 * - Add or remove a spa_t from the namespace
65 * - Increase spa_refcount from non-zero
66 * - Check if spa_refcount is zero
68 * - add/remove/attach/detach devices
69 * - Held for the duration of create/destroy/import/export
71 * It does not need to handle recursion. A create or destroy may
72 * reference objects (files or zvols) in other pools, but by
73 * definition they must have an existing reference, and will never need
74 * to lookup a spa_t by name.
76 * spa_refcount (per-spa refcount_t protected by mutex)
78 * This reference count keep track of any active users of the spa_t. The
79 * spa_t cannot be destroyed or freed while this is non-zero. Internally,
80 * the refcount is never really 'zero' - opening a pool implicitly keeps
81 * some references in the DMU. Internally we check against spa_minref, but
82 * present the image of a zero/non-zero value to consumers.
84 * spa_config_lock[] (per-spa array of rwlocks)
86 * This protects the spa_t from config changes, and must be held in
87 * the following circumstances:
89 * - RW_READER to perform I/O to the spa
90 * - RW_WRITER to change the vdev config
92 * The locking order is fairly straightforward:
94 * spa_namespace_lock -> spa_refcount
96 * The namespace lock must be acquired to increase the refcount from 0
97 * or to check if it is zero.
99 * spa_refcount -> spa_config_lock[]
101 * There must be at least one valid reference on the spa_t to acquire
104 * spa_namespace_lock -> spa_config_lock[]
106 * The namespace lock must always be taken before the config lock.
109 * The spa_namespace_lock can be acquired directly and is globally visible.
111 * The namespace is manipulated using the following functions, all of which
112 * require the spa_namespace_lock to be held.
114 * spa_lookup() Lookup a spa_t by name.
116 * spa_add() Create a new spa_t in the namespace.
118 * spa_remove() Remove a spa_t from the namespace. This also
119 * frees up any memory associated with the spa_t.
121 * spa_next() Returns the next spa_t in the system, or the
122 * first if NULL is passed.
124 * spa_evict_all() Shutdown and remove all spa_t structures in
127 * spa_guid_exists() Determine whether a pool/device guid exists.
129 * The spa_refcount is manipulated using the following functions:
131 * spa_open_ref() Adds a reference to the given spa_t. Must be
132 * called with spa_namespace_lock held if the
133 * refcount is currently zero.
135 * spa_close() Remove a reference from the spa_t. This will
136 * not free the spa_t or remove it from the
137 * namespace. No locking is required.
139 * spa_refcount_zero() Returns true if the refcount is currently
140 * zero. Must be called with spa_namespace_lock
143 * The spa_config_lock[] is an array of rwlocks, ordered as follows:
144 * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV.
145 * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}().
147 * To read the configuration, it suffices to hold one of these locks as reader.
148 * To modify the configuration, you must hold all locks as writer. To modify
149 * vdev state without altering the vdev tree's topology (e.g. online/offline),
150 * you must hold SCL_STATE and SCL_ZIO as writer.
152 * We use these distinct config locks to avoid recursive lock entry.
153 * For example, spa_sync() (which holds SCL_CONFIG as reader) induces
154 * block allocations (SCL_ALLOC), which may require reading space maps
155 * from disk (dmu_read() -> zio_read() -> SCL_ZIO).
157 * The spa config locks cannot be normal rwlocks because we need the
158 * ability to hand off ownership. For example, SCL_ZIO is acquired
159 * by the issuing thread and later released by an interrupt thread.
160 * They do, however, obey the usual write-wanted semantics to prevent
161 * writer (i.e. system administrator) starvation.
163 * The lock acquisition rules are as follows:
166 * Protects changes to the vdev tree topology, such as vdev
167 * add/remove/attach/detach. Protects the dirty config list
168 * (spa_config_dirty_list) and the set of spares and l2arc devices.
171 * Protects changes to pool state and vdev state, such as vdev
172 * online/offline/fault/degrade/clear. Protects the dirty state list
173 * (spa_state_dirty_list) and global pool state (spa_state).
176 * Protects changes to metaslab groups and classes.
177 * Held as reader by metaslab_alloc() and metaslab_claim().
180 * Held by bp-level zios (those which have no io_vd upon entry)
181 * to prevent changes to the vdev tree. The bp-level zio implicitly
182 * protects all of its vdev child zios, which do not hold SCL_ZIO.
185 * Protects changes to metaslab groups and classes.
186 * Held as reader by metaslab_free(). SCL_FREE is distinct from
187 * SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free
188 * blocks in zio_done() while another i/o that holds either
189 * SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete.
192 * Held as reader to prevent changes to the vdev tree during trivial
193 * inquiries such as bp_get_dsize(). SCL_VDEV is distinct from the
194 * other locks, and lower than all of them, to ensure that it's safe
195 * to acquire regardless of caller context.
197 * In addition, the following rules apply:
199 * (a) spa_props_lock protects pool properties, spa_config and spa_config_list.
200 * The lock ordering is SCL_CONFIG > spa_props_lock.
202 * (b) I/O operations on leaf vdevs. For any zio operation that takes
203 * an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(),
204 * or zio_write_phys() -- the caller must ensure that the config cannot
205 * cannot change in the interim, and that the vdev cannot be reopened.
206 * SCL_STATE as reader suffices for both.
208 * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
210 * spa_vdev_enter() Acquire the namespace lock and the config lock
213 * spa_vdev_exit() Release the config lock, wait for all I/O
214 * to complete, sync the updated configs to the
215 * cache, and release the namespace lock.
217 * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit().
218 * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual
219 * locking is, always, based on spa_namespace_lock and spa_config_lock[].
221 * spa_rename() is also implemented within this file since it requires
222 * manipulation of the namespace.
225 static avl_tree_t spa_namespace_avl
;
226 kmutex_t spa_namespace_lock
;
227 static kcondvar_t spa_namespace_cv
;
228 static int spa_active_count
;
229 int spa_max_replication_override
= SPA_DVAS_PER_BP
;
231 static kmutex_t spa_spare_lock
;
232 static avl_tree_t spa_spare_avl
;
233 static kmutex_t spa_l2cache_lock
;
234 static avl_tree_t spa_l2cache_avl
;
236 kmem_cache_t
*spa_buffer_pool
;
240 * Expiration time in units of zfs_txg_synctime_ms. This value has two
241 * meanings. First it is used to determine when the spa_deadman logic
242 * should fire. By default the spa_deadman will fire if spa_sync has
243 * not completed in 1000 * zfs_txg_synctime_ms (i.e. 1000 seconds).
244 * Secondly, the value determines if an I/O is considered "hung".
245 * Any I/O that has not completed in zfs_deadman_synctime is considered
246 * "hung" resulting in a zevent being posted.
247 * 1000 zfs_txg_synctime_ms (i.e. 1000 seconds).
249 unsigned long zfs_deadman_synctime
= 1000ULL;
252 * By default the deadman is enabled.
254 int zfs_deadman_enabled
= 1;
258 * ==========================================================================
260 * ==========================================================================
263 spa_config_lock_init(spa_t
*spa
)
267 for (i
= 0; i
< SCL_LOCKS
; i
++) {
268 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
269 mutex_init(&scl
->scl_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
270 cv_init(&scl
->scl_cv
, NULL
, CV_DEFAULT
, NULL
);
271 refcount_create_untracked(&scl
->scl_count
);
272 scl
->scl_writer
= NULL
;
273 scl
->scl_write_wanted
= 0;
278 spa_config_lock_destroy(spa_t
*spa
)
282 for (i
= 0; i
< SCL_LOCKS
; i
++) {
283 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
284 mutex_destroy(&scl
->scl_lock
);
285 cv_destroy(&scl
->scl_cv
);
286 refcount_destroy(&scl
->scl_count
);
287 ASSERT(scl
->scl_writer
== NULL
);
288 ASSERT(scl
->scl_write_wanted
== 0);
293 spa_config_tryenter(spa_t
*spa
, int locks
, void *tag
, krw_t rw
)
297 for (i
= 0; i
< SCL_LOCKS
; i
++) {
298 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
299 if (!(locks
& (1 << i
)))
301 mutex_enter(&scl
->scl_lock
);
302 if (rw
== RW_READER
) {
303 if (scl
->scl_writer
|| scl
->scl_write_wanted
) {
304 mutex_exit(&scl
->scl_lock
);
305 spa_config_exit(spa
, locks
^ (1 << i
), tag
);
309 ASSERT(scl
->scl_writer
!= curthread
);
310 if (!refcount_is_zero(&scl
->scl_count
)) {
311 mutex_exit(&scl
->scl_lock
);
312 spa_config_exit(spa
, locks
^ (1 << i
), tag
);
315 scl
->scl_writer
= curthread
;
317 (void) refcount_add(&scl
->scl_count
, tag
);
318 mutex_exit(&scl
->scl_lock
);
324 spa_config_enter(spa_t
*spa
, int locks
, void *tag
, krw_t rw
)
329 ASSERT3U(SCL_LOCKS
, <, sizeof (wlocks_held
) * NBBY
);
331 for (i
= 0; i
< SCL_LOCKS
; i
++) {
332 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
333 if (scl
->scl_writer
== curthread
)
334 wlocks_held
|= (1 << i
);
335 if (!(locks
& (1 << i
)))
337 mutex_enter(&scl
->scl_lock
);
338 if (rw
== RW_READER
) {
339 while (scl
->scl_writer
|| scl
->scl_write_wanted
) {
340 cv_wait(&scl
->scl_cv
, &scl
->scl_lock
);
343 ASSERT(scl
->scl_writer
!= curthread
);
344 while (!refcount_is_zero(&scl
->scl_count
)) {
345 scl
->scl_write_wanted
++;
346 cv_wait(&scl
->scl_cv
, &scl
->scl_lock
);
347 scl
->scl_write_wanted
--;
349 scl
->scl_writer
= curthread
;
351 (void) refcount_add(&scl
->scl_count
, tag
);
352 mutex_exit(&scl
->scl_lock
);
354 ASSERT(wlocks_held
<= locks
);
358 spa_config_exit(spa_t
*spa
, int locks
, void *tag
)
362 for (i
= SCL_LOCKS
- 1; i
>= 0; i
--) {
363 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
364 if (!(locks
& (1 << i
)))
366 mutex_enter(&scl
->scl_lock
);
367 ASSERT(!refcount_is_zero(&scl
->scl_count
));
368 if (refcount_remove(&scl
->scl_count
, tag
) == 0) {
369 ASSERT(scl
->scl_writer
== NULL
||
370 scl
->scl_writer
== curthread
);
371 scl
->scl_writer
= NULL
; /* OK in either case */
372 cv_broadcast(&scl
->scl_cv
);
374 mutex_exit(&scl
->scl_lock
);
379 spa_config_held(spa_t
*spa
, int locks
, krw_t rw
)
381 int i
, locks_held
= 0;
383 for (i
= 0; i
< SCL_LOCKS
; i
++) {
384 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
385 if (!(locks
& (1 << i
)))
387 if ((rw
== RW_READER
&& !refcount_is_zero(&scl
->scl_count
)) ||
388 (rw
== RW_WRITER
&& scl
->scl_writer
== curthread
))
389 locks_held
|= 1 << i
;
396 * ==========================================================================
397 * SPA namespace functions
398 * ==========================================================================
402 * Lookup the named spa_t in the AVL tree. The spa_namespace_lock must be held.
403 * Returns NULL if no matching spa_t is found.
406 spa_lookup(const char *name
)
408 static spa_t search
; /* spa_t is large; don't allocate on stack */
413 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
415 (void) strlcpy(search
.spa_name
, name
, sizeof (search
.spa_name
));
418 * If it's a full dataset name, figure out the pool name and
421 cp
= strpbrk(search
.spa_name
, "/@");
425 spa
= avl_find(&spa_namespace_avl
, &search
, &where
);
431 * Fires when spa_sync has not completed within zfs_deadman_synctime_ms.
432 * If the zfs_deadman_enabled flag is set then it inspects all vdev queues
433 * looking for potentially hung I/Os.
436 spa_deadman(void *arg
)
440 zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu",
441 (gethrtime() - spa
->spa_sync_starttime
) / NANOSEC
,
442 ++spa
->spa_deadman_calls
);
443 if (zfs_deadman_enabled
)
444 vdev_deadman(spa
->spa_root_vdev
);
446 spa
->spa_deadman_tqid
= taskq_dispatch_delay(system_taskq
,
447 spa_deadman
, spa
, TQ_SLEEP
, ddi_get_lbolt() +
448 NSEC_TO_TICK(spa
->spa_deadman_synctime
));
452 * Create an uninitialized spa_t with the given name. Requires
453 * spa_namespace_lock. The caller must ensure that the spa_t doesn't already
454 * exist by calling spa_lookup() first.
457 spa_add(const char *name
, nvlist_t
*config
, const char *altroot
)
460 spa_config_dirent_t
*dp
;
463 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
465 spa
= kmem_zalloc(sizeof (spa_t
), KM_PUSHPAGE
| KM_NODEBUG
);
467 mutex_init(&spa
->spa_async_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
468 mutex_init(&spa
->spa_errlist_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
469 mutex_init(&spa
->spa_errlog_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
470 mutex_init(&spa
->spa_history_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
471 mutex_init(&spa
->spa_proc_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
472 mutex_init(&spa
->spa_props_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
473 mutex_init(&spa
->spa_scrub_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
474 mutex_init(&spa
->spa_suspend_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
475 mutex_init(&spa
->spa_vdev_top_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
477 cv_init(&spa
->spa_async_cv
, NULL
, CV_DEFAULT
, NULL
);
478 cv_init(&spa
->spa_proc_cv
, NULL
, CV_DEFAULT
, NULL
);
479 cv_init(&spa
->spa_scrub_io_cv
, NULL
, CV_DEFAULT
, NULL
);
480 cv_init(&spa
->spa_suspend_cv
, NULL
, CV_DEFAULT
, NULL
);
482 for (t
= 0; t
< TXG_SIZE
; t
++)
483 bplist_create(&spa
->spa_free_bplist
[t
]);
485 (void) strlcpy(spa
->spa_name
, name
, sizeof (spa
->spa_name
));
486 spa
->spa_state
= POOL_STATE_UNINITIALIZED
;
487 spa
->spa_freeze_txg
= UINT64_MAX
;
488 spa
->spa_final_txg
= UINT64_MAX
;
489 spa
->spa_load_max_txg
= UINT64_MAX
;
491 spa
->spa_proc_state
= SPA_PROC_NONE
;
493 spa
->spa_deadman_synctime
= zfs_deadman_synctime
*
494 zfs_txg_synctime_ms
* MICROSEC
;
496 refcount_create(&spa
->spa_refcount
);
497 spa_config_lock_init(spa
);
499 avl_add(&spa_namespace_avl
, spa
);
502 * Set the alternate root, if there is one.
505 spa
->spa_root
= spa_strdup(altroot
);
510 * Every pool starts with the default cachefile
512 list_create(&spa
->spa_config_list
, sizeof (spa_config_dirent_t
),
513 offsetof(spa_config_dirent_t
, scd_link
));
515 dp
= kmem_zalloc(sizeof (spa_config_dirent_t
), KM_PUSHPAGE
);
516 dp
->scd_path
= altroot
? NULL
: spa_strdup(spa_config_path
);
517 list_insert_head(&spa
->spa_config_list
, dp
);
519 VERIFY(nvlist_alloc(&spa
->spa_load_info
, NV_UNIQUE_NAME
,
522 if (config
!= NULL
) {
525 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_FEATURES_FOR_READ
,
527 VERIFY(nvlist_dup(features
, &spa
->spa_label_features
,
531 VERIFY(nvlist_dup(config
, &spa
->spa_config
, 0) == 0);
534 if (spa
->spa_label_features
== NULL
) {
535 VERIFY(nvlist_alloc(&spa
->spa_label_features
, NV_UNIQUE_NAME
,
539 spa
->spa_debug
= ((zfs_flags
& ZFS_DEBUG_SPA
) != 0);
545 * Removes a spa_t from the namespace, freeing up any memory used. Requires
546 * spa_namespace_lock. This is called only after the spa_t has been closed and
550 spa_remove(spa_t
*spa
)
552 spa_config_dirent_t
*dp
;
555 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
556 ASSERT(spa
->spa_state
== POOL_STATE_UNINITIALIZED
);
558 nvlist_free(spa
->spa_config_splitting
);
560 avl_remove(&spa_namespace_avl
, spa
);
561 cv_broadcast(&spa_namespace_cv
);
564 spa_strfree(spa
->spa_root
);
568 while ((dp
= list_head(&spa
->spa_config_list
)) != NULL
) {
569 list_remove(&spa
->spa_config_list
, dp
);
570 if (dp
->scd_path
!= NULL
)
571 spa_strfree(dp
->scd_path
);
572 kmem_free(dp
, sizeof (spa_config_dirent_t
));
575 list_destroy(&spa
->spa_config_list
);
577 nvlist_free(spa
->spa_label_features
);
578 nvlist_free(spa
->spa_load_info
);
579 spa_config_set(spa
, NULL
);
581 refcount_destroy(&spa
->spa_refcount
);
583 spa_config_lock_destroy(spa
);
585 for (t
= 0; t
< TXG_SIZE
; t
++)
586 bplist_destroy(&spa
->spa_free_bplist
[t
]);
588 cv_destroy(&spa
->spa_async_cv
);
589 cv_destroy(&spa
->spa_proc_cv
);
590 cv_destroy(&spa
->spa_scrub_io_cv
);
591 cv_destroy(&spa
->spa_suspend_cv
);
593 mutex_destroy(&spa
->spa_async_lock
);
594 mutex_destroy(&spa
->spa_errlist_lock
);
595 mutex_destroy(&spa
->spa_errlog_lock
);
596 mutex_destroy(&spa
->spa_history_lock
);
597 mutex_destroy(&spa
->spa_proc_lock
);
598 mutex_destroy(&spa
->spa_props_lock
);
599 mutex_destroy(&spa
->spa_scrub_lock
);
600 mutex_destroy(&spa
->spa_suspend_lock
);
601 mutex_destroy(&spa
->spa_vdev_top_lock
);
603 kmem_free(spa
, sizeof (spa_t
));
607 * Given a pool, return the next pool in the namespace, or NULL if there is
608 * none. If 'prev' is NULL, return the first pool.
611 spa_next(spa_t
*prev
)
613 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
616 return (AVL_NEXT(&spa_namespace_avl
, prev
));
618 return (avl_first(&spa_namespace_avl
));
622 * ==========================================================================
623 * SPA refcount functions
624 * ==========================================================================
628 * Add a reference to the given spa_t. Must have at least one reference, or
629 * have the namespace lock held.
632 spa_open_ref(spa_t
*spa
, void *tag
)
634 ASSERT(refcount_count(&spa
->spa_refcount
) >= spa
->spa_minref
||
635 MUTEX_HELD(&spa_namespace_lock
));
636 (void) refcount_add(&spa
->spa_refcount
, tag
);
640 * Remove a reference to the given spa_t. Must have at least one reference, or
641 * have the namespace lock held.
644 spa_close(spa_t
*spa
, void *tag
)
646 ASSERT(refcount_count(&spa
->spa_refcount
) > spa
->spa_minref
||
647 MUTEX_HELD(&spa_namespace_lock
));
648 (void) refcount_remove(&spa
->spa_refcount
, tag
);
652 * Check to see if the spa refcount is zero. Must be called with
653 * spa_namespace_lock held. We really compare against spa_minref, which is the
654 * number of references acquired when opening a pool
657 spa_refcount_zero(spa_t
*spa
)
659 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
661 return (refcount_count(&spa
->spa_refcount
) == spa
->spa_minref
);
665 * ==========================================================================
666 * SPA spare and l2cache tracking
667 * ==========================================================================
671 * Hot spares and cache devices are tracked using the same code below,
672 * for 'auxiliary' devices.
675 typedef struct spa_aux
{
683 spa_aux_compare(const void *a
, const void *b
)
685 const spa_aux_t
*sa
= a
;
686 const spa_aux_t
*sb
= b
;
688 if (sa
->aux_guid
< sb
->aux_guid
)
690 else if (sa
->aux_guid
> sb
->aux_guid
)
697 spa_aux_add(vdev_t
*vd
, avl_tree_t
*avl
)
703 search
.aux_guid
= vd
->vdev_guid
;
704 if ((aux
= avl_find(avl
, &search
, &where
)) != NULL
) {
707 aux
= kmem_zalloc(sizeof (spa_aux_t
), KM_PUSHPAGE
);
708 aux
->aux_guid
= vd
->vdev_guid
;
710 avl_insert(avl
, aux
, where
);
715 spa_aux_remove(vdev_t
*vd
, avl_tree_t
*avl
)
721 search
.aux_guid
= vd
->vdev_guid
;
722 aux
= avl_find(avl
, &search
, &where
);
726 if (--aux
->aux_count
== 0) {
727 avl_remove(avl
, aux
);
728 kmem_free(aux
, sizeof (spa_aux_t
));
729 } else if (aux
->aux_pool
== spa_guid(vd
->vdev_spa
)) {
730 aux
->aux_pool
= 0ULL;
735 spa_aux_exists(uint64_t guid
, uint64_t *pool
, int *refcnt
, avl_tree_t
*avl
)
737 spa_aux_t search
, *found
;
739 search
.aux_guid
= guid
;
740 found
= avl_find(avl
, &search
, NULL
);
744 *pool
= found
->aux_pool
;
751 *refcnt
= found
->aux_count
;
756 return (found
!= NULL
);
760 spa_aux_activate(vdev_t
*vd
, avl_tree_t
*avl
)
762 spa_aux_t search
, *found
;
765 search
.aux_guid
= vd
->vdev_guid
;
766 found
= avl_find(avl
, &search
, &where
);
767 ASSERT(found
!= NULL
);
768 ASSERT(found
->aux_pool
== 0ULL);
770 found
->aux_pool
= spa_guid(vd
->vdev_spa
);
774 * Spares are tracked globally due to the following constraints:
776 * - A spare may be part of multiple pools.
777 * - A spare may be added to a pool even if it's actively in use within
779 * - A spare in use in any pool can only be the source of a replacement if
780 * the target is a spare in the same pool.
782 * We keep track of all spares on the system through the use of a reference
783 * counted AVL tree. When a vdev is added as a spare, or used as a replacement
784 * spare, then we bump the reference count in the AVL tree. In addition, we set
785 * the 'vdev_isspare' member to indicate that the device is a spare (active or
786 * inactive). When a spare is made active (used to replace a device in the
787 * pool), we also keep track of which pool its been made a part of.
789 * The 'spa_spare_lock' protects the AVL tree. These functions are normally
790 * called under the spa_namespace lock as part of vdev reconfiguration. The
791 * separate spare lock exists for the status query path, which does not need to
792 * be completely consistent with respect to other vdev configuration changes.
796 spa_spare_compare(const void *a
, const void *b
)
798 return (spa_aux_compare(a
, b
));
802 spa_spare_add(vdev_t
*vd
)
804 mutex_enter(&spa_spare_lock
);
805 ASSERT(!vd
->vdev_isspare
);
806 spa_aux_add(vd
, &spa_spare_avl
);
807 vd
->vdev_isspare
= B_TRUE
;
808 mutex_exit(&spa_spare_lock
);
812 spa_spare_remove(vdev_t
*vd
)
814 mutex_enter(&spa_spare_lock
);
815 ASSERT(vd
->vdev_isspare
);
816 spa_aux_remove(vd
, &spa_spare_avl
);
817 vd
->vdev_isspare
= B_FALSE
;
818 mutex_exit(&spa_spare_lock
);
822 spa_spare_exists(uint64_t guid
, uint64_t *pool
, int *refcnt
)
826 mutex_enter(&spa_spare_lock
);
827 found
= spa_aux_exists(guid
, pool
, refcnt
, &spa_spare_avl
);
828 mutex_exit(&spa_spare_lock
);
834 spa_spare_activate(vdev_t
*vd
)
836 mutex_enter(&spa_spare_lock
);
837 ASSERT(vd
->vdev_isspare
);
838 spa_aux_activate(vd
, &spa_spare_avl
);
839 mutex_exit(&spa_spare_lock
);
843 * Level 2 ARC devices are tracked globally for the same reasons as spares.
844 * Cache devices currently only support one pool per cache device, and so
845 * for these devices the aux reference count is currently unused beyond 1.
849 spa_l2cache_compare(const void *a
, const void *b
)
851 return (spa_aux_compare(a
, b
));
855 spa_l2cache_add(vdev_t
*vd
)
857 mutex_enter(&spa_l2cache_lock
);
858 ASSERT(!vd
->vdev_isl2cache
);
859 spa_aux_add(vd
, &spa_l2cache_avl
);
860 vd
->vdev_isl2cache
= B_TRUE
;
861 mutex_exit(&spa_l2cache_lock
);
865 spa_l2cache_remove(vdev_t
*vd
)
867 mutex_enter(&spa_l2cache_lock
);
868 ASSERT(vd
->vdev_isl2cache
);
869 spa_aux_remove(vd
, &spa_l2cache_avl
);
870 vd
->vdev_isl2cache
= B_FALSE
;
871 mutex_exit(&spa_l2cache_lock
);
875 spa_l2cache_exists(uint64_t guid
, uint64_t *pool
)
879 mutex_enter(&spa_l2cache_lock
);
880 found
= spa_aux_exists(guid
, pool
, NULL
, &spa_l2cache_avl
);
881 mutex_exit(&spa_l2cache_lock
);
887 spa_l2cache_activate(vdev_t
*vd
)
889 mutex_enter(&spa_l2cache_lock
);
890 ASSERT(vd
->vdev_isl2cache
);
891 spa_aux_activate(vd
, &spa_l2cache_avl
);
892 mutex_exit(&spa_l2cache_lock
);
896 * ==========================================================================
898 * ==========================================================================
902 * Lock the given spa_t for the purpose of adding or removing a vdev.
903 * Grabs the global spa_namespace_lock plus the spa config lock for writing.
904 * It returns the next transaction group for the spa_t.
907 spa_vdev_enter(spa_t
*spa
)
909 mutex_enter(&spa
->spa_vdev_top_lock
);
910 mutex_enter(&spa_namespace_lock
);
911 return (spa_vdev_config_enter(spa
));
915 * Internal implementation for spa_vdev_enter(). Used when a vdev
916 * operation requires multiple syncs (i.e. removing a device) while
917 * keeping the spa_namespace_lock held.
920 spa_vdev_config_enter(spa_t
*spa
)
922 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
924 spa_config_enter(spa
, SCL_ALL
, spa
, RW_WRITER
);
926 return (spa_last_synced_txg(spa
) + 1);
930 * Used in combination with spa_vdev_config_enter() to allow the syncing
931 * of multiple transactions without releasing the spa_namespace_lock.
934 spa_vdev_config_exit(spa_t
*spa
, vdev_t
*vd
, uint64_t txg
, int error
, char *tag
)
936 int config_changed
= B_FALSE
;
938 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
939 ASSERT(txg
> spa_last_synced_txg(spa
));
941 spa
->spa_pending_vdev
= NULL
;
946 vdev_dtl_reassess(spa
->spa_root_vdev
, 0, 0, B_FALSE
);
948 if (error
== 0 && !list_is_empty(&spa
->spa_config_dirty_list
)) {
949 config_changed
= B_TRUE
;
950 spa
->spa_config_generation
++;
954 * Verify the metaslab classes.
956 ASSERT(metaslab_class_validate(spa_normal_class(spa
)) == 0);
957 ASSERT(metaslab_class_validate(spa_log_class(spa
)) == 0);
959 spa_config_exit(spa
, SCL_ALL
, spa
);
962 * Panic the system if the specified tag requires it. This
963 * is useful for ensuring that configurations are updated
966 if (zio_injection_enabled
)
967 zio_handle_panic_injection(spa
, tag
, 0);
970 * Note: this txg_wait_synced() is important because it ensures
971 * that there won't be more than one config change per txg.
972 * This allows us to use the txg as the generation number.
975 txg_wait_synced(spa
->spa_dsl_pool
, txg
);
978 ASSERT(!vd
->vdev_detached
|| vd
->vdev_dtl_smo
.smo_object
== 0);
979 spa_config_enter(spa
, SCL_ALL
, spa
, RW_WRITER
);
981 spa_config_exit(spa
, SCL_ALL
, spa
);
985 * If the config changed, update the config cache.
988 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
992 * Unlock the spa_t after adding or removing a vdev. Besides undoing the
993 * locking of spa_vdev_enter(), we also want make sure the transactions have
994 * synced to disk, and then update the global configuration cache with the new
998 spa_vdev_exit(spa_t
*spa
, vdev_t
*vd
, uint64_t txg
, int error
)
1000 spa_vdev_config_exit(spa
, vd
, txg
, error
, FTAG
);
1001 mutex_exit(&spa_namespace_lock
);
1002 mutex_exit(&spa
->spa_vdev_top_lock
);
1008 * Lock the given spa_t for the purpose of changing vdev state.
1011 spa_vdev_state_enter(spa_t
*spa
, int oplocks
)
1013 int locks
= SCL_STATE_ALL
| oplocks
;
1016 * Root pools may need to read of the underlying devfs filesystem
1017 * when opening up a vdev. Unfortunately if we're holding the
1018 * SCL_ZIO lock it will result in a deadlock when we try to issue
1019 * the read from the root filesystem. Instead we "prefetch"
1020 * the associated vnodes that we need prior to opening the
1021 * underlying devices and cache them so that we can prevent
1022 * any I/O when we are doing the actual open.
1024 if (spa_is_root(spa
)) {
1025 int low
= locks
& ~(SCL_ZIO
- 1);
1026 int high
= locks
& ~low
;
1028 spa_config_enter(spa
, high
, spa
, RW_WRITER
);
1029 vdev_hold(spa
->spa_root_vdev
);
1030 spa_config_enter(spa
, low
, spa
, RW_WRITER
);
1032 spa_config_enter(spa
, locks
, spa
, RW_WRITER
);
1034 spa
->spa_vdev_locks
= locks
;
1038 spa_vdev_state_exit(spa_t
*spa
, vdev_t
*vd
, int error
)
1040 boolean_t config_changed
= B_FALSE
;
1042 if (vd
!= NULL
|| error
== 0)
1043 vdev_dtl_reassess(vd
? vd
->vdev_top
: spa
->spa_root_vdev
,
1047 vdev_state_dirty(vd
->vdev_top
);
1048 config_changed
= B_TRUE
;
1049 spa
->spa_config_generation
++;
1052 if (spa_is_root(spa
))
1053 vdev_rele(spa
->spa_root_vdev
);
1055 ASSERT3U(spa
->spa_vdev_locks
, >=, SCL_STATE_ALL
);
1056 spa_config_exit(spa
, spa
->spa_vdev_locks
, spa
);
1059 * If anything changed, wait for it to sync. This ensures that,
1060 * from the system administrator's perspective, zpool(1M) commands
1061 * are synchronous. This is important for things like zpool offline:
1062 * when the command completes, you expect no further I/O from ZFS.
1065 txg_wait_synced(spa
->spa_dsl_pool
, 0);
1068 * If the config changed, update the config cache.
1070 if (config_changed
) {
1071 mutex_enter(&spa_namespace_lock
);
1072 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
1073 mutex_exit(&spa_namespace_lock
);
1080 * ==========================================================================
1081 * Miscellaneous functions
1082 * ==========================================================================
1086 spa_activate_mos_feature(spa_t
*spa
, const char *feature
)
1088 (void) nvlist_add_boolean(spa
->spa_label_features
, feature
);
1089 vdev_config_dirty(spa
->spa_root_vdev
);
1093 spa_deactivate_mos_feature(spa_t
*spa
, const char *feature
)
1095 (void) nvlist_remove_all(spa
->spa_label_features
, feature
);
1096 vdev_config_dirty(spa
->spa_root_vdev
);
1103 spa_rename(const char *name
, const char *newname
)
1109 * Lookup the spa_t and grab the config lock for writing. We need to
1110 * actually open the pool so that we can sync out the necessary labels.
1111 * It's OK to call spa_open() with the namespace lock held because we
1112 * allow recursive calls for other reasons.
1114 mutex_enter(&spa_namespace_lock
);
1115 if ((err
= spa_open(name
, &spa
, FTAG
)) != 0) {
1116 mutex_exit(&spa_namespace_lock
);
1120 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1122 avl_remove(&spa_namespace_avl
, spa
);
1123 (void) strlcpy(spa
->spa_name
, newname
, sizeof (spa
->spa_name
));
1124 avl_add(&spa_namespace_avl
, spa
);
1127 * Sync all labels to disk with the new names by marking the root vdev
1128 * dirty and waiting for it to sync. It will pick up the new pool name
1131 vdev_config_dirty(spa
->spa_root_vdev
);
1133 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1135 txg_wait_synced(spa
->spa_dsl_pool
, 0);
1138 * Sync the updated config cache.
1140 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
1142 spa_close(spa
, FTAG
);
1144 mutex_exit(&spa_namespace_lock
);
1150 * Return the spa_t associated with given pool_guid, if it exists. If
1151 * device_guid is non-zero, determine whether the pool exists *and* contains
1152 * a device with the specified device_guid.
1155 spa_by_guid(uint64_t pool_guid
, uint64_t device_guid
)
1158 avl_tree_t
*t
= &spa_namespace_avl
;
1160 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1162 for (spa
= avl_first(t
); spa
!= NULL
; spa
= AVL_NEXT(t
, spa
)) {
1163 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
)
1165 if (spa
->spa_root_vdev
== NULL
)
1167 if (spa_guid(spa
) == pool_guid
) {
1168 if (device_guid
== 0)
1171 if (vdev_lookup_by_guid(spa
->spa_root_vdev
,
1172 device_guid
) != NULL
)
1176 * Check any devices we may be in the process of adding.
1178 if (spa
->spa_pending_vdev
) {
1179 if (vdev_lookup_by_guid(spa
->spa_pending_vdev
,
1180 device_guid
) != NULL
)
1190 * Determine whether a pool with the given pool_guid exists.
1193 spa_guid_exists(uint64_t pool_guid
, uint64_t device_guid
)
1195 return (spa_by_guid(pool_guid
, device_guid
) != NULL
);
1199 spa_strdup(const char *s
)
1205 new = kmem_alloc(len
+ 1, KM_PUSHPAGE
);
1213 spa_strfree(char *s
)
1215 kmem_free(s
, strlen(s
) + 1);
1219 spa_get_random(uint64_t range
)
1225 (void) random_get_pseudo_bytes((void *)&r
, sizeof (uint64_t));
1231 spa_generate_guid(spa_t
*spa
)
1233 uint64_t guid
= spa_get_random(-1ULL);
1236 while (guid
== 0 || spa_guid_exists(spa_guid(spa
), guid
))
1237 guid
= spa_get_random(-1ULL);
1239 while (guid
== 0 || spa_guid_exists(guid
, 0))
1240 guid
= spa_get_random(-1ULL);
1247 sprintf_blkptr(char *buf
, const blkptr_t
*bp
)
1250 char *checksum
= NULL
;
1251 char *compress
= NULL
;
1254 if (BP_GET_TYPE(bp
) & DMU_OT_NEWTYPE
) {
1255 dmu_object_byteswap_t bswap
=
1256 DMU_OT_BYTESWAP(BP_GET_TYPE(bp
));
1257 (void) snprintf(type
, sizeof (type
), "bswap %s %s",
1258 DMU_OT_IS_METADATA(BP_GET_TYPE(bp
)) ?
1259 "metadata" : "data",
1260 dmu_ot_byteswap
[bswap
].ob_name
);
1262 (void) strlcpy(type
, dmu_ot
[BP_GET_TYPE(bp
)].ot_name
,
1265 checksum
= zio_checksum_table
[BP_GET_CHECKSUM(bp
)].ci_name
;
1266 compress
= zio_compress_table
[BP_GET_COMPRESS(bp
)].ci_name
;
1269 SPRINTF_BLKPTR(snprintf
, ' ', buf
, bp
, type
, checksum
, compress
);
1273 spa_freeze(spa_t
*spa
)
1275 uint64_t freeze_txg
= 0;
1277 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1278 if (spa
->spa_freeze_txg
== UINT64_MAX
) {
1279 freeze_txg
= spa_last_synced_txg(spa
) + TXG_SIZE
;
1280 spa
->spa_freeze_txg
= freeze_txg
;
1282 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1283 if (freeze_txg
!= 0)
1284 txg_wait_synced(spa_get_dsl(spa
), freeze_txg
);
1288 * This is a stripped-down version of strtoull, suitable only for converting
1289 * lowercase hexidecimal numbers that don't overflow.
1292 strtonum(const char *str
, char **nptr
)
1298 while ((c
= *str
) != '\0') {
1299 if (c
>= '0' && c
<= '9')
1301 else if (c
>= 'a' && c
<= 'f')
1302 digit
= 10 + c
- 'a';
1313 *nptr
= (char *)str
;
1319 * ==========================================================================
1320 * Accessor functions
1321 * ==========================================================================
1325 spa_shutting_down(spa_t
*spa
)
1327 return (spa
->spa_async_suspended
);
1331 spa_get_dsl(spa_t
*spa
)
1333 return (spa
->spa_dsl_pool
);
1337 spa_is_initializing(spa_t
*spa
)
1339 return (spa
->spa_is_initializing
);
1343 spa_get_rootblkptr(spa_t
*spa
)
1345 return (&spa
->spa_ubsync
.ub_rootbp
);
1349 spa_set_rootblkptr(spa_t
*spa
, const blkptr_t
*bp
)
1351 spa
->spa_uberblock
.ub_rootbp
= *bp
;
1355 spa_altroot(spa_t
*spa
, char *buf
, size_t buflen
)
1357 if (spa
->spa_root
== NULL
)
1360 (void) strncpy(buf
, spa
->spa_root
, buflen
);
1364 spa_sync_pass(spa_t
*spa
)
1366 return (spa
->spa_sync_pass
);
1370 spa_name(spa_t
*spa
)
1372 return (spa
->spa_name
);
1376 spa_guid(spa_t
*spa
)
1378 dsl_pool_t
*dp
= spa_get_dsl(spa
);
1382 * If we fail to parse the config during spa_load(), we can go through
1383 * the error path (which posts an ereport) and end up here with no root
1384 * vdev. We stash the original pool guid in 'spa_config_guid' to handle
1387 if (spa
->spa_root_vdev
== NULL
)
1388 return (spa
->spa_config_guid
);
1390 guid
= spa
->spa_last_synced_guid
!= 0 ?
1391 spa
->spa_last_synced_guid
: spa
->spa_root_vdev
->vdev_guid
;
1394 * Return the most recently synced out guid unless we're
1395 * in syncing context.
1397 if (dp
&& dsl_pool_sync_context(dp
))
1398 return (spa
->spa_root_vdev
->vdev_guid
);
1404 spa_load_guid(spa_t
*spa
)
1407 * This is a GUID that exists solely as a reference for the
1408 * purposes of the arc. It is generated at load time, and
1409 * is never written to persistent storage.
1411 return (spa
->spa_load_guid
);
1415 spa_last_synced_txg(spa_t
*spa
)
1417 return (spa
->spa_ubsync
.ub_txg
);
1421 spa_first_txg(spa_t
*spa
)
1423 return (spa
->spa_first_txg
);
1427 spa_syncing_txg(spa_t
*spa
)
1429 return (spa
->spa_syncing_txg
);
1433 spa_state(spa_t
*spa
)
1435 return (spa
->spa_state
);
1439 spa_load_state(spa_t
*spa
)
1441 return (spa
->spa_load_state
);
1445 spa_freeze_txg(spa_t
*spa
)
1447 return (spa
->spa_freeze_txg
);
1452 spa_get_asize(spa_t
*spa
, uint64_t lsize
)
1455 * The worst case is single-sector max-parity RAID-Z blocks, in which
1456 * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1)
1457 * times the size; so just assume that. Add to this the fact that
1458 * we can have up to 3 DVAs per bp, and one more factor of 2 because
1459 * the block may be dittoed with up to 3 DVAs by ddt_sync().
1461 return (lsize
* (VDEV_RAIDZ_MAXPARITY
+ 1) * SPA_DVAS_PER_BP
* 2);
1465 spa_get_dspace(spa_t
*spa
)
1467 return (spa
->spa_dspace
);
1471 spa_update_dspace(spa_t
*spa
)
1473 spa
->spa_dspace
= metaslab_class_get_dspace(spa_normal_class(spa
)) +
1474 ddt_get_dedup_dspace(spa
);
1478 * Return the failure mode that has been set to this pool. The default
1479 * behavior will be to block all I/Os when a complete failure occurs.
1482 spa_get_failmode(spa_t
*spa
)
1484 return (spa
->spa_failmode
);
1488 spa_suspended(spa_t
*spa
)
1490 return (spa
->spa_suspended
);
1494 spa_version(spa_t
*spa
)
1496 return (spa
->spa_ubsync
.ub_version
);
1500 spa_deflate(spa_t
*spa
)
1502 return (spa
->spa_deflate
);
1506 spa_normal_class(spa_t
*spa
)
1508 return (spa
->spa_normal_class
);
1512 spa_log_class(spa_t
*spa
)
1514 return (spa
->spa_log_class
);
1518 spa_max_replication(spa_t
*spa
)
1521 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
1522 * handle BPs with more than one DVA allocated. Set our max
1523 * replication level accordingly.
1525 if (spa_version(spa
) < SPA_VERSION_DITTO_BLOCKS
)
1527 return (MIN(SPA_DVAS_PER_BP
, spa_max_replication_override
));
1531 spa_prev_software_version(spa_t
*spa
)
1533 return (spa
->spa_prev_software_version
);
1537 spa_deadman_synctime(spa_t
*spa
)
1539 return (spa
->spa_deadman_synctime
);
1543 dva_get_dsize_sync(spa_t
*spa
, const dva_t
*dva
)
1545 uint64_t asize
= DVA_GET_ASIZE(dva
);
1546 uint64_t dsize
= asize
;
1548 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_READER
) != 0);
1550 if (asize
!= 0 && spa
->spa_deflate
) {
1551 vdev_t
*vd
= vdev_lookup_top(spa
, DVA_GET_VDEV(dva
));
1552 dsize
= (asize
>> SPA_MINBLOCKSHIFT
) * vd
->vdev_deflate_ratio
;
1559 bp_get_dsize_sync(spa_t
*spa
, const blkptr_t
*bp
)
1564 for (d
= 0; d
< SPA_DVAS_PER_BP
; d
++)
1565 dsize
+= dva_get_dsize_sync(spa
, &bp
->blk_dva
[d
]);
1571 bp_get_dsize(spa_t
*spa
, const blkptr_t
*bp
)
1576 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
1578 for (d
= 0; d
< SPA_DVAS_PER_BP
; d
++)
1579 dsize
+= dva_get_dsize_sync(spa
, &bp
->blk_dva
[d
]);
1581 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
1587 * ==========================================================================
1588 * Initialization and Termination
1589 * ==========================================================================
1593 spa_name_compare(const void *a1
, const void *a2
)
1595 const spa_t
*s1
= a1
;
1596 const spa_t
*s2
= a2
;
1599 s
= strcmp(s1
->spa_name
, s2
->spa_name
);
1616 mutex_init(&spa_namespace_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1617 mutex_init(&spa_spare_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1618 mutex_init(&spa_l2cache_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1619 cv_init(&spa_namespace_cv
, NULL
, CV_DEFAULT
, NULL
);
1621 avl_create(&spa_namespace_avl
, spa_name_compare
, sizeof (spa_t
),
1622 offsetof(spa_t
, spa_avl
));
1624 avl_create(&spa_spare_avl
, spa_spare_compare
, sizeof (spa_aux_t
),
1625 offsetof(spa_aux_t
, aux_avl
));
1627 avl_create(&spa_l2cache_avl
, spa_l2cache_compare
, sizeof (spa_aux_t
),
1628 offsetof(spa_aux_t
, aux_avl
));
1630 spa_mode_global
= mode
;
1639 vdev_cache_stat_init();
1642 zpool_feature_init();
1654 vdev_cache_stat_fini();
1663 avl_destroy(&spa_namespace_avl
);
1664 avl_destroy(&spa_spare_avl
);
1665 avl_destroy(&spa_l2cache_avl
);
1667 cv_destroy(&spa_namespace_cv
);
1668 mutex_destroy(&spa_namespace_lock
);
1669 mutex_destroy(&spa_spare_lock
);
1670 mutex_destroy(&spa_l2cache_lock
);
1674 * Return whether this pool has slogs. No locking needed.
1675 * It's not a problem if the wrong answer is returned as it's only for
1676 * performance and not correctness
1679 spa_has_slogs(spa_t
*spa
)
1681 return (spa
->spa_log_class
->mc_rotor
!= NULL
);
1685 spa_get_log_state(spa_t
*spa
)
1687 return (spa
->spa_log_state
);
1691 spa_set_log_state(spa_t
*spa
, spa_log_state_t state
)
1693 spa
->spa_log_state
= state
;
1697 spa_is_root(spa_t
*spa
)
1699 return (spa
->spa_is_root
);
1703 spa_writeable(spa_t
*spa
)
1705 return (!!(spa
->spa_mode
& FWRITE
));
1709 spa_mode(spa_t
*spa
)
1711 return (spa
->spa_mode
);
1715 spa_bootfs(spa_t
*spa
)
1717 return (spa
->spa_bootfs
);
1721 spa_delegation(spa_t
*spa
)
1723 return (spa
->spa_delegation
);
1727 spa_meta_objset(spa_t
*spa
)
1729 return (spa
->spa_meta_objset
);
1733 spa_dedup_checksum(spa_t
*spa
)
1735 return (spa
->spa_dedup_checksum
);
1739 * Reset pool scan stat per scan pass (or reboot).
1742 spa_scan_stat_init(spa_t
*spa
)
1744 /* data not stored on disk */
1745 spa
->spa_scan_pass_start
= gethrestime_sec();
1746 spa
->spa_scan_pass_exam
= 0;
1747 vdev_scan_stat_init(spa
->spa_root_vdev
);
1751 * Get scan stats for zpool status reports
1754 spa_scan_get_stats(spa_t
*spa
, pool_scan_stat_t
*ps
)
1756 dsl_scan_t
*scn
= spa
->spa_dsl_pool
? spa
->spa_dsl_pool
->dp_scan
: NULL
;
1758 if (scn
== NULL
|| scn
->scn_phys
.scn_func
== POOL_SCAN_NONE
)
1760 bzero(ps
, sizeof (pool_scan_stat_t
));
1762 /* data stored on disk */
1763 ps
->pss_func
= scn
->scn_phys
.scn_func
;
1764 ps
->pss_start_time
= scn
->scn_phys
.scn_start_time
;
1765 ps
->pss_end_time
= scn
->scn_phys
.scn_end_time
;
1766 ps
->pss_to_examine
= scn
->scn_phys
.scn_to_examine
;
1767 ps
->pss_examined
= scn
->scn_phys
.scn_examined
;
1768 ps
->pss_to_process
= scn
->scn_phys
.scn_to_process
;
1769 ps
->pss_processed
= scn
->scn_phys
.scn_processed
;
1770 ps
->pss_errors
= scn
->scn_phys
.scn_errors
;
1771 ps
->pss_state
= scn
->scn_phys
.scn_state
;
1773 /* data not stored on disk */
1774 ps
->pss_pass_start
= spa
->spa_scan_pass_start
;
1775 ps
->pss_pass_exam
= spa
->spa_scan_pass_exam
;
1781 spa_debug_enabled(spa_t
*spa
)
1783 return (spa
->spa_debug
);
1786 #if defined(_KERNEL) && defined(HAVE_SPL)
1787 /* Namespace manipulation */
1788 EXPORT_SYMBOL(spa_lookup
);
1789 EXPORT_SYMBOL(spa_add
);
1790 EXPORT_SYMBOL(spa_remove
);
1791 EXPORT_SYMBOL(spa_next
);
1793 /* Refcount functions */
1794 EXPORT_SYMBOL(spa_open_ref
);
1795 EXPORT_SYMBOL(spa_close
);
1796 EXPORT_SYMBOL(spa_refcount_zero
);
1798 /* Pool configuration lock */
1799 EXPORT_SYMBOL(spa_config_tryenter
);
1800 EXPORT_SYMBOL(spa_config_enter
);
1801 EXPORT_SYMBOL(spa_config_exit
);
1802 EXPORT_SYMBOL(spa_config_held
);
1804 /* Pool vdev add/remove lock */
1805 EXPORT_SYMBOL(spa_vdev_enter
);
1806 EXPORT_SYMBOL(spa_vdev_exit
);
1808 /* Pool vdev state change lock */
1809 EXPORT_SYMBOL(spa_vdev_state_enter
);
1810 EXPORT_SYMBOL(spa_vdev_state_exit
);
1812 /* Accessor functions */
1813 EXPORT_SYMBOL(spa_shutting_down
);
1814 EXPORT_SYMBOL(spa_get_dsl
);
1815 EXPORT_SYMBOL(spa_get_rootblkptr
);
1816 EXPORT_SYMBOL(spa_set_rootblkptr
);
1817 EXPORT_SYMBOL(spa_altroot
);
1818 EXPORT_SYMBOL(spa_sync_pass
);
1819 EXPORT_SYMBOL(spa_name
);
1820 EXPORT_SYMBOL(spa_guid
);
1821 EXPORT_SYMBOL(spa_last_synced_txg
);
1822 EXPORT_SYMBOL(spa_first_txg
);
1823 EXPORT_SYMBOL(spa_syncing_txg
);
1824 EXPORT_SYMBOL(spa_version
);
1825 EXPORT_SYMBOL(spa_state
);
1826 EXPORT_SYMBOL(spa_load_state
);
1827 EXPORT_SYMBOL(spa_freeze_txg
);
1828 EXPORT_SYMBOL(spa_get_asize
);
1829 EXPORT_SYMBOL(spa_get_dspace
);
1830 EXPORT_SYMBOL(spa_update_dspace
);
1831 EXPORT_SYMBOL(spa_deflate
);
1832 EXPORT_SYMBOL(spa_normal_class
);
1833 EXPORT_SYMBOL(spa_log_class
);
1834 EXPORT_SYMBOL(spa_max_replication
);
1835 EXPORT_SYMBOL(spa_prev_software_version
);
1836 EXPORT_SYMBOL(spa_get_failmode
);
1837 EXPORT_SYMBOL(spa_suspended
);
1838 EXPORT_SYMBOL(spa_bootfs
);
1839 EXPORT_SYMBOL(spa_delegation
);
1840 EXPORT_SYMBOL(spa_meta_objset
);
1842 /* Miscellaneous support routines */
1843 EXPORT_SYMBOL(spa_rename
);
1844 EXPORT_SYMBOL(spa_guid_exists
);
1845 EXPORT_SYMBOL(spa_strdup
);
1846 EXPORT_SYMBOL(spa_strfree
);
1847 EXPORT_SYMBOL(spa_get_random
);
1848 EXPORT_SYMBOL(spa_generate_guid
);
1849 EXPORT_SYMBOL(sprintf_blkptr
);
1850 EXPORT_SYMBOL(spa_freeze
);
1851 EXPORT_SYMBOL(spa_upgrade
);
1852 EXPORT_SYMBOL(spa_evict_all
);
1853 EXPORT_SYMBOL(spa_lookup_by_guid
);
1854 EXPORT_SYMBOL(spa_has_spare
);
1855 EXPORT_SYMBOL(dva_get_dsize_sync
);
1856 EXPORT_SYMBOL(bp_get_dsize_sync
);
1857 EXPORT_SYMBOL(bp_get_dsize
);
1858 EXPORT_SYMBOL(spa_has_slogs
);
1859 EXPORT_SYMBOL(spa_is_root
);
1860 EXPORT_SYMBOL(spa_writeable
);
1861 EXPORT_SYMBOL(spa_mode
);
1863 EXPORT_SYMBOL(spa_namespace_lock
);
1865 module_param(zfs_deadman_synctime
, ulong
, 0644);
1866 MODULE_PARM_DESC(zfs_deadman_synctime
,"Expire in units of zfs_txg_synctime_ms");
1868 module_param(zfs_deadman_enabled
, int, 0644);
1869 MODULE_PARM_DESC(zfs_deadman_enabled
, "Enable deadman timer");