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