hammer2 - Rewrite internal chain algorithms
[dragonfly.git] / sys / vfs / hammer2 / hammer2.h
1 /*
2  * Copyright (c) 2011-2013 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@dragonflybsd.org>
6  * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  * 3. Neither the name of The DragonFly Project nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific, prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
26  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35
36 /*
37  * This header file contains structures used internally by the HAMMER2
38  * implementation.  See hammer2_disk.h for on-disk structures.
39  */
40
41 #ifndef _VFS_HAMMER2_HAMMER2_H_
42 #define _VFS_HAMMER2_HAMMER2_H_
43
44 #include <sys/param.h>
45 #include <sys/types.h>
46 #include <sys/kernel.h>
47 #include <sys/conf.h>
48 #include <sys/systm.h>
49 #include <sys/tree.h>
50 #include <sys/malloc.h>
51 #include <sys/mount.h>
52 #include <sys/vnode.h>
53 #include <sys/proc.h>
54 #include <sys/mountctl.h>
55 #include <sys/priv.h>
56 #include <sys/stat.h>
57 #include <sys/thread.h>
58 #include <sys/globaldata.h>
59 #include <sys/lockf.h>
60 #include <sys/buf.h>
61 #include <sys/queue.h>
62 #include <sys/limits.h>
63 #include <sys/buf2.h>
64 #include <sys/signal2.h>
65 #include <sys/dmsg.h>
66 #include <sys/mutex.h>
67 #include <sys/mutex2.h>
68
69 #include "hammer2_disk.h"
70 #include "hammer2_mount.h"
71 #include "hammer2_ioctl.h"
72 #include "hammer2_ccms.h"
73
74 struct hammer2_chain;
75 struct hammer2_inode;
76 struct hammer2_mount;
77 struct hammer2_pfsmount;
78 struct hammer2_span;
79 struct hammer2_state;
80 struct hammer2_msg;
81
82 /*
83  * The chain structure tracks a portion of the media topology from the
84  * root (volume) down.  Chains represent volumes, inodes, indirect blocks,
85  * data blocks, and freemap nodes and leafs.
86  *
87  * The chain structure can be multi-homed and its topological recursion
88  * (chain->core) can be shared amongst several chains.  Chain structures
89  * are topologically stable once placed in the in-memory topology (they
90  * don't move around).  Modifications which cross flush synchronization
91  * boundaries, renames, resizing, or any move of the chain to elsewhere
92  * in the topology is accomplished via the DELETE-DUPLICATE mechanism.
93  *
94  * DELETE-DUPLICATE allows HAMMER2 to track work across flush synchronization
95  * points without stalling the filesystem or corrupting the flush
96  * sychronization point.  When necessary a chain will be marked DELETED
97  * and a new, duplicate chain will be allocated.
98  *
99  * This mechanism necessarily requires that we be able to overload chains
100  * at any given layer in the topology.  Overloading is accomplished via a
101  * RBTREE recursion through chain->rbtree.
102  *
103  * Advantages:
104  *
105  *      (1) Fully coherent snapshots can be taken without requiring
106  *          a pre-flush, resulting in extremely fast (sub-millisecond)
107  *          snapshots.
108  *
109  *      (2) Multiple synchronization points can be in-flight at the same
110  *          time, representing multiple snapshots or flushes.
111  *
112  *      (3) The algorithms needed to keep track of everything are actually
113  *          not that complex.
114  *
115  * Special Considerations:
116  *
117  *      A chain is ref-counted on a per-chain basis, but the chain's lock
118  *      is associated with the shared chain_core and is not per-chain.
119  *
120  *      The power-of-2 nature of the media radix tree ensures that there
121  *      will be no overlaps which straddle edges.
122  */
123 RB_HEAD(hammer2_chain_tree, hammer2_chain);
124 TAILQ_HEAD(h2_flush_deferral_list, hammer2_chain);
125 TAILQ_HEAD(h2_core_list, hammer2_chain);
126 TAILQ_HEAD(h2_layer_list, hammer2_chain_layer);
127
128 struct hammer2_chain_layer {
129         TAILQ_ENTRY(hammer2_chain_layer) entry;
130         struct hammer2_chain_tree rbtree;
131         int     refs;           /* prevent destruction */
132 };
133
134 typedef struct hammer2_chain_layer hammer2_chain_layer_t;
135
136 struct hammer2_chain_core {
137         struct ccms_cst cst;
138         struct h2_core_list ownerq;     /* chain's which own this core */
139         struct h2_layer_list layerq;
140         u_int           chain_count;    /* total chains in layers */
141         u_int           sharecnt;
142         u_int           flags;
143         u_int           live_count;     /* live (not deleted) chains in tree */
144         u_int           live_zero;      /* first empty blockref (index) */
145 };
146
147 typedef struct hammer2_chain_core hammer2_chain_core_t;
148
149 #define HAMMER2_CORE_INDIRECT           0x0001
150 #define HAMMER2_CORE_COUNTEDBREFS       0x0002
151
152 struct hammer2_chain {
153         RB_ENTRY(hammer2_chain) rbnode;         /* node */
154         TAILQ_ENTRY(hammer2_chain) core_entry;  /* contemporary chains */
155         hammer2_chain_layer_t   *inlayer;
156         hammer2_blockref_t      bref;
157         hammer2_chain_core_t    *core;
158         hammer2_chain_core_t    *above;
159         struct hammer2_state    *state;         /* if active cache msg */
160         struct hammer2_mount    *hmp;
161         struct hammer2_pfsmount *pmp;           /* can be NULL */
162
163         hammer2_tid_t   modify_tid;             /* snapshot/flush filter */
164         hammer2_tid_t   delete_tid;
165         hammer2_key_t   data_count;             /* delta's to apply */
166         hammer2_key_t   inode_count;            /* delta's to apply */
167         struct buf      *bp;                    /* physical data buffer */
168         u_int           bytes;                  /* physical data size */
169         u_int           flags;
170         u_int           refs;
171         u_int           lockcnt;
172         hammer2_media_data_t *data;             /* data pointer shortcut */
173         TAILQ_ENTRY(hammer2_chain) flush_node;  /* flush deferral list */
174 };
175
176 typedef struct hammer2_chain hammer2_chain_t;
177
178 int hammer2_chain_cmp(hammer2_chain_t *chain1, hammer2_chain_t *chain2);
179 RB_PROTOTYPE(hammer2_chain_tree, hammer2_chain, rbnode, hammer2_chain_cmp);
180
181 /*
182  * Special notes on flags:
183  *
184  * INITIAL - This flag allows a chain to be created and for storage to
185  *           be allocated without having to immediately instantiate the
186  *           related buffer.  The data is assumed to be all-zeros.  It
187  *           is primarily used for indirect blocks.
188  *
189  * MOVED   - A modified chain becomes MOVED after it flushes.  A chain
190  *           can also become MOVED if it is moved within the topology
191  *           (even if not modified).
192  */
193 #define HAMMER2_CHAIN_MODIFIED          0x00000001      /* dirty chain data */
194 #define HAMMER2_CHAIN_ALLOCATED         0x00000002      /* kmalloc'd chain */
195 #define HAMMER2_CHAIN_DIRTYBP           0x00000004      /* dirty on unlock */
196 #define HAMMER2_CHAIN_SUBMODIFIED       0x00000008      /* recursive flush */
197 #define HAMMER2_CHAIN_DELETED           0x00000010      /* deleted chain */
198 #define HAMMER2_CHAIN_INITIAL           0x00000020      /* initial create */
199 #define HAMMER2_CHAIN_FLUSHED           0x00000040      /* flush on unlock */
200 #define HAMMER2_CHAIN_MOVED             0x00000080      /* bref changed */
201 #define HAMMER2_CHAIN_IOFLUSH           0x00000100      /* bawrite on put */
202 #define HAMMER2_CHAIN_DEFERRED          0x00000200      /* on a deferral list */
203 #define HAMMER2_CHAIN_DESTROYED         0x00000400      /* destroying inode */
204 #define HAMMER2_CHAIN_VOLUMESYNC        0x00000800      /* needs volume sync */
205 #define HAMMER2_CHAIN_RECYCLE           0x00001000      /* force recycle */
206 #define HAMMER2_CHAIN_MOUNTED           0x00002000      /* PFS is mounted */
207 #define HAMMER2_CHAIN_ONRBTREE          0x00004000      /* on parent RB tree */
208 #define HAMMER2_CHAIN_SNAPSHOT          0x00008000      /* snapshot special */
209 #define HAMMER2_CHAIN_EMBEDDED          0x00010000      /* embedded data */
210 #define HAMMER2_CHAIN_HARDLINK          0x00020000      /* converted to hlink */
211 #define HAMMER2_CHAIN_REPLACE           0x00040000      /* replace bref */
212
213 /*
214  * Flags passed to hammer2_chain_lookup() and hammer2_chain_next()
215  *
216  * NOTE: MATCHIND allows an indirect block / freemap node to be returned
217  *       when the passed key range matches the radix.  Remember that key_end
218  *       is inclusive (e.g. {0x000,0xFFF}, not {0x000,0x1000}).
219  */
220 #define HAMMER2_LOOKUP_NOLOCK           0x00000001      /* ref only */
221 #define HAMMER2_LOOKUP_NODATA           0x00000002      /* data left NULL */
222 #define HAMMER2_LOOKUP_SHARED           0x00000100
223 #define HAMMER2_LOOKUP_MATCHIND         0x00000200
224 #define HAMMER2_LOOKUP_FREEMAP          0x00000400      /* freemap base */
225 #define HAMMER2_LOOKUP_ALWAYS           0x00000800      /* resolve data */
226
227 /*
228  * Flags passed to hammer2_chain_modify() and hammer2_chain_resize()
229  *
230  * NOTE: OPTDATA allows us to avoid instantiating buffers for INDIRECT
231  *       blocks in the INITIAL-create state.
232  */
233 #define HAMMER2_MODIFY_OPTDATA          0x00000002      /* data can be NULL */
234 #define HAMMER2_MODIFY_NO_MODIFY_TID    0x00000004
235 #define HAMMER2_MODIFY_ASSERTNOCOPY     0x00000008
236 #define HAMMER2_MODIFY_NOREALLOC        0x00000010
237
238 /*
239  * Flags passed to hammer2_chain_lock()
240  */
241 #define HAMMER2_RESOLVE_NEVER           1
242 #define HAMMER2_RESOLVE_MAYBE           2
243 #define HAMMER2_RESOLVE_ALWAYS          3
244 #define HAMMER2_RESOLVE_MASK            0x0F
245
246 #define HAMMER2_RESOLVE_SHARED          0x10    /* request shared lock */
247 #define HAMMER2_RESOLVE_NOREF           0x20    /* already ref'd on lock */
248
249 /*
250  * Flags passed to hammer2_chain_delete()
251  */
252 #define HAMMER2_DELETE_WILLDUP          0x0001  /* no blk free, will be dup */
253
254 /*
255  * Flags passed to hammer2_chain_delete_duplicate()
256  */
257 #define HAMMER2_DELDUP_RECORE           0x0001
258
259 /*
260  * Cluster different types of storage together for allocations
261  */
262 #define HAMMER2_FREECACHE_INODE         0
263 #define HAMMER2_FREECACHE_INDIR         1
264 #define HAMMER2_FREECACHE_DATA          2
265 #define HAMMER2_FREECACHE_UNUSED3       3
266 #define HAMMER2_FREECACHE_TYPES         4
267
268 /*
269  * hammer2_freemap_alloc() block preference
270  */
271 #define HAMMER2_OFF_NOPREF              ((hammer2_off_t)-1)
272
273 /*
274  * BMAP read-ahead maximum parameters
275  */
276 #define HAMMER2_BMAP_COUNT              16      /* max bmap read-ahead */
277 #define HAMMER2_BMAP_BYTES              (HAMMER2_PBUFSIZE * HAMMER2_BMAP_COUNT)
278
279 /*
280  * Misc
281  */
282 #define HAMMER2_FLUSH_DEPTH_LIMIT       40      /* stack recursion limit */
283
284 /*
285  * HAMMER2 IN-MEMORY CACHE OF MEDIA STRUCTURES
286  *
287  * There is an in-memory representation of all on-media data structure.
288  *
289  * When accessed read-only the data will be mapped to the related buffer
290  * cache buffer.
291  *
292  * When accessed read-write (marked modified) a kmalloc()'d copy of the
293  * is created which can then be modified.  The copy is destroyed when a
294  * filesystem block is allocated to replace it.
295  *
296  * Active inodes (those with vnodes attached) will maintain the kmalloc()'d
297  * copy for both the read-only and the read-write case.  The combination of
298  * (bp) and (data) determines whether (data) was allocated or not.
299  *
300  * The in-memory representation may remain cached (for example in order to
301  * placemark clustering locks) even after the related data has been
302  * detached.
303  */
304
305 RB_HEAD(hammer2_inode_tree, hammer2_inode);
306
307 /*
308  * A hammer2 inode.
309  *
310  * NOTE: The inode's attribute CST which is also used to lock the inode
311  *       is embedded in the chain (chain.cst) and aliased w/ attr_cst.
312  */
313 struct hammer2_inode {
314         RB_ENTRY(hammer2_inode) rbnode;         /* inumber lookup (HL) */
315         ccms_cst_t              topo_cst;       /* directory topology cst */
316         struct hammer2_pfsmount *pmp;           /* PFS mount */
317         struct hammer2_inode    *pip;           /* parent inode */
318         struct vnode            *vp;
319         hammer2_chain_t         *chain;         /* NOTE: rehomed on rename */
320         struct lockf            advlock;
321         hammer2_tid_t           inum;
322         u_int                   flags;
323         u_int                   refs;           /* +vpref, +flushref */
324         uint8_t                 comp_heuristic;
325         hammer2_off_t           size;
326         uint64_t                mtime;
327 };
328
329 typedef struct hammer2_inode hammer2_inode_t;
330
331 #define HAMMER2_INODE_MODIFIED          0x0001
332 #define HAMMER2_INODE_SROOT             0x0002  /* kmalloc special case */
333 #define HAMMER2_INODE_RENAME_INPROG     0x0004
334 #define HAMMER2_INODE_ONRBTREE          0x0008
335 #define HAMMER2_INODE_RESIZED           0x0010
336 #define HAMMER2_INODE_MTIME             0x0020
337
338 int hammer2_inode_cmp(hammer2_inode_t *ip1, hammer2_inode_t *ip2);
339 RB_PROTOTYPE2(hammer2_inode_tree, hammer2_inode, rbnode, hammer2_inode_cmp,
340                 hammer2_tid_t);
341
342 /*
343  * A hammer2 transaction and flush sequencing structure.
344  *
345  * This global structure is tied into hammer2_mount and is used
346  * to sequence modifying operations and flushes.
347  *
348  * (a) Any modifying operations with sync_tid >= flush_tid will stall until
349  *     all modifying operating with sync_tid < flush_tid complete.
350  *
351  *     The flush related to flush_tid stalls until all modifying operations
352  *     with sync_tid < flush_tid complete.
353  *
354  * (b) Once unstalled, modifying operations with sync_tid > flush_tid are
355  *     allowed to run.  All modifications cause modify/duplicate operations
356  *     to occur on the related chains.  Note that most INDIRECT blocks will
357  *     be unaffected because the modifications just overload the RBTREE
358  *     structurally instead of actually modifying the indirect blocks.
359  *
360  * (c) The actual flush unstalls and RUNS CONCURRENTLY with (b), but only
361  *     utilizes the chain structures with sync_tid <= flush_tid.  The
362  *     flush will modify related indirect blocks and inodes in-place
363  *     (rather than duplicate) since the adjustments are compatible with
364  *     (b)'s RBTREE overloading
365  *
366  *     SPECIAL NOTE:  Inode modifications have to also propagate along any
367  *                    modify/duplicate chains.  File writes detect the flush
368  *                    and force out the conflicting buffer cache buffer(s)
369  *                    before reusing them.
370  *
371  * (d) Snapshots can be made instantly but must be flushed and disconnected
372  *     from their duplicative source before they can be mounted.  This is
373  *     because while H2's on-media structure supports forks, its in-memory
374  *     structure only supports very simple forking for background flushing
375  *     purposes.
376  *
377  * TODO: Flush merging.  When fsync() is called on multiple discrete files
378  *       concurrently there is no reason to stall the second fsync.
379  *       The final flush that reaches to root can cover both fsync()s.
380  *
381  *     The chains typically terminate as they fly onto the disk.  The flush
382  *     ultimately reaches the volume header.
383  */
384 struct hammer2_trans {
385         TAILQ_ENTRY(hammer2_trans) entry;
386         struct hammer2_pfsmount *pmp;
387         hammer2_tid_t           sync_tid;
388         thread_t                td;             /* pointer */
389         int                     flags;
390         int                     blocked;
391         uint8_t                 inodes_created;
392         uint8_t                 dummy[7];
393 };
394
395 typedef struct hammer2_trans hammer2_trans_t;
396
397 #define HAMMER2_TRANS_ISFLUSH           0x0001  /* formal flush */
398 #define HAMMER2_TRANS_RESTRICTED        0x0002  /* snapshot flush restrict */
399 #define HAMMER2_TRANS_BUFCACHE          0x0004  /* from bioq strategy write */
400
401 #define HAMMER2_FREEMAP_HEUR_NRADIX     4       /* pwr 2 PBUFRADIX-MINIORADIX */
402 #define HAMMER2_FREEMAP_HEUR_TYPES      8
403 #define HAMMER2_FREEMAP_HEUR            (HAMMER2_FREEMAP_HEUR_NRADIX * \
404                                          HAMMER2_FREEMAP_HEUR_TYPES)
405
406 /*
407  * Global (per device) mount structure for device (aka vp->v_mount->hmp)
408  */
409 TAILQ_HEAD(hammer2_trans_queue, hammer2_trans);
410
411 struct hammer2_mount {
412         struct vnode    *devvp;         /* device vnode */
413         int             ronly;          /* read-only mount */
414         int             pmp_count;      /* PFS mounts backed by us */
415         TAILQ_ENTRY(hammer2_mount) mntentry; /* hammer2_mntlist */
416
417         struct malloc_type *mchain;
418         int             nipstacks;
419         int             maxipstacks;
420         hammer2_chain_t vchain;         /* anchor chain */
421         hammer2_chain_t fchain;         /* freemap chain special */
422         hammer2_chain_t *schain;        /* super-root */
423         hammer2_inode_t *sroot;         /* super-root inode */
424         struct lock     alloclk;        /* lockmgr lock */
425         struct lock     voldatalk;      /* lockmgr lock */
426         struct hammer2_trans_queue transq; /* all in-progress transactions */
427         hammer2_trans_t *curflush;      /* current flush in progress */
428         hammer2_tid_t   topo_flush_tid; /* currently synchronizing flush pt */
429         hammer2_tid_t   free_flush_tid; /* currently synchronizing flush pt */
430         hammer2_off_t   heur_freemap[HAMMER2_FREEMAP_HEUR];
431         int             flushcnt;       /* #of flush trans on the list */
432
433         int             volhdrno;       /* last volhdrno written */
434         hammer2_volume_data_t voldata;
435         hammer2_volume_data_t volsync;  /* synchronized voldata */
436         struct bio_queue_head wthread_bioq; /* bio queue for write thread */
437         struct mtx wthread_mtx;     /* mutex for write thread */
438         int     wthread_destroy;    /* to control the write thread */
439 };
440
441 typedef struct hammer2_mount hammer2_mount_t;
442
443 /*
444  * HAMMER2 cluster - a device/root associated with a PFS.
445  *
446  * A PFS may have several hammer2_cluster's associated with it.
447  */
448 struct hammer2_cluster {
449         struct hammer2_mount    *hmp;           /* device global mount */
450         hammer2_chain_t         *rchain;        /* PFS root chain */
451 };
452
453 typedef struct hammer2_cluster hammer2_cluster_t;
454
455 /*
456  * HAMMER2 PFS mount point structure (aka vp->v_mount->mnt_data).
457  *
458  * This structure represents a cluster mount and not necessarily a
459  * PFS under a specific device mount (HMP).  The distinction is important
460  * because the elements backing a cluster mount can change on the fly.
461  */
462 struct hammer2_pfsmount {
463         struct mount            *mp;            /* kernel mount */
464         hammer2_cluster_t       *mount_cluster;
465         hammer2_cluster_t       *cluster;
466         hammer2_inode_t         *iroot;         /* PFS root inode */
467         hammer2_off_t           inode_count;    /* copy of inode_count */
468         ccms_domain_t           ccms_dom;
469         struct netexport        export;         /* nfs export */
470         int                     ronly;          /* read-only mount */
471         struct malloc_type      *minode;
472         struct malloc_type      *mmsg;
473         kdmsg_iocom_t           iocom;
474         struct spinlock         inum_spin;      /* inumber lookup */
475         struct hammer2_inode_tree inum_tree;
476         long                    inmem_inodes;
477         long                    inmem_chains;
478         int                     inmem_waiting;
479 };
480
481 typedef struct hammer2_pfsmount hammer2_pfsmount_t;
482
483 struct hammer2_cbinfo {
484         hammer2_chain_t *chain;
485         void (*func)(hammer2_chain_t *, struct buf *, char *, void *);
486         void *arg;
487         size_t boff;
488 };
489
490 typedef struct hammer2_cbinfo hammer2_cbinfo_t;
491
492 #if defined(_KERNEL)
493
494 MALLOC_DECLARE(M_HAMMER2);
495
496 #define VTOI(vp)        ((hammer2_inode_t *)(vp)->v_data)
497 #define ITOV(ip)        ((ip)->vp)
498
499 /*
500  * Currently locked chains retain the locked buffer cache buffer for
501  * indirect blocks, and indirect blocks can be one of two sizes.  The
502  * device buffer has to match the case to avoid deadlocking recursive
503  * chains that might otherwise try to access different offsets within
504  * the same device buffer.
505  */
506 static __inline
507 int
508 hammer2_devblkradix(int radix)
509 {
510 #if 1
511         if (radix <= HAMMER2_LBUFRADIX) {
512                 return (HAMMER2_LBUFRADIX);
513         } else {
514                 return (HAMMER2_PBUFRADIX);
515         }
516 #else
517         return (HAMMER2_PBUFRADIX);
518 #endif
519 }
520
521 static __inline
522 size_t
523 hammer2_devblksize(size_t bytes)
524 {
525 #if 1
526         if (bytes <= HAMMER2_LBUFSIZE) {
527                 return(HAMMER2_LBUFSIZE);
528         } else {
529                 KKASSERT(bytes <= HAMMER2_PBUFSIZE &&
530                          (bytes ^ (bytes - 1)) == ((bytes << 1) - 1));
531                 return (HAMMER2_PBUFSIZE);
532         }
533 #else
534         KKASSERT(bytes <= HAMMER2_PBUFSIZE &&
535                  (bytes ^ (bytes - 1)) == ((bytes << 1) - 1));
536         return(HAMMER2_PBUFSIZE);
537 #endif
538 }
539
540
541 static __inline
542 hammer2_pfsmount_t *
543 MPTOPMP(struct mount *mp)
544 {
545         return ((hammer2_pfsmount_t *)mp->mnt_data);
546 }
547
548 static __inline
549 hammer2_mount_t *
550 MPTOHMP(struct mount *mp)
551 {
552         return (((hammer2_pfsmount_t *)mp->mnt_data)->cluster->hmp);
553 }
554
555 static __inline
556 int
557 hammer2_chain_refactor_test(hammer2_chain_t *chain, int traverse_hlink)
558 {
559         hammer2_chain_t *next;
560
561         next = TAILQ_NEXT(chain, core_entry);
562
563         if ((chain->flags & HAMMER2_CHAIN_DELETED) &&
564             next &&
565             (next->flags & HAMMER2_CHAIN_SNAPSHOT) == 0) {
566                 return (1);
567         }
568         if (traverse_hlink &&
569             chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
570             (chain->flags & HAMMER2_CHAIN_HARDLINK) &&
571             next &&
572             (next->flags & HAMMER2_CHAIN_SNAPSHOT) == 0) {
573                 return(1);
574         }
575
576         return (0);
577 }
578
579 extern struct vop_ops hammer2_vnode_vops;
580 extern struct vop_ops hammer2_spec_vops;
581 extern struct vop_ops hammer2_fifo_vops;
582
583 extern int hammer2_debug;
584 extern int hammer2_cluster_enable;
585 extern int hammer2_hardlink_enable;
586 extern long hammer2_iod_file_read;
587 extern long hammer2_iod_meta_read;
588 extern long hammer2_iod_indr_read;
589 extern long hammer2_iod_fmap_read;
590 extern long hammer2_iod_volu_read;
591 extern long hammer2_iod_file_write;
592 extern long hammer2_iod_meta_write;
593 extern long hammer2_iod_indr_write;
594 extern long hammer2_iod_fmap_write;
595 extern long hammer2_iod_volu_write;
596 extern long hammer2_ioa_file_read;
597 extern long hammer2_ioa_meta_read;
598 extern long hammer2_ioa_indr_read;
599 extern long hammer2_ioa_fmap_read;
600 extern long hammer2_ioa_volu_read;
601 extern long hammer2_ioa_file_write;
602 extern long hammer2_ioa_meta_write;
603 extern long hammer2_ioa_indr_write;
604 extern long hammer2_ioa_fmap_write;
605 extern long hammer2_ioa_volu_write;
606
607 extern struct objcache *cache_buffer_read;
608 extern struct objcache *cache_buffer_write;
609
610 extern int destroy;
611 extern int write_thread_wakeup;
612
613 extern mtx_t thread_protect;
614
615 /*
616  * hammer2_subr.c
617  */
618 #define hammer2_icrc32(buf, size)       iscsi_crc32((buf), (size))
619 #define hammer2_icrc32c(buf, size, crc) iscsi_crc32_ext((buf), (size), (crc))
620
621 hammer2_chain_t *hammer2_inode_lock_ex(hammer2_inode_t *ip);
622 hammer2_chain_t *hammer2_inode_lock_sh(hammer2_inode_t *ip);
623 void hammer2_inode_unlock_ex(hammer2_inode_t *ip, hammer2_chain_t *chain);
624 void hammer2_inode_unlock_sh(hammer2_inode_t *ip, hammer2_chain_t *chain);
625 void hammer2_voldata_lock(hammer2_mount_t *hmp);
626 void hammer2_voldata_unlock(hammer2_mount_t *hmp, int modify);
627 ccms_state_t hammer2_inode_lock_temp_release(hammer2_inode_t *ip);
628 void hammer2_inode_lock_temp_restore(hammer2_inode_t *ip, ccms_state_t ostate);
629 ccms_state_t hammer2_inode_lock_upgrade(hammer2_inode_t *ip);
630 void hammer2_inode_lock_downgrade(hammer2_inode_t *ip, ccms_state_t ostate);
631
632 void hammer2_mount_exlock(hammer2_mount_t *hmp);
633 void hammer2_mount_shlock(hammer2_mount_t *hmp);
634 void hammer2_mount_unlock(hammer2_mount_t *hmp);
635
636 int hammer2_get_dtype(hammer2_chain_t *chain);
637 int hammer2_get_vtype(hammer2_chain_t *chain);
638 u_int8_t hammer2_get_obj_type(enum vtype vtype);
639 void hammer2_time_to_timespec(u_int64_t xtime, struct timespec *ts);
640 u_int64_t hammer2_timespec_to_time(struct timespec *ts);
641 u_int32_t hammer2_to_unix_xid(uuid_t *uuid);
642 void hammer2_guid_to_uuid(uuid_t *uuid, u_int32_t guid);
643
644 hammer2_key_t hammer2_dirhash(const unsigned char *name, size_t len);
645 int hammer2_getradix(size_t bytes);
646
647 int hammer2_calc_logical(hammer2_inode_t *ip, hammer2_off_t uoff,
648                         hammer2_key_t *lbasep, hammer2_key_t *leofp);
649 int hammer2_calc_physical(hammer2_inode_t *ip, hammer2_key_t lbase);
650 void hammer2_update_time(uint64_t *timep);
651
652 /*
653  * hammer2_inode.c
654  */
655 struct vnode *hammer2_igetv(hammer2_inode_t *ip, int *errorp);
656
657 void hammer2_inode_lock_nlinks(hammer2_inode_t *ip);
658 void hammer2_inode_unlock_nlinks(hammer2_inode_t *ip);
659 hammer2_inode_t *hammer2_inode_lookup(hammer2_pfsmount_t *pmp,
660                         hammer2_tid_t inum);
661 hammer2_inode_t *hammer2_inode_get(hammer2_pfsmount_t *pmp,
662                         hammer2_inode_t *dip, hammer2_chain_t *chain);
663 void hammer2_inode_free(hammer2_inode_t *ip);
664 void hammer2_inode_ref(hammer2_inode_t *ip);
665 void hammer2_inode_drop(hammer2_inode_t *ip);
666 void hammer2_inode_repoint(hammer2_inode_t *ip, hammer2_inode_t *pip,
667                         hammer2_chain_t *chain);
668
669 hammer2_inode_t *hammer2_inode_create(hammer2_trans_t *trans,
670                         hammer2_inode_t *dip,
671                         struct vattr *vap, struct ucred *cred,
672                         const uint8_t *name, size_t name_len,
673                         hammer2_chain_t **chainp, int *errorp);
674 int hammer2_inode_connect(hammer2_trans_t *trans, int hlink,
675                         hammer2_inode_t *dip, hammer2_chain_t **chainp,
676                         const uint8_t *name, size_t name_len);
677 hammer2_inode_t *hammer2_inode_common_parent(hammer2_inode_t *fdip,
678                         hammer2_inode_t *tdip);
679 void hammer2_inode_fsync(hammer2_trans_t *trans, hammer2_inode_t *ip,
680                         hammer2_chain_t **parentp);
681 int hammer2_unlink_file(hammer2_trans_t *trans, hammer2_inode_t *dip,
682                         const uint8_t *name, size_t name_len, int isdir,
683                         int *hlinkp);
684 int hammer2_hardlink_consolidate(hammer2_trans_t *trans, hammer2_inode_t *ip,
685                         hammer2_chain_t **chainp,
686                         hammer2_inode_t *tdip, int linkcnt);
687 int hammer2_hardlink_deconsolidate(hammer2_trans_t *trans, hammer2_inode_t *dip,
688                         hammer2_chain_t **chainp, hammer2_chain_t **ochainp);
689 int hammer2_hardlink_find(hammer2_inode_t *dip,
690                         hammer2_chain_t **chainp, hammer2_chain_t **ochainp);
691
692 /*
693  * hammer2_chain.c
694  */
695 void hammer2_modify_volume(hammer2_mount_t *hmp);
696 hammer2_chain_t *hammer2_chain_alloc(hammer2_mount_t *hmp, hammer2_pfsmount_t *pmp,
697                                 hammer2_trans_t *trans, hammer2_blockref_t *bref);
698 void hammer2_chain_core_alloc(hammer2_trans_t *trans, hammer2_chain_t *nchain,
699                                 hammer2_chain_t *ochain);
700 void hammer2_chain_ref(hammer2_chain_t *chain);
701 void hammer2_chain_drop(hammer2_chain_t *chain);
702 int hammer2_chain_lock(hammer2_chain_t *chain, int how);
703 void hammer2_chain_load_async(hammer2_chain_t *chain,
704                                 void (*func)(hammer2_chain_t *, struct buf *,
705                                              char *, void *),
706                                 void *arg);
707 void hammer2_chain_moved(hammer2_chain_t *chain);
708 void hammer2_chain_modify(hammer2_trans_t *trans,
709                                 hammer2_chain_t **chainp, int flags);
710 hammer2_inode_data_t *hammer2_chain_modify_ip(hammer2_trans_t *trans,
711                                 hammer2_inode_t *ip, hammer2_chain_t **chainp,
712                                 int flags);
713 void hammer2_chain_resize(hammer2_trans_t *trans, hammer2_inode_t *ip,
714                                 hammer2_chain_t *parent,
715                                 hammer2_chain_t **chainp,
716                                 int nradix, int flags);
717 void hammer2_chain_unlock(hammer2_chain_t *chain);
718 void hammer2_chain_wait(hammer2_chain_t *chain);
719 hammer2_chain_t *hammer2_chain_get(hammer2_chain_t *parent,
720                                 hammer2_blockref_t *bref);
721 hammer2_chain_t *hammer2_chain_lookup_init(hammer2_chain_t *parent, int flags);
722 void hammer2_chain_lookup_done(hammer2_chain_t *parent);
723 hammer2_chain_t *hammer2_chain_lookup(hammer2_chain_t **parentp,
724                                 hammer2_key_t *key_nextp,
725                                 hammer2_key_t key_beg, hammer2_key_t key_end,
726                                 int *cache_indexp, int flags);
727 hammer2_chain_t *hammer2_chain_next(hammer2_chain_t **parentp,
728                                 hammer2_chain_t *chain,
729                                 hammer2_key_t *key_nextp,
730                                 hammer2_key_t key_beg, hammer2_key_t key_end,
731                                 int *cache_indexp, int flags);
732
733 int hammer2_chain_create(hammer2_trans_t *trans,
734                                 hammer2_chain_t **parentp,
735                                 hammer2_chain_t **chainp,
736                                 hammer2_key_t key, int keybits,
737                                 int type, size_t bytes);
738 void hammer2_chain_duplicate(hammer2_trans_t *trans, hammer2_chain_t *parent,
739                                 hammer2_chain_t **chainp,
740                                 hammer2_blockref_t *bref);
741 int hammer2_chain_snapshot(hammer2_trans_t *trans, hammer2_inode_t *ip,
742                                 hammer2_ioc_pfs_t *pfs);
743 void hammer2_chain_delete(hammer2_trans_t *trans, hammer2_chain_t *chain,
744                                 int flags);
745 void hammer2_chain_delete_duplicate(hammer2_trans_t *trans,
746                                 hammer2_chain_t **chainp, int flags);
747 void hammer2_chain_flush(hammer2_trans_t *trans, hammer2_chain_t *chain);
748 void hammer2_chain_commit(hammer2_trans_t *trans, hammer2_chain_t *chain);
749 void hammer2_chain_setsubmod(hammer2_trans_t *trans, hammer2_chain_t *chain);
750
751 void hammer2_chain_memory_wait(hammer2_pfsmount_t *pmp);
752 void hammer2_chain_memory_wakeup(hammer2_pfsmount_t *pmp);
753 void hammer2_chain_countbrefs(hammer2_chain_core_t *above,
754                                 hammer2_blockref_t *base, int count);
755 void hammer2_chain_layer_check_locked(hammer2_mount_t *hmp,
756                                 hammer2_chain_core_t *core);
757
758 int hammer2_base_find(hammer2_blockref_t *base, int count,
759                                 hammer2_chain_core_t *above,
760                                 int *cache_indexp,
761                                 hammer2_key_t *key_nextp, hammer2_key_t key);
762 void hammer2_base_delete(hammer2_blockref_t *base, int count,
763                                 hammer2_chain_core_t *above,
764                                 int *cache_indexp, hammer2_blockref_t *elm);
765 void hammer2_base_insert(hammer2_blockref_t *base, int count,
766                                 hammer2_chain_core_t *above,
767                                 int *cache_indexp, hammer2_blockref_t *elm,
768                                 int flags);
769
770 /*
771  * hammer2_trans.c
772  */
773 void hammer2_trans_init(hammer2_trans_t *trans,
774                         hammer2_pfsmount_t *pmp, int flags);
775 void hammer2_trans_done(hammer2_trans_t *trans);
776
777 /*
778  * hammer2_ioctl.c
779  */
780 int hammer2_ioctl(hammer2_inode_t *ip, u_long com, void *data,
781                                 int fflag, struct ucred *cred);
782
783 /*
784  * hammer2_msgops.c
785  */
786 int hammer2_msg_dbg_rcvmsg(kdmsg_msg_t *msg);
787 int hammer2_msg_adhoc_input(kdmsg_msg_t *msg);
788
789 /*
790  * hammer2_vfsops.c
791  */
792 void hammer2_clusterctl_wakeup(kdmsg_iocom_t *iocom);
793 void hammer2_volconf_update(hammer2_pfsmount_t *pmp, int index);
794 void hammer2_cluster_reconnect(hammer2_pfsmount_t *pmp, struct file *fp);
795 void hammer2_dump_chain(hammer2_chain_t *chain, int tab, int *countp);
796
797 /*
798  * hammer2_freemap.c
799  */
800 int hammer2_freemap_alloc(hammer2_trans_t *trans, hammer2_mount_t *hmp,
801                                 hammer2_blockref_t *bref, size_t bytes);
802 void hammer2_freemap_free(hammer2_trans_t *trans, hammer2_mount_t *hmp,
803                                 hammer2_blockref_t *bref, int how);
804
805
806 #endif /* !_KERNEL */
807 #endif /* !_VFS_HAMMER2_HAMMER2_H_ */