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, 2015 by Delphix. All rights reserved.
24 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
28 #include <sys/zfs_context.h>
29 #include <sys/spa_impl.h>
31 #include <sys/zio_checksum.h>
32 #include <sys/zio_compress.h>
34 #include <sys/dmu_tx.h>
37 #include <sys/vdev_impl.h>
38 #include <sys/vdev_file.h>
39 #include <sys/vdev_raidz.h>
40 #include <sys/metaslab.h>
41 #include <sys/uberblock_impl.h>
44 #include <sys/unique.h>
45 #include <sys/dsl_pool.h>
46 #include <sys/dsl_dir.h>
47 #include <sys/dsl_prop.h>
48 #include <sys/fm/util.h>
49 #include <sys/dsl_scan.h>
50 #include <sys/fs/zfs.h>
51 #include <sys/metaslab_impl.h>
54 #include <sys/kstat.h>
56 #include "zfeature_common.h"
61 * There are four basic locks for managing spa_t structures:
63 * spa_namespace_lock (global mutex)
65 * This lock must be acquired to do any of the following:
67 * - Lookup a spa_t by name
68 * - Add or remove a spa_t from the namespace
69 * - Increase spa_refcount from non-zero
70 * - Check if spa_refcount is zero
72 * - add/remove/attach/detach devices
73 * - Held for the duration of create/destroy/import/export
75 * It does not need to handle recursion. A create or destroy may
76 * reference objects (files or zvols) in other pools, but by
77 * definition they must have an existing reference, and will never need
78 * to lookup a spa_t by name.
80 * spa_refcount (per-spa refcount_t protected by mutex)
82 * This reference count keep track of any active users of the spa_t. The
83 * spa_t cannot be destroyed or freed while this is non-zero. Internally,
84 * the refcount is never really 'zero' - opening a pool implicitly keeps
85 * some references in the DMU. Internally we check against spa_minref, but
86 * present the image of a zero/non-zero value to consumers.
88 * spa_config_lock[] (per-spa array of rwlocks)
90 * This protects the spa_t from config changes, and must be held in
91 * the following circumstances:
93 * - RW_READER to perform I/O to the spa
94 * - RW_WRITER to change the vdev config
96 * The locking order is fairly straightforward:
98 * spa_namespace_lock -> spa_refcount
100 * The namespace lock must be acquired to increase the refcount from 0
101 * or to check if it is zero.
103 * spa_refcount -> spa_config_lock[]
105 * There must be at least one valid reference on the spa_t to acquire
108 * spa_namespace_lock -> spa_config_lock[]
110 * The namespace lock must always be taken before the config lock.
113 * The spa_namespace_lock can be acquired directly and is globally visible.
115 * The namespace is manipulated using the following functions, all of which
116 * require the spa_namespace_lock to be held.
118 * spa_lookup() Lookup a spa_t by name.
120 * spa_add() Create a new spa_t in the namespace.
122 * spa_remove() Remove a spa_t from the namespace. This also
123 * frees up any memory associated with the spa_t.
125 * spa_next() Returns the next spa_t in the system, or the
126 * first if NULL is passed.
128 * spa_evict_all() Shutdown and remove all spa_t structures in
131 * spa_guid_exists() Determine whether a pool/device guid exists.
133 * The spa_refcount is manipulated using the following functions:
135 * spa_open_ref() Adds a reference to the given spa_t. Must be
136 * called with spa_namespace_lock held if the
137 * refcount is currently zero.
139 * spa_close() Remove a reference from the spa_t. This will
140 * not free the spa_t or remove it from the
141 * namespace. No locking is required.
143 * spa_refcount_zero() Returns true if the refcount is currently
144 * zero. Must be called with spa_namespace_lock
147 * The spa_config_lock[] is an array of rwlocks, ordered as follows:
148 * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV.
149 * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}().
151 * To read the configuration, it suffices to hold one of these locks as reader.
152 * To modify the configuration, you must hold all locks as writer. To modify
153 * vdev state without altering the vdev tree's topology (e.g. online/offline),
154 * you must hold SCL_STATE and SCL_ZIO as writer.
156 * We use these distinct config locks to avoid recursive lock entry.
157 * For example, spa_sync() (which holds SCL_CONFIG as reader) induces
158 * block allocations (SCL_ALLOC), which may require reading space maps
159 * from disk (dmu_read() -> zio_read() -> SCL_ZIO).
161 * The spa config locks cannot be normal rwlocks because we need the
162 * ability to hand off ownership. For example, SCL_ZIO is acquired
163 * by the issuing thread and later released by an interrupt thread.
164 * They do, however, obey the usual write-wanted semantics to prevent
165 * writer (i.e. system administrator) starvation.
167 * The lock acquisition rules are as follows:
170 * Protects changes to the vdev tree topology, such as vdev
171 * add/remove/attach/detach. Protects the dirty config list
172 * (spa_config_dirty_list) and the set of spares and l2arc devices.
175 * Protects changes to pool state and vdev state, such as vdev
176 * online/offline/fault/degrade/clear. Protects the dirty state list
177 * (spa_state_dirty_list) and global pool state (spa_state).
180 * Protects changes to metaslab groups and classes.
181 * Held as reader by metaslab_alloc() and metaslab_claim().
184 * Held by bp-level zios (those which have no io_vd upon entry)
185 * to prevent changes to the vdev tree. The bp-level zio implicitly
186 * protects all of its vdev child zios, which do not hold SCL_ZIO.
189 * Protects changes to metaslab groups and classes.
190 * Held as reader by metaslab_free(). SCL_FREE is distinct from
191 * SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free
192 * blocks in zio_done() while another i/o that holds either
193 * SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete.
196 * Held as reader to prevent changes to the vdev tree during trivial
197 * inquiries such as bp_get_dsize(). SCL_VDEV is distinct from the
198 * other locks, and lower than all of them, to ensure that it's safe
199 * to acquire regardless of caller context.
201 * In addition, the following rules apply:
203 * (a) spa_props_lock protects pool properties, spa_config and spa_config_list.
204 * The lock ordering is SCL_CONFIG > spa_props_lock.
206 * (b) I/O operations on leaf vdevs. For any zio operation that takes
207 * an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(),
208 * or zio_write_phys() -- the caller must ensure that the config cannot
209 * cannot change in the interim, and that the vdev cannot be reopened.
210 * SCL_STATE as reader suffices for both.
212 * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
214 * spa_vdev_enter() Acquire the namespace lock and the config lock
217 * spa_vdev_exit() Release the config lock, wait for all I/O
218 * to complete, sync the updated configs to the
219 * cache, and release the namespace lock.
221 * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit().
222 * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual
223 * locking is, always, based on spa_namespace_lock and spa_config_lock[].
225 * spa_rename() is also implemented within this file since it requires
226 * manipulation of the namespace.
229 static avl_tree_t spa_namespace_avl
;
230 kmutex_t spa_namespace_lock
;
231 static kcondvar_t spa_namespace_cv
;
232 int spa_max_replication_override
= SPA_DVAS_PER_BP
;
234 static kmutex_t spa_spare_lock
;
235 static avl_tree_t spa_spare_avl
;
236 static kmutex_t spa_l2cache_lock
;
237 static avl_tree_t spa_l2cache_avl
;
239 kmem_cache_t
*spa_buffer_pool
;
243 /* Everything except dprintf and spa is on by default in debug builds */
244 int zfs_flags
= ~(ZFS_DEBUG_DPRINTF
| ZFS_DEBUG_SPA
);
250 * zfs_recover can be set to nonzero to attempt to recover from
251 * otherwise-fatal errors, typically caused by on-disk corruption. When
252 * set, calls to zfs_panic_recover() will turn into warning messages.
253 * This should only be used as a last resort, as it typically results
254 * in leaked space, or worse.
256 int zfs_recover
= B_FALSE
;
259 * If destroy encounters an EIO while reading metadata (e.g. indirect
260 * blocks), space referenced by the missing metadata can not be freed.
261 * Normally this causes the background destroy to become "stalled", as
262 * it is unable to make forward progress. While in this stalled state,
263 * all remaining space to free from the error-encountering filesystem is
264 * "temporarily leaked". Set this flag to cause it to ignore the EIO,
265 * permanently leak the space from indirect blocks that can not be read,
266 * and continue to free everything else that it can.
268 * The default, "stalling" behavior is useful if the storage partially
269 * fails (i.e. some but not all i/os fail), and then later recovers. In
270 * this case, we will be able to continue pool operations while it is
271 * partially failed, and when it recovers, we can continue to free the
272 * space, with no leaks. However, note that this case is actually
275 * Typically pools either (a) fail completely (but perhaps temporarily,
276 * e.g. a top-level vdev going offline), or (b) have localized,
277 * permanent errors (e.g. disk returns the wrong data due to bit flip or
278 * firmware bug). In case (a), this setting does not matter because the
279 * pool will be suspended and the sync thread will not be able to make
280 * forward progress regardless. In case (b), because the error is
281 * permanent, the best we can do is leak the minimum amount of space,
282 * which is what setting this flag will do. Therefore, it is reasonable
283 * for this flag to normally be set, but we chose the more conservative
284 * approach of not setting it, so that there is no possibility of
285 * leaking space in the "partial temporary" failure case.
287 int zfs_free_leak_on_eio
= B_FALSE
;
290 * Expiration time in milliseconds. This value has two meanings. First it is
291 * used to determine when the spa_deadman() logic should fire. By default the
292 * spa_deadman() will fire if spa_sync() has not completed in 1000 seconds.
293 * Secondly, the value determines if an I/O is considered "hung". Any I/O that
294 * has not completed in zfs_deadman_synctime_ms is considered "hung" resulting
297 unsigned long zfs_deadman_synctime_ms
= 1000000ULL;
300 * By default the deadman is enabled.
302 int zfs_deadman_enabled
= 1;
305 * The worst case is single-sector max-parity RAID-Z blocks, in which
306 * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1)
307 * times the size; so just assume that. Add to this the fact that
308 * we can have up to 3 DVAs per bp, and one more factor of 2 because
309 * the block may be dittoed with up to 3 DVAs by ddt_sync(). All together,
311 * (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2 == 24
313 int spa_asize_inflation
= 24;
316 * Normally, we don't allow the last 3.2% (1/(2^spa_slop_shift)) of space in
317 * the pool to be consumed. This ensures that we don't run the pool
318 * completely out of space, due to unaccounted changes (e.g. to the MOS).
319 * It also limits the worst-case time to allocate space. If we have
320 * less than this amount of free space, most ZPL operations (e.g. write,
321 * create) will return ENOSPC.
323 * Certain operations (e.g. file removal, most administrative actions) can
324 * use half the slop space. They will only return ENOSPC if less than half
325 * the slop space is free. Typically, once the pool has less than the slop
326 * space free, the user will use these operations to free up space in the pool.
327 * These are the operations that call dsl_pool_adjustedsize() with the netfree
328 * argument set to TRUE.
330 * A very restricted set of operations are always permitted, regardless of
331 * the amount of free space. These are the operations that call
332 * dsl_sync_task(ZFS_SPACE_CHECK_NONE), e.g. "zfs destroy". If these
333 * operations result in a net increase in the amount of space used,
334 * it is possible to run the pool completely out of space, causing it to
335 * be permanently read-only.
337 * See also the comments in zfs_space_check_t.
339 int spa_slop_shift
= 5;
342 * ==========================================================================
344 * ==========================================================================
347 spa_config_lock_init(spa_t
*spa
)
351 for (i
= 0; i
< SCL_LOCKS
; i
++) {
352 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
353 mutex_init(&scl
->scl_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
354 cv_init(&scl
->scl_cv
, NULL
, CV_DEFAULT
, NULL
);
355 refcount_create_untracked(&scl
->scl_count
);
356 scl
->scl_writer
= NULL
;
357 scl
->scl_write_wanted
= 0;
362 spa_config_lock_destroy(spa_t
*spa
)
366 for (i
= 0; i
< SCL_LOCKS
; i
++) {
367 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
368 mutex_destroy(&scl
->scl_lock
);
369 cv_destroy(&scl
->scl_cv
);
370 refcount_destroy(&scl
->scl_count
);
371 ASSERT(scl
->scl_writer
== NULL
);
372 ASSERT(scl
->scl_write_wanted
== 0);
377 spa_config_tryenter(spa_t
*spa
, int locks
, void *tag
, krw_t rw
)
381 for (i
= 0; i
< SCL_LOCKS
; i
++) {
382 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
383 if (!(locks
& (1 << i
)))
385 mutex_enter(&scl
->scl_lock
);
386 if (rw
== RW_READER
) {
387 if (scl
->scl_writer
|| scl
->scl_write_wanted
) {
388 mutex_exit(&scl
->scl_lock
);
389 spa_config_exit(spa
, locks
& ((1 << i
) - 1),
394 ASSERT(scl
->scl_writer
!= curthread
);
395 if (!refcount_is_zero(&scl
->scl_count
)) {
396 mutex_exit(&scl
->scl_lock
);
397 spa_config_exit(spa
, locks
& ((1 << i
) - 1),
401 scl
->scl_writer
= curthread
;
403 (void) refcount_add(&scl
->scl_count
, tag
);
404 mutex_exit(&scl
->scl_lock
);
410 spa_config_enter(spa_t
*spa
, int locks
, void *tag
, krw_t rw
)
415 ASSERT3U(SCL_LOCKS
, <, sizeof (wlocks_held
) * NBBY
);
417 for (i
= 0; i
< SCL_LOCKS
; i
++) {
418 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
419 if (scl
->scl_writer
== curthread
)
420 wlocks_held
|= (1 << i
);
421 if (!(locks
& (1 << i
)))
423 mutex_enter(&scl
->scl_lock
);
424 if (rw
== RW_READER
) {
425 while (scl
->scl_writer
|| scl
->scl_write_wanted
) {
426 cv_wait(&scl
->scl_cv
, &scl
->scl_lock
);
429 ASSERT(scl
->scl_writer
!= curthread
);
430 while (!refcount_is_zero(&scl
->scl_count
)) {
431 scl
->scl_write_wanted
++;
432 cv_wait(&scl
->scl_cv
, &scl
->scl_lock
);
433 scl
->scl_write_wanted
--;
435 scl
->scl_writer
= curthread
;
437 (void) refcount_add(&scl
->scl_count
, tag
);
438 mutex_exit(&scl
->scl_lock
);
440 ASSERT(wlocks_held
<= locks
);
444 spa_config_exit(spa_t
*spa
, int locks
, void *tag
)
448 for (i
= SCL_LOCKS
- 1; i
>= 0; i
--) {
449 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
450 if (!(locks
& (1 << i
)))
452 mutex_enter(&scl
->scl_lock
);
453 ASSERT(!refcount_is_zero(&scl
->scl_count
));
454 if (refcount_remove(&scl
->scl_count
, tag
) == 0) {
455 ASSERT(scl
->scl_writer
== NULL
||
456 scl
->scl_writer
== curthread
);
457 scl
->scl_writer
= NULL
; /* OK in either case */
458 cv_broadcast(&scl
->scl_cv
);
460 mutex_exit(&scl
->scl_lock
);
465 spa_config_held(spa_t
*spa
, int locks
, krw_t rw
)
467 int i
, locks_held
= 0;
469 for (i
= 0; i
< SCL_LOCKS
; i
++) {
470 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
471 if (!(locks
& (1 << i
)))
473 if ((rw
== RW_READER
&& !refcount_is_zero(&scl
->scl_count
)) ||
474 (rw
== RW_WRITER
&& scl
->scl_writer
== curthread
))
475 locks_held
|= 1 << i
;
482 * ==========================================================================
483 * SPA namespace functions
484 * ==========================================================================
488 * Lookup the named spa_t in the AVL tree. The spa_namespace_lock must be held.
489 * Returns NULL if no matching spa_t is found.
492 spa_lookup(const char *name
)
494 static spa_t search
; /* spa_t is large; don't allocate on stack */
499 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
501 (void) strlcpy(search
.spa_name
, name
, sizeof (search
.spa_name
));
504 * If it's a full dataset name, figure out the pool name and
507 cp
= strpbrk(search
.spa_name
, "/@#");
511 spa
= avl_find(&spa_namespace_avl
, &search
, &where
);
517 * Fires when spa_sync has not completed within zfs_deadman_synctime_ms.
518 * If the zfs_deadman_enabled flag is set then it inspects all vdev queues
519 * looking for potentially hung I/Os.
522 spa_deadman(void *arg
)
526 zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu",
527 (gethrtime() - spa
->spa_sync_starttime
) / NANOSEC
,
528 ++spa
->spa_deadman_calls
);
529 if (zfs_deadman_enabled
)
530 vdev_deadman(spa
->spa_root_vdev
);
532 spa
->spa_deadman_tqid
= taskq_dispatch_delay(system_taskq
,
533 spa_deadman
, spa
, TQ_SLEEP
, ddi_get_lbolt() +
534 NSEC_TO_TICK(spa
->spa_deadman_synctime
));
538 * Create an uninitialized spa_t with the given name. Requires
539 * spa_namespace_lock. The caller must ensure that the spa_t doesn't already
540 * exist by calling spa_lookup() first.
543 spa_add(const char *name
, nvlist_t
*config
, const char *altroot
)
546 spa_config_dirent_t
*dp
;
550 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
552 spa
= kmem_zalloc(sizeof (spa_t
), KM_SLEEP
);
554 mutex_init(&spa
->spa_async_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
555 mutex_init(&spa
->spa_errlist_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
556 mutex_init(&spa
->spa_errlog_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
557 mutex_init(&spa
->spa_evicting_os_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
558 mutex_init(&spa
->spa_history_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
559 mutex_init(&spa
->spa_proc_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
560 mutex_init(&spa
->spa_props_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
561 mutex_init(&spa
->spa_scrub_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
562 mutex_init(&spa
->spa_suspend_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
563 mutex_init(&spa
->spa_vdev_top_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
564 mutex_init(&spa
->spa_feat_stats_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
566 cv_init(&spa
->spa_async_cv
, NULL
, CV_DEFAULT
, NULL
);
567 cv_init(&spa
->spa_evicting_os_cv
, NULL
, CV_DEFAULT
, NULL
);
568 cv_init(&spa
->spa_proc_cv
, NULL
, CV_DEFAULT
, NULL
);
569 cv_init(&spa
->spa_scrub_io_cv
, NULL
, CV_DEFAULT
, NULL
);
570 cv_init(&spa
->spa_suspend_cv
, NULL
, CV_DEFAULT
, NULL
);
572 for (t
= 0; t
< TXG_SIZE
; t
++)
573 bplist_create(&spa
->spa_free_bplist
[t
]);
575 (void) strlcpy(spa
->spa_name
, name
, sizeof (spa
->spa_name
));
576 spa
->spa_state
= POOL_STATE_UNINITIALIZED
;
577 spa
->spa_freeze_txg
= UINT64_MAX
;
578 spa
->spa_final_txg
= UINT64_MAX
;
579 spa
->spa_load_max_txg
= UINT64_MAX
;
581 spa
->spa_proc_state
= SPA_PROC_NONE
;
583 spa
->spa_deadman_synctime
= MSEC2NSEC(zfs_deadman_synctime_ms
);
585 refcount_create(&spa
->spa_refcount
);
586 spa_config_lock_init(spa
);
589 avl_add(&spa_namespace_avl
, spa
);
592 * Set the alternate root, if there is one.
595 spa
->spa_root
= spa_strdup(altroot
);
598 * Every pool starts with the default cachefile
600 list_create(&spa
->spa_config_list
, sizeof (spa_config_dirent_t
),
601 offsetof(spa_config_dirent_t
, scd_link
));
603 dp
= kmem_zalloc(sizeof (spa_config_dirent_t
), KM_SLEEP
);
604 dp
->scd_path
= altroot
? NULL
: spa_strdup(spa_config_path
);
605 list_insert_head(&spa
->spa_config_list
, dp
);
607 VERIFY(nvlist_alloc(&spa
->spa_load_info
, NV_UNIQUE_NAME
,
610 if (config
!= NULL
) {
613 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_FEATURES_FOR_READ
,
615 VERIFY(nvlist_dup(features
, &spa
->spa_label_features
,
619 VERIFY(nvlist_dup(config
, &spa
->spa_config
, 0) == 0);
622 if (spa
->spa_label_features
== NULL
) {
623 VERIFY(nvlist_alloc(&spa
->spa_label_features
, NV_UNIQUE_NAME
,
627 spa
->spa_debug
= ((zfs_flags
& ZFS_DEBUG_SPA
) != 0);
629 spa
->spa_min_ashift
= INT_MAX
;
630 spa
->spa_max_ashift
= 0;
633 * As a pool is being created, treat all features as disabled by
634 * setting SPA_FEATURE_DISABLED for all entries in the feature
637 for (i
= 0; i
< SPA_FEATURES
; i
++) {
638 spa
->spa_feat_refcount_cache
[i
] = SPA_FEATURE_DISABLED
;
645 * Removes a spa_t from the namespace, freeing up any memory used. Requires
646 * spa_namespace_lock. This is called only after the spa_t has been closed and
650 spa_remove(spa_t
*spa
)
652 spa_config_dirent_t
*dp
;
655 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
656 ASSERT(spa
->spa_state
== POOL_STATE_UNINITIALIZED
);
657 ASSERT3U(refcount_count(&spa
->spa_refcount
), ==, 0);
659 nvlist_free(spa
->spa_config_splitting
);
661 avl_remove(&spa_namespace_avl
, spa
);
662 cv_broadcast(&spa_namespace_cv
);
665 spa_strfree(spa
->spa_root
);
667 while ((dp
= list_head(&spa
->spa_config_list
)) != NULL
) {
668 list_remove(&spa
->spa_config_list
, dp
);
669 if (dp
->scd_path
!= NULL
)
670 spa_strfree(dp
->scd_path
);
671 kmem_free(dp
, sizeof (spa_config_dirent_t
));
674 list_destroy(&spa
->spa_config_list
);
676 nvlist_free(spa
->spa_label_features
);
677 nvlist_free(spa
->spa_load_info
);
678 nvlist_free(spa
->spa_feat_stats
);
679 spa_config_set(spa
, NULL
);
681 refcount_destroy(&spa
->spa_refcount
);
683 spa_stats_destroy(spa
);
684 spa_config_lock_destroy(spa
);
686 for (t
= 0; t
< TXG_SIZE
; t
++)
687 bplist_destroy(&spa
->spa_free_bplist
[t
]);
689 cv_destroy(&spa
->spa_async_cv
);
690 cv_destroy(&spa
->spa_evicting_os_cv
);
691 cv_destroy(&spa
->spa_proc_cv
);
692 cv_destroy(&spa
->spa_scrub_io_cv
);
693 cv_destroy(&spa
->spa_suspend_cv
);
695 mutex_destroy(&spa
->spa_async_lock
);
696 mutex_destroy(&spa
->spa_errlist_lock
);
697 mutex_destroy(&spa
->spa_errlog_lock
);
698 mutex_destroy(&spa
->spa_evicting_os_lock
);
699 mutex_destroy(&spa
->spa_history_lock
);
700 mutex_destroy(&spa
->spa_proc_lock
);
701 mutex_destroy(&spa
->spa_props_lock
);
702 mutex_destroy(&spa
->spa_scrub_lock
);
703 mutex_destroy(&spa
->spa_suspend_lock
);
704 mutex_destroy(&spa
->spa_vdev_top_lock
);
705 mutex_destroy(&spa
->spa_feat_stats_lock
);
707 kmem_free(spa
, sizeof (spa_t
));
711 * Given a pool, return the next pool in the namespace, or NULL if there is
712 * none. If 'prev' is NULL, return the first pool.
715 spa_next(spa_t
*prev
)
717 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
720 return (AVL_NEXT(&spa_namespace_avl
, prev
));
722 return (avl_first(&spa_namespace_avl
));
726 * ==========================================================================
727 * SPA refcount functions
728 * ==========================================================================
732 * Add a reference to the given spa_t. Must have at least one reference, or
733 * have the namespace lock held.
736 spa_open_ref(spa_t
*spa
, void *tag
)
738 ASSERT(refcount_count(&spa
->spa_refcount
) >= spa
->spa_minref
||
739 MUTEX_HELD(&spa_namespace_lock
));
740 (void) refcount_add(&spa
->spa_refcount
, tag
);
744 * Remove a reference to the given spa_t. Must have at least one reference, or
745 * have the namespace lock held.
748 spa_close(spa_t
*spa
, void *tag
)
750 ASSERT(refcount_count(&spa
->spa_refcount
) > spa
->spa_minref
||
751 MUTEX_HELD(&spa_namespace_lock
));
752 (void) refcount_remove(&spa
->spa_refcount
, tag
);
756 * Remove a reference to the given spa_t held by a dsl dir that is
757 * being asynchronously released. Async releases occur from a taskq
758 * performing eviction of dsl datasets and dirs. The namespace lock
759 * isn't held and the hold by the object being evicted may contribute to
760 * spa_minref (e.g. dataset or directory released during pool export),
761 * so the asserts in spa_close() do not apply.
764 spa_async_close(spa_t
*spa
, void *tag
)
766 (void) refcount_remove(&spa
->spa_refcount
, tag
);
770 * Check to see if the spa refcount is zero. Must be called with
771 * spa_namespace_lock held. We really compare against spa_minref, which is the
772 * number of references acquired when opening a pool
775 spa_refcount_zero(spa_t
*spa
)
777 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
779 return (refcount_count(&spa
->spa_refcount
) == spa
->spa_minref
);
783 * ==========================================================================
784 * SPA spare and l2cache tracking
785 * ==========================================================================
789 * Hot spares and cache devices are tracked using the same code below,
790 * for 'auxiliary' devices.
793 typedef struct spa_aux
{
801 spa_aux_compare(const void *a
, const void *b
)
803 const spa_aux_t
*sa
= a
;
804 const spa_aux_t
*sb
= b
;
806 if (sa
->aux_guid
< sb
->aux_guid
)
808 else if (sa
->aux_guid
> sb
->aux_guid
)
815 spa_aux_add(vdev_t
*vd
, avl_tree_t
*avl
)
821 search
.aux_guid
= vd
->vdev_guid
;
822 if ((aux
= avl_find(avl
, &search
, &where
)) != NULL
) {
825 aux
= kmem_zalloc(sizeof (spa_aux_t
), KM_SLEEP
);
826 aux
->aux_guid
= vd
->vdev_guid
;
828 avl_insert(avl
, aux
, where
);
833 spa_aux_remove(vdev_t
*vd
, avl_tree_t
*avl
)
839 search
.aux_guid
= vd
->vdev_guid
;
840 aux
= avl_find(avl
, &search
, &where
);
844 if (--aux
->aux_count
== 0) {
845 avl_remove(avl
, aux
);
846 kmem_free(aux
, sizeof (spa_aux_t
));
847 } else if (aux
->aux_pool
== spa_guid(vd
->vdev_spa
)) {
848 aux
->aux_pool
= 0ULL;
853 spa_aux_exists(uint64_t guid
, uint64_t *pool
, int *refcnt
, avl_tree_t
*avl
)
855 spa_aux_t search
, *found
;
857 search
.aux_guid
= guid
;
858 found
= avl_find(avl
, &search
, NULL
);
862 *pool
= found
->aux_pool
;
869 *refcnt
= found
->aux_count
;
874 return (found
!= NULL
);
878 spa_aux_activate(vdev_t
*vd
, avl_tree_t
*avl
)
880 spa_aux_t search
, *found
;
883 search
.aux_guid
= vd
->vdev_guid
;
884 found
= avl_find(avl
, &search
, &where
);
885 ASSERT(found
!= NULL
);
886 ASSERT(found
->aux_pool
== 0ULL);
888 found
->aux_pool
= spa_guid(vd
->vdev_spa
);
892 * Spares are tracked globally due to the following constraints:
894 * - A spare may be part of multiple pools.
895 * - A spare may be added to a pool even if it's actively in use within
897 * - A spare in use in any pool can only be the source of a replacement if
898 * the target is a spare in the same pool.
900 * We keep track of all spares on the system through the use of a reference
901 * counted AVL tree. When a vdev is added as a spare, or used as a replacement
902 * spare, then we bump the reference count in the AVL tree. In addition, we set
903 * the 'vdev_isspare' member to indicate that the device is a spare (active or
904 * inactive). When a spare is made active (used to replace a device in the
905 * pool), we also keep track of which pool its been made a part of.
907 * The 'spa_spare_lock' protects the AVL tree. These functions are normally
908 * called under the spa_namespace lock as part of vdev reconfiguration. The
909 * separate spare lock exists for the status query path, which does not need to
910 * be completely consistent with respect to other vdev configuration changes.
914 spa_spare_compare(const void *a
, const void *b
)
916 return (spa_aux_compare(a
, b
));
920 spa_spare_add(vdev_t
*vd
)
922 mutex_enter(&spa_spare_lock
);
923 ASSERT(!vd
->vdev_isspare
);
924 spa_aux_add(vd
, &spa_spare_avl
);
925 vd
->vdev_isspare
= B_TRUE
;
926 mutex_exit(&spa_spare_lock
);
930 spa_spare_remove(vdev_t
*vd
)
932 mutex_enter(&spa_spare_lock
);
933 ASSERT(vd
->vdev_isspare
);
934 spa_aux_remove(vd
, &spa_spare_avl
);
935 vd
->vdev_isspare
= B_FALSE
;
936 mutex_exit(&spa_spare_lock
);
940 spa_spare_exists(uint64_t guid
, uint64_t *pool
, int *refcnt
)
944 mutex_enter(&spa_spare_lock
);
945 found
= spa_aux_exists(guid
, pool
, refcnt
, &spa_spare_avl
);
946 mutex_exit(&spa_spare_lock
);
952 spa_spare_activate(vdev_t
*vd
)
954 mutex_enter(&spa_spare_lock
);
955 ASSERT(vd
->vdev_isspare
);
956 spa_aux_activate(vd
, &spa_spare_avl
);
957 mutex_exit(&spa_spare_lock
);
961 * Level 2 ARC devices are tracked globally for the same reasons as spares.
962 * Cache devices currently only support one pool per cache device, and so
963 * for these devices the aux reference count is currently unused beyond 1.
967 spa_l2cache_compare(const void *a
, const void *b
)
969 return (spa_aux_compare(a
, b
));
973 spa_l2cache_add(vdev_t
*vd
)
975 mutex_enter(&spa_l2cache_lock
);
976 ASSERT(!vd
->vdev_isl2cache
);
977 spa_aux_add(vd
, &spa_l2cache_avl
);
978 vd
->vdev_isl2cache
= B_TRUE
;
979 mutex_exit(&spa_l2cache_lock
);
983 spa_l2cache_remove(vdev_t
*vd
)
985 mutex_enter(&spa_l2cache_lock
);
986 ASSERT(vd
->vdev_isl2cache
);
987 spa_aux_remove(vd
, &spa_l2cache_avl
);
988 vd
->vdev_isl2cache
= B_FALSE
;
989 mutex_exit(&spa_l2cache_lock
);
993 spa_l2cache_exists(uint64_t guid
, uint64_t *pool
)
997 mutex_enter(&spa_l2cache_lock
);
998 found
= spa_aux_exists(guid
, pool
, NULL
, &spa_l2cache_avl
);
999 mutex_exit(&spa_l2cache_lock
);
1005 spa_l2cache_activate(vdev_t
*vd
)
1007 mutex_enter(&spa_l2cache_lock
);
1008 ASSERT(vd
->vdev_isl2cache
);
1009 spa_aux_activate(vd
, &spa_l2cache_avl
);
1010 mutex_exit(&spa_l2cache_lock
);
1014 * ==========================================================================
1016 * ==========================================================================
1020 * Lock the given spa_t for the purpose of adding or removing a vdev.
1021 * Grabs the global spa_namespace_lock plus the spa config lock for writing.
1022 * It returns the next transaction group for the spa_t.
1025 spa_vdev_enter(spa_t
*spa
)
1027 mutex_enter(&spa
->spa_vdev_top_lock
);
1028 mutex_enter(&spa_namespace_lock
);
1029 return (spa_vdev_config_enter(spa
));
1033 * Internal implementation for spa_vdev_enter(). Used when a vdev
1034 * operation requires multiple syncs (i.e. removing a device) while
1035 * keeping the spa_namespace_lock held.
1038 spa_vdev_config_enter(spa_t
*spa
)
1040 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1042 spa_config_enter(spa
, SCL_ALL
, spa
, RW_WRITER
);
1044 return (spa_last_synced_txg(spa
) + 1);
1048 * Used in combination with spa_vdev_config_enter() to allow the syncing
1049 * of multiple transactions without releasing the spa_namespace_lock.
1052 spa_vdev_config_exit(spa_t
*spa
, vdev_t
*vd
, uint64_t txg
, int error
, char *tag
)
1054 int config_changed
= B_FALSE
;
1056 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1057 ASSERT(txg
> spa_last_synced_txg(spa
));
1059 spa
->spa_pending_vdev
= NULL
;
1062 * Reassess the DTLs.
1064 vdev_dtl_reassess(spa
->spa_root_vdev
, 0, 0, B_FALSE
);
1066 if (error
== 0 && !list_is_empty(&spa
->spa_config_dirty_list
)) {
1067 config_changed
= B_TRUE
;
1068 spa
->spa_config_generation
++;
1072 * Verify the metaslab classes.
1074 ASSERT(metaslab_class_validate(spa_normal_class(spa
)) == 0);
1075 ASSERT(metaslab_class_validate(spa_log_class(spa
)) == 0);
1077 spa_config_exit(spa
, SCL_ALL
, spa
);
1080 * Panic the system if the specified tag requires it. This
1081 * is useful for ensuring that configurations are updated
1084 if (zio_injection_enabled
)
1085 zio_handle_panic_injection(spa
, tag
, 0);
1088 * Note: this txg_wait_synced() is important because it ensures
1089 * that there won't be more than one config change per txg.
1090 * This allows us to use the txg as the generation number.
1093 txg_wait_synced(spa
->spa_dsl_pool
, txg
);
1096 ASSERT(!vd
->vdev_detached
|| vd
->vdev_dtl_sm
== NULL
);
1097 spa_config_enter(spa
, SCL_ALL
, spa
, RW_WRITER
);
1099 spa_config_exit(spa
, SCL_ALL
, spa
);
1103 * If the config changed, update the config cache.
1106 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
1110 * Unlock the spa_t after adding or removing a vdev. Besides undoing the
1111 * locking of spa_vdev_enter(), we also want make sure the transactions have
1112 * synced to disk, and then update the global configuration cache with the new
1116 spa_vdev_exit(spa_t
*spa
, vdev_t
*vd
, uint64_t txg
, int error
)
1118 spa_vdev_config_exit(spa
, vd
, txg
, error
, FTAG
);
1119 mutex_exit(&spa_namespace_lock
);
1120 mutex_exit(&spa
->spa_vdev_top_lock
);
1126 * Lock the given spa_t for the purpose of changing vdev state.
1129 spa_vdev_state_enter(spa_t
*spa
, int oplocks
)
1131 int locks
= SCL_STATE_ALL
| oplocks
;
1134 * Root pools may need to read of the underlying devfs filesystem
1135 * when opening up a vdev. Unfortunately if we're holding the
1136 * SCL_ZIO lock it will result in a deadlock when we try to issue
1137 * the read from the root filesystem. Instead we "prefetch"
1138 * the associated vnodes that we need prior to opening the
1139 * underlying devices and cache them so that we can prevent
1140 * any I/O when we are doing the actual open.
1142 if (spa_is_root(spa
)) {
1143 int low
= locks
& ~(SCL_ZIO
- 1);
1144 int high
= locks
& ~low
;
1146 spa_config_enter(spa
, high
, spa
, RW_WRITER
);
1147 vdev_hold(spa
->spa_root_vdev
);
1148 spa_config_enter(spa
, low
, spa
, RW_WRITER
);
1150 spa_config_enter(spa
, locks
, spa
, RW_WRITER
);
1152 spa
->spa_vdev_locks
= locks
;
1156 spa_vdev_state_exit(spa_t
*spa
, vdev_t
*vd
, int error
)
1158 boolean_t config_changed
= B_FALSE
;
1160 if (vd
!= NULL
|| error
== 0)
1161 vdev_dtl_reassess(vd
? vd
->vdev_top
: spa
->spa_root_vdev
,
1165 vdev_state_dirty(vd
->vdev_top
);
1166 config_changed
= B_TRUE
;
1167 spa
->spa_config_generation
++;
1170 if (spa_is_root(spa
))
1171 vdev_rele(spa
->spa_root_vdev
);
1173 ASSERT3U(spa
->spa_vdev_locks
, >=, SCL_STATE_ALL
);
1174 spa_config_exit(spa
, spa
->spa_vdev_locks
, spa
);
1177 * If anything changed, wait for it to sync. This ensures that,
1178 * from the system administrator's perspective, zpool(1M) commands
1179 * are synchronous. This is important for things like zpool offline:
1180 * when the command completes, you expect no further I/O from ZFS.
1183 txg_wait_synced(spa
->spa_dsl_pool
, 0);
1186 * If the config changed, update the config cache.
1188 if (config_changed
) {
1189 mutex_enter(&spa_namespace_lock
);
1190 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
1191 mutex_exit(&spa_namespace_lock
);
1198 * ==========================================================================
1199 * Miscellaneous functions
1200 * ==========================================================================
1204 spa_activate_mos_feature(spa_t
*spa
, const char *feature
, dmu_tx_t
*tx
)
1206 if (!nvlist_exists(spa
->spa_label_features
, feature
)) {
1207 fnvlist_add_boolean(spa
->spa_label_features
, feature
);
1209 * When we are creating the pool (tx_txg==TXG_INITIAL), we can't
1210 * dirty the vdev config because lock SCL_CONFIG is not held.
1211 * Thankfully, in this case we don't need to dirty the config
1212 * because it will be written out anyway when we finish
1213 * creating the pool.
1215 if (tx
->tx_txg
!= TXG_INITIAL
)
1216 vdev_config_dirty(spa
->spa_root_vdev
);
1221 spa_deactivate_mos_feature(spa_t
*spa
, const char *feature
)
1223 if (nvlist_remove_all(spa
->spa_label_features
, feature
) == 0)
1224 vdev_config_dirty(spa
->spa_root_vdev
);
1231 spa_rename(const char *name
, const char *newname
)
1237 * Lookup the spa_t and grab the config lock for writing. We need to
1238 * actually open the pool so that we can sync out the necessary labels.
1239 * It's OK to call spa_open() with the namespace lock held because we
1240 * allow recursive calls for other reasons.
1242 mutex_enter(&spa_namespace_lock
);
1243 if ((err
= spa_open(name
, &spa
, FTAG
)) != 0) {
1244 mutex_exit(&spa_namespace_lock
);
1248 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1250 avl_remove(&spa_namespace_avl
, spa
);
1251 (void) strlcpy(spa
->spa_name
, newname
, sizeof (spa
->spa_name
));
1252 avl_add(&spa_namespace_avl
, spa
);
1255 * Sync all labels to disk with the new names by marking the root vdev
1256 * dirty and waiting for it to sync. It will pick up the new pool name
1259 vdev_config_dirty(spa
->spa_root_vdev
);
1261 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1263 txg_wait_synced(spa
->spa_dsl_pool
, 0);
1266 * Sync the updated config cache.
1268 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
1270 spa_close(spa
, FTAG
);
1272 mutex_exit(&spa_namespace_lock
);
1278 * Return the spa_t associated with given pool_guid, if it exists. If
1279 * device_guid is non-zero, determine whether the pool exists *and* contains
1280 * a device with the specified device_guid.
1283 spa_by_guid(uint64_t pool_guid
, uint64_t device_guid
)
1286 avl_tree_t
*t
= &spa_namespace_avl
;
1288 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1290 for (spa
= avl_first(t
); spa
!= NULL
; spa
= AVL_NEXT(t
, spa
)) {
1291 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
)
1293 if (spa
->spa_root_vdev
== NULL
)
1295 if (spa_guid(spa
) == pool_guid
) {
1296 if (device_guid
== 0)
1299 if (vdev_lookup_by_guid(spa
->spa_root_vdev
,
1300 device_guid
) != NULL
)
1304 * Check any devices we may be in the process of adding.
1306 if (spa
->spa_pending_vdev
) {
1307 if (vdev_lookup_by_guid(spa
->spa_pending_vdev
,
1308 device_guid
) != NULL
)
1318 * Determine whether a pool with the given pool_guid exists.
1321 spa_guid_exists(uint64_t pool_guid
, uint64_t device_guid
)
1323 return (spa_by_guid(pool_guid
, device_guid
) != NULL
);
1327 spa_strdup(const char *s
)
1333 new = kmem_alloc(len
+ 1, KM_SLEEP
);
1341 spa_strfree(char *s
)
1343 kmem_free(s
, strlen(s
) + 1);
1347 spa_get_random(uint64_t range
)
1353 (void) random_get_pseudo_bytes((void *)&r
, sizeof (uint64_t));
1359 spa_generate_guid(spa_t
*spa
)
1361 uint64_t guid
= spa_get_random(-1ULL);
1364 while (guid
== 0 || spa_guid_exists(spa_guid(spa
), guid
))
1365 guid
= spa_get_random(-1ULL);
1367 while (guid
== 0 || spa_guid_exists(guid
, 0))
1368 guid
= spa_get_random(-1ULL);
1375 snprintf_blkptr(char *buf
, size_t buflen
, const blkptr_t
*bp
)
1378 char *checksum
= NULL
;
1379 char *compress
= NULL
;
1382 if (BP_GET_TYPE(bp
) & DMU_OT_NEWTYPE
) {
1383 dmu_object_byteswap_t bswap
=
1384 DMU_OT_BYTESWAP(BP_GET_TYPE(bp
));
1385 (void) snprintf(type
, sizeof (type
), "bswap %s %s",
1386 DMU_OT_IS_METADATA(BP_GET_TYPE(bp
)) ?
1387 "metadata" : "data",
1388 dmu_ot_byteswap
[bswap
].ob_name
);
1390 (void) strlcpy(type
, dmu_ot
[BP_GET_TYPE(bp
)].ot_name
,
1393 if (!BP_IS_EMBEDDED(bp
)) {
1395 zio_checksum_table
[BP_GET_CHECKSUM(bp
)].ci_name
;
1397 compress
= zio_compress_table
[BP_GET_COMPRESS(bp
)].ci_name
;
1400 SNPRINTF_BLKPTR(snprintf
, ' ', buf
, buflen
, bp
, type
, checksum
,
1405 spa_freeze(spa_t
*spa
)
1407 uint64_t freeze_txg
= 0;
1409 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1410 if (spa
->spa_freeze_txg
== UINT64_MAX
) {
1411 freeze_txg
= spa_last_synced_txg(spa
) + TXG_SIZE
;
1412 spa
->spa_freeze_txg
= freeze_txg
;
1414 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1415 if (freeze_txg
!= 0)
1416 txg_wait_synced(spa_get_dsl(spa
), freeze_txg
);
1420 zfs_panic_recover(const char *fmt
, ...)
1425 vcmn_err(zfs_recover
? CE_WARN
: CE_PANIC
, fmt
, adx
);
1430 * This is a stripped-down version of strtoull, suitable only for converting
1431 * lowercase hexadecimal numbers that don't overflow.
1434 strtonum(const char *str
, char **nptr
)
1440 while ((c
= *str
) != '\0') {
1441 if (c
>= '0' && c
<= '9')
1443 else if (c
>= 'a' && c
<= 'f')
1444 digit
= 10 + c
- 'a';
1455 *nptr
= (char *)str
;
1461 * ==========================================================================
1462 * Accessor functions
1463 * ==========================================================================
1467 spa_shutting_down(spa_t
*spa
)
1469 return (spa
->spa_async_suspended
);
1473 spa_get_dsl(spa_t
*spa
)
1475 return (spa
->spa_dsl_pool
);
1479 spa_is_initializing(spa_t
*spa
)
1481 return (spa
->spa_is_initializing
);
1485 spa_get_rootblkptr(spa_t
*spa
)
1487 return (&spa
->spa_ubsync
.ub_rootbp
);
1491 spa_set_rootblkptr(spa_t
*spa
, const blkptr_t
*bp
)
1493 spa
->spa_uberblock
.ub_rootbp
= *bp
;
1497 spa_altroot(spa_t
*spa
, char *buf
, size_t buflen
)
1499 if (spa
->spa_root
== NULL
)
1502 (void) strncpy(buf
, spa
->spa_root
, buflen
);
1506 spa_sync_pass(spa_t
*spa
)
1508 return (spa
->spa_sync_pass
);
1512 spa_name(spa_t
*spa
)
1514 return (spa
->spa_name
);
1518 spa_guid(spa_t
*spa
)
1520 dsl_pool_t
*dp
= spa_get_dsl(spa
);
1524 * If we fail to parse the config during spa_load(), we can go through
1525 * the error path (which posts an ereport) and end up here with no root
1526 * vdev. We stash the original pool guid in 'spa_config_guid' to handle
1529 if (spa
->spa_root_vdev
== NULL
)
1530 return (spa
->spa_config_guid
);
1532 guid
= spa
->spa_last_synced_guid
!= 0 ?
1533 spa
->spa_last_synced_guid
: spa
->spa_root_vdev
->vdev_guid
;
1536 * Return the most recently synced out guid unless we're
1537 * in syncing context.
1539 if (dp
&& dsl_pool_sync_context(dp
))
1540 return (spa
->spa_root_vdev
->vdev_guid
);
1546 spa_load_guid(spa_t
*spa
)
1549 * This is a GUID that exists solely as a reference for the
1550 * purposes of the arc. It is generated at load time, and
1551 * is never written to persistent storage.
1553 return (spa
->spa_load_guid
);
1557 spa_last_synced_txg(spa_t
*spa
)
1559 return (spa
->spa_ubsync
.ub_txg
);
1563 spa_first_txg(spa_t
*spa
)
1565 return (spa
->spa_first_txg
);
1569 spa_syncing_txg(spa_t
*spa
)
1571 return (spa
->spa_syncing_txg
);
1575 spa_state(spa_t
*spa
)
1577 return (spa
->spa_state
);
1581 spa_load_state(spa_t
*spa
)
1583 return (spa
->spa_load_state
);
1587 spa_freeze_txg(spa_t
*spa
)
1589 return (spa
->spa_freeze_txg
);
1594 spa_get_asize(spa_t
*spa
, uint64_t lsize
)
1596 return (lsize
* spa_asize_inflation
);
1600 * Return the amount of slop space in bytes. It is 1/32 of the pool (3.2%),
1603 * See the comment above spa_slop_shift for details.
1606 spa_get_slop_space(spa_t
*spa
) {
1607 uint64_t space
= spa_get_dspace(spa
);
1608 return (MAX(space
>> spa_slop_shift
, SPA_MINDEVSIZE
>> 1));
1612 spa_get_dspace(spa_t
*spa
)
1614 return (spa
->spa_dspace
);
1618 spa_update_dspace(spa_t
*spa
)
1620 spa
->spa_dspace
= metaslab_class_get_dspace(spa_normal_class(spa
)) +
1621 ddt_get_dedup_dspace(spa
);
1625 * Return the failure mode that has been set to this pool. The default
1626 * behavior will be to block all I/Os when a complete failure occurs.
1629 spa_get_failmode(spa_t
*spa
)
1631 return (spa
->spa_failmode
);
1635 spa_suspended(spa_t
*spa
)
1637 return (spa
->spa_suspended
);
1641 spa_version(spa_t
*spa
)
1643 return (spa
->spa_ubsync
.ub_version
);
1647 spa_deflate(spa_t
*spa
)
1649 return (spa
->spa_deflate
);
1653 spa_normal_class(spa_t
*spa
)
1655 return (spa
->spa_normal_class
);
1659 spa_log_class(spa_t
*spa
)
1661 return (spa
->spa_log_class
);
1665 spa_evicting_os_register(spa_t
*spa
, objset_t
*os
)
1667 mutex_enter(&spa
->spa_evicting_os_lock
);
1668 list_insert_head(&spa
->spa_evicting_os_list
, os
);
1669 mutex_exit(&spa
->spa_evicting_os_lock
);
1673 spa_evicting_os_deregister(spa_t
*spa
, objset_t
*os
)
1675 mutex_enter(&spa
->spa_evicting_os_lock
);
1676 list_remove(&spa
->spa_evicting_os_list
, os
);
1677 cv_broadcast(&spa
->spa_evicting_os_cv
);
1678 mutex_exit(&spa
->spa_evicting_os_lock
);
1682 spa_evicting_os_wait(spa_t
*spa
)
1684 mutex_enter(&spa
->spa_evicting_os_lock
);
1685 while (!list_is_empty(&spa
->spa_evicting_os_list
))
1686 cv_wait(&spa
->spa_evicting_os_cv
, &spa
->spa_evicting_os_lock
);
1687 mutex_exit(&spa
->spa_evicting_os_lock
);
1689 dmu_buf_user_evict_wait();
1693 spa_max_replication(spa_t
*spa
)
1696 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
1697 * handle BPs with more than one DVA allocated. Set our max
1698 * replication level accordingly.
1700 if (spa_version(spa
) < SPA_VERSION_DITTO_BLOCKS
)
1702 return (MIN(SPA_DVAS_PER_BP
, spa_max_replication_override
));
1706 spa_prev_software_version(spa_t
*spa
)
1708 return (spa
->spa_prev_software_version
);
1712 spa_deadman_synctime(spa_t
*spa
)
1714 return (spa
->spa_deadman_synctime
);
1718 dva_get_dsize_sync(spa_t
*spa
, const dva_t
*dva
)
1720 uint64_t asize
= DVA_GET_ASIZE(dva
);
1721 uint64_t dsize
= asize
;
1723 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_READER
) != 0);
1725 if (asize
!= 0 && spa
->spa_deflate
) {
1726 vdev_t
*vd
= vdev_lookup_top(spa
, DVA_GET_VDEV(dva
));
1728 dsize
= (asize
>> SPA_MINBLOCKSHIFT
) *
1729 vd
->vdev_deflate_ratio
;
1736 bp_get_dsize_sync(spa_t
*spa
, const blkptr_t
*bp
)
1741 for (d
= 0; d
< BP_GET_NDVAS(bp
); d
++)
1742 dsize
+= dva_get_dsize_sync(spa
, &bp
->blk_dva
[d
]);
1748 bp_get_dsize(spa_t
*spa
, const blkptr_t
*bp
)
1753 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
1755 for (d
= 0; d
< BP_GET_NDVAS(bp
); d
++)
1756 dsize
+= dva_get_dsize_sync(spa
, &bp
->blk_dva
[d
]);
1758 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
1764 * ==========================================================================
1765 * Initialization and Termination
1766 * ==========================================================================
1770 spa_name_compare(const void *a1
, const void *a2
)
1772 const spa_t
*s1
= a1
;
1773 const spa_t
*s2
= a2
;
1776 s
= strcmp(s1
->spa_name
, s2
->spa_name
);
1793 mutex_init(&spa_namespace_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1794 mutex_init(&spa_spare_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1795 mutex_init(&spa_l2cache_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1796 cv_init(&spa_namespace_cv
, NULL
, CV_DEFAULT
, NULL
);
1798 avl_create(&spa_namespace_avl
, spa_name_compare
, sizeof (spa_t
),
1799 offsetof(spa_t
, spa_avl
));
1801 avl_create(&spa_spare_avl
, spa_spare_compare
, sizeof (spa_aux_t
),
1802 offsetof(spa_aux_t
, aux_avl
));
1804 avl_create(&spa_l2cache_avl
, spa_l2cache_compare
, sizeof (spa_aux_t
),
1805 offsetof(spa_aux_t
, aux_avl
));
1807 spa_mode_global
= mode
;
1810 if (spa_mode_global
!= FREAD
&& dprintf_find_string("watch")) {
1811 struct sigaction sa
;
1813 sa
.sa_flags
= SA_SIGINFO
;
1814 sigemptyset(&sa
.sa_mask
);
1815 sa
.sa_sigaction
= arc_buf_sigsegv
;
1817 if (sigaction(SIGSEGV
, &sa
, NULL
) == -1) {
1818 perror("could not enable watchpoints: "
1819 "sigaction(SIGSEGV, ...) = ");
1834 vdev_cache_stat_init();
1835 vdev_raidz_math_init();
1838 zpool_feature_init();
1850 vdev_cache_stat_fini();
1851 vdev_raidz_math_fini();
1861 avl_destroy(&spa_namespace_avl
);
1862 avl_destroy(&spa_spare_avl
);
1863 avl_destroy(&spa_l2cache_avl
);
1865 cv_destroy(&spa_namespace_cv
);
1866 mutex_destroy(&spa_namespace_lock
);
1867 mutex_destroy(&spa_spare_lock
);
1868 mutex_destroy(&spa_l2cache_lock
);
1872 * Return whether this pool has slogs. No locking needed.
1873 * It's not a problem if the wrong answer is returned as it's only for
1874 * performance and not correctness
1877 spa_has_slogs(spa_t
*spa
)
1879 return (spa
->spa_log_class
->mc_rotor
!= NULL
);
1883 spa_get_log_state(spa_t
*spa
)
1885 return (spa
->spa_log_state
);
1889 spa_set_log_state(spa_t
*spa
, spa_log_state_t state
)
1891 spa
->spa_log_state
= state
;
1895 spa_is_root(spa_t
*spa
)
1897 return (spa
->spa_is_root
);
1901 spa_writeable(spa_t
*spa
)
1903 return (!!(spa
->spa_mode
& FWRITE
));
1907 * Returns true if there is a pending sync task in any of the current
1908 * syncing txg, the current quiescing txg, or the current open txg.
1911 spa_has_pending_synctask(spa_t
*spa
)
1913 return (!txg_all_lists_empty(&spa
->spa_dsl_pool
->dp_sync_tasks
));
1917 spa_mode(spa_t
*spa
)
1919 return (spa
->spa_mode
);
1923 spa_bootfs(spa_t
*spa
)
1925 return (spa
->spa_bootfs
);
1929 spa_delegation(spa_t
*spa
)
1931 return (spa
->spa_delegation
);
1935 spa_meta_objset(spa_t
*spa
)
1937 return (spa
->spa_meta_objset
);
1941 spa_dedup_checksum(spa_t
*spa
)
1943 return (spa
->spa_dedup_checksum
);
1947 * Reset pool scan stat per scan pass (or reboot).
1950 spa_scan_stat_init(spa_t
*spa
)
1952 /* data not stored on disk */
1953 spa
->spa_scan_pass_start
= gethrestime_sec();
1954 spa
->spa_scan_pass_exam
= 0;
1955 vdev_scan_stat_init(spa
->spa_root_vdev
);
1959 * Get scan stats for zpool status reports
1962 spa_scan_get_stats(spa_t
*spa
, pool_scan_stat_t
*ps
)
1964 dsl_scan_t
*scn
= spa
->spa_dsl_pool
? spa
->spa_dsl_pool
->dp_scan
: NULL
;
1966 if (scn
== NULL
|| scn
->scn_phys
.scn_func
== POOL_SCAN_NONE
)
1967 return (SET_ERROR(ENOENT
));
1968 bzero(ps
, sizeof (pool_scan_stat_t
));
1970 /* data stored on disk */
1971 ps
->pss_func
= scn
->scn_phys
.scn_func
;
1972 ps
->pss_start_time
= scn
->scn_phys
.scn_start_time
;
1973 ps
->pss_end_time
= scn
->scn_phys
.scn_end_time
;
1974 ps
->pss_to_examine
= scn
->scn_phys
.scn_to_examine
;
1975 ps
->pss_examined
= scn
->scn_phys
.scn_examined
;
1976 ps
->pss_to_process
= scn
->scn_phys
.scn_to_process
;
1977 ps
->pss_processed
= scn
->scn_phys
.scn_processed
;
1978 ps
->pss_errors
= scn
->scn_phys
.scn_errors
;
1979 ps
->pss_state
= scn
->scn_phys
.scn_state
;
1981 /* data not stored on disk */
1982 ps
->pss_pass_start
= spa
->spa_scan_pass_start
;
1983 ps
->pss_pass_exam
= spa
->spa_scan_pass_exam
;
1989 spa_debug_enabled(spa_t
*spa
)
1991 return (spa
->spa_debug
);
1995 spa_maxblocksize(spa_t
*spa
)
1997 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_BLOCKS
))
1998 return (SPA_MAXBLOCKSIZE
);
2000 return (SPA_OLD_MAXBLOCKSIZE
);
2004 spa_maxdnodesize(spa_t
*spa
)
2006 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_DNODE
))
2007 return (DNODE_MAX_SIZE
);
2009 return (DNODE_MIN_SIZE
);
2012 #if defined(_KERNEL) && defined(HAVE_SPL)
2013 /* Namespace manipulation */
2014 EXPORT_SYMBOL(spa_lookup
);
2015 EXPORT_SYMBOL(spa_add
);
2016 EXPORT_SYMBOL(spa_remove
);
2017 EXPORT_SYMBOL(spa_next
);
2019 /* Refcount functions */
2020 EXPORT_SYMBOL(spa_open_ref
);
2021 EXPORT_SYMBOL(spa_close
);
2022 EXPORT_SYMBOL(spa_refcount_zero
);
2024 /* Pool configuration lock */
2025 EXPORT_SYMBOL(spa_config_tryenter
);
2026 EXPORT_SYMBOL(spa_config_enter
);
2027 EXPORT_SYMBOL(spa_config_exit
);
2028 EXPORT_SYMBOL(spa_config_held
);
2030 /* Pool vdev add/remove lock */
2031 EXPORT_SYMBOL(spa_vdev_enter
);
2032 EXPORT_SYMBOL(spa_vdev_exit
);
2034 /* Pool vdev state change lock */
2035 EXPORT_SYMBOL(spa_vdev_state_enter
);
2036 EXPORT_SYMBOL(spa_vdev_state_exit
);
2038 /* Accessor functions */
2039 EXPORT_SYMBOL(spa_shutting_down
);
2040 EXPORT_SYMBOL(spa_get_dsl
);
2041 EXPORT_SYMBOL(spa_get_rootblkptr
);
2042 EXPORT_SYMBOL(spa_set_rootblkptr
);
2043 EXPORT_SYMBOL(spa_altroot
);
2044 EXPORT_SYMBOL(spa_sync_pass
);
2045 EXPORT_SYMBOL(spa_name
);
2046 EXPORT_SYMBOL(spa_guid
);
2047 EXPORT_SYMBOL(spa_last_synced_txg
);
2048 EXPORT_SYMBOL(spa_first_txg
);
2049 EXPORT_SYMBOL(spa_syncing_txg
);
2050 EXPORT_SYMBOL(spa_version
);
2051 EXPORT_SYMBOL(spa_state
);
2052 EXPORT_SYMBOL(spa_load_state
);
2053 EXPORT_SYMBOL(spa_freeze_txg
);
2054 EXPORT_SYMBOL(spa_get_asize
);
2055 EXPORT_SYMBOL(spa_get_dspace
);
2056 EXPORT_SYMBOL(spa_update_dspace
);
2057 EXPORT_SYMBOL(spa_deflate
);
2058 EXPORT_SYMBOL(spa_normal_class
);
2059 EXPORT_SYMBOL(spa_log_class
);
2060 EXPORT_SYMBOL(spa_max_replication
);
2061 EXPORT_SYMBOL(spa_prev_software_version
);
2062 EXPORT_SYMBOL(spa_get_failmode
);
2063 EXPORT_SYMBOL(spa_suspended
);
2064 EXPORT_SYMBOL(spa_bootfs
);
2065 EXPORT_SYMBOL(spa_delegation
);
2066 EXPORT_SYMBOL(spa_meta_objset
);
2067 EXPORT_SYMBOL(spa_maxblocksize
);
2068 EXPORT_SYMBOL(spa_maxdnodesize
);
2070 /* Miscellaneous support routines */
2071 EXPORT_SYMBOL(spa_rename
);
2072 EXPORT_SYMBOL(spa_guid_exists
);
2073 EXPORT_SYMBOL(spa_strdup
);
2074 EXPORT_SYMBOL(spa_strfree
);
2075 EXPORT_SYMBOL(spa_get_random
);
2076 EXPORT_SYMBOL(spa_generate_guid
);
2077 EXPORT_SYMBOL(snprintf_blkptr
);
2078 EXPORT_SYMBOL(spa_freeze
);
2079 EXPORT_SYMBOL(spa_upgrade
);
2080 EXPORT_SYMBOL(spa_evict_all
);
2081 EXPORT_SYMBOL(spa_lookup_by_guid
);
2082 EXPORT_SYMBOL(spa_has_spare
);
2083 EXPORT_SYMBOL(dva_get_dsize_sync
);
2084 EXPORT_SYMBOL(bp_get_dsize_sync
);
2085 EXPORT_SYMBOL(bp_get_dsize
);
2086 EXPORT_SYMBOL(spa_has_slogs
);
2087 EXPORT_SYMBOL(spa_is_root
);
2088 EXPORT_SYMBOL(spa_writeable
);
2089 EXPORT_SYMBOL(spa_mode
);
2091 EXPORT_SYMBOL(spa_namespace_lock
);
2093 module_param(zfs_flags
, uint
, 0644);
2094 MODULE_PARM_DESC(zfs_flags
, "Set additional debugging flags");
2096 module_param(zfs_recover
, int, 0644);
2097 MODULE_PARM_DESC(zfs_recover
, "Set to attempt to recover from fatal errors");
2099 module_param(zfs_free_leak_on_eio
, int, 0644);
2100 MODULE_PARM_DESC(zfs_free_leak_on_eio
,
2101 "Set to ignore IO errors during free and permanently leak the space");
2103 module_param(zfs_deadman_synctime_ms
, ulong
, 0644);
2104 MODULE_PARM_DESC(zfs_deadman_synctime_ms
, "Expiration time in milliseconds");
2106 module_param(zfs_deadman_enabled
, int, 0644);
2107 MODULE_PARM_DESC(zfs_deadman_enabled
, "Enable deadman timer");
2109 module_param(spa_asize_inflation
, int, 0644);
2110 MODULE_PARM_DESC(spa_asize_inflation
,
2111 "SPA size estimate multiplication factor");
2113 module_param(spa_slop_shift
, int, 0644);
2114 MODULE_PARM_DESC(spa_slop_shift
, "Reserved free space in pool");