bcachefs_format.h 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _BCACHEFS_FORMAT_H
  3. #define _BCACHEFS_FORMAT_H
  4. /*
  5. * bcachefs on disk data structures
  6. *
  7. * OVERVIEW:
  8. *
  9. * There are three main types of on disk data structures in bcachefs (this is
  10. * reduced from 5 in bcache)
  11. *
  12. * - superblock
  13. * - journal
  14. * - btree
  15. *
  16. * The btree is the primary structure; most metadata exists as keys in the
  17. * various btrees. There are only a small number of btrees, they're not
  18. * sharded - we have one btree for extents, another for inodes, et cetera.
  19. *
  20. * SUPERBLOCK:
  21. *
  22. * The superblock contains the location of the journal, the list of devices in
  23. * the filesystem, and in general any metadata we need in order to decide
  24. * whether we can start a filesystem or prior to reading the journal/btree
  25. * roots.
  26. *
  27. * The superblock is extensible, and most of the contents of the superblock are
  28. * in variable length, type tagged fields; see struct bch_sb_field.
  29. *
  30. * Backup superblocks do not reside in a fixed location; also, superblocks do
  31. * not have a fixed size. To locate backup superblocks we have struct
  32. * bch_sb_layout; we store a copy of this inside every superblock, and also
  33. * before the first superblock.
  34. *
  35. * JOURNAL:
  36. *
  37. * The journal primarily records btree updates in the order they occurred;
  38. * journal replay consists of just iterating over all the keys in the open
  39. * journal entries and re-inserting them into the btrees.
  40. *
  41. * The journal also contains entry types for the btree roots, and blacklisted
  42. * journal sequence numbers (see journal_seq_blacklist.c).
  43. *
  44. * BTREE:
  45. *
  46. * bcachefs btrees are copy on write b+ trees, where nodes are big (typically
  47. * 128k-256k) and log structured. We use struct btree_node for writing the first
  48. * entry in a given node (offset 0), and struct btree_node_entry for all
  49. * subsequent writes.
  50. *
  51. * After the header, btree node entries contain a list of keys in sorted order.
  52. * Values are stored inline with the keys; since values are variable length (and
  53. * keys effectively are variable length too, due to packing) we can't do random
  54. * access without building up additional in memory tables in the btree node read
  55. * path.
  56. *
  57. * BTREE KEYS (struct bkey):
  58. *
  59. * The various btrees share a common format for the key - so as to avoid
  60. * switching in fastpath lookup/comparison code - but define their own
  61. * structures for the key values.
  62. *
  63. * The size of a key/value pair is stored as a u8 in units of u64s, so the max
  64. * size is just under 2k. The common part also contains a type tag for the
  65. * value, and a format field indicating whether the key is packed or not (and
  66. * also meant to allow adding new key fields in the future, if desired).
  67. *
  68. * bkeys, when stored within a btree node, may also be packed. In that case, the
  69. * bkey_format in that node is used to unpack it. Packed bkeys mean that we can
  70. * be generous with field sizes in the common part of the key format (64 bit
  71. * inode number, 64 bit offset, 96 bit version field, etc.) for negligible cost.
  72. */
  73. #include <asm/types.h>
  74. #include <asm/byteorder.h>
  75. #include <linux/kernel.h>
  76. #include <linux/uuid.h>
  77. #include <uapi/linux/magic.h>
  78. #include "vstructs.h"
  79. #ifdef __KERNEL__
  80. typedef uuid_t __uuid_t;
  81. #endif
  82. #define BITMASK(name, type, field, offset, end) \
  83. static const __maybe_unused unsigned name##_OFFSET = offset; \
  84. static const __maybe_unused unsigned name##_BITS = (end - offset); \
  85. \
  86. static inline __u64 name(const type *k) \
  87. { \
  88. return (k->field >> offset) & ~(~0ULL << (end - offset)); \
  89. } \
  90. \
  91. static inline void SET_##name(type *k, __u64 v) \
  92. { \
  93. k->field &= ~(~(~0ULL << (end - offset)) << offset); \
  94. k->field |= (v & ~(~0ULL << (end - offset))) << offset; \
  95. }
  96. #define LE_BITMASK(_bits, name, type, field, offset, end) \
  97. static const __maybe_unused unsigned name##_OFFSET = offset; \
  98. static const __maybe_unused unsigned name##_BITS = (end - offset); \
  99. static const __maybe_unused __u##_bits name##_MAX = (1ULL << (end - offset)) - 1;\
  100. \
  101. static inline __u64 name(const type *k) \
  102. { \
  103. return (__le##_bits##_to_cpu(k->field) >> offset) & \
  104. ~(~0ULL << (end - offset)); \
  105. } \
  106. \
  107. static inline void SET_##name(type *k, __u64 v) \
  108. { \
  109. __u##_bits new = __le##_bits##_to_cpu(k->field); \
  110. \
  111. new &= ~(~(~0ULL << (end - offset)) << offset); \
  112. new |= (v & ~(~0ULL << (end - offset))) << offset; \
  113. k->field = __cpu_to_le##_bits(new); \
  114. }
  115. #define LE16_BITMASK(n, t, f, o, e) LE_BITMASK(16, n, t, f, o, e)
  116. #define LE32_BITMASK(n, t, f, o, e) LE_BITMASK(32, n, t, f, o, e)
  117. #define LE64_BITMASK(n, t, f, o, e) LE_BITMASK(64, n, t, f, o, e)
  118. struct bkey_format {
  119. __u8 key_u64s;
  120. __u8 nr_fields;
  121. /* One unused slot for now: */
  122. __u8 bits_per_field[6];
  123. __le64 field_offset[6];
  124. };
  125. /* Btree keys - all units are in sectors */
  126. struct bpos {
  127. /*
  128. * Word order matches machine byte order - btree code treats a bpos as a
  129. * single large integer, for search/comparison purposes
  130. *
  131. * Note that wherever a bpos is embedded in another on disk data
  132. * structure, it has to be byte swabbed when reading in metadata that
  133. * wasn't written in native endian order:
  134. */
  135. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  136. __u32 snapshot;
  137. __u64 offset;
  138. __u64 inode;
  139. #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  140. __u64 inode;
  141. __u64 offset; /* Points to end of extent - sectors */
  142. __u32 snapshot;
  143. #else
  144. #error edit for your odd byteorder.
  145. #endif
  146. } __packed
  147. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  148. __aligned(4)
  149. #endif
  150. ;
  151. #define KEY_INODE_MAX ((__u64)~0ULL)
  152. #define KEY_OFFSET_MAX ((__u64)~0ULL)
  153. #define KEY_SNAPSHOT_MAX ((__u32)~0U)
  154. #define KEY_SIZE_MAX ((__u32)~0U)
  155. static inline struct bpos SPOS(__u64 inode, __u64 offset, __u32 snapshot)
  156. {
  157. return (struct bpos) {
  158. .inode = inode,
  159. .offset = offset,
  160. .snapshot = snapshot,
  161. };
  162. }
  163. #define POS_MIN SPOS(0, 0, 0)
  164. #define POS_MAX SPOS(KEY_INODE_MAX, KEY_OFFSET_MAX, 0)
  165. #define SPOS_MAX SPOS(KEY_INODE_MAX, KEY_OFFSET_MAX, KEY_SNAPSHOT_MAX)
  166. #define POS(_inode, _offset) SPOS(_inode, _offset, 0)
  167. /* Empty placeholder struct, for container_of() */
  168. struct bch_val {
  169. __u64 __nothing[0];
  170. };
  171. struct bversion {
  172. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  173. __u64 lo;
  174. __u32 hi;
  175. #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  176. __u32 hi;
  177. __u64 lo;
  178. #endif
  179. } __packed
  180. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  181. __aligned(4)
  182. #endif
  183. ;
  184. struct bkey {
  185. /* Size of combined key and value, in u64s */
  186. __u8 u64s;
  187. /* Format of key (0 for format local to btree node) */
  188. #if defined(__LITTLE_ENDIAN_BITFIELD)
  189. __u8 format:7,
  190. needs_whiteout:1;
  191. #elif defined (__BIG_ENDIAN_BITFIELD)
  192. __u8 needs_whiteout:1,
  193. format:7;
  194. #else
  195. #error edit for your odd byteorder.
  196. #endif
  197. /* Type of the value */
  198. __u8 type;
  199. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  200. __u8 pad[1];
  201. struct bversion bversion;
  202. __u32 size; /* extent size, in sectors */
  203. struct bpos p;
  204. #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  205. struct bpos p;
  206. __u32 size; /* extent size, in sectors */
  207. struct bversion bversion;
  208. __u8 pad[1];
  209. #endif
  210. } __packed
  211. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  212. /*
  213. * The big-endian version of bkey can't be compiled by rustc with the "aligned"
  214. * attr since it doesn't allow types to have both "packed" and "aligned" attrs.
  215. * So for Rust compatibility, don't include this. It can be included in the LE
  216. * version because the "packed" attr is redundant in that case.
  217. *
  218. * History: (quoting Kent)
  219. *
  220. * Specifically, when i was designing bkey, I wanted the header to be no
  221. * bigger than necessary so that bkey_packed could use the rest. That means that
  222. * decently offten extent keys will fit into only 8 bytes, instead of spilling over
  223. * to 16.
  224. *
  225. * But packed_bkey treats the part after the header - the packed section -
  226. * as a single multi word, variable length integer. And bkey, the unpacked
  227. * version, is just a special case version of a bkey_packed; all the packed
  228. * bkey code will work on keys in any packed format, the in-memory
  229. * representation of an unpacked key also is just one type of packed key...
  230. *
  231. * So that constrains the key part of a bkig endian bkey to start right
  232. * after the header.
  233. *
  234. * If we ever do a bkey_v2 and need to expand the hedaer by another byte for
  235. * some reason - that will clean up this wart.
  236. */
  237. __aligned(8)
  238. #endif
  239. ;
  240. struct bkey_packed {
  241. __u64 _data[0];
  242. /* Size of combined key and value, in u64s */
  243. __u8 u64s;
  244. /* Format of key (0 for format local to btree node) */
  245. /*
  246. * XXX: next incompat on disk format change, switch format and
  247. * needs_whiteout - bkey_packed() will be cheaper if format is the high
  248. * bits of the bitfield
  249. */
  250. #if defined(__LITTLE_ENDIAN_BITFIELD)
  251. __u8 format:7,
  252. needs_whiteout:1;
  253. #elif defined (__BIG_ENDIAN_BITFIELD)
  254. __u8 needs_whiteout:1,
  255. format:7;
  256. #endif
  257. /* Type of the value */
  258. __u8 type;
  259. __u8 key_start[0];
  260. /*
  261. * We copy bkeys with struct assignment in various places, and while
  262. * that shouldn't be done with packed bkeys we can't disallow it in C,
  263. * and it's legal to cast a bkey to a bkey_packed - so padding it out
  264. * to the same size as struct bkey should hopefully be safest.
  265. */
  266. __u8 pad[sizeof(struct bkey) - 3];
  267. } __packed __aligned(8);
  268. typedef struct {
  269. __le64 lo;
  270. __le64 hi;
  271. } bch_le128;
  272. #define BKEY_U64s (sizeof(struct bkey) / sizeof(__u64))
  273. #define BKEY_U64s_MAX U8_MAX
  274. #define BKEY_VAL_U64s_MAX (BKEY_U64s_MAX - BKEY_U64s)
  275. #define KEY_PACKED_BITS_START 24
  276. #define KEY_FORMAT_LOCAL_BTREE 0
  277. #define KEY_FORMAT_CURRENT 1
  278. enum bch_bkey_fields {
  279. BKEY_FIELD_INODE,
  280. BKEY_FIELD_OFFSET,
  281. BKEY_FIELD_SNAPSHOT,
  282. BKEY_FIELD_SIZE,
  283. BKEY_FIELD_VERSION_HI,
  284. BKEY_FIELD_VERSION_LO,
  285. BKEY_NR_FIELDS,
  286. };
  287. #define bkey_format_field(name, field) \
  288. [BKEY_FIELD_##name] = (sizeof(((struct bkey *) NULL)->field) * 8)
  289. #define BKEY_FORMAT_CURRENT \
  290. ((struct bkey_format) { \
  291. .key_u64s = BKEY_U64s, \
  292. .nr_fields = BKEY_NR_FIELDS, \
  293. .bits_per_field = { \
  294. bkey_format_field(INODE, p.inode), \
  295. bkey_format_field(OFFSET, p.offset), \
  296. bkey_format_field(SNAPSHOT, p.snapshot), \
  297. bkey_format_field(SIZE, size), \
  298. bkey_format_field(VERSION_HI, bversion.hi), \
  299. bkey_format_field(VERSION_LO, bversion.lo), \
  300. }, \
  301. })
  302. /* bkey with inline value */
  303. struct bkey_i {
  304. __u64 _data[0];
  305. struct bkey k;
  306. struct bch_val v;
  307. };
  308. #define POS_KEY(_pos) \
  309. ((struct bkey) { \
  310. .u64s = BKEY_U64s, \
  311. .format = KEY_FORMAT_CURRENT, \
  312. .p = _pos, \
  313. })
  314. #define KEY(_inode, _offset, _size) \
  315. ((struct bkey) { \
  316. .u64s = BKEY_U64s, \
  317. .format = KEY_FORMAT_CURRENT, \
  318. .p = POS(_inode, _offset), \
  319. .size = _size, \
  320. })
  321. static inline void bkey_init(struct bkey *k)
  322. {
  323. *k = KEY(0, 0, 0);
  324. }
  325. #define bkey_bytes(_k) ((_k)->u64s * sizeof(__u64))
  326. #define __BKEY_PADDED(key, pad) \
  327. struct bkey_i key; __u64 key ## _pad[pad]
  328. /*
  329. * - DELETED keys are used internally to mark keys that should be ignored but
  330. * override keys in composition order. Their version number is ignored.
  331. *
  332. * - DISCARDED keys indicate that the data is all 0s because it has been
  333. * discarded. DISCARDs may have a version; if the version is nonzero the key
  334. * will be persistent, otherwise the key will be dropped whenever the btree
  335. * node is rewritten (like DELETED keys).
  336. *
  337. * - ERROR: any read of the data returns a read error, as the data was lost due
  338. * to a failing device. Like DISCARDED keys, they can be removed (overridden)
  339. * by new writes or cluster-wide GC. Node repair can also overwrite them with
  340. * the same or a more recent version number, but not with an older version
  341. * number.
  342. *
  343. * - WHITEOUT: for hash table btrees
  344. */
  345. #define BCH_BKEY_TYPES() \
  346. x(deleted, 0) \
  347. x(whiteout, 1) \
  348. x(error, 2) \
  349. x(cookie, 3) \
  350. x(hash_whiteout, 4) \
  351. x(btree_ptr, 5) \
  352. x(extent, 6) \
  353. x(reservation, 7) \
  354. x(inode, 8) \
  355. x(inode_generation, 9) \
  356. x(dirent, 10) \
  357. x(xattr, 11) \
  358. x(alloc, 12) \
  359. x(quota, 13) \
  360. x(stripe, 14) \
  361. x(reflink_p, 15) \
  362. x(reflink_v, 16) \
  363. x(inline_data, 17) \
  364. x(btree_ptr_v2, 18) \
  365. x(indirect_inline_data, 19) \
  366. x(alloc_v2, 20) \
  367. x(subvolume, 21) \
  368. x(snapshot, 22) \
  369. x(inode_v2, 23) \
  370. x(alloc_v3, 24) \
  371. x(set, 25) \
  372. x(lru, 26) \
  373. x(alloc_v4, 27) \
  374. x(backpointer, 28) \
  375. x(inode_v3, 29) \
  376. x(bucket_gens, 30) \
  377. x(snapshot_tree, 31) \
  378. x(logged_op_truncate, 32) \
  379. x(logged_op_finsert, 33) \
  380. x(accounting, 34)
  381. enum bch_bkey_type {
  382. #define x(name, nr) KEY_TYPE_##name = nr,
  383. BCH_BKEY_TYPES()
  384. #undef x
  385. KEY_TYPE_MAX,
  386. };
  387. struct bch_deleted {
  388. struct bch_val v;
  389. };
  390. struct bch_whiteout {
  391. struct bch_val v;
  392. };
  393. struct bch_error {
  394. struct bch_val v;
  395. };
  396. struct bch_cookie {
  397. struct bch_val v;
  398. __le64 cookie;
  399. };
  400. struct bch_hash_whiteout {
  401. struct bch_val v;
  402. };
  403. struct bch_set {
  404. struct bch_val v;
  405. };
  406. /* 128 bits, sufficient for cryptographic MACs: */
  407. struct bch_csum {
  408. __le64 lo;
  409. __le64 hi;
  410. } __packed __aligned(8);
  411. struct bch_backpointer {
  412. struct bch_val v;
  413. __u8 btree_id;
  414. __u8 level;
  415. __u8 data_type;
  416. __u64 bucket_offset:40;
  417. __u32 bucket_len;
  418. struct bpos pos;
  419. } __packed __aligned(8);
  420. /* Optional/variable size superblock sections: */
  421. struct bch_sb_field {
  422. __u64 _data[0];
  423. __le32 u64s;
  424. __le32 type;
  425. };
  426. #define BCH_SB_FIELDS() \
  427. x(journal, 0) \
  428. x(members_v1, 1) \
  429. x(crypt, 2) \
  430. x(replicas_v0, 3) \
  431. x(quota, 4) \
  432. x(disk_groups, 5) \
  433. x(clean, 6) \
  434. x(replicas, 7) \
  435. x(journal_seq_blacklist, 8) \
  436. x(journal_v2, 9) \
  437. x(counters, 10) \
  438. x(members_v2, 11) \
  439. x(errors, 12) \
  440. x(ext, 13) \
  441. x(downgrade, 14)
  442. #include "alloc_background_format.h"
  443. #include "dirent_format.h"
  444. #include "disk_accounting_format.h"
  445. #include "disk_groups_format.h"
  446. #include "extents_format.h"
  447. #include "ec_format.h"
  448. #include "dirent_format.h"
  449. #include "disk_groups_format.h"
  450. #include "inode_format.h"
  451. #include "journal_seq_blacklist_format.h"
  452. #include "logged_ops_format.h"
  453. #include "lru_format.h"
  454. #include "quota_format.h"
  455. #include "reflink_format.h"
  456. #include "replicas_format.h"
  457. #include "snapshot_format.h"
  458. #include "subvolume_format.h"
  459. #include "sb-counters_format.h"
  460. #include "sb-downgrade_format.h"
  461. #include "sb-errors_format.h"
  462. #include "sb-members_format.h"
  463. #include "xattr_format.h"
  464. enum bch_sb_field_type {
  465. #define x(f, nr) BCH_SB_FIELD_##f = nr,
  466. BCH_SB_FIELDS()
  467. #undef x
  468. BCH_SB_FIELD_NR
  469. };
  470. /*
  471. * Most superblock fields are replicated in all device's superblocks - a few are
  472. * not:
  473. */
  474. #define BCH_SINGLE_DEVICE_SB_FIELDS \
  475. ((1U << BCH_SB_FIELD_journal)| \
  476. (1U << BCH_SB_FIELD_journal_v2))
  477. /* BCH_SB_FIELD_journal: */
  478. struct bch_sb_field_journal {
  479. struct bch_sb_field field;
  480. __le64 buckets[];
  481. };
  482. struct bch_sb_field_journal_v2 {
  483. struct bch_sb_field field;
  484. struct bch_sb_field_journal_v2_entry {
  485. __le64 start;
  486. __le64 nr;
  487. } d[];
  488. };
  489. /* BCH_SB_FIELD_crypt: */
  490. struct nonce {
  491. __le32 d[4];
  492. };
  493. struct bch_key {
  494. __le64 key[4];
  495. };
  496. #define BCH_KEY_MAGIC \
  497. (((__u64) 'b' << 0)|((__u64) 'c' << 8)| \
  498. ((__u64) 'h' << 16)|((__u64) '*' << 24)| \
  499. ((__u64) '*' << 32)|((__u64) 'k' << 40)| \
  500. ((__u64) 'e' << 48)|((__u64) 'y' << 56))
  501. struct bch_encrypted_key {
  502. __le64 magic;
  503. struct bch_key key;
  504. };
  505. /*
  506. * If this field is present in the superblock, it stores an encryption key which
  507. * is used encrypt all other data/metadata. The key will normally be encrypted
  508. * with the key userspace provides, but if encryption has been turned off we'll
  509. * just store the master key unencrypted in the superblock so we can access the
  510. * previously encrypted data.
  511. */
  512. struct bch_sb_field_crypt {
  513. struct bch_sb_field field;
  514. __le64 flags;
  515. __le64 kdf_flags;
  516. struct bch_encrypted_key key;
  517. };
  518. LE64_BITMASK(BCH_CRYPT_KDF_TYPE, struct bch_sb_field_crypt, flags, 0, 4);
  519. enum bch_kdf_types {
  520. BCH_KDF_SCRYPT = 0,
  521. BCH_KDF_NR = 1,
  522. };
  523. /* stored as base 2 log of scrypt params: */
  524. LE64_BITMASK(BCH_KDF_SCRYPT_N, struct bch_sb_field_crypt, kdf_flags, 0, 16);
  525. LE64_BITMASK(BCH_KDF_SCRYPT_R, struct bch_sb_field_crypt, kdf_flags, 16, 32);
  526. LE64_BITMASK(BCH_KDF_SCRYPT_P, struct bch_sb_field_crypt, kdf_flags, 32, 48);
  527. /*
  528. * On clean shutdown, store btree roots and current journal sequence number in
  529. * the superblock:
  530. */
  531. struct jset_entry {
  532. __le16 u64s;
  533. __u8 btree_id;
  534. __u8 level;
  535. __u8 type; /* designates what this jset holds */
  536. __u8 pad[3];
  537. struct bkey_i start[0];
  538. __u64 _data[];
  539. };
  540. struct bch_sb_field_clean {
  541. struct bch_sb_field field;
  542. __le32 flags;
  543. __le16 _read_clock; /* no longer used */
  544. __le16 _write_clock;
  545. __le64 journal_seq;
  546. struct jset_entry start[0];
  547. __u64 _data[];
  548. };
  549. struct bch_sb_field_ext {
  550. struct bch_sb_field field;
  551. __le64 recovery_passes_required[2];
  552. __le64 errors_silent[8];
  553. __le64 btrees_lost_data;
  554. };
  555. /* Superblock: */
  556. /*
  557. * New versioning scheme:
  558. * One common version number for all on disk data structures - superblock, btree
  559. * nodes, journal entries
  560. */
  561. #define BCH_VERSION_MAJOR(_v) ((__u16) ((_v) >> 10))
  562. #define BCH_VERSION_MINOR(_v) ((__u16) ((_v) & ~(~0U << 10)))
  563. #define BCH_VERSION(_major, _minor) (((_major) << 10)|(_minor) << 0)
  564. /*
  565. * field 1: version name
  566. * field 2: BCH_VERSION(major, minor)
  567. * field 3: recovery passess required on upgrade
  568. */
  569. #define BCH_METADATA_VERSIONS() \
  570. x(bkey_renumber, BCH_VERSION(0, 10)) \
  571. x(inode_btree_change, BCH_VERSION(0, 11)) \
  572. x(snapshot, BCH_VERSION(0, 12)) \
  573. x(inode_backpointers, BCH_VERSION(0, 13)) \
  574. x(btree_ptr_sectors_written, BCH_VERSION(0, 14)) \
  575. x(snapshot_2, BCH_VERSION(0, 15)) \
  576. x(reflink_p_fix, BCH_VERSION(0, 16)) \
  577. x(subvol_dirent, BCH_VERSION(0, 17)) \
  578. x(inode_v2, BCH_VERSION(0, 18)) \
  579. x(freespace, BCH_VERSION(0, 19)) \
  580. x(alloc_v4, BCH_VERSION(0, 20)) \
  581. x(new_data_types, BCH_VERSION(0, 21)) \
  582. x(backpointers, BCH_VERSION(0, 22)) \
  583. x(inode_v3, BCH_VERSION(0, 23)) \
  584. x(unwritten_extents, BCH_VERSION(0, 24)) \
  585. x(bucket_gens, BCH_VERSION(0, 25)) \
  586. x(lru_v2, BCH_VERSION(0, 26)) \
  587. x(fragmentation_lru, BCH_VERSION(0, 27)) \
  588. x(no_bps_in_alloc_keys, BCH_VERSION(0, 28)) \
  589. x(snapshot_trees, BCH_VERSION(0, 29)) \
  590. x(major_minor, BCH_VERSION(1, 0)) \
  591. x(snapshot_skiplists, BCH_VERSION(1, 1)) \
  592. x(deleted_inodes, BCH_VERSION(1, 2)) \
  593. x(rebalance_work, BCH_VERSION(1, 3)) \
  594. x(member_seq, BCH_VERSION(1, 4)) \
  595. x(subvolume_fs_parent, BCH_VERSION(1, 5)) \
  596. x(btree_subvolume_children, BCH_VERSION(1, 6)) \
  597. x(mi_btree_bitmap, BCH_VERSION(1, 7)) \
  598. x(bucket_stripe_sectors, BCH_VERSION(1, 8)) \
  599. x(disk_accounting_v2, BCH_VERSION(1, 9)) \
  600. x(disk_accounting_v3, BCH_VERSION(1, 10)) \
  601. x(disk_accounting_inum, BCH_VERSION(1, 11)) \
  602. x(rebalance_work_acct_fix, BCH_VERSION(1, 12)) \
  603. x(inode_has_child_snapshots, BCH_VERSION(1, 13))
  604. enum bcachefs_metadata_version {
  605. bcachefs_metadata_version_min = 9,
  606. #define x(t, n) bcachefs_metadata_version_##t = n,
  607. BCH_METADATA_VERSIONS()
  608. #undef x
  609. bcachefs_metadata_version_max
  610. };
  611. static const __maybe_unused
  612. unsigned bcachefs_metadata_required_upgrade_below = bcachefs_metadata_version_rebalance_work;
  613. #define bcachefs_metadata_version_current (bcachefs_metadata_version_max - 1)
  614. #define BCH_SB_SECTOR 8
  615. #define BCH_SB_LAYOUT_SIZE_BITS_MAX 16 /* 32 MB */
  616. struct bch_sb_layout {
  617. __uuid_t magic; /* bcachefs superblock UUID */
  618. __u8 layout_type;
  619. __u8 sb_max_size_bits; /* base 2 of 512 byte sectors */
  620. __u8 nr_superblocks;
  621. __u8 pad[5];
  622. __le64 sb_offset[61];
  623. } __packed __aligned(8);
  624. #define BCH_SB_LAYOUT_SECTOR 7
  625. /*
  626. * @offset - sector where this sb was written
  627. * @version - on disk format version
  628. * @version_min - Oldest metadata version this filesystem contains; so we can
  629. * safely drop compatibility code and refuse to mount filesystems
  630. * we'd need it for
  631. * @magic - identifies as a bcachefs superblock (BCHFS_MAGIC)
  632. * @seq - incremented each time superblock is written
  633. * @uuid - used for generating various magic numbers and identifying
  634. * member devices, never changes
  635. * @user_uuid - user visible UUID, may be changed
  636. * @label - filesystem label
  637. * @seq - identifies most recent superblock, incremented each time
  638. * superblock is written
  639. * @features - enabled incompatible features
  640. */
  641. struct bch_sb {
  642. struct bch_csum csum;
  643. __le16 version;
  644. __le16 version_min;
  645. __le16 pad[2];
  646. __uuid_t magic;
  647. __uuid_t uuid;
  648. __uuid_t user_uuid;
  649. __u8 label[BCH_SB_LABEL_SIZE];
  650. __le64 offset;
  651. __le64 seq;
  652. __le16 block_size;
  653. __u8 dev_idx;
  654. __u8 nr_devices;
  655. __le32 u64s;
  656. __le64 time_base_lo;
  657. __le32 time_base_hi;
  658. __le32 time_precision;
  659. __le64 flags[7];
  660. __le64 write_time;
  661. __le64 features[2];
  662. __le64 compat[2];
  663. struct bch_sb_layout layout;
  664. struct bch_sb_field start[0];
  665. __le64 _data[];
  666. } __packed __aligned(8);
  667. /*
  668. * Flags:
  669. * BCH_SB_INITALIZED - set on first mount
  670. * BCH_SB_CLEAN - did we shut down cleanly? Just a hint, doesn't affect
  671. * behaviour of mount/recovery path:
  672. * BCH_SB_INODE_32BIT - limit inode numbers to 32 bits
  673. * BCH_SB_128_BIT_MACS - 128 bit macs instead of 80
  674. * BCH_SB_ENCRYPTION_TYPE - if nonzero encryption is enabled; overrides
  675. * DATA/META_CSUM_TYPE. Also indicates encryption
  676. * algorithm in use, if/when we get more than one
  677. */
  678. LE16_BITMASK(BCH_SB_BLOCK_SIZE, struct bch_sb, block_size, 0, 16);
  679. LE64_BITMASK(BCH_SB_INITIALIZED, struct bch_sb, flags[0], 0, 1);
  680. LE64_BITMASK(BCH_SB_CLEAN, struct bch_sb, flags[0], 1, 2);
  681. LE64_BITMASK(BCH_SB_CSUM_TYPE, struct bch_sb, flags[0], 2, 8);
  682. LE64_BITMASK(BCH_SB_ERROR_ACTION, struct bch_sb, flags[0], 8, 12);
  683. LE64_BITMASK(BCH_SB_BTREE_NODE_SIZE, struct bch_sb, flags[0], 12, 28);
  684. LE64_BITMASK(BCH_SB_GC_RESERVE, struct bch_sb, flags[0], 28, 33);
  685. LE64_BITMASK(BCH_SB_ROOT_RESERVE, struct bch_sb, flags[0], 33, 40);
  686. LE64_BITMASK(BCH_SB_META_CSUM_TYPE, struct bch_sb, flags[0], 40, 44);
  687. LE64_BITMASK(BCH_SB_DATA_CSUM_TYPE, struct bch_sb, flags[0], 44, 48);
  688. LE64_BITMASK(BCH_SB_META_REPLICAS_WANT, struct bch_sb, flags[0], 48, 52);
  689. LE64_BITMASK(BCH_SB_DATA_REPLICAS_WANT, struct bch_sb, flags[0], 52, 56);
  690. LE64_BITMASK(BCH_SB_POSIX_ACL, struct bch_sb, flags[0], 56, 57);
  691. LE64_BITMASK(BCH_SB_USRQUOTA, struct bch_sb, flags[0], 57, 58);
  692. LE64_BITMASK(BCH_SB_GRPQUOTA, struct bch_sb, flags[0], 58, 59);
  693. LE64_BITMASK(BCH_SB_PRJQUOTA, struct bch_sb, flags[0], 59, 60);
  694. LE64_BITMASK(BCH_SB_HAS_ERRORS, struct bch_sb, flags[0], 60, 61);
  695. LE64_BITMASK(BCH_SB_HAS_TOPOLOGY_ERRORS,struct bch_sb, flags[0], 61, 62);
  696. LE64_BITMASK(BCH_SB_BIG_ENDIAN, struct bch_sb, flags[0], 62, 63);
  697. LE64_BITMASK(BCH_SB_PROMOTE_WHOLE_EXTENTS,
  698. struct bch_sb, flags[0], 63, 64);
  699. LE64_BITMASK(BCH_SB_STR_HASH_TYPE, struct bch_sb, flags[1], 0, 4);
  700. LE64_BITMASK(BCH_SB_COMPRESSION_TYPE_LO,struct bch_sb, flags[1], 4, 8);
  701. LE64_BITMASK(BCH_SB_INODE_32BIT, struct bch_sb, flags[1], 8, 9);
  702. LE64_BITMASK(BCH_SB_128_BIT_MACS, struct bch_sb, flags[1], 9, 10);
  703. LE64_BITMASK(BCH_SB_ENCRYPTION_TYPE, struct bch_sb, flags[1], 10, 14);
  704. /*
  705. * Max size of an extent that may require bouncing to read or write
  706. * (checksummed, compressed): 64k
  707. */
  708. LE64_BITMASK(BCH_SB_ENCODED_EXTENT_MAX_BITS,
  709. struct bch_sb, flags[1], 14, 20);
  710. LE64_BITMASK(BCH_SB_META_REPLICAS_REQ, struct bch_sb, flags[1], 20, 24);
  711. LE64_BITMASK(BCH_SB_DATA_REPLICAS_REQ, struct bch_sb, flags[1], 24, 28);
  712. LE64_BITMASK(BCH_SB_PROMOTE_TARGET, struct bch_sb, flags[1], 28, 40);
  713. LE64_BITMASK(BCH_SB_FOREGROUND_TARGET, struct bch_sb, flags[1], 40, 52);
  714. LE64_BITMASK(BCH_SB_BACKGROUND_TARGET, struct bch_sb, flags[1], 52, 64);
  715. LE64_BITMASK(BCH_SB_BACKGROUND_COMPRESSION_TYPE_LO,
  716. struct bch_sb, flags[2], 0, 4);
  717. LE64_BITMASK(BCH_SB_GC_RESERVE_BYTES, struct bch_sb, flags[2], 4, 64);
  718. LE64_BITMASK(BCH_SB_ERASURE_CODE, struct bch_sb, flags[3], 0, 16);
  719. LE64_BITMASK(BCH_SB_METADATA_TARGET, struct bch_sb, flags[3], 16, 28);
  720. LE64_BITMASK(BCH_SB_SHARD_INUMS, struct bch_sb, flags[3], 28, 29);
  721. LE64_BITMASK(BCH_SB_INODES_USE_KEY_CACHE,struct bch_sb, flags[3], 29, 30);
  722. LE64_BITMASK(BCH_SB_JOURNAL_FLUSH_DELAY,struct bch_sb, flags[3], 30, 62);
  723. LE64_BITMASK(BCH_SB_JOURNAL_FLUSH_DISABLED,struct bch_sb, flags[3], 62, 63);
  724. LE64_BITMASK(BCH_SB_JOURNAL_RECLAIM_DELAY,struct bch_sb, flags[4], 0, 32);
  725. LE64_BITMASK(BCH_SB_JOURNAL_TRANSACTION_NAMES,struct bch_sb, flags[4], 32, 33);
  726. LE64_BITMASK(BCH_SB_NOCOW, struct bch_sb, flags[4], 33, 34);
  727. LE64_BITMASK(BCH_SB_WRITE_BUFFER_SIZE, struct bch_sb, flags[4], 34, 54);
  728. LE64_BITMASK(BCH_SB_VERSION_UPGRADE, struct bch_sb, flags[4], 54, 56);
  729. LE64_BITMASK(BCH_SB_COMPRESSION_TYPE_HI,struct bch_sb, flags[4], 56, 60);
  730. LE64_BITMASK(BCH_SB_BACKGROUND_COMPRESSION_TYPE_HI,
  731. struct bch_sb, flags[4], 60, 64);
  732. LE64_BITMASK(BCH_SB_VERSION_UPGRADE_COMPLETE,
  733. struct bch_sb, flags[5], 0, 16);
  734. LE64_BITMASK(BCH_SB_ALLOCATOR_STUCK_TIMEOUT,
  735. struct bch_sb, flags[5], 16, 32);
  736. static inline __u64 BCH_SB_COMPRESSION_TYPE(const struct bch_sb *sb)
  737. {
  738. return BCH_SB_COMPRESSION_TYPE_LO(sb) | (BCH_SB_COMPRESSION_TYPE_HI(sb) << 4);
  739. }
  740. static inline void SET_BCH_SB_COMPRESSION_TYPE(struct bch_sb *sb, __u64 v)
  741. {
  742. SET_BCH_SB_COMPRESSION_TYPE_LO(sb, v);
  743. SET_BCH_SB_COMPRESSION_TYPE_HI(sb, v >> 4);
  744. }
  745. static inline __u64 BCH_SB_BACKGROUND_COMPRESSION_TYPE(const struct bch_sb *sb)
  746. {
  747. return BCH_SB_BACKGROUND_COMPRESSION_TYPE_LO(sb) |
  748. (BCH_SB_BACKGROUND_COMPRESSION_TYPE_HI(sb) << 4);
  749. }
  750. static inline void SET_BCH_SB_BACKGROUND_COMPRESSION_TYPE(struct bch_sb *sb, __u64 v)
  751. {
  752. SET_BCH_SB_BACKGROUND_COMPRESSION_TYPE_LO(sb, v);
  753. SET_BCH_SB_BACKGROUND_COMPRESSION_TYPE_HI(sb, v >> 4);
  754. }
  755. /*
  756. * Features:
  757. *
  758. * journal_seq_blacklist_v3: gates BCH_SB_FIELD_journal_seq_blacklist
  759. * reflink: gates KEY_TYPE_reflink
  760. * inline_data: gates KEY_TYPE_inline_data
  761. * new_siphash: gates BCH_STR_HASH_siphash
  762. * new_extent_overwrite: gates BTREE_NODE_NEW_EXTENT_OVERWRITE
  763. */
  764. #define BCH_SB_FEATURES() \
  765. x(lz4, 0) \
  766. x(gzip, 1) \
  767. x(zstd, 2) \
  768. x(atomic_nlink, 3) \
  769. x(ec, 4) \
  770. x(journal_seq_blacklist_v3, 5) \
  771. x(reflink, 6) \
  772. x(new_siphash, 7) \
  773. x(inline_data, 8) \
  774. x(new_extent_overwrite, 9) \
  775. x(incompressible, 10) \
  776. x(btree_ptr_v2, 11) \
  777. x(extents_above_btree_updates, 12) \
  778. x(btree_updates_journalled, 13) \
  779. x(reflink_inline_data, 14) \
  780. x(new_varint, 15) \
  781. x(journal_no_flush, 16) \
  782. x(alloc_v2, 17) \
  783. x(extents_across_btree_nodes, 18)
  784. #define BCH_SB_FEATURES_ALWAYS \
  785. ((1ULL << BCH_FEATURE_new_extent_overwrite)| \
  786. (1ULL << BCH_FEATURE_extents_above_btree_updates)|\
  787. (1ULL << BCH_FEATURE_btree_updates_journalled)|\
  788. (1ULL << BCH_FEATURE_alloc_v2)|\
  789. (1ULL << BCH_FEATURE_extents_across_btree_nodes))
  790. #define BCH_SB_FEATURES_ALL \
  791. (BCH_SB_FEATURES_ALWAYS| \
  792. (1ULL << BCH_FEATURE_new_siphash)| \
  793. (1ULL << BCH_FEATURE_btree_ptr_v2)| \
  794. (1ULL << BCH_FEATURE_new_varint)| \
  795. (1ULL << BCH_FEATURE_journal_no_flush))
  796. enum bch_sb_feature {
  797. #define x(f, n) BCH_FEATURE_##f,
  798. BCH_SB_FEATURES()
  799. #undef x
  800. BCH_FEATURE_NR,
  801. };
  802. #define BCH_SB_COMPAT() \
  803. x(alloc_info, 0) \
  804. x(alloc_metadata, 1) \
  805. x(extents_above_btree_updates_done, 2) \
  806. x(bformat_overflow_done, 3)
  807. enum bch_sb_compat {
  808. #define x(f, n) BCH_COMPAT_##f,
  809. BCH_SB_COMPAT()
  810. #undef x
  811. BCH_COMPAT_NR,
  812. };
  813. /* options: */
  814. #define BCH_VERSION_UPGRADE_OPTS() \
  815. x(compatible, 0) \
  816. x(incompatible, 1) \
  817. x(none, 2)
  818. enum bch_version_upgrade_opts {
  819. #define x(t, n) BCH_VERSION_UPGRADE_##t = n,
  820. BCH_VERSION_UPGRADE_OPTS()
  821. #undef x
  822. };
  823. #define BCH_REPLICAS_MAX 4U
  824. #define BCH_BKEY_PTRS_MAX 16U
  825. #define BCH_ERROR_ACTIONS() \
  826. x(continue, 0) \
  827. x(fix_safe, 1) \
  828. x(panic, 2) \
  829. x(ro, 3)
  830. enum bch_error_actions {
  831. #define x(t, n) BCH_ON_ERROR_##t = n,
  832. BCH_ERROR_ACTIONS()
  833. #undef x
  834. BCH_ON_ERROR_NR
  835. };
  836. #define BCH_STR_HASH_TYPES() \
  837. x(crc32c, 0) \
  838. x(crc64, 1) \
  839. x(siphash_old, 2) \
  840. x(siphash, 3)
  841. enum bch_str_hash_type {
  842. #define x(t, n) BCH_STR_HASH_##t = n,
  843. BCH_STR_HASH_TYPES()
  844. #undef x
  845. BCH_STR_HASH_NR
  846. };
  847. #define BCH_STR_HASH_OPTS() \
  848. x(crc32c, 0) \
  849. x(crc64, 1) \
  850. x(siphash, 2)
  851. enum bch_str_hash_opts {
  852. #define x(t, n) BCH_STR_HASH_OPT_##t = n,
  853. BCH_STR_HASH_OPTS()
  854. #undef x
  855. BCH_STR_HASH_OPT_NR
  856. };
  857. #define BCH_CSUM_TYPES() \
  858. x(none, 0) \
  859. x(crc32c_nonzero, 1) \
  860. x(crc64_nonzero, 2) \
  861. x(chacha20_poly1305_80, 3) \
  862. x(chacha20_poly1305_128, 4) \
  863. x(crc32c, 5) \
  864. x(crc64, 6) \
  865. x(xxhash, 7)
  866. enum bch_csum_type {
  867. #define x(t, n) BCH_CSUM_##t = n,
  868. BCH_CSUM_TYPES()
  869. #undef x
  870. BCH_CSUM_NR
  871. };
  872. static const __maybe_unused unsigned bch_crc_bytes[] = {
  873. [BCH_CSUM_none] = 0,
  874. [BCH_CSUM_crc32c_nonzero] = 4,
  875. [BCH_CSUM_crc32c] = 4,
  876. [BCH_CSUM_crc64_nonzero] = 8,
  877. [BCH_CSUM_crc64] = 8,
  878. [BCH_CSUM_xxhash] = 8,
  879. [BCH_CSUM_chacha20_poly1305_80] = 10,
  880. [BCH_CSUM_chacha20_poly1305_128] = 16,
  881. };
  882. static inline _Bool bch2_csum_type_is_encryption(enum bch_csum_type type)
  883. {
  884. switch (type) {
  885. case BCH_CSUM_chacha20_poly1305_80:
  886. case BCH_CSUM_chacha20_poly1305_128:
  887. return true;
  888. default:
  889. return false;
  890. }
  891. }
  892. #define BCH_CSUM_OPTS() \
  893. x(none, 0) \
  894. x(crc32c, 1) \
  895. x(crc64, 2) \
  896. x(xxhash, 3)
  897. enum bch_csum_opts {
  898. #define x(t, n) BCH_CSUM_OPT_##t = n,
  899. BCH_CSUM_OPTS()
  900. #undef x
  901. BCH_CSUM_OPT_NR
  902. };
  903. #define BCH_COMPRESSION_TYPES() \
  904. x(none, 0) \
  905. x(lz4_old, 1) \
  906. x(gzip, 2) \
  907. x(lz4, 3) \
  908. x(zstd, 4) \
  909. x(incompressible, 5)
  910. enum bch_compression_type {
  911. #define x(t, n) BCH_COMPRESSION_TYPE_##t = n,
  912. BCH_COMPRESSION_TYPES()
  913. #undef x
  914. BCH_COMPRESSION_TYPE_NR
  915. };
  916. #define BCH_COMPRESSION_OPTS() \
  917. x(none, 0) \
  918. x(lz4, 1) \
  919. x(gzip, 2) \
  920. x(zstd, 3)
  921. enum bch_compression_opts {
  922. #define x(t, n) BCH_COMPRESSION_OPT_##t = n,
  923. BCH_COMPRESSION_OPTS()
  924. #undef x
  925. BCH_COMPRESSION_OPT_NR
  926. };
  927. /*
  928. * Magic numbers
  929. *
  930. * The various other data structures have their own magic numbers, which are
  931. * xored with the first part of the cache set's UUID
  932. */
  933. #define BCACHE_MAGIC \
  934. UUID_INIT(0xc68573f6, 0x4e1a, 0x45ca, \
  935. 0x82, 0x65, 0xf5, 0x7f, 0x48, 0xba, 0x6d, 0x81)
  936. #define BCHFS_MAGIC \
  937. UUID_INIT(0xc68573f6, 0x66ce, 0x90a9, \
  938. 0xd9, 0x6a, 0x60, 0xcf, 0x80, 0x3d, 0xf7, 0xef)
  939. #define BCACHEFS_STATFS_MAGIC BCACHEFS_SUPER_MAGIC
  940. #define JSET_MAGIC __cpu_to_le64(0x245235c1a3625032ULL)
  941. #define BSET_MAGIC __cpu_to_le64(0x90135c78b99e07f5ULL)
  942. static inline __le64 __bch2_sb_magic(struct bch_sb *sb)
  943. {
  944. __le64 ret;
  945. memcpy(&ret, &sb->uuid, sizeof(ret));
  946. return ret;
  947. }
  948. static inline __u64 __jset_magic(struct bch_sb *sb)
  949. {
  950. return __le64_to_cpu(__bch2_sb_magic(sb) ^ JSET_MAGIC);
  951. }
  952. static inline __u64 __bset_magic(struct bch_sb *sb)
  953. {
  954. return __le64_to_cpu(__bch2_sb_magic(sb) ^ BSET_MAGIC);
  955. }
  956. /* Journal */
  957. #define JSET_KEYS_U64s (sizeof(struct jset_entry) / sizeof(__u64))
  958. #define BCH_JSET_ENTRY_TYPES() \
  959. x(btree_keys, 0) \
  960. x(btree_root, 1) \
  961. x(prio_ptrs, 2) \
  962. x(blacklist, 3) \
  963. x(blacklist_v2, 4) \
  964. x(usage, 5) \
  965. x(data_usage, 6) \
  966. x(clock, 7) \
  967. x(dev_usage, 8) \
  968. x(log, 9) \
  969. x(overwrite, 10) \
  970. x(write_buffer_keys, 11) \
  971. x(datetime, 12)
  972. enum bch_jset_entry_type {
  973. #define x(f, nr) BCH_JSET_ENTRY_##f = nr,
  974. BCH_JSET_ENTRY_TYPES()
  975. #undef x
  976. BCH_JSET_ENTRY_NR
  977. };
  978. static inline bool jset_entry_is_key(struct jset_entry *e)
  979. {
  980. switch (e->type) {
  981. case BCH_JSET_ENTRY_btree_keys:
  982. case BCH_JSET_ENTRY_btree_root:
  983. case BCH_JSET_ENTRY_write_buffer_keys:
  984. return true;
  985. }
  986. return false;
  987. }
  988. /*
  989. * Journal sequence numbers can be blacklisted: bsets record the max sequence
  990. * number of all the journal entries they contain updates for, so that on
  991. * recovery we can ignore those bsets that contain index updates newer that what
  992. * made it into the journal.
  993. *
  994. * This means that we can't reuse that journal_seq - we have to skip it, and
  995. * then record that we skipped it so that the next time we crash and recover we
  996. * don't think there was a missing journal entry.
  997. */
  998. struct jset_entry_blacklist {
  999. struct jset_entry entry;
  1000. __le64 seq;
  1001. };
  1002. struct jset_entry_blacklist_v2 {
  1003. struct jset_entry entry;
  1004. __le64 start;
  1005. __le64 end;
  1006. };
  1007. #define BCH_FS_USAGE_TYPES() \
  1008. x(reserved, 0) \
  1009. x(inodes, 1) \
  1010. x(key_version, 2)
  1011. enum bch_fs_usage_type {
  1012. #define x(f, nr) BCH_FS_USAGE_##f = nr,
  1013. BCH_FS_USAGE_TYPES()
  1014. #undef x
  1015. BCH_FS_USAGE_NR
  1016. };
  1017. struct jset_entry_usage {
  1018. struct jset_entry entry;
  1019. __le64 v;
  1020. } __packed;
  1021. struct jset_entry_data_usage {
  1022. struct jset_entry entry;
  1023. __le64 v;
  1024. struct bch_replicas_entry_v1 r;
  1025. } __packed;
  1026. struct jset_entry_clock {
  1027. struct jset_entry entry;
  1028. __u8 rw;
  1029. __u8 pad[7];
  1030. __le64 time;
  1031. } __packed;
  1032. struct jset_entry_dev_usage_type {
  1033. __le64 buckets;
  1034. __le64 sectors;
  1035. __le64 fragmented;
  1036. } __packed;
  1037. struct jset_entry_dev_usage {
  1038. struct jset_entry entry;
  1039. __le32 dev;
  1040. __u32 pad;
  1041. __le64 _buckets_ec; /* No longer used */
  1042. __le64 _buckets_unavailable; /* No longer used */
  1043. struct jset_entry_dev_usage_type d[];
  1044. };
  1045. static inline unsigned jset_entry_dev_usage_nr_types(struct jset_entry_dev_usage *u)
  1046. {
  1047. return (vstruct_bytes(&u->entry) - sizeof(struct jset_entry_dev_usage)) /
  1048. sizeof(struct jset_entry_dev_usage_type);
  1049. }
  1050. struct jset_entry_log {
  1051. struct jset_entry entry;
  1052. u8 d[];
  1053. } __packed __aligned(8);
  1054. struct jset_entry_datetime {
  1055. struct jset_entry entry;
  1056. __le64 seconds;
  1057. } __packed __aligned(8);
  1058. /*
  1059. * On disk format for a journal entry:
  1060. * seq is monotonically increasing; every journal entry has its own unique
  1061. * sequence number.
  1062. *
  1063. * last_seq is the oldest journal entry that still has keys the btree hasn't
  1064. * flushed to disk yet.
  1065. *
  1066. * version is for on disk format changes.
  1067. */
  1068. struct jset {
  1069. struct bch_csum csum;
  1070. __le64 magic;
  1071. __le64 seq;
  1072. __le32 version;
  1073. __le32 flags;
  1074. __le32 u64s; /* size of d[] in u64s */
  1075. __u8 encrypted_start[0];
  1076. __le16 _read_clock; /* no longer used */
  1077. __le16 _write_clock;
  1078. /* Sequence number of oldest dirty journal entry */
  1079. __le64 last_seq;
  1080. struct jset_entry start[0];
  1081. __u64 _data[];
  1082. } __packed __aligned(8);
  1083. LE32_BITMASK(JSET_CSUM_TYPE, struct jset, flags, 0, 4);
  1084. LE32_BITMASK(JSET_BIG_ENDIAN, struct jset, flags, 4, 5);
  1085. LE32_BITMASK(JSET_NO_FLUSH, struct jset, flags, 5, 6);
  1086. #define BCH_JOURNAL_BUCKETS_MIN 8
  1087. /* Btree: */
  1088. enum btree_id_flags {
  1089. BTREE_ID_EXTENTS = BIT(0),
  1090. BTREE_ID_SNAPSHOTS = BIT(1),
  1091. BTREE_ID_SNAPSHOT_FIELD = BIT(2),
  1092. BTREE_ID_DATA = BIT(3),
  1093. };
  1094. #define BCH_BTREE_IDS() \
  1095. x(extents, 0, BTREE_ID_EXTENTS|BTREE_ID_SNAPSHOTS|BTREE_ID_DATA,\
  1096. BIT_ULL(KEY_TYPE_whiteout)| \
  1097. BIT_ULL(KEY_TYPE_error)| \
  1098. BIT_ULL(KEY_TYPE_cookie)| \
  1099. BIT_ULL(KEY_TYPE_extent)| \
  1100. BIT_ULL(KEY_TYPE_reservation)| \
  1101. BIT_ULL(KEY_TYPE_reflink_p)| \
  1102. BIT_ULL(KEY_TYPE_inline_data)) \
  1103. x(inodes, 1, BTREE_ID_SNAPSHOTS, \
  1104. BIT_ULL(KEY_TYPE_whiteout)| \
  1105. BIT_ULL(KEY_TYPE_inode)| \
  1106. BIT_ULL(KEY_TYPE_inode_v2)| \
  1107. BIT_ULL(KEY_TYPE_inode_v3)| \
  1108. BIT_ULL(KEY_TYPE_inode_generation)) \
  1109. x(dirents, 2, BTREE_ID_SNAPSHOTS, \
  1110. BIT_ULL(KEY_TYPE_whiteout)| \
  1111. BIT_ULL(KEY_TYPE_hash_whiteout)| \
  1112. BIT_ULL(KEY_TYPE_dirent)) \
  1113. x(xattrs, 3, BTREE_ID_SNAPSHOTS, \
  1114. BIT_ULL(KEY_TYPE_whiteout)| \
  1115. BIT_ULL(KEY_TYPE_cookie)| \
  1116. BIT_ULL(KEY_TYPE_hash_whiteout)| \
  1117. BIT_ULL(KEY_TYPE_xattr)) \
  1118. x(alloc, 4, 0, \
  1119. BIT_ULL(KEY_TYPE_alloc)| \
  1120. BIT_ULL(KEY_TYPE_alloc_v2)| \
  1121. BIT_ULL(KEY_TYPE_alloc_v3)| \
  1122. BIT_ULL(KEY_TYPE_alloc_v4)) \
  1123. x(quotas, 5, 0, \
  1124. BIT_ULL(KEY_TYPE_quota)) \
  1125. x(stripes, 6, 0, \
  1126. BIT_ULL(KEY_TYPE_stripe)) \
  1127. x(reflink, 7, BTREE_ID_EXTENTS|BTREE_ID_DATA, \
  1128. BIT_ULL(KEY_TYPE_reflink_v)| \
  1129. BIT_ULL(KEY_TYPE_indirect_inline_data)| \
  1130. BIT_ULL(KEY_TYPE_error)) \
  1131. x(subvolumes, 8, 0, \
  1132. BIT_ULL(KEY_TYPE_subvolume)) \
  1133. x(snapshots, 9, 0, \
  1134. BIT_ULL(KEY_TYPE_snapshot)) \
  1135. x(lru, 10, 0, \
  1136. BIT_ULL(KEY_TYPE_set)) \
  1137. x(freespace, 11, BTREE_ID_EXTENTS, \
  1138. BIT_ULL(KEY_TYPE_set)) \
  1139. x(need_discard, 12, 0, \
  1140. BIT_ULL(KEY_TYPE_set)) \
  1141. x(backpointers, 13, 0, \
  1142. BIT_ULL(KEY_TYPE_backpointer)) \
  1143. x(bucket_gens, 14, 0, \
  1144. BIT_ULL(KEY_TYPE_bucket_gens)) \
  1145. x(snapshot_trees, 15, 0, \
  1146. BIT_ULL(KEY_TYPE_snapshot_tree)) \
  1147. x(deleted_inodes, 16, BTREE_ID_SNAPSHOT_FIELD, \
  1148. BIT_ULL(KEY_TYPE_set)) \
  1149. x(logged_ops, 17, 0, \
  1150. BIT_ULL(KEY_TYPE_logged_op_truncate)| \
  1151. BIT_ULL(KEY_TYPE_logged_op_finsert)) \
  1152. x(rebalance_work, 18, BTREE_ID_SNAPSHOT_FIELD, \
  1153. BIT_ULL(KEY_TYPE_set)|BIT_ULL(KEY_TYPE_cookie)) \
  1154. x(subvolume_children, 19, 0, \
  1155. BIT_ULL(KEY_TYPE_set)) \
  1156. x(accounting, 20, BTREE_ID_SNAPSHOT_FIELD, \
  1157. BIT_ULL(KEY_TYPE_accounting)) \
  1158. enum btree_id {
  1159. #define x(name, nr, ...) BTREE_ID_##name = nr,
  1160. BCH_BTREE_IDS()
  1161. #undef x
  1162. BTREE_ID_NR
  1163. };
  1164. /*
  1165. * Maximum number of btrees that we will _ever_ have under the current scheme,
  1166. * where we refer to them with 64 bit bitfields - and we also need a bit for
  1167. * the interior btree node type:
  1168. */
  1169. #define BTREE_ID_NR_MAX 63
  1170. static inline bool btree_id_is_alloc(enum btree_id id)
  1171. {
  1172. switch (id) {
  1173. case BTREE_ID_alloc:
  1174. case BTREE_ID_backpointers:
  1175. case BTREE_ID_need_discard:
  1176. case BTREE_ID_freespace:
  1177. case BTREE_ID_bucket_gens:
  1178. return true;
  1179. default:
  1180. return false;
  1181. }
  1182. }
  1183. #define BTREE_MAX_DEPTH 4U
  1184. /* Btree nodes */
  1185. /*
  1186. * Btree nodes
  1187. *
  1188. * On disk a btree node is a list/log of these; within each set the keys are
  1189. * sorted
  1190. */
  1191. struct bset {
  1192. __le64 seq;
  1193. /*
  1194. * Highest journal entry this bset contains keys for.
  1195. * If on recovery we don't see that journal entry, this bset is ignored:
  1196. * this allows us to preserve the order of all index updates after a
  1197. * crash, since the journal records a total order of all index updates
  1198. * and anything that didn't make it to the journal doesn't get used.
  1199. */
  1200. __le64 journal_seq;
  1201. __le32 flags;
  1202. __le16 version;
  1203. __le16 u64s; /* count of d[] in u64s */
  1204. struct bkey_packed start[0];
  1205. __u64 _data[];
  1206. } __packed __aligned(8);
  1207. LE32_BITMASK(BSET_CSUM_TYPE, struct bset, flags, 0, 4);
  1208. LE32_BITMASK(BSET_BIG_ENDIAN, struct bset, flags, 4, 5);
  1209. LE32_BITMASK(BSET_SEPARATE_WHITEOUTS,
  1210. struct bset, flags, 5, 6);
  1211. /* Sector offset within the btree node: */
  1212. LE32_BITMASK(BSET_OFFSET, struct bset, flags, 16, 32);
  1213. struct btree_node {
  1214. struct bch_csum csum;
  1215. __le64 magic;
  1216. /* this flags field is encrypted, unlike bset->flags: */
  1217. __le64 flags;
  1218. /* Closed interval: */
  1219. struct bpos min_key;
  1220. struct bpos max_key;
  1221. struct bch_extent_ptr _ptr; /* not used anymore */
  1222. struct bkey_format format;
  1223. union {
  1224. struct bset keys;
  1225. struct {
  1226. __u8 pad[22];
  1227. __le16 u64s;
  1228. __u64 _data[0];
  1229. };
  1230. };
  1231. } __packed __aligned(8);
  1232. LE64_BITMASK(BTREE_NODE_ID_LO, struct btree_node, flags, 0, 4);
  1233. LE64_BITMASK(BTREE_NODE_LEVEL, struct btree_node, flags, 4, 8);
  1234. LE64_BITMASK(BTREE_NODE_NEW_EXTENT_OVERWRITE,
  1235. struct btree_node, flags, 8, 9);
  1236. LE64_BITMASK(BTREE_NODE_ID_HI, struct btree_node, flags, 9, 25);
  1237. /* 25-32 unused */
  1238. LE64_BITMASK(BTREE_NODE_SEQ, struct btree_node, flags, 32, 64);
  1239. static inline __u64 BTREE_NODE_ID(struct btree_node *n)
  1240. {
  1241. return BTREE_NODE_ID_LO(n) | (BTREE_NODE_ID_HI(n) << 4);
  1242. }
  1243. static inline void SET_BTREE_NODE_ID(struct btree_node *n, __u64 v)
  1244. {
  1245. SET_BTREE_NODE_ID_LO(n, v);
  1246. SET_BTREE_NODE_ID_HI(n, v >> 4);
  1247. }
  1248. struct btree_node_entry {
  1249. struct bch_csum csum;
  1250. union {
  1251. struct bset keys;
  1252. struct {
  1253. __u8 pad[22];
  1254. __le16 u64s;
  1255. __u64 _data[0];
  1256. };
  1257. };
  1258. } __packed __aligned(8);
  1259. #endif /* _BCACHEFS_FORMAT_H */