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