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