spa_misc.c revision 8f18d1fadf6a0c20fac9ff7259a5368faa3c3bfb
1fa9e406ahrens/*
2fa9e406ahrens * CDDL HEADER START
3fa9e406ahrens *
4fa9e406ahrens * The contents of this file are subject to the terms of the
5ea8dc4beschrock * Common Development and Distribution License (the "License").
6ea8dc4beschrock * You may not use this file except in compliance with the License.
7fa9e406ahrens *
8fa9e406ahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e406ahrens * or http://www.opensolaris.org/os/licensing.
10fa9e406ahrens * See the License for the specific language governing permissions
11fa9e406ahrens * and limitations under the License.
12fa9e406ahrens *
13fa9e406ahrens * When distributing Covered Code, include this CDDL HEADER in each
14fa9e406ahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e406ahrens * If applicable, add the following below this CDDL HEADER, with the
16fa9e406ahrens * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e406ahrens * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e406ahrens *
19fa9e406ahrens * CDDL HEADER END
20fa9e406ahrens */
21fa9e406ahrens/*
2254d692bGeorge Wilson * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23fa9e406ahrens * Use is subject to license terms.
24fa9e406ahrens */
25fa9e406ahrens
26fa9e406ahrens#include <sys/zfs_context.h>
27fa9e406ahrens#include <sys/spa_impl.h>
28fa9e406ahrens#include <sys/zio.h>
29fa9e406ahrens#include <sys/zio_checksum.h>
30fa9e406ahrens#include <sys/zio_compress.h>
31fa9e406ahrens#include <sys/dmu.h>
32fa9e406ahrens#include <sys/dmu_tx.h>
33fa9e406ahrens#include <sys/zap.h>
34fa9e406ahrens#include <sys/zil.h>
35fa9e406ahrens#include <sys/vdev_impl.h>
36fa9e406ahrens#include <sys/metaslab.h>
37fa9e406ahrens#include <sys/uberblock_impl.h>
38fa9e406ahrens#include <sys/txg.h>
39fa9e406ahrens#include <sys/avl.h>
40fa9e406ahrens#include <sys/unique.h>
41fa9e406ahrens#include <sys/dsl_pool.h>
42fa9e406ahrens#include <sys/dsl_dir.h>
43fa9e406ahrens#include <sys/dsl_prop.h>
44fa9e406ahrens#include <sys/fs/zfs.h>
456ce0521perrin#include <sys/metaslab_impl.h>
46e14bb32Jeff Bonwick#include <sys/sunddi.h>
47e14bb32Jeff Bonwick#include <sys/arc.h>
4891ebeefahrens#include "zfs_prop.h"
49fa9e406ahrens
50fa9e406ahrens/*
51fa9e406ahrens * SPA locking
52fa9e406ahrens *
53fa9e406ahrens * There are four basic locks for managing spa_t structures:
54fa9e406ahrens *
55fa9e406ahrens * spa_namespace_lock (global mutex)
56fa9e406ahrens *
5744cd46cbillm *	This lock must be acquired to do any of the following:
58fa9e406ahrens *
5944cd46cbillm *		- Lookup a spa_t by name
6044cd46cbillm *		- Add or remove a spa_t from the namespace
6144cd46cbillm *		- Increase spa_refcount from non-zero
6244cd46cbillm *		- Check if spa_refcount is zero
6344cd46cbillm *		- Rename a spa_t
64ea8dc4beschrock *		- add/remove/attach/detach devices
6544cd46cbillm *		- Held for the duration of create/destroy/import/export
66fa9e406ahrens *
6744cd46cbillm *	It does not need to handle recursion.  A create or destroy may
6844cd46cbillm *	reference objects (files or zvols) in other pools, but by
6944cd46cbillm *	definition they must have an existing reference, and will never need
7044cd46cbillm *	to lookup a spa_t by name.
71fa9e406ahrens *
72fa9e406ahrens * spa_refcount (per-spa refcount_t protected by mutex)
73fa9e406ahrens *
7444cd46cbillm *	This reference count keep track of any active users of the spa_t.  The
7544cd46cbillm *	spa_t cannot be destroyed or freed while this is non-zero.  Internally,
7644cd46cbillm *	the refcount is never really 'zero' - opening a pool implicitly keeps
77088f389ahrens *	some references in the DMU.  Internally we check against spa_minref, but
7844cd46cbillm *	present the image of a zero/non-zero value to consumers.
79fa9e406ahrens *
80e14bb32Jeff Bonwick * spa_config_lock[] (per-spa array of rwlocks)
81fa9e406ahrens *
8291ebeefahrens *	This protects the spa_t from config changes, and must be held in
8391ebeefahrens *	the following circumstances:
84fa9e406ahrens *
8544cd46cbillm *		- RW_READER to perform I/O to the spa
8644cd46cbillm *		- RW_WRITER to change the vdev config
87fa9e406ahrens *
88fa9e406ahrens * The locking order is fairly straightforward:
89fa9e406ahrens *
9044cd46cbillm *		spa_namespace_lock	->	spa_refcount
91fa9e406ahrens *
9244cd46cbillm *	The namespace lock must be acquired to increase the refcount from 0
9344cd46cbillm *	or to check if it is zero.
94fa9e406ahrens *
95e14bb32Jeff Bonwick *		spa_refcount		->	spa_config_lock[]
96fa9e406ahrens *
9744cd46cbillm *	There must be at least one valid reference on the spa_t to acquire
9844cd46cbillm *	the config lock.
99fa9e406ahrens *
100e14bb32Jeff Bonwick *		spa_namespace_lock	->	spa_config_lock[]
101fa9e406ahrens *
10244cd46cbillm *	The namespace lock must always be taken before the config lock.
103fa9e406ahrens *
104fa9e406ahrens *
105e14bb32Jeff Bonwick * The spa_namespace_lock can be acquired directly and is globally visible.
106fa9e406ahrens *
107e14bb32Jeff Bonwick * The namespace is manipulated using the following functions, all of which
108e14bb32Jeff Bonwick * require the spa_namespace_lock to be held.
109fa9e406ahrens *
11044cd46cbillm *	spa_lookup()		Lookup a spa_t by name.
111fa9e406ahrens *
11244cd46cbillm *	spa_add()		Create a new spa_t in the namespace.
113fa9e406ahrens *
11444cd46cbillm *	spa_remove()		Remove a spa_t from the namespace.  This also
11544cd46cbillm *				frees up any memory associated with the spa_t.
116fa9e406ahrens *
11744cd46cbillm *	spa_next()		Returns the next spa_t in the system, or the
11844cd46cbillm *				first if NULL is passed.
119fa9e406ahrens *
12044cd46cbillm *	spa_evict_all()		Shutdown and remove all spa_t structures in
12144cd46cbillm *				the system.
122fa9e406ahrens *
123ea8dc4beschrock *	spa_guid_exists()	Determine whether a pool/device guid exists.
124fa9e406ahrens *
125fa9e406ahrens * The spa_refcount is manipulated using the following functions:
126fa9e406ahrens *
12744cd46cbillm *	spa_open_ref()		Adds a reference to the given spa_t.  Must be
12844cd46cbillm *				called with spa_namespace_lock held if the
12944cd46cbillm *				refcount is currently zero.
130fa9e406ahrens *
13144cd46cbillm *	spa_close()		Remove a reference from the spa_t.  This will
13244cd46cbillm *				not free the spa_t or remove it from the
13344cd46cbillm *				namespace.  No locking is required.
134fa9e406ahrens *
13544cd46cbillm *	spa_refcount_zero()	Returns true if the refcount is currently
13644cd46cbillm *				zero.  Must be called with spa_namespace_lock
13744cd46cbillm *				held.
138fa9e406ahrens *
139e14bb32Jeff Bonwick * The spa_config_lock[] is an array of rwlocks, ordered as follows:
140e14bb32Jeff Bonwick * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV.
141e14bb32Jeff Bonwick * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}().
142e14bb32Jeff Bonwick *
143e14bb32Jeff Bonwick * To read the configuration, it suffices to hold one of these locks as reader.
144e14bb32Jeff Bonwick * To modify the configuration, you must hold all locks as writer.  To modify
145e14bb32Jeff Bonwick * vdev state without altering the vdev tree's topology (e.g. online/offline),
146e14bb32Jeff Bonwick * you must hold SCL_STATE and SCL_ZIO as writer.
147e14bb32Jeff Bonwick *
148e14bb32Jeff Bonwick * We use these distinct config locks to avoid recursive lock entry.
149e14bb32Jeff Bonwick * For example, spa_sync() (which holds SCL_CONFIG as reader) induces
150e14bb32Jeff Bonwick * block allocations (SCL_ALLOC), which may require reading space maps
151e14bb32Jeff Bonwick * from disk (dmu_read() -> zio_read() -> SCL_ZIO).
152e14bb32Jeff Bonwick *
153e14bb32Jeff Bonwick * The spa config locks cannot be normal rwlocks because we need the
154e14bb32Jeff Bonwick * ability to hand off ownership.  For example, SCL_ZIO is acquired
155e14bb32Jeff Bonwick * by the issuing thread and later released by an interrupt thread.
156e14bb32Jeff Bonwick * They do, however, obey the usual write-wanted semantics to prevent
157e14bb32Jeff Bonwick * writer (i.e. system administrator) starvation.
158e14bb32Jeff Bonwick *
159e14bb32Jeff Bonwick * The lock acquisition rules are as follows:
160e14bb32Jeff Bonwick *
161e14bb32Jeff Bonwick * SCL_CONFIG
162e14bb32Jeff Bonwick *	Protects changes to the vdev tree topology, such as vdev
163e14bb32Jeff Bonwick *	add/remove/attach/detach.  Protects the dirty config list
164e14bb32Jeff Bonwick *	(spa_config_dirty_list) and the set of spares and l2arc devices.
165e14bb32Jeff Bonwick *
166e14bb32Jeff Bonwick * SCL_STATE
167e14bb32Jeff Bonwick *	Protects changes to pool state and vdev state, such as vdev
168e14bb32Jeff Bonwick *	online/offline/fault/degrade/clear.  Protects the dirty state list
169e14bb32Jeff Bonwick *	(spa_state_dirty_list) and global pool state (spa_state).
170e14bb32Jeff Bonwick *
171e14bb32Jeff Bonwick * SCL_ALLOC
172e14bb32Jeff Bonwick *	Protects changes to metaslab groups and classes.
173e14bb32Jeff Bonwick *	Held as reader by metaslab_alloc() and metaslab_claim().
174e14bb32Jeff Bonwick *
175e14bb32Jeff Bonwick * SCL_ZIO
176e14bb32Jeff Bonwick *	Held by bp-level zios (those which have no io_vd upon entry)
177e14bb32Jeff Bonwick *	to prevent changes to the vdev tree.  The bp-level zio implicitly
178e14bb32Jeff Bonwick *	protects all of its vdev child zios, which do not hold SCL_ZIO.
179e14bb32Jeff Bonwick *
180e14bb32Jeff Bonwick * SCL_FREE
181e14bb32Jeff Bonwick *	Protects changes to metaslab groups and classes.
182e14bb32Jeff Bonwick *	Held as reader by metaslab_free().  SCL_FREE is distinct from
183e14bb32Jeff Bonwick *	SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free
184e14bb32Jeff Bonwick *	blocks in zio_done() while another i/o that holds either
185e14bb32Jeff Bonwick *	SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete.
186e14bb32Jeff Bonwick *
187e14bb32Jeff Bonwick * SCL_VDEV
188e14bb32Jeff Bonwick *	Held as reader to prevent changes to the vdev tree during trivial
189e14bb32Jeff Bonwick *	inquiries such as bp_get_dasize().  SCL_VDEV is distinct from the
190e14bb32Jeff Bonwick *	other locks, and lower than all of them, to ensure that it's safe
191e14bb32Jeff Bonwick *	to acquire regardless of caller context.
192e14bb32Jeff Bonwick *
193e14bb32Jeff Bonwick * In addition, the following rules apply:
194e14bb32Jeff Bonwick *
195e14bb32Jeff Bonwick * (a)	spa_props_lock protects pool properties, spa_config and spa_config_list.
196e14bb32Jeff Bonwick *	The lock ordering is SCL_CONFIG > spa_props_lock.
197e14bb32Jeff Bonwick *
198e14bb32Jeff Bonwick * (b)	I/O operations on leaf vdevs.  For any zio operation that takes
199e14bb32Jeff Bonwick *	an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(),
200e14bb32Jeff Bonwick *	or zio_write_phys() -- the caller must ensure that the config cannot
201e14bb32Jeff Bonwick *	cannot change in the interim, and that the vdev cannot be reopened.
202e14bb32Jeff Bonwick *	SCL_STATE as reader suffices for both.
203fa9e406ahrens *
204ea8dc4beschrock * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
205fa9e406ahrens *
20644cd46cbillm *	spa_vdev_enter()	Acquire the namespace lock and the config lock
207ea8dc4beschrock *				for writing.
208fa9e406ahrens *
20944cd46cbillm *	spa_vdev_exit()		Release the config lock, wait for all I/O
21044cd46cbillm *				to complete, sync the updated configs to the
211ea8dc4beschrock *				cache, and release the namespace lock.
212fa9e406ahrens *
213e14bb32Jeff Bonwick * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit().
214e14bb32Jeff Bonwick * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual
215e14bb32Jeff Bonwick * locking is, always, based on spa_namespace_lock and spa_config_lock[].
216e14bb32Jeff Bonwick *
217e14bb32Jeff Bonwick * spa_rename() is also implemented within this file since is requires
218e14bb32Jeff Bonwick * manipulation of the namespace.
219fa9e406ahrens */
220fa9e406ahrens
221fa9e406ahrensstatic avl_tree_t spa_namespace_avl;
222fa9e406ahrenskmutex_t spa_namespace_lock;
223fa9e406ahrensstatic kcondvar_t spa_namespace_cv;
2240373e76bonwickstatic int spa_active_count;
225416e0cdekint spa_max_replication_override = SPA_DVAS_PER_BP;
226fa9e406ahrens
22799653d4eschrockstatic kmutex_t spa_spare_lock;
22839c2341eschrockstatic avl_tree_t spa_spare_avl;
229fa94a07brendanstatic kmutex_t spa_l2cache_lock;
230fa94a07brendanstatic avl_tree_t spa_l2cache_avl;
23199653d4eschrock
232fa9e406ahrenskmem_cache_t *spa_buffer_pool;
2338ad4d6dJeff Bonwickint spa_mode_global;
234fa9e406ahrens
235fa9e406ahrens#ifdef ZFS_DEBUG
23640feaa9ahrens/* Everything except dprintf is on by default in debug builds */
23740feaa9ahrensint zfs_flags = ~ZFS_DEBUG_DPRINTF;
238fa9e406ahrens#else
239fa9e406ahrensint zfs_flags = 0;
240fa9e406ahrens#endif
241fa9e406ahrens
2420125049ahrens/*
2430125049ahrens * zfs_recover can be set to nonzero to attempt to recover from
2440125049ahrens * otherwise-fatal errors, typically caused by on-disk corruption.  When
2450125049ahrens * set, calls to zfs_panic_recover() will turn into warning messages.
2460125049ahrens */
2470125049ahrensint zfs_recover = 0;
2480125049ahrens
249fa9e406ahrens
250fa9e406ahrens/*
251fa9e406ahrens * ==========================================================================
252e05725bbonwick * SPA config locking
253e05725bbonwick * ==========================================================================
254e05725bbonwick */
255e05725bbonwickstatic void
256e14bb32Jeff Bonwickspa_config_lock_init(spa_t *spa)
257e14bb32Jeff Bonwick{
258e14bb32Jeff Bonwick	for (int i = 0; i < SCL_LOCKS; i++) {
259e14bb32Jeff Bonwick		spa_config_lock_t *scl = &spa->spa_config_lock[i];
260e14bb32Jeff Bonwick		mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL);
261e14bb32Jeff Bonwick		cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL);
262e14bb32Jeff Bonwick		refcount_create(&scl->scl_count);
263e14bb32Jeff Bonwick		scl->scl_writer = NULL;
264e14bb32Jeff Bonwick		scl->scl_write_wanted = 0;
265e14bb32Jeff Bonwick	}
266e05725bbonwick}
267e05725bbonwick
268e05725bbonwickstatic void
269e14bb32Jeff Bonwickspa_config_lock_destroy(spa_t *spa)
270e14bb32Jeff Bonwick{
271e14bb32Jeff Bonwick	for (int i = 0; i < SCL_LOCKS; i++) {
272e14bb32Jeff Bonwick		spa_config_lock_t *scl = &spa->spa_config_lock[i];
273e14bb32Jeff Bonwick		mutex_destroy(&scl->scl_lock);
274e14bb32Jeff Bonwick		cv_destroy(&scl->scl_cv);
275e14bb32Jeff Bonwick		refcount_destroy(&scl->scl_count);
276e14bb32Jeff Bonwick		ASSERT(scl->scl_writer == NULL);
277e14bb32Jeff Bonwick		ASSERT(scl->scl_write_wanted == 0);
278e14bb32Jeff Bonwick	}
279e14bb32Jeff Bonwick}
280e14bb32Jeff Bonwick
281e14bb32Jeff Bonwickint
282e14bb32Jeff Bonwickspa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw)
283e05725bbonwick{
284e14bb32Jeff Bonwick	for (int i = 0; i < SCL_LOCKS; i++) {
285e14bb32Jeff Bonwick		spa_config_lock_t *scl = &spa->spa_config_lock[i];
286e14bb32Jeff Bonwick		if (!(locks & (1 << i)))
287e14bb32Jeff Bonwick			continue;
288e14bb32Jeff Bonwick		mutex_enter(&scl->scl_lock);
289e14bb32Jeff Bonwick		if (rw == RW_READER) {
290e14bb32Jeff Bonwick			if (scl->scl_writer || scl->scl_write_wanted) {
291e14bb32Jeff Bonwick				mutex_exit(&scl->scl_lock);
292e14bb32Jeff Bonwick				spa_config_exit(spa, locks ^ (1 << i), tag);
293e14bb32Jeff Bonwick				return (0);
294e14bb32Jeff Bonwick			}
295e14bb32Jeff Bonwick		} else {
296e14bb32Jeff Bonwick			ASSERT(scl->scl_writer != curthread);
297e14bb32Jeff Bonwick			if (!refcount_is_zero(&scl->scl_count)) {
298e14bb32Jeff Bonwick				mutex_exit(&scl->scl_lock);
299e14bb32Jeff Bonwick				spa_config_exit(spa, locks ^ (1 << i), tag);
300e14bb32Jeff Bonwick				return (0);
301e14bb32Jeff Bonwick			}
302e14bb32Jeff Bonwick			scl->scl_writer = curthread;
303e14bb32Jeff Bonwick		}
304e14bb32Jeff Bonwick		(void) refcount_add(&scl->scl_count, tag);
305e14bb32Jeff Bonwick		mutex_exit(&scl->scl_lock);
306e14bb32Jeff Bonwick	}
307e14bb32Jeff Bonwick	return (1);
308e05725bbonwick}
309e05725bbonwick
310e05725bbonwickvoid
311e14bb32Jeff Bonwickspa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw)
312e05725bbonwick{
313f64c0e3Eric Taylor	int wlocks_held = 0;
314f64c0e3Eric Taylor
315e14bb32Jeff Bonwick	for (int i = 0; i < SCL_LOCKS; i++) {
316e14bb32Jeff Bonwick		spa_config_lock_t *scl = &spa->spa_config_lock[i];
317f64c0e3Eric Taylor		if (scl->scl_writer == curthread)
318f64c0e3Eric Taylor			wlocks_held |= (1 << i);
319e14bb32Jeff Bonwick		if (!(locks & (1 << i)))
320e14bb32Jeff Bonwick			continue;
321e14bb32Jeff Bonwick		mutex_enter(&scl->scl_lock);
322e14bb32Jeff Bonwick		if (rw == RW_READER) {
323e14bb32Jeff Bonwick			while (scl->scl_writer || scl->scl_write_wanted) {
324e14bb32Jeff Bonwick				cv_wait(&scl->scl_cv, &scl->scl_lock);
325e14bb32Jeff Bonwick			}
326e14bb32Jeff Bonwick		} else {
327e14bb32Jeff Bonwick			ASSERT(scl->scl_writer != curthread);
328e14bb32Jeff Bonwick			while (!refcount_is_zero(&scl->scl_count)) {
329e14bb32Jeff Bonwick				scl->scl_write_wanted++;
330e14bb32Jeff Bonwick				cv_wait(&scl->scl_cv, &scl->scl_lock);
331e14bb32Jeff Bonwick				scl->scl_write_wanted--;
332e14bb32Jeff Bonwick			}
333e14bb32Jeff Bonwick			scl->scl_writer = curthread;
334e14bb32Jeff Bonwick		}
335e14bb32Jeff Bonwick		(void) refcount_add(&scl->scl_count, tag);
336e14bb32Jeff Bonwick		mutex_exit(&scl->scl_lock);
337e05725bbonwick	}
338f64c0e3Eric Taylor	ASSERT(wlocks_held <= locks);
339e05725bbonwick}
340e05725bbonwick
341e05725bbonwickvoid
342e14bb32Jeff Bonwickspa_config_exit(spa_t *spa, int locks, void *tag)
343e05725bbonwick{
344e14bb32Jeff Bonwick	for (int i = SCL_LOCKS - 1; i >= 0; i--) {
345e14bb32Jeff Bonwick		spa_config_lock_t *scl = &spa->spa_config_lock[i];
346e14bb32Jeff Bonwick		if (!(locks & (1 << i)))
347e14bb32Jeff Bonwick			continue;
348e14bb32Jeff Bonwick		mutex_enter(&scl->scl_lock);
349e14bb32Jeff Bonwick		ASSERT(!refcount_is_zero(&scl->scl_count));
350e14bb32Jeff Bonwick		if (refcount_remove(&scl->scl_count, tag) == 0) {
351e14bb32Jeff Bonwick			ASSERT(scl->scl_writer == NULL ||
352e14bb32Jeff Bonwick			    scl->scl_writer == curthread);
353e14bb32Jeff Bonwick			scl->scl_writer = NULL;	/* OK in either case */
354e14bb32Jeff Bonwick			cv_broadcast(&scl->scl_cv);
355e14bb32Jeff Bonwick		}
356e14bb32Jeff Bonwick		mutex_exit(&scl->scl_lock);
357e05725bbonwick	}
358e05725bbonwick}
359e05725bbonwick
360e14bb32Jeff Bonwickint
361e14bb32Jeff Bonwickspa_config_held(spa_t *spa, int locks, krw_t rw)
362e05725bbonwick{
363e14bb32Jeff Bonwick	int locks_held = 0;
364e05725bbonwick
365e14bb32Jeff Bonwick	for (int i = 0; i < SCL_LOCKS; i++) {
366e14bb32Jeff Bonwick		spa_config_lock_t *scl = &spa->spa_config_lock[i];
367e14bb32Jeff Bonwick		if (!(locks & (1 << i)))
368e14bb32Jeff Bonwick			continue;
369e14bb32Jeff Bonwick		if ((rw == RW_READER && !refcount_is_zero(&scl->scl_count)) ||
370e14bb32Jeff Bonwick		    (rw == RW_WRITER && scl->scl_writer == curthread))
371e14bb32Jeff Bonwick			locks_held |= 1 << i;
372e14bb32Jeff Bonwick	}
373e14bb32Jeff Bonwick
374e14bb32Jeff Bonwick	return (locks_held);
375e05725bbonwick}
376e05725bbonwick
377e05725bbonwick/*
378e05725bbonwick * ==========================================================================
379fa9e406ahrens * SPA namespace functions
380fa9e406ahrens * ==========================================================================
381fa9e406ahrens */
382fa9e406ahrens
383fa9e406ahrens/*
384fa9e406ahrens * Lookup the named spa_t in the AVL tree.  The spa_namespace_lock must be held.
385fa9e406ahrens * Returns NULL if no matching spa_t is found.
386fa9e406ahrens */
387fa9e406ahrensspa_t *
388fa9e406ahrensspa_lookup(const char *name)
389fa9e406ahrens{
390e14bb32Jeff Bonwick	static spa_t search;	/* spa_t is large; don't allocate on stack */
391e14bb32Jeff Bonwick	spa_t *spa;
392fa9e406ahrens	avl_index_t where;
39340feaa9ahrens	char c;
39440feaa9ahrens	char *cp;
395fa9e406ahrens
396fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
397fa9e406ahrens
39840feaa9ahrens	/*
39940feaa9ahrens	 * If it's a full dataset name, figure out the pool name and
40040feaa9ahrens	 * just use that.
40140feaa9ahrens	 */
40240feaa9ahrens	cp = strpbrk(name, "/@");
40340feaa9ahrens	if (cp) {
40440feaa9ahrens		c = *cp;
40540feaa9ahrens		*cp = '\0';
40640feaa9ahrens	}
40740feaa9ahrens
408e14bb32Jeff Bonwick	(void) strlcpy(search.spa_name, name, sizeof (search.spa_name));
409fa9e406ahrens	spa = avl_find(&spa_namespace_avl, &search, &where);
410fa9e406ahrens
41140feaa9ahrens	if (cp)
41240feaa9ahrens		*cp = c;
41340feaa9ahrens
414fa9e406ahrens	return (spa);
415fa9e406ahrens}
416fa9e406ahrens
417fa9e406ahrens/*
418fa9e406ahrens * Create an uninitialized spa_t with the given name.  Requires
419fa9e406ahrens * spa_namespace_lock.  The caller must ensure that the spa_t doesn't already
420fa9e406ahrens * exist by calling spa_lookup() first.
421fa9e406ahrens */
422fa9e406ahrensspa_t *
4230373e76bonwickspa_add(const char *name, const char *altroot)
424fa9e406ahrens{
425fa9e406ahrens	spa_t *spa;
426c5904d1eschrock	spa_config_dirent_t *dp;
427fa9e406ahrens
428fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
429fa9e406ahrens
430fa9e406ahrens	spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
431fa9e406ahrens
432c25056dgw	mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL);
433c25056dgw	mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL);
434c25056dgw	mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL);
435c25056dgw	mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL);
436c25056dgw	mutex_init(&spa->spa_sync_bplist.bpl_lock, NULL, MUTEX_DEFAULT, NULL);
437c25056dgw	mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL);
438c25056dgw	mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL);
439c25056dgw
440c25056dgw	cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL);
441c25056dgw	cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL);
442e14bb32Jeff Bonwick	cv_init(&spa->spa_suspend_cv, NULL, CV_DEFAULT, NULL);
443c25056dgw
444e14bb32Jeff Bonwick	(void) strlcpy(spa->spa_name, name, sizeof (spa->spa_name));
445fa9e406ahrens	spa->spa_state = POOL_STATE_UNINITIALIZED;
446fa9e406ahrens	spa->spa_freeze_txg = UINT64_MAX;
4470373e76bonwick	spa->spa_final_txg = UINT64_MAX;
448fa9e406ahrens
449fa9e406ahrens	refcount_create(&spa->spa_refcount);
450e14bb32Jeff Bonwick	spa_config_lock_init(spa);
451fa9e406ahrens
452fa9e406ahrens	avl_add(&spa_namespace_avl, spa);
453fa9e406ahrens
454e14bb32Jeff Bonwick	mutex_init(&spa->spa_suspend_lock, NULL, MUTEX_DEFAULT, NULL);
4550a4e951gw
4560373e76bonwick	/*
4570373e76bonwick	 * Set the alternate root, if there is one.
4580373e76bonwick	 */
4590373e76bonwick	if (altroot) {
4600373e76bonwick		spa->spa_root = spa_strdup(altroot);
4610373e76bonwick		spa_active_count++;
4620373e76bonwick	}
4630373e76bonwick
464c5904d1eschrock	/*
465c5904d1eschrock	 * Every pool starts with the default cachefile
466c5904d1eschrock	 */
467c5904d1eschrock	list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t),
468c5904d1eschrock	    offsetof(spa_config_dirent_t, scd_link));
469c5904d1eschrock
470c5904d1eschrock	dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP);
471c5904d1eschrock	dp->scd_path = spa_strdup(spa_config_path);
472c5904d1eschrock	list_insert_head(&spa->spa_config_list, dp);
473c5904d1eschrock
474fa9e406ahrens	return (spa);
475fa9e406ahrens}
476fa9e406ahrens
477fa9e406ahrens/*
478fa9e406ahrens * Removes a spa_t from the namespace, freeing up any memory used.  Requires
479fa9e406ahrens * spa_namespace_lock.  This is called only after the spa_t has been closed and
480fa9e406ahrens * deactivated.
481fa9e406ahrens */
482fa9e406ahrensvoid
483fa9e406ahrensspa_remove(spa_t *spa)
484fa9e406ahrens{
485c5904d1eschrock	spa_config_dirent_t *dp;
486c5904d1eschrock
487fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
488fa9e406ahrens	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
489fa9e406ahrens
490fa9e406ahrens	avl_remove(&spa_namespace_avl, spa);
491fa9e406ahrens	cv_broadcast(&spa_namespace_cv);
492fa9e406ahrens
4930373e76bonwick	if (spa->spa_root) {
494fa9e406ahrens		spa_strfree(spa->spa_root);
4950373e76bonwick		spa_active_count--;
4960373e76bonwick	}
497fa9e406ahrens
498c5904d1eschrock	while ((dp = list_head(&spa->spa_config_list)) != NULL) {
499c5904d1eschrock		list_remove(&spa->spa_config_list, dp);
500c5904d1eschrock		if (dp->scd_path != NULL)
501c5904d1eschrock			spa_strfree(dp->scd_path);
502c5904d1eschrock		kmem_free(dp, sizeof (spa_config_dirent_t));
503c5904d1eschrock	}
504c5904d1eschrock
505c5904d1eschrock	list_destroy(&spa->spa_config_list);
5062f8aaabeschrock
507fa9e406ahrens	spa_config_set(spa, NULL);
508fa9e406ahrens
509fa9e406ahrens	refcount_destroy(&spa->spa_refcount);
51091ebeefahrens
511e14bb32Jeff Bonwick	spa_config_lock_destroy(spa);
512fa9e406ahrens
513c25056dgw	cv_destroy(&spa->spa_async_cv);
514c25056dgw	cv_destroy(&spa->spa_scrub_io_cv);
515e14bb32Jeff Bonwick	cv_destroy(&spa->spa_suspend_cv);
516c25056dgw
5175ad8204nd	mutex_destroy(&spa->spa_async_lock);
518c25056dgw	mutex_destroy(&spa->spa_scrub_lock);
519c25056dgw	mutex_destroy(&spa->spa_errlog_lock);
520c25056dgw	mutex_destroy(&spa->spa_errlist_lock);
521c25056dgw	mutex_destroy(&spa->spa_sync_bplist.bpl_lock);
52206eeb2aek	mutex_destroy(&spa->spa_history_lock);
523b1b8ab3lling	mutex_destroy(&spa->spa_props_lock);
524e14bb32Jeff Bonwick	mutex_destroy(&spa->spa_suspend_lock);
5255ad8204nd
526fa9e406ahrens	kmem_free(spa, sizeof (spa_t));
527fa9e406ahrens}
528fa9e406ahrens
529fa9e406ahrens/*
530fa9e406ahrens * Given a pool, return the next pool in the namespace, or NULL if there is
531fa9e406ahrens * none.  If 'prev' is NULL, return the first pool.
532fa9e406ahrens */
533fa9e406ahrensspa_t *
534fa9e406ahrensspa_next(spa_t *prev)
535fa9e406ahrens{
536fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
537fa9e406ahrens
538fa9e406ahrens	if (prev)
539fa9e406ahrens		return (AVL_NEXT(&spa_namespace_avl, prev));
540fa9e406ahrens	else
541fa9e406ahrens		return (avl_first(&spa_namespace_avl));
542fa9e406ahrens}
543fa9e406ahrens
544fa9e406ahrens/*
545fa9e406ahrens * ==========================================================================
546fa9e406ahrens * SPA refcount functions
547fa9e406ahrens * ==========================================================================
548fa9e406ahrens */
549fa9e406ahrens
550fa9e406ahrens/*
551fa9e406ahrens * Add a reference to the given spa_t.  Must have at least one reference, or
552fa9e406ahrens * have the namespace lock held.
553fa9e406ahrens */
554fa9e406ahrensvoid
555fa9e406ahrensspa_open_ref(spa_t *spa, void *tag)
556fa9e406ahrens{
557088f389ahrens	ASSERT(refcount_count(&spa->spa_refcount) >= spa->spa_minref ||
558fa9e406ahrens	    MUTEX_HELD(&spa_namespace_lock));
559fa9e406ahrens	(void) refcount_add(&spa->spa_refcount, tag);
560fa9e406ahrens}
561fa9e406ahrens
562fa9e406ahrens/*
563fa9e406ahrens * Remove a reference to the given spa_t.  Must have at least one reference, or
564fa9e406ahrens * have the namespace lock held.
565fa9e406ahrens */
566fa9e406ahrensvoid
567fa9e406ahrensspa_close(spa_t *spa, void *tag)
568fa9e406ahrens{
569088f389ahrens	ASSERT(refcount_count(&spa->spa_refcount) > spa->spa_minref ||
570fa9e406ahrens	    MUTEX_HELD(&spa_namespace_lock));
571fa9e406ahrens	(void) refcount_remove(&spa->spa_refcount, tag);
572fa9e406ahrens}
573fa9e406ahrens
574fa9e406ahrens/*
575fa9e406ahrens * Check to see if the spa refcount is zero.  Must be called with
576088f389ahrens * spa_namespace_lock held.  We really compare against spa_minref, which is the
577fa9e406ahrens * number of references acquired when opening a pool
578fa9e406ahrens */
579fa9e406ahrensboolean_t
580fa9e406ahrensspa_refcount_zero(spa_t *spa)
581fa9e406ahrens{
582fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
583fa9e406ahrens
584088f389ahrens	return (refcount_count(&spa->spa_refcount) == spa->spa_minref);
585fa9e406ahrens}
586fa9e406ahrens
587fa9e406ahrens/*
588fa9e406ahrens * ==========================================================================
589fa94a07brendan * SPA spare and l2cache tracking
59099653d4eschrock * ==========================================================================
59199653d4eschrock */
59299653d4eschrock
59399653d4eschrock/*
594fa94a07brendan * Hot spares and cache devices are tracked using the same code below,
595fa94a07brendan * for 'auxiliary' devices.
596fa94a07brendan */
597fa94a07brendan
598fa94a07brendantypedef struct spa_aux {
599fa94a07brendan	uint64_t	aux_guid;
600fa94a07brendan	uint64_t	aux_pool;
601fa94a07brendan	avl_node_t	aux_avl;
602fa94a07brendan	int		aux_count;
603fa94a07brendan} spa_aux_t;
604fa94a07brendan
605fa94a07brendanstatic int
606fa94a07brendanspa_aux_compare(const void *a, const void *b)
607fa94a07brendan{
608fa94a07brendan	const spa_aux_t *sa = a;
609fa94a07brendan	const spa_aux_t *sb = b;
610fa94a07brendan
611fa94a07brendan	if (sa->aux_guid < sb->aux_guid)
612fa94a07brendan		return (-1);
613fa94a07brendan	else if (sa->aux_guid > sb->aux_guid)
614fa94a07brendan		return (1);
615fa94a07brendan	else
616fa94a07brendan		return (0);
617fa94a07brendan}
618fa94a07brendan
619fa94a07brendanvoid
620fa94a07brendanspa_aux_add(vdev_t *vd, avl_tree_t *avl)
621fa94a07brendan{
622fa94a07brendan	avl_index_t where;
623fa94a07brendan	spa_aux_t search;
624fa94a07brendan	spa_aux_t *aux;
625fa94a07brendan
626fa94a07brendan	search.aux_guid = vd->vdev_guid;
627fa94a07brendan	if ((aux = avl_find(avl, &search, &where)) != NULL) {
628fa94a07brendan		aux->aux_count++;
629fa94a07brendan	} else {
630fa94a07brendan		aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP);
631fa94a07brendan		aux->aux_guid = vd->vdev_guid;
632fa94a07brendan		aux->aux_count = 1;
633fa94a07brendan		avl_insert(avl, aux, where);
634fa94a07brendan	}
635fa94a07brendan}
636fa94a07brendan
637fa94a07brendanvoid
638fa94a07brendanspa_aux_remove(vdev_t *vd, avl_tree_t *avl)
639fa94a07brendan{
640fa94a07brendan	spa_aux_t search;
641fa94a07brendan	spa_aux_t *aux;
642fa94a07brendan	avl_index_t where;
643fa94a07brendan
644fa94a07brendan	search.aux_guid = vd->vdev_guid;
645fa94a07brendan	aux = avl_find(avl, &search, &where);
646fa94a07brendan
647fa94a07brendan	ASSERT(aux != NULL);
648fa94a07brendan
649fa94a07brendan	if (--aux->aux_count == 0) {
650fa94a07brendan		avl_remove(avl, aux);
651fa94a07brendan		kmem_free(aux, sizeof (spa_aux_t));
652fa94a07brendan	} else if (aux->aux_pool == spa_guid(vd->vdev_spa)) {
653fa94a07brendan		aux->aux_pool = 0ULL;
654fa94a07brendan	}
655fa94a07brendan}
656fa94a07brendan
657fa94a07brendanboolean_t
65889a89ebllingspa_aux_exists(uint64_t guid, uint64_t *pool, int *refcnt, avl_tree_t *avl)
659fa94a07brendan{
660fa94a07brendan	spa_aux_t search, *found;
661fa94a07brendan
662fa94a07brendan	search.aux_guid = guid;
66389a89eblling	found = avl_find(avl, &search, NULL);
664fa94a07brendan
665fa94a07brendan	if (pool) {
666fa94a07brendan		if (found)
667fa94a07brendan			*pool = found->aux_pool;
668fa94a07brendan		else
669fa94a07brendan			*pool = 0ULL;
670fa94a07brendan	}
671fa94a07brendan
67289a89eblling	if (refcnt) {
67389a89eblling		if (found)
67489a89eblling			*refcnt = found->aux_count;
67589a89eblling		else
67689a89eblling			*refcnt = 0;
67789a89eblling	}
67889a89eblling
679fa94a07brendan	return (found != NULL);
680fa94a07brendan}
681fa94a07brendan
682fa94a07brendanvoid
683fa94a07brendanspa_aux_activate(vdev_t *vd, avl_tree_t *avl)
684fa94a07brendan{
685fa94a07brendan	spa_aux_t search, *found;
686fa94a07brendan	avl_index_t where;
687fa94a07brendan
688fa94a07brendan	search.aux_guid = vd->vdev_guid;
689fa94a07brendan	found = avl_find(avl, &search, &where);
690fa94a07brendan	ASSERT(found != NULL);
691fa94a07brendan	ASSERT(found->aux_pool == 0ULL);
692fa94a07brendan
693fa94a07brendan	found->aux_pool = spa_guid(vd->vdev_spa);
694fa94a07brendan}
695fa94a07brendan
696fa94a07brendan/*
69739c2341eschrock * Spares are tracked globally due to the following constraints:
69839c2341eschrock *
69939c2341eschrock * 	- A spare may be part of multiple pools.
70039c2341eschrock * 	- A spare may be added to a pool even if it's actively in use within
70139c2341eschrock *	  another pool.
70239c2341eschrock * 	- A spare in use in any pool can only be the source of a replacement if
70339c2341eschrock *	  the target is a spare in the same pool.
70439c2341eschrock *
70539c2341eschrock * We keep track of all spares on the system through the use of a reference
70639c2341eschrock * counted AVL tree.  When a vdev is added as a spare, or used as a replacement
70739c2341eschrock * spare, then we bump the reference count in the AVL tree.  In addition, we set
70839c2341eschrock * the 'vdev_isspare' member to indicate that the device is a spare (active or
70939c2341eschrock * inactive).  When a spare is made active (used to replace a device in the
71039c2341eschrock * pool), we also keep track of which pool its been made a part of.
71139c2341eschrock *
71239c2341eschrock * The 'spa_spare_lock' protects the AVL tree.  These functions are normally
71339c2341eschrock * called under the spa_namespace lock as part of vdev reconfiguration.  The
71439c2341eschrock * separate spare lock exists for the status query path, which does not need to
71539c2341eschrock * be completely consistent with respect to other vdev configuration changes.
71699653d4eschrock */
71739c2341eschrock
71899653d4eschrockstatic int
71999653d4eschrockspa_spare_compare(const void *a, const void *b)
72099653d4eschrock{
721fa94a07brendan	return (spa_aux_compare(a, b));
72299653d4eschrock}
72399653d4eschrock
72499653d4eschrockvoid
72539c2341eschrockspa_spare_add(vdev_t *vd)
72699653d4eschrock{
72799653d4eschrock	mutex_enter(&spa_spare_lock);
72839c2341eschrock	ASSERT(!vd->vdev_isspare);
729fa94a07brendan	spa_aux_add(vd, &spa_spare_avl);
73039c2341eschrock	vd->vdev_isspare = B_TRUE;
73199653d4eschrock	mutex_exit(&spa_spare_lock);
73299653d4eschrock}
73399653d4eschrock
73499653d4eschrockvoid
73539c2341eschrockspa_spare_remove(vdev_t *vd)
73699653d4eschrock{
73799653d4eschrock	mutex_enter(&spa_spare_lock);
73839c2341eschrock	ASSERT(vd->vdev_isspare);
739fa94a07brendan	spa_aux_remove(vd, &spa_spare_avl);
74039c2341eschrock	vd->vdev_isspare = B_FALSE;
74199653d4eschrock	mutex_exit(&spa_spare_lock);
74299653d4eschrock}
74399653d4eschrock
74499653d4eschrockboolean_t
74589a89ebllingspa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt)
74699653d4eschrock{
747fa94a07brendan	boolean_t found;
74899653d4eschrock
74999653d4eschrock	mutex_enter(&spa_spare_lock);
75089a89eblling	found = spa_aux_exists(guid, pool, refcnt, &spa_spare_avl);
75199653d4eschrock	mutex_exit(&spa_spare_lock);
75299653d4eschrock
753fa94a07brendan	return (found);
75439c2341eschrock}
75539c2341eschrock
75639c2341eschrockvoid
75739c2341eschrockspa_spare_activate(vdev_t *vd)
75839c2341eschrock{
75939c2341eschrock	mutex_enter(&spa_spare_lock);
76039c2341eschrock	ASSERT(vd->vdev_isspare);
761fa94a07brendan	spa_aux_activate(vd, &spa_spare_avl);
762fa94a07brendan	mutex_exit(&spa_spare_lock);
763fa94a07brendan}
76439c2341eschrock
765fa94a07brendan/*
766fa94a07brendan * Level 2 ARC devices are tracked globally for the same reasons as spares.
767fa94a07brendan * Cache devices currently only support one pool per cache device, and so
768fa94a07brendan * for these devices the aux reference count is currently unused beyond 1.
769fa94a07brendan */
77039c2341eschrock
771fa94a07brendanstatic int
772fa94a07brendanspa_l2cache_compare(const void *a, const void *b)
773fa94a07brendan{
774fa94a07brendan	return (spa_aux_compare(a, b));
775fa94a07brendan}
776fa94a07brendan
777fa94a07brendanvoid
778fa94a07brendanspa_l2cache_add(vdev_t *vd)
779fa94a07brendan{
780fa94a07brendan	mutex_enter(&spa_l2cache_lock);
781fa94a07brendan	ASSERT(!vd->vdev_isl2cache);
782fa94a07brendan	spa_aux_add(vd, &spa_l2cache_avl);
783fa94a07brendan	vd->vdev_isl2cache = B_TRUE;
784fa94a07brendan	mutex_exit(&spa_l2cache_lock);
785fa94a07brendan}
786fa94a07brendan
787fa94a07brendanvoid
788fa94a07brendanspa_l2cache_remove(vdev_t *vd)
789fa94a07brendan{
790fa94a07brendan	mutex_enter(&spa_l2cache_lock);
791fa94a07brendan	ASSERT(vd->vdev_isl2cache);
792fa94a07brendan	spa_aux_remove(vd, &spa_l2cache_avl);
793fa94a07brendan	vd->vdev_isl2cache = B_FALSE;
794fa94a07brendan	mutex_exit(&spa_l2cache_lock);
795fa94a07brendan}
796fa94a07brendan
797fa94a07brendanboolean_t
798fa94a07brendanspa_l2cache_exists(uint64_t guid, uint64_t *pool)
799fa94a07brendan{
800fa94a07brendan	boolean_t found;
801fa94a07brendan
802fa94a07brendan	mutex_enter(&spa_l2cache_lock);
80389a89eblling	found = spa_aux_exists(guid, pool, NULL, &spa_l2cache_avl);
804fa94a07brendan	mutex_exit(&spa_l2cache_lock);
805fa94a07brendan
806fa94a07brendan	return (found);
807fa94a07brendan}
808fa94a07brendan
809fa94a07brendanvoid
810fa94a07brendanspa_l2cache_activate(vdev_t *vd)
811fa94a07brendan{
812fa94a07brendan	mutex_enter(&spa_l2cache_lock);
813fa94a07brendan	ASSERT(vd->vdev_isl2cache);
814fa94a07brendan	spa_aux_activate(vd, &spa_l2cache_avl);
815fa94a07brendan	mutex_exit(&spa_l2cache_lock);
816fa94a07brendan}
817fa94a07brendan
818fa94a07brendanvoid
819fa94a07brendanspa_l2cache_space_update(vdev_t *vd, int64_t space, int64_t alloc)
820fa94a07brendan{
821fa94a07brendan	vdev_space_update(vd, space, alloc, B_FALSE);
82299653d4eschrock}
82399653d4eschrock
82499653d4eschrock/*
82599653d4eschrock * ==========================================================================
826fa9e406ahrens * SPA vdev locking
827fa9e406ahrens * ==========================================================================
828fa9e406ahrens */
829fa9e406ahrens
830fa9e406ahrens/*
831ea8dc4beschrock * Lock the given spa_t for the purpose of adding or removing a vdev.
832ea8dc4beschrock * Grabs the global spa_namespace_lock plus the spa config lock for writing.
833fa9e406ahrens * It returns the next transaction group for the spa_t.
834fa9e406ahrens */
835fa9e406ahrensuint64_t
836fa9e406ahrensspa_vdev_enter(spa_t *spa)
837fa9e406ahrens{
8383d7072feschrock	mutex_enter(&spa_namespace_lock);
83988ecc94George Wilson	return (spa_vdev_config_enter(spa));
84088ecc94George Wilson}
84188ecc94George Wilson
84288ecc94George Wilson/*
84388ecc94George Wilson * Internal implementation for spa_vdev_enter().  Used when a vdev
84488ecc94George Wilson * operation requires multiple syncs (i.e. removing a device) while
84588ecc94George Wilson * keeping the spa_namespace_lock held.
84688ecc94George Wilson */
84788ecc94George Wilsonuint64_t
84888ecc94George Wilsonspa_vdev_config_enter(spa_t *spa)
84988ecc94George Wilson{
85088ecc94George Wilson	ASSERT(MUTEX_HELD(&spa_namespace_lock));
8513d7072feschrock
852e14bb32Jeff Bonwick	spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
853fa9e406ahrens
854fa9e406ahrens	return (spa_last_synced_txg(spa) + 1);
855fa9e406ahrens}
856fa9e406ahrens
857fa9e406ahrens/*
85888ecc94George Wilson * Used in combination with spa_vdev_config_enter() to allow the syncing
85988ecc94George Wilson * of multiple transactions without releasing the spa_namespace_lock.
860fa9e406ahrens */
86188ecc94George Wilsonvoid
86288ecc94George Wilsonspa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error, char *tag)
863fa9e406ahrens{
86488ecc94George Wilson	ASSERT(MUTEX_HELD(&spa_namespace_lock));
86588ecc94George Wilson
8660e34b6abonwick	int config_changed = B_FALSE;
867ea8dc4beschrock
8680373e76bonwick	ASSERT(txg > spa_last_synced_txg(spa));
8690e34b6abonwick
870e14bb32Jeff Bonwick	spa->spa_pending_vdev = NULL;
871e14bb32Jeff Bonwick
8720e34b6abonwick	/*
8730e34b6abonwick	 * Reassess the DTLs.
8740e34b6abonwick	 */
8750373e76bonwick	vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE);
8760e34b6abonwick
8770e34b6abonwick	/*
8780373e76bonwick	 * If the config changed, notify the scrub thread that it must restart.
8790e34b6abonwick	 */
880e14bb32Jeff Bonwick	if (error == 0 && !list_is_empty(&spa->spa_config_dirty_list)) {
881088f389ahrens		dsl_pool_scrub_restart(spa->spa_dsl_pool);
8820e34b6abonwick		config_changed = B_TRUE;
8838f18d1fGeorge Wilson		spa->spa_config_generation++;
8840e34b6abonwick	}
885ea8dc4beschrock
88688ecc94George Wilson	/*
88788ecc94George Wilson	 * Verify the metaslab classes.
88888ecc94George Wilson	 */
88988ecc94George Wilson	ASSERT(metaslab_class_validate(spa->spa_normal_class) == 0);
89088ecc94George Wilson	ASSERT(metaslab_class_validate(spa->spa_log_class) == 0);
89188ecc94George Wilson
892e14bb32Jeff Bonwick	spa_config_exit(spa, SCL_ALL, spa);
893fa9e406ahrens
894ea8dc4beschrock	/*
89588ecc94George Wilson	 * Panic the system if the specified tag requires it.  This
89688ecc94George Wilson	 * is useful for ensuring that configurations are updated
89788ecc94George Wilson	 * transactionally.
89888ecc94George Wilson	 */
89988ecc94George Wilson	if (zio_injection_enabled)
90088ecc94George Wilson		zio_handle_panic_injection(spa, tag);
90188ecc94George Wilson
90288ecc94George Wilson	/*
903