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