bcachefs: btree_bkey_cached_common
[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 911 x(clean, 6) \
1dd7f9d9
KO
912 x(replicas, 7) \
913 x(journal_seq_blacklist, 8)
1c6fdbd8
KO
914
915enum bch_sb_field_type {
916#define x(f, nr) BCH_SB_FIELD_##f = nr,
917 BCH_SB_FIELDS()
918#undef x
919 BCH_SB_FIELD_NR
920};
921
922/* BCH_SB_FIELD_journal: */
923
924struct bch_sb_field_journal {
925 struct bch_sb_field field;
926 __le64 buckets[0];
927};
928
929/* BCH_SB_FIELD_members: */
930
8b335bae
KO
931#define BCH_MIN_NR_NBUCKETS (1 << 6)
932
1c6fdbd8
KO
933struct bch_member {
934 __uuid_t uuid;
935 __le64 nbuckets; /* device size */
936 __le16 first_bucket; /* index of first bucket used */
937 __le16 bucket_size; /* sectors */
938 __le32 pad;
939 __le64 last_mount; /* time_t */
940
941 __le64 flags[2];
942};
943
944LE64_BITMASK(BCH_MEMBER_STATE, struct bch_member, flags[0], 0, 4)
945/* 4-10 unused, was TIER, HAS_(META)DATA */
946LE64_BITMASK(BCH_MEMBER_REPLACEMENT, struct bch_member, flags[0], 10, 14)
947LE64_BITMASK(BCH_MEMBER_DISCARD, struct bch_member, flags[0], 14, 15)
948LE64_BITMASK(BCH_MEMBER_DATA_ALLOWED, struct bch_member, flags[0], 15, 20)
949LE64_BITMASK(BCH_MEMBER_GROUP, struct bch_member, flags[0], 20, 28)
950LE64_BITMASK(BCH_MEMBER_DURABILITY, struct bch_member, flags[0], 28, 30)
951
952#define BCH_TIER_MAX 4U
953
954#if 0
955LE64_BITMASK(BCH_MEMBER_NR_READ_ERRORS, struct bch_member, flags[1], 0, 20);
956LE64_BITMASK(BCH_MEMBER_NR_WRITE_ERRORS,struct bch_member, flags[1], 20, 40);
957#endif
958
959enum bch_member_state {
960 BCH_MEMBER_STATE_RW = 0,
961 BCH_MEMBER_STATE_RO = 1,
962 BCH_MEMBER_STATE_FAILED = 2,
963 BCH_MEMBER_STATE_SPARE = 3,
964 BCH_MEMBER_STATE_NR = 4,
965};
966
967enum cache_replacement {
968 CACHE_REPLACEMENT_LRU = 0,
969 CACHE_REPLACEMENT_FIFO = 1,
970 CACHE_REPLACEMENT_RANDOM = 2,
971 CACHE_REPLACEMENT_NR = 3,
972};
973
974struct bch_sb_field_members {
975 struct bch_sb_field field;
976 struct bch_member members[0];
977};
978
979/* BCH_SB_FIELD_crypt: */
980
981struct nonce {
982 __le32 d[4];
983};
984
985struct bch_key {
986 __le64 key[4];
987};
988
989#define BCH_KEY_MAGIC \
990 (((u64) 'b' << 0)|((u64) 'c' << 8)| \
991 ((u64) 'h' << 16)|((u64) '*' << 24)| \
992 ((u64) '*' << 32)|((u64) 'k' << 40)| \
993 ((u64) 'e' << 48)|((u64) 'y' << 56))
994
995struct bch_encrypted_key {
996 __le64 magic;
997 struct bch_key key;
998};
999
1000/*
1001 * If this field is present in the superblock, it stores an encryption key which
1002 * is used encrypt all other data/metadata. The key will normally be encrypted
1003 * with the key userspace provides, but if encryption has been turned off we'll
1004 * just store the master key unencrypted in the superblock so we can access the
1005 * previously encrypted data.
1006 */
1007struct bch_sb_field_crypt {
1008 struct bch_sb_field field;
1009
1010 __le64 flags;
1011 __le64 kdf_flags;
1012 struct bch_encrypted_key key;
1013};
1014
1015LE64_BITMASK(BCH_CRYPT_KDF_TYPE, struct bch_sb_field_crypt, flags, 0, 4);
1016
1017enum bch_kdf_types {
1018 BCH_KDF_SCRYPT = 0,
1019 BCH_KDF_NR = 1,
1020};
1021
1022/* stored as base 2 log of scrypt params: */
1023LE64_BITMASK(BCH_KDF_SCRYPT_N, struct bch_sb_field_crypt, kdf_flags, 0, 16);
1024LE64_BITMASK(BCH_KDF_SCRYPT_R, struct bch_sb_field_crypt, kdf_flags, 16, 32);
1025LE64_BITMASK(BCH_KDF_SCRYPT_P, struct bch_sb_field_crypt, kdf_flags, 32, 48);
1026
1027/* BCH_SB_FIELD_replicas: */
1028
1029enum bch_data_type {
1030 BCH_DATA_NONE = 0,
1031 BCH_DATA_SB = 1,
1032 BCH_DATA_JOURNAL = 2,
1033 BCH_DATA_BTREE = 3,
1034 BCH_DATA_USER = 4,
1035 BCH_DATA_CACHED = 5,
1036 BCH_DATA_NR = 6,
1037};
1038
af9d3bc2
KO
1039struct bch_replicas_entry_v0 {
1040 __u8 data_type;
1041 __u8 nr_devs;
1042 __u8 devs[];
1043} __attribute__((packed));
1044
1045struct bch_sb_field_replicas_v0 {
1046 struct bch_sb_field field;
1047 struct bch_replicas_entry_v0 entries[];
1048} __attribute__((packed, aligned(8)));
1049
1c6fdbd8 1050struct bch_replicas_entry {
7a920560
KO
1051 __u8 data_type;
1052 __u8 nr_devs;
af9d3bc2 1053 __u8 nr_required;
7a920560 1054 __u8 devs[];
af9d3bc2 1055} __attribute__((packed));
1c6fdbd8
KO
1056
1057struct bch_sb_field_replicas {
1058 struct bch_sb_field field;
1059 struct bch_replicas_entry entries[];
af9d3bc2 1060} __attribute__((packed, aligned(8)));
1c6fdbd8
KO
1061
1062/* BCH_SB_FIELD_quota: */
1063
1064struct bch_sb_quota_counter {
1065 __le32 timelimit;
1066 __le32 warnlimit;
1067};
1068
1069struct bch_sb_quota_type {
1070 __le64 flags;
1071 struct bch_sb_quota_counter c[Q_COUNTERS];
1072};
1073
1074struct bch_sb_field_quota {
1075 struct bch_sb_field field;
1076 struct bch_sb_quota_type q[QTYP_NR];
1077} __attribute__((packed, aligned(8)));
1078
1079/* BCH_SB_FIELD_disk_groups: */
1080
1081#define BCH_SB_LABEL_SIZE 32
1082
1083struct bch_disk_group {
1084 __u8 label[BCH_SB_LABEL_SIZE];
1085 __le64 flags[2];
cd575ddf 1086} __attribute__((packed, aligned(8)));
1c6fdbd8
KO
1087
1088LE64_BITMASK(BCH_GROUP_DELETED, struct bch_disk_group, flags[0], 0, 1)
1089LE64_BITMASK(BCH_GROUP_DATA_ALLOWED, struct bch_disk_group, flags[0], 1, 6)
1090LE64_BITMASK(BCH_GROUP_PARENT, struct bch_disk_group, flags[0], 6, 24)
1091
1092struct bch_sb_field_disk_groups {
1093 struct bch_sb_field field;
1094 struct bch_disk_group entries[0];
cd575ddf 1095} __attribute__((packed, aligned(8)));
1c6fdbd8
KO
1096
1097/*
1098 * On clean shutdown, store btree roots and current journal sequence number in
1099 * the superblock:
1100 */
1101struct jset_entry {
1102 __le16 u64s;
1103 __u8 btree_id;
1104 __u8 level;
1105 __u8 type; /* designates what this jset holds */
1106 __u8 pad[3];
1107
1108 union {
1109 struct bkey_i start[0];
1110 __u64 _data[0];
1111 };
1112};
1113
1114struct bch_sb_field_clean {
1115 struct bch_sb_field field;
1116
1117 __le32 flags;
1118 __le16 read_clock;
1119 __le16 write_clock;
1120 __le64 journal_seq;
1121
1122 union {
1123 struct jset_entry start[0];
1124 __u64 _data[0];
1125 };
1126};
1127
1dd7f9d9
KO
1128struct journal_seq_blacklist_entry {
1129 __le64 start;
1130 __le64 end;
1131};
1132
1133struct bch_sb_field_journal_seq_blacklist {
1134 struct bch_sb_field field;
1135
1136 union {
1137 struct journal_seq_blacklist_entry start[0];
1138 __u64 _data[0];
1139 };
1140};
1141
1c6fdbd8
KO
1142/* Superblock: */
1143
1144/*
26609b61
KO
1145 * New versioning scheme:
1146 * One common version number for all on disk data structures - superblock, btree
1147 * nodes, journal entries
1c6fdbd8 1148 */
26609b61
KO
1149#define BCH_JSET_VERSION_OLD 2
1150#define BCH_BSET_VERSION_OLD 3
1151
1152enum bcachefs_metadata_version {
1153 bcachefs_metadata_version_min = 9,
1154 bcachefs_metadata_version_new_versioning = 10,
1155 bcachefs_metadata_version_bkey_renumber = 10,
1156 bcachefs_metadata_version_max = 11,
1157};
1c6fdbd8 1158
26609b61 1159#define bcachefs_metadata_version_current (bcachefs_metadata_version_max - 1)
1c6fdbd8
KO
1160
1161#define BCH_SB_SECTOR 8
1162#define BCH_SB_MEMBERS_MAX 64 /* XXX kill */
1163
1164struct bch_sb_layout {
1165 __uuid_t magic; /* bcachefs superblock UUID */
1166 __u8 layout_type;
1167 __u8 sb_max_size_bits; /* base 2 of 512 byte sectors */
1168 __u8 nr_superblocks;
1169 __u8 pad[5];
1170 __le64 sb_offset[61];
1171} __attribute__((packed, aligned(8)));
1172
1173#define BCH_SB_LAYOUT_SECTOR 7
1174
1175/*
1176 * @offset - sector where this sb was written
1177 * @version - on disk format version
26609b61
KO
1178 * @version_min - Oldest metadata version this filesystem contains; so we can
1179 * safely drop compatibility code and refuse to mount filesystems
1180 * we'd need it for
1c6fdbd8
KO
1181 * @magic - identifies as a bcachefs superblock (BCACHE_MAGIC)
1182 * @seq - incremented each time superblock is written
1183 * @uuid - used for generating various magic numbers and identifying
1184 * member devices, never changes
1185 * @user_uuid - user visible UUID, may be changed
1186 * @label - filesystem label
1187 * @seq - identifies most recent superblock, incremented each time
1188 * superblock is written
1189 * @features - enabled incompatible features
1190 */
1191struct bch_sb {
1192 struct bch_csum csum;
1193 __le16 version;
1194 __le16 version_min;
1195 __le16 pad[2];
1196 __uuid_t magic;
1197 __uuid_t uuid;
1198 __uuid_t user_uuid;
1199 __u8 label[BCH_SB_LABEL_SIZE];
1200 __le64 offset;
1201 __le64 seq;
1202
1203 __le16 block_size;
1204 __u8 dev_idx;
1205 __u8 nr_devices;
1206 __le32 u64s;
1207
1208 __le64 time_base_lo;
1209 __le32 time_base_hi;
1210 __le32 time_precision;
1211
1212 __le64 flags[8];
1213 __le64 features[2];
1214 __le64 compat[2];
1215
1216 struct bch_sb_layout layout;
1217
1218 union {
1219 struct bch_sb_field start[0];
1220 __le64 _data[0];
1221 };
1222} __attribute__((packed, aligned(8)));
1223
1224/*
1225 * Flags:
1226 * BCH_SB_INITALIZED - set on first mount
1227 * BCH_SB_CLEAN - did we shut down cleanly? Just a hint, doesn't affect
1228 * behaviour of mount/recovery path:
1229 * BCH_SB_INODE_32BIT - limit inode numbers to 32 bits
1230 * BCH_SB_128_BIT_MACS - 128 bit macs instead of 80
1231 * BCH_SB_ENCRYPTION_TYPE - if nonzero encryption is enabled; overrides
1232 * DATA/META_CSUM_TYPE. Also indicates encryption
1233 * algorithm in use, if/when we get more than one
1234 */
1235
1236LE16_BITMASK(BCH_SB_BLOCK_SIZE, struct bch_sb, block_size, 0, 16);
1237
1238LE64_BITMASK(BCH_SB_INITIALIZED, struct bch_sb, flags[0], 0, 1);
1239LE64_BITMASK(BCH_SB_CLEAN, struct bch_sb, flags[0], 1, 2);
1240LE64_BITMASK(BCH_SB_CSUM_TYPE, struct bch_sb, flags[0], 2, 8);
1241LE64_BITMASK(BCH_SB_ERROR_ACTION, struct bch_sb, flags[0], 8, 12);
1242
1243LE64_BITMASK(BCH_SB_BTREE_NODE_SIZE, struct bch_sb, flags[0], 12, 28);
1244
1245LE64_BITMASK(BCH_SB_GC_RESERVE, struct bch_sb, flags[0], 28, 33);
1246LE64_BITMASK(BCH_SB_ROOT_RESERVE, struct bch_sb, flags[0], 33, 40);
1247
1248LE64_BITMASK(BCH_SB_META_CSUM_TYPE, struct bch_sb, flags[0], 40, 44);
1249LE64_BITMASK(BCH_SB_DATA_CSUM_TYPE, struct bch_sb, flags[0], 44, 48);
1250
1251LE64_BITMASK(BCH_SB_META_REPLICAS_WANT, struct bch_sb, flags[0], 48, 52);
1252LE64_BITMASK(BCH_SB_DATA_REPLICAS_WANT, struct bch_sb, flags[0], 52, 56);
1253
1254LE64_BITMASK(BCH_SB_POSIX_ACL, struct bch_sb, flags[0], 56, 57);
1255LE64_BITMASK(BCH_SB_USRQUOTA, struct bch_sb, flags[0], 57, 58);
1256LE64_BITMASK(BCH_SB_GRPQUOTA, struct bch_sb, flags[0], 58, 59);
1257LE64_BITMASK(BCH_SB_PRJQUOTA, struct bch_sb, flags[0], 59, 60);
1258
0bc166ff
KO
1259LE64_BITMASK(BCH_SB_HAS_ERRORS, struct bch_sb, flags[0], 60, 61);
1260
1261/* 61-64 unused */
1c6fdbd8
KO
1262
1263LE64_BITMASK(BCH_SB_STR_HASH_TYPE, struct bch_sb, flags[1], 0, 4);
1264LE64_BITMASK(BCH_SB_COMPRESSION_TYPE, struct bch_sb, flags[1], 4, 8);
1265LE64_BITMASK(BCH_SB_INODE_32BIT, struct bch_sb, flags[1], 8, 9);
1266
1267LE64_BITMASK(BCH_SB_128_BIT_MACS, struct bch_sb, flags[1], 9, 10);
1268LE64_BITMASK(BCH_SB_ENCRYPTION_TYPE, struct bch_sb, flags[1], 10, 14);
1269
1270/*
1271 * Max size of an extent that may require bouncing to read or write
1272 * (checksummed, compressed): 64k
1273 */
1274LE64_BITMASK(BCH_SB_ENCODED_EXTENT_MAX_BITS,
1275 struct bch_sb, flags[1], 14, 20);
1276
1277LE64_BITMASK(BCH_SB_META_REPLICAS_REQ, struct bch_sb, flags[1], 20, 24);
1278LE64_BITMASK(BCH_SB_DATA_REPLICAS_REQ, struct bch_sb, flags[1], 24, 28);
1279
1280LE64_BITMASK(BCH_SB_PROMOTE_TARGET, struct bch_sb, flags[1], 28, 40);
1281LE64_BITMASK(BCH_SB_FOREGROUND_TARGET, struct bch_sb, flags[1], 40, 52);
1282LE64_BITMASK(BCH_SB_BACKGROUND_TARGET, struct bch_sb, flags[1], 52, 64);
1283
1284LE64_BITMASK(BCH_SB_BACKGROUND_COMPRESSION_TYPE,
1285 struct bch_sb, flags[2], 0, 4);
a50ed7c8 1286LE64_BITMASK(BCH_SB_GC_RESERVE_BYTES, struct bch_sb, flags[2], 4, 64);
1c6fdbd8 1287
cd575ddf
KO
1288LE64_BITMASK(BCH_SB_ERASURE_CODE, struct bch_sb, flags[3], 0, 16);
1289
1c6fdbd8
KO
1290/* Features: */
1291enum bch_sb_features {
1292 BCH_FEATURE_LZ4 = 0,
1293 BCH_FEATURE_GZIP = 1,
1294 BCH_FEATURE_ZSTD = 2,
c258f28e 1295 BCH_FEATURE_ATOMIC_NLINK = 3, /* should have gone under compat */
cd575ddf 1296 BCH_FEATURE_EC = 4,
1dd7f9d9 1297 BCH_FEATURE_JOURNAL_SEQ_BLACKLIST_V3 = 5,
c258f28e 1298 BCH_FEATURE_NR,
1c6fdbd8
KO
1299};
1300
1df42b57
KO
1301enum bch_sb_compat {
1302 BCH_COMPAT_FEAT_ALLOC_INFO = 0,
1303};
1304
1c6fdbd8
KO
1305/* options: */
1306
1307#define BCH_REPLICAS_MAX 4U
1308
1309enum bch_error_actions {
1310 BCH_ON_ERROR_CONTINUE = 0,
1311 BCH_ON_ERROR_RO = 1,
1312 BCH_ON_ERROR_PANIC = 2,
1313 BCH_NR_ERROR_ACTIONS = 3,
1314};
1315
1316enum bch_csum_opts {
1317 BCH_CSUM_OPT_NONE = 0,
1318 BCH_CSUM_OPT_CRC32C = 1,
1319 BCH_CSUM_OPT_CRC64 = 2,
1320 BCH_CSUM_OPT_NR = 3,
1321};
1322
1323enum bch_str_hash_opts {
1324 BCH_STR_HASH_CRC32C = 0,
1325 BCH_STR_HASH_CRC64 = 1,
1326 BCH_STR_HASH_SIPHASH = 2,
1327 BCH_STR_HASH_NR = 3,
1328};
1329
1330#define BCH_COMPRESSION_TYPES() \
1331 x(NONE) \
1332 x(LZ4) \
1333 x(GZIP) \
1334 x(ZSTD)
1335
1336enum bch_compression_opts {
1337#define x(t) BCH_COMPRESSION_OPT_##t,
1338 BCH_COMPRESSION_TYPES()
1339#undef x
1340 BCH_COMPRESSION_OPT_NR
1341};
1342
1343/*
1344 * Magic numbers
1345 *
1346 * The various other data structures have their own magic numbers, which are
1347 * xored with the first part of the cache set's UUID
1348 */
1349
1350#define BCACHE_MAGIC \
1351 UUID_INIT(0xc68573f6, 0x4e1a, 0x45ca, \
1352 0x82, 0x65, 0xf5, 0x7f, 0x48, 0xba, 0x6d, 0x81)
1353#define BCHFS_MAGIC \
1354 UUID_INIT(0xc68573f6, 0x66ce, 0x90a9, \
1355 0xd9, 0x6a, 0x60, 0xcf, 0x80, 0x3d, 0xf7, 0xef)
1356
1357#define BCACHEFS_STATFS_MAGIC 0xca451a4e
1358
1359#define JSET_MAGIC __cpu_to_le64(0x245235c1a3625032ULL)
1360#define BSET_MAGIC __cpu_to_le64(0x90135c78b99e07f5ULL)
1361
1362static inline __le64 __bch2_sb_magic(struct bch_sb *sb)
1363{
1364 __le64 ret;
1365 memcpy(&ret, &sb->uuid, sizeof(ret));
1366 return ret;
1367}
1368
1369static inline __u64 __jset_magic(struct bch_sb *sb)
1370{
1371 return __le64_to_cpu(__bch2_sb_magic(sb) ^ JSET_MAGIC);
1372}
1373
1374static inline __u64 __bset_magic(struct bch_sb *sb)
1375{
1376 return __le64_to_cpu(__bch2_sb_magic(sb) ^ BSET_MAGIC);
1377}
1378
1379/* Journal */
1380
1c6fdbd8
KO
1381#define JSET_KEYS_U64s (sizeof(struct jset_entry) / sizeof(__u64))
1382
1383#define BCH_JSET_ENTRY_TYPES() \
1384 x(btree_keys, 0) \
1385 x(btree_root, 1) \
1386 x(prio_ptrs, 2) \
1387 x(blacklist, 3) \
2c5af169 1388 x(blacklist_v2, 4) \
3577df5f
KO
1389 x(usage, 5) \
1390 x(data_usage, 6)
1c6fdbd8
KO
1391
1392enum {
1393#define x(f, nr) BCH_JSET_ENTRY_##f = nr,
1394 BCH_JSET_ENTRY_TYPES()
1395#undef x
1396 BCH_JSET_ENTRY_NR
1397};
1398
1399/*
1400 * Journal sequence numbers can be blacklisted: bsets record the max sequence
1401 * number of all the journal entries they contain updates for, so that on
1402 * recovery we can ignore those bsets that contain index updates newer that what
1403 * made it into the journal.
1404 *
1405 * This means that we can't reuse that journal_seq - we have to skip it, and
1406 * then record that we skipped it so that the next time we crash and recover we
1407 * don't think there was a missing journal entry.
1408 */
1409struct jset_entry_blacklist {
1410 struct jset_entry entry;
1411 __le64 seq;
1412};
1413
1414struct jset_entry_blacklist_v2 {
1415 struct jset_entry entry;
1416 __le64 start;
1417 __le64 end;
1418};
1419
2c5af169 1420enum {
3577df5f 1421 FS_USAGE_RESERVED = 0,
2c5af169
KO
1422 FS_USAGE_INODES = 1,
1423 FS_USAGE_KEY_VERSION = 2,
1424 FS_USAGE_NR = 3
1425};
1426
1427struct jset_entry_usage {
1428 struct jset_entry entry;
3577df5f
KO
1429 __le64 v;
1430} __attribute__((packed));
1431
1432struct jset_entry_data_usage {
1433 struct jset_entry entry;
1434 __le64 v;
2c5af169
KO
1435 struct bch_replicas_entry r;
1436} __attribute__((packed));
1437
1c6fdbd8
KO
1438/*
1439 * On disk format for a journal entry:
1440 * seq is monotonically increasing; every journal entry has its own unique
1441 * sequence number.
1442 *
1443 * last_seq is the oldest journal entry that still has keys the btree hasn't
1444 * flushed to disk yet.
1445 *
1446 * version is for on disk format changes.
1447 */
1448struct jset {
1449 struct bch_csum csum;
1450
1451 __le64 magic;
1452 __le64 seq;
1453 __le32 version;
1454 __le32 flags;
1455
1456 __le32 u64s; /* size of d[] in u64s */
1457
1458 __u8 encrypted_start[0];
1459
1460 __le16 read_clock;
1461 __le16 write_clock;
1462
1463 /* Sequence number of oldest dirty journal entry */
1464 __le64 last_seq;
1465
1466
1467 union {
1468 struct jset_entry start[0];
1469 __u64 _data[0];
1470 };
1471} __attribute__((packed, aligned(8)));
1472
1473LE32_BITMASK(JSET_CSUM_TYPE, struct jset, flags, 0, 4);
1474LE32_BITMASK(JSET_BIG_ENDIAN, struct jset, flags, 4, 5);
1475
8b335bae 1476#define BCH_JOURNAL_BUCKETS_MIN 8
1c6fdbd8
KO
1477
1478/* Btree: */
1479
26609b61
KO
1480#define BCH_BTREE_IDS() \
1481 x(EXTENTS, 0, "extents") \
1482 x(INODES, 1, "inodes") \
1483 x(DIRENTS, 2, "dirents") \
1484 x(XATTRS, 3, "xattrs") \
1485 x(ALLOC, 4, "alloc") \
1486 x(QUOTAS, 5, "quotas") \
1487 x(EC, 6, "erasure_coding")
1c6fdbd8
KO
1488
1489enum btree_id {
26609b61
KO
1490#define x(kwd, val, name) BTREE_ID_##kwd = val,
1491 BCH_BTREE_IDS()
1492#undef x
1c6fdbd8
KO
1493 BTREE_ID_NR
1494};
1495
1c6fdbd8
KO
1496#define BTREE_MAX_DEPTH 4U
1497
1498/* Btree nodes */
1499
1c6fdbd8
KO
1500/*
1501 * Btree nodes
1502 *
1503 * On disk a btree node is a list/log of these; within each set the keys are
1504 * sorted
1505 */
1506struct bset {
1507 __le64 seq;
1508
1509 /*
1510 * Highest journal entry this bset contains keys for.
1511 * If on recovery we don't see that journal entry, this bset is ignored:
1512 * this allows us to preserve the order of all index updates after a
1513 * crash, since the journal records a total order of all index updates
1514 * and anything that didn't make it to the journal doesn't get used.
1515 */
1516 __le64 journal_seq;
1517
1518 __le32 flags;
1519 __le16 version;
1520 __le16 u64s; /* count of d[] in u64s */
1521
1522 union {
1523 struct bkey_packed start[0];
1524 __u64 _data[0];
1525 };
1526} __attribute__((packed, aligned(8)));
1527
1528LE32_BITMASK(BSET_CSUM_TYPE, struct bset, flags, 0, 4);
1529
1530LE32_BITMASK(BSET_BIG_ENDIAN, struct bset, flags, 4, 5);
1531LE32_BITMASK(BSET_SEPARATE_WHITEOUTS,
1532 struct bset, flags, 5, 6);
1533
1534struct btree_node {
1535 struct bch_csum csum;
1536 __le64 magic;
1537
1538 /* this flags field is encrypted, unlike bset->flags: */
1539 __le64 flags;
1540
1541 /* Closed interval: */
1542 struct bpos min_key;
1543 struct bpos max_key;
1544 struct bch_extent_ptr ptr;
1545 struct bkey_format format;
1546
1547 union {
1548 struct bset keys;
1549 struct {
1550 __u8 pad[22];
1551 __le16 u64s;
1552 __u64 _data[0];
1553
1554 };
1555 };
1556} __attribute__((packed, aligned(8)));
1557
1558LE64_BITMASK(BTREE_NODE_ID, struct btree_node, flags, 0, 4);
1559LE64_BITMASK(BTREE_NODE_LEVEL, struct btree_node, flags, 4, 8);
1560/* 8-32 unused */
1561LE64_BITMASK(BTREE_NODE_SEQ, struct btree_node, flags, 32, 64);
1562
1563struct btree_node_entry {
1564 struct bch_csum csum;
1565
1566 union {
1567 struct bset keys;
1568 struct {
1569 __u8 pad[22];
1570 __le16 u64s;
1571 __u64 _data[0];
1572
1573 };
1574 };
1575} __attribute__((packed, aligned(8)));
1576
1577#endif /* _BCACHEFS_FORMAT_H */