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