Merge branch 'vendor/TCSH'
[dragonfly.git] / sys / vfs / hammer / hammer_transaction.c
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
35 #include "hammer.h"
36
37 static u_int32_t ocp_allocbit(hammer_objid_cache_t ocp, u_int32_t n);
38
39
40 /*
41  * Start a standard transaction.
42  */
43 void
44 hammer_start_transaction(struct hammer_transaction *trans,
45                          struct hammer_mount *hmp)
46 {
47         struct timeval tv;
48         int error;
49
50         trans->type = HAMMER_TRANS_STD;
51         trans->hmp = hmp;
52         trans->rootvol = hammer_get_root_volume(hmp, &error);
53         KKASSERT(error == 0);
54         trans->tid = 0;
55         trans->sync_lock_refs = 0;
56         trans->flags = 0;
57
58         getmicrotime(&tv);
59         trans->time = (unsigned long)tv.tv_sec * 1000000ULL + tv.tv_usec;
60         trans->time32 = (u_int32_t)tv.tv_sec;
61 }
62
63 /*
64  * Start a simple read-only transaction.  This will not stall.
65  */
66 void
67 hammer_simple_transaction(struct hammer_transaction *trans,
68                           struct hammer_mount *hmp)
69 {
70         struct timeval tv;
71         int error;
72
73         trans->type = HAMMER_TRANS_RO;
74         trans->hmp = hmp;
75         trans->rootvol = hammer_get_root_volume(hmp, &error);
76         KKASSERT(error == 0);
77         trans->tid = 0;
78         trans->sync_lock_refs = 0;
79         trans->flags = 0;
80
81         getmicrotime(&tv);
82         trans->time = (unsigned long)tv.tv_sec * 1000000ULL + tv.tv_usec;
83         trans->time32 = (u_int32_t)tv.tv_sec;
84 }
85
86 /*
87  * Start a transaction using a particular TID.  Used by the sync code.
88  * This does not stall.
89  *
90  * This routine may only be called from the flusher thread.  We predispose
91  * sync_lock_refs, implying serialization against the synchronization stage
92  * (which the flusher is responsible for).
93  */
94 void
95 hammer_start_transaction_fls(struct hammer_transaction *trans,
96                              struct hammer_mount *hmp)
97 {
98         struct timeval tv;
99         int error;
100
101         bzero(trans, sizeof(*trans));
102
103         trans->type = HAMMER_TRANS_FLS;
104         trans->hmp = hmp;
105         trans->rootvol = hammer_get_root_volume(hmp, &error);
106         KKASSERT(error == 0);
107         trans->tid = hammer_alloc_tid(hmp, 1);
108         trans->sync_lock_refs = 1;
109         trans->flags = 0;
110
111         getmicrotime(&tv);
112         trans->time = (unsigned long)tv.tv_sec * 1000000ULL + tv.tv_usec;
113         trans->time32 = (u_int32_t)tv.tv_sec;
114 }
115
116 void
117 hammer_done_transaction(struct hammer_transaction *trans)
118 {
119         int expected_lock_refs __debugvar;
120
121         hammer_rel_volume(trans->rootvol, 0);
122         trans->rootvol = NULL;
123         expected_lock_refs = (trans->type == HAMMER_TRANS_FLS) ? 1 : 0;
124         KKASSERT(trans->sync_lock_refs == expected_lock_refs);
125         trans->sync_lock_refs = 0;
126         if (trans->type != HAMMER_TRANS_FLS) {
127                 if (trans->flags & HAMMER_TRANSF_NEWINODE)
128                         hammer_inode_waitreclaims(trans);
129                 /*
130                 else if (trans->flags & HAMMER_TRANSF_DIDIO)
131                         hammer_inode_waitreclaims(trans);
132                 */
133         }
134 }
135
136 /*
137  * Allocate (count) TIDs.  If running in multi-master mode the returned
138  * base will be aligned to a 16-count plus the master id (0-15).  
139  * Multi-master mode allows non-conflicting to run and new objects to be
140  * created on multiple masters in parallel.  The transaction id identifies
141  * the original master.  The object_id is also subject to this rule in
142  * order to allow objects to be created on multiple masters in parallel.
143  *
144  * Directories may pre-allocate a large number of object ids (100,000).
145  *
146  * NOTE: There is no longer a requirement that successive transaction
147  *       ids be 2 apart for separator generation.
148  *
149  * NOTE: When called by pseudo-backends such as ioctls the allocated
150  *       TID will be larger then the current flush TID, if a flush is running,
151  *       so any mirroring will pick the records up on a later flush.
152  */
153 hammer_tid_t
154 hammer_alloc_tid(hammer_mount_t hmp, int count)
155 {
156         hammer_tid_t tid;
157
158         if (hmp->master_id < 0) {
159                 tid = hmp->next_tid + 1;
160                 hmp->next_tid = tid + count;
161         } else {
162                 tid = (hmp->next_tid + HAMMER_MAX_MASTERS) &
163                       ~(hammer_tid_t)(HAMMER_MAX_MASTERS - 1);
164                 hmp->next_tid = tid + count * HAMMER_MAX_MASTERS;
165                 tid |= hmp->master_id;
166         }
167         if (tid >= 0xFFFFFFFFFF000000ULL)
168                 panic("hammer_start_transaction: Ran out of TIDs!");
169         if (hammer_debug_tid)
170                 kprintf("alloc_tid %016llx\n", (long long)tid);
171         return(tid);
172 }
173
174 /*
175  * Allocate an object id.
176  *
177  * We use the upper OBJID_CACHE_BITS bits of the namekey to try to match
178  * the low bits of the objid we allocate.
179  */
180 hammer_tid_t
181 hammer_alloc_objid(hammer_mount_t hmp, hammer_inode_t dip, int64_t namekey)
182 {
183         hammer_objid_cache_t ocp;
184         hammer_tid_t tid;
185         u_int32_t n;
186
187         while ((ocp = dip->objid_cache) == NULL) {
188                 if (hmp->objid_cache_count < OBJID_CACHE_SIZE) {
189                         ocp = kmalloc(sizeof(*ocp), hmp->m_misc,
190                                       M_WAITOK|M_ZERO);
191                         ocp->base_tid = hammer_alloc_tid(hmp,
192                                                         OBJID_CACHE_BULK * 2);
193                         ocp->base_tid += OBJID_CACHE_BULK_MASK64;
194                         ocp->base_tid &= ~OBJID_CACHE_BULK_MASK64;
195                         /* may have blocked, recheck */
196                         if (dip->objid_cache == NULL) {
197                                 TAILQ_INSERT_TAIL(&hmp->objid_cache_list,
198                                                   ocp, entry);
199                                 ++hmp->objid_cache_count;
200                                 dip->objid_cache = ocp;
201                                 ocp->dip = dip;
202                         } else {
203                                 kfree(ocp, hmp->m_misc);
204                         }
205                 } else {
206                         /*
207                          * Steal one from another directory?
208                          *
209                          * Throw away ocp's that are more then half full, they
210                          * aren't worth stealing.
211                          */
212                         ocp = TAILQ_FIRST(&hmp->objid_cache_list);
213                         if (ocp->dip)
214                                 ocp->dip->objid_cache = NULL;
215                         if (ocp->count >= OBJID_CACHE_BULK / 2) {
216                                 TAILQ_REMOVE(&hmp->objid_cache_list,
217                                              ocp, entry);
218                                 --hmp->objid_cache_count;
219                                 kfree(ocp, hmp->m_misc);
220                         } else {
221                                 dip->objid_cache = ocp;
222                                 ocp->dip = dip;
223                         }
224                 }
225         }
226         TAILQ_REMOVE(&hmp->objid_cache_list, ocp, entry);
227
228         /*
229          * Allocate inode numbers uniformly.
230          */
231
232         n = (namekey >> (63 - OBJID_CACHE_BULK_BITS)) & OBJID_CACHE_BULK_MASK;
233         n = ocp_allocbit(ocp, n);
234         tid = ocp->base_tid + n;
235
236 #if 0
237         /*
238          * The TID is incremented by 1 or by 16 depending what mode the
239          * mount is operating in.
240          */
241         ocp->next_tid += (hmp->master_id < 0) ? 1 : HAMMER_MAX_MASTERS;
242 #endif
243         if (ocp->count >= OBJID_CACHE_BULK * 3 / 4) {
244                 dip->objid_cache = NULL;
245                 --hmp->objid_cache_count;
246                 ocp->dip = NULL;
247                 kfree(ocp, hmp->m_misc);
248         } else {
249                 TAILQ_INSERT_TAIL(&hmp->objid_cache_list, ocp, entry);
250         }
251         return(tid);
252 }
253
254 /*
255  * Allocate a bit starting with bit n.  Wrap if necessary.
256  *
257  * This routine is only ever called if a bit is available somewhere
258  * in the bitmap.
259  */
260 static u_int32_t
261 ocp_allocbit(hammer_objid_cache_t ocp, u_int32_t n)
262 {
263         u_int32_t n0;
264
265         n0 = (n >> 5) & 31;
266         n &= 31;
267
268         while (ocp->bm1[n0] & (1 << n)) {
269                 if (ocp->bm0 & (1 << n0)) {
270                         n0 = (n0 + 1) & 31;
271                         n = 0;
272                 } else if (++n == 32) {
273                         n0 = (n0 + 1) & 31;
274                         n = 0;
275                 }
276         }
277         ++ocp->count;
278         ocp->bm1[n0] |= 1 << n;
279         if (ocp->bm1[n0] == 0xFFFFFFFFU)
280                 ocp->bm0 |= 1 << n0;
281         return((n0 << 5) + n);
282 }
283
284 void
285 hammer_clear_objid(hammer_inode_t dip)
286 {
287         hammer_objid_cache_t ocp;
288
289         if ((ocp = dip->objid_cache) != NULL) {
290                 dip->objid_cache = NULL;
291                 ocp->dip = NULL;
292                 TAILQ_REMOVE(&dip->hmp->objid_cache_list, ocp, entry);
293                 TAILQ_INSERT_HEAD(&dip->hmp->objid_cache_list, ocp, entry);
294         }
295 }
296
297 void
298 hammer_destroy_objid_cache(hammer_mount_t hmp)
299 {
300         hammer_objid_cache_t ocp;
301
302         while ((ocp = TAILQ_FIRST(&hmp->objid_cache_list)) != NULL) {
303                 TAILQ_REMOVE(&hmp->objid_cache_list, ocp, entry);
304                 if (ocp->dip)
305                         ocp->dip->objid_cache = NULL;
306                 kfree(ocp, hmp->m_misc);
307                 --hmp->objid_cache_count;
308         }
309         KKASSERT(hmp->objid_cache_count == 0);
310 }
311