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