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