wi(4) depends on wlan(4)
[dragonfly.git] / sys / vfs / hammer / hammer.h
1 /*
2  * Copyright (c) 2007 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.51 2008/04/26 08:02:17 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  * inode->inode dependancy
139  */
140 typedef struct hammer_depend {
141         TAILQ_ENTRY(hammer_depend) ip_entry;
142         TAILQ_ENTRY(hammer_depend) rec_entry;
143         struct hammer_inode *ip;
144         struct hammer_record *record;
145 } *hammer_depend_t;
146
147 TAILQ_HEAD(hammer_depend_list, hammer_depend);
148
149 /*
150  * Structure used to represent an inode in-memory.
151  *
152  * The record and data associated with an inode may be out of sync with
153  * the disk (xDIRTY flags), or not even on the disk at all (ONDISK flag
154  * clear).
155  *
156  * An inode may also hold a cache of unsynchronized records, used for
157  * database and directories only.  Unsynchronized regular file data is
158  * stored in the buffer cache.
159  *
160  * NOTE: A file which is created and destroyed within the initial
161  * synchronization period can wind up not doing any disk I/O at all.
162  *
163  * Finally, an inode may cache numerous disk-referencing B-Tree cursors.
164  */
165 struct hammer_ino_rb_tree;
166 struct hammer_inode;
167 RB_HEAD(hammer_ino_rb_tree, hammer_inode);
168 RB_PROTOTYPEX(hammer_ino_rb_tree, INFO, hammer_inode, rb_node,
169               hammer_ino_rb_compare, hammer_inode_info_t);
170
171 struct hammer_rec_rb_tree;
172 struct hammer_record;
173 RB_HEAD(hammer_rec_rb_tree, hammer_record);
174 RB_PROTOTYPEX(hammer_rec_rb_tree, INFO, hammer_record, rb_node,
175               hammer_rec_rb_compare, hammer_base_elm_t);
176
177 TAILQ_HEAD(hammer_node_list, hammer_node);
178
179 typedef enum hammer_inode_state {
180         HAMMER_FST_IDLE,
181         HAMMER_FST_SETUP,
182         HAMMER_FST_FLUSH
183 } hammer_inode_state_t;
184
185 struct hammer_inode {
186         RB_ENTRY(hammer_inode) rb_node;
187         hammer_inode_state_t flush_state;
188         TAILQ_ENTRY(hammer_inode) flush_entry;
189         struct hammer_depend_list depend_list;
190         u_int64_t               obj_id;         /* (key) object identifier */
191         hammer_tid_t            obj_asof;       /* (key) snapshot or 0 */
192         struct hammer_mount     *hmp;
193         int                     flags;
194         int                     error;          /* flush error */
195         int                     depend_count;
196         int                     cursor_ip_refs; /* sanity */
197         struct vnode            *vp;
198         struct lockf            advlock;
199         struct hammer_lock      lock;           /* sync copy interlock */
200         TAILQ_HEAD(, bio)       bio_list;       /* BIOs to flush out */
201         TAILQ_HEAD(, bio)       bio_alt_list;   /* BIOs to flush out */
202         off_t                   trunc_off;
203         struct hammer_inode_record ino_rec;     /* in-memory cache */
204         struct hammer_inode_data ino_data;      /* in-memory cache */
205         struct hammer_rec_rb_tree rec_tree;     /* in-memory cache */
206         struct hammer_node      *cache[2];      /* search initiate cache */
207
208         /*
209          * When a demark is created to synchronize an inode to
210          * disk, certain fields are copied so the front-end VOPs
211          * can continue to run in parallel with the synchronization
212          * occuring in the background.
213          */
214         int             sync_flags;             /* to-sync flags cache */
215         off_t           sync_trunc_off;         /* to-sync truncation */
216         struct hammer_inode_record sync_ino_rec;/* to-sync cache */
217         struct hammer_inode_data sync_ino_data; /* to-sync cache */
218 };
219
220 typedef struct hammer_inode *hammer_inode_t;
221
222 #define VTOI(vp)        ((struct hammer_inode *)(vp)->v_data)
223
224 #define HAMMER_INODE_DDIRTY     0x0001  /* in-memory ino_data is dirty */
225 #define HAMMER_INODE_RDIRTY     0x0002  /* in-memory ino_rec is dirty */
226 #define HAMMER_INODE_ITIMES     0x0004  /* in-memory mtime/atime modified */
227 #define HAMMER_INODE_XDIRTY     0x0008  /* in-memory records/flsbufs present */
228 #define HAMMER_INODE_ONDISK     0x0010  /* inode is on-disk (else not yet) */
229 #define HAMMER_INODE_FLUSH      0x0020  /* flush on last ref */
230 #define HAMMER_INODE_DELETED    0x0080  /* inode ready for deletion */
231 #define HAMMER_INODE_DELONDISK  0x0100  /* delete synchronized to disk */
232 #define HAMMER_INODE_RO         0x0200  /* read-only (because of as-of) */
233 #define HAMMER_INODE_GONE       0x0400  /* delete flushed out */
234 #define HAMMER_INODE_DONDISK    0x0800  /* data records may be on disk */
235 #define HAMMER_INODE_BUFS       0x1000  /* dirty high level bps present */
236 #define HAMMER_INODE_REFLUSH    0x2000  /* pipelined flush during flush */
237 #define HAMMER_INODE_UNUSED4000 0x4000
238 #define HAMMER_INODE_FLUSHW     0x8000  /* Someone waiting for flush */
239
240 #define HAMMER_INODE_TRUNCATED  0x00010000
241
242 #define HAMMER_INODE_MODMASK    (HAMMER_INODE_DDIRTY|HAMMER_INODE_RDIRTY| \
243                                  HAMMER_INODE_XDIRTY|HAMMER_INODE_BUFS|   \
244                                  HAMMER_INODE_ITIMES|HAMMER_INODE_TRUNCATED)
245
246 #define HAMMER_MAX_INODE_CURSORS        4
247
248 #define HAMMER_FLUSH_SIGNAL     0x0001
249 #define HAMMER_FLUSH_FORCE      0x0002
250 #define HAMMER_FLUSH_RELEASE    0x0004
251
252 /*
253  * Structure used to represent an unsynchronized record in-memory.  This
254  * structure is orgranized in a per-inode RB-tree.  If the inode is not
255  * on disk then neither are any records and the in-memory record tree
256  * represents the entire contents of the inode.  If the inode is on disk
257  * then the on-disk B-Tree is scanned in parallel with the in-memory
258  * RB-Tree to synthesize the current state of the file.
259  *
260  * Only current (delete_tid == 0) unsynchronized records are kept in-memory.
261  *
262  * blocked is the count of the number of cursors (ip_first/ip_next) blocked
263  * on the record waiting for a synchronization to complete.
264  */
265 struct hammer_record {
266         RB_ENTRY(hammer_record)         rb_node;
267         hammer_inode_state_t            state;
268         struct hammer_lock              lock;
269         struct hammer_inode             *ip;
270         union hammer_record_ondisk      rec;
271         union hammer_data_ondisk        *data;
272         int                             flags;
273         struct hammer_depend_list       depend_list;
274 };
275
276 typedef struct hammer_record *hammer_record_t;
277
278 #define HAMMER_RECF_ALLOCDATA           0x0001
279 #define HAMMER_RECF_ONRBTREE            0x0002
280 #define HAMMER_RECF_DELETED_FE          0x0004  /* deleted (frontend) */
281 #define HAMMER_RECF_INBAND              0x0008
282 #define HAMMER_RECF_DELETED_BE          0x0010  /* deleted (backend) */
283 #define HAMMER_RECF_WANTED              0x0020
284 #define HAMMER_RECF_DELETE_ONDISK       0x0040
285
286 /*
287  * In-memory structures representing on-disk structures.
288  */
289 struct hammer_volume;
290 struct hammer_buffer;
291 struct hammer_node;
292 RB_HEAD(hammer_vol_rb_tree, hammer_volume);
293 RB_HEAD(hammer_buf_rb_tree, hammer_buffer);
294 RB_HEAD(hammer_nod_rb_tree, hammer_node);
295
296 RB_PROTOTYPE2(hammer_vol_rb_tree, hammer_volume, rb_node,
297               hammer_vol_rb_compare, int32_t);
298 RB_PROTOTYPE2(hammer_buf_rb_tree, hammer_buffer, rb_node,
299               hammer_buf_rb_compare, hammer_off_t);
300 RB_PROTOTYPE2(hammer_nod_rb_tree, hammer_node, rb_node,
301               hammer_nod_rb_compare, hammer_off_t);
302
303 /*
304  * IO management - embedded at the head of various in-memory structures
305  *
306  * VOLUME       - hammer_volume containing meta-data
307  * META_BUFFER  - hammer_buffer containing meta-data
308  * DATA_BUFFER  - hammer_buffer containing pure-data
309  *
310  * Dirty volume headers and dirty meta-data buffers are locked until the
311  * flusher can sequence them out.  Dirty pure-data buffers can be written.
312  * Clean buffers can be passively released.
313  */
314 typedef enum hammer_io_type {
315         HAMMER_STRUCTURE_VOLUME,
316         HAMMER_STRUCTURE_META_BUFFER,
317         HAMMER_STRUCTURE_UNDO_BUFFER,
318         HAMMER_STRUCTURE_DATA_BUFFER
319 } hammer_io_type_t;
320
321 union hammer_io_structure;
322 struct hammer_io;
323
324 struct worklist {
325         LIST_ENTRY(worklist) node;
326 };
327
328 TAILQ_HEAD(hammer_io_list, hammer_io);
329 typedef struct hammer_io_list *hammer_io_list_t;
330
331 struct hammer_io {
332         struct worklist         worklist;
333         struct hammer_lock      lock;
334         enum hammer_io_type     type;
335         struct hammer_mount     *hmp;
336         TAILQ_ENTRY(hammer_io)  mod_entry; /* list entry if modified */
337         hammer_io_list_t        mod_list;
338         struct buf              *bp;
339         int64_t                 offset;
340         int                     loading;   /* loading/unloading interlock */
341         int                     modify_refs;
342
343         u_int           modified : 1;   /* bp's data was modified */
344         u_int           released : 1;   /* bp released (w/ B_LOCKED set) */
345         u_int           running : 1;    /* bp write IO in progress */
346         u_int           waiting : 1;    /* someone is waiting on us */
347         u_int           validated : 1;  /* ondisk has been validated */
348         u_int           flush : 1;      /* flush on last release */
349         u_int           waitdep : 1;    /* flush waits for dependancies */
350 };
351
352 typedef struct hammer_io *hammer_io_t;
353
354 /*
355  * In-memory volume representing on-disk buffer
356  */
357 struct hammer_volume {
358         struct hammer_io io;
359         RB_ENTRY(hammer_volume) rb_node;
360         struct hammer_buf_rb_tree rb_bufs_root;
361         struct hammer_volume_ondisk *ondisk;
362         int32_t vol_no;
363         int64_t nblocks;        /* note: special calculation for statfs */
364         int64_t buffer_base;    /* base offset of buffer 0 */
365         hammer_off_t maxbuf_off; /* Maximum buffer offset */
366         char    *vol_name;
367         struct vnode *devvp;
368         int     vol_flags;
369 };
370
371 typedef struct hammer_volume *hammer_volume_t;
372
373 /*
374  * In-memory buffer (other then volume, super-cluster, or cluster),
375  * representing an on-disk buffer.
376  */
377 struct hammer_buffer {
378         struct hammer_io io;
379         RB_ENTRY(hammer_buffer) rb_node;
380         void *ondisk;
381         struct hammer_volume *volume;
382         hammer_off_t zone2_offset;
383         hammer_off_t zoneX_offset;
384         struct hammer_node_list clist;
385 };
386
387 typedef struct hammer_buffer *hammer_buffer_t;
388
389 /*
390  * In-memory B-Tree node, representing an on-disk B-Tree node.
391  *
392  * This is a hang-on structure which is backed by a hammer_buffer,
393  * indexed by a hammer_cluster, and used for fine-grained locking of
394  * B-Tree nodes in order to properly control lock ordering.  A hammer_buffer
395  * can contain multiple nodes representing wildly disassociated portions
396  * of the B-Tree so locking cannot be done on a buffer-by-buffer basis.
397  *
398  * This structure uses a cluster-relative index to reduce the number
399  * of layers required to access it, and also because all on-disk B-Tree
400  * references are cluster-relative offsets.
401  */
402 struct hammer_node {
403         struct hammer_lock      lock;           /* node-by-node lock */
404         TAILQ_ENTRY(hammer_node) entry;         /* per-buffer linkage */
405         RB_ENTRY(hammer_node)   rb_node;        /* per-cluster linkage */
406         hammer_off_t            node_offset;    /* full offset spec */
407         struct hammer_mount     *hmp;
408         struct hammer_buffer    *buffer;        /* backing buffer */
409         hammer_node_ondisk_t    ondisk;         /* ptr to on-disk structure */
410         struct hammer_node      **cache1;       /* passive cache(s) */
411         struct hammer_node      **cache2;
412         int                     flags;
413         int                     loading;        /* load interlock */
414 };
415
416 #define HAMMER_NODE_DELETED     0x0001
417 #define HAMMER_NODE_FLUSH       0x0002
418
419 typedef struct hammer_node      *hammer_node_t;
420
421 /*
422  * List of locked nodes.
423  */
424 struct hammer_node_locklist {
425         struct hammer_node_locklist *next;
426         hammer_node_t   node;
427 };
428
429 typedef struct hammer_node_locklist *hammer_node_locklist_t;
430
431
432 /*
433  * Common I/O management structure - embedded in in-memory structures
434  * which are backed by filesystem buffers.
435  */
436 union hammer_io_structure {
437         struct hammer_io        io;
438         struct hammer_volume    volume;
439         struct hammer_buffer    buffer;
440 };
441
442 typedef union hammer_io_structure *hammer_io_structure_t;
443
444 /*
445  * Allocation holes are recorded for a short period of time in an attempt
446  * to use up the space.
447  */
448
449 #define HAMMER_MAX_HOLES        8
450
451 struct hammer_hole;
452
453 struct hammer_holes {
454         TAILQ_HEAD(, hammer_hole) list;
455         int     count;
456 };
457
458 typedef struct hammer_holes *hammer_holes_t;
459
460 struct hammer_hole {
461         TAILQ_ENTRY(hammer_hole) entry;
462         hammer_off_t    offset;
463         int             bytes;
464 };
465
466 typedef struct hammer_hole *hammer_hole_t;
467
468 #include "hammer_cursor.h"
469
470 /*
471  * Internal hammer mount data structure
472  */
473 struct hammer_mount {
474         struct mount *mp;
475         /*struct vnode *rootvp;*/
476         struct hammer_ino_rb_tree rb_inos_root;
477         struct hammer_vol_rb_tree rb_vols_root;
478         struct hammer_nod_rb_tree rb_nods_root;
479         struct hammer_volume *rootvol;
480         struct hammer_base_elm root_btree_beg;
481         struct hammer_base_elm root_btree_end;
482         char    *zbuf;  /* HAMMER_BUFSIZE bytes worth of all-zeros */
483         int     hflags;
484         int     ronly;
485         int     nvolumes;
486         int     volume_iterator;
487         int     flusher_seq;
488         int     flusher_act;
489         int     flusher_exiting;
490         int     reclaim_count;
491         thread_t flusher_td;
492         u_int   check_interrupt;
493         uuid_t  fsid;
494         udev_t  fsid_udev;
495         struct hammer_io_list volu_list;        /* dirty undo buffers */
496         struct hammer_io_list undo_list;        /* dirty undo buffers */
497         struct hammer_io_list data_list;        /* dirty data buffers */
498         struct hammer_io_list meta_list;        /* dirty meta bufs    */
499         struct hammer_io_list lose_list;        /* loose buffers      */
500         int     locked_dirty_count;             /* meta/volu count    */
501         int     io_running_count;
502         hammer_tid_t asof;
503         hammer_off_t next_tid;
504         u_int32_t namekey_iterator;
505         hammer_off_t zone_limits[HAMMER_MAX_ZONES];
506         struct netexport export;
507         struct lock blockmap_lock;
508         struct hammer_holes holes[HAMMER_MAX_ZONES];
509         TAILQ_HEAD(, hammer_inode) flush_list;
510 };
511
512 typedef struct hammer_mount     *hammer_mount_t;
513
514 struct hammer_sync_info {
515         int error;
516         int waitfor;
517 };
518
519 #endif
520
521 #if defined(_KERNEL)
522
523 extern struct vop_ops hammer_vnode_vops;
524 extern struct vop_ops hammer_spec_vops;
525 extern struct vop_ops hammer_fifo_vops;
526 extern struct bio_ops hammer_bioops;
527
528 extern int hammer_debug_general;
529 extern int hammer_debug_locks;
530 extern int hammer_debug_btree;
531 extern int hammer_debug_tid;
532 extern int hammer_debug_recover;
533 extern int hammer_debug_recover_faults;
534 extern int hammer_count_inodes;
535 extern int hammer_count_records;
536 extern int hammer_count_record_datas;
537 extern int hammer_count_volumes;
538 extern int hammer_count_buffers;
539 extern int hammer_count_nodes;
540 extern int64_t hammer_contention_count;
541
542 int     hammer_vop_inactive(struct vop_inactive_args *);
543 int     hammer_vop_reclaim(struct vop_reclaim_args *);
544 int     hammer_get_vnode(struct hammer_inode *ip, int lktype,
545                         struct vnode **vpp);
546 struct hammer_inode *hammer_get_inode(hammer_transaction_t trans,
547                         struct hammer_node **cache,
548                         u_int64_t obj_id, hammer_tid_t asof, int flags,
549                         int *errorp);
550 void    hammer_put_inode(struct hammer_inode *ip);
551 void    hammer_put_inode_ref(struct hammer_inode *ip);
552
553 int     hammer_unload_volume(hammer_volume_t volume, void *data __unused);
554 int     hammer_unload_buffer(hammer_buffer_t buffer, void *data __unused);
555 int     hammer_install_volume(hammer_mount_t hmp, const char *volname);
556
557 int     hammer_ip_lookup(hammer_cursor_t cursor, hammer_inode_t ip);
558 int     hammer_ip_first(hammer_cursor_t cursor, hammer_inode_t ip);
559 int     hammer_ip_next(hammer_cursor_t cursor);
560 int     hammer_ip_resolve_record_and_data(hammer_cursor_t cursor);
561 int     hammer_ip_resolve_data(hammer_cursor_t cursor);
562 int     hammer_ip_delete_record(hammer_cursor_t cursor, hammer_tid_t tid);
563 int     hammer_delete_at_cursor(hammer_cursor_t cursor, int64_t *stat_bytes);
564 int     hammer_ip_check_directory_empty(hammer_transaction_t trans,
565                         hammer_inode_t ip);
566 int     hammer_sync_hmp(hammer_mount_t hmp, int waitfor);
567
568 hammer_record_t
569         hammer_alloc_mem_record(hammer_inode_t ip);
570 void    hammer_flush_record_done(hammer_record_t record);
571 void    hammer_wait_mem_record(hammer_record_t record);
572 void    hammer_rel_mem_record(hammer_record_t record);
573 void    hammer_delete_mem_record(hammer_record_t record);
574
575
576 int     hammer_cursor_up(hammer_cursor_t cursor);
577 int     hammer_cursor_down(hammer_cursor_t cursor);
578 int     hammer_cursor_upgrade(hammer_cursor_t cursor);
579 void    hammer_cursor_downgrade(hammer_cursor_t cursor);
580 int     hammer_cursor_seek(hammer_cursor_t cursor, hammer_node_t node,
581                         int index);
582 void    hammer_lock_ex(struct hammer_lock *lock);
583 int     hammer_lock_ex_try(struct hammer_lock *lock);
584 void    hammer_lock_sh(struct hammer_lock *lock);
585 int     hammer_lock_upgrade(struct hammer_lock *lock);
586 void    hammer_lock_downgrade(struct hammer_lock *lock);
587 void    hammer_unlock(struct hammer_lock *lock);
588 void    hammer_ref(struct hammer_lock *lock);
589 void    hammer_unref(struct hammer_lock *lock);
590
591 u_int32_t hammer_to_unix_xid(uuid_t *uuid);
592 void hammer_guid_to_uuid(uuid_t *uuid, u_int32_t guid);
593 void    hammer_to_timespec(hammer_tid_t tid, struct timespec *ts);
594 hammer_tid_t hammer_timespec_to_transid(struct timespec *ts);
595 hammer_tid_t hammer_alloc_tid(hammer_transaction_t trans);
596 hammer_tid_t hammer_now_tid(void);
597 hammer_tid_t hammer_str_to_tid(const char *str);
598
599 enum vtype hammer_get_vnode_type(u_int8_t obj_type);
600 int hammer_get_dtype(u_int8_t obj_type);
601 u_int8_t hammer_get_obj_type(enum vtype vtype);
602 int64_t hammer_directory_namekey(void *name, int len);
603
604 int     hammer_init_cursor(hammer_transaction_t trans, hammer_cursor_t cursor,
605                            struct hammer_node **cache);
606
607 void    hammer_done_cursor(hammer_cursor_t cursor);
608 void    hammer_mem_done(hammer_cursor_t cursor);
609
610 int     hammer_btree_lookup(hammer_cursor_t cursor);
611 int     hammer_btree_first(hammer_cursor_t cursor);
612 int     hammer_btree_last(hammer_cursor_t cursor);
613 int     hammer_btree_extract(hammer_cursor_t cursor, int flags);
614 int     hammer_btree_iterate(hammer_cursor_t cursor);
615 int     hammer_btree_iterate_reverse(hammer_cursor_t cursor);
616 int     hammer_btree_insert(hammer_cursor_t cursor, hammer_btree_elm_t elm);
617 int     hammer_btree_delete(hammer_cursor_t cursor);
618 int     hammer_btree_cmp(hammer_base_elm_t key1, hammer_base_elm_t key2);
619 int     hammer_btree_chkts(hammer_tid_t ts, hammer_base_elm_t key);
620 int     hammer_btree_correct_rhb(hammer_cursor_t cursor, hammer_tid_t tid);
621 int     hammer_btree_correct_lhb(hammer_cursor_t cursor, hammer_tid_t tid);
622
623
624 int     hammer_btree_lock_children(hammer_cursor_t cursor,
625                         struct hammer_node_locklist **locklistp);
626
627 void    hammer_print_btree_node(hammer_node_ondisk_t ondisk);
628 void    hammer_print_btree_elm(hammer_btree_elm_t elm, u_int8_t type, int i);
629
630 void    *hammer_bread(struct hammer_mount *hmp, hammer_off_t off,
631                         int *errorp, struct hammer_buffer **bufferp);
632 void    *hammer_bnew(struct hammer_mount *hmp, hammer_off_t off,
633                         int *errorp, struct hammer_buffer **bufferp);
634
635 hammer_volume_t hammer_get_root_volume(hammer_mount_t hmp, int *errorp);
636 int     hammer_dowrite(hammer_transaction_t trans, hammer_inode_t ip,
637                         struct bio *bio);
638
639 hammer_volume_t hammer_get_volume(hammer_mount_t hmp,
640                         int32_t vol_no, int *errorp);
641 hammer_buffer_t hammer_get_buffer(hammer_mount_t hmp,
642                         hammer_off_t buf_offset, int isnew, int *errorp);
643 void    hammer_uncache_buffer(struct hammer_mount *hmp, hammer_off_t off);
644
645 int             hammer_ref_volume(hammer_volume_t volume);
646 int             hammer_ref_buffer(hammer_buffer_t buffer);
647 void            hammer_flush_buffer_nodes(hammer_buffer_t buffer);
648
649 void            hammer_rel_volume(hammer_volume_t volume, int flush);
650 void            hammer_rel_buffer(hammer_buffer_t buffer, int flush);
651
652 int             hammer_vfs_export(struct mount *mp, int op,
653                         const struct export_args *export);
654 hammer_node_t   hammer_get_node(hammer_mount_t hmp,
655                         hammer_off_t node_offset, int *errorp);
656 void            hammer_ref_node(hammer_node_t node);
657 hammer_node_t   hammer_ref_node_safe(struct hammer_mount *hmp,
658                         struct hammer_node **cache, int *errorp);
659 void            hammer_rel_node(hammer_node_t node);
660 void            hammer_delete_node(hammer_transaction_t trans,
661                         hammer_node_t node);
662 void            hammer_cache_node(hammer_node_t node,
663                         struct hammer_node **cache);
664 void            hammer_uncache_node(struct hammer_node **cache);
665 void            hammer_flush_node(hammer_node_t node);
666
667 void hammer_dup_buffer(struct hammer_buffer **bufferp,
668                         struct hammer_buffer *buffer);
669 hammer_node_t hammer_alloc_btree(hammer_transaction_t trans, int *errorp);
670 void *hammer_alloc_record(hammer_transaction_t trans,
671                         hammer_off_t *rec_offp, u_int16_t rec_type,
672                         struct hammer_buffer **rec_bufferp,
673                         int32_t data_len, void **datap,
674                         struct hammer_buffer **data_bufferp, int *errorp);
675 void *hammer_alloc_data(hammer_transaction_t trans, int32_t data_len,
676                         hammer_off_t *data_offsetp,
677                         struct hammer_buffer **data_bufferp, int *errorp);
678
679 int hammer_generate_undo(hammer_transaction_t trans, hammer_io_t io,
680                         hammer_off_t zone1_offset, void *base, int len);
681
682 void hammer_put_volume(struct hammer_volume *volume, int flush);
683 void hammer_put_buffer(struct hammer_buffer *buffer, int flush);
684
685 hammer_off_t hammer_freemap_alloc(hammer_transaction_t trans,
686                         hammer_off_t owner, int *errorp);
687 void hammer_freemap_free(hammer_transaction_t trans, hammer_off_t phys_offset,
688                         hammer_off_t owner, int *errorp);
689 hammer_off_t hammer_blockmap_alloc(hammer_transaction_t trans, int zone,
690                         int bytes, int *errorp);
691 void hammer_blockmap_free(hammer_transaction_t trans,
692                         hammer_off_t bmap_off, int bytes);
693 int hammer_blockmap_getfree(hammer_mount_t hmp, hammer_off_t bmap_off,
694                         int *curp, int *errorp);
695 hammer_off_t hammer_blockmap_lookup(hammer_mount_t hmp, hammer_off_t bmap_off,
696                         int *errorp);
697 hammer_off_t hammer_undo_lookup(hammer_mount_t hmp, hammer_off_t bmap_off,
698                         int *errorp);
699
700 void hammer_start_transaction(struct hammer_transaction *trans,
701                               struct hammer_mount *hmp);
702 void hammer_simple_transaction(struct hammer_transaction *trans,
703                               struct hammer_mount *hmp);
704 void hammer_start_transaction_fls(struct hammer_transaction *trans,
705                                   struct hammer_mount *hmp);
706 void hammer_done_transaction(struct hammer_transaction *trans);
707
708 void hammer_modify_inode(struct hammer_transaction *trans,
709                         hammer_inode_t ip, int flags);
710 void hammer_flush_inode(hammer_inode_t ip, int flags);
711 void hammer_flush_inode_done(hammer_inode_t ip);
712 void hammer_wait_inode(hammer_inode_t ip);
713
714 int  hammer_create_inode(struct hammer_transaction *trans, struct vattr *vap,
715                         struct ucred *cred, struct hammer_inode *dip,
716                         struct hammer_inode **ipp);
717 void hammer_rel_inode(hammer_inode_t ip, int flush);
718 int hammer_sync_inode(hammer_inode_t ip, int handle_delete);
719
720 int  hammer_ip_add_directory(struct hammer_transaction *trans,
721                         hammer_inode_t dip, struct namecache *ncp,
722                         hammer_inode_t nip);
723 int  hammer_ip_del_directory(struct hammer_transaction *trans,
724                         hammer_cursor_t cursor, hammer_inode_t dip,
725                         hammer_inode_t ip);
726 int  hammer_ip_add_record(struct hammer_transaction *trans,
727                         hammer_record_t record);
728 int  hammer_ip_delete_range(struct hammer_transaction *trans,
729                         hammer_inode_t ip, int64_t ran_beg, int64_t ran_end);
730 int  hammer_ip_delete_range_all(struct hammer_transaction *trans,
731                         hammer_inode_t ip);
732 int  hammer_ip_sync_data(struct hammer_transaction *trans,
733                         hammer_inode_t ip, int64_t offset,
734                         void *data, int bytes);
735 int  hammer_ip_sync_record(hammer_transaction_t trans, hammer_record_t rec);
736
737 int hammer_ioctl(hammer_inode_t ip, u_long com, caddr_t data, int fflag,
738                         struct ucred *cred);
739
740 void hammer_io_init(hammer_io_t io, hammer_mount_t hmp,
741                         enum hammer_io_type type);
742 void hammer_io_reinit(hammer_io_t io, enum hammer_io_type type);
743 int hammer_io_read(struct vnode *devvp, struct hammer_io *io);
744 int hammer_io_new(struct vnode *devvp, struct hammer_io *io);
745 void hammer_io_release(struct hammer_io *io);
746 void hammer_io_flush(struct hammer_io *io);
747 int hammer_io_checkflush(hammer_io_t io);
748 void hammer_io_clear_modify(struct hammer_io *io);
749 void hammer_io_waitdep(struct hammer_io *io);
750
751 void hammer_modify_volume(hammer_transaction_t trans, hammer_volume_t volume,
752                         void *base, int len);
753 void hammer_modify_buffer(hammer_transaction_t trans, hammer_buffer_t buffer,
754                         void *base, int len);
755 void hammer_modify_volume_done(hammer_volume_t volume);
756 void hammer_modify_buffer_done(hammer_buffer_t buffer);
757
758 int hammer_ioc_reblock(hammer_transaction_t trans, hammer_inode_t ip,
759                         struct hammer_ioc_reblock *reblock);
760
761 void hammer_init_holes(hammer_mount_t hmp, hammer_holes_t holes);
762 void hammer_free_holes(hammer_mount_t hmp, hammer_holes_t holes);
763 int hammer_signal_check(hammer_mount_t hmp);
764
765 void hammer_flusher_create(hammer_mount_t hmp);
766 void hammer_flusher_destroy(hammer_mount_t hmp);
767 void hammer_flusher_sync(hammer_mount_t hmp);
768 void hammer_flusher_async(hammer_mount_t hmp);
769
770 int hammer_recover(hammer_mount_t hmp, hammer_volume_t rootvol);
771
772 #endif
773
774 static __inline void
775 hammer_modify_node_noundo(hammer_transaction_t trans, hammer_node_t node)
776 {
777         hammer_modify_buffer(trans, node->buffer, NULL, 0);
778 }
779
780 static __inline void
781 hammer_modify_node_all(hammer_transaction_t trans, struct hammer_node *node)
782 {
783         hammer_modify_buffer(trans, node->buffer,
784                              node->ondisk, sizeof(*node->ondisk));
785 }
786
787 static __inline void
788 hammer_modify_node(hammer_transaction_t trans, hammer_node_t node,
789                    void *base, int len)
790 {
791         KKASSERT((char *)base >= (char *)node->ondisk &&
792                  (char *)base + len <=
793                     (char *)node->ondisk + sizeof(*node->ondisk));
794         hammer_modify_buffer(trans, node->buffer, base, len);
795 }
796
797 static __inline void
798 hammer_modify_node_done(hammer_node_t node)
799 {
800         hammer_modify_buffer_done(node->buffer);
801 }
802
803 static __inline void
804 hammer_modify_record(hammer_transaction_t trans, hammer_buffer_t buffer,
805                      void *base, int len)
806 {
807         KKASSERT((char *)base >= (char *)buffer->ondisk &&
808                  (char *)base + len <= (char *)buffer->ondisk + HAMMER_BUFSIZE);
809         hammer_modify_buffer(trans, buffer, base, len);
810 }
811
812 static __inline void
813 hammer_modify_record_done(hammer_buffer_t buffer)
814 {
815         hammer_modify_buffer_done(buffer);
816 }
817