Sweep over our manual pages and remove .Pp before a .Bd or .Bl without
[dragonfly.git] / sys / vfs / hammer / hammer.h
1 /*
2  * Copyright (c) 2007-2008 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.55 2008/05/02 01:00:42 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/conf.h>
45 #include <sys/systm.h>
46 #include <sys/tree.h>
47 #include <sys/malloc.h>
48 #include <sys/mount.h>
49 #include <sys/mountctl.h>
50 #include <sys/vnode.h>
51 #include <sys/proc.h>
52 #include <sys/globaldata.h>
53 #include <sys/lockf.h>
54 #include <sys/buf.h>
55 #include <sys/queue.h>
56 #include <sys/globaldata.h>
57
58 #include <sys/buf2.h>
59 #include <sys/signal2.h>
60 #include "hammer_disk.h"
61 #include "hammer_mount.h"
62 #include "hammer_ioctl.h"
63
64 #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
65
66 MALLOC_DECLARE(M_HAMMER);
67
68 struct hammer_mount;
69
70 /*
71  * Key structure used for custom RB tree inode lookups.  This prototypes
72  * the function hammer_ino_rb_tree_RB_LOOKUP_INFO(root, info).
73  */
74 typedef struct hammer_inode_info {
75         int64_t         obj_id;         /* (key) object identifier */
76         hammer_tid_t    obj_asof;       /* (key) snapshot transid or 0 */
77 } *hammer_inode_info_t;
78
79 typedef enum hammer_transaction_type {
80         HAMMER_TRANS_RO,
81         HAMMER_TRANS_STD,
82         HAMMER_TRANS_FLS
83 } hammer_transaction_type_t;
84
85 /*
86  * HAMMER Transaction tracking
87  */
88 struct hammer_transaction {
89         hammer_transaction_type_t type;
90         struct hammer_mount *hmp;
91         hammer_tid_t    tid;
92         hammer_tid_t    time;
93         struct hammer_volume *rootvol;
94 };
95
96 typedef struct hammer_transaction *hammer_transaction_t;
97
98 /*
99  * HAMMER locks
100  */
101 struct hammer_lock {
102         int     refs;           /* active references delay writes */
103         int     lockcount;      /* lock count for exclusive/shared access */
104         int     wanted;
105         struct thread *locktd;
106 };
107
108 static __inline int
109 hammer_islocked(struct hammer_lock *lock)
110 {
111         return(lock->lockcount != 0);
112 }
113
114 static __inline int
115 hammer_isactive(struct hammer_lock *lock)
116 {
117         return(lock->refs != 0);
118 }
119
120 static __inline int
121 hammer_islastref(struct hammer_lock *lock)
122 {
123         return(lock->refs == 1);
124 }
125
126 /*
127  * Return if we specifically own the lock exclusively.
128  */
129 static __inline int
130 hammer_lock_excl_owned(struct hammer_lock *lock, thread_t td)
131 {
132         if (lock->lockcount > 0 && lock->locktd == td)
133                 return(1);
134         return(0);
135 }
136
137 /*
138  * Flush state, used by various structures
139  */
140 typedef enum hammer_inode_state {
141         HAMMER_FST_IDLE,
142         HAMMER_FST_SETUP,
143         HAMMER_FST_FLUSH
144 } hammer_inode_state_t;
145
146 TAILQ_HEAD(hammer_record_list, hammer_record);
147
148 /*
149  * Cache object ids.  A fixed number of objid cache structures are
150  * created to reserve object id's for newly created files in multiples
151  * of 100,000, localized to a particular directory, and recycled as
152  * needed.  This allows parallel create operations in different
153  * directories to retain fairly localized object ids which in turn
154  * improves reblocking performance and layout.
155  */
156 #define OBJID_CACHE_SIZE        128
157 #define OBJID_CACHE_BULK        100000
158
159 typedef struct hammer_objid_cache {
160         TAILQ_ENTRY(hammer_objid_cache) entry;
161         struct hammer_inode             *dip;
162         hammer_tid_t                    next_tid;
163         int                             count;
164 } *hammer_objid_cache_t;
165
166 /*
167  * Structure used to represent an inode in-memory.
168  *
169  * The record and data associated with an inode may be out of sync with
170  * the disk (xDIRTY flags), or not even on the disk at all (ONDISK flag
171  * clear).
172  *
173  * An inode may also hold a cache of unsynchronized records, used for
174  * database and directories only.  Unsynchronized regular file data is
175  * stored in the buffer cache.
176  *
177  * NOTE: A file which is created and destroyed within the initial
178  * synchronization period can wind up not doing any disk I/O at all.
179  *
180  * Finally, an inode may cache numerous disk-referencing B-Tree cursors.
181  */
182 struct hammer_ino_rb_tree;
183 struct hammer_inode;
184 RB_HEAD(hammer_ino_rb_tree, hammer_inode);
185 RB_PROTOTYPEX(hammer_ino_rb_tree, INFO, hammer_inode, rb_node,
186               hammer_ino_rb_compare, hammer_inode_info_t);
187
188 struct hammer_rec_rb_tree;
189 struct hammer_record;
190 RB_HEAD(hammer_rec_rb_tree, hammer_record);
191 RB_PROTOTYPEX(hammer_rec_rb_tree, INFO, hammer_record, rb_node,
192               hammer_rec_rb_compare, hammer_base_elm_t);
193
194 TAILQ_HEAD(hammer_node_list, hammer_node);
195
196 struct hammer_inode {
197         RB_ENTRY(hammer_inode)  rb_node;
198         hammer_inode_state_t    flush_state;
199         int                     flush_group;
200         TAILQ_ENTRY(hammer_inode) flush_entry;
201         struct hammer_record_list target_list;  /* target of dependant recs */
202         u_int64_t               obj_id;         /* (key) object identifier */
203         hammer_tid_t            obj_asof;       /* (key) snapshot or 0 */
204         struct hammer_mount     *hmp;
205         hammer_objid_cache_t    objid_cache;
206         int                     flags;
207         int                     error;          /* flush error */
208         int                     cursor_ip_refs; /* sanity */
209         struct vnode            *vp;
210         struct lockf            advlock;
211         struct hammer_lock      lock;           /* sync copy interlock */
212         TAILQ_HEAD(, bio)       bio_list;       /* BIOs to flush out */
213         TAILQ_HEAD(, bio)       bio_alt_list;   /* BIOs to flush out */
214         off_t                   trunc_off;
215         struct hammer_inode_record ino_rec;     /* in-memory cache */
216         struct hammer_inode_data ino_data;      /* in-memory cache */
217         struct hammer_rec_rb_tree rec_tree;     /* in-memory cache */
218         struct hammer_node      *cache[2];      /* search initiate cache */
219
220         /*
221          * When a demark is created to synchronize an inode to
222          * disk, certain fields are copied so the front-end VOPs
223          * can continue to run in parallel with the synchronization
224          * occuring in the background.
225          */
226         int             sync_flags;             /* to-sync flags cache */
227         off_t           sync_trunc_off;         /* to-sync truncation */
228         struct hammer_inode_record sync_ino_rec;/* to-sync cache */
229         struct hammer_inode_data sync_ino_data; /* to-sync cache */
230 };
231
232 typedef struct hammer_inode *hammer_inode_t;
233
234 #define VTOI(vp)        ((struct hammer_inode *)(vp)->v_data)
235
236 #define HAMMER_INODE_DDIRTY     0x0001  /* in-memory ino_data is dirty */
237 #define HAMMER_INODE_RDIRTY     0x0002  /* in-memory ino_rec is dirty */
238 #define HAMMER_INODE_ITIMES     0x0004  /* in-memory mtime/atime modified */
239 #define HAMMER_INODE_XDIRTY     0x0008  /* in-memory records */
240 #define HAMMER_INODE_ONDISK     0x0010  /* inode is on-disk (else not yet) */
241 #define HAMMER_INODE_FLUSH      0x0020  /* flush on last ref */
242 #define HAMMER_INODE_DELETED    0x0080  /* inode ready for deletion */
243 #define HAMMER_INODE_DELONDISK  0x0100  /* delete synchronized to disk */
244 #define HAMMER_INODE_RO         0x0200  /* read-only (because of as-of) */
245 #define HAMMER_INODE_GONE       0x0400  /* delete flushed out */
246 #define HAMMER_INODE_DONDISK    0x0800  /* data records may be on disk */
247 #define HAMMER_INODE_BUFS       0x1000  /* dirty high level bps present */
248 #define HAMMER_INODE_REFLUSH    0x2000  /* pipelined flush during flush */
249 #define HAMMER_INODE_WRITE_ALT  0x4000  /* strategy writes to alt bioq */
250 #define HAMMER_INODE_FLUSHW     0x8000  /* Someone waiting for flush */
251
252 #define HAMMER_INODE_TRUNCATED  0x00010000
253 #define HAMMER_INODE_DELETING   0x00020000 /* Destroy the inode on-disk */
254
255 #define HAMMER_INODE_MODMASK    (HAMMER_INODE_DDIRTY|HAMMER_INODE_RDIRTY| \
256                                  HAMMER_INODE_XDIRTY|HAMMER_INODE_BUFS|   \
257                                  HAMMER_INODE_ITIMES|HAMMER_INODE_TRUNCATED|\
258                                  HAMMER_INODE_DELETING)
259
260 #define HAMMER_INODE_MODMASK_NOXDIRTY \
261                                 (HAMMER_INODE_MODMASK & ~HAMMER_INODE_XDIRTY)
262
263 #define HAMMER_MAX_INODE_CURSORS        4
264
265 #define HAMMER_FLUSH_SIGNAL     0x0001
266 #define HAMMER_FLUSH_FORCE      0x0002
267 #define HAMMER_FLUSH_RECURSION  0x0004
268
269 /*
270  * Structure used to represent an unsynchronized record in-memory.  These
271  * records typically represent directory entries.  Only non-historical
272  * records are kept in-memory.
273  *
274  * Records are organized as a per-inode RB-Tree.  If the inode is not
275  * on disk then neither are any records and the in-memory record tree
276  * represents the entire contents of the inode.  If the inode is on disk
277  * then the on-disk B-Tree is scanned in parallel with the in-memory
278  * RB-Tree to synthesize the current state of the file.
279  *
280  * Records are also used to enforce the ordering of directory create/delete
281  * operations.  A new inode will not be flushed to disk unless its related
282  * directory entry is also being flushed at the same time.  A directory entry
283  * will not be removed unless its related inode is also being removed at the
284  * same time.
285  */
286 typedef enum hammer_record_type {
287         HAMMER_MEM_RECORD_ADD,          /* positive memory cache record */
288         HAMMER_MEM_RECORD_DEL           /* negative delete-on-disk record */
289 } hammer_record_type_t;
290
291 struct hammer_record {
292         RB_ENTRY(hammer_record)         rb_node;
293         TAILQ_ENTRY(hammer_record)      target_entry;
294         hammer_inode_state_t            flush_state;
295         int                             flush_group;
296         hammer_record_type_t            type;
297         struct hammer_lock              lock;
298         struct hammer_inode             *ip;
299         struct hammer_inode             *target_ip;
300         union hammer_record_ondisk      rec;
301         union hammer_data_ondisk        *data;
302         int                             flags;
303 };
304
305 typedef struct hammer_record *hammer_record_t;
306
307 /*
308  * Record flags.  Note that FE can only be set by the frontend if the
309  * record has not been interlocked by the backend w/ BE.
310  */
311 #define HAMMER_RECF_ALLOCDATA           0x0001
312 #define HAMMER_RECF_ONRBTREE            0x0002
313 #define HAMMER_RECF_DELETED_FE          0x0004  /* deleted (frontend) */
314 #define HAMMER_RECF_DELETED_BE          0x0008  /* deleted (backend) */
315 #define HAMMER_RECF_INBAND              0x0010
316 #define HAMMER_RECF_INTERLOCK_BE        0x0020  /* backend interlock */
317 #define HAMMER_RECF_WANTED              0x0040
318 #define HAMMER_RECF_CONVERT_DELETE      0x0100 /* special case */
319
320 /*
321  * In-memory structures representing on-disk structures.
322  */
323 struct hammer_volume;
324 struct hammer_buffer;
325 struct hammer_node;
326 RB_HEAD(hammer_vol_rb_tree, hammer_volume);
327 RB_HEAD(hammer_buf_rb_tree, hammer_buffer);
328 RB_HEAD(hammer_nod_rb_tree, hammer_node);
329
330 RB_PROTOTYPE2(hammer_vol_rb_tree, hammer_volume, rb_node,
331               hammer_vol_rb_compare, int32_t);
332 RB_PROTOTYPE2(hammer_buf_rb_tree, hammer_buffer, rb_node,
333               hammer_buf_rb_compare, hammer_off_t);
334 RB_PROTOTYPE2(hammer_nod_rb_tree, hammer_node, rb_node,
335               hammer_nod_rb_compare, hammer_off_t);
336
337 /*
338  * IO management - embedded at the head of various in-memory structures
339  *
340  * VOLUME       - hammer_volume containing meta-data
341  * META_BUFFER  - hammer_buffer containing meta-data
342  * DATA_BUFFER  - hammer_buffer containing pure-data
343  *
344  * Dirty volume headers and dirty meta-data buffers are locked until the
345  * flusher can sequence them out.  Dirty pure-data buffers can be written.
346  * Clean buffers can be passively released.
347  */
348 typedef enum hammer_io_type {
349         HAMMER_STRUCTURE_VOLUME,
350         HAMMER_STRUCTURE_META_BUFFER,
351         HAMMER_STRUCTURE_UNDO_BUFFER,
352         HAMMER_STRUCTURE_DATA_BUFFER
353 } hammer_io_type_t;
354
355 union hammer_io_structure;
356 struct hammer_io;
357
358 struct worklist {
359         LIST_ENTRY(worklist) node;
360 };
361
362 TAILQ_HEAD(hammer_io_list, hammer_io);
363 typedef struct hammer_io_list *hammer_io_list_t;
364
365 struct hammer_io {
366         struct worklist         worklist;
367         struct hammer_lock      lock;
368         enum hammer_io_type     type;
369         struct hammer_mount     *hmp;
370         TAILQ_ENTRY(hammer_io)  mod_entry; /* list entry if modified */
371         hammer_io_list_t        mod_list;
372         struct buf              *bp;
373         int64_t                 offset;
374         int                     loading;   /* loading/unloading interlock */
375         int                     modify_refs;
376
377         u_int           modified : 1;   /* bp's data was modified */
378         u_int           released : 1;   /* bp released (w/ B_LOCKED set) */
379         u_int           running : 1;    /* bp write IO in progress */
380         u_int           waiting : 1;    /* someone is waiting on us */
381         u_int           validated : 1;  /* ondisk has been validated */
382         u_int           flush : 1;      /* flush on last release */
383         u_int           waitdep : 1;    /* flush waits for dependancies */
384 };
385
386 typedef struct hammer_io *hammer_io_t;
387
388 /*
389  * In-memory volume representing on-disk buffer
390  */
391 struct hammer_volume {
392         struct hammer_io io;
393         RB_ENTRY(hammer_volume) rb_node;
394         struct hammer_buf_rb_tree rb_bufs_root;
395         struct hammer_volume_ondisk *ondisk;
396         int32_t vol_no;
397         int64_t nblocks;        /* note: special calculation for statfs */
398         int64_t buffer_base;    /* base offset of buffer 0 */
399         hammer_off_t maxbuf_off; /* Maximum buffer offset */
400         char    *vol_name;
401         struct vnode *devvp;
402         int     vol_flags;
403 };
404
405 typedef struct hammer_volume *hammer_volume_t;
406
407 /*
408  * In-memory buffer (other then volume, super-cluster, or cluster),
409  * representing an on-disk buffer.
410  */
411 struct hammer_buffer {
412         struct hammer_io io;
413         RB_ENTRY(hammer_buffer) rb_node;
414         void *ondisk;
415         struct hammer_volume *volume;
416         hammer_off_t zone2_offset;
417         hammer_off_t zoneX_offset;
418         struct hammer_node_list clist;
419 };
420
421 typedef struct hammer_buffer *hammer_buffer_t;
422
423 /*
424  * In-memory B-Tree node, representing an on-disk B-Tree node.
425  *
426  * This is a hang-on structure which is backed by a hammer_buffer,
427  * indexed by a hammer_cluster, and used for fine-grained locking of
428  * B-Tree nodes in order to properly control lock ordering.  A hammer_buffer
429  * can contain multiple nodes representing wildly disassociated portions
430  * of the B-Tree so locking cannot be done on a buffer-by-buffer basis.
431  *
432  * This structure uses a cluster-relative index to reduce the number
433  * of layers required to access it, and also because all on-disk B-Tree
434  * references are cluster-relative offsets.
435  */
436 struct hammer_node {
437         struct hammer_lock      lock;           /* node-by-node lock */
438         TAILQ_ENTRY(hammer_node) entry;         /* per-buffer linkage */
439         RB_ENTRY(hammer_node)   rb_node;        /* per-cluster linkage */
440         hammer_off_t            node_offset;    /* full offset spec */
441         struct hammer_mount     *hmp;
442         struct hammer_buffer    *buffer;        /* backing buffer */
443         hammer_node_ondisk_t    ondisk;         /* ptr to on-disk structure */
444         struct hammer_node      **cache1;       /* passive cache(s) */
445         struct hammer_node      **cache2;
446         int                     flags;
447         int                     loading;        /* load interlock */
448 };
449
450 #define HAMMER_NODE_DELETED     0x0001
451 #define HAMMER_NODE_FLUSH       0x0002
452
453 typedef struct hammer_node      *hammer_node_t;
454
455 /*
456  * List of locked nodes.
457  */
458 struct hammer_node_locklist {
459         struct hammer_node_locklist *next;
460         hammer_node_t   node;
461 };
462
463 typedef struct hammer_node_locklist *hammer_node_locklist_t;
464
465
466 /*
467  * Common I/O management structure - embedded in in-memory structures
468  * which are backed by filesystem buffers.
469  */
470 union hammer_io_structure {
471         struct hammer_io        io;
472         struct hammer_volume    volume;
473         struct hammer_buffer    buffer;
474 };
475
476 typedef union hammer_io_structure *hammer_io_structure_t;
477
478 /*
479  * Allocation holes are recorded for a short period of time in an attempt
480  * to use up the space.
481  */
482
483 #define HAMMER_MAX_HOLES        8
484
485 struct hammer_hole;
486
487 struct hammer_holes {
488         TAILQ_HEAD(, hammer_hole) list;
489         int     count;
490 };
491
492 typedef struct hammer_holes *hammer_holes_t;
493
494 struct hammer_hole {
495         TAILQ_ENTRY(hammer_hole) entry;
496         hammer_off_t    offset;
497         int             bytes;
498 };
499
500 typedef struct hammer_hole *hammer_hole_t;
501
502 #include "hammer_cursor.h"
503
504 /*
505  * Internal hammer mount data structure
506  */
507 struct hammer_mount {
508         struct mount *mp;
509         /*struct vnode *rootvp;*/
510         struct hammer_ino_rb_tree rb_inos_root;
511         struct hammer_vol_rb_tree rb_vols_root;
512         struct hammer_nod_rb_tree rb_nods_root;
513         struct hammer_volume *rootvol;
514         struct hammer_base_elm root_btree_beg;
515         struct hammer_base_elm root_btree_end;
516         char    *zbuf;  /* HAMMER_BUFSIZE bytes worth of all-zeros */
517         int     hflags;
518         int     ronly;
519         int     nvolumes;
520         int     volume_iterator;
521         int     flusher_signal; /* flusher thread sequencer */
522         int     flusher_act;    /* currently active flush group */
523         int     flusher_done;   /* set to act when complete */
524         int     flusher_next;   /* next flush group */
525         int     flusher_exiting;
526         int     reclaim_count;
527         thread_t flusher_td;
528         u_int   check_interrupt;
529         uuid_t  fsid;
530         udev_t  fsid_udev;
531         struct hammer_io_list volu_list;        /* dirty undo buffers */
532         struct hammer_io_list undo_list;        /* dirty undo buffers */
533         struct hammer_io_list data_list;        /* dirty data buffers */
534         struct hammer_io_list meta_list;        /* dirty meta bufs    */
535         struct hammer_io_list lose_list;        /* loose buffers      */
536         int     locked_dirty_count;             /* meta/volu count    */
537         int     io_running_count;
538         int     objid_cache_count;
539         hammer_tid_t asof;
540         hammer_off_t next_tid;
541         u_int32_t namekey_iterator;
542         hammer_off_t zone_limits[HAMMER_MAX_ZONES];
543         struct netexport export;
544         struct hammer_lock sync_lock;
545         struct lock blockmap_lock;
546         struct hammer_blockmap  blockmap[HAMMER_MAX_ZONES];
547         struct hammer_holes holes[HAMMER_MAX_ZONES];
548         TAILQ_HEAD(, hammer_inode) flush_list;
549         TAILQ_HEAD(, hammer_objid_cache) objid_cache_list;
550 };
551
552 typedef struct hammer_mount     *hammer_mount_t;
553
554 struct hammer_sync_info {
555         int error;
556         int waitfor;
557 };
558
559 #endif
560
561 #if defined(_KERNEL)
562
563 extern struct vop_ops hammer_vnode_vops;
564 extern struct vop_ops hammer_spec_vops;
565 extern struct vop_ops hammer_fifo_vops;
566 extern struct bio_ops hammer_bioops;
567
568 extern int hammer_debug_general;
569 extern int hammer_debug_locks;
570 extern int hammer_debug_btree;
571 extern int hammer_debug_tid;
572 extern int hammer_debug_recover;
573 extern int hammer_debug_recover_faults;
574 extern int hammer_count_inodes;
575 extern int hammer_count_records;
576 extern int hammer_count_record_datas;
577 extern int hammer_count_volumes;
578 extern int hammer_count_buffers;
579 extern int hammer_count_nodes;
580 extern int hammer_count_dirtybufs;
581 extern int hammer_limit_dirtybufs;
582 extern int hammer_bio_count;
583 extern int64_t hammer_contention_count;
584
585 int     hammer_vop_inactive(struct vop_inactive_args *);
586 int     hammer_vop_reclaim(struct vop_reclaim_args *);
587 int     hammer_get_vnode(struct hammer_inode *ip, int lktype,
588                         struct vnode **vpp);
589 struct hammer_inode *hammer_get_inode(hammer_transaction_t trans,
590                         struct hammer_node **cache,
591                         u_int64_t obj_id, hammer_tid_t asof, int flags,
592                         int *errorp);
593 void    hammer_put_inode(struct hammer_inode *ip);
594 void    hammer_put_inode_ref(struct hammer_inode *ip);
595
596 int     hammer_unload_volume(hammer_volume_t volume, void *data __unused);
597 int     hammer_unload_buffer(hammer_buffer_t buffer, void *data __unused);
598 int     hammer_install_volume(hammer_mount_t hmp, const char *volname);
599
600 int     hammer_ip_lookup(hammer_cursor_t cursor, hammer_inode_t ip);
601 int     hammer_ip_first(hammer_cursor_t cursor, hammer_inode_t ip);
602 int     hammer_ip_next(hammer_cursor_t cursor);
603 int     hammer_ip_resolve_record_and_data(hammer_cursor_t cursor);
604 int     hammer_ip_resolve_data(hammer_cursor_t cursor);
605 int     hammer_ip_delete_record(hammer_cursor_t cursor, hammer_tid_t tid);
606 int     hammer_delete_at_cursor(hammer_cursor_t cursor, int64_t *stat_bytes);
607 int     hammer_ip_check_directory_empty(hammer_transaction_t trans,
608                         hammer_cursor_t parent_cursor, hammer_inode_t ip);
609 int     hammer_sync_hmp(hammer_mount_t hmp, int waitfor);
610
611 hammer_record_t
612         hammer_alloc_mem_record(hammer_inode_t ip);
613 void    hammer_flush_record_done(hammer_record_t record, int error);
614 void    hammer_wait_mem_record(hammer_record_t record);
615 void    hammer_rel_mem_record(hammer_record_t record);
616
617 int     hammer_cursor_up(hammer_cursor_t cursor);
618 int     hammer_cursor_down(hammer_cursor_t cursor);
619 int     hammer_cursor_upgrade(hammer_cursor_t cursor);
620 void    hammer_cursor_downgrade(hammer_cursor_t cursor);
621 int     hammer_cursor_seek(hammer_cursor_t cursor, hammer_node_t node,
622                         int index);
623 void    hammer_lock_ex(struct hammer_lock *lock);
624 int     hammer_lock_ex_try(struct hammer_lock *lock);
625 void    hammer_lock_sh(struct hammer_lock *lock);
626 int     hammer_lock_upgrade(struct hammer_lock *lock);
627 void    hammer_lock_downgrade(struct hammer_lock *lock);
628 void    hammer_unlock(struct hammer_lock *lock);
629 void    hammer_ref(struct hammer_lock *lock);
630 void    hammer_unref(struct hammer_lock *lock);
631
632 u_int32_t hammer_to_unix_xid(uuid_t *uuid);
633 void hammer_guid_to_uuid(uuid_t *uuid, u_int32_t guid);
634 void    hammer_to_timespec(hammer_tid_t tid, struct timespec *ts);
635 hammer_tid_t hammer_timespec_to_transid(struct timespec *ts);
636 hammer_tid_t hammer_now_tid(void);
637 hammer_tid_t hammer_str_to_tid(const char *str);
638 hammer_tid_t hammer_alloc_objid(hammer_transaction_t trans, hammer_inode_t dip);
639 void hammer_clear_objid(hammer_inode_t dip);
640 void hammer_destroy_objid_cache(hammer_mount_t hmp);
641
642 enum vtype hammer_get_vnode_type(u_int8_t obj_type);
643 int hammer_get_dtype(u_int8_t obj_type);
644 u_int8_t hammer_get_obj_type(enum vtype vtype);
645 int64_t hammer_directory_namekey(void *name, int len);
646
647 int     hammer_init_cursor(hammer_transaction_t trans, hammer_cursor_t cursor,
648                            struct hammer_node **cache);
649
650 void    hammer_done_cursor(hammer_cursor_t cursor);
651 void    hammer_mem_done(hammer_cursor_t cursor);
652
653 int     hammer_btree_lookup(hammer_cursor_t cursor);
654 int     hammer_btree_first(hammer_cursor_t cursor);
655 int     hammer_btree_last(hammer_cursor_t cursor);
656 int     hammer_btree_extract(hammer_cursor_t cursor, int flags);
657 int     hammer_btree_iterate(hammer_cursor_t cursor);
658 int     hammer_btree_iterate_reverse(hammer_cursor_t cursor);
659 int     hammer_btree_insert(hammer_cursor_t cursor, hammer_btree_elm_t elm);
660 int     hammer_btree_delete(hammer_cursor_t cursor);
661 int     hammer_btree_cmp(hammer_base_elm_t key1, hammer_base_elm_t key2);
662 int     hammer_btree_chkts(hammer_tid_t ts, hammer_base_elm_t key);
663 int     hammer_btree_correct_rhb(hammer_cursor_t cursor, hammer_tid_t tid);
664 int     hammer_btree_correct_lhb(hammer_cursor_t cursor, hammer_tid_t tid);
665
666
667 int     hammer_btree_lock_children(hammer_cursor_t cursor,
668                         struct hammer_node_locklist **locklistp);
669
670 void    hammer_print_btree_node(hammer_node_ondisk_t ondisk);
671 void    hammer_print_btree_elm(hammer_btree_elm_t elm, u_int8_t type, int i);
672
673 void    *hammer_bread(struct hammer_mount *hmp, hammer_off_t off,
674                         int *errorp, struct hammer_buffer **bufferp);
675 void    *hammer_bnew(struct hammer_mount *hmp, hammer_off_t off,
676                         int *errorp, struct hammer_buffer **bufferp);
677
678 hammer_volume_t hammer_get_root_volume(hammer_mount_t hmp, int *errorp);
679 int     hammer_dowrite(hammer_transaction_t trans, hammer_inode_t ip,
680                         struct bio *bio);
681
682 hammer_volume_t hammer_get_volume(hammer_mount_t hmp,
683                         int32_t vol_no, int *errorp);
684 hammer_buffer_t hammer_get_buffer(hammer_mount_t hmp,
685                         hammer_off_t buf_offset, int isnew, int *errorp);
686 void    hammer_uncache_buffer(struct hammer_mount *hmp, hammer_off_t off);
687
688 int             hammer_ref_volume(hammer_volume_t volume);
689 int             hammer_ref_buffer(hammer_buffer_t buffer);
690 void            hammer_flush_buffer_nodes(hammer_buffer_t buffer);
691
692 void            hammer_rel_volume(hammer_volume_t volume, int flush);
693 void            hammer_rel_buffer(hammer_buffer_t buffer, int flush);
694
695 int             hammer_vfs_export(struct mount *mp, int op,
696                         const struct export_args *export);
697 hammer_node_t   hammer_get_node(hammer_mount_t hmp,
698                         hammer_off_t node_offset, int *errorp);
699 void            hammer_ref_node(hammer_node_t node);
700 hammer_node_t   hammer_ref_node_safe(struct hammer_mount *hmp,
701                         struct hammer_node **cache, int *errorp);
702 void            hammer_rel_node(hammer_node_t node);
703 void            hammer_delete_node(hammer_transaction_t trans,
704                         hammer_node_t node);
705 void            hammer_cache_node(hammer_node_t node,
706                         struct hammer_node **cache);
707 void            hammer_uncache_node(struct hammer_node **cache);
708 void            hammer_flush_node(hammer_node_t node);
709
710 void hammer_dup_buffer(struct hammer_buffer **bufferp,
711                         struct hammer_buffer *buffer);
712 hammer_node_t hammer_alloc_btree(hammer_transaction_t trans, int *errorp);
713 void *hammer_alloc_record(hammer_transaction_t trans,
714                         hammer_off_t *rec_offp, u_int16_t rec_type,
715                         struct hammer_buffer **rec_bufferp,
716                         int32_t data_len, void **datap,
717                         struct hammer_buffer **data_bufferp, int *errorp);
718 void *hammer_alloc_data(hammer_transaction_t trans, int32_t data_len,
719                         hammer_off_t *data_offsetp,
720                         struct hammer_buffer **data_bufferp, int *errorp);
721
722 int hammer_generate_undo(hammer_transaction_t trans, hammer_io_t io,
723                         hammer_off_t zone1_offset, void *base, int len);
724
725 void hammer_put_volume(struct hammer_volume *volume, int flush);
726 void hammer_put_buffer(struct hammer_buffer *buffer, int flush);
727
728 hammer_off_t hammer_freemap_alloc(hammer_transaction_t trans,
729                         hammer_off_t owner, int *errorp);
730 void hammer_freemap_free(hammer_transaction_t trans, hammer_off_t phys_offset,
731                         hammer_off_t owner, int *errorp);
732 hammer_off_t hammer_blockmap_alloc(hammer_transaction_t trans, int zone,
733                         int bytes, int *errorp);
734 void hammer_blockmap_free(hammer_transaction_t trans,
735                         hammer_off_t bmap_off, int bytes);
736 int hammer_blockmap_getfree(hammer_mount_t hmp, hammer_off_t bmap_off,
737                         int *curp, int *errorp);
738 hammer_off_t hammer_blockmap_lookup(hammer_mount_t hmp, hammer_off_t bmap_off,
739                         int *errorp);
740 hammer_off_t hammer_undo_lookup(hammer_mount_t hmp, hammer_off_t bmap_off,
741                         int *errorp);
742 int64_t hammer_undo_space(hammer_mount_t hmp);
743 int64_t hammer_undo_max(hammer_mount_t hmp);
744
745
746 void hammer_start_transaction(struct hammer_transaction *trans,
747                               struct hammer_mount *hmp);
748 void hammer_simple_transaction(struct hammer_transaction *trans,
749                               struct hammer_mount *hmp);
750 void hammer_start_transaction_fls(struct hammer_transaction *trans,
751                                   struct hammer_mount *hmp);
752 void hammer_done_transaction(struct hammer_transaction *trans);
753
754 void hammer_modify_inode(struct hammer_transaction *trans,
755                         hammer_inode_t ip, int flags);
756 void hammer_flush_inode(hammer_inode_t ip, int flags);
757 void hammer_flush_inode_done(hammer_inode_t ip);
758 void hammer_wait_inode(hammer_inode_t ip);
759
760 int  hammer_create_inode(struct hammer_transaction *trans, struct vattr *vap,
761                         struct ucred *cred, struct hammer_inode *dip,
762                         struct hammer_inode **ipp);
763 void hammer_rel_inode(hammer_inode_t ip, int flush);
764 int hammer_sync_inode(hammer_inode_t ip);
765 void hammer_test_inode(hammer_inode_t ip);
766
767 int  hammer_ip_add_directory(struct hammer_transaction *trans,
768                         hammer_inode_t dip, struct namecache *ncp,
769                         hammer_inode_t nip);
770 int  hammer_ip_del_directory(struct hammer_transaction *trans,
771                         hammer_cursor_t cursor, hammer_inode_t dip,
772                         hammer_inode_t ip);
773 int  hammer_ip_add_record(struct hammer_transaction *trans,
774                         hammer_record_t record);
775 int  hammer_ip_delete_range(struct hammer_transaction *trans,
776                         hammer_inode_t ip, int64_t ran_beg, int64_t ran_end);
777 int  hammer_ip_delete_range_all(struct hammer_transaction *trans,
778                         hammer_inode_t ip);
779 int  hammer_ip_sync_data(struct hammer_transaction *trans,
780                         hammer_inode_t ip, int64_t offset,
781                         void *data, int bytes);
782 int  hammer_ip_sync_record(hammer_transaction_t trans, hammer_record_t rec);
783
784 int hammer_ioctl(hammer_inode_t ip, u_long com, caddr_t data, int fflag,
785                         struct ucred *cred);
786
787 void hammer_io_init(hammer_io_t io, hammer_mount_t hmp,
788                         enum hammer_io_type type);
789 void hammer_io_reinit(hammer_io_t io, enum hammer_io_type type);
790 int hammer_io_read(struct vnode *devvp, struct hammer_io *io);
791 int hammer_io_new(struct vnode *devvp, struct hammer_io *io);
792 void hammer_io_release(struct hammer_io *io);
793 void hammer_io_flush(struct hammer_io *io);
794 int hammer_io_checkflush(hammer_io_t io);
795 void hammer_io_clear_modify(struct hammer_io *io);
796 void hammer_io_waitdep(struct hammer_io *io);
797
798 void hammer_modify_volume(hammer_transaction_t trans, hammer_volume_t volume,
799                         void *base, int len);
800 void hammer_modify_buffer(hammer_transaction_t trans, hammer_buffer_t buffer,
801                         void *base, int len);
802 void hammer_modify_volume_done(hammer_volume_t volume);
803 void hammer_modify_buffer_done(hammer_buffer_t buffer);
804
805 int hammer_ioc_reblock(hammer_transaction_t trans, hammer_inode_t ip,
806                         struct hammer_ioc_reblock *reblock);
807
808 void hammer_init_holes(hammer_mount_t hmp, hammer_holes_t holes);
809 void hammer_free_holes(hammer_mount_t hmp, hammer_holes_t holes);
810 int hammer_signal_check(hammer_mount_t hmp);
811
812 void hammer_flusher_create(hammer_mount_t hmp);
813 void hammer_flusher_destroy(hammer_mount_t hmp);
814 void hammer_flusher_sync(hammer_mount_t hmp);
815 void hammer_flusher_async(hammer_mount_t hmp);
816
817 int hammer_recover(hammer_mount_t hmp, hammer_volume_t rootvol);
818
819 #endif
820
821 static __inline void
822 hammer_modify_node_noundo(hammer_transaction_t trans, hammer_node_t node)
823 {
824         hammer_modify_buffer(trans, node->buffer, NULL, 0);
825 }
826
827 static __inline void
828 hammer_modify_node_all(hammer_transaction_t trans, struct hammer_node *node)
829 {
830         hammer_modify_buffer(trans, node->buffer,
831                              node->ondisk, sizeof(*node->ondisk));
832 }
833
834 static __inline void
835 hammer_modify_node(hammer_transaction_t trans, hammer_node_t node,
836                    void *base, int len)
837 {
838         KKASSERT((char *)base >= (char *)node->ondisk &&
839                  (char *)base + len <=
840                     (char *)node->ondisk + sizeof(*node->ondisk));
841         hammer_modify_buffer(trans, node->buffer, base, len);
842 }
843
844 static __inline void
845 hammer_modify_node_done(hammer_node_t node)
846 {
847         hammer_modify_buffer_done(node->buffer);
848 }
849
850 static __inline void
851 hammer_modify_record(hammer_transaction_t trans, hammer_buffer_t buffer,
852                      void *base, int len)
853 {
854         KKASSERT((char *)base >= (char *)buffer->ondisk &&
855                  (char *)base + len <= (char *)buffer->ondisk + HAMMER_BUFSIZE);
856         hammer_modify_buffer(trans, buffer, base, len);
857 }
858
859 static __inline void
860 hammer_modify_record_done(hammer_buffer_t buffer)
861 {
862         hammer_modify_buffer_done(buffer);
863 }
864