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