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