HAMMER 26/many: Misc features.
[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.34 2008/02/06 08:59:28 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 "hammer_alist.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 /*
80  * HAMMER Transaction tracking
81  */
82 struct hammer_transaction {
83         struct hammer_mount *hmp;
84         hammer_tid_t    tid;
85         struct hammer_volume *rootvol;
86 /*      TAILQ_HEAD(, hammer_io) recycle_list;*/
87 };
88
89 typedef struct hammer_transaction *hammer_transaction_t;
90
91 /*
92  * HAMMER locks
93  */
94 struct hammer_lock {
95         int     refs;           /* active references delay writes */
96         int     lockcount;      /* lock count for exclusive/shared access */
97         int     wanted;
98         struct thread *locktd;
99 };
100
101 static __inline int
102 hammer_islocked(struct hammer_lock *lock)
103 {
104         return(lock->lockcount != 0);
105 }
106
107 static __inline int
108 hammer_isactive(struct hammer_lock *lock)
109 {
110         return(lock->refs != 0);
111 }
112
113 static __inline int
114 hammer_islastref(struct hammer_lock *lock)
115 {
116         return(lock->refs == 1);
117 }
118
119 /*
120  * This inline is specifically optimized for the case where the caller
121  * owns the lock, but wants to know what kind of lock he owns.  A
122  * negative value indicates a shared lock, a positive value indicates
123  * an exclusive lock.
124  */
125 static __inline int
126 hammer_lock_held(struct hammer_lock *lock)
127 {
128         return(lock->lockcount);
129 }
130
131 /*
132  * Structure used to represent an inode in-memory.
133  *
134  * The record and data associated with an inode may be out of sync with
135  * the disk (xDIRTY flags), or not even on the disk at all (ONDISK flag
136  * clear).
137  *
138  * An inode may also hold a cache of unsynchronized records, used for
139  * database and directories only.  Unsynchronized regular file data is
140  * stored in the buffer cache.
141  *
142  * NOTE: A file which is created and destroyed within the initial
143  * synchronization period can wind up not doing any disk I/O at all.
144  *
145  * Finally, an inode may cache numerous disk-referencing B-Tree cursors.
146  */
147 struct hammer_ino_rb_tree;
148 struct hammer_inode;
149 RB_HEAD(hammer_ino_rb_tree, hammer_inode);
150 RB_PROTOTYPEX(hammer_ino_rb_tree, INFO, hammer_inode, rb_node,
151               hammer_ino_rb_compare, hammer_inode_info_t);
152
153 struct hammer_rec_rb_tree;
154 struct hammer_record;
155 RB_HEAD(hammer_rec_rb_tree, hammer_record);
156 RB_PROTOTYPEX(hammer_rec_rb_tree, INFO, hammer_record, rb_node,
157               hammer_rec_rb_compare, hammer_base_elm_t);
158
159 TAILQ_HEAD(hammer_node_list, hammer_node);
160
161 struct hammer_inode {
162         RB_ENTRY(hammer_inode) rb_node;
163         u_int64_t       obj_id;         /* (key) object identifier */
164         hammer_tid_t    obj_asof;       /* (key) snapshot transid or 0 */
165         hammer_tid_t    last_tid;       /* last modified tid (for fsync) */
166         struct hammer_mount *hmp;
167         int             flags;
168         struct vnode    *vp;
169         struct lockf    advlock;
170         struct hammer_lock lock;
171         struct hammer_inode_record ino_rec;
172         struct hammer_inode_data ino_data;
173         struct hammer_rec_rb_tree rec_tree;     /* red-black record tree */
174         struct hammer_node      *cache[2];      /* search initiate cache */
175 };
176
177 typedef struct hammer_inode *hammer_inode_t;
178
179 #define VTOI(vp)        ((struct hammer_inode *)(vp)->v_data)
180
181 #define HAMMER_INODE_DDIRTY     0x0001  /* in-memory ino_data is dirty */
182 #define HAMMER_INODE_RDIRTY     0x0002  /* in-memory ino_rec is dirty */
183 #define HAMMER_INODE_ITIMES     0x0004  /* in-memory mtime/atime modified */
184 #define HAMMER_INODE_XDIRTY     0x0008  /* in-memory records present */
185 #define HAMMER_INODE_ONDISK     0x0010  /* inode is on-disk (else not yet) */
186 #define HAMMER_INODE_FLUSH      0x0020  /* flush on last ref */
187 #define HAMMER_INODE_DELETED    0x0080  /* inode ready for deletion */
188 #define HAMMER_INODE_DELONDISK  0x0100  /* delete synchronized to disk */
189 #define HAMMER_INODE_RO         0x0200  /* read-only (because of as-of) */
190 #define HAMMER_INODE_GONE       0x0400  /* delete flushed out */
191 #define HAMMER_INODE_DONDISK    0x0800  /* data records may be on disk */
192 #define HAMMER_INODE_BUFS       0x1000  /* dirty high level bps present */
193 #define HAMMER_INODE_TIDLOCKED  0x2000  /* tid locked until inode synced */
194
195 #define HAMMER_INODE_MODMASK    (HAMMER_INODE_DDIRTY|HAMMER_INODE_RDIRTY| \
196                                  HAMMER_INODE_XDIRTY|HAMMER_INODE_BUFS|   \
197                                  HAMMER_INODE_ITIMES|HAMMER_INODE_DELETED)
198
199 #define HAMMER_MAX_INODE_CURSORS        4
200
201 /*
202  * Structure used to represent an unsynchronized record in-memory.  This
203  * structure is orgranized in a per-inode RB-tree.  If the inode is not
204  * on disk then neither are any records and the in-memory record tree
205  * represents the entire contents of the inode.  If the inode is on disk
206  * then the on-disk B-Tree is scanned in parallel with the in-memory
207  * RB-Tree to synthesize the current state of the file.
208  *
209  * Only current (delete_tid == 0) unsynchronized records are kept in-memory.
210  *
211  * blocked is the count of the number of cursors (ip_first/ip_next) blocked
212  * on the record waiting for a synchronization to complete.
213  */
214 struct hammer_record {
215         RB_ENTRY(hammer_record)         rb_node;
216         struct hammer_lock              lock;
217         struct hammer_inode             *ip;
218         union hammer_record_ondisk      rec;
219         union hammer_data_ondisk        *data;
220         int                             flags;
221         int                             blocked;
222 };
223
224 typedef struct hammer_record *hammer_record_t;
225
226 #define HAMMER_RECF_ALLOCDATA           0x0001
227 #define HAMMER_RECF_ONRBTREE            0x0002
228 #define HAMMER_RECF_DELETED             0x0004
229 #define HAMMER_RECF_EMBEDDED_DATA       0x0008
230 #define HAMMER_RECF_SYNCING             0x0010
231 #define HAMMER_RECF_WANTED              0x0020
232
233 /*
234  * Structures used to internally represent a volume and a cluster
235  */
236 struct hammer_volume;
237 struct hammer_cluster;
238 struct hammer_supercl;
239 struct hammer_buffer;
240 struct hammer_node;
241 RB_HEAD(hammer_vol_rb_tree, hammer_volume);
242 RB_HEAD(hammer_clu_rb_tree, hammer_cluster);
243 RB_HEAD(hammer_scl_rb_tree, hammer_supercl);
244 RB_HEAD(hammer_buf_rb_tree, hammer_buffer);
245 RB_HEAD(hammer_nod_rb_tree, hammer_node);
246
247 RB_PROTOTYPE2(hammer_vol_rb_tree, hammer_volume, rb_node,
248               hammer_vol_rb_compare, int32_t);
249 RB_PROTOTYPE2(hammer_clu_rb_tree, hammer_cluster, rb_node,
250               hammer_clu_rb_compare, int32_t);
251 RB_PROTOTYPE2(hammer_scl_rb_tree, hammer_supercl, rb_node,
252               hammer_scl_rb_compare, int32_t);
253 RB_PROTOTYPE2(hammer_buf_rb_tree, hammer_buffer, rb_node,
254               hammer_buf_rb_compare, int32_t);
255 RB_PROTOTYPE2(hammer_nod_rb_tree, hammer_node, rb_node,
256               hammer_nod_rb_compare, int32_t);
257
258 /*
259  * IO management - embedded at the head of various in-memory structures
260  */
261 enum hammer_io_type { HAMMER_STRUCTURE_VOLUME,
262                       HAMMER_STRUCTURE_SUPERCL,
263                       HAMMER_STRUCTURE_CLUSTER,
264                       HAMMER_STRUCTURE_BUFFER };
265
266 union hammer_io_structure;
267 struct hammer_io;
268
269 struct worklist {
270         LIST_ENTRY(worklist) node;
271 };
272
273 TAILQ_HEAD(hammer_io_list, hammer_io);
274
275 struct hammer_io {
276         struct worklist worklist;
277         struct hammer_lock lock;
278         enum hammer_io_type type;
279         struct buf      *bp;
280         int64_t         offset;
281         TAILQ_ENTRY(hammer_io) entry;   /* based on modified flag */
282         struct hammer_io_list  *entry_list;
283         struct hammer_io_list  deplist;
284         u_int           modified : 1;   /* bp's data was modified */
285         u_int           released : 1;   /* bp released (w/ B_LOCKED set) */
286         u_int           running : 1;    /* bp write IO in progress */
287         u_int           waiting : 1;    /* someone is waiting on us */
288         u_int           loading : 1;    /* ondisk is loading */
289         u_int           validated : 1;  /* ondisk has been validated */
290 };
291
292 typedef struct hammer_io *hammer_io_t;
293
294 /*
295  * In-memory volume representing on-disk buffer
296  */
297 struct hammer_volume {
298         struct hammer_io io;
299         RB_ENTRY(hammer_volume) rb_node;
300         struct hammer_clu_rb_tree rb_clus_root;
301         struct hammer_scl_rb_tree rb_scls_root;
302         struct hammer_volume_ondisk *ondisk;
303         struct hammer_alist_live alist;
304         int32_t vol_no;
305         int32_t vol_clsize;
306         int32_t clu_iterator;   /* cluster allocation iterator */
307         int64_t nblocks;        /* note: special calculation for statfs */
308         int64_t cluster_base;   /* base offset of cluster 0 */
309         char    *vol_name;
310         struct vnode *devvp;
311         struct hammer_mount *hmp;
312         int     vol_flags;
313 };
314
315 typedef struct hammer_volume *hammer_volume_t;
316
317 /*
318  * In-memory super-cluster representing on-disk buffer
319  */
320 struct hammer_supercl {
321         struct hammer_io io;
322         RB_ENTRY(hammer_supercl) rb_node;
323         struct hammer_supercl_ondisk *ondisk;
324         struct hammer_volume *volume;
325         struct hammer_alist_live alist;
326         int32_t scl_no;
327 };
328
329 typedef struct hammer_supercl *hammer_supercl_t;
330
331 /*
332  * In-memory cluster representing on-disk buffer
333  *
334  * The cluster's indexing range is cached in hammer_cluster, separate
335  * from the ondisk info in order to allow cursors to point to it.
336  */
337 struct hammer_cluster {
338         struct hammer_io io;
339         RB_ENTRY(hammer_cluster) rb_node;
340         struct hammer_buf_rb_tree rb_bufs_root;
341         struct hammer_cluster_ondisk *ondisk;
342         struct hammer_volume *volume;
343         struct hammer_alist_live alist_master;
344         struct hammer_alist_live alist_btree;
345         struct hammer_alist_live alist_record;
346         struct hammer_alist_live alist_mdata;
347         struct hammer_nod_rb_tree rb_nods_root; /* cursors in cluster */
348         struct hammer_base_elm clu_btree_beg;   /* copy of on-disk info */
349         struct hammer_base_elm clu_btree_end;   /* copy of on-disk info */
350         int     flags;
351         int32_t clu_no;
352 };
353
354 typedef struct hammer_cluster *hammer_cluster_t;
355
356 #define HAMMER_CLUSTER_DELETED  0x0001
357 #define HAMMER_CLUSTER_EMPTY    0x0002
358
359 /*
360  * Passed to hammer_get_cluster()
361  */
362 #define GET_CLUSTER_NEW         0x0001
363 #define GET_CLUSTER_NORECOVER   0x0002
364
365 /*
366  * In-memory buffer (other then volume, super-cluster, or cluster),
367  * representing an on-disk buffer.
368  */
369 struct hammer_buffer {
370         struct hammer_io io;
371         RB_ENTRY(hammer_buffer) rb_node;
372         hammer_fsbuf_ondisk_t ondisk;
373         struct hammer_volume *volume;
374         struct hammer_cluster *cluster;
375         int32_t buf_no;
376         u_int64_t buf_type;
377         struct hammer_alist_live alist;
378         struct hammer_node_list clist;
379 };
380
381 typedef struct hammer_buffer *hammer_buffer_t;
382
383 /*
384  * In-memory B-Tree node, representing an on-disk B-Tree node.
385  *
386  * This is a hang-on structure which is backed by a hammer_buffer,
387  * indexed by a hammer_cluster, and used for fine-grained locking of
388  * B-Tree nodes in order to properly control lock ordering.  A hammer_buffer
389  * can contain multiple nodes representing wildly disassociated portions
390  * of the B-Tree so locking cannot be done on a buffer-by-buffer basis.
391  *
392  * This structure uses a cluster-relative index to reduce the number
393  * of layers required to access it, and also because all on-disk B-Tree
394  * references are cluster-relative offsets.
395  */
396 struct hammer_node {
397         struct hammer_lock      lock;           /* node-by-node lock */
398         TAILQ_ENTRY(hammer_node) entry;         /* per-buffer linkage */
399         RB_ENTRY(hammer_node)   rb_node;        /* per-cluster linkage */
400         int32_t                 node_offset;    /* cluster-rel offset */
401         struct hammer_cluster   *cluster;
402         struct hammer_buffer    *buffer;        /* backing buffer */
403         hammer_node_ondisk_t    ondisk;         /* ptr to on-disk structure */
404         struct hammer_node      **cache1;       /* passive cache(s) */
405         struct hammer_node      **cache2;
406         int                     flags;
407 };
408
409 #define HAMMER_NODE_DELETED     0x0001
410 #define HAMMER_NODE_FLUSH       0x0002
411
412 typedef struct hammer_node      *hammer_node_t;
413
414 /*
415  * List of locked nodes.
416  */
417 struct hammer_node_locklist {
418         struct hammer_node_locklist *next;
419         hammer_node_t   node;
420 };
421
422 typedef struct hammer_node_locklist *hammer_node_locklist_t;
423
424
425 /*
426  * Common I/O management structure - embedded in in-memory structures
427  * which are backed by filesystem buffers.
428  */
429 union hammer_io_structure {
430         struct hammer_io        io;
431         struct hammer_volume    volume;
432         struct hammer_supercl   supercl;
433         struct hammer_cluster   cluster;
434         struct hammer_buffer    buffer;
435 };
436
437 typedef union hammer_io_structure *hammer_io_structure_t;
438
439 #define HAMFS_CLUSTER_DIRTY     0x0001
440
441 #include "hammer_cursor.h"
442
443 /*
444  * Internal hammer mount data structure
445  */
446 struct hammer_mount {
447         struct mount *mp;
448         /*struct vnode *rootvp;*/
449         struct hammer_ino_rb_tree rb_inos_root;
450         struct hammer_vol_rb_tree rb_vols_root;
451         struct hammer_volume *rootvol;
452         struct hammer_cluster *rootcl;
453         char    *zbuf;  /* HAMMER_BUFSIZE bytes worth of all-zeros */
454         int     hflags;
455         int     ronly;
456         int     nvolumes;
457         int     volume_iterator;
458         uuid_t  fsid;
459         udev_t  fsid_udev;
460         hammer_tid_t asof;
461         u_int32_t namekey_iterator;
462         struct netexport export;
463 };
464
465 typedef struct hammer_mount     *hammer_mount_t;
466
467 struct hammer_sync_info {
468         int error;
469         int waitfor;
470 };
471
472 #endif
473
474 #if defined(_KERNEL)
475
476 extern struct vop_ops hammer_vnode_vops;
477 extern struct vop_ops hammer_spec_vops;
478 extern struct vop_ops hammer_fifo_vops;
479 extern struct hammer_alist_config Buf_alist_config;
480 extern struct hammer_alist_config Vol_normal_alist_config;
481 extern struct hammer_alist_config Vol_super_alist_config;
482 extern struct hammer_alist_config Supercl_alist_config;
483 extern struct hammer_alist_config Clu_master_alist_config;
484 extern struct hammer_alist_config Clu_slave_alist_config;
485 extern struct bio_ops hammer_bioops;
486
487 extern int hammer_debug_general;
488 extern int hammer_debug_btree;
489 extern int hammer_debug_tid;
490 extern int hammer_debug_recover;
491 extern int hammer_debug_recover_faults;
492 extern int hammer_count_inodes;
493 extern int hammer_count_records;
494 extern int hammer_count_record_datas;
495 extern int hammer_count_volumes;
496 extern int hammer_count_supercls;
497 extern int hammer_count_clusters;
498 extern int hammer_count_buffers;
499 extern int hammer_count_nodes;
500 extern int hammer_count_spikes;
501
502 int     hammer_vop_inactive(struct vop_inactive_args *);
503 int     hammer_vop_reclaim(struct vop_reclaim_args *);
504 int     hammer_get_vnode(struct hammer_inode *ip, int lktype,
505                         struct vnode **vpp);
506 struct hammer_inode *hammer_get_inode(hammer_mount_t hmp,
507                         struct hammer_node **cache,
508                         u_int64_t obj_id, hammer_tid_t asof, int flags,
509                         int *errorp);
510 void    hammer_put_inode(struct hammer_inode *ip);
511 void    hammer_put_inode_ref(struct hammer_inode *ip);
512
513 int     hammer_unload_inode(hammer_inode_t ip, void *data);
514 int     hammer_unload_volume(hammer_volume_t volume, void *data __unused);
515 int     hammer_unload_supercl(hammer_supercl_t supercl, void *data __unused);
516 int     hammer_unload_cluster(hammer_cluster_t cluster, void *data __unused);
517 int     hammer_unload_buffer(hammer_buffer_t buffer, void *data __unused);
518 int     hammer_install_volume(hammer_mount_t hmp, const char *volname);
519
520 int     hammer_ip_lookup(hammer_cursor_t cursor, hammer_inode_t ip);
521 int     hammer_ip_first(hammer_cursor_t cursor, hammer_inode_t ip);
522 int     hammer_ip_next(hammer_cursor_t cursor);
523 int     hammer_ip_resolve_data(hammer_cursor_t cursor);
524 int     hammer_ip_delete_record(hammer_cursor_t cursor, hammer_tid_t tid);
525 int     hammer_delete_at_cursor(hammer_cursor_t cursor, int64_t *stat_bytes);
526 int     hammer_ip_check_directory_empty(hammer_transaction_t trans,
527                         hammer_inode_t ip);
528 int     hammer_sync_hmp(hammer_mount_t hmp, int waitfor);
529 int     hammer_sync_volume(hammer_volume_t volume, void *data);
530 int     hammer_sync_cluster(hammer_cluster_t cluster, void *data);
531 int     hammer_sync_buffer(hammer_buffer_t buffer, void *data);
532
533 hammer_record_t
534         hammer_alloc_mem_record(hammer_inode_t ip);
535 void    hammer_rel_mem_record(hammer_record_t record);
536
537 int     hammer_cursor_up(hammer_cursor_t cursor);
538 int     hammer_cursor_toroot(hammer_cursor_t cursor);
539 int     hammer_cursor_down(hammer_cursor_t cursor);
540 int     hammer_cursor_upgrade(hammer_cursor_t cursor);
541 void    hammer_cursor_downgrade(hammer_cursor_t cursor);
542 int     hammer_cursor_seek(hammer_cursor_t cursor, hammer_node_t node,
543                         int index);
544 void    hammer_lock_ex(struct hammer_lock *lock);
545 int     hammer_lock_ex_try(struct hammer_lock *lock);
546 void    hammer_lock_sh(struct hammer_lock *lock);
547 int     hammer_lock_upgrade(struct hammer_lock *lock);
548 void    hammer_lock_downgrade(struct hammer_lock *lock);
549 void    hammer_unlock(struct hammer_lock *lock);
550 void    hammer_ref(struct hammer_lock *lock);
551 void    hammer_unref(struct hammer_lock *lock);
552
553 u_int32_t hammer_to_unix_xid(uuid_t *uuid);
554 void hammer_guid_to_uuid(uuid_t *uuid, u_int32_t guid);
555 void    hammer_to_timespec(hammer_tid_t tid, struct timespec *ts);
556 hammer_tid_t hammer_timespec_to_transid(struct timespec *ts);
557 hammer_tid_t hammer_alloc_tid(hammer_transaction_t trans);
558 hammer_tid_t hammer_now_tid(void);
559 hammer_tid_t hammer_str_to_tid(const char *str);
560 u_int64_t hammer_alloc_recid(hammer_cluster_t cluster);
561
562 enum vtype hammer_get_vnode_type(u_int8_t obj_type);
563 int hammer_get_dtype(u_int8_t obj_type);
564 u_int8_t hammer_get_obj_type(enum vtype vtype);
565 int64_t hammer_directory_namekey(void *name, int len);
566
567 int     hammer_init_cursor_hmp(hammer_cursor_t cursor, struct hammer_node **cache, hammer_mount_t hmp);
568 int     hammer_init_cursor_cluster(hammer_cursor_t cursor, hammer_cluster_t cluster);
569
570 void    hammer_done_cursor(hammer_cursor_t cursor);
571 void    hammer_mem_done(hammer_cursor_t cursor);
572
573 int     hammer_btree_lookup(hammer_cursor_t cursor);
574 int     hammer_btree_first(hammer_cursor_t cursor);
575 int     hammer_btree_last(hammer_cursor_t cursor);
576 int     hammer_btree_extract(hammer_cursor_t cursor, int flags);
577 int     hammer_btree_iterate(hammer_cursor_t cursor);
578 int     hammer_btree_iterate_reverse(hammer_cursor_t cursor);
579 int     hammer_btree_insert(hammer_cursor_t cursor, hammer_btree_elm_t elm);
580 int     hammer_btree_insert_cluster(hammer_cursor_t cursor,
581                         hammer_cluster_t cluster, int32_t rec_offset);
582 int     hammer_btree_delete(hammer_cursor_t cursor);
583 int     hammer_btree_cmp(hammer_base_elm_t key1, hammer_base_elm_t key2);
584 int     hammer_btree_chkts(hammer_tid_t ts, hammer_base_elm_t key);
585 int     hammer_btree_correct_rhb(hammer_cursor_t cursor, hammer_tid_t tid);
586 int     hammer_btree_correct_lhb(hammer_cursor_t cursor, hammer_tid_t tid);
587
588
589 int     hammer_btree_lock_children(hammer_cursor_t cursor,
590                         struct hammer_node_locklist **locklistp);
591 void    hammer_btree_unlock_children(struct hammer_node_locklist **locklistp);
592
593 void    hammer_print_btree_node(hammer_node_ondisk_t ondisk);
594 void    hammer_print_btree_elm(hammer_btree_elm_t elm, u_int8_t type, int i);
595
596 void    *hammer_bread(struct hammer_cluster *cluster, int32_t cloff,
597                         u_int64_t buf_type, int *errorp,
598                         struct hammer_buffer **bufferp);
599
600 hammer_volume_t hammer_get_root_volume(hammer_mount_t hmp, int *errorp);
601 hammer_cluster_t hammer_get_root_cluster(hammer_mount_t hmp, int *errorp);
602
603 hammer_volume_t hammer_get_volume(hammer_mount_t hmp,
604                         int32_t vol_no, int *errorp);
605 hammer_supercl_t hammer_get_supercl(hammer_volume_t volume, int32_t scl_no,
606                         int *errorp, hammer_alloc_state_t isnew);
607 hammer_cluster_t hammer_get_cluster(hammer_volume_t volume, int32_t clu_no,
608                         int *errorp, int getflags);
609 hammer_buffer_t hammer_get_buffer(hammer_cluster_t cluster,
610                         int32_t buf_no, u_int64_t buf_type, int *errorp);
611
612 int             hammer_ref_volume(hammer_volume_t volume);
613 int             hammer_ref_cluster(hammer_cluster_t cluster);
614 int             hammer_ref_buffer(hammer_buffer_t buffer);
615 void            hammer_flush_buffer_nodes(hammer_buffer_t buffer);
616
617 void            hammer_rel_volume(hammer_volume_t volume, int flush);
618 void            hammer_rel_supercl(hammer_supercl_t supercl, int flush);
619 void            hammer_rel_cluster(hammer_cluster_t cluster, int flush);
620 void            hammer_rel_buffer(hammer_buffer_t buffer, int flush);
621
622 int             hammer_vfs_export(struct mount *mp, int op,
623                         const struct export_args *export);
624 hammer_node_t   hammer_get_node(hammer_cluster_t cluster,
625                         int32_t node_offset, int *errorp);
626 int             hammer_ref_node(hammer_node_t node);
627 hammer_node_t   hammer_ref_node_safe(struct hammer_mount *hmp,
628                         struct hammer_node **cache, int *errorp);
629 void            hammer_rel_node(hammer_node_t node);
630 void            hammer_cache_node(hammer_node_t node,
631                         struct hammer_node **cache);
632 void            hammer_uncache_node(struct hammer_node **cache);
633 void            hammer_flush_node(hammer_node_t node);
634
635 void hammer_dup_buffer(struct hammer_buffer **bufferp,
636                         struct hammer_buffer *buffer);
637 void hammer_dup_cluster(struct hammer_cluster **clusterp,
638                         struct hammer_cluster *cluster);
639 hammer_cluster_t hammer_alloc_cluster(hammer_mount_t hmp,
640                         hammer_cluster_t cluster_hint, int *errorp);
641 void hammer_init_cluster(hammer_cluster_t cluster,
642                         hammer_base_elm_t left_bound,
643                         hammer_base_elm_t right_bound);
644 hammer_node_t hammer_alloc_btree(struct hammer_cluster *cluster, int *errorp);
645 void *hammer_alloc_data(struct hammer_cluster *cluster, int32_t bytes,
646                         int *errorp, struct hammer_buffer **bufferp);
647 void *hammer_alloc_record(struct hammer_cluster *cluster, int *errorp,
648                         u_int8_t rec_type, struct hammer_buffer **bufferp);
649 void hammer_initbuffer(hammer_alist_t live, hammer_fsbuf_head_t head,
650                         u_int64_t type);
651 void hammer_free_data_ptr(struct hammer_buffer *buffer, 
652                         void *data, int bytes);
653 void hammer_free_record_ptr(struct hammer_buffer *buffer,
654                         union hammer_record_ondisk *rec, u_int8_t rec_type);
655 void hammer_free_cluster(hammer_cluster_t cluster);
656 void hammer_free_btree(struct hammer_cluster *cluster, int32_t bclu_offset);
657 void hammer_free_data(struct hammer_cluster *cluster, int32_t bclu_offset,
658                         int32_t bytes);
659 void hammer_free_record(struct hammer_cluster *cluster, int32_t bclu_offset,
660                         u_int8_t rec_type);
661
662 void hammer_put_volume(struct hammer_volume *volume, int flush);
663 void hammer_put_supercl(struct hammer_supercl *supercl, int flush);
664 void hammer_put_cluster(struct hammer_cluster *cluster, int flush);
665 void hammer_put_buffer(struct hammer_buffer *buffer, int flush);
666
667 void hammer_init_alist_config(void);
668
669 void hammer_start_transaction(struct hammer_transaction *trans,
670                               struct hammer_mount *hmp);
671 void hammer_start_transaction_tid(struct hammer_transaction *trans,
672                                   struct hammer_mount *hmp, hammer_tid_t tid);
673 void hammer_commit_transaction(struct hammer_transaction *trans);
674 void hammer_abort_transaction(struct hammer_transaction *trans);
675
676 void hammer_modify_inode(struct hammer_transaction *trans,
677                         hammer_inode_t ip, int flags);
678 int  hammer_create_inode(struct hammer_transaction *trans, struct vattr *vap,
679                         struct ucred *cred, struct hammer_inode *dip,
680                         struct hammer_inode **ipp);
681 void hammer_rel_inode(hammer_inode_t ip, int flush);
682 int hammer_sync_inode(hammer_inode_t ip, int waitfor, int handle_delete);
683
684 int  hammer_ip_add_directory(struct hammer_transaction *trans,
685                         hammer_inode_t dip, struct namecache *ncp,
686                         hammer_inode_t nip);
687 int  hammer_ip_del_directory(struct hammer_transaction *trans,
688                         hammer_cursor_t cursor, hammer_inode_t dip,
689                         hammer_inode_t ip);
690 int  hammer_ip_add_record(struct hammer_transaction *trans,
691                         hammer_record_t record);
692 int  hammer_ip_delete_range(struct hammer_transaction *trans,
693                         hammer_inode_t ip, int64_t ran_beg, int64_t ran_end,
694                         struct hammer_cursor **spikep);
695 int  hammer_ip_delete_range_all(struct hammer_transaction *trans,
696                         hammer_inode_t ip);
697 int  hammer_ip_sync_data(struct hammer_transaction *trans,
698                         hammer_inode_t ip, int64_t offset,
699                         void *data, int bytes, struct hammer_cursor **spikep);
700 int  hammer_ip_sync_record(hammer_record_t rec, struct hammer_cursor **spikep);
701 int  hammer_write_record(hammer_cursor_t cursor, hammer_record_ondisk_t rec,
702                         void *data, int cursor_flags);
703
704 void hammer_load_spike(hammer_cursor_t cursor, struct hammer_cursor **spikep);
705 int hammer_spike(struct hammer_cursor **spikep);
706 int hammer_recover(struct hammer_cluster *cluster);
707 int buffer_alist_recover(void *info, int32_t blk, int32_t radix, int32_t count);
708
709 int hammer_ioctl(hammer_inode_t ip, u_long com, caddr_t data, int fflag,
710                         struct ucred *cred);
711
712 void hammer_io_init(hammer_io_t io, enum hammer_io_type type);
713 int hammer_io_read(struct vnode *devvp, struct hammer_io *io);
714 int hammer_io_new(struct vnode *devvp, struct hammer_io *io);
715 void hammer_io_release(struct hammer_io *io, int flush);
716 void hammer_io_flush(struct hammer_io *io);
717 int hammer_io_checkflush(hammer_io_t io);
718 void hammer_io_notify_cluster(hammer_cluster_t cluster);
719 void hammer_io_clear_modify(struct hammer_io *io);
720 void hammer_io_waitdep(struct hammer_io *io);
721
722 void hammer_modify_volume(hammer_volume_t volume);
723 void hammer_modify_supercl(hammer_supercl_t supercl);
724 void hammer_modify_cluster(hammer_cluster_t cluster);
725 void hammer_modify_buffer(hammer_buffer_t buffer);
726 void hammer_modify_buffer_nodep(hammer_buffer_t buffer);
727
728 #endif
729
730 static __inline void
731 hammer_modify_node(struct hammer_node *node)
732 {
733         hammer_modify_buffer(node->buffer);
734 }
735
736 /*
737  * Return the cluster-relative byte offset of an element within a buffer
738  */
739 static __inline int
740 hammer_bclu_offset(struct hammer_buffer *buffer, void *ptr)
741 {
742         int bclu_offset;
743
744         bclu_offset = buffer->buf_no * HAMMER_BUFSIZE + 
745                       ((char *)ptr - (char *)buffer->ondisk);
746         return(bclu_offset);
747 }
748