914e26988c0ccfd260cbef3ae73ebd762974ea4f
[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.59 2008/05/03 20:21: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/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 delete (backend) */
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_VHELD      0x0400  /* vnode held on sync */
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 /* inode delete request (frontend)*/
254 #define HAMMER_INODE_RESIGNAL   0x00040000 /* re-signal on re-flush */
255
256 #define HAMMER_INODE_MODMASK    (HAMMER_INODE_DDIRTY|HAMMER_INODE_RDIRTY| \
257                                  HAMMER_INODE_XDIRTY|HAMMER_INODE_BUFS|   \
258                                  HAMMER_INODE_ITIMES|HAMMER_INODE_TRUNCATED|\
259                                  HAMMER_INODE_DELETING)
260
261 #define HAMMER_INODE_MODMASK_NOXDIRTY \
262                                 (HAMMER_INODE_MODMASK & ~HAMMER_INODE_XDIRTY)
263
264 #define HAMMER_MAX_INODE_CURSORS        4
265
266 #define HAMMER_FLUSH_SIGNAL     0x0001
267 #define HAMMER_FLUSH_RECURSION  0x0002
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_GENERAL,      /* misc record */
288         HAMMER_MEM_RECORD_ADD,          /* positive memory cache record */
289         HAMMER_MEM_RECORD_DEL           /* negative delete-on-disk record */
290 } hammer_record_type_t;
291
292 struct hammer_record {
293         RB_ENTRY(hammer_record)         rb_node;
294         TAILQ_ENTRY(hammer_record)      target_entry;
295         hammer_inode_state_t            flush_state;
296         int                             flush_group;
297         hammer_record_type_t            type;
298         struct hammer_lock              lock;
299         struct hammer_inode             *ip;
300         struct hammer_inode             *target_ip;
301         union hammer_record_ondisk      rec;
302         union hammer_data_ondisk        *data;
303         int                             flags;
304 };
305
306 typedef struct hammer_record *hammer_record_t;
307
308 /*
309  * Record flags.  Note that FE can only be set by the frontend if the
310  * record has not been interlocked by the backend w/ BE.
311  */
312 #define HAMMER_RECF_ALLOCDATA           0x0001
313 #define HAMMER_RECF_ONRBTREE            0x0002
314 #define HAMMER_RECF_DELETED_FE          0x0004  /* deleted (frontend) */
315 #define HAMMER_RECF_DELETED_BE          0x0008  /* deleted (backend) */
316 #define HAMMER_RECF_INBAND              0x0010
317 #define HAMMER_RECF_INTERLOCK_BE        0x0020  /* backend interlock */
318 #define HAMMER_RECF_WANTED              0x0040
319 #define HAMMER_RECF_CONVERT_DELETE      0x0100 /* special case */
320
321 /*
322  * In-memory structures representing on-disk structures.
323  */
324 struct hammer_volume;
325 struct hammer_buffer;
326 struct hammer_node;
327 struct hammer_undo;
328 RB_HEAD(hammer_vol_rb_tree, hammer_volume);
329 RB_HEAD(hammer_buf_rb_tree, hammer_buffer);
330 RB_HEAD(hammer_nod_rb_tree, hammer_node);
331 RB_HEAD(hammer_und_rb_tree, hammer_undo);
332
333 RB_PROTOTYPE2(hammer_vol_rb_tree, hammer_volume, rb_node,
334               hammer_vol_rb_compare, int32_t);
335 RB_PROTOTYPE2(hammer_buf_rb_tree, hammer_buffer, rb_node,
336               hammer_buf_rb_compare, hammer_off_t);
337 RB_PROTOTYPE2(hammer_nod_rb_tree, hammer_node, rb_node,
338               hammer_nod_rb_compare, hammer_off_t);
339 RB_PROTOTYPE2(hammer_und_rb_tree, hammer_undo, rb_node,
340               hammer_und_rb_compare, hammer_off_t);
341
342 /*
343  * IO management - embedded at the head of various in-memory structures
344  *
345  * VOLUME       - hammer_volume containing meta-data
346  * META_BUFFER  - hammer_buffer containing meta-data
347  * DATA_BUFFER  - hammer_buffer containing pure-data
348  *
349  * Dirty volume headers and dirty meta-data buffers are locked until the
350  * flusher can sequence them out.  Dirty pure-data buffers can be written.
351  * Clean buffers can be passively released.
352  */
353 typedef enum hammer_io_type {
354         HAMMER_STRUCTURE_VOLUME,
355         HAMMER_STRUCTURE_META_BUFFER,
356         HAMMER_STRUCTURE_UNDO_BUFFER,
357         HAMMER_STRUCTURE_DATA_BUFFER
358 } hammer_io_type_t;
359
360 union hammer_io_structure;
361 struct hammer_io;
362
363 struct worklist {
364         LIST_ENTRY(worklist) node;
365 };
366
367 TAILQ_HEAD(hammer_io_list, hammer_io);
368 typedef struct hammer_io_list *hammer_io_list_t;
369
370 struct hammer_io {
371         struct worklist         worklist;
372         struct hammer_lock      lock;
373         enum hammer_io_type     type;
374         struct hammer_mount     *hmp;
375         TAILQ_ENTRY(hammer_io)  mod_entry; /* list entry if modified */
376         hammer_io_list_t        mod_list;
377         struct buf              *bp;
378         int64_t                 offset;
379         int                     loading;   /* loading/unloading interlock */
380         int                     modify_refs;
381
382         u_int           modified : 1;   /* bp's data was modified */
383         u_int           released : 1;   /* bp released (w/ B_LOCKED set) */
384         u_int           running : 1;    /* bp write IO in progress */
385         u_int           waiting : 1;    /* someone is waiting on us */
386         u_int           validated : 1;  /* ondisk has been validated */
387         u_int           flush : 1;      /* flush on last release */
388         u_int           waitdep : 1;    /* flush waits for dependancies */
389 };
390
391 typedef struct hammer_io *hammer_io_t;
392
393 /*
394  * In-memory volume representing on-disk buffer
395  */
396 struct hammer_volume {
397         struct hammer_io io;
398         RB_ENTRY(hammer_volume) rb_node;
399         struct hammer_buf_rb_tree rb_bufs_root;
400         struct hammer_volume_ondisk *ondisk;
401         int32_t vol_no;
402         int64_t nblocks;        /* note: special calculation for statfs */
403         int64_t buffer_base;    /* base offset of buffer 0 */
404         hammer_off_t maxbuf_off; /* Maximum buffer offset */
405         char    *vol_name;
406         struct vnode *devvp;
407         int     vol_flags;
408 };
409
410 typedef struct hammer_volume *hammer_volume_t;
411
412 /*
413  * In-memory buffer (other then volume, super-cluster, or cluster),
414  * representing an on-disk buffer.
415  */
416 struct hammer_buffer {
417         struct hammer_io io;
418         RB_ENTRY(hammer_buffer) rb_node;
419         void *ondisk;
420         struct hammer_volume *volume;
421         hammer_off_t zone2_offset;
422         hammer_off_t zoneX_offset;
423         struct hammer_node_list clist;
424 };
425
426 typedef struct hammer_buffer *hammer_buffer_t;
427
428 /*
429  * In-memory B-Tree node, representing an on-disk B-Tree node.
430  *
431  * This is a hang-on structure which is backed by a hammer_buffer,
432  * indexed by a hammer_cluster, and used for fine-grained locking of
433  * B-Tree nodes in order to properly control lock ordering.  A hammer_buffer
434  * can contain multiple nodes representing wildly disassociated portions
435  * of the B-Tree so locking cannot be done on a buffer-by-buffer basis.
436  *
437  * This structure uses a cluster-relative index to reduce the number
438  * of layers required to access it, and also because all on-disk B-Tree
439  * references are cluster-relative offsets.
440  */
441 struct hammer_node {
442         struct hammer_lock      lock;           /* node-by-node lock */
443         TAILQ_ENTRY(hammer_node) entry;         /* per-buffer linkage */
444         RB_ENTRY(hammer_node)   rb_node;        /* per-cluster linkage */
445         hammer_off_t            node_offset;    /* full offset spec */
446         struct hammer_mount     *hmp;
447         struct hammer_buffer    *buffer;        /* backing buffer */
448         hammer_node_ondisk_t    ondisk;         /* ptr to on-disk structure */
449         struct hammer_node      **cache1;       /* passive cache(s) */
450         struct hammer_node      **cache2;
451         int                     flags;
452         int                     loading;        /* load interlock */
453 };
454
455 #define HAMMER_NODE_DELETED     0x0001
456 #define HAMMER_NODE_FLUSH       0x0002
457
458 typedef struct hammer_node      *hammer_node_t;
459
460 /*
461  * List of locked nodes.
462  */
463 struct hammer_node_locklist {
464         struct hammer_node_locklist *next;
465         hammer_node_t   node;
466 };
467
468 typedef struct hammer_node_locklist *hammer_node_locklist_t;
469
470
471 /*
472  * Common I/O management structure - embedded in in-memory structures
473  * which are backed by filesystem buffers.
474  */
475 union hammer_io_structure {
476         struct hammer_io        io;
477         struct hammer_volume    volume;
478         struct hammer_buffer    buffer;
479 };
480
481 typedef union hammer_io_structure *hammer_io_structure_t;
482
483 /*
484  * Allocation holes are recorded for a short period of time in an attempt
485  * to use up the space.
486  */
487
488 #define HAMMER_MAX_HOLES        8
489
490 struct hammer_hole;
491
492 struct hammer_holes {
493         TAILQ_HEAD(, hammer_hole) list;
494         int     count;
495 };
496
497 typedef struct hammer_holes *hammer_holes_t;
498
499 struct hammer_hole {
500         TAILQ_ENTRY(hammer_hole) entry;
501         hammer_off_t    offset;
502         int             bytes;
503 };
504
505 typedef struct hammer_hole *hammer_hole_t;
506
507 #include "hammer_cursor.h"
508
509 /*
510  * Undo history tracking
511  */
512 #define HAMMER_MAX_UNDOS        256
513
514 struct hammer_undo {
515         RB_ENTRY(hammer_undo)   rb_node;
516         TAILQ_ENTRY(hammer_undo) lru_entry;
517         hammer_off_t            offset;
518         int                     bytes;
519 };
520
521 typedef struct hammer_undo *hammer_undo_t;
522
523 /*
524  * Internal hammer mount data structure
525  */
526 struct hammer_mount {
527         struct mount *mp;
528         /*struct vnode *rootvp;*/
529         struct hammer_ino_rb_tree rb_inos_root;
530         struct hammer_vol_rb_tree rb_vols_root;
531         struct hammer_nod_rb_tree rb_nods_root;
532         struct hammer_und_rb_tree rb_undo_root;
533         struct hammer_volume *rootvol;
534         struct hammer_base_elm root_btree_beg;
535         struct hammer_base_elm root_btree_end;
536         char    *zbuf;  /* HAMMER_BUFSIZE bytes worth of all-zeros */
537         int     hflags;
538         int     ronly;
539         int     nvolumes;
540         int     volume_iterator;
541         int     flusher_signal; /* flusher thread sequencer */
542         int     flusher_act;    /* currently active flush group */
543         int     flusher_done;   /* set to act when complete */
544         int     flusher_next;   /* next flush group */
545         int     flusher_lock;   /* lock sequencing of the next flush */
546         int     flusher_exiting;
547         int     reclaim_count;
548         thread_t flusher_td;
549         u_int   check_interrupt;
550         uuid_t  fsid;
551         udev_t  fsid_udev;
552         struct hammer_io_list volu_list;        /* dirty undo buffers */
553         struct hammer_io_list undo_list;        /* dirty undo buffers */
554         struct hammer_io_list data_list;        /* dirty data buffers */
555         struct hammer_io_list meta_list;        /* dirty meta bufs    */
556         struct hammer_io_list lose_list;        /* loose buffers      */
557         int     locked_dirty_count;             /* meta/volu count    */
558         int     io_running_count;
559         int     objid_cache_count;
560         hammer_tid_t asof;
561         hammer_off_t next_tid;
562         u_int32_t namekey_iterator;
563         hammer_off_t zone_limits[HAMMER_MAX_ZONES];
564         struct netexport export;
565         struct hammer_lock sync_lock;
566         struct lock blockmap_lock;
567         struct hammer_blockmap  blockmap[HAMMER_MAX_ZONES];
568         struct hammer_holes     holes[HAMMER_MAX_ZONES];
569         struct hammer_undo      undos[HAMMER_MAX_UNDOS];
570         int                     undo_alloc;
571         TAILQ_HEAD(, hammer_undo)  undo_lru_list;
572         TAILQ_HEAD(, hammer_inode) flush_list;
573         TAILQ_HEAD(, hammer_objid_cache) objid_cache_list;
574 };
575
576 typedef struct hammer_mount     *hammer_mount_t;
577
578 struct hammer_sync_info {
579         int error;
580         int waitfor;
581 };
582
583 #endif
584
585 #if defined(_KERNEL)
586
587 extern struct vop_ops hammer_vnode_vops;
588 extern struct vop_ops hammer_spec_vops;
589 extern struct vop_ops hammer_fifo_vops;
590 extern struct bio_ops hammer_bioops;
591
592 extern int hammer_debug_general;
593 extern int hammer_debug_inode;
594 extern int hammer_debug_locks;
595 extern int hammer_debug_btree;
596 extern int hammer_debug_tid;
597 extern int hammer_debug_recover;
598 extern int hammer_debug_recover_faults;
599 extern int hammer_count_inodes;
600 extern int hammer_count_records;
601 extern int hammer_count_record_datas;
602 extern int hammer_count_volumes;
603 extern int hammer_count_buffers;
604 extern int hammer_count_nodes;
605 extern int hammer_count_dirtybufs;
606 extern int hammer_limit_dirtybufs;
607 extern int hammer_bio_count;
608 extern int64_t hammer_contention_count;
609
610 int     hammer_vop_inactive(struct vop_inactive_args *);
611 int     hammer_vop_reclaim(struct vop_reclaim_args *);
612 int     hammer_get_vnode(struct hammer_inode *ip, struct vnode **vpp);
613 struct hammer_inode *hammer_get_inode(hammer_transaction_t trans,
614                         struct hammer_node **cache,
615                         u_int64_t obj_id, hammer_tid_t asof, int flags,
616                         int *errorp);
617 void    hammer_put_inode(struct hammer_inode *ip);
618 void    hammer_put_inode_ref(struct hammer_inode *ip);
619
620 int     hammer_unload_volume(hammer_volume_t volume, void *data __unused);
621 int     hammer_unload_buffer(hammer_buffer_t buffer, void *data __unused);
622 int     hammer_install_volume(hammer_mount_t hmp, const char *volname);
623
624 int     hammer_ip_lookup(hammer_cursor_t cursor, hammer_inode_t ip);
625 int     hammer_ip_first(hammer_cursor_t cursor);
626 int     hammer_ip_next(hammer_cursor_t cursor);
627 int     hammer_ip_resolve_record_and_data(hammer_cursor_t cursor);
628 int     hammer_ip_resolve_data(hammer_cursor_t cursor);
629 int     hammer_ip_delete_record(hammer_cursor_t cursor, hammer_tid_t tid);
630 int     hammer_delete_at_cursor(hammer_cursor_t cursor, int64_t *stat_bytes);
631 int     hammer_ip_check_directory_empty(hammer_transaction_t trans,
632                         hammer_cursor_t parent_cursor, hammer_inode_t ip);
633 int     hammer_sync_hmp(hammer_mount_t hmp, int waitfor);
634
635 hammer_record_t
636         hammer_alloc_mem_record(hammer_inode_t ip);
637 void    hammer_flush_record_done(hammer_record_t record, int error);
638 void    hammer_wait_mem_record(hammer_record_t record);
639 void    hammer_rel_mem_record(hammer_record_t record);
640
641 int     hammer_cursor_up(hammer_cursor_t cursor);
642 int     hammer_cursor_down(hammer_cursor_t cursor);
643 int     hammer_cursor_upgrade(hammer_cursor_t cursor);
644 void    hammer_cursor_downgrade(hammer_cursor_t cursor);
645 int     hammer_cursor_seek(hammer_cursor_t cursor, hammer_node_t node,
646                         int index);
647 void    hammer_lock_ex(struct hammer_lock *lock);
648 int     hammer_lock_ex_try(struct hammer_lock *lock);
649 void    hammer_lock_sh(struct hammer_lock *lock);
650 int     hammer_lock_upgrade(struct hammer_lock *lock);
651 void    hammer_lock_downgrade(struct hammer_lock *lock);
652 void    hammer_unlock(struct hammer_lock *lock);
653 void    hammer_ref(struct hammer_lock *lock);
654 void    hammer_unref(struct hammer_lock *lock);
655
656 u_int32_t hammer_to_unix_xid(uuid_t *uuid);
657 void hammer_guid_to_uuid(uuid_t *uuid, u_int32_t guid);
658 void    hammer_to_timespec(hammer_tid_t tid, struct timespec *ts);
659 hammer_tid_t hammer_timespec_to_transid(struct timespec *ts);
660 hammer_tid_t hammer_now_tid(void);
661 hammer_tid_t hammer_str_to_tid(const char *str);
662 hammer_tid_t hammer_alloc_objid(hammer_transaction_t trans, hammer_inode_t dip);
663 void hammer_clear_objid(hammer_inode_t dip);
664 void hammer_destroy_objid_cache(hammer_mount_t hmp);
665
666 int hammer_enter_undo_history(hammer_mount_t hmp, hammer_off_t offset,
667                               int bytes);
668 void hammer_clear_undo_history(hammer_mount_t hmp);
669 enum vtype hammer_get_vnode_type(u_int8_t obj_type);
670 int hammer_get_dtype(u_int8_t obj_type);
671 u_int8_t hammer_get_obj_type(enum vtype vtype);
672 int64_t hammer_directory_namekey(void *name, int len);
673
674 int     hammer_init_cursor(hammer_transaction_t trans, hammer_cursor_t cursor,
675                            struct hammer_node **cache, hammer_inode_t ip);
676 int     hammer_reinit_cursor(hammer_cursor_t cursor);
677 void    hammer_normalize_cursor(hammer_cursor_t cursor);
678 void    hammer_done_cursor(hammer_cursor_t cursor);
679 void    hammer_mem_done(hammer_cursor_t cursor);
680
681 int     hammer_btree_lookup(hammer_cursor_t cursor);
682 int     hammer_btree_first(hammer_cursor_t cursor);
683 int     hammer_btree_last(hammer_cursor_t cursor);
684 int     hammer_btree_extract(hammer_cursor_t cursor, int flags);
685 int     hammer_btree_iterate(hammer_cursor_t cursor);
686 int     hammer_btree_iterate_reverse(hammer_cursor_t cursor);
687 int     hammer_btree_insert(hammer_cursor_t cursor, hammer_btree_elm_t elm);
688 int     hammer_btree_delete(hammer_cursor_t cursor);
689 int     hammer_btree_cmp(hammer_base_elm_t key1, hammer_base_elm_t key2);
690 int     hammer_btree_chkts(hammer_tid_t ts, hammer_base_elm_t key);
691 int     hammer_btree_correct_rhb(hammer_cursor_t cursor, hammer_tid_t tid);
692 int     hammer_btree_correct_lhb(hammer_cursor_t cursor, hammer_tid_t tid);
693
694
695 int     hammer_btree_lock_children(hammer_cursor_t cursor,
696                         struct hammer_node_locklist **locklistp);
697
698 void    hammer_print_btree_node(hammer_node_ondisk_t ondisk);
699 void    hammer_print_btree_elm(hammer_btree_elm_t elm, u_int8_t type, int i);
700
701 void    *hammer_bread(struct hammer_mount *hmp, hammer_off_t off,
702                         int *errorp, struct hammer_buffer **bufferp);
703 void    *hammer_bnew(struct hammer_mount *hmp, hammer_off_t off,
704                         int *errorp, struct hammer_buffer **bufferp);
705
706 hammer_volume_t hammer_get_root_volume(hammer_mount_t hmp, int *errorp);
707 int     hammer_dowrite(hammer_cursor_t cursor, hammer_inode_t ip,
708                         struct bio *bio);
709
710 hammer_volume_t hammer_get_volume(hammer_mount_t hmp,
711                         int32_t vol_no, int *errorp);
712 hammer_buffer_t hammer_get_buffer(hammer_mount_t hmp,
713                         hammer_off_t buf_offset, int isnew, int *errorp);
714 void    hammer_uncache_buffer(struct hammer_mount *hmp, hammer_off_t off);
715
716 int             hammer_ref_volume(hammer_volume_t volume);
717 int             hammer_ref_buffer(hammer_buffer_t buffer);
718 void            hammer_flush_buffer_nodes(hammer_buffer_t buffer);
719
720 void            hammer_rel_volume(hammer_volume_t volume, int flush);
721 void            hammer_rel_buffer(hammer_buffer_t buffer, int flush);
722
723 int             hammer_vfs_export(struct mount *mp, int op,
724                         const struct export_args *export);
725 hammer_node_t   hammer_get_node(hammer_mount_t hmp,
726                         hammer_off_t node_offset, int *errorp);
727 void            hammer_ref_node(hammer_node_t node);
728 hammer_node_t   hammer_ref_node_safe(struct hammer_mount *hmp,
729                         struct hammer_node **cache, int *errorp);
730 void            hammer_rel_node(hammer_node_t node);
731 void            hammer_delete_node(hammer_transaction_t trans,
732                         hammer_node_t node);
733 void            hammer_cache_node(hammer_node_t node,
734                         struct hammer_node **cache);
735 void            hammer_uncache_node(struct hammer_node **cache);
736 void            hammer_flush_node(hammer_node_t node);
737
738 void hammer_dup_buffer(struct hammer_buffer **bufferp,
739                         struct hammer_buffer *buffer);
740 hammer_node_t hammer_alloc_btree(hammer_transaction_t trans, int *errorp);
741 void *hammer_alloc_record(hammer_transaction_t trans,
742                         hammer_off_t *rec_offp, u_int16_t rec_type,
743                         struct hammer_buffer **rec_bufferp,
744                         int32_t data_len, void **datap,
745                         struct hammer_buffer **data_bufferp, int *errorp);
746 void *hammer_alloc_data(hammer_transaction_t trans, int32_t data_len,
747                         hammer_off_t *data_offsetp,
748                         struct hammer_buffer **data_bufferp, int *errorp);
749
750 int hammer_generate_undo(hammer_transaction_t trans, hammer_io_t io,
751                         hammer_off_t zone1_offset, void *base, int len);
752
753 void hammer_put_volume(struct hammer_volume *volume, int flush);
754 void hammer_put_buffer(struct hammer_buffer *buffer, int flush);
755
756 hammer_off_t hammer_freemap_alloc(hammer_transaction_t trans,
757                         hammer_off_t owner, int *errorp);
758 void hammer_freemap_free(hammer_transaction_t trans, hammer_off_t phys_offset,
759                         hammer_off_t owner, int *errorp);
760 hammer_off_t hammer_blockmap_alloc(hammer_transaction_t trans, int zone,
761                         int bytes, int *errorp);
762 void hammer_blockmap_free(hammer_transaction_t trans,
763                         hammer_off_t bmap_off, int bytes);
764 int hammer_blockmap_getfree(hammer_mount_t hmp, hammer_off_t bmap_off,
765                         int *curp, int *errorp);
766 hammer_off_t hammer_blockmap_lookup(hammer_mount_t hmp, hammer_off_t bmap_off,
767                         int *errorp);
768 hammer_off_t hammer_undo_lookup(hammer_mount_t hmp, hammer_off_t bmap_off,
769                         int *errorp);
770 int64_t hammer_undo_space(hammer_mount_t hmp);
771 int64_t hammer_undo_max(hammer_mount_t hmp);
772
773
774 void hammer_start_transaction(struct hammer_transaction *trans,
775                               struct hammer_mount *hmp);
776 void hammer_simple_transaction(struct hammer_transaction *trans,
777                               struct hammer_mount *hmp);
778 void hammer_start_transaction_fls(struct hammer_transaction *trans,
779                                   struct hammer_mount *hmp);
780 void hammer_done_transaction(struct hammer_transaction *trans);
781
782 void hammer_modify_inode(struct hammer_transaction *trans,
783                         hammer_inode_t ip, int flags);
784 void hammer_flush_inode(hammer_inode_t ip, int flags);
785 void hammer_flush_inode_done(hammer_inode_t ip);
786 void hammer_wait_inode(hammer_inode_t ip);
787
788 int  hammer_create_inode(struct hammer_transaction *trans, struct vattr *vap,
789                         struct ucred *cred, struct hammer_inode *dip,
790                         struct hammer_inode **ipp);
791 void hammer_rel_inode(hammer_inode_t ip, int flush);
792 int hammer_sync_inode(hammer_inode_t ip);
793 void hammer_test_inode(hammer_inode_t ip);
794 void hammer_inode_unloadable_check(hammer_inode_t ip, int getvp);
795
796 int  hammer_ip_add_directory(struct hammer_transaction *trans,
797                         hammer_inode_t dip, struct namecache *ncp,
798                         hammer_inode_t nip);
799 int  hammer_ip_del_directory(struct hammer_transaction *trans,
800                         hammer_cursor_t cursor, hammer_inode_t dip,
801                         hammer_inode_t ip);
802 int  hammer_ip_add_record(struct hammer_transaction *trans,
803                         hammer_record_t record);
804 int  hammer_ip_delete_range(hammer_cursor_t cursor, hammer_inode_t ip,
805                         int64_t ran_beg, int64_t ran_end);
806 int  hammer_ip_delete_range_all(hammer_cursor_t cursor, hammer_inode_t ip,
807                         int *countp);
808 int  hammer_ip_sync_data(hammer_cursor_t cursor, hammer_inode_t ip,
809                         int64_t offset, void *data, int bytes);
810 int  hammer_ip_sync_record(hammer_transaction_t trans, hammer_record_t rec);
811 int  hammer_ip_sync_record_cursor(hammer_cursor_t cursor, hammer_record_t rec);
812
813 int hammer_ioctl(hammer_inode_t ip, u_long com, caddr_t data, int fflag,
814                         struct ucred *cred);
815
816 void hammer_io_init(hammer_io_t io, hammer_mount_t hmp,
817                         enum hammer_io_type type);
818 void hammer_io_reinit(hammer_io_t io, enum hammer_io_type type);
819 int hammer_io_read(struct vnode *devvp, struct hammer_io *io);
820 int hammer_io_new(struct vnode *devvp, struct hammer_io *io);
821 void hammer_io_release(struct hammer_io *io);
822 void hammer_io_flush(struct hammer_io *io);
823 int hammer_io_checkflush(hammer_io_t io);
824 void hammer_io_clear_modify(struct hammer_io *io);
825 void hammer_io_waitdep(struct hammer_io *io);
826
827 void hammer_modify_volume(hammer_transaction_t trans, hammer_volume_t volume,
828                         void *base, int len);
829 void hammer_modify_buffer(hammer_transaction_t trans, hammer_buffer_t buffer,
830                         void *base, int len);
831 void hammer_modify_volume_done(hammer_volume_t volume);
832 void hammer_modify_buffer_done(hammer_buffer_t buffer);
833
834 int hammer_ioc_reblock(hammer_transaction_t trans, hammer_inode_t ip,
835                         struct hammer_ioc_reblock *reblock);
836
837 void hammer_init_holes(hammer_mount_t hmp, hammer_holes_t holes);
838 void hammer_free_holes(hammer_mount_t hmp, hammer_holes_t holes);
839 int hammer_signal_check(hammer_mount_t hmp);
840
841 void hammer_flusher_create(hammer_mount_t hmp);
842 void hammer_flusher_destroy(hammer_mount_t hmp);
843 void hammer_flusher_sync(hammer_mount_t hmp);
844 void hammer_flusher_async(hammer_mount_t hmp);
845
846 int hammer_recover(hammer_mount_t hmp, hammer_volume_t rootvol);
847
848 #endif
849
850 static __inline void
851 hammer_modify_node_noundo(hammer_transaction_t trans, hammer_node_t node)
852 {
853         hammer_modify_buffer(trans, node->buffer, NULL, 0);
854 }
855
856 static __inline void
857 hammer_modify_node_all(hammer_transaction_t trans, struct hammer_node *node)
858 {
859         hammer_modify_buffer(trans, node->buffer,
860                              node->ondisk, sizeof(*node->ondisk));
861 }
862
863 static __inline void
864 hammer_modify_node(hammer_transaction_t trans, hammer_node_t node,
865                    void *base, int len)
866 {
867         KKASSERT((char *)base >= (char *)node->ondisk &&
868                  (char *)base + len <=
869                     (char *)node->ondisk + sizeof(*node->ondisk));
870         hammer_modify_buffer(trans, node->buffer, base, len);
871 }
872
873 static __inline void
874 hammer_modify_node_done(hammer_node_t node)
875 {
876         hammer_modify_buffer_done(node->buffer);
877 }
878
879 static __inline void
880 hammer_modify_record(hammer_transaction_t trans, hammer_buffer_t buffer,
881                      void *base, int len)
882 {
883         KKASSERT((char *)base >= (char *)buffer->ondisk &&
884                  (char *)base + len <= (char *)buffer->ondisk + HAMMER_BUFSIZE);
885         hammer_modify_buffer(trans, buffer, base, len);
886 }
887
888 static __inline void
889 hammer_modify_record_done(hammer_buffer_t buffer)
890 {
891         hammer_modify_buffer_done(buffer);
892 }
893
894 #define hammer_modify_volume_field(trans, vol, field)           \
895         hammer_modify_volume(trans, vol, &(vol)->ondisk->field, \
896                              sizeof((vol)->ondisk->field))
897
898