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