1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_FORMAT_H
3 #define _BCACHEFS_FORMAT_H
6 * bcachefs on disk data structures
10 * There are three main types of on disk data structures in bcachefs (this is
11 * reduced from 5 in bcache)
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.
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
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.
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.
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.
42 * The journal also contains entry types for the btree roots, and blacklisted
43 * journal sequence numbers (see journal_seq_blacklist.c).
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
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
58 * BTREE KEYS (struct bkey):
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.
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).
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.
75 #include <asm/types.h>
76 #include <asm/byteorder.h>
77 #include <linux/kernel.h>
78 #include <linux/uuid.h>
81 typedef uuid_t __uuid_t;
84 #define LE_BITMASK(_bits, name, type, field, offset, end) \
85 static const unsigned name##_OFFSET = offset; \
86 static const unsigned name##_BITS = (end - offset); \
87 static const __u##_bits name##_MAX = (1ULL << (end - offset)) - 1; \
89 static inline __u64 name(const type *k) \
91 return (__le##_bits##_to_cpu(k->field) >> offset) & \
92 ~(~0ULL << (end - offset)); \
95 static inline void SET_##name(type *k, __u64 v) \
97 __u##_bits new = __le##_bits##_to_cpu(k->field); \
99 new &= ~(~(~0ULL << (end - offset)) << offset); \
100 new |= (v & ~(~0ULL << (end - offset))) << offset; \
101 k->field = __cpu_to_le##_bits(new); \
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)
111 /* One unused slot for now: */
112 __u8 bits_per_field[6];
113 __le64 field_offset[6];
116 /* Btree keys - all units are in sectors */
120 * Word order matches machine byte order - btree code treats a bpos as a
121 * single large integer, for search/comparison purposes
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:
127 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
131 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
133 __u64 offset; /* Points to end of extent - sectors */
136 #error edit for your odd byteorder.
138 } __attribute__((packed, aligned(4)));
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)
145 static inline struct bpos POS(__u64 inode, __u64 offset)
156 #define POS_MIN POS(0, 0)
157 #define POS_MAX POS(KEY_INODE_MAX, KEY_OFFSET_MAX)
159 /* Empty placeholder struct, for container_of() */
165 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
168 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
172 } __attribute__((packed, aligned(4)));
175 /* Size of combined key and value, in u64s */
178 /* Format of key (0 for format local to btree node) */
179 #if defined(__LITTLE_ENDIAN_BITFIELD)
182 #elif defined (__BIG_ENDIAN_BITFIELD)
183 __u8 needs_whiteout:1,
186 #error edit for your odd byteorder.
189 /* Type of the value */
192 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
195 struct bversion version;
196 __u32 size; /* extent size, in sectors */
198 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
200 __u32 size; /* extent size, in sectors */
201 struct bversion version;
205 } __attribute__((packed, aligned(8)));
210 /* Size of combined key and value, in u64s */
213 /* Format of key (0 for format local to btree node) */
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
220 #if defined(__LITTLE_ENDIAN_BITFIELD)
223 #elif defined (__BIG_ENDIAN_BITFIELD)
224 __u8 needs_whiteout:1,
228 /* Type of the value */
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.
238 __u8 pad[sizeof(struct bkey) - 3];
239 } __attribute__((packed, aligned(8)));
241 #define BKEY_U64s (sizeof(struct bkey) / sizeof(__u64))
242 #define BKEY_U64s_MAX U8_MAX
243 #define BKEY_VAL_U64s_MAX (BKEY_U64s_MAX - BKEY_U64s)
245 #define KEY_PACKED_BITS_START 24
247 #define KEY_FORMAT_LOCAL_BTREE 0
248 #define KEY_FORMAT_CURRENT 1
250 enum bch_bkey_fields {
255 BKEY_FIELD_VERSION_HI,
256 BKEY_FIELD_VERSION_LO,
260 #define bkey_format_field(name, field) \
261 [BKEY_FIELD_##name] = (sizeof(((struct bkey *) NULL)->field) * 8)
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), \
277 /* bkey with inline value */
283 /* Size of combined key and value, in u64s */
293 #define KEY(_inode, _offset, _size) \
296 .format = KEY_FORMAT_CURRENT, \
297 .p = POS(_inode, _offset), \
301 static inline void bkey_init(struct bkey *k)
306 #define bkey_bytes(_k) ((_k)->u64s * sizeof(__u64))
308 #define __BKEY_PADDED(key, pad) \
309 struct { struct bkey_i key; __u64 key ## _pad[pad]; }
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.
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).
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
326 * - WHITEOUT: for hash table btrees
328 #define BCH_BKEY_TYPES() \
338 x(inode_generation, 9) \
348 #define x(name, nr) KEY_TYPE_##name = nr,
362 * In extent bkeys, the value is a list of pointers (bch_extent_ptr), optionally
363 * preceded by checksum/compression information (bch_extent_crc32 or
366 * One major determining factor in the format of extents is how we handle and
367 * represent extents that have been partially overwritten and thus trimmed:
369 * If an extent is not checksummed or compressed, when the extent is trimmed we
370 * don't have to remember the extent we originally allocated and wrote: we can
371 * merely adjust ptr->offset to point to the start of the data that is currently
372 * live. The size field in struct bkey records the current (live) size of the
373 * extent, and is also used to mean "size of region on disk that we point to" in
376 * Thus an extent that is not checksummed or compressed will consist only of a
377 * list of bch_extent_ptrs, with none of the fields in
378 * bch_extent_crc32/bch_extent_crc64.
380 * When an extent is checksummed or compressed, it's not possible to read only
381 * the data that is currently live: we have to read the entire extent that was
382 * originally written, and then return only the part of the extent that is
385 * Thus, in addition to the current size of the extent in struct bkey, we need
386 * to store the size of the originally allocated space - this is the
387 * compressed_size and uncompressed_size fields in bch_extent_crc32/64. Also,
388 * when the extent is trimmed, instead of modifying the offset field of the
389 * pointer, we keep a second smaller offset field - "offset into the original
390 * extent of the currently live region".
392 * The other major determining factor is replication and data migration:
394 * Each pointer may have its own bch_extent_crc32/64. When doing a replicated
395 * write, we will initially write all the replicas in the same format, with the
396 * same checksum type and compression format - however, when copygc runs later (or
397 * tiering/cache promotion, anything that moves data), it is not in general
398 * going to rewrite all the pointers at once - one of the replicas may be in a
399 * bucket on one device that has very little fragmentation while another lives
400 * in a bucket that has become heavily fragmented, and thus is being rewritten
401 * sooner than the rest.
403 * Thus it will only move a subset of the pointers (or in the case of
404 * tiering/cache promotion perhaps add a single pointer without dropping any
405 * current pointers), and if the extent has been partially overwritten it must
406 * write only the currently live portion (or copygc would not be able to reduce
407 * fragmentation!) - which necessitates a different bch_extent_crc format for
410 * But in the interests of space efficiency, we don't want to store one
411 * bch_extent_crc for each pointer if we don't have to.
413 * Thus, a bch_extent consists of bch_extent_crc32s, bch_extent_crc64s, and
414 * bch_extent_ptrs appended arbitrarily one after the other. We determine the
415 * type of a given entry with a scheme similar to utf8 (except we're encoding a
416 * type, not a size), encoding the type in the position of the first set bit:
418 * bch_extent_crc32 - 0b1
419 * bch_extent_ptr - 0b10
420 * bch_extent_crc64 - 0b100
422 * We do it this way because bch_extent_crc32 is _very_ constrained on bits (and
423 * bch_extent_crc64 is the least constrained).
425 * Then, each bch_extent_crc32/64 applies to the pointers that follow after it,
426 * until the next bch_extent_crc32/64.
428 * If there are no bch_extent_crcs preceding a bch_extent_ptr, then that pointer
429 * is neither checksummed nor compressed.
432 /* 128 bits, sufficient for cryptographic MACs: */
436 } __attribute__((packed, aligned(8)));
440 BCH_CSUM_CRC32C_NONZERO = 1,
441 BCH_CSUM_CRC64_NONZERO = 2,
442 BCH_CSUM_CHACHA20_POLY1305_80 = 3,
443 BCH_CSUM_CHACHA20_POLY1305_128 = 4,
449 static const unsigned bch_crc_bytes[] = {
451 [BCH_CSUM_CRC32C_NONZERO] = 4,
452 [BCH_CSUM_CRC32C] = 4,
453 [BCH_CSUM_CRC64_NONZERO] = 8,
454 [BCH_CSUM_CRC64] = 8,
455 [BCH_CSUM_CHACHA20_POLY1305_80] = 10,
456 [BCH_CSUM_CHACHA20_POLY1305_128] = 16,
459 static inline _Bool bch2_csum_type_is_encryption(enum bch_csum_type type)
462 case BCH_CSUM_CHACHA20_POLY1305_80:
463 case BCH_CSUM_CHACHA20_POLY1305_128:
470 enum bch_compression_type {
471 BCH_COMPRESSION_NONE = 0,
472 BCH_COMPRESSION_LZ4_OLD = 1,
473 BCH_COMPRESSION_GZIP = 2,
474 BCH_COMPRESSION_LZ4 = 3,
475 BCH_COMPRESSION_ZSTD = 4,
476 BCH_COMPRESSION_NR = 5,
479 #define BCH_EXTENT_ENTRY_TYPES() \
485 #define BCH_EXTENT_ENTRY_MAX 5
487 enum bch_extent_entry_type {
488 #define x(f, n) BCH_EXTENT_ENTRY_##f = n,
489 BCH_EXTENT_ENTRY_TYPES()
493 /* Compressed/uncompressed size are stored biased by 1: */
494 struct bch_extent_crc32 {
495 #if defined(__LITTLE_ENDIAN_BITFIELD)
498 _uncompressed_size:7,
504 #elif defined (__BIG_ENDIAN_BITFIELD)
506 __u32 compression_type:4,
510 _uncompressed_size:7,
514 } __attribute__((packed, aligned(8)));
516 #define CRC32_SIZE_MAX (1U << 7)
517 #define CRC32_NONCE_MAX 0
519 struct bch_extent_crc64 {
520 #if defined(__LITTLE_ENDIAN_BITFIELD)
523 _uncompressed_size:9,
529 #elif defined (__BIG_ENDIAN_BITFIELD)
535 _uncompressed_size:9,
540 } __attribute__((packed, aligned(8)));
542 #define CRC64_SIZE_MAX (1U << 9)
543 #define CRC64_NONCE_MAX ((1U << 10) - 1)
545 struct bch_extent_crc128 {
546 #if defined(__LITTLE_ENDIAN_BITFIELD)
549 _uncompressed_size:13,
554 #elif defined (__BIG_ENDIAN_BITFIELD)
555 __u64 compression_type:4,
559 _uncompressed_size:13,
563 struct bch_csum csum;
564 } __attribute__((packed, aligned(8)));
566 #define CRC128_SIZE_MAX (1U << 13)
567 #define CRC128_NONCE_MAX ((1U << 13) - 1)
570 * @reservation - pointer hasn't been written to, just reserved
572 struct bch_extent_ptr {
573 #if defined(__LITTLE_ENDIAN_BITFIELD)
578 offset:44, /* 8 petabytes */
581 #elif defined (__BIG_ENDIAN_BITFIELD)
590 } __attribute__((packed, aligned(8)));
592 struct bch_extent_stripe_ptr {
593 #if defined(__LITTLE_ENDIAN_BITFIELD)
597 #elif defined (__BIG_ENDIAN_BITFIELD)
604 struct bch_extent_reservation {
605 #if defined(__LITTLE_ENDIAN_BITFIELD)
610 #elif defined (__BIG_ENDIAN_BITFIELD)
618 union bch_extent_entry {
619 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ || __BITS_PER_LONG == 64
621 #elif __BITS_PER_LONG == 32
627 #error edit for your odd byteorder.
630 #define x(f, n) struct bch_extent_##f f;
631 BCH_EXTENT_ENTRY_TYPES()
635 struct bch_btree_ptr {
639 struct bch_extent_ptr start[];
640 } __attribute__((packed, aligned(8)));
646 union bch_extent_entry start[];
647 } __attribute__((packed, aligned(8)));
649 struct bch_reservation {
655 } __attribute__((packed, aligned(8)));
657 /* Maximum size (in u64s) a single pointer could be: */
658 #define BKEY_EXTENT_PTR_U64s_MAX\
659 ((sizeof(struct bch_extent_crc128) + \
660 sizeof(struct bch_extent_ptr)) / sizeof(u64))
662 /* Maximum possible size of an entire extent value: */
663 #define BKEY_EXTENT_VAL_U64s_MAX \
664 (1 + BKEY_EXTENT_PTR_U64s_MAX * (BCH_REPLICAS_MAX + 1))
666 #define BKEY_PADDED(key) __BKEY_PADDED(key, BKEY_EXTENT_VAL_U64s_MAX)
668 /* * Maximum possible size of an entire extent, key + value: */
669 #define BKEY_EXTENT_U64s_MAX (BKEY_U64s + BKEY_EXTENT_VAL_U64s_MAX)
671 /* Btree pointers don't carry around checksums: */
672 #define BKEY_BTREE_PTR_VAL_U64s_MAX \
673 ((sizeof(struct bch_extent_ptr)) / sizeof(u64) * BCH_REPLICAS_MAX)
674 #define BKEY_BTREE_PTR_U64s_MAX \
675 (BKEY_U64s + BKEY_BTREE_PTR_VAL_U64s_MAX)
679 #define BLOCKDEV_INODE_MAX 4096
681 #define BCACHEFS_ROOT_INO 4096
690 } __attribute__((packed, aligned(8)));
692 struct bch_inode_generation {
695 __le32 bi_generation;
697 } __attribute__((packed, aligned(8)));
699 #define BCH_INODE_FIELDS() \
709 x(bi_generation, 32) \
711 x(bi_data_checksum, 8) \
712 x(bi_compression, 8) \
714 x(bi_background_compression, 8) \
715 x(bi_data_replicas, 8) \
716 x(bi_promote_target, 16) \
717 x(bi_foreground_target, 16) \
718 x(bi_background_target, 16) \
719 x(bi_erasure_code, 16) \
722 /* subset of BCH_INODE_FIELDS */
723 #define BCH_INODE_OPTS() \
724 x(data_checksum, 8) \
727 x(background_compression, 8) \
728 x(data_replicas, 8) \
729 x(promote_target, 16) \
730 x(foreground_target, 16) \
731 x(background_target, 16) \
735 #define x(name, ...) \
744 * User flags (get/settable with FS_IOC_*FLAGS, correspond to FS_*_FL
747 __BCH_INODE_SYNC = 0,
748 __BCH_INODE_IMMUTABLE = 1,
749 __BCH_INODE_APPEND = 2,
750 __BCH_INODE_NODUMP = 3,
751 __BCH_INODE_NOATIME = 4,
753 __BCH_INODE_I_SIZE_DIRTY= 5,
754 __BCH_INODE_I_SECTORS_DIRTY= 6,
755 __BCH_INODE_UNLINKED = 7,
757 /* bits 20+ reserved for packed fields below: */
760 #define BCH_INODE_SYNC (1 << __BCH_INODE_SYNC)
761 #define BCH_INODE_IMMUTABLE (1 << __BCH_INODE_IMMUTABLE)
762 #define BCH_INODE_APPEND (1 << __BCH_INODE_APPEND)
763 #define BCH_INODE_NODUMP (1 << __BCH_INODE_NODUMP)
764 #define BCH_INODE_NOATIME (1 << __BCH_INODE_NOATIME)
765 #define BCH_INODE_I_SIZE_DIRTY (1 << __BCH_INODE_I_SIZE_DIRTY)
766 #define BCH_INODE_I_SECTORS_DIRTY (1 << __BCH_INODE_I_SECTORS_DIRTY)
767 #define BCH_INODE_UNLINKED (1 << __BCH_INODE_UNLINKED)
769 LE32_BITMASK(INODE_STR_HASH, struct bch_inode, bi_flags, 20, 24);
770 LE32_BITMASK(INODE_NR_FIELDS, struct bch_inode, bi_flags, 24, 32);
775 * Dirents (and xattrs) have to implement string lookups; since our b-tree
776 * doesn't support arbitrary length strings for the key, we instead index by a
777 * 64 bit hash (currently truncated sha1) of the string, stored in the offset
778 * field of the key - using linear probing to resolve hash collisions. This also
779 * provides us with the readdir cookie posix requires.
781 * Linear probing requires us to use whiteouts for deletions, in the event of a
788 /* Target inode number: */
792 * Copy of mode bits 12-15 from the target inode - so userspace can get
793 * the filetype without having to do a stat()
798 } __attribute__((packed, aligned(8)));
800 #define BCH_NAME_MAX (U8_MAX * sizeof(u64) - \
801 sizeof(struct bkey) - \
802 offsetof(struct bch_dirent, d_name))
807 #define KEY_TYPE_XATTR_INDEX_USER 0
808 #define KEY_TYPE_XATTR_INDEX_POSIX_ACL_ACCESS 1
809 #define KEY_TYPE_XATTR_INDEX_POSIX_ACL_DEFAULT 2
810 #define KEY_TYPE_XATTR_INDEX_TRUSTED 3
811 #define KEY_TYPE_XATTR_INDEX_SECURITY 4
819 } __attribute__((packed, aligned(8)));
821 /* Bucket/allocation information: */
828 } __attribute__((packed, aligned(8)));
830 #define BCH_ALLOC_FIELDS() \
834 x(dirty_sectors, 16) \
835 x(cached_sectors, 16) \
839 #define x(name, bytes) BCH_ALLOC_FIELD_##name,
845 static const unsigned BCH_ALLOC_FIELD_BYTES[] = {
846 #define x(name, bits) [BCH_ALLOC_FIELD_##name] = bits / 8,
851 #define x(name, bits) + (bits / 8)
852 static const unsigned BKEY_ALLOC_VAL_U64s_MAX =
853 DIV_ROUND_UP(offsetof(struct bch_alloc, data)
854 BCH_ALLOC_FIELDS(), sizeof(u64));
857 #define BKEY_ALLOC_U64s_MAX (BKEY_U64s + BKEY_ALLOC_VAL_U64s_MAX)
868 enum quota_counters {
874 struct bch_quota_counter {
881 struct bch_quota_counter c[Q_COUNTERS];
882 } __attribute__((packed, aligned(8)));
893 __u8 csum_granularity_bits;
897 struct bch_extent_ptr ptrs[0];
898 } __attribute__((packed, aligned(8)));
902 struct bch_reflink_p {
906 __le32 reservation_generation;
911 struct bch_reflink_v {
914 union bch_extent_entry start[0];
918 /* Optional/variable size superblock sections: */
920 struct bch_sb_field {
926 #define BCH_SB_FIELDS() \
935 x(journal_seq_blacklist, 8)
937 enum bch_sb_field_type {
938 #define x(f, nr) BCH_SB_FIELD_##f = nr,
944 /* BCH_SB_FIELD_journal: */
946 struct bch_sb_field_journal {
947 struct bch_sb_field field;
951 /* BCH_SB_FIELD_members: */
953 #define BCH_MIN_NR_NBUCKETS (1 << 6)
957 __le64 nbuckets; /* device size */
958 __le16 first_bucket; /* index of first bucket used */
959 __le16 bucket_size; /* sectors */
961 __le64 last_mount; /* time_t */
966 LE64_BITMASK(BCH_MEMBER_STATE, struct bch_member, flags[0], 0, 4)
967 /* 4-10 unused, was TIER, HAS_(META)DATA */
968 LE64_BITMASK(BCH_MEMBER_REPLACEMENT, struct bch_member, flags[0], 10, 14)
969 LE64_BITMASK(BCH_MEMBER_DISCARD, struct bch_member, flags[0], 14, 15)
970 LE64_BITMASK(BCH_MEMBER_DATA_ALLOWED, struct bch_member, flags[0], 15, 20)
971 LE64_BITMASK(BCH_MEMBER_GROUP, struct bch_member, flags[0], 20, 28)
972 LE64_BITMASK(BCH_MEMBER_DURABILITY, struct bch_member, flags[0], 28, 30)
974 #define BCH_TIER_MAX 4U
977 LE64_BITMASK(BCH_MEMBER_NR_READ_ERRORS, struct bch_member, flags[1], 0, 20);
978 LE64_BITMASK(BCH_MEMBER_NR_WRITE_ERRORS,struct bch_member, flags[1], 20, 40);
981 enum bch_member_state {
982 BCH_MEMBER_STATE_RW = 0,
983 BCH_MEMBER_STATE_RO = 1,
984 BCH_MEMBER_STATE_FAILED = 2,
985 BCH_MEMBER_STATE_SPARE = 3,
986 BCH_MEMBER_STATE_NR = 4,
989 enum cache_replacement {
990 CACHE_REPLACEMENT_LRU = 0,
991 CACHE_REPLACEMENT_FIFO = 1,
992 CACHE_REPLACEMENT_RANDOM = 2,
993 CACHE_REPLACEMENT_NR = 3,
996 struct bch_sb_field_members {
997 struct bch_sb_field field;
998 struct bch_member members[0];
1001 /* BCH_SB_FIELD_crypt: */
1011 #define BCH_KEY_MAGIC \
1012 (((u64) 'b' << 0)|((u64) 'c' << 8)| \
1013 ((u64) 'h' << 16)|((u64) '*' << 24)| \
1014 ((u64) '*' << 32)|((u64) 'k' << 40)| \
1015 ((u64) 'e' << 48)|((u64) 'y' << 56))
1017 struct bch_encrypted_key {
1023 * If this field is present in the superblock, it stores an encryption key which
1024 * is used encrypt all other data/metadata. The key will normally be encrypted
1025 * with the key userspace provides, but if encryption has been turned off we'll
1026 * just store the master key unencrypted in the superblock so we can access the
1027 * previously encrypted data.
1029 struct bch_sb_field_crypt {
1030 struct bch_sb_field field;
1034 struct bch_encrypted_key key;
1037 LE64_BITMASK(BCH_CRYPT_KDF_TYPE, struct bch_sb_field_crypt, flags, 0, 4);
1039 enum bch_kdf_types {
1044 /* stored as base 2 log of scrypt params: */
1045 LE64_BITMASK(BCH_KDF_SCRYPT_N, struct bch_sb_field_crypt, kdf_flags, 0, 16);
1046 LE64_BITMASK(BCH_KDF_SCRYPT_R, struct bch_sb_field_crypt, kdf_flags, 16, 32);
1047 LE64_BITMASK(BCH_KDF_SCRYPT_P, struct bch_sb_field_crypt, kdf_flags, 32, 48);
1049 /* BCH_SB_FIELD_replicas: */
1051 enum bch_data_type {
1054 BCH_DATA_JOURNAL = 2,
1057 BCH_DATA_CACHED = 5,
1061 struct bch_replicas_entry_v0 {
1065 } __attribute__((packed));
1067 struct bch_sb_field_replicas_v0 {
1068 struct bch_sb_field field;
1069 struct bch_replicas_entry_v0 entries[];
1070 } __attribute__((packed, aligned(8)));
1072 struct bch_replicas_entry {
1077 } __attribute__((packed));
1079 struct bch_sb_field_replicas {
1080 struct bch_sb_field field;
1081 struct bch_replicas_entry entries[];
1082 } __attribute__((packed, aligned(8)));
1084 /* BCH_SB_FIELD_quota: */
1086 struct bch_sb_quota_counter {
1091 struct bch_sb_quota_type {
1093 struct bch_sb_quota_counter c[Q_COUNTERS];
1096 struct bch_sb_field_quota {
1097 struct bch_sb_field field;
1098 struct bch_sb_quota_type q[QTYP_NR];
1099 } __attribute__((packed, aligned(8)));
1101 /* BCH_SB_FIELD_disk_groups: */
1103 #define BCH_SB_LABEL_SIZE 32
1105 struct bch_disk_group {
1106 __u8 label[BCH_SB_LABEL_SIZE];
1108 } __attribute__((packed, aligned(8)));
1110 LE64_BITMASK(BCH_GROUP_DELETED, struct bch_disk_group, flags[0], 0, 1)
1111 LE64_BITMASK(BCH_GROUP_DATA_ALLOWED, struct bch_disk_group, flags[0], 1, 6)
1112 LE64_BITMASK(BCH_GROUP_PARENT, struct bch_disk_group, flags[0], 6, 24)
1114 struct bch_sb_field_disk_groups {
1115 struct bch_sb_field field;
1116 struct bch_disk_group entries[0];
1117 } __attribute__((packed, aligned(8)));
1120 * On clean shutdown, store btree roots and current journal sequence number in
1127 __u8 type; /* designates what this jset holds */
1131 struct bkey_i start[0];
1136 struct bch_sb_field_clean {
1137 struct bch_sb_field field;
1145 struct jset_entry start[0];
1150 struct journal_seq_blacklist_entry {
1155 struct bch_sb_field_journal_seq_blacklist {
1156 struct bch_sb_field field;
1159 struct journal_seq_blacklist_entry start[0];
1167 * New versioning scheme:
1168 * One common version number for all on disk data structures - superblock, btree
1169 * nodes, journal entries
1171 #define BCH_JSET_VERSION_OLD 2
1172 #define BCH_BSET_VERSION_OLD 3
1174 enum bcachefs_metadata_version {
1175 bcachefs_metadata_version_min = 9,
1176 bcachefs_metadata_version_new_versioning = 10,
1177 bcachefs_metadata_version_bkey_renumber = 10,
1178 bcachefs_metadata_version_max = 11,
1181 #define bcachefs_metadata_version_current (bcachefs_metadata_version_max - 1)
1183 #define BCH_SB_SECTOR 8
1184 #define BCH_SB_MEMBERS_MAX 64 /* XXX kill */
1186 struct bch_sb_layout {
1187 __uuid_t magic; /* bcachefs superblock UUID */
1189 __u8 sb_max_size_bits; /* base 2 of 512 byte sectors */
1190 __u8 nr_superblocks;
1192 __le64 sb_offset[61];
1193 } __attribute__((packed, aligned(8)));
1195 #define BCH_SB_LAYOUT_SECTOR 7
1198 * @offset - sector where this sb was written
1199 * @version - on disk format version
1200 * @version_min - Oldest metadata version this filesystem contains; so we can
1201 * safely drop compatibility code and refuse to mount filesystems
1203 * @magic - identifies as a bcachefs superblock (BCACHE_MAGIC)
1204 * @seq - incremented each time superblock is written
1205 * @uuid - used for generating various magic numbers and identifying
1206 * member devices, never changes
1207 * @user_uuid - user visible UUID, may be changed
1208 * @label - filesystem label
1209 * @seq - identifies most recent superblock, incremented each time
1210 * superblock is written
1211 * @features - enabled incompatible features
1214 struct bch_csum csum;
1221 __u8 label[BCH_SB_LABEL_SIZE];
1230 __le64 time_base_lo;
1231 __le32 time_base_hi;
1232 __le32 time_precision;
1238 struct bch_sb_layout layout;
1241 struct bch_sb_field start[0];
1244 } __attribute__((packed, aligned(8)));
1248 * BCH_SB_INITALIZED - set on first mount
1249 * BCH_SB_CLEAN - did we shut down cleanly? Just a hint, doesn't affect
1250 * behaviour of mount/recovery path:
1251 * BCH_SB_INODE_32BIT - limit inode numbers to 32 bits
1252 * BCH_SB_128_BIT_MACS - 128 bit macs instead of 80
1253 * BCH_SB_ENCRYPTION_TYPE - if nonzero encryption is enabled; overrides
1254 * DATA/META_CSUM_TYPE. Also indicates encryption
1255 * algorithm in use, if/when we get more than one
1258 LE16_BITMASK(BCH_SB_BLOCK_SIZE, struct bch_sb, block_size, 0, 16);
1260 LE64_BITMASK(BCH_SB_INITIALIZED, struct bch_sb, flags[0], 0, 1);
1261 LE64_BITMASK(BCH_SB_CLEAN, struct bch_sb, flags[0], 1, 2);
1262 LE64_BITMASK(BCH_SB_CSUM_TYPE, struct bch_sb, flags[0], 2, 8);
1263 LE64_BITMASK(BCH_SB_ERROR_ACTION, struct bch_sb, flags[0], 8, 12);
1265 LE64_BITMASK(BCH_SB_BTREE_NODE_SIZE, struct bch_sb, flags[0], 12, 28);
1267 LE64_BITMASK(BCH_SB_GC_RESERVE, struct bch_sb, flags[0], 28, 33);
1268 LE64_BITMASK(BCH_SB_ROOT_RESERVE, struct bch_sb, flags[0], 33, 40);
1270 LE64_BITMASK(BCH_SB_META_CSUM_TYPE, struct bch_sb, flags[0], 40, 44);
1271 LE64_BITMASK(BCH_SB_DATA_CSUM_TYPE, struct bch_sb, flags[0], 44, 48);
1273 LE64_BITMASK(BCH_SB_META_REPLICAS_WANT, struct bch_sb, flags[0], 48, 52);
1274 LE64_BITMASK(BCH_SB_DATA_REPLICAS_WANT, struct bch_sb, flags[0], 52, 56);
1276 LE64_BITMASK(BCH_SB_POSIX_ACL, struct bch_sb, flags[0], 56, 57);
1277 LE64_BITMASK(BCH_SB_USRQUOTA, struct bch_sb, flags[0], 57, 58);
1278 LE64_BITMASK(BCH_SB_GRPQUOTA, struct bch_sb, flags[0], 58, 59);
1279 LE64_BITMASK(BCH_SB_PRJQUOTA, struct bch_sb, flags[0], 59, 60);
1281 LE64_BITMASK(BCH_SB_HAS_ERRORS, struct bch_sb, flags[0], 60, 61);
1285 LE64_BITMASK(BCH_SB_STR_HASH_TYPE, struct bch_sb, flags[1], 0, 4);
1286 LE64_BITMASK(BCH_SB_COMPRESSION_TYPE, struct bch_sb, flags[1], 4, 8);
1287 LE64_BITMASK(BCH_SB_INODE_32BIT, struct bch_sb, flags[1], 8, 9);
1289 LE64_BITMASK(BCH_SB_128_BIT_MACS, struct bch_sb, flags[1], 9, 10);
1290 LE64_BITMASK(BCH_SB_ENCRYPTION_TYPE, struct bch_sb, flags[1], 10, 14);
1293 * Max size of an extent that may require bouncing to read or write
1294 * (checksummed, compressed): 64k
1296 LE64_BITMASK(BCH_SB_ENCODED_EXTENT_MAX_BITS,
1297 struct bch_sb, flags[1], 14, 20);
1299 LE64_BITMASK(BCH_SB_META_REPLICAS_REQ, struct bch_sb, flags[1], 20, 24);
1300 LE64_BITMASK(BCH_SB_DATA_REPLICAS_REQ, struct bch_sb, flags[1], 24, 28);
1302 LE64_BITMASK(BCH_SB_PROMOTE_TARGET, struct bch_sb, flags[1], 28, 40);
1303 LE64_BITMASK(BCH_SB_FOREGROUND_TARGET, struct bch_sb, flags[1], 40, 52);
1304 LE64_BITMASK(BCH_SB_BACKGROUND_TARGET, struct bch_sb, flags[1], 52, 64);
1306 LE64_BITMASK(BCH_SB_BACKGROUND_COMPRESSION_TYPE,
1307 struct bch_sb, flags[2], 0, 4);
1308 LE64_BITMASK(BCH_SB_GC_RESERVE_BYTES, struct bch_sb, flags[2], 4, 64);
1310 LE64_BITMASK(BCH_SB_ERASURE_CODE, struct bch_sb, flags[3], 0, 16);
1313 enum bch_sb_features {
1314 BCH_FEATURE_LZ4 = 0,
1315 BCH_FEATURE_GZIP = 1,
1316 BCH_FEATURE_ZSTD = 2,
1317 BCH_FEATURE_ATOMIC_NLINK = 3, /* should have gone under compat */
1319 BCH_FEATURE_JOURNAL_SEQ_BLACKLIST_V3 = 5,
1320 BCH_FEATURE_REFLINK = 6,
1321 BCH_FEATURE_NEW_SIPHASH = 7,
1325 enum bch_sb_compat {
1326 BCH_COMPAT_FEAT_ALLOC_INFO = 0,
1327 BCH_COMPAT_FEAT_ALLOC_METADATA = 1,
1332 #define BCH_REPLICAS_MAX 4U
1334 enum bch_error_actions {
1335 BCH_ON_ERROR_CONTINUE = 0,
1336 BCH_ON_ERROR_RO = 1,
1337 BCH_ON_ERROR_PANIC = 2,
1338 BCH_NR_ERROR_ACTIONS = 3,
1341 enum bch_csum_opts {
1342 BCH_CSUM_OPT_NONE = 0,
1343 BCH_CSUM_OPT_CRC32C = 1,
1344 BCH_CSUM_OPT_CRC64 = 2,
1345 BCH_CSUM_OPT_NR = 3,
1348 enum bch_str_hash_type {
1349 BCH_STR_HASH_CRC32C = 0,
1350 BCH_STR_HASH_CRC64 = 1,
1351 BCH_STR_HASH_SIPHASH_OLD = 2,
1352 BCH_STR_HASH_SIPHASH = 3,
1353 BCH_STR_HASH_NR = 4,
1356 enum bch_str_hash_opts {
1357 BCH_STR_HASH_OPT_CRC32C = 0,
1358 BCH_STR_HASH_OPT_CRC64 = 1,
1359 BCH_STR_HASH_OPT_SIPHASH = 2,
1360 BCH_STR_HASH_OPT_NR = 3,
1363 #define BCH_COMPRESSION_TYPES() \
1369 enum bch_compression_opts {
1370 #define x(t) BCH_COMPRESSION_OPT_##t,
1371 BCH_COMPRESSION_TYPES()
1373 BCH_COMPRESSION_OPT_NR
1379 * The various other data structures have their own magic numbers, which are
1380 * xored with the first part of the cache set's UUID
1383 #define BCACHE_MAGIC \
1384 UUID_INIT(0xc68573f6, 0x4e1a, 0x45ca, \
1385 0x82, 0x65, 0xf5, 0x7f, 0x48, 0xba, 0x6d, 0x81)
1386 #define BCHFS_MAGIC \
1387 UUID_INIT(0xc68573f6, 0x66ce, 0x90a9, \
1388 0xd9, 0x6a, 0x60, 0xcf, 0x80, 0x3d, 0xf7, 0xef)
1390 #define BCACHEFS_STATFS_MAGIC 0xca451a4e
1392 #define JSET_MAGIC __cpu_to_le64(0x245235c1a3625032ULL)
1393 #define BSET_MAGIC __cpu_to_le64(0x90135c78b99e07f5ULL)
1395 static inline __le64 __bch2_sb_magic(struct bch_sb *sb)
1398 memcpy(&ret, &sb->uuid, sizeof(ret));
1402 static inline __u64 __jset_magic(struct bch_sb *sb)
1404 return __le64_to_cpu(__bch2_sb_magic(sb) ^ JSET_MAGIC);
1407 static inline __u64 __bset_magic(struct bch_sb *sb)
1409 return __le64_to_cpu(__bch2_sb_magic(sb) ^ BSET_MAGIC);
1414 #define JSET_KEYS_U64s (sizeof(struct jset_entry) / sizeof(__u64))
1416 #define BCH_JSET_ENTRY_TYPES() \
1421 x(blacklist_v2, 4) \
1426 #define x(f, nr) BCH_JSET_ENTRY_##f = nr,
1427 BCH_JSET_ENTRY_TYPES()
1433 * Journal sequence numbers can be blacklisted: bsets record the max sequence
1434 * number of all the journal entries they contain updates for, so that on
1435 * recovery we can ignore those bsets that contain index updates newer that what
1436 * made it into the journal.
1438 * This means that we can't reuse that journal_seq - we have to skip it, and
1439 * then record that we skipped it so that the next time we crash and recover we
1440 * don't think there was a missing journal entry.
1442 struct jset_entry_blacklist {
1443 struct jset_entry entry;
1447 struct jset_entry_blacklist_v2 {
1448 struct jset_entry entry;
1454 FS_USAGE_RESERVED = 0,
1455 FS_USAGE_INODES = 1,
1456 FS_USAGE_KEY_VERSION = 2,
1460 struct jset_entry_usage {
1461 struct jset_entry entry;
1463 } __attribute__((packed));
1465 struct jset_entry_data_usage {
1466 struct jset_entry entry;
1468 struct bch_replicas_entry r;
1469 } __attribute__((packed));
1472 * On disk format for a journal entry:
1473 * seq is monotonically increasing; every journal entry has its own unique
1476 * last_seq is the oldest journal entry that still has keys the btree hasn't
1477 * flushed to disk yet.
1479 * version is for on disk format changes.
1482 struct bch_csum csum;
1489 __le32 u64s; /* size of d[] in u64s */
1491 __u8 encrypted_start[0];
1496 /* Sequence number of oldest dirty journal entry */
1501 struct jset_entry start[0];
1504 } __attribute__((packed, aligned(8)));
1506 LE32_BITMASK(JSET_CSUM_TYPE, struct jset, flags, 0, 4);
1507 LE32_BITMASK(JSET_BIG_ENDIAN, struct jset, flags, 4, 5);
1509 #define BCH_JOURNAL_BUCKETS_MIN 8
1513 #define BCH_BTREE_IDS() \
1514 x(EXTENTS, 0, "extents") \
1515 x(INODES, 1, "inodes") \
1516 x(DIRENTS, 2, "dirents") \
1517 x(XATTRS, 3, "xattrs") \
1518 x(ALLOC, 4, "alloc") \
1519 x(QUOTAS, 5, "quotas") \
1520 x(EC, 6, "erasure_coding") \
1521 x(REFLINK, 7, "reflink")
1524 #define x(kwd, val, name) BTREE_ID_##kwd = val,
1530 #define BTREE_MAX_DEPTH 4U
1537 * On disk a btree node is a list/log of these; within each set the keys are
1544 * Highest journal entry this bset contains keys for.
1545 * If on recovery we don't see that journal entry, this bset is ignored:
1546 * this allows us to preserve the order of all index updates after a
1547 * crash, since the journal records a total order of all index updates
1548 * and anything that didn't make it to the journal doesn't get used.
1554 __le16 u64s; /* count of d[] in u64s */
1557 struct bkey_packed start[0];
1560 } __attribute__((packed, aligned(8)));
1562 LE32_BITMASK(BSET_CSUM_TYPE, struct bset, flags, 0, 4);
1564 LE32_BITMASK(BSET_BIG_ENDIAN, struct bset, flags, 4, 5);
1565 LE32_BITMASK(BSET_SEPARATE_WHITEOUTS,
1566 struct bset, flags, 5, 6);
1569 struct bch_csum csum;
1572 /* this flags field is encrypted, unlike bset->flags: */
1575 /* Closed interval: */
1576 struct bpos min_key;
1577 struct bpos max_key;
1578 struct bch_extent_ptr ptr;
1579 struct bkey_format format;
1590 } __attribute__((packed, aligned(8)));
1592 LE64_BITMASK(BTREE_NODE_ID, struct btree_node, flags, 0, 4);
1593 LE64_BITMASK(BTREE_NODE_LEVEL, struct btree_node, flags, 4, 8);
1595 LE64_BITMASK(BTREE_NODE_SEQ, struct btree_node, flags, 32, 64);
1597 struct btree_node_entry {
1598 struct bch_csum csum;
1609 } __attribute__((packed, aligned(8)));
1611 #endif /* _BCACHEFS_FORMAT_H */