]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/spa_misc.c
zfs does not honor NFS sync write semantics
[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);
c10d37dd
GW
1200 vdev_initialize_stop(vd, VDEV_INITIALIZE_CANCELED,
1201 NULL);
619f0976
GW
1202 mutex_exit(&vd->vdev_initialize_lock);
1203 }
1204
fb5f0bc8 1205 spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
34dc7c2f 1206 vdev_free(vd);
fb5f0bc8 1207 spa_config_exit(spa, SCL_ALL, spa);
34dc7c2f
BB
1208 }
1209
1210 /*
1211 * If the config changed, update the config cache.
1212 */
1213 if (config_changed)
a1d477c2 1214 spa_write_cachefile(spa, B_FALSE, B_TRUE);
428870ff 1215}
34dc7c2f 1216
428870ff
BB
1217/*
1218 * Unlock the spa_t after adding or removing a vdev. Besides undoing the
1219 * locking of spa_vdev_enter(), we also want make sure the transactions have
1220 * synced to disk, and then update the global configuration cache with the new
1221 * information.
1222 */
1223int
1224spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
1225{
1226 spa_vdev_config_exit(spa, vd, txg, error, FTAG);
34dc7c2f 1227 mutex_exit(&spa_namespace_lock);
428870ff 1228 mutex_exit(&spa->spa_vdev_top_lock);
34dc7c2f
BB
1229
1230 return (error);
1231}
1232
b128c09f
BB
1233/*
1234 * Lock the given spa_t for the purpose of changing vdev state.
1235 */
1236void
428870ff 1237spa_vdev_state_enter(spa_t *spa, int oplocks)
b128c09f 1238{
428870ff
BB
1239 int locks = SCL_STATE_ALL | oplocks;
1240
1241 /*
1242 * Root pools may need to read of the underlying devfs filesystem
1243 * when opening up a vdev. Unfortunately if we're holding the
1244 * SCL_ZIO lock it will result in a deadlock when we try to issue
1245 * the read from the root filesystem. Instead we "prefetch"
1246 * the associated vnodes that we need prior to opening the
1247 * underlying devices and cache them so that we can prevent
1248 * any I/O when we are doing the actual open.
1249 */
1250 if (spa_is_root(spa)) {
1251 int low = locks & ~(SCL_ZIO - 1);
1252 int high = locks & ~low;
1253
1254 spa_config_enter(spa, high, spa, RW_WRITER);
1255 vdev_hold(spa->spa_root_vdev);
1256 spa_config_enter(spa, low, spa, RW_WRITER);
1257 } else {
1258 spa_config_enter(spa, locks, spa, RW_WRITER);
1259 }
1260 spa->spa_vdev_locks = locks;
b128c09f
BB
1261}
1262
1263int
1264spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error)
1265{
428870ff 1266 boolean_t config_changed = B_FALSE;
4a283c7f
TH
1267 vdev_t *vdev_top;
1268
1269 if (vd == NULL || vd == spa->spa_root_vdev) {
1270 vdev_top = spa->spa_root_vdev;
1271 } else {
1272 vdev_top = vd->vdev_top;
1273 }
428870ff
BB
1274
1275 if (vd != NULL || error == 0)
4a283c7f 1276 vdev_dtl_reassess(vdev_top, 0, 0, B_FALSE);
428870ff
BB
1277
1278 if (vd != NULL) {
4a283c7f
TH
1279 if (vd != spa->spa_root_vdev)
1280 vdev_state_dirty(vdev_top);
1281
428870ff
BB
1282 config_changed = B_TRUE;
1283 spa->spa_config_generation++;
1284 }
b128c09f 1285
428870ff
BB
1286 if (spa_is_root(spa))
1287 vdev_rele(spa->spa_root_vdev);
1288
1289 ASSERT3U(spa->spa_vdev_locks, >=, SCL_STATE_ALL);
1290 spa_config_exit(spa, spa->spa_vdev_locks, spa);
b128c09f 1291
fb5f0bc8
BB
1292 /*
1293 * If anything changed, wait for it to sync. This ensures that,
1294 * from the system administrator's perspective, zpool(1M) commands
1295 * are synchronous. This is important for things like zpool offline:
1296 * when the command completes, you expect no further I/O from ZFS.
1297 */
1298 if (vd != NULL)
1299 txg_wait_synced(spa->spa_dsl_pool, 0);
1300
428870ff
BB
1301 /*
1302 * If the config changed, update the config cache.
1303 */
1304 if (config_changed) {
1305 mutex_enter(&spa_namespace_lock);
a1d477c2 1306 spa_write_cachefile(spa, B_FALSE, B_TRUE);
428870ff
BB
1307 mutex_exit(&spa_namespace_lock);
1308 }
1309
b128c09f
BB
1310 return (error);
1311}
1312
34dc7c2f
BB
1313/*
1314 * ==========================================================================
1315 * Miscellaneous functions
1316 * ==========================================================================
1317 */
1318
9ae529ec 1319void
b0bc7a84 1320spa_activate_mos_feature(spa_t *spa, const char *feature, dmu_tx_t *tx)
9ae529ec 1321{
fa86b5db
MA
1322 if (!nvlist_exists(spa->spa_label_features, feature)) {
1323 fnvlist_add_boolean(spa->spa_label_features, feature);
b0bc7a84
MG
1324 /*
1325 * When we are creating the pool (tx_txg==TXG_INITIAL), we can't
1326 * dirty the vdev config because lock SCL_CONFIG is not held.
1327 * Thankfully, in this case we don't need to dirty the config
1328 * because it will be written out anyway when we finish
1329 * creating the pool.
1330 */
1331 if (tx->tx_txg != TXG_INITIAL)
1332 vdev_config_dirty(spa->spa_root_vdev);
fa86b5db 1333 }
9ae529ec
CS
1334}
1335
1336void
1337spa_deactivate_mos_feature(spa_t *spa, const char *feature)
1338{
fa86b5db
MA
1339 if (nvlist_remove_all(spa->spa_label_features, feature) == 0)
1340 vdev_config_dirty(spa->spa_root_vdev);
9ae529ec
CS
1341}
1342
34dc7c2f 1343/*
572e2857
BB
1344 * Return the spa_t associated with given pool_guid, if it exists. If
1345 * device_guid is non-zero, determine whether the pool exists *and* contains
1346 * a device with the specified device_guid.
34dc7c2f 1347 */
572e2857
BB
1348spa_t *
1349spa_by_guid(uint64_t pool_guid, uint64_t device_guid)
34dc7c2f
BB
1350{
1351 spa_t *spa;
1352 avl_tree_t *t = &spa_namespace_avl;
1353
1354 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1355
1356 for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) {
1357 if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1358 continue;
1359 if (spa->spa_root_vdev == NULL)
1360 continue;
1361 if (spa_guid(spa) == pool_guid) {
1362 if (device_guid == 0)
1363 break;
1364
1365 if (vdev_lookup_by_guid(spa->spa_root_vdev,
1366 device_guid) != NULL)
1367 break;
1368
1369 /*
1370 * Check any devices we may be in the process of adding.
1371 */
1372 if (spa->spa_pending_vdev) {
1373 if (vdev_lookup_by_guid(spa->spa_pending_vdev,
1374 device_guid) != NULL)
1375 break;
1376 }
1377 }
1378 }
1379
572e2857
BB
1380 return (spa);
1381}
1382
1383/*
1384 * Determine whether a pool with the given pool_guid exists.
1385 */
1386boolean_t
1387spa_guid_exists(uint64_t pool_guid, uint64_t device_guid)
1388{
1389 return (spa_by_guid(pool_guid, device_guid) != NULL);
34dc7c2f
BB
1390}
1391
1392char *
1393spa_strdup(const char *s)
1394{
1395 size_t len;
1396 char *new;
1397
1398 len = strlen(s);
79c76d5b 1399 new = kmem_alloc(len + 1, KM_SLEEP);
34dc7c2f
BB
1400 bcopy(s, new, len);
1401 new[len] = '\0';
1402
1403 return (new);
1404}
1405
1406void
1407spa_strfree(char *s)
1408{
1409 kmem_free(s, strlen(s) + 1);
1410}
1411
1412uint64_t
1413spa_get_random(uint64_t range)
1414{
1415 uint64_t r;
1416
1417 ASSERT(range != 0);
1418
379ca9cf
OF
1419 if (range == 1)
1420 return (0);
1421
34dc7c2f
BB
1422 (void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t));
1423
1424 return (r % range);
1425}
1426
428870ff
BB
1427uint64_t
1428spa_generate_guid(spa_t *spa)
34dc7c2f 1429{
428870ff 1430 uint64_t guid = spa_get_random(-1ULL);
34dc7c2f 1431
428870ff
BB
1432 if (spa != NULL) {
1433 while (guid == 0 || spa_guid_exists(spa_guid(spa), guid))
1434 guid = spa_get_random(-1ULL);
1435 } else {
1436 while (guid == 0 || spa_guid_exists(guid, 0))
1437 guid = spa_get_random(-1ULL);
34dc7c2f
BB
1438 }
1439
428870ff
BB
1440 return (guid);
1441}
1442
1443void
b0bc7a84 1444snprintf_blkptr(char *buf, size_t buflen, const blkptr_t *bp)
428870ff 1445{
9ae529ec 1446 char type[256];
428870ff
BB
1447 char *checksum = NULL;
1448 char *compress = NULL;
34dc7c2f 1449
428870ff 1450 if (bp != NULL) {
9ae529ec
CS
1451 if (BP_GET_TYPE(bp) & DMU_OT_NEWTYPE) {
1452 dmu_object_byteswap_t bswap =
1453 DMU_OT_BYTESWAP(BP_GET_TYPE(bp));
1454 (void) snprintf(type, sizeof (type), "bswap %s %s",
1455 DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) ?
1456 "metadata" : "data",
1457 dmu_ot_byteswap[bswap].ob_name);
1458 } else {
1459 (void) strlcpy(type, dmu_ot[BP_GET_TYPE(bp)].ot_name,
1460 sizeof (type));
1461 }
9b67f605
MA
1462 if (!BP_IS_EMBEDDED(bp)) {
1463 checksum =
1464 zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name;
1465 }
428870ff 1466 compress = zio_compress_table[BP_GET_COMPRESS(bp)].ci_name;
34dc7c2f
BB
1467 }
1468
b0bc7a84 1469 SNPRINTF_BLKPTR(snprintf, ' ', buf, buflen, bp, type, checksum,
5c27ec10 1470 compress);
34dc7c2f
BB
1471}
1472
1473void
1474spa_freeze(spa_t *spa)
1475{
1476 uint64_t freeze_txg = 0;
1477
b128c09f 1478 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34dc7c2f
BB
1479 if (spa->spa_freeze_txg == UINT64_MAX) {
1480 freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE;
1481 spa->spa_freeze_txg = freeze_txg;
1482 }
b128c09f 1483 spa_config_exit(spa, SCL_ALL, FTAG);
34dc7c2f
BB
1484 if (freeze_txg != 0)
1485 txg_wait_synced(spa_get_dsl(spa), freeze_txg);
1486}
1487
0b39b9f9
PS
1488void
1489zfs_panic_recover(const char *fmt, ...)
1490{
1491 va_list adx;
1492
1493 va_start(adx, fmt);
1494 vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx);
1495 va_end(adx);
1496}
1497
428870ff
BB
1498/*
1499 * This is a stripped-down version of strtoull, suitable only for converting
d3cc8b15 1500 * lowercase hexadecimal numbers that don't overflow.
428870ff
BB
1501 */
1502uint64_t
e19572e4 1503zfs_strtonum(const char *str, char **nptr)
428870ff
BB
1504{
1505 uint64_t val = 0;
1506 char c;
1507 int digit;
1508
1509 while ((c = *str) != '\0') {
1510 if (c >= '0' && c <= '9')
1511 digit = c - '0';
1512 else if (c >= 'a' && c <= 'f')
1513 digit = 10 + c - 'a';
1514 else
1515 break;
1516
1517 val *= 16;
1518 val += digit;
1519
1520 str++;
1521 }
1522
1523 if (nptr)
1524 *nptr = (char *)str;
1525
1526 return (val);
1527}
1528
cc99f275
DB
1529void
1530spa_activate_allocation_classes(spa_t *spa, dmu_tx_t *tx)
1531{
1532 /*
1533 * We bump the feature refcount for each special vdev added to the pool
1534 */
1535 ASSERT(spa_feature_is_enabled(spa, SPA_FEATURE_ALLOCATION_CLASSES));
1536 spa_feature_incr(spa, SPA_FEATURE_ALLOCATION_CLASSES, tx);
1537}
1538
34dc7c2f
BB
1539/*
1540 * ==========================================================================
1541 * Accessor functions
1542 * ==========================================================================
1543 */
1544
b128c09f
BB
1545boolean_t
1546spa_shutting_down(spa_t *spa)
34dc7c2f 1547{
b128c09f 1548 return (spa->spa_async_suspended);
34dc7c2f
BB
1549}
1550
1551dsl_pool_t *
1552spa_get_dsl(spa_t *spa)
1553{
1554 return (spa->spa_dsl_pool);
1555}
1556
9ae529ec
CS
1557boolean_t
1558spa_is_initializing(spa_t *spa)
1559{
1560 return (spa->spa_is_initializing);
1561}
1562
a1d477c2
MA
1563boolean_t
1564spa_indirect_vdevs_loaded(spa_t *spa)
1565{
1566 return (spa->spa_indirect_vdevs_loaded);
1567}
1568
34dc7c2f
BB
1569blkptr_t *
1570spa_get_rootblkptr(spa_t *spa)
1571{
1572 return (&spa->spa_ubsync.ub_rootbp);
1573}
1574
1575void
1576spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp)
1577{
1578 spa->spa_uberblock.ub_rootbp = *bp;
1579}
1580
1581void
1582spa_altroot(spa_t *spa, char *buf, size_t buflen)
1583{
1584 if (spa->spa_root == NULL)
1585 buf[0] = '\0';
1586 else
1587 (void) strncpy(buf, spa->spa_root, buflen);
1588}
1589
1590int
1591spa_sync_pass(spa_t *spa)
1592{
1593 return (spa->spa_sync_pass);
1594}
1595
1596char *
1597spa_name(spa_t *spa)
1598{
34dc7c2f
BB
1599 return (spa->spa_name);
1600}
1601
1602uint64_t
1603spa_guid(spa_t *spa)
1604{
3bc7e0fb
GW
1605 dsl_pool_t *dp = spa_get_dsl(spa);
1606 uint64_t guid;
1607
34dc7c2f
BB
1608 /*
1609 * If we fail to parse the config during spa_load(), we can go through
1610 * the error path (which posts an ereport) and end up here with no root
3541dc6d 1611 * vdev. We stash the original pool guid in 'spa_config_guid' to handle
34dc7c2f
BB
1612 * this case.
1613 */
3bc7e0fb
GW
1614 if (spa->spa_root_vdev == NULL)
1615 return (spa->spa_config_guid);
1616
1617 guid = spa->spa_last_synced_guid != 0 ?
1618 spa->spa_last_synced_guid : spa->spa_root_vdev->vdev_guid;
1619
1620 /*
1621 * Return the most recently synced out guid unless we're
1622 * in syncing context.
1623 */
1624 if (dp && dsl_pool_sync_context(dp))
34dc7c2f
BB
1625 return (spa->spa_root_vdev->vdev_guid);
1626 else
3bc7e0fb 1627 return (guid);
3541dc6d
GA
1628}
1629
1630uint64_t
1631spa_load_guid(spa_t *spa)
1632{
1633 /*
1634 * This is a GUID that exists solely as a reference for the
1635 * purposes of the arc. It is generated at load time, and
1636 * is never written to persistent storage.
1637 */
1638 return (spa->spa_load_guid);
34dc7c2f
BB
1639}
1640
1641uint64_t
1642spa_last_synced_txg(spa_t *spa)
1643{
1644 return (spa->spa_ubsync.ub_txg);
1645}
1646
1647uint64_t
1648spa_first_txg(spa_t *spa)
1649{
1650 return (spa->spa_first_txg);
1651}
1652
428870ff
BB
1653uint64_t
1654spa_syncing_txg(spa_t *spa)
1655{
1656 return (spa->spa_syncing_txg);
1657}
1658
3b7f360c
GW
1659/*
1660 * Return the last txg where data can be dirtied. The final txgs
1661 * will be used to just clear out any deferred frees that remain.
1662 */
1663uint64_t
1664spa_final_dirty_txg(spa_t *spa)
1665{
1666 return (spa->spa_final_txg - TXG_DEFER_SIZE);
1667}
1668
b128c09f 1669pool_state_t
34dc7c2f
BB
1670spa_state(spa_t *spa)
1671{
1672 return (spa->spa_state);
1673}
1674
428870ff
BB
1675spa_load_state_t
1676spa_load_state(spa_t *spa)
34dc7c2f 1677{
428870ff 1678 return (spa->spa_load_state);
34dc7c2f
BB
1679}
1680
34dc7c2f 1681uint64_t
428870ff 1682spa_freeze_txg(spa_t *spa)
34dc7c2f 1683{
428870ff 1684 return (spa->spa_freeze_txg);
34dc7c2f
BB
1685}
1686
047187c1 1687/*
1688 * Return the inflated asize for a logical write in bytes. This is used by the
1689 * DMU to calculate the space a logical write will require on disk.
1690 * If lsize is smaller than the largest physical block size allocatable on this
1691 * pool we use its value instead, since the write will end up using the whole
1692 * block anyway.
1693 */
34dc7c2f 1694uint64_t
3ec3bc21 1695spa_get_worst_case_asize(spa_t *spa, uint64_t lsize)
34dc7c2f 1696{
047187c1 1697 if (lsize == 0)
1698 return (0); /* No inflation needed */
1699 return (MAX(lsize, 1 << spa->spa_max_ashift) * spa_asize_inflation);
34dc7c2f
BB
1700}
1701
3d45fdd6
MA
1702/*
1703 * Return the amount of slop space in bytes. It is 1/32 of the pool (3.2%),
d7958b4c
MA
1704 * or at least 128MB, unless that would cause it to be more than half the
1705 * pool size.
3d45fdd6
MA
1706 *
1707 * See the comment above spa_slop_shift for details.
1708 */
1709uint64_t
4ea3f864
GM
1710spa_get_slop_space(spa_t *spa)
1711{
3d45fdd6 1712 uint64_t space = spa_get_dspace(spa);
d7958b4c 1713 return (MAX(space >> spa_slop_shift, MIN(space >> 1, spa_min_slop)));
3d45fdd6
MA
1714}
1715
34dc7c2f
BB
1716uint64_t
1717spa_get_dspace(spa_t *spa)
1718{
428870ff 1719 return (spa->spa_dspace);
34dc7c2f
BB
1720}
1721
d2734cce
SD
1722uint64_t
1723spa_get_checkpoint_space(spa_t *spa)
1724{
1725 return (spa->spa_checkpoint_info.sci_dspace);
1726}
1727
428870ff
BB
1728void
1729spa_update_dspace(spa_t *spa)
34dc7c2f 1730{
428870ff
BB
1731 spa->spa_dspace = metaslab_class_get_dspace(spa_normal_class(spa)) +
1732 ddt_get_dedup_dspace(spa);
a1d477c2
MA
1733 if (spa->spa_vdev_removal != NULL) {
1734 /*
1735 * We can't allocate from the removing device, so
1736 * subtract its size. This prevents the DMU/DSL from
1737 * filling up the (now smaller) pool while we are in the
1738 * middle of removing the device.
1739 *
1740 * Note that the DMU/DSL doesn't actually know or care
1741 * how much space is allocated (it does its own tracking
1742 * of how much space has been logically used). So it
1743 * doesn't matter that the data we are moving may be
1744 * allocated twice (on the old device and the new
1745 * device).
1746 */
9e052db4
MA
1747 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
1748 vdev_t *vd =
1749 vdev_lookup_top(spa, spa->spa_vdev_removal->svr_vdev_id);
a1d477c2
MA
1750 spa->spa_dspace -= spa_deflate(spa) ?
1751 vd->vdev_stat.vs_dspace : vd->vdev_stat.vs_space;
9e052db4 1752 spa_config_exit(spa, SCL_VDEV, FTAG);
a1d477c2 1753 }
34dc7c2f
BB
1754}
1755
1756/*
1757 * Return the failure mode that has been set to this pool. The default
1758 * behavior will be to block all I/Os when a complete failure occurs.
1759 */
8fb1ede1 1760uint64_t
34dc7c2f
BB
1761spa_get_failmode(spa_t *spa)
1762{
1763 return (spa->spa_failmode);
1764}
1765
b128c09f
BB
1766boolean_t
1767spa_suspended(spa_t *spa)
1768{
cec3a0a1 1769 return (spa->spa_suspended != ZIO_SUSPEND_NONE);
b128c09f
BB
1770}
1771
34dc7c2f
BB
1772uint64_t
1773spa_version(spa_t *spa)
1774{
1775 return (spa->spa_ubsync.ub_version);
1776}
1777
428870ff
BB
1778boolean_t
1779spa_deflate(spa_t *spa)
1780{
1781 return (spa->spa_deflate);
1782}
1783
1784metaslab_class_t *
1785spa_normal_class(spa_t *spa)
1786{
1787 return (spa->spa_normal_class);
1788}
1789
1790metaslab_class_t *
1791spa_log_class(spa_t *spa)
1792{
1793 return (spa->spa_log_class);
1794}
1795
cc99f275
DB
1796metaslab_class_t *
1797spa_special_class(spa_t *spa)
1798{
1799 return (spa->spa_special_class);
1800}
1801
1802metaslab_class_t *
1803spa_dedup_class(spa_t *spa)
1804{
1805 return (spa->spa_dedup_class);
1806}
1807
1808/*
1809 * Locate an appropriate allocation class
1810 */
1811metaslab_class_t *
1812spa_preferred_class(spa_t *spa, uint64_t size, dmu_object_type_t objtype,
1813 uint_t level, uint_t special_smallblk)
1814{
1815 if (DMU_OT_IS_ZIL(objtype)) {
1816 if (spa->spa_log_class->mc_groups != 0)
1817 return (spa_log_class(spa));
1818 else
1819 return (spa_normal_class(spa));
1820 }
1821
1822 boolean_t has_special_class = spa->spa_special_class->mc_groups != 0;
1823
1824 if (DMU_OT_IS_DDT(objtype)) {
1825 if (spa->spa_dedup_class->mc_groups != 0)
1826 return (spa_dedup_class(spa));
1827 else if (has_special_class && zfs_ddt_data_is_special)
1828 return (spa_special_class(spa));
1829 else
1830 return (spa_normal_class(spa));
1831 }
1832
1833 /* Indirect blocks for user data can land in special if allowed */
1834 if (level > 0 && (DMU_OT_IS_FILE(objtype) || objtype == DMU_OT_ZVOL)) {
1835 if (has_special_class && zfs_user_indirect_is_special)
1836 return (spa_special_class(spa));
1837 else
1838 return (spa_normal_class(spa));
1839 }
1840
1841 if (DMU_OT_IS_METADATA(objtype) || level > 0) {
1842 if (has_special_class)
1843 return (spa_special_class(spa));
1844 else
1845 return (spa_normal_class(spa));
1846 }
1847
1848 /*
1849 * Allow small file blocks in special class in some cases (like
1850 * for the dRAID vdev feature). But always leave a reserve of
1851 * zfs_special_class_metadata_reserve_pct exclusively for metadata.
1852 */
1853 if (DMU_OT_IS_FILE(objtype) &&
44170969 1854 has_special_class && size <= special_smallblk) {
cc99f275
DB
1855 metaslab_class_t *special = spa_special_class(spa);
1856 uint64_t alloc = metaslab_class_get_alloc(special);
1857 uint64_t space = metaslab_class_get_space(special);
1858 uint64_t limit =
1859 (space * (100 - zfs_special_class_metadata_reserve_pct))
1860 / 100;
1861
1862 if (alloc < limit)
1863 return (special);
1864 }
1865
1866 return (spa_normal_class(spa));
1867}
1868
0c66c32d
JG
1869void
1870spa_evicting_os_register(spa_t *spa, objset_t *os)
1871{
1872 mutex_enter(&spa->spa_evicting_os_lock);
1873 list_insert_head(&spa->spa_evicting_os_list, os);
1874 mutex_exit(&spa->spa_evicting_os_lock);
1875}
1876
1877void
1878spa_evicting_os_deregister(spa_t *spa, objset_t *os)
1879{
1880 mutex_enter(&spa->spa_evicting_os_lock);
1881 list_remove(&spa->spa_evicting_os_list, os);
1882 cv_broadcast(&spa->spa_evicting_os_cv);
1883 mutex_exit(&spa->spa_evicting_os_lock);
1884}
1885
1886void
1887spa_evicting_os_wait(spa_t *spa)
1888{
1889 mutex_enter(&spa->spa_evicting_os_lock);
1890 while (!list_is_empty(&spa->spa_evicting_os_list))
1891 cv_wait(&spa->spa_evicting_os_cv, &spa->spa_evicting_os_lock);
1892 mutex_exit(&spa->spa_evicting_os_lock);
1893
1894 dmu_buf_user_evict_wait();
1895}
1896
34dc7c2f
BB
1897int
1898spa_max_replication(spa_t *spa)
1899{
1900 /*
1901 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
1902 * handle BPs with more than one DVA allocated. Set our max
1903 * replication level accordingly.
1904 */
1905 if (spa_version(spa) < SPA_VERSION_DITTO_BLOCKS)
1906 return (1);
1907 return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override));
1908}
1909
428870ff
BB
1910int
1911spa_prev_software_version(spa_t *spa)
1912{
1913 return (spa->spa_prev_software_version);
1914}
1915
cc92e9d0
GW
1916uint64_t
1917spa_deadman_synctime(spa_t *spa)
1918{
1919 return (spa->spa_deadman_synctime);
1920}
1921
8fb1ede1
BB
1922uint64_t
1923spa_deadman_ziotime(spa_t *spa)
1924{
1925 return (spa->spa_deadman_ziotime);
1926}
1927
1928uint64_t
1929spa_get_deadman_failmode(spa_t *spa)
1930{
1931 return (spa->spa_deadman_failmode);
1932}
1933
1934void
1935spa_set_deadman_failmode(spa_t *spa, const char *failmode)
1936{
1937 if (strcmp(failmode, "wait") == 0)
1938 spa->spa_deadman_failmode = ZIO_FAILURE_MODE_WAIT;
1939 else if (strcmp(failmode, "continue") == 0)
1940 spa->spa_deadman_failmode = ZIO_FAILURE_MODE_CONTINUE;
1941 else if (strcmp(failmode, "panic") == 0)
1942 spa->spa_deadman_failmode = ZIO_FAILURE_MODE_PANIC;
1943 else
1944 spa->spa_deadman_failmode = ZIO_FAILURE_MODE_WAIT;
1945}
1946
34dc7c2f 1947uint64_t
428870ff 1948dva_get_dsize_sync(spa_t *spa, const dva_t *dva)
34dc7c2f 1949{
428870ff
BB
1950 uint64_t asize = DVA_GET_ASIZE(dva);
1951 uint64_t dsize = asize;
34dc7c2f 1952
428870ff 1953 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
34dc7c2f 1954
428870ff
BB
1955 if (asize != 0 && spa->spa_deflate) {
1956 vdev_t *vd = vdev_lookup_top(spa, DVA_GET_VDEV(dva));
2c33b912
BB
1957 if (vd != NULL)
1958 dsize = (asize >> SPA_MINBLOCKSHIFT) *
1959 vd->vdev_deflate_ratio;
34dc7c2f 1960 }
428870ff
BB
1961
1962 return (dsize);
1963}
1964
1965uint64_t
1966bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp)
1967{
1968 uint64_t dsize = 0;
1969
1c27024e 1970 for (int d = 0; d < BP_GET_NDVAS(bp); d++)
428870ff
BB
1971 dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1972
1973 return (dsize);
1974}
1975
1976uint64_t
1977bp_get_dsize(spa_t *spa, const blkptr_t *bp)
1978{
1979 uint64_t dsize = 0;
1980
1981 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
1982
1c27024e 1983 for (int d = 0; d < BP_GET_NDVAS(bp); d++)
428870ff
BB
1984 dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1985
b128c09f 1986 spa_config_exit(spa, SCL_VDEV, FTAG);
428870ff
BB
1987
1988 return (dsize);
34dc7c2f
BB
1989}
1990
dae3e9ea
DB
1991uint64_t
1992spa_dirty_data(spa_t *spa)
1993{
1994 return (spa->spa_dsl_pool->dp_dirty_total);
1995}
1996
34dc7c2f
BB
1997/*
1998 * ==========================================================================
1999 * Initialization and Termination
2000 * ==========================================================================
2001 */
2002
2003static int
2004spa_name_compare(const void *a1, const void *a2)
2005{
2006 const spa_t *s1 = a1;
2007 const spa_t *s2 = a2;
2008 int s;
2009
2010 s = strcmp(s1->spa_name, s2->spa_name);
ee36c709
GN
2011
2012 return (AVL_ISIGN(s));
34dc7c2f
BB
2013}
2014
34dc7c2f 2015void
0bc8fd78 2016spa_boot_init(void)
34dc7c2f
BB
2017{
2018 spa_config_load();
2019}
2020
2021void
2022spa_init(int mode)
2023{
2024 mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL);
2025 mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL);
2026 mutex_init(&spa_l2cache_lock, NULL, MUTEX_DEFAULT, NULL);
2027 cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL);
2028
2029 avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t),
2030 offsetof(spa_t, spa_avl));
2031
2032 avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_aux_t),
2033 offsetof(spa_aux_t, aux_avl));
2034
2035 avl_create(&spa_l2cache_avl, spa_l2cache_compare, sizeof (spa_aux_t),
2036 offsetof(spa_aux_t, aux_avl));
2037
fb5f0bc8 2038 spa_mode_global = mode;
34dc7c2f 2039
498877ba
MA
2040#ifndef _KERNEL
2041 if (spa_mode_global != FREAD && dprintf_find_string("watch")) {
2042 struct sigaction sa;
2043
2044 sa.sa_flags = SA_SIGINFO;
2045 sigemptyset(&sa.sa_mask);
2046 sa.sa_sigaction = arc_buf_sigsegv;
2047
2048 if (sigaction(SIGSEGV, &sa, NULL) == -1) {
2049 perror("could not enable watchpoints: "
2050 "sigaction(SIGSEGV, ...) = ");
2051 } else {
2052 arc_watch = B_TRUE;
2053 }
2054 }
2055#endif
2056
26685276 2057 fm_init();
424fd7c3 2058 zfs_refcount_init();
34dc7c2f 2059 unique_init();
93cf2076 2060 range_tree_init();
4e21fd06 2061 metaslab_alloc_trace_init();
ecf3d9b8 2062 ddt_init();
34dc7c2f
BB
2063 zio_init();
2064 dmu_init();
2065 zil_init();
2066 vdev_cache_stat_init();
551905dd 2067 vdev_mirror_stat_init();
ab9f4b0b 2068 vdev_raidz_math_init();
da8f51e1 2069 vdev_file_init();
34dc7c2f
BB
2070 zfs_prop_init();
2071 zpool_prop_init();
9ae529ec 2072 zpool_feature_init();
34dc7c2f 2073 spa_config_load();
b128c09f 2074 l2arc_start();
d4a72f23 2075 scan_init();
6a9d6359 2076 qat_init();
34dc7c2f
BB
2077}
2078
2079void
2080spa_fini(void)
2081{
b128c09f
BB
2082 l2arc_stop();
2083
34dc7c2f
BB
2084 spa_evict_all();
2085
da8f51e1 2086 vdev_file_fini();
34dc7c2f 2087 vdev_cache_stat_fini();
551905dd 2088 vdev_mirror_stat_fini();
ab9f4b0b 2089 vdev_raidz_math_fini();
34dc7c2f
BB
2090 zil_fini();
2091 dmu_fini();
2092 zio_fini();
ecf3d9b8 2093 ddt_fini();
4e21fd06 2094 metaslab_alloc_trace_fini();
93cf2076 2095 range_tree_fini();
34dc7c2f 2096 unique_fini();
424fd7c3 2097 zfs_refcount_fini();
26685276 2098 fm_fini();
d4a72f23 2099 scan_fini();
6a9d6359 2100 qat_fini();
34dc7c2f
BB
2101
2102 avl_destroy(&spa_namespace_avl);
2103 avl_destroy(&spa_spare_avl);
2104 avl_destroy(&spa_l2cache_avl);
2105
2106 cv_destroy(&spa_namespace_cv);
2107 mutex_destroy(&spa_namespace_lock);
2108 mutex_destroy(&spa_spare_lock);
2109 mutex_destroy(&spa_l2cache_lock);
2110}
2111
2112/*
2113 * Return whether this pool has slogs. No locking needed.
2114 * It's not a problem if the wrong answer is returned as it's only for
2115 * performance and not correctness
2116 */
2117boolean_t
2118spa_has_slogs(spa_t *spa)
2119{
2120 return (spa->spa_log_class->mc_rotor != NULL);
2121}
b128c09f 2122
428870ff
BB
2123spa_log_state_t
2124spa_get_log_state(spa_t *spa)
2125{
2126 return (spa->spa_log_state);
2127}
2128
2129void
2130spa_set_log_state(spa_t *spa, spa_log_state_t state)
2131{
2132 spa->spa_log_state = state;
2133}
2134
b128c09f
BB
2135boolean_t
2136spa_is_root(spa_t *spa)
2137{
2138 return (spa->spa_is_root);
2139}
fb5f0bc8
BB
2140
2141boolean_t
2142spa_writeable(spa_t *spa)
2143{
6cb8e530 2144 return (!!(spa->spa_mode & FWRITE) && spa->spa_trust_config);
fb5f0bc8
BB
2145}
2146
acbad6ff
AR
2147/*
2148 * Returns true if there is a pending sync task in any of the current
2149 * syncing txg, the current quiescing txg, or the current open txg.
2150 */
2151boolean_t
2152spa_has_pending_synctask(spa_t *spa)
2153{
d2734cce
SD
2154 return (!txg_all_lists_empty(&spa->spa_dsl_pool->dp_sync_tasks) ||
2155 !txg_all_lists_empty(&spa->spa_dsl_pool->dp_early_sync_tasks));
acbad6ff
AR
2156}
2157
fb5f0bc8
BB
2158int
2159spa_mode(spa_t *spa)
2160{
2161 return (spa->spa_mode);
2162}
428870ff
BB
2163
2164uint64_t
2165spa_bootfs(spa_t *spa)
2166{
2167 return (spa->spa_bootfs);
2168}
2169
2170uint64_t
2171spa_delegation(spa_t *spa)
2172{
2173 return (spa->spa_delegation);
2174}
2175
2176objset_t *
2177spa_meta_objset(spa_t *spa)
2178{
2179 return (spa->spa_meta_objset);
2180}
2181
2182enum zio_checksum
2183spa_dedup_checksum(spa_t *spa)
2184{
2185 return (spa->spa_dedup_checksum);
2186}
2187
2188/*
2189 * Reset pool scan stat per scan pass (or reboot).
2190 */
2191void
2192spa_scan_stat_init(spa_t *spa)
2193{
2194 /* data not stored on disk */
2195 spa->spa_scan_pass_start = gethrestime_sec();
0ea05c64
AP
2196 if (dsl_scan_is_paused_scrub(spa->spa_dsl_pool->dp_scan))
2197 spa->spa_scan_pass_scrub_pause = spa->spa_scan_pass_start;
2198 else
2199 spa->spa_scan_pass_scrub_pause = 0;
2200 spa->spa_scan_pass_scrub_spent_paused = 0;
428870ff 2201 spa->spa_scan_pass_exam = 0;
d4a72f23 2202 spa->spa_scan_pass_issued = 0;
428870ff
BB
2203 vdev_scan_stat_init(spa->spa_root_vdev);
2204}
2205
2206/*
2207 * Get scan stats for zpool status reports
2208 */
2209int
2210spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps)
2211{
2212 dsl_scan_t *scn = spa->spa_dsl_pool ? spa->spa_dsl_pool->dp_scan : NULL;
2213
2214 if (scn == NULL || scn->scn_phys.scn_func == POOL_SCAN_NONE)
2e528b49 2215 return (SET_ERROR(ENOENT));
428870ff
BB
2216 bzero(ps, sizeof (pool_scan_stat_t));
2217
2218 /* data stored on disk */
2219 ps->pss_func = scn->scn_phys.scn_func;
d4a72f23 2220 ps->pss_state = scn->scn_phys.scn_state;
428870ff
BB
2221 ps->pss_start_time = scn->scn_phys.scn_start_time;
2222 ps->pss_end_time = scn->scn_phys.scn_end_time;
2223 ps->pss_to_examine = scn->scn_phys.scn_to_examine;
d4677269 2224 ps->pss_examined = scn->scn_phys.scn_examined;
428870ff
BB
2225 ps->pss_to_process = scn->scn_phys.scn_to_process;
2226 ps->pss_processed = scn->scn_phys.scn_processed;
2227 ps->pss_errors = scn->scn_phys.scn_errors;
428870ff
BB
2228
2229 /* data not stored on disk */
428870ff 2230 ps->pss_pass_exam = spa->spa_scan_pass_exam;
d4677269 2231 ps->pss_pass_start = spa->spa_scan_pass_start;
0ea05c64
AP
2232 ps->pss_pass_scrub_pause = spa->spa_scan_pass_scrub_pause;
2233 ps->pss_pass_scrub_spent_paused = spa->spa_scan_pass_scrub_spent_paused;
d4677269
TC
2234 ps->pss_pass_issued = spa->spa_scan_pass_issued;
2235 ps->pss_issued =
2236 scn->scn_issued_before_pass + spa->spa_scan_pass_issued;
428870ff
BB
2237
2238 return (0);
2239}
c28b2279 2240
f1512ee6
MA
2241int
2242spa_maxblocksize(spa_t *spa)
2243{
2244 if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS))
2245 return (SPA_MAXBLOCKSIZE);
2246 else
2247 return (SPA_OLD_MAXBLOCKSIZE);
2248}
2249
a1d477c2
MA
2250
2251/*
2252 * Returns the txg that the last device removal completed. No indirect mappings
2253 * have been added since this txg.
2254 */
2255uint64_t
2256spa_get_last_removal_txg(spa_t *spa)
2257{
2258 uint64_t vdevid;
2259 uint64_t ret = -1ULL;
2260
2261 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2262 /*
2263 * sr_prev_indirect_vdev is only modified while holding all the
2264 * config locks, so it is sufficient to hold SCL_VDEV as reader when
2265 * examining it.
2266 */
2267 vdevid = spa->spa_removing_phys.sr_prev_indirect_vdev;
2268
2269 while (vdevid != -1ULL) {
2270 vdev_t *vd = vdev_lookup_top(spa, vdevid);
2271 vdev_indirect_births_t *vib = vd->vdev_indirect_births;
2272
2273 ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops);
2274
2275 /*
2276 * If the removal did not remap any data, we don't care.
2277 */
2278 if (vdev_indirect_births_count(vib) != 0) {
2279 ret = vdev_indirect_births_last_entry_txg(vib);
2280 break;
2281 }
2282
2283 vdevid = vd->vdev_indirect_config.vic_prev_indirect_vdev;
2284 }
2285 spa_config_exit(spa, SCL_VDEV, FTAG);
2286
2287 IMPLY(ret != -1ULL,
2288 spa_feature_is_active(spa, SPA_FEATURE_DEVICE_REMOVAL));
2289
2290 return (ret);
2291}
2292
50c957f7
NB
2293int
2294spa_maxdnodesize(spa_t *spa)
2295{
2296 if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_DNODE))
2297 return (DNODE_MAX_SIZE);
2298 else
2299 return (DNODE_MIN_SIZE);
2300}
2301
379ca9cf
OF
2302boolean_t
2303spa_multihost(spa_t *spa)
2304{
2305 return (spa->spa_multihost ? B_TRUE : B_FALSE);
2306}
2307
2308unsigned long
2309spa_get_hostid(void)
2310{
2311 unsigned long myhostid;
2312
2313#ifdef _KERNEL
2314 myhostid = zone_get_hostid(NULL);
2315#else /* _KERNEL */
2316 /*
2317 * We're emulating the system's hostid in userland, so
2318 * we can't use zone_get_hostid().
2319 */
2320 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
2321#endif /* _KERNEL */
2322
2323 return (myhostid);
2324}
2325
6cb8e530
PZ
2326boolean_t
2327spa_trust_config(spa_t *spa)
2328{
2329 return (spa->spa_trust_config);
2330}
2331
2332uint64_t
2333spa_missing_tvds_allowed(spa_t *spa)
2334{
2335 return (spa->spa_missing_tvds_allowed);
2336}
2337
2338void
2339spa_set_missing_tvds(spa_t *spa, uint64_t missing)
2340{
2341 spa->spa_missing_tvds = missing;
2342}
2343
f0ed6c74
TH
2344/*
2345 * Return the pool state string ("ONLINE", "DEGRADED", "SUSPENDED", etc).
2346 */
2347const char *
2348spa_state_to_name(spa_t *spa)
2349{
2350 vdev_state_t state = spa->spa_root_vdev->vdev_state;
2351 vdev_aux_t aux = spa->spa_root_vdev->vdev_stat.vs_aux;
2352
2353 if (spa_suspended(spa) &&
2354 (spa_get_failmode(spa) != ZIO_FAILURE_MODE_CONTINUE))
2355 return ("SUSPENDED");
2356
2357 switch (state) {
2358 case VDEV_STATE_CLOSED:
2359 case VDEV_STATE_OFFLINE:
2360 return ("OFFLINE");
2361 case VDEV_STATE_REMOVED:
2362 return ("REMOVED");
2363 case VDEV_STATE_CANT_OPEN:
2364 if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
2365 return ("FAULTED");
2366 else if (aux == VDEV_AUX_SPLIT_POOL)
2367 return ("SPLIT");
2368 else
2369 return ("UNAVAIL");
2370 case VDEV_STATE_FAULTED:
2371 return ("FAULTED");
2372 case VDEV_STATE_DEGRADED:
2373 return ("DEGRADED");
2374 case VDEV_STATE_HEALTHY:
2375 return ("ONLINE");
2376 default:
2377 break;
2378 }
2379
2380 return ("UNKNOWN");
2381}
2382
d2734cce
SD
2383boolean_t
2384spa_top_vdevs_spacemap_addressable(spa_t *spa)
2385{
2386 vdev_t *rvd = spa->spa_root_vdev;
2387 for (uint64_t c = 0; c < rvd->vdev_children; c++) {
2388 if (!vdev_is_spacemap_addressable(rvd->vdev_child[c]))
2389 return (B_FALSE);
2390 }
2391 return (B_TRUE);
2392}
2393
2394boolean_t
2395spa_has_checkpoint(spa_t *spa)
2396{
2397 return (spa->spa_checkpoint_txg != 0);
2398}
2399
2400boolean_t
2401spa_importing_readonly_checkpoint(spa_t *spa)
2402{
2403 return ((spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT) &&
2404 spa->spa_mode == FREAD);
2405}
2406
2407uint64_t
2408spa_min_claim_txg(spa_t *spa)
2409{
2410 uint64_t checkpoint_txg = spa->spa_uberblock.ub_checkpoint_txg;
2411
2412 if (checkpoint_txg != 0)
2413 return (checkpoint_txg + 1);
2414
2415 return (spa->spa_first_txg);
2416}
2417
2418/*
2419 * If there is a checkpoint, async destroys may consume more space from
2420 * the pool instead of freeing it. In an attempt to save the pool from
2421 * getting suspended when it is about to run out of space, we stop
2422 * processing async destroys.
2423 */
2424boolean_t
2425spa_suspend_async_destroy(spa_t *spa)
2426{
2427 dsl_pool_t *dp = spa_get_dsl(spa);
2428
2429 uint64_t unreserved = dsl_pool_unreserved_space(dp,
2430 ZFS_SPACE_CHECK_EXTRA_RESERVED);
2431 uint64_t used = dsl_dir_phys(dp->dp_root_dir)->dd_used_bytes;
2432 uint64_t avail = (unreserved > used) ? (unreserved - used) : 0;
2433
2434 if (spa_has_checkpoint(spa) && avail == 0)
2435 return (B_TRUE);
2436
2437 return (B_FALSE);
2438}
2439
93ce2b4c 2440#if defined(_KERNEL)
8fb1ede1
BB
2441
2442#include <linux/mod_compat.h>
2443
2444static int
2445param_set_deadman_failmode(const char *val, zfs_kernel_param_t *kp)
2446{
2447 spa_t *spa = NULL;
2448 char *p;
2449
2450 if (val == NULL)
2451 return (SET_ERROR(-EINVAL));
2452
2453 if ((p = strchr(val, '\n')) != NULL)
2454 *p = '\0';
2455
2456 if (strcmp(val, "wait") != 0 && strcmp(val, "continue") != 0 &&
2457 strcmp(val, "panic"))
2458 return (SET_ERROR(-EINVAL));
2459
d1043e2f
TC
2460 if (spa_mode_global != 0) {
2461 mutex_enter(&spa_namespace_lock);
2462 while ((spa = spa_next(spa)) != NULL)
2463 spa_set_deadman_failmode(spa, val);
2464 mutex_exit(&spa_namespace_lock);
2465 }
8fb1ede1
BB
2466
2467 return (param_set_charp(val, kp));
2468}
2469
d1043e2f
TC
2470static int
2471param_set_deadman_ziotime(const char *val, zfs_kernel_param_t *kp)
2472{
2473 spa_t *spa = NULL;
2474 int error;
2475
2476 error = param_set_ulong(val, kp);
2477 if (error < 0)
2478 return (SET_ERROR(error));
2479
2480 if (spa_mode_global != 0) {
2481 mutex_enter(&spa_namespace_lock);
2482 while ((spa = spa_next(spa)) != NULL)
2483 spa->spa_deadman_ziotime =
2484 MSEC2NSEC(zfs_deadman_ziotime_ms);
2485 mutex_exit(&spa_namespace_lock);
2486 }
2487
2488 return (0);
2489}
2490
2491static int
2492param_set_deadman_synctime(const char *val, zfs_kernel_param_t *kp)
2493{
2494 spa_t *spa = NULL;
2495 int error;
2496
2497 error = param_set_ulong(val, kp);
2498 if (error < 0)
2499 return (SET_ERROR(error));
2500
2501 if (spa_mode_global != 0) {
2502 mutex_enter(&spa_namespace_lock);
2503 while ((spa = spa_next(spa)) != NULL)
2504 spa->spa_deadman_synctime =
2505 MSEC2NSEC(zfs_deadman_synctime_ms);
2506 mutex_exit(&spa_namespace_lock);
2507 }
2508
2509 return (0);
2510}
2511
7522a260 2512static int
2513param_set_slop_shift(const char *buf, zfs_kernel_param_t *kp)
2514{
2515 unsigned long val;
2516 int error;
2517
2518 error = kstrtoul(buf, 0, &val);
2519 if (error)
2520 return (SET_ERROR(error));
2521
2522 if (val < 1 || val > 31)
2523 return (SET_ERROR(-EINVAL));
2524
2525 error = param_set_int(buf, kp);
2526 if (error < 0)
2527 return (SET_ERROR(error));
2528
2529 return (0);
2530}
2531
c28b2279
BB
2532/* Namespace manipulation */
2533EXPORT_SYMBOL(spa_lookup);
2534EXPORT_SYMBOL(spa_add);
2535EXPORT_SYMBOL(spa_remove);
2536EXPORT_SYMBOL(spa_next);
2537
2538/* Refcount functions */
2539EXPORT_SYMBOL(spa_open_ref);
2540EXPORT_SYMBOL(spa_close);
2541EXPORT_SYMBOL(spa_refcount_zero);
2542
2543/* Pool configuration lock */
2544EXPORT_SYMBOL(spa_config_tryenter);
2545EXPORT_SYMBOL(spa_config_enter);
2546EXPORT_SYMBOL(spa_config_exit);
2547EXPORT_SYMBOL(spa_config_held);
2548
2549/* Pool vdev add/remove lock */
2550EXPORT_SYMBOL(spa_vdev_enter);
2551EXPORT_SYMBOL(spa_vdev_exit);
2552
2553/* Pool vdev state change lock */
2554EXPORT_SYMBOL(spa_vdev_state_enter);
2555EXPORT_SYMBOL(spa_vdev_state_exit);
2556
2557/* Accessor functions */
2558EXPORT_SYMBOL(spa_shutting_down);
2559EXPORT_SYMBOL(spa_get_dsl);
2560EXPORT_SYMBOL(spa_get_rootblkptr);
2561EXPORT_SYMBOL(spa_set_rootblkptr);
2562EXPORT_SYMBOL(spa_altroot);
2563EXPORT_SYMBOL(spa_sync_pass);
2564EXPORT_SYMBOL(spa_name);
2565EXPORT_SYMBOL(spa_guid);
2566EXPORT_SYMBOL(spa_last_synced_txg);
2567EXPORT_SYMBOL(spa_first_txg);
2568EXPORT_SYMBOL(spa_syncing_txg);
2569EXPORT_SYMBOL(spa_version);
2570EXPORT_SYMBOL(spa_state);
2571EXPORT_SYMBOL(spa_load_state);
2572EXPORT_SYMBOL(spa_freeze_txg);
c28b2279
BB
2573EXPORT_SYMBOL(spa_get_dspace);
2574EXPORT_SYMBOL(spa_update_dspace);
2575EXPORT_SYMBOL(spa_deflate);
2576EXPORT_SYMBOL(spa_normal_class);
2577EXPORT_SYMBOL(spa_log_class);
cc99f275
DB
2578EXPORT_SYMBOL(spa_special_class);
2579EXPORT_SYMBOL(spa_preferred_class);
c28b2279
BB
2580EXPORT_SYMBOL(spa_max_replication);
2581EXPORT_SYMBOL(spa_prev_software_version);
2582EXPORT_SYMBOL(spa_get_failmode);
2583EXPORT_SYMBOL(spa_suspended);
2584EXPORT_SYMBOL(spa_bootfs);
2585EXPORT_SYMBOL(spa_delegation);
2586EXPORT_SYMBOL(spa_meta_objset);
f1512ee6 2587EXPORT_SYMBOL(spa_maxblocksize);
50c957f7 2588EXPORT_SYMBOL(spa_maxdnodesize);
c28b2279
BB
2589
2590/* Miscellaneous support routines */
c28b2279
BB
2591EXPORT_SYMBOL(spa_guid_exists);
2592EXPORT_SYMBOL(spa_strdup);
2593EXPORT_SYMBOL(spa_strfree);
2594EXPORT_SYMBOL(spa_get_random);
2595EXPORT_SYMBOL(spa_generate_guid);
b0bc7a84 2596EXPORT_SYMBOL(snprintf_blkptr);
c28b2279
BB
2597EXPORT_SYMBOL(spa_freeze);
2598EXPORT_SYMBOL(spa_upgrade);
2599EXPORT_SYMBOL(spa_evict_all);
2600EXPORT_SYMBOL(spa_lookup_by_guid);
2601EXPORT_SYMBOL(spa_has_spare);
2602EXPORT_SYMBOL(dva_get_dsize_sync);
2603EXPORT_SYMBOL(bp_get_dsize_sync);
2604EXPORT_SYMBOL(bp_get_dsize);
2605EXPORT_SYMBOL(spa_has_slogs);
2606EXPORT_SYMBOL(spa_is_root);
2607EXPORT_SYMBOL(spa_writeable);
2608EXPORT_SYMBOL(spa_mode);
c28b2279 2609EXPORT_SYMBOL(spa_namespace_lock);
6cb8e530
PZ
2610EXPORT_SYMBOL(spa_trust_config);
2611EXPORT_SYMBOL(spa_missing_tvds_allowed);
2612EXPORT_SYMBOL(spa_set_missing_tvds);
f0ed6c74 2613EXPORT_SYMBOL(spa_state_to_name);
d2734cce
SD
2614EXPORT_SYMBOL(spa_importing_readonly_checkpoint);
2615EXPORT_SYMBOL(spa_min_claim_txg);
2616EXPORT_SYMBOL(spa_suspend_async_destroy);
2617EXPORT_SYMBOL(spa_has_checkpoint);
2618EXPORT_SYMBOL(spa_top_vdevs_spacemap_addressable);
cc92e9d0 2619
02730c33 2620/* BEGIN CSTYLED */
33b6dbbc 2621module_param(zfs_flags, uint, 0644);
0b39b9f9
PS
2622MODULE_PARM_DESC(zfs_flags, "Set additional debugging flags");
2623
2624module_param(zfs_recover, int, 0644);
2625MODULE_PARM_DESC(zfs_recover, "Set to attempt to recover from fatal errors");
2626
2627module_param(zfs_free_leak_on_eio, int, 0644);
2628MODULE_PARM_DESC(zfs_free_leak_on_eio,
2629 "Set to ignore IO errors during free and permanently leak the space");
2630
d1043e2f
TC
2631module_param_call(zfs_deadman_synctime_ms, param_set_deadman_synctime,
2632 param_get_ulong, &zfs_deadman_synctime_ms, 0644);
8fb1ede1
BB
2633MODULE_PARM_DESC(zfs_deadman_synctime_ms,
2634 "Pool sync expiration time in milliseconds");
2635
d1043e2f
TC
2636module_param_call(zfs_deadman_ziotime_ms, param_set_deadman_ziotime,
2637 param_get_ulong, &zfs_deadman_ziotime_ms, 0644);
8fb1ede1
BB
2638MODULE_PARM_DESC(zfs_deadman_ziotime_ms,
2639 "IO expiration time in milliseconds");
cc92e9d0 2640
b81a3ddc
TC
2641module_param(zfs_deadman_checktime_ms, ulong, 0644);
2642MODULE_PARM_DESC(zfs_deadman_checktime_ms,
2643 "Dead I/O check interval in milliseconds");
2644
cc92e9d0
GW
2645module_param(zfs_deadman_enabled, int, 0644);
2646MODULE_PARM_DESC(zfs_deadman_enabled, "Enable deadman timer");
e8b96c60 2647
8fb1ede1
BB
2648module_param_call(zfs_deadman_failmode, param_set_deadman_failmode,
2649 param_get_charp, &zfs_deadman_failmode, 0644);
2650MODULE_PARM_DESC(zfs_deadman_failmode, "Failmode for deadman timer");
2651
e8b96c60
MA
2652module_param(spa_asize_inflation, int, 0644);
2653MODULE_PARM_DESC(spa_asize_inflation,
d1d7e268 2654 "SPA size estimate multiplication factor");
6cde6435 2655
7522a260 2656module_param_call(spa_slop_shift, param_set_slop_shift, param_get_int,
2657 &spa_slop_shift, 0644);
6cde6435 2658MODULE_PARM_DESC(spa_slop_shift, "Reserved free space in pool");
cc99f275
DB
2659
2660module_param(zfs_ddt_data_is_special, int, 0644);
2661MODULE_PARM_DESC(zfs_ddt_data_is_special,
2662 "Place DDT data into the special class");
2663
2664module_param(zfs_user_indirect_is_special, int, 0644);
2665MODULE_PARM_DESC(zfs_user_indirect_is_special,
2666 "Place user data indirect blocks into the special class");
02730c33 2667/* END CSTYLED */
c28b2279 2668#endif