]> git.sesse.net Git - bcachefs-tools-debian/blob - bcache.h
ac1633cd0e0b18d1c3d0ba16345d6e97004f7526
[bcachefs-tools-debian] / bcache.h
1 #ifndef _BCACHE_H
2 #define _BCACHE_H
3
4 #define BITMASK(name, type, field, offset, size)                \
5 static inline uint64_t name(const type *k)                      \
6 { return (k->field >> offset) & ~(((uint64_t) ~0) << size); }   \
7                                                                 \
8 static inline void SET_##name(type *k, uint64_t v)              \
9 {                                                               \
10         k->field &= ~(~((uint64_t) ~0 << size) << offset);      \
11         k->field |= v << offset;                                \
12 }
13
14 static const char bcache_magic[] = {
15         0xc6, 0x85, 0x73, 0xf6, 0x4e, 0x1a, 0x45, 0xca,
16         0x82, 0x65, 0xf5, 0x7f, 0x48, 0xba, 0x6d, 0x81 };
17
18 /*
19  * Version 0: Cache device
20  * Version 1: Backing device
21  * Version 2: Seed pointer into btree node checksum
22  * Version 3: Cache device with new UUID format
23  * Version 4: Backing device with data offset
24  */
25 #define BCACHE_SB_VERSION_CDEV                  0
26 #define BCACHE_SB_VERSION_BDEV                  1
27 #define BCACHE_SB_VERSION_CDEV_WITH_UUID        3
28 #define BCACHE_SB_VERSION_BDEV_WITH_OFFSET      4
29 #define BCACHE_SB_MAX_VERSION                   4
30
31 #define SB_SECTOR               8
32 #define SB_LABEL_SIZE           32
33 #define SB_JOURNAL_BUCKETS      256U
34 #define BDEV_DATA_START_DEFAULT 16      /* sectors */
35 #define SB_START                (SB_SECTOR * 512)
36
37 struct cache_sb {
38         uint64_t                csum;
39         uint64_t                offset; /* sector where this sb was written */
40         uint64_t                version;
41
42         uint8_t                 magic[16];
43
44         uint8_t                 uuid[16];
45         union {
46                 uint8_t         set_uuid[16];
47                 uint64_t        set_magic;
48         };
49         uint8_t                 label[SB_LABEL_SIZE];
50
51         uint64_t                flags;
52         uint64_t                seq;
53         uint64_t                pad[8];
54
55         union {
56         struct {
57                 /* Cache devices */
58                 uint64_t        nbuckets;       /* device size */
59
60                 uint16_t        block_size;     /* sectors */
61                 uint16_t        bucket_size;    /* sectors */
62
63                 uint16_t        nr_in_set;
64                 uint16_t        nr_this_dev;
65         };
66         struct {
67                 /* Backing devices */
68                 uint64_t        data_offset;
69
70                 /*
71                  * block_size from the cache device section is still used by
72                  * backing devices, so don't add anything here until we fix
73                  * things to not need it for backing devices anymore
74                  */
75         };
76         };
77
78         uint32_t                last_mount;     /* time_t */
79
80         uint16_t                first_bucket;
81         union {
82                 uint16_t        njournal_buckets;
83                 uint16_t        keys;
84         };
85         uint64_t                d[SB_JOURNAL_BUCKETS];  /* journal buckets */
86 };
87
88 static inline bool SB_IS_BDEV(const struct cache_sb *sb)
89 {
90         return sb->version == BCACHE_SB_VERSION_BDEV
91                 || sb->version == BCACHE_SB_VERSION_BDEV_WITH_OFFSET;
92 }
93
94 BITMASK(CACHE_SYNC,             struct cache_sb, flags, 0, 1);
95 BITMASK(CACHE_DISCARD,          struct cache_sb, flags, 1, 1);
96 BITMASK(CACHE_REPLACEMENT,      struct cache_sb, flags, 2, 3);
97 #define CACHE_REPLACEMENT_LRU   0U
98 #define CACHE_REPLACEMENT_FIFO  1U
99 #define CACHE_REPLACEMENT_RANDOM 2U
100
101 BITMASK(BDEV_CACHE_MODE,        struct cache_sb, flags, 0, 4);
102 #define CACHE_MODE_WRITETHROUGH 0U
103 #define CACHE_MODE_WRITEBACK    1U
104 #define CACHE_MODE_WRITEAROUND  2U
105 #define CACHE_MODE_NONE         3U
106 BITMASK(BDEV_STATE,             struct cache_sb, flags, 61, 2);
107 #define BDEV_STATE_NONE         0U
108 #define BDEV_STATE_CLEAN        1U
109 #define BDEV_STATE_DIRTY        2U
110 #define BDEV_STATE_STALE        3U
111
112 inline uint64_t crc64(const void *_data, size_t len);
113
114 #define node(i, j)              ((void *) ((i)->d + (j)))
115 #define end(i)                  node(i, (i)->keys)
116
117 #define csum_set(i)                                                     \
118         crc64(((void *) (i)) + 8, ((void *) end(i)) - (((void *) (i)) + 8))
119
120 #endif