1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_SUPER_IO_H
3 #define _BCACHEFS_SUPER_IO_H
7 #include "super_types.h"
9 #include "sb-members.h"
11 #include <asm/byteorder.h>
13 static inline bool bch2_version_compatible(u16 version)
15 return BCH_VERSION_MAJOR(version) <= BCH_VERSION_MAJOR(bcachefs_metadata_version_current) &&
16 version >= bcachefs_metadata_version_min;
19 void bch2_version_to_text(struct printbuf *, unsigned);
20 unsigned bch2_latest_compatible_version(unsigned);
22 static inline size_t bch2_sb_field_bytes(struct bch_sb_field *f)
24 return le32_to_cpu(f->u64s) * sizeof(u64);
27 #define field_to_type(_f, _name) \
28 container_of_or_null(_f, struct bch_sb_field_##_name, field)
30 struct bch_sb_field *bch2_sb_field_get_id(struct bch_sb *, enum bch_sb_field_type);
31 #define bch2_sb_field_get(_sb, _name) \
32 field_to_type(bch2_sb_field_get_id(_sb, BCH_SB_FIELD_##_name), _name)
34 struct bch_sb_field *bch2_sb_field_resize_id(struct bch_sb_handle *,
35 enum bch_sb_field_type, unsigned);
36 #define bch2_sb_field_resize(_sb, _name, _u64s) \
37 field_to_type(bch2_sb_field_resize_id(_sb, BCH_SB_FIELD_##_name, _u64s), _name)
39 struct bch_sb_field *bch2_sb_field_get_minsize_id(struct bch_sb_handle *,
40 enum bch_sb_field_type, unsigned);
41 #define bch2_sb_field_get_minsize(_sb, _name, _u64s) \
42 field_to_type(bch2_sb_field_get_minsize_id(_sb, BCH_SB_FIELD_##_name, _u64s), _name)
44 #define bch2_sb_field_nr_entries(_f) \
45 (_f ? ((bch2_sb_field_bytes(&_f->field) - sizeof(*_f)) / \
46 sizeof(_f->entries[0])) \
49 void bch2_sb_field_delete(struct bch_sb_handle *, enum bch_sb_field_type);
51 extern const char * const bch2_sb_fields[];
53 struct bch_sb_field_ops {
54 int (*validate)(struct bch_sb *, struct bch_sb_field *, struct printbuf *);
55 void (*to_text)(struct printbuf *, struct bch_sb *, struct bch_sb_field *);
58 static inline __le64 bch2_sb_magic(struct bch_fs *c)
62 memcpy(&ret, &c->sb.uuid, sizeof(ret));
66 static inline __u64 jset_magic(struct bch_fs *c)
68 return __le64_to_cpu(bch2_sb_magic(c) ^ JSET_MAGIC);
71 static inline __u64 bset_magic(struct bch_fs *c)
73 return __le64_to_cpu(bch2_sb_magic(c) ^ BSET_MAGIC);
76 int bch2_sb_to_fs(struct bch_fs *, struct bch_sb *);
77 int bch2_sb_from_fs(struct bch_fs *, struct bch_dev *);
79 void bch2_free_super(struct bch_sb_handle *);
80 int bch2_sb_realloc(struct bch_sb_handle *, unsigned);
82 int bch2_read_super(const char *, struct bch_opts *, struct bch_sb_handle *);
83 int bch2_read_super_silent(const char *, struct bch_opts *, struct bch_sb_handle *);
84 int bch2_write_super(struct bch_fs *);
85 void __bch2_check_set_feature(struct bch_fs *, unsigned);
87 static inline void bch2_check_set_feature(struct bch_fs *c, unsigned feat)
89 if (!(c->sb.features & (1ULL << feat)))
90 __bch2_check_set_feature(c, feat);
93 bool bch2_check_version_downgrade(struct bch_fs *);
94 void bch2_sb_upgrade(struct bch_fs *, unsigned);
96 void __bch2_sb_field_to_text(struct printbuf *, struct bch_sb *,
97 struct bch_sb_field *);
98 void bch2_sb_field_to_text(struct printbuf *, struct bch_sb *,
99 struct bch_sb_field *);
100 void bch2_sb_layout_to_text(struct printbuf *, struct bch_sb_layout *);
101 void bch2_sb_to_text(struct printbuf *, struct bch_sb *, bool, unsigned);
103 #endif /* _BCACHEFS_SUPER_IO_H */