]> CyberLeo.Net >> Repos - FreeBSD/releng/8.0.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/spa_misc.c
Adjust to reflect 8.0-RELEASE.
[FreeBSD/releng/8.0.git] / sys / cddl / contrib / opensolaris / uts / common / fs / 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 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25
26 #include <sys/zfs_context.h>
27 #include <sys/spa_impl.h>
28 #include <sys/zio.h>
29 #include <sys/zio_checksum.h>
30 #include <sys/zio_compress.h>
31 #include <sys/dmu.h>
32 #include <sys/dmu_tx.h>
33 #include <sys/zap.h>
34 #include <sys/zil.h>
35 #include <sys/vdev_impl.h>
36 #include <sys/metaslab.h>
37 #include <sys/uberblock_impl.h>
38 #include <sys/txg.h>
39 #include <sys/avl.h>
40 #include <sys/unique.h>
41 #include <sys/dsl_pool.h>
42 #include <sys/dsl_dir.h>
43 #include <sys/dsl_prop.h>
44 #include <sys/fs/zfs.h>
45 #include <sys/metaslab_impl.h>
46 #include <sys/sunddi.h>
47 #include <sys/arc.h>
48 #include "zfs_prop.h"
49
50 /*
51  * SPA locking
52  *
53  * There are four basic locks for managing spa_t structures:
54  *
55  * spa_namespace_lock (global mutex)
56  *
57  *      This lock must be acquired to do any of the following:
58  *
59  *              - Lookup a spa_t by name
60  *              - Add or remove a spa_t from the namespace
61  *              - Increase spa_refcount from non-zero
62  *              - Check if spa_refcount is zero
63  *              - Rename a spa_t
64  *              - add/remove/attach/detach devices
65  *              - Held for the duration of create/destroy/import/export
66  *
67  *      It does not need to handle recursion.  A create or destroy may
68  *      reference objects (files or zvols) in other pools, but by
69  *      definition they must have an existing reference, and will never need
70  *      to lookup a spa_t by name.
71  *
72  * spa_refcount (per-spa refcount_t protected by mutex)
73  *
74  *      This reference count keep track of any active users of the spa_t.  The
75  *      spa_t cannot be destroyed or freed while this is non-zero.  Internally,
76  *      the refcount is never really 'zero' - opening a pool implicitly keeps
77  *      some references in the DMU.  Internally we check against spa_minref, but
78  *      present the image of a zero/non-zero value to consumers.
79  *
80  * spa_config_lock[] (per-spa array of rwlocks)
81  *
82  *      This protects the spa_t from config changes, and must be held in
83  *      the following circumstances:
84  *
85  *              - RW_READER to perform I/O to the spa
86  *              - RW_WRITER to change the vdev config
87  *
88  * The locking order is fairly straightforward:
89  *
90  *              spa_namespace_lock      ->      spa_refcount
91  *
92  *      The namespace lock must be acquired to increase the refcount from 0
93  *      or to check if it is zero.
94  *
95  *              spa_refcount            ->      spa_config_lock[]
96  *
97  *      There must be at least one valid reference on the spa_t to acquire
98  *      the config lock.
99  *
100  *              spa_namespace_lock      ->      spa_config_lock[]
101  *
102  *      The namespace lock must always be taken before the config lock.
103  *
104  *
105  * The spa_namespace_lock can be acquired directly and is globally visible.
106  *
107  * The namespace is manipulated using the following functions, all of which
108  * require the spa_namespace_lock to be held.
109  *
110  *      spa_lookup()            Lookup a spa_t by name.
111  *
112  *      spa_add()               Create a new spa_t in the namespace.
113  *
114  *      spa_remove()            Remove a spa_t from the namespace.  This also
115  *                              frees up any memory associated with the spa_t.
116  *
117  *      spa_next()              Returns the next spa_t in the system, or the
118  *                              first if NULL is passed.
119  *
120  *      spa_evict_all()         Shutdown and remove all spa_t structures in
121  *                              the system.
122  *
123  *      spa_guid_exists()       Determine whether a pool/device guid exists.
124  *
125  * The spa_refcount is manipulated using the following functions:
126  *
127  *      spa_open_ref()          Adds a reference to the given spa_t.  Must be
128  *                              called with spa_namespace_lock held if the
129  *                              refcount is currently zero.
130  *
131  *      spa_close()             Remove a reference from the spa_t.  This will
132  *                              not free the spa_t or remove it from the
133  *                              namespace.  No locking is required.
134  *
135  *      spa_refcount_zero()     Returns true if the refcount is currently
136  *                              zero.  Must be called with spa_namespace_lock
137  *                              held.
138  *
139  * The spa_config_lock[] is an array of rwlocks, ordered as follows:
140  * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV.
141  * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}().
142  *
143  * To read the configuration, it suffices to hold one of these locks as reader.
144  * To modify the configuration, you must hold all locks as writer.  To modify
145  * vdev state without altering the vdev tree's topology (e.g. online/offline),
146  * you must hold SCL_STATE and SCL_ZIO as writer.
147  *
148  * We use these distinct config locks to avoid recursive lock entry.
149  * For example, spa_sync() (which holds SCL_CONFIG as reader) induces
150  * block allocations (SCL_ALLOC), which may require reading space maps
151  * from disk (dmu_read() -> zio_read() -> SCL_ZIO).
152  *
153  * The spa config locks cannot be normal rwlocks because we need the
154  * ability to hand off ownership.  For example, SCL_ZIO is acquired
155  * by the issuing thread and later released by an interrupt thread.
156  * They do, however, obey the usual write-wanted semantics to prevent
157  * writer (i.e. system administrator) starvation.
158  *
159  * The lock acquisition rules are as follows:
160  *
161  * SCL_CONFIG
162  *      Protects changes to the vdev tree topology, such as vdev
163  *      add/remove/attach/detach.  Protects the dirty config list
164  *      (spa_config_dirty_list) and the set of spares and l2arc devices.
165  *
166  * SCL_STATE
167  *      Protects changes to pool state and vdev state, such as vdev
168  *      online/offline/fault/degrade/clear.  Protects the dirty state list
169  *      (spa_state_dirty_list) and global pool state (spa_state).
170  *
171  * SCL_ALLOC
172  *      Protects changes to metaslab groups and classes.
173  *      Held as reader by metaslab_alloc() and metaslab_claim().
174  *
175  * SCL_ZIO
176  *      Held by bp-level zios (those which have no io_vd upon entry)
177  *      to prevent changes to the vdev tree.  The bp-level zio implicitly
178  *      protects all of its vdev child zios, which do not hold SCL_ZIO.
179  *
180  * SCL_FREE
181  *      Protects changes to metaslab groups and classes.
182  *      Held as reader by metaslab_free().  SCL_FREE is distinct from
183  *      SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free
184  *      blocks in zio_done() while another i/o that holds either
185  *      SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete.
186  *
187  * SCL_VDEV
188  *      Held as reader to prevent changes to the vdev tree during trivial
189  *      inquiries such as bp_get_dasize().  SCL_VDEV is distinct from the
190  *      other locks, and lower than all of them, to ensure that it's safe
191  *      to acquire regardless of caller context.
192  *
193  * In addition, the following rules apply:
194  *
195  * (a)  spa_props_lock protects pool properties, spa_config and spa_config_list.
196  *      The lock ordering is SCL_CONFIG > spa_props_lock.
197  *
198  * (b)  I/O operations on leaf vdevs.  For any zio operation that takes
199  *      an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(),
200  *      or zio_write_phys() -- the caller must ensure that the config cannot
201  *      cannot change in the interim, and that the vdev cannot be reopened.
202  *      SCL_STATE as reader suffices for both.
203  *
204  * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
205  *
206  *      spa_vdev_enter()        Acquire the namespace lock and the config lock
207  *                              for writing.
208  *
209  *      spa_vdev_exit()         Release the config lock, wait for all I/O
210  *                              to complete, sync the updated configs to the
211  *                              cache, and release the namespace lock.
212  *
213  * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit().
214  * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual
215  * locking is, always, based on spa_namespace_lock and spa_config_lock[].
216  *
217  * spa_rename() is also implemented within this file since is requires
218  * manipulation of the namespace.
219  */
220
221 static avl_tree_t spa_namespace_avl;
222 kmutex_t spa_namespace_lock;
223 static kcondvar_t spa_namespace_cv;
224 static int spa_active_count;
225 int spa_max_replication_override = SPA_DVAS_PER_BP;
226
227 static kmutex_t spa_spare_lock;
228 static avl_tree_t spa_spare_avl;
229 static kmutex_t spa_l2cache_lock;
230 static avl_tree_t spa_l2cache_avl;
231
232 kmem_cache_t *spa_buffer_pool;
233 int spa_mode;
234
235 #ifdef ZFS_DEBUG
236 /* Everything except dprintf is on by default in debug builds */
237 int zfs_flags = ~ZFS_DEBUG_DPRINTF;
238 #else
239 int zfs_flags = 0;
240 #endif
241
242 /*
243  * zfs_recover can be set to nonzero to attempt to recover from
244  * otherwise-fatal errors, typically caused by on-disk corruption.  When
245  * set, calls to zfs_panic_recover() will turn into warning messages.
246  */
247 int zfs_recover = 0;
248 SYSCTL_DECL(_vfs_zfs);
249 TUNABLE_INT("vfs.zfs.recover", &zfs_recover);
250 SYSCTL_INT(_vfs_zfs, OID_AUTO, recover, CTLFLAG_RDTUN, &zfs_recover, 0,
251     "Try to recover from otherwise-fatal errors.");
252
253
254 /*
255  * ==========================================================================
256  * SPA config locking
257  * ==========================================================================
258  */
259 static void
260 spa_config_lock_init(spa_t *spa)
261 {
262         for (int i = 0; i < SCL_LOCKS; i++) {
263                 spa_config_lock_t *scl = &spa->spa_config_lock[i];
264                 mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL);
265                 cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL);
266                 refcount_create(&scl->scl_count);
267                 scl->scl_writer = NULL;
268                 scl->scl_write_wanted = 0;
269         }
270 }
271
272 static void
273 spa_config_lock_destroy(spa_t *spa)
274 {
275         for (int i = 0; i < SCL_LOCKS; i++) {
276                 spa_config_lock_t *scl = &spa->spa_config_lock[i];
277                 mutex_destroy(&scl->scl_lock);
278                 cv_destroy(&scl->scl_cv);
279                 refcount_destroy(&scl->scl_count);
280                 ASSERT(scl->scl_writer == NULL);
281                 ASSERT(scl->scl_write_wanted == 0);
282         }
283 }
284
285 int
286 spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw)
287 {
288         for (int i = 0; i < SCL_LOCKS; i++) {
289                 spa_config_lock_t *scl = &spa->spa_config_lock[i];
290                 if (!(locks & (1 << i)))
291                         continue;
292                 mutex_enter(&scl->scl_lock);
293                 if (rw == RW_READER) {
294                         if (scl->scl_writer || scl->scl_write_wanted) {
295                                 mutex_exit(&scl->scl_lock);
296                                 spa_config_exit(spa, locks ^ (1 << i), tag);
297                                 return (0);
298                         }
299                 } else {
300                         ASSERT(scl->scl_writer != curthread);
301                         if (!refcount_is_zero(&scl->scl_count)) {
302                                 mutex_exit(&scl->scl_lock);
303                                 spa_config_exit(spa, locks ^ (1 << i), tag);
304                                 return (0);
305                         }
306                         scl->scl_writer = curthread;
307                 }
308                 (void) refcount_add(&scl->scl_count, tag);
309                 mutex_exit(&scl->scl_lock);
310         }
311         return (1);
312 }
313
314 void
315 spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw)
316 {
317         for (int i = 0; i < SCL_LOCKS; i++) {
318                 spa_config_lock_t *scl = &spa->spa_config_lock[i];
319                 if (!(locks & (1 << i)))
320                         continue;
321                 mutex_enter(&scl->scl_lock);
322                 if (rw == RW_READER) {
323                         while (scl->scl_writer || scl->scl_write_wanted) {
324                                 cv_wait(&scl->scl_cv, &scl->scl_lock);
325                         }
326                 } else {
327                         ASSERT(scl->scl_writer != curthread);
328                         while (!refcount_is_zero(&scl->scl_count)) {
329                                 scl->scl_write_wanted++;
330                                 cv_wait(&scl->scl_cv, &scl->scl_lock);
331                                 scl->scl_write_wanted--;
332                         }
333                         scl->scl_writer = curthread;
334                 }
335                 (void) refcount_add(&scl->scl_count, tag);
336                 mutex_exit(&scl->scl_lock);
337         }
338 }
339
340 void
341 spa_config_exit(spa_t *spa, int locks, void *tag)
342 {
343         for (int i = SCL_LOCKS - 1; i >= 0; i--) {
344                 spa_config_lock_t *scl = &spa->spa_config_lock[i];
345                 if (!(locks & (1 << i)))
346                         continue;
347                 mutex_enter(&scl->scl_lock);
348                 ASSERT(!refcount_is_zero(&scl->scl_count));
349                 if (refcount_remove(&scl->scl_count, tag) == 0) {
350                         ASSERT(scl->scl_writer == NULL ||
351                             scl->scl_writer == curthread);
352                         scl->scl_writer = NULL; /* OK in either case */
353                         cv_broadcast(&scl->scl_cv);
354                 }
355                 mutex_exit(&scl->scl_lock);
356         }
357 }
358
359 int
360 spa_config_held(spa_t *spa, int locks, krw_t rw)
361 {
362         int locks_held = 0;
363
364         for (int i = 0; i < SCL_LOCKS; i++) {
365                 spa_config_lock_t *scl = &spa->spa_config_lock[i];
366                 if (!(locks & (1 << i)))
367                         continue;
368                 if ((rw == RW_READER && !refcount_is_zero(&scl->scl_count)) ||
369                     (rw == RW_WRITER && scl->scl_writer == curthread))
370                         locks_held |= 1 << i;
371         }
372
373         return (locks_held);
374 }
375
376 /*
377  * ==========================================================================
378  * SPA namespace functions
379  * ==========================================================================
380  */
381
382 /*
383  * Lookup the named spa_t in the AVL tree.  The spa_namespace_lock must be held.
384  * Returns NULL if no matching spa_t is found.
385  */
386 spa_t *
387 spa_lookup(const char *name)
388 {
389         static spa_t search;    /* spa_t is large; don't allocate on stack */
390         spa_t *spa;
391         avl_index_t where;
392         char c;
393         char *cp;
394
395         ASSERT(MUTEX_HELD(&spa_namespace_lock));
396
397         /*
398          * If it's a full dataset name, figure out the pool name and
399          * just use that.
400          */
401         cp = strpbrk(name, "/@");
402         if (cp) {
403                 c = *cp;
404                 *cp = '\0';
405         }
406
407         (void) strlcpy(search.spa_name, name, sizeof (search.spa_name));
408         spa = avl_find(&spa_namespace_avl, &search, &where);
409
410         if (cp)
411                 *cp = c;
412
413         return (spa);
414 }
415
416 /*
417  * Create an uninitialized spa_t with the given name.  Requires
418  * spa_namespace_lock.  The caller must ensure that the spa_t doesn't already
419  * exist by calling spa_lookup() first.
420  */
421 spa_t *
422 spa_add(const char *name, const char *altroot)
423 {
424         spa_t *spa;
425         spa_config_dirent_t *dp;
426
427         ASSERT(MUTEX_HELD(&spa_namespace_lock));
428
429         spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
430
431         rw_init(&spa->spa_traverse_lock, NULL, RW_DEFAULT, NULL);
432
433         mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL);
434         mutex_init(&spa->spa_async_root_lock, NULL, MUTEX_DEFAULT, NULL);
435         mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL);
436         mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL);
437         mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL);
438         mutex_init(&spa->spa_sync_bplist.bpl_lock, NULL, MUTEX_DEFAULT, NULL);
439         mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL);
440         mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL);
441
442         cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL);
443         cv_init(&spa->spa_async_root_cv, NULL, CV_DEFAULT, NULL);
444         cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL);
445         cv_init(&spa->spa_suspend_cv, NULL, CV_DEFAULT, NULL);
446
447         (void) strlcpy(spa->spa_name, name, sizeof (spa->spa_name));
448         spa->spa_state = POOL_STATE_UNINITIALIZED;
449         spa->spa_freeze_txg = UINT64_MAX;
450         spa->spa_final_txg = UINT64_MAX;
451
452         refcount_create(&spa->spa_refcount);
453         spa_config_lock_init(spa);
454
455         avl_add(&spa_namespace_avl, spa);
456
457         mutex_init(&spa->spa_suspend_lock, NULL, MUTEX_DEFAULT, NULL);
458
459         /*
460          * Set the alternate root, if there is one.
461          */
462         if (altroot) {
463                 spa->spa_root = spa_strdup(altroot);
464                 spa_active_count++;
465         }
466
467         /*
468          * Every pool starts with the default cachefile
469          */
470         list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t),
471             offsetof(spa_config_dirent_t, scd_link));
472
473         dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP);
474         dp->scd_path = spa_strdup(spa_config_path);
475         list_insert_head(&spa->spa_config_list, dp);
476
477         return (spa);
478 }
479
480 /*
481  * Removes a spa_t from the namespace, freeing up any memory used.  Requires
482  * spa_namespace_lock.  This is called only after the spa_t has been closed and
483  * deactivated.
484  */
485 void
486 spa_remove(spa_t *spa)
487 {
488         spa_config_dirent_t *dp;
489
490         ASSERT(MUTEX_HELD(&spa_namespace_lock));
491         ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
492
493         avl_remove(&spa_namespace_avl, spa);
494         cv_broadcast(&spa_namespace_cv);
495
496         if (spa->spa_root) {
497                 spa_strfree(spa->spa_root);
498                 spa_active_count--;
499         }
500
501         while ((dp = list_head(&spa->spa_config_list)) != NULL) {
502                 list_remove(&spa->spa_config_list, dp);
503                 if (dp->scd_path != NULL)
504                         spa_strfree(dp->scd_path);
505                 kmem_free(dp, sizeof (spa_config_dirent_t));
506         }
507
508         list_destroy(&spa->spa_config_list);
509
510         spa_config_set(spa, NULL);
511
512         refcount_destroy(&spa->spa_refcount);
513
514         spa_config_lock_destroy(spa);
515
516         rw_destroy(&spa->spa_traverse_lock);
517
518         cv_destroy(&spa->spa_async_cv);
519         cv_destroy(&spa->spa_async_root_cv);
520         cv_destroy(&spa->spa_scrub_io_cv);
521         cv_destroy(&spa->spa_suspend_cv);
522
523         mutex_destroy(&spa->spa_async_lock);
524         mutex_destroy(&spa->spa_async_root_lock);
525         mutex_destroy(&spa->spa_scrub_lock);
526         mutex_destroy(&spa->spa_errlog_lock);
527         mutex_destroy(&spa->spa_errlist_lock);
528         mutex_destroy(&spa->spa_sync_bplist.bpl_lock);
529         mutex_destroy(&spa->spa_history_lock);
530         mutex_destroy(&spa->spa_props_lock);
531         mutex_destroy(&spa->spa_suspend_lock);
532
533         kmem_free(spa, sizeof (spa_t));
534 }
535
536 /*
537  * Given a pool, return the next pool in the namespace, or NULL if there is
538  * none.  If 'prev' is NULL, return the first pool.
539  */
540 spa_t *
541 spa_next(spa_t *prev)
542 {
543         ASSERT(MUTEX_HELD(&spa_namespace_lock));
544
545         if (prev)
546                 return (AVL_NEXT(&spa_namespace_avl, prev));
547         else
548                 return (avl_first(&spa_namespace_avl));
549 }
550
551 /*
552  * ==========================================================================
553  * SPA refcount functions
554  * ==========================================================================
555  */
556
557 /*
558  * Add a reference to the given spa_t.  Must have at least one reference, or
559  * have the namespace lock held.
560  */
561 void
562 spa_open_ref(spa_t *spa, void *tag)
563 {
564         ASSERT(refcount_count(&spa->spa_refcount) >= spa->spa_minref ||
565             MUTEX_HELD(&spa_namespace_lock));
566         (void) refcount_add(&spa->spa_refcount, tag);
567 }
568
569 /*
570  * Remove a reference to the given spa_t.  Must have at least one reference, or
571  * have the namespace lock held.
572  */
573 void
574 spa_close(spa_t *spa, void *tag)
575 {
576         ASSERT(refcount_count(&spa->spa_refcount) > spa->spa_minref ||
577             MUTEX_HELD(&spa_namespace_lock));
578         (void) refcount_remove(&spa->spa_refcount, tag);
579 }
580
581 /*
582  * Check to see if the spa refcount is zero.  Must be called with
583  * spa_namespace_lock held.  We really compare against spa_minref, which is the
584  * number of references acquired when opening a pool
585  */
586 boolean_t
587 spa_refcount_zero(spa_t *spa)
588 {
589         ASSERT(MUTEX_HELD(&spa_namespace_lock));
590
591         return (refcount_count(&spa->spa_refcount) == spa->spa_minref);
592 }
593
594 /*
595  * ==========================================================================
596  * SPA spare and l2cache tracking
597  * ==========================================================================
598  */
599
600 /*
601  * Hot spares and cache devices are tracked using the same code below,
602  * for 'auxiliary' devices.
603  */
604
605 typedef struct spa_aux {
606         uint64_t        aux_guid;
607         uint64_t        aux_pool;
608         avl_node_t      aux_avl;
609         int             aux_count;
610 } spa_aux_t;
611
612 static int
613 spa_aux_compare(const void *a, const void *b)
614 {
615         const spa_aux_t *sa = a;
616         const spa_aux_t *sb = b;
617
618         if (sa->aux_guid < sb->aux_guid)
619                 return (-1);
620         else if (sa->aux_guid > sb->aux_guid)
621                 return (1);
622         else
623                 return (0);
624 }
625
626 void
627 spa_aux_add(vdev_t *vd, avl_tree_t *avl)
628 {
629         avl_index_t where;
630         spa_aux_t search;
631         spa_aux_t *aux;
632
633         search.aux_guid = vd->vdev_guid;
634         if ((aux = avl_find(avl, &search, &where)) != NULL) {
635                 aux->aux_count++;
636         } else {
637                 aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP);
638                 aux->aux_guid = vd->vdev_guid;
639                 aux->aux_count = 1;
640                 avl_insert(avl, aux, where);
641         }
642 }
643
644 void
645 spa_aux_remove(vdev_t *vd, avl_tree_t *avl)
646 {
647         spa_aux_t search;
648         spa_aux_t *aux;
649         avl_index_t where;
650
651         search.aux_guid = vd->vdev_guid;
652         aux = avl_find(avl, &search, &where);
653
654         ASSERT(aux != NULL);
655
656         if (--aux->aux_count == 0) {
657                 avl_remove(avl, aux);
658                 kmem_free(aux, sizeof (spa_aux_t));
659         } else if (aux->aux_pool == spa_guid(vd->vdev_spa)) {
660                 aux->aux_pool = 0ULL;
661         }
662 }
663
664 boolean_t
665 spa_aux_exists(uint64_t guid, uint64_t *pool, int *refcnt, avl_tree_t *avl)
666 {
667         spa_aux_t search, *found;
668
669         search.aux_guid = guid;
670         found = avl_find(avl, &search, NULL);
671
672         if (pool) {
673                 if (found)
674                         *pool = found->aux_pool;
675                 else
676                         *pool = 0ULL;
677         }
678
679         if (refcnt) {
680                 if (found)
681                         *refcnt = found->aux_count;
682                 else
683                         *refcnt = 0;
684         }
685
686         return (found != NULL);
687 }
688
689 void
690 spa_aux_activate(vdev_t *vd, avl_tree_t *avl)
691 {
692         spa_aux_t search, *found;
693         avl_index_t where;
694
695         search.aux_guid = vd->vdev_guid;
696         found = avl_find(avl, &search, &where);
697         ASSERT(found != NULL);
698         ASSERT(found->aux_pool == 0ULL);
699
700         found->aux_pool = spa_guid(vd->vdev_spa);
701 }
702
703 /*
704  * Spares are tracked globally due to the following constraints:
705  *
706  *      - A spare may be part of multiple pools.
707  *      - A spare may be added to a pool even if it's actively in use within
708  *        another pool.
709  *      - A spare in use in any pool can only be the source of a replacement if
710  *        the target is a spare in the same pool.
711  *
712  * We keep track of all spares on the system through the use of a reference
713  * counted AVL tree.  When a vdev is added as a spare, or used as a replacement
714  * spare, then we bump the reference count in the AVL tree.  In addition, we set
715  * the 'vdev_isspare' member to indicate that the device is a spare (active or
716  * inactive).  When a spare is made active (used to replace a device in the
717  * pool), we also keep track of which pool its been made a part of.
718  *
719  * The 'spa_spare_lock' protects the AVL tree.  These functions are normally
720  * called under the spa_namespace lock as part of vdev reconfiguration.  The
721  * separate spare lock exists for the status query path, which does not need to
722  * be completely consistent with respect to other vdev configuration changes.
723  */
724
725 static int
726 spa_spare_compare(const void *a, const void *b)
727 {
728         return (spa_aux_compare(a, b));
729 }
730
731 void
732 spa_spare_add(vdev_t *vd)
733 {
734         mutex_enter(&spa_spare_lock);
735         ASSERT(!vd->vdev_isspare);
736         spa_aux_add(vd, &spa_spare_avl);
737         vd->vdev_isspare = B_TRUE;
738         mutex_exit(&spa_spare_lock);
739 }
740
741 void
742 spa_spare_remove(vdev_t *vd)
743 {
744         mutex_enter(&spa_spare_lock);
745         ASSERT(vd->vdev_isspare);
746         spa_aux_remove(vd, &spa_spare_avl);
747         vd->vdev_isspare = B_FALSE;
748         mutex_exit(&spa_spare_lock);
749 }
750
751 boolean_t
752 spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt)
753 {
754         boolean_t found;
755
756         mutex_enter(&spa_spare_lock);
757         found = spa_aux_exists(guid, pool, refcnt, &spa_spare_avl);
758         mutex_exit(&spa_spare_lock);
759
760         return (found);
761 }
762
763 void
764 spa_spare_activate(vdev_t *vd)
765 {
766         mutex_enter(&spa_spare_lock);
767         ASSERT(vd->vdev_isspare);
768         spa_aux_activate(vd, &spa_spare_avl);
769         mutex_exit(&spa_spare_lock);
770 }
771
772 /*
773  * Level 2 ARC devices are tracked globally for the same reasons as spares.
774  * Cache devices currently only support one pool per cache device, and so
775  * for these devices the aux reference count is currently unused beyond 1.
776  */
777
778 static int
779 spa_l2cache_compare(const void *a, const void *b)
780 {
781         return (spa_aux_compare(a, b));
782 }
783
784 void
785 spa_l2cache_add(vdev_t *vd)
786 {
787         mutex_enter(&spa_l2cache_lock);
788         ASSERT(!vd->vdev_isl2cache);
789         spa_aux_add(vd, &spa_l2cache_avl);
790         vd->vdev_isl2cache = B_TRUE;
791         mutex_exit(&spa_l2cache_lock);
792 }
793
794 void
795 spa_l2cache_remove(vdev_t *vd)
796 {
797         mutex_enter(&spa_l2cache_lock);
798         ASSERT(vd->vdev_isl2cache);
799         spa_aux_remove(vd, &spa_l2cache_avl);
800         vd->vdev_isl2cache = B_FALSE;
801         mutex_exit(&spa_l2cache_lock);
802 }
803
804 boolean_t
805 spa_l2cache_exists(uint64_t guid, uint64_t *pool)
806 {
807         boolean_t found;
808
809         mutex_enter(&spa_l2cache_lock);
810         found = spa_aux_exists(guid, pool, NULL, &spa_l2cache_avl);
811         mutex_exit(&spa_l2cache_lock);
812
813         return (found);
814 }
815
816 void
817 spa_l2cache_activate(vdev_t *vd)
818 {
819         mutex_enter(&spa_l2cache_lock);
820         ASSERT(vd->vdev_isl2cache);
821         spa_aux_activate(vd, &spa_l2cache_avl);
822         mutex_exit(&spa_l2cache_lock);
823 }
824
825 void
826 spa_l2cache_space_update(vdev_t *vd, int64_t space, int64_t alloc)
827 {
828         vdev_space_update(vd, space, alloc, B_FALSE);
829 }
830
831 /*
832  * ==========================================================================
833  * SPA vdev locking
834  * ==========================================================================
835  */
836
837 /*
838  * Lock the given spa_t for the purpose of adding or removing a vdev.
839  * Grabs the global spa_namespace_lock plus the spa config lock for writing.
840  * It returns the next transaction group for the spa_t.
841  */
842 uint64_t
843 spa_vdev_enter(spa_t *spa)
844 {
845         mutex_enter(&spa_namespace_lock);
846
847         spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
848
849         return (spa_last_synced_txg(spa) + 1);
850 }
851
852 /*
853  * Unlock the spa_t after adding or removing a vdev.  Besides undoing the
854  * locking of spa_vdev_enter(), we also want make sure the transactions have
855  * synced to disk, and then update the global configuration cache with the new
856  * information.
857  */
858 int
859 spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
860 {
861         int config_changed = B_FALSE;
862
863         ASSERT(txg > spa_last_synced_txg(spa));
864
865         spa->spa_pending_vdev = NULL;
866
867         /*
868          * Reassess the DTLs.
869          */
870         vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE);
871
872         /*
873          * If the config changed, notify the scrub thread that it must restart.
874          */
875         if (error == 0 && !list_is_empty(&spa->spa_config_dirty_list)) {
876                 dsl_pool_scrub_restart(spa->spa_dsl_pool);
877                 config_changed = B_TRUE;
878         }
879
880         spa_config_exit(spa, SCL_ALL, spa);
881
882         /*
883          * Note: this txg_wait_synced() is important because it ensures
884          * that there won't be more than one config change per txg.
885          * This allows us to use the txg as the generation number.
886          */
887         if (error == 0)
888                 txg_wait_synced(spa->spa_dsl_pool, txg);
889
890         if (vd != NULL) {
891                 ASSERT(!vd->vdev_detached || vd->vdev_dtl.smo_object == 0);
892                 vdev_free(vd);
893         }
894
895         /*
896          * If the config changed, update the config cache.
897          */
898         if (config_changed)
899                 spa_config_sync(spa, B_FALSE, B_TRUE);
900
901         mutex_exit(&spa_namespace_lock);
902
903         return (error);
904 }
905
906 /*
907  * Lock the given spa_t for the purpose of changing vdev state.
908  */
909 void
910 spa_vdev_state_enter(spa_t *spa)
911 {
912         spa_config_enter(spa, SCL_STATE_ALL, spa, RW_WRITER);
913 }
914
915 int
916 spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error)
917 {
918         if (vd != NULL)
919                 vdev_state_dirty(vd->vdev_top);
920
921         spa_config_exit(spa, SCL_STATE_ALL, spa);
922
923         return (error);
924 }
925
926 /*
927  * ==========================================================================
928  * Miscellaneous functions
929  * ==========================================================================
930  */
931
932 /*
933  * Rename a spa_t.
934  */
935 int
936 spa_rename(const char *name, const char *newname)
937 {
938         spa_t *spa;
939         int err;
940
941         /*
942          * Lookup the spa_t and grab the config lock for writing.  We need to
943          * actually open the pool so that we can sync out the necessary labels.
944          * It's OK to call spa_open() with the namespace lock held because we
945          * allow recursive calls for other reasons.
946          */
947         mutex_enter(&spa_namespace_lock);
948         if ((err = spa_open(name, &spa, FTAG)) != 0) {
949                 mutex_exit(&spa_namespace_lock);
950                 return (err);
951         }
952
953         spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
954
955         avl_remove(&spa_namespace_avl, spa);
956         (void) strlcpy(spa->spa_name, newname, sizeof (spa->spa_name));
957         avl_add(&spa_namespace_avl, spa);
958
959         /*
960          * Sync all labels to disk with the new names by marking the root vdev
961          * dirty and waiting for it to sync.  It will pick up the new pool name
962          * during the sync.
963          */
964         vdev_config_dirty(spa->spa_root_vdev);
965
966         spa_config_exit(spa, SCL_ALL, FTAG);
967
968         txg_wait_synced(spa->spa_dsl_pool, 0);
969
970         /*
971          * Sync the updated config cache.
972          */
973         spa_config_sync(spa, B_FALSE, B_TRUE);
974
975         spa_close(spa, FTAG);
976
977         mutex_exit(&spa_namespace_lock);
978
979         return (0);
980 }
981
982
983 /*
984  * Determine whether a pool with given pool_guid exists.  If device_guid is
985  * non-zero, determine whether the pool exists *and* contains a device with the
986  * specified device_guid.
987  */
988 boolean_t
989 spa_guid_exists(uint64_t pool_guid, uint64_t device_guid)
990 {
991         spa_t *spa;
992         avl_tree_t *t = &spa_namespace_avl;
993
994         ASSERT(MUTEX_HELD(&spa_namespace_lock));
995
996         for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) {
997                 if (spa->spa_state == POOL_STATE_UNINITIALIZED)
998                         continue;
999                 if (spa->spa_root_vdev == NULL)
1000                         continue;
1001                 if (spa_guid(spa) == pool_guid) {
1002                         if (device_guid == 0)
1003                                 break;
1004
1005                         if (vdev_lookup_by_guid(spa->spa_root_vdev,
1006                             device_guid) != NULL)
1007                                 break;
1008
1009                         /*
1010                          * Check any devices we may be in the process of adding.
1011                          */
1012                         if (spa->spa_pending_vdev) {
1013                                 if (vdev_lookup_by_guid(spa->spa_pending_vdev,
1014                                     device_guid) != NULL)
1015                                         break;
1016                         }
1017                 }
1018         }
1019
1020         return (spa != NULL);
1021 }
1022
1023 char *
1024 spa_strdup(const char *s)
1025 {
1026         size_t len;
1027         char *new;
1028
1029         len = strlen(s);
1030         new = kmem_alloc(len + 1, KM_SLEEP);
1031         bcopy(s, new, len);
1032         new[len] = '\0';
1033
1034         return (new);
1035 }
1036
1037 void
1038 spa_strfree(char *s)
1039 {
1040         kmem_free(s, strlen(s) + 1);
1041 }
1042
1043 uint64_t
1044 spa_get_random(uint64_t range)
1045 {
1046         uint64_t r;
1047
1048         ASSERT(range != 0);
1049
1050         (void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t));
1051
1052         return (r % range);
1053 }
1054
1055 void
1056 sprintf_blkptr(char *buf, int len, const blkptr_t *bp)
1057 {
1058         int d;
1059
1060         if (bp == NULL) {
1061                 (void) snprintf(buf, len, "<NULL>");
1062                 return;
1063         }
1064
1065         if (BP_IS_HOLE(bp)) {
1066                 (void) snprintf(buf, len, "<hole>");
1067                 return;
1068         }
1069
1070         (void) snprintf(buf, len, "[L%llu %s] %llxL/%llxP ",
1071             (u_longlong_t)BP_GET_LEVEL(bp),
1072             dmu_ot[BP_GET_TYPE(bp)].ot_name,
1073             (u_longlong_t)BP_GET_LSIZE(bp),
1074             (u_longlong_t)BP_GET_PSIZE(bp));
1075
1076         for (d = 0; d < BP_GET_NDVAS(bp); d++) {
1077                 const dva_t *dva = &bp->blk_dva[d];
1078                 (void) snprintf(buf + strlen(buf), len - strlen(buf),
1079                     "DVA[%d]=<%llu:%llx:%llx> ", d,
1080                     (u_longlong_t)DVA_GET_VDEV(dva),
1081                     (u_longlong_t)DVA_GET_OFFSET(dva),
1082                     (u_longlong_t)DVA_GET_ASIZE(dva));
1083         }
1084
1085         (void) snprintf(buf + strlen(buf), len - strlen(buf),
1086             "%s %s %s %s birth=%llu fill=%llu cksum=%llx:%llx:%llx:%llx",
1087             zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name,
1088             zio_compress_table[BP_GET_COMPRESS(bp)].ci_name,
1089             BP_GET_BYTEORDER(bp) == 0 ? "BE" : "LE",
1090             BP_IS_GANG(bp) ? "gang" : "contiguous",
1091             (u_longlong_t)bp->blk_birth,
1092             (u_longlong_t)bp->blk_fill,
1093             (u_longlong_t)bp->blk_cksum.zc_word[0],
1094             (u_longlong_t)bp->blk_cksum.zc_word[1],
1095             (u_longlong_t)bp->blk_cksum.zc_word[2],
1096             (u_longlong_t)bp->blk_cksum.zc_word[3]);
1097 }
1098
1099 void
1100 spa_freeze(spa_t *spa)
1101 {
1102         uint64_t freeze_txg = 0;
1103
1104         spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1105         if (spa->spa_freeze_txg == UINT64_MAX) {
1106                 freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE;
1107                 spa->spa_freeze_txg = freeze_txg;
1108         }
1109         spa_config_exit(spa, SCL_ALL, FTAG);
1110         if (freeze_txg != 0)
1111                 txg_wait_synced(spa_get_dsl(spa), freeze_txg);
1112 }
1113
1114 void
1115 zfs_panic_recover(const char *fmt, ...)
1116 {
1117         va_list adx;
1118
1119         va_start(adx, fmt);
1120         vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx);
1121         va_end(adx);
1122 }
1123
1124 /*
1125  * ==========================================================================
1126  * Accessor functions
1127  * ==========================================================================
1128  */
1129
1130 krwlock_t *
1131 spa_traverse_rwlock(spa_t *spa)
1132 {
1133         return (&spa->spa_traverse_lock);
1134 }
1135
1136 boolean_t
1137 spa_traverse_wanted(spa_t *spa)
1138 {
1139         return (spa->spa_traverse_wanted);
1140 }
1141
1142 dsl_pool_t *
1143 spa_get_dsl(spa_t *spa)
1144 {
1145         return (spa->spa_dsl_pool);
1146 }
1147
1148 blkptr_t *
1149 spa_get_rootblkptr(spa_t *spa)
1150 {
1151         return (&spa->spa_ubsync.ub_rootbp);
1152 }
1153
1154 void
1155 spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp)
1156 {
1157         spa->spa_uberblock.ub_rootbp = *bp;
1158 }
1159
1160 void
1161 spa_altroot(spa_t *spa, char *buf, size_t buflen)
1162 {
1163         if (spa->spa_root == NULL)
1164                 buf[0] = '\0';
1165         else
1166                 (void) strncpy(buf, spa->spa_root, buflen);
1167 }
1168
1169 int
1170 spa_sync_pass(spa_t *spa)
1171 {
1172         return (spa->spa_sync_pass);
1173 }
1174
1175 char *
1176 spa_name(spa_t *spa)
1177 {
1178         return (spa->spa_name);
1179 }
1180
1181 uint64_t
1182 spa_guid(spa_t *spa)
1183 {
1184         /*
1185          * If we fail to parse the config during spa_load(), we can go through
1186          * the error path (which posts an ereport) and end up here with no root
1187          * vdev.  We stash the original pool guid in 'spa_load_guid' to handle
1188          * this case.
1189          */
1190         if (spa->spa_root_vdev != NULL)
1191                 return (spa->spa_root_vdev->vdev_guid);
1192         else
1193                 return (spa->spa_load_guid);
1194 }
1195
1196 uint64_t
1197 spa_last_synced_txg(spa_t *spa)
1198 {
1199         return (spa->spa_ubsync.ub_txg);
1200 }
1201
1202 uint64_t
1203 spa_first_txg(spa_t *spa)
1204 {
1205         return (spa->spa_first_txg);
1206 }
1207
1208 int
1209 spa_state(spa_t *spa)
1210 {
1211         return (spa->spa_state);
1212 }
1213
1214 uint64_t
1215 spa_freeze_txg(spa_t *spa)
1216 {
1217         return (spa->spa_freeze_txg);
1218 }
1219
1220 /*
1221  * Return how much space is allocated in the pool (ie. sum of all asize)
1222  */
1223 uint64_t
1224 spa_get_alloc(spa_t *spa)
1225 {
1226         return (spa->spa_root_vdev->vdev_stat.vs_alloc);
1227 }
1228
1229 /*
1230  * Return how much (raid-z inflated) space there is in the pool.
1231  */
1232 uint64_t
1233 spa_get_space(spa_t *spa)
1234 {
1235         return (spa->spa_root_vdev->vdev_stat.vs_space);
1236 }
1237
1238 /*
1239  * Return the amount of raid-z-deflated space in the pool.
1240  */
1241 uint64_t
1242 spa_get_dspace(spa_t *spa)
1243 {
1244         if (spa->spa_deflate)
1245                 return (spa->spa_root_vdev->vdev_stat.vs_dspace);
1246         else
1247                 return (spa->spa_root_vdev->vdev_stat.vs_space);
1248 }
1249
1250 /* ARGSUSED */
1251 uint64_t
1252 spa_get_asize(spa_t *spa, uint64_t lsize)
1253 {
1254         /*
1255          * For now, the worst case is 512-byte RAID-Z blocks, in which
1256          * case the space requirement is exactly 2x; so just assume that.
1257          * Add to this the fact that we can have up to 3 DVAs per bp, and
1258          * we have to multiply by a total of 6x.
1259          */
1260         return (lsize * 6);
1261 }
1262
1263 /*
1264  * Return the failure mode that has been set to this pool. The default
1265  * behavior will be to block all I/Os when a complete failure occurs.
1266  */
1267 uint8_t
1268 spa_get_failmode(spa_t *spa)
1269 {
1270         return (spa->spa_failmode);
1271 }
1272
1273 boolean_t
1274 spa_suspended(spa_t *spa)
1275 {
1276         return (spa->spa_suspended);
1277 }
1278
1279 uint64_t
1280 spa_version(spa_t *spa)
1281 {
1282         return (spa->spa_ubsync.ub_version);
1283 }
1284
1285 int
1286 spa_max_replication(spa_t *spa)
1287 {
1288         /*
1289          * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
1290          * handle BPs with more than one DVA allocated.  Set our max
1291          * replication level accordingly.
1292          */
1293         if (spa_version(spa) < SPA_VERSION_DITTO_BLOCKS)
1294                 return (1);
1295         return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override));
1296 }
1297
1298 uint64_t
1299 bp_get_dasize(spa_t *spa, const blkptr_t *bp)
1300 {
1301         int sz = 0, i;
1302
1303         if (!spa->spa_deflate)
1304                 return (BP_GET_ASIZE(bp));
1305
1306         spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
1307         for (i = 0; i < SPA_DVAS_PER_BP; i++) {
1308                 vdev_t *vd =
1309                     vdev_lookup_top(spa, DVA_GET_VDEV(&bp->blk_dva[i]));
1310                 if (vd)
1311                         sz += (DVA_GET_ASIZE(&bp->blk_dva[i]) >>
1312                             SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio;
1313         }
1314         spa_config_exit(spa, SCL_VDEV, FTAG);
1315         return (sz);
1316 }
1317
1318 /*
1319  * ==========================================================================
1320  * Initialization and Termination
1321  * ==========================================================================
1322  */
1323
1324 static int
1325 spa_name_compare(const void *a1, const void *a2)
1326 {
1327         const spa_t *s1 = a1;
1328         const spa_t *s2 = a2;
1329         int s;
1330
1331         s = strcmp(s1->spa_name, s2->spa_name);
1332         if (s > 0)
1333                 return (1);
1334         if (s < 0)
1335                 return (-1);
1336         return (0);
1337 }
1338
1339 int
1340 spa_busy(void)
1341 {
1342         return (spa_active_count);
1343 }
1344
1345 void
1346 spa_boot_init()
1347 {
1348         spa_config_load();
1349 }
1350
1351 void
1352 spa_init(int mode)
1353 {
1354         mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL);
1355         mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL);
1356         mutex_init(&spa_l2cache_lock, NULL, MUTEX_DEFAULT, NULL);
1357         cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL);
1358
1359         avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t),
1360             offsetof(spa_t, spa_avl));
1361
1362         avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_aux_t),
1363             offsetof(spa_aux_t, aux_avl));
1364
1365         avl_create(&spa_l2cache_avl, spa_l2cache_compare, sizeof (spa_aux_t),
1366             offsetof(spa_aux_t, aux_avl));
1367
1368         spa_mode = mode;
1369
1370         refcount_sysinit();
1371         unique_init();
1372         zio_init();
1373         dmu_init();
1374         zil_init();
1375         vdev_cache_stat_init();
1376         zfs_prop_init();
1377         zpool_prop_init();
1378         spa_config_load();
1379         l2arc_start();
1380 }
1381
1382 void
1383 spa_fini(void)
1384 {
1385         l2arc_stop();
1386
1387         spa_evict_all();
1388
1389         vdev_cache_stat_fini();
1390         zil_fini();
1391         dmu_fini();
1392         zio_fini();
1393         unique_fini();
1394         refcount_fini();
1395
1396         avl_destroy(&spa_namespace_avl);
1397         avl_destroy(&spa_spare_avl);
1398         avl_destroy(&spa_l2cache_avl);
1399
1400         cv_destroy(&spa_namespace_cv);
1401         mutex_destroy(&spa_namespace_lock);
1402         mutex_destroy(&spa_spare_lock);
1403         mutex_destroy(&spa_l2cache_lock);
1404 }
1405
1406 /*
1407  * Return whether this pool has slogs. No locking needed.
1408  * It's not a problem if the wrong answer is returned as it's only for
1409  * performance and not correctness
1410  */
1411 boolean_t
1412 spa_has_slogs(spa_t *spa)
1413 {
1414         return (spa->spa_log_class->mc_rotor != NULL);
1415 }
1416
1417 /*
1418  * Return whether this pool is the root pool.
1419  */
1420 boolean_t
1421 spa_is_root(spa_t *spa)
1422 {
1423         return (spa->spa_is_root);
1424 }