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