bcache.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
  2. #ifndef _LINUX_BCACHE_H
  3. #define _LINUX_BCACHE_H
  4. /*
  5. * Bcache on disk data structures
  6. */
  7. #include <linux/types.h>
  8. #define BITMASK(name, type, field, offset, size) \
  9. static inline __u64 name(const type *k) \
  10. { return (k->field >> offset) & ~(~0ULL << size); } \
  11. \
  12. static inline void SET_##name(type *k, __u64 v) \
  13. { \
  14. k->field &= ~(~(~0ULL << size) << offset); \
  15. k->field |= (v & ~(~0ULL << size)) << offset; \
  16. }
  17. /* Btree keys - all units are in sectors */
  18. struct bkey {
  19. __u64 high;
  20. __u64 low;
  21. __u64 ptr[];
  22. };
  23. #define KEY_FIELD(name, field, offset, size) \
  24. BITMASK(name, struct bkey, field, offset, size)
  25. #define PTR_FIELD(name, offset, size) \
  26. static inline __u64 name(const struct bkey *k, unsigned int i) \
  27. { return (k->ptr[i] >> offset) & ~(~0ULL << size); } \
  28. \
  29. static inline void SET_##name(struct bkey *k, unsigned int i, __u64 v) \
  30. { \
  31. k->ptr[i] &= ~(~(~0ULL << size) << offset); \
  32. k->ptr[i] |= (v & ~(~0ULL << size)) << offset; \
  33. }
  34. #define KEY_SIZE_BITS 16
  35. #define KEY_MAX_U64S 8
  36. KEY_FIELD(KEY_PTRS, high, 60, 3)
  37. KEY_FIELD(HEADER_SIZE, high, 58, 2)
  38. KEY_FIELD(KEY_CSUM, high, 56, 2)
  39. KEY_FIELD(KEY_PINNED, high, 55, 1)
  40. KEY_FIELD(KEY_DIRTY, high, 36, 1)
  41. KEY_FIELD(KEY_SIZE, high, 20, KEY_SIZE_BITS)
  42. KEY_FIELD(KEY_INODE, high, 0, 20)
  43. /* Next time I change the on disk format, KEY_OFFSET() won't be 64 bits */
  44. static inline __u64 KEY_OFFSET(const struct bkey *k)
  45. {
  46. return k->low;
  47. }
  48. static inline void SET_KEY_OFFSET(struct bkey *k, __u64 v)
  49. {
  50. k->low = v;
  51. }
  52. /*
  53. * The high bit being set is a relic from when we used it to do binary
  54. * searches - it told you where a key started. It's not used anymore,
  55. * and can probably be safely dropped.
  56. */
  57. #define KEY(inode, offset, size) \
  58. ((struct bkey) { \
  59. .high = (1ULL << 63) | ((__u64) (size) << 20) | (inode), \
  60. .low = (offset) \
  61. })
  62. #define ZERO_KEY KEY(0, 0, 0)
  63. #define MAX_KEY_INODE (~(~0 << 20))
  64. #define MAX_KEY_OFFSET (~0ULL >> 1)
  65. #define MAX_KEY KEY(MAX_KEY_INODE, MAX_KEY_OFFSET, 0)
  66. #define KEY_START(k) (KEY_OFFSET(k) - KEY_SIZE(k))
  67. #define START_KEY(k) KEY(KEY_INODE(k), KEY_START(k), 0)
  68. #define PTR_DEV_BITS 12
  69. PTR_FIELD(PTR_DEV, 51, PTR_DEV_BITS)
  70. PTR_FIELD(PTR_OFFSET, 8, 43)
  71. PTR_FIELD(PTR_GEN, 0, 8)
  72. #define PTR_CHECK_DEV ((1 << PTR_DEV_BITS) - 1)
  73. #define MAKE_PTR(gen, offset, dev) \
  74. ((((__u64) dev) << 51) | ((__u64) offset) << 8 | gen)
  75. /* Bkey utility code */
  76. static inline unsigned long bkey_u64s(const struct bkey *k)
  77. {
  78. return (sizeof(struct bkey) / sizeof(__u64)) + KEY_PTRS(k);
  79. }
  80. static inline unsigned long bkey_bytes(const struct bkey *k)
  81. {
  82. return bkey_u64s(k) * sizeof(__u64);
  83. }
  84. #define bkey_copy(_dest, _src) memcpy(_dest, _src, bkey_bytes(_src))
  85. static inline void bkey_copy_key(struct bkey *dest, const struct bkey *src)
  86. {
  87. SET_KEY_INODE(dest, KEY_INODE(src));
  88. SET_KEY_OFFSET(dest, KEY_OFFSET(src));
  89. }
  90. static inline struct bkey *bkey_next(const struct bkey *k)
  91. {
  92. __u64 *d = (void *) k;
  93. return (struct bkey *) (d + bkey_u64s(k));
  94. }
  95. static inline struct bkey *bkey_idx(const struct bkey *k, unsigned int nr_keys)
  96. {
  97. __u64 *d = (void *) k;
  98. return (struct bkey *) (d + nr_keys);
  99. }
  100. /* Enough for a key with 6 pointers */
  101. #define BKEY_PAD 8
  102. #define BKEY_PADDED(key) \
  103. union { struct bkey key; __u64 key ## _pad[BKEY_PAD]; }
  104. /* Superblock */
  105. /* Version 0: Cache device
  106. * Version 1: Backing device
  107. * Version 2: Seed pointer into btree node checksum
  108. * Version 3: Cache device with new UUID format
  109. * Version 4: Backing device with data offset
  110. */
  111. #define BCACHE_SB_VERSION_CDEV 0
  112. #define BCACHE_SB_VERSION_BDEV 1
  113. #define BCACHE_SB_VERSION_CDEV_WITH_UUID 3
  114. #define BCACHE_SB_VERSION_BDEV_WITH_OFFSET 4
  115. #define BCACHE_SB_MAX_VERSION 4
  116. #define SB_SECTOR 8
  117. #define SB_SIZE 4096
  118. #define SB_LABEL_SIZE 32
  119. #define SB_JOURNAL_BUCKETS 256U
  120. /* SB_JOURNAL_BUCKETS must be divisible by BITS_PER_LONG */
  121. #define MAX_CACHES_PER_SET 8
  122. #define BDEV_DATA_START_DEFAULT 16 /* sectors */
  123. struct cache_sb {
  124. __u64 csum;
  125. __u64 offset; /* sector where this sb was written */
  126. __u64 version;
  127. __u8 magic[16];
  128. __u8 uuid[16];
  129. union {
  130. __u8 set_uuid[16];
  131. __u64 set_magic;
  132. };
  133. __u8 label[SB_LABEL_SIZE];
  134. __u64 flags;
  135. __u64 seq;
  136. __u64 pad[8];
  137. union {
  138. struct {
  139. /* Cache devices */
  140. __u64 nbuckets; /* device size */
  141. __u16 block_size; /* sectors */
  142. __u16 bucket_size; /* sectors */
  143. __u16 nr_in_set;
  144. __u16 nr_this_dev;
  145. };
  146. struct {
  147. /* Backing devices */
  148. __u64 data_offset;
  149. /*
  150. * block_size from the cache device section is still used by
  151. * backing devices, so don't add anything here until we fix
  152. * things to not need it for backing devices anymore
  153. */
  154. };
  155. };
  156. __u32 last_mount; /* time overflow in y2106 */
  157. __u16 first_bucket;
  158. union {
  159. __u16 njournal_buckets;
  160. __u16 keys;
  161. };
  162. __u64 d[SB_JOURNAL_BUCKETS]; /* journal buckets */
  163. };
  164. static inline _Bool SB_IS_BDEV(const struct cache_sb *sb)
  165. {
  166. return sb->version == BCACHE_SB_VERSION_BDEV
  167. || sb->version == BCACHE_SB_VERSION_BDEV_WITH_OFFSET;
  168. }
  169. BITMASK(CACHE_SYNC, struct cache_sb, flags, 0, 1);
  170. BITMASK(CACHE_DISCARD, struct cache_sb, flags, 1, 1);
  171. BITMASK(CACHE_REPLACEMENT, struct cache_sb, flags, 2, 3);
  172. #define CACHE_REPLACEMENT_LRU 0U
  173. #define CACHE_REPLACEMENT_FIFO 1U
  174. #define CACHE_REPLACEMENT_RANDOM 2U
  175. BITMASK(BDEV_CACHE_MODE, struct cache_sb, flags, 0, 4);
  176. #define CACHE_MODE_WRITETHROUGH 0U
  177. #define CACHE_MODE_WRITEBACK 1U
  178. #define CACHE_MODE_WRITEAROUND 2U
  179. #define CACHE_MODE_NONE 3U
  180. BITMASK(BDEV_STATE, struct cache_sb, flags, 61, 2);
  181. #define BDEV_STATE_NONE 0U
  182. #define BDEV_STATE_CLEAN 1U
  183. #define BDEV_STATE_DIRTY 2U
  184. #define BDEV_STATE_STALE 3U
  185. /*
  186. * Magic numbers
  187. *
  188. * The various other data structures have their own magic numbers, which are
  189. * xored with the first part of the cache set's UUID
  190. */
  191. #define JSET_MAGIC 0x245235c1a3625032ULL
  192. #define PSET_MAGIC 0x6750e15f87337f91ULL
  193. #define BSET_MAGIC 0x90135c78b99e07f5ULL
  194. static inline __u64 jset_magic(struct cache_sb *sb)
  195. {
  196. return sb->set_magic ^ JSET_MAGIC;
  197. }
  198. static inline __u64 pset_magic(struct cache_sb *sb)
  199. {
  200. return sb->set_magic ^ PSET_MAGIC;
  201. }
  202. static inline __u64 bset_magic(struct cache_sb *sb)
  203. {
  204. return sb->set_magic ^ BSET_MAGIC;
  205. }
  206. /*
  207. * Journal
  208. *
  209. * On disk format for a journal entry:
  210. * seq is monotonically increasing; every journal entry has its own unique
  211. * sequence number.
  212. *
  213. * last_seq is the oldest journal entry that still has keys the btree hasn't
  214. * flushed to disk yet.
  215. *
  216. * version is for on disk format changes.
  217. */
  218. #define BCACHE_JSET_VERSION_UUIDv1 1
  219. #define BCACHE_JSET_VERSION_UUID 1 /* Always latest UUID format */
  220. #define BCACHE_JSET_VERSION 1
  221. struct jset {
  222. __u64 csum;
  223. __u64 magic;
  224. __u64 seq;
  225. __u32 version;
  226. __u32 keys;
  227. __u64 last_seq;
  228. BKEY_PADDED(uuid_bucket);
  229. BKEY_PADDED(btree_root);
  230. __u16 btree_level;
  231. __u16 pad[3];
  232. __u64 prio_bucket[MAX_CACHES_PER_SET];
  233. union {
  234. struct bkey start[0];
  235. __u64 d[0];
  236. };
  237. };
  238. /* Bucket prios/gens */
  239. struct prio_set {
  240. __u64 csum;
  241. __u64 magic;
  242. __u64 seq;
  243. __u32 version;
  244. __u32 pad;
  245. __u64 next_bucket;
  246. struct bucket_disk {
  247. __u16 prio;
  248. __u8 gen;
  249. } __attribute((packed)) data[];
  250. };
  251. /* UUIDS - per backing device/flash only volume metadata */
  252. struct uuid_entry {
  253. union {
  254. struct {
  255. __u8 uuid[16];
  256. __u8 label[32];
  257. __u32 first_reg; /* time overflow in y2106 */
  258. __u32 last_reg;
  259. __u32 invalidated;
  260. __u32 flags;
  261. /* Size of flash only volumes */
  262. __u64 sectors;
  263. };
  264. __u8 pad[128];
  265. };
  266. };
  267. BITMASK(UUID_FLASH_ONLY, struct uuid_entry, flags, 0, 1);
  268. /* Btree nodes */
  269. /* Version 1: Seed pointer into btree node checksum
  270. */
  271. #define BCACHE_BSET_CSUM 1
  272. #define BCACHE_BSET_VERSION 1
  273. /*
  274. * Btree nodes
  275. *
  276. * On disk a btree node is a list/log of these; within each set the keys are
  277. * sorted
  278. */
  279. struct bset {
  280. __u64 csum;
  281. __u64 magic;
  282. __u64 seq;
  283. __u32 version;
  284. __u32 keys;
  285. union {
  286. struct bkey start[0];
  287. __u64 d[0];
  288. };
  289. };
  290. /* OBSOLETE */
  291. /* UUIDS - per backing device/flash only volume metadata */
  292. struct uuid_entry_v0 {
  293. __u8 uuid[16];
  294. __u8 label[32];
  295. __u32 first_reg;
  296. __u32 last_reg;
  297. __u32 invalidated;
  298. __u32 pad;
  299. };
  300. #endif /* _LINUX_BCACHE_H */