2 * Copyright (c) 2007 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.9 2007/12/31 05:33:12 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 kprintf("hammer_lock_ex: held by %p\n", lock->locktd);
54 tsleep(lock, 0, "hmrlck", 0);
55 kprintf("hammer_lock_ex: try again\n");
59 KKASSERT(lock->lockcount >= 0);
65 * Try to obtain an exclusive lock
68 hammer_lock_ex_try(struct hammer_lock *lock)
70 thread_t td = curthread;
72 KKASSERT(lock->refs > 0);
74 if (lock->locktd != td) {
75 if (lock->locktd != NULL || lock->lockcount)
79 KKASSERT(lock->lockcount >= 0);
86 hammer_lock_sh(struct hammer_lock *lock)
88 KKASSERT(lock->refs > 0);
90 while (lock->locktd != NULL) {
91 if (lock->locktd == curthread) {
97 tsleep(lock, 0, "hmrlck", 0);
99 KKASSERT(lock->lockcount <= 0);
105 hammer_downgrade(struct hammer_lock *lock)
107 KKASSERT(lock->lockcount == 1);
109 lock->lockcount = -1;
116 /* XXX memory barrier */
120 hammer_unlock(struct hammer_lock *lock)
123 KKASSERT(lock->lockcount != 0);
124 if (lock->lockcount < 0) {
125 if (++lock->lockcount == 0 && lock->wanted) {
130 KKASSERT(lock->locktd == curthread);
131 if (--lock->lockcount == 0) {
144 hammer_ref(struct hammer_lock *lock)
146 KKASSERT(lock->refs >= 0);
153 hammer_unref(struct hammer_lock *lock)
155 KKASSERT(lock->refs > 0);
162 hammer_to_unix_xid(uuid_t *uuid)
164 return(*(u_int32_t *)&uuid->node[2]);
168 hammer_guid_to_uuid(uuid_t *uuid, u_int32_t guid)
170 bzero(uuid, sizeof(*uuid));
171 *(u_int32_t *)&uuid->node[2] = guid;
175 hammer_to_timespec(hammer_tid_t tid, struct timespec *ts)
177 ts->tv_sec = tid / 1000000000;
178 ts->tv_nsec = tid % 1000000000;
182 hammer_timespec_to_transid(struct timespec *ts)
186 tid = ts->tv_nsec + (unsigned long)ts->tv_sec * 1000000000LL;
192 * Convert a HAMMER filesystem object type to a vnode type
195 hammer_get_vnode_type(u_int8_t obj_type)
198 case HAMMER_OBJTYPE_DIRECTORY:
200 case HAMMER_OBJTYPE_REGFILE:
202 case HAMMER_OBJTYPE_DBFILE:
204 case HAMMER_OBJTYPE_FIFO:
206 case HAMMER_OBJTYPE_CDEV:
208 case HAMMER_OBJTYPE_BDEV:
210 case HAMMER_OBJTYPE_SOFTLINK:
219 hammer_get_dtype(u_int8_t obj_type)
222 case HAMMER_OBJTYPE_DIRECTORY:
224 case HAMMER_OBJTYPE_REGFILE:
226 case HAMMER_OBJTYPE_DBFILE:
228 case HAMMER_OBJTYPE_FIFO:
230 case HAMMER_OBJTYPE_CDEV:
232 case HAMMER_OBJTYPE_BDEV:
234 case HAMMER_OBJTYPE_SOFTLINK:
243 hammer_get_obj_type(enum vtype vtype)
247 return(HAMMER_OBJTYPE_DIRECTORY);
249 return(HAMMER_OBJTYPE_REGFILE);
251 return(HAMMER_OBJTYPE_DBFILE);
253 return(HAMMER_OBJTYPE_FIFO);
255 return(HAMMER_OBJTYPE_CDEV);
257 return(HAMMER_OBJTYPE_BDEV);
259 return(HAMMER_OBJTYPE_SOFTLINK);
261 return(HAMMER_OBJTYPE_UNKNOWN);
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.
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.
275 * This function can never return 0. We use the MSB-0 space to synthesize
276 * artificial directory entries such as "." and "..".
279 hammer_directory_namekey(void *name, int len)
283 key = (int64_t)(crc32(name, len) & 0x7FFFFFFF) << 32;
285 key |= 0x100000000LL;
296 tid = ts.tv_sec * 1000000000LL + ts.tv_nsec;