bcachefs: Start snapshots before bch2_gc()
[linux-block.git] / fs / bcachefs / bcachefs_format.h
CommitLineData
1c6fdbd8
KO
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>
7121643e 77#include <linux/kernel.h>
1c6fdbd8 78#include <linux/uuid.h>
528b18e6 79#include "vstructs.h"
1c6fdbd8
KO
80
81#ifdef __KERNEL__
82typedef uuid_t __uuid_t;
83#endif
84
3d48a7f8
KO
85#define BITMASK(name, type, field, offset, end) \
86static const unsigned name##_OFFSET = offset; \
87static const unsigned name##_BITS = (end - offset); \
88 \
89static inline __u64 name(const type *k) \
90{ \
91 return (k->field >> offset) & ~(~0ULL << (end - offset)); \
92} \
93 \
94static 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
1c6fdbd8
KO
100#define LE_BITMASK(_bits, name, type, field, offset, end) \
101static const unsigned name##_OFFSET = offset; \
102static const unsigned name##_BITS = (end - offset); \
103static const __u##_bits name##_MAX = (1ULL << (end - offset)) - 1; \
104 \
105static inline __u64 name(const type *k) \
106{ \
107 return (__le##_bits##_to_cpu(k->field) >> offset) & \
108 ~(~0ULL << (end - offset)); \
109} \
110 \
111static 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
124struct 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
134struct 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
fd0c7679 154} __packed __aligned(4);
1c6fdbd8
KO
155
156#define KEY_INODE_MAX ((__u64)~0ULL)
157#define KEY_OFFSET_MAX ((__u64)~0ULL)
158#define KEY_SNAPSHOT_MAX ((__u32)~0U)
159#define KEY_SIZE_MAX ((__u32)~0U)
160
e751c01a 161static inline struct bpos SPOS(__u64 inode, __u64 offset, __u32 snapshot)
1c6fdbd8 162{
e751c01a
KO
163 return (struct bpos) {
164 .inode = inode,
165 .offset = offset,
166 .snapshot = snapshot,
167 };
1c6fdbd8
KO
168}
169
e751c01a 170#define POS_MIN SPOS(0, 0, 0)
618b1c0e
KO
171#define POS_MAX SPOS(KEY_INODE_MAX, KEY_OFFSET_MAX, 0)
172#define SPOS_MAX SPOS(KEY_INODE_MAX, KEY_OFFSET_MAX, KEY_SNAPSHOT_MAX)
e751c01a 173#define POS(_inode, _offset) SPOS(_inode, _offset, 0)
1c6fdbd8
KO
174
175/* Empty placeholder struct, for container_of() */
176struct bch_val {
177 __u64 __nothing[0];
178};
179
180struct bversion {
181#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
182 __u64 lo;
183 __u32 hi;
184#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
185 __u32 hi;
186 __u64 lo;
187#endif
fd0c7679 188} __packed __aligned(4);
1c6fdbd8
KO
189
190struct bkey {
191 /* Size of combined key and value, in u64s */
192 __u8 u64s;
193
194 /* Format of key (0 for format local to btree node) */
195#if defined(__LITTLE_ENDIAN_BITFIELD)
196 __u8 format:7,
197 needs_whiteout:1;
198#elif defined (__BIG_ENDIAN_BITFIELD)
199 __u8 needs_whiteout:1,
200 format:7;
201#else
202#error edit for your odd byteorder.
203#endif
204
205 /* Type of the value */
206 __u8 type;
207
208#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
209 __u8 pad[1];
210
211 struct bversion version;
212 __u32 size; /* extent size, in sectors */
213 struct bpos p;
214#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
215 struct bpos p;
216 __u32 size; /* extent size, in sectors */
217 struct bversion version;
218
219 __u8 pad[1];
220#endif
fd0c7679 221} __packed __aligned(8);
1c6fdbd8
KO
222
223struct bkey_packed {
224 __u64 _data[0];
225
226 /* Size of combined key and value, in u64s */
227 __u8 u64s;
228
229 /* Format of key (0 for format local to btree node) */
230
231 /*
232 * XXX: next incompat on disk format change, switch format and
233 * needs_whiteout - bkey_packed() will be cheaper if format is the high
234 * bits of the bitfield
235 */
236#if defined(__LITTLE_ENDIAN_BITFIELD)
237 __u8 format:7,
238 needs_whiteout:1;
239#elif defined (__BIG_ENDIAN_BITFIELD)
240 __u8 needs_whiteout:1,
241 format:7;
242#endif
243
244 /* Type of the value */
245 __u8 type;
246 __u8 key_start[0];
247
248 /*
249 * We copy bkeys with struct assignment in various places, and while
250 * that shouldn't be done with packed bkeys we can't disallow it in C,
251 * and it's legal to cast a bkey to a bkey_packed - so padding it out
252 * to the same size as struct bkey should hopefully be safest.
253 */
254 __u8 pad[sizeof(struct bkey) - 3];
fd0c7679 255} __packed __aligned(8);
1c6fdbd8
KO
256
257#define BKEY_U64s (sizeof(struct bkey) / sizeof(__u64))
cd575ddf
KO
258#define BKEY_U64s_MAX U8_MAX
259#define BKEY_VAL_U64s_MAX (BKEY_U64s_MAX - BKEY_U64s)
260
1c6fdbd8
KO
261#define KEY_PACKED_BITS_START 24
262
263#define KEY_FORMAT_LOCAL_BTREE 0
264#define KEY_FORMAT_CURRENT 1
265
266enum bch_bkey_fields {
267 BKEY_FIELD_INODE,
268 BKEY_FIELD_OFFSET,
269 BKEY_FIELD_SNAPSHOT,
270 BKEY_FIELD_SIZE,
271 BKEY_FIELD_VERSION_HI,
272 BKEY_FIELD_VERSION_LO,
273 BKEY_NR_FIELDS,
274};
275
276#define bkey_format_field(name, field) \
277 [BKEY_FIELD_##name] = (sizeof(((struct bkey *) NULL)->field) * 8)
278
279#define BKEY_FORMAT_CURRENT \
280((struct bkey_format) { \
281 .key_u64s = BKEY_U64s, \
282 .nr_fields = BKEY_NR_FIELDS, \
283 .bits_per_field = { \
284 bkey_format_field(INODE, p.inode), \
285 bkey_format_field(OFFSET, p.offset), \
286 bkey_format_field(SNAPSHOT, p.snapshot), \
287 bkey_format_field(SIZE, size), \
288 bkey_format_field(VERSION_HI, version.hi), \
289 bkey_format_field(VERSION_LO, version.lo), \
290 }, \
291})
292
293/* bkey with inline value */
294struct bkey_i {
295 __u64 _data[0];
296
297 union {
298 struct {
299 /* Size of combined key and value, in u64s */
300 __u8 u64s;
301 };
302 struct {
303 struct bkey k;
304 struct bch_val v;
305 };
306 };
307};
308
309#define KEY(_inode, _offset, _size) \
310((struct bkey) { \
311 .u64s = BKEY_U64s, \
312 .format = KEY_FORMAT_CURRENT, \
313 .p = POS(_inode, _offset), \
314 .size = _size, \
315})
316
317static inline void bkey_init(struct bkey *k)
318{
319 *k = KEY(0, 0, 0);
320}
321
322#define bkey_bytes(_k) ((_k)->u64s * sizeof(__u64))
323
324#define __BKEY_PADDED(key, pad) \
325 struct { struct bkey_i key; __u64 key ## _pad[pad]; }
326
1c6fdbd8
KO
327/*
328 * - DELETED keys are used internally to mark keys that should be ignored but
329 * override keys in composition order. Their version number is ignored.
330 *
331 * - DISCARDED keys indicate that the data is all 0s because it has been
332 * discarded. DISCARDs may have a version; if the version is nonzero the key
333 * will be persistent, otherwise the key will be dropped whenever the btree
334 * node is rewritten (like DELETED keys).
335 *
336 * - ERROR: any read of the data returns a read error, as the data was lost due
337 * to a failing device. Like DISCARDED keys, they can be removed (overridden)
338 * by new writes or cluster-wide GC. Node repair can also overwrite them with
339 * the same or a more recent version number, but not with an older version
340 * number.
26609b61
KO
341 *
342 * - WHITEOUT: for hash table btrees
3e3e02e6 343 */
26609b61
KO
344#define BCH_BKEY_TYPES() \
345 x(deleted, 0) \
7a7d17b2 346 x(whiteout, 1) \
26609b61
KO
347 x(error, 2) \
348 x(cookie, 3) \
79f88eba 349 x(hash_whiteout, 4) \
26609b61
KO
350 x(btree_ptr, 5) \
351 x(extent, 6) \
352 x(reservation, 7) \
353 x(inode, 8) \
354 x(inode_generation, 9) \
355 x(dirent, 10) \
356 x(xattr, 11) \
357 x(alloc, 12) \
358 x(quota, 13) \
76426098
KO
359 x(stripe, 14) \
360 x(reflink_p, 15) \
4be1a412 361 x(reflink_v, 16) \
548b3d20 362 x(inline_data, 17) \
801a3de6 363 x(btree_ptr_v2, 18) \
7f4e1d5d 364 x(indirect_inline_data, 19) \
14b393ee
KO
365 x(alloc_v2, 20) \
366 x(subvolume, 21) \
3e52c222
KO
367 x(snapshot, 22) \
368 x(inode_v2, 23) \
179e3434 369 x(alloc_v3, 24) \
d326ab2f 370 x(set, 25) \
3d48a7f8 371 x(lru, 26) \
a8c752bb
KO
372 x(alloc_v4, 27) \
373 x(backpointer, 28)
26609b61
KO
374
375enum bch_bkey_type {
376#define x(name, nr) KEY_TYPE_##name = nr,
377 BCH_BKEY_TYPES()
378#undef x
379 KEY_TYPE_MAX,
380};
1c6fdbd8 381
79f88eba
KO
382struct bch_deleted {
383 struct bch_val v;
384};
385
7a7d17b2 386struct bch_whiteout {
79f88eba
KO
387 struct bch_val v;
388};
389
390struct bch_error {
391 struct bch_val v;
392};
393
1c6fdbd8
KO
394struct bch_cookie {
395 struct bch_val v;
396 __le64 cookie;
397};
1c6fdbd8 398
79f88eba
KO
399struct bch_hash_whiteout {
400 struct bch_val v;
401};
402
179e3434
KO
403struct bch_set {
404 struct bch_val v;
405};
406
1c6fdbd8
KO
407/* Extents */
408
409/*
410 * In extent bkeys, the value is a list of pointers (bch_extent_ptr), optionally
411 * preceded by checksum/compression information (bch_extent_crc32 or
412 * bch_extent_crc64).
413 *
414 * One major determining factor in the format of extents is how we handle and
415 * represent extents that have been partially overwritten and thus trimmed:
416 *
417 * If an extent is not checksummed or compressed, when the extent is trimmed we
418 * don't have to remember the extent we originally allocated and wrote: we can
419 * merely adjust ptr->offset to point to the start of the data that is currently
420 * live. The size field in struct bkey records the current (live) size of the
421 * extent, and is also used to mean "size of region on disk that we point to" in
422 * this case.
423 *
424 * Thus an extent that is not checksummed or compressed will consist only of a
425 * list of bch_extent_ptrs, with none of the fields in
426 * bch_extent_crc32/bch_extent_crc64.
427 *
428 * When an extent is checksummed or compressed, it's not possible to read only
429 * the data that is currently live: we have to read the entire extent that was
430 * originally written, and then return only the part of the extent that is
431 * currently live.
432 *
433 * Thus, in addition to the current size of the extent in struct bkey, we need
434 * to store the size of the originally allocated space - this is the
435 * compressed_size and uncompressed_size fields in bch_extent_crc32/64. Also,
436 * when the extent is trimmed, instead of modifying the offset field of the
437 * pointer, we keep a second smaller offset field - "offset into the original
438 * extent of the currently live region".
439 *
440 * The other major determining factor is replication and data migration:
441 *
442 * Each pointer may have its own bch_extent_crc32/64. When doing a replicated
443 * write, we will initially write all the replicas in the same format, with the
444 * same checksum type and compression format - however, when copygc runs later (or
445 * tiering/cache promotion, anything that moves data), it is not in general
446 * going to rewrite all the pointers at once - one of the replicas may be in a
447 * bucket on one device that has very little fragmentation while another lives
448 * in a bucket that has become heavily fragmented, and thus is being rewritten
449 * sooner than the rest.
450 *
451 * Thus it will only move a subset of the pointers (or in the case of
452 * tiering/cache promotion perhaps add a single pointer without dropping any
453 * current pointers), and if the extent has been partially overwritten it must
454 * write only the currently live portion (or copygc would not be able to reduce
455 * fragmentation!) - which necessitates a different bch_extent_crc format for
456 * the new pointer.
457 *
458 * But in the interests of space efficiency, we don't want to store one
459 * bch_extent_crc for each pointer if we don't have to.
460 *
461 * Thus, a bch_extent consists of bch_extent_crc32s, bch_extent_crc64s, and
462 * bch_extent_ptrs appended arbitrarily one after the other. We determine the
463 * type of a given entry with a scheme similar to utf8 (except we're encoding a
464 * type, not a size), encoding the type in the position of the first set bit:
465 *
466 * bch_extent_crc32 - 0b1
467 * bch_extent_ptr - 0b10
468 * bch_extent_crc64 - 0b100
469 *
470 * We do it this way because bch_extent_crc32 is _very_ constrained on bits (and
471 * bch_extent_crc64 is the least constrained).
472 *
473 * Then, each bch_extent_crc32/64 applies to the pointers that follow after it,
474 * until the next bch_extent_crc32/64.
475 *
476 * If there are no bch_extent_crcs preceding a bch_extent_ptr, then that pointer
477 * is neither checksummed nor compressed.
478 */
479
480/* 128 bits, sufficient for cryptographic MACs: */
481struct bch_csum {
482 __le64 lo;
483 __le64 hi;
fd0c7679 484} __packed __aligned(8);
1c6fdbd8 485
abce30b7
KO
486#define BCH_EXTENT_ENTRY_TYPES() \
487 x(ptr, 0) \
488 x(crc32, 1) \
489 x(crc64, 2) \
cd575ddf
KO
490 x(crc128, 3) \
491 x(stripe_ptr, 4)
492#define BCH_EXTENT_ENTRY_MAX 5
abce30b7 493
1c6fdbd8 494enum bch_extent_entry_type {
abce30b7
KO
495#define x(f, n) BCH_EXTENT_ENTRY_##f = n,
496 BCH_EXTENT_ENTRY_TYPES()
497#undef x
1c6fdbd8
KO
498};
499
1c6fdbd8
KO
500/* Compressed/uncompressed size are stored biased by 1: */
501struct bch_extent_crc32 {
502#if defined(__LITTLE_ENDIAN_BITFIELD)
503 __u32 type:2,
504 _compressed_size:7,
505 _uncompressed_size:7,
506 offset:7,
507 _unused:1,
508 csum_type:4,
509 compression_type:4;
510 __u32 csum;
511#elif defined (__BIG_ENDIAN_BITFIELD)
512 __u32 csum;
513 __u32 compression_type:4,
514 csum_type:4,
515 _unused:1,
516 offset:7,
517 _uncompressed_size:7,
518 _compressed_size:7,
519 type:2;
520#endif
fd0c7679 521} __packed __aligned(8);
1c6fdbd8
KO
522
523#define CRC32_SIZE_MAX (1U << 7)
524#define CRC32_NONCE_MAX 0
525
526struct bch_extent_crc64 {
527#if defined(__LITTLE_ENDIAN_BITFIELD)
528 __u64 type:3,
529 _compressed_size:9,
530 _uncompressed_size:9,
531 offset:9,
532 nonce:10,
533 csum_type:4,
534 compression_type:4,
535 csum_hi:16;
536#elif defined (__BIG_ENDIAN_BITFIELD)
537 __u64 csum_hi:16,
538 compression_type:4,
539 csum_type:4,
540 nonce:10,
541 offset:9,
542 _uncompressed_size:9,
543 _compressed_size:9,
544 type:3;
545#endif
546 __u64 csum_lo;
fd0c7679 547} __packed __aligned(8);
1c6fdbd8
KO
548
549#define CRC64_SIZE_MAX (1U << 9)
550#define CRC64_NONCE_MAX ((1U << 10) - 1)
551
552struct bch_extent_crc128 {
553#if defined(__LITTLE_ENDIAN_BITFIELD)
554 __u64 type:4,
555 _compressed_size:13,
556 _uncompressed_size:13,
557 offset:13,
558 nonce:13,
559 csum_type:4,
560 compression_type:4;
561#elif defined (__BIG_ENDIAN_BITFIELD)
562 __u64 compression_type:4,
563 csum_type:4,
564 nonce:13,
565 offset:13,
566 _uncompressed_size:13,
567 _compressed_size:13,
568 type:4;
569#endif
570 struct bch_csum csum;
fd0c7679 571} __packed __aligned(8);
1c6fdbd8
KO
572
573#define CRC128_SIZE_MAX (1U << 13)
574#define CRC128_NONCE_MAX ((1U << 13) - 1)
575
576/*
577 * @reservation - pointer hasn't been written to, just reserved
578 */
579struct bch_extent_ptr {
580#if defined(__LITTLE_ENDIAN_BITFIELD)
581 __u64 type:1,
582 cached:1,
cd575ddf 583 unused:1,
1c6fdbd8
KO
584 reservation:1,
585 offset:44, /* 8 petabytes */
586 dev:8,
587 gen:8;
588#elif defined (__BIG_ENDIAN_BITFIELD)
589 __u64 gen:8,
590 dev:8,
591 offset:44,
592 reservation:1,
cd575ddf 593 unused:1,
1c6fdbd8
KO
594 cached:1,
595 type:1;
596#endif
fd0c7679 597} __packed __aligned(8);
1c6fdbd8 598
cd575ddf 599struct bch_extent_stripe_ptr {
1c6fdbd8
KO
600#if defined(__LITTLE_ENDIAN_BITFIELD)
601 __u64 type:5,
cd575ddf 602 block:8,
7f4e1d5d
KO
603 redundancy:4,
604 idx:47;
cd575ddf 605#elif defined (__BIG_ENDIAN_BITFIELD)
7f4e1d5d
KO
606 __u64 idx:47,
607 redundancy:4,
cd575ddf
KO
608 block:8,
609 type:5;
610#endif
611};
612
613struct bch_extent_reservation {
614#if defined(__LITTLE_ENDIAN_BITFIELD)
615 __u64 type:6,
616 unused:22,
1c6fdbd8
KO
617 replicas:4,
618 generation:32;
619#elif defined (__BIG_ENDIAN_BITFIELD)
620 __u64 generation:32,
621 replicas:4,
cd575ddf
KO
622 unused:22,
623 type:6;
1c6fdbd8
KO
624#endif
625};
626
627union bch_extent_entry {
628#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ || __BITS_PER_LONG == 64
629 unsigned long type;
630#elif __BITS_PER_LONG == 32
631 struct {
632 unsigned long pad;
633 unsigned long type;
634 };
635#else
636#error edit for your odd byteorder.
637#endif
abce30b7
KO
638
639#define x(f, n) struct bch_extent_##f f;
640 BCH_EXTENT_ENTRY_TYPES()
641#undef x
1c6fdbd8
KO
642};
643
26609b61
KO
644struct bch_btree_ptr {
645 struct bch_val v;
1c6fdbd8 646
26609b61
KO
647 __u64 _data[0];
648 struct bch_extent_ptr start[];
fd0c7679 649} __packed __aligned(8);
1c6fdbd8 650
548b3d20
KO
651struct bch_btree_ptr_v2 {
652 struct bch_val v;
653
654 __u64 mem_ptr;
655 __le64 seq;
656 __le16 sectors_written;
51d2dfb8 657 __le16 flags;
548b3d20
KO
658 struct bpos min_key;
659 __u64 _data[0];
660 struct bch_extent_ptr start[];
fd0c7679 661} __packed __aligned(8);
548b3d20 662
51d2dfb8
KO
663LE16_BITMASK(BTREE_PTR_RANGE_UPDATED, struct bch_btree_ptr_v2, flags, 0, 1);
664
1c6fdbd8
KO
665struct bch_extent {
666 struct bch_val v;
667
668 __u64 _data[0];
669 union bch_extent_entry start[];
fd0c7679 670} __packed __aligned(8);
1c6fdbd8
KO
671
672struct bch_reservation {
673 struct bch_val v;
674
675 __le32 generation;
676 __u8 nr_replicas;
677 __u8 pad[3];
fd0c7679 678} __packed __aligned(8);
1c6fdbd8
KO
679
680/* Maximum size (in u64s) a single pointer could be: */
681#define BKEY_EXTENT_PTR_U64s_MAX\
682 ((sizeof(struct bch_extent_crc128) + \
683 sizeof(struct bch_extent_ptr)) / sizeof(u64))
684
685/* Maximum possible size of an entire extent value: */
686#define BKEY_EXTENT_VAL_U64s_MAX \
5055b509 687 (1 + BKEY_EXTENT_PTR_U64s_MAX * (BCH_REPLICAS_MAX + 1))
1c6fdbd8 688
1c6fdbd8
KO
689/* * Maximum possible size of an entire extent, key + value: */
690#define BKEY_EXTENT_U64s_MAX (BKEY_U64s + BKEY_EXTENT_VAL_U64s_MAX)
691
692/* Btree pointers don't carry around checksums: */
693#define BKEY_BTREE_PTR_VAL_U64s_MAX \
548b3d20
KO
694 ((sizeof(struct bch_btree_ptr_v2) + \
695 sizeof(struct bch_extent_ptr) * BCH_REPLICAS_MAX) / sizeof(u64))
1c6fdbd8
KO
696#define BKEY_BTREE_PTR_U64s_MAX \
697 (BKEY_U64s + BKEY_BTREE_PTR_VAL_U64s_MAX)
698
699/* Inodes */
700
701#define BLOCKDEV_INODE_MAX 4096
702
703#define BCACHEFS_ROOT_INO 4096
704
1c6fdbd8
KO
705struct bch_inode {
706 struct bch_val v;
707
708 __le64 bi_hash_seed;
709 __le32 bi_flags;
710 __le16 bi_mode;
711 __u8 fields[0];
fd0c7679 712} __packed __aligned(8);
1c6fdbd8 713
3e52c222
KO
714struct bch_inode_v2 {
715 struct bch_val v;
716
717 __le64 bi_journal_seq;
718 __le64 bi_hash_seed;
719 __le64 bi_flags;
720 __le16 bi_mode;
721 __u8 fields[0];
fd0c7679 722} __packed __aligned(8);
3e52c222 723
1c6fdbd8
KO
724struct bch_inode_generation {
725 struct bch_val v;
726
727 __le32 bi_generation;
728 __le32 pad;
fd0c7679 729} __packed __aligned(8);
1c6fdbd8 730
14b393ee
KO
731/*
732 * bi_subvol and bi_parent_subvol are only set for subvolume roots:
733 */
734
a3e70fb2 735#define BCH_INODE_FIELDS() \
a3e72262
KO
736 x(bi_atime, 96) \
737 x(bi_ctime, 96) \
738 x(bi_mtime, 96) \
739 x(bi_otime, 96) \
a3e70fb2
KO
740 x(bi_size, 64) \
741 x(bi_sectors, 64) \
742 x(bi_uid, 32) \
743 x(bi_gid, 32) \
744 x(bi_nlink, 32) \
745 x(bi_generation, 32) \
746 x(bi_dev, 32) \
747 x(bi_data_checksum, 8) \
748 x(bi_compression, 8) \
749 x(bi_project, 32) \
750 x(bi_background_compression, 8) \
751 x(bi_data_replicas, 8) \
752 x(bi_promote_target, 16) \
753 x(bi_foreground_target, 16) \
754 x(bi_background_target, 16) \
721d4ad8 755 x(bi_erasure_code, 16) \
ab2a29cc
KO
756 x(bi_fields_set, 16) \
757 x(bi_dir, 64) \
14b393ee
KO
758 x(bi_dir_offset, 64) \
759 x(bi_subvol, 32) \
760 x(bi_parent_subvol, 32)
a3e70fb2 761
d42dd4ad
KO
762/* subset of BCH_INODE_FIELDS */
763#define BCH_INODE_OPTS() \
764 x(data_checksum, 8) \
765 x(compression, 8) \
766 x(project, 32) \
767 x(background_compression, 8) \
768 x(data_replicas, 8) \
769 x(promote_target, 16) \
770 x(foreground_target, 16) \
771 x(background_target, 16) \
772 x(erasure_code, 16)
1c6fdbd8 773
721d4ad8
KO
774enum inode_opt_id {
775#define x(name, ...) \
776 Inode_opt_##name,
777 BCH_INODE_OPTS()
778#undef x
779 Inode_opt_nr,
780};
781
1c6fdbd8
KO
782enum {
783 /*
784 * User flags (get/settable with FS_IOC_*FLAGS, correspond to FS_*_FL
785 * flags)
786 */
3e3e02e6
KO
787 __BCH_INODE_SYNC = 0,
788 __BCH_INODE_IMMUTABLE = 1,
789 __BCH_INODE_APPEND = 2,
790 __BCH_INODE_NODUMP = 3,
791 __BCH_INODE_NOATIME = 4,
792
793 __BCH_INODE_I_SIZE_DIRTY = 5,
794 __BCH_INODE_I_SECTORS_DIRTY = 6,
795 __BCH_INODE_UNLINKED = 7,
796 __BCH_INODE_BACKPTR_UNTRUSTED = 8,
1c6fdbd8
KO
797
798 /* bits 20+ reserved for packed fields below: */
799};
800
801#define BCH_INODE_SYNC (1 << __BCH_INODE_SYNC)
802#define BCH_INODE_IMMUTABLE (1 << __BCH_INODE_IMMUTABLE)
803#define BCH_INODE_APPEND (1 << __BCH_INODE_APPEND)
804#define BCH_INODE_NODUMP (1 << __BCH_INODE_NODUMP)
805#define BCH_INODE_NOATIME (1 << __BCH_INODE_NOATIME)
806#define BCH_INODE_I_SIZE_DIRTY (1 << __BCH_INODE_I_SIZE_DIRTY)
807#define BCH_INODE_I_SECTORS_DIRTY (1 << __BCH_INODE_I_SECTORS_DIRTY)
808#define BCH_INODE_UNLINKED (1 << __BCH_INODE_UNLINKED)
ab2a29cc 809#define BCH_INODE_BACKPTR_UNTRUSTED (1 << __BCH_INODE_BACKPTR_UNTRUSTED)
1c6fdbd8
KO
810
811LE32_BITMASK(INODE_STR_HASH, struct bch_inode, bi_flags, 20, 24);
a3e72262
KO
812LE32_BITMASK(INODE_NR_FIELDS, struct bch_inode, bi_flags, 24, 31);
813LE32_BITMASK(INODE_NEW_VARINT, struct bch_inode, bi_flags, 31, 32);
1c6fdbd8 814
3e52c222
KO
815LE64_BITMASK(INODEv2_STR_HASH, struct bch_inode_v2, bi_flags, 20, 24);
816LE64_BITMASK(INODEv2_NR_FIELDS, struct bch_inode_v2, bi_flags, 24, 31);
817
1c6fdbd8
KO
818/* Dirents */
819
820/*
821 * Dirents (and xattrs) have to implement string lookups; since our b-tree
822 * doesn't support arbitrary length strings for the key, we instead index by a
823 * 64 bit hash (currently truncated sha1) of the string, stored in the offset
824 * field of the key - using linear probing to resolve hash collisions. This also
825 * provides us with the readdir cookie posix requires.
826 *
827 * Linear probing requires us to use whiteouts for deletions, in the event of a
828 * collision:
829 */
830
1c6fdbd8
KO
831struct bch_dirent {
832 struct bch_val v;
833
834 /* Target inode number: */
4db65027 835 union {
1c6fdbd8 836 __le64 d_inum;
4db65027
KO
837 struct { /* DT_SUBVOL */
838 __le32 d_child_subvol;
839 __le32 d_parent_subvol;
840 };
841 };
1c6fdbd8
KO
842
843 /*
844 * Copy of mode bits 12-15 from the target inode - so userspace can get
845 * the filetype without having to do a stat()
846 */
847 __u8 d_type;
848
849 __u8 d_name[];
fd0c7679 850} __packed __aligned(8);
1c6fdbd8 851
14b393ee
KO
852#define DT_SUBVOL 16
853#define BCH_DT_MAX 17
854
502f973d 855#define BCH_NAME_MAX ((unsigned) (U8_MAX * sizeof(u64) - \
1c6fdbd8 856 sizeof(struct bkey) - \
502f973d 857 offsetof(struct bch_dirent, d_name)))
1c6fdbd8
KO
858
859/* Xattrs */
860
26609b61
KO
861#define KEY_TYPE_XATTR_INDEX_USER 0
862#define KEY_TYPE_XATTR_INDEX_POSIX_ACL_ACCESS 1
863#define KEY_TYPE_XATTR_INDEX_POSIX_ACL_DEFAULT 2
864#define KEY_TYPE_XATTR_INDEX_TRUSTED 3
865#define KEY_TYPE_XATTR_INDEX_SECURITY 4
1c6fdbd8
KO
866
867struct bch_xattr {
868 struct bch_val v;
869 __u8 x_type;
870 __u8 x_name_len;
871 __le16 x_val_len;
872 __u8 x_name[];
fd0c7679 873} __packed __aligned(8);
1c6fdbd8
KO
874
875/* Bucket/allocation information: */
876
1c6fdbd8
KO
877struct bch_alloc {
878 struct bch_val v;
879 __u8 fields;
880 __u8 gen;
881 __u8 data[];
fd0c7679 882} __packed __aligned(8);
1c6fdbd8 883
7f4e1d5d 884#define BCH_ALLOC_FIELDS_V1() \
8fe826f9
KO
885 x(read_time, 16) \
886 x(write_time, 16) \
887 x(data_type, 8) \
888 x(dirty_sectors, 16) \
889 x(cached_sectors, 16) \
7f4e1d5d
KO
890 x(oldest_gen, 8) \
891 x(stripe, 32) \
892 x(stripe_redundancy, 8)
893
a8c752bb
KO
894enum {
895#define x(name, _bits) BCH_ALLOC_FIELD_V1_##name,
896 BCH_ALLOC_FIELDS_V1()
897#undef x
898};
899
7f4e1d5d
KO
900struct bch_alloc_v2 {
901 struct bch_val v;
902 __u8 nr_fields;
903 __u8 gen;
904 __u8 oldest_gen;
905 __u8 data_type;
906 __u8 data[];
fd0c7679 907} __packed __aligned(8);
7f4e1d5d
KO
908
909#define BCH_ALLOC_FIELDS_V2() \
910 x(read_time, 64) \
911 x(write_time, 64) \
66d90823
KO
912 x(dirty_sectors, 32) \
913 x(cached_sectors, 32) \
7f4e1d5d
KO
914 x(stripe, 32) \
915 x(stripe_redundancy, 8)
90541a74 916
3e52c222
KO
917struct bch_alloc_v3 {
918 struct bch_val v;
919 __le64 journal_seq;
920 __le32 flags;
921 __u8 nr_fields;
922 __u8 gen;
923 __u8 oldest_gen;
924 __u8 data_type;
925 __u8 data[];
fd0c7679 926} __packed __aligned(8);
3e52c222 927
a8c752bb
KO
928LE32_BITMASK(BCH_ALLOC_V3_NEED_DISCARD,struct bch_alloc_v3, flags, 0, 1)
929LE32_BITMASK(BCH_ALLOC_V3_NEED_INC_GEN,struct bch_alloc_v3, flags, 1, 2)
930
3d48a7f8
KO
931struct bch_alloc_v4 {
932 struct bch_val v;
933 __u64 journal_seq;
934 __u32 flags;
935 __u8 gen;
936 __u8 oldest_gen;
937 __u8 data_type;
938 __u8 stripe_redundancy;
939 __u32 dirty_sectors;
940 __u32 cached_sectors;
941 __u64 io_time[2];
942 __u32 stripe;
943 __u32 nr_external_backpointers;
fd0c7679 944} __packed __aligned(8);
3d48a7f8 945
19a614d2
KO
946#define BCH_ALLOC_V4_U64s_V0 6
947#define BCH_ALLOC_V4_U64s (sizeof(struct bch_alloc_v4) / sizeof(u64))
948
3d48a7f8
KO
949BITMASK(BCH_ALLOC_V4_NEED_DISCARD, struct bch_alloc_v4, flags, 0, 1)
950BITMASK(BCH_ALLOC_V4_NEED_INC_GEN, struct bch_alloc_v4, flags, 1, 2)
951BITMASK(BCH_ALLOC_V4_BACKPOINTERS_START,struct bch_alloc_v4, flags, 2, 8)
952BITMASK(BCH_ALLOC_V4_NR_BACKPOINTERS, struct bch_alloc_v4, flags, 8, 14)
953
a8c752bb
KO
954#define BCH_ALLOC_V4_NR_BACKPOINTERS_MAX 40
955
956struct bch_backpointer {
957 struct bch_val v;
958 __u8 btree_id;
959 __u8 level;
960 __u8 data_type;
961 __u64 bucket_offset:40;
962 __u32 bucket_len;
963 struct bpos pos;
964} __packed __aligned(8);
90541a74 965
1c6fdbd8
KO
966/* Quotas: */
967
1c6fdbd8
KO
968enum quota_types {
969 QTYP_USR = 0,
970 QTYP_GRP = 1,
971 QTYP_PRJ = 2,
972 QTYP_NR = 3,
973};
974
975enum quota_counters {
976 Q_SPC = 0,
977 Q_INO = 1,
978 Q_COUNTERS = 2,
979};
980
981struct bch_quota_counter {
982 __le64 hardlimit;
983 __le64 softlimit;
984};
985
986struct bch_quota {
987 struct bch_val v;
988 struct bch_quota_counter c[Q_COUNTERS];
fd0c7679 989} __packed __aligned(8);
1c6fdbd8 990
cd575ddf
KO
991/* Erasure coding */
992
cd575ddf
KO
993struct bch_stripe {
994 struct bch_val v;
995 __le16 sectors;
996 __u8 algorithm;
997 __u8 nr_blocks;
998 __u8 nr_redundant;
999
1000 __u8 csum_granularity_bits;
1001 __u8 csum_type;
1002 __u8 pad;
1003
81d8599e 1004 struct bch_extent_ptr ptrs[];
fd0c7679 1005} __packed __aligned(8);
cd575ddf 1006
76426098
KO
1007/* Reflink: */
1008
1009struct bch_reflink_p {
1010 struct bch_val v;
1011 __le64 idx;
6d76aefe
KO
1012 /*
1013 * A reflink pointer might point to an indirect extent which is then
1014 * later split (by copygc or rebalance). If we only pointed to part of
1015 * the original indirect extent, and then one of the fragments is
1016 * outside the range we point to, we'd leak a refcount: so when creating
1017 * reflink pointers, we need to store pad values to remember the full
1018 * range we were taking a reference on.
1019 */
1020 __le32 front_pad;
1021 __le32 back_pad;
fd0c7679 1022} __packed __aligned(8);
76426098
KO
1023
1024struct bch_reflink_v {
1025 struct bch_val v;
1026 __le64 refcount;
1027 union bch_extent_entry start[0];
1028 __u64 _data[0];
fd0c7679 1029} __packed __aligned(8);
76426098 1030
801a3de6
KO
1031struct bch_indirect_inline_data {
1032 struct bch_val v;
1033 __le64 refcount;
1034 u8 data[0];
1035};
1036
4be1a412
KO
1037/* Inline data */
1038
1039struct bch_inline_data {
1040 struct bch_val v;
1041 u8 data[0];
1042};
1043
14b393ee
KO
1044/* Subvolumes: */
1045
1046#define SUBVOL_POS_MIN POS(0, 1)
1047#define SUBVOL_POS_MAX POS(0, S32_MAX)
1048#define BCACHEFS_ROOT_SUBVOL 1
1049
1050struct bch_subvolume {
1051 struct bch_val v;
1052 __le32 flags;
1053 __le32 snapshot;
1054 __le64 inode;
1055};
1056
1057LE32_BITMASK(BCH_SUBVOLUME_RO, struct bch_subvolume, flags, 0, 1)
1058/*
1059 * We need to know whether a subvolume is a snapshot so we can know whether we
1060 * can delete it (or whether it should just be rm -rf'd)
1061 */
1062LE32_BITMASK(BCH_SUBVOLUME_SNAP, struct bch_subvolume, flags, 1, 2)
2027875b 1063LE32_BITMASK(BCH_SUBVOLUME_UNLINKED, struct bch_subvolume, flags, 2, 3)
14b393ee
KO
1064
1065/* Snapshots */
1066
1067struct bch_snapshot {
1068 struct bch_val v;
1069 __le32 flags;
1070 __le32 parent;
1071 __le32 children[2];
1072 __le32 subvol;
1073 __le32 pad;
1074};
1075
1076LE32_BITMASK(BCH_SNAPSHOT_DELETED, struct bch_snapshot, flags, 0, 1)
1077
1078/* True if a subvolume points to this snapshot node: */
1079LE32_BITMASK(BCH_SNAPSHOT_SUBVOL, struct bch_snapshot, flags, 1, 2)
1080
d326ab2f
KO
1081/* LRU btree: */
1082
1083struct bch_lru {
1084 struct bch_val v;
1085 __le64 idx;
fd0c7679 1086} __packed __aligned(8);
d326ab2f
KO
1087
1088#define LRU_ID_STRIPES (1U << 16)
1089
1c6fdbd8
KO
1090/* Optional/variable size superblock sections: */
1091
1092struct bch_sb_field {
1093 __u64 _data[0];
1094 __le32 u64s;
1095 __le32 type;
1096};
1097
25be2e5d
KO
1098#define BCH_SB_FIELDS() \
1099 x(journal, 0) \
1100 x(members, 1) \
1101 x(crypt, 2) \
1102 x(replicas_v0, 3) \
1103 x(quota, 4) \
1104 x(disk_groups, 5) \
1105 x(clean, 6) \
1106 x(replicas, 7) \
1107 x(journal_seq_blacklist, 8) \
104c6974
DH
1108 x(journal_v2, 9) \
1109 x(counters, 10)
1c6fdbd8
KO
1110
1111enum bch_sb_field_type {
1112#define x(f, nr) BCH_SB_FIELD_##f = nr,
1113 BCH_SB_FIELDS()
1114#undef x
1115 BCH_SB_FIELD_NR
1116};
1117
25be2e5d
KO
1118/*
1119 * Most superblock fields are replicated in all device's superblocks - a few are
1120 * not:
1121 */
1122#define BCH_SINGLE_DEVICE_SB_FIELDS \
1123 ((1U << BCH_SB_FIELD_journal)| \
1124 (1U << BCH_SB_FIELD_journal_v2))
1125
1c6fdbd8
KO
1126/* BCH_SB_FIELD_journal: */
1127
1128struct bch_sb_field_journal {
1129 struct bch_sb_field field;
1130 __le64 buckets[0];
1131};
1132
25be2e5d
KO
1133struct bch_sb_field_journal_v2 {
1134 struct bch_sb_field field;
1135
1136 struct bch_sb_field_journal_v2_entry {
1137 __le64 start;
1138 __le64 nr;
1139 } d[0];
1140};
1141
1c6fdbd8
KO
1142/* BCH_SB_FIELD_members: */
1143
8b335bae
KO
1144#define BCH_MIN_NR_NBUCKETS (1 << 6)
1145
1c6fdbd8
KO
1146struct bch_member {
1147 __uuid_t uuid;
1148 __le64 nbuckets; /* device size */
1149 __le16 first_bucket; /* index of first bucket used */
1150 __le16 bucket_size; /* sectors */
1151 __le32 pad;
1152 __le64 last_mount; /* time_t */
1153
1154 __le64 flags[2];
1155};
1156
1157LE64_BITMASK(BCH_MEMBER_STATE, struct bch_member, flags[0], 0, 4)
7243498d 1158/* 4-14 unused, was TIER, HAS_(META)DATA, REPLACEMENT */
1c6fdbd8
KO
1159LE64_BITMASK(BCH_MEMBER_DISCARD, struct bch_member, flags[0], 14, 15)
1160LE64_BITMASK(BCH_MEMBER_DATA_ALLOWED, struct bch_member, flags[0], 15, 20)
1161LE64_BITMASK(BCH_MEMBER_GROUP, struct bch_member, flags[0], 20, 28)
1162LE64_BITMASK(BCH_MEMBER_DURABILITY, struct bch_member, flags[0], 28, 30)
c6b2826c
KO
1163LE64_BITMASK(BCH_MEMBER_FREESPACE_INITIALIZED,
1164 struct bch_member, flags[0], 30, 31)
1c6fdbd8 1165
1c6fdbd8
KO
1166#if 0
1167LE64_BITMASK(BCH_MEMBER_NR_READ_ERRORS, struct bch_member, flags[1], 0, 20);
1168LE64_BITMASK(BCH_MEMBER_NR_WRITE_ERRORS,struct bch_member, flags[1], 20, 40);
1169#endif
1170
2436cb9f
KO
1171#define BCH_MEMBER_STATES() \
1172 x(rw, 0) \
1173 x(ro, 1) \
1174 x(failed, 2) \
1175 x(spare, 3)
1176
1c6fdbd8 1177enum bch_member_state {
2436cb9f
KO
1178#define x(t, n) BCH_MEMBER_STATE_##t = n,
1179 BCH_MEMBER_STATES()
1180#undef x
1181 BCH_MEMBER_STATE_NR
1c6fdbd8
KO
1182};
1183
1c6fdbd8
KO
1184struct bch_sb_field_members {
1185 struct bch_sb_field field;
1186 struct bch_member members[0];
1187};
1188
1189/* BCH_SB_FIELD_crypt: */
1190
1191struct nonce {
1192 __le32 d[4];
1193};
1194
1195struct bch_key {
1196 __le64 key[4];
1197};
1198
1199#define BCH_KEY_MAGIC \
1200 (((u64) 'b' << 0)|((u64) 'c' << 8)| \
1201 ((u64) 'h' << 16)|((u64) '*' << 24)| \
1202 ((u64) '*' << 32)|((u64) 'k' << 40)| \
1203 ((u64) 'e' << 48)|((u64) 'y' << 56))
1204
1205struct bch_encrypted_key {
1206 __le64 magic;
1207 struct bch_key key;
1208};
1209
1210/*
1211 * If this field is present in the superblock, it stores an encryption key which
1212 * is used encrypt all other data/metadata. The key will normally be encrypted
1213 * with the key userspace provides, but if encryption has been turned off we'll
1214 * just store the master key unencrypted in the superblock so we can access the
1215 * previously encrypted data.
1216 */
1217struct bch_sb_field_crypt {
1218 struct bch_sb_field field;
1219
1220 __le64 flags;
1221 __le64 kdf_flags;
1222 struct bch_encrypted_key key;
1223};
1224
1225LE64_BITMASK(BCH_CRYPT_KDF_TYPE, struct bch_sb_field_crypt, flags, 0, 4);
1226
1227enum bch_kdf_types {
1228 BCH_KDF_SCRYPT = 0,
1229 BCH_KDF_NR = 1,
1230};
1231
1232/* stored as base 2 log of scrypt params: */
1233LE64_BITMASK(BCH_KDF_SCRYPT_N, struct bch_sb_field_crypt, kdf_flags, 0, 16);
1234LE64_BITMASK(BCH_KDF_SCRYPT_R, struct bch_sb_field_crypt, kdf_flags, 16, 32);
1235LE64_BITMASK(BCH_KDF_SCRYPT_P, struct bch_sb_field_crypt, kdf_flags, 32, 48);
1236
1237/* BCH_SB_FIELD_replicas: */
1238
89fd25be 1239#define BCH_DATA_TYPES() \
822835ff 1240 x(free, 0) \
89fd25be
KO
1241 x(sb, 1) \
1242 x(journal, 2) \
1243 x(btree, 3) \
1244 x(user, 4) \
af4d05c4 1245 x(cached, 5) \
822835ff
KO
1246 x(parity, 6) \
1247 x(stripe, 7) \
1248 x(need_gc_gens, 8) \
1249 x(need_discard, 9)
89fd25be 1250
1c6fdbd8 1251enum bch_data_type {
89fd25be
KO
1252#define x(t, n) BCH_DATA_##t,
1253 BCH_DATA_TYPES()
1254#undef x
1255 BCH_DATA_NR
1c6fdbd8
KO
1256};
1257
822835ff
KO
1258static inline bool data_type_is_empty(enum bch_data_type type)
1259{
1260 switch (type) {
1261 case BCH_DATA_free:
1262 case BCH_DATA_need_gc_gens:
1263 case BCH_DATA_need_discard:
1264 return true;
1265 default:
1266 return false;
1267 }
1268}
1269
1270static inline bool data_type_is_hidden(enum bch_data_type type)
1271{
1272 switch (type) {
1273 case BCH_DATA_sb:
1274 case BCH_DATA_journal:
1275 return true;
1276 default:
1277 return false;
1278 }
1279}
1280
af9d3bc2
KO
1281struct bch_replicas_entry_v0 {
1282 __u8 data_type;
1283 __u8 nr_devs;
1284 __u8 devs[];
fd0c7679 1285} __packed;
af9d3bc2
KO
1286
1287struct bch_sb_field_replicas_v0 {
1288 struct bch_sb_field field;
1289 struct bch_replicas_entry_v0 entries[];
fd0c7679 1290} __packed __aligned(8);
af9d3bc2 1291
1c6fdbd8 1292struct bch_replicas_entry {
7a920560
KO
1293 __u8 data_type;
1294 __u8 nr_devs;
af9d3bc2 1295 __u8 nr_required;
7a920560 1296 __u8 devs[];
fd0c7679 1297} __packed;
1c6fdbd8 1298
22502ac2
KO
1299#define replicas_entry_bytes(_i) \
1300 (offsetof(typeof(*(_i)), devs) + (_i)->nr_devs)
1301
1c6fdbd8
KO
1302struct bch_sb_field_replicas {
1303 struct bch_sb_field field;
1304 struct bch_replicas_entry entries[];
fd0c7679 1305} __packed __aligned(8);
1c6fdbd8
KO
1306
1307/* BCH_SB_FIELD_quota: */
1308
1309struct bch_sb_quota_counter {
1310 __le32 timelimit;
1311 __le32 warnlimit;
1312};
1313
1314struct bch_sb_quota_type {
1315 __le64 flags;
1316 struct bch_sb_quota_counter c[Q_COUNTERS];
1317};
1318
1319struct bch_sb_field_quota {
1320 struct bch_sb_field field;
1321 struct bch_sb_quota_type q[QTYP_NR];
fd0c7679 1322} __packed __aligned(8);
1c6fdbd8
KO
1323
1324/* BCH_SB_FIELD_disk_groups: */
1325
1326#define BCH_SB_LABEL_SIZE 32
1327
1328struct bch_disk_group {
1329 __u8 label[BCH_SB_LABEL_SIZE];
1330 __le64 flags[2];
fd0c7679 1331} __packed __aligned(8);
1c6fdbd8
KO
1332
1333LE64_BITMASK(BCH_GROUP_DELETED, struct bch_disk_group, flags[0], 0, 1)
1334LE64_BITMASK(BCH_GROUP_DATA_ALLOWED, struct bch_disk_group, flags[0], 1, 6)
1335LE64_BITMASK(BCH_GROUP_PARENT, struct bch_disk_group, flags[0], 6, 24)
1336
1337struct bch_sb_field_disk_groups {
1338 struct bch_sb_field field;
1339 struct bch_disk_group entries[0];
fd0c7679 1340} __packed __aligned(8);
1c6fdbd8 1341
104c6974
DH
1342/* BCH_SB_FIELD_counters */
1343
674cfc26
KO
1344#define BCH_PERSISTENT_COUNTERS() \
1345 x(io_read, 0) \
1346 x(io_write, 1) \
1347 x(io_move, 2) \
1348 x(bucket_invalidate, 3) \
1349 x(bucket_discard, 4) \
1350 x(bucket_alloc, 5) \
1351 x(bucket_alloc_fail, 6) \
1352 x(btree_cache_scan, 7) \
1353 x(btree_cache_reap, 8) \
1354 x(btree_cache_cannibalize, 9) \
1355 x(btree_cache_cannibalize_lock, 10) \
1356 x(btree_cache_cannibalize_lock_fail, 11) \
1357 x(btree_cache_cannibalize_unlock, 12) \
1358 x(btree_node_write, 13) \
1359 x(btree_node_read, 14) \
1360 x(btree_node_compact, 15) \
1361 x(btree_node_merge, 16) \
1362 x(btree_node_split, 17) \
1363 x(btree_node_rewrite, 18) \
1364 x(btree_node_alloc, 19) \
1365 x(btree_node_free, 20) \
1366 x(btree_node_set_root, 21) \
1367 x(btree_path_relock_fail, 22) \
1368 x(btree_path_upgrade_fail, 23) \
1369 x(btree_reserve_get_fail, 24) \
1370 x(journal_entry_full, 25) \
1371 x(journal_full, 26) \
1372 x(journal_reclaim_finish, 27) \
1373 x(journal_reclaim_start, 28) \
1374 x(journal_write, 29) \
1375 x(read_promote, 30) \
1376 x(read_bounce, 31) \
1377 x(read_split, 33) \
1378 x(read_retry, 32) \
1379 x(read_reuse_race, 34) \
1380 x(move_extent_read, 35) \
1381 x(move_extent_write, 36) \
1382 x(move_extent_finish, 37) \
1383 x(move_extent_fail, 38) \
1384 x(move_extent_alloc_mem_fail, 39) \
1385 x(copygc, 40) \
1386 x(copygc_wait, 41) \
1387 x(gc_gens_end, 42) \
1388 x(gc_gens_start, 43) \
1389 x(trans_blocked_journal_reclaim, 44) \
1390 x(trans_restart_btree_node_reused, 45) \
1391 x(trans_restart_btree_node_split, 46) \
1392 x(trans_restart_fault_inject, 47) \
1393 x(trans_restart_iter_upgrade, 48) \
1394 x(trans_restart_journal_preres_get, 49) \
1395 x(trans_restart_journal_reclaim, 50) \
1396 x(trans_restart_journal_res_get, 51) \
1397 x(trans_restart_key_cache_key_realloced, 52) \
1398 x(trans_restart_key_cache_raced, 53) \
1399 x(trans_restart_mark_replicas, 54) \
1400 x(trans_restart_mem_realloced, 55) \
1401 x(trans_restart_memory_allocation_failure, 56) \
1402 x(trans_restart_relock, 57) \
1403 x(trans_restart_relock_after_fill, 58) \
1404 x(trans_restart_relock_key_cache_fill, 59) \
1405 x(trans_restart_relock_next_node, 60) \
1406 x(trans_restart_relock_parent_for_fill, 61) \
1407 x(trans_restart_relock_path, 62) \
1408 x(trans_restart_relock_path_intent, 63) \
1409 x(trans_restart_too_many_iters, 64) \
1410 x(trans_restart_traverse, 65) \
1411 x(trans_restart_upgrade, 66) \
1412 x(trans_restart_would_deadlock, 67) \
1413 x(trans_restart_would_deadlock_write, 68) \
1414 x(trans_restart_injected, 69) \
1415 x(trans_restart_key_cache_upgrade, 70) \
1416 x(trans_traverse_all, 71) \
1417 x(transaction_commit, 72) \
33bd5d06 1418 x(write_super, 73) \
920e69bc
KO
1419 x(trans_restart_would_deadlock_recursion_limit, 74) \
1420 x(trans_restart_write_buffer_flush, 75)
104c6974
DH
1421
1422enum bch_persistent_counters {
1423#define x(t, n, ...) BCH_COUNTER_##t,
1424 BCH_PERSISTENT_COUNTERS()
1425#undef x
1426 BCH_COUNTER_NR
1427};
1428
1429struct bch_sb_field_counters {
1430 struct bch_sb_field field;
1431 __le64 d[0];
1432};
1433
1c6fdbd8
KO
1434/*
1435 * On clean shutdown, store btree roots and current journal sequence number in
1436 * the superblock:
1437 */
1438struct jset_entry {
1439 __le16 u64s;
1440 __u8 btree_id;
1441 __u8 level;
1442 __u8 type; /* designates what this jset holds */
1443 __u8 pad[3];
1444
1445 union {
1446 struct bkey_i start[0];
1447 __u64 _data[0];
1448 };
1449};
1450
1451struct bch_sb_field_clean {
1452 struct bch_sb_field field;
1453
1454 __le32 flags;
2abe5420
KO
1455 __le16 _read_clock; /* no longer used */
1456 __le16 _write_clock;
1c6fdbd8
KO
1457 __le64 journal_seq;
1458
1459 union {
1460 struct jset_entry start[0];
1461 __u64 _data[0];
1462 };
1463};
1464
1dd7f9d9
KO
1465struct journal_seq_blacklist_entry {
1466 __le64 start;
1467 __le64 end;
1468};
1469
1470struct bch_sb_field_journal_seq_blacklist {
1471 struct bch_sb_field field;
1472
1473 union {
1474 struct journal_seq_blacklist_entry start[0];
1475 __u64 _data[0];
1476 };
1477};
1478
1c6fdbd8
KO
1479/* Superblock: */
1480
1481/*
26609b61
KO
1482 * New versioning scheme:
1483 * One common version number for all on disk data structures - superblock, btree
1484 * nodes, journal entries
1c6fdbd8 1485 */
26609b61
KO
1486#define BCH_JSET_VERSION_OLD 2
1487#define BCH_BSET_VERSION_OLD 3
1488
74b33393
KO
1489#define BCH_METADATA_VERSIONS() \
1490 x(bkey_renumber, 10) \
1491 x(inode_btree_change, 11) \
1492 x(snapshot, 12) \
1493 x(inode_backpointers, 13) \
1494 x(btree_ptr_sectors_written, 14) \
1495 x(snapshot_2, 15) \
1496 x(reflink_p_fix, 16) \
1497 x(subvol_dirent, 17) \
c6b2826c
KO
1498 x(inode_v2, 18) \
1499 x(freespace, 19) \
822835ff 1500 x(alloc_v4, 20) \
a8c752bb
KO
1501 x(new_data_types, 21) \
1502 x(backpointers, 22)
74b33393 1503
26609b61 1504enum bcachefs_metadata_version {
74b33393
KO
1505 bcachefs_metadata_version_min = 9,
1506#define x(t, n) bcachefs_metadata_version_##t = n,
1507 BCH_METADATA_VERSIONS()
1508#undef x
1509 bcachefs_metadata_version_max
26609b61 1510};
1c6fdbd8 1511
26609b61 1512#define bcachefs_metadata_version_current (bcachefs_metadata_version_max - 1)
1c6fdbd8
KO
1513
1514#define BCH_SB_SECTOR 8
1515#define BCH_SB_MEMBERS_MAX 64 /* XXX kill */
1516
1517struct bch_sb_layout {
1518 __uuid_t magic; /* bcachefs superblock UUID */
1519 __u8 layout_type;
1520 __u8 sb_max_size_bits; /* base 2 of 512 byte sectors */
1521 __u8 nr_superblocks;
1522 __u8 pad[5];
1523 __le64 sb_offset[61];
fd0c7679 1524} __packed __aligned(8);
1c6fdbd8
KO
1525
1526#define BCH_SB_LAYOUT_SECTOR 7
1527
1528/*
1529 * @offset - sector where this sb was written
1530 * @version - on disk format version
26609b61
KO
1531 * @version_min - Oldest metadata version this filesystem contains; so we can
1532 * safely drop compatibility code and refuse to mount filesystems
1533 * we'd need it for
e1538212 1534 * @magic - identifies as a bcachefs superblock (BCHFS_MAGIC)
1c6fdbd8
KO
1535 * @seq - incremented each time superblock is written
1536 * @uuid - used for generating various magic numbers and identifying
1537 * member devices, never changes
1538 * @user_uuid - user visible UUID, may be changed
1539 * @label - filesystem label
1540 * @seq - identifies most recent superblock, incremented each time
1541 * superblock is written
1542 * @features - enabled incompatible features
1543 */
1544struct bch_sb {
1545 struct bch_csum csum;
1546 __le16 version;
1547 __le16 version_min;
1548 __le16 pad[2];
1549 __uuid_t magic;
1550 __uuid_t uuid;
1551 __uuid_t user_uuid;
1552 __u8 label[BCH_SB_LABEL_SIZE];
1553 __le64 offset;
1554 __le64 seq;
1555
1556 __le16 block_size;
1557 __u8 dev_idx;
1558 __u8 nr_devices;
1559 __le32 u64s;
1560
1561 __le64 time_base_lo;
1562 __le32 time_base_hi;
1563 __le32 time_precision;
1564
1565 __le64 flags[8];
1566 __le64 features[2];
1567 __le64 compat[2];
1568
1569 struct bch_sb_layout layout;
1570
1571 union {
1572 struct bch_sb_field start[0];
1573 __le64 _data[0];
1574 };
fd0c7679 1575} __packed __aligned(8);
1c6fdbd8
KO
1576
1577/*
1578 * Flags:
1579 * BCH_SB_INITALIZED - set on first mount
1580 * BCH_SB_CLEAN - did we shut down cleanly? Just a hint, doesn't affect
1581 * behaviour of mount/recovery path:
1582 * BCH_SB_INODE_32BIT - limit inode numbers to 32 bits
1583 * BCH_SB_128_BIT_MACS - 128 bit macs instead of 80
1584 * BCH_SB_ENCRYPTION_TYPE - if nonzero encryption is enabled; overrides
1585 * DATA/META_CSUM_TYPE. Also indicates encryption
1586 * algorithm in use, if/when we get more than one
1587 */
1588
1589LE16_BITMASK(BCH_SB_BLOCK_SIZE, struct bch_sb, block_size, 0, 16);
1590
1591LE64_BITMASK(BCH_SB_INITIALIZED, struct bch_sb, flags[0], 0, 1);
1592LE64_BITMASK(BCH_SB_CLEAN, struct bch_sb, flags[0], 1, 2);
1593LE64_BITMASK(BCH_SB_CSUM_TYPE, struct bch_sb, flags[0], 2, 8);
1594LE64_BITMASK(BCH_SB_ERROR_ACTION, struct bch_sb, flags[0], 8, 12);
1595
1596LE64_BITMASK(BCH_SB_BTREE_NODE_SIZE, struct bch_sb, flags[0], 12, 28);
1597
1598LE64_BITMASK(BCH_SB_GC_RESERVE, struct bch_sb, flags[0], 28, 33);
1599LE64_BITMASK(BCH_SB_ROOT_RESERVE, struct bch_sb, flags[0], 33, 40);
1600
1601LE64_BITMASK(BCH_SB_META_CSUM_TYPE, struct bch_sb, flags[0], 40, 44);
1602LE64_BITMASK(BCH_SB_DATA_CSUM_TYPE, struct bch_sb, flags[0], 44, 48);
1603
1604LE64_BITMASK(BCH_SB_META_REPLICAS_WANT, struct bch_sb, flags[0], 48, 52);
1605LE64_BITMASK(BCH_SB_DATA_REPLICAS_WANT, struct bch_sb, flags[0], 52, 56);
1606
1607LE64_BITMASK(BCH_SB_POSIX_ACL, struct bch_sb, flags[0], 56, 57);
1608LE64_BITMASK(BCH_SB_USRQUOTA, struct bch_sb, flags[0], 57, 58);
1609LE64_BITMASK(BCH_SB_GRPQUOTA, struct bch_sb, flags[0], 58, 59);
1610LE64_BITMASK(BCH_SB_PRJQUOTA, struct bch_sb, flags[0], 59, 60);
1611
0bc166ff 1612LE64_BITMASK(BCH_SB_HAS_ERRORS, struct bch_sb, flags[0], 60, 61);
aae15aaf 1613LE64_BITMASK(BCH_SB_HAS_TOPOLOGY_ERRORS,struct bch_sb, flags[0], 61, 62);
0bc166ff 1614
7d6f07ed 1615LE64_BITMASK(BCH_SB_BIG_ENDIAN, struct bch_sb, flags[0], 62, 63);
36b8372b 1616
1c6fdbd8
KO
1617LE64_BITMASK(BCH_SB_STR_HASH_TYPE, struct bch_sb, flags[1], 0, 4);
1618LE64_BITMASK(BCH_SB_COMPRESSION_TYPE, struct bch_sb, flags[1], 4, 8);
1619LE64_BITMASK(BCH_SB_INODE_32BIT, struct bch_sb, flags[1], 8, 9);
1620
1621LE64_BITMASK(BCH_SB_128_BIT_MACS, struct bch_sb, flags[1], 9, 10);
1622LE64_BITMASK(BCH_SB_ENCRYPTION_TYPE, struct bch_sb, flags[1], 10, 14);
1623
1624/*
1625 * Max size of an extent that may require bouncing to read or write
1626 * (checksummed, compressed): 64k
1627 */
1628LE64_BITMASK(BCH_SB_ENCODED_EXTENT_MAX_BITS,
1629 struct bch_sb, flags[1], 14, 20);
1630
1631LE64_BITMASK(BCH_SB_META_REPLICAS_REQ, struct bch_sb, flags[1], 20, 24);
1632LE64_BITMASK(BCH_SB_DATA_REPLICAS_REQ, struct bch_sb, flags[1], 24, 28);
1633
1634LE64_BITMASK(BCH_SB_PROMOTE_TARGET, struct bch_sb, flags[1], 28, 40);
1635LE64_BITMASK(BCH_SB_FOREGROUND_TARGET, struct bch_sb, flags[1], 40, 52);
1636LE64_BITMASK(BCH_SB_BACKGROUND_TARGET, struct bch_sb, flags[1], 52, 64);
1637
1638LE64_BITMASK(BCH_SB_BACKGROUND_COMPRESSION_TYPE,
1639 struct bch_sb, flags[2], 0, 4);
a50ed7c8 1640LE64_BITMASK(BCH_SB_GC_RESERVE_BYTES, struct bch_sb, flags[2], 4, 64);
1c6fdbd8 1641
cd575ddf 1642LE64_BITMASK(BCH_SB_ERASURE_CODE, struct bch_sb, flags[3], 0, 16);
d042b040 1643LE64_BITMASK(BCH_SB_METADATA_TARGET, struct bch_sb, flags[3], 16, 28);
b282a74f 1644LE64_BITMASK(BCH_SB_SHARD_INUMS, struct bch_sb, flags[3], 28, 29);
996fb577 1645LE64_BITMASK(BCH_SB_INODES_USE_KEY_CACHE,struct bch_sb, flags[3], 29, 30);
2430e72f
KO
1646LE64_BITMASK(BCH_SB_JOURNAL_FLUSH_DELAY,struct bch_sb, flags[3], 30, 62);
1647LE64_BITMASK(BCH_SB_JOURNAL_FLUSH_DISABLED,struct bch_sb, flags[3], 62, 63);
1648LE64_BITMASK(BCH_SB_JOURNAL_RECLAIM_DELAY,struct bch_sb, flags[4], 0, 32);
fb64f3fd 1649LE64_BITMASK(BCH_SB_JOURNAL_TRANSACTION_NAMES,struct bch_sb, flags[4], 32, 33);
920e69bc 1650LE64_BITMASK(BCH_SB_WRITE_BUFFER_SIZE, struct bch_sb, flags[4], 34, 54);
cd575ddf 1651
1c3ff72c
KO
1652/*
1653 * Features:
1654 *
1655 * journal_seq_blacklist_v3: gates BCH_SB_FIELD_journal_seq_blacklist
1656 * reflink: gates KEY_TYPE_reflink
1657 * inline_data: gates KEY_TYPE_inline_data
6404dcc9 1658 * new_siphash: gates BCH_STR_HASH_siphash
bcd6f3e0 1659 * new_extent_overwrite: gates BTREE_NODE_NEW_EXTENT_OVERWRITE
1c3ff72c
KO
1660 */
1661#define BCH_SB_FEATURES() \
1662 x(lz4, 0) \
1663 x(gzip, 1) \
1664 x(zstd, 2) \
1665 x(atomic_nlink, 3) \
1666 x(ec, 4) \
1667 x(journal_seq_blacklist_v3, 5) \
1668 x(reflink, 6) \
1669 x(new_siphash, 7) \
bcd6f3e0 1670 x(inline_data, 8) \
ab05de4c 1671 x(new_extent_overwrite, 9) \
548b3d20 1672 x(incompressible, 10) \
e3e464ac 1673 x(btree_ptr_v2, 11) \
6357d607 1674 x(extents_above_btree_updates, 12) \
801a3de6 1675 x(btree_updates_journalled, 13) \
a3e72262 1676 x(reflink_inline_data, 14) \
adbcada4 1677 x(new_varint, 15) \
7f4e1d5d 1678 x(journal_no_flush, 16) \
8042b5b7
KO
1679 x(alloc_v2, 17) \
1680 x(extents_across_btree_nodes, 18)
1681
1682#define BCH_SB_FEATURES_ALWAYS \
1683 ((1ULL << BCH_FEATURE_new_extent_overwrite)| \
1684 (1ULL << BCH_FEATURE_extents_above_btree_updates)|\
1685 (1ULL << BCH_FEATURE_btree_updates_journalled)|\
73590619 1686 (1ULL << BCH_FEATURE_alloc_v2)|\
8042b5b7 1687 (1ULL << BCH_FEATURE_extents_across_btree_nodes))
1c3ff72c 1688
b807a0c8 1689#define BCH_SB_FEATURES_ALL \
8042b5b7
KO
1690 (BCH_SB_FEATURES_ALWAYS| \
1691 (1ULL << BCH_FEATURE_new_siphash)| \
e3e464ac 1692 (1ULL << BCH_FEATURE_btree_ptr_v2)| \
adbcada4 1693 (1ULL << BCH_FEATURE_new_varint)| \
73590619 1694 (1ULL << BCH_FEATURE_journal_no_flush))
b807a0c8 1695
1c3ff72c
KO
1696enum bch_sb_feature {
1697#define x(f, n) BCH_FEATURE_##f,
1698 BCH_SB_FEATURES()
1699#undef x
c258f28e 1700 BCH_FEATURE_NR,
1c6fdbd8
KO
1701};
1702
19dd3172
KO
1703#define BCH_SB_COMPAT() \
1704 x(alloc_info, 0) \
1705 x(alloc_metadata, 1) \
1706 x(extents_above_btree_updates_done, 2) \
1707 x(bformat_overflow_done, 3)
1708
1df42b57 1709enum bch_sb_compat {
19dd3172
KO
1710#define x(f, n) BCH_COMPAT_##f,
1711 BCH_SB_COMPAT()
1712#undef x
1713 BCH_COMPAT_NR,
1df42b57
KO
1714};
1715
1c6fdbd8
KO
1716/* options: */
1717
1718#define BCH_REPLICAS_MAX 4U
1719
ffb7c3d3
KO
1720#define BCH_BKEY_PTRS_MAX 16U
1721
2436cb9f
KO
1722#define BCH_ERROR_ACTIONS() \
1723 x(continue, 0) \
1724 x(ro, 1) \
1725 x(panic, 2)
1726
1c6fdbd8 1727enum bch_error_actions {
2436cb9f
KO
1728#define x(t, n) BCH_ON_ERROR_##t = n,
1729 BCH_ERROR_ACTIONS()
1730#undef x
1731 BCH_ON_ERROR_NR
1c6fdbd8
KO
1732};
1733
6404dcc9
KO
1734#define BCH_STR_HASH_TYPES() \
1735 x(crc32c, 0) \
1736 x(crc64, 1) \
1737 x(siphash_old, 2) \
1738 x(siphash, 3)
1739
73501ab8 1740enum bch_str_hash_type {
6404dcc9
KO
1741#define x(t, n) BCH_STR_HASH_##t = n,
1742 BCH_STR_HASH_TYPES()
1743#undef x
1744 BCH_STR_HASH_NR
73501ab8
KO
1745};
1746
2436cb9f
KO
1747#define BCH_STR_HASH_OPTS() \
1748 x(crc32c, 0) \
1749 x(crc64, 1) \
1750 x(siphash, 2)
1751
73501ab8 1752enum bch_str_hash_opts {
2436cb9f
KO
1753#define x(t, n) BCH_STR_HASH_OPT_##t = n,
1754 BCH_STR_HASH_OPTS()
1755#undef x
1756 BCH_STR_HASH_OPT_NR
1c6fdbd8
KO
1757};
1758
6404dcc9
KO
1759#define BCH_CSUM_TYPES() \
1760 x(none, 0) \
1761 x(crc32c_nonzero, 1) \
1762 x(crc64_nonzero, 2) \
1763 x(chacha20_poly1305_80, 3) \
1764 x(chacha20_poly1305_128, 4) \
1765 x(crc32c, 5) \
1766 x(crc64, 6) \
1767 x(xxhash, 7)
1768
1c3ff72c 1769enum bch_csum_type {
6404dcc9
KO
1770#define x(t, n) BCH_CSUM_##t = n,
1771 BCH_CSUM_TYPES()
1772#undef x
1773 BCH_CSUM_NR
1c3ff72c
KO
1774};
1775
1776static const unsigned bch_crc_bytes[] = {
6404dcc9
KO
1777 [BCH_CSUM_none] = 0,
1778 [BCH_CSUM_crc32c_nonzero] = 4,
1779 [BCH_CSUM_crc32c] = 4,
1780 [BCH_CSUM_crc64_nonzero] = 8,
1781 [BCH_CSUM_crc64] = 8,
1782 [BCH_CSUM_xxhash] = 8,
1783 [BCH_CSUM_chacha20_poly1305_80] = 10,
1784 [BCH_CSUM_chacha20_poly1305_128] = 16,
1c3ff72c
KO
1785};
1786
1787static inline _Bool bch2_csum_type_is_encryption(enum bch_csum_type type)
1788{
1789 switch (type) {
6404dcc9
KO
1790 case BCH_CSUM_chacha20_poly1305_80:
1791 case BCH_CSUM_chacha20_poly1305_128:
1c3ff72c
KO
1792 return true;
1793 default:
1794 return false;
1795 }
1796}
1797
2436cb9f
KO
1798#define BCH_CSUM_OPTS() \
1799 x(none, 0) \
1800 x(crc32c, 1) \
41e63382 1801 x(crc64, 2) \
1802 x(xxhash, 3)
2436cb9f 1803
1c3ff72c 1804enum bch_csum_opts {
2436cb9f
KO
1805#define x(t, n) BCH_CSUM_OPT_##t = n,
1806 BCH_CSUM_OPTS()
1807#undef x
1808 BCH_CSUM_OPT_NR
1c3ff72c
KO
1809};
1810
1c6fdbd8 1811#define BCH_COMPRESSION_TYPES() \
ab05de4c
KO
1812 x(none, 0) \
1813 x(lz4_old, 1) \
1814 x(gzip, 2) \
1815 x(lz4, 3) \
1816 x(zstd, 4) \
1817 x(incompressible, 5)
1c6fdbd8 1818
1c3ff72c 1819enum bch_compression_type {
2436cb9f 1820#define x(t, n) BCH_COMPRESSION_TYPE_##t = n,
1c6fdbd8 1821 BCH_COMPRESSION_TYPES()
1c3ff72c
KO
1822#undef x
1823 BCH_COMPRESSION_TYPE_NR
1824};
1825
1826#define BCH_COMPRESSION_OPTS() \
1827 x(none, 0) \
1828 x(lz4, 1) \
1829 x(gzip, 2) \
1830 x(zstd, 3)
1831
1832enum bch_compression_opts {
2436cb9f 1833#define x(t, n) BCH_COMPRESSION_OPT_##t = n,
1c3ff72c 1834 BCH_COMPRESSION_OPTS()
1c6fdbd8
KO
1835#undef x
1836 BCH_COMPRESSION_OPT_NR
1837};
1838
1839/*
1840 * Magic numbers
1841 *
1842 * The various other data structures have their own magic numbers, which are
1843 * xored with the first part of the cache set's UUID
1844 */
1845
1846#define BCACHE_MAGIC \
1847 UUID_INIT(0xc68573f6, 0x4e1a, 0x45ca, \
1848 0x82, 0x65, 0xf5, 0x7f, 0x48, 0xba, 0x6d, 0x81)
1849#define BCHFS_MAGIC \
1850 UUID_INIT(0xc68573f6, 0x66ce, 0x90a9, \
1851 0xd9, 0x6a, 0x60, 0xcf, 0x80, 0x3d, 0xf7, 0xef)
1852
1853#define BCACHEFS_STATFS_MAGIC 0xca451a4e
1854
1855#define JSET_MAGIC __cpu_to_le64(0x245235c1a3625032ULL)
1856#define BSET_MAGIC __cpu_to_le64(0x90135c78b99e07f5ULL)
1857
1858static inline __le64 __bch2_sb_magic(struct bch_sb *sb)
1859{
1860 __le64 ret;
a1019576 1861
1c6fdbd8
KO
1862 memcpy(&ret, &sb->uuid, sizeof(ret));
1863 return ret;
1864}
1865
1866static inline __u64 __jset_magic(struct bch_sb *sb)
1867{
1868 return __le64_to_cpu(__bch2_sb_magic(sb) ^ JSET_MAGIC);
1869}
1870
1871static inline __u64 __bset_magic(struct bch_sb *sb)
1872{
1873 return __le64_to_cpu(__bch2_sb_magic(sb) ^ BSET_MAGIC);
1874}
1875
1876/* Journal */
1877
1c6fdbd8
KO
1878#define JSET_KEYS_U64s (sizeof(struct jset_entry) / sizeof(__u64))
1879
1880#define BCH_JSET_ENTRY_TYPES() \
1881 x(btree_keys, 0) \
1882 x(btree_root, 1) \
1883 x(prio_ptrs, 2) \
1884 x(blacklist, 3) \
2c5af169 1885 x(blacklist_v2, 4) \
3577df5f 1886 x(usage, 5) \
2abe5420 1887 x(data_usage, 6) \
180fb49d 1888 x(clock, 7) \
fb64f3fd 1889 x(dev_usage, 8) \
cb685ce7
KO
1890 x(log, 9) \
1891 x(overwrite, 10)
1c6fdbd8
KO
1892
1893enum {
1894#define x(f, nr) BCH_JSET_ENTRY_##f = nr,
1895 BCH_JSET_ENTRY_TYPES()
1896#undef x
1897 BCH_JSET_ENTRY_NR
1898};
1899
1900/*
1901 * Journal sequence numbers can be blacklisted: bsets record the max sequence
1902 * number of all the journal entries they contain updates for, so that on
1903 * recovery we can ignore those bsets that contain index updates newer that what
1904 * made it into the journal.
1905 *
1906 * This means that we can't reuse that journal_seq - we have to skip it, and
1907 * then record that we skipped it so that the next time we crash and recover we
1908 * don't think there was a missing journal entry.
1909 */
1910struct jset_entry_blacklist {
1911 struct jset_entry entry;
1912 __le64 seq;
1913};
1914
1915struct jset_entry_blacklist_v2 {
1916 struct jset_entry entry;
1917 __le64 start;
1918 __le64 end;
1919};
1920
528b18e6
KO
1921#define BCH_FS_USAGE_TYPES() \
1922 x(reserved, 0) \
1923 x(inodes, 1) \
1924 x(key_version, 2)
1925
2c5af169 1926enum {
528b18e6
KO
1927#define x(f, nr) BCH_FS_USAGE_##f = nr,
1928 BCH_FS_USAGE_TYPES()
1929#undef x
1930 BCH_FS_USAGE_NR
2c5af169
KO
1931};
1932
1933struct jset_entry_usage {
1934 struct jset_entry entry;
3577df5f 1935 __le64 v;
fd0c7679 1936} __packed;
3577df5f
KO
1937
1938struct jset_entry_data_usage {
1939 struct jset_entry entry;
1940 __le64 v;
2c5af169 1941 struct bch_replicas_entry r;
fd0c7679 1942} __packed;
2c5af169 1943
2abe5420
KO
1944struct jset_entry_clock {
1945 struct jset_entry entry;
1946 __u8 rw;
1947 __u8 pad[7];
1948 __le64 time;
fd0c7679 1949} __packed;
2abe5420 1950
180fb49d
KO
1951struct jset_entry_dev_usage_type {
1952 __le64 buckets;
1953 __le64 sectors;
1954 __le64 fragmented;
fd0c7679 1955} __packed;
180fb49d
KO
1956
1957struct jset_entry_dev_usage {
1958 struct jset_entry entry;
1959 __le32 dev;
1960 __u32 pad;
1961
1962 __le64 buckets_ec;
822835ff 1963 __le64 _buckets_unavailable; /* No longer used */
180fb49d
KO
1964
1965 struct jset_entry_dev_usage_type d[];
fd0c7679 1966} __packed;
180fb49d 1967
528b18e6
KO
1968static inline unsigned jset_entry_dev_usage_nr_types(struct jset_entry_dev_usage *u)
1969{
1970 return (vstruct_bytes(&u->entry) - sizeof(struct jset_entry_dev_usage)) /
1971 sizeof(struct jset_entry_dev_usage_type);
1972}
1973
fb64f3fd
KO
1974struct jset_entry_log {
1975 struct jset_entry entry;
1976 u8 d[];
fd0c7679 1977} __packed;
fb64f3fd 1978
1c6fdbd8
KO
1979/*
1980 * On disk format for a journal entry:
1981 * seq is monotonically increasing; every journal entry has its own unique
1982 * sequence number.
1983 *
1984 * last_seq is the oldest journal entry that still has keys the btree hasn't
1985 * flushed to disk yet.
1986 *
1987 * version is for on disk format changes.
1988 */
1989struct jset {
1990 struct bch_csum csum;
1991
1992 __le64 magic;
1993 __le64 seq;
1994 __le32 version;
1995 __le32 flags;
1996
1997 __le32 u64s; /* size of d[] in u64s */
1998
1999 __u8 encrypted_start[0];
2000
2abe5420
KO
2001 __le16 _read_clock; /* no longer used */
2002 __le16 _write_clock;
1c6fdbd8
KO
2003
2004 /* Sequence number of oldest dirty journal entry */
2005 __le64 last_seq;
2006
2007
2008 union {
2009 struct jset_entry start[0];
2010 __u64 _data[0];
2011 };
fd0c7679 2012} __packed __aligned(8);
1c6fdbd8
KO
2013
2014LE32_BITMASK(JSET_CSUM_TYPE, struct jset, flags, 0, 4);
2015LE32_BITMASK(JSET_BIG_ENDIAN, struct jset, flags, 4, 5);
adbcada4 2016LE32_BITMASK(JSET_NO_FLUSH, struct jset, flags, 5, 6);
1c6fdbd8 2017
8b335bae 2018#define BCH_JOURNAL_BUCKETS_MIN 8
1c6fdbd8
KO
2019
2020/* Btree: */
2021
41f8b09e 2022#define BCH_BTREE_IDS() \
a8c752bb
KO
2023 x(extents, 0) \
2024 x(inodes, 1) \
2025 x(dirents, 2) \
2026 x(xattrs, 3) \
2027 x(alloc, 4) \
2028 x(quotas, 5) \
2029 x(stripes, 6) \
2030 x(reflink, 7) \
2031 x(subvolumes, 8) \
2032 x(snapshots, 9) \
2033 x(lru, 10) \
2034 x(freespace, 11) \
2035 x(need_discard, 12) \
2036 x(backpointers, 13)
1c6fdbd8
KO
2037
2038enum btree_id {
41f8b09e 2039#define x(kwd, val) BTREE_ID_##kwd = val,
26609b61
KO
2040 BCH_BTREE_IDS()
2041#undef x
1c6fdbd8
KO
2042 BTREE_ID_NR
2043};
2044
1c6fdbd8
KO
2045#define BTREE_MAX_DEPTH 4U
2046
2047/* Btree nodes */
2048
1c6fdbd8
KO
2049/*
2050 * Btree nodes
2051 *
2052 * On disk a btree node is a list/log of these; within each set the keys are
2053 * sorted
2054 */
2055struct bset {
2056 __le64 seq;
2057
2058 /*
2059 * Highest journal entry this bset contains keys for.
2060 * If on recovery we don't see that journal entry, this bset is ignored:
2061 * this allows us to preserve the order of all index updates after a
2062 * crash, since the journal records a total order of all index updates
2063 * and anything that didn't make it to the journal doesn't get used.
2064 */
2065 __le64 journal_seq;
2066
2067 __le32 flags;
2068 __le16 version;
2069 __le16 u64s; /* count of d[] in u64s */
2070
2071 union {
2072 struct bkey_packed start[0];
2073 __u64 _data[0];
2074 };
fd0c7679 2075} __packed __aligned(8);
1c6fdbd8
KO
2076
2077LE32_BITMASK(BSET_CSUM_TYPE, struct bset, flags, 0, 4);
2078
2079LE32_BITMASK(BSET_BIG_ENDIAN, struct bset, flags, 4, 5);
2080LE32_BITMASK(BSET_SEPARATE_WHITEOUTS,
2081 struct bset, flags, 5, 6);
2082
e719fc34
KO
2083/* Sector offset within the btree node: */
2084LE32_BITMASK(BSET_OFFSET, struct bset, flags, 16, 32);
2085
1c6fdbd8
KO
2086struct btree_node {
2087 struct bch_csum csum;
2088 __le64 magic;
2089
2090 /* this flags field is encrypted, unlike bset->flags: */
2091 __le64 flags;
2092
2093 /* Closed interval: */
2094 struct bpos min_key;
2095 struct bpos max_key;
e751c01a 2096 struct bch_extent_ptr _ptr; /* not used anymore */
1c6fdbd8
KO
2097 struct bkey_format format;
2098
2099 union {
2100 struct bset keys;
2101 struct {
2102 __u8 pad[22];
2103 __le16 u64s;
2104 __u64 _data[0];
2105
2106 };
2107 };
fd0c7679 2108} __packed __aligned(8);
1c6fdbd8
KO
2109
2110LE64_BITMASK(BTREE_NODE_ID, struct btree_node, flags, 0, 4);
2111LE64_BITMASK(BTREE_NODE_LEVEL, struct btree_node, flags, 4, 8);
bcd6f3e0
KO
2112LE64_BITMASK(BTREE_NODE_NEW_EXTENT_OVERWRITE,
2113 struct btree_node, flags, 8, 9);
2114/* 9-32 unused */
1c6fdbd8
KO
2115LE64_BITMASK(BTREE_NODE_SEQ, struct btree_node, flags, 32, 64);
2116
2117struct btree_node_entry {
2118 struct bch_csum csum;
2119
2120 union {
2121 struct bset keys;
2122 struct {
2123 __u8 pad[22];
2124 __le16 u64s;
2125 __u64 _data[0];
2126
2127 };
2128 };
fd0c7679 2129} __packed __aligned(8);
1c6fdbd8
KO
2130
2131#endif /* _BCACHEFS_FORMAT_H */