1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_ALLOC_FOREGROUND_H
3 #define _BCACHEFS_ALLOC_FOREGROUND_H
6 #include "alloc_types.h"
10 #include <linux/hash.h>
17 extern const char * const bch2_alloc_reserves[];
19 struct dev_alloc_list {
21 u8 devs[BCH_SB_MEMBERS_MAX];
24 struct dev_alloc_list bch2_dev_alloc_list(struct bch_fs *,
25 struct dev_stripe_state *,
26 struct bch_devs_mask *);
27 void bch2_dev_stripe_increment(struct bch_dev *, struct dev_stripe_state *);
29 long bch2_bucket_alloc_new_fs(struct bch_dev *);
31 struct open_bucket *bch2_bucket_alloc(struct bch_fs *, struct bch_dev *,
32 enum alloc_reserve, bool,
35 static inline void ob_push(struct bch_fs *c, struct open_buckets *obs,
36 struct open_bucket *ob)
38 BUG_ON(obs->nr >= ARRAY_SIZE(obs->v));
40 obs->v[obs->nr++] = ob - c->open_buckets;
43 #define open_bucket_for_each(_c, _obs, _ob, _i) \
45 (_i) < (_obs)->nr && \
46 ((_ob) = (_c)->open_buckets + (_obs)->v[_i], true); \
49 static inline struct open_bucket *ec_open_bucket(struct bch_fs *c,
50 struct open_buckets *obs)
52 struct open_bucket *ob;
55 open_bucket_for_each(c, obs, ob, i)
62 void bch2_open_bucket_write_error(struct bch_fs *,
63 struct open_buckets *, unsigned);
65 void __bch2_open_bucket_put(struct bch_fs *, struct open_bucket *);
67 static inline void bch2_open_bucket_put(struct bch_fs *c, struct open_bucket *ob)
69 if (atomic_dec_and_test(&ob->pin))
70 __bch2_open_bucket_put(c, ob);
73 static inline void bch2_open_buckets_put(struct bch_fs *c,
74 struct open_buckets *ptrs)
76 struct open_bucket *ob;
79 open_bucket_for_each(c, ptrs, ob, i)
80 bch2_open_bucket_put(c, ob);
84 static inline void bch2_alloc_sectors_done_inlined(struct bch_fs *c, struct write_point *wp)
86 struct open_buckets ptrs = { .nr = 0 }, keep = { .nr = 0 };
87 struct open_bucket *ob;
90 open_bucket_for_each(c, &wp->ptrs, ob, i)
91 ob_push(c, !ob->sectors_free ? &ptrs : &keep, ob);
94 mutex_unlock(&wp->lock);
96 bch2_open_buckets_put(c, &ptrs);
99 static inline void bch2_open_bucket_get(struct bch_fs *c,
100 struct write_point *wp,
101 struct open_buckets *ptrs)
103 struct open_bucket *ob;
106 open_bucket_for_each(c, &wp->ptrs, ob, i) {
107 ob->data_type = wp->data_type;
108 atomic_inc(&ob->pin);
109 ob_push(c, ptrs, ob);
113 static inline open_bucket_idx_t *open_bucket_hashslot(struct bch_fs *c,
114 unsigned dev, u64 bucket)
116 return c->open_buckets_hash +
117 (jhash_3words(dev, bucket, bucket >> 32, 0) &
118 (OPEN_BUCKETS_COUNT - 1));
121 static inline bool bch2_bucket_is_open(struct bch_fs *c, unsigned dev, u64 bucket)
123 open_bucket_idx_t slot = *open_bucket_hashslot(c, dev, bucket);
126 struct open_bucket *ob = &c->open_buckets[slot];
128 if (ob->dev == dev && ob->bucket == bucket)
137 static inline bool bch2_bucket_is_open_safe(struct bch_fs *c, unsigned dev, u64 bucket)
141 if (bch2_bucket_is_open(c, dev, bucket))
144 spin_lock(&c->freelist_lock);
145 ret = bch2_bucket_is_open(c, dev, bucket);
146 spin_unlock(&c->freelist_lock);
151 int bch2_bucket_alloc_set(struct bch_fs *, struct open_buckets *,
152 struct dev_stripe_state *, struct bch_devs_mask *,
153 unsigned, unsigned *, bool *, enum alloc_reserve,
154 unsigned, struct closure *);
156 int bch2_alloc_sectors_start_trans(struct btree_trans *,
158 struct write_point_specifier,
159 struct bch_devs_list *,
164 struct write_point **);
166 struct bch_extent_ptr bch2_ob_ptr(struct bch_fs *, struct open_bucket *);
169 * Append pointers to the space we just allocated to @k, and mark @sectors space
170 * as allocated out of @ob
173 bch2_alloc_sectors_append_ptrs_inlined(struct bch_fs *c, struct write_point *wp,
174 struct bkey_i *k, unsigned sectors,
177 struct open_bucket *ob;
180 BUG_ON(sectors > wp->sectors_free);
181 wp->sectors_free -= sectors;
183 open_bucket_for_each(c, &wp->ptrs, ob, i) {
184 struct bch_dev *ca = bch_dev_bkey_exists(c, ob->dev);
185 struct bch_extent_ptr ptr = bch2_ob_ptr(c, ob);
187 ptr.cached = cached ||
188 (!ca->mi.durability &&
189 wp->data_type == BCH_DATA_user);
191 bch2_bkey_append_ptr(k, ptr);
193 BUG_ON(sectors > ob->sectors_free);
194 ob->sectors_free -= sectors;
198 void bch2_alloc_sectors_append_ptrs(struct bch_fs *, struct write_point *,
199 struct bkey_i *, unsigned, bool);
200 void bch2_alloc_sectors_done(struct bch_fs *, struct write_point *);
202 void bch2_open_buckets_stop_dev(struct bch_fs *, struct bch_dev *,
203 struct open_buckets *);
205 void bch2_writepoint_stop(struct bch_fs *, struct bch_dev *,
206 struct write_point *);
208 static inline struct write_point_specifier writepoint_hashed(unsigned long v)
210 return (struct write_point_specifier) { .v = v | 1 };
213 static inline struct write_point_specifier writepoint_ptr(struct write_point *wp)
215 return (struct write_point_specifier) { .v = (unsigned long) wp };
218 void bch2_fs_allocator_foreground_init(struct bch_fs *);
220 void bch2_open_buckets_to_text(struct printbuf *, struct bch_fs *);
222 #endif /* _BCACHEFS_ALLOC_FOREGROUND_H */