]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/bcachefs_format.h
854e1c3db5aff235b05b1606f91858b953a6255e
[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 <linux/compiler.h>
10 #include <asm/byteorder.h>
11 #include <linux/uuid.h>
12
13 #define LE_BITMASK(_bits, name, type, field, offset, end)               \
14 static const unsigned   name##_OFFSET = offset;                         \
15 static const unsigned   name##_BITS = (end - offset);                   \
16 static const __u##_bits name##_MAX = (1ULL << (end - offset)) - 1;      \
17                                                                         \
18 static inline __u64 name(const type *k)                                 \
19 {                                                                       \
20         return (__le##_bits##_to_cpu(k->field) >> offset) &             \
21                 ~(~0ULL << (end - offset));                             \
22 }                                                                       \
23                                                                         \
24 static inline void SET_##name(type *k, __u64 v)                         \
25 {                                                                       \
26         __u##_bits new = __le##_bits##_to_cpu(k->field);                \
27                                                                         \
28         new &= ~(~(~0ULL << (end - offset)) << offset);                 \
29         new |= (v & ~(~0ULL << (end - offset))) << offset;              \
30         k->field = __cpu_to_le##_bits(new);                             \
31 }
32
33 #define LE16_BITMASK(n, t, f, o, e)     LE_BITMASK(16, n, t, f, o, e)
34 #define LE32_BITMASK(n, t, f, o, e)     LE_BITMASK(32, n, t, f, o, e)
35 #define LE64_BITMASK(n, t, f, o, e)     LE_BITMASK(64, n, t, f, o, e)
36
37 struct bkey_format {
38         __u8            key_u64s;
39         __u8            nr_fields;
40         /* One unused slot for now: */
41         __u8            bits_per_field[6];
42         __le64          field_offset[6];
43 };
44
45 /* Btree keys - all units are in sectors */
46
47 struct bpos {
48         /* Word order matches machine byte order */
49 #if defined(__LITTLE_ENDIAN)
50         __u32           snapshot;
51         __u64           offset;
52         __u64           inode;
53 #elif defined(__BIG_ENDIAN)
54         __u64           inode;
55         __u64           offset;         /* Points to end of extent - sectors */
56         __u32           snapshot;
57 #else
58 #error edit for your odd byteorder.
59 #endif
60 } __attribute__((packed, aligned(4)));
61
62 #define KEY_INODE_MAX                   ((__u64)~0ULL)
63 #define KEY_OFFSET_MAX                  ((__u64)~0ULL)
64 #define KEY_SNAPSHOT_MAX                ((__u32)~0U)
65 #define KEY_SIZE_MAX                    ((__u32)~0U)
66
67 static inline struct bpos POS(__u64 inode, __u64 offset)
68 {
69         struct bpos ret;
70
71         ret.inode       = inode;
72         ret.offset      = offset;
73         ret.snapshot    = 0;
74
75         return ret;
76 }
77
78 #define POS_MIN                         POS(0, 0)
79 #define POS_MAX                         POS(KEY_INODE_MAX, KEY_OFFSET_MAX)
80
81 /* Empty placeholder struct, for container_of() */
82 struct bch_val {
83         __u64           __nothing[0];
84 };
85
86 struct bversion {
87 #if defined(__LITTLE_ENDIAN)
88         __u64           lo;
89         __u32           hi;
90 #elif defined(__BIG_ENDIAN)
91         __u32           hi;
92         __u64           lo;
93 #endif
94 } __attribute__((packed, aligned(4)));
95
96 struct bkey {
97         /* Size of combined key and value, in u64s */
98         __u8            u64s;
99
100         /* Format of key (0 for format local to btree node) */
101 #if defined(__LITTLE_ENDIAN_BITFIELD)
102         __u8            format:7,
103                         needs_whiteout:1;
104 #elif defined (__BIG_ENDIAN_BITFIELD)
105         __u8            needs_whiteout:1,
106                         format:7;
107 #else
108 #error edit for your odd byteorder.
109 #endif
110
111         /* Type of the value */
112         __u8            type;
113
114 #if defined(__LITTLE_ENDIAN)
115         __u8            pad[1];
116
117         struct bversion version;
118         __u32           size;           /* extent size, in sectors */
119         struct bpos     p;
120 #elif defined(__BIG_ENDIAN)
121         struct bpos     p;
122         __u32           size;           /* extent size, in sectors */
123         struct bversion version;
124
125         __u8            pad[1];
126 #endif
127 } __attribute__((packed, aligned(8)));
128
129 struct bkey_packed {
130         __u64           _data[0];
131
132         /* Size of combined key and value, in u64s */
133         __u8            u64s;
134
135         /* Format of key (0 for format local to btree node) */
136
137         /*
138          * XXX: next incompat on disk format change, switch format and
139          * needs_whiteout - bkey_packed() will be cheaper if format is the high
140          * bits of the bitfield
141          */
142 #if defined(__LITTLE_ENDIAN_BITFIELD)
143         __u8            format:7,
144                         needs_whiteout:1;
145 #elif defined (__BIG_ENDIAN_BITFIELD)
146         __u8            needs_whiteout:1,
147                         format:7;
148 #endif
149
150         /* Type of the value */
151         __u8            type;
152         __u8            key_start[0];
153
154         /*
155          * We copy bkeys with struct assignment in various places, and while
156          * that shouldn't be done with packed bkeys we can't disallow it in C,
157          * and it's legal to cast a bkey to a bkey_packed  - so padding it out
158          * to the same size as struct bkey should hopefully be safest.
159          */
160         __u8            pad[sizeof(struct bkey) - 3];
161 } __attribute__((packed, aligned(8)));
162
163 #define BKEY_U64s                       (sizeof(struct bkey) / sizeof(__u64))
164 #define KEY_PACKED_BITS_START           24
165
166 #define KEY_FORMAT_LOCAL_BTREE          0
167 #define KEY_FORMAT_CURRENT              1
168
169 enum bch_bkey_fields {
170         BKEY_FIELD_INODE,
171         BKEY_FIELD_OFFSET,
172         BKEY_FIELD_SNAPSHOT,
173         BKEY_FIELD_SIZE,
174         BKEY_FIELD_VERSION_HI,
175         BKEY_FIELD_VERSION_LO,
176         BKEY_NR_FIELDS,
177 };
178
179 #define bkey_format_field(name, field)                                  \
180         [BKEY_FIELD_##name] = (sizeof(((struct bkey *) NULL)->field) * 8)
181
182 #define BKEY_FORMAT_CURRENT                                             \
183 ((struct bkey_format) {                                                 \
184         .key_u64s       = BKEY_U64s,                                    \
185         .nr_fields      = BKEY_NR_FIELDS,                               \
186         .bits_per_field = {                                             \
187                 bkey_format_field(INODE,        p.inode),               \
188                 bkey_format_field(OFFSET,       p.offset),              \
189                 bkey_format_field(SNAPSHOT,     p.snapshot),            \
190                 bkey_format_field(SIZE,         size),                  \
191                 bkey_format_field(VERSION_HI,   version.hi),            \
192                 bkey_format_field(VERSION_LO,   version.lo),            \
193         },                                                              \
194 })
195
196 /* bkey with inline value */
197 struct bkey_i {
198         __u64                   _data[0];
199
200         union {
201         struct {
202                 /* Size of combined key and value, in u64s */
203                 __u8            u64s;
204         };
205         struct {
206                 struct bkey     k;
207                 struct bch_val  v;
208         };
209         };
210 };
211
212 #define KEY(_inode, _offset, _size)                                     \
213 ((struct bkey) {                                                        \
214         .u64s           = BKEY_U64s,                                    \
215         .format         = KEY_FORMAT_CURRENT,                           \
216         .p              = POS(_inode, _offset),                         \
217         .size           = _size,                                        \
218 })
219
220 static inline void bkey_init(struct bkey *k)
221 {
222         *k = KEY(0, 0, 0);
223 }
224
225 #define bkey_bytes(_k)          ((_k)->u64s * sizeof(__u64))
226
227 #define __BKEY_PADDED(key, pad)                                 \
228         struct { struct bkey_i key; __u64 key ## _pad[pad]; }
229
230 #define BKEY_VAL_TYPE(name, nr)                                         \
231 struct bkey_i_##name {                                                  \
232         union {                                                         \
233                 struct bkey             k;                              \
234                 struct bkey_i           k_i;                            \
235         };                                                              \
236         struct bch_##name               v;                              \
237 }
238
239 /*
240  * - DELETED keys are used internally to mark keys that should be ignored but
241  *   override keys in composition order.  Their version number is ignored.
242  *
243  * - DISCARDED keys indicate that the data is all 0s because it has been
244  *   discarded. DISCARDs may have a version; if the version is nonzero the key
245  *   will be persistent, otherwise the key will be dropped whenever the btree
246  *   node is rewritten (like DELETED keys).
247  *
248  * - ERROR: any read of the data returns a read error, as the data was lost due
249  *   to a failing device. Like DISCARDED keys, they can be removed (overridden)
250  *   by new writes or cluster-wide GC. Node repair can also overwrite them with
251  *   the same or a more recent version number, but not with an older version
252  *   number.
253 */
254 #define KEY_TYPE_DELETED                0
255 #define KEY_TYPE_DISCARD                1
256 #define KEY_TYPE_ERROR                  2
257 #define KEY_TYPE_COOKIE                 3
258 #define KEY_TYPE_PERSISTENT_DISCARD     4
259 #define KEY_TYPE_GENERIC_NR             128
260
261 struct bch_cookie {
262         struct bch_val          v;
263         __le64                  cookie;
264 };
265 BKEY_VAL_TYPE(cookie,           KEY_TYPE_COOKIE);
266
267 /* Extents */
268
269 /*
270  * In extent bkeys, the value is a list of pointers (bch_extent_ptr), optionally
271  * preceded by checksum/compression information (bch_extent_crc32 or
272  * bch_extent_crc64).
273  *
274  * One major determining factor in the format of extents is how we handle and
275  * represent extents that have been partially overwritten and thus trimmed:
276  *
277  * If an extent is not checksummed or compressed, when the extent is trimmed we
278  * don't have to remember the extent we originally allocated and wrote: we can
279  * merely adjust ptr->offset to point to the start of the start of the data that
280  * is currently live. The size field in struct bkey records the current (live)
281  * size of the extent, and is also used to mean "size of region on disk that we
282  * point to" in this case.
283  *
284  * Thus an extent that is not checksummed or compressed will consist only of a
285  * list of bch_extent_ptrs, with none of the fields in
286  * bch_extent_crc32/bch_extent_crc64.
287  *
288  * When an extent is checksummed or compressed, it's not possible to read only
289  * the data that is currently live: we have to read the entire extent that was
290  * originally written, and then return only the part of the extent that is
291  * currently live.
292  *
293  * Thus, in addition to the current size of the extent in struct bkey, we need
294  * to store the size of the originally allocated space - this is the
295  * compressed_size and uncompressed_size fields in bch_extent_crc32/64. Also,
296  * when the extent is trimmed, instead of modifying the offset field of the
297  * pointer, we keep a second smaller offset field - "offset into the original
298  * extent of the currently live region".
299  *
300  * The other major determining factor is replication and data migration:
301  *
302  * Each pointer may have its own bch_extent_crc32/64. When doing a replicated
303  * write, we will initially write all the replicas in the same format, with the
304  * same checksum type and compression format - however, when copygc runs later (or
305  * tiering/cache promotion, anything that moves data), it is not in general
306  * going to rewrite all the pointers at once - one of the replicas may be in a
307  * bucket on one device that has very little fragmentation while another lives
308  * in a bucket that has become heavily fragmented, and thus is being rewritten
309  * sooner than the rest.
310  *
311  * Thus it will only move a subset of the pointers (or in the case of
312  * tiering/cache promotion perhaps add a single pointer without dropping any
313  * current pointers), and if the extent has been partially overwritten it must
314  * write only the currently live portion (or copygc would not be able to reduce
315  * fragmentation!) - which necessitates a different bch_extent_crc format for
316  * the new pointer.
317  *
318  * But in the interests of space efficiency, we don't want to store one
319  * bch_extent_crc for each pointer if we don't have to.
320  *
321  * Thus, a bch_extent consists of bch_extent_crc32s, bch_extent_crc64s, and
322  * bch_extent_ptrs appended arbitrarily one after the other. We determine the
323  * type of a given entry with a scheme similar to utf8 (except we're encoding a
324  * type, not a size), encoding the type in the position of the first set bit:
325  *
326  * bch_extent_crc32     - 0b1
327  * bch_extent_ptr       - 0b10
328  * bch_extent_crc64     - 0b100
329  *
330  * We do it this way because bch_extent_crc32 is _very_ constrained on bits (and
331  * bch_extent_crc64 is the least constrained).
332  *
333  * Then, each bch_extent_crc32/64 applies to the pointers that follow after it,
334  * until the next bch_extent_crc32/64.
335  *
336  * If there are no bch_extent_crcs preceding a bch_extent_ptr, then that pointer
337  * is neither checksummed nor compressed.
338  */
339
340 /* 128 bits, sufficient for cryptographic MACs: */
341 struct bch_csum {
342         __le64                  lo;
343         __le64                  hi;
344 } __attribute__((packed, aligned(8)));
345
346 enum bch_csum_type {
347         BCH_CSUM_NONE                   = 0,
348         BCH_CSUM_CRC32C_NONZERO         = 1,
349         BCH_CSUM_CRC64_NONZERO          = 2,
350         BCH_CSUM_CHACHA20_POLY1305_80   = 3,
351         BCH_CSUM_CHACHA20_POLY1305_128  = 4,
352         BCH_CSUM_CRC32C                 = 5,
353         BCH_CSUM_CRC64                  = 6,
354         BCH_CSUM_NR                     = 7,
355 };
356
357 static inline _Bool bch2_csum_type_is_encryption(enum bch_csum_type type)
358 {
359         switch (type) {
360         case BCH_CSUM_CHACHA20_POLY1305_80:
361         case BCH_CSUM_CHACHA20_POLY1305_128:
362                 return true;
363         default:
364                 return false;
365         }
366 }
367
368 enum bch_compression_type {
369         BCH_COMPRESSION_NONE            = 0,
370         BCH_COMPRESSION_LZ4_OLD         = 1,
371         BCH_COMPRESSION_GZIP            = 2,
372         BCH_COMPRESSION_LZ4             = 3,
373         BCH_COMPRESSION_NR              = 4,
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 };
1086
1087 /* options: */
1088
1089 #define BCH_REPLICAS_MAX                4U
1090
1091 enum bch_error_actions {
1092         BCH_ON_ERROR_CONTINUE           = 0,
1093         BCH_ON_ERROR_RO                 = 1,
1094         BCH_ON_ERROR_PANIC              = 2,
1095         BCH_NR_ERROR_ACTIONS            = 3,
1096 };
1097
1098 enum bch_csum_opts {
1099         BCH_CSUM_OPT_NONE               = 0,
1100         BCH_CSUM_OPT_CRC32C             = 1,
1101         BCH_CSUM_OPT_CRC64              = 2,
1102         BCH_CSUM_OPT_NR                 = 3,
1103 };
1104
1105 enum bch_str_hash_opts {
1106         BCH_STR_HASH_CRC32C             = 0,
1107         BCH_STR_HASH_CRC64              = 1,
1108         BCH_STR_HASH_SIPHASH            = 2,
1109         BCH_STR_HASH_NR                 = 3,
1110 };
1111
1112 enum bch_compression_opts {
1113         BCH_COMPRESSION_OPT_NONE        = 0,
1114         BCH_COMPRESSION_OPT_LZ4         = 1,
1115         BCH_COMPRESSION_OPT_GZIP        = 2,
1116         BCH_COMPRESSION_OPT_NR          = 3,
1117 };
1118
1119 /*
1120  * Magic numbers
1121  *
1122  * The various other data structures have their own magic numbers, which are
1123  * xored with the first part of the cache set's UUID
1124  */
1125
1126 #define BCACHE_MAGIC                                                    \
1127         UUID_LE(0xf67385c6, 0x1a4e, 0xca45,                             \
1128                 0x82, 0x65, 0xf5, 0x7f, 0x48, 0xba, 0x6d, 0x81)
1129
1130 #define BCACHEFS_STATFS_MAGIC           0xca451a4e
1131
1132 #define JSET_MAGIC              __cpu_to_le64(0x245235c1a3625032ULL)
1133 #define BSET_MAGIC              __cpu_to_le64(0x90135c78b99e07f5ULL)
1134
1135 static inline __le64 __bch2_sb_magic(struct bch_sb *sb)
1136 {
1137         __le64 ret;
1138         memcpy(&ret, &sb->uuid, sizeof(ret));
1139         return ret;
1140 }
1141
1142 static inline __u64 __jset_magic(struct bch_sb *sb)
1143 {
1144         return __le64_to_cpu(__bch2_sb_magic(sb) ^ JSET_MAGIC);
1145 }
1146
1147 static inline __u64 __bset_magic(struct bch_sb *sb)
1148 {
1149         return __le64_to_cpu(__bch2_sb_magic(sb) ^ BSET_MAGIC);
1150 }
1151
1152 /* Journal */
1153
1154 #define BCACHE_JSET_VERSION_UUIDv1      1
1155 #define BCACHE_JSET_VERSION_UUID        1       /* Always latest UUID format */
1156 #define BCACHE_JSET_VERSION_JKEYS       2
1157 #define BCACHE_JSET_VERSION             2
1158
1159 struct jset_entry {
1160         __le16                  u64s;
1161         __u8                    btree_id;
1162         __u8                    level;
1163         __u8                    type; /* designates what this jset holds */
1164         __u8                    pad[3];
1165
1166         union {
1167                 struct bkey_i   start[0];
1168                 __u64           _data[0];
1169         };
1170 };
1171
1172 struct jset_entry_blacklist {
1173         struct jset_entry       entry;
1174         __le64                  seq;
1175 };
1176
1177 #define JSET_KEYS_U64s  (sizeof(struct jset_entry) / sizeof(__u64))
1178
1179 enum {
1180         JOURNAL_ENTRY_BTREE_KEYS                = 0,
1181         JOURNAL_ENTRY_BTREE_ROOT                = 1,
1182         JOURNAL_ENTRY_PRIO_PTRS                 = 2, /* Obsolete */
1183
1184         /*
1185          * Journal sequence numbers can be blacklisted: bsets record the max
1186          * sequence number of all the journal entries they contain updates for,
1187          * so that on recovery we can ignore those bsets that contain index
1188          * updates newer that what made it into the journal.
1189          *
1190          * This means that we can't reuse that journal_seq - we have to skip it,
1191          * and then record that we skipped it so that the next time we crash and
1192          * recover we don't think there was a missing journal entry.
1193          */
1194         JOURNAL_ENTRY_JOURNAL_SEQ_BLACKLISTED   = 3,
1195 };
1196
1197 /*
1198  * On disk format for a journal entry:
1199  * seq is monotonically increasing; every journal entry has its own unique
1200  * sequence number.
1201  *
1202  * last_seq is the oldest journal entry that still has keys the btree hasn't
1203  * flushed to disk yet.
1204  *
1205  * version is for on disk format changes.
1206  */
1207 struct jset {
1208         struct bch_csum         csum;
1209
1210         __le64                  magic;
1211         __le64                  seq;
1212         __le32                  version;
1213         __le32                  flags;
1214
1215         __le32                  u64s; /* size of d[] in u64s */
1216
1217         __u8                    encrypted_start[0];
1218
1219         __le16                  read_clock;
1220         __le16                  write_clock;
1221
1222         /* Sequence number of oldest dirty journal entry */
1223         __le64                  last_seq;
1224
1225
1226         union {
1227                 struct jset_entry start[0];
1228                 __u64           _data[0];
1229         };
1230 } __attribute__((packed, aligned(8)));
1231
1232 LE32_BITMASK(JSET_CSUM_TYPE,    struct jset, flags, 0, 4);
1233 LE32_BITMASK(JSET_BIG_ENDIAN,   struct jset, flags, 4, 5);
1234
1235 #define BCH_JOURNAL_BUCKETS_MIN         20
1236
1237 /* Btree: */
1238
1239 #define DEFINE_BCH_BTREE_IDS()                                  \
1240         DEF_BTREE_ID(EXTENTS,   0, "extents")                   \
1241         DEF_BTREE_ID(INODES,    1, "inodes")                    \
1242         DEF_BTREE_ID(DIRENTS,   2, "dirents")                   \
1243         DEF_BTREE_ID(XATTRS,    3, "xattrs")                    \
1244         DEF_BTREE_ID(ALLOC,     4, "alloc")                     \
1245         DEF_BTREE_ID(QUOTAS,    5, "quotas")
1246
1247 #define DEF_BTREE_ID(kwd, val, name) BTREE_ID_##kwd = val,
1248
1249 enum btree_id {
1250         DEFINE_BCH_BTREE_IDS()
1251         BTREE_ID_NR
1252 };
1253
1254 #undef DEF_BTREE_ID
1255
1256 #define BTREE_MAX_DEPTH         4U
1257
1258 /* Btree nodes */
1259
1260 /* Version 1: Seed pointer into btree node checksum
1261  */
1262 #define BCACHE_BSET_CSUM                1
1263 #define BCACHE_BSET_KEY_v1              2
1264 #define BCACHE_BSET_JOURNAL_SEQ         3
1265 #define BCACHE_BSET_VERSION             3
1266
1267 /*
1268  * Btree nodes
1269  *
1270  * On disk a btree node is a list/log of these; within each set the keys are
1271  * sorted
1272  */
1273 struct bset {
1274         __le64                  seq;
1275
1276         /*
1277          * Highest journal entry this bset contains keys for.
1278          * If on recovery we don't see that journal entry, this bset is ignored:
1279          * this allows us to preserve the order of all index updates after a
1280          * crash, since the journal records a total order of all index updates
1281          * and anything that didn't make it to the journal doesn't get used.
1282          */
1283         __le64                  journal_seq;
1284
1285         __le32                  flags;
1286         __le16                  version;
1287         __le16                  u64s; /* count of d[] in u64s */
1288
1289         union {
1290                 struct bkey_packed start[0];
1291                 __u64           _data[0];
1292         };
1293 } __attribute__((packed, aligned(8)));
1294
1295 LE32_BITMASK(BSET_CSUM_TYPE,    struct bset, flags, 0, 4);
1296
1297 LE32_BITMASK(BSET_BIG_ENDIAN,   struct bset, flags, 4, 5);
1298 LE32_BITMASK(BSET_SEPARATE_WHITEOUTS,
1299                                 struct bset, flags, 5, 6);
1300
1301 struct btree_node {
1302         struct bch_csum         csum;
1303         __le64                  magic;
1304
1305         /* this flags field is encrypted, unlike bset->flags: */
1306         __le64                  flags;
1307
1308         /* Closed interval: */
1309         struct bpos             min_key;
1310         struct bpos             max_key;
1311         struct bch_extent_ptr   ptr;
1312         struct bkey_format      format;
1313
1314         union {
1315         struct bset             keys;
1316         struct {
1317                 __u8            pad[22];
1318                 __le16          u64s;
1319                 __u64           _data[0];
1320
1321         };
1322         };
1323 } __attribute__((packed, aligned(8)));
1324
1325 LE64_BITMASK(BTREE_NODE_ID,     struct btree_node, flags, 0, 4);
1326 LE64_BITMASK(BTREE_NODE_LEVEL,  struct btree_node, flags, 4, 8);
1327
1328 struct btree_node_entry {
1329         struct bch_csum         csum;
1330
1331         union {
1332         struct bset             keys;
1333         struct {
1334                 __u8            pad[22];
1335                 __le16          u64s;
1336                 __u64           _data[0];
1337
1338         };
1339         };
1340 } __attribute__((packed, aligned(8)));
1341
1342 /* Obsolete: */
1343
1344 struct prio_set {
1345         struct bch_csum         csum;
1346
1347         __le64                  magic;
1348         __le32                  nonce[3];
1349         __le16                  version;
1350         __le16                  flags;
1351
1352         __u8                    encrypted_start[0];
1353
1354         __le64                  next_bucket;
1355
1356         struct bucket_disk {
1357                 __le16          prio[2];
1358                 __u8            gen;
1359         } __attribute__((packed)) data[];
1360 } __attribute__((packed, aligned(8)));
1361
1362 LE32_BITMASK(PSET_CSUM_TYPE,    struct prio_set, flags, 0, 4);
1363
1364 #define PSET_MAGIC              __cpu_to_le64(0x6750e15f87337f91ULL)
1365
1366 static inline __u64 __pset_magic(struct bch_sb *sb)
1367 {
1368         return __le64_to_cpu(__bch2_sb_magic(sb) ^ PSET_MAGIC);
1369 }
1370
1371 #endif /* _BCACHEFS_FORMAT_H */