spa_misc.c revision 44cd46cadd9aab751dae6a4023c1cb5bf316d274
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 *
5544cd46cbillm *	This lock must be acquired to do any of the following:
56fa9e406ahrens *
5744cd46cbillm *		- Lookup a spa_t by name
5844cd46cbillm *		- Add or remove a spa_t from the namespace
5944cd46cbillm *		- Increase spa_refcount from non-zero
6044cd46cbillm *		- Check if spa_refcount is zero
6144cd46cbillm *		- Rename a spa_t
62ea8dc4beschrock *		- add/remove/attach/detach devices
6344cd46cbillm *		- Held for the duration of create/destroy/import/export
64fa9e406ahrens *
6544cd46cbillm *	It does not need to handle recursion.  A create or destroy may
6644cd46cbillm *	reference objects (files or zvols) in other pools, but by
6744cd46cbillm *	definition they must have an existing reference, and will never need
6844cd46cbillm *	to lookup a spa_t by name.
69fa9e406ahrens *
70fa9e406ahrens * spa_refcount (per-spa refcount_t protected by mutex)
71fa9e406ahrens *
7244cd46cbillm *	This reference count keep track of any active users of the spa_t.  The
7344cd46cbillm *	spa_t cannot be destroyed or freed while this is non-zero.  Internally,
7444cd46cbillm *	the refcount is never really 'zero' - opening a pool implicitly keeps
7544cd46cbillm *	some references in the DMU.  Internally we check against SPA_MINREF, but
7644cd46cbillm *	present the image of a zero/non-zero value to consumers.
77fa9e406ahrens *
78fa9e406ahrens * spa_config_lock (per-spa crazy rwlock)
79fa9e406ahrens *
8044cd46cbillm *	This SPA special is a recursive rwlock, capable of being acquired from
8144cd46cbillm *	asynchronous threads.  It has protects the spa_t from config changes,
8244cd46cbillm *	and must be held in the following circumstances:
83fa9e406ahrens *
8444cd46cbillm *		- RW_READER to perform I/O to the spa
8544cd46cbillm *		- RW_WRITER to change the vdev config
86fa9e406ahrens *
87fa9e406ahrens * spa_config_cache_lock (per-spa mutex)
88fa9e406ahrens *
8944cd46cbillm *	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 *
9644cd46cbillm *		spa_namespace_lock	->	spa_refcount
97fa9e406ahrens *
9844cd46cbillm *	The namespace lock must be acquired to increase the refcount from 0
9944cd46cbillm *	or to check if it is zero.
100fa9e406ahrens *
10144cd46cbillm *		spa_refcount		->	spa_config_lock
102fa9e406ahrens *
10344cd46cbillm *	There must be at least one valid reference on the spa_t to acquire
10444cd46cbillm *	the config lock.
105fa9e406ahrens *
10644cd46cbillm *		spa_namespace_lock	->	spa_config_lock
107fa9e406ahrens *
10844cd46cbillm *	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 *
11744cd46cbillm *	spa_lookup()		Lookup a spa_t by name.
118fa9e406ahrens *
11944cd46cbillm *	spa_add()		Create a new spa_t in the namespace.
120fa9e406ahrens *
12144cd46cbillm *	spa_remove()		Remove a spa_t from the namespace.  This also
12244cd46cbillm *				frees up any memory associated with the spa_t.
123fa9e406ahrens *
12444cd46cbillm *	spa_next()		Returns the next spa_t in the system, or the
12544cd46cbillm *				first if NULL is passed.
126fa9e406ahrens *
12744cd46cbillm *	spa_evict_all()		Shutdown and remove all spa_t structures in
12844cd46cbillm *				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 *
13444cd46cbillm *	spa_open_ref()		Adds a reference to the given spa_t.  Must be
13544cd46cbillm *				called with spa_namespace_lock held if the
13644cd46cbillm *				refcount is currently zero.
137fa9e406ahrens *
13844cd46cbillm *	spa_close()		Remove a reference from the spa_t.  This will
13944cd46cbillm *				not free the spa_t or remove it from the
14044cd46cbillm *				namespace.  No locking is required.
141fa9e406ahrens *
14244cd46cbillm *	spa_refcount_zero()	Returns true if the refcount is currently
14344cd46cbillm *				zero.  Must be called with spa_namespace_lock
14444cd46cbillm *				held.
145fa9e406ahrens *
146fa9e406ahrens * The spa_config_lock is manipulated using the following functions:
147fa9e406ahrens *
14844cd46cbillm *	spa_config_enter()	Acquire the config lock as RW_READER or
14944cd46cbillm *				RW_WRITER.  At least one reference on the spa_t
15044cd46cbillm *				must exist.
151fa9e406ahrens *
15244cd46cbillm *	spa_config_exit()	Release the config lock.
153fa9e406ahrens *
15444cd46cbillm *	spa_config_held()	Returns true if the config lock is currently
15544cd46cbillm *				held in the given state.
156fa9e406ahrens *
157ea8dc4beschrock * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
158fa9e406ahrens *
15944cd46cbillm *	spa_vdev_enter()	Acquire the namespace lock and the config lock
160ea8dc4beschrock *				for writing.
161fa9e406ahrens *
16244cd46cbillm *	spa_vdev_exit()		Release the config lock, wait for all I/O
16344cd46cbillm *				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;
17644cd46cbillmstatic int spa_max_replication_override = SPA_DVAS_PER_BP;
177fa9e406ahrens
178fa9e406ahrenskmem_cache_t *spa_buffer_pool;
179fa9e406ahrensint spa_mode;
180fa9e406ahrens
181fa9e406ahrens#ifdef ZFS_DEBUG
182fa9e406ahrensint zfs_flags = ~0;
183fa9e406ahrens#else
184fa9e406ahrensint zfs_flags = 0;
185fa9e406ahrens#endif
186fa9e406ahrens
187fa9e406ahrens#define	SPA_MINREF	5	/* spa_refcnt for an open-but-idle pool */
188fa9e406ahrens
189fa9e406ahrens/*
190fa9e406ahrens * ==========================================================================
191fa9e406ahrens * SPA namespace functions
192fa9e406ahrens * ==========================================================================
193fa9e406ahrens */
194fa9e406ahrens
195fa9e406ahrens/*
196fa9e406ahrens * Lookup the named spa_t in the AVL tree.  The spa_namespace_lock must be held.
197fa9e406ahrens * Returns NULL if no matching spa_t is found.
198fa9e406ahrens */
199fa9e406ahrensspa_t *
200fa9e406ahrensspa_lookup(const char *name)
201fa9e406ahrens{
202fa9e406ahrens	spa_t search, *spa;
203fa9e406ahrens	avl_index_t where;
204fa9e406ahrens
205fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
206fa9e406ahrens
207fa9e406ahrens	search.spa_name = (char *)name;
208fa9e406ahrens	spa = avl_find(&spa_namespace_avl, &search, &where);
209fa9e406ahrens
210fa9e406ahrens	return (spa);
211fa9e406ahrens}
212fa9e406ahrens
213fa9e406ahrens/*
214fa9e406ahrens * Create an uninitialized spa_t with the given name.  Requires
215fa9e406ahrens * spa_namespace_lock.  The caller must ensure that the spa_t doesn't already
216fa9e406ahrens * exist by calling spa_lookup() first.
217fa9e406ahrens */
218fa9e406ahrensspa_t *
2190373e76bonwickspa_add(const char *name, const char *altroot)
220fa9e406ahrens{
221fa9e406ahrens	spa_t *spa;
222fa9e406ahrens
223fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
224fa9e406ahrens
225fa9e406ahrens	spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
226fa9e406ahrens
227fa9e406ahrens	spa->spa_name = spa_strdup(name);
228fa9e406ahrens	spa->spa_state = POOL_STATE_UNINITIALIZED;
229fa9e406ahrens	spa->spa_freeze_txg = UINT64_MAX;
2300373e76bonwick	spa->spa_final_txg = UINT64_MAX;
231fa9e406ahrens
232fa9e406ahrens	refcount_create(&spa->spa_refcount);
233ea8dc4beschrock	refcount_create(&spa->spa_config_lock.scl_count);
234fa9e406ahrens
235fa9e406ahrens	avl_add(&spa_namespace_avl, spa);
236fa9e406ahrens
2370373e76bonwick	/*
2380373e76bonwick	 * Set the alternate root, if there is one.
2390373e76bonwick	 */
2400373e76bonwick	if (altroot) {
2410373e76bonwick		spa->spa_root = spa_strdup(altroot);
2420373e76bonwick		spa_active_count++;
2430373e76bonwick	}
2440373e76bonwick
245fa9e406ahrens	return (spa);
246fa9e406ahrens}
247fa9e406ahrens
248fa9e406ahrens/*
249fa9e406ahrens * Removes a spa_t from the namespace, freeing up any memory used.  Requires
250fa9e406ahrens * spa_namespace_lock.  This is called only after the spa_t has been closed and
251fa9e406ahrens * deactivated.
252fa9e406ahrens */
253fa9e406ahrensvoid
254fa9e406ahrensspa_remove(spa_t *spa)
255fa9e406ahrens{
256fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
257fa9e406ahrens	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
258fa9e406ahrens	ASSERT(spa->spa_scrub_thread == NULL);
259fa9e406ahrens
260fa9e406ahrens	avl_remove(&spa_namespace_avl, spa);
261fa9e406ahrens	cv_broadcast(&spa_namespace_cv);
262fa9e406ahrens
2630373e76bonwick	if (spa->spa_root) {
264fa9e406ahrens		spa_strfree(spa->spa_root);
2650373e76bonwick		spa_active_count--;
2660373e76bonwick	}
267fa9e406ahrens
268fa9e406ahrens	if (spa->spa_name)
269fa9e406ahrens		spa_strfree(spa->spa_name);
270fa9e406ahrens
271fa9e406ahrens	spa_config_set(spa, NULL);
272fa9e406ahrens
273fa9e406ahrens	refcount_destroy(&spa->spa_refcount);
274ea8dc4beschrock	refcount_destroy(&spa->spa_config_lock.scl_count);
275fa9e406ahrens
276fa9e406ahrens	kmem_free(spa, sizeof (spa_t));
277fa9e406ahrens}
278fa9e406ahrens
279fa9e406ahrens/*
280fa9e406ahrens * Given a pool, return the next pool in the namespace, or NULL if there is
281fa9e406ahrens * none.  If 'prev' is NULL, return the first pool.
282fa9e406ahrens */
283fa9e406ahrensspa_t *
284fa9e406ahrensspa_next(spa_t *prev)
285fa9e406ahrens{
286fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
287fa9e406ahrens
288fa9e406ahrens	if (prev)
289fa9e406ahrens		return (AVL_NEXT(&spa_namespace_avl, prev));
290fa9e406ahrens	else
291fa9e406ahrens		return (avl_first(&spa_namespace_avl));
292fa9e406ahrens}
293fa9e406ahrens
294fa9e406ahrens/*
295fa9e406ahrens * ==========================================================================
296fa9e406ahrens * SPA refcount functions
297fa9e406ahrens * ==========================================================================
298fa9e406ahrens */
299fa9e406ahrens
300fa9e406ahrens/*
301fa9e406ahrens * Add a reference to the given spa_t.  Must have at least one reference, or
302fa9e406ahrens * have the namespace lock held.
303fa9e406ahrens */
304fa9e406ahrensvoid
305fa9e406ahrensspa_open_ref(spa_t *spa, void *tag)
306fa9e406ahrens{
307fa9e406ahrens	ASSERT(refcount_count(&spa->spa_refcount) > SPA_MINREF ||
308fa9e406ahrens	    MUTEX_HELD(&spa_namespace_lock));
309fa9e406ahrens
310fa9e406ahrens	(void) refcount_add(&spa->spa_refcount, tag);
311fa9e406ahrens}
312fa9e406ahrens
313fa9e406ahrens/*
314fa9e406ahrens * Remove a reference to the given spa_t.  Must have at least one reference, or
315fa9e406ahrens * have the namespace lock held.
316fa9e406ahrens */
317fa9e406ahrensvoid
318fa9e406ahrensspa_close(spa_t *spa, void *tag)
319fa9e406ahrens{
320fa9e406ahrens	ASSERT(refcount_count(&spa->spa_refcount) > SPA_MINREF ||
321fa9e406ahrens	    MUTEX_HELD(&spa_namespace_lock));
322fa9e406ahrens
323fa9e406ahrens	(void) refcount_remove(&spa->spa_refcount, tag);
324fa9e406ahrens}
325fa9e406ahrens
326fa9e406ahrens/*
327fa9e406ahrens * Check to see if the spa refcount is zero.  Must be called with
328fa9e406ahrens * spa_namespace_lock held.  We really compare against SPA_MINREF, which is the
329fa9e406ahrens * number of references acquired when opening a pool
330fa9e406ahrens */
331fa9e406ahrensboolean_t
332fa9e406ahrensspa_refcount_zero(spa_t *spa)
333fa9e406ahrens{
334fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
335fa9e406ahrens
336fa9e406ahrens	return (refcount_count(&spa->spa_refcount) == SPA_MINREF);
337fa9e406ahrens}
338fa9e406ahrens
339fa9e406ahrens/*
340fa9e406ahrens * ==========================================================================
341fa9e406ahrens * SPA config locking
342fa9e406ahrens * ==========================================================================
343fa9e406ahrens */
344fa9e406ahrens
345fa9e406ahrens/*
346fa9e406ahrens * Acquire the config lock.  The config lock is a special rwlock that allows for
347fa9e406ahrens * recursive enters.  Because these enters come from the same thread as well as
348fa9e406ahrens * asynchronous threads working on behalf of the owner, we must unilaterally
349fa9e406ahrens * allow all reads access as long at least one reader is held (even if a write
350fa9e406ahrens * is requested).  This has the side effect of write starvation, but write locks
351fa9e406ahrens * are extremely rare, and a solution to this problem would be significantly
352fa9e406ahrens * more complex (if even possible).
353fa9e406ahrens *
354fa9e406ahrens * We would like to assert that the namespace lock isn't held, but this is a
355fa9e406ahrens * valid use during create.
356fa9e406ahrens */
357fa9e406ahrensvoid
358ea8dc4beschrockspa_config_enter(spa_t *spa, krw_t rw, void *tag)
359fa9e406ahrens{
360fa9e406ahrens	spa_config_lock_t *scl = &spa->spa_config_lock;
361fa9e406ahrens
362fa9e406ahrens	mutex_enter(&scl->scl_lock);
363fa9e406ahrens
364fa9e406ahrens	if (scl->scl_writer != curthread) {
365fa9e406ahrens		if (rw == RW_READER) {
366fa9e406ahrens			while (scl->scl_writer != NULL)
367fa9e406ahrens				cv_wait(&scl->scl_cv, &scl->scl_lock);
368fa9e406ahrens		} else {
369ea8dc4beschrock			while (scl->scl_writer != NULL ||
370ea8dc4beschrock			    !refcount_is_zero(&scl->scl_count))
371fa9e406ahrens				cv_wait(&scl->scl_cv, &scl->scl_lock);
372fa9e406ahrens			scl->scl_writer = curthread;
373fa9e406ahrens		}
374fa9e406ahrens	}
375fa9e406ahrens
376ea8dc4beschrock	(void) refcount_add(&scl->scl_count, tag);
377fa9e406ahrens
378fa9e406ahrens	mutex_exit(&scl->scl_lock);
379fa9e406ahrens}
380fa9e406ahrens
381fa9e406ahrens/*
382fa9e406ahrens * Release the spa config lock, notifying any waiters in the process.
383fa9e406ahrens */
384fa9e406ahrensvoid
385ea8dc4beschrockspa_config_exit(spa_t *spa, void *tag)
386fa9e406ahrens{
387fa9e406ahrens	spa_config_lock_t *scl = &spa->spa_config_lock;
388fa9e406ahrens
389fa9e406ahrens	mutex_enter(&scl->scl_lock);
390fa9e406ahrens
391ea8dc4beschrock	ASSERT(!refcount_is_zero(&scl->scl_count));
392ea8dc4beschrock	if (refcount_remove(&scl->scl_count, tag) == 0) {
393fa9e406ahrens		cv_broadcast(&scl->scl_cv);
394fa9e406ahrens		scl->scl_writer = NULL;  /* OK in either case */
395fa9e406ahrens	}
396fa9e406ahrens
397fa9e406ahrens	mutex_exit(&scl->scl_lock);
398fa9e406ahrens}
399fa9e406ahrens
400fa9e406ahrens/*
401fa9e406ahrens * Returns true if the config lock is held in the given manner.
402fa9e406ahrens */
403fa9e406ahrensboolean_t
404fa9e406ahrensspa_config_held(spa_t *spa, krw_t rw)
405fa9e406ahrens{
406fa9e406ahrens	spa_config_lock_t *scl = &spa->spa_config_lock;
407fa9e406ahrens	boolean_t held;
408fa9e406ahrens
409fa9e406ahrens	mutex_enter(&scl->scl_lock);
410fa9e406ahrens	if (rw == RW_WRITER)
411fa9e406ahrens		held = (scl->scl_writer == curthread);
412fa9e406ahrens	else
413ea8dc4beschrock		held = !refcount_is_zero(&scl->scl_count);
414fa9e406ahrens	mutex_exit(&scl->scl_lock);
415fa9e406ahrens
416fa9e406ahrens	return (held);
417fa9e406ahrens}
418fa9e406ahrens
419fa9e406ahrens/*
420fa9e406ahrens * ==========================================================================
421fa9e406ahrens * SPA vdev locking
422fa9e406ahrens * ==========================================================================
423fa9e406ahrens */
424fa9e406ahrens
425fa9e406ahrens/*
426ea8dc4beschrock * Lock the given spa_t for the purpose of adding or removing a vdev.
427ea8dc4beschrock * Grabs the global spa_namespace_lock plus the spa config lock for writing.
428fa9e406ahrens * It returns the next transaction group for the spa_t.
429fa9e406ahrens */
430fa9e406ahrensuint64_t
431fa9e406ahrensspa_vdev_enter(spa_t *spa)
432fa9e406ahrens{
433ea8dc4beschrock	/*
434ea8dc4beschrock	 * Suspend scrub activity while we mess with the config.
435ea8dc4beschrock	 */
436ea8dc4beschrock	spa_scrub_suspend(spa);
437fa9e406ahrens
4380373e76bonwick	mutex_enter(&spa_namespace_lock);
439ea8dc4beschrock
440ea8dc4beschrock	spa_config_enter(spa, RW_WRITER, spa);
441fa9e406ahrens
442fa9e406ahrens	return (spa_last_synced_txg(spa) + 1);
443fa9e406ahrens}
444fa9e406ahrens
445fa9e406ahrens/*
446fa9e406ahrens * Unlock the spa_t after adding or removing a vdev.  Besides undoing the
447fa9e406ahrens * locking of spa_vdev_enter(), we also want make sure the transactions have
448fa9e406ahrens * synced to disk, and then update the global configuration cache with the new
449fa9e406ahrens * information.
450fa9e406ahrens */
451fa9e406ahrensint
452fa9e406ahrensspa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
453fa9e406ahrens{
4540e34b6abonwick	int config_changed = B_FALSE;
455ea8dc4beschrock
4560373e76bonwick	ASSERT(txg > spa_last_synced_txg(spa));
4570e34b6abonwick
4580e34b6abonwick	/*
4590e34b6abonwick	 * Reassess the DTLs.
4600e34b6abonwick	 */
4610373e76bonwick	vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE);
4620e34b6abonwick
4630e34b6abonwick	/*
4640373e76bonwick	 * If the config changed, notify the scrub thread that it must restart.
4650e34b6abonwick	 */
4660e34b6abonwick	if (error == 0 && !list_is_empty(&spa->spa_dirty_list)) {
4670e34b6abonwick		config_changed = B_TRUE;
4680373e76bonwick		spa_scrub_restart(spa, txg);
4690e34b6abonwick	}
470ea8dc4beschrock
471ea8dc4beschrock	spa_config_exit(spa, spa);
472fa9e406ahrens
473ea8dc4beschrock	/*
4745dabedebonwick	 * Allow scrubbing to resume.
475ea8dc4beschrock	 */
476ea8dc4beschrock	spa_scrub_resume(spa);
477fa9e406ahrens
478fa9e406ahrens	/*
479fa9e406ahrens	 * Note: this txg_wait_synced() is important because it ensures
480fa9e406ahrens	 * that there won't be more than one config change per txg.
481fa9e406ahrens	 * This allows us to use the txg as the generation number.
482fa9e406ahrens	 */
483fa9e406ahrens	if (error == 0)
484fa9e406ahrens		txg_wait_synced(spa->spa_dsl_pool, txg);
485fa9e406ahrens
486fa9e406ahrens	if (vd != NULL) {
487fa9e406ahrens		ASSERT(!vd->vdev_detached || vd->vdev_dtl.smo_object == 0);
488fa9e406ahrens		vdev_free(vd);
489fa9e406ahrens	}
490fa9e406ahrens
491fa9e406ahrens	/*
4920e34b6abonwick	 * If the config changed, update the config cache.
493fa9e406ahrens	 */
4940e34b6abonwick	if (config_changed)
495fa9e406ahrens		spa_config_sync();
496ea8dc4beschrock
497ea8dc4beschrock	mutex_exit(&spa_namespace_lock);
498fa9e406ahrens
499fa9e406ahrens	return (error);
500fa9e406ahrens}
501fa9e406ahrens
502fa9e406ahrens/*
503fa9e406ahrens * ==========================================================================
504fa9e406ahrens * Miscellaneous functions
505fa9e406ahrens * ==========================================================================
506fa9e406ahrens */
507fa9e406ahrens
508fa9e406ahrens/*
509fa9e406ahrens * Rename a spa_t.
510fa9e406ahrens */
511fa9e406ahrensint
512fa9e406ahrensspa_rename(const char *name, const char *newname)
513fa9e406ahrens{
514fa9e406ahrens	spa_t *spa;
515fa9e406ahrens	int err;
516fa9e406ahrens
517fa9e406ahrens	/*
518fa9e406ahrens	 * Lookup the spa_t and grab the config lock for writing.  We need to
519fa9e406ahrens	 * actually open the pool so that we can sync out the necessary labels.
520fa9e406ahrens	 * It's OK to call spa_open() with the namespace lock held because we
521ea8dc4beschrock	 * allow recursive calls for other reasons.
522fa9e406ahrens	 */
523fa9e406ahrens	mutex_enter(&spa_namespace_lock);
524fa9e406ahrens	if ((err = spa_open(name, &spa, FTAG)) != 0) {
525fa9e406ahrens		mutex_exit(&spa_namespace_lock);
526fa9e406ahrens		return (err);
527fa9e406ahrens	}
528fa9e406ahrens
529ea8dc4beschrock	spa_config_enter(spa, RW_WRITER, FTAG);
530fa9e406ahrens
531fa9e406ahrens	avl_remove(&spa_namespace_avl, spa);
532fa9e406ahrens	spa_strfree(spa->spa_name);
533fa9e406ahrens	spa->spa_name = spa_strdup(newname);
534fa9e406ahrens	avl_add(&spa_namespace_avl, spa);
535fa9e406ahrens
536fa9e406ahrens	/*
537fa9e406ahrens	 * Sync all labels to disk with the new names by marking the root vdev
538fa9e406ahrens	 * dirty and waiting for it to sync.  It will pick up the new pool name
539fa9e406ahrens	 * during the sync.
540fa9e406ahrens	 */
541fa9e406ahrens	vdev_config_dirty(spa->spa_root_vdev);
542fa9e406ahrens
543ea8dc4beschrock	spa_config_exit(spa, FTAG);
544fa9e406ahrens
5450373e76bonwick	txg_wait_synced(spa->spa_dsl_pool, 0);
546fa9e406ahrens
547fa9e406ahrens	/*
548fa9e406ahrens	 * Sync the updated config cache.
549fa9e406ahrens	 */
550fa9e406ahrens	spa_config_sync();
551fa9e406ahrens
552fa9e406ahrens	spa_close(spa, FTAG);
553fa9e406ahrens
554fa9e406ahrens	mutex_exit(&spa_namespace_lock);
555fa9e406ahrens
556fa9e406ahrens	return (0);
557fa9e406ahrens}
558fa9e406ahrens
559fa9e406ahrens
560fa9e406ahrens/*
561fa9e406ahrens * Determine whether a pool with given pool_guid exists.  If device_guid is
562fa9e406ahrens * non-zero, determine whether the pool exists *and* contains a device with the
563fa9e406ahrens * specified device_guid.
564fa9e406ahrens */
565fa9e406ahrensboolean_t
566fa9e406ahrensspa_guid_exists(uint64_t pool_guid, uint64_t device_guid)
567fa9e406ahrens{
568fa9e406ahrens	spa_t *spa;
569fa9e406ahrens	avl_tree_t *t = &spa_namespace_avl;
570fa9e406ahrens
571ea8dc4beschrock	ASSERT(MUTEX_HELD(&spa_namespace_lock));
572fa9e406ahrens
573fa9e406ahrens	for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) {
574fa9e406ahrens		if (spa->spa_state == POOL_STATE_UNINITIALIZED)
575fa9e406ahrens			continue;
576fa9e406ahrens		if (spa->spa_root_vdev == NULL)
577fa9e406ahrens			continue;
578fa9e406ahrens		if (spa_guid(spa) == pool_guid && (device_guid == 0 ||
579fa9e406ahrens		    vdev_lookup_by_guid(spa->spa_root_vdev, device_guid)))
580fa9e406ahrens			break;
581fa9e406ahrens	}
582fa9e406ahrens
583fa9e406ahrens	return (spa != NULL);
584fa9e406ahrens}
585fa9e406ahrens
586fa9e406ahrenschar *
587fa9e406ahrensspa_strdup(const char *s)
588fa9e406ahrens{
589fa9e406ahrens	size_t len;
590fa9e406ahrens	char *new;
591fa9e406ahrens
592fa9e406ahrens	len = strlen(s);
593fa9e406ahrens	new = kmem_alloc(len + 1, KM_SLEEP);
594fa9e406ahrens	bcopy(s, new, len);
595fa9e406ahrens	new[len] = '\0';
596fa9e406ahrens
597fa9e406ahrens	return (new);
598fa9e406ahrens}
599fa9e406ahrens
600fa9e406ahrensvoid
601fa9e406ahrensspa_strfree(char *s)
602fa9e406ahrens{
603fa9e406ahrens	kmem_free(s, strlen(s) + 1);
604fa9e406ahrens}
605fa9e406ahrens
606fa9e406ahrensuint64_t
607fa9e406ahrensspa_get_random(uint64_t range)
608fa9e406ahrens{
609fa9e406ahrens	uint64_t r;
610fa9e406ahrens
611fa9e406ahrens	ASSERT(range != 0);
612fa9e406ahrens
613fa9e406ahrens	(void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t));
614fa9e406ahrens
615fa9e406ahrens	return (r % range);
616fa9e406ahrens}
617fa9e406ahrens
618fa9e406ahrensvoid
619fbabab8maybeesprintf_blkptr(char *buf, int len, blkptr_t *bp)
620fa9e406ahrens{
62144cd46cbillm	int d;
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
63344cd46cbillm	(void) snprintf(buf, len, "[L%llu %s] %llxL/%llxP ",
634fa9e406ahrens	    (u_longlong_t)BP_GET_LEVEL(bp),
635fa9e406ahrens	    dmu_ot[BP_GET_TYPE(bp)].ot_name,
636fa9e406ahrens	    (u_longlong_t)BP_GET_LSIZE(bp),
63744cd46cbillm	    (u_longlong_t)BP_GET_PSIZE(bp));
63844cd46cbillm
63944cd46cbillm	for (d = 0; d < BP_GET_NDVAS(bp); d++) {
64044cd46cbillm		dva_t *dva = &bp->blk_dva[d];
64144cd46cbillm		(void) snprintf(buf + strlen(buf), len - strlen(buf),
64244cd46cbillm		    "DVA[%d]=<%llu:%llx:%llx> ", d,
64344cd46cbillm		    (u_longlong_t)DVA_GET_VDEV(dva),
64444cd46cbillm		    (u_longlong_t)DVA_GET_OFFSET(dva),
64544cd46cbillm		    (u_longlong_t)DVA_GET_ASIZE(dva));
64644cd46cbillm	}
64744cd46cbillm
64844cd46cbillm	(void) snprintf(buf + strlen(buf), len - strlen(buf),
64944cd46cbillm	    "%s %s %s %s birth=%llu fill=%llu cksum=%llx:%llx:%llx:%llx",
650fa9e406ahrens	    zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name,
651fa9e406ahrens	    zio_compress_table[BP_GET_COMPRESS(bp)].ci_name,
652fa9e406ahrens	    BP_GET_BYTEORDER(bp) == 0 ? "BE" : "LE",
65344cd46cbillm	    BP_IS_GANG(bp) ? "gang" : "contiguous",
654fa9e406ahrens	    (u_longlong_t)bp->blk_birth,
655fa9e406ahrens	    (u_longlong_t)bp->blk_fill,
656fa9e406ahrens	    (u_longlong_t)bp->blk_cksum.zc_word[0],
657fa9e406ahrens	    (u_longlong_t)bp->blk_cksum.zc_word[1],
658fa9e406ahrens	    (u_longlong_t)bp->blk_cksum.zc_word[2],
659fa9e406ahrens	    (u_longlong_t)bp->blk_cksum.zc_word[3]);
660fa9e406ahrens}
661fa9e406ahrens
662fa9e406ahrensvoid
663fa9e406ahrensspa_freeze(spa_t *spa)
664fa9e406ahrens{
665fa9e406ahrens	uint64_t freeze_txg = 0;
666fa9e406ahrens
667ea8dc4beschrock	spa_config_enter(spa, RW_WRITER, FTAG);
668fa9e406ahrens	if (spa->spa_freeze_txg == UINT64_MAX) {
669fa9e406ahrens		freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE;
670fa9e406ahrens		spa->spa_freeze_txg = freeze_txg;
671fa9e406ahrens	}
672ea8dc4beschrock	spa_config_exit(spa, FTAG);
673fa9e406ahrens	if (freeze_txg != 0)
674fa9e406ahrens		txg_wait_synced(spa_get_dsl(spa), freeze_txg);
675fa9e406ahrens}
676fa9e406ahrens
677fa9e406ahrens/*
678fa9e406ahrens * ==========================================================================
679fa9e406ahrens * Accessor functions
680fa9e406ahrens * ==========================================================================
681fa9e406ahrens */
682fa9e406ahrens
683fa9e406ahrenskrwlock_t *
684fa9e406ahrensspa_traverse_rwlock(spa_t *spa)
685fa9e406ahrens{
686fa9e406ahrens	return (&spa->spa_traverse_lock);
687fa9e406ahrens}
688fa9e406ahrens
689fa9e406ahrensint
690fa9e406ahrensspa_traverse_wanted(spa_t *spa)
691fa9e406ahrens{
692fa9e406ahrens	return (spa->spa_traverse_wanted);
693fa9e406ahrens}
694fa9e406ahrens
695fa9e406ahrensdsl_pool_t *
696fa9e406ahrensspa_get_dsl(spa_t *spa)
697fa9e406ahrens{
698fa9e406ahrens	return (spa->spa_dsl_pool);
699fa9e406ahrens}
700fa9e406ahrens
701fa9e406ahrensblkptr_t *
702fa9e406ahrensspa_get_rootblkptr(spa_t *spa)
703fa9e406ahrens{
704fa9e406ahrens	return (&spa->spa_ubsync.ub_rootbp);
705fa9e406ahrens}
706fa9e406ahrens
707fa9e406ahrensvoid
708fa9e406ahrensspa_set_rootblkptr(spa_t *spa, const blkptr_t *bp)
709fa9e406ahrens{
710fa9e406ahrens	spa->spa_uberblock.ub_rootbp = *bp;
711fa9e406ahrens}
712fa9e406ahrens
713fa9e406ahrensvoid
714fa9e406ahrensspa_altroot(spa_t *spa, char *buf, size_t buflen)
715fa9e406ahrens{
716fa9e406ahrens	if (spa->spa_root == NULL)
717fa9e406ahrens		buf[0] = '\0';
718fa9e406ahrens	else
719fa9e406ahrens		(void) strncpy(buf, spa->spa_root, buflen);
720fa9e406ahrens}
721fa9e406ahrens
722fa9e406ahrensint
723fa9e406ahrensspa_sync_pass(spa_t *spa)
724fa9e406ahrens{
725fa9e406ahrens	return (spa->spa_sync_pass);
726fa9e406ahrens}
727fa9e406ahrens
728fa9e406ahrenschar *
729fa9e406ahrensspa_name(spa_t *spa)
730fa9e406ahrens{
731fa9e406ahrens	/*
732fa9e406ahrens	 * Accessing the name requires holding either the namespace lock or the
733fa9e406ahrens	 * config lock, both of which are required to do a rename.
734fa9e406ahrens	 */
735fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock) ||
736fa9e406ahrens	    spa_config_held(spa, RW_READER) || spa_config_held(spa, RW_WRITER));
737fa9e406ahrens
738fa9e406ahrens	return (spa->spa_name);
739fa9e406ahrens}
740fa9e406ahrens
741fa9e406ahrensuint64_t
742fa9e406ahrensspa_guid(spa_t *spa)
743fa9e406ahrens{
744fa9e406ahrens	return (spa->spa_root_vdev->vdev_guid);
745fa9e406ahrens}
746fa9e406ahrens
747fa9e406ahrensuint64_t
748fa9e406ahrensspa_last_synced_txg(spa_t *spa)
749fa9e406ahrens{
750fa9e406ahrens	return (spa->spa_ubsync.ub_txg);
751fa9e406ahrens}
752fa9e406ahrens
753fa9e406ahrensuint64_t
754fa9e406ahrensspa_first_txg(spa_t *spa)
755fa9e406ahrens{
756fa9e406ahrens	return (spa->spa_first_txg);
757fa9e406ahrens}
758fa9e406ahrens
759fa9e406ahrensint
760fa9e406ahrensspa_state(spa_t *spa)
761fa9e406ahrens{
762fa9e406ahrens	return (spa->spa_state);
763fa9e406ahrens}
764fa9e406ahrens
765fa9e406ahrensuint64_t
766fa9e406ahrensspa_freeze_txg(spa_t *spa)
767fa9e406ahrens{
768fa9e406ahrens	return (spa->spa_freeze_txg);
769fa9e406ahrens}
770fa9e406ahrens
771fa9e406ahrens/*
772fa9e406ahrens * In the future, this may select among different metaslab classes
773fa9e406ahrens * depending on the zdp.  For now, there's no such distinction.
774fa9e406ahrens */
775fa9e406ahrensmetaslab_class_t *
776fa9e406ahrensspa_metaslab_class_select(spa_t *spa)
777fa9e406ahrens{
778fa9e406ahrens	return (spa->spa_normal_class);
779fa9e406ahrens}
780fa9e406ahrens
781fa9e406ahrens/*
782fa9e406ahrens * Return pool-wide allocated space.
783fa9e406ahrens */
784fa9e406ahrensuint64_t
785fa9e406ahrensspa_get_alloc(spa_t *spa)
786fa9e406ahrens{
787fa9e406ahrens	return (spa->spa_root_vdev->vdev_stat.vs_alloc);
788fa9e406ahrens}
789fa9e406ahrens
790fa9e406ahrens/*
791fa9e406ahrens * Return pool-wide allocated space.
792fa9e406ahrens */
793fa9e406ahrensuint64_t
794fa9e406ahrensspa_get_space(spa_t *spa)
795fa9e406ahrens{
796fa9e406ahrens	return (spa->spa_root_vdev->vdev_stat.vs_space);
797fa9e406ahrens}
798fa9e406ahrens
799fa9e406ahrens/* ARGSUSED */
800fa9e406ahrensuint64_t
801fa9e406ahrensspa_get_asize(spa_t *spa, uint64_t lsize)
802fa9e406ahrens{
803fa9e406ahrens	/*
804fa9e406ahrens	 * For now, the worst case is 512-byte RAID-Z blocks, in which
805fa9e406ahrens	 * case the space requirement is exactly 2x; so just assume that.
80644cd46cbillm	 * Add to this the fact that we can have up to 3 DVAs per bp, and
80744cd46cbillm	 * we have to multiply by a total of 6x.
80844cd46cbillm	 */
80944cd46cbillm	return (lsize * 6);
81044cd46cbillm}
81144cd46cbillm
81244cd46cbillmuint64_t
81344cd46cbillmspa_version(spa_t *spa)
81444cd46cbillm{
81544cd46cbillm	return (spa->spa_ubsync.ub_version);
81644cd46cbillm}
81744cd46cbillm
81844cd46cbillmint
81944cd46cbillmspa_max_replication(spa_t *spa)
82044cd46cbillm{
82144cd46cbillm	/*
82244cd46cbillm	 * As of ZFS_VERSION == ZFS_VERSION_DITTO_BLOCKS, we are able to
82344cd46cbillm	 * handle BPs with more than one DVA allocated.  Set our max
82444cd46cbillm	 * replication level accordingly.
825fa9e406ahrens	 */
82644cd46cbillm	if (spa_version(spa) < ZFS_VERSION_DITTO_BLOCKS)
82744cd46cbillm		return (1);
82844cd46cbillm	return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override));
829fa9e406ahrens}
830fa9e406ahrens
831fa9e406ahrens/*
832fa9e406ahrens * ==========================================================================
833fa9e406ahrens * Initialization and Termination
834fa9e406ahrens * ==========================================================================
835fa9e406ahrens */
836fa9e406ahrens
837fa9e406ahrensstatic int
838fa9e406ahrensspa_name_compare(const void *a1, const void *a2)
839fa9e406ahrens{
840fa9e406ahrens	const spa_t *s1 = a1;
841fa9e406ahrens	const spa_t *s2 = a2;
842fa9e406ahrens	int s;
843fa9e406ahrens
844fa9e406ahrens	s = strcmp(s1->spa_name, s2->spa_name);
845fa9e406ahrens	if (s > 0)
846fa9e406ahrens		return (1);
847fa9e406ahrens	if (s < 0)
848fa9e406ahrens		return (-1);
849fa9e406ahrens	return (0);
850fa9e406ahrens}
851fa9e406ahrens
8520373e76bonwickint
8530373e76bonwickspa_busy(void)
8540373e76bonwick{
8550373e76bonwick	return (spa_active_count);
8560373e76bonwick}
8570373e76bonwick
858fa9e406ahrensvoid
859fa9e406ahrensspa_init(int mode)
860fa9e406ahrens{
861fa9e406ahrens	mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL);
862fa9e406ahrens	cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL);
863fa9e406ahrens
864fa9e406ahrens	avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t),
865fa9e406ahrens	    offsetof(spa_t, spa_avl));
866fa9e406ahrens
867fa9e406ahrens	spa_mode = mode;
868fa9e406ahrens
869fa9e406ahrens	refcount_init();
870fa9e406ahrens	unique_init();
871fa9e406ahrens	zio_init();
872fa9e406ahrens	dmu_init();
873fa9e406ahrens	zil_init();
874fa9e406ahrens	spa_config_load();
875fa9e406ahrens}
876fa9e406ahrens
877fa9e406ahrensvoid
878fa9e406ahrensspa_fini(void)
879fa9e406ahrens{
880fa9e406ahrens	spa_evict_all();
881fa9e406ahrens
882fa9e406ahrens	zil_fini();
883fa9e406ahrens	dmu_fini();
884fa9e406ahrens	zio_fini();
885fa9e406ahrens	refcount_fini();
886fa9e406ahrens
887fa9e406ahrens	avl_destroy(&spa_namespace_avl);
888fa9e406ahrens
889fa9e406ahrens	cv_destroy(&spa_namespace_cv);
890fa9e406ahrens	mutex_destroy(&spa_namespace_lock);
891fa9e406ahrens}
892