spa_misc.c revision fa9e4066f08beec538e775443c5be79dd423fcab
1fa9e406ahrens/*
2fa9e406ahrens * CDDL HEADER START
3fa9e406ahrens *
4fa9e406ahrens * The contents of this file are subject to the terms of the
5fa9e406ahrens * Common Development and Distribution License, Version 1.0 only
6fa9e406ahrens * (the "License").  You may not use this file except in compliance
7fa9e406ahrens * with the License.
8fa9e406ahrens *
9fa9e406ahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10fa9e406ahrens * or http://www.opensolaris.org/os/licensing.
11fa9e406ahrens * See the License for the specific language governing permissions
12fa9e406ahrens * and limitations under the License.
13fa9e406ahrens *
14fa9e406ahrens * When distributing Covered Code, include this CDDL HEADER in each
15fa9e406ahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16fa9e406ahrens * If applicable, add the following below this CDDL HEADER, with the
17fa9e406ahrens * fields enclosed by brackets "[]" replaced with your own identifying
18fa9e406ahrens * information: Portions Copyright [yyyy] [name of copyright owner]
19fa9e406ahrens *
20fa9e406ahrens * CDDL HEADER END
21fa9e406ahrens */
22fa9e406ahrens/*
23fa9e406ahrens * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24fa9e406ahrens * Use is subject to license terms.
25fa9e406ahrens */
26fa9e406ahrens
27fa9e406ahrens#pragma ident	"%Z%%M%	%I%	%E% SMI"
28fa9e406ahrens
29fa9e406ahrens#include <sys/zfs_context.h>
30fa9e406ahrens#include <sys/spa_impl.h>
31fa9e406ahrens#include <sys/zio.h>
32fa9e406ahrens#include <sys/zio_checksum.h>
33fa9e406ahrens#include <sys/zio_compress.h>
34fa9e406ahrens#include <sys/dmu.h>
35fa9e406ahrens#include <sys/dmu_tx.h>
36fa9e406ahrens#include <sys/zap.h>
37fa9e406ahrens#include <sys/zil.h>
38fa9e406ahrens#include <sys/vdev_impl.h>
39fa9e406ahrens#include <sys/metaslab.h>
40fa9e406ahrens#include <sys/uberblock_impl.h>
41fa9e406ahrens#include <sys/txg.h>
42fa9e406ahrens#include <sys/avl.h>
43fa9e406ahrens#include <sys/unique.h>
44fa9e406ahrens#include <sys/dsl_pool.h>
45fa9e406ahrens#include <sys/dsl_dir.h>
46fa9e406ahrens#include <sys/dsl_prop.h>
47fa9e406ahrens#include <sys/fs/zfs.h>
48fa9e406ahrens
49fa9e406ahrens/*
50fa9e406ahrens * SPA locking
51fa9e406ahrens *
52fa9e406ahrens * There are four basic locks for managing spa_t structures:
53fa9e406ahrens *
54fa9e406ahrens * spa_namespace_lock (global mutex)
55fa9e406ahrens *
56fa9e406ahrens * 	This lock must be acquired to do any of the following:
57fa9e406ahrens *
58fa9e406ahrens * 		- Lookup a spa_t by name
59fa9e406ahrens * 		- Add or remove a spa_t from the namespace
60fa9e406ahrens * 		- Increase spa_refcount from non-zero
61fa9e406ahrens * 		- Check if spa_refcount is zero
62fa9e406ahrens * 		- Rename a spa_t
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 * spa_vdev_lock (global mutex)
95fa9e406ahrens *
96fa9e406ahrens * 	This special lock is a global mutex used to serialize attempts to
97fa9e406ahrens * 	access devices through ZFS.  It makes sure that we do not try to add
98fa9e406ahrens * 	a single vdev to multiple pools at the same time.  It must be held
99fa9e406ahrens * 	when adding or removing a device from the pool.
100fa9e406ahrens *
101fa9e406ahrens *
102fa9e406ahrens * The locking order is fairly straightforward:
103fa9e406ahrens *
104fa9e406ahrens * 		spa_namespace_lock	->	spa_refcount
105fa9e406ahrens *
106fa9e406ahrens * 	The namespace lock must be acquired to increase the refcount from 0
107fa9e406ahrens * 	or to check if it is zero.
108fa9e406ahrens *
109fa9e406ahrens * 		spa_refcount 		->	spa_config_lock
110fa9e406ahrens *
111fa9e406ahrens * 	There must be at least one valid reference on the spa_t to acquire
112fa9e406ahrens * 	the config lock.
113fa9e406ahrens *
114fa9e406ahrens * 		spa_vdev_lock		->	spa_config_lock
115fa9e406ahrens *
116fa9e406ahrens * 	There are no locks required for spa_vdev_lock, but it must be
117fa9e406ahrens * 	acquired before spa_config_lock.
118fa9e406ahrens *
119fa9e406ahrens *
120fa9e406ahrens * The spa_namespace_lock and spa_config_cache_lock can be acquired directly and
121fa9e406ahrens * are globally visible.
122fa9e406ahrens *
123fa9e406ahrens * The namespace is manipulated using the following functions, all which require
124fa9e406ahrens * the spa_namespace_lock to be held.
125fa9e406ahrens *
126fa9e406ahrens * 	spa_lookup()		Lookup a spa_t by name.
127fa9e406ahrens *
128fa9e406ahrens * 	spa_add()		Create a new spa_t in the namespace.
129fa9e406ahrens *
130fa9e406ahrens * 	spa_remove()		Remove a spa_t from the namespace.  This also
131fa9e406ahrens * 				frees up any memory associated with the spa_t.
132fa9e406ahrens *
133fa9e406ahrens * 	spa_next()		Returns the next spa_t in the system, or the
134fa9e406ahrens * 				first if NULL is passed.
135fa9e406ahrens *
136fa9e406ahrens * 	spa_evict_all()		Shutdown and remove all spa_t structures in
137fa9e406ahrens * 				the system.
138fa9e406ahrens *
139fa9e406ahrens *
140fa9e406ahrens * The spa_refcount is manipulated using the following functions:
141fa9e406ahrens *
142fa9e406ahrens * 	spa_open_ref()		Adds a reference to the given spa_t.  Must be
143fa9e406ahrens * 				called with spa_namespace_lock held if the
144fa9e406ahrens * 				refcount is currently zero.
145fa9e406ahrens *
146fa9e406ahrens * 	spa_close()		Remove a reference from the spa_t.  This will
147fa9e406ahrens * 				not free the spa_t or remove it from the
148fa9e406ahrens * 				namespace.  No locking is required.
149fa9e406ahrens *
150fa9e406ahrens * 	spa_refcount_zero()	Returns true if the refcount is currently
151fa9e406ahrens * 				zero.  Must be called with spa_namespace_lock
152fa9e406ahrens * 				held.
153fa9e406ahrens *
154fa9e406ahrens * The spa_config_lock is manipulated using the following functions:
155fa9e406ahrens *
156fa9e406ahrens * 	spa_config_enter()	Acquire the config lock as RW_READER or
157fa9e406ahrens * 				RW_WRITER.  At least one reference on the spa_t
158fa9e406ahrens * 				must exist.
159fa9e406ahrens *
160fa9e406ahrens * 	spa_config_exit()	Release the config lock.
161fa9e406ahrens *
162fa9e406ahrens * 	spa_config_held()	Returns true if the config lock is currently
163fa9e406ahrens * 				held in the given state.
164fa9e406ahrens *
165fa9e406ahrens * The spa_vdev_lock, while acquired directly, is hidden by the following
166fa9e406ahrens * functions, which imply additional semantics that must be followed:
167fa9e406ahrens *
168fa9e406ahrens * 	spa_vdev_enter()	Acquire the vdev lock and the config lock for
169fa9e406ahrens * 				writing.
170fa9e406ahrens *
171fa9e406ahrens * 	spa_vdev_exit()		Release the config lock, wait for all I/O
172fa9e406ahrens * 				to complete, release the vdev lock, and sync
173fa9e406ahrens * 				the updated configs to the cache.
174fa9e406ahrens *
175fa9e406ahrens * The spa_name() function also requires either the spa_namespace_lock
176fa9e406ahrens * or the spa_config_lock, as both are needed to do a rename.  spa_rename() is
177fa9e406ahrens * also implemented within this file since is requires manipulation of the
178fa9e406ahrens * namespace.
179fa9e406ahrens */
180fa9e406ahrens
181fa9e406ahrensstatic avl_tree_t spa_namespace_avl;
182fa9e406ahrenskmutex_t spa_namespace_lock;
183fa9e406ahrensstatic kcondvar_t spa_namespace_cv;
184fa9e406ahrens
185fa9e406ahrenskmem_cache_t *spa_buffer_pool;
186fa9e406ahrensint spa_mode;
187fa9e406ahrens
188fa9e406ahrens#ifdef ZFS_DEBUG
189fa9e406ahrensint zfs_flags = ~0;
190fa9e406ahrens#else
191fa9e406ahrensint zfs_flags = 0;
192fa9e406ahrens#endif
193fa9e406ahrens
194fa9e406ahrensstatic kmutex_t spa_vdev_lock;
195fa9e406ahrens
196fa9e406ahrens#define	SPA_MINREF	5	/* spa_refcnt for an open-but-idle pool */
197fa9e406ahrens
198fa9e406ahrens/*
199fa9e406ahrens * ==========================================================================
200fa9e406ahrens * SPA namespace functions
201fa9e406ahrens * ==========================================================================
202fa9e406ahrens */
203fa9e406ahrens
204fa9e406ahrens/*
205fa9e406ahrens * Lookup the named spa_t in the AVL tree.  The spa_namespace_lock must be held.
206fa9e406ahrens * Returns NULL if no matching spa_t is found.
207fa9e406ahrens */
208fa9e406ahrensspa_t *
209fa9e406ahrensspa_lookup(const char *name)
210fa9e406ahrens{
211fa9e406ahrens	spa_t search, *spa;
212fa9e406ahrens	avl_index_t where;
213fa9e406ahrens
214fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
215fa9e406ahrens
216fa9e406ahrens	search.spa_name = (char *)name;
217fa9e406ahrens	spa = avl_find(&spa_namespace_avl, &search, &where);
218fa9e406ahrens
219fa9e406ahrens	return (spa);
220fa9e406ahrens}
221fa9e406ahrens
222fa9e406ahrens/*
223fa9e406ahrens * Create an uninitialized spa_t with the given name.  Requires
224fa9e406ahrens * spa_namespace_lock.  The caller must ensure that the spa_t doesn't already
225fa9e406ahrens * exist by calling spa_lookup() first.
226fa9e406ahrens */
227fa9e406ahrensspa_t *
228fa9e406ahrensspa_add(const char *name)
229fa9e406ahrens{
230fa9e406ahrens	spa_t *spa;
231fa9e406ahrens
232fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
233fa9e406ahrens
234fa9e406ahrens	spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
235fa9e406ahrens
236fa9e406ahrens	spa->spa_name = spa_strdup(name);
237fa9e406ahrens	spa->spa_state = POOL_STATE_UNINITIALIZED;
238fa9e406ahrens	spa->spa_freeze_txg = UINT64_MAX;
239fa9e406ahrens
240fa9e406ahrens	refcount_create(&spa->spa_refcount);
241fa9e406ahrens
242fa9e406ahrens	avl_add(&spa_namespace_avl, spa);
243fa9e406ahrens
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
262fa9e406ahrens	if (spa->spa_root)
263fa9e406ahrens		spa_strfree(spa->spa_root);
264fa9e406ahrens
265fa9e406ahrens	if (spa->spa_name)
266fa9e406ahrens		spa_strfree(spa->spa_name);
267fa9e406ahrens
268fa9e406ahrens	spa_config_set(spa, NULL);
269fa9e406ahrens
270fa9e406ahrens	refcount_destroy(&spa->spa_refcount);
271fa9e406ahrens
272fa9e406ahrens	kmem_free(spa, sizeof (spa_t));
273fa9e406ahrens}
274fa9e406ahrens
275fa9e406ahrens/*
276fa9e406ahrens * Given a pool, return the next pool in the namespace, or NULL if there is
277fa9e406ahrens * none.  If 'prev' is NULL, return the first pool.
278fa9e406ahrens */
279fa9e406ahrensspa_t *
280fa9e406ahrensspa_next(spa_t *prev)
281fa9e406ahrens{
282fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
283fa9e406ahrens
284fa9e406ahrens	if (prev)
285fa9e406ahrens		return (AVL_NEXT(&spa_namespace_avl, prev));
286fa9e406ahrens	else
287fa9e406ahrens		return (avl_first(&spa_namespace_avl));
288fa9e406ahrens}
289fa9e406ahrens
290fa9e406ahrens/*
291fa9e406ahrens * ==========================================================================
292fa9e406ahrens * SPA refcount functions
293fa9e406ahrens * ==========================================================================
294fa9e406ahrens */
295fa9e406ahrens
296fa9e406ahrens/*
297fa9e406ahrens * Add a reference to the given spa_t.  Must have at least one reference, or
298fa9e406ahrens * have the namespace lock held.
299fa9e406ahrens */
300fa9e406ahrensvoid
301fa9e406ahrensspa_open_ref(spa_t *spa, void *tag)
302fa9e406ahrens{
303fa9e406ahrens	ASSERT(refcount_count(&spa->spa_refcount) > SPA_MINREF ||
304fa9e406ahrens	    MUTEX_HELD(&spa_namespace_lock));
305fa9e406ahrens
306fa9e406ahrens	(void) refcount_add(&spa->spa_refcount, tag);
307fa9e406ahrens}
308fa9e406ahrens
309fa9e406ahrens/*
310fa9e406ahrens * Remove a reference to the given spa_t.  Must have at least one reference, or
311fa9e406ahrens * have the namespace lock held.
312fa9e406ahrens */
313fa9e406ahrensvoid
314fa9e406ahrensspa_close(spa_t *spa, void *tag)
315fa9e406ahrens{
316fa9e406ahrens	ASSERT(refcount_count(&spa->spa_refcount) > SPA_MINREF ||
317fa9e406ahrens	    MUTEX_HELD(&spa_namespace_lock));
318fa9e406ahrens
319fa9e406ahrens	(void) refcount_remove(&spa->spa_refcount, tag);
320fa9e406ahrens}
321fa9e406ahrens
322fa9e406ahrens/*
323fa9e406ahrens * Check to see if the spa refcount is zero.  Must be called with
324fa9e406ahrens * spa_namespace_lock held.  We really compare against SPA_MINREF, which is the
325fa9e406ahrens * number of references acquired when opening a pool
326fa9e406ahrens */
327fa9e406ahrensboolean_t
328fa9e406ahrensspa_refcount_zero(spa_t *spa)
329fa9e406ahrens{
330fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
331fa9e406ahrens
332fa9e406ahrens	return (refcount_count(&spa->spa_refcount) == SPA_MINREF);
333fa9e406ahrens}
334fa9e406ahrens
335fa9e406ahrens/*
336fa9e406ahrens * ==========================================================================
337fa9e406ahrens * SPA config locking
338fa9e406ahrens * ==========================================================================
339fa9e406ahrens */
340fa9e406ahrens
341fa9e406ahrens/*
342fa9e406ahrens * Acquire the config lock.  The config lock is a special rwlock that allows for
343fa9e406ahrens * recursive enters.  Because these enters come from the same thread as well as
344fa9e406ahrens * asynchronous threads working on behalf of the owner, we must unilaterally
345fa9e406ahrens * allow all reads access as long at least one reader is held (even if a write
346fa9e406ahrens * is requested).  This has the side effect of write starvation, but write locks
347fa9e406ahrens * are extremely rare, and a solution to this problem would be significantly
348fa9e406ahrens * more complex (if even possible).
349fa9e406ahrens *
350fa9e406ahrens * We would like to assert that the namespace lock isn't held, but this is a
351fa9e406ahrens * valid use during create.
352fa9e406ahrens */
353fa9e406ahrensvoid
354fa9e406ahrensspa_config_enter(spa_t *spa, krw_t rw)
355fa9e406ahrens{
356fa9e406ahrens	spa_config_lock_t *scl = &spa->spa_config_lock;
357fa9e406ahrens
358fa9e406ahrens	mutex_enter(&scl->scl_lock);
359fa9e406ahrens
360fa9e406ahrens	if (scl->scl_writer != curthread) {
361fa9e406ahrens		if (rw == RW_READER) {
362fa9e406ahrens			while (scl->scl_writer != NULL)
363fa9e406ahrens				cv_wait(&scl->scl_cv, &scl->scl_lock);
364fa9e406ahrens		} else {
365fa9e406ahrens			while (scl->scl_writer != NULL || scl->scl_count > 0)
366fa9e406ahrens				cv_wait(&scl->scl_cv, &scl->scl_lock);
367fa9e406ahrens			scl->scl_writer = curthread;
368fa9e406ahrens		}
369fa9e406ahrens	}
370fa9e406ahrens
371fa9e406ahrens	scl->scl_count++;
372fa9e406ahrens
373fa9e406ahrens	mutex_exit(&scl->scl_lock);
374fa9e406ahrens}
375fa9e406ahrens
376fa9e406ahrens/*
377fa9e406ahrens * Release the spa config lock, notifying any waiters in the process.
378fa9e406ahrens */
379fa9e406ahrensvoid
380fa9e406ahrensspa_config_exit(spa_t *spa)
381fa9e406ahrens{
382fa9e406ahrens	spa_config_lock_t *scl = &spa->spa_config_lock;
383fa9e406ahrens
384fa9e406ahrens	mutex_enter(&scl->scl_lock);
385fa9e406ahrens
386fa9e406ahrens	ASSERT(scl->scl_count > 0);
387fa9e406ahrens	if (--scl->scl_count == 0) {
388fa9e406ahrens		cv_broadcast(&scl->scl_cv);
389fa9e406ahrens		scl->scl_writer = NULL;  /* OK in either case */
390fa9e406ahrens	}
391fa9e406ahrens
392fa9e406ahrens	mutex_exit(&scl->scl_lock);
393fa9e406ahrens}
394fa9e406ahrens
395fa9e406ahrens/*
396fa9e406ahrens * Returns true if the config lock is held in the given manner.
397fa9e406ahrens */
398fa9e406ahrensboolean_t
399fa9e406ahrensspa_config_held(spa_t *spa, krw_t rw)
400fa9e406ahrens{
401fa9e406ahrens	spa_config_lock_t *scl = &spa->spa_config_lock;
402fa9e406ahrens	boolean_t held;
403fa9e406ahrens
404fa9e406ahrens	mutex_enter(&scl->scl_lock);
405fa9e406ahrens	if (rw == RW_WRITER)
406fa9e406ahrens		held = (scl->scl_writer == curthread);
407fa9e406ahrens	else
408fa9e406ahrens		held = (scl->scl_count != 0);
409fa9e406ahrens	mutex_exit(&scl->scl_lock);
410fa9e406ahrens
411fa9e406ahrens	return (held);
412fa9e406ahrens}
413fa9e406ahrens
414fa9e406ahrens/*
415fa9e406ahrens * ==========================================================================
416fa9e406ahrens * SPA vdev locking
417fa9e406ahrens * ==========================================================================
418fa9e406ahrens */
419fa9e406ahrens
420fa9e406ahrens/*
421fa9e406ahrens * Lock the given spa_t for the purpose of adding or removing a vdev.  This
422fa9e406ahrens * grabs the global spa_vdev_lock as well as the spa config lock for writing.
423fa9e406ahrens * It returns the next transaction group for the spa_t.
424fa9e406ahrens */
425fa9e406ahrensuint64_t
426fa9e406ahrensspa_vdev_enter(spa_t *spa)
427fa9e406ahrens{
428fa9e406ahrens	mutex_enter(&spa_vdev_lock);
429fa9e406ahrens
430fa9e406ahrens	spa_config_enter(spa, RW_WRITER);
431fa9e406ahrens
432fa9e406ahrens	return (spa_last_synced_txg(spa) + 1);
433fa9e406ahrens}
434fa9e406ahrens
435fa9e406ahrens/*
436fa9e406ahrens * Unlock the spa_t after adding or removing a vdev.  Besides undoing the
437fa9e406ahrens * locking of spa_vdev_enter(), we also want make sure the transactions have
438fa9e406ahrens * synced to disk, and then update the global configuration cache with the new
439fa9e406ahrens * information.
440fa9e406ahrens */
441fa9e406ahrensint
442fa9e406ahrensspa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
443fa9e406ahrens{
444fa9e406ahrens	vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE);
445fa9e406ahrens
446fa9e406ahrens	spa_config_exit(spa);
447fa9e406ahrens
448fa9e406ahrens	if (vd == spa->spa_root_vdev) {		/* spa_create() */
449fa9e406ahrens		mutex_exit(&spa_vdev_lock);
450fa9e406ahrens		return (error);
451fa9e406ahrens	}
452fa9e406ahrens
453fa9e406ahrens	/*
454fa9e406ahrens	 * Note: this txg_wait_synced() is important because it ensures
455fa9e406ahrens	 * that there won't be more than one config change per txg.
456fa9e406ahrens	 * This allows us to use the txg as the generation number.
457fa9e406ahrens	 */
458fa9e406ahrens	if (error == 0)
459fa9e406ahrens		txg_wait_synced(spa->spa_dsl_pool, txg);
460fa9e406ahrens
461fa9e406ahrens	mutex_exit(&spa_vdev_lock);
462fa9e406ahrens
463fa9e406ahrens	if (vd != NULL) {
464fa9e406ahrens		ASSERT(!vd->vdev_detached || vd->vdev_dtl.smo_object == 0);
465fa9e406ahrens		vdev_free(vd);
466fa9e406ahrens	}
467fa9e406ahrens
468fa9e406ahrens	/*
469fa9e406ahrens	 * If we're in the middle of export or destroy, don't sync the
470fa9e406ahrens	 * config -- it will do that anyway, and we deadlock if we try.
471fa9e406ahrens	 */
472fa9e406ahrens	if (error == 0 && spa->spa_state == POOL_STATE_ACTIVE) {
473fa9e406ahrens		mutex_enter(&spa_namespace_lock);
474fa9e406ahrens		spa_config_sync();
475fa9e406ahrens		mutex_exit(&spa_namespace_lock);
476fa9e406ahrens	}
477fa9e406ahrens
478fa9e406ahrens	return (error);
479fa9e406ahrens}
480fa9e406ahrens
481fa9e406ahrens/*
482fa9e406ahrens * ==========================================================================
483fa9e406ahrens * Miscellaneous functions
484fa9e406ahrens * ==========================================================================
485fa9e406ahrens */
486fa9e406ahrens
487fa9e406ahrens/*
488fa9e406ahrens * Rename a spa_t.
489fa9e406ahrens */
490fa9e406ahrensint
491fa9e406ahrensspa_rename(const char *name, const char *newname)
492fa9e406ahrens{
493fa9e406ahrens	spa_t *spa;
494fa9e406ahrens	int err;
495fa9e406ahrens
496fa9e406ahrens	/*
497fa9e406ahrens	 * Lookup the spa_t and grab the config lock for writing.  We need to
498fa9e406ahrens	 * actually open the pool so that we can sync out the necessary labels.
499fa9e406ahrens	 * It's OK to call spa_open() with the namespace lock held because we
500fa9e406ahrens	 * alllow recursive calls for other reasons.
501fa9e406ahrens	 */
502fa9e406ahrens	mutex_enter(&spa_namespace_lock);
503fa9e406ahrens	if ((err = spa_open(name, &spa, FTAG)) != 0) {
504fa9e406ahrens		mutex_exit(&spa_namespace_lock);
505fa9e406ahrens		return (err);
506fa9e406ahrens	}
507fa9e406ahrens
508fa9e406ahrens	spa_config_enter(spa, RW_WRITER);
509fa9e406ahrens
510fa9e406ahrens	avl_remove(&spa_namespace_avl, spa);
511fa9e406ahrens	spa_strfree(spa->spa_name);
512fa9e406ahrens	spa->spa_name = spa_strdup(newname);
513fa9e406ahrens	avl_add(&spa_namespace_avl, spa);
514fa9e406ahrens
515fa9e406ahrens	/*
516fa9e406ahrens	 * Sync all labels to disk with the new names by marking the root vdev
517fa9e406ahrens	 * dirty and waiting for it to sync.  It will pick up the new pool name
518fa9e406ahrens	 * during the sync.
519fa9e406ahrens	 */
520fa9e406ahrens	vdev_config_dirty(spa->spa_root_vdev);
521fa9e406ahrens
522fa9e406ahrens	spa_config_exit(spa);
523fa9e406ahrens
524fa9e406ahrens	txg_wait_synced(spa->spa_dsl_pool, 0);
525fa9e406ahrens
526fa9e406ahrens	/*
527fa9e406ahrens	 * Sync the updated config cache.
528fa9e406ahrens	 */
529fa9e406ahrens	spa_config_set(spa,
530fa9e406ahrens	    spa_config_generate(spa, NULL, spa_last_synced_txg(spa), 0));
531fa9e406ahrens	spa_config_sync();
532fa9e406ahrens
533fa9e406ahrens	spa_close(spa, FTAG);
534fa9e406ahrens
535fa9e406ahrens	mutex_exit(&spa_namespace_lock);
536fa9e406ahrens
537fa9e406ahrens	return (0);
538fa9e406ahrens}
539fa9e406ahrens
540fa9e406ahrens
541fa9e406ahrens/*
542fa9e406ahrens * Determine whether a pool with given pool_guid exists.  If device_guid is
543fa9e406ahrens * non-zero, determine whether the pool exists *and* contains a device with the
544fa9e406ahrens * specified device_guid.
545fa9e406ahrens */
546fa9e406ahrensboolean_t
547fa9e406ahrensspa_guid_exists(uint64_t pool_guid, uint64_t device_guid)
548fa9e406ahrens{
549fa9e406ahrens	spa_t *spa;
550fa9e406ahrens	avl_tree_t *t = &spa_namespace_avl;
551fa9e406ahrens	boolean_t locked = B_FALSE;
552fa9e406ahrens
553fa9e406ahrens	if (mutex_owner(&spa_namespace_lock) != curthread) {
554fa9e406ahrens		mutex_enter(&spa_namespace_lock);
555fa9e406ahrens		locked = B_TRUE;
556fa9e406ahrens	}
557fa9e406ahrens
558fa9e406ahrens	for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) {
559fa9e406ahrens		if (spa->spa_state == POOL_STATE_UNINITIALIZED)
560fa9e406ahrens			continue;
561fa9e406ahrens		if (spa->spa_root_vdev == NULL)
562fa9e406ahrens			continue;
563fa9e406ahrens		if (spa_guid(spa) == pool_guid && (device_guid == 0 ||
564fa9e406ahrens		    vdev_lookup_by_guid(spa->spa_root_vdev, device_guid)))
565fa9e406ahrens			break;
566fa9e406ahrens	}
567fa9e406ahrens
568fa9e406ahrens	if (locked)
569fa9e406ahrens		mutex_exit(&spa_namespace_lock);
570fa9e406ahrens
571fa9e406ahrens	return (spa != NULL);
572fa9e406ahrens}
573fa9e406ahrens
574fa9e406ahrenschar *
575fa9e406ahrensspa_strdup(const char *s)
576fa9e406ahrens{
577fa9e406ahrens	size_t len;
578fa9e406ahrens	char *new;
579fa9e406ahrens
580fa9e406ahrens	len = strlen(s);
581fa9e406ahrens	new = kmem_alloc(len + 1, KM_SLEEP);
582fa9e406ahrens	bcopy(s, new, len);
583fa9e406ahrens	new[len] = '\0';
584fa9e406ahrens
585fa9e406ahrens	return (new);
586fa9e406ahrens}
587fa9e406ahrens
588fa9e406ahrensvoid
589fa9e406ahrensspa_strfree(char *s)
590fa9e406ahrens{
591fa9e406ahrens	kmem_free(s, strlen(s) + 1);
592fa9e406ahrens}
593fa9e406ahrens
594fa9e406ahrensuint64_t
595fa9e406ahrensspa_get_random(uint64_t range)
596fa9e406ahrens{
597fa9e406ahrens	uint64_t r;
598fa9e406ahrens
599fa9e406ahrens	ASSERT(range != 0);
600fa9e406ahrens
601fa9e406ahrens	(void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t));
602fa9e406ahrens
603fa9e406ahrens	return (r % range);
604fa9e406ahrens}
605fa9e406ahrens
606fa9e406ahrensvoid
607fa9e406ahrenssprintf_blkptr(char *buf, blkptr_t *bp)
608fa9e406ahrens{
609fa9e406ahrens	/* XXBP - Need to see if we want all DVAs or not */
610fa9e406ahrens	dva_t *dva = BP_IDENTITY(bp);
611fa9e406ahrens
612fa9e406ahrens	if (bp == NULL) {
613fa9e406ahrens		(void) sprintf(buf, "<NULL>");
614fa9e406ahrens		return;
615fa9e406ahrens	}
616fa9e406ahrens
617fa9e406ahrens	if (BP_IS_HOLE(bp)) {
618fa9e406ahrens		(void) sprintf(buf, "<hole>");
619fa9e406ahrens		return;
620fa9e406ahrens	}
621fa9e406ahrens
622fa9e406ahrens	(void) sprintf(buf, "[L%llu %s] vdev=%llu offset=%llx "
623fa9e406ahrens	    "size=%llxL/%llxP/%llxA %s %s %s %s",
624fa9e406ahrens	    (u_longlong_t)BP_GET_LEVEL(bp),
625fa9e406ahrens	    dmu_ot[BP_GET_TYPE(bp)].ot_name,
626fa9e406ahrens	    (u_longlong_t)DVA_GET_VDEV(dva),
627fa9e406ahrens	    (u_longlong_t)DVA_GET_OFFSET(dva),
628fa9e406ahrens	    (u_longlong_t)BP_GET_LSIZE(bp),
629fa9e406ahrens	    (u_longlong_t)BP_GET_PSIZE(bp),
630fa9e406ahrens	    (u_longlong_t)DVA_GET_ASIZE(dva),
631fa9e406ahrens	    zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name,
632fa9e406ahrens	    zio_compress_table[BP_GET_COMPRESS(bp)].ci_name,
633fa9e406ahrens	    BP_GET_BYTEORDER(bp) == 0 ? "BE" : "LE",
634fa9e406ahrens	    DVA_GET_GANG(dva) == 0 ? "contiguous" : "gang");
635fa9e406ahrens
636fa9e406ahrens	(void) sprintf(buf + strlen(buf), " birth=%llu fill=%llu"
637fa9e406ahrens	    " cksum=%llx:%llx:%llx:%llx",
638fa9e406ahrens	    (u_longlong_t)bp->blk_birth,
639fa9e406ahrens	    (u_longlong_t)bp->blk_fill,
640fa9e406ahrens	    (u_longlong_t)bp->blk_cksum.zc_word[0],
641fa9e406ahrens	    (u_longlong_t)bp->blk_cksum.zc_word[1],
642fa9e406ahrens	    (u_longlong_t)bp->blk_cksum.zc_word[2],
643fa9e406ahrens	    (u_longlong_t)bp->blk_cksum.zc_word[3]);
644fa9e406ahrens}
645fa9e406ahrens
646fa9e406ahrensvoid
647fa9e406ahrensspa_freeze(spa_t *spa)
648fa9e406ahrens{
649fa9e406ahrens	uint64_t freeze_txg = 0;
650fa9e406ahrens
651fa9e406ahrens	spa_config_enter(spa, RW_WRITER);
652fa9e406ahrens	if (spa->spa_freeze_txg == UINT64_MAX) {
653fa9e406ahrens		freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE;
654fa9e406ahrens		spa->spa_freeze_txg = freeze_txg;
655fa9e406ahrens	}
656fa9e406ahrens	spa_config_exit(spa);
657fa9e406ahrens	if (freeze_txg != 0)
658fa9e406ahrens		txg_wait_synced(spa_get_dsl(spa), freeze_txg);
659fa9e406ahrens}
660fa9e406ahrens
661fa9e406ahrens/*
662fa9e406ahrens * ==========================================================================
663fa9e406ahrens * Accessor functions
664fa9e406ahrens * ==========================================================================
665fa9e406ahrens */
666fa9e406ahrens
667fa9e406ahrenskrwlock_t *
668fa9e406ahrensspa_traverse_rwlock(spa_t *spa)
669fa9e406ahrens{
670fa9e406ahrens	return (&spa->spa_traverse_lock);
671fa9e406ahrens}
672fa9e406ahrens
673fa9e406ahrensint
674fa9e406ahrensspa_traverse_wanted(spa_t *spa)
675fa9e406ahrens{
676fa9e406ahrens	return (spa->spa_traverse_wanted);
677fa9e406ahrens}
678fa9e406ahrens
679fa9e406ahrensdsl_pool_t *
680fa9e406ahrensspa_get_dsl(spa_t *spa)
681fa9e406ahrens{
682fa9e406ahrens	return (spa->spa_dsl_pool);
683fa9e406ahrens}
684fa9e406ahrens
685fa9e406ahrensblkptr_t *
686fa9e406ahrensspa_get_rootblkptr(spa_t *spa)
687fa9e406ahrens{
688fa9e406ahrens	return (&spa->spa_ubsync.ub_rootbp);
689fa9e406ahrens}
690fa9e406ahrens
691fa9e406ahrensvoid
692fa9e406ahrensspa_set_rootblkptr(spa_t *spa, const blkptr_t *bp)
693fa9e406ahrens{
694fa9e406ahrens	spa->spa_uberblock.ub_rootbp = *bp;
695fa9e406ahrens}
696fa9e406ahrens
697fa9e406ahrensvoid
698fa9e406ahrensspa_altroot(spa_t *spa, char *buf, size_t buflen)
699fa9e406ahrens{
700fa9e406ahrens	if (spa->spa_root == NULL)
701fa9e406ahrens		buf[0] = '\0';
702fa9e406ahrens	else
703fa9e406ahrens		(void) strncpy(buf, spa->spa_root, buflen);
704fa9e406ahrens}
705fa9e406ahrens
706fa9e406ahrensint
707fa9e406ahrensspa_sync_pass(spa_t *spa)
708fa9e406ahrens{
709fa9e406ahrens	return (spa->spa_sync_pass);
710fa9e406ahrens}
711fa9e406ahrens
712fa9e406ahrenschar *
713fa9e406ahrensspa_name(spa_t *spa)
714fa9e406ahrens{
715fa9e406ahrens	/*
716fa9e406ahrens	 * Accessing the name requires holding either the namespace lock or the
717fa9e406ahrens	 * config lock, both of which are required to do a rename.
718fa9e406ahrens	 */
719fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock) ||
720fa9e406ahrens	    spa_config_held(spa, RW_READER) || spa_config_held(spa, RW_WRITER));
721fa9e406ahrens
722fa9e406ahrens	return (spa->spa_name);
723fa9e406ahrens}
724fa9e406ahrens
725fa9e406ahrensuint64_t
726fa9e406ahrensspa_guid(spa_t *spa)
727fa9e406ahrens{
728fa9e406ahrens	return (spa->spa_root_vdev->vdev_guid);
729fa9e406ahrens}
730fa9e406ahrens
731fa9e406ahrensuint64_t
732fa9e406ahrensspa_last_synced_txg(spa_t *spa)
733fa9e406ahrens{
734fa9e406ahrens	return (spa->spa_ubsync.ub_txg);
735fa9e406ahrens}
736fa9e406ahrens
737fa9e406ahrensuint64_t
738fa9e406ahrensspa_first_txg(spa_t *spa)
739fa9e406ahrens{
740fa9e406ahrens	return (spa->spa_first_txg);
741fa9e406ahrens}
742fa9e406ahrens
743fa9e406ahrensint
744fa9e406ahrensspa_state(spa_t *spa)
745fa9e406ahrens{
746fa9e406ahrens	return (spa->spa_state);
747fa9e406ahrens}
748fa9e406ahrens
749fa9e406ahrensuint64_t
750fa9e406ahrensspa_freeze_txg(spa_t *spa)
751fa9e406ahrens{
752fa9e406ahrens	return (spa->spa_freeze_txg);
753fa9e406ahrens}
754fa9e406ahrens
755fa9e406ahrens/*
756fa9e406ahrens * In the future, this may select among different metaslab classes
757fa9e406ahrens * depending on the zdp.  For now, there's no such distinction.
758fa9e406ahrens */
759fa9e406ahrensmetaslab_class_t *
760fa9e406ahrensspa_metaslab_class_select(spa_t *spa)
761fa9e406ahrens{
762fa9e406ahrens	return (spa->spa_normal_class);
763fa9e406ahrens}
764fa9e406ahrens
765fa9e406ahrens/*
766fa9e406ahrens * Return pool-wide allocated space.
767fa9e406ahrens */
768fa9e406ahrensuint64_t
769fa9e406ahrensspa_get_alloc(spa_t *spa)
770fa9e406ahrens{
771fa9e406ahrens	return (spa->spa_root_vdev->vdev_stat.vs_alloc);
772fa9e406ahrens}
773fa9e406ahrens
774fa9e406ahrens/*
775fa9e406ahrens * Return pool-wide allocated space.
776fa9e406ahrens */
777fa9e406ahrensuint64_t
778fa9e406ahrensspa_get_space(spa_t *spa)
779fa9e406ahrens{
780fa9e406ahrens	return (spa->spa_root_vdev->vdev_stat.vs_space);
781fa9e406ahrens}
782fa9e406ahrens
783fa9e406ahrens/* ARGSUSED */
784fa9e406ahrensuint64_t
785fa9e406ahrensspa_get_asize(spa_t *spa, uint64_t lsize)
786fa9e406ahrens{
787fa9e406ahrens	/*
788fa9e406ahrens	 * For now, the worst case is 512-byte RAID-Z blocks, in which
789fa9e406ahrens	 * case the space requirement is exactly 2x; so just assume that.
790fa9e406ahrens	 */
791fa9e406ahrens	return (lsize << 1);
792fa9e406ahrens}
793fa9e406ahrens
794fa9e406ahrens/*
795fa9e406ahrens * ==========================================================================
796fa9e406ahrens * Initialization and Termination
797fa9e406ahrens * ==========================================================================
798fa9e406ahrens */
799fa9e406ahrens
800fa9e406ahrensstatic int
801fa9e406ahrensspa_name_compare(const void *a1, const void *a2)
802fa9e406ahrens{
803fa9e406ahrens	const spa_t *s1 = a1;
804fa9e406ahrens	const spa_t *s2 = a2;
805fa9e406ahrens	int s;
806fa9e406ahrens
807fa9e406ahrens	s = strcmp(s1->spa_name, s2->spa_name);
808fa9e406ahrens	if (s > 0)
809fa9e406ahrens		return (1);
810fa9e406ahrens	if (s < 0)
811fa9e406ahrens		return (-1);
812fa9e406ahrens	return (0);
813fa9e406ahrens}
814fa9e406ahrens
815fa9e406ahrensvoid
816fa9e406ahrensspa_init(int mode)
817fa9e406ahrens{
818fa9e406ahrens	mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL);
819fa9e406ahrens	cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL);
820fa9e406ahrens
821fa9e406ahrens	avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t),
822fa9e406ahrens	    offsetof(spa_t, spa_avl));
823fa9e406ahrens
824fa9e406ahrens	spa_mode = mode;
825fa9e406ahrens
826fa9e406ahrens	refcount_init();
827fa9e406ahrens	unique_init();
828fa9e406ahrens	zio_init();
829fa9e406ahrens	dmu_init();
830fa9e406ahrens	zil_init();
831fa9e406ahrens	spa_config_load();
832fa9e406ahrens}
833fa9e406ahrens
834fa9e406ahrensvoid
835fa9e406ahrensspa_fini(void)
836fa9e406ahrens{
837fa9e406ahrens	spa_evict_all();
838fa9e406ahrens
839fa9e406ahrens	zil_fini();
840fa9e406ahrens	dmu_fini();
841fa9e406ahrens	zio_fini();
842fa9e406ahrens	refcount_fini();
843fa9e406ahrens
844fa9e406ahrens	avl_destroy(&spa_namespace_avl);
845fa9e406ahrens
846fa9e406ahrens	cv_destroy(&spa_namespace_cv);
847fa9e406ahrens	mutex_destroy(&spa_namespace_lock);
848fa9e406ahrens}
849