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