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