]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/spa_misc.c
Fix kstat state update during pool transition
[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.
492f64e9 23 * Copyright (c) 2011, 2018 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.
cc99f275 28 * Copyright (c) 2017, Intel Corporation.
34dc7c2f
BB
29 */
30
34dc7c2f
BB
31#include <sys/zfs_context.h>
32#include <sys/spa_impl.h>
33#include <sys/zio.h>
34#include <sys/zio_checksum.h>
35#include <sys/zio_compress.h>
36#include <sys/dmu.h>
37#include <sys/dmu_tx.h>
38#include <sys/zap.h>
39#include <sys/zil.h>
40#include <sys/vdev_impl.h>
619f0976 41#include <sys/vdev_initialize.h>
1b939560 42#include <sys/vdev_trim.h>
bc25c932 43#include <sys/vdev_file.h>
ab9f4b0b 44#include <sys/vdev_raidz.h>
34dc7c2f
BB
45#include <sys/metaslab.h>
46#include <sys/uberblock_impl.h>
47#include <sys/txg.h>
48#include <sys/avl.h>
49#include <sys/unique.h>
50#include <sys/dsl_pool.h>
51#include <sys/dsl_dir.h>
52#include <sys/dsl_prop.h>
26685276 53#include <sys/fm/util.h>
428870ff 54#include <sys/dsl_scan.h>
34dc7c2f
BB
55#include <sys/fs/zfs.h>
56#include <sys/metaslab_impl.h>
b128c09f 57#include <sys/arc.h>
428870ff 58#include <sys/ddt.h>
1421c891 59#include <sys/kstat.h>
34dc7c2f 60#include "zfs_prop.h"
3c67d83a 61#include <sys/zfeature.h>
cf637391 62#include "qat.h"
34dc7c2f
BB
63
64/*
65 * SPA locking
66 *
67 * There are four basic locks for managing spa_t structures:
68 *
69 * spa_namespace_lock (global mutex)
70 *
71 * This lock must be acquired to do any of the following:
72 *
73 * - Lookup a spa_t by name
74 * - Add or remove a spa_t from the namespace
75 * - Increase spa_refcount from non-zero
76 * - Check if spa_refcount is zero
77 * - Rename a spa_t
78 * - add/remove/attach/detach devices
79 * - Held for the duration of create/destroy/import/export
80 *
81 * It does not need to handle recursion. A create or destroy may
82 * reference objects (files or zvols) in other pools, but by
83 * definition they must have an existing reference, and will never need
84 * to lookup a spa_t by name.
85 *
c13060e4 86 * spa_refcount (per-spa zfs_refcount_t protected by mutex)
34dc7c2f
BB
87 *
88 * This reference count keep track of any active users of the spa_t. The
89 * spa_t cannot be destroyed or freed while this is non-zero. Internally,
90 * the refcount is never really 'zero' - opening a pool implicitly keeps
b128c09f 91 * some references in the DMU. Internally we check against spa_minref, but
34dc7c2f
BB
92 * present the image of a zero/non-zero value to consumers.
93 *
b128c09f 94 * spa_config_lock[] (per-spa array of rwlocks)
34dc7c2f
BB
95 *
96 * This protects the spa_t from config changes, and must be held in
97 * the following circumstances:
98 *
99 * - RW_READER to perform I/O to the spa
100 * - RW_WRITER to change the vdev config
101 *
34dc7c2f
BB
102 * The locking order is fairly straightforward:
103 *
104 * spa_namespace_lock -> spa_refcount
105 *
106 * The namespace lock must be acquired to increase the refcount from 0
107 * or to check if it is zero.
108 *
b128c09f 109 * spa_refcount -> spa_config_lock[]
34dc7c2f
BB
110 *
111 * There must be at least one valid reference on the spa_t to acquire
112 * the config lock.
113 *
b128c09f 114 * spa_namespace_lock -> spa_config_lock[]
34dc7c2f
BB
115 *
116 * The namespace lock must always be taken before the config lock.
117 *
118 *
b128c09f 119 * The spa_namespace_lock can be acquired directly and is globally visible.
34dc7c2f 120 *
b128c09f
BB
121 * The namespace is manipulated using the following functions, all of which
122 * require the spa_namespace_lock to be held.
34dc7c2f
BB
123 *
124 * spa_lookup() Lookup a spa_t by name.
125 *
126 * spa_add() Create a new spa_t in the namespace.
127 *
128 * spa_remove() Remove a spa_t from the namespace. This also
129 * frees up any memory associated with the spa_t.
130 *
131 * spa_next() Returns the next spa_t in the system, or the
132 * first if NULL is passed.
133 *
134 * spa_evict_all() Shutdown and remove all spa_t structures in
135 * the system.
136 *
137 * spa_guid_exists() Determine whether a pool/device guid exists.
138 *
139 * The spa_refcount is manipulated using the following functions:
140 *
141 * spa_open_ref() Adds a reference to the given spa_t. Must be
142 * called with spa_namespace_lock held if the
143 * refcount is currently zero.
144 *
145 * spa_close() Remove a reference from the spa_t. This will
146 * not free the spa_t or remove it from the
147 * namespace. No locking is required.
148 *
149 * spa_refcount_zero() Returns true if the refcount is currently
150 * zero. Must be called with spa_namespace_lock
151 * held.
152 *
b128c09f
BB
153 * The spa_config_lock[] is an array of rwlocks, ordered as follows:
154 * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV.
155 * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}().
156 *
157 * To read the configuration, it suffices to hold one of these locks as reader.
158 * To modify the configuration, you must hold all locks as writer. To modify
159 * vdev state without altering the vdev tree's topology (e.g. online/offline),
160 * you must hold SCL_STATE and SCL_ZIO as writer.
161 *
162 * We use these distinct config locks to avoid recursive lock entry.
163 * For example, spa_sync() (which holds SCL_CONFIG as reader) induces
164 * block allocations (SCL_ALLOC), which may require reading space maps
165 * from disk (dmu_read() -> zio_read() -> SCL_ZIO).
166 *
167 * The spa config locks cannot be normal rwlocks because we need the
168 * ability to hand off ownership. For example, SCL_ZIO is acquired
169 * by the issuing thread and later released by an interrupt thread.
170 * They do, however, obey the usual write-wanted semantics to prevent
171 * writer (i.e. system administrator) starvation.
172 *
173 * The lock acquisition rules are as follows:
174 *
175 * SCL_CONFIG
176 * Protects changes to the vdev tree topology, such as vdev
177 * add/remove/attach/detach. Protects the dirty config list
178 * (spa_config_dirty_list) and the set of spares and l2arc devices.
179 *
180 * SCL_STATE
181 * Protects changes to pool state and vdev state, such as vdev
182 * online/offline/fault/degrade/clear. Protects the dirty state list
183 * (spa_state_dirty_list) and global pool state (spa_state).
184 *
185 * SCL_ALLOC
186 * Protects changes to metaslab groups and classes.
187 * Held as reader by metaslab_alloc() and metaslab_claim().
188 *
189 * SCL_ZIO
190 * Held by bp-level zios (those which have no io_vd upon entry)
191 * to prevent changes to the vdev tree. The bp-level zio implicitly
192 * protects all of its vdev child zios, which do not hold SCL_ZIO.
193 *
194 * SCL_FREE
195 * Protects changes to metaslab groups and classes.
196 * Held as reader by metaslab_free(). SCL_FREE is distinct from
197 * SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free
198 * blocks in zio_done() while another i/o that holds either
199 * SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete.
200 *
201 * SCL_VDEV
202 * Held as reader to prevent changes to the vdev tree during trivial
428870ff 203 * inquiries such as bp_get_dsize(). SCL_VDEV is distinct from the
b128c09f
BB
204 * other locks, and lower than all of them, to ensure that it's safe
205 * to acquire regardless of caller context.
206 *
207 * In addition, the following rules apply:
208 *
209 * (a) spa_props_lock protects pool properties, spa_config and spa_config_list.
210 * The lock ordering is SCL_CONFIG > spa_props_lock.
211 *
212 * (b) I/O operations on leaf vdevs. For any zio operation that takes
213 * an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(),
214 * or zio_write_phys() -- the caller must ensure that the config cannot
215 * cannot change in the interim, and that the vdev cannot be reopened.
216 * SCL_STATE as reader suffices for both.
34dc7c2f
BB
217 *
218 * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
219 *
220 * spa_vdev_enter() Acquire the namespace lock and the config lock
221 * for writing.
222 *
223 * spa_vdev_exit() Release the config lock, wait for all I/O
224 * to complete, sync the updated configs to the
225 * cache, and release the namespace lock.
226 *
b128c09f
BB
227 * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit().
228 * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual
229 * locking is, always, based on spa_namespace_lock and spa_config_lock[].
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
a1d477c2
MA
246/*
247 * Everything except dprintf, set_error, spa, and indirect_remap is on
248 * by default in debug builds.
249 */
250int zfs_flags = ~(ZFS_DEBUG_DPRINTF | ZFS_DEBUG_SET_ERROR |
964c2d69 251 ZFS_DEBUG_INDIRECT_REMAP);
0b39b9f9
PS
252#else
253int zfs_flags = 0;
254#endif
255
256/*
257 * zfs_recover can be set to nonzero to attempt to recover from
258 * otherwise-fatal errors, typically caused by on-disk corruption. When
259 * set, calls to zfs_panic_recover() will turn into warning messages.
260 * This should only be used as a last resort, as it typically results
261 * in leaked space, or worse.
262 */
263int zfs_recover = B_FALSE;
264
265/*
266 * If destroy encounters an EIO while reading metadata (e.g. indirect
267 * blocks), space referenced by the missing metadata can not be freed.
268 * Normally this causes the background destroy to become "stalled", as
269 * it is unable to make forward progress. While in this stalled state,
270 * all remaining space to free from the error-encountering filesystem is
271 * "temporarily leaked". Set this flag to cause it to ignore the EIO,
272 * permanently leak the space from indirect blocks that can not be read,
273 * and continue to free everything else that it can.
274 *
275 * The default, "stalling" behavior is useful if the storage partially
276 * fails (i.e. some but not all i/os fail), and then later recovers. In
277 * this case, we will be able to continue pool operations while it is
278 * partially failed, and when it recovers, we can continue to free the
279 * space, with no leaks. However, note that this case is actually
280 * fairly rare.
281 *
282 * Typically pools either (a) fail completely (but perhaps temporarily,
283 * e.g. a top-level vdev going offline), or (b) have localized,
284 * permanent errors (e.g. disk returns the wrong data due to bit flip or
285 * firmware bug). In case (a), this setting does not matter because the
286 * pool will be suspended and the sync thread will not be able to make
287 * forward progress regardless. In case (b), because the error is
288 * permanent, the best we can do is leak the minimum amount of space,
289 * which is what setting this flag will do. Therefore, it is reasonable
290 * for this flag to normally be set, but we chose the more conservative
291 * approach of not setting it, so that there is no possibility of
292 * leaking space in the "partial temporary" failure case.
293 */
294int zfs_free_leak_on_eio = B_FALSE;
295
cc92e9d0 296/*
e8b96c60
MA
297 * Expiration time in milliseconds. This value has two meanings. First it is
298 * used to determine when the spa_deadman() logic should fire. By default the
8fb1ede1 299 * spa_deadman() will fire if spa_sync() has not completed in 600 seconds.
e8b96c60
MA
300 * Secondly, the value determines if an I/O is considered "hung". Any I/O that
301 * has not completed in zfs_deadman_synctime_ms is considered "hung" resulting
8fb1ede1 302 * in one of three behaviors controlled by zfs_deadman_failmode.
cc92e9d0 303 */
8fb1ede1
BB
304unsigned long zfs_deadman_synctime_ms = 600000ULL;
305
306/*
307 * This value controls the maximum amount of time zio_wait() will block for an
308 * outstanding IO. By default this is 300 seconds at which point the "hung"
309 * behavior will be applied as described for zfs_deadman_synctime_ms.
310 */
311unsigned long zfs_deadman_ziotime_ms = 300000ULL;
cc92e9d0 312
b81a3ddc
TC
313/*
314 * Check time in milliseconds. This defines the frequency at which we check
315 * for hung I/O.
316 */
94102578 317unsigned long zfs_deadman_checktime_ms = 60000ULL;
b81a3ddc 318
cc92e9d0
GW
319/*
320 * By default the deadman is enabled.
321 */
322int zfs_deadman_enabled = 1;
323
8fb1ede1
BB
324/*
325 * Controls the behavior of the deadman when it detects a "hung" I/O.
326 * Valid values are zfs_deadman_failmode=<wait|continue|panic>.
327 *
328 * wait - Wait for the "hung" I/O (default)
329 * continue - Attempt to recover from a "hung" I/O
330 * panic - Panic the system
331 */
332char *zfs_deadman_failmode = "wait";
333
e8b96c60
MA
334/*
335 * The worst case is single-sector max-parity RAID-Z blocks, in which
336 * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1)
337 * times the size; so just assume that. Add to this the fact that
338 * we can have up to 3 DVAs per bp, and one more factor of 2 because
339 * the block may be dittoed with up to 3 DVAs by ddt_sync(). All together,
340 * the worst case is:
341 * (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2 == 24
342 */
343int spa_asize_inflation = 24;
344
3d45fdd6
MA
345/*
346 * Normally, we don't allow the last 3.2% (1/(2^spa_slop_shift)) of space in
347 * the pool to be consumed. This ensures that we don't run the pool
348 * completely out of space, due to unaccounted changes (e.g. to the MOS).
349 * It also limits the worst-case time to allocate space. If we have
350 * less than this amount of free space, most ZPL operations (e.g. write,
351 * create) will return ENOSPC.
352 *
353 * Certain operations (e.g. file removal, most administrative actions) can
354 * use half the slop space. They will only return ENOSPC if less than half
355 * the slop space is free. Typically, once the pool has less than the slop
356 * space free, the user will use these operations to free up space in the pool.
357 * These are the operations that call dsl_pool_adjustedsize() with the netfree
358 * argument set to TRUE.
359 *
d2734cce
SD
360 * Operations that are almost guaranteed to free up space in the absence of
361 * a pool checkpoint can use up to three quarters of the slop space
362 * (e.g zfs destroy).
363 *
3d45fdd6
MA
364 * A very restricted set of operations are always permitted, regardless of
365 * the amount of free space. These are the operations that call
d2734cce
SD
366 * dsl_sync_task(ZFS_SPACE_CHECK_NONE). If these operations result in a net
367 * increase in the amount of space used, it is possible to run the pool
368 * completely out of space, causing it to be permanently read-only.
3d45fdd6 369 *
d7958b4c
MA
370 * Note that on very small pools, the slop space will be larger than
371 * 3.2%, in an effort to have it be at least spa_min_slop (128MB),
372 * but we never allow it to be more than half the pool size.
373 *
3d45fdd6
MA
374 * See also the comments in zfs_space_check_t.
375 */
376int spa_slop_shift = 5;
d7958b4c 377uint64_t spa_min_slop = 128 * 1024 * 1024;
492f64e9
PD
378int spa_allocators = 4;
379
3d45fdd6 380
4a0ee12a
PZ
381/*PRINTFLIKE2*/
382void
383spa_load_failed(spa_t *spa, const char *fmt, ...)
384{
385 va_list adx;
386 char buf[256];
387
388 va_start(adx, fmt);
389 (void) vsnprintf(buf, sizeof (buf), fmt, adx);
390 va_end(adx);
391
6cb8e530
PZ
392 zfs_dbgmsg("spa_load(%s, config %s): FAILED: %s", spa->spa_name,
393 spa->spa_trust_config ? "trusted" : "untrusted", buf);
4a0ee12a
PZ
394}
395
396/*PRINTFLIKE2*/
397void
398spa_load_note(spa_t *spa, const char *fmt, ...)
399{
400 va_list adx;
401 char buf[256];
402
403 va_start(adx, fmt);
404 (void) vsnprintf(buf, sizeof (buf), fmt, adx);
405 va_end(adx);
406
6cb8e530
PZ
407 zfs_dbgmsg("spa_load(%s, config %s): %s", spa->spa_name,
408 spa->spa_trust_config ? "trusted" : "untrusted", buf);
4a0ee12a
PZ
409}
410
cc99f275
DB
411/*
412 * By default dedup and user data indirects land in the special class
413 */
414int zfs_ddt_data_is_special = B_TRUE;
415int zfs_user_indirect_is_special = B_TRUE;
416
417/*
418 * The percentage of special class final space reserved for metadata only.
419 * Once we allocate 100 - zfs_special_class_metadata_reserve_pct we only
420 * let metadata into the class.
421 */
422int zfs_special_class_metadata_reserve_pct = 25;
423
34dc7c2f
BB
424/*
425 * ==========================================================================
426 * SPA config locking
427 * ==========================================================================
428 */
429static void
b128c09f
BB
430spa_config_lock_init(spa_t *spa)
431{
1c27024e 432 for (int i = 0; i < SCL_LOCKS; i++) {
b128c09f
BB
433 spa_config_lock_t *scl = &spa->spa_config_lock[i];
434 mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL);
435 cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL);
424fd7c3 436 zfs_refcount_create_untracked(&scl->scl_count);
b128c09f
BB
437 scl->scl_writer = NULL;
438 scl->scl_write_wanted = 0;
439 }
34dc7c2f
BB
440}
441
442static void
b128c09f
BB
443spa_config_lock_destroy(spa_t *spa)
444{
1c27024e 445 for (int i = 0; i < SCL_LOCKS; i++) {
b128c09f
BB
446 spa_config_lock_t *scl = &spa->spa_config_lock[i];
447 mutex_destroy(&scl->scl_lock);
448 cv_destroy(&scl->scl_cv);
424fd7c3 449 zfs_refcount_destroy(&scl->scl_count);
b128c09f
BB
450 ASSERT(scl->scl_writer == NULL);
451 ASSERT(scl->scl_write_wanted == 0);
452 }
453}
454
455int
456spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw)
34dc7c2f 457{
1c27024e 458 for (int i = 0; i < SCL_LOCKS; i++) {
b128c09f
BB
459 spa_config_lock_t *scl = &spa->spa_config_lock[i];
460 if (!(locks & (1 << i)))
461 continue;
462 mutex_enter(&scl->scl_lock);
463 if (rw == RW_READER) {
464 if (scl->scl_writer || scl->scl_write_wanted) {
465 mutex_exit(&scl->scl_lock);
adfe9d93
SK
466 spa_config_exit(spa, locks & ((1 << i) - 1),
467 tag);
b128c09f
BB
468 return (0);
469 }
470 } else {
471 ASSERT(scl->scl_writer != curthread);
424fd7c3 472 if (!zfs_refcount_is_zero(&scl->scl_count)) {
b128c09f 473 mutex_exit(&scl->scl_lock);
adfe9d93
SK
474 spa_config_exit(spa, locks & ((1 << i) - 1),
475 tag);
b128c09f
BB
476 return (0);
477 }
478 scl->scl_writer = curthread;
479 }
c13060e4 480 (void) zfs_refcount_add(&scl->scl_count, tag);
b128c09f
BB
481 mutex_exit(&scl->scl_lock);
482 }
483 return (1);
34dc7c2f
BB
484}
485
486void
b128c09f 487spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw)
34dc7c2f 488{
45d1cae3
BB
489 int wlocks_held = 0;
490
13fe0198
MA
491 ASSERT3U(SCL_LOCKS, <, sizeof (wlocks_held) * NBBY);
492
1c27024e 493 for (int i = 0; i < SCL_LOCKS; i++) {
b128c09f 494 spa_config_lock_t *scl = &spa->spa_config_lock[i];
45d1cae3
BB
495 if (scl->scl_writer == curthread)
496 wlocks_held |= (1 << i);
b128c09f
BB
497 if (!(locks & (1 << i)))
498 continue;
499 mutex_enter(&scl->scl_lock);
500 if (rw == RW_READER) {
501 while (scl->scl_writer || scl->scl_write_wanted) {
502 cv_wait(&scl->scl_cv, &scl->scl_lock);
503 }
504 } else {
505 ASSERT(scl->scl_writer != curthread);
424fd7c3 506 while (!zfs_refcount_is_zero(&scl->scl_count)) {
b128c09f
BB
507 scl->scl_write_wanted++;
508 cv_wait(&scl->scl_cv, &scl->scl_lock);
509 scl->scl_write_wanted--;
510 }
511 scl->scl_writer = curthread;
512 }
c13060e4 513 (void) zfs_refcount_add(&scl->scl_count, tag);
b128c09f 514 mutex_exit(&scl->scl_lock);
34dc7c2f 515 }
a1d477c2 516 ASSERT3U(wlocks_held, <=, locks);
34dc7c2f
BB
517}
518
519void
b128c09f 520spa_config_exit(spa_t *spa, int locks, void *tag)
34dc7c2f 521{
1c27024e 522 for (int i = SCL_LOCKS - 1; i >= 0; i--) {
b128c09f
BB
523 spa_config_lock_t *scl = &spa->spa_config_lock[i];
524 if (!(locks & (1 << i)))
525 continue;
526 mutex_enter(&scl->scl_lock);
424fd7c3
TS
527 ASSERT(!zfs_refcount_is_zero(&scl->scl_count));
528 if (zfs_refcount_remove(&scl->scl_count, tag) == 0) {
b128c09f
BB
529 ASSERT(scl->scl_writer == NULL ||
530 scl->scl_writer == curthread);
531 scl->scl_writer = NULL; /* OK in either case */
532 cv_broadcast(&scl->scl_cv);
533 }
534 mutex_exit(&scl->scl_lock);
34dc7c2f 535 }
34dc7c2f
BB
536}
537
b128c09f
BB
538int
539spa_config_held(spa_t *spa, int locks, krw_t rw)
34dc7c2f 540{
1c27024e 541 int locks_held = 0;
34dc7c2f 542
1c27024e 543 for (int i = 0; i < SCL_LOCKS; i++) {
b128c09f
BB
544 spa_config_lock_t *scl = &spa->spa_config_lock[i];
545 if (!(locks & (1 << i)))
546 continue;
424fd7c3
TS
547 if ((rw == RW_READER &&
548 !zfs_refcount_is_zero(&scl->scl_count)) ||
b128c09f
BB
549 (rw == RW_WRITER && scl->scl_writer == curthread))
550 locks_held |= 1 << i;
551 }
552
553 return (locks_held);
34dc7c2f
BB
554}
555
556/*
557 * ==========================================================================
558 * SPA namespace functions
559 * ==========================================================================
560 */
561
562/*
563 * Lookup the named spa_t in the AVL tree. The spa_namespace_lock must be held.
564 * Returns NULL if no matching spa_t is found.
565 */
566spa_t *
567spa_lookup(const char *name)
568{
b128c09f
BB
569 static spa_t search; /* spa_t is large; don't allocate on stack */
570 spa_t *spa;
34dc7c2f 571 avl_index_t where;
34dc7c2f
BB
572 char *cp;
573
574 ASSERT(MUTEX_HELD(&spa_namespace_lock));
575
13fe0198
MA
576 (void) strlcpy(search.spa_name, name, sizeof (search.spa_name));
577
34dc7c2f
BB
578 /*
579 * If it's a full dataset name, figure out the pool name and
580 * just use that.
581 */
da536844 582 cp = strpbrk(search.spa_name, "/@#");
13fe0198 583 if (cp != NULL)
34dc7c2f 584 *cp = '\0';
34dc7c2f 585
34dc7c2f
BB
586 spa = avl_find(&spa_namespace_avl, &search, &where);
587
34dc7c2f
BB
588 return (spa);
589}
590
cc92e9d0
GW
591/*
592 * Fires when spa_sync has not completed within zfs_deadman_synctime_ms.
593 * If the zfs_deadman_enabled flag is set then it inspects all vdev queues
594 * looking for potentially hung I/Os.
595 */
596void
597spa_deadman(void *arg)
598{
599 spa_t *spa = arg;
600
b81a3ddc
TC
601 /* Disable the deadman if the pool is suspended. */
602 if (spa_suspended(spa))
603 return;
604
cc92e9d0
GW
605 zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu",
606 (gethrtime() - spa->spa_sync_starttime) / NANOSEC,
607 ++spa->spa_deadman_calls);
608 if (zfs_deadman_enabled)
8fb1ede1 609 vdev_deadman(spa->spa_root_vdev, FTAG);
cc92e9d0 610
57ddcda1 611 spa->spa_deadman_tqid = taskq_dispatch_delay(system_delay_taskq,
f764edf0 612 spa_deadman, spa, TQ_SLEEP, ddi_get_lbolt() +
b81a3ddc 613 MSEC_TO_TICK(zfs_deadman_checktime_ms));
cc92e9d0
GW
614}
615
34dc7c2f
BB
616/*
617 * Create an uninitialized spa_t with the given name. Requires
618 * spa_namespace_lock. The caller must ensure that the spa_t doesn't already
619 * exist by calling spa_lookup() first.
620 */
621spa_t *
428870ff 622spa_add(const char *name, nvlist_t *config, const char *altroot)
34dc7c2f
BB
623{
624 spa_t *spa;
b128c09f 625 spa_config_dirent_t *dp;
34dc7c2f
BB
626
627 ASSERT(MUTEX_HELD(&spa_namespace_lock));
628
79c76d5b 629 spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
34dc7c2f 630
34dc7c2f 631 mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL);
34dc7c2f 632 mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL);
428870ff 633 mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL);
0c66c32d 634 mutex_init(&spa->spa_evicting_os_lock, NULL, MUTEX_DEFAULT, NULL);
34dc7c2f 635 mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL);
428870ff 636 mutex_init(&spa->spa_proc_lock, NULL, MUTEX_DEFAULT, NULL);
34dc7c2f 637 mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL);
3c67d83a 638 mutex_init(&spa->spa_cksum_tmpls_lock, NULL, MUTEX_DEFAULT, NULL);
428870ff
BB
639 mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL);
640 mutex_init(&spa->spa_suspend_lock, NULL, MUTEX_DEFAULT, NULL);
641 mutex_init(&spa->spa_vdev_top_lock, NULL, MUTEX_DEFAULT, NULL);
4eb30c68 642 mutex_init(&spa->spa_feat_stats_lock, NULL, MUTEX_DEFAULT, NULL);
34dc7c2f
BB
643
644 cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL);
0c66c32d 645 cv_init(&spa->spa_evicting_os_cv, NULL, CV_DEFAULT, NULL);
428870ff 646 cv_init(&spa->spa_proc_cv, NULL, CV_DEFAULT, NULL);
34dc7c2f 647 cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL);
b128c09f 648 cv_init(&spa->spa_suspend_cv, NULL, CV_DEFAULT, NULL);
34dc7c2f 649
1c27024e 650 for (int t = 0; t < TXG_SIZE; t++)
428870ff
BB
651 bplist_create(&spa->spa_free_bplist[t]);
652
b128c09f 653 (void) strlcpy(spa->spa_name, name, sizeof (spa->spa_name));
34dc7c2f
BB
654 spa->spa_state = POOL_STATE_UNINITIALIZED;
655 spa->spa_freeze_txg = UINT64_MAX;
656 spa->spa_final_txg = UINT64_MAX;
428870ff
BB
657 spa->spa_load_max_txg = UINT64_MAX;
658 spa->spa_proc = &p0;
659 spa->spa_proc_state = SPA_PROC_NONE;
6cb8e530 660 spa->spa_trust_config = B_TRUE;
34dc7c2f 661
e8b96c60 662 spa->spa_deadman_synctime = MSEC2NSEC(zfs_deadman_synctime_ms);
8fb1ede1
BB
663 spa->spa_deadman_ziotime = MSEC2NSEC(zfs_deadman_ziotime_ms);
664 spa_set_deadman_failmode(spa, zfs_deadman_failmode);
cc92e9d0 665
424fd7c3 666 zfs_refcount_create(&spa->spa_refcount);
b128c09f 667 spa_config_lock_init(spa);
1421c891 668 spa_stats_init(spa);
34dc7c2f
BB
669
670 avl_add(&spa_namespace_avl, spa);
671
34dc7c2f
BB
672 /*
673 * Set the alternate root, if there is one.
674 */
0336f3d0 675 if (altroot)
34dc7c2f 676 spa->spa_root = spa_strdup(altroot);
34dc7c2f 677
492f64e9
PD
678 spa->spa_alloc_count = spa_allocators;
679 spa->spa_alloc_locks = kmem_zalloc(spa->spa_alloc_count *
680 sizeof (kmutex_t), KM_SLEEP);
681 spa->spa_alloc_trees = kmem_zalloc(spa->spa_alloc_count *
682 sizeof (avl_tree_t), KM_SLEEP);
683 for (int i = 0; i < spa->spa_alloc_count; i++) {
684 mutex_init(&spa->spa_alloc_locks[i], NULL, MUTEX_DEFAULT, NULL);
685 avl_create(&spa->spa_alloc_trees[i], zio_bookmark_compare,
686 sizeof (zio_t), offsetof(zio_t, io_alloc_node));
687 }
3dfb57a3 688
b128c09f
BB
689 /*
690 * Every pool starts with the default cachefile
691 */
692 list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t),
693 offsetof(spa_config_dirent_t, scd_link));
694
79c76d5b 695 dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP);
428870ff 696 dp->scd_path = altroot ? NULL : spa_strdup(spa_config_path);
b128c09f
BB
697 list_insert_head(&spa->spa_config_list, dp);
698
572e2857 699 VERIFY(nvlist_alloc(&spa->spa_load_info, NV_UNIQUE_NAME,
79c76d5b 700 KM_SLEEP) == 0);
572e2857 701
9ae529ec
CS
702 if (config != NULL) {
703 nvlist_t *features;
704
705 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_FEATURES_FOR_READ,
706 &features) == 0) {
707 VERIFY(nvlist_dup(features, &spa->spa_label_features,
708 0) == 0);
709 }
710
428870ff 711 VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0);
9ae529ec
CS
712 }
713
714 if (spa->spa_label_features == NULL) {
715 VERIFY(nvlist_alloc(&spa->spa_label_features, NV_UNIQUE_NAME,
79c76d5b 716 KM_SLEEP) == 0);
9ae529ec 717 }
428870ff 718
c3520e7f
MA
719 spa->spa_min_ashift = INT_MAX;
720 spa->spa_max_ashift = 0;
721
e8a20144
GN
722 /* Reset cached value */
723 spa->spa_dedup_dspace = ~0ULL;
724
b0bc7a84
MG
725 /*
726 * As a pool is being created, treat all features as disabled by
727 * setting SPA_FEATURE_DISABLED for all entries in the feature
728 * refcount cache.
729 */
1c27024e 730 for (int i = 0; i < SPA_FEATURES; i++) {
b0bc7a84
MG
731 spa->spa_feat_refcount_cache[i] = SPA_FEATURE_DISABLED;
732 }
733
3d31aad8
OF
734 list_create(&spa->spa_leaf_list, sizeof (vdev_t),
735 offsetof(vdev_t, vdev_leaf_node));
736
34dc7c2f
BB
737 return (spa);
738}
739
740/*
741 * Removes a spa_t from the namespace, freeing up any memory used. Requires
742 * spa_namespace_lock. This is called only after the spa_t has been closed and
743 * deactivated.
744 */
745void
746spa_remove(spa_t *spa)
747{
b128c09f
BB
748 spa_config_dirent_t *dp;
749
34dc7c2f
BB
750 ASSERT(MUTEX_HELD(&spa_namespace_lock));
751 ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
424fd7c3 752 ASSERT3U(zfs_refcount_count(&spa->spa_refcount), ==, 0);
34dc7c2f 753
428870ff
BB
754 nvlist_free(spa->spa_config_splitting);
755
34dc7c2f
BB
756 avl_remove(&spa_namespace_avl, spa);
757 cv_broadcast(&spa_namespace_cv);
758
0336f3d0 759 if (spa->spa_root)
34dc7c2f 760 spa_strfree(spa->spa_root);
34dc7c2f 761
b128c09f
BB
762 while ((dp = list_head(&spa->spa_config_list)) != NULL) {
763 list_remove(&spa->spa_config_list, dp);
764 if (dp->scd_path != NULL)
765 spa_strfree(dp->scd_path);
766 kmem_free(dp, sizeof (spa_config_dirent_t));
767 }
34dc7c2f 768
492f64e9
PD
769 for (int i = 0; i < spa->spa_alloc_count; i++) {
770 avl_destroy(&spa->spa_alloc_trees[i]);
771 mutex_destroy(&spa->spa_alloc_locks[i]);
772 }
773 kmem_free(spa->spa_alloc_locks, spa->spa_alloc_count *
774 sizeof (kmutex_t));
775 kmem_free(spa->spa_alloc_trees, spa->spa_alloc_count *
776 sizeof (avl_tree_t));
777
b128c09f 778 list_destroy(&spa->spa_config_list);
3d31aad8 779 list_destroy(&spa->spa_leaf_list);
34dc7c2f 780
9ae529ec 781 nvlist_free(spa->spa_label_features);
572e2857 782 nvlist_free(spa->spa_load_info);
417104bd 783 nvlist_free(spa->spa_feat_stats);
34dc7c2f
BB
784 spa_config_set(spa, NULL);
785
424fd7c3 786 zfs_refcount_destroy(&spa->spa_refcount);
34dc7c2f 787
1421c891 788 spa_stats_destroy(spa);
b128c09f 789 spa_config_lock_destroy(spa);
34dc7c2f 790
1c27024e 791 for (int t = 0; t < TXG_SIZE; t++)
428870ff
BB
792 bplist_destroy(&spa->spa_free_bplist[t]);
793
3c67d83a
TH
794 zio_checksum_templates_free(spa);
795
34dc7c2f 796 cv_destroy(&spa->spa_async_cv);
0c66c32d 797 cv_destroy(&spa->spa_evicting_os_cv);
428870ff 798 cv_destroy(&spa->spa_proc_cv);
34dc7c2f 799 cv_destroy(&spa->spa_scrub_io_cv);
b128c09f 800 cv_destroy(&spa->spa_suspend_cv);
34dc7c2f 801
34dc7c2f 802 mutex_destroy(&spa->spa_async_lock);
34dc7c2f 803 mutex_destroy(&spa->spa_errlist_lock);
428870ff 804 mutex_destroy(&spa->spa_errlog_lock);
0c66c32d 805 mutex_destroy(&spa->spa_evicting_os_lock);
34dc7c2f 806 mutex_destroy(&spa->spa_history_lock);
428870ff 807 mutex_destroy(&spa->spa_proc_lock);
34dc7c2f 808 mutex_destroy(&spa->spa_props_lock);
3c67d83a 809 mutex_destroy(&spa->spa_cksum_tmpls_lock);
428870ff 810 mutex_destroy(&spa->spa_scrub_lock);
b128c09f 811 mutex_destroy(&spa->spa_suspend_lock);
428870ff 812 mutex_destroy(&spa->spa_vdev_top_lock);
4eb30c68 813 mutex_destroy(&spa->spa_feat_stats_lock);
34dc7c2f
BB
814
815 kmem_free(spa, sizeof (spa_t));
816}
817
818/*
819 * Given a pool, return the next pool in the namespace, or NULL if there is
820 * none. If 'prev' is NULL, return the first pool.
821 */
822spa_t *
823spa_next(spa_t *prev)
824{
825 ASSERT(MUTEX_HELD(&spa_namespace_lock));
826
827 if (prev)
828 return (AVL_NEXT(&spa_namespace_avl, prev));
829 else
830 return (avl_first(&spa_namespace_avl));
831}
832
833/*
834 * ==========================================================================
835 * SPA refcount functions
836 * ==========================================================================
837 */
838
839/*
840 * Add a reference to the given spa_t. Must have at least one reference, or
841 * have the namespace lock held.
842 */
843void
844spa_open_ref(spa_t *spa, void *tag)
845{
424fd7c3 846 ASSERT(zfs_refcount_count(&spa->spa_refcount) >= spa->spa_minref ||
34dc7c2f 847 MUTEX_HELD(&spa_namespace_lock));
c13060e4 848 (void) zfs_refcount_add(&spa->spa_refcount, tag);
34dc7c2f
BB
849}
850
851/*
852 * Remove a reference to the given spa_t. Must have at least one reference, or
853 * have the namespace lock held.
854 */
855void
856spa_close(spa_t *spa, void *tag)
857{
424fd7c3 858 ASSERT(zfs_refcount_count(&spa->spa_refcount) > spa->spa_minref ||
34dc7c2f 859 MUTEX_HELD(&spa_namespace_lock));
424fd7c3 860 (void) zfs_refcount_remove(&spa->spa_refcount, tag);
34dc7c2f
BB
861}
862
0c66c32d
JG
863/*
864 * Remove a reference to the given spa_t held by a dsl dir that is
865 * being asynchronously released. Async releases occur from a taskq
866 * performing eviction of dsl datasets and dirs. The namespace lock
867 * isn't held and the hold by the object being evicted may contribute to
868 * spa_minref (e.g. dataset or directory released during pool export),
869 * so the asserts in spa_close() do not apply.
870 */
871void
872spa_async_close(spa_t *spa, void *tag)
873{
424fd7c3 874 (void) zfs_refcount_remove(&spa->spa_refcount, tag);
0c66c32d
JG
875}
876
34dc7c2f
BB
877/*
878 * Check to see if the spa refcount is zero. Must be called with
b128c09f 879 * spa_namespace_lock held. We really compare against spa_minref, which is the
34dc7c2f
BB
880 * number of references acquired when opening a pool
881 */
882boolean_t
883spa_refcount_zero(spa_t *spa)
884{
885 ASSERT(MUTEX_HELD(&spa_namespace_lock));
886
424fd7c3 887 return (zfs_refcount_count(&spa->spa_refcount) == spa->spa_minref);
34dc7c2f
BB
888}
889
890/*
891 * ==========================================================================
892 * SPA spare and l2cache tracking
893 * ==========================================================================
894 */
895
896/*
897 * Hot spares and cache devices are tracked using the same code below,
898 * for 'auxiliary' devices.
899 */
900
901typedef struct spa_aux {
902 uint64_t aux_guid;
903 uint64_t aux_pool;
904 avl_node_t aux_avl;
905 int aux_count;
906} spa_aux_t;
907
ee36c709 908static inline int
34dc7c2f
BB
909spa_aux_compare(const void *a, const void *b)
910{
ee36c709
GN
911 const spa_aux_t *sa = (const spa_aux_t *)a;
912 const spa_aux_t *sb = (const spa_aux_t *)b;
34dc7c2f 913
ee36c709 914 return (AVL_CMP(sa->aux_guid, sb->aux_guid));
34dc7c2f
BB
915}
916
917void
918spa_aux_add(vdev_t *vd, avl_tree_t *avl)
919{
920 avl_index_t where;
921 spa_aux_t search;
922 spa_aux_t *aux;
923
924 search.aux_guid = vd->vdev_guid;
925 if ((aux = avl_find(avl, &search, &where)) != NULL) {
926 aux->aux_count++;
927 } else {
79c76d5b 928 aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP);
34dc7c2f
BB
929 aux->aux_guid = vd->vdev_guid;
930 aux->aux_count = 1;
931 avl_insert(avl, aux, where);
932 }
933}
934
935void
936spa_aux_remove(vdev_t *vd, avl_tree_t *avl)
937{
938 spa_aux_t search;
939 spa_aux_t *aux;
940 avl_index_t where;
941
942 search.aux_guid = vd->vdev_guid;
943 aux = avl_find(avl, &search, &where);
944
945 ASSERT(aux != NULL);
946
947 if (--aux->aux_count == 0) {
948 avl_remove(avl, aux);
949 kmem_free(aux, sizeof (spa_aux_t));
950 } else if (aux->aux_pool == spa_guid(vd->vdev_spa)) {
951 aux->aux_pool = 0ULL;
952 }
953}
954
955boolean_t
b128c09f 956spa_aux_exists(uint64_t guid, uint64_t *pool, int *refcnt, avl_tree_t *avl)
34dc7c2f
BB
957{
958 spa_aux_t search, *found;
34dc7c2f
BB
959
960 search.aux_guid = guid;
b128c09f 961 found = avl_find(avl, &search, NULL);
34dc7c2f
BB
962
963 if (pool) {
964 if (found)
965 *pool = found->aux_pool;
966 else
967 *pool = 0ULL;
968 }
969
b128c09f
BB
970 if (refcnt) {
971 if (found)
972 *refcnt = found->aux_count;
973 else
974 *refcnt = 0;
975 }
976
34dc7c2f
BB
977 return (found != NULL);
978}
979
980void
981spa_aux_activate(vdev_t *vd, avl_tree_t *avl)
982{
983 spa_aux_t search, *found;
984 avl_index_t where;
985
986 search.aux_guid = vd->vdev_guid;
987 found = avl_find(avl, &search, &where);
988 ASSERT(found != NULL);
989 ASSERT(found->aux_pool == 0ULL);
990
991 found->aux_pool = spa_guid(vd->vdev_spa);
992}
993
994/*
995 * Spares are tracked globally due to the following constraints:
996 *
997 * - A spare may be part of multiple pools.
998 * - A spare may be added to a pool even if it's actively in use within
999 * another pool.
1000 * - A spare in use in any pool can only be the source of a replacement if
1001 * the target is a spare in the same pool.
1002 *
1003 * We keep track of all spares on the system through the use of a reference
1004 * counted AVL tree. When a vdev is added as a spare, or used as a replacement
1005 * spare, then we bump the reference count in the AVL tree. In addition, we set
1006 * the 'vdev_isspare' member to indicate that the device is a spare (active or
1007 * inactive). When a spare is made active (used to replace a device in the
1008 * pool), we also keep track of which pool its been made a part of.
1009 *
1010 * The 'spa_spare_lock' protects the AVL tree. These functions are normally
1011 * called under the spa_namespace lock as part of vdev reconfiguration. The
1012 * separate spare lock exists for the status query path, which does not need to
1013 * be completely consistent with respect to other vdev configuration changes.
1014 */
1015
1016static int
1017spa_spare_compare(const void *a, const void *b)
1018{
1019 return (spa_aux_compare(a, b));
1020}
1021
1022void
1023spa_spare_add(vdev_t *vd)
1024{
1025 mutex_enter(&spa_spare_lock);
1026 ASSERT(!vd->vdev_isspare);
1027 spa_aux_add(vd, &spa_spare_avl);
1028 vd->vdev_isspare = B_TRUE;
1029 mutex_exit(&spa_spare_lock);
1030}
1031
1032void
1033spa_spare_remove(vdev_t *vd)
1034{
1035 mutex_enter(&spa_spare_lock);
1036 ASSERT(vd->vdev_isspare);
1037 spa_aux_remove(vd, &spa_spare_avl);
1038 vd->vdev_isspare = B_FALSE;
1039 mutex_exit(&spa_spare_lock);
1040}
1041
1042boolean_t
b128c09f 1043spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt)
34dc7c2f
BB
1044{
1045 boolean_t found;
1046
1047 mutex_enter(&spa_spare_lock);
b128c09f 1048 found = spa_aux_exists(guid, pool, refcnt, &spa_spare_avl);
34dc7c2f
BB
1049 mutex_exit(&spa_spare_lock);
1050
1051 return (found);
1052}
1053
1054void
1055spa_spare_activate(vdev_t *vd)
1056{
1057 mutex_enter(&spa_spare_lock);
1058 ASSERT(vd->vdev_isspare);
1059 spa_aux_activate(vd, &spa_spare_avl);
1060 mutex_exit(&spa_spare_lock);
1061}
1062
1063/*
1064 * Level 2 ARC devices are tracked globally for the same reasons as spares.
1065 * Cache devices currently only support one pool per cache device, and so
1066 * for these devices the aux reference count is currently unused beyond 1.
1067 */
1068
1069static int
1070spa_l2cache_compare(const void *a, const void *b)
1071{
1072 return (spa_aux_compare(a, b));
1073}
1074
1075void
1076spa_l2cache_add(vdev_t *vd)
1077{
1078 mutex_enter(&spa_l2cache_lock);
1079 ASSERT(!vd->vdev_isl2cache);
1080 spa_aux_add(vd, &spa_l2cache_avl);
1081 vd->vdev_isl2cache = B_TRUE;
1082 mutex_exit(&spa_l2cache_lock);
1083}
1084
1085void
1086spa_l2cache_remove(vdev_t *vd)
1087{
1088 mutex_enter(&spa_l2cache_lock);
1089 ASSERT(vd->vdev_isl2cache);
1090 spa_aux_remove(vd, &spa_l2cache_avl);
1091 vd->vdev_isl2cache = B_FALSE;
1092 mutex_exit(&spa_l2cache_lock);
1093}
1094
1095boolean_t
1096spa_l2cache_exists(uint64_t guid, uint64_t *pool)
1097{
1098 boolean_t found;
1099
1100 mutex_enter(&spa_l2cache_lock);
b128c09f 1101 found = spa_aux_exists(guid, pool, NULL, &spa_l2cache_avl);
34dc7c2f
BB
1102 mutex_exit(&spa_l2cache_lock);
1103
1104 return (found);
1105}
1106
1107void
1108spa_l2cache_activate(vdev_t *vd)
1109{
1110 mutex_enter(&spa_l2cache_lock);
1111 ASSERT(vd->vdev_isl2cache);
1112 spa_aux_activate(vd, &spa_l2cache_avl);
1113 mutex_exit(&spa_l2cache_lock);
1114}
1115
34dc7c2f
BB
1116/*
1117 * ==========================================================================
1118 * SPA vdev locking
1119 * ==========================================================================
1120 */
1121
1122/*
1123 * Lock the given spa_t for the purpose of adding or removing a vdev.
1124 * Grabs the global spa_namespace_lock plus the spa config lock for writing.
1125 * It returns the next transaction group for the spa_t.
1126 */
1127uint64_t
1128spa_vdev_enter(spa_t *spa)
1129{
428870ff 1130 mutex_enter(&spa->spa_vdev_top_lock);
34dc7c2f 1131 mutex_enter(&spa_namespace_lock);
1b939560
BB
1132
1133 vdev_autotrim_stop_all(spa);
1134
428870ff
BB
1135 return (spa_vdev_config_enter(spa));
1136}
1137
1138/*
1139 * Internal implementation for spa_vdev_enter(). Used when a vdev
1140 * operation requires multiple syncs (i.e. removing a device) while
1141 * keeping the spa_namespace_lock held.
1142 */
1143uint64_t
1144spa_vdev_config_enter(spa_t *spa)
1145{
1146 ASSERT(MUTEX_HELD(&spa_namespace_lock));
34dc7c2f 1147
b128c09f 1148 spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
34dc7c2f
BB
1149
1150 return (spa_last_synced_txg(spa) + 1);
1151}
1152
1153/*
428870ff
BB
1154 * Used in combination with spa_vdev_config_enter() to allow the syncing
1155 * of multiple transactions without releasing the spa_namespace_lock.
34dc7c2f 1156 */
428870ff
BB
1157void
1158spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error, char *tag)
34dc7c2f 1159{
1c27024e
DB
1160 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1161
34dc7c2f
BB
1162 int config_changed = B_FALSE;
1163
1164 ASSERT(txg > spa_last_synced_txg(spa));
1165
b128c09f
BB
1166 spa->spa_pending_vdev = NULL;
1167
34dc7c2f
BB
1168 /*
1169 * Reassess the DTLs.
1170 */
1171 vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE);
1172
b128c09f 1173 if (error == 0 && !list_is_empty(&spa->spa_config_dirty_list)) {
34dc7c2f 1174 config_changed = B_TRUE;
428870ff 1175 spa->spa_config_generation++;
34dc7c2f
BB
1176 }
1177
428870ff
BB
1178 /*
1179 * Verify the metaslab classes.
1180 */
1181 ASSERT(metaslab_class_validate(spa_normal_class(spa)) == 0);
1182 ASSERT(metaslab_class_validate(spa_log_class(spa)) == 0);
cc99f275
DB
1183 ASSERT(metaslab_class_validate(spa_special_class(spa)) == 0);
1184 ASSERT(metaslab_class_validate(spa_dedup_class(spa)) == 0);
428870ff 1185
b128c09f 1186 spa_config_exit(spa, SCL_ALL, spa);
34dc7c2f 1187
428870ff
BB
1188 /*
1189 * Panic the system if the specified tag requires it. This
1190 * is useful for ensuring that configurations are updated
1191 * transactionally.
1192 */
1193 if (zio_injection_enabled)
1194 zio_handle_panic_injection(spa, tag, 0);
1195
34dc7c2f
BB
1196 /*
1197 * Note: this txg_wait_synced() is important because it ensures
1198 * that there won't be more than one config change per txg.
1199 * This allows us to use the txg as the generation number.
1200 */
1201 if (error == 0)
1202 txg_wait_synced(spa->spa_dsl_pool, txg);
1203
1204 if (vd != NULL) {
93cf2076 1205 ASSERT(!vd->vdev_detached || vd->vdev_dtl_sm == NULL);
619f0976
GW
1206 if (vd->vdev_ops->vdev_op_leaf) {
1207 mutex_enter(&vd->vdev_initialize_lock);
c10d37dd
GW
1208 vdev_initialize_stop(vd, VDEV_INITIALIZE_CANCELED,
1209 NULL);
619f0976 1210 mutex_exit(&vd->vdev_initialize_lock);
1b939560
BB
1211
1212 mutex_enter(&vd->vdev_trim_lock);
1213 vdev_trim_stop(vd, VDEV_TRIM_CANCELED, NULL);
1214 mutex_exit(&vd->vdev_trim_lock);
619f0976
GW
1215 }
1216
1b939560
BB
1217 /*
1218 * The vdev may be both a leaf and top-level device.
1219 */
1220 vdev_autotrim_stop_wait(vd);
1221
fb5f0bc8 1222 spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
34dc7c2f 1223 vdev_free(vd);
fb5f0bc8 1224 spa_config_exit(spa, SCL_ALL, spa);
34dc7c2f
BB
1225 }
1226
1227 /*
1228 * If the config changed, update the config cache.
1229 */
1230 if (config_changed)
a1d477c2 1231 spa_write_cachefile(spa, B_FALSE, B_TRUE);
428870ff 1232}
34dc7c2f 1233
428870ff
BB
1234/*
1235 * Unlock the spa_t after adding or removing a vdev. Besides undoing the
1236 * locking of spa_vdev_enter(), we also want make sure the transactions have
1237 * synced to disk, and then update the global configuration cache with the new
1238 * information.
1239 */
1240int
1241spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
1242{
1b939560
BB
1243 vdev_autotrim_restart(spa);
1244
428870ff 1245 spa_vdev_config_exit(spa, vd, txg, error, FTAG);
34dc7c2f 1246 mutex_exit(&spa_namespace_lock);
428870ff 1247 mutex_exit(&spa->spa_vdev_top_lock);
34dc7c2f
BB
1248
1249 return (error);
1250}
1251
b128c09f
BB
1252/*
1253 * Lock the given spa_t for the purpose of changing vdev state.
1254 */
1255void
428870ff 1256spa_vdev_state_enter(spa_t *spa, int oplocks)
b128c09f 1257{
428870ff
BB
1258 int locks = SCL_STATE_ALL | oplocks;
1259
1260 /*
1261 * Root pools may need to read of the underlying devfs filesystem
1262 * when opening up a vdev. Unfortunately if we're holding the
1263 * SCL_ZIO lock it will result in a deadlock when we try to issue
1264 * the read from the root filesystem. Instead we "prefetch"
1265 * the associated vnodes that we need prior to opening the
1266 * underlying devices and cache them so that we can prevent
1267 * any I/O when we are doing the actual open.
1268 */
1269 if (spa_is_root(spa)) {
1270 int low = locks & ~(SCL_ZIO - 1);
1271 int high = locks & ~low;
1272
1273 spa_config_enter(spa, high, spa, RW_WRITER);
1274 vdev_hold(spa->spa_root_vdev);
1275 spa_config_enter(spa, low, spa, RW_WRITER);
1276 } else {
1277 spa_config_enter(spa, locks, spa, RW_WRITER);
1278 }
1279 spa->spa_vdev_locks = locks;
b128c09f
BB
1280}
1281
1282int
1283spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error)
1284{
428870ff 1285 boolean_t config_changed = B_FALSE;
4a283c7f
TH
1286 vdev_t *vdev_top;
1287
1288 if (vd == NULL || vd == spa->spa_root_vdev) {
1289 vdev_top = spa->spa_root_vdev;
1290 } else {
1291 vdev_top = vd->vdev_top;
1292 }
428870ff
BB
1293
1294 if (vd != NULL || error == 0)
4a283c7f 1295 vdev_dtl_reassess(vdev_top, 0, 0, B_FALSE);
428870ff
BB
1296
1297 if (vd != NULL) {
4a283c7f
TH
1298 if (vd != spa->spa_root_vdev)
1299 vdev_state_dirty(vdev_top);
1300
428870ff
BB
1301 config_changed = B_TRUE;
1302 spa->spa_config_generation++;
1303 }
b128c09f 1304
428870ff
BB
1305 if (spa_is_root(spa))
1306 vdev_rele(spa->spa_root_vdev);
1307
1308 ASSERT3U(spa->spa_vdev_locks, >=, SCL_STATE_ALL);
1309 spa_config_exit(spa, spa->spa_vdev_locks, spa);
b128c09f 1310
fb5f0bc8
BB
1311 /*
1312 * If anything changed, wait for it to sync. This ensures that,
1313 * from the system administrator's perspective, zpool(1M) commands
1314 * are synchronous. This is important for things like zpool offline:
1315 * when the command completes, you expect no further I/O from ZFS.
1316 */
1317 if (vd != NULL)
1318 txg_wait_synced(spa->spa_dsl_pool, 0);
1319
428870ff
BB
1320 /*
1321 * If the config changed, update the config cache.
1322 */
1323 if (config_changed) {
1324 mutex_enter(&spa_namespace_lock);
a1d477c2 1325 spa_write_cachefile(spa, B_FALSE, B_TRUE);
428870ff
BB
1326 mutex_exit(&spa_namespace_lock);
1327 }
1328
b128c09f
BB
1329 return (error);
1330}
1331
34dc7c2f
BB
1332/*
1333 * ==========================================================================
1334 * Miscellaneous functions
1335 * ==========================================================================
1336 */
1337
9ae529ec 1338void
b0bc7a84 1339spa_activate_mos_feature(spa_t *spa, const char *feature, dmu_tx_t *tx)
9ae529ec 1340{
fa86b5db
MA
1341 if (!nvlist_exists(spa->spa_label_features, feature)) {
1342 fnvlist_add_boolean(spa->spa_label_features, feature);
b0bc7a84
MG
1343 /*
1344 * When we are creating the pool (tx_txg==TXG_INITIAL), we can't
1345 * dirty the vdev config because lock SCL_CONFIG is not held.
1346 * Thankfully, in this case we don't need to dirty the config
1347 * because it will be written out anyway when we finish
1348 * creating the pool.
1349 */
1350 if (tx->tx_txg != TXG_INITIAL)
1351 vdev_config_dirty(spa->spa_root_vdev);
fa86b5db 1352 }
9ae529ec
CS
1353}
1354
1355void
1356spa_deactivate_mos_feature(spa_t *spa, const char *feature)
1357{
fa86b5db
MA
1358 if (nvlist_remove_all(spa->spa_label_features, feature) == 0)
1359 vdev_config_dirty(spa->spa_root_vdev);
9ae529ec
CS
1360}
1361
34dc7c2f 1362/*
572e2857
BB
1363 * Return the spa_t associated with given pool_guid, if it exists. If
1364 * device_guid is non-zero, determine whether the pool exists *and* contains
1365 * a device with the specified device_guid.
34dc7c2f 1366 */
572e2857
BB
1367spa_t *
1368spa_by_guid(uint64_t pool_guid, uint64_t device_guid)
34dc7c2f
BB
1369{
1370 spa_t *spa;
1371 avl_tree_t *t = &spa_namespace_avl;
1372
1373 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1374
1375 for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) {
1376 if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1377 continue;
1378 if (spa->spa_root_vdev == NULL)
1379 continue;
1380 if (spa_guid(spa) == pool_guid) {
1381 if (device_guid == 0)
1382 break;
1383
1384 if (vdev_lookup_by_guid(spa->spa_root_vdev,
1385 device_guid) != NULL)
1386 break;
1387
1388 /*
1389 * Check any devices we may be in the process of adding.
1390 */
1391 if (spa->spa_pending_vdev) {
1392 if (vdev_lookup_by_guid(spa->spa_pending_vdev,
1393 device_guid) != NULL)
1394 break;
1395 }
1396 }
1397 }
1398
572e2857
BB
1399 return (spa);
1400}
1401
1402/*
1403 * Determine whether a pool with the given pool_guid exists.
1404 */
1405boolean_t
1406spa_guid_exists(uint64_t pool_guid, uint64_t device_guid)
1407{
1408 return (spa_by_guid(pool_guid, device_guid) != NULL);
34dc7c2f
BB
1409}
1410
1411char *
1412spa_strdup(const char *s)
1413{
1414 size_t len;
1415 char *new;
1416
1417 len = strlen(s);
79c76d5b 1418 new = kmem_alloc(len + 1, KM_SLEEP);
34dc7c2f
BB
1419 bcopy(s, new, len);
1420 new[len] = '\0';
1421
1422 return (new);
1423}
1424
1425void
1426spa_strfree(char *s)
1427{
1428 kmem_free(s, strlen(s) + 1);
1429}
1430
1431uint64_t
1432spa_get_random(uint64_t range)
1433{
1434 uint64_t r;
1435
1436 ASSERT(range != 0);
1437
379ca9cf
OF
1438 if (range == 1)
1439 return (0);
1440
34dc7c2f
BB
1441 (void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t));
1442
1443 return (r % range);
1444}
1445
428870ff
BB
1446uint64_t
1447spa_generate_guid(spa_t *spa)
34dc7c2f 1448{
428870ff 1449 uint64_t guid = spa_get_random(-1ULL);
34dc7c2f 1450
428870ff
BB
1451 if (spa != NULL) {
1452 while (guid == 0 || spa_guid_exists(spa_guid(spa), guid))
1453 guid = spa_get_random(-1ULL);
1454 } else {
1455 while (guid == 0 || spa_guid_exists(guid, 0))
1456 guid = spa_get_random(-1ULL);
34dc7c2f
BB
1457 }
1458
428870ff
BB
1459 return (guid);
1460}
1461
1462void
b0bc7a84 1463snprintf_blkptr(char *buf, size_t buflen, const blkptr_t *bp)
428870ff 1464{
9ae529ec 1465 char type[256];
428870ff
BB
1466 char *checksum = NULL;
1467 char *compress = NULL;
34dc7c2f 1468
428870ff 1469 if (bp != NULL) {
9ae529ec
CS
1470 if (BP_GET_TYPE(bp) & DMU_OT_NEWTYPE) {
1471 dmu_object_byteswap_t bswap =
1472 DMU_OT_BYTESWAP(BP_GET_TYPE(bp));
1473 (void) snprintf(type, sizeof (type), "bswap %s %s",
1474 DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) ?
1475 "metadata" : "data",
1476 dmu_ot_byteswap[bswap].ob_name);
1477 } else {
1478 (void) strlcpy(type, dmu_ot[BP_GET_TYPE(bp)].ot_name,
1479 sizeof (type));
1480 }
9b67f605
MA
1481 if (!BP_IS_EMBEDDED(bp)) {
1482 checksum =
1483 zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name;
1484 }
428870ff 1485 compress = zio_compress_table[BP_GET_COMPRESS(bp)].ci_name;
34dc7c2f
BB
1486 }
1487
b0bc7a84 1488 SNPRINTF_BLKPTR(snprintf, ' ', buf, buflen, bp, type, checksum,
5c27ec10 1489 compress);
34dc7c2f
BB
1490}
1491
1492void
1493spa_freeze(spa_t *spa)
1494{
1495 uint64_t freeze_txg = 0;
1496
b128c09f 1497 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34dc7c2f
BB
1498 if (spa->spa_freeze_txg == UINT64_MAX) {
1499 freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE;
1500 spa->spa_freeze_txg = freeze_txg;
1501 }
b128c09f 1502 spa_config_exit(spa, SCL_ALL, FTAG);
34dc7c2f
BB
1503 if (freeze_txg != 0)
1504 txg_wait_synced(spa_get_dsl(spa), freeze_txg);
1505}
1506
0b39b9f9
PS
1507void
1508zfs_panic_recover(const char *fmt, ...)
1509{
1510 va_list adx;
1511
1512 va_start(adx, fmt);
1513 vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx);
1514 va_end(adx);
1515}
1516
428870ff
BB
1517/*
1518 * This is a stripped-down version of strtoull, suitable only for converting
d3cc8b15 1519 * lowercase hexadecimal numbers that don't overflow.
428870ff
BB
1520 */
1521uint64_t
e19572e4 1522zfs_strtonum(const char *str, char **nptr)
428870ff
BB
1523{
1524 uint64_t val = 0;
1525 char c;
1526 int digit;
1527
1528 while ((c = *str) != '\0') {
1529 if (c >= '0' && c <= '9')
1530 digit = c - '0';
1531 else if (c >= 'a' && c <= 'f')
1532 digit = 10 + c - 'a';
1533 else
1534 break;
1535
1536 val *= 16;
1537 val += digit;
1538
1539 str++;
1540 }
1541
1542 if (nptr)
1543 *nptr = (char *)str;
1544
1545 return (val);
1546}
1547
cc99f275
DB
1548void
1549spa_activate_allocation_classes(spa_t *spa, dmu_tx_t *tx)
1550{
1551 /*
1552 * We bump the feature refcount for each special vdev added to the pool
1553 */
1554 ASSERT(spa_feature_is_enabled(spa, SPA_FEATURE_ALLOCATION_CLASSES));
1555 spa_feature_incr(spa, SPA_FEATURE_ALLOCATION_CLASSES, tx);
1556}
1557
34dc7c2f
BB
1558/*
1559 * ==========================================================================
1560 * Accessor functions
1561 * ==========================================================================
1562 */
1563
b128c09f
BB
1564boolean_t
1565spa_shutting_down(spa_t *spa)
34dc7c2f 1566{
b128c09f 1567 return (spa->spa_async_suspended);
34dc7c2f
BB
1568}
1569
1570dsl_pool_t *
1571spa_get_dsl(spa_t *spa)
1572{
1573 return (spa->spa_dsl_pool);
1574}
1575
9ae529ec
CS
1576boolean_t
1577spa_is_initializing(spa_t *spa)
1578{
1579 return (spa->spa_is_initializing);
1580}
1581
a1d477c2
MA
1582boolean_t
1583spa_indirect_vdevs_loaded(spa_t *spa)
1584{
1585 return (spa->spa_indirect_vdevs_loaded);
1586}
1587
34dc7c2f
BB
1588blkptr_t *
1589spa_get_rootblkptr(spa_t *spa)
1590{
1591 return (&spa->spa_ubsync.ub_rootbp);
1592}
1593
1594void
1595spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp)
1596{
1597 spa->spa_uberblock.ub_rootbp = *bp;
1598}
1599
1600void
1601spa_altroot(spa_t *spa, char *buf, size_t buflen)
1602{
1603 if (spa->spa_root == NULL)
1604 buf[0] = '\0';
1605 else
1606 (void) strncpy(buf, spa->spa_root, buflen);
1607}
1608
1609int
1610spa_sync_pass(spa_t *spa)
1611{
1612 return (spa->spa_sync_pass);
1613}
1614
1615char *
1616spa_name(spa_t *spa)
1617{
34dc7c2f
BB
1618 return (spa->spa_name);
1619}
1620
1621uint64_t
1622spa_guid(spa_t *spa)
1623{
3bc7e0fb
GW
1624 dsl_pool_t *dp = spa_get_dsl(spa);
1625 uint64_t guid;
1626
34dc7c2f
BB
1627 /*
1628 * If we fail to parse the config during spa_load(), we can go through
1629 * the error path (which posts an ereport) and end up here with no root
3541dc6d 1630 * vdev. We stash the original pool guid in 'spa_config_guid' to handle
34dc7c2f
BB
1631 * this case.
1632 */
3bc7e0fb
GW
1633 if (spa->spa_root_vdev == NULL)
1634 return (spa->spa_config_guid);
1635
1636 guid = spa->spa_last_synced_guid != 0 ?
1637 spa->spa_last_synced_guid : spa->spa_root_vdev->vdev_guid;
1638
1639 /*
1640 * Return the most recently synced out guid unless we're
1641 * in syncing context.
1642 */
1643 if (dp && dsl_pool_sync_context(dp))
34dc7c2f
BB
1644 return (spa->spa_root_vdev->vdev_guid);
1645 else
3bc7e0fb 1646 return (guid);
3541dc6d
GA
1647}
1648
1649uint64_t
1650spa_load_guid(spa_t *spa)
1651{
1652 /*
1653 * This is a GUID that exists solely as a reference for the
1654 * purposes of the arc. It is generated at load time, and
1655 * is never written to persistent storage.
1656 */
1657 return (spa->spa_load_guid);
34dc7c2f
BB
1658}
1659
1660uint64_t
1661spa_last_synced_txg(spa_t *spa)
1662{
1663 return (spa->spa_ubsync.ub_txg);
1664}
1665
1666uint64_t
1667spa_first_txg(spa_t *spa)
1668{
1669 return (spa->spa_first_txg);
1670}
1671
428870ff
BB
1672uint64_t
1673spa_syncing_txg(spa_t *spa)
1674{
1675 return (spa->spa_syncing_txg);
1676}
1677
3b7f360c
GW
1678/*
1679 * Return the last txg where data can be dirtied. The final txgs
1680 * will be used to just clear out any deferred frees that remain.
1681 */
1682uint64_t
1683spa_final_dirty_txg(spa_t *spa)
1684{
1685 return (spa->spa_final_txg - TXG_DEFER_SIZE);
1686}
1687
b128c09f 1688pool_state_t
34dc7c2f
BB
1689spa_state(spa_t *spa)
1690{
1691 return (spa->spa_state);
1692}
1693
428870ff
BB
1694spa_load_state_t
1695spa_load_state(spa_t *spa)
34dc7c2f 1696{
428870ff 1697 return (spa->spa_load_state);
34dc7c2f
BB
1698}
1699
34dc7c2f 1700uint64_t
428870ff 1701spa_freeze_txg(spa_t *spa)
34dc7c2f 1702{
428870ff 1703 return (spa->spa_freeze_txg);
34dc7c2f
BB
1704}
1705
047187c1 1706/*
1707 * Return the inflated asize for a logical write in bytes. This is used by the
1708 * DMU to calculate the space a logical write will require on disk.
1709 * If lsize is smaller than the largest physical block size allocatable on this
1710 * pool we use its value instead, since the write will end up using the whole
1711 * block anyway.
1712 */
34dc7c2f 1713uint64_t
3ec3bc21 1714spa_get_worst_case_asize(spa_t *spa, uint64_t lsize)
34dc7c2f 1715{
047187c1 1716 if (lsize == 0)
1717 return (0); /* No inflation needed */
1718 return (MAX(lsize, 1 << spa->spa_max_ashift) * spa_asize_inflation);
34dc7c2f
BB
1719}
1720
3d45fdd6
MA
1721/*
1722 * Return the amount of slop space in bytes. It is 1/32 of the pool (3.2%),
d7958b4c
MA
1723 * or at least 128MB, unless that would cause it to be more than half the
1724 * pool size.
3d45fdd6
MA
1725 *
1726 * See the comment above spa_slop_shift for details.
1727 */
1728uint64_t
4ea3f864
GM
1729spa_get_slop_space(spa_t *spa)
1730{
3d45fdd6 1731 uint64_t space = spa_get_dspace(spa);
d7958b4c 1732 return (MAX(space >> spa_slop_shift, MIN(space >> 1, spa_min_slop)));
3d45fdd6
MA
1733}
1734
34dc7c2f
BB
1735uint64_t
1736spa_get_dspace(spa_t *spa)
1737{
428870ff 1738 return (spa->spa_dspace);
34dc7c2f
BB
1739}
1740
d2734cce
SD
1741uint64_t
1742spa_get_checkpoint_space(spa_t *spa)
1743{
1744 return (spa->spa_checkpoint_info.sci_dspace);
1745}
1746
428870ff
BB
1747void
1748spa_update_dspace(spa_t *spa)
34dc7c2f 1749{
428870ff
BB
1750 spa->spa_dspace = metaslab_class_get_dspace(spa_normal_class(spa)) +
1751 ddt_get_dedup_dspace(spa);
a1d477c2
MA
1752 if (spa->spa_vdev_removal != NULL) {
1753 /*
1754 * We can't allocate from the removing device, so
1755 * subtract its size. This prevents the DMU/DSL from
1756 * filling up the (now smaller) pool while we are in the
1757 * middle of removing the device.
1758 *
1759 * Note that the DMU/DSL doesn't actually know or care
1760 * how much space is allocated (it does its own tracking
1761 * of how much space has been logically used). So it
1762 * doesn't matter that the data we are moving may be
1763 * allocated twice (on the old device and the new
1764 * device).
1765 */
9e052db4
MA
1766 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
1767 vdev_t *vd =
1768 vdev_lookup_top(spa, spa->spa_vdev_removal->svr_vdev_id);
a1d477c2
MA
1769 spa->spa_dspace -= spa_deflate(spa) ?
1770 vd->vdev_stat.vs_dspace : vd->vdev_stat.vs_space;
9e052db4 1771 spa_config_exit(spa, SCL_VDEV, FTAG);
a1d477c2 1772 }
34dc7c2f
BB
1773}
1774
1775/*
1776 * Return the failure mode that has been set to this pool. The default
1777 * behavior will be to block all I/Os when a complete failure occurs.
1778 */
8fb1ede1 1779uint64_t
34dc7c2f
BB
1780spa_get_failmode(spa_t *spa)
1781{
1782 return (spa->spa_failmode);
1783}
1784
b128c09f
BB
1785boolean_t
1786spa_suspended(spa_t *spa)
1787{
cec3a0a1 1788 return (spa->spa_suspended != ZIO_SUSPEND_NONE);
b128c09f
BB
1789}
1790
34dc7c2f
BB
1791uint64_t
1792spa_version(spa_t *spa)
1793{
1794 return (spa->spa_ubsync.ub_version);
1795}
1796
428870ff
BB
1797boolean_t
1798spa_deflate(spa_t *spa)
1799{
1800 return (spa->spa_deflate);
1801}
1802
1803metaslab_class_t *
1804spa_normal_class(spa_t *spa)
1805{
1806 return (spa->spa_normal_class);
1807}
1808
1809metaslab_class_t *
1810spa_log_class(spa_t *spa)
1811{
1812 return (spa->spa_log_class);
1813}
1814
cc99f275
DB
1815metaslab_class_t *
1816spa_special_class(spa_t *spa)
1817{
1818 return (spa->spa_special_class);
1819}
1820
1821metaslab_class_t *
1822spa_dedup_class(spa_t *spa)
1823{
1824 return (spa->spa_dedup_class);
1825}
1826
1827/*
1828 * Locate an appropriate allocation class
1829 */
1830metaslab_class_t *
1831spa_preferred_class(spa_t *spa, uint64_t size, dmu_object_type_t objtype,
1832 uint_t level, uint_t special_smallblk)
1833{
1834 if (DMU_OT_IS_ZIL(objtype)) {
1835 if (spa->spa_log_class->mc_groups != 0)
1836 return (spa_log_class(spa));
1837 else
1838 return (spa_normal_class(spa));
1839 }
1840
1841 boolean_t has_special_class = spa->spa_special_class->mc_groups != 0;
1842
1843 if (DMU_OT_IS_DDT(objtype)) {
1844 if (spa->spa_dedup_class->mc_groups != 0)
1845 return (spa_dedup_class(spa));
1846 else if (has_special_class && zfs_ddt_data_is_special)
1847 return (spa_special_class(spa));
1848 else
1849 return (spa_normal_class(spa));
1850 }
1851
1852 /* Indirect blocks for user data can land in special if allowed */
1853 if (level > 0 && (DMU_OT_IS_FILE(objtype) || objtype == DMU_OT_ZVOL)) {
1854 if (has_special_class && zfs_user_indirect_is_special)
1855 return (spa_special_class(spa));
1856 else
1857 return (spa_normal_class(spa));
1858 }
1859
1860 if (DMU_OT_IS_METADATA(objtype) || level > 0) {
1861 if (has_special_class)
1862 return (spa_special_class(spa));
1863 else
1864 return (spa_normal_class(spa));
1865 }
1866
1867 /*
1868 * Allow small file blocks in special class in some cases (like
1869 * for the dRAID vdev feature). But always leave a reserve of
1870 * zfs_special_class_metadata_reserve_pct exclusively for metadata.
1871 */
1872 if (DMU_OT_IS_FILE(objtype) &&
44170969 1873 has_special_class && size <= special_smallblk) {
cc99f275
DB
1874 metaslab_class_t *special = spa_special_class(spa);
1875 uint64_t alloc = metaslab_class_get_alloc(special);
1876 uint64_t space = metaslab_class_get_space(special);
1877 uint64_t limit =
1878 (space * (100 - zfs_special_class_metadata_reserve_pct))
1879 / 100;
1880
1881 if (alloc < limit)
1882 return (special);
1883 }
1884
1885 return (spa_normal_class(spa));
1886}
1887
0c66c32d
JG
1888void
1889spa_evicting_os_register(spa_t *spa, objset_t *os)
1890{
1891 mutex_enter(&spa->spa_evicting_os_lock);
1892 list_insert_head(&spa->spa_evicting_os_list, os);
1893 mutex_exit(&spa->spa_evicting_os_lock);
1894}
1895
1896void
1897spa_evicting_os_deregister(spa_t *spa, objset_t *os)
1898{
1899 mutex_enter(&spa->spa_evicting_os_lock);
1900 list_remove(&spa->spa_evicting_os_list, os);
1901 cv_broadcast(&spa->spa_evicting_os_cv);
1902 mutex_exit(&spa->spa_evicting_os_lock);
1903}
1904
1905void
1906spa_evicting_os_wait(spa_t *spa)
1907{
1908 mutex_enter(&spa->spa_evicting_os_lock);
1909 while (!list_is_empty(&spa->spa_evicting_os_list))
1910 cv_wait(&spa->spa_evicting_os_cv, &spa->spa_evicting_os_lock);
1911 mutex_exit(&spa->spa_evicting_os_lock);
1912
1913 dmu_buf_user_evict_wait();
1914}
1915
34dc7c2f
BB
1916int
1917spa_max_replication(spa_t *spa)
1918{
1919 /*
1920 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
1921 * handle BPs with more than one DVA allocated. Set our max
1922 * replication level accordingly.
1923 */
1924 if (spa_version(spa) < SPA_VERSION_DITTO_BLOCKS)
1925 return (1);
1926 return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override));
1927}
1928
428870ff
BB
1929int
1930spa_prev_software_version(spa_t *spa)
1931{
1932 return (spa->spa_prev_software_version);
1933}
1934
cc92e9d0
GW
1935uint64_t
1936spa_deadman_synctime(spa_t *spa)
1937{
1938 return (spa->spa_deadman_synctime);
1939}
1940
1b939560
BB
1941spa_autotrim_t
1942spa_get_autotrim(spa_t *spa)
1943{
1944 return (spa->spa_autotrim);
1945}
1946
8fb1ede1
BB
1947uint64_t
1948spa_deadman_ziotime(spa_t *spa)
1949{
1950 return (spa->spa_deadman_ziotime);
1951}
1952
1953uint64_t
1954spa_get_deadman_failmode(spa_t *spa)
1955{
1956 return (spa->spa_deadman_failmode);
1957}
1958
1959void
1960spa_set_deadman_failmode(spa_t *spa, const char *failmode)
1961{
1962 if (strcmp(failmode, "wait") == 0)
1963 spa->spa_deadman_failmode = ZIO_FAILURE_MODE_WAIT;
1964 else if (strcmp(failmode, "continue") == 0)
1965 spa->spa_deadman_failmode = ZIO_FAILURE_MODE_CONTINUE;
1966 else if (strcmp(failmode, "panic") == 0)
1967 spa->spa_deadman_failmode = ZIO_FAILURE_MODE_PANIC;
1968 else
1969 spa->spa_deadman_failmode = ZIO_FAILURE_MODE_WAIT;
1970}
1971
34dc7c2f 1972uint64_t
428870ff 1973dva_get_dsize_sync(spa_t *spa, const dva_t *dva)
34dc7c2f 1974{
428870ff
BB
1975 uint64_t asize = DVA_GET_ASIZE(dva);
1976 uint64_t dsize = asize;
34dc7c2f 1977
428870ff 1978 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
34dc7c2f 1979
428870ff
BB
1980 if (asize != 0 && spa->spa_deflate) {
1981 vdev_t *vd = vdev_lookup_top(spa, DVA_GET_VDEV(dva));
2c33b912
BB
1982 if (vd != NULL)
1983 dsize = (asize >> SPA_MINBLOCKSHIFT) *
1984 vd->vdev_deflate_ratio;
34dc7c2f 1985 }
428870ff
BB
1986
1987 return (dsize);
1988}
1989
1990uint64_t
1991bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp)
1992{
1993 uint64_t dsize = 0;
1994
1c27024e 1995 for (int d = 0; d < BP_GET_NDVAS(bp); d++)
428870ff
BB
1996 dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1997
1998 return (dsize);
1999}
2000
2001uint64_t
2002bp_get_dsize(spa_t *spa, const blkptr_t *bp)
2003{
2004 uint64_t dsize = 0;
2005
2006 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2007
1c27024e 2008 for (int d = 0; d < BP_GET_NDVAS(bp); d++)
428870ff
BB
2009 dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
2010
b128c09f 2011 spa_config_exit(spa, SCL_VDEV, FTAG);
428870ff
BB
2012
2013 return (dsize);
34dc7c2f
BB
2014}
2015
dae3e9ea
DB
2016uint64_t
2017spa_dirty_data(spa_t *spa)
2018{
2019 return (spa->spa_dsl_pool->dp_dirty_total);
2020}
2021
ca95f70d
OF
2022/*
2023 * ==========================================================================
2024 * SPA Import Progress Routines
2025 * ==========================================================================
2026 */
2027
2028typedef struct spa_import_progress {
2029 uint64_t pool_guid; /* unique id for updates */
2030 char *pool_name;
2031 spa_load_state_t spa_load_state;
2032 uint64_t mmp_sec_remaining; /* MMP activity check */
2033 uint64_t spa_load_max_txg; /* rewind txg */
2034 procfs_list_node_t smh_node;
2035} spa_import_progress_t;
2036
2037spa_history_list_t *spa_import_progress_list = NULL;
2038
2039static int
2040spa_import_progress_show_header(struct seq_file *f)
2041{
2042 seq_printf(f, "%-20s %-14s %-14s %-12s %s\n", "pool_guid",
2043 "load_state", "multihost_secs", "max_txg",
2044 "pool_name");
2045 return (0);
2046}
2047
2048static int
2049spa_import_progress_show(struct seq_file *f, void *data)
2050{
2051 spa_import_progress_t *sip = (spa_import_progress_t *)data;
2052
2053 seq_printf(f, "%-20llu %-14llu %-14llu %-12llu %s\n",
2054 (u_longlong_t)sip->pool_guid, (u_longlong_t)sip->spa_load_state,
2055 (u_longlong_t)sip->mmp_sec_remaining,
2056 (u_longlong_t)sip->spa_load_max_txg,
2057 (sip->pool_name ? sip->pool_name : "-"));
2058
2059 return (0);
2060}
2061
2062/* Remove oldest elements from list until there are no more than 'size' left */
2063static void
2064spa_import_progress_truncate(spa_history_list_t *shl, unsigned int size)
2065{
2066 spa_import_progress_t *sip;
2067 while (shl->size > size) {
2068 sip = list_remove_head(&shl->procfs_list.pl_list);
2069 if (sip->pool_name)
2070 spa_strfree(sip->pool_name);
2071 kmem_free(sip, sizeof (spa_import_progress_t));
2072 shl->size--;
2073 }
2074
2075 IMPLY(size == 0, list_is_empty(&shl->procfs_list.pl_list));
2076}
2077
2078static void
2079spa_import_progress_init(void)
2080{
2081 spa_import_progress_list = kmem_zalloc(sizeof (spa_history_list_t),
2082 KM_SLEEP);
2083
2084 spa_import_progress_list->size = 0;
2085
2086 spa_import_progress_list->procfs_list.pl_private =
2087 spa_import_progress_list;
2088
2089 procfs_list_install("zfs",
2090 "import_progress",
2091 0644,
2092 &spa_import_progress_list->procfs_list,
2093 spa_import_progress_show,
2094 spa_import_progress_show_header,
2095 NULL,
2096 offsetof(spa_import_progress_t, smh_node));
2097}
2098
2099static void
2100spa_import_progress_destroy(void)
2101{
2102 spa_history_list_t *shl = spa_import_progress_list;
2103 procfs_list_uninstall(&shl->procfs_list);
2104 spa_import_progress_truncate(shl, 0);
2105 kmem_free(shl, sizeof (spa_history_list_t));
2106 procfs_list_destroy(&shl->procfs_list);
2107}
2108
2109int
2110spa_import_progress_set_state(uint64_t pool_guid,
2111 spa_load_state_t load_state)
2112{
2113 spa_history_list_t *shl = spa_import_progress_list;
2114 spa_import_progress_t *sip;
2115 int error = ENOENT;
2116
2117 if (shl->size == 0)
2118 return (0);
2119
2120 mutex_enter(&shl->procfs_list.pl_lock);
2121 for (sip = list_tail(&shl->procfs_list.pl_list); sip != NULL;
2122 sip = list_prev(&shl->procfs_list.pl_list, sip)) {
2123 if (sip->pool_guid == pool_guid) {
2124 sip->spa_load_state = load_state;
2125 error = 0;
2126 break;
2127 }
2128 }
2129 mutex_exit(&shl->procfs_list.pl_lock);
2130
2131 return (error);
2132}
2133
2134int
2135spa_import_progress_set_max_txg(uint64_t pool_guid, uint64_t load_max_txg)
2136{
2137 spa_history_list_t *shl = spa_import_progress_list;
2138 spa_import_progress_t *sip;
2139 int error = ENOENT;
2140
2141 if (shl->size == 0)
2142 return (0);
2143
2144 mutex_enter(&shl->procfs_list.pl_lock);
2145 for (sip = list_tail(&shl->procfs_list.pl_list); sip != NULL;
2146 sip = list_prev(&shl->procfs_list.pl_list, sip)) {
2147 if (sip->pool_guid == pool_guid) {
2148 sip->spa_load_max_txg = load_max_txg;
2149 error = 0;
2150 break;
2151 }
2152 }
2153 mutex_exit(&shl->procfs_list.pl_lock);
2154
2155 return (error);
2156}
2157
2158int
2159spa_import_progress_set_mmp_check(uint64_t pool_guid,
2160 uint64_t mmp_sec_remaining)
2161{
2162 spa_history_list_t *shl = spa_import_progress_list;
2163 spa_import_progress_t *sip;
2164 int error = ENOENT;
2165
2166 if (shl->size == 0)
2167 return (0);
2168
2169 mutex_enter(&shl->procfs_list.pl_lock);
2170 for (sip = list_tail(&shl->procfs_list.pl_list); sip != NULL;
2171 sip = list_prev(&shl->procfs_list.pl_list, sip)) {
2172 if (sip->pool_guid == pool_guid) {
2173 sip->mmp_sec_remaining = mmp_sec_remaining;
2174 error = 0;
2175 break;
2176 }
2177 }
2178 mutex_exit(&shl->procfs_list.pl_lock);
2179
2180 return (error);
2181}
2182
2183/*
2184 * A new import is in progress, add an entry.
2185 */
2186void
2187spa_import_progress_add(spa_t *spa)
2188{
2189 spa_history_list_t *shl = spa_import_progress_list;
2190 spa_import_progress_t *sip;
2191 char *poolname = NULL;
2192
2193 sip = kmem_zalloc(sizeof (spa_import_progress_t), KM_SLEEP);
2194 sip->pool_guid = spa_guid(spa);
2195
2196 (void) nvlist_lookup_string(spa->spa_config, ZPOOL_CONFIG_POOL_NAME,
2197 &poolname);
2198 if (poolname == NULL)
2199 poolname = spa_name(spa);
2200 sip->pool_name = spa_strdup(poolname);
2201 sip->spa_load_state = spa_load_state(spa);
2202
2203 mutex_enter(&shl->procfs_list.pl_lock);
2204 procfs_list_add(&shl->procfs_list, sip);
2205 shl->size++;
2206 mutex_exit(&shl->procfs_list.pl_lock);
2207}
2208
2209void
2210spa_import_progress_remove(uint64_t pool_guid)
2211{
2212 spa_history_list_t *shl = spa_import_progress_list;
2213 spa_import_progress_t *sip;
2214
2215 mutex_enter(&shl->procfs_list.pl_lock);
2216 for (sip = list_tail(&shl->procfs_list.pl_list); sip != NULL;
2217 sip = list_prev(&shl->procfs_list.pl_list, sip)) {
2218 if (sip->pool_guid == pool_guid) {
2219 if (sip->pool_name)
2220 spa_strfree(sip->pool_name);
2221 list_remove(&shl->procfs_list.pl_list, sip);
2222 shl->size--;
2223 kmem_free(sip, sizeof (spa_import_progress_t));
2224 break;
2225 }
2226 }
2227 mutex_exit(&shl->procfs_list.pl_lock);
2228}
2229
34dc7c2f
BB
2230/*
2231 * ==========================================================================
2232 * Initialization and Termination
2233 * ==========================================================================
2234 */
2235
2236static int
2237spa_name_compare(const void *a1, const void *a2)
2238{
2239 const spa_t *s1 = a1;
2240 const spa_t *s2 = a2;
2241 int s;
2242
2243 s = strcmp(s1->spa_name, s2->spa_name);
ee36c709
GN
2244
2245 return (AVL_ISIGN(s));
34dc7c2f
BB
2246}
2247
34dc7c2f 2248void
0bc8fd78 2249spa_boot_init(void)
34dc7c2f
BB
2250{
2251 spa_config_load();
2252}
2253
2254void
2255spa_init(int mode)
2256{
2257 mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL);
2258 mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL);
2259 mutex_init(&spa_l2cache_lock, NULL, MUTEX_DEFAULT, NULL);
2260 cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL);
2261
2262 avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t),
2263 offsetof(spa_t, spa_avl));
2264
2265 avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_aux_t),
2266 offsetof(spa_aux_t, aux_avl));
2267
2268 avl_create(&spa_l2cache_avl, spa_l2cache_compare, sizeof (spa_aux_t),
2269 offsetof(spa_aux_t, aux_avl));
2270
fb5f0bc8 2271 spa_mode_global = mode;
34dc7c2f 2272
498877ba
MA
2273#ifndef _KERNEL
2274 if (spa_mode_global != FREAD && dprintf_find_string("watch")) {
2275 struct sigaction sa;
2276
2277 sa.sa_flags = SA_SIGINFO;
2278 sigemptyset(&sa.sa_mask);
2279 sa.sa_sigaction = arc_buf_sigsegv;
2280
2281 if (sigaction(SIGSEGV, &sa, NULL) == -1) {
2282 perror("could not enable watchpoints: "
2283 "sigaction(SIGSEGV, ...) = ");
2284 } else {
2285 arc_watch = B_TRUE;
2286 }
2287 }
2288#endif
2289
26685276 2290 fm_init();
424fd7c3 2291 zfs_refcount_init();
34dc7c2f 2292 unique_init();
93cf2076 2293 range_tree_init();
4e21fd06 2294 metaslab_alloc_trace_init();
ecf3d9b8 2295 ddt_init();
34dc7c2f
BB
2296 zio_init();
2297 dmu_init();
2298 zil_init();
2299 vdev_cache_stat_init();
551905dd 2300 vdev_mirror_stat_init();
ab9f4b0b 2301 vdev_raidz_math_init();
da8f51e1 2302 vdev_file_init();
34dc7c2f
BB
2303 zfs_prop_init();
2304 zpool_prop_init();
9ae529ec 2305 zpool_feature_init();
34dc7c2f 2306 spa_config_load();
b128c09f 2307 l2arc_start();
d4a72f23 2308 scan_init();
6a9d6359 2309 qat_init();
ca95f70d 2310 spa_import_progress_init();
34dc7c2f
BB
2311}
2312
2313void
2314spa_fini(void)
2315{
b128c09f
BB
2316 l2arc_stop();
2317
34dc7c2f
BB
2318 spa_evict_all();
2319
da8f51e1 2320 vdev_file_fini();
34dc7c2f 2321 vdev_cache_stat_fini();
551905dd 2322 vdev_mirror_stat_fini();
ab9f4b0b 2323 vdev_raidz_math_fini();
34dc7c2f
BB
2324 zil_fini();
2325 dmu_fini();
2326 zio_fini();
ecf3d9b8 2327 ddt_fini();
4e21fd06 2328 metaslab_alloc_trace_fini();
93cf2076 2329 range_tree_fini();
34dc7c2f 2330 unique_fini();
424fd7c3 2331 zfs_refcount_fini();
26685276 2332 fm_fini();
d4a72f23 2333 scan_fini();
6a9d6359 2334 qat_fini();
ca95f70d 2335 spa_import_progress_destroy();
34dc7c2f
BB
2336
2337 avl_destroy(&spa_namespace_avl);
2338 avl_destroy(&spa_spare_avl);
2339 avl_destroy(&spa_l2cache_avl);
2340
2341 cv_destroy(&spa_namespace_cv);
2342 mutex_destroy(&spa_namespace_lock);
2343 mutex_destroy(&spa_spare_lock);
2344 mutex_destroy(&spa_l2cache_lock);
2345}
2346
2347/*
2348 * Return whether this pool has slogs. No locking needed.
2349 * It's not a problem if the wrong answer is returned as it's only for
2350 * performance and not correctness
2351 */
2352boolean_t
2353spa_has_slogs(spa_t *spa)
2354{
2355 return (spa->spa_log_class->mc_rotor != NULL);
2356}
b128c09f 2357
428870ff
BB
2358spa_log_state_t
2359spa_get_log_state(spa_t *spa)
2360{
2361 return (spa->spa_log_state);
2362}
2363
2364void
2365spa_set_log_state(spa_t *spa, spa_log_state_t state)
2366{
2367 spa->spa_log_state = state;
2368}
2369
b128c09f
BB
2370boolean_t
2371spa_is_root(spa_t *spa)
2372{
2373 return (spa->spa_is_root);
2374}
fb5f0bc8
BB
2375
2376boolean_t
2377spa_writeable(spa_t *spa)
2378{
6cb8e530 2379 return (!!(spa->spa_mode & FWRITE) && spa->spa_trust_config);
fb5f0bc8
BB
2380}
2381
acbad6ff
AR
2382/*
2383 * Returns true if there is a pending sync task in any of the current
2384 * syncing txg, the current quiescing txg, or the current open txg.
2385 */
2386boolean_t
2387spa_has_pending_synctask(spa_t *spa)
2388{
d2734cce
SD
2389 return (!txg_all_lists_empty(&spa->spa_dsl_pool->dp_sync_tasks) ||
2390 !txg_all_lists_empty(&spa->spa_dsl_pool->dp_early_sync_tasks));
acbad6ff
AR
2391}
2392
fb5f0bc8
BB
2393int
2394spa_mode(spa_t *spa)
2395{
2396 return (spa->spa_mode);
2397}
428870ff
BB
2398
2399uint64_t
2400spa_bootfs(spa_t *spa)
2401{
2402 return (spa->spa_bootfs);
2403}
2404
2405uint64_t
2406spa_delegation(spa_t *spa)
2407{
2408 return (spa->spa_delegation);
2409}
2410
2411objset_t *
2412spa_meta_objset(spa_t *spa)
2413{
2414 return (spa->spa_meta_objset);
2415}
2416
2417enum zio_checksum
2418spa_dedup_checksum(spa_t *spa)
2419{
2420 return (spa->spa_dedup_checksum);
2421}
2422
2423/*
2424 * Reset pool scan stat per scan pass (or reboot).
2425 */
2426void
2427spa_scan_stat_init(spa_t *spa)
2428{
2429 /* data not stored on disk */
2430 spa->spa_scan_pass_start = gethrestime_sec();
0ea05c64
AP
2431 if (dsl_scan_is_paused_scrub(spa->spa_dsl_pool->dp_scan))
2432 spa->spa_scan_pass_scrub_pause = spa->spa_scan_pass_start;
2433 else
2434 spa->spa_scan_pass_scrub_pause = 0;
2435 spa->spa_scan_pass_scrub_spent_paused = 0;
428870ff 2436 spa->spa_scan_pass_exam = 0;
d4a72f23 2437 spa->spa_scan_pass_issued = 0;
428870ff
BB
2438 vdev_scan_stat_init(spa->spa_root_vdev);
2439}
2440
2441/*
2442 * Get scan stats for zpool status reports
2443 */
2444int
2445spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps)
2446{
2447 dsl_scan_t *scn = spa->spa_dsl_pool ? spa->spa_dsl_pool->dp_scan : NULL;
2448
2449 if (scn == NULL || scn->scn_phys.scn_func == POOL_SCAN_NONE)
2e528b49 2450 return (SET_ERROR(ENOENT));
428870ff
BB
2451 bzero(ps, sizeof (pool_scan_stat_t));
2452
2453 /* data stored on disk */
2454 ps->pss_func = scn->scn_phys.scn_func;
d4a72f23 2455 ps->pss_state = scn->scn_phys.scn_state;
428870ff
BB
2456 ps->pss_start_time = scn->scn_phys.scn_start_time;
2457 ps->pss_end_time = scn->scn_phys.scn_end_time;
2458 ps->pss_to_examine = scn->scn_phys.scn_to_examine;
d4677269 2459 ps->pss_examined = scn->scn_phys.scn_examined;
428870ff
BB
2460 ps->pss_to_process = scn->scn_phys.scn_to_process;
2461 ps->pss_processed = scn->scn_phys.scn_processed;
2462 ps->pss_errors = scn->scn_phys.scn_errors;
428870ff
BB
2463
2464 /* data not stored on disk */
428870ff 2465 ps->pss_pass_exam = spa->spa_scan_pass_exam;
d4677269 2466 ps->pss_pass_start = spa->spa_scan_pass_start;
0ea05c64
AP
2467 ps->pss_pass_scrub_pause = spa->spa_scan_pass_scrub_pause;
2468 ps->pss_pass_scrub_spent_paused = spa->spa_scan_pass_scrub_spent_paused;
d4677269
TC
2469 ps->pss_pass_issued = spa->spa_scan_pass_issued;
2470 ps->pss_issued =
2471 scn->scn_issued_before_pass + spa->spa_scan_pass_issued;
428870ff
BB
2472
2473 return (0);
2474}
c28b2279 2475
f1512ee6
MA
2476int
2477spa_maxblocksize(spa_t *spa)
2478{
2479 if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS))
2480 return (SPA_MAXBLOCKSIZE);
2481 else
2482 return (SPA_OLD_MAXBLOCKSIZE);
2483}
2484
a1d477c2
MA
2485
2486/*
2487 * Returns the txg that the last device removal completed. No indirect mappings
2488 * have been added since this txg.
2489 */
2490uint64_t
2491spa_get_last_removal_txg(spa_t *spa)
2492{
2493 uint64_t vdevid;
2494 uint64_t ret = -1ULL;
2495
2496 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2497 /*
2498 * sr_prev_indirect_vdev is only modified while holding all the
2499 * config locks, so it is sufficient to hold SCL_VDEV as reader when
2500 * examining it.
2501 */
2502 vdevid = spa->spa_removing_phys.sr_prev_indirect_vdev;
2503
2504 while (vdevid != -1ULL) {
2505 vdev_t *vd = vdev_lookup_top(spa, vdevid);
2506 vdev_indirect_births_t *vib = vd->vdev_indirect_births;
2507
2508 ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops);
2509
2510 /*
2511 * If the removal did not remap any data, we don't care.
2512 */
2513 if (vdev_indirect_births_count(vib) != 0) {
2514 ret = vdev_indirect_births_last_entry_txg(vib);
2515 break;
2516 }
2517
2518 vdevid = vd->vdev_indirect_config.vic_prev_indirect_vdev;
2519 }
2520 spa_config_exit(spa, SCL_VDEV, FTAG);
2521
2522 IMPLY(ret != -1ULL,
2523 spa_feature_is_active(spa, SPA_FEATURE_DEVICE_REMOVAL));
2524
2525 return (ret);
2526}
2527
50c957f7
NB
2528int
2529spa_maxdnodesize(spa_t *spa)
2530{
2531 if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_DNODE))
2532 return (DNODE_MAX_SIZE);
2533 else
2534 return (DNODE_MIN_SIZE);
2535}
2536
379ca9cf
OF
2537boolean_t
2538spa_multihost(spa_t *spa)
2539{
2540 return (spa->spa_multihost ? B_TRUE : B_FALSE);
2541}
2542
2543unsigned long
2544spa_get_hostid(void)
2545{
2546 unsigned long myhostid;
2547
2548#ifdef _KERNEL
2549 myhostid = zone_get_hostid(NULL);
2550#else /* _KERNEL */
2551 /*
2552 * We're emulating the system's hostid in userland, so
2553 * we can't use zone_get_hostid().
2554 */
2555 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
2556#endif /* _KERNEL */
2557
2558 return (myhostid);
2559}
2560
6cb8e530
PZ
2561boolean_t
2562spa_trust_config(spa_t *spa)
2563{
2564 return (spa->spa_trust_config);
2565}
2566
2567uint64_t
2568spa_missing_tvds_allowed(spa_t *spa)
2569{
2570 return (spa->spa_missing_tvds_allowed);
2571}
2572
2573void
2574spa_set_missing_tvds(spa_t *spa, uint64_t missing)
2575{
2576 spa->spa_missing_tvds = missing;
2577}
2578
f0ed6c74
TH
2579/*
2580 * Return the pool state string ("ONLINE", "DEGRADED", "SUSPENDED", etc).
2581 */
2582const char *
2583spa_state_to_name(spa_t *spa)
2584{
78fac8d9
RE
2585 ASSERT3P(spa, !=, NULL);
2586
2587 /*
2588 * it is possible for the spa to exist, without root vdev
2589 * as the spa transitions during import/export
2590 */
2591 vdev_t *rvd = spa->spa_root_vdev;
2592 if (rvd == NULL) {
2593 return ("TRANSITIONING");
2594 }
2595 vdev_state_t state = rvd->vdev_state;
2596 vdev_aux_t aux = rvd->vdev_stat.vs_aux;
f0ed6c74
TH
2597
2598 if (spa_suspended(spa) &&
2599 (spa_get_failmode(spa) != ZIO_FAILURE_MODE_CONTINUE))
2600 return ("SUSPENDED");
2601
2602 switch (state) {
2603 case VDEV_STATE_CLOSED:
2604 case VDEV_STATE_OFFLINE:
2605 return ("OFFLINE");
2606 case VDEV_STATE_REMOVED:
2607 return ("REMOVED");
2608 case VDEV_STATE_CANT_OPEN:
2609 if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
2610 return ("FAULTED");
2611 else if (aux == VDEV_AUX_SPLIT_POOL)
2612 return ("SPLIT");
2613 else
2614 return ("UNAVAIL");
2615 case VDEV_STATE_FAULTED:
2616 return ("FAULTED");
2617 case VDEV_STATE_DEGRADED:
2618 return ("DEGRADED");
2619 case VDEV_STATE_HEALTHY:
2620 return ("ONLINE");
2621 default:
2622 break;
2623 }
2624
2625 return ("UNKNOWN");
2626}
2627
d2734cce
SD
2628boolean_t
2629spa_top_vdevs_spacemap_addressable(spa_t *spa)
2630{
2631 vdev_t *rvd = spa->spa_root_vdev;
2632 for (uint64_t c = 0; c < rvd->vdev_children; c++) {
2633 if (!vdev_is_spacemap_addressable(rvd->vdev_child[c]))
2634 return (B_FALSE);
2635 }
2636 return (B_TRUE);
2637}
2638
2639boolean_t
2640spa_has_checkpoint(spa_t *spa)
2641{
2642 return (spa->spa_checkpoint_txg != 0);
2643}
2644
2645boolean_t
2646spa_importing_readonly_checkpoint(spa_t *spa)
2647{
2648 return ((spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT) &&
2649 spa->spa_mode == FREAD);
2650}
2651
2652uint64_t
2653spa_min_claim_txg(spa_t *spa)
2654{
2655 uint64_t checkpoint_txg = spa->spa_uberblock.ub_checkpoint_txg;
2656
2657 if (checkpoint_txg != 0)
2658 return (checkpoint_txg + 1);
2659
2660 return (spa->spa_first_txg);
2661}
2662
2663/*
2664 * If there is a checkpoint, async destroys may consume more space from
2665 * the pool instead of freeing it. In an attempt to save the pool from
2666 * getting suspended when it is about to run out of space, we stop
2667 * processing async destroys.
2668 */
2669boolean_t
2670spa_suspend_async_destroy(spa_t *spa)
2671{
2672 dsl_pool_t *dp = spa_get_dsl(spa);
2673
2674 uint64_t unreserved = dsl_pool_unreserved_space(dp,
2675 ZFS_SPACE_CHECK_EXTRA_RESERVED);
2676 uint64_t used = dsl_dir_phys(dp->dp_root_dir)->dd_used_bytes;
2677 uint64_t avail = (unreserved > used) ? (unreserved - used) : 0;
2678
2679 if (spa_has_checkpoint(spa) && avail == 0)
2680 return (B_TRUE);
2681
2682 return (B_FALSE);
2683}
2684
93ce2b4c 2685#if defined(_KERNEL)
8fb1ede1
BB
2686
2687#include <linux/mod_compat.h>
2688
2689static int
2690param_set_deadman_failmode(const char *val, zfs_kernel_param_t *kp)
2691{
2692 spa_t *spa = NULL;
2693 char *p;
2694
2695 if (val == NULL)
2696 return (SET_ERROR(-EINVAL));
2697
2698 if ((p = strchr(val, '\n')) != NULL)
2699 *p = '\0';
2700
2701 if (strcmp(val, "wait") != 0 && strcmp(val, "continue") != 0 &&
2702 strcmp(val, "panic"))
2703 return (SET_ERROR(-EINVAL));
2704
d1043e2f
TC
2705 if (spa_mode_global != 0) {
2706 mutex_enter(&spa_namespace_lock);
2707 while ((spa = spa_next(spa)) != NULL)
2708 spa_set_deadman_failmode(spa, val);
2709 mutex_exit(&spa_namespace_lock);
2710 }
8fb1ede1
BB
2711
2712 return (param_set_charp(val, kp));
2713}
2714
d1043e2f
TC
2715static int
2716param_set_deadman_ziotime(const char *val, zfs_kernel_param_t *kp)
2717{
2718 spa_t *spa = NULL;
2719 int error;
2720
2721 error = param_set_ulong(val, kp);
2722 if (error < 0)
2723 return (SET_ERROR(error));
2724
2725 if (spa_mode_global != 0) {
2726 mutex_enter(&spa_namespace_lock);
2727 while ((spa = spa_next(spa)) != NULL)
2728 spa->spa_deadman_ziotime =
2729 MSEC2NSEC(zfs_deadman_ziotime_ms);
2730 mutex_exit(&spa_namespace_lock);
2731 }
2732
2733 return (0);
2734}
2735
2736static int
2737param_set_deadman_synctime(const char *val, zfs_kernel_param_t *kp)
2738{
2739 spa_t *spa = NULL;
2740 int error;
2741
2742 error = param_set_ulong(val, kp);
2743 if (error < 0)
2744 return (SET_ERROR(error));
2745
2746 if (spa_mode_global != 0) {
2747 mutex_enter(&spa_namespace_lock);
2748 while ((spa = spa_next(spa)) != NULL)
2749 spa->spa_deadman_synctime =
2750 MSEC2NSEC(zfs_deadman_synctime_ms);
2751 mutex_exit(&spa_namespace_lock);
2752 }
2753
2754 return (0);
2755}
2756
7522a260 2757static int
2758param_set_slop_shift(const char *buf, zfs_kernel_param_t *kp)
2759{
2760 unsigned long val;
2761 int error;
2762
2763 error = kstrtoul(buf, 0, &val);
2764 if (error)
2765 return (SET_ERROR(error));
2766
2767 if (val < 1 || val > 31)
2768 return (SET_ERROR(-EINVAL));
2769
2770 error = param_set_int(buf, kp);
2771 if (error < 0)
2772 return (SET_ERROR(error));
2773
2774 return (0);
2775}
2776
c28b2279
BB
2777/* Namespace manipulation */
2778EXPORT_SYMBOL(spa_lookup);
2779EXPORT_SYMBOL(spa_add);
2780EXPORT_SYMBOL(spa_remove);
2781EXPORT_SYMBOL(spa_next);
2782
2783/* Refcount functions */
2784EXPORT_SYMBOL(spa_open_ref);
2785EXPORT_SYMBOL(spa_close);
2786EXPORT_SYMBOL(spa_refcount_zero);
2787
2788/* Pool configuration lock */
2789EXPORT_SYMBOL(spa_config_tryenter);
2790EXPORT_SYMBOL(spa_config_enter);
2791EXPORT_SYMBOL(spa_config_exit);
2792EXPORT_SYMBOL(spa_config_held);
2793
2794/* Pool vdev add/remove lock */
2795EXPORT_SYMBOL(spa_vdev_enter);
2796EXPORT_SYMBOL(spa_vdev_exit);
2797
2798/* Pool vdev state change lock */
2799EXPORT_SYMBOL(spa_vdev_state_enter);
2800EXPORT_SYMBOL(spa_vdev_state_exit);
2801
2802/* Accessor functions */
2803EXPORT_SYMBOL(spa_shutting_down);
2804EXPORT_SYMBOL(spa_get_dsl);
2805EXPORT_SYMBOL(spa_get_rootblkptr);
2806EXPORT_SYMBOL(spa_set_rootblkptr);
2807EXPORT_SYMBOL(spa_altroot);
2808EXPORT_SYMBOL(spa_sync_pass);
2809EXPORT_SYMBOL(spa_name);
2810EXPORT_SYMBOL(spa_guid);
2811EXPORT_SYMBOL(spa_last_synced_txg);
2812EXPORT_SYMBOL(spa_first_txg);
2813EXPORT_SYMBOL(spa_syncing_txg);
2814EXPORT_SYMBOL(spa_version);
2815EXPORT_SYMBOL(spa_state);
2816EXPORT_SYMBOL(spa_load_state);
2817EXPORT_SYMBOL(spa_freeze_txg);
c28b2279
BB
2818EXPORT_SYMBOL(spa_get_dspace);
2819EXPORT_SYMBOL(spa_update_dspace);
2820EXPORT_SYMBOL(spa_deflate);
2821EXPORT_SYMBOL(spa_normal_class);
2822EXPORT_SYMBOL(spa_log_class);
cc99f275
DB
2823EXPORT_SYMBOL(spa_special_class);
2824EXPORT_SYMBOL(spa_preferred_class);
c28b2279
BB
2825EXPORT_SYMBOL(spa_max_replication);
2826EXPORT_SYMBOL(spa_prev_software_version);
2827EXPORT_SYMBOL(spa_get_failmode);
2828EXPORT_SYMBOL(spa_suspended);
2829EXPORT_SYMBOL(spa_bootfs);
2830EXPORT_SYMBOL(spa_delegation);
2831EXPORT_SYMBOL(spa_meta_objset);
f1512ee6 2832EXPORT_SYMBOL(spa_maxblocksize);
50c957f7 2833EXPORT_SYMBOL(spa_maxdnodesize);
c28b2279
BB
2834
2835/* Miscellaneous support routines */
c28b2279
BB
2836EXPORT_SYMBOL(spa_guid_exists);
2837EXPORT_SYMBOL(spa_strdup);
2838EXPORT_SYMBOL(spa_strfree);
2839EXPORT_SYMBOL(spa_get_random);
2840EXPORT_SYMBOL(spa_generate_guid);
b0bc7a84 2841EXPORT_SYMBOL(snprintf_blkptr);
c28b2279
BB
2842EXPORT_SYMBOL(spa_freeze);
2843EXPORT_SYMBOL(spa_upgrade);
2844EXPORT_SYMBOL(spa_evict_all);
2845EXPORT_SYMBOL(spa_lookup_by_guid);
2846EXPORT_SYMBOL(spa_has_spare);
2847EXPORT_SYMBOL(dva_get_dsize_sync);
2848EXPORT_SYMBOL(bp_get_dsize_sync);
2849EXPORT_SYMBOL(bp_get_dsize);
2850EXPORT_SYMBOL(spa_has_slogs);
2851EXPORT_SYMBOL(spa_is_root);
2852EXPORT_SYMBOL(spa_writeable);
2853EXPORT_SYMBOL(spa_mode);
c28b2279 2854EXPORT_SYMBOL(spa_namespace_lock);
6cb8e530
PZ
2855EXPORT_SYMBOL(spa_trust_config);
2856EXPORT_SYMBOL(spa_missing_tvds_allowed);
2857EXPORT_SYMBOL(spa_set_missing_tvds);
f0ed6c74 2858EXPORT_SYMBOL(spa_state_to_name);
d2734cce
SD
2859EXPORT_SYMBOL(spa_importing_readonly_checkpoint);
2860EXPORT_SYMBOL(spa_min_claim_txg);
2861EXPORT_SYMBOL(spa_suspend_async_destroy);
2862EXPORT_SYMBOL(spa_has_checkpoint);
2863EXPORT_SYMBOL(spa_top_vdevs_spacemap_addressable);
cc92e9d0 2864
02730c33 2865/* BEGIN CSTYLED */
33b6dbbc 2866module_param(zfs_flags, uint, 0644);
0b39b9f9
PS
2867MODULE_PARM_DESC(zfs_flags, "Set additional debugging flags");
2868
2869module_param(zfs_recover, int, 0644);
2870MODULE_PARM_DESC(zfs_recover, "Set to attempt to recover from fatal errors");
2871
2872module_param(zfs_free_leak_on_eio, int, 0644);
2873MODULE_PARM_DESC(zfs_free_leak_on_eio,
2874 "Set to ignore IO errors during free and permanently leak the space");
2875
d1043e2f
TC
2876module_param_call(zfs_deadman_synctime_ms, param_set_deadman_synctime,
2877 param_get_ulong, &zfs_deadman_synctime_ms, 0644);
8fb1ede1
BB
2878MODULE_PARM_DESC(zfs_deadman_synctime_ms,
2879 "Pool sync expiration time in milliseconds");
2880
d1043e2f
TC
2881module_param_call(zfs_deadman_ziotime_ms, param_set_deadman_ziotime,
2882 param_get_ulong, &zfs_deadman_ziotime_ms, 0644);
8fb1ede1
BB
2883MODULE_PARM_DESC(zfs_deadman_ziotime_ms,
2884 "IO expiration time in milliseconds");
cc92e9d0 2885
b81a3ddc
TC
2886module_param(zfs_deadman_checktime_ms, ulong, 0644);
2887MODULE_PARM_DESC(zfs_deadman_checktime_ms,
2888 "Dead I/O check interval in milliseconds");
2889
cc92e9d0
GW
2890module_param(zfs_deadman_enabled, int, 0644);
2891MODULE_PARM_DESC(zfs_deadman_enabled, "Enable deadman timer");
e8b96c60 2892
8fb1ede1
BB
2893module_param_call(zfs_deadman_failmode, param_set_deadman_failmode,
2894 param_get_charp, &zfs_deadman_failmode, 0644);
2895MODULE_PARM_DESC(zfs_deadman_failmode, "Failmode for deadman timer");
2896
e8b96c60
MA
2897module_param(spa_asize_inflation, int, 0644);
2898MODULE_PARM_DESC(spa_asize_inflation,
d1d7e268 2899 "SPA size estimate multiplication factor");
6cde6435 2900
7522a260 2901module_param_call(spa_slop_shift, param_set_slop_shift, param_get_int,
2902 &spa_slop_shift, 0644);
6cde6435 2903MODULE_PARM_DESC(spa_slop_shift, "Reserved free space in pool");
cc99f275
DB
2904
2905module_param(zfs_ddt_data_is_special, int, 0644);
2906MODULE_PARM_DESC(zfs_ddt_data_is_special,
2907 "Place DDT data into the special class");
2908
2909module_param(zfs_user_indirect_is_special, int, 0644);
2910MODULE_PARM_DESC(zfs_user_indirect_is_special,
2911 "Place user data indirect blocks into the special class");
1f02ecc5
D
2912
2913module_param(zfs_special_class_metadata_reserve_pct, int, 0644);
2914MODULE_PARM_DESC(zfs_special_class_metadata_reserve_pct,
2915 "Small file blocks in special vdevs depends on this much "
2916 "free space available");
02730c33 2917/* END CSTYLED */
c28b2279 2918#endif