Merge branch 'master' of ssh://crater.dragonflybsd.org/repository/git/dragonfly
[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.130 2008/11/13 02:18:43 dillon Exp $
35  */
36 /*
37  * This header file contains structures used internally by the HAMMERFS
38  * implementation.  See hammer_disk.h for on-disk structures.
39  */
40
41 #include <sys/param.h>
42 #include <sys/types.h>
43 #include <sys/kernel.h>
44 #include <sys/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/priv.h>
53 #include <sys/stat.h>
54 #include <sys/globaldata.h>
55 #include <sys/lockf.h>
56 #include <sys/buf.h>
57 #include <sys/queue.h>
58 #include <sys/ktr.h>
59 #include <sys/globaldata.h>
60 #include <sys/limits.h>
61
62 #include <sys/buf2.h>
63 #include <sys/signal2.h>
64 #include <sys/mplock2.h>
65 #include "hammer_disk.h"
66 #include "hammer_mount.h"
67 #include "hammer_ioctl.h"
68
69 #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
70
71 MALLOC_DECLARE(M_HAMMER);
72
73 /*
74  * Kernel trace
75  */
76 #if !defined(KTR_HAMMER)
77 #define KTR_HAMMER      KTR_ALL
78 #endif
79 KTR_INFO_MASTER_EXTERN(hammer);
80
81 /*
82  * Misc structures
83  */
84 struct hammer_mount;
85
86 /*
87  * Key structure used for custom RB tree inode lookups.  This prototypes
88  * the function hammer_ino_rb_tree_RB_LOOKUP_INFO(root, info).
89  */
90 typedef struct hammer_inode_info {
91         int64_t         obj_id;         /* (key) object identifier */
92         hammer_tid_t    obj_asof;       /* (key) snapshot transid or 0 */
93         u_int32_t       obj_localization; /* (key) pseudo-fs */
94         union {
95                 struct hammer_btree_leaf_elm *leaf;
96         } u;
97 } *hammer_inode_info_t;
98
99 typedef enum hammer_transaction_type {
100         HAMMER_TRANS_RO,
101         HAMMER_TRANS_STD,
102         HAMMER_TRANS_FLS
103 } hammer_transaction_type_t;
104
105 /*
106  * HAMMER Transaction tracking
107  */
108 struct hammer_transaction {
109         hammer_transaction_type_t type;
110         struct hammer_mount *hmp;
111         hammer_tid_t    tid;
112         u_int64_t       time;
113         u_int32_t       time32;
114         int             sync_lock_refs;
115         int             flags;
116         struct hammer_volume *rootvol;
117 };
118
119 typedef struct hammer_transaction *hammer_transaction_t;
120
121 #define HAMMER_TRANSF_NEWINODE  0x0001
122 #define HAMMER_TRANSF_DIDIO     0x0002
123 #define HAMMER_TRANSF_CRCDOM    0x0004  /* EDOM on CRC error, less critical */
124
125 /*
126  * HAMMER locks
127  */
128 struct hammer_lock {
129         int             refs;           /* active references delay writes */
130         volatile u_int  lockval;        /* lock count and control bits */
131         struct thread   *owner;         /* owner if exclusively held */
132 };
133
134 #define HAMMER_LOCKF_EXCLUSIVE  0x40000000
135 #define HAMMER_LOCKF_WANTED     0x80000000
136
137 static __inline int
138 hammer_notlocked(struct hammer_lock *lock)
139 {
140         return(lock->lockval == 0);
141 }
142
143 static __inline int
144 hammer_islocked(struct hammer_lock *lock)
145 {
146         return(lock->lockval != 0);
147 }
148
149 static __inline int
150 hammer_isactive(struct hammer_lock *lock)
151 {
152         return(lock->refs != 0);
153 }
154
155 static __inline int
156 hammer_islastref(struct hammer_lock *lock)
157 {
158         return(lock->refs == 1);
159 }
160
161 /*
162  * Return if we specifically own the lock exclusively.
163  */
164 static __inline int
165 hammer_lock_excl_owned(struct hammer_lock *lock, thread_t td)
166 {
167         if ((lock->lockval & HAMMER_LOCKF_EXCLUSIVE) &&
168             lock->owner == td) {
169                 return(1);
170         }
171         return(0);
172 }
173
174 /*
175  * Flush state, used by various structures
176  */
177 typedef enum hammer_inode_state {
178         HAMMER_FST_IDLE,
179         HAMMER_FST_SETUP,
180         HAMMER_FST_FLUSH
181 } hammer_inode_state_t;
182
183 TAILQ_HEAD(hammer_record_list, hammer_record);
184
185 /*
186  * Pseudo-filesystem extended data tracking
187  */
188 struct hammer_pfs_rb_tree;
189 struct hammer_pseudofs_inmem;
190 RB_HEAD(hammer_pfs_rb_tree, hammer_pseudofs_inmem);
191 RB_PROTOTYPE2(hammer_pfs_rb_tree, hammer_pseudofs_inmem, rb_node,
192               hammer_pfs_rb_compare, u_int32_t);
193
194 struct hammer_pseudofs_inmem {
195         RB_ENTRY(hammer_pseudofs_inmem) rb_node;
196         struct hammer_lock      lock;
197         u_int32_t               localization;
198         hammer_tid_t            create_tid;
199         int                     flags;
200         udev_t                  fsid_udev;
201         struct hammer_pseudofs_data pfsd;
202 };
203
204 typedef struct hammer_pseudofs_inmem *hammer_pseudofs_inmem_t;
205
206 #define HAMMER_PFSM_DELETED     0x0001
207
208 /*
209  * Cache object ids.  A fixed number of objid cache structures are
210  * created to reserve object id's for newly created files in multiples
211  * of 100,000, localized to a particular directory, and recycled as
212  * needed.  This allows parallel create operations in different
213  * directories to retain fairly localized object ids which in turn
214  * improves reblocking performance and layout.
215  */
216 #define OBJID_CACHE_SIZE        1024
217 #define OBJID_CACHE_BULK_BITS   10              /* 10 bits (1024)       */
218 #define OBJID_CACHE_BULK        (32 * 32)       /* two level (1024)     */
219 #define OBJID_CACHE_BULK_MASK   (OBJID_CACHE_BULK - 1)
220 #define OBJID_CACHE_BULK_MASK64 ((u_int64_t)(OBJID_CACHE_BULK - 1))
221
222 typedef struct hammer_objid_cache {
223         TAILQ_ENTRY(hammer_objid_cache) entry;
224         struct hammer_inode             *dip;
225         hammer_tid_t                    base_tid;
226         int                             count;
227         u_int32_t                       bm0;
228         u_int32_t                       bm1[32];
229 } *hammer_objid_cache_t;
230
231 /*
232  * Associate an inode with a B-Tree node to cache search start positions
233  */
234 typedef struct hammer_node_cache {
235         TAILQ_ENTRY(hammer_node_cache)  entry;
236         struct hammer_node              *node;
237         struct hammer_inode             *ip;
238 } *hammer_node_cache_t;
239
240 TAILQ_HEAD(hammer_node_cache_list, hammer_node_cache);
241
242 /*
243  * Structure used to organize flush groups.  Flush groups must be
244  * organized into chunks in order to avoid blowing out the UNDO FIFO.
245  * Without this a 'sync' could end up flushing 50,000 inodes in a single
246  * transaction.
247  */
248 struct hammer_fls_rb_tree;
249 RB_HEAD(hammer_fls_rb_tree, hammer_inode);
250 RB_PROTOTYPE(hammer_fls_rb_tree, hammer_inode, rb_flsnode,
251               hammer_ino_rb_compare);
252
253 struct hammer_flush_group {
254         TAILQ_ENTRY(hammer_flush_group) flush_entry;
255         struct hammer_fls_rb_tree       flush_tree;
256         int                             unused01;
257         int                             total_count;    /* record load */
258         int                             running;        /* group is running */
259         int                             closed;
260         int                             refs;
261 };
262
263 typedef struct hammer_flush_group *hammer_flush_group_t;
264
265 TAILQ_HEAD(hammer_flush_group_list, hammer_flush_group);
266
267 /*
268  * Structure used to represent an inode in-memory.
269  *
270  * The record and data associated with an inode may be out of sync with
271  * the disk (xDIRTY flags), or not even on the disk at all (ONDISK flag
272  * clear).
273  *
274  * An inode may also hold a cache of unsynchronized records, used for
275  * database and directories only.  Unsynchronized regular file data is
276  * stored in the buffer cache.
277  *
278  * NOTE: A file which is created and destroyed within the initial
279  * synchronization period can wind up not doing any disk I/O at all.
280  *
281  * Finally, an inode may cache numerous disk-referencing B-Tree cursors.
282  */
283 struct hammer_ino_rb_tree;
284 struct hammer_inode;
285 RB_HEAD(hammer_ino_rb_tree, hammer_inode);
286 RB_PROTOTYPEX(hammer_ino_rb_tree, INFO, hammer_inode, rb_node,
287               hammer_ino_rb_compare, hammer_inode_info_t);
288
289 struct hammer_rec_rb_tree;
290 struct hammer_record;
291 RB_HEAD(hammer_rec_rb_tree, hammer_record);
292 RB_PROTOTYPEX(hammer_rec_rb_tree, INFO, hammer_record, rb_node,
293               hammer_rec_rb_compare, hammer_btree_leaf_elm_t);
294
295 TAILQ_HEAD(hammer_node_list, hammer_node);
296
297 struct hammer_inode {
298         RB_ENTRY(hammer_inode)  rb_node;
299         hammer_inode_state_t    flush_state;
300         hammer_flush_group_t    flush_group;
301         RB_ENTRY(hammer_inode)  rb_flsnode;     /* when on flush list */
302         struct hammer_record_list target_list;  /* target of dependant recs */
303         int64_t                 obj_id;         /* (key) object identifier */
304         hammer_tid_t            obj_asof;       /* (key) snapshot or 0 */
305         u_int32_t               obj_localization; /* (key) pseudo-fs */
306         struct hammer_mount     *hmp;
307         hammer_objid_cache_t    objid_cache;
308         int                     flags;
309         int                     error;          /* flush error */
310         int                     cursor_ip_refs; /* sanity */
311         int                     rsv_recs;
312         struct vnode            *vp;
313         hammer_pseudofs_inmem_t pfsm;
314         struct lockf            advlock;
315         struct hammer_lock      lock;           /* sync copy interlock */
316         off_t                   trunc_off;
317         struct hammer_btree_leaf_elm ino_leaf;  /* in-memory cache */
318         struct hammer_inode_data ino_data;      /* in-memory cache */
319         struct hammer_rec_rb_tree rec_tree;     /* in-memory cache */
320         int                     rec_generation;
321         struct hammer_node_cache cache[4];      /* search initiate cache */
322
323         /*
324          * When a demark is created to synchronize an inode to
325          * disk, certain fields are copied so the front-end VOPs
326          * can continue to run in parallel with the synchronization
327          * occuring in the background.
328          */
329         int             sync_flags;             /* to-sync flags cache */
330         off_t           sync_trunc_off;         /* to-sync truncation */
331         off_t           save_trunc_off;         /* write optimization */
332         struct hammer_btree_leaf_elm sync_ino_leaf; /* to-sync cache */
333         struct hammer_inode_data sync_ino_data; /* to-sync cache */
334         size_t          redo_count;
335 };
336
337 typedef struct hammer_inode *hammer_inode_t;
338
339 #define VTOI(vp)        ((struct hammer_inode *)(vp)->v_data)
340
341 /*
342  * NOTE: DDIRTY does not include atime or mtime and does not include
343  *       write-append size changes.  SDIRTY handles write-append size
344  *       changes.
345  */
346                                         /* (not including atime/mtime) */
347 #define HAMMER_INODE_DDIRTY     0x0001  /* in-memory ino_data is dirty */
348 #define HAMMER_INODE_RSV_INODES 0x0002  /* hmp->rsv_inodes bumped */
349 #define HAMMER_INODE_CONN_DOWN  0x0004  /* include in downward recursion */
350 #define HAMMER_INODE_XDIRTY     0x0008  /* in-memory records */
351 #define HAMMER_INODE_ONDISK     0x0010  /* inode is on-disk (else not yet) */
352 #define HAMMER_INODE_FLUSH      0x0020  /* flush on last ref */
353 #define HAMMER_INODE_DELETED    0x0080  /* inode delete (backend) */
354 #define HAMMER_INODE_DELONDISK  0x0100  /* delete synchronized to disk */
355 #define HAMMER_INODE_RO         0x0200  /* read-only (because of as-of) */
356 #define HAMMER_INODE_VHELD      0x0400  /* vnode held on sync */
357 #define HAMMER_INODE_DONDISK    0x0800  /* data records may be on disk */
358 #define HAMMER_INODE_BUFS       0x1000  /* dirty high level bps present */
359 #define HAMMER_INODE_REFLUSH    0x2000  /* flush on dependancy / reflush */
360 #define HAMMER_INODE_RECLAIM    0x4000  /* trying to reclaim */
361 #define HAMMER_INODE_FLUSHW     0x8000  /* Someone waiting for flush */
362
363 #define HAMMER_INODE_TRUNCATED  0x00010000
364 #define HAMMER_INODE_DELETING   0x00020000 /* inode delete request (frontend)*/
365 #define HAMMER_INODE_RESIGNAL   0x00040000 /* re-signal on re-flush */
366 #define HAMMER_INODE_ATIME      0x00100000 /* in-memory atime modified */
367 #define HAMMER_INODE_MTIME      0x00200000 /* in-memory mtime modified */
368 #define HAMMER_INODE_WOULDBLOCK 0x00400000 /* re-issue to new flush group */
369 #define HAMMER_INODE_DUMMY      0x00800000 /* dummy inode covering bad file */
370 #define HAMMER_INODE_SDIRTY     0x01000000 /* in-memory ino_data.size is dirty*/
371
372 #define HAMMER_INODE_MODMASK    (HAMMER_INODE_DDIRTY|HAMMER_INODE_SDIRTY|   \
373                                  HAMMER_INODE_XDIRTY|HAMMER_INODE_BUFS|     \
374                                  HAMMER_INODE_ATIME|HAMMER_INODE_MTIME|     \
375                                  HAMMER_INODE_TRUNCATED|HAMMER_INODE_DELETING)
376
377 #define HAMMER_INODE_MODMASK_NOXDIRTY   \
378                                 (HAMMER_INODE_MODMASK & ~HAMMER_INODE_XDIRTY)
379
380 #define HAMMER_INODE_MODMASK_NOREDO     \
381                                 (HAMMER_INODE_DDIRTY|                       \
382                                  HAMMER_INODE_XDIRTY|                       \
383                                  HAMMER_INODE_TRUNCATED|HAMMER_INODE_DELETING)
384
385 #define HAMMER_FLUSH_SIGNAL     0x0001
386 #define HAMMER_FLUSH_RECURSION  0x0002
387
388 /*
389  * Used by the inode reclaim code to pipeline reclaims and avoid
390  * blowing out kernel memory or letting the flusher get too far
391  * behind.  The reclaim wakes up when count reaches 0 or the
392  * timer expires.
393  */
394 struct hammer_reclaim {
395         TAILQ_ENTRY(hammer_reclaim) entry;
396         int     count;
397 };
398
399 #define HAMMER_RECLAIM_WAIT     4000    /* default vfs.hammer.limit_reclaim */
400
401 /*
402  * Structure used to represent an unsynchronized record in-memory.  These
403  * records typically represent directory entries.  Only non-historical
404  * records are kept in-memory.
405  *
406  * Records are organized as a per-inode RB-Tree.  If the inode is not
407  * on disk then neither are any records and the in-memory record tree
408  * represents the entire contents of the inode.  If the inode is on disk
409  * then the on-disk B-Tree is scanned in parallel with the in-memory
410  * RB-Tree to synthesize the current state of the file.
411  *
412  * Records are also used to enforce the ordering of directory create/delete
413  * operations.  A new inode will not be flushed to disk unless its related
414  * directory entry is also being flushed at the same time.  A directory entry
415  * will not be removed unless its related inode is also being removed at the
416  * same time.
417  */
418 typedef enum hammer_record_type {
419         HAMMER_MEM_RECORD_GENERAL,      /* misc record */
420         HAMMER_MEM_RECORD_INODE,        /* inode record */
421         HAMMER_MEM_RECORD_ADD,          /* positive memory cache record */
422         HAMMER_MEM_RECORD_DEL,          /* negative delete-on-disk record */
423         HAMMER_MEM_RECORD_DATA          /* bulk-data record w/on-disk ref */
424 } hammer_record_type_t;
425
426 struct hammer_record {
427         RB_ENTRY(hammer_record)         rb_node;
428         TAILQ_ENTRY(hammer_record)      target_entry;
429         hammer_inode_state_t            flush_state;
430         hammer_flush_group_t            flush_group;
431         hammer_record_type_t            type;
432         struct hammer_lock              lock;
433         struct hammer_reserve           *resv;
434         struct hammer_inode             *ip;
435         struct hammer_inode             *target_ip;
436         struct hammer_btree_leaf_elm    leaf;
437         union hammer_data_ondisk        *data;
438         int                             flags;
439         hammer_off_t                    zone2_offset;   /* direct-write only */
440 };
441
442 typedef struct hammer_record *hammer_record_t;
443
444 /*
445  * Record flags.  Note that FE can only be set by the frontend if the
446  * record has not been interlocked by the backend w/ BE.
447  */
448 #define HAMMER_RECF_ALLOCDATA           0x0001
449 #define HAMMER_RECF_ONRBTREE            0x0002
450 #define HAMMER_RECF_DELETED_FE          0x0004  /* deleted (frontend) */
451 #define HAMMER_RECF_DELETED_BE          0x0008  /* deleted (backend) */
452 #define HAMMER_RECF_COMMITTED           0x0010  /* committed to the B-Tree */
453 #define HAMMER_RECF_INTERLOCK_BE        0x0020  /* backend interlock */
454 #define HAMMER_RECF_WANTED              0x0040  /* wanted by the frontend */
455 #define HAMMER_RECF_CONVERT_DELETE      0x0100  /* special case */
456 #define HAMMER_RECF_DIRECT_IO           0x0200  /* related direct I/O running*/
457 #define HAMMER_RECF_DIRECT_WAIT         0x0400  /* related direct I/O running*/
458 #define HAMMER_RECF_DIRECT_INVAL        0x0800  /* buffer alias invalidation */
459
460 /*
461  * hammer_create_at_cursor() and hammer_delete_at_cursor() flags.
462  */
463 #define HAMMER_CREATE_MODE_UMIRROR      0x0001
464 #define HAMMER_CREATE_MODE_SYS          0x0002
465
466 #define HAMMER_DELETE_ADJUST            0x0001
467 #define HAMMER_DELETE_DESTROY           0x0002
468
469 /*
470  * In-memory structures representing on-disk structures.
471  */
472 struct hammer_volume;
473 struct hammer_buffer;
474 struct hammer_node;
475 struct hammer_undo;
476 struct hammer_reserve;
477
478 RB_HEAD(hammer_vol_rb_tree, hammer_volume);
479 RB_HEAD(hammer_buf_rb_tree, hammer_buffer);
480 RB_HEAD(hammer_nod_rb_tree, hammer_node);
481 RB_HEAD(hammer_und_rb_tree, hammer_undo);
482 RB_HEAD(hammer_res_rb_tree, hammer_reserve);
483
484 RB_PROTOTYPE2(hammer_vol_rb_tree, hammer_volume, rb_node,
485               hammer_vol_rb_compare, int32_t);
486 RB_PROTOTYPE2(hammer_buf_rb_tree, hammer_buffer, rb_node,
487               hammer_buf_rb_compare, hammer_off_t);
488 RB_PROTOTYPE2(hammer_nod_rb_tree, hammer_node, rb_node,
489               hammer_nod_rb_compare, hammer_off_t);
490 RB_PROTOTYPE2(hammer_und_rb_tree, hammer_undo, rb_node,
491               hammer_und_rb_compare, hammer_off_t);
492 RB_PROTOTYPE2(hammer_res_rb_tree, hammer_reserve, rb_node,
493               hammer_res_rb_compare, hammer_off_t);
494
495 /*
496  * IO management - embedded at the head of various in-memory structures
497  *
498  * VOLUME       - hammer_volume containing meta-data
499  * META_BUFFER  - hammer_buffer containing meta-data
500  * DATA_BUFFER  - hammer_buffer containing pure-data
501  *
502  * Dirty volume headers and dirty meta-data buffers are locked until the
503  * flusher can sequence them out.  Dirty pure-data buffers can be written.
504  * Clean buffers can be passively released.
505  */
506 typedef enum hammer_io_type {
507         HAMMER_STRUCTURE_VOLUME,
508         HAMMER_STRUCTURE_META_BUFFER,
509         HAMMER_STRUCTURE_UNDO_BUFFER,
510         HAMMER_STRUCTURE_DATA_BUFFER
511 } hammer_io_type_t;
512
513 union hammer_io_structure;
514 struct hammer_io;
515
516 struct worklist {
517         LIST_ENTRY(worklist) node;
518 };
519
520 TAILQ_HEAD(hammer_io_list, hammer_io);
521 typedef struct hammer_io_list *hammer_io_list_t;
522
523 struct hammer_io {
524         struct worklist         worklist;
525         struct hammer_lock      lock;
526         enum hammer_io_type     type;
527         struct hammer_mount     *hmp;
528         struct hammer_volume    *volume;
529         TAILQ_ENTRY(hammer_io)  mod_entry; /* list entry if modified */
530         hammer_io_list_t        mod_list;
531         struct buf              *bp;
532         int64_t                 offset;    /* zone-2 offset */
533         int                     bytes;     /* buffer cache buffer size */
534         int                     loading;   /* loading/unloading interlock */
535         int                     modify_refs;
536
537         u_int           modified : 1;   /* bp's data was modified */
538         u_int           released : 1;   /* bp released (w/ B_LOCKED set) */
539         u_int           running : 1;    /* bp write IO in progress */
540         u_int           waiting : 1;    /* someone is waiting on us */
541         u_int           validated : 1;  /* ondisk has been validated */
542         u_int           waitdep : 1;    /* flush waits for dependancies */
543         u_int           recovered : 1;  /* has recovery ref */
544         u_int           waitmod : 1;    /* waiting for modify_refs */
545         u_int           reclaim : 1;    /* reclaim requested */
546         u_int           gencrc : 1;     /* crc needs to be generated */
547         u_int           ioerror : 1;    /* abort on io-error */
548 };
549
550 typedef struct hammer_io *hammer_io_t;
551
552 #define HAMMER_CLUSTER_SIZE     (64 * 1024)
553 #if HAMMER_CLUSTER_SIZE > MAXBSIZE
554 #undef  HAMMER_CLUSTER_SIZE
555 #define HAMMER_CLUSTER_SIZE     MAXBSIZE
556 #endif
557 #define HAMMER_CLUSTER_BUFS     (HAMMER_CLUSTER_SIZE / HAMMER_BUFSIZE)
558
559 /*
560  * In-memory volume representing on-disk buffer
561  */
562 struct hammer_volume {
563         struct hammer_io io;
564         RB_ENTRY(hammer_volume) rb_node;
565         struct hammer_volume_ondisk *ondisk;
566         int32_t vol_no;
567         int64_t nblocks;        /* note: special calculation for statfs */
568         int64_t buffer_base;    /* base offset of buffer 0 */
569         hammer_off_t maxbuf_off; /* Maximum buffer offset (zone-2) */
570         hammer_off_t maxraw_off; /* Maximum raw offset for device */
571         char    *vol_name;
572         struct vnode *devvp;
573         int     vol_flags;
574 };
575
576 typedef struct hammer_volume *hammer_volume_t;
577
578 /*
579  * In-memory buffer (other then volume, super-cluster, or cluster),
580  * representing an on-disk buffer.
581  */
582 struct hammer_buffer {
583         struct hammer_io io;
584         RB_ENTRY(hammer_buffer) rb_node;
585         void *ondisk;
586         hammer_off_t zoneX_offset;
587         hammer_off_t zone2_offset;
588         struct hammer_reserve *resv;
589         struct hammer_node_list clist;
590 };
591
592 typedef struct hammer_buffer *hammer_buffer_t;
593
594 /*
595  * In-memory B-Tree node, representing an on-disk B-Tree node.
596  *
597  * This is a hang-on structure which is backed by a hammer_buffer,
598  * indexed by a hammer_cluster, and used for fine-grained locking of
599  * B-Tree nodes in order to properly control lock ordering.  A hammer_buffer
600  * can contain multiple nodes representing wildly disassociated portions
601  * of the B-Tree so locking cannot be done on a buffer-by-buffer basis.
602  *
603  * This structure uses a cluster-relative index to reduce the number
604  * of layers required to access it, and also because all on-disk B-Tree
605  * references are cluster-relative offsets.
606  */
607 struct hammer_node {
608         struct hammer_lock      lock;           /* node-by-node lock */
609         TAILQ_ENTRY(hammer_node) entry;         /* per-buffer linkage */
610         RB_ENTRY(hammer_node)   rb_node;        /* per-cluster linkage */
611         hammer_off_t            node_offset;    /* full offset spec */
612         struct hammer_mount     *hmp;
613         struct hammer_buffer    *buffer;        /* backing buffer */
614         hammer_node_ondisk_t    ondisk;         /* ptr to on-disk structure */
615         TAILQ_HEAD(, hammer_cursor) cursor_list;  /* deadlock recovery */
616         struct hammer_node_cache_list cache_list; /* passive caches */
617         int                     flags;
618         int                     loading;        /* load interlock */
619 };
620
621 #define HAMMER_NODE_DELETED     0x0001
622 #define HAMMER_NODE_FLUSH       0x0002
623 #define HAMMER_NODE_CRCGOOD     0x0004
624 #define HAMMER_NODE_NEEDSCRC    0x0008
625 #define HAMMER_NODE_NEEDSMIRROR 0x0010
626 #define HAMMER_NODE_CRCBAD      0x0020
627
628 #define HAMMER_NODE_CRCANY      (HAMMER_NODE_CRCGOOD | HAMMER_NODE_CRCBAD)
629
630 typedef struct hammer_node      *hammer_node_t;
631
632 /*
633  * List of locked nodes.  This structure is used to lock potentially large
634  * numbers of nodes as an aid for complex B-Tree operations.
635  */
636 struct hammer_node_lock;
637 TAILQ_HEAD(hammer_node_lock_list, hammer_node_lock);
638
639 struct hammer_node_lock {
640         TAILQ_ENTRY(hammer_node_lock) entry;
641         struct hammer_node_lock_list  list;
642         struct hammer_node_lock       *parent;
643         hammer_node_t   node;
644         hammer_node_ondisk_t copy;      /* copy of on-disk data */
645         int             index;          /* index of this node in parent */
646         int             count;          /* count children */
647         int             flags;
648 };
649
650 typedef struct hammer_node_lock *hammer_node_lock_t;
651
652 #define HAMMER_NODE_LOCK_UPDATED        0x0001
653
654 /*
655  * Common I/O management structure - embedded in in-memory structures
656  * which are backed by filesystem buffers.
657  */
658 union hammer_io_structure {
659         struct hammer_io        io;
660         struct hammer_volume    volume;
661         struct hammer_buffer    buffer;
662 };
663
664 typedef union hammer_io_structure *hammer_io_structure_t;
665
666 /*
667  * The reserve structure prevents the blockmap from allocating
668  * out of a reserved bigblock.  Such reservations are used by
669  * the direct-write mechanism.
670  *
671  * The structure is also used to hold off on reallocations of
672  * big blocks from the freemap until flush dependancies have
673  * been dealt with.
674  */
675 struct hammer_reserve {
676         RB_ENTRY(hammer_reserve) rb_node;
677         TAILQ_ENTRY(hammer_reserve) delay_entry;
678         int             flush_group;
679         int             flags;
680         int             refs;
681         int             zone;
682         int             append_off;
683         hammer_off_t    zone_offset;
684 };
685
686 typedef struct hammer_reserve *hammer_reserve_t;
687
688 #define HAMMER_RESF_ONDELAY     0x0001
689 #define HAMMER_RESF_LAYER2FREE  0x0002
690
691 #include "hammer_cursor.h"
692
693 /*
694  * The undo structure tracks recent undos to avoid laying down duplicate
695  * undos within a flush group, saving us a significant amount of overhead.
696  *
697  * This is strictly a heuristic.
698  */
699 #define HAMMER_MAX_UNDOS                1024
700 #define HAMMER_MAX_FLUSHERS             4
701
702 struct hammer_undo {
703         RB_ENTRY(hammer_undo)   rb_node;
704         TAILQ_ENTRY(hammer_undo) lru_entry;
705         hammer_off_t            offset;
706         int                     bytes;
707 };
708
709 typedef struct hammer_undo *hammer_undo_t;
710
711 struct hammer_flusher_info;
712 TAILQ_HEAD(hammer_flusher_info_list, hammer_flusher_info);
713
714 struct hammer_flusher {
715         int             signal;         /* flusher thread sequencer */
716         int             act;            /* currently active flush group */
717         int             done;           /* set to act when complete */
718         int             next;           /* next flush group */
719         int             group_lock;     /* lock sequencing of the next flush */
720         int             exiting;        /* request master exit */
721         thread_t        td;             /* master flusher thread */
722         hammer_tid_t    tid;            /* last flushed transaction id */
723         int             finalize_want;          /* serialize finalization */
724         struct hammer_lock finalize_lock;       /* serialize finalization */
725         struct hammer_transaction trans;        /* shared transaction */
726         struct hammer_flusher_info_list run_list;
727         struct hammer_flusher_info_list ready_list;
728 };
729
730 #define HAMMER_FLUSH_UNDOS_RELAXED      0
731 #define HAMMER_FLUSH_UNDOS_FORCED       1
732 #define HAMMER_FLUSH_UNDOS_AUTO         2
733 /*
734  * Internal hammer mount data structure
735  */
736 struct hammer_mount {
737         struct mount *mp;
738         /*struct vnode *rootvp;*/
739         struct hammer_ino_rb_tree rb_inos_root;
740         struct hammer_vol_rb_tree rb_vols_root;
741         struct hammer_nod_rb_tree rb_nods_root;
742         struct hammer_und_rb_tree rb_undo_root;
743         struct hammer_res_rb_tree rb_resv_root;
744         struct hammer_buf_rb_tree rb_bufs_root;
745         struct hammer_pfs_rb_tree rb_pfsm_root;
746         struct hammer_volume *rootvol;
747         struct hammer_base_elm root_btree_beg;
748         struct hammer_base_elm root_btree_end;
749
750         struct malloc_type      *m_misc;
751         struct malloc_type      *m_inodes;
752
753         int     flags;          /* HAMMER_MOUNT_xxx flags */
754         int     hflags;
755         int     ronly;
756         int     nvolumes;
757         int     volume_iterator;
758         int     master_id;      /* -1 or 0-15 - clustering and mirroring */
759         int     version;        /* hammer filesystem version to use */
760         int     rsv_inodes;     /* reserved space due to dirty inodes */
761         int64_t rsv_databytes;  /* reserved space due to record data */
762         int     rsv_recs;       /* reserved space due to dirty records */
763         int     rsv_fromdelay;  /* bigblocks reserved due to flush delay */
764         int     undo_rec_limit; /* based on size of undo area */
765         int     last_newrecords;
766         int     count_newrecords;
767
768         int     volume_to_remove; /* volume that is currently being removed */
769
770         int     inode_reclaims; /* inodes pending reclaim by flusher */
771         int     count_inodes;   /* total number of inodes */
772         int     count_iqueued;  /* inodes queued to flusher */
773
774         struct hammer_flusher flusher;
775
776         u_int   check_interrupt;
777         u_int   check_yield;
778         uuid_t  fsid;
779         struct hammer_io_list volu_list;        /* dirty undo buffers */
780         struct hammer_io_list undo_list;        /* dirty undo buffers */
781         struct hammer_io_list data_list;        /* dirty data buffers */
782         struct hammer_io_list alt_data_list;    /* dirty data buffers */
783         struct hammer_io_list meta_list;        /* dirty meta bufs    */
784         struct hammer_io_list lose_list;        /* loose buffers      */
785         int     locked_dirty_space;             /* meta/volu count    */
786         int     io_running_space;
787         int     objid_cache_count;
788         int     error;                          /* critical I/O error */
789         struct krate    krate;                  /* rate limited kprintf */
790         hammer_tid_t    asof;                   /* snapshot mount */
791         hammer_tid_t    next_tid;
792         hammer_tid_t    flush_tid1;             /* flusher tid sequencing */
793         hammer_tid_t    flush_tid2;             /* flusher tid sequencing */
794         int64_t copy_stat_freebigblocks;        /* number of free bigblocks */
795         u_int32_t       undo_seqno;             /* UNDO/REDO FIFO seqno */
796
797         struct netexport export;
798         struct hammer_lock sync_lock;
799         struct hammer_lock free_lock;
800         struct hammer_lock undo_lock;
801         struct hammer_lock blkmap_lock;
802         struct hammer_lock snapshot_lock;
803         struct hammer_lock volume_lock;
804         struct hammer_blockmap  blockmap[HAMMER_MAX_ZONES];
805         struct hammer_undo      undos[HAMMER_MAX_UNDOS];
806         int                     undo_alloc;
807         TAILQ_HEAD(, hammer_undo)  undo_lru_list;
808         TAILQ_HEAD(, hammer_reserve) delay_list;
809         struct hammer_flush_group_list  flush_group_list;
810         hammer_flush_group_t    next_flush_group;
811         TAILQ_HEAD(, hammer_objid_cache) objid_cache_list;
812         TAILQ_HEAD(, hammer_reclaim) reclaim_list;
813 };
814
815 typedef struct hammer_mount     *hammer_mount_t;
816
817 #define HAMMER_MOUNT_CRITICAL_ERROR     0x0001
818 #define HAMMER_MOUNT_FLUSH_RECOVERY     0x0002
819
820 struct hammer_sync_info {
821         int error;
822         int waitfor;
823 };
824
825 #endif
826
827 /*
828  * checkspace slop (8MB chunks), higher numbers are more conservative.
829  */
830 #define HAMMER_CHKSPC_REBLOCK   25
831 #define HAMMER_CHKSPC_MIRROR    20
832 #define HAMMER_CHKSPC_WRITE     20
833 #define HAMMER_CHKSPC_CREATE    20
834 #define HAMMER_CHKSPC_REMOVE    10
835 #define HAMMER_CHKSPC_EMERGENCY 0
836
837 #if defined(_KERNEL)
838
839 extern struct vop_ops hammer_vnode_vops;
840 extern struct vop_ops hammer_spec_vops;
841 extern struct vop_ops hammer_fifo_vops;
842 extern struct bio_ops hammer_bioops;
843
844 extern int hammer_debug_io;
845 extern int hammer_debug_general;
846 extern int hammer_debug_debug;
847 extern int hammer_debug_inode;
848 extern int hammer_debug_locks;
849 extern int hammer_debug_btree;
850 extern int hammer_debug_tid;
851 extern int hammer_debug_recover;
852 extern int hammer_debug_recover_faults;
853 extern int hammer_debug_critical;
854 extern int hammer_cluster_enable;
855 extern int hammer_count_fsyncs;
856 extern int hammer_count_inodes;
857 extern int hammer_count_iqueued;
858 extern int hammer_count_reclaiming;
859 extern int hammer_count_records;
860 extern int hammer_count_record_datas;
861 extern int hammer_count_volumes;
862 extern int hammer_count_buffers;
863 extern int hammer_count_nodes;
864 extern int64_t hammer_count_extra_space_used;
865 extern int64_t hammer_stats_btree_lookups;
866 extern int64_t hammer_stats_btree_searches;
867 extern int64_t hammer_stats_btree_inserts;
868 extern int64_t hammer_stats_btree_deletes;
869 extern int64_t hammer_stats_btree_elements;
870 extern int64_t hammer_stats_btree_splits;
871 extern int64_t hammer_stats_btree_iterations;
872 extern int64_t hammer_stats_btree_root_iterations;
873 extern int64_t hammer_stats_record_iterations;
874 extern int64_t hammer_stats_file_read;
875 extern int64_t hammer_stats_file_write;
876 extern int64_t hammer_stats_file_iopsr;
877 extern int64_t hammer_stats_file_iopsw;
878 extern int64_t hammer_stats_disk_read;
879 extern int64_t hammer_stats_disk_write;
880 extern int64_t hammer_stats_inode_flushes;
881 extern int64_t hammer_stats_commits;
882 extern int64_t hammer_stats_undo;
883 extern int64_t hammer_stats_redo;
884 extern int hammer_count_dirtybufspace;
885 extern int hammer_count_refedbufs;
886 extern int hammer_count_reservations;
887 extern int hammer_count_io_running_read;
888 extern int hammer_count_io_running_write;
889 extern int hammer_count_io_locked;
890 extern int hammer_limit_dirtybufspace;
891 extern int hammer_limit_recs;
892 extern int hammer_limit_inode_recs;
893 extern int hammer_limit_reclaim;
894 extern int hammer_limit_redo;
895 extern int hammer_bio_count;
896 extern int hammer_verify_zone;
897 extern int hammer_verify_data;
898 extern int hammer_write_mode;
899 extern int hammer_yield_check;
900 extern int hammer_fsync_mode;
901 extern int hammer_autoflush;
902 extern int64_t hammer_contention_count;
903
904 void    hammer_critical_error(hammer_mount_t hmp, hammer_inode_t ip,
905                         int error, const char *msg);
906 int     hammer_vop_inactive(struct vop_inactive_args *);
907 int     hammer_vop_reclaim(struct vop_reclaim_args *);
908 int     hammer_get_vnode(struct hammer_inode *ip, struct vnode **vpp);
909 struct hammer_inode *hammer_get_inode(hammer_transaction_t trans,
910                         hammer_inode_t dip, int64_t obj_id,
911                         hammer_tid_t asof, u_int32_t localization,
912                         int flags, int *errorp);
913 struct hammer_inode *hammer_get_dummy_inode(hammer_transaction_t trans,
914                         hammer_inode_t dip, int64_t obj_id,
915                         hammer_tid_t asof, u_int32_t localization,
916                         int flags, int *errorp);
917 struct hammer_inode *hammer_find_inode(hammer_transaction_t trans,
918                         int64_t obj_id, hammer_tid_t asof,
919                         u_int32_t localization);
920 void    hammer_scan_inode_snapshots(hammer_mount_t hmp,
921                         hammer_inode_info_t iinfo,
922                         int (*callback)(hammer_inode_t ip, void *data),
923                         void *data);
924 void    hammer_put_inode(struct hammer_inode *ip);
925 void    hammer_put_inode_ref(struct hammer_inode *ip);
926 void    hammer_inode_waitreclaims(hammer_mount_t hmp);
927 void    hammer_inode_waithard(hammer_mount_t hmp);
928
929 int     hammer_unload_volume(hammer_volume_t volume, void *data __unused);
930 int     hammer_adjust_volume_mode(hammer_volume_t volume, void *data __unused);
931
932 int     hammer_unload_buffer(hammer_buffer_t buffer, void *data);
933 int     hammer_install_volume(hammer_mount_t hmp, const char *volname,
934                         struct vnode *devvp);
935 int     hammer_mountcheck_volumes(hammer_mount_t hmp);
936
937 int     hammer_mem_add(hammer_record_t record);
938 int     hammer_ip_lookup(hammer_cursor_t cursor);
939 int     hammer_ip_first(hammer_cursor_t cursor);
940 int     hammer_ip_next(hammer_cursor_t cursor);
941 int     hammer_ip_resolve_data(hammer_cursor_t cursor);
942 int     hammer_ip_delete_record(hammer_cursor_t cursor, hammer_inode_t ip,
943                         hammer_tid_t tid);
944 int     hammer_create_at_cursor(hammer_cursor_t cursor,
945                         hammer_btree_leaf_elm_t leaf, void *udata, int mode);
946 int     hammer_delete_at_cursor(hammer_cursor_t cursor, int delete_flags,
947                         hammer_tid_t delete_tid, u_int32_t delete_ts,
948                         int track, int64_t *stat_bytes);
949 int     hammer_ip_check_directory_empty(hammer_transaction_t trans,
950                         hammer_inode_t ip);
951 int     hammer_sync_hmp(hammer_mount_t hmp, int waitfor);
952 int     hammer_queue_inodes_flusher(hammer_mount_t hmp, int waitfor);
953
954 hammer_record_t
955         hammer_alloc_mem_record(hammer_inode_t ip, int data_len);
956 void    hammer_flush_record_done(hammer_record_t record, int error);
957 void    hammer_wait_mem_record_ident(hammer_record_t record, const char *ident);
958 void    hammer_rel_mem_record(hammer_record_t record);
959
960 int     hammer_cursor_up(hammer_cursor_t cursor);
961 int     hammer_cursor_up_locked(hammer_cursor_t cursor);
962 int     hammer_cursor_down(hammer_cursor_t cursor);
963 int     hammer_cursor_upgrade(hammer_cursor_t cursor);
964 int     hammer_cursor_upgrade_node(hammer_cursor_t cursor);
965 void    hammer_cursor_downgrade(hammer_cursor_t cursor);
966 int     hammer_cursor_seek(hammer_cursor_t cursor, hammer_node_t node,
967                         int index);
968 void    hammer_lock_ex_ident(struct hammer_lock *lock, const char *ident);
969 int     hammer_lock_ex_try(struct hammer_lock *lock);
970 void    hammer_lock_sh(struct hammer_lock *lock);
971 int     hammer_lock_sh_try(struct hammer_lock *lock);
972 int     hammer_lock_upgrade(struct hammer_lock *lock);
973 void    hammer_lock_downgrade(struct hammer_lock *lock);
974 int     hammer_lock_status(struct hammer_lock *lock);
975 void    hammer_unlock(struct hammer_lock *lock);
976 void    hammer_ref(struct hammer_lock *lock);
977 void    hammer_unref(struct hammer_lock *lock);
978
979 void    hammer_sync_lock_ex(hammer_transaction_t trans);
980 void    hammer_sync_lock_sh(hammer_transaction_t trans);
981 int     hammer_sync_lock_sh_try(hammer_transaction_t trans);
982 void    hammer_sync_unlock(hammer_transaction_t trans);
983
984 u_int32_t hammer_to_unix_xid(uuid_t *uuid);
985 void hammer_guid_to_uuid(uuid_t *uuid, u_int32_t guid);
986 void    hammer_time_to_timespec(u_int64_t xtime, struct timespec *ts);
987 u_int64_t hammer_timespec_to_time(struct timespec *ts);
988 int     hammer_str_to_tid(const char *str, int *ispfsp,
989                         hammer_tid_t *tidp, u_int32_t *localizationp);
990 int     hammer_is_atatext(const char *name, int len);
991 hammer_tid_t hammer_alloc_objid(hammer_mount_t hmp, hammer_inode_t dip,
992                         int64_t namekey);
993 void hammer_clear_objid(hammer_inode_t dip);
994 void hammer_destroy_objid_cache(hammer_mount_t hmp);
995
996 int hammer_enter_undo_history(hammer_mount_t hmp, hammer_off_t offset,
997                         int bytes);
998 void hammer_clear_undo_history(hammer_mount_t hmp);
999 enum vtype hammer_get_vnode_type(u_int8_t obj_type);
1000 int hammer_get_dtype(u_int8_t obj_type);
1001 u_int8_t hammer_get_obj_type(enum vtype vtype);
1002 int64_t hammer_directory_namekey(hammer_inode_t dip, const void *name, int len,
1003                         u_int32_t *max_iterationsp);
1004 int     hammer_nohistory(hammer_inode_t ip);
1005
1006 int     hammer_init_cursor(hammer_transaction_t trans, hammer_cursor_t cursor,
1007                         hammer_node_cache_t cache, hammer_inode_t ip);
1008 void    hammer_normalize_cursor(hammer_cursor_t cursor);
1009 void    hammer_done_cursor(hammer_cursor_t cursor);
1010 int     hammer_recover_cursor(hammer_cursor_t cursor);
1011 void    hammer_unlock_cursor(hammer_cursor_t cursor);
1012 int     hammer_lock_cursor(hammer_cursor_t cursor);
1013 hammer_cursor_t hammer_push_cursor(hammer_cursor_t ocursor);
1014 void    hammer_pop_cursor(hammer_cursor_t ocursor, hammer_cursor_t ncursor);
1015
1016 void    hammer_cursor_replaced_node(hammer_node_t onode, hammer_node_t nnode);
1017 void    hammer_cursor_removed_node(hammer_node_t onode, hammer_node_t parent,
1018                         int index);
1019 void    hammer_cursor_split_node(hammer_node_t onode, hammer_node_t nnode,
1020                         int index);
1021 void    hammer_cursor_moved_element(hammer_node_t onode, hammer_node_t nnode,
1022                         int oindex, int nindex);
1023 void    hammer_cursor_parent_changed(hammer_node_t node, hammer_node_t oparent,
1024                         hammer_node_t nparent, int nindex);
1025 void    hammer_cursor_inserted_element(hammer_node_t node, int index);
1026 void    hammer_cursor_deleted_element(hammer_node_t node, int index);
1027
1028 int     hammer_btree_lookup(hammer_cursor_t cursor);
1029 int     hammer_btree_first(hammer_cursor_t cursor);
1030 int     hammer_btree_last(hammer_cursor_t cursor);
1031 int     hammer_btree_extract(hammer_cursor_t cursor, int flags);
1032 int     hammer_btree_iterate(hammer_cursor_t cursor);
1033 int     hammer_btree_iterate_reverse(hammer_cursor_t cursor);
1034 int     hammer_btree_insert(hammer_cursor_t cursor,
1035                             hammer_btree_leaf_elm_t elm, int *doprop);
1036 int     hammer_btree_delete(hammer_cursor_t cursor);
1037 void    hammer_btree_do_propagation(hammer_cursor_t cursor,
1038                             hammer_pseudofs_inmem_t pfsm,
1039                             hammer_btree_leaf_elm_t leaf);
1040 int     hammer_btree_cmp(hammer_base_elm_t key1, hammer_base_elm_t key2);
1041 int     hammer_btree_chkts(hammer_tid_t ts, hammer_base_elm_t key);
1042 int     hammer_btree_correct_rhb(hammer_cursor_t cursor, hammer_tid_t tid);
1043 int     hammer_btree_correct_lhb(hammer_cursor_t cursor, hammer_tid_t tid);
1044
1045 int     btree_set_parent(hammer_transaction_t trans, hammer_node_t node,
1046                         hammer_btree_elm_t elm);
1047 void    hammer_node_lock_init(hammer_node_lock_t parent, hammer_node_t node);
1048 int     hammer_btree_lock_children(hammer_cursor_t cursor, int depth,
1049                         hammer_node_lock_t parent);
1050 void    hammer_btree_lock_copy(hammer_cursor_t cursor,
1051                         hammer_node_lock_t parent);
1052 int     hammer_btree_sync_copy(hammer_cursor_t cursor,
1053                         hammer_node_lock_t parent);
1054 void    hammer_btree_unlock_children(hammer_cursor_t cursor,
1055                         hammer_node_lock_t parent);
1056 int     hammer_btree_search_node(hammer_base_elm_t elm, hammer_node_ondisk_t node);
1057 hammer_node_t hammer_btree_get_parent(hammer_transaction_t trans,
1058                         hammer_node_t node, int *parent_indexp,
1059                         int *errorp, int try_exclusive);
1060
1061 void    hammer_print_btree_node(hammer_node_ondisk_t ondisk);
1062 void    hammer_print_btree_elm(hammer_btree_elm_t elm, u_int8_t type, int i);
1063
1064 void    *hammer_bread(struct hammer_mount *hmp, hammer_off_t off,
1065                         int *errorp, struct hammer_buffer **bufferp);
1066 void    *hammer_bnew(struct hammer_mount *hmp, hammer_off_t off,
1067                         int *errorp, struct hammer_buffer **bufferp);
1068 void    *hammer_bread_ext(struct hammer_mount *hmp, hammer_off_t off, int bytes,
1069                         int *errorp, struct hammer_buffer **bufferp);
1070 void    *hammer_bnew_ext(struct hammer_mount *hmp, hammer_off_t off, int bytes,
1071                         int *errorp, struct hammer_buffer **bufferp);
1072
1073 hammer_volume_t hammer_get_root_volume(hammer_mount_t hmp, int *errorp);
1074
1075 hammer_volume_t hammer_get_volume(hammer_mount_t hmp,
1076                         int32_t vol_no, int *errorp);
1077 hammer_buffer_t hammer_get_buffer(hammer_mount_t hmp, hammer_off_t buf_offset,
1078                         int bytes, int isnew, int *errorp);
1079 void            hammer_sync_buffers(hammer_mount_t hmp,
1080                         hammer_off_t base_offset, int bytes);
1081 int             hammer_del_buffers(hammer_mount_t hmp,
1082                         hammer_off_t base_offset,
1083                         hammer_off_t zone2_offset, int bytes,
1084                         int report_conflicts);
1085
1086 int             hammer_ref_volume(hammer_volume_t volume);
1087 int             hammer_ref_buffer(hammer_buffer_t buffer);
1088 void            hammer_flush_buffer_nodes(hammer_buffer_t buffer);
1089
1090 void            hammer_rel_volume(hammer_volume_t volume, int flush);
1091 void            hammer_rel_buffer(hammer_buffer_t buffer, int flush);
1092
1093 int             hammer_vfs_export(struct mount *mp, int op,
1094                         const struct export_args *export);
1095 hammer_node_t   hammer_get_node(hammer_transaction_t trans,
1096                         hammer_off_t node_offset, int isnew, int *errorp);
1097 void            hammer_ref_node(hammer_node_t node);
1098 hammer_node_t   hammer_ref_node_safe(hammer_transaction_t trans,
1099                         hammer_node_cache_t cache, int *errorp);
1100 void            hammer_rel_node(hammer_node_t node);
1101 void            hammer_delete_node(hammer_transaction_t trans,
1102                         hammer_node_t node);
1103 void            hammer_cache_node(hammer_node_cache_t cache,
1104                         hammer_node_t node);
1105 void            hammer_uncache_node(hammer_node_cache_t cache);
1106 void            hammer_flush_node(hammer_node_t node);
1107
1108 void hammer_dup_buffer(struct hammer_buffer **bufferp,
1109                         struct hammer_buffer *buffer);
1110 hammer_node_t hammer_alloc_btree(hammer_transaction_t trans,
1111                         hammer_off_t hint, int *errorp);
1112 void *hammer_alloc_data(hammer_transaction_t trans, int32_t data_len,
1113                         u_int16_t rec_type, hammer_off_t *data_offsetp,
1114                         struct hammer_buffer **data_bufferp,
1115                         hammer_off_t hint, int *errorp);
1116
1117 int hammer_generate_undo(hammer_transaction_t trans,
1118                         hammer_off_t zone1_offset, void *base, int len);
1119 int hammer_generate_redo(hammer_transaction_t trans, hammer_inode_t ip,
1120                         hammer_off_t file_offset, void *base, int len);
1121 int hammer_upgrade_undo_4(hammer_transaction_t trans);
1122
1123 void hammer_put_volume(struct hammer_volume *volume, int flush);
1124 void hammer_put_buffer(struct hammer_buffer *buffer, int flush);
1125
1126 hammer_off_t hammer_freemap_alloc(hammer_transaction_t trans,
1127                         hammer_off_t owner, int *errorp);
1128 void hammer_freemap_free(hammer_transaction_t trans, hammer_off_t phys_offset,
1129                         hammer_off_t owner, int *errorp);
1130 int _hammer_checkspace(hammer_mount_t hmp, int slop, int64_t *resp);
1131 hammer_off_t hammer_blockmap_alloc(hammer_transaction_t trans, int zone,
1132                         int bytes, hammer_off_t hint, int *errorp);
1133 hammer_reserve_t hammer_blockmap_reserve(hammer_mount_t hmp, int zone,
1134                         int bytes, hammer_off_t *zone_offp, int *errorp);
1135 void hammer_blockmap_reserve_complete(hammer_mount_t hmp,
1136                         hammer_reserve_t resv);
1137 void hammer_reserve_clrdelay(hammer_mount_t hmp, hammer_reserve_t resv);
1138 void hammer_blockmap_free(hammer_transaction_t trans,
1139                         hammer_off_t bmap_off, int bytes);
1140 int hammer_blockmap_finalize(hammer_transaction_t trans,
1141                         hammer_reserve_t resv,
1142                         hammer_off_t bmap_off, int bytes);
1143 int hammer_blockmap_getfree(hammer_mount_t hmp, hammer_off_t bmap_off,
1144                         int *curp, int *errorp);
1145 hammer_off_t hammer_blockmap_lookup(hammer_mount_t hmp, hammer_off_t bmap_off,
1146                         int *errorp);
1147 hammer_off_t hammer_undo_lookup(hammer_mount_t hmp, hammer_off_t bmap_off,
1148                         int *errorp);
1149 int64_t hammer_undo_used(hammer_transaction_t trans);
1150 int64_t hammer_undo_space(hammer_transaction_t trans);
1151 int64_t hammer_undo_max(hammer_mount_t hmp);
1152 int hammer_undo_reclaim(hammer_io_t io);
1153
1154 void hammer_start_transaction(struct hammer_transaction *trans,
1155                               struct hammer_mount *hmp);
1156 void hammer_simple_transaction(struct hammer_transaction *trans,
1157                               struct hammer_mount *hmp);
1158 void hammer_start_transaction_fls(struct hammer_transaction *trans,
1159                                   struct hammer_mount *hmp);
1160 void hammer_done_transaction(struct hammer_transaction *trans);
1161 hammer_tid_t hammer_alloc_tid(hammer_mount_t hmp, int count);
1162
1163 void hammer_modify_inode(hammer_inode_t ip, int flags);
1164 void hammer_flush_inode(hammer_inode_t ip, int flags);
1165 void hammer_flush_inode_done(hammer_inode_t ip, int error);
1166 void hammer_wait_inode(hammer_inode_t ip);
1167
1168 int  hammer_create_inode(struct hammer_transaction *trans, struct vattr *vap,
1169                         struct ucred *cred, struct hammer_inode *dip,
1170                         const char *name, int namelen,
1171                         hammer_pseudofs_inmem_t pfsm,
1172                         struct hammer_inode **ipp);
1173 void hammer_rel_inode(hammer_inode_t ip, int flush);
1174 int hammer_reload_inode(hammer_inode_t ip, void *arg __unused);
1175 int hammer_ino_rb_compare(hammer_inode_t ip1, hammer_inode_t ip2);
1176 int hammer_destroy_inode_callback(hammer_inode_t ip, void *data __unused);
1177
1178 int hammer_sync_inode(hammer_transaction_t trans, hammer_inode_t ip);
1179 void hammer_test_inode(hammer_inode_t dip);
1180 void hammer_inode_unloadable_check(hammer_inode_t ip, int getvp);
1181
1182 int  hammer_ip_add_directory(struct hammer_transaction *trans,
1183                         hammer_inode_t dip, const char *name, int bytes,
1184                         hammer_inode_t nip);
1185 int  hammer_ip_del_directory(struct hammer_transaction *trans,
1186                         hammer_cursor_t cursor, hammer_inode_t dip,
1187                         hammer_inode_t ip);
1188 hammer_record_t hammer_ip_add_bulk(hammer_inode_t ip, off_t file_offset,
1189                         void *data, int bytes, int *errorp);
1190 int  hammer_ip_frontend_trunc(struct hammer_inode *ip, off_t file_size);
1191 int  hammer_ip_add_record(struct hammer_transaction *trans,
1192                         hammer_record_t record);
1193 int  hammer_ip_delete_range(hammer_cursor_t cursor, hammer_inode_t ip,
1194                         int64_t ran_beg, int64_t ran_end, int truncating);
1195 int  hammer_ip_delete_clean(hammer_cursor_t cursor, hammer_inode_t ip,
1196                         int *countp);
1197 int  hammer_ip_sync_data(hammer_cursor_t cursor, hammer_inode_t ip,
1198                         int64_t offset, void *data, int bytes);
1199 int  hammer_ip_sync_record(hammer_transaction_t trans, hammer_record_t rec);
1200 int  hammer_ip_sync_record_cursor(hammer_cursor_t cursor, hammer_record_t rec);
1201 hammer_pseudofs_inmem_t  hammer_load_pseudofs(hammer_transaction_t trans,
1202                         u_int32_t localization, int *errorp);
1203 int  hammer_mkroot_pseudofs(hammer_transaction_t trans, struct ucred *cred,
1204                         hammer_pseudofs_inmem_t pfsm);
1205 int  hammer_save_pseudofs(hammer_transaction_t trans,
1206                         hammer_pseudofs_inmem_t pfsm);
1207 int  hammer_unload_pseudofs(hammer_transaction_t trans, u_int32_t localization);
1208 void hammer_rel_pseudofs(hammer_mount_t hmp, hammer_pseudofs_inmem_t pfsm);
1209 int hammer_ioctl(hammer_inode_t ip, u_long com, caddr_t data, int fflag,
1210                         struct ucred *cred);
1211
1212 void hammer_io_init(hammer_io_t io, hammer_volume_t volume,
1213                         enum hammer_io_type type);
1214 int hammer_io_read(struct vnode *devvp, struct hammer_io *io,
1215                         hammer_off_t limit);
1216 void hammer_io_advance(struct hammer_io *io);
1217 int hammer_io_new(struct vnode *devvp, struct hammer_io *io);
1218 int hammer_io_inval(hammer_volume_t volume, hammer_off_t zone2_offset);
1219 struct buf *hammer_io_release(struct hammer_io *io, int flush);
1220 void hammer_io_flush(struct hammer_io *io, int reclaim);
1221 void hammer_io_wait(struct hammer_io *io);
1222 void hammer_io_waitdep(struct hammer_io *io);
1223 void hammer_io_wait_all(hammer_mount_t hmp, const char *ident);
1224 int hammer_io_direct_read(hammer_mount_t hmp, struct bio *bio,
1225                         hammer_btree_leaf_elm_t leaf);
1226 int hammer_io_direct_write(hammer_mount_t hmp, hammer_record_t record,
1227                         struct bio *bio);
1228 void hammer_io_direct_wait(hammer_record_t record);
1229 void hammer_io_direct_uncache(hammer_mount_t hmp, hammer_btree_leaf_elm_t leaf);
1230 void hammer_io_write_interlock(hammer_io_t io);
1231 void hammer_io_done_interlock(hammer_io_t io);
1232 void hammer_io_clear_modify(struct hammer_io *io, int inval);
1233 void hammer_io_clear_modlist(struct hammer_io *io);
1234 void hammer_io_flush_sync(hammer_mount_t hmp);
1235 void hammer_io_clear_error(struct hammer_io *io);
1236
1237 void hammer_modify_volume(hammer_transaction_t trans, hammer_volume_t volume,
1238                         void *base, int len);
1239 void hammer_modify_buffer(hammer_transaction_t trans, hammer_buffer_t buffer,
1240                         void *base, int len);
1241 void hammer_modify_volume_done(hammer_volume_t volume);
1242 void hammer_modify_buffer_done(hammer_buffer_t buffer);
1243
1244 int hammer_ioc_reblock(hammer_transaction_t trans, hammer_inode_t ip,
1245                         struct hammer_ioc_reblock *reblock);
1246 int hammer_ioc_rebalance(hammer_transaction_t trans, hammer_inode_t ip,
1247                         struct hammer_ioc_rebalance *rebal);
1248 int hammer_ioc_prune(hammer_transaction_t trans, hammer_inode_t ip,
1249                         struct hammer_ioc_prune *prune);
1250 int hammer_ioc_mirror_read(hammer_transaction_t trans, hammer_inode_t ip,
1251                         struct hammer_ioc_mirror_rw *mirror);
1252 int hammer_ioc_mirror_write(hammer_transaction_t trans, hammer_inode_t ip,
1253                         struct hammer_ioc_mirror_rw *mirror);
1254 int hammer_ioc_set_pseudofs(hammer_transaction_t trans, hammer_inode_t ip,
1255                         struct ucred *cred, struct hammer_ioc_pseudofs_rw *pfs);
1256 int hammer_ioc_get_pseudofs(hammer_transaction_t trans, hammer_inode_t ip,
1257                         struct hammer_ioc_pseudofs_rw *pfs);
1258 int hammer_ioc_destroy_pseudofs(hammer_transaction_t trans, hammer_inode_t ip,
1259                         struct hammer_ioc_pseudofs_rw *pfs);
1260 int hammer_ioc_downgrade_pseudofs(hammer_transaction_t trans, hammer_inode_t ip,
1261                         struct hammer_ioc_pseudofs_rw *pfs);
1262 int hammer_ioc_upgrade_pseudofs(hammer_transaction_t trans, hammer_inode_t ip,
1263                         struct hammer_ioc_pseudofs_rw *pfs);
1264 int hammer_ioc_wait_pseudofs(hammer_transaction_t trans, hammer_inode_t ip,
1265                         struct hammer_ioc_pseudofs_rw *pfs);
1266 int hammer_ioc_volume_add(hammer_transaction_t trans, hammer_inode_t ip,
1267                         struct hammer_ioc_volume *ioc);
1268 int hammer_ioc_volume_del(hammer_transaction_t trans, hammer_inode_t ip,
1269                         struct hammer_ioc_volume *ioc);
1270
1271 int hammer_signal_check(hammer_mount_t hmp);
1272
1273 void hammer_flusher_create(hammer_mount_t hmp);
1274 void hammer_flusher_destroy(hammer_mount_t hmp);
1275 void hammer_flusher_sync(hammer_mount_t hmp);
1276 int  hammer_flusher_async(hammer_mount_t hmp, hammer_flush_group_t flg);
1277 int  hammer_flusher_async_one(hammer_mount_t hmp);
1278 void hammer_flusher_wait(hammer_mount_t hmp, int seq);
1279 void hammer_flusher_wait_next(hammer_mount_t hmp);
1280 int  hammer_flusher_meta_limit(hammer_mount_t hmp);
1281 int  hammer_flusher_meta_halflimit(hammer_mount_t hmp);
1282 int  hammer_flusher_undo_exhausted(hammer_transaction_t trans, int quarter);
1283 void hammer_flusher_clean_loose_ios(hammer_mount_t hmp);
1284 void hammer_flusher_finalize(hammer_transaction_t trans, int final);
1285 int  hammer_flusher_haswork(hammer_mount_t hmp);
1286 void hammer_flusher_flush_undos(hammer_mount_t hmp, int already_flushed);
1287
1288 int hammer_recover_stage1(hammer_mount_t hmp, hammer_volume_t rootvol);
1289 int hammer_recover_stage2(hammer_mount_t hmp, hammer_volume_t rootvol);
1290 void hammer_recover_flush_buffers(hammer_mount_t hmp,
1291                         hammer_volume_t root_volume, int final);
1292
1293 void hammer_crc_set_blockmap(hammer_blockmap_t blockmap);
1294 void hammer_crc_set_volume(hammer_volume_ondisk_t ondisk);
1295 void hammer_crc_set_leaf(void *data, hammer_btree_leaf_elm_t leaf);
1296
1297 int hammer_crc_test_blockmap(hammer_blockmap_t blockmap);
1298 int hammer_crc_test_volume(hammer_volume_ondisk_t ondisk);
1299 int hammer_crc_test_btree(hammer_node_ondisk_t ondisk);
1300 int hammer_crc_test_leaf(void *data, hammer_btree_leaf_elm_t leaf);
1301 void hkprintf(const char *ctl, ...);
1302 udev_t hammer_fsid_to_udev(uuid_t *uuid);
1303
1304
1305 int hammer_blocksize(int64_t file_offset);
1306 int64_t hammer_blockdemarc(int64_t file_offset1, int64_t file_offset2);
1307
1308 /*
1309  * Shortcut for _hammer_checkspace(), used all over the code.
1310  */
1311 static __inline int
1312 hammer_checkspace(hammer_mount_t hmp, int slop)
1313 {
1314         return(_hammer_checkspace(hmp, slop, NULL));
1315 }
1316
1317 #endif
1318
1319 static __inline void
1320 hammer_wait_mem_record(hammer_record_t record)
1321 {
1322         hammer_wait_mem_record_ident(record, "hmmwai");
1323 }
1324
1325 static __inline void
1326 hammer_lock_ex(struct hammer_lock *lock)
1327 {
1328         hammer_lock_ex_ident(lock, "hmrlck");
1329 }
1330
1331 /*
1332  * Indicate that a B-Tree node is being modified.
1333  */
1334 static __inline void
1335 hammer_modify_node_noundo(hammer_transaction_t trans, hammer_node_t node)
1336 {
1337         KKASSERT((node->flags & HAMMER_NODE_CRCBAD) == 0);
1338         hammer_modify_buffer(trans, node->buffer, NULL, 0);
1339 }
1340
1341 static __inline void
1342 hammer_modify_node_all(hammer_transaction_t trans, struct hammer_node *node)
1343 {
1344         KKASSERT((node->flags & HAMMER_NODE_CRCBAD) == 0);
1345         hammer_modify_buffer(trans, node->buffer,
1346                              node->ondisk, sizeof(*node->ondisk));
1347 }
1348
1349 static __inline void
1350 hammer_modify_node(hammer_transaction_t trans, hammer_node_t node,
1351                    void *base, int len)
1352 {
1353         hammer_crc_t *crcptr;
1354
1355         KKASSERT((char *)base >= (char *)node->ondisk &&
1356                  (char *)base + len <=
1357                     (char *)node->ondisk + sizeof(*node->ondisk));
1358         KKASSERT((node->flags & HAMMER_NODE_CRCBAD) == 0);
1359         hammer_modify_buffer(trans, node->buffer, base, len);
1360         crcptr = &node->ondisk->crc;
1361         hammer_modify_buffer(trans, node->buffer, crcptr, sizeof(hammer_crc_t));
1362         --node->buffer->io.modify_refs; /* only want one ref */
1363 }
1364
1365 /*
1366  * Indicate that the specified modifications have been completed.
1367  *
1368  * Do not try to generate the crc here, it's very expensive to do and a
1369  * sequence of insertions or deletions can result in many calls to this
1370  * function on the same node.
1371  */
1372 static __inline void
1373 hammer_modify_node_done(hammer_node_t node)
1374 {
1375         node->flags |= HAMMER_NODE_CRCGOOD;
1376         if ((node->flags & HAMMER_NODE_NEEDSCRC) == 0) {
1377                 node->flags |= HAMMER_NODE_NEEDSCRC;
1378                 node->buffer->io.gencrc = 1;
1379                 hammer_ref_node(node);
1380         }
1381         hammer_modify_buffer_done(node->buffer);
1382 }
1383
1384 #define hammer_modify_volume_field(trans, vol, field)           \
1385         hammer_modify_volume(trans, vol, &(vol)->ondisk->field, \
1386                              sizeof((vol)->ondisk->field))
1387
1388 #define hammer_modify_node_field(trans, node, field)            \
1389         hammer_modify_node(trans, node, &(node)->ondisk->field, \
1390                              sizeof((node)->ondisk->field))
1391
1392 /*
1393  * The HAMMER_INODE_CAP_DIR_LOCAL_INO capability is set on newly
1394  * created directories for HAMMER version 2 or greater and causes
1395  * directory entries to be placed the inode localization zone in
1396  * the B-Tree instead of the misc zone.
1397  *
1398  * This greatly improves localization between directory entries and
1399  * inodes
1400  */
1401 static __inline u_int32_t
1402 hammer_dir_localization(hammer_inode_t dip)
1403 {
1404         if (dip->ino_data.cap_flags & HAMMER_INODE_CAP_DIR_LOCAL_INO)
1405                 return(HAMMER_LOCALIZE_INODE);
1406         else
1407                 return(HAMMER_LOCALIZE_MISC);
1408 }