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