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