]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/spa_misc.c
Add device rebuild feature
[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 = 600000UL;
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 = 300000UL;
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 = 60000UL;
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 static 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 static 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 static 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 static 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 static 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 * The same as spa_vdev_enter() above but additionally takes the guid of
1170 * the vdev being detached. When there is a rebuild in process it will be
1171 * suspended while the vdev tree is modified then resumed by spa_vdev_exit().
1172 * The rebuild is canceled if only a single child remains after the detach.
1173 */
1174 uint64_t
1175 spa_vdev_detach_enter(spa_t *spa, uint64_t guid)
1176 {
1177 mutex_enter(&spa->spa_vdev_top_lock);
1178 mutex_enter(&spa_namespace_lock);
1179
1180 vdev_autotrim_stop_all(spa);
1181
1182 if (guid != 0) {
1183 vdev_t *vd = spa_lookup_by_guid(spa, guid, B_FALSE);
1184 if (vd) {
1185 vdev_rebuild_stop_wait(vd->vdev_top);
1186 }
1187 }
1188
1189 return (spa_vdev_config_enter(spa));
1190 }
1191
1192 /*
1193 * Internal implementation for spa_vdev_enter(). Used when a vdev
1194 * operation requires multiple syncs (i.e. removing a device) while
1195 * keeping the spa_namespace_lock held.
1196 */
1197 uint64_t
1198 spa_vdev_config_enter(spa_t *spa)
1199 {
1200 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1201
1202 spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
1203
1204 return (spa_last_synced_txg(spa) + 1);
1205 }
1206
1207 /*
1208 * Used in combination with spa_vdev_config_enter() to allow the syncing
1209 * of multiple transactions without releasing the spa_namespace_lock.
1210 */
1211 void
1212 spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error, char *tag)
1213 {
1214 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1215
1216 int config_changed = B_FALSE;
1217
1218 ASSERT(txg > spa_last_synced_txg(spa));
1219
1220 spa->spa_pending_vdev = NULL;
1221
1222 /*
1223 * Reassess the DTLs.
1224 */
1225 vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE, B_FALSE);
1226
1227 if (error == 0 && !list_is_empty(&spa->spa_config_dirty_list)) {
1228 config_changed = B_TRUE;
1229 spa->spa_config_generation++;
1230 }
1231
1232 /*
1233 * Verify the metaslab classes.
1234 */
1235 ASSERT(metaslab_class_validate(spa_normal_class(spa)) == 0);
1236 ASSERT(metaslab_class_validate(spa_log_class(spa)) == 0);
1237 ASSERT(metaslab_class_validate(spa_special_class(spa)) == 0);
1238 ASSERT(metaslab_class_validate(spa_dedup_class(spa)) == 0);
1239
1240 spa_config_exit(spa, SCL_ALL, spa);
1241
1242 /*
1243 * Panic the system if the specified tag requires it. This
1244 * is useful for ensuring that configurations are updated
1245 * transactionally.
1246 */
1247 if (zio_injection_enabled)
1248 zio_handle_panic_injection(spa, tag, 0);
1249
1250 /*
1251 * Note: this txg_wait_synced() is important because it ensures
1252 * that there won't be more than one config change per txg.
1253 * This allows us to use the txg as the generation number.
1254 */
1255 if (error == 0)
1256 txg_wait_synced(spa->spa_dsl_pool, txg);
1257
1258 if (vd != NULL) {
1259 ASSERT(!vd->vdev_detached || vd->vdev_dtl_sm == NULL);
1260 if (vd->vdev_ops->vdev_op_leaf) {
1261 mutex_enter(&vd->vdev_initialize_lock);
1262 vdev_initialize_stop(vd, VDEV_INITIALIZE_CANCELED,
1263 NULL);
1264 mutex_exit(&vd->vdev_initialize_lock);
1265
1266 mutex_enter(&vd->vdev_trim_lock);
1267 vdev_trim_stop(vd, VDEV_TRIM_CANCELED, NULL);
1268 mutex_exit(&vd->vdev_trim_lock);
1269 }
1270
1271 /*
1272 * The vdev may be both a leaf and top-level device.
1273 */
1274 vdev_autotrim_stop_wait(vd);
1275
1276 spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
1277 vdev_free(vd);
1278 spa_config_exit(spa, SCL_ALL, spa);
1279 }
1280
1281 /*
1282 * If the config changed, update the config cache.
1283 */
1284 if (config_changed)
1285 spa_write_cachefile(spa, B_FALSE, B_TRUE);
1286 }
1287
1288 /*
1289 * Unlock the spa_t after adding or removing a vdev. Besides undoing the
1290 * locking of spa_vdev_enter(), we also want make sure the transactions have
1291 * synced to disk, and then update the global configuration cache with the new
1292 * information.
1293 */
1294 int
1295 spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
1296 {
1297 vdev_autotrim_restart(spa);
1298 vdev_rebuild_restart(spa);
1299
1300 spa_vdev_config_exit(spa, vd, txg, error, FTAG);
1301 mutex_exit(&spa_namespace_lock);
1302 mutex_exit(&spa->spa_vdev_top_lock);
1303
1304 return (error);
1305 }
1306
1307 /*
1308 * Lock the given spa_t for the purpose of changing vdev state.
1309 */
1310 void
1311 spa_vdev_state_enter(spa_t *spa, int oplocks)
1312 {
1313 int locks = SCL_STATE_ALL | oplocks;
1314
1315 /*
1316 * Root pools may need to read of the underlying devfs filesystem
1317 * when opening up a vdev. Unfortunately if we're holding the
1318 * SCL_ZIO lock it will result in a deadlock when we try to issue
1319 * the read from the root filesystem. Instead we "prefetch"
1320 * the associated vnodes that we need prior to opening the
1321 * underlying devices and cache them so that we can prevent
1322 * any I/O when we are doing the actual open.
1323 */
1324 if (spa_is_root(spa)) {
1325 int low = locks & ~(SCL_ZIO - 1);
1326 int high = locks & ~low;
1327
1328 spa_config_enter(spa, high, spa, RW_WRITER);
1329 vdev_hold(spa->spa_root_vdev);
1330 spa_config_enter(spa, low, spa, RW_WRITER);
1331 } else {
1332 spa_config_enter(spa, locks, spa, RW_WRITER);
1333 }
1334 spa->spa_vdev_locks = locks;
1335 }
1336
1337 int
1338 spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error)
1339 {
1340 boolean_t config_changed = B_FALSE;
1341 vdev_t *vdev_top;
1342
1343 if (vd == NULL || vd == spa->spa_root_vdev) {
1344 vdev_top = spa->spa_root_vdev;
1345 } else {
1346 vdev_top = vd->vdev_top;
1347 }
1348
1349 if (vd != NULL || error == 0)
1350 vdev_dtl_reassess(vdev_top, 0, 0, B_FALSE, B_FALSE);
1351
1352 if (vd != NULL) {
1353 if (vd != spa->spa_root_vdev)
1354 vdev_state_dirty(vdev_top);
1355
1356 config_changed = B_TRUE;
1357 spa->spa_config_generation++;
1358 }
1359
1360 if (spa_is_root(spa))
1361 vdev_rele(spa->spa_root_vdev);
1362
1363 ASSERT3U(spa->spa_vdev_locks, >=, SCL_STATE_ALL);
1364 spa_config_exit(spa, spa->spa_vdev_locks, spa);
1365
1366 /*
1367 * If anything changed, wait for it to sync. This ensures that,
1368 * from the system administrator's perspective, zpool(1M) commands
1369 * are synchronous. This is important for things like zpool offline:
1370 * when the command completes, you expect no further I/O from ZFS.
1371 */
1372 if (vd != NULL)
1373 txg_wait_synced(spa->spa_dsl_pool, 0);
1374
1375 /*
1376 * If the config changed, update the config cache.
1377 */
1378 if (config_changed) {
1379 mutex_enter(&spa_namespace_lock);
1380 spa_write_cachefile(spa, B_FALSE, B_TRUE);
1381 mutex_exit(&spa_namespace_lock);
1382 }
1383
1384 return (error);
1385 }
1386
1387 /*
1388 * ==========================================================================
1389 * Miscellaneous functions
1390 * ==========================================================================
1391 */
1392
1393 void
1394 spa_activate_mos_feature(spa_t *spa, const char *feature, dmu_tx_t *tx)
1395 {
1396 if (!nvlist_exists(spa->spa_label_features, feature)) {
1397 fnvlist_add_boolean(spa->spa_label_features, feature);
1398 /*
1399 * When we are creating the pool (tx_txg==TXG_INITIAL), we can't
1400 * dirty the vdev config because lock SCL_CONFIG is not held.
1401 * Thankfully, in this case we don't need to dirty the config
1402 * because it will be written out anyway when we finish
1403 * creating the pool.
1404 */
1405 if (tx->tx_txg != TXG_INITIAL)
1406 vdev_config_dirty(spa->spa_root_vdev);
1407 }
1408 }
1409
1410 void
1411 spa_deactivate_mos_feature(spa_t *spa, const char *feature)
1412 {
1413 if (nvlist_remove_all(spa->spa_label_features, feature) == 0)
1414 vdev_config_dirty(spa->spa_root_vdev);
1415 }
1416
1417 /*
1418 * Return the spa_t associated with given pool_guid, if it exists. If
1419 * device_guid is non-zero, determine whether the pool exists *and* contains
1420 * a device with the specified device_guid.
1421 */
1422 spa_t *
1423 spa_by_guid(uint64_t pool_guid, uint64_t device_guid)
1424 {
1425 spa_t *spa;
1426 avl_tree_t *t = &spa_namespace_avl;
1427
1428 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1429
1430 for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) {
1431 if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1432 continue;
1433 if (spa->spa_root_vdev == NULL)
1434 continue;
1435 if (spa_guid(spa) == pool_guid) {
1436 if (device_guid == 0)
1437 break;
1438
1439 if (vdev_lookup_by_guid(spa->spa_root_vdev,
1440 device_guid) != NULL)
1441 break;
1442
1443 /*
1444 * Check any devices we may be in the process of adding.
1445 */
1446 if (spa->spa_pending_vdev) {
1447 if (vdev_lookup_by_guid(spa->spa_pending_vdev,
1448 device_guid) != NULL)
1449 break;
1450 }
1451 }
1452 }
1453
1454 return (spa);
1455 }
1456
1457 /*
1458 * Determine whether a pool with the given pool_guid exists.
1459 */
1460 boolean_t
1461 spa_guid_exists(uint64_t pool_guid, uint64_t device_guid)
1462 {
1463 return (spa_by_guid(pool_guid, device_guid) != NULL);
1464 }
1465
1466 char *
1467 spa_strdup(const char *s)
1468 {
1469 size_t len;
1470 char *new;
1471
1472 len = strlen(s);
1473 new = kmem_alloc(len + 1, KM_SLEEP);
1474 bcopy(s, new, len);
1475 new[len] = '\0';
1476
1477 return (new);
1478 }
1479
1480 void
1481 spa_strfree(char *s)
1482 {
1483 kmem_free(s, strlen(s) + 1);
1484 }
1485
1486 uint64_t
1487 spa_get_random(uint64_t range)
1488 {
1489 uint64_t r;
1490
1491 ASSERT(range != 0);
1492
1493 if (range == 1)
1494 return (0);
1495
1496 (void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t));
1497
1498 return (r % range);
1499 }
1500
1501 uint64_t
1502 spa_generate_guid(spa_t *spa)
1503 {
1504 uint64_t guid = spa_get_random(-1ULL);
1505
1506 if (spa != NULL) {
1507 while (guid == 0 || spa_guid_exists(spa_guid(spa), guid))
1508 guid = spa_get_random(-1ULL);
1509 } else {
1510 while (guid == 0 || spa_guid_exists(guid, 0))
1511 guid = spa_get_random(-1ULL);
1512 }
1513
1514 return (guid);
1515 }
1516
1517 void
1518 snprintf_blkptr(char *buf, size_t buflen, const blkptr_t *bp)
1519 {
1520 char type[256];
1521 char *checksum = NULL;
1522 char *compress = NULL;
1523
1524 if (bp != NULL) {
1525 if (BP_GET_TYPE(bp) & DMU_OT_NEWTYPE) {
1526 dmu_object_byteswap_t bswap =
1527 DMU_OT_BYTESWAP(BP_GET_TYPE(bp));
1528 (void) snprintf(type, sizeof (type), "bswap %s %s",
1529 DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) ?
1530 "metadata" : "data",
1531 dmu_ot_byteswap[bswap].ob_name);
1532 } else {
1533 (void) strlcpy(type, dmu_ot[BP_GET_TYPE(bp)].ot_name,
1534 sizeof (type));
1535 }
1536 if (!BP_IS_EMBEDDED(bp)) {
1537 checksum =
1538 zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name;
1539 }
1540 compress = zio_compress_table[BP_GET_COMPRESS(bp)].ci_name;
1541 }
1542
1543 SNPRINTF_BLKPTR(snprintf, ' ', buf, buflen, bp, type, checksum,
1544 compress);
1545 }
1546
1547 void
1548 spa_freeze(spa_t *spa)
1549 {
1550 uint64_t freeze_txg = 0;
1551
1552 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1553 if (spa->spa_freeze_txg == UINT64_MAX) {
1554 freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE;
1555 spa->spa_freeze_txg = freeze_txg;
1556 }
1557 spa_config_exit(spa, SCL_ALL, FTAG);
1558 if (freeze_txg != 0)
1559 txg_wait_synced(spa_get_dsl(spa), freeze_txg);
1560 }
1561
1562 void
1563 zfs_panic_recover(const char *fmt, ...)
1564 {
1565 va_list adx;
1566
1567 va_start(adx, fmt);
1568 vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx);
1569 va_end(adx);
1570 }
1571
1572 /*
1573 * This is a stripped-down version of strtoull, suitable only for converting
1574 * lowercase hexadecimal numbers that don't overflow.
1575 */
1576 uint64_t
1577 zfs_strtonum(const char *str, char **nptr)
1578 {
1579 uint64_t val = 0;
1580 char c;
1581 int digit;
1582
1583 while ((c = *str) != '\0') {
1584 if (c >= '0' && c <= '9')
1585 digit = c - '0';
1586 else if (c >= 'a' && c <= 'f')
1587 digit = 10 + c - 'a';
1588 else
1589 break;
1590
1591 val *= 16;
1592 val += digit;
1593
1594 str++;
1595 }
1596
1597 if (nptr)
1598 *nptr = (char *)str;
1599
1600 return (val);
1601 }
1602
1603 void
1604 spa_activate_allocation_classes(spa_t *spa, dmu_tx_t *tx)
1605 {
1606 /*
1607 * We bump the feature refcount for each special vdev added to the pool
1608 */
1609 ASSERT(spa_feature_is_enabled(spa, SPA_FEATURE_ALLOCATION_CLASSES));
1610 spa_feature_incr(spa, SPA_FEATURE_ALLOCATION_CLASSES, tx);
1611 }
1612
1613 /*
1614 * ==========================================================================
1615 * Accessor functions
1616 * ==========================================================================
1617 */
1618
1619 boolean_t
1620 spa_shutting_down(spa_t *spa)
1621 {
1622 return (spa->spa_async_suspended);
1623 }
1624
1625 dsl_pool_t *
1626 spa_get_dsl(spa_t *spa)
1627 {
1628 return (spa->spa_dsl_pool);
1629 }
1630
1631 boolean_t
1632 spa_is_initializing(spa_t *spa)
1633 {
1634 return (spa->spa_is_initializing);
1635 }
1636
1637 boolean_t
1638 spa_indirect_vdevs_loaded(spa_t *spa)
1639 {
1640 return (spa->spa_indirect_vdevs_loaded);
1641 }
1642
1643 blkptr_t *
1644 spa_get_rootblkptr(spa_t *spa)
1645 {
1646 return (&spa->spa_ubsync.ub_rootbp);
1647 }
1648
1649 void
1650 spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp)
1651 {
1652 spa->spa_uberblock.ub_rootbp = *bp;
1653 }
1654
1655 void
1656 spa_altroot(spa_t *spa, char *buf, size_t buflen)
1657 {
1658 if (spa->spa_root == NULL)
1659 buf[0] = '\0';
1660 else
1661 (void) strncpy(buf, spa->spa_root, buflen);
1662 }
1663
1664 int
1665 spa_sync_pass(spa_t *spa)
1666 {
1667 return (spa->spa_sync_pass);
1668 }
1669
1670 char *
1671 spa_name(spa_t *spa)
1672 {
1673 return (spa->spa_name);
1674 }
1675
1676 uint64_t
1677 spa_guid(spa_t *spa)
1678 {
1679 dsl_pool_t *dp = spa_get_dsl(spa);
1680 uint64_t guid;
1681
1682 /*
1683 * If we fail to parse the config during spa_load(), we can go through
1684 * the error path (which posts an ereport) and end up here with no root
1685 * vdev. We stash the original pool guid in 'spa_config_guid' to handle
1686 * this case.
1687 */
1688 if (spa->spa_root_vdev == NULL)
1689 return (spa->spa_config_guid);
1690
1691 guid = spa->spa_last_synced_guid != 0 ?
1692 spa->spa_last_synced_guid : spa->spa_root_vdev->vdev_guid;
1693
1694 /*
1695 * Return the most recently synced out guid unless we're
1696 * in syncing context.
1697 */
1698 if (dp && dsl_pool_sync_context(dp))
1699 return (spa->spa_root_vdev->vdev_guid);
1700 else
1701 return (guid);
1702 }
1703
1704 uint64_t
1705 spa_load_guid(spa_t *spa)
1706 {
1707 /*
1708 * This is a GUID that exists solely as a reference for the
1709 * purposes of the arc. It is generated at load time, and
1710 * is never written to persistent storage.
1711 */
1712 return (spa->spa_load_guid);
1713 }
1714
1715 uint64_t
1716 spa_last_synced_txg(spa_t *spa)
1717 {
1718 return (spa->spa_ubsync.ub_txg);
1719 }
1720
1721 uint64_t
1722 spa_first_txg(spa_t *spa)
1723 {
1724 return (spa->spa_first_txg);
1725 }
1726
1727 uint64_t
1728 spa_syncing_txg(spa_t *spa)
1729 {
1730 return (spa->spa_syncing_txg);
1731 }
1732
1733 /*
1734 * Return the last txg where data can be dirtied. The final txgs
1735 * will be used to just clear out any deferred frees that remain.
1736 */
1737 uint64_t
1738 spa_final_dirty_txg(spa_t *spa)
1739 {
1740 return (spa->spa_final_txg - TXG_DEFER_SIZE);
1741 }
1742
1743 pool_state_t
1744 spa_state(spa_t *spa)
1745 {
1746 return (spa->spa_state);
1747 }
1748
1749 spa_load_state_t
1750 spa_load_state(spa_t *spa)
1751 {
1752 return (spa->spa_load_state);
1753 }
1754
1755 uint64_t
1756 spa_freeze_txg(spa_t *spa)
1757 {
1758 return (spa->spa_freeze_txg);
1759 }
1760
1761 /*
1762 * Return the inflated asize for a logical write in bytes. This is used by the
1763 * DMU to calculate the space a logical write will require on disk.
1764 * If lsize is smaller than the largest physical block size allocatable on this
1765 * pool we use its value instead, since the write will end up using the whole
1766 * block anyway.
1767 */
1768 uint64_t
1769 spa_get_worst_case_asize(spa_t *spa, uint64_t lsize)
1770 {
1771 if (lsize == 0)
1772 return (0); /* No inflation needed */
1773 return (MAX(lsize, 1 << spa->spa_max_ashift) * spa_asize_inflation);
1774 }
1775
1776 /*
1777 * Return the amount of slop space in bytes. It is 1/32 of the pool (3.2%),
1778 * or at least 128MB, unless that would cause it to be more than half the
1779 * pool size.
1780 *
1781 * See the comment above spa_slop_shift for details.
1782 */
1783 uint64_t
1784 spa_get_slop_space(spa_t *spa)
1785 {
1786 uint64_t space = spa_get_dspace(spa);
1787 return (MAX(space >> spa_slop_shift, MIN(space >> 1, spa_min_slop)));
1788 }
1789
1790 uint64_t
1791 spa_get_dspace(spa_t *spa)
1792 {
1793 return (spa->spa_dspace);
1794 }
1795
1796 uint64_t
1797 spa_get_checkpoint_space(spa_t *spa)
1798 {
1799 return (spa->spa_checkpoint_info.sci_dspace);
1800 }
1801
1802 void
1803 spa_update_dspace(spa_t *spa)
1804 {
1805 spa->spa_dspace = metaslab_class_get_dspace(spa_normal_class(spa)) +
1806 ddt_get_dedup_dspace(spa);
1807 if (spa->spa_vdev_removal != NULL) {
1808 /*
1809 * We can't allocate from the removing device, so
1810 * subtract its size. This prevents the DMU/DSL from
1811 * filling up the (now smaller) pool while we are in the
1812 * middle of removing the device.
1813 *
1814 * Note that the DMU/DSL doesn't actually know or care
1815 * how much space is allocated (it does its own tracking
1816 * of how much space has been logically used). So it
1817 * doesn't matter that the data we are moving may be
1818 * allocated twice (on the old device and the new
1819 * device).
1820 */
1821 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
1822 vdev_t *vd =
1823 vdev_lookup_top(spa, spa->spa_vdev_removal->svr_vdev_id);
1824 spa->spa_dspace -= spa_deflate(spa) ?
1825 vd->vdev_stat.vs_dspace : vd->vdev_stat.vs_space;
1826 spa_config_exit(spa, SCL_VDEV, FTAG);
1827 }
1828 }
1829
1830 /*
1831 * Return the failure mode that has been set to this pool. The default
1832 * behavior will be to block all I/Os when a complete failure occurs.
1833 */
1834 uint64_t
1835 spa_get_failmode(spa_t *spa)
1836 {
1837 return (spa->spa_failmode);
1838 }
1839
1840 boolean_t
1841 spa_suspended(spa_t *spa)
1842 {
1843 return (spa->spa_suspended != ZIO_SUSPEND_NONE);
1844 }
1845
1846 uint64_t
1847 spa_version(spa_t *spa)
1848 {
1849 return (spa->spa_ubsync.ub_version);
1850 }
1851
1852 boolean_t
1853 spa_deflate(spa_t *spa)
1854 {
1855 return (spa->spa_deflate);
1856 }
1857
1858 metaslab_class_t *
1859 spa_normal_class(spa_t *spa)
1860 {
1861 return (spa->spa_normal_class);
1862 }
1863
1864 metaslab_class_t *
1865 spa_log_class(spa_t *spa)
1866 {
1867 return (spa->spa_log_class);
1868 }
1869
1870 metaslab_class_t *
1871 spa_special_class(spa_t *spa)
1872 {
1873 return (spa->spa_special_class);
1874 }
1875
1876 metaslab_class_t *
1877 spa_dedup_class(spa_t *spa)
1878 {
1879 return (spa->spa_dedup_class);
1880 }
1881
1882 /*
1883 * Locate an appropriate allocation class
1884 */
1885 metaslab_class_t *
1886 spa_preferred_class(spa_t *spa, uint64_t size, dmu_object_type_t objtype,
1887 uint_t level, uint_t special_smallblk)
1888 {
1889 if (DMU_OT_IS_ZIL(objtype)) {
1890 if (spa->spa_log_class->mc_groups != 0)
1891 return (spa_log_class(spa));
1892 else
1893 return (spa_normal_class(spa));
1894 }
1895
1896 boolean_t has_special_class = spa->spa_special_class->mc_groups != 0;
1897
1898 if (DMU_OT_IS_DDT(objtype)) {
1899 if (spa->spa_dedup_class->mc_groups != 0)
1900 return (spa_dedup_class(spa));
1901 else if (has_special_class && zfs_ddt_data_is_special)
1902 return (spa_special_class(spa));
1903 else
1904 return (spa_normal_class(spa));
1905 }
1906
1907 /* Indirect blocks for user data can land in special if allowed */
1908 if (level > 0 && (DMU_OT_IS_FILE(objtype) || objtype == DMU_OT_ZVOL)) {
1909 if (has_special_class && zfs_user_indirect_is_special)
1910 return (spa_special_class(spa));
1911 else
1912 return (spa_normal_class(spa));
1913 }
1914
1915 if (DMU_OT_IS_METADATA(objtype) || level > 0) {
1916 if (has_special_class)
1917 return (spa_special_class(spa));
1918 else
1919 return (spa_normal_class(spa));
1920 }
1921
1922 /*
1923 * Allow small file blocks in special class in some cases (like
1924 * for the dRAID vdev feature). But always leave a reserve of
1925 * zfs_special_class_metadata_reserve_pct exclusively for metadata.
1926 */
1927 if (DMU_OT_IS_FILE(objtype) &&
1928 has_special_class && size <= special_smallblk) {
1929 metaslab_class_t *special = spa_special_class(spa);
1930 uint64_t alloc = metaslab_class_get_alloc(special);
1931 uint64_t space = metaslab_class_get_space(special);
1932 uint64_t limit =
1933 (space * (100 - zfs_special_class_metadata_reserve_pct))
1934 / 100;
1935
1936 if (alloc < limit)
1937 return (special);
1938 }
1939
1940 return (spa_normal_class(spa));
1941 }
1942
1943 void
1944 spa_evicting_os_register(spa_t *spa, objset_t *os)
1945 {
1946 mutex_enter(&spa->spa_evicting_os_lock);
1947 list_insert_head(&spa->spa_evicting_os_list, os);
1948 mutex_exit(&spa->spa_evicting_os_lock);
1949 }
1950
1951 void
1952 spa_evicting_os_deregister(spa_t *spa, objset_t *os)
1953 {
1954 mutex_enter(&spa->spa_evicting_os_lock);
1955 list_remove(&spa->spa_evicting_os_list, os);
1956 cv_broadcast(&spa->spa_evicting_os_cv);
1957 mutex_exit(&spa->spa_evicting_os_lock);
1958 }
1959
1960 void
1961 spa_evicting_os_wait(spa_t *spa)
1962 {
1963 mutex_enter(&spa->spa_evicting_os_lock);
1964 while (!list_is_empty(&spa->spa_evicting_os_list))
1965 cv_wait(&spa->spa_evicting_os_cv, &spa->spa_evicting_os_lock);
1966 mutex_exit(&spa->spa_evicting_os_lock);
1967
1968 dmu_buf_user_evict_wait();
1969 }
1970
1971 int
1972 spa_max_replication(spa_t *spa)
1973 {
1974 /*
1975 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
1976 * handle BPs with more than one DVA allocated. Set our max
1977 * replication level accordingly.
1978 */
1979 if (spa_version(spa) < SPA_VERSION_DITTO_BLOCKS)
1980 return (1);
1981 return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override));
1982 }
1983
1984 int
1985 spa_prev_software_version(spa_t *spa)
1986 {
1987 return (spa->spa_prev_software_version);
1988 }
1989
1990 uint64_t
1991 spa_deadman_synctime(spa_t *spa)
1992 {
1993 return (spa->spa_deadman_synctime);
1994 }
1995
1996 spa_autotrim_t
1997 spa_get_autotrim(spa_t *spa)
1998 {
1999 return (spa->spa_autotrim);
2000 }
2001
2002 uint64_t
2003 spa_deadman_ziotime(spa_t *spa)
2004 {
2005 return (spa->spa_deadman_ziotime);
2006 }
2007
2008 uint64_t
2009 spa_get_deadman_failmode(spa_t *spa)
2010 {
2011 return (spa->spa_deadman_failmode);
2012 }
2013
2014 void
2015 spa_set_deadman_failmode(spa_t *spa, const char *failmode)
2016 {
2017 if (strcmp(failmode, "wait") == 0)
2018 spa->spa_deadman_failmode = ZIO_FAILURE_MODE_WAIT;
2019 else if (strcmp(failmode, "continue") == 0)
2020 spa->spa_deadman_failmode = ZIO_FAILURE_MODE_CONTINUE;
2021 else if (strcmp(failmode, "panic") == 0)
2022 spa->spa_deadman_failmode = ZIO_FAILURE_MODE_PANIC;
2023 else
2024 spa->spa_deadman_failmode = ZIO_FAILURE_MODE_WAIT;
2025 }
2026
2027 void
2028 spa_set_deadman_ziotime(hrtime_t ns)
2029 {
2030 spa_t *spa = NULL;
2031
2032 if (spa_mode_global != SPA_MODE_UNINIT) {
2033 mutex_enter(&spa_namespace_lock);
2034 while ((spa = spa_next(spa)) != NULL)
2035 spa->spa_deadman_ziotime = ns;
2036 mutex_exit(&spa_namespace_lock);
2037 }
2038 }
2039
2040 void
2041 spa_set_deadman_synctime(hrtime_t ns)
2042 {
2043 spa_t *spa = NULL;
2044
2045 if (spa_mode_global != SPA_MODE_UNINIT) {
2046 mutex_enter(&spa_namespace_lock);
2047 while ((spa = spa_next(spa)) != NULL)
2048 spa->spa_deadman_synctime = ns;
2049 mutex_exit(&spa_namespace_lock);
2050 }
2051 }
2052
2053 uint64_t
2054 dva_get_dsize_sync(spa_t *spa, const dva_t *dva)
2055 {
2056 uint64_t asize = DVA_GET_ASIZE(dva);
2057 uint64_t dsize = asize;
2058
2059 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
2060
2061 if (asize != 0 && spa->spa_deflate) {
2062 vdev_t *vd = vdev_lookup_top(spa, DVA_GET_VDEV(dva));
2063 if (vd != NULL)
2064 dsize = (asize >> SPA_MINBLOCKSHIFT) *
2065 vd->vdev_deflate_ratio;
2066 }
2067
2068 return (dsize);
2069 }
2070
2071 uint64_t
2072 bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp)
2073 {
2074 uint64_t dsize = 0;
2075
2076 for (int d = 0; d < BP_GET_NDVAS(bp); d++)
2077 dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
2078
2079 return (dsize);
2080 }
2081
2082 uint64_t
2083 bp_get_dsize(spa_t *spa, const blkptr_t *bp)
2084 {
2085 uint64_t dsize = 0;
2086
2087 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2088
2089 for (int d = 0; d < BP_GET_NDVAS(bp); d++)
2090 dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
2091
2092 spa_config_exit(spa, SCL_VDEV, FTAG);
2093
2094 return (dsize);
2095 }
2096
2097 uint64_t
2098 spa_dirty_data(spa_t *spa)
2099 {
2100 return (spa->spa_dsl_pool->dp_dirty_total);
2101 }
2102
2103 /*
2104 * ==========================================================================
2105 * SPA Import Progress Routines
2106 * ==========================================================================
2107 */
2108
2109 typedef struct spa_import_progress {
2110 uint64_t pool_guid; /* unique id for updates */
2111 char *pool_name;
2112 spa_load_state_t spa_load_state;
2113 uint64_t mmp_sec_remaining; /* MMP activity check */
2114 uint64_t spa_load_max_txg; /* rewind txg */
2115 procfs_list_node_t smh_node;
2116 } spa_import_progress_t;
2117
2118 spa_history_list_t *spa_import_progress_list = NULL;
2119
2120 static int
2121 spa_import_progress_show_header(struct seq_file *f)
2122 {
2123 seq_printf(f, "%-20s %-14s %-14s %-12s %s\n", "pool_guid",
2124 "load_state", "multihost_secs", "max_txg",
2125 "pool_name");
2126 return (0);
2127 }
2128
2129 static int
2130 spa_import_progress_show(struct seq_file *f, void *data)
2131 {
2132 spa_import_progress_t *sip = (spa_import_progress_t *)data;
2133
2134 seq_printf(f, "%-20llu %-14llu %-14llu %-12llu %s\n",
2135 (u_longlong_t)sip->pool_guid, (u_longlong_t)sip->spa_load_state,
2136 (u_longlong_t)sip->mmp_sec_remaining,
2137 (u_longlong_t)sip->spa_load_max_txg,
2138 (sip->pool_name ? sip->pool_name : "-"));
2139
2140 return (0);
2141 }
2142
2143 /* Remove oldest elements from list until there are no more than 'size' left */
2144 static void
2145 spa_import_progress_truncate(spa_history_list_t *shl, unsigned int size)
2146 {
2147 spa_import_progress_t *sip;
2148 while (shl->size > size) {
2149 sip = list_remove_head(&shl->procfs_list.pl_list);
2150 if (sip->pool_name)
2151 spa_strfree(sip->pool_name);
2152 kmem_free(sip, sizeof (spa_import_progress_t));
2153 shl->size--;
2154 }
2155
2156 IMPLY(size == 0, list_is_empty(&shl->procfs_list.pl_list));
2157 }
2158
2159 static void
2160 spa_import_progress_init(void)
2161 {
2162 spa_import_progress_list = kmem_zalloc(sizeof (spa_history_list_t),
2163 KM_SLEEP);
2164
2165 spa_import_progress_list->size = 0;
2166
2167 spa_import_progress_list->procfs_list.pl_private =
2168 spa_import_progress_list;
2169
2170 procfs_list_install("zfs",
2171 "import_progress",
2172 0644,
2173 &spa_import_progress_list->procfs_list,
2174 spa_import_progress_show,
2175 spa_import_progress_show_header,
2176 NULL,
2177 offsetof(spa_import_progress_t, smh_node));
2178 }
2179
2180 static void
2181 spa_import_progress_destroy(void)
2182 {
2183 spa_history_list_t *shl = spa_import_progress_list;
2184 procfs_list_uninstall(&shl->procfs_list);
2185 spa_import_progress_truncate(shl, 0);
2186 procfs_list_destroy(&shl->procfs_list);
2187 kmem_free(shl, sizeof (spa_history_list_t));
2188 }
2189
2190 int
2191 spa_import_progress_set_state(uint64_t pool_guid,
2192 spa_load_state_t load_state)
2193 {
2194 spa_history_list_t *shl = spa_import_progress_list;
2195 spa_import_progress_t *sip;
2196 int error = ENOENT;
2197
2198 if (shl->size == 0)
2199 return (0);
2200
2201 mutex_enter(&shl->procfs_list.pl_lock);
2202 for (sip = list_tail(&shl->procfs_list.pl_list); sip != NULL;
2203 sip = list_prev(&shl->procfs_list.pl_list, sip)) {
2204 if (sip->pool_guid == pool_guid) {
2205 sip->spa_load_state = load_state;
2206 error = 0;
2207 break;
2208 }
2209 }
2210 mutex_exit(&shl->procfs_list.pl_lock);
2211
2212 return (error);
2213 }
2214
2215 int
2216 spa_import_progress_set_max_txg(uint64_t pool_guid, uint64_t load_max_txg)
2217 {
2218 spa_history_list_t *shl = spa_import_progress_list;
2219 spa_import_progress_t *sip;
2220 int error = ENOENT;
2221
2222 if (shl->size == 0)
2223 return (0);
2224
2225 mutex_enter(&shl->procfs_list.pl_lock);
2226 for (sip = list_tail(&shl->procfs_list.pl_list); sip != NULL;
2227 sip = list_prev(&shl->procfs_list.pl_list, sip)) {
2228 if (sip->pool_guid == pool_guid) {
2229 sip->spa_load_max_txg = load_max_txg;
2230 error = 0;
2231 break;
2232 }
2233 }
2234 mutex_exit(&shl->procfs_list.pl_lock);
2235
2236 return (error);
2237 }
2238
2239 int
2240 spa_import_progress_set_mmp_check(uint64_t pool_guid,
2241 uint64_t mmp_sec_remaining)
2242 {
2243 spa_history_list_t *shl = spa_import_progress_list;
2244 spa_import_progress_t *sip;
2245 int error = ENOENT;
2246
2247 if (shl->size == 0)
2248 return (0);
2249
2250 mutex_enter(&shl->procfs_list.pl_lock);
2251 for (sip = list_tail(&shl->procfs_list.pl_list); sip != NULL;
2252 sip = list_prev(&shl->procfs_list.pl_list, sip)) {
2253 if (sip->pool_guid == pool_guid) {
2254 sip->mmp_sec_remaining = mmp_sec_remaining;
2255 error = 0;
2256 break;
2257 }
2258 }
2259 mutex_exit(&shl->procfs_list.pl_lock);
2260
2261 return (error);
2262 }
2263
2264 /*
2265 * A new import is in progress, add an entry.
2266 */
2267 void
2268 spa_import_progress_add(spa_t *spa)
2269 {
2270 spa_history_list_t *shl = spa_import_progress_list;
2271 spa_import_progress_t *sip;
2272 char *poolname = NULL;
2273
2274 sip = kmem_zalloc(sizeof (spa_import_progress_t), KM_SLEEP);
2275 sip->pool_guid = spa_guid(spa);
2276
2277 (void) nvlist_lookup_string(spa->spa_config, ZPOOL_CONFIG_POOL_NAME,
2278 &poolname);
2279 if (poolname == NULL)
2280 poolname = spa_name(spa);
2281 sip->pool_name = spa_strdup(poolname);
2282 sip->spa_load_state = spa_load_state(spa);
2283
2284 mutex_enter(&shl->procfs_list.pl_lock);
2285 procfs_list_add(&shl->procfs_list, sip);
2286 shl->size++;
2287 mutex_exit(&shl->procfs_list.pl_lock);
2288 }
2289
2290 void
2291 spa_import_progress_remove(uint64_t pool_guid)
2292 {
2293 spa_history_list_t *shl = spa_import_progress_list;
2294 spa_import_progress_t *sip;
2295
2296 mutex_enter(&shl->procfs_list.pl_lock);
2297 for (sip = list_tail(&shl->procfs_list.pl_list); sip != NULL;
2298 sip = list_prev(&shl->procfs_list.pl_list, sip)) {
2299 if (sip->pool_guid == pool_guid) {
2300 if (sip->pool_name)
2301 spa_strfree(sip->pool_name);
2302 list_remove(&shl->procfs_list.pl_list, sip);
2303 shl->size--;
2304 kmem_free(sip, sizeof (spa_import_progress_t));
2305 break;
2306 }
2307 }
2308 mutex_exit(&shl->procfs_list.pl_lock);
2309 }
2310
2311 /*
2312 * ==========================================================================
2313 * Initialization and Termination
2314 * ==========================================================================
2315 */
2316
2317 static int
2318 spa_name_compare(const void *a1, const void *a2)
2319 {
2320 const spa_t *s1 = a1;
2321 const spa_t *s2 = a2;
2322 int s;
2323
2324 s = strcmp(s1->spa_name, s2->spa_name);
2325
2326 return (TREE_ISIGN(s));
2327 }
2328
2329 void
2330 spa_boot_init(void)
2331 {
2332 spa_config_load();
2333 }
2334
2335 void
2336 spa_init(spa_mode_t mode)
2337 {
2338 mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL);
2339 mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL);
2340 mutex_init(&spa_l2cache_lock, NULL, MUTEX_DEFAULT, NULL);
2341 cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL);
2342
2343 avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t),
2344 offsetof(spa_t, spa_avl));
2345
2346 avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_aux_t),
2347 offsetof(spa_aux_t, aux_avl));
2348
2349 avl_create(&spa_l2cache_avl, spa_l2cache_compare, sizeof (spa_aux_t),
2350 offsetof(spa_aux_t, aux_avl));
2351
2352 spa_mode_global = mode;
2353
2354 #ifndef _KERNEL
2355 if (spa_mode_global != SPA_MODE_READ && dprintf_find_string("watch")) {
2356 struct sigaction sa;
2357
2358 sa.sa_flags = SA_SIGINFO;
2359 sigemptyset(&sa.sa_mask);
2360 sa.sa_sigaction = arc_buf_sigsegv;
2361
2362 if (sigaction(SIGSEGV, &sa, NULL) == -1) {
2363 perror("could not enable watchpoints: "
2364 "sigaction(SIGSEGV, ...) = ");
2365 } else {
2366 arc_watch = B_TRUE;
2367 }
2368 }
2369 #endif
2370
2371 fm_init();
2372 zfs_refcount_init();
2373 unique_init();
2374 zfs_btree_init();
2375 metaslab_stat_init();
2376 ddt_init();
2377 zio_init();
2378 dmu_init();
2379 zil_init();
2380 vdev_cache_stat_init();
2381 vdev_mirror_stat_init();
2382 vdev_raidz_math_init();
2383 vdev_file_init();
2384 zfs_prop_init();
2385 zpool_prop_init();
2386 zpool_feature_init();
2387 spa_config_load();
2388 l2arc_start();
2389 scan_init();
2390 qat_init();
2391 spa_import_progress_init();
2392 }
2393
2394 void
2395 spa_fini(void)
2396 {
2397 l2arc_stop();
2398
2399 spa_evict_all();
2400
2401 vdev_file_fini();
2402 vdev_cache_stat_fini();
2403 vdev_mirror_stat_fini();
2404 vdev_raidz_math_fini();
2405 zil_fini();
2406 dmu_fini();
2407 zio_fini();
2408 ddt_fini();
2409 metaslab_stat_fini();
2410 zfs_btree_fini();
2411 unique_fini();
2412 zfs_refcount_fini();
2413 fm_fini();
2414 scan_fini();
2415 qat_fini();
2416 spa_import_progress_destroy();
2417
2418 avl_destroy(&spa_namespace_avl);
2419 avl_destroy(&spa_spare_avl);
2420 avl_destroy(&spa_l2cache_avl);
2421
2422 cv_destroy(&spa_namespace_cv);
2423 mutex_destroy(&spa_namespace_lock);
2424 mutex_destroy(&spa_spare_lock);
2425 mutex_destroy(&spa_l2cache_lock);
2426 }
2427
2428 /*
2429 * Return whether this pool has slogs. No locking needed.
2430 * It's not a problem if the wrong answer is returned as it's only for
2431 * performance and not correctness
2432 */
2433 boolean_t
2434 spa_has_slogs(spa_t *spa)
2435 {
2436 return (spa->spa_log_class->mc_rotor != NULL);
2437 }
2438
2439 spa_log_state_t
2440 spa_get_log_state(spa_t *spa)
2441 {
2442 return (spa->spa_log_state);
2443 }
2444
2445 void
2446 spa_set_log_state(spa_t *spa, spa_log_state_t state)
2447 {
2448 spa->spa_log_state = state;
2449 }
2450
2451 boolean_t
2452 spa_is_root(spa_t *spa)
2453 {
2454 return (spa->spa_is_root);
2455 }
2456
2457 boolean_t
2458 spa_writeable(spa_t *spa)
2459 {
2460 return (!!(spa->spa_mode & SPA_MODE_WRITE) && spa->spa_trust_config);
2461 }
2462
2463 /*
2464 * Returns true if there is a pending sync task in any of the current
2465 * syncing txg, the current quiescing txg, or the current open txg.
2466 */
2467 boolean_t
2468 spa_has_pending_synctask(spa_t *spa)
2469 {
2470 return (!txg_all_lists_empty(&spa->spa_dsl_pool->dp_sync_tasks) ||
2471 !txg_all_lists_empty(&spa->spa_dsl_pool->dp_early_sync_tasks));
2472 }
2473
2474 spa_mode_t
2475 spa_mode(spa_t *spa)
2476 {
2477 return (spa->spa_mode);
2478 }
2479
2480 uint64_t
2481 spa_bootfs(spa_t *spa)
2482 {
2483 return (spa->spa_bootfs);
2484 }
2485
2486 uint64_t
2487 spa_delegation(spa_t *spa)
2488 {
2489 return (spa->spa_delegation);
2490 }
2491
2492 objset_t *
2493 spa_meta_objset(spa_t *spa)
2494 {
2495 return (spa->spa_meta_objset);
2496 }
2497
2498 enum zio_checksum
2499 spa_dedup_checksum(spa_t *spa)
2500 {
2501 return (spa->spa_dedup_checksum);
2502 }
2503
2504 /*
2505 * Reset pool scan stat per scan pass (or reboot).
2506 */
2507 void
2508 spa_scan_stat_init(spa_t *spa)
2509 {
2510 /* data not stored on disk */
2511 spa->spa_scan_pass_start = gethrestime_sec();
2512 if (dsl_scan_is_paused_scrub(spa->spa_dsl_pool->dp_scan))
2513 spa->spa_scan_pass_scrub_pause = spa->spa_scan_pass_start;
2514 else
2515 spa->spa_scan_pass_scrub_pause = 0;
2516 spa->spa_scan_pass_scrub_spent_paused = 0;
2517 spa->spa_scan_pass_exam = 0;
2518 spa->spa_scan_pass_issued = 0;
2519 vdev_scan_stat_init(spa->spa_root_vdev);
2520 }
2521
2522 /*
2523 * Get scan stats for zpool status reports
2524 */
2525 int
2526 spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps)
2527 {
2528 dsl_scan_t *scn = spa->spa_dsl_pool ? spa->spa_dsl_pool->dp_scan : NULL;
2529
2530 if (scn == NULL || scn->scn_phys.scn_func == POOL_SCAN_NONE)
2531 return (SET_ERROR(ENOENT));
2532 bzero(ps, sizeof (pool_scan_stat_t));
2533
2534 /* data stored on disk */
2535 ps->pss_func = scn->scn_phys.scn_func;
2536 ps->pss_state = scn->scn_phys.scn_state;
2537 ps->pss_start_time = scn->scn_phys.scn_start_time;
2538 ps->pss_end_time = scn->scn_phys.scn_end_time;
2539 ps->pss_to_examine = scn->scn_phys.scn_to_examine;
2540 ps->pss_examined = scn->scn_phys.scn_examined;
2541 ps->pss_to_process = scn->scn_phys.scn_to_process;
2542 ps->pss_processed = scn->scn_phys.scn_processed;
2543 ps->pss_errors = scn->scn_phys.scn_errors;
2544
2545 /* data not stored on disk */
2546 ps->pss_pass_exam = spa->spa_scan_pass_exam;
2547 ps->pss_pass_start = spa->spa_scan_pass_start;
2548 ps->pss_pass_scrub_pause = spa->spa_scan_pass_scrub_pause;
2549 ps->pss_pass_scrub_spent_paused = spa->spa_scan_pass_scrub_spent_paused;
2550 ps->pss_pass_issued = spa->spa_scan_pass_issued;
2551 ps->pss_issued =
2552 scn->scn_issued_before_pass + spa->spa_scan_pass_issued;
2553
2554 return (0);
2555 }
2556
2557 int
2558 spa_maxblocksize(spa_t *spa)
2559 {
2560 if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS))
2561 return (SPA_MAXBLOCKSIZE);
2562 else
2563 return (SPA_OLD_MAXBLOCKSIZE);
2564 }
2565
2566
2567 /*
2568 * Returns the txg that the last device removal completed. No indirect mappings
2569 * have been added since this txg.
2570 */
2571 uint64_t
2572 spa_get_last_removal_txg(spa_t *spa)
2573 {
2574 uint64_t vdevid;
2575 uint64_t ret = -1ULL;
2576
2577 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2578 /*
2579 * sr_prev_indirect_vdev is only modified while holding all the
2580 * config locks, so it is sufficient to hold SCL_VDEV as reader when
2581 * examining it.
2582 */
2583 vdevid = spa->spa_removing_phys.sr_prev_indirect_vdev;
2584
2585 while (vdevid != -1ULL) {
2586 vdev_t *vd = vdev_lookup_top(spa, vdevid);
2587 vdev_indirect_births_t *vib = vd->vdev_indirect_births;
2588
2589 ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops);
2590
2591 /*
2592 * If the removal did not remap any data, we don't care.
2593 */
2594 if (vdev_indirect_births_count(vib) != 0) {
2595 ret = vdev_indirect_births_last_entry_txg(vib);
2596 break;
2597 }
2598
2599 vdevid = vd->vdev_indirect_config.vic_prev_indirect_vdev;
2600 }
2601 spa_config_exit(spa, SCL_VDEV, FTAG);
2602
2603 IMPLY(ret != -1ULL,
2604 spa_feature_is_active(spa, SPA_FEATURE_DEVICE_REMOVAL));
2605
2606 return (ret);
2607 }
2608
2609 int
2610 spa_maxdnodesize(spa_t *spa)
2611 {
2612 if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_DNODE))
2613 return (DNODE_MAX_SIZE);
2614 else
2615 return (DNODE_MIN_SIZE);
2616 }
2617
2618 boolean_t
2619 spa_multihost(spa_t *spa)
2620 {
2621 return (spa->spa_multihost ? B_TRUE : B_FALSE);
2622 }
2623
2624 uint32_t
2625 spa_get_hostid(spa_t *spa)
2626 {
2627 return (spa->spa_hostid);
2628 }
2629
2630 boolean_t
2631 spa_trust_config(spa_t *spa)
2632 {
2633 return (spa->spa_trust_config);
2634 }
2635
2636 uint64_t
2637 spa_missing_tvds_allowed(spa_t *spa)
2638 {
2639 return (spa->spa_missing_tvds_allowed);
2640 }
2641
2642 space_map_t *
2643 spa_syncing_log_sm(spa_t *spa)
2644 {
2645 return (spa->spa_syncing_log_sm);
2646 }
2647
2648 void
2649 spa_set_missing_tvds(spa_t *spa, uint64_t missing)
2650 {
2651 spa->spa_missing_tvds = missing;
2652 }
2653
2654 /*
2655 * Return the pool state string ("ONLINE", "DEGRADED", "SUSPENDED", etc).
2656 */
2657 const char *
2658 spa_state_to_name(spa_t *spa)
2659 {
2660 ASSERT3P(spa, !=, NULL);
2661
2662 /*
2663 * it is possible for the spa to exist, without root vdev
2664 * as the spa transitions during import/export
2665 */
2666 vdev_t *rvd = spa->spa_root_vdev;
2667 if (rvd == NULL) {
2668 return ("TRANSITIONING");
2669 }
2670 vdev_state_t state = rvd->vdev_state;
2671 vdev_aux_t aux = rvd->vdev_stat.vs_aux;
2672
2673 if (spa_suspended(spa) &&
2674 (spa_get_failmode(spa) != ZIO_FAILURE_MODE_CONTINUE))
2675 return ("SUSPENDED");
2676
2677 switch (state) {
2678 case VDEV_STATE_CLOSED:
2679 case VDEV_STATE_OFFLINE:
2680 return ("OFFLINE");
2681 case VDEV_STATE_REMOVED:
2682 return ("REMOVED");
2683 case VDEV_STATE_CANT_OPEN:
2684 if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
2685 return ("FAULTED");
2686 else if (aux == VDEV_AUX_SPLIT_POOL)
2687 return ("SPLIT");
2688 else
2689 return ("UNAVAIL");
2690 case VDEV_STATE_FAULTED:
2691 return ("FAULTED");
2692 case VDEV_STATE_DEGRADED:
2693 return ("DEGRADED");
2694 case VDEV_STATE_HEALTHY:
2695 return ("ONLINE");
2696 default:
2697 break;
2698 }
2699
2700 return ("UNKNOWN");
2701 }
2702
2703 boolean_t
2704 spa_top_vdevs_spacemap_addressable(spa_t *spa)
2705 {
2706 vdev_t *rvd = spa->spa_root_vdev;
2707 for (uint64_t c = 0; c < rvd->vdev_children; c++) {
2708 if (!vdev_is_spacemap_addressable(rvd->vdev_child[c]))
2709 return (B_FALSE);
2710 }
2711 return (B_TRUE);
2712 }
2713
2714 boolean_t
2715 spa_has_checkpoint(spa_t *spa)
2716 {
2717 return (spa->spa_checkpoint_txg != 0);
2718 }
2719
2720 boolean_t
2721 spa_importing_readonly_checkpoint(spa_t *spa)
2722 {
2723 return ((spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT) &&
2724 spa->spa_mode == SPA_MODE_READ);
2725 }
2726
2727 uint64_t
2728 spa_min_claim_txg(spa_t *spa)
2729 {
2730 uint64_t checkpoint_txg = spa->spa_uberblock.ub_checkpoint_txg;
2731
2732 if (checkpoint_txg != 0)
2733 return (checkpoint_txg + 1);
2734
2735 return (spa->spa_first_txg);
2736 }
2737
2738 /*
2739 * If there is a checkpoint, async destroys may consume more space from
2740 * the pool instead of freeing it. In an attempt to save the pool from
2741 * getting suspended when it is about to run out of space, we stop
2742 * processing async destroys.
2743 */
2744 boolean_t
2745 spa_suspend_async_destroy(spa_t *spa)
2746 {
2747 dsl_pool_t *dp = spa_get_dsl(spa);
2748
2749 uint64_t unreserved = dsl_pool_unreserved_space(dp,
2750 ZFS_SPACE_CHECK_EXTRA_RESERVED);
2751 uint64_t used = dsl_dir_phys(dp->dp_root_dir)->dd_used_bytes;
2752 uint64_t avail = (unreserved > used) ? (unreserved - used) : 0;
2753
2754 if (spa_has_checkpoint(spa) && avail == 0)
2755 return (B_TRUE);
2756
2757 return (B_FALSE);
2758 }
2759
2760 #if defined(_KERNEL)
2761
2762 int
2763 param_set_deadman_failmode_common(const char *val)
2764 {
2765 spa_t *spa = NULL;
2766 char *p;
2767
2768 if (val == NULL)
2769 return (SET_ERROR(EINVAL));
2770
2771 if ((p = strchr(val, '\n')) != NULL)
2772 *p = '\0';
2773
2774 if (strcmp(val, "wait") != 0 && strcmp(val, "continue") != 0 &&
2775 strcmp(val, "panic"))
2776 return (SET_ERROR(EINVAL));
2777
2778 if (spa_mode_global != SPA_MODE_UNINIT) {
2779 mutex_enter(&spa_namespace_lock);
2780 while ((spa = spa_next(spa)) != NULL)
2781 spa_set_deadman_failmode(spa, val);
2782 mutex_exit(&spa_namespace_lock);
2783 }
2784
2785 return (0);
2786 }
2787 #endif
2788
2789 /* Namespace manipulation */
2790 EXPORT_SYMBOL(spa_lookup);
2791 EXPORT_SYMBOL(spa_add);
2792 EXPORT_SYMBOL(spa_remove);
2793 EXPORT_SYMBOL(spa_next);
2794
2795 /* Refcount functions */
2796 EXPORT_SYMBOL(spa_open_ref);
2797 EXPORT_SYMBOL(spa_close);
2798 EXPORT_SYMBOL(spa_refcount_zero);
2799
2800 /* Pool configuration lock */
2801 EXPORT_SYMBOL(spa_config_tryenter);
2802 EXPORT_SYMBOL(spa_config_enter);
2803 EXPORT_SYMBOL(spa_config_exit);
2804 EXPORT_SYMBOL(spa_config_held);
2805
2806 /* Pool vdev add/remove lock */
2807 EXPORT_SYMBOL(spa_vdev_enter);
2808 EXPORT_SYMBOL(spa_vdev_exit);
2809
2810 /* Pool vdev state change lock */
2811 EXPORT_SYMBOL(spa_vdev_state_enter);
2812 EXPORT_SYMBOL(spa_vdev_state_exit);
2813
2814 /* Accessor functions */
2815 EXPORT_SYMBOL(spa_shutting_down);
2816 EXPORT_SYMBOL(spa_get_dsl);
2817 EXPORT_SYMBOL(spa_get_rootblkptr);
2818 EXPORT_SYMBOL(spa_set_rootblkptr);
2819 EXPORT_SYMBOL(spa_altroot);
2820 EXPORT_SYMBOL(spa_sync_pass);
2821 EXPORT_SYMBOL(spa_name);
2822 EXPORT_SYMBOL(spa_guid);
2823 EXPORT_SYMBOL(spa_last_synced_txg);
2824 EXPORT_SYMBOL(spa_first_txg);
2825 EXPORT_SYMBOL(spa_syncing_txg);
2826 EXPORT_SYMBOL(spa_version);
2827 EXPORT_SYMBOL(spa_state);
2828 EXPORT_SYMBOL(spa_load_state);
2829 EXPORT_SYMBOL(spa_freeze_txg);
2830 EXPORT_SYMBOL(spa_get_dspace);
2831 EXPORT_SYMBOL(spa_update_dspace);
2832 EXPORT_SYMBOL(spa_deflate);
2833 EXPORT_SYMBOL(spa_normal_class);
2834 EXPORT_SYMBOL(spa_log_class);
2835 EXPORT_SYMBOL(spa_special_class);
2836 EXPORT_SYMBOL(spa_preferred_class);
2837 EXPORT_SYMBOL(spa_max_replication);
2838 EXPORT_SYMBOL(spa_prev_software_version);
2839 EXPORT_SYMBOL(spa_get_failmode);
2840 EXPORT_SYMBOL(spa_suspended);
2841 EXPORT_SYMBOL(spa_bootfs);
2842 EXPORT_SYMBOL(spa_delegation);
2843 EXPORT_SYMBOL(spa_meta_objset);
2844 EXPORT_SYMBOL(spa_maxblocksize);
2845 EXPORT_SYMBOL(spa_maxdnodesize);
2846
2847 /* Miscellaneous support routines */
2848 EXPORT_SYMBOL(spa_guid_exists);
2849 EXPORT_SYMBOL(spa_strdup);
2850 EXPORT_SYMBOL(spa_strfree);
2851 EXPORT_SYMBOL(spa_get_random);
2852 EXPORT_SYMBOL(spa_generate_guid);
2853 EXPORT_SYMBOL(snprintf_blkptr);
2854 EXPORT_SYMBOL(spa_freeze);
2855 EXPORT_SYMBOL(spa_upgrade);
2856 EXPORT_SYMBOL(spa_evict_all);
2857 EXPORT_SYMBOL(spa_lookup_by_guid);
2858 EXPORT_SYMBOL(spa_has_spare);
2859 EXPORT_SYMBOL(dva_get_dsize_sync);
2860 EXPORT_SYMBOL(bp_get_dsize_sync);
2861 EXPORT_SYMBOL(bp_get_dsize);
2862 EXPORT_SYMBOL(spa_has_slogs);
2863 EXPORT_SYMBOL(spa_is_root);
2864 EXPORT_SYMBOL(spa_writeable);
2865 EXPORT_SYMBOL(spa_mode);
2866 EXPORT_SYMBOL(spa_namespace_lock);
2867 EXPORT_SYMBOL(spa_trust_config);
2868 EXPORT_SYMBOL(spa_missing_tvds_allowed);
2869 EXPORT_SYMBOL(spa_set_missing_tvds);
2870 EXPORT_SYMBOL(spa_state_to_name);
2871 EXPORT_SYMBOL(spa_importing_readonly_checkpoint);
2872 EXPORT_SYMBOL(spa_min_claim_txg);
2873 EXPORT_SYMBOL(spa_suspend_async_destroy);
2874 EXPORT_SYMBOL(spa_has_checkpoint);
2875 EXPORT_SYMBOL(spa_top_vdevs_spacemap_addressable);
2876
2877 ZFS_MODULE_PARAM(zfs, zfs_, flags, UINT, ZMOD_RW,
2878 "Set additional debugging flags");
2879
2880 ZFS_MODULE_PARAM(zfs, zfs_, recover, INT, ZMOD_RW,
2881 "Set to attempt to recover from fatal errors");
2882
2883 ZFS_MODULE_PARAM(zfs, zfs_, free_leak_on_eio, INT, ZMOD_RW,
2884 "Set to ignore IO errors during free and permanently leak the space");
2885
2886 ZFS_MODULE_PARAM(zfs, zfs_, deadman_checktime_ms, ULONG, ZMOD_RW,
2887 "Dead I/O check interval in milliseconds");
2888
2889 ZFS_MODULE_PARAM(zfs, zfs_, deadman_enabled, INT, ZMOD_RW,
2890 "Enable deadman timer");
2891
2892 ZFS_MODULE_PARAM(zfs_spa, spa_, asize_inflation, INT, ZMOD_RW,
2893 "SPA size estimate multiplication factor");
2894
2895 ZFS_MODULE_PARAM(zfs, zfs_, ddt_data_is_special, INT, ZMOD_RW,
2896 "Place DDT data into the special class");
2897
2898 ZFS_MODULE_PARAM(zfs, zfs_, user_indirect_is_special, INT, ZMOD_RW,
2899 "Place user data indirect blocks into the special class");
2900
2901 /* BEGIN CSTYLED */
2902 ZFS_MODULE_PARAM_CALL(zfs_deadman, zfs_deadman_, failmode,
2903 param_set_deadman_failmode, param_get_charp, ZMOD_RW,
2904 "Failmode for deadman timer");
2905
2906 ZFS_MODULE_PARAM_CALL(zfs_deadman, zfs_deadman_, synctime_ms,
2907 param_set_deadman_synctime, param_get_ulong, ZMOD_RW,
2908 "Pool sync expiration time in milliseconds");
2909
2910 ZFS_MODULE_PARAM_CALL(zfs_deadman, zfs_deadman_, ziotime_ms,
2911 param_set_deadman_ziotime, param_get_ulong, ZMOD_RW,
2912 "IO expiration time in milliseconds");
2913
2914 ZFS_MODULE_PARAM(zfs, zfs_, special_class_metadata_reserve_pct, INT, ZMOD_RW,
2915 "Small file blocks in special vdevs depends on this much "
2916 "free space available");
2917 /* END CSTYLED */
2918
2919 ZFS_MODULE_PARAM_CALL(zfs_spa, spa_, slop_shift, param_set_slop_shift,
2920 param_get_int, ZMOD_RW, "Reserved free space in pool");