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