]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/sb-members.h
Update bcachefs sources to df6415fefb27 bcachefs: Fixes for rust bindgen
[bcachefs-tools-debian] / libbcachefs / sb-members.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_SB_MEMBERS_H
3 #define _BCACHEFS_SB_MEMBERS_H
4
5 extern char * const bch2_member_error_strs[];
6
7 static inline struct bch_member *
8 __bch2_members_v2_get_mut(struct bch_sb_field_members_v2 *mi, unsigned i)
9 {
10         return (void *) mi->_members + (i * le16_to_cpu(mi->member_bytes));
11 }
12
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);
17
18 static inline bool bch2_dev_is_online(struct bch_dev *ca)
19 {
20         return !percpu_ref_is_zero(&ca->io_ref);
21 }
22
23 static inline bool bch2_dev_is_readable(struct bch_dev *ca)
24 {
25         return bch2_dev_is_online(ca) &&
26                 ca->mi.state != BCH_MEMBER_STATE_failed;
27 }
28
29 static inline bool bch2_dev_get_ioref(struct bch_dev *ca, int rw)
30 {
31         if (!percpu_ref_tryget(&ca->io_ref))
32                 return false;
33
34         if (ca->mi.state == BCH_MEMBER_STATE_rw ||
35             (ca->mi.state == BCH_MEMBER_STATE_ro && rw == READ))
36                 return true;
37
38         percpu_ref_put(&ca->io_ref);
39         return false;
40 }
41
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
47 static inline bool bch2_dev_list_has_dev(struct bch_devs_list devs,
48                                          unsigned dev)
49 {
50         unsigned i;
51
52         for (i = 0; i < devs.nr; i++)
53                 if (devs.devs[i] == dev)
54                         return true;
55
56         return false;
57 }
58
59 static inline void bch2_dev_list_drop_dev(struct bch_devs_list *devs,
60                                           unsigned dev)
61 {
62         unsigned i;
63
64         for (i = 0; i < devs->nr; i++)
65                 if (devs->devs[i] == dev) {
66                         array_remove_item(devs->devs, devs->nr, i);
67                         return;
68                 }
69 }
70
71 static inline void bch2_dev_list_add_dev(struct bch_devs_list *devs,
72                                          unsigned dev)
73 {
74         if (!bch2_dev_list_has_dev(*devs, dev)) {
75                 BUG_ON(devs->nr >= ARRAY_SIZE(devs->devs));
76                 devs->devs[devs->nr++] = dev;
77         }
78 }
79
80 static inline struct bch_devs_list bch2_dev_list_single(unsigned dev)
81 {
82         return (struct bch_devs_list) { .nr = 1, .devs[0] = dev };
83 }
84
85 static inline struct bch_dev *__bch2_next_dev_idx(struct bch_fs *c, unsigned idx,
86                                                   const struct bch_devs_mask *mask)
87 {
88         struct bch_dev *ca = NULL;
89
90         while ((idx = mask
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))))
95                 idx++;
96
97         return ca;
98 }
99
100 static inline struct bch_dev *__bch2_next_dev(struct bch_fs *c, struct bch_dev *ca,
101                                               const struct bch_devs_mask *mask)
102 {
103         return __bch2_next_dev_idx(c, ca ? ca->dev_idx + 1 : 0, mask);
104 }
105
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)));)
109
110 static inline struct bch_dev *bch2_get_next_dev(struct bch_fs *c, struct bch_dev *ca)
111 {
112         if (ca)
113                 percpu_ref_put(&ca->ref);
114
115         rcu_read_lock();
116         if ((ca = __bch2_next_dev(c, ca, NULL)))
117                 percpu_ref_get(&ca->ref);
118         rcu_read_unlock();
119
120         return ca;
121 }
122
123 /*
124  * If you break early, you must drop your ref on the current device
125  */
126 #define __for_each_member_device(_c, _ca)                               \
127         for (;  (_ca = bch2_get_next_dev(_c, _ca));)
128
129 #define for_each_member_device(_c, _ca)                                 \
130         for (struct bch_dev *_ca = NULL;                                \
131              (_ca = bch2_get_next_dev(_c, _ca));)
132
133 static inline struct bch_dev *bch2_get_next_online_dev(struct bch_fs *c,
134                                                        struct bch_dev *ca,
135                                                        unsigned state_mask)
136 {
137         if (ca)
138                 percpu_ref_put(&ca->io_ref);
139
140         rcu_read_lock();
141         while ((ca = __bch2_next_dev(c, ca, NULL)) &&
142                (!((1 << ca->mi.state) & state_mask) ||
143                 !percpu_ref_tryget(&ca->io_ref)))
144                 ;
145         rcu_read_unlock();
146
147         return ca;
148 }
149
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));)
153
154 #define for_each_online_member(c, ca)                                   \
155         __for_each_online_member(c, ca, ~0)
156
157 #define for_each_rw_member(c, ca)                                       \
158         __for_each_online_member(c, ca, BIT(BCH_MEMBER_STATE_rw))
159
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))
162
163 /*
164  * If a key exists that references a device, the device won't be going away and
165  * we can omit rcu_read_lock():
166  */
167 static inline struct bch_dev *bch_dev_bkey_exists(const struct bch_fs *c, unsigned idx)
168 {
169         EBUG_ON(idx >= c->sb.nr_devices || !c->devs[idx]);
170
171         return rcu_dereference_check(c->devs[idx], 1);
172 }
173
174 static inline struct bch_dev *bch_dev_locked(struct bch_fs *c, unsigned idx)
175 {
176         EBUG_ON(idx >= c->sb.nr_devices || !c->devs[idx]);
177
178         return rcu_dereference_protected(c->devs[idx],
179                                          lockdep_is_held(&c->sb_lock) ||
180                                          lockdep_is_held(&c->state_lock));
181 }
182
183 /* XXX kill, move to struct bch_fs */
184 static inline struct bch_devs_mask bch2_online_devs(struct bch_fs *c)
185 {
186         struct bch_devs_mask devs;
187
188         memset(&devs, 0, sizeof(devs));
189         for_each_online_member(c, ca)
190                 __set_bit(ca->dev_idx, devs.d);
191         return devs;
192 }
193
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;
196
197 static inline bool bch2_member_exists(struct bch_member *m)
198 {
199         return !bch2_is_zero(&m->uuid, sizeof(m->uuid));
200 }
201
202 static inline bool bch2_dev_exists(struct bch_sb *sb, unsigned dev)
203 {
204         if (dev < sb->nr_devices) {
205                 struct bch_member m = bch2_sb_member_get(sb, dev);
206                 return bch2_member_exists(&m);
207         }
208         return false;
209 }
210
211 static inline struct bch_member_cpu bch2_mi_to_cpu(struct bch_member *mi)
212 {
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
223                         : 1,
224                 .freespace_initialized = BCH_MEMBER_FREESPACE_INITIALIZED(mi),
225                 .valid          = bch2_member_exists(mi),
226         };
227 }
228
229 void bch2_sb_members_from_cpu(struct bch_fs *);
230
231 void bch2_dev_io_errors_to_text(struct printbuf *, struct bch_dev *);
232 void bch2_dev_errors_reset(struct bch_dev *);
233
234 #endif /* _BCACHEFS_SB_MEMBERS_H */