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