1 /*
2 * CDDL HEADER START
3 *
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.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2011, 2024 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.
27 * Copyright (c) 2017 Datto Inc.
28 * Copyright (c) 2017, Intel Corporation.
29 * Copyright (c) 2019, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
30 * Copyright (c) 2023, Klara Inc.
31 */
32
33 #include <sys/zfs_context.h>
34 #include <sys/zfs_chksum.h>
35 #include <sys/spa_impl.h>
36 #include <sys/zio.h>
37 #include <sys/zio_checksum.h>
38 #include <sys/zio_compress.h>
39 #include <sys/dmu.h>
40 #include <sys/dmu_tx.h>
41 #include <sys/zap.h>
42 #include <sys/zil.h>
43 #include <sys/vdev_impl.h>
44 #include <sys/vdev_initialize.h>
45 #include <sys/vdev_trim.h>
46 #include <sys/vdev_file.h>
47 #include <sys/vdev_raidz.h>
48 #include <sys/metaslab.h>
49 #include <sys/uberblock_impl.h>
50 #include <sys/txg.h>
51 #include <sys/avl.h>
52 #include <sys/unique.h>
53 #include <sys/dsl_pool.h>
54 #include <sys/dsl_dir.h>
55 #include <sys/dsl_prop.h>
56 #include <sys/fm/util.h>
57 #include <sys/dsl_scan.h>
58 #include <sys/fs/zfs.h>
59 #include <sys/metaslab_impl.h>
60 #include <sys/arc.h>
61 #include <sys/brt.h>
62 #include <sys/ddt.h>
63 #include <sys/kstat.h>
64 #include "zfs_prop.h"
65 #include <sys/btree.h>
66 #include <sys/zfeature.h>
67 #include <sys/qat.h>
68 #include <sys/zstd/zstd.h>
69
70 /*
71 * SPA locking
72 *
73 * There are three basic locks for managing spa_t structures:
74 *
75 * spa_namespace_lock (global mutex)
76 *
77 * This lock must be acquired to do any of the following:
78 *
79 * - Lookup a spa_t by name
80 * - Add or remove a spa_t from the namespace
81 * - Increase spa_refcount from non-zero
82 * - Check if spa_refcount is zero
83 * - Rename a spa_t
84 * - add/remove/attach/detach devices
85 * - Held for the duration of create/destroy/export
86 * - Held at the start and end of import
87 *
88 * It does not need to handle recursion. A create or destroy may
89 * reference objects (files or zvols) in other pools, but by
90 * definition they must have an existing reference, and will never need
91 * to lookup a spa_t by name.
92 *
93 * spa_refcount (per-spa zfs_refcount_t protected by mutex)
94 *
95 * This reference count keep track of any active users of the spa_t. The
96 * spa_t cannot be destroyed or freed while this is non-zero. Internally,
97 * the refcount is never really 'zero' - opening a pool implicitly keeps
98 * some references in the DMU. Internally we check against spa_minref, but
99 * present the image of a zero/non-zero value to consumers.
100 *
101 * spa_config_lock[] (per-spa array of rwlocks)
102 *
103 * This protects the spa_t from config changes, and must be held in
104 * the following circumstances:
105 *
106 * - RW_READER to perform I/O to the spa
107 * - RW_WRITER to change the vdev config
108 *
109 * The locking order is fairly straightforward:
110 *
111 * spa_namespace_lock -> spa_refcount
112 *
113 * The namespace lock must be acquired to increase the refcount from 0
114 * or to check if it is zero.
115 *
116 * spa_refcount -> spa_config_lock[]
117 *
118 * There must be at least one valid reference on the spa_t to acquire
119 * the config lock.
120 *
121 * spa_namespace_lock -> spa_config_lock[]
122 *
123 * The namespace lock must always be taken before the config lock.
124 *
125 *
126 * The spa_namespace_lock can be acquired directly and is globally visible.
127 *
128 * The namespace is manipulated using the following functions, all of which
129 * require the spa_namespace_lock to be held.
130 *
131 * spa_lookup() Lookup a spa_t by name.
132 *
133 * spa_add() Create a new spa_t in the namespace.
134 *
135 * spa_remove() Remove a spa_t from the namespace. This also
136 * frees up any memory associated with the spa_t.
137 *
138 * spa_next() Returns the next spa_t in the system, or the
139 * first if NULL is passed.
140 *
141 * spa_evict_all() Shutdown and remove all spa_t structures in
142 * the system.
143 *
144 * spa_guid_exists() Determine whether a pool/device guid exists.
145 *
146 * The spa_refcount is manipulated using the following functions:
147 *
148 * spa_open_ref() Adds a reference to the given spa_t. Must be
149 * called with spa_namespace_lock held if the
150 * refcount is currently zero.
151 *
152 * spa_close() Remove a reference from the spa_t. This will
153 * not free the spa_t or remove it from the
154 * namespace. No locking is required.
155 *
156 * spa_refcount_zero() Returns true if the refcount is currently
157 * zero. Must be called with spa_namespace_lock
158 * held.
159 *
160 * The spa_config_lock[] is an array of rwlocks, ordered as follows:
161 * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV.
162 * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}().
163 *
164 * To read the configuration, it suffices to hold one of these locks as reader.
165 * To modify the configuration, you must hold all locks as writer. To modify
166 * vdev state without altering the vdev tree's topology (e.g. online/offline),
167 * you must hold SCL_STATE and SCL_ZIO as writer.
168 *
169 * We use these distinct config locks to avoid recursive lock entry.
170 * For example, spa_sync() (which holds SCL_CONFIG as reader) induces
171 * block allocations (SCL_ALLOC), which may require reading space maps
172 * from disk (dmu_read() -> zio_read() -> SCL_ZIO).
173 *
174 * The spa config locks cannot be normal rwlocks because we need the
175 * ability to hand off ownership. For example, SCL_ZIO is acquired
176 * by the issuing thread and later released by an interrupt thread.
177 * They do, however, obey the usual write-wanted semantics to prevent
178 * writer (i.e. system administrator) starvation.
179 *
180 * The lock acquisition rules are as follows:
181 *
182 * SCL_CONFIG
183 * Protects changes to the vdev tree topology, such as vdev
184 * add/remove/attach/detach. Protects the dirty config list
185 * (spa_config_dirty_list) and the set of spares and l2arc devices.
186 *
187 * SCL_STATE
188 * Protects changes to pool state and vdev state, such as vdev
189 * online/offline/fault/degrade/clear. Protects the dirty state list
190 * (spa_state_dirty_list) and global pool state (spa_state).
191 *
192 * SCL_ALLOC
193 * Protects changes to metaslab groups and classes.
194 * Held as reader by metaslab_alloc() and metaslab_claim().
195 *
196 * SCL_ZIO
197 * Held by bp-level zios (those which have no io_vd upon entry)
198 * to prevent changes to the vdev tree. The bp-level zio implicitly
199 * protects all of its vdev child zios, which do not hold SCL_ZIO.
200 *
201 * SCL_FREE
202 * Protects changes to metaslab groups and classes.
203 * Held as reader by metaslab_free(). SCL_FREE is distinct from
204 * SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free
205 * blocks in zio_done() while another i/o that holds either
206 * SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete.
207 *
208 * SCL_VDEV
209 * Held as reader to prevent changes to the vdev tree during trivial
210 * inquiries such as bp_get_dsize(). SCL_VDEV is distinct from the
211 * other locks, and lower than all of them, to ensure that it's safe
212 * to acquire regardless of caller context.
213 *
214 * In addition, the following rules apply:
215 *
216 * (a) spa_props_lock protects pool properties, spa_config and spa_config_list.
217 * The lock ordering is SCL_CONFIG > spa_props_lock.
218 *
219 * (b) I/O operations on leaf vdevs. For any zio operation that takes
220 * an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(),
221 * or zio_write_phys() -- the caller must ensure that the config cannot
222 * cannot change in the interim, and that the vdev cannot be reopened.
223 * SCL_STATE as reader suffices for both.
224 *
225 * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
226 *
227 * spa_vdev_enter() Acquire the namespace lock and the config lock
228 * for writing.
229 *
230 * spa_vdev_exit() Release the config lock, wait for all I/O
231 * to complete, sync the updated configs to the
232 * cache, and release the namespace lock.
233 *
234 * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit().
235 * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual
236 * locking is, always, based on spa_namespace_lock and spa_config_lock[].
237 */
238
239 avl_tree_t spa_namespace_avl;
240 kmutex_t spa_namespace_lock;
241 kcondvar_t spa_namespace_cv;
242 static const int spa_max_replication_override = SPA_DVAS_PER_BP;
243
244 static kmutex_t spa_spare_lock;
245 static avl_tree_t spa_spare_avl;
246 static kmutex_t spa_l2cache_lock;
247 static avl_tree_t spa_l2cache_avl;
248
249 spa_mode_t spa_mode_global = SPA_MODE_UNINIT;
250
251 #ifdef ZFS_DEBUG
252 /*
253 * Everything except dprintf, set_error, spa, and indirect_remap is on
254 * by default in debug builds.
255 */
256 int zfs_flags = ~(ZFS_DEBUG_DPRINTF | ZFS_DEBUG_SET_ERROR |
257 ZFS_DEBUG_INDIRECT_REMAP);
258 #else
259 int zfs_flags = 0;
260 #endif
261
262 /*
263 * zfs_recover can be set to nonzero to attempt to recover from
264 * otherwise-fatal errors, typically caused by on-disk corruption. When
265 * set, calls to zfs_panic_recover() will turn into warning messages.
266 * This should only be used as a last resort, as it typically results
267 * in leaked space, or worse.
268 */
269 int zfs_recover = B_FALSE;
270
271 /*
272 * If destroy encounters an EIO while reading metadata (e.g. indirect
273 * blocks), space referenced by the missing metadata can not be freed.
274 * Normally this causes the background destroy to become "stalled", as
275 * it is unable to make forward progress. While in this stalled state,
276 * all remaining space to free from the error-encountering filesystem is
277 * "temporarily leaked". Set this flag to cause it to ignore the EIO,
278 * permanently leak the space from indirect blocks that can not be read,
279 * and continue to free everything else that it can.
280 *
281 * The default, "stalling" behavior is useful if the storage partially
282 * fails (i.e. some but not all i/os fail), and then later recovers. In
283 * this case, we will be able to continue pool operations while it is
284 * partially failed, and when it recovers, we can continue to free the
285 * space, with no leaks. However, note that this case is actually
286 * fairly rare.
287 *
288 * Typically pools either (a) fail completely (but perhaps temporarily,
289 * e.g. a top-level vdev going offline), or (b) have localized,
290 * permanent errors (e.g. disk returns the wrong data due to bit flip or
291 * firmware bug). In case (a), this setting does not matter because the
292 * pool will be suspended and the sync thread will not be able to make
293 * forward progress regardless. In case (b), because the error is
294 * permanent, the best we can do is leak the minimum amount of space,
295 * which is what setting this flag will do. Therefore, it is reasonable
296 * for this flag to normally be set, but we chose the more conservative
297 * approach of not setting it, so that there is no possibility of
298 * leaking space in the "partial temporary" failure case.
299 */
300 int zfs_free_leak_on_eio = B_FALSE;
301
302 /*
303 * Expiration time in milliseconds. This value has two meanings. First it is
304 * used to determine when the spa_deadman() logic should fire. By default the
305 * spa_deadman() will fire if spa_sync() has not completed in 600 seconds.
306 * Secondly, the value determines if an I/O is considered "hung". Any I/O that
307 * has not completed in zfs_deadman_synctime_ms is considered "hung" resulting
308 * in one of three behaviors controlled by zfs_deadman_failmode.
309 */
310 uint64_t zfs_deadman_synctime_ms = 600000UL; /* 10 min. */
311
312 /*
313 * This value controls the maximum amount of time zio_wait() will block for an
314 * outstanding IO. By default this is 300 seconds at which point the "hung"
315 * behavior will be applied as described for zfs_deadman_synctime_ms.
316 */
317 uint64_t zfs_deadman_ziotime_ms = 300000UL; /* 5 min. */
318
319 /*
320 * Check time in milliseconds. This defines the frequency at which we check
321 * for hung I/O.
322 */
323 uint64_t zfs_deadman_checktime_ms = 60000UL; /* 1 min. */
324
325 /*
326 * By default the deadman is enabled.
327 */
328 int zfs_deadman_enabled = B_TRUE;
329
330 /*
331 * Controls the behavior of the deadman when it detects a "hung" I/O.
332 * Valid values are zfs_deadman_failmode=<wait|continue|panic>.
333 *
334 * wait - Wait for the "hung" I/O (default)
335 * continue - Attempt to recover from a "hung" I/O
336 * panic - Panic the system
337 */
338 const char *zfs_deadman_failmode = "wait";
339
340 /*
341 * The worst case is single-sector max-parity RAID-Z blocks, in which
342 * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1)
343 * times the size; so just assume that. Add to this the fact that
344 * we can have up to 3 DVAs per bp, and one more factor of 2 because
345 * the block may be dittoed with up to 3 DVAs by ddt_sync(). All together,
346 * the worst case is:
347 * (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2 == 24
348 */
349 uint_t spa_asize_inflation = 24;
350
351 /*
352 * Normally, we don't allow the last 3.2% (1/(2^spa_slop_shift)) of space in
353 * the pool to be consumed (bounded by spa_max_slop). This ensures that we
354 * don't run the pool completely out of space, due to unaccounted changes (e.g.
355 * to the MOS). It also limits the worst-case time to allocate space. If we
356 * have less than this amount of free space, most ZPL operations (e.g. write,
357 * create) will return ENOSPC. The ZIL metaslabs (spa_embedded_log_class) are
358 * also part of this 3.2% of space which can't be consumed by normal writes;
359 * the slop space "proper" (spa_get_slop_space()) is decreased by the embedded
360 * log space.
361 *
362 * Certain operations (e.g. file removal, most administrative actions) can
363 * use half the slop space. They will only return ENOSPC if less than half
364 * the slop space is free. Typically, once the pool has less than the slop
365 * space free, the user will use these operations to free up space in the pool.
366 * These are the operations that call dsl_pool_adjustedsize() with the netfree
367 * argument set to TRUE.
368 *
369 * Operations that are almost guaranteed to free up space in the absence of
370 * a pool checkpoint can use up to three quarters of the slop space
371 * (e.g zfs destroy).
372 *
373 * A very restricted set of operations are always permitted, regardless of
374 * the amount of free space. These are the operations that call
375 * dsl_sync_task(ZFS_SPACE_CHECK_NONE). If these operations result in a net
376 * increase in the amount of space used, it is possible to run the pool
377 * completely out of space, causing it to be permanently read-only.
378 *
379 * Note that on very small pools, the slop space will be larger than
380 * 3.2%, in an effort to have it be at least spa_min_slop (128MB),
381 * but we never allow it to be more than half the pool size.
382 *
383 * Further, on very large pools, the slop space will be smaller than
384 * 3.2%, to avoid reserving much more space than we actually need; bounded
385 * by spa_max_slop (128GB).
386 *
387 * See also the comments in zfs_space_check_t.
388 */
389 uint_t spa_slop_shift = 5;
390 static const uint64_t spa_min_slop = 128ULL * 1024 * 1024;
391 static const uint64_t spa_max_slop = 128ULL * 1024 * 1024 * 1024;
392
393 /*
394 * Number of allocators to use, per spa instance
395 */
396 static int spa_num_allocators = 4;
397 static int spa_cpus_per_allocator = 4;
398
399 /*
400 * Spa active allocator.
401 * Valid values are zfs_active_allocator=<dynamic|cursor|new-dynamic>.
402 */
403 const char *zfs_active_allocator = "dynamic";
404
405 void
spa_load_failed(spa_t * spa,const char * fmt,...)406 spa_load_failed(spa_t *spa, const char *fmt, ...)
407 {
408 va_list adx;
409 char buf[256];
410
411 va_start(adx, fmt);
412 (void) vsnprintf(buf, sizeof (buf), fmt, adx);
413 va_end(adx);
414
415 zfs_dbgmsg("spa_load(%s, config %s): FAILED: %s", spa->spa_name,
416 spa->spa_trust_config ? "trusted" : "untrusted", buf);
417 }
418
419 void
spa_load_note(spa_t * spa,const char * fmt,...)420 spa_load_note(spa_t *spa, const char *fmt, ...)
421 {
422 va_list adx;
423 char buf[256];
424
425 va_start(adx, fmt);
426 (void) vsnprintf(buf, sizeof (buf), fmt, adx);
427 va_end(adx);
428
429 zfs_dbgmsg("spa_load(%s, config %s): %s", spa->spa_name,
430 spa->spa_trust_config ? "trusted" : "untrusted", buf);
431
432 spa_import_progress_set_notes_nolog(spa, "%s", buf);
433 }
434
435 /*
436 * By default dedup and user data indirects land in the special class
437 */
438 static int zfs_ddt_data_is_special = B_TRUE;
439 static int zfs_user_indirect_is_special = B_TRUE;
440
441 /*
442 * The percentage of special class final space reserved for metadata only.
443 * Once we allocate 100 - zfs_special_class_metadata_reserve_pct we only
444 * let metadata into the class.
445 */
446 static uint_t zfs_special_class_metadata_reserve_pct = 25;
447
448 /*
449 * ==========================================================================
450 * SPA config locking
451 * ==========================================================================
452 */
453 static void
spa_config_lock_init(spa_t * spa)454 spa_config_lock_init(spa_t *spa)
455 {
456 for (int i = 0; i < SCL_LOCKS; i++) {
457 spa_config_lock_t *scl = &spa->spa_config_lock[i];
458 mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL);
459 cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL);
460 scl->scl_writer = NULL;
461 scl->scl_write_wanted = 0;
462 scl->scl_count = 0;
463 }
464 }
465
466 static void
spa_config_lock_destroy(spa_t * spa)467 spa_config_lock_destroy(spa_t *spa)
468 {
469 for (int i = 0; i < SCL_LOCKS; i++) {
470 spa_config_lock_t *scl = &spa->spa_config_lock[i];
471 mutex_destroy(&scl->scl_lock);
472 cv_destroy(&scl->scl_cv);
473 ASSERT(scl->scl_writer == NULL);
474 ASSERT(scl->scl_write_wanted == 0);
475 ASSERT(scl->scl_count == 0);
476 }
477 }
478
479 int
spa_config_tryenter(spa_t * spa,int locks,const void * tag,krw_t rw)480 spa_config_tryenter(spa_t *spa, int locks, const void *tag, krw_t rw)
481 {
482 for (int i = 0; i < SCL_LOCKS; i++) {
483 spa_config_lock_t *scl = &spa->spa_config_lock[i];
484 if (!(locks & (1 << i)))
485 continue;
486 mutex_enter(&scl->scl_lock);
487 if (rw == RW_READER) {
488 if (scl->scl_writer || scl->scl_write_wanted) {
489 mutex_exit(&scl->scl_lock);
490 spa_config_exit(spa, locks & ((1 << i) - 1),
491 tag);
492 return (0);
493 }
494 } else {
495 ASSERT(scl->scl_writer != curthread);
496 if (scl->scl_count != 0) {
497 mutex_exit(&scl->scl_lock);
498 spa_config_exit(spa, locks & ((1 << i) - 1),
499 tag);
500 return (0);
501 }
502 scl->scl_writer = curthread;
503 }
504 scl->scl_count++;
505 mutex_exit(&scl->scl_lock);
506 }
507 return (1);
508 }
509
510 static void
spa_config_enter_impl(spa_t * spa,int locks,const void * tag,krw_t rw,int mmp_flag)511 spa_config_enter_impl(spa_t *spa, int locks, const void *tag, krw_t rw,
512 int mmp_flag)
513 {
514 (void) tag;
515 int wlocks_held = 0;
516
517 ASSERT3U(SCL_LOCKS, <, sizeof (wlocks_held) * NBBY);
518
519 for (int i = 0; i < SCL_LOCKS; i++) {
520 spa_config_lock_t *scl = &spa->spa_config_lock[i];
521 if (scl->scl_writer == curthread)
522 wlocks_held |= (1 << i);
523 if (!(locks & (1 << i)))
524 continue;
525 mutex_enter(&scl->scl_lock);
526 if (rw == RW_READER) {
527 while (scl->scl_writer ||
528 (!mmp_flag && scl->scl_write_wanted)) {
529 cv_wait(&scl->scl_cv, &scl->scl_lock);
530 }
531 } else {
532 ASSERT(scl->scl_writer != curthread);
533 while (scl->scl_count != 0) {
534 scl->scl_write_wanted++;
535 cv_wait(&scl->scl_cv, &scl->scl_lock);
536 scl->scl_write_wanted--;
537 }
538 scl->scl_writer = curthread;
539 }
540 scl->scl_count++;
541 mutex_exit(&scl->scl_lock);
542 }
543 ASSERT3U(wlocks_held, <=, locks);
544 }
545
546 void
spa_config_enter(spa_t * spa,int locks,const void * tag,krw_t rw)547 spa_config_enter(spa_t *spa, int locks, const void *tag, krw_t rw)
548 {
549 spa_config_enter_impl(spa, locks, tag, rw, 0);
550 }
551
552 /*
553 * The spa_config_enter_mmp() allows the mmp thread to cut in front of
554 * outstanding write lock requests. This is needed since the mmp updates are
555 * time sensitive and failure to service them promptly will result in a
556 * suspended pool. This pool suspension has been seen in practice when there is
557 * a single disk in a pool that is responding slowly and presumably about to
558 * fail.
559 */
560
561 void
spa_config_enter_mmp(spa_t * spa,int locks,const void * tag,krw_t rw)562 spa_config_enter_mmp(spa_t *spa, int locks, const void *tag, krw_t rw)
563 {
564 spa_config_enter_impl(spa, locks, tag, rw, 1);
565 }
566
567 void
spa_config_exit(spa_t * spa,int locks,const void * tag)568 spa_config_exit(spa_t *spa, int locks, const void *tag)
569 {
570 (void) tag;
571 for (int i = SCL_LOCKS - 1; i >= 0; i--) {
572 spa_config_lock_t *scl = &spa->spa_config_lock[i];
573 if (!(locks & (1 << i)))
574 continue;
575 mutex_enter(&scl->scl_lock);
576 ASSERT(scl->scl_count > 0);
577 if (--scl->scl_count == 0) {
578 ASSERT(scl->scl_writer == NULL ||
579 scl->scl_writer == curthread);
580 scl->scl_writer = NULL; /* OK in either case */
581 cv_broadcast(&scl->scl_cv);
582 }
583 mutex_exit(&scl->scl_lock);
584 }
585 }
586
587 int
spa_config_held(spa_t * spa,int locks,krw_t rw)588 spa_config_held(spa_t *spa, int locks, krw_t rw)
589 {
590 int locks_held = 0;
591
592 for (int i = 0; i < SCL_LOCKS; i++) {
593 spa_config_lock_t *scl = &spa->spa_config_lock[i];
594 if (!(locks & (1 << i)))
595 continue;
596 if ((rw == RW_READER && scl->scl_count != 0) ||
597 (rw == RW_WRITER && scl->scl_writer == curthread))
598 locks_held |= 1 << i;
599 }
600
601 return (locks_held);
602 }
603
604 /*
605 * ==========================================================================
606 * SPA namespace functions
607 * ==========================================================================
608 */
609
610 /*
611 * Lookup the named spa_t in the AVL tree. The spa_namespace_lock must be held.
612 * Returns NULL if no matching spa_t is found.
613 */
614 spa_t *
spa_lookup(const char * name)615 spa_lookup(const char *name)
616 {
617 static spa_t search; /* spa_t is large; don't allocate on stack */
618 spa_t *spa;
619 avl_index_t where;
620 char *cp;
621
622 ASSERT(MUTEX_HELD(&spa_namespace_lock));
623
624 retry:
625 (void) strlcpy(search.spa_name, name, sizeof (search.spa_name));
626
627 /*
628 * If it's a full dataset name, figure out the pool name and
629 * just use that.
630 */
631 cp = strpbrk(search.spa_name, "/@#");
632 if (cp != NULL)
633 *cp = '\0';
634
635 spa = avl_find(&spa_namespace_avl, &search, &where);
636 if (spa == NULL)
637 return (NULL);
638
639 if (spa->spa_load_thread != NULL &&
640 spa->spa_load_thread != curthread) {
641 cv_wait(&spa_namespace_cv, &spa_namespace_lock);
642 goto retry;
643 }
644
645 return (spa);
646 }
647
648 /*
649 * Fires when spa_sync has not completed within zfs_deadman_synctime_ms.
650 * If the zfs_deadman_enabled flag is set then it inspects all vdev queues
651 * looking for potentially hung I/Os.
652 */
653 void
spa_deadman(void * arg)654 spa_deadman(void *arg)
655 {
656 spa_t *spa = arg;
657
658 /* Disable the deadman if the pool is suspended. */
659 if (spa_suspended(spa))
660 return;
661
662 zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu",
663 (gethrtime() - spa->spa_sync_starttime) / NANOSEC,
664 (u_longlong_t)++spa->spa_deadman_calls);
665 if (zfs_deadman_enabled)
666 vdev_deadman(spa->spa_root_vdev, FTAG);
667
668 spa->spa_deadman_tqid = taskq_dispatch_delay(system_delay_taskq,
669 spa_deadman, spa, TQ_SLEEP, ddi_get_lbolt() +
670 MSEC_TO_TICK(zfs_deadman_checktime_ms));
671 }
672
673 static int
spa_log_sm_sort_by_txg(const void * va,const void * vb)674 spa_log_sm_sort_by_txg(const void *va, const void *vb)
675 {
676 const spa_log_sm_t *a = va;
677 const spa_log_sm_t *b = vb;
678
679 return (TREE_CMP(a->sls_txg, b->sls_txg));
680 }
681
682 /*
683 * Create an uninitialized spa_t with the given name. Requires
684 * spa_namespace_lock. The caller must ensure that the spa_t doesn't already
685 * exist by calling spa_lookup() first.
686 */
687 spa_t *
spa_add(const char * name,nvlist_t * config,const char * altroot)688 spa_add(const char *name, nvlist_t *config, const char *altroot)
689 {
690 spa_t *spa;
691 spa_config_dirent_t *dp;
692
693 ASSERT(MUTEX_HELD(&spa_namespace_lock));
694
695 spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
696
697 mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL);
698 mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL);
699 mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL);
700 mutex_init(&spa->spa_evicting_os_lock, NULL, MUTEX_DEFAULT, NULL);
701 mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL);
702 mutex_init(&spa->spa_proc_lock, NULL, MUTEX_DEFAULT, NULL);
703 mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL);
704 mutex_init(&spa->spa_cksum_tmpls_lock, NULL, MUTEX_DEFAULT, NULL);
705 mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL);
706 mutex_init(&spa->spa_suspend_lock, NULL, MUTEX_DEFAULT, NULL);
707 mutex_init(&spa->spa_vdev_top_lock, NULL, MUTEX_DEFAULT, NULL);
708 mutex_init(&spa->spa_feat_stats_lock, NULL, MUTEX_DEFAULT, NULL);
709 mutex_init(&spa->spa_flushed_ms_lock, NULL, MUTEX_DEFAULT, NULL);
710 mutex_init(&spa->spa_activities_lock, NULL, MUTEX_DEFAULT, NULL);
711
712 cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL);
713 cv_init(&spa->spa_evicting_os_cv, NULL, CV_DEFAULT, NULL);
714 cv_init(&spa->spa_proc_cv, NULL, CV_DEFAULT, NULL);
715 cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL);
716 cv_init(&spa->spa_suspend_cv, NULL, CV_DEFAULT, NULL);
717 cv_init(&spa->spa_activities_cv, NULL, CV_DEFAULT, NULL);
718 cv_init(&spa->spa_waiters_cv, NULL, CV_DEFAULT, NULL);
719
720 for (int t = 0; t < TXG_SIZE; t++)
721 bplist_create(&spa->spa_free_bplist[t]);
722
723 (void) strlcpy(spa->spa_name, name, sizeof (spa->spa_name));
724 spa->spa_state = POOL_STATE_UNINITIALIZED;
725 spa->spa_freeze_txg = UINT64_MAX;
726 spa->spa_final_txg = UINT64_MAX;
727 spa->spa_load_max_txg = UINT64_MAX;
728 spa->spa_proc = &p0;
729 spa->spa_proc_state = SPA_PROC_NONE;
730 spa->spa_trust_config = B_TRUE;
731 spa->spa_hostid = zone_get_hostid(NULL);
732
733 spa->spa_deadman_synctime = MSEC2NSEC(zfs_deadman_synctime_ms);
734 spa->spa_deadman_ziotime = MSEC2NSEC(zfs_deadman_ziotime_ms);
735 spa_set_deadman_failmode(spa, zfs_deadman_failmode);
736 spa_set_allocator(spa, zfs_active_allocator);
737
738 zfs_refcount_create(&spa->spa_refcount);
739 spa_config_lock_init(spa);
740 spa_stats_init(spa);
741
742 ASSERT(MUTEX_HELD(&spa_namespace_lock));
743 avl_add(&spa_namespace_avl, spa);
744
745 /*
746 * Set the alternate root, if there is one.
747 */
748 if (altroot)
749 spa->spa_root = spa_strdup(altroot);
750
751 /* Do not allow more allocators than fraction of CPUs. */
752 spa->spa_alloc_count = MAX(MIN(spa_num_allocators,
753 boot_ncpus / MAX(spa_cpus_per_allocator, 1)), 1);
754
755 spa->spa_allocs = kmem_zalloc(spa->spa_alloc_count *
756 sizeof (spa_alloc_t), KM_SLEEP);
757 for (int i = 0; i < spa->spa_alloc_count; i++) {
758 mutex_init(&spa->spa_allocs[i].spaa_lock, NULL, MUTEX_DEFAULT,
759 NULL);
760 avl_create(&spa->spa_allocs[i].spaa_tree, zio_bookmark_compare,
761 sizeof (zio_t), offsetof(zio_t, io_queue_node.a));
762 }
763 if (spa->spa_alloc_count > 1) {
764 spa->spa_allocs_use = kmem_zalloc(offsetof(spa_allocs_use_t,
765 sau_inuse[spa->spa_alloc_count]), KM_SLEEP);
766 mutex_init(&spa->spa_allocs_use->sau_lock, NULL, MUTEX_DEFAULT,
767 NULL);
768 }
769
770 avl_create(&spa->spa_metaslabs_by_flushed, metaslab_sort_by_flushed,
771 sizeof (metaslab_t), offsetof(metaslab_t, ms_spa_txg_node));
772 avl_create(&spa->spa_sm_logs_by_txg, spa_log_sm_sort_by_txg,
773 sizeof (spa_log_sm_t), offsetof(spa_log_sm_t, sls_node));
774 list_create(&spa->spa_log_summary, sizeof (log_summary_entry_t),
775 offsetof(log_summary_entry_t, lse_node));
776
777 /*
778 * Every pool starts with the default cachefile
779 */
780 list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t),
781 offsetof(spa_config_dirent_t, scd_link));
782
783 dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP);
784 dp->scd_path = altroot ? NULL : spa_strdup(spa_config_path);
785 list_insert_head(&spa->spa_config_list, dp);
786
787 VERIFY(nvlist_alloc(&spa->spa_load_info, NV_UNIQUE_NAME,
788 KM_SLEEP) == 0);
789
790 if (config != NULL) {
791 nvlist_t *features;
792
793 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_FEATURES_FOR_READ,
794 &features) == 0) {
795 VERIFY(nvlist_dup(features, &spa->spa_label_features,
796 0) == 0);
797 }
798
799 VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0);
800 }
801
802 if (spa->spa_label_features == NULL) {
803 VERIFY(nvlist_alloc(&spa->spa_label_features, NV_UNIQUE_NAME,
804 KM_SLEEP) == 0);
805 }
806
807 spa->spa_min_ashift = INT_MAX;
808 spa->spa_max_ashift = 0;
809 spa->spa_min_alloc = INT_MAX;
810 spa->spa_gcd_alloc = INT_MAX;
811
812 /* Reset cached value */
813 spa->spa_dedup_dspace = ~0ULL;
814
815 /*
816 * As a pool is being created, treat all features as disabled by
817 * setting SPA_FEATURE_DISABLED for all entries in the feature
818 * refcount cache.
819 */
820 for (int i = 0; i < SPA_FEATURES; i++) {
821 spa->spa_feat_refcount_cache[i] = SPA_FEATURE_DISABLED;
822 }
823
824 list_create(&spa->spa_leaf_list, sizeof (vdev_t),
825 offsetof(vdev_t, vdev_leaf_node));
826
827 return (spa);
828 }
829
830 /*
831 * Removes a spa_t from the namespace, freeing up any memory used. Requires
832 * spa_namespace_lock. This is called only after the spa_t has been closed and
833 * deactivated.
834 */
835 void
spa_remove(spa_t * spa)836 spa_remove(spa_t *spa)
837 {
838 spa_config_dirent_t *dp;
839
840 ASSERT(MUTEX_HELD(&spa_namespace_lock));
841 ASSERT(spa_state(spa) == POOL_STATE_UNINITIALIZED);
842 ASSERT3U(zfs_refcount_count(&spa->spa_refcount), ==, 0);
843 ASSERT0(spa->spa_waiters);
844
845 nvlist_free(spa->spa_config_splitting);
846
847 avl_remove(&spa_namespace_avl, spa);
848
849 if (spa->spa_root)
850 spa_strfree(spa->spa_root);
851
852 while ((dp = list_remove_head(&spa->spa_config_list)) != NULL) {
853 if (dp->scd_path != NULL)
854 spa_strfree(dp->scd_path);
855 kmem_free(dp, sizeof (spa_config_dirent_t));
856 }
857
858 for (int i = 0; i < spa->spa_alloc_count; i++) {
859 avl_destroy(&spa->spa_allocs[i].spaa_tree);
860 mutex_destroy(&spa->spa_allocs[i].spaa_lock);
861 }
862 kmem_free(spa->spa_allocs, spa->spa_alloc_count *
863 sizeof (spa_alloc_t));
864 if (spa->spa_alloc_count > 1) {
865 mutex_destroy(&spa->spa_allocs_use->sau_lock);
866 kmem_free(spa->spa_allocs_use, offsetof(spa_allocs_use_t,
867 sau_inuse[spa->spa_alloc_count]));
868 }
869
870 avl_destroy(&spa->spa_metaslabs_by_flushed);
871 avl_destroy(&spa->spa_sm_logs_by_txg);
872 list_destroy(&spa->spa_log_summary);
873 list_destroy(&spa->spa_config_list);
874 list_destroy(&spa->spa_leaf_list);
875
876 nvlist_free(spa->spa_label_features);
877 nvlist_free(spa->spa_load_info);
878 nvlist_free(spa->spa_feat_stats);
879 spa_config_set(spa, NULL);
880
881 zfs_refcount_destroy(&spa->spa_refcount);
882
883 spa_stats_destroy(spa);
884 spa_config_lock_destroy(spa);
885
886 for (int t = 0; t < TXG_SIZE; t++)
887 bplist_destroy(&spa->spa_free_bplist[t]);
888
889 zio_checksum_templates_free(spa);
890
891 cv_destroy(&spa->spa_async_cv);
892 cv_destroy(&spa->spa_evicting_os_cv);
893 cv_destroy(&spa->spa_proc_cv);
894 cv_destroy(&spa->spa_scrub_io_cv);
895 cv_destroy(&spa->spa_suspend_cv);
896 cv_destroy(&spa->spa_activities_cv);
897 cv_destroy(&spa->spa_waiters_cv);
898
899 mutex_destroy(&spa->spa_flushed_ms_lock);
900 mutex_destroy(&spa->spa_async_lock);
901 mutex_destroy(&spa->spa_errlist_lock);
902 mutex_destroy(&spa->spa_errlog_lock);
903 mutex_destroy(&spa->spa_evicting_os_lock);
904 mutex_destroy(&spa->spa_history_lock);
905 mutex_destroy(&spa->spa_proc_lock);
906 mutex_destroy(&spa->spa_props_lock);
907 mutex_destroy(&spa->spa_cksum_tmpls_lock);
908 mutex_destroy(&spa->spa_scrub_lock);
909 mutex_destroy(&spa->spa_suspend_lock);
910 mutex_destroy(&spa->spa_vdev_top_lock);
911 mutex_destroy(&spa->spa_feat_stats_lock);
912 mutex_destroy(&spa->spa_activities_lock);
913
914 kmem_free(spa, sizeof (spa_t));
915 }
916
917 /*
918 * Given a pool, return the next pool in the namespace, or NULL if there is
919 * none. If 'prev' is NULL, return the first pool.
920 */
921 spa_t *
spa_next(spa_t * prev)922 spa_next(spa_t *prev)
923 {
924 ASSERT(MUTEX_HELD(&spa_namespace_lock));
925
926 if (prev)
927 return (AVL_NEXT(&spa_namespace_avl, prev));
928 else
929 return (avl_first(&spa_namespace_avl));
930 }
931
932 /*
933 * ==========================================================================
934 * SPA refcount functions
935 * ==========================================================================
936 */
937
938 /*
939 * Add a reference to the given spa_t. Must have at least one reference, or
940 * have the namespace lock held.
941 */
942 void
spa_open_ref(spa_t * spa,const void * tag)943 spa_open_ref(spa_t *spa, const void *tag)
944 {
945 ASSERT(zfs_refcount_count(&spa->spa_refcount) >= spa->spa_minref ||
946 MUTEX_HELD(&spa_namespace_lock) ||
947 spa->spa_load_thread == curthread);
948 (void) zfs_refcount_add(&spa->spa_refcount, tag);
949 }
950
951 /*
952 * Remove a reference to the given spa_t. Must have at least one reference, or
953 * have the namespace lock held.
954 */
955 void
spa_close(spa_t * spa,const void * tag)956 spa_close(spa_t *spa, const void *tag)
957 {
958 ASSERT(zfs_refcount_count(&spa->spa_refcount) > spa->spa_minref ||
959 MUTEX_HELD(&spa_namespace_lock) ||
960 spa->spa_load_thread == curthread);
961 (void) zfs_refcount_remove(&spa->spa_refcount, tag);
962 }
963
964 /*
965 * Remove a reference to the given spa_t held by a dsl dir that is
966 * being asynchronously released. Async releases occur from a taskq
967 * performing eviction of dsl datasets and dirs. The namespace lock
968 * isn't held and the hold by the object being evicted may contribute to
969 * spa_minref (e.g. dataset or directory released during pool export),
970 * so the asserts in spa_close() do not apply.
971 */
972 void
spa_async_close(spa_t * spa,const void * tag)973 spa_async_close(spa_t *spa, const void *tag)
974 {
975 (void) zfs_refcount_remove(&spa->spa_refcount, tag);
976 }
977
978 /*
979 * Check to see if the spa refcount is zero. Must be called with
980 * spa_namespace_lock held. We really compare against spa_minref, which is the
981 * number of references acquired when opening a pool
982 */
983 boolean_t
spa_refcount_zero(spa_t * spa)984 spa_refcount_zero(spa_t *spa)
985 {
986 ASSERT(MUTEX_HELD(&spa_namespace_lock));
987
988 return (zfs_refcount_count(&spa->spa_refcount) == spa->spa_minref);
989 }
990
991 /*
992 * ==========================================================================
993 * SPA spare and l2cache tracking
994 * ==========================================================================
995 */
996
997 /*
998 * Hot spares and cache devices are tracked using the same code below,
999 * for 'auxiliary' devices.
1000 */
1001
1002 typedef struct spa_aux {
1003 uint64_t aux_guid;
1004 uint64_t aux_pool;
1005 avl_node_t aux_avl;
1006 int aux_count;
1007 } spa_aux_t;
1008
1009 static inline int
spa_aux_compare(const void * a,const void * b)1010 spa_aux_compare(const void *a, const void *b)
1011 {
1012 const spa_aux_t *sa = (const spa_aux_t *)a;
1013 const spa_aux_t *sb = (const spa_aux_t *)b;
1014
1015 return (TREE_CMP(sa->aux_guid, sb->aux_guid));
1016 }
1017
1018 static void
spa_aux_add(vdev_t * vd,avl_tree_t * avl)1019 spa_aux_add(vdev_t *vd, avl_tree_t *avl)
1020 {
1021 avl_index_t where;
1022 spa_aux_t search;
1023 spa_aux_t *aux;
1024
1025 search.aux_guid = vd->vdev_guid;
1026 if ((aux = avl_find(avl, &search, &where)) != NULL) {
1027 aux->aux_count++;
1028 } else {
1029 aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP);
1030 aux->aux_guid = vd->vdev_guid;
1031 aux->aux_count = 1;
1032 avl_insert(avl, aux, where);
1033 }
1034 }
1035
1036 static void
spa_aux_remove(vdev_t * vd,avl_tree_t * avl)1037 spa_aux_remove(vdev_t *vd, avl_tree_t *avl)
1038 {
1039 spa_aux_t search;
1040 spa_aux_t *aux;
1041 avl_index_t where;
1042
1043 search.aux_guid = vd->vdev_guid;
1044 aux = avl_find(avl, &search, &where);
1045
1046 ASSERT(aux != NULL);
1047
1048 if (--aux->aux_count == 0) {
1049 avl_remove(avl, aux);
1050 kmem_free(aux, sizeof (spa_aux_t));
1051 } else if (aux->aux_pool == spa_guid(vd->vdev_spa)) {
1052 aux->aux_pool = 0ULL;
1053 }
1054 }
1055
1056 static boolean_t
spa_aux_exists(uint64_t guid,uint64_t * pool,int * refcnt,avl_tree_t * avl)1057 spa_aux_exists(uint64_t guid, uint64_t *pool, int *refcnt, avl_tree_t *avl)
1058 {
1059 spa_aux_t search, *found;
1060
1061 search.aux_guid = guid;
1062 found = avl_find(avl, &search, NULL);
1063
1064 if (pool) {
1065 if (found)
1066 *pool = found->aux_pool;
1067 else
1068 *pool = 0ULL;
1069 }
1070
1071 if (refcnt) {
1072 if (found)
1073 *refcnt = found->aux_count;
1074 else
1075 *refcnt = 0;
1076 }
1077
1078 return (found != NULL);
1079 }
1080
1081 static void
spa_aux_activate(vdev_t * vd,avl_tree_t * avl)1082 spa_aux_activate(vdev_t *vd, avl_tree_t *avl)
1083 {
1084 spa_aux_t search, *found;
1085 avl_index_t where;
1086
1087 search.aux_guid = vd->vdev_guid;
1088 found = avl_find(avl, &search, &where);
1089 ASSERT(found != NULL);
1090 ASSERT(found->aux_pool == 0ULL);
1091
1092 found->aux_pool = spa_guid(vd->vdev_spa);
1093 }
1094
1095 /*
1096 * Spares are tracked globally due to the following constraints:
1097 *
1098 * - A spare may be part of multiple pools.
1099 * - A spare may be added to a pool even if it's actively in use within
1100 * another pool.
1101 * - A spare in use in any pool can only be the source of a replacement if
1102 * the target is a spare in the same pool.
1103 *
1104 * We keep track of all spares on the system through the use of a reference
1105 * counted AVL tree. When a vdev is added as a spare, or used as a replacement
1106 * spare, then we bump the reference count in the AVL tree. In addition, we set
1107 * the 'vdev_isspare' member to indicate that the device is a spare (active or
1108 * inactive). When a spare is made active (used to replace a device in the
1109 * pool), we also keep track of which pool its been made a part of.
1110 *
1111 * The 'spa_spare_lock' protects the AVL tree. These functions are normally
1112 * called under the spa_namespace lock as part of vdev reconfiguration. The
1113 * separate spare lock exists for the status query path, which does not need to
1114 * be completely consistent with respect to other vdev configuration changes.
1115 */
1116
1117 static int
spa_spare_compare(const void * a,const void * b)1118 spa_spare_compare(const void *a, const void *b)
1119 {
1120 return (spa_aux_compare(a, b));
1121 }
1122
1123 void
spa_spare_add(vdev_t * vd)1124 spa_spare_add(vdev_t *vd)
1125 {
1126 mutex_enter(&spa_spare_lock);
1127 ASSERT(!vd->vdev_isspare);
1128 spa_aux_add(vd, &spa_spare_avl);
1129 vd->vdev_isspare = B_TRUE;
1130 mutex_exit(&spa_spare_lock);
1131 }
1132
1133 void
spa_spare_remove(vdev_t * vd)1134 spa_spare_remove(vdev_t *vd)
1135 {
1136 mutex_enter(&spa_spare_lock);
1137 ASSERT(vd->vdev_isspare);
1138 spa_aux_remove(vd, &spa_spare_avl);
1139 vd->vdev_isspare = B_FALSE;
1140 mutex_exit(&spa_spare_lock);
1141 }
1142
1143 boolean_t
spa_spare_exists(uint64_t guid,uint64_t * pool,int * refcnt)1144 spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt)
1145 {
1146 boolean_t found;
1147
1148 mutex_enter(&spa_spare_lock);
1149 found = spa_aux_exists(guid, pool, refcnt, &spa_spare_avl);
1150 mutex_exit(&spa_spare_lock);
1151
1152 return (found);
1153 }
1154
1155 void
spa_spare_activate(vdev_t * vd)1156 spa_spare_activate(vdev_t *vd)
1157 {
1158 mutex_enter(&spa_spare_lock);
1159 ASSERT(vd->vdev_isspare);
1160 spa_aux_activate(vd, &spa_spare_avl);
1161 mutex_exit(&spa_spare_lock);
1162 }
1163
1164 /*
1165 * Level 2 ARC devices are tracked globally for the same reasons as spares.
1166 * Cache devices currently only support one pool per cache device, and so
1167 * for these devices the aux reference count is currently unused beyond 1.
1168 */
1169
1170 static int
spa_l2cache_compare(const void * a,const void * b)1171 spa_l2cache_compare(const void *a, const void *b)
1172 {
1173 return (spa_aux_compare(a, b));
1174 }
1175
1176 void
spa_l2cache_add(vdev_t * vd)1177 spa_l2cache_add(vdev_t *vd)
1178 {
1179 mutex_enter(&spa_l2cache_lock);
1180 ASSERT(!vd->vdev_isl2cache);
1181 spa_aux_add(vd, &spa_l2cache_avl);
1182 vd->vdev_isl2cache = B_TRUE;
1183 mutex_exit(&spa_l2cache_lock);
1184 }
1185
1186 void
spa_l2cache_remove(vdev_t * vd)1187 spa_l2cache_remove(vdev_t *vd)
1188 {
1189 mutex_enter(&spa_l2cache_lock);
1190 ASSERT(vd->vdev_isl2cache);
1191 spa_aux_remove(vd, &spa_l2cache_avl);
1192 vd->vdev_isl2cache = B_FALSE;
1193 mutex_exit(&spa_l2cache_lock);
1194 }
1195
1196 boolean_t
spa_l2cache_exists(uint64_t guid,uint64_t * pool)1197 spa_l2cache_exists(uint64_t guid, uint64_t *pool)
1198 {
1199 boolean_t found;
1200
1201 mutex_enter(&spa_l2cache_lock);
1202 found = spa_aux_exists(guid, pool, NULL, &spa_l2cache_avl);
1203 mutex_exit(&spa_l2cache_lock);
1204
1205 return (found);
1206 }
1207
1208 void
spa_l2cache_activate(vdev_t * vd)1209 spa_l2cache_activate(vdev_t *vd)
1210 {
1211 mutex_enter(&spa_l2cache_lock);
1212 ASSERT(vd->vdev_isl2cache);
1213 spa_aux_activate(vd, &spa_l2cache_avl);
1214 mutex_exit(&spa_l2cache_lock);
1215 }
1216
1217 /*
1218 * ==========================================================================
1219 * SPA vdev locking
1220 * ==========================================================================
1221 */
1222
1223 /*
1224 * Lock the given spa_t for the purpose of adding or removing a vdev.
1225 * Grabs the global spa_namespace_lock plus the spa config lock for writing.
1226 * It returns the next transaction group for the spa_t.
1227 */
1228 uint64_t
spa_vdev_enter(spa_t * spa)1229 spa_vdev_enter(spa_t *spa)
1230 {
1231 mutex_enter(&spa->spa_vdev_top_lock);
1232 mutex_enter(&spa_namespace_lock);
1233
1234 vdev_autotrim_stop_all(spa);
1235
1236 return (spa_vdev_config_enter(spa));
1237 }
1238
1239 /*
1240 * The same as spa_vdev_enter() above but additionally takes the guid of
1241 * the vdev being detached. When there is a rebuild in process it will be
1242 * suspended while the vdev tree is modified then resumed by spa_vdev_exit().
1243 * The rebuild is canceled if only a single child remains after the detach.
1244 */
1245 uint64_t
spa_vdev_detach_enter(spa_t * spa,uint64_t guid)1246 spa_vdev_detach_enter(spa_t *spa, uint64_t guid)
1247 {
1248 mutex_enter(&spa->spa_vdev_top_lock);
1249 mutex_enter(&spa_namespace_lock);
1250
1251 vdev_autotrim_stop_all(spa);
1252
1253 if (guid != 0) {
1254 vdev_t *vd = spa_lookup_by_guid(spa, guid, B_FALSE);
1255 if (vd) {
1256 vdev_rebuild_stop_wait(vd->vdev_top);
1257 }
1258 }
1259
1260 return (spa_vdev_config_enter(spa));
1261 }
1262
1263 /*
1264 * Internal implementation for spa_vdev_enter(). Used when a vdev
1265 * operation requires multiple syncs (i.e. removing a device) while
1266 * keeping the spa_namespace_lock held.
1267 */
1268 uint64_t
spa_vdev_config_enter(spa_t * spa)1269 spa_vdev_config_enter(spa_t *spa)
1270 {
1271 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1272
1273 spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
1274
1275 return (spa_last_synced_txg(spa) + 1);
1276 }
1277
1278 /*
1279 * Used in combination with spa_vdev_config_enter() to allow the syncing
1280 * of multiple transactions without releasing the spa_namespace_lock.
1281 */
1282 void
spa_vdev_config_exit(spa_t * spa,vdev_t * vd,uint64_t txg,int error,const char * tag)1283 spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error,
1284 const char *tag)
1285 {
1286 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1287
1288 int config_changed = B_FALSE;
1289
1290 ASSERT(txg > spa_last_synced_txg(spa));
1291
1292 spa->spa_pending_vdev = NULL;
1293
1294 /*
1295 * Reassess the DTLs.
1296 */
1297 vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE, B_FALSE);
1298
1299 if (error == 0 && !list_is_empty(&spa->spa_config_dirty_list)) {
1300 config_changed = B_TRUE;
1301 spa->spa_config_generation++;
1302 }
1303
1304 /*
1305 * Verify the metaslab classes.
1306 */
1307 ASSERT(metaslab_class_validate(spa_normal_class(spa)) == 0);
1308 ASSERT(metaslab_class_validate(spa_log_class(spa)) == 0);
1309 ASSERT(metaslab_class_validate(spa_embedded_log_class(spa)) == 0);
1310 ASSERT(metaslab_class_validate(spa_special_class(spa)) == 0);
1311 ASSERT(metaslab_class_validate(spa_dedup_class(spa)) == 0);
1312
1313 spa_config_exit(spa, SCL_ALL, spa);
1314
1315 /*
1316 * Panic the system if the specified tag requires it. This
1317 * is useful for ensuring that configurations are updated
1318 * transactionally.
1319 */
1320 if (zio_injection_enabled)
1321 zio_handle_panic_injection(spa, tag, 0);
1322
1323 /*
1324 * Note: this txg_wait_synced() is important because it ensures
1325 * that there won't be more than one config change per txg.
1326 * This allows us to use the txg as the generation number.
1327 */
1328 if (error == 0)
1329 txg_wait_synced(spa->spa_dsl_pool, txg);
1330
1331 if (vd != NULL) {
1332 ASSERT(!vd->vdev_detached || vd->vdev_dtl_sm == NULL);
1333 if (vd->vdev_ops->vdev_op_leaf) {
1334 mutex_enter(&vd->vdev_initialize_lock);
1335 vdev_initialize_stop(vd, VDEV_INITIALIZE_CANCELED,
1336 NULL);
1337 mutex_exit(&vd->vdev_initialize_lock);
1338
1339 mutex_enter(&vd->vdev_trim_lock);
1340 vdev_trim_stop(vd, VDEV_TRIM_CANCELED, NULL);
1341 mutex_exit(&vd->vdev_trim_lock);
1342 }
1343
1344 /*
1345 * The vdev may be both a leaf and top-level device.
1346 */
1347 vdev_autotrim_stop_wait(vd);
1348
1349 spa_config_enter(spa, SCL_STATE_ALL, spa, RW_WRITER);
1350 vdev_free(vd);
1351 spa_config_exit(spa, SCL_STATE_ALL, spa);
1352 }
1353
1354 /*
1355 * If the config changed, update the config cache.
1356 */
1357 if (config_changed)
1358 spa_write_cachefile(spa, B_FALSE, B_TRUE, B_TRUE);
1359 }
1360
1361 /*
1362 * Unlock the spa_t after adding or removing a vdev. Besides undoing the
1363 * locking of spa_vdev_enter(), we also want make sure the transactions have
1364 * synced to disk, and then update the global configuration cache with the new
1365 * information.
1366 */
1367 int
spa_vdev_exit(spa_t * spa,vdev_t * vd,uint64_t txg,int error)1368 spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
1369 {
1370 vdev_autotrim_restart(spa);
1371 vdev_rebuild_restart(spa);
1372
1373 spa_vdev_config_exit(spa, vd, txg, error, FTAG);
1374 mutex_exit(&spa_namespace_lock);
1375 mutex_exit(&spa->spa_vdev_top_lock);
1376
1377 return (error);
1378 }
1379
1380 /*
1381 * Lock the given spa_t for the purpose of changing vdev state.
1382 */
1383 void
spa_vdev_state_enter(spa_t * spa,int oplocks)1384 spa_vdev_state_enter(spa_t *spa, int oplocks)
1385 {
1386 int locks = SCL_STATE_ALL | oplocks;
1387
1388 /*
1389 * Root pools may need to read of the underlying devfs filesystem
1390 * when opening up a vdev. Unfortunately if we're holding the
1391 * SCL_ZIO lock it will result in a deadlock when we try to issue
1392 * the read from the root filesystem. Instead we "prefetch"
1393 * the associated vnodes that we need prior to opening the
1394 * underlying devices and cache them so that we can prevent
1395 * any I/O when we are doing the actual open.
1396 */
1397 if (spa_is_root(spa)) {
1398 int low = locks & ~(SCL_ZIO - 1);
1399 int high = locks & ~low;
1400
1401 spa_config_enter(spa, high, spa, RW_WRITER);
1402 vdev_hold(spa->spa_root_vdev);
1403 spa_config_enter(spa, low, spa, RW_WRITER);
1404 } else {
1405 spa_config_enter(spa, locks, spa, RW_WRITER);
1406 }
1407 spa->spa_vdev_locks = locks;
1408 }
1409
1410 int
spa_vdev_state_exit(spa_t * spa,vdev_t * vd,int error)1411 spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error)
1412 {
1413 boolean_t config_changed = B_FALSE;
1414 vdev_t *vdev_top;
1415
1416 if (vd == NULL || vd == spa->spa_root_vdev) {
1417 vdev_top = spa->spa_root_vdev;
1418 } else {
1419 vdev_top = vd->vdev_top;
1420 }
1421
1422 if (vd != NULL || error == 0)
1423 vdev_dtl_reassess(vdev_top, 0, 0, B_FALSE, B_FALSE);
1424
1425 if (vd != NULL) {
1426 if (vd != spa->spa_root_vdev)
1427 vdev_state_dirty(vdev_top);
1428
1429 config_changed = B_TRUE;
1430 spa->spa_config_generation++;
1431 }
1432
1433 if (spa_is_root(spa))
1434 vdev_rele(spa->spa_root_vdev);
1435
1436 ASSERT3U(spa->spa_vdev_locks, >=, SCL_STATE_ALL);
1437 spa_config_exit(spa, spa->spa_vdev_locks, spa);
1438
1439 /*
1440 * If anything changed, wait for it to sync. This ensures that,
1441 * from the system administrator's perspective, zpool(8) commands
1442 * are synchronous. This is important for things like zpool offline:
1443 * when the command completes, you expect no further I/O from ZFS.
1444 */
1445 if (vd != NULL)
1446 txg_wait_synced(spa->spa_dsl_pool, 0);
1447
1448 /*
1449 * If the config changed, update the config cache.
1450 */
1451 if (config_changed) {
1452 mutex_enter(&spa_namespace_lock);
1453 spa_write_cachefile(spa, B_FALSE, B_TRUE, B_FALSE);
1454 mutex_exit(&spa_namespace_lock);
1455 }
1456
1457 return (error);
1458 }
1459
1460 /*
1461 * ==========================================================================
1462 * Miscellaneous functions
1463 * ==========================================================================
1464 */
1465
1466 void
spa_activate_mos_feature(spa_t * spa,const char * feature,dmu_tx_t * tx)1467 spa_activate_mos_feature(spa_t *spa, const char *feature, dmu_tx_t *tx)
1468 {
1469 if (!nvlist_exists(spa->spa_label_features, feature)) {
1470 fnvlist_add_boolean(spa->spa_label_features, feature);
1471 /*
1472 * When we are creating the pool (tx_txg==TXG_INITIAL), we can't
1473 * dirty the vdev config because lock SCL_CONFIG is not held.
1474 * Thankfully, in this case we don't need to dirty the config
1475 * because it will be written out anyway when we finish
1476 * creating the pool.
1477 */
1478 if (tx->tx_txg != TXG_INITIAL)
1479 vdev_config_dirty(spa->spa_root_vdev);
1480 }
1481 }
1482
1483 void
spa_deactivate_mos_feature(spa_t * spa,const char * feature)1484 spa_deactivate_mos_feature(spa_t *spa, const char *feature)
1485 {
1486 if (nvlist_remove_all(spa->spa_label_features, feature) == 0)
1487 vdev_config_dirty(spa->spa_root_vdev);
1488 }
1489
1490 /*
1491 * Return the spa_t associated with given pool_guid, if it exists. If
1492 * device_guid is non-zero, determine whether the pool exists *and* contains
1493 * a device with the specified device_guid.
1494 */
1495 spa_t *
spa_by_guid(uint64_t pool_guid,uint64_t device_guid)1496 spa_by_guid(uint64_t pool_guid, uint64_t device_guid)
1497 {
1498 spa_t *spa;
1499 avl_tree_t *t = &spa_namespace_avl;
1500
1501 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1502
1503 for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) {
1504 if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1505 continue;
1506 if (spa->spa_root_vdev == NULL)
1507 continue;
1508 if (spa_guid(spa) == pool_guid) {
1509 if (device_guid == 0)
1510 break;
1511
1512 if (vdev_lookup_by_guid(spa->spa_root_vdev,
1513 device_guid) != NULL)
1514 break;
1515
1516 /*
1517 * Check any devices we may be in the process of adding.
1518 */
1519 if (spa->spa_pending_vdev) {
1520 if (vdev_lookup_by_guid(spa->spa_pending_vdev,
1521 device_guid) != NULL)
1522 break;
1523 }
1524 }
1525 }
1526
1527 return (spa);
1528 }
1529
1530 /*
1531 * Determine whether a pool with the given pool_guid exists.
1532 */
1533 boolean_t
spa_guid_exists(uint64_t pool_guid,uint64_t device_guid)1534 spa_guid_exists(uint64_t pool_guid, uint64_t device_guid)
1535 {
1536 return (spa_by_guid(pool_guid, device_guid) != NULL);
1537 }
1538
1539 char *
spa_strdup(const char * s)1540 spa_strdup(const char *s)
1541 {
1542 size_t len;
1543 char *new;
1544
1545 len = strlen(s);
1546 new = kmem_alloc(len + 1, KM_SLEEP);
1547 memcpy(new, s, len + 1);
1548
1549 return (new);
1550 }
1551
1552 void
spa_strfree(char * s)1553 spa_strfree(char *s)
1554 {
1555 kmem_free(s, strlen(s) + 1);
1556 }
1557
1558 uint64_t
spa_generate_guid(spa_t * spa)1559 spa_generate_guid(spa_t *spa)
1560 {
1561 uint64_t guid;
1562
1563 if (spa != NULL) {
1564 do {
1565 (void) random_get_pseudo_bytes((void *)&guid,
1566 sizeof (guid));
1567 } while (guid == 0 || spa_guid_exists(spa_guid(spa), guid));
1568 } else {
1569 do {
1570 (void) random_get_pseudo_bytes((void *)&guid,
1571 sizeof (guid));
1572 } while (guid == 0 || spa_guid_exists(guid, 0));
1573 }
1574
1575 return (guid);
1576 }
1577
1578 void
snprintf_blkptr(char * buf,size_t buflen,const blkptr_t * bp)1579 snprintf_blkptr(char *buf, size_t buflen, const blkptr_t *bp)
1580 {
1581 char type[256];
1582 const char *checksum = NULL;
1583 const char *compress = NULL;
1584
1585 if (bp != NULL) {
1586 if (BP_GET_TYPE(bp) & DMU_OT_NEWTYPE) {
1587 dmu_object_byteswap_t bswap =
1588 DMU_OT_BYTESWAP(BP_GET_TYPE(bp));
1589 (void) snprintf(type, sizeof (type), "bswap %s %s",
1590 DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) ?
1591 "metadata" : "data",
1592 dmu_ot_byteswap[bswap].ob_name);
1593 } else {
1594 (void) strlcpy(type, dmu_ot[BP_GET_TYPE(bp)].ot_name,
1595 sizeof (type));
1596 }
1597 if (!BP_IS_EMBEDDED(bp)) {
1598 checksum =
1599 zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name;
1600 }
1601 compress = zio_compress_table[BP_GET_COMPRESS(bp)].ci_name;
1602 }
1603
1604 SNPRINTF_BLKPTR(kmem_scnprintf, ' ', buf, buflen, bp, type, checksum,
1605 compress);
1606 }
1607
1608 void
spa_freeze(spa_t * spa)1609 spa_freeze(spa_t *spa)
1610 {
1611 uint64_t freeze_txg = 0;
1612
1613 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1614 if (spa->spa_freeze_txg == UINT64_MAX) {
1615 freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE;
1616 spa->spa_freeze_txg = freeze_txg;
1617 }
1618 spa_config_exit(spa, SCL_ALL, FTAG);
1619 if (freeze_txg != 0)
1620 txg_wait_synced(spa_get_dsl(spa), freeze_txg);
1621 }
1622
1623 void
zfs_panic_recover(const char * fmt,...)1624 zfs_panic_recover(const char *fmt, ...)
1625 {
1626 va_list adx;
1627
1628 va_start(adx, fmt);
1629 vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx);
1630 va_end(adx);
1631 }
1632
1633 /*
1634 * This is a stripped-down version of strtoull, suitable only for converting
1635 * lowercase hexadecimal numbers that don't overflow.
1636 */
1637 uint64_t
zfs_strtonum(const char * str,char ** nptr)1638 zfs_strtonum(const char *str, char **nptr)
1639 {
1640 uint64_t val = 0;
1641 char c;
1642 int digit;
1643
1644 while ((c = *str) != '\0') {
1645 if (c >= '0' && c <= '9')
1646 digit = c - '0';
1647 else if (c >= 'a' && c <= 'f')
1648 digit = 10 + c - 'a';
1649 else
1650 break;
1651
1652 val *= 16;
1653 val += digit;
1654
1655 str++;
1656 }
1657
1658 if (nptr)
1659 *nptr = (char *)str;
1660
1661 return (val);
1662 }
1663
1664 void
spa_activate_allocation_classes(spa_t * spa,dmu_tx_t * tx)1665 spa_activate_allocation_classes(spa_t *spa, dmu_tx_t *tx)
1666 {
1667 /*
1668 * We bump the feature refcount for each special vdev added to the pool
1669 */
1670 ASSERT(spa_feature_is_enabled(spa, SPA_FEATURE_ALLOCATION_CLASSES));
1671 spa_feature_incr(spa, SPA_FEATURE_ALLOCATION_CLASSES, tx);
1672 }
1673
1674 /*
1675 * ==========================================================================
1676 * Accessor functions
1677 * ==========================================================================
1678 */
1679
1680 boolean_t
spa_shutting_down(spa_t * spa)1681 spa_shutting_down(spa_t *spa)
1682 {
1683 return (spa->spa_async_suspended);
1684 }
1685
1686 dsl_pool_t *
spa_get_dsl(spa_t * spa)1687 spa_get_dsl(spa_t *spa)
1688 {
1689 return (spa->spa_dsl_pool);
1690 }
1691
1692 boolean_t
spa_is_initializing(spa_t * spa)1693 spa_is_initializing(spa_t *spa)
1694 {
1695 return (spa->spa_is_initializing);
1696 }
1697
1698 boolean_t
spa_indirect_vdevs_loaded(spa_t * spa)1699 spa_indirect_vdevs_loaded(spa_t *spa)
1700 {
1701 return (spa->spa_indirect_vdevs_loaded);
1702 }
1703
1704 blkptr_t *
spa_get_rootblkptr(spa_t * spa)1705 spa_get_rootblkptr(spa_t *spa)
1706 {
1707 return (&spa->spa_ubsync.ub_rootbp);
1708 }
1709
1710 void
spa_set_rootblkptr(spa_t * spa,const blkptr_t * bp)1711 spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp)
1712 {
1713 spa->spa_uberblock.ub_rootbp = *bp;
1714 }
1715
1716 void
spa_altroot(spa_t * spa,char * buf,size_t buflen)1717 spa_altroot(spa_t *spa, char *buf, size_t buflen)
1718 {
1719 if (spa->spa_root == NULL)
1720 buf[0] = '\0';
1721 else
1722 (void) strlcpy(buf, spa->spa_root, buflen);
1723 }
1724
1725 uint32_t
spa_sync_pass(spa_t * spa)1726 spa_sync_pass(spa_t *spa)
1727 {
1728 return (spa->spa_sync_pass);
1729 }
1730
1731 char *
spa_name(spa_t * spa)1732 spa_name(spa_t *spa)
1733 {
1734 return (spa->spa_name);
1735 }
1736
1737 uint64_t
spa_guid(spa_t * spa)1738 spa_guid(spa_t *spa)
1739 {
1740 dsl_pool_t *dp = spa_get_dsl(spa);
1741 uint64_t guid;
1742
1743 /*
1744 * If we fail to parse the config during spa_load(), we can go through
1745 * the error path (which posts an ereport) and end up here with no root
1746 * vdev. We stash the original pool guid in 'spa_config_guid' to handle
1747 * this case.
1748 */
1749 if (spa->spa_root_vdev == NULL)
1750 return (spa->spa_config_guid);
1751
1752 guid = spa->spa_last_synced_guid != 0 ?
1753 spa->spa_last_synced_guid : spa->spa_root_vdev->vdev_guid;
1754
1755 /*
1756 * Return the most recently synced out guid unless we're
1757 * in syncing context.
1758 */
1759 if (dp && dsl_pool_sync_context(dp))
1760 return (spa->spa_root_vdev->vdev_guid);
1761 else
1762 return (guid);
1763 }
1764
1765 uint64_t
spa_load_guid(spa_t * spa)1766 spa_load_guid(spa_t *spa)
1767 {
1768 /*
1769 * This is a GUID that exists solely as a reference for the
1770 * purposes of the arc. It is generated at load time, and
1771 * is never written to persistent storage.
1772 */
1773 return (spa->spa_load_guid);
1774 }
1775
1776 uint64_t
spa_last_synced_txg(spa_t * spa)1777 spa_last_synced_txg(spa_t *spa)
1778 {
1779 return (spa->spa_ubsync.ub_txg);
1780 }
1781
1782 uint64_t
spa_first_txg(spa_t * spa)1783 spa_first_txg(spa_t *spa)
1784 {
1785 return (spa->spa_first_txg);
1786 }
1787
1788 uint64_t
spa_syncing_txg(spa_t * spa)1789 spa_syncing_txg(spa_t *spa)
1790 {
1791 return (spa->spa_syncing_txg);
1792 }
1793
1794 /*
1795 * Return the last txg where data can be dirtied. The final txgs
1796 * will be used to just clear out any deferred frees that remain.
1797 */
1798 uint64_t
spa_final_dirty_txg(spa_t * spa)1799 spa_final_dirty_txg(spa_t *spa)
1800 {
1801 return (spa->spa_final_txg - TXG_DEFER_SIZE);
1802 }
1803
1804 pool_state_t
spa_state(spa_t * spa)1805 spa_state(spa_t *spa)
1806 {
1807 return (spa->spa_state);
1808 }
1809
1810 spa_load_state_t
spa_load_state(spa_t * spa)1811 spa_load_state(spa_t *spa)
1812 {
1813 return (spa->spa_load_state);
1814 }
1815
1816 uint64_t
spa_freeze_txg(spa_t * spa)1817 spa_freeze_txg(spa_t *spa)
1818 {
1819 return (spa->spa_freeze_txg);
1820 }
1821
1822 /*
1823 * Return the inflated asize for a logical write in bytes. This is used by the
1824 * DMU to calculate the space a logical write will require on disk.
1825 * If lsize is smaller than the largest physical block size allocatable on this
1826 * pool we use its value instead, since the write will end up using the whole
1827 * block anyway.
1828 */
1829 uint64_t
spa_get_worst_case_asize(spa_t * spa,uint64_t lsize)1830 spa_get_worst_case_asize(spa_t *spa, uint64_t lsize)
1831 {
1832 if (lsize == 0)
1833 return (0); /* No inflation needed */
1834 return (MAX(lsize, 1 << spa->spa_max_ashift) * spa_asize_inflation);
1835 }
1836
1837 /*
1838 * Return the amount of slop space in bytes. It is typically 1/32 of the pool
1839 * (3.2%), minus the embedded log space. On very small pools, it may be
1840 * slightly larger than this. On very large pools, it will be capped to
1841 * the value of spa_max_slop. The embedded log space is not included in
1842 * spa_dspace. By subtracting it, the usable space (per "zfs list") is a
1843 * constant 97% of the total space, regardless of metaslab size (assuming the
1844 * default spa_slop_shift=5 and a non-tiny pool).
1845 *
1846 * See the comment above spa_slop_shift for more details.
1847 */
1848 uint64_t
spa_get_slop_space(spa_t * spa)1849 spa_get_slop_space(spa_t *spa)
1850 {
1851 uint64_t space = 0;
1852 uint64_t slop = 0;
1853
1854 /*
1855 * Make sure spa_dedup_dspace has been set.
1856 */
1857 if (spa->spa_dedup_dspace == ~0ULL)
1858 spa_update_dspace(spa);
1859
1860 /*
1861 * spa_get_dspace() includes the space only logically "used" by
1862 * deduplicated data, so since it's not useful to reserve more
1863 * space with more deduplicated data, we subtract that out here.
1864 */
1865 space =
1866 spa_get_dspace(spa) - spa->spa_dedup_dspace - brt_get_dspace(spa);
1867 slop = MIN(space >> spa_slop_shift, spa_max_slop);
1868
1869 /*
1870 * Subtract the embedded log space, but no more than half the (3.2%)
1871 * unusable space. Note, the "no more than half" is only relevant if
1872 * zfs_embedded_slog_min_ms >> spa_slop_shift < 2, which is not true by
1873 * default.
1874 */
1875 uint64_t embedded_log =
1876 metaslab_class_get_dspace(spa_embedded_log_class(spa));
1877 slop -= MIN(embedded_log, slop >> 1);
1878
1879 /*
1880 * Slop space should be at least spa_min_slop, but no more than half
1881 * the entire pool.
1882 */
1883 slop = MAX(slop, MIN(space >> 1, spa_min_slop));
1884 return (slop);
1885 }
1886
1887 uint64_t
spa_get_dspace(spa_t * spa)1888 spa_get_dspace(spa_t *spa)
1889 {
1890 return (spa->spa_dspace);
1891 }
1892
1893 uint64_t
spa_get_checkpoint_space(spa_t * spa)1894 spa_get_checkpoint_space(spa_t *spa)
1895 {
1896 return (spa->spa_checkpoint_info.sci_dspace);
1897 }
1898
1899 void
spa_update_dspace(spa_t * spa)1900 spa_update_dspace(spa_t *spa)
1901 {
1902 spa->spa_dspace = metaslab_class_get_dspace(spa_normal_class(spa)) +
1903 ddt_get_dedup_dspace(spa) + brt_get_dspace(spa);
1904 if (spa->spa_nonallocating_dspace > 0) {
1905 /*
1906 * Subtract the space provided by all non-allocating vdevs that
1907 * contribute to dspace. If a file is overwritten, its old
1908 * blocks are freed and new blocks are allocated. If there are
1909 * no snapshots of the file, the available space should remain
1910 * the same. The old blocks could be freed from the
1911 * non-allocating vdev, but the new blocks must be allocated on
1912 * other (allocating) vdevs. By reserving the entire size of
1913 * the non-allocating vdevs (including allocated space), we
1914 * ensure that there will be enough space on the allocating
1915 * vdevs for this file overwrite to succeed.
1916 *
1917 * Note that the DMU/DSL doesn't actually know or care
1918 * how much space is allocated (it does its own tracking
1919 * of how much space has been logically used). So it
1920 * doesn't matter that the data we are moving may be
1921 * allocated twice (on the old device and the new device).
1922 */
1923 ASSERT3U(spa->spa_dspace, >=, spa->spa_nonallocating_dspace);
1924 spa->spa_dspace -= spa->spa_nonallocating_dspace;
1925 }
1926 }
1927
1928 /*
1929 * Return the failure mode that has been set to this pool. The default
1930 * behavior will be to block all I/Os when a complete failure occurs.
1931 */
1932 uint64_t
spa_get_failmode(spa_t * spa)1933 spa_get_failmode(spa_t *spa)
1934 {
1935 return (spa->spa_failmode);
1936 }
1937
1938 boolean_t
spa_suspended(spa_t * spa)1939 spa_suspended(spa_t *spa)
1940 {
1941 return (spa->spa_suspended != ZIO_SUSPEND_NONE);
1942 }
1943
1944 uint64_t
spa_version(spa_t * spa)1945 spa_version(spa_t *spa)
1946 {
1947 return (spa->spa_ubsync.ub_version);
1948 }
1949
1950 boolean_t
spa_deflate(spa_t * spa)1951 spa_deflate(spa_t *spa)
1952 {
1953 return (spa->spa_deflate);
1954 }
1955
1956 metaslab_class_t *
spa_normal_class(spa_t * spa)1957 spa_normal_class(spa_t *spa)
1958 {
1959 return (spa->spa_normal_class);
1960 }
1961
1962 metaslab_class_t *
spa_log_class(spa_t * spa)1963 spa_log_class(spa_t *spa)
1964 {
1965 return (spa->spa_log_class);
1966 }
1967
1968 metaslab_class_t *
spa_embedded_log_class(spa_t * spa)1969 spa_embedded_log_class(spa_t *spa)
1970 {
1971 return (spa->spa_embedded_log_class);
1972 }
1973
1974 metaslab_class_t *
spa_special_class(spa_t * spa)1975 spa_special_class(spa_t *spa)
1976 {
1977 return (spa->spa_special_class);
1978 }
1979
1980 metaslab_class_t *
spa_dedup_class(spa_t * spa)1981 spa_dedup_class(spa_t *spa)
1982 {
1983 return (spa->spa_dedup_class);
1984 }
1985
1986 /*
1987 * Locate an appropriate allocation class
1988 */
1989 metaslab_class_t *
spa_preferred_class(spa_t * spa,uint64_t size,dmu_object_type_t objtype,uint_t level,uint_t special_smallblk)1990 spa_preferred_class(spa_t *spa, uint64_t size, dmu_object_type_t objtype,
1991 uint_t level, uint_t special_smallblk)
1992 {
1993 /*
1994 * ZIL allocations determine their class in zio_alloc_zil().
1995 */
1996 ASSERT(objtype != DMU_OT_INTENT_LOG);
1997
1998 boolean_t has_special_class = spa->spa_special_class->mc_groups != 0;
1999
2000 if (DMU_OT_IS_DDT(objtype)) {
2001 if (spa->spa_dedup_class->mc_groups != 0)
2002 return (spa_dedup_class(spa));
2003 else if (has_special_class && zfs_ddt_data_is_special)
2004 return (spa_special_class(spa));
2005 else
2006 return (spa_normal_class(spa));
2007 }
2008
2009 /* Indirect blocks for user data can land in special if allowed */
2010 if (level > 0 && (DMU_OT_IS_FILE(objtype) || objtype == DMU_OT_ZVOL)) {
2011 if (has_special_class && zfs_user_indirect_is_special)
2012 return (spa_special_class(spa));
2013 else
2014 return (spa_normal_class(spa));
2015 }
2016
2017 if (DMU_OT_IS_METADATA(objtype) || level > 0) {
2018 if (has_special_class)
2019 return (spa_special_class(spa));
2020 else
2021 return (spa_normal_class(spa));
2022 }
2023
2024 /*
2025 * Allow small file blocks in special class in some cases (like
2026 * for the dRAID vdev feature). But always leave a reserve of
2027 * zfs_special_class_metadata_reserve_pct exclusively for metadata.
2028 */
2029 if (DMU_OT_IS_FILE(objtype) &&
2030 has_special_class && size <= special_smallblk) {
2031 metaslab_class_t *special = spa_special_class(spa);
2032 uint64_t alloc = metaslab_class_get_alloc(special);
2033 uint64_t space = metaslab_class_get_space(special);
2034 uint64_t limit =
2035 (space * (100 - zfs_special_class_metadata_reserve_pct))
2036 / 100;
2037
2038 if (alloc < limit)
2039 return (special);
2040 }
2041
2042 return (spa_normal_class(spa));
2043 }
2044
2045 void
spa_evicting_os_register(spa_t * spa,objset_t * os)2046 spa_evicting_os_register(spa_t *spa, objset_t *os)
2047 {
2048 mutex_enter(&spa->spa_evicting_os_lock);
2049 list_insert_head(&spa->spa_evicting_os_list, os);
2050 mutex_exit(&spa->spa_evicting_os_lock);
2051 }
2052
2053 void
spa_evicting_os_deregister(spa_t * spa,objset_t * os)2054 spa_evicting_os_deregister(spa_t *spa, objset_t *os)
2055 {
2056 mutex_enter(&spa->spa_evicting_os_lock);
2057 list_remove(&spa->spa_evicting_os_list, os);
2058 cv_broadcast(&spa->spa_evicting_os_cv);
2059 mutex_exit(&spa->spa_evicting_os_lock);
2060 }
2061
2062 void
spa_evicting_os_wait(spa_t * spa)2063 spa_evicting_os_wait(spa_t *spa)
2064 {
2065 mutex_enter(&spa->spa_evicting_os_lock);
2066 while (!list_is_empty(&spa->spa_evicting_os_list))
2067 cv_wait(&spa->spa_evicting_os_cv, &spa->spa_evicting_os_lock);
2068 mutex_exit(&spa->spa_evicting_os_lock);
2069
2070 dmu_buf_user_evict_wait();
2071 }
2072
2073 int
spa_max_replication(spa_t * spa)2074 spa_max_replication(spa_t *spa)
2075 {
2076 /*
2077 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
2078 * handle BPs with more than one DVA allocated. Set our max
2079 * replication level accordingly.
2080 */
2081 if (spa_version(spa) < SPA_VERSION_DITTO_BLOCKS)
2082 return (1);
2083 return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override));
2084 }
2085
2086 int
spa_prev_software_version(spa_t * spa)2087 spa_prev_software_version(spa_t *spa)
2088 {
2089 return (spa->spa_prev_software_version);
2090 }
2091
2092 uint64_t
spa_deadman_synctime(spa_t * spa)2093 spa_deadman_synctime(spa_t *spa)
2094 {
2095 return (spa->spa_deadman_synctime);
2096 }
2097
2098 spa_autotrim_t
spa_get_autotrim(spa_t * spa)2099 spa_get_autotrim(spa_t *spa)
2100 {
2101 return (spa->spa_autotrim);
2102 }
2103
2104 uint64_t
spa_deadman_ziotime(spa_t * spa)2105 spa_deadman_ziotime(spa_t *spa)
2106 {
2107 return (spa->spa_deadman_ziotime);
2108 }
2109
2110 uint64_t
spa_get_deadman_failmode(spa_t * spa)2111 spa_get_deadman_failmode(spa_t *spa)
2112 {
2113 return (spa->spa_deadman_failmode);
2114 }
2115
2116 void
spa_set_deadman_failmode(spa_t * spa,const char * failmode)2117 spa_set_deadman_failmode(spa_t *spa, const char *failmode)
2118 {
2119 if (strcmp(failmode, "wait") == 0)
2120 spa->spa_deadman_failmode = ZIO_FAILURE_MODE_WAIT;
2121 else if (strcmp(failmode, "continue") == 0)
2122 spa->spa_deadman_failmode = ZIO_FAILURE_MODE_CONTINUE;
2123 else if (strcmp(failmode, "panic") == 0)
2124 spa->spa_deadman_failmode = ZIO_FAILURE_MODE_PANIC;
2125 else
2126 spa->spa_deadman_failmode = ZIO_FAILURE_MODE_WAIT;
2127 }
2128
2129 void
spa_set_deadman_ziotime(hrtime_t ns)2130 spa_set_deadman_ziotime(hrtime_t ns)
2131 {
2132 spa_t *spa = NULL;
2133
2134 if (spa_mode_global != SPA_MODE_UNINIT) {
2135 mutex_enter(&spa_namespace_lock);
2136 while ((spa = spa_next(spa)) != NULL)
2137 spa->spa_deadman_ziotime = ns;
2138 mutex_exit(&spa_namespace_lock);
2139 }
2140 }
2141
2142 void
spa_set_deadman_synctime(hrtime_t ns)2143 spa_set_deadman_synctime(hrtime_t ns)
2144 {
2145 spa_t *spa = NULL;
2146
2147 if (spa_mode_global != SPA_MODE_UNINIT) {
2148 mutex_enter(&spa_namespace_lock);
2149 while ((spa = spa_next(spa)) != NULL)
2150 spa->spa_deadman_synctime = ns;
2151 mutex_exit(&spa_namespace_lock);
2152 }
2153 }
2154
2155 uint64_t
dva_get_dsize_sync(spa_t * spa,const dva_t * dva)2156 dva_get_dsize_sync(spa_t *spa, const dva_t *dva)
2157 {
2158 uint64_t asize = DVA_GET_ASIZE(dva);
2159 uint64_t dsize = asize;
2160
2161 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
2162
2163 if (asize != 0 && spa->spa_deflate) {
2164 vdev_t *vd = vdev_lookup_top(spa, DVA_GET_VDEV(dva));
2165 if (vd != NULL)
2166 dsize = (asize >> SPA_MINBLOCKSHIFT) *
2167 vd->vdev_deflate_ratio;
2168 }
2169
2170 return (dsize);
2171 }
2172
2173 uint64_t
bp_get_dsize_sync(spa_t * spa,const blkptr_t * bp)2174 bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp)
2175 {
2176 uint64_t dsize = 0;
2177
2178 for (int d = 0; d < BP_GET_NDVAS(bp); d++)
2179 dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
2180
2181 return (dsize);
2182 }
2183
2184 uint64_t
bp_get_dsize(spa_t * spa,const blkptr_t * bp)2185 bp_get_dsize(spa_t *spa, const blkptr_t *bp)
2186 {
2187 uint64_t dsize = 0;
2188
2189 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2190
2191 for (int d = 0; d < BP_GET_NDVAS(bp); d++)
2192 dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
2193
2194 spa_config_exit(spa, SCL_VDEV, FTAG);
2195
2196 return (dsize);
2197 }
2198
2199 uint64_t
spa_dirty_data(spa_t * spa)2200 spa_dirty_data(spa_t *spa)
2201 {
2202 return (spa->spa_dsl_pool->dp_dirty_total);
2203 }
2204
2205 /*
2206 * ==========================================================================
2207 * SPA Import Progress Routines
2208 * ==========================================================================
2209 */
2210
2211 typedef struct spa_import_progress {
2212 uint64_t pool_guid; /* unique id for updates */
2213 char *pool_name;
2214 spa_load_state_t spa_load_state;
2215 char *spa_load_notes;
2216 uint64_t mmp_sec_remaining; /* MMP activity check */
2217 uint64_t spa_load_max_txg; /* rewind txg */
2218 procfs_list_node_t smh_node;
2219 } spa_import_progress_t;
2220
2221 spa_history_list_t *spa_import_progress_list = NULL;
2222
2223 static int
spa_import_progress_show_header(struct seq_file * f)2224 spa_import_progress_show_header(struct seq_file *f)
2225 {
2226 seq_printf(f, "%-20s %-14s %-14s %-12s %-16s %s\n", "pool_guid",
2227 "load_state", "multihost_secs", "max_txg",
2228 "pool_name", "notes");
2229 return (0);
2230 }
2231
2232 static int
spa_import_progress_show(struct seq_file * f,void * data)2233 spa_import_progress_show(struct seq_file *f, void *data)
2234 {
2235 spa_import_progress_t *sip = (spa_import_progress_t *)data;
2236
2237 seq_printf(f, "%-20llu %-14llu %-14llu %-12llu %-16s %s\n",
2238 (u_longlong_t)sip->pool_guid, (u_longlong_t)sip->spa_load_state,
2239 (u_longlong_t)sip->mmp_sec_remaining,
2240 (u_longlong_t)sip->spa_load_max_txg,
2241 (sip->pool_name ? sip->pool_name : "-"),
2242 (sip->spa_load_notes ? sip->spa_load_notes : "-"));
2243
2244 return (0);
2245 }
2246
2247 /* Remove oldest elements from list until there are no more than 'size' left */
2248 static void
spa_import_progress_truncate(spa_history_list_t * shl,unsigned int size)2249 spa_import_progress_truncate(spa_history_list_t *shl, unsigned int size)
2250 {
2251 spa_import_progress_t *sip;
2252 while (shl->size > size) {
2253 sip = list_remove_head(&shl->procfs_list.pl_list);
2254 if (sip->pool_name)
2255 spa_strfree(sip->pool_name);
2256 if (sip->spa_load_notes)
2257 kmem_strfree(sip->spa_load_notes);
2258 kmem_free(sip, sizeof (spa_import_progress_t));
2259 shl->size--;
2260 }
2261
2262 IMPLY(size == 0, list_is_empty(&shl->procfs_list.pl_list));
2263 }
2264
2265 static void
spa_import_progress_init(void)2266 spa_import_progress_init(void)
2267 {
2268 spa_import_progress_list = kmem_zalloc(sizeof (spa_history_list_t),
2269 KM_SLEEP);
2270
2271 spa_import_progress_list->size = 0;
2272
2273 spa_import_progress_list->procfs_list.pl_private =
2274 spa_import_progress_list;
2275
2276 procfs_list_install("zfs",
2277 NULL,
2278 "import_progress",
2279 0644,
2280 &spa_import_progress_list->procfs_list,
2281 spa_import_progress_show,
2282 spa_import_progress_show_header,
2283 NULL,
2284 offsetof(spa_import_progress_t, smh_node));
2285 }
2286
2287 static void
spa_import_progress_destroy(void)2288 spa_import_progress_destroy(void)
2289 {
2290 spa_history_list_t *shl = spa_import_progress_list;
2291 procfs_list_uninstall(&shl->procfs_list);
2292 spa_import_progress_truncate(shl, 0);
2293 procfs_list_destroy(&shl->procfs_list);
2294 kmem_free(shl, sizeof (spa_history_list_t));
2295 }
2296
2297 int
spa_import_progress_set_state(uint64_t pool_guid,spa_load_state_t load_state)2298 spa_import_progress_set_state(uint64_t pool_guid,
2299 spa_load_state_t load_state)
2300 {
2301 spa_history_list_t *shl = spa_import_progress_list;
2302 spa_import_progress_t *sip;
2303 int error = ENOENT;
2304
2305 if (shl->size == 0)
2306 return (0);
2307
2308 mutex_enter(&shl->procfs_list.pl_lock);
2309 for (sip = list_tail(&shl->procfs_list.pl_list); sip != NULL;
2310 sip = list_prev(&shl->procfs_list.pl_list, sip)) {
2311 if (sip->pool_guid == pool_guid) {
2312 sip->spa_load_state = load_state;
2313 if (sip->spa_load_notes != NULL) {
2314 kmem_strfree(sip->spa_load_notes);
2315 sip->spa_load_notes = NULL;
2316 }
2317 error = 0;
2318 break;
2319 }
2320 }
2321 mutex_exit(&shl->procfs_list.pl_lock);
2322
2323 return (error);
2324 }
2325
2326 static void
spa_import_progress_set_notes_impl(spa_t * spa,boolean_t log_dbgmsg,const char * fmt,va_list adx)2327 spa_import_progress_set_notes_impl(spa_t *spa, boolean_t log_dbgmsg,
2328 const char *fmt, va_list adx)
2329 {
2330 spa_history_list_t *shl = spa_import_progress_list;
2331 spa_import_progress_t *sip;
2332 uint64_t pool_guid = spa_guid(spa);
2333
2334 if (shl->size == 0)
2335 return;
2336
2337 char *notes = kmem_vasprintf(fmt, adx);
2338
2339 mutex_enter(&shl->procfs_list.pl_lock);
2340 for (sip = list_tail(&shl->procfs_list.pl_list); sip != NULL;
2341 sip = list_prev(&shl->procfs_list.pl_list, sip)) {
2342 if (sip->pool_guid == pool_guid) {
2343 if (sip->spa_load_notes != NULL) {
2344 kmem_strfree(sip->spa_load_notes);
2345 sip->spa_load_notes = NULL;
2346 }
2347 sip->spa_load_notes = notes;
2348 if (log_dbgmsg)
2349 zfs_dbgmsg("'%s' %s", sip->pool_name, notes);
2350 notes = NULL;
2351 break;
2352 }
2353 }
2354 mutex_exit(&shl->procfs_list.pl_lock);
2355 if (notes != NULL)
2356 kmem_strfree(notes);
2357 }
2358
2359 void
spa_import_progress_set_notes(spa_t * spa,const char * fmt,...)2360 spa_import_progress_set_notes(spa_t *spa, const char *fmt, ...)
2361 {
2362 va_list adx;
2363
2364 va_start(adx, fmt);
2365 spa_import_progress_set_notes_impl(spa, B_TRUE, fmt, adx);
2366 va_end(adx);
2367 }
2368
2369 void
spa_import_progress_set_notes_nolog(spa_t * spa,const char * fmt,...)2370 spa_import_progress_set_notes_nolog(spa_t *spa, const char *fmt, ...)
2371 {
2372 va_list adx;
2373
2374 va_start(adx, fmt);
2375 spa_import_progress_set_notes_impl(spa, B_FALSE, fmt, adx);
2376 va_end(adx);
2377 }
2378
2379 int
spa_import_progress_set_max_txg(uint64_t pool_guid,uint64_t load_max_txg)2380 spa_import_progress_set_max_txg(uint64_t pool_guid, uint64_t load_max_txg)
2381 {
2382 spa_history_list_t *shl = spa_import_progress_list;
2383 spa_import_progress_t *sip;
2384 int error = ENOENT;
2385
2386 if (shl->size == 0)
2387 return (0);
2388
2389 mutex_enter(&shl->procfs_list.pl_lock);
2390 for (sip = list_tail(&shl->procfs_list.pl_list); sip != NULL;
2391 sip = list_prev(&shl->procfs_list.pl_list, sip)) {
2392 if (sip->pool_guid == pool_guid) {
2393 sip->spa_load_max_txg = load_max_txg;
2394 error = 0;
2395 break;
2396 }
2397 }
2398 mutex_exit(&shl->procfs_list.pl_lock);
2399
2400 return (error);
2401 }
2402
2403 int
spa_import_progress_set_mmp_check(uint64_t pool_guid,uint64_t mmp_sec_remaining)2404 spa_import_progress_set_mmp_check(uint64_t pool_guid,
2405 uint64_t mmp_sec_remaining)
2406 {
2407 spa_history_list_t *shl = spa_import_progress_list;
2408 spa_import_progress_t *sip;
2409 int error = ENOENT;
2410
2411 if (shl->size == 0)
2412 return (0);
2413
2414 mutex_enter(&shl->procfs_list.pl_lock);
2415 for (sip = list_tail(&shl->procfs_list.pl_list); sip != NULL;
2416 sip = list_prev(&shl->procfs_list.pl_list, sip)) {
2417 if (sip->pool_guid == pool_guid) {
2418 sip->mmp_sec_remaining = mmp_sec_remaining;
2419 error = 0;
2420 break;
2421 }
2422 }
2423 mutex_exit(&shl->procfs_list.pl_lock);
2424
2425 return (error);
2426 }
2427
2428 /*
2429 * A new import is in progress, add an entry.
2430 */
2431 void
spa_import_progress_add(spa_t * spa)2432 spa_import_progress_add(spa_t *spa)
2433 {
2434 spa_history_list_t *shl = spa_import_progress_list;
2435 spa_import_progress_t *sip;
2436 const char *poolname = NULL;
2437
2438 sip = kmem_zalloc(sizeof (spa_import_progress_t), KM_SLEEP);
2439 sip->pool_guid = spa_guid(spa);
2440
2441 (void) nvlist_lookup_string(spa->spa_config, ZPOOL_CONFIG_POOL_NAME,
2442 &poolname);
2443 if (poolname == NULL)
2444 poolname = spa_name(spa);
2445 sip->pool_name = spa_strdup(poolname);
2446 sip->spa_load_state = spa_load_state(spa);
2447 sip->spa_load_notes = NULL;
2448
2449 mutex_enter(&shl->procfs_list.pl_lock);
2450 procfs_list_add(&shl->procfs_list, sip);
2451 shl->size++;
2452 mutex_exit(&shl->procfs_list.pl_lock);
2453 }
2454
2455 void
spa_import_progress_remove(uint64_t pool_guid)2456 spa_import_progress_remove(uint64_t pool_guid)
2457 {
2458 spa_history_list_t *shl = spa_import_progress_list;
2459 spa_import_progress_t *sip;
2460
2461 mutex_enter(&shl->procfs_list.pl_lock);
2462 for (sip = list_tail(&shl->procfs_list.pl_list); sip != NULL;
2463 sip = list_prev(&shl->procfs_list.pl_list, sip)) {
2464 if (sip->pool_guid == pool_guid) {
2465 if (sip->pool_name)
2466 spa_strfree(sip->pool_name);
2467 if (sip->spa_load_notes)
2468 spa_strfree(sip->spa_load_notes);
2469 list_remove(&shl->procfs_list.pl_list, sip);
2470 shl->size--;
2471 kmem_free(sip, sizeof (spa_import_progress_t));
2472 break;
2473 }
2474 }
2475 mutex_exit(&shl->procfs_list.pl_lock);
2476 }
2477
2478 /*
2479 * ==========================================================================
2480 * Initialization and Termination
2481 * ==========================================================================
2482 */
2483
2484 static int
spa_name_compare(const void * a1,const void * a2)2485 spa_name_compare(const void *a1, const void *a2)
2486 {
2487 const spa_t *s1 = a1;
2488 const spa_t *s2 = a2;
2489 int s;
2490
2491 s = strcmp(s1->spa_name, s2->spa_name);
2492
2493 return (TREE_ISIGN(s));
2494 }
2495
2496 void
spa_boot_init(void)2497 spa_boot_init(void)
2498 {
2499 spa_config_load();
2500 }
2501
2502 void
spa_init(spa_mode_t mode)2503 spa_init(spa_mode_t mode)
2504 {
2505 mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL);
2506 mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL);
2507 mutex_init(&spa_l2cache_lock, NULL, MUTEX_DEFAULT, NULL);
2508 cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL);
2509
2510 avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t),
2511 offsetof(spa_t, spa_avl));
2512
2513 avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_aux_t),
2514 offsetof(spa_aux_t, aux_avl));
2515
2516 avl_create(&spa_l2cache_avl, spa_l2cache_compare, sizeof (spa_aux_t),
2517 offsetof(spa_aux_t, aux_avl));
2518
2519 spa_mode_global = mode;
2520
2521 #ifndef _KERNEL
2522 if (spa_mode_global != SPA_MODE_READ && dprintf_find_string("watch")) {
2523 struct sigaction sa;
2524
2525 sa.sa_flags = SA_SIGINFO;
2526 sigemptyset(&sa.sa_mask);
2527 sa.sa_sigaction = arc_buf_sigsegv;
2528
2529 if (sigaction(SIGSEGV, &sa, NULL) == -1) {
2530 perror("could not enable watchpoints: "
2531 "sigaction(SIGSEGV, ...) = ");
2532 } else {
2533 arc_watch = B_TRUE;
2534 }
2535 }
2536 #endif
2537
2538 fm_init();
2539 zfs_refcount_init();
2540 unique_init();
2541 zfs_btree_init();
2542 metaslab_stat_init();
2543 brt_init();
2544 ddt_init();
2545 zio_init();
2546 dmu_init();
2547 zil_init();
2548 vdev_mirror_stat_init();
2549 vdev_raidz_math_init();
2550 vdev_file_init();
2551 zfs_prop_init();
2552 chksum_init();
2553 zpool_prop_init();
2554 zpool_feature_init();
2555 spa_config_load();
2556 vdev_prop_init();
2557 l2arc_start();
2558 scan_init();
2559 qat_init();
2560 spa_import_progress_init();
2561 }
2562
2563 void
spa_fini(void)2564 spa_fini(void)
2565 {
2566 l2arc_stop();
2567
2568 spa_evict_all();
2569
2570 vdev_file_fini();
2571 vdev_mirror_stat_fini();
2572 vdev_raidz_math_fini();
2573 chksum_fini();
2574 zil_fini();
2575 dmu_fini();
2576 zio_fini();
2577 ddt_fini();
2578 brt_fini();
2579 metaslab_stat_fini();
2580 zfs_btree_fini();
2581 unique_fini();
2582 zfs_refcount_fini();
2583 fm_fini();
2584 scan_fini();
2585 qat_fini();
2586 spa_import_progress_destroy();
2587
2588 avl_destroy(&spa_namespace_avl);
2589 avl_destroy(&spa_spare_avl);
2590 avl_destroy(&spa_l2cache_avl);
2591
2592 cv_destroy(&spa_namespace_cv);
2593 mutex_destroy(&spa_namespace_lock);
2594 mutex_destroy(&spa_spare_lock);
2595 mutex_destroy(&spa_l2cache_lock);
2596 }
2597
2598 /*
2599 * Return whether this pool has a dedicated slog device. No locking needed.
2600 * It's not a problem if the wrong answer is returned as it's only for
2601 * performance and not correctness.
2602 */
2603 boolean_t
spa_has_slogs(spa_t * spa)2604 spa_has_slogs(spa_t *spa)
2605 {
2606 return (spa->spa_log_class->mc_groups != 0);
2607 }
2608
2609 spa_log_state_t
spa_get_log_state(spa_t * spa)2610 spa_get_log_state(spa_t *spa)
2611 {
2612 return (spa->spa_log_state);
2613 }
2614
2615 void
spa_set_log_state(spa_t * spa,spa_log_state_t state)2616 spa_set_log_state(spa_t *spa, spa_log_state_t state)
2617 {
2618 spa->spa_log_state = state;
2619 }
2620
2621 boolean_t
spa_is_root(spa_t * spa)2622 spa_is_root(spa_t *spa)
2623 {
2624 return (spa->spa_is_root);
2625 }
2626
2627 boolean_t
spa_writeable(spa_t * spa)2628 spa_writeable(spa_t *spa)
2629 {
2630 return (!!(spa->spa_mode & SPA_MODE_WRITE) && spa->spa_trust_config);
2631 }
2632
2633 /*
2634 * Returns true if there is a pending sync task in any of the current
2635 * syncing txg, the current quiescing txg, or the current open txg.
2636 */
2637 boolean_t
spa_has_pending_synctask(spa_t * spa)2638 spa_has_pending_synctask(spa_t *spa)
2639 {
2640 return (!txg_all_lists_empty(&spa->spa_dsl_pool->dp_sync_tasks) ||
2641 !txg_all_lists_empty(&spa->spa_dsl_pool->dp_early_sync_tasks));
2642 }
2643
2644 spa_mode_t
spa_mode(spa_t * spa)2645 spa_mode(spa_t *spa)
2646 {
2647 return (spa->spa_mode);
2648 }
2649
2650 uint64_t
spa_bootfs(spa_t * spa)2651 spa_bootfs(spa_t *spa)
2652 {
2653 return (spa->spa_bootfs);
2654 }
2655
2656 uint64_t
spa_delegation(spa_t * spa)2657 spa_delegation(spa_t *spa)
2658 {
2659 return (spa->spa_delegation);
2660 }
2661
2662 objset_t *
spa_meta_objset(spa_t * spa)2663 spa_meta_objset(spa_t *spa)
2664 {
2665 return (spa->spa_meta_objset);
2666 }
2667
2668 enum zio_checksum
spa_dedup_checksum(spa_t * spa)2669 spa_dedup_checksum(spa_t *spa)
2670 {
2671 return (spa->spa_dedup_checksum);
2672 }
2673
2674 /*
2675 * Reset pool scan stat per scan pass (or reboot).
2676 */
2677 void
spa_scan_stat_init(spa_t * spa)2678 spa_scan_stat_init(spa_t *spa)
2679 {
2680 /* data not stored on disk */
2681 spa->spa_scan_pass_start = gethrestime_sec();
2682 if (dsl_scan_is_paused_scrub(spa->spa_dsl_pool->dp_scan))
2683 spa->spa_scan_pass_scrub_pause = spa->spa_scan_pass_start;
2684 else
2685 spa->spa_scan_pass_scrub_pause = 0;
2686
2687 if (dsl_errorscrub_is_paused(spa->spa_dsl_pool->dp_scan))
2688 spa->spa_scan_pass_errorscrub_pause = spa->spa_scan_pass_start;
2689 else
2690 spa->spa_scan_pass_errorscrub_pause = 0;
2691
2692 spa->spa_scan_pass_scrub_spent_paused = 0;
2693 spa->spa_scan_pass_exam = 0;
2694 spa->spa_scan_pass_issued = 0;
2695
2696 // error scrub stats
2697 spa->spa_scan_pass_errorscrub_spent_paused = 0;
2698 }
2699
2700 /*
2701 * Get scan stats for zpool status reports
2702 */
2703 int
spa_scan_get_stats(spa_t * spa,pool_scan_stat_t * ps)2704 spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps)
2705 {
2706 dsl_scan_t *scn = spa->spa_dsl_pool ? spa->spa_dsl_pool->dp_scan : NULL;
2707
2708 if (scn == NULL || (scn->scn_phys.scn_func == POOL_SCAN_NONE &&
2709 scn->errorscrub_phys.dep_func == POOL_SCAN_NONE))
2710 return (SET_ERROR(ENOENT));
2711
2712 memset(ps, 0, sizeof (pool_scan_stat_t));
2713
2714 /* data stored on disk */
2715 ps->pss_func = scn->scn_phys.scn_func;
2716 ps->pss_state = scn->scn_phys.scn_state;
2717 ps->pss_start_time = scn->scn_phys.scn_start_time;
2718 ps->pss_end_time = scn->scn_phys.scn_end_time;
2719 ps->pss_to_examine = scn->scn_phys.scn_to_examine;
2720 ps->pss_examined = scn->scn_phys.scn_examined;
2721 ps->pss_skipped = scn->scn_phys.scn_skipped;
2722 ps->pss_processed = scn->scn_phys.scn_processed;
2723 ps->pss_errors = scn->scn_phys.scn_errors;
2724
2725 /* data not stored on disk */
2726 ps->pss_pass_exam = spa->spa_scan_pass_exam;
2727 ps->pss_pass_start = spa->spa_scan_pass_start;
2728 ps->pss_pass_scrub_pause = spa->spa_scan_pass_scrub_pause;
2729 ps->pss_pass_scrub_spent_paused = spa->spa_scan_pass_scrub_spent_paused;
2730 ps->pss_pass_issued = spa->spa_scan_pass_issued;
2731 ps->pss_issued =
2732 scn->scn_issued_before_pass + spa->spa_scan_pass_issued;
2733
2734 /* error scrub data stored on disk */
2735 ps->pss_error_scrub_func = scn->errorscrub_phys.dep_func;
2736 ps->pss_error_scrub_state = scn->errorscrub_phys.dep_state;
2737 ps->pss_error_scrub_start = scn->errorscrub_phys.dep_start_time;
2738 ps->pss_error_scrub_end = scn->errorscrub_phys.dep_end_time;
2739 ps->pss_error_scrub_examined = scn->errorscrub_phys.dep_examined;
2740 ps->pss_error_scrub_to_be_examined =
2741 scn->errorscrub_phys.dep_to_examine;
2742
2743 /* error scrub data not stored on disk */
2744 ps->pss_pass_error_scrub_pause = spa->spa_scan_pass_errorscrub_pause;
2745
2746 return (0);
2747 }
2748
2749 int
spa_maxblocksize(spa_t * spa)2750 spa_maxblocksize(spa_t *spa)
2751 {
2752 if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS))
2753 return (SPA_MAXBLOCKSIZE);
2754 else
2755 return (SPA_OLD_MAXBLOCKSIZE);
2756 }
2757
2758
2759 /*
2760 * Returns the txg that the last device removal completed. No indirect mappings
2761 * have been added since this txg.
2762 */
2763 uint64_t
spa_get_last_removal_txg(spa_t * spa)2764 spa_get_last_removal_txg(spa_t *spa)
2765 {
2766 uint64_t vdevid;
2767 uint64_t ret = -1ULL;
2768
2769 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2770 /*
2771 * sr_prev_indirect_vdev is only modified while holding all the
2772 * config locks, so it is sufficient to hold SCL_VDEV as reader when
2773 * examining it.
2774 */
2775 vdevid = spa->spa_removing_phys.sr_prev_indirect_vdev;
2776
2777 while (vdevid != -1ULL) {
2778 vdev_t *vd = vdev_lookup_top(spa, vdevid);
2779 vdev_indirect_births_t *vib = vd->vdev_indirect_births;
2780
2781 ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops);
2782
2783 /*
2784 * If the removal did not remap any data, we don't care.
2785 */
2786 if (vdev_indirect_births_count(vib) != 0) {
2787 ret = vdev_indirect_births_last_entry_txg(vib);
2788 break;
2789 }
2790
2791 vdevid = vd->vdev_indirect_config.vic_prev_indirect_vdev;
2792 }
2793 spa_config_exit(spa, SCL_VDEV, FTAG);
2794
2795 IMPLY(ret != -1ULL,
2796 spa_feature_is_active(spa, SPA_FEATURE_DEVICE_REMOVAL));
2797
2798 return (ret);
2799 }
2800
2801 int
spa_maxdnodesize(spa_t * spa)2802 spa_maxdnodesize(spa_t *spa)
2803 {
2804 if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_DNODE))
2805 return (DNODE_MAX_SIZE);
2806 else
2807 return (DNODE_MIN_SIZE);
2808 }
2809
2810 boolean_t
spa_multihost(spa_t * spa)2811 spa_multihost(spa_t *spa)
2812 {
2813 return (spa->spa_multihost ? B_TRUE : B_FALSE);
2814 }
2815
2816 uint32_t
spa_get_hostid(spa_t * spa)2817 spa_get_hostid(spa_t *spa)
2818 {
2819 return (spa->spa_hostid);
2820 }
2821
2822 boolean_t
spa_trust_config(spa_t * spa)2823 spa_trust_config(spa_t *spa)
2824 {
2825 return (spa->spa_trust_config);
2826 }
2827
2828 uint64_t
spa_missing_tvds_allowed(spa_t * spa)2829 spa_missing_tvds_allowed(spa_t *spa)
2830 {
2831 return (spa->spa_missing_tvds_allowed);
2832 }
2833
2834 space_map_t *
spa_syncing_log_sm(spa_t * spa)2835 spa_syncing_log_sm(spa_t *spa)
2836 {
2837 return (spa->spa_syncing_log_sm);
2838 }
2839
2840 void
spa_set_missing_tvds(spa_t * spa,uint64_t missing)2841 spa_set_missing_tvds(spa_t *spa, uint64_t missing)
2842 {
2843 spa->spa_missing_tvds = missing;
2844 }
2845
2846 /*
2847 * Return the pool state string ("ONLINE", "DEGRADED", "SUSPENDED", etc).
2848 */
2849 const char *
spa_state_to_name(spa_t * spa)2850 spa_state_to_name(spa_t *spa)
2851 {
2852 ASSERT3P(spa, !=, NULL);
2853
2854 /*
2855 * it is possible for the spa to exist, without root vdev
2856 * as the spa transitions during import/export
2857 */
2858 vdev_t *rvd = spa->spa_root_vdev;
2859 if (rvd == NULL) {
2860 return ("TRANSITIONING");
2861 }
2862 vdev_state_t state = rvd->vdev_state;
2863 vdev_aux_t aux = rvd->vdev_stat.vs_aux;
2864
2865 if (spa_suspended(spa))
2866 return ("SUSPENDED");
2867
2868 switch (state) {
2869 case VDEV_STATE_CLOSED:
2870 case VDEV_STATE_OFFLINE:
2871 return ("OFFLINE");
2872 case VDEV_STATE_REMOVED:
2873 return ("REMOVED");
2874 case VDEV_STATE_CANT_OPEN:
2875 if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
2876 return ("FAULTED");
2877 else if (aux == VDEV_AUX_SPLIT_POOL)
2878 return ("SPLIT");
2879 else
2880 return ("UNAVAIL");
2881 case VDEV_STATE_FAULTED:
2882 return ("FAULTED");
2883 case VDEV_STATE_DEGRADED:
2884 return ("DEGRADED");
2885 case VDEV_STATE_HEALTHY:
2886 return ("ONLINE");
2887 default:
2888 break;
2889 }
2890
2891 return ("UNKNOWN");
2892 }
2893
2894 boolean_t
spa_top_vdevs_spacemap_addressable(spa_t * spa)2895 spa_top_vdevs_spacemap_addressable(spa_t *spa)
2896 {
2897 vdev_t *rvd = spa->spa_root_vdev;
2898 for (uint64_t c = 0; c < rvd->vdev_children; c++) {
2899 if (!vdev_is_spacemap_addressable(rvd->vdev_child[c]))
2900 return (B_FALSE);
2901 }
2902 return (B_TRUE);
2903 }
2904
2905 boolean_t
spa_has_checkpoint(spa_t * spa)2906 spa_has_checkpoint(spa_t *spa)
2907 {
2908 return (spa->spa_checkpoint_txg != 0);
2909 }
2910
2911 boolean_t
spa_importing_readonly_checkpoint(spa_t * spa)2912 spa_importing_readonly_checkpoint(spa_t *spa)
2913 {
2914 return ((spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT) &&
2915 spa->spa_mode == SPA_MODE_READ);
2916 }
2917
2918 uint64_t
spa_min_claim_txg(spa_t * spa)2919 spa_min_claim_txg(spa_t *spa)
2920 {
2921 uint64_t checkpoint_txg = spa->spa_uberblock.ub_checkpoint_txg;
2922
2923 if (checkpoint_txg != 0)
2924 return (checkpoint_txg + 1);
2925
2926 return (spa->spa_first_txg);
2927 }
2928
2929 /*
2930 * If there is a checkpoint, async destroys may consume more space from
2931 * the pool instead of freeing it. In an attempt to save the pool from
2932 * getting suspended when it is about to run out of space, we stop
2933 * processing async destroys.
2934 */
2935 boolean_t
spa_suspend_async_destroy(spa_t * spa)2936 spa_suspend_async_destroy(spa_t *spa)
2937 {
2938 dsl_pool_t *dp = spa_get_dsl(spa);
2939
2940 uint64_t unreserved = dsl_pool_unreserved_space(dp,
2941 ZFS_SPACE_CHECK_EXTRA_RESERVED);
2942 uint64_t used = dsl_dir_phys(dp->dp_root_dir)->dd_used_bytes;
2943 uint64_t avail = (unreserved > used) ? (unreserved - used) : 0;
2944
2945 if (spa_has_checkpoint(spa) && avail == 0)
2946 return (B_TRUE);
2947
2948 return (B_FALSE);
2949 }
2950
2951 #if defined(_KERNEL)
2952
2953 int
param_set_deadman_failmode_common(const char * val)2954 param_set_deadman_failmode_common(const char *val)
2955 {
2956 spa_t *spa = NULL;
2957 char *p;
2958
2959 if (val == NULL)
2960 return (SET_ERROR(EINVAL));
2961
2962 if ((p = strchr(val, '\n')) != NULL)
2963 *p = '\0';
2964
2965 if (strcmp(val, "wait") != 0 && strcmp(val, "continue") != 0 &&
2966 strcmp(val, "panic"))
2967 return (SET_ERROR(EINVAL));
2968
2969 if (spa_mode_global != SPA_MODE_UNINIT) {
2970 mutex_enter(&spa_namespace_lock);
2971 while ((spa = spa_next(spa)) != NULL)
2972 spa_set_deadman_failmode(spa, val);
2973 mutex_exit(&spa_namespace_lock);
2974 }
2975
2976 return (0);
2977 }
2978 #endif
2979
2980 /* Namespace manipulation */
2981 EXPORT_SYMBOL(spa_lookup);
2982 EXPORT_SYMBOL(spa_add);
2983 EXPORT_SYMBOL(spa_remove);
2984 EXPORT_SYMBOL(spa_next);
2985
2986 /* Refcount functions */
2987 EXPORT_SYMBOL(spa_open_ref);
2988 EXPORT_SYMBOL(spa_close);
2989 EXPORT_SYMBOL(spa_refcount_zero);
2990
2991 /* Pool configuration lock */
2992 EXPORT_SYMBOL(spa_config_tryenter);
2993 EXPORT_SYMBOL(spa_config_enter);
2994 EXPORT_SYMBOL(spa_config_exit);
2995 EXPORT_SYMBOL(spa_config_held);
2996
2997 /* Pool vdev add/remove lock */
2998 EXPORT_SYMBOL(spa_vdev_enter);
2999 EXPORT_SYMBOL(spa_vdev_exit);
3000
3001 /* Pool vdev state change lock */
3002 EXPORT_SYMBOL(spa_vdev_state_enter);
3003 EXPORT_SYMBOL(spa_vdev_state_exit);
3004
3005 /* Accessor functions */
3006 EXPORT_SYMBOL(spa_shutting_down);
3007 EXPORT_SYMBOL(spa_get_dsl);
3008 EXPORT_SYMBOL(spa_get_rootblkptr);
3009 EXPORT_SYMBOL(spa_set_rootblkptr);
3010 EXPORT_SYMBOL(spa_altroot);
3011 EXPORT_SYMBOL(spa_sync_pass);
3012 EXPORT_SYMBOL(spa_name);
3013 EXPORT_SYMBOL(spa_guid);
3014 EXPORT_SYMBOL(spa_last_synced_txg);
3015 EXPORT_SYMBOL(spa_first_txg);
3016 EXPORT_SYMBOL(spa_syncing_txg);
3017 EXPORT_SYMBOL(spa_version);
3018 EXPORT_SYMBOL(spa_state);
3019 EXPORT_SYMBOL(spa_load_state);
3020 EXPORT_SYMBOL(spa_freeze_txg);
3021 EXPORT_SYMBOL(spa_get_dspace);
3022 EXPORT_SYMBOL(spa_update_dspace);
3023 EXPORT_SYMBOL(spa_deflate);
3024 EXPORT_SYMBOL(spa_normal_class);
3025 EXPORT_SYMBOL(spa_log_class);
3026 EXPORT_SYMBOL(spa_special_class);
3027 EXPORT_SYMBOL(spa_preferred_class);
3028 EXPORT_SYMBOL(spa_max_replication);
3029 EXPORT_SYMBOL(spa_prev_software_version);
3030 EXPORT_SYMBOL(spa_get_failmode);
3031 EXPORT_SYMBOL(spa_suspended);
3032 EXPORT_SYMBOL(spa_bootfs);
3033 EXPORT_SYMBOL(spa_delegation);
3034 EXPORT_SYMBOL(spa_meta_objset);
3035 EXPORT_SYMBOL(spa_maxblocksize);
3036 EXPORT_SYMBOL(spa_maxdnodesize);
3037
3038 /* Miscellaneous support routines */
3039 EXPORT_SYMBOL(spa_guid_exists);
3040 EXPORT_SYMBOL(spa_strdup);
3041 EXPORT_SYMBOL(spa_strfree);
3042 EXPORT_SYMBOL(spa_generate_guid);
3043 EXPORT_SYMBOL(snprintf_blkptr);
3044 EXPORT_SYMBOL(spa_freeze);
3045 EXPORT_SYMBOL(spa_upgrade);
3046 EXPORT_SYMBOL(spa_evict_all);
3047 EXPORT_SYMBOL(spa_lookup_by_guid);
3048 EXPORT_SYMBOL(spa_has_spare);
3049 EXPORT_SYMBOL(dva_get_dsize_sync);
3050 EXPORT_SYMBOL(bp_get_dsize_sync);
3051 EXPORT_SYMBOL(bp_get_dsize);
3052 EXPORT_SYMBOL(spa_has_slogs);
3053 EXPORT_SYMBOL(spa_is_root);
3054 EXPORT_SYMBOL(spa_writeable);
3055 EXPORT_SYMBOL(spa_mode);
3056 EXPORT_SYMBOL(spa_namespace_lock);
3057 EXPORT_SYMBOL(spa_trust_config);
3058 EXPORT_SYMBOL(spa_missing_tvds_allowed);
3059 EXPORT_SYMBOL(spa_set_missing_tvds);
3060 EXPORT_SYMBOL(spa_state_to_name);
3061 EXPORT_SYMBOL(spa_importing_readonly_checkpoint);
3062 EXPORT_SYMBOL(spa_min_claim_txg);
3063 EXPORT_SYMBOL(spa_suspend_async_destroy);
3064 EXPORT_SYMBOL(spa_has_checkpoint);
3065 EXPORT_SYMBOL(spa_top_vdevs_spacemap_addressable);
3066
3067 ZFS_MODULE_PARAM(zfs, zfs_, flags, UINT, ZMOD_RW,
3068 "Set additional debugging flags");
3069
3070 ZFS_MODULE_PARAM(zfs, zfs_, recover, INT, ZMOD_RW,
3071 "Set to attempt to recover from fatal errors");
3072
3073 ZFS_MODULE_PARAM(zfs, zfs_, free_leak_on_eio, INT, ZMOD_RW,
3074 "Set to ignore IO errors during free and permanently leak the space");
3075
3076 ZFS_MODULE_PARAM(zfs_deadman, zfs_deadman_, checktime_ms, U64, ZMOD_RW,
3077 "Dead I/O check interval in milliseconds");
3078
3079 ZFS_MODULE_PARAM(zfs_deadman, zfs_deadman_, enabled, INT, ZMOD_RW,
3080 "Enable deadman timer");
3081
3082 ZFS_MODULE_PARAM(zfs_spa, spa_, asize_inflation, UINT, ZMOD_RW,
3083 "SPA size estimate multiplication factor");
3084
3085 ZFS_MODULE_PARAM(zfs, zfs_, ddt_data_is_special, INT, ZMOD_RW,
3086 "Place DDT data into the special class");
3087
3088 ZFS_MODULE_PARAM(zfs, zfs_, user_indirect_is_special, INT, ZMOD_RW,
3089 "Place user data indirect blocks into the special class");
3090
3091 /* BEGIN CSTYLED */
3092 ZFS_MODULE_PARAM_CALL(zfs_deadman, zfs_deadman_, failmode,
3093 param_set_deadman_failmode, param_get_charp, ZMOD_RW,
3094 "Failmode for deadman timer");
3095
3096 ZFS_MODULE_PARAM_CALL(zfs_deadman, zfs_deadman_, synctime_ms,
3097 param_set_deadman_synctime, spl_param_get_u64, ZMOD_RW,
3098 "Pool sync expiration time in milliseconds");
3099
3100 ZFS_MODULE_PARAM_CALL(zfs_deadman, zfs_deadman_, ziotime_ms,
3101 param_set_deadman_ziotime, spl_param_get_u64, ZMOD_RW,
3102 "IO expiration time in milliseconds");
3103
3104 ZFS_MODULE_PARAM(zfs, zfs_, special_class_metadata_reserve_pct, UINT, ZMOD_RW,
3105 "Small file blocks in special vdevs depends on this much "
3106 "free space available");
3107 /* END CSTYLED */
3108
3109 ZFS_MODULE_PARAM_CALL(zfs_spa, spa_, slop_shift, param_set_slop_shift,
3110 param_get_uint, ZMOD_RW, "Reserved free space in pool");
3111
3112 ZFS_MODULE_PARAM(zfs, spa_, num_allocators, INT, ZMOD_RW,
3113 "Number of allocators per spa");
3114
3115 ZFS_MODULE_PARAM(zfs, spa_, cpus_per_allocator, INT, ZMOD_RW,
3116 "Minimum number of CPUs per allocators");
3117