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