]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/super.h
02c81f3555c3559d7a7d2b2f65bce28ded807899
[bcachefs-tools-debian] / libbcachefs / super.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_SUPER_H
3 #define _BCACHEFS_SUPER_H
4
5 #include "extents.h"
6
7 #include "bcachefs_ioctl.h"
8
9 #include <linux/math64.h>
10
11 static inline size_t sector_to_bucket(const struct bch_dev *ca, sector_t s)
12 {
13         return div_u64(s, ca->mi.bucket_size);
14 }
15
16 static inline sector_t bucket_to_sector(const struct bch_dev *ca, size_t b)
17 {
18         return ((sector_t) b) * ca->mi.bucket_size;
19 }
20
21 static inline sector_t bucket_remainder(const struct bch_dev *ca, sector_t s)
22 {
23         u32 remainder;
24
25         div_u64_rem(s, ca->mi.bucket_size, &remainder);
26         return remainder;
27 }
28
29 static inline bool bch2_dev_is_online(struct bch_dev *ca)
30 {
31         return !percpu_ref_is_zero(&ca->io_ref);
32 }
33
34 static inline bool bch2_dev_is_readable(struct bch_dev *ca)
35 {
36         return bch2_dev_is_online(ca) &&
37                 ca->mi.state != BCH_MEMBER_STATE_FAILED;
38 }
39
40 static inline bool bch2_dev_get_ioref(struct bch_dev *ca, int rw)
41 {
42         if (!percpu_ref_tryget(&ca->io_ref))
43                 return false;
44
45         if (ca->mi.state == BCH_MEMBER_STATE_RW ||
46             (ca->mi.state == BCH_MEMBER_STATE_RO && rw == READ))
47                 return true;
48
49         percpu_ref_put(&ca->io_ref);
50         return false;
51 }
52
53 static inline unsigned dev_mask_nr(const struct bch_devs_mask *devs)
54 {
55         return bitmap_weight(devs->d, BCH_SB_MEMBERS_MAX);
56 }
57
58 static inline bool bch2_dev_list_has_dev(struct bch_devs_list devs,
59                                          unsigned dev)
60 {
61         unsigned i;
62
63         for (i = 0; i < devs.nr; i++)
64                 if (devs.devs[i] == dev)
65                         return true;
66
67         return false;
68 }
69
70 static inline void bch2_dev_list_drop_dev(struct bch_devs_list *devs,
71                                           unsigned dev)
72 {
73         unsigned i;
74
75         for (i = 0; i < devs->nr; i++)
76                 if (devs->devs[i] == dev) {
77                         array_remove_item(devs->devs, devs->nr, i);
78                         return;
79                 }
80 }
81
82 static inline void bch2_dev_list_add_dev(struct bch_devs_list *devs,
83                                          unsigned dev)
84 {
85         BUG_ON(bch2_dev_list_has_dev(*devs, dev));
86         BUG_ON(devs->nr >= BCH_REPLICAS_MAX);
87         devs->devs[devs->nr++] = dev;
88 }
89
90 static inline struct bch_devs_list bch2_dev_list_single(unsigned dev)
91 {
92         return (struct bch_devs_list) { .nr = 1, .devs[0] = dev };
93 }
94
95 static inline struct bch_dev *__bch2_next_dev(struct bch_fs *c, unsigned *iter,
96                                               const struct bch_devs_mask *mask)
97 {
98         struct bch_dev *ca = NULL;
99
100         while ((*iter = mask
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))))
105                 (*iter)++;
106
107         return ca;
108 }
109
110 #define __for_each_member_device(ca, c, iter, mask)                     \
111         for ((iter) = 0; ((ca) = __bch2_next_dev((c), &(iter), mask)); (iter)++)
112
113 #define for_each_member_device_rcu(ca, c, iter, mask)                   \
114         __for_each_member_device(ca, c, iter, mask)
115
116 static inline struct bch_dev *bch2_get_next_dev(struct bch_fs *c, unsigned *iter)
117 {
118         struct bch_dev *ca;
119
120         rcu_read_lock();
121         if ((ca = __bch2_next_dev(c, iter, NULL)))
122                 percpu_ref_get(&ca->ref);
123         rcu_read_unlock();
124
125         return ca;
126 }
127
128 /*
129  * If you break early, you must drop your ref on the current device
130  */
131 #define for_each_member_device(ca, c, iter)                             \
132         for ((iter) = 0;                                                \
133              (ca = bch2_get_next_dev(c, &(iter)));                      \
134              percpu_ref_put(&ca->ref), (iter)++)
135
136 static inline struct bch_dev *bch2_get_next_online_dev(struct bch_fs *c,
137                                                       unsigned *iter,
138                                                       int state_mask)
139 {
140         struct bch_dev *ca;
141
142         rcu_read_lock();
143         while ((ca = __bch2_next_dev(c, iter, NULL)) &&
144                (!((1 << ca->mi.state) & state_mask) ||
145                 !percpu_ref_tryget(&ca->io_ref)))
146                 (*iter)++;
147         rcu_read_unlock();
148
149         return ca;
150 }
151
152 #define __for_each_online_member(ca, c, iter, state_mask)               \
153         for ((iter) = 0;                                                \
154              (ca = bch2_get_next_online_dev(c, &(iter), state_mask));   \
155              percpu_ref_put(&ca->io_ref), (iter)++)
156
157 #define for_each_online_member(ca, c, iter)                             \
158         __for_each_online_member(ca, c, iter, ~0)
159
160 #define for_each_rw_member(ca, c, iter)                                 \
161         __for_each_online_member(ca, c, iter, 1 << BCH_MEMBER_STATE_RW)
162
163 #define for_each_readable_member(ca, c, iter)                           \
164         __for_each_online_member(ca, c, iter,                           \
165                 (1 << BCH_MEMBER_STATE_RW)|(1 << BCH_MEMBER_STATE_RO))
166
167 /*
168  * If a key exists that references a device, the device won't be going away and
169  * we can omit rcu_read_lock():
170  */
171 static inline struct bch_dev *bch_dev_bkey_exists(const struct bch_fs *c, unsigned idx)
172 {
173         EBUG_ON(idx >= c->sb.nr_devices || !c->devs[idx]);
174
175         return rcu_dereference_check(c->devs[idx], 1);
176 }
177
178 static inline struct bch_dev *bch_dev_locked(struct bch_fs *c, unsigned idx)
179 {
180         EBUG_ON(idx >= c->sb.nr_devices || !c->devs[idx]);
181
182         return rcu_dereference_protected(c->devs[idx],
183                                          lockdep_is_held(&c->sb_lock) ||
184                                          lockdep_is_held(&c->state_lock));
185 }
186
187 /* XXX kill, move to struct bch_fs */
188 static inline struct bch_devs_mask bch2_online_devs(struct bch_fs *c)
189 {
190         struct bch_devs_mask devs;
191         struct bch_dev *ca;
192         unsigned i;
193
194         memset(&devs, 0, sizeof(devs));
195         for_each_online_member(ca, c, i)
196                 __set_bit(ca->dev_idx, devs.d);
197         return devs;
198 }
199
200 struct bch_fs *bch2_bdev_to_fs(struct block_device *);
201 struct bch_fs *bch2_uuid_to_fs(uuid_le);
202
203 bool bch2_dev_state_allowed(struct bch_fs *, struct bch_dev *,
204                            enum bch_member_state, int);
205 int __bch2_dev_set_state(struct bch_fs *, struct bch_dev *,
206                         enum bch_member_state, int);
207 int bch2_dev_set_state(struct bch_fs *, struct bch_dev *,
208                       enum bch_member_state, int);
209
210 int bch2_dev_fail(struct bch_dev *, int);
211 int bch2_dev_remove(struct bch_fs *, struct bch_dev *, int);
212 int bch2_dev_add(struct bch_fs *, const char *);
213 int bch2_dev_online(struct bch_fs *, const char *);
214 int bch2_dev_offline(struct bch_fs *, struct bch_dev *, int);
215 int bch2_dev_resize(struct bch_fs *, struct bch_dev *, u64);
216 struct bch_dev *bch2_dev_lookup(struct bch_fs *, const char *);
217
218 bool bch2_fs_emergency_read_only(struct bch_fs *);
219 void bch2_fs_read_only(struct bch_fs *);
220
221 int bch2_fs_read_write(struct bch_fs *);
222 int bch2_fs_read_write_early(struct bch_fs *);
223
224 /*
225  * Only for use in the recovery/fsck path:
226  */
227 static inline void bch2_fs_lazy_rw(struct bch_fs *c)
228 {
229         if (percpu_ref_is_zero(&c->writes))
230                 bch2_fs_read_write_early(c);
231 }
232
233 void __bch2_fs_stop(struct bch_fs *);
234 void bch2_fs_free(struct bch_fs *);
235 void bch2_fs_stop(struct bch_fs *);
236
237 int bch2_fs_start(struct bch_fs *);
238 struct bch_fs *bch2_fs_open(char * const *, unsigned, struct bch_opts);
239 const char *bch2_fs_open_incremental(const char *path);
240
241 #endif /* _BCACHEFS_SUPER_H */