extents_format.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _BCACHEFS_EXTENTS_FORMAT_H
  3. #define _BCACHEFS_EXTENTS_FORMAT_H
  4. /*
  5. * In extent bkeys, the value is a list of pointers (bch_extent_ptr), optionally
  6. * preceded by checksum/compression information (bch_extent_crc32 or
  7. * bch_extent_crc64).
  8. *
  9. * One major determining factor in the format of extents is how we handle and
  10. * represent extents that have been partially overwritten and thus trimmed:
  11. *
  12. * If an extent is not checksummed or compressed, when the extent is trimmed we
  13. * don't have to remember the extent we originally allocated and wrote: we can
  14. * merely adjust ptr->offset to point to the start of the data that is currently
  15. * live. The size field in struct bkey records the current (live) size of the
  16. * extent, and is also used to mean "size of region on disk that we point to" in
  17. * this case.
  18. *
  19. * Thus an extent that is not checksummed or compressed will consist only of a
  20. * list of bch_extent_ptrs, with none of the fields in
  21. * bch_extent_crc32/bch_extent_crc64.
  22. *
  23. * When an extent is checksummed or compressed, it's not possible to read only
  24. * the data that is currently live: we have to read the entire extent that was
  25. * originally written, and then return only the part of the extent that is
  26. * currently live.
  27. *
  28. * Thus, in addition to the current size of the extent in struct bkey, we need
  29. * to store the size of the originally allocated space - this is the
  30. * compressed_size and uncompressed_size fields in bch_extent_crc32/64. Also,
  31. * when the extent is trimmed, instead of modifying the offset field of the
  32. * pointer, we keep a second smaller offset field - "offset into the original
  33. * extent of the currently live region".
  34. *
  35. * The other major determining factor is replication and data migration:
  36. *
  37. * Each pointer may have its own bch_extent_crc32/64. When doing a replicated
  38. * write, we will initially write all the replicas in the same format, with the
  39. * same checksum type and compression format - however, when copygc runs later (or
  40. * tiering/cache promotion, anything that moves data), it is not in general
  41. * going to rewrite all the pointers at once - one of the replicas may be in a
  42. * bucket on one device that has very little fragmentation while another lives
  43. * in a bucket that has become heavily fragmented, and thus is being rewritten
  44. * sooner than the rest.
  45. *
  46. * Thus it will only move a subset of the pointers (or in the case of
  47. * tiering/cache promotion perhaps add a single pointer without dropping any
  48. * current pointers), and if the extent has been partially overwritten it must
  49. * write only the currently live portion (or copygc would not be able to reduce
  50. * fragmentation!) - which necessitates a different bch_extent_crc format for
  51. * the new pointer.
  52. *
  53. * But in the interests of space efficiency, we don't want to store one
  54. * bch_extent_crc for each pointer if we don't have to.
  55. *
  56. * Thus, a bch_extent consists of bch_extent_crc32s, bch_extent_crc64s, and
  57. * bch_extent_ptrs appended arbitrarily one after the other. We determine the
  58. * type of a given entry with a scheme similar to utf8 (except we're encoding a
  59. * type, not a size), encoding the type in the position of the first set bit:
  60. *
  61. * bch_extent_crc32 - 0b1
  62. * bch_extent_ptr - 0b10
  63. * bch_extent_crc64 - 0b100
  64. *
  65. * We do it this way because bch_extent_crc32 is _very_ constrained on bits (and
  66. * bch_extent_crc64 is the least constrained).
  67. *
  68. * Then, each bch_extent_crc32/64 applies to the pointers that follow after it,
  69. * until the next bch_extent_crc32/64.
  70. *
  71. * If there are no bch_extent_crcs preceding a bch_extent_ptr, then that pointer
  72. * is neither checksummed nor compressed.
  73. */
  74. #define BCH_EXTENT_ENTRY_TYPES() \
  75. x(ptr, 0) \
  76. x(crc32, 1) \
  77. x(crc64, 2) \
  78. x(crc128, 3) \
  79. x(stripe_ptr, 4) \
  80. x(rebalance, 5)
  81. #define BCH_EXTENT_ENTRY_MAX 6
  82. enum bch_extent_entry_type {
  83. #define x(f, n) BCH_EXTENT_ENTRY_##f = n,
  84. BCH_EXTENT_ENTRY_TYPES()
  85. #undef x
  86. };
  87. /* Compressed/uncompressed size are stored biased by 1: */
  88. struct bch_extent_crc32 {
  89. #if defined(__LITTLE_ENDIAN_BITFIELD)
  90. __u32 type:2,
  91. _compressed_size:7,
  92. _uncompressed_size:7,
  93. offset:7,
  94. _unused:1,
  95. csum_type:4,
  96. compression_type:4;
  97. __u32 csum;
  98. #elif defined (__BIG_ENDIAN_BITFIELD)
  99. __u32 csum;
  100. __u32 compression_type:4,
  101. csum_type:4,
  102. _unused:1,
  103. offset:7,
  104. _uncompressed_size:7,
  105. _compressed_size:7,
  106. type:2;
  107. #endif
  108. } __packed __aligned(8);
  109. #define CRC32_SIZE_MAX (1U << 7)
  110. #define CRC32_NONCE_MAX 0
  111. struct bch_extent_crc64 {
  112. #if defined(__LITTLE_ENDIAN_BITFIELD)
  113. __u64 type:3,
  114. _compressed_size:9,
  115. _uncompressed_size:9,
  116. offset:9,
  117. nonce:10,
  118. csum_type:4,
  119. compression_type:4,
  120. csum_hi:16;
  121. #elif defined (__BIG_ENDIAN_BITFIELD)
  122. __u64 csum_hi:16,
  123. compression_type:4,
  124. csum_type:4,
  125. nonce:10,
  126. offset:9,
  127. _uncompressed_size:9,
  128. _compressed_size:9,
  129. type:3;
  130. #endif
  131. __u64 csum_lo;
  132. } __packed __aligned(8);
  133. #define CRC64_SIZE_MAX (1U << 9)
  134. #define CRC64_NONCE_MAX ((1U << 10) - 1)
  135. struct bch_extent_crc128 {
  136. #if defined(__LITTLE_ENDIAN_BITFIELD)
  137. __u64 type:4,
  138. _compressed_size:13,
  139. _uncompressed_size:13,
  140. offset:13,
  141. nonce:13,
  142. csum_type:4,
  143. compression_type:4;
  144. #elif defined (__BIG_ENDIAN_BITFIELD)
  145. __u64 compression_type:4,
  146. csum_type:4,
  147. nonce:13,
  148. offset:13,
  149. _uncompressed_size:13,
  150. _compressed_size:13,
  151. type:4;
  152. #endif
  153. struct bch_csum csum;
  154. } __packed __aligned(8);
  155. #define CRC128_SIZE_MAX (1U << 13)
  156. #define CRC128_NONCE_MAX ((1U << 13) - 1)
  157. /*
  158. * @reservation - pointer hasn't been written to, just reserved
  159. */
  160. struct bch_extent_ptr {
  161. #if defined(__LITTLE_ENDIAN_BITFIELD)
  162. __u64 type:1,
  163. cached:1,
  164. unused:1,
  165. unwritten:1,
  166. offset:44, /* 8 petabytes */
  167. dev:8,
  168. gen:8;
  169. #elif defined (__BIG_ENDIAN_BITFIELD)
  170. __u64 gen:8,
  171. dev:8,
  172. offset:44,
  173. unwritten:1,
  174. unused:1,
  175. cached:1,
  176. type:1;
  177. #endif
  178. } __packed __aligned(8);
  179. struct bch_extent_stripe_ptr {
  180. #if defined(__LITTLE_ENDIAN_BITFIELD)
  181. __u64 type:5,
  182. block:8,
  183. redundancy:4,
  184. idx:47;
  185. #elif defined (__BIG_ENDIAN_BITFIELD)
  186. __u64 idx:47,
  187. redundancy:4,
  188. block:8,
  189. type:5;
  190. #endif
  191. };
  192. struct bch_extent_rebalance {
  193. #if defined(__LITTLE_ENDIAN_BITFIELD)
  194. __u64 type:6,
  195. unused:34,
  196. compression:8, /* enum bch_compression_opt */
  197. target:16;
  198. #elif defined (__BIG_ENDIAN_BITFIELD)
  199. __u64 target:16,
  200. compression:8,
  201. unused:34,
  202. type:6;
  203. #endif
  204. };
  205. union bch_extent_entry {
  206. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ || __BITS_PER_LONG == 64
  207. unsigned long type;
  208. #elif __BITS_PER_LONG == 32
  209. struct {
  210. unsigned long pad;
  211. unsigned long type;
  212. };
  213. #else
  214. #error edit for your odd byteorder.
  215. #endif
  216. #define x(f, n) struct bch_extent_##f f;
  217. BCH_EXTENT_ENTRY_TYPES()
  218. #undef x
  219. };
  220. struct bch_btree_ptr {
  221. struct bch_val v;
  222. __u64 _data[0];
  223. struct bch_extent_ptr start[];
  224. } __packed __aligned(8);
  225. struct bch_btree_ptr_v2 {
  226. struct bch_val v;
  227. __u64 mem_ptr;
  228. __le64 seq;
  229. __le16 sectors_written;
  230. __le16 flags;
  231. struct bpos min_key;
  232. __u64 _data[0];
  233. struct bch_extent_ptr start[];
  234. } __packed __aligned(8);
  235. LE16_BITMASK(BTREE_PTR_RANGE_UPDATED, struct bch_btree_ptr_v2, flags, 0, 1);
  236. struct bch_extent {
  237. struct bch_val v;
  238. __u64 _data[0];
  239. union bch_extent_entry start[];
  240. } __packed __aligned(8);
  241. /* Maximum size (in u64s) a single pointer could be: */
  242. #define BKEY_EXTENT_PTR_U64s_MAX\
  243. ((sizeof(struct bch_extent_crc128) + \
  244. sizeof(struct bch_extent_ptr)) / sizeof(__u64))
  245. /* Maximum possible size of an entire extent value: */
  246. #define BKEY_EXTENT_VAL_U64s_MAX \
  247. (1 + BKEY_EXTENT_PTR_U64s_MAX * (BCH_REPLICAS_MAX + 1))
  248. /* * Maximum possible size of an entire extent, key + value: */
  249. #define BKEY_EXTENT_U64s_MAX (BKEY_U64s + BKEY_EXTENT_VAL_U64s_MAX)
  250. /* Btree pointers don't carry around checksums: */
  251. #define BKEY_BTREE_PTR_VAL_U64s_MAX \
  252. ((sizeof(struct bch_btree_ptr_v2) + \
  253. sizeof(struct bch_extent_ptr) * BCH_REPLICAS_MAX) / sizeof(__u64))
  254. #define BKEY_BTREE_PTR_U64s_MAX \
  255. (BKEY_U64s + BKEY_BTREE_PTR_VAL_U64s_MAX)
  256. struct bch_reservation {
  257. struct bch_val v;
  258. __le32 generation;
  259. __u8 nr_replicas;
  260. __u8 pad[3];
  261. } __packed __aligned(8);
  262. struct bch_inline_data {
  263. struct bch_val v;
  264. u8 data[];
  265. };
  266. #endif /* _BCACHEFS_EXTENTS_FORMAT_H */