Loading...
1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _BCACHEFS_ALLOC_BACKGROUND_H
3#define _BCACHEFS_ALLOC_BACKGROUND_H
4
5#include "bcachefs.h"
6#include "alloc_types.h"
7#include "buckets.h"
8#include "debug.h"
9#include "super.h"
10
11enum bkey_invalid_flags;
12
13/* How out of date a pointer gen is allowed to be: */
14#define BUCKET_GC_GEN_MAX 96U
15
16static inline bool bch2_dev_bucket_exists(struct bch_fs *c, struct bpos pos)
17{
18 struct bch_dev *ca;
19
20 if (!bch2_dev_exists2(c, pos.inode))
21 return false;
22
23 ca = bch_dev_bkey_exists(c, pos.inode);
24 return pos.offset >= ca->mi.first_bucket &&
25 pos.offset < ca->mi.nbuckets;
26}
27
28static inline u64 bucket_to_u64(struct bpos bucket)
29{
30 return (bucket.inode << 48) | bucket.offset;
31}
32
33static inline struct bpos u64_to_bucket(u64 bucket)
34{
35 return POS(bucket >> 48, bucket & ~(~0ULL << 48));
36}
37
38static inline u8 alloc_gc_gen(struct bch_alloc_v4 a)
39{
40 return a.gen - a.oldest_gen;
41}
42
43static inline enum bch_data_type __alloc_data_type(u32 dirty_sectors,
44 u32 cached_sectors,
45 u32 stripe,
46 struct bch_alloc_v4 a,
47 enum bch_data_type data_type)
48{
49 if (stripe)
50 return data_type == BCH_DATA_parity ? data_type : BCH_DATA_stripe;
51 if (dirty_sectors)
52 return data_type;
53 if (cached_sectors)
54 return BCH_DATA_cached;
55 if (BCH_ALLOC_V4_NEED_DISCARD(&a))
56 return BCH_DATA_need_discard;
57 if (alloc_gc_gen(a) >= BUCKET_GC_GEN_MAX)
58 return BCH_DATA_need_gc_gens;
59 return BCH_DATA_free;
60}
61
62static inline enum bch_data_type alloc_data_type(struct bch_alloc_v4 a,
63 enum bch_data_type data_type)
64{
65 return __alloc_data_type(a.dirty_sectors, a.cached_sectors,
66 a.stripe, a, data_type);
67}
68
69static inline enum bch_data_type bucket_data_type(enum bch_data_type data_type)
70{
71 return data_type == BCH_DATA_stripe ? BCH_DATA_user : data_type;
72}
73
74static inline unsigned bch2_bucket_sectors(struct bch_alloc_v4 a)
75{
76 return a.dirty_sectors + a.cached_sectors;
77}
78
79static inline unsigned bch2_bucket_sectors_dirty(struct bch_alloc_v4 a)
80{
81 return a.dirty_sectors;
82}
83
84static inline unsigned bch2_bucket_sectors_fragmented(struct bch_dev *ca,
85 struct bch_alloc_v4 a)
86{
87 int d = bch2_bucket_sectors_dirty(a);
88
89 return d ? max(0, ca->mi.bucket_size - d) : 0;
90}
91
92static inline u64 alloc_lru_idx_read(struct bch_alloc_v4 a)
93{
94 return a.data_type == BCH_DATA_cached ? a.io_time[READ] : 0;
95}
96
97#define DATA_TYPES_MOVABLE \
98 ((1U << BCH_DATA_btree)| \
99 (1U << BCH_DATA_user)| \
100 (1U << BCH_DATA_stripe))
101
102static inline bool data_type_movable(enum bch_data_type type)
103{
104 return (1U << type) & DATA_TYPES_MOVABLE;
105}
106
107static inline u64 alloc_lru_idx_fragmentation(struct bch_alloc_v4 a,
108 struct bch_dev *ca)
109{
110 if (!data_type_movable(a.data_type) ||
111 !bch2_bucket_sectors_fragmented(ca, a))
112 return 0;
113
114 u64 d = bch2_bucket_sectors_dirty(a);
115 return div_u64(d * (1ULL << 31), ca->mi.bucket_size);
116}
117
118static inline u64 alloc_freespace_genbits(struct bch_alloc_v4 a)
119{
120 return ((u64) alloc_gc_gen(a) >> 4) << 56;
121}
122
123static inline struct bpos alloc_freespace_pos(struct bpos pos, struct bch_alloc_v4 a)
124{
125 pos.offset |= alloc_freespace_genbits(a);
126 return pos;
127}
128
129static inline unsigned alloc_v4_u64s_noerror(const struct bch_alloc_v4 *a)
130{
131 return (BCH_ALLOC_V4_BACKPOINTERS_START(a) ?:
132 BCH_ALLOC_V4_U64s_V0) +
133 BCH_ALLOC_V4_NR_BACKPOINTERS(a) *
134 (sizeof(struct bch_backpointer) / sizeof(u64));
135}
136
137static inline unsigned alloc_v4_u64s(const struct bch_alloc_v4 *a)
138{
139 unsigned ret = alloc_v4_u64s_noerror(a);
140 BUG_ON(ret > U8_MAX - BKEY_U64s);
141 return ret;
142}
143
144static inline void set_alloc_v4_u64s(struct bkey_i_alloc_v4 *a)
145{
146 set_bkey_val_u64s(&a->k, alloc_v4_u64s(&a->v));
147}
148
149struct bkey_i_alloc_v4 *
150bch2_trans_start_alloc_update(struct btree_trans *, struct btree_iter *, struct bpos);
151
152void __bch2_alloc_to_v4(struct bkey_s_c, struct bch_alloc_v4 *);
153
154static inline const struct bch_alloc_v4 *bch2_alloc_to_v4(struct bkey_s_c k, struct bch_alloc_v4 *convert)
155{
156 const struct bch_alloc_v4 *ret;
157
158 if (unlikely(k.k->type != KEY_TYPE_alloc_v4))
159 goto slowpath;
160
161 ret = bkey_s_c_to_alloc_v4(k).v;
162 if (BCH_ALLOC_V4_BACKPOINTERS_START(ret) != BCH_ALLOC_V4_U64s)
163 goto slowpath;
164
165 return ret;
166slowpath:
167 __bch2_alloc_to_v4(k, convert);
168 return convert;
169}
170
171struct bkey_i_alloc_v4 *bch2_alloc_to_v4_mut(struct btree_trans *, struct bkey_s_c);
172
173int bch2_bucket_io_time_reset(struct btree_trans *, unsigned, size_t, int);
174
175int bch2_alloc_v1_invalid(struct bch_fs *, struct bkey_s_c,
176 enum bkey_invalid_flags, struct printbuf *);
177int bch2_alloc_v2_invalid(struct bch_fs *, struct bkey_s_c,
178 enum bkey_invalid_flags, struct printbuf *);
179int bch2_alloc_v3_invalid(struct bch_fs *, struct bkey_s_c,
180 enum bkey_invalid_flags, struct printbuf *);
181int bch2_alloc_v4_invalid(struct bch_fs *, struct bkey_s_c,
182 enum bkey_invalid_flags, struct printbuf *);
183void bch2_alloc_v4_swab(struct bkey_s);
184void bch2_alloc_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
185
186#define bch2_bkey_ops_alloc ((struct bkey_ops) { \
187 .key_invalid = bch2_alloc_v1_invalid, \
188 .val_to_text = bch2_alloc_to_text, \
189 .trigger = bch2_trigger_alloc, \
190 .min_val_size = 8, \
191})
192
193#define bch2_bkey_ops_alloc_v2 ((struct bkey_ops) { \
194 .key_invalid = bch2_alloc_v2_invalid, \
195 .val_to_text = bch2_alloc_to_text, \
196 .trigger = bch2_trigger_alloc, \
197 .min_val_size = 8, \
198})
199
200#define bch2_bkey_ops_alloc_v3 ((struct bkey_ops) { \
201 .key_invalid = bch2_alloc_v3_invalid, \
202 .val_to_text = bch2_alloc_to_text, \
203 .trigger = bch2_trigger_alloc, \
204 .min_val_size = 16, \
205})
206
207#define bch2_bkey_ops_alloc_v4 ((struct bkey_ops) { \
208 .key_invalid = bch2_alloc_v4_invalid, \
209 .val_to_text = bch2_alloc_to_text, \
210 .swab = bch2_alloc_v4_swab, \
211 .trigger = bch2_trigger_alloc, \
212 .min_val_size = 48, \
213})
214
215int bch2_bucket_gens_invalid(struct bch_fs *, struct bkey_s_c,
216 enum bkey_invalid_flags, struct printbuf *);
217void bch2_bucket_gens_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
218
219#define bch2_bkey_ops_bucket_gens ((struct bkey_ops) { \
220 .key_invalid = bch2_bucket_gens_invalid, \
221 .val_to_text = bch2_bucket_gens_to_text, \
222})
223
224int bch2_bucket_gens_init(struct bch_fs *);
225
226static inline bool bkey_is_alloc(const struct bkey *k)
227{
228 return k->type == KEY_TYPE_alloc ||
229 k->type == KEY_TYPE_alloc_v2 ||
230 k->type == KEY_TYPE_alloc_v3;
231}
232
233int bch2_alloc_read(struct bch_fs *);
234
235int bch2_trigger_alloc(struct btree_trans *, enum btree_id, unsigned,
236 struct bkey_s_c, struct bkey_s, unsigned);
237int bch2_check_alloc_info(struct bch_fs *);
238int bch2_check_alloc_to_lru_refs(struct bch_fs *);
239void bch2_do_discards(struct bch_fs *);
240
241static inline u64 should_invalidate_buckets(struct bch_dev *ca,
242 struct bch_dev_usage u)
243{
244 u64 want_free = ca->mi.nbuckets >> 7;
245 u64 free = max_t(s64, 0,
246 u.d[BCH_DATA_free].buckets
247 + u.d[BCH_DATA_need_discard].buckets
248 - bch2_dev_buckets_reserved(ca, BCH_WATERMARK_stripe));
249
250 return clamp_t(s64, want_free - free, 0, u.d[BCH_DATA_cached].buckets);
251}
252
253void bch2_do_invalidates(struct bch_fs *);
254
255static inline struct bch_backpointer *alloc_v4_backpointers(struct bch_alloc_v4 *a)
256{
257 return (void *) ((u64 *) &a->v +
258 (BCH_ALLOC_V4_BACKPOINTERS_START(a) ?:
259 BCH_ALLOC_V4_U64s_V0));
260}
261
262static inline const struct bch_backpointer *alloc_v4_backpointers_c(const struct bch_alloc_v4 *a)
263{
264 return (void *) ((u64 *) &a->v + BCH_ALLOC_V4_BACKPOINTERS_START(a));
265}
266
267int bch2_dev_freespace_init(struct bch_fs *, struct bch_dev *, u64, u64);
268int bch2_fs_freespace_init(struct bch_fs *);
269
270void bch2_recalc_capacity(struct bch_fs *);
271u64 bch2_min_rw_member_capacity(struct bch_fs *);
272
273void bch2_dev_allocator_remove(struct bch_fs *, struct bch_dev *);
274void bch2_dev_allocator_add(struct bch_fs *, struct bch_dev *);
275
276void bch2_fs_allocator_background_exit(struct bch_fs *);
277void bch2_fs_allocator_background_init(struct bch_fs *);
278
279#endif /* _BCACHEFS_ALLOC_BACKGROUND_H */