2 * Copyright (c) 2011-2012 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@dragonflybsd.org>
6 * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
18 * 3. Neither the name of The DragonFly Project nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific, prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #ifndef VFS_HAMMER2_DISK_H_
36 #define VFS_HAMMER2_DISK_H_
46 * The structures below represent the on-disk media structures for the HAMMER2
47 * filesystem. Note that all fields for on-disk structures are naturally
48 * aligned. The host endian format is typically used - compatibility is
49 * possible if the implementation detects reversed endian and adjusts accesses
52 * HAMMER2 primarily revolves around the directory topology: inodes,
53 * directory entries, and block tables. Block device buffer cache buffers
54 * are always 64KB. Logical file buffers are typically 16KB. All data
55 * references utilize 64-bit byte offsets.
57 * Free block management is handled independently using blocks reserved by
62 * The data at the end of a file or directory may be a fragment in order
63 * to optimize storage efficiency. The minimum fragment size is 1KB.
64 * Since allocations are in powers of 2 fragments must also be sized in
65 * powers of 2 (1024, 2048, ... 65536).
67 * For the moment the maximum allocation size is HAMMER2_PBUFSIZE (64K),
68 * which is 2^16. Larger extents may be supported in the future. Smaller
69 * fragments might be supported in the future (down to 64 bytes is possible),
70 * but probably will not be.
72 * A full indirect block use supports 1024 x 64-byte blockrefs in a 64KB
73 * buffer. Indirect blocks down to 1KB are supported to keep small
76 * A maximally sized file (2^64-1 bytes) requires 5 indirect block levels.
77 * The hammer2_blockset in the volume header or file inode has another 8
78 * entries, giving us 66+3 = 69 bits of address space. However, some bits
79 * are taken up by (potentially) requests for redundant copies. HAMMER2
80 * currently supports up to 8 copies, which brings the address space down
81 * to 66 bits and gives us 2 bits of leeway.
83 #define HAMMER2_MIN_ALLOC 1024 /* minimum allocation size */
84 #define HAMMER2_MIN_RADIX 10 /* minimum allocation size 2^N */
85 #define HAMMER2_MAX_RADIX 16 /* maximum allocation size 2^N */
86 #define HAMMER2_KEY_RADIX 64 /* number of bits in key */
89 * MINALLOCSIZE - The minimum allocation size. This can be smaller
90 * or larger than the minimum physical IO size.
92 * NOTE: Should not be larger than 1K since inodes
95 * MINIOSIZE - The minimum IO size. This must be less than
96 * or equal to HAMMER2_PBUFSIZE.
98 * XXX currently must be set to MINALLOCSIZE until/if
99 * we deal with recursive buffer cache locks.
101 * HAMMER2_PBUFSIZE - Topological block size used by files for all
102 * blocks except the block straddling EOF.
104 * HAMMER2_SEGSIZE - Allocation map segment size, typically 2MB
107 #define HAMMER2_SEGSIZE (65536 * 8)
109 #define HAMMER2_PBUFRADIX 16 /* physical buf (1<<16) bytes */
110 #define HAMMER2_PBUFSIZE 65536
111 #define HAMMER2_LBUFRADIX 14 /* logical buf (1<<14) bytes */
112 #define HAMMER2_LBUFSIZE 16384
115 #define HAMMER2_MINIORADIX 16 /* minimum phsical IO size */
116 #define HAMMER2_MINIOSIZE 65536
118 #define HAMMER2_MINIORADIX HAMMER2_MINALLOCRADIX
119 #define HAMMER2_MINIOSIZE HAMMER2_MINALLOCSIZE
121 #define HAMMER2_MINALLOCRADIX 10 /* minimum block allocation size */
122 #define HAMMER2_MINALLOCSIZE 1024
123 #define HAMMER2_IND_BYTES_MIN 4096 /* first indirect layer only */
124 #define HAMMER2_IND_BYTES_MAX HAMMER2_PBUFSIZE
125 #define HAMMER2_IND_COUNT_MIN (HAMMER2_IND_BYTES_MIN / \
126 sizeof(hammer2_blockref_t))
127 #define HAMMER2_IND_COUNT_MAX (HAMMER2_IND_BYTES_MAX / \
128 sizeof(hammer2_blockref_t))
131 * In HAMMER2, arrays of blockrefs are fully set-associative, meaning that
132 * any element can occur at any index and holes can be anywhere. As a
133 * future optimization we will be able to flag that such arrays are sorted
134 * and thus optimize lookups, but for now we don't.
136 * Inodes embed either 512 bytes of direct data or an array of 8 blockrefs,
137 * resulting in highly efficient storage for files <= 512 bytes and for files
138 * <= 512KB. Up to 8 directory entries can be referenced from a directory
139 * without requiring an indirect block.
141 * Indirect blocks are typically either 4KB (64 blockrefs / ~4MB represented),
142 * or 64KB (1024 blockrefs / ~64MB represented).
144 #define HAMMER2_SET_COUNT 8 /* direct entries */
145 #define HAMMER2_SET_RADIX 3
146 #define HAMMER2_EMBEDDED_BYTES 512
147 #define HAMMER2_EMBEDDED_RADIX 9
149 #define HAMMER2_PBUFMASK (HAMMER2_PBUFSIZE - 1)
150 #define HAMMER2_LBUFMASK (HAMMER2_LBUFSIZE - 1)
151 #define HAMMER2_SEGMASK (HAMMER2_SEGSIZE - 1)
153 #define HAMMER2_LBUFMASK64 ((hammer2_off_t)HAMMER2_LBUFMASK)
154 #define HAMMER2_PBUFSIZE64 ((hammer2_off_t)HAMMER2_PBUFSIZE)
155 #define HAMMER2_PBUFMASK64 ((hammer2_off_t)HAMMER2_PBUFMASK)
156 #define HAMMER2_SEGSIZE64 ((hammer2_off_t)HAMMER2_SEGSIZE)
157 #define HAMMER2_SEGMASK64 ((hammer2_off_t)HAMMER2_SEGMASK)
159 #define HAMMER2_UUID_STRING "5cbb9ad1-862d-11dc-a94d-01301bb8a9f5"
162 * A HAMMER2 filesystem is always sized in multiples of 8MB.
164 * A 4MB segment is reserved at the beginning of each 2GB zone. This segment
165 * contains the volume header (or backup volume header), the free block
166 * table, and possibly other information in the future.
168 * 4MB = 64 x 64K blocks. Each 4MB segment is broken down as follows:
170 * +-----------------------+
171 * | Volume Hdr | block 0 volume header & alternates
172 * +-----------------------+ (first four zones only)
173 * | (A) FreeBlk layer0 | block 1 free block table
174 * | (A) FreeBlk layer1 |
175 * | (A) FreeBlk layer2 |
176 * | (A) FreeBlk layer3 |
177 * | (A) FreeBlk layer4[8] | (note: 8x64K -> 128x4K)
178 * +-----------------------+
179 * | (B) FreeBlk layer0 | block 13 free block table
180 * | (B) FreeBlk layer1 |
181 * | (B) FreeBlk layer2 |
182 * | (B) FreeBlk layer3 |
183 * | (B) FreeBlk layer4[8] |
184 * +-----------------------+
185 * | (C) FreeBlk layer0 | block 25 free block table
186 * | (C) FreeBlk layer1 |
187 * | (C) FreeBlk layer2 |
188 * | (C) FreeBlk layer3 |
189 * | (C) FreeBlk layer4[8] |
190 * +-----------------------+
191 * | (D) FreeBlk layer0 | block 37 free block table
192 * | (D) FreeBlk layer1 |
193 * | (D) FreeBlk layer2 |
194 * | (D) FreeBlk layer3 |
195 * | (D) FreeBlk layer4[8] |
196 * +-----------------------+
200 * +-----------------------+
202 * The first few 2GB zones contain volume headers and volume header backups.
203 * After that the volume header block# is reserved. The first 2GB zone
204 * contains all four FreeBlk layers, for example, but the layer1 FreeBlk
205 * is only needed once every 1TB. The free block topology rotates between
206 * several groups {A,B,C,D} in order to ensure that the free block table
207 * is clean upon reboot after a crash or disk failure.
209 * The Free block table has a resolution of 1KB
211 #define HAMMER2_VOLUME_ALIGN (8 * 1024 * 1024)
212 #define HAMMER2_VOLUME_ALIGN64 ((hammer2_off_t)HAMMER2_VOLUME_ALIGN)
213 #define HAMMER2_VOLUME_ALIGNMASK (HAMMER2_VOLUME_ALIGN - 1)
214 #define HAMMER2_VOLUME_ALIGNMASK64 ((hammer2_off_t)HAMMER2_VOLUME_ALIGNMASK)
216 #define HAMMER2_NEWFS_ALIGN (HAMMER2_VOLUME_ALIGN)
217 #define HAMMER2_NEWFS_ALIGN64 ((hammer2_off_t)HAMMER2_VOLUME_ALIGN)
218 #define HAMMER2_NEWFS_ALIGNMASK (HAMMER2_VOLUME_ALIGN - 1)
219 #define HAMMER2_NEWFS_ALIGNMASK64 ((hammer2_off_t)HAMMER2_NEWFS_ALIGNMASK)
221 #define HAMMER2_ZONE_BYTES64 (2LLU * 1024 * 1024 * 1024)
222 #define HAMMER2_ZONE_MASK64 (HAMMER2_ZONE_BYTES64 - 1)
223 #define HAMMER2_ZONE_SEG (4 * 1024 * 1024)
224 #define HAMMER2_ZONE_SEG64 ((hammer2_off_t)HAMMER2_ZONE_SEG)
225 #define HAMMER2_ZONE_BLOCKS_SEG (HAMMER2_ZONE_SEG / HAMMER2_PBUFSIZE)
228 * 64 x 64KB blocks are reserved at the base of each 2GB zone. These blocks
229 * are used to store the volume header or volume header backups, allocation
230 * tree, and other information in the future.
232 * All specified blocks are not necessarily used in all 2GB zones. However,
233 * dead areas are reserved and MUST NOT BE USED for other purposes.
235 * The freemap is arranged into four groups. Modifications rotate through
236 * the groups on a block by block basis (so all the blocks are not necessarily
237 * synchronized to the same group). Only three groups are actually necessary
238 * (stable, flushing, modifying).
240 * 64KB freemap indirect blocks are represented by layers 0, 1, 2, and 3.
241 * 4KB freemap leaf blocks each represent 16MB of storage so 128 x 4KB are
242 * needed per zone, which equates to 8 x 64KB layer4 blocks per zone.
244 #define HAMMER2_ZONE_VOLHDR 0 /* volume header or backup */
245 #define HAMMER2_ZONE_FREEMAP_A 1 /* freemap layer group A */
246 #define HAMMER2_ZONE_FREEMAP_B 13 /* freemap layer group B */
247 #define HAMMER2_ZONE_FREEMAP_C 25 /* freemap layer group C */
248 #define HAMMER2_ZONE_FREEMAP_D 37 /* freemap layer group D */
250 #define HAMMER2_ZONEFM_LAYER0 0 /* relative to FREEMAP_x */
251 #define HAMMER2_ZONEFM_LAYER1 1
252 #define HAMMER2_ZONEFM_LAYER2 2
253 #define HAMMER2_ZONEFM_LAYER3 3
254 #define HAMMER2_ZONEFM_LAYER4 4 /* 4-11 (8 64KB blocks) */
256 #define HAMMER2_ZONE_BLOCK49 49 /* future */
257 #define HAMMER2_ZONE_BLOCK50 50 /* future */
258 #define HAMMER2_ZONE_BLOCK51 51 /* future */
259 #define HAMMER2_ZONE_BLOCK52 52 /* future */
260 #define HAMMER2_ZONE_BLOCK53 53 /* future */
261 #define HAMMER2_ZONE_BLOCK54 54 /* future */
262 #define HAMMER2_ZONE_BLOCK55 55 /* future */
263 #define HAMMER2_ZONE_BLOCK56 56 /* future */
264 #define HAMMER2_ZONE_BLOCK57 57 /* future */
265 #define HAMMER2_ZONE_BLOCK58 58 /* future */
266 #define HAMMER2_ZONE_BLOCK59 59 /* future */
268 #define HAMMER2_ZONE_BLOCK60 60 /* future */
269 #define HAMMER2_ZONE_BLOCK61 61 /* future */
270 #define HAMMER2_ZONE_BLOCK62 62 /* future */
271 #define HAMMER2_ZONE_BLOCK63 63 /* future */
274 * Two linear areas can be reserved after the initial 2MB segment in the base
275 * zone (the one starting at offset 0). These areas are NOT managed by the
276 * block allocator and do not fall under HAMMER2 crc checking rules based
277 * at the volume header (but can be self-CRCd internally, depending).
279 #define HAMMER2_BOOT_MIN_BYTES HAMMER2_VOLUME_ALIGN
280 #define HAMMER2_BOOT_NOM_BYTES (64*1024*1024)
281 #define HAMMER2_BOOT_MAX_BYTES (256*1024*1024)
283 #define HAMMER2_REDO_MIN_BYTES HAMMER2_VOLUME_ALIGN
284 #define HAMMER2_REDO_NOM_BYTES (256*1024*1024)
285 #define HAMMER2_REDO_MAX_BYTES (1024*1024*1024)
288 * Most HAMMER2 types are implemented as unsigned 64-bit integers.
289 * Transaction ids are monotonic.
291 * We utilize 32-bit iSCSI CRCs.
293 typedef uint64_t hammer2_tid_t;
294 typedef uint64_t hammer2_off_t;
295 typedef uint64_t hammer2_key_t;
296 typedef uint32_t hammer2_crc32_t;
299 * Miscellanious ranges (all are unsigned).
301 #define HAMMER2_MIN_TID 1ULL
302 #define HAMMER2_MAX_TID 0xFFFFFFFFFFFFFFFFULL
303 #define HAMMER2_MIN_KEY 0ULL
304 #define HAMMER2_MAX_KEY 0xFFFFFFFFFFFFFFFFULL
305 #define HAMMER2_MIN_OFFSET 0ULL
306 #define HAMMER2_MAX_OFFSET 0xFFFFFFFFFFFFFFFFULL
309 * HAMMER2 data offset special cases and masking.
311 * All HAMMER2 data offsets have to be broken down into a 64K buffer base
312 * offset (HAMMER2_OFF_MASK_HI) and a 64K buffer index (HAMMER2_OFF_MASK_LO).
314 * Indexes into physical buffers are always 64-byte aligned. The low 6 bits
315 * of the data offset field specifies how large the data chunk being pointed
316 * to as a power of 2. The theoretical minimum radix is thus 6 (The space
317 * needed in the low bits of the data offset field). However, the practical
318 * minimum allocation chunk size is 1KB (a radix of 10), so HAMMER2 sets
319 * HAMMER2_MIN_RADIX to 10. The maximum radix is currently 16 (64KB), but
320 * we fully intend to support larger extents in the future.
322 #define HAMMER2_OFF_BAD ((hammer2_off_t)-1)
323 #define HAMMER2_OFF_MASK 0xFFFFFFFFFFFFFFC0ULL
324 #define HAMMER2_OFF_MASK_LO (HAMMER2_OFF_MASK & HAMMER2_PBUFMASK64)
325 #define HAMMER2_OFF_MASK_HI (~HAMMER2_PBUFMASK64)
326 #define HAMMER2_OFF_MASK_RADIX 0x000000000000003FULL
327 #define HAMMER2_MAX_COPIES 6
330 * HAMMER2 directory support and pre-defined keys
332 #define HAMMER2_DIRHASH_VISIBLE 0x8000000000000000ULL
333 #define HAMMER2_DIRHASH_USERMSK 0x7FFFFFFFFFFFFFFFULL
334 #define HAMMER2_DIRHASH_LOMASK 0x0000000000007FFFULL
335 #define HAMMER2_DIRHASH_HIMASK 0xFFFFFFFFFFFF0000ULL
336 #define HAMMER2_DIRHASH_FORCED 0x0000000000008000ULL /* bit forced on */
338 #define HAMMER2_SROOT_KEY 0x0000000000000000ULL /* volume to sroot */
341 * The media block reference structure. This forms the core of the HAMMER2
342 * media topology recursion. This 64-byte data structure is embedded in the
343 * volume header, in inodes (which are also directory entries), and in
346 * A blockref references a single media item, which typically can be a
347 * directory entry (aka inode), indirect block, or data block.
349 * The primary feature a blockref represents is the ability to validate
350 * the entire tree underneath it via its check code. Any modification to
351 * anything propagates up the blockref tree all the way to the root, replacing
352 * the related blocks. Propagations can shortcut to the volume root to
353 * implement the 'fast syncing' feature but this only delays the eventual
356 * The check code can be a simple 32-bit iscsi code, a 64-bit crc,
357 * or as complex as a 192 bit cryptographic hash. 192 bits is the maximum
358 * supported check code size, which is not sufficient for unverified dedup
359 * UNLESS one doesn't mind once-in-a-blue-moon data corruption (such as when
360 * farming web data). HAMMER2 has an unverified dedup feature for just this
365 * NOTE: The range of keys represented by the blockref is (key) to
366 * ((key) + (1LL << keybits) - 1). HAMMER2 usually populates
367 * blocks bottom-up, inserting a new root when radix expansion
370 struct hammer2_blockref { /* MUST BE EXACTLY 64 BYTES */
371 uint8_t type; /* type of underlying item */
372 uint8_t methods; /* check method & compression method */
373 uint8_t copyid; /* specify which copy this is */
374 uint8_t keybits; /* #of keybits masked off 0=leaf */
375 uint8_t vradix; /* virtual data/meta-data size */
376 uint8_t flags; /* blockref flags */
379 hammer2_key_t key; /* key specification */
380 hammer2_tid_t mirror_tid; /* propagate for mirror scan */
381 hammer2_tid_t modify_tid; /* modifications sans propagation */
382 hammer2_off_t data_off; /* low 6 bits is phys size (radix)*/
383 union { /* check info */
398 * Freemap hints are embedded in addition to the icrc32.
400 * biggest - largest possible allocation 2^N within sub-tree.
401 * typically initialized to 64 in freemap_blockref
402 * and to 54 in each blockref[] entry in the
403 * FREEMAP_ROOT indirect block.
405 * An allocation > 2^N is guaranteed to fail. An
406 * allocation <= 2^N MAY fail, and if it does the
407 * biggest hint will be adjusted downward.
409 * Used when allocating space.
417 uint64_t avail; /* total available bytes */
418 uint64_t unused; /* unused must be 0 */
423 typedef struct hammer2_blockref hammer2_blockref_t;
426 #define HAMMER2_BREF_SYNC1 0x01 /* modification synchronized */
427 #define HAMMER2_BREF_SYNC2 0x02 /* modification committed */
428 #define HAMMER2_BREF_DESYNCCHLD 0x04 /* desynchronize children */
429 #define HAMMER2_BREF_DELETED 0x80 /* indicates a deletion */
432 #define HAMMER2_BLOCKREF_BYTES 64 /* blockref struct in bytes */
434 #define HAMMER2_BREF_TYPE_EMPTY 0
435 #define HAMMER2_BREF_TYPE_INODE 1
436 #define HAMMER2_BREF_TYPE_INDIRECT 2
437 #define HAMMER2_BREF_TYPE_DATA 3
438 #define HAMMER2_BREF_TYPE_FREEMAP_ROOT 4
439 #define HAMMER2_BREF_TYPE_FREEMAP_NODE 5
440 #define HAMMER2_BREF_TYPE_FREEMAP_LEAF 6
441 #define HAMMER2_BREF_TYPE_VOLUME 255 /* pseudo-type */
443 #define HAMMER2_ENC_CHECK(n) ((n) << 4)
444 #define HAMMER2_DEC_CHECK(n) (((n) >> 4) & 15)
446 #define HAMMER2_CHECK_NONE 0
447 #define HAMMER2_CHECK_ISCSI32 1
448 #define HAMMER2_CHECK_CRC64 2
449 #define HAMMER2_CHECK_SHA192 3
450 #define HAMMER2_CHECK_FREEMAP 4
452 #define HAMMER2_ENC_COMP(n) (n)
453 #define HAMMER2_DEC_COMP(n) ((n) & 15)
455 #define HAMMER2_COMP_NONE 0
456 #define HAMMER2_COMP_AUTOZERO 1
460 * HAMMER2 block references are collected into sets of 8 blockrefs. These
461 * sets are fully associative, meaning the elements making up a set are
462 * not sorted in any way and may contain duplicate entries, holes, or
463 * entries which shortcut multiple levels of indirection. Sets are used
466 * (1) When redundancy is desired a set may contain several duplicate
467 * entries pointing to different copies of the same data. Up to 8 copies
468 * are supported but the set structure becomes a bit inefficient once
471 * (2) The blockrefs in a set can shortcut multiple levels of indirections
472 * within the bounds imposed by the parent of set.
474 * When a set fills up another level of indirection is inserted, moving
475 * some or all of the set's contents into indirect blocks placed under the
476 * set. This is a top-down approach in that indirect blocks are not created
477 * until the set actually becomes full (that is, the entries in the set can
478 * shortcut the indirect blocks when the set is not full). Depending on how
479 * things are filled multiple indirect blocks will eventually be created.
481 * Indirect blocks are typically 4KB (64 entres) or 64KB (1024 entries) and
482 * are also treated as fully set-associative.
484 struct hammer2_blockset {
485 hammer2_blockref_t blockref[HAMMER2_SET_COUNT];
488 typedef struct hammer2_blockset hammer2_blockset_t;
491 * Catch programmer snafus
493 #if (1 << HAMMER2_SET_RADIX) != HAMMER2_SET_COUNT
494 #error "hammer2 direct radix is incorrect"
496 #if (1 << HAMMER2_PBUFRADIX) != HAMMER2_PBUFSIZE
497 #error "HAMMER2_PBUFRADIX and HAMMER2_PBUFSIZE are inconsistent"
499 #if (1 << HAMMER2_MIN_RADIX) != HAMMER2_MIN_ALLOC
500 #error "HAMMER2_MIN_RADIX and HAMMER2_MIN_ALLOC are inconsistent"
504 * The media indirect block structure.
506 struct hammer2_indblock_data {
507 hammer2_blockref_t blockref[HAMMER2_IND_COUNT_MAX];
510 typedef struct hammer2_indblock_data hammer2_indblock_data_t;
513 * In HAMMER2 inodes ARE directory entries, with a special exception for
514 * hardlinks. The inode number is stored in the inode rather than being
515 * based on the location of the inode (since the location moves every time
516 * the inode or anything underneath the inode is modified).
518 * The inode is 1024 bytes, made up of 256 bytes of meta-data, 256 bytes
519 * for the filename, and 512 bytes worth of direct file data OR an embedded
522 * Directories represent one inode per blockref. Inodes are not laid out
523 * as a file but instead are represented by the related blockrefs. The
524 * blockrefs, in turn, are indexed by the 64-bit directory hash key. Remember
525 * that blocksets are fully associative, so a certain degree efficiency is
526 * achieved just from that.
528 * Up to 512 bytes of direct data can be embedded in an inode, and since
529 * inodes are essentially directory entries this also means that small data
530 * files end up simply being laid out linearly in the directory, resulting
531 * in fewer seeks and highly optimal access.
533 * The compression mode can be changed at any time in the inode and is
534 * recorded on a blockref-by-blockref basis.
536 * Hardlinks are supported via the inode map. Essentially the way a hardlink
537 * works is that all individual directory entries representing the same file
538 * are special cased and specify the same inode number. The actual file
539 * is placed in the nearest parent directory that is parent to all instances
540 * of the hardlink. If all hardlinks to a file are in the same directory
541 * the actual file will also be placed in that directory. This file uses
542 * the inode number as the directory entry key and is invisible to normal
543 * directory scans. Real directory entry keys are differentiated from the
544 * inode number key via bit 63. Access to the hardlink silently looks up
545 * the real file and forwards all operations to that file. Removal of the
546 * last hardlink also removes the real file.
548 * (attr_tid) is only updated when the inode's specific attributes or regular
549 * file size has changed, and affects path lookups and stat. (attr_tid)
550 * represents a special cache coherency lock under the inode. The inode
551 * blockref's modify_tid will always cover it.
553 * (dirent_tid) is only updated when an entry under a directory inode has
554 * been created, deleted, renamed, or had its attributes change, and affects
555 * directory lookups and scans. (dirent_tid) represents another special cache
556 * coherency lock under the inode. The inode blockref's modify_tid will
559 #define HAMMER2_INODE_BYTES 1024 /* (asserted by code) */
560 #define HAMMER2_INODE_MAXNAME 256 /* maximum name in bytes */
561 #define HAMMER2_INODE_VERSION_ONE 1
563 struct hammer2_inode_data {
564 uint16_t version; /* 0000 inode data version */
565 uint16_t reserved02; /* 0002 */
568 * core inode attributes, inode type, misc flags
570 uint32_t uflags; /* 0004 chflags */
571 uint32_t rmajor; /* 0008 available for device nodes */
572 uint32_t rminor; /* 000C available for device nodes */
573 uint64_t ctime; /* 0010 inode change time */
574 uint64_t mtime; /* 0018 modified time */
575 uint64_t atime; /* 0020 access time (unsupported) */
576 uint64_t btime; /* 0028 birth time */
577 uuid_t uid; /* 0030 uid / degenerate unix uid */
578 uuid_t gid; /* 0040 gid / degenerate unix gid */
580 uint8_t type; /* 0050 object type */
581 uint8_t op_flags; /* 0051 operational flags */
582 uint16_t cap_flags; /* 0052 capability flags */
583 uint32_t mode; /* 0054 unix modes (typ low 16 bits) */
586 * inode size, identification, localized recursive configuration
587 * for compression and backup copies.
589 hammer2_tid_t inum; /* 0058 inode number */
590 hammer2_off_t size; /* 0060 size of file */
591 uint64_t nlinks; /* 0068 hard links (typ only dirs) */
592 hammer2_tid_t iparent; /* 0070 parent inum (recovery only) */
593 hammer2_key_t name_key; /* 0078 full filename key */
594 uint16_t name_len; /* 0080 filename length */
595 uint8_t ncopies; /* 0082 ncopies to local media */
596 uint8_t comp_algo; /* 0083 compression request & algo */
599 * These fields are currently only applicable to PFSROOTs.
601 * NOTE: We can't use {volume_data->fsid, pfs_clid} to uniquely
602 * identify an instance of a PFS in the cluster because
603 * a mount may contain more than one copy of the PFS as
604 * a separate node. {pfs_clid, pfs_fsid} must be used for
605 * registration in the cluster.
607 uint8_t target_type; /* 0084 hardlink target type */
608 uint8_t reserved85; /* 0085 */
609 uint8_t reserved86; /* 0086 */
610 uint8_t pfs_type; /* 0087 (if PFSROOT) node type */
611 uint64_t pfs_inum; /* 0088 (if PFSROOT) inum allocator */
612 uuid_t pfs_clid; /* 0090 (if PFSROOT) cluster uuid */
613 uuid_t pfs_fsid; /* 00A0 (if PFSROOT) unique uuid */
616 * Quotas and cumulative sub-tree counters.
618 hammer2_off_t data_quota; /* 00B0 subtree quota in bytes */
619 hammer2_off_t data_count; /* 00B8 subtree byte count */
620 hammer2_off_t inode_quota; /* 00C0 subtree quota inode count */
621 hammer2_off_t inode_count; /* 00C8 subtree inode count */
622 hammer2_tid_t attr_tid; /* 00D0 attributes changed */
623 hammer2_tid_t dirent_tid; /* 00D8 directory/attr changed */
624 uint64_t reservedE0; /* 00E0 */
625 uint64_t reservedE8; /* 00E8 */
626 uint64_t reservedF0; /* 00F0 */
627 uint64_t reservedF8; /* 00F8 */
629 unsigned char filename[HAMMER2_INODE_MAXNAME];
630 /* 0100-01FF (256 char, unterminated) */
631 union { /* 0200-03FF (64x8 = 512 bytes) */
632 struct hammer2_blockset blockset;
633 char data[HAMMER2_EMBEDDED_BYTES];
637 typedef struct hammer2_inode_data hammer2_inode_data_t;
639 #define HAMMER2_OPFLAG_DIRECTDATA 0x01
640 #define HAMMER2_OPFLAG_PFSROOT 0x02
641 #define HAMMER2_OPFLAG_COPYIDS 0x04 /* copyids override parent */
643 #define HAMMER2_OBJTYPE_UNKNOWN 0
644 #define HAMMER2_OBJTYPE_DIRECTORY 1
645 #define HAMMER2_OBJTYPE_REGFILE 2
646 #define HAMMER2_OBJTYPE_FIFO 4
647 #define HAMMER2_OBJTYPE_CDEV 5
648 #define HAMMER2_OBJTYPE_BDEV 6
649 #define HAMMER2_OBJTYPE_SOFTLINK 7
650 #define HAMMER2_OBJTYPE_HARDLINK 8 /* dummy entry for hardlink */
651 #define HAMMER2_OBJTYPE_SOCKET 9
652 #define HAMMER2_OBJTYPE_WHITEOUT 10
654 #define HAMMER2_COPYID_NONE 0
655 #define HAMMER2_COPYID_LOCAL ((uint8_t)-1)
658 * PEER types identify connections and help cluster controller filter
659 * out unwanted SPANs.
661 #define HAMMER2_PEER_NONE DMSG_PEER_NONE
662 #define HAMMER2_PEER_CLUSTER DMSG_PEER_CLUSTER
663 #define HAMMER2_PEER_BLOCK DMSG_PEER_BLOCK
664 #define HAMMER2_PEER_HAMMER2 DMSG_PEER_HAMMER2
666 #define HAMMER2_COPYID_COUNT DMSG_COPYID_COUNT
669 * PFS types identify a PFS on media and in LNK_SPAN messages.
671 #define HAMMER2_PFSTYPE_NONE DMSG_PFSTYPE_NONE
672 #define HAMMER2_PFSTYPE_ADMIN DMSG_PFSTYPE_ADMIN
673 #define HAMMER2_PFSTYPE_CLIENT DMSG_PFSTYPE_CLIENT
674 #define HAMMER2_PFSTYPE_CACHE DMSG_PFSTYPE_CACHE
675 #define HAMMER2_PFSTYPE_COPY DMSG_PFSTYPE_COPY
676 #define HAMMER2_PFSTYPE_SLAVE DMSG_PFSTYPE_SLAVE
677 #define HAMMER2_PFSTYPE_SOFT_SLAVE DMSG_PFSTYPE_SOFT_SLAVE
678 #define HAMMER2_PFSTYPE_SOFT_MASTER DMSG_PFSTYPE_SOFT_MASTER
679 #define HAMMER2_PFSTYPE_MASTER DMSG_PFSTYPE_MASTER
680 #define HAMMER2_PFSTYPE_MAX DMSG_PFSTYPE_MAX
685 * In HAMMER2 the allocation table hangs off of the volume header and
686 * utilizes somewhat customized hammer2_blockref based indirect blocks
687 * until hitting the leaf bitmap. BREF_TYPE_FREEMAP_ROOT and
688 * BREF_TYPE_FREEMAP_NODE represent the indirect blocks but are formatted
689 * the same as BREF_TYPE_INDIRECT except for the (biggest) and (avail)
690 * fields which use some of the check union space. Thus a special CHECK
691 * id (CHECK_FREEMAP instead of CHECK_ISCSI32) is also specified for these
694 * newfs_hammer2 builds the FREEMAP_ROOT block and assigns a radix of
695 * 34, 44, 54, or 64 depending on whether the freemap is to be fitted
696 * to the storage or is to maximized for (possibly) sparse storage.
697 * Other keybits specifications for FREEMAP_ROOT are illegal. Even fitted
698 * storage is required to specify at least a keybits value of 34.
700 * Total possible representation is 2^64 (16 Exabytes).
701 * 10: 1024 entries / 64KB 16EB (16PB per entry) layer0
702 * 10: 1024 entries / 64KB 16PB (16TB per entry) layer1
703 * 10: 1024 entries / 64KB 16TB (16GB per entry) layer2
704 * 10: 1024 entries / 64KB 16GB (16MB per entry) layer3
705 * 24: 16384 x 1KB allocgran / 4KB 16MB layer4
707 * To make the radix come out to exactly 64 the leaf bitmaps are arranged
708 * into 4KB buffers, with each buffer representing a freemap for 16MB worth
709 * of storage using a 1KB allocation granularity. The leaf bitmaps are
710 * structures and not just a plain bitmap, hence the extra space needed to
711 * represent 16384 x 1KB blocks.
713 * The reserved area at the beginning of each 2GB zone is marked as being
714 * allocated on-the-fly and does not have to be pre-set in the freemap,
715 * which is just as well as that would require newfs_hammer2 to do a lot
716 * of writing otherwise.
718 * Indirect blocks are usually created with a semi-dynamic radix but in the
719 * case of freemap-related indirect blocks, the blocks use a static radix
720 * tree with associations to specific reserved blocks.
724 * 4KB -> hammer2_freemap_elm[256]
726 * bitmap - 64 bits x 1KB representing 64KB. A '1' bit represents
727 * an allocated block.
729 * generation - Incremented upon any allocation. Can't increment more
730 * than +64 per background freeing pass due to there being
733 * biggest0 - biggest hint (radix) for freemap_elm. Represents up to
736 * biggest1 - biggest hint (radix) for aligned groups of 16 elements,
737 * stored in elm[0], elm[16], etc. Represents up to 1MB.
740 * biggest2 - biggest hint (radix) for aligned groups of 256 elements
741 * (i.e. the whole array, only used by elm[0]).
742 * Represents up to 16MB (radix 24).
744 * The hinting is used as part of the allocation mechanism to reduce scan
745 * time, which is particularly important as a filesystem approaches full.
746 * Fill ratios are handled at the indirect block level (in the blockrefs) and
749 struct hammer2_freemap_elm {
758 typedef struct hammer2_freemap_elm hammer2_freemap_elm_t;
760 #define HAMMER2_FREEMAP_LEAF_BYTES 4096
761 #define HAMMER2_FREEMAP_LEAF_ENTRIES (HAMMER2_FREEMAP_LEAF_BYTES / \
762 sizeof(hammer2_freemap_elm_t))
763 #define HAMMER2_FREEMAP_LEAF_RADIX 24
764 #define HAMMER2_FREEMAP_NODE_RADIX 10
765 #define HAMMER2_FREEMAP_ELM_RADIX 5 /* 2^5 == 32 bits */
767 #define HAMMER2_BIGF_KILLED 0x80
770 * Flags (8 bits) - blockref, for freemap only
772 * Note that the minimum chunk size is 1KB so we could theoretically have
773 * 10 bits here, but we might have some future extension that allows a
774 * chunk size down to 256 bytes and if so we will need bits 8 and 9.
776 #define HAMMER2_AVF_SELMASK 0x03 /* select group */
777 #define HAMMER2_AVF_ALL_ALLOC 0x04 /* indicate all allocated */
778 #define HAMMER2_AVF_ALL_FREE 0x08 /* indicate all free */
779 #define HAMMER2_AVF_RESERVED10 0x10
780 #define HAMMER2_AVF_RESERVED20 0x20
781 #define HAMMER2_AVF_RESERVED40 0x40
782 #define HAMMER2_AVF_RESERVED80 0x80
783 #define HAMMER2_AVF_AVMASK32 ((uint32_t)0xFFFFFF00LU)
784 #define HAMMER2_AVF_AVMASK64 ((uint64_t)0xFFFFFFFFFFFFFF00LLU)
786 #define HAMMER2_AV_SELECT_A 0x00
787 #define HAMMER2_AV_SELECT_B 0x01
788 #define HAMMER2_AV_SELECT_C 0x02
789 #define HAMMER2_AV_SELECT_D 0x03
792 * The volume header eats a 64K block. There is currently an issue where
793 * we want to try to fit all nominal filesystem updates in a 512-byte section
794 * but it may be a lost cause due to the need for a blockset.
796 * All information is stored in host byte order. The volume header's magic
797 * number may be checked to determine the byte order. If you wish to mount
798 * between machines w/ different endian modes you'll need filesystem code
799 * which acts on the media data consistently (either all one way or all the
800 * other). Our code currently does not do that.
802 * A read-write mount may have to recover missing allocations by doing an
803 * incremental mirror scan looking for modifications made after alloc_tid.
804 * If alloc_tid == last_tid then no recovery operation is needed. Recovery
805 * operations are usually very, very fast.
807 * Read-only mounts do not need to do any recovery, access to the filesystem
808 * topology is always consistent after a crash (is always consistent, period).
809 * However, there may be shortcutted blockref updates present from deep in
810 * the tree which are stored in the volumeh eader and must be tracked on
813 * NOTE: The copyinfo[] array contains the configuration for both the
814 * cluster connections and any local media copies. The volume
815 * header will be replicated for each local media copy.
817 * The mount command may specify multiple medias or just one and
818 * allow HAMMER2 to pick up the others when it checks the copyinfo[]
821 * NOTE: root_blockref points to the super-root directory, not the root
822 * directory. The root directory will be a subdirectory under the
825 * The super-root directory contains all root directories and all
826 * snapshots (readonly or writable). It is possible to do a
827 * null-mount of the super-root using special path constructions
828 * relative to your mounted root.
830 * NOTE: HAMMER2 allows any subdirectory tree to be managed as if it were
831 * a PFS, including mirroring and storage quota operations, and this is
832 * prefered over creating discrete PFSs in the super-root. Instead
833 * the super-root is most typically used to create writable snapshots,
834 * alternative roots, and so forth. The super-root is also used by
835 * the automatic snapshotting mechanism.
837 #define HAMMER2_VOLUME_ID_HBO 0x48414d3205172011LLU
838 #define HAMMER2_VOLUME_ID_ABO 0x11201705324d4148LLU
840 struct hammer2_volume_data {
842 * sector #0 - 512 bytes
844 uint64_t magic; /* 0000 Signature */
845 hammer2_off_t boot_beg; /* 0008 Boot area (future) */
846 hammer2_off_t boot_end; /* 0010 (size = end - beg) */
847 hammer2_off_t aux_beg; /* 0018 Aux area (future) */
848 hammer2_off_t aux_end; /* 0020 (size = end - beg) */
849 hammer2_off_t volu_size; /* 0028 Volume size, bytes */
851 uint32_t version; /* 0030 */
852 uint32_t flags; /* 0034 */
853 uint8_t copyid; /* 0038 copyid of phys vol */
854 uint8_t freemap_version; /* 0039 freemap algorithm */
855 uint8_t peer_type; /* 003A HAMMER2_PEER_xxx */
856 uint8_t reserved003B; /* 003B */
857 uint32_t reserved003C; /* 003C */
859 uuid_t fsid; /* 0040 */
860 uuid_t fstype; /* 0050 */
863 * allocator_size is precalculated at newfs time and does not include
864 * reserved blocks, boot, or redo areas.
866 * Initial non-reserved-area allocations do not use the freemap
867 * but instead adjust alloc_iterator. Dynamic allocations take
868 * over starting at (allocator_beg). This makes newfs_hammer2's
869 * job a lot easier and can also serve as a testing jig.
871 hammer2_off_t allocator_size; /* 0060 Total data space */
872 hammer2_off_t allocator_free; /* 0068 Free space */
873 hammer2_off_t allocator_beg; /* 0070 Initial allocations */
874 hammer2_tid_t mirror_tid; /* 0078 best committed tid */
875 hammer2_tid_t alloc_tid; /* 0080 Alloctable modify tid */
876 hammer2_blockref_t freemap_blockref; /* 0088-00C7 */
879 * Copyids are allocated dynamically from the copyexists bitmap.
880 * An id from the active copies set (up to 8, see copyinfo later on)
881 * may still exist after the copy set has been removed from the
882 * volume header and its bit will remain active in the bitmap and
883 * cannot be reused until it is 100% removed from the hierarchy.
885 uint32_t copyexists[8]; /* 00C8-00E7 copy exists bmap */
886 char reserved0140[248]; /* 00E8-01DF */
889 * 32 bit CRC array at the end of the first 512 byte sector.
891 * icrc_sects[7] - First 512-4 bytes of volume header (including all
892 * the other icrc's except the last one).
894 * icrc_sects[6] - Second 512-4 bytes of volume header, which is
895 * the blockset for the root.
897 hammer2_crc32_t icrc_sects[8]; /* 01E0-01FF */
900 * sector #1 - 512 bytes
902 * The entire sector is used by a blockset.
904 hammer2_blockset_t sroot_blockset; /* 0200-03FF Superroot dir */
909 char sector2[512]; /* 0400-05FF reserved */
910 char sector3[512]; /* 0600-07FF reserved */
911 char sector4[512]; /* 0800-09FF reserved */
912 char sector5[512]; /* 0A00-0BFF reserved */
913 char sector6[512]; /* 0C00-0DFF reserved */
914 char sector7[512]; /* 0E00-0FFF reserved */
917 * sector #8-71 - 32768 bytes
919 * Contains the configuration for up to 256 copyinfo targets. These
920 * specify local and remote copies operating as masters or slaves.
921 * copyid's 0 and 255 are reserved (0 indicates an empty slot and 255
922 * indicates the local media).
924 * Each inode contains a set of up to 8 copyids, either inherited
925 * from its parent or explicitly specified in the inode, which
926 * indexes into this array.
928 /* 1000-8FFF copyinfo config */
929 dmsg_vol_data_t copyinfo[HAMMER2_COPYID_COUNT];
932 * Remaining sections are reserved for future use.
934 char reserved0400[0x6FFC]; /* 9000-FFFB reserved */
937 * icrc on entire volume header
939 hammer2_crc32_t icrc_volheader; /* FFFC-FFFF full volume icrc*/
942 typedef struct hammer2_volume_data hammer2_volume_data_t;
945 * Various parts of the volume header have their own iCRCs.
947 * The first 512 bytes has its own iCRC stored at the end of the 512 bytes
948 * and not included the icrc calculation.
950 * The second 512 bytes also has its own iCRC but it is stored in the first
951 * 512 bytes so it covers the entire second 512 bytes.
953 * The whole volume block (64KB) has an iCRC covering all but the last 4 bytes,
954 * which is where the iCRC for the whole volume is stored. This is currently
955 * a catch-all for anything not individually iCRCd.
957 #define HAMMER2_VOL_ICRC_SECT0 7
958 #define HAMMER2_VOL_ICRC_SECT1 6
960 #define HAMMER2_VOLUME_BYTES 65536
962 #define HAMMER2_VOLUME_ICRC0_OFF 0
963 #define HAMMER2_VOLUME_ICRC1_OFF 512
964 #define HAMMER2_VOLUME_ICRCVH_OFF 0
966 #define HAMMER2_VOLUME_ICRC0_SIZE (512 - 4)
967 #define HAMMER2_VOLUME_ICRC1_SIZE (512)
968 #define HAMMER2_VOLUME_ICRCVH_SIZE (65536 - 4)
970 #define HAMMER2_VOL_VERSION_MIN 1
971 #define HAMMER2_VOL_VERSION_DEFAULT 1
972 #define HAMMER2_VOL_VERSION_WIP 2
974 #define HAMMER2_NUM_VOLHDRS 4
976 union hammer2_media_data {
977 hammer2_volume_data_t voldata;
978 hammer2_inode_data_t ipdata;
979 hammer2_indblock_data_t npdata;
980 char buf[HAMMER2_PBUFSIZE];
983 typedef union hammer2_media_data hammer2_media_data_t;