hammer2 - Refactor frontend part 2/many
[dragonfly.git] / sys / vfs / hammer2 / hammer2.h
1 /*
2  * Copyright (c) 2011-2015 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  * HAMMER2 IN-MEMORY CACHE OF MEDIA STRUCTURES
38  *
39  * This header file contains structures used internally by the HAMMER2
40  * implementation.  See hammer2_disk.h for on-disk structures.
41  *
42  * There is an in-memory representation of all on-media data structure.
43  * Almost everything is represented by a hammer2_chain structure in-memory.
44  * Other higher-level structures typically map to chains.
45  *
46  * A great deal of data is accessed simply via its buffer cache buffer,
47  * which is mapped for the duration of the chain's lock.  Hammer2 must
48  * implement its own buffer cache layer on top of the system layer to
49  * allow for different threads to lock different sub-block-sized buffers.
50  *
51  * When modifications are made to a chain a new filesystem block must be
52  * allocated.  Multiple modifications do not typically allocate new blocks
53  * until the current block has been flushed.  Flushes do not block the
54  * front-end unless the front-end operation crosses the current inode being
55  * flushed.
56  *
57  * The in-memory representation may remain cached (for example in order to
58  * placemark clustering locks) even after the related data has been
59  * detached.
60  */
61
62 #ifndef _VFS_HAMMER2_HAMMER2_H_
63 #define _VFS_HAMMER2_HAMMER2_H_
64
65 #include <sys/param.h>
66 #include <sys/types.h>
67 #include <sys/kernel.h>
68 #include <sys/conf.h>
69 #include <sys/systm.h>
70 #include <sys/tree.h>
71 #include <sys/malloc.h>
72 #include <sys/mount.h>
73 #include <sys/vnode.h>
74 #include <sys/proc.h>
75 #include <sys/mountctl.h>
76 #include <sys/priv.h>
77 #include <sys/stat.h>
78 #include <sys/thread.h>
79 #include <sys/globaldata.h>
80 #include <sys/lockf.h>
81 #include <sys/buf.h>
82 #include <sys/queue.h>
83 #include <sys/limits.h>
84 #include <sys/dmsg.h>
85 #include <sys/mutex.h>
86 #include <sys/kern_syscall.h>
87
88 #include <sys/signal2.h>
89 #include <sys/buf2.h>
90 #include <sys/mutex2.h>
91 #include <sys/thread2.h>
92
93 #include "hammer2_disk.h"
94 #include "hammer2_mount.h"
95 #include "hammer2_ioctl.h"
96
97 struct hammer2_io;
98 struct hammer2_iocb;
99 struct hammer2_chain;
100 struct hammer2_cluster;
101 struct hammer2_inode;
102 struct hammer2_dev;
103 struct hammer2_pfs;
104 struct hammer2_span;
105 struct hammer2_state;
106 struct hammer2_msg;
107
108 /*
109  * Mutex and lock shims.  Hammer2 requires support for asynchronous and
110  * abortable locks, and both exclusive and shared spinlocks.  Normal
111  * synchronous non-abortable locks can be substituted for spinlocks.
112  */
113 typedef mtx_t                           hammer2_mtx_t;
114 typedef mtx_link_t                      hammer2_mtx_link_t;
115 typedef mtx_state_t                     hammer2_mtx_state_t;
116
117 typedef struct spinlock                 hammer2_spin_t;
118
119 #define hammer2_mtx_ex                  mtx_lock_ex_quick
120 #define hammer2_mtx_sh                  mtx_lock_sh_quick
121 #define hammer2_mtx_unlock              mtx_unlock
122 #define hammer2_mtx_owned               mtx_owned
123 #define hammer2_mtx_init                mtx_init
124 #define hammer2_mtx_temp_release        mtx_lock_temp_release
125 #define hammer2_mtx_temp_restore        mtx_lock_temp_restore
126 #define hammer2_mtx_refs                mtx_lockrefs
127
128 #define hammer2_spin_init               spin_init
129 #define hammer2_spin_sh                 spin_lock_shared
130 #define hammer2_spin_ex                 spin_lock
131 #define hammer2_spin_unsh               spin_unlock_shared
132 #define hammer2_spin_unex               spin_unlock
133
134 /*
135  * General lock support
136  */
137 static __inline
138 int
139 hammer2_mtx_upgrade(hammer2_mtx_t *mtx)
140 {
141         int wasexclusive;
142
143         if (mtx_islocked_ex(mtx)) {
144                 wasexclusive = 1;
145         } else {
146                 mtx_unlock(mtx);
147                 mtx_lock_ex_quick(mtx);
148                 wasexclusive = 0;
149         }
150         return wasexclusive;
151 }
152
153 /*
154  * Downgrade an inode lock from exclusive to shared only if the inode
155  * lock was previously shared.  If the inode lock was previously exclusive,
156  * this is a NOP.
157  */
158 static __inline
159 void
160 hammer2_mtx_downgrade(hammer2_mtx_t *mtx, int wasexclusive)
161 {
162         if (wasexclusive == 0)
163                 mtx_downgrade(mtx);
164 }
165
166 /*
167  * The xid tracks internal transactional updates.
168  *
169  * XXX fix-me, really needs to be 64-bits
170  */
171 typedef uint32_t hammer2_xid_t;
172
173 #define HAMMER2_XID_MIN 0x00000000U
174 #define HAMMER2_XID_MAX 0x7FFFFFFFU
175
176 /*
177  * The chain structure tracks a portion of the media topology from the
178  * root (volume) down.  Chains represent volumes, inodes, indirect blocks,
179  * data blocks, and freemap nodes and leafs.
180  *
181  * The chain structure utilizes a simple singly-homed topology and the
182  * chain's in-memory topology will move around as the chains do, due mainly
183  * to renames and indirect block creation.
184  *
185  * Block Table Updates
186  *
187  *      Block table updates for insertions and updates are delayed until the
188  *      flush.  This allows us to avoid having to modify the parent chain
189  *      all the way to the root.
190  *
191  *      Block table deletions are performed immediately (modifying the parent
192  *      in the process) because the flush code uses the chain structure to
193  *      track delayed updates and the chain will be (likely) gone or moved to
194  *      another location in the topology after a deletion.
195  *
196  *      A prior iteration of the code tried to keep the relationship intact
197  *      on deletes by doing a delete-duplicate operation on the chain, but
198  *      it added way too much complexity to the codebase.
199  *
200  * Flush Synchronization
201  *
202  *      The flush code must flush modified chains bottom-up.  Because chain
203  *      structures can shift around and are NOT topologically stable,
204  *      modified chains are independently indexed for the flush.  As the flush
205  *      runs it modifies (or further modifies) and updates the parents,
206  *      propagating the flush all the way to the volume root.
207  *
208  *      Modifying front-end operations can occur during a flush but will block
209  *      in two cases: (1) when the front-end tries to operate on the inode
210  *      currently in the midst of being flushed and (2) if the front-end
211  *      crosses an inode currently being flushed (such as during a rename).
212  *      So, for example, if you rename directory "x" to "a/b/c/d/e/f/g/x" and
213  *      the flusher is currently working on "a/b/c", the rename will block
214  *      temporarily in order to ensure that "x" exists in one place or the
215  *      other.
216  *
217  *      Meta-data statistics are updated by the flusher.  The front-end will
218  *      make estimates but meta-data must be fully synchronized only during a
219  *      flush in order to ensure that it remains correct across a crash.
220  *
221  *      Multiple flush synchronizations can theoretically be in-flight at the
222  *      same time but the implementation is not coded to handle the case and
223  *      currently serializes them.
224  *
225  * Snapshots:
226  *
227  *      Snapshots currently require the subdirectory tree being snapshotted
228  *      to be flushed.  The snapshot then creates a new super-root inode which
229  *      copies the flushed blockdata of the directory or file that was
230  *      snapshotted.
231  *
232  * RBTREE NOTES:
233  *
234  *      - Note that the radix tree runs in powers of 2 only so sub-trees
235  *        cannot straddle edges.
236  */
237 RB_HEAD(hammer2_chain_tree, hammer2_chain);
238 TAILQ_HEAD(h2_flush_list, hammer2_chain);
239 TAILQ_HEAD(h2_core_list, hammer2_chain);
240 TAILQ_HEAD(h2_iocb_list, hammer2_iocb);
241
242 #define CHAIN_CORE_DELETE_BMAP_ENTRIES  \
243         (HAMMER2_PBUFSIZE / sizeof(hammer2_blockref_t) / sizeof(uint32_t))
244
245 /*
246  * Core topology for chain (embedded in chain).  Protected by a spinlock.
247  */
248 struct hammer2_chain_core {
249         hammer2_spin_t  spin;
250         struct hammer2_chain_tree rbtree; /* sub-chains */
251         int             live_zero;      /* blockref array opt */
252         u_int           live_count;     /* live (not deleted) chains in tree */
253         u_int           chain_count;    /* live + deleted chains under core */
254         int             generation;     /* generation number (inserts only) */
255 };
256
257 typedef struct hammer2_chain_core hammer2_chain_core_t;
258
259 RB_HEAD(hammer2_io_tree, hammer2_io);
260
261 /*
262  * IOCB - IO callback (into chain, cluster, or manual request)
263  */
264 struct hammer2_iocb {
265         TAILQ_ENTRY(hammer2_iocb) entry;
266         void (*callback)(struct hammer2_iocb *iocb);
267         struct hammer2_io       *dio;
268         struct hammer2_cluster  *cluster;
269         struct hammer2_chain    *chain;
270         void                    *ptr;
271         off_t                   lbase;
272         int                     lsize;
273         uint32_t                flags;
274         int                     error;
275 };
276
277 typedef struct hammer2_iocb hammer2_iocb_t;
278
279 #define HAMMER2_IOCB_INTERLOCK  0x00000001
280 #define HAMMER2_IOCB_ONQ        0x00000002
281 #define HAMMER2_IOCB_DONE       0x00000004
282 #define HAMMER2_IOCB_INPROG     0x00000008
283 #define HAMMER2_IOCB_UNUSED10   0x00000010
284 #define HAMMER2_IOCB_QUICK      0x00010000
285 #define HAMMER2_IOCB_ZERO       0x00020000
286 #define HAMMER2_IOCB_READ       0x00040000
287 #define HAMMER2_IOCB_WAKEUP     0x00080000
288
289 /*
290  * DIO - Management structure wrapping system buffer cache.
291  *
292  *       Used for multiple purposes including concurrent management
293  *       if small requests by chains into larger DIOs.
294  */
295 struct hammer2_io {
296         RB_ENTRY(hammer2_io) rbnode;    /* indexed by device offset */
297         struct h2_iocb_list iocbq;
298         struct spinlock spin;
299         struct hammer2_dev *hmp;
300         struct buf      *bp;
301         off_t           pbase;
302         int             psize;
303         int             refs;
304         int             act;                    /* activity */
305 };
306
307 typedef struct hammer2_io hammer2_io_t;
308
309 #define HAMMER2_DIO_INPROG      0x80000000      /* bio in progress */
310 #define HAMMER2_DIO_GOOD        0x40000000      /* dio->bp is stable */
311 #define HAMMER2_DIO_WAITING     0x20000000      /* (old) */
312 #define HAMMER2_DIO_DIRTY       0x10000000      /* flush on last drop */
313
314 #define HAMMER2_DIO_MASK        0x0FFFFFFF
315
316 /*
317  * Primary chain structure keeps track of the topology in-memory.
318  */
319 struct hammer2_chain {
320         hammer2_mtx_t           lock;
321         hammer2_chain_core_t    core;
322         RB_ENTRY(hammer2_chain) rbnode;         /* live chain(s) */
323         hammer2_blockref_t      bref;
324         struct hammer2_chain    *parent;
325         struct hammer2_state    *state;         /* if active cache msg */
326         struct hammer2_dev      *hmp;
327         struct hammer2_pfs      *pmp;           /* A PFS or super-root (spmp) */
328
329         hammer2_xid_t   flush_xid;              /* flush sequencing */
330         hammer2_io_t    *dio;                   /* physical data buffer */
331         u_int           bytes;                  /* physical data size */
332         u_int           flags;
333         u_int           refs;
334         u_int           lockcnt;
335         int             error;                  /* on-lock data error state */
336
337         hammer2_media_data_t *data;             /* data pointer shortcut */
338         TAILQ_ENTRY(hammer2_chain) flush_node;  /* flush list */
339 };
340
341 typedef struct hammer2_chain hammer2_chain_t;
342
343 int hammer2_chain_cmp(hammer2_chain_t *chain1, hammer2_chain_t *chain2);
344 RB_PROTOTYPE(hammer2_chain_tree, hammer2_chain, rbnode, hammer2_chain_cmp);
345
346 /*
347  * Special notes on flags:
348  *
349  * INITIAL      - This flag allows a chain to be created and for storage to
350  *                be allocated without having to immediately instantiate the
351  *                related buffer.  The data is assumed to be all-zeros.  It
352  *                is primarily used for indirect blocks.
353  *
354  * MODIFIED     - The chain's media data has been modified.
355  *
356  * UPDATE       - Chain might not be modified but parent blocktable needs update
357  *
358  * FICTITIOUS   - Faked chain as a placeholder for an error condition.  This
359  *                chain is unsuitable for I/O.
360  *
361  * BMAPPED      - Indicates that the chain is present in the parent blockmap.
362  *
363  * BMAPUPD      - Indicates that the chain is present but needs to be updated
364  *                in the parent blockmap.
365  */
366 #define HAMMER2_CHAIN_MODIFIED          0x00000001      /* dirty chain data */
367 #define HAMMER2_CHAIN_ALLOCATED         0x00000002      /* kmalloc'd chain */
368 #define HAMMER2_CHAIN_DESTROY           0x00000004
369 #define HAMMER2_CHAIN_UNUSED0008        0x00000008
370 #define HAMMER2_CHAIN_DELETED           0x00000010      /* deleted chain */
371 #define HAMMER2_CHAIN_INITIAL           0x00000020      /* initial create */
372 #define HAMMER2_CHAIN_UPDATE            0x00000040      /* need parent update */
373 #define HAMMER2_CHAIN_DEFERRED          0x00000080      /* flush depth defer */
374 #define HAMMER2_CHAIN_IOFLUSH           0x00000100      /* bawrite on put */
375 #define HAMMER2_CHAIN_ONFLUSH           0x00000200      /* on a flush list */
376 #define HAMMER2_CHAIN_FICTITIOUS        0x00000400      /* unsuitable for I/O */
377 #define HAMMER2_CHAIN_VOLUMESYNC        0x00000800      /* needs volume sync */
378 #define HAMMER2_CHAIN_DELAYED           0x00001000      /* delayed flush */
379 #define HAMMER2_CHAIN_COUNTEDBREFS      0x00002000      /* block table stats */
380 #define HAMMER2_CHAIN_ONRBTREE          0x00004000      /* on parent RB tree */
381 #define HAMMER2_CHAIN_UNUSED00008000    0x00008000
382 #define HAMMER2_CHAIN_EMBEDDED          0x00010000      /* embedded data */
383 #define HAMMER2_CHAIN_RELEASE           0x00020000      /* don't keep around */
384 #define HAMMER2_CHAIN_BMAPPED           0x00040000      /* present in blkmap */
385 #define HAMMER2_CHAIN_BMAPUPD           0x00080000      /* +needs updating */
386 #define HAMMER2_CHAIN_IOINPROG          0x00100000      /* I/O interlock */
387 #define HAMMER2_CHAIN_IOSIGNAL          0x00200000      /* I/O interlock */
388 #define HAMMER2_CHAIN_PFSBOUNDARY       0x00400000      /* super->pfs inode */
389
390 #define HAMMER2_CHAIN_FLUSH_MASK        (HAMMER2_CHAIN_MODIFIED |       \
391                                          HAMMER2_CHAIN_UPDATE |         \
392                                          HAMMER2_CHAIN_ONFLUSH)
393
394 /*
395  * Hammer2 error codes, used by chain->error and cluster->error.  The error
396  * code is typically set on-lock unless no I/O was requested, and set on
397  * I/O otherwise.  If set for a cluster it generally means that the cluster
398  * code could not find a valid copy to present.
399  *
400  * IO           - An I/O error occurred
401  * CHECK        - I/O succeeded but did not match the check code
402  * INCOMPLETE   - A cluster is not complete enough to use, or
403  *                a chain cannot be loaded because its parent has an error.
404  *
405  * NOTE: API allows callers to check zero/non-zero to determine if an error
406  *       condition exists.
407  *
408  * NOTE: Chain's data field is usually NULL on an IO error but not necessarily
409  *       NULL on other errors.  Check chain->error, not chain->data.
410  */
411 #define HAMMER2_ERROR_NONE              0
412 #define HAMMER2_ERROR_IO                1       /* device I/O error */
413 #define HAMMER2_ERROR_CHECK             2       /* check code mismatch */
414 #define HAMMER2_ERROR_INCOMPLETE        3       /* incomplete cluster */
415 #define HAMMER2_ERROR_DEPTH             4       /* temporary depth limit */
416
417 /*
418  * Flags passed to hammer2_chain_lookup() and hammer2_chain_next()
419  *
420  * NOTE: MATCHIND allows an indirect block / freemap node to be returned
421  *       when the passed key range matches the radix.  Remember that key_end
422  *       is inclusive (e.g. {0x000,0xFFF}, not {0x000,0x1000}).
423  *
424  * NOTE: NODIRECT prevents a lookup of offset 0 in an inode from returning
425  *       the inode itself if the inode is in DIRECTDATA mode (i.e. file is
426  *       <= 512 bytes).
427  */
428 #define HAMMER2_LOOKUP_NOLOCK           0x00000001      /* ref only */
429 #define HAMMER2_LOOKUP_NODATA           0x00000002      /* data left NULL */
430 #define HAMMER2_LOOKUP_NODIRECT         0x00000004      /* no offset=0 DD */
431 #define HAMMER2_LOOKUP_SHARED           0x00000100
432 #define HAMMER2_LOOKUP_MATCHIND         0x00000200      /* return all chains */
433 #define HAMMER2_LOOKUP_ALLNODES         0x00000400      /* allow NULL focus */
434 #define HAMMER2_LOOKUP_ALWAYS           0x00000800      /* resolve data */
435
436 /*
437  * Flags passed to hammer2_chain_modify() and hammer2_chain_resize()
438  *
439  * NOTE: OPTDATA allows us to avoid instantiating buffers for INDIRECT
440  *       blocks in the INITIAL-create state.
441  */
442 #define HAMMER2_MODIFY_OPTDATA          0x00000002      /* data can be NULL */
443 #define HAMMER2_MODIFY_NO_MODIFY_TID    0x00000004
444 #define HAMMER2_MODIFY_UNUSED0008       0x00000008
445 #define HAMMER2_MODIFY_NOREALLOC        0x00000010
446
447 /*
448  * Flags passed to hammer2_chain_lock()
449  *
450  * NOTE: RDONLY is set to optimize cluster operations when *no* modifications
451  *       will be made to either the cluster being locked or any underlying
452  *       cluster.  It allows the cluster to lock and access data for a subset
453  *       of available nodes instead of all available nodes.
454  */
455 #define HAMMER2_RESOLVE_NEVER           1
456 #define HAMMER2_RESOLVE_MAYBE           2
457 #define HAMMER2_RESOLVE_ALWAYS          3
458 #define HAMMER2_RESOLVE_MASK            0x0F
459
460 #define HAMMER2_RESOLVE_SHARED          0x10    /* request shared lock */
461 #define HAMMER2_RESOLVE_UNUSED20        0x20
462 #define HAMMER2_RESOLVE_RDONLY          0x40    /* higher level op flag */
463
464 /*
465  * Flags passed to hammer2_chain_delete()
466  */
467 #define HAMMER2_DELETE_PERMANENT        0x0001
468 #define HAMMER2_DELETE_NOSTATS          0x0002
469
470 #define HAMMER2_INSERT_NOSTATS          0x0002
471 #define HAMMER2_INSERT_PFSROOT          0x0004
472
473 /*
474  * Flags passed to hammer2_chain_delete_duplicate()
475  */
476 #define HAMMER2_DELDUP_RECORE           0x0001
477
478 /*
479  * Cluster different types of storage together for allocations
480  */
481 #define HAMMER2_FREECACHE_INODE         0
482 #define HAMMER2_FREECACHE_INDIR         1
483 #define HAMMER2_FREECACHE_DATA          2
484 #define HAMMER2_FREECACHE_UNUSED3       3
485 #define HAMMER2_FREECACHE_TYPES         4
486
487 /*
488  * hammer2_freemap_alloc() block preference
489  */
490 #define HAMMER2_OFF_NOPREF              ((hammer2_off_t)-1)
491
492 /*
493  * BMAP read-ahead maximum parameters
494  */
495 #define HAMMER2_BMAP_COUNT              16      /* max bmap read-ahead */
496 #define HAMMER2_BMAP_BYTES              (HAMMER2_PBUFSIZE * HAMMER2_BMAP_COUNT)
497
498 /*
499  * hammer2_freemap_adjust()
500  */
501 #define HAMMER2_FREEMAP_DORECOVER       1
502 #define HAMMER2_FREEMAP_DOMAYFREE       2
503 #define HAMMER2_FREEMAP_DOREALFREE      3
504
505 /*
506  * HAMMER2 cluster - A set of chains representing the same entity.
507  *
508  * hammer2_cluster typically represents a temporary set of representitive
509  * chains.  The one exception is that a hammer2_cluster is embedded in
510  * hammer2_inode.  This embedded cluster is ONLY used to track the
511  * representitive chains and cannot be directly locked.
512  *
513  * A cluster is usually temporary (and thus per-thread) for locking purposes,
514  * allowing us to embed the asynchronous storage required for cluster
515  * operations in the cluster itself and adjust the state and status without
516  * having to worry too much about SMP issues.
517  *
518  * The exception is the cluster embedded in the hammer2_inode structure.
519  * This is used to cache the cluster state on an inode-by-inode basis.
520  * Individual hammer2_chain structures not incorporated into clusters might
521  * also stick around to cache miscellanious elements.
522  *
523  * Because the cluster is a 'working copy' and is usually subject to cluster
524  * quorum rules, it is quite possible for us to end up with an insufficient
525  * number of live chains to execute an operation.  If an insufficient number
526  * of chains remain in a working copy, the operation may have to be
527  * downgraded, retried, stall until the requisit number of chains are
528  * available, or possibly even error out depending on the mount type.
529  *
530  * A cluster's focus is set when it is locked.  The focus can only be set
531  * to a chain still part of the synchronized set.
532  */
533 #define HAMMER2_MAXCLUSTER      8
534
535 struct hammer2_cluster_item {
536 #if 0
537         hammer2_mtx_link_t      async_link;
538 #endif
539         hammer2_chain_t         *chain;
540 #if 0
541         struct hammer2_cluster  *cluster;       /* link back to cluster */
542 #endif
543         int                     cache_index;
544         uint32_t                flags;
545 };
546
547 typedef struct hammer2_cluster_item hammer2_cluster_item_t;
548
549 /*
550  * INVALID      - Invalid for focus, i.e. not part of synchronized set.
551  *                Once set, this bit is sticky across operations.
552  *
553  * FEMOD        - Indicates that front-end modifying operations can
554  *                mess with this entry and MODSYNC will copy also
555  *                effect it.
556  */
557 #define HAMMER2_CITEM_INVALID   0x00000001
558 #define HAMMER2_CITEM_FEMOD     0x00000002
559
560 struct hammer2_cluster {
561         int                     refs;           /* track for deallocation */
562         int                     ddflag;
563         struct hammer2_pfs      *pmp;
564         uint32_t                flags;
565         int                     nchains;
566         int                     error;          /* error code valid on lock */
567         int                     focus_index;
568         hammer2_iocb_t          iocb;
569         hammer2_chain_t         *focus;         /* current focus (or mod) */
570         hammer2_cluster_item_t  array[HAMMER2_MAXCLUSTER];
571 };
572
573 typedef struct hammer2_cluster  hammer2_cluster_t;
574
575 /*
576  * WRHARD       - Hard mounts can write fully synchronized
577  * RDHARD       - Hard mounts can read fully synchronized
578  * UNHARD       - Unsynchronized masters present
579  * NOHARD       - No masters visible
580  * WRSOFT       - Soft mounts can write to at least the SOFT_MASTER
581  * RDSOFT       - Soft mounts can read from at least a SOFT_SLAVE
582  * UNSOFT       - Unsynchronized slaves present
583  * NOSOFT       - No slaves visible
584  * RDSLAVE      - slaves are accessible (possibly unsynchronized or remote).
585  * MSYNCED      - All masters are fully synchronized
586  * SSYNCED      - All known local slaves are fully synchronized to masters
587  *
588  * All available masters are always incorporated.  All PFSs belonging to a
589  * cluster (master, slave, copy, whatever) always try to synchronize the
590  * total number of known masters in the PFSs root inode.
591  *
592  * A cluster might have access to many slaves, copies, or caches, but we
593  * have a limited number of cluster slots.  Any such elements which are
594  * directly mounted from block device(s) will always be incorporated.   Note
595  * that SSYNCED only applies to such elements which are directly mounted,
596  * not to any remote slaves, copies, or caches that could be available.  These
597  * bits are used to monitor and drive our synchronization threads.
598  *
599  * When asking the question 'is any data accessible at all', then a simple
600  * test against (RDHARD|RDSOFT|RDSLAVE) gives you the answer.  If any of
601  * these bits are set the object can be read with certain caveats:
602  * RDHARD - no caveats.  RDSOFT - authoritative but might not be synchronized.
603  * and RDSLAVE - not authoritative, has some data but it could be old or
604  * incomplete.
605  *
606  * When both soft and hard mounts are available, data will be read and written
607  * via the soft mount only.  But all might be in the cluster because
608  * background synchronization threads still need to do their work.
609  */
610 #define HAMMER2_CLUSTER_INODE   0x00000001      /* embedded in inode struct */
611 #define HAMMER2_CLUSTER_UNUSED2 0x00000002
612 #define HAMMER2_CLUSTER_LOCKED  0x00000004      /* cluster lks not recursive */
613 #define HAMMER2_CLUSTER_WRHARD  0x00000100      /* hard-mount can write */
614 #define HAMMER2_CLUSTER_RDHARD  0x00000200      /* hard-mount can read */
615 #define HAMMER2_CLUSTER_UNHARD  0x00000400      /* unsynchronized masters */
616 #define HAMMER2_CLUSTER_NOHARD  0x00000800      /* no masters visible */
617 #define HAMMER2_CLUSTER_WRSOFT  0x00001000      /* soft-mount can write */
618 #define HAMMER2_CLUSTER_RDSOFT  0x00002000      /* soft-mount can read */
619 #define HAMMER2_CLUSTER_UNSOFT  0x00004000      /* unsynchronized slaves */
620 #define HAMMER2_CLUSTER_NOSOFT  0x00008000      /* no slaves visible */
621 #define HAMMER2_CLUSTER_MSYNCED 0x00010000      /* all masters synchronized */
622 #define HAMMER2_CLUSTER_SSYNCED 0x00020000      /* known slaves synchronized */
623
624 #define HAMMER2_CLUSTER_ANYDATA ( HAMMER2_CLUSTER_RDHARD |      \
625                                   HAMMER2_CLUSTER_RDSOFT |      \
626                                   HAMMER2_CLUSTER_RDSLAVE)
627
628 #define HAMMER2_CLUSTER_RDOK    ( HAMMER2_CLUSTER_RDHARD |      \
629                                   HAMMER2_CLUSTER_RDSOFT)
630
631 #define HAMMER2_CLUSTER_WROK    ( HAMMER2_CLUSTER_WRHARD |      \
632                                   HAMMER2_CLUSTER_WRSOFT)
633
634 #define HAMMER2_CLUSTER_ZFLAGS  ( HAMMER2_CLUSTER_WRHARD |      \
635                                   HAMMER2_CLUSTER_RDHARD |      \
636                                   HAMMER2_CLUSTER_WRSOFT |      \
637                                   HAMMER2_CLUSTER_RDSOFT |      \
638                                   HAMMER2_CLUSTER_MSYNCED |     \
639                                   HAMMER2_CLUSTER_SSYNCED)
640
641 /*
642  * Helper functions (cluster must be locked for flags to be valid).
643  */
644 static __inline
645 int
646 hammer2_cluster_rdok(hammer2_cluster_t *cluster)
647 {
648         return (cluster->flags & HAMMER2_CLUSTER_RDOK);
649 }
650
651 static __inline
652 int
653 hammer2_cluster_wrok(hammer2_cluster_t *cluster)
654 {
655         return (cluster->flags & HAMMER2_CLUSTER_WROK);
656 }
657
658 RB_HEAD(hammer2_inode_tree, hammer2_inode);
659
660 /*
661  * A hammer2 inode.
662  *
663  * NOTE: The inode-embedded cluster is never used directly for I/O (since
664  *       it may be shared).  Instead it will be replicated-in and synchronized
665  *       back out if changed.
666  */
667 struct hammer2_inode {
668         RB_ENTRY(hammer2_inode) rbnode;         /* inumber lookup (HL) */
669         hammer2_mtx_t           lock;           /* inode lock */
670         struct hammer2_pfs      *pmp;           /* PFS mount */
671         struct hammer2_inode    *pip;           /* parent inode */
672         struct vnode            *vp;
673         struct spinlock         cluster_spin;   /* update cluster */
674         hammer2_cluster_t       cluster;
675         struct lockf            advlock;
676         hammer2_tid_t           inum;
677         u_int                   flags;
678         u_int                   refs;           /* +vpref, +flushref */
679         uint8_t                 comp_heuristic;
680         hammer2_off_t           size;           /* cache file size */
681         uint64_t                mtime;          /* cache mtime */
682 };
683
684 typedef struct hammer2_inode hammer2_inode_t;
685
686 #define HAMMER2_INODE_MODIFIED          0x0001
687 #define HAMMER2_INODE_SROOT             0x0002  /* kmalloc special case */
688 #define HAMMER2_INODE_RENAME_INPROG     0x0004
689 #define HAMMER2_INODE_ONRBTREE          0x0008
690 #define HAMMER2_INODE_RESIZED           0x0010
691 #define HAMMER2_INODE_MTIME             0x0020
692 #define HAMMER2_INODE_ISUNLINKED        0x0040
693
694 int hammer2_inode_cmp(hammer2_inode_t *ip1, hammer2_inode_t *ip2);
695 RB_PROTOTYPE2(hammer2_inode_tree, hammer2_inode, rbnode, hammer2_inode_cmp,
696                 hammer2_tid_t);
697
698 /*
699  * inode-unlink side-structure
700  */
701 struct hammer2_inode_unlink {
702         TAILQ_ENTRY(hammer2_inode_unlink) entry;
703         hammer2_inode_t *ip;
704 };
705 TAILQ_HEAD(h2_unlk_list, hammer2_inode_unlink);
706
707 typedef struct hammer2_inode_unlink hammer2_inode_unlink_t;
708
709 /*
710  * A hammer2 transaction and flush sequencing structure.
711  *
712  * This global structure is tied into hammer2_dev and is used
713  * to sequence modifying operations and flushes.  These operations
714  * run on whole cluster PFSs, not individual nodes (at this level),
715  * so we do not record mirror_tid here.
716  */
717 struct hammer2_trans {
718         TAILQ_ENTRY(hammer2_trans) entry;
719         struct hammer2_pfs      *pmp;
720         hammer2_xid_t           sync_xid;       /* transaction sequencer */
721         hammer2_tid_t           inode_tid;      /* inode number assignment */
722         hammer2_tid_t           modify_tid;     /* modify transaction id */
723         thread_t                td;             /* pointer */
724         int                     flags;
725         int                     blocked;
726         uint8_t                 inodes_created;
727         uint8_t                 dummy[7];
728 };
729
730 typedef struct hammer2_trans hammer2_trans_t;
731
732 #define HAMMER2_TRANS_ISFLUSH           0x0001  /* formal flush */
733 #define HAMMER2_TRANS_CONCURRENT        0x0002  /* concurrent w/flush */
734 #define HAMMER2_TRANS_BUFCACHE          0x0004  /* from bioq strategy write */
735 #define HAMMER2_TRANS_NEWINODE          0x0008  /* caller allocating inode */
736 #define HAMMER2_TRANS_KEEPMODIFY        0x0010  /* do not change bref.modify */
737 #define HAMMER2_TRANS_PREFLUSH          0x0020  /* preflush state */
738
739 #define HAMMER2_FREEMAP_HEUR_NRADIX     4       /* pwr 2 PBUFRADIX-MINIORADIX */
740 #define HAMMER2_FREEMAP_HEUR_TYPES      8
741 #define HAMMER2_FREEMAP_HEUR            (HAMMER2_FREEMAP_HEUR_NRADIX * \
742                                          HAMMER2_FREEMAP_HEUR_TYPES)
743
744 /*
745  * Transaction Rendezvous
746  */
747 TAILQ_HEAD(hammer2_trans_queue, hammer2_trans);
748
749 struct hammer2_trans_manage {
750         hammer2_xid_t           flush_xid;      /* last flush transaction */
751         hammer2_xid_t           alloc_xid;
752         struct lock             translk;        /* lockmgr lock */
753         struct hammer2_trans_queue transq;      /* modifying transactions */
754         int                     flushcnt;       /* track flush trans */
755 };
756
757 typedef struct hammer2_trans_manage hammer2_trans_manage_t;
758
759 /*
760  * Collection point for distributed frontend operation.  Uses a managed
761  * objcache.
762  */
763 struct hammer2_vop_info {
764         hammer2_inode_t *dip;
765         hammer2_inode_t *ip;
766         struct uio      *uio;
767 };
768
769
770 /*
771  * Cluster node synchronization thread element.
772  *
773  * Multiple syncthr's can hang off of a hammer2_pfs structure, typically one
774  * for each block device that is part of the PFS.  Synchronization threads
775  * for PFSs accessed over the network are handled by their respective hosts.
776  *
777  * Synchronization threads are responsible for keeping a local node
778  * synchronized to the greater cluster.
779  *
780  * A syncthr can also hang off each hammer2_dev's super-root PFS (spmp).
781  * This thread is responsible for automatic bulkfree and dedup scans.
782  */
783 struct hammer2_syncthr {
784         struct hammer2_pfs *pmp;
785         kdmsg_state_t   *span;
786         thread_t        td;
787         uint32_t        flags;
788         int             depth;
789         int             clindex;        /* sync_thrs[] array index */
790         hammer2_trans_t trans;
791         struct lock     lk;
792 };
793
794 typedef struct hammer2_syncthr hammer2_syncthr_t;
795
796 #define HAMMER2_SYNCTHR_UNMOUNTING      0x0001  /* unmount request */
797 #define HAMMER2_SYNCTHR_DEV             0x0002  /* related to dev, not pfs */
798 #define HAMMER2_SYNCTHR_SPANNED         0x0004  /* LNK_SPAN active */
799 #define HAMMER2_SYNCTHR_REMASTER        0x0008  /* remaster request */
800 #define HAMMER2_SYNCTHR_STOP            0x0010  /* exit request */
801 #define HAMMER2_SYNCTHR_FREEZE          0x0020  /* force idle */
802 #define HAMMER2_SYNCTHR_FROZEN          0x0040  /* restart */
803
804
805 /*
806  * Global (per partition) management structure, represents a hard block
807  * device.  Typically referenced by hammer2_chain structures when applicable.
808  * Typically not used for network-managed elements.
809  *
810  * Note that a single hammer2_dev can be indirectly tied to multiple system
811  * mount points.  There is no direct relationship.  System mounts are
812  * per-cluster-id, not per-block-device, and a single hard mount might contain
813  * many PFSs and those PFSs might combine together in various ways to form
814  * the set of available clusters.
815  */
816 struct hammer2_dev {
817         struct vnode    *devvp;         /* device vnode */
818         int             ronly;          /* read-only mount */
819         int             mount_count;    /* number of actively mounted PFSs */
820         TAILQ_ENTRY(hammer2_dev) mntentry; /* hammer2_mntlist */
821
822         struct malloc_type *mchain;
823         int             nipstacks;
824         int             maxipstacks;
825         kdmsg_iocom_t   iocom;          /* volume-level dmsg interface */
826         struct spinlock io_spin;        /* iotree access */
827         struct hammer2_io_tree iotree;
828         int             iofree_count;
829         hammer2_chain_t vchain;         /* anchor chain (topology) */
830         hammer2_chain_t fchain;         /* anchor chain (freemap) */
831         struct spinlock list_spin;
832         struct h2_flush_list    flushq; /* flush seeds */
833         struct hammer2_pfs *spmp;       /* super-root pmp for transactions */
834         struct lock     vollk;          /* lockmgr lock */
835         hammer2_off_t   heur_freemap[HAMMER2_FREEMAP_HEUR];
836         int             volhdrno;       /* last volhdrno written */
837         char            devrepname[64]; /* for kprintf */
838         hammer2_volume_data_t voldata;
839         hammer2_volume_data_t volsync;  /* synchronized voldata */
840 };
841
842 typedef struct hammer2_dev hammer2_dev_t;
843
844 /*
845  * Helper functions (cluster must be locked for flags to be valid).
846  */
847 static __inline
848 int
849 hammer2_chain_rdok(hammer2_chain_t *chain)
850 {
851         return (chain->error == 0);
852 }
853
854 static __inline
855 int
856 hammer2_chain_wrok(hammer2_chain_t *chain)
857 {
858         return (chain->error == 0 && chain->hmp->ronly == 0);
859 }
860
861 /*
862  * Per-cluster management structure.  This structure will be tied to a
863  * system mount point if the system is mounting the PFS, but is also used
864  * to manage clusters encountered during the super-root scan or received
865  * via LNK_SPANs that might not be mounted.
866  *
867  * This structure is also used to represent the super-root that hangs off
868  * of a hard mount point.  The super-root is not really a cluster element.
869  * In this case the spmp_hmp field will be non-NULL.  It's just easier to do
870  * this than to special case super-root manipulation in the hammer2_chain*
871  * code as being only hammer2_dev-related.
872  *
873  * pfs_mode and pfs_nmasters are rollup fields which critically describes
874  * how elements of the cluster act on the cluster.  pfs_mode is only applicable
875  * when a PFS is mounted by the system.  pfs_nmasters is our best guess as to
876  * how many masters have been configured for a cluster and is always
877  * applicable.  pfs_types[] is an array with 1:1 correspondance to the
878  * iroot cluster and describes the PFS types of the nodes making up the
879  * cluster.
880  *
881  * WARNING! Portions of this structure have deferred initialization.  In
882  *          particular, if not mounted there will be no ihidden or wthread.
883  *          umounted network PFSs will also be missing iroot and numerous
884  *          other fields will not be initialized prior to mount.
885  *
886  *          Synchronization threads are chain-specific and only applicable
887  *          to local hard PFS entries.  A hammer2_pfs structure may contain
888  *          more than one when multiple hard PFSs are present on the local
889  *          machine which require synchronization monitoring.  Most PFSs
890  *          (such as snapshots) are 1xMASTER PFSs which do not need a
891  *          synchronization thread.
892  *
893  * WARNING! The chains making up pfs->iroot's cluster are accounted for in
894  *          hammer2_dev->mount_count when the pfs is associated with a mount
895  *          point.
896  */
897 struct hammer2_pfs {
898         struct mount            *mp;
899         TAILQ_ENTRY(hammer2_pfs) mntentry;      /* hammer2_pfslist */
900         uuid_t                  pfs_clid;
901         hammer2_dev_t           *spmp_hmp;      /* only if super-root pmp */
902         hammer2_inode_t         *iroot;         /* PFS root inode */
903         hammer2_inode_t         *ihidden;       /* PFS hidden directory */
904         uint8_t                 pfs_types[HAMMER2_MAXCLUSTER];
905         char                    *pfs_names[HAMMER2_MAXCLUSTER];
906         hammer2_trans_manage_t  tmanage;        /* transaction management */
907         struct lock             lock;           /* PFS lock for certain ops */
908         struct netexport        export;         /* nfs export */
909         int                     ronly;          /* read-only mount */
910         struct malloc_type      *minode;
911         struct malloc_type      *mmsg;
912         struct spinlock         inum_spin;      /* inumber lookup */
913         struct hammer2_inode_tree inum_tree;    /* (not applicable to spmp) */
914         hammer2_tid_t           modify_tid;     /* modify transaction id */
915         hammer2_tid_t           inode_tid;      /* inode allocator */
916         uint8_t                 pfs_nmasters;   /* total masters */
917         uint8_t                 pfs_mode;       /* operating mode PFSMODE */
918         uint8_t                 unused01;
919         uint8_t                 unused02;
920         uint32_t                unused03;
921         long                    inmem_inodes;
922         uint32_t                inmem_dirty_chains;
923         int                     count_lwinprog; /* logical write in prog */
924         struct spinlock         list_spin;
925         struct h2_unlk_list     unlinkq;        /* last-close unlink */
926         hammer2_syncthr_t       sync_thrs[HAMMER2_MAXCLUSTER];
927         thread_t                wthread_td;     /* write thread td */
928         struct bio_queue_head   wthread_bioq;   /* logical buffer bioq */
929         hammer2_mtx_t           wthread_mtx;    /* interlock */
930         int                     wthread_destroy;/* termination sequencing */
931         uint32_t                flags;          /* cached cluster flags */
932 };
933
934 typedef struct hammer2_pfs hammer2_pfs_t;
935
936 #define HAMMER2_DIRTYCHAIN_WAITING      0x80000000
937 #define HAMMER2_DIRTYCHAIN_MASK         0x7FFFFFFF
938
939 #define HAMMER2_LWINPROG_WAITING        0x80000000
940 #define HAMMER2_LWINPROG_MASK           0x7FFFFFFF
941
942 /*
943  * Bulkscan
944  */
945 #define HAMMER2_BULK_ABORT      0x00000001
946
947 /*
948  * Misc
949  */
950 #if defined(_KERNEL)
951
952 MALLOC_DECLARE(M_HAMMER2);
953
954 #define VTOI(vp)        ((hammer2_inode_t *)(vp)->v_data)
955 #define ITOV(ip)        ((ip)->vp)
956
957 /*
958  * Currently locked chains retain the locked buffer cache buffer for
959  * indirect blocks, and indirect blocks can be one of two sizes.  The
960  * device buffer has to match the case to avoid deadlocking recursive
961  * chains that might otherwise try to access different offsets within
962  * the same device buffer.
963  */
964 static __inline
965 int
966 hammer2_devblkradix(int radix)
967 {
968 #if 0
969         if (radix <= HAMMER2_LBUFRADIX) {
970                 return (HAMMER2_LBUFRADIX);
971         } else {
972                 return (HAMMER2_PBUFRADIX);
973         }
974 #endif
975         return (HAMMER2_PBUFRADIX);
976 }
977
978 /*
979  * XXX almost time to remove this.  DIO uses PBUFSIZE exclusively now.
980  */
981 static __inline
982 size_t
983 hammer2_devblksize(size_t bytes)
984 {
985 #if 0
986         if (bytes <= HAMMER2_LBUFSIZE) {
987                 return(HAMMER2_LBUFSIZE);
988         } else {
989                 KKASSERT(bytes <= HAMMER2_PBUFSIZE &&
990                          (bytes ^ (bytes - 1)) == ((bytes << 1) - 1));
991                 return (HAMMER2_PBUFSIZE);
992         }
993 #endif
994         return (HAMMER2_PBUFSIZE);
995 }
996
997
998 static __inline
999 hammer2_pfs_t *
1000 MPTOPMP(struct mount *mp)
1001 {
1002         return ((hammer2_pfs_t *)mp->mnt_data);
1003 }
1004
1005 #define LOCKSTART       int __nlocks = curthread->td_locks
1006 #define LOCKENTER       (++curthread->td_locks)
1007 #define LOCKEXIT        (--curthread->td_locks)
1008 #define LOCKSTOP        KKASSERT(curthread->td_locks == __nlocks)
1009
1010 extern struct vop_ops hammer2_vnode_vops;
1011 extern struct vop_ops hammer2_spec_vops;
1012 extern struct vop_ops hammer2_fifo_vops;
1013
1014 extern int hammer2_debug;
1015 extern int hammer2_cluster_enable;
1016 extern int hammer2_hardlink_enable;
1017 extern int hammer2_flush_pipe;
1018 extern int hammer2_synchronous_flush;
1019 extern int hammer2_dio_count;
1020 extern long hammer2_limit_dirty_chains;
1021 extern long hammer2_iod_file_read;
1022 extern long hammer2_iod_meta_read;
1023 extern long hammer2_iod_indr_read;
1024 extern long hammer2_iod_fmap_read;
1025 extern long hammer2_iod_volu_read;
1026 extern long hammer2_iod_file_write;
1027 extern long hammer2_iod_meta_write;
1028 extern long hammer2_iod_indr_write;
1029 extern long hammer2_iod_fmap_write;
1030 extern long hammer2_iod_volu_write;
1031 extern long hammer2_ioa_file_read;
1032 extern long hammer2_ioa_meta_read;
1033 extern long hammer2_ioa_indr_read;
1034 extern long hammer2_ioa_fmap_read;
1035 extern long hammer2_ioa_volu_read;
1036 extern long hammer2_ioa_file_write;
1037 extern long hammer2_ioa_meta_write;
1038 extern long hammer2_ioa_indr_write;
1039 extern long hammer2_ioa_fmap_write;
1040 extern long hammer2_ioa_volu_write;
1041
1042 extern struct objcache *cache_buffer_read;
1043 extern struct objcache *cache_buffer_write;
1044
1045 extern int destroy;
1046 extern int write_thread_wakeup;
1047
1048 /*
1049  * hammer2_subr.c
1050  */
1051 #define hammer2_icrc32(buf, size)       iscsi_crc32((buf), (size))
1052 #define hammer2_icrc32c(buf, size, crc) iscsi_crc32_ext((buf), (size), (crc))
1053
1054 int hammer2_signal_check(time_t *timep);
1055 const char *hammer2_error_str(int error);
1056
1057 hammer2_cluster_t *hammer2_inode_lock(hammer2_inode_t *ip, int how);
1058 void hammer2_inode_unlock(hammer2_inode_t *ip, hammer2_cluster_t *cluster);
1059 hammer2_mtx_state_t hammer2_inode_lock_temp_release(hammer2_inode_t *ip);
1060 void hammer2_inode_lock_temp_restore(hammer2_inode_t *ip,
1061                         hammer2_mtx_state_t ostate);
1062 int hammer2_inode_lock_upgrade(hammer2_inode_t *ip);
1063 void hammer2_inode_lock_downgrade(hammer2_inode_t *ip, int);
1064
1065 void hammer2_dev_exlock(hammer2_dev_t *hmp);
1066 void hammer2_dev_shlock(hammer2_dev_t *hmp);
1067 void hammer2_dev_unlock(hammer2_dev_t *hmp);
1068
1069 int hammer2_get_dtype(const hammer2_inode_data_t *ipdata);
1070 int hammer2_get_vtype(const hammer2_inode_data_t *ipdata);
1071 u_int8_t hammer2_get_obj_type(enum vtype vtype);
1072 void hammer2_time_to_timespec(u_int64_t xtime, struct timespec *ts);
1073 u_int64_t hammer2_timespec_to_time(const struct timespec *ts);
1074 u_int32_t hammer2_to_unix_xid(const uuid_t *uuid);
1075 void hammer2_guid_to_uuid(uuid_t *uuid, u_int32_t guid);
1076 hammer2_xid_t hammer2_trans_newxid(hammer2_pfs_t *pmp);
1077 void hammer2_trans_manage_init(hammer2_trans_manage_t *tman);
1078
1079 hammer2_key_t hammer2_dirhash(const unsigned char *name, size_t len);
1080 int hammer2_getradix(size_t bytes);
1081
1082 int hammer2_calc_logical(hammer2_inode_t *ip, hammer2_off_t uoff,
1083                         hammer2_key_t *lbasep, hammer2_key_t *leofp);
1084 int hammer2_calc_physical(hammer2_inode_t *ip,
1085                         const hammer2_inode_data_t *ipdata,
1086                         hammer2_key_t lbase);
1087 void hammer2_update_time(uint64_t *timep);
1088 void hammer2_adjreadcounter(hammer2_blockref_t *bref, size_t bytes);
1089
1090 /*
1091  * hammer2_inode.c
1092  */
1093 struct vnode *hammer2_igetv(hammer2_inode_t *ip, hammer2_cluster_t *cparent,
1094                         int *errorp);
1095 hammer2_inode_t *hammer2_inode_lookup(hammer2_pfs_t *pmp,
1096                         hammer2_tid_t inum);
1097 hammer2_inode_t *hammer2_inode_get(hammer2_pfs_t *pmp,
1098                         hammer2_inode_t *dip, hammer2_cluster_t *cluster);
1099 void hammer2_inode_free(hammer2_inode_t *ip);
1100 void hammer2_inode_ref(hammer2_inode_t *ip);
1101 void hammer2_inode_drop(hammer2_inode_t *ip);
1102 void hammer2_inode_repoint(hammer2_inode_t *ip, hammer2_inode_t *pip,
1103                         hammer2_cluster_t *cluster);
1104 void hammer2_inode_repoint_one(hammer2_inode_t *ip, hammer2_cluster_t *cluster,
1105                         int idx);
1106
1107 void hammer2_run_unlinkq(hammer2_trans_t *trans, hammer2_pfs_t *pmp);
1108
1109 hammer2_inode_t *hammer2_inode_create(hammer2_trans_t *trans,
1110                         hammer2_inode_t *dip,
1111                         struct vattr *vap, struct ucred *cred,
1112                         const uint8_t *name, size_t name_len,
1113                         hammer2_cluster_t **clusterp,
1114                         int flags, int *errorp);
1115 int hammer2_inode_connect(hammer2_trans_t *trans,
1116                         hammer2_cluster_t **clusterp, int hlink,
1117                         hammer2_inode_t *dip, hammer2_cluster_t *dcluster,
1118                         const uint8_t *name, size_t name_len,
1119                         hammer2_key_t key);
1120 hammer2_inode_t *hammer2_inode_common_parent(hammer2_inode_t *fdip,
1121                         hammer2_inode_t *tdip);
1122 void hammer2_inode_fsync(hammer2_trans_t *trans, hammer2_inode_t *ip,
1123                         hammer2_cluster_t *cparent);
1124 int hammer2_unlink_file(hammer2_trans_t *trans, hammer2_inode_t *dip,
1125                         const uint8_t *name, size_t name_len, int isdir,
1126                         int *hlinkp, struct nchandle *nch, int nlinks);
1127 int hammer2_hardlink_consolidate(hammer2_trans_t *trans,
1128                         hammer2_inode_t *ip, hammer2_cluster_t **clusterp,
1129                         hammer2_inode_t *cdip, hammer2_cluster_t *cdcluster,
1130                         int nlinks);
1131 int hammer2_hardlink_deconsolidate(hammer2_trans_t *trans, hammer2_inode_t *dip,
1132                         hammer2_chain_t **chainp, hammer2_chain_t **ochainp);
1133 int hammer2_hardlink_find(hammer2_inode_t *dip, hammer2_cluster_t **cparentp,
1134                         hammer2_cluster_t **clusterp);
1135 int hammer2_parent_find(hammer2_cluster_t **cparentp,
1136                         hammer2_cluster_t *cluster);
1137 void hammer2_inode_install_hidden(hammer2_pfs_t *pmp);
1138
1139 /*
1140  * hammer2_chain.c
1141  */
1142 void hammer2_voldata_lock(hammer2_dev_t *hmp);
1143 void hammer2_voldata_unlock(hammer2_dev_t *hmp);
1144 void hammer2_voldata_modify(hammer2_dev_t *hmp);
1145 hammer2_chain_t *hammer2_chain_alloc(hammer2_dev_t *hmp,
1146                                 hammer2_pfs_t *pmp,
1147                                 hammer2_trans_t *trans,
1148                                 hammer2_blockref_t *bref);
1149 void hammer2_chain_core_init(hammer2_chain_t *chain);
1150 void hammer2_chain_ref(hammer2_chain_t *chain);
1151 void hammer2_chain_drop(hammer2_chain_t *chain);
1152 void hammer2_chain_lock(hammer2_chain_t *chain, int how);
1153 void hammer2_chain_load_data(hammer2_chain_t *chain);
1154 const hammer2_media_data_t *hammer2_chain_rdata(hammer2_chain_t *chain);
1155 hammer2_media_data_t *hammer2_chain_wdata(hammer2_chain_t *chain);
1156
1157 /*
1158  * hammer2_cluster.c
1159  */
1160 void hammer2_cluster_load_async(hammer2_cluster_t *cluster,
1161                                 void (*callback)(hammer2_iocb_t *iocb),
1162                                 void *ptr);
1163 void hammer2_chain_moved(hammer2_chain_t *chain);
1164 void hammer2_chain_modify(hammer2_trans_t *trans,
1165                                 hammer2_chain_t *chain, int flags);
1166 void hammer2_chain_resize(hammer2_trans_t *trans, hammer2_inode_t *ip,
1167                                 hammer2_chain_t *parent,
1168                                 hammer2_chain_t *chain,
1169                                 int nradix, int flags);
1170 void hammer2_chain_unlock(hammer2_chain_t *chain);
1171 void hammer2_chain_wait(hammer2_chain_t *chain);
1172 hammer2_chain_t *hammer2_chain_get(hammer2_chain_t *parent, int generation,
1173                                 hammer2_blockref_t *bref);
1174 hammer2_chain_t *hammer2_chain_lookup_init(hammer2_chain_t *parent, int flags);
1175 void hammer2_chain_lookup_done(hammer2_chain_t *parent);
1176 hammer2_chain_t *hammer2_chain_lookup(hammer2_chain_t **parentp,
1177                                 hammer2_key_t *key_nextp,
1178                                 hammer2_key_t key_beg, hammer2_key_t key_end,
1179                                 int *cache_indexp, int flags);
1180 hammer2_chain_t *hammer2_chain_next(hammer2_chain_t **parentp,
1181                                 hammer2_chain_t *chain,
1182                                 hammer2_key_t *key_nextp,
1183                                 hammer2_key_t key_beg, hammer2_key_t key_end,
1184                                 int *cache_indexp, int flags);
1185 hammer2_chain_t *hammer2_chain_scan(hammer2_chain_t *parent,
1186                                 hammer2_chain_t *chain,
1187                                 int *cache_indexp, int flags);
1188
1189 int hammer2_chain_create(hammer2_trans_t *trans, hammer2_chain_t **parentp,
1190                                 hammer2_chain_t **chainp,
1191                                 hammer2_pfs_t *pmp,
1192                                 hammer2_key_t key, int keybits,
1193                                 int type, size_t bytes, int flags);
1194 void hammer2_chain_rename(hammer2_trans_t *trans, hammer2_blockref_t *bref,
1195                                 hammer2_chain_t **parentp,
1196                                 hammer2_chain_t *chain, int flags);
1197 int hammer2_chain_snapshot(hammer2_trans_t *trans, hammer2_chain_t **chainp,
1198                                 hammer2_ioc_pfs_t *pfs);
1199 void hammer2_chain_delete(hammer2_trans_t *trans, hammer2_chain_t *parent,
1200                                 hammer2_chain_t *chain, int flags);
1201 void hammer2_chain_delete_duplicate(hammer2_trans_t *trans,
1202                                 hammer2_chain_t **chainp, int flags);
1203 void hammer2_flush(hammer2_trans_t *trans, hammer2_chain_t *chain, int istop);
1204 void hammer2_delayed_flush(hammer2_trans_t *trans, hammer2_chain_t *chain);
1205 void hammer2_chain_commit(hammer2_trans_t *trans, hammer2_chain_t *chain);
1206 void hammer2_chain_setflush(hammer2_trans_t *trans, hammer2_chain_t *chain);
1207 void hammer2_chain_countbrefs(hammer2_chain_t *chain,
1208                                 hammer2_blockref_t *base, int count);
1209
1210 void hammer2_chain_setcheck(hammer2_chain_t *chain, void *bdata);
1211 int hammer2_chain_testcheck(hammer2_chain_t *chain, void *bdata);
1212
1213
1214 void hammer2_pfs_memory_wait(hammer2_pfs_t *pmp);
1215 void hammer2_pfs_memory_inc(hammer2_pfs_t *pmp);
1216 void hammer2_pfs_memory_wakeup(hammer2_pfs_t *pmp);
1217
1218 void hammer2_base_delete(hammer2_trans_t *trans, hammer2_chain_t *chain,
1219                                 hammer2_blockref_t *base, int count,
1220                                 int *cache_indexp, hammer2_chain_t *child);
1221 void hammer2_base_insert(hammer2_trans_t *trans, hammer2_chain_t *chain,
1222                                 hammer2_blockref_t *base, int count,
1223                                 int *cache_indexp, hammer2_chain_t *child);
1224
1225 /*
1226  * hammer2_trans.c
1227  */
1228 void hammer2_trans_init(hammer2_trans_t *trans, hammer2_pfs_t *pmp,
1229                                 int flags);
1230 void hammer2_trans_done(hammer2_trans_t *trans);
1231 void hammer2_trans_assert_strategy(hammer2_pfs_t *pmp);
1232
1233 /*
1234  * hammer2_ioctl.c
1235  */
1236 int hammer2_ioctl(hammer2_inode_t *ip, u_long com, void *data,
1237                                 int fflag, struct ucred *cred);
1238
1239 /*
1240  * hammer2_io.c
1241  */
1242 void hammer2_io_putblk(hammer2_io_t **diop);
1243 void hammer2_io_cleanup(hammer2_dev_t *hmp, struct hammer2_io_tree *tree);
1244 char *hammer2_io_data(hammer2_io_t *dio, off_t lbase);
1245 void hammer2_io_getblk(hammer2_dev_t *hmp, off_t lbase, int lsize,
1246                                 hammer2_iocb_t *iocb);
1247 void hammer2_io_complete(hammer2_iocb_t *iocb);
1248 void hammer2_io_callback(struct bio *bio);
1249 void hammer2_iocb_wait(hammer2_iocb_t *iocb);
1250 int hammer2_io_new(hammer2_dev_t *hmp, off_t lbase, int lsize,
1251                                 hammer2_io_t **diop);
1252 int hammer2_io_newnz(hammer2_dev_t *hmp, off_t lbase, int lsize,
1253                                 hammer2_io_t **diop);
1254 int hammer2_io_newq(hammer2_dev_t *hmp, off_t lbase, int lsize,
1255                                 hammer2_io_t **diop);
1256 int hammer2_io_bread(hammer2_dev_t *hmp, off_t lbase, int lsize,
1257                                 hammer2_io_t **diop);
1258 void hammer2_io_bawrite(hammer2_io_t **diop);
1259 void hammer2_io_bdwrite(hammer2_io_t **diop);
1260 int hammer2_io_bwrite(hammer2_io_t **diop);
1261 int hammer2_io_isdirty(hammer2_io_t *dio);
1262 void hammer2_io_setdirty(hammer2_io_t *dio);
1263 void hammer2_io_setinval(hammer2_io_t *dio, u_int bytes);
1264 void hammer2_io_brelse(hammer2_io_t **diop);
1265 void hammer2_io_bqrelse(hammer2_io_t **diop);
1266
1267 /*
1268  * hammer2_msgops.c
1269  */
1270 int hammer2_msg_dbg_rcvmsg(kdmsg_msg_t *msg);
1271 int hammer2_msg_adhoc_input(kdmsg_msg_t *msg);
1272
1273 /*
1274  * hammer2_vfsops.c
1275  */
1276 void hammer2_clusterctl_wakeup(kdmsg_iocom_t *iocom);
1277 void hammer2_volconf_update(hammer2_dev_t *hmp, int index);
1278 void hammer2_dump_chain(hammer2_chain_t *chain, int tab, int *countp, char pfx);
1279 void hammer2_bioq_sync(hammer2_pfs_t *pmp);
1280 int hammer2_vfs_sync(struct mount *mp, int waitflags);
1281 hammer2_pfs_t *hammer2_pfsalloc(hammer2_cluster_t *cluster,
1282                                 const hammer2_inode_data_t *ripdata,
1283                                 hammer2_tid_t modify_tid);
1284
1285 void hammer2_lwinprog_ref(hammer2_pfs_t *pmp);
1286 void hammer2_lwinprog_drop(hammer2_pfs_t *pmp);
1287 void hammer2_lwinprog_wait(hammer2_pfs_t *pmp);
1288
1289 /*
1290  * hammer2_freemap.c
1291  */
1292 int hammer2_freemap_alloc(hammer2_trans_t *trans, hammer2_chain_t *chain,
1293                                 size_t bytes);
1294 void hammer2_freemap_adjust(hammer2_trans_t *trans, hammer2_dev_t *hmp,
1295                                 hammer2_blockref_t *bref, int how);
1296
1297 /*
1298  * hammer2_cluster.c
1299  */
1300 int hammer2_cluster_need_resize(hammer2_cluster_t *cluster, int bytes);
1301 uint8_t hammer2_cluster_type(hammer2_cluster_t *cluster);
1302 const hammer2_media_data_t *hammer2_cluster_rdata(hammer2_cluster_t *cluster);
1303 hammer2_media_data_t *hammer2_cluster_wdata(hammer2_cluster_t *cluster);
1304 hammer2_cluster_t *hammer2_cluster_from_chain(hammer2_chain_t *chain);
1305 int hammer2_cluster_modified(hammer2_cluster_t *cluster);
1306 int hammer2_cluster_duplicated(hammer2_cluster_t *cluster);
1307 void hammer2_cluster_bref(hammer2_cluster_t *cluster, hammer2_blockref_t *bref);
1308 void hammer2_cluster_setflush(hammer2_trans_t *trans,
1309                         hammer2_cluster_t *cluster);
1310 void hammer2_cluster_setmethod_check(hammer2_trans_t *trans,
1311                         hammer2_cluster_t *cluster, int check_algo);
1312 hammer2_cluster_t *hammer2_cluster_alloc(hammer2_pfs_t *pmp,
1313                         hammer2_trans_t *trans,
1314                         hammer2_blockref_t *bref);
1315 void hammer2_cluster_ref(hammer2_cluster_t *cluster);
1316 void hammer2_cluster_drop(hammer2_cluster_t *cluster);
1317 void hammer2_cluster_wait(hammer2_cluster_t *cluster);
1318 void hammer2_cluster_lock(hammer2_cluster_t *cluster, int how);
1319 void hammer2_cluster_lock_except(hammer2_cluster_t *cluster, int idx, int how);
1320 void hammer2_cluster_resolve(hammer2_cluster_t *cluster);
1321 void hammer2_cluster_forcegood(hammer2_cluster_t *cluster);
1322 hammer2_cluster_t *hammer2_cluster_copy(hammer2_cluster_t *ocluster);
1323 void hammer2_cluster_unlock(hammer2_cluster_t *cluster);
1324 void hammer2_cluster_unlock_except(hammer2_cluster_t *cluster, int idx);
1325 void hammer2_cluster_resize(hammer2_trans_t *trans, hammer2_inode_t *ip,
1326                         hammer2_cluster_t *cparent, hammer2_cluster_t *cluster,
1327                         int nradix, int flags);
1328 hammer2_inode_data_t *hammer2_cluster_modify_ip(hammer2_trans_t *trans,
1329                         hammer2_inode_t *ip, hammer2_cluster_t *cluster,
1330                         int flags);
1331 void hammer2_cluster_modify(hammer2_trans_t *trans, hammer2_cluster_t *cluster,
1332                         int flags);
1333 void hammer2_cluster_modsync(hammer2_cluster_t *cluster);
1334 hammer2_cluster_t *hammer2_cluster_lookup_init(hammer2_cluster_t *cparent,
1335                         int flags);
1336 void hammer2_cluster_lookup_done(hammer2_cluster_t *cparent);
1337 hammer2_cluster_t *hammer2_cluster_lookup(hammer2_cluster_t *cparent,
1338                         hammer2_key_t *key_nextp,
1339                         hammer2_key_t key_beg, hammer2_key_t key_end,
1340                         int flags);
1341 hammer2_cluster_t *hammer2_cluster_next(hammer2_cluster_t *cparent,
1342                         hammer2_cluster_t *cluster,
1343                         hammer2_key_t *key_nextp,
1344                         hammer2_key_t key_beg, hammer2_key_t key_end,
1345                         int flags);
1346 void hammer2_cluster_next_single_chain(hammer2_cluster_t *cparent,
1347                         hammer2_cluster_t *cluster,
1348                         hammer2_key_t *key_nextp,
1349                         hammer2_key_t key_beg,
1350                         hammer2_key_t key_end,
1351                         int i, int flags);
1352 hammer2_cluster_t *hammer2_cluster_scan(hammer2_cluster_t *cparent,
1353                         hammer2_cluster_t *cluster, int flags);
1354 int hammer2_cluster_create(hammer2_trans_t *trans, hammer2_cluster_t *cparent,
1355                         hammer2_cluster_t **clusterp,
1356                         hammer2_key_t key, int keybits,
1357                         int type, size_t bytes, int flags);
1358 void hammer2_cluster_rename(hammer2_trans_t *trans, hammer2_blockref_t *bref,
1359                         hammer2_cluster_t *cparent, hammer2_cluster_t *cluster,
1360                         int flags);
1361 void hammer2_cluster_delete(hammer2_trans_t *trans, hammer2_cluster_t *pcluster,
1362                         hammer2_cluster_t *cluster, int flags);
1363 int hammer2_cluster_snapshot(hammer2_trans_t *trans,
1364                         hammer2_cluster_t *ocluster, hammer2_ioc_pfs_t *pfs);
1365 hammer2_cluster_t *hammer2_cluster_parent(hammer2_cluster_t *cluster);
1366
1367 int hammer2_bulk_scan(hammer2_trans_t *trans, hammer2_chain_t *parent,
1368                         int (*func)(hammer2_chain_t *chain, void *info),
1369                         void *info);
1370 int hammer2_bulkfree_pass(hammer2_dev_t *hmp,
1371                         struct hammer2_ioc_bulkfree *bfi);
1372
1373 /*
1374  * hammer2_iocom.c
1375  */
1376 void hammer2_iocom_init(hammer2_dev_t *hmp);
1377 void hammer2_iocom_uninit(hammer2_dev_t *hmp);
1378 void hammer2_cluster_reconnect(hammer2_dev_t *hmp, struct file *fp);
1379
1380 /*
1381  * hammer2_syncthr.c
1382  */
1383 void hammer2_syncthr_create(hammer2_syncthr_t *thr, hammer2_pfs_t *pmp,
1384                         int clindex, void (*func)(void *arg));
1385 void hammer2_syncthr_delete(hammer2_syncthr_t *thr);
1386 void hammer2_syncthr_remaster(hammer2_syncthr_t *thr);
1387 void hammer2_syncthr_freeze(hammer2_syncthr_t *thr);
1388 void hammer2_syncthr_unfreeze(hammer2_syncthr_t *thr);
1389 void hammer2_syncthr_primary(void *arg);
1390
1391 /*
1392  * hammer2_strategy.c
1393  */
1394 int hammer2_vop_strategy(struct vop_strategy_args *ap);
1395 int hammer2_vop_bmap(struct vop_bmap_args *ap);
1396
1397 #endif /* !_KERNEL */
1398 #endif /* !_VFS_HAMMER2_HAMMER2_H_ */