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