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