1
2#ifndef _LINUX_BCACHE_H
3#define _LINUX_BCACHE_H
4
5
6
7
8
9#include <linux/types.h>
10
11#define BITMASK(name, type, field, offset, size) \
12static inline __u64 name(const type *k) \
13{ return (k->field >> offset) & ~(~0ULL << size); } \
14 \
15static inline void SET_##name(type *k, __u64 v) \
16{ \
17 k->field &= ~(~(~0ULL << size) << offset); \
18 k->field |= (v & ~(~0ULL << size)) << offset; \
19}
20
21
22
23struct bkey {
24 __u64 high;
25 __u64 low;
26 __u64 ptr[];
27};
28
29#define KEY_FIELD(name, field, offset, size) \
30 BITMASK(name, struct bkey, field, offset, size)
31
32#define PTR_FIELD(name, offset, size) \
33static inline __u64 name(const struct bkey *k, unsigned i) \
34{ return (k->ptr[i] >> offset) & ~(~0ULL << size); } \
35 \
36static inline void SET_##name(struct bkey *k, unsigned i, __u64 v) \
37{ \
38 k->ptr[i] &= ~(~(~0ULL << size) << offset); \
39 k->ptr[i] |= (v & ~(~0ULL << size)) << offset; \
40}
41
42#define KEY_SIZE_BITS 16
43#define KEY_MAX_U64S 8
44
45KEY_FIELD(KEY_PTRS, high, 60, 3)
46KEY_FIELD(HEADER_SIZE, high, 58, 2)
47KEY_FIELD(KEY_CSUM, high, 56, 2)
48KEY_FIELD(KEY_PINNED, high, 55, 1)
49KEY_FIELD(KEY_DIRTY, high, 36, 1)
50
51KEY_FIELD(KEY_SIZE, high, 20, KEY_SIZE_BITS)
52KEY_FIELD(KEY_INODE, high, 0, 20)
53
54
55
56static inline __u64 KEY_OFFSET(const struct bkey *k)
57{
58 return k->low;
59}
60
61static inline void SET_KEY_OFFSET(struct bkey *k, __u64 v)
62{
63 k->low = v;
64}
65
66
67
68
69
70
71#define KEY(inode, offset, size) \
72((struct bkey) { \
73 .high = (1ULL << 63) | ((__u64) (size) << 20) | (inode), \
74 .low = (offset) \
75})
76
77#define ZERO_KEY KEY(0, 0, 0)
78
79#define MAX_KEY_INODE (~(~0 << 20))
80#define MAX_KEY_OFFSET (~0ULL >> 1)
81#define MAX_KEY KEY(MAX_KEY_INODE, MAX_KEY_OFFSET, 0)
82
83#define KEY_START(k) (KEY_OFFSET(k) - KEY_SIZE(k))
84#define START_KEY(k) KEY(KEY_INODE(k), KEY_START(k), 0)
85
86#define PTR_DEV_BITS 12
87
88PTR_FIELD(PTR_DEV, 51, PTR_DEV_BITS)
89PTR_FIELD(PTR_OFFSET, 8, 43)
90PTR_FIELD(PTR_GEN, 0, 8)
91
92#define PTR_CHECK_DEV ((1 << PTR_DEV_BITS) - 1)
93
94#define MAKE_PTR(gen, offset, dev) \
95 ((((__u64) dev) << 51) | ((__u64) offset) << 8 | gen)
96
97
98
99static inline unsigned long bkey_u64s(const struct bkey *k)
100{
101 return (sizeof(struct bkey) / sizeof(__u64)) + KEY_PTRS(k);
102}
103
104static inline unsigned long bkey_bytes(const struct bkey *k)
105{
106 return bkey_u64s(k) * sizeof(__u64);
107}
108
109#define bkey_copy(_dest, _src) memcpy(_dest, _src, bkey_bytes(_src))
110
111static inline void bkey_copy_key(struct bkey *dest, const struct bkey *src)
112{
113 SET_KEY_INODE(dest, KEY_INODE(src));
114 SET_KEY_OFFSET(dest, KEY_OFFSET(src));
115}
116
117static inline struct bkey *bkey_next(const struct bkey *k)
118{
119 __u64 *d = (void *) k;
120 return (struct bkey *) (d + bkey_u64s(k));
121}
122
123static inline struct bkey *bkey_idx(const struct bkey *k, unsigned nr_keys)
124{
125 __u64 *d = (void *) k;
126 return (struct bkey *) (d + nr_keys);
127}
128
129#define BKEY_PAD 8
130
131#define BKEY_PADDED(key) \
132 union { struct bkey key; __u64 key ## _pad[BKEY_PAD]; }
133
134
135
136
137
138
139
140
141
142#define BCACHE_SB_VERSION_CDEV 0
143#define BCACHE_SB_VERSION_BDEV 1
144#define BCACHE_SB_VERSION_CDEV_WITH_UUID 3
145#define BCACHE_SB_VERSION_BDEV_WITH_OFFSET 4
146#define BCACHE_SB_MAX_VERSION 4
147
148#define SB_SECTOR 8
149#define SB_SIZE 4096
150#define SB_LABEL_SIZE 32
151#define SB_JOURNAL_BUCKETS 256U
152
153#define MAX_CACHES_PER_SET 8
154
155#define BDEV_DATA_START_DEFAULT 16
156
157struct cache_sb {
158 __u64 csum;
159 __u64 offset;
160 __u64 version;
161
162 __u8 magic[16];
163
164 __u8 uuid[16];
165 union {
166 __u8 set_uuid[16];
167 __u64 set_magic;
168 };
169 __u8 label[SB_LABEL_SIZE];
170
171 __u64 flags;
172 __u64 seq;
173 __u64 pad[8];
174
175 union {
176 struct {
177
178 __u64 nbuckets;
179
180 __u16 block_size;
181 __u16 bucket_size;
182
183 __u16 nr_in_set;
184 __u16 nr_this_dev;
185 };
186 struct {
187
188 __u64 data_offset;
189
190
191
192
193
194
195 };
196 };
197
198 __u32 last_mount;
199
200 __u16 first_bucket;
201 union {
202 __u16 njournal_buckets;
203 __u16 keys;
204 };
205 __u64 d[SB_JOURNAL_BUCKETS];
206};
207
208static inline _Bool SB_IS_BDEV(const struct cache_sb *sb)
209{
210 return sb->version == BCACHE_SB_VERSION_BDEV
211 || sb->version == BCACHE_SB_VERSION_BDEV_WITH_OFFSET;
212}
213
214BITMASK(CACHE_SYNC, struct cache_sb, flags, 0, 1);
215BITMASK(CACHE_DISCARD, struct cache_sb, flags, 1, 1);
216BITMASK(CACHE_REPLACEMENT, struct cache_sb, flags, 2, 3);
217#define CACHE_REPLACEMENT_LRU 0U
218#define CACHE_REPLACEMENT_FIFO 1U
219#define CACHE_REPLACEMENT_RANDOM 2U
220
221BITMASK(BDEV_CACHE_MODE, struct cache_sb, flags, 0, 4);
222#define CACHE_MODE_WRITETHROUGH 0U
223#define CACHE_MODE_WRITEBACK 1U
224#define CACHE_MODE_WRITEAROUND 2U
225#define CACHE_MODE_NONE 3U
226BITMASK(BDEV_STATE, struct cache_sb, flags, 61, 2);
227#define BDEV_STATE_NONE 0U
228#define BDEV_STATE_CLEAN 1U
229#define BDEV_STATE_DIRTY 2U
230#define BDEV_STATE_STALE 3U
231
232
233
234
235
236
237
238
239#define JSET_MAGIC 0x245235c1a3625032ULL
240#define PSET_MAGIC 0x6750e15f87337f91ULL
241#define BSET_MAGIC 0x90135c78b99e07f5ULL
242
243static inline __u64 jset_magic(struct cache_sb *sb)
244{
245 return sb->set_magic ^ JSET_MAGIC;
246}
247
248static inline __u64 pset_magic(struct cache_sb *sb)
249{
250 return sb->set_magic ^ PSET_MAGIC;
251}
252
253static inline __u64 bset_magic(struct cache_sb *sb)
254{
255 return sb->set_magic ^ BSET_MAGIC;
256}
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271#define BCACHE_JSET_VERSION_UUIDv1 1
272#define BCACHE_JSET_VERSION_UUID 1
273#define BCACHE_JSET_VERSION 1
274
275struct jset {
276 __u64 csum;
277 __u64 magic;
278 __u64 seq;
279 __u32 version;
280 __u32 keys;
281
282 __u64 last_seq;
283
284 BKEY_PADDED(uuid_bucket);
285 BKEY_PADDED(btree_root);
286 __u16 btree_level;
287 __u16 pad[3];
288
289 __u64 prio_bucket[MAX_CACHES_PER_SET];
290
291 union {
292 struct bkey start[0];
293 __u64 d[0];
294 };
295};
296
297
298
299struct prio_set {
300 __u64 csum;
301 __u64 magic;
302 __u64 seq;
303 __u32 version;
304 __u32 pad;
305
306 __u64 next_bucket;
307
308 struct bucket_disk {
309 __u16 prio;
310 __u8 gen;
311 } __attribute((packed)) data[];
312};
313
314
315
316struct uuid_entry {
317 union {
318 struct {
319 __u8 uuid[16];
320 __u8 label[32];
321 __u32 first_reg;
322 __u32 last_reg;
323 __u32 invalidated;
324
325 __u32 flags;
326
327 __u64 sectors;
328 };
329
330 __u8 pad[128];
331 };
332};
333
334BITMASK(UUID_FLASH_ONLY, struct uuid_entry, flags, 0, 1);
335
336
337
338
339
340#define BCACHE_BSET_CSUM 1
341#define BCACHE_BSET_VERSION 1
342
343
344
345
346
347
348
349struct bset {
350 __u64 csum;
351 __u64 magic;
352 __u64 seq;
353 __u32 version;
354 __u32 keys;
355
356 union {
357 struct bkey start[0];
358 __u64 d[0];
359 };
360};
361
362
363
364
365
366struct uuid_entry_v0 {
367 __u8 uuid[16];
368 __u8 label[32];
369 __u32 first_reg;
370 __u32 last_reg;
371 __u32 invalidated;
372 __u32 pad;
373};
374
375#endif
376