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