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