hammer2 - More media format spec work, add DESIGN document
[dragonfly.git] / sys / vfs / hammer2 / hammer2_disk.h
1 /*
2  * Copyright (c) 2011-2012 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@dragonflybsd.org>
6  * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  * 3. Neither the name of The DragonFly Project nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific, prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
26  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 #ifndef VFS_HAMMER2_DISK_H_
36 #define VFS_HAMMER2_DISK_H_
37
38 /*
39  * The structures below represent the on-disk media structures for the HAMMER2
40  * filesystem.  Note that all fields for on-disk structures are naturally
41  * aligned.  The host endian format is typically used - compatibility is
42  * possible if the implementation detects reversed endian and adjusts accesses
43  * accordingly.
44  *
45  * HAMMER2 primarily revolves around the directory topology:  inodes,
46  * directory entries, and block tables.  Block device buffer cache buffers
47  * are always 64KB.  Logical file buffers are typically 16KB.  All data
48  * references utilize 64-bit byte offsets.
49  *
50  * Free block management is handled independently using blocks reserved by
51  * the media topology.
52  */
53
54 /*
55  * The data at the end of a file or directory may be a fragment in order
56  * to optimize storage efficiency.  The minimum fragment size is 64 bytes.
57  * Since allocations are in powers of 2 fragments must also be sized in
58  * powers of 2 (64, 128, 256, ... 65536).
59  *
60  * For the moment the maximum allocation size is HAMMER2_PBUFSIZE (64K),
61  * which is 2^16.  Larger extents may be supported in the future.
62  *
63  * A full indirect block uses supports 1024 x 64-byte blockrefs.
64  *
65  * A maximally sized file (2^64-1 bytes) requires 5 indirect block levels.
66  * The hammer2_blockset in the volume header or file inode has another 8
67  * entries, giving us 66+3 = 69 bits of address space.  However, some bits
68  * are taken up by (potentially) requests for redundant copies.  HAMMER2
69  * currently supports up to 8 copies, which brings the address space down
70  * to 66 bits and gives us 2 bits of leeway.
71  */
72 #define HAMMER2_MIN_ALLOC       64      /* minimum allocation size */
73 #define HAMMER2_MIN_RADIX       6       /* minimum allocation size 2^N */
74 #define HAMMER2_MAX_RADIX       16      /* maximum allocation size 2^N */
75 #define HAMMER2_IND1_RADIX      26      /* lowest full indirect block radix */
76 #define HAMMER2_IND2_RADIX      36
77 #define HAMMER2_IND3_RADIX      46
78 #define HAMMER2_IND4_RADIX      56
79 #define HAMMER2_IND5_RADIX      66      /* highest full indirect block radix */
80
81 /*
82  * HAMMER2 utilizes 64K physical buffers and 16K logical filesystem buffers.
83  * The smaller logical filesystem buffers reduce ram waste when the OS is
84  * caching lots of small files.
85  */
86 #define HAMMER2_PBUFRADIX       16      /* physical buf (1<<16) bytes */
87 #define HAMMER2_PBUFSIZE        65536   /* fixed physical device buffer size */
88 #define HAMMER2_LBUFSIZE        16384   /* vnode/logical file buffer size */
89
90 /*
91  * HAMMER2 processes blockrefs in sets of 8.  The set is fully associative,
92  * is not sorted, and may contain holes.
93  *
94  * A full indirect block supports 1024 blockrefs.
95  *
96  * An inode embeds one set of blockrefs but may also use the data area for
97  * up to 512 bytes of direct data.
98  */
99 #define HAMMER2_SET_COUNT       8       /* direct entries & associativity */
100 #define HAMMER2_SET_RADIX       3
101 #define HAMMER2_IND_COUNT       1024    /* 1 << HAMMER2_IND_RADIX */
102 #define HAMMER2_IND_RADIX       10
103 #define HAMMER2_EMBEDDED_BYTES  512
104 #define HAMMER2_EMBEDDED_RADIX  9
105
106 #define HAMMER2_PBUFMASK        (HAMMER2_PBUFSIZE - 1)
107 #define HAMMER2_LBUFMASK        (HAMMER2_LBUFSIZE - 1)
108
109 #define HAMMER2_PBUFSIZE64      ((hammer2_off_t)HAMMER2_PBUFSIZE)
110 #define HAMMER2_PBUFMASK64      ((hammer2_off_t)HAMMER2_PBUFMASK)
111 #define HAMMER2_LBUFMASK64      ((hammer2_off_t)HAMMER2_LBUFMASK)
112
113 #define HAMMER2_UUID_STRING     "5cbb9ad1-862d-11dc-a94d-01301bb8a9f5"
114
115 /*
116  * A HAMMER2 filesystem is always sized in multiples of 8MB.
117  *
118  * A 2MB segment is reserved at the beginning of each 2GB zone.  This segment
119  * contains the volume header and the free block table.
120  */
121 #define HAMMER2_VOLUME_ALIGN            (8 * 1024 * 1024)
122 #define HAMMER2_VOLUME_ALIGN64          ((hammer2_off_t)HAMMER2_VOLUME_ALIGN)
123 #define HAMMER2_VOLUME_ALIGNMASK        (HAMMER2_VOLUME_ALIGN - 1)
124 #define HAMMER2_VOLUME_ALIGNMASK64     ((hammer2_off_t)HAMMER2_VOLUME_ALIGNMASK)
125
126 #define HAMMER2_NEWFS_ALIGN             (HAMMER2_VOLUME_ALIGN)
127 #define HAMMER2_NEWFS_ALIGN64           ((hammer2_off_t)HAMMER2_VOLUME_ALIGN)
128 #define HAMMER2_NEWFS_ALIGNMASK         (HAMMER2_VOLUME_ALIGN - 1)
129 #define HAMMER2_NEWFS_ALIGNMASK64       ((hammer2_off_t)HAMMER2_NEWFS_ALIGNMASK)
130
131 #define HAMMER2_RESERVE_BYTES64         (2LLU * 1024 * 1024 * 1024)
132 #define HAMMER2_RESERVE_MASK64          (HAMMER2_RESERVE_BYTES64 - 1)
133 #define HAMMER2_RESERVE_SEG             (2 * 1024 * 1024)
134 #define HAMMER2_RESERVE_SEG64           ((hammer2_off_t)HAMMER2_RESERVE_SEG)
135 #define HAMMER2_RESERVE_SEG_ENTRIES     (HAMMER2_RESERVE_SEG/HAMMER2_BUFSIZE)
136
137 /*
138  * Two linear areas can be reserved after the initial 2MB segment in the base
139  * zone (the one starting at offset 0).  These areas are NOT managed by the
140  * block allocator and do not fall under HAMMER2 crc checking rules based
141  * at the volume header (but can be self-CRCd internally, depending).
142  */
143 #define HAMMER2_BOOT_MIN_BYTES          HAMMER2_VOLUME_ALIGN
144 #define HAMMER2_BOOT_NOM_BYTES          (64*1024*1024)
145 #define HAMMER2_BOOT_MAX_BYTES          (256*1024*1024)
146
147 #define HAMMER2_REDO_MIN_BYTES          HAMMER2_VOLUME_ALIGN
148 #define HAMMER2_REDO_NOM_BYTES          (256*1024*1024)
149 #define HAMMER2_REDO_MAX_BYTES          (1024*1024*1024)
150
151 /*
152  * Most HAMMER2 types are implemented as unsigned 64-bit integers.
153  * Transaction ids are monotonic.
154  *
155  * We utilize 32-bit iSCSI CRCs.
156  */
157 typedef uint64_t hammer2_tid_t;
158 typedef uint64_t hammer2_off_t;
159 typedef uint64_t hammer2_key_t;
160 typedef uint32_t hammer2_crc32_t;
161
162 /*
163  * Miscellanious ranges (all are unsigned).
164  */
165 #define HAMMER2_MIN_TID         1ULL
166 #define HAMMER2_MAX_TID         0xFFFFFFFFFFFFFFFFULL
167 #define HAMMER2_MIN_KEY         0ULL
168 #define HAMMER2_MAX_KEY         0xFFFFFFFFFFFFFFFFULL
169 #define HAMMER2_MIN_OFFSET      0ULL
170 #define HAMMER2_MAX_OFFSET      0xFFFFFFFFFFFFFFFFULL
171
172 /*
173  * HAMMER2 data offset special cases and masking.
174  *
175  * All HAMMER2 data offsets have to be broken down into a 64K buffer base
176  * offset (HAMMER2_OFF_MASK_HI) and a 64K buffer index (HAMMER2_OFF_MASK_LO).
177  *
178  * Indexes into physical buffers are always 64-byte aligned.  The low 6 bits
179  * of the data offset field specifies how large the data chunk being pointed
180  * to as a power of 2.  This value typically ranges from HAMMER2_MIN_RADIX
181  * to HAMMER2_MAX_RADIX (6-16).  Larger values may be supported in the future
182  * to support file extents.
183  */
184 #define HAMMER2_OFF_BAD         ((hammer2_off_t)-1)
185 #define HAMMER2_OFF_MASK        0xFFFFFFFFFFFFFFC0ULL
186 #define HAMMER2_OFF_MASK_LO     (HAMMER2_OFF_MASK & HAMMER2_PBUFMASK64)
187 #define HAMMER2_OFF_MASK_HI     (~HAMMER2_PBUFMASK64)
188 #define HAMMER2_OFF_MASK_RADIX  0x000000000000003FULL
189 #define HAMMER2_MAX_COPIES      6
190
191 /*
192  * The media block reference structure.  This forms the core of the HAMMER2
193  * media topology recursion.  This 64-byte data structure is embedded in the
194  * volume header, in inodes (which are also directory entries), and in
195  * indirect blocks.
196  *
197  * A blockref references a single media item, which typically can be a
198  * directory entry (aka inode), indirect block, or data block.
199  *
200  * The primary feature a blockref represents is the ability to validate
201  * the entire tree underneath it via its check code.  Any modification to
202  * anything propagates up the blockref tree all the way to the root, replacing
203  * the related blocks.  Propagations can shortcut to the volume root to
204  * implement the 'fast syncing' feature but this only delays the eventual
205  * propagation.
206  *
207  * The check code can be a simple 32-bit iscsi code, a 64-bit crc,
208  * or as complex as a 192 bit cryptographic hash.  192 bits is the maximum
209  * supported check code size, which is not sufficient for unverified dedup
210  * UNLESS one doesn't mind once-in-a-blue-moon data corruption (such as when
211  * farming web data).  HAMMER2 has an unverified dedup feature for just this
212  * purpose.
213  */
214 struct hammer2_blockref {               /* MUST BE EXACTLY 64 BYTES */
215         uint8_t         type;           /* type of underlying item */
216         uint8_t         methods;        /* check method & compression method */
217         uint8_t         copyid;         /* specify which copy this is */
218         uint8_t         keybits;        /* key mask bits for recursion */
219         uint8_t         vradix;         /* virtual data/meta-data size */
220         uint8_t         flags;          /* blockref flags */
221         uint8_t         reserved06;
222         uint8_t         reserved07;
223         hammer2_key_t   key;            /* key specification */
224         hammer2_tid_t   mirror_tid;     /* propagate for mirror scan */
225         hammer2_tid_t   modify_tid;     /* modifications sans propagation */
226         hammer2_off_t   data_off;       /* low 6 bits is phys size (radix)*/
227         union {                         /* check info */
228                 char    buf[24];
229                 struct {
230                         uint32_t value;
231                         uint32_t unused[5];
232                 } iscsi32;
233                 struct {
234                         uint64_t value;
235                         uint64_t unused[2];
236                 } crc64;
237                 struct {
238                         char data[24];
239                 } sha192;
240         } check;
241 };
242
243 typedef struct hammer2_blockref hammer2_blockref_t;
244
245 #define HAMMER2_BREF_SYNC1      0x01    /* modification synchronized */
246 #define HAMMER2_BREF_SYNC2      0x02    /* modification committed */
247 #define HAMMER2_BREF_DESYNCCHLD 0x04    /* children must be desynchronized */
248 #define HAMMER2_BREF_DELETED    0x80    /* indicates a deletion */
249
250 #define HAMMER2_BLOCKREF_BYTES          64      /* blockref struct in bytes */
251 #define HAMMER2_ENC_COMPMETHOD(n)       (n)
252 #define HAMMER2_ENC_CHECKMETHOD(n)      ((n) << 4)
253 #define HAMMER2_DEC_COMPMETHOD(n)       ((n) & 15)
254 #define HAMMER2_DEC_CHECKMETHOD(n)      (((n) >> 4) & 15)
255
256 /*
257  * HAMMER2 block references are collected into sets of 8 blockrefs.  These
258  * sets are fully associative, meaning the elements making up a set are
259  * not sorted in any way and may contain duplicate entries, holes, or
260  * entries which shortcut multiple levels of indirection.  Sets are used
261  * in various ways:
262  *
263  * (1) When redundancy is desired a set may contain several duplicate
264  *     entries pointing to different copies of the same data.  Up to 8 copies
265  *     are supported but the set structure becomes a bit inefficient once
266  *     you go over 4.
267  *
268  * (2) The blockrefs in a set can shortcut multiple levels of indirections
269  *     within the bounds imposed by the parent of set.
270  *
271  * When a set fills up another level of indirection is inserted, moving
272  * some or all of the set's contents into indirect blocks placed under the
273  * set.  This is a top-down approach in that indirect blocks are not created
274  * until the set actually becomes full (that is, the entries in the set can
275  * shortcut the indirect blocks when the set is not full).  Depending on how
276  * things are filled multiple indirect blocks will eventually be created.
277  */
278 struct hammer2_blockset {
279         hammer2_blockref_t      refs[HAMMER2_SET_COUNT];
280 };
281
282 /*
283  * Catch programmer snafus
284  */
285 #if (1 << HAMMER2_IND_RADIX) != HAMMER2_IND_COUNT
286 #error "hammer2 indirect radix is incorrect"
287 #endif
288 #if (HAMMER2_IND_COUNT * 64) != HAMMER2_BUFSIZE
289 #error "hammer2 indirect entries is incorrect"
290 #endif
291 #if (1 << HAMMER2_SET_RADIX) != HAMMER2_SET_COUNT
292 #error "hammer2 direct radix is incorrect"
293 #endif
294 #if (1 << HAMMER2_PBUFRADIX) != HAMMER2_PBUFSIZE
295 #error "HAMMER2_PBUFRADIX and HAMMER2_PBUFSIZE are inconsistent"
296 #endif
297 #if (1 << HAMMER2_MIN_RADIX) != HAMMER2_MIN_ALLOC
298 #error "HAMMER2_MIN_RADIX and HAMMER2_MIN_ALLOC are inconsistent"
299 #endif
300
301 /*
302  * The media indirect block structure.
303  */
304 struct hammer2_indblock {
305         hammer2_blockref_t blocks[HAMMER2_IND_COUNT];
306 };
307
308 typedef struct hammer2_indblock hammer2_indblock_t;
309
310 /*
311  * In HAMMER2 inodes ARE directory entries, with a special exception for
312  * hardlinks.  The inode number is stored in the inode rather than being
313  * based on the location of the inode (since the location moves every time
314  * the inode or anything underneath the inode is modified).
315  *
316  * The inode is 1024 bytes, made up of 256 bytes of meta-data, 256 bytes
317  * for the filename, and 512 bytes worth of direct file data OR an embedded
318  * blockset.
319  *
320  * Directories represent one inode per blockref.  Inodes are not laid out
321  * as a file but instead are represented by the related blockrefs.  The
322  * blockrefs, in turn, are indexed by the 64-bit directory hash key.  Remember
323  * that blocksets are fully associative, so a certain degree efficiency is
324  * achieved just from that.
325  *
326  * Up to 512 bytes of direct data can be embedded in an inode, and since
327  * inodes are essentially directory entries this also means that small data
328  * files end up simply being laid out linearly in the directory, resulting
329  * in fewer seeks and highly optimal access.
330  *
331  * The compression mode can be changed at any time in the inode and is
332  * recorded on a blockref-by-blockref basis.
333  *
334  * Hardlinks are supported via the inode map.  Essentially the way a hardlink
335  * works is that all individual directory entries representing the same file
336  * are special cased and specify the same inode number.  The actual file
337  * is placed in the nearest parent directory that is parent to all instances
338  * of the hardlink.  If all hardlinks to a file are in the same directory
339  * the actual file will also be placed in that directory.  This file uses
340  * the inode number as the directory entry key and is invisible to normal
341  * directory scans.  Real directory entry keys are differentiated from the
342  * inode number key via bit 63.  Access to the hardlink silently looks up
343  * the real file and forwards all operations to that file.  Removal of the
344  * last hardlink also removes the real file.
345  */
346 #define HAMMER2_INODE_BYTES             1024    /* (asserted by code) */
347 #define HAMMER2_INODE_MAXNAME           256     /* maximum name in bytes */
348 #define HAMMER2_INODE_VERSION_ONE       1
349
350 struct hammer2_inode_data {
351         uint16_t        version;        /* 0000 inode data version */
352         uint16_t        reserved02;     /* 0002 */
353         uint32_t        uflags;         /* 0004 chflags */
354         uint32_t        rmajor;         /* 0008 available for device nodes */
355         uint32_t        rminor;         /* 000C available for device nodes */
356         uint64_t        ctime;          /* 0010 inode change time */
357         uint64_t        mtime;          /* 0018 modified time */
358         uint64_t        atime;          /* 0020 access time (unsupported) */
359         uint64_t        btime;          /* 0028 birth time */
360         uuid_t          uid;            /* 0030 uid / degenerate unix uid */
361         uuid_t          gid;            /* 0040 gid / degenerate unix gid */
362
363         uint8_t         type;           /* 0050 object type */
364         uint8_t         op_flags;       /* 0051 operational flags */
365         uint16_t        cap_flags;      /* 0052 capability flags */
366         uint32_t        mode;           /* 0054 unix modes (typ low 16 bits) */
367
368         hammer2_tid_t   inum;           /* 0058 inode number */
369         hammer2_off_t   size;           /* 0060 size of file */
370         uint64_t        nlinks;         /* 0068 hard links (typ only dirs) */
371         hammer2_tid_t   iparent;        /* 0070 parent inum (recovery only) */
372         uint64_t        reserved78;     /* 0078 */
373
374         hammer2_off_t   data_quota;     /* 0080 subtree quota in bytes */
375         hammer2_off_t   data_count;     /* 0088 subtree byte count */
376         hammer2_off_t   inode_quota;    /* 0090 subtree quota inode count */
377         hammer2_off_t   inode_count;    /* 0098 subtree inode count */
378         uint16_t        name_len;       /* 00A0 filename length */
379         uint8_t         comp_algo;      /* 00A2 compression request & algo */
380         uint8_t         reservedA3;     /* 00A3 */
381         uint32_t        reservedA4;     /* 00A4 */
382         hammer2_key_t   name_key;       /* 00A8 full filename key */
383         uint8_t         copyids[8];     /* 00B0 request copies to (up to 8) */
384         uint64_t        reservedB8;     /* 00B8 */
385         uint64_t        reservedC0;     /* 00C0 */
386         uint64_t        reservedC8;     /* 00C8 */
387         uint64_t        reservedD0;     /* 00D0 */
388         uint64_t        reservedD8;     /* 00D8 */
389         uint64_t        reservedE0;     /* 00E0 */
390         uint64_t        reservedE8;     /* 00E8 */
391         uint64_t        reservedF0;     /* 00F0 */
392         uint64_t        reservedF8;     /* 00F8 */
393
394         char            filename[HAMMER_INODE_MAXNAME];
395                                         /* 0100-01FF (256 char, unterminated) */
396         union {                         /* 0200-03FF (64x8 = 512 bytes) */
397                 struct hammer2_blockset blockset;
398                 char data[HAMMER2_EMBEDDED_BYTES];
399         } u;
400 };
401
402 #define HAMMER2_OPFLAG_DIRECTDATA       0x01
403
404 #define HAMMER2_OBJTYPE_UNKNOWN         0
405 #define HAMMER2_OBJTYPE_DIRECTORY       1
406 #define HAMMER2_OBJTYPE_REGFILE         2
407 #define HAMMER2_OBJTYPE_FIFO            4
408 #define HAMMER2_OBJTYPE_CDEV            5
409 #define HAMMER2_OBJTYPE_BDEV            6
410 #define HAMMER2_OBJTYPE_SOFTLINK        7
411 #define HAMMER2_OBJTYPE_HARDLINK        8
412 #define HAMMER2_OBJTYPE_SOCKET          9
413 #define HAMMER2_OBJTYPE_WHITEOUT        10
414
415 #if 0
416 /*
417  * HAMMER2 special blocks, 128 64K buffers at the beginning of each 2GB segment.
418  */
419 #define HAMMER2_SPECBLOCK(n)                    (HAMMER2_PBUFSIZE64 * (n))
420
421 #define HAMMER2_SBLOCK_VOLHDR                   (0)
422 #define HAMMER2_SBLOCK_FREEMAP_ROOT(side)       (1 + (8 * (side)))
423 #define HAMMER2_SBLOCK_FREEMAP_L1(side)         (2 + (8 * (side)))
424 #define HAMMER2_SBLOCK_FREEMAP_L2(side)         (3 + (8 * (side)))
425 #define HAMMER2_SBLOCK_FREEMAP_LEAF(side, n)    (4 + (8 * (side)) + (n))
426
427 /*
428  * The allocref structure represents the allocation table.  One 64K block
429  * is broken down into 4096 x 16 byte entries.  Each indirect block chops
430  * 11 bits off the 64-bit storage space, with leaf entries representing
431  * 64KB blocks.  So:  (12, 12, 12, 12, 16) = 64 bit storage space.
432  *
433  * Each 64K freemap block breaks the 4096 entries into a 64x64 tree with
434  * big_hint1 representing the top level every 64th entry and big_hint2
435  * representing the lower level in each entry.  These fields specify the
436  * largest contiguous radix (1-63) available for allocation in the related
437  * sub-tree.  The largest contiguous radix available for the entire block
438  * is saved in the parent (for the root this will be alloc_blockref in the
439  * volume header).  The hints may be larger than actual and will be corrected
440  * on the fly but must not be smaller.  The allocator uses the hints to
441  * very quickly locate nearby blocks of the desired size.
442  *
443  * In indirect blocks the 64-bit free[_or_mask] field stores the total free
444  * space for each of the 4096 sub-nodes in bytes.  The total free space
445  * represented by the indirect block is stored in its parent.
446  *
447  * Each leaf element represents a 64K block.  A bitmap replaces the free space
448  * count, giving us a 1KB allocation resolution.  A micro-allocation append
449  * offset replaces the icrc field.  The micro-allocation feature is not
450  * currently implemented and the field will be set to 65536.
451  *
452  * The allocation map uses reserved blocks so no data block reference is
453  * required, only a bit in the flags field to specify which of two possible
454  * reserved blocks to use.  This allows the allocation map to be flushed to
455  * disk with minimal synchronization.
456  */
457 struct hammer2_allocref {
458         uint32_t        icrc_or_app;    /* node: icrc, leaf: append offset */
459         uint16_t        flags;
460         uint8_t         big_hint1;      /* upper level hint */
461         uint8_t         big_hint2;      /* lower level hint */
462         uint64_t        free_or_mask;   /* node: free bytes, leaf: bitmask */
463 };
464
465 typedef struct hammer2_allocref hammer2_allocref_t;
466
467 /*
468  * WARNING - allocref size x entries must equate to the hammer buffer size,
469  *           and 12 bits per recursion is assumed by the allocator.
470  *
471  * ALTA-D       Since no data_offset is specified flags are needed to select
472  *              which sub-block to recurse down into for root & internal nodes.
473  *              (only ALTA and ALTB is currently supported).
474  *
475  * LEAF         Terminal entry, always set for leafs.  May be used to support
476  *              4MB extent allocations and early termination in the future.
477  *              (not required to shortcut allocation scans as the big_hint1/2
478  *              fields are used for this).
479  */
480 #define HAMMER2_ALLOCREF_BYTES          16      /* structure size */
481 #define HAMMER2_ALLOCREF_ENTRIES        4096    /* entries */
482 #define HAMMER2_ALLOCREF_RADIX          12      /* log2(entries) */
483
484 #if (HAMMER2_ALLOCREF_BYTES * HAMMER2_ALLOCREF_ENTRIES) != HAMMER2_BUFSIZE
485 #error "allocref parameters do not fit in hammer buffer"
486 #endif
487 #if (1 << HAMMER2_ALLOCREF_RADIX) != HAMMER2_ALLOCREF_ENTRIES
488 #error "allocref parameters are inconsistent"
489 #endif
490
491 #define HAMMER2_ALLOCREF_ALTMASK        0x0003  /* select block for recurse */
492 #define HAMMER2_ALLOCREF_ALTA           0x0000
493 #define HAMMER2_ALLOCREF_ALTB           0x0001
494 #define HAMMER2_ALLOCREF_ALTC           0x0002  /* unsupported */
495 #define HAMMER2_ALLOCREF_ALTD           0x0003  /* unsupported */
496 #define HAMMER2_ALLOCREF_LEAF           0x0004
497
498 #endif
499
500 /*
501  * Copies information stored in the volume header.  Typically formatted
502  * e.g. like 'serno/A21343249.s1d'
503  *
504  * There are 8 copy_data[]'s in the volume header but up to 256 copyid's.
505  * When a copy is removed its copyid remains reserved in the copyid bitmap
506  * (copyexists[] bitmap in volume_data) until the copy references have
507  * been removed from the entire filesystem and cannot be reused until the
508  * removal is complete.  However, new copy entries with other ids can be
509  * instantly added, replacing the original copy_data[]... which is fine as
510  * long as the copyid does not conflict.
511  *
512  * This structure must be exactly 64 bytes long.
513  */
514 struct hammer2_copy_data {
515         uint8_t copyid;         /* 0-255 */
516         uint8_t flags;
517         uint8_t reserved02;
518         uint8_t reserved03;
519         uint8_t path[60];       /* up to 59-char string, nul-terminated */
520 };
521
522 typedef struct hammer2_copy_data hammer2_copy_data_t;
523
524 #define COPYDATAF_OUTOFSYNC     0x0001
525
526 /*
527  * The volume header eats a 64K block.  There is currently an issue where
528  * we want to try to fit all nominal filesystem updates in a 512-byte section
529  * but it may be a lost cause due to the need for a blockset.
530  *
531  * All information is stored in host byte order.  The volume header's magic
532  * number may be checked to determine the byte order.  If you wish to mount
533  * between machines w/ different endian modes you'll need filesystem code
534  * which acts on the media data consistently (either all one way or all the
535  * other).  Our code currently does not do that.
536  *
537  * A read-write mount may have to recover missing allocations by doing an
538  * incremental mirror scan looking for modifications made after alloc_tid.
539  * If alloc_tid == last_tid then no recovery operation is needed.  Recovery
540  * operations are usually very, very fast.
541  *
542  * Read-only mounts do not need to do any recovery, access to the filesystem
543  * topology is always consistent after a crash (is always consistent, period).
544  * However, there may be shortcutted blockref updates present from deep in
545  * the tree which are stored in the volumeh eader and must be tracked on
546  * the fly.
547  *
548  * icrc_sect0 only applies to the first 512-4 bytes in the volume header.
549  *
550  * COPIES: Multiple copies may be specified on the mount line AND/OR you
551  *         just specify one and the mount code tries to pick up the others
552  *         from copyinfo[].  The copyid field in the volume header along
553  *         with the fsid validates the copies.
554  *
555  * NOTE: root_blockref points to the super-root directory, not the root
556  *       directory.  The root directory will be a subdirectory under the
557  *       super-root.
558  *
559  *       The super-root directory contains all root directories and all
560  *       snapshots (readonly or writable).  It is possible to do a
561  *       null-mount of the super-root using special path constructions
562  *       relative to your mounted root.
563  *
564  * NOTE: HAMMER2 allows any subdirectory tree to be managed as if it were
565  *       a PFS, including mirroring and storage quota operations, and this is
566  *       prefered over creating discrete PFSs in the super-root.  Instead
567  *       the super-root is most typically used to create writable snapshots,
568  *       alternative roots, and so forth.  The super-root is also used by
569  *       the automatic snapshotting mechanism.
570  */
571 #define HAMMER2_VOLUME_ID_HBO   0x48414d3205172011LLU
572 #define HAMMER2_VOLUME_ID_ABO   0x11201705324d4148LLU
573
574 struct hammer2_volume_data {
575         /*
576          * 512-byte sector #0
577          */
578         uint64_t        magic;                  /* 0000 Signature */
579         hammer2_off_t   boot_beg;               /* 0008 Boot area (future) */
580         hammer2_off_t   boot_end;               /* 0010 (size = end - beg) */
581         hammer2_off_t   redo_beg;               /* 0018 Redo area (future) */
582         hammer2_off_t   redo_end;               /* 0020 (size = end - beg) */
583         hammer2_off_t   volu_size;              /* 0028 Volume size, bytes */
584
585         uint32_t        version;                /* 0030 */
586         uint32_t        flags;                  /* 0034 */
587         uint8_t         copyid;                 /* 0038 copyid of phys vol */
588         uint8_t         freemap_version;        /* 0039 freemap algorithm */
589         uint8_t         reserved003A;           /* 003A */
590         uint8_t         reserved003B;           /* 003B */
591         uint32_t        reserved003C;           /* 003C */
592
593         uuid_t          fsid;                   /* 0040 */
594         uuid_t          fstype;                 /* 0050 */
595
596         /*
597          * allocator_size is precalculated at newfs time and does not include
598          * reserved blocks, boot, or redo areas.
599          *
600          * Initial non-reserved-area allocations do not use the allocation
601          * map but instead adjust alloc_iterator.  Dynamic allocations take
602          * over starting at (allocator_beg).  This makes newfs_hammer2's
603          * job a lot easier and can also serve as a testing jig.
604          */
605         hammer2_off_t   allocator_size;         /* 0060 Total data space */
606         hammer2_off_t   allocator_free;         /* 0068 Free space */
607         hammer2_tid_t   allocator_beg;          /* 0070 Initial allocations */
608         hammer2_tid_t   last_tid;               /* 0078 Last transaction id */
609         hammer2_tid_t   alloc_tid;              /* 0080 Alloctable modify tid */
610         hammer2_blockref_t alloc_blockref;      /* 0088-00C7 */
611
612         /*
613          * Copyids are allocated dynamically from the copyexists bitmap.
614          * An id from the active copies set (up to 8, see copyinfo later on)
615          * may still exist after the copy set has been removed from the
616          * volume header and its bit will remain active in the bitmap and
617          * cannot be reused until it is 100% removed from the hierarchy.
618          */
619         uint32_t        copyexists[8];          /* 00C8-00E7 copy exists bmap */
620         char            reserved0140[248];      /* 00E8-01DF */
621
622         /*
623          * 32 bit CRC array at the end of the first 512 byte sector.
624          *
625          * icrc_sects[7] - First 512-4 bytes of volume header (including all
626          *                 the other icrc's except the last one).
627          *
628          * icrc_sects[6] - Second 512-4 bytes of volume header, which is
629          *                 the blockset for the root.
630          */
631         hammer2_crc32_t icrc_sects[8];          /* 01E0-01FF */
632
633         /*
634          * 512-byte sector #1
635          *
636          * The entire sector is used by a blockset.
637          */
638         hammer2_blockset_t sroot_blockset;      /* 0200 Superroot directory */
639
640         /*
641          * 512-byte sector #2-33
642          *
643          * Up to 256 copyinfo specifications can be configured.  Note that
644          * any given subdirectory tree can only use 8 of the 256.  Having
645          * up to 256 configurable in the volume header allows
646          *
647          * A specification takes 64 bytes.  Each specification typically
648          * configures a device path such as 'serno/<serial>.s1d'.
649          */
650         struct hammer2_copy_data copyinfo[256]; /* 0400-43FF copyinfo config */
651
652         /*
653          * Remaining sections are reserved for future use.
654          */
655         char            reserved0400[0xBBFC];   /* 4400-FFFB reserved */
656
657         /*
658          * icrc on entire volume header
659          */
660         hammer2_crc32_t icrc_volheader;         /* FFFC-FFFF full volume icrc*/
661 };
662
663 /*
664  * Section 0 and section 1 have their own iCRCs.  Remaining icrc_sets[]
665  * entries are reserved for future use.
666  *
667  * icrc_volheader iCRCs the whole 64K volume header block and is catch-all
668  * for anything not individually iCRCd.
669  */
670 #define HAMMER2_VOL_ICRC_SECT0          7
671 #define HAMMER2_VOL_ICRC_SECT1          6
672
673
674 #define HAMMER2_VOLUME_BYTES            65536
675 #define HAMMER2_VOLUME_ICRCSIZE offsetof(hammer2_volume_data_t, icrc_sect0)
676
677 #define HAMMER2_VOL_VERSION_MIN         1
678 #define HAMMER2_VOL_VERSION_DEFAULT     1
679 #define HAMMER2_VOL_VERSION_WIP         2
680
681 #define HAMMER2_NUM_VOLHDRS             4
682
683 /*
684  * Prototypes for user & kernel functions.  Kernel-only prototypes are
685  * elsewhere.
686  */
687 uint32_t hammer2_icrc32(const void *buf, size_t size);
688 uint32_t hammer2_icrc32c(const void *buf, size_t size, uint32_t crc);
689
690 #endif