]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/super-io.h
Update bcachefs sources to 9ceb982d77 bcachefs: Store bucket gens in a btree
[bcachefs-tools-debian] / libbcachefs / super-io.h
1 #ifndef _BCACHE_SUPER_IO_H
2 #define _BCACHE_SUPER_IO_H
3
4 #include "extents.h"
5 #include "eytzinger.h"
6 #include "super_types.h"
7
8 #include <asm/byteorder.h>
9
10 struct bch_sb_field *bch2_sb_field_get(struct bch_sb *, enum bch_sb_field_type);
11 struct bch_sb_field *bch2_sb_field_resize(struct bcache_superblock *,
12                                          enum bch_sb_field_type, unsigned);
13 struct bch_sb_field *bch2_fs_sb_field_resize(struct bch_fs *,
14                                          enum bch_sb_field_type, unsigned);
15
16 #define field_to_type(_f, _name)                                        \
17         container_of_or_null(_f, struct bch_sb_field_##_name, field)
18
19 #define BCH_SB_FIELD_TYPE(_name)                                        \
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 bcache_superblock *sb, unsigned u64s)     \
29 {                                                                       \
30         return field_to_type(bch2_sb_field_resize(sb,                   \
31                                 BCH_SB_FIELD_##_name, u64s), _name);    \
32 }                                                                       \
33                                                                         \
34 static inline struct bch_sb_field_##_name *                             \
35 bch2_fs_sb_resize_##_name(struct bch_fs *c, unsigned u64s)              \
36 {                                                                       \
37         return field_to_type(bch2_fs_sb_field_resize(c,                 \
38                                 BCH_SB_FIELD_##_name, u64s), _name);    \
39 }
40
41 BCH_SB_FIELD_TYPE(journal);
42 BCH_SB_FIELD_TYPE(members);
43 BCH_SB_FIELD_TYPE(crypt);
44 BCH_SB_FIELD_TYPE(replicas);
45
46 static inline bool bch2_dev_exists(struct bch_sb *sb,
47                                    struct bch_sb_field_members *mi,
48                                    unsigned dev)
49 {
50         return dev < sb->nr_devices &&
51                 !bch2_is_zero(mi->members[dev].uuid.b, sizeof(uuid_le));
52 }
53
54 static inline bool bch2_sb_test_feature(struct bch_sb *sb,
55                                         enum bch_sb_features f)
56 {
57         unsigned w = f / 64;
58         unsigned b = f % 64;
59
60         return le64_to_cpu(sb->features[w]) & (1ULL << b);
61 }
62
63 static inline void bch2_sb_set_feature(struct bch_sb *sb,
64                                        enum bch_sb_features f)
65 {
66         if (!bch2_sb_test_feature(sb, f)) {
67                 unsigned w = f / 64;
68                 unsigned b = f % 64;
69
70                 le64_add_cpu(&sb->features[w], 1ULL << b);
71         }
72 }
73
74 static inline __le64 bch2_sb_magic(struct bch_fs *c)
75 {
76         __le64 ret;
77         memcpy(&ret, &c->sb.uuid, sizeof(ret));
78         return ret;
79 }
80
81 static inline __u64 jset_magic(struct bch_fs *c)
82 {
83         return __le64_to_cpu(bch2_sb_magic(c) ^ JSET_MAGIC);
84 }
85
86 static inline __u64 pset_magic(struct bch_fs *c)
87 {
88         return __le64_to_cpu(bch2_sb_magic(c) ^ PSET_MAGIC);
89 }
90
91 static inline __u64 bset_magic(struct bch_fs *c)
92 {
93         return __le64_to_cpu(bch2_sb_magic(c) ^ BSET_MAGIC);
94 }
95
96 static inline struct bch_member_cpu bch2_mi_to_cpu(struct bch_member *mi)
97 {
98         return (struct bch_member_cpu) {
99                 .nbuckets       = le64_to_cpu(mi->nbuckets),
100                 .first_bucket   = le16_to_cpu(mi->first_bucket),
101                 .bucket_size    = le16_to_cpu(mi->bucket_size),
102                 .state          = BCH_MEMBER_STATE(mi),
103                 .tier           = BCH_MEMBER_TIER(mi),
104                 .replacement    = BCH_MEMBER_REPLACEMENT(mi),
105                 .discard        = BCH_MEMBER_DISCARD(mi),
106                 .valid          = !bch2_is_zero(mi->uuid.b, sizeof(uuid_le)),
107         };
108 }
109
110 int bch2_sb_to_fs(struct bch_fs *, struct bch_sb *);
111 int bch2_sb_from_fs(struct bch_fs *, struct bch_dev *);
112
113 void bch2_free_super(struct bcache_superblock *);
114 int bch2_super_realloc(struct bcache_superblock *, unsigned);
115
116 const char *bch2_sb_validate_journal(struct bch_sb *,
117                                          struct bch_member_cpu);
118 const char *bch2_sb_validate(struct bcache_superblock *);
119
120 const char *bch2_read_super(struct bcache_superblock *,
121                            struct bch_opts, const char *);
122 void bch2_write_super(struct bch_fs *);
123
124 static inline bool replicas_test_dev(struct bch_replicas_cpu_entry *e,
125                                      unsigned dev)
126 {
127         return (e->devs[dev >> 3] & (1 << (dev & 7))) != 0;
128 }
129
130 static inline void replicas_set_dev(struct bch_replicas_cpu_entry *e,
131                                     unsigned dev)
132 {
133         e->devs[dev >> 3] |= 1 << (dev & 7);
134 }
135
136 static inline unsigned replicas_dev_slots(struct bch_replicas_cpu *r)
137 {
138         return (r->entry_size -
139                 offsetof(struct bch_replicas_cpu_entry, devs)) * 8;
140 }
141
142 static inline struct bch_replicas_cpu_entry *
143 cpu_replicas_entry(struct bch_replicas_cpu *r, unsigned i)
144 {
145         return (void *) r->entries + r->entry_size * i;
146 }
147
148 int bch2_check_mark_super_slowpath(struct bch_fs *, struct bkey_s_c_extent,
149                                    enum bch_data_types);
150
151 static inline bool replicas_has_extent(struct bch_replicas_cpu *r,
152                                        struct bkey_s_c_extent e,
153                                        enum bch_data_types data_type)
154 {
155         const struct bch_extent_ptr *ptr;
156         struct bch_replicas_cpu_entry search = {
157                 .data_type = data_type,
158         };
159         unsigned max_dev = 0;
160
161         BUG_ON(!data_type ||
162                data_type == BCH_DATA_SB ||
163                data_type >= BCH_DATA_NR);
164
165         extent_for_each_ptr(e, ptr)
166                 if (!ptr->cached) {
167                         max_dev = max_t(unsigned, max_dev, ptr->dev);
168                         replicas_set_dev(&search, ptr->dev);
169                 }
170
171         return max_dev < replicas_dev_slots(r) &&
172                 eytzinger0_find(r->entries, r->nr,
173                                 r->entry_size,
174                                 memcmp, &search) < r->nr;
175 }
176
177 static inline bool bch2_sb_has_replicas(struct bch_fs *c,
178                                         struct bkey_s_c_extent e,
179                                         enum bch_data_types data_type)
180 {
181         bool ret;
182
183         rcu_read_lock();
184         ret = replicas_has_extent(rcu_dereference(c->replicas),
185                                   e, data_type);
186         rcu_read_unlock();
187
188         return ret;
189 }
190
191 static inline int bch2_check_mark_super(struct bch_fs *c,
192                                         struct bkey_s_c_extent e,
193                                         enum bch_data_types data_type)
194 {
195         struct bch_replicas_cpu *gc_r;
196         bool marked;
197
198         rcu_read_lock();
199         marked = replicas_has_extent(rcu_dereference(c->replicas),
200                                      e, data_type) &&
201                 (!(gc_r = rcu_dereference(c->replicas_gc)) ||
202                  replicas_has_extent(gc_r, e, data_type));
203         rcu_read_unlock();
204
205         if (marked)
206                 return 0;
207
208         return bch2_check_mark_super_slowpath(c, e, data_type);
209 }
210
211 struct replicas_status {
212         struct {
213                 unsigned        nr_online;
214                 unsigned        nr_offline;
215         }                       replicas[BCH_DATA_NR];
216 };
217
218 struct replicas_status __bch2_replicas_status(struct bch_fs *,
219                                               struct bch_dev *);
220 struct replicas_status bch2_replicas_status(struct bch_fs *);
221
222 unsigned bch2_replicas_online(struct bch_fs *, bool);
223 unsigned bch2_dev_has_data(struct bch_fs *, struct bch_dev *);
224
225 int bch2_replicas_gc_end(struct bch_fs *, int);
226 int bch2_replicas_gc_start(struct bch_fs *, unsigned);
227
228 #endif /* _BCACHE_SUPER_IO_H */