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, 2017 by Delphix. All rights reserved.
24 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
26 * Copyright 2013 Saso Kiselkov. All rights reserved.
29 #include <sys/zfs_context.h>
30 #include <sys/spa_impl.h>
32 #include <sys/zio_checksum.h>
33 #include <sys/zio_compress.h>
35 #include <sys/dmu_tx.h>
38 #include <sys/vdev_impl.h>
39 #include <sys/vdev_file.h>
40 #include <sys/vdev_raidz.h>
41 #include <sys/metaslab.h>
42 #include <sys/uberblock_impl.h>
45 #include <sys/unique.h>
46 #include <sys/dsl_pool.h>
47 #include <sys/dsl_dir.h>
48 #include <sys/dsl_prop.h>
49 #include <sys/fm/util.h>
50 #include <sys/dsl_scan.h>
51 #include <sys/fs/zfs.h>
52 #include <sys/metaslab_impl.h>
55 #include <sys/kstat.h>
57 #include <sys/zfeature.h>
58 #include "qat_compress.h"
63 * There are four basic locks for managing spa_t structures:
65 * spa_namespace_lock (global mutex)
67 * This lock must be acquired to do any of the following:
69 * - Lookup a spa_t by name
70 * - Add or remove a spa_t from the namespace
71 * - Increase spa_refcount from non-zero
72 * - Check if spa_refcount is zero
74 * - add/remove/attach/detach devices
75 * - Held for the duration of create/destroy/import/export
77 * It does not need to handle recursion. A create or destroy may
78 * reference objects (files or zvols) in other pools, but by
79 * definition they must have an existing reference, and will never need
80 * to lookup a spa_t by name.
82 * spa_refcount (per-spa refcount_t protected by mutex)
84 * This reference count keep track of any active users of the spa_t. The
85 * spa_t cannot be destroyed or freed while this is non-zero. Internally,
86 * the refcount is never really 'zero' - opening a pool implicitly keeps
87 * some references in the DMU. Internally we check against spa_minref, but
88 * present the image of a zero/non-zero value to consumers.
90 * spa_config_lock[] (per-spa array of rwlocks)
92 * This protects the spa_t from config changes, and must be held in
93 * the following circumstances:
95 * - RW_READER to perform I/O to the spa
96 * - RW_WRITER to change the vdev config
98 * The locking order is fairly straightforward:
100 * spa_namespace_lock -> spa_refcount
102 * The namespace lock must be acquired to increase the refcount from 0
103 * or to check if it is zero.
105 * spa_refcount -> spa_config_lock[]
107 * There must be at least one valid reference on the spa_t to acquire
110 * spa_namespace_lock -> spa_config_lock[]
112 * The namespace lock must always be taken before the config lock.
115 * The spa_namespace_lock can be acquired directly and is globally visible.
117 * The namespace is manipulated using the following functions, all of which
118 * require the spa_namespace_lock to be held.
120 * spa_lookup() Lookup a spa_t by name.
122 * spa_add() Create a new spa_t in the namespace.
124 * spa_remove() Remove a spa_t from the namespace. This also
125 * frees up any memory associated with the spa_t.
127 * spa_next() Returns the next spa_t in the system, or the
128 * first if NULL is passed.
130 * spa_evict_all() Shutdown and remove all spa_t structures in
133 * spa_guid_exists() Determine whether a pool/device guid exists.
135 * The spa_refcount is manipulated using the following functions:
137 * spa_open_ref() Adds a reference to the given spa_t. Must be
138 * called with spa_namespace_lock held if the
139 * refcount is currently zero.
141 * spa_close() Remove a reference from the spa_t. This will
142 * not free the spa_t or remove it from the
143 * namespace. No locking is required.
145 * spa_refcount_zero() Returns true if the refcount is currently
146 * zero. Must be called with spa_namespace_lock
149 * The spa_config_lock[] is an array of rwlocks, ordered as follows:
150 * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV.
151 * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}().
153 * To read the configuration, it suffices to hold one of these locks as reader.
154 * To modify the configuration, you must hold all locks as writer. To modify
155 * vdev state without altering the vdev tree's topology (e.g. online/offline),
156 * you must hold SCL_STATE and SCL_ZIO as writer.
158 * We use these distinct config locks to avoid recursive lock entry.
159 * For example, spa_sync() (which holds SCL_CONFIG as reader) induces
160 * block allocations (SCL_ALLOC), which may require reading space maps
161 * from disk (dmu_read() -> zio_read() -> SCL_ZIO).
163 * The spa config locks cannot be normal rwlocks because we need the
164 * ability to hand off ownership. For example, SCL_ZIO is acquired
165 * by the issuing thread and later released by an interrupt thread.
166 * They do, however, obey the usual write-wanted semantics to prevent
167 * writer (i.e. system administrator) starvation.
169 * The lock acquisition rules are as follows:
172 * Protects changes to the vdev tree topology, such as vdev
173 * add/remove/attach/detach. Protects the dirty config list
174 * (spa_config_dirty_list) and the set of spares and l2arc devices.
177 * Protects changes to pool state and vdev state, such as vdev
178 * online/offline/fault/degrade/clear. Protects the dirty state list
179 * (spa_state_dirty_list) and global pool state (spa_state).
182 * Protects changes to metaslab groups and classes.
183 * Held as reader by metaslab_alloc() and metaslab_claim().
186 * Held by bp-level zios (those which have no io_vd upon entry)
187 * to prevent changes to the vdev tree. The bp-level zio implicitly
188 * protects all of its vdev child zios, which do not hold SCL_ZIO.
191 * Protects changes to metaslab groups and classes.
192 * Held as reader by metaslab_free(). SCL_FREE is distinct from
193 * SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free
194 * blocks in zio_done() while another i/o that holds either
195 * SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete.
198 * Held as reader to prevent changes to the vdev tree during trivial
199 * inquiries such as bp_get_dsize(). SCL_VDEV is distinct from the
200 * other locks, and lower than all of them, to ensure that it's safe
201 * to acquire regardless of caller context.
203 * In addition, the following rules apply:
205 * (a) spa_props_lock protects pool properties, spa_config and spa_config_list.
206 * The lock ordering is SCL_CONFIG > spa_props_lock.
208 * (b) I/O operations on leaf vdevs. For any zio operation that takes
209 * an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(),
210 * or zio_write_phys() -- the caller must ensure that the config cannot
211 * cannot change in the interim, and that the vdev cannot be reopened.
212 * SCL_STATE as reader suffices for both.
214 * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
216 * spa_vdev_enter() Acquire the namespace lock and the config lock
219 * spa_vdev_exit() Release the config lock, wait for all I/O
220 * to complete, sync the updated configs to the
221 * cache, and release the namespace lock.
223 * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit().
224 * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual
225 * locking is, always, based on spa_namespace_lock and spa_config_lock[].
227 * spa_rename() is also implemented within this file since it requires
228 * manipulation of the namespace.
231 static avl_tree_t spa_namespace_avl
;
232 kmutex_t spa_namespace_lock
;
233 static kcondvar_t spa_namespace_cv
;
234 int spa_max_replication_override
= SPA_DVAS_PER_BP
;
236 static kmutex_t spa_spare_lock
;
237 static avl_tree_t spa_spare_avl
;
238 static kmutex_t spa_l2cache_lock
;
239 static avl_tree_t spa_l2cache_avl
;
241 kmem_cache_t
*spa_buffer_pool
;
245 /* Everything except dprintf and spa is on by default in debug builds */
246 int zfs_flags
= ~(ZFS_DEBUG_DPRINTF
| ZFS_DEBUG_SPA
);
252 * zfs_recover can be set to nonzero to attempt to recover from
253 * otherwise-fatal errors, typically caused by on-disk corruption. When
254 * set, calls to zfs_panic_recover() will turn into warning messages.
255 * This should only be used as a last resort, as it typically results
256 * in leaked space, or worse.
258 int zfs_recover
= B_FALSE
;
261 * If destroy encounters an EIO while reading metadata (e.g. indirect
262 * blocks), space referenced by the missing metadata can not be freed.
263 * Normally this causes the background destroy to become "stalled", as
264 * it is unable to make forward progress. While in this stalled state,
265 * all remaining space to free from the error-encountering filesystem is
266 * "temporarily leaked". Set this flag to cause it to ignore the EIO,
267 * permanently leak the space from indirect blocks that can not be read,
268 * and continue to free everything else that it can.
270 * The default, "stalling" behavior is useful if the storage partially
271 * fails (i.e. some but not all i/os fail), and then later recovers. In
272 * this case, we will be able to continue pool operations while it is
273 * partially failed, and when it recovers, we can continue to free the
274 * space, with no leaks. However, note that this case is actually
277 * Typically pools either (a) fail completely (but perhaps temporarily,
278 * e.g. a top-level vdev going offline), or (b) have localized,
279 * permanent errors (e.g. disk returns the wrong data due to bit flip or
280 * firmware bug). In case (a), this setting does not matter because the
281 * pool will be suspended and the sync thread will not be able to make
282 * forward progress regardless. In case (b), because the error is
283 * permanent, the best we can do is leak the minimum amount of space,
284 * which is what setting this flag will do. Therefore, it is reasonable
285 * for this flag to normally be set, but we chose the more conservative
286 * approach of not setting it, so that there is no possibility of
287 * leaking space in the "partial temporary" failure case.
289 int zfs_free_leak_on_eio
= B_FALSE
;
292 * Expiration time in milliseconds. This value has two meanings. First it is
293 * used to determine when the spa_deadman() logic should fire. By default the
294 * spa_deadman() will fire if spa_sync() has not completed in 1000 seconds.
295 * Secondly, the value determines if an I/O is considered "hung". Any I/O that
296 * has not completed in zfs_deadman_synctime_ms is considered "hung" resulting
299 unsigned long zfs_deadman_synctime_ms
= 1000000ULL;
302 * Check time in milliseconds. This defines the frequency at which we check
305 unsigned long zfs_deadman_checktime_ms
= 5000ULL;
308 * By default the deadman is enabled.
310 int zfs_deadman_enabled
= 1;
313 * The worst case is single-sector max-parity RAID-Z blocks, in which
314 * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1)
315 * times the size; so just assume that. Add to this the fact that
316 * we can have up to 3 DVAs per bp, and one more factor of 2 because
317 * the block may be dittoed with up to 3 DVAs by ddt_sync(). All together,
319 * (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2 == 24
321 int spa_asize_inflation
= 24;
324 * Normally, we don't allow the last 3.2% (1/(2^spa_slop_shift)) of space in
325 * the pool to be consumed. This ensures that we don't run the pool
326 * completely out of space, due to unaccounted changes (e.g. to the MOS).
327 * It also limits the worst-case time to allocate space. If we have
328 * less than this amount of free space, most ZPL operations (e.g. write,
329 * create) will return ENOSPC.
331 * Certain operations (e.g. file removal, most administrative actions) can
332 * use half the slop space. They will only return ENOSPC if less than half
333 * the slop space is free. Typically, once the pool has less than the slop
334 * space free, the user will use these operations to free up space in the pool.
335 * These are the operations that call dsl_pool_adjustedsize() with the netfree
336 * argument set to TRUE.
338 * A very restricted set of operations are always permitted, regardless of
339 * the amount of free space. These are the operations that call
340 * dsl_sync_task(ZFS_SPACE_CHECK_NONE), e.g. "zfs destroy". If these
341 * operations result in a net increase in the amount of space used,
342 * it is possible to run the pool completely out of space, causing it to
343 * be permanently read-only.
345 * Note that on very small pools, the slop space will be larger than
346 * 3.2%, in an effort to have it be at least spa_min_slop (128MB),
347 * but we never allow it to be more than half the pool size.
349 * See also the comments in zfs_space_check_t.
351 int spa_slop_shift
= 5;
352 uint64_t spa_min_slop
= 128 * 1024 * 1024;
355 * ==========================================================================
357 * ==========================================================================
360 spa_config_lock_init(spa_t
*spa
)
364 for (i
= 0; i
< SCL_LOCKS
; i
++) {
365 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
366 mutex_init(&scl
->scl_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
367 cv_init(&scl
->scl_cv
, NULL
, CV_DEFAULT
, NULL
);
368 refcount_create_untracked(&scl
->scl_count
);
369 scl
->scl_writer
= NULL
;
370 scl
->scl_write_wanted
= 0;
375 spa_config_lock_destroy(spa_t
*spa
)
379 for (i
= 0; i
< SCL_LOCKS
; i
++) {
380 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
381 mutex_destroy(&scl
->scl_lock
);
382 cv_destroy(&scl
->scl_cv
);
383 refcount_destroy(&scl
->scl_count
);
384 ASSERT(scl
->scl_writer
== NULL
);
385 ASSERT(scl
->scl_write_wanted
== 0);
390 spa_config_tryenter(spa_t
*spa
, int locks
, void *tag
, krw_t rw
)
394 for (i
= 0; i
< SCL_LOCKS
; i
++) {
395 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
396 if (!(locks
& (1 << i
)))
398 mutex_enter(&scl
->scl_lock
);
399 if (rw
== RW_READER
) {
400 if (scl
->scl_writer
|| scl
->scl_write_wanted
) {
401 mutex_exit(&scl
->scl_lock
);
402 spa_config_exit(spa
, locks
& ((1 << i
) - 1),
407 ASSERT(scl
->scl_writer
!= curthread
);
408 if (!refcount_is_zero(&scl
->scl_count
)) {
409 mutex_exit(&scl
->scl_lock
);
410 spa_config_exit(spa
, locks
& ((1 << i
) - 1),
414 scl
->scl_writer
= curthread
;
416 (void) refcount_add(&scl
->scl_count
, tag
);
417 mutex_exit(&scl
->scl_lock
);
423 spa_config_enter(spa_t
*spa
, int locks
, void *tag
, krw_t rw
)
428 ASSERT3U(SCL_LOCKS
, <, sizeof (wlocks_held
) * NBBY
);
430 for (i
= 0; i
< SCL_LOCKS
; i
++) {
431 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
432 if (scl
->scl_writer
== curthread
)
433 wlocks_held
|= (1 << i
);
434 if (!(locks
& (1 << i
)))
436 mutex_enter(&scl
->scl_lock
);
437 if (rw
== RW_READER
) {
438 while (scl
->scl_writer
|| scl
->scl_write_wanted
) {
439 cv_wait(&scl
->scl_cv
, &scl
->scl_lock
);
442 ASSERT(scl
->scl_writer
!= curthread
);
443 while (!refcount_is_zero(&scl
->scl_count
)) {
444 scl
->scl_write_wanted
++;
445 cv_wait(&scl
->scl_cv
, &scl
->scl_lock
);
446 scl
->scl_write_wanted
--;
448 scl
->scl_writer
= curthread
;
450 (void) refcount_add(&scl
->scl_count
, tag
);
451 mutex_exit(&scl
->scl_lock
);
453 ASSERT(wlocks_held
<= locks
);
457 spa_config_exit(spa_t
*spa
, int locks
, void *tag
)
461 for (i
= SCL_LOCKS
- 1; i
>= 0; i
--) {
462 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
463 if (!(locks
& (1 << i
)))
465 mutex_enter(&scl
->scl_lock
);
466 ASSERT(!refcount_is_zero(&scl
->scl_count
));
467 if (refcount_remove(&scl
->scl_count
, tag
) == 0) {
468 ASSERT(scl
->scl_writer
== NULL
||
469 scl
->scl_writer
== curthread
);
470 scl
->scl_writer
= NULL
; /* OK in either case */
471 cv_broadcast(&scl
->scl_cv
);
473 mutex_exit(&scl
->scl_lock
);
478 spa_config_held(spa_t
*spa
, int locks
, krw_t rw
)
480 int i
, locks_held
= 0;
482 for (i
= 0; i
< SCL_LOCKS
; i
++) {
483 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
484 if (!(locks
& (1 << i
)))
486 if ((rw
== RW_READER
&& !refcount_is_zero(&scl
->scl_count
)) ||
487 (rw
== RW_WRITER
&& scl
->scl_writer
== curthread
))
488 locks_held
|= 1 << i
;
495 * ==========================================================================
496 * SPA namespace functions
497 * ==========================================================================
501 * Lookup the named spa_t in the AVL tree. The spa_namespace_lock must be held.
502 * Returns NULL if no matching spa_t is found.
505 spa_lookup(const char *name
)
507 static spa_t search
; /* spa_t is large; don't allocate on stack */
512 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
514 (void) strlcpy(search
.spa_name
, name
, sizeof (search
.spa_name
));
517 * If it's a full dataset name, figure out the pool name and
520 cp
= strpbrk(search
.spa_name
, "/@#");
524 spa
= avl_find(&spa_namespace_avl
, &search
, &where
);
530 * Fires when spa_sync has not completed within zfs_deadman_synctime_ms.
531 * If the zfs_deadman_enabled flag is set then it inspects all vdev queues
532 * looking for potentially hung I/Os.
535 spa_deadman(void *arg
)
539 /* Disable the deadman if the pool is suspended. */
540 if (spa_suspended(spa
))
543 zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu",
544 (gethrtime() - spa
->spa_sync_starttime
) / NANOSEC
,
545 ++spa
->spa_deadman_calls
);
546 if (zfs_deadman_enabled
)
547 vdev_deadman(spa
->spa_root_vdev
);
549 spa
->spa_deadman_tqid
= taskq_dispatch_delay(system_delay_taskq
,
550 spa_deadman
, spa
, TQ_SLEEP
, ddi_get_lbolt() +
551 MSEC_TO_TICK(zfs_deadman_checktime_ms
));
555 * Create an uninitialized spa_t with the given name. Requires
556 * spa_namespace_lock. The caller must ensure that the spa_t doesn't already
557 * exist by calling spa_lookup() first.
560 spa_add(const char *name
, nvlist_t
*config
, const char *altroot
)
563 spa_config_dirent_t
*dp
;
567 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
569 spa
= kmem_zalloc(sizeof (spa_t
), KM_SLEEP
);
571 mutex_init(&spa
->spa_async_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
572 mutex_init(&spa
->spa_errlist_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
573 mutex_init(&spa
->spa_errlog_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
574 mutex_init(&spa
->spa_evicting_os_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
575 mutex_init(&spa
->spa_history_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
576 mutex_init(&spa
->spa_proc_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
577 mutex_init(&spa
->spa_props_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
578 mutex_init(&spa
->spa_cksum_tmpls_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
579 mutex_init(&spa
->spa_scrub_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
580 mutex_init(&spa
->spa_suspend_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
581 mutex_init(&spa
->spa_vdev_top_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
582 mutex_init(&spa
->spa_feat_stats_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
583 mutex_init(&spa
->spa_alloc_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
585 cv_init(&spa
->spa_async_cv
, NULL
, CV_DEFAULT
, NULL
);
586 cv_init(&spa
->spa_evicting_os_cv
, NULL
, CV_DEFAULT
, NULL
);
587 cv_init(&spa
->spa_proc_cv
, NULL
, CV_DEFAULT
, NULL
);
588 cv_init(&spa
->spa_scrub_io_cv
, NULL
, CV_DEFAULT
, NULL
);
589 cv_init(&spa
->spa_suspend_cv
, NULL
, CV_DEFAULT
, NULL
);
591 for (t
= 0; t
< TXG_SIZE
; t
++)
592 bplist_create(&spa
->spa_free_bplist
[t
]);
594 (void) strlcpy(spa
->spa_name
, name
, sizeof (spa
->spa_name
));
595 spa
->spa_state
= POOL_STATE_UNINITIALIZED
;
596 spa
->spa_freeze_txg
= UINT64_MAX
;
597 spa
->spa_final_txg
= UINT64_MAX
;
598 spa
->spa_load_max_txg
= UINT64_MAX
;
600 spa
->spa_proc_state
= SPA_PROC_NONE
;
602 spa
->spa_deadman_synctime
= MSEC2NSEC(zfs_deadman_synctime_ms
);
604 refcount_create(&spa
->spa_refcount
);
605 spa_config_lock_init(spa
);
608 avl_add(&spa_namespace_avl
, spa
);
611 * Set the alternate root, if there is one.
614 spa
->spa_root
= spa_strdup(altroot
);
616 avl_create(&spa
->spa_alloc_tree
, zio_bookmark_compare
,
617 sizeof (zio_t
), offsetof(zio_t
, io_alloc_node
));
620 * Every pool starts with the default cachefile
622 list_create(&spa
->spa_config_list
, sizeof (spa_config_dirent_t
),
623 offsetof(spa_config_dirent_t
, scd_link
));
625 dp
= kmem_zalloc(sizeof (spa_config_dirent_t
), KM_SLEEP
);
626 dp
->scd_path
= altroot
? NULL
: spa_strdup(spa_config_path
);
627 list_insert_head(&spa
->spa_config_list
, dp
);
629 VERIFY(nvlist_alloc(&spa
->spa_load_info
, NV_UNIQUE_NAME
,
632 if (config
!= NULL
) {
635 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_FEATURES_FOR_READ
,
637 VERIFY(nvlist_dup(features
, &spa
->spa_label_features
,
641 VERIFY(nvlist_dup(config
, &spa
->spa_config
, 0) == 0);
644 if (spa
->spa_label_features
== NULL
) {
645 VERIFY(nvlist_alloc(&spa
->spa_label_features
, NV_UNIQUE_NAME
,
649 spa
->spa_debug
= ((zfs_flags
& ZFS_DEBUG_SPA
) != 0);
651 spa
->spa_min_ashift
= INT_MAX
;
652 spa
->spa_max_ashift
= 0;
654 /* Reset cached value */
655 spa
->spa_dedup_dspace
= ~0ULL;
658 * As a pool is being created, treat all features as disabled by
659 * setting SPA_FEATURE_DISABLED for all entries in the feature
662 for (i
= 0; i
< SPA_FEATURES
; i
++) {
663 spa
->spa_feat_refcount_cache
[i
] = SPA_FEATURE_DISABLED
;
670 * Removes a spa_t from the namespace, freeing up any memory used. Requires
671 * spa_namespace_lock. This is called only after the spa_t has been closed and
675 spa_remove(spa_t
*spa
)
677 spa_config_dirent_t
*dp
;
680 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
681 ASSERT(spa
->spa_state
== POOL_STATE_UNINITIALIZED
);
682 ASSERT3U(refcount_count(&spa
->spa_refcount
), ==, 0);
684 nvlist_free(spa
->spa_config_splitting
);
686 avl_remove(&spa_namespace_avl
, spa
);
687 cv_broadcast(&spa_namespace_cv
);
690 spa_strfree(spa
->spa_root
);
692 while ((dp
= list_head(&spa
->spa_config_list
)) != NULL
) {
693 list_remove(&spa
->spa_config_list
, dp
);
694 if (dp
->scd_path
!= NULL
)
695 spa_strfree(dp
->scd_path
);
696 kmem_free(dp
, sizeof (spa_config_dirent_t
));
699 avl_destroy(&spa
->spa_alloc_tree
);
700 list_destroy(&spa
->spa_config_list
);
702 nvlist_free(spa
->spa_label_features
);
703 nvlist_free(spa
->spa_load_info
);
704 nvlist_free(spa
->spa_feat_stats
);
705 spa_config_set(spa
, NULL
);
707 refcount_destroy(&spa
->spa_refcount
);
709 spa_stats_destroy(spa
);
710 spa_config_lock_destroy(spa
);
712 for (t
= 0; t
< TXG_SIZE
; t
++)
713 bplist_destroy(&spa
->spa_free_bplist
[t
]);
715 zio_checksum_templates_free(spa
);
717 cv_destroy(&spa
->spa_async_cv
);
718 cv_destroy(&spa
->spa_evicting_os_cv
);
719 cv_destroy(&spa
->spa_proc_cv
);
720 cv_destroy(&spa
->spa_scrub_io_cv
);
721 cv_destroy(&spa
->spa_suspend_cv
);
723 mutex_destroy(&spa
->spa_alloc_lock
);
724 mutex_destroy(&spa
->spa_async_lock
);
725 mutex_destroy(&spa
->spa_errlist_lock
);
726 mutex_destroy(&spa
->spa_errlog_lock
);
727 mutex_destroy(&spa
->spa_evicting_os_lock
);
728 mutex_destroy(&spa
->spa_history_lock
);
729 mutex_destroy(&spa
->spa_proc_lock
);
730 mutex_destroy(&spa
->spa_props_lock
);
731 mutex_destroy(&spa
->spa_cksum_tmpls_lock
);
732 mutex_destroy(&spa
->spa_scrub_lock
);
733 mutex_destroy(&spa
->spa_suspend_lock
);
734 mutex_destroy(&spa
->spa_vdev_top_lock
);
735 mutex_destroy(&spa
->spa_feat_stats_lock
);
737 kmem_free(spa
, sizeof (spa_t
));
741 * Given a pool, return the next pool in the namespace, or NULL if there is
742 * none. If 'prev' is NULL, return the first pool.
745 spa_next(spa_t
*prev
)
747 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
750 return (AVL_NEXT(&spa_namespace_avl
, prev
));
752 return (avl_first(&spa_namespace_avl
));
756 * ==========================================================================
757 * SPA refcount functions
758 * ==========================================================================
762 * Add a reference to the given spa_t. Must have at least one reference, or
763 * have the namespace lock held.
766 spa_open_ref(spa_t
*spa
, void *tag
)
768 ASSERT(refcount_count(&spa
->spa_refcount
) >= spa
->spa_minref
||
769 MUTEX_HELD(&spa_namespace_lock
));
770 (void) refcount_add(&spa
->spa_refcount
, tag
);
774 * Remove a reference to the given spa_t. Must have at least one reference, or
775 * have the namespace lock held.
778 spa_close(spa_t
*spa
, void *tag
)
780 ASSERT(refcount_count(&spa
->spa_refcount
) > spa
->spa_minref
||
781 MUTEX_HELD(&spa_namespace_lock
));
782 (void) refcount_remove(&spa
->spa_refcount
, tag
);
786 * Remove a reference to the given spa_t held by a dsl dir that is
787 * being asynchronously released. Async releases occur from a taskq
788 * performing eviction of dsl datasets and dirs. The namespace lock
789 * isn't held and the hold by the object being evicted may contribute to
790 * spa_minref (e.g. dataset or directory released during pool export),
791 * so the asserts in spa_close() do not apply.
794 spa_async_close(spa_t
*spa
, void *tag
)
796 (void) refcount_remove(&spa
->spa_refcount
, tag
);
800 * Check to see if the spa refcount is zero. Must be called with
801 * spa_namespace_lock held. We really compare against spa_minref, which is the
802 * number of references acquired when opening a pool
805 spa_refcount_zero(spa_t
*spa
)
807 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
809 return (refcount_count(&spa
->spa_refcount
) == spa
->spa_minref
);
813 * ==========================================================================
814 * SPA spare and l2cache tracking
815 * ==========================================================================
819 * Hot spares and cache devices are tracked using the same code below,
820 * for 'auxiliary' devices.
823 typedef struct spa_aux
{
831 spa_aux_compare(const void *a
, const void *b
)
833 const spa_aux_t
*sa
= (const spa_aux_t
*)a
;
834 const spa_aux_t
*sb
= (const spa_aux_t
*)b
;
836 return (AVL_CMP(sa
->aux_guid
, sb
->aux_guid
));
840 spa_aux_add(vdev_t
*vd
, avl_tree_t
*avl
)
846 search
.aux_guid
= vd
->vdev_guid
;
847 if ((aux
= avl_find(avl
, &search
, &where
)) != NULL
) {
850 aux
= kmem_zalloc(sizeof (spa_aux_t
), KM_SLEEP
);
851 aux
->aux_guid
= vd
->vdev_guid
;
853 avl_insert(avl
, aux
, where
);
858 spa_aux_remove(vdev_t
*vd
, avl_tree_t
*avl
)
864 search
.aux_guid
= vd
->vdev_guid
;
865 aux
= avl_find(avl
, &search
, &where
);
869 if (--aux
->aux_count
== 0) {
870 avl_remove(avl
, aux
);
871 kmem_free(aux
, sizeof (spa_aux_t
));
872 } else if (aux
->aux_pool
== spa_guid(vd
->vdev_spa
)) {
873 aux
->aux_pool
= 0ULL;
878 spa_aux_exists(uint64_t guid
, uint64_t *pool
, int *refcnt
, avl_tree_t
*avl
)
880 spa_aux_t search
, *found
;
882 search
.aux_guid
= guid
;
883 found
= avl_find(avl
, &search
, NULL
);
887 *pool
= found
->aux_pool
;
894 *refcnt
= found
->aux_count
;
899 return (found
!= NULL
);
903 spa_aux_activate(vdev_t
*vd
, avl_tree_t
*avl
)
905 spa_aux_t search
, *found
;
908 search
.aux_guid
= vd
->vdev_guid
;
909 found
= avl_find(avl
, &search
, &where
);
910 ASSERT(found
!= NULL
);
911 ASSERT(found
->aux_pool
== 0ULL);
913 found
->aux_pool
= spa_guid(vd
->vdev_spa
);
917 * Spares are tracked globally due to the following constraints:
919 * - A spare may be part of multiple pools.
920 * - A spare may be added to a pool even if it's actively in use within
922 * - A spare in use in any pool can only be the source of a replacement if
923 * the target is a spare in the same pool.
925 * We keep track of all spares on the system through the use of a reference
926 * counted AVL tree. When a vdev is added as a spare, or used as a replacement
927 * spare, then we bump the reference count in the AVL tree. In addition, we set
928 * the 'vdev_isspare' member to indicate that the device is a spare (active or
929 * inactive). When a spare is made active (used to replace a device in the
930 * pool), we also keep track of which pool its been made a part of.
932 * The 'spa_spare_lock' protects the AVL tree. These functions are normally
933 * called under the spa_namespace lock as part of vdev reconfiguration. The
934 * separate spare lock exists for the status query path, which does not need to
935 * be completely consistent with respect to other vdev configuration changes.
939 spa_spare_compare(const void *a
, const void *b
)
941 return (spa_aux_compare(a
, b
));
945 spa_spare_add(vdev_t
*vd
)
947 mutex_enter(&spa_spare_lock
);
948 ASSERT(!vd
->vdev_isspare
);
949 spa_aux_add(vd
, &spa_spare_avl
);
950 vd
->vdev_isspare
= B_TRUE
;
951 mutex_exit(&spa_spare_lock
);
955 spa_spare_remove(vdev_t
*vd
)
957 mutex_enter(&spa_spare_lock
);
958 ASSERT(vd
->vdev_isspare
);
959 spa_aux_remove(vd
, &spa_spare_avl
);
960 vd
->vdev_isspare
= B_FALSE
;
961 mutex_exit(&spa_spare_lock
);
965 spa_spare_exists(uint64_t guid
, uint64_t *pool
, int *refcnt
)
969 mutex_enter(&spa_spare_lock
);
970 found
= spa_aux_exists(guid
, pool
, refcnt
, &spa_spare_avl
);
971 mutex_exit(&spa_spare_lock
);
977 spa_spare_activate(vdev_t
*vd
)
979 mutex_enter(&spa_spare_lock
);
980 ASSERT(vd
->vdev_isspare
);
981 spa_aux_activate(vd
, &spa_spare_avl
);
982 mutex_exit(&spa_spare_lock
);
986 * Level 2 ARC devices are tracked globally for the same reasons as spares.
987 * Cache devices currently only support one pool per cache device, and so
988 * for these devices the aux reference count is currently unused beyond 1.
992 spa_l2cache_compare(const void *a
, const void *b
)
994 return (spa_aux_compare(a
, b
));
998 spa_l2cache_add(vdev_t
*vd
)
1000 mutex_enter(&spa_l2cache_lock
);
1001 ASSERT(!vd
->vdev_isl2cache
);
1002 spa_aux_add(vd
, &spa_l2cache_avl
);
1003 vd
->vdev_isl2cache
= B_TRUE
;
1004 mutex_exit(&spa_l2cache_lock
);
1008 spa_l2cache_remove(vdev_t
*vd
)
1010 mutex_enter(&spa_l2cache_lock
);
1011 ASSERT(vd
->vdev_isl2cache
);
1012 spa_aux_remove(vd
, &spa_l2cache_avl
);
1013 vd
->vdev_isl2cache
= B_FALSE
;
1014 mutex_exit(&spa_l2cache_lock
);
1018 spa_l2cache_exists(uint64_t guid
, uint64_t *pool
)
1022 mutex_enter(&spa_l2cache_lock
);
1023 found
= spa_aux_exists(guid
, pool
, NULL
, &spa_l2cache_avl
);
1024 mutex_exit(&spa_l2cache_lock
);
1030 spa_l2cache_activate(vdev_t
*vd
)
1032 mutex_enter(&spa_l2cache_lock
);
1033 ASSERT(vd
->vdev_isl2cache
);
1034 spa_aux_activate(vd
, &spa_l2cache_avl
);
1035 mutex_exit(&spa_l2cache_lock
);
1039 * ==========================================================================
1041 * ==========================================================================
1045 * Lock the given spa_t for the purpose of adding or removing a vdev.
1046 * Grabs the global spa_namespace_lock plus the spa config lock for writing.
1047 * It returns the next transaction group for the spa_t.
1050 spa_vdev_enter(spa_t
*spa
)
1052 mutex_enter(&spa
->spa_vdev_top_lock
);
1053 mutex_enter(&spa_namespace_lock
);
1054 return (spa_vdev_config_enter(spa
));
1058 * Internal implementation for spa_vdev_enter(). Used when a vdev
1059 * operation requires multiple syncs (i.e. removing a device) while
1060 * keeping the spa_namespace_lock held.
1063 spa_vdev_config_enter(spa_t
*spa
)
1065 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1067 spa_config_enter(spa
, SCL_ALL
, spa
, RW_WRITER
);
1069 return (spa_last_synced_txg(spa
) + 1);
1073 * Used in combination with spa_vdev_config_enter() to allow the syncing
1074 * of multiple transactions without releasing the spa_namespace_lock.
1077 spa_vdev_config_exit(spa_t
*spa
, vdev_t
*vd
, uint64_t txg
, int error
, char *tag
)
1079 int config_changed
= B_FALSE
;
1081 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1082 ASSERT(txg
> spa_last_synced_txg(spa
));
1084 spa
->spa_pending_vdev
= NULL
;
1087 * Reassess the DTLs.
1089 vdev_dtl_reassess(spa
->spa_root_vdev
, 0, 0, B_FALSE
);
1091 if (error
== 0 && !list_is_empty(&spa
->spa_config_dirty_list
)) {
1092 config_changed
= B_TRUE
;
1093 spa
->spa_config_generation
++;
1097 * Verify the metaslab classes.
1099 ASSERT(metaslab_class_validate(spa_normal_class(spa
)) == 0);
1100 ASSERT(metaslab_class_validate(spa_log_class(spa
)) == 0);
1102 spa_config_exit(spa
, SCL_ALL
, spa
);
1105 * Panic the system if the specified tag requires it. This
1106 * is useful for ensuring that configurations are updated
1109 if (zio_injection_enabled
)
1110 zio_handle_panic_injection(spa
, tag
, 0);
1113 * Note: this txg_wait_synced() is important because it ensures
1114 * that there won't be more than one config change per txg.
1115 * This allows us to use the txg as the generation number.
1118 txg_wait_synced(spa
->spa_dsl_pool
, txg
);
1121 ASSERT(!vd
->vdev_detached
|| vd
->vdev_dtl_sm
== NULL
);
1122 spa_config_enter(spa
, SCL_ALL
, spa
, RW_WRITER
);
1124 spa_config_exit(spa
, SCL_ALL
, spa
);
1128 * If the config changed, update the config cache.
1131 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
1135 * Unlock the spa_t after adding or removing a vdev. Besides undoing the
1136 * locking of spa_vdev_enter(), we also want make sure the transactions have
1137 * synced to disk, and then update the global configuration cache with the new
1141 spa_vdev_exit(spa_t
*spa
, vdev_t
*vd
, uint64_t txg
, int error
)
1143 spa_vdev_config_exit(spa
, vd
, txg
, error
, FTAG
);
1144 mutex_exit(&spa_namespace_lock
);
1145 mutex_exit(&spa
->spa_vdev_top_lock
);
1151 * Lock the given spa_t for the purpose of changing vdev state.
1154 spa_vdev_state_enter(spa_t
*spa
, int oplocks
)
1156 int locks
= SCL_STATE_ALL
| oplocks
;
1159 * Root pools may need to read of the underlying devfs filesystem
1160 * when opening up a vdev. Unfortunately if we're holding the
1161 * SCL_ZIO lock it will result in a deadlock when we try to issue
1162 * the read from the root filesystem. Instead we "prefetch"
1163 * the associated vnodes that we need prior to opening the
1164 * underlying devices and cache them so that we can prevent
1165 * any I/O when we are doing the actual open.
1167 if (spa_is_root(spa
)) {
1168 int low
= locks
& ~(SCL_ZIO
- 1);
1169 int high
= locks
& ~low
;
1171 spa_config_enter(spa
, high
, spa
, RW_WRITER
);
1172 vdev_hold(spa
->spa_root_vdev
);
1173 spa_config_enter(spa
, low
, spa
, RW_WRITER
);
1175 spa_config_enter(spa
, locks
, spa
, RW_WRITER
);
1177 spa
->spa_vdev_locks
= locks
;
1181 spa_vdev_state_exit(spa_t
*spa
, vdev_t
*vd
, int error
)
1183 boolean_t config_changed
= B_FALSE
;
1186 if (vd
== NULL
|| vd
== spa
->spa_root_vdev
) {
1187 vdev_top
= spa
->spa_root_vdev
;
1189 vdev_top
= vd
->vdev_top
;
1192 if (vd
!= NULL
|| error
== 0)
1193 vdev_dtl_reassess(vdev_top
, 0, 0, B_FALSE
);
1196 if (vd
!= spa
->spa_root_vdev
)
1197 vdev_state_dirty(vdev_top
);
1199 config_changed
= B_TRUE
;
1200 spa
->spa_config_generation
++;
1203 if (spa_is_root(spa
))
1204 vdev_rele(spa
->spa_root_vdev
);
1206 ASSERT3U(spa
->spa_vdev_locks
, >=, SCL_STATE_ALL
);
1207 spa_config_exit(spa
, spa
->spa_vdev_locks
, spa
);
1210 * If anything changed, wait for it to sync. This ensures that,
1211 * from the system administrator's perspective, zpool(1M) commands
1212 * are synchronous. This is important for things like zpool offline:
1213 * when the command completes, you expect no further I/O from ZFS.
1216 txg_wait_synced(spa
->spa_dsl_pool
, 0);
1219 * If the config changed, update the config cache.
1221 if (config_changed
) {
1222 mutex_enter(&spa_namespace_lock
);
1223 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
1224 mutex_exit(&spa_namespace_lock
);
1231 * ==========================================================================
1232 * Miscellaneous functions
1233 * ==========================================================================
1237 spa_activate_mos_feature(spa_t
*spa
, const char *feature
, dmu_tx_t
*tx
)
1239 if (!nvlist_exists(spa
->spa_label_features
, feature
)) {
1240 fnvlist_add_boolean(spa
->spa_label_features
, feature
);
1242 * When we are creating the pool (tx_txg==TXG_INITIAL), we can't
1243 * dirty the vdev config because lock SCL_CONFIG is not held.
1244 * Thankfully, in this case we don't need to dirty the config
1245 * because it will be written out anyway when we finish
1246 * creating the pool.
1248 if (tx
->tx_txg
!= TXG_INITIAL
)
1249 vdev_config_dirty(spa
->spa_root_vdev
);
1254 spa_deactivate_mos_feature(spa_t
*spa
, const char *feature
)
1256 if (nvlist_remove_all(spa
->spa_label_features
, feature
) == 0)
1257 vdev_config_dirty(spa
->spa_root_vdev
);
1264 spa_rename(const char *name
, const char *newname
)
1270 * Lookup the spa_t and grab the config lock for writing. We need to
1271 * actually open the pool so that we can sync out the necessary labels.
1272 * It's OK to call spa_open() with the namespace lock held because we
1273 * allow recursive calls for other reasons.
1275 mutex_enter(&spa_namespace_lock
);
1276 if ((err
= spa_open(name
, &spa
, FTAG
)) != 0) {
1277 mutex_exit(&spa_namespace_lock
);
1281 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1283 avl_remove(&spa_namespace_avl
, spa
);
1284 (void) strlcpy(spa
->spa_name
, newname
, sizeof (spa
->spa_name
));
1285 avl_add(&spa_namespace_avl
, spa
);
1288 * Sync all labels to disk with the new names by marking the root vdev
1289 * dirty and waiting for it to sync. It will pick up the new pool name
1292 vdev_config_dirty(spa
->spa_root_vdev
);
1294 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1296 txg_wait_synced(spa
->spa_dsl_pool
, 0);
1299 * Sync the updated config cache.
1301 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
1303 spa_close(spa
, FTAG
);
1305 mutex_exit(&spa_namespace_lock
);
1311 * Return the spa_t associated with given pool_guid, if it exists. If
1312 * device_guid is non-zero, determine whether the pool exists *and* contains
1313 * a device with the specified device_guid.
1316 spa_by_guid(uint64_t pool_guid
, uint64_t device_guid
)
1319 avl_tree_t
*t
= &spa_namespace_avl
;
1321 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1323 for (spa
= avl_first(t
); spa
!= NULL
; spa
= AVL_NEXT(t
, spa
)) {
1324 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
)
1326 if (spa
->spa_root_vdev
== NULL
)
1328 if (spa_guid(spa
) == pool_guid
) {
1329 if (device_guid
== 0)
1332 if (vdev_lookup_by_guid(spa
->spa_root_vdev
,
1333 device_guid
) != NULL
)
1337 * Check any devices we may be in the process of adding.
1339 if (spa
->spa_pending_vdev
) {
1340 if (vdev_lookup_by_guid(spa
->spa_pending_vdev
,
1341 device_guid
) != NULL
)
1351 * Determine whether a pool with the given pool_guid exists.
1354 spa_guid_exists(uint64_t pool_guid
, uint64_t device_guid
)
1356 return (spa_by_guid(pool_guid
, device_guid
) != NULL
);
1360 spa_strdup(const char *s
)
1366 new = kmem_alloc(len
+ 1, KM_SLEEP
);
1374 spa_strfree(char *s
)
1376 kmem_free(s
, strlen(s
) + 1);
1380 spa_get_random(uint64_t range
)
1386 (void) random_get_pseudo_bytes((void *)&r
, sizeof (uint64_t));
1392 spa_generate_guid(spa_t
*spa
)
1394 uint64_t guid
= spa_get_random(-1ULL);
1397 while (guid
== 0 || spa_guid_exists(spa_guid(spa
), guid
))
1398 guid
= spa_get_random(-1ULL);
1400 while (guid
== 0 || spa_guid_exists(guid
, 0))
1401 guid
= spa_get_random(-1ULL);
1408 snprintf_blkptr(char *buf
, size_t buflen
, const blkptr_t
*bp
)
1411 char *checksum
= NULL
;
1412 char *compress
= NULL
;
1415 if (BP_GET_TYPE(bp
) & DMU_OT_NEWTYPE
) {
1416 dmu_object_byteswap_t bswap
=
1417 DMU_OT_BYTESWAP(BP_GET_TYPE(bp
));
1418 (void) snprintf(type
, sizeof (type
), "bswap %s %s",
1419 DMU_OT_IS_METADATA(BP_GET_TYPE(bp
)) ?
1420 "metadata" : "data",
1421 dmu_ot_byteswap
[bswap
].ob_name
);
1423 (void) strlcpy(type
, dmu_ot
[BP_GET_TYPE(bp
)].ot_name
,
1426 if (!BP_IS_EMBEDDED(bp
)) {
1428 zio_checksum_table
[BP_GET_CHECKSUM(bp
)].ci_name
;
1430 compress
= zio_compress_table
[BP_GET_COMPRESS(bp
)].ci_name
;
1433 SNPRINTF_BLKPTR(snprintf
, ' ', buf
, buflen
, bp
, type
, checksum
,
1438 spa_freeze(spa_t
*spa
)
1440 uint64_t freeze_txg
= 0;
1442 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1443 if (spa
->spa_freeze_txg
== UINT64_MAX
) {
1444 freeze_txg
= spa_last_synced_txg(spa
) + TXG_SIZE
;
1445 spa
->spa_freeze_txg
= freeze_txg
;
1447 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1448 if (freeze_txg
!= 0)
1449 txg_wait_synced(spa_get_dsl(spa
), freeze_txg
);
1453 zfs_panic_recover(const char *fmt
, ...)
1458 vcmn_err(zfs_recover
? CE_WARN
: CE_PANIC
, fmt
, adx
);
1463 * This is a stripped-down version of strtoull, suitable only for converting
1464 * lowercase hexadecimal numbers that don't overflow.
1467 strtonum(const char *str
, char **nptr
)
1473 while ((c
= *str
) != '\0') {
1474 if (c
>= '0' && c
<= '9')
1476 else if (c
>= 'a' && c
<= 'f')
1477 digit
= 10 + c
- 'a';
1488 *nptr
= (char *)str
;
1494 * ==========================================================================
1495 * Accessor functions
1496 * ==========================================================================
1500 spa_shutting_down(spa_t
*spa
)
1502 return (spa
->spa_async_suspended
);
1506 spa_get_dsl(spa_t
*spa
)
1508 return (spa
->spa_dsl_pool
);
1512 spa_is_initializing(spa_t
*spa
)
1514 return (spa
->spa_is_initializing
);
1518 spa_get_rootblkptr(spa_t
*spa
)
1520 return (&spa
->spa_ubsync
.ub_rootbp
);
1524 spa_set_rootblkptr(spa_t
*spa
, const blkptr_t
*bp
)
1526 spa
->spa_uberblock
.ub_rootbp
= *bp
;
1530 spa_altroot(spa_t
*spa
, char *buf
, size_t buflen
)
1532 if (spa
->spa_root
== NULL
)
1535 (void) strncpy(buf
, spa
->spa_root
, buflen
);
1539 spa_sync_pass(spa_t
*spa
)
1541 return (spa
->spa_sync_pass
);
1545 spa_name(spa_t
*spa
)
1547 return (spa
->spa_name
);
1551 spa_guid(spa_t
*spa
)
1553 dsl_pool_t
*dp
= spa_get_dsl(spa
);
1557 * If we fail to parse the config during spa_load(), we can go through
1558 * the error path (which posts an ereport) and end up here with no root
1559 * vdev. We stash the original pool guid in 'spa_config_guid' to handle
1562 if (spa
->spa_root_vdev
== NULL
)
1563 return (spa
->spa_config_guid
);
1565 guid
= spa
->spa_last_synced_guid
!= 0 ?
1566 spa
->spa_last_synced_guid
: spa
->spa_root_vdev
->vdev_guid
;
1569 * Return the most recently synced out guid unless we're
1570 * in syncing context.
1572 if (dp
&& dsl_pool_sync_context(dp
))
1573 return (spa
->spa_root_vdev
->vdev_guid
);
1579 spa_load_guid(spa_t
*spa
)
1582 * This is a GUID that exists solely as a reference for the
1583 * purposes of the arc. It is generated at load time, and
1584 * is never written to persistent storage.
1586 return (spa
->spa_load_guid
);
1590 spa_last_synced_txg(spa_t
*spa
)
1592 return (spa
->spa_ubsync
.ub_txg
);
1596 spa_first_txg(spa_t
*spa
)
1598 return (spa
->spa_first_txg
);
1602 spa_syncing_txg(spa_t
*spa
)
1604 return (spa
->spa_syncing_txg
);
1608 * Return the last txg where data can be dirtied. The final txgs
1609 * will be used to just clear out any deferred frees that remain.
1612 spa_final_dirty_txg(spa_t
*spa
)
1614 return (spa
->spa_final_txg
- TXG_DEFER_SIZE
);
1618 spa_state(spa_t
*spa
)
1620 return (spa
->spa_state
);
1624 spa_load_state(spa_t
*spa
)
1626 return (spa
->spa_load_state
);
1630 spa_freeze_txg(spa_t
*spa
)
1632 return (spa
->spa_freeze_txg
);
1636 * Return the inflated asize for a logical write in bytes. This is used by the
1637 * DMU to calculate the space a logical write will require on disk.
1638 * If lsize is smaller than the largest physical block size allocatable on this
1639 * pool we use its value instead, since the write will end up using the whole
1643 spa_get_worst_case_asize(spa_t
*spa
, uint64_t lsize
)
1646 return (0); /* No inflation needed */
1647 return (MAX(lsize
, 1 << spa
->spa_max_ashift
) * spa_asize_inflation
);
1651 * Return the amount of slop space in bytes. It is 1/32 of the pool (3.2%),
1652 * or at least 128MB, unless that would cause it to be more than half the
1655 * See the comment above spa_slop_shift for details.
1658 spa_get_slop_space(spa_t
*spa
)
1660 uint64_t space
= spa_get_dspace(spa
);
1661 return (MAX(space
>> spa_slop_shift
, MIN(space
>> 1, spa_min_slop
)));
1665 spa_get_dspace(spa_t
*spa
)
1667 return (spa
->spa_dspace
);
1671 spa_update_dspace(spa_t
*spa
)
1673 spa
->spa_dspace
= metaslab_class_get_dspace(spa_normal_class(spa
)) +
1674 ddt_get_dedup_dspace(spa
);
1678 * Return the failure mode that has been set to this pool. The default
1679 * behavior will be to block all I/Os when a complete failure occurs.
1682 spa_get_failmode(spa_t
*spa
)
1684 return (spa
->spa_failmode
);
1688 spa_suspended(spa_t
*spa
)
1690 return (spa
->spa_suspended
);
1694 spa_version(spa_t
*spa
)
1696 return (spa
->spa_ubsync
.ub_version
);
1700 spa_deflate(spa_t
*spa
)
1702 return (spa
->spa_deflate
);
1706 spa_normal_class(spa_t
*spa
)
1708 return (spa
->spa_normal_class
);
1712 spa_log_class(spa_t
*spa
)
1714 return (spa
->spa_log_class
);
1718 spa_evicting_os_register(spa_t
*spa
, objset_t
*os
)
1720 mutex_enter(&spa
->spa_evicting_os_lock
);
1721 list_insert_head(&spa
->spa_evicting_os_list
, os
);
1722 mutex_exit(&spa
->spa_evicting_os_lock
);
1726 spa_evicting_os_deregister(spa_t
*spa
, objset_t
*os
)
1728 mutex_enter(&spa
->spa_evicting_os_lock
);
1729 list_remove(&spa
->spa_evicting_os_list
, os
);
1730 cv_broadcast(&spa
->spa_evicting_os_cv
);
1731 mutex_exit(&spa
->spa_evicting_os_lock
);
1735 spa_evicting_os_wait(spa_t
*spa
)
1737 mutex_enter(&spa
->spa_evicting_os_lock
);
1738 while (!list_is_empty(&spa
->spa_evicting_os_list
))
1739 cv_wait(&spa
->spa_evicting_os_cv
, &spa
->spa_evicting_os_lock
);
1740 mutex_exit(&spa
->spa_evicting_os_lock
);
1742 dmu_buf_user_evict_wait();
1746 spa_max_replication(spa_t
*spa
)
1749 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
1750 * handle BPs with more than one DVA allocated. Set our max
1751 * replication level accordingly.
1753 if (spa_version(spa
) < SPA_VERSION_DITTO_BLOCKS
)
1755 return (MIN(SPA_DVAS_PER_BP
, spa_max_replication_override
));
1759 spa_prev_software_version(spa_t
*spa
)
1761 return (spa
->spa_prev_software_version
);
1765 spa_deadman_synctime(spa_t
*spa
)
1767 return (spa
->spa_deadman_synctime
);
1771 dva_get_dsize_sync(spa_t
*spa
, const dva_t
*dva
)
1773 uint64_t asize
= DVA_GET_ASIZE(dva
);
1774 uint64_t dsize
= asize
;
1776 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_READER
) != 0);
1778 if (asize
!= 0 && spa
->spa_deflate
) {
1779 vdev_t
*vd
= vdev_lookup_top(spa
, DVA_GET_VDEV(dva
));
1781 dsize
= (asize
>> SPA_MINBLOCKSHIFT
) *
1782 vd
->vdev_deflate_ratio
;
1789 bp_get_dsize_sync(spa_t
*spa
, const blkptr_t
*bp
)
1794 for (d
= 0; d
< BP_GET_NDVAS(bp
); d
++)
1795 dsize
+= dva_get_dsize_sync(spa
, &bp
->blk_dva
[d
]);
1801 bp_get_dsize(spa_t
*spa
, const blkptr_t
*bp
)
1806 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
1808 for (d
= 0; d
< BP_GET_NDVAS(bp
); d
++)
1809 dsize
+= dva_get_dsize_sync(spa
, &bp
->blk_dva
[d
]);
1811 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
1817 * ==========================================================================
1818 * Initialization and Termination
1819 * ==========================================================================
1823 spa_name_compare(const void *a1
, const void *a2
)
1825 const spa_t
*s1
= a1
;
1826 const spa_t
*s2
= a2
;
1829 s
= strcmp(s1
->spa_name
, s2
->spa_name
);
1831 return (AVL_ISIGN(s
));
1843 mutex_init(&spa_namespace_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1844 mutex_init(&spa_spare_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1845 mutex_init(&spa_l2cache_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1846 cv_init(&spa_namespace_cv
, NULL
, CV_DEFAULT
, NULL
);
1848 avl_create(&spa_namespace_avl
, spa_name_compare
, sizeof (spa_t
),
1849 offsetof(spa_t
, spa_avl
));
1851 avl_create(&spa_spare_avl
, spa_spare_compare
, sizeof (spa_aux_t
),
1852 offsetof(spa_aux_t
, aux_avl
));
1854 avl_create(&spa_l2cache_avl
, spa_l2cache_compare
, sizeof (spa_aux_t
),
1855 offsetof(spa_aux_t
, aux_avl
));
1857 spa_mode_global
= mode
;
1860 if (spa_mode_global
!= FREAD
&& dprintf_find_string("watch")) {
1861 struct sigaction sa
;
1863 sa
.sa_flags
= SA_SIGINFO
;
1864 sigemptyset(&sa
.sa_mask
);
1865 sa
.sa_sigaction
= arc_buf_sigsegv
;
1867 if (sigaction(SIGSEGV
, &sa
, NULL
) == -1) {
1868 perror("could not enable watchpoints: "
1869 "sigaction(SIGSEGV, ...) = ");
1880 metaslab_alloc_trace_init();
1885 vdev_cache_stat_init();
1886 vdev_raidz_math_init();
1890 zpool_feature_init();
1904 vdev_cache_stat_fini();
1905 vdev_raidz_math_fini();
1910 metaslab_alloc_trace_fini();
1917 avl_destroy(&spa_namespace_avl
);
1918 avl_destroy(&spa_spare_avl
);
1919 avl_destroy(&spa_l2cache_avl
);
1921 cv_destroy(&spa_namespace_cv
);
1922 mutex_destroy(&spa_namespace_lock
);
1923 mutex_destroy(&spa_spare_lock
);
1924 mutex_destroy(&spa_l2cache_lock
);
1928 * Return whether this pool has slogs. No locking needed.
1929 * It's not a problem if the wrong answer is returned as it's only for
1930 * performance and not correctness
1933 spa_has_slogs(spa_t
*spa
)
1935 return (spa
->spa_log_class
->mc_rotor
!= NULL
);
1939 spa_get_log_state(spa_t
*spa
)
1941 return (spa
->spa_log_state
);
1945 spa_set_log_state(spa_t
*spa
, spa_log_state_t state
)
1947 spa
->spa_log_state
= state
;
1951 spa_is_root(spa_t
*spa
)
1953 return (spa
->spa_is_root
);
1957 spa_writeable(spa_t
*spa
)
1959 return (!!(spa
->spa_mode
& FWRITE
));
1963 * Returns true if there is a pending sync task in any of the current
1964 * syncing txg, the current quiescing txg, or the current open txg.
1967 spa_has_pending_synctask(spa_t
*spa
)
1969 return (!txg_all_lists_empty(&spa
->spa_dsl_pool
->dp_sync_tasks
));
1973 spa_mode(spa_t
*spa
)
1975 return (spa
->spa_mode
);
1979 spa_bootfs(spa_t
*spa
)
1981 return (spa
->spa_bootfs
);
1985 spa_delegation(spa_t
*spa
)
1987 return (spa
->spa_delegation
);
1991 spa_meta_objset(spa_t
*spa
)
1993 return (spa
->spa_meta_objset
);
1997 spa_dedup_checksum(spa_t
*spa
)
1999 return (spa
->spa_dedup_checksum
);
2003 * Reset pool scan stat per scan pass (or reboot).
2006 spa_scan_stat_init(spa_t
*spa
)
2008 /* data not stored on disk */
2009 spa
->spa_scan_pass_start
= gethrestime_sec();
2010 spa
->spa_scan_pass_exam
= 0;
2011 vdev_scan_stat_init(spa
->spa_root_vdev
);
2015 * Get scan stats for zpool status reports
2018 spa_scan_get_stats(spa_t
*spa
, pool_scan_stat_t
*ps
)
2020 dsl_scan_t
*scn
= spa
->spa_dsl_pool
? spa
->spa_dsl_pool
->dp_scan
: NULL
;
2022 if (scn
== NULL
|| scn
->scn_phys
.scn_func
== POOL_SCAN_NONE
)
2023 return (SET_ERROR(ENOENT
));
2024 bzero(ps
, sizeof (pool_scan_stat_t
));
2026 /* data stored on disk */
2027 ps
->pss_func
= scn
->scn_phys
.scn_func
;
2028 ps
->pss_start_time
= scn
->scn_phys
.scn_start_time
;
2029 ps
->pss_end_time
= scn
->scn_phys
.scn_end_time
;
2030 ps
->pss_to_examine
= scn
->scn_phys
.scn_to_examine
;
2031 ps
->pss_examined
= scn
->scn_phys
.scn_examined
;
2032 ps
->pss_to_process
= scn
->scn_phys
.scn_to_process
;
2033 ps
->pss_processed
= scn
->scn_phys
.scn_processed
;
2034 ps
->pss_errors
= scn
->scn_phys
.scn_errors
;
2035 ps
->pss_state
= scn
->scn_phys
.scn_state
;
2037 /* data not stored on disk */
2038 ps
->pss_pass_start
= spa
->spa_scan_pass_start
;
2039 ps
->pss_pass_exam
= spa
->spa_scan_pass_exam
;
2045 spa_debug_enabled(spa_t
*spa
)
2047 return (spa
->spa_debug
);
2051 spa_maxblocksize(spa_t
*spa
)
2053 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_BLOCKS
))
2054 return (SPA_MAXBLOCKSIZE
);
2056 return (SPA_OLD_MAXBLOCKSIZE
);
2060 spa_maxdnodesize(spa_t
*spa
)
2062 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_DNODE
))
2063 return (DNODE_MAX_SIZE
);
2065 return (DNODE_MIN_SIZE
);
2068 #if defined(_KERNEL) && defined(HAVE_SPL)
2069 /* Namespace manipulation */
2070 EXPORT_SYMBOL(spa_lookup
);
2071 EXPORT_SYMBOL(spa_add
);
2072 EXPORT_SYMBOL(spa_remove
);
2073 EXPORT_SYMBOL(spa_next
);
2075 /* Refcount functions */
2076 EXPORT_SYMBOL(spa_open_ref
);
2077 EXPORT_SYMBOL(spa_close
);
2078 EXPORT_SYMBOL(spa_refcount_zero
);
2080 /* Pool configuration lock */
2081 EXPORT_SYMBOL(spa_config_tryenter
);
2082 EXPORT_SYMBOL(spa_config_enter
);
2083 EXPORT_SYMBOL(spa_config_exit
);
2084 EXPORT_SYMBOL(spa_config_held
);
2086 /* Pool vdev add/remove lock */
2087 EXPORT_SYMBOL(spa_vdev_enter
);
2088 EXPORT_SYMBOL(spa_vdev_exit
);
2090 /* Pool vdev state change lock */
2091 EXPORT_SYMBOL(spa_vdev_state_enter
);
2092 EXPORT_SYMBOL(spa_vdev_state_exit
);
2094 /* Accessor functions */
2095 EXPORT_SYMBOL(spa_shutting_down
);
2096 EXPORT_SYMBOL(spa_get_dsl
);
2097 EXPORT_SYMBOL(spa_get_rootblkptr
);
2098 EXPORT_SYMBOL(spa_set_rootblkptr
);
2099 EXPORT_SYMBOL(spa_altroot
);
2100 EXPORT_SYMBOL(spa_sync_pass
);
2101 EXPORT_SYMBOL(spa_name
);
2102 EXPORT_SYMBOL(spa_guid
);
2103 EXPORT_SYMBOL(spa_last_synced_txg
);
2104 EXPORT_SYMBOL(spa_first_txg
);
2105 EXPORT_SYMBOL(spa_syncing_txg
);
2106 EXPORT_SYMBOL(spa_version
);
2107 EXPORT_SYMBOL(spa_state
);
2108 EXPORT_SYMBOL(spa_load_state
);
2109 EXPORT_SYMBOL(spa_freeze_txg
);
2110 EXPORT_SYMBOL(spa_get_dspace
);
2111 EXPORT_SYMBOL(spa_update_dspace
);
2112 EXPORT_SYMBOL(spa_deflate
);
2113 EXPORT_SYMBOL(spa_normal_class
);
2114 EXPORT_SYMBOL(spa_log_class
);
2115 EXPORT_SYMBOL(spa_max_replication
);
2116 EXPORT_SYMBOL(spa_prev_software_version
);
2117 EXPORT_SYMBOL(spa_get_failmode
);
2118 EXPORT_SYMBOL(spa_suspended
);
2119 EXPORT_SYMBOL(spa_bootfs
);
2120 EXPORT_SYMBOL(spa_delegation
);
2121 EXPORT_SYMBOL(spa_meta_objset
);
2122 EXPORT_SYMBOL(spa_maxblocksize
);
2123 EXPORT_SYMBOL(spa_maxdnodesize
);
2125 /* Miscellaneous support routines */
2126 EXPORT_SYMBOL(spa_rename
);
2127 EXPORT_SYMBOL(spa_guid_exists
);
2128 EXPORT_SYMBOL(spa_strdup
);
2129 EXPORT_SYMBOL(spa_strfree
);
2130 EXPORT_SYMBOL(spa_get_random
);
2131 EXPORT_SYMBOL(spa_generate_guid
);
2132 EXPORT_SYMBOL(snprintf_blkptr
);
2133 EXPORT_SYMBOL(spa_freeze
);
2134 EXPORT_SYMBOL(spa_upgrade
);
2135 EXPORT_SYMBOL(spa_evict_all
);
2136 EXPORT_SYMBOL(spa_lookup_by_guid
);
2137 EXPORT_SYMBOL(spa_has_spare
);
2138 EXPORT_SYMBOL(dva_get_dsize_sync
);
2139 EXPORT_SYMBOL(bp_get_dsize_sync
);
2140 EXPORT_SYMBOL(bp_get_dsize
);
2141 EXPORT_SYMBOL(spa_has_slogs
);
2142 EXPORT_SYMBOL(spa_is_root
);
2143 EXPORT_SYMBOL(spa_writeable
);
2144 EXPORT_SYMBOL(spa_mode
);
2145 EXPORT_SYMBOL(spa_namespace_lock
);
2148 module_param(zfs_flags
, uint
, 0644);
2149 MODULE_PARM_DESC(zfs_flags
, "Set additional debugging flags");
2151 module_param(zfs_recover
, int, 0644);
2152 MODULE_PARM_DESC(zfs_recover
, "Set to attempt to recover from fatal errors");
2154 module_param(zfs_free_leak_on_eio
, int, 0644);
2155 MODULE_PARM_DESC(zfs_free_leak_on_eio
,
2156 "Set to ignore IO errors during free and permanently leak the space");
2158 module_param(zfs_deadman_synctime_ms
, ulong
, 0644);
2159 MODULE_PARM_DESC(zfs_deadman_synctime_ms
, "Expiration time in milliseconds");
2161 module_param(zfs_deadman_checktime_ms
, ulong
, 0644);
2162 MODULE_PARM_DESC(zfs_deadman_checktime_ms
,
2163 "Dead I/O check interval in milliseconds");
2165 module_param(zfs_deadman_enabled
, int, 0644);
2166 MODULE_PARM_DESC(zfs_deadman_enabled
, "Enable deadman timer");
2168 module_param(spa_asize_inflation
, int, 0644);
2169 MODULE_PARM_DESC(spa_asize_inflation
,
2170 "SPA size estimate multiplication factor");
2172 module_param(spa_slop_shift
, int, 0644);
2173 MODULE_PARM_DESC(spa_slop_shift
, "Reserved free space in pool");