1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_SUPER_H
3 #define _BCACHEFS_SUPER_H
7 #include "bcachefs_ioctl.h"
9 #include <linux/math64.h>
11 static inline size_t sector_to_bucket(const struct bch_dev *ca, sector_t s)
13 return div_u64(s, ca->mi.bucket_size);
16 static inline sector_t bucket_to_sector(const struct bch_dev *ca, size_t b)
18 return ((sector_t) b) * ca->mi.bucket_size;
21 static inline sector_t bucket_remainder(const struct bch_dev *ca, sector_t s)
25 div_u64_rem(s, ca->mi.bucket_size, &remainder);
29 static inline bool bch2_dev_is_online(struct bch_dev *ca)
31 return !percpu_ref_is_zero(&ca->io_ref);
34 static inline bool bch2_dev_is_readable(struct bch_dev *ca)
36 return bch2_dev_is_online(ca) &&
37 ca->mi.state != BCH_MEMBER_STATE_failed;
40 static inline bool bch2_dev_get_ioref(struct bch_dev *ca, int rw)
42 if (!percpu_ref_tryget(&ca->io_ref))
45 if (ca->mi.state == BCH_MEMBER_STATE_rw ||
46 (ca->mi.state == BCH_MEMBER_STATE_ro && rw == READ))
49 percpu_ref_put(&ca->io_ref);
53 static inline unsigned dev_mask_nr(const struct bch_devs_mask *devs)
55 return bitmap_weight(devs->d, BCH_SB_MEMBERS_MAX);
58 static inline bool bch2_dev_list_has_dev(struct bch_devs_list devs,
63 for (i = 0; i < devs.nr; i++)
64 if (devs.devs[i] == dev)
70 static inline void bch2_dev_list_drop_dev(struct bch_devs_list *devs,
75 for (i = 0; i < devs->nr; i++)
76 if (devs->devs[i] == dev) {
77 array_remove_item(devs->devs, devs->nr, i);
82 static inline void bch2_dev_list_add_dev(struct bch_devs_list *devs,
85 BUG_ON(bch2_dev_list_has_dev(*devs, dev));
86 BUG_ON(devs->nr >= BCH_REPLICAS_MAX);
87 devs->devs[devs->nr++] = dev;
90 static inline struct bch_devs_list bch2_dev_list_single(unsigned dev)
92 return (struct bch_devs_list) { .nr = 1, .devs[0] = dev };
95 static inline struct bch_dev *__bch2_next_dev(struct bch_fs *c, unsigned *iter,
96 const struct bch_devs_mask *mask)
98 struct bch_dev *ca = NULL;
101 ? find_next_bit(mask->d, c->sb.nr_devices, *iter)
102 : *iter) < c->sb.nr_devices &&
103 !(ca = rcu_dereference_check(c->devs[*iter],
104 lockdep_is_held(&c->state_lock))))
110 #define for_each_member_device_rcu(ca, c, iter, mask) \
111 for ((iter) = 0; ((ca) = __bch2_next_dev((c), &(iter), mask)); (iter)++)
113 static inline struct bch_dev *bch2_get_next_dev(struct bch_fs *c, unsigned *iter)
118 if ((ca = __bch2_next_dev(c, iter, NULL)))
119 percpu_ref_get(&ca->ref);
126 * If you break early, you must drop your ref on the current device
128 #define for_each_member_device(ca, c, iter) \
130 (ca = bch2_get_next_dev(c, &(iter))); \
131 percpu_ref_put(&ca->ref), (iter)++)
133 static inline struct bch_dev *bch2_get_next_online_dev(struct bch_fs *c,
140 while ((ca = __bch2_next_dev(c, iter, NULL)) &&
141 (!((1 << ca->mi.state) & state_mask) ||
142 !percpu_ref_tryget(&ca->io_ref)))
149 #define __for_each_online_member(ca, c, iter, state_mask) \
151 (ca = bch2_get_next_online_dev(c, &(iter), state_mask)); \
152 percpu_ref_put(&ca->io_ref), (iter)++)
154 #define for_each_online_member(ca, c, iter) \
155 __for_each_online_member(ca, c, iter, ~0)
157 #define for_each_rw_member(ca, c, iter) \
158 __for_each_online_member(ca, c, iter, 1 << BCH_MEMBER_STATE_rw)
160 #define for_each_readable_member(ca, c, iter) \
161 __for_each_online_member(ca, c, iter, \
162 (1 << BCH_MEMBER_STATE_rw)|(1 << BCH_MEMBER_STATE_ro))
165 * If a key exists that references a device, the device won't be going away and
166 * we can omit rcu_read_lock():
168 static inline struct bch_dev *bch_dev_bkey_exists(const struct bch_fs *c, unsigned idx)
170 EBUG_ON(idx >= c->sb.nr_devices || !c->devs[idx]);
172 return rcu_dereference_check(c->devs[idx], 1);
175 static inline struct bch_dev *bch_dev_locked(struct bch_fs *c, unsigned idx)
177 EBUG_ON(idx >= c->sb.nr_devices || !c->devs[idx]);
179 return rcu_dereference_protected(c->devs[idx],
180 lockdep_is_held(&c->sb_lock) ||
181 lockdep_is_held(&c->state_lock));
184 /* XXX kill, move to struct bch_fs */
185 static inline struct bch_devs_mask bch2_online_devs(struct bch_fs *c)
187 struct bch_devs_mask devs;
191 memset(&devs, 0, sizeof(devs));
192 for_each_online_member(ca, c, i)
193 __set_bit(ca->dev_idx, devs.d);
197 static inline bool is_superblock_bucket(struct bch_dev *ca, u64 b)
199 struct bch_sb_layout *layout = &ca->disk_sb.sb->layout;
200 u64 b_offset = bucket_to_sector(ca, b);
201 u64 b_end = bucket_to_sector(ca, b + 1);
207 for (i = 0; i < layout->nr_superblocks; i++) {
208 u64 offset = le64_to_cpu(layout->sb_offset[i]);
209 u64 end = offset + (1 << layout->sb_max_size_bits);
211 if (!(offset >= b_end || end <= b_offset))
218 struct bch_fs *bch2_dev_to_fs(dev_t);
219 struct bch_fs *bch2_uuid_to_fs(uuid_le);
221 bool bch2_dev_state_allowed(struct bch_fs *, struct bch_dev *,
222 enum bch_member_state, int);
223 int __bch2_dev_set_state(struct bch_fs *, struct bch_dev *,
224 enum bch_member_state, int);
225 int bch2_dev_set_state(struct bch_fs *, struct bch_dev *,
226 enum bch_member_state, int);
228 int bch2_dev_fail(struct bch_dev *, int);
229 int bch2_dev_remove(struct bch_fs *, struct bch_dev *, int);
230 int bch2_dev_add(struct bch_fs *, const char *);
231 int bch2_dev_online(struct bch_fs *, const char *);
232 int bch2_dev_offline(struct bch_fs *, struct bch_dev *, int);
233 int bch2_dev_resize(struct bch_fs *, struct bch_dev *, u64);
234 struct bch_dev *bch2_dev_lookup(struct bch_fs *, const char *);
236 bool bch2_fs_emergency_read_only(struct bch_fs *);
237 void bch2_fs_read_only(struct bch_fs *);
239 int bch2_fs_read_write(struct bch_fs *);
240 int bch2_fs_read_write_early(struct bch_fs *);
243 * Only for use in the recovery/fsck path:
245 static inline void bch2_fs_lazy_rw(struct bch_fs *c)
247 if (percpu_ref_is_zero(&c->writes))
248 bch2_fs_read_write_early(c);
251 void __bch2_fs_stop(struct bch_fs *);
252 void bch2_fs_free(struct bch_fs *);
253 void bch2_fs_stop(struct bch_fs *);
255 int bch2_fs_start(struct bch_fs *);
256 struct bch_fs *bch2_fs_open(char * const *, unsigned, struct bch_opts);
258 #endif /* _BCACHEFS_SUPER_H */