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