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