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