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) 2011, 2014 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/vdev_file.h>
38 #include <sys/metaslab.h>
39 #include <sys/uberblock_impl.h>
42 #include <sys/unique.h>
43 #include <sys/dsl_pool.h>
44 #include <sys/dsl_dir.h>
45 #include <sys/dsl_prop.h>
46 #include <sys/fm/util.h>
47 #include <sys/dsl_scan.h>
48 #include <sys/fs/zfs.h>
49 #include <sys/metaslab_impl.h>
52 #include <sys/kstat.h>
54 #include "zfeature_common.h"
59 * There are four basic locks for managing spa_t structures:
61 * spa_namespace_lock (global mutex)
63 * This lock must be acquired to do any of the following:
65 * - Lookup a spa_t by name
66 * - Add or remove a spa_t from the namespace
67 * - Increase spa_refcount from non-zero
68 * - Check if spa_refcount is zero
70 * - add/remove/attach/detach devices
71 * - Held for the duration of create/destroy/import/export
73 * It does not need to handle recursion. A create or destroy may
74 * reference objects (files or zvols) in other pools, but by
75 * definition they must have an existing reference, and will never need
76 * to lookup a spa_t by name.
78 * spa_refcount (per-spa refcount_t protected by mutex)
80 * This reference count keep track of any active users of the spa_t. The
81 * spa_t cannot be destroyed or freed while this is non-zero. Internally,
82 * the refcount is never really 'zero' - opening a pool implicitly keeps
83 * some references in the DMU. Internally we check against spa_minref, but
84 * present the image of a zero/non-zero value to consumers.
86 * spa_config_lock[] (per-spa array of rwlocks)
88 * This protects the spa_t from config changes, and must be held in
89 * the following circumstances:
91 * - RW_READER to perform I/O to the spa
92 * - RW_WRITER to change the vdev config
94 * The locking order is fairly straightforward:
96 * spa_namespace_lock -> spa_refcount
98 * The namespace lock must be acquired to increase the refcount from 0
99 * or to check if it is zero.
101 * spa_refcount -> spa_config_lock[]
103 * There must be at least one valid reference on the spa_t to acquire
106 * spa_namespace_lock -> spa_config_lock[]
108 * The namespace lock must always be taken before the config lock.
111 * The spa_namespace_lock can be acquired directly and is globally visible.
113 * The namespace is manipulated using the following functions, all of which
114 * require the spa_namespace_lock to be held.
116 * spa_lookup() Lookup a spa_t by name.
118 * spa_add() Create a new spa_t in the namespace.
120 * spa_remove() Remove a spa_t from the namespace. This also
121 * frees up any memory associated with the spa_t.
123 * spa_next() Returns the next spa_t in the system, or the
124 * first if NULL is passed.
126 * spa_evict_all() Shutdown and remove all spa_t structures in
129 * spa_guid_exists() Determine whether a pool/device guid exists.
131 * The spa_refcount is manipulated using the following functions:
133 * spa_open_ref() Adds a reference to the given spa_t. Must be
134 * called with spa_namespace_lock held if the
135 * refcount is currently zero.
137 * spa_close() Remove a reference from the spa_t. This will
138 * not free the spa_t or remove it from the
139 * namespace. No locking is required.
141 * spa_refcount_zero() Returns true if the refcount is currently
142 * zero. Must be called with spa_namespace_lock
145 * The spa_config_lock[] is an array of rwlocks, ordered as follows:
146 * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV.
147 * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}().
149 * To read the configuration, it suffices to hold one of these locks as reader.
150 * To modify the configuration, you must hold all locks as writer. To modify
151 * vdev state without altering the vdev tree's topology (e.g. online/offline),
152 * you must hold SCL_STATE and SCL_ZIO as writer.
154 * We use these distinct config locks to avoid recursive lock entry.
155 * For example, spa_sync() (which holds SCL_CONFIG as reader) induces
156 * block allocations (SCL_ALLOC), which may require reading space maps
157 * from disk (dmu_read() -> zio_read() -> SCL_ZIO).
159 * The spa config locks cannot be normal rwlocks because we need the
160 * ability to hand off ownership. For example, SCL_ZIO is acquired
161 * by the issuing thread and later released by an interrupt thread.
162 * They do, however, obey the usual write-wanted semantics to prevent
163 * writer (i.e. system administrator) starvation.
165 * The lock acquisition rules are as follows:
168 * Protects changes to the vdev tree topology, such as vdev
169 * add/remove/attach/detach. Protects the dirty config list
170 * (spa_config_dirty_list) and the set of spares and l2arc devices.
173 * Protects changes to pool state and vdev state, such as vdev
174 * online/offline/fault/degrade/clear. Protects the dirty state list
175 * (spa_state_dirty_list) and global pool state (spa_state).
178 * Protects changes to metaslab groups and classes.
179 * Held as reader by metaslab_alloc() and metaslab_claim().
182 * Held by bp-level zios (those which have no io_vd upon entry)
183 * to prevent changes to the vdev tree. The bp-level zio implicitly
184 * protects all of its vdev child zios, which do not hold SCL_ZIO.
187 * Protects changes to metaslab groups and classes.
188 * Held as reader by metaslab_free(). SCL_FREE is distinct from
189 * SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free
190 * blocks in zio_done() while another i/o that holds either
191 * SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete.
194 * Held as reader to prevent changes to the vdev tree during trivial
195 * inquiries such as bp_get_dsize(). SCL_VDEV is distinct from the
196 * other locks, and lower than all of them, to ensure that it's safe
197 * to acquire regardless of caller context.
199 * In addition, the following rules apply:
201 * (a) spa_props_lock protects pool properties, spa_config and spa_config_list.
202 * The lock ordering is SCL_CONFIG > spa_props_lock.
204 * (b) I/O operations on leaf vdevs. For any zio operation that takes
205 * an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(),
206 * or zio_write_phys() -- the caller must ensure that the config cannot
207 * cannot change in the interim, and that the vdev cannot be reopened.
208 * SCL_STATE as reader suffices for both.
210 * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
212 * spa_vdev_enter() Acquire the namespace lock and the config lock
215 * spa_vdev_exit() Release the config lock, wait for all I/O
216 * to complete, sync the updated configs to the
217 * cache, and release the namespace lock.
219 * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit().
220 * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual
221 * locking is, always, based on spa_namespace_lock and spa_config_lock[].
223 * spa_rename() is also implemented within this file since it requires
224 * manipulation of the namespace.
227 static avl_tree_t spa_namespace_avl
;
228 kmutex_t spa_namespace_lock
;
229 static kcondvar_t spa_namespace_cv
;
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 /* Everything except dprintf and spa is on by default in debug builds */
242 int zfs_flags
= ~(ZFS_DEBUG_DPRINTF
| ZFS_DEBUG_SPA
);
248 * zfs_recover can be set to nonzero to attempt to recover from
249 * otherwise-fatal errors, typically caused by on-disk corruption. When
250 * set, calls to zfs_panic_recover() will turn into warning messages.
251 * This should only be used as a last resort, as it typically results
252 * in leaked space, or worse.
254 int zfs_recover
= B_FALSE
;
257 * If destroy encounters an EIO while reading metadata (e.g. indirect
258 * blocks), space referenced by the missing metadata can not be freed.
259 * Normally this causes the background destroy to become "stalled", as
260 * it is unable to make forward progress. While in this stalled state,
261 * all remaining space to free from the error-encountering filesystem is
262 * "temporarily leaked". Set this flag to cause it to ignore the EIO,
263 * permanently leak the space from indirect blocks that can not be read,
264 * and continue to free everything else that it can.
266 * The default, "stalling" behavior is useful if the storage partially
267 * fails (i.e. some but not all i/os fail), and then later recovers. In
268 * this case, we will be able to continue pool operations while it is
269 * partially failed, and when it recovers, we can continue to free the
270 * space, with no leaks. However, note that this case is actually
273 * Typically pools either (a) fail completely (but perhaps temporarily,
274 * e.g. a top-level vdev going offline), or (b) have localized,
275 * permanent errors (e.g. disk returns the wrong data due to bit flip or
276 * firmware bug). In case (a), this setting does not matter because the
277 * pool will be suspended and the sync thread will not be able to make
278 * forward progress regardless. In case (b), because the error is
279 * permanent, the best we can do is leak the minimum amount of space,
280 * which is what setting this flag will do. Therefore, it is reasonable
281 * for this flag to normally be set, but we chose the more conservative
282 * approach of not setting it, so that there is no possibility of
283 * leaking space in the "partial temporary" failure case.
285 int zfs_free_leak_on_eio
= B_FALSE
;
288 * Expiration time in milliseconds. This value has two meanings. First it is
289 * used to determine when the spa_deadman() logic should fire. By default the
290 * spa_deadman() will fire if spa_sync() has not completed in 1000 seconds.
291 * Secondly, the value determines if an I/O is considered "hung". Any I/O that
292 * has not completed in zfs_deadman_synctime_ms is considered "hung" resulting
295 unsigned long zfs_deadman_synctime_ms
= 1000000ULL;
298 * By default the deadman is enabled.
300 int zfs_deadman_enabled
= 1;
303 * The worst case is single-sector max-parity RAID-Z blocks, in which
304 * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1)
305 * times the size; so just assume that. Add to this the fact that
306 * we can have up to 3 DVAs per bp, and one more factor of 2 because
307 * the block may be dittoed with up to 3 DVAs by ddt_sync(). All together,
309 * (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2 == 24
311 int spa_asize_inflation
= 24;
314 * ==========================================================================
316 * ==========================================================================
319 spa_config_lock_init(spa_t
*spa
)
323 for (i
= 0; i
< SCL_LOCKS
; i
++) {
324 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
325 mutex_init(&scl
->scl_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
326 cv_init(&scl
->scl_cv
, NULL
, CV_DEFAULT
, NULL
);
327 refcount_create_untracked(&scl
->scl_count
);
328 scl
->scl_writer
= NULL
;
329 scl
->scl_write_wanted
= 0;
334 spa_config_lock_destroy(spa_t
*spa
)
338 for (i
= 0; i
< SCL_LOCKS
; i
++) {
339 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
340 mutex_destroy(&scl
->scl_lock
);
341 cv_destroy(&scl
->scl_cv
);
342 refcount_destroy(&scl
->scl_count
);
343 ASSERT(scl
->scl_writer
== NULL
);
344 ASSERT(scl
->scl_write_wanted
== 0);
349 spa_config_tryenter(spa_t
*spa
, int locks
, void *tag
, krw_t rw
)
353 for (i
= 0; i
< SCL_LOCKS
; i
++) {
354 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
355 if (!(locks
& (1 << i
)))
357 mutex_enter(&scl
->scl_lock
);
358 if (rw
== RW_READER
) {
359 if (scl
->scl_writer
|| scl
->scl_write_wanted
) {
360 mutex_exit(&scl
->scl_lock
);
361 spa_config_exit(spa
, locks
^ (1 << i
), tag
);
365 ASSERT(scl
->scl_writer
!= curthread
);
366 if (!refcount_is_zero(&scl
->scl_count
)) {
367 mutex_exit(&scl
->scl_lock
);
368 spa_config_exit(spa
, locks
^ (1 << i
), tag
);
371 scl
->scl_writer
= curthread
;
373 (void) refcount_add(&scl
->scl_count
, tag
);
374 mutex_exit(&scl
->scl_lock
);
380 spa_config_enter(spa_t
*spa
, int locks
, void *tag
, krw_t rw
)
385 ASSERT3U(SCL_LOCKS
, <, sizeof (wlocks_held
) * NBBY
);
387 for (i
= 0; i
< SCL_LOCKS
; i
++) {
388 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
389 if (scl
->scl_writer
== curthread
)
390 wlocks_held
|= (1 << i
);
391 if (!(locks
& (1 << i
)))
393 mutex_enter(&scl
->scl_lock
);
394 if (rw
== RW_READER
) {
395 while (scl
->scl_writer
|| scl
->scl_write_wanted
) {
396 cv_wait(&scl
->scl_cv
, &scl
->scl_lock
);
399 ASSERT(scl
->scl_writer
!= curthread
);
400 while (!refcount_is_zero(&scl
->scl_count
)) {
401 scl
->scl_write_wanted
++;
402 cv_wait(&scl
->scl_cv
, &scl
->scl_lock
);
403 scl
->scl_write_wanted
--;
405 scl
->scl_writer
= curthread
;
407 (void) refcount_add(&scl
->scl_count
, tag
);
408 mutex_exit(&scl
->scl_lock
);
410 ASSERT(wlocks_held
<= locks
);
414 spa_config_exit(spa_t
*spa
, int locks
, void *tag
)
418 for (i
= SCL_LOCKS
- 1; i
>= 0; i
--) {
419 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
420 if (!(locks
& (1 << i
)))
422 mutex_enter(&scl
->scl_lock
);
423 ASSERT(!refcount_is_zero(&scl
->scl_count
));
424 if (refcount_remove(&scl
->scl_count
, tag
) == 0) {
425 ASSERT(scl
->scl_writer
== NULL
||
426 scl
->scl_writer
== curthread
);
427 scl
->scl_writer
= NULL
; /* OK in either case */
428 cv_broadcast(&scl
->scl_cv
);
430 mutex_exit(&scl
->scl_lock
);
435 spa_config_held(spa_t
*spa
, int locks
, krw_t rw
)
437 int i
, locks_held
= 0;
439 for (i
= 0; i
< SCL_LOCKS
; i
++) {
440 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
441 if (!(locks
& (1 << i
)))
443 if ((rw
== RW_READER
&& !refcount_is_zero(&scl
->scl_count
)) ||
444 (rw
== RW_WRITER
&& scl
->scl_writer
== curthread
))
445 locks_held
|= 1 << i
;
452 * ==========================================================================
453 * SPA namespace functions
454 * ==========================================================================
458 * Lookup the named spa_t in the AVL tree. The spa_namespace_lock must be held.
459 * Returns NULL if no matching spa_t is found.
462 spa_lookup(const char *name
)
464 static spa_t search
; /* spa_t is large; don't allocate on stack */
469 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
471 (void) strlcpy(search
.spa_name
, name
, sizeof (search
.spa_name
));
474 * If it's a full dataset name, figure out the pool name and
477 cp
= strpbrk(search
.spa_name
, "/@#");
481 spa
= avl_find(&spa_namespace_avl
, &search
, &where
);
487 * Fires when spa_sync has not completed within zfs_deadman_synctime_ms.
488 * If the zfs_deadman_enabled flag is set then it inspects all vdev queues
489 * looking for potentially hung I/Os.
492 spa_deadman(void *arg
)
496 zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu",
497 (gethrtime() - spa
->spa_sync_starttime
) / NANOSEC
,
498 ++spa
->spa_deadman_calls
);
499 if (zfs_deadman_enabled
)
500 vdev_deadman(spa
->spa_root_vdev
);
502 spa
->spa_deadman_tqid
= taskq_dispatch_delay(system_taskq
,
503 spa_deadman
, spa
, KM_SLEEP
, ddi_get_lbolt() +
504 NSEC_TO_TICK(spa
->spa_deadman_synctime
));
508 * Create an uninitialized spa_t with the given name. Requires
509 * spa_namespace_lock. The caller must ensure that the spa_t doesn't already
510 * exist by calling spa_lookup() first.
513 spa_add(const char *name
, nvlist_t
*config
, const char *altroot
)
516 spa_config_dirent_t
*dp
;
520 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
522 spa
= kmem_zalloc(sizeof (spa_t
), KM_SLEEP
);
524 mutex_init(&spa
->spa_async_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
525 mutex_init(&spa
->spa_errlist_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
526 mutex_init(&spa
->spa_errlog_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
527 mutex_init(&spa
->spa_history_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
528 mutex_init(&spa
->spa_proc_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
529 mutex_init(&spa
->spa_props_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
530 mutex_init(&spa
->spa_scrub_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
531 mutex_init(&spa
->spa_suspend_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
532 mutex_init(&spa
->spa_vdev_top_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
533 mutex_init(&spa
->spa_feat_stats_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
535 cv_init(&spa
->spa_async_cv
, NULL
, CV_DEFAULT
, NULL
);
536 cv_init(&spa
->spa_proc_cv
, NULL
, CV_DEFAULT
, NULL
);
537 cv_init(&spa
->spa_scrub_io_cv
, NULL
, CV_DEFAULT
, NULL
);
538 cv_init(&spa
->spa_suspend_cv
, NULL
, CV_DEFAULT
, NULL
);
540 for (t
= 0; t
< TXG_SIZE
; t
++)
541 bplist_create(&spa
->spa_free_bplist
[t
]);
543 (void) strlcpy(spa
->spa_name
, name
, sizeof (spa
->spa_name
));
544 spa
->spa_state
= POOL_STATE_UNINITIALIZED
;
545 spa
->spa_freeze_txg
= UINT64_MAX
;
546 spa
->spa_final_txg
= UINT64_MAX
;
547 spa
->spa_load_max_txg
= UINT64_MAX
;
549 spa
->spa_proc_state
= SPA_PROC_NONE
;
551 spa
->spa_deadman_synctime
= MSEC2NSEC(zfs_deadman_synctime_ms
);
553 refcount_create(&spa
->spa_refcount
);
554 spa_config_lock_init(spa
);
557 avl_add(&spa_namespace_avl
, spa
);
560 * Set the alternate root, if there is one.
563 spa
->spa_root
= spa_strdup(altroot
);
566 * Every pool starts with the default cachefile
568 list_create(&spa
->spa_config_list
, sizeof (spa_config_dirent_t
),
569 offsetof(spa_config_dirent_t
, scd_link
));
571 dp
= kmem_zalloc(sizeof (spa_config_dirent_t
), KM_SLEEP
);
572 dp
->scd_path
= altroot
? NULL
: spa_strdup(spa_config_path
);
573 list_insert_head(&spa
->spa_config_list
, dp
);
575 VERIFY(nvlist_alloc(&spa
->spa_load_info
, NV_UNIQUE_NAME
,
578 if (config
!= NULL
) {
581 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_FEATURES_FOR_READ
,
583 VERIFY(nvlist_dup(features
, &spa
->spa_label_features
,
587 VERIFY(nvlist_dup(config
, &spa
->spa_config
, 0) == 0);
590 if (spa
->spa_label_features
== NULL
) {
591 VERIFY(nvlist_alloc(&spa
->spa_label_features
, NV_UNIQUE_NAME
,
595 spa
->spa_debug
= ((zfs_flags
& ZFS_DEBUG_SPA
) != 0);
598 * As a pool is being created, treat all features as disabled by
599 * setting SPA_FEATURE_DISABLED for all entries in the feature
602 for (i
= 0; i
< SPA_FEATURES
; i
++) {
603 spa
->spa_feat_refcount_cache
[i
] = SPA_FEATURE_DISABLED
;
610 * Removes a spa_t from the namespace, freeing up any memory used. Requires
611 * spa_namespace_lock. This is called only after the spa_t has been closed and
615 spa_remove(spa_t
*spa
)
617 spa_config_dirent_t
*dp
;
620 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
621 ASSERT(spa
->spa_state
== POOL_STATE_UNINITIALIZED
);
623 nvlist_free(spa
->spa_config_splitting
);
625 avl_remove(&spa_namespace_avl
, spa
);
626 cv_broadcast(&spa_namespace_cv
);
629 spa_strfree(spa
->spa_root
);
631 while ((dp
= list_head(&spa
->spa_config_list
)) != NULL
) {
632 list_remove(&spa
->spa_config_list
, dp
);
633 if (dp
->scd_path
!= NULL
)
634 spa_strfree(dp
->scd_path
);
635 kmem_free(dp
, sizeof (spa_config_dirent_t
));
638 list_destroy(&spa
->spa_config_list
);
640 nvlist_free(spa
->spa_label_features
);
641 nvlist_free(spa
->spa_load_info
);
642 nvlist_free(spa
->spa_feat_stats
);
643 spa_config_set(spa
, NULL
);
645 refcount_destroy(&spa
->spa_refcount
);
647 spa_stats_destroy(spa
);
648 spa_config_lock_destroy(spa
);
650 for (t
= 0; t
< TXG_SIZE
; t
++)
651 bplist_destroy(&spa
->spa_free_bplist
[t
]);
653 cv_destroy(&spa
->spa_async_cv
);
654 cv_destroy(&spa
->spa_proc_cv
);
655 cv_destroy(&spa
->spa_scrub_io_cv
);
656 cv_destroy(&spa
->spa_suspend_cv
);
658 mutex_destroy(&spa
->spa_async_lock
);
659 mutex_destroy(&spa
->spa_errlist_lock
);
660 mutex_destroy(&spa
->spa_errlog_lock
);
661 mutex_destroy(&spa
->spa_history_lock
);
662 mutex_destroy(&spa
->spa_proc_lock
);
663 mutex_destroy(&spa
->spa_props_lock
);
664 mutex_destroy(&spa
->spa_scrub_lock
);
665 mutex_destroy(&spa
->spa_suspend_lock
);
666 mutex_destroy(&spa
->spa_vdev_top_lock
);
667 mutex_destroy(&spa
->spa_feat_stats_lock
);
669 kmem_free(spa
, sizeof (spa_t
));
673 * Given a pool, return the next pool in the namespace, or NULL if there is
674 * none. If 'prev' is NULL, return the first pool.
677 spa_next(spa_t
*prev
)
679 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
682 return (AVL_NEXT(&spa_namespace_avl
, prev
));
684 return (avl_first(&spa_namespace_avl
));
688 * ==========================================================================
689 * SPA refcount functions
690 * ==========================================================================
694 * Add a reference to the given spa_t. Must have at least one reference, or
695 * have the namespace lock held.
698 spa_open_ref(spa_t
*spa
, void *tag
)
700 ASSERT(refcount_count(&spa
->spa_refcount
) >= spa
->spa_minref
||
701 MUTEX_HELD(&spa_namespace_lock
));
702 (void) refcount_add(&spa
->spa_refcount
, tag
);
706 * Remove a reference to the given spa_t. Must have at least one reference, or
707 * have the namespace lock held.
710 spa_close(spa_t
*spa
, void *tag
)
712 ASSERT(refcount_count(&spa
->spa_refcount
) > spa
->spa_minref
||
713 MUTEX_HELD(&spa_namespace_lock
));
714 (void) refcount_remove(&spa
->spa_refcount
, tag
);
718 * Check to see if the spa refcount is zero. Must be called with
719 * spa_namespace_lock held. We really compare against spa_minref, which is the
720 * number of references acquired when opening a pool
723 spa_refcount_zero(spa_t
*spa
)
725 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
727 return (refcount_count(&spa
->spa_refcount
) == spa
->spa_minref
);
731 * ==========================================================================
732 * SPA spare and l2cache tracking
733 * ==========================================================================
737 * Hot spares and cache devices are tracked using the same code below,
738 * for 'auxiliary' devices.
741 typedef struct spa_aux
{
749 spa_aux_compare(const void *a
, const void *b
)
751 const spa_aux_t
*sa
= a
;
752 const spa_aux_t
*sb
= b
;
754 if (sa
->aux_guid
< sb
->aux_guid
)
756 else if (sa
->aux_guid
> sb
->aux_guid
)
763 spa_aux_add(vdev_t
*vd
, avl_tree_t
*avl
)
769 search
.aux_guid
= vd
->vdev_guid
;
770 if ((aux
= avl_find(avl
, &search
, &where
)) != NULL
) {
773 aux
= kmem_zalloc(sizeof (spa_aux_t
), KM_SLEEP
);
774 aux
->aux_guid
= vd
->vdev_guid
;
776 avl_insert(avl
, aux
, where
);
781 spa_aux_remove(vdev_t
*vd
, avl_tree_t
*avl
)
787 search
.aux_guid
= vd
->vdev_guid
;
788 aux
= avl_find(avl
, &search
, &where
);
792 if (--aux
->aux_count
== 0) {
793 avl_remove(avl
, aux
);
794 kmem_free(aux
, sizeof (spa_aux_t
));
795 } else if (aux
->aux_pool
== spa_guid(vd
->vdev_spa
)) {
796 aux
->aux_pool
= 0ULL;
801 spa_aux_exists(uint64_t guid
, uint64_t *pool
, int *refcnt
, avl_tree_t
*avl
)
803 spa_aux_t search
, *found
;
805 search
.aux_guid
= guid
;
806 found
= avl_find(avl
, &search
, NULL
);
810 *pool
= found
->aux_pool
;
817 *refcnt
= found
->aux_count
;
822 return (found
!= NULL
);
826 spa_aux_activate(vdev_t
*vd
, avl_tree_t
*avl
)
828 spa_aux_t search
, *found
;
831 search
.aux_guid
= vd
->vdev_guid
;
832 found
= avl_find(avl
, &search
, &where
);
833 ASSERT(found
!= NULL
);
834 ASSERT(found
->aux_pool
== 0ULL);
836 found
->aux_pool
= spa_guid(vd
->vdev_spa
);
840 * Spares are tracked globally due to the following constraints:
842 * - A spare may be part of multiple pools.
843 * - A spare may be added to a pool even if it's actively in use within
845 * - A spare in use in any pool can only be the source of a replacement if
846 * the target is a spare in the same pool.
848 * We keep track of all spares on the system through the use of a reference
849 * counted AVL tree. When a vdev is added as a spare, or used as a replacement
850 * spare, then we bump the reference count in the AVL tree. In addition, we set
851 * the 'vdev_isspare' member to indicate that the device is a spare (active or
852 * inactive). When a spare is made active (used to replace a device in the
853 * pool), we also keep track of which pool its been made a part of.
855 * The 'spa_spare_lock' protects the AVL tree. These functions are normally
856 * called under the spa_namespace lock as part of vdev reconfiguration. The
857 * separate spare lock exists for the status query path, which does not need to
858 * be completely consistent with respect to other vdev configuration changes.
862 spa_spare_compare(const void *a
, const void *b
)
864 return (spa_aux_compare(a
, b
));
868 spa_spare_add(vdev_t
*vd
)
870 mutex_enter(&spa_spare_lock
);
871 ASSERT(!vd
->vdev_isspare
);
872 spa_aux_add(vd
, &spa_spare_avl
);
873 vd
->vdev_isspare
= B_TRUE
;
874 mutex_exit(&spa_spare_lock
);
878 spa_spare_remove(vdev_t
*vd
)
880 mutex_enter(&spa_spare_lock
);
881 ASSERT(vd
->vdev_isspare
);
882 spa_aux_remove(vd
, &spa_spare_avl
);
883 vd
->vdev_isspare
= B_FALSE
;
884 mutex_exit(&spa_spare_lock
);
888 spa_spare_exists(uint64_t guid
, uint64_t *pool
, int *refcnt
)
892 mutex_enter(&spa_spare_lock
);
893 found
= spa_aux_exists(guid
, pool
, refcnt
, &spa_spare_avl
);
894 mutex_exit(&spa_spare_lock
);
900 spa_spare_activate(vdev_t
*vd
)
902 mutex_enter(&spa_spare_lock
);
903 ASSERT(vd
->vdev_isspare
);
904 spa_aux_activate(vd
, &spa_spare_avl
);
905 mutex_exit(&spa_spare_lock
);
909 * Level 2 ARC devices are tracked globally for the same reasons as spares.
910 * Cache devices currently only support one pool per cache device, and so
911 * for these devices the aux reference count is currently unused beyond 1.
915 spa_l2cache_compare(const void *a
, const void *b
)
917 return (spa_aux_compare(a
, b
));
921 spa_l2cache_add(vdev_t
*vd
)
923 mutex_enter(&spa_l2cache_lock
);
924 ASSERT(!vd
->vdev_isl2cache
);
925 spa_aux_add(vd
, &spa_l2cache_avl
);
926 vd
->vdev_isl2cache
= B_TRUE
;
927 mutex_exit(&spa_l2cache_lock
);
931 spa_l2cache_remove(vdev_t
*vd
)
933 mutex_enter(&spa_l2cache_lock
);
934 ASSERT(vd
->vdev_isl2cache
);
935 spa_aux_remove(vd
, &spa_l2cache_avl
);
936 vd
->vdev_isl2cache
= B_FALSE
;
937 mutex_exit(&spa_l2cache_lock
);
941 spa_l2cache_exists(uint64_t guid
, uint64_t *pool
)
945 mutex_enter(&spa_l2cache_lock
);
946 found
= spa_aux_exists(guid
, pool
, NULL
, &spa_l2cache_avl
);
947 mutex_exit(&spa_l2cache_lock
);
953 spa_l2cache_activate(vdev_t
*vd
)
955 mutex_enter(&spa_l2cache_lock
);
956 ASSERT(vd
->vdev_isl2cache
);
957 spa_aux_activate(vd
, &spa_l2cache_avl
);
958 mutex_exit(&spa_l2cache_lock
);
962 * ==========================================================================
964 * ==========================================================================
968 * Lock the given spa_t for the purpose of adding or removing a vdev.
969 * Grabs the global spa_namespace_lock plus the spa config lock for writing.
970 * It returns the next transaction group for the spa_t.
973 spa_vdev_enter(spa_t
*spa
)
975 mutex_enter(&spa
->spa_vdev_top_lock
);
976 mutex_enter(&spa_namespace_lock
);
977 return (spa_vdev_config_enter(spa
));
981 * Internal implementation for spa_vdev_enter(). Used when a vdev
982 * operation requires multiple syncs (i.e. removing a device) while
983 * keeping the spa_namespace_lock held.
986 spa_vdev_config_enter(spa_t
*spa
)
988 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
990 spa_config_enter(spa
, SCL_ALL
, spa
, RW_WRITER
);
992 return (spa_last_synced_txg(spa
) + 1);
996 * Used in combination with spa_vdev_config_enter() to allow the syncing
997 * of multiple transactions without releasing the spa_namespace_lock.
1000 spa_vdev_config_exit(spa_t
*spa
, vdev_t
*vd
, uint64_t txg
, int error
, char *tag
)
1002 int config_changed
= B_FALSE
;
1004 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1005 ASSERT(txg
> spa_last_synced_txg(spa
));
1007 spa
->spa_pending_vdev
= NULL
;
1010 * Reassess the DTLs.
1012 vdev_dtl_reassess(spa
->spa_root_vdev
, 0, 0, B_FALSE
);
1014 if (error
== 0 && !list_is_empty(&spa
->spa_config_dirty_list
)) {
1015 config_changed
= B_TRUE
;
1016 spa
->spa_config_generation
++;
1020 * Verify the metaslab classes.
1022 ASSERT(metaslab_class_validate(spa_normal_class(spa
)) == 0);
1023 ASSERT(metaslab_class_validate(spa_log_class(spa
)) == 0);
1025 spa_config_exit(spa
, SCL_ALL
, spa
);
1028 * Panic the system if the specified tag requires it. This
1029 * is useful for ensuring that configurations are updated
1032 if (zio_injection_enabled
)
1033 zio_handle_panic_injection(spa
, tag
, 0);
1036 * Note: this txg_wait_synced() is important because it ensures
1037 * that there won't be more than one config change per txg.
1038 * This allows us to use the txg as the generation number.
1041 txg_wait_synced(spa
->spa_dsl_pool
, txg
);
1044 ASSERT(!vd
->vdev_detached
|| vd
->vdev_dtl_sm
== NULL
);
1045 spa_config_enter(spa
, SCL_ALL
, spa
, RW_WRITER
);
1047 spa_config_exit(spa
, SCL_ALL
, spa
);
1051 * If the config changed, update the config cache.
1054 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
1058 * Unlock the spa_t after adding or removing a vdev. Besides undoing the
1059 * locking of spa_vdev_enter(), we also want make sure the transactions have
1060 * synced to disk, and then update the global configuration cache with the new
1064 spa_vdev_exit(spa_t
*spa
, vdev_t
*vd
, uint64_t txg
, int error
)
1066 spa_vdev_config_exit(spa
, vd
, txg
, error
, FTAG
);
1067 mutex_exit(&spa_namespace_lock
);
1068 mutex_exit(&spa
->spa_vdev_top_lock
);
1074 * Lock the given spa_t for the purpose of changing vdev state.
1077 spa_vdev_state_enter(spa_t
*spa
, int oplocks
)
1079 int locks
= SCL_STATE_ALL
| oplocks
;
1082 * Root pools may need to read of the underlying devfs filesystem
1083 * when opening up a vdev. Unfortunately if we're holding the
1084 * SCL_ZIO lock it will result in a deadlock when we try to issue
1085 * the read from the root filesystem. Instead we "prefetch"
1086 * the associated vnodes that we need prior to opening the
1087 * underlying devices and cache them so that we can prevent
1088 * any I/O when we are doing the actual open.
1090 if (spa_is_root(spa
)) {
1091 int low
= locks
& ~(SCL_ZIO
- 1);
1092 int high
= locks
& ~low
;
1094 spa_config_enter(spa
, high
, spa
, RW_WRITER
);
1095 vdev_hold(spa
->spa_root_vdev
);
1096 spa_config_enter(spa
, low
, spa
, RW_WRITER
);
1098 spa_config_enter(spa
, locks
, spa
, RW_WRITER
);
1100 spa
->spa_vdev_locks
= locks
;
1104 spa_vdev_state_exit(spa_t
*spa
, vdev_t
*vd
, int error
)
1106 boolean_t config_changed
= B_FALSE
;
1108 if (vd
!= NULL
|| error
== 0)
1109 vdev_dtl_reassess(vd
? vd
->vdev_top
: spa
->spa_root_vdev
,
1113 vdev_state_dirty(vd
->vdev_top
);
1114 config_changed
= B_TRUE
;
1115 spa
->spa_config_generation
++;
1118 if (spa_is_root(spa
))
1119 vdev_rele(spa
->spa_root_vdev
);
1121 ASSERT3U(spa
->spa_vdev_locks
, >=, SCL_STATE_ALL
);
1122 spa_config_exit(spa
, spa
->spa_vdev_locks
, spa
);
1125 * If anything changed, wait for it to sync. This ensures that,
1126 * from the system administrator's perspective, zpool(1M) commands
1127 * are synchronous. This is important for things like zpool offline:
1128 * when the command completes, you expect no further I/O from ZFS.
1131 txg_wait_synced(spa
->spa_dsl_pool
, 0);
1134 * If the config changed, update the config cache.
1136 if (config_changed
) {
1137 mutex_enter(&spa_namespace_lock
);
1138 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
1139 mutex_exit(&spa_namespace_lock
);
1146 * ==========================================================================
1147 * Miscellaneous functions
1148 * ==========================================================================
1152 spa_activate_mos_feature(spa_t
*spa
, const char *feature
, dmu_tx_t
*tx
)
1154 if (!nvlist_exists(spa
->spa_label_features
, feature
)) {
1155 fnvlist_add_boolean(spa
->spa_label_features
, feature
);
1157 * When we are creating the pool (tx_txg==TXG_INITIAL), we can't
1158 * dirty the vdev config because lock SCL_CONFIG is not held.
1159 * Thankfully, in this case we don't need to dirty the config
1160 * because it will be written out anyway when we finish
1161 * creating the pool.
1163 if (tx
->tx_txg
!= TXG_INITIAL
)
1164 vdev_config_dirty(spa
->spa_root_vdev
);
1169 spa_deactivate_mos_feature(spa_t
*spa
, const char *feature
)
1171 if (nvlist_remove_all(spa
->spa_label_features
, feature
) == 0)
1172 vdev_config_dirty(spa
->spa_root_vdev
);
1179 spa_rename(const char *name
, const char *newname
)
1185 * Lookup the spa_t and grab the config lock for writing. We need to
1186 * actually open the pool so that we can sync out the necessary labels.
1187 * It's OK to call spa_open() with the namespace lock held because we
1188 * allow recursive calls for other reasons.
1190 mutex_enter(&spa_namespace_lock
);
1191 if ((err
= spa_open(name
, &spa
, FTAG
)) != 0) {
1192 mutex_exit(&spa_namespace_lock
);
1196 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1198 avl_remove(&spa_namespace_avl
, spa
);
1199 (void) strlcpy(spa
->spa_name
, newname
, sizeof (spa
->spa_name
));
1200 avl_add(&spa_namespace_avl
, spa
);
1203 * Sync all labels to disk with the new names by marking the root vdev
1204 * dirty and waiting for it to sync. It will pick up the new pool name
1207 vdev_config_dirty(spa
->spa_root_vdev
);
1209 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1211 txg_wait_synced(spa
->spa_dsl_pool
, 0);
1214 * Sync the updated config cache.
1216 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
1218 spa_close(spa
, FTAG
);
1220 mutex_exit(&spa_namespace_lock
);
1226 * Return the spa_t associated with given pool_guid, if it exists. If
1227 * device_guid is non-zero, determine whether the pool exists *and* contains
1228 * a device with the specified device_guid.
1231 spa_by_guid(uint64_t pool_guid
, uint64_t device_guid
)
1234 avl_tree_t
*t
= &spa_namespace_avl
;
1236 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1238 for (spa
= avl_first(t
); spa
!= NULL
; spa
= AVL_NEXT(t
, spa
)) {
1239 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
)
1241 if (spa
->spa_root_vdev
== NULL
)
1243 if (spa_guid(spa
) == pool_guid
) {
1244 if (device_guid
== 0)
1247 if (vdev_lookup_by_guid(spa
->spa_root_vdev
,
1248 device_guid
) != NULL
)
1252 * Check any devices we may be in the process of adding.
1254 if (spa
->spa_pending_vdev
) {
1255 if (vdev_lookup_by_guid(spa
->spa_pending_vdev
,
1256 device_guid
) != NULL
)
1266 * Determine whether a pool with the given pool_guid exists.
1269 spa_guid_exists(uint64_t pool_guid
, uint64_t device_guid
)
1271 return (spa_by_guid(pool_guid
, device_guid
) != NULL
);
1275 spa_strdup(const char *s
)
1281 new = kmem_alloc(len
+ 1, KM_SLEEP
);
1289 spa_strfree(char *s
)
1291 kmem_free(s
, strlen(s
) + 1);
1295 spa_get_random(uint64_t range
)
1301 (void) random_get_pseudo_bytes((void *)&r
, sizeof (uint64_t));
1307 spa_generate_guid(spa_t
*spa
)
1309 uint64_t guid
= spa_get_random(-1ULL);
1312 while (guid
== 0 || spa_guid_exists(spa_guid(spa
), guid
))
1313 guid
= spa_get_random(-1ULL);
1315 while (guid
== 0 || spa_guid_exists(guid
, 0))
1316 guid
= spa_get_random(-1ULL);
1323 snprintf_blkptr(char *buf
, size_t buflen
, const blkptr_t
*bp
)
1326 char *checksum
= NULL
;
1327 char *compress
= NULL
;
1330 if (BP_GET_TYPE(bp
) & DMU_OT_NEWTYPE
) {
1331 dmu_object_byteswap_t bswap
=
1332 DMU_OT_BYTESWAP(BP_GET_TYPE(bp
));
1333 (void) snprintf(type
, sizeof (type
), "bswap %s %s",
1334 DMU_OT_IS_METADATA(BP_GET_TYPE(bp
)) ?
1335 "metadata" : "data",
1336 dmu_ot_byteswap
[bswap
].ob_name
);
1338 (void) strlcpy(type
, dmu_ot
[BP_GET_TYPE(bp
)].ot_name
,
1341 if (!BP_IS_EMBEDDED(bp
)) {
1343 zio_checksum_table
[BP_GET_CHECKSUM(bp
)].ci_name
;
1345 compress
= zio_compress_table
[BP_GET_COMPRESS(bp
)].ci_name
;
1348 SNPRINTF_BLKPTR(snprintf
, ' ', buf
, buflen
, bp
, type
, checksum
,
1353 spa_freeze(spa_t
*spa
)
1355 uint64_t freeze_txg
= 0;
1357 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1358 if (spa
->spa_freeze_txg
== UINT64_MAX
) {
1359 freeze_txg
= spa_last_synced_txg(spa
) + TXG_SIZE
;
1360 spa
->spa_freeze_txg
= freeze_txg
;
1362 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1363 if (freeze_txg
!= 0)
1364 txg_wait_synced(spa_get_dsl(spa
), freeze_txg
);
1368 zfs_panic_recover(const char *fmt
, ...)
1373 vcmn_err(zfs_recover
? CE_WARN
: CE_PANIC
, fmt
, adx
);
1378 * This is a stripped-down version of strtoull, suitable only for converting
1379 * lowercase hexadecimal numbers that don't overflow.
1382 strtonum(const char *str
, char **nptr
)
1388 while ((c
= *str
) != '\0') {
1389 if (c
>= '0' && c
<= '9')
1391 else if (c
>= 'a' && c
<= 'f')
1392 digit
= 10 + c
- 'a';
1403 *nptr
= (char *)str
;
1409 * ==========================================================================
1410 * Accessor functions
1411 * ==========================================================================
1415 spa_shutting_down(spa_t
*spa
)
1417 return (spa
->spa_async_suspended
);
1421 spa_get_dsl(spa_t
*spa
)
1423 return (spa
->spa_dsl_pool
);
1427 spa_is_initializing(spa_t
*spa
)
1429 return (spa
->spa_is_initializing
);
1433 spa_get_rootblkptr(spa_t
*spa
)
1435 return (&spa
->spa_ubsync
.ub_rootbp
);
1439 spa_set_rootblkptr(spa_t
*spa
, const blkptr_t
*bp
)
1441 spa
->spa_uberblock
.ub_rootbp
= *bp
;
1445 spa_altroot(spa_t
*spa
, char *buf
, size_t buflen
)
1447 if (spa
->spa_root
== NULL
)
1450 (void) strncpy(buf
, spa
->spa_root
, buflen
);
1454 spa_sync_pass(spa_t
*spa
)
1456 return (spa
->spa_sync_pass
);
1460 spa_name(spa_t
*spa
)
1462 return (spa
->spa_name
);
1466 spa_guid(spa_t
*spa
)
1468 dsl_pool_t
*dp
= spa_get_dsl(spa
);
1472 * If we fail to parse the config during spa_load(), we can go through
1473 * the error path (which posts an ereport) and end up here with no root
1474 * vdev. We stash the original pool guid in 'spa_config_guid' to handle
1477 if (spa
->spa_root_vdev
== NULL
)
1478 return (spa
->spa_config_guid
);
1480 guid
= spa
->spa_last_synced_guid
!= 0 ?
1481 spa
->spa_last_synced_guid
: spa
->spa_root_vdev
->vdev_guid
;
1484 * Return the most recently synced out guid unless we're
1485 * in syncing context.
1487 if (dp
&& dsl_pool_sync_context(dp
))
1488 return (spa
->spa_root_vdev
->vdev_guid
);
1494 spa_load_guid(spa_t
*spa
)
1497 * This is a GUID that exists solely as a reference for the
1498 * purposes of the arc. It is generated at load time, and
1499 * is never written to persistent storage.
1501 return (spa
->spa_load_guid
);
1505 spa_last_synced_txg(spa_t
*spa
)
1507 return (spa
->spa_ubsync
.ub_txg
);
1511 spa_first_txg(spa_t
*spa
)
1513 return (spa
->spa_first_txg
);
1517 spa_syncing_txg(spa_t
*spa
)
1519 return (spa
->spa_syncing_txg
);
1523 spa_state(spa_t
*spa
)
1525 return (spa
->spa_state
);
1529 spa_load_state(spa_t
*spa
)
1531 return (spa
->spa_load_state
);
1535 spa_freeze_txg(spa_t
*spa
)
1537 return (spa
->spa_freeze_txg
);
1542 spa_get_asize(spa_t
*spa
, uint64_t lsize
)
1544 return (lsize
* spa_asize_inflation
);
1548 spa_get_dspace(spa_t
*spa
)
1550 return (spa
->spa_dspace
);
1554 spa_update_dspace(spa_t
*spa
)
1556 spa
->spa_dspace
= metaslab_class_get_dspace(spa_normal_class(spa
)) +
1557 ddt_get_dedup_dspace(spa
);
1561 * Return the failure mode that has been set to this pool. The default
1562 * behavior will be to block all I/Os when a complete failure occurs.
1565 spa_get_failmode(spa_t
*spa
)
1567 return (spa
->spa_failmode
);
1571 spa_suspended(spa_t
*spa
)
1573 return (spa
->spa_suspended
);
1577 spa_version(spa_t
*spa
)
1579 return (spa
->spa_ubsync
.ub_version
);
1583 spa_deflate(spa_t
*spa
)
1585 return (spa
->spa_deflate
);
1589 spa_normal_class(spa_t
*spa
)
1591 return (spa
->spa_normal_class
);
1595 spa_log_class(spa_t
*spa
)
1597 return (spa
->spa_log_class
);
1601 spa_max_replication(spa_t
*spa
)
1604 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
1605 * handle BPs with more than one DVA allocated. Set our max
1606 * replication level accordingly.
1608 if (spa_version(spa
) < SPA_VERSION_DITTO_BLOCKS
)
1610 return (MIN(SPA_DVAS_PER_BP
, spa_max_replication_override
));
1614 spa_prev_software_version(spa_t
*spa
)
1616 return (spa
->spa_prev_software_version
);
1620 spa_deadman_synctime(spa_t
*spa
)
1622 return (spa
->spa_deadman_synctime
);
1626 dva_get_dsize_sync(spa_t
*spa
, const dva_t
*dva
)
1628 uint64_t asize
= DVA_GET_ASIZE(dva
);
1629 uint64_t dsize
= asize
;
1631 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_READER
) != 0);
1633 if (asize
!= 0 && spa
->spa_deflate
) {
1634 vdev_t
*vd
= vdev_lookup_top(spa
, DVA_GET_VDEV(dva
));
1636 dsize
= (asize
>> SPA_MINBLOCKSHIFT
) *
1637 vd
->vdev_deflate_ratio
;
1644 bp_get_dsize_sync(spa_t
*spa
, const blkptr_t
*bp
)
1649 for (d
= 0; d
< BP_GET_NDVAS(bp
); d
++)
1650 dsize
+= dva_get_dsize_sync(spa
, &bp
->blk_dva
[d
]);
1656 bp_get_dsize(spa_t
*spa
, const blkptr_t
*bp
)
1661 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
1663 for (d
= 0; d
< BP_GET_NDVAS(bp
); d
++)
1664 dsize
+= dva_get_dsize_sync(spa
, &bp
->blk_dva
[d
]);
1666 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
1672 * ==========================================================================
1673 * Initialization and Termination
1674 * ==========================================================================
1678 spa_name_compare(const void *a1
, const void *a2
)
1680 const spa_t
*s1
= a1
;
1681 const spa_t
*s2
= a2
;
1684 s
= strcmp(s1
->spa_name
, s2
->spa_name
);
1701 mutex_init(&spa_namespace_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1702 mutex_init(&spa_spare_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1703 mutex_init(&spa_l2cache_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1704 cv_init(&spa_namespace_cv
, NULL
, CV_DEFAULT
, NULL
);
1706 avl_create(&spa_namespace_avl
, spa_name_compare
, sizeof (spa_t
),
1707 offsetof(spa_t
, spa_avl
));
1709 avl_create(&spa_spare_avl
, spa_spare_compare
, sizeof (spa_aux_t
),
1710 offsetof(spa_aux_t
, aux_avl
));
1712 avl_create(&spa_l2cache_avl
, spa_l2cache_compare
, sizeof (spa_aux_t
),
1713 offsetof(spa_aux_t
, aux_avl
));
1715 spa_mode_global
= mode
;
1718 if (spa_mode_global
!= FREAD
&& dprintf_find_string("watch")) {
1719 struct sigaction sa
;
1721 sa
.sa_flags
= SA_SIGINFO
;
1722 sigemptyset(&sa
.sa_mask
);
1723 sa
.sa_sigaction
= arc_buf_sigsegv
;
1725 if (sigaction(SIGSEGV
, &sa
, NULL
) == -1) {
1726 perror("could not enable watchpoints: "
1727 "sigaction(SIGSEGV, ...) = ");
1742 vdev_cache_stat_init();
1746 zpool_feature_init();
1759 vdev_cache_stat_fini();
1769 avl_destroy(&spa_namespace_avl
);
1770 avl_destroy(&spa_spare_avl
);
1771 avl_destroy(&spa_l2cache_avl
);
1773 cv_destroy(&spa_namespace_cv
);
1774 mutex_destroy(&spa_namespace_lock
);
1775 mutex_destroy(&spa_spare_lock
);
1776 mutex_destroy(&spa_l2cache_lock
);
1780 * Return whether this pool has slogs. No locking needed.
1781 * It's not a problem if the wrong answer is returned as it's only for
1782 * performance and not correctness
1785 spa_has_slogs(spa_t
*spa
)
1787 return (spa
->spa_log_class
->mc_rotor
!= NULL
);
1791 spa_get_log_state(spa_t
*spa
)
1793 return (spa
->spa_log_state
);
1797 spa_set_log_state(spa_t
*spa
, spa_log_state_t state
)
1799 spa
->spa_log_state
= state
;
1803 spa_is_root(spa_t
*spa
)
1805 return (spa
->spa_is_root
);
1809 spa_writeable(spa_t
*spa
)
1811 return (!!(spa
->spa_mode
& FWRITE
));
1815 * Returns true if there is a pending sync task in any of the current
1816 * syncing txg, the current quiescing txg, or the current open txg.
1819 spa_has_pending_synctask(spa_t
*spa
)
1821 return (!txg_all_lists_empty(&spa
->spa_dsl_pool
->dp_sync_tasks
));
1825 spa_mode(spa_t
*spa
)
1827 return (spa
->spa_mode
);
1831 spa_bootfs(spa_t
*spa
)
1833 return (spa
->spa_bootfs
);
1837 spa_delegation(spa_t
*spa
)
1839 return (spa
->spa_delegation
);
1843 spa_meta_objset(spa_t
*spa
)
1845 return (spa
->spa_meta_objset
);
1849 spa_dedup_checksum(spa_t
*spa
)
1851 return (spa
->spa_dedup_checksum
);
1855 * Reset pool scan stat per scan pass (or reboot).
1858 spa_scan_stat_init(spa_t
*spa
)
1860 /* data not stored on disk */
1861 spa
->spa_scan_pass_start
= gethrestime_sec();
1862 spa
->spa_scan_pass_exam
= 0;
1863 vdev_scan_stat_init(spa
->spa_root_vdev
);
1867 * Get scan stats for zpool status reports
1870 spa_scan_get_stats(spa_t
*spa
, pool_scan_stat_t
*ps
)
1872 dsl_scan_t
*scn
= spa
->spa_dsl_pool
? spa
->spa_dsl_pool
->dp_scan
: NULL
;
1874 if (scn
== NULL
|| scn
->scn_phys
.scn_func
== POOL_SCAN_NONE
)
1875 return (SET_ERROR(ENOENT
));
1876 bzero(ps
, sizeof (pool_scan_stat_t
));
1878 /* data stored on disk */
1879 ps
->pss_func
= scn
->scn_phys
.scn_func
;
1880 ps
->pss_start_time
= scn
->scn_phys
.scn_start_time
;
1881 ps
->pss_end_time
= scn
->scn_phys
.scn_end_time
;
1882 ps
->pss_to_examine
= scn
->scn_phys
.scn_to_examine
;
1883 ps
->pss_examined
= scn
->scn_phys
.scn_examined
;
1884 ps
->pss_to_process
= scn
->scn_phys
.scn_to_process
;
1885 ps
->pss_processed
= scn
->scn_phys
.scn_processed
;
1886 ps
->pss_errors
= scn
->scn_phys
.scn_errors
;
1887 ps
->pss_state
= scn
->scn_phys
.scn_state
;
1889 /* data not stored on disk */
1890 ps
->pss_pass_start
= spa
->spa_scan_pass_start
;
1891 ps
->pss_pass_exam
= spa
->spa_scan_pass_exam
;
1897 spa_debug_enabled(spa_t
*spa
)
1899 return (spa
->spa_debug
);
1902 #if defined(_KERNEL) && defined(HAVE_SPL)
1903 /* Namespace manipulation */
1904 EXPORT_SYMBOL(spa_lookup
);
1905 EXPORT_SYMBOL(spa_add
);
1906 EXPORT_SYMBOL(spa_remove
);
1907 EXPORT_SYMBOL(spa_next
);
1909 /* Refcount functions */
1910 EXPORT_SYMBOL(spa_open_ref
);
1911 EXPORT_SYMBOL(spa_close
);
1912 EXPORT_SYMBOL(spa_refcount_zero
);
1914 /* Pool configuration lock */
1915 EXPORT_SYMBOL(spa_config_tryenter
);
1916 EXPORT_SYMBOL(spa_config_enter
);
1917 EXPORT_SYMBOL(spa_config_exit
);
1918 EXPORT_SYMBOL(spa_config_held
);
1920 /* Pool vdev add/remove lock */
1921 EXPORT_SYMBOL(spa_vdev_enter
);
1922 EXPORT_SYMBOL(spa_vdev_exit
);
1924 /* Pool vdev state change lock */
1925 EXPORT_SYMBOL(spa_vdev_state_enter
);
1926 EXPORT_SYMBOL(spa_vdev_state_exit
);
1928 /* Accessor functions */
1929 EXPORT_SYMBOL(spa_shutting_down
);
1930 EXPORT_SYMBOL(spa_get_dsl
);
1931 EXPORT_SYMBOL(spa_get_rootblkptr
);
1932 EXPORT_SYMBOL(spa_set_rootblkptr
);
1933 EXPORT_SYMBOL(spa_altroot
);
1934 EXPORT_SYMBOL(spa_sync_pass
);
1935 EXPORT_SYMBOL(spa_name
);
1936 EXPORT_SYMBOL(spa_guid
);
1937 EXPORT_SYMBOL(spa_last_synced_txg
);
1938 EXPORT_SYMBOL(spa_first_txg
);
1939 EXPORT_SYMBOL(spa_syncing_txg
);
1940 EXPORT_SYMBOL(spa_version
);
1941 EXPORT_SYMBOL(spa_state
);
1942 EXPORT_SYMBOL(spa_load_state
);
1943 EXPORT_SYMBOL(spa_freeze_txg
);
1944 EXPORT_SYMBOL(spa_get_asize
);
1945 EXPORT_SYMBOL(spa_get_dspace
);
1946 EXPORT_SYMBOL(spa_update_dspace
);
1947 EXPORT_SYMBOL(spa_deflate
);
1948 EXPORT_SYMBOL(spa_normal_class
);
1949 EXPORT_SYMBOL(spa_log_class
);
1950 EXPORT_SYMBOL(spa_max_replication
);
1951 EXPORT_SYMBOL(spa_prev_software_version
);
1952 EXPORT_SYMBOL(spa_get_failmode
);
1953 EXPORT_SYMBOL(spa_suspended
);
1954 EXPORT_SYMBOL(spa_bootfs
);
1955 EXPORT_SYMBOL(spa_delegation
);
1956 EXPORT_SYMBOL(spa_meta_objset
);
1958 /* Miscellaneous support routines */
1959 EXPORT_SYMBOL(spa_rename
);
1960 EXPORT_SYMBOL(spa_guid_exists
);
1961 EXPORT_SYMBOL(spa_strdup
);
1962 EXPORT_SYMBOL(spa_strfree
);
1963 EXPORT_SYMBOL(spa_get_random
);
1964 EXPORT_SYMBOL(spa_generate_guid
);
1965 EXPORT_SYMBOL(snprintf_blkptr
);
1966 EXPORT_SYMBOL(spa_freeze
);
1967 EXPORT_SYMBOL(spa_upgrade
);
1968 EXPORT_SYMBOL(spa_evict_all
);
1969 EXPORT_SYMBOL(spa_lookup_by_guid
);
1970 EXPORT_SYMBOL(spa_has_spare
);
1971 EXPORT_SYMBOL(dva_get_dsize_sync
);
1972 EXPORT_SYMBOL(bp_get_dsize_sync
);
1973 EXPORT_SYMBOL(bp_get_dsize
);
1974 EXPORT_SYMBOL(spa_has_slogs
);
1975 EXPORT_SYMBOL(spa_is_root
);
1976 EXPORT_SYMBOL(spa_writeable
);
1977 EXPORT_SYMBOL(spa_mode
);
1979 EXPORT_SYMBOL(spa_namespace_lock
);
1981 module_param(zfs_flags
, uint
, 0644);
1982 MODULE_PARM_DESC(zfs_flags
, "Set additional debugging flags");
1984 module_param(zfs_recover
, int, 0644);
1985 MODULE_PARM_DESC(zfs_recover
, "Set to attempt to recover from fatal errors");
1987 module_param(zfs_free_leak_on_eio
, int, 0644);
1988 MODULE_PARM_DESC(zfs_free_leak_on_eio
,
1989 "Set to ignore IO errors during free and permanently leak the space");
1991 module_param(zfs_deadman_synctime_ms
, ulong
, 0644);
1992 MODULE_PARM_DESC(zfs_deadman_synctime_ms
, "Expiration time in milliseconds");
1994 module_param(zfs_deadman_enabled
, int, 0644);
1995 MODULE_PARM_DESC(zfs_deadman_enabled
, "Enable deadman timer");
1997 module_param(spa_asize_inflation
, int, 0644);
1998 MODULE_PARM_DESC(spa_asize_inflation
,
1999 "SPA size estimate multiplication factor");