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