HAMMER 46/Many: Performance pass, media changes, bug fixes.
[dragonfly.git] / sys / vfs / hammer / hammer_subs.c
CommitLineData
427e5fc6 1/*
b84de5af 2 * Copyright (c) 2007-2008 The DragonFly Project. All rights reserved.
427e5fc6
MD
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 *
2f85fa4d 34 * $DragonFly: src/sys/vfs/hammer/hammer_subs.c,v 1.20 2008/05/18 01:48:50 dillon Exp $
427e5fc6
MD
35 */
36/*
37 * HAMMER structural locking
38 */
39
40#include "hammer.h"
6b4f890b 41#include <sys/dirent.h>
427e5fc6
MD
42
43void
8cd0a023 44hammer_lock_ex(struct hammer_lock *lock)
427e5fc6
MD
45{
46 thread_t td = curthread;
47
8cd0a023 48 KKASSERT(lock->refs > 0);
66325755 49 crit_enter();
427e5fc6 50 if (lock->locktd != td) {
c0ade690 51 while (lock->locktd != NULL || lock->lockcount) {
427e5fc6 52 lock->wanted = 1;
7d683b0f
MD
53 if (hammer_debug_locks) {
54 kprintf("hammer_lock_ex: held by %p\n",
55 lock->locktd);
56 }
57 ++hammer_contention_count;
427e5fc6 58 tsleep(lock, 0, "hmrlck", 0);
7d683b0f
MD
59 if (hammer_debug_locks)
60 kprintf("hammer_lock_ex: try again\n");
427e5fc6
MD
61 }
62 lock->locktd = td;
63 }
0b075555 64 KKASSERT(lock->lockcount >= 0);
8cd0a023 65 ++lock->lockcount;
66325755 66 crit_exit();
427e5fc6
MD
67}
68
8cd0a023
MD
69/*
70 * Try to obtain an exclusive lock
71 */
72int
73hammer_lock_ex_try(struct hammer_lock *lock)
74{
75 thread_t td = curthread;
76
77 KKASSERT(lock->refs > 0);
78 crit_enter();
79 if (lock->locktd != td) {
4d75d829
MD
80 if (lock->locktd != NULL || lock->lockcount) {
81 crit_exit();
8cd0a023 82 return(EAGAIN);
4d75d829 83 }
8cd0a023
MD
84 lock->locktd = td;
85 }
0b075555 86 KKASSERT(lock->lockcount >= 0);
8cd0a023
MD
87 ++lock->lockcount;
88 crit_exit();
89 return(0);
90}
91
427e5fc6 92void
8cd0a023 93hammer_lock_sh(struct hammer_lock *lock)
427e5fc6 94{
427e5fc6 95 KKASSERT(lock->refs > 0);
66325755 96 crit_enter();
8cd0a023
MD
97 while (lock->locktd != NULL) {
98 if (lock->locktd == curthread) {
6a37e7e4 99 Debugger("hammer_lock_sh: lock_sh on exclusive");
8cd0a023
MD
100 ++lock->lockcount;
101 crit_exit();
102 return;
427e5fc6 103 }
8cd0a023
MD
104 lock->wanted = 1;
105 tsleep(lock, 0, "hmrlck", 0);
427e5fc6 106 }
8cd0a023
MD
107 KKASSERT(lock->lockcount <= 0);
108 --lock->lockcount;
66325755
MD
109 crit_exit();
110}
111
6a37e7e4
MD
112/*
113 * Upgrade a shared lock to an exclusively held lock. This function will
114 * return EDEADLK If there is more then one shared holder.
115 *
116 * No error occurs and no action is taken if the lock is already exclusively
7aa3b8a6
MD
117 * held by the caller. If the lock is not held at all or held exclusively
118 * by someone else, this function will panic.
6a37e7e4
MD
119 */
120int
121hammer_lock_upgrade(struct hammer_lock *lock)
122{
123 int error;
124
125 crit_enter();
126 if (lock->lockcount > 0) {
7aa3b8a6
MD
127 if (lock->locktd != curthread)
128 panic("hammer_lock_upgrade: illegal lock state");
6a37e7e4
MD
129 error = 0;
130 } else if (lock->lockcount == -1) {
131 lock->lockcount = 1;
132 lock->locktd = curthread;
133 error = 0;
7aa3b8a6 134 } else if (lock->lockcount != 0) {
6a37e7e4 135 error = EDEADLK;
7aa3b8a6
MD
136 } else {
137 panic("hammer_lock_upgrade: lock is not held");
138 /* NOT REACHED */
139 error = 0;
6a37e7e4
MD
140 }
141 crit_exit();
142 return(error);
143}
144
145/*
146 * Downgrade an exclusively held lock to a shared lock.
147 */
66325755 148void
6a37e7e4 149hammer_lock_downgrade(struct hammer_lock *lock)
66325755 150{
7aa3b8a6 151 KKASSERT(lock->lockcount == 1 && lock->locktd == curthread);
66325755 152 crit_enter();
8cd0a023
MD
153 lock->lockcount = -1;
154 lock->locktd = NULL;
155 if (lock->wanted) {
156 lock->wanted = 0;
157 wakeup(lock);
158 }
66325755 159 crit_exit();
8cd0a023 160 /* XXX memory barrier */
66325755
MD
161}
162
163void
8cd0a023 164hammer_unlock(struct hammer_lock *lock)
66325755
MD
165{
166 crit_enter();
8cd0a023
MD
167 KKASSERT(lock->lockcount != 0);
168 if (lock->lockcount < 0) {
169 if (++lock->lockcount == 0 && lock->wanted) {
170 lock->wanted = 0;
171 wakeup(lock);
172 }
173 } else {
174 KKASSERT(lock->locktd == curthread);
175 if (--lock->lockcount == 0) {
176 lock->locktd = NULL;
177 if (lock->wanted) {
178 lock->wanted = 0;
179 wakeup(lock);
180 }
181 }
182
183 }
66325755
MD
184 crit_exit();
185}
186
187void
8cd0a023 188hammer_ref(struct hammer_lock *lock)
66325755 189{
0b075555 190 KKASSERT(lock->refs >= 0);
66325755
MD
191 crit_enter();
192 ++lock->refs;
66325755
MD
193 crit_exit();
194}
195
196void
8cd0a023 197hammer_unref(struct hammer_lock *lock)
66325755 198{
a89aec1b 199 KKASSERT(lock->refs > 0);
0b075555 200 crit_enter();
66325755
MD
201 --lock->refs;
202 crit_exit();
203}
204
2f85fa4d
MD
205/*
206 * The sync_lock must be held when doing any modifying operations on
207 * meta-data. The flusher holds the lock exclusively while the reblocker
208 * and pruner use a shared lock.
209 *
210 * Modifying operations can run in parallel until the flusher needs to
211 * sync the disk media.
212 */
213void
214hammer_sync_lock_ex(hammer_transaction_t trans)
215{
216 ++trans->sync_lock_refs;
217 hammer_lock_sh(&trans->hmp->sync_lock);
218}
219
220void
221hammer_sync_lock_sh(hammer_transaction_t trans)
222{
223 ++trans->sync_lock_refs;
224 hammer_lock_ex(&trans->hmp->sync_lock);
225}
226
227void
228hammer_sync_unlock(hammer_transaction_t trans)
229{
230 --trans->sync_lock_refs;
231 hammer_unlock(&trans->hmp->sync_lock);
232}
233
234/*
235 * Misc
236 */
66325755
MD
237u_int32_t
238hammer_to_unix_xid(uuid_t *uuid)
239{
240 return(*(u_int32_t *)&uuid->node[2]);
241}
242
243void
8cd0a023
MD
244hammer_guid_to_uuid(uuid_t *uuid, u_int32_t guid)
245{
246 bzero(uuid, sizeof(*uuid));
247 *(u_int32_t *)&uuid->node[2] = guid;
248}
249
250void
251hammer_to_timespec(hammer_tid_t tid, struct timespec *ts)
66325755 252{
8cd0a023
MD
253 ts->tv_sec = tid / 1000000000;
254 ts->tv_nsec = tid % 1000000000;
66325755
MD
255}
256
8cd0a023
MD
257hammer_tid_t
258hammer_timespec_to_transid(struct timespec *ts)
259{
260 hammer_tid_t tid;
261
262 tid = ts->tv_nsec + (unsigned long)ts->tv_sec * 1000000000LL;
263 return(tid);
264}
265
266
66325755
MD
267/*
268 * Convert a HAMMER filesystem object type to a vnode type
269 */
270enum vtype
271hammer_get_vnode_type(u_int8_t obj_type)
272{
273 switch(obj_type) {
274 case HAMMER_OBJTYPE_DIRECTORY:
275 return(VDIR);
276 case HAMMER_OBJTYPE_REGFILE:
277 return(VREG);
278 case HAMMER_OBJTYPE_DBFILE:
279 return(VDATABASE);
280 case HAMMER_OBJTYPE_FIFO:
281 return(VFIFO);
282 case HAMMER_OBJTYPE_CDEV:
283 return(VCHR);
284 case HAMMER_OBJTYPE_BDEV:
285 return(VBLK);
286 case HAMMER_OBJTYPE_SOFTLINK:
287 return(VLNK);
288 default:
289 return(VBAD);
290 }
291 /* not reached */
292}
293
6b4f890b
MD
294int
295hammer_get_dtype(u_int8_t obj_type)
296{
297 switch(obj_type) {
298 case HAMMER_OBJTYPE_DIRECTORY:
299 return(DT_DIR);
300 case HAMMER_OBJTYPE_REGFILE:
301 return(DT_REG);
302 case HAMMER_OBJTYPE_DBFILE:
303 return(DT_DBF);
304 case HAMMER_OBJTYPE_FIFO:
305 return(DT_FIFO);
306 case HAMMER_OBJTYPE_CDEV:
307 return(DT_CHR);
308 case HAMMER_OBJTYPE_BDEV:
309 return(DT_BLK);
310 case HAMMER_OBJTYPE_SOFTLINK:
311 return(DT_LNK);
312 default:
313 return(DT_UNKNOWN);
314 }
315 /* not reached */
316}
317
66325755
MD
318u_int8_t
319hammer_get_obj_type(enum vtype vtype)
320{
321 switch(vtype) {
322 case VDIR:
323 return(HAMMER_OBJTYPE_DIRECTORY);
324 case VREG:
325 return(HAMMER_OBJTYPE_REGFILE);
326 case VDATABASE:
327 return(HAMMER_OBJTYPE_DBFILE);
328 case VFIFO:
329 return(HAMMER_OBJTYPE_FIFO);
330 case VCHR:
331 return(HAMMER_OBJTYPE_CDEV);
332 case VBLK:
333 return(HAMMER_OBJTYPE_BDEV);
334 case VLNK:
335 return(HAMMER_OBJTYPE_SOFTLINK);
336 default:
337 return(HAMMER_OBJTYPE_UNKNOWN);
338 }
339 /* not reached */
340}
341
342/*
343 * Return a namekey hash. The 64 bit namekey hash consists of a 32 bit
344 * crc in the MSB and 0 in the LSB. The caller will use the low bits to
345 * generate a unique key and will scan all entries with the same upper
346 * 32 bits when issuing a lookup.
6b4f890b
MD
347 *
348 * We strip bit 63 in order to provide a positive key, this way a seek
349 * offset of 0 will represent the base of the directory.
b3deaf57
MD
350 *
351 * This function can never return 0. We use the MSB-0 space to synthesize
352 * artificial directory entries such as "." and "..".
66325755
MD
353 */
354int64_t
355hammer_directory_namekey(void *name, int len)
356{
357 int64_t key;
358
6b4f890b 359 key = (int64_t)(crc32(name, len) & 0x7FFFFFFF) << 32;
b3deaf57
MD
360 if (key == 0)
361 key |= 0x100000000LL;
66325755 362 return(key);
427e5fc6
MD
363}
364
7f7c1f84
MD
365hammer_tid_t
366hammer_now_tid(void)
367{
368 struct timespec ts;
369 hammer_tid_t tid;
370
371 getnanotime(&ts);
372 tid = ts.tv_sec * 1000000000LL + ts.tv_nsec;
373 return(tid);
374}
375
d113fda1
MD
376hammer_tid_t
377hammer_str_to_tid(const char *str)
378{
379 hammer_tid_t tid;
7dc57964 380 int len = strlen(str);
d113fda1 381
7dc57964
MD
382 if (len > 10)
383 tid = strtouq(str, NULL, 0); /* full TID */
384 else
385 tid = strtouq(str, NULL, 0) * 1000000000LL; /* time_t */
d113fda1
MD
386 return(tid);
387}
388
19619882
MD
389void
390hammer_crc_set_blockmap(hammer_blockmap_t blockmap)
391{
392 blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
393}
394
395void
396hammer_crc_set_volume(hammer_volume_ondisk_t ondisk)
397{
398 ondisk->vol_crc = crc32(ondisk, HAMMER_VOL_CRCSIZE1) ^
399 crc32(&ondisk->vol_crc + 1, HAMMER_VOL_CRCSIZE2);
400}
401
402int
403hammer_crc_test_blockmap(hammer_blockmap_t blockmap)
404{
405 hammer_crc_t crc;
406
407 crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
408 return (blockmap->entry_crc == crc);
409}
410
411int
412hammer_crc_test_volume(hammer_volume_ondisk_t ondisk)
413{
414 hammer_crc_t crc;
415
416 crc = crc32(ondisk, HAMMER_VOL_CRCSIZE1) ^
417 crc32(&ondisk->vol_crc + 1, HAMMER_VOL_CRCSIZE2);
418 return (ondisk->vol_crc == crc);
419}
420
19619882
MD
421int
422hammer_crc_test_btree(hammer_node_ondisk_t ondisk)
423{
424 hammer_crc_t crc;
425
426 crc = crc32(&ondisk->crc + 1, HAMMER_BTREE_CRCSIZE);
427 return (ondisk->crc == crc);
428}
429
77062c8a
MD
430void
431hkprintf(const char *ctl, ...)
432{
433 __va_list va;
434
435 if (hammer_debug_debug) {
436 __va_start(va, ctl);
437 kvprintf(ctl, va);
438 __va_end(va);
439 }
440}
441