1 #ifndef _BCACHEFS_FORMAT_H
2 #define _BCACHEFS_FORMAT_H
5 * bcachefs on disk data structures
9 #include <linux/compiler.h>
10 #include <asm/byteorder.h>
11 #include <linux/uuid.h>
13 #define LE_BITMASK(_bits, name, type, field, offset, end) \
14 static const unsigned name##_OFFSET = offset; \
15 static const unsigned name##_BITS = (end - offset); \
16 static const __u##_bits name##_MAX = (1ULL << (end - offset)) - 1; \
18 static inline __u64 name(const type *k) \
20 return (__le##_bits##_to_cpu(k->field) >> offset) & \
21 ~(~0ULL << (end - offset)); \
24 static inline void SET_##name(type *k, __u64 v) \
26 __u##_bits new = __le##_bits##_to_cpu(k->field); \
28 new &= ~(~(~0ULL << (end - offset)) << offset); \
29 new |= (v & ~(~0ULL << (end - offset))) << offset; \
30 k->field = __cpu_to_le##_bits(new); \
33 #define LE16_BITMASK(n, t, f, o, e) LE_BITMASK(16, n, t, f, o, e)
34 #define LE32_BITMASK(n, t, f, o, e) LE_BITMASK(32, n, t, f, o, e)
35 #define LE64_BITMASK(n, t, f, o, e) LE_BITMASK(64, n, t, f, o, e)
40 /* One unused slot for now: */
41 __u8 bits_per_field[6];
42 __le64 field_offset[6];
45 /* Btree keys - all units are in sectors */
48 /* Word order matches machine byte order */
49 #if defined(__LITTLE_ENDIAN)
53 #elif defined(__BIG_ENDIAN)
55 __u64 offset; /* Points to end of extent - sectors */
58 #error edit for your odd byteorder.
60 } __attribute__((packed, aligned(4)));
62 #define KEY_INODE_MAX ((__u64)~0ULL)
63 #define KEY_OFFSET_MAX ((__u64)~0ULL)
64 #define KEY_SNAPSHOT_MAX ((__u32)~0U)
65 #define KEY_SIZE_MAX ((__u32)~0U)
67 static inline struct bpos POS(__u64 inode, __u64 offset)
78 #define POS_MIN POS(0, 0)
79 #define POS_MAX POS(KEY_INODE_MAX, KEY_OFFSET_MAX)
81 /* Empty placeholder struct, for container_of() */
87 #if defined(__LITTLE_ENDIAN)
90 #elif defined(__BIG_ENDIAN)
94 } __attribute__((packed, aligned(4)));
97 /* Size of combined key and value, in u64s */
100 /* Format of key (0 for format local to btree node) */
101 #if defined(__LITTLE_ENDIAN_BITFIELD)
104 #elif defined (__BIG_ENDIAN_BITFIELD)
105 __u8 needs_whiteout:1,
108 #error edit for your odd byteorder.
111 /* Type of the value */
114 #if defined(__LITTLE_ENDIAN)
117 struct bversion version;
118 __u32 size; /* extent size, in sectors */
120 #elif defined(__BIG_ENDIAN)
122 __u32 size; /* extent size, in sectors */
123 struct bversion version;
127 } __attribute__((packed, aligned(8)));
132 /* Size of combined key and value, in u64s */
135 /* Format of key (0 for format local to btree node) */
138 * XXX: next incompat on disk format change, switch format and
139 * needs_whiteout - bkey_packed() will be cheaper if format is the high
140 * bits of the bitfield
142 #if defined(__LITTLE_ENDIAN_BITFIELD)
145 #elif defined (__BIG_ENDIAN_BITFIELD)
146 __u8 needs_whiteout:1,
150 /* Type of the value */
155 * We copy bkeys with struct assignment in various places, and while
156 * that shouldn't be done with packed bkeys we can't disallow it in C,
157 * and it's legal to cast a bkey to a bkey_packed - so padding it out
158 * to the same size as struct bkey should hopefully be safest.
160 __u8 pad[sizeof(struct bkey) - 3];
161 } __attribute__((packed, aligned(8)));
163 #define BKEY_U64s (sizeof(struct bkey) / sizeof(__u64))
164 #define KEY_PACKED_BITS_START 24
166 #define KEY_FORMAT_LOCAL_BTREE 0
167 #define KEY_FORMAT_CURRENT 1
169 enum bch_bkey_fields {
174 BKEY_FIELD_VERSION_HI,
175 BKEY_FIELD_VERSION_LO,
179 #define bkey_format_field(name, field) \
180 [BKEY_FIELD_##name] = (sizeof(((struct bkey *) NULL)->field) * 8)
182 #define BKEY_FORMAT_CURRENT \
183 ((struct bkey_format) { \
184 .key_u64s = BKEY_U64s, \
185 .nr_fields = BKEY_NR_FIELDS, \
186 .bits_per_field = { \
187 bkey_format_field(INODE, p.inode), \
188 bkey_format_field(OFFSET, p.offset), \
189 bkey_format_field(SNAPSHOT, p.snapshot), \
190 bkey_format_field(SIZE, size), \
191 bkey_format_field(VERSION_HI, version.hi), \
192 bkey_format_field(VERSION_LO, version.lo), \
196 /* bkey with inline value */
202 /* Size of combined key and value, in u64s */
212 #define KEY(_inode, _offset, _size) \
215 .format = KEY_FORMAT_CURRENT, \
216 .p = POS(_inode, _offset), \
220 static inline void bkey_init(struct bkey *k)
225 #define bkey_bytes(_k) ((_k)->u64s * sizeof(__u64))
227 #define __BKEY_PADDED(key, pad) \
228 struct { struct bkey_i key; __u64 key ## _pad[pad]; }
230 #define BKEY_VAL_TYPE(name, nr) \
231 struct bkey_i_##name { \
236 struct bch_##name v; \
240 * - DELETED keys are used internally to mark keys that should be ignored but
241 * override keys in composition order. Their version number is ignored.
243 * - DISCARDED keys indicate that the data is all 0s because it has been
244 * discarded. DISCARDs may have a version; if the version is nonzero the key
245 * will be persistent, otherwise the key will be dropped whenever the btree
246 * node is rewritten (like DELETED keys).
248 * - ERROR: any read of the data returns a read error, as the data was lost due
249 * to a failing device. Like DISCARDED keys, they can be removed (overridden)
250 * by new writes or cluster-wide GC. Node repair can also overwrite them with
251 * the same or a more recent version number, but not with an older version
254 #define KEY_TYPE_DELETED 0
255 #define KEY_TYPE_DISCARD 1
256 #define KEY_TYPE_ERROR 2
257 #define KEY_TYPE_COOKIE 3
258 #define KEY_TYPE_PERSISTENT_DISCARD 4
259 #define KEY_TYPE_GENERIC_NR 128
265 BKEY_VAL_TYPE(cookie, KEY_TYPE_COOKIE);
270 * In extent bkeys, the value is a list of pointers (bch_extent_ptr), optionally
271 * preceded by checksum/compression information (bch_extent_crc32 or
274 * One major determining factor in the format of extents is how we handle and
275 * represent extents that have been partially overwritten and thus trimmed:
277 * If an extent is not checksummed or compressed, when the extent is trimmed we
278 * don't have to remember the extent we originally allocated and wrote: we can
279 * merely adjust ptr->offset to point to the start of the start of the data that
280 * is currently live. The size field in struct bkey records the current (live)
281 * size of the extent, and is also used to mean "size of region on disk that we
282 * point to" in this case.
284 * Thus an extent that is not checksummed or compressed will consist only of a
285 * list of bch_extent_ptrs, with none of the fields in
286 * bch_extent_crc32/bch_extent_crc64.
288 * When an extent is checksummed or compressed, it's not possible to read only
289 * the data that is currently live: we have to read the entire extent that was
290 * originally written, and then return only the part of the extent that is
293 * Thus, in addition to the current size of the extent in struct bkey, we need
294 * to store the size of the originally allocated space - this is the
295 * compressed_size and uncompressed_size fields in bch_extent_crc32/64. Also,
296 * when the extent is trimmed, instead of modifying the offset field of the
297 * pointer, we keep a second smaller offset field - "offset into the original
298 * extent of the currently live region".
300 * The other major determining factor is replication and data migration:
302 * Each pointer may have its own bch_extent_crc32/64. When doing a replicated
303 * write, we will initially write all the replicas in the same format, with the
304 * same checksum type and compression format - however, when copygc runs later (or
305 * tiering/cache promotion, anything that moves data), it is not in general
306 * going to rewrite all the pointers at once - one of the replicas may be in a
307 * bucket on one device that has very little fragmentation while another lives
308 * in a bucket that has become heavily fragmented, and thus is being rewritten
309 * sooner than the rest.
311 * Thus it will only move a subset of the pointers (or in the case of
312 * tiering/cache promotion perhaps add a single pointer without dropping any
313 * current pointers), and if the extent has been partially overwritten it must
314 * write only the currently live portion (or copygc would not be able to reduce
315 * fragmentation!) - which necessitates a different bch_extent_crc format for
318 * But in the interests of space efficiency, we don't want to store one
319 * bch_extent_crc for each pointer if we don't have to.
321 * Thus, a bch_extent consists of bch_extent_crc32s, bch_extent_crc64s, and
322 * bch_extent_ptrs appended arbitrarily one after the other. We determine the
323 * type of a given entry with a scheme similar to utf8 (except we're encoding a
324 * type, not a size), encoding the type in the position of the first set bit:
326 * bch_extent_crc32 - 0b1
327 * bch_extent_ptr - 0b10
328 * bch_extent_crc64 - 0b100
330 * We do it this way because bch_extent_crc32 is _very_ constrained on bits (and
331 * bch_extent_crc64 is the least constrained).
333 * Then, each bch_extent_crc32/64 applies to the pointers that follow after it,
334 * until the next bch_extent_crc32/64.
336 * If there are no bch_extent_crcs preceding a bch_extent_ptr, then that pointer
337 * is neither checksummed nor compressed.
340 /* 128 bits, sufficient for cryptographic MACs: */
344 } __attribute__((packed, aligned(8)));
348 BCH_CSUM_CRC32C_NONZERO = 1,
349 BCH_CSUM_CRC64_NONZERO = 2,
350 BCH_CSUM_CHACHA20_POLY1305_80 = 3,
351 BCH_CSUM_CHACHA20_POLY1305_128 = 4,
357 static inline _Bool bch2_csum_type_is_encryption(enum bch_csum_type type)
360 case BCH_CSUM_CHACHA20_POLY1305_80:
361 case BCH_CSUM_CHACHA20_POLY1305_128:
368 enum bch_compression_type {
369 BCH_COMPRESSION_NONE = 0,
370 BCH_COMPRESSION_LZ4_OLD = 1,
371 BCH_COMPRESSION_GZIP = 2,
372 BCH_COMPRESSION_LZ4 = 3,
373 BCH_COMPRESSION_NR = 4,
376 enum bch_extent_entry_type {
377 BCH_EXTENT_ENTRY_ptr = 0,
378 BCH_EXTENT_ENTRY_crc32 = 1,
379 BCH_EXTENT_ENTRY_crc64 = 2,
380 BCH_EXTENT_ENTRY_crc128 = 3,
383 #define BCH_EXTENT_ENTRY_MAX 4
385 /* Compressed/uncompressed size are stored biased by 1: */
386 struct bch_extent_crc32 {
387 #if defined(__LITTLE_ENDIAN_BITFIELD)
390 _uncompressed_size:7,
396 #elif defined (__BIG_ENDIAN_BITFIELD)
398 __u32 compression_type:4,
402 _uncompressed_size:7,
406 } __attribute__((packed, aligned(8)));
408 #define CRC32_SIZE_MAX (1U << 7)
409 #define CRC32_NONCE_MAX 0
411 struct bch_extent_crc64 {
412 #if defined(__LITTLE_ENDIAN_BITFIELD)
415 _uncompressed_size:9,
421 #elif defined (__BIG_ENDIAN_BITFIELD)
427 _uncompressed_size:9,
432 } __attribute__((packed, aligned(8)));
434 #define CRC64_SIZE_MAX (1U << 9)
435 #define CRC64_NONCE_MAX ((1U << 10) - 1)
437 struct bch_extent_crc128 {
438 #if defined(__LITTLE_ENDIAN_BITFIELD)
441 _uncompressed_size:13,
446 #elif defined (__BIG_ENDIAN_BITFIELD)
447 __u64 compression_type:4,
451 _uncompressed_size:13,
455 struct bch_csum csum;
456 } __attribute__((packed, aligned(8)));
458 #define CRC128_SIZE_MAX (1U << 13)
459 #define CRC128_NONCE_MAX ((1U << 13) - 1)
462 * @reservation - pointer hasn't been written to, just reserved
464 struct bch_extent_ptr {
465 #if defined(__LITTLE_ENDIAN_BITFIELD)
470 offset:44, /* 8 petabytes */
473 #elif defined (__BIG_ENDIAN_BITFIELD)
482 } __attribute__((packed, aligned(8)));
484 struct bch_extent_reservation {
485 #if defined(__LITTLE_ENDIAN_BITFIELD)
490 #elif defined (__BIG_ENDIAN_BITFIELD)
498 union bch_extent_entry {
499 #if defined(__LITTLE_ENDIAN) || __BITS_PER_LONG == 64
501 #elif __BITS_PER_LONG == 32
507 #error edit for your odd byteorder.
509 struct bch_extent_crc32 crc32;
510 struct bch_extent_crc64 crc64;
511 struct bch_extent_crc128 crc128;
512 struct bch_extent_ptr ptr;
519 * This is kind of a hack, we're overloading the type for a boolean that
520 * really should be part of the value - BCH_EXTENT and BCH_EXTENT_CACHED
521 * have the same value type:
523 BCH_EXTENT_CACHED = 129,
526 * Persistent reservation:
528 BCH_RESERVATION = 130,
534 union bch_extent_entry start[0];
536 } __attribute__((packed, aligned(8)));
537 BKEY_VAL_TYPE(extent, BCH_EXTENT);
539 struct bch_reservation {
545 } __attribute__((packed, aligned(8)));
546 BKEY_VAL_TYPE(reservation, BCH_RESERVATION);
548 /* Maximum size (in u64s) a single pointer could be: */
549 #define BKEY_EXTENT_PTR_U64s_MAX\
550 ((sizeof(struct bch_extent_crc128) + \
551 sizeof(struct bch_extent_ptr)) / sizeof(u64))
553 /* Maximum possible size of an entire extent value: */
554 /* There's a hack in the keylist code that needs to be fixed.. */
555 #define BKEY_EXTENT_VAL_U64s_MAX \
556 (BKEY_EXTENT_PTR_U64s_MAX * (BCH_REPLICAS_MAX + 1))
558 /* * Maximum possible size of an entire extent, key + value: */
559 #define BKEY_EXTENT_U64s_MAX (BKEY_U64s + BKEY_EXTENT_VAL_U64s_MAX)
561 /* Btree pointers don't carry around checksums: */
562 #define BKEY_BTREE_PTR_VAL_U64s_MAX \
563 ((sizeof(struct bch_extent_ptr)) / sizeof(u64) * BCH_REPLICAS_MAX)
564 #define BKEY_BTREE_PTR_U64s_MAX \
565 (BKEY_U64s + BKEY_BTREE_PTR_VAL_U64s_MAX)
569 #define BLOCKDEV_INODE_MAX 4096
571 #define BCACHEFS_ROOT_INO 4096
573 enum bch_inode_types {
575 BCH_INODE_BLOCKDEV = 129,
576 BCH_INODE_GENERATION = 130,
586 } __attribute__((packed, aligned(8)));
587 BKEY_VAL_TYPE(inode, BCH_INODE_FS);
589 struct bch_inode_generation {
592 __le32 bi_generation;
594 } __attribute__((packed, aligned(8)));
595 BKEY_VAL_TYPE(inode_generation, BCH_INODE_GENERATION);
597 #define BCH_INODE_FIELDS() \
598 BCH_INODE_FIELD(bi_atime, 64) \
599 BCH_INODE_FIELD(bi_ctime, 64) \
600 BCH_INODE_FIELD(bi_mtime, 64) \
601 BCH_INODE_FIELD(bi_otime, 64) \
602 BCH_INODE_FIELD(bi_size, 64) \
603 BCH_INODE_FIELD(bi_sectors, 64) \
604 BCH_INODE_FIELD(bi_uid, 32) \
605 BCH_INODE_FIELD(bi_gid, 32) \
606 BCH_INODE_FIELD(bi_nlink, 32) \
607 BCH_INODE_FIELD(bi_generation, 32) \
608 BCH_INODE_FIELD(bi_dev, 32) \
609 BCH_INODE_FIELD(bi_data_checksum, 8) \
610 BCH_INODE_FIELD(bi_compression, 8) \
611 BCH_INODE_FIELD(bi_project, 32)
613 #define BCH_INODE_FIELDS_INHERIT() \
614 BCH_INODE_FIELD(bi_data_checksum) \
615 BCH_INODE_FIELD(bi_compression) \
616 BCH_INODE_FIELD(bi_project)
620 * User flags (get/settable with FS_IOC_*FLAGS, correspond to FS_*_FL
623 __BCH_INODE_SYNC = 0,
624 __BCH_INODE_IMMUTABLE = 1,
625 __BCH_INODE_APPEND = 2,
626 __BCH_INODE_NODUMP = 3,
627 __BCH_INODE_NOATIME = 4,
629 __BCH_INODE_I_SIZE_DIRTY= 5,
630 __BCH_INODE_I_SECTORS_DIRTY= 6,
632 /* not implemented yet: */
633 __BCH_INODE_HAS_XATTRS = 7, /* has xattrs in xattr btree */
635 /* bits 20+ reserved for packed fields below: */
638 #define BCH_INODE_SYNC (1 << __BCH_INODE_SYNC)
639 #define BCH_INODE_IMMUTABLE (1 << __BCH_INODE_IMMUTABLE)
640 #define BCH_INODE_APPEND (1 << __BCH_INODE_APPEND)
641 #define BCH_INODE_NODUMP (1 << __BCH_INODE_NODUMP)
642 #define BCH_INODE_NOATIME (1 << __BCH_INODE_NOATIME)
643 #define BCH_INODE_I_SIZE_DIRTY (1 << __BCH_INODE_I_SIZE_DIRTY)
644 #define BCH_INODE_I_SECTORS_DIRTY (1 << __BCH_INODE_I_SECTORS_DIRTY)
645 #define BCH_INODE_HAS_XATTRS (1 << __BCH_INODE_HAS_XATTRS)
647 LE32_BITMASK(INODE_STR_HASH, struct bch_inode, bi_flags, 20, 24);
648 LE32_BITMASK(INODE_NR_FIELDS, struct bch_inode, bi_flags, 24, 32);
650 struct bch_inode_blockdev {
662 } __attribute__((packed, aligned(8)));
663 BKEY_VAL_TYPE(inode_blockdev, BCH_INODE_BLOCKDEV);
665 /* Thin provisioned volume, or cache for another block device? */
666 LE64_BITMASK(CACHED_DEV, struct bch_inode_blockdev, i_flags, 0, 1)
671 * Dirents (and xattrs) have to implement string lookups; since our b-tree
672 * doesn't support arbitrary length strings for the key, we instead index by a
673 * 64 bit hash (currently truncated sha1) of the string, stored in the offset
674 * field of the key - using linear probing to resolve hash collisions. This also
675 * provides us with the readdir cookie posix requires.
677 * Linear probing requires us to use whiteouts for deletions, in the event of a
683 BCH_DIRENT_WHITEOUT = 129,
689 /* Target inode number: */
693 * Copy of mode bits 12-15 from the target inode - so userspace can get
694 * the filetype without having to do a stat()
699 } __attribute__((packed, aligned(8)));
700 BKEY_VAL_TYPE(dirent, BCH_DIRENT);
706 BCH_XATTR_WHITEOUT = 129,
709 #define BCH_XATTR_INDEX_USER 0
710 #define BCH_XATTR_INDEX_POSIX_ACL_ACCESS 1
711 #define BCH_XATTR_INDEX_POSIX_ACL_DEFAULT 2
712 #define BCH_XATTR_INDEX_TRUSTED 3
713 #define BCH_XATTR_INDEX_SECURITY 4
721 } __attribute__((packed, aligned(8)));
722 BKEY_VAL_TYPE(xattr, BCH_XATTR);
724 /* Bucket/allocation information: */
731 BCH_ALLOC_FIELD_READ_TIME = 0,
732 BCH_ALLOC_FIELD_WRITE_TIME = 1,
740 } __attribute__((packed, aligned(8)));
741 BKEY_VAL_TYPE(alloc, BCH_ALLOC);
756 enum quota_counters {
762 struct bch_quota_counter {
769 struct bch_quota_counter c[Q_COUNTERS];
770 } __attribute__((packed, aligned(8)));
771 BKEY_VAL_TYPE(quota, BCH_QUOTA);
773 /* Optional/variable size superblock sections: */
775 struct bch_sb_field {
781 #define BCH_SB_FIELDS() \
789 enum bch_sb_field_type {
790 #define x(f, nr) BCH_SB_FIELD_##f = nr,
796 /* BCH_SB_FIELD_journal: */
798 struct bch_sb_field_journal {
799 struct bch_sb_field field;
803 /* BCH_SB_FIELD_members: */
807 __le64 nbuckets; /* device size */
808 __le16 first_bucket; /* index of first bucket used */
809 __le16 bucket_size; /* sectors */
811 __le64 last_mount; /* time_t */
816 LE64_BITMASK(BCH_MEMBER_STATE, struct bch_member, flags[0], 0, 4)
817 LE64_BITMASK(BCH_MEMBER_TIER, struct bch_member, flags[0], 4, 8)
818 /* 8-10 unused, was HAS_(META)DATA */
819 LE64_BITMASK(BCH_MEMBER_REPLACEMENT, struct bch_member, flags[0], 10, 14)
820 LE64_BITMASK(BCH_MEMBER_DISCARD, struct bch_member, flags[0], 14, 15)
821 LE64_BITMASK(BCH_MEMBER_DATA_ALLOWED, struct bch_member, flags[0], 15, 20)
822 LE64_BITMASK(BCH_MEMBER_GROUP, struct bch_member, flags[0], 20, 28)
825 LE64_BITMASK(BCH_MEMBER_NR_READ_ERRORS, struct bch_member, flags[1], 0, 20);
826 LE64_BITMASK(BCH_MEMBER_NR_WRITE_ERRORS,struct bch_member, flags[1], 20, 40);
829 enum bch_member_state {
830 BCH_MEMBER_STATE_RW = 0,
831 BCH_MEMBER_STATE_RO = 1,
832 BCH_MEMBER_STATE_FAILED = 2,
833 BCH_MEMBER_STATE_SPARE = 3,
834 BCH_MEMBER_STATE_NR = 4,
837 #define BCH_TIER_MAX 4U
839 enum cache_replacement {
840 CACHE_REPLACEMENT_LRU = 0,
841 CACHE_REPLACEMENT_FIFO = 1,
842 CACHE_REPLACEMENT_RANDOM = 2,
843 CACHE_REPLACEMENT_NR = 3,
846 struct bch_sb_field_members {
847 struct bch_sb_field field;
848 struct bch_member members[0];
851 /* BCH_SB_FIELD_crypt: */
861 #define BCH_KEY_MAGIC \
862 (((u64) 'b' << 0)|((u64) 'c' << 8)| \
863 ((u64) 'h' << 16)|((u64) '*' << 24)| \
864 ((u64) '*' << 32)|((u64) 'k' << 40)| \
865 ((u64) 'e' << 48)|((u64) 'y' << 56))
867 struct bch_encrypted_key {
873 * If this field is present in the superblock, it stores an encryption key which
874 * is used encrypt all other data/metadata. The key will normally be encrypted
875 * with the key userspace provides, but if encryption has been turned off we'll
876 * just store the master key unencrypted in the superblock so we can access the
877 * previously encrypted data.
879 struct bch_sb_field_crypt {
880 struct bch_sb_field field;
884 struct bch_encrypted_key key;
887 LE64_BITMASK(BCH_CRYPT_KDF_TYPE, struct bch_sb_field_crypt, flags, 0, 4);
894 /* stored as base 2 log of scrypt params: */
895 LE64_BITMASK(BCH_KDF_SCRYPT_N, struct bch_sb_field_crypt, kdf_flags, 0, 16);
896 LE64_BITMASK(BCH_KDF_SCRYPT_R, struct bch_sb_field_crypt, kdf_flags, 16, 32);
897 LE64_BITMASK(BCH_KDF_SCRYPT_P, struct bch_sb_field_crypt, kdf_flags, 32, 48);
899 /* BCH_SB_FIELD_replicas: */
904 BCH_DATA_JOURNAL = 2,
911 struct bch_replicas_entry {
917 struct bch_sb_field_replicas {
918 struct bch_sb_field field;
919 struct bch_replicas_entry entries[0];
922 /* BCH_SB_FIELD_quota: */
924 struct bch_sb_quota_counter {
929 struct bch_sb_quota_type {
931 struct bch_sb_quota_counter c[Q_COUNTERS];
934 struct bch_sb_field_quota {
935 struct bch_sb_field field;
936 struct bch_sb_quota_type q[QTYP_NR];
937 } __attribute__((packed, aligned(8)));
939 /* BCH_SB_FIELD_disk_groups: */
941 #define BCH_SB_LABEL_SIZE 32
943 struct bch_disk_group {
944 __u8 label[BCH_SB_LABEL_SIZE];
948 LE64_BITMASK(BCH_GROUP_DELETED, struct bch_disk_group, flags[0], 0, 1)
949 LE64_BITMASK(BCH_GROUP_DATA_ALLOWED, struct bch_disk_group, flags[0], 1, 6)
951 struct bch_sb_field_disk_groups {
952 struct bch_sb_field field;
953 struct bch_disk_group entries[0];
959 * Version 8: BCH_SB_ENCODED_EXTENT_MAX_BITS
960 * BCH_MEMBER_DATA_ALLOWED
961 * Version 9: incompatible extent nonce change
964 #define BCH_SB_VERSION_MIN 7
965 #define BCH_SB_VERSION_EXTENT_MAX 8
966 #define BCH_SB_VERSION_EXTENT_NONCE_V1 9
967 #define BCH_SB_VERSION_MAX 9
969 #define BCH_SB_SECTOR 8
970 #define BCH_SB_MEMBERS_MAX 64 /* XXX kill */
972 struct bch_sb_layout {
973 uuid_le magic; /* bcachefs superblock UUID */
975 __u8 sb_max_size_bits; /* base 2 of 512 byte sectors */
978 __le64 sb_offset[61];
979 } __attribute__((packed, aligned(8)));
981 #define BCH_SB_LAYOUT_SECTOR 7
984 * @offset - sector where this sb was written
985 * @version - on disk format version
986 * @magic - identifies as a bcachefs superblock (BCACHE_MAGIC)
987 * @seq - incremented each time superblock is written
988 * @uuid - used for generating various magic numbers and identifying
989 * member devices, never changes
990 * @user_uuid - user visible UUID, may be changed
991 * @label - filesystem label
992 * @seq - identifies most recent superblock, incremented each time
993 * superblock is written
994 * @features - enabled incompatible features
997 struct bch_csum csum;
1002 __u8 label[BCH_SB_LABEL_SIZE];
1011 __le64 time_base_lo;
1012 __le32 time_base_hi;
1013 __le32 time_precision;
1019 struct bch_sb_layout layout;
1022 struct bch_sb_field start[0];
1025 } __attribute__((packed, aligned(8)));
1029 * BCH_SB_INITALIZED - set on first mount
1030 * BCH_SB_CLEAN - did we shut down cleanly? Just a hint, doesn't affect
1031 * behaviour of mount/recovery path:
1032 * BCH_SB_INODE_32BIT - limit inode numbers to 32 bits
1033 * BCH_SB_128_BIT_MACS - 128 bit macs instead of 80
1034 * BCH_SB_ENCRYPTION_TYPE - if nonzero encryption is enabled; overrides
1035 * DATA/META_CSUM_TYPE. Also indicates encryption
1036 * algorithm in use, if/when we get more than one
1039 LE16_BITMASK(BCH_SB_BLOCK_SIZE, struct bch_sb, block_size, 0, 16);
1041 LE64_BITMASK(BCH_SB_INITIALIZED, struct bch_sb, flags[0], 0, 1);
1042 LE64_BITMASK(BCH_SB_CLEAN, struct bch_sb, flags[0], 1, 2);
1043 LE64_BITMASK(BCH_SB_CSUM_TYPE, struct bch_sb, flags[0], 2, 8);
1044 LE64_BITMASK(BCH_SB_ERROR_ACTION, struct bch_sb, flags[0], 8, 12);
1046 LE64_BITMASK(BCH_SB_BTREE_NODE_SIZE, struct bch_sb, flags[0], 12, 28);
1048 LE64_BITMASK(BCH_SB_GC_RESERVE, struct bch_sb, flags[0], 28, 33);
1049 LE64_BITMASK(BCH_SB_ROOT_RESERVE, struct bch_sb, flags[0], 33, 40);
1051 LE64_BITMASK(BCH_SB_META_CSUM_TYPE, struct bch_sb, flags[0], 40, 44);
1052 LE64_BITMASK(BCH_SB_DATA_CSUM_TYPE, struct bch_sb, flags[0], 44, 48);
1054 LE64_BITMASK(BCH_SB_META_REPLICAS_WANT, struct bch_sb, flags[0], 48, 52);
1055 LE64_BITMASK(BCH_SB_DATA_REPLICAS_WANT, struct bch_sb, flags[0], 52, 56);
1057 LE64_BITMASK(BCH_SB_POSIX_ACL, struct bch_sb, flags[0], 56, 57);
1058 LE64_BITMASK(BCH_SB_USRQUOTA, struct bch_sb, flags[0], 57, 58);
1059 LE64_BITMASK(BCH_SB_GRPQUOTA, struct bch_sb, flags[0], 58, 59);
1060 LE64_BITMASK(BCH_SB_PRJQUOTA, struct bch_sb, flags[0], 59, 60);
1064 LE64_BITMASK(BCH_SB_STR_HASH_TYPE, struct bch_sb, flags[1], 0, 4);
1065 LE64_BITMASK(BCH_SB_COMPRESSION_TYPE, struct bch_sb, flags[1], 4, 8);
1066 LE64_BITMASK(BCH_SB_INODE_32BIT, struct bch_sb, flags[1], 8, 9);
1068 LE64_BITMASK(BCH_SB_128_BIT_MACS, struct bch_sb, flags[1], 9, 10);
1069 LE64_BITMASK(BCH_SB_ENCRYPTION_TYPE, struct bch_sb, flags[1], 10, 14);
1072 * Max size of an extent that may require bouncing to read or write
1073 * (checksummed, compressed): 64k
1075 LE64_BITMASK(BCH_SB_ENCODED_EXTENT_MAX_BITS,
1076 struct bch_sb, flags[1], 14, 20);
1078 LE64_BITMASK(BCH_SB_META_REPLICAS_REQ, struct bch_sb, flags[1], 20, 24);
1079 LE64_BITMASK(BCH_SB_DATA_REPLICAS_REQ, struct bch_sb, flags[1], 24, 28);
1082 enum bch_sb_features {
1083 BCH_FEATURE_LZ4 = 0,
1084 BCH_FEATURE_GZIP = 1,
1089 #define BCH_REPLICAS_MAX 4U
1091 enum bch_error_actions {
1092 BCH_ON_ERROR_CONTINUE = 0,
1093 BCH_ON_ERROR_RO = 1,
1094 BCH_ON_ERROR_PANIC = 2,
1095 BCH_NR_ERROR_ACTIONS = 3,
1098 enum bch_csum_opts {
1099 BCH_CSUM_OPT_NONE = 0,
1100 BCH_CSUM_OPT_CRC32C = 1,
1101 BCH_CSUM_OPT_CRC64 = 2,
1102 BCH_CSUM_OPT_NR = 3,
1105 enum bch_str_hash_opts {
1106 BCH_STR_HASH_CRC32C = 0,
1107 BCH_STR_HASH_CRC64 = 1,
1108 BCH_STR_HASH_SIPHASH = 2,
1109 BCH_STR_HASH_NR = 3,
1112 enum bch_compression_opts {
1113 BCH_COMPRESSION_OPT_NONE = 0,
1114 BCH_COMPRESSION_OPT_LZ4 = 1,
1115 BCH_COMPRESSION_OPT_GZIP = 2,
1116 BCH_COMPRESSION_OPT_NR = 3,
1122 * The various other data structures have their own magic numbers, which are
1123 * xored with the first part of the cache set's UUID
1126 #define BCACHE_MAGIC \
1127 UUID_LE(0xf67385c6, 0x1a4e, 0xca45, \
1128 0x82, 0x65, 0xf5, 0x7f, 0x48, 0xba, 0x6d, 0x81)
1130 #define BCACHEFS_STATFS_MAGIC 0xca451a4e
1132 #define JSET_MAGIC __cpu_to_le64(0x245235c1a3625032ULL)
1133 #define BSET_MAGIC __cpu_to_le64(0x90135c78b99e07f5ULL)
1135 static inline __le64 __bch2_sb_magic(struct bch_sb *sb)
1138 memcpy(&ret, &sb->uuid, sizeof(ret));
1142 static inline __u64 __jset_magic(struct bch_sb *sb)
1144 return __le64_to_cpu(__bch2_sb_magic(sb) ^ JSET_MAGIC);
1147 static inline __u64 __bset_magic(struct bch_sb *sb)
1149 return __le64_to_cpu(__bch2_sb_magic(sb) ^ BSET_MAGIC);
1154 #define BCACHE_JSET_VERSION_UUIDv1 1
1155 #define BCACHE_JSET_VERSION_UUID 1 /* Always latest UUID format */
1156 #define BCACHE_JSET_VERSION_JKEYS 2
1157 #define BCACHE_JSET_VERSION 2
1163 __u8 type; /* designates what this jset holds */
1167 struct bkey_i start[0];
1172 struct jset_entry_blacklist {
1173 struct jset_entry entry;
1177 #define JSET_KEYS_U64s (sizeof(struct jset_entry) / sizeof(__u64))
1180 JOURNAL_ENTRY_BTREE_KEYS = 0,
1181 JOURNAL_ENTRY_BTREE_ROOT = 1,
1182 JOURNAL_ENTRY_PRIO_PTRS = 2, /* Obsolete */
1185 * Journal sequence numbers can be blacklisted: bsets record the max
1186 * sequence number of all the journal entries they contain updates for,
1187 * so that on recovery we can ignore those bsets that contain index
1188 * updates newer that what made it into the journal.
1190 * This means that we can't reuse that journal_seq - we have to skip it,
1191 * and then record that we skipped it so that the next time we crash and
1192 * recover we don't think there was a missing journal entry.
1194 JOURNAL_ENTRY_JOURNAL_SEQ_BLACKLISTED = 3,
1198 * On disk format for a journal entry:
1199 * seq is monotonically increasing; every journal entry has its own unique
1202 * last_seq is the oldest journal entry that still has keys the btree hasn't
1203 * flushed to disk yet.
1205 * version is for on disk format changes.
1208 struct bch_csum csum;
1215 __le32 u64s; /* size of d[] in u64s */
1217 __u8 encrypted_start[0];
1222 /* Sequence number of oldest dirty journal entry */
1227 struct jset_entry start[0];
1230 } __attribute__((packed, aligned(8)));
1232 LE32_BITMASK(JSET_CSUM_TYPE, struct jset, flags, 0, 4);
1233 LE32_BITMASK(JSET_BIG_ENDIAN, struct jset, flags, 4, 5);
1235 #define BCH_JOURNAL_BUCKETS_MIN 20
1239 #define DEFINE_BCH_BTREE_IDS() \
1240 DEF_BTREE_ID(EXTENTS, 0, "extents") \
1241 DEF_BTREE_ID(INODES, 1, "inodes") \
1242 DEF_BTREE_ID(DIRENTS, 2, "dirents") \
1243 DEF_BTREE_ID(XATTRS, 3, "xattrs") \
1244 DEF_BTREE_ID(ALLOC, 4, "alloc") \
1245 DEF_BTREE_ID(QUOTAS, 5, "quotas")
1247 #define DEF_BTREE_ID(kwd, val, name) BTREE_ID_##kwd = val,
1250 DEFINE_BCH_BTREE_IDS()
1256 #define BTREE_MAX_DEPTH 4U
1260 /* Version 1: Seed pointer into btree node checksum
1262 #define BCACHE_BSET_CSUM 1
1263 #define BCACHE_BSET_KEY_v1 2
1264 #define BCACHE_BSET_JOURNAL_SEQ 3
1265 #define BCACHE_BSET_VERSION 3
1270 * On disk a btree node is a list/log of these; within each set the keys are
1277 * Highest journal entry this bset contains keys for.
1278 * If on recovery we don't see that journal entry, this bset is ignored:
1279 * this allows us to preserve the order of all index updates after a
1280 * crash, since the journal records a total order of all index updates
1281 * and anything that didn't make it to the journal doesn't get used.
1287 __le16 u64s; /* count of d[] in u64s */
1290 struct bkey_packed start[0];
1293 } __attribute__((packed, aligned(8)));
1295 LE32_BITMASK(BSET_CSUM_TYPE, struct bset, flags, 0, 4);
1297 LE32_BITMASK(BSET_BIG_ENDIAN, struct bset, flags, 4, 5);
1298 LE32_BITMASK(BSET_SEPARATE_WHITEOUTS,
1299 struct bset, flags, 5, 6);
1302 struct bch_csum csum;
1305 /* this flags field is encrypted, unlike bset->flags: */
1308 /* Closed interval: */
1309 struct bpos min_key;
1310 struct bpos max_key;
1311 struct bch_extent_ptr ptr;
1312 struct bkey_format format;
1323 } __attribute__((packed, aligned(8)));
1325 LE64_BITMASK(BTREE_NODE_ID, struct btree_node, flags, 0, 4);
1326 LE64_BITMASK(BTREE_NODE_LEVEL, struct btree_node, flags, 4, 8);
1328 struct btree_node_entry {
1329 struct bch_csum csum;
1340 } __attribute__((packed, aligned(8)));
1345 struct bch_csum csum;
1352 __u8 encrypted_start[0];
1356 struct bucket_disk {
1359 } __attribute__((packed)) data[];
1360 } __attribute__((packed, aligned(8)));
1362 LE32_BITMASK(PSET_CSUM_TYPE, struct prio_set, flags, 0, 4);
1364 #define PSET_MAGIC __cpu_to_le64(0x6750e15f87337f91ULL)
1366 static inline __u64 __pset_magic(struct bch_sb *sb)
1368 return __le64_to_cpu(__bch2_sb_magic(sb) ^ PSET_MAGIC);
1371 #endif /* _BCACHEFS_FORMAT_H */