xref: /titanic_52/usr/src/uts/common/fs/zfs/dsl_pool.c (revision f841f6ad96ea6675d6c6b35c749eaac601799fdf)
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 /* internal reserved dir name */
40 #define	MOS_DIR_NAME "$MOS"
41 
42 static int
43 dsl_pool_open_mos_dir(dsl_pool_t *dp, dsl_dir_t **ddp)
44 {
45 	uint64_t obj;
46 	int err;
47 
48 	err = zap_lookup(dp->dp_meta_objset,
49 	    dp->dp_root_dir->dd_phys->dd_child_dir_zapobj,
50 	    MOS_DIR_NAME, sizeof (obj), 1, &obj);
51 	if (err)
52 		return (err);
53 
54 	return (dsl_dir_open_obj(dp, obj, MOS_DIR_NAME, dp, ddp));
55 }
56 
57 static dsl_pool_t *
58 dsl_pool_open_impl(spa_t *spa, uint64_t txg)
59 {
60 	dsl_pool_t *dp;
61 	blkptr_t *bp = spa_get_rootblkptr(spa);
62 
63 	dp = kmem_zalloc(sizeof (dsl_pool_t), KM_SLEEP);
64 	dp->dp_spa = spa;
65 	dp->dp_meta_rootbp = *bp;
66 	txg_init(dp, txg);
67 
68 	txg_list_create(&dp->dp_dirty_datasets,
69 	    offsetof(dsl_dataset_t, ds_dirty_link));
70 	txg_list_create(&dp->dp_dirty_dirs,
71 	    offsetof(dsl_dir_t, dd_dirty_link));
72 	txg_list_create(&dp->dp_sync_tasks,
73 	    offsetof(dsl_sync_task_group_t, dstg_node));
74 	list_create(&dp->dp_synced_objsets, sizeof (dsl_dataset_t),
75 	    offsetof(dsl_dataset_t, ds_synced_link));
76 
77 	return (dp);
78 }
79 
80 int
81 dsl_pool_open(spa_t *spa, uint64_t txg, dsl_pool_t **dpp)
82 {
83 	int err;
84 	dsl_pool_t *dp = dsl_pool_open_impl(spa, txg);
85 	objset_impl_t *osi;
86 
87 	rw_enter(&dp->dp_config_rwlock, RW_READER);
88 	err = dmu_objset_open_impl(spa, NULL, &dp->dp_meta_rootbp, &osi);
89 	if (err)
90 		goto out;
91 	dp->dp_meta_objset = &osi->os;
92 
93 	err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
94 	    DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1,
95 	    &dp->dp_root_dir_obj);
96 	if (err)
97 		goto out;
98 
99 	err = dsl_dir_open_obj(dp, dp->dp_root_dir_obj,
100 	    NULL, dp, &dp->dp_root_dir);
101 	if (err)
102 		goto out;
103 
104 	err = dsl_pool_open_mos_dir(dp, &dp->dp_mos_dir);
105 	if (err)
106 		goto out;
107 
108 out:
109 	rw_exit(&dp->dp_config_rwlock);
110 	if (err)
111 		dsl_pool_close(dp);
112 	else
113 		*dpp = dp;
114 
115 	return (err);
116 }
117 
118 void
119 dsl_pool_close(dsl_pool_t *dp)
120 {
121 	/* drop our reference from dsl_pool_open() */
122 	if (dp->dp_mos_dir)
123 		dsl_dir_close(dp->dp_mos_dir, dp);
124 	if (dp->dp_root_dir)
125 		dsl_dir_close(dp->dp_root_dir, dp);
126 
127 	/* undo the dmu_objset_open_impl(mos) from dsl_pool_open() */
128 	if (dp->dp_meta_objset)
129 		dmu_objset_evict(NULL, dp->dp_meta_objset->os);
130 
131 	txg_list_destroy(&dp->dp_dirty_datasets);
132 	txg_list_destroy(&dp->dp_dirty_dirs);
133 	list_destroy(&dp->dp_synced_objsets);
134 
135 	arc_flush();
136 	txg_fini(dp);
137 	kmem_free(dp, sizeof (dsl_pool_t));
138 }
139 
140 dsl_pool_t *
141 dsl_pool_create(spa_t *spa, uint64_t txg)
142 {
143 	int err;
144 	dsl_pool_t *dp = dsl_pool_open_impl(spa, txg);
145 	dmu_tx_t *tx = dmu_tx_create_assigned(dp, txg);
146 	dp->dp_meta_objset = &dmu_objset_create_impl(spa,
147 	    NULL, DMU_OST_META, tx)->os;
148 
149 	/* create the pool directory */
150 	err = zap_create_claim(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
151 	    DMU_OT_OBJECT_DIRECTORY, DMU_OT_NONE, 0, tx);
152 	ASSERT3U(err, ==, 0);
153 
154 	/* create and open the root dir */
155 	dsl_dataset_create_root(dp, &dp->dp_root_dir_obj, tx);
156 	VERIFY(0 == dsl_dir_open_obj(dp, dp->dp_root_dir_obj,
157 	    NULL, dp, &dp->dp_root_dir));
158 
159 	/* create and open the meta-objset dir */
160 	(void) dsl_dir_create_sync(dp->dp_root_dir, MOS_DIR_NAME, tx);
161 	VERIFY(0 == dsl_pool_open_mos_dir(dp, &dp->dp_mos_dir));
162 
163 	dmu_tx_commit(tx);
164 
165 	return (dp);
166 }
167 
168 void
169 dsl_pool_sync(dsl_pool_t *dp, uint64_t txg)
170 {
171 	dmu_tx_t *tx;
172 	objset_impl_t *mosi = dp->dp_meta_objset->os;
173 
174 	tx = dmu_tx_create_assigned(dp, txg);
175 
176 	do {
177 		dsl_dir_t *dd;
178 		dsl_dataset_t *ds;
179 		dsl_sync_task_group_t *dstg;
180 
181 		while (ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) {
182 			if (!list_link_active(&ds->ds_synced_link))
183 				list_insert_tail(&dp->dp_synced_objsets, ds);
184 			dsl_dataset_sync(ds, tx);
185 		}
186 		while (dstg = txg_list_remove(&dp->dp_sync_tasks, txg))
187 			dsl_sync_task_group_sync(dstg, tx);
188 		while (dd = txg_list_remove(&dp->dp_dirty_dirs, txg))
189 			dsl_dir_sync(dd, tx);
190 		/*
191 		 * We need to loop since dsl_sync_task_group_sync()
192 		 * could create a new (dirty) objset.
193 		 * XXX - isn't this taken care of by the spa's sync to
194 		 * convergence loop?
195 		 */
196 	} while (!txg_list_empty(&dp->dp_dirty_datasets, txg));
197 
198 	if (list_head(&mosi->os_dirty_dnodes[txg & TXG_MASK]) != NULL ||
199 	    list_head(&mosi->os_free_dnodes[txg & TXG_MASK]) != NULL) {
200 		dmu_objset_sync(mosi, tx);
201 		dprintf_bp(&dp->dp_meta_rootbp, "meta objset rootbp is %s", "");
202 		spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp);
203 	}
204 
205 	dmu_tx_commit(tx);
206 }
207 
208 void
209 dsl_pool_zil_clean(dsl_pool_t *dp)
210 {
211 	dsl_dataset_t *ds;
212 
213 	while (ds = list_head(&dp->dp_synced_objsets)) {
214 		list_remove(&dp->dp_synced_objsets, ds);
215 		ASSERT(ds->ds_user_ptr != NULL);
216 		zil_clean(((objset_impl_t *)ds->ds_user_ptr)->os_zil);
217 	}
218 }
219 
220 int
221 dsl_pool_sync_context(dsl_pool_t *dp)
222 {
223 	/*
224 	 * Yeah, this is cheesy.  But the SPA needs some way to let
225 	 * the sync threads invoke spa_open() and spa_close() while
226 	 * it holds the namespace lock.  I'm certainly open to better
227 	 * ideas for how to determine whether the current thread is
228 	 * operating on behalf of spa_sync().  This works for now.
229 	 */
230 	return (curthread == dp->dp_tx.tx_sync_thread ||
231 	    BP_IS_HOLE(&dp->dp_meta_rootbp));
232 }
233 
234 uint64_t
235 dsl_pool_adjustedsize(dsl_pool_t *dp, boolean_t netfree)
236 {
237 	uint64_t space, resv;
238 
239 	/*
240 	 * Reserve about 1.6% (1/64), or at least 32MB, for allocation
241 	 * efficiency.
242 	 * XXX The intent log is not accounted for, so it must fit
243 	 * within this slop.
244 	 *
245 	 * If we're trying to assess whether it's OK to do a free,
246 	 * cut the reservation in half to allow forward progress
247 	 * (e.g. make it possible to rm(1) files from a full pool).
248 	 */
249 	space = spa_get_dspace(dp->dp_spa);
250 	resv = MAX(space >> 6, SPA_MINDEVSIZE >> 1);
251 	if (netfree)
252 		resv >>= 1;
253 
254 	return (space - resv);
255 }
256