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