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