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