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