]> Git Repo - linux.git/blob - fs/bcachefs/alloc_background.h
Merge patch series "riscv: Extension parsing fixes"
[linux.git] / fs / bcachefs / alloc_background.h
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
11 enum bch_validate_flags;
12
13 /* How out of date a pointer gen is allowed to be: */
14 #define BUCKET_GC_GEN_MAX       96U
15
16 static inline bool bch2_dev_bucket_exists(struct bch_fs *c, struct bpos pos)
17 {
18         rcu_read_lock();
19         struct bch_dev *ca = bch2_dev_rcu(c, pos.inode);
20         bool ret = ca && bucket_valid(ca, pos.offset);
21         rcu_read_unlock();
22         return ret;
23 }
24
25 static inline u64 bucket_to_u64(struct bpos bucket)
26 {
27         return (bucket.inode << 48) | bucket.offset;
28 }
29
30 static inline struct bpos u64_to_bucket(u64 bucket)
31 {
32         return POS(bucket >> 48, bucket & ~(~0ULL << 48));
33 }
34
35 static inline u8 alloc_gc_gen(struct bch_alloc_v4 a)
36 {
37         return a.gen - a.oldest_gen;
38 }
39
40 static inline void alloc_to_bucket(struct bucket *dst, struct bch_alloc_v4 src)
41 {
42         dst->gen                = src.gen;
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;
47 }
48
49 static inline void __bucket_m_to_alloc(struct bch_alloc_v4 *dst, struct bucket src)
50 {
51         dst->gen                = src.gen;
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;
56 }
57
58 static inline struct bch_alloc_v4 bucket_m_to_alloc(struct bucket b)
59 {
60         struct bch_alloc_v4 ret = {};
61         __bucket_m_to_alloc(&ret, b);
62         return ret;
63 }
64
65 static inline enum bch_data_type bucket_data_type(enum bch_data_type data_type)
66 {
67         switch (data_type) {
68         case BCH_DATA_cached:
69         case BCH_DATA_stripe:
70                 return BCH_DATA_user;
71         default:
72                 return data_type;
73         }
74 }
75
76 static inline bool bucket_data_type_mismatch(enum bch_data_type bucket,
77                                              enum bch_data_type ptr)
78 {
79         return !data_type_is_empty(bucket) &&
80                 bucket_data_type(bucket) != bucket_data_type(ptr);
81 }
82
83 static inline unsigned bch2_bucket_sectors_total(struct bch_alloc_v4 a)
84 {
85         return a.dirty_sectors + a.cached_sectors;
86 }
87
88 static inline unsigned bch2_bucket_sectors_dirty(struct bch_alloc_v4 a)
89 {
90         return a.dirty_sectors;
91 }
92
93 static inline unsigned bch2_bucket_sectors_fragmented(struct bch_dev *ca,
94                                                  struct bch_alloc_v4 a)
95 {
96         int d = bch2_bucket_sectors_dirty(a);
97
98         return d ? max(0, ca->mi.bucket_size - d) : 0;
99 }
100
101 static inline enum bch_data_type alloc_data_type(struct bch_alloc_v4 a,
102                                                  enum bch_data_type data_type)
103 {
104         if (a.stripe)
105                 return data_type == BCH_DATA_parity ? data_type : BCH_DATA_stripe;
106         if (a.dirty_sectors)
107                 return data_type;
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;
115 }
116
117 static inline void alloc_data_type_set(struct bch_alloc_v4 *a, enum bch_data_type data_type)
118 {
119         a->data_type = alloc_data_type(*a, data_type);
120 }
121
122 static inline u64 alloc_lru_idx_read(struct bch_alloc_v4 a)
123 {
124         return a.data_type == BCH_DATA_cached ? a.io_time[READ] : 0;
125 }
126
127 #define DATA_TYPES_MOVABLE              \
128         ((1U << BCH_DATA_btree)|        \
129          (1U << BCH_DATA_user)|         \
130          (1U << BCH_DATA_stripe))
131
132 static inline bool data_type_movable(enum bch_data_type type)
133 {
134         return (1U << type) & DATA_TYPES_MOVABLE;
135 }
136
137 static inline u64 alloc_lru_idx_fragmentation(struct bch_alloc_v4 a,
138                                               struct bch_dev *ca)
139 {
140         if (!data_type_movable(a.data_type) ||
141             !bch2_bucket_sectors_fragmented(ca, a))
142                 return 0;
143
144         u64 d = bch2_bucket_sectors_dirty(a);
145         return div_u64(d * (1ULL << 31), ca->mi.bucket_size);
146 }
147
148 static inline u64 alloc_freespace_genbits(struct bch_alloc_v4 a)
149 {
150         return ((u64) alloc_gc_gen(a) >> 4) << 56;
151 }
152
153 static inline struct bpos alloc_freespace_pos(struct bpos pos, struct bch_alloc_v4 a)
154 {
155         pos.offset |= alloc_freespace_genbits(a);
156         return pos;
157 }
158
159 static inline unsigned alloc_v4_u64s_noerror(const struct bch_alloc_v4 *a)
160 {
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));
165 }
166
167 static inline unsigned alloc_v4_u64s(const struct bch_alloc_v4 *a)
168 {
169         unsigned ret = alloc_v4_u64s_noerror(a);
170         BUG_ON(ret > U8_MAX - BKEY_U64s);
171         return ret;
172 }
173
174 static inline void set_alloc_v4_u64s(struct bkey_i_alloc_v4 *a)
175 {
176         set_bkey_val_u64s(&a->k, alloc_v4_u64s(&a->v));
177 }
178
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);
183
184 void __bch2_alloc_to_v4(struct bkey_s_c, struct bch_alloc_v4 *);
185
186 static inline const struct bch_alloc_v4 *bch2_alloc_to_v4(struct bkey_s_c k, struct bch_alloc_v4 *convert)
187 {
188         const struct bch_alloc_v4 *ret;
189
190         if (unlikely(k.k->type != KEY_TYPE_alloc_v4))
191                 goto slowpath;
192
193         ret = bkey_s_c_to_alloc_v4(k).v;
194         if (BCH_ALLOC_V4_BACKPOINTERS_START(ret) != BCH_ALLOC_V4_U64s)
195                 goto slowpath;
196
197         return ret;
198 slowpath:
199         __bch2_alloc_to_v4(k, convert);
200         return convert;
201 }
202
203 struct bkey_i_alloc_v4 *bch2_alloc_to_v4_mut(struct btree_trans *, struct bkey_s_c);
204
205 int bch2_bucket_io_time_reset(struct btree_trans *, unsigned, size_t, int);
206
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);
217
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,           \
222         .min_val_size   = 8,                            \
223 })
224
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,           \
229         .min_val_size   = 8,                            \
230 })
231
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,                           \
237 })
238
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,                           \
245 })
246
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);
250
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,     \
254 })
255
256 int bch2_bucket_gens_init(struct bch_fs *);
257
258 static inline bool bkey_is_alloc(const struct bkey *k)
259 {
260         return  k->type == KEY_TYPE_alloc ||
261                 k->type == KEY_TYPE_alloc_v2 ||
262                 k->type == KEY_TYPE_alloc_v3;
263 }
264
265 int bch2_alloc_read(struct bch_fs *);
266
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 *);
273
274 static inline u64 should_invalidate_buckets(struct bch_dev *ca,
275                                             struct bch_dev_usage u)
276 {
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));
282
283         return clamp_t(s64, want_free - free, 0, u.d[BCH_DATA_cached].buckets);
284 }
285
286 void bch2_do_invalidates(struct bch_fs *);
287
288 static inline struct bch_backpointer *alloc_v4_backpointers(struct bch_alloc_v4 *a)
289 {
290         return (void *) ((u64 *) &a->v +
291                          (BCH_ALLOC_V4_BACKPOINTERS_START(a) ?:
292                           BCH_ALLOC_V4_U64s_V0));
293 }
294
295 static inline const struct bch_backpointer *alloc_v4_backpointers_c(const struct bch_alloc_v4 *a)
296 {
297         return (void *) ((u64 *) &a->v + BCH_ALLOC_V4_BACKPOINTERS_START(a));
298 }
299
300 int bch2_dev_freespace_init(struct bch_fs *, struct bch_dev *, u64, u64);
301 int bch2_fs_freespace_init(struct bch_fs *);
302
303 void bch2_recalc_capacity(struct bch_fs *);
304 u64 bch2_min_rw_member_capacity(struct bch_fs *);
305
306 void bch2_dev_allocator_remove(struct bch_fs *, struct bch_dev *);
307 void bch2_dev_allocator_add(struct bch_fs *, struct bch_dev *);
308
309 void bch2_fs_allocator_background_exit(struct bch_fs *);
310 void bch2_fs_allocator_background_init(struct bch_fs *);
311
312 #endif /* _BCACHEFS_ALLOC_BACKGROUND_H */
This page took 0.106172 seconds and 4 git commands to generate.