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))
145 * avoid overflowing LRU_TIME_BITS on a corrupted fs, when
146 * bucket_sectors_dirty is (much) bigger than bucket_size
148 u64 d = min(bch2_bucket_sectors_dirty(a),
151 return div_u64(d * (1ULL << 31), ca->mi.bucket_size);
154 static inline u64 alloc_freespace_genbits(struct bch_alloc_v4 a)
156 return ((u64) alloc_gc_gen(a) >> 4) << 56;
159 static inline struct bpos alloc_freespace_pos(struct bpos pos, struct bch_alloc_v4 a)
161 pos.offset |= alloc_freespace_genbits(a);
165 static inline unsigned alloc_v4_u64s_noerror(const struct bch_alloc_v4 *a)
167 return (BCH_ALLOC_V4_BACKPOINTERS_START(a) ?:
168 BCH_ALLOC_V4_U64s_V0) +
169 BCH_ALLOC_V4_NR_BACKPOINTERS(a) *
170 (sizeof(struct bch_backpointer) / sizeof(u64));
173 static inline unsigned alloc_v4_u64s(const struct bch_alloc_v4 *a)
175 unsigned ret = alloc_v4_u64s_noerror(a);
176 BUG_ON(ret > U8_MAX - BKEY_U64s);
180 static inline void set_alloc_v4_u64s(struct bkey_i_alloc_v4 *a)
182 set_bkey_val_u64s(&a->k, alloc_v4_u64s(&a->v));
185 struct bkey_i_alloc_v4 *
186 bch2_trans_start_alloc_update_noupdate(struct btree_trans *, struct btree_iter *, struct bpos);
187 struct bkey_i_alloc_v4 *
188 bch2_trans_start_alloc_update(struct btree_trans *, struct bpos);
190 void __bch2_alloc_to_v4(struct bkey_s_c, struct bch_alloc_v4 *);
192 static inline const struct bch_alloc_v4 *bch2_alloc_to_v4(struct bkey_s_c k, struct bch_alloc_v4 *convert)
194 const struct bch_alloc_v4 *ret;
196 if (unlikely(k.k->type != KEY_TYPE_alloc_v4))
199 ret = bkey_s_c_to_alloc_v4(k).v;
200 if (BCH_ALLOC_V4_BACKPOINTERS_START(ret) != BCH_ALLOC_V4_U64s)
205 __bch2_alloc_to_v4(k, convert);
209 struct bkey_i_alloc_v4 *bch2_alloc_to_v4_mut(struct btree_trans *, struct bkey_s_c);
211 int bch2_bucket_io_time_reset(struct btree_trans *, unsigned, size_t, int);
213 int bch2_alloc_v1_invalid(struct bch_fs *, struct bkey_s_c,
214 enum bch_validate_flags, struct printbuf *);
215 int bch2_alloc_v2_invalid(struct bch_fs *, struct bkey_s_c,
216 enum bch_validate_flags, struct printbuf *);
217 int bch2_alloc_v3_invalid(struct bch_fs *, struct bkey_s_c,
218 enum bch_validate_flags, struct printbuf *);
219 int bch2_alloc_v4_invalid(struct bch_fs *, struct bkey_s_c,
220 enum bch_validate_flags, struct printbuf *);
221 void bch2_alloc_v4_swab(struct bkey_s);
222 void bch2_alloc_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
224 #define bch2_bkey_ops_alloc ((struct bkey_ops) { \
225 .key_invalid = bch2_alloc_v1_invalid, \
226 .val_to_text = bch2_alloc_to_text, \
227 .trigger = bch2_trigger_alloc, \
231 #define bch2_bkey_ops_alloc_v2 ((struct bkey_ops) { \
232 .key_invalid = bch2_alloc_v2_invalid, \
233 .val_to_text = bch2_alloc_to_text, \
234 .trigger = bch2_trigger_alloc, \
238 #define bch2_bkey_ops_alloc_v3 ((struct bkey_ops) { \
239 .key_invalid = bch2_alloc_v3_invalid, \
240 .val_to_text = bch2_alloc_to_text, \
241 .trigger = bch2_trigger_alloc, \
242 .min_val_size = 16, \
245 #define bch2_bkey_ops_alloc_v4 ((struct bkey_ops) { \
246 .key_invalid = bch2_alloc_v4_invalid, \
247 .val_to_text = bch2_alloc_to_text, \
248 .swab = bch2_alloc_v4_swab, \
249 .trigger = bch2_trigger_alloc, \
250 .min_val_size = 48, \
253 int bch2_bucket_gens_invalid(struct bch_fs *, struct bkey_s_c,
254 enum bch_validate_flags, struct printbuf *);
255 void bch2_bucket_gens_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
257 #define bch2_bkey_ops_bucket_gens ((struct bkey_ops) { \
258 .key_invalid = bch2_bucket_gens_invalid, \
259 .val_to_text = bch2_bucket_gens_to_text, \
262 int bch2_bucket_gens_init(struct bch_fs *);
264 static inline bool bkey_is_alloc(const struct bkey *k)
266 return k->type == KEY_TYPE_alloc ||
267 k->type == KEY_TYPE_alloc_v2 ||
268 k->type == KEY_TYPE_alloc_v3;
271 int bch2_alloc_read(struct bch_fs *);
273 int bch2_trigger_alloc(struct btree_trans *, enum btree_id, unsigned,
274 struct bkey_s_c, struct bkey_s,
275 enum btree_iter_update_trigger_flags);
276 int bch2_check_alloc_info(struct bch_fs *);
277 int bch2_check_alloc_to_lru_refs(struct bch_fs *);
278 void bch2_dev_do_discards(struct bch_dev *);
279 void bch2_do_discards(struct bch_fs *);
281 static inline u64 should_invalidate_buckets(struct bch_dev *ca,
282 struct bch_dev_usage u)
284 u64 want_free = ca->mi.nbuckets >> 7;
285 u64 free = max_t(s64, 0,
286 u.d[BCH_DATA_free].buckets
287 + u.d[BCH_DATA_need_discard].buckets
288 - bch2_dev_buckets_reserved(ca, BCH_WATERMARK_stripe));
290 return clamp_t(s64, want_free - free, 0, u.d[BCH_DATA_cached].buckets);
293 void bch2_dev_do_invalidates(struct bch_dev *);
294 void bch2_do_invalidates(struct bch_fs *);
296 static inline struct bch_backpointer *alloc_v4_backpointers(struct bch_alloc_v4 *a)
298 return (void *) ((u64 *) &a->v +
299 (BCH_ALLOC_V4_BACKPOINTERS_START(a) ?:
300 BCH_ALLOC_V4_U64s_V0));
303 static inline const struct bch_backpointer *alloc_v4_backpointers_c(const struct bch_alloc_v4 *a)
305 return (void *) ((u64 *) &a->v + BCH_ALLOC_V4_BACKPOINTERS_START(a));
308 int bch2_dev_freespace_init(struct bch_fs *, struct bch_dev *, u64, u64);
309 int bch2_fs_freespace_init(struct bch_fs *);
311 void bch2_recalc_capacity(struct bch_fs *);
312 u64 bch2_min_rw_member_capacity(struct bch_fs *);
314 void bch2_dev_allocator_remove(struct bch_fs *, struct bch_dev *);
315 void bch2_dev_allocator_add(struct bch_fs *, struct bch_dev *);
317 void bch2_dev_allocator_background_exit(struct bch_dev *);
318 void bch2_dev_allocator_background_init(struct bch_dev *);
320 void bch2_fs_allocator_background_init(struct bch_fs *);
322 #endif /* _BCACHEFS_ALLOC_BACKGROUND_H */