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