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