Merge from vendor branch FILE:
[dragonfly.git] / sys / vfs / hammer / hammer.h
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.h,v 1.26 2008/01/18 07:02:41 dillon Exp $
35  */
36 /*
37  * This header file contains structures used internally by the HAMMERFS
38  * implementation.  See hammer_disk.h for on-disk structures.
39  */
40
41 #include <sys/param.h>
42 #include <sys/types.h>
43 #include <sys/kernel.h>
44 #include <sys/systm.h>
45 #include <sys/tree.h>
46 #include <sys/malloc.h>
47 #include <sys/mount.h>
48 #include <sys/vnode.h>
49 #include <sys/globaldata.h>
50 #include <sys/lockf.h>
51 #include <sys/buf.h>
52 #include <sys/queue.h>
53 #include <sys/globaldata.h>
54
55 #include <sys/buf2.h>
56 #include "hammer_alist.h"
57 #include "hammer_disk.h"
58 #include "hammer_mount.h"
59
60 #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
61
62 MALLOC_DECLARE(M_HAMMER);
63
64 struct hammer_mount;
65
66 /*
67  * Key structure used for custom RB tree inode lookups.  This prototypes
68  * the function hammer_ino_rb_tree_RB_LOOKUP_INFO(root, info).
69  */
70 typedef struct hammer_inode_info {
71         u_int64_t       obj_id;         /* (key) object identifier */
72         hammer_tid_t    obj_asof;       /* (key) snapshot transid or 0 */
73 } *hammer_inode_info_t;
74
75 /*
76  * HAMMER Transaction tracking
77  */
78 struct hammer_transaction {
79         struct hammer_mount *hmp;
80         hammer_tid_t    tid;
81         struct hammer_volume *rootvol;
82 };
83
84 typedef struct hammer_transaction *hammer_transaction_t;
85
86 /*
87  * HAMMER locks
88  */
89 struct hammer_lock {
90         int     refs;           /* active references delay writes */
91         int     lockcount;      /* lock count for exclusive/shared access */
92         int     wanted;
93         struct thread *locktd;
94 };
95
96 static __inline int
97 hammer_islocked(struct hammer_lock *lock)
98 {
99         return(lock->lockcount != 0);
100 }
101
102 static __inline int
103 hammer_isactive(struct hammer_lock *lock)
104 {
105         return(lock->refs != 0);
106 }
107
108 static __inline int
109 hammer_islastref(struct hammer_lock *lock)
110 {
111         return(lock->refs == 1);
112 }
113
114 /*
115  * This inline is specifically optimized for the case where the caller
116  * owns the lock, but wants to know what kind of lock he owns.  A
117  * negative value indicates a shared lock, a positive value indicates
118  * an exclusive lock.
119  */
120 static __inline int
121 hammer_lock_held(struct hammer_lock *lock)
122 {
123         return(lock->lockcount);
124 }
125
126 /*
127  * Structure used to represent an inode in-memory.
128  *
129  * The record and data associated with an inode may be out of sync with
130  * the disk (xDIRTY flags), or not even on the disk at all (ONDISK flag
131  * clear).
132  *
133  * An inode may also hold a cache of unsynchronized records, used for
134  * database and directories only.  Unsynchronized regular file data is
135  * stored in the buffer cache.
136  *
137  * NOTE: A file which is created and destroyed within the initial
138  * synchronization period can wind up not doing any disk I/O at all.
139  *
140  * Finally, an inode may cache numerous disk-referencing B-Tree cursors.
141  */
142 struct hammer_ino_rb_tree;
143 struct hammer_inode;
144 RB_HEAD(hammer_ino_rb_tree, hammer_inode);
145 RB_PROTOTYPEX(hammer_ino_rb_tree, INFO, hammer_inode, rb_node,
146               hammer_ino_rb_compare, hammer_inode_info_t);
147
148 struct hammer_rec_rb_tree;
149 struct hammer_record;
150 RB_HEAD(hammer_rec_rb_tree, hammer_record);
151 RB_PROTOTYPEX(hammer_rec_rb_tree, INFO, hammer_record, rb_node,
152               hammer_rec_rb_compare, hammer_base_elm_t);
153
154 TAILQ_HEAD(hammer_node_list, hammer_node);
155
156 struct hammer_inode {
157         RB_ENTRY(hammer_inode) rb_node;
158         u_int64_t       obj_id;         /* (key) object identifier */
159         hammer_tid_t    obj_asof;       /* (key) snapshot transid or 0 */
160         hammer_tid_t    last_tid;       /* last modified tid (for fsync) */
161         struct hammer_mount *hmp;
162         int             flags;
163         struct vnode    *vp;
164         struct lockf    advlock;
165         struct hammer_lock lock;
166         struct hammer_inode_record ino_rec;
167         struct hammer_inode_data ino_data;
168         struct hammer_rec_rb_tree rec_tree;     /* red-black record tree */
169         struct hammer_node      *cache[2];      /* search initiate cache */
170 };
171
172 typedef struct hammer_inode *hammer_inode_t;
173
174 #define VTOI(vp)        ((struct hammer_inode *)(vp)->v_data)
175
176 #define HAMMER_INODE_DDIRTY     0x0001  /* in-memory ino_data is dirty */
177 #define HAMMER_INODE_RDIRTY     0x0002  /* in-memory ino_rec is dirty */
178 #define HAMMER_INODE_ITIMES     0x0004  /* in-memory mtime/atime modified */
179 #define HAMMER_INODE_XDIRTY     0x0008  /* in-memory records present */
180 #define HAMMER_INODE_ONDISK     0x0010  /* inode is on-disk (else not yet) */
181 #define HAMMER_INODE_FLUSH      0x0020  /* flush on last ref */
182 #define HAMMER_INODE_DELETED    0x0080  /* inode ready for deletion */
183 #define HAMMER_INODE_DELONDISK  0x0100  /* delete synchronized to disk */
184 #define HAMMER_INODE_RO         0x0200  /* read-only (because of as-of) */
185 #define HAMMER_INODE_GONE       0x0400  /* delete flushed out */
186 #define HAMMER_INODE_DONDISK    0x0800  /* data records may be on disk */
187 #define HAMMER_INODE_BUFS       0x1000  /* dirty high level bps present */
188
189 #define HAMMER_INODE_MODMASK    (HAMMER_INODE_DDIRTY|HAMMER_INODE_RDIRTY| \
190                                  HAMMER_INODE_XDIRTY|HAMMER_INODE_BUFS|   \
191                                  HAMMER_INODE_ITIMES|HAMMER_INODE_DELETED)
192
193 #define HAMMER_MAX_INODE_CURSORS        4
194
195 /*
196  * Structure used to represent an unsynchronized record in-memory.  This
197  * structure is orgranized in a per-inode RB-tree.  If the inode is not
198  * on disk then neither are any records and the in-memory record tree
199  * represents the entire contents of the inode.  If the inode is on disk
200  * then the on-disk B-Tree is scanned in parallel with the in-memory
201  * RB-Tree to synthesize the current state of the file.
202  *
203  * Only current (delete_tid == 0) unsynchronized records are kept in-memory.
204  */
205 struct hammer_record {
206         RB_ENTRY(hammer_record)         rb_node;
207         struct hammer_lock              lock;
208         struct hammer_inode             *ip;
209         union hammer_record_ondisk      rec;
210         union hammer_data_ondisk        *data;
211         int                             flags;
212 };
213
214 typedef struct hammer_record *hammer_record_t;
215
216 #define HAMMER_RECF_ALLOCDATA           0x0001
217 #define HAMMER_RECF_ONRBTREE            0x0002
218 #define HAMMER_RECF_DELETED             0x0004
219 #define HAMMER_RECF_EMBEDDED_DATA       0x0008
220 #define HAMMER_RECF_SYNCING             0x0010
221
222 /*
223  * Structures used to internally represent a volume and a cluster
224  */
225 struct hammer_volume;
226 struct hammer_cluster;
227 struct hammer_supercl;
228 struct hammer_buffer;
229 struct hammer_node;
230 RB_HEAD(hammer_vol_rb_tree, hammer_volume);
231 RB_HEAD(hammer_clu_rb_tree, hammer_cluster);
232 RB_HEAD(hammer_scl_rb_tree, hammer_supercl);
233 RB_HEAD(hammer_buf_rb_tree, hammer_buffer);
234 RB_HEAD(hammer_nod_rb_tree, hammer_node);
235
236 RB_PROTOTYPE2(hammer_vol_rb_tree, hammer_volume, rb_node,
237               hammer_vol_rb_compare, int32_t);
238 RB_PROTOTYPE2(hammer_clu_rb_tree, hammer_cluster, rb_node,
239               hammer_clu_rb_compare, int32_t);
240 RB_PROTOTYPE2(hammer_scl_rb_tree, hammer_supercl, rb_node,
241               hammer_scl_rb_compare, int32_t);
242 RB_PROTOTYPE2(hammer_buf_rb_tree, hammer_buffer, rb_node,
243               hammer_buf_rb_compare, int32_t);
244 RB_PROTOTYPE2(hammer_nod_rb_tree, hammer_node, rb_node,
245               hammer_nod_rb_compare, int32_t);
246
247 /*
248  * IO management - embedded at the head of various in-memory structures
249  */
250 enum hammer_io_type { HAMMER_STRUCTURE_VOLUME,
251                       HAMMER_STRUCTURE_SUPERCL,
252                       HAMMER_STRUCTURE_CLUSTER,
253                       HAMMER_STRUCTURE_BUFFER };
254
255 union hammer_io_structure;
256 struct hammer_io;
257
258 struct worklist {
259         LIST_ENTRY(worklist) node;
260 };
261
262 TAILQ_HEAD(hammer_io_list, hammer_io);
263
264 struct hammer_io {
265         struct worklist worklist;
266         struct hammer_lock lock;
267         enum hammer_io_type type;
268         struct buf      *bp;
269         int64_t         offset;
270         TAILQ_ENTRY(hammer_io) entry;   /* based on modified flag */
271         struct hammer_io_list  *entry_list;
272         struct hammer_io_list  deplist;
273         u_int           modified : 1;   /* bp's data was modified */
274         u_int           released : 1;   /* bp released (w/ B_LOCKED set) */
275         u_int           running : 1;    /* bp write IO in progress */
276         u_int           waiting : 1;    /* someone is waiting on us */
277 };
278
279 typedef struct hammer_io *hammer_io_t;
280
281 /*
282  * In-memory volume representing on-disk buffer
283  */
284 struct hammer_volume {
285         struct hammer_io io;
286         RB_ENTRY(hammer_volume) rb_node;
287         struct hammer_clu_rb_tree rb_clus_root;
288         struct hammer_scl_rb_tree rb_scls_root;
289         struct hammer_volume_ondisk *ondisk;
290         struct hammer_alist_live alist;
291         int32_t vol_no;
292         int32_t vol_clsize;
293         int32_t clu_iterator;   /* cluster allocation iterator */
294         int64_t nblocks;        /* note: special calculation for statfs */
295         int64_t cluster_base;   /* base offset of cluster 0 */
296         char    *vol_name;
297         struct vnode *devvp;
298         struct hammer_mount *hmp;
299         int     vol_flags;
300 };
301
302 typedef struct hammer_volume *hammer_volume_t;
303
304 /*
305  * In-memory super-cluster representing on-disk buffer
306  */
307 struct hammer_supercl {
308         struct hammer_io io;
309         RB_ENTRY(hammer_supercl) rb_node;
310         struct hammer_supercl_ondisk *ondisk;
311         struct hammer_volume *volume;
312         struct hammer_alist_live alist;
313         int32_t scl_no;
314 };
315
316 typedef struct hammer_supercl *hammer_supercl_t;
317
318 /*
319  * In-memory cluster representing on-disk buffer
320  *
321  * The cluster's indexing range is cached in hammer_cluster, separate
322  * from the ondisk info in order to allow cursors to point to it.
323  */
324 struct hammer_cluster {
325         struct hammer_io io;
326         RB_ENTRY(hammer_cluster) rb_node;
327         struct hammer_buf_rb_tree rb_bufs_root;
328         struct hammer_cluster_ondisk *ondisk;
329         struct hammer_volume *volume;
330         struct hammer_alist_live alist_master;
331         struct hammer_alist_live alist_btree;
332         struct hammer_alist_live alist_record;
333         struct hammer_alist_live alist_mdata;
334         struct hammer_nod_rb_tree rb_nods_root; /* cursors in cluster */
335         struct hammer_base_elm clu_btree_beg;   /* copy of on-disk info */
336         struct hammer_base_elm clu_btree_end;   /* copy of on-disk info */
337         int32_t clu_no;
338 };
339
340 typedef struct hammer_cluster *hammer_cluster_t;
341
342 /*
343  * In-memory buffer (other then volume, super-cluster, or cluster),
344  * representing an on-disk buffer.
345  */
346 struct hammer_buffer {
347         struct hammer_io io;
348         RB_ENTRY(hammer_buffer) rb_node;
349         hammer_fsbuf_ondisk_t ondisk;
350         struct hammer_volume *volume;
351         struct hammer_cluster *cluster;
352         int32_t buf_no;
353         u_int64_t buf_type;
354         struct hammer_alist_live alist;
355         struct hammer_node_list clist;
356 };
357
358 typedef struct hammer_buffer *hammer_buffer_t;
359
360 /*
361  * In-memory B-Tree node, representing an on-disk B-Tree node.
362  *
363  * This is a hang-on structure which is backed by a hammer_buffer,
364  * indexed by a hammer_cluster, and used for fine-grained locking of
365  * B-Tree nodes in order to properly control lock ordering.  A hammer_buffer
366  * can contain multiple nodes representing wildly disassociated portions
367  * of the B-Tree so locking cannot be done on a buffer-by-buffer basis.
368  *
369  * This structure uses a cluster-relative index to reduce the number
370  * of layers required to access it, and also because all on-disk B-Tree
371  * references are cluster-relative offsets.
372  */
373 struct hammer_node {
374         struct hammer_lock      lock;           /* node-by-node lock */
375         TAILQ_ENTRY(hammer_node) entry;         /* per-buffer linkage */
376         RB_ENTRY(hammer_node)   rb_node;        /* per-cluster linkage */
377         int32_t                 node_offset;    /* cluster-rel offset */
378         struct hammer_cluster   *cluster;
379         struct hammer_buffer    *buffer;        /* backing buffer */
380         hammer_node_ondisk_t    ondisk;         /* ptr to on-disk structure */
381         struct hammer_node      **cache1;       /* passive cache(s) */
382         struct hammer_node      **cache2;
383         int                     flags;
384 };
385
386 #define HAMMER_NODE_DELETED     0x0001
387 #define HAMMER_NODE_FLUSH       0x0002
388
389 typedef struct hammer_node      *hammer_node_t;
390
391 /*
392  * Common I/O management structure - embedded in in-memory structures
393  * which are backed by filesystem buffers.
394  */
395 union hammer_io_structure {
396         struct hammer_io        io;
397         struct hammer_volume    volume;
398         struct hammer_supercl   supercl;
399         struct hammer_cluster   cluster;
400         struct hammer_buffer    buffer;
401 };
402
403 typedef union hammer_io_structure *hammer_io_structure_t;
404
405 #define HAMFS_CLUSTER_DIRTY     0x0001
406
407 #include "hammer_cursor.h"
408
409 /*
410  * Internal hammer mount data structure
411  */
412 struct hammer_mount {
413         struct mount *mp;
414         /*struct vnode *rootvp;*/
415         struct hammer_ino_rb_tree rb_inos_root;
416         struct hammer_vol_rb_tree rb_vols_root;
417         struct hammer_volume *rootvol;
418         struct hammer_cluster *rootcl;
419         char    *zbuf;  /* HAMMER_BUFSIZE bytes worth of all-zeros */
420         int     hflags;
421         int     ronly;
422         int     nvolumes;
423         int     volume_iterator;
424         uuid_t  fsid;
425         udev_t  fsid_udev;
426         hammer_tid_t asof;
427         u_int32_t namekey_iterator;
428 };
429
430 typedef struct hammer_mount     *hammer_mount_t;
431
432 struct hammer_sync_info {
433         int error;
434         int waitfor;
435 };
436
437 #endif
438
439 #if defined(_KERNEL)
440
441 extern struct vop_ops hammer_vnode_vops;
442 extern struct vop_ops hammer_spec_vops;
443 extern struct vop_ops hammer_fifo_vops;
444 extern struct hammer_alist_config Buf_alist_config;
445 extern struct hammer_alist_config Vol_normal_alist_config;
446 extern struct hammer_alist_config Vol_super_alist_config;
447 extern struct hammer_alist_config Supercl_alist_config;
448 extern struct hammer_alist_config Clu_master_alist_config;
449 extern struct hammer_alist_config Clu_slave_alist_config;
450 extern struct bio_ops hammer_bioops;
451
452 extern int hammer_debug_btree;
453 extern int hammer_debug_tid;
454 extern int hammer_count_inodes;
455 extern int hammer_count_records;
456 extern int hammer_count_record_datas;
457 extern int hammer_count_volumes;
458 extern int hammer_count_supercls;
459 extern int hammer_count_clusters;
460 extern int hammer_count_buffers;
461 extern int hammer_count_nodes;
462 extern int hammer_count_spikes;
463
464 int     hammer_vop_inactive(struct vop_inactive_args *);
465 int     hammer_vop_reclaim(struct vop_reclaim_args *);
466 int     hammer_vfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp);
467 int     hammer_get_vnode(struct hammer_inode *ip, int lktype,
468                         struct vnode **vpp);
469 struct hammer_inode *hammer_get_inode(hammer_mount_t hmp,
470                         struct hammer_node **cache,
471                         u_int64_t obj_id, hammer_tid_t asof, int flags,
472                         int *errorp);
473 void    hammer_put_inode(struct hammer_inode *ip);
474 void    hammer_put_inode_ref(struct hammer_inode *ip);
475
476 int     hammer_unload_inode(hammer_inode_t ip, void *data);
477 int     hammer_unload_volume(hammer_volume_t volume, void *data __unused);
478 int     hammer_unload_supercl(hammer_supercl_t supercl, void *data __unused);
479 int     hammer_unload_cluster(hammer_cluster_t cluster, void *data __unused);
480 void    hammer_update_syncid(hammer_cluster_t cluster, hammer_tid_t tid);
481 int     hammer_unload_buffer(hammer_buffer_t buffer, void *data __unused);
482 int     hammer_install_volume(hammer_mount_t hmp, const char *volname);
483
484 int     hammer_ip_lookup(hammer_cursor_t cursor, hammer_inode_t ip);
485 int     hammer_ip_first(hammer_cursor_t cursor, hammer_inode_t ip);
486 int     hammer_ip_next(hammer_cursor_t cursor);
487 int     hammer_ip_resolve_data(hammer_cursor_t cursor);
488 int     hammer_ip_delete_record(hammer_cursor_t cursor, hammer_tid_t tid);
489 int     hammer_ip_check_directory_empty(hammer_transaction_t trans,
490                         hammer_inode_t ip);
491 int     hammer_sync_hmp(hammer_mount_t hmp, int waitfor);
492 int     hammer_sync_volume(hammer_volume_t volume, void *data);
493 int     hammer_sync_cluster(hammer_cluster_t cluster, void *data);
494 int     hammer_sync_buffer(hammer_buffer_t buffer, void *data);
495
496 hammer_record_t
497         hammer_alloc_mem_record(hammer_inode_t ip);
498 void    hammer_rel_mem_record(hammer_record_t record);
499
500 int     hammer_cursor_up(hammer_cursor_t cursor);
501 int     hammer_cursor_toroot(hammer_cursor_t cursor);
502 int     hammer_cursor_down(hammer_cursor_t cursor);
503 int     hammer_cursor_upgrade(hammer_cursor_t cursor);
504 void    hammer_cursor_downgrade(hammer_cursor_t cursor);
505
506 void    hammer_lock_ex(struct hammer_lock *lock);
507 int     hammer_lock_ex_try(struct hammer_lock *lock);
508 void    hammer_lock_sh(struct hammer_lock *lock);
509 int     hammer_lock_upgrade(struct hammer_lock *lock);
510 void    hammer_lock_downgrade(struct hammer_lock *lock);
511 void    hammer_unlock(struct hammer_lock *lock);
512 void    hammer_ref(struct hammer_lock *lock);
513 void    hammer_unref(struct hammer_lock *lock);
514
515 u_int32_t hammer_to_unix_xid(uuid_t *uuid);
516 void hammer_guid_to_uuid(uuid_t *uuid, u_int32_t guid);
517 void    hammer_to_timespec(hammer_tid_t tid, struct timespec *ts);
518 hammer_tid_t hammer_timespec_to_transid(struct timespec *ts);
519 hammer_tid_t hammer_alloc_tid(hammer_transaction_t trans);
520 hammer_tid_t hammer_now_tid(void);
521 hammer_tid_t hammer_str_to_tid(const char *str);
522 hammer_tid_t hammer_alloc_recid(hammer_transaction_t trans);
523
524 enum vtype hammer_get_vnode_type(u_int8_t obj_type);
525 int hammer_get_dtype(u_int8_t obj_type);
526 u_int8_t hammer_get_obj_type(enum vtype vtype);
527 int64_t hammer_directory_namekey(void *name, int len);
528
529 int     hammer_init_cursor_hmp(hammer_cursor_t cursor, struct hammer_node **cache, hammer_mount_t hmp);
530 int     hammer_init_cursor_cluster(hammer_cursor_t cursor, hammer_cluster_t cluster);
531
532 void    hammer_done_cursor(hammer_cursor_t cursor);
533 void    hammer_mem_done(hammer_cursor_t cursor);
534
535 int     hammer_btree_lookup(hammer_cursor_t cursor);
536 int     hammer_btree_first(hammer_cursor_t cursor);
537 int     hammer_btree_extract(hammer_cursor_t cursor, int flags);
538 int     hammer_btree_iterate(hammer_cursor_t cursor);
539 int     hammer_btree_insert(hammer_cursor_t cursor, hammer_btree_elm_t elm);
540 int     hammer_btree_insert_cluster(hammer_cursor_t cursor,
541                         hammer_cluster_t cluster, int32_t rec_offset);
542 int     hammer_btree_delete(hammer_cursor_t cursor);
543 int     hammer_btree_cmp(hammer_base_elm_t key1, hammer_base_elm_t key2);
544 int     hammer_btree_chkts(hammer_tid_t ts, hammer_base_elm_t key);
545 void    hammer_make_base_inclusive(hammer_base_elm_t key);
546
547 void    hammer_print_btree_node(hammer_node_ondisk_t ondisk);
548 void    hammer_print_btree_elm(hammer_btree_elm_t elm, u_int8_t type, int i);
549
550 void    *hammer_bread(struct hammer_cluster *cluster, int32_t cloff,
551                         u_int64_t buf_type, int *errorp,
552                         struct hammer_buffer **bufferp);
553
554 hammer_volume_t hammer_get_root_volume(hammer_mount_t hmp, int *errorp);
555 hammer_cluster_t hammer_get_root_cluster(hammer_mount_t hmp, int *errorp);
556
557 hammer_volume_t hammer_get_volume(hammer_mount_t hmp,
558                         int32_t vol_no, int *errorp);
559 hammer_supercl_t hammer_get_supercl(hammer_volume_t volume, int32_t scl_no,
560                         int *errorp, hammer_alloc_state_t isnew);
561 hammer_cluster_t hammer_get_cluster(hammer_volume_t volume, int32_t clu_no,
562                         int *errorp, hammer_alloc_state_t isnew);
563 hammer_buffer_t hammer_get_buffer(hammer_cluster_t cluster,
564                         int32_t buf_no, u_int64_t buf_type, int *errorp);
565
566 int             hammer_ref_volume(hammer_volume_t volume);
567 int             hammer_ref_cluster(hammer_cluster_t cluster);
568 int             hammer_ref_buffer(hammer_buffer_t buffer);
569 void            hammer_flush_buffer_nodes(hammer_buffer_t buffer);
570
571 void            hammer_rel_volume(hammer_volume_t volume, int flush);
572 void            hammer_rel_supercl(hammer_supercl_t supercl, int flush);
573 void            hammer_rel_cluster(hammer_cluster_t cluster, int flush);
574 void            hammer_rel_buffer(hammer_buffer_t buffer, int flush);
575
576 hammer_node_t   hammer_get_node(hammer_cluster_t cluster,
577                         int32_t node_offset, int *errorp);
578 int             hammer_ref_node(hammer_node_t node);
579 hammer_node_t   hammer_ref_node_safe(struct hammer_mount *hmp,
580                         struct hammer_node **cache, int *errorp);
581 void            hammer_rel_node(hammer_node_t node);
582 void            hammer_cache_node(hammer_node_t node,
583                         struct hammer_node **cache);
584 void            hammer_uncache_node(struct hammer_node **cache);
585 void            hammer_flush_node(hammer_node_t node);
586
587 void hammer_dup_buffer(struct hammer_buffer **bufferp,
588                         struct hammer_buffer *buffer);
589 void hammer_dup_cluster(struct hammer_cluster **clusterp,
590                         struct hammer_cluster *cluster);
591 hammer_cluster_t hammer_alloc_cluster(hammer_mount_t hmp,
592                         hammer_cluster_t cluster_hint, int *errorp);
593 void hammer_init_cluster(hammer_cluster_t cluster,
594                         hammer_base_elm_t left_bound,
595                         hammer_base_elm_t right_bound);
596 hammer_node_t hammer_alloc_btree(struct hammer_cluster *cluster, int *errorp);
597 void *hammer_alloc_data(struct hammer_cluster *cluster, int32_t bytes,
598                         int *errorp, struct hammer_buffer **bufferp);
599 void *hammer_alloc_record(struct hammer_cluster *cluster,
600                         int *errorp, struct hammer_buffer **bufferp);
601 void hammer_initbuffer(hammer_alist_t live, hammer_fsbuf_head_t head,
602                         u_int64_t type);
603 void hammer_free_data_ptr(struct hammer_buffer *buffer, 
604                         void *data, int bytes);
605 void hammer_free_record_ptr(struct hammer_buffer *buffer,
606                         union hammer_record_ondisk *rec);
607 void hammer_free_cluster(hammer_cluster_t cluster);
608 void hammer_free_btree(struct hammer_cluster *cluster, int32_t bclu_offset);
609 void hammer_free_data(struct hammer_cluster *cluster, int32_t bclu_offset,
610                         int32_t bytes);
611 void hammer_free_record(struct hammer_cluster *cluster, int32_t bclu_offset);
612
613 void hammer_put_volume(struct hammer_volume *volume, int flush);
614 void hammer_put_supercl(struct hammer_supercl *supercl, int flush);
615 void hammer_put_cluster(struct hammer_cluster *cluster, int flush);
616 void hammer_put_buffer(struct hammer_buffer *buffer, int flush);
617
618 void hammer_init_alist_config(void);
619
620 void hammer_start_transaction(struct hammer_transaction *trans,
621                               struct hammer_mount *hmp);
622 void hammer_start_transaction_tid(struct hammer_transaction *trans,
623                                   struct hammer_mount *hmp, hammer_tid_t tid);
624 void hammer_commit_transaction(struct hammer_transaction *trans);
625 void hammer_abort_transaction(struct hammer_transaction *trans);
626
627 void hammer_modify_inode(struct hammer_transaction *trans,
628                         hammer_inode_t ip, int flags);
629 int  hammer_create_inode(struct hammer_transaction *trans, struct vattr *vap,
630                         struct ucred *cred, struct hammer_inode *dip,
631                         struct hammer_inode **ipp);
632 void hammer_rel_inode(hammer_inode_t ip, int flush);
633 int hammer_sync_inode(hammer_inode_t ip, int waitfor, int handle_delete);
634
635 int  hammer_ip_add_directory(struct hammer_transaction *trans,
636                         hammer_inode_t dip, struct namecache *ncp,
637                         hammer_inode_t nip);
638 int  hammer_ip_del_directory(struct hammer_transaction *trans,
639                         hammer_cursor_t cursor, hammer_inode_t dip,
640                         hammer_inode_t ip);
641 int  hammer_ip_add_record(struct hammer_transaction *trans,
642                         hammer_record_t record);
643 int  hammer_ip_delete_range(struct hammer_transaction *trans,
644                         hammer_inode_t ip, int64_t ran_beg, int64_t ran_end,
645                         struct hammer_cursor **spikep);
646 int  hammer_ip_delete_range_all(struct hammer_transaction *trans,
647                         hammer_inode_t ip);
648 int  hammer_ip_sync_data(struct hammer_transaction *trans,
649                         hammer_inode_t ip, int64_t offset,
650                         void *data, int bytes, struct hammer_cursor **spikep);
651 int  hammer_ip_sync_record(hammer_record_t rec, struct hammer_cursor **spikep);
652 int  hammer_write_record(hammer_cursor_t cursor, hammer_record_ondisk_t rec,
653                         void *data, int cursor_flags);
654
655 void hammer_load_spike(hammer_cursor_t cursor, struct hammer_cursor **spikep);
656 int hammer_spike(struct hammer_cursor **spikep);
657 int hammer_recover(struct hammer_cluster *cluster);
658
659 void hammer_io_init(hammer_io_t io, enum hammer_io_type type);
660 int hammer_io_read(struct vnode *devvp, struct hammer_io *io);
661 int hammer_io_new(struct vnode *devvp, struct hammer_io *io);
662 void hammer_io_release(struct hammer_io *io, int flush);
663 void hammer_io_flush(struct hammer_io *io);
664 int hammer_io_checkflush(hammer_io_t io);
665 void hammer_io_notify_cluster(hammer_cluster_t cluster);
666 void hammer_io_clear_modify(struct hammer_io *io);
667 void hammer_io_waitdep(struct hammer_io *io);
668
669 void hammer_modify_volume(hammer_volume_t volume);
670 void hammer_modify_supercl(hammer_supercl_t supercl);
671 void hammer_modify_cluster(hammer_cluster_t cluster);
672 void hammer_modify_buffer(hammer_buffer_t buffer);
673
674 #endif
675
676 static __inline void
677 hammer_modify_node(struct hammer_node *node)
678 {
679         hammer_modify_buffer(node->buffer);
680 }
681
682 /*
683  * Return the cluster-relative byte offset of an element within a buffer
684  */
685 static __inline int
686 hammer_bclu_offset(struct hammer_buffer *buffer, void *ptr)
687 {
688         int bclu_offset;
689
690         bclu_offset = buffer->buf_no * HAMMER_BUFSIZE + 
691                       ((char *)ptr - (char *)buffer->ondisk);
692         return(bclu_offset);
693 }
694