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