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