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