1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_SB_MEMBERS_H
3 #define _BCACHEFS_SB_MEMBERS_H
4
5 #include "darray.h"
6 #include "bkey_types.h"
7 #include "enumerated_ref.h"
8
9 extern char * const bch2_member_error_strs[];
10
11 static inline struct bch_member *
__bch2_members_v2_get_mut(struct bch_sb_field_members_v2 * mi,unsigned i)12 __bch2_members_v2_get_mut(struct bch_sb_field_members_v2 *mi, unsigned i)
13 {
14 return (void *) mi->_members + (i * le16_to_cpu(mi->member_bytes));
15 }
16
17 int bch2_sb_members_v2_init(struct bch_fs *c);
18 int bch2_sb_members_cpy_v2_v1(struct bch_sb_handle *disk_sb);
19 struct bch_member *bch2_members_v2_get_mut(struct bch_sb *sb, int i);
20 struct bch_member bch2_sb_member_get(struct bch_sb *sb, int i);
21
bch2_dev_is_online(struct bch_dev * ca)22 static inline bool bch2_dev_is_online(struct bch_dev *ca)
23 {
24 return !enumerated_ref_is_zero(&ca->io_ref[READ]);
25 }
26
27 static inline struct bch_dev *bch2_dev_rcu(struct bch_fs *, unsigned);
28
bch2_dev_idx_is_online(struct bch_fs * c,unsigned dev)29 static inline bool bch2_dev_idx_is_online(struct bch_fs *c, unsigned dev)
30 {
31 guard(rcu)();
32 struct bch_dev *ca = bch2_dev_rcu(c, dev);
33 return ca && bch2_dev_is_online(ca);
34 }
35
bch2_dev_is_healthy(struct bch_dev * ca)36 static inline bool bch2_dev_is_healthy(struct bch_dev *ca)
37 {
38 return bch2_dev_is_online(ca) &&
39 ca->mi.state != BCH_MEMBER_STATE_failed;
40 }
41
dev_mask_nr(const struct bch_devs_mask * devs)42 static inline unsigned dev_mask_nr(const struct bch_devs_mask *devs)
43 {
44 return bitmap_weight(devs->d, BCH_SB_MEMBERS_MAX);
45 }
46
bch2_dev_list_has_dev(struct bch_devs_list devs,unsigned dev)47 static inline bool bch2_dev_list_has_dev(struct bch_devs_list devs,
48 unsigned dev)
49 {
50 darray_for_each(devs, i)
51 if (*i == dev)
52 return true;
53 return false;
54 }
55
bch2_dev_list_drop_dev(struct bch_devs_list * devs,unsigned dev)56 static inline void bch2_dev_list_drop_dev(struct bch_devs_list *devs,
57 unsigned dev)
58 {
59 darray_for_each(*devs, i)
60 if (*i == dev) {
61 darray_remove_item(devs, i);
62 return;
63 }
64 }
65
bch2_dev_list_add_dev(struct bch_devs_list * devs,unsigned dev)66 static inline void bch2_dev_list_add_dev(struct bch_devs_list *devs,
67 unsigned dev)
68 {
69 if (!bch2_dev_list_has_dev(*devs, dev)) {
70 BUG_ON(devs->nr >= ARRAY_SIZE(devs->data));
71 devs->data[devs->nr++] = dev;
72 }
73 }
74
bch2_dev_list_single(unsigned dev)75 static inline struct bch_devs_list bch2_dev_list_single(unsigned dev)
76 {
77 return (struct bch_devs_list) { .nr = 1, .data[0] = dev };
78 }
79
__bch2_next_dev_idx(struct bch_fs * c,unsigned idx,const struct bch_devs_mask * mask)80 static inline struct bch_dev *__bch2_next_dev_idx(struct bch_fs *c, unsigned idx,
81 const struct bch_devs_mask *mask)
82 {
83 struct bch_dev *ca = NULL;
84
85 while ((idx = mask
86 ? find_next_bit(mask->d, c->sb.nr_devices, idx)
87 : idx) < c->sb.nr_devices &&
88 !(ca = rcu_dereference_check(c->devs[idx],
89 lockdep_is_held(&c->state_lock))))
90 idx++;
91
92 return ca;
93 }
94
__bch2_next_dev(struct bch_fs * c,struct bch_dev * ca,const struct bch_devs_mask * mask)95 static inline struct bch_dev *__bch2_next_dev(struct bch_fs *c, struct bch_dev *ca,
96 const struct bch_devs_mask *mask)
97 {
98 return __bch2_next_dev_idx(c, ca ? ca->dev_idx + 1 : 0, mask);
99 }
100
101 #define for_each_member_device_rcu(_c, _ca, _mask) \
102 for (struct bch_dev *_ca = NULL; \
103 (_ca = __bch2_next_dev((_c), _ca, (_mask)));)
104
105 #define for_each_online_member_rcu(_c, _ca) \
106 for_each_member_device_rcu(_c, _ca, &(_c)->online_devs)
107
108 #define for_each_rw_member_rcu(_c, _ca) \
109 for_each_member_device_rcu(_c, _ca, &(_c)->rw_devs[BCH_DATA_free])
110
bch2_dev_get(struct bch_dev * ca)111 static inline void bch2_dev_get(struct bch_dev *ca)
112 {
113 #ifdef CONFIG_BCACHEFS_DEBUG
114 BUG_ON(atomic_long_inc_return(&ca->ref) <= 1L);
115 #else
116 percpu_ref_get(&ca->ref);
117 #endif
118 }
119
__bch2_dev_put(struct bch_dev * ca)120 static inline void __bch2_dev_put(struct bch_dev *ca)
121 {
122 #ifdef CONFIG_BCACHEFS_DEBUG
123 long r = atomic_long_dec_return(&ca->ref);
124 if (r < (long) !ca->dying)
125 panic("bch_dev->ref underflow, last put: %pS\n", (void *) ca->last_put);
126 ca->last_put = _THIS_IP_;
127 if (!r)
128 complete(&ca->ref_completion);
129 #else
130 percpu_ref_put(&ca->ref);
131 #endif
132 }
133
bch2_dev_put(struct bch_dev * ca)134 static inline void bch2_dev_put(struct bch_dev *ca)
135 {
136 if (ca)
137 __bch2_dev_put(ca);
138 }
139
bch2_get_next_dev(struct bch_fs * c,struct bch_dev * ca)140 static inline struct bch_dev *bch2_get_next_dev(struct bch_fs *c, struct bch_dev *ca)
141 {
142 guard(rcu)();
143 bch2_dev_put(ca);
144 if ((ca = __bch2_next_dev(c, ca, NULL)))
145 bch2_dev_get(ca);
146 return ca;
147 }
148
149 /*
150 * If you break early, you must drop your ref on the current device
151 */
152 #define __for_each_member_device(_c, _ca) \
153 for (; (_ca = bch2_get_next_dev(_c, _ca));)
154
155 #define for_each_member_device(_c, _ca) \
156 for (struct bch_dev *_ca = NULL; \
157 (_ca = bch2_get_next_dev(_c, _ca));)
158
bch2_get_next_online_dev(struct bch_fs * c,struct bch_dev * ca,unsigned state_mask,int rw,unsigned ref_idx)159 static inline struct bch_dev *bch2_get_next_online_dev(struct bch_fs *c,
160 struct bch_dev *ca,
161 unsigned state_mask,
162 int rw, unsigned ref_idx)
163 {
164 guard(rcu)();
165 if (ca)
166 enumerated_ref_put(&ca->io_ref[rw], ref_idx);
167
168 while ((ca = __bch2_next_dev(c, ca, NULL)) &&
169 (!((1 << ca->mi.state) & state_mask) ||
170 !enumerated_ref_tryget(&ca->io_ref[rw], ref_idx)))
171 ;
172
173 return ca;
174 }
175
176 #define __for_each_online_member(_c, _ca, state_mask, rw, ref_idx) \
177 for (struct bch_dev *_ca = NULL; \
178 (_ca = bch2_get_next_online_dev(_c, _ca, state_mask, rw, ref_idx));)
179
180 #define for_each_online_member(c, ca, ref_idx) \
181 __for_each_online_member(c, ca, ~0, READ, ref_idx)
182
183 #define for_each_rw_member(c, ca, ref_idx) \
184 __for_each_online_member(c, ca, BIT(BCH_MEMBER_STATE_rw), WRITE, ref_idx)
185
186 #define for_each_readable_member(c, ca, ref_idx) \
187 __for_each_online_member(c, ca, BIT( BCH_MEMBER_STATE_rw)|BIT(BCH_MEMBER_STATE_ro), READ, ref_idx)
188
bch2_dev_exists(const struct bch_fs * c,unsigned dev)189 static inline bool bch2_dev_exists(const struct bch_fs *c, unsigned dev)
190 {
191 return dev < c->sb.nr_devices && c->devs[dev];
192 }
193
bucket_valid(const struct bch_dev * ca,u64 b)194 static inline bool bucket_valid(const struct bch_dev *ca, u64 b)
195 {
196 return b - ca->mi.first_bucket < ca->mi.nbuckets_minus_first;
197 }
198
bch2_dev_have_ref(const struct bch_fs * c,unsigned dev)199 static inline struct bch_dev *bch2_dev_have_ref(const struct bch_fs *c, unsigned dev)
200 {
201 EBUG_ON(!bch2_dev_exists(c, dev));
202
203 return rcu_dereference_check(c->devs[dev], 1);
204 }
205
bch2_dev_locked(struct bch_fs * c,unsigned dev)206 static inline struct bch_dev *bch2_dev_locked(struct bch_fs *c, unsigned dev)
207 {
208 EBUG_ON(!bch2_dev_exists(c, dev));
209
210 return rcu_dereference_protected(c->devs[dev],
211 lockdep_is_held(&c->sb_lock) ||
212 lockdep_is_held(&c->state_lock));
213 }
214
bch2_dev_rcu_noerror(struct bch_fs * c,unsigned dev)215 static inline struct bch_dev *bch2_dev_rcu_noerror(struct bch_fs *c, unsigned dev)
216 {
217 return c && dev < c->sb.nr_devices
218 ? rcu_dereference(c->devs[dev])
219 : NULL;
220 }
221
222 int bch2_dev_missing_bkey(struct bch_fs *, struct bkey_s_c, unsigned);
223
224 void bch2_dev_missing_atomic(struct bch_fs *, unsigned);
225
bch2_dev_rcu(struct bch_fs * c,unsigned dev)226 static inline struct bch_dev *bch2_dev_rcu(struct bch_fs *c, unsigned dev)
227 {
228 struct bch_dev *ca = bch2_dev_rcu_noerror(c, dev);
229 if (unlikely(!ca))
230 bch2_dev_missing_atomic(c, dev);
231 return ca;
232 }
233
bch2_dev_tryget_noerror(struct bch_fs * c,unsigned dev)234 static inline struct bch_dev *bch2_dev_tryget_noerror(struct bch_fs *c, unsigned dev)
235 {
236 guard(rcu)();
237 struct bch_dev *ca = bch2_dev_rcu_noerror(c, dev);
238 if (ca)
239 bch2_dev_get(ca);
240 return ca;
241 }
242
bch2_dev_tryget(struct bch_fs * c,unsigned dev)243 static inline struct bch_dev *bch2_dev_tryget(struct bch_fs *c, unsigned dev)
244 {
245 struct bch_dev *ca = bch2_dev_tryget_noerror(c, dev);
246 if (unlikely(!ca))
247 bch2_dev_missing_atomic(c, dev);
248 return ca;
249 }
250
bch2_dev_bucket_tryget_noerror(struct bch_fs * c,struct bpos bucket)251 static inline struct bch_dev *bch2_dev_bucket_tryget_noerror(struct bch_fs *c, struct bpos bucket)
252 {
253 struct bch_dev *ca = bch2_dev_tryget_noerror(c, bucket.inode);
254 if (ca && unlikely(!bucket_valid(ca, bucket.offset))) {
255 bch2_dev_put(ca);
256 ca = NULL;
257 }
258 return ca;
259 }
260
261 void bch2_dev_bucket_missing(struct bch_dev *, u64);
262
bch2_dev_bucket_tryget(struct bch_fs * c,struct bpos bucket)263 static inline struct bch_dev *bch2_dev_bucket_tryget(struct bch_fs *c, struct bpos bucket)
264 {
265 struct bch_dev *ca = bch2_dev_tryget(c, bucket.inode);
266 if (ca && unlikely(!bucket_valid(ca, bucket.offset))) {
267 bch2_dev_bucket_missing(ca, bucket.offset);
268 bch2_dev_put(ca);
269 ca = NULL;
270 }
271 return ca;
272 }
273
bch2_dev_iterate_noerror(struct bch_fs * c,struct bch_dev * ca,unsigned dev_idx)274 static inline struct bch_dev *bch2_dev_iterate_noerror(struct bch_fs *c, struct bch_dev *ca, unsigned dev_idx)
275 {
276 if (ca && ca->dev_idx == dev_idx)
277 return ca;
278 bch2_dev_put(ca);
279 return bch2_dev_tryget_noerror(c, dev_idx);
280 }
281
bch2_dev_iterate(struct bch_fs * c,struct bch_dev * ca,unsigned dev_idx)282 static inline struct bch_dev *bch2_dev_iterate(struct bch_fs *c, struct bch_dev *ca, unsigned dev_idx)
283 {
284 if (ca && ca->dev_idx == dev_idx)
285 return ca;
286 bch2_dev_put(ca);
287 return bch2_dev_tryget(c, dev_idx);
288 }
289
bch2_dev_get_ioref(struct bch_fs * c,unsigned dev,int rw,unsigned ref_idx)290 static inline struct bch_dev *bch2_dev_get_ioref(struct bch_fs *c, unsigned dev,
291 int rw, unsigned ref_idx)
292 {
293 might_sleep();
294
295 guard(rcu)();
296 struct bch_dev *ca = bch2_dev_rcu(c, dev);
297 if (!ca || !enumerated_ref_tryget(&ca->io_ref[rw], ref_idx))
298 return NULL;
299
300 if (ca->mi.state == BCH_MEMBER_STATE_rw ||
301 (ca->mi.state == BCH_MEMBER_STATE_ro && rw == READ))
302 return ca;
303
304 enumerated_ref_put(&ca->io_ref[rw], ref_idx);
305 return NULL;
306 }
307
308 extern const struct bch_sb_field_ops bch_sb_field_ops_members_v1;
309 extern const struct bch_sb_field_ops bch_sb_field_ops_members_v2;
310
bch2_member_alive(struct bch_member * m)311 static inline bool bch2_member_alive(struct bch_member *m)
312 {
313 return !bch2_is_zero(&m->uuid, sizeof(m->uuid)) &&
314 !uuid_equal(&m->uuid, &BCH_SB_MEMBER_DELETED_UUID);
315 }
316
bch2_member_exists(struct bch_sb * sb,unsigned dev)317 static inline bool bch2_member_exists(struct bch_sb *sb, unsigned dev)
318 {
319 if (dev < sb->nr_devices) {
320 struct bch_member m = bch2_sb_member_get(sb, dev);
321 return bch2_member_alive(&m);
322 }
323 return false;
324 }
325
326 unsigned bch2_sb_nr_devices(const struct bch_sb *);
327
bch2_mi_to_cpu(struct bch_member * mi)328 static inline struct bch_member_cpu bch2_mi_to_cpu(struct bch_member *mi)
329 {
330 return (struct bch_member_cpu) {
331 .nbuckets = le64_to_cpu(mi->nbuckets),
332 .nbuckets_minus_first = le64_to_cpu(mi->nbuckets) -
333 le16_to_cpu(mi->first_bucket),
334 .first_bucket = le16_to_cpu(mi->first_bucket),
335 .bucket_size = le16_to_cpu(mi->bucket_size),
336 .group = BCH_MEMBER_GROUP(mi),
337 .state = BCH_MEMBER_STATE(mi),
338 .discard = BCH_MEMBER_DISCARD(mi),
339 .data_allowed = BCH_MEMBER_DATA_ALLOWED(mi),
340 .durability = BCH_MEMBER_DURABILITY(mi)
341 ? BCH_MEMBER_DURABILITY(mi) - 1
342 : 1,
343 .freespace_initialized = BCH_MEMBER_FREESPACE_INITIALIZED(mi),
344 .resize_on_mount = BCH_MEMBER_RESIZE_ON_MOUNT(mi),
345 .valid = bch2_member_alive(mi),
346 .btree_bitmap_shift = mi->btree_bitmap_shift,
347 .btree_allocated_bitmap = le64_to_cpu(mi->btree_allocated_bitmap),
348 };
349 }
350
351 void bch2_sb_members_from_cpu(struct bch_fs *);
352
353 void bch2_dev_io_errors_to_text(struct printbuf *, struct bch_dev *);
354 void bch2_dev_errors_reset(struct bch_dev *);
355
bch2_dev_btree_bitmap_marked_sectors(struct bch_dev * ca,u64 start,unsigned sectors)356 static inline bool bch2_dev_btree_bitmap_marked_sectors(struct bch_dev *ca, u64 start, unsigned sectors)
357 {
358 u64 end = start + sectors;
359
360 if (end > 64ULL << ca->mi.btree_bitmap_shift)
361 return false;
362
363 for (unsigned bit = start >> ca->mi.btree_bitmap_shift;
364 (u64) bit << ca->mi.btree_bitmap_shift < end;
365 bit++)
366 if (!(ca->mi.btree_allocated_bitmap & BIT_ULL(bit)))
367 return false;
368 return true;
369 }
370
371 bool bch2_dev_btree_bitmap_marked(struct bch_fs *, struct bkey_s_c);
372 void bch2_dev_btree_bitmap_mark(struct bch_fs *, struct bkey_s_c);
373
374 int bch2_sb_member_alloc(struct bch_fs *);
375 void bch2_sb_members_clean_deleted(struct bch_fs *);
376
377 #endif /* _BCACHEFS_SB_MEMBERS_H */
378