xref: /freebsd/usr.sbin/fstyp/hammer2_disk.h (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
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 #ifndef _HAMMER2_DISK_H_
37 #define _HAMMER2_DISK_H_
38 
39 #ifndef _SYS_UUID_H_
40 #include <sys/uuid.h>
41 #endif
42 #ifndef _SYS_DMSG_H_
43 /*
44  * dmsg_hdr must be 64 bytes
45  */
46 struct dmsg_hdr {
47 	uint16_t	magic;		/* 00 sanity, synchro, endian */
48 	uint16_t	reserved02;	/* 02 */
49 	uint32_t	salt;		/* 04 random salt helps w/crypto */
50 
51 	uint64_t	msgid;		/* 08 message transaction id */
52 	uint64_t	circuit;	/* 10 circuit id or 0	*/
53 	uint64_t	reserved18;	/* 18 */
54 
55 	uint32_t	cmd;		/* 20 flags | cmd | hdr_size / ALIGN */
56 	uint32_t	aux_crc;	/* 24 auxiliary data crc */
57 	uint32_t	aux_bytes;	/* 28 auxiliary data length (bytes) */
58 	uint32_t	error;		/* 2C error code or 0 */
59 	uint64_t	aux_descr;	/* 30 negotiated OOB data descr */
60 	uint32_t	reserved38;	/* 38 */
61 	uint32_t	hdr_crc;	/* 3C (aligned) extended header crc */
62 };
63 
64 typedef struct dmsg_hdr dmsg_hdr_t;
65 #endif
66 
67 /*
68  * The structures below represent the on-disk media structures for the HAMMER2
69  * filesystem.  Note that all fields for on-disk structures are naturally
70  * aligned.  The host endian format is typically used - compatibility is
71  * possible if the implementation detects reversed endian and adjusts accesses
72  * accordingly.
73  *
74  * HAMMER2 primarily revolves around the directory topology:  inodes,
75  * directory entries, and block tables.  Block device buffer cache buffers
76  * are always 64KB.  Logical file buffers are typically 16KB.  All data
77  * references utilize 64-bit byte offsets.
78  *
79  * Free block management is handled independently using blocks reserved by
80  * the media topology.
81  */
82 
83 /*
84  * The data at the end of a file or directory may be a fragment in order
85  * to optimize storage efficiency.  The minimum fragment size is 1KB.
86  * Since allocations are in powers of 2 fragments must also be sized in
87  * powers of 2 (1024, 2048, ... 65536).
88  *
89  * For the moment the maximum allocation size is HAMMER2_PBUFSIZE (64K),
90  * which is 2^16.  Larger extents may be supported in the future.  Smaller
91  * fragments might be supported in the future (down to 64 bytes is possible),
92  * but probably will not be.
93  *
94  * A full indirect block use supports 512 x 128-byte blockrefs in a 64KB
95  * buffer.  Indirect blocks down to 1KB are supported to keep small
96  * directories small.
97  *
98  * A maximally sized file (2^64-1 bytes) requires ~6 indirect block levels
99  * using 64KB indirect blocks (128 byte refs, 512 or radix 9 per indblk).
100  *
101  *	16(datablk) + 9 + 9 + 9 + 9 + 9 + 9 = ~70.
102  *	16(datablk) + 7 + 9 + 9 + 9 + 9 + 9 = ~68.  (smaller top level indblk)
103  *
104  * The actual depth depends on copies redundancy and whether the filesystem
105  * has chosen to use a smaller indirect block size at the top level or not.
106  */
107 #define HAMMER2_ALLOC_MIN	1024	/* minimum allocation size */
108 #define HAMMER2_RADIX_MIN	10	/* minimum allocation size 2^N */
109 #define HAMMER2_ALLOC_MAX	65536	/* maximum allocation size */
110 #define HAMMER2_RADIX_MAX	16	/* maximum allocation size 2^N */
111 #define HAMMER2_RADIX_KEY	64	/* number of bits in key */
112 
113 /*
114  * MINALLOCSIZE		- The minimum allocation size.  This can be smaller
115  *		  	  or larger than the minimum physical IO size.
116  *
117  *			  NOTE: Should not be larger than 1K since inodes
118  *				are 1K.
119  *
120  * MINIOSIZE		- The minimum IO size.  This must be less than
121  *			  or equal to HAMMER2_LBUFSIZE.
122  *
123  * HAMMER2_LBUFSIZE	- Nominal buffer size for I/O rollups.
124  *
125  * HAMMER2_PBUFSIZE	- Topological block size used by files for all
126  *			  blocks except the block straddling EOF.
127  *
128  * HAMMER2_SEGSIZE	- Allocation map segment size, typically 4MB
129  *			  (space represented by a level0 bitmap).
130  */
131 
132 #define HAMMER2_SEGSIZE		(1 << HAMMER2_FREEMAP_LEVEL0_RADIX)
133 #define HAMMER2_SEGRADIX	HAMMER2_FREEMAP_LEVEL0_RADIX
134 
135 #define HAMMER2_PBUFRADIX	16	/* physical buf (1<<16) bytes */
136 #define HAMMER2_PBUFSIZE	65536
137 #define HAMMER2_LBUFRADIX	14	/* logical buf (1<<14) bytes */
138 #define HAMMER2_LBUFSIZE	16384
139 
140 /*
141  * Generally speaking we want to use 16K and 64K I/Os
142  */
143 #define HAMMER2_MINIORADIX	HAMMER2_LBUFRADIX
144 #define HAMMER2_MINIOSIZE	HAMMER2_LBUFSIZE
145 
146 #define HAMMER2_IND_BYTES_MIN	4096
147 #define HAMMER2_IND_BYTES_NOM	HAMMER2_LBUFSIZE
148 #define HAMMER2_IND_BYTES_MAX	HAMMER2_PBUFSIZE
149 #define HAMMER2_IND_RADIX_MIN	12
150 #define HAMMER2_IND_RADIX_NOM	HAMMER2_LBUFRADIX
151 #define HAMMER2_IND_RADIX_MAX	HAMMER2_PBUFRADIX
152 #define HAMMER2_IND_COUNT_MIN	(HAMMER2_IND_BYTES_MIN / \
153 				 sizeof(hammer2_blockref_t))
154 #define HAMMER2_IND_COUNT_MAX	(HAMMER2_IND_BYTES_MAX / \
155 				 sizeof(hammer2_blockref_t))
156 
157 /*
158  * In HAMMER2, arrays of blockrefs are fully set-associative, meaning that
159  * any element can occur at any index and holes can be anywhere.  As a
160  * future optimization we will be able to flag that such arrays are sorted
161  * and thus optimize lookups, but for now we don't.
162  *
163  * Inodes embed either 512 bytes of direct data or an array of 4 blockrefs,
164  * resulting in highly efficient storage for files <= 512 bytes and for files
165  * <= 512KB.  Up to 4 directory entries can be referenced from a directory
166  * without requiring an indirect block.
167  *
168  * Indirect blocks are typically either 4KB (64 blockrefs / ~4MB represented),
169  * or 64KB (1024 blockrefs / ~64MB represented).
170  */
171 #define HAMMER2_SET_RADIX		2	/* radix 2 = 4 entries */
172 #define HAMMER2_SET_COUNT		(1 << HAMMER2_SET_RADIX)
173 #define HAMMER2_EMBEDDED_BYTES		512	/* inode blockset/dd size */
174 #define HAMMER2_EMBEDDED_RADIX		9
175 
176 #define HAMMER2_PBUFMASK	(HAMMER2_PBUFSIZE - 1)
177 #define HAMMER2_LBUFMASK	(HAMMER2_LBUFSIZE - 1)
178 #define HAMMER2_SEGMASK		(HAMMER2_SEGSIZE - 1)
179 
180 #define HAMMER2_LBUFMASK64	((hammer2_off_t)HAMMER2_LBUFMASK)
181 #define HAMMER2_PBUFSIZE64	((hammer2_off_t)HAMMER2_PBUFSIZE)
182 #define HAMMER2_PBUFMASK64	((hammer2_off_t)HAMMER2_PBUFMASK)
183 #define HAMMER2_SEGSIZE64	((hammer2_off_t)HAMMER2_SEGSIZE)
184 #define HAMMER2_SEGMASK64	((hammer2_off_t)HAMMER2_SEGMASK)
185 
186 #define HAMMER2_UUID_STRING	"5cbb9ad1-862d-11dc-a94d-01301bb8a9f5"
187 
188 /*
189  * A 4MB segment is reserved at the beginning of each 2GB zone.  This segment
190  * contains the volume header (or backup volume header), the free block
191  * table, and possibly other information in the future.  A 4MB segment for
192  * freemap is reserved at the beginning of every 1GB.
193  *
194  * 4MB = 64 x 64K blocks.  Each 4MB segment is broken down as follows:
195  *
196  * ==========
197  *  0 volume header (for the first four 2GB zones)
198  *  1 freemap00 level1 FREEMAP_LEAF (256 x 128B bitmap data per 1GB)
199  *  2           level2 FREEMAP_NODE (256 x 128B indirect block per 256GB)
200  *  3           level3 FREEMAP_NODE (256 x 128B indirect block per 64TB)
201  *  4           level4 FREEMAP_NODE (256 x 128B indirect block per 16PB)
202  *  5           level5 FREEMAP_NODE (256 x 128B indirect block per 4EB)
203  *  6 freemap01 level1 (rotation)
204  *  7           level2
205  *  8           level3
206  *  9           level4
207  * 10           level5
208  * 11 freemap02 level1 (rotation)
209  * 12           level2
210  * 13           level3
211  * 14           level4
212  * 15           level5
213  * 16 freemap03 level1 (rotation)
214  * 17           level2
215  * 18           level3
216  * 19           level4
217  * 20           level5
218  * 21 freemap04 level1 (rotation)
219  * 22           level2
220  * 23           level3
221  * 24           level4
222  * 25           level5
223  * 26 freemap05 level1 (rotation)
224  * 27           level2
225  * 28           level3
226  * 29           level4
227  * 30           level5
228  * 31 freemap06 level1 (rotation)
229  * 32           level2
230  * 33           level3
231  * 34           level4
232  * 35           level5
233  * 36 freemap07 level1 (rotation)
234  * 37           level2
235  * 38           level3
236  * 39           level4
237  * 40           level5
238  * 41 unused
239  * .. unused
240  * 63 unused
241  * ==========
242  *
243  * The first four 2GB zones contain volume headers and volume header backups.
244  * After that the volume header block# is reserved for future use.  Similarly,
245  * there are many blocks related to various Freemap levels which are not
246  * used in every segment and those are also reserved for future use.
247  * Note that each FREEMAP_LEAF or FREEMAP_NODE uses 32KB out of 64KB slot.
248  *
249  *			Freemap (see the FREEMAP document)
250  *
251  * The freemap utilizes blocks #1-40 in 8 sets of 5 blocks.  Each block in
252  * a set represents a level of depth in the freemap topology.  Eight sets
253  * exist to prevent live updates from disturbing the state of the freemap
254  * were a crash/reboot to occur.  That is, a live update is not committed
255  * until the update's flush reaches the volume root.  There are FOUR volume
256  * roots representing the last four synchronization points, so the freemap
257  * must be consistent no matter which volume root is chosen by the mount
258  * code.
259  *
260  * Each freemap set is 5 x 64K blocks and represents the 1GB, 256GB, 64TB,
261  * 16PB and 4EB indirect map.  The volume header itself has a set of 4 freemap
262  * blockrefs representing another 2 bits, giving us a total 64 bits of
263  * representable address space.
264  *
265  * The Level 0 64KB block represents 1GB of storage represented by 32KB
266  * (256 x struct hammer2_bmap_data).  Each structure represents 4MB of storage
267  * and has a 512 bit bitmap, using 2 bits to represent a 16KB chunk of
268  * storage.  These 2 bits represent the following states:
269  *
270  *	00	Free
271  *	01	(reserved) (Possibly partially allocated)
272  *	10	Possibly free
273  *	11	Allocated
274  *
275  * One important thing to note here is that the freemap resolution is 16KB,
276  * but the minimum storage allocation size is 1KB.  The hammer2 vfs keeps
277  * track of sub-allocations in memory, which means that on a unmount or reboot
278  * the entire 16KB of a partially allocated block will be considered fully
279  * allocated.  It is possible for fragmentation to build up over time, but
280  * defragmentation is fairly easy to accomplish since all modifications
281  * allocate a new block.
282  *
283  * The Second thing to note is that due to the way snapshots and inode
284  * replication works, deleting a file cannot immediately free the related
285  * space.  Furthermore, deletions often do not bother to traverse the
286  * block subhierarchy being deleted.  And to go even further, whole
287  * sub-directory trees can be deleted simply by deleting the directory inode
288  * at the top.  So even though we have a symbol to represent a 'possibly free'
289  * block (binary 10), only the bulk free scanning code can actually use it.
290  * Normal 'rm's or other deletions do not.
291  *
292  * WARNING!  ZONE_SEG and VOLUME_ALIGN must be a multiple of 1<<LEVEL0_RADIX
293  *	     (i.e. a multiple of 4MB).  VOLUME_ALIGN must be >= ZONE_SEG.
294  *
295  * In Summary:
296  *
297  * (1) Modifications to freemap blocks 'allocate' a new copy (aka use a block
298  *     from the next set).  The new copy is reused until a flush occurs at
299  *     which point the next modification will then rotate to the next set.
300  */
301 #define HAMMER2_VOLUME_ALIGN		(8 * 1024 * 1024)
302 #define HAMMER2_VOLUME_ALIGN64		((hammer2_off_t)HAMMER2_VOLUME_ALIGN)
303 #define HAMMER2_VOLUME_ALIGNMASK	(HAMMER2_VOLUME_ALIGN - 1)
304 #define HAMMER2_VOLUME_ALIGNMASK64     ((hammer2_off_t)HAMMER2_VOLUME_ALIGNMASK)
305 
306 #define HAMMER2_NEWFS_ALIGN		(HAMMER2_VOLUME_ALIGN)
307 #define HAMMER2_NEWFS_ALIGN64		((hammer2_off_t)HAMMER2_VOLUME_ALIGN)
308 #define HAMMER2_NEWFS_ALIGNMASK		(HAMMER2_VOLUME_ALIGN - 1)
309 #define HAMMER2_NEWFS_ALIGNMASK64	((hammer2_off_t)HAMMER2_NEWFS_ALIGNMASK)
310 
311 #define HAMMER2_ZONE_BYTES64		(2LLU * 1024 * 1024 * 1024)
312 #define HAMMER2_ZONE_MASK64		(HAMMER2_ZONE_BYTES64 - 1)
313 #define HAMMER2_ZONE_SEG		(4 * 1024 * 1024)
314 #define HAMMER2_ZONE_SEG64		((hammer2_off_t)HAMMER2_ZONE_SEG)
315 #define HAMMER2_ZONE_BLOCKS_SEG		(HAMMER2_ZONE_SEG / HAMMER2_PBUFSIZE)
316 
317 #define HAMMER2_ZONE_FREEMAP_INC	5	/* 5 deep */
318 
319 #define HAMMER2_ZONE_VOLHDR		0	/* volume header or backup */
320 #define HAMMER2_ZONE_FREEMAP_00		1	/* normal freemap rotation */
321 #define HAMMER2_ZONE_FREEMAP_01		6	/* normal freemap rotation */
322 #define HAMMER2_ZONE_FREEMAP_02		11	/* normal freemap rotation */
323 #define HAMMER2_ZONE_FREEMAP_03		16	/* normal freemap rotation */
324 #define HAMMER2_ZONE_FREEMAP_04		21	/* normal freemap rotation */
325 #define HAMMER2_ZONE_FREEMAP_05		26	/* normal freemap rotation */
326 #define HAMMER2_ZONE_FREEMAP_06		31	/* normal freemap rotation */
327 #define HAMMER2_ZONE_FREEMAP_07		36	/* normal freemap rotation */
328 #define HAMMER2_ZONE_FREEMAP_END	41	/* (non-inclusive) */
329 
330 #define HAMMER2_ZONE_UNUSED41		41
331 #define HAMMER2_ZONE_UNUSED42		42
332 #define HAMMER2_ZONE_UNUSED43		43
333 #define HAMMER2_ZONE_UNUSED44		44
334 #define HAMMER2_ZONE_UNUSED45		45
335 #define HAMMER2_ZONE_UNUSED46		46
336 #define HAMMER2_ZONE_UNUSED47		47
337 #define HAMMER2_ZONE_UNUSED48		48
338 #define HAMMER2_ZONE_UNUSED49		49
339 #define HAMMER2_ZONE_UNUSED50		50
340 #define HAMMER2_ZONE_UNUSED51		51
341 #define HAMMER2_ZONE_UNUSED52		52
342 #define HAMMER2_ZONE_UNUSED53		53
343 #define HAMMER2_ZONE_UNUSED54		54
344 #define HAMMER2_ZONE_UNUSED55		55
345 #define HAMMER2_ZONE_UNUSED56		56
346 #define HAMMER2_ZONE_UNUSED57		57
347 #define HAMMER2_ZONE_UNUSED58		58
348 #define HAMMER2_ZONE_UNUSED59		59
349 #define HAMMER2_ZONE_UNUSED60		60
350 #define HAMMER2_ZONE_UNUSED61		61
351 #define HAMMER2_ZONE_UNUSED62		62
352 #define HAMMER2_ZONE_UNUSED63		63
353 #define HAMMER2_ZONE_END		64	/* non-inclusive */
354 
355 #define HAMMER2_NFREEMAPS		8	/* FREEMAP_00 - FREEMAP_07 */
356 
357 						/* relative to FREEMAP_x */
358 #define HAMMER2_ZONEFM_LEVEL1		0	/* 1GB leafmap */
359 #define HAMMER2_ZONEFM_LEVEL2		1	/* 256GB indmap */
360 #define HAMMER2_ZONEFM_LEVEL3		2	/* 64TB indmap */
361 #define HAMMER2_ZONEFM_LEVEL4		3	/* 16PB indmap */
362 #define HAMMER2_ZONEFM_LEVEL5		4	/* 4EB indmap */
363 /* LEVEL6 is a set of 4 blockrefs in the volume header 16EB */
364 
365 /*
366  * Freemap radix.  Assumes a set-count of 4, 128-byte blockrefs,
367  * 32KB indirect block for freemap (LEVELN_PSIZE below).
368  *
369  * Leaf entry represents 4MB of storage broken down into a 512-bit
370  * bitmap, 2-bits per entry.  So course bitmap item represents 16KB.
371  */
372 #if HAMMER2_SET_COUNT != 4
373 #error "hammer2_disk.h - freemap assumes SET_COUNT is 4"
374 #endif
375 #define HAMMER2_FREEMAP_LEVEL6_RADIX	64	/* 16EB (end) */
376 #define HAMMER2_FREEMAP_LEVEL5_RADIX	62	/* 4EB */
377 #define HAMMER2_FREEMAP_LEVEL4_RADIX	54	/* 16PB */
378 #define HAMMER2_FREEMAP_LEVEL3_RADIX	46	/* 64TB */
379 #define HAMMER2_FREEMAP_LEVEL2_RADIX	38	/* 256GB */
380 #define HAMMER2_FREEMAP_LEVEL1_RADIX	30	/* 1GB */
381 #define HAMMER2_FREEMAP_LEVEL0_RADIX	22	/* 4MB (128by in l-1 leaf) */
382 
383 #define HAMMER2_FREEMAP_LEVELN_PSIZE	32768	/* physical bytes */
384 
385 #define HAMMER2_FREEMAP_LEVEL5_SIZE	((hammer2_off_t)1 <<		\
386 					 HAMMER2_FREEMAP_LEVEL5_RADIX)
387 #define HAMMER2_FREEMAP_LEVEL4_SIZE	((hammer2_off_t)1 <<		\
388 					 HAMMER2_FREEMAP_LEVEL4_RADIX)
389 #define HAMMER2_FREEMAP_LEVEL3_SIZE	((hammer2_off_t)1 <<		\
390 					 HAMMER2_FREEMAP_LEVEL3_RADIX)
391 #define HAMMER2_FREEMAP_LEVEL2_SIZE	((hammer2_off_t)1 <<		\
392 					 HAMMER2_FREEMAP_LEVEL2_RADIX)
393 #define HAMMER2_FREEMAP_LEVEL1_SIZE	((hammer2_off_t)1 <<		\
394 					 HAMMER2_FREEMAP_LEVEL1_RADIX)
395 #define HAMMER2_FREEMAP_LEVEL0_SIZE	((hammer2_off_t)1 <<		\
396 					 HAMMER2_FREEMAP_LEVEL0_RADIX)
397 
398 #define HAMMER2_FREEMAP_LEVEL5_MASK	(HAMMER2_FREEMAP_LEVEL5_SIZE - 1)
399 #define HAMMER2_FREEMAP_LEVEL4_MASK	(HAMMER2_FREEMAP_LEVEL4_SIZE - 1)
400 #define HAMMER2_FREEMAP_LEVEL3_MASK	(HAMMER2_FREEMAP_LEVEL3_SIZE - 1)
401 #define HAMMER2_FREEMAP_LEVEL2_MASK	(HAMMER2_FREEMAP_LEVEL2_SIZE - 1)
402 #define HAMMER2_FREEMAP_LEVEL1_MASK	(HAMMER2_FREEMAP_LEVEL1_SIZE - 1)
403 #define HAMMER2_FREEMAP_LEVEL0_MASK	(HAMMER2_FREEMAP_LEVEL0_SIZE - 1)
404 
405 #define HAMMER2_FREEMAP_COUNT		(int)(HAMMER2_FREEMAP_LEVELN_PSIZE / \
406 					 sizeof(hammer2_bmap_data_t))
407 
408 /*
409  * XXX I made a mistake and made the reserved area begin at each LEVEL1 zone,
410  *     which is on a 1GB demark.  This will eat a little more space but for
411  *     now we retain compatibility and make FMZONEBASE every 1GB
412  */
413 #define H2FMZONEBASE(key)	((key) & ~HAMMER2_FREEMAP_LEVEL1_MASK)
414 #define H2FMBASE(key, radix)	((key) & ~(((hammer2_off_t)1 << (radix)) - 1))
415 
416 /*
417  * 16KB bitmap granularity (x2 bits per entry).
418  */
419 #define HAMMER2_FREEMAP_BLOCK_RADIX	14
420 #define HAMMER2_FREEMAP_BLOCK_SIZE	(1 << HAMMER2_FREEMAP_BLOCK_RADIX)
421 #define HAMMER2_FREEMAP_BLOCK_MASK	(HAMMER2_FREEMAP_BLOCK_SIZE - 1)
422 
423 /*
424  * bitmap[] structure.  2 bits per HAMMER2_FREEMAP_BLOCK_SIZE.
425  *
426  * 8 x 64-bit elements, 2 bits per block.
427  * 32 blocks (radix 5) per element.
428  * representing INDEX_SIZE bytes worth of storage per element.
429  */
430 
431 typedef uint64_t			hammer2_bitmap_t;
432 
433 #define HAMMER2_BMAP_ALLONES		((hammer2_bitmap_t)-1)
434 #define HAMMER2_BMAP_ELEMENTS		8
435 #define HAMMER2_BMAP_BITS_PER_ELEMENT	64
436 #define HAMMER2_BMAP_INDEX_RADIX	5	/* 32 blocks per element */
437 #define HAMMER2_BMAP_BLOCKS_PER_ELEMENT	(1 << HAMMER2_BMAP_INDEX_RADIX)
438 
439 #define HAMMER2_BMAP_INDEX_SIZE		(HAMMER2_FREEMAP_BLOCK_SIZE * \
440 					 HAMMER2_BMAP_BLOCKS_PER_ELEMENT)
441 #define HAMMER2_BMAP_INDEX_MASK		(HAMMER2_BMAP_INDEX_SIZE - 1)
442 
443 #define HAMMER2_BMAP_SIZE		(HAMMER2_BMAP_INDEX_SIZE * \
444 					 HAMMER2_BMAP_ELEMENTS)
445 #define HAMMER2_BMAP_MASK		(HAMMER2_BMAP_SIZE - 1)
446 
447 /*
448  * Two linear areas can be reserved after the initial 4MB segment in the base
449  * zone (the one starting at offset 0).  These areas are NOT managed by the
450  * block allocator and do not fall under HAMMER2 crc checking rules based
451  * at the volume header (but can be self-CRCd internally, depending).
452  */
453 #define HAMMER2_BOOT_MIN_BYTES		HAMMER2_VOLUME_ALIGN
454 #define HAMMER2_BOOT_NOM_BYTES		(64*1024*1024)
455 #define HAMMER2_BOOT_MAX_BYTES		(256*1024*1024)
456 
457 #define HAMMER2_REDO_MIN_BYTES		HAMMER2_VOLUME_ALIGN
458 #define HAMMER2_REDO_NOM_BYTES		(256*1024*1024)
459 #define HAMMER2_REDO_MAX_BYTES		(1024*1024*1024)
460 
461 /*
462  * Most HAMMER2 types are implemented as unsigned 64-bit integers.
463  * Transaction ids are monotonic.
464  *
465  * We utilize 32-bit iSCSI CRCs.
466  */
467 typedef uint64_t hammer2_tid_t;
468 typedef uint64_t hammer2_off_t;
469 typedef uint64_t hammer2_key_t;
470 typedef uint32_t hammer2_crc32_t;
471 
472 /*
473  * Miscellaneous ranges (all are unsigned).
474  */
475 #define HAMMER2_TID_MIN		1ULL
476 #define HAMMER2_TID_MAX		0xFFFFFFFFFFFFFFFFULL
477 #define HAMMER2_KEY_MIN		0ULL
478 #define HAMMER2_KEY_MAX		0xFFFFFFFFFFFFFFFFULL
479 #define HAMMER2_OFFSET_MIN	0ULL
480 #define HAMMER2_OFFSET_MAX	0xFFFFFFFFFFFFFFFFULL
481 
482 /*
483  * HAMMER2 data offset special cases and masking.
484  *
485  * All HAMMER2 data offsets have to be broken down into a 64K buffer base
486  * offset (HAMMER2_OFF_MASK_HI) and a 64K buffer index (HAMMER2_OFF_MASK_LO).
487  *
488  * Indexes into physical buffers are always 64-byte aligned.  The low 6 bits
489  * of the data offset field specifies how large the data chunk being pointed
490  * to as a power of 2.  The theoretical minimum radix is thus 6 (The space
491  * needed in the low bits of the data offset field).  However, the practical
492  * minimum allocation chunk size is 1KB (a radix of 10), so HAMMER2 sets
493  * HAMMER2_RADIX_MIN to 10.  The maximum radix is currently 16 (64KB), but
494  * we fully intend to support larger extents in the future.
495  *
496  * WARNING! A radix of 0 (such as when data_off is all 0's) is a special
497  *	    case which means no data associated with the blockref, and
498  *	    not the '1 byte' it would otherwise calculate to.
499  */
500 #define HAMMER2_OFF_BAD		((hammer2_off_t)-1)
501 #define HAMMER2_OFF_MASK	0xFFFFFFFFFFFFFFC0ULL
502 #define HAMMER2_OFF_MASK_LO	(HAMMER2_OFF_MASK & HAMMER2_PBUFMASK64)
503 #define HAMMER2_OFF_MASK_HI	(~HAMMER2_PBUFMASK64)
504 #define HAMMER2_OFF_MASK_RADIX	0x000000000000003FULL
505 #define HAMMER2_MAX_COPIES	6
506 
507 /*
508  * HAMMER2 directory support and pre-defined keys
509  */
510 #define HAMMER2_DIRHASH_VISIBLE	0x8000000000000000ULL
511 #define HAMMER2_DIRHASH_USERMSK	0x7FFFFFFFFFFFFFFFULL
512 #define HAMMER2_DIRHASH_LOMASK	0x0000000000007FFFULL
513 #define HAMMER2_DIRHASH_HIMASK	0xFFFFFFFFFFFF0000ULL
514 #define HAMMER2_DIRHASH_FORCED	0x0000000000008000ULL	/* bit forced on */
515 
516 #define HAMMER2_SROOT_KEY	0x0000000000000000ULL	/* volume to sroot */
517 #define HAMMER2_BOOT_KEY	0xd9b36ce135528000ULL	/* sroot to BOOT PFS */
518 
519 /************************************************************************
520  *				DMSG SUPPORT				*
521  ************************************************************************
522  * LNK_VOLCONF
523  *
524  * All HAMMER2 directories directly under the super-root on your local
525  * media can be mounted separately, even if they share the same physical
526  * device.
527  *
528  * When you do a HAMMER2 mount you are effectively tying into a HAMMER2
529  * cluster via local media.  The local media does not have to participate
530  * in the cluster, other than to provide the hammer2_volconf[] array and
531  * root inode for the mount.
532  *
533  * This is important: The mount device path you specify serves to bootstrap
534  * your entry into the cluster, but your mount will make active connections
535  * to ALL copy elements in the hammer2_volconf[] array which match the
536  * PFSID of the directory in the super-root that you specified.  The local
537  * media path does not have to be mentioned in this array but becomes part
538  * of the cluster based on its type and access rights.  ALL ELEMENTS ARE
539  * TREATED ACCORDING TO TYPE NO MATTER WHICH ONE YOU MOUNT FROM.
540  *
541  * The actual cluster may be far larger than the elements you list in the
542  * hammer2_volconf[] array.  You list only the elements you wish to
543  * directly connect to and you are able to access the rest of the cluster
544  * indirectly through those connections.
545  *
546  * WARNING!  This structure must be exactly 128 bytes long for its config
547  *	     array to fit in the volume header.
548  */
549 struct hammer2_volconf {
550 	uint8_t	copyid;		/* 00	 copyid 0-255 (must match slot) */
551 	uint8_t inprog;		/* 01	 operation in progress, or 0 */
552 	uint8_t chain_to;	/* 02	 operation chaining to, or 0 */
553 	uint8_t chain_from;	/* 03	 operation chaining from, or 0 */
554 	uint16_t flags;		/* 04-05 flags field */
555 	uint8_t error;		/* 06	 last operational error */
556 	uint8_t priority;	/* 07	 priority and round-robin flag */
557 	uint8_t remote_pfs_type;/* 08	 probed direct remote PFS type */
558 	uint8_t reserved08[23];	/* 09-1F */
559 	uuid_t	pfs_clid;	/* 20-2F copy target must match this uuid */
560 	uint8_t label[16];	/* 30-3F import/export label */
561 	uint8_t path[64];	/* 40-7F target specification string or key */
562 } __packed;
563 
564 typedef struct hammer2_volconf hammer2_volconf_t;
565 
566 #define DMSG_VOLF_ENABLED	0x0001
567 #define DMSG_VOLF_INPROG	0x0002
568 #define DMSG_VOLF_CONN_RR	0x80	/* round-robin at same priority */
569 #define DMSG_VOLF_CONN_EF	0x40	/* media errors flagged */
570 #define DMSG_VOLF_CONN_PRI	0x0F	/* select priority 0-15 (15=best) */
571 
572 struct dmsg_lnk_hammer2_volconf {
573 	dmsg_hdr_t		head;
574 	hammer2_volconf_t	copy;	/* copy spec */
575 	int32_t			index;
576 	int32_t			unused01;
577 	uuid_t			mediaid;
578 	int64_t			reserved02[32];
579 } __packed;
580 
581 typedef struct dmsg_lnk_hammer2_volconf dmsg_lnk_hammer2_volconf_t;
582 
583 #define DMSG_LNK_HAMMER2_VOLCONF DMSG_LNK(DMSG_LNK_CMD_HAMMER2_VOLCONF, \
584 					  dmsg_lnk_hammer2_volconf)
585 
586 #define H2_LNK_VOLCONF(msg)	((dmsg_lnk_hammer2_volconf_t *)(msg)->any.buf)
587 
588 /*
589  * HAMMER2 directory entry header (embedded in blockref)  exactly 16 bytes
590  */
591 struct hammer2_dirent_head {
592 	hammer2_tid_t		inum;		/* inode number */
593 	uint16_t		namlen;		/* name length */
594 	uint8_t			type;		/* OBJTYPE_*	*/
595 	uint8_t			unused0B;
596 	uint8_t			unused0C[4];
597 } __packed;
598 
599 typedef struct hammer2_dirent_head hammer2_dirent_head_t;
600 
601 /*
602  * The media block reference structure.  This forms the core of the HAMMER2
603  * media topology recursion.  This 128-byte data structure is embedded in the
604  * volume header, in inodes (which are also directory entries), and in
605  * indirect blocks.
606  *
607  * A blockref references a single media item, which typically can be a
608  * directory entry (aka inode), indirect block, or data block.
609  *
610  * The primary feature a blockref represents is the ability to validate
611  * the entire tree underneath it via its check code.  Any modification to
612  * anything propagates up the blockref tree all the way to the root, replacing
613  * the related blocks and compounding the generated check code.
614  *
615  * The check code can be a simple 32-bit iscsi code, a 64-bit crc, or as
616  * complex as a 512 bit cryptographic hash.  I originally used a 64-byte
617  * blockref but later expanded it to 128 bytes to be able to support the
618  * larger check code as well as to embed statistics for quota operation.
619  *
620  * Simple check codes are not sufficient for unverified dedup.  Even with
621  * a maximally-sized check code unverified dedup should only be used in
622  * in subdirectory trees where you do not need 100% data integrity.
623  *
624  * Unverified dedup is deduping based on meta-data only without verifying
625  * that the data blocks are actually identical.  Verified dedup guarantees
626  * integrity but is a far more I/O-expensive operation.
627  *
628  * --
629  *
630  * mirror_tid - per cluster node modified (propagated upward by flush)
631  * modify_tid - clc record modified (not propagated).
632  * update_tid - clc record updated (propagated upward on verification)
633  *
634  * CLC - Stands for 'Cluster Level Change', identifiers which are identical
635  *	 within the topology across all cluster nodes (when fully
636  *	 synchronized).
637  *
638  * NOTE: The range of keys represented by the blockref is (key) to
639  *	 ((key) + (1LL << keybits) - 1).  HAMMER2 usually populates
640  *	 blocks bottom-up, inserting a new root when radix expansion
641  *	 is required.
642  *
643  * leaf_count  - Helps manage leaf collapse calculations when indirect
644  *		 blocks become mostly empty.  This value caps out at
645  *		 HAMMER2_BLOCKREF_LEAF_MAX (65535).
646  *
647  *		 Used by the chain code to determine when to pull leafs up
648  *		 from nearly empty indirect blocks.  For the purposes of this
649  *		 calculation, BREF_TYPE_INODE is considered a leaf, along
650  *		 with DIRENT and DATA.
651  *
652  *				    RESERVED FIELDS
653  *
654  * A number of blockref fields are reserved and should generally be set to
655  * 0 for future compatibility.
656  *
657  *				FUTURE BLOCKREF EXPANSION
658  *
659  * CONTENT ADDRESSABLE INDEXING (future) - Using a 256 or 512-bit check code.
660  */
661 struct hammer2_blockref {		/* MUST BE EXACTLY 64 BYTES */
662 	uint8_t		type;		/* type of underlying item */
663 	uint8_t		methods;	/* check method & compression method */
664 	uint8_t		copyid;		/* specify which copy this is */
665 	uint8_t		keybits;	/* #of keybits masked off 0=leaf */
666 	uint8_t		vradix;		/* virtual data/meta-data size */
667 	uint8_t		flags;		/* blockref flags */
668 	uint16_t	leaf_count;	/* leaf aggregation count */
669 	hammer2_key_t	key;		/* key specification */
670 	hammer2_tid_t	mirror_tid;	/* media flush topology & freemap */
671 	hammer2_tid_t	modify_tid;	/* clc modify (not propagated) */
672 	hammer2_off_t	data_off;	/* low 6 bits is phys size (radix)*/
673 	hammer2_tid_t	update_tid;	/* clc modify (propagated upward) */
674 	union {
675 		char	buf[16];
676 
677 		/*
678 		 * Directory entry header (BREF_TYPE_DIRENT)
679 		 *
680 		 * NOTE: check.buf contains filename if <= 64 bytes.  Longer
681 		 *	 filenames are stored in a data reference of size
682 		 *	 HAMMER2_ALLOC_MIN (at least 256, typically 1024).
683 		 *
684 		 * NOTE: inode structure may contain a copy of a recently
685 		 *	 associated filename, for recovery purposes.
686 		 *
687 		 * NOTE: Superroot entries are INODEs, not DIRENTs.  Code
688 		 *	 allows both cases.
689 		 */
690 		hammer2_dirent_head_t dirent;
691 
692 		/*
693 		 * Statistics aggregation (BREF_TYPE_INODE, BREF_TYPE_INDIRECT)
694 		 */
695 		struct {
696 			hammer2_key_t	data_count;
697 			hammer2_key_t	inode_count;
698 		} stats;
699 	} embed;
700 	union {				/* check info */
701 		char	buf[64];
702 		struct {
703 			uint32_t value;
704 			uint32_t reserved[15];
705 		} iscsi32;
706 		struct {
707 			uint64_t value;
708 			uint64_t reserved[7];
709 		} xxhash64;
710 		struct {
711 			char data[24];
712 			char reserved[40];
713 		} sha192;
714 		struct {
715 			char data[32];
716 			char reserved[32];
717 		} sha256;
718 		struct {
719 			char data[64];
720 		} sha512;
721 
722 		/*
723 		 * Freemap hints are embedded in addition to the icrc32.
724 		 *
725 		 * bigmask - Radixes available for allocation (0-31).
726 		 *	     Heuristical (may be permissive but not
727 		 *	     restrictive).  Typically only radix values
728 		 *	     10-16 are used (i.e. (1<<10) through (1<<16)).
729 		 *
730 		 * avail   - Total available space remaining, in bytes
731 		 */
732 		struct {
733 			uint32_t icrc32;
734 			uint32_t bigmask;	/* available radixes */
735 			uint64_t avail;		/* total available bytes */
736 			char reserved[48];
737 		} freemap;
738 	} check;
739 } __packed;
740 
741 typedef struct hammer2_blockref hammer2_blockref_t;
742 
743 #define HAMMER2_BLOCKREF_BYTES		128	/* blockref struct in bytes */
744 #define HAMMER2_BLOCKREF_RADIX		7
745 
746 #define HAMMER2_BLOCKREF_LEAF_MAX	65535
747 
748 /*
749  * On-media and off-media blockref types.
750  *
751  * types >= 128 are pseudo values that should never be present on-media.
752  */
753 #define HAMMER2_BREF_TYPE_EMPTY		0
754 #define HAMMER2_BREF_TYPE_INODE		1
755 #define HAMMER2_BREF_TYPE_INDIRECT	2
756 #define HAMMER2_BREF_TYPE_DATA		3
757 #define HAMMER2_BREF_TYPE_DIRENT	4
758 #define HAMMER2_BREF_TYPE_FREEMAP_NODE	5
759 #define HAMMER2_BREF_TYPE_FREEMAP_LEAF	6
760 #define HAMMER2_BREF_TYPE_FREEMAP	254	/* pseudo-type */
761 #define HAMMER2_BREF_TYPE_VOLUME	255	/* pseudo-type */
762 
763 #define HAMMER2_BREF_FLAG_PFSROOT	0x01	/* see also related opflag */
764 #define HAMMER2_BREF_FLAG_ZERO		0x02
765 
766 /*
767  * Encode/decode check mode and compression mode for
768  * bref.methods.  The compression level is not encoded in
769  * bref.methods.
770  */
771 #define HAMMER2_ENC_CHECK(n)		(((n) & 15) << 4)
772 #define HAMMER2_DEC_CHECK(n)		(((n) >> 4) & 15)
773 #define HAMMER2_ENC_COMP(n)		((n) & 15)
774 #define HAMMER2_DEC_COMP(n)		((n) & 15)
775 
776 #define HAMMER2_CHECK_NONE		0
777 #define HAMMER2_CHECK_DISABLED		1
778 #define HAMMER2_CHECK_ISCSI32		2
779 #define HAMMER2_CHECK_XXHASH64		3
780 #define HAMMER2_CHECK_SHA192		4
781 #define HAMMER2_CHECK_FREEMAP		5
782 
783 #define HAMMER2_CHECK_DEFAULT		HAMMER2_CHECK_XXHASH64
784 
785 /* user-specifiable check modes only */
786 #define HAMMER2_CHECK_STRINGS		{ "none", "disabled", "crc32", \
787 					  "xxhash64", "sha192" }
788 #define HAMMER2_CHECK_STRINGS_COUNT	5
789 
790 /*
791  * Encode/decode check or compression algorithm request in
792  * ipdata->meta.check_algo and ipdata->meta.comp_algo.
793  */
794 #define HAMMER2_ENC_ALGO(n)		(n)
795 #define HAMMER2_DEC_ALGO(n)		((n) & 15)
796 #define HAMMER2_ENC_LEVEL(n)		((n) << 4)
797 #define HAMMER2_DEC_LEVEL(n)		(((n) >> 4) & 15)
798 
799 #define HAMMER2_COMP_NONE		0
800 #define HAMMER2_COMP_AUTOZERO		1
801 #define HAMMER2_COMP_LZ4		2
802 #define HAMMER2_COMP_ZLIB		3
803 
804 #define HAMMER2_COMP_NEWFS_DEFAULT	HAMMER2_COMP_LZ4
805 #define HAMMER2_COMP_STRINGS		{ "none", "autozero", "lz4", "zlib" }
806 #define HAMMER2_COMP_STRINGS_COUNT	4
807 
808 /*
809  * Passed to hammer2_chain_create(), causes methods to be inherited from
810  * parent.
811  */
812 #define HAMMER2_METH_DEFAULT		-1
813 
814 /*
815  * HAMMER2 block references are collected into sets of 4 blockrefs.  These
816  * sets are fully associative, meaning the elements making up a set are
817  * not sorted in any way and may contain duplicate entries, holes, or
818  * entries which shortcut multiple levels of indirection.  Sets are used
819  * in various ways:
820  *
821  * (1) When redundancy is desired a set may contain several duplicate
822  *     entries pointing to different copies of the same data.  Up to 4 copies
823  *     are supported.
824  *
825  * (2) The blockrefs in a set can shortcut multiple levels of indirections
826  *     within the bounds imposed by the parent of set.
827  *
828  * When a set fills up another level of indirection is inserted, moving
829  * some or all of the set's contents into indirect blocks placed under the
830  * set.  This is a top-down approach in that indirect blocks are not created
831  * until the set actually becomes full (that is, the entries in the set can
832  * shortcut the indirect blocks when the set is not full).  Depending on how
833  * things are filled multiple indirect blocks will eventually be created.
834  *
835  * Indirect blocks are typically 4KB (64 entres) or 64KB (1024 entries) and
836  * are also treated as fully set-associative.
837  */
838 struct hammer2_blockset {
839 	hammer2_blockref_t	blockref[HAMMER2_SET_COUNT];
840 };
841 
842 typedef struct hammer2_blockset hammer2_blockset_t;
843 
844 /*
845  * Catch programmer snafus
846  */
847 #if (1 << HAMMER2_SET_RADIX) != HAMMER2_SET_COUNT
848 #error "hammer2 direct radix is incorrect"
849 #endif
850 #if (1 << HAMMER2_PBUFRADIX) != HAMMER2_PBUFSIZE
851 #error "HAMMER2_PBUFRADIX and HAMMER2_PBUFSIZE are inconsistent"
852 #endif
853 #if (1 << HAMMER2_RADIX_MIN) != HAMMER2_ALLOC_MIN
854 #error "HAMMER2_RADIX_MIN and HAMMER2_ALLOC_MIN are inconsistent"
855 #endif
856 
857 /*
858  * hammer2_bmap_data - A freemap entry in the LEVEL1 block.
859  *
860  * Each 128-byte entry contains the bitmap and meta-data required to manage
861  * a LEVEL0 (4MB) block of storage.  The storage is managed in 256 x 16KB
862  * chunks.
863  *
864  * A smaller allocation granularity is supported via a linear iterator and/or
865  * must otherwise be tracked in ram.
866  *
867  * (data structure must be 128 bytes exactly)
868  *
869  * linear  - A BYTE linear allocation offset used for sub-16KB allocations
870  *	     only.  May contain values between 0 and 4MB.  Must be ignored
871  *	     if 16KB-aligned (i.e. force bitmap scan), otherwise may be
872  *	     used to sub-allocate within the 16KB block (which is already
873  *	     marked as allocated in the bitmap).
874  *
875  *	     Sub-allocations need only be 1KB-aligned and do not have to be
876  *	     size-aligned, and 16KB or larger allocations do not update this
877  *	     field, resulting in pretty good packing.
878  *
879  *	     Please note that file data granularity may be limited by
880  *	     other issues such as buffer cache direct-mapping and the
881  *	     desire to support sector sizes up to 16KB (so H2 only issues
882  *	     I/O's in multiples of 16KB anyway).
883  *
884  * class   - Clustering class.  Cleared to 0 only if the entire leaf becomes
885  *	     free.  Used to cluster device buffers so all elements must have
886  *	     the same device block size, but may mix logical sizes.
887  *
888  *	     Typically integrated with the blockref type in the upper 8 bits
889  *	     to localize inodes and indrect blocks, improving bulk free scans
890  *	     and directory scans.
891  *
892  * bitmap  - Two bits per 16KB allocation block arranged in arrays of
893  *	     64-bit elements, 256x2 bits representing ~4MB worth of media
894  *	     storage.  Bit patterns are as follows:
895  *
896  *	     00	Unallocated
897  *	     01 (reserved)
898  *	     10 Possibly free
899  *           11 Allocated
900  */
901 struct hammer2_bmap_data {
902 	int32_t linear;		/* 00 linear sub-granular allocation offset */
903 	uint16_t class;		/* 04-05 clustering class ((type<<8)|radix) */
904 	uint8_t reserved06;	/* 06 */
905 	uint8_t reserved07;	/* 07 */
906 	uint32_t reserved08;	/* 08 */
907 	uint32_t reserved0C;	/* 0C */
908 	uint32_t reserved10;	/* 10 */
909 	uint32_t reserved14;	/* 14 */
910 	uint32_t reserved18;	/* 18 */
911 	uint32_t avail;		/* 1C */
912 	uint32_t reserved20[8];	/* 20-3F 256 bits manages 128K/1KB/2-bits */
913 				/* 40-7F 512 bits manages 4MB of storage */
914 	hammer2_bitmap_t bitmapq[HAMMER2_BMAP_ELEMENTS];
915 } __packed;
916 
917 typedef struct hammer2_bmap_data hammer2_bmap_data_t;
918 
919 /*
920  * XXX "Inodes ARE directory entries" is no longer the case.  Hardlinks are
921  * dirents which refer to the same inode#, which is how filesystems usually
922  * implement hardlink.  The following comments need to be updated.
923  *
924  * In HAMMER2 inodes ARE directory entries, with a special exception for
925  * hardlinks.  The inode number is stored in the inode rather than being
926  * based on the location of the inode (since the location moves every time
927  * the inode or anything underneath the inode is modified).
928  *
929  * The inode is 1024 bytes, made up of 256 bytes of meta-data, 256 bytes
930  * for the filename, and 512 bytes worth of direct file data OR an embedded
931  * blockset.  The in-memory hammer2_inode structure contains only the mostly-
932  * node-independent meta-data portion (some flags are node-specific and will
933  * not be synchronized).  The rest of the inode is node-specific and chain I/O
934  * is required to obtain it.
935  *
936  * Directories represent one inode per blockref.  Inodes are not laid out
937  * as a file but instead are represented by the related blockrefs.  The
938  * blockrefs, in turn, are indexed by the 64-bit directory hash key.  Remember
939  * that blocksets are fully associative, so a certain degree efficiency is
940  * achieved just from that.
941  *
942  * Up to 512 bytes of direct data can be embedded in an inode, and since
943  * inodes are essentially directory entries this also means that small data
944  * files end up simply being laid out linearly in the directory, resulting
945  * in fewer seeks and highly optimal access.
946  *
947  * The compression mode can be changed at any time in the inode and is
948  * recorded on a blockref-by-blockref basis.
949  *
950  * Hardlinks are supported via the inode map.  Essentially the way a hardlink
951  * works is that all individual directory entries representing the same file
952  * are special cased and specify the same inode number.  The actual file
953  * is placed in the nearest parent directory that is parent to all instances
954  * of the hardlink.  If all hardlinks to a file are in the same directory
955  * the actual file will also be placed in that directory.  This file uses
956  * the inode number as the directory entry key and is invisible to normal
957  * directory scans.  Real directory entry keys are differentiated from the
958  * inode number key via bit 63.  Access to the hardlink silently looks up
959  * the real file and forwards all operations to that file.  Removal of the
960  * last hardlink also removes the real file.
961  */
962 #define HAMMER2_INODE_BYTES		1024	/* (asserted by code) */
963 #define HAMMER2_INODE_MAXNAME		256	/* maximum name in bytes */
964 #define HAMMER2_INODE_VERSION_ONE	1
965 
966 #define HAMMER2_INODE_START		1024	/* dynamically allocated */
967 
968 struct hammer2_inode_meta {
969 	uint16_t	version;	/* 0000 inode data version */
970 	uint8_t		reserved02;	/* 0002 */
971 	uint8_t		pfs_subtype;	/* 0003 pfs sub-type */
972 
973 	/*
974 	 * core inode attributes, inode type, misc flags
975 	 */
976 	uint32_t	uflags;		/* 0004 chflags */
977 	uint32_t	rmajor;		/* 0008 available for device nodes */
978 	uint32_t	rminor;		/* 000C available for device nodes */
979 	uint64_t	ctime;		/* 0010 inode change time */
980 	uint64_t	mtime;		/* 0018 modified time */
981 	uint64_t	atime;		/* 0020 access time (unsupported) */
982 	uint64_t	btime;		/* 0028 birth time */
983 	uuid_t		uid;		/* 0030 uid / degenerate unix uid */
984 	uuid_t		gid;		/* 0040 gid / degenerate unix gid */
985 
986 	uint8_t		type;		/* 0050 object type */
987 	uint8_t		op_flags;	/* 0051 operational flags */
988 	uint16_t	cap_flags;	/* 0052 capability flags */
989 	uint32_t	mode;		/* 0054 unix modes (typ low 16 bits) */
990 
991 	/*
992 	 * inode size, identification, localized recursive configuration
993 	 * for compression and backup copies.
994 	 *
995 	 * NOTE: Nominal parent inode number (iparent) is only applicable
996 	 *	 for directories but can also help for files during
997 	 *	 catastrophic recovery.
998 	 */
999 	hammer2_tid_t	inum;		/* 0058 inode number */
1000 	hammer2_off_t	size;		/* 0060 size of file */
1001 	uint64_t	nlinks;		/* 0068 hard links (typ only dirs) */
1002 	hammer2_tid_t	iparent;	/* 0070 nominal parent inum */
1003 	hammer2_key_t	name_key;	/* 0078 full filename key */
1004 	uint16_t	name_len;	/* 0080 filename length */
1005 	uint8_t		ncopies;	/* 0082 ncopies to local media */
1006 	uint8_t		comp_algo;	/* 0083 compression request & algo */
1007 
1008 	/*
1009 	 * These fields are currently only applicable to PFSROOTs.
1010 	 *
1011 	 * NOTE: We can't use {volume_data->fsid, pfs_clid} to uniquely
1012 	 *	 identify an instance of a PFS in the cluster because
1013 	 *	 a mount may contain more than one copy of the PFS as
1014 	 *	 a separate node.  {pfs_clid, pfs_fsid} must be used for
1015 	 *	 registration in the cluster.
1016 	 */
1017 	uint8_t		target_type;	/* 0084 hardlink target type */
1018 	uint8_t		check_algo;	/* 0085 check code request & algo */
1019 	uint8_t		pfs_nmasters;	/* 0086 (if PFSROOT) if multi-master */
1020 	uint8_t		pfs_type;	/* 0087 (if PFSROOT) node type */
1021 	uint64_t	pfs_inum;	/* 0088 (if PFSROOT) inum allocator */
1022 	uuid_t		pfs_clid;	/* 0090 (if PFSROOT) cluster uuid */
1023 	uuid_t		pfs_fsid;	/* 00A0 (if PFSROOT) unique uuid */
1024 
1025 	/*
1026 	 * Quotas and aggregate sub-tree inode and data counters.  Note that
1027 	 * quotas are not replicated downward, they are explicitly set by
1028 	 * the sysop and in-memory structures keep track of inheritance.
1029 	 */
1030 	hammer2_key_t	data_quota;	/* 00B0 subtree quota in bytes */
1031 	hammer2_key_t	unusedB8;	/* 00B8 subtree byte count */
1032 	hammer2_key_t	inode_quota;	/* 00C0 subtree quota inode count */
1033 	hammer2_key_t	unusedC8;	/* 00C8 subtree inode count */
1034 
1035 	/*
1036 	 * The last snapshot tid is tested against modify_tid to determine
1037 	 * when a copy must be made of a data block whos check mode has been
1038 	 * disabled (a disabled check mode allows data blocks to be updated
1039 	 * in place instead of copy-on-write).
1040 	 */
1041 	hammer2_tid_t	pfs_lsnap_tid;	/* 00D0 last snapshot tid */
1042 	hammer2_tid_t	reservedD8;	/* 00D8 (avail) */
1043 
1044 	/*
1045 	 * Tracks (possibly degenerate) free areas covering all sub-tree
1046 	 * allocations under inode, not counting the inode itself.
1047 	 * 0/0 indicates empty entry.  fully set-associative.
1048 	 *
1049 	 * (not yet implemented)
1050 	 */
1051 	uint64_t	decrypt_check;	/* 00E0 decryption validator */
1052 	hammer2_off_t	reservedE0[3];	/* 00E8/F0/F8 */
1053 } __packed;
1054 
1055 typedef struct hammer2_inode_meta hammer2_inode_meta_t;
1056 
1057 struct hammer2_inode_data {
1058 	hammer2_inode_meta_t	meta;	/* 0000-00FF */
1059 	unsigned char	filename[HAMMER2_INODE_MAXNAME];
1060 					/* 0100-01FF (256 char, unterminated) */
1061 	union {				/* 0200-03FF (64x8 = 512 bytes) */
1062 		hammer2_blockset_t blockset;
1063 		char data[HAMMER2_EMBEDDED_BYTES];
1064 	} u;
1065 } __packed;
1066 
1067 typedef struct hammer2_inode_data hammer2_inode_data_t;
1068 
1069 #define HAMMER2_OPFLAG_DIRECTDATA	0x01
1070 #define HAMMER2_OPFLAG_PFSROOT		0x02	/* (see also bref flag) */
1071 #define HAMMER2_OPFLAG_COPYIDS		0x04	/* copyids override parent */
1072 
1073 #define HAMMER2_OBJTYPE_UNKNOWN		0
1074 #define HAMMER2_OBJTYPE_DIRECTORY	1
1075 #define HAMMER2_OBJTYPE_REGFILE		2
1076 #define HAMMER2_OBJTYPE_FIFO		4
1077 #define HAMMER2_OBJTYPE_CDEV		5
1078 #define HAMMER2_OBJTYPE_BDEV		6
1079 #define HAMMER2_OBJTYPE_SOFTLINK	7
1080 #define HAMMER2_OBJTYPE_UNUSED08	8
1081 #define HAMMER2_OBJTYPE_SOCKET		9
1082 #define HAMMER2_OBJTYPE_WHITEOUT	10
1083 
1084 #define HAMMER2_COPYID_NONE		0
1085 #define HAMMER2_COPYID_LOCAL		((uint8_t)-1)
1086 
1087 #define HAMMER2_COPYID_COUNT		256
1088 
1089 /*
1090  * PFS types identify the role of a PFS within a cluster.  The PFS types
1091  * is stored on media and in LNK_SPAN messages and used in other places.
1092  *
1093  * The low 4 bits specify the current active type while the high 4 bits
1094  * specify the transition target if the PFS is being upgraded or downgraded,
1095  * If the upper 4 bits are not zero it may effect how a PFS is used during
1096  * the transition.
1097  *
1098  * Generally speaking, downgrading a MASTER to a SLAVE cannot complete until
1099  * at least all MASTERs have updated their pfs_nmasters field.  And upgrading
1100  * a SLAVE to a MASTER cannot complete until the new prospective master has
1101  * been fully synchronized (though theoretically full synchronization is
1102  * not required if a (new) quorum of other masters are fully synchronized).
1103  *
1104  * It generally does not matter which PFS element you actually mount, you
1105  * are mounting 'the cluster'.  So, for example, a network mount will mount
1106  * a DUMMY PFS type on a memory filesystem.  However, there are two exceptions.
1107  * In order to gain the benefits of a SOFT_MASTER or SOFT_SLAVE, those PFSs
1108  * must be directly mounted.
1109  */
1110 #define HAMMER2_PFSTYPE_NONE		0x00
1111 #define HAMMER2_PFSTYPE_CACHE		0x01
1112 #define HAMMER2_PFSTYPE_UNUSED02	0x02
1113 #define HAMMER2_PFSTYPE_SLAVE		0x03
1114 #define HAMMER2_PFSTYPE_SOFT_SLAVE	0x04
1115 #define HAMMER2_PFSTYPE_SOFT_MASTER	0x05
1116 #define HAMMER2_PFSTYPE_MASTER		0x06
1117 #define HAMMER2_PFSTYPE_UNUSED07	0x07
1118 #define HAMMER2_PFSTYPE_SUPROOT		0x08
1119 #define HAMMER2_PFSTYPE_DUMMY		0x09
1120 #define HAMMER2_PFSTYPE_MAX		16
1121 
1122 #define HAMMER2_PFSTRAN_NONE		0x00	/* no transition in progress */
1123 #define HAMMER2_PFSTRAN_CACHE		0x10
1124 #define HAMMER2_PFSTRAN_UNMUSED20	0x20
1125 #define HAMMER2_PFSTRAN_SLAVE		0x30
1126 #define HAMMER2_PFSTRAN_SOFT_SLAVE	0x40
1127 #define HAMMER2_PFSTRAN_SOFT_MASTER	0x50
1128 #define HAMMER2_PFSTRAN_MASTER		0x60
1129 #define HAMMER2_PFSTRAN_UNUSED70	0x70
1130 #define HAMMER2_PFSTRAN_SUPROOT		0x80
1131 #define HAMMER2_PFSTRAN_DUMMY		0x90
1132 
1133 #define HAMMER2_PFS_DEC(n)		((n) & 0x0F)
1134 #define HAMMER2_PFS_DEC_TRANSITION(n)	(((n) >> 4) & 0x0F)
1135 #define HAMMER2_PFS_ENC_TRANSITION(n)	(((n) & 0x0F) << 4)
1136 
1137 #define HAMMER2_PFSSUBTYPE_NONE		0
1138 #define HAMMER2_PFSSUBTYPE_SNAPSHOT	1	/* manual/managed snapshot */
1139 #define HAMMER2_PFSSUBTYPE_AUTOSNAP	2	/* automatic snapshot */
1140 
1141 /*
1142  * PFS mode of operation is a bitmask.  This is typically not stored
1143  * on-media, but defined here because the field may be used in dmsgs.
1144  */
1145 #define HAMMER2_PFSMODE_QUORUM		0x01
1146 #define HAMMER2_PFSMODE_RW		0x02
1147 
1148 /*
1149  *				Allocation Table
1150  *
1151  */
1152 
1153 
1154 /*
1155  * Flags (8 bits) - blockref, for freemap only
1156  *
1157  * Note that the minimum chunk size is 1KB so we could theoretically have
1158  * 10 bits here, but we might have some future extension that allows a
1159  * chunk size down to 256 bytes and if so we will need bits 8 and 9.
1160  */
1161 #define HAMMER2_AVF_SELMASK		0x03	/* select group */
1162 #define HAMMER2_AVF_ALL_ALLOC		0x04	/* indicate all allocated */
1163 #define HAMMER2_AVF_ALL_FREE		0x08	/* indicate all free */
1164 #define HAMMER2_AVF_RESERVED10		0x10
1165 #define HAMMER2_AVF_RESERVED20		0x20
1166 #define HAMMER2_AVF_RESERVED40		0x40
1167 #define HAMMER2_AVF_RESERVED80		0x80
1168 #define HAMMER2_AVF_AVMASK32		((uint32_t)0xFFFFFF00LU)
1169 #define HAMMER2_AVF_AVMASK64		((uint64_t)0xFFFFFFFFFFFFFF00LLU)
1170 
1171 #define HAMMER2_AV_SELECT_A		0x00
1172 #define HAMMER2_AV_SELECT_B		0x01
1173 #define HAMMER2_AV_SELECT_C		0x02
1174 #define HAMMER2_AV_SELECT_D		0x03
1175 
1176 /*
1177  * The volume header eats a 64K block.  There is currently an issue where
1178  * we want to try to fit all nominal filesystem updates in a 512-byte section
1179  * but it may be a lost cause due to the need for a blockset.
1180  *
1181  * All information is stored in host byte order.  The volume header's magic
1182  * number may be checked to determine the byte order.  If you wish to mount
1183  * between machines w/ different endian modes you'll need filesystem code
1184  * which acts on the media data consistently (either all one way or all the
1185  * other).  Our code currently does not do that.
1186  *
1187  * A read-write mount may have to recover missing allocations by doing an
1188  * incremental mirror scan looking for modifications made after alloc_tid.
1189  * If alloc_tid == last_tid then no recovery operation is needed.  Recovery
1190  * operations are usually very, very fast.
1191  *
1192  * Read-only mounts do not need to do any recovery, access to the filesystem
1193  * topology is always consistent after a crash (is always consistent, period).
1194  * However, there may be shortcutted blockref updates present from deep in
1195  * the tree which are stored in the volumeh eader and must be tracked on
1196  * the fly.
1197  *
1198  * NOTE: The copyinfo[] array contains the configuration for both the
1199  *	 cluster connections and any local media copies.  The volume
1200  *	 header will be replicated for each local media copy.
1201  *
1202  *	 The mount command may specify multiple medias or just one and
1203  *	 allow HAMMER2 to pick up the others when it checks the copyinfo[]
1204  *	 array on mount.
1205  *
1206  * NOTE: root_blockref points to the super-root directory, not the root
1207  *	 directory.  The root directory will be a subdirectory under the
1208  *	 super-root.
1209  *
1210  *	 The super-root directory contains all root directories and all
1211  *	 snapshots (readonly or writable).  It is possible to do a
1212  *	 null-mount of the super-root using special path constructions
1213  *	 relative to your mounted root.
1214  *
1215  * NOTE: HAMMER2 allows any subdirectory tree to be managed as if it were
1216  *	 a PFS, including mirroring and storage quota operations, and this is
1217  *	 preferred over creating discrete PFSs in the super-root.  Instead
1218  *	 the super-root is most typically used to create writable snapshots,
1219  *	 alternative roots, and so forth.  The super-root is also used by
1220  *	 the automatic snapshotting mechanism.
1221  */
1222 #define HAMMER2_VOLUME_ID_HBO	0x48414d3205172011LLU
1223 #define HAMMER2_VOLUME_ID_ABO	0x11201705324d4148LLU
1224 
1225 struct hammer2_volume_data {
1226 	/*
1227 	 * sector #0 - 512 bytes
1228 	 */
1229 	uint64_t	magic;			/* 0000 Signature */
1230 	hammer2_off_t	boot_beg;		/* 0008 Boot area (future) */
1231 	hammer2_off_t	boot_end;		/* 0010 (size = end - beg) */
1232 	hammer2_off_t	aux_beg;		/* 0018 Aux area (future) */
1233 	hammer2_off_t	aux_end;		/* 0020 (size = end - beg) */
1234 	hammer2_off_t	volu_size;		/* 0028 Volume size, bytes */
1235 
1236 	uint32_t	version;		/* 0030 */
1237 	uint32_t	flags;			/* 0034 */
1238 	uint8_t		copyid;			/* 0038 copyid of phys vol */
1239 	uint8_t		freemap_version;	/* 0039 freemap algorithm */
1240 	uint8_t		peer_type;		/* 003A HAMMER2_PEER_xxx */
1241 	uint8_t		reserved003B;		/* 003B */
1242 	uint32_t	reserved003C;		/* 003C */
1243 
1244 	uuid_t		fsid;			/* 0040 */
1245 	uuid_t		fstype;			/* 0050 */
1246 
1247 	/*
1248 	 * allocator_size is precalculated at newfs time and does not include
1249 	 * reserved blocks, boot, or redo areas.
1250 	 *
1251 	 * Initial non-reserved-area allocations do not use the freemap
1252 	 * but instead adjust alloc_iterator.  Dynamic allocations take
1253 	 * over starting at (allocator_beg).  This makes newfs_hammer2's
1254 	 * job a lot easier and can also serve as a testing jig.
1255 	 */
1256 	hammer2_off_t	allocator_size;		/* 0060 Total data space */
1257 	hammer2_off_t   allocator_free;		/* 0068	Free space */
1258 	hammer2_off_t	allocator_beg;		/* 0070 Initial allocations */
1259 
1260 	/*
1261 	 * mirror_tid reflects the highest committed change for this
1262 	 * block device regardless of whether it is to the super-root
1263 	 * or to a PFS or whatever.
1264 	 *
1265 	 * freemap_tid reflects the highest committed freemap change for
1266 	 * this block device.
1267 	 */
1268 	hammer2_tid_t	mirror_tid;		/* 0078 committed tid (vol) */
1269 	hammer2_tid_t	reserved0080;		/* 0080 */
1270 	hammer2_tid_t	reserved0088;		/* 0088 */
1271 	hammer2_tid_t	freemap_tid;		/* 0090 committed tid (fmap) */
1272 	hammer2_tid_t	bulkfree_tid;		/* 0098 bulkfree incremental */
1273 	hammer2_tid_t	reserved00A0[5];	/* 00A0-00C7 */
1274 
1275 	/*
1276 	 * Copyids are allocated dynamically from the copyexists bitmap.
1277 	 * An id from the active copies set (up to 8, see copyinfo later on)
1278 	 * may still exist after the copy set has been removed from the
1279 	 * volume header and its bit will remain active in the bitmap and
1280 	 * cannot be reused until it is 100% removed from the hierarchy.
1281 	 */
1282 	uint32_t	copyexists[8];		/* 00C8-00E7 copy exists bmap */
1283 	char		reserved0140[248];	/* 00E8-01DF */
1284 
1285 	/*
1286 	 * 32 bit CRC array at the end of the first 512 byte sector.
1287 	 *
1288 	 * icrc_sects[7] - First 512-4 bytes of volume header (including all
1289 	 *		   the other icrc's except this one).
1290 	 *
1291 	 * icrc_sects[6] - Sector 1 (512 bytes) of volume header, which is
1292 	 *		   the blockset for the root.
1293 	 *
1294 	 * icrc_sects[5] - Sector 2
1295 	 * icrc_sects[4] - Sector 3
1296 	 * icrc_sects[3] - Sector 4 (the freemap blockset)
1297 	 */
1298 	hammer2_crc32_t	icrc_sects[8];		/* 01E0-01FF */
1299 
1300 	/*
1301 	 * sector #1 - 512 bytes
1302 	 *
1303 	 * The entire sector is used by a blockset.
1304 	 */
1305 	hammer2_blockset_t sroot_blockset;	/* 0200-03FF Superroot dir */
1306 
1307 	/*
1308 	 * sector #2-7
1309 	 */
1310 	char	sector2[512];			/* 0400-05FF reserved */
1311 	char	sector3[512];			/* 0600-07FF reserved */
1312 	hammer2_blockset_t freemap_blockset;	/* 0800-09FF freemap  */
1313 	char	sector5[512];			/* 0A00-0BFF reserved */
1314 	char	sector6[512];			/* 0C00-0DFF reserved */
1315 	char	sector7[512];			/* 0E00-0FFF reserved */
1316 
1317 	/*
1318 	 * sector #8-71	- 32768 bytes
1319 	 *
1320 	 * Contains the configuration for up to 256 copyinfo targets.  These
1321 	 * specify local and remote copies operating as masters or slaves.
1322 	 * copyid's 0 and 255 are reserved (0 indicates an empty slot and 255
1323 	 * indicates the local media).
1324 	 *
1325 	 * Each inode contains a set of up to 8 copyids, either inherited
1326 	 * from its parent or explicitly specified in the inode, which
1327 	 * indexes into this array.
1328 	 */
1329 						/* 1000-8FFF copyinfo config */
1330 	hammer2_volconf_t copyinfo[HAMMER2_COPYID_COUNT];
1331 
1332 	/*
1333 	 * Remaining sections are reserved for future use.
1334 	 */
1335 	char		reserved0400[0x6FFC];	/* 9000-FFFB reserved */
1336 
1337 	/*
1338 	 * icrc on entire volume header
1339 	 */
1340 	hammer2_crc32_t	icrc_volheader;		/* FFFC-FFFF full volume icrc*/
1341 } __packed;
1342 
1343 typedef struct hammer2_volume_data hammer2_volume_data_t;
1344 
1345 /*
1346  * Various parts of the volume header have their own iCRCs.
1347  *
1348  * The first 512 bytes has its own iCRC stored at the end of the 512 bytes
1349  * and not included the icrc calculation.
1350  *
1351  * The second 512 bytes also has its own iCRC but it is stored in the first
1352  * 512 bytes so it covers the entire second 512 bytes.
1353  *
1354  * The whole volume block (64KB) has an iCRC covering all but the last 4 bytes,
1355  * which is where the iCRC for the whole volume is stored.  This is currently
1356  * a catch-all for anything not individually iCRCd.
1357  */
1358 #define HAMMER2_VOL_ICRC_SECT0		7
1359 #define HAMMER2_VOL_ICRC_SECT1		6
1360 
1361 #define HAMMER2_VOLUME_BYTES		65536
1362 
1363 #define HAMMER2_VOLUME_ICRC0_OFF	0
1364 #define HAMMER2_VOLUME_ICRC1_OFF	512
1365 #define HAMMER2_VOLUME_ICRCVH_OFF	0
1366 
1367 #define HAMMER2_VOLUME_ICRC0_SIZE	(512 - 4)
1368 #define HAMMER2_VOLUME_ICRC1_SIZE	(512)
1369 #define HAMMER2_VOLUME_ICRCVH_SIZE	(65536 - 4)
1370 
1371 #define HAMMER2_VOL_VERSION_MIN		1
1372 #define HAMMER2_VOL_VERSION_DEFAULT	1
1373 #define HAMMER2_VOL_VERSION_WIP 	2
1374 
1375 #define HAMMER2_NUM_VOLHDRS		4
1376 
1377 union hammer2_media_data {
1378 	hammer2_volume_data_t	voldata;
1379         hammer2_inode_data_t    ipdata;
1380 	hammer2_blockset_t	blkset;
1381 	hammer2_blockref_t	npdata[HAMMER2_IND_COUNT_MAX];
1382 	hammer2_bmap_data_t	bmdata[HAMMER2_FREEMAP_COUNT];
1383 	char			buf[HAMMER2_PBUFSIZE];
1384 } __packed;
1385 
1386 typedef union hammer2_media_data hammer2_media_data_t;
1387 
1388 #endif /* !_HAMMER2_DISK_H_ */
1389