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