Merge branches 'hammer2' and 'master' of ssh://crater.dragonflybsd.org/repository...
[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_KEY_RADIX       64      /* number of bits in key */
76
77 /*
78  * MINALLOCSIZE         - The minimum allocation size.  This can be smaller
79  *                        or larger than the minimum physical IO size.
80  *
81  *                        NOTE: Should not be larger than 1K since inodes
82  *                              are 1K.
83  *
84  * MINIOSIZE            - The minimum IO size.  This must be less than
85  *                        or equal to HAMMER2_PBUFSIZE.
86  *
87  *                        XXX currently must be set to MINALLOCSIZE until/if
88  *                            we deal with recursive buffer cache locks.
89  *
90  * HAMMER2_PBUFSIZE     - Topological block size used by files for all
91  *                        blocks except the block straddling EOF.
92  *
93  * HAMMER2_SEGSIZE      - Allocation map segment size, typically 2MB
94  */
95
96 #define HAMMER2_SEGSIZE         (65536 * 8)
97
98 #define HAMMER2_PBUFRADIX       16      /* physical buf (1<<16) bytes */
99 #define HAMMER2_PBUFSIZE        65536
100 #define HAMMER2_LBUFRADIX       14      /* logical buf (1<<14) bytes */
101 #define HAMMER2_LBUFSIZE        16384
102
103 #if 0
104 #define HAMMER2_MINIORADIX      16      /* minimum phsical IO size */
105 #define HAMMER2_MINIOSIZE       65536
106 #endif
107 #define HAMMER2_MINIORADIX      HAMMER2_MINALLOCRADIX
108 #define HAMMER2_MINIOSIZE       HAMMER2_MINALLOCSIZE
109
110 #define HAMMER2_MINALLOCRADIX   10      /* minimum block allocation size */
111 #define HAMMER2_MINALLOCSIZE    1024
112 #define HAMMER2_IND_BYTES_MIN   4096    /* first indirect layer only */
113 #define HAMMER2_IND_BYTES_MAX   HAMMER2_PBUFSIZE
114 #define HAMMER2_IND_COUNT_MIN   (HAMMER2_IND_BYTES_MIN / \
115                                  sizeof(hammer2_blockref_t))
116 #define HAMMER2_IND_COUNT_MAX   (HAMMER2_IND_BYTES_MAX / \
117                                  sizeof(hammer2_blockref_t))
118
119 /*
120  * HAMMER2 processes blockrefs in sets of 8.  The set is fully associative,
121  * is not sorted, and may contain holes.
122  *
123  * A full indirect block supports 1024 blockrefs.
124  *
125  * An inode embeds one set of blockrefs but may also use the data area for
126  * up to 512 bytes of direct data.
127  */
128 #define HAMMER2_SET_COUNT       8       /* direct entries & associativity */
129 #define HAMMER2_SET_RADIX       3
130 #define HAMMER2_EMBEDDED_BYTES  512
131 #define HAMMER2_EMBEDDED_RADIX  9
132
133 #define HAMMER2_PBUFMASK        (HAMMER2_PBUFSIZE - 1)
134 #define HAMMER2_LBUFMASK        (HAMMER2_LBUFSIZE - 1)
135 #define HAMMER2_SEGMASK         (HAMMER2_SEGSIZE - 1)
136
137 #define HAMMER2_LBUFMASK64      ((hammer2_off_t)HAMMER2_LBUFMASK)
138 #define HAMMER2_PBUFSIZE64      ((hammer2_off_t)HAMMER2_PBUFSIZE)
139 #define HAMMER2_PBUFMASK64      ((hammer2_off_t)HAMMER2_PBUFMASK)
140 #define HAMMER2_SEGSIZE64       ((hammer2_off_t)HAMMER2_SEGSIZE)
141 #define HAMMER2_SEGMASK64       ((hammer2_off_t)HAMMER2_SEGMASK)
142
143 #define HAMMER2_UUID_STRING     "5cbb9ad1-862d-11dc-a94d-01301bb8a9f5"
144
145 /*
146  * A HAMMER2 filesystem is always sized in multiples of 8MB.
147  *
148  * A 4MB segment is reserved at the beginning of each 2GB zone.  This segment
149  * contains the volume header, the free block table, and possibly other
150  * information in the future.  4MB = 64 x 64K blocks.
151  */
152 #define HAMMER2_VOLUME_ALIGN            (8 * 1024 * 1024)
153 #define HAMMER2_VOLUME_ALIGN64          ((hammer2_off_t)HAMMER2_VOLUME_ALIGN)
154 #define HAMMER2_VOLUME_ALIGNMASK        (HAMMER2_VOLUME_ALIGN - 1)
155 #define HAMMER2_VOLUME_ALIGNMASK64     ((hammer2_off_t)HAMMER2_VOLUME_ALIGNMASK)
156
157 #define HAMMER2_NEWFS_ALIGN             (HAMMER2_VOLUME_ALIGN)
158 #define HAMMER2_NEWFS_ALIGN64           ((hammer2_off_t)HAMMER2_VOLUME_ALIGN)
159 #define HAMMER2_NEWFS_ALIGNMASK         (HAMMER2_VOLUME_ALIGN - 1)
160 #define HAMMER2_NEWFS_ALIGNMASK64       ((hammer2_off_t)HAMMER2_NEWFS_ALIGNMASK)
161
162 #define HAMMER2_RESERVE_BYTES64         (2LLU * 1024 * 1024 * 1024)
163 #define HAMMER2_RESERVE_MASK64          (HAMMER2_RESERVE_BYTES64 - 1)
164 #define HAMMER2_RESERVE_SEG             (4 * 1024 * 1024)
165 #define HAMMER2_RESERVE_SEG64           ((hammer2_off_t)HAMMER2_RESERVE_SEG)
166 #define HAMMER2_RESERVE_BLOCKS          (HAMMER2_RESERVE_SEG / HAMMER2_PBUFSIZE)
167
168 /*
169  * Two linear areas can be reserved after the initial 2MB segment in the base
170  * zone (the one starting at offset 0).  These areas are NOT managed by the
171  * block allocator and do not fall under HAMMER2 crc checking rules based
172  * at the volume header (but can be self-CRCd internally, depending).
173  */
174 #define HAMMER2_BOOT_MIN_BYTES          HAMMER2_VOLUME_ALIGN
175 #define HAMMER2_BOOT_NOM_BYTES          (64*1024*1024)
176 #define HAMMER2_BOOT_MAX_BYTES          (256*1024*1024)
177
178 #define HAMMER2_REDO_MIN_BYTES          HAMMER2_VOLUME_ALIGN
179 #define HAMMER2_REDO_NOM_BYTES          (256*1024*1024)
180 #define HAMMER2_REDO_MAX_BYTES          (1024*1024*1024)
181
182 /*
183  * Most HAMMER2 types are implemented as unsigned 64-bit integers.
184  * Transaction ids are monotonic.
185  *
186  * We utilize 32-bit iSCSI CRCs.
187  */
188 typedef uint64_t hammer2_tid_t;
189 typedef uint64_t hammer2_off_t;
190 typedef uint64_t hammer2_key_t;
191 typedef uint32_t hammer2_crc32_t;
192
193 /*
194  * Miscellanious ranges (all are unsigned).
195  */
196 #define HAMMER2_MIN_TID         1ULL
197 #define HAMMER2_MAX_TID         0xFFFFFFFFFFFFFFFFULL
198 #define HAMMER2_MIN_KEY         0ULL
199 #define HAMMER2_MAX_KEY         0xFFFFFFFFFFFFFFFFULL
200 #define HAMMER2_MIN_OFFSET      0ULL
201 #define HAMMER2_MAX_OFFSET      0xFFFFFFFFFFFFFFFFULL
202
203 /*
204  * HAMMER2 data offset special cases and masking.
205  *
206  * All HAMMER2 data offsets have to be broken down into a 64K buffer base
207  * offset (HAMMER2_OFF_MASK_HI) and a 64K buffer index (HAMMER2_OFF_MASK_LO).
208  *
209  * Indexes into physical buffers are always 64-byte aligned.  The low 6 bits
210  * of the data offset field specifies how large the data chunk being pointed
211  * to as a power of 2.  This value typically ranges from HAMMER2_MIN_RADIX
212  * to HAMMER2_MAX_RADIX (6-16).  Larger values may be supported in the future
213  * to support file extents.
214  */
215 #define HAMMER2_OFF_BAD         ((hammer2_off_t)-1)
216 #define HAMMER2_OFF_MASK        0xFFFFFFFFFFFFFFC0ULL
217 #define HAMMER2_OFF_MASK_LO     (HAMMER2_OFF_MASK & HAMMER2_PBUFMASK64)
218 #define HAMMER2_OFF_MASK_HI     (~HAMMER2_PBUFMASK64)
219 #define HAMMER2_OFF_MASK_RADIX  0x000000000000003FULL
220 #define HAMMER2_MAX_COPIES      6
221
222 /*
223  * HAMMER2 directory support and pre-defined keys
224  */
225 #define HAMMER2_DIRHASH_VISIBLE 0x8000000000000000ULL
226 #define HAMMER2_DIRHASH_USERMSK 0x7FFFFFFFFFFFFFFFULL
227 #define HAMMER2_DIRHASH_LOMASK  0x0000000000007FFFULL
228 #define HAMMER2_DIRHASH_HIMASK  0xFFFFFFFFFFFF0000ULL
229 #define HAMMER2_DIRHASH_FORCED  0x0000000000008000ULL   /* bit forced on */
230
231 #define HAMMER2_SROOT_KEY       0x0000000000000000ULL   /* volume to sroot */
232
233 /*
234  * The media block reference structure.  This forms the core of the HAMMER2
235  * media topology recursion.  This 64-byte data structure is embedded in the
236  * volume header, in inodes (which are also directory entries), and in
237  * indirect blocks.
238  *
239  * A blockref references a single media item, which typically can be a
240  * directory entry (aka inode), indirect block, or data block.
241  *
242  * The primary feature a blockref represents is the ability to validate
243  * the entire tree underneath it via its check code.  Any modification to
244  * anything propagates up the blockref tree all the way to the root, replacing
245  * the related blocks.  Propagations can shortcut to the volume root to
246  * implement the 'fast syncing' feature but this only delays the eventual
247  * propagation.
248  *
249  * The check code can be a simple 32-bit iscsi code, a 64-bit crc,
250  * or as complex as a 192 bit cryptographic hash.  192 bits is the maximum
251  * supported check code size, which is not sufficient for unverified dedup
252  * UNLESS one doesn't mind once-in-a-blue-moon data corruption (such as when
253  * farming web data).  HAMMER2 has an unverified dedup feature for just this
254  * purpose.
255  */
256 struct hammer2_blockref {               /* MUST BE EXACTLY 64 BYTES */
257         uint8_t         type;           /* type of underlying item */
258         uint8_t         methods;        /* check method & compression method */
259         uint8_t         copyid;         /* specify which copy this is */
260         uint8_t         keybits;        /* #of keybits masked off 0=leaf */
261         uint8_t         vradix;         /* virtual data/meta-data size */
262         uint8_t         flags;          /* blockref flags */
263         uint8_t         reserved06;
264         uint8_t         reserved07;
265         hammer2_key_t   key;            /* key specification */
266         hammer2_tid_t   mirror_tid;     /* propagate for mirror scan */
267         hammer2_tid_t   modify_tid;     /* modifications sans propagation */
268         hammer2_off_t   data_off;       /* low 6 bits is phys size (radix)*/
269         union {                         /* check info */
270                 char    buf[24];
271                 struct {
272                         uint32_t value;
273                         uint32_t unused[5];
274                 } iscsi32;
275                 struct {
276                         uint64_t value;
277                         uint64_t unused[2];
278                 } crc64;
279                 struct {
280                         char data[24];
281                 } sha192;
282         } check;
283 };
284
285 typedef struct hammer2_blockref hammer2_blockref_t;
286
287 #define HAMMER2_BREF_SYNC1              0x01    /* modification synchronized */
288 #define HAMMER2_BREF_SYNC2              0x02    /* modification committed */
289 #define HAMMER2_BREF_DESYNCCHLD         0x04    /* desynchronize children */
290 #define HAMMER2_BREF_DELETED            0x80    /* indicates a deletion */
291
292 #define HAMMER2_BLOCKREF_BYTES          64      /* blockref struct in bytes */
293
294 #define HAMMER2_BREF_TYPE_EMPTY         0
295 #define HAMMER2_BREF_TYPE_INODE         1
296 #define HAMMER2_BREF_TYPE_INDIRECT      2
297 #define HAMMER2_BREF_TYPE_DATA          3
298 #define HAMMER2_BREF_TYPE_VOLUME        255     /* pseudo-type */
299
300 #define HAMMER2_ENC_COMPMETHOD(n)       (n)
301 #define HAMMER2_ENC_CHECKMETHOD(n)      ((n) << 4)
302 #define HAMMER2_DEC_COMPMETHOD(n)       ((n) & 15)
303 #define HAMMER2_DEC_CHECKMETHOD(n)      (((n) >> 4) & 15)
304
305 /*
306  * HAMMER2 block references are collected into sets of 8 blockrefs.  These
307  * sets are fully associative, meaning the elements making up a set are
308  * not sorted in any way and may contain duplicate entries, holes, or
309  * entries which shortcut multiple levels of indirection.  Sets are used
310  * in various ways:
311  *
312  * (1) When redundancy is desired a set may contain several duplicate
313  *     entries pointing to different copies of the same data.  Up to 8 copies
314  *     are supported but the set structure becomes a bit inefficient once
315  *     you go over 4.
316  *
317  * (2) The blockrefs in a set can shortcut multiple levels of indirections
318  *     within the bounds imposed by the parent of set.
319  *
320  * When a set fills up another level of indirection is inserted, moving
321  * some or all of the set's contents into indirect blocks placed under the
322  * set.  This is a top-down approach in that indirect blocks are not created
323  * until the set actually becomes full (that is, the entries in the set can
324  * shortcut the indirect blocks when the set is not full).  Depending on how
325  * things are filled multiple indirect blocks will eventually be created.
326  */
327 struct hammer2_blockset {
328         hammer2_blockref_t      blockref[HAMMER2_SET_COUNT];
329 };
330
331 typedef struct hammer2_blockset hammer2_blockset_t;
332
333 /*
334  * Catch programmer snafus
335  */
336 #if (1 << HAMMER2_SET_RADIX) != HAMMER2_SET_COUNT
337 #error "hammer2 direct radix is incorrect"
338 #endif
339 #if (1 << HAMMER2_PBUFRADIX) != HAMMER2_PBUFSIZE
340 #error "HAMMER2_PBUFRADIX and HAMMER2_PBUFSIZE are inconsistent"
341 #endif
342 #if (1 << HAMMER2_MIN_RADIX) != HAMMER2_MIN_ALLOC
343 #error "HAMMER2_MIN_RADIX and HAMMER2_MIN_ALLOC are inconsistent"
344 #endif
345
346 /*
347  * The media indirect block structure.
348  */
349 struct hammer2_indblock_data {
350         hammer2_blockref_t blockref[HAMMER2_IND_COUNT_MAX];
351 };
352
353 typedef struct hammer2_indblock_data hammer2_indblock_data_t;
354
355 /*
356  * In HAMMER2 inodes ARE directory entries, with a special exception for
357  * hardlinks.  The inode number is stored in the inode rather than being
358  * based on the location of the inode (since the location moves every time
359  * the inode or anything underneath the inode is modified).
360  *
361  * The inode is 1024 bytes, made up of 256 bytes of meta-data, 256 bytes
362  * for the filename, and 512 bytes worth of direct file data OR an embedded
363  * blockset.
364  *
365  * Directories represent one inode per blockref.  Inodes are not laid out
366  * as a file but instead are represented by the related blockrefs.  The
367  * blockrefs, in turn, are indexed by the 64-bit directory hash key.  Remember
368  * that blocksets are fully associative, so a certain degree efficiency is
369  * achieved just from that.
370  *
371  * Up to 512 bytes of direct data can be embedded in an inode, and since
372  * inodes are essentially directory entries this also means that small data
373  * files end up simply being laid out linearly in the directory, resulting
374  * in fewer seeks and highly optimal access.
375  *
376  * The compression mode can be changed at any time in the inode and is
377  * recorded on a blockref-by-blockref basis.
378  *
379  * Hardlinks are supported via the inode map.  Essentially the way a hardlink
380  * works is that all individual directory entries representing the same file
381  * are special cased and specify the same inode number.  The actual file
382  * is placed in the nearest parent directory that is parent to all instances
383  * of the hardlink.  If all hardlinks to a file are in the same directory
384  * the actual file will also be placed in that directory.  This file uses
385  * the inode number as the directory entry key and is invisible to normal
386  * directory scans.  Real directory entry keys are differentiated from the
387  * inode number key via bit 63.  Access to the hardlink silently looks up
388  * the real file and forwards all operations to that file.  Removal of the
389  * last hardlink also removes the real file.
390  */
391 #define HAMMER2_INODE_BYTES             1024    /* (asserted by code) */
392 #define HAMMER2_INODE_MAXNAME           256     /* maximum name in bytes */
393 #define HAMMER2_INODE_VERSION_ONE       1
394
395 struct hammer2_inode_data {
396         uint16_t        version;        /* 0000 inode data version */
397         uint16_t        reserved02;     /* 0002 */
398         uint32_t        uflags;         /* 0004 chflags */
399         uint32_t        rmajor;         /* 0008 available for device nodes */
400         uint32_t        rminor;         /* 000C available for device nodes */
401         uint64_t        ctime;          /* 0010 inode change time */
402         uint64_t        mtime;          /* 0018 modified time */
403         uint64_t        atime;          /* 0020 access time (unsupported) */
404         uint64_t        btime;          /* 0028 birth time */
405         uuid_t          uid;            /* 0030 uid / degenerate unix uid */
406         uuid_t          gid;            /* 0040 gid / degenerate unix gid */
407
408         uint8_t         type;           /* 0050 object type */
409         uint8_t         op_flags;       /* 0051 operational flags */
410         uint16_t        cap_flags;      /* 0052 capability flags */
411         uint32_t        mode;           /* 0054 unix modes (typ low 16 bits) */
412
413         hammer2_tid_t   inum;           /* 0058 inode number */
414         hammer2_off_t   size;           /* 0060 size of file */
415         uint64_t        nlinks;         /* 0068 hard links (typ only dirs) */
416         hammer2_tid_t   iparent;        /* 0070 parent inum (recovery only) */
417         uint64_t        reserved78;     /* 0078 */
418
419         hammer2_off_t   data_quota;     /* 0080 subtree quota in bytes */
420         hammer2_off_t   data_count;     /* 0088 subtree byte count */
421         hammer2_off_t   inode_quota;    /* 0090 subtree quota inode count */
422         hammer2_off_t   inode_count;    /* 0098 subtree inode count */
423         uint16_t        name_len;       /* 00A0 filename length */
424         uint8_t         comp_algo;      /* 00A2 compression request & algo */
425         uint8_t         reservedA3;     /* 00A3 */
426         uint32_t        reservedA4;     /* 00A4 */
427         hammer2_key_t   name_key;       /* 00A8 full filename key */
428         uint8_t         copyids[8];     /* 00B0 request copies to (up to 8) */
429         uuid_t          pfsid;          /* 00B8 pfs uuid if PFSROOT */
430         uint64_t        pfsinum;        /* 00C8 pfs inum allocator */
431         uint64_t        reservedD0;     /* 00D0 */
432         uint64_t        reservedD8;     /* 00D8 */
433         uint64_t        reservedE0;     /* 00E0 */
434         uint64_t        reservedE8;     /* 00E8 */
435         uint64_t        reservedF0;     /* 00F0 */
436         uint64_t        reservedF8;     /* 00F8 */
437
438         unsigned char   filename[HAMMER2_INODE_MAXNAME];
439                                         /* 0100-01FF (256 char, unterminated) */
440         union {                         /* 0200-03FF (64x8 = 512 bytes) */
441                 struct hammer2_blockset blockset;
442                 char data[HAMMER2_EMBEDDED_BYTES];
443         } u;
444 };
445
446 typedef struct hammer2_inode_data hammer2_inode_data_t;
447
448 #define HAMMER2_OPFLAG_DIRECTDATA       0x01
449 #define HAMMER2_OPFLAG_PFSROOT          0x02
450
451 #define HAMMER2_OBJTYPE_UNKNOWN         0
452 #define HAMMER2_OBJTYPE_DIRECTORY       1
453 #define HAMMER2_OBJTYPE_REGFILE         2
454 #define HAMMER2_OBJTYPE_FIFO            4
455 #define HAMMER2_OBJTYPE_CDEV            5
456 #define HAMMER2_OBJTYPE_BDEV            6
457 #define HAMMER2_OBJTYPE_SOFTLINK        7
458 #define HAMMER2_OBJTYPE_HARDLINK        8       /* dummy entry for hardlink */
459 #define HAMMER2_OBJTYPE_SOCKET          9
460 #define HAMMER2_OBJTYPE_WHITEOUT        10
461
462 #define HAMMER2_COPYID_NONE             0
463 #define HAMMER2_COPYID_LOCAL            ((uint8_t)-1)
464
465 #define HAMMER2_COMP_NONE               0
466 #define HAMMER2_COMP_AUTOZERO           1
467
468 #define HAMMER2_CHECK_NONE              0
469 #define HAMMER2_CHECK_ICRC              1
470
471 /*
472  * The allocref structure represents the allocation table.  One 64K block
473  * is broken down into 4096 x 16 byte entries.  Each indirect block chops
474  * 11 bits off the 64-bit storage space, with leaf entries representing
475  * 64KB blocks.  So:  (12, 12, 12, 12, 16) = 64 bit storage space.
476  *
477  * Each 64K freemap block breaks the 4096 entries into a 64x64 tree with
478  * big_hint1 representing the top level every 64th entry and big_hint2
479  * representing the lower level in each entry.  These fields specify the
480  * largest contiguous radix (1-63) available for allocation in the related
481  * sub-tree.  The largest contiguous radix available for the entire block
482  * is saved in the parent (for the root this will be alloc_blockref in the
483  * volume header).  The hints may be larger than actual and will be corrected
484  * on the fly but must not be smaller.  The allocator uses the hints to
485  * very quickly locate nearby blocks of the desired size.
486  *
487  * In indirect blocks the 64-bit free[_or_mask] field stores the total free
488  * space for each of the 4096 sub-nodes in bytes.  The total free space
489  * represented by the indirect block is stored in its parent.
490  *
491  * Each leaf element represents a 64K block.  A bitmap replaces the free space
492  * count, giving us a 1KB allocation resolution.  A micro-allocation append
493  * offset replaces the icrc field.  The micro-allocation feature is not
494  * currently implemented and the field will be set to 65536.
495  *
496  * The allocation map uses reserved blocks so no data block reference is
497  * required, only a bit in the flags field to specify which of two possible
498  * reserved blocks to use.  This allows the allocation map to be flushed to
499  * disk with minimal synchronization.
500  */
501 struct hammer2_allocref {
502         uint32_t        icrc_or_app;    /* node: icrc, leaf: append offset */
503         uint16_t        flags;
504         uint8_t         big_hint1;      /* upper level hint */
505         uint8_t         big_hint2;      /* lower level hint */
506         uint64_t        free_or_mask;   /* node: free bytes, leaf: bitmask */
507 };
508
509 typedef struct hammer2_allocref hammer2_allocref_t;
510
511 /*
512  * WARNING - allocref size x entries must equate to the hammer buffer size,
513  *           and 12 bits per recursion is assumed by the allocator.
514  *
515  * ALTA-D       Since no data_offset is specified flags are needed to select
516  *              which sub-block to recurse down into for root & internal nodes.
517  *              (only ALTA and ALTB is currently supported).
518  *
519  * LEAF         Terminal entry, always set for leafs.  May be used to support
520  *              4MB extent allocations and early termination in the future.
521  *              (not required to shortcut allocation scans as the big_hint1/2
522  *              fields are used for this).
523  */
524 #define HAMMER2_ALLOCREF_BYTES          16      /* structure size */
525 #define HAMMER2_ALLOCREF_ENTRIES        4096    /* entries */
526 #define HAMMER2_ALLOCREF_RADIX          12      /* log2(entries) */
527
528 #if (HAMMER2_ALLOCREF_BYTES * HAMMER2_ALLOCREF_ENTRIES) != HAMMER2_PBUFSIZE
529 #error "allocref parameters do not fit in hammer buffer"
530 #endif
531 #if (1 << HAMMER2_ALLOCREF_RADIX) != HAMMER2_ALLOCREF_ENTRIES
532 #error "allocref parameters are inconsistent"
533 #endif
534
535 #define HAMMER2_ALLOCREF_ALTMASK        0x0003  /* select block for recurse */
536 #define HAMMER2_ALLOCREF_ALTA           0x0000
537 #define HAMMER2_ALLOCREF_ALTB           0x0001
538 #define HAMMER2_ALLOCREF_ALTC           0x0002  /* unsupported */
539 #define HAMMER2_ALLOCREF_ALTD           0x0003  /* unsupported */
540 #define HAMMER2_ALLOCREF_LEAF           0x0004
541
542 /*
543  * Copies information stored in the volume header.  Typically formatted
544  * e.g. like 'serno/A21343249.s1d'
545  *
546  * There are 8 copy_data[]'s in the volume header but up to 256 copyid's.
547  * When a copy is removed its copyid remains reserved in the copyid bitmap
548  * (copyexists[] bitmap in volume_data) until the copy references have
549  * been removed from the entire filesystem and cannot be reused until the
550  * removal is complete.  However, new copy entries with other ids can be
551  * instantly added, replacing the original copy_data[]... which is fine as
552  * long as the copyid does not conflict.
553  *
554  * This structure must be exactly 64 bytes long.
555  */
556 struct hammer2_copy_data {
557         uint8_t copyid;         /* 0-255 */
558         uint8_t flags;
559         uint8_t reserved02;
560         uint8_t reserved03;
561         uint8_t path[60];       /* up to 59-char string, nul-terminated */
562 };
563
564 typedef struct hammer2_copy_data hammer2_copy_data_t;
565
566 #define COPYDATAF_OUTOFSYNC     0x0001
567
568 /*
569  * The volume header eats a 64K block.  There is currently an issue where
570  * we want to try to fit all nominal filesystem updates in a 512-byte section
571  * but it may be a lost cause due to the need for a blockset.
572  *
573  * All information is stored in host byte order.  The volume header's magic
574  * number may be checked to determine the byte order.  If you wish to mount
575  * between machines w/ different endian modes you'll need filesystem code
576  * which acts on the media data consistently (either all one way or all the
577  * other).  Our code currently does not do that.
578  *
579  * A read-write mount may have to recover missing allocations by doing an
580  * incremental mirror scan looking for modifications made after alloc_tid.
581  * If alloc_tid == last_tid then no recovery operation is needed.  Recovery
582  * operations are usually very, very fast.
583  *
584  * Read-only mounts do not need to do any recovery, access to the filesystem
585  * topology is always consistent after a crash (is always consistent, period).
586  * However, there may be shortcutted blockref updates present from deep in
587  * the tree which are stored in the volumeh eader and must be tracked on
588  * the fly.
589  *
590  * COPIES: Multiple copies may be specified on the mount line AND/OR you
591  *         just specify one and the mount code tries to pick up the others
592  *         from copyinfo[].  The copyid field in the volume header along
593  *         with the fsid validates the copies.
594  *
595  * NOTE: root_blockref points to the super-root directory, not the root
596  *       directory.  The root directory will be a subdirectory under the
597  *       super-root.
598  *
599  *       The super-root directory contains all root directories and all
600  *       snapshots (readonly or writable).  It is possible to do a
601  *       null-mount of the super-root using special path constructions
602  *       relative to your mounted root.
603  *
604  * NOTE: HAMMER2 allows any subdirectory tree to be managed as if it were
605  *       a PFS, including mirroring and storage quota operations, and this is
606  *       prefered over creating discrete PFSs in the super-root.  Instead
607  *       the super-root is most typically used to create writable snapshots,
608  *       alternative roots, and so forth.  The super-root is also used by
609  *       the automatic snapshotting mechanism.
610  */
611 #define HAMMER2_VOLUME_ID_HBO   0x48414d3205172011LLU
612 #define HAMMER2_VOLUME_ID_ABO   0x11201705324d4148LLU
613
614 struct hammer2_volume_data {
615         /*
616          * 512-byte sector #0
617          */
618         uint64_t        magic;                  /* 0000 Signature */
619         hammer2_off_t   boot_beg;               /* 0008 Boot area (future) */
620         hammer2_off_t   boot_end;               /* 0010 (size = end - beg) */
621         hammer2_off_t   aux_beg;                /* 0018 Aux area (future) */
622         hammer2_off_t   aux_end;                /* 0020 (size = end - beg) */
623         hammer2_off_t   volu_size;              /* 0028 Volume size, bytes */
624
625         uint32_t        version;                /* 0030 */
626         uint32_t        flags;                  /* 0034 */
627         uint8_t         copyid;                 /* 0038 copyid of phys vol */
628         uint8_t         freemap_version;        /* 0039 freemap algorithm */
629         uint8_t         reserved003A;           /* 003A */
630         uint8_t         reserved003B;           /* 003B */
631         uint32_t        reserved003C;           /* 003C */
632
633         uuid_t          fsid;                   /* 0040 */
634         uuid_t          fstype;                 /* 0050 */
635
636         /*
637          * allocator_size is precalculated at newfs time and does not include
638          * reserved blocks, boot, or redo areas.
639          *
640          * Initial non-reserved-area allocations do not use the allocation
641          * map but instead adjust alloc_iterator.  Dynamic allocations take
642          * over starting at (allocator_beg).  This makes newfs_hammer2's
643          * job a lot easier and can also serve as a testing jig.
644          */
645         hammer2_off_t   allocator_size;         /* 0060 Total data space */
646         hammer2_off_t   allocator_free;         /* 0068 Free space */
647         hammer2_tid_t   allocator_beg;          /* 0070 Initial allocations */
648         hammer2_tid_t   last_tid;               /* 0078 Last transaction id */
649         hammer2_tid_t   alloc_tid;              /* 0080 Alloctable modify tid */
650         hammer2_blockref_t alloc_blockref;      /* 0088-00C7 */
651
652         /*
653          * Copyids are allocated dynamically from the copyexists bitmap.
654          * An id from the active copies set (up to 8, see copyinfo later on)
655          * may still exist after the copy set has been removed from the
656          * volume header and its bit will remain active in the bitmap and
657          * cannot be reused until it is 100% removed from the hierarchy.
658          */
659         uint32_t        copyexists[8];          /* 00C8-00E7 copy exists bmap */
660         char            reserved0140[248];      /* 00E8-01DF */
661
662         /*
663          * 32 bit CRC array at the end of the first 512 byte sector.
664          *
665          * icrc_sects[7] - First 512-4 bytes of volume header (including all
666          *                 the other icrc's except the last one).
667          *
668          * icrc_sects[6] - Second 512-4 bytes of volume header, which is
669          *                 the blockset for the root.
670          */
671         hammer2_crc32_t icrc_sects[8];          /* 01E0-01FF */
672
673         /*
674          * 512-byte sector #1
675          *
676          * The entire sector is used by a blockset.
677          */
678         hammer2_blockset_t sroot_blockset;      /* 0200 Superroot directory */
679
680         /*
681          * 512-byte sector #2-33
682          *
683          * Up to 256 copyinfo specifications can be configured.  Note that
684          * any given subdirectory tree can only use 8 of the 256.  Having
685          * up to 256 configurable in the volume header allows
686          *
687          * A specification takes 64 bytes.  Each specification typically
688          * configures a device path such as 'serno/<serial>.s1d'.
689          */
690         struct hammer2_copy_data copyinfo[256]; /* 0400-43FF copyinfo config */
691
692         /*
693          * Remaining sections are reserved for future use.
694          */
695         char            reserved0400[0xBBFC];   /* 4400-FFFB reserved */
696
697         /*
698          * icrc on entire volume header
699          */
700         hammer2_crc32_t icrc_volheader;         /* FFFC-FFFF full volume icrc*/
701 };
702
703 typedef struct hammer2_volume_data hammer2_volume_data_t;
704
705 /*
706  * Various parts of the volume header have their own iCRCs.
707  *
708  * The first 512 bytes has its own iCRC stored at the end of the 512 bytes
709  * and not included the icrc calculation.
710  *
711  * The second 512 bytes also has its own iCRC but it is stored in the first
712  * 512 bytes so it covers the entire second 512 bytes.
713  *
714  * The whole volume block (64KB) has an iCRC covering all but the last 4 bytes,
715  * which is where the iCRC for the whole volume is stored.  This is currently
716  * a catch-all for anything not individually iCRCd.
717  */
718 #define HAMMER2_VOL_ICRC_SECT0          7
719 #define HAMMER2_VOL_ICRC_SECT1          6
720
721 #define HAMMER2_VOLUME_BYTES            65536
722
723 #define HAMMER2_VOLUME_ICRC0_OFF        0
724 #define HAMMER2_VOLUME_ICRC1_OFF        512
725 #define HAMMER2_VOLUME_ICRCVH_OFF       0
726
727 #define HAMMER2_VOLUME_ICRC0_SIZE       (512 - 4)
728 #define HAMMER2_VOLUME_ICRC1_SIZE       (512)
729 #define HAMMER2_VOLUME_ICRCVH_SIZE      (65536 - 4)
730
731 #define HAMMER2_VOL_VERSION_MIN         1
732 #define HAMMER2_VOL_VERSION_DEFAULT     1
733 #define HAMMER2_VOL_VERSION_WIP         2
734
735 #define HAMMER2_NUM_VOLHDRS             4
736
737 union hammer2_media_data {
738         hammer2_inode_data_t    ipdata;
739         hammer2_indblock_data_t npdata;
740         char                    buf[HAMMER2_PBUFSIZE];
741 };
742
743 typedef union hammer2_media_data hammer2_media_data_t;
744
745 /*
746  * Prototypes for user & kernel functions.  Kernel-only prototypes are
747  * elsewhere.
748  */
749 uint32_t hammer2_icrc32(const void *buf, size_t size);
750 uint32_t hammer2_icrc32c(const void *buf, size_t size, uint32_t crc);
751
752 #endif