]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/spa_misc.c
Fix type mismatch on 32-bit systems
[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.
b02fe35d 23 * Copyright (c) 2011, 2014 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
b0bc7a84
MG
626 /*
627 * As a pool is being created, treat all features as disabled by
628 * setting SPA_FEATURE_DISABLED for all entries in the feature
629 * refcount cache.
630 */
631 for (i = 0; i < SPA_FEATURES; i++) {
632 spa->spa_feat_refcount_cache[i] = SPA_FEATURE_DISABLED;
633 }
634
34dc7c2f
BB
635 return (spa);
636}
637
638/*
639 * Removes a spa_t from the namespace, freeing up any memory used. Requires
640 * spa_namespace_lock. This is called only after the spa_t has been closed and
641 * deactivated.
642 */
643void
644spa_remove(spa_t *spa)
645{
b128c09f 646 spa_config_dirent_t *dp;
d6320ddb 647 int t;
b128c09f 648
34dc7c2f
BB
649 ASSERT(MUTEX_HELD(&spa_namespace_lock));
650 ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
0c66c32d 651 ASSERT3U(refcount_count(&spa->spa_refcount), ==, 0);
34dc7c2f 652
428870ff
BB
653 nvlist_free(spa->spa_config_splitting);
654
34dc7c2f
BB
655 avl_remove(&spa_namespace_avl, spa);
656 cv_broadcast(&spa_namespace_cv);
657
0336f3d0 658 if (spa->spa_root)
34dc7c2f 659 spa_strfree(spa->spa_root);
34dc7c2f 660
b128c09f
BB
661 while ((dp = list_head(&spa->spa_config_list)) != NULL) {
662 list_remove(&spa->spa_config_list, dp);
663 if (dp->scd_path != NULL)
664 spa_strfree(dp->scd_path);
665 kmem_free(dp, sizeof (spa_config_dirent_t));
666 }
34dc7c2f 667
b128c09f 668 list_destroy(&spa->spa_config_list);
34dc7c2f 669
9ae529ec 670 nvlist_free(spa->spa_label_features);
572e2857 671 nvlist_free(spa->spa_load_info);
417104bd 672 nvlist_free(spa->spa_feat_stats);
34dc7c2f
BB
673 spa_config_set(spa, NULL);
674
675 refcount_destroy(&spa->spa_refcount);
676
1421c891 677 spa_stats_destroy(spa);
b128c09f 678 spa_config_lock_destroy(spa);
34dc7c2f 679
d6320ddb 680 for (t = 0; t < TXG_SIZE; t++)
428870ff
BB
681 bplist_destroy(&spa->spa_free_bplist[t]);
682
34dc7c2f 683 cv_destroy(&spa->spa_async_cv);
0c66c32d 684 cv_destroy(&spa->spa_evicting_os_cv);
428870ff 685 cv_destroy(&spa->spa_proc_cv);
34dc7c2f 686 cv_destroy(&spa->spa_scrub_io_cv);
b128c09f 687 cv_destroy(&spa->spa_suspend_cv);
34dc7c2f 688
34dc7c2f 689 mutex_destroy(&spa->spa_async_lock);
34dc7c2f 690 mutex_destroy(&spa->spa_errlist_lock);
428870ff 691 mutex_destroy(&spa->spa_errlog_lock);
0c66c32d 692 mutex_destroy(&spa->spa_evicting_os_lock);
34dc7c2f 693 mutex_destroy(&spa->spa_history_lock);
428870ff 694 mutex_destroy(&spa->spa_proc_lock);
34dc7c2f 695 mutex_destroy(&spa->spa_props_lock);
428870ff 696 mutex_destroy(&spa->spa_scrub_lock);
b128c09f 697 mutex_destroy(&spa->spa_suspend_lock);
428870ff 698 mutex_destroy(&spa->spa_vdev_top_lock);
4eb30c68 699 mutex_destroy(&spa->spa_feat_stats_lock);
34dc7c2f
BB
700
701 kmem_free(spa, sizeof (spa_t));
702}
703
704/*
705 * Given a pool, return the next pool in the namespace, or NULL if there is
706 * none. If 'prev' is NULL, return the first pool.
707 */
708spa_t *
709spa_next(spa_t *prev)
710{
711 ASSERT(MUTEX_HELD(&spa_namespace_lock));
712
713 if (prev)
714 return (AVL_NEXT(&spa_namespace_avl, prev));
715 else
716 return (avl_first(&spa_namespace_avl));
717}
718
719/*
720 * ==========================================================================
721 * SPA refcount functions
722 * ==========================================================================
723 */
724
725/*
726 * Add a reference to the given spa_t. Must have at least one reference, or
727 * have the namespace lock held.
728 */
729void
730spa_open_ref(spa_t *spa, void *tag)
731{
b128c09f 732 ASSERT(refcount_count(&spa->spa_refcount) >= spa->spa_minref ||
34dc7c2f 733 MUTEX_HELD(&spa_namespace_lock));
34dc7c2f
BB
734 (void) refcount_add(&spa->spa_refcount, tag);
735}
736
737/*
738 * Remove a reference to the given spa_t. Must have at least one reference, or
739 * have the namespace lock held.
740 */
741void
742spa_close(spa_t *spa, void *tag)
743{
b128c09f 744 ASSERT(refcount_count(&spa->spa_refcount) > spa->spa_minref ||
34dc7c2f 745 MUTEX_HELD(&spa_namespace_lock));
34dc7c2f
BB
746 (void) refcount_remove(&spa->spa_refcount, tag);
747}
748
0c66c32d
JG
749/*
750 * Remove a reference to the given spa_t held by a dsl dir that is
751 * being asynchronously released. Async releases occur from a taskq
752 * performing eviction of dsl datasets and dirs. The namespace lock
753 * isn't held and the hold by the object being evicted may contribute to
754 * spa_minref (e.g. dataset or directory released during pool export),
755 * so the asserts in spa_close() do not apply.
756 */
757void
758spa_async_close(spa_t *spa, void *tag)
759{
760 (void) refcount_remove(&spa->spa_refcount, tag);
761}
762
34dc7c2f
BB
763/*
764 * Check to see if the spa refcount is zero. Must be called with
b128c09f 765 * spa_namespace_lock held. We really compare against spa_minref, which is the
34dc7c2f
BB
766 * number of references acquired when opening a pool
767 */
768boolean_t
769spa_refcount_zero(spa_t *spa)
770{
771 ASSERT(MUTEX_HELD(&spa_namespace_lock));
772
b128c09f 773 return (refcount_count(&spa->spa_refcount) == spa->spa_minref);
34dc7c2f
BB
774}
775
776/*
777 * ==========================================================================
778 * SPA spare and l2cache tracking
779 * ==========================================================================
780 */
781
782/*
783 * Hot spares and cache devices are tracked using the same code below,
784 * for 'auxiliary' devices.
785 */
786
787typedef struct spa_aux {
788 uint64_t aux_guid;
789 uint64_t aux_pool;
790 avl_node_t aux_avl;
791 int aux_count;
792} spa_aux_t;
793
794static int
795spa_aux_compare(const void *a, const void *b)
796{
797 const spa_aux_t *sa = a;
798 const spa_aux_t *sb = b;
799
800 if (sa->aux_guid < sb->aux_guid)
801 return (-1);
802 else if (sa->aux_guid > sb->aux_guid)
803 return (1);
804 else
805 return (0);
806}
807
808void
809spa_aux_add(vdev_t *vd, avl_tree_t *avl)
810{
811 avl_index_t where;
812 spa_aux_t search;
813 spa_aux_t *aux;
814
815 search.aux_guid = vd->vdev_guid;
816 if ((aux = avl_find(avl, &search, &where)) != NULL) {
817 aux->aux_count++;
818 } else {
79c76d5b 819 aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP);
34dc7c2f
BB
820 aux->aux_guid = vd->vdev_guid;
821 aux->aux_count = 1;
822 avl_insert(avl, aux, where);
823 }
824}
825
826void
827spa_aux_remove(vdev_t *vd, avl_tree_t *avl)
828{
829 spa_aux_t search;
830 spa_aux_t *aux;
831 avl_index_t where;
832
833 search.aux_guid = vd->vdev_guid;
834 aux = avl_find(avl, &search, &where);
835
836 ASSERT(aux != NULL);
837
838 if (--aux->aux_count == 0) {
839 avl_remove(avl, aux);
840 kmem_free(aux, sizeof (spa_aux_t));
841 } else if (aux->aux_pool == spa_guid(vd->vdev_spa)) {
842 aux->aux_pool = 0ULL;
843 }
844}
845
846boolean_t
b128c09f 847spa_aux_exists(uint64_t guid, uint64_t *pool, int *refcnt, avl_tree_t *avl)
34dc7c2f
BB
848{
849 spa_aux_t search, *found;
34dc7c2f
BB
850
851 search.aux_guid = guid;
b128c09f 852 found = avl_find(avl, &search, NULL);
34dc7c2f
BB
853
854 if (pool) {
855 if (found)
856 *pool = found->aux_pool;
857 else
858 *pool = 0ULL;
859 }
860
b128c09f
BB
861 if (refcnt) {
862 if (found)
863 *refcnt = found->aux_count;
864 else
865 *refcnt = 0;
866 }
867
34dc7c2f
BB
868 return (found != NULL);
869}
870
871void
872spa_aux_activate(vdev_t *vd, avl_tree_t *avl)
873{
874 spa_aux_t search, *found;
875 avl_index_t where;
876
877 search.aux_guid = vd->vdev_guid;
878 found = avl_find(avl, &search, &where);
879 ASSERT(found != NULL);
880 ASSERT(found->aux_pool == 0ULL);
881
882 found->aux_pool = spa_guid(vd->vdev_spa);
883}
884
885/*
886 * Spares are tracked globally due to the following constraints:
887 *
888 * - A spare may be part of multiple pools.
889 * - A spare may be added to a pool even if it's actively in use within
890 * another pool.
891 * - A spare in use in any pool can only be the source of a replacement if
892 * the target is a spare in the same pool.
893 *
894 * We keep track of all spares on the system through the use of a reference
895 * counted AVL tree. When a vdev is added as a spare, or used as a replacement
896 * spare, then we bump the reference count in the AVL tree. In addition, we set
897 * the 'vdev_isspare' member to indicate that the device is a spare (active or
898 * inactive). When a spare is made active (used to replace a device in the
899 * pool), we also keep track of which pool its been made a part of.
900 *
901 * The 'spa_spare_lock' protects the AVL tree. These functions are normally
902 * called under the spa_namespace lock as part of vdev reconfiguration. The
903 * separate spare lock exists for the status query path, which does not need to
904 * be completely consistent with respect to other vdev configuration changes.
905 */
906
907static int
908spa_spare_compare(const void *a, const void *b)
909{
910 return (spa_aux_compare(a, b));
911}
912
913void
914spa_spare_add(vdev_t *vd)
915{
916 mutex_enter(&spa_spare_lock);
917 ASSERT(!vd->vdev_isspare);
918 spa_aux_add(vd, &spa_spare_avl);
919 vd->vdev_isspare = B_TRUE;
920 mutex_exit(&spa_spare_lock);
921}
922
923void
924spa_spare_remove(vdev_t *vd)
925{
926 mutex_enter(&spa_spare_lock);
927 ASSERT(vd->vdev_isspare);
928 spa_aux_remove(vd, &spa_spare_avl);
929 vd->vdev_isspare = B_FALSE;
930 mutex_exit(&spa_spare_lock);
931}
932
933boolean_t
b128c09f 934spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt)
34dc7c2f
BB
935{
936 boolean_t found;
937
938 mutex_enter(&spa_spare_lock);
b128c09f 939 found = spa_aux_exists(guid, pool, refcnt, &spa_spare_avl);
34dc7c2f
BB
940 mutex_exit(&spa_spare_lock);
941
942 return (found);
943}
944
945void
946spa_spare_activate(vdev_t *vd)
947{
948 mutex_enter(&spa_spare_lock);
949 ASSERT(vd->vdev_isspare);
950 spa_aux_activate(vd, &spa_spare_avl);
951 mutex_exit(&spa_spare_lock);
952}
953
954/*
955 * Level 2 ARC devices are tracked globally for the same reasons as spares.
956 * Cache devices currently only support one pool per cache device, and so
957 * for these devices the aux reference count is currently unused beyond 1.
958 */
959
960static int
961spa_l2cache_compare(const void *a, const void *b)
962{
963 return (spa_aux_compare(a, b));
964}
965
966void
967spa_l2cache_add(vdev_t *vd)
968{
969 mutex_enter(&spa_l2cache_lock);
970 ASSERT(!vd->vdev_isl2cache);
971 spa_aux_add(vd, &spa_l2cache_avl);
972 vd->vdev_isl2cache = B_TRUE;
973 mutex_exit(&spa_l2cache_lock);
974}
975
976void
977spa_l2cache_remove(vdev_t *vd)
978{
979 mutex_enter(&spa_l2cache_lock);
980 ASSERT(vd->vdev_isl2cache);
981 spa_aux_remove(vd, &spa_l2cache_avl);
982 vd->vdev_isl2cache = B_FALSE;
983 mutex_exit(&spa_l2cache_lock);
984}
985
986boolean_t
987spa_l2cache_exists(uint64_t guid, uint64_t *pool)
988{
989 boolean_t found;
990
991 mutex_enter(&spa_l2cache_lock);
b128c09f 992 found = spa_aux_exists(guid, pool, NULL, &spa_l2cache_avl);
34dc7c2f
BB
993 mutex_exit(&spa_l2cache_lock);
994
995 return (found);
996}
997
998void
999spa_l2cache_activate(vdev_t *vd)
1000{
1001 mutex_enter(&spa_l2cache_lock);
1002 ASSERT(vd->vdev_isl2cache);
1003 spa_aux_activate(vd, &spa_l2cache_avl);
1004 mutex_exit(&spa_l2cache_lock);
1005}
1006
34dc7c2f
BB
1007/*
1008 * ==========================================================================
1009 * SPA vdev locking
1010 * ==========================================================================
1011 */
1012
1013/*
1014 * Lock the given spa_t for the purpose of adding or removing a vdev.
1015 * Grabs the global spa_namespace_lock plus the spa config lock for writing.
1016 * It returns the next transaction group for the spa_t.
1017 */
1018uint64_t
1019spa_vdev_enter(spa_t *spa)
1020{
428870ff 1021 mutex_enter(&spa->spa_vdev_top_lock);
34dc7c2f 1022 mutex_enter(&spa_namespace_lock);
428870ff
BB
1023 return (spa_vdev_config_enter(spa));
1024}
1025
1026/*
1027 * Internal implementation for spa_vdev_enter(). Used when a vdev
1028 * operation requires multiple syncs (i.e. removing a device) while
1029 * keeping the spa_namespace_lock held.
1030 */
1031uint64_t
1032spa_vdev_config_enter(spa_t *spa)
1033{
1034 ASSERT(MUTEX_HELD(&spa_namespace_lock));
34dc7c2f 1035
b128c09f 1036 spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
34dc7c2f
BB
1037
1038 return (spa_last_synced_txg(spa) + 1);
1039}
1040
1041/*
428870ff
BB
1042 * Used in combination with spa_vdev_config_enter() to allow the syncing
1043 * of multiple transactions without releasing the spa_namespace_lock.
34dc7c2f 1044 */
428870ff
BB
1045void
1046spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error, char *tag)
34dc7c2f
BB
1047{
1048 int config_changed = B_FALSE;
1049
d6320ddb 1050 ASSERT(MUTEX_HELD(&spa_namespace_lock));
34dc7c2f
BB
1051 ASSERT(txg > spa_last_synced_txg(spa));
1052
b128c09f
BB
1053 spa->spa_pending_vdev = NULL;
1054
34dc7c2f
BB
1055 /*
1056 * Reassess the DTLs.
1057 */
1058 vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE);
1059
b128c09f 1060 if (error == 0 && !list_is_empty(&spa->spa_config_dirty_list)) {
34dc7c2f 1061 config_changed = B_TRUE;
428870ff 1062 spa->spa_config_generation++;
34dc7c2f
BB
1063 }
1064
428870ff
BB
1065 /*
1066 * Verify the metaslab classes.
1067 */
1068 ASSERT(metaslab_class_validate(spa_normal_class(spa)) == 0);
1069 ASSERT(metaslab_class_validate(spa_log_class(spa)) == 0);
1070
b128c09f 1071 spa_config_exit(spa, SCL_ALL, spa);
34dc7c2f 1072
428870ff
BB
1073 /*
1074 * Panic the system if the specified tag requires it. This
1075 * is useful for ensuring that configurations are updated
1076 * transactionally.
1077 */
1078 if (zio_injection_enabled)
1079 zio_handle_panic_injection(spa, tag, 0);
1080
34dc7c2f
BB
1081 /*
1082 * Note: this txg_wait_synced() is important because it ensures
1083 * that there won't be more than one config change per txg.
1084 * This allows us to use the txg as the generation number.
1085 */
1086 if (error == 0)
1087 txg_wait_synced(spa->spa_dsl_pool, txg);
1088
1089 if (vd != NULL) {
93cf2076 1090 ASSERT(!vd->vdev_detached || vd->vdev_dtl_sm == NULL);
fb5f0bc8 1091 spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
34dc7c2f 1092 vdev_free(vd);
fb5f0bc8 1093 spa_config_exit(spa, SCL_ALL, spa);
34dc7c2f
BB
1094 }
1095
1096 /*
1097 * If the config changed, update the config cache.
1098 */
1099 if (config_changed)
b128c09f 1100 spa_config_sync(spa, B_FALSE, B_TRUE);
428870ff 1101}
34dc7c2f 1102
428870ff
BB
1103/*
1104 * Unlock the spa_t after adding or removing a vdev. Besides undoing the
1105 * locking of spa_vdev_enter(), we also want make sure the transactions have
1106 * synced to disk, and then update the global configuration cache with the new
1107 * information.
1108 */
1109int
1110spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
1111{
1112 spa_vdev_config_exit(spa, vd, txg, error, FTAG);
34dc7c2f 1113 mutex_exit(&spa_namespace_lock);
428870ff 1114 mutex_exit(&spa->spa_vdev_top_lock);
34dc7c2f
BB
1115
1116 return (error);
1117}
1118
b128c09f
BB
1119/*
1120 * Lock the given spa_t for the purpose of changing vdev state.
1121 */
1122void
428870ff 1123spa_vdev_state_enter(spa_t *spa, int oplocks)
b128c09f 1124{
428870ff
BB
1125 int locks = SCL_STATE_ALL | oplocks;
1126
1127 /*
1128 * Root pools may need to read of the underlying devfs filesystem
1129 * when opening up a vdev. Unfortunately if we're holding the
1130 * SCL_ZIO lock it will result in a deadlock when we try to issue
1131 * the read from the root filesystem. Instead we "prefetch"
1132 * the associated vnodes that we need prior to opening the
1133 * underlying devices and cache them so that we can prevent
1134 * any I/O when we are doing the actual open.
1135 */
1136 if (spa_is_root(spa)) {
1137 int low = locks & ~(SCL_ZIO - 1);
1138 int high = locks & ~low;
1139
1140 spa_config_enter(spa, high, spa, RW_WRITER);
1141 vdev_hold(spa->spa_root_vdev);
1142 spa_config_enter(spa, low, spa, RW_WRITER);
1143 } else {
1144 spa_config_enter(spa, locks, spa, RW_WRITER);
1145 }
1146 spa->spa_vdev_locks = locks;
b128c09f
BB
1147}
1148
1149int
1150spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error)
1151{
428870ff
BB
1152 boolean_t config_changed = B_FALSE;
1153
1154 if (vd != NULL || error == 0)
1155 vdev_dtl_reassess(vd ? vd->vdev_top : spa->spa_root_vdev,
1156 0, 0, B_FALSE);
1157
1158 if (vd != NULL) {
b128c09f 1159 vdev_state_dirty(vd->vdev_top);
428870ff
BB
1160 config_changed = B_TRUE;
1161 spa->spa_config_generation++;
1162 }
b128c09f 1163
428870ff
BB
1164 if (spa_is_root(spa))
1165 vdev_rele(spa->spa_root_vdev);
1166
1167 ASSERT3U(spa->spa_vdev_locks, >=, SCL_STATE_ALL);
1168 spa_config_exit(spa, spa->spa_vdev_locks, spa);
b128c09f 1169
fb5f0bc8
BB
1170 /*
1171 * If anything changed, wait for it to sync. This ensures that,
1172 * from the system administrator's perspective, zpool(1M) commands
1173 * are synchronous. This is important for things like zpool offline:
1174 * when the command completes, you expect no further I/O from ZFS.
1175 */
1176 if (vd != NULL)
1177 txg_wait_synced(spa->spa_dsl_pool, 0);
1178
428870ff
BB
1179 /*
1180 * If the config changed, update the config cache.
1181 */
1182 if (config_changed) {
1183 mutex_enter(&spa_namespace_lock);
1184 spa_config_sync(spa, B_FALSE, B_TRUE);
1185 mutex_exit(&spa_namespace_lock);
1186 }
1187
b128c09f
BB
1188 return (error);
1189}
1190
34dc7c2f
BB
1191/*
1192 * ==========================================================================
1193 * Miscellaneous functions
1194 * ==========================================================================
1195 */
1196
9ae529ec 1197void
b0bc7a84 1198spa_activate_mos_feature(spa_t *spa, const char *feature, dmu_tx_t *tx)
9ae529ec 1199{
fa86b5db
MA
1200 if (!nvlist_exists(spa->spa_label_features, feature)) {
1201 fnvlist_add_boolean(spa->spa_label_features, feature);
b0bc7a84
MG
1202 /*
1203 * When we are creating the pool (tx_txg==TXG_INITIAL), we can't
1204 * dirty the vdev config because lock SCL_CONFIG is not held.
1205 * Thankfully, in this case we don't need to dirty the config
1206 * because it will be written out anyway when we finish
1207 * creating the pool.
1208 */
1209 if (tx->tx_txg != TXG_INITIAL)
1210 vdev_config_dirty(spa->spa_root_vdev);
fa86b5db 1211 }
9ae529ec
CS
1212}
1213
1214void
1215spa_deactivate_mos_feature(spa_t *spa, const char *feature)
1216{
fa86b5db
MA
1217 if (nvlist_remove_all(spa->spa_label_features, feature) == 0)
1218 vdev_config_dirty(spa->spa_root_vdev);
9ae529ec
CS
1219}
1220
34dc7c2f
BB
1221/*
1222 * Rename a spa_t.
1223 */
1224int
1225spa_rename(const char *name, const char *newname)
1226{
1227 spa_t *spa;
1228 int err;
1229
1230 /*
1231 * Lookup the spa_t and grab the config lock for writing. We need to
1232 * actually open the pool so that we can sync out the necessary labels.
1233 * It's OK to call spa_open() with the namespace lock held because we
1234 * allow recursive calls for other reasons.
1235 */
1236 mutex_enter(&spa_namespace_lock);
1237 if ((err = spa_open(name, &spa, FTAG)) != 0) {
1238 mutex_exit(&spa_namespace_lock);
1239 return (err);
1240 }
1241
b128c09f 1242 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34dc7c2f
BB
1243
1244 avl_remove(&spa_namespace_avl, spa);
b128c09f 1245 (void) strlcpy(spa->spa_name, newname, sizeof (spa->spa_name));
34dc7c2f
BB
1246 avl_add(&spa_namespace_avl, spa);
1247
1248 /*
1249 * Sync all labels to disk with the new names by marking the root vdev
1250 * dirty and waiting for it to sync. It will pick up the new pool name
1251 * during the sync.
1252 */
1253 vdev_config_dirty(spa->spa_root_vdev);
1254
b128c09f 1255 spa_config_exit(spa, SCL_ALL, FTAG);
34dc7c2f
BB
1256
1257 txg_wait_synced(spa->spa_dsl_pool, 0);
1258
1259 /*
1260 * Sync the updated config cache.
1261 */
b128c09f 1262 spa_config_sync(spa, B_FALSE, B_TRUE);
34dc7c2f
BB
1263
1264 spa_close(spa, FTAG);
1265
1266 mutex_exit(&spa_namespace_lock);
1267
1268 return (0);
1269}
1270
34dc7c2f 1271/*
572e2857
BB
1272 * Return the spa_t associated with given pool_guid, if it exists. If
1273 * device_guid is non-zero, determine whether the pool exists *and* contains
1274 * a device with the specified device_guid.
34dc7c2f 1275 */
572e2857
BB
1276spa_t *
1277spa_by_guid(uint64_t pool_guid, uint64_t device_guid)
34dc7c2f
BB
1278{
1279 spa_t *spa;
1280 avl_tree_t *t = &spa_namespace_avl;
1281
1282 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1283
1284 for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) {
1285 if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1286 continue;
1287 if (spa->spa_root_vdev == NULL)
1288 continue;
1289 if (spa_guid(spa) == pool_guid) {
1290 if (device_guid == 0)
1291 break;
1292
1293 if (vdev_lookup_by_guid(spa->spa_root_vdev,
1294 device_guid) != NULL)
1295 break;
1296
1297 /*
1298 * Check any devices we may be in the process of adding.
1299 */
1300 if (spa->spa_pending_vdev) {
1301 if (vdev_lookup_by_guid(spa->spa_pending_vdev,
1302 device_guid) != NULL)
1303 break;
1304 }
1305 }
1306 }
1307
572e2857
BB
1308 return (spa);
1309}
1310
1311/*
1312 * Determine whether a pool with the given pool_guid exists.
1313 */
1314boolean_t
1315spa_guid_exists(uint64_t pool_guid, uint64_t device_guid)
1316{
1317 return (spa_by_guid(pool_guid, device_guid) != NULL);
34dc7c2f
BB
1318}
1319
1320char *
1321spa_strdup(const char *s)
1322{
1323 size_t len;
1324 char *new;
1325
1326 len = strlen(s);
79c76d5b 1327 new = kmem_alloc(len + 1, KM_SLEEP);
34dc7c2f
BB
1328 bcopy(s, new, len);
1329 new[len] = '\0';
1330
1331 return (new);
1332}
1333
1334void
1335spa_strfree(char *s)
1336{
1337 kmem_free(s, strlen(s) + 1);
1338}
1339
1340uint64_t
1341spa_get_random(uint64_t range)
1342{
1343 uint64_t r;
1344
1345 ASSERT(range != 0);
1346
1347 (void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t));
1348
1349 return (r % range);
1350}
1351
428870ff
BB
1352uint64_t
1353spa_generate_guid(spa_t *spa)
34dc7c2f 1354{
428870ff 1355 uint64_t guid = spa_get_random(-1ULL);
34dc7c2f 1356
428870ff
BB
1357 if (spa != NULL) {
1358 while (guid == 0 || spa_guid_exists(spa_guid(spa), guid))
1359 guid = spa_get_random(-1ULL);
1360 } else {
1361 while (guid == 0 || spa_guid_exists(guid, 0))
1362 guid = spa_get_random(-1ULL);
34dc7c2f
BB
1363 }
1364
428870ff
BB
1365 return (guid);
1366}
1367
1368void
b0bc7a84 1369snprintf_blkptr(char *buf, size_t buflen, const blkptr_t *bp)
428870ff 1370{
9ae529ec 1371 char type[256];
428870ff
BB
1372 char *checksum = NULL;
1373 char *compress = NULL;
34dc7c2f 1374
428870ff 1375 if (bp != NULL) {
9ae529ec
CS
1376 if (BP_GET_TYPE(bp) & DMU_OT_NEWTYPE) {
1377 dmu_object_byteswap_t bswap =
1378 DMU_OT_BYTESWAP(BP_GET_TYPE(bp));
1379 (void) snprintf(type, sizeof (type), "bswap %s %s",
1380 DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) ?
1381 "metadata" : "data",
1382 dmu_ot_byteswap[bswap].ob_name);
1383 } else {
1384 (void) strlcpy(type, dmu_ot[BP_GET_TYPE(bp)].ot_name,
1385 sizeof (type));
1386 }
9b67f605
MA
1387 if (!BP_IS_EMBEDDED(bp)) {
1388 checksum =
1389 zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name;
1390 }
428870ff 1391 compress = zio_compress_table[BP_GET_COMPRESS(bp)].ci_name;
34dc7c2f
BB
1392 }
1393
b0bc7a84
MG
1394 SNPRINTF_BLKPTR(snprintf, ' ', buf, buflen, bp, type, checksum,
1395 compress);
34dc7c2f
BB
1396}
1397
1398void
1399spa_freeze(spa_t *spa)
1400{
1401 uint64_t freeze_txg = 0;
1402
b128c09f 1403 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34dc7c2f
BB
1404 if (spa->spa_freeze_txg == UINT64_MAX) {
1405 freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE;
1406 spa->spa_freeze_txg = freeze_txg;
1407 }
b128c09f 1408 spa_config_exit(spa, SCL_ALL, FTAG);
34dc7c2f
BB
1409 if (freeze_txg != 0)
1410 txg_wait_synced(spa_get_dsl(spa), freeze_txg);
1411}
1412
0b39b9f9
PS
1413void
1414zfs_panic_recover(const char *fmt, ...)
1415{
1416 va_list adx;
1417
1418 va_start(adx, fmt);
1419 vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx);
1420 va_end(adx);
1421}
1422
428870ff
BB
1423/*
1424 * This is a stripped-down version of strtoull, suitable only for converting
d3cc8b15 1425 * lowercase hexadecimal numbers that don't overflow.
428870ff
BB
1426 */
1427uint64_t
1428strtonum(const char *str, char **nptr)
1429{
1430 uint64_t val = 0;
1431 char c;
1432 int digit;
1433
1434 while ((c = *str) != '\0') {
1435 if (c >= '0' && c <= '9')
1436 digit = c - '0';
1437 else if (c >= 'a' && c <= 'f')
1438 digit = 10 + c - 'a';
1439 else
1440 break;
1441
1442 val *= 16;
1443 val += digit;
1444
1445 str++;
1446 }
1447
1448 if (nptr)
1449 *nptr = (char *)str;
1450
1451 return (val);
1452}
1453
34dc7c2f
BB
1454/*
1455 * ==========================================================================
1456 * Accessor functions
1457 * ==========================================================================
1458 */
1459
b128c09f
BB
1460boolean_t
1461spa_shutting_down(spa_t *spa)
34dc7c2f 1462{
b128c09f 1463 return (spa->spa_async_suspended);
34dc7c2f
BB
1464}
1465
1466dsl_pool_t *
1467spa_get_dsl(spa_t *spa)
1468{
1469 return (spa->spa_dsl_pool);
1470}
1471
9ae529ec
CS
1472boolean_t
1473spa_is_initializing(spa_t *spa)
1474{
1475 return (spa->spa_is_initializing);
1476}
1477
34dc7c2f
BB
1478blkptr_t *
1479spa_get_rootblkptr(spa_t *spa)
1480{
1481 return (&spa->spa_ubsync.ub_rootbp);
1482}
1483
1484void
1485spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp)
1486{
1487 spa->spa_uberblock.ub_rootbp = *bp;
1488}
1489
1490void
1491spa_altroot(spa_t *spa, char *buf, size_t buflen)
1492{
1493 if (spa->spa_root == NULL)
1494 buf[0] = '\0';
1495 else
1496 (void) strncpy(buf, spa->spa_root, buflen);
1497}
1498
1499int
1500spa_sync_pass(spa_t *spa)
1501{
1502 return (spa->spa_sync_pass);
1503}
1504
1505char *
1506spa_name(spa_t *spa)
1507{
34dc7c2f
BB
1508 return (spa->spa_name);
1509}
1510
1511uint64_t
1512spa_guid(spa_t *spa)
1513{
3bc7e0fb
GW
1514 dsl_pool_t *dp = spa_get_dsl(spa);
1515 uint64_t guid;
1516
34dc7c2f
BB
1517 /*
1518 * If we fail to parse the config during spa_load(), we can go through
1519 * the error path (which posts an ereport) and end up here with no root
3541dc6d 1520 * vdev. We stash the original pool guid in 'spa_config_guid' to handle
34dc7c2f
BB
1521 * this case.
1522 */
3bc7e0fb
GW
1523 if (spa->spa_root_vdev == NULL)
1524 return (spa->spa_config_guid);
1525
1526 guid = spa->spa_last_synced_guid != 0 ?
1527 spa->spa_last_synced_guid : spa->spa_root_vdev->vdev_guid;
1528
1529 /*
1530 * Return the most recently synced out guid unless we're
1531 * in syncing context.
1532 */
1533 if (dp && dsl_pool_sync_context(dp))
34dc7c2f
BB
1534 return (spa->spa_root_vdev->vdev_guid);
1535 else
3bc7e0fb 1536 return (guid);
3541dc6d
GA
1537}
1538
1539uint64_t
1540spa_load_guid(spa_t *spa)
1541{
1542 /*
1543 * This is a GUID that exists solely as a reference for the
1544 * purposes of the arc. It is generated at load time, and
1545 * is never written to persistent storage.
1546 */
1547 return (spa->spa_load_guid);
34dc7c2f
BB
1548}
1549
1550uint64_t
1551spa_last_synced_txg(spa_t *spa)
1552{
1553 return (spa->spa_ubsync.ub_txg);
1554}
1555
1556uint64_t
1557spa_first_txg(spa_t *spa)
1558{
1559 return (spa->spa_first_txg);
1560}
1561
428870ff
BB
1562uint64_t
1563spa_syncing_txg(spa_t *spa)
1564{
1565 return (spa->spa_syncing_txg);
1566}
1567
b128c09f 1568pool_state_t
34dc7c2f
BB
1569spa_state(spa_t *spa)
1570{
1571 return (spa->spa_state);
1572}
1573
428870ff
BB
1574spa_load_state_t
1575spa_load_state(spa_t *spa)
34dc7c2f 1576{
428870ff 1577 return (spa->spa_load_state);
34dc7c2f
BB
1578}
1579
34dc7c2f 1580uint64_t
428870ff 1581spa_freeze_txg(spa_t *spa)
34dc7c2f 1582{
428870ff 1583 return (spa->spa_freeze_txg);
34dc7c2f
BB
1584}
1585
428870ff 1586/* ARGSUSED */
34dc7c2f 1587uint64_t
428870ff 1588spa_get_asize(spa_t *spa, uint64_t lsize)
34dc7c2f 1589{
e8b96c60 1590 return (lsize * spa_asize_inflation);
34dc7c2f
BB
1591}
1592
3d45fdd6
MA
1593/*
1594 * Return the amount of slop space in bytes. It is 1/32 of the pool (3.2%),
1595 * or at least 32MB.
1596 *
1597 * See the comment above spa_slop_shift for details.
1598 */
1599uint64_t
1600spa_get_slop_space(spa_t *spa) {
1601 uint64_t space = spa_get_dspace(spa);
1602 return (MAX(space >> spa_slop_shift, SPA_MINDEVSIZE >> 1));
1603}
1604
34dc7c2f
BB
1605uint64_t
1606spa_get_dspace(spa_t *spa)
1607{
428870ff 1608 return (spa->spa_dspace);
34dc7c2f
BB
1609}
1610
428870ff
BB
1611void
1612spa_update_dspace(spa_t *spa)
34dc7c2f 1613{
428870ff
BB
1614 spa->spa_dspace = metaslab_class_get_dspace(spa_normal_class(spa)) +
1615 ddt_get_dedup_dspace(spa);
34dc7c2f
BB
1616}
1617
1618/*
1619 * Return the failure mode that has been set to this pool. The default
1620 * behavior will be to block all I/Os when a complete failure occurs.
1621 */
1622uint8_t
1623spa_get_failmode(spa_t *spa)
1624{
1625 return (spa->spa_failmode);
1626}
1627
b128c09f
BB
1628boolean_t
1629spa_suspended(spa_t *spa)
1630{
1631 return (spa->spa_suspended);
1632}
1633
34dc7c2f
BB
1634uint64_t
1635spa_version(spa_t *spa)
1636{
1637 return (spa->spa_ubsync.ub_version);
1638}
1639
428870ff
BB
1640boolean_t
1641spa_deflate(spa_t *spa)
1642{
1643 return (spa->spa_deflate);
1644}
1645
1646metaslab_class_t *
1647spa_normal_class(spa_t *spa)
1648{
1649 return (spa->spa_normal_class);
1650}
1651
1652metaslab_class_t *
1653spa_log_class(spa_t *spa)
1654{
1655 return (spa->spa_log_class);
1656}
1657
0c66c32d
JG
1658void
1659spa_evicting_os_register(spa_t *spa, objset_t *os)
1660{
1661 mutex_enter(&spa->spa_evicting_os_lock);
1662 list_insert_head(&spa->spa_evicting_os_list, os);
1663 mutex_exit(&spa->spa_evicting_os_lock);
1664}
1665
1666void
1667spa_evicting_os_deregister(spa_t *spa, objset_t *os)
1668{
1669 mutex_enter(&spa->spa_evicting_os_lock);
1670 list_remove(&spa->spa_evicting_os_list, os);
1671 cv_broadcast(&spa->spa_evicting_os_cv);
1672 mutex_exit(&spa->spa_evicting_os_lock);
1673}
1674
1675void
1676spa_evicting_os_wait(spa_t *spa)
1677{
1678 mutex_enter(&spa->spa_evicting_os_lock);
1679 while (!list_is_empty(&spa->spa_evicting_os_list))
1680 cv_wait(&spa->spa_evicting_os_cv, &spa->spa_evicting_os_lock);
1681 mutex_exit(&spa->spa_evicting_os_lock);
1682
1683 dmu_buf_user_evict_wait();
1684}
1685
34dc7c2f
BB
1686int
1687spa_max_replication(spa_t *spa)
1688{
1689 /*
1690 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
1691 * handle BPs with more than one DVA allocated. Set our max
1692 * replication level accordingly.
1693 */
1694 if (spa_version(spa) < SPA_VERSION_DITTO_BLOCKS)
1695 return (1);
1696 return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override));
1697}
1698
428870ff
BB
1699int
1700spa_prev_software_version(spa_t *spa)
1701{
1702 return (spa->spa_prev_software_version);
1703}
1704
cc92e9d0
GW
1705uint64_t
1706spa_deadman_synctime(spa_t *spa)
1707{
1708 return (spa->spa_deadman_synctime);
1709}
1710
34dc7c2f 1711uint64_t
428870ff 1712dva_get_dsize_sync(spa_t *spa, const dva_t *dva)
34dc7c2f 1713{
428870ff
BB
1714 uint64_t asize = DVA_GET_ASIZE(dva);
1715 uint64_t dsize = asize;
34dc7c2f 1716
428870ff 1717 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
34dc7c2f 1718
428870ff
BB
1719 if (asize != 0 && spa->spa_deflate) {
1720 vdev_t *vd = vdev_lookup_top(spa, DVA_GET_VDEV(dva));
2c33b912
BB
1721 if (vd != NULL)
1722 dsize = (asize >> SPA_MINBLOCKSHIFT) *
1723 vd->vdev_deflate_ratio;
34dc7c2f 1724 }
428870ff
BB
1725
1726 return (dsize);
1727}
1728
1729uint64_t
1730bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp)
1731{
1732 uint64_t dsize = 0;
d6320ddb 1733 int d;
428870ff 1734
9b67f605 1735 for (d = 0; d < BP_GET_NDVAS(bp); d++)
428870ff
BB
1736 dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1737
1738 return (dsize);
1739}
1740
1741uint64_t
1742bp_get_dsize(spa_t *spa, const blkptr_t *bp)
1743{
1744 uint64_t dsize = 0;
d6320ddb 1745 int d;
428870ff
BB
1746
1747 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
1748
9b67f605 1749 for (d = 0; d < BP_GET_NDVAS(bp); d++)
428870ff
BB
1750 dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1751
b128c09f 1752 spa_config_exit(spa, SCL_VDEV, FTAG);
428870ff
BB
1753
1754 return (dsize);
34dc7c2f
BB
1755}
1756
1757/*
1758 * ==========================================================================
1759 * Initialization and Termination
1760 * ==========================================================================
1761 */
1762
1763static int
1764spa_name_compare(const void *a1, const void *a2)
1765{
1766 const spa_t *s1 = a1;
1767 const spa_t *s2 = a2;
1768 int s;
1769
1770 s = strcmp(s1->spa_name, s2->spa_name);
1771 if (s > 0)
1772 return (1);
1773 if (s < 0)
1774 return (-1);
1775 return (0);
1776}
1777
34dc7c2f 1778void
0bc8fd78 1779spa_boot_init(void)
34dc7c2f
BB
1780{
1781 spa_config_load();
1782}
1783
1784void
1785spa_init(int mode)
1786{
1787 mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL);
1788 mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL);
1789 mutex_init(&spa_l2cache_lock, NULL, MUTEX_DEFAULT, NULL);
1790 cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL);
1791
1792 avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t),
1793 offsetof(spa_t, spa_avl));
1794
1795 avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_aux_t),
1796 offsetof(spa_aux_t, aux_avl));
1797
1798 avl_create(&spa_l2cache_avl, spa_l2cache_compare, sizeof (spa_aux_t),
1799 offsetof(spa_aux_t, aux_avl));
1800
fb5f0bc8 1801 spa_mode_global = mode;
34dc7c2f 1802
498877ba
MA
1803#ifndef _KERNEL
1804 if (spa_mode_global != FREAD && dprintf_find_string("watch")) {
1805 struct sigaction sa;
1806
1807 sa.sa_flags = SA_SIGINFO;
1808 sigemptyset(&sa.sa_mask);
1809 sa.sa_sigaction = arc_buf_sigsegv;
1810
1811 if (sigaction(SIGSEGV, &sa, NULL) == -1) {
1812 perror("could not enable watchpoints: "
1813 "sigaction(SIGSEGV, ...) = ");
1814 } else {
1815 arc_watch = B_TRUE;
1816 }
1817 }
1818#endif
1819
26685276 1820 fm_init();
34dc7c2f
BB
1821 refcount_init();
1822 unique_init();
93cf2076 1823 range_tree_init();
ecf3d9b8 1824 ddt_init();
34dc7c2f
BB
1825 zio_init();
1826 dmu_init();
1827 zil_init();
1828 vdev_cache_stat_init();
bc25c932 1829 vdev_file_init();
34dc7c2f
BB
1830 zfs_prop_init();
1831 zpool_prop_init();
9ae529ec 1832 zpool_feature_init();
34dc7c2f 1833 spa_config_load();
b128c09f 1834 l2arc_start();
34dc7c2f
BB
1835}
1836
1837void
1838spa_fini(void)
1839{
b128c09f
BB
1840 l2arc_stop();
1841
34dc7c2f
BB
1842 spa_evict_all();
1843
bc25c932 1844 vdev_file_fini();
34dc7c2f
BB
1845 vdev_cache_stat_fini();
1846 zil_fini();
1847 dmu_fini();
1848 zio_fini();
ecf3d9b8 1849 ddt_fini();
93cf2076 1850 range_tree_fini();
34dc7c2f
BB
1851 unique_fini();
1852 refcount_fini();
26685276 1853 fm_fini();
34dc7c2f
BB
1854
1855 avl_destroy(&spa_namespace_avl);
1856 avl_destroy(&spa_spare_avl);
1857 avl_destroy(&spa_l2cache_avl);
1858
1859 cv_destroy(&spa_namespace_cv);
1860 mutex_destroy(&spa_namespace_lock);
1861 mutex_destroy(&spa_spare_lock);
1862 mutex_destroy(&spa_l2cache_lock);
1863}
1864
1865/*
1866 * Return whether this pool has slogs. No locking needed.
1867 * It's not a problem if the wrong answer is returned as it's only for
1868 * performance and not correctness
1869 */
1870boolean_t
1871spa_has_slogs(spa_t *spa)
1872{
1873 return (spa->spa_log_class->mc_rotor != NULL);
1874}
b128c09f 1875
428870ff
BB
1876spa_log_state_t
1877spa_get_log_state(spa_t *spa)
1878{
1879 return (spa->spa_log_state);
1880}
1881
1882void
1883spa_set_log_state(spa_t *spa, spa_log_state_t state)
1884{
1885 spa->spa_log_state = state;
1886}
1887
b128c09f
BB
1888boolean_t
1889spa_is_root(spa_t *spa)
1890{
1891 return (spa->spa_is_root);
1892}
fb5f0bc8
BB
1893
1894boolean_t
1895spa_writeable(spa_t *spa)
1896{
1897 return (!!(spa->spa_mode & FWRITE));
1898}
1899
acbad6ff
AR
1900/*
1901 * Returns true if there is a pending sync task in any of the current
1902 * syncing txg, the current quiescing txg, or the current open txg.
1903 */
1904boolean_t
1905spa_has_pending_synctask(spa_t *spa)
1906{
1907 return (!txg_all_lists_empty(&spa->spa_dsl_pool->dp_sync_tasks));
1908}
1909
fb5f0bc8
BB
1910int
1911spa_mode(spa_t *spa)
1912{
1913 return (spa->spa_mode);
1914}
428870ff
BB
1915
1916uint64_t
1917spa_bootfs(spa_t *spa)
1918{
1919 return (spa->spa_bootfs);
1920}
1921
1922uint64_t
1923spa_delegation(spa_t *spa)
1924{
1925 return (spa->spa_delegation);
1926}
1927
1928objset_t *
1929spa_meta_objset(spa_t *spa)
1930{
1931 return (spa->spa_meta_objset);
1932}
1933
1934enum zio_checksum
1935spa_dedup_checksum(spa_t *spa)
1936{
1937 return (spa->spa_dedup_checksum);
1938}
1939
1940/*
1941 * Reset pool scan stat per scan pass (or reboot).
1942 */
1943void
1944spa_scan_stat_init(spa_t *spa)
1945{
1946 /* data not stored on disk */
1947 spa->spa_scan_pass_start = gethrestime_sec();
1948 spa->spa_scan_pass_exam = 0;
1949 vdev_scan_stat_init(spa->spa_root_vdev);
1950}
1951
1952/*
1953 * Get scan stats for zpool status reports
1954 */
1955int
1956spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps)
1957{
1958 dsl_scan_t *scn = spa->spa_dsl_pool ? spa->spa_dsl_pool->dp_scan : NULL;
1959
1960 if (scn == NULL || scn->scn_phys.scn_func == POOL_SCAN_NONE)
2e528b49 1961 return (SET_ERROR(ENOENT));
428870ff
BB
1962 bzero(ps, sizeof (pool_scan_stat_t));
1963
1964 /* data stored on disk */
1965 ps->pss_func = scn->scn_phys.scn_func;
1966 ps->pss_start_time = scn->scn_phys.scn_start_time;
1967 ps->pss_end_time = scn->scn_phys.scn_end_time;
1968 ps->pss_to_examine = scn->scn_phys.scn_to_examine;
1969 ps->pss_examined = scn->scn_phys.scn_examined;
1970 ps->pss_to_process = scn->scn_phys.scn_to_process;
1971 ps->pss_processed = scn->scn_phys.scn_processed;
1972 ps->pss_errors = scn->scn_phys.scn_errors;
1973 ps->pss_state = scn->scn_phys.scn_state;
1974
1975 /* data not stored on disk */
1976 ps->pss_pass_start = spa->spa_scan_pass_start;
1977 ps->pss_pass_exam = spa->spa_scan_pass_exam;
1978
1979 return (0);
1980}
c28b2279 1981
6d974228
GW
1982boolean_t
1983spa_debug_enabled(spa_t *spa)
1984{
1985 return (spa->spa_debug);
1986}
1987
c28b2279
BB
1988#if defined(_KERNEL) && defined(HAVE_SPL)
1989/* Namespace manipulation */
1990EXPORT_SYMBOL(spa_lookup);
1991EXPORT_SYMBOL(spa_add);
1992EXPORT_SYMBOL(spa_remove);
1993EXPORT_SYMBOL(spa_next);
1994
1995/* Refcount functions */
1996EXPORT_SYMBOL(spa_open_ref);
1997EXPORT_SYMBOL(spa_close);
1998EXPORT_SYMBOL(spa_refcount_zero);
1999
2000/* Pool configuration lock */
2001EXPORT_SYMBOL(spa_config_tryenter);
2002EXPORT_SYMBOL(spa_config_enter);
2003EXPORT_SYMBOL(spa_config_exit);
2004EXPORT_SYMBOL(spa_config_held);
2005
2006/* Pool vdev add/remove lock */
2007EXPORT_SYMBOL(spa_vdev_enter);
2008EXPORT_SYMBOL(spa_vdev_exit);
2009
2010/* Pool vdev state change lock */
2011EXPORT_SYMBOL(spa_vdev_state_enter);
2012EXPORT_SYMBOL(spa_vdev_state_exit);
2013
2014/* Accessor functions */
2015EXPORT_SYMBOL(spa_shutting_down);
2016EXPORT_SYMBOL(spa_get_dsl);
2017EXPORT_SYMBOL(spa_get_rootblkptr);
2018EXPORT_SYMBOL(spa_set_rootblkptr);
2019EXPORT_SYMBOL(spa_altroot);
2020EXPORT_SYMBOL(spa_sync_pass);
2021EXPORT_SYMBOL(spa_name);
2022EXPORT_SYMBOL(spa_guid);
2023EXPORT_SYMBOL(spa_last_synced_txg);
2024EXPORT_SYMBOL(spa_first_txg);
2025EXPORT_SYMBOL(spa_syncing_txg);
2026EXPORT_SYMBOL(spa_version);
2027EXPORT_SYMBOL(spa_state);
2028EXPORT_SYMBOL(spa_load_state);
2029EXPORT_SYMBOL(spa_freeze_txg);
2030EXPORT_SYMBOL(spa_get_asize);
2031EXPORT_SYMBOL(spa_get_dspace);
2032EXPORT_SYMBOL(spa_update_dspace);
2033EXPORT_SYMBOL(spa_deflate);
2034EXPORT_SYMBOL(spa_normal_class);
2035EXPORT_SYMBOL(spa_log_class);
2036EXPORT_SYMBOL(spa_max_replication);
2037EXPORT_SYMBOL(spa_prev_software_version);
2038EXPORT_SYMBOL(spa_get_failmode);
2039EXPORT_SYMBOL(spa_suspended);
2040EXPORT_SYMBOL(spa_bootfs);
2041EXPORT_SYMBOL(spa_delegation);
2042EXPORT_SYMBOL(spa_meta_objset);
2043
2044/* Miscellaneous support routines */
2045EXPORT_SYMBOL(spa_rename);
2046EXPORT_SYMBOL(spa_guid_exists);
2047EXPORT_SYMBOL(spa_strdup);
2048EXPORT_SYMBOL(spa_strfree);
2049EXPORT_SYMBOL(spa_get_random);
2050EXPORT_SYMBOL(spa_generate_guid);
b0bc7a84 2051EXPORT_SYMBOL(snprintf_blkptr);
c28b2279
BB
2052EXPORT_SYMBOL(spa_freeze);
2053EXPORT_SYMBOL(spa_upgrade);
2054EXPORT_SYMBOL(spa_evict_all);
2055EXPORT_SYMBOL(spa_lookup_by_guid);
2056EXPORT_SYMBOL(spa_has_spare);
2057EXPORT_SYMBOL(dva_get_dsize_sync);
2058EXPORT_SYMBOL(bp_get_dsize_sync);
2059EXPORT_SYMBOL(bp_get_dsize);
2060EXPORT_SYMBOL(spa_has_slogs);
2061EXPORT_SYMBOL(spa_is_root);
2062EXPORT_SYMBOL(spa_writeable);
2063EXPORT_SYMBOL(spa_mode);
2064
2065EXPORT_SYMBOL(spa_namespace_lock);
cc92e9d0 2066
33b6dbbc 2067module_param(zfs_flags, uint, 0644);
0b39b9f9
PS
2068MODULE_PARM_DESC(zfs_flags, "Set additional debugging flags");
2069
2070module_param(zfs_recover, int, 0644);
2071MODULE_PARM_DESC(zfs_recover, "Set to attempt to recover from fatal errors");
2072
2073module_param(zfs_free_leak_on_eio, int, 0644);
2074MODULE_PARM_DESC(zfs_free_leak_on_eio,
2075 "Set to ignore IO errors during free and permanently leak the space");
2076
e8b96c60 2077module_param(zfs_deadman_synctime_ms, ulong, 0644);
d1d7e268 2078MODULE_PARM_DESC(zfs_deadman_synctime_ms, "Expiration time in milliseconds");
cc92e9d0
GW
2079
2080module_param(zfs_deadman_enabled, int, 0644);
2081MODULE_PARM_DESC(zfs_deadman_enabled, "Enable deadman timer");
e8b96c60
MA
2082
2083module_param(spa_asize_inflation, int, 0644);
2084MODULE_PARM_DESC(spa_asize_inflation,
d1d7e268 2085 "SPA size estimate multiplication factor");
c28b2279 2086#endif