1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_ALLOC_FOREGROUND_H
3 #define _BCACHEFS_ALLOC_FOREGROUND_H
6 #include "alloc_types.h"
8 #include "sb-members.h"
10 #include <linux/hash.h>
17 extern const char * const bch2_watermarks[];
19 void bch2_reset_alloc_cursors(struct bch_fs *);
21 struct dev_alloc_list {
23 u8 data[BCH_SB_MEMBERS_MAX];
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 *);
31 static inline struct bch_dev *ob_dev(struct bch_fs *c, struct open_bucket *ob)
33 return bch2_dev_have_ref(c, ob->dev);
36 struct open_bucket *bch2_bucket_alloc(struct bch_fs *, struct bch_dev *,
37 enum bch_watermark, enum bch_data_type,
40 static inline void ob_push(struct bch_fs *c, struct open_buckets *obs,
41 struct open_bucket *ob)
43 BUG_ON(obs->nr >= ARRAY_SIZE(obs->v));
45 obs->v[obs->nr++] = ob - c->open_buckets;
48 #define open_bucket_for_each(_c, _obs, _ob, _i) \
50 (_i) < (_obs)->nr && \
51 ((_ob) = (_c)->open_buckets + (_obs)->v[_i], true); \
54 static inline struct open_bucket *ec_open_bucket(struct bch_fs *c,
55 struct open_buckets *obs)
57 struct open_bucket *ob;
60 open_bucket_for_each(c, obs, ob, i)
67 void bch2_open_bucket_write_error(struct bch_fs *,
68 struct open_buckets *, unsigned);
70 void __bch2_open_bucket_put(struct bch_fs *, struct open_bucket *);
72 static inline void bch2_open_bucket_put(struct bch_fs *c, struct open_bucket *ob)
74 if (atomic_dec_and_test(&ob->pin))
75 __bch2_open_bucket_put(c, ob);
78 static inline void bch2_open_buckets_put(struct bch_fs *c,
79 struct open_buckets *ptrs)
81 struct open_bucket *ob;
84 open_bucket_for_each(c, ptrs, ob, i)
85 bch2_open_bucket_put(c, ob);
89 static inline void bch2_alloc_sectors_done_inlined(struct bch_fs *c, struct write_point *wp)
91 struct open_buckets ptrs = { .nr = 0 }, keep = { .nr = 0 };
92 struct open_bucket *ob;
95 open_bucket_for_each(c, &wp->ptrs, ob, i)
96 ob_push(c, !ob->sectors_free ? &ptrs : &keep, ob);
99 mutex_unlock(&wp->lock);
101 bch2_open_buckets_put(c, &ptrs);
104 static inline void bch2_open_bucket_get(struct bch_fs *c,
105 struct write_point *wp,
106 struct open_buckets *ptrs)
108 struct open_bucket *ob;
111 open_bucket_for_each(c, &wp->ptrs, ob, i) {
112 ob->data_type = wp->data_type;
113 atomic_inc(&ob->pin);
114 ob_push(c, ptrs, ob);
118 static inline open_bucket_idx_t *open_bucket_hashslot(struct bch_fs *c,
119 unsigned dev, u64 bucket)
121 return c->open_buckets_hash +
122 (jhash_3words(dev, bucket, bucket >> 32, 0) &
123 (OPEN_BUCKETS_COUNT - 1));
126 static inline bool bch2_bucket_is_open(struct bch_fs *c, unsigned dev, u64 bucket)
128 open_bucket_idx_t slot = *open_bucket_hashslot(c, dev, bucket);
131 struct open_bucket *ob = &c->open_buckets[slot];
133 if (ob->dev == dev && ob->bucket == bucket)
142 static inline bool bch2_bucket_is_open_safe(struct bch_fs *c, unsigned dev, u64 bucket)
146 if (bch2_bucket_is_open(c, dev, bucket))
149 spin_lock(&c->freelist_lock);
150 ret = bch2_bucket_is_open(c, dev, bucket);
151 spin_unlock(&c->freelist_lock);
156 enum bch_write_flags;
157 int bch2_bucket_alloc_set_trans(struct btree_trans *, struct open_buckets *,
158 struct dev_stripe_state *, struct bch_devs_mask *,
159 unsigned, unsigned *, bool *, enum bch_write_flags,
160 enum bch_data_type, enum bch_watermark,
163 int bch2_alloc_sectors_start_trans(struct btree_trans *,
165 struct write_point_specifier,
166 struct bch_devs_list *,
169 enum bch_write_flags,
171 struct write_point **);
173 struct bch_extent_ptr bch2_ob_ptr(struct bch_fs *, struct open_bucket *);
176 * Append pointers to the space we just allocated to @k, and mark @sectors space
177 * as allocated out of @ob
180 bch2_alloc_sectors_append_ptrs_inlined(struct bch_fs *c, struct write_point *wp,
181 struct bkey_i *k, unsigned sectors,
184 struct open_bucket *ob;
187 BUG_ON(sectors > wp->sectors_free);
188 wp->sectors_free -= sectors;
189 wp->sectors_allocated += sectors;
191 open_bucket_for_each(c, &wp->ptrs, ob, i) {
192 struct bch_dev *ca = ob_dev(c, ob);
193 struct bch_extent_ptr ptr = bch2_ob_ptr(c, ob);
195 ptr.cached = cached ||
196 (!ca->mi.durability &&
197 wp->data_type == BCH_DATA_user);
199 bch2_bkey_append_ptr(k, ptr);
201 BUG_ON(sectors > ob->sectors_free);
202 ob->sectors_free -= sectors;
206 void bch2_alloc_sectors_append_ptrs(struct bch_fs *, struct write_point *,
207 struct bkey_i *, unsigned, bool);
208 void bch2_alloc_sectors_done(struct bch_fs *, struct write_point *);
210 void bch2_open_buckets_stop(struct bch_fs *c, struct bch_dev *, bool);
212 static inline struct write_point_specifier writepoint_hashed(unsigned long v)
214 return (struct write_point_specifier) { .v = v | 1 };
217 static inline struct write_point_specifier writepoint_ptr(struct write_point *wp)
219 return (struct write_point_specifier) { .v = (unsigned long) wp };
222 void bch2_fs_allocator_foreground_init(struct bch_fs *);
224 void bch2_open_bucket_to_text(struct printbuf *, struct bch_fs *, struct open_bucket *);
225 void bch2_open_buckets_to_text(struct printbuf *, struct bch_fs *, struct bch_dev *);
226 void bch2_open_buckets_partial_to_text(struct printbuf *, struct bch_fs *);
228 void bch2_write_points_to_text(struct printbuf *, struct bch_fs *);
230 void bch2_fs_alloc_debug_to_text(struct printbuf *, struct bch_fs *);
231 void bch2_dev_alloc_debug_to_text(struct printbuf *, struct bch_dev *);
233 void __bch2_wait_on_allocator(struct bch_fs *, struct closure *);
234 static inline void bch2_wait_on_allocator(struct bch_fs *c, struct closure *cl)
236 if (cl->closure_get_happened)
237 __bch2_wait_on_allocator(c, cl);
240 #endif /* _BCACHEFS_ALLOC_FOREGROUND_H */