xref: /dragonfly/sys/vfs/hammer2/hammer2.h (revision fae225dc)
1 /*
2  * Copyright (c) 2011-2016 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/tree.h>
77 #include <sys/malloc.h>
78 #include <sys/mount.h>
79 #include <sys/vnode.h>
80 #include <sys/proc.h>
81 #include <sys/mountctl.h>
82 #include <sys/priv.h>
83 #include <sys/stat.h>
84 #include <sys/thread.h>
85 #include <sys/globaldata.h>
86 #include <sys/lockf.h>
87 #include <sys/buf.h>
88 #include <sys/queue.h>
89 #include <sys/limits.h>
90 #include <sys/dmsg.h>
91 #include <sys/mutex.h>
92 #ifdef _KERNEL
93 #include <sys/kern_syscall.h>
94 #endif
95 
96 #ifdef _KERNEL
97 #include <sys/signal2.h>
98 #include <sys/buf2.h>
99 #include <sys/mutex2.h>
100 #include <sys/thread2.h>
101 #endif
102 
103 #include "hammer2_xxhash.h"
104 #include "hammer2_disk.h"
105 #include "hammer2_mount.h"
106 #include "hammer2_ioctl.h"
107 
108 struct hammer2_io;
109 struct hammer2_iocb;
110 struct hammer2_chain;
111 struct hammer2_cluster;
112 struct hammer2_inode;
113 struct hammer2_dev;
114 struct hammer2_pfs;
115 struct hammer2_span;
116 struct hammer2_state;
117 struct hammer2_msg;
118 struct hammer2_thread;
119 union hammer2_xop;
120 
121 /*
122  * Mutex and lock shims.  Hammer2 requires support for asynchronous and
123  * abortable locks, and both exclusive and shared spinlocks.  Normal
124  * synchronous non-abortable locks can be substituted for spinlocks.
125  */
126 typedef mtx_t				hammer2_mtx_t;
127 typedef mtx_link_t			hammer2_mtx_link_t;
128 typedef mtx_state_t			hammer2_mtx_state_t;
129 
130 typedef struct spinlock			hammer2_spin_t;
131 
132 #define hammer2_mtx_ex			mtx_lock_ex_quick
133 #define hammer2_mtx_sh			mtx_lock_sh_quick
134 #define hammer2_mtx_sh_again		mtx_lock_sh_again
135 #define hammer2_mtx_unlock		mtx_unlock
136 #define hammer2_mtx_downgrade		mtx_downgrade
137 #define hammer2_mtx_owned		mtx_owned
138 #define hammer2_mtx_init		mtx_init
139 #define hammer2_mtx_temp_release	mtx_lock_temp_release
140 #define hammer2_mtx_temp_restore	mtx_lock_temp_restore
141 #define hammer2_mtx_refs		mtx_lockrefs
142 
143 #define hammer2_spin_init		spin_init
144 #define hammer2_spin_sh			spin_lock_shared
145 #define hammer2_spin_ex			spin_lock
146 #define hammer2_spin_unsh		spin_unlock_shared
147 #define hammer2_spin_unex		spin_unlock
148 
149 TAILQ_HEAD(hammer2_xop_list, hammer2_xop_head);
150 TAILQ_HEAD(hammer2_chain_list, hammer2_chain);
151 
152 typedef struct hammer2_xop_list	hammer2_xop_list_t;
153 
154 #ifdef _KERNEL
155 /*
156  * General lock support
157  */
158 static __inline
159 int
160 hammer2_mtx_upgrade_try(hammer2_mtx_t *mtx)
161 {
162 	return mtx_upgrade_try(mtx);
163 }
164 
165 #endif
166 
167 /*
168  * The xid tracks internal transactional updates.
169  *
170  * XXX fix-me, really needs to be 64-bits
171  */
172 typedef uint32_t hammer2_xid_t;
173 
174 #define HAMMER2_XID_MIN			0x00000000U
175 #define HAMMER2_XID_MAX			0x7FFFFFFFU
176 
177 #define HAMMER2_LIMIT_DIRTY_CHAINS	(65536)
178 
179 /*
180  * The chain structure tracks a portion of the media topology from the
181  * root (volume) down.  Chains represent volumes, inodes, indirect blocks,
182  * data blocks, and freemap nodes and leafs.
183  *
184  * The chain structure utilizes a simple singly-homed topology and the
185  * chain's in-memory topology will move around as the chains do, due mainly
186  * to renames and indirect block creation.
187  *
188  * Block Table Updates
189  *
190  *	Block table updates for insertions and updates are delayed until the
191  *	flush.  This allows us to avoid having to modify the parent chain
192  *	all the way to the root.
193  *
194  *	Block table deletions are performed immediately (modifying the parent
195  *	in the process) because the flush code uses the chain structure to
196  *	track delayed updates and the chain will be (likely) gone or moved to
197  *	another location in the topology after a deletion.
198  *
199  *	A prior iteration of the code tried to keep the relationship intact
200  *	on deletes by doing a delete-duplicate operation on the chain, but
201  *	it added way too much complexity to the codebase.
202  *
203  * Flush Synchronization
204  *
205  *	The flush code must flush modified chains bottom-up.  Because chain
206  *	structures can shift around and are NOT topologically stable,
207  *	modified chains are independently indexed for the flush.  As the flush
208  *	runs it modifies (or further modifies) and updates the parents,
209  *	propagating the flush all the way to the volume root.
210  *
211  *	Modifying front-end operations can occur during a flush but will block
212  *	in two cases: (1) when the front-end tries to operate on the inode
213  *	currently in the midst of being flushed and (2) if the front-end
214  *	crosses an inode currently being flushed (such as during a rename).
215  *	So, for example, if you rename directory "x" to "a/b/c/d/e/f/g/x" and
216  *	the flusher is currently working on "a/b/c", the rename will block
217  *	temporarily in order to ensure that "x" exists in one place or the
218  *	other.
219  *
220  *	Meta-data statistics are updated by the flusher.  The front-end will
221  *	make estimates but meta-data must be fully synchronized only during a
222  *	flush in order to ensure that it remains correct across a crash.
223  *
224  *	Multiple flush synchronizations can theoretically be in-flight at the
225  *	same time but the implementation is not coded to handle the case and
226  *	currently serializes them.
227  *
228  * Snapshots:
229  *
230  *	Snapshots currently require the subdirectory tree being snapshotted
231  *	to be flushed.  The snapshot then creates a new super-root inode which
232  *	copies the flushed blockdata of the directory or file that was
233  *	snapshotted.
234  *
235  * RBTREE NOTES:
236  *
237  *	- Note that the radix tree runs in powers of 2 only so sub-trees
238  *	  cannot straddle edges.
239  */
240 RB_HEAD(hammer2_chain_tree, hammer2_chain);
241 TAILQ_HEAD(h2_flush_list, hammer2_chain);
242 TAILQ_HEAD(h2_core_list, hammer2_chain);
243 TAILQ_HEAD(h2_iocb_list, hammer2_iocb);
244 
245 #define CHAIN_CORE_DELETE_BMAP_ENTRIES	\
246 	(HAMMER2_PBUFSIZE / sizeof(hammer2_blockref_t) / sizeof(uint32_t))
247 
248 /*
249  * Core topology for chain (embedded in chain).  Protected by a spinlock.
250  */
251 struct hammer2_chain_core {
252 	hammer2_spin_t	spin;
253 	struct hammer2_chain_tree rbtree; /* sub-chains */
254 	int		live_zero;	/* blockref array opt */
255 	u_int		live_count;	/* live (not deleted) chains in tree */
256 	u_int		chain_count;	/* live + deleted chains under core */
257 	int		generation;	/* generation number (inserts only) */
258 };
259 
260 typedef struct hammer2_chain_core hammer2_chain_core_t;
261 
262 RB_HEAD(hammer2_io_tree, hammer2_io);
263 
264 /*
265  * IOCB - IO callback (into chain, cluster, or manual request)
266  */
267 struct hammer2_iocb {
268 	TAILQ_ENTRY(hammer2_iocb) entry;
269 	void (*callback)(struct hammer2_iocb *iocb);
270 	struct hammer2_io	*dio;
271 	struct hammer2_chain	*chain;
272 	void			*ptr;
273 	off_t			lbase;
274 	int			lsize;
275 	uint32_t		flags;
276 	int			error;
277 	int			btype;
278 };
279 
280 typedef struct hammer2_iocb hammer2_iocb_t;
281 
282 #define HAMMER2_IOCB_INTERLOCK	0x00000001
283 #define HAMMER2_IOCB_ONQ	0x00000002
284 #define HAMMER2_IOCB_DONE	0x00000004
285 #define HAMMER2_IOCB_INPROG	0x00000008
286 #define HAMMER2_IOCB_UNUSED10	0x00000010
287 #define HAMMER2_IOCB_QUICK	0x00010000
288 #define HAMMER2_IOCB_ZERO	0x00020000
289 #define HAMMER2_IOCB_READ	0x00040000
290 #define HAMMER2_IOCB_WAKEUP	0x00080000
291 
292 /*
293  * DIO - Management structure wrapping system buffer cache.
294  *
295  *	 Used for multiple purposes including concurrent management
296  *	 if small requests by chains into larger DIOs.
297  */
298 struct hammer2_io {
299 	RB_ENTRY(hammer2_io) rbnode;	/* indexed by device offset */
300 	struct h2_iocb_list iocbq;
301 	struct spinlock spin;
302 	struct hammer2_dev *hmp;
303 	struct buf	*bp;
304 	off_t		pbase;
305 	uint64_t	refs;
306 	int		psize;
307 	int		act;		/* activity */
308 	int		btype;		/* approximate BREF_TYPE_* */
309 	int		unused01;
310 };
311 
312 typedef struct hammer2_io hammer2_io_t;
313 
314 #define HAMMER2_DIO_INPROG	0x8000000000000000LLU	/* bio in progress */
315 #define HAMMER2_DIO_GOOD	0x4000000000000000LLU	/* dio->bp is stable */
316 #define HAMMER2_DIO_WAITING	0x2000000000000000LLU	/* wait on INPROG */
317 #define HAMMER2_DIO_DIRTY	0x1000000000000000LLU	/* flush last drop */
318 #define HAMMER2_DIO_INVALOK	0x0800000000000000LLU	/* ok to inval */
319 #define HAMMER2_DIO_INVAL	0x0400000000000000LLU	/* inval request */
320 
321 #define HAMMER2_DIO_MASK	0x00FFFFFFFFFFFFFFLLU
322 
323 #define HAMMER2_DIO_INVALBITS	(HAMMER2_DIO_INVAL | HAMMER2_DIO_INVALOK)
324 
325 /*
326  * Primary chain structure keeps track of the topology in-memory.
327  */
328 struct hammer2_chain {
329 	hammer2_mtx_t		lock;
330 	hammer2_chain_core_t	core;
331 	RB_ENTRY(hammer2_chain) rbnode;		/* live chain(s) */
332 	hammer2_blockref_t	bref;
333 	struct hammer2_chain	*parent;
334 	struct hammer2_state	*state;		/* if active cache msg */
335 	struct hammer2_dev	*hmp;
336 	struct hammer2_pfs	*pmp;		/* A PFS or super-root (spmp) */
337 
338 	hammer2_io_t	*dio;			/* physical data buffer */
339 	u_int		bytes;			/* physical data size */
340 	u_int		flags;
341 	u_int		refs;
342 	u_int		lockcnt;
343 	int		error;			/* on-lock data error state */
344 	int		persist_refs;		/* (aka ip->cluster) */
345 
346 	hammer2_media_data_t *data;		/* data pointer shortcut */
347 	TAILQ_ENTRY(hammer2_chain) flush_node;	/* flush list */
348 	TAILQ_ENTRY(hammer2_chain) lru_node;	/* 0-refs LRU */
349 };
350 
351 typedef struct hammer2_chain hammer2_chain_t;
352 
353 int hammer2_chain_cmp(hammer2_chain_t *chain1, hammer2_chain_t *chain2);
354 RB_PROTOTYPE(hammer2_chain_tree, hammer2_chain, rbnode, hammer2_chain_cmp);
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  * FICTITIOUS	- Faked chain as a placeholder for an error condition.  This
372  *		  chain is unsuitable for I/O.
373  *
374  * BMAPPED	- Indicates that the chain is present in the parent blockmap.
375  *
376  * BMAPUPD	- Indicates that the chain is present but needs to be updated
377  *		  in the parent blockmap.
378  */
379 #define HAMMER2_CHAIN_MODIFIED		0x00000001	/* dirty chain data */
380 #define HAMMER2_CHAIN_ALLOCATED		0x00000002	/* kmalloc'd chain */
381 #define HAMMER2_CHAIN_DESTROY		0x00000004
382 #define HAMMER2_CHAIN_DEDUP		0x00000008	/* recorded for dedup */
383 #define HAMMER2_CHAIN_DELETED		0x00000010	/* deleted chain */
384 #define HAMMER2_CHAIN_INITIAL		0x00000020	/* initial create */
385 #define HAMMER2_CHAIN_UPDATE		0x00000040	/* need parent update */
386 #define HAMMER2_CHAIN_DEFERRED		0x00000080	/* flush depth defer */
387 #define HAMMER2_CHAIN_TESTEDGOOD	0x00000100	/* crc tested good */
388 #define HAMMER2_CHAIN_ONFLUSH		0x00000200	/* on a flush list */
389 #define HAMMER2_CHAIN_FICTITIOUS	0x00000400	/* unsuitable for I/O */
390 #define HAMMER2_CHAIN_VOLUMESYNC	0x00000800	/* needs volume sync */
391 #define HAMMER2_CHAIN_DELAYED		0x00001000	/* delayed flush */
392 #define HAMMER2_CHAIN_COUNTEDBREFS	0x00002000	/* block table stats */
393 #define HAMMER2_CHAIN_ONRBTREE		0x00004000	/* on parent RB tree */
394 #define HAMMER2_CHAIN_ONLRU		0x00008000	/* on LRU list */
395 #define HAMMER2_CHAIN_EMBEDDED		0x00010000	/* embedded data */
396 #define HAMMER2_CHAIN_RELEASE		0x00020000	/* don't keep around */
397 #define HAMMER2_CHAIN_BMAPPED		0x00040000	/* present in blkmap */
398 #define HAMMER2_CHAIN_BMAPUPD		0x00080000	/* +needs updating */
399 #define HAMMER2_CHAIN_IOINPROG		0x00100000	/* I/O interlock */
400 #define HAMMER2_CHAIN_IOSIGNAL		0x00200000	/* I/O interlock */
401 #define HAMMER2_CHAIN_PFSBOUNDARY	0x00400000	/* super->pfs inode */
402 
403 #define HAMMER2_CHAIN_FLUSH_MASK	(HAMMER2_CHAIN_MODIFIED |	\
404 					 HAMMER2_CHAIN_UPDATE |		\
405 					 HAMMER2_CHAIN_ONFLUSH |	\
406 					 HAMMER2_CHAIN_DESTROY)
407 
408 /*
409  * Hammer2 error codes, used by chain->error and cluster->error.  The error
410  * code is typically set on-lock unless no I/O was requested, and set on
411  * I/O otherwise.  If set for a cluster it generally means that the cluster
412  * code could not find a valid copy to present.
413  *
414  * IO		- An I/O error occurred
415  * CHECK	- I/O succeeded but did not match the check code
416  * INCOMPLETE	- A cluster is not complete enough to use, or
417  *		  a chain cannot be loaded because its parent has an error.
418  *
419  * NOTE: API allows callers to check zero/non-zero to determine if an error
420  *	 condition exists.
421  *
422  * NOTE: Chain's data field is usually NULL on an IO error but not necessarily
423  *	 NULL on other errors.  Check chain->error, not chain->data.
424  */
425 #define HAMMER2_ERROR_NONE		0
426 #define HAMMER2_ERROR_IO		1	/* device I/O error */
427 #define HAMMER2_ERROR_CHECK		2	/* check code mismatch */
428 #define HAMMER2_ERROR_INCOMPLETE	3	/* incomplete cluster */
429 #define HAMMER2_ERROR_DEPTH		4	/* temporary depth limit */
430 
431 /*
432  * Flags passed to hammer2_chain_lookup() and hammer2_chain_next()
433  *
434  * NOTES:
435  *	NOLOCK	    - Input and output chains are referenced only and not
436  *		      locked.  Output chain might be temporarily locked
437  *		      internally.
438  *
439  *	NODATA	    - Asks that the chain->data not be resolved in order
440  *		      to avoid I/O.
441  *
442  *	NODIRECT    - Prevents a lookup of offset 0 in an inode from returning
443  *		      the inode itself if the inode is in DIRECTDATA mode
444  *		      (i.e. file is <= 512 bytes).  Used by the synchronization
445  *		      code to prevent confusion.
446  *
447  *	SHARED	    - The input chain is expected to be locked shared,
448  *		      and the output chain is locked shared.
449  *
450  *	MATCHIND    - Allows an indirect block / freemap node to be returned
451  *		      when the passed key range matches the radix.  Remember
452  *		      that key_end is inclusive (e.g. {0x000,0xFFF},
453  *		      not {0x000,0x1000}).
454  *
455  *		      (Cannot be used for remote or cluster ops).
456  *
457  *	ALLNODES    - Allows NULL focus.
458  *
459  *	ALWAYS	    - Always resolve the data.  If ALWAYS and NODATA are both
460  *		      missing, bulk file data is not resolved but inodes and
461  *		      other meta-data will.
462  *
463  *	NOUNLOCK    - Used by hammer2_chain_next() to leave the lock on
464  *		      the input chain intact.  The chain is still dropped.
465  *		      This allows the caller to add a reference to the chain
466  *		      and retain it in a locked state (used by the
467  *		      XOP/feed/collect code).
468  */
469 #define HAMMER2_LOOKUP_NOLOCK		0x00000001	/* ref only */
470 #define HAMMER2_LOOKUP_NODATA		0x00000002	/* data left NULL */
471 #define HAMMER2_LOOKUP_NODIRECT		0x00000004	/* no offset=0 DD */
472 #define HAMMER2_LOOKUP_SHARED		0x00000100
473 #define HAMMER2_LOOKUP_MATCHIND		0x00000200	/* return all chains */
474 #define HAMMER2_LOOKUP_ALLNODES		0x00000400	/* allow NULL focus */
475 #define HAMMER2_LOOKUP_ALWAYS		0x00000800	/* resolve data */
476 #define HAMMER2_LOOKUP_NOUNLOCK		0x00001000	/* leave lock intact */
477 
478 /*
479  * Flags passed to hammer2_chain_modify() and hammer2_chain_resize()
480  *
481  * NOTE: OPTDATA allows us to avoid instantiating buffers for INDIRECT
482  *	 blocks in the INITIAL-create state.
483  */
484 #define HAMMER2_MODIFY_OPTDATA		0x00000002	/* data can be NULL */
485 #define HAMMER2_MODIFY_NO_MODIFY_TID	0x00000004
486 #define HAMMER2_MODIFY_UNUSED0008	0x00000008
487 
488 /*
489  * Flags passed to hammer2_chain_lock()
490  *
491  * NOTE: RDONLY is set to optimize cluster operations when *no* modifications
492  *	 will be made to either the cluster being locked or any underlying
493  *	 cluster.  It allows the cluster to lock and access data for a subset
494  *	 of available nodes instead of all available nodes.
495  */
496 #define HAMMER2_RESOLVE_NEVER		1
497 #define HAMMER2_RESOLVE_MAYBE		2
498 #define HAMMER2_RESOLVE_ALWAYS		3
499 #define HAMMER2_RESOLVE_MASK		0x0F
500 
501 #define HAMMER2_RESOLVE_SHARED		0x10	/* request shared lock */
502 #define HAMMER2_RESOLVE_LOCKAGAIN	0x20	/* another shared lock */
503 #define HAMMER2_RESOLVE_RDONLY		0x40	/* higher level op flag */
504 
505 /*
506  * Flags passed to hammer2_chain_delete()
507  */
508 #define HAMMER2_DELETE_PERMANENT	0x0001
509 
510 /*
511  * Flags passed to hammer2_chain_insert() or hammer2_chain_rename()
512  */
513 #define HAMMER2_INSERT_PFSROOT		0x0004
514 
515 /*
516  * Flags passed to hammer2_chain_delete_duplicate()
517  */
518 #define HAMMER2_DELDUP_RECORE		0x0001
519 
520 /*
521  * Cluster different types of storage together for allocations
522  */
523 #define HAMMER2_FREECACHE_INODE		0
524 #define HAMMER2_FREECACHE_INDIR		1
525 #define HAMMER2_FREECACHE_DATA		2
526 #define HAMMER2_FREECACHE_UNUSED3	3
527 #define HAMMER2_FREECACHE_TYPES		4
528 
529 /*
530  * hammer2_freemap_alloc() block preference
531  */
532 #define HAMMER2_OFF_NOPREF		((hammer2_off_t)-1)
533 
534 /*
535  * BMAP read-ahead maximum parameters
536  */
537 #define HAMMER2_BMAP_COUNT		16	/* max bmap read-ahead */
538 #define HAMMER2_BMAP_BYTES		(HAMMER2_PBUFSIZE * HAMMER2_BMAP_COUNT)
539 
540 /*
541  * hammer2_freemap_adjust()
542  */
543 #define HAMMER2_FREEMAP_DORECOVER	1
544 #define HAMMER2_FREEMAP_DOMAYFREE	2
545 #define HAMMER2_FREEMAP_DOREALFREE	3
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_MAXCLUSTER	8
576 #define HAMMER2_XOPMASK_CLUSTER	((1U << HAMMER2_MAXCLUSTER) - 1)
577 #define HAMMER2_XOPFIFO		16
578 #define HAMMER2_XOPFIFO_MASK	(HAMMER2_XOPFIFO - 1)
579 #define HAMMER2_XOPGROUPS	32
580 #define HAMMER2_XOPGROUPS_MASK	(HAMMER2_XOPGROUPS - 1)
581 #define HAMMER2_XOPMASK_VOP	0x80000000U
582 #define HAMMER2_XOPMASK_FIFOW	0x40000000U
583 
584 #define HAMMER2_XOPMASK_ALLDONE	(HAMMER2_XOPMASK_VOP | HAMMER2_XOPMASK_CLUSTER)
585 
586 #define HAMMER2_SPECTHREADS	1	/* sync */
587 
588 struct hammer2_cluster_item {
589 	hammer2_chain_t		*chain;
590 	int			cache_index;
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_iocb_t		iocb;
618 	hammer2_chain_t		*focus;		/* current focus (or mod) */
619 	hammer2_cluster_item_t	array[HAMMER2_MAXCLUSTER];
620 };
621 
622 typedef struct hammer2_cluster	hammer2_cluster_t;
623 
624 /*
625  * WRHARD	- Hard mounts can write fully synchronized
626  * RDHARD	- Hard mounts can read fully synchronized
627  * UNHARD	- Unsynchronized masters present
628  * NOHARD	- No masters visible
629  * WRSOFT	- Soft mounts can write to at least the SOFT_MASTER
630  * RDSOFT	- Soft mounts can read from at least a SOFT_SLAVE
631  * UNSOFT	- Unsynchronized slaves present
632  * NOSOFT	- No slaves visible
633  * RDSLAVE	- slaves are accessible (possibly unsynchronized or remote).
634  * MSYNCED	- All masters are fully synchronized
635  * SSYNCED	- All known local slaves are fully synchronized to masters
636  *
637  * All available masters are always incorporated.  All PFSs belonging to a
638  * cluster (master, slave, copy, whatever) always try to synchronize the
639  * total number of known masters in the PFSs root inode.
640  *
641  * A cluster might have access to many slaves, copies, or caches, but we
642  * have a limited number of cluster slots.  Any such elements which are
643  * directly mounted from block device(s) will always be incorporated.   Note
644  * that SSYNCED only applies to such elements which are directly mounted,
645  * not to any remote slaves, copies, or caches that could be available.  These
646  * bits are used to monitor and drive our synchronization threads.
647  *
648  * When asking the question 'is any data accessible at all', then a simple
649  * test against (RDHARD|RDSOFT|RDSLAVE) gives you the answer.  If any of
650  * these bits are set the object can be read with certain caveats:
651  * RDHARD - no caveats.  RDSOFT - authoritative but might not be synchronized.
652  * and RDSLAVE - not authoritative, has some data but it could be old or
653  * incomplete.
654  *
655  * When both soft and hard mounts are available, data will be read and written
656  * via the soft mount only.  But all might be in the cluster because
657  * background synchronization threads still need to do their work.
658  */
659 #define HAMMER2_CLUSTER_INODE	0x00000001	/* embedded in inode struct */
660 #define HAMMER2_CLUSTER_UNUSED2	0x00000002
661 #define HAMMER2_CLUSTER_LOCKED	0x00000004	/* cluster lks not recursive */
662 #define HAMMER2_CLUSTER_WRHARD	0x00000100	/* hard-mount can write */
663 #define HAMMER2_CLUSTER_RDHARD	0x00000200	/* hard-mount can read */
664 #define HAMMER2_CLUSTER_UNHARD	0x00000400	/* unsynchronized masters */
665 #define HAMMER2_CLUSTER_NOHARD	0x00000800	/* no masters visible */
666 #define HAMMER2_CLUSTER_WRSOFT	0x00001000	/* soft-mount can write */
667 #define HAMMER2_CLUSTER_RDSOFT	0x00002000	/* soft-mount can read */
668 #define HAMMER2_CLUSTER_UNSOFT	0x00004000	/* unsynchronized slaves */
669 #define HAMMER2_CLUSTER_NOSOFT	0x00008000	/* no slaves visible */
670 #define HAMMER2_CLUSTER_MSYNCED	0x00010000	/* all masters synchronized */
671 #define HAMMER2_CLUSTER_SSYNCED	0x00020000	/* known slaves synchronized */
672 
673 #define HAMMER2_CLUSTER_ANYDATA	( HAMMER2_CLUSTER_RDHARD |	\
674 				  HAMMER2_CLUSTER_RDSOFT |	\
675 				  HAMMER2_CLUSTER_RDSLAVE)
676 
677 #define HAMMER2_CLUSTER_RDOK	( HAMMER2_CLUSTER_RDHARD |	\
678 				  HAMMER2_CLUSTER_RDSOFT)
679 
680 #define HAMMER2_CLUSTER_WROK	( HAMMER2_CLUSTER_WRHARD |	\
681 				  HAMMER2_CLUSTER_WRSOFT)
682 
683 #define HAMMER2_CLUSTER_ZFLAGS	( HAMMER2_CLUSTER_WRHARD |	\
684 				  HAMMER2_CLUSTER_RDHARD |	\
685 				  HAMMER2_CLUSTER_WRSOFT |	\
686 				  HAMMER2_CLUSTER_RDSOFT |	\
687 				  HAMMER2_CLUSTER_MSYNCED |	\
688 				  HAMMER2_CLUSTER_SSYNCED)
689 
690 /*
691  * Helper functions (cluster must be locked for flags to be valid).
692  */
693 static __inline
694 int
695 hammer2_cluster_rdok(hammer2_cluster_t *cluster)
696 {
697 	return (cluster->flags & HAMMER2_CLUSTER_RDOK);
698 }
699 
700 static __inline
701 int
702 hammer2_cluster_wrok(hammer2_cluster_t *cluster)
703 {
704 	return (cluster->flags & HAMMER2_CLUSTER_WROK);
705 }
706 
707 RB_HEAD(hammer2_inode_tree, hammer2_inode);
708 
709 /*
710  * A hammer2 inode.
711  *
712  * NOTE: The inode-embedded cluster is never used directly for I/O (since
713  *	 it may be shared).  Instead it will be replicated-in and synchronized
714  *	 back out if changed.
715  */
716 struct hammer2_inode {
717 	RB_ENTRY(hammer2_inode) rbnode;		/* inumber lookup (HL) */
718 	hammer2_mtx_t		lock;		/* inode lock */
719 	hammer2_mtx_t		truncate_lock;	/* prevent truncates */
720 	struct hammer2_pfs	*pmp;		/* PFS mount */
721 	struct vnode		*vp;
722 	struct spinlock		cluster_spin;	/* update cluster */
723 	hammer2_cluster_t	cluster;
724 	struct lockf		advlock;
725 	u_int			flags;
726 	u_int			refs;		/* +vpref, +flushref */
727 	uint8_t			comp_heuristic;
728 	hammer2_inode_meta_t	meta;		/* copy of meta-data */
729 	hammer2_off_t		osize;
730 };
731 
732 typedef struct hammer2_inode hammer2_inode_t;
733 
734 /*
735  * MODIFIED	- Inode is in a modified state, ip->meta may have changes.
736  * RESIZED	- Inode truncated (any) or inode extended beyond
737  *		  EMBEDDED_BYTES.
738  */
739 #define HAMMER2_INODE_MODIFIED		0x0001
740 #define HAMMER2_INODE_SROOT		0x0002	/* kmalloc special case */
741 #define HAMMER2_INODE_RENAME_INPROG	0x0004
742 #define HAMMER2_INODE_ONRBTREE		0x0008
743 #define HAMMER2_INODE_RESIZED		0x0010	/* requires inode_fsync */
744 #define HAMMER2_INODE_ISDELETED		0x0020	/* deleted */
745 #define HAMMER2_INODE_ISUNLINKED	0x0040
746 #define HAMMER2_INODE_METAGOOD		0x0080	/* inode meta-data good */
747 #define HAMMER2_INODE_ONSIDEQ		0x0100	/* on side processing queue */
748 
749 int hammer2_inode_cmp(hammer2_inode_t *ip1, hammer2_inode_t *ip2);
750 RB_PROTOTYPE2(hammer2_inode_tree, hammer2_inode, rbnode, hammer2_inode_cmp,
751 		hammer2_tid_t);
752 
753 /*
754  * inode-unlink side-structure
755  */
756 struct hammer2_inode_sideq {
757 	TAILQ_ENTRY(hammer2_inode_sideq) entry;
758 	hammer2_inode_t	*ip;
759 };
760 TAILQ_HEAD(h2_sideq_list, hammer2_inode_sideq);
761 
762 typedef struct hammer2_inode_sideq hammer2_inode_sideq_t;
763 
764 /*
765  * Transaction management sub-structure under hammer2_pfs
766  */
767 struct hammer2_trans {
768 	uint32_t		flags;
769 	uint32_t		sync_wait;
770 };
771 
772 typedef struct hammer2_trans hammer2_trans_t;
773 
774 #define HAMMER2_TRANS_ISFLUSH		0x80000000	/* flush code */
775 #define HAMMER2_TRANS_BUFCACHE		0x40000000	/* bio strategy */
776 #define HAMMER2_TRANS_UNUSED20		0x20000000
777 #define HAMMER2_TRANS_FPENDING		0x10000000	/* flush pending */
778 #define HAMMER2_TRANS_WAITING		0x08000000	/* someone waiting */
779 #define HAMMER2_TRANS_MASK		0x00FFFFFF	/* count mask */
780 
781 #define HAMMER2_FREEMAP_HEUR_NRADIX	4	/* pwr 2 PBUFRADIX-MINIORADIX */
782 #define HAMMER2_FREEMAP_HEUR_TYPES	8
783 #define HAMMER2_FREEMAP_HEUR_SIZE	(HAMMER2_FREEMAP_HEUR_NRADIX * \
784 					 HAMMER2_FREEMAP_HEUR_TYPES)
785 
786 #define HAMMER2_DEDUP_HEUR_SIZE		65536
787 #define HAMMER2_DEDUP_HEUR_MASK		(HAMMER2_DEDUP_HEUR_SIZE - 1)
788 
789 #define HAMMER2_FLUSH_TOP		0x0001
790 #define HAMMER2_FLUSH_ALL		0x0002
791 
792 
793 /*
794  * Hammer2 support thread element.
795  *
796  * Potentially many support threads can hang off of hammer2, primarily
797  * off the hammer2_pfs structure.  Typically:
798  *
799  * td x Nodes		 	A synchronization thread for each node.
800  * td x Nodes x workers		Worker threads for frontend operations.
801  * td x 1			Bioq thread for logical buffer writes.
802  *
803  * In addition, the synchronization thread(s) associated with the
804  * super-root PFS (spmp) for a node is responsible for automatic bulkfree
805  * and dedup scans.
806  */
807 struct hammer2_thread {
808 	struct hammer2_pfs *pmp;
809 	struct hammer2_dev *hmp;
810 	hammer2_xop_list_t xopq;
811 	thread_t	td;
812 	uint32_t	flags;
813 	int		depth;
814 	int		clindex;	/* cluster element index */
815 	int		repidx;
816 	char		*scratch;	/* MAXPHYS */
817 };
818 
819 typedef struct hammer2_thread hammer2_thread_t;
820 
821 #define HAMMER2_THREAD_UNMOUNTING	0x0001	/* unmount request */
822 #define HAMMER2_THREAD_DEV		0x0002	/* related to dev, not pfs */
823 #define HAMMER2_THREAD_WAITING		0x0004	/* thread in idle tsleep */
824 #define HAMMER2_THREAD_REMASTER		0x0008	/* remaster request */
825 #define HAMMER2_THREAD_STOP		0x0010	/* exit request */
826 #define HAMMER2_THREAD_FREEZE		0x0020	/* force idle */
827 #define HAMMER2_THREAD_FROZEN		0x0040	/* thread is frozen */
828 #define HAMMER2_THREAD_XOPQ		0x0080	/* work pending */
829 #define HAMMER2_THREAD_STOPPED		0x0100	/* thread has stopped */
830 #define HAMMER2_THREAD_UNFREEZE		0x0200
831 
832 #define HAMMER2_THREAD_WAKEUP_MASK	(HAMMER2_THREAD_UNMOUNTING |	\
833 					 HAMMER2_THREAD_REMASTER |	\
834 					 HAMMER2_THREAD_STOP |		\
835 					 HAMMER2_THREAD_FREEZE |	\
836 					 HAMMER2_THREAD_XOPQ)
837 
838 /*
839  * Support structure for dedup heuristic.
840  */
841 struct hammer2_dedup {
842 	hammer2_off_t	data_off;
843 	uint64_t	data_crc;
844 	uint32_t	ticks;
845 	uint32_t	unused03;
846 };
847 
848 typedef struct hammer2_dedup hammer2_dedup_t;
849 
850 /*
851  * hammer2_xop - container for VOP/XOP operation (allocated, not on stack).
852  *
853  * This structure is used to distribute a VOP operation across multiple
854  * nodes.  It provides a rendezvous for concurrent node execution and
855  * can be detached from the frontend operation to allow the frontend to
856  * return early.
857  *
858  * This structure also sequences operations on up to three inodes.
859  */
860 typedef void (*hammer2_xop_func_t)(hammer2_thread_t *thr,
861 				   union hammer2_xop *xop);
862 
863 struct hammer2_xop_fifo {
864 	TAILQ_ENTRY(hammer2_xop_head) entry;
865 	hammer2_chain_t		*array[HAMMER2_XOPFIFO];
866 	int			errors[HAMMER2_XOPFIFO];
867 	int			ri;
868 	int			wi;
869 	int			flags;
870 	hammer2_thread_t	*thr;
871 };
872 
873 typedef struct hammer2_xop_fifo hammer2_xop_fifo_t;
874 
875 #define HAMMER2_XOP_FIFO_RUN	0x0001
876 #define HAMMER2_XOP_FIFO_STALL	0x0002
877 
878 struct hammer2_xop_head {
879 	hammer2_xop_func_t	func;
880 	hammer2_tid_t		mtid;
881 	struct hammer2_inode	*ip1;
882 	struct hammer2_inode	*ip2;
883 	struct hammer2_inode	*ip3;
884 	uint32_t		check_counter;
885 	uint32_t		run_mask;
886 	uint32_t		chk_mask;
887 	int			flags;
888 	int			state;
889 	int			error;
890 	hammer2_key_t		collect_key;
891 	char			*name1;
892 	size_t			name1_len;
893 	char			*name2;
894 	size_t			name2_len;
895 	hammer2_xop_fifo_t	collect[HAMMER2_MAXCLUSTER];
896 	hammer2_cluster_t	cluster;	/* help collections */
897 };
898 
899 typedef struct hammer2_xop_head hammer2_xop_head_t;
900 
901 #define HAMMER2_XOP_CHKWAIT	0x00000001U
902 #define HAMMER2_XOP_CHKINC	0x00000002U
903 
904 struct hammer2_xop_ipcluster {
905 	hammer2_xop_head_t	head;
906 };
907 
908 struct hammer2_xop_strategy {
909 	hammer2_xop_head_t	head;
910 	hammer2_key_t		lbase;
911 	int			finished;
912 	hammer2_mtx_t		lock;
913 	struct bio		*bio;
914 };
915 
916 struct hammer2_xop_readdir {
917 	hammer2_xop_head_t	head;
918 	hammer2_key_t		lkey;
919 };
920 
921 struct hammer2_xop_nresolve {
922 	hammer2_xop_head_t	head;
923 	hammer2_key_t		lhc;	/* if name is NULL used lhc */
924 };
925 
926 struct hammer2_xop_unlink {
927 	hammer2_xop_head_t	head;
928 	int			isdir;
929 	int			dopermanent;
930 };
931 
932 struct hammer2_xop_nrename {
933 	hammer2_xop_head_t	head;
934 	hammer2_tid_t		lhc;
935 	int			ip_key;
936 };
937 
938 struct hammer2_xop_scanlhc {
939 	hammer2_xop_head_t	head;
940 	hammer2_key_t		lhc;
941 };
942 
943 struct hammer2_xop_scanall {
944 	hammer2_xop_head_t	head;
945 	hammer2_key_t		key_beg;	/* inclusive */
946 	hammer2_key_t		key_end;	/* inclusive */
947 	int			resolve_flags;
948 	int			lookup_flags;
949 };
950 
951 struct hammer2_xop_lookup {
952 	hammer2_xop_head_t	head;
953 	hammer2_key_t		lhc;
954 };
955 
956 struct hammer2_xop_mkdirent {
957 	hammer2_xop_head_t	head;
958 	hammer2_dirent_head_t	dirent;
959 	hammer2_key_t		lhc;
960 };
961 
962 struct hammer2_xop_create {
963 	hammer2_xop_head_t	head;
964 	hammer2_inode_meta_t	meta;		/* initial metadata */
965 	hammer2_key_t		lhc;
966 	int			flags;
967 };
968 
969 struct hammer2_xop_destroy {
970 	hammer2_xop_head_t	head;
971 };
972 
973 struct hammer2_xop_fsync {
974 	hammer2_xop_head_t	head;
975 	hammer2_inode_meta_t	meta;
976 	hammer2_off_t		osize;
977 	u_int			ipflags;
978 	int			clear_directdata;
979 };
980 
981 struct hammer2_xop_unlinkall {
982 	hammer2_xop_head_t	head;
983 	hammer2_key_t		key_beg;
984 	hammer2_key_t		key_end;
985 };
986 
987 struct hammer2_xop_connect {
988 	hammer2_xop_head_t	head;
989 	hammer2_key_t		lhc;
990 };
991 
992 struct hammer2_xop_flush {
993 	hammer2_xop_head_t	head;
994 };
995 
996 typedef struct hammer2_xop_readdir hammer2_xop_readdir_t;
997 typedef struct hammer2_xop_nresolve hammer2_xop_nresolve_t;
998 typedef struct hammer2_xop_unlink hammer2_xop_unlink_t;
999 typedef struct hammer2_xop_nrename hammer2_xop_nrename_t;
1000 typedef struct hammer2_xop_ipcluster hammer2_xop_ipcluster_t;
1001 typedef struct hammer2_xop_strategy hammer2_xop_strategy_t;
1002 typedef struct hammer2_xop_mkdirent hammer2_xop_mkdirent_t;
1003 typedef struct hammer2_xop_create hammer2_xop_create_t;
1004 typedef struct hammer2_xop_destroy hammer2_xop_destroy_t;
1005 typedef struct hammer2_xop_fsync hammer2_xop_fsync_t;
1006 typedef struct hammer2_xop_unlinkall hammer2_xop_unlinkall_t;
1007 typedef struct hammer2_xop_scanlhc hammer2_xop_scanlhc_t;
1008 typedef struct hammer2_xop_scanall hammer2_xop_scanall_t;
1009 typedef struct hammer2_xop_lookup hammer2_xop_lookup_t;
1010 typedef struct hammer2_xop_connect hammer2_xop_connect_t;
1011 typedef struct hammer2_xop_flush hammer2_xop_flush_t;
1012 
1013 union hammer2_xop {
1014 	hammer2_xop_head_t	head;
1015 	hammer2_xop_ipcluster_t	xop_ipcluster;
1016 	hammer2_xop_readdir_t	xop_readdir;
1017 	hammer2_xop_nresolve_t	xop_nresolve;
1018 	hammer2_xop_unlink_t	xop_unlink;
1019 	hammer2_xop_nrename_t	xop_nrename;
1020 	hammer2_xop_strategy_t	xop_strategy;
1021 	hammer2_xop_mkdirent_t	xop_mkdirent;
1022 	hammer2_xop_create_t	xop_create;
1023 	hammer2_xop_destroy_t	xop_destroy;
1024 	hammer2_xop_fsync_t	xop_fsync;
1025 	hammer2_xop_unlinkall_t	xop_unlinkall;
1026 	hammer2_xop_scanlhc_t	xop_scanlhc;
1027 	hammer2_xop_scanall_t	xop_scanall;
1028 	hammer2_xop_lookup_t	xop_lookup;
1029 	hammer2_xop_flush_t	xop_flush;
1030 	hammer2_xop_connect_t	xop_connect;
1031 };
1032 
1033 typedef union hammer2_xop hammer2_xop_t;
1034 
1035 /*
1036  * hammer2_xop_group - Manage XOP support threads.
1037  */
1038 struct hammer2_xop_group {
1039 	hammer2_thread_t	thrs[HAMMER2_MAXCLUSTER];
1040 };
1041 
1042 typedef struct hammer2_xop_group hammer2_xop_group_t;
1043 
1044 /*
1045  * flags to hammer2_xop_collect()
1046  */
1047 #define HAMMER2_XOP_COLLECT_NOWAIT	0x00000001
1048 #define HAMMER2_XOP_COLLECT_WAITALL	0x00000002
1049 
1050 /*
1051  * flags to hammer2_xop_alloc()
1052  *
1053  * MODIFYING	- This is a modifying transaction, allocate a mtid.
1054  */
1055 #define HAMMER2_XOP_MODIFYING		0x00000001
1056 #define HAMMER2_XOP_STRATEGY		0x00000002
1057 
1058 /*
1059  * Global (per partition) management structure, represents a hard block
1060  * device.  Typically referenced by hammer2_chain structures when applicable.
1061  * Typically not used for network-managed elements.
1062  *
1063  * Note that a single hammer2_dev can be indirectly tied to multiple system
1064  * mount points.  There is no direct relationship.  System mounts are
1065  * per-cluster-id, not per-block-device, and a single hard mount might contain
1066  * many PFSs and those PFSs might combine together in various ways to form
1067  * the set of available clusters.
1068  */
1069 struct hammer2_dev {
1070 	struct vnode	*devvp;		/* device vnode */
1071 	int		ronly;		/* read-only mount */
1072 	int		mount_count;	/* number of actively mounted PFSs */
1073 	TAILQ_ENTRY(hammer2_dev) mntentry; /* hammer2_mntlist */
1074 
1075 	struct malloc_type *mchain;
1076 	int		nipstacks;
1077 	int		maxipstacks;
1078 	kdmsg_iocom_t	iocom;		/* volume-level dmsg interface */
1079 	struct spinlock	io_spin;	/* iotree access */
1080 	struct hammer2_io_tree iotree;
1081 	int		iofree_count;
1082 	hammer2_chain_t vchain;		/* anchor chain (topology) */
1083 	hammer2_chain_t fchain;		/* anchor chain (freemap) */
1084 	struct spinlock	list_spin;
1085 	struct h2_flush_list flushq;	/* flush seeds */
1086 	struct hammer2_pfs *spmp;	/* super-root pmp for transactions */
1087 	struct lock	vollk;		/* lockmgr lock */
1088 	struct lock	bulklk;		/* bulkfree operation lock */
1089 	struct lock	bflock;		/* bulk-free manual function lock */
1090 	hammer2_off_t	heur_freemap[HAMMER2_FREEMAP_HEUR_SIZE];
1091 	hammer2_dedup_t heur_dedup[HAMMER2_DEDUP_HEUR_SIZE];
1092 	int		volhdrno;	/* last volhdrno written */
1093 	uint32_t	hflags;		/* HMNT2 flags applicable to device */
1094 	hammer2_thread_t bfthr;		/* bulk-free thread */
1095 	char		devrepname[64];	/* for kprintf */
1096 	hammer2_ioc_bulkfree_t bflast;	/* stats for last bulkfree run */
1097 	hammer2_volume_data_t voldata;
1098 	hammer2_volume_data_t volsync;	/* synchronized voldata */
1099 };
1100 
1101 typedef struct hammer2_dev hammer2_dev_t;
1102 
1103 /*
1104  * Helper functions (cluster must be locked for flags to be valid).
1105  */
1106 static __inline
1107 int
1108 hammer2_chain_rdok(hammer2_chain_t *chain)
1109 {
1110 	return (chain->error == 0);
1111 }
1112 
1113 static __inline
1114 int
1115 hammer2_chain_wrok(hammer2_chain_t *chain)
1116 {
1117 	return (chain->error == 0 && chain->hmp->ronly == 0);
1118 }
1119 
1120 /*
1121  * Per-cluster management structure.  This structure will be tied to a
1122  * system mount point if the system is mounting the PFS, but is also used
1123  * to manage clusters encountered during the super-root scan or received
1124  * via LNK_SPANs that might not be mounted.
1125  *
1126  * This structure is also used to represent the super-root that hangs off
1127  * of a hard mount point.  The super-root is not really a cluster element.
1128  * In this case the spmp_hmp field will be non-NULL.  It's just easier to do
1129  * this than to special case super-root manipulation in the hammer2_chain*
1130  * code as being only hammer2_dev-related.
1131  *
1132  * pfs_mode and pfs_nmasters are rollup fields which critically describes
1133  * how elements of the cluster act on the cluster.  pfs_mode is only applicable
1134  * when a PFS is mounted by the system.  pfs_nmasters is our best guess as to
1135  * how many masters have been configured for a cluster and is always
1136  * applicable.  pfs_types[] is an array with 1:1 correspondance to the
1137  * iroot cluster and describes the PFS types of the nodes making up the
1138  * cluster.
1139  *
1140  * WARNING! Portions of this structure have deferred initialization.  In
1141  *	    particular, if not mounted there will be no wthread.
1142  *	    umounted network PFSs will also be missing iroot and numerous
1143  *	    other fields will not be initialized prior to mount.
1144  *
1145  *	    Synchronization threads are chain-specific and only applicable
1146  *	    to local hard PFS entries.  A hammer2_pfs structure may contain
1147  *	    more than one when multiple hard PFSs are present on the local
1148  *	    machine which require synchronization monitoring.  Most PFSs
1149  *	    (such as snapshots) are 1xMASTER PFSs which do not need a
1150  *	    synchronization thread.
1151  *
1152  * WARNING! The chains making up pfs->iroot's cluster are accounted for in
1153  *	    hammer2_dev->mount_count when the pfs is associated with a mount
1154  *	    point.
1155  */
1156 struct hammer2_pfs {
1157 	struct mount		*mp;
1158 	TAILQ_ENTRY(hammer2_pfs) mntentry;	/* hammer2_pfslist */
1159 	uuid_t			pfs_clid;
1160 	hammer2_dev_t		*spmp_hmp;	/* only if super-root pmp */
1161 	hammer2_dev_t		*force_local;	/* only if 'local' mount */
1162 	hammer2_inode_t		*iroot;		/* PFS root inode */
1163 	uint8_t			pfs_types[HAMMER2_MAXCLUSTER];
1164 	char			*pfs_names[HAMMER2_MAXCLUSTER];
1165 	hammer2_dev_t		*pfs_hmps[HAMMER2_MAXCLUSTER];
1166 	hammer2_trans_t		trans;
1167 	struct lock		lock;		/* PFS lock for certain ops */
1168 	struct lock		lock_nlink;	/* rename and nlink lock */
1169 	struct netexport	export;		/* nfs export */
1170 	int			ronly;		/* read-only mount */
1171 	int			hflags;		/* pfs-specific mount flags */
1172 	struct malloc_type	*minode;
1173 	struct malloc_type	*mmsg;
1174 	struct spinlock		inum_spin;	/* inumber lookup */
1175 	struct hammer2_inode_tree inum_tree;	/* (not applicable to spmp) */
1176 	struct spinlock		lru_spin;	/* inumber lookup */
1177 	struct hammer2_chain_list lru_list;	/* chains on LRU */
1178 	int			lru_count;	/* #of chains on LRU */
1179 	hammer2_tid_t		modify_tid;	/* modify transaction id */
1180 	hammer2_tid_t		inode_tid;	/* inode allocator */
1181 	uint8_t			pfs_nmasters;	/* total masters */
1182 	uint8_t			pfs_mode;	/* operating mode PFSMODE */
1183 	uint8_t			unused01;
1184 	uint8_t			unused02;
1185 	int			unused03;
1186 	long			inmem_inodes;
1187 	uint32_t		inmem_dirty_chains;
1188 	int			count_lwinprog;	/* logical write in prog */
1189 	struct spinlock		list_spin;
1190 	struct h2_sideq_list	sideq;		/* last-close dirty/unlink */
1191 	hammer2_thread_t	sync_thrs[HAMMER2_MAXCLUSTER];
1192 	uint32_t		cluster_flags;	/* cached cluster flags */
1193 	int			has_xop_threads;
1194 	struct spinlock		xop_spin;	/* xop sequencer */
1195 	hammer2_xop_group_t	xop_groups[HAMMER2_XOPGROUPS];
1196 };
1197 
1198 typedef struct hammer2_pfs hammer2_pfs_t;
1199 
1200 TAILQ_HEAD(hammer2_pfslist, hammer2_pfs);
1201 
1202 #define HAMMER2_LRU_LIMIT		1024	/* per pmp lru_list */
1203 
1204 #define HAMMER2_DIRTYCHAIN_WAITING	0x80000000
1205 #define HAMMER2_DIRTYCHAIN_MASK		0x7FFFFFFF
1206 
1207 #define HAMMER2_LWINPROG_WAITING	0x80000000
1208 #define HAMMER2_LWINPROG_WAITING0	0x40000000
1209 #define HAMMER2_LWINPROG_MASK		0x3FFFFFFF
1210 
1211 /*
1212  * hammer2_cluster_check
1213  */
1214 #define HAMMER2_CHECK_NULL	0x00000001
1215 
1216 /*
1217  * Bulkscan
1218  */
1219 #define HAMMER2_BULK_ABORT	0x00000001
1220 
1221 /*
1222  * Misc
1223  */
1224 #if defined(_KERNEL)
1225 
1226 MALLOC_DECLARE(M_HAMMER2);
1227 
1228 #define VTOI(vp)	((hammer2_inode_t *)(vp)->v_data)
1229 #define ITOV(ip)	((ip)->vp)
1230 
1231 /*
1232  * Currently locked chains retain the locked buffer cache buffer for
1233  * indirect blocks, and indirect blocks can be one of two sizes.  The
1234  * device buffer has to match the case to avoid deadlocking recursive
1235  * chains that might otherwise try to access different offsets within
1236  * the same device buffer.
1237  */
1238 static __inline
1239 int
1240 hammer2_devblkradix(int radix)
1241 {
1242 #if 0
1243 	if (radix <= HAMMER2_LBUFRADIX) {
1244 		return (HAMMER2_LBUFRADIX);
1245 	} else {
1246 		return (HAMMER2_PBUFRADIX);
1247 	}
1248 #endif
1249 	return (HAMMER2_PBUFRADIX);
1250 }
1251 
1252 /*
1253  * XXX almost time to remove this.  DIO uses PBUFSIZE exclusively now.
1254  */
1255 static __inline
1256 size_t
1257 hammer2_devblksize(size_t bytes)
1258 {
1259 #if 0
1260 	if (bytes <= HAMMER2_LBUFSIZE) {
1261 		return(HAMMER2_LBUFSIZE);
1262 	} else {
1263 		KKASSERT(bytes <= HAMMER2_PBUFSIZE &&
1264 			 (bytes ^ (bytes - 1)) == ((bytes << 1) - 1));
1265 		return (HAMMER2_PBUFSIZE);
1266 	}
1267 #endif
1268 	return (HAMMER2_PBUFSIZE);
1269 }
1270 
1271 
1272 static __inline
1273 hammer2_pfs_t *
1274 MPTOPMP(struct mount *mp)
1275 {
1276 	return ((hammer2_pfs_t *)mp->mnt_data);
1277 }
1278 
1279 #define LOCKSTART	int __nlocks = curthread->td_locks
1280 #define LOCKENTER	(++curthread->td_locks)
1281 #define LOCKEXIT	(--curthread->td_locks)
1282 #define LOCKSTOP	KKASSERT(curthread->td_locks == __nlocks)
1283 
1284 extern struct vop_ops hammer2_vnode_vops;
1285 extern struct vop_ops hammer2_spec_vops;
1286 extern struct vop_ops hammer2_fifo_vops;
1287 extern struct hammer2_pfslist hammer2_pfslist;
1288 extern struct lock hammer2_mntlk;
1289 
1290 
1291 extern int hammer2_debug;
1292 extern int hammer2_cluster_read;
1293 extern int hammer2_cluster_write;
1294 extern int hammer2_dedup_enable;
1295 extern int hammer2_inval_enable;
1296 extern int hammer2_flush_pipe;
1297 extern int hammer2_synchronous_flush;
1298 extern int hammer2_dio_count;
1299 extern long hammer2_chain_allocs;
1300 extern long hammer2_chain_frees;
1301 extern long hammer2_limit_dirty_chains;
1302 extern long hammer2_count_modified_chains;
1303 extern long hammer2_iod_invals;
1304 extern long hammer2_iod_file_read;
1305 extern long hammer2_iod_meta_read;
1306 extern long hammer2_iod_indr_read;
1307 extern long hammer2_iod_fmap_read;
1308 extern long hammer2_iod_volu_read;
1309 extern long hammer2_iod_file_write;
1310 extern long hammer2_iod_file_wembed;
1311 extern long hammer2_iod_file_wzero;
1312 extern long hammer2_iod_file_wdedup;
1313 extern long hammer2_iod_meta_write;
1314 extern long hammer2_iod_indr_write;
1315 extern long hammer2_iod_fmap_write;
1316 extern long hammer2_iod_volu_write;
1317 
1318 extern long hammer2_check_xxhash64;
1319 extern long hammer2_check_icrc32;
1320 
1321 extern struct objcache *cache_buffer_read;
1322 extern struct objcache *cache_buffer_write;
1323 extern struct objcache *cache_xops;
1324 
1325 /*
1326  * hammer2_subr.c
1327  */
1328 #define hammer2_icrc32(buf, size)	iscsi_crc32((buf), (size))
1329 #define hammer2_icrc32c(buf, size, crc)	iscsi_crc32_ext((buf), (size), (crc))
1330 
1331 int hammer2_signal_check(time_t *timep);
1332 const char *hammer2_error_str(int error);
1333 
1334 void hammer2_inode_lock(hammer2_inode_t *ip, int how);
1335 void hammer2_inode_unlock(hammer2_inode_t *ip);
1336 hammer2_chain_t *hammer2_inode_chain(hammer2_inode_t *ip, int clindex, int how);
1337 hammer2_chain_t *hammer2_inode_chain_and_parent(hammer2_inode_t *ip,
1338 			int clindex, hammer2_chain_t **parentp, int how);
1339 hammer2_mtx_state_t hammer2_inode_lock_temp_release(hammer2_inode_t *ip);
1340 void hammer2_inode_lock_temp_restore(hammer2_inode_t *ip,
1341 			hammer2_mtx_state_t ostate);
1342 int hammer2_inode_lock_upgrade(hammer2_inode_t *ip);
1343 void hammer2_inode_lock_downgrade(hammer2_inode_t *ip, int);
1344 
1345 void hammer2_dev_exlock(hammer2_dev_t *hmp);
1346 void hammer2_dev_shlock(hammer2_dev_t *hmp);
1347 void hammer2_dev_unlock(hammer2_dev_t *hmp);
1348 
1349 int hammer2_get_dtype(uint8_t type);
1350 int hammer2_get_vtype(uint8_t type);
1351 uint8_t hammer2_get_obj_type(enum vtype vtype);
1352 void hammer2_time_to_timespec(uint64_t xtime, struct timespec *ts);
1353 uint64_t hammer2_timespec_to_time(const struct timespec *ts);
1354 uint32_t hammer2_to_unix_xid(const uuid_t *uuid);
1355 void hammer2_guid_to_uuid(uuid_t *uuid, uint32_t guid);
1356 void hammer2_trans_manage_init(hammer2_pfs_t *pmp);
1357 
1358 hammer2_key_t hammer2_dirhash(const unsigned char *name, size_t len);
1359 int hammer2_getradix(size_t bytes);
1360 
1361 int hammer2_calc_logical(hammer2_inode_t *ip, hammer2_off_t uoff,
1362 			hammer2_key_t *lbasep, hammer2_key_t *leofp);
1363 int hammer2_calc_physical(hammer2_inode_t *ip, hammer2_key_t lbase);
1364 void hammer2_update_time(uint64_t *timep);
1365 void hammer2_adjreadcounter(hammer2_blockref_t *bref, size_t bytes);
1366 
1367 /*
1368  * hammer2_inode.c
1369  */
1370 struct vnode *hammer2_igetv(hammer2_inode_t *ip, int *errorp);
1371 hammer2_inode_t *hammer2_inode_lookup(hammer2_pfs_t *pmp,
1372 			hammer2_tid_t inum);
1373 hammer2_inode_t *hammer2_inode_get(hammer2_pfs_t *pmp, hammer2_inode_t *dip,
1374 			hammer2_cluster_t *cluster, int idx);
1375 void hammer2_inode_free(hammer2_inode_t *ip);
1376 void hammer2_inode_ref(hammer2_inode_t *ip);
1377 void hammer2_inode_drop(hammer2_inode_t *ip);
1378 void hammer2_inode_repoint(hammer2_inode_t *ip, hammer2_inode_t *pip,
1379 			hammer2_cluster_t *cluster);
1380 void hammer2_inode_repoint_one(hammer2_inode_t *ip, hammer2_cluster_t *cluster,
1381 			int idx);
1382 void hammer2_inode_modify(hammer2_inode_t *ip);
1383 void hammer2_inode_run_sideq(hammer2_pfs_t *pmp);
1384 
1385 hammer2_inode_t *hammer2_inode_create(hammer2_inode_t *dip,
1386 			hammer2_inode_t *pip,
1387 			struct vattr *vap, struct ucred *cred,
1388 			const uint8_t *name, size_t name_len, hammer2_key_t lhc,
1389 			hammer2_key_t inum, uint8_t type, uint8_t target_type,
1390 			int flags, int *errorp);
1391 void hammer2_inode_chain_sync(hammer2_inode_t *ip);
1392 int hammer2_inode_unlink_finisher(hammer2_inode_t *ip, int isopen);
1393 int hammer2_dirent_create(hammer2_inode_t *dip, const char *name,
1394 			size_t name_len, hammer2_key_t inum, uint8_t type);
1395 
1396 /*
1397  * hammer2_chain.c
1398  */
1399 void hammer2_voldata_lock(hammer2_dev_t *hmp);
1400 void hammer2_voldata_unlock(hammer2_dev_t *hmp);
1401 void hammer2_voldata_modify(hammer2_dev_t *hmp);
1402 hammer2_chain_t *hammer2_chain_alloc(hammer2_dev_t *hmp,
1403 				hammer2_pfs_t *pmp,
1404 				hammer2_blockref_t *bref);
1405 void hammer2_chain_core_init(hammer2_chain_t *chain);
1406 void hammer2_chain_ref(hammer2_chain_t *chain);
1407 void hammer2_chain_ref_hold(hammer2_chain_t *chain);
1408 void hammer2_chain_drop(hammer2_chain_t *chain);
1409 void hammer2_chain_drop_unhold(hammer2_chain_t *chain);
1410 void hammer2_chain_lock(hammer2_chain_t *chain, int how);
1411 void hammer2_chain_lock_unhold(hammer2_chain_t *chain, int how);
1412 #if 0
1413 void hammer2_chain_push_shared_lock(hammer2_chain_t *chain);
1414 void hammer2_chain_pull_shared_lock(hammer2_chain_t *chain);
1415 #endif
1416 void hammer2_chain_load_data(hammer2_chain_t *chain);
1417 const hammer2_media_data_t *hammer2_chain_rdata(hammer2_chain_t *chain);
1418 hammer2_media_data_t *hammer2_chain_wdata(hammer2_chain_t *chain);
1419 int hammer2_chain_snapshot(hammer2_chain_t *chain, hammer2_ioc_pfs_t *pmp,
1420 				hammer2_tid_t mtid);
1421 
1422 int hammer2_chain_inode_find(hammer2_pfs_t *pmp, hammer2_key_t inum,
1423 				int clindex, int flags,
1424 				hammer2_chain_t **parentp,
1425 				hammer2_chain_t **chainp);
1426 void hammer2_chain_modify(hammer2_chain_t *chain, hammer2_tid_t mtid,
1427 				hammer2_off_t dedup_off, int flags);
1428 void hammer2_chain_modify_ip(hammer2_inode_t *ip, hammer2_chain_t *chain,
1429 				hammer2_tid_t mtid, int flags);
1430 void hammer2_chain_resize(hammer2_chain_t *chain,
1431 				hammer2_tid_t mtid, hammer2_off_t dedup_off,
1432 				int nradix, int flags);
1433 void hammer2_chain_unlock(hammer2_chain_t *chain);
1434 void hammer2_chain_unlock_hold(hammer2_chain_t *chain);
1435 void hammer2_chain_wait(hammer2_chain_t *chain);
1436 hammer2_chain_t *hammer2_chain_get(hammer2_chain_t *parent, int generation,
1437 				hammer2_blockref_t *bref);
1438 hammer2_chain_t *hammer2_chain_lookup_init(hammer2_chain_t *parent, int flags);
1439 void hammer2_chain_lookup_done(hammer2_chain_t *parent);
1440 hammer2_chain_t *hammer2_chain_getparent(hammer2_chain_t **parentp, int how);
1441 hammer2_chain_t *hammer2_chain_lookup(hammer2_chain_t **parentp,
1442 				hammer2_key_t *key_nextp,
1443 				hammer2_key_t key_beg, hammer2_key_t key_end,
1444 				int *cache_indexp, int flags);
1445 hammer2_chain_t *hammer2_chain_next(hammer2_chain_t **parentp,
1446 				hammer2_chain_t *chain,
1447 				hammer2_key_t *key_nextp,
1448 				hammer2_key_t key_beg, hammer2_key_t key_end,
1449 				int *cache_indexp, int flags);
1450 hammer2_blockref_t *hammer2_chain_scan(hammer2_chain_t *parent,
1451 				hammer2_chain_t **chainp,
1452 				hammer2_blockref_t *bref,
1453 				int *firstp, int *cache_indexp, int flags);
1454 
1455 int hammer2_chain_create(hammer2_chain_t **parentp, hammer2_chain_t **chainp,
1456 				hammer2_pfs_t *pmp, int methods,
1457 				hammer2_key_t key, int keybits,
1458 				int type, size_t bytes, hammer2_tid_t mtid,
1459 				hammer2_off_t dedup_off, int flags);
1460 void hammer2_chain_rename(hammer2_blockref_t *bref,
1461 				hammer2_chain_t **parentp,
1462 				hammer2_chain_t *chain,
1463 				hammer2_tid_t mtid, int flags);
1464 void hammer2_chain_delete(hammer2_chain_t *parent, hammer2_chain_t *chain,
1465 				hammer2_tid_t mtid, int flags);
1466 void hammer2_chain_setflush(hammer2_chain_t *chain);
1467 void hammer2_chain_countbrefs(hammer2_chain_t *chain,
1468 				hammer2_blockref_t *base, int count);
1469 hammer2_chain_t *hammer2_chain_bulksnap(hammer2_chain_t *chain);
1470 void hammer2_chain_bulkdrop(hammer2_chain_t *copy);
1471 
1472 void hammer2_chain_setcheck(hammer2_chain_t *chain, void *bdata);
1473 int hammer2_chain_testcheck(hammer2_chain_t *chain, void *bdata);
1474 int hammer2_chain_dirent_test(hammer2_chain_t *chain, const char *name,
1475 				size_t name_len);
1476 
1477 void hammer2_pfs_memory_wait(hammer2_pfs_t *pmp);
1478 void hammer2_pfs_memory_inc(hammer2_pfs_t *pmp);
1479 void hammer2_pfs_memory_wakeup(hammer2_pfs_t *pmp);
1480 
1481 void hammer2_base_delete(hammer2_chain_t *chain,
1482 				hammer2_blockref_t *base, int count,
1483 				int *cache_indexp, hammer2_chain_t *child);
1484 void hammer2_base_insert(hammer2_chain_t *chain,
1485 				hammer2_blockref_t *base, int count,
1486 				int *cache_indexp, hammer2_chain_t *child);
1487 
1488 /*
1489  * hammer2_flush.c
1490  */
1491 hammer2_chain_t *hammer2_flush_quick(hammer2_dev_t *hmp);
1492 void hammer2_flush(hammer2_chain_t *chain, int istop);
1493 void hammer2_delayed_flush(hammer2_chain_t *chain);
1494 
1495 /*
1496  * hammer2_trans.c
1497  */
1498 void hammer2_trans_init(hammer2_pfs_t *pmp, uint32_t flags);
1499 hammer2_tid_t hammer2_trans_sub(hammer2_pfs_t *pmp);
1500 void hammer2_trans_done(hammer2_pfs_t *pmp);
1501 hammer2_tid_t hammer2_trans_newinum(hammer2_pfs_t *pmp);
1502 void hammer2_trans_assert_strategy(hammer2_pfs_t *pmp);
1503 void hammer2_dedup_record(hammer2_chain_t *chain, char *data);
1504 
1505 /*
1506  * hammer2_ioctl.c
1507  */
1508 int hammer2_ioctl(hammer2_inode_t *ip, u_long com, void *data,
1509 				int fflag, struct ucred *cred);
1510 
1511 /*
1512  * hammer2_io.c
1513  */
1514 void hammer2_io_putblk(hammer2_io_t **diop);
1515 void hammer2_io_cleanup(hammer2_dev_t *hmp, struct hammer2_io_tree *tree);
1516 char *hammer2_io_data(hammer2_io_t *dio, off_t lbase);
1517 hammer2_io_t *hammer2_io_getquick(hammer2_dev_t *hmp, off_t lbase, int lsize);
1518 void hammer2_io_resetinval(hammer2_dev_t *hmp, off_t lbase);
1519 void hammer2_io_getblk(hammer2_dev_t *hmp, off_t lbase, int lsize,
1520 				hammer2_iocb_t *iocb);
1521 void hammer2_io_complete(hammer2_iocb_t *iocb);
1522 void hammer2_io_callback(struct bio *bio);
1523 void hammer2_iocb_wait(hammer2_iocb_t *iocb);
1524 int hammer2_io_new(hammer2_dev_t *hmp, int btype, off_t lbase, int lsize,
1525 				hammer2_io_t **diop);
1526 int hammer2_io_newnz(hammer2_dev_t *hmp, int btype, off_t lbase, int lsize,
1527 				hammer2_io_t **diop);
1528 void hammer2_io_newq(hammer2_dev_t *hmp, int btype, off_t lbase, int lsize);
1529 int hammer2_io_bread(hammer2_dev_t *hmp, int btype, off_t lbase, int lsize,
1530 				hammer2_io_t **diop);
1531 void hammer2_io_bawrite(hammer2_io_t **diop);
1532 void hammer2_io_bdwrite(hammer2_io_t **diop);
1533 int hammer2_io_bwrite(hammer2_io_t **diop);
1534 int hammer2_io_isdirty(hammer2_io_t *dio);
1535 void hammer2_io_setdirty(hammer2_io_t *dio);
1536 void hammer2_io_setinval(hammer2_io_t *dio, hammer2_off_t off, u_int bytes);
1537 void hammer2_io_brelse(hammer2_io_t **diop);
1538 void hammer2_io_bqrelse(hammer2_io_t **diop);
1539 int hammer2_io_crc_good(hammer2_chain_t *chain, uint64_t *maskp);
1540 void hammer2_io_crc_setmask(hammer2_io_t *dio, uint64_t mask);
1541 void hammer2_io_crc_clrmask(hammer2_io_t *dio, uint64_t mask);
1542 
1543 /*
1544  * hammer2_thread.c
1545  */
1546 void hammer2_thr_signal(hammer2_thread_t *thr, uint32_t flags);
1547 void hammer2_thr_signal2(hammer2_thread_t *thr,
1548 			uint32_t pflags, uint32_t nflags);
1549 void hammer2_thr_wait(hammer2_thread_t *thr, uint32_t flags);
1550 void hammer2_thr_wait_neg(hammer2_thread_t *thr, uint32_t flags);
1551 int hammer2_thr_wait_any(hammer2_thread_t *thr, uint32_t flags, int timo);
1552 void hammer2_thr_create(hammer2_thread_t *thr,
1553 			hammer2_pfs_t *pmp, hammer2_dev_t *hmp,
1554 			const char *id, int clindex, int repidx,
1555 			void (*func)(void *arg));
1556 void hammer2_thr_delete(hammer2_thread_t *thr);
1557 void hammer2_thr_remaster(hammer2_thread_t *thr);
1558 void hammer2_thr_freeze_async(hammer2_thread_t *thr);
1559 void hammer2_thr_freeze(hammer2_thread_t *thr);
1560 void hammer2_thr_unfreeze(hammer2_thread_t *thr);
1561 int hammer2_thr_break(hammer2_thread_t *thr);
1562 void hammer2_primary_xops_thread(void *arg);
1563 
1564 /*
1565  * hammer2_thread.c (XOP API)
1566  */
1567 void hammer2_xop_group_init(hammer2_pfs_t *pmp, hammer2_xop_group_t *xgrp);
1568 void *hammer2_xop_alloc(hammer2_inode_t *ip, int flags);
1569 void hammer2_xop_setname(hammer2_xop_head_t *xop,
1570 				const char *name, size_t name_len);
1571 void hammer2_xop_setname2(hammer2_xop_head_t *xop,
1572 				const char *name, size_t name_len);
1573 size_t hammer2_xop_setname_inum(hammer2_xop_head_t *xop, hammer2_key_t inum);
1574 void hammer2_xop_setip2(hammer2_xop_head_t *xop, hammer2_inode_t *ip2);
1575 void hammer2_xop_setip3(hammer2_xop_head_t *xop, hammer2_inode_t *ip3);
1576 void hammer2_xop_reinit(hammer2_xop_head_t *xop);
1577 void hammer2_xop_helper_create(hammer2_pfs_t *pmp);
1578 void hammer2_xop_helper_cleanup(hammer2_pfs_t *pmp);
1579 void hammer2_xop_start(hammer2_xop_head_t *xop, hammer2_xop_func_t func);
1580 void hammer2_xop_start_except(hammer2_xop_head_t *xop, hammer2_xop_func_t func,
1581 				int notidx);
1582 int hammer2_xop_collect(hammer2_xop_head_t *xop, int flags);
1583 void hammer2_xop_retire(hammer2_xop_head_t *xop, uint32_t mask);
1584 int hammer2_xop_active(hammer2_xop_head_t *xop);
1585 int hammer2_xop_feed(hammer2_xop_head_t *xop, hammer2_chain_t *chain,
1586 				int clindex, int error);
1587 
1588 /*
1589  * hammer2_synchro.c
1590  */
1591 void hammer2_primary_sync_thread(void *arg);
1592 
1593 /*
1594  * XOP backends in hammer2_xops.c, primarily for VNOPS.  Other XOP backends
1595  * may be integrated into other source files.
1596  */
1597 void hammer2_xop_ipcluster(hammer2_thread_t *thr, hammer2_xop_t *xop);
1598 void hammer2_xop_readdir(hammer2_thread_t *thr, hammer2_xop_t *xop);
1599 void hammer2_xop_nresolve(hammer2_thread_t *thr, hammer2_xop_t *xop);
1600 void hammer2_xop_unlink(hammer2_thread_t *thr, hammer2_xop_t *xop);
1601 void hammer2_xop_nrename(hammer2_thread_t *thr, hammer2_xop_t *xop);
1602 void hammer2_xop_scanlhc(hammer2_thread_t *thr, hammer2_xop_t *xop);
1603 void hammer2_xop_scanall(hammer2_thread_t *thr, hammer2_xop_t *xop);
1604 void hammer2_xop_lookup(hammer2_thread_t *thr, hammer2_xop_t *xop);
1605 void hammer2_inode_xop_mkdirent(hammer2_thread_t *thr, hammer2_xop_t *xop);
1606 void hammer2_inode_xop_create(hammer2_thread_t *thr, hammer2_xop_t *xop);
1607 void hammer2_inode_xop_destroy(hammer2_thread_t *thr, hammer2_xop_t *xop);
1608 void hammer2_inode_xop_chain_sync(hammer2_thread_t *thr, hammer2_xop_t *xop);
1609 void hammer2_inode_xop_unlinkall(hammer2_thread_t *thr, hammer2_xop_t *xop);
1610 void hammer2_inode_xop_connect(hammer2_thread_t *thr, hammer2_xop_t *xop);
1611 void hammer2_inode_xop_flush(hammer2_thread_t *thr, hammer2_xop_t *xop);
1612 
1613 /*
1614  * hammer2_msgops.c
1615  */
1616 int hammer2_msg_dbg_rcvmsg(kdmsg_msg_t *msg);
1617 int hammer2_msg_adhoc_input(kdmsg_msg_t *msg);
1618 
1619 /*
1620  * hammer2_vfsops.c
1621  */
1622 void hammer2_volconf_update(hammer2_dev_t *hmp, int index);
1623 void hammer2_dump_chain(hammer2_chain_t *chain, int tab, int *countp, char pfx);
1624 int hammer2_vfs_sync(struct mount *mp, int waitflags);
1625 hammer2_pfs_t *hammer2_pfsalloc(hammer2_chain_t *chain,
1626 				const hammer2_inode_data_t *ripdata,
1627 				hammer2_tid_t modify_tid,
1628 				hammer2_dev_t *force_local);
1629 void hammer2_pfsdealloc(hammer2_pfs_t *pmp, int clindex, int destroying);
1630 int hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
1631 				ino_t ino, struct vnode **vpp);
1632 
1633 void hammer2_lwinprog_ref(hammer2_pfs_t *pmp);
1634 void hammer2_lwinprog_drop(hammer2_pfs_t *pmp);
1635 void hammer2_lwinprog_wait(hammer2_pfs_t *pmp, int pipe);
1636 
1637 /*
1638  * hammer2_freemap.c
1639  */
1640 int hammer2_freemap_alloc(hammer2_chain_t *chain, size_t bytes);
1641 void hammer2_freemap_adjust(hammer2_dev_t *hmp,
1642 				hammer2_blockref_t *bref, int how);
1643 
1644 /*
1645  * hammer2_cluster.c
1646  */
1647 uint8_t hammer2_cluster_type(hammer2_cluster_t *cluster);
1648 const hammer2_media_data_t *hammer2_cluster_rdata(hammer2_cluster_t *cluster);
1649 hammer2_media_data_t *hammer2_cluster_wdata(hammer2_cluster_t *cluster);
1650 hammer2_cluster_t *hammer2_cluster_from_chain(hammer2_chain_t *chain);
1651 void hammer2_cluster_bref(hammer2_cluster_t *cluster, hammer2_blockref_t *bref);
1652 hammer2_cluster_t *hammer2_cluster_alloc(hammer2_pfs_t *pmp,
1653 				hammer2_blockref_t *bref);
1654 void hammer2_cluster_ref(hammer2_cluster_t *cluster);
1655 void hammer2_cluster_drop(hammer2_cluster_t *cluster);
1656 void hammer2_cluster_lock(hammer2_cluster_t *cluster, int how);
1657 int hammer2_cluster_check(hammer2_cluster_t *cluster, hammer2_key_t lokey,
1658 			int flags);
1659 void hammer2_cluster_resolve(hammer2_cluster_t *cluster);
1660 void hammer2_cluster_forcegood(hammer2_cluster_t *cluster);
1661 void hammer2_cluster_unlock(hammer2_cluster_t *cluster);
1662 
1663 void hammer2_bulkfree_init(hammer2_dev_t *hmp);
1664 void hammer2_bulkfree_uninit(hammer2_dev_t *hmp);
1665 int hammer2_bulkfree_pass(hammer2_dev_t *hmp, struct hammer2_ioc_bulkfree *bfi);
1666 
1667 /*
1668  * hammer2_iocom.c
1669  */
1670 void hammer2_iocom_init(hammer2_dev_t *hmp);
1671 void hammer2_iocom_uninit(hammer2_dev_t *hmp);
1672 void hammer2_cluster_reconnect(hammer2_dev_t *hmp, struct file *fp);
1673 
1674 /*
1675  * hammer2_strategy.c
1676  */
1677 int hammer2_vop_strategy(struct vop_strategy_args *ap);
1678 int hammer2_vop_bmap(struct vop_bmap_args *ap);
1679 void hammer2_write_thread(void *arg);
1680 void hammer2_bioq_sync(hammer2_pfs_t *pmp);
1681 void hammer2_dedup_clear(hammer2_dev_t *hmp);
1682 
1683 #endif /* !_KERNEL */
1684 #endif /* !_VFS_HAMMER2_HAMMER2_H_ */
1685