hammer2 - Fix infinite flush recursion, reduce bulkfree console spam
[dragonfly.git] / sys / vfs / hammer2 / hammer2.h
1 /*
2  * Copyright (c) 2011-2016 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_xxhash.h"
94 #include "hammer2_disk.h"
95 #include "hammer2_mount.h"
96 #include "hammer2_ioctl.h"
97
98 struct hammer2_io;
99 struct hammer2_iocb;
100 struct hammer2_chain;
101 struct hammer2_cluster;
102 struct hammer2_inode;
103 struct hammer2_dev;
104 struct hammer2_pfs;
105 struct hammer2_span;
106 struct hammer2_state;
107 struct hammer2_msg;
108 struct hammer2_thread;
109 union hammer2_xop;
110
111 /*
112  * Mutex and lock shims.  Hammer2 requires support for asynchronous and
113  * abortable locks, and both exclusive and shared spinlocks.  Normal
114  * synchronous non-abortable locks can be substituted for spinlocks.
115  */
116 typedef mtx_t                           hammer2_mtx_t;
117 typedef mtx_link_t                      hammer2_mtx_link_t;
118 typedef mtx_state_t                     hammer2_mtx_state_t;
119
120 typedef struct spinlock                 hammer2_spin_t;
121
122 #define hammer2_mtx_ex                  mtx_lock_ex_quick
123 #define hammer2_mtx_sh                  mtx_lock_sh_quick
124 #define hammer2_mtx_sh_again            mtx_lock_sh_again
125 #define hammer2_mtx_unlock              mtx_unlock
126 #define hammer2_mtx_downgrade           mtx_downgrade
127 #define hammer2_mtx_owned               mtx_owned
128 #define hammer2_mtx_init                mtx_init
129 #define hammer2_mtx_temp_release        mtx_lock_temp_release
130 #define hammer2_mtx_temp_restore        mtx_lock_temp_restore
131 #define hammer2_mtx_refs                mtx_lockrefs
132
133 #define hammer2_spin_init               spin_init
134 #define hammer2_spin_sh                 spin_lock_shared
135 #define hammer2_spin_ex                 spin_lock
136 #define hammer2_spin_unsh               spin_unlock_shared
137 #define hammer2_spin_unex               spin_unlock
138
139 TAILQ_HEAD(hammer2_xop_list, hammer2_xop_head);
140 TAILQ_HEAD(hammer2_chain_list, hammer2_chain);
141
142 typedef struct hammer2_xop_list hammer2_xop_list_t;
143
144
145 /*
146  * General lock support
147  */
148 static __inline
149 int
150 hammer2_mtx_upgrade_try(hammer2_mtx_t *mtx)
151 {
152         return mtx_upgrade_try(mtx);
153 }
154
155 /*
156  * The xid tracks internal transactional updates.
157  *
158  * XXX fix-me, really needs to be 64-bits
159  */
160 typedef uint32_t hammer2_xid_t;
161
162 #define HAMMER2_XID_MIN 0x00000000U
163 #define HAMMER2_XID_MAX 0x7FFFFFFFU
164
165 /*
166  * The chain structure tracks a portion of the media topology from the
167  * root (volume) down.  Chains represent volumes, inodes, indirect blocks,
168  * data blocks, and freemap nodes and leafs.
169  *
170  * The chain structure utilizes a simple singly-homed topology and the
171  * chain's in-memory topology will move around as the chains do, due mainly
172  * to renames and indirect block creation.
173  *
174  * Block Table Updates
175  *
176  *      Block table updates for insertions and updates are delayed until the
177  *      flush.  This allows us to avoid having to modify the parent chain
178  *      all the way to the root.
179  *
180  *      Block table deletions are performed immediately (modifying the parent
181  *      in the process) because the flush code uses the chain structure to
182  *      track delayed updates and the chain will be (likely) gone or moved to
183  *      another location in the topology after a deletion.
184  *
185  *      A prior iteration of the code tried to keep the relationship intact
186  *      on deletes by doing a delete-duplicate operation on the chain, but
187  *      it added way too much complexity to the codebase.
188  *
189  * Flush Synchronization
190  *
191  *      The flush code must flush modified chains bottom-up.  Because chain
192  *      structures can shift around and are NOT topologically stable,
193  *      modified chains are independently indexed for the flush.  As the flush
194  *      runs it modifies (or further modifies) and updates the parents,
195  *      propagating the flush all the way to the volume root.
196  *
197  *      Modifying front-end operations can occur during a flush but will block
198  *      in two cases: (1) when the front-end tries to operate on the inode
199  *      currently in the midst of being flushed and (2) if the front-end
200  *      crosses an inode currently being flushed (such as during a rename).
201  *      So, for example, if you rename directory "x" to "a/b/c/d/e/f/g/x" and
202  *      the flusher is currently working on "a/b/c", the rename will block
203  *      temporarily in order to ensure that "x" exists in one place or the
204  *      other.
205  *
206  *      Meta-data statistics are updated by the flusher.  The front-end will
207  *      make estimates but meta-data must be fully synchronized only during a
208  *      flush in order to ensure that it remains correct across a crash.
209  *
210  *      Multiple flush synchronizations can theoretically be in-flight at the
211  *      same time but the implementation is not coded to handle the case and
212  *      currently serializes them.
213  *
214  * Snapshots:
215  *
216  *      Snapshots currently require the subdirectory tree being snapshotted
217  *      to be flushed.  The snapshot then creates a new super-root inode which
218  *      copies the flushed blockdata of the directory or file that was
219  *      snapshotted.
220  *
221  * RBTREE NOTES:
222  *
223  *      - Note that the radix tree runs in powers of 2 only so sub-trees
224  *        cannot straddle edges.
225  */
226 RB_HEAD(hammer2_chain_tree, hammer2_chain);
227 TAILQ_HEAD(h2_flush_list, hammer2_chain);
228 TAILQ_HEAD(h2_core_list, hammer2_chain);
229 TAILQ_HEAD(h2_iocb_list, hammer2_iocb);
230
231 #define CHAIN_CORE_DELETE_BMAP_ENTRIES  \
232         (HAMMER2_PBUFSIZE / sizeof(hammer2_blockref_t) / sizeof(uint32_t))
233
234 /*
235  * Core topology for chain (embedded in chain).  Protected by a spinlock.
236  */
237 struct hammer2_chain_core {
238         hammer2_spin_t  spin;
239         struct hammer2_chain_tree rbtree; /* sub-chains */
240         int             live_zero;      /* blockref array opt */
241         u_int           live_count;     /* live (not deleted) chains in tree */
242         u_int           chain_count;    /* live + deleted chains under core */
243         int             generation;     /* generation number (inserts only) */
244 };
245
246 typedef struct hammer2_chain_core hammer2_chain_core_t;
247
248 RB_HEAD(hammer2_io_tree, hammer2_io);
249
250 /*
251  * IOCB - IO callback (into chain, cluster, or manual request)
252  */
253 struct hammer2_iocb {
254         TAILQ_ENTRY(hammer2_iocb) entry;
255         void (*callback)(struct hammer2_iocb *iocb);
256         struct hammer2_io       *dio;
257         struct hammer2_cluster  *cluster;
258         struct hammer2_chain    *chain;
259         void                    *ptr;
260         off_t                   lbase;
261         int                     lsize;
262         uint32_t                flags;
263         int                     error;
264         int                     btype;
265 };
266
267 typedef struct hammer2_iocb hammer2_iocb_t;
268
269 #define HAMMER2_IOCB_INTERLOCK  0x00000001
270 #define HAMMER2_IOCB_ONQ        0x00000002
271 #define HAMMER2_IOCB_DONE       0x00000004
272 #define HAMMER2_IOCB_INPROG     0x00000008
273 #define HAMMER2_IOCB_UNUSED10   0x00000010
274 #define HAMMER2_IOCB_QUICK      0x00010000
275 #define HAMMER2_IOCB_ZERO       0x00020000
276 #define HAMMER2_IOCB_READ       0x00040000
277 #define HAMMER2_IOCB_WAKEUP     0x00080000
278
279 /*
280  * DIO - Management structure wrapping system buffer cache.
281  *
282  *       Used for multiple purposes including concurrent management
283  *       if small requests by chains into larger DIOs.
284  */
285 struct hammer2_io {
286         RB_ENTRY(hammer2_io) rbnode;    /* indexed by device offset */
287         struct h2_iocb_list iocbq;
288         struct spinlock spin;
289         struct hammer2_dev *hmp;
290         struct buf      *bp;
291         uint64_t        crc_good_mask;
292         off_t           pbase;
293         int             psize;
294         int             refs;
295         int             act;            /* activity */
296         int             btype;          /* approximate BREF_TYPE_* */
297 };
298
299 typedef struct hammer2_io hammer2_io_t;
300
301 #define HAMMER2_DIO_INPROG      0x80000000      /* bio in progress */
302 #define HAMMER2_DIO_GOOD        0x40000000      /* dio->bp is stable */
303 #define HAMMER2_DIO_WAITING     0x20000000      /* wait for inprog clr */
304 #define HAMMER2_DIO_DIRTY       0x10000000      /* flush on last drop */
305
306 #define HAMMER2_DIO_MASK        0x0FFFFFFF
307
308 /*
309  * Primary chain structure keeps track of the topology in-memory.
310  */
311 struct hammer2_chain {
312         hammer2_mtx_t           lock;
313         hammer2_chain_core_t    core;
314         RB_ENTRY(hammer2_chain) rbnode;         /* live chain(s) */
315         hammer2_blockref_t      bref;
316         struct hammer2_chain    *parent;
317         struct hammer2_state    *state;         /* if active cache msg */
318         struct hammer2_dev      *hmp;
319         struct hammer2_pfs      *pmp;           /* A PFS or super-root (spmp) */
320
321         hammer2_io_t    *dio;                   /* physical data buffer */
322         u_int           bytes;                  /* physical data size */
323         u_int           flags;
324         u_int           refs;
325         u_int           lockcnt;
326         int             error;                  /* on-lock data error state */
327         int             persist_refs;           /* (aka ip->cluster) */
328
329         hammer2_media_data_t *data;             /* data pointer shortcut */
330         TAILQ_ENTRY(hammer2_chain) flush_node;  /* flush list */
331         TAILQ_ENTRY(hammer2_chain) lru_node;    /* 0-refs LRU */
332 };
333
334 typedef struct hammer2_chain hammer2_chain_t;
335
336 int hammer2_chain_cmp(hammer2_chain_t *chain1, hammer2_chain_t *chain2);
337 RB_PROTOTYPE(hammer2_chain_tree, hammer2_chain, rbnode, hammer2_chain_cmp);
338
339 /*
340  * Special notes on flags:
341  *
342  * INITIAL      - This flag allows a chain to be created and for storage to
343  *                be allocated without having to immediately instantiate the
344  *                related buffer.  The data is assumed to be all-zeros.  It
345  *                is primarily used for indirect blocks.
346  *
347  * MODIFIED     - The chain's media data has been modified.  Prevents chain
348  *                free on lastdrop if still in the topology.
349  *
350  * UPDATE       - Chain might not be modified but parent blocktable needs
351  *                an update.  Prevents chain free on lastdrop if still in
352  *                the topology.
353  *
354  * FICTITIOUS   - Faked chain as a placeholder for an error condition.  This
355  *                chain is unsuitable for I/O.
356  *
357  * BMAPPED      - Indicates that the chain is present in the parent blockmap.
358  *
359  * BMAPUPD      - Indicates that the chain is present but needs to be updated
360  *                in the parent blockmap.
361  */
362 #define HAMMER2_CHAIN_MODIFIED          0x00000001      /* dirty chain data */
363 #define HAMMER2_CHAIN_ALLOCATED         0x00000002      /* kmalloc'd chain */
364 #define HAMMER2_CHAIN_DESTROY           0x00000004
365 #define HAMMER2_CHAIN_DEDUP             0x00000008      /* used as dedup src */
366 #define HAMMER2_CHAIN_DELETED           0x00000010      /* deleted chain */
367 #define HAMMER2_CHAIN_INITIAL           0x00000020      /* initial create */
368 #define HAMMER2_CHAIN_UPDATE            0x00000040      /* need parent update */
369 #define HAMMER2_CHAIN_DEFERRED          0x00000080      /* flush depth defer */
370 #define HAMMER2_CHAIN_TESTEDGOOD        0x00000100      /* crc tested good */
371 #define HAMMER2_CHAIN_ONFLUSH           0x00000200      /* on a flush list */
372 #define HAMMER2_CHAIN_FICTITIOUS        0x00000400      /* unsuitable for I/O */
373 #define HAMMER2_CHAIN_VOLUMESYNC        0x00000800      /* needs volume sync */
374 #define HAMMER2_CHAIN_DELAYED           0x00001000      /* delayed flush */
375 #define HAMMER2_CHAIN_COUNTEDBREFS      0x00002000      /* block table stats */
376 #define HAMMER2_CHAIN_ONRBTREE          0x00004000      /* on parent RB tree */
377 #define HAMMER2_CHAIN_ONLRU             0x00008000      /* on LRU list */
378 #define HAMMER2_CHAIN_EMBEDDED          0x00010000      /* embedded data */
379 #define HAMMER2_CHAIN_RELEASE           0x00020000      /* don't keep around */
380 #define HAMMER2_CHAIN_BMAPPED           0x00040000      /* present in blkmap */
381 #define HAMMER2_CHAIN_BMAPUPD           0x00080000      /* +needs updating */
382 #define HAMMER2_CHAIN_IOINPROG          0x00100000      /* I/O interlock */
383 #define HAMMER2_CHAIN_IOSIGNAL          0x00200000      /* I/O interlock */
384 #define HAMMER2_CHAIN_PFSBOUNDARY       0x00400000      /* super->pfs inode */
385
386 #define HAMMER2_CHAIN_FLUSH_MASK        (HAMMER2_CHAIN_MODIFIED |       \
387                                          HAMMER2_CHAIN_UPDATE |         \
388                                          HAMMER2_CHAIN_ONFLUSH |        \
389                                          HAMMER2_CHAIN_DESTROY)
390
391 /*
392  * Hammer2 error codes, used by chain->error and cluster->error.  The error
393  * code is typically set on-lock unless no I/O was requested, and set on
394  * I/O otherwise.  If set for a cluster it generally means that the cluster
395  * code could not find a valid copy to present.
396  *
397  * IO           - An I/O error occurred
398  * CHECK        - I/O succeeded but did not match the check code
399  * INCOMPLETE   - A cluster is not complete enough to use, or
400  *                a chain cannot be loaded because its parent has an error.
401  *
402  * NOTE: API allows callers to check zero/non-zero to determine if an error
403  *       condition exists.
404  *
405  * NOTE: Chain's data field is usually NULL on an IO error but not necessarily
406  *       NULL on other errors.  Check chain->error, not chain->data.
407  */
408 #define HAMMER2_ERROR_NONE              0
409 #define HAMMER2_ERROR_IO                1       /* device I/O error */
410 #define HAMMER2_ERROR_CHECK             2       /* check code mismatch */
411 #define HAMMER2_ERROR_INCOMPLETE        3       /* incomplete cluster */
412 #define HAMMER2_ERROR_DEPTH             4       /* temporary depth limit */
413
414 /*
415  * Flags passed to hammer2_chain_lookup() and hammer2_chain_next()
416  *
417  * NOTES:
418  *      NOLOCK      - Input and output chains are referenced only and not
419  *                    locked.  Output chain might be temporarily locked
420  *                    internally.
421  *
422  *      NODATA      - Asks that the chain->data not be resolved in order
423  *                    to avoid I/O.
424  *
425  *      NODIRECT    - Prevents a lookup of offset 0 in an inode from returning
426  *                    the inode itself if the inode is in DIRECTDATA mode
427  *                    (i.e. file is <= 512 bytes).  Used by the synchronization
428  *                    code to prevent confusion.
429  *
430  *      SHARED      - The input chain is expected to be locked shared,
431  *                    and the output chain is locked shared.
432  *
433  *      MATCHIND    - Allows an indirect block / freemap node to be returned
434  *                    when the passed key range matches the radix.  Remember
435  *                    that key_end is inclusive (e.g. {0x000,0xFFF},
436  *                    not {0x000,0x1000}).
437  *
438  *                    (Cannot be used for remote or cluster ops).
439  *
440  *      ALLNODES    - Allows NULL focus.
441  *
442  *      ALWAYS      - Always resolve the data.  If ALWAYS and NODATA are both
443  *                    missing, bulk file data is not resolved but inodes and
444  *                    other meta-data will.
445  *
446  *      NOUNLOCK    - Used by hammer2_chain_next() to leave the lock on
447  *                    the input chain intact.  The chain is still dropped.
448  *                    This allows the caller to add a reference to the chain
449  *                    and retain it in a locked state (used by the
450  *                    XOP/feed/collect code).
451  */
452 #define HAMMER2_LOOKUP_NOLOCK           0x00000001      /* ref only */
453 #define HAMMER2_LOOKUP_NODATA           0x00000002      /* data left NULL */
454 #define HAMMER2_LOOKUP_NODIRECT         0x00000004      /* no offset=0 DD */
455 #define HAMMER2_LOOKUP_SHARED           0x00000100
456 #define HAMMER2_LOOKUP_MATCHIND         0x00000200      /* return all chains */
457 #define HAMMER2_LOOKUP_ALLNODES         0x00000400      /* allow NULL focus */
458 #define HAMMER2_LOOKUP_ALWAYS           0x00000800      /* resolve data */
459 #define HAMMER2_LOOKUP_NOUNLOCK         0x00001000      /* leave lock intact */
460
461 /*
462  * Flags passed to hammer2_chain_modify() and hammer2_chain_resize()
463  *
464  * NOTE: OPTDATA allows us to avoid instantiating buffers for INDIRECT
465  *       blocks in the INITIAL-create state.
466  */
467 #define HAMMER2_MODIFY_OPTDATA          0x00000002      /* data can be NULL */
468 #define HAMMER2_MODIFY_NO_MODIFY_TID    0x00000004
469 #define HAMMER2_MODIFY_UNUSED0008       0x00000008
470
471 /*
472  * Flags passed to hammer2_chain_lock()
473  *
474  * NOTE: RDONLY is set to optimize cluster operations when *no* modifications
475  *       will be made to either the cluster being locked or any underlying
476  *       cluster.  It allows the cluster to lock and access data for a subset
477  *       of available nodes instead of all available nodes.
478  */
479 #define HAMMER2_RESOLVE_NEVER           1
480 #define HAMMER2_RESOLVE_MAYBE           2
481 #define HAMMER2_RESOLVE_ALWAYS          3
482 #define HAMMER2_RESOLVE_MASK            0x0F
483
484 #define HAMMER2_RESOLVE_SHARED          0x10    /* request shared lock */
485 #define HAMMER2_RESOLVE_UNUSED20        0x20
486 #define HAMMER2_RESOLVE_RDONLY          0x40    /* higher level op flag */
487
488 /*
489  * Flags passed to hammer2_chain_delete()
490  */
491 #define HAMMER2_DELETE_PERMANENT        0x0001
492
493 /*
494  * Flags passed to hammer2_chain_insert() or hammer2_chain_rename()
495  */
496 #define HAMMER2_INSERT_PFSROOT          0x0004
497
498 /*
499  * Flags passed to hammer2_chain_delete_duplicate()
500  */
501 #define HAMMER2_DELDUP_RECORE           0x0001
502
503 /*
504  * Cluster different types of storage together for allocations
505  */
506 #define HAMMER2_FREECACHE_INODE         0
507 #define HAMMER2_FREECACHE_INDIR         1
508 #define HAMMER2_FREECACHE_DATA          2
509 #define HAMMER2_FREECACHE_UNUSED3       3
510 #define HAMMER2_FREECACHE_TYPES         4
511
512 /*
513  * hammer2_freemap_alloc() block preference
514  */
515 #define HAMMER2_OFF_NOPREF              ((hammer2_off_t)-1)
516
517 /*
518  * BMAP read-ahead maximum parameters
519  */
520 #define HAMMER2_BMAP_COUNT              16      /* max bmap read-ahead */
521 #define HAMMER2_BMAP_BYTES              (HAMMER2_PBUFSIZE * HAMMER2_BMAP_COUNT)
522
523 /*
524  * hammer2_freemap_adjust()
525  */
526 #define HAMMER2_FREEMAP_DORECOVER       1
527 #define HAMMER2_FREEMAP_DOMAYFREE       2
528 #define HAMMER2_FREEMAP_DOREALFREE      3
529
530 /*
531  * HAMMER2 cluster - A set of chains representing the same entity.
532  *
533  * hammer2_cluster typically represents a temporary set of representitive
534  * chains.  The one exception is that a hammer2_cluster is embedded in
535  * hammer2_inode.  This embedded cluster is ONLY used to track the
536  * representitive chains and cannot be directly locked.
537  *
538  * A cluster is usually temporary (and thus per-thread) for locking purposes,
539  * allowing us to embed the asynchronous storage required for cluster
540  * operations in the cluster itself and adjust the state and status without
541  * having to worry too much about SMP issues.
542  *
543  * The exception is the cluster embedded in the hammer2_inode structure.
544  * This is used to cache the cluster state on an inode-by-inode basis.
545  * Individual hammer2_chain structures not incorporated into clusters might
546  * also stick around to cache miscellanious elements.
547  *
548  * Because the cluster is a 'working copy' and is usually subject to cluster
549  * quorum rules, it is quite possible for us to end up with an insufficient
550  * number of live chains to execute an operation.  If an insufficient number
551  * of chains remain in a working copy, the operation may have to be
552  * downgraded, retried, stall until the requisit number of chains are
553  * available, or possibly even error out depending on the mount type.
554  *
555  * A cluster's focus is set when it is locked.  The focus can only be set
556  * to a chain still part of the synchronized set.
557  */
558 #define HAMMER2_MAXCLUSTER      8
559 #define HAMMER2_XOPMASK_CLUSTER ((1U << HAMMER2_MAXCLUSTER) - 1)
560 #define HAMMER2_XOPFIFO         16
561 #define HAMMER2_XOPFIFO_MASK    (HAMMER2_XOPFIFO - 1)
562 #define HAMMER2_XOPGROUPS       16
563 #define HAMMER2_XOPGROUPS_MASK  (HAMMER2_XOPGROUPS - 1)
564 #define HAMMER2_XOPMASK_VOP     0x80000000U
565 #define HAMMER2_XOPMASK_FIFOW   0x40000000U
566
567 #define HAMMER2_XOPMASK_ALLDONE (HAMMER2_XOPMASK_VOP | HAMMER2_XOPMASK_CLUSTER)
568
569 #define HAMMER2_SPECTHREADS     1       /* sync */
570
571 struct hammer2_cluster_item {
572         hammer2_chain_t         *chain;
573         int                     cache_index;
574         int                     error;
575         uint32_t                flags;
576 };
577
578 typedef struct hammer2_cluster_item hammer2_cluster_item_t;
579
580 /*
581  * INVALID      - Invalid for focus, i.e. not part of synchronized set.
582  *                Once set, this bit is sticky across operations.
583  *
584  * FEMOD        - Indicates that front-end modifying operations can
585  *                mess with this entry and MODSYNC will copy also
586  *                effect it.
587  */
588 #define HAMMER2_CITEM_INVALID   0x00000001
589 #define HAMMER2_CITEM_FEMOD     0x00000002
590 #define HAMMER2_CITEM_NULL      0x00000004
591
592 struct hammer2_cluster {
593         int                     refs;           /* track for deallocation */
594         int                     ddflag;
595         struct hammer2_pfs      *pmp;
596         uint32_t                flags;
597         int                     nchains;
598         int                     error;          /* error code valid on lock */
599         int                     focus_index;
600         hammer2_iocb_t          iocb;
601         hammer2_chain_t         *focus;         /* current focus (or mod) */
602         hammer2_cluster_item_t  array[HAMMER2_MAXCLUSTER];
603 };
604
605 typedef struct hammer2_cluster  hammer2_cluster_t;
606
607 /*
608  * WRHARD       - Hard mounts can write fully synchronized
609  * RDHARD       - Hard mounts can read fully synchronized
610  * UNHARD       - Unsynchronized masters present
611  * NOHARD       - No masters visible
612  * WRSOFT       - Soft mounts can write to at least the SOFT_MASTER
613  * RDSOFT       - Soft mounts can read from at least a SOFT_SLAVE
614  * UNSOFT       - Unsynchronized slaves present
615  * NOSOFT       - No slaves visible
616  * RDSLAVE      - slaves are accessible (possibly unsynchronized or remote).
617  * MSYNCED      - All masters are fully synchronized
618  * SSYNCED      - All known local slaves are fully synchronized to masters
619  *
620  * All available masters are always incorporated.  All PFSs belonging to a
621  * cluster (master, slave, copy, whatever) always try to synchronize the
622  * total number of known masters in the PFSs root inode.
623  *
624  * A cluster might have access to many slaves, copies, or caches, but we
625  * have a limited number of cluster slots.  Any such elements which are
626  * directly mounted from block device(s) will always be incorporated.   Note
627  * that SSYNCED only applies to such elements which are directly mounted,
628  * not to any remote slaves, copies, or caches that could be available.  These
629  * bits are used to monitor and drive our synchronization threads.
630  *
631  * When asking the question 'is any data accessible at all', then a simple
632  * test against (RDHARD|RDSOFT|RDSLAVE) gives you the answer.  If any of
633  * these bits are set the object can be read with certain caveats:
634  * RDHARD - no caveats.  RDSOFT - authoritative but might not be synchronized.
635  * and RDSLAVE - not authoritative, has some data but it could be old or
636  * incomplete.
637  *
638  * When both soft and hard mounts are available, data will be read and written
639  * via the soft mount only.  But all might be in the cluster because
640  * background synchronization threads still need to do their work.
641  */
642 #define HAMMER2_CLUSTER_INODE   0x00000001      /* embedded in inode struct */
643 #define HAMMER2_CLUSTER_UNUSED2 0x00000002
644 #define HAMMER2_CLUSTER_LOCKED  0x00000004      /* cluster lks not recursive */
645 #define HAMMER2_CLUSTER_WRHARD  0x00000100      /* hard-mount can write */
646 #define HAMMER2_CLUSTER_RDHARD  0x00000200      /* hard-mount can read */
647 #define HAMMER2_CLUSTER_UNHARD  0x00000400      /* unsynchronized masters */
648 #define HAMMER2_CLUSTER_NOHARD  0x00000800      /* no masters visible */
649 #define HAMMER2_CLUSTER_WRSOFT  0x00001000      /* soft-mount can write */
650 #define HAMMER2_CLUSTER_RDSOFT  0x00002000      /* soft-mount can read */
651 #define HAMMER2_CLUSTER_UNSOFT  0x00004000      /* unsynchronized slaves */
652 #define HAMMER2_CLUSTER_NOSOFT  0x00008000      /* no slaves visible */
653 #define HAMMER2_CLUSTER_MSYNCED 0x00010000      /* all masters synchronized */
654 #define HAMMER2_CLUSTER_SSYNCED 0x00020000      /* known slaves synchronized */
655
656 #define HAMMER2_CLUSTER_ANYDATA ( HAMMER2_CLUSTER_RDHARD |      \
657                                   HAMMER2_CLUSTER_RDSOFT |      \
658                                   HAMMER2_CLUSTER_RDSLAVE)
659
660 #define HAMMER2_CLUSTER_RDOK    ( HAMMER2_CLUSTER_RDHARD |      \
661                                   HAMMER2_CLUSTER_RDSOFT)
662
663 #define HAMMER2_CLUSTER_WROK    ( HAMMER2_CLUSTER_WRHARD |      \
664                                   HAMMER2_CLUSTER_WRSOFT)
665
666 #define HAMMER2_CLUSTER_ZFLAGS  ( HAMMER2_CLUSTER_WRHARD |      \
667                                   HAMMER2_CLUSTER_RDHARD |      \
668                                   HAMMER2_CLUSTER_WRSOFT |      \
669                                   HAMMER2_CLUSTER_RDSOFT |      \
670                                   HAMMER2_CLUSTER_MSYNCED |     \
671                                   HAMMER2_CLUSTER_SSYNCED)
672
673 /*
674  * Helper functions (cluster must be locked for flags to be valid).
675  */
676 static __inline
677 int
678 hammer2_cluster_rdok(hammer2_cluster_t *cluster)
679 {
680         return (cluster->flags & HAMMER2_CLUSTER_RDOK);
681 }
682
683 static __inline
684 int
685 hammer2_cluster_wrok(hammer2_cluster_t *cluster)
686 {
687         return (cluster->flags & HAMMER2_CLUSTER_WROK);
688 }
689
690 RB_HEAD(hammer2_inode_tree, hammer2_inode);
691
692 /*
693  * A hammer2 inode.
694  *
695  * NOTE: The inode-embedded cluster is never used directly for I/O (since
696  *       it may be shared).  Instead it will be replicated-in and synchronized
697  *       back out if changed.
698  */
699 struct hammer2_inode {
700         RB_ENTRY(hammer2_inode) rbnode;         /* inumber lookup (HL) */
701         hammer2_mtx_t           lock;           /* inode lock */
702         hammer2_mtx_t           truncate_lock;  /* prevent truncates */
703         struct hammer2_pfs      *pmp;           /* PFS mount */
704         struct hammer2_inode    *pip;           /* parent inode */
705         struct vnode            *vp;
706         struct spinlock         cluster_spin;   /* update cluster */
707         hammer2_cluster_t       cluster;
708         struct lockf            advlock;
709         u_int                   flags;
710         u_int                   refs;           /* +vpref, +flushref */
711         uint8_t                 comp_heuristic;
712         hammer2_inode_meta_t    meta;           /* copy of meta-data */
713         hammer2_off_t           osize;
714         hammer2_cluster_t       *cluster_cache;
715 };
716
717 typedef struct hammer2_inode hammer2_inode_t;
718
719 /*
720  * MODIFIED     - Inode is in a modified state, ip->meta may have changes.
721  * RESIZED      - Inode truncated (any) or inode extended beyond
722  *                EMBEDDED_BYTES.
723  */
724 #define HAMMER2_INODE_MODIFIED          0x0001
725 #define HAMMER2_INODE_SROOT             0x0002  /* kmalloc special case */
726 #define HAMMER2_INODE_RENAME_INPROG     0x0004
727 #define HAMMER2_INODE_ONRBTREE          0x0008
728 #define HAMMER2_INODE_RESIZED           0x0010  /* requires inode_fsync */
729 #define HAMMER2_INODE_ISDELETED         0x0020  /* deleted, not in ihidden */
730 #define HAMMER2_INODE_ISUNLINKED        0x0040
731 #define HAMMER2_INODE_METAGOOD          0x0080  /* inode meta-data good */
732 #define HAMMER2_INODE_ONSIDEQ           0x0100  /* on side processing queue */
733
734 int hammer2_inode_cmp(hammer2_inode_t *ip1, hammer2_inode_t *ip2);
735 RB_PROTOTYPE2(hammer2_inode_tree, hammer2_inode, rbnode, hammer2_inode_cmp,
736                 hammer2_tid_t);
737
738 /*
739  * inode-unlink side-structure
740  */
741 struct hammer2_inode_sideq {
742         TAILQ_ENTRY(hammer2_inode_sideq) entry;
743         hammer2_inode_t *ip;
744 };
745 TAILQ_HEAD(h2_sideq_list, hammer2_inode_sideq);
746
747 typedef struct hammer2_inode_sideq hammer2_inode_sideq_t;
748
749 /*
750  * Transaction management sub-structure under hammer2_pfs
751  */
752 struct hammer2_trans {
753         uint32_t                flags;
754         uint32_t                sync_wait;
755 };
756
757 typedef struct hammer2_trans hammer2_trans_t;
758
759 #define HAMMER2_TRANS_ISFLUSH           0x80000000      /* flush code */
760 #define HAMMER2_TRANS_BUFCACHE          0x40000000      /* bio strategy */
761 #define HAMMER2_TRANS_PREFLUSH          0x20000000      /* preflush state */
762 #define HAMMER2_TRANS_FPENDING          0x10000000      /* flush pending */
763 #define HAMMER2_TRANS_WAITING           0x08000000      /* someone waiting */
764 #define HAMMER2_TRANS_MASK              0x00FFFFFF      /* count mask */
765
766 #define HAMMER2_FREEMAP_HEUR_NRADIX     4       /* pwr 2 PBUFRADIX-MINIORADIX */
767 #define HAMMER2_FREEMAP_HEUR_TYPES      8
768 #define HAMMER2_FREEMAP_HEUR_SIZE       (HAMMER2_FREEMAP_HEUR_NRADIX * \
769                                          HAMMER2_FREEMAP_HEUR_TYPES)
770
771 #define HAMMER2_DEDUP_HEUR_SIZE         65536
772 #define HAMMER2_DEDUP_HEUR_MASK         (HAMMER2_DEDUP_HEUR_SIZE - 1)
773
774 #define HAMMER2_FLUSH_TOP               0x0001
775 #define HAMMER2_FLUSH_ALL               0x0002
776
777
778 /*
779  * Hammer2 support thread element.
780  *
781  * Potentially many support threads can hang off of hammer2, primarily
782  * off the hammer2_pfs structure.  Typically:
783  *
784  * td x Nodes                   A synchronization thread for each node.
785  * td x Nodes x workers         Worker threads for frontend operations.
786  * td x 1                       Bioq thread for logical buffer writes.
787  *
788  * In addition, the synchronization thread(s) associated with the
789  * super-root PFS (spmp) for a node is responsible for automatic bulkfree
790  * and dedup scans.
791  */
792 struct hammer2_thread {
793         struct hammer2_pfs *pmp;
794         hammer2_xop_list_t *xopq;       /* points into pmp->xopq[] */
795         thread_t        td;
796         uint32_t        flags;
797         int             depth;
798         int             clindex;        /* cluster element index */
799         int             repidx;
800 };
801
802 typedef struct hammer2_thread hammer2_thread_t;
803
804 #define HAMMER2_THREAD_UNMOUNTING       0x0001  /* unmount request */
805 #define HAMMER2_THREAD_DEV              0x0002  /* related to dev, not pfs */
806 #define HAMMER2_THREAD_WAITING          0x0004  /* thread in idle tsleep */
807 #define HAMMER2_THREAD_REMASTER         0x0008  /* remaster request */
808 #define HAMMER2_THREAD_STOP             0x0010  /* exit request */
809 #define HAMMER2_THREAD_FREEZE           0x0020  /* force idle */
810 #define HAMMER2_THREAD_FROZEN           0x0040  /* thread is frozen */
811 #define HAMMER2_THREAD_XOPQ             0x0080  /* work pending */
812 #define HAMMER2_THREAD_STOPPED          0x0100  /* thread has stopped */
813 #define HAMMER2_THREAD_UNFREEZE         0x0200
814 #define HAMMER2_THREAD_CLIENTWAIT       0x0400
815
816 #define HAMMER2_THREAD_WAKEUP_MASK      (HAMMER2_THREAD_UNMOUNTING |    \
817                                          HAMMER2_THREAD_REMASTER |      \
818                                          HAMMER2_THREAD_STOP |          \
819                                          HAMMER2_THREAD_FREEZE |        \
820                                          HAMMER2_THREAD_XOPQ)
821
822 /*
823  * Support structure for dedup heuristic.
824  */
825 struct hammer2_dedup {
826         hammer2_off_t   data_off;
827         uint64_t        data_crc;
828         uint32_t        ticks;
829         uint32_t        unused03;
830 };
831
832 typedef struct hammer2_dedup hammer2_dedup_t;
833
834 /*
835  * hammer2_xop - container for VOP/XOP operation (allocated, not on stack).
836  *
837  * This structure is used to distribute a VOP operation across multiple
838  * nodes.  It provides a rendezvous for concurrent node execution and
839  * can be detached from the frontend operation to allow the frontend to
840  * return early.
841  *
842  * This structure also sequences operations on up to three inodes.
843  */
844 typedef void (*hammer2_xop_func_t)(union hammer2_xop *xop, int clidx);
845
846 struct hammer2_xop_fifo {
847         TAILQ_ENTRY(hammer2_xop_head) entry;
848         hammer2_chain_t         *array[HAMMER2_XOPFIFO];
849         int                     errors[HAMMER2_XOPFIFO];
850         int                     ri;
851         int                     wi;
852         int                     flags;
853 };
854
855 typedef struct hammer2_xop_fifo hammer2_xop_fifo_t;
856
857 #define HAMMER2_XOP_FIFO_RUN    0x0001
858 #define HAMMER2_XOP_FIFO_STALL  0x0002
859
860 struct hammer2_xop_head {
861         hammer2_xop_func_t      func;
862         hammer2_tid_t           mtid;
863         struct hammer2_inode    *ip1;
864         struct hammer2_inode    *ip2;
865         struct hammer2_inode    *ip3;
866         uint32_t                check_counter;
867         uint32_t                run_mask;
868         uint32_t                chk_mask;
869         int                     flags;
870         int                     state;
871         int                     error;
872         hammer2_key_t           collect_key;
873         char                    *name1;
874         size_t                  name1_len;
875         char                    *name2;
876         size_t                  name2_len;
877         hammer2_xop_fifo_t      collect[HAMMER2_MAXCLUSTER];
878         hammer2_cluster_t       cluster;        /* help collections */
879 };
880
881 typedef struct hammer2_xop_head hammer2_xop_head_t;
882
883 struct hammer2_xop_ipcluster {
884         hammer2_xop_head_t      head;
885 };
886
887 struct hammer2_xop_strategy {
888         hammer2_xop_head_t      head;
889         hammer2_key_t           lbase;
890         int                     finished;
891         hammer2_mtx_t           lock;
892         struct bio              *bio;
893 };
894
895 struct hammer2_xop_readdir {
896         hammer2_xop_head_t      head;
897         hammer2_key_t           lkey;
898 };
899
900 struct hammer2_xop_nresolve {
901         hammer2_xop_head_t      head;
902         hammer2_key_t           lhc;    /* if name is NULL used lhc */
903 };
904
905 struct hammer2_xop_nlink {
906         hammer2_xop_head_t      head;
907         int                     nlinks_delta;
908 };
909
910 struct hammer2_xop_unlink {
911         hammer2_xop_head_t      head;
912         int                     isdir;
913         int                     dopermanent;
914 };
915
916 struct hammer2_xop_nrename {
917         hammer2_xop_head_t      head;
918         hammer2_tid_t           lhc;
919         int                     ip_key;
920 };
921
922 struct hammer2_xop_scanlhc {
923         hammer2_xop_head_t      head;
924         hammer2_key_t           lhc;
925 };
926
927 struct hammer2_xop_scanall {
928         hammer2_xop_head_t      head;
929         hammer2_key_t           key_beg;        /* inclusive */
930         hammer2_key_t           key_end;        /* inclusive */
931         int                     resolve_flags;
932         int                     lookup_flags;
933 };
934
935 struct hammer2_xop_lookup {
936         hammer2_xop_head_t      head;
937         hammer2_key_t           lhc;
938 };
939
940 struct hammer2_xop_create {
941         hammer2_xop_head_t      head;
942         hammer2_inode_meta_t    meta;           /* initial metadata */
943         hammer2_key_t           lhc;
944         int                     flags;
945 };
946
947 struct hammer2_xop_destroy {
948         hammer2_xop_head_t      head;
949 };
950
951 struct hammer2_xop_fsync {
952         hammer2_xop_head_t      head;
953         hammer2_inode_meta_t    meta;
954         hammer2_off_t           osize;
955         u_int                   ipflags;
956         int                     clear_directdata;
957 };
958
959 struct hammer2_xop_unlinkall {
960         hammer2_xop_head_t      head;
961         hammer2_key_t           key_beg;
962         hammer2_key_t           key_end;
963 };
964
965 struct hammer2_xop_connect {
966         hammer2_xop_head_t      head;
967         hammer2_key_t           lhc;
968 };
969
970 struct hammer2_xop_flush {
971         hammer2_xop_head_t      head;
972 };
973
974 typedef struct hammer2_xop_readdir hammer2_xop_readdir_t;
975 typedef struct hammer2_xop_nresolve hammer2_xop_nresolve_t;
976 typedef struct hammer2_xop_nlink hammer2_xop_nlink_t;
977 typedef struct hammer2_xop_unlink hammer2_xop_unlink_t;
978 typedef struct hammer2_xop_nrename hammer2_xop_nrename_t;
979 typedef struct hammer2_xop_ipcluster hammer2_xop_ipcluster_t;
980 typedef struct hammer2_xop_strategy hammer2_xop_strategy_t;
981 typedef struct hammer2_xop_create hammer2_xop_create_t;
982 typedef struct hammer2_xop_destroy hammer2_xop_destroy_t;
983 typedef struct hammer2_xop_fsync hammer2_xop_fsync_t;
984 typedef struct hammer2_xop_unlinkall hammer2_xop_unlinkall_t;
985 typedef struct hammer2_xop_scanlhc hammer2_xop_scanlhc_t;
986 typedef struct hammer2_xop_scanall hammer2_xop_scanall_t;
987 typedef struct hammer2_xop_lookup hammer2_xop_lookup_t;
988 typedef struct hammer2_xop_connect hammer2_xop_connect_t;
989 typedef struct hammer2_xop_flush hammer2_xop_flush_t;
990
991 union hammer2_xop {
992         hammer2_xop_head_t      head;
993         hammer2_xop_ipcluster_t xop_ipcluster;
994         hammer2_xop_readdir_t   xop_readdir;
995         hammer2_xop_nresolve_t  xop_nresolve;
996         hammer2_xop_nlink_t     xop_nlink;
997         hammer2_xop_unlink_t    xop_unlink;
998         hammer2_xop_nrename_t   xop_nrename;
999         hammer2_xop_strategy_t  xop_strategy;
1000         hammer2_xop_create_t    xop_create;
1001         hammer2_xop_destroy_t   xop_destroy;
1002         hammer2_xop_fsync_t     xop_fsync;
1003         hammer2_xop_unlinkall_t xop_unlinkall;
1004         hammer2_xop_scanlhc_t   xop_scanlhc;
1005         hammer2_xop_scanall_t   xop_scanall;
1006         hammer2_xop_lookup_t    xop_lookup;
1007         hammer2_xop_flush_t     xop_flush;
1008         hammer2_xop_connect_t   xop_connect;
1009 };
1010
1011 typedef union hammer2_xop hammer2_xop_t;
1012
1013 /*
1014  * hammer2_xop_group - Manage XOP support threads.
1015  */
1016 struct hammer2_xop_group {
1017         hammer2_thread_t        thrs[HAMMER2_MAXCLUSTER];
1018 };
1019
1020 typedef struct hammer2_xop_group hammer2_xop_group_t;
1021
1022 /*
1023  * flags to hammer2_xop_collect()
1024  */
1025 #define HAMMER2_XOP_COLLECT_NOWAIT      0x00000001
1026 #define HAMMER2_XOP_COLLECT_WAITALL     0x00000002
1027
1028 /*
1029  * flags to hammer2_xop_alloc()
1030  *
1031  * MODIFYING    - This is a modifying transaction, allocate a mtid.
1032  */
1033 #define HAMMER2_XOP_MODIFYING           0x00000001
1034 #define HAMMER2_XOP_ITERATOR            0x00000002
1035
1036 /*
1037  * Global (per partition) management structure, represents a hard block
1038  * device.  Typically referenced by hammer2_chain structures when applicable.
1039  * Typically not used for network-managed elements.
1040  *
1041  * Note that a single hammer2_dev can be indirectly tied to multiple system
1042  * mount points.  There is no direct relationship.  System mounts are
1043  * per-cluster-id, not per-block-device, and a single hard mount might contain
1044  * many PFSs and those PFSs might combine together in various ways to form
1045  * the set of available clusters.
1046  */
1047 struct hammer2_dev {
1048         struct vnode    *devvp;         /* device vnode */
1049         int             ronly;          /* read-only mount */
1050         int             mount_count;    /* number of actively mounted PFSs */
1051         TAILQ_ENTRY(hammer2_dev) mntentry; /* hammer2_mntlist */
1052
1053         struct malloc_type *mchain;
1054         int             nipstacks;
1055         int             maxipstacks;
1056         kdmsg_iocom_t   iocom;          /* volume-level dmsg interface */
1057         struct spinlock io_spin;        /* iotree access */
1058         struct hammer2_io_tree iotree;
1059         int             iofree_count;
1060         hammer2_chain_t vchain;         /* anchor chain (topology) */
1061         hammer2_chain_t fchain;         /* anchor chain (freemap) */
1062         struct spinlock list_spin;
1063         struct h2_flush_list    flushq; /* flush seeds */
1064         struct hammer2_pfs *spmp;       /* super-root pmp for transactions */
1065         struct lock     bulklk;         /* bulkfree lock */
1066         struct lock     vollk;          /* lockmgr lock */
1067         hammer2_off_t   heur_freemap[HAMMER2_FREEMAP_HEUR_SIZE];
1068         hammer2_dedup_t heur_dedup[HAMMER2_DEDUP_HEUR_SIZE];
1069         int             volhdrno;       /* last volhdrno written */
1070         int             hflags;         /* HMNT2 flags applicable to device */
1071         char            devrepname[64]; /* for kprintf */
1072         hammer2_volume_data_t voldata;
1073         hammer2_volume_data_t volsync;  /* synchronized voldata */
1074 };
1075
1076 typedef struct hammer2_dev hammer2_dev_t;
1077
1078 /*
1079  * Helper functions (cluster must be locked for flags to be valid).
1080  */
1081 static __inline
1082 int
1083 hammer2_chain_rdok(hammer2_chain_t *chain)
1084 {
1085         return (chain->error == 0);
1086 }
1087
1088 static __inline
1089 int
1090 hammer2_chain_wrok(hammer2_chain_t *chain)
1091 {
1092         return (chain->error == 0 && chain->hmp->ronly == 0);
1093 }
1094
1095 /*
1096  * Per-cluster management structure.  This structure will be tied to a
1097  * system mount point if the system is mounting the PFS, but is also used
1098  * to manage clusters encountered during the super-root scan or received
1099  * via LNK_SPANs that might not be mounted.
1100  *
1101  * This structure is also used to represent the super-root that hangs off
1102  * of a hard mount point.  The super-root is not really a cluster element.
1103  * In this case the spmp_hmp field will be non-NULL.  It's just easier to do
1104  * this than to special case super-root manipulation in the hammer2_chain*
1105  * code as being only hammer2_dev-related.
1106  *
1107  * pfs_mode and pfs_nmasters are rollup fields which critically describes
1108  * how elements of the cluster act on the cluster.  pfs_mode is only applicable
1109  * when a PFS is mounted by the system.  pfs_nmasters is our best guess as to
1110  * how many masters have been configured for a cluster and is always
1111  * applicable.  pfs_types[] is an array with 1:1 correspondance to the
1112  * iroot cluster and describes the PFS types of the nodes making up the
1113  * cluster.
1114  *
1115  * WARNING! Portions of this structure have deferred initialization.  In
1116  *          particular, if not mounted there will be no ihidden or wthread.
1117  *          umounted network PFSs will also be missing iroot and numerous
1118  *          other fields will not be initialized prior to mount.
1119  *
1120  *          Synchronization threads are chain-specific and only applicable
1121  *          to local hard PFS entries.  A hammer2_pfs structure may contain
1122  *          more than one when multiple hard PFSs are present on the local
1123  *          machine which require synchronization monitoring.  Most PFSs
1124  *          (such as snapshots) are 1xMASTER PFSs which do not need a
1125  *          synchronization thread.
1126  *
1127  * WARNING! The chains making up pfs->iroot's cluster are accounted for in
1128  *          hammer2_dev->mount_count when the pfs is associated with a mount
1129  *          point.
1130  */
1131 struct hammer2_pfs {
1132         struct mount            *mp;
1133         TAILQ_ENTRY(hammer2_pfs) mntentry;      /* hammer2_pfslist */
1134         uuid_t                  pfs_clid;
1135         hammer2_dev_t           *spmp_hmp;      /* only if super-root pmp */
1136         hammer2_dev_t           *force_local;   /* only if 'local' mount */
1137         hammer2_inode_t         *iroot;         /* PFS root inode */
1138         hammer2_inode_t         *ihidden;       /* PFS hidden directory */
1139         uint8_t                 pfs_types[HAMMER2_MAXCLUSTER];
1140         char                    *pfs_names[HAMMER2_MAXCLUSTER];
1141         hammer2_dev_t           *pfs_hmps[HAMMER2_MAXCLUSTER];
1142         hammer2_trans_t         trans;
1143         struct lock             lock;           /* PFS lock for certain ops */
1144         struct lock             lock_nlink;     /* rename and nlink lock */
1145         struct netexport        export;         /* nfs export */
1146         int                     ronly;          /* read-only mount */
1147         int                     hflags;         /* pfs-specific mount flags */
1148         struct malloc_type      *minode;
1149         struct malloc_type      *mmsg;
1150         struct spinlock         inum_spin;      /* inumber lookup */
1151         struct hammer2_inode_tree inum_tree;    /* (not applicable to spmp) */
1152         struct spinlock         lru_spin;       /* inumber lookup */
1153         struct hammer2_chain_list lru_list;     /* chains on LRU */
1154         int                     lru_count;      /* #of chains on LRU */
1155         hammer2_tid_t           modify_tid;     /* modify transaction id */
1156         hammer2_tid_t           inode_tid;      /* inode allocator */
1157         uint8_t                 pfs_nmasters;   /* total masters */
1158         uint8_t                 pfs_mode;       /* operating mode PFSMODE */
1159         uint8_t                 unused01;
1160         uint8_t                 unused02;
1161         int                     xop_iterator;
1162         long                    inmem_inodes;
1163         uint32_t                inmem_dirty_chains;
1164         int                     count_lwinprog; /* logical write in prog */
1165         struct spinlock         list_spin;
1166         struct h2_sideq_list    sideq;          /* last-close dirty/unlink */
1167         hammer2_thread_t        sync_thrs[HAMMER2_MAXCLUSTER];
1168         uint32_t                cluster_flags;  /* cached cluster flags */
1169         int                     has_xop_threads;
1170         struct spinlock         xop_spin;       /* xop sequencer */
1171         hammer2_xop_group_t     xop_groups[HAMMER2_XOPGROUPS];
1172         hammer2_xop_list_t      xopq[HAMMER2_MAXCLUSTER][HAMMER2_XOPGROUPS+
1173                                                          HAMMER2_SPECTHREADS];
1174 };
1175
1176 typedef struct hammer2_pfs hammer2_pfs_t;
1177
1178 #define HAMMER2_LRU_LIMIT               1024    /* per pmp lru_list */
1179
1180 #define HAMMER2_DIRTYCHAIN_WAITING      0x80000000
1181 #define HAMMER2_DIRTYCHAIN_MASK         0x7FFFFFFF
1182
1183 #define HAMMER2_LWINPROG_WAITING        0x80000000
1184 #define HAMMER2_LWINPROG_WAITING0       0x40000000
1185 #define HAMMER2_LWINPROG_MASK           0x3FFFFFFF
1186
1187 /*
1188  * hammer2_cluster_check
1189  */
1190 #define HAMMER2_CHECK_NULL      0x00000001
1191
1192 /*
1193  * Bulkscan
1194  */
1195 #define HAMMER2_BULK_ABORT      0x00000001
1196
1197 /*
1198  * Misc
1199  */
1200 #if defined(_KERNEL)
1201
1202 MALLOC_DECLARE(M_HAMMER2);
1203
1204 #define VTOI(vp)        ((hammer2_inode_t *)(vp)->v_data)
1205 #define ITOV(ip)        ((ip)->vp)
1206
1207 /*
1208  * Currently locked chains retain the locked buffer cache buffer for
1209  * indirect blocks, and indirect blocks can be one of two sizes.  The
1210  * device buffer has to match the case to avoid deadlocking recursive
1211  * chains that might otherwise try to access different offsets within
1212  * the same device buffer.
1213  */
1214 static __inline
1215 int
1216 hammer2_devblkradix(int radix)
1217 {
1218 #if 0
1219         if (radix <= HAMMER2_LBUFRADIX) {
1220                 return (HAMMER2_LBUFRADIX);
1221         } else {
1222                 return (HAMMER2_PBUFRADIX);
1223         }
1224 #endif
1225         return (HAMMER2_PBUFRADIX);
1226 }
1227
1228 /*
1229  * XXX almost time to remove this.  DIO uses PBUFSIZE exclusively now.
1230  */
1231 static __inline
1232 size_t
1233 hammer2_devblksize(size_t bytes)
1234 {
1235 #if 0
1236         if (bytes <= HAMMER2_LBUFSIZE) {
1237                 return(HAMMER2_LBUFSIZE);
1238         } else {
1239                 KKASSERT(bytes <= HAMMER2_PBUFSIZE &&
1240                          (bytes ^ (bytes - 1)) == ((bytes << 1) - 1));
1241                 return (HAMMER2_PBUFSIZE);
1242         }
1243 #endif
1244         return (HAMMER2_PBUFSIZE);
1245 }
1246
1247
1248 static __inline
1249 hammer2_pfs_t *
1250 MPTOPMP(struct mount *mp)
1251 {
1252         return ((hammer2_pfs_t *)mp->mnt_data);
1253 }
1254
1255 #define LOCKSTART       int __nlocks = curthread->td_locks
1256 #define LOCKENTER       (++curthread->td_locks)
1257 #define LOCKEXIT        (--curthread->td_locks)
1258 #define LOCKSTOP        KKASSERT(curthread->td_locks == __nlocks)
1259
1260 extern struct vop_ops hammer2_vnode_vops;
1261 extern struct vop_ops hammer2_spec_vops;
1262 extern struct vop_ops hammer2_fifo_vops;
1263
1264 extern int hammer2_debug;
1265 extern int hammer2_cluster_enable;
1266 extern int hammer2_hardlink_enable;
1267 extern int hammer2_flush_pipe;
1268 extern int hammer2_synchronous_flush;
1269 extern int hammer2_dio_count;
1270 extern long hammer2_chain_allocs;
1271 extern long hammer2_chain_frees;
1272 extern long hammer2_limit_dirty_chains;
1273 extern long hammer2_count_modified_chains;
1274 extern long hammer2_iod_file_read;
1275 extern long hammer2_iod_meta_read;
1276 extern long hammer2_iod_indr_read;
1277 extern long hammer2_iod_fmap_read;
1278 extern long hammer2_iod_volu_read;
1279 extern long hammer2_iod_file_write;
1280 extern long hammer2_iod_file_wembed;
1281 extern long hammer2_iod_file_wzero;
1282 extern long hammer2_iod_file_wdedup;
1283 extern long hammer2_iod_meta_write;
1284 extern long hammer2_iod_indr_write;
1285 extern long hammer2_iod_fmap_write;
1286 extern long hammer2_iod_volu_write;
1287 extern long hammer2_ioa_file_read;
1288 extern long hammer2_ioa_meta_read;
1289 extern long hammer2_ioa_indr_read;
1290 extern long hammer2_ioa_fmap_read;
1291 extern long hammer2_ioa_volu_read;
1292 extern long hammer2_ioa_file_write;
1293 extern long hammer2_ioa_meta_write;
1294 extern long hammer2_ioa_indr_write;
1295 extern long hammer2_ioa_fmap_write;
1296 extern long hammer2_ioa_volu_write;
1297
1298 extern long hammer2_check_xxhash64;
1299 extern long hammer2_check_icrc32;
1300
1301 extern struct objcache *cache_buffer_read;
1302 extern struct objcache *cache_buffer_write;
1303 extern struct objcache *cache_xops;
1304
1305 /*
1306  * hammer2_subr.c
1307  */
1308 #define hammer2_icrc32(buf, size)       iscsi_crc32((buf), (size))
1309 #define hammer2_icrc32c(buf, size, crc) iscsi_crc32_ext((buf), (size), (crc))
1310
1311 int hammer2_signal_check(time_t *timep);
1312 const char *hammer2_error_str(int error);
1313
1314 void hammer2_inode_lock(hammer2_inode_t *ip, int how);
1315 void hammer2_inode_unlock(hammer2_inode_t *ip);
1316 hammer2_chain_t *hammer2_inode_chain(hammer2_inode_t *ip, int clindex, int how);
1317 hammer2_chain_t *hammer2_inode_chain_and_parent(hammer2_inode_t *ip,
1318                         int clindex, hammer2_chain_t **parentp, int how);
1319 hammer2_mtx_state_t hammer2_inode_lock_temp_release(hammer2_inode_t *ip);
1320 void hammer2_inode_lock_temp_restore(hammer2_inode_t *ip,
1321                         hammer2_mtx_state_t ostate);
1322 int hammer2_inode_lock_upgrade(hammer2_inode_t *ip);
1323 void hammer2_inode_lock_downgrade(hammer2_inode_t *ip, int);
1324
1325 void hammer2_dev_exlock(hammer2_dev_t *hmp);
1326 void hammer2_dev_shlock(hammer2_dev_t *hmp);
1327 void hammer2_dev_unlock(hammer2_dev_t *hmp);
1328
1329 int hammer2_get_dtype(const hammer2_inode_data_t *ipdata);
1330 int hammer2_get_vtype(uint8_t type);
1331 uint8_t hammer2_get_obj_type(enum vtype vtype);
1332 void hammer2_time_to_timespec(uint64_t xtime, struct timespec *ts);
1333 uint64_t hammer2_timespec_to_time(const struct timespec *ts);
1334 uint32_t hammer2_to_unix_xid(const uuid_t *uuid);
1335 void hammer2_guid_to_uuid(uuid_t *uuid, uint32_t guid);
1336 void hammer2_trans_manage_init(hammer2_pfs_t *pmp);
1337
1338 hammer2_key_t hammer2_dirhash(const unsigned char *name, size_t len);
1339 int hammer2_getradix(size_t bytes);
1340
1341 int hammer2_calc_logical(hammer2_inode_t *ip, hammer2_off_t uoff,
1342                         hammer2_key_t *lbasep, hammer2_key_t *leofp);
1343 int hammer2_calc_physical(hammer2_inode_t *ip, hammer2_key_t lbase);
1344 void hammer2_update_time(uint64_t *timep);
1345 void hammer2_adjreadcounter(hammer2_blockref_t *bref, size_t bytes);
1346
1347 /*
1348  * hammer2_inode.c
1349  */
1350 struct vnode *hammer2_igetv(hammer2_inode_t *ip, int *errorp);
1351 hammer2_inode_t *hammer2_inode_lookup(hammer2_pfs_t *pmp,
1352                         hammer2_tid_t inum);
1353 hammer2_inode_t *hammer2_inode_get(hammer2_pfs_t *pmp, hammer2_inode_t *dip,
1354                         hammer2_cluster_t *cluster, int idx);
1355 void hammer2_inode_free(hammer2_inode_t *ip);
1356 void hammer2_inode_ref(hammer2_inode_t *ip);
1357 void hammer2_inode_drop(hammer2_inode_t *ip);
1358 void hammer2_inode_repoint(hammer2_inode_t *ip, hammer2_inode_t *pip,
1359                         hammer2_cluster_t *cluster);
1360 void hammer2_inode_repoint_one(hammer2_inode_t *ip, hammer2_cluster_t *cluster,
1361                         int idx);
1362 void hammer2_inode_modify(hammer2_inode_t *ip);
1363 void hammer2_inode_run_sideq(hammer2_pfs_t *pmp);
1364
1365 hammer2_inode_t *hammer2_inode_create(hammer2_inode_t *dip,
1366                         struct vattr *vap, struct ucred *cred,
1367                         const uint8_t *name, size_t name_len, hammer2_key_t lhc,
1368                         hammer2_key_t inum, uint8_t type, uint8_t target_type,
1369                         int flags, int *errorp);
1370 int hammer2_inode_connect(hammer2_inode_t *dip, hammer2_inode_t *ip,
1371                         const char *name, size_t name_len,
1372                         hammer2_key_t lhc);
1373 hammer2_inode_t *hammer2_inode_common_parent(hammer2_inode_t *fdip,
1374                         hammer2_inode_t *tdip, int *errorp, int ishardlink);
1375 void hammer2_inode_chain_sync(hammer2_inode_t *ip);
1376 int hammer2_inode_unlink_finisher(hammer2_inode_t *ip, int isopen);
1377 void hammer2_inode_install_hidden(hammer2_pfs_t *pmp);
1378
1379 /*
1380  * hammer2_chain.c
1381  */
1382 void hammer2_voldata_lock(hammer2_dev_t *hmp);
1383 void hammer2_voldata_unlock(hammer2_dev_t *hmp);
1384 void hammer2_voldata_modify(hammer2_dev_t *hmp);
1385 hammer2_chain_t *hammer2_chain_alloc(hammer2_dev_t *hmp,
1386                                 hammer2_pfs_t *pmp,
1387                                 hammer2_blockref_t *bref);
1388 void hammer2_chain_core_init(hammer2_chain_t *chain);
1389 void hammer2_chain_ref(hammer2_chain_t *chain);
1390 void hammer2_chain_drop(hammer2_chain_t *chain);
1391 void hammer2_chain_lock(hammer2_chain_t *chain, int how);
1392 void hammer2_chain_lock_downgrade(hammer2_chain_t *chain);
1393 void hammer2_chain_push_shared_lock(hammer2_chain_t *chain);
1394 void hammer2_chain_pull_shared_lock(hammer2_chain_t *chain);
1395 void hammer2_chain_load_data(hammer2_chain_t *chain);
1396 const hammer2_media_data_t *hammer2_chain_rdata(hammer2_chain_t *chain);
1397 hammer2_media_data_t *hammer2_chain_wdata(hammer2_chain_t *chain);
1398 int hammer2_chain_snapshot(hammer2_chain_t *chain, hammer2_ioc_pfs_t *pmp,
1399                                 hammer2_tid_t mtid);
1400
1401 int hammer2_chain_hardlink_find(hammer2_inode_t *dip,
1402                                 hammer2_chain_t **parentp,
1403                                 hammer2_chain_t **chainp,
1404                                 int flags);
1405 void hammer2_chain_modify(hammer2_chain_t *chain, hammer2_tid_t mtid,
1406                                 hammer2_off_t dedup_off, int flags);
1407 void hammer2_chain_modify_ip(hammer2_inode_t *ip, hammer2_chain_t *chain,
1408                                 hammer2_tid_t mtid, int flags);
1409 void hammer2_chain_resize(hammer2_inode_t *ip, hammer2_chain_t *parent,
1410                                 hammer2_chain_t *chain,
1411                                 hammer2_tid_t mtid, hammer2_off_t dedup_off,
1412                                 int nradix, int flags);
1413 void hammer2_chain_unlock(hammer2_chain_t *chain);
1414 void hammer2_chain_wait(hammer2_chain_t *chain);
1415 hammer2_chain_t *hammer2_chain_get(hammer2_chain_t *parent, int generation,
1416                                 hammer2_blockref_t *bref);
1417 hammer2_chain_t *hammer2_chain_lookup_init(hammer2_chain_t *parent, int flags);
1418 void hammer2_chain_lookup_done(hammer2_chain_t *parent);
1419 hammer2_chain_t *hammer2_chain_getparent(hammer2_chain_t **parentp, int how);
1420 hammer2_chain_t *hammer2_chain_lookup(hammer2_chain_t **parentp,
1421                                 hammer2_key_t *key_nextp,
1422                                 hammer2_key_t key_beg, hammer2_key_t key_end,
1423                                 int *cache_indexp, int flags);
1424 hammer2_chain_t *hammer2_chain_next(hammer2_chain_t **parentp,
1425                                 hammer2_chain_t *chain,
1426                                 hammer2_key_t *key_nextp,
1427                                 hammer2_key_t key_beg, hammer2_key_t key_end,
1428                                 int *cache_indexp, int flags);
1429 hammer2_blockref_t *hammer2_chain_scan(hammer2_chain_t *parent,
1430                                 hammer2_chain_t **chainp,
1431                                 hammer2_blockref_t *bref,
1432                                 int *firstp, int *cache_indexp, int flags);
1433
1434 int hammer2_chain_create(hammer2_chain_t **parentp,
1435                                 hammer2_chain_t **chainp, hammer2_pfs_t *pmp,
1436                                 hammer2_key_t key, int keybits,
1437                                 int type, size_t bytes, hammer2_tid_t mtid,
1438                                 hammer2_off_t dedup_off, int flags);
1439 void hammer2_chain_rename(hammer2_blockref_t *bref,
1440                                 hammer2_chain_t **parentp,
1441                                 hammer2_chain_t *chain,
1442                                 hammer2_tid_t mtid, int flags);
1443 void hammer2_chain_delete(hammer2_chain_t *parent, hammer2_chain_t *chain,
1444                                 hammer2_tid_t mtid, int flags);
1445 void hammer2_chain_setflush(hammer2_chain_t *chain);
1446 void hammer2_chain_countbrefs(hammer2_chain_t *chain,
1447                                 hammer2_blockref_t *base, int count);
1448 hammer2_chain_t *hammer2_chain_bulksnap(hammer2_chain_t *chain);
1449 void hammer2_chain_bulkdrop(hammer2_chain_t *copy);
1450
1451 void hammer2_chain_setcheck(hammer2_chain_t *chain, void *bdata);
1452 int hammer2_chain_testcheck(hammer2_chain_t *chain, void *bdata);
1453
1454
1455 void hammer2_pfs_memory_wait(hammer2_pfs_t *pmp);
1456 void hammer2_pfs_memory_inc(hammer2_pfs_t *pmp);
1457 void hammer2_pfs_memory_wakeup(hammer2_pfs_t *pmp);
1458
1459 void hammer2_base_delete(hammer2_chain_t *chain,
1460                                 hammer2_blockref_t *base, int count,
1461                                 int *cache_indexp, hammer2_chain_t *child);
1462 void hammer2_base_insert(hammer2_chain_t *chain,
1463                                 hammer2_blockref_t *base, int count,
1464                                 int *cache_indexp, hammer2_chain_t *child);
1465
1466 /*
1467  * hammer2_flush.c
1468  */
1469 void hammer2_flush(hammer2_chain_t *chain, int istop);
1470 void hammer2_flush_quick(hammer2_dev_t *hmp);
1471 void hammer2_delayed_flush(hammer2_chain_t *chain);
1472
1473 /*
1474  * hammer2_trans.c
1475  */
1476 void hammer2_trans_init(hammer2_pfs_t *pmp, uint32_t flags);
1477 hammer2_tid_t hammer2_trans_sub(hammer2_pfs_t *pmp);
1478 void hammer2_trans_clear_preflush(hammer2_pfs_t *pmp);
1479 void hammer2_trans_done(hammer2_pfs_t *pmp);
1480 hammer2_tid_t hammer2_trans_newinum(hammer2_pfs_t *pmp);
1481 void hammer2_trans_assert_strategy(hammer2_pfs_t *pmp);
1482
1483 /*
1484  * hammer2_ioctl.c
1485  */
1486 int hammer2_ioctl(hammer2_inode_t *ip, u_long com, void *data,
1487                                 int fflag, struct ucred *cred);
1488
1489 /*
1490  * hammer2_io.c
1491  */
1492 void hammer2_io_putblk(hammer2_io_t **diop);
1493 void hammer2_io_cleanup(hammer2_dev_t *hmp, struct hammer2_io_tree *tree);
1494 char *hammer2_io_data(hammer2_io_t *dio, off_t lbase);
1495 hammer2_io_t *hammer2_io_getquick(hammer2_dev_t *hmp, off_t lbase, int lsize);
1496 void hammer2_io_getblk(hammer2_dev_t *hmp, off_t lbase, int lsize,
1497                                 hammer2_iocb_t *iocb);
1498 void hammer2_io_complete(hammer2_iocb_t *iocb);
1499 void hammer2_io_callback(struct bio *bio);
1500 void hammer2_iocb_wait(hammer2_iocb_t *iocb);
1501 int hammer2_io_new(hammer2_dev_t *hmp, int btype, off_t lbase, int lsize,
1502                                 hammer2_io_t **diop);
1503 int hammer2_io_newnz(hammer2_dev_t *hmp, int btype, off_t lbase, int lsize,
1504                                 hammer2_io_t **diop);
1505 int hammer2_io_newq(hammer2_dev_t *hmp, int btype, off_t lbase, int lsize,
1506                                 hammer2_io_t **diop);
1507 int hammer2_io_bread(hammer2_dev_t *hmp, int btype, off_t lbase, int lsize,
1508                                 hammer2_io_t **diop);
1509 void hammer2_io_bawrite(hammer2_io_t **diop);
1510 void hammer2_io_bdwrite(hammer2_io_t **diop);
1511 int hammer2_io_bwrite(hammer2_io_t **diop);
1512 int hammer2_io_isdirty(hammer2_io_t *dio);
1513 void hammer2_io_setdirty(hammer2_io_t *dio);
1514 void hammer2_io_setinval(hammer2_io_t *dio, hammer2_off_t off, u_int bytes);
1515 void hammer2_io_brelse(hammer2_io_t **diop);
1516 void hammer2_io_bqrelse(hammer2_io_t **diop);
1517 int hammer2_io_crc_good(hammer2_chain_t *chain, uint64_t *maskp);
1518 void hammer2_io_crc_setmask(hammer2_io_t *dio, uint64_t mask);
1519 void hammer2_io_crc_clrmask(hammer2_io_t *dio, uint64_t mask);
1520
1521 /*
1522  * hammer2_thread.c
1523  */
1524 void hammer2_thr_signal(hammer2_thread_t *thr, uint32_t flags);
1525 void hammer2_thr_return(hammer2_thread_t *thr, uint32_t flags);
1526 void hammer2_thr_wait(hammer2_thread_t *thr, uint32_t flags);
1527 void hammer2_thr_wait_neg(hammer2_thread_t *thr, uint32_t flags);
1528 void hammer2_thr_create(hammer2_thread_t *thr, hammer2_pfs_t *pmp,
1529                         const char *id, int clindex, int repidx,
1530                         void (*func)(void *arg));
1531 void hammer2_thr_delete(hammer2_thread_t *thr);
1532 void hammer2_thr_remaster(hammer2_thread_t *thr);
1533 void hammer2_thr_freeze_async(hammer2_thread_t *thr);
1534 void hammer2_thr_freeze(hammer2_thread_t *thr);
1535 void hammer2_thr_unfreeze(hammer2_thread_t *thr);
1536 int hammer2_thr_break(hammer2_thread_t *thr);
1537 void hammer2_primary_xops_thread(void *arg);
1538
1539 /*
1540  * hammer2_thread.c (XOP API)
1541  */
1542 void hammer2_xop_group_init(hammer2_pfs_t *pmp, hammer2_xop_group_t *xgrp);
1543 void *hammer2_xop_alloc(hammer2_inode_t *ip, int flags);
1544 void hammer2_xop_setname(hammer2_xop_head_t *xop,
1545                                 const char *name, size_t name_len);
1546 void hammer2_xop_setname2(hammer2_xop_head_t *xop,
1547                                 const char *name, size_t name_len);
1548 void hammer2_xop_setip2(hammer2_xop_head_t *xop, hammer2_inode_t *ip2);
1549 void hammer2_xop_setip3(hammer2_xop_head_t *xop, hammer2_inode_t *ip3);
1550 void hammer2_xop_reinit(hammer2_xop_head_t *xop);
1551 void hammer2_xop_helper_create(hammer2_pfs_t *pmp);
1552 void hammer2_xop_helper_cleanup(hammer2_pfs_t *pmp);
1553 void hammer2_xop_start(hammer2_xop_head_t *xop, hammer2_xop_func_t func);
1554 void hammer2_xop_start_except(hammer2_xop_head_t *xop, hammer2_xop_func_t func,
1555                                 int notidx);
1556 int hammer2_xop_collect(hammer2_xop_head_t *xop, int flags);
1557 void hammer2_xop_retire(hammer2_xop_head_t *xop, uint32_t mask);
1558 int hammer2_xop_active(hammer2_xop_head_t *xop);
1559 int hammer2_xop_feed(hammer2_xop_head_t *xop, hammer2_chain_t *chain,
1560                                 int clindex, int error);
1561
1562 /*
1563  * hammer2_synchro.c
1564  */
1565 void hammer2_primary_sync_thread(void *arg);
1566
1567 /*
1568  * XOP backends in hammer2_xops.c, primarily for VNOPS.  Other XOP backends
1569  * may be integrated into other source files.
1570  */
1571 void hammer2_xop_ipcluster(hammer2_xop_t *xop, int clidx);
1572 void hammer2_xop_readdir(hammer2_xop_t *xop, int clidx);
1573 void hammer2_xop_nresolve(hammer2_xop_t *xop, int clidx);
1574 void hammer2_xop_unlink(hammer2_xop_t *xop, int clidx);
1575 void hammer2_xop_nrename(hammer2_xop_t *xop, int clidx);
1576 void hammer2_xop_nlink(hammer2_xop_t *xop, int clidx);
1577 void hammer2_xop_scanlhc(hammer2_xop_t *xop, int clidx);
1578 void hammer2_xop_scanall(hammer2_xop_t *xop, int clidx);
1579 void hammer2_xop_lookup(hammer2_xop_t *xop, int clidx);
1580 void hammer2_inode_xop_create(hammer2_xop_t *xop, int clidx);
1581 void hammer2_inode_xop_destroy(hammer2_xop_t *xop, int clidx);
1582 void hammer2_inode_xop_chain_sync(hammer2_xop_t *xop, int clidx);
1583 void hammer2_inode_xop_unlinkall(hammer2_xop_t *xop, int clidx);
1584 void hammer2_inode_xop_connect(hammer2_xop_t *xop, int clidx);
1585 void hammer2_inode_xop_flush(hammer2_xop_t *xop, int clidx);
1586
1587 /*
1588  * hammer2_msgops.c
1589  */
1590 int hammer2_msg_dbg_rcvmsg(kdmsg_msg_t *msg);
1591 int hammer2_msg_adhoc_input(kdmsg_msg_t *msg);
1592
1593 /*
1594  * hammer2_vfsops.c
1595  */
1596 void hammer2_volconf_update(hammer2_dev_t *hmp, int index);
1597 void hammer2_dump_chain(hammer2_chain_t *chain, int tab, int *countp, char pfx);
1598 int hammer2_vfs_sync(struct mount *mp, int waitflags);
1599 hammer2_pfs_t *hammer2_pfsalloc(hammer2_chain_t *chain,
1600                                 const hammer2_inode_data_t *ripdata,
1601                                 hammer2_tid_t modify_tid,
1602                                 hammer2_dev_t *force_local);
1603
1604 void hammer2_lwinprog_ref(hammer2_pfs_t *pmp);
1605 void hammer2_lwinprog_drop(hammer2_pfs_t *pmp);
1606 void hammer2_lwinprog_wait(hammer2_pfs_t *pmp, int pipe);
1607
1608 /*
1609  * hammer2_freemap.c
1610  */
1611 int hammer2_freemap_alloc(hammer2_chain_t *chain, size_t bytes);
1612 void hammer2_freemap_adjust(hammer2_dev_t *hmp,
1613                                 hammer2_blockref_t *bref, int how);
1614
1615 /*
1616  * hammer2_cluster.c
1617  */
1618 uint8_t hammer2_cluster_type(hammer2_cluster_t *cluster);
1619 const hammer2_media_data_t *hammer2_cluster_rdata(hammer2_cluster_t *cluster);
1620 hammer2_media_data_t *hammer2_cluster_wdata(hammer2_cluster_t *cluster);
1621 hammer2_cluster_t *hammer2_cluster_from_chain(hammer2_chain_t *chain);
1622 void hammer2_cluster_bref(hammer2_cluster_t *cluster, hammer2_blockref_t *bref);
1623 hammer2_cluster_t *hammer2_cluster_alloc(hammer2_pfs_t *pmp,
1624                                 hammer2_blockref_t *bref);
1625 void hammer2_cluster_ref(hammer2_cluster_t *cluster);
1626 void hammer2_cluster_drop(hammer2_cluster_t *cluster);
1627 void hammer2_cluster_lock(hammer2_cluster_t *cluster, int how);
1628 int hammer2_cluster_check(hammer2_cluster_t *cluster, hammer2_key_t lokey,
1629                         int flags);
1630 void hammer2_cluster_resolve(hammer2_cluster_t *cluster);
1631 void hammer2_cluster_forcegood(hammer2_cluster_t *cluster);
1632 hammer2_cluster_t *hammer2_cluster_copy(hammer2_cluster_t *ocluster);
1633 void hammer2_cluster_unlock(hammer2_cluster_t *cluster);
1634
1635 int hammer2_bulkfree_pass(hammer2_dev_t *hmp,
1636                         struct hammer2_ioc_bulkfree *bfi);
1637
1638 /*
1639  * hammer2_iocom.c
1640  */
1641 void hammer2_iocom_init(hammer2_dev_t *hmp);
1642 void hammer2_iocom_uninit(hammer2_dev_t *hmp);
1643 void hammer2_cluster_reconnect(hammer2_dev_t *hmp, struct file *fp);
1644
1645 /*
1646  * hammer2_strategy.c
1647  */
1648 int hammer2_vop_strategy(struct vop_strategy_args *ap);
1649 int hammer2_vop_bmap(struct vop_bmap_args *ap);
1650 void hammer2_write_thread(void *arg);
1651 void hammer2_bioq_sync(hammer2_pfs_t *pmp);
1652 void hammer2_dedup_clear(hammer2_dev_t *hmp);
1653
1654 #endif /* !_KERNEL */
1655 #endif /* !_VFS_HAMMER2_HAMMER2_H_ */