1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_ALLOC_BACKGROUND_H
3 #define _BCACHEFS_ALLOC_BACKGROUND_H
6 #include "alloc_types.h"
11 enum bch_validate_flags;
13 /* How out of date a pointer gen is allowed to be: */
14 #define BUCKET_GC_GEN_MAX 96U
16 static inline bool bch2_dev_bucket_exists(struct bch_fs *c, struct bpos pos)
19 struct bch_dev *ca = bch2_dev_rcu(c, pos.inode);
20 bool ret = ca && bucket_valid(ca, pos.offset);
25 static inline u64 bucket_to_u64(struct bpos bucket)
27 return (bucket.inode << 48) | bucket.offset;
30 static inline struct bpos u64_to_bucket(u64 bucket)
32 return POS(bucket >> 48, bucket & ~(~0ULL << 48));
35 static inline u8 alloc_gc_gen(struct bch_alloc_v4 a)
37 return a.gen - a.oldest_gen;
40 static inline void alloc_to_bucket(struct bucket *dst, struct bch_alloc_v4 src)
43 dst->data_type = src.data_type;
44 dst->dirty_sectors = src.dirty_sectors;
45 dst->cached_sectors = src.cached_sectors;
46 dst->stripe = src.stripe;
49 static inline void __bucket_m_to_alloc(struct bch_alloc_v4 *dst, struct bucket src)
52 dst->data_type = src.data_type;
53 dst->dirty_sectors = src.dirty_sectors;
54 dst->cached_sectors = src.cached_sectors;
55 dst->stripe = src.stripe;
58 static inline struct bch_alloc_v4 bucket_m_to_alloc(struct bucket b)
60 struct bch_alloc_v4 ret = {};
61 __bucket_m_to_alloc(&ret, b);
65 static inline enum bch_data_type bucket_data_type(enum bch_data_type data_type)
76 static inline bool bucket_data_type_mismatch(enum bch_data_type bucket,
77 enum bch_data_type ptr)
79 return !data_type_is_empty(bucket) &&
80 bucket_data_type(bucket) != bucket_data_type(ptr);
83 static inline unsigned bch2_bucket_sectors_total(struct bch_alloc_v4 a)
85 return a.dirty_sectors + a.cached_sectors;
88 static inline unsigned bch2_bucket_sectors_dirty(struct bch_alloc_v4 a)
90 return a.dirty_sectors;
93 static inline unsigned bch2_bucket_sectors_fragmented(struct bch_dev *ca,
94 struct bch_alloc_v4 a)
96 int d = bch2_bucket_sectors_dirty(a);
98 return d ? max(0, ca->mi.bucket_size - d) : 0;
101 static inline enum bch_data_type alloc_data_type(struct bch_alloc_v4 a,
102 enum bch_data_type data_type)
105 return data_type == BCH_DATA_parity ? data_type : BCH_DATA_stripe;
108 if (a.cached_sectors)
109 return BCH_DATA_cached;
110 if (BCH_ALLOC_V4_NEED_DISCARD(&a))
111 return BCH_DATA_need_discard;
112 if (alloc_gc_gen(a) >= BUCKET_GC_GEN_MAX)
113 return BCH_DATA_need_gc_gens;
114 return BCH_DATA_free;
117 static inline void alloc_data_type_set(struct bch_alloc_v4 *a, enum bch_data_type data_type)
119 a->data_type = alloc_data_type(*a, data_type);
122 static inline u64 alloc_lru_idx_read(struct bch_alloc_v4 a)
124 return a.data_type == BCH_DATA_cached ? a.io_time[READ] : 0;
127 #define DATA_TYPES_MOVABLE \
128 ((1U << BCH_DATA_btree)| \
129 (1U << BCH_DATA_user)| \
130 (1U << BCH_DATA_stripe))
132 static inline bool data_type_movable(enum bch_data_type type)
134 return (1U << type) & DATA_TYPES_MOVABLE;
137 static inline u64 alloc_lru_idx_fragmentation(struct bch_alloc_v4 a,
140 if (!data_type_movable(a.data_type) ||
141 !bch2_bucket_sectors_fragmented(ca, a))
144 u64 d = bch2_bucket_sectors_dirty(a);
145 return div_u64(d * (1ULL << 31), ca->mi.bucket_size);
148 static inline u64 alloc_freespace_genbits(struct bch_alloc_v4 a)
150 return ((u64) alloc_gc_gen(a) >> 4) << 56;
153 static inline struct bpos alloc_freespace_pos(struct bpos pos, struct bch_alloc_v4 a)
155 pos.offset |= alloc_freespace_genbits(a);
159 static inline unsigned alloc_v4_u64s_noerror(const struct bch_alloc_v4 *a)
161 return (BCH_ALLOC_V4_BACKPOINTERS_START(a) ?:
162 BCH_ALLOC_V4_U64s_V0) +
163 BCH_ALLOC_V4_NR_BACKPOINTERS(a) *
164 (sizeof(struct bch_backpointer) / sizeof(u64));
167 static inline unsigned alloc_v4_u64s(const struct bch_alloc_v4 *a)
169 unsigned ret = alloc_v4_u64s_noerror(a);
170 BUG_ON(ret > U8_MAX - BKEY_U64s);
174 static inline void set_alloc_v4_u64s(struct bkey_i_alloc_v4 *a)
176 set_bkey_val_u64s(&a->k, alloc_v4_u64s(&a->v));
179 struct bkey_i_alloc_v4 *
180 bch2_trans_start_alloc_update_noupdate(struct btree_trans *, struct btree_iter *, struct bpos);
181 struct bkey_i_alloc_v4 *
182 bch2_trans_start_alloc_update(struct btree_trans *, struct bpos);
184 void __bch2_alloc_to_v4(struct bkey_s_c, struct bch_alloc_v4 *);
186 static inline const struct bch_alloc_v4 *bch2_alloc_to_v4(struct bkey_s_c k, struct bch_alloc_v4 *convert)
188 const struct bch_alloc_v4 *ret;
190 if (unlikely(k.k->type != KEY_TYPE_alloc_v4))
193 ret = bkey_s_c_to_alloc_v4(k).v;
194 if (BCH_ALLOC_V4_BACKPOINTERS_START(ret) != BCH_ALLOC_V4_U64s)
199 __bch2_alloc_to_v4(k, convert);
203 struct bkey_i_alloc_v4 *bch2_alloc_to_v4_mut(struct btree_trans *, struct bkey_s_c);
205 int bch2_bucket_io_time_reset(struct btree_trans *, unsigned, size_t, int);
207 int bch2_alloc_v1_invalid(struct bch_fs *, struct bkey_s_c,
208 enum bch_validate_flags, struct printbuf *);
209 int bch2_alloc_v2_invalid(struct bch_fs *, struct bkey_s_c,
210 enum bch_validate_flags, struct printbuf *);
211 int bch2_alloc_v3_invalid(struct bch_fs *, struct bkey_s_c,
212 enum bch_validate_flags, struct printbuf *);
213 int bch2_alloc_v4_invalid(struct bch_fs *, struct bkey_s_c,
214 enum bch_validate_flags, struct printbuf *);
215 void bch2_alloc_v4_swab(struct bkey_s);
216 void bch2_alloc_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
218 #define bch2_bkey_ops_alloc ((struct bkey_ops) { \
219 .key_invalid = bch2_alloc_v1_invalid, \
220 .val_to_text = bch2_alloc_to_text, \
221 .trigger = bch2_trigger_alloc, \
225 #define bch2_bkey_ops_alloc_v2 ((struct bkey_ops) { \
226 .key_invalid = bch2_alloc_v2_invalid, \
227 .val_to_text = bch2_alloc_to_text, \
228 .trigger = bch2_trigger_alloc, \
232 #define bch2_bkey_ops_alloc_v3 ((struct bkey_ops) { \
233 .key_invalid = bch2_alloc_v3_invalid, \
234 .val_to_text = bch2_alloc_to_text, \
235 .trigger = bch2_trigger_alloc, \
236 .min_val_size = 16, \
239 #define bch2_bkey_ops_alloc_v4 ((struct bkey_ops) { \
240 .key_invalid = bch2_alloc_v4_invalid, \
241 .val_to_text = bch2_alloc_to_text, \
242 .swab = bch2_alloc_v4_swab, \
243 .trigger = bch2_trigger_alloc, \
244 .min_val_size = 48, \
247 int bch2_bucket_gens_invalid(struct bch_fs *, struct bkey_s_c,
248 enum bch_validate_flags, struct printbuf *);
249 void bch2_bucket_gens_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
251 #define bch2_bkey_ops_bucket_gens ((struct bkey_ops) { \
252 .key_invalid = bch2_bucket_gens_invalid, \
253 .val_to_text = bch2_bucket_gens_to_text, \
256 int bch2_bucket_gens_init(struct bch_fs *);
258 static inline bool bkey_is_alloc(const struct bkey *k)
260 return k->type == KEY_TYPE_alloc ||
261 k->type == KEY_TYPE_alloc_v2 ||
262 k->type == KEY_TYPE_alloc_v3;
265 int bch2_alloc_read(struct bch_fs *);
267 int bch2_trigger_alloc(struct btree_trans *, enum btree_id, unsigned,
268 struct bkey_s_c, struct bkey_s,
269 enum btree_iter_update_trigger_flags);
270 int bch2_check_alloc_info(struct bch_fs *);
271 int bch2_check_alloc_to_lru_refs(struct bch_fs *);
272 void bch2_do_discards(struct bch_fs *);
274 static inline u64 should_invalidate_buckets(struct bch_dev *ca,
275 struct bch_dev_usage u)
277 u64 want_free = ca->mi.nbuckets >> 7;
278 u64 free = max_t(s64, 0,
279 u.d[BCH_DATA_free].buckets
280 + u.d[BCH_DATA_need_discard].buckets
281 - bch2_dev_buckets_reserved(ca, BCH_WATERMARK_stripe));
283 return clamp_t(s64, want_free - free, 0, u.d[BCH_DATA_cached].buckets);
286 void bch2_do_invalidates(struct bch_fs *);
288 static inline struct bch_backpointer *alloc_v4_backpointers(struct bch_alloc_v4 *a)
290 return (void *) ((u64 *) &a->v +
291 (BCH_ALLOC_V4_BACKPOINTERS_START(a) ?:
292 BCH_ALLOC_V4_U64s_V0));
295 static inline const struct bch_backpointer *alloc_v4_backpointers_c(const struct bch_alloc_v4 *a)
297 return (void *) ((u64 *) &a->v + BCH_ALLOC_V4_BACKPOINTERS_START(a));
300 int bch2_dev_freespace_init(struct bch_fs *, struct bch_dev *, u64, u64);
301 int bch2_fs_freespace_init(struct bch_fs *);
303 void bch2_recalc_capacity(struct bch_fs *);
304 u64 bch2_min_rw_member_capacity(struct bch_fs *);
306 void bch2_dev_allocator_remove(struct bch_fs *, struct bch_dev *);
307 void bch2_dev_allocator_add(struct bch_fs *, struct bch_dev *);
309 void bch2_fs_allocator_background_exit(struct bch_fs *);
310 void bch2_fs_allocator_background_init(struct bch_fs *);
312 #endif /* _BCACHEFS_ALLOC_BACKGROUND_H */