Merge from vendor branch NETGRAPH:
[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 *
5de0c0e5 34 * $DragonFly: src/sys/vfs/hammer/hammer_subs.c,v 1.28 2008/06/23 21:42:48 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
af209b0f 44hammer_lock_ex_ident(struct hammer_lock *lock, const char *ident)
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) {
4a2796f3 52 ++lock->exwanted;
427e5fc6 53 lock->wanted = 1;
7d683b0f
MD
54 if (hammer_debug_locks) {
55 kprintf("hammer_lock_ex: held by %p\n",
56 lock->locktd);
57 }
58 ++hammer_contention_count;
af209b0f 59 tsleep(lock, 0, ident, 0);
7d683b0f
MD
60 if (hammer_debug_locks)
61 kprintf("hammer_lock_ex: try again\n");
4a2796f3 62 --lock->exwanted;
427e5fc6
MD
63 }
64 lock->locktd = td;
65 }
0b075555 66 KKASSERT(lock->lockcount >= 0);
8cd0a023 67 ++lock->lockcount;
66325755 68 crit_exit();
427e5fc6
MD
69}
70
8cd0a023
MD
71/*
72 * Try to obtain an exclusive lock
73 */
74int
75hammer_lock_ex_try(struct hammer_lock *lock)
76{
77 thread_t td = curthread;
78
79 KKASSERT(lock->refs > 0);
80 crit_enter();
81 if (lock->locktd != td) {
4d75d829
MD
82 if (lock->locktd != NULL || lock->lockcount) {
83 crit_exit();
8cd0a023 84 return(EAGAIN);
4d75d829 85 }
8cd0a023
MD
86 lock->locktd = td;
87 }
0b075555 88 KKASSERT(lock->lockcount >= 0);
8cd0a023
MD
89 ++lock->lockcount;
90 crit_exit();
91 return(0);
92}
93
4a2796f3
MD
94/*
95 * Obtain a shared lock
96 */
427e5fc6 97void
8cd0a023 98hammer_lock_sh(struct hammer_lock *lock)
427e5fc6 99{
427e5fc6 100 KKASSERT(lock->refs > 0);
66325755 101 crit_enter();
8cd0a023
MD
102 while (lock->locktd != NULL) {
103 if (lock->locktd == curthread) {
6a37e7e4 104 Debugger("hammer_lock_sh: lock_sh on exclusive");
8cd0a023
MD
105 ++lock->lockcount;
106 crit_exit();
107 return;
427e5fc6 108 }
8cd0a023
MD
109 lock->wanted = 1;
110 tsleep(lock, 0, "hmrlck", 0);
427e5fc6 111 }
8cd0a023
MD
112 KKASSERT(lock->lockcount <= 0);
113 --lock->lockcount;
66325755
MD
114 crit_exit();
115}
116
4a2796f3
MD
117/*
118 * Obtain a shared lock at a lower priority then thread waiting for an
119 * exclusive lock. To avoid a deadlock this may only be done if no other
120 * shared locks are being held by the caller.
121 */
122void
123hammer_lock_sh_lowpri(struct hammer_lock *lock)
124{
125 KKASSERT(lock->refs > 0);
126 crit_enter();
127 while (lock->locktd != NULL || lock->exwanted) {
128 if (lock->locktd == curthread) {
129 Debugger("hammer_lock_sh: lock_sh on exclusive");
130 ++lock->lockcount;
131 crit_exit();
132 return;
133 }
134 lock->wanted = 1;
135 tsleep(lock, 0, "hmrlck", 0);
136 }
137 KKASSERT(lock->lockcount <= 0);
138 --lock->lockcount;
139 crit_exit();
140}
141
47637bff
MD
142int
143hammer_lock_sh_try(struct hammer_lock *lock)
144{
145 KKASSERT(lock->refs > 0);
146 crit_enter();
147 if (lock->locktd) {
148 crit_exit();
149 return(EAGAIN);
150 }
151 KKASSERT(lock->lockcount <= 0);
152 --lock->lockcount;
153 crit_exit();
154 return(0);
155}
156
6a37e7e4
MD
157/*
158 * Upgrade a shared lock to an exclusively held lock. This function will
159 * return EDEADLK If there is more then one shared holder.
160 *
161 * No error occurs and no action is taken if the lock is already exclusively
7aa3b8a6
MD
162 * held by the caller. If the lock is not held at all or held exclusively
163 * by someone else, this function will panic.
6a37e7e4
MD
164 */
165int
166hammer_lock_upgrade(struct hammer_lock *lock)
167{
168 int error;
169
170 crit_enter();
171 if (lock->lockcount > 0) {
7aa3b8a6
MD
172 if (lock->locktd != curthread)
173 panic("hammer_lock_upgrade: illegal lock state");
6a37e7e4
MD
174 error = 0;
175 } else if (lock->lockcount == -1) {
176 lock->lockcount = 1;
177 lock->locktd = curthread;
178 error = 0;
7aa3b8a6 179 } else if (lock->lockcount != 0) {
6a37e7e4 180 error = EDEADLK;
7aa3b8a6
MD
181 } else {
182 panic("hammer_lock_upgrade: lock is not held");
183 /* NOT REACHED */
184 error = 0;
6a37e7e4
MD
185 }
186 crit_exit();
187 return(error);
188}
189
190/*
191 * Downgrade an exclusively held lock to a shared lock.
192 */
66325755 193void
6a37e7e4 194hammer_lock_downgrade(struct hammer_lock *lock)
66325755 195{
7aa3b8a6 196 KKASSERT(lock->lockcount == 1 && lock->locktd == curthread);
66325755 197 crit_enter();
8cd0a023
MD
198 lock->lockcount = -1;
199 lock->locktd = NULL;
200 if (lock->wanted) {
201 lock->wanted = 0;
202 wakeup(lock);
203 }
66325755 204 crit_exit();
8cd0a023 205 /* XXX memory barrier */
66325755
MD
206}
207
208void
8cd0a023 209hammer_unlock(struct hammer_lock *lock)
66325755
MD
210{
211 crit_enter();
8cd0a023
MD
212 KKASSERT(lock->lockcount != 0);
213 if (lock->lockcount < 0) {
214 if (++lock->lockcount == 0 && lock->wanted) {
215 lock->wanted = 0;
216 wakeup(lock);
217 }
218 } else {
219 KKASSERT(lock->locktd == curthread);
220 if (--lock->lockcount == 0) {
221 lock->locktd = NULL;
222 if (lock->wanted) {
223 lock->wanted = 0;
224 wakeup(lock);
225 }
226 }
227
228 }
66325755
MD
229 crit_exit();
230}
231
232void
8cd0a023 233hammer_ref(struct hammer_lock *lock)
66325755 234{
0b075555 235 KKASSERT(lock->refs >= 0);
66325755
MD
236 crit_enter();
237 ++lock->refs;
66325755
MD
238 crit_exit();
239}
240
241void
8cd0a023 242hammer_unref(struct hammer_lock *lock)
66325755 243{
a89aec1b 244 KKASSERT(lock->refs > 0);
0b075555 245 crit_enter();
66325755
MD
246 --lock->refs;
247 crit_exit();
248}
249
2f85fa4d
MD
250/*
251 * The sync_lock must be held when doing any modifying operations on
252 * meta-data. The flusher holds the lock exclusively while the reblocker
253 * and pruner use a shared lock.
254 *
255 * Modifying operations can run in parallel until the flusher needs to
256 * sync the disk media.
257 */
258void
259hammer_sync_lock_ex(hammer_transaction_t trans)
260{
261 ++trans->sync_lock_refs;
7538695e 262 hammer_lock_ex(&trans->hmp->sync_lock);
2f85fa4d
MD
263}
264
265void
266hammer_sync_lock_sh(hammer_transaction_t trans)
267{
268 ++trans->sync_lock_refs;
7538695e 269 hammer_lock_sh(&trans->hmp->sync_lock);
2f85fa4d
MD
270}
271
47637bff
MD
272int
273hammer_sync_lock_sh_try(hammer_transaction_t trans)
274{
275 int error;
276
277 ++trans->sync_lock_refs;
278 if ((error = hammer_lock_sh_try(&trans->hmp->sync_lock)) != 0)
279 --trans->sync_lock_refs;
280 return (error);
281}
282
2f85fa4d
MD
283void
284hammer_sync_unlock(hammer_transaction_t trans)
285{
286 --trans->sync_lock_refs;
287 hammer_unlock(&trans->hmp->sync_lock);
288}
289
290/*
291 * Misc
292 */
66325755
MD
293u_int32_t
294hammer_to_unix_xid(uuid_t *uuid)
295{
296 return(*(u_int32_t *)&uuid->node[2]);
297}
298
299void
8cd0a023
MD
300hammer_guid_to_uuid(uuid_t *uuid, u_int32_t guid)
301{
302 bzero(uuid, sizeof(*uuid));
303 *(u_int32_t *)&uuid->node[2] = guid;
304}
305
306void
ddfdf542 307hammer_time_to_timespec(u_int64_t xtime, struct timespec *ts)
66325755 308{
ddfdf542
MD
309 ts->tv_sec = (unsigned long)(xtime / 1000000);
310 ts->tv_nsec = (unsigned int)(xtime % 1000000) * 1000L;
66325755
MD
311}
312
ddfdf542
MD
313u_int64_t
314hammer_timespec_to_time(struct timespec *ts)
8cd0a023 315{
ddfdf542 316 u_int64_t xtime;
8cd0a023 317
ddfdf542
MD
318 xtime = (unsigned)(ts->tv_nsec / 1000) +
319 (unsigned long)ts->tv_sec * 1000000ULL;
320 return(xtime);
8cd0a023
MD
321}
322
323
66325755
MD
324/*
325 * Convert a HAMMER filesystem object type to a vnode type
326 */
327enum vtype
328hammer_get_vnode_type(u_int8_t obj_type)
329{
330 switch(obj_type) {
331 case HAMMER_OBJTYPE_DIRECTORY:
332 return(VDIR);
333 case HAMMER_OBJTYPE_REGFILE:
334 return(VREG);
335 case HAMMER_OBJTYPE_DBFILE:
336 return(VDATABASE);
337 case HAMMER_OBJTYPE_FIFO:
338 return(VFIFO);
339 case HAMMER_OBJTYPE_CDEV:
340 return(VCHR);
341 case HAMMER_OBJTYPE_BDEV:
342 return(VBLK);
343 case HAMMER_OBJTYPE_SOFTLINK:
344 return(VLNK);
345 default:
346 return(VBAD);
347 }
348 /* not reached */
349}
350
6b4f890b
MD
351int
352hammer_get_dtype(u_int8_t obj_type)
353{
354 switch(obj_type) {
355 case HAMMER_OBJTYPE_DIRECTORY:
356 return(DT_DIR);
357 case HAMMER_OBJTYPE_REGFILE:
358 return(DT_REG);
359 case HAMMER_OBJTYPE_DBFILE:
360 return(DT_DBF);
361 case HAMMER_OBJTYPE_FIFO:
362 return(DT_FIFO);
363 case HAMMER_OBJTYPE_CDEV:
364 return(DT_CHR);
365 case HAMMER_OBJTYPE_BDEV:
366 return(DT_BLK);
367 case HAMMER_OBJTYPE_SOFTLINK:
368 return(DT_LNK);
369 default:
370 return(DT_UNKNOWN);
371 }
372 /* not reached */
373}
374
66325755
MD
375u_int8_t
376hammer_get_obj_type(enum vtype vtype)
377{
378 switch(vtype) {
379 case VDIR:
380 return(HAMMER_OBJTYPE_DIRECTORY);
381 case VREG:
382 return(HAMMER_OBJTYPE_REGFILE);
383 case VDATABASE:
384 return(HAMMER_OBJTYPE_DBFILE);
385 case VFIFO:
386 return(HAMMER_OBJTYPE_FIFO);
387 case VCHR:
388 return(HAMMER_OBJTYPE_CDEV);
389 case VBLK:
390 return(HAMMER_OBJTYPE_BDEV);
391 case VLNK:
392 return(HAMMER_OBJTYPE_SOFTLINK);
393 default:
394 return(HAMMER_OBJTYPE_UNKNOWN);
395 }
396 /* not reached */
397}
398
e63644f0
MD
399int
400hammer_nohistory(hammer_inode_t ip)
401{
402 if (ip->hmp->hflags & HMNT_NOHISTORY)
403 return(1);
404 if (ip->ino_data.uflags & (SF_NOHISTORY|UF_NOHISTORY))
405 return(1);
406 return(0);
407}
408
66325755
MD
409/*
410 * Return a namekey hash. The 64 bit namekey hash consists of a 32 bit
411 * crc in the MSB and 0 in the LSB. The caller will use the low bits to
412 * generate a unique key and will scan all entries with the same upper
413 * 32 bits when issuing a lookup.
6b4f890b
MD
414 *
415 * We strip bit 63 in order to provide a positive key, this way a seek
416 * offset of 0 will represent the base of the directory.
b3deaf57
MD
417 *
418 * This function can never return 0. We use the MSB-0 space to synthesize
419 * artificial directory entries such as "." and "..".
66325755
MD
420 */
421int64_t
5a930e66 422hammer_directory_namekey(const void *name, int len)
66325755
MD
423{
424 int64_t key;
425
6b4f890b 426 key = (int64_t)(crc32(name, len) & 0x7FFFFFFF) << 32;
b3deaf57
MD
427 if (key == 0)
428 key |= 0x100000000LL;
66325755 429 return(key);
427e5fc6
MD
430}
431
d113fda1
MD
432hammer_tid_t
433hammer_str_to_tid(const char *str)
434{
435 hammer_tid_t tid;
436
5de0c0e5 437 tid = strtouq(str, NULL, 0); /* full TID */
d113fda1
MD
438 return(tid);
439}
440
19619882
MD
441void
442hammer_crc_set_blockmap(hammer_blockmap_t blockmap)
443{
444 blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
445}
446
447void
448hammer_crc_set_volume(hammer_volume_ondisk_t ondisk)
449{
450 ondisk->vol_crc = crc32(ondisk, HAMMER_VOL_CRCSIZE1) ^
451 crc32(&ondisk->vol_crc + 1, HAMMER_VOL_CRCSIZE2);
452}
453
454int
455hammer_crc_test_blockmap(hammer_blockmap_t blockmap)
456{
457 hammer_crc_t crc;
458
459 crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
460 return (blockmap->entry_crc == crc);
461}
462
463int
464hammer_crc_test_volume(hammer_volume_ondisk_t ondisk)
465{
466 hammer_crc_t crc;
467
468 crc = crc32(ondisk, HAMMER_VOL_CRCSIZE1) ^
469 crc32(&ondisk->vol_crc + 1, HAMMER_VOL_CRCSIZE2);
470 return (ondisk->vol_crc == crc);
471}
472
19619882
MD
473int
474hammer_crc_test_btree(hammer_node_ondisk_t ondisk)
475{
476 hammer_crc_t crc;
477
478 crc = crc32(&ondisk->crc + 1, HAMMER_BTREE_CRCSIZE);
479 return (ondisk->crc == crc);
480}
481
ddfdf542
MD
482/*
483 * Test or set the leaf->data_crc field. Deal with any special cases given
484 * a generic B-Tree leaf element and its data.
485 *
486 * NOTE: Inode-data: the atime and mtime fields are not CRCd, allowing them
487 * to be updated in-place.
488 */
489int
490hammer_crc_test_leaf(void *data, hammer_btree_leaf_elm_t leaf)
491{
492 hammer_crc_t crc;
493
494 if (leaf->data_len == 0) {
495 crc = 0;
496 } else {
497 switch(leaf->base.rec_type) {
498 case HAMMER_RECTYPE_INODE:
499 if (leaf->data_len != sizeof(struct hammer_inode_data))
500 return(0);
501 crc = crc32(data, HAMMER_INODE_CRCSIZE);
502 break;
503 default:
504 crc = crc32(data, leaf->data_len);
505 break;
506 }
507 }
508 return (leaf->data_crc == crc);
509}
510
511void
512hammer_crc_set_leaf(void *data, hammer_btree_leaf_elm_t leaf)
513{
514 if (leaf->data_len == 0) {
515 leaf->data_crc = 0;
516 } else {
517 switch(leaf->base.rec_type) {
518 case HAMMER_RECTYPE_INODE:
519 KKASSERT(leaf->data_len ==
520 sizeof(struct hammer_inode_data));
521 leaf->data_crc = crc32(data, HAMMER_INODE_CRCSIZE);
522 break;
523 default:
524 leaf->data_crc = crc32(data, leaf->data_len);
525 break;
526 }
527 }
528}
529
77062c8a
MD
530void
531hkprintf(const char *ctl, ...)
532{
533 __va_list va;
534
535 if (hammer_debug_debug) {
536 __va_start(va, ctl);
537 kvprintf(ctl, va);
538 __va_end(va);
539 }
540}
541
4a2796f3
MD
542/*
543 * Return the block size at the specified file offset.
544 */
545int
546hammer_blocksize(int64_t file_offset)
547{
548 if (file_offset < HAMMER_XDEMARC)
549 return(HAMMER_BUFSIZE);
550 else
551 return(HAMMER_XBUFSIZE);
552}
553
554/*
555 * Return the demarkation point between the two offsets where
556 * the block size changes.
557 */
558int64_t
559hammer_blockdemarc(int64_t file_offset1, int64_t file_offset2)
560{
561 if (file_offset1 < HAMMER_XDEMARC) {
562 if (file_offset2 <= HAMMER_XDEMARC)
563 return(file_offset2);
564 return(HAMMER_XDEMARC);
565 }
566 panic("hammer_blockdemarc: illegal range %lld %lld\n",
567 file_offset1, file_offset2);
568}
569