bcachefs: introduce b->hash_val
[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) \
76426098
KO
343 x(stripe, 14) \
344 x(reflink_p, 15) \
4be1a412
KO
345 x(reflink_v, 16) \
346 x(inline_data, 17)
26609b61
KO
347
348enum bch_bkey_type {
349#define x(name, nr) KEY_TYPE_##name = nr,
350 BCH_BKEY_TYPES()
351#undef x
352 KEY_TYPE_MAX,
353};
1c6fdbd8
KO
354
355struct bch_cookie {
356 struct bch_val v;
357 __le64 cookie;
358};
1c6fdbd8
KO
359
360/* Extents */
361
362/*
363 * In extent bkeys, the value is a list of pointers (bch_extent_ptr), optionally
364 * preceded by checksum/compression information (bch_extent_crc32 or
365 * bch_extent_crc64).
366 *
367 * One major determining factor in the format of extents is how we handle and
368 * represent extents that have been partially overwritten and thus trimmed:
369 *
370 * If an extent is not checksummed or compressed, when the extent is trimmed we
371 * don't have to remember the extent we originally allocated and wrote: we can
372 * merely adjust ptr->offset to point to the start of the data that is currently
373 * live. The size field in struct bkey records the current (live) size of the
374 * extent, and is also used to mean "size of region on disk that we point to" in
375 * this case.
376 *
377 * Thus an extent that is not checksummed or compressed will consist only of a
378 * list of bch_extent_ptrs, with none of the fields in
379 * bch_extent_crc32/bch_extent_crc64.
380 *
381 * When an extent is checksummed or compressed, it's not possible to read only
382 * the data that is currently live: we have to read the entire extent that was
383 * originally written, and then return only the part of the extent that is
384 * currently live.
385 *
386 * Thus, in addition to the current size of the extent in struct bkey, we need
387 * to store the size of the originally allocated space - this is the
388 * compressed_size and uncompressed_size fields in bch_extent_crc32/64. Also,
389 * when the extent is trimmed, instead of modifying the offset field of the
390 * pointer, we keep a second smaller offset field - "offset into the original
391 * extent of the currently live region".
392 *
393 * The other major determining factor is replication and data migration:
394 *
395 * Each pointer may have its own bch_extent_crc32/64. When doing a replicated
396 * write, we will initially write all the replicas in the same format, with the
397 * same checksum type and compression format - however, when copygc runs later (or
398 * tiering/cache promotion, anything that moves data), it is not in general
399 * going to rewrite all the pointers at once - one of the replicas may be in a
400 * bucket on one device that has very little fragmentation while another lives
401 * in a bucket that has become heavily fragmented, and thus is being rewritten
402 * sooner than the rest.
403 *
404 * Thus it will only move a subset of the pointers (or in the case of
405 * tiering/cache promotion perhaps add a single pointer without dropping any
406 * current pointers), and if the extent has been partially overwritten it must
407 * write only the currently live portion (or copygc would not be able to reduce
408 * fragmentation!) - which necessitates a different bch_extent_crc format for
409 * the new pointer.
410 *
411 * But in the interests of space efficiency, we don't want to store one
412 * bch_extent_crc for each pointer if we don't have to.
413 *
414 * Thus, a bch_extent consists of bch_extent_crc32s, bch_extent_crc64s, and
415 * bch_extent_ptrs appended arbitrarily one after the other. We determine the
416 * type of a given entry with a scheme similar to utf8 (except we're encoding a
417 * type, not a size), encoding the type in the position of the first set bit:
418 *
419 * bch_extent_crc32 - 0b1
420 * bch_extent_ptr - 0b10
421 * bch_extent_crc64 - 0b100
422 *
423 * We do it this way because bch_extent_crc32 is _very_ constrained on bits (and
424 * bch_extent_crc64 is the least constrained).
425 *
426 * Then, each bch_extent_crc32/64 applies to the pointers that follow after it,
427 * until the next bch_extent_crc32/64.
428 *
429 * If there are no bch_extent_crcs preceding a bch_extent_ptr, then that pointer
430 * is neither checksummed nor compressed.
431 */
432
433/* 128 bits, sufficient for cryptographic MACs: */
434struct bch_csum {
435 __le64 lo;
436 __le64 hi;
437} __attribute__((packed, aligned(8)));
438
abce30b7
KO
439#define BCH_EXTENT_ENTRY_TYPES() \
440 x(ptr, 0) \
441 x(crc32, 1) \
442 x(crc64, 2) \
cd575ddf
KO
443 x(crc128, 3) \
444 x(stripe_ptr, 4)
445#define BCH_EXTENT_ENTRY_MAX 5
abce30b7 446
1c6fdbd8 447enum bch_extent_entry_type {
abce30b7
KO
448#define x(f, n) BCH_EXTENT_ENTRY_##f = n,
449 BCH_EXTENT_ENTRY_TYPES()
450#undef x
1c6fdbd8
KO
451};
452
1c6fdbd8
KO
453/* Compressed/uncompressed size are stored biased by 1: */
454struct bch_extent_crc32 {
455#if defined(__LITTLE_ENDIAN_BITFIELD)
456 __u32 type:2,
457 _compressed_size:7,
458 _uncompressed_size:7,
459 offset:7,
460 _unused:1,
461 csum_type:4,
462 compression_type:4;
463 __u32 csum;
464#elif defined (__BIG_ENDIAN_BITFIELD)
465 __u32 csum;
466 __u32 compression_type:4,
467 csum_type:4,
468 _unused:1,
469 offset:7,
470 _uncompressed_size:7,
471 _compressed_size:7,
472 type:2;
473#endif
474} __attribute__((packed, aligned(8)));
475
476#define CRC32_SIZE_MAX (1U << 7)
477#define CRC32_NONCE_MAX 0
478
479struct bch_extent_crc64 {
480#if defined(__LITTLE_ENDIAN_BITFIELD)
481 __u64 type:3,
482 _compressed_size:9,
483 _uncompressed_size:9,
484 offset:9,
485 nonce:10,
486 csum_type:4,
487 compression_type:4,
488 csum_hi:16;
489#elif defined (__BIG_ENDIAN_BITFIELD)
490 __u64 csum_hi:16,
491 compression_type:4,
492 csum_type:4,
493 nonce:10,
494 offset:9,
495 _uncompressed_size:9,
496 _compressed_size:9,
497 type:3;
498#endif
499 __u64 csum_lo;
500} __attribute__((packed, aligned(8)));
501
502#define CRC64_SIZE_MAX (1U << 9)
503#define CRC64_NONCE_MAX ((1U << 10) - 1)
504
505struct bch_extent_crc128 {
506#if defined(__LITTLE_ENDIAN_BITFIELD)
507 __u64 type:4,
508 _compressed_size:13,
509 _uncompressed_size:13,
510 offset:13,
511 nonce:13,
512 csum_type:4,
513 compression_type:4;
514#elif defined (__BIG_ENDIAN_BITFIELD)
515 __u64 compression_type:4,
516 csum_type:4,
517 nonce:13,
518 offset:13,
519 _uncompressed_size:13,
520 _compressed_size:13,
521 type:4;
522#endif
523 struct bch_csum csum;
524} __attribute__((packed, aligned(8)));
525
526#define CRC128_SIZE_MAX (1U << 13)
527#define CRC128_NONCE_MAX ((1U << 13) - 1)
528
529/*
530 * @reservation - pointer hasn't been written to, just reserved
531 */
532struct bch_extent_ptr {
533#if defined(__LITTLE_ENDIAN_BITFIELD)
534 __u64 type:1,
535 cached:1,
cd575ddf 536 unused:1,
1c6fdbd8
KO
537 reservation:1,
538 offset:44, /* 8 petabytes */
539 dev:8,
540 gen:8;
541#elif defined (__BIG_ENDIAN_BITFIELD)
542 __u64 gen:8,
543 dev:8,
544 offset:44,
545 reservation:1,
cd575ddf 546 unused:1,
1c6fdbd8
KO
547 cached:1,
548 type:1;
549#endif
550} __attribute__((packed, aligned(8)));
551
cd575ddf 552struct bch_extent_stripe_ptr {
1c6fdbd8
KO
553#if defined(__LITTLE_ENDIAN_BITFIELD)
554 __u64 type:5,
cd575ddf
KO
555 block:8,
556 idx:51;
557#elif defined (__BIG_ENDIAN_BITFIELD)
558 __u64 idx:51,
559 block:8,
560 type:5;
561#endif
562};
563
564struct bch_extent_reservation {
565#if defined(__LITTLE_ENDIAN_BITFIELD)
566 __u64 type:6,
567 unused:22,
1c6fdbd8
KO
568 replicas:4,
569 generation:32;
570#elif defined (__BIG_ENDIAN_BITFIELD)
571 __u64 generation:32,
572 replicas:4,
cd575ddf
KO
573 unused:22,
574 type:6;
1c6fdbd8
KO
575#endif
576};
577
578union bch_extent_entry {
579#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ || __BITS_PER_LONG == 64
580 unsigned long type;
581#elif __BITS_PER_LONG == 32
582 struct {
583 unsigned long pad;
584 unsigned long type;
585 };
586#else
587#error edit for your odd byteorder.
588#endif
abce30b7
KO
589
590#define x(f, n) struct bch_extent_##f f;
591 BCH_EXTENT_ENTRY_TYPES()
592#undef x
1c6fdbd8
KO
593};
594
26609b61
KO
595struct bch_btree_ptr {
596 struct bch_val v;
1c6fdbd8 597
26609b61
KO
598 __u64 _data[0];
599 struct bch_extent_ptr start[];
600} __attribute__((packed, aligned(8)));
1c6fdbd8
KO
601
602struct bch_extent {
603 struct bch_val v;
604
605 __u64 _data[0];
606 union bch_extent_entry start[];
607} __attribute__((packed, aligned(8)));
1c6fdbd8
KO
608
609struct bch_reservation {
610 struct bch_val v;
611
612 __le32 generation;
613 __u8 nr_replicas;
614 __u8 pad[3];
615} __attribute__((packed, aligned(8)));
1c6fdbd8
KO
616
617/* Maximum size (in u64s) a single pointer could be: */
618#define BKEY_EXTENT_PTR_U64s_MAX\
619 ((sizeof(struct bch_extent_crc128) + \
620 sizeof(struct bch_extent_ptr)) / sizeof(u64))
621
622/* Maximum possible size of an entire extent value: */
623#define BKEY_EXTENT_VAL_U64s_MAX \
5055b509 624 (1 + BKEY_EXTENT_PTR_U64s_MAX * (BCH_REPLICAS_MAX + 1))
1c6fdbd8
KO
625
626#define BKEY_PADDED(key) __BKEY_PADDED(key, BKEY_EXTENT_VAL_U64s_MAX)
627
628/* * Maximum possible size of an entire extent, key + value: */
629#define BKEY_EXTENT_U64s_MAX (BKEY_U64s + BKEY_EXTENT_VAL_U64s_MAX)
630
631/* Btree pointers don't carry around checksums: */
632#define BKEY_BTREE_PTR_VAL_U64s_MAX \
633 ((sizeof(struct bch_extent_ptr)) / sizeof(u64) * BCH_REPLICAS_MAX)
634#define BKEY_BTREE_PTR_U64s_MAX \
635 (BKEY_U64s + BKEY_BTREE_PTR_VAL_U64s_MAX)
636
637/* Inodes */
638
639#define BLOCKDEV_INODE_MAX 4096
640
641#define BCACHEFS_ROOT_INO 4096
642
1c6fdbd8
KO
643struct bch_inode {
644 struct bch_val v;
645
646 __le64 bi_hash_seed;
647 __le32 bi_flags;
648 __le16 bi_mode;
649 __u8 fields[0];
650} __attribute__((packed, aligned(8)));
1c6fdbd8
KO
651
652struct bch_inode_generation {
653 struct bch_val v;
654
655 __le32 bi_generation;
656 __le32 pad;
657} __attribute__((packed, aligned(8)));
1c6fdbd8 658
a3e70fb2
KO
659#define BCH_INODE_FIELDS() \
660 x(bi_atime, 64) \
661 x(bi_ctime, 64) \
662 x(bi_mtime, 64) \
663 x(bi_otime, 64) \
664 x(bi_size, 64) \
665 x(bi_sectors, 64) \
666 x(bi_uid, 32) \
667 x(bi_gid, 32) \
668 x(bi_nlink, 32) \
669 x(bi_generation, 32) \
670 x(bi_dev, 32) \
671 x(bi_data_checksum, 8) \
672 x(bi_compression, 8) \
673 x(bi_project, 32) \
674 x(bi_background_compression, 8) \
675 x(bi_data_replicas, 8) \
676 x(bi_promote_target, 16) \
677 x(bi_foreground_target, 16) \
678 x(bi_background_target, 16) \
721d4ad8
KO
679 x(bi_erasure_code, 16) \
680 x(bi_fields_set, 16)
a3e70fb2 681
d42dd4ad
KO
682/* subset of BCH_INODE_FIELDS */
683#define BCH_INODE_OPTS() \
684 x(data_checksum, 8) \
685 x(compression, 8) \
686 x(project, 32) \
687 x(background_compression, 8) \
688 x(data_replicas, 8) \
689 x(promote_target, 16) \
690 x(foreground_target, 16) \
691 x(background_target, 16) \
692 x(erasure_code, 16)
1c6fdbd8 693
721d4ad8
KO
694enum inode_opt_id {
695#define x(name, ...) \
696 Inode_opt_##name,
697 BCH_INODE_OPTS()
698#undef x
699 Inode_opt_nr,
700};
701
1c6fdbd8
KO
702enum {
703 /*
704 * User flags (get/settable with FS_IOC_*FLAGS, correspond to FS_*_FL
705 * flags)
706 */
707 __BCH_INODE_SYNC = 0,
708 __BCH_INODE_IMMUTABLE = 1,
709 __BCH_INODE_APPEND = 2,
710 __BCH_INODE_NODUMP = 3,
711 __BCH_INODE_NOATIME = 4,
712
713 __BCH_INODE_I_SIZE_DIRTY= 5,
714 __BCH_INODE_I_SECTORS_DIRTY= 6,
715 __BCH_INODE_UNLINKED = 7,
716
717 /* bits 20+ reserved for packed fields below: */
718};
719
720#define BCH_INODE_SYNC (1 << __BCH_INODE_SYNC)
721#define BCH_INODE_IMMUTABLE (1 << __BCH_INODE_IMMUTABLE)
722#define BCH_INODE_APPEND (1 << __BCH_INODE_APPEND)
723#define BCH_INODE_NODUMP (1 << __BCH_INODE_NODUMP)
724#define BCH_INODE_NOATIME (1 << __BCH_INODE_NOATIME)
725#define BCH_INODE_I_SIZE_DIRTY (1 << __BCH_INODE_I_SIZE_DIRTY)
726#define BCH_INODE_I_SECTORS_DIRTY (1 << __BCH_INODE_I_SECTORS_DIRTY)
727#define BCH_INODE_UNLINKED (1 << __BCH_INODE_UNLINKED)
728
729LE32_BITMASK(INODE_STR_HASH, struct bch_inode, bi_flags, 20, 24);
730LE32_BITMASK(INODE_NR_FIELDS, struct bch_inode, bi_flags, 24, 32);
731
1c6fdbd8
KO
732/* Dirents */
733
734/*
735 * Dirents (and xattrs) have to implement string lookups; since our b-tree
736 * doesn't support arbitrary length strings for the key, we instead index by a
737 * 64 bit hash (currently truncated sha1) of the string, stored in the offset
738 * field of the key - using linear probing to resolve hash collisions. This also
739 * provides us with the readdir cookie posix requires.
740 *
741 * Linear probing requires us to use whiteouts for deletions, in the event of a
742 * collision:
743 */
744
1c6fdbd8
KO
745struct bch_dirent {
746 struct bch_val v;
747
748 /* Target inode number: */
749 __le64 d_inum;
750
751 /*
752 * Copy of mode bits 12-15 from the target inode - so userspace can get
753 * the filetype without having to do a stat()
754 */
755 __u8 d_type;
756
757 __u8 d_name[];
758} __attribute__((packed, aligned(8)));
1c6fdbd8
KO
759
760#define BCH_NAME_MAX (U8_MAX * sizeof(u64) - \
761 sizeof(struct bkey) - \
762 offsetof(struct bch_dirent, d_name))
763
764
765/* Xattrs */
766
26609b61
KO
767#define KEY_TYPE_XATTR_INDEX_USER 0
768#define KEY_TYPE_XATTR_INDEX_POSIX_ACL_ACCESS 1
769#define KEY_TYPE_XATTR_INDEX_POSIX_ACL_DEFAULT 2
770#define KEY_TYPE_XATTR_INDEX_TRUSTED 3
771#define KEY_TYPE_XATTR_INDEX_SECURITY 4
1c6fdbd8
KO
772
773struct bch_xattr {
774 struct bch_val v;
775 __u8 x_type;
776 __u8 x_name_len;
777 __le16 x_val_len;
778 __u8 x_name[];
779} __attribute__((packed, aligned(8)));
1c6fdbd8
KO
780
781/* Bucket/allocation information: */
782
1c6fdbd8
KO
783struct bch_alloc {
784 struct bch_val v;
785 __u8 fields;
786 __u8 gen;
787 __u8 data[];
788} __attribute__((packed, aligned(8)));
1c6fdbd8 789
90541a74 790#define BCH_ALLOC_FIELDS() \
8fe826f9
KO
791 x(read_time, 16) \
792 x(write_time, 16) \
793 x(data_type, 8) \
794 x(dirty_sectors, 16) \
795 x(cached_sectors, 16) \
796 x(oldest_gen, 8)
90541a74
KO
797
798enum {
799#define x(name, bytes) BCH_ALLOC_FIELD_##name,
800 BCH_ALLOC_FIELDS()
801#undef x
802 BCH_ALLOC_FIELD_NR
803};
804
805static const unsigned BCH_ALLOC_FIELD_BYTES[] = {
8fe826f9 806#define x(name, bits) [BCH_ALLOC_FIELD_##name] = bits / 8,
90541a74
KO
807 BCH_ALLOC_FIELDS()
808#undef x
809};
810
8fe826f9 811#define x(name, bits) + (bits / 8)
90541a74
KO
812static const unsigned BKEY_ALLOC_VAL_U64s_MAX =
813 DIV_ROUND_UP(offsetof(struct bch_alloc, data)
814 BCH_ALLOC_FIELDS(), sizeof(u64));
815#undef x
816
d74dfe02 817#define BKEY_ALLOC_U64s_MAX (BKEY_U64s + BKEY_ALLOC_VAL_U64s_MAX)
91052b9d 818
1c6fdbd8
KO
819/* Quotas: */
820
1c6fdbd8
KO
821enum quota_types {
822 QTYP_USR = 0,
823 QTYP_GRP = 1,
824 QTYP_PRJ = 2,
825 QTYP_NR = 3,
826};
827
828enum quota_counters {
829 Q_SPC = 0,
830 Q_INO = 1,
831 Q_COUNTERS = 2,
832};
833
834struct bch_quota_counter {
835 __le64 hardlimit;
836 __le64 softlimit;
837};
838
839struct bch_quota {
840 struct bch_val v;
841 struct bch_quota_counter c[Q_COUNTERS];
842} __attribute__((packed, aligned(8)));
1c6fdbd8 843
cd575ddf
KO
844/* Erasure coding */
845
cd575ddf
KO
846struct bch_stripe {
847 struct bch_val v;
848 __le16 sectors;
849 __u8 algorithm;
850 __u8 nr_blocks;
851 __u8 nr_redundant;
852
853 __u8 csum_granularity_bits;
854 __u8 csum_type;
855 __u8 pad;
856
857 struct bch_extent_ptr ptrs[0];
858} __attribute__((packed, aligned(8)));
cd575ddf 859
76426098
KO
860/* Reflink: */
861
862struct bch_reflink_p {
863 struct bch_val v;
864 __le64 idx;
865
866 __le32 reservation_generation;
867 __u8 nr_replicas;
868 __u8 pad[3];
869};
870
871struct bch_reflink_v {
872 struct bch_val v;
873 __le64 refcount;
874 union bch_extent_entry start[0];
875 __u64 _data[0];
876};
877
4be1a412
KO
878/* Inline data */
879
880struct bch_inline_data {
881 struct bch_val v;
882 u8 data[0];
883};
884
1c6fdbd8
KO
885/* Optional/variable size superblock sections: */
886
887struct bch_sb_field {
888 __u64 _data[0];
889 __le32 u64s;
890 __le32 type;
891};
892
893#define BCH_SB_FIELDS() \
894 x(journal, 0) \
895 x(members, 1) \
896 x(crypt, 2) \
af9d3bc2 897 x(replicas_v0, 3) \
1c6fdbd8
KO
898 x(quota, 4) \
899 x(disk_groups, 5) \
af9d3bc2 900 x(clean, 6) \
1dd7f9d9
KO
901 x(replicas, 7) \
902 x(journal_seq_blacklist, 8)
1c6fdbd8
KO
903
904enum bch_sb_field_type {
905#define x(f, nr) BCH_SB_FIELD_##f = nr,
906 BCH_SB_FIELDS()
907#undef x
908 BCH_SB_FIELD_NR
909};
910
911/* BCH_SB_FIELD_journal: */
912
913struct bch_sb_field_journal {
914 struct bch_sb_field field;
915 __le64 buckets[0];
916};
917
918/* BCH_SB_FIELD_members: */
919
8b335bae
KO
920#define BCH_MIN_NR_NBUCKETS (1 << 6)
921
1c6fdbd8
KO
922struct bch_member {
923 __uuid_t uuid;
924 __le64 nbuckets; /* device size */
925 __le16 first_bucket; /* index of first bucket used */
926 __le16 bucket_size; /* sectors */
927 __le32 pad;
928 __le64 last_mount; /* time_t */
929
930 __le64 flags[2];
931};
932
933LE64_BITMASK(BCH_MEMBER_STATE, struct bch_member, flags[0], 0, 4)
934/* 4-10 unused, was TIER, HAS_(META)DATA */
935LE64_BITMASK(BCH_MEMBER_REPLACEMENT, struct bch_member, flags[0], 10, 14)
936LE64_BITMASK(BCH_MEMBER_DISCARD, struct bch_member, flags[0], 14, 15)
937LE64_BITMASK(BCH_MEMBER_DATA_ALLOWED, struct bch_member, flags[0], 15, 20)
938LE64_BITMASK(BCH_MEMBER_GROUP, struct bch_member, flags[0], 20, 28)
939LE64_BITMASK(BCH_MEMBER_DURABILITY, struct bch_member, flags[0], 28, 30)
940
941#define BCH_TIER_MAX 4U
942
943#if 0
944LE64_BITMASK(BCH_MEMBER_NR_READ_ERRORS, struct bch_member, flags[1], 0, 20);
945LE64_BITMASK(BCH_MEMBER_NR_WRITE_ERRORS,struct bch_member, flags[1], 20, 40);
946#endif
947
948enum bch_member_state {
949 BCH_MEMBER_STATE_RW = 0,
950 BCH_MEMBER_STATE_RO = 1,
951 BCH_MEMBER_STATE_FAILED = 2,
952 BCH_MEMBER_STATE_SPARE = 3,
953 BCH_MEMBER_STATE_NR = 4,
954};
955
956enum cache_replacement {
957 CACHE_REPLACEMENT_LRU = 0,
958 CACHE_REPLACEMENT_FIFO = 1,
959 CACHE_REPLACEMENT_RANDOM = 2,
960 CACHE_REPLACEMENT_NR = 3,
961};
962
963struct bch_sb_field_members {
964 struct bch_sb_field field;
965 struct bch_member members[0];
966};
967
968/* BCH_SB_FIELD_crypt: */
969
970struct nonce {
971 __le32 d[4];
972};
973
974struct bch_key {
975 __le64 key[4];
976};
977
978#define BCH_KEY_MAGIC \
979 (((u64) 'b' << 0)|((u64) 'c' << 8)| \
980 ((u64) 'h' << 16)|((u64) '*' << 24)| \
981 ((u64) '*' << 32)|((u64) 'k' << 40)| \
982 ((u64) 'e' << 48)|((u64) 'y' << 56))
983
984struct bch_encrypted_key {
985 __le64 magic;
986 struct bch_key key;
987};
988
989/*
990 * If this field is present in the superblock, it stores an encryption key which
991 * is used encrypt all other data/metadata. The key will normally be encrypted
992 * with the key userspace provides, but if encryption has been turned off we'll
993 * just store the master key unencrypted in the superblock so we can access the
994 * previously encrypted data.
995 */
996struct bch_sb_field_crypt {
997 struct bch_sb_field field;
998
999 __le64 flags;
1000 __le64 kdf_flags;
1001 struct bch_encrypted_key key;
1002};
1003
1004LE64_BITMASK(BCH_CRYPT_KDF_TYPE, struct bch_sb_field_crypt, flags, 0, 4);
1005
1006enum bch_kdf_types {
1007 BCH_KDF_SCRYPT = 0,
1008 BCH_KDF_NR = 1,
1009};
1010
1011/* stored as base 2 log of scrypt params: */
1012LE64_BITMASK(BCH_KDF_SCRYPT_N, struct bch_sb_field_crypt, kdf_flags, 0, 16);
1013LE64_BITMASK(BCH_KDF_SCRYPT_R, struct bch_sb_field_crypt, kdf_flags, 16, 32);
1014LE64_BITMASK(BCH_KDF_SCRYPT_P, struct bch_sb_field_crypt, kdf_flags, 32, 48);
1015
1016/* BCH_SB_FIELD_replicas: */
1017
1018enum bch_data_type {
1019 BCH_DATA_NONE = 0,
1020 BCH_DATA_SB = 1,
1021 BCH_DATA_JOURNAL = 2,
1022 BCH_DATA_BTREE = 3,
1023 BCH_DATA_USER = 4,
1024 BCH_DATA_CACHED = 5,
1025 BCH_DATA_NR = 6,
1026};
1027
af9d3bc2
KO
1028struct bch_replicas_entry_v0 {
1029 __u8 data_type;
1030 __u8 nr_devs;
1031 __u8 devs[];
1032} __attribute__((packed));
1033
1034struct bch_sb_field_replicas_v0 {
1035 struct bch_sb_field field;
1036 struct bch_replicas_entry_v0 entries[];
1037} __attribute__((packed, aligned(8)));
1038
1c6fdbd8 1039struct bch_replicas_entry {
7a920560
KO
1040 __u8 data_type;
1041 __u8 nr_devs;
af9d3bc2 1042 __u8 nr_required;
7a920560 1043 __u8 devs[];
af9d3bc2 1044} __attribute__((packed));
1c6fdbd8 1045
22502ac2
KO
1046#define replicas_entry_bytes(_i) \
1047 (offsetof(typeof(*(_i)), devs) + (_i)->nr_devs)
1048
1c6fdbd8
KO
1049struct bch_sb_field_replicas {
1050 struct bch_sb_field field;
1051 struct bch_replicas_entry entries[];
af9d3bc2 1052} __attribute__((packed, aligned(8)));
1c6fdbd8
KO
1053
1054/* BCH_SB_FIELD_quota: */
1055
1056struct bch_sb_quota_counter {
1057 __le32 timelimit;
1058 __le32 warnlimit;
1059};
1060
1061struct bch_sb_quota_type {
1062 __le64 flags;
1063 struct bch_sb_quota_counter c[Q_COUNTERS];
1064};
1065
1066struct bch_sb_field_quota {
1067 struct bch_sb_field field;
1068 struct bch_sb_quota_type q[QTYP_NR];
1069} __attribute__((packed, aligned(8)));
1070
1071/* BCH_SB_FIELD_disk_groups: */
1072
1073#define BCH_SB_LABEL_SIZE 32
1074
1075struct bch_disk_group {
1076 __u8 label[BCH_SB_LABEL_SIZE];
1077 __le64 flags[2];
cd575ddf 1078} __attribute__((packed, aligned(8)));
1c6fdbd8
KO
1079
1080LE64_BITMASK(BCH_GROUP_DELETED, struct bch_disk_group, flags[0], 0, 1)
1081LE64_BITMASK(BCH_GROUP_DATA_ALLOWED, struct bch_disk_group, flags[0], 1, 6)
1082LE64_BITMASK(BCH_GROUP_PARENT, struct bch_disk_group, flags[0], 6, 24)
1083
1084struct bch_sb_field_disk_groups {
1085 struct bch_sb_field field;
1086 struct bch_disk_group entries[0];
cd575ddf 1087} __attribute__((packed, aligned(8)));
1c6fdbd8
KO
1088
1089/*
1090 * On clean shutdown, store btree roots and current journal sequence number in
1091 * the superblock:
1092 */
1093struct jset_entry {
1094 __le16 u64s;
1095 __u8 btree_id;
1096 __u8 level;
1097 __u8 type; /* designates what this jset holds */
1098 __u8 pad[3];
1099
1100 union {
1101 struct bkey_i start[0];
1102 __u64 _data[0];
1103 };
1104};
1105
1106struct bch_sb_field_clean {
1107 struct bch_sb_field field;
1108
1109 __le32 flags;
1110 __le16 read_clock;
1111 __le16 write_clock;
1112 __le64 journal_seq;
1113
1114 union {
1115 struct jset_entry start[0];
1116 __u64 _data[0];
1117 };
1118};
1119
1dd7f9d9
KO
1120struct journal_seq_blacklist_entry {
1121 __le64 start;
1122 __le64 end;
1123};
1124
1125struct bch_sb_field_journal_seq_blacklist {
1126 struct bch_sb_field field;
1127
1128 union {
1129 struct journal_seq_blacklist_entry start[0];
1130 __u64 _data[0];
1131 };
1132};
1133
1c6fdbd8
KO
1134/* Superblock: */
1135
1136/*
26609b61
KO
1137 * New versioning scheme:
1138 * One common version number for all on disk data structures - superblock, btree
1139 * nodes, journal entries
1c6fdbd8 1140 */
26609b61
KO
1141#define BCH_JSET_VERSION_OLD 2
1142#define BCH_BSET_VERSION_OLD 3
1143
1144enum bcachefs_metadata_version {
1145 bcachefs_metadata_version_min = 9,
1146 bcachefs_metadata_version_new_versioning = 10,
1147 bcachefs_metadata_version_bkey_renumber = 10,
1148 bcachefs_metadata_version_max = 11,
1149};
1c6fdbd8 1150
26609b61 1151#define bcachefs_metadata_version_current (bcachefs_metadata_version_max - 1)
1c6fdbd8
KO
1152
1153#define BCH_SB_SECTOR 8
1154#define BCH_SB_MEMBERS_MAX 64 /* XXX kill */
1155
1156struct bch_sb_layout {
1157 __uuid_t magic; /* bcachefs superblock UUID */
1158 __u8 layout_type;
1159 __u8 sb_max_size_bits; /* base 2 of 512 byte sectors */
1160 __u8 nr_superblocks;
1161 __u8 pad[5];
1162 __le64 sb_offset[61];
1163} __attribute__((packed, aligned(8)));
1164
1165#define BCH_SB_LAYOUT_SECTOR 7
1166
1167/*
1168 * @offset - sector where this sb was written
1169 * @version - on disk format version
26609b61
KO
1170 * @version_min - Oldest metadata version this filesystem contains; so we can
1171 * safely drop compatibility code and refuse to mount filesystems
1172 * we'd need it for
1c6fdbd8
KO
1173 * @magic - identifies as a bcachefs superblock (BCACHE_MAGIC)
1174 * @seq - incremented each time superblock is written
1175 * @uuid - used for generating various magic numbers and identifying
1176 * member devices, never changes
1177 * @user_uuid - user visible UUID, may be changed
1178 * @label - filesystem label
1179 * @seq - identifies most recent superblock, incremented each time
1180 * superblock is written
1181 * @features - enabled incompatible features
1182 */
1183struct bch_sb {
1184 struct bch_csum csum;
1185 __le16 version;
1186 __le16 version_min;
1187 __le16 pad[2];
1188 __uuid_t magic;
1189 __uuid_t uuid;
1190 __uuid_t user_uuid;
1191 __u8 label[BCH_SB_LABEL_SIZE];
1192 __le64 offset;
1193 __le64 seq;
1194
1195 __le16 block_size;
1196 __u8 dev_idx;
1197 __u8 nr_devices;
1198 __le32 u64s;
1199
1200 __le64 time_base_lo;
1201 __le32 time_base_hi;
1202 __le32 time_precision;
1203
1204 __le64 flags[8];
1205 __le64 features[2];
1206 __le64 compat[2];
1207
1208 struct bch_sb_layout layout;
1209
1210 union {
1211 struct bch_sb_field start[0];
1212 __le64 _data[0];
1213 };
1214} __attribute__((packed, aligned(8)));
1215
1216/*
1217 * Flags:
1218 * BCH_SB_INITALIZED - set on first mount
1219 * BCH_SB_CLEAN - did we shut down cleanly? Just a hint, doesn't affect
1220 * behaviour of mount/recovery path:
1221 * BCH_SB_INODE_32BIT - limit inode numbers to 32 bits
1222 * BCH_SB_128_BIT_MACS - 128 bit macs instead of 80
1223 * BCH_SB_ENCRYPTION_TYPE - if nonzero encryption is enabled; overrides
1224 * DATA/META_CSUM_TYPE. Also indicates encryption
1225 * algorithm in use, if/when we get more than one
1226 */
1227
1228LE16_BITMASK(BCH_SB_BLOCK_SIZE, struct bch_sb, block_size, 0, 16);
1229
1230LE64_BITMASK(BCH_SB_INITIALIZED, struct bch_sb, flags[0], 0, 1);
1231LE64_BITMASK(BCH_SB_CLEAN, struct bch_sb, flags[0], 1, 2);
1232LE64_BITMASK(BCH_SB_CSUM_TYPE, struct bch_sb, flags[0], 2, 8);
1233LE64_BITMASK(BCH_SB_ERROR_ACTION, struct bch_sb, flags[0], 8, 12);
1234
1235LE64_BITMASK(BCH_SB_BTREE_NODE_SIZE, struct bch_sb, flags[0], 12, 28);
1236
1237LE64_BITMASK(BCH_SB_GC_RESERVE, struct bch_sb, flags[0], 28, 33);
1238LE64_BITMASK(BCH_SB_ROOT_RESERVE, struct bch_sb, flags[0], 33, 40);
1239
1240LE64_BITMASK(BCH_SB_META_CSUM_TYPE, struct bch_sb, flags[0], 40, 44);
1241LE64_BITMASK(BCH_SB_DATA_CSUM_TYPE, struct bch_sb, flags[0], 44, 48);
1242
1243LE64_BITMASK(BCH_SB_META_REPLICAS_WANT, struct bch_sb, flags[0], 48, 52);
1244LE64_BITMASK(BCH_SB_DATA_REPLICAS_WANT, struct bch_sb, flags[0], 52, 56);
1245
1246LE64_BITMASK(BCH_SB_POSIX_ACL, struct bch_sb, flags[0], 56, 57);
1247LE64_BITMASK(BCH_SB_USRQUOTA, struct bch_sb, flags[0], 57, 58);
1248LE64_BITMASK(BCH_SB_GRPQUOTA, struct bch_sb, flags[0], 58, 59);
1249LE64_BITMASK(BCH_SB_PRJQUOTA, struct bch_sb, flags[0], 59, 60);
1250
0bc166ff
KO
1251LE64_BITMASK(BCH_SB_HAS_ERRORS, struct bch_sb, flags[0], 60, 61);
1252
1253/* 61-64 unused */
1c6fdbd8
KO
1254
1255LE64_BITMASK(BCH_SB_STR_HASH_TYPE, struct bch_sb, flags[1], 0, 4);
1256LE64_BITMASK(BCH_SB_COMPRESSION_TYPE, struct bch_sb, flags[1], 4, 8);
1257LE64_BITMASK(BCH_SB_INODE_32BIT, struct bch_sb, flags[1], 8, 9);
1258
1259LE64_BITMASK(BCH_SB_128_BIT_MACS, struct bch_sb, flags[1], 9, 10);
1260LE64_BITMASK(BCH_SB_ENCRYPTION_TYPE, struct bch_sb, flags[1], 10, 14);
1261
1262/*
1263 * Max size of an extent that may require bouncing to read or write
1264 * (checksummed, compressed): 64k
1265 */
1266LE64_BITMASK(BCH_SB_ENCODED_EXTENT_MAX_BITS,
1267 struct bch_sb, flags[1], 14, 20);
1268
1269LE64_BITMASK(BCH_SB_META_REPLICAS_REQ, struct bch_sb, flags[1], 20, 24);
1270LE64_BITMASK(BCH_SB_DATA_REPLICAS_REQ, struct bch_sb, flags[1], 24, 28);
1271
1272LE64_BITMASK(BCH_SB_PROMOTE_TARGET, struct bch_sb, flags[1], 28, 40);
1273LE64_BITMASK(BCH_SB_FOREGROUND_TARGET, struct bch_sb, flags[1], 40, 52);
1274LE64_BITMASK(BCH_SB_BACKGROUND_TARGET, struct bch_sb, flags[1], 52, 64);
1275
1276LE64_BITMASK(BCH_SB_BACKGROUND_COMPRESSION_TYPE,
1277 struct bch_sb, flags[2], 0, 4);
a50ed7c8 1278LE64_BITMASK(BCH_SB_GC_RESERVE_BYTES, struct bch_sb, flags[2], 4, 64);
1c6fdbd8 1279
cd575ddf
KO
1280LE64_BITMASK(BCH_SB_ERASURE_CODE, struct bch_sb, flags[3], 0, 16);
1281
1c3ff72c
KO
1282/*
1283 * Features:
1284 *
1285 * journal_seq_blacklist_v3: gates BCH_SB_FIELD_journal_seq_blacklist
1286 * reflink: gates KEY_TYPE_reflink
1287 * inline_data: gates KEY_TYPE_inline_data
1288 * new_siphash: gates BCH_STR_HASH_SIPHASH
bcd6f3e0 1289 * new_extent_overwrite: gates BTREE_NODE_NEW_EXTENT_OVERWRITE
1c3ff72c
KO
1290 */
1291#define BCH_SB_FEATURES() \
1292 x(lz4, 0) \
1293 x(gzip, 1) \
1294 x(zstd, 2) \
1295 x(atomic_nlink, 3) \
1296 x(ec, 4) \
1297 x(journal_seq_blacklist_v3, 5) \
1298 x(reflink, 6) \
1299 x(new_siphash, 7) \
bcd6f3e0 1300 x(inline_data, 8) \
ab05de4c
KO
1301 x(new_extent_overwrite, 9) \
1302 x(incompressible, 10)
1c3ff72c
KO
1303
1304enum bch_sb_feature {
1305#define x(f, n) BCH_FEATURE_##f,
1306 BCH_SB_FEATURES()
1307#undef x
c258f28e 1308 BCH_FEATURE_NR,
1c6fdbd8
KO
1309};
1310
1df42b57
KO
1311enum bch_sb_compat {
1312 BCH_COMPAT_FEAT_ALLOC_INFO = 0,
932aa837 1313 BCH_COMPAT_FEAT_ALLOC_METADATA = 1,
1df42b57
KO
1314};
1315
1c6fdbd8
KO
1316/* options: */
1317
1318#define BCH_REPLICAS_MAX 4U
1319
1320enum bch_error_actions {
1321 BCH_ON_ERROR_CONTINUE = 0,
1322 BCH_ON_ERROR_RO = 1,
1323 BCH_ON_ERROR_PANIC = 2,
1324 BCH_NR_ERROR_ACTIONS = 3,
1325};
1326
73501ab8 1327enum bch_str_hash_type {
1c6fdbd8
KO
1328 BCH_STR_HASH_CRC32C = 0,
1329 BCH_STR_HASH_CRC64 = 1,
73501ab8
KO
1330 BCH_STR_HASH_SIPHASH_OLD = 2,
1331 BCH_STR_HASH_SIPHASH = 3,
1332 BCH_STR_HASH_NR = 4,
1333};
1334
1335enum bch_str_hash_opts {
1336 BCH_STR_HASH_OPT_CRC32C = 0,
1337 BCH_STR_HASH_OPT_CRC64 = 1,
1338 BCH_STR_HASH_OPT_SIPHASH = 2,
1339 BCH_STR_HASH_OPT_NR = 3,
1c6fdbd8
KO
1340};
1341
1c3ff72c
KO
1342enum bch_csum_type {
1343 BCH_CSUM_NONE = 0,
1344 BCH_CSUM_CRC32C_NONZERO = 1,
1345 BCH_CSUM_CRC64_NONZERO = 2,
1346 BCH_CSUM_CHACHA20_POLY1305_80 = 3,
1347 BCH_CSUM_CHACHA20_POLY1305_128 = 4,
1348 BCH_CSUM_CRC32C = 5,
1349 BCH_CSUM_CRC64 = 6,
1350 BCH_CSUM_NR = 7,
1351};
1352
1353static const unsigned bch_crc_bytes[] = {
1354 [BCH_CSUM_NONE] = 0,
1355 [BCH_CSUM_CRC32C_NONZERO] = 4,
1356 [BCH_CSUM_CRC32C] = 4,
1357 [BCH_CSUM_CRC64_NONZERO] = 8,
1358 [BCH_CSUM_CRC64] = 8,
1359 [BCH_CSUM_CHACHA20_POLY1305_80] = 10,
1360 [BCH_CSUM_CHACHA20_POLY1305_128] = 16,
1361};
1362
1363static inline _Bool bch2_csum_type_is_encryption(enum bch_csum_type type)
1364{
1365 switch (type) {
1366 case BCH_CSUM_CHACHA20_POLY1305_80:
1367 case BCH_CSUM_CHACHA20_POLY1305_128:
1368 return true;
1369 default:
1370 return false;
1371 }
1372}
1373
1374enum bch_csum_opts {
1375 BCH_CSUM_OPT_NONE = 0,
1376 BCH_CSUM_OPT_CRC32C = 1,
1377 BCH_CSUM_OPT_CRC64 = 2,
1378 BCH_CSUM_OPT_NR = 3,
1379};
1380
1c6fdbd8 1381#define BCH_COMPRESSION_TYPES() \
ab05de4c
KO
1382 x(none, 0) \
1383 x(lz4_old, 1) \
1384 x(gzip, 2) \
1385 x(lz4, 3) \
1386 x(zstd, 4) \
1387 x(incompressible, 5)
1c6fdbd8 1388
1c3ff72c
KO
1389enum bch_compression_type {
1390#define x(t, n) BCH_COMPRESSION_TYPE_##t,
1c6fdbd8 1391 BCH_COMPRESSION_TYPES()
1c3ff72c
KO
1392#undef x
1393 BCH_COMPRESSION_TYPE_NR
1394};
1395
1396#define BCH_COMPRESSION_OPTS() \
1397 x(none, 0) \
1398 x(lz4, 1) \
1399 x(gzip, 2) \
1400 x(zstd, 3)
1401
1402enum bch_compression_opts {
1403#define x(t, n) BCH_COMPRESSION_OPT_##t,
1404 BCH_COMPRESSION_OPTS()
1c6fdbd8
KO
1405#undef x
1406 BCH_COMPRESSION_OPT_NR
1407};
1408
1409/*
1410 * Magic numbers
1411 *
1412 * The various other data structures have their own magic numbers, which are
1413 * xored with the first part of the cache set's UUID
1414 */
1415
1416#define BCACHE_MAGIC \
1417 UUID_INIT(0xc68573f6, 0x4e1a, 0x45ca, \
1418 0x82, 0x65, 0xf5, 0x7f, 0x48, 0xba, 0x6d, 0x81)
1419#define BCHFS_MAGIC \
1420 UUID_INIT(0xc68573f6, 0x66ce, 0x90a9, \
1421 0xd9, 0x6a, 0x60, 0xcf, 0x80, 0x3d, 0xf7, 0xef)
1422
1423#define BCACHEFS_STATFS_MAGIC 0xca451a4e
1424
1425#define JSET_MAGIC __cpu_to_le64(0x245235c1a3625032ULL)
1426#define BSET_MAGIC __cpu_to_le64(0x90135c78b99e07f5ULL)
1427
1428static inline __le64 __bch2_sb_magic(struct bch_sb *sb)
1429{
1430 __le64 ret;
1431 memcpy(&ret, &sb->uuid, sizeof(ret));
1432 return ret;
1433}
1434
1435static inline __u64 __jset_magic(struct bch_sb *sb)
1436{
1437 return __le64_to_cpu(__bch2_sb_magic(sb) ^ JSET_MAGIC);
1438}
1439
1440static inline __u64 __bset_magic(struct bch_sb *sb)
1441{
1442 return __le64_to_cpu(__bch2_sb_magic(sb) ^ BSET_MAGIC);
1443}
1444
1445/* Journal */
1446
1c6fdbd8
KO
1447#define JSET_KEYS_U64s (sizeof(struct jset_entry) / sizeof(__u64))
1448
1449#define BCH_JSET_ENTRY_TYPES() \
1450 x(btree_keys, 0) \
1451 x(btree_root, 1) \
1452 x(prio_ptrs, 2) \
1453 x(blacklist, 3) \
2c5af169 1454 x(blacklist_v2, 4) \
3577df5f
KO
1455 x(usage, 5) \
1456 x(data_usage, 6)
1c6fdbd8
KO
1457
1458enum {
1459#define x(f, nr) BCH_JSET_ENTRY_##f = nr,
1460 BCH_JSET_ENTRY_TYPES()
1461#undef x
1462 BCH_JSET_ENTRY_NR
1463};
1464
1465/*
1466 * Journal sequence numbers can be blacklisted: bsets record the max sequence
1467 * number of all the journal entries they contain updates for, so that on
1468 * recovery we can ignore those bsets that contain index updates newer that what
1469 * made it into the journal.
1470 *
1471 * This means that we can't reuse that journal_seq - we have to skip it, and
1472 * then record that we skipped it so that the next time we crash and recover we
1473 * don't think there was a missing journal entry.
1474 */
1475struct jset_entry_blacklist {
1476 struct jset_entry entry;
1477 __le64 seq;
1478};
1479
1480struct jset_entry_blacklist_v2 {
1481 struct jset_entry entry;
1482 __le64 start;
1483 __le64 end;
1484};
1485
2c5af169 1486enum {
3577df5f 1487 FS_USAGE_RESERVED = 0,
2c5af169
KO
1488 FS_USAGE_INODES = 1,
1489 FS_USAGE_KEY_VERSION = 2,
1490 FS_USAGE_NR = 3
1491};
1492
1493struct jset_entry_usage {
1494 struct jset_entry entry;
3577df5f
KO
1495 __le64 v;
1496} __attribute__((packed));
1497
1498struct jset_entry_data_usage {
1499 struct jset_entry entry;
1500 __le64 v;
2c5af169
KO
1501 struct bch_replicas_entry r;
1502} __attribute__((packed));
1503
1c6fdbd8
KO
1504/*
1505 * On disk format for a journal entry:
1506 * seq is monotonically increasing; every journal entry has its own unique
1507 * sequence number.
1508 *
1509 * last_seq is the oldest journal entry that still has keys the btree hasn't
1510 * flushed to disk yet.
1511 *
1512 * version is for on disk format changes.
1513 */
1514struct jset {
1515 struct bch_csum csum;
1516
1517 __le64 magic;
1518 __le64 seq;
1519 __le32 version;
1520 __le32 flags;
1521
1522 __le32 u64s; /* size of d[] in u64s */
1523
1524 __u8 encrypted_start[0];
1525
1526 __le16 read_clock;
1527 __le16 write_clock;
1528
1529 /* Sequence number of oldest dirty journal entry */
1530 __le64 last_seq;
1531
1532
1533 union {
1534 struct jset_entry start[0];
1535 __u64 _data[0];
1536 };
1537} __attribute__((packed, aligned(8)));
1538
1539LE32_BITMASK(JSET_CSUM_TYPE, struct jset, flags, 0, 4);
1540LE32_BITMASK(JSET_BIG_ENDIAN, struct jset, flags, 4, 5);
1541
8b335bae 1542#define BCH_JOURNAL_BUCKETS_MIN 8
1c6fdbd8
KO
1543
1544/* Btree: */
1545
9ec211b0 1546#define BCH_BTREE_IDS() \
26609b61
KO
1547 x(EXTENTS, 0, "extents") \
1548 x(INODES, 1, "inodes") \
1549 x(DIRENTS, 2, "dirents") \
1550 x(XATTRS, 3, "xattrs") \
1551 x(ALLOC, 4, "alloc") \
1552 x(QUOTAS, 5, "quotas") \
9ec211b0 1553 x(EC, 6, "stripes") \
76426098 1554 x(REFLINK, 7, "reflink")
1c6fdbd8
KO
1555
1556enum btree_id {
26609b61
KO
1557#define x(kwd, val, name) BTREE_ID_##kwd = val,
1558 BCH_BTREE_IDS()
1559#undef x
1c6fdbd8
KO
1560 BTREE_ID_NR
1561};
1562
1c6fdbd8
KO
1563#define BTREE_MAX_DEPTH 4U
1564
1565/* Btree nodes */
1566
1c6fdbd8
KO
1567/*
1568 * Btree nodes
1569 *
1570 * On disk a btree node is a list/log of these; within each set the keys are
1571 * sorted
1572 */
1573struct bset {
1574 __le64 seq;
1575
1576 /*
1577 * Highest journal entry this bset contains keys for.
1578 * If on recovery we don't see that journal entry, this bset is ignored:
1579 * this allows us to preserve the order of all index updates after a
1580 * crash, since the journal records a total order of all index updates
1581 * and anything that didn't make it to the journal doesn't get used.
1582 */
1583 __le64 journal_seq;
1584
1585 __le32 flags;
1586 __le16 version;
1587 __le16 u64s; /* count of d[] in u64s */
1588
1589 union {
1590 struct bkey_packed start[0];
1591 __u64 _data[0];
1592 };
1593} __attribute__((packed, aligned(8)));
1594
1595LE32_BITMASK(BSET_CSUM_TYPE, struct bset, flags, 0, 4);
1596
1597LE32_BITMASK(BSET_BIG_ENDIAN, struct bset, flags, 4, 5);
1598LE32_BITMASK(BSET_SEPARATE_WHITEOUTS,
1599 struct bset, flags, 5, 6);
1600
1601struct btree_node {
1602 struct bch_csum csum;
1603 __le64 magic;
1604
1605 /* this flags field is encrypted, unlike bset->flags: */
1606 __le64 flags;
1607
1608 /* Closed interval: */
1609 struct bpos min_key;
1610 struct bpos max_key;
1611 struct bch_extent_ptr ptr;
1612 struct bkey_format format;
1613
1614 union {
1615 struct bset keys;
1616 struct {
1617 __u8 pad[22];
1618 __le16 u64s;
1619 __u64 _data[0];
1620
1621 };
1622 };
1623} __attribute__((packed, aligned(8)));
1624
1625LE64_BITMASK(BTREE_NODE_ID, struct btree_node, flags, 0, 4);
1626LE64_BITMASK(BTREE_NODE_LEVEL, struct btree_node, flags, 4, 8);
bcd6f3e0
KO
1627LE64_BITMASK(BTREE_NODE_NEW_EXTENT_OVERWRITE,
1628 struct btree_node, flags, 8, 9);
1629/* 9-32 unused */
1c6fdbd8
KO
1630LE64_BITMASK(BTREE_NODE_SEQ, struct btree_node, flags, 32, 64);
1631
1632struct btree_node_entry {
1633 struct bch_csum csum;
1634
1635 union {
1636 struct bset keys;
1637 struct {
1638 __u8 pad[22];
1639 __le16 u64s;
1640 __u64 _data[0];
1641
1642 };
1643 };
1644} __attribute__((packed, aligned(8)));
1645
1646#endif /* _BCACHEFS_FORMAT_H */