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