spa.c revision ea8dc4b6d2251b437950c0056bc626b311c73c27
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/*
22c67d967eschrock * 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/*
29fa9e406ahrens * This file contains all the routines used when modifying on-disk SPA state.
30fa9e406ahrens * This includes opening, importing, destroying, exporting a pool, and syncing a
31fa9e406ahrens * pool.
32fa9e406ahrens */
33fa9e406ahrens
34fa9e406ahrens#include <sys/zfs_context.h>
35ea8dc4beschrock#include <sys/fm/fs/zfs.h>
36fa9e406ahrens#include <sys/spa_impl.h>
37fa9e406ahrens#include <sys/zio.h>
38fa9e406ahrens#include <sys/zio_checksum.h>
39fa9e406ahrens#include <sys/zio_compress.h>
40fa9e406ahrens#include <sys/dmu.h>
41fa9e406ahrens#include <sys/dmu_tx.h>
42fa9e406ahrens#include <sys/zap.h>
43fa9e406ahrens#include <sys/zil.h>
44fa9e406ahrens#include <sys/vdev_impl.h>
45fa9e406ahrens#include <sys/metaslab.h>
46fa9e406ahrens#include <sys/uberblock_impl.h>
47fa9e406ahrens#include <sys/txg.h>
48fa9e406ahrens#include <sys/avl.h>
49fa9e406ahrens#include <sys/dmu_traverse.h>
50fa9e406ahrens#include <sys/unique.h>
51fa9e406ahrens#include <sys/dsl_pool.h>
52fa9e406ahrens#include <sys/dsl_dir.h>
53fa9e406ahrens#include <sys/dsl_prop.h>
54fa9e406ahrens#include <sys/fs/zfs.h>
55fa9e406ahrens#include <sys/callb.h>
56fa9e406ahrens
57fa9e406ahrensstatic uint32_t spa_active_count;
58fa9e406ahrens
59fa9e406ahrens/*
60fa9e406ahrens * ==========================================================================
61fa9e406ahrens * SPA state manipulation (open/create/destroy/import/export)
62fa9e406ahrens * ==========================================================================
63fa9e406ahrens */
64fa9e406ahrens
65ea8dc4beschrockstatic int
66ea8dc4beschrockspa_error_entry_compare(const void *a, const void *b)
67ea8dc4beschrock{
68ea8dc4beschrock	spa_error_entry_t *sa = (spa_error_entry_t *)a;
69ea8dc4beschrock	spa_error_entry_t *sb = (spa_error_entry_t *)b;
70ea8dc4beschrock	int ret;
71ea8dc4beschrock
72ea8dc4beschrock	ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
73ea8dc4beschrock	    sizeof (zbookmark_t));
74ea8dc4beschrock
75ea8dc4beschrock	if (ret < 0)
76ea8dc4beschrock		return (-1);
77ea8dc4beschrock	else if (ret > 0)
78ea8dc4beschrock		return (1);
79ea8dc4beschrock	else
80ea8dc4beschrock		return (0);
81ea8dc4beschrock}
82ea8dc4beschrock
83ea8dc4beschrock/*
84ea8dc4beschrock * Utility function which retrieves copies of the current logs and
85ea8dc4beschrock * re-initializes them in the process.
86ea8dc4beschrock */
87ea8dc4beschrockvoid
88ea8dc4beschrockspa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
89ea8dc4beschrock{
90ea8dc4beschrock	ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
91ea8dc4beschrock
92ea8dc4beschrock	bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
93ea8dc4beschrock	bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
94ea8dc4beschrock
95ea8dc4beschrock	avl_create(&spa->spa_errlist_scrub,
96ea8dc4beschrock	    spa_error_entry_compare, sizeof (spa_error_entry_t),
97ea8dc4beschrock	    offsetof(spa_error_entry_t, se_avl));
98ea8dc4beschrock	avl_create(&spa->spa_errlist_last,
99ea8dc4beschrock	    spa_error_entry_compare, sizeof (spa_error_entry_t),
100ea8dc4beschrock	    offsetof(spa_error_entry_t, se_avl));
101ea8dc4beschrock}
102ea8dc4beschrock
103fa9e406ahrens/*
104fa9e406ahrens * Activate an uninitialized pool.
105fa9e406ahrens */
106fa9e406ahrensstatic void
107fa9e406ahrensspa_activate(spa_t *spa)
108fa9e406ahrens{
109fa9e406ahrens	int t;
110fa9e406ahrens
111fa9e406ahrens	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
112fa9e406ahrens
113fa9e406ahrens	spa->spa_state = POOL_STATE_ACTIVE;
114fa9e406ahrens
115fa9e406ahrens	spa->spa_normal_class = metaslab_class_create();
116fa9e406ahrens
117fa9e406ahrens	for (t = 0; t < ZIO_TYPES; t++) {
118fa9e406ahrens		spa->spa_zio_issue_taskq[t] = taskq_create("spa_zio_issue",
119fa9e406ahrens		    8, maxclsyspri, 50, INT_MAX,
120fa9e406ahrens		    TASKQ_PREPOPULATE);
121fa9e406ahrens		spa->spa_zio_intr_taskq[t] = taskq_create("spa_zio_intr",
122fa9e406ahrens		    8, maxclsyspri, 50, INT_MAX,
123fa9e406ahrens		    TASKQ_PREPOPULATE);
124fa9e406ahrens	}
125fa9e406ahrens
126fa9e406ahrens	rw_init(&spa->spa_traverse_lock, NULL, RW_DEFAULT, NULL);
127fa9e406ahrens
128fa9e406ahrens	list_create(&spa->spa_dirty_list, sizeof (vdev_t),
129fa9e406ahrens	    offsetof(vdev_t, vdev_dirty_node));
130fa9e406ahrens
131fa9e406ahrens	txg_list_create(&spa->spa_vdev_txg_list,
132fa9e406ahrens	    offsetof(struct vdev, vdev_txg_node));
133ea8dc4beschrock
134ea8dc4beschrock	avl_create(&spa->spa_errlist_scrub,
135ea8dc4beschrock	    spa_error_entry_compare, sizeof (spa_error_entry_t),
136ea8dc4beschrock	    offsetof(spa_error_entry_t, se_avl));
137ea8dc4beschrock	avl_create(&spa->spa_errlist_last,
138ea8dc4beschrock	    spa_error_entry_compare, sizeof (spa_error_entry_t),
139ea8dc4beschrock	    offsetof(spa_error_entry_t, se_avl));
140fa9e406ahrens}
141fa9e406ahrens
142fa9e406ahrens/*
143fa9e406ahrens * Opposite of spa_activate().
144fa9e406ahrens */
145fa9e406ahrensstatic void
146fa9e406ahrensspa_deactivate(spa_t *spa)
147fa9e406ahrens{
148fa9e406ahrens	int t;
149fa9e406ahrens
150fa9e406ahrens	ASSERT(spa->spa_sync_on == B_FALSE);
151fa9e406ahrens	ASSERT(spa->spa_dsl_pool == NULL);
152fa9e406ahrens	ASSERT(spa->spa_root_vdev == NULL);
153fa9e406ahrens
154fa9e406ahrens	ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
155fa9e406ahrens
156fa9e406ahrens	txg_list_destroy(&spa->spa_vdev_txg_list);
157fa9e406ahrens
158fa9e406ahrens	list_destroy(&spa->spa_dirty_list);
159fa9e406ahrens
160fa9e406ahrens	rw_destroy(&spa->spa_traverse_lock);
161fa9e406ahrens
162fa9e406ahrens	for (t = 0; t < ZIO_TYPES; t++) {
163fa9e406ahrens		taskq_destroy(spa->spa_zio_issue_taskq[t]);
164fa9e406ahrens		taskq_destroy(spa->spa_zio_intr_taskq[t]);
165fa9e406ahrens		spa->spa_zio_issue_taskq[t] = NULL;
166fa9e406ahrens		spa->spa_zio_intr_taskq[t] = NULL;
167fa9e406ahrens	}
168fa9e406ahrens
169fa9e406ahrens	metaslab_class_destroy(spa->spa_normal_class);
170fa9e406ahrens	spa->spa_normal_class = NULL;
171fa9e406ahrens
172ea8dc4beschrock	/*
173ea8dc4beschrock	 * If this was part of an import or the open otherwise failed, we may
174ea8dc4beschrock	 * still have errors left in the queues.  Empty them just in case.
175ea8dc4beschrock	 */
176ea8dc4beschrock	spa_errlog_drain(spa);
177ea8dc4beschrock
178ea8dc4beschrock	avl_destroy(&spa->spa_errlist_scrub);
179ea8dc4beschrock	avl_destroy(&spa->spa_errlist_last);
180ea8dc4beschrock
181fa9e406ahrens	spa->spa_state = POOL_STATE_UNINITIALIZED;
182fa9e406ahrens}
183fa9e406ahrens
184fa9e406ahrens/*
185fa9e406ahrens * Verify a pool configuration, and construct the vdev tree appropriately.  This
186fa9e406ahrens * will create all the necessary vdevs in the appropriate layout, with each vdev
187fa9e406ahrens * in the CLOSED state.  This will prep the pool before open/creation/import.
188fa9e406ahrens * All vdev validation is done by the vdev_alloc() routine.
189fa9e406ahrens */
190fa9e406ahrensstatic vdev_t *
191fa9e406ahrensspa_config_parse(spa_t *spa, nvlist_t *nv, vdev_t *parent, uint_t id, int atype)
192fa9e406ahrens{
193fa9e406ahrens	nvlist_t **child;
194fa9e406ahrens	uint_t c, children;
195fa9e406ahrens	vdev_t *vd;
196fa9e406ahrens
197fa9e406ahrens	if ((vd = vdev_alloc(spa, nv, parent, id, atype)) == NULL)
198fa9e406ahrens		return (NULL);
199fa9e406ahrens
200fa9e406ahrens	if (vd->vdev_ops->vdev_op_leaf)
201fa9e406ahrens		return (vd);
202fa9e406ahrens
203fa9e406ahrens	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
204fa9e406ahrens	    &child, &children) != 0) {
205fa9e406ahrens		vdev_free(vd);
206fa9e406ahrens		return (NULL);
207fa9e406ahrens	}
208fa9e406ahrens
209fa9e406ahrens	for (c = 0; c < children; c++) {
210fa9e406ahrens		if (spa_config_parse(spa, child[c], vd, c, atype) == NULL) {
211fa9e406ahrens			vdev_free(vd);
212fa9e406ahrens			return (NULL);
213fa9e406ahrens		}
214fa9e406ahrens	}
215fa9e406ahrens
216fa9e406ahrens	return (vd);
217fa9e406ahrens}
218fa9e406ahrens
219fa9e406ahrens/*
220fa9e406ahrens * Opposite of spa_load().
221fa9e406ahrens */
222fa9e406ahrensstatic void
223fa9e406ahrensspa_unload(spa_t *spa)
224fa9e406ahrens{
225fa9e406ahrens	/*
226ea8dc4beschrock	 * Stop async tasks.
227ea8dc4beschrock	 */
228ea8dc4beschrock	spa_async_suspend(spa);
229ea8dc4beschrock
230ea8dc4beschrock	/*
231fa9e406ahrens	 * Stop syncing.
232fa9e406ahrens	 */
233fa9e406ahrens	if (spa->spa_sync_on) {
234fa9e406ahrens		txg_sync_stop(spa->spa_dsl_pool);
235fa9e406ahrens		spa->spa_sync_on = B_FALSE;
236fa9e406ahrens	}
237fa9e406ahrens
238fa9e406ahrens	/*
239fa9e406ahrens	 * Wait for any outstanding prefetch I/O to complete.
240fa9e406ahrens	 */
241ea8dc4beschrock	spa_config_enter(spa, RW_WRITER, FTAG);
242ea8dc4beschrock	spa_config_exit(spa, FTAG);
243fa9e406ahrens
244fa9e406ahrens	/*
245fa9e406ahrens	 * Close the dsl pool.
246fa9e406ahrens	 */
247fa9e406ahrens	if (spa->spa_dsl_pool) {
248fa9e406ahrens		dsl_pool_close(spa->spa_dsl_pool);
249fa9e406ahrens		spa->spa_dsl_pool = NULL;
250fa9e406ahrens	}
251fa9e406ahrens
252fa9e406ahrens	/*
253fa9e406ahrens	 * Close all vdevs.
254fa9e406ahrens	 */
255fa9e406ahrens	if (spa->spa_root_vdev) {
256fa9e406ahrens		vdev_free(spa->spa_root_vdev);
257fa9e406ahrens		spa->spa_root_vdev = NULL;
258fa9e406ahrens	}
259ea8dc4beschrock
260ea8dc4beschrock	spa->spa_async_suspended = 0;
261fa9e406ahrens}
262fa9e406ahrens
263fa9e406ahrens/*
264fa9e406ahrens * Load an existing storage pool, using the pool's builtin spa_config as a
265ea8dc4beschrock * source of configuration information.
266fa9e406ahrens */
267fa9e406ahrensstatic int
268ea8dc4beschrockspa_load(spa_t *spa, nvlist_t *config, spa_load_state_t state, int mosconfig)
269fa9e406ahrens{
270fa9e406ahrens	int error = 0;
271fa9e406ahrens	nvlist_t *nvroot = NULL;
272fa9e406ahrens	vdev_t *rvd;
273fa9e406ahrens	uberblock_t *ub = &spa->spa_uberblock;
274fa9e406ahrens	uint64_t pool_guid;
275fa9e406ahrens	zio_t *zio;
276fa9e406ahrens
277ea8dc4beschrock	spa->spa_load_state = state;
278fa9e406ahrens	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot) ||
279ea8dc4beschrock	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) {
280ea8dc4beschrock		error = EINVAL;
281ea8dc4beschrock		goto out;
282ea8dc4beschrock	}
283fa9e406ahrens
284fa9e406ahrens	(void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
285fa9e406ahrens	    &spa->spa_config_txg);
286fa9e406ahrens
287ea8dc4beschrock	if ((spa->spa_load_state == SPA_LOAD_IMPORT ||
288ea8dc4beschrock	    spa->spa_load_state == SPA_LOAD_TRYIMPORT) &&
289ea8dc4beschrock	    spa_guid_exists(pool_guid, 0)) {
290ea8dc4beschrock		error = EEXIST;
291ea8dc4beschrock		goto out;
292ea8dc4beschrock	}
293fa9e406ahrens
294fa9e406ahrens	/*
295fa9e406ahrens	 * Parse the configuration into a vdev tree.
296fa9e406ahrens	 */
297ea8dc4beschrock	spa_config_enter(spa, RW_WRITER, FTAG);
298fa9e406ahrens	rvd = spa_config_parse(spa, nvroot, NULL, 0, VDEV_ALLOC_LOAD);
299ea8dc4beschrock	spa_config_exit(spa, FTAG);
300fa9e406ahrens
301ea8dc4beschrock	if (rvd == NULL) {
302ea8dc4beschrock		error = EINVAL;
303ea8dc4beschrock		goto out;
304ea8dc4beschrock	}
305fa9e406ahrens
306fa9e406ahrens	spa->spa_root_vdev = rvd;
307fa9e406ahrens	ASSERT(spa_guid(spa) == pool_guid);
308fa9e406ahrens
309fa9e406ahrens	/*
310fa9e406ahrens	 * Try to open all vdevs, loading each label in the process.
311fa9e406ahrens	 */
312ea8dc4beschrock	if (vdev_open(rvd) != 0) {
313ea8dc4beschrock		error = ENXIO;
314ea8dc4beschrock		goto out;
315ea8dc4beschrock	}
316fa9e406ahrens
317fa9e406ahrens	/*
318fa9e406ahrens	 * Find the best uberblock.
319fa9e406ahrens	 */
320fa9e406ahrens	bzero(ub, sizeof (uberblock_t));
321fa9e406ahrens
322fa9e406ahrens	zio = zio_root(spa, NULL, NULL,
323fa9e406ahrens	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
324fa9e406ahrens	vdev_uberblock_load(zio, rvd, ub);
325fa9e406ahrens	error = zio_wait(zio);
326fa9e406ahrens
327fa9e406ahrens	/*
328fa9e406ahrens	 * If we weren't able to find a single valid uberblock, return failure.
329fa9e406ahrens	 */
330fa9e406ahrens	if (ub->ub_txg == 0) {
331ea8dc4beschrock		error = ENXIO;
332ea8dc4beschrock		goto out;
333ea8dc4beschrock	}
334ea8dc4beschrock
335ea8dc4beschrock	/*
336ea8dc4beschrock	 * If the pool is newer than the code, we can't open it.
337ea8dc4beschrock	 */
338ea8dc4beschrock	if (ub->ub_version > UBERBLOCK_VERSION) {
339ea8dc4beschrock		error = ENOTSUP;
340ea8dc4beschrock		goto out;
341fa9e406ahrens	}
342fa9e406ahrens
343fa9e406ahrens	/*
344fa9e406ahrens	 * If the vdev guid sum doesn't match the uberblock, we have an
345fa9e406ahrens	 * incomplete configuration.
346fa9e406ahrens	 */
347fa9e406ahrens	if (rvd->vdev_guid_sum != ub->ub_guid_sum && mosconfig) {
348ea8dc4beschrock		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
349ea8dc4beschrock		    VDEV_AUX_BAD_GUID_SUM);
350ea8dc4beschrock		error = ENXIO;
351ea8dc4beschrock		goto out;
352fa9e406ahrens	}
353fa9e406ahrens
354fa9e406ahrens	/*
355fa9e406ahrens	 * Initialize internal SPA structures.
356fa9e406ahrens	 */
357fa9e406ahrens	spa->spa_state = POOL_STATE_ACTIVE;
358fa9e406ahrens	spa->spa_ubsync = spa->spa_uberblock;
359fa9e406ahrens	spa->spa_first_txg = spa_last_synced_txg(spa) + 1;
360ea8dc4beschrock	error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
361ea8dc4beschrock	if (error) {
362ea8dc4beschrock		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
363ea8dc4beschrock		    VDEV_AUX_CORRUPT_DATA);
364ea8dc4beschrock		goto out;
365ea8dc4beschrock	}
366fa9e406ahrens	spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
367fa9e406ahrens
368ea8dc4beschrock	if (zap_lookup(spa->spa_meta_objset,
369fa9e406ahrens	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
370ea8dc4beschrock	    sizeof (uint64_t), 1, &spa->spa_config_object) != 0) {
371ea8dc4beschrock		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
372ea8dc4beschrock		    VDEV_AUX_CORRUPT_DATA);
373ea8dc4beschrock		error = EIO;
374ea8dc4beschrock		goto out;
375ea8dc4beschrock	}
376fa9e406ahrens
377fa9e406ahrens	if (!mosconfig) {
378fa9e406ahrens		dmu_buf_t *db;
379fa9e406ahrens		char *packed = NULL;
380fa9e406ahrens		size_t nvsize = 0;
381fa9e406ahrens		nvlist_t *newconfig = NULL;
382fa9e406ahrens
383ea8dc4beschrock		VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset,
384ea8dc4beschrock		    spa->spa_config_object, FTAG, &db));
385fa9e406ahrens		nvsize = *(uint64_t *)db->db_data;
386ea8dc4beschrock		dmu_buf_rele(db, FTAG);
387fa9e406ahrens
388fa9e406ahrens		packed = kmem_alloc(nvsize, KM_SLEEP);
389ea8dc4beschrock		error = dmu_read(spa->spa_meta_objset,
390fa9e406ahrens		    spa->spa_config_object, 0, nvsize, packed);
391fa9e406ahrens		if (error == 0)
392fa9e406ahrens			error = nvlist_unpack(packed, nvsize, &newconfig, 0);
393fa9e406ahrens		kmem_free(packed, nvsize);
394fa9e406ahrens
395ea8dc4beschrock		if (error) {
396ea8dc4beschrock			vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
397ea8dc4beschrock			    VDEV_AUX_CORRUPT_DATA);
398ea8dc4beschrock			error = EIO;
399ea8dc4beschrock			goto out;
400ea8dc4beschrock		}
401fa9e406ahrens
402fa9e406ahrens		spa_config_set(spa, newconfig);
403fa9e406ahrens
404fa9e406ahrens		spa_unload(spa);
405fa9e406ahrens		spa_deactivate(spa);
406fa9e406ahrens		spa_activate(spa);
407fa9e406ahrens
408ea8dc4beschrock		return (spa_load(spa, newconfig, state, B_TRUE));
409fa9e406ahrens	}
410fa9e406ahrens
411ea8dc4beschrock	if (zap_lookup(spa->spa_meta_objset,
412fa9e406ahrens	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST,
413ea8dc4beschrock	    sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj) != 0) {
414ea8dc4beschrock		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
415ea8dc4beschrock		    VDEV_AUX_CORRUPT_DATA);
416ea8dc4beschrock		error = EIO;
417ea8dc4beschrock		goto out;
418ea8dc4beschrock	}
419fa9e406ahrens
420fa9e406ahrens	/*
421ea8dc4beschrock	 * Load the persistent error log.  If we have an older pool, this will
422