1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2 #ifndef _LINUX_BCACHE_H
3 #define _LINUX_BCACHE_H
4 
5 /*
6  * Bcache on disk data structures
7  */
8 
9 #include <linux/types.h>
10 
11 #define BITMASK(name, type, field, offset, size)		\
12 static inline uint64_t name(const type *k)				\
13 { return (k->field >> offset) & ~(~0ULL << size); }		\
14 								\
15 static inline void SET_##name(type *k, uint64_t v)			\
16 {								\
17 	k->field &= ~(~(~0ULL << size) << offset);		\
18 	k->field |= (v & ~(~0ULL << size)) << offset;		\
19 }
20 
21 /* Btree keys - all units are in sectors */
22 
23 struct bkey {
24 	uint64_t	high;
25 	uint64_t	low;
26 	uint64_t	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)					\
33 static inline uint64_t name(const struct bkey *k, unsigned int i)		\
34 { return (k->ptr[i] >> offset) & ~(~0ULL << size); }			\
35 									\
36 static inline void SET_##name(struct bkey *k, unsigned int i, uint64_t 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 
45 KEY_FIELD(KEY_PTRS,	high, 60, 3)
46 KEY_FIELD(HEADER_SIZE,	high, 58, 2)
47 KEY_FIELD(KEY_CSUM,	high, 56, 2)
48 KEY_FIELD(KEY_PINNED,	high, 55, 1)
49 KEY_FIELD(KEY_DIRTY,	high, 36, 1)
50 
51 KEY_FIELD(KEY_SIZE,	high, 20, KEY_SIZE_BITS)
52 KEY_FIELD(KEY_INODE,	high, 0,  20)
53 
54 /* Next time I change the on disk format, KEY_OFFSET() won't be 64 bits */
55 
KEY_OFFSET(const struct bkey * k)56 static inline uint64_t KEY_OFFSET(const struct bkey *k)
57 {
58 	return k->low;
59 }
60 
SET_KEY_OFFSET(struct bkey * k,uint64_t v)61 static inline void SET_KEY_OFFSET(struct bkey *k, uint64_t v)
62 {
63 	k->low = v;
64 }
65 
66 /*
67  * The high bit being set is a relic from when we used it to do binary
68  * searches - it told you where a key started. It's not used anymore,
69  * and can probably be safely dropped.
70  */
71 #define KEY(inode, offset, size)					\
72 ((struct bkey) {							\
73 	.high = (1ULL << 63) | ((uint64_t) (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 
88 PTR_FIELD(PTR_DEV,			51, PTR_DEV_BITS)
89 PTR_FIELD(PTR_OFFSET,			8,  43)
90 PTR_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 	((((uint64_t) dev) << 51) | ((uint64_t) offset) << 8 | gen)
96 
97 /* Bkey utility code */
98 
bkey_u64s(const struct bkey * k)99 static inline unsigned long bkey_u64s(const struct bkey *k)
100 {
101 	return (sizeof(struct bkey) / sizeof(uint64_t)) + KEY_PTRS(k);
102 }
103 
bkey_bytes(const struct bkey * k)104 static inline unsigned long bkey_bytes(const struct bkey *k)
105 {
106 	return bkey_u64s(k) * sizeof(uint64_t);
107 }
108 
109 #define bkey_copy(_dest, _src)	memcpy(_dest, _src, bkey_bytes(_src))
110 
bkey_copy_key(struct bkey * dest,const struct bkey * src)111 static 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 
bkey_next(const struct bkey * k)117 static inline struct bkey *bkey_next(const struct bkey *k)
118 {
119 	uint64_t *d = (void *) k;
120 
121 	return (struct bkey *) (d + bkey_u64s(k));
122 }
123 
bkey_idx(const struct bkey * k,unsigned int nr_keys)124 static inline struct bkey *bkey_idx(const struct bkey *k, unsigned int nr_keys)
125 {
126 	uint64_t *d = (void *) k;
127 
128 	return (struct bkey *) (d + nr_keys);
129 }
130 /* Enough for a key with 6 pointers */
131 #define BKEY_PAD		8
132 
133 #define BKEY_PADDED(key)					\
134 	union { struct bkey key; uint64_t key ## _pad[BKEY_PAD]; }
135 
136 /* Superblock */
137 
138 /* Version 0: Cache device
139  * Version 1: Backing device
140  * Version 2: Seed pointer into btree node checksum
141  * Version 3: Cache device with new UUID format
142  * Version 4: Backing device with data offset
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_VERSION_CDEV_WITH_FEATURES	5
149 #define BCACHE_SB_VERSION_BDEV_WITH_FEATURES	6
150 #define BCACHE_SB_MAX_VERSION			6
151 
152 #define SB_SECTOR			8
153 #define SB_OFFSET			(SB_SECTOR << SECTOR_SHIFT)
154 #define SB_SIZE				4096
155 #define SB_LABEL_SIZE			32
156 #define SB_JOURNAL_BUCKETS		256U
157 /* SB_JOURNAL_BUCKETS must be divisible by BITS_PER_LONG */
158 #define MAX_CACHES_PER_SET		8
159 
160 #define BDEV_DATA_START_DEFAULT		16	/* sectors */
161 
162 struct cache_sb_disk {
163 	uint64_t			csum;
164 	uint64_t			offset;	/* sector where this sb was written */
165 	uint64_t			version;
166 
167 	uint8_t			magic[16];
168 
169 	uint8_t			uuid[16];
170 	union {
171 		uint8_t		set_uuid[16];
172 		uint64_t		set_magic;
173 	};
174 	uint8_t			label[SB_LABEL_SIZE];
175 
176 	uint64_t			flags;
177 	uint64_t			seq;
178 
179 	uint64_t			feature_compat;
180 	uint64_t			feature_incompat;
181 	uint64_t			feature_ro_compat;
182 
183 	uint64_t			pad[5];
184 
185 	union {
186 	struct {
187 		/* Cache devices */
188 		uint64_t		nbuckets;	/* device size */
189 
190 		uint16_t		block_size;	/* sectors */
191 		uint16_t		bucket_size;	/* sectors */
192 
193 		uint16_t		nr_in_set;
194 		uint16_t		nr_this_dev;
195 	};
196 	struct {
197 		/* Backing devices */
198 		uint64_t		data_offset;
199 
200 		/*
201 		 * block_size from the cache device section is still used by
202 		 * backing devices, so don't add anything here until we fix
203 		 * things to not need it for backing devices anymore
204 		 */
205 	};
206 	};
207 
208 	uint32_t			last_mount;	/* time overflow in y2106 */
209 
210 	uint16_t			first_bucket;
211 	union {
212 		uint16_t		njournal_buckets;
213 		uint16_t		keys;
214 	};
215 	uint64_t			d[SB_JOURNAL_BUCKETS];	/* journal buckets */
216 	uint16_t			obso_bucket_size_hi;	/* obsoleted */
217 };
218 
219 /*
220  * This is for in-memory bcache super block.
221  * NOTE: cache_sb is NOT exactly mapping to cache_sb_disk, the member
222  *       size, ordering and even whole struct size may be different
223  *       from cache_sb_disk.
224  */
225 struct cache_sb {
226 	uint64_t			offset;	/* sector where this sb was written */
227 	uint64_t			version;
228 
229 	uint8_t			magic[16];
230 
231 	uint8_t			uuid[16];
232 	union {
233 		uint8_t		set_uuid[16];
234 		uint64_t		set_magic;
235 	};
236 	uint8_t			label[SB_LABEL_SIZE];
237 
238 	uint64_t			flags;
239 	uint64_t			seq;
240 
241 	uint64_t			feature_compat;
242 	uint64_t			feature_incompat;
243 	uint64_t			feature_ro_compat;
244 
245 	union {
246 	struct {
247 		/* Cache devices */
248 		uint64_t		nbuckets;	/* device size */
249 
250 		uint16_t		block_size;	/* sectors */
251 		uint16_t		nr_in_set;
252 		uint16_t		nr_this_dev;
253 		uint32_t		bucket_size;	/* sectors */
254 	};
255 	struct {
256 		/* Backing devices */
257 		uint64_t		data_offset;
258 
259 		/*
260 		 * block_size from the cache device section is still used by
261 		 * backing devices, so don't add anything here until we fix
262 		 * things to not need it for backing devices anymore
263 		 */
264 	};
265 	};
266 
267 	uint32_t			last_mount;	/* time overflow in y2106 */
268 
269 	uint16_t			first_bucket;
270 	union {
271 		uint16_t		njournal_buckets;
272 		uint16_t		keys;
273 	};
274 	uint64_t			d[SB_JOURNAL_BUCKETS];	/* journal buckets */
275 };
276 
SB_IS_BDEV(const struct cache_sb * sb)277 static inline _Bool SB_IS_BDEV(const struct cache_sb *sb)
278 {
279 	return sb->version == BCACHE_SB_VERSION_BDEV
280 		|| sb->version == BCACHE_SB_VERSION_BDEV_WITH_OFFSET
281 		|| sb->version == BCACHE_SB_VERSION_BDEV_WITH_FEATURES;
282 }
283 
284 BITMASK(CACHE_SYNC,			struct cache_sb, flags, 0, 1);
285 BITMASK(CACHE_DISCARD,			struct cache_sb, flags, 1, 1);
286 BITMASK(CACHE_REPLACEMENT,		struct cache_sb, flags, 2, 3);
287 #define CACHE_REPLACEMENT_LRU		0U
288 #define CACHE_REPLACEMENT_FIFO		1U
289 #define CACHE_REPLACEMENT_RANDOM	2U
290 
291 BITMASK(BDEV_CACHE_MODE,		struct cache_sb, flags, 0, 4);
292 #define CACHE_MODE_WRITETHROUGH		0U
293 #define CACHE_MODE_WRITEBACK		1U
294 #define CACHE_MODE_WRITEAROUND		2U
295 #define CACHE_MODE_NONE			3U
296 BITMASK(BDEV_STATE,			struct cache_sb, flags, 61, 2);
297 #define BDEV_STATE_NONE			0U
298 #define BDEV_STATE_CLEAN		1U
299 #define BDEV_STATE_DIRTY		2U
300 #define BDEV_STATE_STALE		3U
301 
302 /*
303  * Magic numbers
304  *
305  * The various other data structures have their own magic numbers, which are
306  * xored with the first part of the cache set's UUID
307  */
308 
309 #define JSET_MAGIC			0x245235c1a3625032ULL
310 #define PSET_MAGIC			0x6750e15f87337f91ULL
311 #define BSET_MAGIC			0x90135c78b99e07f5ULL
312 
jset_magic(struct cache_sb * sb)313 static inline uint64_t jset_magic(struct cache_sb *sb)
314 {
315 	return sb->set_magic ^ JSET_MAGIC;
316 }
317 
pset_magic(struct cache_sb * sb)318 static inline uint64_t pset_magic(struct cache_sb *sb)
319 {
320 	return sb->set_magic ^ PSET_MAGIC;
321 }
322 
bset_magic(struct cache_sb * sb)323 static inline uint64_t bset_magic(struct cache_sb *sb)
324 {
325 	return sb->set_magic ^ BSET_MAGIC;
326 }
327 
328 /*
329  * Journal
330  *
331  * On disk format for a journal entry:
332  * seq is monotonically increasing; every journal entry has its own unique
333  * sequence number.
334  *
335  * last_seq is the oldest journal entry that still has keys the btree hasn't
336  * flushed to disk yet.
337  *
338  * version is for on disk format changes.
339  */
340 
341 #define BCACHE_JSET_VERSION_UUIDv1	1
342 #define BCACHE_JSET_VERSION_UUID	1	/* Always latest UUID format */
343 #define BCACHE_JSET_VERSION		1
344 
345 struct jset {
346 	uint64_t			csum;
347 	uint64_t			magic;
348 	uint64_t			seq;
349 	uint32_t			version;
350 	uint32_t			keys;
351 
352 	uint64_t			last_seq;
353 
354 	BKEY_PADDED(uuid_bucket);
355 	BKEY_PADDED(btree_root);
356 	uint16_t			btree_level;
357 	uint16_t			pad[3];
358 
359 	uint64_t			prio_bucket[MAX_CACHES_PER_SET];
360 
361 	union {
362 		struct bkey	start[0];
363 		uint64_t		d[0];
364 	};
365 };
366 
367 /* Bucket prios/gens */
368 
369 struct prio_set {
370 	uint64_t			csum;
371 	uint64_t			magic;
372 	uint64_t			seq;
373 	uint32_t			version;
374 	uint32_t			pad;
375 
376 	uint64_t			next_bucket;
377 
378 	struct bucket_disk {
379 		uint16_t		prio;
380 		uint8_t		gen;
381 	} __attribute((packed)) data[];
382 };
383 
384 /* UUIDS - per backing device/flash only volume metadata */
385 
386 struct uuid_entry {
387 	union {
388 		struct {
389 			uint8_t	uuid[16];
390 			uint8_t	label[32];
391 			uint32_t	first_reg; /* time overflow in y2106 */
392 			uint32_t	last_reg;
393 			uint32_t	invalidated;
394 
395 			uint32_t	flags;
396 			/* Size of flash only volumes */
397 			uint64_t	sectors;
398 		};
399 
400 		uint8_t		pad[128];
401 	};
402 };
403 
404 BITMASK(UUID_FLASH_ONLY,	struct uuid_entry, flags, 0, 1);
405 
406 /* Btree nodes */
407 
408 /* Version 1: Seed pointer into btree node checksum
409  */
410 #define BCACHE_BSET_CSUM		1
411 #define BCACHE_BSET_VERSION		1
412 
413 /*
414  * Btree nodes
415  *
416  * On disk a btree node is a list/log of these; within each set the keys are
417  * sorted
418  */
419 struct bset {
420 	uint64_t			csum;
421 	uint64_t			magic;
422 	uint64_t			seq;
423 	uint32_t			version;
424 	uint32_t			keys;
425 
426 	union {
427 		struct bkey	start[0];
428 		uint64_t		d[0];
429 	};
430 };
431 
432 /* OBSOLETE */
433 
434 /* UUIDS - per backing device/flash only volume metadata */
435 
436 struct uuid_entry_v0 {
437 	uint8_t		uuid[16];
438 	uint8_t		label[32];
439 	uint32_t		first_reg;
440 	uint32_t		last_reg;
441 	uint32_t		invalidated;
442 	uint32_t		pad;
443 };
444 
445 #endif /* _LINUX_BCACHE_H */
446