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