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