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