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