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 int i) \
34{ return (k->ptr[i] >> offset) & ~(~0ULL << size); } \
35 \
36static inline void SET_##name(struct bkey *k, unsigned int 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
121 return (struct bkey *) (d + bkey_u64s(k));
122}
123
124static inline struct bkey *bkey_idx(const struct bkey *k, unsigned int nr_keys)
125{
126 __u64 *d = (void *) k;
127
128 return (struct bkey *) (d + nr_keys);
129}
130
131#define BKEY_PAD 8
132
133#define BKEY_PADDED(key) \
134 union { struct bkey key; __u64 key ## _pad[BKEY_PAD]; }
135
136
137
138
139
140
141
142
143
144#define BCACHE_SB_VERSION_CDEV 0
145#define BCACHE_SB_VERSION_BDEV 1
146#define BCACHE_SB_VERSION_CDEV_WITH_UUID 3
147#define BCACHE_SB_VERSION_BDEV_WITH_OFFSET 4
148#define BCACHE_SB_MAX_VERSION 4
149
150#define SB_SECTOR 8
151#define SB_SIZE 4096
152#define SB_LABEL_SIZE 32
153#define SB_JOURNAL_BUCKETS 256U
154
155#define MAX_CACHES_PER_SET 8
156
157#define BDEV_DATA_START_DEFAULT 16
158
159struct cache_sb {
160 __u64 csum;
161 __u64 offset;
162 __u64 version;
163
164 __u8 magic[16];
165
166 __u8 uuid[16];
167 union {
168 __u8 set_uuid[16];
169 __u64 set_magic;
170 };
171 __u8 label[SB_LABEL_SIZE];
172
173 __u64 flags;
174 __u64 seq;
175 __u64 pad[8];
176
177 union {
178 struct {
179
180 __u64 nbuckets;
181
182 __u16 block_size;
183 __u16 bucket_size;
184
185 __u16 nr_in_set;
186 __u16 nr_this_dev;
187 };
188 struct {
189
190 __u64 data_offset;
191
192
193
194
195
196
197 };
198 };
199
200 __u32 last_mount;
201
202 __u16 first_bucket;
203 union {
204 __u16 njournal_buckets;
205 __u16 keys;
206 };
207 __u64 d[SB_JOURNAL_BUCKETS];
208};
209
210static inline _Bool SB_IS_BDEV(const struct cache_sb *sb)
211{
212 return sb->version == BCACHE_SB_VERSION_BDEV
213 || sb->version == BCACHE_SB_VERSION_BDEV_WITH_OFFSET;
214}
215
216BITMASK(CACHE_SYNC, struct cache_sb, flags, 0, 1);
217BITMASK(CACHE_DISCARD, struct cache_sb, flags, 1, 1);
218BITMASK(CACHE_REPLACEMENT, struct cache_sb, flags, 2, 3);
219#define CACHE_REPLACEMENT_LRU 0U
220#define CACHE_REPLACEMENT_FIFO 1U
221#define CACHE_REPLACEMENT_RANDOM 2U
222
223BITMASK(BDEV_CACHE_MODE, struct cache_sb, flags, 0, 4);
224#define CACHE_MODE_WRITETHROUGH 0U
225#define CACHE_MODE_WRITEBACK 1U
226#define CACHE_MODE_WRITEAROUND 2U
227#define CACHE_MODE_NONE 3U
228BITMASK(BDEV_STATE, struct cache_sb, flags, 61, 2);
229#define BDEV_STATE_NONE 0U
230#define BDEV_STATE_CLEAN 1U
231#define BDEV_STATE_DIRTY 2U
232#define BDEV_STATE_STALE 3U
233
234
235
236
237
238
239
240
241#define JSET_MAGIC 0x245235c1a3625032ULL
242#define PSET_MAGIC 0x6750e15f87337f91ULL
243#define BSET_MAGIC 0x90135c78b99e07f5ULL
244
245static inline __u64 jset_magic(struct cache_sb *sb)
246{
247 return sb->set_magic ^ JSET_MAGIC;
248}
249
250static inline __u64 pset_magic(struct cache_sb *sb)
251{
252 return sb->set_magic ^ PSET_MAGIC;
253}
254
255static inline __u64 bset_magic(struct cache_sb *sb)
256{
257 return sb->set_magic ^ BSET_MAGIC;
258}
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273#define BCACHE_JSET_VERSION_UUIDv1 1
274#define BCACHE_JSET_VERSION_UUID 1
275#define BCACHE_JSET_VERSION 1
276
277struct jset {
278 __u64 csum;
279 __u64 magic;
280 __u64 seq;
281 __u32 version;
282 __u32 keys;
283
284 __u64 last_seq;
285
286 BKEY_PADDED(uuid_bucket);
287 BKEY_PADDED(btree_root);
288 __u16 btree_level;
289 __u16 pad[3];
290
291 __u64 prio_bucket[MAX_CACHES_PER_SET];
292
293 union {
294 struct bkey start[0];
295 __u64 d[0];
296 };
297};
298
299
300
301struct prio_set {
302 __u64 csum;
303 __u64 magic;
304 __u64 seq;
305 __u32 version;
306 __u32 pad;
307
308 __u64 next_bucket;
309
310 struct bucket_disk {
311 __u16 prio;
312 __u8 gen;
313 } __attribute((packed)) data[];
314};
315
316
317
318struct uuid_entry {
319 union {
320 struct {
321 __u8 uuid[16];
322 __u8 label[32];
323 __u32 first_reg;
324 __u32 last_reg;
325 __u32 invalidated;
326
327 __u32 flags;
328
329 __u64 sectors;
330 };
331
332 __u8 pad[128];
333 };
334};
335
336BITMASK(UUID_FLASH_ONLY, struct uuid_entry, flags, 0, 1);
337
338
339
340
341
342#define BCACHE_BSET_CSUM 1
343#define BCACHE_BSET_VERSION 1
344
345
346
347
348
349
350
351struct bset {
352 __u64 csum;
353 __u64 magic;
354 __u64 seq;
355 __u32 version;
356 __u32 keys;
357
358 union {
359 struct bkey start[0];
360 __u64 d[0];
361 };
362};
363
364
365
366
367
368struct uuid_entry_v0 {
369 __u8 uuid[16];
370 __u8 label[32];
371 __u32 first_reg;
372 __u32 last_reg;
373 __u32 invalidated;
374 __u32 pad;
375};
376
377#endif
378