]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/alloc_background.h
Update bcachefs sources to dab31ca168 bcachefs: Add some logging for btree node rewri...
[bcachefs-tools-debian] / libbcachefs / 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 /* How out of date a pointer gen is allowed to be: */
12 #define BUCKET_GC_GEN_MAX       96U
13
14 static inline bool bch2_dev_bucket_exists(struct bch_fs *c, struct bpos pos)
15 {
16         struct bch_dev *ca;
17
18         if (!bch2_dev_exists2(c, pos.inode))
19                 return false;
20
21         ca = bch_dev_bkey_exists(c, pos.inode);
22         return pos.offset >= ca->mi.first_bucket &&
23                 pos.offset < ca->mi.nbuckets;
24 }
25
26 static inline u64 bucket_to_u64(struct bpos bucket)
27 {
28         return (bucket.inode << 48) | bucket.offset;
29 }
30
31 static inline struct bpos u64_to_bucket(u64 bucket)
32 {
33         return POS(bucket >> 48, bucket & ~(~0ULL << 48));
34 }
35
36 static inline u8 alloc_gc_gen(struct bch_alloc_v4 a)
37 {
38         return a.gen - a.oldest_gen;
39 }
40
41 static inline enum bch_data_type __alloc_data_type(u32 dirty_sectors,
42                                                    u32 cached_sectors,
43                                                    u32 stripe,
44                                                    struct bch_alloc_v4 a,
45                                                    enum bch_data_type data_type)
46 {
47         if (dirty_sectors)
48                 return data_type;
49         if (stripe)
50                 return BCH_DATA_stripe;
51         if (cached_sectors)
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;
57         return BCH_DATA_free;
58 }
59
60 static inline enum bch_data_type alloc_data_type(struct bch_alloc_v4 a,
61                                                  enum bch_data_type data_type)
62 {
63         return __alloc_data_type(a.dirty_sectors, a.cached_sectors,
64                                  a.stripe, a, data_type);
65 }
66
67 static inline u64 alloc_lru_idx(struct bch_alloc_v4 a)
68 {
69         return a.data_type == BCH_DATA_cached ? a.io_time[READ] : 0;
70 }
71
72 static inline u64 alloc_freespace_genbits(struct bch_alloc_v4 a)
73 {
74         return ((u64) alloc_gc_gen(a) >> 4) << 56;
75 }
76
77 static inline struct bpos alloc_freespace_pos(struct bpos pos, struct bch_alloc_v4 a)
78 {
79         pos.offset |= alloc_freespace_genbits(a);
80         return pos;
81 }
82
83 static inline unsigned alloc_v4_u64s(const struct bch_alloc_v4 *a)
84 {
85         unsigned ret = (BCH_ALLOC_V4_BACKPOINTERS_START(a) ?:
86                         BCH_ALLOC_V4_U64s_V0) +
87                 BCH_ALLOC_V4_NR_BACKPOINTERS(a) *
88                 (sizeof(struct bch_backpointer) / sizeof(u64));
89
90         BUG_ON(ret > U8_MAX - BKEY_U64s);
91         return ret;
92 }
93
94 static inline void set_alloc_v4_u64s(struct bkey_i_alloc_v4 *a)
95 {
96         set_bkey_val_u64s(&a->k, alloc_v4_u64s(&a->v));
97 }
98
99 struct bkey_i_alloc_v4 *
100 bch2_trans_start_alloc_update(struct btree_trans *, struct btree_iter *, struct bpos);
101
102 void __bch2_alloc_to_v4(struct bkey_s_c, struct bch_alloc_v4 *);
103
104 static inline const struct bch_alloc_v4 *bch2_alloc_to_v4(struct bkey_s_c k, struct bch_alloc_v4 *convert)
105 {
106         const struct bch_alloc_v4 *ret;
107
108         if (unlikely(k.k->type != KEY_TYPE_alloc_v4))
109                 goto slowpath;
110
111         ret = bkey_s_c_to_alloc_v4(k).v;
112         if (BCH_ALLOC_V4_BACKPOINTERS_START(ret) != BCH_ALLOC_V4_U64s)
113                 goto slowpath;
114
115         return ret;
116 slowpath:
117         __bch2_alloc_to_v4(k, convert);
118         return convert;
119 }
120
121 struct bkey_i_alloc_v4 *bch2_alloc_to_v4_mut(struct btree_trans *, struct bkey_s_c);
122
123 int bch2_bucket_io_time_reset(struct btree_trans *, unsigned, size_t, int);
124
125 int bch2_alloc_v1_invalid(const struct bch_fs *, struct bkey_s_c, unsigned, struct printbuf *);
126 int bch2_alloc_v2_invalid(const struct bch_fs *, struct bkey_s_c, unsigned, struct printbuf *);
127 int bch2_alloc_v3_invalid(const struct bch_fs *, struct bkey_s_c, unsigned, struct printbuf *);
128 int bch2_alloc_v4_invalid(const struct bch_fs *, struct bkey_s_c, unsigned, struct printbuf *);
129 void bch2_alloc_v4_swab(struct bkey_s);
130 void bch2_alloc_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
131
132 #define bch2_bkey_ops_alloc ((struct bkey_ops) {        \
133         .key_invalid    = bch2_alloc_v1_invalid,        \
134         .val_to_text    = bch2_alloc_to_text,           \
135         .trans_trigger  = bch2_trans_mark_alloc,        \
136         .atomic_trigger = bch2_mark_alloc,              \
137 })
138
139 #define bch2_bkey_ops_alloc_v2 ((struct bkey_ops) {     \
140         .key_invalid    = bch2_alloc_v2_invalid,        \
141         .val_to_text    = bch2_alloc_to_text,           \
142         .trans_trigger  = bch2_trans_mark_alloc,        \
143         .atomic_trigger = bch2_mark_alloc,              \
144 })
145
146 #define bch2_bkey_ops_alloc_v3 ((struct bkey_ops) {     \
147         .key_invalid    = bch2_alloc_v3_invalid,        \
148         .val_to_text    = bch2_alloc_to_text,           \
149         .trans_trigger  = bch2_trans_mark_alloc,        \
150         .atomic_trigger = bch2_mark_alloc,              \
151 })
152
153 #define bch2_bkey_ops_alloc_v4 ((struct bkey_ops) {     \
154         .key_invalid    = bch2_alloc_v4_invalid,        \
155         .val_to_text    = bch2_alloc_to_text,           \
156         .swab           = bch2_alloc_v4_swab,           \
157         .trans_trigger  = bch2_trans_mark_alloc,        \
158         .atomic_trigger = bch2_mark_alloc,              \
159 })
160
161 int bch2_bucket_gens_invalid(const struct bch_fs *, struct bkey_s_c, unsigned, struct printbuf *);
162 void bch2_bucket_gens_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
163
164 #define bch2_bkey_ops_bucket_gens ((struct bkey_ops) {  \
165         .key_invalid    = bch2_bucket_gens_invalid,     \
166         .val_to_text    = bch2_bucket_gens_to_text,     \
167 })
168
169 int bch2_bucket_gens_init(struct bch_fs *);
170
171 static inline bool bkey_is_alloc(const struct bkey *k)
172 {
173         return  k->type == KEY_TYPE_alloc ||
174                 k->type == KEY_TYPE_alloc_v2 ||
175                 k->type == KEY_TYPE_alloc_v3;
176 }
177
178 int bch2_alloc_read(struct bch_fs *);
179 int bch2_bucket_gens_read(struct bch_fs *);
180
181 int bch2_trans_mark_alloc(struct btree_trans *, enum btree_id, unsigned,
182                           struct bkey_s_c, struct bkey_i *, unsigned);
183 int bch2_check_alloc_info(struct bch_fs *);
184 int bch2_check_alloc_to_lru_refs(struct bch_fs *);
185 void bch2_do_discards(struct bch_fs *);
186
187 static inline u64 should_invalidate_buckets(struct bch_dev *ca,
188                                             struct bch_dev_usage u)
189 {
190         u64 want_free = ca->mi.nbuckets >> 7;
191         u64 free = max_t(s64, 0,
192                            u.d[BCH_DATA_free].buckets
193                          + u.d[BCH_DATA_need_discard].buckets
194                          - bch2_dev_buckets_reserved(ca, RESERVE_none));
195
196         return clamp_t(s64, want_free - free, 0, u.d[BCH_DATA_cached].buckets);
197 }
198
199 void bch2_do_invalidates(struct bch_fs *);
200
201 static inline struct bch_backpointer *alloc_v4_backpointers(struct bch_alloc_v4 *a)
202 {
203         return (void *) ((u64 *) &a->v +
204                          (BCH_ALLOC_V4_BACKPOINTERS_START(a) ?:
205                           BCH_ALLOC_V4_U64s_V0));
206 }
207
208 static inline const struct bch_backpointer *alloc_v4_backpointers_c(const struct bch_alloc_v4 *a)
209 {
210         return (void *) ((u64 *) &a->v + BCH_ALLOC_V4_BACKPOINTERS_START(a));
211 }
212
213 int bch2_fs_freespace_init(struct bch_fs *);
214
215 void bch2_recalc_capacity(struct bch_fs *);
216
217 void bch2_dev_allocator_remove(struct bch_fs *, struct bch_dev *);
218 void bch2_dev_allocator_add(struct bch_fs *, struct bch_dev *);
219
220 void bch2_fs_allocator_background_init(struct bch_fs *);
221
222 #endif /* _BCACHEFS_ALLOC_BACKGROUND_H */