1 #ifndef _BCACHEFS_SUPER_H
2 #define _BCACHEFS_SUPER_H
6 #include "bcachefs_ioctl.h"
8 #include <linux/math64.h>
10 static inline size_t sector_to_bucket(const struct bch_dev *ca, sector_t s)
12 return div_u64(s, ca->mi.bucket_size);
15 static inline sector_t bucket_to_sector(const struct bch_dev *ca, size_t b)
17 return ((sector_t) b) * ca->mi.bucket_size;
20 static inline sector_t bucket_remainder(const struct bch_dev *ca, sector_t s)
24 div_u64_rem(s, ca->mi.bucket_size, &remainder);
28 static inline bool bch2_dev_is_online(struct bch_dev *ca)
30 return ca->disk_sb.bdev != NULL;
33 static inline unsigned dev_mask_nr(const struct bch_devs_mask *devs)
35 return bitmap_weight(devs->d, BCH_SB_MEMBERS_MAX);
38 static inline bool bch2_dev_list_has_dev(struct bch_devs_list devs,
43 for (i = 0; i < devs.nr; i++)
44 if (devs.devs[i] == dev)
50 static inline void bch2_dev_list_drop_dev(struct bch_devs_list *devs,
55 for (i = 0; i < devs->nr; i++)
56 if (devs->devs[i] == dev) {
57 array_remove_item(devs->devs, devs->nr, i);
62 static inline void bch2_dev_list_add_dev(struct bch_devs_list *devs,
65 BUG_ON(bch2_dev_list_has_dev(*devs, dev));
66 BUG_ON(devs->nr >= BCH_REPLICAS_MAX);
67 devs->devs[devs->nr++] = dev;
70 static inline struct bch_dev *__bch2_next_dev(struct bch_fs *c, unsigned *iter,
71 const struct bch_devs_mask *mask)
73 struct bch_dev *ca = NULL;
76 ? find_next_bit(mask->d, c->sb.nr_devices, *iter)
77 : *iter) < c->sb.nr_devices &&
78 !(ca = rcu_dereference_check(c->devs[*iter],
79 lockdep_is_held(&c->state_lock))))
85 #define __for_each_member_device(ca, c, iter, mask) \
86 for ((iter) = 0; ((ca) = __bch2_next_dev((c), &(iter), mask)); (iter)++)
88 #define for_each_member_device_rcu(ca, c, iter, mask) \
89 __for_each_member_device(ca, c, iter, mask)
91 static inline struct bch_dev *bch2_get_next_dev(struct bch_fs *c, unsigned *iter)
96 if ((ca = __bch2_next_dev(c, iter, NULL)))
97 percpu_ref_get(&ca->ref);
104 * If you break early, you must drop your ref on the current device
106 #define for_each_member_device(ca, c, iter) \
108 (ca = bch2_get_next_dev(c, &(iter))); \
109 percpu_ref_put(&ca->ref), (iter)++)
111 static inline struct bch_dev *bch2_get_next_online_dev(struct bch_fs *c,
118 while ((ca = __bch2_next_dev(c, iter, NULL)) &&
119 (!((1 << ca->mi.state) & state_mask) ||
120 !percpu_ref_tryget(&ca->io_ref)))
127 #define __for_each_online_member(ca, c, iter, state_mask) \
129 (ca = bch2_get_next_online_dev(c, &(iter), state_mask)); \
130 percpu_ref_put(&ca->io_ref), (iter)++)
132 #define for_each_online_member(ca, c, iter) \
133 __for_each_online_member(ca, c, iter, ~0)
135 #define for_each_rw_member(ca, c, iter) \
136 __for_each_online_member(ca, c, iter, 1 << BCH_MEMBER_STATE_RW)
138 #define for_each_readable_member(ca, c, iter) \
139 __for_each_online_member(ca, c, iter, \
140 (1 << BCH_MEMBER_STATE_RW)|(1 << BCH_MEMBER_STATE_RO))
143 * If a key exists that references a device, the device won't be going away and
144 * we can omit rcu_read_lock():
146 static inline struct bch_dev *bch_dev_bkey_exists(const struct bch_fs *c, unsigned idx)
148 EBUG_ON(idx >= c->sb.nr_devices || !c->devs[idx]);
150 return rcu_dereference_check(c->devs[idx], 1);
153 static inline struct bch_dev *bch_dev_locked(struct bch_fs *c, unsigned idx)
155 EBUG_ON(idx >= c->sb.nr_devices || !c->devs[idx]);
157 return rcu_dereference_protected(c->devs[idx],
158 lockdep_is_held(&c->sb_lock) ||
159 lockdep_is_held(&c->state_lock));
162 /* XXX kill, move to struct bch_fs */
163 static inline struct bch_devs_mask bch2_online_devs(struct bch_fs *c)
165 struct bch_devs_mask devs;
169 memset(&devs, 0, sizeof(devs));
170 for_each_online_member(ca, c, i)
171 __set_bit(ca->dev_idx, devs.d);
175 struct bch_fs *bch2_bdev_to_fs(struct block_device *);
176 struct bch_fs *bch2_uuid_to_fs(uuid_le);
177 int bch2_congested(void *, int);
179 bool bch2_dev_state_allowed(struct bch_fs *, struct bch_dev *,
180 enum bch_member_state, int);
181 int __bch2_dev_set_state(struct bch_fs *, struct bch_dev *,
182 enum bch_member_state, int);
183 int bch2_dev_set_state(struct bch_fs *, struct bch_dev *,
184 enum bch_member_state, int);
186 int bch2_dev_fail(struct bch_dev *, int);
187 int bch2_dev_remove(struct bch_fs *, struct bch_dev *, int);
188 int bch2_dev_add(struct bch_fs *, const char *);
189 int bch2_dev_online(struct bch_fs *, const char *);
190 int bch2_dev_offline(struct bch_fs *, struct bch_dev *, int);
191 int bch2_dev_resize(struct bch_fs *, struct bch_dev *, u64);
193 bool bch2_fs_emergency_read_only(struct bch_fs *);
194 void bch2_fs_read_only(struct bch_fs *);
195 const char *bch2_fs_read_write(struct bch_fs *);
197 void bch2_fs_stop(struct bch_fs *);
199 const char *bch2_fs_start(struct bch_fs *);
200 struct bch_fs *bch2_fs_open(char * const *, unsigned, struct bch_opts);
201 const char *bch2_fs_open_incremental(const char *path);
203 #endif /* _BCACHEFS_SUPER_H */