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