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