/* * Copyright (c) 2011-2012 The DragonFly Project. All rights reserved. * * This code is derived from software contributed to The DragonFly Project * by Matthew Dillon * by Venkatesh Srinivas * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * 3. Neither the name of The DragonFly Project nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific, prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #ifndef VFS_HAMMER2_DISK_H_ #define VFS_HAMMER2_DISK_H_ #ifndef _SYS_UUID_H_ #include #endif #ifndef _SYS_DMSG_H_ #include #endif /* * The structures below represent the on-disk media structures for the HAMMER2 * filesystem. Note that all fields for on-disk structures are naturally * aligned. The host endian format is typically used - compatibility is * possible if the implementation detects reversed endian and adjusts accesses * accordingly. * * HAMMER2 primarily revolves around the directory topology: inodes, * directory entries, and block tables. Block device buffer cache buffers * are always 64KB. Logical file buffers are typically 16KB. All data * references utilize 64-bit byte offsets. * * Free block management is handled independently using blocks reserved by * the media topology. */ /* * The data at the end of a file or directory may be a fragment in order * to optimize storage efficiency. The minimum fragment size is 1KB. * Since allocations are in powers of 2 fragments must also be sized in * powers of 2 (1024, 2048, ... 65536). * * For the moment the maximum allocation size is HAMMER2_PBUFSIZE (64K), * which is 2^16. Larger extents may be supported in the future. Smaller * fragments might be supported in the future (down to 64 bytes is possible), * but probably will not be. * * A full indirect block use supports 1024 x 64-byte blockrefs in a 64KB * buffer. Indirect blocks down to 1KB are supported to keep small * directories small. * * A maximally sized file (2^64-1 bytes) requires 5 indirect block levels. * The hammer2_blockset in the volume header or file inode has another 8 * entries, giving us 66+3 = 69 bits of address space. However, some bits * are taken up by (potentially) requests for redundant copies. HAMMER2 * currently supports up to 8 copies, which brings the address space down * to 66 bits and gives us 2 bits of leeway. */ #define HAMMER2_MIN_ALLOC 1024 /* minimum allocation size */ #define HAMMER2_MIN_RADIX 10 /* minimum allocation size 2^N */ #define HAMMER2_MAX_RADIX 16 /* maximum allocation size 2^N */ #define HAMMER2_KEY_RADIX 64 /* number of bits in key */ /* * MINALLOCSIZE - The minimum allocation size. This can be smaller * or larger than the minimum physical IO size. * * NOTE: Should not be larger than 1K since inodes * are 1K. * * MINIOSIZE - The minimum IO size. This must be less than * or equal to HAMMER2_PBUFSIZE. * * XXX currently must be set to MINALLOCSIZE until/if * we deal with recursive buffer cache locks. * * HAMMER2_PBUFSIZE - Topological block size used by files for all * blocks except the block straddling EOF. * * HAMMER2_SEGSIZE - Allocation map segment size, typically 2MB */ #define HAMMER2_SEGSIZE (65536 * 8) #define HAMMER2_PBUFRADIX 16 /* physical buf (1<<16) bytes */ #define HAMMER2_PBUFSIZE 65536 #define HAMMER2_LBUFRADIX 14 /* logical buf (1<<14) bytes */ #define HAMMER2_LBUFSIZE 16384 #if 0 #define HAMMER2_MINIORADIX 16 /* minimum phsical IO size */ #define HAMMER2_MINIOSIZE 65536 #endif #define HAMMER2_MINIORADIX HAMMER2_MINALLOCRADIX #define HAMMER2_MINIOSIZE HAMMER2_MINALLOCSIZE #define HAMMER2_MINALLOCRADIX 10 /* minimum block allocation size */ #define HAMMER2_MINALLOCSIZE 1024 #define HAMMER2_IND_BYTES_MIN 4096 /* first indirect layer only */ #define HAMMER2_IND_BYTES_MAX HAMMER2_PBUFSIZE #define HAMMER2_IND_COUNT_MIN (HAMMER2_IND_BYTES_MIN / \ sizeof(hammer2_blockref_t)) #define HAMMER2_IND_COUNT_MAX (HAMMER2_IND_BYTES_MAX / \ sizeof(hammer2_blockref_t)) /* * In HAMMER2, arrays of blockrefs are fully set-associative, meaning that * any element can occur at any index and holes can be anywhere. As a * future optimization we will be able to flag that such arrays are sorted * and thus optimize lookups, but for now we don't. * * Inodes embed either 512 bytes of direct data or an array of 8 blockrefs, * resulting in highly efficient storage for files <= 512 bytes and for files * <= 512KB. Up to 8 directory entries can be referenced from a directory * without requiring an indirect block. * * Indirect blocks are typically either 4KB (64 blockrefs / ~4MB represented), * or 64KB (1024 blockrefs / ~64MB represented). */ #define HAMMER2_SET_COUNT 8 /* direct entries */ #define HAMMER2_SET_RADIX 3 #define HAMMER2_EMBEDDED_BYTES 512 #define HAMMER2_EMBEDDED_RADIX 9 #define HAMMER2_PBUFMASK (HAMMER2_PBUFSIZE - 1) #define HAMMER2_LBUFMASK (HAMMER2_LBUFSIZE - 1) #define HAMMER2_SEGMASK (HAMMER2_SEGSIZE - 1) #define HAMMER2_LBUFMASK64 ((hammer2_off_t)HAMMER2_LBUFMASK) #define HAMMER2_PBUFSIZE64 ((hammer2_off_t)HAMMER2_PBUFSIZE) #define HAMMER2_PBUFMASK64 ((hammer2_off_t)HAMMER2_PBUFMASK) #define HAMMER2_SEGSIZE64 ((hammer2_off_t)HAMMER2_SEGSIZE) #define HAMMER2_SEGMASK64 ((hammer2_off_t)HAMMER2_SEGMASK) #define HAMMER2_UUID_STRING "5cbb9ad1-862d-11dc-a94d-01301bb8a9f5" /* * A HAMMER2 filesystem is always sized in multiples of 8MB. * * A 4MB segment is reserved at the beginning of each 2GB zone. This segment * contains the volume header (or backup volume header), the free block * table, and possibly other information in the future. * * 4MB = 64 x 64K blocks. Each 4MB segment is broken down as follows: * * +-----------------------+ * | Volume Hdr | block 0 volume header & alternates * +-----------------------+ (first four zones only) * | (A) FreeBlk layer0 | block 1 free block table * | (A) FreeBlk layer1 | * | (A) FreeBlk layer2 | * | (A) FreeBlk layer3 | * | (A) FreeBlk layer4[8] | (note: 8x64K -> 128x4K) * +-----------------------+ * | (B) FreeBlk layer0 | block 13 free block table * | (B) FreeBlk layer1 | * | (B) FreeBlk layer2 | * | (B) FreeBlk layer3 | * | (B) FreeBlk layer4[8] | * +-----------------------+ * | (C) FreeBlk layer0 | block 25 free block table * | (C) FreeBlk layer1 | * | (C) FreeBlk layer2 | * | (C) FreeBlk layer3 | * | (C) FreeBlk layer4[8] | * +-----------------------+ * | (D) FreeBlk layer0 | block 37 free block table * | (D) FreeBlk layer1 | * | (D) FreeBlk layer2 | * | (D) FreeBlk layer3 | * | (D) FreeBlk layer4[8] | * +-----------------------+ * | | block 49...63 * | reserved | * | | * +-----------------------+ * * The first few 2GB zones contain volume headers and volume header backups. * After that the volume header block# is reserved. The first 2GB zone * contains all four FreeBlk layers, for example, but the layer1 FreeBlk * is only needed once every 1TB. The free block topology rotates between * several groups {A,B,C,D} in order to ensure that the free block table * is clean upon reboot after a crash or disk failure. * * The Free block table has a resolution of 1KB */ #define HAMMER2_VOLUME_ALIGN (8 * 1024 * 1024) #define HAMMER2_VOLUME_ALIGN64 ((hammer2_off_t)HAMMER2_VOLUME_ALIGN) #define HAMMER2_VOLUME_ALIGNMASK (HAMMER2_VOLUME_ALIGN - 1) #define HAMMER2_VOLUME_ALIGNMASK64 ((hammer2_off_t)HAMMER2_VOLUME_ALIGNMASK) #define HAMMER2_NEWFS_ALIGN (HAMMER2_VOLUME_ALIGN) #define HAMMER2_NEWFS_ALIGN64 ((hammer2_off_t)HAMMER2_VOLUME_ALIGN) #define HAMMER2_NEWFS_ALIGNMASK (HAMMER2_VOLUME_ALIGN - 1) #define HAMMER2_NEWFS_ALIGNMASK64 ((hammer2_off_t)HAMMER2_NEWFS_ALIGNMASK) #define HAMMER2_ZONE_BYTES64 (2LLU * 1024 * 1024 * 1024) #define HAMMER2_ZONE_MASK64 (HAMMER2_ZONE_BYTES64 - 1) #define HAMMER2_ZONE_SEG (4 * 1024 * 1024) #define HAMMER2_ZONE_SEG64 ((hammer2_off_t)HAMMER2_ZONE_SEG) #define HAMMER2_ZONE_BLOCKS_SEG (HAMMER2_ZONE_SEG / HAMMER2_PBUFSIZE) /* * 64 x 64KB blocks are reserved at the base of each 2GB zone. These blocks * are used to store the volume header or volume header backups, allocation * tree, and other information in the future. * * All specified blocks are not necessarily used in all 2GB zones. However, * dead areas are reserved and MUST NOT BE USED for other purposes. * * The freemap is arranged into four groups. Modifications rotate through * the groups on a block by block basis (so all the blocks are not necessarily * synchronized to the same group). Only three groups are actually necessary * (stable, flushing, modifying). * * 64KB freemap indirect blocks are represented by layers 0, 1, 2, and 3. * 4KB freemap leaf blocks each represent 16MB of storage so 128 x 4KB are * needed per zone, which equates to 8 x 64KB layer4 blocks per zone. */ #define HAMMER2_ZONE_VOLHDR 0 /* volume header or backup */ #define HAMMER2_ZONE_FREEMAP_A 1 /* freemap layer group A */ #define HAMMER2_ZONE_FREEMAP_B 13 /* freemap layer group B */ #define HAMMER2_ZONE_FREEMAP_C 25 /* freemap layer group C */ #define HAMMER2_ZONE_FREEMAP_D 37 /* freemap layer group D */ #define HAMMER2_ZONEFM_LAYER0 0 /* relative to FREEMAP_x */ #define HAMMER2_ZONEFM_LAYER1 1 #define HAMMER2_ZONEFM_LAYER2 2 #define HAMMER2_ZONEFM_LAYER3 3 #define HAMMER2_ZONEFM_LAYER4 4 /* 4-11 (8 64KB blocks) */ #define HAMMER2_ZONE_BLOCK49 49 /* future */ #define HAMMER2_ZONE_BLOCK50 50 /* future */ #define HAMMER2_ZONE_BLOCK51 51 /* future */ #define HAMMER2_ZONE_BLOCK52 52 /* future */ #define HAMMER2_ZONE_BLOCK53 53 /* future */ #define HAMMER2_ZONE_BLOCK54 54 /* future */ #define HAMMER2_ZONE_BLOCK55 55 /* future */ #define HAMMER2_ZONE_BLOCK56 56 /* future */ #define HAMMER2_ZONE_BLOCK57 57 /* future */ #define HAMMER2_ZONE_BLOCK58 58 /* future */ #define HAMMER2_ZONE_BLOCK59 59 /* future */ #define HAMMER2_ZONE_BLOCK60 60 /* future */ #define HAMMER2_ZONE_BLOCK61 61 /* future */ #define HAMMER2_ZONE_BLOCK62 62 /* future */ #define HAMMER2_ZONE_BLOCK63 63 /* future */ /* * Two linear areas can be reserved after the initial 2MB segment in the base * zone (the one starting at offset 0). These areas are NOT managed by the * block allocator and do not fall under HAMMER2 crc checking rules based * at the volume header (but can be self-CRCd internally, depending). */ #define HAMMER2_BOOT_MIN_BYTES HAMMER2_VOLUME_ALIGN #define HAMMER2_BOOT_NOM_BYTES (64*1024*1024) #define HAMMER2_BOOT_MAX_BYTES (256*1024*1024) #define HAMMER2_REDO_MIN_BYTES HAMMER2_VOLUME_ALIGN #define HAMMER2_REDO_NOM_BYTES (256*1024*1024) #define HAMMER2_REDO_MAX_BYTES (1024*1024*1024) /* * Most HAMMER2 types are implemented as unsigned 64-bit integers. * Transaction ids are monotonic. * * We utilize 32-bit iSCSI CRCs. */ typedef uint64_t hammer2_tid_t; typedef uint64_t hammer2_off_t; typedef uint64_t hammer2_key_t; typedef uint32_t hammer2_crc32_t; /* * Miscellanious ranges (all are unsigned). */ #define HAMMER2_MIN_TID 1ULL #define HAMMER2_MAX_TID 0xFFFFFFFFFFFFFFFFULL #define HAMMER2_MIN_KEY 0ULL #define HAMMER2_MAX_KEY 0xFFFFFFFFFFFFFFFFULL #define HAMMER2_MIN_OFFSET 0ULL #define HAMMER2_MAX_OFFSET 0xFFFFFFFFFFFFFFFFULL /* * HAMMER2 data offset special cases and masking. * * All HAMMER2 data offsets have to be broken down into a 64K buffer base * offset (HAMMER2_OFF_MASK_HI) and a 64K buffer index (HAMMER2_OFF_MASK_LO). * * Indexes into physical buffers are always 64-byte aligned. The low 6 bits * of the data offset field specifies how large the data chunk being pointed * to as a power of 2. The theoretical minimum radix is thus 6 (The space * needed in the low bits of the data offset field). However, the practical * minimum allocation chunk size is 1KB (a radix of 10), so HAMMER2 sets * HAMMER2_MIN_RADIX to 10. The maximum radix is currently 16 (64KB), but * we fully intend to support larger extents in the future. */ #define HAMMER2_OFF_BAD ((hammer2_off_t)-1) #define HAMMER2_OFF_MASK 0xFFFFFFFFFFFFFFC0ULL #define HAMMER2_OFF_MASK_LO (HAMMER2_OFF_MASK & HAMMER2_PBUFMASK64) #define HAMMER2_OFF_MASK_HI (~HAMMER2_PBUFMASK64) #define HAMMER2_OFF_MASK_RADIX 0x000000000000003FULL #define HAMMER2_MAX_COPIES 6 /* * HAMMER2 directory support and pre-defined keys */ #define HAMMER2_DIRHASH_VISIBLE 0x8000000000000000ULL #define HAMMER2_DIRHASH_USERMSK 0x7FFFFFFFFFFFFFFFULL #define HAMMER2_DIRHASH_LOMASK 0x0000000000007FFFULL #define HAMMER2_DIRHASH_HIMASK 0xFFFFFFFFFFFF0000ULL #define HAMMER2_DIRHASH_FORCED 0x0000000000008000ULL /* bit forced on */ #define HAMMER2_SROOT_KEY 0x0000000000000000ULL /* volume to sroot */ /* * The media block reference structure. This forms the core of the HAMMER2 * media topology recursion. This 64-byte data structure is embedded in the * volume header, in inodes (which are also directory entries), and in * indirect blocks. * * A blockref references a single media item, which typically can be a * directory entry (aka inode), indirect block, or data block. * * The primary feature a blockref represents is the ability to validate * the entire tree underneath it via its check code. Any modification to * anything propagates up the blockref tree all the way to the root, replacing * the related blocks. Propagations can shortcut to the volume root to * implement the 'fast syncing' feature but this only delays the eventual * propagation. * * The check code can be a simple 32-bit iscsi code, a 64-bit crc, * or as complex as a 192 bit cryptographic hash. 192 bits is the maximum * supported check code size, which is not sufficient for unverified dedup * UNLESS one doesn't mind once-in-a-blue-moon data corruption (such as when * farming web data). HAMMER2 has an unverified dedup feature for just this * purpose. * * -- * * NOTE: The range of keys represented by the blockref is (key) to * ((key) + (1LL << keybits) - 1). HAMMER2 usually populates * blocks bottom-up, inserting a new root when radix expansion * is required. */ struct hammer2_blockref { /* MUST BE EXACTLY 64 BYTES */ uint8_t type; /* type of underlying item */ uint8_t methods; /* check method & compression method */ uint8_t copyid; /* specify which copy this is */ uint8_t keybits; /* #of keybits masked off 0=leaf */ uint8_t vradix; /* virtual data/meta-data size */ uint8_t flags; /* blockref flags */ uint8_t reserved06; uint8_t reserved07; hammer2_key_t key; /* key specification */ hammer2_tid_t mirror_tid; /* propagate for mirror scan */ hammer2_tid_t modify_tid; /* modifications sans propagation */ hammer2_off_t data_off; /* low 6 bits is phys size (radix)*/ union { /* check info */ char buf[24]; struct { uint32_t value; uint32_t unused[5]; } iscsi32; struct { uint64_t value; uint64_t unused[2]; } crc64; struct { char data[24]; } sha192; /* * Freemap hints are embedded in addition to the icrc32. * * biggest - largest possible allocation 2^N within sub-tree. * typically initialized to 64 in freemap_blockref * and to 54 in each blockref[] entry in the * FREEMAP_ROOT indirect block. * * An allocation > 2^N is guaranteed to fail. An * allocation <= 2^N MAY fail, and if it does the * biggest hint will be adjusted downward. * * Used when allocating space. */ struct { uint32_t icrc32; uint8_t biggest; uint8_t reserved05; uint8_t reserved06; uint8_t reserved07; uint64_t avail; /* total available bytes */ uint64_t unused; /* unused must be 0 */ } freemap; } check; }; typedef struct hammer2_blockref hammer2_blockref_t; #if 0 #define HAMMER2_BREF_SYNC1 0x01 /* modification synchronized */ #define HAMMER2_BREF_SYNC2 0x02 /* modification committed */ #define HAMMER2_BREF_DESYNCCHLD 0x04 /* desynchronize children */ #define HAMMER2_BREF_DELETED 0x80 /* indicates a deletion */ #endif #define HAMMER2_BLOCKREF_BYTES 64 /* blockref struct in bytes */ #define HAMMER2_BREF_TYPE_EMPTY 0 #define HAMMER2_BREF_TYPE_INODE 1 #define HAMMER2_BREF_TYPE_INDIRECT 2 #define HAMMER2_BREF_TYPE_DATA 3 #define HAMMER2_BREF_TYPE_FREEMAP_ROOT 4 #define HAMMER2_BREF_TYPE_FREEMAP_NODE 5 #define HAMMER2_BREF_TYPE_FREEMAP_LEAF 6 #define HAMMER2_BREF_TYPE_VOLUME 255 /* pseudo-type */ #define HAMMER2_ENC_CHECK(n) ((n) << 4) #define HAMMER2_DEC_CHECK(n) (((n) >> 4) & 15) #define HAMMER2_CHECK_NONE 0 #define HAMMER2_CHECK_ISCSI32 1 #define HAMMER2_CHECK_CRC64 2 #define HAMMER2_CHECK_SHA192 3 #define HAMMER2_CHECK_FREEMAP 4 #define HAMMER2_ENC_COMP(n) (n) #define HAMMER2_DEC_COMP(n) ((n) & 15) #define HAMMER2_COMP_NONE 0 #define HAMMER2_COMP_AUTOZERO 1 /* * HAMMER2 block references are collected into sets of 8 blockrefs. These * sets are fully associative, meaning the elements making up a set are * not sorted in any way and may contain duplicate entries, holes, or * entries which shortcut multiple levels of indirection. Sets are used * in various ways: * * (1) When redundancy is desired a set may contain several duplicate * entries pointing to different copies of the same data. Up to 8 copies * are supported but the set structure becomes a bit inefficient once * you go over 4. * * (2) The blockrefs in a set can shortcut multiple levels of indirections * within the bounds imposed by the parent of set. * * When a set fills up another level of indirection is inserted, moving * some or all of the set's contents into indirect blocks placed under the * set. This is a top-down approach in that indirect blocks are not created * until the set actually becomes full (that is, the entries in the set can * shortcut the indirect blocks when the set is not full). Depending on how * things are filled multiple indirect blocks will eventually be created. * * Indirect blocks are typically 4KB (64 entres) or 64KB (1024 entries) and * are also treated as fully set-associative. */ struct hammer2_blockset { hammer2_blockref_t blockref[HAMMER2_SET_COUNT]; }; typedef struct hammer2_blockset hammer2_blockset_t; /* * Catch programmer snafus */ #if (1 << HAMMER2_SET_RADIX) != HAMMER2_SET_COUNT #error "hammer2 direct radix is incorrect" #endif #if (1 << HAMMER2_PBUFRADIX) != HAMMER2_PBUFSIZE #error "HAMMER2_PBUFRADIX and HAMMER2_PBUFSIZE are inconsistent" #endif #if (1 << HAMMER2_MIN_RADIX) != HAMMER2_MIN_ALLOC #error "HAMMER2_MIN_RADIX and HAMMER2_MIN_ALLOC are inconsistent" #endif /* * The media indirect block structure. */ struct hammer2_indblock_data { hammer2_blockref_t blockref[HAMMER2_IND_COUNT_MAX]; }; typedef struct hammer2_indblock_data hammer2_indblock_data_t; /* * In HAMMER2 inodes ARE directory entries, with a special exception for * hardlinks. The inode number is stored in the inode rather than being * based on the location of the inode (since the location moves every time * the inode or anything underneath the inode is modified). * * The inode is 1024 bytes, made up of 256 bytes of meta-data, 256 bytes * for the filename, and 512 bytes worth of direct file data OR an embedded * blockset. * * Directories represent one inode per blockref. Inodes are not laid out * as a file but instead are represented by the related blockrefs. The * blockrefs, in turn, are indexed by the 64-bit directory hash key. Remember * that blocksets are fully associative, so a certain degree efficiency is * achieved just from that. * * Up to 512 bytes of direct data can be embedded in an inode, and since * inodes are essentially directory entries this also means that small data * files end up simply being laid out linearly in the directory, resulting * in fewer seeks and highly optimal access. * * The compression mode can be changed at any time in the inode and is * recorded on a blockref-by-blockref basis. * * Hardlinks are supported via the inode map. Essentially the way a hardlink * works is that all individual directory entries representing the same file * are special cased and specify the same inode number. The actual file * is placed in the nearest parent directory that is parent to all instances * of the hardlink. If all hardlinks to a file are in the same directory * the actual file will also be placed in that directory. This file uses * the inode number as the directory entry key and is invisible to normal * directory scans. Real directory entry keys are differentiated from the * inode number key via bit 63. Access to the hardlink silently looks up * the real file and forwards all operations to that file. Removal of the * last hardlink also removes the real file. * * (attr_tid) is only updated when the inode's specific attributes or regular * file size has changed, and affects path lookups and stat. (attr_tid) * represents a special cache coherency lock under the inode. The inode * blockref's modify_tid will always cover it. * * (dirent_tid) is only updated when an entry under a directory inode has * been created, deleted, renamed, or had its attributes change, and affects * directory lookups and scans. (dirent_tid) represents another special cache * coherency lock under the inode. The inode blockref's modify_tid will * always cover it. */ #define HAMMER2_INODE_BYTES 1024 /* (asserted by code) */ #define HAMMER2_INODE_MAXNAME 256 /* maximum name in bytes */ #define HAMMER2_INODE_VERSION_ONE 1 struct hammer2_inode_data { uint16_t version; /* 0000 inode data version */ uint16_t reserved02; /* 0002 */ /* * core inode attributes, inode type, misc flags */ uint32_t uflags; /* 0004 chflags */ uint32_t rmajor; /* 0008 available for device nodes */ uint32_t rminor; /* 000C available for device nodes */ uint64_t ctime; /* 0010 inode change time */ uint64_t mtime; /* 0018 modified time */ uint64_t atime; /* 0020 access time (unsupported) */ uint64_t btime; /* 0028 birth time */ uuid_t uid; /* 0030 uid / degenerate unix uid */ uuid_t gid; /* 0040 gid / degenerate unix gid */ uint8_t type; /* 0050 object type */ uint8_t op_flags; /* 0051 operational flags */ uint16_t cap_flags; /* 0052 capability flags */ uint32_t mode; /* 0054 unix modes (typ low 16 bits) */ /* * inode size, identification, localized recursive configuration * for compression and backup copies. */ hammer2_tid_t inum; /* 0058 inode number */ hammer2_off_t size; /* 0060 size of file */ uint64_t nlinks; /* 0068 hard links (typ only dirs) */ hammer2_tid_t iparent; /* 0070 parent inum (recovery only) */ hammer2_key_t name_key; /* 0078 full filename key */ uint16_t name_len; /* 0080 filename length */ uint8_t ncopies; /* 0082 ncopies to local media */ uint8_t comp_algo; /* 0083 compression request & algo */ /* * These fields are currently only applicable to PFSROOTs. * * NOTE: We can't use {volume_data->fsid, pfs_clid} to uniquely * identify an instance of a PFS in the cluster because * a mount may contain more than one copy of the PFS as * a separate node. {pfs_clid, pfs_fsid} must be used for * registration in the cluster. */ uint8_t target_type; /* 0084 hardlink target type */ uint8_t reserved85; /* 0085 */ uint8_t reserved86; /* 0086 */ uint8_t pfs_type; /* 0087 (if PFSROOT) node type */ uint64_t pfs_inum; /* 0088 (if PFSROOT) inum allocator */ uuid_t pfs_clid; /* 0090 (if PFSROOT) cluster uuid */ uuid_t pfs_fsid; /* 00A0 (if PFSROOT) unique uuid */ /* * Quotas and cumulative sub-tree counters. */ hammer2_off_t data_quota; /* 00B0 subtree quota in bytes */ hammer2_off_t data_count; /* 00B8 subtree byte count */ hammer2_off_t inode_quota; /* 00C0 subtree quota inode count */ hammer2_off_t inode_count; /* 00C8 subtree inode count */ hammer2_tid_t attr_tid; /* 00D0 attributes changed */ hammer2_tid_t dirent_tid; /* 00D8 directory/attr changed */ uint64_t reservedE0; /* 00E0 */ uint64_t reservedE8; /* 00E8 */ uint64_t reservedF0; /* 00F0 */ uint64_t reservedF8; /* 00F8 */ unsigned char filename[HAMMER2_INODE_MAXNAME]; /* 0100-01FF (256 char, unterminated) */ union { /* 0200-03FF (64x8 = 512 bytes) */ struct hammer2_blockset blockset; char data[HAMMER2_EMBEDDED_BYTES]; } u; }; typedef struct hammer2_inode_data hammer2_inode_data_t; #define HAMMER2_OPFLAG_DIRECTDATA 0x01 #define HAMMER2_OPFLAG_PFSROOT 0x02 #define HAMMER2_OPFLAG_COPYIDS 0x04 /* copyids override parent */ #define HAMMER2_OBJTYPE_UNKNOWN 0 #define HAMMER2_OBJTYPE_DIRECTORY 1 #define HAMMER2_OBJTYPE_REGFILE 2 #define HAMMER2_OBJTYPE_FIFO 4 #define HAMMER2_OBJTYPE_CDEV 5 #define HAMMER2_OBJTYPE_BDEV 6 #define HAMMER2_OBJTYPE_SOFTLINK 7 #define HAMMER2_OBJTYPE_HARDLINK 8 /* dummy entry for hardlink */ #define HAMMER2_OBJTYPE_SOCKET 9 #define HAMMER2_OBJTYPE_WHITEOUT 10 #define HAMMER2_COPYID_NONE 0 #define HAMMER2_COPYID_LOCAL ((uint8_t)-1) /* * PEER types identify connections and help cluster controller filter * out unwanted SPANs. */ #define HAMMER2_PEER_NONE DMSG_PEER_NONE #define HAMMER2_PEER_CLUSTER DMSG_PEER_CLUSTER #define HAMMER2_PEER_BLOCK DMSG_PEER_BLOCK #define HAMMER2_PEER_HAMMER2 DMSG_PEER_HAMMER2 #define HAMMER2_COPYID_COUNT DMSG_COPYID_COUNT /* * PFS types identify a PFS on media and in LNK_SPAN messages. */ #define HAMMER2_PFSTYPE_NONE DMSG_PFSTYPE_NONE #define HAMMER2_PFSTYPE_ADMIN DMSG_PFSTYPE_ADMIN #define HAMMER2_PFSTYPE_CLIENT DMSG_PFSTYPE_CLIENT #define HAMMER2_PFSTYPE_CACHE DMSG_PFSTYPE_CACHE #define HAMMER2_PFSTYPE_COPY DMSG_PFSTYPE_COPY #define HAMMER2_PFSTYPE_SLAVE DMSG_PFSTYPE_SLAVE #define HAMMER2_PFSTYPE_SOFT_SLAVE DMSG_PFSTYPE_SOFT_SLAVE #define HAMMER2_PFSTYPE_SOFT_MASTER DMSG_PFSTYPE_SOFT_MASTER #define HAMMER2_PFSTYPE_MASTER DMSG_PFSTYPE_MASTER #define HAMMER2_PFSTYPE_MAX DMSG_PFSTYPE_MAX /* * Allocation Table * * In HAMMER2 the allocation table hangs off of the volume header and * utilizes somewhat customized hammer2_blockref based indirect blocks * until hitting the leaf bitmap. BREF_TYPE_FREEMAP_ROOT and * BREF_TYPE_FREEMAP_NODE represent the indirect blocks but are formatted * the same as BREF_TYPE_INDIRECT except for the (biggest) and (avail) * fields which use some of the check union space. Thus a special CHECK * id (CHECK_FREEMAP instead of CHECK_ISCSI32) is also specified for these * babies. * * newfs_hammer2 builds the FREEMAP_ROOT block and assigns a radix of * 34, 44, 54, or 64 depending on whether the freemap is to be fitted * to the storage or is to maximized for (possibly) sparse storage. * Other keybits specifications for FREEMAP_ROOT are illegal. Even fitted * storage is required to specify at least a keybits value of 34. * * Total possible representation is 2^64 (16 Exabytes). * 10: 1024 entries / 64KB 16EB (16PB per entry) layer0 * 10: 1024 entries / 64KB 16PB (16TB per entry) layer1 * 10: 1024 entries / 64KB 16TB (16GB per entry) layer2 * 10: 1024 entries / 64KB 16GB (16MB per entry) layer3 * 24: 16384 x 1KB allocgran / 4KB 16MB layer4 * * To make the radix come out to exactly 64 the leaf bitmaps are arranged * into 4KB buffers, with each buffer representing a freemap for 16MB worth * of storage using a 1KB allocation granularity. The leaf bitmaps are * structures and not just a plain bitmap, hence the extra space needed to * represent 16384 x 1KB blocks. * * The reserved area at the beginning of each 2GB zone is marked as being * allocated on-the-fly and does not have to be pre-set in the freemap, * which is just as well as that would require newfs_hammer2 to do a lot * of writing otherwise. * * Indirect blocks are usually created with a semi-dynamic radix but in the * case of freemap-related indirect blocks, the blocks use a static radix * tree with associations to specific reserved blocks. */ /* * 4KB -> hammer2_freemap_elm[256] * * bitmap - 64 bits x 1KB representing 64KB. A '1' bit represents * an allocated block. * * generation - Incremented upon any allocation. Can't increment more * than +64 per background freeing pass due to there being * only 64 bits. * * biggest0 - biggest hint (radix) for freemap_elm. Represents up to * 64KB (radix 16). * * biggest1 - biggest hint (radix) for aligned groups of 16 elements, * stored in elm[0], elm[16], etc. Represents up to 1MB. * (radix 20) * * biggest2 - biggest hint (radix) for aligned groups of 256 elements * (i.e. the whole array, only used by elm[0]). * Represents up to 16MB (radix 24). * * The hinting is used as part of the allocation mechanism to reduce scan * time, which is particularly important as a filesystem approaches full. * Fill ratios are handled at the indirect block level (in the blockrefs) and * not here. */ struct hammer2_freemap_elm { uint64_t bitmap; uint8_t generation; uint8_t biggest0; uint8_t biggest1; uint8_t biggest2; uint32_t reserved0C; }; typedef struct hammer2_freemap_elm hammer2_freemap_elm_t; #define HAMMER2_FREEMAP_LEAF_BYTES 4096 #define HAMMER2_FREEMAP_LEAF_ENTRIES (HAMMER2_FREEMAP_LEAF_BYTES / \ sizeof(hammer2_freemap_elm_t)) #define HAMMER2_FREEMAP_LEAF_RADIX 24 #define HAMMER2_FREEMAP_NODE_RADIX 10 #define HAMMER2_FREEMAP_ELM_RADIX 5 /* 2^5 == 32 bits */ #define HAMMER2_BIGF_KILLED 0x80 /* * Flags (8 bits) - blockref, for freemap only * * Note that the minimum chunk size is 1KB so we could theoretically have * 10 bits here, but we might have some future extension that allows a * chunk size down to 256 bytes and if so we will need bits 8 and 9. */ #define HAMMER2_AVF_SELMASK 0x03 /* select group */ #define HAMMER2_AVF_ALL_ALLOC 0x04 /* indicate all allocated */ #define HAMMER2_AVF_ALL_FREE 0x08 /* indicate all free */ #define HAMMER2_AVF_RESERVED10 0x10 #define HAMMER2_AVF_RESERVED20 0x20 #define HAMMER2_AVF_RESERVED40 0x40 #define HAMMER2_AVF_RESERVED80 0x80 #define HAMMER2_AVF_AVMASK32 ((uint32_t)0xFFFFFF00LU) #define HAMMER2_AVF_AVMASK64 ((uint64_t)0xFFFFFFFFFFFFFF00LLU) #define HAMMER2_AV_SELECT_A 0x00 #define HAMMER2_AV_SELECT_B 0x01 #define HAMMER2_AV_SELECT_C 0x02 #define HAMMER2_AV_SELECT_D 0x03 /* * The volume header eats a 64K block. There is currently an issue where * we want to try to fit all nominal filesystem updates in a 512-byte section * but it may be a lost cause due to the need for a blockset. * * All information is stored in host byte order. The volume header's magic * number may be checked to determine the byte order. If you wish to mount * between machines w/ different endian modes you'll need filesystem code * which acts on the media data consistently (either all one way or all the * other). Our code currently does not do that. * * A read-write mount may have to recover missing allocations by doing an * incremental mirror scan looking for modifications made after alloc_tid. * If alloc_tid == last_tid then no recovery operation is needed. Recovery * operations are usually very, very fast. * * Read-only mounts do not need to do any recovery, access to the filesystem * topology is always consistent after a crash (is always consistent, period). * However, there may be shortcutted blockref updates present from deep in * the tree which are stored in the volumeh eader and must be tracked on * the fly. * * NOTE: The copyinfo[] array contains the configuration for both the * cluster connections and any local media copies. The volume * header will be replicated for each local media copy. * * The mount command may specify multiple medias or just one and * allow HAMMER2 to pick up the others when it checks the copyinfo[] * array on mount. * * NOTE: root_blockref points to the super-root directory, not the root * directory. The root directory will be a subdirectory under the * super-root. * * The super-root directory contains all root directories and all * snapshots (readonly or writable). It is possible to do a * null-mount of the super-root using special path constructions * relative to your mounted root. * * NOTE: HAMMER2 allows any subdirectory tree to be managed as if it were * a PFS, including mirroring and storage quota operations, and this is * prefered over creating discrete PFSs in the super-root. Instead * the super-root is most typically used to create writable snapshots, * alternative roots, and so forth. The super-root is also used by * the automatic snapshotting mechanism. */ #define HAMMER2_VOLUME_ID_HBO 0x48414d3205172011LLU #define HAMMER2_VOLUME_ID_ABO 0x11201705324d4148LLU struct hammer2_volume_data { /* * sector #0 - 512 bytes */ uint64_t magic; /* 0000 Signature */ hammer2_off_t boot_beg; /* 0008 Boot area (future) */ hammer2_off_t boot_end; /* 0010 (size = end - beg) */ hammer2_off_t aux_beg; /* 0018 Aux area (future) */ hammer2_off_t aux_end; /* 0020 (size = end - beg) */ hammer2_off_t volu_size; /* 0028 Volume size, bytes */ uint32_t version; /* 0030 */ uint32_t flags; /* 0034 */ uint8_t copyid; /* 0038 copyid of phys vol */ uint8_t freemap_version; /* 0039 freemap algorithm */ uint8_t peer_type; /* 003A HAMMER2_PEER_xxx */ uint8_t reserved003B; /* 003B */ uint32_t reserved003C; /* 003C */ uuid_t fsid; /* 0040 */ uuid_t fstype; /* 0050 */ /* * allocator_size is precalculated at newfs time and does not include * reserved blocks, boot, or redo areas. * * Initial non-reserved-area allocations do not use the freemap * but instead adjust alloc_iterator. Dynamic allocations take * over starting at (allocator_beg). This makes newfs_hammer2's * job a lot easier and can also serve as a testing jig. */ hammer2_off_t allocator_size; /* 0060 Total data space */ hammer2_off_t allocator_free; /* 0068 Free space */ hammer2_off_t allocator_beg; /* 0070 Initial allocations */ hammer2_tid_t mirror_tid; /* 0078 best committed tid */ hammer2_tid_t alloc_tid; /* 0080 Alloctable modify tid */ hammer2_blockref_t freemap_blockref; /* 0088-00C7 */ /* * Copyids are allocated dynamically from the copyexists bitmap. * An id from the active copies set (up to 8, see copyinfo later on) * may still exist after the copy set has been removed from the * volume header and its bit will remain active in the bitmap and * cannot be reused until it is 100% removed from the hierarchy. */ uint32_t copyexists[8]; /* 00C8-00E7 copy exists bmap */ char reserved0140[248]; /* 00E8-01DF */ /* * 32 bit CRC array at the end of the first 512 byte sector. * * icrc_sects[7] - First 512-4 bytes of volume header (including all * the other icrc's except the last one). * * icrc_sects[6] - Second 512-4 bytes of volume header, which is * the blockset for the root. */ hammer2_crc32_t icrc_sects[8]; /* 01E0-01FF */ /* * sector #1 - 512 bytes * * The entire sector is used by a blockset. */ hammer2_blockset_t sroot_blockset; /* 0200-03FF Superroot dir */ /* * sector #2-7 */ char sector2[512]; /* 0400-05FF reserved */ char sector3[512]; /* 0600-07FF reserved */ char sector4[512]; /* 0800-09FF reserved */ char sector5[512]; /* 0A00-0BFF reserved */ char sector6[512]; /* 0C00-0DFF reserved */ char sector7[512]; /* 0E00-0FFF reserved */ /* * sector #8-71 - 32768 bytes * * Contains the configuration for up to 256 copyinfo targets. These * specify local and remote copies operating as masters or slaves. * copyid's 0 and 255 are reserved (0 indicates an empty slot and 255 * indicates the local media). * * Each inode contains a set of up to 8 copyids, either inherited * from its parent or explicitly specified in the inode, which * indexes into this array. */ /* 1000-8FFF copyinfo config */ dmsg_vol_data_t copyinfo[HAMMER2_COPYID_COUNT]; /* * Remaining sections are reserved for future use. */ char reserved0400[0x6FFC]; /* 9000-FFFB reserved */ /* * icrc on entire volume header */ hammer2_crc32_t icrc_volheader; /* FFFC-FFFF full volume icrc*/ }; typedef struct hammer2_volume_data hammer2_volume_data_t; /* * Various parts of the volume header have their own iCRCs. * * The first 512 bytes has its own iCRC stored at the end of the 512 bytes * and not included the icrc calculation. * * The second 512 bytes also has its own iCRC but it is stored in the first * 512 bytes so it covers the entire second 512 bytes. * * The whole volume block (64KB) has an iCRC covering all but the last 4 bytes, * which is where the iCRC for the whole volume is stored. This is currently * a catch-all for anything not individually iCRCd. */ #define HAMMER2_VOL_ICRC_SECT0 7 #define HAMMER2_VOL_ICRC_SECT1 6 #define HAMMER2_VOLUME_BYTES 65536 #define HAMMER2_VOLUME_ICRC0_OFF 0 #define HAMMER2_VOLUME_ICRC1_OFF 512 #define HAMMER2_VOLUME_ICRCVH_OFF 0 #define HAMMER2_VOLUME_ICRC0_SIZE (512 - 4) #define HAMMER2_VOLUME_ICRC1_SIZE (512) #define HAMMER2_VOLUME_ICRCVH_SIZE (65536 - 4) #define HAMMER2_VOL_VERSION_MIN 1 #define HAMMER2_VOL_VERSION_DEFAULT 1 #define HAMMER2_VOL_VERSION_WIP 2 #define HAMMER2_NUM_VOLHDRS 4 union hammer2_media_data { hammer2_volume_data_t voldata; hammer2_inode_data_t ipdata; hammer2_indblock_data_t npdata; char buf[HAMMER2_PBUFSIZE]; }; typedef union hammer2_media_data hammer2_media_data_t; #endif