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>
29 #include <sys/spa_boot.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/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/dsl_scan.h>
47 #include <sys/fs/zfs.h>
48 #include <sys/metaslab_impl.h>
52 #include "zfeature_common.h"
57 * There are four basic locks for managing spa_t structures:
59 * spa_namespace_lock (global mutex)
61 * This lock must be acquired to do any of the following:
63 * - Lookup a spa_t by name
64 * - Add or remove a spa_t from the namespace
65 * - Increase spa_refcount from non-zero
66 * - Check if spa_refcount is zero
68 * - add/remove/attach/detach devices
69 * - Held for the duration of create/destroy/import/export
71 * It does not need to handle recursion. A create or destroy may
72 * reference objects (files or zvols) in other pools, but by
73 * definition they must have an existing reference, and will never need
74 * to lookup a spa_t by name.
76 * spa_refcount (per-spa refcount_t protected by mutex)
78 * This reference count keep track of any active users of the spa_t. The
79 * spa_t cannot be destroyed or freed while this is non-zero. Internally,
80 * the refcount is never really 'zero' - opening a pool implicitly keeps
81 * some references in the DMU. Internally we check against spa_minref, but
82 * present the image of a zero/non-zero value to consumers.
84 * spa_config_lock[] (per-spa array of rwlocks)
86 * This protects the spa_t from config changes, and must be held in
87 * the following circumstances:
89 * - RW_READER to perform I/O to the spa
90 * - RW_WRITER to change the vdev config
92 * The locking order is fairly straightforward:
94 * spa_namespace_lock -> spa_refcount
96 * The namespace lock must be acquired to increase the refcount from 0
97 * or to check if it is zero.
99 * spa_refcount -> spa_config_lock[]
101 * There must be at least one valid reference on the spa_t to acquire
104 * spa_namespace_lock -> spa_config_lock[]
106 * The namespace lock must always be taken before the config lock.
109 * The spa_namespace_lock can be acquired directly and is globally visible.
111 * The namespace is manipulated using the following functions, all of which
112 * require the spa_namespace_lock to be held.
114 * spa_lookup() Lookup a spa_t by name.
116 * spa_add() Create a new spa_t in the namespace.
118 * spa_remove() Remove a spa_t from the namespace. This also
119 * frees up any memory associated with the spa_t.
121 * spa_next() Returns the next spa_t in the system, or the
122 * first if NULL is passed.
124 * spa_evict_all() Shutdown and remove all spa_t structures in
127 * spa_guid_exists() Determine whether a pool/device guid exists.
129 * The spa_refcount is manipulated using the following functions:
131 * spa_open_ref() Adds a reference to the given spa_t. Must be
132 * called with spa_namespace_lock held if the
133 * refcount is currently zero.
135 * spa_close() Remove a reference from the spa_t. This will
136 * not free the spa_t or remove it from the
137 * namespace. No locking is required.
139 * spa_refcount_zero() Returns true if the refcount is currently
140 * zero. Must be called with spa_namespace_lock
143 * The spa_config_lock[] is an array of rwlocks, ordered as follows:
144 * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV.
145 * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}().
147 * To read the configuration, it suffices to hold one of these locks as reader.
148 * To modify the configuration, you must hold all locks as writer. To modify
149 * vdev state without altering the vdev tree's topology (e.g. online/offline),
150 * you must hold SCL_STATE and SCL_ZIO as writer.
152 * We use these distinct config locks to avoid recursive lock entry.
153 * For example, spa_sync() (which holds SCL_CONFIG as reader) induces
154 * block allocations (SCL_ALLOC), which may require reading space maps
155 * from disk (dmu_read() -> zio_read() -> SCL_ZIO).
157 * The spa config locks cannot be normal rwlocks because we need the
158 * ability to hand off ownership. For example, SCL_ZIO is acquired
159 * by the issuing thread and later released by an interrupt thread.
160 * They do, however, obey the usual write-wanted semantics to prevent
161 * writer (i.e. system administrator) starvation.
163 * The lock acquisition rules are as follows:
166 * Protects changes to the vdev tree topology, such as vdev
167 * add/remove/attach/detach. Protects the dirty config list
168 * (spa_config_dirty_list) and the set of spares and l2arc devices.
171 * Protects changes to pool state and vdev state, such as vdev
172 * online/offline/fault/degrade/clear. Protects the dirty state list
173 * (spa_state_dirty_list) and global pool state (spa_state).
176 * Protects changes to metaslab groups and classes.
177 * Held as reader by metaslab_alloc() and metaslab_claim().
180 * Held by bp-level zios (those which have no io_vd upon entry)
181 * to prevent changes to the vdev tree. The bp-level zio implicitly
182 * protects all of its vdev child zios, which do not hold SCL_ZIO.
185 * Protects changes to metaslab groups and classes.
186 * Held as reader by metaslab_free(). SCL_FREE is distinct from
187 * SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free
188 * blocks in zio_done() while another i/o that holds either
189 * SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete.
192 * Held as reader to prevent changes to the vdev tree during trivial
193 * inquiries such as bp_get_dsize(). SCL_VDEV is distinct from the
194 * other locks, and lower than all of them, to ensure that it's safe
195 * to acquire regardless of caller context.
197 * In addition, the following rules apply:
199 * (a) spa_props_lock protects pool properties, spa_config and spa_config_list.
200 * The lock ordering is SCL_CONFIG > spa_props_lock.
202 * (b) I/O operations on leaf vdevs. For any zio operation that takes
203 * an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(),
204 * or zio_write_phys() -- the caller must ensure that the config cannot
205 * cannot change in the interim, and that the vdev cannot be reopened.
206 * SCL_STATE as reader suffices for both.
208 * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
210 * spa_vdev_enter() Acquire the namespace lock and the config lock
213 * spa_vdev_exit() Release the config lock, wait for all I/O
214 * to complete, sync the updated configs to the
215 * cache, and release the namespace lock.
217 * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit().
218 * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual
219 * locking is, always, based on spa_namespace_lock and spa_config_lock[].
221 * spa_rename() is also implemented within this file since it requires
222 * manipulation of the namespace.
225 static avl_tree_t spa_namespace_avl
;
226 kmutex_t spa_namespace_lock
;
227 static kcondvar_t spa_namespace_cv
;
228 static int spa_active_count
;
229 int spa_max_replication_override
= SPA_DVAS_PER_BP
;
231 static kmutex_t spa_spare_lock
;
232 static avl_tree_t spa_spare_avl
;
233 static kmutex_t spa_l2cache_lock
;
234 static avl_tree_t spa_l2cache_avl
;
236 kmem_cache_t
*spa_buffer_pool
;
240 /* Everything except dprintf and spa is on by default in debug builds */
241 int zfs_flags
= ~(ZFS_DEBUG_DPRINTF
| ZFS_DEBUG_SPA
);
247 * zfs_recover can be set to nonzero to attempt to recover from
248 * otherwise-fatal errors, typically caused by on-disk corruption. When
249 * set, calls to zfs_panic_recover() will turn into warning messages.
250 * This should only be used as a last resort, as it typically results
251 * in leaked space, or worse.
253 boolean_t zfs_recover
= B_FALSE
;
256 * If destroy encounters an EIO while reading metadata (e.g. indirect
257 * blocks), space referenced by the missing metadata can not be freed.
258 * Normally this causes the background destroy to become "stalled", as
259 * it is unable to make forward progress. While in this stalled state,
260 * all remaining space to free from the error-encountering filesystem is
261 * "temporarily leaked". Set this flag to cause it to ignore the EIO,
262 * permanently leak the space from indirect blocks that can not be read,
263 * and continue to free everything else that it can.
265 * The default, "stalling" behavior is useful if the storage partially
266 * fails (i.e. some but not all i/os fail), and then later recovers. In
267 * this case, we will be able to continue pool operations while it is
268 * partially failed, and when it recovers, we can continue to free the
269 * space, with no leaks. However, note that this case is actually
272 * Typically pools either (a) fail completely (but perhaps temporarily,
273 * e.g. a top-level vdev going offline), or (b) have localized,
274 * permanent errors (e.g. disk returns the wrong data due to bit flip or
275 * firmware bug). In case (a), this setting does not matter because the
276 * pool will be suspended and the sync thread will not be able to make
277 * forward progress regardless. In case (b), because the error is
278 * permanent, the best we can do is leak the minimum amount of space,
279 * which is what setting this flag will do. Therefore, it is reasonable
280 * for this flag to normally be set, but we chose the more conservative
281 * approach of not setting it, so that there is no possibility of
282 * leaking space in the "partial temporary" failure case.
284 boolean_t zfs_free_leak_on_eio
= B_FALSE
;
287 * Expiration time in milliseconds. This value has two meanings. First it is
288 * used to determine when the spa_deadman() logic should fire. By default the
289 * spa_deadman() will fire if spa_sync() has not completed in 1000 seconds.
290 * Secondly, the value determines if an I/O is considered "hung". Any I/O that
291 * has not completed in zfs_deadman_synctime_ms is considered "hung" resulting
294 uint64_t zfs_deadman_synctime_ms
= 1000000ULL;
297 * Check time in milliseconds. This defines the frequency at which we check
300 uint64_t zfs_deadman_checktime_ms
= 5000ULL;
303 * Override the zfs deadman behavior via /etc/system. By default the
304 * deadman is enabled except on VMware and sparc deployments.
306 int zfs_deadman_enabled
= -1;
309 * The worst case is single-sector max-parity RAID-Z blocks, in which
310 * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1)
311 * times the size; so just assume that. Add to this the fact that
312 * we can have up to 3 DVAs per bp, and one more factor of 2 because
313 * the block may be dittoed with up to 3 DVAs by ddt_sync(). All together,
315 * (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2 == 24
317 int spa_asize_inflation
= 24;
320 * Normally, we don't allow the last 3.2% (1/(2^spa_slop_shift)) of space in
321 * the pool to be consumed. This ensures that we don't run the pool
322 * completely out of space, due to unaccounted changes (e.g. to the MOS).
323 * It also limits the worst-case time to allocate space. If we have
324 * less than this amount of free space, most ZPL operations (e.g. write,
325 * create) will return ENOSPC.
327 * Certain operations (e.g. file removal, most administrative actions) can
328 * use half the slop space. They will only return ENOSPC if less than half
329 * the slop space is free. Typically, once the pool has less than the slop
330 * space free, the user will use these operations to free up space in the pool.
331 * These are the operations that call dsl_pool_adjustedsize() with the netfree
332 * argument set to TRUE.
334 * A very restricted set of operations are always permitted, regardless of
335 * the amount of free space. These are the operations that call
336 * dsl_sync_task(ZFS_SPACE_CHECK_NONE), e.g. "zfs destroy". If these
337 * operations result in a net increase in the amount of space used,
338 * it is possible to run the pool completely out of space, causing it to
339 * be permanently read-only.
341 * See also the comments in zfs_space_check_t.
343 int spa_slop_shift
= 5;
346 * ==========================================================================
348 * ==========================================================================
351 spa_config_lock_init(spa_t
*spa
)
353 for (int i
= 0; i
< SCL_LOCKS
; i
++) {
354 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
355 mutex_init(&scl
->scl_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
356 cv_init(&scl
->scl_cv
, NULL
, CV_DEFAULT
, NULL
);
357 refcount_create_untracked(&scl
->scl_count
);
358 scl
->scl_writer
= NULL
;
359 scl
->scl_write_wanted
= 0;
364 spa_config_lock_destroy(spa_t
*spa
)
366 for (int 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
)
379 for (int i
= 0; i
< SCL_LOCKS
; i
++) {
380 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
381 if (!(locks
& (1 << i
)))
383 mutex_enter(&scl
->scl_lock
);
384 if (rw
== RW_READER
) {
385 if (scl
->scl_writer
|| scl
->scl_write_wanted
) {
386 mutex_exit(&scl
->scl_lock
);
387 spa_config_exit(spa
, locks
^ (1 << i
), tag
);
391 ASSERT(scl
->scl_writer
!= curthread
);
392 if (!refcount_is_zero(&scl
->scl_count
)) {
393 mutex_exit(&scl
->scl_lock
);
394 spa_config_exit(spa
, locks
^ (1 << i
), tag
);
397 scl
->scl_writer
= curthread
;
399 (void) refcount_add(&scl
->scl_count
, tag
);
400 mutex_exit(&scl
->scl_lock
);
406 spa_config_enter(spa_t
*spa
, int locks
, void *tag
, krw_t rw
)
410 ASSERT3U(SCL_LOCKS
, <, sizeof (wlocks_held
) * NBBY
);
412 for (int i
= 0; i
< SCL_LOCKS
; i
++) {
413 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
414 if (scl
->scl_writer
== curthread
)
415 wlocks_held
|= (1 << i
);
416 if (!(locks
& (1 << i
)))
418 mutex_enter(&scl
->scl_lock
);
419 if (rw
== RW_READER
) {
420 while (scl
->scl_writer
|| scl
->scl_write_wanted
) {
421 cv_wait(&scl
->scl_cv
, &scl
->scl_lock
);
424 ASSERT(scl
->scl_writer
!= curthread
);
425 while (!refcount_is_zero(&scl
->scl_count
)) {
426 scl
->scl_write_wanted
++;
427 cv_wait(&scl
->scl_cv
, &scl
->scl_lock
);
428 scl
->scl_write_wanted
--;
430 scl
->scl_writer
= curthread
;
432 (void) refcount_add(&scl
->scl_count
, tag
);
433 mutex_exit(&scl
->scl_lock
);
435 ASSERT(wlocks_held
<= locks
);
439 spa_config_exit(spa_t
*spa
, int locks
, void *tag
)
441 for (int i
= SCL_LOCKS
- 1; i
>= 0; i
--) {
442 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
443 if (!(locks
& (1 << i
)))
445 mutex_enter(&scl
->scl_lock
);
446 ASSERT(!refcount_is_zero(&scl
->scl_count
));
447 if (refcount_remove(&scl
->scl_count
, tag
) == 0) {
448 ASSERT(scl
->scl_writer
== NULL
||
449 scl
->scl_writer
== curthread
);
450 scl
->scl_writer
= NULL
; /* OK in either case */
451 cv_broadcast(&scl
->scl_cv
);
453 mutex_exit(&scl
->scl_lock
);
458 spa_config_held(spa_t
*spa
, int locks
, krw_t rw
)
462 for (int i
= 0; i
< SCL_LOCKS
; i
++) {
463 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
464 if (!(locks
& (1 << i
)))
466 if ((rw
== RW_READER
&& !refcount_is_zero(&scl
->scl_count
)) ||
467 (rw
== RW_WRITER
&& scl
->scl_writer
== curthread
))
468 locks_held
|= 1 << i
;
475 * ==========================================================================
476 * SPA namespace functions
477 * ==========================================================================
481 * Lookup the named spa_t in the AVL tree. The spa_namespace_lock must be held.
482 * Returns NULL if no matching spa_t is found.
485 spa_lookup(const char *name
)
487 static spa_t search
; /* spa_t is large; don't allocate on stack */
492 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
494 (void) strlcpy(search
.spa_name
, name
, sizeof (search
.spa_name
));
497 * If it's a full dataset name, figure out the pool name and
500 cp
= strpbrk(search
.spa_name
, "/@#");
504 spa
= avl_find(&spa_namespace_avl
, &search
, &where
);
510 * Fires when spa_sync has not completed within zfs_deadman_synctime_ms.
511 * If the zfs_deadman_enabled flag is set then it inspects all vdev queues
512 * looking for potentially hung I/Os.
515 spa_deadman(void *arg
)
520 * Disable the deadman timer if the pool is suspended.
522 if (spa_suspended(spa
)) {
523 VERIFY(cyclic_reprogram(spa
->spa_deadman_cycid
, CY_INFINITY
));
527 zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu",
528 (gethrtime() - spa
->spa_sync_starttime
) / NANOSEC
,
529 ++spa
->spa_deadman_calls
);
530 if (zfs_deadman_enabled
)
531 vdev_deadman(spa
->spa_root_vdev
);
535 * Create an uninitialized spa_t with the given name. Requires
536 * spa_namespace_lock. The caller must ensure that the spa_t doesn't already
537 * exist by calling spa_lookup() first.
540 spa_add(const char *name
, nvlist_t
*config
, const char *altroot
)
543 spa_config_dirent_t
*dp
;
547 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
549 spa
= kmem_zalloc(sizeof (spa_t
), KM_SLEEP
);
551 mutex_init(&spa
->spa_async_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
552 mutex_init(&spa
->spa_errlist_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
553 mutex_init(&spa
->spa_errlog_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
554 mutex_init(&spa
->spa_history_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
555 mutex_init(&spa
->spa_proc_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
556 mutex_init(&spa
->spa_props_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
557 mutex_init(&spa
->spa_scrub_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
558 mutex_init(&spa
->spa_suspend_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
559 mutex_init(&spa
->spa_vdev_top_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
560 mutex_init(&spa
->spa_iokstat_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
562 cv_init(&spa
->spa_async_cv
, NULL
, CV_DEFAULT
, NULL
);
563 cv_init(&spa
->spa_proc_cv
, NULL
, CV_DEFAULT
, NULL
);
564 cv_init(&spa
->spa_scrub_io_cv
, NULL
, CV_DEFAULT
, NULL
);
565 cv_init(&spa
->spa_suspend_cv
, NULL
, CV_DEFAULT
, NULL
);
567 for (int t
= 0; t
< TXG_SIZE
; t
++)
568 bplist_create(&spa
->spa_free_bplist
[t
]);
570 (void) strlcpy(spa
->spa_name
, name
, sizeof (spa
->spa_name
));
571 spa
->spa_state
= POOL_STATE_UNINITIALIZED
;
572 spa
->spa_freeze_txg
= UINT64_MAX
;
573 spa
->spa_final_txg
= UINT64_MAX
;
574 spa
->spa_load_max_txg
= UINT64_MAX
;
576 spa
->spa_proc_state
= SPA_PROC_NONE
;
578 hdlr
.cyh_func
= spa_deadman
;
580 hdlr
.cyh_level
= CY_LOW_LEVEL
;
582 spa
->spa_deadman_synctime
= MSEC2NSEC(zfs_deadman_synctime_ms
);
585 * This determines how often we need to check for hung I/Os after
586 * the cyclic has already fired. Since checking for hung I/Os is
587 * an expensive operation we don't want to check too frequently.
588 * Instead wait for 5 seconds before checking again.
590 when
.cyt_interval
= MSEC2NSEC(zfs_deadman_checktime_ms
);
591 when
.cyt_when
= CY_INFINITY
;
592 mutex_enter(&cpu_lock
);
593 spa
->spa_deadman_cycid
= cyclic_add(&hdlr
, &when
);
594 mutex_exit(&cpu_lock
);
596 refcount_create(&spa
->spa_refcount
);
597 spa_config_lock_init(spa
);
599 avl_add(&spa_namespace_avl
, spa
);
602 * Set the alternate root, if there is one.
605 spa
->spa_root
= spa_strdup(altroot
);
610 * Every pool starts with the default cachefile
612 list_create(&spa
->spa_config_list
, sizeof (spa_config_dirent_t
),
613 offsetof(spa_config_dirent_t
, scd_link
));
615 dp
= kmem_zalloc(sizeof (spa_config_dirent_t
), KM_SLEEP
);
616 dp
->scd_path
= altroot
? NULL
: spa_strdup(spa_config_path
);
617 list_insert_head(&spa
->spa_config_list
, dp
);
619 VERIFY(nvlist_alloc(&spa
->spa_load_info
, NV_UNIQUE_NAME
,
622 if (config
!= NULL
) {
625 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_FEATURES_FOR_READ
,
627 VERIFY(nvlist_dup(features
, &spa
->spa_label_features
,
631 VERIFY(nvlist_dup(config
, &spa
->spa_config
, 0) == 0);
634 if (spa
->spa_label_features
== NULL
) {
635 VERIFY(nvlist_alloc(&spa
->spa_label_features
, NV_UNIQUE_NAME
,
639 spa
->spa_iokstat
= kstat_create("zfs", 0, name
,
640 "disk", KSTAT_TYPE_IO
, 1, 0);
641 if (spa
->spa_iokstat
) {
642 spa
->spa_iokstat
->ks_lock
= &spa
->spa_iokstat_lock
;
643 kstat_install(spa
->spa_iokstat
);
646 spa
->spa_debug
= ((zfs_flags
& ZFS_DEBUG_SPA
) != 0);
649 * As a pool is being created, treat all features as disabled by
650 * setting SPA_FEATURE_DISABLED for all entries in the feature
653 for (int i
= 0; i
< SPA_FEATURES
; i
++) {
654 spa
->spa_feat_refcount_cache
[i
] = SPA_FEATURE_DISABLED
;
661 * Removes a spa_t from the namespace, freeing up any memory used. Requires
662 * spa_namespace_lock. This is called only after the spa_t has been closed and
666 spa_remove(spa_t
*spa
)
668 spa_config_dirent_t
*dp
;
670 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
671 ASSERT(spa
->spa_state
== POOL_STATE_UNINITIALIZED
);
673 nvlist_free(spa
->spa_config_splitting
);
675 avl_remove(&spa_namespace_avl
, spa
);
676 cv_broadcast(&spa_namespace_cv
);
679 spa_strfree(spa
->spa_root
);
683 while ((dp
= list_head(&spa
->spa_config_list
)) != NULL
) {
684 list_remove(&spa
->spa_config_list
, dp
);
685 if (dp
->scd_path
!= NULL
)
686 spa_strfree(dp
->scd_path
);
687 kmem_free(dp
, sizeof (spa_config_dirent_t
));
690 list_destroy(&spa
->spa_config_list
);
692 nvlist_free(spa
->spa_label_features
);
693 nvlist_free(spa
->spa_load_info
);
694 spa_config_set(spa
, NULL
);
696 mutex_enter(&cpu_lock
);
697 if (spa
->spa_deadman_cycid
!= CYCLIC_NONE
)
698 cyclic_remove(spa
->spa_deadman_cycid
);
699 mutex_exit(&cpu_lock
);
700 spa
->spa_deadman_cycid
= CYCLIC_NONE
;
702 refcount_destroy(&spa
->spa_refcount
);
704 spa_config_lock_destroy(spa
);
706 kstat_delete(spa
->spa_iokstat
);
707 spa
->spa_iokstat
= NULL
;
709 for (int t
= 0; t
< TXG_SIZE
; t
++)
710 bplist_destroy(&spa
->spa_free_bplist
[t
]);
712 cv_destroy(&spa
->spa_async_cv
);
713 cv_destroy(&spa
->spa_proc_cv
);
714 cv_destroy(&spa
->spa_scrub_io_cv
);
715 cv_destroy(&spa
->spa_suspend_cv
);
717 mutex_destroy(&spa
->spa_async_lock
);
718 mutex_destroy(&spa
->spa_errlist_lock
);
719 mutex_destroy(&spa
->spa_errlog_lock
);
720 mutex_destroy(&spa
->spa_history_lock
);
721 mutex_destroy(&spa
->spa_proc_lock
);
722 mutex_destroy(&spa
->spa_props_lock
);
723 mutex_destroy(&spa
->spa_scrub_lock
);
724 mutex_destroy(&spa
->spa_suspend_lock
);
725 mutex_destroy(&spa
->spa_vdev_top_lock
);
726 mutex_destroy(&spa
->spa_iokstat_lock
);
728 kmem_free(spa
, sizeof (spa_t
));
732 * Given a pool, return the next pool in the namespace, or NULL if there is
733 * none. If 'prev' is NULL, return the first pool.
736 spa_next(spa_t
*prev
)
738 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
741 return (AVL_NEXT(&spa_namespace_avl
, prev
));
743 return (avl_first(&spa_namespace_avl
));
747 * ==========================================================================
748 * SPA refcount functions
749 * ==========================================================================
753 * Add a reference to the given spa_t. Must have at least one reference, or
754 * have the namespace lock held.
757 spa_open_ref(spa_t
*spa
, void *tag
)
759 ASSERT(refcount_count(&spa
->spa_refcount
) >= spa
->spa_minref
||
760 MUTEX_HELD(&spa_namespace_lock
));
761 (void) refcount_add(&spa
->spa_refcount
, tag
);
765 * Remove a reference to the given spa_t. Must have at least one reference, or
766 * have the namespace lock held.
769 spa_close(spa_t
*spa
, void *tag
)
771 ASSERT(refcount_count(&spa
->spa_refcount
) > spa
->spa_minref
||
772 MUTEX_HELD(&spa_namespace_lock
));
773 (void) refcount_remove(&spa
->spa_refcount
, tag
);
777 * Check to see if the spa refcount is zero. Must be called with
778 * spa_namespace_lock held. We really compare against spa_minref, which is the
779 * number of references acquired when opening a pool
782 spa_refcount_zero(spa_t
*spa
)
784 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
786 return (refcount_count(&spa
->spa_refcount
) == spa
->spa_minref
);
790 * ==========================================================================
791 * SPA spare and l2cache tracking
792 * ==========================================================================
796 * Hot spares and cache devices are tracked using the same code below,
797 * for 'auxiliary' devices.
800 typedef struct spa_aux
{
808 spa_aux_compare(const void *a
, const void *b
)
810 const spa_aux_t
*sa
= a
;
811 const spa_aux_t
*sb
= b
;
813 if (sa
->aux_guid
< sb
->aux_guid
)
815 else if (sa
->aux_guid
> sb
->aux_guid
)
822 spa_aux_add(vdev_t
*vd
, avl_tree_t
*avl
)
828 search
.aux_guid
= vd
->vdev_guid
;
829 if ((aux
= avl_find(avl
, &search
, &where
)) != NULL
) {
832 aux
= kmem_zalloc(sizeof (spa_aux_t
), KM_SLEEP
);
833 aux
->aux_guid
= vd
->vdev_guid
;
835 avl_insert(avl
, aux
, where
);
840 spa_aux_remove(vdev_t
*vd
, avl_tree_t
*avl
)
846 search
.aux_guid
= vd
->vdev_guid
;
847 aux
= avl_find(avl
, &search
, &where
);
851 if (--aux
->aux_count
== 0) {
852 avl_remove(avl
, aux
);
853 kmem_free(aux
, sizeof (spa_aux_t
));
854 } else if (aux
->aux_pool
== spa_guid(vd
->vdev_spa
)) {
855 aux
->aux_pool
= 0ULL;
860 spa_aux_exists(uint64_t guid
, uint64_t *pool
, int *refcnt
, avl_tree_t
*avl
)
862 spa_aux_t search
, *found
;
864 search
.aux_guid
= guid
;
865 found
= avl_find(avl
, &search
, NULL
);
869 *pool
= found
->aux_pool
;
876 *refcnt
= found
->aux_count
;
881 return (found
!= NULL
);
885 spa_aux_activate(vdev_t
*vd
, avl_tree_t
*avl
)
887 spa_aux_t search
, *found
;
890 search
.aux_guid
= vd
->vdev_guid
;
891 found
= avl_find(avl
, &search
, &where
);
892 ASSERT(found
!= NULL
);
893 ASSERT(found
->aux_pool
== 0ULL);
895 found
->aux_pool
= spa_guid(vd
->vdev_spa
);
899 * Spares are tracked globally due to the following constraints:
901 * - A spare may be part of multiple pools.
902 * - A spare may be added to a pool even if it's actively in use within
904 * - A spare in use in any pool can only be the source of a replacement if
905 * the target is a spare in the same pool.
907 * We keep track of all spares on the system through the use of a reference
908 * counted AVL tree. When a vdev is added as a spare, or used as a replacement
909 * spare, then we bump the reference count in the AVL tree. In addition, we set
910 * the 'vdev_isspare' member to indicate that the device is a spare (active or
911 * inactive). When a spare is made active (used to replace a device in the
912 * pool), we also keep track of which pool its been made a part of.
914 * The 'spa_spare_lock' protects the AVL tree. These functions are normally
915 * called under the spa_namespace lock as part of vdev reconfiguration. The
916 * separate spare lock exists for the status query path, which does not need to
917 * be completely consistent with respect to other vdev configuration changes.
921 spa_spare_compare(const void *a
, const void *b
)
923 return (spa_aux_compare(a
, b
));
927 spa_spare_add(vdev_t
*vd
)
929 mutex_enter(&spa_spare_lock
);
930 ASSERT(!vd
->vdev_isspare
);
931 spa_aux_add(vd
, &spa_spare_avl
);
932 vd
->vdev_isspare
= B_TRUE
;
933 mutex_exit(&spa_spare_lock
);
937 spa_spare_remove(vdev_t
*vd
)
939 mutex_enter(&spa_spare_lock
);
940 ASSERT(vd
->vdev_isspare
);
941 spa_aux_remove(vd
, &spa_spare_avl
);
942 vd
->vdev_isspare
= B_FALSE
;
943 mutex_exit(&spa_spare_lock
);
947 spa_spare_exists(uint64_t guid
, uint64_t *pool
, int *refcnt
)
951 mutex_enter(&spa_spare_lock
);
952 found
= spa_aux_exists(guid
, pool
, refcnt
, &spa_spare_avl
);
953 mutex_exit(&spa_spare_lock
);
959 spa_spare_activate(vdev_t
*vd
)
961 mutex_enter(&spa_spare_lock
);
962 ASSERT(vd
->vdev_isspare
);
963 spa_aux_activate(vd
, &spa_spare_avl
);
964 mutex_exit(&spa_spare_lock
);
968 * Level 2 ARC devices are tracked globally for the same reasons as spares.
969 * Cache devices currently only support one pool per cache device, and so
970 * for these devices the aux reference count is currently unused beyond 1.
974 spa_l2cache_compare(const void *a
, const void *b
)
976 return (spa_aux_compare(a
, b
));
980 spa_l2cache_add(vdev_t
*vd
)
982 mutex_enter(&spa_l2cache_lock
);
983 ASSERT(!vd
->vdev_isl2cache
);
984 spa_aux_add(vd
, &spa_l2cache_avl
);
985 vd
->vdev_isl2cache
= B_TRUE
;
986 mutex_exit(&spa_l2cache_lock
);
990 spa_l2cache_remove(vdev_t
*vd
)
992 mutex_enter(&spa_l2cache_lock
);
993 ASSERT(vd
->vdev_isl2cache
);
994 spa_aux_remove(vd
, &spa_l2cache_avl
);
995 vd
->vdev_isl2cache
= B_FALSE
;
996 mutex_exit(&spa_l2cache_lock
);
1000 spa_l2cache_exists(uint64_t guid
, uint64_t *pool
)
1004 mutex_enter(&spa_l2cache_lock
);
1005 found
= spa_aux_exists(guid
, pool
, NULL
, &spa_l2cache_avl
);
1006 mutex_exit(&spa_l2cache_lock
);
1012 spa_l2cache_activate(vdev_t
*vd
)
1014 mutex_enter(&spa_l2cache_lock
);
1015 ASSERT(vd
->vdev_isl2cache
);
1016 spa_aux_activate(vd
, &spa_l2cache_avl
);
1017 mutex_exit(&spa_l2cache_lock
);
1021 * ==========================================================================
1023 * ==========================================================================
1027 * Lock the given spa_t for the purpose of adding or removing a vdev.
1028 * Grabs the global spa_namespace_lock plus the spa config lock for writing.
1029 * It returns the next transaction group for the spa_t.
1032 spa_vdev_enter(spa_t
*spa
)
1034 mutex_enter(&spa
->spa_vdev_top_lock
);
1035 mutex_enter(&spa_namespace_lock
);
1036 return (spa_vdev_config_enter(spa
));
1040 * Internal implementation for spa_vdev_enter(). Used when a vdev
1041 * operation requires multiple syncs (i.e. removing a device) while
1042 * keeping the spa_namespace_lock held.
1045 spa_vdev_config_enter(spa_t
*spa
)
1047 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1049 spa_config_enter(spa
, SCL_ALL
, spa
, RW_WRITER
);
1051 return (spa_last_synced_txg(spa
) + 1);
1055 * Used in combination with spa_vdev_config_enter() to allow the syncing
1056 * of multiple transactions without releasing the spa_namespace_lock.
1059 spa_vdev_config_exit(spa_t
*spa
, vdev_t
*vd
, uint64_t txg
, int error
, char *tag
)
1061 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1063 int config_changed
= B_FALSE
;
1065 ASSERT(txg
> spa_last_synced_txg(spa
));
1067 spa
->spa_pending_vdev
= NULL
;
1070 * Reassess the DTLs.
1072 vdev_dtl_reassess(spa
->spa_root_vdev
, 0, 0, B_FALSE
);
1074 if (error
== 0 && !list_is_empty(&spa
->spa_config_dirty_list
)) {
1075 config_changed
= B_TRUE
;
1076 spa
->spa_config_generation
++;
1080 * Verify the metaslab classes.
1082 ASSERT(metaslab_class_validate(spa_normal_class(spa
)) == 0);
1083 ASSERT(metaslab_class_validate(spa_log_class(spa
)) == 0);
1085 spa_config_exit(spa
, SCL_ALL
, spa
);
1088 * Panic the system if the specified tag requires it. This
1089 * is useful for ensuring that configurations are updated
1092 if (zio_injection_enabled
)
1093 zio_handle_panic_injection(spa
, tag
, 0);
1096 * Note: this txg_wait_synced() is important because it ensures
1097 * that there won't be more than one config change per txg.
1098 * This allows us to use the txg as the generation number.
1101 txg_wait_synced(spa
->spa_dsl_pool
, txg
);
1104 ASSERT(!vd
->vdev_detached
|| vd
->vdev_dtl_sm
== NULL
);
1105 spa_config_enter(spa
, SCL_ALL
, spa
, RW_WRITER
);
1107 spa_config_exit(spa
, SCL_ALL
, spa
);
1111 * If the config changed, update the config cache.
1114 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
1118 * Unlock the spa_t after adding or removing a vdev. Besides undoing the
1119 * locking of spa_vdev_enter(), we also want make sure the transactions have
1120 * synced to disk, and then update the global configuration cache with the new
1124 spa_vdev_exit(spa_t
*spa
, vdev_t
*vd
, uint64_t txg
, int error
)
1126 spa_vdev_config_exit(spa
, vd
, txg
, error
, FTAG
);
1127 mutex_exit(&spa_namespace_lock
);
1128 mutex_exit(&spa
->spa_vdev_top_lock
);
1134 * Lock the given spa_t for the purpose of changing vdev state.
1137 spa_vdev_state_enter(spa_t
*spa
, int oplocks
)
1139 int locks
= SCL_STATE_ALL
| oplocks
;
1142 * Root pools may need to read of the underlying devfs filesystem
1143 * when opening up a vdev. Unfortunately if we're holding the
1144 * SCL_ZIO lock it will result in a deadlock when we try to issue
1145 * the read from the root filesystem. Instead we "prefetch"
1146 * the associated vnodes that we need prior to opening the
1147 * underlying devices and cache them so that we can prevent
1148 * any I/O when we are doing the actual open.
1150 if (spa_is_root(spa
)) {
1151 int low
= locks
& ~(SCL_ZIO
- 1);
1152 int high
= locks
& ~low
;
1154 spa_config_enter(spa
, high
, spa
, RW_WRITER
);
1155 vdev_hold(spa
->spa_root_vdev
);
1156 spa_config_enter(spa
, low
, spa
, RW_WRITER
);
1158 spa_config_enter(spa
, locks
, spa
, RW_WRITER
);
1160 spa
->spa_vdev_locks
= locks
;
1164 spa_vdev_state_exit(spa_t
*spa
, vdev_t
*vd
, int error
)
1166 boolean_t config_changed
= B_FALSE
;
1168 if (vd
!= NULL
|| error
== 0)
1169 vdev_dtl_reassess(vd
? vd
->vdev_top
: spa
->spa_root_vdev
,
1173 vdev_state_dirty(vd
->vdev_top
);
1174 config_changed
= B_TRUE
;
1175 spa
->spa_config_generation
++;
1178 if (spa_is_root(spa
))
1179 vdev_rele(spa
->spa_root_vdev
);
1181 ASSERT3U(spa
->spa_vdev_locks
, >=, SCL_STATE_ALL
);
1182 spa_config_exit(spa
, spa
->spa_vdev_locks
, spa
);
1185 * If anything changed, wait for it to sync. This ensures that,
1186 * from the system administrator's perspective, zpool(1M) commands
1187 * are synchronous. This is important for things like zpool offline:
1188 * when the command completes, you expect no further I/O from ZFS.
1191 txg_wait_synced(spa
->spa_dsl_pool
, 0);
1194 * If the config changed, update the config cache.
1196 if (config_changed
) {
1197 mutex_enter(&spa_namespace_lock
);
1198 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
1199 mutex_exit(&spa_namespace_lock
);
1206 * ==========================================================================
1207 * Miscellaneous functions
1208 * ==========================================================================
1212 spa_activate_mos_feature(spa_t
*spa
, const char *feature
, dmu_tx_t
*tx
)
1214 if (!nvlist_exists(spa
->spa_label_features
, feature
)) {
1215 fnvlist_add_boolean(spa
->spa_label_features
, feature
);
1217 * When we are creating the pool (tx_txg==TXG_INITIAL), we can't
1218 * dirty the vdev config because lock SCL_CONFIG is not held.
1219 * Thankfully, in this case we don't need to dirty the config
1220 * because it will be written out anyway when we finish
1221 * creating the pool.
1223 if (tx
->tx_txg
!= TXG_INITIAL
)
1224 vdev_config_dirty(spa
->spa_root_vdev
);
1229 spa_deactivate_mos_feature(spa_t
*spa
, const char *feature
)
1231 if (nvlist_remove_all(spa
->spa_label_features
, feature
) == 0)
1232 vdev_config_dirty(spa
->spa_root_vdev
);
1239 spa_rename(const char *name
, const char *newname
)
1245 * Lookup the spa_t and grab the config lock for writing. We need to
1246 * actually open the pool so that we can sync out the necessary labels.
1247 * It's OK to call spa_open() with the namespace lock held because we
1248 * allow recursive calls for other reasons.
1250 mutex_enter(&spa_namespace_lock
);
1251 if ((err
= spa_open(name
, &spa
, FTAG
)) != 0) {
1252 mutex_exit(&spa_namespace_lock
);
1256 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1258 avl_remove(&spa_namespace_avl
, spa
);
1259 (void) strlcpy(spa
->spa_name
, newname
, sizeof (spa
->spa_name
));
1260 avl_add(&spa_namespace_avl
, spa
);
1263 * Sync all labels to disk with the new names by marking the root vdev
1264 * dirty and waiting for it to sync. It will pick up the new pool name
1267 vdev_config_dirty(spa
->spa_root_vdev
);
1269 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1271 txg_wait_synced(spa
->spa_dsl_pool
, 0);
1274 * Sync the updated config cache.
1276 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
1278 spa_close(spa
, FTAG
);
1280 mutex_exit(&spa_namespace_lock
);
1286 * Return the spa_t associated with given pool_guid, if it exists. If
1287 * device_guid is non-zero, determine whether the pool exists *and* contains
1288 * a device with the specified device_guid.
1291 spa_by_guid(uint64_t pool_guid
, uint64_t device_guid
)
1294 avl_tree_t
*t
= &spa_namespace_avl
;
1296 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1298 for (spa
= avl_first(t
); spa
!= NULL
; spa
= AVL_NEXT(t
, spa
)) {
1299 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
)
1301 if (spa
->spa_root_vdev
== NULL
)
1303 if (spa_guid(spa
) == pool_guid
) {
1304 if (device_guid
== 0)
1307 if (vdev_lookup_by_guid(spa
->spa_root_vdev
,
1308 device_guid
) != NULL
)
1312 * Check any devices we may be in the process of adding.
1314 if (spa
->spa_pending_vdev
) {
1315 if (vdev_lookup_by_guid(spa
->spa_pending_vdev
,
1316 device_guid
) != NULL
)
1326 * Determine whether a pool with the given pool_guid exists.
1329 spa_guid_exists(uint64_t pool_guid
, uint64_t device_guid
)
1331 return (spa_by_guid(pool_guid
, device_guid
) != NULL
);
1335 spa_strdup(const char *s
)
1341 new = kmem_alloc(len
+ 1, KM_SLEEP
);
1349 spa_strfree(char *s
)
1351 kmem_free(s
, strlen(s
) + 1);
1355 spa_get_random(uint64_t range
)
1361 (void) random_get_pseudo_bytes((void *)&r
, sizeof (uint64_t));
1367 spa_generate_guid(spa_t
*spa
)
1369 uint64_t guid
= spa_get_random(-1ULL);
1372 while (guid
== 0 || spa_guid_exists(spa_guid(spa
), guid
))
1373 guid
= spa_get_random(-1ULL);
1375 while (guid
== 0 || spa_guid_exists(guid
, 0))
1376 guid
= spa_get_random(-1ULL);
1383 snprintf_blkptr(char *buf
, size_t buflen
, const blkptr_t
*bp
)
1386 char *checksum
= NULL
;
1387 char *compress
= NULL
;
1390 if (BP_GET_TYPE(bp
) & DMU_OT_NEWTYPE
) {
1391 dmu_object_byteswap_t bswap
=
1392 DMU_OT_BYTESWAP(BP_GET_TYPE(bp
));
1393 (void) snprintf(type
, sizeof (type
), "bswap %s %s",
1394 DMU_OT_IS_METADATA(BP_GET_TYPE(bp
)) ?
1395 "metadata" : "data",
1396 dmu_ot_byteswap
[bswap
].ob_name
);
1398 (void) strlcpy(type
, dmu_ot
[BP_GET_TYPE(bp
)].ot_name
,
1401 if (!BP_IS_EMBEDDED(bp
)) {
1403 zio_checksum_table
[BP_GET_CHECKSUM(bp
)].ci_name
;
1405 compress
= zio_compress_table
[BP_GET_COMPRESS(bp
)].ci_name
;
1408 SNPRINTF_BLKPTR(snprintf
, ' ', buf
, buflen
, bp
, type
, checksum
,
1413 spa_freeze(spa_t
*spa
)
1415 uint64_t freeze_txg
= 0;
1417 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1418 if (spa
->spa_freeze_txg
== UINT64_MAX
) {
1419 freeze_txg
= spa_last_synced_txg(spa
) + TXG_SIZE
;
1420 spa
->spa_freeze_txg
= freeze_txg
;
1422 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1423 if (freeze_txg
!= 0)
1424 txg_wait_synced(spa_get_dsl(spa
), freeze_txg
);
1428 zfs_panic_recover(const char *fmt
, ...)
1433 vcmn_err(zfs_recover
? CE_WARN
: CE_PANIC
, fmt
, adx
);
1438 * This is a stripped-down version of strtoull, suitable only for converting
1439 * lowercase hexadecimal numbers that don't overflow.
1442 strtonum(const char *str
, char **nptr
)
1448 while ((c
= *str
) != '\0') {
1449 if (c
>= '0' && c
<= '9')
1451 else if (c
>= 'a' && c
<= 'f')
1452 digit
= 10 + c
- 'a';
1463 *nptr
= (char *)str
;
1469 * ==========================================================================
1470 * Accessor functions
1471 * ==========================================================================
1475 spa_shutting_down(spa_t
*spa
)
1477 return (spa
->spa_async_suspended
);
1481 spa_get_dsl(spa_t
*spa
)
1483 return (spa
->spa_dsl_pool
);
1487 spa_is_initializing(spa_t
*spa
)
1489 return (spa
->spa_is_initializing
);
1493 spa_get_rootblkptr(spa_t
*spa
)
1495 return (&spa
->spa_ubsync
.ub_rootbp
);
1499 spa_set_rootblkptr(spa_t
*spa
, const blkptr_t
*bp
)
1501 spa
->spa_uberblock
.ub_rootbp
= *bp
;
1505 spa_altroot(spa_t
*spa
, char *buf
, size_t buflen
)
1507 if (spa
->spa_root
== NULL
)
1510 (void) strncpy(buf
, spa
->spa_root
, buflen
);
1514 spa_sync_pass(spa_t
*spa
)
1516 return (spa
->spa_sync_pass
);
1520 spa_name(spa_t
*spa
)
1522 return (spa
->spa_name
);
1526 spa_guid(spa_t
*spa
)
1528 dsl_pool_t
*dp
= spa_get_dsl(spa
);
1532 * If we fail to parse the config during spa_load(), we can go through
1533 * the error path (which posts an ereport) and end up here with no root
1534 * vdev. We stash the original pool guid in 'spa_config_guid' to handle
1537 if (spa
->spa_root_vdev
== NULL
)
1538 return (spa
->spa_config_guid
);
1540 guid
= spa
->spa_last_synced_guid
!= 0 ?
1541 spa
->spa_last_synced_guid
: spa
->spa_root_vdev
->vdev_guid
;
1544 * Return the most recently synced out guid unless we're
1545 * in syncing context.
1547 if (dp
&& dsl_pool_sync_context(dp
))
1548 return (spa
->spa_root_vdev
->vdev_guid
);
1554 spa_load_guid(spa_t
*spa
)
1557 * This is a GUID that exists solely as a reference for the
1558 * purposes of the arc. It is generated at load time, and
1559 * is never written to persistent storage.
1561 return (spa
->spa_load_guid
);
1565 spa_last_synced_txg(spa_t
*spa
)
1567 return (spa
->spa_ubsync
.ub_txg
);
1571 spa_first_txg(spa_t
*spa
)
1573 return (spa
->spa_first_txg
);
1577 spa_syncing_txg(spa_t
*spa
)
1579 return (spa
->spa_syncing_txg
);
1583 spa_state(spa_t
*spa
)
1585 return (spa
->spa_state
);
1589 spa_load_state(spa_t
*spa
)
1591 return (spa
->spa_load_state
);
1595 spa_freeze_txg(spa_t
*spa
)
1597 return (spa
->spa_freeze_txg
);
1602 spa_get_asize(spa_t
*spa
, uint64_t lsize
)
1604 return (lsize
* spa_asize_inflation
);
1608 * Return the amount of slop space in bytes. It is 1/32 of the pool (3.2%),
1611 * See the comment above spa_slop_shift for details.
1614 spa_get_slop_space(spa_t
*spa
) {
1615 uint64_t space
= spa_get_dspace(spa
);
1616 return (MAX(space
>> spa_slop_shift
, SPA_MINDEVSIZE
>> 1));
1620 spa_get_dspace(spa_t
*spa
)
1622 return (spa
->spa_dspace
);
1626 spa_update_dspace(spa_t
*spa
)
1628 spa
->spa_dspace
= metaslab_class_get_dspace(spa_normal_class(spa
)) +
1629 ddt_get_dedup_dspace(spa
);
1633 * Return the failure mode that has been set to this pool. The default
1634 * behavior will be to block all I/Os when a complete failure occurs.
1637 spa_get_failmode(spa_t
*spa
)
1639 return (spa
->spa_failmode
);
1643 spa_suspended(spa_t
*spa
)
1645 return (spa
->spa_suspended
);
1649 spa_version(spa_t
*spa
)
1651 return (spa
->spa_ubsync
.ub_version
);
1655 spa_deflate(spa_t
*spa
)
1657 return (spa
->spa_deflate
);
1661 spa_normal_class(spa_t
*spa
)
1663 return (spa
->spa_normal_class
);
1667 spa_log_class(spa_t
*spa
)
1669 return (spa
->spa_log_class
);
1673 spa_max_replication(spa_t
*spa
)
1676 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
1677 * handle BPs with more than one DVA allocated. Set our max
1678 * replication level accordingly.
1680 if (spa_version(spa
) < SPA_VERSION_DITTO_BLOCKS
)
1682 return (MIN(SPA_DVAS_PER_BP
, spa_max_replication_override
));
1686 spa_prev_software_version(spa_t
*spa
)
1688 return (spa
->spa_prev_software_version
);
1692 spa_deadman_synctime(spa_t
*spa
)
1694 return (spa
->spa_deadman_synctime
);
1698 dva_get_dsize_sync(spa_t
*spa
, const dva_t
*dva
)
1700 uint64_t asize
= DVA_GET_ASIZE(dva
);
1701 uint64_t dsize
= asize
;
1703 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_READER
) != 0);
1705 if (asize
!= 0 && spa
->spa_deflate
) {
1706 vdev_t
*vd
= vdev_lookup_top(spa
, DVA_GET_VDEV(dva
));
1707 dsize
= (asize
>> SPA_MINBLOCKSHIFT
) * vd
->vdev_deflate_ratio
;
1714 bp_get_dsize_sync(spa_t
*spa
, const blkptr_t
*bp
)
1718 for (int d
= 0; d
< BP_GET_NDVAS(bp
); d
++)
1719 dsize
+= dva_get_dsize_sync(spa
, &bp
->blk_dva
[d
]);
1725 bp_get_dsize(spa_t
*spa
, const blkptr_t
*bp
)
1729 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
1731 for (int d
= 0; d
< BP_GET_NDVAS(bp
); d
++)
1732 dsize
+= dva_get_dsize_sync(spa
, &bp
->blk_dva
[d
]);
1734 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
1740 * ==========================================================================
1741 * Initialization and Termination
1742 * ==========================================================================
1746 spa_name_compare(const void *a1
, const void *a2
)
1748 const spa_t
*s1
= a1
;
1749 const spa_t
*s2
= a2
;
1752 s
= strcmp(s1
->spa_name
, s2
->spa_name
);
1763 return (spa_active_count
);
1775 mutex_init(&spa_namespace_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1776 mutex_init(&spa_spare_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1777 mutex_init(&spa_l2cache_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1778 cv_init(&spa_namespace_cv
, NULL
, CV_DEFAULT
, NULL
);
1780 avl_create(&spa_namespace_avl
, spa_name_compare
, sizeof (spa_t
),
1781 offsetof(spa_t
, spa_avl
));
1783 avl_create(&spa_spare_avl
, spa_spare_compare
, sizeof (spa_aux_t
),
1784 offsetof(spa_aux_t
, aux_avl
));
1786 avl_create(&spa_l2cache_avl
, spa_l2cache_compare
, sizeof (spa_aux_t
),
1787 offsetof(spa_aux_t
, aux_avl
));
1789 spa_mode_global
= mode
;
1794 if (spa_mode_global
!= FREAD
&& dprintf_find_string("watch")) {
1795 arc_procfd
= open("/proc/self/ctl", O_WRONLY
);
1796 if (arc_procfd
== -1) {
1797 perror("could not enable watchpoints: "
1798 "opening /proc/self/ctl failed: ");
1811 vdev_cache_stat_init();
1814 zpool_feature_init();
1826 vdev_cache_stat_fini();
1834 avl_destroy(&spa_namespace_avl
);
1835 avl_destroy(&spa_spare_avl
);
1836 avl_destroy(&spa_l2cache_avl
);
1838 cv_destroy(&spa_namespace_cv
);
1839 mutex_destroy(&spa_namespace_lock
);
1840 mutex_destroy(&spa_spare_lock
);
1841 mutex_destroy(&spa_l2cache_lock
);
1845 * Return whether this pool has slogs. No locking needed.
1846 * It's not a problem if the wrong answer is returned as it's only for
1847 * performance and not correctness
1850 spa_has_slogs(spa_t
*spa
)
1852 return (spa
->spa_log_class
->mc_rotor
!= NULL
);
1856 spa_get_log_state(spa_t
*spa
)
1858 return (spa
->spa_log_state
);
1862 spa_set_log_state(spa_t
*spa
, spa_log_state_t state
)
1864 spa
->spa_log_state
= state
;
1868 spa_is_root(spa_t
*spa
)
1870 return (spa
->spa_is_root
);
1874 spa_writeable(spa_t
*spa
)
1876 return (!!(spa
->spa_mode
& FWRITE
));
1880 * Returns true if there is a pending sync task in any of the current
1881 * syncing txg, the current quiescing txg, or the current open txg.
1884 spa_has_pending_synctask(spa_t
*spa
)
1886 return (!txg_all_lists_empty(&spa
->spa_dsl_pool
->dp_sync_tasks
));
1890 spa_mode(spa_t
*spa
)
1892 return (spa
->spa_mode
);
1896 spa_bootfs(spa_t
*spa
)
1898 return (spa
->spa_bootfs
);
1902 spa_delegation(spa_t
*spa
)
1904 return (spa
->spa_delegation
);
1908 spa_meta_objset(spa_t
*spa
)
1910 return (spa
->spa_meta_objset
);
1914 spa_dedup_checksum(spa_t
*spa
)
1916 return (spa
->spa_dedup_checksum
);
1920 * Reset pool scan stat per scan pass (or reboot).
1923 spa_scan_stat_init(spa_t
*spa
)
1925 /* data not stored on disk */
1926 spa
->spa_scan_pass_start
= gethrestime_sec();
1927 spa
->spa_scan_pass_exam
= 0;
1928 vdev_scan_stat_init(spa
->spa_root_vdev
);
1932 * Get scan stats for zpool status reports
1935 spa_scan_get_stats(spa_t
*spa
, pool_scan_stat_t
*ps
)
1937 dsl_scan_t
*scn
= spa
->spa_dsl_pool
? spa
->spa_dsl_pool
->dp_scan
: NULL
;
1939 if (scn
== NULL
|| scn
->scn_phys
.scn_func
== POOL_SCAN_NONE
)
1940 return (SET_ERROR(ENOENT
));
1941 bzero(ps
, sizeof (pool_scan_stat_t
));
1943 /* data stored on disk */
1944 ps
->pss_func
= scn
->scn_phys
.scn_func
;
1945 ps
->pss_start_time
= scn
->scn_phys
.scn_start_time
;
1946 ps
->pss_end_time
= scn
->scn_phys
.scn_end_time
;
1947 ps
->pss_to_examine
= scn
->scn_phys
.scn_to_examine
;
1948 ps
->pss_examined
= scn
->scn_phys
.scn_examined
;
1949 ps
->pss_to_process
= scn
->scn_phys
.scn_to_process
;
1950 ps
->pss_processed
= scn
->scn_phys
.scn_processed
;
1951 ps
->pss_errors
= scn
->scn_phys
.scn_errors
;
1952 ps
->pss_state
= scn
->scn_phys
.scn_state
;
1954 /* data not stored on disk */
1955 ps
->pss_pass_start
= spa
->spa_scan_pass_start
;
1956 ps
->pss_pass_exam
= spa
->spa_scan_pass_exam
;
1962 spa_debug_enabled(spa_t
*spa
)
1964 return (spa
->spa_debug
);
1968 spa_maxblocksize(spa_t
*spa
)
1970 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_BLOCKS
))
1971 return (SPA_MAXBLOCKSIZE
);
1973 return (SPA_OLD_MAXBLOCKSIZE
);