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