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