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