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