2 * Copyright (c) 2007-2008 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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
34 * $DragonFly: src/sys/vfs/hammer/hammer_subs.c,v 1.18 2008/05/06 00:21:08 dillon Exp $
37 * HAMMER structural locking
41 #include <sys/dirent.h>
44 hammer_lock_ex(struct hammer_lock *lock)
46 thread_t td = curthread;
48 KKASSERT(lock->refs > 0);
50 if (lock->locktd != td) {
51 while (lock->locktd != NULL || lock->lockcount) {
53 if (hammer_debug_locks) {
54 kprintf("hammer_lock_ex: held by %p\n",
57 ++hammer_contention_count;
58 tsleep(lock, 0, "hmrlck", 0);
59 if (hammer_debug_locks)
60 kprintf("hammer_lock_ex: try again\n");
64 KKASSERT(lock->lockcount >= 0);
70 * Try to obtain an exclusive lock
73 hammer_lock_ex_try(struct hammer_lock *lock)
75 thread_t td = curthread;
77 KKASSERT(lock->refs > 0);
79 if (lock->locktd != td) {
80 if (lock->locktd != NULL || lock->lockcount) {
86 KKASSERT(lock->lockcount >= 0);
93 hammer_lock_sh(struct hammer_lock *lock)
95 KKASSERT(lock->refs > 0);
97 while (lock->locktd != NULL) {
98 if (lock->locktd == curthread) {
99 Debugger("hammer_lock_sh: lock_sh on exclusive");
105 tsleep(lock, 0, "hmrlck", 0);
107 KKASSERT(lock->lockcount <= 0);
113 * Upgrade a shared lock to an exclusively held lock. This function will
114 * return EDEADLK If there is more then one shared holder.
116 * No error occurs and no action is taken if the lock is already exclusively
117 * held by the caller. If the lock is not held at all or held exclusively
118 * by someone else, this function will panic.
121 hammer_lock_upgrade(struct hammer_lock *lock)
126 if (lock->lockcount > 0) {
127 if (lock->locktd != curthread)
128 panic("hammer_lock_upgrade: illegal lock state");
130 } else if (lock->lockcount == -1) {
132 lock->locktd = curthread;
134 } else if (lock->lockcount != 0) {
137 panic("hammer_lock_upgrade: lock is not held");
146 * Downgrade an exclusively held lock to a shared lock.
149 hammer_lock_downgrade(struct hammer_lock *lock)
151 KKASSERT(lock->lockcount == 1 && lock->locktd == curthread);
153 lock->lockcount = -1;
160 /* XXX memory barrier */
164 hammer_unlock(struct hammer_lock *lock)
167 KKASSERT(lock->lockcount != 0);
168 if (lock->lockcount < 0) {
169 if (++lock->lockcount == 0 && lock->wanted) {
174 KKASSERT(lock->locktd == curthread);
175 if (--lock->lockcount == 0) {
188 hammer_ref(struct hammer_lock *lock)
190 KKASSERT(lock->refs >= 0);
197 hammer_unref(struct hammer_lock *lock)
199 KKASSERT(lock->refs > 0);
206 hammer_to_unix_xid(uuid_t *uuid)
208 return(*(u_int32_t *)&uuid->node[2]);
212 hammer_guid_to_uuid(uuid_t *uuid, u_int32_t guid)
214 bzero(uuid, sizeof(*uuid));
215 *(u_int32_t *)&uuid->node[2] = guid;
219 hammer_to_timespec(hammer_tid_t tid, struct timespec *ts)
221 ts->tv_sec = tid / 1000000000;
222 ts->tv_nsec = tid % 1000000000;
226 hammer_timespec_to_transid(struct timespec *ts)
230 tid = ts->tv_nsec + (unsigned long)ts->tv_sec * 1000000000LL;
236 * Convert a HAMMER filesystem object type to a vnode type
239 hammer_get_vnode_type(u_int8_t obj_type)
242 case HAMMER_OBJTYPE_DIRECTORY:
244 case HAMMER_OBJTYPE_REGFILE:
246 case HAMMER_OBJTYPE_DBFILE:
248 case HAMMER_OBJTYPE_FIFO:
250 case HAMMER_OBJTYPE_CDEV:
252 case HAMMER_OBJTYPE_BDEV:
254 case HAMMER_OBJTYPE_SOFTLINK:
263 hammer_get_dtype(u_int8_t obj_type)
266 case HAMMER_OBJTYPE_DIRECTORY:
268 case HAMMER_OBJTYPE_REGFILE:
270 case HAMMER_OBJTYPE_DBFILE:
272 case HAMMER_OBJTYPE_FIFO:
274 case HAMMER_OBJTYPE_CDEV:
276 case HAMMER_OBJTYPE_BDEV:
278 case HAMMER_OBJTYPE_SOFTLINK:
287 hammer_get_obj_type(enum vtype vtype)
291 return(HAMMER_OBJTYPE_DIRECTORY);
293 return(HAMMER_OBJTYPE_REGFILE);
295 return(HAMMER_OBJTYPE_DBFILE);
297 return(HAMMER_OBJTYPE_FIFO);
299 return(HAMMER_OBJTYPE_CDEV);
301 return(HAMMER_OBJTYPE_BDEV);
303 return(HAMMER_OBJTYPE_SOFTLINK);
305 return(HAMMER_OBJTYPE_UNKNOWN);
311 * Return a namekey hash. The 64 bit namekey hash consists of a 32 bit
312 * crc in the MSB and 0 in the LSB. The caller will use the low bits to
313 * generate a unique key and will scan all entries with the same upper
314 * 32 bits when issuing a lookup.
316 * We strip bit 63 in order to provide a positive key, this way a seek
317 * offset of 0 will represent the base of the directory.
319 * This function can never return 0. We use the MSB-0 space to synthesize
320 * artificial directory entries such as "." and "..".
323 hammer_directory_namekey(void *name, int len)
327 key = (int64_t)(crc32(name, len) & 0x7FFFFFFF) << 32;
329 key |= 0x100000000LL;
340 tid = ts.tv_sec * 1000000000LL + ts.tv_nsec;
345 hammer_str_to_tid(const char *str)
348 int len = strlen(str);
351 tid = strtouq(str, NULL, 0); /* full TID */
353 tid = strtouq(str, NULL, 0) * 1000000000LL; /* time_t */
358 hammer_crc_set_blockmap(hammer_blockmap_t blockmap)
360 blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
364 hammer_crc_set_volume(hammer_volume_ondisk_t ondisk)
366 ondisk->vol_crc = crc32(ondisk, HAMMER_VOL_CRCSIZE1) ^
367 crc32(&ondisk->vol_crc + 1, HAMMER_VOL_CRCSIZE2);
371 hammer_crc_test_blockmap(hammer_blockmap_t blockmap)
375 crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
376 return (blockmap->entry_crc == crc);
380 hammer_crc_test_volume(hammer_volume_ondisk_t ondisk)
384 crc = crc32(ondisk, HAMMER_VOL_CRCSIZE1) ^
385 crc32(&ondisk->vol_crc + 1, HAMMER_VOL_CRCSIZE2);
386 return (ondisk->vol_crc == crc);
390 hammer_crc_test_record(hammer_record_ondisk_t ondisk)
394 crc = crc32(&ondisk->base.rec_crc + 1, HAMMER_RECORD_CRCSIZE);
395 return (ondisk->base.rec_crc == crc &&
396 ondisk->base.signature == HAMMER_RECORD_SIGNATURE_GOOD);
400 hammer_crc_test_btree(hammer_node_ondisk_t ondisk)
404 crc = crc32(&ondisk->crc + 1, HAMMER_BTREE_CRCSIZE);
405 return (ondisk->crc == crc);
409 hkprintf(const char *ctl, ...)
413 if (hammer_debug_debug) {