bcachefs: More btree gc refactorings
[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>
77#include <linux/uuid.h>
78
79#ifdef __KERNEL__
80typedef uuid_t __uuid_t;
81#endif
82
83#define LE_BITMASK(_bits, name, type, field, offset, end) \
84static const unsigned name##_OFFSET = offset; \
85static const unsigned name##_BITS = (end - offset); \
86static const __u##_bits name##_MAX = (1ULL << (end - offset)) - 1; \
87 \
88static inline __u64 name(const type *k) \
89{ \
90 return (__le##_bits##_to_cpu(k->field) >> offset) & \
91 ~(~0ULL << (end - offset)); \
92} \
93 \
94static inline void SET_##name(type *k, __u64 v) \
95{ \
96 __u##_bits new = __le##_bits##_to_cpu(k->field); \
97 \
98 new &= ~(~(~0ULL << (end - offset)) << offset); \
99 new |= (v & ~(~0ULL << (end - offset))) << offset; \
100 k->field = __cpu_to_le##_bits(new); \
101}
102
103#define LE16_BITMASK(n, t, f, o, e) LE_BITMASK(16, n, t, f, o, e)
104#define LE32_BITMASK(n, t, f, o, e) LE_BITMASK(32, n, t, f, o, e)
105#define LE64_BITMASK(n, t, f, o, e) LE_BITMASK(64, n, t, f, o, e)
106
107struct bkey_format {
108 __u8 key_u64s;
109 __u8 nr_fields;
110 /* One unused slot for now: */
111 __u8 bits_per_field[6];
112 __le64 field_offset[6];
113};
114
115/* Btree keys - all units are in sectors */
116
117struct bpos {
118 /*
119 * Word order matches machine byte order - btree code treats a bpos as a
120 * single large integer, for search/comparison purposes
121 *
122 * Note that wherever a bpos is embedded in another on disk data
123 * structure, it has to be byte swabbed when reading in metadata that
124 * wasn't written in native endian order:
125 */
126#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
127 __u32 snapshot;
128 __u64 offset;
129 __u64 inode;
130#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
131 __u64 inode;
132 __u64 offset; /* Points to end of extent - sectors */
133 __u32 snapshot;
134#else
135#error edit for your odd byteorder.
136#endif
137} __attribute__((packed, aligned(4)));
138
139#define KEY_INODE_MAX ((__u64)~0ULL)
140#define KEY_OFFSET_MAX ((__u64)~0ULL)
141#define KEY_SNAPSHOT_MAX ((__u32)~0U)
142#define KEY_SIZE_MAX ((__u32)~0U)
143
144static inline struct bpos POS(__u64 inode, __u64 offset)
145{
146 struct bpos ret;
147
148 ret.inode = inode;
149 ret.offset = offset;
150 ret.snapshot = 0;
151
152 return ret;
153}
154
155#define POS_MIN POS(0, 0)
156#define POS_MAX POS(KEY_INODE_MAX, KEY_OFFSET_MAX)
157
158/* Empty placeholder struct, for container_of() */
159struct bch_val {
160 __u64 __nothing[0];
161};
162
163struct bversion {
164#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
165 __u64 lo;
166 __u32 hi;
167#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
168 __u32 hi;
169 __u64 lo;
170#endif
171} __attribute__((packed, aligned(4)));
172
173struct bkey {
174 /* Size of combined key and value, in u64s */
175 __u8 u64s;
176
177 /* Format of key (0 for format local to btree node) */
178#if defined(__LITTLE_ENDIAN_BITFIELD)
179 __u8 format:7,
180 needs_whiteout:1;
181#elif defined (__BIG_ENDIAN_BITFIELD)
182 __u8 needs_whiteout:1,
183 format:7;
184#else
185#error edit for your odd byteorder.
186#endif
187
188 /* Type of the value */
189 __u8 type;
190
191#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
192 __u8 pad[1];
193
194 struct bversion version;
195 __u32 size; /* extent size, in sectors */
196 struct bpos p;
197#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
198 struct bpos p;
199 __u32 size; /* extent size, in sectors */
200 struct bversion version;
201
202 __u8 pad[1];
203#endif
204} __attribute__((packed, aligned(8)));
205
206struct bkey_packed {
207 __u64 _data[0];
208
209 /* Size of combined key and value, in u64s */
210 __u8 u64s;
211
212 /* Format of key (0 for format local to btree node) */
213
214 /*
215 * XXX: next incompat on disk format change, switch format and
216 * needs_whiteout - bkey_packed() will be cheaper if format is the high
217 * bits of the bitfield
218 */
219#if defined(__LITTLE_ENDIAN_BITFIELD)
220 __u8 format:7,
221 needs_whiteout:1;
222#elif defined (__BIG_ENDIAN_BITFIELD)
223 __u8 needs_whiteout:1,
224 format:7;
225#endif
226
227 /* Type of the value */
228 __u8 type;
229 __u8 key_start[0];
230
231 /*
232 * We copy bkeys with struct assignment in various places, and while
233 * that shouldn't be done with packed bkeys we can't disallow it in C,
234 * and it's legal to cast a bkey to a bkey_packed - so padding it out
235 * to the same size as struct bkey should hopefully be safest.
236 */
237 __u8 pad[sizeof(struct bkey) - 3];
238} __attribute__((packed, aligned(8)));
239
240#define BKEY_U64s (sizeof(struct bkey) / sizeof(__u64))
241#define KEY_PACKED_BITS_START 24
242
243#define KEY_FORMAT_LOCAL_BTREE 0
244#define KEY_FORMAT_CURRENT 1
245
246enum bch_bkey_fields {
247 BKEY_FIELD_INODE,
248 BKEY_FIELD_OFFSET,
249 BKEY_FIELD_SNAPSHOT,
250 BKEY_FIELD_SIZE,
251 BKEY_FIELD_VERSION_HI,
252 BKEY_FIELD_VERSION_LO,
253 BKEY_NR_FIELDS,
254};
255
256#define bkey_format_field(name, field) \
257 [BKEY_FIELD_##name] = (sizeof(((struct bkey *) NULL)->field) * 8)
258
259#define BKEY_FORMAT_CURRENT \
260((struct bkey_format) { \
261 .key_u64s = BKEY_U64s, \
262 .nr_fields = BKEY_NR_FIELDS, \
263 .bits_per_field = { \
264 bkey_format_field(INODE, p.inode), \
265 bkey_format_field(OFFSET, p.offset), \
266 bkey_format_field(SNAPSHOT, p.snapshot), \
267 bkey_format_field(SIZE, size), \
268 bkey_format_field(VERSION_HI, version.hi), \
269 bkey_format_field(VERSION_LO, version.lo), \
270 }, \
271})
272
273/* bkey with inline value */
274struct bkey_i {
275 __u64 _data[0];
276
277 union {
278 struct {
279 /* Size of combined key and value, in u64s */
280 __u8 u64s;
281 };
282 struct {
283 struct bkey k;
284 struct bch_val v;
285 };
286 };
287};
288
289#define KEY(_inode, _offset, _size) \
290((struct bkey) { \
291 .u64s = BKEY_U64s, \
292 .format = KEY_FORMAT_CURRENT, \
293 .p = POS(_inode, _offset), \
294 .size = _size, \
295})
296
297static inline void bkey_init(struct bkey *k)
298{
299 *k = KEY(0, 0, 0);
300}
301
302#define bkey_bytes(_k) ((_k)->u64s * sizeof(__u64))
303
304#define __BKEY_PADDED(key, pad) \
305 struct { struct bkey_i key; __u64 key ## _pad[pad]; }
306
307#define BKEY_VAL_TYPE(name, nr) \
308struct bkey_i_##name { \
309 union { \
310 struct bkey k; \
311 struct bkey_i k_i; \
312 }; \
313 struct bch_##name v; \
314}
315
316/*
317 * - DELETED keys are used internally to mark keys that should be ignored but
318 * override keys in composition order. Their version number is ignored.
319 *
320 * - DISCARDED keys indicate that the data is all 0s because it has been
321 * discarded. DISCARDs may have a version; if the version is nonzero the key
322 * will be persistent, otherwise the key will be dropped whenever the btree
323 * node is rewritten (like DELETED keys).
324 *
325 * - ERROR: any read of the data returns a read error, as the data was lost due
326 * to a failing device. Like DISCARDED keys, they can be removed (overridden)
327 * by new writes or cluster-wide GC. Node repair can also overwrite them with
328 * the same or a more recent version number, but not with an older version
329 * number.
330*/
331#define KEY_TYPE_DELETED 0
332#define KEY_TYPE_DISCARD 1
333#define KEY_TYPE_ERROR 2
334#define KEY_TYPE_COOKIE 3
335#define KEY_TYPE_PERSISTENT_DISCARD 4
336#define KEY_TYPE_GENERIC_NR 128
337
338struct bch_cookie {
339 struct bch_val v;
340 __le64 cookie;
341};
342BKEY_VAL_TYPE(cookie, KEY_TYPE_COOKIE);
343
344/* Extents */
345
346/*
347 * In extent bkeys, the value is a list of pointers (bch_extent_ptr), optionally
348 * preceded by checksum/compression information (bch_extent_crc32 or
349 * bch_extent_crc64).
350 *
351 * One major determining factor in the format of extents is how we handle and
352 * represent extents that have been partially overwritten and thus trimmed:
353 *
354 * If an extent is not checksummed or compressed, when the extent is trimmed we
355 * don't have to remember the extent we originally allocated and wrote: we can
356 * merely adjust ptr->offset to point to the start of the data that is currently
357 * live. The size field in struct bkey records the current (live) size of the
358 * extent, and is also used to mean "size of region on disk that we point to" in
359 * this case.
360 *
361 * Thus an extent that is not checksummed or compressed will consist only of a
362 * list of bch_extent_ptrs, with none of the fields in
363 * bch_extent_crc32/bch_extent_crc64.
364 *
365 * When an extent is checksummed or compressed, it's not possible to read only
366 * the data that is currently live: we have to read the entire extent that was
367 * originally written, and then return only the part of the extent that is
368 * currently live.
369 *
370 * Thus, in addition to the current size of the extent in struct bkey, we need
371 * to store the size of the originally allocated space - this is the
372 * compressed_size and uncompressed_size fields in bch_extent_crc32/64. Also,
373 * when the extent is trimmed, instead of modifying the offset field of the
374 * pointer, we keep a second smaller offset field - "offset into the original
375 * extent of the currently live region".
376 *
377 * The other major determining factor is replication and data migration:
378 *
379 * Each pointer may have its own bch_extent_crc32/64. When doing a replicated
380 * write, we will initially write all the replicas in the same format, with the
381 * same checksum type and compression format - however, when copygc runs later (or
382 * tiering/cache promotion, anything that moves data), it is not in general
383 * going to rewrite all the pointers at once - one of the replicas may be in a
384 * bucket on one device that has very little fragmentation while another lives
385 * in a bucket that has become heavily fragmented, and thus is being rewritten
386 * sooner than the rest.
387 *
388 * Thus it will only move a subset of the pointers (or in the case of
389 * tiering/cache promotion perhaps add a single pointer without dropping any
390 * current pointers), and if the extent has been partially overwritten it must
391 * write only the currently live portion (or copygc would not be able to reduce
392 * fragmentation!) - which necessitates a different bch_extent_crc format for
393 * the new pointer.
394 *
395 * But in the interests of space efficiency, we don't want to store one
396 * bch_extent_crc for each pointer if we don't have to.
397 *
398 * Thus, a bch_extent consists of bch_extent_crc32s, bch_extent_crc64s, and
399 * bch_extent_ptrs appended arbitrarily one after the other. We determine the
400 * type of a given entry with a scheme similar to utf8 (except we're encoding a
401 * type, not a size), encoding the type in the position of the first set bit:
402 *
403 * bch_extent_crc32 - 0b1
404 * bch_extent_ptr - 0b10
405 * bch_extent_crc64 - 0b100
406 *
407 * We do it this way because bch_extent_crc32 is _very_ constrained on bits (and
408 * bch_extent_crc64 is the least constrained).
409 *
410 * Then, each bch_extent_crc32/64 applies to the pointers that follow after it,
411 * until the next bch_extent_crc32/64.
412 *
413 * If there are no bch_extent_crcs preceding a bch_extent_ptr, then that pointer
414 * is neither checksummed nor compressed.
415 */
416
417/* 128 bits, sufficient for cryptographic MACs: */
418struct bch_csum {
419 __le64 lo;
420 __le64 hi;
421} __attribute__((packed, aligned(8)));
422
423enum bch_csum_type {
424 BCH_CSUM_NONE = 0,
425 BCH_CSUM_CRC32C_NONZERO = 1,
426 BCH_CSUM_CRC64_NONZERO = 2,
427 BCH_CSUM_CHACHA20_POLY1305_80 = 3,
428 BCH_CSUM_CHACHA20_POLY1305_128 = 4,
429 BCH_CSUM_CRC32C = 5,
430 BCH_CSUM_CRC64 = 6,
431 BCH_CSUM_NR = 7,
432};
433
434static const unsigned bch_crc_bytes[] = {
435 [BCH_CSUM_NONE] = 0,
436 [BCH_CSUM_CRC32C_NONZERO] = 4,
437 [BCH_CSUM_CRC32C] = 4,
438 [BCH_CSUM_CRC64_NONZERO] = 8,
439 [BCH_CSUM_CRC64] = 8,
440 [BCH_CSUM_CHACHA20_POLY1305_80] = 10,
441 [BCH_CSUM_CHACHA20_POLY1305_128] = 16,
442};
443
444static inline _Bool bch2_csum_type_is_encryption(enum bch_csum_type type)
445{
446 switch (type) {
447 case BCH_CSUM_CHACHA20_POLY1305_80:
448 case BCH_CSUM_CHACHA20_POLY1305_128:
449 return true;
450 default:
451 return false;
452 }
453}
454
455enum bch_compression_type {
456 BCH_COMPRESSION_NONE = 0,
457 BCH_COMPRESSION_LZ4_OLD = 1,
458 BCH_COMPRESSION_GZIP = 2,
459 BCH_COMPRESSION_LZ4 = 3,
460 BCH_COMPRESSION_ZSTD = 4,
461 BCH_COMPRESSION_NR = 5,
462};
463
abce30b7
KO
464#define BCH_EXTENT_ENTRY_TYPES() \
465 x(ptr, 0) \
466 x(crc32, 1) \
467 x(crc64, 2) \
468 x(crc128, 3)
469#define BCH_EXTENT_ENTRY_MAX 4
470
1c6fdbd8 471enum bch_extent_entry_type {
abce30b7
KO
472#define x(f, n) BCH_EXTENT_ENTRY_##f = n,
473 BCH_EXTENT_ENTRY_TYPES()
474#undef x
1c6fdbd8
KO
475};
476
1c6fdbd8
KO
477/* Compressed/uncompressed size are stored biased by 1: */
478struct bch_extent_crc32 {
479#if defined(__LITTLE_ENDIAN_BITFIELD)
480 __u32 type:2,
481 _compressed_size:7,
482 _uncompressed_size:7,
483 offset:7,
484 _unused:1,
485 csum_type:4,
486 compression_type:4;
487 __u32 csum;
488#elif defined (__BIG_ENDIAN_BITFIELD)
489 __u32 csum;
490 __u32 compression_type:4,
491 csum_type:4,
492 _unused:1,
493 offset:7,
494 _uncompressed_size:7,
495 _compressed_size:7,
496 type:2;
497#endif
498} __attribute__((packed, aligned(8)));
499
500#define CRC32_SIZE_MAX (1U << 7)
501#define CRC32_NONCE_MAX 0
502
503struct bch_extent_crc64 {
504#if defined(__LITTLE_ENDIAN_BITFIELD)
505 __u64 type:3,
506 _compressed_size:9,
507 _uncompressed_size:9,
508 offset:9,
509 nonce:10,
510 csum_type:4,
511 compression_type:4,
512 csum_hi:16;
513#elif defined (__BIG_ENDIAN_BITFIELD)
514 __u64 csum_hi:16,
515 compression_type:4,
516 csum_type:4,
517 nonce:10,
518 offset:9,
519 _uncompressed_size:9,
520 _compressed_size:9,
521 type:3;
522#endif
523 __u64 csum_lo;
524} __attribute__((packed, aligned(8)));
525
526#define CRC64_SIZE_MAX (1U << 9)
527#define CRC64_NONCE_MAX ((1U << 10) - 1)
528
529struct bch_extent_crc128 {
530#if defined(__LITTLE_ENDIAN_BITFIELD)
531 __u64 type:4,
532 _compressed_size:13,
533 _uncompressed_size:13,
534 offset:13,
535 nonce:13,
536 csum_type:4,
537 compression_type:4;
538#elif defined (__BIG_ENDIAN_BITFIELD)
539 __u64 compression_type:4,
540 csum_type:4,
541 nonce:13,
542 offset:13,
543 _uncompressed_size:13,
544 _compressed_size:13,
545 type:4;
546#endif
547 struct bch_csum csum;
548} __attribute__((packed, aligned(8)));
549
550#define CRC128_SIZE_MAX (1U << 13)
551#define CRC128_NONCE_MAX ((1U << 13) - 1)
552
553/*
554 * @reservation - pointer hasn't been written to, just reserved
555 */
556struct bch_extent_ptr {
557#if defined(__LITTLE_ENDIAN_BITFIELD)
558 __u64 type:1,
559 cached:1,
560 erasure_coded:1,
561 reservation:1,
562 offset:44, /* 8 petabytes */
563 dev:8,
564 gen:8;
565#elif defined (__BIG_ENDIAN_BITFIELD)
566 __u64 gen:8,
567 dev:8,
568 offset:44,
569 reservation:1,
570 erasure_coded:1,
571 cached:1,
572 type:1;
573#endif
574} __attribute__((packed, aligned(8)));
575
576struct bch_extent_reservation {
577#if defined(__LITTLE_ENDIAN_BITFIELD)
578 __u64 type:5,
579 unused:23,
580 replicas:4,
581 generation:32;
582#elif defined (__BIG_ENDIAN_BITFIELD)
583 __u64 generation:32,
584 replicas:4,
585 unused:23,
586 type:5;
587#endif
588};
589
590union bch_extent_entry {
591#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ || __BITS_PER_LONG == 64
592 unsigned long type;
593#elif __BITS_PER_LONG == 32
594 struct {
595 unsigned long pad;
596 unsigned long type;
597 };
598#else
599#error edit for your odd byteorder.
600#endif
abce30b7
KO
601
602#define x(f, n) struct bch_extent_##f f;
603 BCH_EXTENT_ENTRY_TYPES()
604#undef x
1c6fdbd8
KO
605};
606
607enum {
608 BCH_EXTENT = 128,
609
610 /*
611 * This is kind of a hack, we're overloading the type for a boolean that
612 * really should be part of the value - BCH_EXTENT and BCH_EXTENT_CACHED
613 * have the same value type:
614 */
615 BCH_EXTENT_CACHED = 129,
616
617 /*
618 * Persistent reservation:
619 */
620 BCH_RESERVATION = 130,
621};
622
623struct bch_extent {
624 struct bch_val v;
625
626 __u64 _data[0];
627 union bch_extent_entry start[];
628} __attribute__((packed, aligned(8)));
629BKEY_VAL_TYPE(extent, BCH_EXTENT);
630
631struct bch_reservation {
632 struct bch_val v;
633
634 __le32 generation;
635 __u8 nr_replicas;
636 __u8 pad[3];
637} __attribute__((packed, aligned(8)));
638BKEY_VAL_TYPE(reservation, BCH_RESERVATION);
639
640/* Maximum size (in u64s) a single pointer could be: */
641#define BKEY_EXTENT_PTR_U64s_MAX\
642 ((sizeof(struct bch_extent_crc128) + \
643 sizeof(struct bch_extent_ptr)) / sizeof(u64))
644
645/* Maximum possible size of an entire extent value: */
646#define BKEY_EXTENT_VAL_U64s_MAX \
647 (BKEY_EXTENT_PTR_U64s_MAX * (BCH_REPLICAS_MAX + 1))
648
649#define BKEY_PADDED(key) __BKEY_PADDED(key, BKEY_EXTENT_VAL_U64s_MAX)
650
651/* * Maximum possible size of an entire extent, key + value: */
652#define BKEY_EXTENT_U64s_MAX (BKEY_U64s + BKEY_EXTENT_VAL_U64s_MAX)
653
654/* Btree pointers don't carry around checksums: */
655#define BKEY_BTREE_PTR_VAL_U64s_MAX \
656 ((sizeof(struct bch_extent_ptr)) / sizeof(u64) * BCH_REPLICAS_MAX)
657#define BKEY_BTREE_PTR_U64s_MAX \
658 (BKEY_U64s + BKEY_BTREE_PTR_VAL_U64s_MAX)
659
660/* Inodes */
661
662#define BLOCKDEV_INODE_MAX 4096
663
664#define BCACHEFS_ROOT_INO 4096
665
666enum bch_inode_types {
667 BCH_INODE_FS = 128,
668 BCH_INODE_BLOCKDEV = 129,
669 BCH_INODE_GENERATION = 130,
670};
671
672struct bch_inode {
673 struct bch_val v;
674
675 __le64 bi_hash_seed;
676 __le32 bi_flags;
677 __le16 bi_mode;
678 __u8 fields[0];
679} __attribute__((packed, aligned(8)));
680BKEY_VAL_TYPE(inode, BCH_INODE_FS);
681
682struct bch_inode_generation {
683 struct bch_val v;
684
685 __le32 bi_generation;
686 __le32 pad;
687} __attribute__((packed, aligned(8)));
688BKEY_VAL_TYPE(inode_generation, BCH_INODE_GENERATION);
689
690#define BCH_INODE_FIELDS() \
691 BCH_INODE_FIELD(bi_atime, 64) \
692 BCH_INODE_FIELD(bi_ctime, 64) \
693 BCH_INODE_FIELD(bi_mtime, 64) \
694 BCH_INODE_FIELD(bi_otime, 64) \
695 BCH_INODE_FIELD(bi_size, 64) \
696 BCH_INODE_FIELD(bi_sectors, 64) \
697 BCH_INODE_FIELD(bi_uid, 32) \
698 BCH_INODE_FIELD(bi_gid, 32) \
699 BCH_INODE_FIELD(bi_nlink, 32) \
700 BCH_INODE_FIELD(bi_generation, 32) \
701 BCH_INODE_FIELD(bi_dev, 32) \
702 BCH_INODE_FIELD(bi_data_checksum, 8) \
703 BCH_INODE_FIELD(bi_compression, 8) \
704 BCH_INODE_FIELD(bi_project, 32) \
705 BCH_INODE_FIELD(bi_background_compression, 8) \
706 BCH_INODE_FIELD(bi_data_replicas, 8) \
707 BCH_INODE_FIELD(bi_promote_target, 16) \
708 BCH_INODE_FIELD(bi_foreground_target, 16) \
709 BCH_INODE_FIELD(bi_background_target, 16)
710
711#define BCH_INODE_FIELDS_INHERIT() \
712 BCH_INODE_FIELD(bi_data_checksum) \
713 BCH_INODE_FIELD(bi_compression) \
714 BCH_INODE_FIELD(bi_project) \
715 BCH_INODE_FIELD(bi_background_compression) \
716 BCH_INODE_FIELD(bi_data_replicas) \
717 BCH_INODE_FIELD(bi_promote_target) \
718 BCH_INODE_FIELD(bi_foreground_target) \
719 BCH_INODE_FIELD(bi_background_target)
720
721enum {
722 /*
723 * User flags (get/settable with FS_IOC_*FLAGS, correspond to FS_*_FL
724 * flags)
725 */
726 __BCH_INODE_SYNC = 0,
727 __BCH_INODE_IMMUTABLE = 1,
728 __BCH_INODE_APPEND = 2,
729 __BCH_INODE_NODUMP = 3,
730 __BCH_INODE_NOATIME = 4,
731
732 __BCH_INODE_I_SIZE_DIRTY= 5,
733 __BCH_INODE_I_SECTORS_DIRTY= 6,
734 __BCH_INODE_UNLINKED = 7,
735
736 /* bits 20+ reserved for packed fields below: */
737};
738
739#define BCH_INODE_SYNC (1 << __BCH_INODE_SYNC)
740#define BCH_INODE_IMMUTABLE (1 << __BCH_INODE_IMMUTABLE)
741#define BCH_INODE_APPEND (1 << __BCH_INODE_APPEND)
742#define BCH_INODE_NODUMP (1 << __BCH_INODE_NODUMP)
743#define BCH_INODE_NOATIME (1 << __BCH_INODE_NOATIME)
744#define BCH_INODE_I_SIZE_DIRTY (1 << __BCH_INODE_I_SIZE_DIRTY)
745#define BCH_INODE_I_SECTORS_DIRTY (1 << __BCH_INODE_I_SECTORS_DIRTY)
746#define BCH_INODE_UNLINKED (1 << __BCH_INODE_UNLINKED)
747
748LE32_BITMASK(INODE_STR_HASH, struct bch_inode, bi_flags, 20, 24);
749LE32_BITMASK(INODE_NR_FIELDS, struct bch_inode, bi_flags, 24, 32);
750
751struct bch_inode_blockdev {
752 struct bch_val v;
753
754 __le64 i_size;
755 __le64 i_flags;
756
757 /* Seconds: */
758 __le64 i_ctime;
759 __le64 i_mtime;
760
761 __uuid_t i_uuid;
762 __u8 i_label[32];
763} __attribute__((packed, aligned(8)));
764BKEY_VAL_TYPE(inode_blockdev, BCH_INODE_BLOCKDEV);
765
766/* Thin provisioned volume, or cache for another block device? */
767LE64_BITMASK(CACHED_DEV, struct bch_inode_blockdev, i_flags, 0, 1)
768
769/* Dirents */
770
771/*
772 * Dirents (and xattrs) have to implement string lookups; since our b-tree
773 * doesn't support arbitrary length strings for the key, we instead index by a
774 * 64 bit hash (currently truncated sha1) of the string, stored in the offset
775 * field of the key - using linear probing to resolve hash collisions. This also
776 * provides us with the readdir cookie posix requires.
777 *
778 * Linear probing requires us to use whiteouts for deletions, in the event of a
779 * collision:
780 */
781
782enum {
783 BCH_DIRENT = 128,
784 BCH_DIRENT_WHITEOUT = 129,
785};
786
787struct bch_dirent {
788 struct bch_val v;
789
790 /* Target inode number: */
791 __le64 d_inum;
792
793 /*
794 * Copy of mode bits 12-15 from the target inode - so userspace can get
795 * the filetype without having to do a stat()
796 */
797 __u8 d_type;
798
799 __u8 d_name[];
800} __attribute__((packed, aligned(8)));
801BKEY_VAL_TYPE(dirent, BCH_DIRENT);
802
803#define BCH_NAME_MAX (U8_MAX * sizeof(u64) - \
804 sizeof(struct bkey) - \
805 offsetof(struct bch_dirent, d_name))
806
807
808/* Xattrs */
809
810enum {
811 BCH_XATTR = 128,
812 BCH_XATTR_WHITEOUT = 129,
813};
814
815#define BCH_XATTR_INDEX_USER 0
816#define BCH_XATTR_INDEX_POSIX_ACL_ACCESS 1
817#define BCH_XATTR_INDEX_POSIX_ACL_DEFAULT 2
818#define BCH_XATTR_INDEX_TRUSTED 3
819#define BCH_XATTR_INDEX_SECURITY 4
820
821struct bch_xattr {
822 struct bch_val v;
823 __u8 x_type;
824 __u8 x_name_len;
825 __le16 x_val_len;
826 __u8 x_name[];
827} __attribute__((packed, aligned(8)));
828BKEY_VAL_TYPE(xattr, BCH_XATTR);
829
830/* Bucket/allocation information: */
831
832enum {
833 BCH_ALLOC = 128,
834};
835
836enum {
837 BCH_ALLOC_FIELD_READ_TIME = 0,
838 BCH_ALLOC_FIELD_WRITE_TIME = 1,
839};
840
841struct bch_alloc {
842 struct bch_val v;
843 __u8 fields;
844 __u8 gen;
845 __u8 data[];
846} __attribute__((packed, aligned(8)));
847BKEY_VAL_TYPE(alloc, BCH_ALLOC);
848
849/* Quotas: */
850
851enum {
852 BCH_QUOTA = 128,
853};
854
855enum quota_types {
856 QTYP_USR = 0,
857 QTYP_GRP = 1,
858 QTYP_PRJ = 2,
859 QTYP_NR = 3,
860};
861
862enum quota_counters {
863 Q_SPC = 0,
864 Q_INO = 1,
865 Q_COUNTERS = 2,
866};
867
868struct bch_quota_counter {
869 __le64 hardlimit;
870 __le64 softlimit;
871};
872
873struct bch_quota {
874 struct bch_val v;
875 struct bch_quota_counter c[Q_COUNTERS];
876} __attribute__((packed, aligned(8)));
877BKEY_VAL_TYPE(quota, BCH_QUOTA);
878
879/* Optional/variable size superblock sections: */
880
881struct bch_sb_field {
882 __u64 _data[0];
883 __le32 u64s;
884 __le32 type;
885};
886
887#define BCH_SB_FIELDS() \
888 x(journal, 0) \
889 x(members, 1) \
890 x(crypt, 2) \
af9d3bc2 891 x(replicas_v0, 3) \
1c6fdbd8
KO
892 x(quota, 4) \
893 x(disk_groups, 5) \
af9d3bc2
KO
894 x(clean, 6) \
895 x(replicas, 7)
1c6fdbd8
KO
896
897enum bch_sb_field_type {
898#define x(f, nr) BCH_SB_FIELD_##f = nr,
899 BCH_SB_FIELDS()
900#undef x
901 BCH_SB_FIELD_NR
902};
903
904/* BCH_SB_FIELD_journal: */
905
906struct bch_sb_field_journal {
907 struct bch_sb_field field;
908 __le64 buckets[0];
909};
910
911/* BCH_SB_FIELD_members: */
912
8b335bae
KO
913#define BCH_MIN_NR_NBUCKETS (1 << 6)
914
1c6fdbd8
KO
915struct bch_member {
916 __uuid_t uuid;
917 __le64 nbuckets; /* device size */
918 __le16 first_bucket; /* index of first bucket used */
919 __le16 bucket_size; /* sectors */
920 __le32 pad;
921 __le64 last_mount; /* time_t */
922
923 __le64 flags[2];
924};
925
926LE64_BITMASK(BCH_MEMBER_STATE, struct bch_member, flags[0], 0, 4)
927/* 4-10 unused, was TIER, HAS_(META)DATA */
928LE64_BITMASK(BCH_MEMBER_REPLACEMENT, struct bch_member, flags[0], 10, 14)
929LE64_BITMASK(BCH_MEMBER_DISCARD, struct bch_member, flags[0], 14, 15)
930LE64_BITMASK(BCH_MEMBER_DATA_ALLOWED, struct bch_member, flags[0], 15, 20)
931LE64_BITMASK(BCH_MEMBER_GROUP, struct bch_member, flags[0], 20, 28)
932LE64_BITMASK(BCH_MEMBER_DURABILITY, struct bch_member, flags[0], 28, 30)
933
934#define BCH_TIER_MAX 4U
935
936#if 0
937LE64_BITMASK(BCH_MEMBER_NR_READ_ERRORS, struct bch_member, flags[1], 0, 20);
938LE64_BITMASK(BCH_MEMBER_NR_WRITE_ERRORS,struct bch_member, flags[1], 20, 40);
939#endif
940
941enum bch_member_state {
942 BCH_MEMBER_STATE_RW = 0,
943 BCH_MEMBER_STATE_RO = 1,
944 BCH_MEMBER_STATE_FAILED = 2,
945 BCH_MEMBER_STATE_SPARE = 3,
946 BCH_MEMBER_STATE_NR = 4,
947};
948
949enum cache_replacement {
950 CACHE_REPLACEMENT_LRU = 0,
951 CACHE_REPLACEMENT_FIFO = 1,
952 CACHE_REPLACEMENT_RANDOM = 2,
953 CACHE_REPLACEMENT_NR = 3,
954};
955
956struct bch_sb_field_members {
957 struct bch_sb_field field;
958 struct bch_member members[0];
959};
960
961/* BCH_SB_FIELD_crypt: */
962
963struct nonce {
964 __le32 d[4];
965};
966
967struct bch_key {
968 __le64 key[4];
969};
970
971#define BCH_KEY_MAGIC \
972 (((u64) 'b' << 0)|((u64) 'c' << 8)| \
973 ((u64) 'h' << 16)|((u64) '*' << 24)| \
974 ((u64) '*' << 32)|((u64) 'k' << 40)| \
975 ((u64) 'e' << 48)|((u64) 'y' << 56))
976
977struct bch_encrypted_key {
978 __le64 magic;
979 struct bch_key key;
980};
981
982/*
983 * If this field is present in the superblock, it stores an encryption key which
984 * is used encrypt all other data/metadata. The key will normally be encrypted
985 * with the key userspace provides, but if encryption has been turned off we'll
986 * just store the master key unencrypted in the superblock so we can access the
987 * previously encrypted data.
988 */
989struct bch_sb_field_crypt {
990 struct bch_sb_field field;
991
992 __le64 flags;
993 __le64 kdf_flags;
994 struct bch_encrypted_key key;
995};
996
997LE64_BITMASK(BCH_CRYPT_KDF_TYPE, struct bch_sb_field_crypt, flags, 0, 4);
998
999enum bch_kdf_types {
1000 BCH_KDF_SCRYPT = 0,
1001 BCH_KDF_NR = 1,
1002};
1003
1004/* stored as base 2 log of scrypt params: */
1005LE64_BITMASK(BCH_KDF_SCRYPT_N, struct bch_sb_field_crypt, kdf_flags, 0, 16);
1006LE64_BITMASK(BCH_KDF_SCRYPT_R, struct bch_sb_field_crypt, kdf_flags, 16, 32);
1007LE64_BITMASK(BCH_KDF_SCRYPT_P, struct bch_sb_field_crypt, kdf_flags, 32, 48);
1008
1009/* BCH_SB_FIELD_replicas: */
1010
1011enum bch_data_type {
1012 BCH_DATA_NONE = 0,
1013 BCH_DATA_SB = 1,
1014 BCH_DATA_JOURNAL = 2,
1015 BCH_DATA_BTREE = 3,
1016 BCH_DATA_USER = 4,
1017 BCH_DATA_CACHED = 5,
1018 BCH_DATA_NR = 6,
1019};
1020
af9d3bc2
KO
1021struct bch_replicas_entry_v0 {
1022 __u8 data_type;
1023 __u8 nr_devs;
1024 __u8 devs[];
1025} __attribute__((packed));
1026
1027struct bch_sb_field_replicas_v0 {
1028 struct bch_sb_field field;
1029 struct bch_replicas_entry_v0 entries[];
1030} __attribute__((packed, aligned(8)));
1031
1c6fdbd8 1032struct bch_replicas_entry {
7a920560
KO
1033 __u8 data_type;
1034 __u8 nr_devs;
af9d3bc2 1035 __u8 nr_required;
7a920560 1036 __u8 devs[];
af9d3bc2 1037} __attribute__((packed));
1c6fdbd8
KO
1038
1039struct bch_sb_field_replicas {
1040 struct bch_sb_field field;
1041 struct bch_replicas_entry entries[];
af9d3bc2 1042} __attribute__((packed, aligned(8)));
1c6fdbd8
KO
1043
1044/* BCH_SB_FIELD_quota: */
1045
1046struct bch_sb_quota_counter {
1047 __le32 timelimit;
1048 __le32 warnlimit;
1049};
1050
1051struct bch_sb_quota_type {
1052 __le64 flags;
1053 struct bch_sb_quota_counter c[Q_COUNTERS];
1054};
1055
1056struct bch_sb_field_quota {
1057 struct bch_sb_field field;
1058 struct bch_sb_quota_type q[QTYP_NR];
1059} __attribute__((packed, aligned(8)));
1060
1061/* BCH_SB_FIELD_disk_groups: */
1062
1063#define BCH_SB_LABEL_SIZE 32
1064
1065struct bch_disk_group {
1066 __u8 label[BCH_SB_LABEL_SIZE];
1067 __le64 flags[2];
1068};
1069
1070LE64_BITMASK(BCH_GROUP_DELETED, struct bch_disk_group, flags[0], 0, 1)
1071LE64_BITMASK(BCH_GROUP_DATA_ALLOWED, struct bch_disk_group, flags[0], 1, 6)
1072LE64_BITMASK(BCH_GROUP_PARENT, struct bch_disk_group, flags[0], 6, 24)
1073
1074struct bch_sb_field_disk_groups {
1075 struct bch_sb_field field;
1076 struct bch_disk_group entries[0];
1077};
1078
1079/*
1080 * On clean shutdown, store btree roots and current journal sequence number in
1081 * the superblock:
1082 */
1083struct jset_entry {
1084 __le16 u64s;
1085 __u8 btree_id;
1086 __u8 level;
1087 __u8 type; /* designates what this jset holds */
1088 __u8 pad[3];
1089
1090 union {
1091 struct bkey_i start[0];
1092 __u64 _data[0];
1093 };
1094};
1095
1096struct bch_sb_field_clean {
1097 struct bch_sb_field field;
1098
1099 __le32 flags;
1100 __le16 read_clock;
1101 __le16 write_clock;
1102 __le64 journal_seq;
1103
1104 union {
1105 struct jset_entry start[0];
1106 __u64 _data[0];
1107 };
1108};
1109
1110/* Superblock: */
1111
1112/*
1113 * Version 8: BCH_SB_ENCODED_EXTENT_MAX_BITS
1114 * BCH_MEMBER_DATA_ALLOWED
1115 * Version 9: incompatible extent nonce change
1116 */
1117
1118#define BCH_SB_VERSION_MIN 7
1119#define BCH_SB_VERSION_EXTENT_MAX 8
1120#define BCH_SB_VERSION_EXTENT_NONCE_V1 9
1121#define BCH_SB_VERSION_MAX 9
1122
1123#define BCH_SB_SECTOR 8
1124#define BCH_SB_MEMBERS_MAX 64 /* XXX kill */
1125
1126struct bch_sb_layout {
1127 __uuid_t magic; /* bcachefs superblock UUID */
1128 __u8 layout_type;
1129 __u8 sb_max_size_bits; /* base 2 of 512 byte sectors */
1130 __u8 nr_superblocks;
1131 __u8 pad[5];
1132 __le64 sb_offset[61];
1133} __attribute__((packed, aligned(8)));
1134
1135#define BCH_SB_LAYOUT_SECTOR 7
1136
1137/*
1138 * @offset - sector where this sb was written
1139 * @version - on disk format version
1140 * @magic - identifies as a bcachefs superblock (BCACHE_MAGIC)
1141 * @seq - incremented each time superblock is written
1142 * @uuid - used for generating various magic numbers and identifying
1143 * member devices, never changes
1144 * @user_uuid - user visible UUID, may be changed
1145 * @label - filesystem label
1146 * @seq - identifies most recent superblock, incremented each time
1147 * superblock is written
1148 * @features - enabled incompatible features
1149 */
1150struct bch_sb {
1151 struct bch_csum csum;
1152 __le16 version;
1153 __le16 version_min;
1154 __le16 pad[2];
1155 __uuid_t magic;
1156 __uuid_t uuid;
1157 __uuid_t user_uuid;
1158 __u8 label[BCH_SB_LABEL_SIZE];
1159 __le64 offset;
1160 __le64 seq;
1161
1162 __le16 block_size;
1163 __u8 dev_idx;
1164 __u8 nr_devices;
1165 __le32 u64s;
1166
1167 __le64 time_base_lo;
1168 __le32 time_base_hi;
1169 __le32 time_precision;
1170
1171 __le64 flags[8];
1172 __le64 features[2];
1173 __le64 compat[2];
1174
1175 struct bch_sb_layout layout;
1176
1177 union {
1178 struct bch_sb_field start[0];
1179 __le64 _data[0];
1180 };
1181} __attribute__((packed, aligned(8)));
1182
1183/*
1184 * Flags:
1185 * BCH_SB_INITALIZED - set on first mount
1186 * BCH_SB_CLEAN - did we shut down cleanly? Just a hint, doesn't affect
1187 * behaviour of mount/recovery path:
1188 * BCH_SB_INODE_32BIT - limit inode numbers to 32 bits
1189 * BCH_SB_128_BIT_MACS - 128 bit macs instead of 80
1190 * BCH_SB_ENCRYPTION_TYPE - if nonzero encryption is enabled; overrides
1191 * DATA/META_CSUM_TYPE. Also indicates encryption
1192 * algorithm in use, if/when we get more than one
1193 */
1194
1195LE16_BITMASK(BCH_SB_BLOCK_SIZE, struct bch_sb, block_size, 0, 16);
1196
1197LE64_BITMASK(BCH_SB_INITIALIZED, struct bch_sb, flags[0], 0, 1);
1198LE64_BITMASK(BCH_SB_CLEAN, struct bch_sb, flags[0], 1, 2);
1199LE64_BITMASK(BCH_SB_CSUM_TYPE, struct bch_sb, flags[0], 2, 8);
1200LE64_BITMASK(BCH_SB_ERROR_ACTION, struct bch_sb, flags[0], 8, 12);
1201
1202LE64_BITMASK(BCH_SB_BTREE_NODE_SIZE, struct bch_sb, flags[0], 12, 28);
1203
1204LE64_BITMASK(BCH_SB_GC_RESERVE, struct bch_sb, flags[0], 28, 33);
1205LE64_BITMASK(BCH_SB_ROOT_RESERVE, struct bch_sb, flags[0], 33, 40);
1206
1207LE64_BITMASK(BCH_SB_META_CSUM_TYPE, struct bch_sb, flags[0], 40, 44);
1208LE64_BITMASK(BCH_SB_DATA_CSUM_TYPE, struct bch_sb, flags[0], 44, 48);
1209
1210LE64_BITMASK(BCH_SB_META_REPLICAS_WANT, struct bch_sb, flags[0], 48, 52);
1211LE64_BITMASK(BCH_SB_DATA_REPLICAS_WANT, struct bch_sb, flags[0], 52, 56);
1212
1213LE64_BITMASK(BCH_SB_POSIX_ACL, struct bch_sb, flags[0], 56, 57);
1214LE64_BITMASK(BCH_SB_USRQUOTA, struct bch_sb, flags[0], 57, 58);
1215LE64_BITMASK(BCH_SB_GRPQUOTA, struct bch_sb, flags[0], 58, 59);
1216LE64_BITMASK(BCH_SB_PRJQUOTA, struct bch_sb, flags[0], 59, 60);
1217
1218/* 60-64 unused */
1219
1220LE64_BITMASK(BCH_SB_STR_HASH_TYPE, struct bch_sb, flags[1], 0, 4);
1221LE64_BITMASK(BCH_SB_COMPRESSION_TYPE, struct bch_sb, flags[1], 4, 8);
1222LE64_BITMASK(BCH_SB_INODE_32BIT, struct bch_sb, flags[1], 8, 9);
1223
1224LE64_BITMASK(BCH_SB_128_BIT_MACS, struct bch_sb, flags[1], 9, 10);
1225LE64_BITMASK(BCH_SB_ENCRYPTION_TYPE, struct bch_sb, flags[1], 10, 14);
1226
1227/*
1228 * Max size of an extent that may require bouncing to read or write
1229 * (checksummed, compressed): 64k
1230 */
1231LE64_BITMASK(BCH_SB_ENCODED_EXTENT_MAX_BITS,
1232 struct bch_sb, flags[1], 14, 20);
1233
1234LE64_BITMASK(BCH_SB_META_REPLICAS_REQ, struct bch_sb, flags[1], 20, 24);
1235LE64_BITMASK(BCH_SB_DATA_REPLICAS_REQ, struct bch_sb, flags[1], 24, 28);
1236
1237LE64_BITMASK(BCH_SB_PROMOTE_TARGET, struct bch_sb, flags[1], 28, 40);
1238LE64_BITMASK(BCH_SB_FOREGROUND_TARGET, struct bch_sb, flags[1], 40, 52);
1239LE64_BITMASK(BCH_SB_BACKGROUND_TARGET, struct bch_sb, flags[1], 52, 64);
1240
1241LE64_BITMASK(BCH_SB_BACKGROUND_COMPRESSION_TYPE,
1242 struct bch_sb, flags[2], 0, 4);
a50ed7c8 1243LE64_BITMASK(BCH_SB_GC_RESERVE_BYTES, struct bch_sb, flags[2], 4, 64);
1c6fdbd8
KO
1244
1245/* Features: */
1246enum bch_sb_features {
1247 BCH_FEATURE_LZ4 = 0,
1248 BCH_FEATURE_GZIP = 1,
1249 BCH_FEATURE_ZSTD = 2,
c258f28e
KO
1250 BCH_FEATURE_ATOMIC_NLINK = 3, /* should have gone under compat */
1251 BCH_FEATURE_NR,
1c6fdbd8
KO
1252};
1253
1254/* options: */
1255
1256#define BCH_REPLICAS_MAX 4U
1257
1258enum bch_error_actions {
1259 BCH_ON_ERROR_CONTINUE = 0,
1260 BCH_ON_ERROR_RO = 1,
1261 BCH_ON_ERROR_PANIC = 2,
1262 BCH_NR_ERROR_ACTIONS = 3,
1263};
1264
1265enum bch_csum_opts {
1266 BCH_CSUM_OPT_NONE = 0,
1267 BCH_CSUM_OPT_CRC32C = 1,
1268 BCH_CSUM_OPT_CRC64 = 2,
1269 BCH_CSUM_OPT_NR = 3,
1270};
1271
1272enum bch_str_hash_opts {
1273 BCH_STR_HASH_CRC32C = 0,
1274 BCH_STR_HASH_CRC64 = 1,
1275 BCH_STR_HASH_SIPHASH = 2,
1276 BCH_STR_HASH_NR = 3,
1277};
1278
1279#define BCH_COMPRESSION_TYPES() \
1280 x(NONE) \
1281 x(LZ4) \
1282 x(GZIP) \
1283 x(ZSTD)
1284
1285enum bch_compression_opts {
1286#define x(t) BCH_COMPRESSION_OPT_##t,
1287 BCH_COMPRESSION_TYPES()
1288#undef x
1289 BCH_COMPRESSION_OPT_NR
1290};
1291
1292/*
1293 * Magic numbers
1294 *
1295 * The various other data structures have their own magic numbers, which are
1296 * xored with the first part of the cache set's UUID
1297 */
1298
1299#define BCACHE_MAGIC \
1300 UUID_INIT(0xc68573f6, 0x4e1a, 0x45ca, \
1301 0x82, 0x65, 0xf5, 0x7f, 0x48, 0xba, 0x6d, 0x81)
1302#define BCHFS_MAGIC \
1303 UUID_INIT(0xc68573f6, 0x66ce, 0x90a9, \
1304 0xd9, 0x6a, 0x60, 0xcf, 0x80, 0x3d, 0xf7, 0xef)
1305
1306#define BCACHEFS_STATFS_MAGIC 0xca451a4e
1307
1308#define JSET_MAGIC __cpu_to_le64(0x245235c1a3625032ULL)
1309#define BSET_MAGIC __cpu_to_le64(0x90135c78b99e07f5ULL)
1310
1311static inline __le64 __bch2_sb_magic(struct bch_sb *sb)
1312{
1313 __le64 ret;
1314 memcpy(&ret, &sb->uuid, sizeof(ret));
1315 return ret;
1316}
1317
1318static inline __u64 __jset_magic(struct bch_sb *sb)
1319{
1320 return __le64_to_cpu(__bch2_sb_magic(sb) ^ JSET_MAGIC);
1321}
1322
1323static inline __u64 __bset_magic(struct bch_sb *sb)
1324{
1325 return __le64_to_cpu(__bch2_sb_magic(sb) ^ BSET_MAGIC);
1326}
1327
1328/* Journal */
1329
1330#define BCACHE_JSET_VERSION_UUIDv1 1
1331#define BCACHE_JSET_VERSION_UUID 1 /* Always latest UUID format */
1332#define BCACHE_JSET_VERSION_JKEYS 2
1333#define BCACHE_JSET_VERSION 2
1334
1335#define JSET_KEYS_U64s (sizeof(struct jset_entry) / sizeof(__u64))
1336
1337#define BCH_JSET_ENTRY_TYPES() \
1338 x(btree_keys, 0) \
1339 x(btree_root, 1) \
1340 x(prio_ptrs, 2) \
1341 x(blacklist, 3) \
1342 x(blacklist_v2, 4)
1343
1344enum {
1345#define x(f, nr) BCH_JSET_ENTRY_##f = nr,
1346 BCH_JSET_ENTRY_TYPES()
1347#undef x
1348 BCH_JSET_ENTRY_NR
1349};
1350
1351/*
1352 * Journal sequence numbers can be blacklisted: bsets record the max sequence
1353 * number of all the journal entries they contain updates for, so that on
1354 * recovery we can ignore those bsets that contain index updates newer that what
1355 * made it into the journal.
1356 *
1357 * This means that we can't reuse that journal_seq - we have to skip it, and
1358 * then record that we skipped it so that the next time we crash and recover we
1359 * don't think there was a missing journal entry.
1360 */
1361struct jset_entry_blacklist {
1362 struct jset_entry entry;
1363 __le64 seq;
1364};
1365
1366struct jset_entry_blacklist_v2 {
1367 struct jset_entry entry;
1368 __le64 start;
1369 __le64 end;
1370};
1371
1372/*
1373 * On disk format for a journal entry:
1374 * seq is monotonically increasing; every journal entry has its own unique
1375 * sequence number.
1376 *
1377 * last_seq is the oldest journal entry that still has keys the btree hasn't
1378 * flushed to disk yet.
1379 *
1380 * version is for on disk format changes.
1381 */
1382struct jset {
1383 struct bch_csum csum;
1384
1385 __le64 magic;
1386 __le64 seq;
1387 __le32 version;
1388 __le32 flags;
1389
1390 __le32 u64s; /* size of d[] in u64s */
1391
1392 __u8 encrypted_start[0];
1393
1394 __le16 read_clock;
1395 __le16 write_clock;
1396
1397 /* Sequence number of oldest dirty journal entry */
1398 __le64 last_seq;
1399
1400
1401 union {
1402 struct jset_entry start[0];
1403 __u64 _data[0];
1404 };
1405} __attribute__((packed, aligned(8)));
1406
1407LE32_BITMASK(JSET_CSUM_TYPE, struct jset, flags, 0, 4);
1408LE32_BITMASK(JSET_BIG_ENDIAN, struct jset, flags, 4, 5);
1409
8b335bae 1410#define BCH_JOURNAL_BUCKETS_MIN 8
1c6fdbd8
KO
1411
1412/* Btree: */
1413
1414#define DEFINE_BCH_BTREE_IDS() \
1415 DEF_BTREE_ID(EXTENTS, 0, "extents") \
1416 DEF_BTREE_ID(INODES, 1, "inodes") \
1417 DEF_BTREE_ID(DIRENTS, 2, "dirents") \
1418 DEF_BTREE_ID(XATTRS, 3, "xattrs") \
1419 DEF_BTREE_ID(ALLOC, 4, "alloc") \
1420 DEF_BTREE_ID(QUOTAS, 5, "quotas")
1421
1422#define DEF_BTREE_ID(kwd, val, name) BTREE_ID_##kwd = val,
1423
1424enum btree_id {
1425 DEFINE_BCH_BTREE_IDS()
1426 BTREE_ID_NR
1427};
1428
1429#undef DEF_BTREE_ID
1430
1431#define BTREE_MAX_DEPTH 4U
1432
1433/* Btree nodes */
1434
1435/* Version 1: Seed pointer into btree node checksum
1436 */
1437#define BCACHE_BSET_CSUM 1
1438#define BCACHE_BSET_KEY_v1 2
1439#define BCACHE_BSET_JOURNAL_SEQ 3
1440#define BCACHE_BSET_VERSION 3
1441
1442/*
1443 * Btree nodes
1444 *
1445 * On disk a btree node is a list/log of these; within each set the keys are
1446 * sorted
1447 */
1448struct bset {
1449 __le64 seq;
1450
1451 /*
1452 * Highest journal entry this bset contains keys for.
1453 * If on recovery we don't see that journal entry, this bset is ignored:
1454 * this allows us to preserve the order of all index updates after a
1455 * crash, since the journal records a total order of all index updates
1456 * and anything that didn't make it to the journal doesn't get used.
1457 */
1458 __le64 journal_seq;
1459
1460 __le32 flags;
1461 __le16 version;
1462 __le16 u64s; /* count of d[] in u64s */
1463
1464 union {
1465 struct bkey_packed start[0];
1466 __u64 _data[0];
1467 };
1468} __attribute__((packed, aligned(8)));
1469
1470LE32_BITMASK(BSET_CSUM_TYPE, struct bset, flags, 0, 4);
1471
1472LE32_BITMASK(BSET_BIG_ENDIAN, struct bset, flags, 4, 5);
1473LE32_BITMASK(BSET_SEPARATE_WHITEOUTS,
1474 struct bset, flags, 5, 6);
1475
1476struct btree_node {
1477 struct bch_csum csum;
1478 __le64 magic;
1479
1480 /* this flags field is encrypted, unlike bset->flags: */
1481 __le64 flags;
1482
1483 /* Closed interval: */
1484 struct bpos min_key;
1485 struct bpos max_key;
1486 struct bch_extent_ptr ptr;
1487 struct bkey_format format;
1488
1489 union {
1490 struct bset keys;
1491 struct {
1492 __u8 pad[22];
1493 __le16 u64s;
1494 __u64 _data[0];
1495
1496 };
1497 };
1498} __attribute__((packed, aligned(8)));
1499
1500LE64_BITMASK(BTREE_NODE_ID, struct btree_node, flags, 0, 4);
1501LE64_BITMASK(BTREE_NODE_LEVEL, struct btree_node, flags, 4, 8);
1502/* 8-32 unused */
1503LE64_BITMASK(BTREE_NODE_SEQ, struct btree_node, flags, 32, 64);
1504
1505struct btree_node_entry {
1506 struct bch_csum csum;
1507
1508 union {
1509 struct bset keys;
1510 struct {
1511 __u8 pad[22];
1512 __le16 u64s;
1513 __u64 _data[0];
1514
1515 };
1516 };
1517} __attribute__((packed, aligned(8)));
1518
1519#endif /* _BCACHEFS_FORMAT_H */