1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_SB_MEMBERS_H
3 #define _BCACHEFS_SB_MEMBERS_H
5 extern char * const bch2_member_error_strs[];
7 static inline struct bch_member *
8 __bch2_members_v2_get_mut(struct bch_sb_field_members_v2 *mi, unsigned i)
10 return (void *) mi->_members + (i * le16_to_cpu(mi->member_bytes));
13 int bch2_sb_members_v2_init(struct bch_fs *c);
14 int bch2_sb_members_cpy_v2_v1(struct bch_sb_handle *disk_sb);
15 struct bch_member *bch2_members_v2_get_mut(struct bch_sb *sb, int i);
16 struct bch_member bch2_sb_member_get(struct bch_sb *sb, int i);
18 static inline bool bch2_dev_is_online(struct bch_dev *ca)
20 return !percpu_ref_is_zero(&ca->io_ref);
23 static inline bool bch2_dev_is_readable(struct bch_dev *ca)
25 return bch2_dev_is_online(ca) &&
26 ca->mi.state != BCH_MEMBER_STATE_failed;
29 static inline bool bch2_dev_get_ioref(struct bch_dev *ca, int rw)
31 if (!percpu_ref_tryget(&ca->io_ref))
34 if (ca->mi.state == BCH_MEMBER_STATE_rw ||
35 (ca->mi.state == BCH_MEMBER_STATE_ro && rw == READ))
38 percpu_ref_put(&ca->io_ref);
42 static inline unsigned dev_mask_nr(const struct bch_devs_mask *devs)
44 return bitmap_weight(devs->d, BCH_SB_MEMBERS_MAX);
47 static inline bool bch2_dev_list_has_dev(struct bch_devs_list devs,
52 for (i = 0; i < devs.nr; i++)
53 if (devs.devs[i] == dev)
59 static inline void bch2_dev_list_drop_dev(struct bch_devs_list *devs,
64 for (i = 0; i < devs->nr; i++)
65 if (devs->devs[i] == dev) {
66 array_remove_item(devs->devs, devs->nr, i);
71 static inline void bch2_dev_list_add_dev(struct bch_devs_list *devs,
74 if (!bch2_dev_list_has_dev(*devs, dev)) {
75 BUG_ON(devs->nr >= ARRAY_SIZE(devs->devs));
76 devs->devs[devs->nr++] = dev;
80 static inline struct bch_devs_list bch2_dev_list_single(unsigned dev)
82 return (struct bch_devs_list) { .nr = 1, .devs[0] = dev };
85 static inline struct bch_dev *__bch2_next_dev_idx(struct bch_fs *c, unsigned idx,
86 const struct bch_devs_mask *mask)
88 struct bch_dev *ca = NULL;
91 ? find_next_bit(mask->d, c->sb.nr_devices, idx)
92 : idx) < c->sb.nr_devices &&
93 !(ca = rcu_dereference_check(c->devs[idx],
94 lockdep_is_held(&c->state_lock))))
100 static inline struct bch_dev *__bch2_next_dev(struct bch_fs *c, struct bch_dev *ca,
101 const struct bch_devs_mask *mask)
103 return __bch2_next_dev_idx(c, ca ? ca->dev_idx + 1 : 0, mask);
106 #define for_each_member_device_rcu(_c, _ca, _mask) \
107 for (struct bch_dev *_ca = NULL; \
108 (_ca = __bch2_next_dev((_c), _ca, (_mask)));)
110 static inline struct bch_dev *bch2_get_next_dev(struct bch_fs *c, struct bch_dev *ca)
113 percpu_ref_put(&ca->ref);
116 if ((ca = __bch2_next_dev(c, ca, NULL)))
117 percpu_ref_get(&ca->ref);
124 * If you break early, you must drop your ref on the current device
126 #define __for_each_member_device(_c, _ca) \
127 for (; (_ca = bch2_get_next_dev(_c, _ca));)
129 #define for_each_member_device(_c, _ca) \
130 for (struct bch_dev *_ca = NULL; \
131 (_ca = bch2_get_next_dev(_c, _ca));)
133 static inline struct bch_dev *bch2_get_next_online_dev(struct bch_fs *c,
138 percpu_ref_put(&ca->io_ref);
141 while ((ca = __bch2_next_dev(c, ca, NULL)) &&
142 (!((1 << ca->mi.state) & state_mask) ||
143 !percpu_ref_tryget(&ca->io_ref)))
150 #define __for_each_online_member(_c, _ca, state_mask) \
151 for (struct bch_dev *_ca = NULL; \
152 (_ca = bch2_get_next_online_dev(_c, _ca, state_mask));)
154 #define for_each_online_member(c, ca) \
155 __for_each_online_member(c, ca, ~0)
157 #define for_each_rw_member(c, ca) \
158 __for_each_online_member(c, ca, BIT(BCH_MEMBER_STATE_rw))
160 #define for_each_readable_member(c, ca) \
161 __for_each_online_member(c, ca, BIT( BCH_MEMBER_STATE_rw)|BIT(BCH_MEMBER_STATE_ro))
164 * If a key exists that references a device, the device won't be going away and
165 * we can omit rcu_read_lock():
167 static inline struct bch_dev *bch_dev_bkey_exists(const struct bch_fs *c, unsigned idx)
169 EBUG_ON(idx >= c->sb.nr_devices || !c->devs[idx]);
171 return rcu_dereference_check(c->devs[idx], 1);
174 static inline struct bch_dev *bch_dev_locked(struct bch_fs *c, unsigned idx)
176 EBUG_ON(idx >= c->sb.nr_devices || !c->devs[idx]);
178 return rcu_dereference_protected(c->devs[idx],
179 lockdep_is_held(&c->sb_lock) ||
180 lockdep_is_held(&c->state_lock));
183 /* XXX kill, move to struct bch_fs */
184 static inline struct bch_devs_mask bch2_online_devs(struct bch_fs *c)
186 struct bch_devs_mask devs;
188 memset(&devs, 0, sizeof(devs));
189 for_each_online_member(c, ca)
190 __set_bit(ca->dev_idx, devs.d);
194 extern const struct bch_sb_field_ops bch_sb_field_ops_members_v1;
195 extern const struct bch_sb_field_ops bch_sb_field_ops_members_v2;
197 static inline bool bch2_member_exists(struct bch_member *m)
199 return !bch2_is_zero(&m->uuid, sizeof(m->uuid));
202 static inline bool bch2_dev_exists(struct bch_sb *sb, unsigned dev)
204 if (dev < sb->nr_devices) {
205 struct bch_member m = bch2_sb_member_get(sb, dev);
206 return bch2_member_exists(&m);
211 static inline struct bch_member_cpu bch2_mi_to_cpu(struct bch_member *mi)
213 return (struct bch_member_cpu) {
214 .nbuckets = le64_to_cpu(mi->nbuckets),
215 .first_bucket = le16_to_cpu(mi->first_bucket),
216 .bucket_size = le16_to_cpu(mi->bucket_size),
217 .group = BCH_MEMBER_GROUP(mi),
218 .state = BCH_MEMBER_STATE(mi),
219 .discard = BCH_MEMBER_DISCARD(mi),
220 .data_allowed = BCH_MEMBER_DATA_ALLOWED(mi),
221 .durability = BCH_MEMBER_DURABILITY(mi)
222 ? BCH_MEMBER_DURABILITY(mi) - 1
224 .freespace_initialized = BCH_MEMBER_FREESPACE_INITIALIZED(mi),
225 .valid = bch2_member_exists(mi),
229 void bch2_sb_members_from_cpu(struct bch_fs *);
231 void bch2_dev_io_errors_to_text(struct printbuf *, struct bch_dev *);
232 void bch2_dev_errors_reset(struct bch_dev *);
234 #endif /* _BCACHEFS_SB_MEMBERS_H */