]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/super-io.h
Update bcachefs sources to a9f14c773f bcachefs: More btree gc refactorings
[bcachefs-tools-debian] / libbcachefs / super-io.h
1 #ifndef _BCACHEFS_SUPER_IO_H
2 #define _BCACHEFS_SUPER_IO_H
3
4 #include "extents.h"
5 #include "eytzinger.h"
6 #include "super_types.h"
7 #include "super.h"
8
9 #include <asm/byteorder.h>
10
11 struct bch_sb_field *bch2_sb_field_get(struct bch_sb *, enum bch_sb_field_type);
12 struct bch_sb_field *bch2_sb_field_resize(struct bch_sb_handle *,
13                                           enum bch_sb_field_type, unsigned);
14 void bch2_sb_field_delete(struct bch_sb_handle *, enum bch_sb_field_type);
15
16 #define field_to_type(_f, _name)                                        \
17         container_of_or_null(_f, struct bch_sb_field_##_name, field)
18
19 #define x(_name, _nr)                                                   \
20 static inline struct bch_sb_field_##_name *                             \
21 bch2_sb_get_##_name(struct bch_sb *sb)                                  \
22 {                                                                       \
23         return field_to_type(bch2_sb_field_get(sb,                      \
24                                 BCH_SB_FIELD_##_name), _name);          \
25 }                                                                       \
26                                                                         \
27 static inline struct bch_sb_field_##_name *                             \
28 bch2_sb_resize_##_name(struct bch_sb_handle *sb, unsigned u64s) \
29 {                                                                       \
30         return field_to_type(bch2_sb_field_resize(sb,                   \
31                                 BCH_SB_FIELD_##_name, u64s), _name);    \
32 }
33
34 BCH_SB_FIELDS()
35 #undef x
36
37 extern const char * const bch2_sb_fields[];
38
39 struct bch_sb_field_ops {
40         const char *    (*validate)(struct bch_sb *, struct bch_sb_field *);
41         void            (*to_text)(struct printbuf *, struct bch_sb *,
42                                    struct bch_sb_field *);
43 };
44
45 static inline bool bch2_sb_test_feature(struct bch_sb *sb,
46                                         enum bch_sb_features f)
47 {
48         unsigned w = f / 64;
49         unsigned b = f % 64;
50
51         return le64_to_cpu(sb->features[w]) & (1ULL << b);
52 }
53
54 static inline void bch2_sb_set_feature(struct bch_sb *sb,
55                                        enum bch_sb_features f)
56 {
57         if (!bch2_sb_test_feature(sb, f)) {
58                 unsigned w = f / 64;
59                 unsigned b = f % 64;
60
61                 le64_add_cpu(&sb->features[w], 1ULL << b);
62         }
63 }
64
65 static inline __le64 bch2_sb_magic(struct bch_fs *c)
66 {
67         __le64 ret;
68         memcpy(&ret, &c->sb.uuid, sizeof(ret));
69         return ret;
70 }
71
72 static inline __u64 jset_magic(struct bch_fs *c)
73 {
74         return __le64_to_cpu(bch2_sb_magic(c) ^ JSET_MAGIC);
75 }
76
77 static inline __u64 bset_magic(struct bch_fs *c)
78 {
79         return __le64_to_cpu(bch2_sb_magic(c) ^ BSET_MAGIC);
80 }
81
82 int bch2_sb_to_fs(struct bch_fs *, struct bch_sb *);
83 int bch2_sb_from_fs(struct bch_fs *, struct bch_dev *);
84
85 void bch2_free_super(struct bch_sb_handle *);
86 int bch2_sb_realloc(struct bch_sb_handle *, unsigned);
87
88 const char *bch2_sb_validate(struct bch_sb_handle *);
89
90 int bch2_read_super(const char *, struct bch_opts *, struct bch_sb_handle *);
91 void bch2_write_super(struct bch_fs *);
92
93 /* BCH_SB_FIELD_journal: */
94
95 static inline unsigned bch2_nr_journal_buckets(struct bch_sb_field_journal *j)
96 {
97         return j
98                 ? (__le64 *) vstruct_end(&j->field) - j->buckets
99                 : 0;
100 }
101
102 /* BCH_SB_FIELD_members: */
103
104 static inline bool bch2_member_exists(struct bch_member *m)
105 {
106         return !bch2_is_zero(m->uuid.b, sizeof(uuid_le));
107 }
108
109 static inline bool bch2_dev_exists(struct bch_sb *sb,
110                                    struct bch_sb_field_members *mi,
111                                    unsigned dev)
112 {
113         return dev < sb->nr_devices &&
114                 bch2_member_exists(&mi->members[dev]);
115 }
116
117 static inline struct bch_member_cpu bch2_mi_to_cpu(struct bch_member *mi)
118 {
119         return (struct bch_member_cpu) {
120                 .nbuckets       = le64_to_cpu(mi->nbuckets),
121                 .first_bucket   = le16_to_cpu(mi->first_bucket),
122                 .bucket_size    = le16_to_cpu(mi->bucket_size),
123                 .group          = BCH_MEMBER_GROUP(mi),
124                 .state          = BCH_MEMBER_STATE(mi),
125                 .replacement    = BCH_MEMBER_REPLACEMENT(mi),
126                 .discard        = BCH_MEMBER_DISCARD(mi),
127                 .data_allowed   = BCH_MEMBER_DATA_ALLOWED(mi),
128                 .durability     = BCH_MEMBER_DURABILITY(mi)
129                         ? BCH_MEMBER_DURABILITY(mi) - 1
130                         : 1,
131                 .valid          = !bch2_is_zero(mi->uuid.b, sizeof(uuid_le)),
132         };
133 }
134
135 /* BCH_SB_FIELD_clean: */
136
137 void bch2_fs_mark_clean(struct bch_fs *, bool);
138
139 void bch2_sb_field_to_text(struct printbuf *, struct bch_sb *,
140                            struct bch_sb_field *);
141
142 #endif /* _BCACHEFS_SUPER_IO_H */