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