1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_ALLOC_FOREGROUND_H
3 #define _BCACHEFS_ALLOC_FOREGROUND_H
4
5 #include "bcachefs.h"
6 #include "alloc_types.h"
7 #include "extents.h"
8 #include "sb-members.h"
9
10 #include <linux/hash.h>
11
12 struct bkey;
13 struct bch_dev;
14 struct bch_fs;
15 struct bch_devs_List;
16
17 extern const char * const bch2_watermarks[];
18
19 void bch2_reset_alloc_cursors(struct bch_fs *);
20
21 struct dev_alloc_list {
22 unsigned nr;
23 u8 data[BCH_SB_MEMBERS_MAX];
24 };
25
26 struct dev_alloc_list bch2_dev_alloc_list(struct bch_fs *,
27 struct dev_stripe_state *,
28 struct bch_devs_mask *);
29 void bch2_dev_stripe_increment(struct bch_dev *, struct dev_stripe_state *);
30
ob_dev(struct bch_fs * c,struct open_bucket * ob)31 static inline struct bch_dev *ob_dev(struct bch_fs *c, struct open_bucket *ob)
32 {
33 return bch2_dev_have_ref(c, ob->dev);
34 }
35
bch2_open_buckets_reserved(enum bch_watermark watermark)36 static inline unsigned bch2_open_buckets_reserved(enum bch_watermark watermark)
37 {
38 switch (watermark) {
39 case BCH_WATERMARK_interior_updates:
40 return 0;
41 case BCH_WATERMARK_reclaim:
42 return OPEN_BUCKETS_COUNT / 6;
43 case BCH_WATERMARK_btree:
44 case BCH_WATERMARK_btree_copygc:
45 return OPEN_BUCKETS_COUNT / 4;
46 case BCH_WATERMARK_copygc:
47 return OPEN_BUCKETS_COUNT / 3;
48 default:
49 return OPEN_BUCKETS_COUNT / 2;
50 }
51 }
52
53 struct open_bucket *bch2_bucket_alloc(struct bch_fs *, struct bch_dev *,
54 enum bch_watermark, enum bch_data_type,
55 struct closure *);
56
ob_push(struct bch_fs * c,struct open_buckets * obs,struct open_bucket * ob)57 static inline void ob_push(struct bch_fs *c, struct open_buckets *obs,
58 struct open_bucket *ob)
59 {
60 BUG_ON(obs->nr >= ARRAY_SIZE(obs->v));
61
62 obs->v[obs->nr++] = ob - c->open_buckets;
63 }
64
65 #define open_bucket_for_each(_c, _obs, _ob, _i) \
66 for ((_i) = 0; \
67 (_i) < (_obs)->nr && \
68 ((_ob) = (_c)->open_buckets + (_obs)->v[_i], true); \
69 (_i)++)
70
ec_open_bucket(struct bch_fs * c,struct open_buckets * obs)71 static inline struct open_bucket *ec_open_bucket(struct bch_fs *c,
72 struct open_buckets *obs)
73 {
74 struct open_bucket *ob;
75 unsigned i;
76
77 open_bucket_for_each(c, obs, ob, i)
78 if (ob->ec)
79 return ob;
80
81 return NULL;
82 }
83
84 void bch2_open_bucket_write_error(struct bch_fs *,
85 struct open_buckets *, unsigned, int);
86
87 void __bch2_open_bucket_put(struct bch_fs *, struct open_bucket *);
88
bch2_open_bucket_put(struct bch_fs * c,struct open_bucket * ob)89 static inline void bch2_open_bucket_put(struct bch_fs *c, struct open_bucket *ob)
90 {
91 if (atomic_dec_and_test(&ob->pin))
92 __bch2_open_bucket_put(c, ob);
93 }
94
bch2_open_buckets_put(struct bch_fs * c,struct open_buckets * ptrs)95 static inline void bch2_open_buckets_put(struct bch_fs *c,
96 struct open_buckets *ptrs)
97 {
98 struct open_bucket *ob;
99 unsigned i;
100
101 open_bucket_for_each(c, ptrs, ob, i)
102 bch2_open_bucket_put(c, ob);
103 ptrs->nr = 0;
104 }
105
bch2_alloc_sectors_done_inlined(struct bch_fs * c,struct write_point * wp)106 static inline void bch2_alloc_sectors_done_inlined(struct bch_fs *c, struct write_point *wp)
107 {
108 struct open_buckets ptrs = { .nr = 0 }, keep = { .nr = 0 };
109 struct open_bucket *ob;
110 unsigned i;
111
112 open_bucket_for_each(c, &wp->ptrs, ob, i)
113 ob_push(c, !ob->sectors_free ? &ptrs : &keep, ob);
114 wp->ptrs = keep;
115
116 mutex_unlock(&wp->lock);
117
118 bch2_open_buckets_put(c, &ptrs);
119 }
120
bch2_open_bucket_get(struct bch_fs * c,struct write_point * wp,struct open_buckets * ptrs)121 static inline void bch2_open_bucket_get(struct bch_fs *c,
122 struct write_point *wp,
123 struct open_buckets *ptrs)
124 {
125 struct open_bucket *ob;
126 unsigned i;
127
128 open_bucket_for_each(c, &wp->ptrs, ob, i) {
129 ob->data_type = wp->data_type;
130 atomic_inc(&ob->pin);
131 ob_push(c, ptrs, ob);
132 }
133 }
134
open_bucket_hashslot(struct bch_fs * c,unsigned dev,u64 bucket)135 static inline open_bucket_idx_t *open_bucket_hashslot(struct bch_fs *c,
136 unsigned dev, u64 bucket)
137 {
138 return c->open_buckets_hash +
139 (jhash_3words(dev, bucket, bucket >> 32, 0) &
140 (OPEN_BUCKETS_COUNT - 1));
141 }
142
bch2_bucket_is_open(struct bch_fs * c,unsigned dev,u64 bucket)143 static inline bool bch2_bucket_is_open(struct bch_fs *c, unsigned dev, u64 bucket)
144 {
145 open_bucket_idx_t slot = *open_bucket_hashslot(c, dev, bucket);
146
147 while (slot) {
148 struct open_bucket *ob = &c->open_buckets[slot];
149
150 if (ob->dev == dev && ob->bucket == bucket)
151 return true;
152
153 slot = ob->hash;
154 }
155
156 return false;
157 }
158
bch2_bucket_is_open_safe(struct bch_fs * c,unsigned dev,u64 bucket)159 static inline bool bch2_bucket_is_open_safe(struct bch_fs *c, unsigned dev, u64 bucket)
160 {
161 bool ret;
162
163 if (bch2_bucket_is_open(c, dev, bucket))
164 return true;
165
166 spin_lock(&c->freelist_lock);
167 ret = bch2_bucket_is_open(c, dev, bucket);
168 spin_unlock(&c->freelist_lock);
169
170 return ret;
171 }
172
173 enum bch_write_flags;
174 int bch2_bucket_alloc_set_trans(struct btree_trans *, struct open_buckets *,
175 struct dev_stripe_state *, struct bch_devs_mask *,
176 unsigned, unsigned *, bool *, enum bch_write_flags,
177 enum bch_data_type, enum bch_watermark,
178 struct closure *);
179
180 int bch2_alloc_sectors_start_trans(struct btree_trans *,
181 unsigned, unsigned,
182 struct write_point_specifier,
183 struct bch_devs_list *,
184 unsigned, unsigned,
185 enum bch_watermark,
186 enum bch_write_flags,
187 struct closure *,
188 struct write_point **);
189
190 struct bch_extent_ptr bch2_ob_ptr(struct bch_fs *, struct open_bucket *);
191
192 /*
193 * Append pointers to the space we just allocated to @k, and mark @sectors space
194 * as allocated out of @ob
195 */
196 static inline void
bch2_alloc_sectors_append_ptrs_inlined(struct bch_fs * c,struct write_point * wp,struct bkey_i * k,unsigned sectors,bool cached)197 bch2_alloc_sectors_append_ptrs_inlined(struct bch_fs *c, struct write_point *wp,
198 struct bkey_i *k, unsigned sectors,
199 bool cached)
200 {
201 struct open_bucket *ob;
202 unsigned i;
203
204 BUG_ON(sectors > wp->sectors_free);
205 wp->sectors_free -= sectors;
206 wp->sectors_allocated += sectors;
207
208 open_bucket_for_each(c, &wp->ptrs, ob, i) {
209 struct bch_dev *ca = ob_dev(c, ob);
210 struct bch_extent_ptr ptr = bch2_ob_ptr(c, ob);
211
212 ptr.cached = cached ||
213 (!ca->mi.durability &&
214 wp->data_type == BCH_DATA_user);
215
216 bch2_bkey_append_ptr(k, ptr);
217
218 BUG_ON(sectors > ob->sectors_free);
219 ob->sectors_free -= sectors;
220 }
221 }
222
223 void bch2_alloc_sectors_append_ptrs(struct bch_fs *, struct write_point *,
224 struct bkey_i *, unsigned, bool);
225 void bch2_alloc_sectors_done(struct bch_fs *, struct write_point *);
226
227 void bch2_open_buckets_stop(struct bch_fs *c, struct bch_dev *, bool);
228
writepoint_hashed(unsigned long v)229 static inline struct write_point_specifier writepoint_hashed(unsigned long v)
230 {
231 return (struct write_point_specifier) { .v = v | 1 };
232 }
233
writepoint_ptr(struct write_point * wp)234 static inline struct write_point_specifier writepoint_ptr(struct write_point *wp)
235 {
236 return (struct write_point_specifier) { .v = (unsigned long) wp };
237 }
238
239 void bch2_fs_allocator_foreground_init(struct bch_fs *);
240
241 void bch2_open_bucket_to_text(struct printbuf *, struct bch_fs *, struct open_bucket *);
242 void bch2_open_buckets_to_text(struct printbuf *, struct bch_fs *, struct bch_dev *);
243 void bch2_open_buckets_partial_to_text(struct printbuf *, struct bch_fs *);
244
245 void bch2_write_points_to_text(struct printbuf *, struct bch_fs *);
246
247 void bch2_fs_alloc_debug_to_text(struct printbuf *, struct bch_fs *);
248 void bch2_dev_alloc_debug_to_text(struct printbuf *, struct bch_dev *);
249
250 void __bch2_wait_on_allocator(struct bch_fs *, struct closure *);
bch2_wait_on_allocator(struct bch_fs * c,struct closure * cl)251 static inline void bch2_wait_on_allocator(struct bch_fs *c, struct closure *cl)
252 {
253 if (cl->closure_get_happened)
254 __bch2_wait_on_allocator(c, cl);
255 }
256
257 #endif /* _BCACHEFS_ALLOC_FOREGROUND_H */
258