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