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