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