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