bcachefs: don't lose errors from iterators that have been freed
[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
KO
78#include <linux/uuid.h>
79
80#ifdef __KERNEL__
81typedef uuid_t __uuid_t;
82#endif
83
84#define LE_BITMASK(_bits, name, type, field, offset, end) \
85static const unsigned name##_OFFSET = offset; \
86static const unsigned name##_BITS = (end - offset); \
87static const __u##_bits name##_MAX = (1ULL << (end - offset)) - 1; \
88 \
89static inline __u64 name(const type *k) \
90{ \
91 return (__le##_bits##_to_cpu(k->field) >> offset) & \
92 ~(~0ULL << (end - offset)); \
93} \
94 \
95static inline void SET_##name(type *k, __u64 v) \
96{ \
97 __u##_bits new = __le##_bits##_to_cpu(k->field); \
98 \
99 new &= ~(~(~0ULL << (end - offset)) << offset); \
100 new |= (v & ~(~0ULL << (end - offset))) << offset; \
101 k->field = __cpu_to_le##_bits(new); \
102}
103
104#define LE16_BITMASK(n, t, f, o, e) LE_BITMASK(16, n, t, f, o, e)
105#define LE32_BITMASK(n, t, f, o, e) LE_BITMASK(32, n, t, f, o, e)
106#define LE64_BITMASK(n, t, f, o, e) LE_BITMASK(64, n, t, f, o, e)
107
108struct bkey_format {
109 __u8 key_u64s;
110 __u8 nr_fields;
111 /* One unused slot for now: */
112 __u8 bits_per_field[6];
113 __le64 field_offset[6];
114};
115
116/* Btree keys - all units are in sectors */
117
118struct bpos {
119 /*
120 * Word order matches machine byte order - btree code treats a bpos as a
121 * single large integer, for search/comparison purposes
122 *
123 * Note that wherever a bpos is embedded in another on disk data
124 * structure, it has to be byte swabbed when reading in metadata that
125 * wasn't written in native endian order:
126 */
127#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
128 __u32 snapshot;
129 __u64 offset;
130 __u64 inode;
131#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
132 __u64 inode;
133 __u64 offset; /* Points to end of extent - sectors */
134 __u32 snapshot;
135#else
136#error edit for your odd byteorder.
137#endif
138} __attribute__((packed, aligned(4)));
139
140#define KEY_INODE_MAX ((__u64)~0ULL)
141#define KEY_OFFSET_MAX ((__u64)~0ULL)
142#define KEY_SNAPSHOT_MAX ((__u32)~0U)
143#define KEY_SIZE_MAX ((__u32)~0U)
144
145static inline struct bpos POS(__u64 inode, __u64 offset)
146{
147 struct bpos ret;
148
149 ret.inode = inode;
150 ret.offset = offset;
151 ret.snapshot = 0;
152
153 return ret;
154}
155
156#define POS_MIN POS(0, 0)
157#define POS_MAX POS(KEY_INODE_MAX, KEY_OFFSET_MAX)
158
159/* Empty placeholder struct, for container_of() */
160struct bch_val {
161 __u64 __nothing[0];
162};
163
164struct bversion {
165#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
166 __u64 lo;
167 __u32 hi;
168#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
169 __u32 hi;
170 __u64 lo;
171#endif
172} __attribute__((packed, aligned(4)));
173
174struct bkey {
175 /* Size of combined key and value, in u64s */
176 __u8 u64s;
177
178 /* Format of key (0 for format local to btree node) */
179#if defined(__LITTLE_ENDIAN_BITFIELD)
180 __u8 format:7,
181 needs_whiteout:1;
182#elif defined (__BIG_ENDIAN_BITFIELD)
183 __u8 needs_whiteout:1,
184 format:7;
185#else
186#error edit for your odd byteorder.
187#endif
188
189 /* Type of the value */
190 __u8 type;
191
192#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
193 __u8 pad[1];
194
195 struct bversion version;
196 __u32 size; /* extent size, in sectors */
197 struct bpos p;
198#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
199 struct bpos p;
200 __u32 size; /* extent size, in sectors */
201 struct bversion version;
202
203 __u8 pad[1];
204#endif
205} __attribute__((packed, aligned(8)));
206
207struct bkey_packed {
208 __u64 _data[0];
209
210 /* Size of combined key and value, in u64s */
211 __u8 u64s;
212
213 /* Format of key (0 for format local to btree node) */
214
215 /*
216 * XXX: next incompat on disk format change, switch format and
217 * needs_whiteout - bkey_packed() will be cheaper if format is the high
218 * bits of the bitfield
219 */
220#if defined(__LITTLE_ENDIAN_BITFIELD)
221 __u8 format:7,
222 needs_whiteout:1;
223#elif defined (__BIG_ENDIAN_BITFIELD)
224 __u8 needs_whiteout:1,
225 format:7;
226#endif
227
228 /* Type of the value */
229 __u8 type;
230 __u8 key_start[0];
231
232 /*
233 * We copy bkeys with struct assignment in various places, and while
234 * that shouldn't be done with packed bkeys we can't disallow it in C,
235 * and it's legal to cast a bkey to a bkey_packed - so padding it out
236 * to the same size as struct bkey should hopefully be safest.
237 */
238 __u8 pad[sizeof(struct bkey) - 3];
239} __attribute__((packed, aligned(8)));
240
241#define BKEY_U64s (sizeof(struct bkey) / sizeof(__u64))
cd575ddf
KO
242#define BKEY_U64s_MAX U8_MAX
243#define BKEY_VAL_U64s_MAX (BKEY_U64s_MAX - BKEY_U64s)
244
1c6fdbd8
KO
245#define KEY_PACKED_BITS_START 24
246
247#define KEY_FORMAT_LOCAL_BTREE 0
248#define KEY_FORMAT_CURRENT 1
249
250enum bch_bkey_fields {
251 BKEY_FIELD_INODE,
252 BKEY_FIELD_OFFSET,
253 BKEY_FIELD_SNAPSHOT,
254 BKEY_FIELD_SIZE,
255 BKEY_FIELD_VERSION_HI,
256 BKEY_FIELD_VERSION_LO,
257 BKEY_NR_FIELDS,
258};
259
260#define bkey_format_field(name, field) \
261 [BKEY_FIELD_##name] = (sizeof(((struct bkey *) NULL)->field) * 8)
262
263#define BKEY_FORMAT_CURRENT \
264((struct bkey_format) { \
265 .key_u64s = BKEY_U64s, \
266 .nr_fields = BKEY_NR_FIELDS, \
267 .bits_per_field = { \
268 bkey_format_field(INODE, p.inode), \
269 bkey_format_field(OFFSET, p.offset), \
270 bkey_format_field(SNAPSHOT, p.snapshot), \
271 bkey_format_field(SIZE, size), \
272 bkey_format_field(VERSION_HI, version.hi), \
273 bkey_format_field(VERSION_LO, version.lo), \
274 }, \
275})
276
277/* bkey with inline value */
278struct bkey_i {
279 __u64 _data[0];
280
281 union {
282 struct {
283 /* Size of combined key and value, in u64s */
284 __u8 u64s;
285 };
286 struct {
287 struct bkey k;
288 struct bch_val v;
289 };
290 };
291};
292
293#define KEY(_inode, _offset, _size) \
294((struct bkey) { \
295 .u64s = BKEY_U64s, \
296 .format = KEY_FORMAT_CURRENT, \
297 .p = POS(_inode, _offset), \
298 .size = _size, \
299})
300
301static inline void bkey_init(struct bkey *k)
302{
303 *k = KEY(0, 0, 0);
304}
305
306#define bkey_bytes(_k) ((_k)->u64s * sizeof(__u64))
307
308#define __BKEY_PADDED(key, pad) \
309 struct { struct bkey_i key; __u64 key ## _pad[pad]; }
310
1c6fdbd8
KO
311/*
312 * - DELETED keys are used internally to mark keys that should be ignored but
313 * override keys in composition order. Their version number is ignored.
314 *
315 * - DISCARDED keys indicate that the data is all 0s because it has been
316 * discarded. DISCARDs may have a version; if the version is nonzero the key
317 * will be persistent, otherwise the key will be dropped whenever the btree
318 * node is rewritten (like DELETED keys).
319 *
320 * - ERROR: any read of the data returns a read error, as the data was lost due
321 * to a failing device. Like DISCARDED keys, they can be removed (overridden)
322 * by new writes or cluster-wide GC. Node repair can also overwrite them with
323 * the same or a more recent version number, but not with an older version
324 * number.
26609b61
KO
325 *
326 * - WHITEOUT: for hash table btrees
1c6fdbd8 327*/
26609b61
KO
328#define BCH_BKEY_TYPES() \
329 x(deleted, 0) \
330 x(discard, 1) \
331 x(error, 2) \
332 x(cookie, 3) \
333 x(whiteout, 4) \
334 x(btree_ptr, 5) \
335 x(extent, 6) \
336 x(reservation, 7) \
337 x(inode, 8) \
338 x(inode_generation, 9) \
339 x(dirent, 10) \
340 x(xattr, 11) \
341 x(alloc, 12) \
342 x(quota, 13) \
343 x(stripe, 14)
344
345enum bch_bkey_type {
346#define x(name, nr) KEY_TYPE_##name = nr,
347 BCH_BKEY_TYPES()
348#undef x
349 KEY_TYPE_MAX,
350};
1c6fdbd8
KO
351
352struct bch_cookie {
353 struct bch_val v;
354 __le64 cookie;
355};
1c6fdbd8
KO
356
357/* Extents */
358
359/*
360 * In extent bkeys, the value is a list of pointers (bch_extent_ptr), optionally
361 * preceded by checksum/compression information (bch_extent_crc32 or
362 * bch_extent_crc64).
363 *
364 * One major determining factor in the format of extents is how we handle and
365 * represent extents that have been partially overwritten and thus trimmed:
366 *
367 * If an extent is not checksummed or compressed, when the extent is trimmed we
368 * don't have to remember the extent we originally allocated and wrote: we can
369 * merely adjust ptr->offset to point to the start of the data that is currently
370 * live. The size field in struct bkey records the current (live) size of the
371 * extent, and is also used to mean "size of region on disk that we point to" in
372 * this case.
373 *
374 * Thus an extent that is not checksummed or compressed will consist only of a
375 * list of bch_extent_ptrs, with none of the fields in
376 * bch_extent_crc32/bch_extent_crc64.
377 *
378 * When an extent is checksummed or compressed, it's not possible to read only
379 * the data that is currently live: we have to read the entire extent that was
380 * originally written, and then return only the part of the extent that is
381 * currently live.
382 *
383 * Thus, in addition to the current size of the extent in struct bkey, we need
384 * to store the size of the originally allocated space - this is the
385 * compressed_size and uncompressed_size fields in bch_extent_crc32/64. Also,
386 * when the extent is trimmed, instead of modifying the offset field of the
387 * pointer, we keep a second smaller offset field - "offset into the original
388 * extent of the currently live region".
389 *
390 * The other major determining factor is replication and data migration:
391 *
392 * Each pointer may have its own bch_extent_crc32/64. When doing a replicated
393 * write, we will initially write all the replicas in the same format, with the
394 * same checksum type and compression format - however, when copygc runs later (or
395 * tiering/cache promotion, anything that moves data), it is not in general
396 * going to rewrite all the pointers at once - one of the replicas may be in a
397 * bucket on one device that has very little fragmentation while another lives
398 * in a bucket that has become heavily fragmented, and thus is being rewritten
399 * sooner than the rest.
400 *
401 * Thus it will only move a subset of the pointers (or in the case of
402 * tiering/cache promotion perhaps add a single pointer without dropping any
403 * current pointers), and if the extent has been partially overwritten it must
404 * write only the currently live portion (or copygc would not be able to reduce
405 * fragmentation!) - which necessitates a different bch_extent_crc format for
406 * the new pointer.
407 *
408 * But in the interests of space efficiency, we don't want to store one
409 * bch_extent_crc for each pointer if we don't have to.
410 *
411 * Thus, a bch_extent consists of bch_extent_crc32s, bch_extent_crc64s, and
412 * bch_extent_ptrs appended arbitrarily one after the other. We determine the
413 * type of a given entry with a scheme similar to utf8 (except we're encoding a
414 * type, not a size), encoding the type in the position of the first set bit:
415 *
416 * bch_extent_crc32 - 0b1
417 * bch_extent_ptr - 0b10
418 * bch_extent_crc64 - 0b100
419 *
420 * We do it this way because bch_extent_crc32 is _very_ constrained on bits (and
421 * bch_extent_crc64 is the least constrained).
422 *
423 * Then, each bch_extent_crc32/64 applies to the pointers that follow after it,
424 * until the next bch_extent_crc32/64.
425 *
426 * If there are no bch_extent_crcs preceding a bch_extent_ptr, then that pointer
427 * is neither checksummed nor compressed.
428 */
429
430/* 128 bits, sufficient for cryptographic MACs: */
431struct bch_csum {
432 __le64 lo;
433 __le64 hi;
434} __attribute__((packed, aligned(8)));
435
436enum bch_csum_type {
437 BCH_CSUM_NONE = 0,
438 BCH_CSUM_CRC32C_NONZERO = 1,
439 BCH_CSUM_CRC64_NONZERO = 2,
440 BCH_CSUM_CHACHA20_POLY1305_80 = 3,
441 BCH_CSUM_CHACHA20_POLY1305_128 = 4,
442 BCH_CSUM_CRC32C = 5,
443 BCH_CSUM_CRC64 = 6,
444 BCH_CSUM_NR = 7,
445};
446
447static const unsigned bch_crc_bytes[] = {
448 [BCH_CSUM_NONE] = 0,
449 [BCH_CSUM_CRC32C_NONZERO] = 4,
450 [BCH_CSUM_CRC32C] = 4,
451 [BCH_CSUM_CRC64_NONZERO] = 8,
452 [BCH_CSUM_CRC64] = 8,
453 [BCH_CSUM_CHACHA20_POLY1305_80] = 10,
454 [BCH_CSUM_CHACHA20_POLY1305_128] = 16,
455};
456
457static inline _Bool bch2_csum_type_is_encryption(enum bch_csum_type type)
458{
459 switch (type) {
460 case BCH_CSUM_CHACHA20_POLY1305_80:
461 case BCH_CSUM_CHACHA20_POLY1305_128:
462 return true;
463 default:
464 return false;
465 }
466}
467
468enum bch_compression_type {
469 BCH_COMPRESSION_NONE = 0,
470 BCH_COMPRESSION_LZ4_OLD = 1,
471 BCH_COMPRESSION_GZIP = 2,
472 BCH_COMPRESSION_LZ4 = 3,
473 BCH_COMPRESSION_ZSTD = 4,
474 BCH_COMPRESSION_NR = 5,
475};
476
abce30b7
KO
477#define BCH_EXTENT_ENTRY_TYPES() \
478 x(ptr, 0) \
479 x(crc32, 1) \
480 x(crc64, 2) \
cd575ddf
KO
481 x(crc128, 3) \
482 x(stripe_ptr, 4)
483#define BCH_EXTENT_ENTRY_MAX 5
abce30b7 484
1c6fdbd8 485enum bch_extent_entry_type {
abce30b7
KO
486#define x(f, n) BCH_EXTENT_ENTRY_##f = n,
487 BCH_EXTENT_ENTRY_TYPES()
488#undef x
1c6fdbd8
KO
489};
490
1c6fdbd8
KO
491/* Compressed/uncompressed size are stored biased by 1: */
492struct bch_extent_crc32 {
493#if defined(__LITTLE_ENDIAN_BITFIELD)
494 __u32 type:2,
495 _compressed_size:7,
496 _uncompressed_size:7,
497 offset:7,
498 _unused:1,
499 csum_type:4,
500 compression_type:4;
501 __u32 csum;
502#elif defined (__BIG_ENDIAN_BITFIELD)
503 __u32 csum;
504 __u32 compression_type:4,
505 csum_type:4,
506 _unused:1,
507 offset:7,
508 _uncompressed_size:7,
509 _compressed_size:7,
510 type:2;
511#endif
512} __attribute__((packed, aligned(8)));
513
514#define CRC32_SIZE_MAX (1U << 7)
515#define CRC32_NONCE_MAX 0
516
517struct bch_extent_crc64 {
518#if defined(__LITTLE_ENDIAN_BITFIELD)
519 __u64 type:3,
520 _compressed_size:9,
521 _uncompressed_size:9,
522 offset:9,
523 nonce:10,
524 csum_type:4,
525 compression_type:4,
526 csum_hi:16;
527#elif defined (__BIG_ENDIAN_BITFIELD)
528 __u64 csum_hi:16,
529 compression_type:4,
530 csum_type:4,
531 nonce:10,
532 offset:9,
533 _uncompressed_size:9,
534 _compressed_size:9,
535 type:3;
536#endif
537 __u64 csum_lo;
538} __attribute__((packed, aligned(8)));
539
540#define CRC64_SIZE_MAX (1U << 9)
541#define CRC64_NONCE_MAX ((1U << 10) - 1)
542
543struct bch_extent_crc128 {
544#if defined(__LITTLE_ENDIAN_BITFIELD)
545 __u64 type:4,
546 _compressed_size:13,
547 _uncompressed_size:13,
548 offset:13,
549 nonce:13,
550 csum_type:4,
551 compression_type:4;
552#elif defined (__BIG_ENDIAN_BITFIELD)
553 __u64 compression_type:4,
554 csum_type:4,
555 nonce:13,
556 offset:13,
557 _uncompressed_size:13,
558 _compressed_size:13,
559 type:4;
560#endif
561 struct bch_csum csum;
562} __attribute__((packed, aligned(8)));
563
564#define CRC128_SIZE_MAX (1U << 13)
565#define CRC128_NONCE_MAX ((1U << 13) - 1)
566
567/*
568 * @reservation - pointer hasn't been written to, just reserved
569 */
570struct bch_extent_ptr {
571#if defined(__LITTLE_ENDIAN_BITFIELD)
572 __u64 type:1,
573 cached:1,
cd575ddf 574 unused:1,
1c6fdbd8
KO
575 reservation:1,
576 offset:44, /* 8 petabytes */
577 dev:8,
578 gen:8;
579#elif defined (__BIG_ENDIAN_BITFIELD)
580 __u64 gen:8,
581 dev:8,
582 offset:44,
583 reservation:1,
cd575ddf 584 unused:1,
1c6fdbd8
KO
585 cached:1,
586 type:1;
587#endif
588} __attribute__((packed, aligned(8)));
589
cd575ddf 590struct bch_extent_stripe_ptr {
1c6fdbd8
KO
591#if defined(__LITTLE_ENDIAN_BITFIELD)
592 __u64 type:5,
cd575ddf
KO
593 block:8,
594 idx:51;
595#elif defined (__BIG_ENDIAN_BITFIELD)
596 __u64 idx:51,
597 block:8,
598 type:5;
599#endif
600};
601
602struct bch_extent_reservation {
603#if defined(__LITTLE_ENDIAN_BITFIELD)
604 __u64 type:6,
605 unused:22,
1c6fdbd8
KO
606 replicas:4,
607 generation:32;
608#elif defined (__BIG_ENDIAN_BITFIELD)
609 __u64 generation:32,
610 replicas:4,
cd575ddf
KO
611 unused:22,
612 type:6;
1c6fdbd8
KO
613#endif
614};
615
616union bch_extent_entry {
617#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ || __BITS_PER_LONG == 64
618 unsigned long type;
619#elif __BITS_PER_LONG == 32
620 struct {
621 unsigned long pad;
622 unsigned long type;
623 };
624#else
625#error edit for your odd byteorder.
626#endif
abce30b7
KO
627
628#define x(f, n) struct bch_extent_##f f;
629 BCH_EXTENT_ENTRY_TYPES()
630#undef x
1c6fdbd8
KO
631};
632
26609b61
KO
633struct bch_btree_ptr {
634 struct bch_val v;
1c6fdbd8 635
26609b61
KO
636 __u64 _data[0];
637 struct bch_extent_ptr start[];
638} __attribute__((packed, aligned(8)));
1c6fdbd8
KO
639
640struct bch_extent {
641 struct bch_val v;
642
643 __u64 _data[0];
644 union bch_extent_entry start[];
645} __attribute__((packed, aligned(8)));
1c6fdbd8
KO
646
647struct bch_reservation {
648 struct bch_val v;
649
650 __le32 generation;
651 __u8 nr_replicas;
652 __u8 pad[3];
653} __attribute__((packed, aligned(8)));
1c6fdbd8
KO
654
655/* Maximum size (in u64s) a single pointer could be: */
656#define BKEY_EXTENT_PTR_U64s_MAX\
657 ((sizeof(struct bch_extent_crc128) + \
658 sizeof(struct bch_extent_ptr)) / sizeof(u64))
659
660/* Maximum possible size of an entire extent value: */
661#define BKEY_EXTENT_VAL_U64s_MAX \
662 (BKEY_EXTENT_PTR_U64s_MAX * (BCH_REPLICAS_MAX + 1))
663
664#define BKEY_PADDED(key) __BKEY_PADDED(key, BKEY_EXTENT_VAL_U64s_MAX)
665
666/* * Maximum possible size of an entire extent, key + value: */
667#define BKEY_EXTENT_U64s_MAX (BKEY_U64s + BKEY_EXTENT_VAL_U64s_MAX)
668
669/* Btree pointers don't carry around checksums: */
670#define BKEY_BTREE_PTR_VAL_U64s_MAX \
671 ((sizeof(struct bch_extent_ptr)) / sizeof(u64) * BCH_REPLICAS_MAX)
672#define BKEY_BTREE_PTR_U64s_MAX \
673 (BKEY_U64s + BKEY_BTREE_PTR_VAL_U64s_MAX)
674
675/* Inodes */
676
677#define BLOCKDEV_INODE_MAX 4096
678
679#define BCACHEFS_ROOT_INO 4096
680
1c6fdbd8
KO
681struct bch_inode {
682 struct bch_val v;
683
684 __le64 bi_hash_seed;
685 __le32 bi_flags;
686 __le16 bi_mode;
687 __u8 fields[0];
688} __attribute__((packed, aligned(8)));
1c6fdbd8
KO
689
690struct bch_inode_generation {
691 struct bch_val v;
692
693 __le32 bi_generation;
694 __le32 pad;
695} __attribute__((packed, aligned(8)));
1c6fdbd8 696
a3e70fb2
KO
697#define BCH_INODE_FIELDS() \
698 x(bi_atime, 64) \
699 x(bi_ctime, 64) \
700 x(bi_mtime, 64) \
701 x(bi_otime, 64) \
702 x(bi_size, 64) \
703 x(bi_sectors, 64) \
704 x(bi_uid, 32) \
705 x(bi_gid, 32) \
706 x(bi_nlink, 32) \
707 x(bi_generation, 32) \
708 x(bi_dev, 32) \
709 x(bi_data_checksum, 8) \
710 x(bi_compression, 8) \
711 x(bi_project, 32) \
712 x(bi_background_compression, 8) \
713 x(bi_data_replicas, 8) \
714 x(bi_promote_target, 16) \
715 x(bi_foreground_target, 16) \
716 x(bi_background_target, 16) \
721d4ad8
KO
717 x(bi_erasure_code, 16) \
718 x(bi_fields_set, 16)
a3e70fb2 719
d42dd4ad
KO
720/* subset of BCH_INODE_FIELDS */
721#define BCH_INODE_OPTS() \
722 x(data_checksum, 8) \
723 x(compression, 8) \
724 x(project, 32) \
725 x(background_compression, 8) \
726 x(data_replicas, 8) \
727 x(promote_target, 16) \
728 x(foreground_target, 16) \
729 x(background_target, 16) \
730 x(erasure_code, 16)
1c6fdbd8 731
721d4ad8
KO
732enum inode_opt_id {
733#define x(name, ...) \
734 Inode_opt_##name,
735 BCH_INODE_OPTS()
736#undef x
737 Inode_opt_nr,
738};
739
1c6fdbd8
KO
740enum {
741 /*
742 * User flags (get/settable with FS_IOC_*FLAGS, correspond to FS_*_FL
743 * flags)
744 */
745 __BCH_INODE_SYNC = 0,
746 __BCH_INODE_IMMUTABLE = 1,
747 __BCH_INODE_APPEND = 2,
748 __BCH_INODE_NODUMP = 3,
749 __BCH_INODE_NOATIME = 4,
750
751 __BCH_INODE_I_SIZE_DIRTY= 5,
752 __BCH_INODE_I_SECTORS_DIRTY= 6,
753 __BCH_INODE_UNLINKED = 7,
754
755 /* bits 20+ reserved for packed fields below: */
756};
757
758#define BCH_INODE_SYNC (1 << __BCH_INODE_SYNC)
759#define BCH_INODE_IMMUTABLE (1 << __BCH_INODE_IMMUTABLE)
760#define BCH_INODE_APPEND (1 << __BCH_INODE_APPEND)
761#define BCH_INODE_NODUMP (1 << __BCH_INODE_NODUMP)
762#define BCH_INODE_NOATIME (1 << __BCH_INODE_NOATIME)
763#define BCH_INODE_I_SIZE_DIRTY (1 << __BCH_INODE_I_SIZE_DIRTY)
764#define BCH_INODE_I_SECTORS_DIRTY (1 << __BCH_INODE_I_SECTORS_DIRTY)
765#define BCH_INODE_UNLINKED (1 << __BCH_INODE_UNLINKED)
766
767LE32_BITMASK(INODE_STR_HASH, struct bch_inode, bi_flags, 20, 24);
768LE32_BITMASK(INODE_NR_FIELDS, struct bch_inode, bi_flags, 24, 32);
769
1c6fdbd8
KO
770/* Dirents */
771
772/*
773 * Dirents (and xattrs) have to implement string lookups; since our b-tree
774 * doesn't support arbitrary length strings for the key, we instead index by a
775 * 64 bit hash (currently truncated sha1) of the string, stored in the offset
776 * field of the key - using linear probing to resolve hash collisions. This also
777 * provides us with the readdir cookie posix requires.
778 *
779 * Linear probing requires us to use whiteouts for deletions, in the event of a
780 * collision:
781 */
782
1c6fdbd8
KO
783struct bch_dirent {
784 struct bch_val v;
785
786 /* Target inode number: */
787 __le64 d_inum;
788
789 /*
790 * Copy of mode bits 12-15 from the target inode - so userspace can get
791 * the filetype without having to do a stat()
792 */
793 __u8 d_type;
794
795 __u8 d_name[];
796} __attribute__((packed, aligned(8)));
1c6fdbd8
KO
797
798#define BCH_NAME_MAX (U8_MAX * sizeof(u64) - \
799 sizeof(struct bkey) - \
800 offsetof(struct bch_dirent, d_name))
801
802
803/* Xattrs */
804
26609b61
KO
805#define KEY_TYPE_XATTR_INDEX_USER 0
806#define KEY_TYPE_XATTR_INDEX_POSIX_ACL_ACCESS 1
807#define KEY_TYPE_XATTR_INDEX_POSIX_ACL_DEFAULT 2
808#define KEY_TYPE_XATTR_INDEX_TRUSTED 3
809#define KEY_TYPE_XATTR_INDEX_SECURITY 4
1c6fdbd8
KO
810
811struct bch_xattr {
812 struct bch_val v;
813 __u8 x_type;
814 __u8 x_name_len;
815 __le16 x_val_len;
816 __u8 x_name[];
817} __attribute__((packed, aligned(8)));
1c6fdbd8
KO
818
819/* Bucket/allocation information: */
820
1c6fdbd8
KO
821struct bch_alloc {
822 struct bch_val v;
823 __u8 fields;
824 __u8 gen;
825 __u8 data[];
826} __attribute__((packed, aligned(8)));
1c6fdbd8 827
90541a74 828#define BCH_ALLOC_FIELDS() \
8fe826f9
KO
829 x(read_time, 16) \
830 x(write_time, 16) \
831 x(data_type, 8) \
832 x(dirty_sectors, 16) \
833 x(cached_sectors, 16) \
834 x(oldest_gen, 8)
90541a74
KO
835
836enum {
837#define x(name, bytes) BCH_ALLOC_FIELD_##name,
838 BCH_ALLOC_FIELDS()
839#undef x
840 BCH_ALLOC_FIELD_NR
841};
842
843static const unsigned BCH_ALLOC_FIELD_BYTES[] = {
8fe826f9 844#define x(name, bits) [BCH_ALLOC_FIELD_##name] = bits / 8,
90541a74
KO
845 BCH_ALLOC_FIELDS()
846#undef x
847};
848
8fe826f9 849#define x(name, bits) + (bits / 8)
90541a74
KO
850static const unsigned BKEY_ALLOC_VAL_U64s_MAX =
851 DIV_ROUND_UP(offsetof(struct bch_alloc, data)
852 BCH_ALLOC_FIELDS(), sizeof(u64));
853#undef x
854
1c6fdbd8
KO
855/* Quotas: */
856
1c6fdbd8
KO
857enum quota_types {
858 QTYP_USR = 0,
859 QTYP_GRP = 1,
860 QTYP_PRJ = 2,
861 QTYP_NR = 3,
862};
863
864enum quota_counters {
865 Q_SPC = 0,
866 Q_INO = 1,
867 Q_COUNTERS = 2,
868};
869
870struct bch_quota_counter {
871 __le64 hardlimit;
872 __le64 softlimit;
873};
874
875struct bch_quota {
876 struct bch_val v;
877 struct bch_quota_counter c[Q_COUNTERS];
878} __attribute__((packed, aligned(8)));
1c6fdbd8 879
cd575ddf
KO
880/* Erasure coding */
881
cd575ddf
KO
882struct bch_stripe {
883 struct bch_val v;
884 __le16 sectors;
885 __u8 algorithm;
886 __u8 nr_blocks;
887 __u8 nr_redundant;
888
889 __u8 csum_granularity_bits;
890 __u8 csum_type;
891 __u8 pad;
892
893 struct bch_extent_ptr ptrs[0];
894} __attribute__((packed, aligned(8)));
cd575ddf 895
1c6fdbd8
KO
896/* Optional/variable size superblock sections: */
897
898struct bch_sb_field {
899 __u64 _data[0];
900 __le32 u64s;
901 __le32 type;
902};
903
904#define BCH_SB_FIELDS() \
905 x(journal, 0) \
906 x(members, 1) \
907 x(crypt, 2) \
af9d3bc2 908 x(replicas_v0, 3) \
1c6fdbd8
KO
909 x(quota, 4) \
910 x(disk_groups, 5) \
af9d3bc2
KO
911 x(clean, 6) \
912 x(replicas, 7)
1c6fdbd8
KO
913
914enum bch_sb_field_type {
915#define x(f, nr) BCH_SB_FIELD_##f = nr,
916 BCH_SB_FIELDS()
917#undef x
918 BCH_SB_FIELD_NR
919};
920
921/* BCH_SB_FIELD_journal: */
922
923struct bch_sb_field_journal {
924 struct bch_sb_field field;
925 __le64 buckets[0];
926};
927
928/* BCH_SB_FIELD_members: */
929
8b335bae
KO
930#define BCH_MIN_NR_NBUCKETS (1 << 6)
931
1c6fdbd8
KO
932struct bch_member {
933 __uuid_t uuid;
934 __le64 nbuckets; /* device size */
935 __le16 first_bucket; /* index of first bucket used */
936 __le16 bucket_size; /* sectors */
937 __le32 pad;
938 __le64 last_mount; /* time_t */
939
940 __le64 flags[2];
941};
942
943LE64_BITMASK(BCH_MEMBER_STATE, struct bch_member, flags[0], 0, 4)
944/* 4-10 unused, was TIER, HAS_(META)DATA */
945LE64_BITMASK(BCH_MEMBER_REPLACEMENT, struct bch_member, flags[0], 10, 14)
946LE64_BITMASK(BCH_MEMBER_DISCARD, struct bch_member, flags[0], 14, 15)
947LE64_BITMASK(BCH_MEMBER_DATA_ALLOWED, struct bch_member, flags[0], 15, 20)
948LE64_BITMASK(BCH_MEMBER_GROUP, struct bch_member, flags[0], 20, 28)
949LE64_BITMASK(BCH_MEMBER_DURABILITY, struct bch_member, flags[0], 28, 30)
950
951#define BCH_TIER_MAX 4U
952
953#if 0
954LE64_BITMASK(BCH_MEMBER_NR_READ_ERRORS, struct bch_member, flags[1], 0, 20);
955LE64_BITMASK(BCH_MEMBER_NR_WRITE_ERRORS,struct bch_member, flags[1], 20, 40);
956#endif
957
958enum bch_member_state {
959 BCH_MEMBER_STATE_RW = 0,
960 BCH_MEMBER_STATE_RO = 1,
961 BCH_MEMBER_STATE_FAILED = 2,
962 BCH_MEMBER_STATE_SPARE = 3,
963 BCH_MEMBER_STATE_NR = 4,
964};
965
966enum cache_replacement {
967 CACHE_REPLACEMENT_LRU = 0,
968 CACHE_REPLACEMENT_FIFO = 1,
969 CACHE_REPLACEMENT_RANDOM = 2,
970 CACHE_REPLACEMENT_NR = 3,
971};
972
973struct bch_sb_field_members {
974 struct bch_sb_field field;
975 struct bch_member members[0];
976};
977
978/* BCH_SB_FIELD_crypt: */
979
980struct nonce {
981 __le32 d[4];
982};
983
984struct bch_key {
985 __le64 key[4];
986};
987
988#define BCH_KEY_MAGIC \
989 (((u64) 'b' << 0)|((u64) 'c' << 8)| \
990 ((u64) 'h' << 16)|((u64) '*' << 24)| \
991 ((u64) '*' << 32)|((u64) 'k' << 40)| \
992 ((u64) 'e' << 48)|((u64) 'y' << 56))
993
994struct bch_encrypted_key {
995 __le64 magic;
996 struct bch_key key;
997};
998
999/*
1000 * If this field is present in the superblock, it stores an encryption key which
1001 * is used encrypt all other data/metadata. The key will normally be encrypted
1002 * with the key userspace provides, but if encryption has been turned off we'll
1003 * just store the master key unencrypted in the superblock so we can access the
1004 * previously encrypted data.
1005 */
1006struct bch_sb_field_crypt {
1007 struct bch_sb_field field;
1008
1009 __le64 flags;
1010 __le64 kdf_flags;
1011 struct bch_encrypted_key key;
1012};
1013
1014LE64_BITMASK(BCH_CRYPT_KDF_TYPE, struct bch_sb_field_crypt, flags, 0, 4);
1015
1016enum bch_kdf_types {
1017 BCH_KDF_SCRYPT = 0,
1018 BCH_KDF_NR = 1,
1019};
1020
1021/* stored as base 2 log of scrypt params: */
1022LE64_BITMASK(BCH_KDF_SCRYPT_N, struct bch_sb_field_crypt, kdf_flags, 0, 16);
1023LE64_BITMASK(BCH_KDF_SCRYPT_R, struct bch_sb_field_crypt, kdf_flags, 16, 32);
1024LE64_BITMASK(BCH_KDF_SCRYPT_P, struct bch_sb_field_crypt, kdf_flags, 32, 48);
1025
1026/* BCH_SB_FIELD_replicas: */
1027
1028enum bch_data_type {
1029 BCH_DATA_NONE = 0,
1030 BCH_DATA_SB = 1,
1031 BCH_DATA_JOURNAL = 2,
1032 BCH_DATA_BTREE = 3,
1033 BCH_DATA_USER = 4,
1034 BCH_DATA_CACHED = 5,
1035 BCH_DATA_NR = 6,
1036};
1037
af9d3bc2
KO
1038struct bch_replicas_entry_v0 {
1039 __u8 data_type;
1040 __u8 nr_devs;
1041 __u8 devs[];
1042} __attribute__((packed));
1043
1044struct bch_sb_field_replicas_v0 {
1045 struct bch_sb_field field;
1046 struct bch_replicas_entry_v0 entries[];
1047} __attribute__((packed, aligned(8)));
1048
1c6fdbd8 1049struct bch_replicas_entry {
7a920560
KO
1050 __u8 data_type;
1051 __u8 nr_devs;
af9d3bc2 1052 __u8 nr_required;
7a920560 1053 __u8 devs[];
af9d3bc2 1054} __attribute__((packed));
1c6fdbd8
KO
1055
1056struct bch_sb_field_replicas {
1057 struct bch_sb_field field;
1058 struct bch_replicas_entry entries[];
af9d3bc2 1059} __attribute__((packed, aligned(8)));
1c6fdbd8
KO
1060
1061/* BCH_SB_FIELD_quota: */
1062
1063struct bch_sb_quota_counter {
1064 __le32 timelimit;
1065 __le32 warnlimit;
1066};
1067
1068struct bch_sb_quota_type {
1069 __le64 flags;
1070 struct bch_sb_quota_counter c[Q_COUNTERS];
1071};
1072
1073struct bch_sb_field_quota {
1074 struct bch_sb_field field;
1075 struct bch_sb_quota_type q[QTYP_NR];
1076} __attribute__((packed, aligned(8)));
1077
1078/* BCH_SB_FIELD_disk_groups: */
1079
1080#define BCH_SB_LABEL_SIZE 32
1081
1082struct bch_disk_group {
1083 __u8 label[BCH_SB_LABEL_SIZE];
1084 __le64 flags[2];
cd575ddf 1085} __attribute__((packed, aligned(8)));
1c6fdbd8
KO
1086
1087LE64_BITMASK(BCH_GROUP_DELETED, struct bch_disk_group, flags[0], 0, 1)
1088LE64_BITMASK(BCH_GROUP_DATA_ALLOWED, struct bch_disk_group, flags[0], 1, 6)
1089LE64_BITMASK(BCH_GROUP_PARENT, struct bch_disk_group, flags[0], 6, 24)
1090
1091struct bch_sb_field_disk_groups {
1092 struct bch_sb_field field;
1093 struct bch_disk_group entries[0];
cd575ddf 1094} __attribute__((packed, aligned(8)));
1c6fdbd8
KO
1095
1096/*
1097 * On clean shutdown, store btree roots and current journal sequence number in
1098 * the superblock:
1099 */
1100struct jset_entry {
1101 __le16 u64s;
1102 __u8 btree_id;
1103 __u8 level;
1104 __u8 type; /* designates what this jset holds */
1105 __u8 pad[3];
1106
1107 union {
1108 struct bkey_i start[0];
1109 __u64 _data[0];
1110 };
1111};
1112
1113struct bch_sb_field_clean {
1114 struct bch_sb_field field;
1115
1116 __le32 flags;
1117 __le16 read_clock;
1118 __le16 write_clock;
1119 __le64 journal_seq;
1120
1121 union {
1122 struct jset_entry start[0];
1123 __u64 _data[0];
1124 };
1125};
1126
1127/* Superblock: */
1128
1129/*
26609b61
KO
1130 * New versioning scheme:
1131 * One common version number for all on disk data structures - superblock, btree
1132 * nodes, journal entries
1c6fdbd8 1133 */
26609b61
KO
1134#define BCH_JSET_VERSION_OLD 2
1135#define BCH_BSET_VERSION_OLD 3
1136
1137enum bcachefs_metadata_version {
1138 bcachefs_metadata_version_min = 9,
1139 bcachefs_metadata_version_new_versioning = 10,
1140 bcachefs_metadata_version_bkey_renumber = 10,
1141 bcachefs_metadata_version_max = 11,
1142};
1c6fdbd8 1143
26609b61 1144#define bcachefs_metadata_version_current (bcachefs_metadata_version_max - 1)
1c6fdbd8
KO
1145
1146#define BCH_SB_SECTOR 8
1147#define BCH_SB_MEMBERS_MAX 64 /* XXX kill */
1148
1149struct bch_sb_layout {
1150 __uuid_t magic; /* bcachefs superblock UUID */
1151 __u8 layout_type;
1152 __u8 sb_max_size_bits; /* base 2 of 512 byte sectors */
1153 __u8 nr_superblocks;
1154 __u8 pad[5];
1155 __le64 sb_offset[61];
1156} __attribute__((packed, aligned(8)));
1157
1158#define BCH_SB_LAYOUT_SECTOR 7
1159
1160/*
1161 * @offset - sector where this sb was written
1162 * @version - on disk format version
26609b61
KO
1163 * @version_min - Oldest metadata version this filesystem contains; so we can
1164 * safely drop compatibility code and refuse to mount filesystems
1165 * we'd need it for
1c6fdbd8
KO
1166 * @magic - identifies as a bcachefs superblock (BCACHE_MAGIC)
1167 * @seq - incremented each time superblock is written
1168 * @uuid - used for generating various magic numbers and identifying
1169 * member devices, never changes
1170 * @user_uuid - user visible UUID, may be changed
1171 * @label - filesystem label
1172 * @seq - identifies most recent superblock, incremented each time
1173 * superblock is written
1174 * @features - enabled incompatible features
1175 */
1176struct bch_sb {
1177 struct bch_csum csum;
1178 __le16 version;
1179 __le16 version_min;
1180 __le16 pad[2];
1181 __uuid_t magic;
1182 __uuid_t uuid;
1183 __uuid_t user_uuid;
1184 __u8 label[BCH_SB_LABEL_SIZE];
1185 __le64 offset;
1186 __le64 seq;
1187
1188 __le16 block_size;
1189 __u8 dev_idx;
1190 __u8 nr_devices;
1191 __le32 u64s;
1192
1193 __le64 time_base_lo;
1194 __le32 time_base_hi;
1195 __le32 time_precision;
1196
1197 __le64 flags[8];
1198 __le64 features[2];
1199 __le64 compat[2];
1200
1201 struct bch_sb_layout layout;
1202
1203 union {
1204 struct bch_sb_field start[0];
1205 __le64 _data[0];
1206 };
1207} __attribute__((packed, aligned(8)));
1208
1209/*
1210 * Flags:
1211 * BCH_SB_INITALIZED - set on first mount
1212 * BCH_SB_CLEAN - did we shut down cleanly? Just a hint, doesn't affect
1213 * behaviour of mount/recovery path:
1214 * BCH_SB_INODE_32BIT - limit inode numbers to 32 bits
1215 * BCH_SB_128_BIT_MACS - 128 bit macs instead of 80
1216 * BCH_SB_ENCRYPTION_TYPE - if nonzero encryption is enabled; overrides
1217 * DATA/META_CSUM_TYPE. Also indicates encryption
1218 * algorithm in use, if/when we get more than one
1219 */
1220
1221LE16_BITMASK(BCH_SB_BLOCK_SIZE, struct bch_sb, block_size, 0, 16);
1222
1223LE64_BITMASK(BCH_SB_INITIALIZED, struct bch_sb, flags[0], 0, 1);
1224LE64_BITMASK(BCH_SB_CLEAN, struct bch_sb, flags[0], 1, 2);
1225LE64_BITMASK(BCH_SB_CSUM_TYPE, struct bch_sb, flags[0], 2, 8);
1226LE64_BITMASK(BCH_SB_ERROR_ACTION, struct bch_sb, flags[0], 8, 12);
1227
1228LE64_BITMASK(BCH_SB_BTREE_NODE_SIZE, struct bch_sb, flags[0], 12, 28);
1229
1230LE64_BITMASK(BCH_SB_GC_RESERVE, struct bch_sb, flags[0], 28, 33);
1231LE64_BITMASK(BCH_SB_ROOT_RESERVE, struct bch_sb, flags[0], 33, 40);
1232
1233LE64_BITMASK(BCH_SB_META_CSUM_TYPE, struct bch_sb, flags[0], 40, 44);
1234LE64_BITMASK(BCH_SB_DATA_CSUM_TYPE, struct bch_sb, flags[0], 44, 48);
1235
1236LE64_BITMASK(BCH_SB_META_REPLICAS_WANT, struct bch_sb, flags[0], 48, 52);
1237LE64_BITMASK(BCH_SB_DATA_REPLICAS_WANT, struct bch_sb, flags[0], 52, 56);
1238
1239LE64_BITMASK(BCH_SB_POSIX_ACL, struct bch_sb, flags[0], 56, 57);
1240LE64_BITMASK(BCH_SB_USRQUOTA, struct bch_sb, flags[0], 57, 58);
1241LE64_BITMASK(BCH_SB_GRPQUOTA, struct bch_sb, flags[0], 58, 59);
1242LE64_BITMASK(BCH_SB_PRJQUOTA, struct bch_sb, flags[0], 59, 60);
1243
0bc166ff
KO
1244LE64_BITMASK(BCH_SB_HAS_ERRORS, struct bch_sb, flags[0], 60, 61);
1245
1246/* 61-64 unused */
1c6fdbd8
KO
1247
1248LE64_BITMASK(BCH_SB_STR_HASH_TYPE, struct bch_sb, flags[1], 0, 4);
1249LE64_BITMASK(BCH_SB_COMPRESSION_TYPE, struct bch_sb, flags[1], 4, 8);
1250LE64_BITMASK(BCH_SB_INODE_32BIT, struct bch_sb, flags[1], 8, 9);
1251
1252LE64_BITMASK(BCH_SB_128_BIT_MACS, struct bch_sb, flags[1], 9, 10);
1253LE64_BITMASK(BCH_SB_ENCRYPTION_TYPE, struct bch_sb, flags[1], 10, 14);
1254
1255/*
1256 * Max size of an extent that may require bouncing to read or write
1257 * (checksummed, compressed): 64k
1258 */
1259LE64_BITMASK(BCH_SB_ENCODED_EXTENT_MAX_BITS,
1260 struct bch_sb, flags[1], 14, 20);
1261
1262LE64_BITMASK(BCH_SB_META_REPLICAS_REQ, struct bch_sb, flags[1], 20, 24);
1263LE64_BITMASK(BCH_SB_DATA_REPLICAS_REQ, struct bch_sb, flags[1], 24, 28);
1264
1265LE64_BITMASK(BCH_SB_PROMOTE_TARGET, struct bch_sb, flags[1], 28, 40);
1266LE64_BITMASK(BCH_SB_FOREGROUND_TARGET, struct bch_sb, flags[1], 40, 52);
1267LE64_BITMASK(BCH_SB_BACKGROUND_TARGET, struct bch_sb, flags[1], 52, 64);
1268
1269LE64_BITMASK(BCH_SB_BACKGROUND_COMPRESSION_TYPE,
1270 struct bch_sb, flags[2], 0, 4);
a50ed7c8 1271LE64_BITMASK(BCH_SB_GC_RESERVE_BYTES, struct bch_sb, flags[2], 4, 64);
1c6fdbd8 1272
cd575ddf
KO
1273LE64_BITMASK(BCH_SB_ERASURE_CODE, struct bch_sb, flags[3], 0, 16);
1274
1c6fdbd8
KO
1275/* Features: */
1276enum bch_sb_features {
1277 BCH_FEATURE_LZ4 = 0,
1278 BCH_FEATURE_GZIP = 1,
1279 BCH_FEATURE_ZSTD = 2,
c258f28e 1280 BCH_FEATURE_ATOMIC_NLINK = 3, /* should have gone under compat */
cd575ddf 1281 BCH_FEATURE_EC = 4,
c258f28e 1282 BCH_FEATURE_NR,
1c6fdbd8
KO
1283};
1284
1df42b57
KO
1285enum bch_sb_compat {
1286 BCH_COMPAT_FEAT_ALLOC_INFO = 0,
1287};
1288
1c6fdbd8
KO
1289/* options: */
1290
1291#define BCH_REPLICAS_MAX 4U
1292
1293enum bch_error_actions {
1294 BCH_ON_ERROR_CONTINUE = 0,
1295 BCH_ON_ERROR_RO = 1,
1296 BCH_ON_ERROR_PANIC = 2,
1297 BCH_NR_ERROR_ACTIONS = 3,
1298};
1299
1300enum bch_csum_opts {
1301 BCH_CSUM_OPT_NONE = 0,
1302 BCH_CSUM_OPT_CRC32C = 1,
1303 BCH_CSUM_OPT_CRC64 = 2,
1304 BCH_CSUM_OPT_NR = 3,
1305};
1306
1307enum bch_str_hash_opts {
1308 BCH_STR_HASH_CRC32C = 0,
1309 BCH_STR_HASH_CRC64 = 1,
1310 BCH_STR_HASH_SIPHASH = 2,
1311 BCH_STR_HASH_NR = 3,
1312};
1313
1314#define BCH_COMPRESSION_TYPES() \
1315 x(NONE) \
1316 x(LZ4) \
1317 x(GZIP) \
1318 x(ZSTD)
1319
1320enum bch_compression_opts {
1321#define x(t) BCH_COMPRESSION_OPT_##t,
1322 BCH_COMPRESSION_TYPES()
1323#undef x
1324 BCH_COMPRESSION_OPT_NR
1325};
1326
1327/*
1328 * Magic numbers
1329 *
1330 * The various other data structures have their own magic numbers, which are
1331 * xored with the first part of the cache set's UUID
1332 */
1333
1334#define BCACHE_MAGIC \
1335 UUID_INIT(0xc68573f6, 0x4e1a, 0x45ca, \
1336 0x82, 0x65, 0xf5, 0x7f, 0x48, 0xba, 0x6d, 0x81)
1337#define BCHFS_MAGIC \
1338 UUID_INIT(0xc68573f6, 0x66ce, 0x90a9, \
1339 0xd9, 0x6a, 0x60, 0xcf, 0x80, 0x3d, 0xf7, 0xef)
1340
1341#define BCACHEFS_STATFS_MAGIC 0xca451a4e
1342
1343#define JSET_MAGIC __cpu_to_le64(0x245235c1a3625032ULL)
1344#define BSET_MAGIC __cpu_to_le64(0x90135c78b99e07f5ULL)
1345
1346static inline __le64 __bch2_sb_magic(struct bch_sb *sb)
1347{
1348 __le64 ret;
1349 memcpy(&ret, &sb->uuid, sizeof(ret));
1350 return ret;
1351}
1352
1353static inline __u64 __jset_magic(struct bch_sb *sb)
1354{
1355 return __le64_to_cpu(__bch2_sb_magic(sb) ^ JSET_MAGIC);
1356}
1357
1358static inline __u64 __bset_magic(struct bch_sb *sb)
1359{
1360 return __le64_to_cpu(__bch2_sb_magic(sb) ^ BSET_MAGIC);
1361}
1362
1363/* Journal */
1364
1c6fdbd8
KO
1365#define JSET_KEYS_U64s (sizeof(struct jset_entry) / sizeof(__u64))
1366
1367#define BCH_JSET_ENTRY_TYPES() \
1368 x(btree_keys, 0) \
1369 x(btree_root, 1) \
1370 x(prio_ptrs, 2) \
1371 x(blacklist, 3) \
2c5af169 1372 x(blacklist_v2, 4) \
3577df5f
KO
1373 x(usage, 5) \
1374 x(data_usage, 6)
1c6fdbd8
KO
1375
1376enum {
1377#define x(f, nr) BCH_JSET_ENTRY_##f = nr,
1378 BCH_JSET_ENTRY_TYPES()
1379#undef x
1380 BCH_JSET_ENTRY_NR
1381};
1382
1383/*
1384 * Journal sequence numbers can be blacklisted: bsets record the max sequence
1385 * number of all the journal entries they contain updates for, so that on
1386 * recovery we can ignore those bsets that contain index updates newer that what
1387 * made it into the journal.
1388 *
1389 * This means that we can't reuse that journal_seq - we have to skip it, and
1390 * then record that we skipped it so that the next time we crash and recover we
1391 * don't think there was a missing journal entry.
1392 */
1393struct jset_entry_blacklist {
1394 struct jset_entry entry;
1395 __le64 seq;
1396};
1397
1398struct jset_entry_blacklist_v2 {
1399 struct jset_entry entry;
1400 __le64 start;
1401 __le64 end;
1402};
1403
2c5af169 1404enum {
3577df5f 1405 FS_USAGE_RESERVED = 0,
2c5af169
KO
1406 FS_USAGE_INODES = 1,
1407 FS_USAGE_KEY_VERSION = 2,
1408 FS_USAGE_NR = 3
1409};
1410
1411struct jset_entry_usage {
1412 struct jset_entry entry;
3577df5f
KO
1413 __le64 v;
1414} __attribute__((packed));
1415
1416struct jset_entry_data_usage {
1417 struct jset_entry entry;
1418 __le64 v;
2c5af169
KO
1419 struct bch_replicas_entry r;
1420} __attribute__((packed));
1421
1c6fdbd8
KO
1422/*
1423 * On disk format for a journal entry:
1424 * seq is monotonically increasing; every journal entry has its own unique
1425 * sequence number.
1426 *
1427 * last_seq is the oldest journal entry that still has keys the btree hasn't
1428 * flushed to disk yet.
1429 *
1430 * version is for on disk format changes.
1431 */
1432struct jset {
1433 struct bch_csum csum;
1434
1435 __le64 magic;
1436 __le64 seq;
1437 __le32 version;
1438 __le32 flags;
1439
1440 __le32 u64s; /* size of d[] in u64s */
1441
1442 __u8 encrypted_start[0];
1443
1444 __le16 read_clock;
1445 __le16 write_clock;
1446
1447 /* Sequence number of oldest dirty journal entry */
1448 __le64 last_seq;
1449
1450
1451 union {
1452 struct jset_entry start[0];
1453 __u64 _data[0];
1454 };
1455} __attribute__((packed, aligned(8)));
1456
1457LE32_BITMASK(JSET_CSUM_TYPE, struct jset, flags, 0, 4);
1458LE32_BITMASK(JSET_BIG_ENDIAN, struct jset, flags, 4, 5);
1459
8b335bae 1460#define BCH_JOURNAL_BUCKETS_MIN 8
1c6fdbd8
KO
1461
1462/* Btree: */
1463
26609b61
KO
1464#define BCH_BTREE_IDS() \
1465 x(EXTENTS, 0, "extents") \
1466 x(INODES, 1, "inodes") \
1467 x(DIRENTS, 2, "dirents") \
1468 x(XATTRS, 3, "xattrs") \
1469 x(ALLOC, 4, "alloc") \
1470 x(QUOTAS, 5, "quotas") \
1471 x(EC, 6, "erasure_coding")
1c6fdbd8
KO
1472
1473enum btree_id {
26609b61
KO
1474#define x(kwd, val, name) BTREE_ID_##kwd = val,
1475 BCH_BTREE_IDS()
1476#undef x
1c6fdbd8
KO
1477 BTREE_ID_NR
1478};
1479
1c6fdbd8
KO
1480#define BTREE_MAX_DEPTH 4U
1481
1482/* Btree nodes */
1483
1c6fdbd8
KO
1484/*
1485 * Btree nodes
1486 *
1487 * On disk a btree node is a list/log of these; within each set the keys are
1488 * sorted
1489 */
1490struct bset {
1491 __le64 seq;
1492
1493 /*
1494 * Highest journal entry this bset contains keys for.
1495 * If on recovery we don't see that journal entry, this bset is ignored:
1496 * this allows us to preserve the order of all index updates after a
1497 * crash, since the journal records a total order of all index updates
1498 * and anything that didn't make it to the journal doesn't get used.
1499 */
1500 __le64 journal_seq;
1501
1502 __le32 flags;
1503 __le16 version;
1504 __le16 u64s; /* count of d[] in u64s */
1505
1506 union {
1507 struct bkey_packed start[0];
1508 __u64 _data[0];
1509 };
1510} __attribute__((packed, aligned(8)));
1511
1512LE32_BITMASK(BSET_CSUM_TYPE, struct bset, flags, 0, 4);
1513
1514LE32_BITMASK(BSET_BIG_ENDIAN, struct bset, flags, 4, 5);
1515LE32_BITMASK(BSET_SEPARATE_WHITEOUTS,
1516 struct bset, flags, 5, 6);
1517
1518struct btree_node {
1519 struct bch_csum csum;
1520 __le64 magic;
1521
1522 /* this flags field is encrypted, unlike bset->flags: */
1523 __le64 flags;
1524
1525 /* Closed interval: */
1526 struct bpos min_key;
1527 struct bpos max_key;
1528 struct bch_extent_ptr ptr;
1529 struct bkey_format format;
1530
1531 union {
1532 struct bset keys;
1533 struct {
1534 __u8 pad[22];
1535 __le16 u64s;
1536 __u64 _data[0];
1537
1538 };
1539 };
1540} __attribute__((packed, aligned(8)));
1541
1542LE64_BITMASK(BTREE_NODE_ID, struct btree_node, flags, 0, 4);
1543LE64_BITMASK(BTREE_NODE_LEVEL, struct btree_node, flags, 4, 8);
1544/* 8-32 unused */
1545LE64_BITMASK(BTREE_NODE_SEQ, struct btree_node, flags, 32, 64);
1546
1547struct btree_node_entry {
1548 struct bch_csum csum;
1549
1550 union {
1551 struct bset keys;
1552 struct {
1553 __u8 pad[22];
1554 __le16 u64s;
1555 __u64 _data[0];
1556
1557 };
1558 };
1559} __attribute__((packed, aligned(8)));
1560
1561#endif /* _BCACHEFS_FORMAT_H */