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