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