HAMMER 12/many - buffer cache sync, buffer cache interactions, misc fixes.
[dragonfly.git] / sys / vfs / hammer / hammer_subs.c
1 /*
2  * Copyright (c) 2007 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_subs.c,v 1.8 2007/12/30 08:49:20 dillon Exp $
35  */
36 /*
37  * HAMMER structural locking
38  */
39
40 #include "hammer.h"
41 #include <sys/dirent.h>
42
43 void
44 hammer_lock_ex(struct hammer_lock *lock)
45 {
46         thread_t td = curthread;
47
48         KKASSERT(lock->refs > 0);
49         crit_enter();
50         if (lock->locktd != td) {
51                 while (lock->locktd != NULL || lock->lockcount) {
52                         lock->wanted = 1;
53                         kprintf("hammer_lock_ex: held by %p\n", lock->locktd);
54                         tsleep(lock, 0, "hmrlck", 0);
55                         kprintf("hammer_lock_ex: try again\n");
56                 }
57                 lock->locktd = td;
58         }
59         KKASSERT(lock->lockcount >= 0);
60         ++lock->lockcount;
61         crit_exit();
62 }
63
64 /*
65  * Try to obtain an exclusive lock
66  */
67 int
68 hammer_lock_ex_try(struct hammer_lock *lock)
69 {
70         thread_t td = curthread;
71
72         KKASSERT(lock->refs > 0);
73         crit_enter();
74         if (lock->locktd != td) {
75                 if (lock->locktd != NULL || lock->lockcount)
76                         return(EAGAIN);
77                 lock->locktd = td;
78         }
79         KKASSERT(lock->lockcount >= 0);
80         ++lock->lockcount;
81         crit_exit();
82         return(0);
83 }
84
85 void
86 hammer_lock_sh(struct hammer_lock *lock)
87 {
88         KKASSERT(lock->refs > 0);
89         crit_enter();
90         while (lock->locktd != NULL) {
91                 if (lock->locktd == curthread) {
92                         ++lock->lockcount;
93                         crit_exit();
94                         return;
95                 }
96                 lock->wanted = 1;
97                 tsleep(lock, 0, "hmrlck", 0);
98         }
99         KKASSERT(lock->lockcount <= 0);
100         --lock->lockcount;
101         crit_exit();
102 }
103
104 void
105 hammer_downgrade(struct hammer_lock *lock)
106 {
107         KKASSERT(lock->lockcount == 1);
108         crit_enter();
109         lock->lockcount = -1;
110         lock->locktd = NULL;
111         if (lock->wanted) {
112                 lock->wanted = 0;
113                 wakeup(lock);
114         }
115         crit_exit();
116         /* XXX memory barrier */
117 }
118
119 void
120 hammer_unlock(struct hammer_lock *lock)
121 {
122         crit_enter();
123         KKASSERT(lock->lockcount != 0);
124         if (lock->lockcount < 0) {
125                 if (++lock->lockcount == 0 && lock->wanted) {
126                         lock->wanted = 0;
127                         wakeup(lock);
128                 }
129         } else {
130                 KKASSERT(lock->locktd == curthread);
131                 if (--lock->lockcount == 0) {
132                         lock->locktd = NULL;
133                         if (lock->wanted) {
134                                 lock->wanted = 0;
135                                 wakeup(lock);
136                         }
137                 }
138
139         }
140         crit_exit();
141 }
142
143 void
144 hammer_ref(struct hammer_lock *lock)
145 {
146         KKASSERT(lock->refs >= 0);
147         crit_enter();
148         ++lock->refs;
149         crit_exit();
150 }
151
152 void
153 hammer_unref(struct hammer_lock *lock)
154 {
155         KKASSERT(lock->refs > 0);
156         crit_enter();
157         --lock->refs;
158         crit_exit();
159 }
160
161 u_int32_t
162 hammer_to_unix_xid(uuid_t *uuid)
163 {
164         return(*(u_int32_t *)&uuid->node[2]);
165 }
166
167 void
168 hammer_guid_to_uuid(uuid_t *uuid, u_int32_t guid)
169 {
170         bzero(uuid, sizeof(*uuid));
171         *(u_int32_t *)&uuid->node[2] = guid;
172 }
173
174 void
175 hammer_to_timespec(hammer_tid_t tid, struct timespec *ts)
176 {
177         ts->tv_sec = tid / 1000000000;
178         ts->tv_nsec = tid % 1000000000;
179 }
180
181 hammer_tid_t
182 hammer_timespec_to_transid(struct timespec *ts)
183 {
184         hammer_tid_t tid;
185
186         tid = ts->tv_nsec + (unsigned long)ts->tv_sec * 1000000000LL;
187         return(tid);
188 }
189
190
191 /*
192  * Convert a HAMMER filesystem object type to a vnode type
193  */
194 enum vtype
195 hammer_get_vnode_type(u_int8_t obj_type)
196 {
197         switch(obj_type) {
198         case HAMMER_OBJTYPE_DIRECTORY:
199                 return(VDIR);
200         case HAMMER_OBJTYPE_REGFILE:
201                 return(VREG);
202         case HAMMER_OBJTYPE_DBFILE:
203                 return(VDATABASE);
204         case HAMMER_OBJTYPE_FIFO:
205                 return(VFIFO);
206         case HAMMER_OBJTYPE_CDEV:
207                 return(VCHR);
208         case HAMMER_OBJTYPE_BDEV:
209                 return(VBLK);
210         case HAMMER_OBJTYPE_SOFTLINK:
211                 return(VLNK);
212         default:
213                 return(VBAD);
214         }
215         /* not reached */
216 }
217
218 int
219 hammer_get_dtype(u_int8_t obj_type)
220 {
221         switch(obj_type) {
222         case HAMMER_OBJTYPE_DIRECTORY:
223                 return(DT_DIR);
224         case HAMMER_OBJTYPE_REGFILE:
225                 return(DT_REG);
226         case HAMMER_OBJTYPE_DBFILE:
227                 return(DT_DBF);
228         case HAMMER_OBJTYPE_FIFO:
229                 return(DT_FIFO);
230         case HAMMER_OBJTYPE_CDEV:
231                 return(DT_CHR);
232         case HAMMER_OBJTYPE_BDEV:
233                 return(DT_BLK);
234         case HAMMER_OBJTYPE_SOFTLINK:
235                 return(DT_LNK);
236         default:
237                 return(DT_UNKNOWN);
238         }
239         /* not reached */
240 }
241
242 u_int8_t
243 hammer_get_obj_type(enum vtype vtype)
244 {
245         switch(vtype) {
246         case VDIR:
247                 return(HAMMER_OBJTYPE_DIRECTORY);
248         case VREG:
249                 return(HAMMER_OBJTYPE_REGFILE);
250         case VDATABASE:
251                 return(HAMMER_OBJTYPE_DBFILE);
252         case VFIFO:
253                 return(HAMMER_OBJTYPE_FIFO);
254         case VCHR:
255                 return(HAMMER_OBJTYPE_CDEV);
256         case VBLK:
257                 return(HAMMER_OBJTYPE_BDEV);
258         case VLNK:
259                 return(HAMMER_OBJTYPE_SOFTLINK);
260         default:
261                 return(HAMMER_OBJTYPE_UNKNOWN);
262         }
263         /* not reached */
264 }
265
266 /*
267  * Return a namekey hash.   The 64 bit namekey hash consists of a 32 bit
268  * crc in the MSB and 0 in the LSB.  The caller will use the low bits to
269  * generate a unique key and will scan all entries with the same upper
270  * 32 bits when issuing a lookup.
271  *
272  * We strip bit 63 in order to provide a positive key, this way a seek
273  * offset of 0 will represent the base of the directory.
274  */
275 int64_t
276 hammer_directory_namekey(void *name, int len)
277 {
278         int64_t key;
279
280         key = (int64_t)(crc32(name, len) & 0x7FFFFFFF) << 32;
281         return(key);
282 }
283
284 hammer_tid_t
285 hammer_now_tid(void)
286 {
287         struct timespec ts;
288         hammer_tid_t tid;
289
290         getnanotime(&ts);
291         tid = ts.tv_sec * 1000000000LL + ts.tv_nsec;
292         return(tid);
293 }
294