1 /*
2  * Copyright (c) 2007-2008 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * $DragonFly: src/sys/vfs/hammer/hammer_transaction.c,v 1.25 2008/09/23 21:03:52 dillon Exp $
35  */
36 
37 #include "hammer.h"
38 
39 static u_int32_t ocp_allocbit(hammer_objid_cache_t ocp, u_int32_t n);
40 
41 
42 /*
43  * Start a standard transaction.
44  */
45 void
46 hammer_start_transaction(struct hammer_transaction *trans,
47 			 struct hammer_mount *hmp)
48 {
49 	struct timeval tv;
50 	int error;
51 
52 	trans->type = HAMMER_TRANS_STD;
53 	trans->hmp = hmp;
54 	trans->rootvol = hammer_get_root_volume(hmp, &error);
55 	KKASSERT(error == 0);
56 	trans->tid = 0;
57 	trans->sync_lock_refs = 0;
58 	trans->flags = 0;
59 
60 	getmicrotime(&tv);
61 	trans->time = (unsigned long)tv.tv_sec * 1000000ULL + tv.tv_usec;
62 	trans->time32 = (u_int32_t)tv.tv_sec;
63 }
64 
65 /*
66  * Start a simple read-only transaction.  This will not stall.
67  */
68 void
69 hammer_simple_transaction(struct hammer_transaction *trans,
70 			  struct hammer_mount *hmp)
71 {
72 	struct timeval tv;
73 	int error;
74 
75 	trans->type = HAMMER_TRANS_RO;
76 	trans->hmp = hmp;
77 	trans->rootvol = hammer_get_root_volume(hmp, &error);
78 	KKASSERT(error == 0);
79 	trans->tid = 0;
80 	trans->sync_lock_refs = 0;
81 	trans->flags = 0;
82 
83 	getmicrotime(&tv);
84 	trans->time = (unsigned long)tv.tv_sec * 1000000ULL + tv.tv_usec;
85 	trans->time32 = (u_int32_t)tv.tv_sec;
86 }
87 
88 /*
89  * Start a transaction using a particular TID.  Used by the sync code.
90  * This does not stall.
91  *
92  * This routine may only be called from the flusher thread.  We predispose
93  * sync_lock_refs, implying serialization against the synchronization stage
94  * (which the flusher is responsible for).
95  */
96 void
97 hammer_start_transaction_fls(struct hammer_transaction *trans,
98 			     struct hammer_mount *hmp)
99 {
100 	struct timeval tv;
101 	int error;
102 
103 	bzero(trans, sizeof(*trans));
104 
105 	trans->type = HAMMER_TRANS_FLS;
106 	trans->hmp = hmp;
107 	trans->rootvol = hammer_get_root_volume(hmp, &error);
108 	KKASSERT(error == 0);
109 	trans->tid = hammer_alloc_tid(hmp, 1);
110 	trans->sync_lock_refs = 1;
111 	trans->flags = 0;
112 
113 	getmicrotime(&tv);
114 	trans->time = (unsigned long)tv.tv_sec * 1000000ULL + tv.tv_usec;
115 	trans->time32 = (u_int32_t)tv.tv_sec;
116 }
117 
118 void
119 hammer_done_transaction(struct hammer_transaction *trans)
120 {
121 	hammer_mount_t hmp = trans->hmp;
122 	int expected_lock_refs;
123 
124 	hammer_rel_volume(trans->rootvol, 0);
125 	trans->rootvol = NULL;
126 	expected_lock_refs = (trans->type == HAMMER_TRANS_FLS) ? 1 : 0;
127 	KKASSERT(trans->sync_lock_refs == expected_lock_refs);
128 	trans->sync_lock_refs = 0;
129 	if (trans->type != HAMMER_TRANS_FLS) {
130 		if (trans->flags & HAMMER_TRANSF_NEWINODE)
131 			hammer_inode_waitreclaims(hmp);
132 		else if (trans->flags & HAMMER_TRANSF_DIDIO)
133 			hammer_inode_waitreclaims(hmp);
134 	}
135 }
136 
137 /*
138  * Allocate (count) TIDs.  If running in multi-master mode the returned
139  * base will be aligned to a 16-count plus the master id (0-15).
140  * Multi-master mode allows non-conflicting to run and new objects to be
141  * created on multiple masters in parallel.  The transaction id identifies
142  * the original master.  The object_id is also subject to this rule in
143  * order to allow objects to be created on multiple masters in parallel.
144  *
145  * Directories may pre-allocate a large number of object ids (100,000).
146  *
147  * NOTE: There is no longer a requirement that successive transaction
148  *	 ids be 2 apart for separator generation.
149  *
150  * NOTE: When called by pseudo-backends such as ioctls the allocated
151  *	 TID will be larger then the current flush TID, if a flush is running,
152  *	 so any mirroring will pick the records up on a later flush.
153  */
154 hammer_tid_t
155 hammer_alloc_tid(hammer_mount_t hmp, int count)
156 {
157 	hammer_tid_t tid;
158 
159 	if (hmp->master_id < 0) {
160 		tid = hmp->next_tid + 1;
161 		hmp->next_tid = tid + count;
162 	} else {
163 		tid = (hmp->next_tid + HAMMER_MAX_MASTERS) &
164 		      ~(hammer_tid_t)(HAMMER_MAX_MASTERS - 1);
165 		hmp->next_tid = tid + count * HAMMER_MAX_MASTERS;
166 		tid |= hmp->master_id;
167 	}
168 	if (tid >= 0xFFFFFFFFFF000000ULL)
169 		panic("hammer_start_transaction: Ran out of TIDs!");
170 	if (hammer_debug_tid)
171 		kprintf("alloc_tid %016llx\n", (long long)tid);
172 	return(tid);
173 }
174 
175 /*
176  * Allocate an object id.
177  *
178  * We use the upper OBJID_CACHE_BITS bits of the namekey to try to match
179  * the low bits of the objid we allocate.
180  */
181 hammer_tid_t
182 hammer_alloc_objid(hammer_mount_t hmp, hammer_inode_t dip, int64_t namekey)
183 {
184 	hammer_objid_cache_t ocp;
185 	hammer_tid_t tid;
186 	int incluster;
187 	u_int32_t n;
188 
189 	while ((ocp = dip->objid_cache) == NULL) {
190 		if (hmp->objid_cache_count < OBJID_CACHE_SIZE) {
191 			ocp = kmalloc(sizeof(*ocp), hmp->m_misc,
192 				      M_WAITOK|M_ZERO);
193 			ocp->base_tid = hammer_alloc_tid(hmp,
194 							OBJID_CACHE_BULK * 2);
195 			ocp->base_tid += OBJID_CACHE_BULK_MASK64;
196 			ocp->base_tid &= ~OBJID_CACHE_BULK_MASK64;
197 			TAILQ_INSERT_HEAD(&hmp->objid_cache_list, ocp, entry);
198 			++hmp->objid_cache_count;
199 			/* may have blocked, recheck */
200 			if (dip->objid_cache == NULL) {
201 				dip->objid_cache = ocp;
202 				ocp->dip = dip;
203 			}
204 		} else {
205 			/*
206 			 * Steal one from another directory?
207 			 *
208 			 * Throw away ocp's that are more then half full, they
209 			 * aren't worth stealing.
210 			 */
211 			ocp = TAILQ_FIRST(&hmp->objid_cache_list);
212 			if (ocp->dip)
213 				ocp->dip->objid_cache = NULL;
214 			if (ocp->count >= OBJID_CACHE_BULK / 2) {
215 				--hmp->objid_cache_count;
216 				kfree(ocp, hmp->m_misc);
217 			} else {
218 				dip->objid_cache = ocp;
219 				ocp->dip = dip;
220 			}
221 		}
222 	}
223 	TAILQ_REMOVE(&hmp->objid_cache_list, ocp, entry);
224 
225 	/*
226 	 * Allocate a bit based on our namekey for the low bits of our
227 	 * objid.
228 	 */
229 	incluster = (hmp->master_id >= 0);
230 	n = (namekey >> (63 - OBJID_CACHE_BULK_BITS)) & OBJID_CACHE_BULK_MASK;
231 	n = ocp_allocbit(ocp, n);
232 	tid = ocp->base_tid + n;
233 
234 #if 0
235 	/*
236 	 * The TID is incremented by 1 or by 16 depending what mode the
237 	 * mount is operating in.
238 	 */
239 	ocp->next_tid += (hmp->master_id < 0) ? 1 : HAMMER_MAX_MASTERS;
240 #endif
241 	if (ocp->count >= OBJID_CACHE_BULK / 2) {
242 		dip->objid_cache = NULL;
243 		--hmp->objid_cache_count;
244 		ocp->dip = NULL;
245 		kfree(ocp, hmp->m_misc);
246 	} else {
247 		TAILQ_INSERT_TAIL(&hmp->objid_cache_list, ocp, entry);
248 	}
249 	return(tid);
250 }
251 
252 /*
253  * Allocate a bit starting with bit n.  Wrap if necessary.
254  *
255  * This routine is only ever called if a bit is available somewhere
256  * in the bitmap.
257  */
258 static u_int32_t
259 ocp_allocbit(hammer_objid_cache_t ocp, u_int32_t n)
260 {
261 	u_int32_t n0;
262 
263 	n0 = (n >> 5) & 31;
264 	n &= 31;
265 
266 	while (ocp->bm1[n0] & (1 << n)) {
267 		if (ocp->bm0 & (1 << n0)) {
268 			n0 = (n0 + 1) & 31;
269 			n = 0;
270 		} else if (++n == 32) {
271 			n0 = (n0 + 1) & 31;
272 			n = 0;
273 		}
274 	}
275 	++ocp->count;
276 	ocp->bm1[n0] |= 1 << n;
277 	if (ocp->bm1[n0] == 0xFFFFFFFFU)
278 		ocp->bm0 |= 1 << n0;
279 	return((n0 << 5) + n);
280 }
281 
282 void
283 hammer_clear_objid(hammer_inode_t dip)
284 {
285 	hammer_objid_cache_t ocp;
286 
287 	if ((ocp = dip->objid_cache) != NULL) {
288 		dip->objid_cache = NULL;
289 		ocp->dip = NULL;
290 		TAILQ_REMOVE(&dip->hmp->objid_cache_list, ocp, entry);
291 		TAILQ_INSERT_HEAD(&dip->hmp->objid_cache_list, ocp, entry);
292 	}
293 }
294 
295 void
296 hammer_destroy_objid_cache(hammer_mount_t hmp)
297 {
298 	hammer_objid_cache_t ocp;
299 
300 	while ((ocp = TAILQ_FIRST(&hmp->objid_cache_list)) != NULL) {
301 		TAILQ_REMOVE(&hmp->objid_cache_list, ocp, entry);
302 		if (ocp->dip)
303 			ocp->dip->objid_cache = NULL;
304 		kfree(ocp, hmp->m_misc);
305 	}
306 }
307 
308