]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/spa_misc.c
Force fault a vdev with 'zpool offline -f'
[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, 2017 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 */
28
29 #include <sys/zfs_context.h>
30 #include <sys/spa_impl.h>
31 #include <sys/zio.h>
32 #include <sys/zio_checksum.h>
33 #include <sys/zio_compress.h>
34 #include <sys/dmu.h>
35 #include <sys/dmu_tx.h>
36 #include <sys/zap.h>
37 #include <sys/zil.h>
38 #include <sys/vdev_impl.h>
39 #include <sys/vdev_file.h>
40 #include <sys/vdev_raidz.h>
41 #include <sys/metaslab.h>
42 #include <sys/uberblock_impl.h>
43 #include <sys/txg.h>
44 #include <sys/avl.h>
45 #include <sys/unique.h>
46 #include <sys/dsl_pool.h>
47 #include <sys/dsl_dir.h>
48 #include <sys/dsl_prop.h>
49 #include <sys/fm/util.h>
50 #include <sys/dsl_scan.h>
51 #include <sys/fs/zfs.h>
52 #include <sys/metaslab_impl.h>
53 #include <sys/arc.h>
54 #include <sys/ddt.h>
55 #include <sys/kstat.h>
56 #include "zfs_prop.h"
57 #include <sys/zfeature.h>
58 #include "qat_compress.h"
59
60 /*
61 * SPA locking
62 *
63 * There are four basic locks for managing spa_t structures:
64 *
65 * spa_namespace_lock (global mutex)
66 *
67 * This lock must be acquired to do any of the following:
68 *
69 * - Lookup a spa_t by name
70 * - Add or remove a spa_t from the namespace
71 * - Increase spa_refcount from non-zero
72 * - Check if spa_refcount is zero
73 * - Rename a spa_t
74 * - add/remove/attach/detach devices
75 * - Held for the duration of create/destroy/import/export
76 *
77 * It does not need to handle recursion. A create or destroy may
78 * reference objects (files or zvols) in other pools, but by
79 * definition they must have an existing reference, and will never need
80 * to lookup a spa_t by name.
81 *
82 * spa_refcount (per-spa refcount_t protected by mutex)
83 *
84 * This reference count keep track of any active users of the spa_t. The
85 * spa_t cannot be destroyed or freed while this is non-zero. Internally,
86 * the refcount is never really 'zero' - opening a pool implicitly keeps
87 * some references in the DMU. Internally we check against spa_minref, but
88 * present the image of a zero/non-zero value to consumers.
89 *
90 * spa_config_lock[] (per-spa array of rwlocks)
91 *
92 * This protects the spa_t from config changes, and must be held in
93 * the following circumstances:
94 *
95 * - RW_READER to perform I/O to the spa
96 * - RW_WRITER to change the vdev config
97 *
98 * The locking order is fairly straightforward:
99 *
100 * spa_namespace_lock -> spa_refcount
101 *
102 * The namespace lock must be acquired to increase the refcount from 0
103 * or to check if it is zero.
104 *
105 * spa_refcount -> spa_config_lock[]
106 *
107 * There must be at least one valid reference on the spa_t to acquire
108 * the config lock.
109 *
110 * spa_namespace_lock -> spa_config_lock[]
111 *
112 * The namespace lock must always be taken before the config lock.
113 *
114 *
115 * The spa_namespace_lock can be acquired directly and is globally visible.
116 *
117 * The namespace is manipulated using the following functions, all of which
118 * require the spa_namespace_lock to be held.
119 *
120 * spa_lookup() Lookup a spa_t by name.
121 *
122 * spa_add() Create a new spa_t in the namespace.
123 *
124 * spa_remove() Remove a spa_t from the namespace. This also
125 * frees up any memory associated with the spa_t.
126 *
127 * spa_next() Returns the next spa_t in the system, or the
128 * first if NULL is passed.
129 *
130 * spa_evict_all() Shutdown and remove all spa_t structures in
131 * the system.
132 *
133 * spa_guid_exists() Determine whether a pool/device guid exists.
134 *
135 * The spa_refcount is manipulated using the following functions:
136 *
137 * spa_open_ref() Adds a reference to the given spa_t. Must be
138 * called with spa_namespace_lock held if the
139 * refcount is currently zero.
140 *
141 * spa_close() Remove a reference from the spa_t. This will
142 * not free the spa_t or remove it from the
143 * namespace. No locking is required.
144 *
145 * spa_refcount_zero() Returns true if the refcount is currently
146 * zero. Must be called with spa_namespace_lock
147 * held.
148 *
149 * The spa_config_lock[] is an array of rwlocks, ordered as follows:
150 * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV.
151 * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}().
152 *
153 * To read the configuration, it suffices to hold one of these locks as reader.
154 * To modify the configuration, you must hold all locks as writer. To modify
155 * vdev state without altering the vdev tree's topology (e.g. online/offline),
156 * you must hold SCL_STATE and SCL_ZIO as writer.
157 *
158 * We use these distinct config locks to avoid recursive lock entry.
159 * For example, spa_sync() (which holds SCL_CONFIG as reader) induces
160 * block allocations (SCL_ALLOC), which may require reading space maps
161 * from disk (dmu_read() -> zio_read() -> SCL_ZIO).
162 *
163 * The spa config locks cannot be normal rwlocks because we need the
164 * ability to hand off ownership. For example, SCL_ZIO is acquired
165 * by the issuing thread and later released by an interrupt thread.
166 * They do, however, obey the usual write-wanted semantics to prevent
167 * writer (i.e. system administrator) starvation.
168 *
169 * The lock acquisition rules are as follows:
170 *
171 * SCL_CONFIG
172 * Protects changes to the vdev tree topology, such as vdev
173 * add/remove/attach/detach. Protects the dirty config list
174 * (spa_config_dirty_list) and the set of spares and l2arc devices.
175 *
176 * SCL_STATE
177 * Protects changes to pool state and vdev state, such as vdev
178 * online/offline/fault/degrade/clear. Protects the dirty state list
179 * (spa_state_dirty_list) and global pool state (spa_state).
180 *
181 * SCL_ALLOC
182 * Protects changes to metaslab groups and classes.
183 * Held as reader by metaslab_alloc() and metaslab_claim().
184 *
185 * SCL_ZIO
186 * Held by bp-level zios (those which have no io_vd upon entry)
187 * to prevent changes to the vdev tree. The bp-level zio implicitly
188 * protects all of its vdev child zios, which do not hold SCL_ZIO.
189 *
190 * SCL_FREE
191 * Protects changes to metaslab groups and classes.
192 * Held as reader by metaslab_free(). SCL_FREE is distinct from
193 * SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free
194 * blocks in zio_done() while another i/o that holds either
195 * SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete.
196 *
197 * SCL_VDEV
198 * Held as reader to prevent changes to the vdev tree during trivial
199 * inquiries such as bp_get_dsize(). SCL_VDEV is distinct from the
200 * other locks, and lower than all of them, to ensure that it's safe
201 * to acquire regardless of caller context.
202 *
203 * In addition, the following rules apply:
204 *
205 * (a) spa_props_lock protects pool properties, spa_config and spa_config_list.
206 * The lock ordering is SCL_CONFIG > spa_props_lock.
207 *
208 * (b) I/O operations on leaf vdevs. For any zio operation that takes
209 * an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(),
210 * or zio_write_phys() -- the caller must ensure that the config cannot
211 * cannot change in the interim, and that the vdev cannot be reopened.
212 * SCL_STATE as reader suffices for both.
213 *
214 * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
215 *
216 * spa_vdev_enter() Acquire the namespace lock and the config lock
217 * for writing.
218 *
219 * spa_vdev_exit() Release the config lock, wait for all I/O
220 * to complete, sync the updated configs to the
221 * cache, and release the namespace lock.
222 *
223 * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit().
224 * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual
225 * locking is, always, based on spa_namespace_lock and spa_config_lock[].
226 *
227 * spa_rename() is also implemented within this file since it requires
228 * manipulation of the namespace.
229 */
230
231 static avl_tree_t spa_namespace_avl;
232 kmutex_t spa_namespace_lock;
233 static kcondvar_t spa_namespace_cv;
234 int spa_max_replication_override = SPA_DVAS_PER_BP;
235
236 static kmutex_t spa_spare_lock;
237 static avl_tree_t spa_spare_avl;
238 static kmutex_t spa_l2cache_lock;
239 static avl_tree_t spa_l2cache_avl;
240
241 kmem_cache_t *spa_buffer_pool;
242 int spa_mode_global;
243
244 #ifdef ZFS_DEBUG
245 /* Everything except dprintf and spa is on by default in debug builds */
246 int zfs_flags = ~(ZFS_DEBUG_DPRINTF | ZFS_DEBUG_SPA);
247 #else
248 int zfs_flags = 0;
249 #endif
250
251 /*
252 * zfs_recover can be set to nonzero to attempt to recover from
253 * otherwise-fatal errors, typically caused by on-disk corruption. When
254 * set, calls to zfs_panic_recover() will turn into warning messages.
255 * This should only be used as a last resort, as it typically results
256 * in leaked space, or worse.
257 */
258 int zfs_recover = B_FALSE;
259
260 /*
261 * If destroy encounters an EIO while reading metadata (e.g. indirect
262 * blocks), space referenced by the missing metadata can not be freed.
263 * Normally this causes the background destroy to become "stalled", as
264 * it is unable to make forward progress. While in this stalled state,
265 * all remaining space to free from the error-encountering filesystem is
266 * "temporarily leaked". Set this flag to cause it to ignore the EIO,
267 * permanently leak the space from indirect blocks that can not be read,
268 * and continue to free everything else that it can.
269 *
270 * The default, "stalling" behavior is useful if the storage partially
271 * fails (i.e. some but not all i/os fail), and then later recovers. In
272 * this case, we will be able to continue pool operations while it is
273 * partially failed, and when it recovers, we can continue to free the
274 * space, with no leaks. However, note that this case is actually
275 * fairly rare.
276 *
277 * Typically pools either (a) fail completely (but perhaps temporarily,
278 * e.g. a top-level vdev going offline), or (b) have localized,
279 * permanent errors (e.g. disk returns the wrong data due to bit flip or
280 * firmware bug). In case (a), this setting does not matter because the
281 * pool will be suspended and the sync thread will not be able to make
282 * forward progress regardless. In case (b), because the error is
283 * permanent, the best we can do is leak the minimum amount of space,
284 * which is what setting this flag will do. Therefore, it is reasonable
285 * for this flag to normally be set, but we chose the more conservative
286 * approach of not setting it, so that there is no possibility of
287 * leaking space in the "partial temporary" failure case.
288 */
289 int zfs_free_leak_on_eio = B_FALSE;
290
291 /*
292 * Expiration time in milliseconds. This value has two meanings. First it is
293 * used to determine when the spa_deadman() logic should fire. By default the
294 * spa_deadman() will fire if spa_sync() has not completed in 1000 seconds.
295 * Secondly, the value determines if an I/O is considered "hung". Any I/O that
296 * has not completed in zfs_deadman_synctime_ms is considered "hung" resulting
297 * in a system panic.
298 */
299 unsigned long zfs_deadman_synctime_ms = 1000000ULL;
300
301 /*
302 * Check time in milliseconds. This defines the frequency at which we check
303 * for hung I/O.
304 */
305 unsigned long zfs_deadman_checktime_ms = 5000ULL;
306
307 /*
308 * By default the deadman is enabled.
309 */
310 int zfs_deadman_enabled = 1;
311
312 /*
313 * The worst case is single-sector max-parity RAID-Z blocks, in which
314 * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1)
315 * times the size; so just assume that. Add to this the fact that
316 * we can have up to 3 DVAs per bp, and one more factor of 2 because
317 * the block may be dittoed with up to 3 DVAs by ddt_sync(). All together,
318 * the worst case is:
319 * (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2 == 24
320 */
321 int spa_asize_inflation = 24;
322
323 /*
324 * Normally, we don't allow the last 3.2% (1/(2^spa_slop_shift)) of space in
325 * the pool to be consumed. This ensures that we don't run the pool
326 * completely out of space, due to unaccounted changes (e.g. to the MOS).
327 * It also limits the worst-case time to allocate space. If we have
328 * less than this amount of free space, most ZPL operations (e.g. write,
329 * create) will return ENOSPC.
330 *
331 * Certain operations (e.g. file removal, most administrative actions) can
332 * use half the slop space. They will only return ENOSPC if less than half
333 * the slop space is free. Typically, once the pool has less than the slop
334 * space free, the user will use these operations to free up space in the pool.
335 * These are the operations that call dsl_pool_adjustedsize() with the netfree
336 * argument set to TRUE.
337 *
338 * A very restricted set of operations are always permitted, regardless of
339 * the amount of free space. These are the operations that call
340 * dsl_sync_task(ZFS_SPACE_CHECK_NONE), e.g. "zfs destroy". If these
341 * operations result in a net increase in the amount of space used,
342 * it is possible to run the pool completely out of space, causing it to
343 * be permanently read-only.
344 *
345 * Note that on very small pools, the slop space will be larger than
346 * 3.2%, in an effort to have it be at least spa_min_slop (128MB),
347 * but we never allow it to be more than half the pool size.
348 *
349 * See also the comments in zfs_space_check_t.
350 */
351 int spa_slop_shift = 5;
352 uint64_t spa_min_slop = 128 * 1024 * 1024;
353
354 /*
355 * ==========================================================================
356 * SPA config locking
357 * ==========================================================================
358 */
359 static void
360 spa_config_lock_init(spa_t *spa)
361 {
362 int i;
363
364 for (i = 0; i < SCL_LOCKS; i++) {
365 spa_config_lock_t *scl = &spa->spa_config_lock[i];
366 mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL);
367 cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL);
368 refcount_create_untracked(&scl->scl_count);
369 scl->scl_writer = NULL;
370 scl->scl_write_wanted = 0;
371 }
372 }
373
374 static void
375 spa_config_lock_destroy(spa_t *spa)
376 {
377 int i;
378
379 for (i = 0; i < SCL_LOCKS; i++) {
380 spa_config_lock_t *scl = &spa->spa_config_lock[i];
381 mutex_destroy(&scl->scl_lock);
382 cv_destroy(&scl->scl_cv);
383 refcount_destroy(&scl->scl_count);
384 ASSERT(scl->scl_writer == NULL);
385 ASSERT(scl->scl_write_wanted == 0);
386 }
387 }
388
389 int
390 spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw)
391 {
392 int i;
393
394 for (i = 0; i < SCL_LOCKS; i++) {
395 spa_config_lock_t *scl = &spa->spa_config_lock[i];
396 if (!(locks & (1 << i)))
397 continue;
398 mutex_enter(&scl->scl_lock);
399 if (rw == RW_READER) {
400 if (scl->scl_writer || scl->scl_write_wanted) {
401 mutex_exit(&scl->scl_lock);
402 spa_config_exit(spa, locks & ((1 << i) - 1),
403 tag);
404 return (0);
405 }
406 } else {
407 ASSERT(scl->scl_writer != curthread);
408 if (!refcount_is_zero(&scl->scl_count)) {
409 mutex_exit(&scl->scl_lock);
410 spa_config_exit(spa, locks & ((1 << i) - 1),
411 tag);
412 return (0);
413 }
414 scl->scl_writer = curthread;
415 }
416 (void) refcount_add(&scl->scl_count, tag);
417 mutex_exit(&scl->scl_lock);
418 }
419 return (1);
420 }
421
422 void
423 spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw)
424 {
425 int wlocks_held = 0;
426 int i;
427
428 ASSERT3U(SCL_LOCKS, <, sizeof (wlocks_held) * NBBY);
429
430 for (i = 0; i < SCL_LOCKS; i++) {
431 spa_config_lock_t *scl = &spa->spa_config_lock[i];
432 if (scl->scl_writer == curthread)
433 wlocks_held |= (1 << i);
434 if (!(locks & (1 << i)))
435 continue;
436 mutex_enter(&scl->scl_lock);
437 if (rw == RW_READER) {
438 while (scl->scl_writer || scl->scl_write_wanted) {
439 cv_wait(&scl->scl_cv, &scl->scl_lock);
440 }
441 } else {
442 ASSERT(scl->scl_writer != curthread);
443 while (!refcount_is_zero(&scl->scl_count)) {
444 scl->scl_write_wanted++;
445 cv_wait(&scl->scl_cv, &scl->scl_lock);
446 scl->scl_write_wanted--;
447 }
448 scl->scl_writer = curthread;
449 }
450 (void) refcount_add(&scl->scl_count, tag);
451 mutex_exit(&scl->scl_lock);
452 }
453 ASSERT(wlocks_held <= locks);
454 }
455
456 void
457 spa_config_exit(spa_t *spa, int locks, void *tag)
458 {
459 int i;
460
461 for (i = SCL_LOCKS - 1; i >= 0; i--) {
462 spa_config_lock_t *scl = &spa->spa_config_lock[i];
463 if (!(locks & (1 << i)))
464 continue;
465 mutex_enter(&scl->scl_lock);
466 ASSERT(!refcount_is_zero(&scl->scl_count));
467 if (refcount_remove(&scl->scl_count, tag) == 0) {
468 ASSERT(scl->scl_writer == NULL ||
469 scl->scl_writer == curthread);
470 scl->scl_writer = NULL; /* OK in either case */
471 cv_broadcast(&scl->scl_cv);
472 }
473 mutex_exit(&scl->scl_lock);
474 }
475 }
476
477 int
478 spa_config_held(spa_t *spa, int locks, krw_t rw)
479 {
480 int i, locks_held = 0;
481
482 for (i = 0; i < SCL_LOCKS; i++) {
483 spa_config_lock_t *scl = &spa->spa_config_lock[i];
484 if (!(locks & (1 << i)))
485 continue;
486 if ((rw == RW_READER && !refcount_is_zero(&scl->scl_count)) ||
487 (rw == RW_WRITER && scl->scl_writer == curthread))
488 locks_held |= 1 << i;
489 }
490
491 return (locks_held);
492 }
493
494 /*
495 * ==========================================================================
496 * SPA namespace functions
497 * ==========================================================================
498 */
499
500 /*
501 * Lookup the named spa_t in the AVL tree. The spa_namespace_lock must be held.
502 * Returns NULL if no matching spa_t is found.
503 */
504 spa_t *
505 spa_lookup(const char *name)
506 {
507 static spa_t search; /* spa_t is large; don't allocate on stack */
508 spa_t *spa;
509 avl_index_t where;
510 char *cp;
511
512 ASSERT(MUTEX_HELD(&spa_namespace_lock));
513
514 (void) strlcpy(search.spa_name, name, sizeof (search.spa_name));
515
516 /*
517 * If it's a full dataset name, figure out the pool name and
518 * just use that.
519 */
520 cp = strpbrk(search.spa_name, "/@#");
521 if (cp != NULL)
522 *cp = '\0';
523
524 spa = avl_find(&spa_namespace_avl, &search, &where);
525
526 return (spa);
527 }
528
529 /*
530 * Fires when spa_sync has not completed within zfs_deadman_synctime_ms.
531 * If the zfs_deadman_enabled flag is set then it inspects all vdev queues
532 * looking for potentially hung I/Os.
533 */
534 void
535 spa_deadman(void *arg)
536 {
537 spa_t *spa = arg;
538
539 /* Disable the deadman if the pool is suspended. */
540 if (spa_suspended(spa))
541 return;
542
543 zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu",
544 (gethrtime() - spa->spa_sync_starttime) / NANOSEC,
545 ++spa->spa_deadman_calls);
546 if (zfs_deadman_enabled)
547 vdev_deadman(spa->spa_root_vdev);
548
549 spa->spa_deadman_tqid = taskq_dispatch_delay(system_delay_taskq,
550 spa_deadman, spa, TQ_SLEEP, ddi_get_lbolt() +
551 MSEC_TO_TICK(zfs_deadman_checktime_ms));
552 }
553
554 /*
555 * Create an uninitialized spa_t with the given name. Requires
556 * spa_namespace_lock. The caller must ensure that the spa_t doesn't already
557 * exist by calling spa_lookup() first.
558 */
559 spa_t *
560 spa_add(const char *name, nvlist_t *config, const char *altroot)
561 {
562 spa_t *spa;
563 spa_config_dirent_t *dp;
564 int t;
565 int i;
566
567 ASSERT(MUTEX_HELD(&spa_namespace_lock));
568
569 spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
570
571 mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL);
572 mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL);
573 mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL);
574 mutex_init(&spa->spa_evicting_os_lock, NULL, MUTEX_DEFAULT, NULL);
575 mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL);
576 mutex_init(&spa->spa_proc_lock, NULL, MUTEX_DEFAULT, NULL);
577 mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL);
578 mutex_init(&spa->spa_cksum_tmpls_lock, NULL, MUTEX_DEFAULT, NULL);
579 mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL);
580 mutex_init(&spa->spa_suspend_lock, NULL, MUTEX_DEFAULT, NULL);
581 mutex_init(&spa->spa_vdev_top_lock, NULL, MUTEX_DEFAULT, NULL);
582 mutex_init(&spa->spa_feat_stats_lock, NULL, MUTEX_DEFAULT, NULL);
583 mutex_init(&spa->spa_alloc_lock, NULL, MUTEX_DEFAULT, NULL);
584
585 cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL);
586 cv_init(&spa->spa_evicting_os_cv, NULL, CV_DEFAULT, NULL);
587 cv_init(&spa->spa_proc_cv, NULL, CV_DEFAULT, NULL);
588 cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL);
589 cv_init(&spa->spa_suspend_cv, NULL, CV_DEFAULT, NULL);
590
591 for (t = 0; t < TXG_SIZE; t++)
592 bplist_create(&spa->spa_free_bplist[t]);
593
594 (void) strlcpy(spa->spa_name, name, sizeof (spa->spa_name));
595 spa->spa_state = POOL_STATE_UNINITIALIZED;
596 spa->spa_freeze_txg = UINT64_MAX;
597 spa->spa_final_txg = UINT64_MAX;
598 spa->spa_load_max_txg = UINT64_MAX;
599 spa->spa_proc = &p0;
600 spa->spa_proc_state = SPA_PROC_NONE;
601
602 spa->spa_deadman_synctime = MSEC2NSEC(zfs_deadman_synctime_ms);
603
604 refcount_create(&spa->spa_refcount);
605 spa_config_lock_init(spa);
606 spa_stats_init(spa);
607
608 avl_add(&spa_namespace_avl, spa);
609
610 /*
611 * Set the alternate root, if there is one.
612 */
613 if (altroot)
614 spa->spa_root = spa_strdup(altroot);
615
616 avl_create(&spa->spa_alloc_tree, zio_bookmark_compare,
617 sizeof (zio_t), offsetof(zio_t, io_alloc_node));
618
619 /*
620 * Every pool starts with the default cachefile
621 */
622 list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t),
623 offsetof(spa_config_dirent_t, scd_link));
624
625 dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP);
626 dp->scd_path = altroot ? NULL : spa_strdup(spa_config_path);
627 list_insert_head(&spa->spa_config_list, dp);
628
629 VERIFY(nvlist_alloc(&spa->spa_load_info, NV_UNIQUE_NAME,
630 KM_SLEEP) == 0);
631
632 if (config != NULL) {
633 nvlist_t *features;
634
635 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_FEATURES_FOR_READ,
636 &features) == 0) {
637 VERIFY(nvlist_dup(features, &spa->spa_label_features,
638 0) == 0);
639 }
640
641 VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0);
642 }
643
644 if (spa->spa_label_features == NULL) {
645 VERIFY(nvlist_alloc(&spa->spa_label_features, NV_UNIQUE_NAME,
646 KM_SLEEP) == 0);
647 }
648
649 spa->spa_debug = ((zfs_flags & ZFS_DEBUG_SPA) != 0);
650
651 spa->spa_min_ashift = INT_MAX;
652 spa->spa_max_ashift = 0;
653
654 /* Reset cached value */
655 spa->spa_dedup_dspace = ~0ULL;
656
657 /*
658 * As a pool is being created, treat all features as disabled by
659 * setting SPA_FEATURE_DISABLED for all entries in the feature
660 * refcount cache.
661 */
662 for (i = 0; i < SPA_FEATURES; i++) {
663 spa->spa_feat_refcount_cache[i] = SPA_FEATURE_DISABLED;
664 }
665
666 return (spa);
667 }
668
669 /*
670 * Removes a spa_t from the namespace, freeing up any memory used. Requires
671 * spa_namespace_lock. This is called only after the spa_t has been closed and
672 * deactivated.
673 */
674 void
675 spa_remove(spa_t *spa)
676 {
677 spa_config_dirent_t *dp;
678 int t;
679
680 ASSERT(MUTEX_HELD(&spa_namespace_lock));
681 ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
682 ASSERT3U(refcount_count(&spa->spa_refcount), ==, 0);
683
684 nvlist_free(spa->spa_config_splitting);
685
686 avl_remove(&spa_namespace_avl, spa);
687 cv_broadcast(&spa_namespace_cv);
688
689 if (spa->spa_root)
690 spa_strfree(spa->spa_root);
691
692 while ((dp = list_head(&spa->spa_config_list)) != NULL) {
693 list_remove(&spa->spa_config_list, dp);
694 if (dp->scd_path != NULL)
695 spa_strfree(dp->scd_path);
696 kmem_free(dp, sizeof (spa_config_dirent_t));
697 }
698
699 avl_destroy(&spa->spa_alloc_tree);
700 list_destroy(&spa->spa_config_list);
701
702 nvlist_free(spa->spa_label_features);
703 nvlist_free(spa->spa_load_info);
704 nvlist_free(spa->spa_feat_stats);
705 spa_config_set(spa, NULL);
706
707 refcount_destroy(&spa->spa_refcount);
708
709 spa_stats_destroy(spa);
710 spa_config_lock_destroy(spa);
711
712 for (t = 0; t < TXG_SIZE; t++)
713 bplist_destroy(&spa->spa_free_bplist[t]);
714
715 zio_checksum_templates_free(spa);
716
717 cv_destroy(&spa->spa_async_cv);
718 cv_destroy(&spa->spa_evicting_os_cv);
719 cv_destroy(&spa->spa_proc_cv);
720 cv_destroy(&spa->spa_scrub_io_cv);
721 cv_destroy(&spa->spa_suspend_cv);
722
723 mutex_destroy(&spa->spa_alloc_lock);
724 mutex_destroy(&spa->spa_async_lock);
725 mutex_destroy(&spa->spa_errlist_lock);
726 mutex_destroy(&spa->spa_errlog_lock);
727 mutex_destroy(&spa->spa_evicting_os_lock);
728 mutex_destroy(&spa->spa_history_lock);
729 mutex_destroy(&spa->spa_proc_lock);
730 mutex_destroy(&spa->spa_props_lock);
731 mutex_destroy(&spa->spa_cksum_tmpls_lock);
732 mutex_destroy(&spa->spa_scrub_lock);
733 mutex_destroy(&spa->spa_suspend_lock);
734 mutex_destroy(&spa->spa_vdev_top_lock);
735 mutex_destroy(&spa->spa_feat_stats_lock);
736
737 kmem_free(spa, sizeof (spa_t));
738 }
739
740 /*
741 * Given a pool, return the next pool in the namespace, or NULL if there is
742 * none. If 'prev' is NULL, return the first pool.
743 */
744 spa_t *
745 spa_next(spa_t *prev)
746 {
747 ASSERT(MUTEX_HELD(&spa_namespace_lock));
748
749 if (prev)
750 return (AVL_NEXT(&spa_namespace_avl, prev));
751 else
752 return (avl_first(&spa_namespace_avl));
753 }
754
755 /*
756 * ==========================================================================
757 * SPA refcount functions
758 * ==========================================================================
759 */
760
761 /*
762 * Add a reference to the given spa_t. Must have at least one reference, or
763 * have the namespace lock held.
764 */
765 void
766 spa_open_ref(spa_t *spa, void *tag)
767 {
768 ASSERT(refcount_count(&spa->spa_refcount) >= spa->spa_minref ||
769 MUTEX_HELD(&spa_namespace_lock));
770 (void) refcount_add(&spa->spa_refcount, tag);
771 }
772
773 /*
774 * Remove a reference to the given spa_t. Must have at least one reference, or
775 * have the namespace lock held.
776 */
777 void
778 spa_close(spa_t *spa, void *tag)
779 {
780 ASSERT(refcount_count(&spa->spa_refcount) > spa->spa_minref ||
781 MUTEX_HELD(&spa_namespace_lock));
782 (void) refcount_remove(&spa->spa_refcount, tag);
783 }
784
785 /*
786 * Remove a reference to the given spa_t held by a dsl dir that is
787 * being asynchronously released. Async releases occur from a taskq
788 * performing eviction of dsl datasets and dirs. The namespace lock
789 * isn't held and the hold by the object being evicted may contribute to
790 * spa_minref (e.g. dataset or directory released during pool export),
791 * so the asserts in spa_close() do not apply.
792 */
793 void
794 spa_async_close(spa_t *spa, void *tag)
795 {
796 (void) refcount_remove(&spa->spa_refcount, tag);
797 }
798
799 /*
800 * Check to see if the spa refcount is zero. Must be called with
801 * spa_namespace_lock held. We really compare against spa_minref, which is the
802 * number of references acquired when opening a pool
803 */
804 boolean_t
805 spa_refcount_zero(spa_t *spa)
806 {
807 ASSERT(MUTEX_HELD(&spa_namespace_lock));
808
809 return (refcount_count(&spa->spa_refcount) == spa->spa_minref);
810 }
811
812 /*
813 * ==========================================================================
814 * SPA spare and l2cache tracking
815 * ==========================================================================
816 */
817
818 /*
819 * Hot spares and cache devices are tracked using the same code below,
820 * for 'auxiliary' devices.
821 */
822
823 typedef struct spa_aux {
824 uint64_t aux_guid;
825 uint64_t aux_pool;
826 avl_node_t aux_avl;
827 int aux_count;
828 } spa_aux_t;
829
830 static inline int
831 spa_aux_compare(const void *a, const void *b)
832 {
833 const spa_aux_t *sa = (const spa_aux_t *)a;
834 const spa_aux_t *sb = (const spa_aux_t *)b;
835
836 return (AVL_CMP(sa->aux_guid, sb->aux_guid));
837 }
838
839 void
840 spa_aux_add(vdev_t *vd, avl_tree_t *avl)
841 {
842 avl_index_t where;
843 spa_aux_t search;
844 spa_aux_t *aux;
845
846 search.aux_guid = vd->vdev_guid;
847 if ((aux = avl_find(avl, &search, &where)) != NULL) {
848 aux->aux_count++;
849 } else {
850 aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP);
851 aux->aux_guid = vd->vdev_guid;
852 aux->aux_count = 1;
853 avl_insert(avl, aux, where);
854 }
855 }
856
857 void
858 spa_aux_remove(vdev_t *vd, avl_tree_t *avl)
859 {
860 spa_aux_t search;
861 spa_aux_t *aux;
862 avl_index_t where;
863
864 search.aux_guid = vd->vdev_guid;
865 aux = avl_find(avl, &search, &where);
866
867 ASSERT(aux != NULL);
868
869 if (--aux->aux_count == 0) {
870 avl_remove(avl, aux);
871 kmem_free(aux, sizeof (spa_aux_t));
872 } else if (aux->aux_pool == spa_guid(vd->vdev_spa)) {
873 aux->aux_pool = 0ULL;
874 }
875 }
876
877 boolean_t
878 spa_aux_exists(uint64_t guid, uint64_t *pool, int *refcnt, avl_tree_t *avl)
879 {
880 spa_aux_t search, *found;
881
882 search.aux_guid = guid;
883 found = avl_find(avl, &search, NULL);
884
885 if (pool) {
886 if (found)
887 *pool = found->aux_pool;
888 else
889 *pool = 0ULL;
890 }
891
892 if (refcnt) {
893 if (found)
894 *refcnt = found->aux_count;
895 else
896 *refcnt = 0;
897 }
898
899 return (found != NULL);
900 }
901
902 void
903 spa_aux_activate(vdev_t *vd, avl_tree_t *avl)
904 {
905 spa_aux_t search, *found;
906 avl_index_t where;
907
908 search.aux_guid = vd->vdev_guid;
909 found = avl_find(avl, &search, &where);
910 ASSERT(found != NULL);
911 ASSERT(found->aux_pool == 0ULL);
912
913 found->aux_pool = spa_guid(vd->vdev_spa);
914 }
915
916 /*
917 * Spares are tracked globally due to the following constraints:
918 *
919 * - A spare may be part of multiple pools.
920 * - A spare may be added to a pool even if it's actively in use within
921 * another pool.
922 * - A spare in use in any pool can only be the source of a replacement if
923 * the target is a spare in the same pool.
924 *
925 * We keep track of all spares on the system through the use of a reference
926 * counted AVL tree. When a vdev is added as a spare, or used as a replacement
927 * spare, then we bump the reference count in the AVL tree. In addition, we set
928 * the 'vdev_isspare' member to indicate that the device is a spare (active or
929 * inactive). When a spare is made active (used to replace a device in the
930 * pool), we also keep track of which pool its been made a part of.
931 *
932 * The 'spa_spare_lock' protects the AVL tree. These functions are normally
933 * called under the spa_namespace lock as part of vdev reconfiguration. The
934 * separate spare lock exists for the status query path, which does not need to
935 * be completely consistent with respect to other vdev configuration changes.
936 */
937
938 static int
939 spa_spare_compare(const void *a, const void *b)
940 {
941 return (spa_aux_compare(a, b));
942 }
943
944 void
945 spa_spare_add(vdev_t *vd)
946 {
947 mutex_enter(&spa_spare_lock);
948 ASSERT(!vd->vdev_isspare);
949 spa_aux_add(vd, &spa_spare_avl);
950 vd->vdev_isspare = B_TRUE;
951 mutex_exit(&spa_spare_lock);
952 }
953
954 void
955 spa_spare_remove(vdev_t *vd)
956 {
957 mutex_enter(&spa_spare_lock);
958 ASSERT(vd->vdev_isspare);
959 spa_aux_remove(vd, &spa_spare_avl);
960 vd->vdev_isspare = B_FALSE;
961 mutex_exit(&spa_spare_lock);
962 }
963
964 boolean_t
965 spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt)
966 {
967 boolean_t found;
968
969 mutex_enter(&spa_spare_lock);
970 found = spa_aux_exists(guid, pool, refcnt, &spa_spare_avl);
971 mutex_exit(&spa_spare_lock);
972
973 return (found);
974 }
975
976 void
977 spa_spare_activate(vdev_t *vd)
978 {
979 mutex_enter(&spa_spare_lock);
980 ASSERT(vd->vdev_isspare);
981 spa_aux_activate(vd, &spa_spare_avl);
982 mutex_exit(&spa_spare_lock);
983 }
984
985 /*
986 * Level 2 ARC devices are tracked globally for the same reasons as spares.
987 * Cache devices currently only support one pool per cache device, and so
988 * for these devices the aux reference count is currently unused beyond 1.
989 */
990
991 static int
992 spa_l2cache_compare(const void *a, const void *b)
993 {
994 return (spa_aux_compare(a, b));
995 }
996
997 void
998 spa_l2cache_add(vdev_t *vd)
999 {
1000 mutex_enter(&spa_l2cache_lock);
1001 ASSERT(!vd->vdev_isl2cache);
1002 spa_aux_add(vd, &spa_l2cache_avl);
1003 vd->vdev_isl2cache = B_TRUE;
1004 mutex_exit(&spa_l2cache_lock);
1005 }
1006
1007 void
1008 spa_l2cache_remove(vdev_t *vd)
1009 {
1010 mutex_enter(&spa_l2cache_lock);
1011 ASSERT(vd->vdev_isl2cache);
1012 spa_aux_remove(vd, &spa_l2cache_avl);
1013 vd->vdev_isl2cache = B_FALSE;
1014 mutex_exit(&spa_l2cache_lock);
1015 }
1016
1017 boolean_t
1018 spa_l2cache_exists(uint64_t guid, uint64_t *pool)
1019 {
1020 boolean_t found;
1021
1022 mutex_enter(&spa_l2cache_lock);
1023 found = spa_aux_exists(guid, pool, NULL, &spa_l2cache_avl);
1024 mutex_exit(&spa_l2cache_lock);
1025
1026 return (found);
1027 }
1028
1029 void
1030 spa_l2cache_activate(vdev_t *vd)
1031 {
1032 mutex_enter(&spa_l2cache_lock);
1033 ASSERT(vd->vdev_isl2cache);
1034 spa_aux_activate(vd, &spa_l2cache_avl);
1035 mutex_exit(&spa_l2cache_lock);
1036 }
1037
1038 /*
1039 * ==========================================================================
1040 * SPA vdev locking
1041 * ==========================================================================
1042 */
1043
1044 /*
1045 * Lock the given spa_t for the purpose of adding or removing a vdev.
1046 * Grabs the global spa_namespace_lock plus the spa config lock for writing.
1047 * It returns the next transaction group for the spa_t.
1048 */
1049 uint64_t
1050 spa_vdev_enter(spa_t *spa)
1051 {
1052 mutex_enter(&spa->spa_vdev_top_lock);
1053 mutex_enter(&spa_namespace_lock);
1054 return (spa_vdev_config_enter(spa));
1055 }
1056
1057 /*
1058 * Internal implementation for spa_vdev_enter(). Used when a vdev
1059 * operation requires multiple syncs (i.e. removing a device) while
1060 * keeping the spa_namespace_lock held.
1061 */
1062 uint64_t
1063 spa_vdev_config_enter(spa_t *spa)
1064 {
1065 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1066
1067 spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
1068
1069 return (spa_last_synced_txg(spa) + 1);
1070 }
1071
1072 /*
1073 * Used in combination with spa_vdev_config_enter() to allow the syncing
1074 * of multiple transactions without releasing the spa_namespace_lock.
1075 */
1076 void
1077 spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error, char *tag)
1078 {
1079 int config_changed = B_FALSE;
1080
1081 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1082 ASSERT(txg > spa_last_synced_txg(spa));
1083
1084 spa->spa_pending_vdev = NULL;
1085
1086 /*
1087 * Reassess the DTLs.
1088 */
1089 vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE);
1090
1091 if (error == 0 && !list_is_empty(&spa->spa_config_dirty_list)) {
1092 config_changed = B_TRUE;
1093 spa->spa_config_generation++;
1094 }
1095
1096 /*
1097 * Verify the metaslab classes.
1098 */
1099 ASSERT(metaslab_class_validate(spa_normal_class(spa)) == 0);
1100 ASSERT(metaslab_class_validate(spa_log_class(spa)) == 0);
1101
1102 spa_config_exit(spa, SCL_ALL, spa);
1103
1104 /*
1105 * Panic the system if the specified tag requires it. This
1106 * is useful for ensuring that configurations are updated
1107 * transactionally.
1108 */
1109 if (zio_injection_enabled)
1110 zio_handle_panic_injection(spa, tag, 0);
1111
1112 /*
1113 * Note: this txg_wait_synced() is important because it ensures
1114 * that there won't be more than one config change per txg.
1115 * This allows us to use the txg as the generation number.
1116 */
1117 if (error == 0)
1118 txg_wait_synced(spa->spa_dsl_pool, txg);
1119
1120 if (vd != NULL) {
1121 ASSERT(!vd->vdev_detached || vd->vdev_dtl_sm == NULL);
1122 spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
1123 vdev_free(vd);
1124 spa_config_exit(spa, SCL_ALL, spa);
1125 }
1126
1127 /*
1128 * If the config changed, update the config cache.
1129 */
1130 if (config_changed)
1131 spa_config_sync(spa, B_FALSE, B_TRUE);
1132 }
1133
1134 /*
1135 * Unlock the spa_t after adding or removing a vdev. Besides undoing the
1136 * locking of spa_vdev_enter(), we also want make sure the transactions have
1137 * synced to disk, and then update the global configuration cache with the new
1138 * information.
1139 */
1140 int
1141 spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
1142 {
1143 spa_vdev_config_exit(spa, vd, txg, error, FTAG);
1144 mutex_exit(&spa_namespace_lock);
1145 mutex_exit(&spa->spa_vdev_top_lock);
1146
1147 return (error);
1148 }
1149
1150 /*
1151 * Lock the given spa_t for the purpose of changing vdev state.
1152 */
1153 void
1154 spa_vdev_state_enter(spa_t *spa, int oplocks)
1155 {
1156 int locks = SCL_STATE_ALL | oplocks;
1157
1158 /*
1159 * Root pools may need to read of the underlying devfs filesystem
1160 * when opening up a vdev. Unfortunately if we're holding the
1161 * SCL_ZIO lock it will result in a deadlock when we try to issue
1162 * the read from the root filesystem. Instead we "prefetch"
1163 * the associated vnodes that we need prior to opening the
1164 * underlying devices and cache them so that we can prevent
1165 * any I/O when we are doing the actual open.
1166 */
1167 if (spa_is_root(spa)) {
1168 int low = locks & ~(SCL_ZIO - 1);
1169 int high = locks & ~low;
1170
1171 spa_config_enter(spa, high, spa, RW_WRITER);
1172 vdev_hold(spa->spa_root_vdev);
1173 spa_config_enter(spa, low, spa, RW_WRITER);
1174 } else {
1175 spa_config_enter(spa, locks, spa, RW_WRITER);
1176 }
1177 spa->spa_vdev_locks = locks;
1178 }
1179
1180 int
1181 spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error)
1182 {
1183 boolean_t config_changed = B_FALSE;
1184 vdev_t *vdev_top;
1185
1186 if (vd == NULL || vd == spa->spa_root_vdev) {
1187 vdev_top = spa->spa_root_vdev;
1188 } else {
1189 vdev_top = vd->vdev_top;
1190 }
1191
1192 if (vd != NULL || error == 0)
1193 vdev_dtl_reassess(vdev_top, 0, 0, B_FALSE);
1194
1195 if (vd != NULL) {
1196 if (vd != spa->spa_root_vdev)
1197 vdev_state_dirty(vdev_top);
1198
1199 config_changed = B_TRUE;
1200 spa->spa_config_generation++;
1201 }
1202
1203 if (spa_is_root(spa))
1204 vdev_rele(spa->spa_root_vdev);
1205
1206 ASSERT3U(spa->spa_vdev_locks, >=, SCL_STATE_ALL);
1207 spa_config_exit(spa, spa->spa_vdev_locks, spa);
1208
1209 /*
1210 * If anything changed, wait for it to sync. This ensures that,
1211 * from the system administrator's perspective, zpool(1M) commands
1212 * are synchronous. This is important for things like zpool offline:
1213 * when the command completes, you expect no further I/O from ZFS.
1214 */
1215 if (vd != NULL)
1216 txg_wait_synced(spa->spa_dsl_pool, 0);
1217
1218 /*
1219 * If the config changed, update the config cache.
1220 */
1221 if (config_changed) {
1222 mutex_enter(&spa_namespace_lock);
1223 spa_config_sync(spa, B_FALSE, B_TRUE);
1224 mutex_exit(&spa_namespace_lock);
1225 }
1226
1227 return (error);
1228 }
1229
1230 /*
1231 * ==========================================================================
1232 * Miscellaneous functions
1233 * ==========================================================================
1234 */
1235
1236 void
1237 spa_activate_mos_feature(spa_t *spa, const char *feature, dmu_tx_t *tx)
1238 {
1239 if (!nvlist_exists(spa->spa_label_features, feature)) {
1240 fnvlist_add_boolean(spa->spa_label_features, feature);
1241 /*
1242 * When we are creating the pool (tx_txg==TXG_INITIAL), we can't
1243 * dirty the vdev config because lock SCL_CONFIG is not held.
1244 * Thankfully, in this case we don't need to dirty the config
1245 * because it will be written out anyway when we finish
1246 * creating the pool.
1247 */
1248 if (tx->tx_txg != TXG_INITIAL)
1249 vdev_config_dirty(spa->spa_root_vdev);
1250 }
1251 }
1252
1253 void
1254 spa_deactivate_mos_feature(spa_t *spa, const char *feature)
1255 {
1256 if (nvlist_remove_all(spa->spa_label_features, feature) == 0)
1257 vdev_config_dirty(spa->spa_root_vdev);
1258 }
1259
1260 /*
1261 * Rename a spa_t.
1262 */
1263 int
1264 spa_rename(const char *name, const char *newname)
1265 {
1266 spa_t *spa;
1267 int err;
1268
1269 /*
1270 * Lookup the spa_t and grab the config lock for writing. We need to
1271 * actually open the pool so that we can sync out the necessary labels.
1272 * It's OK to call spa_open() with the namespace lock held because we
1273 * allow recursive calls for other reasons.
1274 */
1275 mutex_enter(&spa_namespace_lock);
1276 if ((err = spa_open(name, &spa, FTAG)) != 0) {
1277 mutex_exit(&spa_namespace_lock);
1278 return (err);
1279 }
1280
1281 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1282
1283 avl_remove(&spa_namespace_avl, spa);
1284 (void) strlcpy(spa->spa_name, newname, sizeof (spa->spa_name));
1285 avl_add(&spa_namespace_avl, spa);
1286
1287 /*
1288 * Sync all labels to disk with the new names by marking the root vdev
1289 * dirty and waiting for it to sync. It will pick up the new pool name
1290 * during the sync.
1291 */
1292 vdev_config_dirty(spa->spa_root_vdev);
1293
1294 spa_config_exit(spa, SCL_ALL, FTAG);
1295
1296 txg_wait_synced(spa->spa_dsl_pool, 0);
1297
1298 /*
1299 * Sync the updated config cache.
1300 */
1301 spa_config_sync(spa, B_FALSE, B_TRUE);
1302
1303 spa_close(spa, FTAG);
1304
1305 mutex_exit(&spa_namespace_lock);
1306
1307 return (0);
1308 }
1309
1310 /*
1311 * Return the spa_t associated with given pool_guid, if it exists. If
1312 * device_guid is non-zero, determine whether the pool exists *and* contains
1313 * a device with the specified device_guid.
1314 */
1315 spa_t *
1316 spa_by_guid(uint64_t pool_guid, uint64_t device_guid)
1317 {
1318 spa_t *spa;
1319 avl_tree_t *t = &spa_namespace_avl;
1320
1321 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1322
1323 for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) {
1324 if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1325 continue;
1326 if (spa->spa_root_vdev == NULL)
1327 continue;
1328 if (spa_guid(spa) == pool_guid) {
1329 if (device_guid == 0)
1330 break;
1331
1332 if (vdev_lookup_by_guid(spa->spa_root_vdev,
1333 device_guid) != NULL)
1334 break;
1335
1336 /*
1337 * Check any devices we may be in the process of adding.
1338 */
1339 if (spa->spa_pending_vdev) {
1340 if (vdev_lookup_by_guid(spa->spa_pending_vdev,
1341 device_guid) != NULL)
1342 break;
1343 }
1344 }
1345 }
1346
1347 return (spa);
1348 }
1349
1350 /*
1351 * Determine whether a pool with the given pool_guid exists.
1352 */
1353 boolean_t
1354 spa_guid_exists(uint64_t pool_guid, uint64_t device_guid)
1355 {
1356 return (spa_by_guid(pool_guid, device_guid) != NULL);
1357 }
1358
1359 char *
1360 spa_strdup(const char *s)
1361 {
1362 size_t len;
1363 char *new;
1364
1365 len = strlen(s);
1366 new = kmem_alloc(len + 1, KM_SLEEP);
1367 bcopy(s, new, len);
1368 new[len] = '\0';
1369
1370 return (new);
1371 }
1372
1373 void
1374 spa_strfree(char *s)
1375 {
1376 kmem_free(s, strlen(s) + 1);
1377 }
1378
1379 uint64_t
1380 spa_get_random(uint64_t range)
1381 {
1382 uint64_t r;
1383
1384 ASSERT(range != 0);
1385
1386 (void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t));
1387
1388 return (r % range);
1389 }
1390
1391 uint64_t
1392 spa_generate_guid(spa_t *spa)
1393 {
1394 uint64_t guid = spa_get_random(-1ULL);
1395
1396 if (spa != NULL) {
1397 while (guid == 0 || spa_guid_exists(spa_guid(spa), guid))
1398 guid = spa_get_random(-1ULL);
1399 } else {
1400 while (guid == 0 || spa_guid_exists(guid, 0))
1401 guid = spa_get_random(-1ULL);
1402 }
1403
1404 return (guid);
1405 }
1406
1407 void
1408 snprintf_blkptr(char *buf, size_t buflen, const blkptr_t *bp)
1409 {
1410 char type[256];
1411 char *checksum = NULL;
1412 char *compress = NULL;
1413
1414 if (bp != NULL) {
1415 if (BP_GET_TYPE(bp) & DMU_OT_NEWTYPE) {
1416 dmu_object_byteswap_t bswap =
1417 DMU_OT_BYTESWAP(BP_GET_TYPE(bp));
1418 (void) snprintf(type, sizeof (type), "bswap %s %s",
1419 DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) ?
1420 "metadata" : "data",
1421 dmu_ot_byteswap[bswap].ob_name);
1422 } else {
1423 (void) strlcpy(type, dmu_ot[BP_GET_TYPE(bp)].ot_name,
1424 sizeof (type));
1425 }
1426 if (!BP_IS_EMBEDDED(bp)) {
1427 checksum =
1428 zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name;
1429 }
1430 compress = zio_compress_table[BP_GET_COMPRESS(bp)].ci_name;
1431 }
1432
1433 SNPRINTF_BLKPTR(snprintf, ' ', buf, buflen, bp, type, checksum,
1434 compress);
1435 }
1436
1437 void
1438 spa_freeze(spa_t *spa)
1439 {
1440 uint64_t freeze_txg = 0;
1441
1442 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1443 if (spa->spa_freeze_txg == UINT64_MAX) {
1444 freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE;
1445 spa->spa_freeze_txg = freeze_txg;
1446 }
1447 spa_config_exit(spa, SCL_ALL, FTAG);
1448 if (freeze_txg != 0)
1449 txg_wait_synced(spa_get_dsl(spa), freeze_txg);
1450 }
1451
1452 void
1453 zfs_panic_recover(const char *fmt, ...)
1454 {
1455 va_list adx;
1456
1457 va_start(adx, fmt);
1458 vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx);
1459 va_end(adx);
1460 }
1461
1462 /*
1463 * This is a stripped-down version of strtoull, suitable only for converting
1464 * lowercase hexadecimal numbers that don't overflow.
1465 */
1466 uint64_t
1467 strtonum(const char *str, char **nptr)
1468 {
1469 uint64_t val = 0;
1470 char c;
1471 int digit;
1472
1473 while ((c = *str) != '\0') {
1474 if (c >= '0' && c <= '9')
1475 digit = c - '0';
1476 else if (c >= 'a' && c <= 'f')
1477 digit = 10 + c - 'a';
1478 else
1479 break;
1480
1481 val *= 16;
1482 val += digit;
1483
1484 str++;
1485 }
1486
1487 if (nptr)
1488 *nptr = (char *)str;
1489
1490 return (val);
1491 }
1492
1493 /*
1494 * ==========================================================================
1495 * Accessor functions
1496 * ==========================================================================
1497 */
1498
1499 boolean_t
1500 spa_shutting_down(spa_t *spa)
1501 {
1502 return (spa->spa_async_suspended);
1503 }
1504
1505 dsl_pool_t *
1506 spa_get_dsl(spa_t *spa)
1507 {
1508 return (spa->spa_dsl_pool);
1509 }
1510
1511 boolean_t
1512 spa_is_initializing(spa_t *spa)
1513 {
1514 return (spa->spa_is_initializing);
1515 }
1516
1517 blkptr_t *
1518 spa_get_rootblkptr(spa_t *spa)
1519 {
1520 return (&spa->spa_ubsync.ub_rootbp);
1521 }
1522
1523 void
1524 spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp)
1525 {
1526 spa->spa_uberblock.ub_rootbp = *bp;
1527 }
1528
1529 void
1530 spa_altroot(spa_t *spa, char *buf, size_t buflen)
1531 {
1532 if (spa->spa_root == NULL)
1533 buf[0] = '\0';
1534 else
1535 (void) strncpy(buf, spa->spa_root, buflen);
1536 }
1537
1538 int
1539 spa_sync_pass(spa_t *spa)
1540 {
1541 return (spa->spa_sync_pass);
1542 }
1543
1544 char *
1545 spa_name(spa_t *spa)
1546 {
1547 return (spa->spa_name);
1548 }
1549
1550 uint64_t
1551 spa_guid(spa_t *spa)
1552 {
1553 dsl_pool_t *dp = spa_get_dsl(spa);
1554 uint64_t guid;
1555
1556 /*
1557 * If we fail to parse the config during spa_load(), we can go through
1558 * the error path (which posts an ereport) and end up here with no root
1559 * vdev. We stash the original pool guid in 'spa_config_guid' to handle
1560 * this case.
1561 */
1562 if (spa->spa_root_vdev == NULL)
1563 return (spa->spa_config_guid);
1564
1565 guid = spa->spa_last_synced_guid != 0 ?
1566 spa->spa_last_synced_guid : spa->spa_root_vdev->vdev_guid;
1567
1568 /*
1569 * Return the most recently synced out guid unless we're
1570 * in syncing context.
1571 */
1572 if (dp && dsl_pool_sync_context(dp))
1573 return (spa->spa_root_vdev->vdev_guid);
1574 else
1575 return (guid);
1576 }
1577
1578 uint64_t
1579 spa_load_guid(spa_t *spa)
1580 {
1581 /*
1582 * This is a GUID that exists solely as a reference for the
1583 * purposes of the arc. It is generated at load time, and
1584 * is never written to persistent storage.
1585 */
1586 return (spa->spa_load_guid);
1587 }
1588
1589 uint64_t
1590 spa_last_synced_txg(spa_t *spa)
1591 {
1592 return (spa->spa_ubsync.ub_txg);
1593 }
1594
1595 uint64_t
1596 spa_first_txg(spa_t *spa)
1597 {
1598 return (spa->spa_first_txg);
1599 }
1600
1601 uint64_t
1602 spa_syncing_txg(spa_t *spa)
1603 {
1604 return (spa->spa_syncing_txg);
1605 }
1606
1607 /*
1608 * Return the last txg where data can be dirtied. The final txgs
1609 * will be used to just clear out any deferred frees that remain.
1610 */
1611 uint64_t
1612 spa_final_dirty_txg(spa_t *spa)
1613 {
1614 return (spa->spa_final_txg - TXG_DEFER_SIZE);
1615 }
1616
1617 pool_state_t
1618 spa_state(spa_t *spa)
1619 {
1620 return (spa->spa_state);
1621 }
1622
1623 spa_load_state_t
1624 spa_load_state(spa_t *spa)
1625 {
1626 return (spa->spa_load_state);
1627 }
1628
1629 uint64_t
1630 spa_freeze_txg(spa_t *spa)
1631 {
1632 return (spa->spa_freeze_txg);
1633 }
1634
1635 /*
1636 * Return the inflated asize for a logical write in bytes. This is used by the
1637 * DMU to calculate the space a logical write will require on disk.
1638 * If lsize is smaller than the largest physical block size allocatable on this
1639 * pool we use its value instead, since the write will end up using the whole
1640 * block anyway.
1641 */
1642 uint64_t
1643 spa_get_worst_case_asize(spa_t *spa, uint64_t lsize)
1644 {
1645 if (lsize == 0)
1646 return (0); /* No inflation needed */
1647 return (MAX(lsize, 1 << spa->spa_max_ashift) * spa_asize_inflation);
1648 }
1649
1650 /*
1651 * Return the amount of slop space in bytes. It is 1/32 of the pool (3.2%),
1652 * or at least 128MB, unless that would cause it to be more than half the
1653 * pool size.
1654 *
1655 * See the comment above spa_slop_shift for details.
1656 */
1657 uint64_t
1658 spa_get_slop_space(spa_t *spa)
1659 {
1660 uint64_t space = spa_get_dspace(spa);
1661 return (MAX(space >> spa_slop_shift, MIN(space >> 1, spa_min_slop)));
1662 }
1663
1664 uint64_t
1665 spa_get_dspace(spa_t *spa)
1666 {
1667 return (spa->spa_dspace);
1668 }
1669
1670 void
1671 spa_update_dspace(spa_t *spa)
1672 {
1673 spa->spa_dspace = metaslab_class_get_dspace(spa_normal_class(spa)) +
1674 ddt_get_dedup_dspace(spa);
1675 }
1676
1677 /*
1678 * Return the failure mode that has been set to this pool. The default
1679 * behavior will be to block all I/Os when a complete failure occurs.
1680 */
1681 uint8_t
1682 spa_get_failmode(spa_t *spa)
1683 {
1684 return (spa->spa_failmode);
1685 }
1686
1687 boolean_t
1688 spa_suspended(spa_t *spa)
1689 {
1690 return (spa->spa_suspended);
1691 }
1692
1693 uint64_t
1694 spa_version(spa_t *spa)
1695 {
1696 return (spa->spa_ubsync.ub_version);
1697 }
1698
1699 boolean_t
1700 spa_deflate(spa_t *spa)
1701 {
1702 return (spa->spa_deflate);
1703 }
1704
1705 metaslab_class_t *
1706 spa_normal_class(spa_t *spa)
1707 {
1708 return (spa->spa_normal_class);
1709 }
1710
1711 metaslab_class_t *
1712 spa_log_class(spa_t *spa)
1713 {
1714 return (spa->spa_log_class);
1715 }
1716
1717 void
1718 spa_evicting_os_register(spa_t *spa, objset_t *os)
1719 {
1720 mutex_enter(&spa->spa_evicting_os_lock);
1721 list_insert_head(&spa->spa_evicting_os_list, os);
1722 mutex_exit(&spa->spa_evicting_os_lock);
1723 }
1724
1725 void
1726 spa_evicting_os_deregister(spa_t *spa, objset_t *os)
1727 {
1728 mutex_enter(&spa->spa_evicting_os_lock);
1729 list_remove(&spa->spa_evicting_os_list, os);
1730 cv_broadcast(&spa->spa_evicting_os_cv);
1731 mutex_exit(&spa->spa_evicting_os_lock);
1732 }
1733
1734 void
1735 spa_evicting_os_wait(spa_t *spa)
1736 {
1737 mutex_enter(&spa->spa_evicting_os_lock);
1738 while (!list_is_empty(&spa->spa_evicting_os_list))
1739 cv_wait(&spa->spa_evicting_os_cv, &spa->spa_evicting_os_lock);
1740 mutex_exit(&spa->spa_evicting_os_lock);
1741
1742 dmu_buf_user_evict_wait();
1743 }
1744
1745 int
1746 spa_max_replication(spa_t *spa)
1747 {
1748 /*
1749 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
1750 * handle BPs with more than one DVA allocated. Set our max
1751 * replication level accordingly.
1752 */
1753 if (spa_version(spa) < SPA_VERSION_DITTO_BLOCKS)
1754 return (1);
1755 return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override));
1756 }
1757
1758 int
1759 spa_prev_software_version(spa_t *spa)
1760 {
1761 return (spa->spa_prev_software_version);
1762 }
1763
1764 uint64_t
1765 spa_deadman_synctime(spa_t *spa)
1766 {
1767 return (spa->spa_deadman_synctime);
1768 }
1769
1770 uint64_t
1771 dva_get_dsize_sync(spa_t *spa, const dva_t *dva)
1772 {
1773 uint64_t asize = DVA_GET_ASIZE(dva);
1774 uint64_t dsize = asize;
1775
1776 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1777
1778 if (asize != 0 && spa->spa_deflate) {
1779 vdev_t *vd = vdev_lookup_top(spa, DVA_GET_VDEV(dva));
1780 if (vd != NULL)
1781 dsize = (asize >> SPA_MINBLOCKSHIFT) *
1782 vd->vdev_deflate_ratio;
1783 }
1784
1785 return (dsize);
1786 }
1787
1788 uint64_t
1789 bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp)
1790 {
1791 uint64_t dsize = 0;
1792 int d;
1793
1794 for (d = 0; d < BP_GET_NDVAS(bp); d++)
1795 dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1796
1797 return (dsize);
1798 }
1799
1800 uint64_t
1801 bp_get_dsize(spa_t *spa, const blkptr_t *bp)
1802 {
1803 uint64_t dsize = 0;
1804 int d;
1805
1806 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
1807
1808 for (d = 0; d < BP_GET_NDVAS(bp); d++)
1809 dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1810
1811 spa_config_exit(spa, SCL_VDEV, FTAG);
1812
1813 return (dsize);
1814 }
1815
1816 /*
1817 * ==========================================================================
1818 * Initialization and Termination
1819 * ==========================================================================
1820 */
1821
1822 static int
1823 spa_name_compare(const void *a1, const void *a2)
1824 {
1825 const spa_t *s1 = a1;
1826 const spa_t *s2 = a2;
1827 int s;
1828
1829 s = strcmp(s1->spa_name, s2->spa_name);
1830
1831 return (AVL_ISIGN(s));
1832 }
1833
1834 void
1835 spa_boot_init(void)
1836 {
1837 spa_config_load();
1838 }
1839
1840 void
1841 spa_init(int mode)
1842 {
1843 mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL);
1844 mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL);
1845 mutex_init(&spa_l2cache_lock, NULL, MUTEX_DEFAULT, NULL);
1846 cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL);
1847
1848 avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t),
1849 offsetof(spa_t, spa_avl));
1850
1851 avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_aux_t),
1852 offsetof(spa_aux_t, aux_avl));
1853
1854 avl_create(&spa_l2cache_avl, spa_l2cache_compare, sizeof (spa_aux_t),
1855 offsetof(spa_aux_t, aux_avl));
1856
1857 spa_mode_global = mode;
1858
1859 #ifndef _KERNEL
1860 if (spa_mode_global != FREAD && dprintf_find_string("watch")) {
1861 struct sigaction sa;
1862
1863 sa.sa_flags = SA_SIGINFO;
1864 sigemptyset(&sa.sa_mask);
1865 sa.sa_sigaction = arc_buf_sigsegv;
1866
1867 if (sigaction(SIGSEGV, &sa, NULL) == -1) {
1868 perror("could not enable watchpoints: "
1869 "sigaction(SIGSEGV, ...) = ");
1870 } else {
1871 arc_watch = B_TRUE;
1872 }
1873 }
1874 #endif
1875
1876 fm_init();
1877 refcount_init();
1878 unique_init();
1879 range_tree_init();
1880 metaslab_alloc_trace_init();
1881 ddt_init();
1882 zio_init();
1883 dmu_init();
1884 zil_init();
1885 vdev_cache_stat_init();
1886 vdev_raidz_math_init();
1887 vdev_file_init();
1888 zfs_prop_init();
1889 zpool_prop_init();
1890 zpool_feature_init();
1891 spa_config_load();
1892 l2arc_start();
1893 qat_init();
1894 }
1895
1896 void
1897 spa_fini(void)
1898 {
1899 l2arc_stop();
1900
1901 spa_evict_all();
1902
1903 vdev_file_fini();
1904 vdev_cache_stat_fini();
1905 vdev_raidz_math_fini();
1906 zil_fini();
1907 dmu_fini();
1908 zio_fini();
1909 ddt_fini();
1910 metaslab_alloc_trace_fini();
1911 range_tree_fini();
1912 unique_fini();
1913 refcount_fini();
1914 fm_fini();
1915 qat_fini();
1916
1917 avl_destroy(&spa_namespace_avl);
1918 avl_destroy(&spa_spare_avl);
1919 avl_destroy(&spa_l2cache_avl);
1920
1921 cv_destroy(&spa_namespace_cv);
1922 mutex_destroy(&spa_namespace_lock);
1923 mutex_destroy(&spa_spare_lock);
1924 mutex_destroy(&spa_l2cache_lock);
1925 }
1926
1927 /*
1928 * Return whether this pool has slogs. No locking needed.
1929 * It's not a problem if the wrong answer is returned as it's only for
1930 * performance and not correctness
1931 */
1932 boolean_t
1933 spa_has_slogs(spa_t *spa)
1934 {
1935 return (spa->spa_log_class->mc_rotor != NULL);
1936 }
1937
1938 spa_log_state_t
1939 spa_get_log_state(spa_t *spa)
1940 {
1941 return (spa->spa_log_state);
1942 }
1943
1944 void
1945 spa_set_log_state(spa_t *spa, spa_log_state_t state)
1946 {
1947 spa->spa_log_state = state;
1948 }
1949
1950 boolean_t
1951 spa_is_root(spa_t *spa)
1952 {
1953 return (spa->spa_is_root);
1954 }
1955
1956 boolean_t
1957 spa_writeable(spa_t *spa)
1958 {
1959 return (!!(spa->spa_mode & FWRITE));
1960 }
1961
1962 /*
1963 * Returns true if there is a pending sync task in any of the current
1964 * syncing txg, the current quiescing txg, or the current open txg.
1965 */
1966 boolean_t
1967 spa_has_pending_synctask(spa_t *spa)
1968 {
1969 return (!txg_all_lists_empty(&spa->spa_dsl_pool->dp_sync_tasks));
1970 }
1971
1972 int
1973 spa_mode(spa_t *spa)
1974 {
1975 return (spa->spa_mode);
1976 }
1977
1978 uint64_t
1979 spa_bootfs(spa_t *spa)
1980 {
1981 return (spa->spa_bootfs);
1982 }
1983
1984 uint64_t
1985 spa_delegation(spa_t *spa)
1986 {
1987 return (spa->spa_delegation);
1988 }
1989
1990 objset_t *
1991 spa_meta_objset(spa_t *spa)
1992 {
1993 return (spa->spa_meta_objset);
1994 }
1995
1996 enum zio_checksum
1997 spa_dedup_checksum(spa_t *spa)
1998 {
1999 return (spa->spa_dedup_checksum);
2000 }
2001
2002 /*
2003 * Reset pool scan stat per scan pass (or reboot).
2004 */
2005 void
2006 spa_scan_stat_init(spa_t *spa)
2007 {
2008 /* data not stored on disk */
2009 spa->spa_scan_pass_start = gethrestime_sec();
2010 spa->spa_scan_pass_exam = 0;
2011 vdev_scan_stat_init(spa->spa_root_vdev);
2012 }
2013
2014 /*
2015 * Get scan stats for zpool status reports
2016 */
2017 int
2018 spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps)
2019 {
2020 dsl_scan_t *scn = spa->spa_dsl_pool ? spa->spa_dsl_pool->dp_scan : NULL;
2021
2022 if (scn == NULL || scn->scn_phys.scn_func == POOL_SCAN_NONE)
2023 return (SET_ERROR(ENOENT));
2024 bzero(ps, sizeof (pool_scan_stat_t));
2025
2026 /* data stored on disk */
2027 ps->pss_func = scn->scn_phys.scn_func;
2028 ps->pss_start_time = scn->scn_phys.scn_start_time;
2029 ps->pss_end_time = scn->scn_phys.scn_end_time;
2030 ps->pss_to_examine = scn->scn_phys.scn_to_examine;
2031 ps->pss_examined = scn->scn_phys.scn_examined;
2032 ps->pss_to_process = scn->scn_phys.scn_to_process;
2033 ps->pss_processed = scn->scn_phys.scn_processed;
2034 ps->pss_errors = scn->scn_phys.scn_errors;
2035 ps->pss_state = scn->scn_phys.scn_state;
2036
2037 /* data not stored on disk */
2038 ps->pss_pass_start = spa->spa_scan_pass_start;
2039 ps->pss_pass_exam = spa->spa_scan_pass_exam;
2040
2041 return (0);
2042 }
2043
2044 boolean_t
2045 spa_debug_enabled(spa_t *spa)
2046 {
2047 return (spa->spa_debug);
2048 }
2049
2050 int
2051 spa_maxblocksize(spa_t *spa)
2052 {
2053 if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS))
2054 return (SPA_MAXBLOCKSIZE);
2055 else
2056 return (SPA_OLD_MAXBLOCKSIZE);
2057 }
2058
2059 int
2060 spa_maxdnodesize(spa_t *spa)
2061 {
2062 if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_DNODE))
2063 return (DNODE_MAX_SIZE);
2064 else
2065 return (DNODE_MIN_SIZE);
2066 }
2067
2068 #if defined(_KERNEL) && defined(HAVE_SPL)
2069 /* Namespace manipulation */
2070 EXPORT_SYMBOL(spa_lookup);
2071 EXPORT_SYMBOL(spa_add);
2072 EXPORT_SYMBOL(spa_remove);
2073 EXPORT_SYMBOL(spa_next);
2074
2075 /* Refcount functions */
2076 EXPORT_SYMBOL(spa_open_ref);
2077 EXPORT_SYMBOL(spa_close);
2078 EXPORT_SYMBOL(spa_refcount_zero);
2079
2080 /* Pool configuration lock */
2081 EXPORT_SYMBOL(spa_config_tryenter);
2082 EXPORT_SYMBOL(spa_config_enter);
2083 EXPORT_SYMBOL(spa_config_exit);
2084 EXPORT_SYMBOL(spa_config_held);
2085
2086 /* Pool vdev add/remove lock */
2087 EXPORT_SYMBOL(spa_vdev_enter);
2088 EXPORT_SYMBOL(spa_vdev_exit);
2089
2090 /* Pool vdev state change lock */
2091 EXPORT_SYMBOL(spa_vdev_state_enter);
2092 EXPORT_SYMBOL(spa_vdev_state_exit);
2093
2094 /* Accessor functions */
2095 EXPORT_SYMBOL(spa_shutting_down);
2096 EXPORT_SYMBOL(spa_get_dsl);
2097 EXPORT_SYMBOL(spa_get_rootblkptr);
2098 EXPORT_SYMBOL(spa_set_rootblkptr);
2099 EXPORT_SYMBOL(spa_altroot);
2100 EXPORT_SYMBOL(spa_sync_pass);
2101 EXPORT_SYMBOL(spa_name);
2102 EXPORT_SYMBOL(spa_guid);
2103 EXPORT_SYMBOL(spa_last_synced_txg);
2104 EXPORT_SYMBOL(spa_first_txg);
2105 EXPORT_SYMBOL(spa_syncing_txg);
2106 EXPORT_SYMBOL(spa_version);
2107 EXPORT_SYMBOL(spa_state);
2108 EXPORT_SYMBOL(spa_load_state);
2109 EXPORT_SYMBOL(spa_freeze_txg);
2110 EXPORT_SYMBOL(spa_get_dspace);
2111 EXPORT_SYMBOL(spa_update_dspace);
2112 EXPORT_SYMBOL(spa_deflate);
2113 EXPORT_SYMBOL(spa_normal_class);
2114 EXPORT_SYMBOL(spa_log_class);
2115 EXPORT_SYMBOL(spa_max_replication);
2116 EXPORT_SYMBOL(spa_prev_software_version);
2117 EXPORT_SYMBOL(spa_get_failmode);
2118 EXPORT_SYMBOL(spa_suspended);
2119 EXPORT_SYMBOL(spa_bootfs);
2120 EXPORT_SYMBOL(spa_delegation);
2121 EXPORT_SYMBOL(spa_meta_objset);
2122 EXPORT_SYMBOL(spa_maxblocksize);
2123 EXPORT_SYMBOL(spa_maxdnodesize);
2124
2125 /* Miscellaneous support routines */
2126 EXPORT_SYMBOL(spa_rename);
2127 EXPORT_SYMBOL(spa_guid_exists);
2128 EXPORT_SYMBOL(spa_strdup);
2129 EXPORT_SYMBOL(spa_strfree);
2130 EXPORT_SYMBOL(spa_get_random);
2131 EXPORT_SYMBOL(spa_generate_guid);
2132 EXPORT_SYMBOL(snprintf_blkptr);
2133 EXPORT_SYMBOL(spa_freeze);
2134 EXPORT_SYMBOL(spa_upgrade);
2135 EXPORT_SYMBOL(spa_evict_all);
2136 EXPORT_SYMBOL(spa_lookup_by_guid);
2137 EXPORT_SYMBOL(spa_has_spare);
2138 EXPORT_SYMBOL(dva_get_dsize_sync);
2139 EXPORT_SYMBOL(bp_get_dsize_sync);
2140 EXPORT_SYMBOL(bp_get_dsize);
2141 EXPORT_SYMBOL(spa_has_slogs);
2142 EXPORT_SYMBOL(spa_is_root);
2143 EXPORT_SYMBOL(spa_writeable);
2144 EXPORT_SYMBOL(spa_mode);
2145 EXPORT_SYMBOL(spa_namespace_lock);
2146
2147 /* BEGIN CSTYLED */
2148 module_param(zfs_flags, uint, 0644);
2149 MODULE_PARM_DESC(zfs_flags, "Set additional debugging flags");
2150
2151 module_param(zfs_recover, int, 0644);
2152 MODULE_PARM_DESC(zfs_recover, "Set to attempt to recover from fatal errors");
2153
2154 module_param(zfs_free_leak_on_eio, int, 0644);
2155 MODULE_PARM_DESC(zfs_free_leak_on_eio,
2156 "Set to ignore IO errors during free and permanently leak the space");
2157
2158 module_param(zfs_deadman_synctime_ms, ulong, 0644);
2159 MODULE_PARM_DESC(zfs_deadman_synctime_ms, "Expiration time in milliseconds");
2160
2161 module_param(zfs_deadman_checktime_ms, ulong, 0644);
2162 MODULE_PARM_DESC(zfs_deadman_checktime_ms,
2163 "Dead I/O check interval in milliseconds");
2164
2165 module_param(zfs_deadman_enabled, int, 0644);
2166 MODULE_PARM_DESC(zfs_deadman_enabled, "Enable deadman timer");
2167
2168 module_param(spa_asize_inflation, int, 0644);
2169 MODULE_PARM_DESC(spa_asize_inflation,
2170 "SPA size estimate multiplication factor");
2171
2172 module_param(spa_slop_shift, int, 0644);
2173 MODULE_PARM_DESC(spa_slop_shift, "Reserved free space in pool");
2174 /* END CSTYLED */
2175 #endif