spa_misc.c revision 0373e76b3c3643df49ef3483e0f293fdea61d8c6
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/*
22ea8dc4beschrock * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23fa9e406ahrens * Use is subject to license terms.
24fa9e406ahrens */
25fa9e406ahrens
26fa9e406ahrens#pragma ident	"%Z%%M%	%I%	%E% SMI"
27fa9e406ahrens
28fa9e406ahrens#include <sys/zfs_context.h>
29fa9e406ahrens#include <sys/spa_impl.h>
30fa9e406ahrens#include <sys/zio.h>
31fa9e406ahrens#include <sys/zio_checksum.h>
32fa9e406ahrens#include <sys/zio_compress.h>
33fa9e406ahrens#include <sys/dmu.h>
34fa9e406ahrens#include <sys/dmu_tx.h>
35fa9e406ahrens#include <sys/zap.h>
36fa9e406ahrens#include <sys/zil.h>
37fa9e406ahrens#include <sys/vdev_impl.h>
38fa9e406ahrens#include <sys/metaslab.h>
39fa9e406ahrens#include <sys/uberblock_impl.h>
40fa9e406ahrens#include <sys/txg.h>
41fa9e406ahrens#include <sys/avl.h>
42fa9e406ahrens#include <sys/unique.h>
43fa9e406ahrens#include <sys/dsl_pool.h>
44fa9e406ahrens#include <sys/dsl_dir.h>
45fa9e406ahrens#include <sys/dsl_prop.h>
46fa9e406ahrens#include <sys/fs/zfs.h>
47fa9e406ahrens
48fa9e406ahrens/*
49fa9e406ahrens * SPA locking
50fa9e406ahrens *
51fa9e406ahrens * There are four basic locks for managing spa_t structures:
52fa9e406ahrens *
53fa9e406ahrens * spa_namespace_lock (global mutex)
54fa9e406ahrens *
55fa9e406ahrens * 	This lock must be acquired to do any of the following:
56fa9e406ahrens *
57fa9e406ahrens * 		- Lookup a spa_t by name
58fa9e406ahrens * 		- Add or remove a spa_t from the namespace
59fa9e406ahrens * 		- Increase spa_refcount from non-zero
60fa9e406ahrens * 		- Check if spa_refcount is zero
61fa9e406ahrens * 		- Rename a spa_t
62ea8dc4beschrock *		- add/remove/attach/detach devices
63fa9e406ahrens * 		- Held for the duration of create/destroy/import/export
64fa9e406ahrens *
65fa9e406ahrens * 	It does not need to handle recursion.  A create or destroy may
66fa9e406ahrens * 	reference objects (files or zvols) in other pools, but by
67fa9e406ahrens * 	definition they must have an existing reference, and will never need
68fa9e406ahrens * 	to lookup a spa_t by name.
69fa9e406ahrens *
70fa9e406ahrens * spa_refcount (per-spa refcount_t protected by mutex)
71fa9e406ahrens *
72fa9e406ahrens * 	This reference count keep track of any active users of the spa_t.  The
73fa9e406ahrens * 	spa_t cannot be destroyed or freed while this is non-zero.  Internally,
74fa9e406ahrens * 	the refcount is never really 'zero' - opening a pool implicitly keeps
75fa9e406ahrens * 	some references in the DMU.  Internally we check against SPA_MINREF, but
76fa9e406ahrens * 	present the image of a zero/non-zero value to consumers.
77fa9e406ahrens *
78fa9e406ahrens * spa_config_lock (per-spa crazy rwlock)
79fa9e406ahrens *
80fa9e406ahrens * 	This SPA special is a recursive rwlock, capable of being acquired from
81fa9e406ahrens * 	asynchronous threads.  It has protects the spa_t from config changes,
82fa9e406ahrens * 	and must be held in the following circumstances:
83fa9e406ahrens *
84fa9e406ahrens * 		- RW_READER to perform I/O to the spa
85fa9e406ahrens * 		- RW_WRITER to change the vdev config
86fa9e406ahrens *
87fa9e406ahrens * spa_config_cache_lock (per-spa mutex)
88fa9e406ahrens *
89fa9e406ahrens * 	This mutex prevents the spa_config nvlist from being updated.  No
90fa9e406ahrens *      other locks are required to obtain this lock, although implicitly you
91fa9e406ahrens *      must have the namespace lock or non-zero refcount to have any kind
92fa9e406ahrens *      of spa_t pointer at all.
93fa9e406ahrens *
94fa9e406ahrens * The locking order is fairly straightforward:
95fa9e406ahrens *
96fa9e406ahrens * 		spa_namespace_lock	->	spa_refcount
97fa9e406ahrens *
98fa9e406ahrens * 	The namespace lock must be acquired to increase the refcount from 0
99fa9e406ahrens * 	or to check if it is zero.
100fa9e406ahrens *
101fa9e406ahrens * 		spa_refcount 		->	spa_config_lock
102fa9e406ahrens *
103fa9e406ahrens * 	There must be at least one valid reference on the spa_t to acquire
104fa9e406ahrens * 	the config lock.
105fa9e406ahrens *
106ea8dc4beschrock * 		spa_namespace_lock	->	spa_config_lock
107fa9e406ahrens *
108ea8dc4beschrock * 	The namespace lock must always be taken before the config lock.
109fa9e406ahrens *
110fa9e406ahrens *
111fa9e406ahrens * The spa_namespace_lock and spa_config_cache_lock can be acquired directly and
112fa9e406ahrens * are globally visible.
113fa9e406ahrens *
114fa9e406ahrens * The namespace is manipulated using the following functions, all which require
115fa9e406ahrens * the spa_namespace_lock to be held.
116fa9e406ahrens *
117fa9e406ahrens * 	spa_lookup()		Lookup a spa_t by name.
118fa9e406ahrens *
119fa9e406ahrens * 	spa_add()		Create a new spa_t in the namespace.
120fa9e406ahrens *
121fa9e406ahrens * 	spa_remove()		Remove a spa_t from the namespace.  This also
122fa9e406ahrens * 				frees up any memory associated with the spa_t.
123fa9e406ahrens *
124fa9e406ahrens * 	spa_next()		Returns the next spa_t in the system, or the
125fa9e406ahrens * 				first if NULL is passed.
126fa9e406ahrens *
127fa9e406ahrens * 	spa_evict_all()		Shutdown and remove all spa_t structures in
128fa9e406ahrens * 				the system.
129fa9e406ahrens *
130ea8dc4beschrock *	spa_guid_exists()	Determine whether a pool/device guid exists.
131fa9e406ahrens *
132fa9e406ahrens * The spa_refcount is manipulated using the following functions:
133fa9e406ahrens *
134fa9e406ahrens * 	spa_open_ref()		Adds a reference to the given spa_t.  Must be
135fa9e406ahrens * 				called with spa_namespace_lock held if the
136fa9e406ahrens * 				refcount is currently zero.
137fa9e406ahrens *
138fa9e406ahrens * 	spa_close()		Remove a reference from the spa_t.  This will
139fa9e406ahrens * 				not free the spa_t or remove it from the
140fa9e406ahrens * 				namespace.  No locking is required.
141fa9e406ahrens *
142fa9e406ahrens * 	spa_refcount_zero()	Returns true if the refcount is currently
143fa9e406ahrens * 				zero.  Must be called with spa_namespace_lock
144fa9e406ahrens * 				held.
145fa9e406ahrens *
146fa9e406ahrens * The spa_config_lock is manipulated using the following functions:
147fa9e406ahrens *
148fa9e406ahrens * 	spa_config_enter()	Acquire the config lock as RW_READER or
149fa9e406ahrens * 				RW_WRITER.  At least one reference on the spa_t
150fa9e406ahrens * 				must exist.
151fa9e406ahrens *
152fa9e406ahrens * 	spa_config_exit()	Release the config lock.
153fa9e406ahrens *
154fa9e406ahrens * 	spa_config_held()	Returns true if the config lock is currently
155fa9e406ahrens * 				held in the given state.
156fa9e406ahrens *
157ea8dc4beschrock * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
158fa9e406ahrens *
159ea8dc4beschrock * 	spa_vdev_enter()	Acquire the namespace lock and the config lock
160ea8dc4beschrock *				for writing.
161fa9e406ahrens *
162fa9e406ahrens * 	spa_vdev_exit()		Release the config lock, wait for all I/O
163ea8dc4beschrock * 				to complete, sync the updated configs to the
164ea8dc4beschrock *				cache, and release the namespace lock.
165fa9e406ahrens *
166fa9e406ahrens * The spa_name() function also requires either the spa_namespace_lock
167fa9e406ahrens * or the spa_config_lock, as both are needed to do a rename.  spa_rename() is
168fa9e406ahrens * also implemented within this file since is requires manipulation of the
169fa9e406ahrens * namespace.
170fa9e406ahrens */
171fa9e406ahrens
172fa9e406ahrensstatic avl_tree_t spa_namespace_avl;
173fa9e406ahrenskmutex_t spa_namespace_lock;
174fa9e406ahrensstatic kcondvar_t spa_namespace_cv;
1750373e76bonwickstatic int spa_active_count;
176fa9e406ahrens
177fa9e406ahrenskmem_cache_t *spa_buffer_pool;
178fa9e406ahrensint spa_mode;
179fa9e406ahrens
180fa9e406ahrens#ifdef ZFS_DEBUG
181fa9e406ahrensint zfs_flags = ~0;
182fa9e406ahrens#else
183fa9e406ahrensint zfs_flags = 0;
184fa9e406ahrens#endif
185fa9e406ahrens
186fa9e406ahrens#define	SPA_MINREF	5	/* spa_refcnt for an open-but-idle pool */
187fa9e406ahrens
188fa9e406ahrens/*
189fa9e406ahrens * ==========================================================================
190fa9e406ahrens * SPA namespace functions
191fa9e406ahrens * ==========================================================================
192fa9e406ahrens */
193fa9e406ahrens
194fa9e406ahrens/*
195fa9e406ahrens * Lookup the named spa_t in the AVL tree.  The spa_namespace_lock must be held.
196fa9e406ahrens * Returns NULL if no matching spa_t is found.
197fa9e406ahrens */
198fa9e406ahrensspa_t *
199fa9e406ahrensspa_lookup(const char *name)
200fa9e406ahrens{
201fa9e406ahrens	spa_t search, *spa;
202fa9e406ahrens	avl_index_t where;
203fa9e406ahrens
204fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
205fa9e406ahrens
206fa9e406ahrens	search.spa_name = (char *)name;
207fa9e406ahrens	spa = avl_find(&spa_namespace_avl, &search, &where);
208fa9e406ahrens
209fa9e406ahrens	return (spa);
210fa9e406ahrens}
211fa9e406ahrens
212fa9e406ahrens/*
213fa9e406ahrens * Create an uninitialized spa_t with the given name.  Requires
214fa9e406ahrens * spa_namespace_lock.  The caller must ensure that the spa_t doesn't already
215fa9e406ahrens * exist by calling spa_lookup() first.
216fa9e406ahrens */
217fa9e406ahrensspa_t *
2180373e76bonwickspa_add(const char *name, const char *altroot)
219fa9e406ahrens{
220fa9e406ahrens	spa_t *spa;
221fa9e406ahrens
222fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
223fa9e406ahrens
224fa9e406ahrens	spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
225fa9e406ahrens
226fa9e406ahrens	spa->spa_name = spa_strdup(name);
227fa9e406ahrens	spa->spa_state = POOL_STATE_UNINITIALIZED;
228fa9e406ahrens	spa->spa_freeze_txg = UINT64_MAX;
2290373e76bonwick	spa->spa_final_txg = UINT64_MAX;
230fa9e406ahrens
231fa9e406ahrens	refcount_create(&spa->spa_refcount);
232ea8dc4beschrock	refcount_create(&spa->spa_config_lock.scl_count);
233fa9e406ahrens
234fa9e406ahrens	avl_add(&spa_namespace_avl, spa);
235fa9e406ahrens
2360373e76bonwick	/*
2370373e76bonwick	 * Set the alternate root, if there is one.
2380373e76bonwick	 */
2390373e76bonwick	if (altroot) {
2400373e76bonwick		spa->spa_root = spa_strdup(altroot);
2410373e76bonwick		spa_active_count++;
2420373e76bonwick	}
2430373e76bonwick
244fa9e406ahrens	return (spa);
245fa9e406ahrens}
246fa9e406ahrens
247fa9e406ahrens/*
248fa9e406ahrens * Removes a spa_t from the namespace, freeing up any memory used.  Requires
249fa9e406ahrens * spa_namespace_lock.  This is called only after the spa_t has been closed and
250fa9e406ahrens * deactivated.
251fa9e406ahrens */
252fa9e406ahrensvoid
253fa9e406ahrensspa_remove(spa_t *spa)
254fa9e406ahrens{
255fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
256fa9e406ahrens	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
257fa9e406ahrens	ASSERT(spa->spa_scrub_thread == NULL);
258fa9e406ahrens
259fa9e406ahrens	avl_remove(&spa_namespace_avl, spa);
260fa9e406ahrens	cv_broadcast(&spa_namespace_cv);
261fa9e406ahrens
2620373e76bonwick	if (spa->spa_root) {
263fa9e406ahrens		spa_strfree(spa->spa_root);
2640373e76bonwick		spa_active_count--;
2650373e76bonwick	}
266fa9e406ahrens
267fa9e406ahrens	if (spa->spa_name)
268fa9e406ahrens		spa_strfree(spa->spa_name);
269fa9e406ahrens
270fa9e406ahrens	spa_config_set(spa, NULL);
271fa9e406ahrens
272fa9e406ahrens	refcount_destroy(&spa->spa_refcount);
273ea8dc4beschrock	refcount_destroy(&spa->spa_config_lock.scl_count);
274fa9e406ahrens
275fa9e406ahrens	kmem_free(spa, sizeof (spa_t));
276fa9e406ahrens}
277fa9e406ahrens
278fa9e406ahrens/*
279fa9e406ahrens * Given a pool, return the next pool in the namespace, or NULL if there is
280fa9e406ahrens * none.  If 'prev' is NULL, return the first pool.
281fa9e406ahrens */
282fa9e406ahrensspa_t *
283fa9e406ahrensspa_next(spa_t *prev)
284fa9e406ahrens{
285fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
286fa9e406ahrens
287fa9e406ahrens	if (prev)
288fa9e406ahrens		return (AVL_NEXT(&spa_namespace_avl, prev));
289fa9e406ahrens	else
290fa9e406ahrens		return (avl_first(&spa_namespace_avl));
291fa9e406ahrens}
292fa9e406ahrens
293fa9e406ahrens/*
294fa9e406ahrens * ==========================================================================
295fa9e406ahrens * SPA refcount functions
296fa9e406ahrens * ==========================================================================
297fa9e406ahrens */
298fa9e406ahrens
299fa9e406ahrens/*
300fa9e406ahrens * Add a reference to the given spa_t.  Must have at least one reference, or
301fa9e406ahrens * have the namespace lock held.
302fa9e406ahrens */
303fa9e406ahrensvoid
304fa9e406ahrensspa_open_ref(spa_t *spa, void *tag)
305fa9e406ahrens{
306fa9e406ahrens	ASSERT(refcount_count(&spa->spa_refcount) > SPA_MINREF ||
307fa9e406ahrens	    MUTEX_HELD(&spa_namespace_lock));
308fa9e406ahrens
309fa9e406ahrens	(void) refcount_add(&spa->spa_refcount, tag);
310fa9e406ahrens}
311fa9e406ahrens
312fa9e406ahrens/*
313fa9e406ahrens * Remove a reference to the given spa_t.  Must have at least one reference, or
314fa9e406ahrens * have the namespace lock held.
315fa9e406ahrens */
316fa9e406ahrensvoid
317fa9e406ahrensspa_close(spa_t *spa, void *tag)
318fa9e406ahrens{
319fa9e406ahrens	ASSERT(refcount_count(&spa->spa_refcount) > SPA_MINREF ||
320fa9e406ahrens	    MUTEX_HELD(&spa_namespace_lock));
321fa9e406ahrens
322fa9e406ahrens	(void) refcount_remove(&spa->spa_refcount, tag);
323fa9e406ahrens}
324fa9e406ahrens
325fa9e406ahrens/*
326fa9e406ahrens * Check to see if the spa refcount is zero.  Must be called with
327fa9e406ahrens * spa_namespace_lock held.  We really compare against SPA_MINREF, which is the
328fa9e406ahrens * number of references acquired when opening a pool
329fa9e406ahrens */
330fa9e406ahrensboolean_t
331fa9e406ahrensspa_refcount_zero(spa_t *spa)
332fa9e406ahrens{
333fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
334fa9e406ahrens
335fa9e406ahrens	return (refcount_count(&spa->spa_refcount) == SPA_MINREF);
336fa9e406ahrens}
337fa9e406ahrens
338fa9e406ahrens/*
339fa9e406ahrens * ==========================================================================
340fa9e406ahrens * SPA config locking
341fa9e406ahrens * ==========================================================================
342fa9e406ahrens */
343fa9e406ahrens
344fa9e406ahrens/*
345fa9e406ahrens * Acquire the config lock.  The config lock is a special rwlock that allows for
346fa9e406ahrens * recursive enters.  Because these enters come from the same thread as well as
347fa9e406ahrens * asynchronous threads working on behalf of the owner, we must unilaterally
348fa9e406ahrens * allow all reads access as long at least one reader is held (even if a write
349fa9e406ahrens * is requested).  This has the side effect of write starvation, but write locks
350fa9e406ahrens * are extremely rare, and a solution to this problem would be significantly
351fa9e406ahrens * more complex (if even possible).
352fa9e406ahrens *
353fa9e406ahrens * We would like to assert that the namespace lock isn't held, but this is a
354fa9e406ahrens * valid use during create.
355fa9e406ahrens */
356fa9e406ahrensvoid
357ea8dc4beschrockspa_config_enter(spa_t *spa, krw_t rw, void *tag)
358fa9e406ahrens{
359fa9e406ahrens	spa_config_lock_t *scl = &spa->spa_config_lock;
360fa9e406ahrens
361fa9e406ahrens	mutex_enter(&scl->scl_lock);
362fa9e406ahrens
363fa9e406ahrens	if (scl->scl_writer != curthread) {
364fa9e406ahrens		if (rw == RW_READER) {
365fa9e406ahrens			while (scl->scl_writer != NULL)
366fa9e406ahrens				cv_wait(&scl->scl_cv, &scl->scl_lock);
367fa9e406ahrens		} else {
368ea8dc4beschrock			while (scl->scl_writer != NULL ||
369ea8dc4beschrock			    !refcount_is_zero(&scl->scl_count))
370fa9e406ahrens				cv_wait(&scl->scl_cv, &scl->scl_lock);
371fa9e406ahrens			scl->scl_writer = curthread;
372fa9e406ahrens		}
373fa9e406ahrens	}
374fa9e406ahrens
375ea8dc4beschrock	(void) refcount_add(&scl->scl_count, tag);
376fa9e406ahrens
377fa9e406ahrens	mutex_exit(&scl->scl_lock);
378fa9e406ahrens}
379fa9e406ahrens
380fa9e406ahrens/*
381fa9e406ahrens * Release the spa config lock, notifying any waiters in the process.
382fa9e406ahrens */
383fa9e406ahrensvoid
384ea8dc4beschrockspa_config_exit(spa_t *spa, void *tag)
385fa9e406ahrens{
386fa9e406ahrens	spa_config_lock_t *scl = &spa->spa_config_lock;
387fa9e406ahrens
388fa9e406ahrens	mutex_enter(&scl->scl_lock);
389fa9e406ahrens
390ea8dc4beschrock	ASSERT(!refcount_is_zero(&scl->scl_count));
391ea8dc4beschrock	if (refcount_remove(&scl->scl_count, tag) == 0) {
392fa9e406ahrens		cv_broadcast(&scl->scl_cv);
393fa9e406ahrens		scl->scl_writer = NULL;  /* OK in either case */
394fa9e406ahrens	}
395fa9e406ahrens
396fa9e406ahrens	mutex_exit(&scl->scl_lock);
397fa9e406ahrens}
398fa9e406ahrens
399fa9e406ahrens/*
400fa9e406ahrens * Returns true if the config lock is held in the given manner.
401fa9e406ahrens */
402fa9e406ahrensboolean_t
403fa9e406ahrensspa_config_held(spa_t *spa, krw_t rw)
404fa9e406ahrens{
405fa9e406ahrens	spa_config_lock_t *scl = &spa->spa_config_lock;
406fa9e406ahrens	boolean_t held;
407fa9e406ahrens
408fa9e406ahrens	mutex_enter(&scl->scl_lock);
409fa9e406ahrens	if (rw == RW_WRITER)
410fa9e406ahrens		held = (scl->scl_writer == curthread);
411fa9e406ahrens	else
412ea8dc4beschrock		held = !refcount_is_zero(&scl->scl_count);
413fa9e406ahrens	mutex_exit(&scl->scl_lock);
414fa9e406ahrens
415fa9e406ahrens	return (held);
416fa9e406ahrens}
417fa9e406ahrens
418fa9e406ahrens/*
419fa9e406ahrens * ==========================================================================
420fa9e406ahrens * SPA vdev locking
421fa9e406ahrens * ==========================================================================
422fa9e406ahrens */
423fa9e406ahrens
424fa9e406ahrens/*
425ea8dc4beschrock * Lock the given spa_t for the purpose of adding or removing a vdev.
426ea8dc4beschrock * Grabs the global spa_namespace_lock plus the spa config lock for writing.
427fa9e406ahrens * It returns the next transaction group for the spa_t.
428fa9e406ahrens */
429fa9e406ahrensuint64_t
430fa9e406ahrensspa_vdev_enter(spa_t *spa)
431fa9e406ahrens{
432ea8dc4beschrock	/*
433ea8dc4beschrock	 * Suspend scrub activity while we mess with the config.
434ea8dc4beschrock	 */
435ea8dc4beschrock	spa_scrub_suspend(spa);
436fa9e406ahrens
4370373e76bonwick	mutex_enter(&spa_namespace_lock);
438ea8dc4beschrock
439ea8dc4beschrock	spa_config_enter(spa, RW_WRITER, spa);
440fa9e406ahrens
441fa9e406ahrens	return (spa_last_synced_txg(spa) + 1);
442fa9e406ahrens}
443fa9e406ahrens
444fa9e406ahrens/*
445fa9e406ahrens * Unlock the spa_t after adding or removing a vdev.  Besides undoing the
446fa9e406ahrens * locking of spa_vdev_enter(), we also want make sure the transactions have
447fa9e406ahrens * synced to disk, and then update the global configuration cache with the new
448fa9e406ahrens * information.
449fa9e406ahrens */
450fa9e406ahrensint
451fa9e406ahrensspa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
452fa9e406ahrens{
4530e34b6abonwick	int config_changed = B_FALSE;
454ea8dc4beschrock
4550373e76bonwick	ASSERT(txg > spa_last_synced_txg(spa));
4560e34b6abonwick
4570e34b6abonwick	/*
4580e34b6abonwick	 * Reassess the DTLs.
4590e34b6abonwick	 */
4600373e76bonwick	vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE);
4610e34b6abonwick
4620e34b6abonwick	/*
4630373e76bonwick	 * If the config changed, notify the scrub thread that it must restart.
4640e34b6abonwick	 */
4650e34b6abonwick	if (error == 0 && !list_is_empty(&spa->spa_dirty_list)) {
4660e34b6abonwick		config_changed = B_TRUE;
4670373e76bonwick		spa_scrub_restart(spa, txg);
4680e34b6abonwick	}
469ea8dc4beschrock
470ea8dc4beschrock	spa_config_exit(spa, spa);
471fa9e406ahrens
472ea8dc4beschrock	/*
4735dabedebonwick	 * Allow scrubbing to resume.
474ea8dc4beschrock	 */
475ea8dc4beschrock	spa_scrub_resume(spa);
476fa9e406ahrens
477fa9e406ahrens	/*
478fa9e406ahrens	 * Note: this txg_wait_synced() is important because it ensures
479fa9e406ahrens	 * that there won't be more than one config change per txg.
480fa9e406ahrens	 * This allows us to use the txg as the generation number.
481fa9e406ahrens	 */
482fa9e406ahrens	if (error == 0)
483fa9e406ahrens		txg_wait_synced(spa->spa_dsl_pool, txg);
484fa9e406ahrens
485fa9e406ahrens	if (vd != NULL) {
486fa9e406ahrens		ASSERT(!vd->vdev_detached || vd->vdev_dtl.smo_object == 0);
487fa9e406ahrens		vdev_free(vd);
488fa9e406ahrens	}
489fa9e406ahrens
490fa9e406ahrens	/*
4910e34b6abonwick	 * If the config changed, update the config cache.
492fa9e406ahrens	 */
4930e34b6abonwick	if (config_changed)
494fa9e406ahrens		spa_config_sync();
495ea8dc4beschrock
496ea8dc4beschrock	mutex_exit(&spa_namespace_lock);
497fa9e406ahrens
498fa9e406ahrens	return (error);
499fa9e406ahrens}
500fa9e406ahrens
501fa9e406ahrens/*
502fa9e406ahrens * ==========================================================================
503fa9e406ahrens * Miscellaneous functions
504fa9e406ahrens * ==========================================================================
505fa9e406ahrens */
506fa9e406ahrens
507fa9e406ahrens/*
508fa9e406ahrens * Rename a spa_t.
509fa9e406ahrens */
510fa9e406ahrensint
511fa9e406ahrensspa_rename(const char *name, const char *newname)
512fa9e406ahrens{
513fa9e406ahrens	spa_t *spa;
514fa9e406ahrens	int err;
515fa9e406ahrens
516fa9e406ahrens	/*
517fa9e406ahrens	 * Lookup the spa_t and grab the config lock for writing.  We need to
518fa9e406ahrens	 * actually open the pool so that we can sync out the necessary labels.
519fa9e406ahrens	 * It's OK to call spa_open() with the namespace lock held because we
520ea8dc4beschrock	 * allow recursive calls for other reasons.
521fa9e406ahrens	 */
522fa9e406ahrens	mutex_enter(&spa_namespace_lock);
523fa9e406ahrens	if ((err = spa_open(name, &spa, FTAG)) != 0) {
524fa9e406ahrens		mutex_exit(&spa_namespace_lock);
525fa9e406ahrens		return (err);
526fa9e406ahrens	}
527fa9e406ahrens
528ea8dc4beschrock	spa_config_enter(spa, RW_WRITER, FTAG);
529fa9e406ahrens
530fa9e406ahrens	avl_remove(&spa_namespace_avl, spa);
531fa9e406ahrens	spa_strfree(spa->spa_name);
532fa9e406ahrens	spa->spa_name = spa_strdup(newname);
533fa9e406ahrens	avl_add(&spa_namespace_avl, spa);
534fa9e406ahrens
535fa9e406ahrens	/*
536fa9e406ahrens	 * Sync all labels to disk with the new names by marking the root vdev
537fa9e406ahrens	 * dirty and waiting for it to sync.  It will pick up the new pool name
538fa9e406ahrens	 * during the sync.
539fa9e406ahrens	 */
540fa9e406ahrens	vdev_config_dirty(spa->spa_root_vdev);
541fa9e406ahrens
542ea8dc4beschrock	spa_config_exit(spa, FTAG);
543fa9e406ahrens
5440373e76bonwick	txg_wait_synced(spa->spa_dsl_pool, 0);
545fa9e406ahrens
546fa9e406ahrens	/*
547fa9e406ahrens	 * Sync the updated config cache.
548fa9e406ahrens	 */
549fa9e406ahrens	spa_config_sync();
550fa9e406ahrens
551fa9e406ahrens	spa_close(spa, FTAG);
552fa9e406ahrens
553fa9e406ahrens	mutex_exit(&spa_namespace_lock);
554fa9e406ahrens
555fa9e406ahrens	return (0);
556fa9e406ahrens}
557fa9e406ahrens
558fa9e406ahrens
559fa9e406ahrens/*
560fa9e406ahrens * Determine whether a pool with given pool_guid exists.  If device_guid is
561fa9e406ahrens * non-zero, determine whether the pool exists *and* contains a device with the
562fa9e406ahrens * specified device_guid.
563fa9e406ahrens */
564fa9e406ahrensboolean_t
565fa9e406ahrensspa_guid_exists(uint64_t pool_guid, uint64_t device_guid)
566fa9e406ahrens{
567fa9e406ahrens	spa_t *spa;
568fa9e406ahrens	avl_tree_t *t = &spa_namespace_avl;
569fa9e406ahrens
570ea8dc4beschrock	ASSERT(MUTEX_HELD(&spa_namespace_lock));
571fa9e406ahrens
572fa9e406ahrens	for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) {
573fa9e406ahrens		if (spa->spa_state == POOL_STATE_UNINITIALIZED)
574fa9e406ahrens			continue;
575fa9e406ahrens		if (spa->spa_root_vdev == NULL)
576fa9e406ahrens			continue;
577fa9e406ahrens		if (spa_guid(spa) == pool_guid && (device_guid == 0 ||
578fa9e406ahrens		    vdev_lookup_by_guid(spa->spa_root_vdev, device_guid)))
579fa9e406ahrens			break;
580fa9e406ahrens	}
581fa9e406ahrens
582fa9e406ahrens	return (spa != NULL);
583fa9e406ahrens}
584fa9e406ahrens
585fa9e406ahrenschar *
586fa9e406ahrensspa_strdup(const char *s)
587fa9e406ahrens{
588fa9e406ahrens	size_t len;
589fa9e406ahrens	char *new;
590fa9e406ahrens
591fa9e406ahrens	len = strlen(s);
592fa9e406ahrens	new = kmem_alloc(len + 1, KM_SLEEP);
593fa9e406ahrens	bcopy(s, new, len);
594fa9e406ahrens	new[len] = '\0';
595fa9e406ahrens
596fa9e406ahrens	return (new);
597fa9e406ahrens}
598fa9e406ahrens
599fa9e406ahrensvoid
600fa9e406ahrensspa_strfree(char *s)
601fa9e406ahrens{
602fa9e406ahrens	kmem_free(s, strlen(s) + 1);
603fa9e406ahrens}
604fa9e406ahrens
605fa9e406ahrensuint64_t
606fa9e406ahrensspa_get_random(uint64_t range)
607fa9e406ahrens{
608fa9e406ahrens	uint64_t r;
609fa9e406ahrens
610fa9e406ahrens	ASSERT(range != 0);
611fa9e406ahrens
612fa9e406ahrens	(void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t));
613fa9e406ahrens
614fa9e406ahrens	return (r % range);
615fa9e406ahrens}
616fa9e406ahrens
617fa9e406ahrensvoid
618fbabab8maybeesprintf_blkptr(char *buf, int len, blkptr_t *bp)
619fa9e406ahrens{
620fa9e406ahrens	/* XXBP - Need to see if we want all DVAs or not */
621fa9e406ahrens	dva_t *dva = BP_IDENTITY(bp);
622fa9e406ahrens
623fa9e406ahrens	if (bp == NULL) {
624fbabab8maybee		(void) snprintf(buf, len, "<NULL>");
625fa9e406ahrens		return;
626fa9e406ahrens	}
627fa9e406ahrens
628fa9e406ahrens	if (BP_IS_HOLE(bp)) {
629fbabab8maybee		(void) snprintf(buf, len, "<hole>");
630fa9e406ahrens		return;
631fa9e406ahrens	}
632fa9e406ahrens
633fbabab8maybee	(void) snprintf(buf, len, "[L%llu %s] vdev=%llu offset=%llx "
634fbabab8maybee	    "size=%llxL/%llxP/%llxA %s %s %s %s "
635fbabab8maybee	    "birth=%llu fill=%llu cksum=%llx:%llx:%llx:%llx",
636fa9e406ahrens	    (u_longlong_t)BP_GET_LEVEL(bp),
637fa9e406ahrens	    dmu_ot[BP_GET_TYPE(bp)].ot_name,
638fa9e406ahrens	    (u_longlong_t)DVA_GET_VDEV(dva),
639fa9e406ahrens	    (u_longlong_t)DVA_GET_OFFSET(dva),
640fa9e406ahrens	    (u_longlong_t)BP_GET_LSIZE(bp),
641fa9e406ahrens	    (u_longlong_t)BP_GET_PSIZE(bp),
642fa9e406ahrens	    (u_longlong_t)DVA_GET_ASIZE(dva),
643fa9e406ahrens	    zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name,
644fa9e406ahrens	    zio_compress_table[BP_GET_COMPRESS(bp)].ci_name,
645fa9e406ahrens	    BP_GET_BYTEORDER(bp) == 0 ? "BE" : "LE",
646fbabab8maybee	    DVA_GET_GANG(dva) == 0 ? "contiguous" : "gang",
647fa9e406ahrens	    (u_longlong_t)bp->blk_birth,
648fa9e406ahrens	    (u_longlong_t)bp->blk_fill,
649fa9e406ahrens	    (u_longlong_t)bp->blk_cksum.zc_word[0],
650fa9e406ahrens	    (u_longlong_t)bp->blk_cksum.zc_word[1],
651fa9e406ahrens	    (u_longlong_t)bp->blk_cksum.zc_word[2],
652fa9e406ahrens	    (u_longlong_t)bp->blk_cksum.zc_word[3]);
653fa9e406ahrens}
654fa9e406ahrens
655fa9e406ahrensvoid
656fa9e406ahrensspa_freeze(spa_t *spa)
657fa9e406ahrens{
658fa9e406ahrens	uint64_t freeze_txg = 0;
659fa9e406ahrens
660ea8dc4beschrock	spa_config_enter(spa, RW_WRITER, FTAG);
661fa9e406ahrens	if (spa->spa_freeze_txg == UINT64_MAX) {
662fa9e406ahrens		freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE;
663fa9e406ahrens		spa->spa_freeze_txg = freeze_txg;
664fa9e406ahrens	}
665ea8dc4beschrock	spa_config_exit(spa, FTAG);
666fa9e406ahrens	if (freeze_txg != 0)
667fa9e406ahrens		txg_wait_synced(spa_get_dsl(spa), freeze_txg);
668fa9e406ahrens}
669fa9e406ahrens
670fa9e406ahrens/*
671fa9e406ahrens * ==========================================================================
672fa9e406ahrens * Accessor functions
673fa9e406ahrens * ==========================================================================
674fa9e406ahrens */
675fa9e406ahrens
676fa9e406ahrenskrwlock_t *
677fa9e406ahrensspa_traverse_rwlock(spa_t *spa)
678fa9e406ahrens{
679fa9e406ahrens	return (&spa->spa_traverse_lock);
680fa9e406ahrens}
681fa9e406ahrens
682fa9e406ahrensint
683fa9e406ahrensspa_traverse_wanted(spa_t *spa)
684fa9e406ahrens{
685fa9e406ahrens	return (spa->spa_traverse_wanted);
686fa9e406ahrens}
687fa9e406ahrens
688fa9e406ahrensdsl_pool_t *
689fa9e406ahrensspa_get_dsl(spa_t *spa)
690fa9e406ahrens{
691fa9e406ahrens	return (spa->spa_dsl_pool);
692fa9e406ahrens}
693fa9e406ahrens
694fa9e406ahrensblkptr_t *
695fa9e406ahrensspa_get_rootblkptr(spa_t *spa)
696fa9e406ahrens{
697fa9e406ahrens	return (&spa->spa_ubsync.ub_rootbp);
698fa9e406ahrens}
699fa9e406ahrens
700fa9e406ahrensvoid
701fa9e406ahrensspa_set_rootblkptr(spa_t *spa, const blkptr_t *bp)
702fa9e406ahrens{
703fa9e406ahrens	spa->spa_uberblock.ub_rootbp = *bp;
704fa9e406ahrens}
705fa9e406ahrens
706fa9e406ahrensvoid
707fa9e406ahrensspa_altroot(spa_t *spa, char *buf, size_t buflen)
708fa9e406ahrens{
709fa9e406ahrens	if (spa->spa_root == NULL)
710fa9e406ahrens		buf[0] = '\0';
711fa9e406ahrens	else
712fa9e406ahrens		(void) strncpy(buf, spa->spa_root, buflen);
713fa9e406ahrens}
714fa9e406ahrens
715fa9e406ahrensint
716fa9e406ahrensspa_sync_pass(spa_t *spa)
717fa9e406ahrens{
718fa9e406ahrens	return (spa->spa_sync_pass);
719fa9e406ahrens}
720fa9e406ahrens
721fa9e406ahrenschar *
722fa9e406ahrensspa_name(spa_t *spa)
723fa9e406ahrens{
724fa9e406ahrens	/*
725fa9e406ahrens	 * Accessing the name requires holding either the namespace lock or the
726fa9e406ahrens	 * config lock, both of which are required to do a rename.
727fa9e406ahrens	 */
728fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock) ||
729fa9e406ahrens	    spa_config_held(spa, RW_READER) || spa_config_held(spa, RW_WRITER));
730fa9e406ahrens
731fa9e406ahrens	return (spa->spa_name);
732fa9e406ahrens}
733fa9e406ahrens
734fa9e406ahrensuint64_t
735fa9e406ahrensspa_guid(spa_t *spa)
736fa9e406ahrens{
737fa9e406ahrens	return (spa->spa_root_vdev->vdev_guid);
738fa9e406ahrens}
739fa9e406ahrens
740fa9e406ahrensuint64_t
741fa9e406ahrensspa_last_synced_txg(spa_t *spa)
742fa9e406ahrens{
743fa9e406ahrens	return (spa->spa_ubsync.ub_txg);
744fa9e406ahrens}
745fa9e406ahrens
746fa9e406ahrensuint64_t
747fa9e406ahrensspa_first_txg(spa_t *spa)
748fa9e406ahrens{
749fa9e406ahrens	return (spa->spa_first_txg);
750fa9e406ahrens}
751fa9e406ahrens
752fa9e406ahrensint
753fa9e406ahrensspa_state(spa_t *spa)
754fa9e406ahrens{
755fa9e406ahrens	return (spa->spa_state);
756fa9e406ahrens}
757fa9e406ahrens
758fa9e406ahrensuint64_t
759fa9e406ahrensspa_freeze_txg(spa_t *spa)
760fa9e406ahrens{
761fa9e406ahrens	return (spa->spa_freeze_txg);
762fa9e406ahrens}
763fa9e406ahrens
764fa9e406ahrens/*
765fa9e406ahrens * In the future, this may select among different metaslab classes
766fa9e406ahrens * depending on the zdp.  For now, there's no such distinction.
767fa9e406ahrens */
768fa9e406ahrensmetaslab_class_t *
769fa9e406ahrensspa_metaslab_class_select(spa_t *spa)
770fa9e406ahrens{
771fa9e406ahrens	return (spa->spa_normal_class);
772fa9e406ahrens}
773fa9e406ahrens
774fa9e406ahrens/*
775fa9e406ahrens * Return pool-wide allocated space.
776fa9e406ahrens */
777fa9e406ahrensuint64_t
778fa9e406ahrensspa_get_alloc(spa_t *spa)
779fa9e406ahrens{
780fa9e406ahrens	return (spa->spa_root_vdev->vdev_stat.vs_alloc);
781fa9e406ahrens}
782fa9e406ahrens
783fa9e406ahrens/*
784fa9e406ahrens * Return pool-wide allocated space.
785fa9e406ahrens */
786fa9e406ahrensuint64_t
787fa9e406ahrensspa_get_space(spa_t *spa)
788fa9e406ahrens{
789fa9e406ahrens	return (spa->spa_root_vdev->vdev_stat.vs_space);
790fa9e406ahrens}
791fa9e406ahrens
792fa9e406ahrens/* ARGSUSED */
793fa9e406ahrensuint64_t
794fa9e406ahrensspa_get_asize(spa_t *spa, uint64_t lsize)
795fa9e406ahrens{
796fa9e406ahrens	/*
797fa9e406ahrens	 * For now, the worst case is 512-byte RAID-Z blocks, in which
798fa9e406ahrens	 * case the space requirement is exactly 2x; so just assume that.
799fa9e406ahrens	 */
800fa9e406ahrens	return (lsize << 1);
801fa9e406ahrens}
802fa9e406ahrens
803fa9e406ahrens/*
804fa9e406ahrens * ==========================================================================
805fa9e406ahrens * Initialization and Termination
806fa9e406ahrens * ==========================================================================
807fa9e406ahrens */
808fa9e406ahrens
809fa9e406ahrensstatic int
810fa9e406ahrensspa_name_compare(const void *a1, const void *a2)
811fa9e406ahrens{
812fa9e406ahrens	const spa_t *s1 = a1;
813fa9e406ahrens	const spa_t *s2 = a2;
814fa9e406ahrens	int s;
815fa9e406ahrens
816fa9e406ahrens	s = strcmp(s1->spa_name, s2->spa_name);
817fa9e406ahrens	if (s > 0)
818fa9e406ahrens		return (1);
819fa9e406ahrens	if (s < 0)
820fa9e406ahrens		return (-1);
821fa9e406ahrens	return (0);
822fa9e406ahrens}
823fa9e406ahrens
8240373e76bonwickint
8250373e76bonwickspa_busy(void)
8260373e76bonwick{
8270373e76bonwick	return (spa_active_count);
8280373e76bonwick}
8290373e76bonwick
830fa9e406ahrensvoid
831fa9e406ahrensspa_init(int mode)
832fa9e406ahrens{
833fa9e406ahrens	mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL);
834fa9e406ahrens	cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL);
835fa9e406ahrens
836fa9e406ahrens	avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t),
837fa9e406ahrens	    offsetof(spa_t, spa_avl));
838fa9e406ahrens
839fa9e406ahrens	spa_mode = mode;
840fa9e406ahrens
841fa9e406ahrens	refcount_init();
842fa9e406ahrens	unique_init();
843fa9e406ahrens	zio_init();
844fa9e406ahrens	dmu_init();
845fa9e406ahrens	zil_init();
846fa9e406ahrens	spa_config_load();
847fa9e406ahrens}
848fa9e406ahrens
849fa9e406ahrensvoid
850fa9e406ahrensspa_fini(void)
851fa9e406ahrens{
852fa9e406ahrens	spa_evict_all();
853fa9e406ahrens
854fa9e406ahrens	zil_fini();
855fa9e406ahrens	dmu_fini();
856fa9e406ahrens	zio_fini();
857fa9e406ahrens	refcount_fini();
858fa9e406ahrens
859fa9e406ahrens	avl_destroy(&spa_namespace_avl);
860fa9e406ahrens
861fa9e406ahrens	cv_destroy(&spa_namespace_cv);
862fa9e406ahrens	mutex_destroy(&spa_namespace_lock);
863fa9e406ahrens}
864