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 <linux/hash.h>
15 enum bucket_alloc_ret {
18 FREELIST_EMPTY, /* Allocator thread not keeping up */
22 struct dev_alloc_list {
24 u8 devs[BCH_SB_MEMBERS_MAX];
27 struct dev_alloc_list bch2_dev_alloc_list(struct bch_fs *,
28 struct dev_stripe_state *,
29 struct bch_devs_mask *);
30 void bch2_dev_stripe_increment(struct bch_dev *, struct dev_stripe_state *);
32 long bch2_bucket_alloc_new_fs(struct bch_dev *);
34 struct open_bucket *bch2_bucket_alloc(struct bch_fs *, struct bch_dev *,
35 enum alloc_reserve, bool,
38 static inline void ob_push(struct bch_fs *c, struct open_buckets *obs,
39 struct open_bucket *ob)
41 BUG_ON(obs->nr >= ARRAY_SIZE(obs->v));
43 obs->v[obs->nr++] = ob - c->open_buckets;
46 #define open_bucket_for_each(_c, _obs, _ob, _i) \
48 (_i) < (_obs)->nr && \
49 ((_ob) = (_c)->open_buckets + (_obs)->v[_i], true); \
52 static inline struct open_bucket *ec_open_bucket(struct bch_fs *c,
53 struct open_buckets *obs)
55 struct open_bucket *ob;
58 open_bucket_for_each(c, obs, ob, i)
65 void bch2_open_bucket_write_error(struct bch_fs *,
66 struct open_buckets *, unsigned);
68 void __bch2_open_bucket_put(struct bch_fs *, struct open_bucket *);
70 static inline void bch2_open_bucket_put(struct bch_fs *c, struct open_bucket *ob)
72 if (atomic_dec_and_test(&ob->pin))
73 __bch2_open_bucket_put(c, ob);
76 static inline void bch2_open_buckets_put(struct bch_fs *c,
77 struct open_buckets *ptrs)
79 struct open_bucket *ob;
82 open_bucket_for_each(c, ptrs, ob, i)
83 bch2_open_bucket_put(c, ob);
87 static inline void bch2_open_bucket_get(struct bch_fs *c,
88 struct write_point *wp,
89 struct open_buckets *ptrs)
91 struct open_bucket *ob;
94 open_bucket_for_each(c, &wp->ptrs, ob, i) {
101 enum bucket_alloc_ret
102 bch2_bucket_alloc_set(struct bch_fs *, struct open_buckets *,
103 struct dev_stripe_state *, struct bch_devs_mask *,
104 unsigned, unsigned *, bool *, enum alloc_reserve,
105 unsigned, struct closure *);
107 struct write_point *bch2_alloc_sectors_start(struct bch_fs *,
109 struct write_point_specifier,
110 struct bch_devs_list *,
116 void bch2_alloc_sectors_append_ptrs(struct bch_fs *, struct write_point *,
117 struct bkey_i *, unsigned);
118 void bch2_alloc_sectors_done(struct bch_fs *, struct write_point *);
120 void bch2_open_buckets_stop_dev(struct bch_fs *, struct bch_dev *,
121 struct open_buckets *);
123 void bch2_writepoint_stop(struct bch_fs *, struct bch_dev *,
124 struct write_point *);
126 static inline struct write_point_specifier writepoint_hashed(unsigned long v)
128 return (struct write_point_specifier) { .v = v | 1 };
131 static inline struct write_point_specifier writepoint_ptr(struct write_point *wp)
133 return (struct write_point_specifier) { .v = (unsigned long) wp };
136 void bch2_fs_allocator_foreground_init(struct bch_fs *);
138 #endif /* _BCACHEFS_ALLOC_FOREGROUND_H */