xref: /dflybsd-src/sys/vfs/hammer2/hammer2_disk.h (revision 2d0700913d3c55b6181d2b703dd69aae2179ce8c)
1 /*
2  * Copyright (c) 2011-2012 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 #ifndef VFS_HAMMER2_DISK_H_
36 #define VFS_HAMMER2_DISK_H_
37 
38 #ifndef _SYS_UUID_H_
39 #include <sys/uuid.h>
40 #endif
41 #ifndef _SYS_DMSG_H_
42 #include <sys/dmsg.h>
43 #endif
44 
45 /*
46  * The structures below represent the on-disk media structures for the HAMMER2
47  * filesystem.  Note that all fields for on-disk structures are naturally
48  * aligned.  The host endian format is typically used - compatibility is
49  * possible if the implementation detects reversed endian and adjusts accesses
50  * accordingly.
51  *
52  * HAMMER2 primarily revolves around the directory topology:  inodes,
53  * directory entries, and block tables.  Block device buffer cache buffers
54  * are always 64KB.  Logical file buffers are typically 16KB.  All data
55  * references utilize 64-bit byte offsets.
56  *
57  * Free block management is handled independently using blocks reserved by
58  * the media topology.
59  */
60 
61 /*
62  * The data at the end of a file or directory may be a fragment in order
63  * to optimize storage efficiency.  The minimum fragment size is 1KB.
64  * Since allocations are in powers of 2 fragments must also be sized in
65  * powers of 2 (1024, 2048, ... 65536).
66  *
67  * For the moment the maximum allocation size is HAMMER2_PBUFSIZE (64K),
68  * which is 2^16.  Larger extents may be supported in the future.  Smaller
69  * fragments might be supported in the future (down to 64 bytes is possible),
70  * but probably will not be.
71  *
72  * A full indirect block use supports 1024 x 64-byte blockrefs in a 64KB
73  * buffer.  Indirect blocks down to 1KB are supported to keep small
74  * directories small.
75  *
76  * A maximally sized file (2^64-1 bytes) requires 5 indirect block levels.
77  * The hammer2_blockset in the volume header or file inode has another 8
78  * entries, giving us 66+3 = 69 bits of address space.  However, some bits
79  * are taken up by (potentially) requests for redundant copies.  HAMMER2
80  * currently supports up to 8 copies, which brings the address space down
81  * to 66 bits and gives us 2 bits of leeway.
82  */
83 #define HAMMER2_MIN_ALLOC	1024	/* minimum allocation size */
84 #define HAMMER2_MIN_RADIX	10	/* minimum allocation size 2^N */
85 #define HAMMER2_MAX_ALLOC	65536	/* maximum allocation size */
86 #define HAMMER2_MAX_RADIX	16	/* maximum allocation size 2^N */
87 #define HAMMER2_KEY_RADIX	64	/* number of bits in key */
88 
89 /*
90  * MINALLOCSIZE		- The minimum allocation size.  This can be smaller
91  *		  	  or larger than the minimum physical IO size.
92  *
93  *			  NOTE: Should not be larger than 1K since inodes
94  *				are 1K.
95  *
96  * MINIOSIZE		- The minimum IO size.  This must be less than
97  *			  or equal to HAMMER2_LBUFSIZE.
98  *
99  * HAMMER2_LBUFSIZE	- Nominal buffer size for I/O rollups.
100  *
101  * HAMMER2_PBUFSIZE	- Topological block size used by files for all
102  *			  blocks except the block straddling EOF.
103  *
104  * HAMMER2_SEGSIZE	- Allocation map segment size, typically 2MB
105  *			  (space represented by a level0 bitmap).
106  */
107 
108 #define HAMMER2_SEGSIZE		(1 << HAMMER2_FREEMAP_LEVEL0_RADIX)
109 
110 #define HAMMER2_PBUFRADIX	16	/* physical buf (1<<16) bytes */
111 #define HAMMER2_PBUFSIZE	65536
112 #define HAMMER2_LBUFRADIX	14	/* logical buf (1<<14) bytes */
113 #define HAMMER2_LBUFSIZE	16384
114 
115 /*
116  * Generally speaking we want to use 16K and 64K I/Os
117  */
118 #define HAMMER2_MINIORADIX	HAMMER2_LBUFRADIX
119 #define HAMMER2_MINIOSIZE	HAMMER2_LBUFSIZE
120 
121 #define HAMMER2_IND_BYTES_MIN	HAMMER2_LBUFSIZE
122 #define HAMMER2_IND_BYTES_MAX	HAMMER2_PBUFSIZE
123 #define HAMMER2_IND_COUNT_MIN	(HAMMER2_IND_BYTES_MIN / \
124 				 sizeof(hammer2_blockref_t))
125 #define HAMMER2_IND_COUNT_MAX	(HAMMER2_IND_BYTES_MAX / \
126 				 sizeof(hammer2_blockref_t))
127 
128 /*
129  * In HAMMER2, arrays of blockrefs are fully set-associative, meaning that
130  * any element can occur at any index and holes can be anywhere.  As a
131  * future optimization we will be able to flag that such arrays are sorted
132  * and thus optimize lookups, but for now we don't.
133  *
134  * Inodes embed either 512 bytes of direct data or an array of 8 blockrefs,
135  * resulting in highly efficient storage for files <= 512 bytes and for files
136  * <= 512KB.  Up to 8 directory entries can be referenced from a directory
137  * without requiring an indirect block.
138  *
139  * Indirect blocks are typically either 4KB (64 blockrefs / ~4MB represented),
140  * or 64KB (1024 blockrefs / ~64MB represented).
141  */
142 #define HAMMER2_SET_COUNT		8	/* direct entries */
143 #define HAMMER2_SET_RADIX		3
144 #define HAMMER2_EMBEDDED_BYTES		512	/* inode blockset/dd size */
145 #define HAMMER2_EMBEDDED_RADIX		9
146 
147 #define HAMMER2_PBUFMASK	(HAMMER2_PBUFSIZE - 1)
148 #define HAMMER2_LBUFMASK	(HAMMER2_LBUFSIZE - 1)
149 #define HAMMER2_SEGMASK		(HAMMER2_SEGSIZE - 1)
150 
151 #define HAMMER2_LBUFMASK64	((hammer2_off_t)HAMMER2_LBUFMASK)
152 #define HAMMER2_PBUFSIZE64	((hammer2_off_t)HAMMER2_PBUFSIZE)
153 #define HAMMER2_PBUFMASK64	((hammer2_off_t)HAMMER2_PBUFMASK)
154 #define HAMMER2_SEGSIZE64	((hammer2_off_t)HAMMER2_SEGSIZE)
155 #define HAMMER2_SEGMASK64	((hammer2_off_t)HAMMER2_SEGMASK)
156 
157 #define HAMMER2_UUID_STRING	"5cbb9ad1-862d-11dc-a94d-01301bb8a9f5"
158 
159 /*
160  * A HAMMER2 filesystem is always sized in multiples of 8MB.
161  *
162  * A 4MB segment is reserved at the beginning of each 2GB zone.  This segment
163  * contains the volume header (or backup volume header), the free block
164  * table, and possibly other information in the future.
165  *
166  * 4MB = 64 x 64K blocks.  Each 4MB segment is broken down as follows:
167  *
168  *	+-----------------------+
169  *      |	Volume Hdr	| block 0	volume header & alternates
170  *	+-----------------------+		(first four zones only)
171  *	|   FreeBlk Section A   | block 1-8
172  *	+-----------------------+
173  *	|   FreeBlk Section B   | block 9-16
174  *	+-----------------------+
175  *	|   FreeBlk Section C   | block 17-24
176  *	+-----------------------+
177  *	|   FreeBlk Section D   | block 25-32
178  *	+-----------------------+
179  *      |			| block 33...63
180  *      |	reserved	|
181  *      |			|
182  *	+-----------------------+
183  *
184  * The first few 2GB zones contain volume headers and volume header backups.
185  * After that the volume header block# is reserved.
186  *
187  * The freemap utilizes blocks #1-32 for now, see the FREEMAP document.
188  * The Free block table has a resolution of 1KB
189  *
190  * WARNING!  ZONE_SEG and VOLUME_ALIGN must be a multiple of 1<<LEVEL0_RADIX
191  *	     (i.e. a multiple of 2MB).  VOLUME_ALIGN must be >= ZONE_SEG.
192  */
193 #define HAMMER2_VOLUME_ALIGN		(8 * 1024 * 1024)
194 #define HAMMER2_VOLUME_ALIGN64		((hammer2_off_t)HAMMER2_VOLUME_ALIGN)
195 #define HAMMER2_VOLUME_ALIGNMASK	(HAMMER2_VOLUME_ALIGN - 1)
196 #define HAMMER2_VOLUME_ALIGNMASK64     ((hammer2_off_t)HAMMER2_VOLUME_ALIGNMASK)
197 
198 #define HAMMER2_NEWFS_ALIGN		(HAMMER2_VOLUME_ALIGN)
199 #define HAMMER2_NEWFS_ALIGN64		((hammer2_off_t)HAMMER2_VOLUME_ALIGN)
200 #define HAMMER2_NEWFS_ALIGNMASK		(HAMMER2_VOLUME_ALIGN - 1)
201 #define HAMMER2_NEWFS_ALIGNMASK64	((hammer2_off_t)HAMMER2_NEWFS_ALIGNMASK)
202 
203 #define HAMMER2_ZONE_BYTES64		(2LLU * 1024 * 1024 * 1024)
204 #define HAMMER2_ZONE_MASK64		(HAMMER2_ZONE_BYTES64 - 1)
205 #define HAMMER2_ZONE_SEG		(4 * 1024 * 1024)
206 #define HAMMER2_ZONE_SEG64		((hammer2_off_t)HAMMER2_ZONE_SEG)
207 #define HAMMER2_ZONE_BLOCKS_SEG		(HAMMER2_ZONE_SEG / HAMMER2_PBUFSIZE)
208 
209 /*
210  * 64 x 64KB blocks are reserved at the base of each 2GB zone.  These blocks
211  * are used to store the volume header or volume header backups, allocation
212  * tree, and other information in the future.
213  *
214  * All specified blocks are not necessarily used in all 2GB zones.  However,
215  * dead areas are reserved for future use and MUST NOT BE USED for other
216  * purposes.
217  *
218  * The freemap is arranged into four groups.  Modifications rotate through
219  * the groups on a block by block basis (so all the blocks are not necessarily
220  * synchronized to the same group).  Because the freemap is flushed
221  * independent of the main filesystem, the freemap only really needs two
222  * groups to operate efficiently.
223  *
224  *
225  *
226  */
227 #define HAMMER2_ZONE_VOLHDR		0	/* volume header or backup */
228 #define HAMMER2_ZONE_FREEMAP_A		1	/* freemap layer group A */
229 #define HAMMER2_ZONE_FREEMAP_B		5	/* freemap layer group B */
230 #define HAMMER2_ZONE_FREEMAP_C		9	/* freemap layer group C */
231 #define HAMMER2_ZONE_FREEMAP_D		13	/* freemap layer group D */
232 
233 						/* relative to FREEMAP_x */
234 #define HAMMER2_ZONEFM_LEVEL1		0	/* 2GB leafmap */
235 #define HAMMER2_ZONEFM_LEVEL2		1	/* 2TB indmap */
236 #define HAMMER2_ZONEFM_LEVEL3		2	/* 2PB indmap */
237 #define HAMMER2_ZONEFM_LEVEL4		3	/* 2EB indmap */
238 /* LEVEL5 is a set of 8 blockrefs in the volume header 16EB */
239 
240 
241 /*
242  * Freemap radii.  Please note that LEVEL 1 blockref array entries
243  * point to 256-byte sections of the bitmap representing 2MB of storage.
244  * Even though the chain structures represent only 256 bytes, they are
245  * mapped using larger 16K or 64K buffer cache buffers.
246  */
247 #define HAMMER2_FREEMAP_LEVEL5_RADIX	64	/* 16EB */
248 #define HAMMER2_FREEMAP_LEVEL4_RADIX	61	/* 2EB */
249 #define HAMMER2_FREEMAP_LEVEL3_RADIX	51	/* 2PB */
250 #define HAMMER2_FREEMAP_LEVEL2_RADIX	41	/* 2TB */
251 #define HAMMER2_FREEMAP_LEVEL1_RADIX	31	/* 2GB */
252 #define HAMMER2_FREEMAP_LEVEL0_RADIX	21	/* 2MB (entry in l-1 leaf) */
253 
254 #define HAMMER2_FREEMAP_LEVELN_PSIZE	65536	/* physical bytes */
255 
256 #define HAMMER2_FREEMAP_COUNT		(int)(HAMMER2_FREEMAP_LEVELN_PSIZE / \
257 					 sizeof(hammer2_bmap_data_t))
258 #define HAMMER2_FREEMAP_BLOCK_RADIX	14
259 #define HAMMER2_FREEMAP_BLOCK_SIZE	(1 << HAMMER2_FREEMAP_BLOCK_RADIX)
260 #define HAMMER2_FREEMAP_BLOCK_MASK	(HAMMER2_FREEMAP_BLOCK_SIZE - 1)
261 
262 /*
263  * Two linear areas can be reserved after the initial 2MB segment in the base
264  * zone (the one starting at offset 0).  These areas are NOT managed by the
265  * block allocator and do not fall under HAMMER2 crc checking rules based
266  * at the volume header (but can be self-CRCd internally, depending).
267  */
268 #define HAMMER2_BOOT_MIN_BYTES		HAMMER2_VOLUME_ALIGN
269 #define HAMMER2_BOOT_NOM_BYTES		(64*1024*1024)
270 #define HAMMER2_BOOT_MAX_BYTES		(256*1024*1024)
271 
272 #define HAMMER2_REDO_MIN_BYTES		HAMMER2_VOLUME_ALIGN
273 #define HAMMER2_REDO_NOM_BYTES		(256*1024*1024)
274 #define HAMMER2_REDO_MAX_BYTES		(1024*1024*1024)
275 
276 /*
277  * Most HAMMER2 types are implemented as unsigned 64-bit integers.
278  * Transaction ids are monotonic.
279  *
280  * We utilize 32-bit iSCSI CRCs.
281  */
282 typedef uint64_t hammer2_tid_t;
283 typedef uint64_t hammer2_off_t;
284 typedef uint64_t hammer2_key_t;
285 typedef uint32_t hammer2_crc32_t;
286 
287 /*
288  * Miscellanious ranges (all are unsigned).
289  */
290 #define HAMMER2_MIN_TID		1ULL
291 #define HAMMER2_MAX_TID		0xFFFFFFFFFFFFFFFFULL
292 #define HAMMER2_MIN_KEY		0ULL
293 #define HAMMER2_MAX_KEY		0xFFFFFFFFFFFFFFFFULL
294 #define HAMMER2_MIN_OFFSET	0ULL
295 #define HAMMER2_MAX_OFFSET	0xFFFFFFFFFFFFFFFFULL
296 
297 /*
298  * HAMMER2 data offset special cases and masking.
299  *
300  * All HAMMER2 data offsets have to be broken down into a 64K buffer base
301  * offset (HAMMER2_OFF_MASK_HI) and a 64K buffer index (HAMMER2_OFF_MASK_LO).
302  *
303  * Indexes into physical buffers are always 64-byte aligned.  The low 6 bits
304  * of the data offset field specifies how large the data chunk being pointed
305  * to as a power of 2.  The theoretical minimum radix is thus 6 (The space
306  * needed in the low bits of the data offset field).  However, the practical
307  * minimum allocation chunk size is 1KB (a radix of 10), so HAMMER2 sets
308  * HAMMER2_MIN_RADIX to 10.  The maximum radix is currently 16 (64KB), but
309  * we fully intend to support larger extents in the future.
310  */
311 #define HAMMER2_OFF_BAD		((hammer2_off_t)-1)
312 #define HAMMER2_OFF_MASK	0xFFFFFFFFFFFFFFC0ULL
313 #define HAMMER2_OFF_MASK_LO	(HAMMER2_OFF_MASK & HAMMER2_PBUFMASK64)
314 #define HAMMER2_OFF_MASK_HI	(~HAMMER2_PBUFMASK64)
315 #define HAMMER2_OFF_MASK_RADIX	0x000000000000003FULL
316 #define HAMMER2_MAX_COPIES	6
317 
318 /*
319  * HAMMER2 directory support and pre-defined keys
320  */
321 #define HAMMER2_DIRHASH_VISIBLE	0x8000000000000000ULL
322 #define HAMMER2_DIRHASH_USERMSK	0x7FFFFFFFFFFFFFFFULL
323 #define HAMMER2_DIRHASH_LOMASK	0x0000000000007FFFULL
324 #define HAMMER2_DIRHASH_HIMASK	0xFFFFFFFFFFFF0000ULL
325 #define HAMMER2_DIRHASH_FORCED	0x0000000000008000ULL	/* bit forced on */
326 
327 #define HAMMER2_SROOT_KEY	0x0000000000000000ULL	/* volume to sroot */
328 
329 /*
330  * The media block reference structure.  This forms the core of the HAMMER2
331  * media topology recursion.  This 64-byte data structure is embedded in the
332  * volume header, in inodes (which are also directory entries), and in
333  * indirect blocks.
334  *
335  * A blockref references a single media item, which typically can be a
336  * directory entry (aka inode), indirect block, or data block.
337  *
338  * The primary feature a blockref represents is the ability to validate
339  * the entire tree underneath it via its check code.  Any modification to
340  * anything propagates up the blockref tree all the way to the root, replacing
341  * the related blocks.  Propagations can shortcut to the volume root to
342  * implement the 'fast syncing' feature but this only delays the eventual
343  * propagation.
344  *
345  * The check code can be a simple 32-bit iscsi code, a 64-bit crc,
346  * or as complex as a 192 bit cryptographic hash.  192 bits is the maximum
347  * supported check code size, which is not sufficient for unverified dedup
348  * UNLESS one doesn't mind once-in-a-blue-moon data corruption (such as when
349  * farming web data).  HAMMER2 has an unverified dedup feature for just this
350  * purpose.
351  *
352  * --
353  *
354  * NOTE: The range of keys represented by the blockref is (key) to
355  *	 ((key) + (1LL << keybits) - 1).  HAMMER2 usually populates
356  *	 blocks bottom-up, inserting a new root when radix expansion
357  *	 is required.
358  */
359 struct hammer2_blockref {		/* MUST BE EXACTLY 64 BYTES */
360 	uint8_t		type;		/* type of underlying item */
361 	uint8_t		methods;	/* check method & compression method */
362 	uint8_t		copyid;		/* specify which copy this is */
363 	uint8_t		keybits;	/* #of keybits masked off 0=leaf */
364 	uint8_t		vradix;		/* virtual data/meta-data size */
365 	uint8_t		flags;		/* blockref flags */
366 	uint8_t		reserved06;
367 	uint8_t		reserved07;
368 	hammer2_key_t	key;		/* key specification */
369 	hammer2_tid_t	mirror_tid;	/* propagate for mirror scan */
370 	hammer2_tid_t	modify_tid;	/* modifications sans propagation */
371 	hammer2_off_t	data_off;	/* low 6 bits is phys size (radix)*/
372 	union {				/* check info */
373 		char	buf[24];
374 		struct {
375 			uint32_t value;
376 			uint32_t unused[5];
377 		} iscsi32;
378 		struct {
379 			uint64_t value;
380 			uint64_t unused[2];
381 		} crc64;
382 		struct {
383 			char data[24];
384 		} sha192;
385 
386 		/*
387 		 * Freemap hints are embedded in addition to the icrc32.
388 		 *
389 		 * bigmask - Radixes available for allocation (0-31).
390 		 *	     Heuristical (may be permissive but not
391 		 *	     restrictive).  Typically only radix values
392 		 *	     10-16 are used (i.e. (1<<10) through (1<<16)).
393 		 *
394 		 * avail   - Total available space remaining, in bytes
395 		 */
396 		struct {
397 			uint32_t icrc32;
398 			uint32_t bigmask;	/* available radixes */
399 			uint64_t avail;		/* total available bytes */
400 			uint64_t unused;	/* unused must be 0 */
401 		} freemap;
402 	} check;
403 };
404 
405 typedef struct hammer2_blockref hammer2_blockref_t;
406 
407 #if 0
408 #define HAMMER2_BREF_SYNC1		0x01	/* modification synchronized */
409 #define HAMMER2_BREF_SYNC2		0x02	/* modification committed */
410 #define HAMMER2_BREF_DESYNCCHLD		0x04	/* desynchronize children */
411 #define HAMMER2_BREF_DELETED		0x80	/* indicates a deletion */
412 #endif
413 
414 #define HAMMER2_BLOCKREF_BYTES		64	/* blockref struct in bytes */
415 
416 #define HAMMER2_BREF_TYPE_EMPTY		0
417 #define HAMMER2_BREF_TYPE_INODE		1
418 #define HAMMER2_BREF_TYPE_INDIRECT	2
419 #define HAMMER2_BREF_TYPE_DATA		3
420 #define HAMMER2_BREF_TYPE_UNUSED04	4
421 #define HAMMER2_BREF_TYPE_FREEMAP_NODE	5
422 #define HAMMER2_BREF_TYPE_FREEMAP_LEAF	6
423 #define HAMMER2_BREF_TYPE_FREEMAP	254	/* pseudo-type */
424 #define HAMMER2_BREF_TYPE_VOLUME	255	/* pseudo-type */
425 
426 #define HAMMER2_ENC_CHECK(n)		((n) << 4)
427 #define HAMMER2_DEC_CHECK(n)		(((n) >> 4) & 15)
428 
429 #define HAMMER2_CHECK_NONE		0
430 #define HAMMER2_CHECK_ISCSI32		1
431 #define HAMMER2_CHECK_CRC64		2
432 #define HAMMER2_CHECK_SHA192		3
433 #define HAMMER2_CHECK_FREEMAP		4
434 
435 #define HAMMER2_ENC_COMP(n)		(n)
436 #define HAMMER2_DEC_COMP(n)		((n) & 15)
437 
438 #define HAMMER2_COMP_NONE		0
439 #define HAMMER2_COMP_AUTOZERO		1
440 
441 
442 /*
443  * HAMMER2 block references are collected into sets of 8 blockrefs.  These
444  * sets are fully associative, meaning the elements making up a set are
445  * not sorted in any way and may contain duplicate entries, holes, or
446  * entries which shortcut multiple levels of indirection.  Sets are used
447  * in various ways:
448  *
449  * (1) When redundancy is desired a set may contain several duplicate
450  *     entries pointing to different copies of the same data.  Up to 8 copies
451  *     are supported but the set structure becomes a bit inefficient once
452  *     you go over 4.
453  *
454  * (2) The blockrefs in a set can shortcut multiple levels of indirections
455  *     within the bounds imposed by the parent of set.
456  *
457  * When a set fills up another level of indirection is inserted, moving
458  * some or all of the set's contents into indirect blocks placed under the
459  * set.  This is a top-down approach in that indirect blocks are not created
460  * until the set actually becomes full (that is, the entries in the set can
461  * shortcut the indirect blocks when the set is not full).  Depending on how
462  * things are filled multiple indirect blocks will eventually be created.
463  *
464  * Indirect blocks are typically 4KB (64 entres) or 64KB (1024 entries) and
465  * are also treated as fully set-associative.
466  */
467 struct hammer2_blockset {
468 	hammer2_blockref_t	blockref[HAMMER2_SET_COUNT];
469 };
470 
471 typedef struct hammer2_blockset hammer2_blockset_t;
472 
473 /*
474  * Catch programmer snafus
475  */
476 #if (1 << HAMMER2_SET_RADIX) != HAMMER2_SET_COUNT
477 #error "hammer2 direct radix is incorrect"
478 #endif
479 #if (1 << HAMMER2_PBUFRADIX) != HAMMER2_PBUFSIZE
480 #error "HAMMER2_PBUFRADIX and HAMMER2_PBUFSIZE are inconsistent"
481 #endif
482 #if (1 << HAMMER2_MIN_RADIX) != HAMMER2_MIN_ALLOC
483 #error "HAMMER2_MIN_RADIX and HAMMER2_MIN_ALLOC are inconsistent"
484 #endif
485 
486 /*
487  * hammer2_bmap_data - A freemap entry in the LEVEL1 block.
488  *
489  * Each 64-byte entry contains the bitmap and meta-data required to manage
490  * a LEVEL0 (2MB) block of storage.  The storage is managed in 128 x 16KB
491  * chunks.  Smaller allocation granularity is supported via a linear iterator
492  * and/or must otherwise be tracked in ram.
493  *
494  * (data structure must be 64 bytes exactly)
495  *
496  * linear  - A BYTE linear allocation offset.  May contain values between
497  *	     0 and 2MB.  Any value which is 16KB-aligned is effective neutral
498  *	     (forces the bitmap to be checked), whereas intermediate values
499  *	     allow iterative allocations from a bitmap that is already marked
500  *	     allocated.
501  *
502  *	     Please note that file data granularity may be limited by
503  *	     other issues such as buffer cache direct-mapping and the
504  *	     desire to support sector sizes up to 16KB (so H2 only issues
505  *	     I/O's in multiples of 16KB anyway).
506  *
507  * radix   - Once assigned, radix for clustering.  Cleared to 0 only if
508  *	     the entire leaf becomes free (related device buffer cache buffers
509  *	     must also be destroyed to avoid later overlap assertions).  All
510  *	     chain's within this 2MB segment must match the clustering radix.
511  *
512  *	     Device I/O size may be further adjusted to the minimum (16KB),
513  *	     even if radix is smaller.   This forms the I/O clustering radix.
514  *
515  *	     Value will typically be 10-16 (1KB to 64KB).  Smaller values may
516  *	     be allowed in the future (probably unnecessary to add that
517  *	     complication though).
518  *
519  * bitmap  - Two bits per 16KB allocation block arranged in arrays of
520  *	     32-bit elements, 128x2 bits representing ~2MB worth of media
521  *	     storage.  Bit patterns are as follows:
522  *
523  *	     00	Unallocated
524  *	     01 Armed for bulk free scan
525  *	     10 Possibly free
526  *           11 Allocated
527  */
528 struct hammer2_bmap_data {
529 	int32_t linear;		/* 00 linear sub-granular allocation offset */
530 	uint8_t reserved04;	/* 04 */
531 	uint8_t radix;		/* 05 cluster I/O 0, LBUFRADIX, PBUFRADIX */
532 	uint8_t reserved06;	/* 06 */
533 	uint8_t reserved07;	/* 07 */
534 	uint32_t reserved08;	/* 08 */
535 	uint32_t reserved0C;	/* 0C */
536 	uint32_t reserved10;	/* 10 */
537 	uint32_t reserved14;	/* 14 */
538 	uint32_t reserved18;	/* 18 */
539 	uint32_t avail;		/* 1C */
540 	uint32_t bitmap[8];	/* 20-3F 256 bits manages 2MB/16KB/2-bits */
541 };
542 
543 typedef struct hammer2_bmap_data hammer2_bmap_data_t;
544 
545 /*
546  * In HAMMER2 inodes ARE directory entries, with a special exception for
547  * hardlinks.  The inode number is stored in the inode rather than being
548  * based on the location of the inode (since the location moves every time
549  * the inode or anything underneath the inode is modified).
550  *
551  * The inode is 1024 bytes, made up of 256 bytes of meta-data, 256 bytes
552  * for the filename, and 512 bytes worth of direct file data OR an embedded
553  * blockset.
554  *
555  * Directories represent one inode per blockref.  Inodes are not laid out
556  * as a file but instead are represented by the related blockrefs.  The
557  * blockrefs, in turn, are indexed by the 64-bit directory hash key.  Remember
558  * that blocksets are fully associative, so a certain degree efficiency is
559  * achieved just from that.
560  *
561  * Up to 512 bytes of direct data can be embedded in an inode, and since
562  * inodes are essentially directory entries this also means that small data
563  * files end up simply being laid out linearly in the directory, resulting
564  * in fewer seeks and highly optimal access.
565  *
566  * The compression mode can be changed at any time in the inode and is
567  * recorded on a blockref-by-blockref basis.
568  *
569  * Hardlinks are supported via the inode map.  Essentially the way a hardlink
570  * works is that all individual directory entries representing the same file
571  * are special cased and specify the same inode number.  The actual file
572  * is placed in the nearest parent directory that is parent to all instances
573  * of the hardlink.  If all hardlinks to a file are in the same directory
574  * the actual file will also be placed in that directory.  This file uses
575  * the inode number as the directory entry key and is invisible to normal
576  * directory scans.  Real directory entry keys are differentiated from the
577  * inode number key via bit 63.  Access to the hardlink silently looks up
578  * the real file and forwards all operations to that file.  Removal of the
579  * last hardlink also removes the real file.
580  *
581  * (attr_tid) is only updated when the inode's specific attributes or regular
582  * file size has changed, and affects path lookups and stat.  (attr_tid)
583  * represents a special cache coherency lock under the inode.  The inode
584  * blockref's modify_tid will always cover it.
585  *
586  * (dirent_tid) is only updated when an entry under a directory inode has
587  * been created, deleted, renamed, or had its attributes change, and affects
588  * directory lookups and scans.  (dirent_tid) represents another special cache
589  * coherency lock under the inode.  The inode blockref's modify_tid will
590  * always cover it.
591  */
592 #define HAMMER2_INODE_BYTES		1024	/* (asserted by code) */
593 #define HAMMER2_INODE_MAXNAME		256	/* maximum name in bytes */
594 #define HAMMER2_INODE_VERSION_ONE	1
595 
596 struct hammer2_inode_data {
597 	uint16_t	version;	/* 0000 inode data version */
598 	uint16_t	reserved02;	/* 0002 */
599 
600 	/*
601 	 * core inode attributes, inode type, misc flags
602 	 */
603 	uint32_t	uflags;		/* 0004 chflags */
604 	uint32_t	rmajor;		/* 0008 available for device nodes */
605 	uint32_t	rminor;		/* 000C available for device nodes */
606 	uint64_t	ctime;		/* 0010 inode change time */
607 	uint64_t	mtime;		/* 0018 modified time */
608 	uint64_t	atime;		/* 0020 access time (unsupported) */
609 	uint64_t	btime;		/* 0028 birth time */
610 	uuid_t		uid;		/* 0030 uid / degenerate unix uid */
611 	uuid_t		gid;		/* 0040 gid / degenerate unix gid */
612 
613 	uint8_t		type;		/* 0050 object type */
614 	uint8_t		op_flags;	/* 0051 operational flags */
615 	uint16_t	cap_flags;	/* 0052 capability flags */
616 	uint32_t	mode;		/* 0054 unix modes (typ low 16 bits) */
617 
618 	/*
619 	 * inode size, identification, localized recursive configuration
620 	 * for compression and backup copies.
621 	 */
622 	hammer2_tid_t	inum;		/* 0058 inode number */
623 	hammer2_off_t	size;		/* 0060 size of file */
624 	uint64_t	nlinks;		/* 0068 hard links (typ only dirs) */
625 	hammer2_tid_t	iparent;	/* 0070 parent inum (recovery only) */
626 	hammer2_key_t	name_key;	/* 0078 full filename key */
627 	uint16_t	name_len;	/* 0080 filename length */
628 	uint8_t		ncopies;	/* 0082 ncopies to local media */
629 	uint8_t		comp_algo;	/* 0083 compression request & algo */
630 
631 	/*
632 	 * These fields are currently only applicable to PFSROOTs.
633 	 *
634 	 * NOTE: We can't use {volume_data->fsid, pfs_clid} to uniquely
635 	 *	 identify an instance of a PFS in the cluster because
636 	 *	 a mount may contain more than one copy of the PFS as
637 	 *	 a separate node.  {pfs_clid, pfs_fsid} must be used for
638 	 *	 registration in the cluster.
639 	 */
640 	uint8_t		target_type;	/* 0084 hardlink target type */
641 	uint8_t		reserved85;	/* 0085 */
642 	uint8_t		reserved86;	/* 0086 */
643 	uint8_t		pfs_type;	/* 0087 (if PFSROOT) node type */
644 	uint64_t	pfs_inum;	/* 0088 (if PFSROOT) inum allocator */
645 	uuid_t		pfs_clid;	/* 0090 (if PFSROOT) cluster uuid */
646 	uuid_t		pfs_fsid;	/* 00A0 (if PFSROOT) unique uuid */
647 
648 	/*
649 	 * Quotas and cumulative sub-tree counters.
650 	 */
651 	hammer2_key_t	data_quota;	/* 00B0 subtree quota in bytes */
652 	hammer2_key_t	data_count;	/* 00B8 subtree byte count */
653 	hammer2_key_t	inode_quota;	/* 00C0 subtree quota inode count */
654 	hammer2_key_t	inode_count;	/* 00C8 subtree inode count */
655 	hammer2_tid_t	attr_tid;	/* 00D0 attributes changed */
656 	hammer2_tid_t	dirent_tid;	/* 00D8 directory/attr changed */
657 
658 	/*
659 	 * Tracks (possibly degenerate) free areas covering all sub-tree
660 	 * allocations under inode, not counting the inode itself.
661 	 * 0/0 indicates empty entry.  fully set-associative.
662 	 */
663 	hammer2_off_t	freezones[4];	/* 00E0/E8/F0/F8 base|radix */
664 
665 	unsigned char	filename[HAMMER2_INODE_MAXNAME];
666 					/* 0100-01FF (256 char, unterminated) */
667 	union {				/* 0200-03FF (64x8 = 512 bytes) */
668 		struct hammer2_blockset blockset;
669 		char data[HAMMER2_EMBEDDED_BYTES];
670 	} u;
671 };
672 
673 typedef struct hammer2_inode_data hammer2_inode_data_t;
674 
675 #define HAMMER2_OPFLAG_DIRECTDATA	0x01
676 #define HAMMER2_OPFLAG_PFSROOT		0x02
677 #define HAMMER2_OPFLAG_COPYIDS		0x04	/* copyids override parent */
678 
679 #define HAMMER2_OBJTYPE_UNKNOWN		0
680 #define HAMMER2_OBJTYPE_DIRECTORY	1
681 #define HAMMER2_OBJTYPE_REGFILE		2
682 #define HAMMER2_OBJTYPE_FIFO		4
683 #define HAMMER2_OBJTYPE_CDEV		5
684 #define HAMMER2_OBJTYPE_BDEV		6
685 #define HAMMER2_OBJTYPE_SOFTLINK	7
686 #define HAMMER2_OBJTYPE_HARDLINK	8	/* dummy entry for hardlink */
687 #define HAMMER2_OBJTYPE_SOCKET		9
688 #define HAMMER2_OBJTYPE_WHITEOUT	10
689 
690 #define HAMMER2_COPYID_NONE		0
691 #define HAMMER2_COPYID_LOCAL		((uint8_t)-1)
692 
693 /*
694  * PEER types identify connections and help cluster controller filter
695  * out unwanted SPANs.
696  */
697 #define HAMMER2_PEER_NONE		DMSG_PEER_NONE
698 #define HAMMER2_PEER_CLUSTER		DMSG_PEER_CLUSTER
699 #define HAMMER2_PEER_BLOCK		DMSG_PEER_BLOCK
700 #define HAMMER2_PEER_HAMMER2		DMSG_PEER_HAMMER2
701 
702 #define HAMMER2_COPYID_COUNT		DMSG_COPYID_COUNT
703 
704 /*
705  * PFS types identify a PFS on media and in LNK_SPAN messages.
706  */
707 #define HAMMER2_PFSTYPE_NONE		DMSG_PFSTYPE_NONE
708 #define HAMMER2_PFSTYPE_ADMIN		DMSG_PFSTYPE_ADMIN
709 #define HAMMER2_PFSTYPE_CLIENT		DMSG_PFSTYPE_CLIENT
710 #define HAMMER2_PFSTYPE_CACHE		DMSG_PFSTYPE_CACHE
711 #define HAMMER2_PFSTYPE_COPY		DMSG_PFSTYPE_COPY
712 #define HAMMER2_PFSTYPE_SLAVE		DMSG_PFSTYPE_SLAVE
713 #define HAMMER2_PFSTYPE_SOFT_SLAVE	DMSG_PFSTYPE_SOFT_SLAVE
714 #define HAMMER2_PFSTYPE_SOFT_MASTER	DMSG_PFSTYPE_SOFT_MASTER
715 #define HAMMER2_PFSTYPE_MASTER		DMSG_PFSTYPE_MASTER
716 #define HAMMER2_PFSTYPE_SNAPSHOT	DMSG_PFSTYPE_SNAPSHOT
717 #define HAMMER2_PFSTYPE_MAX		DMSG_PFSTYPE_MAX
718 
719 /*
720  *				Allocation Table
721  *
722  */
723 
724 
725 /*
726  * Flags (8 bits) - blockref, for freemap only
727  *
728  * Note that the minimum chunk size is 1KB so we could theoretically have
729  * 10 bits here, but we might have some future extension that allows a
730  * chunk size down to 256 bytes and if so we will need bits 8 and 9.
731  */
732 #define HAMMER2_AVF_SELMASK		0x03	/* select group */
733 #define HAMMER2_AVF_ALL_ALLOC		0x04	/* indicate all allocated */
734 #define HAMMER2_AVF_ALL_FREE		0x08	/* indicate all free */
735 #define HAMMER2_AVF_RESERVED10		0x10
736 #define HAMMER2_AVF_RESERVED20		0x20
737 #define HAMMER2_AVF_RESERVED40		0x40
738 #define HAMMER2_AVF_RESERVED80		0x80
739 #define HAMMER2_AVF_AVMASK32		((uint32_t)0xFFFFFF00LU)
740 #define HAMMER2_AVF_AVMASK64		((uint64_t)0xFFFFFFFFFFFFFF00LLU)
741 
742 #define HAMMER2_AV_SELECT_A		0x00
743 #define HAMMER2_AV_SELECT_B		0x01
744 #define HAMMER2_AV_SELECT_C		0x02
745 #define HAMMER2_AV_SELECT_D		0x03
746 
747 /*
748  * The volume header eats a 64K block.  There is currently an issue where
749  * we want to try to fit all nominal filesystem updates in a 512-byte section
750  * but it may be a lost cause due to the need for a blockset.
751  *
752  * All information is stored in host byte order.  The volume header's magic
753  * number may be checked to determine the byte order.  If you wish to mount
754  * between machines w/ different endian modes you'll need filesystem code
755  * which acts on the media data consistently (either all one way or all the
756  * other).  Our code currently does not do that.
757  *
758  * A read-write mount may have to recover missing allocations by doing an
759  * incremental mirror scan looking for modifications made after alloc_tid.
760  * If alloc_tid == last_tid then no recovery operation is needed.  Recovery
761  * operations are usually very, very fast.
762  *
763  * Read-only mounts do not need to do any recovery, access to the filesystem
764  * topology is always consistent after a crash (is always consistent, period).
765  * However, there may be shortcutted blockref updates present from deep in
766  * the tree which are stored in the volumeh eader and must be tracked on
767  * the fly.
768  *
769  * NOTE: The copyinfo[] array contains the configuration for both the
770  *	 cluster connections and any local media copies.  The volume
771  *	 header will be replicated for each local media copy.
772  *
773  *	 The mount command may specify multiple medias or just one and
774  *	 allow HAMMER2 to pick up the others when it checks the copyinfo[]
775  *	 array on mount.
776  *
777  * NOTE: root_blockref points to the super-root directory, not the root
778  *	 directory.  The root directory will be a subdirectory under the
779  *	 super-root.
780  *
781  *	 The super-root directory contains all root directories and all
782  *	 snapshots (readonly or writable).  It is possible to do a
783  *	 null-mount of the super-root using special path constructions
784  *	 relative to your mounted root.
785  *
786  * NOTE: HAMMER2 allows any subdirectory tree to be managed as if it were
787  *	 a PFS, including mirroring and storage quota operations, and this is
788  *	 prefered over creating discrete PFSs in the super-root.  Instead
789  *	 the super-root is most typically used to create writable snapshots,
790  *	 alternative roots, and so forth.  The super-root is also used by
791  *	 the automatic snapshotting mechanism.
792  */
793 #define HAMMER2_VOLUME_ID_HBO	0x48414d3205172011LLU
794 #define HAMMER2_VOLUME_ID_ABO	0x11201705324d4148LLU
795 
796 struct hammer2_volume_data {
797 	/*
798 	 * sector #0 - 512 bytes
799 	 */
800 	uint64_t	magic;			/* 0000 Signature */
801 	hammer2_off_t	boot_beg;		/* 0008 Boot area (future) */
802 	hammer2_off_t	boot_end;		/* 0010 (size = end - beg) */
803 	hammer2_off_t	aux_beg;		/* 0018 Aux area (future) */
804 	hammer2_off_t	aux_end;		/* 0020 (size = end - beg) */
805 	hammer2_off_t	volu_size;		/* 0028 Volume size, bytes */
806 
807 	uint32_t	version;		/* 0030 */
808 	uint32_t	flags;			/* 0034 */
809 	uint8_t		copyid;			/* 0038 copyid of phys vol */
810 	uint8_t		freemap_version;	/* 0039 freemap algorithm */
811 	uint8_t		peer_type;		/* 003A HAMMER2_PEER_xxx */
812 	uint8_t		reserved003B;		/* 003B */
813 	uint32_t	reserved003C;		/* 003C */
814 
815 	uuid_t		fsid;			/* 0040 */
816 	uuid_t		fstype;			/* 0050 */
817 
818 	/*
819 	 * allocator_size is precalculated at newfs time and does not include
820 	 * reserved blocks, boot, or redo areas.
821 	 *
822 	 * Initial non-reserved-area allocations do not use the freemap
823 	 * but instead adjust alloc_iterator.  Dynamic allocations take
824 	 * over starting at (allocator_beg).  This makes newfs_hammer2's
825 	 * job a lot easier and can also serve as a testing jig.
826 	 */
827 	hammer2_off_t	allocator_size;		/* 0060 Total data space */
828 	hammer2_off_t   allocator_free;		/* 0068	Free space */
829 	hammer2_off_t	allocator_beg;		/* 0070 Initial allocations */
830 	hammer2_tid_t	mirror_tid;		/* 0078 best committed tid */
831 	hammer2_tid_t	alloc_tid;		/* 0080 Alloctable modify tid */
832 	hammer2_blockref_t reserved0088;	/* 0088-00C7 */
833 
834 	/*
835 	 * Copyids are allocated dynamically from the copyexists bitmap.
836 	 * An id from the active copies set (up to 8, see copyinfo later on)
837 	 * may still exist after the copy set has been removed from the
838 	 * volume header and its bit will remain active in the bitmap and
839 	 * cannot be reused until it is 100% removed from the hierarchy.
840 	 */
841 	uint32_t	copyexists[8];		/* 00C8-00E7 copy exists bmap */
842 	char		reserved0140[248];	/* 00E8-01DF */
843 
844 	/*
845 	 * 32 bit CRC array at the end of the first 512 byte sector.
846 	 *
847 	 * icrc_sects[7] - First 512-4 bytes of volume header (including all
848 	 *		   the other icrc's except this one).
849 	 *
850 	 * icrc_sects[6] - Sector 1 (512 bytes) of volume header, which is
851 	 *		   the blockset for the root.
852 	 *
853 	 * icrc_sects[5] - Sector 2
854 	 * icrc_sects[4] - Sector 3
855 	 * icrc_sects[3] - Sector 4 (the freemap blockset)
856 	 */
857 	hammer2_crc32_t	icrc_sects[8];		/* 01E0-01FF */
858 
859 	/*
860 	 * sector #1 - 512 bytes
861 	 *
862 	 * The entire sector is used by a blockset.
863 	 */
864 	hammer2_blockset_t sroot_blockset;	/* 0200-03FF Superroot dir */
865 
866 	/*
867 	 * sector #2-7
868 	 */
869 	char	sector2[512];			/* 0400-05FF reserved */
870 	char	sector3[512];			/* 0600-07FF reserved */
871 	hammer2_blockset_t freemap_blockset;	/* 0800-09FF freemap  */
872 	char	sector5[512];			/* 0A00-0BFF reserved */
873 	char	sector6[512];			/* 0C00-0DFF reserved */
874 	char	sector7[512];			/* 0E00-0FFF reserved */
875 
876 	/*
877 	 * sector #8-71	- 32768 bytes
878 	 *
879 	 * Contains the configuration for up to 256 copyinfo targets.  These
880 	 * specify local and remote copies operating as masters or slaves.
881 	 * copyid's 0 and 255 are reserved (0 indicates an empty slot and 255
882 	 * indicates the local media).
883 	 *
884 	 * Each inode contains a set of up to 8 copyids, either inherited
885 	 * from its parent or explicitly specified in the inode, which
886 	 * indexes into this array.
887 	 */
888 						/* 1000-8FFF copyinfo config */
889 	dmsg_vol_data_t	copyinfo[HAMMER2_COPYID_COUNT];
890 
891 	/*
892 	 * Remaining sections are reserved for future use.
893 	 */
894 	char		reserved0400[0x6FFC];	/* 9000-FFFB reserved */
895 
896 	/*
897 	 * icrc on entire volume header
898 	 */
899 	hammer2_crc32_t	icrc_volheader;		/* FFFC-FFFF full volume icrc*/
900 };
901 
902 typedef struct hammer2_volume_data hammer2_volume_data_t;
903 
904 /*
905  * Various parts of the volume header have their own iCRCs.
906  *
907  * The first 512 bytes has its own iCRC stored at the end of the 512 bytes
908  * and not included the icrc calculation.
909  *
910  * The second 512 bytes also has its own iCRC but it is stored in the first
911  * 512 bytes so it covers the entire second 512 bytes.
912  *
913  * The whole volume block (64KB) has an iCRC covering all but the last 4 bytes,
914  * which is where the iCRC for the whole volume is stored.  This is currently
915  * a catch-all for anything not individually iCRCd.
916  */
917 #define HAMMER2_VOL_ICRC_SECT0		7
918 #define HAMMER2_VOL_ICRC_SECT1		6
919 
920 #define HAMMER2_VOLUME_BYTES		65536
921 
922 #define HAMMER2_VOLUME_ICRC0_OFF	0
923 #define HAMMER2_VOLUME_ICRC1_OFF	512
924 #define HAMMER2_VOLUME_ICRCVH_OFF	0
925 
926 #define HAMMER2_VOLUME_ICRC0_SIZE	(512 - 4)
927 #define HAMMER2_VOLUME_ICRC1_SIZE	(512)
928 #define HAMMER2_VOLUME_ICRCVH_SIZE	(65536 - 4)
929 
930 #define HAMMER2_VOL_VERSION_MIN		1
931 #define HAMMER2_VOL_VERSION_DEFAULT	1
932 #define HAMMER2_VOL_VERSION_WIP 	2
933 
934 #define HAMMER2_NUM_VOLHDRS		4
935 
936 union hammer2_media_data {
937 	hammer2_volume_data_t	voldata;
938         hammer2_inode_data_t    ipdata;
939 	hammer2_blockref_t	npdata[HAMMER2_IND_COUNT_MAX];
940 	hammer2_bmap_data_t	bmdata[HAMMER2_FREEMAP_COUNT];
941 	char			buf[HAMMER2_PBUFSIZE];
942 };
943 
944 typedef union hammer2_media_data hammer2_media_data_t;
945 
946 #endif
947