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 milliseconds. This value has two meanings. First it is
242 * used to determine when the spa_deadman() logic should fire. By default the
243 * spa_deadman() will fire if spa_sync() has not completed in 1000 seconds.
244 * Secondly, the value determines if an I/O is considered "hung". Any I/O that
245 * has not completed in zfs_deadman_synctime_ms is considered "hung" resulting
248 unsigned long zfs_deadman_synctime_ms
= 1000000ULL;
251 * By default the deadman is enabled.
253 int zfs_deadman_enabled
= 1;
256 * The worst case is single-sector max-parity RAID-Z blocks, in which
257 * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1)
258 * times the size; so just assume that. Add to this the fact that
259 * we can have up to 3 DVAs per bp, and one more factor of 2 because
260 * the block may be dittoed with up to 3 DVAs by ddt_sync(). All together,
262 * (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2 == 24
264 int spa_asize_inflation
= 24;
267 * ==========================================================================
269 * ==========================================================================
272 spa_config_lock_init(spa_t
*spa
)
276 for (i
= 0; i
< SCL_LOCKS
; i
++) {
277 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
278 mutex_init(&scl
->scl_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
279 cv_init(&scl
->scl_cv
, NULL
, CV_DEFAULT
, NULL
);
280 refcount_create_untracked(&scl
->scl_count
);
281 scl
->scl_writer
= NULL
;
282 scl
->scl_write_wanted
= 0;
287 spa_config_lock_destroy(spa_t
*spa
)
291 for (i
= 0; i
< SCL_LOCKS
; i
++) {
292 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
293 mutex_destroy(&scl
->scl_lock
);
294 cv_destroy(&scl
->scl_cv
);
295 refcount_destroy(&scl
->scl_count
);
296 ASSERT(scl
->scl_writer
== NULL
);
297 ASSERT(scl
->scl_write_wanted
== 0);
302 spa_config_tryenter(spa_t
*spa
, int locks
, void *tag
, krw_t rw
)
306 for (i
= 0; i
< SCL_LOCKS
; i
++) {
307 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
308 if (!(locks
& (1 << i
)))
310 mutex_enter(&scl
->scl_lock
);
311 if (rw
== RW_READER
) {
312 if (scl
->scl_writer
|| scl
->scl_write_wanted
) {
313 mutex_exit(&scl
->scl_lock
);
314 spa_config_exit(spa
, locks
^ (1 << i
), tag
);
318 ASSERT(scl
->scl_writer
!= curthread
);
319 if (!refcount_is_zero(&scl
->scl_count
)) {
320 mutex_exit(&scl
->scl_lock
);
321 spa_config_exit(spa
, locks
^ (1 << i
), tag
);
324 scl
->scl_writer
= curthread
;
326 (void) refcount_add(&scl
->scl_count
, tag
);
327 mutex_exit(&scl
->scl_lock
);
333 spa_config_enter(spa_t
*spa
, int locks
, void *tag
, krw_t rw
)
338 ASSERT3U(SCL_LOCKS
, <, sizeof (wlocks_held
) * NBBY
);
340 for (i
= 0; i
< SCL_LOCKS
; i
++) {
341 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
342 if (scl
->scl_writer
== curthread
)
343 wlocks_held
|= (1 << i
);
344 if (!(locks
& (1 << i
)))
346 mutex_enter(&scl
->scl_lock
);
347 if (rw
== RW_READER
) {
348 while (scl
->scl_writer
|| scl
->scl_write_wanted
) {
349 cv_wait(&scl
->scl_cv
, &scl
->scl_lock
);
352 ASSERT(scl
->scl_writer
!= curthread
);
353 while (!refcount_is_zero(&scl
->scl_count
)) {
354 scl
->scl_write_wanted
++;
355 cv_wait(&scl
->scl_cv
, &scl
->scl_lock
);
356 scl
->scl_write_wanted
--;
358 scl
->scl_writer
= curthread
;
360 (void) refcount_add(&scl
->scl_count
, tag
);
361 mutex_exit(&scl
->scl_lock
);
363 ASSERT(wlocks_held
<= locks
);
367 spa_config_exit(spa_t
*spa
, int locks
, void *tag
)
371 for (i
= SCL_LOCKS
- 1; i
>= 0; i
--) {
372 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
373 if (!(locks
& (1 << i
)))
375 mutex_enter(&scl
->scl_lock
);
376 ASSERT(!refcount_is_zero(&scl
->scl_count
));
377 if (refcount_remove(&scl
->scl_count
, tag
) == 0) {
378 ASSERT(scl
->scl_writer
== NULL
||
379 scl
->scl_writer
== curthread
);
380 scl
->scl_writer
= NULL
; /* OK in either case */
381 cv_broadcast(&scl
->scl_cv
);
383 mutex_exit(&scl
->scl_lock
);
388 spa_config_held(spa_t
*spa
, int locks
, krw_t rw
)
390 int i
, locks_held
= 0;
392 for (i
= 0; i
< SCL_LOCKS
; i
++) {
393 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
394 if (!(locks
& (1 << i
)))
396 if ((rw
== RW_READER
&& !refcount_is_zero(&scl
->scl_count
)) ||
397 (rw
== RW_WRITER
&& scl
->scl_writer
== curthread
))
398 locks_held
|= 1 << i
;
405 * ==========================================================================
406 * SPA namespace functions
407 * ==========================================================================
411 * Lookup the named spa_t in the AVL tree. The spa_namespace_lock must be held.
412 * Returns NULL if no matching spa_t is found.
415 spa_lookup(const char *name
)
417 static spa_t search
; /* spa_t is large; don't allocate on stack */
422 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
424 (void) strlcpy(search
.spa_name
, name
, sizeof (search
.spa_name
));
427 * If it's a full dataset name, figure out the pool name and
430 cp
= strpbrk(search
.spa_name
, "/@");
434 spa
= avl_find(&spa_namespace_avl
, &search
, &where
);
440 * Fires when spa_sync has not completed within zfs_deadman_synctime_ms.
441 * If the zfs_deadman_enabled flag is set then it inspects all vdev queues
442 * looking for potentially hung I/Os.
445 spa_deadman(void *arg
)
449 zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu",
450 (gethrtime() - spa
->spa_sync_starttime
) / NANOSEC
,
451 ++spa
->spa_deadman_calls
);
452 if (zfs_deadman_enabled
)
453 vdev_deadman(spa
->spa_root_vdev
);
455 spa
->spa_deadman_tqid
= taskq_dispatch_delay(system_taskq
,
456 spa_deadman
, spa
, TQ_PUSHPAGE
, ddi_get_lbolt() +
457 NSEC_TO_TICK(spa
->spa_deadman_synctime
));
461 * Create an uninitialized spa_t with the given name. Requires
462 * spa_namespace_lock. The caller must ensure that the spa_t doesn't already
463 * exist by calling spa_lookup() first.
466 spa_add(const char *name
, nvlist_t
*config
, const char *altroot
)
469 spa_config_dirent_t
*dp
;
472 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
474 spa
= kmem_zalloc(sizeof (spa_t
), KM_PUSHPAGE
| KM_NODEBUG
);
476 mutex_init(&spa
->spa_async_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
477 mutex_init(&spa
->spa_errlist_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
478 mutex_init(&spa
->spa_errlog_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
479 mutex_init(&spa
->spa_history_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
480 mutex_init(&spa
->spa_proc_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
481 mutex_init(&spa
->spa_props_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
482 mutex_init(&spa
->spa_scrub_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
483 mutex_init(&spa
->spa_suspend_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
484 mutex_init(&spa
->spa_vdev_top_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
486 cv_init(&spa
->spa_async_cv
, NULL
, CV_DEFAULT
, NULL
);
487 cv_init(&spa
->spa_proc_cv
, NULL
, CV_DEFAULT
, NULL
);
488 cv_init(&spa
->spa_scrub_io_cv
, NULL
, CV_DEFAULT
, NULL
);
489 cv_init(&spa
->spa_suspend_cv
, NULL
, CV_DEFAULT
, NULL
);
491 for (t
= 0; t
< TXG_SIZE
; t
++)
492 bplist_create(&spa
->spa_free_bplist
[t
]);
494 (void) strlcpy(spa
->spa_name
, name
, sizeof (spa
->spa_name
));
495 spa
->spa_state
= POOL_STATE_UNINITIALIZED
;
496 spa
->spa_freeze_txg
= UINT64_MAX
;
497 spa
->spa_final_txg
= UINT64_MAX
;
498 spa
->spa_load_max_txg
= UINT64_MAX
;
500 spa
->spa_proc_state
= SPA_PROC_NONE
;
502 spa
->spa_deadman_synctime
= MSEC2NSEC(zfs_deadman_synctime_ms
);
504 refcount_create(&spa
->spa_refcount
);
505 spa_config_lock_init(spa
);
508 avl_add(&spa_namespace_avl
, spa
);
511 * Set the alternate root, if there is one.
514 spa
->spa_root
= spa_strdup(altroot
);
519 * Every pool starts with the default cachefile
521 list_create(&spa
->spa_config_list
, sizeof (spa_config_dirent_t
),
522 offsetof(spa_config_dirent_t
, scd_link
));
524 dp
= kmem_zalloc(sizeof (spa_config_dirent_t
), KM_PUSHPAGE
);
525 dp
->scd_path
= altroot
? NULL
: spa_strdup(spa_config_path
);
526 list_insert_head(&spa
->spa_config_list
, dp
);
528 VERIFY(nvlist_alloc(&spa
->spa_load_info
, NV_UNIQUE_NAME
,
531 if (config
!= NULL
) {
534 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_FEATURES_FOR_READ
,
536 VERIFY(nvlist_dup(features
, &spa
->spa_label_features
,
540 VERIFY(nvlist_dup(config
, &spa
->spa_config
, 0) == 0);
543 if (spa
->spa_label_features
== NULL
) {
544 VERIFY(nvlist_alloc(&spa
->spa_label_features
, NV_UNIQUE_NAME
,
548 spa
->spa_debug
= ((zfs_flags
& ZFS_DEBUG_SPA
) != 0);
554 * Removes a spa_t from the namespace, freeing up any memory used. Requires
555 * spa_namespace_lock. This is called only after the spa_t has been closed and
559 spa_remove(spa_t
*spa
)
561 spa_config_dirent_t
*dp
;
564 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
565 ASSERT(spa
->spa_state
== POOL_STATE_UNINITIALIZED
);
567 nvlist_free(spa
->spa_config_splitting
);
569 avl_remove(&spa_namespace_avl
, spa
);
570 cv_broadcast(&spa_namespace_cv
);
573 spa_strfree(spa
->spa_root
);
577 while ((dp
= list_head(&spa
->spa_config_list
)) != NULL
) {
578 list_remove(&spa
->spa_config_list
, dp
);
579 if (dp
->scd_path
!= NULL
)
580 spa_strfree(dp
->scd_path
);
581 kmem_free(dp
, sizeof (spa_config_dirent_t
));
584 list_destroy(&spa
->spa_config_list
);
586 nvlist_free(spa
->spa_label_features
);
587 nvlist_free(spa
->spa_load_info
);
588 spa_config_set(spa
, NULL
);
590 refcount_destroy(&spa
->spa_refcount
);
592 spa_stats_destroy(spa
);
593 spa_config_lock_destroy(spa
);
595 for (t
= 0; t
< TXG_SIZE
; t
++)
596 bplist_destroy(&spa
->spa_free_bplist
[t
]);
598 cv_destroy(&spa
->spa_async_cv
);
599 cv_destroy(&spa
->spa_proc_cv
);
600 cv_destroy(&spa
->spa_scrub_io_cv
);
601 cv_destroy(&spa
->spa_suspend_cv
);
603 mutex_destroy(&spa
->spa_async_lock
);
604 mutex_destroy(&spa
->spa_errlist_lock
);
605 mutex_destroy(&spa
->spa_errlog_lock
);
606 mutex_destroy(&spa
->spa_history_lock
);
607 mutex_destroy(&spa
->spa_proc_lock
);
608 mutex_destroy(&spa
->spa_props_lock
);
609 mutex_destroy(&spa
->spa_scrub_lock
);
610 mutex_destroy(&spa
->spa_suspend_lock
);
611 mutex_destroy(&spa
->spa_vdev_top_lock
);
613 kmem_free(spa
, sizeof (spa_t
));
617 * Given a pool, return the next pool in the namespace, or NULL if there is
618 * none. If 'prev' is NULL, return the first pool.
621 spa_next(spa_t
*prev
)
623 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
626 return (AVL_NEXT(&spa_namespace_avl
, prev
));
628 return (avl_first(&spa_namespace_avl
));
632 * ==========================================================================
633 * SPA refcount functions
634 * ==========================================================================
638 * Add a reference to the given spa_t. Must have at least one reference, or
639 * have the namespace lock held.
642 spa_open_ref(spa_t
*spa
, void *tag
)
644 ASSERT(refcount_count(&spa
->spa_refcount
) >= spa
->spa_minref
||
645 MUTEX_HELD(&spa_namespace_lock
));
646 (void) refcount_add(&spa
->spa_refcount
, tag
);
650 * Remove a reference to the given spa_t. Must have at least one reference, or
651 * have the namespace lock held.
654 spa_close(spa_t
*spa
, void *tag
)
656 ASSERT(refcount_count(&spa
->spa_refcount
) > spa
->spa_minref
||
657 MUTEX_HELD(&spa_namespace_lock
));
658 (void) refcount_remove(&spa
->spa_refcount
, tag
);
662 * Check to see if the spa refcount is zero. Must be called with
663 * spa_namespace_lock held. We really compare against spa_minref, which is the
664 * number of references acquired when opening a pool
667 spa_refcount_zero(spa_t
*spa
)
669 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
671 return (refcount_count(&spa
->spa_refcount
) == spa
->spa_minref
);
675 * ==========================================================================
676 * SPA spare and l2cache tracking
677 * ==========================================================================
681 * Hot spares and cache devices are tracked using the same code below,
682 * for 'auxiliary' devices.
685 typedef struct spa_aux
{
693 spa_aux_compare(const void *a
, const void *b
)
695 const spa_aux_t
*sa
= a
;
696 const spa_aux_t
*sb
= b
;
698 if (sa
->aux_guid
< sb
->aux_guid
)
700 else if (sa
->aux_guid
> sb
->aux_guid
)
707 spa_aux_add(vdev_t
*vd
, avl_tree_t
*avl
)
713 search
.aux_guid
= vd
->vdev_guid
;
714 if ((aux
= avl_find(avl
, &search
, &where
)) != NULL
) {
717 aux
= kmem_zalloc(sizeof (spa_aux_t
), KM_PUSHPAGE
);
718 aux
->aux_guid
= vd
->vdev_guid
;
720 avl_insert(avl
, aux
, where
);
725 spa_aux_remove(vdev_t
*vd
, avl_tree_t
*avl
)
731 search
.aux_guid
= vd
->vdev_guid
;
732 aux
= avl_find(avl
, &search
, &where
);
736 if (--aux
->aux_count
== 0) {
737 avl_remove(avl
, aux
);
738 kmem_free(aux
, sizeof (spa_aux_t
));
739 } else if (aux
->aux_pool
== spa_guid(vd
->vdev_spa
)) {
740 aux
->aux_pool
= 0ULL;
745 spa_aux_exists(uint64_t guid
, uint64_t *pool
, int *refcnt
, avl_tree_t
*avl
)
747 spa_aux_t search
, *found
;
749 search
.aux_guid
= guid
;
750 found
= avl_find(avl
, &search
, NULL
);
754 *pool
= found
->aux_pool
;
761 *refcnt
= found
->aux_count
;
766 return (found
!= NULL
);
770 spa_aux_activate(vdev_t
*vd
, avl_tree_t
*avl
)
772 spa_aux_t search
, *found
;
775 search
.aux_guid
= vd
->vdev_guid
;
776 found
= avl_find(avl
, &search
, &where
);
777 ASSERT(found
!= NULL
);
778 ASSERT(found
->aux_pool
== 0ULL);
780 found
->aux_pool
= spa_guid(vd
->vdev_spa
);
784 * Spares are tracked globally due to the following constraints:
786 * - A spare may be part of multiple pools.
787 * - A spare may be added to a pool even if it's actively in use within
789 * - A spare in use in any pool can only be the source of a replacement if
790 * the target is a spare in the same pool.
792 * We keep track of all spares on the system through the use of a reference
793 * counted AVL tree. When a vdev is added as a spare, or used as a replacement
794 * spare, then we bump the reference count in the AVL tree. In addition, we set
795 * the 'vdev_isspare' member to indicate that the device is a spare (active or
796 * inactive). When a spare is made active (used to replace a device in the
797 * pool), we also keep track of which pool its been made a part of.
799 * The 'spa_spare_lock' protects the AVL tree. These functions are normally
800 * called under the spa_namespace lock as part of vdev reconfiguration. The
801 * separate spare lock exists for the status query path, which does not need to
802 * be completely consistent with respect to other vdev configuration changes.
806 spa_spare_compare(const void *a
, const void *b
)
808 return (spa_aux_compare(a
, b
));
812 spa_spare_add(vdev_t
*vd
)
814 mutex_enter(&spa_spare_lock
);
815 ASSERT(!vd
->vdev_isspare
);
816 spa_aux_add(vd
, &spa_spare_avl
);
817 vd
->vdev_isspare
= B_TRUE
;
818 mutex_exit(&spa_spare_lock
);
822 spa_spare_remove(vdev_t
*vd
)
824 mutex_enter(&spa_spare_lock
);
825 ASSERT(vd
->vdev_isspare
);
826 spa_aux_remove(vd
, &spa_spare_avl
);
827 vd
->vdev_isspare
= B_FALSE
;
828 mutex_exit(&spa_spare_lock
);
832 spa_spare_exists(uint64_t guid
, uint64_t *pool
, int *refcnt
)
836 mutex_enter(&spa_spare_lock
);
837 found
= spa_aux_exists(guid
, pool
, refcnt
, &spa_spare_avl
);
838 mutex_exit(&spa_spare_lock
);
844 spa_spare_activate(vdev_t
*vd
)
846 mutex_enter(&spa_spare_lock
);
847 ASSERT(vd
->vdev_isspare
);
848 spa_aux_activate(vd
, &spa_spare_avl
);
849 mutex_exit(&spa_spare_lock
);
853 * Level 2 ARC devices are tracked globally for the same reasons as spares.
854 * Cache devices currently only support one pool per cache device, and so
855 * for these devices the aux reference count is currently unused beyond 1.
859 spa_l2cache_compare(const void *a
, const void *b
)
861 return (spa_aux_compare(a
, b
));
865 spa_l2cache_add(vdev_t
*vd
)
867 mutex_enter(&spa_l2cache_lock
);
868 ASSERT(!vd
->vdev_isl2cache
);
869 spa_aux_add(vd
, &spa_l2cache_avl
);
870 vd
->vdev_isl2cache
= B_TRUE
;
871 mutex_exit(&spa_l2cache_lock
);
875 spa_l2cache_remove(vdev_t
*vd
)
877 mutex_enter(&spa_l2cache_lock
);
878 ASSERT(vd
->vdev_isl2cache
);
879 spa_aux_remove(vd
, &spa_l2cache_avl
);
880 vd
->vdev_isl2cache
= B_FALSE
;
881 mutex_exit(&spa_l2cache_lock
);
885 spa_l2cache_exists(uint64_t guid
, uint64_t *pool
)
889 mutex_enter(&spa_l2cache_lock
);
890 found
= spa_aux_exists(guid
, pool
, NULL
, &spa_l2cache_avl
);
891 mutex_exit(&spa_l2cache_lock
);
897 spa_l2cache_activate(vdev_t
*vd
)
899 mutex_enter(&spa_l2cache_lock
);
900 ASSERT(vd
->vdev_isl2cache
);
901 spa_aux_activate(vd
, &spa_l2cache_avl
);
902 mutex_exit(&spa_l2cache_lock
);
906 * ==========================================================================
908 * ==========================================================================
912 * Lock the given spa_t for the purpose of adding or removing a vdev.
913 * Grabs the global spa_namespace_lock plus the spa config lock for writing.
914 * It returns the next transaction group for the spa_t.
917 spa_vdev_enter(spa_t
*spa
)
919 mutex_enter(&spa
->spa_vdev_top_lock
);
920 mutex_enter(&spa_namespace_lock
);
921 return (spa_vdev_config_enter(spa
));
925 * Internal implementation for spa_vdev_enter(). Used when a vdev
926 * operation requires multiple syncs (i.e. removing a device) while
927 * keeping the spa_namespace_lock held.
930 spa_vdev_config_enter(spa_t
*spa
)
932 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
934 spa_config_enter(spa
, SCL_ALL
, spa
, RW_WRITER
);
936 return (spa_last_synced_txg(spa
) + 1);
940 * Used in combination with spa_vdev_config_enter() to allow the syncing
941 * of multiple transactions without releasing the spa_namespace_lock.
944 spa_vdev_config_exit(spa_t
*spa
, vdev_t
*vd
, uint64_t txg
, int error
, char *tag
)
946 int config_changed
= B_FALSE
;
948 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
949 ASSERT(txg
> spa_last_synced_txg(spa
));
951 spa
->spa_pending_vdev
= NULL
;
956 vdev_dtl_reassess(spa
->spa_root_vdev
, 0, 0, B_FALSE
);
958 if (error
== 0 && !list_is_empty(&spa
->spa_config_dirty_list
)) {
959 config_changed
= B_TRUE
;
960 spa
->spa_config_generation
++;
964 * Verify the metaslab classes.
966 ASSERT(metaslab_class_validate(spa_normal_class(spa
)) == 0);
967 ASSERT(metaslab_class_validate(spa_log_class(spa
)) == 0);
969 spa_config_exit(spa
, SCL_ALL
, spa
);
972 * Panic the system if the specified tag requires it. This
973 * is useful for ensuring that configurations are updated
976 if (zio_injection_enabled
)
977 zio_handle_panic_injection(spa
, tag
, 0);
980 * Note: this txg_wait_synced() is important because it ensures
981 * that there won't be more than one config change per txg.
982 * This allows us to use the txg as the generation number.
985 txg_wait_synced(spa
->spa_dsl_pool
, txg
);
988 ASSERT(!vd
->vdev_detached
|| vd
->vdev_dtl_smo
.smo_object
== 0);
989 spa_config_enter(spa
, SCL_ALL
, spa
, RW_WRITER
);
991 spa_config_exit(spa
, SCL_ALL
, spa
);
995 * If the config changed, update the config cache.
998 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
1002 * Unlock the spa_t after adding or removing a vdev. Besides undoing the
1003 * locking of spa_vdev_enter(), we also want make sure the transactions have
1004 * synced to disk, and then update the global configuration cache with the new
1008 spa_vdev_exit(spa_t
*spa
, vdev_t
*vd
, uint64_t txg
, int error
)
1010 spa_vdev_config_exit(spa
, vd
, txg
, error
, FTAG
);
1011 mutex_exit(&spa_namespace_lock
);
1012 mutex_exit(&spa
->spa_vdev_top_lock
);
1018 * Lock the given spa_t for the purpose of changing vdev state.
1021 spa_vdev_state_enter(spa_t
*spa
, int oplocks
)
1023 int locks
= SCL_STATE_ALL
| oplocks
;
1026 * Root pools may need to read of the underlying devfs filesystem
1027 * when opening up a vdev. Unfortunately if we're holding the
1028 * SCL_ZIO lock it will result in a deadlock when we try to issue
1029 * the read from the root filesystem. Instead we "prefetch"
1030 * the associated vnodes that we need prior to opening the
1031 * underlying devices and cache them so that we can prevent
1032 * any I/O when we are doing the actual open.
1034 if (spa_is_root(spa
)) {
1035 int low
= locks
& ~(SCL_ZIO
- 1);
1036 int high
= locks
& ~low
;
1038 spa_config_enter(spa
, high
, spa
, RW_WRITER
);
1039 vdev_hold(spa
->spa_root_vdev
);
1040 spa_config_enter(spa
, low
, spa
, RW_WRITER
);
1042 spa_config_enter(spa
, locks
, spa
, RW_WRITER
);
1044 spa
->spa_vdev_locks
= locks
;
1048 spa_vdev_state_exit(spa_t
*spa
, vdev_t
*vd
, int error
)
1050 boolean_t config_changed
= B_FALSE
;
1052 if (vd
!= NULL
|| error
== 0)
1053 vdev_dtl_reassess(vd
? vd
->vdev_top
: spa
->spa_root_vdev
,
1057 vdev_state_dirty(vd
->vdev_top
);
1058 config_changed
= B_TRUE
;
1059 spa
->spa_config_generation
++;
1062 if (spa_is_root(spa
))
1063 vdev_rele(spa
->spa_root_vdev
);
1065 ASSERT3U(spa
->spa_vdev_locks
, >=, SCL_STATE_ALL
);
1066 spa_config_exit(spa
, spa
->spa_vdev_locks
, spa
);
1069 * If anything changed, wait for it to sync. This ensures that,
1070 * from the system administrator's perspective, zpool(1M) commands
1071 * are synchronous. This is important for things like zpool offline:
1072 * when the command completes, you expect no further I/O from ZFS.
1075 txg_wait_synced(spa
->spa_dsl_pool
, 0);
1078 * If the config changed, update the config cache.
1080 if (config_changed
) {
1081 mutex_enter(&spa_namespace_lock
);
1082 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
1083 mutex_exit(&spa_namespace_lock
);
1090 * ==========================================================================
1091 * Miscellaneous functions
1092 * ==========================================================================
1096 spa_activate_mos_feature(spa_t
*spa
, const char *feature
)
1098 (void) nvlist_add_boolean(spa
->spa_label_features
, feature
);
1099 vdev_config_dirty(spa
->spa_root_vdev
);
1103 spa_deactivate_mos_feature(spa_t
*spa
, const char *feature
)
1105 (void) nvlist_remove_all(spa
->spa_label_features
, feature
);
1106 vdev_config_dirty(spa
->spa_root_vdev
);
1113 spa_rename(const char *name
, const char *newname
)
1119 * Lookup the spa_t and grab the config lock for writing. We need to
1120 * actually open the pool so that we can sync out the necessary labels.
1121 * It's OK to call spa_open() with the namespace lock held because we
1122 * allow recursive calls for other reasons.
1124 mutex_enter(&spa_namespace_lock
);
1125 if ((err
= spa_open(name
, &spa
, FTAG
)) != 0) {
1126 mutex_exit(&spa_namespace_lock
);
1130 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1132 avl_remove(&spa_namespace_avl
, spa
);
1133 (void) strlcpy(spa
->spa_name
, newname
, sizeof (spa
->spa_name
));
1134 avl_add(&spa_namespace_avl
, spa
);
1137 * Sync all labels to disk with the new names by marking the root vdev
1138 * dirty and waiting for it to sync. It will pick up the new pool name
1141 vdev_config_dirty(spa
->spa_root_vdev
);
1143 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1145 txg_wait_synced(spa
->spa_dsl_pool
, 0);
1148 * Sync the updated config cache.
1150 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
1152 spa_close(spa
, FTAG
);
1154 mutex_exit(&spa_namespace_lock
);
1160 * Return the spa_t associated with given pool_guid, if it exists. If
1161 * device_guid is non-zero, determine whether the pool exists *and* contains
1162 * a device with the specified device_guid.
1165 spa_by_guid(uint64_t pool_guid
, uint64_t device_guid
)
1168 avl_tree_t
*t
= &spa_namespace_avl
;
1170 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1172 for (spa
= avl_first(t
); spa
!= NULL
; spa
= AVL_NEXT(t
, spa
)) {
1173 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
)
1175 if (spa
->spa_root_vdev
== NULL
)
1177 if (spa_guid(spa
) == pool_guid
) {
1178 if (device_guid
== 0)
1181 if (vdev_lookup_by_guid(spa
->spa_root_vdev
,
1182 device_guid
) != NULL
)
1186 * Check any devices we may be in the process of adding.
1188 if (spa
->spa_pending_vdev
) {
1189 if (vdev_lookup_by_guid(spa
->spa_pending_vdev
,
1190 device_guid
) != NULL
)
1200 * Determine whether a pool with the given pool_guid exists.
1203 spa_guid_exists(uint64_t pool_guid
, uint64_t device_guid
)
1205 return (spa_by_guid(pool_guid
, device_guid
) != NULL
);
1209 spa_strdup(const char *s
)
1215 new = kmem_alloc(len
+ 1, KM_PUSHPAGE
);
1223 spa_strfree(char *s
)
1225 kmem_free(s
, strlen(s
) + 1);
1229 spa_get_random(uint64_t range
)
1235 (void) random_get_pseudo_bytes((void *)&r
, sizeof (uint64_t));
1241 spa_generate_guid(spa_t
*spa
)
1243 uint64_t guid
= spa_get_random(-1ULL);
1246 while (guid
== 0 || spa_guid_exists(spa_guid(spa
), guid
))
1247 guid
= spa_get_random(-1ULL);
1249 while (guid
== 0 || spa_guid_exists(guid
, 0))
1250 guid
= spa_get_random(-1ULL);
1257 sprintf_blkptr(char *buf
, const blkptr_t
*bp
)
1260 char *checksum
= NULL
;
1261 char *compress
= NULL
;
1264 if (BP_GET_TYPE(bp
) & DMU_OT_NEWTYPE
) {
1265 dmu_object_byteswap_t bswap
=
1266 DMU_OT_BYTESWAP(BP_GET_TYPE(bp
));
1267 (void) snprintf(type
, sizeof (type
), "bswap %s %s",
1268 DMU_OT_IS_METADATA(BP_GET_TYPE(bp
)) ?
1269 "metadata" : "data",
1270 dmu_ot_byteswap
[bswap
].ob_name
);
1272 (void) strlcpy(type
, dmu_ot
[BP_GET_TYPE(bp
)].ot_name
,
1275 checksum
= zio_checksum_table
[BP_GET_CHECKSUM(bp
)].ci_name
;
1276 compress
= zio_compress_table
[BP_GET_COMPRESS(bp
)].ci_name
;
1279 SPRINTF_BLKPTR(snprintf
, ' ', buf
, bp
, type
, checksum
, compress
);
1283 spa_freeze(spa_t
*spa
)
1285 uint64_t freeze_txg
= 0;
1287 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1288 if (spa
->spa_freeze_txg
== UINT64_MAX
) {
1289 freeze_txg
= spa_last_synced_txg(spa
) + TXG_SIZE
;
1290 spa
->spa_freeze_txg
= freeze_txg
;
1292 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1293 if (freeze_txg
!= 0)
1294 txg_wait_synced(spa_get_dsl(spa
), freeze_txg
);
1298 * This is a stripped-down version of strtoull, suitable only for converting
1299 * lowercase hexadecimal numbers that don't overflow.
1302 strtonum(const char *str
, char **nptr
)
1308 while ((c
= *str
) != '\0') {
1309 if (c
>= '0' && c
<= '9')
1311 else if (c
>= 'a' && c
<= 'f')
1312 digit
= 10 + c
- 'a';
1323 *nptr
= (char *)str
;
1329 * ==========================================================================
1330 * Accessor functions
1331 * ==========================================================================
1335 spa_shutting_down(spa_t
*spa
)
1337 return (spa
->spa_async_suspended
);
1341 spa_get_dsl(spa_t
*spa
)
1343 return (spa
->spa_dsl_pool
);
1347 spa_is_initializing(spa_t
*spa
)
1349 return (spa
->spa_is_initializing
);
1353 spa_get_rootblkptr(spa_t
*spa
)
1355 return (&spa
->spa_ubsync
.ub_rootbp
);
1359 spa_set_rootblkptr(spa_t
*spa
, const blkptr_t
*bp
)
1361 spa
->spa_uberblock
.ub_rootbp
= *bp
;
1365 spa_altroot(spa_t
*spa
, char *buf
, size_t buflen
)
1367 if (spa
->spa_root
== NULL
)
1370 (void) strncpy(buf
, spa
->spa_root
, buflen
);
1374 spa_sync_pass(spa_t
*spa
)
1376 return (spa
->spa_sync_pass
);
1380 spa_name(spa_t
*spa
)
1382 return (spa
->spa_name
);
1386 spa_guid(spa_t
*spa
)
1388 dsl_pool_t
*dp
= spa_get_dsl(spa
);
1392 * If we fail to parse the config during spa_load(), we can go through
1393 * the error path (which posts an ereport) and end up here with no root
1394 * vdev. We stash the original pool guid in 'spa_config_guid' to handle
1397 if (spa
->spa_root_vdev
== NULL
)
1398 return (spa
->spa_config_guid
);
1400 guid
= spa
->spa_last_synced_guid
!= 0 ?
1401 spa
->spa_last_synced_guid
: spa
->spa_root_vdev
->vdev_guid
;
1404 * Return the most recently synced out guid unless we're
1405 * in syncing context.
1407 if (dp
&& dsl_pool_sync_context(dp
))
1408 return (spa
->spa_root_vdev
->vdev_guid
);
1414 spa_load_guid(spa_t
*spa
)
1417 * This is a GUID that exists solely as a reference for the
1418 * purposes of the arc. It is generated at load time, and
1419 * is never written to persistent storage.
1421 return (spa
->spa_load_guid
);
1425 spa_last_synced_txg(spa_t
*spa
)
1427 return (spa
->spa_ubsync
.ub_txg
);
1431 spa_first_txg(spa_t
*spa
)
1433 return (spa
->spa_first_txg
);
1437 spa_syncing_txg(spa_t
*spa
)
1439 return (spa
->spa_syncing_txg
);
1443 spa_state(spa_t
*spa
)
1445 return (spa
->spa_state
);
1449 spa_load_state(spa_t
*spa
)
1451 return (spa
->spa_load_state
);
1455 spa_freeze_txg(spa_t
*spa
)
1457 return (spa
->spa_freeze_txg
);
1462 spa_get_asize(spa_t
*spa
, uint64_t lsize
)
1464 return (lsize
* spa_asize_inflation
);
1468 spa_get_dspace(spa_t
*spa
)
1470 return (spa
->spa_dspace
);
1474 spa_update_dspace(spa_t
*spa
)
1476 spa
->spa_dspace
= metaslab_class_get_dspace(spa_normal_class(spa
)) +
1477 ddt_get_dedup_dspace(spa
);
1481 * Return the failure mode that has been set to this pool. The default
1482 * behavior will be to block all I/Os when a complete failure occurs.
1485 spa_get_failmode(spa_t
*spa
)
1487 return (spa
->spa_failmode
);
1491 spa_suspended(spa_t
*spa
)
1493 return (spa
->spa_suspended
);
1497 spa_version(spa_t
*spa
)
1499 return (spa
->spa_ubsync
.ub_version
);
1503 spa_deflate(spa_t
*spa
)
1505 return (spa
->spa_deflate
);
1509 spa_normal_class(spa_t
*spa
)
1511 return (spa
->spa_normal_class
);
1515 spa_log_class(spa_t
*spa
)
1517 return (spa
->spa_log_class
);
1521 spa_max_replication(spa_t
*spa
)
1524 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
1525 * handle BPs with more than one DVA allocated. Set our max
1526 * replication level accordingly.
1528 if (spa_version(spa
) < SPA_VERSION_DITTO_BLOCKS
)
1530 return (MIN(SPA_DVAS_PER_BP
, spa_max_replication_override
));
1534 spa_prev_software_version(spa_t
*spa
)
1536 return (spa
->spa_prev_software_version
);
1540 spa_deadman_synctime(spa_t
*spa
)
1542 return (spa
->spa_deadman_synctime
);
1546 dva_get_dsize_sync(spa_t
*spa
, const dva_t
*dva
)
1548 uint64_t asize
= DVA_GET_ASIZE(dva
);
1549 uint64_t dsize
= asize
;
1551 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_READER
) != 0);
1553 if (asize
!= 0 && spa
->spa_deflate
) {
1554 vdev_t
*vd
= vdev_lookup_top(spa
, DVA_GET_VDEV(dva
));
1555 dsize
= (asize
>> SPA_MINBLOCKSHIFT
) * vd
->vdev_deflate_ratio
;
1562 bp_get_dsize_sync(spa_t
*spa
, const blkptr_t
*bp
)
1567 for (d
= 0; d
< SPA_DVAS_PER_BP
; d
++)
1568 dsize
+= dva_get_dsize_sync(spa
, &bp
->blk_dva
[d
]);
1574 bp_get_dsize(spa_t
*spa
, const blkptr_t
*bp
)
1579 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
1581 for (d
= 0; d
< SPA_DVAS_PER_BP
; d
++)
1582 dsize
+= dva_get_dsize_sync(spa
, &bp
->blk_dva
[d
]);
1584 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
1590 * ==========================================================================
1591 * Initialization and Termination
1592 * ==========================================================================
1596 spa_name_compare(const void *a1
, const void *a2
)
1598 const spa_t
*s1
= a1
;
1599 const spa_t
*s2
= a2
;
1602 s
= strcmp(s1
->spa_name
, s2
->spa_name
);
1619 mutex_init(&spa_namespace_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1620 mutex_init(&spa_spare_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1621 mutex_init(&spa_l2cache_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1622 cv_init(&spa_namespace_cv
, NULL
, CV_DEFAULT
, NULL
);
1624 avl_create(&spa_namespace_avl
, spa_name_compare
, sizeof (spa_t
),
1625 offsetof(spa_t
, spa_avl
));
1627 avl_create(&spa_spare_avl
, spa_spare_compare
, sizeof (spa_aux_t
),
1628 offsetof(spa_aux_t
, aux_avl
));
1630 avl_create(&spa_l2cache_avl
, spa_l2cache_compare
, sizeof (spa_aux_t
),
1631 offsetof(spa_aux_t
, aux_avl
));
1633 spa_mode_global
= mode
;
1636 if (spa_mode_global
!= FREAD
&& dprintf_find_string("watch")) {
1637 struct sigaction sa
;
1639 sa
.sa_flags
= SA_SIGINFO
;
1640 sigemptyset(&sa
.sa_mask
);
1641 sa
.sa_sigaction
= arc_buf_sigsegv
;
1643 if (sigaction(SIGSEGV
, &sa
, NULL
) == -1) {
1644 perror("could not enable watchpoints: "
1645 "sigaction(SIGSEGV, ...) = ");
1659 vdev_cache_stat_init();
1662 zpool_feature_init();
1674 vdev_cache_stat_fini();
1683 avl_destroy(&spa_namespace_avl
);
1684 avl_destroy(&spa_spare_avl
);
1685 avl_destroy(&spa_l2cache_avl
);
1687 cv_destroy(&spa_namespace_cv
);
1688 mutex_destroy(&spa_namespace_lock
);
1689 mutex_destroy(&spa_spare_lock
);
1690 mutex_destroy(&spa_l2cache_lock
);
1694 * Return whether this pool has slogs. No locking needed.
1695 * It's not a problem if the wrong answer is returned as it's only for
1696 * performance and not correctness
1699 spa_has_slogs(spa_t
*spa
)
1701 return (spa
->spa_log_class
->mc_rotor
!= NULL
);
1705 spa_get_log_state(spa_t
*spa
)
1707 return (spa
->spa_log_state
);
1711 spa_set_log_state(spa_t
*spa
, spa_log_state_t state
)
1713 spa
->spa_log_state
= state
;
1717 spa_is_root(spa_t
*spa
)
1719 return (spa
->spa_is_root
);
1723 spa_writeable(spa_t
*spa
)
1725 return (!!(spa
->spa_mode
& FWRITE
));
1729 spa_mode(spa_t
*spa
)
1731 return (spa
->spa_mode
);
1735 spa_bootfs(spa_t
*spa
)
1737 return (spa
->spa_bootfs
);
1741 spa_delegation(spa_t
*spa
)
1743 return (spa
->spa_delegation
);
1747 spa_meta_objset(spa_t
*spa
)
1749 return (spa
->spa_meta_objset
);
1753 spa_dedup_checksum(spa_t
*spa
)
1755 return (spa
->spa_dedup_checksum
);
1759 * Reset pool scan stat per scan pass (or reboot).
1762 spa_scan_stat_init(spa_t
*spa
)
1764 /* data not stored on disk */
1765 spa
->spa_scan_pass_start
= gethrestime_sec();
1766 spa
->spa_scan_pass_exam
= 0;
1767 vdev_scan_stat_init(spa
->spa_root_vdev
);
1771 * Get scan stats for zpool status reports
1774 spa_scan_get_stats(spa_t
*spa
, pool_scan_stat_t
*ps
)
1776 dsl_scan_t
*scn
= spa
->spa_dsl_pool
? spa
->spa_dsl_pool
->dp_scan
: NULL
;
1778 if (scn
== NULL
|| scn
->scn_phys
.scn_func
== POOL_SCAN_NONE
)
1779 return (SET_ERROR(ENOENT
));
1780 bzero(ps
, sizeof (pool_scan_stat_t
));
1782 /* data stored on disk */
1783 ps
->pss_func
= scn
->scn_phys
.scn_func
;
1784 ps
->pss_start_time
= scn
->scn_phys
.scn_start_time
;
1785 ps
->pss_end_time
= scn
->scn_phys
.scn_end_time
;
1786 ps
->pss_to_examine
= scn
->scn_phys
.scn_to_examine
;
1787 ps
->pss_examined
= scn
->scn_phys
.scn_examined
;
1788 ps
->pss_to_process
= scn
->scn_phys
.scn_to_process
;
1789 ps
->pss_processed
= scn
->scn_phys
.scn_processed
;
1790 ps
->pss_errors
= scn
->scn_phys
.scn_errors
;
1791 ps
->pss_state
= scn
->scn_phys
.scn_state
;
1793 /* data not stored on disk */
1794 ps
->pss_pass_start
= spa
->spa_scan_pass_start
;
1795 ps
->pss_pass_exam
= spa
->spa_scan_pass_exam
;
1801 spa_debug_enabled(spa_t
*spa
)
1803 return (spa
->spa_debug
);
1806 #if defined(_KERNEL) && defined(HAVE_SPL)
1807 /* Namespace manipulation */
1808 EXPORT_SYMBOL(spa_lookup
);
1809 EXPORT_SYMBOL(spa_add
);
1810 EXPORT_SYMBOL(spa_remove
);
1811 EXPORT_SYMBOL(spa_next
);
1813 /* Refcount functions */
1814 EXPORT_SYMBOL(spa_open_ref
);
1815 EXPORT_SYMBOL(spa_close
);
1816 EXPORT_SYMBOL(spa_refcount_zero
);
1818 /* Pool configuration lock */
1819 EXPORT_SYMBOL(spa_config_tryenter
);
1820 EXPORT_SYMBOL(spa_config_enter
);
1821 EXPORT_SYMBOL(spa_config_exit
);
1822 EXPORT_SYMBOL(spa_config_held
);
1824 /* Pool vdev add/remove lock */
1825 EXPORT_SYMBOL(spa_vdev_enter
);
1826 EXPORT_SYMBOL(spa_vdev_exit
);
1828 /* Pool vdev state change lock */
1829 EXPORT_SYMBOL(spa_vdev_state_enter
);
1830 EXPORT_SYMBOL(spa_vdev_state_exit
);
1832 /* Accessor functions */
1833 EXPORT_SYMBOL(spa_shutting_down
);
1834 EXPORT_SYMBOL(spa_get_dsl
);
1835 EXPORT_SYMBOL(spa_get_rootblkptr
);
1836 EXPORT_SYMBOL(spa_set_rootblkptr
);
1837 EXPORT_SYMBOL(spa_altroot
);
1838 EXPORT_SYMBOL(spa_sync_pass
);
1839 EXPORT_SYMBOL(spa_name
);
1840 EXPORT_SYMBOL(spa_guid
);
1841 EXPORT_SYMBOL(spa_last_synced_txg
);
1842 EXPORT_SYMBOL(spa_first_txg
);
1843 EXPORT_SYMBOL(spa_syncing_txg
);
1844 EXPORT_SYMBOL(spa_version
);
1845 EXPORT_SYMBOL(spa_state
);
1846 EXPORT_SYMBOL(spa_load_state
);
1847 EXPORT_SYMBOL(spa_freeze_txg
);
1848 EXPORT_SYMBOL(spa_get_asize
);
1849 EXPORT_SYMBOL(spa_get_dspace
);
1850 EXPORT_SYMBOL(spa_update_dspace
);
1851 EXPORT_SYMBOL(spa_deflate
);
1852 EXPORT_SYMBOL(spa_normal_class
);
1853 EXPORT_SYMBOL(spa_log_class
);
1854 EXPORT_SYMBOL(spa_max_replication
);
1855 EXPORT_SYMBOL(spa_prev_software_version
);
1856 EXPORT_SYMBOL(spa_get_failmode
);
1857 EXPORT_SYMBOL(spa_suspended
);
1858 EXPORT_SYMBOL(spa_bootfs
);
1859 EXPORT_SYMBOL(spa_delegation
);
1860 EXPORT_SYMBOL(spa_meta_objset
);
1862 /* Miscellaneous support routines */
1863 EXPORT_SYMBOL(spa_rename
);
1864 EXPORT_SYMBOL(spa_guid_exists
);
1865 EXPORT_SYMBOL(spa_strdup
);
1866 EXPORT_SYMBOL(spa_strfree
);
1867 EXPORT_SYMBOL(spa_get_random
);
1868 EXPORT_SYMBOL(spa_generate_guid
);
1869 EXPORT_SYMBOL(sprintf_blkptr
);
1870 EXPORT_SYMBOL(spa_freeze
);
1871 EXPORT_SYMBOL(spa_upgrade
);
1872 EXPORT_SYMBOL(spa_evict_all
);
1873 EXPORT_SYMBOL(spa_lookup_by_guid
);
1874 EXPORT_SYMBOL(spa_has_spare
);
1875 EXPORT_SYMBOL(dva_get_dsize_sync
);
1876 EXPORT_SYMBOL(bp_get_dsize_sync
);
1877 EXPORT_SYMBOL(bp_get_dsize
);
1878 EXPORT_SYMBOL(spa_has_slogs
);
1879 EXPORT_SYMBOL(spa_is_root
);
1880 EXPORT_SYMBOL(spa_writeable
);
1881 EXPORT_SYMBOL(spa_mode
);
1883 EXPORT_SYMBOL(spa_namespace_lock
);
1885 module_param(zfs_deadman_synctime_ms
, ulong
, 0644);
1886 MODULE_PARM_DESC(zfs_deadman_synctime_ms
,"Expiration time in milliseconds");
1888 module_param(zfs_deadman_enabled
, int, 0644);
1889 MODULE_PARM_DESC(zfs_deadman_enabled
, "Enable deadman timer");
1891 module_param(spa_asize_inflation
, int, 0644);
1892 MODULE_PARM_DESC(spa_asize_inflation
,
1893 "SPA size estimate multiplication factor");