]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/bcachefs_format.h
Update bcachefs sources to aa439f3b94 bcachefs: btree_gc no longer uses main in-memor...
[bcachefs-tools-debian] / libbcachefs / bcachefs_format.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_FORMAT_H
3 #define _BCACHEFS_FORMAT_H
4
5 /*
6  * bcachefs on disk data structures
7  *
8  * OVERVIEW:
9  *
10  * There are three main types of on disk data structures in bcachefs (this is
11  * reduced from 5 in bcache)
12  *
13  *  - superblock
14  *  - journal
15  *  - btree
16  *
17  * The btree is the primary structure; most metadata exists as keys in the
18  * various btrees. There are only a small number of btrees, they're not
19  * sharded - we have one btree for extents, another for inodes, et cetera.
20  *
21  * SUPERBLOCK:
22  *
23  * The superblock contains the location of the journal, the list of devices in
24  * the filesystem, and in general any metadata we need in order to decide
25  * whether we can start a filesystem or prior to reading the journal/btree
26  * roots.
27  *
28  * The superblock is extensible, and most of the contents of the superblock are
29  * in variable length, type tagged fields; see struct bch_sb_field.
30  *
31  * Backup superblocks do not reside in a fixed location; also, superblocks do
32  * not have a fixed size. To locate backup superblocks we have struct
33  * bch_sb_layout; we store a copy of this inside every superblock, and also
34  * before the first superblock.
35  *
36  * JOURNAL:
37  *
38  * The journal primarily records btree updates in the order they occurred;
39  * journal replay consists of just iterating over all the keys in the open
40  * journal entries and re-inserting them into the btrees.
41  *
42  * The journal also contains entry types for the btree roots, and blacklisted
43  * journal sequence numbers (see journal_seq_blacklist.c).
44  *
45  * BTREE:
46  *
47  * bcachefs btrees are copy on write b+ trees, where nodes are big (typically
48  * 128k-256k) and log structured. We use struct btree_node for writing the first
49  * entry in a given node (offset 0), and struct btree_node_entry for all
50  * subsequent writes.
51  *
52  * After the header, btree node entries contain a list of keys in sorted order.
53  * Values are stored inline with the keys; since values are variable length (and
54  * keys effectively are variable length too, due to packing) we can't do random
55  * access without building up additional in memory tables in the btree node read
56  * path.
57  *
58  * BTREE KEYS (struct bkey):
59  *
60  * The various btrees share a common format for the key - so as to avoid
61  * switching in fastpath lookup/comparison code - but define their own
62  * structures for the key values.
63  *
64  * The size of a key/value pair is stored as a u8 in units of u64s, so the max
65  * size is just under 2k. The common part also contains a type tag for the
66  * value, and a format field indicating whether the key is packed or not (and
67  * also meant to allow adding new key fields in the future, if desired).
68  *
69  * bkeys, when stored within a btree node, may also be packed. In that case, the
70  * bkey_format in that node is used to unpack it. Packed bkeys mean that we can
71  * be generous with field sizes in the common part of the key format (64 bit
72  * inode number, 64 bit offset, 96 bit version field, etc.) for negligible cost.
73  */
74
75 #include <asm/types.h>
76 #include <asm/byteorder.h>
77 #include <linux/kernel.h>
78 #include <linux/uuid.h>
79 #include "vstructs.h"
80
81 #define LE_BITMASK(_bits, name, type, field, offset, end)               \
82 static const unsigned   name##_OFFSET = offset;                         \
83 static const unsigned   name##_BITS = (end - offset);                   \
84 static const __u##_bits name##_MAX = (1ULL << (end - offset)) - 1;      \
85                                                                         \
86 static inline __u64 name(const type *k)                                 \
87 {                                                                       \
88         return (__le##_bits##_to_cpu(k->field) >> offset) &             \
89                 ~(~0ULL << (end - offset));                             \
90 }                                                                       \
91                                                                         \
92 static inline void SET_##name(type *k, __u64 v)                         \
93 {                                                                       \
94         __u##_bits new = __le##_bits##_to_cpu(k->field);                \
95                                                                         \
96         new &= ~(~(~0ULL << (end - offset)) << offset);                 \
97         new |= (v & ~(~0ULL << (end - offset))) << offset;              \
98         k->field = __cpu_to_le##_bits(new);                             \
99 }
100
101 #define LE16_BITMASK(n, t, f, o, e)     LE_BITMASK(16, n, t, f, o, e)
102 #define LE32_BITMASK(n, t, f, o, e)     LE_BITMASK(32, n, t, f, o, e)
103 #define LE64_BITMASK(n, t, f, o, e)     LE_BITMASK(64, n, t, f, o, e)
104
105 struct bkey_format {
106         __u8            key_u64s;
107         __u8            nr_fields;
108         /* One unused slot for now: */
109         __u8            bits_per_field[6];
110         __le64          field_offset[6];
111 };
112
113 /* Btree keys - all units are in sectors */
114
115 struct bpos {
116         /*
117          * Word order matches machine byte order - btree code treats a bpos as a
118          * single large integer, for search/comparison purposes
119          *
120          * Note that wherever a bpos is embedded in another on disk data
121          * structure, it has to be byte swabbed when reading in metadata that
122          * wasn't written in native endian order:
123          */
124 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
125         __u32           snapshot;
126         __u64           offset;
127         __u64           inode;
128 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
129         __u64           inode;
130         __u64           offset;         /* Points to end of extent - sectors */
131         __u32           snapshot;
132 #else
133 #error edit for your odd byteorder.
134 #endif
135 } __attribute__((packed, aligned(4)));
136
137 #define KEY_INODE_MAX                   ((__u64)~0ULL)
138 #define KEY_OFFSET_MAX                  ((__u64)~0ULL)
139 #define KEY_SNAPSHOT_MAX                ((__u32)~0U)
140 #define KEY_SIZE_MAX                    ((__u32)~0U)
141
142 static inline struct bpos SPOS(__u64 inode, __u64 offset, __u32 snapshot)
143 {
144         return (struct bpos) {
145                 .inode          = inode,
146                 .offset         = offset,
147                 .snapshot       = snapshot,
148         };
149 }
150
151 #define POS_MIN                         SPOS(0, 0, 0)
152 #define POS_MAX                         SPOS(KEY_INODE_MAX, KEY_OFFSET_MAX, 0)
153 #define SPOS_MAX                        SPOS(KEY_INODE_MAX, KEY_OFFSET_MAX, KEY_SNAPSHOT_MAX)
154 #define POS(_inode, _offset)            SPOS(_inode, _offset, 0)
155
156 /* Empty placeholder struct, for container_of() */
157 struct bch_val {
158         __u64           __nothing[0];
159 };
160
161 struct bversion {
162 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
163         __u64           lo;
164         __u32           hi;
165 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
166         __u32           hi;
167         __u64           lo;
168 #endif
169 } __attribute__((packed, aligned(4)));
170
171 struct bkey {
172         /* Size of combined key and value, in u64s */
173         __u8            u64s;
174
175         /* Format of key (0 for format local to btree node) */
176 #if defined(__LITTLE_ENDIAN_BITFIELD)
177         __u8            format:7,
178                         needs_whiteout:1;
179 #elif defined (__BIG_ENDIAN_BITFIELD)
180         __u8            needs_whiteout:1,
181                         format:7;
182 #else
183 #error edit for your odd byteorder.
184 #endif
185
186         /* Type of the value */
187         __u8            type;
188
189 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
190         __u8            pad[1];
191
192         struct bversion version;
193         __u32           size;           /* extent size, in sectors */
194         struct bpos     p;
195 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
196         struct bpos     p;
197         __u32           size;           /* extent size, in sectors */
198         struct bversion version;
199
200         __u8            pad[1];
201 #endif
202 } __attribute__((packed, aligned(8)));
203
204 struct bkey_packed {
205         __u64           _data[0];
206
207         /* Size of combined key and value, in u64s */
208         __u8            u64s;
209
210         /* Format of key (0 for format local to btree node) */
211
212         /*
213          * XXX: next incompat on disk format change, switch format and
214          * needs_whiteout - bkey_packed() will be cheaper if format is the high
215          * bits of the bitfield
216          */
217 #if defined(__LITTLE_ENDIAN_BITFIELD)
218         __u8            format:7,
219                         needs_whiteout:1;
220 #elif defined (__BIG_ENDIAN_BITFIELD)
221         __u8            needs_whiteout:1,
222                         format:7;
223 #endif
224
225         /* Type of the value */
226         __u8            type;
227         __u8            key_start[0];
228
229         /*
230          * We copy bkeys with struct assignment in various places, and while
231          * that shouldn't be done with packed bkeys we can't disallow it in C,
232          * and it's legal to cast a bkey to a bkey_packed  - so padding it out
233          * to the same size as struct bkey should hopefully be safest.
234          */
235         __u8            pad[sizeof(struct bkey) - 3];
236 } __attribute__((packed, aligned(8)));
237
238 #define BKEY_U64s                       (sizeof(struct bkey) / sizeof(__u64))
239 #define BKEY_U64s_MAX                   U8_MAX
240 #define BKEY_VAL_U64s_MAX               (BKEY_U64s_MAX - BKEY_U64s)
241
242 #define KEY_PACKED_BITS_START           24
243
244 #define KEY_FORMAT_LOCAL_BTREE          0
245 #define KEY_FORMAT_CURRENT              1
246
247 enum bch_bkey_fields {
248         BKEY_FIELD_INODE,
249         BKEY_FIELD_OFFSET,
250         BKEY_FIELD_SNAPSHOT,
251         BKEY_FIELD_SIZE,
252         BKEY_FIELD_VERSION_HI,
253         BKEY_FIELD_VERSION_LO,
254         BKEY_NR_FIELDS,
255 };
256
257 #define bkey_format_field(name, field)                                  \
258         [BKEY_FIELD_##name] = (sizeof(((struct bkey *) NULL)->field) * 8)
259
260 #define BKEY_FORMAT_CURRENT                                             \
261 ((struct bkey_format) {                                                 \
262         .key_u64s       = BKEY_U64s,                                    \
263         .nr_fields      = BKEY_NR_FIELDS,                               \
264         .bits_per_field = {                                             \
265                 bkey_format_field(INODE,        p.inode),               \
266                 bkey_format_field(OFFSET,       p.offset),              \
267                 bkey_format_field(SNAPSHOT,     p.snapshot),            \
268                 bkey_format_field(SIZE,         size),                  \
269                 bkey_format_field(VERSION_HI,   version.hi),            \
270                 bkey_format_field(VERSION_LO,   version.lo),            \
271         },                                                              \
272 })
273
274 /* bkey with inline value */
275 struct bkey_i {
276         __u64                   _data[0];
277
278         union {
279         struct {
280                 /* Size of combined key and value, in u64s */
281                 __u8            u64s;
282         };
283         struct {
284                 struct bkey     k;
285                 struct bch_val  v;
286         };
287         };
288 };
289
290 #define KEY(_inode, _offset, _size)                                     \
291 ((struct bkey) {                                                        \
292         .u64s           = BKEY_U64s,                                    \
293         .format         = KEY_FORMAT_CURRENT,                           \
294         .p              = POS(_inode, _offset),                         \
295         .size           = _size,                                        \
296 })
297
298 static inline void bkey_init(struct bkey *k)
299 {
300         *k = KEY(0, 0, 0);
301 }
302
303 #define bkey_bytes(_k)          ((_k)->u64s * sizeof(__u64))
304
305 #define __BKEY_PADDED(key, pad)                                 \
306         struct { struct bkey_i key; __u64 key ## _pad[pad]; }
307
308 /*
309  * - DELETED keys are used internally to mark keys that should be ignored but
310  *   override keys in composition order.  Their version number is ignored.
311  *
312  * - DISCARDED keys indicate that the data is all 0s because it has been
313  *   discarded. DISCARDs may have a version; if the version is nonzero the key
314  *   will be persistent, otherwise the key will be dropped whenever the btree
315  *   node is rewritten (like DELETED keys).
316  *
317  * - ERROR: any read of the data returns a read error, as the data was lost due
318  *   to a failing device. Like DISCARDED keys, they can be removed (overridden)
319  *   by new writes or cluster-wide GC. Node repair can also overwrite them with
320  *   the same or a more recent version number, but not with an older version
321  *   number.
322  *
323  * - WHITEOUT: for hash table btrees
324 */
325 #define BCH_BKEY_TYPES()                                \
326         x(deleted,              0)                      \
327         x(whiteout,             1)                      \
328         x(error,                2)                      \
329         x(cookie,               3)                      \
330         x(hash_whiteout,        4)                      \
331         x(btree_ptr,            5)                      \
332         x(extent,               6)                      \
333         x(reservation,          7)                      \
334         x(inode,                8)                      \
335         x(inode_generation,     9)                      \
336         x(dirent,               10)                     \
337         x(xattr,                11)                     \
338         x(alloc,                12)                     \
339         x(quota,                13)                     \
340         x(stripe,               14)                     \
341         x(reflink_p,            15)                     \
342         x(reflink_v,            16)                     \
343         x(inline_data,          17)                     \
344         x(btree_ptr_v2,         18)                     \
345         x(indirect_inline_data, 19)                     \
346         x(alloc_v2,             20)                     \
347         x(subvolume,            21)                     \
348         x(snapshot,             22)                     \
349         x(inode_v2,             23)                     \
350         x(alloc_v3,             24)
351
352 enum bch_bkey_type {
353 #define x(name, nr) KEY_TYPE_##name     = nr,
354         BCH_BKEY_TYPES()
355 #undef x
356         KEY_TYPE_MAX,
357 };
358
359 struct bch_deleted {
360         struct bch_val          v;
361 };
362
363 struct bch_whiteout {
364         struct bch_val          v;
365 };
366
367 struct bch_error {
368         struct bch_val          v;
369 };
370
371 struct bch_cookie {
372         struct bch_val          v;
373         __le64                  cookie;
374 };
375
376 struct bch_hash_whiteout {
377         struct bch_val          v;
378 };
379
380 /* Extents */
381
382 /*
383  * In extent bkeys, the value is a list of pointers (bch_extent_ptr), optionally
384  * preceded by checksum/compression information (bch_extent_crc32 or
385  * bch_extent_crc64).
386  *
387  * One major determining factor in the format of extents is how we handle and
388  * represent extents that have been partially overwritten and thus trimmed:
389  *
390  * If an extent is not checksummed or compressed, when the extent is trimmed we
391  * don't have to remember the extent we originally allocated and wrote: we can
392  * merely adjust ptr->offset to point to the start of the data that is currently
393  * live. The size field in struct bkey records the current (live) size of the
394  * extent, and is also used to mean "size of region on disk that we point to" in
395  * this case.
396  *
397  * Thus an extent that is not checksummed or compressed will consist only of a
398  * list of bch_extent_ptrs, with none of the fields in
399  * bch_extent_crc32/bch_extent_crc64.
400  *
401  * When an extent is checksummed or compressed, it's not possible to read only
402  * the data that is currently live: we have to read the entire extent that was
403  * originally written, and then return only the part of the extent that is
404  * currently live.
405  *
406  * Thus, in addition to the current size of the extent in struct bkey, we need
407  * to store the size of the originally allocated space - this is the
408  * compressed_size and uncompressed_size fields in bch_extent_crc32/64. Also,
409  * when the extent is trimmed, instead of modifying the offset field of the
410  * pointer, we keep a second smaller offset field - "offset into the original
411  * extent of the currently live region".
412  *
413  * The other major determining factor is replication and data migration:
414  *
415  * Each pointer may have its own bch_extent_crc32/64. When doing a replicated
416  * write, we will initially write all the replicas in the same format, with the
417  * same checksum type and compression format - however, when copygc runs later (or
418  * tiering/cache promotion, anything that moves data), it is not in general
419  * going to rewrite all the pointers at once - one of the replicas may be in a
420  * bucket on one device that has very little fragmentation while another lives
421  * in a bucket that has become heavily fragmented, and thus is being rewritten
422  * sooner than the rest.
423  *
424  * Thus it will only move a subset of the pointers (or in the case of
425  * tiering/cache promotion perhaps add a single pointer without dropping any
426  * current pointers), and if the extent has been partially overwritten it must
427  * write only the currently live portion (or copygc would not be able to reduce
428  * fragmentation!) - which necessitates a different bch_extent_crc format for
429  * the new pointer.
430  *
431  * But in the interests of space efficiency, we don't want to store one
432  * bch_extent_crc for each pointer if we don't have to.
433  *
434  * Thus, a bch_extent consists of bch_extent_crc32s, bch_extent_crc64s, and
435  * bch_extent_ptrs appended arbitrarily one after the other. We determine the
436  * type of a given entry with a scheme similar to utf8 (except we're encoding a
437  * type, not a size), encoding the type in the position of the first set bit:
438  *
439  * bch_extent_crc32     - 0b1
440  * bch_extent_ptr       - 0b10
441  * bch_extent_crc64     - 0b100
442  *
443  * We do it this way because bch_extent_crc32 is _very_ constrained on bits (and
444  * bch_extent_crc64 is the least constrained).
445  *
446  * Then, each bch_extent_crc32/64 applies to the pointers that follow after it,
447  * until the next bch_extent_crc32/64.
448  *
449  * If there are no bch_extent_crcs preceding a bch_extent_ptr, then that pointer
450  * is neither checksummed nor compressed.
451  */
452
453 /* 128 bits, sufficient for cryptographic MACs: */
454 struct bch_csum {
455         __le64                  lo;
456         __le64                  hi;
457 } __attribute__((packed, aligned(8)));
458
459 #define BCH_EXTENT_ENTRY_TYPES()                \
460         x(ptr,                  0)              \
461         x(crc32,                1)              \
462         x(crc64,                2)              \
463         x(crc128,               3)              \
464         x(stripe_ptr,           4)
465 #define BCH_EXTENT_ENTRY_MAX    5
466
467 enum bch_extent_entry_type {
468 #define x(f, n) BCH_EXTENT_ENTRY_##f = n,
469         BCH_EXTENT_ENTRY_TYPES()
470 #undef x
471 };
472
473 /* Compressed/uncompressed size are stored biased by 1: */
474 struct bch_extent_crc32 {
475 #if defined(__LITTLE_ENDIAN_BITFIELD)
476         __u32                   type:2,
477                                 _compressed_size:7,
478                                 _uncompressed_size:7,
479                                 offset:7,
480                                 _unused:1,
481                                 csum_type:4,
482                                 compression_type:4;
483         __u32                   csum;
484 #elif defined (__BIG_ENDIAN_BITFIELD)
485         __u32                   csum;
486         __u32                   compression_type:4,
487                                 csum_type:4,
488                                 _unused:1,
489                                 offset:7,
490                                 _uncompressed_size:7,
491                                 _compressed_size:7,
492                                 type:2;
493 #endif
494 } __attribute__((packed, aligned(8)));
495
496 #define CRC32_SIZE_MAX          (1U << 7)
497 #define CRC32_NONCE_MAX         0
498
499 struct bch_extent_crc64 {
500 #if defined(__LITTLE_ENDIAN_BITFIELD)
501         __u64                   type:3,
502                                 _compressed_size:9,
503                                 _uncompressed_size:9,
504                                 offset:9,
505                                 nonce:10,
506                                 csum_type:4,
507                                 compression_type:4,
508                                 csum_hi:16;
509 #elif defined (__BIG_ENDIAN_BITFIELD)
510         __u64                   csum_hi:16,
511                                 compression_type:4,
512                                 csum_type:4,
513                                 nonce:10,
514                                 offset:9,
515                                 _uncompressed_size:9,
516                                 _compressed_size:9,
517                                 type:3;
518 #endif
519         __u64                   csum_lo;
520 } __attribute__((packed, aligned(8)));
521
522 #define CRC64_SIZE_MAX          (1U << 9)
523 #define CRC64_NONCE_MAX         ((1U << 10) - 1)
524
525 struct bch_extent_crc128 {
526 #if defined(__LITTLE_ENDIAN_BITFIELD)
527         __u64                   type:4,
528                                 _compressed_size:13,
529                                 _uncompressed_size:13,
530                                 offset:13,
531                                 nonce:13,
532                                 csum_type:4,
533                                 compression_type:4;
534 #elif defined (__BIG_ENDIAN_BITFIELD)
535         __u64                   compression_type:4,
536                                 csum_type:4,
537                                 nonce:13,
538                                 offset:13,
539                                 _uncompressed_size:13,
540                                 _compressed_size:13,
541                                 type:4;
542 #endif
543         struct bch_csum         csum;
544 } __attribute__((packed, aligned(8)));
545
546 #define CRC128_SIZE_MAX         (1U << 13)
547 #define CRC128_NONCE_MAX        ((1U << 13) - 1)
548
549 /*
550  * @reservation - pointer hasn't been written to, just reserved
551  */
552 struct bch_extent_ptr {
553 #if defined(__LITTLE_ENDIAN_BITFIELD)
554         __u64                   type:1,
555                                 cached:1,
556                                 unused:1,
557                                 reservation:1,
558                                 offset:44, /* 8 petabytes */
559                                 dev:8,
560                                 gen:8;
561 #elif defined (__BIG_ENDIAN_BITFIELD)
562         __u64                   gen:8,
563                                 dev:8,
564                                 offset:44,
565                                 reservation:1,
566                                 unused:1,
567                                 cached:1,
568                                 type:1;
569 #endif
570 } __attribute__((packed, aligned(8)));
571
572 struct bch_extent_stripe_ptr {
573 #if defined(__LITTLE_ENDIAN_BITFIELD)
574         __u64                   type:5,
575                                 block:8,
576                                 redundancy:4,
577                                 idx:47;
578 #elif defined (__BIG_ENDIAN_BITFIELD)
579         __u64                   idx:47,
580                                 redundancy:4,
581                                 block:8,
582                                 type:5;
583 #endif
584 };
585
586 struct bch_extent_reservation {
587 #if defined(__LITTLE_ENDIAN_BITFIELD)
588         __u64                   type:6,
589                                 unused:22,
590                                 replicas:4,
591                                 generation:32;
592 #elif defined (__BIG_ENDIAN_BITFIELD)
593         __u64                   generation:32,
594                                 replicas:4,
595                                 unused:22,
596                                 type:6;
597 #endif
598 };
599
600 union bch_extent_entry {
601 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ ||  __BITS_PER_LONG == 64
602         unsigned long                   type;
603 #elif __BITS_PER_LONG == 32
604         struct {
605                 unsigned long           pad;
606                 unsigned long           type;
607         };
608 #else
609 #error edit for your odd byteorder.
610 #endif
611
612 #define x(f, n) struct bch_extent_##f   f;
613         BCH_EXTENT_ENTRY_TYPES()
614 #undef x
615 };
616
617 struct bch_btree_ptr {
618         struct bch_val          v;
619
620         struct bch_extent_ptr   start[0];
621         __u64                   _data[0];
622 } __attribute__((packed, aligned(8)));
623
624 struct bch_btree_ptr_v2 {
625         struct bch_val          v;
626
627         __u64                   mem_ptr;
628         __le64                  seq;
629         __le16                  sectors_written;
630         __le16                  flags;
631         struct bpos             min_key;
632         struct bch_extent_ptr   start[0];
633         __u64                   _data[0];
634 } __attribute__((packed, aligned(8)));
635
636 LE16_BITMASK(BTREE_PTR_RANGE_UPDATED,   struct bch_btree_ptr_v2, flags, 0, 1);
637
638 struct bch_extent {
639         struct bch_val          v;
640
641         union bch_extent_entry  start[0];
642         __u64                   _data[0];
643 } __attribute__((packed, aligned(8)));
644
645 struct bch_reservation {
646         struct bch_val          v;
647
648         __le32                  generation;
649         __u8                    nr_replicas;
650         __u8                    pad[3];
651 } __attribute__((packed, aligned(8)));
652
653 /* Maximum size (in u64s) a single pointer could be: */
654 #define BKEY_EXTENT_PTR_U64s_MAX\
655         ((sizeof(struct bch_extent_crc128) +                    \
656           sizeof(struct bch_extent_ptr)) / sizeof(u64))
657
658 /* Maximum possible size of an entire extent value: */
659 #define BKEY_EXTENT_VAL_U64s_MAX                                \
660         (1 + BKEY_EXTENT_PTR_U64s_MAX * (BCH_REPLICAS_MAX + 1))
661
662 /* * Maximum possible size of an entire extent, key + value: */
663 #define BKEY_EXTENT_U64s_MAX            (BKEY_U64s + BKEY_EXTENT_VAL_U64s_MAX)
664
665 /* Btree pointers don't carry around checksums: */
666 #define BKEY_BTREE_PTR_VAL_U64s_MAX                             \
667         ((sizeof(struct bch_btree_ptr_v2) +                     \
668           sizeof(struct bch_extent_ptr) * BCH_REPLICAS_MAX) / sizeof(u64))
669 #define BKEY_BTREE_PTR_U64s_MAX                                 \
670         (BKEY_U64s + BKEY_BTREE_PTR_VAL_U64s_MAX)
671
672 /* Inodes */
673
674 #define BLOCKDEV_INODE_MAX      4096
675
676 #define BCACHEFS_ROOT_INO       4096
677
678 struct bch_inode {
679         struct bch_val          v;
680
681         __le64                  bi_hash_seed;
682         __le32                  bi_flags;
683         __le16                  bi_mode;
684         __u8                    fields[0];
685 } __attribute__((packed, aligned(8)));
686
687 struct bch_inode_v2 {
688         struct bch_val          v;
689
690         __le64                  bi_journal_seq;
691         __le64                  bi_hash_seed;
692         __le64                  bi_flags;
693         __le16                  bi_mode;
694         __u8                    fields[0];
695 } __attribute__((packed, aligned(8)));
696
697 struct bch_inode_generation {
698         struct bch_val          v;
699
700         __le32                  bi_generation;
701         __le32                  pad;
702 } __attribute__((packed, aligned(8)));
703
704 /*
705  * bi_subvol and bi_parent_subvol are only set for subvolume roots:
706  */
707
708 #define BCH_INODE_FIELDS()                      \
709         x(bi_atime,                     96)     \
710         x(bi_ctime,                     96)     \
711         x(bi_mtime,                     96)     \
712         x(bi_otime,                     96)     \
713         x(bi_size,                      64)     \
714         x(bi_sectors,                   64)     \
715         x(bi_uid,                       32)     \
716         x(bi_gid,                       32)     \
717         x(bi_nlink,                     32)     \
718         x(bi_generation,                32)     \
719         x(bi_dev,                       32)     \
720         x(bi_data_checksum,             8)      \
721         x(bi_compression,               8)      \
722         x(bi_project,                   32)     \
723         x(bi_background_compression,    8)      \
724         x(bi_data_replicas,             8)      \
725         x(bi_promote_target,            16)     \
726         x(bi_foreground_target,         16)     \
727         x(bi_background_target,         16)     \
728         x(bi_erasure_code,              16)     \
729         x(bi_fields_set,                16)     \
730         x(bi_dir,                       64)     \
731         x(bi_dir_offset,                64)     \
732         x(bi_subvol,                    32)     \
733         x(bi_parent_subvol,             32)
734
735 /* subset of BCH_INODE_FIELDS */
736 #define BCH_INODE_OPTS()                        \
737         x(data_checksum,                8)      \
738         x(compression,                  8)      \
739         x(project,                      32)     \
740         x(background_compression,       8)      \
741         x(data_replicas,                8)      \
742         x(promote_target,               16)     \
743         x(foreground_target,            16)     \
744         x(background_target,            16)     \
745         x(erasure_code,                 16)
746
747 enum inode_opt_id {
748 #define x(name, ...)                            \
749         Inode_opt_##name,
750         BCH_INODE_OPTS()
751 #undef  x
752         Inode_opt_nr,
753 };
754
755 enum {
756         /*
757          * User flags (get/settable with FS_IOC_*FLAGS, correspond to FS_*_FL
758          * flags)
759          */
760         __BCH_INODE_SYNC        = 0,
761         __BCH_INODE_IMMUTABLE   = 1,
762         __BCH_INODE_APPEND      = 2,
763         __BCH_INODE_NODUMP      = 3,
764         __BCH_INODE_NOATIME     = 4,
765
766         __BCH_INODE_I_SIZE_DIRTY= 5,
767         __BCH_INODE_I_SECTORS_DIRTY= 6,
768         __BCH_INODE_UNLINKED    = 7,
769         __BCH_INODE_BACKPTR_UNTRUSTED = 8,
770
771         /* bits 20+ reserved for packed fields below: */
772 };
773
774 #define BCH_INODE_SYNC          (1 << __BCH_INODE_SYNC)
775 #define BCH_INODE_IMMUTABLE     (1 << __BCH_INODE_IMMUTABLE)
776 #define BCH_INODE_APPEND        (1 << __BCH_INODE_APPEND)
777 #define BCH_INODE_NODUMP        (1 << __BCH_INODE_NODUMP)
778 #define BCH_INODE_NOATIME       (1 << __BCH_INODE_NOATIME)
779 #define BCH_INODE_I_SIZE_DIRTY  (1 << __BCH_INODE_I_SIZE_DIRTY)
780 #define BCH_INODE_I_SECTORS_DIRTY (1 << __BCH_INODE_I_SECTORS_DIRTY)
781 #define BCH_INODE_UNLINKED      (1 << __BCH_INODE_UNLINKED)
782 #define BCH_INODE_BACKPTR_UNTRUSTED (1 << __BCH_INODE_BACKPTR_UNTRUSTED)
783
784 LE32_BITMASK(INODE_STR_HASH,    struct bch_inode, bi_flags, 20, 24);
785 LE32_BITMASK(INODE_NR_FIELDS,   struct bch_inode, bi_flags, 24, 31);
786 LE32_BITMASK(INODE_NEW_VARINT,  struct bch_inode, bi_flags, 31, 32);
787
788 LE64_BITMASK(INODEv2_STR_HASH,  struct bch_inode_v2, bi_flags, 20, 24);
789 LE64_BITMASK(INODEv2_NR_FIELDS, struct bch_inode_v2, bi_flags, 24, 31);
790
791 /* Dirents */
792
793 /*
794  * Dirents (and xattrs) have to implement string lookups; since our b-tree
795  * doesn't support arbitrary length strings for the key, we instead index by a
796  * 64 bit hash (currently truncated sha1) of the string, stored in the offset
797  * field of the key - using linear probing to resolve hash collisions. This also
798  * provides us with the readdir cookie posix requires.
799  *
800  * Linear probing requires us to use whiteouts for deletions, in the event of a
801  * collision:
802  */
803
804 struct bch_dirent {
805         struct bch_val          v;
806
807         /* Target inode number: */
808         union {
809         __le64                  d_inum;
810         struct {                /* DT_SUBVOL */
811         __le32                  d_child_subvol;
812         __le32                  d_parent_subvol;
813         };
814         };
815
816         /*
817          * Copy of mode bits 12-15 from the target inode - so userspace can get
818          * the filetype without having to do a stat()
819          */
820         __u8                    d_type;
821
822         __u8                    d_name[];
823 } __attribute__((packed, aligned(8)));
824
825 #define DT_SUBVOL       16
826 #define BCH_DT_MAX      17
827
828 #define BCH_NAME_MAX    (U8_MAX * sizeof(u64) -                         \
829                          sizeof(struct bkey) -                          \
830                          offsetof(struct bch_dirent, d_name))
831
832
833 /* Xattrs */
834
835 #define KEY_TYPE_XATTR_INDEX_USER                       0
836 #define KEY_TYPE_XATTR_INDEX_POSIX_ACL_ACCESS   1
837 #define KEY_TYPE_XATTR_INDEX_POSIX_ACL_DEFAULT  2
838 #define KEY_TYPE_XATTR_INDEX_TRUSTED                    3
839 #define KEY_TYPE_XATTR_INDEX_SECURITY           4
840
841 struct bch_xattr {
842         struct bch_val          v;
843         __u8                    x_type;
844         __u8                    x_name_len;
845         __le16                  x_val_len;
846         __u8                    x_name[];
847 } __attribute__((packed, aligned(8)));
848
849 /* Bucket/allocation information: */
850
851 struct bch_alloc {
852         struct bch_val          v;
853         __u8                    fields;
854         __u8                    gen;
855         __u8                    data[];
856 } __attribute__((packed, aligned(8)));
857
858 #define BCH_ALLOC_FIELDS_V1()                   \
859         x(read_time,            16)             \
860         x(write_time,           16)             \
861         x(data_type,            8)              \
862         x(dirty_sectors,        16)             \
863         x(cached_sectors,       16)             \
864         x(oldest_gen,           8)              \
865         x(stripe,               32)             \
866         x(stripe_redundancy,    8)
867
868 struct bch_alloc_v2 {
869         struct bch_val          v;
870         __u8                    nr_fields;
871         __u8                    gen;
872         __u8                    oldest_gen;
873         __u8                    data_type;
874         __u8                    data[];
875 } __attribute__((packed, aligned(8)));
876
877 #define BCH_ALLOC_FIELDS_V2()                   \
878         x(read_time,            64)             \
879         x(write_time,           64)             \
880         x(dirty_sectors,        16)             \
881         x(cached_sectors,       16)             \
882         x(stripe,               32)             \
883         x(stripe_redundancy,    8)
884
885 struct bch_alloc_v3 {
886         struct bch_val          v;
887         __le64                  journal_seq;
888         __le32                  flags;
889         __u8                    nr_fields;
890         __u8                    gen;
891         __u8                    oldest_gen;
892         __u8                    data_type;
893         __u8                    data[];
894 } __attribute__((packed, aligned(8)));
895
896 enum {
897 #define x(name, _bits) BCH_ALLOC_FIELD_V1_##name,
898         BCH_ALLOC_FIELDS_V1()
899 #undef x
900         BCH_ALLOC_FIELD_NR
901 };
902
903 /* Quotas: */
904
905 enum quota_types {
906         QTYP_USR                = 0,
907         QTYP_GRP                = 1,
908         QTYP_PRJ                = 2,
909         QTYP_NR                 = 3,
910 };
911
912 enum quota_counters {
913         Q_SPC                   = 0,
914         Q_INO                   = 1,
915         Q_COUNTERS              = 2,
916 };
917
918 struct bch_quota_counter {
919         __le64                  hardlimit;
920         __le64                  softlimit;
921 };
922
923 struct bch_quota {
924         struct bch_val          v;
925         struct bch_quota_counter c[Q_COUNTERS];
926 } __attribute__((packed, aligned(8)));
927
928 /* Erasure coding */
929
930 struct bch_stripe {
931         struct bch_val          v;
932         __le16                  sectors;
933         __u8                    algorithm;
934         __u8                    nr_blocks;
935         __u8                    nr_redundant;
936
937         __u8                    csum_granularity_bits;
938         __u8                    csum_type;
939         __u8                    pad;
940
941         struct bch_extent_ptr   ptrs[0];
942 } __attribute__((packed, aligned(8)));
943
944 /* Reflink: */
945
946 struct bch_reflink_p {
947         struct bch_val          v;
948         __le64                  idx;
949         /*
950          * A reflink pointer might point to an indirect extent which is then
951          * later split (by copygc or rebalance). If we only pointed to part of
952          * the original indirect extent, and then one of the fragments is
953          * outside the range we point to, we'd leak a refcount: so when creating
954          * reflink pointers, we need to store pad values to remember the full
955          * range we were taking a reference on.
956          */
957         __le32                  front_pad;
958         __le32                  back_pad;
959 } __attribute__((packed, aligned(8)));
960
961 struct bch_reflink_v {
962         struct bch_val          v;
963         __le64                  refcount;
964         union bch_extent_entry  start[0];
965         __u64                   _data[0];
966 } __attribute__((packed, aligned(8)));
967
968 struct bch_indirect_inline_data {
969         struct bch_val          v;
970         __le64                  refcount;
971         u8                      data[0];
972 };
973
974 /* Inline data */
975
976 struct bch_inline_data {
977         struct bch_val          v;
978         u8                      data[0];
979 };
980
981 /* Subvolumes: */
982
983 #define SUBVOL_POS_MIN          POS(0, 1)
984 #define SUBVOL_POS_MAX          POS(0, S32_MAX)
985 #define BCACHEFS_ROOT_SUBVOL    1
986
987 struct bch_subvolume {
988         struct bch_val          v;
989         __le32                  flags;
990         __le32                  snapshot;
991         __le64                  inode;
992 };
993
994 LE32_BITMASK(BCH_SUBVOLUME_RO,          struct bch_subvolume, flags,  0,  1)
995 /*
996  * We need to know whether a subvolume is a snapshot so we can know whether we
997  * can delete it (or whether it should just be rm -rf'd)
998  */
999 LE32_BITMASK(BCH_SUBVOLUME_SNAP,        struct bch_subvolume, flags,  1,  2)
1000 LE32_BITMASK(BCH_SUBVOLUME_UNLINKED,    struct bch_subvolume, flags,  2,  3)
1001
1002 /* Snapshots */
1003
1004 struct bch_snapshot {
1005         struct bch_val          v;
1006         __le32                  flags;
1007         __le32                  parent;
1008         __le32                  children[2];
1009         __le32                  subvol;
1010         __le32                  pad;
1011 };
1012
1013 LE32_BITMASK(BCH_SNAPSHOT_DELETED,      struct bch_snapshot, flags,  0,  1)
1014
1015 /* True if a subvolume points to this snapshot node: */
1016 LE32_BITMASK(BCH_SNAPSHOT_SUBVOL,       struct bch_snapshot, flags,  1,  2)
1017
1018 /* Optional/variable size superblock sections: */
1019
1020 struct bch_sb_field {
1021         __u64                   _data[0];
1022         __le32                  u64s;
1023         __le32                  type;
1024 };
1025
1026 #define BCH_SB_FIELDS()         \
1027         x(journal,      0)      \
1028         x(members,      1)      \
1029         x(crypt,        2)      \
1030         x(replicas_v0,  3)      \
1031         x(quota,        4)      \
1032         x(disk_groups,  5)      \
1033         x(clean,        6)      \
1034         x(replicas,     7)      \
1035         x(journal_seq_blacklist, 8)
1036
1037 enum bch_sb_field_type {
1038 #define x(f, nr)        BCH_SB_FIELD_##f = nr,
1039         BCH_SB_FIELDS()
1040 #undef x
1041         BCH_SB_FIELD_NR
1042 };
1043
1044 /* BCH_SB_FIELD_journal: */
1045
1046 struct bch_sb_field_journal {
1047         struct bch_sb_field     field;
1048         __le64                  buckets[0];
1049 };
1050
1051 /* BCH_SB_FIELD_members: */
1052
1053 #define BCH_MIN_NR_NBUCKETS     (1 << 6)
1054
1055 struct bch_member {
1056         uuid_le                 uuid;
1057         __le64                  nbuckets;       /* device size */
1058         __le16                  first_bucket;   /* index of first bucket used */
1059         __le16                  bucket_size;    /* sectors */
1060         __le32                  pad;
1061         __le64                  last_mount;     /* time_t */
1062
1063         __le64                  flags[2];
1064 };
1065
1066 LE64_BITMASK(BCH_MEMBER_STATE,          struct bch_member, flags[0],  0,  4)
1067 /* 4-14 unused, was TIER, HAS_(META)DATA, REPLACEMENT */
1068 LE64_BITMASK(BCH_MEMBER_DISCARD,        struct bch_member, flags[0], 14, 15)
1069 LE64_BITMASK(BCH_MEMBER_DATA_ALLOWED,   struct bch_member, flags[0], 15, 20)
1070 LE64_BITMASK(BCH_MEMBER_GROUP,          struct bch_member, flags[0], 20, 28)
1071 LE64_BITMASK(BCH_MEMBER_DURABILITY,     struct bch_member, flags[0], 28, 30)
1072
1073 #if 0
1074 LE64_BITMASK(BCH_MEMBER_NR_READ_ERRORS, struct bch_member, flags[1], 0,  20);
1075 LE64_BITMASK(BCH_MEMBER_NR_WRITE_ERRORS,struct bch_member, flags[1], 20, 40);
1076 #endif
1077
1078 #define BCH_MEMBER_STATES()                     \
1079         x(rw,           0)                      \
1080         x(ro,           1)                      \
1081         x(failed,       2)                      \
1082         x(spare,        3)
1083
1084 enum bch_member_state {
1085 #define x(t, n) BCH_MEMBER_STATE_##t = n,
1086         BCH_MEMBER_STATES()
1087 #undef x
1088         BCH_MEMBER_STATE_NR
1089 };
1090
1091 struct bch_sb_field_members {
1092         struct bch_sb_field     field;
1093         struct bch_member       members[0];
1094 };
1095
1096 /* BCH_SB_FIELD_crypt: */
1097
1098 struct nonce {
1099         __le32                  d[4];
1100 };
1101
1102 struct bch_key {
1103         __le64                  key[4];
1104 };
1105
1106 #define BCH_KEY_MAGIC                                   \
1107         (((u64) 'b' <<  0)|((u64) 'c' <<  8)|           \
1108          ((u64) 'h' << 16)|((u64) '*' << 24)|           \
1109          ((u64) '*' << 32)|((u64) 'k' << 40)|           \
1110          ((u64) 'e' << 48)|((u64) 'y' << 56))
1111
1112 struct bch_encrypted_key {
1113         __le64                  magic;
1114         struct bch_key          key;
1115 };
1116
1117 /*
1118  * If this field is present in the superblock, it stores an encryption key which
1119  * is used encrypt all other data/metadata. The key will normally be encrypted
1120  * with the key userspace provides, but if encryption has been turned off we'll
1121  * just store the master key unencrypted in the superblock so we can access the
1122  * previously encrypted data.
1123  */
1124 struct bch_sb_field_crypt {
1125         struct bch_sb_field     field;
1126
1127         __le64                  flags;
1128         __le64                  kdf_flags;
1129         struct bch_encrypted_key key;
1130 };
1131
1132 LE64_BITMASK(BCH_CRYPT_KDF_TYPE,        struct bch_sb_field_crypt, flags, 0, 4);
1133
1134 enum bch_kdf_types {
1135         BCH_KDF_SCRYPT          = 0,
1136         BCH_KDF_NR              = 1,
1137 };
1138
1139 /* stored as base 2 log of scrypt params: */
1140 LE64_BITMASK(BCH_KDF_SCRYPT_N,  struct bch_sb_field_crypt, kdf_flags,  0, 16);
1141 LE64_BITMASK(BCH_KDF_SCRYPT_R,  struct bch_sb_field_crypt, kdf_flags, 16, 32);
1142 LE64_BITMASK(BCH_KDF_SCRYPT_P,  struct bch_sb_field_crypt, kdf_flags, 32, 48);
1143
1144 /* BCH_SB_FIELD_replicas: */
1145
1146 #define BCH_DATA_TYPES()                \
1147         x(none,         0)              \
1148         x(sb,           1)              \
1149         x(journal,      2)              \
1150         x(btree,        3)              \
1151         x(user,         4)              \
1152         x(cached,       5)              \
1153         x(parity,       6)
1154
1155 enum bch_data_type {
1156 #define x(t, n) BCH_DATA_##t,
1157         BCH_DATA_TYPES()
1158 #undef x
1159         BCH_DATA_NR
1160 };
1161
1162 struct bch_replicas_entry_v0 {
1163         __u8                    data_type;
1164         __u8                    nr_devs;
1165         __u8                    devs[0];
1166 } __attribute__((packed));
1167
1168 struct bch_sb_field_replicas_v0 {
1169         struct bch_sb_field     field;
1170         struct bch_replicas_entry_v0 entries[0];
1171 } __attribute__((packed, aligned(8)));
1172
1173 struct bch_replicas_entry {
1174         __u8                    data_type;
1175         __u8                    nr_devs;
1176         __u8                    nr_required;
1177         __u8                    devs[0];
1178 } __attribute__((packed));
1179
1180 #define replicas_entry_bytes(_i)                                        \
1181         (offsetof(typeof(*(_i)), devs) + (_i)->nr_devs)
1182
1183 struct bch_sb_field_replicas {
1184         struct bch_sb_field     field;
1185         struct bch_replicas_entry entries[0];
1186 } __attribute__((packed, aligned(8)));
1187
1188 /* BCH_SB_FIELD_quota: */
1189
1190 struct bch_sb_quota_counter {
1191         __le32                          timelimit;
1192         __le32                          warnlimit;
1193 };
1194
1195 struct bch_sb_quota_type {
1196         __le64                          flags;
1197         struct bch_sb_quota_counter     c[Q_COUNTERS];
1198 };
1199
1200 struct bch_sb_field_quota {
1201         struct bch_sb_field             field;
1202         struct bch_sb_quota_type        q[QTYP_NR];
1203 } __attribute__((packed, aligned(8)));
1204
1205 /* BCH_SB_FIELD_disk_groups: */
1206
1207 #define BCH_SB_LABEL_SIZE               32
1208
1209 struct bch_disk_group {
1210         __u8                    label[BCH_SB_LABEL_SIZE];
1211         __le64                  flags[2];
1212 } __attribute__((packed, aligned(8)));
1213
1214 LE64_BITMASK(BCH_GROUP_DELETED,         struct bch_disk_group, flags[0], 0,  1)
1215 LE64_BITMASK(BCH_GROUP_DATA_ALLOWED,    struct bch_disk_group, flags[0], 1,  6)
1216 LE64_BITMASK(BCH_GROUP_PARENT,          struct bch_disk_group, flags[0], 6, 24)
1217
1218 struct bch_sb_field_disk_groups {
1219         struct bch_sb_field     field;
1220         struct bch_disk_group   entries[0];
1221 } __attribute__((packed, aligned(8)));
1222
1223 /*
1224  * On clean shutdown, store btree roots and current journal sequence number in
1225  * the superblock:
1226  */
1227 struct jset_entry {
1228         __le16                  u64s;
1229         __u8                    btree_id;
1230         __u8                    level;
1231         __u8                    type; /* designates what this jset holds */
1232         __u8                    pad[3];
1233
1234         union {
1235                 struct bkey_i   start[0];
1236                 __u64           _data[0];
1237         };
1238 };
1239
1240 struct bch_sb_field_clean {
1241         struct bch_sb_field     field;
1242
1243         __le32                  flags;
1244         __le16                  _read_clock; /* no longer used */
1245         __le16                  _write_clock;
1246         __le64                  journal_seq;
1247
1248         union {
1249                 struct jset_entry start[0];
1250                 __u64           _data[0];
1251         };
1252 };
1253
1254 struct journal_seq_blacklist_entry {
1255         __le64                  start;
1256         __le64                  end;
1257 };
1258
1259 struct bch_sb_field_journal_seq_blacklist {
1260         struct bch_sb_field     field;
1261
1262         union {
1263                 struct journal_seq_blacklist_entry start[0];
1264                 __u64           _data[0];
1265         };
1266 };
1267
1268 /* Superblock: */
1269
1270 /*
1271  * New versioning scheme:
1272  * One common version number for all on disk data structures - superblock, btree
1273  * nodes, journal entries
1274  */
1275 #define BCH_JSET_VERSION_OLD                    2
1276 #define BCH_BSET_VERSION_OLD                    3
1277
1278 enum bcachefs_metadata_version {
1279         bcachefs_metadata_version_min                   = 9,
1280         bcachefs_metadata_version_new_versioning        = 10,
1281         bcachefs_metadata_version_bkey_renumber         = 10,
1282         bcachefs_metadata_version_inode_btree_change    = 11,
1283         bcachefs_metadata_version_snapshot              = 12,
1284         bcachefs_metadata_version_inode_backpointers    = 13,
1285         bcachefs_metadata_version_btree_ptr_sectors_written = 14,
1286         bcachefs_metadata_version_snapshot_2            = 15,
1287         bcachefs_metadata_version_reflink_p_fix         = 16,
1288         bcachefs_metadata_version_subvol_dirent         = 17,
1289         bcachefs_metadata_version_inode_v2              = 18,
1290         bcachefs_metadata_version_max                   = 19,
1291 };
1292
1293 #define bcachefs_metadata_version_current       (bcachefs_metadata_version_max - 1)
1294
1295 #define BCH_SB_SECTOR                   8
1296 #define BCH_SB_MEMBERS_MAX              64 /* XXX kill */
1297
1298 struct bch_sb_layout {
1299         uuid_le                 magic;  /* bcachefs superblock UUID */
1300         __u8                    layout_type;
1301         __u8                    sb_max_size_bits; /* base 2 of 512 byte sectors */
1302         __u8                    nr_superblocks;
1303         __u8                    pad[5];
1304         __le64                  sb_offset[61];
1305 } __attribute__((packed, aligned(8)));
1306
1307 #define BCH_SB_LAYOUT_SECTOR    7
1308
1309 /*
1310  * @offset      - sector where this sb was written
1311  * @version     - on disk format version
1312  * @version_min - Oldest metadata version this filesystem contains; so we can
1313  *                safely drop compatibility code and refuse to mount filesystems
1314  *                we'd need it for
1315  * @magic       - identifies as a bcachefs superblock (BCACHE_MAGIC)
1316  * @seq         - incremented each time superblock is written
1317  * @uuid        - used for generating various magic numbers and identifying
1318  *                member devices, never changes
1319  * @user_uuid   - user visible UUID, may be changed
1320  * @label       - filesystem label
1321  * @seq         - identifies most recent superblock, incremented each time
1322  *                superblock is written
1323  * @features    - enabled incompatible features
1324  */
1325 struct bch_sb {
1326         struct bch_csum         csum;
1327         __le16                  version;
1328         __le16                  version_min;
1329         __le16                  pad[2];
1330         uuid_le                 magic;
1331         uuid_le                 uuid;
1332         uuid_le                 user_uuid;
1333         __u8                    label[BCH_SB_LABEL_SIZE];
1334         __le64                  offset;
1335         __le64                  seq;
1336
1337         __le16                  block_size;
1338         __u8                    dev_idx;
1339         __u8                    nr_devices;
1340         __le32                  u64s;
1341
1342         __le64                  time_base_lo;
1343         __le32                  time_base_hi;
1344         __le32                  time_precision;
1345
1346         __le64                  flags[8];
1347         __le64                  features[2];
1348         __le64                  compat[2];
1349
1350         struct bch_sb_layout    layout;
1351
1352         union {
1353                 struct bch_sb_field start[0];
1354                 __le64          _data[0];
1355         };
1356 } __attribute__((packed, aligned(8)));
1357
1358 /*
1359  * Flags:
1360  * BCH_SB_INITALIZED    - set on first mount
1361  * BCH_SB_CLEAN         - did we shut down cleanly? Just a hint, doesn't affect
1362  *                        behaviour of mount/recovery path:
1363  * BCH_SB_INODE_32BIT   - limit inode numbers to 32 bits
1364  * BCH_SB_128_BIT_MACS  - 128 bit macs instead of 80
1365  * BCH_SB_ENCRYPTION_TYPE - if nonzero encryption is enabled; overrides
1366  *                         DATA/META_CSUM_TYPE. Also indicates encryption
1367  *                         algorithm in use, if/when we get more than one
1368  */
1369
1370 LE16_BITMASK(BCH_SB_BLOCK_SIZE,         struct bch_sb, block_size, 0, 16);
1371
1372 LE64_BITMASK(BCH_SB_INITIALIZED,        struct bch_sb, flags[0],  0,  1);
1373 LE64_BITMASK(BCH_SB_CLEAN,              struct bch_sb, flags[0],  1,  2);
1374 LE64_BITMASK(BCH_SB_CSUM_TYPE,          struct bch_sb, flags[0],  2,  8);
1375 LE64_BITMASK(BCH_SB_ERROR_ACTION,       struct bch_sb, flags[0],  8, 12);
1376
1377 LE64_BITMASK(BCH_SB_BTREE_NODE_SIZE,    struct bch_sb, flags[0], 12, 28);
1378
1379 LE64_BITMASK(BCH_SB_GC_RESERVE,         struct bch_sb, flags[0], 28, 33);
1380 LE64_BITMASK(BCH_SB_ROOT_RESERVE,       struct bch_sb, flags[0], 33, 40);
1381
1382 LE64_BITMASK(BCH_SB_META_CSUM_TYPE,     struct bch_sb, flags[0], 40, 44);
1383 LE64_BITMASK(BCH_SB_DATA_CSUM_TYPE,     struct bch_sb, flags[0], 44, 48);
1384
1385 LE64_BITMASK(BCH_SB_META_REPLICAS_WANT, struct bch_sb, flags[0], 48, 52);
1386 LE64_BITMASK(BCH_SB_DATA_REPLICAS_WANT, struct bch_sb, flags[0], 52, 56);
1387
1388 LE64_BITMASK(BCH_SB_POSIX_ACL,          struct bch_sb, flags[0], 56, 57);
1389 LE64_BITMASK(BCH_SB_USRQUOTA,           struct bch_sb, flags[0], 57, 58);
1390 LE64_BITMASK(BCH_SB_GRPQUOTA,           struct bch_sb, flags[0], 58, 59);
1391 LE64_BITMASK(BCH_SB_PRJQUOTA,           struct bch_sb, flags[0], 59, 60);
1392
1393 LE64_BITMASK(BCH_SB_HAS_ERRORS,         struct bch_sb, flags[0], 60, 61);
1394 LE64_BITMASK(BCH_SB_HAS_TOPOLOGY_ERRORS,struct bch_sb, flags[0], 61, 62);
1395
1396 LE64_BITMASK(BCH_SB_BIG_ENDIAN,         struct bch_sb, flags[0], 62, 63);
1397
1398 LE64_BITMASK(BCH_SB_STR_HASH_TYPE,      struct bch_sb, flags[1],  0,  4);
1399 LE64_BITMASK(BCH_SB_COMPRESSION_TYPE,   struct bch_sb, flags[1],  4,  8);
1400 LE64_BITMASK(BCH_SB_INODE_32BIT,        struct bch_sb, flags[1],  8,  9);
1401
1402 LE64_BITMASK(BCH_SB_128_BIT_MACS,       struct bch_sb, flags[1],  9, 10);
1403 LE64_BITMASK(BCH_SB_ENCRYPTION_TYPE,    struct bch_sb, flags[1], 10, 14);
1404
1405 /*
1406  * Max size of an extent that may require bouncing to read or write
1407  * (checksummed, compressed): 64k
1408  */
1409 LE64_BITMASK(BCH_SB_ENCODED_EXTENT_MAX_BITS,
1410                                         struct bch_sb, flags[1], 14, 20);
1411
1412 LE64_BITMASK(BCH_SB_META_REPLICAS_REQ,  struct bch_sb, flags[1], 20, 24);
1413 LE64_BITMASK(BCH_SB_DATA_REPLICAS_REQ,  struct bch_sb, flags[1], 24, 28);
1414
1415 LE64_BITMASK(BCH_SB_PROMOTE_TARGET,     struct bch_sb, flags[1], 28, 40);
1416 LE64_BITMASK(BCH_SB_FOREGROUND_TARGET,  struct bch_sb, flags[1], 40, 52);
1417 LE64_BITMASK(BCH_SB_BACKGROUND_TARGET,  struct bch_sb, flags[1], 52, 64);
1418
1419 LE64_BITMASK(BCH_SB_BACKGROUND_COMPRESSION_TYPE,
1420                                         struct bch_sb, flags[2],  0,  4);
1421 LE64_BITMASK(BCH_SB_GC_RESERVE_BYTES,   struct bch_sb, flags[2],  4, 64);
1422
1423 LE64_BITMASK(BCH_SB_ERASURE_CODE,       struct bch_sb, flags[3],  0, 16);
1424 LE64_BITMASK(BCH_SB_METADATA_TARGET,    struct bch_sb, flags[3], 16, 28);
1425 LE64_BITMASK(BCH_SB_SHARD_INUMS,        struct bch_sb, flags[3], 28, 29);
1426 LE64_BITMASK(BCH_SB_INODES_USE_KEY_CACHE,struct bch_sb, flags[3], 29, 30);
1427 LE64_BITMASK(BCH_SB_JOURNAL_FLUSH_DELAY,struct bch_sb, flags[3], 30, 62);
1428 LE64_BITMASK(BCH_SB_JOURNAL_FLUSH_DISABLED,struct bch_sb, flags[3], 62, 63);
1429 LE64_BITMASK(BCH_SB_JOURNAL_RECLAIM_DELAY,struct bch_sb, flags[4], 0, 32);
1430 LE64_BITMASK(BCH_SB_JOURNAL_TRANSACTION_NAMES,struct bch_sb, flags[4], 32, 33);
1431
1432 /*
1433  * Features:
1434  *
1435  * journal_seq_blacklist_v3:    gates BCH_SB_FIELD_journal_seq_blacklist
1436  * reflink:                     gates KEY_TYPE_reflink
1437  * inline_data:                 gates KEY_TYPE_inline_data
1438  * new_siphash:                 gates BCH_STR_HASH_siphash
1439  * new_extent_overwrite:        gates BTREE_NODE_NEW_EXTENT_OVERWRITE
1440  */
1441 #define BCH_SB_FEATURES()                       \
1442         x(lz4,                          0)      \
1443         x(gzip,                         1)      \
1444         x(zstd,                         2)      \
1445         x(atomic_nlink,                 3)      \
1446         x(ec,                           4)      \
1447         x(journal_seq_blacklist_v3,     5)      \
1448         x(reflink,                      6)      \
1449         x(new_siphash,                  7)      \
1450         x(inline_data,                  8)      \
1451         x(new_extent_overwrite,         9)      \
1452         x(incompressible,               10)     \
1453         x(btree_ptr_v2,                 11)     \
1454         x(extents_above_btree_updates,  12)     \
1455         x(btree_updates_journalled,     13)     \
1456         x(reflink_inline_data,          14)     \
1457         x(new_varint,                   15)     \
1458         x(journal_no_flush,             16)     \
1459         x(alloc_v2,                     17)     \
1460         x(extents_across_btree_nodes,   18)
1461
1462 #define BCH_SB_FEATURES_ALWAYS                          \
1463         ((1ULL << BCH_FEATURE_new_extent_overwrite)|    \
1464          (1ULL << BCH_FEATURE_extents_above_btree_updates)|\
1465          (1ULL << BCH_FEATURE_btree_updates_journalled)|\
1466          (1ULL << BCH_FEATURE_alloc_v2)|\
1467          (1ULL << BCH_FEATURE_extents_across_btree_nodes))
1468
1469 #define BCH_SB_FEATURES_ALL                             \
1470         (BCH_SB_FEATURES_ALWAYS|                        \
1471          (1ULL << BCH_FEATURE_new_siphash)|             \
1472          (1ULL << BCH_FEATURE_btree_ptr_v2)|            \
1473          (1ULL << BCH_FEATURE_new_varint)|              \
1474          (1ULL << BCH_FEATURE_journal_no_flush))
1475
1476 enum bch_sb_feature {
1477 #define x(f, n) BCH_FEATURE_##f,
1478         BCH_SB_FEATURES()
1479 #undef x
1480         BCH_FEATURE_NR,
1481 };
1482
1483 #define BCH_SB_COMPAT()                                 \
1484         x(alloc_info,                           0)      \
1485         x(alloc_metadata,                       1)      \
1486         x(extents_above_btree_updates_done,     2)      \
1487         x(bformat_overflow_done,                3)
1488
1489 enum bch_sb_compat {
1490 #define x(f, n) BCH_COMPAT_##f,
1491         BCH_SB_COMPAT()
1492 #undef x
1493         BCH_COMPAT_NR,
1494 };
1495
1496 /* options: */
1497
1498 #define BCH_REPLICAS_MAX                4U
1499
1500 #define BCH_BKEY_PTRS_MAX               16U
1501
1502 #define BCH_ERROR_ACTIONS()             \
1503         x(continue,             0)      \
1504         x(ro,                   1)      \
1505         x(panic,                2)
1506
1507 enum bch_error_actions {
1508 #define x(t, n) BCH_ON_ERROR_##t = n,
1509         BCH_ERROR_ACTIONS()
1510 #undef x
1511         BCH_ON_ERROR_NR
1512 };
1513
1514 #define BCH_STR_HASH_TYPES()            \
1515         x(crc32c,               0)      \
1516         x(crc64,                1)      \
1517         x(siphash_old,          2)      \
1518         x(siphash,              3)
1519
1520 enum bch_str_hash_type {
1521 #define x(t, n) BCH_STR_HASH_##t = n,
1522         BCH_STR_HASH_TYPES()
1523 #undef x
1524         BCH_STR_HASH_NR
1525 };
1526
1527 #define BCH_STR_HASH_OPTS()             \
1528         x(crc32c,               0)      \
1529         x(crc64,                1)      \
1530         x(siphash,              2)
1531
1532 enum bch_str_hash_opts {
1533 #define x(t, n) BCH_STR_HASH_OPT_##t = n,
1534         BCH_STR_HASH_OPTS()
1535 #undef x
1536         BCH_STR_HASH_OPT_NR
1537 };
1538
1539 #define BCH_CSUM_TYPES()                        \
1540         x(none,                         0)      \
1541         x(crc32c_nonzero,               1)      \
1542         x(crc64_nonzero,                2)      \
1543         x(chacha20_poly1305_80,         3)      \
1544         x(chacha20_poly1305_128,        4)      \
1545         x(crc32c,                       5)      \
1546         x(crc64,                        6)      \
1547         x(xxhash,                       7)
1548
1549 enum bch_csum_type {
1550 #define x(t, n) BCH_CSUM_##t = n,
1551         BCH_CSUM_TYPES()
1552 #undef x
1553         BCH_CSUM_NR
1554 };
1555
1556 static const unsigned bch_crc_bytes[] = {
1557         [BCH_CSUM_none]                         = 0,
1558         [BCH_CSUM_crc32c_nonzero]               = 4,
1559         [BCH_CSUM_crc32c]                       = 4,
1560         [BCH_CSUM_crc64_nonzero]                = 8,
1561         [BCH_CSUM_crc64]                        = 8,
1562         [BCH_CSUM_xxhash]                       = 8,
1563         [BCH_CSUM_chacha20_poly1305_80]         = 10,
1564         [BCH_CSUM_chacha20_poly1305_128]        = 16,
1565 };
1566
1567 static inline _Bool bch2_csum_type_is_encryption(enum bch_csum_type type)
1568 {
1569         switch (type) {
1570         case BCH_CSUM_chacha20_poly1305_80:
1571         case BCH_CSUM_chacha20_poly1305_128:
1572                 return true;
1573         default:
1574                 return false;
1575         }
1576 }
1577
1578 #define BCH_CSUM_OPTS()                 \
1579         x(none,                 0)      \
1580         x(crc32c,               1)      \
1581         x(crc64,                2)      \
1582         x(xxhash,               3)
1583
1584 enum bch_csum_opts {
1585 #define x(t, n) BCH_CSUM_OPT_##t = n,
1586         BCH_CSUM_OPTS()
1587 #undef x
1588         BCH_CSUM_OPT_NR
1589 };
1590
1591 #define BCH_COMPRESSION_TYPES()         \
1592         x(none,                 0)      \
1593         x(lz4_old,              1)      \
1594         x(gzip,                 2)      \
1595         x(lz4,                  3)      \
1596         x(zstd,                 4)      \
1597         x(incompressible,       5)
1598
1599 enum bch_compression_type {
1600 #define x(t, n) BCH_COMPRESSION_TYPE_##t = n,
1601         BCH_COMPRESSION_TYPES()
1602 #undef x
1603         BCH_COMPRESSION_TYPE_NR
1604 };
1605
1606 #define BCH_COMPRESSION_OPTS()          \
1607         x(none,         0)              \
1608         x(lz4,          1)              \
1609         x(gzip,         2)              \
1610         x(zstd,         3)
1611
1612 enum bch_compression_opts {
1613 #define x(t, n) BCH_COMPRESSION_OPT_##t = n,
1614         BCH_COMPRESSION_OPTS()
1615 #undef x
1616         BCH_COMPRESSION_OPT_NR
1617 };
1618
1619 /*
1620  * Magic numbers
1621  *
1622  * The various other data structures have their own magic numbers, which are
1623  * xored with the first part of the cache set's UUID
1624  */
1625
1626 #define BCACHE_MAGIC                                                    \
1627         UUID_LE(0xf67385c6, 0x1a4e, 0xca45,                             \
1628                 0x82, 0x65, 0xf5, 0x7f, 0x48, 0xba, 0x6d, 0x81)
1629
1630 #define BCACHEFS_STATFS_MAGIC           0xca451a4e
1631
1632 #define JSET_MAGIC              __cpu_to_le64(0x245235c1a3625032ULL)
1633 #define BSET_MAGIC              __cpu_to_le64(0x90135c78b99e07f5ULL)
1634
1635 static inline __le64 __bch2_sb_magic(struct bch_sb *sb)
1636 {
1637         __le64 ret;
1638         memcpy(&ret, &sb->uuid, sizeof(ret));
1639         return ret;
1640 }
1641
1642 static inline __u64 __jset_magic(struct bch_sb *sb)
1643 {
1644         return __le64_to_cpu(__bch2_sb_magic(sb) ^ JSET_MAGIC);
1645 }
1646
1647 static inline __u64 __bset_magic(struct bch_sb *sb)
1648 {
1649         return __le64_to_cpu(__bch2_sb_magic(sb) ^ BSET_MAGIC);
1650 }
1651
1652 /* Journal */
1653
1654 #define JSET_KEYS_U64s  (sizeof(struct jset_entry) / sizeof(__u64))
1655
1656 #define BCH_JSET_ENTRY_TYPES()                  \
1657         x(btree_keys,           0)              \
1658         x(btree_root,           1)              \
1659         x(prio_ptrs,            2)              \
1660         x(blacklist,            3)              \
1661         x(blacklist_v2,         4)              \
1662         x(usage,                5)              \
1663         x(data_usage,           6)              \
1664         x(clock,                7)              \
1665         x(dev_usage,            8)              \
1666         x(log,                  9)
1667
1668 enum {
1669 #define x(f, nr)        BCH_JSET_ENTRY_##f      = nr,
1670         BCH_JSET_ENTRY_TYPES()
1671 #undef x
1672         BCH_JSET_ENTRY_NR
1673 };
1674
1675 /*
1676  * Journal sequence numbers can be blacklisted: bsets record the max sequence
1677  * number of all the journal entries they contain updates for, so that on
1678  * recovery we can ignore those bsets that contain index updates newer that what
1679  * made it into the journal.
1680  *
1681  * This means that we can't reuse that journal_seq - we have to skip it, and
1682  * then record that we skipped it so that the next time we crash and recover we
1683  * don't think there was a missing journal entry.
1684  */
1685 struct jset_entry_blacklist {
1686         struct jset_entry       entry;
1687         __le64                  seq;
1688 };
1689
1690 struct jset_entry_blacklist_v2 {
1691         struct jset_entry       entry;
1692         __le64                  start;
1693         __le64                  end;
1694 };
1695
1696 #define BCH_FS_USAGE_TYPES()                    \
1697         x(reserved,             0)              \
1698         x(inodes,               1)              \
1699         x(key_version,          2)
1700
1701 enum {
1702 #define x(f, nr)        BCH_FS_USAGE_##f        = nr,
1703         BCH_FS_USAGE_TYPES()
1704 #undef x
1705         BCH_FS_USAGE_NR
1706 };
1707
1708 struct jset_entry_usage {
1709         struct jset_entry       entry;
1710         __le64                  v;
1711 } __attribute__((packed));
1712
1713 struct jset_entry_data_usage {
1714         struct jset_entry       entry;
1715         __le64                  v;
1716         struct bch_replicas_entry r;
1717 } __attribute__((packed));
1718
1719 struct jset_entry_clock {
1720         struct jset_entry       entry;
1721         __u8                    rw;
1722         __u8                    pad[7];
1723         __le64                  time;
1724 } __attribute__((packed));
1725
1726 struct jset_entry_dev_usage_type {
1727         __le64                  buckets;
1728         __le64                  sectors;
1729         __le64                  fragmented;
1730 } __attribute__((packed));
1731
1732 struct jset_entry_dev_usage {
1733         struct jset_entry       entry;
1734         __le32                  dev;
1735         __u32                   pad;
1736
1737         __le64                  buckets_ec;
1738         __le64                  buckets_unavailable;
1739
1740         struct jset_entry_dev_usage_type d[];
1741 } __attribute__((packed));
1742
1743 static inline unsigned jset_entry_dev_usage_nr_types(struct jset_entry_dev_usage *u)
1744 {
1745         return (vstruct_bytes(&u->entry) - sizeof(struct jset_entry_dev_usage)) /
1746                 sizeof(struct jset_entry_dev_usage_type);
1747 }
1748
1749 struct jset_entry_log {
1750         struct jset_entry       entry;
1751         u8                      d[];
1752 } __attribute__((packed));
1753
1754 /*
1755  * On disk format for a journal entry:
1756  * seq is monotonically increasing; every journal entry has its own unique
1757  * sequence number.
1758  *
1759  * last_seq is the oldest journal entry that still has keys the btree hasn't
1760  * flushed to disk yet.
1761  *
1762  * version is for on disk format changes.
1763  */
1764 struct jset {
1765         struct bch_csum         csum;
1766
1767         __le64                  magic;
1768         __le64                  seq;
1769         __le32                  version;
1770         __le32                  flags;
1771
1772         __le32                  u64s; /* size of d[] in u64s */
1773
1774         __u8                    encrypted_start[0];
1775
1776         __le16                  _read_clock; /* no longer used */
1777         __le16                  _write_clock;
1778
1779         /* Sequence number of oldest dirty journal entry */
1780         __le64                  last_seq;
1781
1782
1783         union {
1784                 struct jset_entry start[0];
1785                 __u64           _data[0];
1786         };
1787 } __attribute__((packed, aligned(8)));
1788
1789 LE32_BITMASK(JSET_CSUM_TYPE,    struct jset, flags, 0, 4);
1790 LE32_BITMASK(JSET_BIG_ENDIAN,   struct jset, flags, 4, 5);
1791 LE32_BITMASK(JSET_NO_FLUSH,     struct jset, flags, 5, 6);
1792
1793 #define BCH_JOURNAL_BUCKETS_MIN         8
1794
1795 /* Btree: */
1796
1797 #define BCH_BTREE_IDS()                         \
1798         x(extents,      0)                      \
1799         x(inodes,       1)                      \
1800         x(dirents,      2)                      \
1801         x(xattrs,       3)                      \
1802         x(alloc,        4)                      \
1803         x(quotas,       5)                      \
1804         x(stripes,      6)                      \
1805         x(reflink,      7)                      \
1806         x(subvolumes,   8)                      \
1807         x(snapshots,    9)
1808
1809 enum btree_id {
1810 #define x(kwd, val) BTREE_ID_##kwd = val,
1811         BCH_BTREE_IDS()
1812 #undef x
1813         BTREE_ID_NR
1814 };
1815
1816 #define BTREE_MAX_DEPTH         4U
1817
1818 /* Btree nodes */
1819
1820 /*
1821  * Btree nodes
1822  *
1823  * On disk a btree node is a list/log of these; within each set the keys are
1824  * sorted
1825  */
1826 struct bset {
1827         __le64                  seq;
1828
1829         /*
1830          * Highest journal entry this bset contains keys for.
1831          * If on recovery we don't see that journal entry, this bset is ignored:
1832          * this allows us to preserve the order of all index updates after a
1833          * crash, since the journal records a total order of all index updates
1834          * and anything that didn't make it to the journal doesn't get used.
1835          */
1836         __le64                  journal_seq;
1837
1838         __le32                  flags;
1839         __le16                  version;
1840         __le16                  u64s; /* count of d[] in u64s */
1841
1842         union {
1843                 struct bkey_packed start[0];
1844                 __u64           _data[0];
1845         };
1846 } __attribute__((packed, aligned(8)));
1847
1848 LE32_BITMASK(BSET_CSUM_TYPE,    struct bset, flags, 0, 4);
1849
1850 LE32_BITMASK(BSET_BIG_ENDIAN,   struct bset, flags, 4, 5);
1851 LE32_BITMASK(BSET_SEPARATE_WHITEOUTS,
1852                                 struct bset, flags, 5, 6);
1853
1854 /* Sector offset within the btree node: */
1855 LE32_BITMASK(BSET_OFFSET,       struct bset, flags, 16, 32);
1856
1857 struct btree_node {
1858         struct bch_csum         csum;
1859         __le64                  magic;
1860
1861         /* this flags field is encrypted, unlike bset->flags: */
1862         __le64                  flags;
1863
1864         /* Closed interval: */
1865         struct bpos             min_key;
1866         struct bpos             max_key;
1867         struct bch_extent_ptr   _ptr; /* not used anymore */
1868         struct bkey_format      format;
1869
1870         union {
1871         struct bset             keys;
1872         struct {
1873                 __u8            pad[22];
1874                 __le16          u64s;
1875                 __u64           _data[0];
1876
1877         };
1878         };
1879 } __attribute__((packed, aligned(8)));
1880
1881 LE64_BITMASK(BTREE_NODE_ID,     struct btree_node, flags,  0,  4);
1882 LE64_BITMASK(BTREE_NODE_LEVEL,  struct btree_node, flags,  4,  8);
1883 LE64_BITMASK(BTREE_NODE_NEW_EXTENT_OVERWRITE,
1884                                 struct btree_node, flags,  8,  9);
1885 /* 9-32 unused */
1886 LE64_BITMASK(BTREE_NODE_SEQ,    struct btree_node, flags, 32, 64);
1887
1888 struct btree_node_entry {
1889         struct bch_csum         csum;
1890
1891         union {
1892         struct bset             keys;
1893         struct {
1894                 __u8            pad[22];
1895                 __le16          u64s;
1896                 __u64           _data[0];
1897
1898         };
1899         };
1900 } __attribute__((packed, aligned(8)));
1901
1902 #endif /* _BCACHEFS_FORMAT_H */