Commit | Line | Data |
---|---|---|
1c6fdbd8 KO |
1 | /* SPDX-License-Identifier: GPL-2.0 */ |
2 | #ifndef _BCACHEFS_SUPER_IO_H | |
3 | #define _BCACHEFS_SUPER_IO_H | |
4 | ||
5 | #include "extents.h" | |
6 | #include "eytzinger.h" | |
7 | #include "super_types.h" | |
8 | #include "super.h" | |
1241df58 | 9 | #include "sb-members.h" |
1c6fdbd8 KO |
10 | |
11 | #include <asm/byteorder.h> | |
12 | ||
5c3911ac KO |
13 | #define BCH_SB_READ_SCRATCH_BUF_SIZE 4096 |
14 | ||
a02a0121 KO |
15 | static inline bool bch2_version_compatible(u16 version) |
16 | { | |
ba8eeae8 | 17 | return BCH_VERSION_MAJOR(version) <= BCH_VERSION_MAJOR(bcachefs_metadata_version_current) && |
a02a0121 KO |
18 | version >= bcachefs_metadata_version_min; |
19 | } | |
20 | ||
a36d8f0e KO |
21 | void bch2_version_to_text(struct printbuf *, enum bcachefs_metadata_version); |
22 | enum bcachefs_metadata_version bch2_latest_compatible_version(enum bcachefs_metadata_version); | |
23 | ||
6422bf81 | 24 | int bch2_set_version_incompat(struct bch_fs *, enum bcachefs_metadata_version); |
a36d8f0e | 25 | |
6422bf81 KO |
26 | static inline int bch2_request_incompat_feature(struct bch_fs *c, |
27 | enum bcachefs_metadata_version version) | |
a36d8f0e | 28 | { |
4a4f9b5c | 29 | return likely(version <= c->sb.version_incompat) |
6422bf81 | 30 | ? 0 |
4a4f9b5c | 31 | : bch2_set_version_incompat(c, version); |
a36d8f0e | 32 | } |
e3804b55 | 33 | |
f5d26fa3 KO |
34 | static inline size_t bch2_sb_field_bytes(struct bch_sb_field *f) |
35 | { | |
36 | return le32_to_cpu(f->u64s) * sizeof(u64); | |
37 | } | |
38 | ||
1c6fdbd8 KO |
39 | #define field_to_type(_f, _name) \ |
40 | container_of_or_null(_f, struct bch_sb_field_##_name, field) | |
41 | ||
4637429e KO |
42 | struct bch_sb_field *bch2_sb_field_get_id(struct bch_sb *, enum bch_sb_field_type); |
43 | #define bch2_sb_field_get(_sb, _name) \ | |
44 | field_to_type(bch2_sb_field_get_id(_sb, BCH_SB_FIELD_##_name), _name) | |
1c6fdbd8 | 45 | |
4637429e KO |
46 | struct bch_sb_field *bch2_sb_field_resize_id(struct bch_sb_handle *, |
47 | enum bch_sb_field_type, unsigned); | |
48 | #define bch2_sb_field_resize(_sb, _name, _u64s) \ | |
49 | field_to_type(bch2_sb_field_resize_id(_sb, BCH_SB_FIELD_##_name, _u64s), _name) | |
50 | ||
8b16413c KO |
51 | struct bch_sb_field *bch2_sb_field_get_minsize_id(struct bch_sb_handle *, |
52 | enum bch_sb_field_type, unsigned); | |
53 | #define bch2_sb_field_get_minsize(_sb, _name, _u64s) \ | |
54 | field_to_type(bch2_sb_field_get_minsize_id(_sb, BCH_SB_FIELD_##_name, _u64s), _name) | |
55 | ||
56 | #define bch2_sb_field_nr_entries(_f) \ | |
57 | (_f ? ((bch2_sb_field_bytes(&_f->field) - sizeof(*_f)) / \ | |
58 | sizeof(_f->entries[0])) \ | |
59 | : 0) | |
60 | ||
4637429e | 61 | void bch2_sb_field_delete(struct bch_sb_handle *, enum bch_sb_field_type); |
1c6fdbd8 KO |
62 | |
63 | extern const char * const bch2_sb_fields[]; | |
64 | ||
65 | struct bch_sb_field_ops { | |
a5c3e265 KO |
66 | int (*validate)(struct bch_sb *, struct bch_sb_field *, |
67 | enum bch_validate_flags, struct printbuf *); | |
efe68e1d | 68 | void (*to_text)(struct printbuf *, struct bch_sb *, struct bch_sb_field *); |
1c6fdbd8 KO |
69 | }; |
70 | ||
1c6fdbd8 KO |
71 | static inline __le64 bch2_sb_magic(struct bch_fs *c) |
72 | { | |
73 | __le64 ret; | |
1e81f89b | 74 | |
1c6fdbd8 KO |
75 | memcpy(&ret, &c->sb.uuid, sizeof(ret)); |
76 | return ret; | |
77 | } | |
78 | ||
79 | static inline __u64 jset_magic(struct bch_fs *c) | |
80 | { | |
81 | return __le64_to_cpu(bch2_sb_magic(c) ^ JSET_MAGIC); | |
82 | } | |
83 | ||
84 | static inline __u64 bset_magic(struct bch_fs *c) | |
85 | { | |
86 | return __le64_to_cpu(bch2_sb_magic(c) ^ BSET_MAGIC); | |
87 | } | |
88 | ||
89 | int bch2_sb_to_fs(struct bch_fs *, struct bch_sb *); | |
90 | int bch2_sb_from_fs(struct bch_fs *, struct bch_dev *); | |
91 | ||
92 | void bch2_free_super(struct bch_sb_handle *); | |
93 | int bch2_sb_realloc(struct bch_sb_handle *, unsigned); | |
94 | ||
92c7789a | 95 | int bch2_sb_validate(struct bch_sb *, u64, enum bch_validate_flags, struct printbuf *); |
8bd875ae | 96 | |
1c6fdbd8 | 97 | int bch2_read_super(const char *, struct bch_opts *, struct bch_sb_handle *); |
2b161cc7 | 98 | int bch2_read_super_silent(const char *, struct bch_opts *, struct bch_sb_handle *); |
03e183cb | 99 | int bch2_write_super(struct bch_fs *); |
ba239c95 KO |
100 | void __bch2_check_set_feature(struct bch_fs *, unsigned); |
101 | ||
102 | static inline void bch2_check_set_feature(struct bch_fs *c, unsigned feat) | |
103 | { | |
104 | if (!(c->sb.features & (1ULL << feat))) | |
105 | __bch2_check_set_feature(c, feat); | |
106 | } | |
1c6fdbd8 | 107 | |
84f16387 | 108 | bool bch2_check_version_downgrade(struct bch_fs *); |
a36d8f0e | 109 | void bch2_sb_upgrade(struct bch_fs *, unsigned, bool); |
5022d0e1 | 110 | void bch2_sb_upgrade_incompat(struct bch_fs *); |
6619d846 | 111 | |
5b883656 KO |
112 | void __bch2_sb_field_to_text(struct printbuf *, struct bch_sb *, |
113 | struct bch_sb_field *); | |
319f9ac3 KO |
114 | void bch2_sb_field_to_text(struct printbuf *, struct bch_sb *, |
115 | struct bch_sb_field *); | |
12bf93a4 KO |
116 | void bch2_sb_layout_to_text(struct printbuf *, struct bch_sb_layout *); |
117 | void bch2_sb_to_text(struct printbuf *, struct bch_sb *, bool, unsigned); | |
1c6fdbd8 KO |
118 | |
119 | #endif /* _BCACHEFS_SUPER_IO_H */ |