spa_misc.c revision 485bbbf5450c6645352388d798251c1a89ef4c9c
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>
48485bbbfGeorge Wilson#include <sys/ddt.h>
4991ebeefahrens#include "zfs_prop.h"
50fa9e406ahrens
51fa9e406ahrens/*
52fa9e406ahrens * SPA locking
53fa9e406ahrens *
54fa9e406ahrens * There are four basic locks for managing spa_t structures:
55fa9e406ahrens *
56fa9e406ahrens * spa_namespace_lock (global mutex)
57fa9e406ahrens *
5844cd46cbillm *	This lock must be acquired to do any of the following:
59fa9e406ahrens *
6044cd46cbillm *		- Lookup a spa_t by name
6144cd46cbillm *		- Add or remove a spa_t from the namespace
6244cd46cbillm *		- Increase spa_refcount from non-zero
6344cd46cbillm *		- Check if spa_refcount is zero
6444cd46cbillm *		- Rename a spa_t
65ea8dc4beschrock *		- add/remove/attach/detach devices
6644cd46cbillm *		- Held for the duration of create/destroy/import/export
67fa9e406ahrens *
6844cd46cbillm *	It does not need to handle recursion.  A create or destroy may
6944cd46cbillm *	reference objects (files or zvols) in other pools, but by
7044cd46cbillm *	definition they must have an existing reference, and will never need
7144cd46cbillm *	to lookup a spa_t by name.
72fa9e406ahrens *
73fa9e406ahrens * spa_refcount (per-spa refcount_t protected by mutex)
74fa9e406ahrens *
7544cd46cbillm *	This reference count keep track of any active users of the spa_t.  The
7644cd46cbillm *	spa_t cannot be destroyed or freed while this is non-zero.  Internally,
7744cd46cbillm *	the refcount is never really 'zero' - opening a pool implicitly keeps
78088f389ahrens *	some references in the DMU.  Internally we check against spa_minref, but
7944cd46cbillm *	present the image of a zero/non-zero value to consumers.
80fa9e406ahrens *
81e14bb32Jeff Bonwick * spa_config_lock[] (per-spa array of rwlocks)
82fa9e406ahrens *
8391ebeefahrens *	This protects the spa_t from config changes, and must be held in
8491ebeefahrens *	the following circumstances:
85fa9e406ahrens *
8644cd46cbillm *		- RW_READER to perform I/O to the spa
8744cd46cbillm *		- RW_WRITER to change the vdev config
88fa9e406ahrens *
89fa9e406ahrens * The locking order is fairly straightforward:
90fa9e406ahrens *
9144cd46cbillm *		spa_namespace_lock	->	spa_refcount
92fa9e406ahrens *
9344cd46cbillm *	The namespace lock must be acquired to increase the refcount from 0
9444cd46cbillm *	or to check if it is zero.
95fa9e406ahrens *
96e14bb32Jeff Bonwick *		spa_refcount		->	spa_config_lock[]
97fa9e406ahrens *
9844cd46cbillm *	There must be at least one valid reference on the spa_t to acquire
9944cd46cbillm *	the config lock.
100fa9e406ahrens *
101e14bb32Jeff Bonwick *		spa_namespace_lock	->	spa_config_lock[]
102fa9e406ahrens *
10344cd46cbillm *	The namespace lock must always be taken before the config lock.
104fa9e406ahrens *
105fa9e406ahrens *
106e14bb32Jeff Bonwick * The spa_namespace_lock can be acquired directly and is globally visible.
107fa9e406ahrens *
108e14bb32Jeff Bonwick * The namespace is manipulated using the following functions, all of which
109e14bb32Jeff Bonwick * require the spa_namespace_lock to be held.
110fa9e406ahrens *
11144cd46cbillm *	spa_lookup()		Lookup a spa_t by name.
112fa9e406ahrens *
11344cd46cbillm *	spa_add()		Create a new spa_t in the namespace.
114fa9e406ahrens *
11544cd46cbillm *	spa_remove()		Remove a spa_t from the namespace.  This also
11644cd46cbillm *				frees up any memory associated with the spa_t.
117fa9e406ahrens *
11844cd46cbillm *	spa_next()		Returns the next spa_t in the system, or the
11944cd46cbillm *				first if NULL is passed.
120fa9e406ahrens *
12144cd46cbillm *	spa_evict_all()		Shutdown and remove all spa_t structures in
12244cd46cbillm *				the system.
123fa9e406ahrens *
124ea8dc4beschrock *	spa_guid_exists()	Determine whether a pool/device guid exists.
125fa9e406ahrens *
126fa9e406ahrens * The spa_refcount is manipulated using the following functions:
127fa9e406ahrens *
12844cd46cbillm *	spa_open_ref()		Adds a reference to the given spa_t.  Must be
12944cd46cbillm *				called with spa_namespace_lock held if the
13044cd46cbillm *				refcount is currently zero.
131fa9e406ahrens *
13244cd46cbillm *	spa_close()		Remove a reference from the spa_t.  This will
13344cd46cbillm *				not free the spa_t or remove it from the
13444cd46cbillm *				namespace.  No locking is required.
135fa9e406ahrens *
13644cd46cbillm *	spa_refcount_zero()	Returns true if the refcount is currently
13744cd46cbillm *				zero.  Must be called with spa_namespace_lock
13844cd46cbillm *				held.
139fa9e406ahrens *
140e14bb32Jeff Bonwick * The spa_config_lock[] is an array of rwlocks, ordered as follows:
141e14bb32Jeff Bonwick * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV.
142e14bb32Jeff Bonwick * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}().
143e14bb32Jeff Bonwick *
144e14bb32Jeff Bonwick * To read the configuration, it suffices to hold one of these locks as reader.
145e14bb32Jeff Bonwick * To modify the configuration, you must hold all locks as writer.  To modify
146e14bb32Jeff Bonwick * vdev state without altering the vdev tree's topology (e.g. online/offline),
147e14bb32Jeff Bonwick * you must hold SCL_STATE and SCL_ZIO as writer.
148e14bb32Jeff Bonwick *
149e14bb32Jeff Bonwick * We use these distinct config locks to avoid recursive lock entry.
150e14bb32Jeff Bonwick * For example, spa_sync() (which holds SCL_CONFIG as reader) induces
151e14bb32Jeff Bonwick * block allocations (SCL_ALLOC), which may require reading space maps
152e14bb32Jeff Bonwick * from disk (dmu_read() -> zio_read() -> SCL_ZIO).
153e14bb32Jeff Bonwick *
154e14bb32Jeff Bonwick * The spa config locks cannot be normal rwlocks because we need the
155e14bb32Jeff Bonwick * ability to hand off ownership.  For example, SCL_ZIO is acquired
156e14bb32Jeff Bonwick * by the issuing thread and later released by an interrupt thread.
157e14bb32Jeff Bonwick * They do, however, obey the usual write-wanted semantics to prevent
158e14bb32Jeff Bonwick * writer (i.e. system administrator) starvation.
159e14bb32Jeff Bonwick *
160e14bb32Jeff Bonwick * The lock acquisition rules are as follows:
161e14bb32Jeff Bonwick *
162e14bb32Jeff Bonwick * SCL_CONFIG
163e14bb32Jeff Bonwick *	Protects changes to the vdev tree topology, such as vdev
164e14bb32Jeff Bonwick *	add/remove/attach/detach.  Protects the dirty config list
165e14bb32Jeff Bonwick *	(spa_config_dirty_list) and the set of spares and l2arc devices.
166e14bb32Jeff Bonwick *
167e14bb32Jeff Bonwick * SCL_STATE
168e14bb32Jeff Bonwick *	Protects changes to pool state and vdev state, such as vdev
169e14bb32Jeff Bonwick *	online/offline/fault/degrade/clear.  Protects the dirty state list
170e14bb32Jeff Bonwick *	(spa_state_dirty_list) and global pool state (spa_state).
171e14bb32Jeff Bonwick *
172e14bb32Jeff Bonwick * SCL_ALLOC
173e14bb32Jeff Bonwick *	Protects changes to metaslab groups and classes.
174e14bb32Jeff Bonwick *	Held as reader by metaslab_alloc() and metaslab_claim().
175e14bb32Jeff Bonwick *
176e14bb32Jeff Bonwick * SCL_ZIO
177e14bb32Jeff Bonwick *	Held by bp-level zios (those which have no io_vd upon entry)
178e14bb32Jeff Bonwick *	to prevent changes to the vdev tree.  The bp-level zio implicitly
179e14bb32Jeff Bonwick *	protects all of its vdev child zios, which do not hold SCL_ZIO.
180e14bb32Jeff Bonwick *
181e14bb32Jeff Bonwick * SCL_FREE
182e14bb32Jeff Bonwick *	Protects changes to metaslab groups and classes.
183e14bb32Jeff Bonwick *	Held as reader by metaslab_free().  SCL_FREE is distinct from
184e14bb32Jeff Bonwick *	SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free
185e14bb32Jeff Bonwick *	blocks in zio_done() while another i/o that holds either
186e14bb32Jeff Bonwick *	SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete.
187e14bb32Jeff Bonwick *
188e14bb32Jeff Bonwick * SCL_VDEV
189e14bb32Jeff Bonwick *	Held as reader to prevent changes to the vdev tree during trivial
190b24ab67Jeff Bonwick *	inquiries such as bp_get_dsize().  SCL_VDEV is distinct from the
191e14bb32Jeff Bonwick *	other locks, and lower than all of them, to ensure that it's safe
192e14bb32Jeff Bonwick *	to acquire regardless of caller context.
193e14bb32Jeff Bonwick *
194e14bb32Jeff Bonwick * In addition, the following rules apply:
195e14bb32Jeff Bonwick *
196e14bb32Jeff Bonwick * (a)	spa_props_lock protects pool properties, spa_config and spa_config_list.
197e14bb32Jeff Bonwick *	The lock ordering is SCL_CONFIG > spa_props_lock.
198e14bb32Jeff Bonwick *
199e14bb32Jeff Bonwick * (b)	I/O operations on leaf vdevs.  For any zio operation that takes
200e14bb32Jeff Bonwick *	an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(),
201e14bb32Jeff Bonwick *	or zio_write_phys() -- the caller must ensure that the config cannot
202e14bb32Jeff Bonwick *	cannot change in the interim, and that the vdev cannot be reopened.
203e14bb32Jeff Bonwick *	SCL_STATE as reader suffices for both.
204fa9e406ahrens *
205ea8dc4beschrock * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
206fa9e406ahrens *
20744cd46cbillm *	spa_vdev_enter()	Acquire the namespace lock and the config lock
208ea8dc4beschrock *				for writing.
209fa9e406ahrens *
21044cd46cbillm *	spa_vdev_exit()		Release the config lock, wait for all I/O
21144cd46cbillm *				to complete, sync the updated configs to the
212ea8dc4beschrock *				cache, and release the namespace lock.
213fa9e406ahrens *
214e14bb32Jeff Bonwick * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit().
215e14bb32Jeff Bonwick * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual
216e14bb32Jeff Bonwick * locking is, always, based on spa_namespace_lock and spa_config_lock[].
217e14bb32Jeff Bonwick *
218e14bb32Jeff Bonwick * spa_rename() is also implemented within this file since is requires
219e14bb32Jeff Bonwick * manipulation of the namespace.
220fa9e406ahrens */
221fa9e406ahrens
222fa9e406ahrensstatic avl_tree_t spa_namespace_avl;
223fa9e406ahrenskmutex_t spa_namespace_lock;
224fa9e406ahrensstatic kcondvar_t spa_namespace_cv;
2250373e76bonwickstatic int spa_active_count;
226416e0cdekint spa_max_replication_override = SPA_DVAS_PER_BP;
227fa9e406ahrens
22899653d4eschrockstatic kmutex_t spa_spare_lock;
22939c2341eschrockstatic avl_tree_t spa_spare_avl;
230fa94a07brendanstatic kmutex_t spa_l2cache_lock;
231fa94a07brendanstatic avl_tree_t spa_l2cache_avl;
23299653d4eschrock
233fa9e406ahrenskmem_cache_t *spa_buffer_pool;
2348ad4d6dJeff Bonwickint spa_mode_global;
235fa9e406ahrens
236fa9e406ahrens#ifdef ZFS_DEBUG
23740feaa9ahrens/* Everything except dprintf is on by default in debug builds */
23840feaa9ahrensint zfs_flags = ~ZFS_DEBUG_DPRINTF;
239fa9e406ahrens#else
240fa9e406ahrensint zfs_flags = 0;
241fa9e406ahrens#endif
242fa9e406ahrens
2430125049ahrens/*
2440125049ahrens * zfs_recover can be set to nonzero to attempt to recover from
2450125049ahrens * otherwise-fatal errors, typically caused by on-disk corruption.  When
2460125049ahrens * set, calls to zfs_panic_recover() will turn into warning messages.
2470125049ahrens */
2480125049ahrensint zfs_recover = 0;
2490125049ahrens
250fa9e406ahrens
251fa9e406ahrens/*
252fa9e406ahrens * ==========================================================================
253e05725bbonwick * SPA config locking
254e05725bbonwick * ==========================================================================
255e05725bbonwick */
256e05725bbonwickstatic void
257e14bb32Jeff Bonwickspa_config_lock_init(spa_t *spa)
258e14bb32Jeff Bonwick{
259e14bb32Jeff Bonwick	for (int i = 0; i < SCL_LOCKS; i++) {
260e14bb32Jeff Bonwick		spa_config_lock_t *scl = &spa->spa_config_lock[i];
261e14bb32Jeff Bonwick		mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL);
262e14bb32Jeff Bonwick		cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL);
263e14bb32Jeff Bonwick		refcount_create(&scl->scl_count);
264e14bb32Jeff Bonwick		scl->scl_writer = NULL;
265e14bb32Jeff Bonwick		scl->scl_write_wanted = 0;
266e14bb32Jeff Bonwick	}
267e05725bbonwick}
268e05725bbonwick
269e05725bbonwickstatic void
270e14bb32Jeff Bonwickspa_config_lock_destroy(spa_t *spa)
271e14bb32Jeff Bonwick{
272e14bb32Jeff Bonwick	for (int i = 0; i < SCL_LOCKS; i++) {
273e14bb32Jeff Bonwick		spa_config_lock_t *scl = &spa->spa_config_lock[i];
274e14bb32Jeff Bonwick		mutex_destroy(&scl->scl_lock);
275e14bb32Jeff Bonwick		cv_destroy(&scl->scl_cv);
276e14bb32Jeff Bonwick		refcount_destroy(&scl->scl_count);
277e14bb32Jeff Bonwick		ASSERT(scl->scl_writer == NULL);
278e14bb32Jeff Bonwick		ASSERT(scl->scl_write_wanted == 0);
279e14bb32Jeff Bonwick	}
280e14bb32Jeff Bonwick}
281e14bb32Jeff Bonwick
282e14bb32Jeff Bonwickint
283e14bb32Jeff Bonwickspa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw)
284e05725bbonwick{
285e14bb32Jeff Bonwick	for (int i = 0; i < SCL_LOCKS; i++) {
286e14bb32Jeff Bonwick		spa_config_lock_t *scl = &spa->spa_config_lock[i];
287e14bb32Jeff Bonwick		if (!(locks & (1 << i)))
288e14bb32Jeff Bonwick			continue;
289e14bb32Jeff Bonwick		mutex_enter(&scl->scl_lock);
290e14bb32Jeff Bonwick		if (rw == RW_READER) {
291e14bb32Jeff Bonwick			if (scl->scl_writer || scl->scl_write_wanted) {
292e14bb32Jeff Bonwick				mutex_exit(&scl->scl_lock);
293e14bb32Jeff Bonwick				spa_config_exit(spa, locks ^ (1 << i), tag);
294e14bb32Jeff Bonwick				return (0);
295e14bb32Jeff Bonwick			}
296e14bb32Jeff Bonwick		} else {
297e14bb32Jeff Bonwick			ASSERT(scl->scl_writer != curthread);
298e14bb32Jeff Bonwick			if (!refcount_is_zero(&scl->scl_count)) {
299e14bb32Jeff Bonwick				mutex_exit(&scl->scl_lock);
300e14bb32Jeff Bonwick				spa_config_exit(spa, locks ^ (1 << i), tag);
301e14bb32Jeff Bonwick				return (0);
302e14bb32Jeff Bonwick			}
303e14bb32Jeff Bonwick			scl->scl_writer = curthread;
304e14bb32Jeff Bonwick		}
305e14bb32Jeff Bonwick		(void) refcount_add(&scl->scl_count, tag);
306e14bb32Jeff Bonwick		mutex_exit(&scl->scl_lock);
307e14bb32Jeff Bonwick	}
308e14bb32Jeff Bonwick	return (1);
309e05725bbonwick}
310e05725bbonwick
311e05725bbonwickvoid
312e14bb32Jeff Bonwickspa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw)
313e05725bbonwick{
314f64c0e3Eric Taylor	int wlocks_held = 0;
315f64c0e3Eric Taylor
316e14bb32Jeff Bonwick	for (int i = 0; i < SCL_LOCKS; i++) {
317e14bb32Jeff Bonwick		spa_config_lock_t *scl = &spa->spa_config_lock[i];
318f64c0e3Eric Taylor		if (scl->scl_writer == curthread)
319f64c0e3Eric Taylor			wlocks_held |= (1 << i);
320e14bb32Jeff Bonwick		if (!(locks & (1 << i)))
321e14bb32Jeff Bonwick			continue;
322e14bb32Jeff Bonwick		mutex_enter(&scl->scl_lock);
323e14bb32Jeff Bonwick		if (rw == RW_READER) {
324e14bb32Jeff Bonwick			while (scl->scl_writer || scl->scl_write_wanted) {
325e14bb32Jeff Bonwick				cv_wait(&scl->scl_cv, &scl->scl_lock);
326e14bb32Jeff Bonwick			}
327e14bb32Jeff Bonwick		} else {
328e14bb32Jeff Bonwick			ASSERT(scl->scl_writer != curthread);
329e14bb32Jeff Bonwick			while (!refcount_is_zero(&scl->scl_count)) {
330e14bb32Jeff Bonwick				scl->scl_write_wanted++;
331e14bb32Jeff Bonwick				cv_wait(&scl->scl_cv, &scl->scl_lock);
332e14bb32Jeff Bonwick				scl->scl_write_wanted--;
333e14bb32Jeff Bonwick			}
334e14bb32Jeff Bonwick			scl->scl_writer = curthread;
335e14bb32Jeff Bonwick		}
336e14bb32Jeff Bonwick		(void) refcount_add(&scl->scl_count, tag);
337e14bb32Jeff Bonwick		mutex_exit(&scl->scl_lock);
338e05725bbonwick	}
339f64c0e3Eric Taylor	ASSERT(wlocks_held <= locks);
340e05725bbonwick}
341e05725bbonwick
342e05725bbonwickvoid
343e14bb32Jeff Bonwickspa_config_exit(spa_t *spa, int locks, void *tag)
344e05725bbonwick{
345e14bb32Jeff Bonwick	for (int i = SCL_LOCKS - 1; i >= 0; i--) {
346e14bb32Jeff Bonwick		spa_config_lock_t *scl = &spa->spa_config_lock[i];
347e14bb32Jeff Bonwick		if (!(locks & (1 << i)))
348e14bb32Jeff Bonwick			continue;
349e14bb32Jeff Bonwick		mutex_enter(&scl->scl_lock);
350e14bb32Jeff Bonwick		ASSERT(!refcount_is_zero(&scl->scl_count));
351e14bb32Jeff Bonwick		if (refcount_remove(&scl->scl_count, tag) == 0) {
352e14bb32Jeff Bonwick			ASSERT(scl->scl_writer == NULL ||
353e14bb32Jeff Bonwick			    scl->scl_writer == curthread);
354e14bb32Jeff Bonwick			scl->scl_writer = NULL;	/* OK in either case */
355e14bb32Jeff Bonwick			cv_broadcast(&scl->scl_cv);
356e14bb32Jeff Bonwick		}
357e14bb32Jeff Bonwick		mutex_exit(&scl->scl_lock);
358e05725bbonwick	}
359e05725bbonwick}
360e05725bbonwick
361e14bb32Jeff Bonwickint
362e14bb32Jeff Bonwickspa_config_held(spa_t *spa, int locks, krw_t rw)
363e05725bbonwick{
364e14bb32Jeff Bonwick	int locks_held = 0;
365e05725bbonwick
366e14bb32Jeff Bonwick	for (int i = 0; i < SCL_LOCKS; i++) {
367e14bb32Jeff Bonwick		spa_config_lock_t *scl = &spa->spa_config_lock[i];
368e14bb32Jeff Bonwick		if (!(locks & (1 << i)))
369e14bb32Jeff Bonwick			continue;
370e14bb32Jeff Bonwick		if ((rw == RW_READER && !refcount_is_zero(&scl->scl_count)) ||
371e14bb32Jeff Bonwick		    (rw == RW_WRITER && scl->scl_writer == curthread))
372e14bb32Jeff Bonwick			locks_held |= 1 << i;
373e14bb32Jeff Bonwick	}
374e14bb32Jeff Bonwick
375e14bb32Jeff Bonwick	return (locks_held);
376e05725bbonwick}
377e05725bbonwick
378e05725bbonwick/*
379e05725bbonwick * ==========================================================================
380fa9e406ahrens * SPA namespace functions
381fa9e406ahrens * ==========================================================================
382fa9e406ahrens */
383fa9e406ahrens
384fa9e406ahrens/*
385fa9e406ahrens * Lookup the named spa_t in the AVL tree.  The spa_namespace_lock must be held.
386fa9e406ahrens * Returns NULL if no matching spa_t is found.
387fa9e406ahrens */
388fa9e406ahrensspa_t *
389fa9e406ahrensspa_lookup(const char *name)
390fa9e406ahrens{
391e14bb32Jeff Bonwick	static spa_t search;	/* spa_t is large; don't allocate on stack */
392e14bb32Jeff Bonwick	spa_t *spa;
393fa9e406ahrens	avl_index_t where;
39440feaa9ahrens	char c;
39540feaa9ahrens	char *cp;
396fa9e406ahrens
397fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
398fa9e406ahrens
39940feaa9ahrens	/*
40040feaa9ahrens	 * If it's a full dataset name, figure out the pool name and
40140feaa9ahrens	 * just use that.
40240feaa9ahrens	 */
40340feaa9ahrens	cp = strpbrk(name, "/@");
40440feaa9ahrens	if (cp) {
40540feaa9ahrens		c = *cp;
40640feaa9ahrens		*cp = '\0';
40740feaa9ahrens	}
40840feaa9ahrens
409e14bb32Jeff Bonwick	(void) strlcpy(search.spa_name, name, sizeof (search.spa_name));
410fa9e406ahrens	spa = avl_find(&spa_namespace_avl, &search, &where);
411fa9e406ahrens
41240feaa9ahrens	if (cp)
41340feaa9ahrens		*cp = c;
41440feaa9ahrens
415fa9e406ahrens	return (spa);
416fa9e406ahrens}
417fa9e406ahrens
418fa9e406ahrens/*
419fa9e406ahrens * Create an uninitialized spa_t with the given name.  Requires
420fa9e406ahrens * spa_namespace_lock.  The caller must ensure that the spa_t doesn't already
421fa9e406ahrens * exist by calling spa_lookup() first.
422fa9e406ahrens */
423fa9e406ahrensspa_t *
424468c413Tim Haleyspa_add(const char *name, nvlist_t *config, const char *altroot)
425fa9e406ahrens{
426fa9e406ahrens	spa_t *spa;
427c5904d1eschrock	spa_config_dirent_t *dp;
428fa9e406ahrens
429fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
430fa9e406ahrens
431fa9e406ahrens	spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
432fa9e406ahrens
433c25056dgw	mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL);
434c25056dgw	mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL);
435c25056dgw	mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL);
436c25056dgw	mutex_init(&spa->spa_errlist_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
444b24ab67Jeff Bonwick	for (int t = 0; t < TXG_SIZE; t++)
445b24ab67Jeff Bonwick		bplist_init(&spa->spa_free_bplist[t]);
446b24ab67Jeff Bonwick	bplist_init(&spa->spa_deferred_bplist);
447b24ab67Jeff Bonwick
448e14bb32Jeff Bonwick	(void) strlcpy(spa->spa_name, name, sizeof (spa->spa_name));
449fa9e406ahrens	spa->spa_state = POOL_STATE_UNINITIALIZED;
450fa9e406ahrens	spa->spa_freeze_txg = UINT64_MAX;
4510373e76bonwick	spa->spa_final_txg = UINT64_MAX;
452468c413Tim Haley	spa->spa_load_max_txg = UINT64_MAX;
453fa9e406ahrens
454fa9e406ahrens	refcount_create(&spa->spa_refcount);
455e14bb32Jeff Bonwick	spa_config_lock_init(spa);
456fa9e406ahrens
457fa9e406ahrens	avl_add(&spa_namespace_avl, spa);
458fa9e406ahrens
459e14bb32Jeff Bonwick	mutex_init(&spa->spa_suspend_lock, NULL, MUTEX_DEFAULT, NULL);
4600a4e951gw
4610373e76bonwick	/*
4620373e76bonwick	 * Set the alternate root, if there is one.
4630373e76bonwick	 */
4640373e76bonwick	if (altroot) {
4650373e76bonwick		spa->spa_root = spa_strdup(altroot);
4660373e76bonwick		spa_active_count++;
4670373e76bonwick	}
4680373e76bonwick
469c5904d1eschrock	/*
470c5904d1eschrock	 * Every pool starts with the default cachefile
471c5904d1eschrock	 */
472c5904d1eschrock	list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t),
473c5904d1eschrock	    offsetof(spa_config_dirent_t, scd_link));
474c5904d1eschrock
475c5904d1eschrock	dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP);
476c5904d1eschrock	dp->scd_path = spa_strdup(spa_config_path);
477c5904d1eschrock	list_insert_head(&spa->spa_config_list, dp);
478c5904d1eschrock
479468c413Tim Haley	if (config != NULL)
480468c413Tim Haley		VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0);
481468c413Tim Haley
482fa9e406ahrens	return (spa);
483fa9e406ahrens}
484fa9e406ahrens
485fa9e406ahrens/*
486fa9e406ahrens * Removes a spa_t from the namespace, freeing up any memory used.  Requires
487fa9e406ahrens * spa_namespace_lock.  This is called only after the spa_t has been closed and
488fa9e406ahrens * deactivated.
489fa9e406ahrens */
490fa9e406ahrensvoid
491fa9e406ahrensspa_remove(spa_t *spa)
492fa9e406ahrens{
493c5904d1eschrock	spa_config_dirent_t *dp;
494c5904d1eschrock
495fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
496fa9e406ahrens	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
497fa9e406ahrens
498fa9e406ahrens	avl_remove(&spa_namespace_avl, spa);
499fa9e406ahrens	cv_broadcast(&spa_namespace_cv);
500fa9e406ahrens
5010373e76bonwick	if (spa->spa_root) {
502fa9e406ahrens		spa_strfree(spa->spa_root);
5030373e76bonwick		spa_active_count--;
5040373e76bonwick	}
505fa9e406ahrens
506c5904d1eschrock	while ((dp = list_head(&spa->spa_config_list)) != NULL) {
507c5904d1eschrock		list_remove(&spa->spa_config_list, dp);
508c5904d1eschrock		if (dp->scd_path != NULL)
509c5904d1eschrock			spa_strfree(dp->scd_path);
510c5904d1eschrock		kmem_free(dp, sizeof (spa_config_dirent_t));
511c5904d1eschrock	}
512c5904d1eschrock
513c5904d1eschrock	list_destroy(&spa->spa_config_list);
5142f8aaabeschrock
515fa9e406ahrens	spa_config_set(spa, NULL);
516fa9e406ahrens
517fa9e406ahrens	refcount_destroy(&spa->spa_refcount);
51891ebeefahrens
519e14bb32Jeff Bonwick	spa_config_lock_destroy(spa);
520fa9e406ahrens
521b24ab67Jeff Bonwick	for (int t = 0; t < TXG_SIZE; t++)
522b24ab67Jeff Bonwick		bplist_fini(&spa->spa_free_bplist[t]);
523b24ab67Jeff Bonwick	bplist_fini(&spa->spa_deferred_bplist);
524b24ab67Jeff Bonwick
525c25056dgw	cv_destroy(&spa->spa_async_cv);
526c25056dgw	cv_destroy(&spa->spa_scrub_io_cv);
527e14bb32Jeff Bonwick	cv_destroy(&spa->spa_suspend_cv);
528c25056dgw
5295ad8204nd	mutex_destroy(&spa->spa_async_lock);
530c25056dgw	mutex_destroy(&spa->spa_scrub_lock);
531c25056dgw	mutex_destroy(&spa->spa_errlog_lock);
532c25056dgw	mutex_destroy(&spa->spa_errlist_lock);
53306eeb2aek	mutex_destroy(&spa->spa_history_lock);
534b1b8ab3lling	mutex_destroy(&spa->spa_props_lock);
535e14bb32Jeff Bonwick	mutex_destroy(&spa->spa_suspend_lock);
5365ad8204nd
537fa9e406ahrens	kmem_free(spa, sizeof (spa_t));
538fa9e406ahrens}
539fa9e406ahrens
540fa9e406ahrens/*
541fa9e406ahrens * Given a pool, return the next pool in the namespace, or NULL if there is
542fa9e406ahrens * none.  If 'prev' is NULL, return the first pool.
543fa9e406ahrens */
544fa9e406ahrensspa_t *
545fa9e406ahrensspa_next(spa_t *prev)
546fa9e406ahrens{
547fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
548fa9e406ahrens
549fa9e406ahrens	if (prev)
550fa9e406ahrens		return (AVL_NEXT(&spa_namespace_avl, prev));
551fa9e406ahrens	else
552fa9e406ahrens		return (avl_first(&spa_namespace_avl));
553fa9e406ahrens}
554fa9e406ahrens
555fa9e406ahrens/*
556fa9e406ahrens * ==========================================================================
557fa9e406ahrens * SPA refcount functions
558fa9e406ahrens * ==========================================================================
559fa9e406ahrens */
560fa9e406ahrens
561fa9e406ahrens/*
562fa9e406ahrens * Add a reference to the given spa_t.  Must have at least one reference, or
563fa9e406ahrens * have the namespace lock held.
564fa9e406ahrens */
565fa9e406ahrensvoid
566fa9e406ahrensspa_open_ref(spa_t *spa, void *tag)
567fa9e406ahrens{
568088f389ahrens	ASSERT(refcount_count(&spa->spa_refcount) >= spa->spa_minref ||
569fa9e406ahrens	    MUTEX_HELD(&spa_namespace_lock));
570fa9e406ahrens	(void) refcount_add(&spa->spa_refcount, tag);
571fa9e406ahrens}
572fa9e406ahrens
573fa9e406ahrens/*
574fa9e406ahrens * Remove a reference to the given spa_t.  Must have at least one reference, or
575fa9e406ahrens * have the namespace lock held.
576fa9e406ahrens */
577fa9e406ahrensvoid
578fa9e406ahrensspa_close(spa_t *spa, void *tag)
579fa9e406ahrens{
580088f389ahrens	ASSERT(refcount_count(&spa->spa_refcount) > spa->spa_minref ||
581fa9e406ahrens	    MUTEX_HELD(&spa_namespace_lock));
582fa9e406ahrens	(void) refcount_remove(&spa->spa_refcount, tag);
583fa9e406ahrens}
584fa9e406ahrens
585fa9e406ahrens/*
586fa9e406ahrens * Check to see if the spa refcount is zero.  Must be called with
587088f389ahrens * spa_namespace_lock held.  We really compare against spa_minref, which is the
588fa9e406ahrens * number of references acquired when opening a pool
589fa9e406ahrens */
590fa9e406ahrensboolean_t
591fa9e406ahrensspa_refcount_zero(spa_t *spa)
592fa9e406ahrens{
593fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
594fa9e406ahrens
595088f389ahrens	return (refcount_count(&spa->spa_refcount) == spa->spa_minref);
596fa9e406ahrens}
597fa9e406ahrens
598fa9e406ahrens/*
599fa9e406ahrens * ==========================================================================
600fa94a07brendan * SPA spare and l2cache tracking
60199653d4eschrock * ==========================================================================
60299653d4eschrock */
60399653d4eschrock
60499653d4eschrock/*
605fa94a07brendan * Hot spares and cache devices are tracked using the same code below,
606fa94a07brendan * for 'auxiliary' devices.
607fa94a07brendan */
608fa94a07brendan
609fa94a07brendantypedef struct spa_aux {
610fa94a07brendan	uint64_t	aux_guid;
611fa94a07brendan	uint64_t	aux_pool;
612fa94a07brendan	avl_node_t	aux_avl;
613fa94a07brendan	int		aux_count;
614fa94a07brendan} spa_aux_t;
615fa94a07brendan
616fa94a07brendanstatic int
617fa94a07brendanspa_aux_compare(const void *a, const void *b)
618fa94a07brendan{
619fa94a07brendan	const spa_aux_t *sa = a;
620fa94a07brendan	const spa_aux_t *sb = b;
621fa94a07brendan
622fa94a07brendan	if (sa->aux_guid < sb->aux_guid)
623fa94a07brendan		return (-1);
624fa94a07brendan	else if (sa->aux_guid > sb->aux_guid)
625fa94a07brendan		return (1);
626fa94a07brendan	else
627fa94a07brendan		return (0);
628fa94a07brendan}
629fa94a07brendan
630fa94a07brendanvoid
631fa94a07brendanspa_aux_add(vdev_t *vd, avl_tree_t *avl)
632fa94a07brendan{
633fa94a07brendan	avl_index_t where;
634fa94a07brendan	spa_aux_t search;
635fa94a07brendan	spa_aux_t *aux;
636fa94a07brendan
637fa94a07brendan	search.aux_guid = vd->vdev_guid;
638fa94a07brendan	if ((aux = avl_find(avl, &search, &where)) != NULL) {
639fa94a07brendan		aux->aux_count++;
640fa94a07brendan	} else {
641fa94a07brendan		aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP);
642fa94a07brendan		aux->aux_guid = vd->vdev_guid;
643fa94a07brendan		aux->aux_count = 1;
644fa94a07brendan		avl_insert(avl, aux, where);
645fa94a07brendan	}
646fa94a07brendan}
647fa94a07brendan
648fa94a07brendanvoid
649fa94a07brendanspa_aux_remove(vdev_t *vd, avl_tree_t *avl)
650fa94a07brendan{
651fa94a07brendan	spa_aux_t search;
652fa94a07brendan	spa_aux_t *aux;
653fa94a07brendan	avl_index_t where;
654fa94a07brendan
655fa94a07brendan	search.aux_guid = vd->vdev_guid;
656fa94a07brendan	aux = avl_find(avl, &search, &where);
657fa94a07brendan
658fa94a07brendan	ASSERT(aux != NULL);
659fa94a07brendan
660fa94a07brendan	if (--aux->aux_count == 0) {
661fa94a07brendan		avl_remove(avl, aux);
662fa94a07brendan		kmem_free(aux, sizeof (spa_aux_t));
663fa94a07brendan	} else if (aux->aux_pool == spa_guid(vd->vdev_spa)) {
664fa94a07brendan		aux->aux_pool = 0ULL;
665fa94a07brendan	}
666fa94a07brendan}
667fa94a07brendan
668fa94a07brendanboolean_t
66989a89ebllingspa_aux_exists(uint64_t guid, uint64_t *pool, int *refcnt, avl_tree_t *avl)
670fa94a07brendan{
671fa94a07brendan	spa_aux_t search, *found;
672fa94a07brendan
673fa94a07brendan	search.aux_guid = guid;
67489a89eblling	found = avl_find(avl, &search, NULL);
675fa94a07brendan
676fa94a07brendan	if (pool) {
677fa94a07brendan		if (found)
678fa94a07brendan			*pool = found->aux_pool;
679fa94a07brendan		else
680fa94a07brendan			*pool = 0ULL;
681fa94a07brendan	}
682fa94a07brendan
68389a89eblling	if (refcnt) {
68489a89eblling		if (found)
68589a89eblling			*refcnt = found->aux_count;
68689a89eblling		else
68789a89eblling			*refcnt = 0;
68889a89eblling	}
68989a89eblling
690fa94a07brendan	return (found != NULL);
691fa94a07brendan}
692fa94a07brendan
693fa94a07brendanvoid
694fa94a07brendanspa_aux_activate(vdev_t *vd, avl_tree_t *avl)
695fa94a07brendan{
696fa94a07brendan	spa_aux_t search, *found;
697fa94a07brendan	avl_index_t where;
698fa94a07brendan
699fa94a07brendan	search.aux_guid = vd->vdev_guid;
700fa94a07brendan	found = avl_find(avl, &search, &where);
701fa94a07brendan	ASSERT(found != NULL);
702fa94a07brendan	ASSERT(found->aux_pool == 0ULL);
703fa94a07brendan
704fa94a07brendan	found->aux_pool = spa_guid(vd->vdev_spa);
705fa94a07brendan}
706fa94a07brendan
707fa94a07brendan/*
70839c2341eschrock * Spares are tracked globally due to the following constraints:
70939c2341eschrock *
71039c2341eschrock * 	- A spare may be part of multiple pools.
71139c2341eschrock * 	- A spare may be added to a pool even if it's actively in use within
71239c2341eschrock *	  another pool.
71339c2341eschrock * 	- A spare in use in any pool can only be the source of a replacement if
71439c2341eschrock *	  the target is a spare in the same pool.
71539c2341eschrock *
71639c2341eschrock * We keep track of all spares on the system through the use of a reference
71739c2341eschrock * counted AVL tree.  When a vdev is added as a spare, or used as a replacement
71839c2341eschrock * spare, then we bump the reference count in the AVL tree.  In addition, we set
71939c2341eschrock * the 'vdev_isspare' member to indicate that the device is a spare (active or
72039c2341eschrock * inactive).  When a spare is made active (used to replace a device in the
72139c2341eschrock * pool), we also keep track of which pool its been made a part of.
72239c2341eschrock *
72339c2341eschrock * The 'spa_spare_lock' protects the AVL tree.  These functions are normally
72439c2341eschrock * called under the spa_namespace lock as part of vdev reconfiguration.  The
72539c2341eschrock * separate spare lock exists for the status query path, which does not need to
72639c2341eschrock * be completely consistent with respect to other vdev configuration changes.
72799653d4eschrock */
72839c2341eschrock
72999653d4eschrockstatic int
73099653d4eschrockspa_spare_compare(const void *a, const void *b)
73199653d4eschrock{
732fa94a07brendan	return (spa_aux_compare(a, b));
73399653d4eschrock}
73499653d4eschrock
73599653d4eschrockvoid
73639c2341eschrockspa_spare_add(vdev_t *vd)
73799653d4eschrock{
73899653d4eschrock	mutex_enter(&spa_spare_lock);
73939c2341eschrock	ASSERT(!vd->vdev_isspare);
740fa94a07brendan	spa_aux_add(vd, &spa_spare_avl);
74139c2341eschrock	vd->vdev_isspare = B_TRUE;
74299653d4eschrock	mutex_exit(&spa_spare_lock);
74399653d4eschrock}
74499653d4eschrock
74599653d4eschrockvoid
74639c2341eschrockspa_spare_remove(vdev_t *vd)
74799653d4eschrock{
74899653d4eschrock	mutex_enter(&spa_spare_lock);
74939c2341eschrock	ASSERT(vd->vdev_isspare);
750fa94a07brendan	spa_aux_remove(vd, &spa_spare_avl);
75139c2341eschrock	vd->vdev_isspare = B_FALSE;
75299653d4eschrock	mutex_exit(&spa_spare_lock);
75399653d4eschrock}
75499653d4eschrock
75599653d4eschrockboolean_t
75689a89ebllingspa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt)
75799653d4eschrock{
758fa94a07brendan	boolean_t found;
75999653d4eschrock
76099653d4eschrock	mutex_enter(&spa_spare_lock);
76189a89eblling	found = spa_aux_exists(guid, pool, refcnt, &spa_spare_avl);
76299653d4eschrock	mutex_exit(&spa_spare_lock);
76399653d4eschrock
764fa94a07brendan	return (found);
76539c2341eschrock}
76639c2341eschrock
76739c2341eschrockvoid
76839c2341eschrockspa_spare_activate(vdev_t *vd)
76939c2341eschrock{
77039c2341eschrock	mutex_enter(&spa_spare_lock);
77139c2341eschrock	ASSERT(vd->vdev_isspare);
772fa94a07brendan	spa_aux_activate(vd, &spa_spare_avl);
773fa94a07brendan	mutex_exit(&spa_spare_lock);
774fa94a07brendan}
77539c2341eschrock
776fa94a07brendan/*
777fa94a07brendan * Level 2 ARC devices are tracked globally for the same reasons as spares.
778fa94a07brendan * Cache devices currently only support one pool per cache device, and so
779fa94a07brendan * for these devices the aux reference count is currently unused beyond 1.
780fa94a07brendan */
78139c2341eschrock
782fa94a07brendanstatic int
783fa94a07brendanspa_l2cache_compare(const void *a, const void *b)
784fa94a07brendan{
785fa94a07brendan	return (spa_aux_compare(a, b));
786fa94a07brendan}
787fa94a07brendan
788fa94a07brendanvoid
789fa94a07brendanspa_l2cache_add(vdev_t *vd)
790fa94a07brendan{
791fa94a07brendan	mutex_enter(&spa_l2cache_lock);
792fa94a07brendan	ASSERT(!vd->vdev_isl2cache);
793fa94a07brendan	spa_aux_add(vd, &spa_l2cache_avl);
794fa94a07brendan	vd->vdev_isl2cache = B_TRUE;
795fa94a07brendan	mutex_exit(&spa_l2cache_lock);
796fa94a07brendan}
797fa94a07brendan
798fa94a07brendanvoid
799fa94a07brendanspa_l2cache_remove(vdev_t *vd)
800fa94a07brendan{
801fa94a07brendan	mutex_enter(&spa_l2cache_lock);
802fa94a07brendan	ASSERT(vd->vdev_isl2cache);
803fa94a07brendan	spa_aux_remove(vd, &spa_l2cache_avl);
804fa94a07brendan	vd->vdev_isl2cache = B_FALSE;
805fa94a07brendan	mutex_exit(&spa_l2cache_lock);
806fa94a07brendan}
807fa94a07brendan
808fa94a07brendanboolean_t
809fa94a07brendanspa_l2cache_exists(uint64_t guid, uint64_t *pool)
810fa94a07brendan{
811fa94a07brendan	boolean_t found;
812fa94a07brendan
813fa94a07brendan	mutex_enter(&spa_l2cache_lock);
81489a89eblling	found = spa_aux_exists(guid, pool, NULL, &spa_l2cache_avl);
815fa94a07brendan	mutex_exit(&spa_l2cache_lock);
816fa94a07brendan
817fa94a07brendan	return (found);
818fa94a07brendan}
819fa94a07brendan
820fa94a07brendanvoid
821fa94a07brendanspa_l2cache_activate(vdev_t *vd)
822fa94a07brendan{
823fa94a07brendan	mutex_enter(&spa_l2cache_lock);
824fa94a07brendan	ASSERT(vd->vdev_isl2cache);
825fa94a07brendan	spa_aux_activate(vd, &spa_l2cache_avl);
826fa94a07brendan	mutex_exit(&spa_l2cache_lock);
827fa94a07brendan}
828fa94a07brendan
82999653d4eschrock/*
83099653d4eschrock * ==========================================================================
831fa9e406ahrens * SPA vdev locking
832fa9e406ahrens * ==========================================================================
833fa9e406ahrens */
834fa9e406ahrens
835fa9e406ahrens/*
836ea8dc4beschrock * Lock the given spa_t for the purpose of adding or removing a vdev.
837ea8dc4beschrock * Grabs the global spa_namespace_lock plus the spa config lock for writing.
838fa9e406ahrens * It returns the next transaction group for the spa_t.
839fa9e406ahrens */
840fa9e406ahrensuint64_t
841fa9e406ahrensspa_vdev_enter(spa_t *spa)
842fa9e406ahrens{
8433d7072feschrock	mutex_enter(&spa_namespace_lock);
84488ecc94George Wilson	return (spa_vdev_config_enter(spa));
84588ecc94George Wilson}
84688ecc94George Wilson
84788ecc94George Wilson/*
84888ecc94George Wilson * Internal implementation for spa_vdev_enter().  Used when a vdev
84988ecc94George Wilson * operation requires multiple syncs (i.e. removing a device) while
85088ecc94George Wilson * keeping the spa_namespace_lock held.
85188ecc94George Wilson */
85288ecc94George Wilsonuint64_t
85388ecc94George Wilsonspa_vdev_config_enter(spa_t *spa)
85488ecc94George Wilson{
85588ecc94George Wilson	ASSERT(MUTEX_HELD(&spa_namespace_lock));
8563d7072feschrock
857e14bb32Jeff Bonwick	spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
858fa9e406ahrens
859fa9e406ahrens	return (spa_last_synced_txg(spa) + 1);
860fa9e406ahrens}
861fa9e406ahrens
862fa9e406ahrens/*
86388ecc94George Wilson * Used in combination with spa_vdev_config_enter() to allow the syncing
86488ecc94George Wilson * of multiple transactions without releasing the spa_namespace_lock.
865fa9e406ahrens */
86688ecc94George Wilsonvoid
86788ecc94George Wilsonspa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error, char *tag)
868fa9e406ahrens{
86988ecc94George Wilson	ASSERT(MUTEX_HELD(&spa_namespace_lock));
87088ecc94George Wilson
8710e34b6abonwick	int config_changed = B_FALSE;
872ea8dc4beschrock
8730373e76bonwick	ASSERT(txg > spa_last_synced_txg(spa));
8740e34b6abonwick
875e14bb32Jeff Bonwick	spa->spa_pending_vdev = NULL;
876e14bb32Jeff Bonwick
8770e34b6abonwick	/*
8780e34b6abonwick	 * Reassess the DTLs.
8790e34b6abonwick	 */
8800373e76bonwick	vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE);
8810e34b6abonwick
8820e34b6abonwick	/*
8830373e76bonwick	 * If the config changed, notify the scrub thread that it must restart.
8840e34b6abonwick	 */
885e14bb32Jeff Bonwick	if (error == 0 && !list_is_empty(&spa->spa_config_dirty_list)) {
886088f389ahrens		dsl_pool_scrub_restart(spa->spa_dsl_pool);
8870e34b6abonwick		config_changed = B_TRUE;
8888f18d1fGeorge Wilson		spa->spa_config_generation++;
8890e34b6abonwick	}
890ea8dc4beschrock
89188ecc94George Wilson	/*
89288ecc94George Wilson	 * Verify the metaslab classes.
89388ecc94George Wilson	 */
894b24ab67Jeff Bonwick	ASSERT(metaslab_class_validate(spa_normal_class(spa)) == 0);
895b24ab67Jeff Bonwick	ASSERT(metaslab_class_validate(spa_log_class(spa)) == 0);
89688ecc94George Wilson
897e14bb32Jeff Bonwick	spa_config_exit(spa, SCL_ALL, spa);
898fa9e406ahrens
899ea8dc4beschrock	/*
90088ecc94George Wilson	 * Panic the system if the specified tag requires it.  This
90188ecc94George Wilson	 * is useful for ensuring that configurations are updated
90288ecc94George Wilson	 * transactionally.
90388ecc94George Wilson	 */
90488ecc94George Wilson	if (zio_injection_enabled)
90588ecc94George Wilson		zio_handle_panic_injection(spa, tag);
90688ecc94George Wilson
90788ecc94George Wilson	/*
908fa9e406ahrens	 * Note: