1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2 #ifndef _LINUX_BCACHE_H
3 #define _LINUX_BCACHE_H
6 * Bcache on disk data structures
9 #include <linux/types.h>
11 #define BITMASK(name, type, field, offset, size) \
12 static inline __u64 name(const type *k) \
13 { return (k->field >> offset) & ~(~0ULL << size); } \
15 static inline void SET_##name(type *k, __u64 v) \
17 k->field &= ~(~(~0ULL << size) << offset); \
18 k->field |= (v & ~(~0ULL << size)) << offset; \
21 /* Btree keys - all units are in sectors */
29 #define KEY_FIELD(name, field, offset, size) \
30 BITMASK(name, struct bkey, field, offset, size)
32 #define PTR_FIELD(name, offset, size) \
33 static inline __u64 name(const struct bkey *k, unsigned i) \
34 { return (k->ptr[i] >> offset) & ~(~0ULL << size); } \
36 static inline void SET_##name(struct bkey *k, unsigned i, __u64 v) \
38 k->ptr[i] &= ~(~(~0ULL << size) << offset); \
39 k->ptr[i] |= (v & ~(~0ULL << size)) << offset; \
42 #define KEY_SIZE_BITS 16
43 #define KEY_MAX_U64S 8
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)
51 KEY_FIELD(KEY_SIZE
, high
, 20, KEY_SIZE_BITS
)
52 KEY_FIELD(KEY_INODE
, high
, 0, 20)
54 /* Next time I change the on disk format, KEY_OFFSET() won't be 64 bits */
56 static inline __u64
KEY_OFFSET(const struct bkey
*k
)
61 static inline void SET_KEY_OFFSET(struct bkey
*k
, __u64 v
)
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.
71 #define KEY(inode, offset, size) \
73 .high = (1ULL << 63) | ((__u64) (size) << 20) | (inode), \
77 #define ZERO_KEY KEY(0, 0, 0)
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)
83 #define KEY_START(k) (KEY_OFFSET(k) - KEY_SIZE(k))
84 #define START_KEY(k) KEY(KEY_INODE(k), KEY_START(k), 0)
86 #define PTR_DEV_BITS 12
88 PTR_FIELD(PTR_DEV
, 51, PTR_DEV_BITS
)
89 PTR_FIELD(PTR_OFFSET
, 8, 43)
90 PTR_FIELD(PTR_GEN
, 0, 8)
92 #define PTR_CHECK_DEV ((1 << PTR_DEV_BITS) - 1)
94 #define MAKE_PTR(gen, offset, dev) \
95 ((((__u64) dev) << 51) | ((__u64) offset) << 8 | gen)
97 /* Bkey utility code */
99 static inline unsigned long bkey_u64s(const struct bkey
*k
)
101 return (sizeof(struct bkey
) / sizeof(__u64
)) + KEY_PTRS(k
);
104 static inline unsigned long bkey_bytes(const struct bkey
*k
)
106 return bkey_u64s(k
) * sizeof(__u64
);
109 #define bkey_copy(_dest, _src) memcpy(_dest, _src, bkey_bytes(_src))
111 static inline void bkey_copy_key(struct bkey
*dest
, const struct bkey
*src
)
113 SET_KEY_INODE(dest
, KEY_INODE(src
));
114 SET_KEY_OFFSET(dest
, KEY_OFFSET(src
));
117 static inline struct bkey
*bkey_next(const struct bkey
*k
)
119 __u64
*d
= (void *) k
;
120 return (struct bkey
*) (d
+ bkey_u64s(k
));
123 static inline struct bkey
*bkey_idx(const struct bkey
*k
, unsigned nr_keys
)
125 __u64
*d
= (void *) k
;
126 return (struct bkey
*) (d
+ nr_keys
);
128 /* Enough for a key with 6 pointers */
131 #define BKEY_PADDED(key) \
132 union { struct bkey key; __u64 key ## _pad[BKEY_PAD]; }
136 /* Version 0: Cache device
137 * Version 1: Backing device
138 * Version 2: Seed pointer into btree node checksum
139 * Version 3: Cache device with new UUID format
140 * Version 4: Backing device with data offset
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
150 #define SB_LABEL_SIZE 32
151 #define SB_JOURNAL_BUCKETS 256U
152 /* SB_JOURNAL_BUCKETS must be divisible by BITS_PER_LONG */
153 #define MAX_CACHES_PER_SET 8
155 #define BDEV_DATA_START_DEFAULT 16 /* sectors */
159 __u64 offset
; /* sector where this sb was written */
169 __u8 label
[SB_LABEL_SIZE
];
178 __u64 nbuckets
; /* device size */
180 __u16 block_size
; /* sectors */
181 __u16 bucket_size
; /* sectors */
187 /* Backing devices */
191 * block_size from the cache device section is still used by
192 * backing devices, so don't add anything here until we fix
193 * things to not need it for backing devices anymore
198 __u32 last_mount
; /* time_t */
202 __u16 njournal_buckets
;
205 __u64 d
[SB_JOURNAL_BUCKETS
]; /* journal buckets */
208 static inline _Bool
SB_IS_BDEV(const struct cache_sb
*sb
)
210 return sb
->version
== BCACHE_SB_VERSION_BDEV
211 || sb
->version
== BCACHE_SB_VERSION_BDEV_WITH_OFFSET
;
214 BITMASK(CACHE_SYNC
, struct cache_sb
, flags
, 0, 1);
215 BITMASK(CACHE_DISCARD
, struct cache_sb
, flags
, 1, 1);
216 BITMASK(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
221 BITMASK(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
226 BITMASK(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
235 * The various other data structures have their own magic numbers, which are
236 * xored with the first part of the cache set's UUID
239 #define JSET_MAGIC 0x245235c1a3625032ULL
240 #define PSET_MAGIC 0x6750e15f87337f91ULL
241 #define BSET_MAGIC 0x90135c78b99e07f5ULL
243 static inline __u64
jset_magic(struct cache_sb
*sb
)
245 return sb
->set_magic
^ JSET_MAGIC
;
248 static inline __u64
pset_magic(struct cache_sb
*sb
)
250 return sb
->set_magic
^ PSET_MAGIC
;
253 static inline __u64
bset_magic(struct cache_sb
*sb
)
255 return sb
->set_magic
^ BSET_MAGIC
;
261 * On disk format for a journal entry:
262 * seq is monotonically increasing; every journal entry has its own unique
265 * last_seq is the oldest journal entry that still has keys the btree hasn't
266 * flushed to disk yet.
268 * version is for on disk format changes.
271 #define BCACHE_JSET_VERSION_UUIDv1 1
272 #define BCACHE_JSET_VERSION_UUID 1 /* Always latest UUID format */
273 #define BCACHE_JSET_VERSION 1
284 BKEY_PADDED(uuid_bucket
);
285 BKEY_PADDED(btree_root
);
289 __u64 prio_bucket
[MAX_CACHES_PER_SET
];
292 struct bkey start
[0];
297 /* Bucket prios/gens */
311 } __attribute((packed
)) data
[];
314 /* UUIDS - per backing device/flash only volume metadata */
326 /* Size of flash only volumes */
334 BITMASK(UUID_FLASH_ONLY
, struct uuid_entry
, flags
, 0, 1);
338 /* Version 1: Seed pointer into btree node checksum
340 #define BCACHE_BSET_CSUM 1
341 #define BCACHE_BSET_VERSION 1
346 * On disk a btree node is a list/log of these; within each set the keys are
357 struct bkey start
[0];
364 /* UUIDS - per backing device/flash only volume metadata */
366 struct uuid_entry_v0
{
375 #endif /* _LINUX_BCACHE_H */