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) 2013 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>
51 #include <sys/kstat.h>
53 #include "zfeature_common.h"
58 * There are four basic locks for managing spa_t structures:
60 * spa_namespace_lock (global mutex)
62 * This lock must be acquired to do any of the following:
64 * - Lookup a spa_t by name
65 * - Add or remove a spa_t from the namespace
66 * - Increase spa_refcount from non-zero
67 * - Check if spa_refcount is zero
69 * - add/remove/attach/detach devices
70 * - Held for the duration of create/destroy/import/export
72 * It does not need to handle recursion. A create or destroy may
73 * reference objects (files or zvols) in other pools, but by
74 * definition they must have an existing reference, and will never need
75 * to lookup a spa_t by name.
77 * spa_refcount (per-spa refcount_t protected by mutex)
79 * This reference count keep track of any active users of the spa_t. The
80 * spa_t cannot be destroyed or freed while this is non-zero. Internally,
81 * the refcount is never really 'zero' - opening a pool implicitly keeps
82 * some references in the DMU. Internally we check against spa_minref, but
83 * present the image of a zero/non-zero value to consumers.
85 * spa_config_lock[] (per-spa array of rwlocks)
87 * This protects the spa_t from config changes, and must be held in
88 * the following circumstances:
90 * - RW_READER to perform I/O to the spa
91 * - RW_WRITER to change the vdev config
93 * The locking order is fairly straightforward:
95 * spa_namespace_lock -> spa_refcount
97 * The namespace lock must be acquired to increase the refcount from 0
98 * or to check if it is zero.
100 * spa_refcount -> spa_config_lock[]
102 * There must be at least one valid reference on the spa_t to acquire
105 * spa_namespace_lock -> spa_config_lock[]
107 * The namespace lock must always be taken before the config lock.
110 * The spa_namespace_lock can be acquired directly and is globally visible.
112 * The namespace is manipulated using the following functions, all of which
113 * require the spa_namespace_lock to be held.
115 * spa_lookup() Lookup a spa_t by name.
117 * spa_add() Create a new spa_t in the namespace.
119 * spa_remove() Remove a spa_t from the namespace. This also
120 * frees up any memory associated with the spa_t.
122 * spa_next() Returns the next spa_t in the system, or the
123 * first if NULL is passed.
125 * spa_evict_all() Shutdown and remove all spa_t structures in
128 * spa_guid_exists() Determine whether a pool/device guid exists.
130 * The spa_refcount is manipulated using the following functions:
132 * spa_open_ref() Adds a reference to the given spa_t. Must be
133 * called with spa_namespace_lock held if the
134 * refcount is currently zero.
136 * spa_close() Remove a reference from the spa_t. This will
137 * not free the spa_t or remove it from the
138 * namespace. No locking is required.
140 * spa_refcount_zero() Returns true if the refcount is currently
141 * zero. Must be called with spa_namespace_lock
144 * The spa_config_lock[] is an array of rwlocks, ordered as follows:
145 * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV.
146 * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}().
148 * To read the configuration, it suffices to hold one of these locks as reader.
149 * To modify the configuration, you must hold all locks as writer. To modify
150 * vdev state without altering the vdev tree's topology (e.g. online/offline),
151 * you must hold SCL_STATE and SCL_ZIO as writer.
153 * We use these distinct config locks to avoid recursive lock entry.
154 * For example, spa_sync() (which holds SCL_CONFIG as reader) induces
155 * block allocations (SCL_ALLOC), which may require reading space maps
156 * from disk (dmu_read() -> zio_read() -> SCL_ZIO).
158 * The spa config locks cannot be normal rwlocks because we need the
159 * ability to hand off ownership. For example, SCL_ZIO is acquired
160 * by the issuing thread and later released by an interrupt thread.
161 * They do, however, obey the usual write-wanted semantics to prevent
162 * writer (i.e. system administrator) starvation.
164 * The lock acquisition rules are as follows:
167 * Protects changes to the vdev tree topology, such as vdev
168 * add/remove/attach/detach. Protects the dirty config list
169 * (spa_config_dirty_list) and the set of spares and l2arc devices.
172 * Protects changes to pool state and vdev state, such as vdev
173 * online/offline/fault/degrade/clear. Protects the dirty state list
174 * (spa_state_dirty_list) and global pool state (spa_state).
177 * Protects changes to metaslab groups and classes.
178 * Held as reader by metaslab_alloc() and metaslab_claim().
181 * Held by bp-level zios (those which have no io_vd upon entry)
182 * to prevent changes to the vdev tree. The bp-level zio implicitly
183 * protects all of its vdev child zios, which do not hold SCL_ZIO.
186 * Protects changes to metaslab groups and classes.
187 * Held as reader by metaslab_free(). SCL_FREE is distinct from
188 * SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free
189 * blocks in zio_done() while another i/o that holds either
190 * SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete.
193 * Held as reader to prevent changes to the vdev tree during trivial
194 * inquiries such as bp_get_dsize(). SCL_VDEV is distinct from the
195 * other locks, and lower than all of them, to ensure that it's safe
196 * to acquire regardless of caller context.
198 * In addition, the following rules apply:
200 * (a) spa_props_lock protects pool properties, spa_config and spa_config_list.
201 * The lock ordering is SCL_CONFIG > spa_props_lock.
203 * (b) I/O operations on leaf vdevs. For any zio operation that takes
204 * an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(),
205 * or zio_write_phys() -- the caller must ensure that the config cannot
206 * cannot change in the interim, and that the vdev cannot be reopened.
207 * SCL_STATE as reader suffices for both.
209 * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
211 * spa_vdev_enter() Acquire the namespace lock and the config lock
214 * spa_vdev_exit() Release the config lock, wait for all I/O
215 * to complete, sync the updated configs to the
216 * cache, and release the namespace lock.
218 * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit().
219 * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual
220 * locking is, always, based on spa_namespace_lock and spa_config_lock[].
222 * spa_rename() is also implemented within this file since it requires
223 * manipulation of the namespace.
226 static avl_tree_t spa_namespace_avl
;
227 kmutex_t spa_namespace_lock
;
228 static kcondvar_t spa_namespace_cv
;
229 static int spa_active_count
;
230 int spa_max_replication_override
= SPA_DVAS_PER_BP
;
232 static kmutex_t spa_spare_lock
;
233 static avl_tree_t spa_spare_avl
;
234 static kmutex_t spa_l2cache_lock
;
235 static avl_tree_t spa_l2cache_avl
;
237 kmem_cache_t
*spa_buffer_pool
;
241 * Expiration time in units of zfs_txg_synctime_ms. This value has two
242 * meanings. First it is used to determine when the spa_deadman logic
243 * should fire. By default the spa_deadman will fire if spa_sync has
244 * not completed in 1000 * zfs_txg_synctime_ms (i.e. 1000 seconds).
245 * Secondly, the value determines if an I/O is considered "hung".
246 * Any I/O that has not completed in zfs_deadman_synctime is considered
247 * "hung" resulting in a zevent being posted.
249 unsigned long zfs_deadman_synctime
= 1000ULL;
252 * By default the deadman is enabled.
254 int zfs_deadman_enabled
= 1;
257 * ==========================================================================
259 * ==========================================================================
262 spa_config_lock_init(spa_t
*spa
)
266 for (i
= 0; i
< SCL_LOCKS
; i
++) {
267 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
268 mutex_init(&scl
->scl_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
269 cv_init(&scl
->scl_cv
, NULL
, CV_DEFAULT
, NULL
);
270 refcount_create_untracked(&scl
->scl_count
);
271 scl
->scl_writer
= NULL
;
272 scl
->scl_write_wanted
= 0;
277 spa_config_lock_destroy(spa_t
*spa
)
281 for (i
= 0; i
< SCL_LOCKS
; i
++) {
282 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
283 mutex_destroy(&scl
->scl_lock
);
284 cv_destroy(&scl
->scl_cv
);
285 refcount_destroy(&scl
->scl_count
);
286 ASSERT(scl
->scl_writer
== NULL
);
287 ASSERT(scl
->scl_write_wanted
== 0);
292 spa_config_tryenter(spa_t
*spa
, int locks
, void *tag
, krw_t rw
)
296 for (i
= 0; i
< SCL_LOCKS
; i
++) {
297 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
298 if (!(locks
& (1 << i
)))
300 mutex_enter(&scl
->scl_lock
);
301 if (rw
== RW_READER
) {
302 if (scl
->scl_writer
|| scl
->scl_write_wanted
) {
303 mutex_exit(&scl
->scl_lock
);
304 spa_config_exit(spa
, locks
^ (1 << i
), tag
);
308 ASSERT(scl
->scl_writer
!= curthread
);
309 if (!refcount_is_zero(&scl
->scl_count
)) {
310 mutex_exit(&scl
->scl_lock
);
311 spa_config_exit(spa
, locks
^ (1 << i
), tag
);
314 scl
->scl_writer
= curthread
;
316 (void) refcount_add(&scl
->scl_count
, tag
);
317 mutex_exit(&scl
->scl_lock
);
323 spa_config_enter(spa_t
*spa
, int locks
, void *tag
, krw_t rw
)
328 ASSERT3U(SCL_LOCKS
, <, sizeof (wlocks_held
) * NBBY
);
330 for (i
= 0; i
< SCL_LOCKS
; i
++) {
331 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
332 if (scl
->scl_writer
== curthread
)
333 wlocks_held
|= (1 << i
);
334 if (!(locks
& (1 << i
)))
336 mutex_enter(&scl
->scl_lock
);
337 if (rw
== RW_READER
) {
338 while (scl
->scl_writer
|| scl
->scl_write_wanted
) {
339 cv_wait(&scl
->scl_cv
, &scl
->scl_lock
);
342 ASSERT(scl
->scl_writer
!= curthread
);
343 while (!refcount_is_zero(&scl
->scl_count
)) {
344 scl
->scl_write_wanted
++;
345 cv_wait(&scl
->scl_cv
, &scl
->scl_lock
);
346 scl
->scl_write_wanted
--;
348 scl
->scl_writer
= curthread
;
350 (void) refcount_add(&scl
->scl_count
, tag
);
351 mutex_exit(&scl
->scl_lock
);
353 ASSERT(wlocks_held
<= locks
);
357 spa_config_exit(spa_t
*spa
, int locks
, void *tag
)
361 for (i
= SCL_LOCKS
- 1; i
>= 0; i
--) {
362 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
363 if (!(locks
& (1 << i
)))
365 mutex_enter(&scl
->scl_lock
);
366 ASSERT(!refcount_is_zero(&scl
->scl_count
));
367 if (refcount_remove(&scl
->scl_count
, tag
) == 0) {
368 ASSERT(scl
->scl_writer
== NULL
||
369 scl
->scl_writer
== curthread
);
370 scl
->scl_writer
= NULL
; /* OK in either case */
371 cv_broadcast(&scl
->scl_cv
);
373 mutex_exit(&scl
->scl_lock
);
378 spa_config_held(spa_t
*spa
, int locks
, krw_t rw
)
380 int i
, locks_held
= 0;
382 for (i
= 0; i
< SCL_LOCKS
; i
++) {
383 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
384 if (!(locks
& (1 << i
)))
386 if ((rw
== RW_READER
&& !refcount_is_zero(&scl
->scl_count
)) ||
387 (rw
== RW_WRITER
&& scl
->scl_writer
== curthread
))
388 locks_held
|= 1 << i
;
395 * ==========================================================================
396 * SPA namespace functions
397 * ==========================================================================
401 * Lookup the named spa_t in the AVL tree. The spa_namespace_lock must be held.
402 * Returns NULL if no matching spa_t is found.
405 spa_lookup(const char *name
)
407 static spa_t search
; /* spa_t is large; don't allocate on stack */
412 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
414 (void) strlcpy(search
.spa_name
, name
, sizeof (search
.spa_name
));
417 * If it's a full dataset name, figure out the pool name and
420 cp
= strpbrk(search
.spa_name
, "/@");
424 spa
= avl_find(&spa_namespace_avl
, &search
, &where
);
430 * Fires when spa_sync has not completed within zfs_deadman_synctime_ms.
431 * If the zfs_deadman_enabled flag is set then it inspects all vdev queues
432 * looking for potentially hung I/Os.
435 spa_deadman(void *arg
)
439 zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu",
440 (gethrtime() - spa
->spa_sync_starttime
) / NANOSEC
,
441 ++spa
->spa_deadman_calls
);
442 if (zfs_deadman_enabled
)
443 vdev_deadman(spa
->spa_root_vdev
);
445 spa
->spa_deadman_tqid
= taskq_dispatch_delay(system_taskq
,
446 spa_deadman
, spa
, TQ_PUSHPAGE
, ddi_get_lbolt() +
447 NSEC_TO_TICK(spa
->spa_deadman_synctime
));
451 * Create an uninitialized spa_t with the given name. Requires
452 * spa_namespace_lock. The caller must ensure that the spa_t doesn't already
453 * exist by calling spa_lookup() first.
456 spa_add(const char *name
, nvlist_t
*config
, const char *altroot
)
459 spa_config_dirent_t
*dp
;
462 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
464 spa
= kmem_zalloc(sizeof (spa_t
), KM_PUSHPAGE
| KM_NODEBUG
);
466 mutex_init(&spa
->spa_async_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
467 mutex_init(&spa
->spa_errlist_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
468 mutex_init(&spa
->spa_errlog_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
469 mutex_init(&spa
->spa_history_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
470 mutex_init(&spa
->spa_proc_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
471 mutex_init(&spa
->spa_props_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
472 mutex_init(&spa
->spa_scrub_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
473 mutex_init(&spa
->spa_suspend_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
474 mutex_init(&spa
->spa_vdev_top_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
476 cv_init(&spa
->spa_async_cv
, NULL
, CV_DEFAULT
, NULL
);
477 cv_init(&spa
->spa_proc_cv
, NULL
, CV_DEFAULT
, NULL
);
478 cv_init(&spa
->spa_scrub_io_cv
, NULL
, CV_DEFAULT
, NULL
);
479 cv_init(&spa
->spa_suspend_cv
, NULL
, CV_DEFAULT
, NULL
);
481 for (t
= 0; t
< TXG_SIZE
; t
++)
482 bplist_create(&spa
->spa_free_bplist
[t
]);
484 (void) strlcpy(spa
->spa_name
, name
, sizeof (spa
->spa_name
));
485 spa
->spa_state
= POOL_STATE_UNINITIALIZED
;
486 spa
->spa_freeze_txg
= UINT64_MAX
;
487 spa
->spa_final_txg
= UINT64_MAX
;
488 spa
->spa_load_max_txg
= UINT64_MAX
;
490 spa
->spa_proc_state
= SPA_PROC_NONE
;
492 spa
->spa_deadman_synctime
= MSEC2NSEC(zfs_deadman_synctime
*
493 zfs_txg_synctime_ms
);
495 refcount_create(&spa
->spa_refcount
);
496 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_stats_destroy(spa
);
584 spa_config_lock_destroy(spa
);
586 for (t
= 0; t
< TXG_SIZE
; t
++)
587 bplist_destroy(&spa
->spa_free_bplist
[t
]);
589 cv_destroy(&spa
->spa_async_cv
);
590 cv_destroy(&spa
->spa_proc_cv
);
591 cv_destroy(&spa
->spa_scrub_io_cv
);
592 cv_destroy(&spa
->spa_suspend_cv
);
594 mutex_destroy(&spa
->spa_async_lock
);
595 mutex_destroy(&spa
->spa_errlist_lock
);
596 mutex_destroy(&spa
->spa_errlog_lock
);
597 mutex_destroy(&spa
->spa_history_lock
);
598 mutex_destroy(&spa
->spa_proc_lock
);
599 mutex_destroy(&spa
->spa_props_lock
);
600 mutex_destroy(&spa
->spa_scrub_lock
);
601 mutex_destroy(&spa
->spa_suspend_lock
);
602 mutex_destroy(&spa
->spa_vdev_top_lock
);
604 kmem_free(spa
, sizeof (spa_t
));
608 * Given a pool, return the next pool in the namespace, or NULL if there is
609 * none. If 'prev' is NULL, return the first pool.
612 spa_next(spa_t
*prev
)
614 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
617 return (AVL_NEXT(&spa_namespace_avl
, prev
));
619 return (avl_first(&spa_namespace_avl
));
623 * ==========================================================================
624 * SPA refcount functions
625 * ==========================================================================
629 * Add a reference to the given spa_t. Must have at least one reference, or
630 * have the namespace lock held.
633 spa_open_ref(spa_t
*spa
, void *tag
)
635 ASSERT(refcount_count(&spa
->spa_refcount
) >= spa
->spa_minref
||
636 MUTEX_HELD(&spa_namespace_lock
));
637 (void) refcount_add(&spa
->spa_refcount
, tag
);
641 * Remove a reference to the given spa_t. Must have at least one reference, or
642 * have the namespace lock held.
645 spa_close(spa_t
*spa
, void *tag
)
647 ASSERT(refcount_count(&spa
->spa_refcount
) > spa
->spa_minref
||
648 MUTEX_HELD(&spa_namespace_lock
));
649 (void) refcount_remove(&spa
->spa_refcount
, tag
);
653 * Check to see if the spa refcount is zero. Must be called with
654 * spa_namespace_lock held. We really compare against spa_minref, which is the
655 * number of references acquired when opening a pool
658 spa_refcount_zero(spa_t
*spa
)
660 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
662 return (refcount_count(&spa
->spa_refcount
) == spa
->spa_minref
);
666 * ==========================================================================
667 * SPA spare and l2cache tracking
668 * ==========================================================================
672 * Hot spares and cache devices are tracked using the same code below,
673 * for 'auxiliary' devices.
676 typedef struct spa_aux
{
684 spa_aux_compare(const void *a
, const void *b
)
686 const spa_aux_t
*sa
= a
;
687 const spa_aux_t
*sb
= b
;
689 if (sa
->aux_guid
< sb
->aux_guid
)
691 else if (sa
->aux_guid
> sb
->aux_guid
)
698 spa_aux_add(vdev_t
*vd
, avl_tree_t
*avl
)
704 search
.aux_guid
= vd
->vdev_guid
;
705 if ((aux
= avl_find(avl
, &search
, &where
)) != NULL
) {
708 aux
= kmem_zalloc(sizeof (spa_aux_t
), KM_PUSHPAGE
);
709 aux
->aux_guid
= vd
->vdev_guid
;
711 avl_insert(avl
, aux
, where
);
716 spa_aux_remove(vdev_t
*vd
, avl_tree_t
*avl
)
722 search
.aux_guid
= vd
->vdev_guid
;
723 aux
= avl_find(avl
, &search
, &where
);
727 if (--aux
->aux_count
== 0) {
728 avl_remove(avl
, aux
);
729 kmem_free(aux
, sizeof (spa_aux_t
));
730 } else if (aux
->aux_pool
== spa_guid(vd
->vdev_spa
)) {
731 aux
->aux_pool
= 0ULL;
736 spa_aux_exists(uint64_t guid
, uint64_t *pool
, int *refcnt
, avl_tree_t
*avl
)
738 spa_aux_t search
, *found
;
740 search
.aux_guid
= guid
;
741 found
= avl_find(avl
, &search
, NULL
);
745 *pool
= found
->aux_pool
;
752 *refcnt
= found
->aux_count
;
757 return (found
!= NULL
);
761 spa_aux_activate(vdev_t
*vd
, avl_tree_t
*avl
)
763 spa_aux_t search
, *found
;
766 search
.aux_guid
= vd
->vdev_guid
;
767 found
= avl_find(avl
, &search
, &where
);
768 ASSERT(found
!= NULL
);
769 ASSERT(found
->aux_pool
== 0ULL);
771 found
->aux_pool
= spa_guid(vd
->vdev_spa
);
775 * Spares are tracked globally due to the following constraints:
777 * - A spare may be part of multiple pools.
778 * - A spare may be added to a pool even if it's actively in use within
780 * - A spare in use in any pool can only be the source of a replacement if
781 * the target is a spare in the same pool.
783 * We keep track of all spares on the system through the use of a reference
784 * counted AVL tree. When a vdev is added as a spare, or used as a replacement
785 * spare, then we bump the reference count in the AVL tree. In addition, we set
786 * the 'vdev_isspare' member to indicate that the device is a spare (active or
787 * inactive). When a spare is made active (used to replace a device in the
788 * pool), we also keep track of which pool its been made a part of.
790 * The 'spa_spare_lock' protects the AVL tree. These functions are normally
791 * called under the spa_namespace lock as part of vdev reconfiguration. The
792 * separate spare lock exists for the status query path, which does not need to
793 * be completely consistent with respect to other vdev configuration changes.
797 spa_spare_compare(const void *a
, const void *b
)
799 return (spa_aux_compare(a
, b
));
803 spa_spare_add(vdev_t
*vd
)
805 mutex_enter(&spa_spare_lock
);
806 ASSERT(!vd
->vdev_isspare
);
807 spa_aux_add(vd
, &spa_spare_avl
);
808 vd
->vdev_isspare
= B_TRUE
;
809 mutex_exit(&spa_spare_lock
);
813 spa_spare_remove(vdev_t
*vd
)
815 mutex_enter(&spa_spare_lock
);
816 ASSERT(vd
->vdev_isspare
);
817 spa_aux_remove(vd
, &spa_spare_avl
);
818 vd
->vdev_isspare
= B_FALSE
;
819 mutex_exit(&spa_spare_lock
);
823 spa_spare_exists(uint64_t guid
, uint64_t *pool
, int *refcnt
)
827 mutex_enter(&spa_spare_lock
);
828 found
= spa_aux_exists(guid
, pool
, refcnt
, &spa_spare_avl
);
829 mutex_exit(&spa_spare_lock
);
835 spa_spare_activate(vdev_t
*vd
)
837 mutex_enter(&spa_spare_lock
);
838 ASSERT(vd
->vdev_isspare
);
839 spa_aux_activate(vd
, &spa_spare_avl
);
840 mutex_exit(&spa_spare_lock
);
844 * Level 2 ARC devices are tracked globally for the same reasons as spares.
845 * Cache devices currently only support one pool per cache device, and so
846 * for these devices the aux reference count is currently unused beyond 1.
850 spa_l2cache_compare(const void *a
, const void *b
)
852 return (spa_aux_compare(a
, b
));
856 spa_l2cache_add(vdev_t
*vd
)
858 mutex_enter(&spa_l2cache_lock
);
859 ASSERT(!vd
->vdev_isl2cache
);
860 spa_aux_add(vd
, &spa_l2cache_avl
);
861 vd
->vdev_isl2cache
= B_TRUE
;
862 mutex_exit(&spa_l2cache_lock
);
866 spa_l2cache_remove(vdev_t
*vd
)
868 mutex_enter(&spa_l2cache_lock
);
869 ASSERT(vd
->vdev_isl2cache
);
870 spa_aux_remove(vd
, &spa_l2cache_avl
);
871 vd
->vdev_isl2cache
= B_FALSE
;
872 mutex_exit(&spa_l2cache_lock
);
876 spa_l2cache_exists(uint64_t guid
, uint64_t *pool
)
880 mutex_enter(&spa_l2cache_lock
);
881 found
= spa_aux_exists(guid
, pool
, NULL
, &spa_l2cache_avl
);
882 mutex_exit(&spa_l2cache_lock
);
888 spa_l2cache_activate(vdev_t
*vd
)
890 mutex_enter(&spa_l2cache_lock
);
891 ASSERT(vd
->vdev_isl2cache
);
892 spa_aux_activate(vd
, &spa_l2cache_avl
);
893 mutex_exit(&spa_l2cache_lock
);
897 * ==========================================================================
899 * ==========================================================================
903 * Lock the given spa_t for the purpose of adding or removing a vdev.
904 * Grabs the global spa_namespace_lock plus the spa config lock for writing.
905 * It returns the next transaction group for the spa_t.
908 spa_vdev_enter(spa_t
*spa
)
910 mutex_enter(&spa
->spa_vdev_top_lock
);
911 mutex_enter(&spa_namespace_lock
);
912 return (spa_vdev_config_enter(spa
));
916 * Internal implementation for spa_vdev_enter(). Used when a vdev
917 * operation requires multiple syncs (i.e. removing a device) while
918 * keeping the spa_namespace_lock held.
921 spa_vdev_config_enter(spa_t
*spa
)
923 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
925 spa_config_enter(spa
, SCL_ALL
, spa
, RW_WRITER
);
927 return (spa_last_synced_txg(spa
) + 1);
931 * Used in combination with spa_vdev_config_enter() to allow the syncing
932 * of multiple transactions without releasing the spa_namespace_lock.
935 spa_vdev_config_exit(spa_t
*spa
, vdev_t
*vd
, uint64_t txg
, int error
, char *tag
)
937 int config_changed
= B_FALSE
;
939 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
940 ASSERT(txg
> spa_last_synced_txg(spa
));
942 spa
->spa_pending_vdev
= NULL
;
947 vdev_dtl_reassess(spa
->spa_root_vdev
, 0, 0, B_FALSE
);
949 if (error
== 0 && !list_is_empty(&spa
->spa_config_dirty_list
)) {
950 config_changed
= B_TRUE
;
951 spa
->spa_config_generation
++;
955 * Verify the metaslab classes.
957 ASSERT(metaslab_class_validate(spa_normal_class(spa
)) == 0);
958 ASSERT(metaslab_class_validate(spa_log_class(spa
)) == 0);
960 spa_config_exit(spa
, SCL_ALL
, spa
);
963 * Panic the system if the specified tag requires it. This
964 * is useful for ensuring that configurations are updated
967 if (zio_injection_enabled
)
968 zio_handle_panic_injection(spa
, tag
, 0);
971 * Note: this txg_wait_synced() is important because it ensures
972 * that there won't be more than one config change per txg.
973 * This allows us to use the txg as the generation number.
976 txg_wait_synced(spa
->spa_dsl_pool
, txg
);
979 ASSERT(!vd
->vdev_detached
|| vd
->vdev_dtl_smo
.smo_object
== 0);
980 spa_config_enter(spa
, SCL_ALL
, spa
, RW_WRITER
);
982 spa_config_exit(spa
, SCL_ALL
, spa
);
986 * If the config changed, update the config cache.
989 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
993 * Unlock the spa_t after adding or removing a vdev. Besides undoing the
994 * locking of spa_vdev_enter(), we also want make sure the transactions have
995 * synced to disk, and then update the global configuration cache with the new
999 spa_vdev_exit(spa_t
*spa
, vdev_t
*vd
, uint64_t txg
, int error
)
1001 spa_vdev_config_exit(spa
, vd
, txg
, error
, FTAG
);
1002 mutex_exit(&spa_namespace_lock
);
1003 mutex_exit(&spa
->spa_vdev_top_lock
);
1009 * Lock the given spa_t for the purpose of changing vdev state.
1012 spa_vdev_state_enter(spa_t
*spa
, int oplocks
)
1014 int locks
= SCL_STATE_ALL
| oplocks
;
1017 * Root pools may need to read of the underlying devfs filesystem
1018 * when opening up a vdev. Unfortunately if we're holding the
1019 * SCL_ZIO lock it will result in a deadlock when we try to issue
1020 * the read from the root filesystem. Instead we "prefetch"
1021 * the associated vnodes that we need prior to opening the
1022 * underlying devices and cache them so that we can prevent
1023 * any I/O when we are doing the actual open.
1025 if (spa_is_root(spa
)) {
1026 int low
= locks
& ~(SCL_ZIO
- 1);
1027 int high
= locks
& ~low
;
1029 spa_config_enter(spa
, high
, spa
, RW_WRITER
);
1030 vdev_hold(spa
->spa_root_vdev
);
1031 spa_config_enter(spa
, low
, spa
, RW_WRITER
);
1033 spa_config_enter(spa
, locks
, spa
, RW_WRITER
);
1035 spa
->spa_vdev_locks
= locks
;
1039 spa_vdev_state_exit(spa_t
*spa
, vdev_t
*vd
, int error
)
1041 boolean_t config_changed
= B_FALSE
;
1043 if (vd
!= NULL
|| error
== 0)
1044 vdev_dtl_reassess(vd
? vd
->vdev_top
: spa
->spa_root_vdev
,
1048 vdev_state_dirty(vd
->vdev_top
);
1049 config_changed
= B_TRUE
;
1050 spa
->spa_config_generation
++;
1053 if (spa_is_root(spa
))
1054 vdev_rele(spa
->spa_root_vdev
);
1056 ASSERT3U(spa
->spa_vdev_locks
, >=, SCL_STATE_ALL
);
1057 spa_config_exit(spa
, spa
->spa_vdev_locks
, spa
);
1060 * If anything changed, wait for it to sync. This ensures that,
1061 * from the system administrator's perspective, zpool(1M) commands
1062 * are synchronous. This is important for things like zpool offline:
1063 * when the command completes, you expect no further I/O from ZFS.
1066 txg_wait_synced(spa
->spa_dsl_pool
, 0);
1069 * If the config changed, update the config cache.
1071 if (config_changed
) {
1072 mutex_enter(&spa_namespace_lock
);
1073 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
1074 mutex_exit(&spa_namespace_lock
);
1081 * ==========================================================================
1082 * Miscellaneous functions
1083 * ==========================================================================
1087 spa_activate_mos_feature(spa_t
*spa
, const char *feature
)
1089 (void) nvlist_add_boolean(spa
->spa_label_features
, feature
);
1090 vdev_config_dirty(spa
->spa_root_vdev
);
1094 spa_deactivate_mos_feature(spa_t
*spa
, const char *feature
)
1096 (void) nvlist_remove_all(spa
->spa_label_features
, feature
);
1097 vdev_config_dirty(spa
->spa_root_vdev
);
1104 spa_rename(const char *name
, const char *newname
)
1110 * Lookup the spa_t and grab the config lock for writing. We need to
1111 * actually open the pool so that we can sync out the necessary labels.
1112 * It's OK to call spa_open() with the namespace lock held because we
1113 * allow recursive calls for other reasons.
1115 mutex_enter(&spa_namespace_lock
);
1116 if ((err
= spa_open(name
, &spa
, FTAG
)) != 0) {
1117 mutex_exit(&spa_namespace_lock
);
1121 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1123 avl_remove(&spa_namespace_avl
, spa
);
1124 (void) strlcpy(spa
->spa_name
, newname
, sizeof (spa
->spa_name
));
1125 avl_add(&spa_namespace_avl
, spa
);
1128 * Sync all labels to disk with the new names by marking the root vdev
1129 * dirty and waiting for it to sync. It will pick up the new pool name
1132 vdev_config_dirty(spa
->spa_root_vdev
);
1134 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1136 txg_wait_synced(spa
->spa_dsl_pool
, 0);
1139 * Sync the updated config cache.
1141 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
1143 spa_close(spa
, FTAG
);
1145 mutex_exit(&spa_namespace_lock
);
1151 * Return the spa_t associated with given pool_guid, if it exists. If
1152 * device_guid is non-zero, determine whether the pool exists *and* contains
1153 * a device with the specified device_guid.
1156 spa_by_guid(uint64_t pool_guid
, uint64_t device_guid
)
1159 avl_tree_t
*t
= &spa_namespace_avl
;
1161 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1163 for (spa
= avl_first(t
); spa
!= NULL
; spa
= AVL_NEXT(t
, spa
)) {
1164 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
)
1166 if (spa
->spa_root_vdev
== NULL
)
1168 if (spa_guid(spa
) == pool_guid
) {
1169 if (device_guid
== 0)
1172 if (vdev_lookup_by_guid(spa
->spa_root_vdev
,
1173 device_guid
) != NULL
)
1177 * Check any devices we may be in the process of adding.
1179 if (spa
->spa_pending_vdev
) {
1180 if (vdev_lookup_by_guid(spa
->spa_pending_vdev
,
1181 device_guid
) != NULL
)
1191 * Determine whether a pool with the given pool_guid exists.
1194 spa_guid_exists(uint64_t pool_guid
, uint64_t device_guid
)
1196 return (spa_by_guid(pool_guid
, device_guid
) != NULL
);
1200 spa_strdup(const char *s
)
1206 new = kmem_alloc(len
+ 1, KM_PUSHPAGE
);
1214 spa_strfree(char *s
)
1216 kmem_free(s
, strlen(s
) + 1);
1220 spa_get_random(uint64_t range
)
1226 (void) random_get_pseudo_bytes((void *)&r
, sizeof (uint64_t));
1232 spa_generate_guid(spa_t
*spa
)
1234 uint64_t guid
= spa_get_random(-1ULL);
1237 while (guid
== 0 || spa_guid_exists(spa_guid(spa
), guid
))
1238 guid
= spa_get_random(-1ULL);
1240 while (guid
== 0 || spa_guid_exists(guid
, 0))
1241 guid
= spa_get_random(-1ULL);
1248 sprintf_blkptr(char *buf
, const blkptr_t
*bp
)
1251 char *checksum
= NULL
;
1252 char *compress
= NULL
;
1255 if (BP_GET_TYPE(bp
) & DMU_OT_NEWTYPE
) {
1256 dmu_object_byteswap_t bswap
=
1257 DMU_OT_BYTESWAP(BP_GET_TYPE(bp
));
1258 (void) snprintf(type
, sizeof (type
), "bswap %s %s",
1259 DMU_OT_IS_METADATA(BP_GET_TYPE(bp
)) ?
1260 "metadata" : "data",
1261 dmu_ot_byteswap
[bswap
].ob_name
);
1263 (void) strlcpy(type
, dmu_ot
[BP_GET_TYPE(bp
)].ot_name
,
1266 checksum
= zio_checksum_table
[BP_GET_CHECKSUM(bp
)].ci_name
;
1267 compress
= zio_compress_table
[BP_GET_COMPRESS(bp
)].ci_name
;
1270 SPRINTF_BLKPTR(snprintf
, ' ', buf
, bp
, type
, checksum
, compress
);
1274 spa_freeze(spa_t
*spa
)
1276 uint64_t freeze_txg
= 0;
1278 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1279 if (spa
->spa_freeze_txg
== UINT64_MAX
) {
1280 freeze_txg
= spa_last_synced_txg(spa
) + TXG_SIZE
;
1281 spa
->spa_freeze_txg
= freeze_txg
;
1283 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1284 if (freeze_txg
!= 0)
1285 txg_wait_synced(spa_get_dsl(spa
), freeze_txg
);
1289 * This is a stripped-down version of strtoull, suitable only for converting
1290 * lowercase hexadecimal numbers that don't overflow.
1293 strtonum(const char *str
, char **nptr
)
1299 while ((c
= *str
) != '\0') {
1300 if (c
>= '0' && c
<= '9')
1302 else if (c
>= 'a' && c
<= 'f')
1303 digit
= 10 + c
- 'a';
1314 *nptr
= (char *)str
;
1320 * ==========================================================================
1321 * Accessor functions
1322 * ==========================================================================
1326 spa_shutting_down(spa_t
*spa
)
1328 return (spa
->spa_async_suspended
);
1332 spa_get_dsl(spa_t
*spa
)
1334 return (spa
->spa_dsl_pool
);
1338 spa_is_initializing(spa_t
*spa
)
1340 return (spa
->spa_is_initializing
);
1344 spa_get_rootblkptr(spa_t
*spa
)
1346 return (&spa
->spa_ubsync
.ub_rootbp
);
1350 spa_set_rootblkptr(spa_t
*spa
, const blkptr_t
*bp
)
1352 spa
->spa_uberblock
.ub_rootbp
= *bp
;
1356 spa_altroot(spa_t
*spa
, char *buf
, size_t buflen
)
1358 if (spa
->spa_root
== NULL
)
1361 (void) strncpy(buf
, spa
->spa_root
, buflen
);
1365 spa_sync_pass(spa_t
*spa
)
1367 return (spa
->spa_sync_pass
);
1371 spa_name(spa_t
*spa
)
1373 return (spa
->spa_name
);
1377 spa_guid(spa_t
*spa
)
1379 dsl_pool_t
*dp
= spa_get_dsl(spa
);
1383 * If we fail to parse the config during spa_load(), we can go through
1384 * the error path (which posts an ereport) and end up here with no root
1385 * vdev. We stash the original pool guid in 'spa_config_guid' to handle
1388 if (spa
->spa_root_vdev
== NULL
)
1389 return (spa
->spa_config_guid
);
1391 guid
= spa
->spa_last_synced_guid
!= 0 ?
1392 spa
->spa_last_synced_guid
: spa
->spa_root_vdev
->vdev_guid
;
1395 * Return the most recently synced out guid unless we're
1396 * in syncing context.
1398 if (dp
&& dsl_pool_sync_context(dp
))
1399 return (spa
->spa_root_vdev
->vdev_guid
);
1405 spa_load_guid(spa_t
*spa
)
1408 * This is a GUID that exists solely as a reference for the
1409 * purposes of the arc. It is generated at load time, and
1410 * is never written to persistent storage.
1412 return (spa
->spa_load_guid
);
1416 spa_last_synced_txg(spa_t
*spa
)
1418 return (spa
->spa_ubsync
.ub_txg
);
1422 spa_first_txg(spa_t
*spa
)
1424 return (spa
->spa_first_txg
);
1428 spa_syncing_txg(spa_t
*spa
)
1430 return (spa
->spa_syncing_txg
);
1434 spa_state(spa_t
*spa
)
1436 return (spa
->spa_state
);
1440 spa_load_state(spa_t
*spa
)
1442 return (spa
->spa_load_state
);
1446 spa_freeze_txg(spa_t
*spa
)
1448 return (spa
->spa_freeze_txg
);
1453 spa_get_asize(spa_t
*spa
, uint64_t lsize
)
1456 * The worst case is single-sector max-parity RAID-Z blocks, in which
1457 * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1)
1458 * times the size; so just assume that. Add to this the fact that
1459 * we can have up to 3 DVAs per bp, and one more factor of 2 because
1460 * the block may be dittoed with up to 3 DVAs by ddt_sync().
1462 return (lsize
* (VDEV_RAIDZ_MAXPARITY
+ 1) * SPA_DVAS_PER_BP
* 2);
1466 spa_get_dspace(spa_t
*spa
)
1468 return (spa
->spa_dspace
);
1472 spa_update_dspace(spa_t
*spa
)
1474 spa
->spa_dspace
= metaslab_class_get_dspace(spa_normal_class(spa
)) +
1475 ddt_get_dedup_dspace(spa
);
1479 * Return the failure mode that has been set to this pool. The default
1480 * behavior will be to block all I/Os when a complete failure occurs.
1483 spa_get_failmode(spa_t
*spa
)
1485 return (spa
->spa_failmode
);
1489 spa_suspended(spa_t
*spa
)
1491 return (spa
->spa_suspended
);
1495 spa_version(spa_t
*spa
)
1497 return (spa
->spa_ubsync
.ub_version
);
1501 spa_deflate(spa_t
*spa
)
1503 return (spa
->spa_deflate
);
1507 spa_normal_class(spa_t
*spa
)
1509 return (spa
->spa_normal_class
);
1513 spa_log_class(spa_t
*spa
)
1515 return (spa
->spa_log_class
);
1519 spa_max_replication(spa_t
*spa
)
1522 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
1523 * handle BPs with more than one DVA allocated. Set our max
1524 * replication level accordingly.
1526 if (spa_version(spa
) < SPA_VERSION_DITTO_BLOCKS
)
1528 return (MIN(SPA_DVAS_PER_BP
, spa_max_replication_override
));
1532 spa_prev_software_version(spa_t
*spa
)
1534 return (spa
->spa_prev_software_version
);
1538 spa_deadman_synctime(spa_t
*spa
)
1540 return (spa
->spa_deadman_synctime
);
1544 dva_get_dsize_sync(spa_t
*spa
, const dva_t
*dva
)
1546 uint64_t asize
= DVA_GET_ASIZE(dva
);
1547 uint64_t dsize
= asize
;
1549 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_READER
) != 0);
1551 if (asize
!= 0 && spa
->spa_deflate
) {
1552 vdev_t
*vd
= vdev_lookup_top(spa
, DVA_GET_VDEV(dva
));
1553 dsize
= (asize
>> SPA_MINBLOCKSHIFT
) * vd
->vdev_deflate_ratio
;
1560 bp_get_dsize_sync(spa_t
*spa
, const blkptr_t
*bp
)
1565 for (d
= 0; d
< SPA_DVAS_PER_BP
; d
++)
1566 dsize
+= dva_get_dsize_sync(spa
, &bp
->blk_dva
[d
]);
1572 bp_get_dsize(spa_t
*spa
, const blkptr_t
*bp
)
1577 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
1579 for (d
= 0; d
< SPA_DVAS_PER_BP
; d
++)
1580 dsize
+= dva_get_dsize_sync(spa
, &bp
->blk_dva
[d
]);
1582 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
1588 * ==========================================================================
1589 * Initialization and Termination
1590 * ==========================================================================
1594 spa_name_compare(const void *a1
, const void *a2
)
1596 const spa_t
*s1
= a1
;
1597 const spa_t
*s2
= a2
;
1600 s
= strcmp(s1
->spa_name
, s2
->spa_name
);
1617 mutex_init(&spa_namespace_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1618 mutex_init(&spa_spare_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1619 mutex_init(&spa_l2cache_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1620 cv_init(&spa_namespace_cv
, NULL
, CV_DEFAULT
, NULL
);
1622 avl_create(&spa_namespace_avl
, spa_name_compare
, sizeof (spa_t
),
1623 offsetof(spa_t
, spa_avl
));
1625 avl_create(&spa_spare_avl
, spa_spare_compare
, sizeof (spa_aux_t
),
1626 offsetof(spa_aux_t
, aux_avl
));
1628 avl_create(&spa_l2cache_avl
, spa_l2cache_compare
, sizeof (spa_aux_t
),
1629 offsetof(spa_aux_t
, aux_avl
));
1631 spa_mode_global
= mode
;
1634 if (spa_mode_global
!= FREAD
&& dprintf_find_string("watch")) {
1635 struct sigaction sa
;
1637 sa
.sa_flags
= SA_SIGINFO
;
1638 sigemptyset(&sa
.sa_mask
);
1639 sa
.sa_sigaction
= arc_buf_sigsegv
;
1641 if (sigaction(SIGSEGV
, &sa
, NULL
) == -1) {
1642 perror("could not enable watchpoints: "
1643 "sigaction(SIGSEGV, ...) = ");
1657 vdev_cache_stat_init();
1660 zpool_feature_init();
1672 vdev_cache_stat_fini();
1681 avl_destroy(&spa_namespace_avl
);
1682 avl_destroy(&spa_spare_avl
);
1683 avl_destroy(&spa_l2cache_avl
);
1685 cv_destroy(&spa_namespace_cv
);
1686 mutex_destroy(&spa_namespace_lock
);
1687 mutex_destroy(&spa_spare_lock
);
1688 mutex_destroy(&spa_l2cache_lock
);
1692 * Return whether this pool has slogs. No locking needed.
1693 * It's not a problem if the wrong answer is returned as it's only for
1694 * performance and not correctness
1697 spa_has_slogs(spa_t
*spa
)
1699 return (spa
->spa_log_class
->mc_rotor
!= NULL
);
1703 spa_get_log_state(spa_t
*spa
)
1705 return (spa
->spa_log_state
);
1709 spa_set_log_state(spa_t
*spa
, spa_log_state_t state
)
1711 spa
->spa_log_state
= state
;
1715 spa_is_root(spa_t
*spa
)
1717 return (spa
->spa_is_root
);
1721 spa_writeable(spa_t
*spa
)
1723 return (!!(spa
->spa_mode
& FWRITE
));
1727 spa_mode(spa_t
*spa
)
1729 return (spa
->spa_mode
);
1733 spa_bootfs(spa_t
*spa
)
1735 return (spa
->spa_bootfs
);
1739 spa_delegation(spa_t
*spa
)
1741 return (spa
->spa_delegation
);
1745 spa_meta_objset(spa_t
*spa
)
1747 return (spa
->spa_meta_objset
);
1751 spa_dedup_checksum(spa_t
*spa
)
1753 return (spa
->spa_dedup_checksum
);
1757 * Reset pool scan stat per scan pass (or reboot).
1760 spa_scan_stat_init(spa_t
*spa
)
1762 /* data not stored on disk */
1763 spa
->spa_scan_pass_start
= gethrestime_sec();
1764 spa
->spa_scan_pass_exam
= 0;
1765 vdev_scan_stat_init(spa
->spa_root_vdev
);
1769 * Get scan stats for zpool status reports
1772 spa_scan_get_stats(spa_t
*spa
, pool_scan_stat_t
*ps
)
1774 dsl_scan_t
*scn
= spa
->spa_dsl_pool
? spa
->spa_dsl_pool
->dp_scan
: NULL
;
1776 if (scn
== NULL
|| scn
->scn_phys
.scn_func
== POOL_SCAN_NONE
)
1777 return (SET_ERROR(ENOENT
));
1778 bzero(ps
, sizeof (pool_scan_stat_t
));
1780 /* data stored on disk */
1781 ps
->pss_func
= scn
->scn_phys
.scn_func
;
1782 ps
->pss_start_time
= scn
->scn_phys
.scn_start_time
;
1783 ps
->pss_end_time
= scn
->scn_phys
.scn_end_time
;
1784 ps
->pss_to_examine
= scn
->scn_phys
.scn_to_examine
;
1785 ps
->pss_examined
= scn
->scn_phys
.scn_examined
;
1786 ps
->pss_to_process
= scn
->scn_phys
.scn_to_process
;
1787 ps
->pss_processed
= scn
->scn_phys
.scn_processed
;
1788 ps
->pss_errors
= scn
->scn_phys
.scn_errors
;
1789 ps
->pss_state
= scn
->scn_phys
.scn_state
;
1791 /* data not stored on disk */
1792 ps
->pss_pass_start
= spa
->spa_scan_pass_start
;
1793 ps
->pss_pass_exam
= spa
->spa_scan_pass_exam
;
1799 spa_debug_enabled(spa_t
*spa
)
1801 return (spa
->spa_debug
);
1804 #if defined(_KERNEL) && defined(HAVE_SPL)
1805 /* Namespace manipulation */
1806 EXPORT_SYMBOL(spa_lookup
);
1807 EXPORT_SYMBOL(spa_add
);
1808 EXPORT_SYMBOL(spa_remove
);
1809 EXPORT_SYMBOL(spa_next
);
1811 /* Refcount functions */
1812 EXPORT_SYMBOL(spa_open_ref
);
1813 EXPORT_SYMBOL(spa_close
);
1814 EXPORT_SYMBOL(spa_refcount_zero
);
1816 /* Pool configuration lock */
1817 EXPORT_SYMBOL(spa_config_tryenter
);
1818 EXPORT_SYMBOL(spa_config_enter
);
1819 EXPORT_SYMBOL(spa_config_exit
);
1820 EXPORT_SYMBOL(spa_config_held
);
1822 /* Pool vdev add/remove lock */
1823 EXPORT_SYMBOL(spa_vdev_enter
);
1824 EXPORT_SYMBOL(spa_vdev_exit
);
1826 /* Pool vdev state change lock */
1827 EXPORT_SYMBOL(spa_vdev_state_enter
);
1828 EXPORT_SYMBOL(spa_vdev_state_exit
);
1830 /* Accessor functions */
1831 EXPORT_SYMBOL(spa_shutting_down
);
1832 EXPORT_SYMBOL(spa_get_dsl
);
1833 EXPORT_SYMBOL(spa_get_rootblkptr
);
1834 EXPORT_SYMBOL(spa_set_rootblkptr
);
1835 EXPORT_SYMBOL(spa_altroot
);
1836 EXPORT_SYMBOL(spa_sync_pass
);
1837 EXPORT_SYMBOL(spa_name
);
1838 EXPORT_SYMBOL(spa_guid
);
1839 EXPORT_SYMBOL(spa_last_synced_txg
);
1840 EXPORT_SYMBOL(spa_first_txg
);
1841 EXPORT_SYMBOL(spa_syncing_txg
);
1842 EXPORT_SYMBOL(spa_version
);
1843 EXPORT_SYMBOL(spa_state
);
1844 EXPORT_SYMBOL(spa_load_state
);
1845 EXPORT_SYMBOL(spa_freeze_txg
);
1846 EXPORT_SYMBOL(spa_get_asize
);
1847 EXPORT_SYMBOL(spa_get_dspace
);
1848 EXPORT_SYMBOL(spa_update_dspace
);
1849 EXPORT_SYMBOL(spa_deflate
);
1850 EXPORT_SYMBOL(spa_normal_class
);
1851 EXPORT_SYMBOL(spa_log_class
);
1852 EXPORT_SYMBOL(spa_max_replication
);
1853 EXPORT_SYMBOL(spa_prev_software_version
);
1854 EXPORT_SYMBOL(spa_get_failmode
);
1855 EXPORT_SYMBOL(spa_suspended
);
1856 EXPORT_SYMBOL(spa_bootfs
);
1857 EXPORT_SYMBOL(spa_delegation
);
1858 EXPORT_SYMBOL(spa_meta_objset
);
1860 /* Miscellaneous support routines */
1861 EXPORT_SYMBOL(spa_rename
);
1862 EXPORT_SYMBOL(spa_guid_exists
);
1863 EXPORT_SYMBOL(spa_strdup
);
1864 EXPORT_SYMBOL(spa_strfree
);
1865 EXPORT_SYMBOL(spa_get_random
);
1866 EXPORT_SYMBOL(spa_generate_guid
);
1867 EXPORT_SYMBOL(sprintf_blkptr
);
1868 EXPORT_SYMBOL(spa_freeze
);
1869 EXPORT_SYMBOL(spa_upgrade
);
1870 EXPORT_SYMBOL(spa_evict_all
);
1871 EXPORT_SYMBOL(spa_lookup_by_guid
);
1872 EXPORT_SYMBOL(spa_has_spare
);
1873 EXPORT_SYMBOL(dva_get_dsize_sync
);
1874 EXPORT_SYMBOL(bp_get_dsize_sync
);
1875 EXPORT_SYMBOL(bp_get_dsize
);
1876 EXPORT_SYMBOL(spa_has_slogs
);
1877 EXPORT_SYMBOL(spa_is_root
);
1878 EXPORT_SYMBOL(spa_writeable
);
1879 EXPORT_SYMBOL(spa_mode
);
1881 EXPORT_SYMBOL(spa_namespace_lock
);
1883 module_param(zfs_deadman_synctime
, ulong
, 0644);
1884 MODULE_PARM_DESC(zfs_deadman_synctime
,"Expire in units of zfs_txg_synctime_ms");
1886 module_param(zfs_deadman_enabled
, int, 0644);
1887 MODULE_PARM_DESC(zfs_deadman_enabled
, "Enable deadman timer");