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