]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/super-io.h
Update bcachefs sources to 717b356d1d bcachefs: Convert journal validation to bkey_in...
[bcachefs-tools-debian] / libbcachefs / super-io.h
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"
9
10 #include <asm/byteorder.h>
11
12 static inline bool bch2_version_compatible(u16 version)
13 {
14         return BCH_VERSION_MAJOR(version) <= BCH_VERSION_MAJOR(bcachefs_metadata_version_current) &&
15                 version >= bcachefs_metadata_version_min;
16 }
17
18 void bch2_version_to_text(struct printbuf *, unsigned);
19 unsigned bch2_latest_compatible_version(unsigned);
20
21 u64 bch2_upgrade_recovery_passes(struct bch_fs *c,
22                                  unsigned,
23                                  unsigned);
24
25 struct bch_sb_field *bch2_sb_field_get(struct bch_sb *, enum bch_sb_field_type);
26 struct bch_sb_field *bch2_sb_field_resize(struct bch_sb_handle *,
27                                           enum bch_sb_field_type, unsigned);
28 void bch2_sb_field_delete(struct bch_sb_handle *, enum bch_sb_field_type);
29
30 #define field_to_type(_f, _name)                                        \
31         container_of_or_null(_f, struct bch_sb_field_##_name, field)
32
33 #define x(_name, _nr)                                                   \
34 static inline struct bch_sb_field_##_name *                             \
35 bch2_sb_get_##_name(struct bch_sb *sb)                                  \
36 {                                                                       \
37         return field_to_type(bch2_sb_field_get(sb,                      \
38                                 BCH_SB_FIELD_##_name), _name);          \
39 }                                                                       \
40                                                                         \
41 static inline struct bch_sb_field_##_name *                             \
42 bch2_sb_resize_##_name(struct bch_sb_handle *sb, unsigned u64s) \
43 {                                                                       \
44         return field_to_type(bch2_sb_field_resize(sb,                   \
45                                 BCH_SB_FIELD_##_name, u64s), _name);    \
46 }
47
48 BCH_SB_FIELDS()
49 #undef x
50
51 extern const char * const bch2_sb_fields[];
52
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 *);
56 };
57
58 static inline __le64 bch2_sb_magic(struct bch_fs *c)
59 {
60         __le64 ret;
61         memcpy(&ret, &c->sb.uuid, sizeof(ret));
62         return ret;
63 }
64
65 static inline __u64 jset_magic(struct bch_fs *c)
66 {
67         return __le64_to_cpu(bch2_sb_magic(c) ^ JSET_MAGIC);
68 }
69
70 static inline __u64 bset_magic(struct bch_fs *c)
71 {
72         return __le64_to_cpu(bch2_sb_magic(c) ^ BSET_MAGIC);
73 }
74
75 int bch2_sb_to_fs(struct bch_fs *, struct bch_sb *);
76 int bch2_sb_from_fs(struct bch_fs *, struct bch_dev *);
77
78 void bch2_free_super(struct bch_sb_handle *);
79 int bch2_sb_realloc(struct bch_sb_handle *, unsigned);
80
81 int bch2_read_super(const char *, struct bch_opts *, struct bch_sb_handle *);
82 int bch2_write_super(struct bch_fs *);
83 void __bch2_check_set_feature(struct bch_fs *, unsigned);
84
85 static inline void bch2_check_set_feature(struct bch_fs *c, unsigned feat)
86 {
87         if (!(c->sb.features & (1ULL << feat)))
88                 __bch2_check_set_feature(c, feat);
89 }
90
91 /* BCH_SB_FIELD_members: */
92
93 static inline bool bch2_member_exists(struct bch_member *m)
94 {
95         return !bch2_is_zero(&m->uuid, sizeof(m->uuid));
96 }
97
98 static inline bool bch2_dev_exists(struct bch_sb *sb,
99                                    struct bch_sb_field_members *mi,
100                                    unsigned dev)
101 {
102         return dev < sb->nr_devices &&
103                 bch2_member_exists(&mi->members[dev]);
104 }
105
106 static inline struct bch_member_cpu bch2_mi_to_cpu(struct bch_member *mi)
107 {
108         return (struct bch_member_cpu) {
109                 .nbuckets       = le64_to_cpu(mi->nbuckets),
110                 .first_bucket   = le16_to_cpu(mi->first_bucket),
111                 .bucket_size    = le16_to_cpu(mi->bucket_size),
112                 .group          = BCH_MEMBER_GROUP(mi),
113                 .state          = BCH_MEMBER_STATE(mi),
114                 .discard        = BCH_MEMBER_DISCARD(mi),
115                 .data_allowed   = BCH_MEMBER_DATA_ALLOWED(mi),
116                 .durability     = BCH_MEMBER_DURABILITY(mi)
117                         ? BCH_MEMBER_DURABILITY(mi) - 1
118                         : 1,
119                 .freespace_initialized = BCH_MEMBER_FREESPACE_INITIALIZED(mi),
120                 .valid          = bch2_member_exists(mi),
121         };
122 }
123
124 void bch2_sb_maybe_downgrade(struct bch_fs *);
125 void bch2_sb_upgrade(struct bch_fs *, unsigned);
126
127 void bch2_sb_field_to_text(struct printbuf *, struct bch_sb *,
128                            struct bch_sb_field *);
129 void bch2_sb_layout_to_text(struct printbuf *, struct bch_sb_layout *);
130 void bch2_sb_to_text(struct printbuf *, struct bch_sb *, bool, unsigned);
131
132 #endif /* _BCACHEFS_SUPER_IO_H */