]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/buckets.h
Update bcachefs sources to 0010403265 bcachefs: Fix spurious alloc errors on forced...
[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 *bucket(struct bch_dev *ca, size_t b)
57 {
58         return __bucket(ca, b, false);
59 }
60
61 static inline u16 bucket_last_io(struct bch_fs *c, struct bucket *g, int rw)
62 {
63         return c->bucket_clock[rw].hand - g->io_time[rw];
64 }
65
66 /*
67  * bucket_gc_gen() returns the difference between the bucket's current gen and
68  * the oldest gen of any pointer into that bucket in the btree.
69  */
70
71 static inline u8 bucket_gc_gen(struct bch_dev *ca, size_t b)
72 {
73         struct bucket *g = bucket(ca, b);
74
75         return g->mark.gen - g->oldest_gen;
76 }
77
78 static inline size_t PTR_BUCKET_NR(const struct bch_dev *ca,
79                                    const struct bch_extent_ptr *ptr)
80 {
81         return sector_to_bucket(ca, ptr->offset);
82 }
83
84 static inline struct bucket *PTR_BUCKET(struct bch_dev *ca,
85                                         const struct bch_extent_ptr *ptr,
86                                         bool gc)
87 {
88         return __bucket(ca, PTR_BUCKET_NR(ca, ptr), gc);
89 }
90
91 static inline enum bch_data_type ptr_data_type(const struct bkey *k,
92                                                const struct bch_extent_ptr *ptr)
93 {
94         if (k->type == KEY_TYPE_btree_ptr ||
95             k->type == KEY_TYPE_btree_ptr_v2)
96                 return BCH_DATA_btree;
97
98         return ptr->cached ? BCH_DATA_cached : BCH_DATA_user;
99 }
100
101 static inline struct bucket_mark ptr_bucket_mark(struct bch_dev *ca,
102                                                  const struct bch_extent_ptr *ptr)
103 {
104         struct bucket_mark m;
105
106         rcu_read_lock();
107         m = READ_ONCE(PTR_BUCKET(ca, ptr, 0)->mark);
108         rcu_read_unlock();
109
110         return m;
111 }
112
113 static inline int gen_cmp(u8 a, u8 b)
114 {
115         return (s8) (a - b);
116 }
117
118 static inline int gen_after(u8 a, u8 b)
119 {
120         int r = gen_cmp(a, b);
121
122         return r > 0 ? r : 0;
123 }
124
125 /**
126  * ptr_stale() - check if a pointer points into a bucket that has been
127  * invalidated.
128  */
129 static inline u8 ptr_stale(struct bch_dev *ca,
130                            const struct bch_extent_ptr *ptr)
131 {
132         return gen_after(ptr_bucket_mark(ca, ptr).gen, ptr->gen);
133 }
134
135 static inline s64 __ptr_disk_sectors(struct extent_ptr_decoded p,
136                                      unsigned live_size)
137 {
138         return live_size && p.crc.compression_type
139                 ? max(1U, DIV_ROUND_UP(live_size * p.crc.compressed_size,
140                                        p.crc.uncompressed_size))
141                 : live_size;
142 }
143
144 static inline s64 ptr_disk_sectors(struct extent_ptr_decoded p)
145 {
146         return __ptr_disk_sectors(p, p.crc.live_size);
147 }
148
149 /* bucket gc marks */
150
151 static inline unsigned bucket_sectors_used(struct bucket_mark mark)
152 {
153         return mark.dirty_sectors + mark.cached_sectors;
154 }
155
156 static inline bool bucket_unused(struct bucket_mark mark)
157 {
158         return !mark.owned_by_allocator &&
159                 !mark.data_type &&
160                 !bucket_sectors_used(mark);
161 }
162
163 static inline bool is_available_bucket(struct bucket_mark mark)
164 {
165         return (!mark.owned_by_allocator &&
166                 !mark.dirty_sectors &&
167                 !mark.stripe);
168 }
169
170 static inline bool bucket_needs_journal_commit(struct bucket_mark m,
171                                                u16 last_seq_ondisk)
172 {
173         return m.journal_seq_valid &&
174                 ((s16) m.journal_seq - (s16) last_seq_ondisk > 0);
175 }
176
177 /* Device usage: */
178
179 struct bch_dev_usage bch2_dev_usage_read(struct bch_dev *);
180
181 void bch2_dev_usage_from_buckets(struct bch_fs *);
182
183 static inline u64 __dev_buckets_available(struct bch_dev *ca,
184                                           struct bch_dev_usage stats)
185 {
186         u64 total = ca->mi.nbuckets - ca->mi.first_bucket;
187
188         if (WARN_ONCE(stats.buckets_unavailable > total,
189                       "buckets_unavailable overflow (%llu > %llu)\n",
190                       stats.buckets_unavailable, total))
191                 return 0;
192
193         return total - stats.buckets_unavailable;
194 }
195
196 /*
197  * Number of reclaimable buckets - only for use by the allocator thread:
198  */
199 static inline u64 dev_buckets_available(struct bch_dev *ca)
200 {
201         return __dev_buckets_available(ca, bch2_dev_usage_read(ca));
202 }
203
204 static inline u64 __dev_buckets_free(struct bch_dev *ca,
205                                      struct bch_dev_usage stats)
206 {
207         return __dev_buckets_available(ca, stats) +
208                 fifo_used(&ca->free[RESERVE_NONE]) +
209                 fifo_used(&ca->free_inc);
210 }
211
212 static inline u64 dev_buckets_free(struct bch_dev *ca)
213 {
214         return __dev_buckets_free(ca, bch2_dev_usage_read(ca));
215 }
216
217 /* Filesystem usage: */
218
219 static inline unsigned fs_usage_u64s(struct bch_fs *c)
220 {
221
222         return sizeof(struct bch_fs_usage) / sizeof(u64) +
223                 READ_ONCE(c->replicas.nr);
224 }
225
226 void bch2_fs_usage_scratch_put(struct bch_fs *, struct bch_fs_usage *);
227 struct bch_fs_usage *bch2_fs_usage_scratch_get(struct bch_fs *);
228
229 u64 bch2_fs_usage_read_one(struct bch_fs *, u64 *);
230
231 struct bch_fs_usage *bch2_fs_usage_read(struct bch_fs *);
232
233 void bch2_fs_usage_acc_to_base(struct bch_fs *, unsigned);
234
235 void bch2_fs_usage_to_text(struct printbuf *,
236                            struct bch_fs *, struct bch_fs_usage *);
237
238 u64 bch2_fs_sectors_used(struct bch_fs *, struct bch_fs_usage *);
239
240 struct bch_fs_usage_short
241 bch2_fs_usage_read_short(struct bch_fs *);
242
243 /* key/bucket marking: */
244
245 void bch2_bucket_seq_cleanup(struct bch_fs *);
246 void bch2_fs_usage_initialize(struct bch_fs *);
247
248 void bch2_invalidate_bucket(struct bch_fs *, struct bch_dev *,
249                             size_t, struct bucket_mark *);
250 void bch2_mark_alloc_bucket(struct bch_fs *, struct bch_dev *,
251                             size_t, bool, struct gc_pos, unsigned);
252 void bch2_mark_metadata_bucket(struct bch_fs *, struct bch_dev *,
253                                size_t, enum bch_data_type, unsigned,
254                                struct gc_pos, unsigned);
255
256 int bch2_mark_key(struct bch_fs *, struct bkey_s_c, unsigned,
257                   s64, struct bch_fs_usage *, u64, unsigned);
258 int bch2_fs_usage_apply(struct bch_fs *, struct bch_fs_usage *,
259                         struct disk_reservation *, unsigned);
260
261 int bch2_mark_update(struct btree_trans *, struct btree_iter *,
262                      struct bkey_i *, struct bch_fs_usage *, unsigned);
263
264 int bch2_replicas_delta_list_apply(struct bch_fs *,
265                                    struct bch_fs_usage *,
266                                    struct replicas_delta_list *);
267 int bch2_trans_mark_key(struct btree_trans *, struct bkey_s_c,
268                         unsigned, s64, unsigned);
269 int bch2_trans_mark_update(struct btree_trans *, struct btree_iter *iter,
270                            struct bkey_i *insert, unsigned);
271 void bch2_trans_fs_usage_apply(struct btree_trans *, struct bch_fs_usage *);
272
273 /* disk reservations: */
274
275 void __bch2_disk_reservation_put(struct bch_fs *, struct disk_reservation *);
276
277 static inline void bch2_disk_reservation_put(struct bch_fs *c,
278                                              struct disk_reservation *res)
279 {
280         if (res->sectors)
281                 __bch2_disk_reservation_put(c, res);
282 }
283
284 #define BCH_DISK_RESERVATION_NOFAIL             (1 << 0)
285
286 int bch2_disk_reservation_add(struct bch_fs *,
287                              struct disk_reservation *,
288                              unsigned, int);
289
290 static inline struct disk_reservation
291 bch2_disk_reservation_init(struct bch_fs *c, unsigned nr_replicas)
292 {
293         return (struct disk_reservation) {
294                 .sectors        = 0,
295 #if 0
296                 /* not used yet: */
297                 .gen            = c->capacity_gen,
298 #endif
299                 .nr_replicas    = nr_replicas,
300         };
301 }
302
303 static inline int bch2_disk_reservation_get(struct bch_fs *c,
304                                             struct disk_reservation *res,
305                                             unsigned sectors,
306                                             unsigned nr_replicas,
307                                             int flags)
308 {
309         *res = bch2_disk_reservation_init(c, nr_replicas);
310
311         return bch2_disk_reservation_add(c, res, sectors * nr_replicas, flags);
312 }
313
314 int bch2_dev_buckets_resize(struct bch_fs *, struct bch_dev *, u64);
315 void bch2_dev_buckets_free(struct bch_dev *);
316 int bch2_dev_buckets_alloc(struct bch_fs *, struct bch_dev *);
317
318 #endif /* _BUCKETS_H */