xref: /onnv-gate/usr/src/uts/common/fs/zfs/dsl_pool.c (revision 2082:76b439ec3ac1)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51544Seschrock  * Common Development and Distribution License (the "License").
61544Seschrock  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
21789Sahrens /*
221544Seschrock  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
26789Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
27789Sahrens 
28789Sahrens #include <sys/dsl_pool.h>
29789Sahrens #include <sys/dsl_dataset.h>
30789Sahrens #include <sys/dsl_dir.h>
31789Sahrens #include <sys/dmu_tx.h>
32789Sahrens #include <sys/dmu_objset.h>
33789Sahrens #include <sys/arc.h>
34789Sahrens #include <sys/zap.h>
35789Sahrens #include <sys/zfs_context.h>
36789Sahrens #include <sys/fs/zfs.h>
37789Sahrens 
38789Sahrens /* internal reserved dir name */
39789Sahrens #define	MOS_DIR_NAME "$MOS"
40789Sahrens 
411544Seschrock static int
421544Seschrock dsl_pool_open_mos_dir(dsl_pool_t *dp, dsl_dir_t **ddp)
43789Sahrens {
44789Sahrens 	uint64_t obj;
45789Sahrens 	int err;
46789Sahrens 
47789Sahrens 	err = zap_lookup(dp->dp_meta_objset,
48789Sahrens 	    dp->dp_root_dir->dd_phys->dd_child_dir_zapobj,
49789Sahrens 	    MOS_DIR_NAME, sizeof (obj), 1, &obj);
501544Seschrock 	if (err)
511544Seschrock 		return (err);
52789Sahrens 
531544Seschrock 	return (dsl_dir_open_obj(dp, obj, MOS_DIR_NAME, dp, ddp));
54789Sahrens }
55789Sahrens 
56789Sahrens static dsl_pool_t *
57789Sahrens dsl_pool_open_impl(spa_t *spa, uint64_t txg)
58789Sahrens {
59789Sahrens 	dsl_pool_t *dp;
60789Sahrens 	blkptr_t *bp = spa_get_rootblkptr(spa);
61789Sahrens 
62789Sahrens 	dp = kmem_zalloc(sizeof (dsl_pool_t), KM_SLEEP);
63789Sahrens 	dp->dp_spa = spa;
64789Sahrens 	dp->dp_meta_rootbp = *bp;
65789Sahrens 	txg_init(dp, txg);
66789Sahrens 
67789Sahrens 	txg_list_create(&dp->dp_dirty_datasets,
68789Sahrens 	    offsetof(dsl_dataset_t, ds_dirty_link));
69789Sahrens 	txg_list_create(&dp->dp_dirty_dirs,
70789Sahrens 	    offsetof(dsl_dir_t, dd_dirty_link));
71789Sahrens 	list_create(&dp->dp_synced_objsets, sizeof (dsl_dataset_t),
72789Sahrens 	    offsetof(dsl_dataset_t, ds_synced_link));
73789Sahrens 
74789Sahrens 	return (dp);
75789Sahrens }
76789Sahrens 
771544Seschrock int
781544Seschrock dsl_pool_open(spa_t *spa, uint64_t txg, dsl_pool_t **dpp)
79789Sahrens {
80789Sahrens 	int err;
81789Sahrens 	dsl_pool_t *dp = dsl_pool_open_impl(spa, txg);
821544Seschrock 	objset_impl_t *osi;
83789Sahrens 
84789Sahrens 	rw_enter(&dp->dp_config_rwlock, RW_READER);
851544Seschrock 	err = dmu_objset_open_impl(spa, NULL, &dp->dp_meta_rootbp, &osi);
861544Seschrock 	if (err)
871544Seschrock 		goto out;
881544Seschrock 	dp->dp_meta_objset = &osi->os;
891544Seschrock 
90789Sahrens 	err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
91789Sahrens 	    DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1,
92789Sahrens 	    &dp->dp_root_dir_obj);
931544Seschrock 	if (err)
941544Seschrock 		goto out;
951544Seschrock 
961544Seschrock 	err = dsl_dir_open_obj(dp, dp->dp_root_dir_obj,
971544Seschrock 	    NULL, dp, &dp->dp_root_dir);
981544Seschrock 	if (err)
991544Seschrock 		goto out;
100789Sahrens 
1011544Seschrock 	err = dsl_pool_open_mos_dir(dp, &dp->dp_mos_dir);
1021544Seschrock 	if (err)
1031544Seschrock 		goto out;
1041544Seschrock 
1051544Seschrock out:
106789Sahrens 	rw_exit(&dp->dp_config_rwlock);
1071544Seschrock 	if (err)
1081544Seschrock 		dsl_pool_close(dp);
1091544Seschrock 	else
1101544Seschrock 		*dpp = dp;
111789Sahrens 
1121544Seschrock 	return (err);
113789Sahrens }
114789Sahrens 
115789Sahrens void
116789Sahrens dsl_pool_close(dsl_pool_t *dp)
117789Sahrens {
118789Sahrens 	/* drop our reference from dsl_pool_open() */
1191544Seschrock 	if (dp->dp_mos_dir)
1201544Seschrock 		dsl_dir_close(dp->dp_mos_dir, dp);
1211544Seschrock 	if (dp->dp_root_dir)
1221544Seschrock 		dsl_dir_close(dp->dp_root_dir, dp);
123789Sahrens 
124789Sahrens 	/* undo the dmu_objset_open_impl(mos) from dsl_pool_open() */
1251544Seschrock 	if (dp->dp_meta_objset)
1261544Seschrock 		dmu_objset_evict(NULL, dp->dp_meta_objset->os);
127789Sahrens 
128789Sahrens 	txg_list_destroy(&dp->dp_dirty_datasets);
129789Sahrens 	txg_list_destroy(&dp->dp_dirty_dirs);
130789Sahrens 	list_destroy(&dp->dp_synced_objsets);
131789Sahrens 
132789Sahrens 	arc_flush();
133789Sahrens 	txg_fini(dp);
134789Sahrens 	kmem_free(dp, sizeof (dsl_pool_t));
135789Sahrens }
136789Sahrens 
137789Sahrens dsl_pool_t *
138789Sahrens dsl_pool_create(spa_t *spa, uint64_t txg)
139789Sahrens {
140789Sahrens 	int err;
141789Sahrens 	dsl_pool_t *dp = dsl_pool_open_impl(spa, txg);
142789Sahrens 	dmu_tx_t *tx = dmu_tx_create_assigned(dp, txg);
143789Sahrens 	dp->dp_meta_objset = &dmu_objset_create_impl(spa,
144789Sahrens 	    NULL, DMU_OST_META, tx)->os;
145789Sahrens 
146789Sahrens 	/* create the pool directory */
147789Sahrens 	err = zap_create_claim(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
148789Sahrens 	    DMU_OT_OBJECT_DIRECTORY, DMU_OT_NONE, 0, tx);
149789Sahrens 	ASSERT3U(err, ==, 0);
150789Sahrens 
151789Sahrens 	/* create and open the root dir */
152789Sahrens 	dsl_dataset_create_root(dp, &dp->dp_root_dir_obj, tx);
1531544Seschrock 	VERIFY(0 == dsl_dir_open_obj(dp, dp->dp_root_dir_obj,
1541544Seschrock 	    NULL, dp, &dp->dp_root_dir));
155789Sahrens 
156789Sahrens 	/* create and open the meta-objset dir */
1571544Seschrock 	VERIFY(0 == dsl_dir_create_sync(dp->dp_root_dir, MOS_DIR_NAME, tx));
158789Sahrens 	ASSERT3U(err, ==, 0);
1591544Seschrock 	VERIFY(0 == dsl_pool_open_mos_dir(dp, &dp->dp_mos_dir));
160789Sahrens 
161789Sahrens 	dmu_tx_commit(tx);
162789Sahrens 
163789Sahrens 	return (dp);
164789Sahrens }
165789Sahrens 
166789Sahrens void
167789Sahrens dsl_pool_sync(dsl_pool_t *dp, uint64_t txg)
168789Sahrens {
169789Sahrens 	dmu_tx_t *tx;
170789Sahrens 	objset_impl_t *mosi = dp->dp_meta_objset->os;
171789Sahrens 
172789Sahrens 	tx = dmu_tx_create_assigned(dp, txg);
173789Sahrens 
174789Sahrens 	do {
175789Sahrens 		dsl_dir_t *dd;
176789Sahrens 		dsl_dataset_t *ds;
177789Sahrens 
178789Sahrens 		while (ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) {
179789Sahrens 			if (!list_link_active(&ds->ds_synced_link))
180789Sahrens 				list_insert_tail(&dp->dp_synced_objsets, ds);
181789Sahrens 			dsl_dataset_sync(ds, tx);
182789Sahrens 		}
183789Sahrens 		while (dd = txg_list_remove(&dp->dp_dirty_dirs, txg))
184789Sahrens 			dsl_dir_sync(dd, tx);
185789Sahrens 		/*
186789Sahrens 		 * We need to loop since dsl_dir_sync() could create a
187789Sahrens 		 * new (dirty) objset.
188789Sahrens 		 * XXX - isn't this taken care of by the spa's sync to
189789Sahrens 		 * convergence loop?
190789Sahrens 		 */
191789Sahrens 	} while (!txg_list_empty(&dp->dp_dirty_datasets, txg));
192789Sahrens 
193789Sahrens 	if (list_head(&mosi->os_dirty_dnodes[txg & TXG_MASK]) != NULL ||
194789Sahrens 	    list_head(&mosi->os_free_dnodes[txg & TXG_MASK]) != NULL) {
195789Sahrens 		dmu_objset_sync(mosi, tx);
196789Sahrens 		dprintf_bp(&dp->dp_meta_rootbp, "meta objset rootbp is %s", "");
197789Sahrens 		spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp);
198789Sahrens 	}
199789Sahrens 
200789Sahrens 	dmu_tx_commit(tx);
201789Sahrens }
202789Sahrens 
203789Sahrens void
204789Sahrens dsl_pool_zil_clean(dsl_pool_t *dp)
205789Sahrens {
206789Sahrens 	dsl_dataset_t *ds;
207789Sahrens 
208789Sahrens 	while (ds = list_head(&dp->dp_synced_objsets)) {
209789Sahrens 		list_remove(&dp->dp_synced_objsets, ds);
210789Sahrens 		ASSERT(ds->ds_user_ptr != NULL);
211789Sahrens 		zil_clean(((objset_impl_t *)ds->ds_user_ptr)->os_zil);
212789Sahrens 	}
213789Sahrens }
214789Sahrens 
215789Sahrens int
216789Sahrens dsl_pool_sync_context(dsl_pool_t *dp)
217789Sahrens {
218789Sahrens 	/*
219789Sahrens 	 * Yeah, this is cheesy.  But the SPA needs some way to let
220789Sahrens 	 * the sync threads invoke spa_open() and spa_close() while
221789Sahrens 	 * it holds the namespace lock.  I'm certainly open to better
222789Sahrens 	 * ideas for how to determine whether the current thread is
223789Sahrens 	 * operating on behalf of spa_sync().  This works for now.
224789Sahrens 	 */
225789Sahrens 	return (curthread == dp->dp_tx.tx_sync_thread ||
226789Sahrens 	    BP_IS_HOLE(&dp->dp_meta_rootbp));
227789Sahrens }
228789Sahrens 
229789Sahrens uint64_t
230789Sahrens dsl_pool_adjustedsize(dsl_pool_t *dp, boolean_t netfree)
231789Sahrens {
232789Sahrens 	uint64_t space, resv;
233789Sahrens 
234789Sahrens 	/*
2351775Sbillm 	 * Reserve about 1.6% (1/64), or at least 32MB, for allocation
236789Sahrens 	 * efficiency.
237789Sahrens 	 * XXX The intent log is not accounted for, so it must fit
238789Sahrens 	 * within this slop.
239789Sahrens 	 *
240789Sahrens 	 * If we're trying to assess whether it's OK to do a free,
241789Sahrens 	 * cut the reservation in half to allow forward progress
242789Sahrens 	 * (e.g. make it possible to rm(1) files from a full pool).
243789Sahrens 	 */
244*2082Seschrock 	space = spa_get_dspace(dp->dp_spa);
2451775Sbillm 	resv = MAX(space >> 6, SPA_MINDEVSIZE >> 1);
246789Sahrens 	if (netfree)
247789Sahrens 		resv >>= 1;
248789Sahrens 
249789Sahrens 	return (space - resv);
250789Sahrens }
251