]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/bcachefs_format.h
7ad080bfff318f0b4ef3e71e279364132bcca196
[bcachefs-tools-debian] / libbcachefs / bcachefs_format.h
1 #ifndef _BCACHEFS_FORMAT_H
2 #define _BCACHEFS_FORMAT_H
3
4 /*
5  * bcachefs on disk data structures
6  *
7  * OVERVIEW:
8  *
9  * There are three main types of on disk data structures in bcachefs (this is
10  * reduced from 5 in bcache)
11  *
12  *  - superblock
13  *  - journal
14  *  - btree
15  *
16  * The btree is the primary structure; most metadata exists as keys in the
17  * various btrees. There are only a small number of btrees, they're not
18  * sharded - we have one btree for extents, another for inodes, et cetera.
19  *
20  * SUPERBLOCK:
21  *
22  * The superblock contains the location of the journal, the list of devices in
23  * the filesystem, and in general any metadata we need in order to decide
24  * whether we can start a filesystem or prior to reading the journal/btree
25  * roots.
26  *
27  * The superblock is extensible, and most of the contents of the superblock are
28  * in variable length, type tagged fields; see struct bch_sb_field.
29  *
30  * Backup superblocks do not reside in a fixed location; also, superblocks do
31  * not have a fixed size. To locate backup superblocks we have struct
32  * bch_sb_layout; we store a copy of this inside every superblock, and also
33  * before the first superblock.
34  *
35  * JOURNAL:
36  *
37  * The journal primarily records btree updates in the order they occurred;
38  * journal replay consists of just iterating over all the keys in the open
39  * journal entries and re-inserting them into the btrees.
40  *
41  * The journal also contains entry types for the btree roots, and blacklisted
42  * journal sequence numbers (see journal_seq_blacklist.c).
43  *
44  * BTREE:
45  *
46  * bcachefs btrees are copy on write b+ trees, where nodes are big (typically
47  * 128k-256k) and log structured. We use struct btree_node for writing the first
48  * entry in a given node (offset 0), and struct btree_node_entry for all
49  * subsequent writes.
50  *
51  * After the header, btree node entries contain a list of keys in sorted order.
52  * Values are stored inline with the keys; since values are variable length (and
53  * keys effectively are variable length too, due to packing) we can't do random
54  * access without building up additional in memory tables in the btree node read
55  * path.
56  *
57  * BTREE KEYS (struct bkey):
58  *
59  * The various btrees share a common format for the key - so as to avoid
60  * switching in fastpath lookup/comparison code - but define their own
61  * structures for the key values.
62  *
63  * The size of a key/value pair is stored as a u8 in units of u64s, so the max
64  * size is just under 2k. The common part also contains a type tag for the
65  * value, and a format field indicating whether the key is packed or not (and
66  * also meant to allow adding new key fields in the future, if desired).
67  *
68  * bkeys, when stored within a btree node, may also be packed. In that case, the
69  * bkey_format in that node is used to unpack it. Packed bkeys mean that we can
70  * be generous with field sizes in the common part of the key format (64 bit
71  * inode number, 64 bit offset, 96 bit version field, etc.) for negligible cost.
72  */
73
74 #include <asm/types.h>
75 #include <asm/byteorder.h>
76 #include <linux/uuid.h>
77
78 #define LE_BITMASK(_bits, name, type, field, offset, end)               \
79 static const unsigned   name##_OFFSET = offset;                         \
80 static const unsigned   name##_BITS = (end - offset);                   \
81 static const __u##_bits name##_MAX = (1ULL << (end - offset)) - 1;      \
82                                                                         \
83 static inline __u64 name(const type *k)                                 \
84 {                                                                       \
85         return (__le##_bits##_to_cpu(k->field) >> offset) &             \
86                 ~(~0ULL << (end - offset));                             \
87 }                                                                       \
88                                                                         \
89 static inline void SET_##name(type *k, __u64 v)                         \
90 {                                                                       \
91         __u##_bits new = __le##_bits##_to_cpu(k->field);                \
92                                                                         \
93         new &= ~(~(~0ULL << (end - offset)) << offset);                 \
94         new |= (v & ~(~0ULL << (end - offset))) << offset;              \
95         k->field = __cpu_to_le##_bits(new);                             \
96 }
97
98 #define LE16_BITMASK(n, t, f, o, e)     LE_BITMASK(16, n, t, f, o, e)
99 #define LE32_BITMASK(n, t, f, o, e)     LE_BITMASK(32, n, t, f, o, e)
100 #define LE64_BITMASK(n, t, f, o, e)     LE_BITMASK(64, n, t, f, o, e)
101
102 struct bkey_format {
103         __u8            key_u64s;
104         __u8            nr_fields;
105         /* One unused slot for now: */
106         __u8            bits_per_field[6];
107         __le64          field_offset[6];
108 };
109
110 /* Btree keys - all units are in sectors */
111
112 struct bpos {
113         /*
114          * Word order matches machine byte order - btree code treats a bpos as a
115          * single large integer, for search/comparison purposes
116          *
117          * Note that wherever a bpos is embedded in another on disk data
118          * structure, it has to be byte swabbed when reading in metadata that
119          * wasn't written in native endian order:
120          */
121 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
122         __u32           snapshot;
123         __u64           offset;
124         __u64           inode;
125 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
126         __u64           inode;
127         __u64           offset;         /* Points to end of extent - sectors */
128         __u32           snapshot;
129 #else
130 #error edit for your odd byteorder.
131 #endif
132 } __attribute__((packed, aligned(4)));
133
134 #define KEY_INODE_MAX                   ((__u64)~0ULL)
135 #define KEY_OFFSET_MAX                  ((__u64)~0ULL)
136 #define KEY_SNAPSHOT_MAX                ((__u32)~0U)
137 #define KEY_SIZE_MAX                    ((__u32)~0U)
138
139 static inline struct bpos POS(__u64 inode, __u64 offset)
140 {
141         struct bpos ret;
142
143         ret.inode       = inode;
144         ret.offset      = offset;
145         ret.snapshot    = 0;
146
147         return ret;
148 }
149
150 #define POS_MIN                         POS(0, 0)
151 #define POS_MAX                         POS(KEY_INODE_MAX, KEY_OFFSET_MAX)
152
153 /* Empty placeholder struct, for container_of() */
154 struct bch_val {
155         __u64           __nothing[0];
156 };
157
158 struct bversion {
159 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
160         __u64           lo;
161         __u32           hi;
162 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
163         __u32           hi;
164         __u64           lo;
165 #endif
166 } __attribute__((packed, aligned(4)));
167
168 struct bkey {
169         /* Size of combined key and value, in u64s */
170         __u8            u64s;
171
172         /* Format of key (0 for format local to btree node) */
173 #if defined(__LITTLE_ENDIAN_BITFIELD)
174         __u8            format:7,
175                         needs_whiteout:1;
176 #elif defined (__BIG_ENDIAN_BITFIELD)
177         __u8            needs_whiteout:1,
178                         format:7;
179 #else
180 #error edit for your odd byteorder.
181 #endif
182
183         /* Type of the value */
184         __u8            type;
185
186 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
187         __u8            pad[1];
188
189         struct bversion version;
190         __u32           size;           /* extent size, in sectors */
191         struct bpos     p;
192 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
193         struct bpos     p;
194         __u32           size;           /* extent size, in sectors */
195         struct bversion version;
196
197         __u8            pad[1];
198 #endif
199 } __attribute__((packed, aligned(8)));
200
201 struct bkey_packed {
202         __u64           _data[0];
203
204         /* Size of combined key and value, in u64s */
205         __u8            u64s;
206
207         /* Format of key (0 for format local to btree node) */
208
209         /*
210          * XXX: next incompat on disk format change, switch format and
211          * needs_whiteout - bkey_packed() will be cheaper if format is the high
212          * bits of the bitfield
213          */
214 #if defined(__LITTLE_ENDIAN_BITFIELD)
215         __u8            format:7,
216                         needs_whiteout:1;
217 #elif defined (__BIG_ENDIAN_BITFIELD)
218         __u8            needs_whiteout:1,
219                         format:7;
220 #endif
221
222         /* Type of the value */
223         __u8            type;
224         __u8            key_start[0];
225
226         /*
227          * We copy bkeys with struct assignment in various places, and while
228          * that shouldn't be done with packed bkeys we can't disallow it in C,
229          * and it's legal to cast a bkey to a bkey_packed  - so padding it out
230          * to the same size as struct bkey should hopefully be safest.
231          */
232         __u8            pad[sizeof(struct bkey) - 3];
233 } __attribute__((packed, aligned(8)));
234
235 #define BKEY_U64s                       (sizeof(struct bkey) / sizeof(__u64))
236 #define KEY_PACKED_BITS_START           24
237
238 #define KEY_FORMAT_LOCAL_BTREE          0
239 #define KEY_FORMAT_CURRENT              1
240
241 enum bch_bkey_fields {
242         BKEY_FIELD_INODE,
243         BKEY_FIELD_OFFSET,
244         BKEY_FIELD_SNAPSHOT,
245         BKEY_FIELD_SIZE,
246         BKEY_FIELD_VERSION_HI,
247         BKEY_FIELD_VERSION_LO,
248         BKEY_NR_FIELDS,
249 };
250
251 #define bkey_format_field(name, field)                                  \
252         [BKEY_FIELD_##name] = (sizeof(((struct bkey *) NULL)->field) * 8)
253
254 #define BKEY_FORMAT_CURRENT                                             \
255 ((struct bkey_format) {                                                 \
256         .key_u64s       = BKEY_U64s,                                    \
257         .nr_fields      = BKEY_NR_FIELDS,                               \
258         .bits_per_field = {                                             \
259                 bkey_format_field(INODE,        p.inode),               \
260                 bkey_format_field(OFFSET,       p.offset),              \
261                 bkey_format_field(SNAPSHOT,     p.snapshot),            \
262                 bkey_format_field(SIZE,         size),                  \
263                 bkey_format_field(VERSION_HI,   version.hi),            \
264                 bkey_format_field(VERSION_LO,   version.lo),            \
265         },                                                              \
266 })
267
268 /* bkey with inline value */
269 struct bkey_i {
270         __u64                   _data[0];
271
272         union {
273         struct {
274                 /* Size of combined key and value, in u64s */
275                 __u8            u64s;
276         };
277         struct {
278                 struct bkey     k;
279                 struct bch_val  v;
280         };
281         };
282 };
283
284 #define KEY(_inode, _offset, _size)                                     \
285 ((struct bkey) {                                                        \
286         .u64s           = BKEY_U64s,                                    \
287         .format         = KEY_FORMAT_CURRENT,                           \
288         .p              = POS(_inode, _offset),                         \
289         .size           = _size,                                        \
290 })
291
292 static inline void bkey_init(struct bkey *k)
293 {
294         *k = KEY(0, 0, 0);
295 }
296
297 #define bkey_bytes(_k)          ((_k)->u64s * sizeof(__u64))
298
299 #define __BKEY_PADDED(key, pad)                                 \
300         struct { struct bkey_i key; __u64 key ## _pad[pad]; }
301
302 #define BKEY_VAL_TYPE(name, nr)                                         \
303 struct bkey_i_##name {                                                  \
304         union {                                                         \
305                 struct bkey             k;                              \
306                 struct bkey_i           k_i;                            \
307         };                                                              \
308         struct bch_##name               v;                              \
309 }
310
311 /*
312  * - DELETED keys are used internally to mark keys that should be ignored but
313  *   override keys in composition order.  Their version number is ignored.
314  *
315  * - DISCARDED keys indicate that the data is all 0s because it has been
316  *   discarded. DISCARDs may have a version; if the version is nonzero the key
317  *   will be persistent, otherwise the key will be dropped whenever the btree
318  *   node is rewritten (like DELETED keys).
319  *
320  * - ERROR: any read of the data returns a read error, as the data was lost due
321  *   to a failing device. Like DISCARDED keys, they can be removed (overridden)
322  *   by new writes or cluster-wide GC. Node repair can also overwrite them with
323  *   the same or a more recent version number, but not with an older version
324  *   number.
325 */
326 #define KEY_TYPE_DELETED                0
327 #define KEY_TYPE_DISCARD                1
328 #define KEY_TYPE_ERROR                  2
329 #define KEY_TYPE_COOKIE                 3
330 #define KEY_TYPE_PERSISTENT_DISCARD     4
331 #define KEY_TYPE_GENERIC_NR             128
332
333 struct bch_cookie {
334         struct bch_val          v;
335         __le64                  cookie;
336 };
337 BKEY_VAL_TYPE(cookie,           KEY_TYPE_COOKIE);
338
339 /* Extents */
340
341 /*
342  * In extent bkeys, the value is a list of pointers (bch_extent_ptr), optionally
343  * preceded by checksum/compression information (bch_extent_crc32 or
344  * bch_extent_crc64).
345  *
346  * One major determining factor in the format of extents is how we handle and
347  * represent extents that have been partially overwritten and thus trimmed:
348  *
349  * If an extent is not checksummed or compressed, when the extent is trimmed we
350  * don't have to remember the extent we originally allocated and wrote: we can
351  * merely adjust ptr->offset to point to the start of the data that is currently
352  * live. The size field in struct bkey records the current (live) size of the
353  * extent, and is also used to mean "size of region on disk that we point to" in
354  * this case.
355  *
356  * Thus an extent that is not checksummed or compressed will consist only of a
357  * list of bch_extent_ptrs, with none of the fields in
358  * bch_extent_crc32/bch_extent_crc64.
359  *
360  * When an extent is checksummed or compressed, it's not possible to read only
361  * the data that is currently live: we have to read the entire extent that was
362  * originally written, and then return only the part of the extent that is
363  * currently live.
364  *
365  * Thus, in addition to the current size of the extent in struct bkey, we need
366  * to store the size of the originally allocated space - this is the
367  * compressed_size and uncompressed_size fields in bch_extent_crc32/64. Also,
368  * when the extent is trimmed, instead of modifying the offset field of the
369  * pointer, we keep a second smaller offset field - "offset into the original
370  * extent of the currently live region".
371  *
372  * The other major determining factor is replication and data migration:
373  *
374  * Each pointer may have its own bch_extent_crc32/64. When doing a replicated
375  * write, we will initially write all the replicas in the same format, with the
376  * same checksum type and compression format - however, when copygc runs later (or
377  * tiering/cache promotion, anything that moves data), it is not in general
378  * going to rewrite all the pointers at once - one of the replicas may be in a
379  * bucket on one device that has very little fragmentation while another lives
380  * in a bucket that has become heavily fragmented, and thus is being rewritten
381  * sooner than the rest.
382  *
383  * Thus it will only move a subset of the pointers (or in the case of
384  * tiering/cache promotion perhaps add a single pointer without dropping any
385  * current pointers), and if the extent has been partially overwritten it must
386  * write only the currently live portion (or copygc would not be able to reduce
387  * fragmentation!) - which necessitates a different bch_extent_crc format for
388  * the new pointer.
389  *
390  * But in the interests of space efficiency, we don't want to store one
391  * bch_extent_crc for each pointer if we don't have to.
392  *
393  * Thus, a bch_extent consists of bch_extent_crc32s, bch_extent_crc64s, and
394  * bch_extent_ptrs appended arbitrarily one after the other. We determine the
395  * type of a given entry with a scheme similar to utf8 (except we're encoding a
396  * type, not a size), encoding the type in the position of the first set bit:
397  *
398  * bch_extent_crc32     - 0b1
399  * bch_extent_ptr       - 0b10
400  * bch_extent_crc64     - 0b100
401  *
402  * We do it this way because bch_extent_crc32 is _very_ constrained on bits (and
403  * bch_extent_crc64 is the least constrained).
404  *
405  * Then, each bch_extent_crc32/64 applies to the pointers that follow after it,
406  * until the next bch_extent_crc32/64.
407  *
408  * If there are no bch_extent_crcs preceding a bch_extent_ptr, then that pointer
409  * is neither checksummed nor compressed.
410  */
411
412 /* 128 bits, sufficient for cryptographic MACs: */
413 struct bch_csum {
414         __le64                  lo;
415         __le64                  hi;
416 } __attribute__((packed, aligned(8)));
417
418 enum bch_csum_type {
419         BCH_CSUM_NONE                   = 0,
420         BCH_CSUM_CRC32C_NONZERO         = 1,
421         BCH_CSUM_CRC64_NONZERO          = 2,
422         BCH_CSUM_CHACHA20_POLY1305_80   = 3,
423         BCH_CSUM_CHACHA20_POLY1305_128  = 4,
424         BCH_CSUM_CRC32C                 = 5,
425         BCH_CSUM_CRC64                  = 6,
426         BCH_CSUM_NR                     = 7,
427 };
428
429 static const unsigned bch_crc_bytes[] = {
430         [BCH_CSUM_NONE]                         = 0,
431         [BCH_CSUM_CRC32C_NONZERO]               = 4,
432         [BCH_CSUM_CRC32C]                       = 4,
433         [BCH_CSUM_CRC64_NONZERO]                = 8,
434         [BCH_CSUM_CRC64]                        = 8,
435         [BCH_CSUM_CHACHA20_POLY1305_80]         = 10,
436         [BCH_CSUM_CHACHA20_POLY1305_128]        = 16,
437 };
438
439 static inline _Bool bch2_csum_type_is_encryption(enum bch_csum_type type)
440 {
441         switch (type) {
442         case BCH_CSUM_CHACHA20_POLY1305_80:
443         case BCH_CSUM_CHACHA20_POLY1305_128:
444                 return true;
445         default:
446                 return false;
447         }
448 }
449
450 enum bch_compression_type {
451         BCH_COMPRESSION_NONE            = 0,
452         BCH_COMPRESSION_LZ4_OLD         = 1,
453         BCH_COMPRESSION_GZIP            = 2,
454         BCH_COMPRESSION_LZ4             = 3,
455         BCH_COMPRESSION_ZSTD            = 4,
456         BCH_COMPRESSION_NR              = 5,
457 };
458
459 #define BCH_EXTENT_ENTRY_TYPES()                \
460         x(ptr,                  0)              \
461         x(crc32,                1)              \
462         x(crc64,                2)              \
463         x(crc128,               3)
464 #define BCH_EXTENT_ENTRY_MAX    4
465
466 enum bch_extent_entry_type {
467 #define x(f, n) BCH_EXTENT_ENTRY_##f = n,
468         BCH_EXTENT_ENTRY_TYPES()
469 #undef x
470 };
471
472 /* Compressed/uncompressed size are stored biased by 1: */
473 struct bch_extent_crc32 {
474 #if defined(__LITTLE_ENDIAN_BITFIELD)
475         __u32                   type:2,
476                                 _compressed_size:7,
477                                 _uncompressed_size:7,
478                                 offset:7,
479                                 _unused:1,
480                                 csum_type:4,
481                                 compression_type:4;
482         __u32                   csum;
483 #elif defined (__BIG_ENDIAN_BITFIELD)
484         __u32                   csum;
485         __u32                   compression_type:4,
486                                 csum_type:4,
487                                 _unused:1,
488                                 offset:7,
489                                 _uncompressed_size:7,
490                                 _compressed_size:7,
491                                 type:2;
492 #endif
493 } __attribute__((packed, aligned(8)));
494
495 #define CRC32_SIZE_MAX          (1U << 7)
496 #define CRC32_NONCE_MAX         0
497
498 struct bch_extent_crc64 {
499 #if defined(__LITTLE_ENDIAN_BITFIELD)
500         __u64                   type:3,
501                                 _compressed_size:9,
502                                 _uncompressed_size:9,
503                                 offset:9,
504                                 nonce:10,
505                                 csum_type:4,
506                                 compression_type:4,
507                                 csum_hi:16;
508 #elif defined (__BIG_ENDIAN_BITFIELD)
509         __u64                   csum_hi:16,
510                                 compression_type:4,
511                                 csum_type:4,
512                                 nonce:10,
513                                 offset:9,
514                                 _uncompressed_size:9,
515                                 _compressed_size:9,
516                                 type:3;
517 #endif
518         __u64                   csum_lo;
519 } __attribute__((packed, aligned(8)));
520
521 #define CRC64_SIZE_MAX          (1U << 9)
522 #define CRC64_NONCE_MAX         ((1U << 10) - 1)
523
524 struct bch_extent_crc128 {
525 #if defined(__LITTLE_ENDIAN_BITFIELD)
526         __u64                   type:4,
527                                 _compressed_size:13,
528                                 _uncompressed_size:13,
529                                 offset:13,
530                                 nonce:13,
531                                 csum_type:4,
532                                 compression_type:4;
533 #elif defined (__BIG_ENDIAN_BITFIELD)
534         __u64                   compression_type:4,
535                                 csum_type:4,
536                                 nonce:13,
537                                 offset:13,
538                                 _uncompressed_size:13,
539                                 _compressed_size:13,
540                                 type:4;
541 #endif
542         struct bch_csum         csum;
543 } __attribute__((packed, aligned(8)));
544
545 #define CRC128_SIZE_MAX         (1U << 13)
546 #define CRC128_NONCE_MAX        ((1U << 13) - 1)
547
548 /*
549  * @reservation - pointer hasn't been written to, just reserved
550  */
551 struct bch_extent_ptr {
552 #if defined(__LITTLE_ENDIAN_BITFIELD)
553         __u64                   type:1,
554                                 cached:1,
555                                 erasure_coded:1,
556                                 reservation:1,
557                                 offset:44, /* 8 petabytes */
558                                 dev:8,
559                                 gen:8;
560 #elif defined (__BIG_ENDIAN_BITFIELD)
561         __u64                   gen:8,
562                                 dev:8,
563                                 offset:44,
564                                 reservation:1,
565                                 erasure_coded:1,
566                                 cached:1,
567                                 type:1;
568 #endif
569 } __attribute__((packed, aligned(8)));
570
571 struct bch_extent_reservation {
572 #if defined(__LITTLE_ENDIAN_BITFIELD)
573         __u64                   type:5,
574                                 unused:23,
575                                 replicas:4,
576                                 generation:32;
577 #elif defined (__BIG_ENDIAN_BITFIELD)
578         __u64                   generation:32,
579                                 replicas:4,
580                                 unused:23,
581                                 type:5;
582 #endif
583 };
584
585 union bch_extent_entry {
586 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ ||  __BITS_PER_LONG == 64
587         unsigned long                   type;
588 #elif __BITS_PER_LONG == 32
589         struct {
590                 unsigned long           pad;
591                 unsigned long           type;
592         };
593 #else
594 #error edit for your odd byteorder.
595 #endif
596
597 #define x(f, n) struct bch_extent_##f   f;
598         BCH_EXTENT_ENTRY_TYPES()
599 #undef x
600 };
601
602 enum {
603         BCH_EXTENT              = 128,
604
605         /*
606          * This is kind of a hack, we're overloading the type for a boolean that
607          * really should be part of the value - BCH_EXTENT and BCH_EXTENT_CACHED
608          * have the same value type:
609          */
610         BCH_EXTENT_CACHED       = 129,
611
612         /*
613          * Persistent reservation:
614          */
615         BCH_RESERVATION         = 130,
616 };
617
618 struct bch_extent {
619         struct bch_val          v;
620
621         union bch_extent_entry  start[0];
622         __u64                   _data[0];
623 } __attribute__((packed, aligned(8)));
624 BKEY_VAL_TYPE(extent,           BCH_EXTENT);
625
626 struct bch_reservation {
627         struct bch_val          v;
628
629         __le32                  generation;
630         __u8                    nr_replicas;
631         __u8                    pad[3];
632 } __attribute__((packed, aligned(8)));
633 BKEY_VAL_TYPE(reservation,      BCH_RESERVATION);
634
635 /* Maximum size (in u64s) a single pointer could be: */
636 #define BKEY_EXTENT_PTR_U64s_MAX\
637         ((sizeof(struct bch_extent_crc128) +                    \
638           sizeof(struct bch_extent_ptr)) / sizeof(u64))
639
640 /* Maximum possible size of an entire extent value: */
641 #define BKEY_EXTENT_VAL_U64s_MAX                                \
642         (BKEY_EXTENT_PTR_U64s_MAX * (BCH_REPLICAS_MAX + 1))
643
644 #define BKEY_PADDED(key)        __BKEY_PADDED(key, BKEY_EXTENT_VAL_U64s_MAX)
645
646 /* * Maximum possible size of an entire extent, key + value: */
647 #define BKEY_EXTENT_U64s_MAX            (BKEY_U64s + BKEY_EXTENT_VAL_U64s_MAX)
648
649 /* Btree pointers don't carry around checksums: */
650 #define BKEY_BTREE_PTR_VAL_U64s_MAX                             \
651         ((sizeof(struct bch_extent_ptr)) / sizeof(u64) * BCH_REPLICAS_MAX)
652 #define BKEY_BTREE_PTR_U64s_MAX                                 \
653         (BKEY_U64s + BKEY_BTREE_PTR_VAL_U64s_MAX)
654
655 /* Inodes */
656
657 #define BLOCKDEV_INODE_MAX      4096
658
659 #define BCACHEFS_ROOT_INO       4096
660
661 enum bch_inode_types {
662         BCH_INODE_FS            = 128,
663         BCH_INODE_BLOCKDEV      = 129,
664         BCH_INODE_GENERATION    = 130,
665 };
666
667 struct bch_inode {
668         struct bch_val          v;
669
670         __le64                  bi_hash_seed;
671         __le32                  bi_flags;
672         __le16                  bi_mode;
673         __u8                    fields[0];
674 } __attribute__((packed, aligned(8)));
675 BKEY_VAL_TYPE(inode,            BCH_INODE_FS);
676
677 struct bch_inode_generation {
678         struct bch_val          v;
679
680         __le32                  bi_generation;
681         __le32                  pad;
682 } __attribute__((packed, aligned(8)));
683 BKEY_VAL_TYPE(inode_generation, BCH_INODE_GENERATION);
684
685 #define BCH_INODE_FIELDS()                                      \
686         BCH_INODE_FIELD(bi_atime,                       64)     \
687         BCH_INODE_FIELD(bi_ctime,                       64)     \
688         BCH_INODE_FIELD(bi_mtime,                       64)     \
689         BCH_INODE_FIELD(bi_otime,                       64)     \
690         BCH_INODE_FIELD(bi_size,                        64)     \
691         BCH_INODE_FIELD(bi_sectors,                     64)     \
692         BCH_INODE_FIELD(bi_uid,                         32)     \
693         BCH_INODE_FIELD(bi_gid,                         32)     \
694         BCH_INODE_FIELD(bi_nlink,                       32)     \
695         BCH_INODE_FIELD(bi_generation,                  32)     \
696         BCH_INODE_FIELD(bi_dev,                         32)     \
697         BCH_INODE_FIELD(bi_data_checksum,               8)      \
698         BCH_INODE_FIELD(bi_compression,                 8)      \
699         BCH_INODE_FIELD(bi_project,                     32)     \
700         BCH_INODE_FIELD(bi_background_compression,      8)      \
701         BCH_INODE_FIELD(bi_data_replicas,               8)      \
702         BCH_INODE_FIELD(bi_promote_target,              16)     \
703         BCH_INODE_FIELD(bi_foreground_target,           16)     \
704         BCH_INODE_FIELD(bi_background_target,           16)
705
706 #define BCH_INODE_FIELDS_INHERIT()                              \
707         BCH_INODE_FIELD(bi_data_checksum)                       \
708         BCH_INODE_FIELD(bi_compression)                         \
709         BCH_INODE_FIELD(bi_project)                             \
710         BCH_INODE_FIELD(bi_background_compression)              \
711         BCH_INODE_FIELD(bi_data_replicas)                       \
712         BCH_INODE_FIELD(bi_promote_target)                      \
713         BCH_INODE_FIELD(bi_foreground_target)                   \
714         BCH_INODE_FIELD(bi_background_target)
715
716 enum {
717         /*
718          * User flags (get/settable with FS_IOC_*FLAGS, correspond to FS_*_FL
719          * flags)
720          */
721         __BCH_INODE_SYNC        = 0,
722         __BCH_INODE_IMMUTABLE   = 1,
723         __BCH_INODE_APPEND      = 2,
724         __BCH_INODE_NODUMP      = 3,
725         __BCH_INODE_NOATIME     = 4,
726
727         __BCH_INODE_I_SIZE_DIRTY= 5,
728         __BCH_INODE_I_SECTORS_DIRTY= 6,
729         __BCH_INODE_UNLINKED    = 7,
730
731         /* bits 20+ reserved for packed fields below: */
732 };
733
734 #define BCH_INODE_SYNC          (1 << __BCH_INODE_SYNC)
735 #define BCH_INODE_IMMUTABLE     (1 << __BCH_INODE_IMMUTABLE)
736 #define BCH_INODE_APPEND        (1 << __BCH_INODE_APPEND)
737 #define BCH_INODE_NODUMP        (1 << __BCH_INODE_NODUMP)
738 #define BCH_INODE_NOATIME       (1 << __BCH_INODE_NOATIME)
739 #define BCH_INODE_I_SIZE_DIRTY  (1 << __BCH_INODE_I_SIZE_DIRTY)
740 #define BCH_INODE_I_SECTORS_DIRTY (1 << __BCH_INODE_I_SECTORS_DIRTY)
741 #define BCH_INODE_UNLINKED      (1 << __BCH_INODE_UNLINKED)
742
743 LE32_BITMASK(INODE_STR_HASH,    struct bch_inode, bi_flags, 20, 24);
744 LE32_BITMASK(INODE_NR_FIELDS,   struct bch_inode, bi_flags, 24, 32);
745
746 struct bch_inode_blockdev {
747         struct bch_val          v;
748
749         __le64                  i_size;
750         __le64                  i_flags;
751
752         /* Seconds: */
753         __le64                  i_ctime;
754         __le64                  i_mtime;
755
756         uuid_le                 i_uuid;
757         __u8                    i_label[32];
758 } __attribute__((packed, aligned(8)));
759 BKEY_VAL_TYPE(inode_blockdev,   BCH_INODE_BLOCKDEV);
760
761 /* Thin provisioned volume, or cache for another block device? */
762 LE64_BITMASK(CACHED_DEV,        struct bch_inode_blockdev, i_flags, 0,  1)
763
764 /* Dirents */
765
766 /*
767  * Dirents (and xattrs) have to implement string lookups; since our b-tree
768  * doesn't support arbitrary length strings for the key, we instead index by a
769  * 64 bit hash (currently truncated sha1) of the string, stored in the offset
770  * field of the key - using linear probing to resolve hash collisions. This also
771  * provides us with the readdir cookie posix requires.
772  *
773  * Linear probing requires us to use whiteouts for deletions, in the event of a
774  * collision:
775  */
776
777 enum {
778         BCH_DIRENT              = 128,
779         BCH_DIRENT_WHITEOUT     = 129,
780 };
781
782 struct bch_dirent {
783         struct bch_val          v;
784
785         /* Target inode number: */
786         __le64                  d_inum;
787
788         /*
789          * Copy of mode bits 12-15 from the target inode - so userspace can get
790          * the filetype without having to do a stat()
791          */
792         __u8                    d_type;
793
794         __u8                    d_name[];
795 } __attribute__((packed, aligned(8)));
796 BKEY_VAL_TYPE(dirent,           BCH_DIRENT);
797
798 #define BCH_NAME_MAX    (U8_MAX * sizeof(u64) -                         \
799                          sizeof(struct bkey) -                          \
800                          offsetof(struct bch_dirent, d_name))
801
802
803 /* Xattrs */
804
805 enum {
806         BCH_XATTR               = 128,
807         BCH_XATTR_WHITEOUT      = 129,
808 };
809
810 #define BCH_XATTR_INDEX_USER                    0
811 #define BCH_XATTR_INDEX_POSIX_ACL_ACCESS        1
812 #define BCH_XATTR_INDEX_POSIX_ACL_DEFAULT       2
813 #define BCH_XATTR_INDEX_TRUSTED                 3
814 #define BCH_XATTR_INDEX_SECURITY                4
815
816 struct bch_xattr {
817         struct bch_val          v;
818         __u8                    x_type;
819         __u8                    x_name_len;
820         __le16                  x_val_len;
821         __u8                    x_name[];
822 } __attribute__((packed, aligned(8)));
823 BKEY_VAL_TYPE(xattr,            BCH_XATTR);
824
825 /* Bucket/allocation information: */
826
827 enum {
828         BCH_ALLOC               = 128,
829 };
830
831 enum {
832         BCH_ALLOC_FIELD_READ_TIME       = 0,
833         BCH_ALLOC_FIELD_WRITE_TIME      = 1,
834 };
835
836 struct bch_alloc {
837         struct bch_val          v;
838         __u8                    fields;
839         __u8                    gen;
840         __u8                    data[];
841 } __attribute__((packed, aligned(8)));
842 BKEY_VAL_TYPE(alloc,    BCH_ALLOC);
843
844 /* Quotas: */
845
846 enum {
847         BCH_QUOTA               = 128,
848 };
849
850 enum quota_types {
851         QTYP_USR                = 0,
852         QTYP_GRP                = 1,
853         QTYP_PRJ                = 2,
854         QTYP_NR                 = 3,
855 };
856
857 enum quota_counters {
858         Q_SPC                   = 0,
859         Q_INO                   = 1,
860         Q_COUNTERS              = 2,
861 };
862
863 struct bch_quota_counter {
864         __le64                  hardlimit;
865         __le64                  softlimit;
866 };
867
868 struct bch_quota {
869         struct bch_val          v;
870         struct bch_quota_counter c[Q_COUNTERS];
871 } __attribute__((packed, aligned(8)));
872 BKEY_VAL_TYPE(quota,    BCH_QUOTA);
873
874 /* Optional/variable size superblock sections: */
875
876 struct bch_sb_field {
877         __u64                   _data[0];
878         __le32                  u64s;
879         __le32                  type;
880 };
881
882 #define BCH_SB_FIELDS()         \
883         x(journal,      0)      \
884         x(members,      1)      \
885         x(crypt,        2)      \
886         x(replicas,     3)      \
887         x(quota,        4)      \
888         x(disk_groups,  5)      \
889         x(clean,        6)
890
891 enum bch_sb_field_type {
892 #define x(f, nr)        BCH_SB_FIELD_##f = nr,
893         BCH_SB_FIELDS()
894 #undef x
895         BCH_SB_FIELD_NR
896 };
897
898 /* BCH_SB_FIELD_journal: */
899
900 struct bch_sb_field_journal {
901         struct bch_sb_field     field;
902         __le64                  buckets[0];
903 };
904
905 /* BCH_SB_FIELD_members: */
906
907 #define BCH_MIN_NR_NBUCKETS     (1 << 6)
908
909 struct bch_member {
910         uuid_le                 uuid;
911         __le64                  nbuckets;       /* device size */
912         __le16                  first_bucket;   /* index of first bucket used */
913         __le16                  bucket_size;    /* sectors */
914         __le32                  pad;
915         __le64                  last_mount;     /* time_t */
916
917         __le64                  flags[2];
918 };
919
920 LE64_BITMASK(BCH_MEMBER_STATE,          struct bch_member, flags[0],  0,  4)
921 /* 4-10 unused, was TIER, HAS_(META)DATA */
922 LE64_BITMASK(BCH_MEMBER_REPLACEMENT,    struct bch_member, flags[0], 10, 14)
923 LE64_BITMASK(BCH_MEMBER_DISCARD,        struct bch_member, flags[0], 14, 15)
924 LE64_BITMASK(BCH_MEMBER_DATA_ALLOWED,   struct bch_member, flags[0], 15, 20)
925 LE64_BITMASK(BCH_MEMBER_GROUP,          struct bch_member, flags[0], 20, 28)
926 LE64_BITMASK(BCH_MEMBER_DURABILITY,     struct bch_member, flags[0], 28, 30)
927
928 #define BCH_TIER_MAX                    4U
929
930 #if 0
931 LE64_BITMASK(BCH_MEMBER_NR_READ_ERRORS, struct bch_member, flags[1], 0,  20);
932 LE64_BITMASK(BCH_MEMBER_NR_WRITE_ERRORS,struct bch_member, flags[1], 20, 40);
933 #endif
934
935 enum bch_member_state {
936         BCH_MEMBER_STATE_RW             = 0,
937         BCH_MEMBER_STATE_RO             = 1,
938         BCH_MEMBER_STATE_FAILED         = 2,
939         BCH_MEMBER_STATE_SPARE          = 3,
940         BCH_MEMBER_STATE_NR             = 4,
941 };
942
943 enum cache_replacement {
944         CACHE_REPLACEMENT_LRU           = 0,
945         CACHE_REPLACEMENT_FIFO          = 1,
946         CACHE_REPLACEMENT_RANDOM        = 2,
947         CACHE_REPLACEMENT_NR            = 3,
948 };
949
950 struct bch_sb_field_members {
951         struct bch_sb_field     field;
952         struct bch_member       members[0];
953 };
954
955 /* BCH_SB_FIELD_crypt: */
956
957 struct nonce {
958         __le32                  d[4];
959 };
960
961 struct bch_key {
962         __le64                  key[4];
963 };
964
965 #define BCH_KEY_MAGIC                                   \
966         (((u64) 'b' <<  0)|((u64) 'c' <<  8)|           \
967          ((u64) 'h' << 16)|((u64) '*' << 24)|           \
968          ((u64) '*' << 32)|((u64) 'k' << 40)|           \
969          ((u64) 'e' << 48)|((u64) 'y' << 56))
970
971 struct bch_encrypted_key {
972         __le64                  magic;
973         struct bch_key          key;
974 };
975
976 /*
977  * If this field is present in the superblock, it stores an encryption key which
978  * is used encrypt all other data/metadata. The key will normally be encrypted
979  * with the key userspace provides, but if encryption has been turned off we'll
980  * just store the master key unencrypted in the superblock so we can access the
981  * previously encrypted data.
982  */
983 struct bch_sb_field_crypt {
984         struct bch_sb_field     field;
985
986         __le64                  flags;
987         __le64                  kdf_flags;
988         struct bch_encrypted_key key;
989 };
990
991 LE64_BITMASK(BCH_CRYPT_KDF_TYPE,        struct bch_sb_field_crypt, flags, 0, 4);
992
993 enum bch_kdf_types {
994         BCH_KDF_SCRYPT          = 0,
995         BCH_KDF_NR              = 1,
996 };
997
998 /* stored as base 2 log of scrypt params: */
999 LE64_BITMASK(BCH_KDF_SCRYPT_N,  struct bch_sb_field_crypt, kdf_flags,  0, 16);
1000 LE64_BITMASK(BCH_KDF_SCRYPT_R,  struct bch_sb_field_crypt, kdf_flags, 16, 32);
1001 LE64_BITMASK(BCH_KDF_SCRYPT_P,  struct bch_sb_field_crypt, kdf_flags, 32, 48);
1002
1003 /* BCH_SB_FIELD_replicas: */
1004
1005 enum bch_data_type {
1006         BCH_DATA_NONE           = 0,
1007         BCH_DATA_SB             = 1,
1008         BCH_DATA_JOURNAL        = 2,
1009         BCH_DATA_BTREE          = 3,
1010         BCH_DATA_USER           = 4,
1011         BCH_DATA_CACHED         = 5,
1012         BCH_DATA_NR             = 6,
1013 };
1014
1015 struct bch_replicas_entry {
1016         __u8                    data_type;
1017         __u8                    nr_devs;
1018         __u8                    devs[0];
1019 };
1020
1021 struct bch_sb_field_replicas {
1022         struct bch_sb_field     field;
1023         struct bch_replicas_entry entries[0];
1024 };
1025
1026 /* BCH_SB_FIELD_quota: */
1027
1028 struct bch_sb_quota_counter {
1029         __le32                          timelimit;
1030         __le32                          warnlimit;
1031 };
1032
1033 struct bch_sb_quota_type {
1034         __le64                          flags;
1035         struct bch_sb_quota_counter     c[Q_COUNTERS];
1036 };
1037
1038 struct bch_sb_field_quota {
1039         struct bch_sb_field             field;
1040         struct bch_sb_quota_type        q[QTYP_NR];
1041 } __attribute__((packed, aligned(8)));
1042
1043 /* BCH_SB_FIELD_disk_groups: */
1044
1045 #define BCH_SB_LABEL_SIZE               32
1046
1047 struct bch_disk_group {
1048         __u8                    label[BCH_SB_LABEL_SIZE];
1049         __le64                  flags[2];
1050 };
1051
1052 LE64_BITMASK(BCH_GROUP_DELETED,         struct bch_disk_group, flags[0], 0,  1)
1053 LE64_BITMASK(BCH_GROUP_DATA_ALLOWED,    struct bch_disk_group, flags[0], 1,  6)
1054 LE64_BITMASK(BCH_GROUP_PARENT,          struct bch_disk_group, flags[0], 6, 24)
1055
1056 struct bch_sb_field_disk_groups {
1057         struct bch_sb_field     field;
1058         struct bch_disk_group   entries[0];
1059 };
1060
1061 /*
1062  * On clean shutdown, store btree roots and current journal sequence number in
1063  * the superblock:
1064  */
1065 struct jset_entry {
1066         __le16                  u64s;
1067         __u8                    btree_id;
1068         __u8                    level;
1069         __u8                    type; /* designates what this jset holds */
1070         __u8                    pad[3];
1071
1072         union {
1073                 struct bkey_i   start[0];
1074                 __u64           _data[0];
1075         };
1076 };
1077
1078 struct bch_sb_field_clean {
1079         struct bch_sb_field     field;
1080
1081         __le32                  flags;
1082         __le16                  read_clock;
1083         __le16                  write_clock;
1084         __le64                  journal_seq;
1085
1086         union {
1087                 struct jset_entry start[0];
1088                 __u64           _data[0];
1089         };
1090 };
1091
1092 /* Superblock: */
1093
1094 /*
1095  * Version 8:   BCH_SB_ENCODED_EXTENT_MAX_BITS
1096  *              BCH_MEMBER_DATA_ALLOWED
1097  * Version 9:   incompatible extent nonce change
1098  */
1099
1100 #define BCH_SB_VERSION_MIN              7
1101 #define BCH_SB_VERSION_EXTENT_MAX       8
1102 #define BCH_SB_VERSION_EXTENT_NONCE_V1  9
1103 #define BCH_SB_VERSION_MAX              9
1104
1105 #define BCH_SB_SECTOR                   8
1106 #define BCH_SB_MEMBERS_MAX              64 /* XXX kill */
1107
1108 struct bch_sb_layout {
1109         uuid_le                 magic;  /* bcachefs superblock UUID */
1110         __u8                    layout_type;
1111         __u8                    sb_max_size_bits; /* base 2 of 512 byte sectors */
1112         __u8                    nr_superblocks;
1113         __u8                    pad[5];
1114         __le64                  sb_offset[61];
1115 } __attribute__((packed, aligned(8)));
1116
1117 #define BCH_SB_LAYOUT_SECTOR    7
1118
1119 /*
1120  * @offset      - sector where this sb was written
1121  * @version     - on disk format version
1122  * @magic       - identifies as a bcachefs superblock (BCACHE_MAGIC)
1123  * @seq         - incremented each time superblock is written
1124  * @uuid        - used for generating various magic numbers and identifying
1125  *                member devices, never changes
1126  * @user_uuid   - user visible UUID, may be changed
1127  * @label       - filesystem label
1128  * @seq         - identifies most recent superblock, incremented each time
1129  *                superblock is written
1130  * @features    - enabled incompatible features
1131  */
1132 struct bch_sb {
1133         struct bch_csum         csum;
1134         __le64                  version;
1135         uuid_le                 magic;
1136         uuid_le                 uuid;
1137         uuid_le                 user_uuid;
1138         __u8                    label[BCH_SB_LABEL_SIZE];
1139         __le64                  offset;
1140         __le64                  seq;
1141
1142         __le16                  block_size;
1143         __u8                    dev_idx;
1144         __u8                    nr_devices;
1145         __le32                  u64s;
1146
1147         __le64                  time_base_lo;
1148         __le32                  time_base_hi;
1149         __le32                  time_precision;
1150
1151         __le64                  flags[8];
1152         __le64                  features[2];
1153         __le64                  compat[2];
1154
1155         struct bch_sb_layout    layout;
1156
1157         union {
1158                 struct bch_sb_field start[0];
1159                 __le64          _data[0];
1160         };
1161 } __attribute__((packed, aligned(8)));
1162
1163 /*
1164  * Flags:
1165  * BCH_SB_INITALIZED    - set on first mount
1166  * BCH_SB_CLEAN         - did we shut down cleanly? Just a hint, doesn't affect
1167  *                        behaviour of mount/recovery path:
1168  * BCH_SB_INODE_32BIT   - limit inode numbers to 32 bits
1169  * BCH_SB_128_BIT_MACS  - 128 bit macs instead of 80
1170  * BCH_SB_ENCRYPTION_TYPE - if nonzero encryption is enabled; overrides
1171  *                         DATA/META_CSUM_TYPE. Also indicates encryption
1172  *                         algorithm in use, if/when we get more than one
1173  */
1174
1175 LE16_BITMASK(BCH_SB_BLOCK_SIZE,         struct bch_sb, block_size, 0, 16);
1176
1177 LE64_BITMASK(BCH_SB_INITIALIZED,        struct bch_sb, flags[0],  0,  1);
1178 LE64_BITMASK(BCH_SB_CLEAN,              struct bch_sb, flags[0],  1,  2);
1179 LE64_BITMASK(BCH_SB_CSUM_TYPE,          struct bch_sb, flags[0],  2,  8);
1180 LE64_BITMASK(BCH_SB_ERROR_ACTION,       struct bch_sb, flags[0],  8, 12);
1181
1182 LE64_BITMASK(BCH_SB_BTREE_NODE_SIZE,    struct bch_sb, flags[0], 12, 28);
1183
1184 LE64_BITMASK(BCH_SB_GC_RESERVE,         struct bch_sb, flags[0], 28, 33);
1185 LE64_BITMASK(BCH_SB_ROOT_RESERVE,       struct bch_sb, flags[0], 33, 40);
1186
1187 LE64_BITMASK(BCH_SB_META_CSUM_TYPE,     struct bch_sb, flags[0], 40, 44);
1188 LE64_BITMASK(BCH_SB_DATA_CSUM_TYPE,     struct bch_sb, flags[0], 44, 48);
1189
1190 LE64_BITMASK(BCH_SB_META_REPLICAS_WANT, struct bch_sb, flags[0], 48, 52);
1191 LE64_BITMASK(BCH_SB_DATA_REPLICAS_WANT, struct bch_sb, flags[0], 52, 56);
1192
1193 LE64_BITMASK(BCH_SB_POSIX_ACL,          struct bch_sb, flags[0], 56, 57);
1194 LE64_BITMASK(BCH_SB_USRQUOTA,           struct bch_sb, flags[0], 57, 58);
1195 LE64_BITMASK(BCH_SB_GRPQUOTA,           struct bch_sb, flags[0], 58, 59);
1196 LE64_BITMASK(BCH_SB_PRJQUOTA,           struct bch_sb, flags[0], 59, 60);
1197
1198 /* 60-64 unused */
1199
1200 LE64_BITMASK(BCH_SB_STR_HASH_TYPE,      struct bch_sb, flags[1],  0,  4);
1201 LE64_BITMASK(BCH_SB_COMPRESSION_TYPE,   struct bch_sb, flags[1],  4,  8);
1202 LE64_BITMASK(BCH_SB_INODE_32BIT,        struct bch_sb, flags[1],  8,  9);
1203
1204 LE64_BITMASK(BCH_SB_128_BIT_MACS,       struct bch_sb, flags[1],  9, 10);
1205 LE64_BITMASK(BCH_SB_ENCRYPTION_TYPE,    struct bch_sb, flags[1], 10, 14);
1206
1207 /*
1208  * Max size of an extent that may require bouncing to read or write
1209  * (checksummed, compressed): 64k
1210  */
1211 LE64_BITMASK(BCH_SB_ENCODED_EXTENT_MAX_BITS,
1212                                         struct bch_sb, flags[1], 14, 20);
1213
1214 LE64_BITMASK(BCH_SB_META_REPLICAS_REQ,  struct bch_sb, flags[1], 20, 24);
1215 LE64_BITMASK(BCH_SB_DATA_REPLICAS_REQ,  struct bch_sb, flags[1], 24, 28);
1216
1217 LE64_BITMASK(BCH_SB_PROMOTE_TARGET,     struct bch_sb, flags[1], 28, 40);
1218 LE64_BITMASK(BCH_SB_FOREGROUND_TARGET,  struct bch_sb, flags[1], 40, 52);
1219 LE64_BITMASK(BCH_SB_BACKGROUND_TARGET,  struct bch_sb, flags[1], 52, 64);
1220
1221 LE64_BITMASK(BCH_SB_BACKGROUND_COMPRESSION_TYPE,
1222                                         struct bch_sb, flags[2],  0,  4);
1223 LE64_BITMASK(BCH_SB_GC_RESERVE_BYTES,   struct bch_sb, flags[2],  4, 64);
1224
1225 /* Features: */
1226 enum bch_sb_features {
1227         BCH_FEATURE_LZ4                 = 0,
1228         BCH_FEATURE_GZIP                = 1,
1229         BCH_FEATURE_ZSTD                = 2,
1230         BCH_FEATURE_ATOMIC_NLINK        = 3,
1231 };
1232
1233 /* options: */
1234
1235 #define BCH_REPLICAS_MAX                4U
1236
1237 enum bch_error_actions {
1238         BCH_ON_ERROR_CONTINUE           = 0,
1239         BCH_ON_ERROR_RO                 = 1,
1240         BCH_ON_ERROR_PANIC              = 2,
1241         BCH_NR_ERROR_ACTIONS            = 3,
1242 };
1243
1244 enum bch_csum_opts {
1245         BCH_CSUM_OPT_NONE               = 0,
1246         BCH_CSUM_OPT_CRC32C             = 1,
1247         BCH_CSUM_OPT_CRC64              = 2,
1248         BCH_CSUM_OPT_NR                 = 3,
1249 };
1250
1251 enum bch_str_hash_opts {
1252         BCH_STR_HASH_CRC32C             = 0,
1253         BCH_STR_HASH_CRC64              = 1,
1254         BCH_STR_HASH_SIPHASH            = 2,
1255         BCH_STR_HASH_NR                 = 3,
1256 };
1257
1258 #define BCH_COMPRESSION_TYPES()         \
1259         x(NONE)                         \
1260         x(LZ4)                          \
1261         x(GZIP)                         \
1262         x(ZSTD)
1263
1264 enum bch_compression_opts {
1265 #define x(t) BCH_COMPRESSION_OPT_##t,
1266         BCH_COMPRESSION_TYPES()
1267 #undef x
1268         BCH_COMPRESSION_OPT_NR
1269 };
1270
1271 /*
1272  * Magic numbers
1273  *
1274  * The various other data structures have their own magic numbers, which are
1275  * xored with the first part of the cache set's UUID
1276  */
1277
1278 #define BCACHE_MAGIC                                                    \
1279         UUID_LE(0xf67385c6, 0x1a4e, 0xca45,                             \
1280                 0x82, 0x65, 0xf5, 0x7f, 0x48, 0xba, 0x6d, 0x81)
1281
1282 #define BCACHEFS_STATFS_MAGIC           0xca451a4e
1283
1284 #define JSET_MAGIC              __cpu_to_le64(0x245235c1a3625032ULL)
1285 #define BSET_MAGIC              __cpu_to_le64(0x90135c78b99e07f5ULL)
1286
1287 static inline __le64 __bch2_sb_magic(struct bch_sb *sb)
1288 {
1289         __le64 ret;
1290         memcpy(&ret, &sb->uuid, sizeof(ret));
1291         return ret;
1292 }
1293
1294 static inline __u64 __jset_magic(struct bch_sb *sb)
1295 {
1296         return __le64_to_cpu(__bch2_sb_magic(sb) ^ JSET_MAGIC);
1297 }
1298
1299 static inline __u64 __bset_magic(struct bch_sb *sb)
1300 {
1301         return __le64_to_cpu(__bch2_sb_magic(sb) ^ BSET_MAGIC);
1302 }
1303
1304 /* Journal */
1305
1306 #define BCACHE_JSET_VERSION_UUIDv1      1
1307 #define BCACHE_JSET_VERSION_UUID        1       /* Always latest UUID format */
1308 #define BCACHE_JSET_VERSION_JKEYS       2
1309 #define BCACHE_JSET_VERSION             2
1310
1311 #define JSET_KEYS_U64s  (sizeof(struct jset_entry) / sizeof(__u64))
1312
1313 #define BCH_JSET_ENTRY_TYPES()                  \
1314         x(btree_keys,           0)              \
1315         x(btree_root,           1)              \
1316         x(prio_ptrs,            2)              \
1317         x(blacklist,            3)              \
1318         x(blacklist_v2,         4)
1319
1320 enum {
1321 #define x(f, nr)        BCH_JSET_ENTRY_##f      = nr,
1322         BCH_JSET_ENTRY_TYPES()
1323 #undef x
1324         BCH_JSET_ENTRY_NR
1325 };
1326
1327 /*
1328  * Journal sequence numbers can be blacklisted: bsets record the max sequence
1329  * number of all the journal entries they contain updates for, so that on
1330  * recovery we can ignore those bsets that contain index updates newer that what
1331  * made it into the journal.
1332  *
1333  * This means that we can't reuse that journal_seq - we have to skip it, and
1334  * then record that we skipped it so that the next time we crash and recover we
1335  * don't think there was a missing journal entry.
1336  */
1337 struct jset_entry_blacklist {
1338         struct jset_entry       entry;
1339         __le64                  seq;
1340 };
1341
1342 struct jset_entry_blacklist_v2 {
1343         struct jset_entry       entry;
1344         __le64                  start;
1345         __le64                  end;
1346 };
1347
1348 /*
1349  * On disk format for a journal entry:
1350  * seq is monotonically increasing; every journal entry has its own unique
1351  * sequence number.
1352  *
1353  * last_seq is the oldest journal entry that still has keys the btree hasn't
1354  * flushed to disk yet.
1355  *
1356  * version is for on disk format changes.
1357  */
1358 struct jset {
1359         struct bch_csum         csum;
1360
1361         __le64                  magic;
1362         __le64                  seq;
1363         __le32                  version;
1364         __le32                  flags;
1365
1366         __le32                  u64s; /* size of d[] in u64s */
1367
1368         __u8                    encrypted_start[0];
1369
1370         __le16                  read_clock;
1371         __le16                  write_clock;
1372
1373         /* Sequence number of oldest dirty journal entry */
1374         __le64                  last_seq;
1375
1376
1377         union {
1378                 struct jset_entry start[0];
1379                 __u64           _data[0];
1380         };
1381 } __attribute__((packed, aligned(8)));
1382
1383 LE32_BITMASK(JSET_CSUM_TYPE,    struct jset, flags, 0, 4);
1384 LE32_BITMASK(JSET_BIG_ENDIAN,   struct jset, flags, 4, 5);
1385
1386 #define BCH_JOURNAL_BUCKETS_MIN         8
1387
1388 /* Btree: */
1389
1390 #define DEFINE_BCH_BTREE_IDS()                                  \
1391         DEF_BTREE_ID(EXTENTS,   0, "extents")                   \
1392         DEF_BTREE_ID(INODES,    1, "inodes")                    \
1393         DEF_BTREE_ID(DIRENTS,   2, "dirents")                   \
1394         DEF_BTREE_ID(XATTRS,    3, "xattrs")                    \
1395         DEF_BTREE_ID(ALLOC,     4, "alloc")                     \
1396         DEF_BTREE_ID(QUOTAS,    5, "quotas")
1397
1398 #define DEF_BTREE_ID(kwd, val, name) BTREE_ID_##kwd = val,
1399
1400 enum btree_id {
1401         DEFINE_BCH_BTREE_IDS()
1402         BTREE_ID_NR
1403 };
1404
1405 #undef DEF_BTREE_ID
1406
1407 #define BTREE_MAX_DEPTH         4U
1408
1409 /* Btree nodes */
1410
1411 /* Version 1: Seed pointer into btree node checksum
1412  */
1413 #define BCACHE_BSET_CSUM                1
1414 #define BCACHE_BSET_KEY_v1              2
1415 #define BCACHE_BSET_JOURNAL_SEQ         3
1416 #define BCACHE_BSET_VERSION             3
1417
1418 /*
1419  * Btree nodes
1420  *
1421  * On disk a btree node is a list/log of these; within each set the keys are
1422  * sorted
1423  */
1424 struct bset {
1425         __le64                  seq;
1426
1427         /*
1428          * Highest journal entry this bset contains keys for.
1429          * If on recovery we don't see that journal entry, this bset is ignored:
1430          * this allows us to preserve the order of all index updates after a
1431          * crash, since the journal records a total order of all index updates
1432          * and anything that didn't make it to the journal doesn't get used.
1433          */
1434         __le64                  journal_seq;
1435
1436         __le32                  flags;
1437         __le16                  version;
1438         __le16                  u64s; /* count of d[] in u64s */
1439
1440         union {
1441                 struct bkey_packed start[0];
1442                 __u64           _data[0];
1443         };
1444 } __attribute__((packed, aligned(8)));
1445
1446 LE32_BITMASK(BSET_CSUM_TYPE,    struct bset, flags, 0, 4);
1447
1448 LE32_BITMASK(BSET_BIG_ENDIAN,   struct bset, flags, 4, 5);
1449 LE32_BITMASK(BSET_SEPARATE_WHITEOUTS,
1450                                 struct bset, flags, 5, 6);
1451
1452 struct btree_node {
1453         struct bch_csum         csum;
1454         __le64                  magic;
1455
1456         /* this flags field is encrypted, unlike bset->flags: */
1457         __le64                  flags;
1458
1459         /* Closed interval: */
1460         struct bpos             min_key;
1461         struct bpos             max_key;
1462         struct bch_extent_ptr   ptr;
1463         struct bkey_format      format;
1464
1465         union {
1466         struct bset             keys;
1467         struct {
1468                 __u8            pad[22];
1469                 __le16          u64s;
1470                 __u64           _data[0];
1471
1472         };
1473         };
1474 } __attribute__((packed, aligned(8)));
1475
1476 LE64_BITMASK(BTREE_NODE_ID,     struct btree_node, flags,  0,  4);
1477 LE64_BITMASK(BTREE_NODE_LEVEL,  struct btree_node, flags,  4,  8);
1478 /* 8-32 unused */
1479 LE64_BITMASK(BTREE_NODE_SEQ,    struct btree_node, flags, 32, 64);
1480
1481 struct btree_node_entry {
1482         struct bch_csum         csum;
1483
1484         union {
1485         struct bset             keys;
1486         struct {
1487                 __u8            pad[22];
1488                 __le16          u64s;
1489                 __u64           _data[0];
1490
1491         };
1492         };
1493 } __attribute__((packed, aligned(8)));
1494
1495 #endif /* _BCACHEFS_FORMAT_H */