xref: /illumos-gate/usr/src/uts/common/fs/zfs/dsl_pool.c (revision 15f66a7f2650f57ff45e30a01a5324ad3e96aaa8)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/dsl_pool.h>
29 #include <sys/dsl_dataset.h>
30 #include <sys/dsl_dir.h>
31 #include <sys/dsl_synctask.h>
32 #include <sys/dmu_tx.h>
33 #include <sys/dmu_objset.h>
34 #include <sys/arc.h>
35 #include <sys/zap.h>
36 #include <sys/zfs_context.h>
37 #include <sys/fs/zfs.h>
38 
39 static int
40 dsl_pool_open_mos_dir(dsl_pool_t *dp, dsl_dir_t **ddp)
41 {
42 	uint64_t obj;
43 	int err;
44 
45 	err = zap_lookup(dp->dp_meta_objset,
46 	    dp->dp_root_dir->dd_phys->dd_child_dir_zapobj,
47 	    MOS_DIR_NAME, sizeof (obj), 1, &obj);
48 	if (err)
49 		return (err);
50 
51 	return (dsl_dir_open_obj(dp, obj, MOS_DIR_NAME, dp, ddp));
52 }
53 
54 static dsl_pool_t *
55 dsl_pool_open_impl(spa_t *spa, uint64_t txg)
56 {
57 	dsl_pool_t *dp;
58 	blkptr_t *bp = spa_get_rootblkptr(spa);
59 
60 	dp = kmem_zalloc(sizeof (dsl_pool_t), KM_SLEEP);
61 	dp->dp_spa = spa;
62 	dp->dp_meta_rootbp = *bp;
63 	txg_init(dp, txg);
64 
65 	txg_list_create(&dp->dp_dirty_datasets,
66 	    offsetof(dsl_dataset_t, ds_dirty_link));
67 	txg_list_create(&dp->dp_dirty_dirs,
68 	    offsetof(dsl_dir_t, dd_dirty_link));
69 	txg_list_create(&dp->dp_sync_tasks,
70 	    offsetof(dsl_sync_task_group_t, dstg_node));
71 	list_create(&dp->dp_synced_objsets, sizeof (dsl_dataset_t),
72 	    offsetof(dsl_dataset_t, ds_synced_link));
73 
74 	return (dp);
75 }
76 
77 int
78 dsl_pool_open(spa_t *spa, uint64_t txg, dsl_pool_t **dpp)
79 {
80 	int err;
81 	dsl_pool_t *dp = dsl_pool_open_impl(spa, txg);
82 	objset_impl_t *osi;
83 
84 	rw_enter(&dp->dp_config_rwlock, RW_READER);
85 	err = dmu_objset_open_impl(spa, NULL, &dp->dp_meta_rootbp, &osi);
86 	if (err)
87 		goto out;
88 	dp->dp_meta_objset = &osi->os;
89 
90 	err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
91 	    DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1,
92 	    &dp->dp_root_dir_obj);
93 	if (err)
94 		goto out;
95 
96 	err = dsl_dir_open_obj(dp, dp->dp_root_dir_obj,
97 	    NULL, dp, &dp->dp_root_dir);
98 	if (err)
99 		goto out;
100 
101 	err = dsl_pool_open_mos_dir(dp, &dp->dp_mos_dir);
102 	if (err)
103 		goto out;
104 
105 out:
106 	rw_exit(&dp->dp_config_rwlock);
107 	if (err)
108 		dsl_pool_close(dp);
109 	else
110 		*dpp = dp;
111 
112 	return (err);
113 }
114 
115 void
116 dsl_pool_close(dsl_pool_t *dp)
117 {
118 	/* drop our reference from dsl_pool_open() */
119 	if (dp->dp_mos_dir)
120 		dsl_dir_close(dp->dp_mos_dir, dp);
121 	if (dp->dp_root_dir)
122 		dsl_dir_close(dp->dp_root_dir, dp);
123 
124 	/* undo the dmu_objset_open_impl(mos) from dsl_pool_open() */
125 	if (dp->dp_meta_objset)
126 		dmu_objset_evict(NULL, dp->dp_meta_objset->os);
127 
128 	txg_list_destroy(&dp->dp_dirty_datasets);
129 	txg_list_destroy(&dp->dp_dirty_dirs);
130 	list_destroy(&dp->dp_synced_objsets);
131 
132 	arc_flush();
133 	txg_fini(dp);
134 	kmem_free(dp, sizeof (dsl_pool_t));
135 }
136 
137 dsl_pool_t *
138 dsl_pool_create(spa_t *spa, uint64_t txg)
139 {
140 	int err;
141 	dsl_pool_t *dp = dsl_pool_open_impl(spa, txg);
142 	dmu_tx_t *tx = dmu_tx_create_assigned(dp, txg);
143 	dp->dp_meta_objset = &dmu_objset_create_impl(spa,
144 	    NULL, DMU_OST_META, tx)->os;
145 
146 	/* create the pool directory */
147 	err = zap_create_claim(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
148 	    DMU_OT_OBJECT_DIRECTORY, DMU_OT_NONE, 0, tx);
149 	ASSERT3U(err, ==, 0);
150 
151 	/* create and open the root dir */
152 	dsl_dataset_create_root(dp, &dp->dp_root_dir_obj, tx);
153 	VERIFY(0 == dsl_dir_open_obj(dp, dp->dp_root_dir_obj,
154 	    NULL, dp, &dp->dp_root_dir));
155 
156 	/* create and open the meta-objset dir */
157 	(void) dsl_dir_create_sync(dp->dp_root_dir, MOS_DIR_NAME, tx);
158 	VERIFY(0 == dsl_pool_open_mos_dir(dp, &dp->dp_mos_dir));
159 
160 	dmu_tx_commit(tx);
161 
162 	return (dp);
163 }
164 
165 void
166 dsl_pool_sync(dsl_pool_t *dp, uint64_t txg)
167 {
168 	dmu_tx_t *tx;
169 	objset_impl_t *mosi = dp->dp_meta_objset->os;
170 
171 	tx = dmu_tx_create_assigned(dp, txg);
172 
173 	do {
174 		dsl_dir_t *dd;
175 		dsl_dataset_t *ds;
176 		dsl_sync_task_group_t *dstg;
177 
178 		while (ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) {
179 			if (!list_link_active(&ds->ds_synced_link))
180 				list_insert_tail(&dp->dp_synced_objsets, ds);
181 			dsl_dataset_sync(ds, tx);
182 		}
183 		while (dstg = txg_list_remove(&dp->dp_sync_tasks, txg))
184 			dsl_sync_task_group_sync(dstg, tx);
185 		while (dd = txg_list_remove(&dp->dp_dirty_dirs, txg))
186 			dsl_dir_sync(dd, tx);
187 		/*
188 		 * We need to loop since dsl_sync_task_group_sync()
189 		 * could create a new (dirty) objset.
190 		 * XXX - isn't this taken care of by the spa's sync to
191 		 * convergence loop?
192 		 */
193 	} while (!txg_list_empty(&dp->dp_dirty_datasets, txg));
194 
195 	if (list_head(&mosi->os_dirty_dnodes[txg & TXG_MASK]) != NULL ||
196 	    list_head(&mosi->os_free_dnodes[txg & TXG_MASK]) != NULL) {
197 		dmu_objset_sync(mosi, tx);
198 		dprintf_bp(&dp->dp_meta_rootbp, "meta objset rootbp is %s", "");
199 		spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp);
200 	}
201 
202 	dmu_tx_commit(tx);
203 }
204 
205 void
206 dsl_pool_zil_clean(dsl_pool_t *dp)
207 {
208 	dsl_dataset_t *ds;
209 
210 	while (ds = list_head(&dp->dp_synced_objsets)) {
211 		list_remove(&dp->dp_synced_objsets, ds);
212 		ASSERT(ds->ds_user_ptr != NULL);
213 		zil_clean(((objset_impl_t *)ds->ds_user_ptr)->os_zil);
214 	}
215 }
216 
217 int
218 dsl_pool_sync_context(dsl_pool_t *dp)
219 {
220 	/*
221 	 * Yeah, this is cheesy.  But the SPA needs some way to let
222 	 * the sync threads invoke spa_open() and spa_close() while
223 	 * it holds the namespace lock.  I'm certainly open to better
224 	 * ideas for how to determine whether the current thread is
225 	 * operating on behalf of spa_sync().  This works for now.
226 	 */
227 	return (curthread == dp->dp_tx.tx_sync_thread ||
228 	    BP_IS_HOLE(&dp->dp_meta_rootbp));
229 }
230 
231 uint64_t
232 dsl_pool_adjustedsize(dsl_pool_t *dp, boolean_t netfree)
233 {
234 	uint64_t space, resv;
235 
236 	/*
237 	 * Reserve about 1.6% (1/64), or at least 32MB, for allocation
238 	 * efficiency.
239 	 * XXX The intent log is not accounted for, so it must fit
240 	 * within this slop.
241 	 *
242 	 * If we're trying to assess whether it's OK to do a free,
243 	 * cut the reservation in half to allow forward progress
244 	 * (e.g. make it possible to rm(1) files from a full pool).
245 	 */
246 	space = spa_get_dspace(dp->dp_spa);
247 	resv = MAX(space >> 6, SPA_MINDEVSIZE >> 1);
248 	if (netfree)
249 		resv >>= 1;
250 
251 	return (space - resv);
252 }
253