]> git.sesse.net Git - bcachefs-tools-debian/blob - include/linux/bcache.h
4179f8ddd85057841e25935f0dd845b8e98f48b7
[bcachefs-tools-debian] / include / linux / bcache.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 bch_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_ACTIVE         = 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;  /* bcache 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_types {
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 /*
890  * @offset      - sector where this sb was written
891  * @version     - on disk format version
892  * @magic       - identifies as a bcache superblock (BCACHE_MAGIC)
893  * @seq         - incremented each time superblock is written
894  * @uuid        - used for generating various magic numbers and identifying
895  *                member devices, never changes
896  * @user_uuid   - user visible UUID, may be changed
897  * @label       - filesystem label
898  * @seq         - identifies most recent superblock, incremented each time
899  *                superblock is written
900  * @features    - enabled incompatible features
901  */
902 struct bch_sb {
903         struct bch_csum         csum;
904         __le64                  version;
905         uuid_le                 magic;
906         uuid_le                 uuid;
907         uuid_le                 user_uuid;
908         __u8                    label[BCH_SB_LABEL_SIZE];
909         __le64                  offset;
910         __le64                  seq;
911
912         __le16                  block_size;
913         __u8                    dev_idx;
914         __u8                    nr_devices;
915         __le32                  u64s;
916
917         __le64                  time_base_lo;
918         __le32                  time_base_hi;
919         __le32                  time_precision;
920
921         __le64                  flags[8];
922         __le64                  features[2];
923         __le64                  compat[2];
924
925         struct bch_sb_layout    layout;
926
927         union {
928                 struct bch_sb_field start[0];
929                 __le64          _data[0];
930         };
931 } __attribute__((packed, aligned(8)));
932
933 /*
934  * Flags:
935  * BCH_SB_INITALIZED    - set on first mount
936  * BCH_SB_CLEAN         - did we shut down cleanly? Just a hint, doesn't affect
937  *                        behaviour of mount/recovery path:
938  * BCH_SB_INODE_32BIT   - limit inode numbers to 32 bits
939  * BCH_SB_128_BIT_MACS  - 128 bit macs instead of 80
940  * BCH_SB_ENCRYPTION_TYPE - if nonzero encryption is enabled; overrides
941  *                         DATA/META_CSUM_TYPE. Also indicates encryption
942  *                         algorithm in use, if/when we get more than one
943  */
944
945 LE64_BITMASK(BCH_SB_INITIALIZED,        struct bch_sb, flags[0],  0,  1);
946 LE64_BITMASK(BCH_SB_CLEAN,              struct bch_sb, flags[0],  1,  2);
947 LE64_BITMASK(BCH_SB_CSUM_TYPE,          struct bch_sb, flags[0],  2,  8);
948 LE64_BITMASK(BCH_SB_ERROR_ACTION,       struct bch_sb, flags[0],  8, 12);
949
950 LE64_BITMASK(BCH_SB_BTREE_NODE_SIZE,    struct bch_sb, flags[0], 12, 28);
951
952 LE64_BITMASK(BCH_SB_GC_RESERVE,         struct bch_sb, flags[0], 28, 33);
953 LE64_BITMASK(BCH_SB_ROOT_RESERVE,       struct bch_sb, flags[0], 33, 40);
954
955 LE64_BITMASK(BCH_SB_META_CSUM_TYPE,     struct bch_sb, flags[0], 40, 44);
956 LE64_BITMASK(BCH_SB_DATA_CSUM_TYPE,     struct bch_sb, flags[0], 44, 48);
957
958 LE64_BITMASK(BCH_SB_META_REPLICAS_WANT, struct bch_sb, flags[0], 48, 52);
959 LE64_BITMASK(BCH_SB_DATA_REPLICAS_WANT, struct bch_sb, flags[0], 52, 56);
960
961 LE64_BITMASK(BCH_SB_META_REPLICAS_HAVE, struct bch_sb, flags[0], 56, 60);
962 LE64_BITMASK(BCH_SB_DATA_REPLICAS_HAVE, struct bch_sb, flags[0], 60, 64);
963
964 LE64_BITMASK(BCH_SB_STR_HASH_TYPE,      struct bch_sb, flags[1],  0,  4);
965 LE64_BITMASK(BCH_SB_COMPRESSION_TYPE,   struct bch_sb, flags[1],  4,  8);
966 LE64_BITMASK(BCH_SB_INODE_32BIT,        struct bch_sb, flags[1],  8,  9);
967
968 LE64_BITMASK(BCH_SB_128_BIT_MACS,       struct bch_sb, flags[1],  9, 10);
969 LE64_BITMASK(BCH_SB_ENCRYPTION_TYPE,    struct bch_sb, flags[1], 10, 14);
970 LE64_BITMASK(BCH_SB_JOURNAL_ENTRY_SIZE, struct bch_sb, flags[1], 14, 20);
971
972 /* Features: */
973 enum bch_sb_features {
974         BCH_FEATURE_LZ4                 = 0,
975         BCH_FEATURE_GZIP                = 1,
976 };
977
978 /* options: */
979
980 #define BCH_REPLICAS_MAX                4U
981
982 #if 0
983 #define BCH_ERROR_ACTIONS()                                     \
984         x(BCH_ON_ERROR_CONTINUE,        0, "continue")          \
985         x(BCH_ON_ERROR_RO,              1, "remount-ro")        \
986         x(BCH_ON_ERROR_PANIC,           2, "panic")             \
987         x(BCH_NR_ERROR_ACTIONS,         3, NULL)
988
989 enum bch_error_actions {
990 #define x(_opt, _nr, _str)      _opt = _nr,
991         BCH_ERROR_ACTIONS()
992 #undef x
993 };
994 #endif
995
996 enum bch_error_actions {
997         BCH_ON_ERROR_CONTINUE           = 0,
998         BCH_ON_ERROR_RO                 = 1,
999         BCH_ON_ERROR_PANIC              = 2,
1000         BCH_NR_ERROR_ACTIONS            = 3,
1001 };
1002
1003 enum bch_csum_opts {
1004         BCH_CSUM_OPT_NONE               = 0,
1005         BCH_CSUM_OPT_CRC32C             = 1,
1006         BCH_CSUM_OPT_CRC64              = 2,
1007         BCH_CSUM_OPT_NR                 = 3,
1008 };
1009
1010 enum bch_str_hash_opts {
1011         BCH_STR_HASH_CRC32C             = 0,
1012         BCH_STR_HASH_CRC64              = 1,
1013         BCH_STR_HASH_SIPHASH            = 2,
1014         BCH_STR_HASH_NR                 = 3,
1015 };
1016
1017 enum bch_compression_opts {
1018         BCH_COMPRESSION_NONE            = 0,
1019         BCH_COMPRESSION_LZ4             = 1,
1020         BCH_COMPRESSION_GZIP            = 2,
1021         BCH_COMPRESSION_NR              = 3,
1022 };
1023
1024 /**
1025  * BCH_OPT(name, choices, min, max, sb_option, sysfs_writeable)
1026  *
1027  * @name - name of mount option, sysfs attribute, and struct cache_set_opts
1028  *      member
1029  *
1030  * @choices - array of strings that the user can select from - option is by
1031  *      array index
1032  *
1033  *      Booleans are special cased; if @choices is bch_bool_opt the mount
1034  *      options name and noname will work as expected.
1035  *
1036  * @min, @max
1037  *
1038  * @sb_option - name of corresponding superblock option
1039  *
1040  * @sysfs_writeable - if true, option will be modifiable at runtime via sysfs
1041  */
1042
1043 #define BCH_SB_OPTS()                                           \
1044         BCH_OPT(errors,                                         \
1045                 bch_error_actions,                              \
1046                 0, BCH_NR_ERROR_ACTIONS,                        \
1047                 BCH_SB_ERROR_ACTION,                            \
1048                 true)                                           \
1049         BCH_OPT(metadata_replicas,                              \
1050                 bch_uint_opt,                                   \
1051                 0, BCH_REPLICAS_MAX,                            \
1052                 BCH_SB_META_REPLICAS_WANT,                      \
1053                 false)                                          \
1054         BCH_OPT(data_replicas,                                  \
1055                 bch_uint_opt,                                   \
1056                 0, BCH_REPLICAS_MAX,                            \
1057                 BCH_SB_DATA_REPLICAS_WANT,                      \
1058                 false)                                          \
1059         BCH_OPT(metadata_checksum,                              \
1060                 bch_csum_types,                                 \
1061                 0, BCH_CSUM_OPT_NR,                             \
1062                 BCH_SB_META_CSUM_TYPE,                          \
1063                 true)                                           \
1064         BCH_OPT(data_checksum,                                  \
1065                 bch_csum_types,                                 \
1066                 0, BCH_CSUM_OPT_NR,                             \
1067                 BCH_SB_DATA_CSUM_TYPE,                          \
1068                 true)                                           \
1069         BCH_OPT(compression,                                    \
1070                 bch_compression_types,                          \
1071                 0, BCH_COMPRESSION_NR,                          \
1072                 BCH_SB_COMPRESSION_TYPE,                        \
1073                 true)                                           \
1074         BCH_OPT(str_hash,                                       \
1075                 bch_str_hash_types,                             \
1076                 0, BCH_STR_HASH_NR,                             \
1077                 BCH_SB_STR_HASH_TYPE,                           \
1078                 true)                                           \
1079         BCH_OPT(inodes_32bit,                                   \
1080                 bch_bool_opt, 0, 2,                             \
1081                 BCH_SB_INODE_32BIT,                             \
1082                 true)                                           \
1083         BCH_OPT(gc_reserve_percent,                             \
1084                 bch_uint_opt,                                   \
1085                 5, 21,                                          \
1086                 BCH_SB_GC_RESERVE,                              \
1087                 false)                                          \
1088         BCH_OPT(root_reserve_percent,                           \
1089                 bch_uint_opt,                                   \
1090                 0, 100,                                         \
1091                 BCH_SB_ROOT_RESERVE,                            \
1092                 false)                                          \
1093         BCH_OPT(wide_macs,                                      \
1094                 bch_bool_opt, 0, 2,                             \
1095                 BCH_SB_128_BIT_MACS,                            \
1096                 true)
1097
1098 /* backing device specific stuff: */
1099
1100 struct backingdev_sb {
1101         __le64                  csum;
1102         __le64                  offset; /* sector where this sb was written */
1103         __le64                  version; /* of on disk format */
1104
1105         uuid_le                 magic;  /* bcache superblock UUID */
1106
1107         uuid_le                 disk_uuid;
1108
1109         /*
1110          * Internal cache set UUID - xored with various magic numbers and thus
1111          * must never change:
1112          */
1113         union {
1114                 uuid_le         set_uuid;
1115                 __le64          set_magic;
1116         };
1117         __u8                    label[BCH_SB_LABEL_SIZE];
1118
1119         __le64                  flags;
1120
1121         /* Incremented each time superblock is written: */
1122         __le64                  seq;
1123
1124         /*
1125          * User visible UUID for identifying the cache set the user is allowed
1126          * to change:
1127          *
1128          * XXX hooked up?
1129          */
1130         uuid_le                 user_uuid;
1131         __le64                  pad1[6];
1132
1133         __le64                  data_offset;
1134         __le16                  block_size;     /* sectors */
1135         __le16                  pad2[3];
1136
1137         __le32                  last_mount;     /* time_t */
1138         __le16                  pad3;
1139         /* size of variable length portion - always 0 for backingdev superblock */
1140         __le16                  u64s;
1141         __u64                   _data[0];
1142 };
1143
1144 LE64_BITMASK(BDEV_CACHE_MODE,           struct backingdev_sb, flags, 0, 4);
1145 #define CACHE_MODE_WRITETHROUGH         0U
1146 #define CACHE_MODE_WRITEBACK            1U
1147 #define CACHE_MODE_WRITEAROUND          2U
1148 #define CACHE_MODE_NONE                 3U
1149
1150 LE64_BITMASK(BDEV_STATE,                struct backingdev_sb, flags, 61, 63);
1151 #define BDEV_STATE_NONE                 0U
1152 #define BDEV_STATE_CLEAN                1U
1153 #define BDEV_STATE_DIRTY                2U
1154 #define BDEV_STATE_STALE                3U
1155
1156 #define BDEV_DATA_START_DEFAULT         16      /* sectors */
1157
1158 static inline _Bool __SB_IS_BDEV(__u64 version)
1159 {
1160         return version == BCACHE_SB_VERSION_BDEV
1161                 || version == BCACHE_SB_VERSION_BDEV_WITH_OFFSET;
1162 }
1163
1164 static inline _Bool SB_IS_BDEV(const struct bch_sb *sb)
1165 {
1166         return __SB_IS_BDEV(sb->version);
1167 }
1168
1169 /*
1170  * Magic numbers
1171  *
1172  * The various other data structures have their own magic numbers, which are
1173  * xored with the first part of the cache set's UUID
1174  */
1175
1176 #define BCACHE_MAGIC                                                    \
1177         UUID_LE(0xf67385c6, 0x1a4e, 0xca45,                             \
1178                 0x82, 0x65, 0xf5, 0x7f, 0x48, 0xba, 0x6d, 0x81)
1179
1180 #define BCACHE_STATFS_MAGIC             0xca451a4e
1181
1182 #define JSET_MAGIC              __cpu_to_le64(0x245235c1a3625032ULL)
1183 #define PSET_MAGIC              __cpu_to_le64(0x6750e15f87337f91ULL)
1184 #define BSET_MAGIC              __cpu_to_le64(0x90135c78b99e07f5ULL)
1185
1186 static inline __le64 __bch_sb_magic(struct bch_sb *sb)
1187 {
1188         __le64 ret;
1189         memcpy(&ret, &sb->uuid, sizeof(ret));
1190         return ret;
1191 }
1192
1193 static inline __u64 __jset_magic(struct bch_sb *sb)
1194 {
1195         return __le64_to_cpu(__bch_sb_magic(sb) ^ JSET_MAGIC);
1196 }
1197
1198 static inline __u64 __pset_magic(struct bch_sb *sb)
1199 {
1200         return __le64_to_cpu(__bch_sb_magic(sb) ^ PSET_MAGIC);
1201 }
1202
1203 static inline __u64 __bset_magic(struct bch_sb *sb)
1204 {
1205         return __le64_to_cpu(__bch_sb_magic(sb) ^ BSET_MAGIC);
1206 }
1207
1208 /* Journal */
1209
1210 #define BCACHE_JSET_VERSION_UUIDv1      1
1211 #define BCACHE_JSET_VERSION_UUID        1       /* Always latest UUID format */
1212 #define BCACHE_JSET_VERSION_JKEYS       2
1213 #define BCACHE_JSET_VERSION             2
1214
1215 struct jset_entry {
1216         __le16                  u64s;
1217         __u8                    btree_id;
1218         __u8                    level;
1219         __le32                  flags; /* designates what this jset holds */
1220
1221         union {
1222                 struct bkey_i   start[0];
1223                 __u64           _data[0];
1224         };
1225 };
1226
1227 #define JSET_KEYS_U64s  (sizeof(struct jset_entry) / sizeof(__u64))
1228
1229 LE32_BITMASK(JOURNAL_ENTRY_TYPE,        struct jset_entry, flags, 0, 8);
1230 enum {
1231         JOURNAL_ENTRY_BTREE_KEYS        = 0,
1232         JOURNAL_ENTRY_BTREE_ROOT        = 1,
1233         JOURNAL_ENTRY_PRIO_PTRS         = 2,
1234
1235         /*
1236          * Journal sequence numbers can be blacklisted: bsets record the max
1237          * sequence number of all the journal entries they contain updates for,
1238          * so that on recovery we can ignore those bsets that contain index
1239          * updates newer that what made it into the journal.
1240          *
1241          * This means that we can't reuse that journal_seq - we have to skip it,
1242          * and then record that we skipped it so that the next time we crash and
1243          * recover we don't think there was a missing journal entry.
1244          */
1245         JOURNAL_ENTRY_JOURNAL_SEQ_BLACKLISTED = 3,
1246 };
1247
1248 /*
1249  * On disk format for a journal entry:
1250  * seq is monotonically increasing; every journal entry has its own unique
1251  * sequence number.
1252  *
1253  * last_seq is the oldest journal entry that still has keys the btree hasn't
1254  * flushed to disk yet.
1255  *
1256  * version is for on disk format changes.
1257  */
1258 struct jset {
1259         struct bch_csum         csum;
1260
1261         __le64                  magic;
1262         __le64                  seq;
1263         __le32                  version;
1264         __le32                  flags;
1265
1266         __le32                  u64s; /* size of d[] in u64s */
1267
1268         __u8                    encrypted_start[0];
1269
1270         __le16                  read_clock;
1271         __le16                  write_clock;
1272
1273         /* Sequence number of oldest dirty journal entry */
1274         __le64                  last_seq;
1275
1276
1277         union {
1278                 struct jset_entry start[0];
1279                 __u64           _data[0];
1280         };
1281 } __attribute__((packed));
1282
1283 LE32_BITMASK(JSET_CSUM_TYPE,    struct jset, flags, 0, 4);
1284 LE32_BITMASK(JSET_BIG_ENDIAN,   struct jset, flags, 4, 5);
1285
1286 #define BCH_JOURNAL_BUCKETS_MIN         20
1287
1288 /* Bucket prios/gens */
1289
1290 struct prio_set {
1291         struct bch_csum         csum;
1292
1293         __le64                  magic;
1294         __le32                  nonce[3];
1295         __le16                  version;
1296         __le16                  flags;
1297
1298         __u8                    encrypted_start[0];
1299
1300         __le64                  next_bucket;
1301
1302         struct bucket_disk {
1303                 __le16          read_prio;
1304                 __le16          write_prio;
1305                 __u8            gen;
1306         } __attribute__((packed)) data[];
1307 } __attribute__((packed));
1308
1309 LE32_BITMASK(PSET_CSUM_TYPE,    struct prio_set, flags, 0, 4);
1310
1311 /* Btree: */
1312
1313 #define DEFINE_BCH_BTREE_IDS()                                  \
1314         DEF_BTREE_ID(EXTENTS, 0, "extents")                     \
1315         DEF_BTREE_ID(INODES,  1, "inodes")                      \
1316         DEF_BTREE_ID(DIRENTS, 2, "dirents")                     \
1317         DEF_BTREE_ID(XATTRS,  3, "xattrs")
1318
1319 #define DEF_BTREE_ID(kwd, val, name) BTREE_ID_##kwd = val,
1320
1321 enum btree_id {
1322         DEFINE_BCH_BTREE_IDS()
1323         BTREE_ID_NR
1324 };
1325
1326 #undef DEF_BTREE_ID
1327
1328 #define BTREE_MAX_DEPTH         4U
1329
1330 /* Btree nodes */
1331
1332 /* Version 1: Seed pointer into btree node checksum
1333  */
1334 #define BCACHE_BSET_CSUM                1
1335 #define BCACHE_BSET_KEY_v1              2
1336 #define BCACHE_BSET_JOURNAL_SEQ         3
1337 #define BCACHE_BSET_VERSION             3
1338
1339 /*
1340  * Btree nodes
1341  *
1342  * On disk a btree node is a list/log of these; within each set the keys are
1343  * sorted
1344  */
1345 struct bset {
1346         __le64                  seq;
1347
1348         /*
1349          * Highest journal entry this bset contains keys for.
1350          * If on recovery we don't see that journal entry, this bset is ignored:
1351          * this allows us to preserve the order of all index updates after a
1352          * crash, since the journal records a total order of all index updates
1353          * and anything that didn't make it to the journal doesn't get used.
1354          */
1355         __le64                  journal_seq;
1356
1357         __le32                  flags;
1358         __le16                  version;
1359         __le16                  u64s; /* count of d[] in u64s */
1360
1361         union {
1362                 struct bkey_packed start[0];
1363                 __u64           _data[0];
1364         };
1365 } __attribute__((packed));
1366
1367 LE32_BITMASK(BSET_CSUM_TYPE,    struct bset, flags, 0, 4);
1368
1369 LE32_BITMASK(BSET_BIG_ENDIAN,   struct bset, flags, 4, 5);
1370 LE32_BITMASK(BSET_SEPARATE_WHITEOUTS,
1371                                 struct bset, flags, 5, 6);
1372
1373 struct btree_node {
1374         struct bch_csum         csum;
1375         __le64                  magic;
1376
1377         /* this flags field is encrypted, unlike bset->flags: */
1378         __le64                  flags;
1379
1380         /* Closed interval: */
1381         struct bpos             min_key;
1382         struct bpos             max_key;
1383         struct bch_extent_ptr   ptr;
1384         struct bkey_format      format;
1385
1386         union {
1387         struct bset             keys;
1388         struct {
1389                 __u8            pad[22];
1390                 __le16          u64s;
1391                 __u64           _data[0];
1392
1393         };
1394         };
1395 } __attribute__((packed));
1396
1397 LE64_BITMASK(BTREE_NODE_ID,     struct btree_node, flags, 0, 4);
1398 LE64_BITMASK(BTREE_NODE_LEVEL,  struct btree_node, flags, 4, 8);
1399
1400 struct btree_node_entry {
1401         struct bch_csum         csum;
1402
1403         union {
1404         struct bset             keys;
1405         struct {
1406                 __u8            pad[22];
1407                 __le16          u64s;
1408                 __u64           _data[0];
1409
1410         };
1411         };
1412 } __attribute__((packed));
1413
1414 /* OBSOLETE */
1415
1416 #define BITMASK(name, type, field, offset, end)                         \
1417 static const unsigned   name##_OFFSET = offset;                         \
1418 static const unsigned   name##_BITS = (end - offset);                   \
1419 static const __u64      name##_MAX = (1ULL << (end - offset)) - 1;      \
1420                                                                         \
1421 static inline __u64 name(const type *k)                                 \
1422 { return (k->field >> offset) & ~(~0ULL << (end - offset)); }           \
1423                                                                         \
1424 static inline void SET_##name(type *k, __u64 v)                         \
1425 {                                                                       \
1426         k->field &= ~(~(~0ULL << (end - offset)) << offset);            \
1427         k->field |= (v & ~(~0ULL << (end - offset))) << offset;         \
1428 }
1429
1430 struct bkey_v0 {
1431         __u64   high;
1432         __u64   low;
1433         __u64   ptr[];
1434 };
1435
1436 #define KEY0_FIELD(name, field, offset, size)                           \
1437         BITMASK(name, struct bkey_v0, field, offset, size)
1438
1439 KEY0_FIELD(KEY0_PTRS,           high, 60, 63)
1440 KEY0_FIELD(KEY0_CSUM,           high, 56, 58)
1441 KEY0_FIELD(KEY0_DIRTY,          high, 36, 37)
1442
1443 KEY0_FIELD(KEY0_SIZE,           high, 20, 36)
1444 KEY0_FIELD(KEY0_INODE,          high, 0,  20)
1445
1446 static inline unsigned long bkey_v0_u64s(const struct bkey_v0 *k)
1447 {
1448         return (sizeof(struct bkey_v0) / sizeof(__u64)) + KEY0_PTRS(k);
1449 }
1450
1451 static inline struct bkey_v0 *bkey_v0_next(const struct bkey_v0 *k)
1452 {
1453         __u64 *d = (__u64 *) k;
1454
1455         return (struct bkey_v0 *) (d + bkey_v0_u64s(k));
1456 }
1457
1458 struct jset_v0 {
1459         __u64                   csum;
1460         __u64                   magic;
1461         __u64                   seq;
1462         __u32                   version;
1463         __u32                   keys;
1464
1465         __u64                   last_seq;
1466
1467         __BKEY_PADDED(uuid_bucket, 4);
1468         __BKEY_PADDED(btree_root, 4);
1469         __u16                   btree_level;
1470         __u16                   pad[3];
1471
1472         __u64                   prio_bucket[64];
1473
1474         union {
1475                 struct bkey     start[0];
1476                 __u64           d[0];
1477         };
1478 };
1479
1480 /* UUIDS - per backing device/flash only volume metadata */
1481
1482 struct uuid_entry_v0 {
1483         uuid_le         uuid;
1484         __u8            label[32];
1485         __u32           first_reg;
1486         __u32           last_reg;
1487         __u32           invalidated;
1488         __u32           pad;
1489 };
1490
1491 struct uuid_entry {
1492         union {
1493                 struct {
1494                         uuid_le uuid;
1495                         __u8    label[32];
1496                         __u32   first_reg;
1497                         __u32   last_reg;
1498                         __u32   invalidated;
1499
1500                         __u32   flags;
1501                         /* Size of flash only volumes */
1502                         __u64   sectors;
1503                 };
1504
1505                 __u8            pad[128];
1506         };
1507 };
1508
1509 BITMASK(UUID_FLASH_ONLY,        struct uuid_entry, flags, 0, 1);
1510
1511 #ifdef __cplusplus
1512 }
1513 #endif
1514 #endif /* _LINUX_BCACHE_H */
1515
1516 /* vim: set foldnestmax=2: */