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