]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/buckets.h
Update bcachefs sources to bf340e68c7 bcachefs: Ignore cached data when calculating...
[bcachefs-tools-debian] / libbcachefs / buckets.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Code for manipulating bucket marks for garbage collection.
4  *
5  * Copyright 2014 Datera, Inc.
6  */
7
8 #ifndef _BUCKETS_H
9 #define _BUCKETS_H
10
11 #include "buckets_types.h"
12 #include "super.h"
13
14 #define for_each_bucket(_b, _buckets)                           \
15         for (_b = (_buckets)->b + (_buckets)->first_bucket;     \
16              _b < (_buckets)->b + (_buckets)->nbuckets; _b++)
17
18 #define bucket_cmpxchg(g, new, expr)                            \
19 ({                                                              \
20         struct bucket *_g = g;                                  \
21         u64 _v = atomic64_read(&(g)->_mark.v);                  \
22         struct bucket_mark _old;                                \
23                                                                 \
24         do {                                                    \
25                 (new).v.counter = _old.v.counter = _v;          \
26                 expr;                                           \
27         } while ((_v = atomic64_cmpxchg(&(_g)->_mark.v,         \
28                                _old.v.counter,                  \
29                                (new).v.counter)) != _old.v.counter);\
30         _old;                                                   \
31 })
32
33 static inline struct bucket_array *__bucket_array(struct bch_dev *ca,
34                                                   bool gc)
35 {
36         return rcu_dereference_check(ca->buckets[gc],
37                                      !ca->fs ||
38                                      percpu_rwsem_is_held(&ca->fs->mark_lock) ||
39                                      lockdep_is_held(&ca->fs->gc_lock) ||
40                                      lockdep_is_held(&ca->bucket_lock));
41 }
42
43 static inline struct bucket_array *bucket_array(struct bch_dev *ca)
44 {
45         return __bucket_array(ca, false);
46 }
47
48 static inline struct bucket *__bucket(struct bch_dev *ca, size_t b, bool gc)
49 {
50         struct bucket_array *buckets = __bucket_array(ca, gc);
51
52         BUG_ON(b < buckets->first_bucket || b >= buckets->nbuckets);
53         return buckets->b + b;
54 }
55
56 static inline struct bucket *gc_bucket(struct bch_dev *ca, size_t b)
57 {
58         return __bucket(ca, b, true);
59 }
60
61 static inline struct bucket *bucket(struct bch_dev *ca, size_t b)
62 {
63         return __bucket(ca, b, false);
64 }
65
66 static inline struct bucket_gens *bucket_gens(struct bch_dev *ca)
67 {
68         return rcu_dereference_check(ca->bucket_gens,
69                                      !ca->fs ||
70                                      percpu_rwsem_is_held(&ca->fs->mark_lock) ||
71                                      lockdep_is_held(&ca->fs->gc_lock) ||
72                                      lockdep_is_held(&ca->bucket_lock));
73
74 }
75
76 static inline u8 *bucket_gen(struct bch_dev *ca, size_t b)
77 {
78         struct bucket_gens *gens = bucket_gens(ca);
79
80         BUG_ON(b < gens->first_bucket || b >= gens->nbuckets);
81         return gens->b + b;
82 }
83
84 /*
85  * bucket_gc_gen() returns the difference between the bucket's current gen and
86  * the oldest gen of any pointer into that bucket in the btree.
87  */
88
89 static inline u8 bucket_gc_gen(struct bucket *g)
90 {
91         return g->mark.gen - g->oldest_gen;
92 }
93
94 static inline size_t PTR_BUCKET_NR(const struct bch_dev *ca,
95                                    const struct bch_extent_ptr *ptr)
96 {
97         return sector_to_bucket(ca, ptr->offset);
98 }
99
100 static inline struct bucket *PTR_BUCKET(struct bch_dev *ca,
101                                         const struct bch_extent_ptr *ptr)
102 {
103         return bucket(ca, PTR_BUCKET_NR(ca, ptr));
104 }
105
106 static inline struct bucket *PTR_GC_BUCKET(struct bch_dev *ca,
107                                            const struct bch_extent_ptr *ptr)
108 {
109         return gc_bucket(ca, PTR_BUCKET_NR(ca, ptr));
110 }
111
112 static inline enum bch_data_type ptr_data_type(const struct bkey *k,
113                                                const struct bch_extent_ptr *ptr)
114 {
115         if (k->type == KEY_TYPE_btree_ptr ||
116             k->type == KEY_TYPE_btree_ptr_v2)
117                 return BCH_DATA_btree;
118
119         return ptr->cached ? BCH_DATA_cached : BCH_DATA_user;
120 }
121
122 static inline int gen_cmp(u8 a, u8 b)
123 {
124         return (s8) (a - b);
125 }
126
127 static inline int gen_after(u8 a, u8 b)
128 {
129         int r = gen_cmp(a, b);
130
131         return r > 0 ? r : 0;
132 }
133
134 /**
135  * ptr_stale() - check if a pointer points into a bucket that has been
136  * invalidated.
137  */
138 static inline u8 ptr_stale(struct bch_dev *ca,
139                            const struct bch_extent_ptr *ptr)
140 {
141         u8 ret;
142
143         rcu_read_lock();
144         ret = gen_after(*bucket_gen(ca, PTR_BUCKET_NR(ca, ptr)), ptr->gen);
145         rcu_read_unlock();
146
147         return ret;
148 }
149
150 /* bucket gc marks */
151
152 static inline bool is_available_bucket(struct bucket_mark mark)
153 {
154         return !mark.dirty_sectors && !mark.stripe;
155 }
156
157 /* Device usage: */
158
159 struct bch_dev_usage bch2_dev_usage_read(struct bch_dev *);
160
161 static inline u64 __dev_buckets_available(struct bch_dev *ca,
162                                           struct bch_dev_usage stats)
163 {
164         u64 total = ca->mi.nbuckets - ca->mi.first_bucket;
165
166         if (WARN_ONCE(stats.buckets_unavailable > total,
167                       "buckets_unavailable overflow (%llu > %llu)\n",
168                       stats.buckets_unavailable, total))
169                 return 0;
170
171         return total - stats.buckets_unavailable;
172 }
173
174 static inline u64 dev_buckets_available(struct bch_dev *ca)
175 {
176         return __dev_buckets_available(ca, bch2_dev_usage_read(ca));
177 }
178
179 static inline u64 __dev_buckets_reclaimable(struct bch_dev *ca,
180                                             struct bch_dev_usage stats)
181 {
182         struct bch_fs *c = ca->fs;
183         s64 available = __dev_buckets_available(ca, stats);
184         unsigned i;
185
186         spin_lock(&c->freelist_lock);
187         for (i = 0; i < RESERVE_NR; i++)
188                 available -= fifo_used(&ca->free[i]);
189         available -= fifo_used(&ca->free_inc);
190         available -= ca->nr_open_buckets;
191         spin_unlock(&c->freelist_lock);
192
193         return max(available, 0LL);
194 }
195
196 static inline u64 dev_buckets_reclaimable(struct bch_dev *ca)
197 {
198         return __dev_buckets_reclaimable(ca, bch2_dev_usage_read(ca));
199 }
200
201 /* Filesystem usage: */
202
203 static inline unsigned fs_usage_u64s(struct bch_fs *c)
204 {
205
206         return sizeof(struct bch_fs_usage) / sizeof(u64) +
207                 READ_ONCE(c->replicas.nr);
208 }
209
210 static inline unsigned dev_usage_u64s(void)
211 {
212         return sizeof(struct bch_dev_usage) / sizeof(u64);
213 }
214
215 u64 bch2_fs_usage_read_one(struct bch_fs *, u64 *);
216
217 struct bch_fs_usage_online *bch2_fs_usage_read(struct bch_fs *);
218
219 void bch2_fs_usage_acc_to_base(struct bch_fs *, unsigned);
220
221 void bch2_fs_usage_to_text(struct printbuf *,
222                            struct bch_fs *, struct bch_fs_usage_online *);
223
224 u64 bch2_fs_sectors_used(struct bch_fs *, struct bch_fs_usage_online *);
225
226 struct bch_fs_usage_short
227 bch2_fs_usage_read_short(struct bch_fs *);
228
229 /* key/bucket marking: */
230
231 void bch2_fs_usage_initialize(struct bch_fs *);
232
233 void bch2_mark_alloc_bucket(struct bch_fs *, struct bch_dev *, size_t, bool);
234 void bch2_mark_metadata_bucket(struct bch_fs *, struct bch_dev *,
235                                size_t, enum bch_data_type, unsigned,
236                                struct gc_pos, unsigned);
237
238 int bch2_mark_key(struct btree_trans *, struct bkey_s_c, struct bkey_s_c, unsigned);
239
240 int bch2_mark_update(struct btree_trans *, struct btree_path *,
241                      struct bkey_i *, unsigned);
242
243 int bch2_trans_mark_key(struct btree_trans *, struct bkey_s_c,
244                         struct bkey_s_c, unsigned);
245 int bch2_trans_fs_usage_apply(struct btree_trans *, struct replicas_delta_list *);
246
247 int bch2_trans_mark_metadata_bucket(struct btree_trans *, struct bch_dev *,
248                                     size_t, enum bch_data_type, unsigned);
249 int bch2_trans_mark_dev_sb(struct bch_fs *, struct bch_dev *);
250
251 /* disk reservations: */
252
253 static inline void bch2_disk_reservation_put(struct bch_fs *c,
254                                              struct disk_reservation *res)
255 {
256         this_cpu_sub(*c->online_reserved, res->sectors);
257         res->sectors = 0;
258 }
259
260 #define BCH_DISK_RESERVATION_NOFAIL             (1 << 0)
261
262 int bch2_disk_reservation_add(struct bch_fs *,
263                               struct disk_reservation *,
264                               u64, int);
265
266 static inline struct disk_reservation
267 bch2_disk_reservation_init(struct bch_fs *c, unsigned nr_replicas)
268 {
269         return (struct disk_reservation) {
270                 .sectors        = 0,
271 #if 0
272                 /* not used yet: */
273                 .gen            = c->capacity_gen,
274 #endif
275                 .nr_replicas    = nr_replicas,
276         };
277 }
278
279 static inline int bch2_disk_reservation_get(struct bch_fs *c,
280                                             struct disk_reservation *res,
281                                             u64 sectors, unsigned nr_replicas,
282                                             int flags)
283 {
284         *res = bch2_disk_reservation_init(c, nr_replicas);
285
286         return bch2_disk_reservation_add(c, res, sectors * nr_replicas, flags);
287 }
288
289 #define RESERVE_FACTOR  6
290
291 static inline u64 avail_factor(u64 r)
292 {
293         return div_u64(r << RESERVE_FACTOR, (1 << RESERVE_FACTOR) + 1);
294 }
295
296 int bch2_dev_buckets_resize(struct bch_fs *, struct bch_dev *, u64);
297 void bch2_dev_buckets_free(struct bch_dev *);
298 int bch2_dev_buckets_alloc(struct bch_fs *, struct bch_dev *);
299
300 #endif /* _BUCKETS_H */