Merge branch 'vendor/WPA_SUPPLICANT'
[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.
188  *
189  *                      Freemap (see the FREEMAP document)
190  *
191  * The freemap utilizes blocks #1-16 for now, see the FREEMAP document.
192  * The filesystems rotations through the sections to avoid disturbing the
193  * 'previous' version of the freemap during a flush.
194  *
195  * Each freemap section is 4 x 64K blocks and represents 2GB, 2TB, 2PB,
196  * and 2EB indirect map, plus the volume header has a set of 8 blockrefs
197  * for another 3 bits for a total of 64 bits of address space.  The Level 0
198  * 64KB block representing 2GB of storage is a hammer2_bmap_data[1024].
199  * Each element contains a 128x2 bit bitmap representing 16KB per chunk for
200  * 2MB of storage (x1024 elements = 2GB).  2 bits per chunk:
201  *
202  *      00      Free
203  *      01      (reserved)
204  *      10      Possibly free
205  *      11      Allocated
206  *
207  * One important thing to note here is that the freemap resolution is 16KB,
208  * but the minimuim storage allocation size is 1KB.  The hammer2 vfs keeps
209  * track of sub-allocations in memory (on umount or reboot obvious the whole
210  * 16KB will be considered allocated even if only 1KB is allocated).  It is
211  * possible for fragmentation to build up over time.
212  *
213  * The Second thing to note is that due to the way snapshots and inode
214  * replication works, deleting a file cannot immediately free the related
215  * space.  Instead, the freemap elements transition from 11->10.  The bulk
216  * freeing code which does a complete scan is then responsible for
217  * transitioning the elements to 00 or back to 11 or to 01 for that matter.
218  *
219  * WARNING!  ZONE_SEG and VOLUME_ALIGN must be a multiple of 1<<LEVEL0_RADIX
220  *           (i.e. a multiple of 2MB).  VOLUME_ALIGN must be >= ZONE_SEG.
221  */
222 #define HAMMER2_VOLUME_ALIGN            (8 * 1024 * 1024)
223 #define HAMMER2_VOLUME_ALIGN64          ((hammer2_off_t)HAMMER2_VOLUME_ALIGN)
224 #define HAMMER2_VOLUME_ALIGNMASK        (HAMMER2_VOLUME_ALIGN - 1)
225 #define HAMMER2_VOLUME_ALIGNMASK64     ((hammer2_off_t)HAMMER2_VOLUME_ALIGNMASK)
226
227 #define HAMMER2_NEWFS_ALIGN             (HAMMER2_VOLUME_ALIGN)
228 #define HAMMER2_NEWFS_ALIGN64           ((hammer2_off_t)HAMMER2_VOLUME_ALIGN)
229 #define HAMMER2_NEWFS_ALIGNMASK         (HAMMER2_VOLUME_ALIGN - 1)
230 #define HAMMER2_NEWFS_ALIGNMASK64       ((hammer2_off_t)HAMMER2_NEWFS_ALIGNMASK)
231
232 #define HAMMER2_ZONE_BYTES64            (2LLU * 1024 * 1024 * 1024)
233 #define HAMMER2_ZONE_MASK64             (HAMMER2_ZONE_BYTES64 - 1)
234 #define HAMMER2_ZONE_SEG                (4 * 1024 * 1024)
235 #define HAMMER2_ZONE_SEG64              ((hammer2_off_t)HAMMER2_ZONE_SEG)
236 #define HAMMER2_ZONE_BLOCKS_SEG         (HAMMER2_ZONE_SEG / HAMMER2_PBUFSIZE)
237
238 /*
239  * 64 x 64KB blocks are reserved at the base of each 2GB zone.  These blocks
240  * are used to store the volume header or volume header backups, allocation
241  * tree, and other information in the future.
242  *
243  * All specified blocks are not necessarily used in all 2GB zones.  However,
244  * dead areas are reserved for future use and MUST NOT BE USED for other
245  * purposes.
246  *
247  * The freemap is arranged into 15 groups of 4x64KB each.  The 4 sub-groups
248  * are labeled ZONEFM1..4 and representing HAMMER2_FREEMAP_LEVEL{1-4}_RADIX,
249  * for the up to 4 levels of radix tree representing the freemap.  For
250  * simplicity we are reserving all four radix tree layers even though the
251  * higher layers do not require teh reservation at each 2GB mark.  That
252  * space is reserved for future use.
253  *
254  * Freemap blocks are not allocated dynamically but instead rotate through
255  * one of 15 possible copies.  We require 15 copies for several reasons:
256  *
257  * (1) For distinguishing freemap 'allocations' made by the current flush
258  *     verses the concurrently running front-end (at flush_tid + 1).  This
259  *     theoretically requires two copies but the algorithm is greatly
260  *     simplified if we use three.
261  *
262  * (2) There are up to 4 copies of the volume header (iterated on each flush),
263  *     and if the mount code is forced to use an older copy due to corruption
264  *     we must be sure that the state of the freemap AS-OF the earlier copy
265  *     remains valid.
266  *
267  *     This means 3 copies x 4 flushes = 12 copies to be able to mount any
268  *     of the four volume header backups after on boot or after a crash.
269  *
270  * (3) Freemap recovery on-mount eats a copy.  We don't want freemap recovery
271  *     to blow away the copy used by some other volume header in case H2
272  *     crashes during the recovery.  Total is now 13.
273  *
274  * (4) And I want some breathing room to ensure that complex flushes do not
275  *     cause problems.  Also note that bulk block freeing itself must be
276  *     careful so even on a live system, post-mount, the four volume header
277  *     backups effectively represent short-lived snapshots.  And I only
278  *     have room for 15 copies so it works out.
279  *
280  * Preferably I would like to improve the algorithm to only use 2 copies per
281  * volume header (which would be a total of 2 x 4 = 8 + 1 for freemap recovery
282  * + 1 for breathing room = 10 total instead of 15).  For now we use 15.
283  */
284 #define HAMMER2_ZONE_VOLHDR             0       /* volume header or backup */
285 #define HAMMER2_ZONE_FREEMAP_00         1
286 #define HAMMER2_ZONE_FREEMAP_01         5
287 #define HAMMER2_ZONE_FREEMAP_02         9
288 #define HAMMER2_ZONE_FREEMAP_03         13
289 #define HAMMER2_ZONE_FREEMAP_04         17
290 #define HAMMER2_ZONE_FREEMAP_05         21
291 #define HAMMER2_ZONE_FREEMAP_06         25
292 #define HAMMER2_ZONE_FREEMAP_07         29
293 #define HAMMER2_ZONE_FREEMAP_08         33
294 #define HAMMER2_ZONE_FREEMAP_09         37
295 #define HAMMER2_ZONE_FREEMAP_10         41
296 #define HAMMER2_ZONE_FREEMAP_11         45
297 #define HAMMER2_ZONE_FREEMAP_12         49
298 #define HAMMER2_ZONE_FREEMAP_13         53
299 #define HAMMER2_ZONE_FREEMAP_14         57
300 #define HAMMER2_ZONE_FREEMAP_END        61      /* (non-inclusive) */
301 #define HAMMER2_ZONE_UNUSED62           62
302 #define HAMMER2_ZONE_UNUSED63           63
303
304 #define HAMMER2_ZONE_FREEMAP_COPIES     15
305                                                 /* relative to FREEMAP_x */
306 #define HAMMER2_ZONEFM_LEVEL1           0       /* 2GB leafmap */
307 #define HAMMER2_ZONEFM_LEVEL2           1       /* 2TB indmap */
308 #define HAMMER2_ZONEFM_LEVEL3           2       /* 2PB indmap */
309 #define HAMMER2_ZONEFM_LEVEL4           3       /* 2EB indmap */
310 /* LEVEL5 is a set of 8 blockrefs in the volume header 16EB */
311
312
313 /*
314  * Freemap radii.  Please note that LEVEL 1 blockref array entries
315  * point to 256-byte sections of the bitmap representing 2MB of storage.
316  * Even though the chain structures represent only 256 bytes, they are
317  * mapped using larger 16K or 64K buffer cache buffers.
318  */
319 #define HAMMER2_FREEMAP_LEVEL5_RADIX    64      /* 16EB */
320 #define HAMMER2_FREEMAP_LEVEL4_RADIX    61      /* 2EB */
321 #define HAMMER2_FREEMAP_LEVEL3_RADIX    51      /* 2PB */
322 #define HAMMER2_FREEMAP_LEVEL2_RADIX    41      /* 2TB */
323 #define HAMMER2_FREEMAP_LEVEL1_RADIX    31      /* 2GB */
324 #define HAMMER2_FREEMAP_LEVEL0_RADIX    21      /* 2MB (entry in l-1 leaf) */
325
326 #define HAMMER2_FREEMAP_LEVELN_PSIZE    65536   /* physical bytes */
327
328 #define HAMMER2_FREEMAP_COUNT           (int)(HAMMER2_FREEMAP_LEVELN_PSIZE / \
329                                          sizeof(hammer2_bmap_data_t))
330 #define HAMMER2_FREEMAP_BLOCK_RADIX     14
331 #define HAMMER2_FREEMAP_BLOCK_SIZE      (1 << HAMMER2_FREEMAP_BLOCK_RADIX)
332 #define HAMMER2_FREEMAP_BLOCK_MASK      (HAMMER2_FREEMAP_BLOCK_SIZE - 1)
333
334 /*
335  * Two linear areas can be reserved after the initial 2MB segment in the base
336  * zone (the one starting at offset 0).  These areas are NOT managed by the
337  * block allocator and do not fall under HAMMER2 crc checking rules based
338  * at the volume header (but can be self-CRCd internally, depending).
339  */
340 #define HAMMER2_BOOT_MIN_BYTES          HAMMER2_VOLUME_ALIGN
341 #define HAMMER2_BOOT_NOM_BYTES          (64*1024*1024)
342 #define HAMMER2_BOOT_MAX_BYTES          (256*1024*1024)
343
344 #define HAMMER2_REDO_MIN_BYTES          HAMMER2_VOLUME_ALIGN
345 #define HAMMER2_REDO_NOM_BYTES          (256*1024*1024)
346 #define HAMMER2_REDO_MAX_BYTES          (1024*1024*1024)
347
348 /*
349  * Most HAMMER2 types are implemented as unsigned 64-bit integers.
350  * Transaction ids are monotonic.
351  *
352  * We utilize 32-bit iSCSI CRCs.
353  */
354 typedef uint64_t hammer2_tid_t;
355 typedef uint64_t hammer2_off_t;
356 typedef uint64_t hammer2_key_t;
357 typedef uint32_t hammer2_crc32_t;
358
359 /*
360  * Miscellanious ranges (all are unsigned).
361  */
362 #define HAMMER2_TID_MIN         1ULL
363 #define HAMMER2_TID_MAX         0xFFFFFFFFFFFFFFFFULL
364 #define HAMMER2_KEY_MIN         0ULL
365 #define HAMMER2_KEY_MAX         0xFFFFFFFFFFFFFFFFULL
366 #define HAMMER2_OFFSET_MIN      0ULL
367 #define HAMMER2_OFFSET_MAX      0xFFFFFFFFFFFFFFFFULL
368
369 /*
370  * HAMMER2 data offset special cases and masking.
371  *
372  * All HAMMER2 data offsets have to be broken down into a 64K buffer base
373  * offset (HAMMER2_OFF_MASK_HI) and a 64K buffer index (HAMMER2_OFF_MASK_LO).
374  *
375  * Indexes into physical buffers are always 64-byte aligned.  The low 6 bits
376  * of the data offset field specifies how large the data chunk being pointed
377  * to as a power of 2.  The theoretical minimum radix is thus 6 (The space
378  * needed in the low bits of the data offset field).  However, the practical
379  * minimum allocation chunk size is 1KB (a radix of 10), so HAMMER2 sets
380  * HAMMER2_RADIX_MIN to 10.  The maximum radix is currently 16 (64KB), but
381  * we fully intend to support larger extents in the future.
382  */
383 #define HAMMER2_OFF_BAD         ((hammer2_off_t)-1)
384 #define HAMMER2_OFF_MASK        0xFFFFFFFFFFFFFFC0ULL
385 #define HAMMER2_OFF_MASK_LO     (HAMMER2_OFF_MASK & HAMMER2_PBUFMASK64)
386 #define HAMMER2_OFF_MASK_HI     (~HAMMER2_PBUFMASK64)
387 #define HAMMER2_OFF_MASK_RADIX  0x000000000000003FULL
388 #define HAMMER2_MAX_COPIES      6
389
390 /*
391  * HAMMER2 directory support and pre-defined keys
392  */
393 #define HAMMER2_DIRHASH_VISIBLE 0x8000000000000000ULL
394 #define HAMMER2_DIRHASH_USERMSK 0x7FFFFFFFFFFFFFFFULL
395 #define HAMMER2_DIRHASH_LOMASK  0x0000000000007FFFULL
396 #define HAMMER2_DIRHASH_HIMASK  0xFFFFFFFFFFFF0000ULL
397 #define HAMMER2_DIRHASH_FORCED  0x0000000000008000ULL   /* bit forced on */
398
399 #define HAMMER2_SROOT_KEY       0x0000000000000000ULL   /* volume to sroot */
400
401 /************************************************************************
402  *                              DMSG SUPPORT                            *
403  ************************************************************************
404  * LNK_VOLCONF
405  *
406  * All HAMMER2 directories directly under the super-root on your local
407  * media can be mounted separately, even if they share the same physical
408  * device.
409  *
410  * When you do a HAMMER2 mount you are effectively tying into a HAMMER2
411  * cluster via local media.  The local media does not have to participate
412  * in the cluster, other than to provide the hammer2_volconf[] array and
413  * root inode for the mount.
414  *
415  * This is important: The mount device path you specify serves to bootstrap
416  * your entry into the cluster, but your mount will make active connections
417  * to ALL copy elements in the hammer2_volconf[] array which match the
418  * PFSID of the directory in the super-root that you specified.  The local
419  * media path does not have to be mentioned in this array but becomes part
420  * of the cluster based on its type and access rights.  ALL ELEMENTS ARE
421  * TREATED ACCORDING TO TYPE NO MATTER WHICH ONE YOU MOUNT FROM.
422  *
423  * The actual cluster may be far larger than the elements you list in the
424  * hammer2_volconf[] array.  You list only the elements you wish to
425  * directly connect to and you are able to access the rest of the cluster
426  * indirectly through those connections.
427  *
428  * WARNING!  This structure must be exactly 128 bytes long for its config
429  *           array to fit in the volume header.
430  */
431 struct hammer2_volconf {
432         uint8_t copyid;         /* 00    copyid 0-255 (must match slot) */
433         uint8_t inprog;         /* 01    operation in progress, or 0 */
434         uint8_t chain_to;       /* 02    operation chaining to, or 0 */
435         uint8_t chain_from;     /* 03    operation chaining from, or 0 */
436         uint16_t flags;         /* 04-05 flags field */
437         uint8_t error;          /* 06    last operational error */
438         uint8_t priority;       /* 07    priority and round-robin flag */
439         uint8_t remote_pfs_type;/* 08    probed direct remote PFS type */
440         uint8_t reserved08[23]; /* 09-1F */
441         uuid_t  pfs_clid;       /* 20-2F copy target must match this uuid */
442         uint8_t label[16];      /* 30-3F import/export label */
443         uint8_t path[64];       /* 40-7F target specification string or key */
444 };
445
446 typedef struct hammer2_volconf hammer2_volconf_t;
447
448 #define DMSG_VOLF_ENABLED       0x0001
449 #define DMSG_VOLF_INPROG        0x0002
450 #define DMSG_VOLF_CONN_RR       0x80    /* round-robin at same priority */
451 #define DMSG_VOLF_CONN_EF       0x40    /* media errors flagged */
452 #define DMSG_VOLF_CONN_PRI      0x0F    /* select priority 0-15 (15=best) */
453
454 struct dmsg_lnk_hammer2_volconf {
455         dmsg_hdr_t              head;
456         hammer2_volconf_t       copy;   /* copy spec */
457         int32_t                 index;
458         int32_t                 unused01;
459         uuid_t                  mediaid;
460         int64_t                 reserved02[32];
461 };
462
463 typedef struct dmsg_lnk_hammer2_volconf dmsg_lnk_hammer2_volconf_t;
464
465 #define DMSG_LNK_HAMMER2_VOLCONF DMSG_LNK(DMSG_LNK_CMD_HAMMER2_VOLCONF, \
466                                           dmsg_lnk_hammer2_volconf)
467
468 #define H2_LNK_VOLCONF(msg)     ((dmsg_lnk_hammer2_volconf_t *)(msg)->any.buf)
469
470 /*
471  * The media block reference structure.  This forms the core of the HAMMER2
472  * media topology recursion.  This 64-byte data structure is embedded in the
473  * volume header, in inodes (which are also directory entries), and in
474  * indirect blocks.
475  *
476  * A blockref references a single media item, which typically can be a
477  * directory entry (aka inode), indirect block, or data block.
478  *
479  * The primary feature a blockref represents is the ability to validate
480  * the entire tree underneath it via its check code.  Any modification to
481  * anything propagates up the blockref tree all the way to the root, replacing
482  * the related blocks.  Propagations can shortcut to the volume root to
483  * implement the 'fast syncing' feature but this only delays the eventual
484  * propagation.
485  *
486  * The check code can be a simple 32-bit iscsi code, a 64-bit crc,
487  * or as complex as a 192 bit cryptographic hash.  192 bits is the maximum
488  * supported check code size, which is not sufficient for unverified dedup
489  * UNLESS one doesn't mind once-in-a-blue-moon data corruption (such as when
490  * farming web data).  HAMMER2 has an unverified dedup feature for just this
491  * purpose.
492  *
493  * --
494  *
495  * NOTE: The range of keys represented by the blockref is (key) to
496  *       ((key) + (1LL << keybits) - 1).  HAMMER2 usually populates
497  *       blocks bottom-up, inserting a new root when radix expansion
498  *       is required.
499  */
500 struct hammer2_blockref {               /* MUST BE EXACTLY 64 BYTES */
501         uint8_t         type;           /* type of underlying item */
502         uint8_t         methods;        /* check method & compression method */
503         uint8_t         copyid;         /* specify which copy this is */
504         uint8_t         keybits;        /* #of keybits masked off 0=leaf */
505         uint8_t         vradix;         /* virtual data/meta-data size */
506         uint8_t         flags;          /* blockref flags */
507         uint8_t         reserved06;
508         uint8_t         reserved07;
509         hammer2_key_t   key;            /* key specification */
510         hammer2_tid_t   mirror_tid;     /* propagate for mirror scan */
511         hammer2_tid_t   modify_tid;     /* modifications sans propagation */
512         hammer2_off_t   data_off;       /* low 6 bits is phys size (radix)*/
513         union {                         /* check info */
514                 char    buf[24];
515                 struct {
516                         uint32_t value;
517                         uint32_t unused[5];
518                 } iscsi32;
519                 struct {
520                         uint64_t value;
521                         uint64_t unused[2];
522                 } crc64;
523                 struct {
524                         char data[24];
525                 } sha192;
526
527                 /*
528                  * Freemap hints are embedded in addition to the icrc32.
529                  *
530                  * bigmask - Radixes available for allocation (0-31).
531                  *           Heuristical (may be permissive but not
532                  *           restrictive).  Typically only radix values
533                  *           10-16 are used (i.e. (1<<10) through (1<<16)).
534                  *
535                  * avail   - Total available space remaining, in bytes
536                  */
537                 struct {
538                         uint32_t icrc32;
539                         uint32_t bigmask;       /* available radixes */
540                         uint64_t avail;         /* total available bytes */
541                         uint64_t unused;        /* unused must be 0 */
542                 } freemap;
543
544                 /*
545                  * Debugging
546                  */
547                 struct {
548                         hammer2_tid_t sync_tid;
549                 } debug;
550         } check;
551 };
552
553 typedef struct hammer2_blockref hammer2_blockref_t;
554
555 #define HAMMER2_BLOCKREF_BYTES          64      /* blockref struct in bytes */
556
557 /*
558  * On-media and off-media blockref types.
559  */
560 #define HAMMER2_BREF_TYPE_EMPTY         0
561 #define HAMMER2_BREF_TYPE_INODE         1
562 #define HAMMER2_BREF_TYPE_INDIRECT      2
563 #define HAMMER2_BREF_TYPE_DATA          3
564 #define HAMMER2_BREF_TYPE_UNUSED04      4
565 #define HAMMER2_BREF_TYPE_FREEMAP_NODE  5
566 #define HAMMER2_BREF_TYPE_FREEMAP_LEAF  6
567 #define HAMMER2_BREF_TYPE_FREEMAP       254     /* pseudo-type */
568 #define HAMMER2_BREF_TYPE_VOLUME        255     /* pseudo-type */
569
570 #define HAMMER2_BREF_FLAG_PFSROOT       0x01    /* see also related opflag */
571
572 #define HAMMER2_ENC_CHECK(n)            ((n) << 4)
573 #define HAMMER2_DEC_CHECK(n)            (((n) >> 4) & 15)
574
575 #define HAMMER2_CHECK_NONE              0
576 #define HAMMER2_CHECK_ISCSI32           1
577 #define HAMMER2_CHECK_CRC64             2
578 #define HAMMER2_CHECK_SHA192            3
579 #define HAMMER2_CHECK_FREEMAP           4
580
581 #define HAMMER2_ENC_COMP(n)             (n)
582 #define HAMMER2_ENC_LEVEL(n)            ((n) << 4)
583 #define HAMMER2_DEC_COMP(n)             ((n) & 15)
584 #define HAMMER2_DEC_LEVEL(n)            (((n) >> 4) & 15)
585
586 #define HAMMER2_COMP_NONE               0
587 #define HAMMER2_COMP_AUTOZERO           1
588 #define HAMMER2_COMP_LZ4                2
589 #define HAMMER2_COMP_ZLIB               3
590
591 #define HAMMER2_COMP_NEWFS_DEFAULT      HAMMER2_COMP_LZ4
592 #define HAMMER2_COMP_STRINGS            { "none", "autozero", "lz4", "zlib" }
593 #define HAMMER2_COMP_STRINGS_COUNT      4
594
595
596 /*
597  * HAMMER2 block references are collected into sets of 8 blockrefs.  These
598  * sets are fully associative, meaning the elements making up a set are
599  * not sorted in any way and may contain duplicate entries, holes, or
600  * entries which shortcut multiple levels of indirection.  Sets are used
601  * in various ways:
602  *
603  * (1) When redundancy is desired a set may contain several duplicate
604  *     entries pointing to different copies of the same data.  Up to 8 copies
605  *     are supported but the set structure becomes a bit inefficient once
606  *     you go over 4.
607  *
608  * (2) The blockrefs in a set can shortcut multiple levels of indirections
609  *     within the bounds imposed by the parent of set.
610  *
611  * When a set fills up another level of indirection is inserted, moving
612  * some or all of the set's contents into indirect blocks placed under the
613  * set.  This is a top-down approach in that indirect blocks are not created
614  * until the set actually becomes full (that is, the entries in the set can
615  * shortcut the indirect blocks when the set is not full).  Depending on how
616  * things are filled multiple indirect blocks will eventually be created.
617  *
618  * Indirect blocks are typically 4KB (64 entres) or 64KB (1024 entries) and
619  * are also treated as fully set-associative.
620  */
621 struct hammer2_blockset {
622         hammer2_blockref_t      blockref[HAMMER2_SET_COUNT];
623 };
624
625 typedef struct hammer2_blockset hammer2_blockset_t;
626
627 /*
628  * Catch programmer snafus
629  */
630 #if (1 << HAMMER2_SET_RADIX) != HAMMER2_SET_COUNT
631 #error "hammer2 direct radix is incorrect"
632 #endif
633 #if (1 << HAMMER2_PBUFRADIX) != HAMMER2_PBUFSIZE
634 #error "HAMMER2_PBUFRADIX and HAMMER2_PBUFSIZE are inconsistent"
635 #endif
636 #if (1 << HAMMER2_RADIX_MIN) != HAMMER2_ALLOC_MIN
637 #error "HAMMER2_RADIX_MIN and HAMMER2_ALLOC_MIN are inconsistent"
638 #endif
639
640 /*
641  * hammer2_bmap_data - A freemap entry in the LEVEL1 block.
642  *
643  * Each 64-byte entry contains the bitmap and meta-data required to manage
644  * a LEVEL0 (2MB) block of storage.  The storage is managed in 128 x 16KB
645  * chunks.  Smaller allocation granularity is supported via a linear iterator
646  * and/or must otherwise be tracked in ram.
647  *
648  * (data structure must be 64 bytes exactly)
649  *
650  * linear  - A BYTE linear allocation offset used for sub-16KB allocations
651  *           only.  May contain values between 0 and 2MB.  Must be ignored
652  *           if 16KB-aligned (i.e. force bitmap scan), otherwise may be
653  *           used to sub-allocate within the 16KB block (which is already
654  *           marked as allocated in the bitmap).
655  *
656  *           Sub-allocations need only be 1KB-aligned and do not have to be
657  *           size-aligned, and 16KB or larger allocations do not update this
658  *           field, resulting in pretty good packing.
659  *
660  *           Please note that file data granularity may be limited by
661  *           other issues such as buffer cache direct-mapping and the
662  *           desire to support sector sizes up to 16KB (so H2 only issues
663  *           I/O's in multiples of 16KB anyway).
664  *
665  * class   - Clustering class.  Cleared to 0 only if the entire leaf becomes
666  *           free.  Used to cluster device buffers so all elements must have
667  *           the same device block size, but may mix logical sizes.
668  *
669  *           Typically integrated with the blockref type in the upper 8 bits
670  *           to localize inodes and indrect blocks, improving bulk free scans
671  *           and directory scans.
672  *
673  * bitmap  - Two bits per 16KB allocation block arranged in arrays of
674  *           32-bit elements, 128x2 bits representing ~2MB worth of media
675  *           storage.  Bit patterns are as follows:
676  *
677  *           00 Unallocated
678  *           01 (reserved)
679  *           10 Possibly free
680  *           11 Allocated
681  */
682 struct hammer2_bmap_data {
683         int32_t linear;         /* 00 linear sub-granular allocation offset */
684         uint16_t class;         /* 04-05 clustering class ((type<<8)|radix) */
685         uint8_t reserved06;     /* 06 */
686         uint8_t reserved07;     /* 07 */
687         uint32_t reserved08;    /* 08 */
688         uint32_t reserved0C;    /* 0C */
689         uint32_t reserved10;    /* 10 */
690         uint32_t reserved14;    /* 14 */
691         uint32_t reserved18;    /* 18 */
692         uint32_t avail;         /* 1C */
693         uint32_t bitmap[8];     /* 20-3F 256 bits manages 2MB/16KB/2-bits */
694 };
695
696 typedef struct hammer2_bmap_data hammer2_bmap_data_t;
697
698 /*
699  * In HAMMER2 inodes ARE directory entries, with a special exception for
700  * hardlinks.  The inode number is stored in the inode rather than being
701  * based on the location of the inode (since the location moves every time
702  * the inode or anything underneath the inode is modified).
703  *
704  * The inode is 1024 bytes, made up of 256 bytes of meta-data, 256 bytes
705  * for the filename, and 512 bytes worth of direct file data OR an embedded
706  * blockset.
707  *
708  * Directories represent one inode per blockref.  Inodes are not laid out
709  * as a file but instead are represented by the related blockrefs.  The
710  * blockrefs, in turn, are indexed by the 64-bit directory hash key.  Remember
711  * that blocksets are fully associative, so a certain degree efficiency is
712  * achieved just from that.
713  *
714  * Up to 512 bytes of direct data can be embedded in an inode, and since
715  * inodes are essentially directory entries this also means that small data
716  * files end up simply being laid out linearly in the directory, resulting
717  * in fewer seeks and highly optimal access.
718  *
719  * The compression mode can be changed at any time in the inode and is
720  * recorded on a blockref-by-blockref basis.
721  *
722  * Hardlinks are supported via the inode map.  Essentially the way a hardlink
723  * works is that all individual directory entries representing the same file
724  * are special cased and specify the same inode number.  The actual file
725  * is placed in the nearest parent directory that is parent to all instances
726  * of the hardlink.  If all hardlinks to a file are in the same directory
727  * the actual file will also be placed in that directory.  This file uses
728  * the inode number as the directory entry key and is invisible to normal
729  * directory scans.  Real directory entry keys are differentiated from the
730  * inode number key via bit 63.  Access to the hardlink silently looks up
731  * the real file and forwards all operations to that file.  Removal of the
732  * last hardlink also removes the real file.
733  *
734  * (attr_tid) is only updated when the inode's specific attributes or regular
735  * file size has changed, and affects path lookups and stat.  (attr_tid)
736  * represents a special cache coherency lock under the inode.  The inode
737  * blockref's modify_tid will always cover it.
738  *
739  * (dirent_tid) is only updated when an entry under a directory inode has
740  * been created, deleted, renamed, or had its attributes change, and affects
741  * directory lookups and scans.  (dirent_tid) represents another special cache
742  * coherency lock under the inode.  The inode blockref's modify_tid will
743  * always cover it.
744  */
745 #define HAMMER2_INODE_BYTES             1024    /* (asserted by code) */
746 #define HAMMER2_INODE_MAXNAME           256     /* maximum name in bytes */
747 #define HAMMER2_INODE_VERSION_ONE       1
748
749 #define HAMMER2_INODE_HIDDENDIR         16      /* special inode */
750 #define HAMMER2_INODE_START             1024    /* dynamically allocated */
751
752 struct hammer2_inode_data {
753         uint16_t        version;        /* 0000 inode data version */
754         uint16_t        reserved02;     /* 0002 */
755
756         /*
757          * core inode attributes, inode type, misc flags
758          */
759         uint32_t        uflags;         /* 0004 chflags */
760         uint32_t        rmajor;         /* 0008 available for device nodes */
761         uint32_t        rminor;         /* 000C available for device nodes */
762         uint64_t        ctime;          /* 0010 inode change time */
763         uint64_t        mtime;          /* 0018 modified time */
764         uint64_t        atime;          /* 0020 access time (unsupported) */
765         uint64_t        btime;          /* 0028 birth time */
766         uuid_t          uid;            /* 0030 uid / degenerate unix uid */
767         uuid_t          gid;            /* 0040 gid / degenerate unix gid */
768
769         uint8_t         type;           /* 0050 object type */
770         uint8_t         op_flags;       /* 0051 operational flags */
771         uint16_t        cap_flags;      /* 0052 capability flags */
772         uint32_t        mode;           /* 0054 unix modes (typ low 16 bits) */
773
774         /*
775          * inode size, identification, localized recursive configuration
776          * for compression and backup copies.
777          */
778         hammer2_tid_t   inum;           /* 0058 inode number */
779         hammer2_off_t   size;           /* 0060 size of file */
780         uint64_t        nlinks;         /* 0068 hard links (typ only dirs) */
781         hammer2_tid_t   iparent;        /* 0070 parent inum (recovery only) */
782         hammer2_key_t   name_key;       /* 0078 full filename key */
783         uint16_t        name_len;       /* 0080 filename length */
784         uint8_t         ncopies;        /* 0082 ncopies to local media */
785         uint8_t         comp_algo;      /* 0083 compression request & algo */
786
787         /*
788          * These fields are currently only applicable to PFSROOTs.
789          *
790          * NOTE: We can't use {volume_data->fsid, pfs_clid} to uniquely
791          *       identify an instance of a PFS in the cluster because
792          *       a mount may contain more than one copy of the PFS as
793          *       a separate node.  {pfs_clid, pfs_fsid} must be used for
794          *       registration in the cluster.
795          */
796         uint8_t         target_type;    /* 0084 hardlink target type */
797         uint8_t         reserved85;     /* 0085 */
798         uint8_t         pfs_nmasters;   /* 0086 (if PFSROOT) if multi-master */
799         uint8_t         pfs_type;       /* 0087 (if PFSROOT) node type */
800         uint64_t        pfs_inum;       /* 0088 (if PFSROOT) inum allocator */
801         uuid_t          pfs_clid;       /* 0090 (if PFSROOT) cluster uuid */
802         uuid_t          pfs_fsid;       /* 00A0 (if PFSROOT) unique uuid */
803
804         /*
805          * Quotas and cumulative sub-tree counters.
806          */
807         hammer2_key_t   data_quota;     /* 00B0 subtree quota in bytes */
808         hammer2_key_t   data_count;     /* 00B8 subtree byte count */
809         hammer2_key_t   inode_quota;    /* 00C0 subtree quota inode count */
810         hammer2_key_t   inode_count;    /* 00C8 subtree inode count */
811         hammer2_tid_t   attr_tid;       /* 00D0 attributes changed */
812         hammer2_tid_t   dirent_tid;     /* 00D8 directory/attr changed */
813
814         /*
815          * Tracks (possibly degenerate) free areas covering all sub-tree
816          * allocations under inode, not counting the inode itself.
817          * 0/0 indicates empty entry.  fully set-associative.
818          */
819         hammer2_off_t   reservedE0[4];  /* 00E0/E8/F0/F8 */
820
821         unsigned char   filename[HAMMER2_INODE_MAXNAME];
822                                         /* 0100-01FF (256 char, unterminated) */
823         union {                         /* 0200-03FF (64x8 = 512 bytes) */
824                 struct hammer2_blockset blockset;
825                 char data[HAMMER2_EMBEDDED_BYTES];
826         } u;
827 };
828
829 typedef struct hammer2_inode_data hammer2_inode_data_t;
830
831 #define HAMMER2_OPFLAG_DIRECTDATA       0x01
832 #define HAMMER2_OPFLAG_PFSROOT          0x02    /* (see also bref flag) */
833 #define HAMMER2_OPFLAG_COPYIDS          0x04    /* copyids override parent */
834
835 #define HAMMER2_OBJTYPE_UNKNOWN         0
836 #define HAMMER2_OBJTYPE_DIRECTORY       1
837 #define HAMMER2_OBJTYPE_REGFILE         2
838 #define HAMMER2_OBJTYPE_FIFO            4
839 #define HAMMER2_OBJTYPE_CDEV            5
840 #define HAMMER2_OBJTYPE_BDEV            6
841 #define HAMMER2_OBJTYPE_SOFTLINK        7
842 #define HAMMER2_OBJTYPE_HARDLINK        8       /* dummy entry for hardlink */
843 #define HAMMER2_OBJTYPE_SOCKET          9
844 #define HAMMER2_OBJTYPE_WHITEOUT        10
845
846 #define HAMMER2_COPYID_NONE             0
847 #define HAMMER2_COPYID_LOCAL            ((uint8_t)-1)
848
849 #define HAMMER2_COPYID_COUNT            256
850
851 /*
852  * PFS types identify a PFS on media and in LNK_SPAN messages.
853  *
854  * PFS types >= 16 belong to HAMMER, 0-15 are defined in sys/dmsg.h
855  */
856 /* 0-15 reserved by sys/dmsg.h */
857 #define HAMMER2_PFSTYPE_CACHE           16
858 #define HAMMER2_PFSTYPE_COPY            17
859 #define HAMMER2_PFSTYPE_SLAVE           18
860 #define HAMMER2_PFSTYPE_SOFT_SLAVE      19
861 #define HAMMER2_PFSTYPE_SOFT_MASTER     20
862 #define HAMMER2_PFSTYPE_MASTER          21
863 #define HAMMER2_PFSTYPE_SNAPSHOT        22
864 #define HAMMER2_PFSTYPE_SUPROOT         23
865 #define HAMMER2_PFSTYPE_MAX             32
866
867 /*
868  *                              Allocation Table
869  *
870  */
871
872
873 /*
874  * Flags (8 bits) - blockref, for freemap only
875  *
876  * Note that the minimum chunk size is 1KB so we could theoretically have
877  * 10 bits here, but we might have some future extension that allows a
878  * chunk size down to 256 bytes and if so we will need bits 8 and 9.
879  */
880 #define HAMMER2_AVF_SELMASK             0x03    /* select group */
881 #define HAMMER2_AVF_ALL_ALLOC           0x04    /* indicate all allocated */
882 #define HAMMER2_AVF_ALL_FREE            0x08    /* indicate all free */
883 #define HAMMER2_AVF_RESERVED10          0x10
884 #define HAMMER2_AVF_RESERVED20          0x20
885 #define HAMMER2_AVF_RESERVED40          0x40
886 #define HAMMER2_AVF_RESERVED80          0x80
887 #define HAMMER2_AVF_AVMASK32            ((uint32_t)0xFFFFFF00LU)
888 #define HAMMER2_AVF_AVMASK64            ((uint64_t)0xFFFFFFFFFFFFFF00LLU)
889
890 #define HAMMER2_AV_SELECT_A             0x00
891 #define HAMMER2_AV_SELECT_B             0x01
892 #define HAMMER2_AV_SELECT_C             0x02
893 #define HAMMER2_AV_SELECT_D             0x03
894
895 /*
896  * The volume header eats a 64K block.  There is currently an issue where
897  * we want to try to fit all nominal filesystem updates in a 512-byte section
898  * but it may be a lost cause due to the need for a blockset.
899  *
900  * All information is stored in host byte order.  The volume header's magic
901  * number may be checked to determine the byte order.  If you wish to mount
902  * between machines w/ different endian modes you'll need filesystem code
903  * which acts on the media data consistently (either all one way or all the
904  * other).  Our code currently does not do that.
905  *
906  * A read-write mount may have to recover missing allocations by doing an
907  * incremental mirror scan looking for modifications made after alloc_tid.
908  * If alloc_tid == last_tid then no recovery operation is needed.  Recovery
909  * operations are usually very, very fast.
910  *
911  * Read-only mounts do not need to do any recovery, access to the filesystem
912  * topology is always consistent after a crash (is always consistent, period).
913  * However, there may be shortcutted blockref updates present from deep in
914  * the tree which are stored in the volumeh eader and must be tracked on
915  * the fly.
916  *
917  * NOTE: The copyinfo[] array contains the configuration for both the
918  *       cluster connections and any local media copies.  The volume
919  *       header will be replicated for each local media copy.
920  *
921  *       The mount command may specify multiple medias or just one and
922  *       allow HAMMER2 to pick up the others when it checks the copyinfo[]
923  *       array on mount.
924  *
925  * NOTE: root_blockref points to the super-root directory, not the root
926  *       directory.  The root directory will be a subdirectory under the
927  *       super-root.
928  *
929  *       The super-root directory contains all root directories and all
930  *       snapshots (readonly or writable).  It is possible to do a
931  *       null-mount of the super-root using special path constructions
932  *       relative to your mounted root.
933  *
934  * NOTE: HAMMER2 allows any subdirectory tree to be managed as if it were
935  *       a PFS, including mirroring and storage quota operations, and this is
936  *       prefered over creating discrete PFSs in the super-root.  Instead
937  *       the super-root is most typically used to create writable snapshots,
938  *       alternative roots, and so forth.  The super-root is also used by
939  *       the automatic snapshotting mechanism.
940  */
941 #define HAMMER2_VOLUME_ID_HBO   0x48414d3205172011LLU
942 #define HAMMER2_VOLUME_ID_ABO   0x11201705324d4148LLU
943
944 struct hammer2_volume_data {
945         /*
946          * sector #0 - 512 bytes
947          */
948         uint64_t        magic;                  /* 0000 Signature */
949         hammer2_off_t   boot_beg;               /* 0008 Boot area (future) */
950         hammer2_off_t   boot_end;               /* 0010 (size = end - beg) */
951         hammer2_off_t   aux_beg;                /* 0018 Aux area (future) */
952         hammer2_off_t   aux_end;                /* 0020 (size = end - beg) */
953         hammer2_off_t   volu_size;              /* 0028 Volume size, bytes */
954
955         uint32_t        version;                /* 0030 */
956         uint32_t        flags;                  /* 0034 */
957         uint8_t         copyid;                 /* 0038 copyid of phys vol */
958         uint8_t         freemap_version;        /* 0039 freemap algorithm */
959         uint8_t         peer_type;              /* 003A HAMMER2_PEER_xxx */
960         uint8_t         reserved003B;           /* 003B */
961         uint32_t        reserved003C;           /* 003C */
962
963         uuid_t          fsid;                   /* 0040 */
964         uuid_t          fstype;                 /* 0050 */
965
966         /*
967          * allocator_size is precalculated at newfs time and does not include
968          * reserved blocks, boot, or redo areas.
969          *
970          * Initial non-reserved-area allocations do not use the freemap
971          * but instead adjust alloc_iterator.  Dynamic allocations take
972          * over starting at (allocator_beg).  This makes newfs_hammer2's
973          * job a lot easier and can also serve as a testing jig.
974          */
975         hammer2_off_t   allocator_size;         /* 0060 Total data space */
976         hammer2_off_t   allocator_free;         /* 0068 Free space */
977         hammer2_off_t   allocator_beg;          /* 0070 Initial allocations */
978
979         /*
980          * mirror_tid reflects the highest committed super-root change
981          * freemap_tid reflects the highest committed freemap change
982          *
983          * NOTE: mirror_tid does not track (and should not track) changes
984          *       made to or under PFS roots.
985          */
986         hammer2_tid_t   mirror_tid;             /* 0078 committed tid (vol) */
987         hammer2_tid_t   reserved0080;           /* 0080 */
988         hammer2_tid_t   reserved0088;           /* 0088 */
989         hammer2_tid_t   freemap_tid;            /* 0090 committed tid (fmap) */
990         hammer2_tid_t   bulkfree_tid;           /* 0098 bulkfree incremental */
991         hammer2_tid_t   reserved00A0[5];        /* 00A0-00C7 */
992
993         /*
994          * Copyids are allocated dynamically from the copyexists bitmap.
995          * An id from the active copies set (up to 8, see copyinfo later on)
996          * may still exist after the copy set has been removed from the
997          * volume header and its bit will remain active in the bitmap and
998          * cannot be reused until it is 100% removed from the hierarchy.
999          */
1000         uint32_t        copyexists[8];          /* 00C8-00E7 copy exists bmap */
1001         char            reserved0140[248];      /* 00E8-01DF */
1002
1003         /*
1004          * 32 bit CRC array at the end of the first 512 byte sector.
1005          *
1006          * icrc_sects[7] - First 512-4 bytes of volume header (including all
1007          *                 the other icrc's except this one).
1008          *
1009          * icrc_sects[6] - Sector 1 (512 bytes) of volume header, which is
1010          *                 the blockset for the root.
1011          *
1012          * icrc_sects[5] - Sector 2
1013          * icrc_sects[4] - Sector 3
1014          * icrc_sects[3] - Sector 4 (the freemap blockset)
1015          */
1016         hammer2_crc32_t icrc_sects[8];          /* 01E0-01FF */
1017
1018         /*
1019          * sector #1 - 512 bytes
1020          *
1021          * The entire sector is used by a blockset.
1022          */
1023         hammer2_blockset_t sroot_blockset;      /* 0200-03FF Superroot dir */
1024
1025         /*
1026          * sector #2-7
1027          */
1028         char    sector2[512];                   /* 0400-05FF reserved */
1029         char    sector3[512];                   /* 0600-07FF reserved */
1030         hammer2_blockset_t freemap_blockset;    /* 0800-09FF freemap  */
1031         char    sector5[512];                   /* 0A00-0BFF reserved */
1032         char    sector6[512];                   /* 0C00-0DFF reserved */
1033         char    sector7[512];                   /* 0E00-0FFF reserved */
1034
1035         /*
1036          * sector #8-71 - 32768 bytes
1037          *
1038          * Contains the configuration for up to 256 copyinfo targets.  These
1039          * specify local and remote copies operating as masters or slaves.
1040          * copyid's 0 and 255 are reserved (0 indicates an empty slot and 255
1041          * indicates the local media).
1042          *
1043          * Each inode contains a set of up to 8 copyids, either inherited
1044          * from its parent or explicitly specified in the inode, which
1045          * indexes into this array.
1046          */
1047                                                 /* 1000-8FFF copyinfo config */
1048         hammer2_volconf_t copyinfo[HAMMER2_COPYID_COUNT];
1049
1050         /*
1051          * Remaining sections are reserved for future use.
1052          */
1053         char            reserved0400[0x6FFC];   /* 9000-FFFB reserved */
1054
1055         /*
1056          * icrc on entire volume header
1057          */
1058         hammer2_crc32_t icrc_volheader;         /* FFFC-FFFF full volume icrc*/
1059 };
1060
1061 typedef struct hammer2_volume_data hammer2_volume_data_t;
1062
1063 /*
1064  * Various parts of the volume header have their own iCRCs.
1065  *
1066  * The first 512 bytes has its own iCRC stored at the end of the 512 bytes
1067  * and not included the icrc calculation.
1068  *
1069  * The second 512 bytes also has its own iCRC but it is stored in the first
1070  * 512 bytes so it covers the entire second 512 bytes.
1071  *
1072  * The whole volume block (64KB) has an iCRC covering all but the last 4 bytes,
1073  * which is where the iCRC for the whole volume is stored.  This is currently
1074  * a catch-all for anything not individually iCRCd.
1075  */
1076 #define HAMMER2_VOL_ICRC_SECT0          7
1077 #define HAMMER2_VOL_ICRC_SECT1          6
1078
1079 #define HAMMER2_VOLUME_BYTES            65536
1080
1081 #define HAMMER2_VOLUME_ICRC0_OFF        0
1082 #define HAMMER2_VOLUME_ICRC1_OFF        512
1083 #define HAMMER2_VOLUME_ICRCVH_OFF       0
1084
1085 #define HAMMER2_VOLUME_ICRC0_SIZE       (512 - 4)
1086 #define HAMMER2_VOLUME_ICRC1_SIZE       (512)
1087 #define HAMMER2_VOLUME_ICRCVH_SIZE      (65536 - 4)
1088
1089 #define HAMMER2_VOL_VERSION_MIN         1
1090 #define HAMMER2_VOL_VERSION_DEFAULT     1
1091 #define HAMMER2_VOL_VERSION_WIP         2
1092
1093 #define HAMMER2_NUM_VOLHDRS             4
1094
1095 union hammer2_media_data {
1096         hammer2_volume_data_t   voldata;
1097         hammer2_inode_data_t    ipdata;
1098         hammer2_blockref_t      npdata[HAMMER2_IND_COUNT_MAX];
1099         hammer2_bmap_data_t     bmdata[HAMMER2_FREEMAP_COUNT];
1100         char                    buf[HAMMER2_PBUFSIZE];
1101 };
1102
1103 typedef union hammer2_media_data hammer2_media_data_t;
1104
1105 #endif /* !_VFS_HAMMER2_DISK_H_ */