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