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