]> git.proxmox.com Git - mirror_zfs-debian.git/blame - module/zfs/spa_misc.c
Imported Upstream version 0.6.4.2
[mirror_zfs-debian.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.
ea04106b 23 * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
3541dc6d 24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
34dc7c2f
BB
25 */
26
34dc7c2f
BB
27#include <sys/zfs_context.h>
28#include <sys/spa_impl.h>
29#include <sys/zio.h>
30#include <sys/zio_checksum.h>
31#include <sys/zio_compress.h>
32#include <sys/dmu.h>
33#include <sys/dmu_tx.h>
34#include <sys/zap.h>
35#include <sys/zil.h>
36#include <sys/vdev_impl.h>
ea04106b 37#include <sys/vdev_file.h>
34dc7c2f
BB
38#include <sys/metaslab.h>
39#include <sys/uberblock_impl.h>
40#include <sys/txg.h>
41#include <sys/avl.h>
42#include <sys/unique.h>
43#include <sys/dsl_pool.h>
44#include <sys/dsl_dir.h>
45#include <sys/dsl_prop.h>
26685276 46#include <sys/fm/util.h>
428870ff 47#include <sys/dsl_scan.h>
34dc7c2f
BB
48#include <sys/fs/zfs.h>
49#include <sys/metaslab_impl.h>
b128c09f 50#include <sys/arc.h>
428870ff 51#include <sys/ddt.h>
a08ee875 52#include <sys/kstat.h>
34dc7c2f 53#include "zfs_prop.h"
9ae529ec 54#include "zfeature_common.h"
34dc7c2f
BB
55
56/*
57 * SPA locking
58 *
59 * There are four basic locks for managing spa_t structures:
60 *
61 * spa_namespace_lock (global mutex)
62 *
63 * This lock must be acquired to do any of the following:
64 *
65 * - Lookup a spa_t by name
66 * - Add or remove a spa_t from the namespace
67 * - Increase spa_refcount from non-zero
68 * - Check if spa_refcount is zero
69 * - Rename a spa_t
70 * - add/remove/attach/detach devices
71 * - Held for the duration of create/destroy/import/export
72 *
73 * It does not need to handle recursion. A create or destroy may
74 * reference objects (files or zvols) in other pools, but by
75 * definition they must have an existing reference, and will never need
76 * to lookup a spa_t by name.
77 *
78 * spa_refcount (per-spa refcount_t protected by mutex)
79 *
80 * This reference count keep track of any active users of the spa_t. The
81 * spa_t cannot be destroyed or freed while this is non-zero. Internally,
82 * the refcount is never really 'zero' - opening a pool implicitly keeps
b128c09f 83 * some references in the DMU. Internally we check against spa_minref, but
34dc7c2f
BB
84 * present the image of a zero/non-zero value to consumers.
85 *
b128c09f 86 * spa_config_lock[] (per-spa array of rwlocks)
34dc7c2f
BB
87 *
88 * This protects the spa_t from config changes, and must be held in
89 * the following circumstances:
90 *
91 * - RW_READER to perform I/O to the spa
92 * - RW_WRITER to change the vdev config
93 *
34dc7c2f
BB
94 * The locking order is fairly straightforward:
95 *
96 * spa_namespace_lock -> spa_refcount
97 *
98 * The namespace lock must be acquired to increase the refcount from 0
99 * or to check if it is zero.
100 *
b128c09f 101 * spa_refcount -> spa_config_lock[]
34dc7c2f
BB
102 *
103 * There must be at least one valid reference on the spa_t to acquire
104 * the config lock.
105 *
b128c09f 106 * spa_namespace_lock -> spa_config_lock[]
34dc7c2f
BB
107 *
108 * The namespace lock must always be taken before the config lock.
109 *
110 *
b128c09f 111 * The spa_namespace_lock can be acquired directly and is globally visible.
34dc7c2f 112 *
b128c09f
BB
113 * The namespace is manipulated using the following functions, all of which
114 * require the spa_namespace_lock to be held.
34dc7c2f
BB
115 *
116 * spa_lookup() Lookup a spa_t by name.
117 *
118 * spa_add() Create a new spa_t in the namespace.
119 *
120 * spa_remove() Remove a spa_t from the namespace. This also
121 * frees up any memory associated with the spa_t.
122 *
123 * spa_next() Returns the next spa_t in the system, or the
124 * first if NULL is passed.
125 *
126 * spa_evict_all() Shutdown and remove all spa_t structures in
127 * the system.
128 *
129 * spa_guid_exists() Determine whether a pool/device guid exists.
130 *
131 * The spa_refcount is manipulated using the following functions:
132 *
133 * spa_open_ref() Adds a reference to the given spa_t. Must be
134 * called with spa_namespace_lock held if the
135 * refcount is currently zero.
136 *
137 * spa_close() Remove a reference from the spa_t. This will
138 * not free the spa_t or remove it from the
139 * namespace. No locking is required.
140 *
141 * spa_refcount_zero() Returns true if the refcount is currently
142 * zero. Must be called with spa_namespace_lock
143 * held.
144 *
b128c09f
BB
145 * The spa_config_lock[] is an array of rwlocks, ordered as follows:
146 * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV.
147 * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}().
148 *
149 * To read the configuration, it suffices to hold one of these locks as reader.
150 * To modify the configuration, you must hold all locks as writer. To modify
151 * vdev state without altering the vdev tree's topology (e.g. online/offline),
152 * you must hold SCL_STATE and SCL_ZIO as writer.
153 *
154 * We use these distinct config locks to avoid recursive lock entry.
155 * For example, spa_sync() (which holds SCL_CONFIG as reader) induces
156 * block allocations (SCL_ALLOC), which may require reading space maps
157 * from disk (dmu_read() -> zio_read() -> SCL_ZIO).
158 *
159 * The spa config locks cannot be normal rwlocks because we need the
160 * ability to hand off ownership. For example, SCL_ZIO is acquired
161 * by the issuing thread and later released by an interrupt thread.
162 * They do, however, obey the usual write-wanted semantics to prevent
163 * writer (i.e. system administrator) starvation.
164 *
165 * The lock acquisition rules are as follows:
166 *
167 * SCL_CONFIG
168 * Protects changes to the vdev tree topology, such as vdev
169 * add/remove/attach/detach. Protects the dirty config list
170 * (spa_config_dirty_list) and the set of spares and l2arc devices.
171 *
172 * SCL_STATE
173 * Protects changes to pool state and vdev state, such as vdev
174 * online/offline/fault/degrade/clear. Protects the dirty state list
175 * (spa_state_dirty_list) and global pool state (spa_state).
176 *
177 * SCL_ALLOC
178 * Protects changes to metaslab groups and classes.
179 * Held as reader by metaslab_alloc() and metaslab_claim().
180 *
181 * SCL_ZIO
182 * Held by bp-level zios (those which have no io_vd upon entry)
183 * to prevent changes to the vdev tree. The bp-level zio implicitly
184 * protects all of its vdev child zios, which do not hold SCL_ZIO.
185 *
186 * SCL_FREE
187 * Protects changes to metaslab groups and classes.
188 * Held as reader by metaslab_free(). SCL_FREE is distinct from
189 * SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free
190 * blocks in zio_done() while another i/o that holds either
191 * SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete.
192 *
193 * SCL_VDEV
194 * Held as reader to prevent changes to the vdev tree during trivial
428870ff 195 * inquiries such as bp_get_dsize(). SCL_VDEV is distinct from the
b128c09f
BB
196 * other locks, and lower than all of them, to ensure that it's safe
197 * to acquire regardless of caller context.
198 *
199 * In addition, the following rules apply:
200 *
201 * (a) spa_props_lock protects pool properties, spa_config and spa_config_list.
202 * The lock ordering is SCL_CONFIG > spa_props_lock.
203 *
204 * (b) I/O operations on leaf vdevs. For any zio operation that takes
205 * an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(),
206 * or zio_write_phys() -- the caller must ensure that the config cannot
207 * cannot change in the interim, and that the vdev cannot be reopened.
208 * SCL_STATE as reader suffices for both.
34dc7c2f
BB
209 *
210 * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
211 *
212 * spa_vdev_enter() Acquire the namespace lock and the config lock
213 * for writing.
214 *
215 * spa_vdev_exit() Release the config lock, wait for all I/O
216 * to complete, sync the updated configs to the
217 * cache, and release the namespace lock.
218 *
b128c09f
BB
219 * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit().
220 * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual
221 * locking is, always, based on spa_namespace_lock and spa_config_lock[].
222 *
9ae529ec 223 * spa_rename() is also implemented within this file since it requires
b128c09f 224 * manipulation of the namespace.
34dc7c2f
BB
225 */
226
227static avl_tree_t spa_namespace_avl;
228kmutex_t spa_namespace_lock;
229static kcondvar_t spa_namespace_cv;
230static int spa_active_count;
231int spa_max_replication_override = SPA_DVAS_PER_BP;
232
233static kmutex_t spa_spare_lock;
234static avl_tree_t spa_spare_avl;
235static kmutex_t spa_l2cache_lock;
236static avl_tree_t spa_l2cache_avl;
237
238kmem_cache_t *spa_buffer_pool;
fb5f0bc8 239int spa_mode_global;
34dc7c2f 240
ea04106b
AX
241#ifdef ZFS_DEBUG
242/* Everything except dprintf and spa is on by default in debug builds */
243int zfs_flags = ~(ZFS_DEBUG_DPRINTF | ZFS_DEBUG_SPA);
244#else
245int zfs_flags = 0;
246#endif
247
248/*
249 * zfs_recover can be set to nonzero to attempt to recover from
250 * otherwise-fatal errors, typically caused by on-disk corruption. When
251 * set, calls to zfs_panic_recover() will turn into warning messages.
252 * This should only be used as a last resort, as it typically results
253 * in leaked space, or worse.
254 */
255int zfs_recover = B_FALSE;
256
257/*
258 * If destroy encounters an EIO while reading metadata (e.g. indirect
259 * blocks), space referenced by the missing metadata can not be freed.
260 * Normally this causes the background destroy to become "stalled", as
261 * it is unable to make forward progress. While in this stalled state,
262 * all remaining space to free from the error-encountering filesystem is
263 * "temporarily leaked". Set this flag to cause it to ignore the EIO,
264 * permanently leak the space from indirect blocks that can not be read,
265 * and continue to free everything else that it can.
266 *
267 * The default, "stalling" behavior is useful if the storage partially
268 * fails (i.e. some but not all i/os fail), and then later recovers. In
269 * this case, we will be able to continue pool operations while it is
270 * partially failed, and when it recovers, we can continue to free the
271 * space, with no leaks. However, note that this case is actually
272 * fairly rare.
273 *
274 * Typically pools either (a) fail completely (but perhaps temporarily,
275 * e.g. a top-level vdev going offline), or (b) have localized,
276 * permanent errors (e.g. disk returns the wrong data due to bit flip or
277 * firmware bug). In case (a), this setting does not matter because the
278 * pool will be suspended and the sync thread will not be able to make
279 * forward progress regardless. In case (b), because the error is
280 * permanent, the best we can do is leak the minimum amount of space,
281 * which is what setting this flag will do. Therefore, it is reasonable
282 * for this flag to normally be set, but we chose the more conservative
283 * approach of not setting it, so that there is no possibility of
284 * leaking space in the "partial temporary" failure case.
285 */
286int zfs_free_leak_on_eio = B_FALSE;
287
c06d4368 288/*
a08ee875
LG
289 * Expiration time in milliseconds. This value has two meanings. First it is
290 * used to determine when the spa_deadman() logic should fire. By default the
291 * spa_deadman() will fire if spa_sync() has not completed in 1000 seconds.
292 * Secondly, the value determines if an I/O is considered "hung". Any I/O that
293 * has not completed in zfs_deadman_synctime_ms is considered "hung" resulting
294 * in a system panic.
c06d4368 295 */
a08ee875 296unsigned long zfs_deadman_synctime_ms = 1000000ULL;
c06d4368
AX
297
298/*
299 * By default the deadman is enabled.
300 */
301int zfs_deadman_enabled = 1;
302
a08ee875
LG
303/*
304 * The worst case is single-sector max-parity RAID-Z blocks, in which
305 * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1)
306 * times the size; so just assume that. Add to this the fact that
307 * we can have up to 3 DVAs per bp, and one more factor of 2 because
308 * the block may be dittoed with up to 3 DVAs by ddt_sync(). All together,
309 * the worst case is:
310 * (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2 == 24
311 */
312int spa_asize_inflation = 24;
c06d4368 313
34dc7c2f
BB
314/*
315 * ==========================================================================
316 * SPA config locking
317 * ==========================================================================
318 */
319static void
b128c09f
BB
320spa_config_lock_init(spa_t *spa)
321{
d6320ddb
BB
322 int i;
323
324 for (i = 0; i < SCL_LOCKS; i++) {
b128c09f
BB
325 spa_config_lock_t *scl = &spa->spa_config_lock[i];
326 mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL);
327 cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL);
a08ee875 328 refcount_create_untracked(&scl->scl_count);
b128c09f
BB
329 scl->scl_writer = NULL;
330 scl->scl_write_wanted = 0;
331 }
34dc7c2f
BB
332}
333
334static void
b128c09f
BB
335spa_config_lock_destroy(spa_t *spa)
336{
d6320ddb
BB
337 int i;
338
339 for (i = 0; i < SCL_LOCKS; i++) {
b128c09f
BB
340 spa_config_lock_t *scl = &spa->spa_config_lock[i];
341 mutex_destroy(&scl->scl_lock);
342 cv_destroy(&scl->scl_cv);
343 refcount_destroy(&scl->scl_count);
344 ASSERT(scl->scl_writer == NULL);
345 ASSERT(scl->scl_write_wanted == 0);
346 }
347}
348
349int
350spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw)
34dc7c2f 351{
d6320ddb
BB
352 int i;
353
354 for (i = 0; i < SCL_LOCKS; i++) {
b128c09f
BB
355 spa_config_lock_t *scl = &spa->spa_config_lock[i];
356 if (!(locks & (1 << i)))
357 continue;
358 mutex_enter(&scl->scl_lock);
359 if (rw == RW_READER) {
360 if (scl->scl_writer || scl->scl_write_wanted) {
361 mutex_exit(&scl->scl_lock);
362 spa_config_exit(spa, locks ^ (1 << i), tag);
363 return (0);
364 }
365 } else {
366 ASSERT(scl->scl_writer != curthread);
367 if (!refcount_is_zero(&scl->scl_count)) {
368 mutex_exit(&scl->scl_lock);
369 spa_config_exit(spa, locks ^ (1 << i), tag);
370 return (0);
371 }
372 scl->scl_writer = curthread;
373 }
374 (void) refcount_add(&scl->scl_count, tag);
375 mutex_exit(&scl->scl_lock);
376 }
377 return (1);
34dc7c2f
BB
378}
379
380void
b128c09f 381spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw)
34dc7c2f 382{
45d1cae3 383 int wlocks_held = 0;
d6320ddb 384 int i;
45d1cae3 385
a08ee875
LG
386 ASSERT3U(SCL_LOCKS, <, sizeof (wlocks_held) * NBBY);
387
d6320ddb 388 for (i = 0; i < SCL_LOCKS; i++) {
b128c09f 389 spa_config_lock_t *scl = &spa->spa_config_lock[i];
45d1cae3
BB
390 if (scl->scl_writer == curthread)
391 wlocks_held |= (1 << i);
b128c09f
BB
392 if (!(locks & (1 << i)))
393 continue;
394 mutex_enter(&scl->scl_lock);
395 if (rw == RW_READER) {
396 while (scl->scl_writer || scl->scl_write_wanted) {
397 cv_wait(&scl->scl_cv, &scl->scl_lock);
398 }
399 } else {
400 ASSERT(scl->scl_writer != curthread);
401 while (!refcount_is_zero(&scl->scl_count)) {
402 scl->scl_write_wanted++;
403 cv_wait(&scl->scl_cv, &scl->scl_lock);
404 scl->scl_write_wanted--;
405 }
406 scl->scl_writer = curthread;
407 }
408 (void) refcount_add(&scl->scl_count, tag);
409 mutex_exit(&scl->scl_lock);
34dc7c2f 410 }
45d1cae3 411 ASSERT(wlocks_held <= locks);
34dc7c2f
BB
412}
413
414void
b128c09f 415spa_config_exit(spa_t *spa, int locks, void *tag)
34dc7c2f 416{
d6320ddb
BB
417 int i;
418
419 for (i = SCL_LOCKS - 1; i >= 0; i--) {
b128c09f
BB
420 spa_config_lock_t *scl = &spa->spa_config_lock[i];
421 if (!(locks & (1 << i)))
422 continue;
423 mutex_enter(&scl->scl_lock);
424 ASSERT(!refcount_is_zero(&scl->scl_count));
425 if (refcount_remove(&scl->scl_count, tag) == 0) {
426 ASSERT(scl->scl_writer == NULL ||
427 scl->scl_writer == curthread);
428 scl->scl_writer = NULL; /* OK in either case */
429 cv_broadcast(&scl->scl_cv);
430 }
431 mutex_exit(&scl->scl_lock);
34dc7c2f 432 }
34dc7c2f
BB
433}
434
b128c09f
BB
435int
436spa_config_held(spa_t *spa, int locks, krw_t rw)
34dc7c2f 437{
d6320ddb 438 int i, locks_held = 0;
34dc7c2f 439
d6320ddb 440 for (i = 0; i < SCL_LOCKS; i++) {
b128c09f
BB
441 spa_config_lock_t *scl = &spa->spa_config_lock[i];
442 if (!(locks & (1 << i)))
443 continue;
444 if ((rw == RW_READER && !refcount_is_zero(&scl->scl_count)) ||
445 (rw == RW_WRITER && scl->scl_writer == curthread))
446 locks_held |= 1 << i;
447 }
448
449 return (locks_held);
34dc7c2f
BB
450}
451
452/*
453 * ==========================================================================
454 * SPA namespace functions
455 * ==========================================================================
456 */
457
458/*
459 * Lookup the named spa_t in the AVL tree. The spa_namespace_lock must be held.
460 * Returns NULL if no matching spa_t is found.
461 */
462spa_t *
463spa_lookup(const char *name)
464{
b128c09f
BB
465 static spa_t search; /* spa_t is large; don't allocate on stack */
466 spa_t *spa;
34dc7c2f 467 avl_index_t where;
34dc7c2f
BB
468 char *cp;
469
470 ASSERT(MUTEX_HELD(&spa_namespace_lock));
471
a08ee875
LG
472 (void) strlcpy(search.spa_name, name, sizeof (search.spa_name));
473
34dc7c2f
BB
474 /*
475 * If it's a full dataset name, figure out the pool name and
476 * just use that.
477 */
ea04106b 478 cp = strpbrk(search.spa_name, "/@#");
a08ee875 479 if (cp != NULL)
34dc7c2f 480 *cp = '\0';
34dc7c2f 481
34dc7c2f
BB
482 spa = avl_find(&spa_namespace_avl, &search, &where);
483
34dc7c2f
BB
484 return (spa);
485}
486
c06d4368
AX
487/*
488 * Fires when spa_sync has not completed within zfs_deadman_synctime_ms.
489 * If the zfs_deadman_enabled flag is set then it inspects all vdev queues
490 * looking for potentially hung I/Os.
491 */
492void
493spa_deadman(void *arg)
494{
495 spa_t *spa = arg;
496
497 zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu",
498 (gethrtime() - spa->spa_sync_starttime) / NANOSEC,
499 ++spa->spa_deadman_calls);
500 if (zfs_deadman_enabled)
501 vdev_deadman(spa->spa_root_vdev);
502
503 spa->spa_deadman_tqid = taskq_dispatch_delay(system_taskq,
ea04106b 504 spa_deadman, spa, KM_SLEEP, ddi_get_lbolt() +
c06d4368
AX
505 NSEC_TO_TICK(spa->spa_deadman_synctime));
506}
507
34dc7c2f
BB
508/*
509 * Create an uninitialized spa_t with the given name. Requires
510 * spa_namespace_lock. The caller must ensure that the spa_t doesn't already
511 * exist by calling spa_lookup() first.
512 */
513spa_t *
428870ff 514spa_add(const char *name, nvlist_t *config, const char *altroot)
34dc7c2f
BB
515{
516 spa_t *spa;
b128c09f 517 spa_config_dirent_t *dp;
d6320ddb 518 int t;
ea04106b 519 int i;
34dc7c2f
BB
520
521 ASSERT(MUTEX_HELD(&spa_namespace_lock));
522
ea04106b 523 spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
34dc7c2f 524
34dc7c2f 525 mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL);
34dc7c2f 526 mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL);
428870ff 527 mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL);
34dc7c2f 528 mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL);
428870ff 529 mutex_init(&spa->spa_proc_lock, NULL, MUTEX_DEFAULT, NULL);
34dc7c2f 530 mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL);
428870ff
BB
531 mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL);
532 mutex_init(&spa->spa_suspend_lock, NULL, MUTEX_DEFAULT, NULL);
533 mutex_init(&spa->spa_vdev_top_lock, NULL, MUTEX_DEFAULT, NULL);
ea04106b 534 mutex_init(&spa->spa_feat_stats_lock, NULL, MUTEX_DEFAULT, NULL);
34dc7c2f
BB
535
536 cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL);
428870ff 537 cv_init(&spa->spa_proc_cv, NULL, CV_DEFAULT, NULL);
34dc7c2f 538 cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL);
b128c09f 539 cv_init(&spa->spa_suspend_cv, NULL, CV_DEFAULT, NULL);
34dc7c2f 540
d6320ddb 541 for (t = 0; t < TXG_SIZE; t++)
428870ff
BB
542 bplist_create(&spa->spa_free_bplist[t]);
543
b128c09f 544 (void) strlcpy(spa->spa_name, name, sizeof (spa->spa_name));
34dc7c2f
BB
545 spa->spa_state = POOL_STATE_UNINITIALIZED;
546 spa->spa_freeze_txg = UINT64_MAX;
547 spa->spa_final_txg = UINT64_MAX;
428870ff
BB
548 spa->spa_load_max_txg = UINT64_MAX;
549 spa->spa_proc = &p0;
550 spa->spa_proc_state = SPA_PROC_NONE;
34dc7c2f 551
a08ee875 552 spa->spa_deadman_synctime = MSEC2NSEC(zfs_deadman_synctime_ms);
c06d4368 553
34dc7c2f 554 refcount_create(&spa->spa_refcount);
b128c09f 555 spa_config_lock_init(spa);
a08ee875 556 spa_stats_init(spa);
34dc7c2f
BB
557
558 avl_add(&spa_namespace_avl, spa);
559
34dc7c2f
BB
560 /*
561 * Set the alternate root, if there is one.
562 */
563 if (altroot) {
564 spa->spa_root = spa_strdup(altroot);
565 spa_active_count++;
566 }
567
b128c09f
BB
568 /*
569 * Every pool starts with the default cachefile
570 */
571 list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t),
572 offsetof(spa_config_dirent_t, scd_link));
573
ea04106b 574 dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP);
428870ff 575 dp->scd_path = altroot ? NULL : spa_strdup(spa_config_path);
b128c09f
BB
576 list_insert_head(&spa->spa_config_list, dp);
577
572e2857 578 VERIFY(nvlist_alloc(&spa->spa_load_info, NV_UNIQUE_NAME,
ea04106b 579 KM_SLEEP) == 0);
572e2857 580
9ae529ec
CS
581 if (config != NULL) {
582 nvlist_t *features;
583
584 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_FEATURES_FOR_READ,
585 &features) == 0) {
586 VERIFY(nvlist_dup(features, &spa->spa_label_features,
587 0) == 0);
588 }
589
428870ff 590 VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0);
9ae529ec
CS
591 }
592
593 if (spa->spa_label_features == NULL) {
594 VERIFY(nvlist_alloc(&spa->spa_label_features, NV_UNIQUE_NAME,
595 KM_SLEEP) == 0);
596 }
428870ff 597
a08ee875
LG
598 spa->spa_debug = ((zfs_flags & ZFS_DEBUG_SPA) != 0);
599
ea04106b
AX
600 /*
601 * As a pool is being created, treat all features as disabled by
602 * setting SPA_FEATURE_DISABLED for all entries in the feature
603 * refcount cache.
604 */
605 for (i = 0; i < SPA_FEATURES; i++) {
606 spa->spa_feat_refcount_cache[i] = SPA_FEATURE_DISABLED;
607 }
608
34dc7c2f
BB
609 return (spa);
610}
611
612/*
613 * Removes a spa_t from the namespace, freeing up any memory used. Requires
614 * spa_namespace_lock. This is called only after the spa_t has been closed and
615 * deactivated.
616 */
617void
618spa_remove(spa_t *spa)
619{
b128c09f 620 spa_config_dirent_t *dp;
d6320ddb 621 int t;
b128c09f 622
34dc7c2f
BB
623 ASSERT(MUTEX_HELD(&spa_namespace_lock));
624 ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
34dc7c2f 625
428870ff
BB
626 nvlist_free(spa->spa_config_splitting);
627
34dc7c2f
BB
628 avl_remove(&spa_namespace_avl, spa);
629 cv_broadcast(&spa_namespace_cv);
630
631 if (spa->spa_root) {
632 spa_strfree(spa->spa_root);
633 spa_active_count--;
634 }
635
b128c09f
BB
636 while ((dp = list_head(&spa->spa_config_list)) != NULL) {
637 list_remove(&spa->spa_config_list, dp);
638 if (dp->scd_path != NULL)
639 spa_strfree(dp->scd_path);
640 kmem_free(dp, sizeof (spa_config_dirent_t));
641 }
34dc7c2f 642
b128c09f 643 list_destroy(&spa->spa_config_list);
34dc7c2f 644
9ae529ec 645 nvlist_free(spa->spa_label_features);
572e2857 646 nvlist_free(spa->spa_load_info);
ea04106b 647 nvlist_free(spa->spa_feat_stats);
34dc7c2f
BB
648 spa_config_set(spa, NULL);
649
650 refcount_destroy(&spa->spa_refcount);
651
a08ee875 652 spa_stats_destroy(spa);
b128c09f 653 spa_config_lock_destroy(spa);
34dc7c2f 654
d6320ddb 655 for (t = 0; t < TXG_SIZE; t++)
428870ff
BB
656 bplist_destroy(&spa->spa_free_bplist[t]);
657
34dc7c2f 658 cv_destroy(&spa->spa_async_cv);
428870ff 659 cv_destroy(&spa->spa_proc_cv);
34dc7c2f 660 cv_destroy(&spa->spa_scrub_io_cv);
b128c09f 661 cv_destroy(&spa->spa_suspend_cv);
34dc7c2f 662
34dc7c2f 663 mutex_destroy(&spa->spa_async_lock);
34dc7c2f 664 mutex_destroy(&spa->spa_errlist_lock);
428870ff 665 mutex_destroy(&spa->spa_errlog_lock);
34dc7c2f 666 mutex_destroy(&spa->spa_history_lock);
428870ff 667 mutex_destroy(&spa->spa_proc_lock);
34dc7c2f 668 mutex_destroy(&spa->spa_props_lock);
428870ff 669 mutex_destroy(&spa->spa_scrub_lock);
b128c09f 670 mutex_destroy(&spa->spa_suspend_lock);
428870ff 671 mutex_destroy(&spa->spa_vdev_top_lock);
ea04106b 672 mutex_destroy(&spa->spa_feat_stats_lock);
34dc7c2f
BB
673
674 kmem_free(spa, sizeof (spa_t));
675}
676
677/*
678 * Given a pool, return the next pool in the namespace, or NULL if there is
679 * none. If 'prev' is NULL, return the first pool.
680 */
681spa_t *
682spa_next(spa_t *prev)
683{
684 ASSERT(MUTEX_HELD(&spa_namespace_lock));
685
686 if (prev)
687 return (AVL_NEXT(&spa_namespace_avl, prev));
688 else
689 return (avl_first(&spa_namespace_avl));
690}
691
692/*
693 * ==========================================================================
694 * SPA refcount functions
695 * ==========================================================================
696 */
697
698/*
699 * Add a reference to the given spa_t. Must have at least one reference, or
700 * have the namespace lock held.
701 */
702void
703spa_open_ref(spa_t *spa, void *tag)
704{
b128c09f 705 ASSERT(refcount_count(&spa->spa_refcount) >= spa->spa_minref ||
34dc7c2f 706 MUTEX_HELD(&spa_namespace_lock));
34dc7c2f
BB
707 (void) refcount_add(&spa->spa_refcount, tag);
708}
709
710/*
711 * Remove a reference to the given spa_t. Must have at least one reference, or
712 * have the namespace lock held.
713 */
714void
715spa_close(spa_t *spa, void *tag)
716{
b128c09f 717 ASSERT(refcount_count(&spa->spa_refcount) > spa->spa_minref ||
34dc7c2f 718 MUTEX_HELD(&spa_namespace_lock));
34dc7c2f
BB
719 (void) refcount_remove(&spa->spa_refcount, tag);
720}
721
722/*
723 * Check to see if the spa refcount is zero. Must be called with
b128c09f 724 * spa_namespace_lock held. We really compare against spa_minref, which is the
34dc7c2f
BB
725 * number of references acquired when opening a pool
726 */
727boolean_t
728spa_refcount_zero(spa_t *spa)
729{
730 ASSERT(MUTEX_HELD(&spa_namespace_lock));
731
b128c09f 732 return (refcount_count(&spa->spa_refcount) == spa->spa_minref);
34dc7c2f
BB
733}
734
735/*
736 * ==========================================================================
737 * SPA spare and l2cache tracking
738 * ==========================================================================
739 */
740
741/*
742 * Hot spares and cache devices are tracked using the same code below,
743 * for 'auxiliary' devices.
744 */
745
746typedef struct spa_aux {
747 uint64_t aux_guid;
748 uint64_t aux_pool;
749 avl_node_t aux_avl;
750 int aux_count;
751} spa_aux_t;
752
753static int
754spa_aux_compare(const void *a, const void *b)
755{
756 const spa_aux_t *sa = a;
757 const spa_aux_t *sb = b;
758
759 if (sa->aux_guid < sb->aux_guid)
760 return (-1);
761 else if (sa->aux_guid > sb->aux_guid)
762 return (1);
763 else
764 return (0);
765}
766
767void
768spa_aux_add(vdev_t *vd, avl_tree_t *avl)
769{
770 avl_index_t where;
771 spa_aux_t search;
772 spa_aux_t *aux;
773
774 search.aux_guid = vd->vdev_guid;
775 if ((aux = avl_find(avl, &search, &where)) != NULL) {
776 aux->aux_count++;
777 } else {
ea04106b 778 aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP);
34dc7c2f
BB
779 aux->aux_guid = vd->vdev_guid;
780 aux->aux_count = 1;
781 avl_insert(avl, aux, where);
782 }
783}
784
785void
786spa_aux_remove(vdev_t *vd, avl_tree_t *avl)
787{
788 spa_aux_t search;
789 spa_aux_t *aux;
790 avl_index_t where;
791
792 search.aux_guid = vd->vdev_guid;
793 aux = avl_find(avl, &search, &where);
794
795 ASSERT(aux != NULL);
796
797 if (--aux->aux_count == 0) {
798 avl_remove(avl, aux);
799 kmem_free(aux, sizeof (spa_aux_t));
800 } else if (aux->aux_pool == spa_guid(vd->vdev_spa)) {
801 aux->aux_pool = 0ULL;
802 }
803}
804
805boolean_t
b128c09f 806spa_aux_exists(uint64_t guid, uint64_t *pool, int *refcnt, avl_tree_t *avl)
34dc7c2f
BB
807{
808 spa_aux_t search, *found;
34dc7c2f
BB
809
810 search.aux_guid = guid;
b128c09f 811 found = avl_find(avl, &search, NULL);
34dc7c2f
BB
812
813 if (pool) {
814 if (found)
815 *pool = found->aux_pool;
816 else
817 *pool = 0ULL;
818 }
819
b128c09f
BB
820 if (refcnt) {
821 if (found)
822 *refcnt = found->aux_count;
823 else
824 *refcnt = 0;
825 }
826
34dc7c2f
BB
827 return (found != NULL);
828}
829
830void
831spa_aux_activate(vdev_t *vd, avl_tree_t *avl)
832{
833 spa_aux_t search, *found;
834 avl_index_t where;
835
836 search.aux_guid = vd->vdev_guid;
837 found = avl_find(avl, &search, &where);
838 ASSERT(found != NULL);
839 ASSERT(found->aux_pool == 0ULL);
840
841 found->aux_pool = spa_guid(vd->vdev_spa);
842}
843
844/*
845 * Spares are tracked globally due to the following constraints:
846 *
847 * - A spare may be part of multiple pools.
848 * - A spare may be added to a pool even if it's actively in use within
849 * another pool.
850 * - A spare in use in any pool can only be the source of a replacement if
851 * the target is a spare in the same pool.
852 *
853 * We keep track of all spares on the system through the use of a reference
854 * counted AVL tree. When a vdev is added as a spare, or used as a replacement
855 * spare, then we bump the reference count in the AVL tree. In addition, we set
856 * the 'vdev_isspare' member to indicate that the device is a spare (active or
857 * inactive). When a spare is made active (used to replace a device in the
858 * pool), we also keep track of which pool its been made a part of.
859 *
860 * The 'spa_spare_lock' protects the AVL tree. These functions are normally
861 * called under the spa_namespace lock as part of vdev reconfiguration. The
862 * separate spare lock exists for the status query path, which does not need to
863 * be completely consistent with respect to other vdev configuration changes.
864 */
865
866static int
867spa_spare_compare(const void *a, const void *b)
868{
869 return (spa_aux_compare(a, b));
870}
871
872void
873spa_spare_add(vdev_t *vd)
874{
875 mutex_enter(&spa_spare_lock);
876 ASSERT(!vd->vdev_isspare);
877 spa_aux_add(vd, &spa_spare_avl);
878 vd->vdev_isspare = B_TRUE;
879 mutex_exit(&spa_spare_lock);
880}
881
882void
883spa_spare_remove(vdev_t *vd)
884{
885 mutex_enter(&spa_spare_lock);
886 ASSERT(vd->vdev_isspare);
887 spa_aux_remove(vd, &spa_spare_avl);
888 vd->vdev_isspare = B_FALSE;
889 mutex_exit(&spa_spare_lock);
890}
891
892boolean_t
b128c09f 893spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt)
34dc7c2f
BB
894{
895 boolean_t found;
896
897 mutex_enter(&spa_spare_lock);
b128c09f 898 found = spa_aux_exists(guid, pool, refcnt, &spa_spare_avl);
34dc7c2f
BB
899 mutex_exit(&spa_spare_lock);
900
901 return (found);
902}
903
904void
905spa_spare_activate(vdev_t *vd)
906{
907 mutex_enter(&spa_spare_lock);
908 ASSERT(vd->vdev_isspare);
909 spa_aux_activate(vd, &spa_spare_avl);
910 mutex_exit(&spa_spare_lock);
911}
912
913/*
914 * Level 2 ARC devices are tracked globally for the same reasons as spares.
915 * Cache devices currently only support one pool per cache device, and so
916 * for these devices the aux reference count is currently unused beyond 1.
917 */
918
919static int
920spa_l2cache_compare(const void *a, const void *b)
921{
922 return (spa_aux_compare(a, b));
923}
924
925void
926spa_l2cache_add(vdev_t *vd)
927{
928 mutex_enter(&spa_l2cache_lock);
929 ASSERT(!vd->vdev_isl2cache);
930 spa_aux_add(vd, &spa_l2cache_avl);
931 vd->vdev_isl2cache = B_TRUE;
932 mutex_exit(&spa_l2cache_lock);
933}
934
935void
936spa_l2cache_remove(vdev_t *vd)
937{
938 mutex_enter(&spa_l2cache_lock);
939 ASSERT(vd->vdev_isl2cache);
940 spa_aux_remove(vd, &spa_l2cache_avl);
941 vd->vdev_isl2cache = B_FALSE;
942 mutex_exit(&spa_l2cache_lock);
943}
944
945boolean_t
946spa_l2cache_exists(uint64_t guid, uint64_t *pool)
947{
948 boolean_t found;
949
950 mutex_enter(&spa_l2cache_lock);
b128c09f 951 found = spa_aux_exists(guid, pool, NULL, &spa_l2cache_avl);
34dc7c2f
BB
952 mutex_exit(&spa_l2cache_lock);
953
954 return (found);
955}
956
957void
958spa_l2cache_activate(vdev_t *vd)
959{
960 mutex_enter(&spa_l2cache_lock);
961 ASSERT(vd->vdev_isl2cache);
962 spa_aux_activate(vd, &spa_l2cache_avl);
963 mutex_exit(&spa_l2cache_lock);
964}
965
34dc7c2f
BB
966/*
967 * ==========================================================================
968 * SPA vdev locking
969 * ==========================================================================
970 */
971
972/*
973 * Lock the given spa_t for the purpose of adding or removing a vdev.
974 * Grabs the global spa_namespace_lock plus the spa config lock for writing.
975 * It returns the next transaction group for the spa_t.
976 */
977uint64_t
978spa_vdev_enter(spa_t *spa)
979{
428870ff 980 mutex_enter(&spa->spa_vdev_top_lock);
34dc7c2f 981 mutex_enter(&spa_namespace_lock);
428870ff
BB
982 return (spa_vdev_config_enter(spa));
983}
984
985/*
986 * Internal implementation for spa_vdev_enter(). Used when a vdev
987 * operation requires multiple syncs (i.e. removing a device) while
988 * keeping the spa_namespace_lock held.
989 */
990uint64_t
991spa_vdev_config_enter(spa_t *spa)
992{
993 ASSERT(MUTEX_HELD(&spa_namespace_lock));
34dc7c2f 994
b128c09f 995 spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
34dc7c2f
BB
996
997 return (spa_last_synced_txg(spa) + 1);
998}
999
1000/*
428870ff
BB
1001 * Used in combination with spa_vdev_config_enter() to allow the syncing
1002 * of multiple transactions without releasing the spa_namespace_lock.
34dc7c2f 1003 */
428870ff
BB
1004void
1005spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error, char *tag)
34dc7c2f
BB
1006{
1007 int config_changed = B_FALSE;
1008
d6320ddb 1009 ASSERT(MUTEX_HELD(&spa_namespace_lock));
34dc7c2f
BB
1010 ASSERT(txg > spa_last_synced_txg(spa));
1011
b128c09f
BB
1012 spa->spa_pending_vdev = NULL;
1013
34dc7c2f
BB
1014 /*
1015 * Reassess the DTLs.
1016 */
1017 vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE);
1018
b128c09f 1019 if (error == 0 && !list_is_empty(&spa->spa_config_dirty_list)) {
34dc7c2f 1020 config_changed = B_TRUE;
428870ff 1021 spa->spa_config_generation++;
34dc7c2f
BB
1022 }
1023
428870ff
BB
1024 /*
1025 * Verify the metaslab classes.
1026 */
1027 ASSERT(metaslab_class_validate(spa_normal_class(spa)) == 0);
1028 ASSERT(metaslab_class_validate(spa_log_class(spa)) == 0);
1029
b128c09f 1030 spa_config_exit(spa, SCL_ALL, spa);
34dc7c2f 1031
428870ff
BB
1032 /*
1033 * Panic the system if the specified tag requires it. This
1034 * is useful for ensuring that configurations are updated
1035 * transactionally.
1036 */
1037 if (zio_injection_enabled)
1038 zio_handle_panic_injection(spa, tag, 0);
1039
34dc7c2f
BB
1040 /*
1041 * Note: this txg_wait_synced() is important because it ensures
1042 * that there won't be more than one config change per txg.
1043 * This allows us to use the txg as the generation number.
1044 */
1045 if (error == 0)
1046 txg_wait_synced(spa->spa_dsl_pool, txg);
1047
1048 if (vd != NULL) {
ea04106b 1049 ASSERT(!vd->vdev_detached || vd->vdev_dtl_sm == NULL);
fb5f0bc8 1050 spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
34dc7c2f 1051 vdev_free(vd);
fb5f0bc8 1052 spa_config_exit(spa, SCL_ALL, spa);
34dc7c2f
BB
1053 }
1054
1055 /*
1056 * If the config changed, update the config cache.
1057 */
1058 if (config_changed)
b128c09f 1059 spa_config_sync(spa, B_FALSE, B_TRUE);
428870ff 1060}
34dc7c2f 1061
428870ff
BB
1062/*
1063 * Unlock the spa_t after adding or removing a vdev. Besides undoing the
1064 * locking of spa_vdev_enter(), we also want make sure the transactions have
1065 * synced to disk, and then update the global configuration cache with the new
1066 * information.
1067 */
1068int
1069spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
1070{
1071 spa_vdev_config_exit(spa, vd, txg, error, FTAG);
34dc7c2f 1072 mutex_exit(&spa_namespace_lock);
428870ff 1073 mutex_exit(&spa->spa_vdev_top_lock);
34dc7c2f
BB
1074
1075 return (error);
1076}
1077
b128c09f
BB
1078/*
1079 * Lock the given spa_t for the purpose of changing vdev state.
1080 */
1081void
428870ff 1082spa_vdev_state_enter(spa_t *spa, int oplocks)
b128c09f 1083{
428870ff
BB
1084 int locks = SCL_STATE_ALL | oplocks;
1085
1086 /*
1087 * Root pools may need to read of the underlying devfs filesystem
1088 * when opening up a vdev. Unfortunately if we're holding the
1089 * SCL_ZIO lock it will result in a deadlock when we try to issue
1090 * the read from the root filesystem. Instead we "prefetch"
1091 * the associated vnodes that we need prior to opening the
1092 * underlying devices and cache them so that we can prevent
1093 * any I/O when we are doing the actual open.
1094 */
1095 if (spa_is_root(spa)) {
1096 int low = locks & ~(SCL_ZIO - 1);
1097 int high = locks & ~low;
1098
1099 spa_config_enter(spa, high, spa, RW_WRITER);
1100 vdev_hold(spa->spa_root_vdev);
1101 spa_config_enter(spa, low, spa, RW_WRITER);
1102 } else {
1103 spa_config_enter(spa, locks, spa, RW_WRITER);
1104 }
1105 spa->spa_vdev_locks = locks;
b128c09f
BB
1106}
1107
1108int
1109spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error)
1110{
428870ff
BB
1111 boolean_t config_changed = B_FALSE;
1112
1113 if (vd != NULL || error == 0)
1114 vdev_dtl_reassess(vd ? vd->vdev_top : spa->spa_root_vdev,
1115 0, 0, B_FALSE);
1116
1117 if (vd != NULL) {
b128c09f 1118 vdev_state_dirty(vd->vdev_top);
428870ff
BB
1119 config_changed = B_TRUE;
1120 spa->spa_config_generation++;
1121 }
b128c09f 1122
428870ff
BB
1123 if (spa_is_root(spa))
1124 vdev_rele(spa->spa_root_vdev);
1125
1126 ASSERT3U(spa->spa_vdev_locks, >=, SCL_STATE_ALL);
1127 spa_config_exit(spa, spa->spa_vdev_locks, spa);
b128c09f 1128
fb5f0bc8
BB
1129 /*
1130 * If anything changed, wait for it to sync. This ensures that,
1131 * from the system administrator's perspective, zpool(1M) commands
1132 * are synchronous. This is important for things like zpool offline:
1133 * when the command completes, you expect no further I/O from ZFS.
1134 */
1135 if (vd != NULL)
1136 txg_wait_synced(spa->spa_dsl_pool, 0);
1137
428870ff
BB
1138 /*
1139 * If the config changed, update the config cache.
1140 */
1141 if (config_changed) {
1142 mutex_enter(&spa_namespace_lock);
1143 spa_config_sync(spa, B_FALSE, B_TRUE);
1144 mutex_exit(&spa_namespace_lock);
1145 }
1146
b128c09f
BB
1147 return (error);
1148}
1149
34dc7c2f
BB
1150/*
1151 * ==========================================================================
1152 * Miscellaneous functions
1153 * ==========================================================================
1154 */
1155
9ae529ec 1156void
ea04106b
AX
1157spa_activate_mos_feature(spa_t *spa, const char *feature, dmu_tx_t *tx)
1158{
1159 if (!nvlist_exists(spa->spa_label_features, feature)) {
1160 fnvlist_add_boolean(spa->spa_label_features, feature);
1161 /*
1162 * When we are creating the pool (tx_txg==TXG_INITIAL), we can't
1163 * dirty the vdev config because lock SCL_CONFIG is not held.
1164 * Thankfully, in this case we don't need to dirty the config
1165 * because it will be written out anyway when we finish
1166 * creating the pool.
1167 */
1168 if (tx->tx_txg != TXG_INITIAL)
1169 vdev_config_dirty(spa->spa_root_vdev);
1170 }
9ae529ec
CS
1171}
1172
1173void
1174spa_deactivate_mos_feature(spa_t *spa, const char *feature)
1175{
ea04106b
AX
1176 if (nvlist_remove_all(spa->spa_label_features, feature) == 0)
1177 vdev_config_dirty(spa->spa_root_vdev);
9ae529ec
CS
1178}
1179
34dc7c2f
BB
1180/*
1181 * Rename a spa_t.
1182 */
1183int
1184spa_rename(const char *name, const char *newname)
1185{
1186 spa_t *spa;
1187 int err;
1188
1189 /*
1190 * Lookup the spa_t and grab the config lock for writing. We need to
1191 * actually open the pool so that we can sync out the necessary labels.
1192 * It's OK to call spa_open() with the namespace lock held because we
1193 * allow recursive calls for other reasons.
1194 */
1195 mutex_enter(&spa_namespace_lock);
1196 if ((err = spa_open(name, &spa, FTAG)) != 0) {
1197 mutex_exit(&spa_namespace_lock);
1198 return (err);
1199 }
1200
b128c09f 1201 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34dc7c2f
BB
1202
1203 avl_remove(&spa_namespace_avl, spa);
b128c09f 1204 (void) strlcpy(spa->spa_name, newname, sizeof (spa->spa_name));
34dc7c2f
BB
1205 avl_add(&spa_namespace_avl, spa);
1206
1207 /*
1208 * Sync all labels to disk with the new names by marking the root vdev
1209 * dirty and waiting for it to sync. It will pick up the new pool name
1210 * during the sync.
1211 */
1212 vdev_config_dirty(spa->spa_root_vdev);
1213
b128c09f 1214 spa_config_exit(spa, SCL_ALL, FTAG);
34dc7c2f
BB
1215
1216 txg_wait_synced(spa->spa_dsl_pool, 0);
1217
1218 /*
1219 * Sync the updated config cache.
1220 */
b128c09f 1221 spa_config_sync(spa, B_FALSE, B_TRUE);
34dc7c2f
BB
1222
1223 spa_close(spa, FTAG);
1224
1225 mutex_exit(&spa_namespace_lock);
1226
1227 return (0);
1228}
1229
34dc7c2f 1230/*
572e2857
BB
1231 * Return the spa_t associated with given pool_guid, if it exists. If
1232 * device_guid is non-zero, determine whether the pool exists *and* contains
1233 * a device with the specified device_guid.
34dc7c2f 1234 */
572e2857
BB
1235spa_t *
1236spa_by_guid(uint64_t pool_guid, uint64_t device_guid)
34dc7c2f
BB
1237{
1238 spa_t *spa;
1239 avl_tree_t *t = &spa_namespace_avl;
1240
1241 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1242
1243 for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) {
1244 if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1245 continue;
1246 if (spa->spa_root_vdev == NULL)
1247 continue;
1248 if (spa_guid(spa) == pool_guid) {
1249 if (device_guid == 0)
1250 break;
1251
1252 if (vdev_lookup_by_guid(spa->spa_root_vdev,
1253 device_guid) != NULL)
1254 break;
1255
1256 /*
1257 * Check any devices we may be in the process of adding.
1258 */
1259 if (spa->spa_pending_vdev) {
1260 if (vdev_lookup_by_guid(spa->spa_pending_vdev,
1261 device_guid) != NULL)
1262 break;
1263 }
1264 }
1265 }
1266
572e2857
BB
1267 return (spa);
1268}
1269
1270/*
1271 * Determine whether a pool with the given pool_guid exists.
1272 */
1273boolean_t
1274spa_guid_exists(uint64_t pool_guid, uint64_t device_guid)
1275{
1276 return (spa_by_guid(pool_guid, device_guid) != NULL);
34dc7c2f
BB
1277}
1278
1279char *
1280spa_strdup(const char *s)
1281{
1282 size_t len;
1283 char *new;
1284
1285 len = strlen(s);
ea04106b 1286 new = kmem_alloc(len + 1, KM_SLEEP);
34dc7c2f
BB
1287 bcopy(s, new, len);
1288 new[len] = '\0';
1289
1290 return (new);
1291}
1292
1293void
1294spa_strfree(char *s)
1295{
1296 kmem_free(s, strlen(s) + 1);
1297}
1298
1299uint64_t
1300spa_get_random(uint64_t range)
1301{
1302 uint64_t r;
1303
1304 ASSERT(range != 0);
1305
1306 (void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t));
1307
1308 return (r % range);
1309}
1310
428870ff
BB
1311uint64_t
1312spa_generate_guid(spa_t *spa)
34dc7c2f 1313{
428870ff 1314 uint64_t guid = spa_get_random(-1ULL);
34dc7c2f 1315
428870ff
BB
1316 if (spa != NULL) {
1317 while (guid == 0 || spa_guid_exists(spa_guid(spa), guid))
1318 guid = spa_get_random(-1ULL);
1319 } else {
1320 while (guid == 0 || spa_guid_exists(guid, 0))
1321 guid = spa_get_random(-1ULL);
34dc7c2f
BB
1322 }
1323
428870ff
BB
1324 return (guid);
1325}
1326
1327void
ea04106b 1328snprintf_blkptr(char *buf, size_t buflen, const blkptr_t *bp)
428870ff 1329{
9ae529ec 1330 char type[256];
428870ff
BB
1331 char *checksum = NULL;
1332 char *compress = NULL;
34dc7c2f 1333
428870ff 1334 if (bp != NULL) {
9ae529ec
CS
1335 if (BP_GET_TYPE(bp) & DMU_OT_NEWTYPE) {
1336 dmu_object_byteswap_t bswap =
1337 DMU_OT_BYTESWAP(BP_GET_TYPE(bp));
1338 (void) snprintf(type, sizeof (type), "bswap %s %s",
1339 DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) ?
1340 "metadata" : "data",
1341 dmu_ot_byteswap[bswap].ob_name);
1342 } else {
1343 (void) strlcpy(type, dmu_ot[BP_GET_TYPE(bp)].ot_name,
1344 sizeof (type));
1345 }
ea04106b
AX
1346 if (!BP_IS_EMBEDDED(bp)) {
1347 checksum =
1348 zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name;
1349 }
428870ff 1350 compress = zio_compress_table[BP_GET_COMPRESS(bp)].ci_name;
34dc7c2f
BB
1351 }
1352
ea04106b
AX
1353 SNPRINTF_BLKPTR(snprintf, ' ', buf, buflen, bp, type, checksum,
1354 compress);
34dc7c2f
BB
1355}
1356
1357void
1358spa_freeze(spa_t *spa)
1359{
1360 uint64_t freeze_txg = 0;
1361
b128c09f 1362 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34dc7c2f
BB
1363 if (spa->spa_freeze_txg == UINT64_MAX) {
1364 freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE;
1365 spa->spa_freeze_txg = freeze_txg;
1366 }
b128c09f 1367 spa_config_exit(spa, SCL_ALL, FTAG);
34dc7c2f
BB
1368 if (freeze_txg != 0)
1369 txg_wait_synced(spa_get_dsl(spa), freeze_txg);
1370}
1371
ea04106b
AX
1372void
1373zfs_panic_recover(const char *fmt, ...)
1374{
1375 va_list adx;
1376
1377 va_start(adx, fmt);
1378 vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx);
1379 va_end(adx);
1380}
1381
428870ff
BB
1382/*
1383 * This is a stripped-down version of strtoull, suitable only for converting
a08ee875 1384 * lowercase hexadecimal numbers that don't overflow.
428870ff
BB
1385 */
1386uint64_t
1387strtonum(const char *str, char **nptr)
1388{
1389 uint64_t val = 0;
1390 char c;
1391 int digit;
1392
1393 while ((c = *str) != '\0') {
1394 if (c >= '0' && c <= '9')
1395 digit = c - '0';
1396 else if (c >= 'a' && c <= 'f')
1397 digit = 10 + c - 'a';
1398 else
1399 break;
1400
1401 val *= 16;
1402 val += digit;
1403
1404 str++;
1405 }
1406
1407 if (nptr)
1408 *nptr = (char *)str;
1409
1410 return (val);
1411}
1412
34dc7c2f
BB
1413/*
1414 * ==========================================================================
1415 * Accessor functions
1416 * ==========================================================================
1417 */
1418
b128c09f
BB
1419boolean_t
1420spa_shutting_down(spa_t *spa)
34dc7c2f 1421{
b128c09f 1422 return (spa->spa_async_suspended);
34dc7c2f
BB
1423}
1424
1425dsl_pool_t *
1426spa_get_dsl(spa_t *spa)
1427{
1428 return (spa->spa_dsl_pool);
1429}
1430
9ae529ec
CS
1431boolean_t
1432spa_is_initializing(spa_t *spa)
1433{
1434 return (spa->spa_is_initializing);
1435}
1436
34dc7c2f
BB
1437blkptr_t *
1438spa_get_rootblkptr(spa_t *spa)
1439{
1440 return (&spa->spa_ubsync.ub_rootbp);
1441}
1442
1443void
1444spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp)
1445{
1446 spa->spa_uberblock.ub_rootbp = *bp;
1447}
1448
1449void
1450spa_altroot(spa_t *spa, char *buf, size_t buflen)
1451{
1452 if (spa->spa_root == NULL)
1453 buf[0] = '\0';
1454 else
1455 (void) strncpy(buf, spa->spa_root, buflen);
1456}
1457
1458int
1459spa_sync_pass(spa_t *spa)
1460{
1461 return (spa->spa_sync_pass);
1462}
1463
1464char *
1465spa_name(spa_t *spa)
1466{
34dc7c2f
BB
1467 return (spa->spa_name);
1468}
1469
1470uint64_t
1471spa_guid(spa_t *spa)
1472{
3bc7e0fb
GW
1473 dsl_pool_t *dp = spa_get_dsl(spa);
1474 uint64_t guid;
1475
34dc7c2f
BB
1476 /*
1477 * If we fail to parse the config during spa_load(), we can go through
1478 * the error path (which posts an ereport) and end up here with no root
3541dc6d 1479 * vdev. We stash the original pool guid in 'spa_config_guid' to handle
34dc7c2f
BB
1480 * this case.
1481 */
3bc7e0fb
GW
1482 if (spa->spa_root_vdev == NULL)
1483 return (spa->spa_config_guid);
1484
1485 guid = spa->spa_last_synced_guid != 0 ?
1486 spa->spa_last_synced_guid : spa->spa_root_vdev->vdev_guid;
1487
1488 /*
1489 * Return the most recently synced out guid unless we're
1490 * in syncing context.
1491 */
1492 if (dp && dsl_pool_sync_context(dp))
34dc7c2f
BB
1493 return (spa->spa_root_vdev->vdev_guid);
1494 else
3bc7e0fb 1495 return (guid);
3541dc6d
GA
1496}
1497
1498uint64_t
1499spa_load_guid(spa_t *spa)
1500{
1501 /*
1502 * This is a GUID that exists solely as a reference for the
1503 * purposes of the arc. It is generated at load time, and
1504 * is never written to persistent storage.
1505 */
1506 return (spa->spa_load_guid);
34dc7c2f
BB
1507}
1508
1509uint64_t
1510spa_last_synced_txg(spa_t *spa)
1511{
1512 return (spa->spa_ubsync.ub_txg);
1513}
1514
1515uint64_t
1516spa_first_txg(spa_t *spa)
1517{
1518 return (spa->spa_first_txg);
1519}
1520
428870ff
BB
1521uint64_t
1522spa_syncing_txg(spa_t *spa)
1523{
1524 return (spa->spa_syncing_txg);
1525}
1526
b128c09f 1527pool_state_t
34dc7c2f
BB
1528spa_state(spa_t *spa)
1529{
1530 return (spa->spa_state);
1531}
1532
428870ff
BB
1533spa_load_state_t
1534spa_load_state(spa_t *spa)
34dc7c2f 1535{
428870ff 1536 return (spa->spa_load_state);
34dc7c2f
BB
1537}
1538
34dc7c2f 1539uint64_t
428870ff 1540spa_freeze_txg(spa_t *spa)
34dc7c2f 1541{
428870ff 1542 return (spa->spa_freeze_txg);
34dc7c2f
BB
1543}
1544
428870ff 1545/* ARGSUSED */
34dc7c2f 1546uint64_t
428870ff 1547spa_get_asize(spa_t *spa, uint64_t lsize)
34dc7c2f 1548{
a08ee875 1549 return (lsize * spa_asize_inflation);
34dc7c2f
BB
1550}
1551
34dc7c2f
BB
1552uint64_t
1553spa_get_dspace(spa_t *spa)
1554{
428870ff 1555 return (spa->spa_dspace);
34dc7c2f
BB
1556}
1557
428870ff
BB
1558void
1559spa_update_dspace(spa_t *spa)
34dc7c2f 1560{
428870ff
BB
1561 spa->spa_dspace = metaslab_class_get_dspace(spa_normal_class(spa)) +
1562 ddt_get_dedup_dspace(spa);
34dc7c2f
BB
1563}
1564
1565/*
1566 * Return the failure mode that has been set to this pool. The default
1567 * behavior will be to block all I/Os when a complete failure occurs.
1568 */
1569uint8_t
1570spa_get_failmode(spa_t *spa)
1571{
1572 return (spa->spa_failmode);
1573}
1574
b128c09f
BB
1575boolean_t
1576spa_suspended(spa_t *spa)
1577{
1578 return (spa->spa_suspended);
1579}
1580
34dc7c2f
BB
1581uint64_t
1582spa_version(spa_t *spa)
1583{
1584 return (spa->spa_ubsync.ub_version);
1585}
1586
428870ff
BB
1587boolean_t
1588spa_deflate(spa_t *spa)
1589{
1590 return (spa->spa_deflate);
1591}
1592
1593metaslab_class_t *
1594spa_normal_class(spa_t *spa)
1595{
1596 return (spa->spa_normal_class);
1597}
1598
1599metaslab_class_t *
1600spa_log_class(spa_t *spa)
1601{
1602 return (spa->spa_log_class);
1603}
1604
34dc7c2f
BB
1605int
1606spa_max_replication(spa_t *spa)
1607{
1608 /*
1609 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
1610 * handle BPs with more than one DVA allocated. Set our max
1611 * replication level accordingly.
1612 */
1613 if (spa_version(spa) < SPA_VERSION_DITTO_BLOCKS)
1614 return (1);
1615 return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override));
1616}
1617
428870ff
BB
1618int
1619spa_prev_software_version(spa_t *spa)
1620{
1621 return (spa->spa_prev_software_version);
1622}
1623
c06d4368
AX
1624uint64_t
1625spa_deadman_synctime(spa_t *spa)
1626{
1627 return (spa->spa_deadman_synctime);
1628}
1629
34dc7c2f 1630uint64_t
428870ff 1631dva_get_dsize_sync(spa_t *spa, const dva_t *dva)
34dc7c2f 1632{
428870ff
BB
1633 uint64_t asize = DVA_GET_ASIZE(dva);
1634 uint64_t dsize = asize;
34dc7c2f 1635
428870ff 1636 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
34dc7c2f 1637
428870ff
BB
1638 if (asize != 0 && spa->spa_deflate) {
1639 vdev_t *vd = vdev_lookup_top(spa, DVA_GET_VDEV(dva));
ea04106b
AX
1640 if (vd != NULL)
1641 dsize = (asize >> SPA_MINBLOCKSHIFT) *
1642 vd->vdev_deflate_ratio;
34dc7c2f 1643 }
428870ff
BB
1644
1645 return (dsize);
1646}
1647
1648uint64_t
1649bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp)
1650{
1651 uint64_t dsize = 0;
d6320ddb 1652 int d;
428870ff 1653
ea04106b 1654 for (d = 0; d < BP_GET_NDVAS(bp); d++)
428870ff
BB
1655 dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1656
1657 return (dsize);
1658}
1659
1660uint64_t
1661bp_get_dsize(spa_t *spa, const blkptr_t *bp)
1662{
1663 uint64_t dsize = 0;
d6320ddb 1664 int d;
428870ff
BB
1665
1666 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
1667
ea04106b 1668 for (d = 0; d < BP_GET_NDVAS(bp); d++)
428870ff
BB
1669 dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1670
b128c09f 1671 spa_config_exit(spa, SCL_VDEV, FTAG);
428870ff
BB
1672
1673 return (dsize);
34dc7c2f
BB
1674}
1675
1676/*
1677 * ==========================================================================
1678 * Initialization and Termination
1679 * ==========================================================================
1680 */
1681
1682static int
1683spa_name_compare(const void *a1, const void *a2)
1684{
1685 const spa_t *s1 = a1;
1686 const spa_t *s2 = a2;
1687 int s;
1688
1689 s = strcmp(s1->spa_name, s2->spa_name);
1690 if (s > 0)
1691 return (1);
1692 if (s < 0)
1693 return (-1);
1694 return (0);
1695}
1696
34dc7c2f 1697void
0bc8fd78 1698spa_boot_init(void)
34dc7c2f
BB
1699{
1700 spa_config_load();
1701}
1702
1703void
1704spa_init(int mode)
1705{
1706 mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL);
1707 mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL);
1708 mutex_init(&spa_l2cache_lock, NULL, MUTEX_DEFAULT, NULL);
1709 cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL);
1710
1711 avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t),
1712 offsetof(spa_t, spa_avl));
1713
1714 avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_aux_t),
1715 offsetof(spa_aux_t, aux_avl));
1716
1717 avl_create(&spa_l2cache_avl, spa_l2cache_compare, sizeof (spa_aux_t),
1718 offsetof(spa_aux_t, aux_avl));
1719
fb5f0bc8 1720 spa_mode_global = mode;
34dc7c2f 1721
a08ee875
LG
1722#ifndef _KERNEL
1723 if (spa_mode_global != FREAD && dprintf_find_string("watch")) {
1724 struct sigaction sa;
1725
1726 sa.sa_flags = SA_SIGINFO;
1727 sigemptyset(&sa.sa_mask);
1728 sa.sa_sigaction = arc_buf_sigsegv;
1729
1730 if (sigaction(SIGSEGV, &sa, NULL) == -1) {
1731 perror("could not enable watchpoints: "
1732 "sigaction(SIGSEGV, ...) = ");
1733 } else {
1734 arc_watch = B_TRUE;
1735 }
1736 }
1737#endif
1738
26685276 1739 fm_init();
34dc7c2f
BB
1740 refcount_init();
1741 unique_init();
ea04106b 1742 range_tree_init();
a08ee875 1743 ddt_init();
34dc7c2f
BB
1744 zio_init();
1745 dmu_init();
1746 zil_init();
1747 vdev_cache_stat_init();
ea04106b 1748 vdev_file_init();
34dc7c2f
BB
1749 zfs_prop_init();
1750 zpool_prop_init();
9ae529ec 1751 zpool_feature_init();
34dc7c2f 1752 spa_config_load();
b128c09f 1753 l2arc_start();
34dc7c2f
BB
1754}
1755
1756void
1757spa_fini(void)
1758{
b128c09f
BB
1759 l2arc_stop();
1760
34dc7c2f
BB
1761 spa_evict_all();
1762
ea04106b 1763 vdev_file_fini();
34dc7c2f
BB
1764 vdev_cache_stat_fini();
1765 zil_fini();
1766 dmu_fini();
1767 zio_fini();
a08ee875 1768 ddt_fini();
ea04106b 1769 range_tree_fini();
34dc7c2f
BB
1770 unique_fini();
1771 refcount_fini();
26685276 1772 fm_fini();
34dc7c2f
BB
1773
1774 avl_destroy(&spa_namespace_avl);
1775 avl_destroy(&spa_spare_avl);
1776 avl_destroy(&spa_l2cache_avl);
1777
1778 cv_destroy(&spa_namespace_cv);
1779 mutex_destroy(&spa_namespace_lock);
1780 mutex_destroy(&spa_spare_lock);
1781 mutex_destroy(&spa_l2cache_lock);
1782}
1783
1784/*
1785 * Return whether this pool has slogs. No locking needed.
1786 * It's not a problem if the wrong answer is returned as it's only for
1787 * performance and not correctness
1788 */
1789boolean_t
1790spa_has_slogs(spa_t *spa)
1791{
1792 return (spa->spa_log_class->mc_rotor != NULL);
1793}
b128c09f 1794
428870ff
BB
1795spa_log_state_t
1796spa_get_log_state(spa_t *spa)
1797{
1798 return (spa->spa_log_state);
1799}
1800
1801void
1802spa_set_log_state(spa_t *spa, spa_log_state_t state)
1803{
1804 spa->spa_log_state = state;
1805}
1806
b128c09f
BB
1807boolean_t
1808spa_is_root(spa_t *spa)
1809{
1810 return (spa->spa_is_root);
1811}
fb5f0bc8
BB
1812
1813boolean_t
1814spa_writeable(spa_t *spa)
1815{
1816 return (!!(spa->spa_mode & FWRITE));
1817}
1818
ea04106b
AX
1819/*
1820 * Returns true if there is a pending sync task in any of the current
1821 * syncing txg, the current quiescing txg, or the current open txg.
1822 */
1823boolean_t
1824spa_has_pending_synctask(spa_t *spa)
1825{
1826 return (!txg_all_lists_empty(&spa->spa_dsl_pool->dp_sync_tasks));
1827}
1828
fb5f0bc8
BB
1829int
1830spa_mode(spa_t *spa)
1831{
1832 return (spa->spa_mode);
1833}
428870ff
BB
1834
1835uint64_t
1836spa_bootfs(spa_t *spa)
1837{
1838 return (spa->spa_bootfs);
1839}
1840
1841uint64_t
1842spa_delegation(spa_t *spa)
1843{
1844 return (spa->spa_delegation);
1845}
1846
1847objset_t *
1848spa_meta_objset(spa_t *spa)
1849{
1850 return (spa->spa_meta_objset);
1851}
1852
1853enum zio_checksum
1854spa_dedup_checksum(spa_t *spa)
1855{
1856 return (spa->spa_dedup_checksum);
1857}
1858
1859/*
1860 * Reset pool scan stat per scan pass (or reboot).
1861 */
1862void
1863spa_scan_stat_init(spa_t *spa)
1864{
1865 /* data not stored on disk */
1866 spa->spa_scan_pass_start = gethrestime_sec();
1867 spa->spa_scan_pass_exam = 0;
1868 vdev_scan_stat_init(spa->spa_root_vdev);
1869}
1870
1871/*
1872 * Get scan stats for zpool status reports
1873 */
1874int
1875spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps)
1876{
1877 dsl_scan_t *scn = spa->spa_dsl_pool ? spa->spa_dsl_pool->dp_scan : NULL;
1878
1879 if (scn == NULL || scn->scn_phys.scn_func == POOL_SCAN_NONE)
a08ee875 1880 return (SET_ERROR(ENOENT));
428870ff
BB
1881 bzero(ps, sizeof (pool_scan_stat_t));
1882
1883 /* data stored on disk */
1884 ps->pss_func = scn->scn_phys.scn_func;
1885 ps->pss_start_time = scn->scn_phys.scn_start_time;
1886 ps->pss_end_time = scn->scn_phys.scn_end_time;
1887 ps->pss_to_examine = scn->scn_phys.scn_to_examine;
1888 ps->pss_examined = scn->scn_phys.scn_examined;
1889 ps->pss_to_process = scn->scn_phys.scn_to_process;
1890 ps->pss_processed = scn->scn_phys.scn_processed;
1891 ps->pss_errors = scn->scn_phys.scn_errors;
1892 ps->pss_state = scn->scn_phys.scn_state;
1893
1894 /* data not stored on disk */
1895 ps->pss_pass_start = spa->spa_scan_pass_start;
1896 ps->pss_pass_exam = spa->spa_scan_pass_exam;
1897
1898 return (0);
1899}
c28b2279 1900
6d974228
GW
1901boolean_t
1902spa_debug_enabled(spa_t *spa)
1903{
1904 return (spa->spa_debug);
1905}
1906
c28b2279
BB
1907#if defined(_KERNEL) && defined(HAVE_SPL)
1908/* Namespace manipulation */
1909EXPORT_SYMBOL(spa_lookup);
1910EXPORT_SYMBOL(spa_add);
1911EXPORT_SYMBOL(spa_remove);
1912EXPORT_SYMBOL(spa_next);
1913
1914/* Refcount functions */
1915EXPORT_SYMBOL(spa_open_ref);
1916EXPORT_SYMBOL(spa_close);
1917EXPORT_SYMBOL(spa_refcount_zero);
1918
1919/* Pool configuration lock */
1920EXPORT_SYMBOL(spa_config_tryenter);
1921EXPORT_SYMBOL(spa_config_enter);
1922EXPORT_SYMBOL(spa_config_exit);
1923EXPORT_SYMBOL(spa_config_held);
1924
1925/* Pool vdev add/remove lock */
1926EXPORT_SYMBOL(spa_vdev_enter);
1927EXPORT_SYMBOL(spa_vdev_exit);
1928
1929/* Pool vdev state change lock */
1930EXPORT_SYMBOL(spa_vdev_state_enter);
1931EXPORT_SYMBOL(spa_vdev_state_exit);
1932
1933/* Accessor functions */
1934EXPORT_SYMBOL(spa_shutting_down);
1935EXPORT_SYMBOL(spa_get_dsl);
1936EXPORT_SYMBOL(spa_get_rootblkptr);
1937EXPORT_SYMBOL(spa_set_rootblkptr);
1938EXPORT_SYMBOL(spa_altroot);
1939EXPORT_SYMBOL(spa_sync_pass);
1940EXPORT_SYMBOL(spa_name);
1941EXPORT_SYMBOL(spa_guid);
1942EXPORT_SYMBOL(spa_last_synced_txg);
1943EXPORT_SYMBOL(spa_first_txg);
1944EXPORT_SYMBOL(spa_syncing_txg);
1945EXPORT_SYMBOL(spa_version);
1946EXPORT_SYMBOL(spa_state);
1947EXPORT_SYMBOL(spa_load_state);
1948EXPORT_SYMBOL(spa_freeze_txg);
1949EXPORT_SYMBOL(spa_get_asize);
1950EXPORT_SYMBOL(spa_get_dspace);
1951EXPORT_SYMBOL(spa_update_dspace);
1952EXPORT_SYMBOL(spa_deflate);
1953EXPORT_SYMBOL(spa_normal_class);
1954EXPORT_SYMBOL(spa_log_class);
1955EXPORT_SYMBOL(spa_max_replication);
1956EXPORT_SYMBOL(spa_prev_software_version);
1957EXPORT_SYMBOL(spa_get_failmode);
1958EXPORT_SYMBOL(spa_suspended);
1959EXPORT_SYMBOL(spa_bootfs);
1960EXPORT_SYMBOL(spa_delegation);
1961EXPORT_SYMBOL(spa_meta_objset);
1962
1963/* Miscellaneous support routines */
1964EXPORT_SYMBOL(spa_rename);
1965EXPORT_SYMBOL(spa_guid_exists);
1966EXPORT_SYMBOL(spa_strdup);
1967EXPORT_SYMBOL(spa_strfree);
1968EXPORT_SYMBOL(spa_get_random);
1969EXPORT_SYMBOL(spa_generate_guid);
ea04106b 1970EXPORT_SYMBOL(snprintf_blkptr);
c28b2279
BB
1971EXPORT_SYMBOL(spa_freeze);
1972EXPORT_SYMBOL(spa_upgrade);
1973EXPORT_SYMBOL(spa_evict_all);
1974EXPORT_SYMBOL(spa_lookup_by_guid);
1975EXPORT_SYMBOL(spa_has_spare);
1976EXPORT_SYMBOL(dva_get_dsize_sync);
1977EXPORT_SYMBOL(bp_get_dsize_sync);
1978EXPORT_SYMBOL(bp_get_dsize);
1979EXPORT_SYMBOL(spa_has_slogs);
1980EXPORT_SYMBOL(spa_is_root);
1981EXPORT_SYMBOL(spa_writeable);
1982EXPORT_SYMBOL(spa_mode);
1983
1984EXPORT_SYMBOL(spa_namespace_lock);
c06d4368 1985
ea04106b
AX
1986module_param(zfs_flags, uint, 0644);
1987MODULE_PARM_DESC(zfs_flags, "Set additional debugging flags");
1988
1989module_param(zfs_recover, int, 0644);
1990MODULE_PARM_DESC(zfs_recover, "Set to attempt to recover from fatal errors");
1991
1992module_param(zfs_free_leak_on_eio, int, 0644);
1993MODULE_PARM_DESC(zfs_free_leak_on_eio,
1994 "Set to ignore IO errors during free and permanently leak the space");
1995
a08ee875
LG
1996module_param(zfs_deadman_synctime_ms, ulong, 0644);
1997MODULE_PARM_DESC(zfs_deadman_synctime_ms, "Expiration time in milliseconds");
c06d4368
AX
1998
1999module_param(zfs_deadman_enabled, int, 0644);
2000MODULE_PARM_DESC(zfs_deadman_enabled, "Enable deadman timer");
a08ee875
LG
2001
2002module_param(spa_asize_inflation, int, 0644);
2003MODULE_PARM_DESC(spa_asize_inflation,
2004 "SPA size estimate multiplication factor");
c28b2279 2005#endif