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 /* How out of date a pointer gen is allowed to be: */
12 #define BUCKET_GC_GEN_MAX 96U
14 static inline bool bch2_dev_bucket_exists(struct bch_fs *c, struct bpos pos)
18 if (!bch2_dev_exists2(c, pos.inode))
21 ca = bch_dev_bkey_exists(c, pos.inode);
22 return pos.offset >= ca->mi.first_bucket &&
23 pos.offset < ca->mi.nbuckets;
26 static inline u64 bucket_to_u64(struct bpos bucket)
28 return (bucket.inode << 48) | bucket.offset;
31 static inline struct bpos u64_to_bucket(u64 bucket)
33 return POS(bucket >> 48, bucket & ~(~0ULL << 48));
36 static inline u8 alloc_gc_gen(struct bch_alloc_v4 a)
38 return a.gen - a.oldest_gen;
41 static inline enum bch_data_type __alloc_data_type(u32 dirty_sectors,
44 struct bch_alloc_v4 a,
45 enum bch_data_type data_type)
50 return BCH_DATA_stripe;
52 return BCH_DATA_cached;
53 if (BCH_ALLOC_V4_NEED_DISCARD(&a))
54 return BCH_DATA_need_discard;
55 if (alloc_gc_gen(a) >= BUCKET_GC_GEN_MAX)
56 return BCH_DATA_need_gc_gens;
60 static inline enum bch_data_type alloc_data_type(struct bch_alloc_v4 a,
61 enum bch_data_type data_type)
63 return __alloc_data_type(a.dirty_sectors, a.cached_sectors,
64 a.stripe, a, data_type);
67 static inline u64 alloc_lru_idx_read(struct bch_alloc_v4 a)
69 return a.data_type == BCH_DATA_cached ? a.io_time[READ] : 0;
72 static inline u64 alloc_lru_idx_fragmentation(struct bch_alloc_v4 a,
75 if (a.data_type != BCH_DATA_btree &&
76 a.data_type != BCH_DATA_user)
79 if (a.dirty_sectors >= ca->mi.bucket_size)
82 return div_u64((u64) a.dirty_sectors * (1ULL << 31), ca->mi.bucket_size);
85 static inline u64 alloc_freespace_genbits(struct bch_alloc_v4 a)
87 return ((u64) alloc_gc_gen(a) >> 4) << 56;
90 static inline struct bpos alloc_freespace_pos(struct bpos pos, struct bch_alloc_v4 a)
92 pos.offset |= alloc_freespace_genbits(a);
96 static inline unsigned alloc_v4_u64s(const struct bch_alloc_v4 *a)
98 unsigned ret = (BCH_ALLOC_V4_BACKPOINTERS_START(a) ?:
99 BCH_ALLOC_V4_U64s_V0) +
100 BCH_ALLOC_V4_NR_BACKPOINTERS(a) *
101 (sizeof(struct bch_backpointer) / sizeof(u64));
103 BUG_ON(ret > U8_MAX - BKEY_U64s);
107 static inline void set_alloc_v4_u64s(struct bkey_i_alloc_v4 *a)
109 set_bkey_val_u64s(&a->k, alloc_v4_u64s(&a->v));
112 struct bkey_i_alloc_v4 *
113 bch2_trans_start_alloc_update(struct btree_trans *, struct btree_iter *, struct bpos);
115 void __bch2_alloc_to_v4(struct bkey_s_c, struct bch_alloc_v4 *);
117 static inline const struct bch_alloc_v4 *bch2_alloc_to_v4(struct bkey_s_c k, struct bch_alloc_v4 *convert)
119 const struct bch_alloc_v4 *ret;
121 if (unlikely(k.k->type != KEY_TYPE_alloc_v4))
124 ret = bkey_s_c_to_alloc_v4(k).v;
125 if (BCH_ALLOC_V4_BACKPOINTERS_START(ret) != BCH_ALLOC_V4_U64s)
130 __bch2_alloc_to_v4(k, convert);
134 struct bkey_i_alloc_v4 *bch2_alloc_to_v4_mut(struct btree_trans *, struct bkey_s_c);
136 int bch2_bucket_io_time_reset(struct btree_trans *, unsigned, size_t, int);
138 int bch2_alloc_v1_invalid(const struct bch_fs *, struct bkey_s_c, unsigned, struct printbuf *);
139 int bch2_alloc_v2_invalid(const struct bch_fs *, struct bkey_s_c, unsigned, struct printbuf *);
140 int bch2_alloc_v3_invalid(const struct bch_fs *, struct bkey_s_c, unsigned, struct printbuf *);
141 int bch2_alloc_v4_invalid(const struct bch_fs *, struct bkey_s_c, unsigned, struct printbuf *);
142 void bch2_alloc_v4_swab(struct bkey_s);
143 void bch2_alloc_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
145 #define bch2_bkey_ops_alloc ((struct bkey_ops) { \
146 .key_invalid = bch2_alloc_v1_invalid, \
147 .val_to_text = bch2_alloc_to_text, \
148 .trans_trigger = bch2_trans_mark_alloc, \
149 .atomic_trigger = bch2_mark_alloc, \
152 #define bch2_bkey_ops_alloc_v2 ((struct bkey_ops) { \
153 .key_invalid = bch2_alloc_v2_invalid, \
154 .val_to_text = bch2_alloc_to_text, \
155 .trans_trigger = bch2_trans_mark_alloc, \
156 .atomic_trigger = bch2_mark_alloc, \
159 #define bch2_bkey_ops_alloc_v3 ((struct bkey_ops) { \
160 .key_invalid = bch2_alloc_v3_invalid, \
161 .val_to_text = bch2_alloc_to_text, \
162 .trans_trigger = bch2_trans_mark_alloc, \
163 .atomic_trigger = bch2_mark_alloc, \
166 #define bch2_bkey_ops_alloc_v4 ((struct bkey_ops) { \
167 .key_invalid = bch2_alloc_v4_invalid, \
168 .val_to_text = bch2_alloc_to_text, \
169 .swab = bch2_alloc_v4_swab, \
170 .trans_trigger = bch2_trans_mark_alloc, \
171 .atomic_trigger = bch2_mark_alloc, \
174 int bch2_bucket_gens_invalid(const struct bch_fs *, struct bkey_s_c, unsigned, struct printbuf *);
175 void bch2_bucket_gens_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
177 #define bch2_bkey_ops_bucket_gens ((struct bkey_ops) { \
178 .key_invalid = bch2_bucket_gens_invalid, \
179 .val_to_text = bch2_bucket_gens_to_text, \
182 int bch2_bucket_gens_init(struct bch_fs *);
184 static inline bool bkey_is_alloc(const struct bkey *k)
186 return k->type == KEY_TYPE_alloc ||
187 k->type == KEY_TYPE_alloc_v2 ||
188 k->type == KEY_TYPE_alloc_v3;
191 int bch2_alloc_read(struct bch_fs *);
192 int bch2_bucket_gens_read(struct bch_fs *);
194 int bch2_trans_mark_alloc(struct btree_trans *, enum btree_id, unsigned,
195 struct bkey_s_c, struct bkey_i *, unsigned);
196 int bch2_check_alloc_info(struct bch_fs *);
197 int bch2_check_alloc_to_lru_refs(struct bch_fs *);
198 void bch2_do_discards(struct bch_fs *);
200 static inline u64 should_invalidate_buckets(struct bch_dev *ca,
201 struct bch_dev_usage u)
203 u64 want_free = ca->mi.nbuckets >> 7;
204 u64 free = max_t(s64, 0,
205 u.d[BCH_DATA_free].buckets
206 + u.d[BCH_DATA_need_discard].buckets
207 - bch2_dev_buckets_reserved(ca, RESERVE_none));
209 return clamp_t(s64, want_free - free, 0, u.d[BCH_DATA_cached].buckets);
212 void bch2_do_invalidates(struct bch_fs *);
214 static inline struct bch_backpointer *alloc_v4_backpointers(struct bch_alloc_v4 *a)
216 return (void *) ((u64 *) &a->v +
217 (BCH_ALLOC_V4_BACKPOINTERS_START(a) ?:
218 BCH_ALLOC_V4_U64s_V0));
221 static inline const struct bch_backpointer *alloc_v4_backpointers_c(const struct bch_alloc_v4 *a)
223 return (void *) ((u64 *) &a->v + BCH_ALLOC_V4_BACKPOINTERS_START(a));
226 int bch2_fs_freespace_init(struct bch_fs *);
228 void bch2_recalc_capacity(struct bch_fs *);
230 void bch2_dev_allocator_remove(struct bch_fs *, struct bch_dev *);
231 void bch2_dev_allocator_add(struct bch_fs *, struct bch_dev *);
233 void bch2_fs_allocator_background_init(struct bch_fs *);
235 #endif /* _BCACHEFS_ALLOC_BACKGROUND_H */