1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Code for manipulating bucket marks for garbage collection.
5 * Copyright 2014 Datera, Inc.
11 #include "buckets_types.h"
14 #define for_each_bucket(_b, _buckets) \
15 for (_b = (_buckets)->b + (_buckets)->first_bucket; \
16 _b < (_buckets)->b + (_buckets)->nbuckets; _b++)
18 #define bucket_cmpxchg(g, new, expr) \
20 struct bucket *_g = g; \
21 u64 _v = atomic64_read(&(g)->_mark.v); \
22 struct bucket_mark _old; \
25 (new).v.counter = _old.v.counter = _v; \
27 } while ((_v = atomic64_cmpxchg(&(_g)->_mark.v, \
29 (new).v.counter)) != _old.v.counter);\
33 static inline struct bucket_array *__bucket_array(struct bch_dev *ca,
36 return rcu_dereference_check(ca->buckets[gc],
38 percpu_rwsem_is_held(&ca->fs->mark_lock) ||
39 lockdep_is_held(&ca->fs->gc_lock) ||
40 lockdep_is_held(&ca->bucket_lock));
43 static inline struct bucket_array *bucket_array(struct bch_dev *ca)
45 return __bucket_array(ca, false);
48 static inline struct bucket *__bucket(struct bch_dev *ca, size_t b, bool gc)
50 struct bucket_array *buckets = __bucket_array(ca, gc);
52 BUG_ON(b < buckets->first_bucket || b >= buckets->nbuckets);
53 return buckets->b + b;
56 static inline struct bucket *bucket(struct bch_dev *ca, size_t b)
58 return __bucket(ca, b, false);
61 static inline void bucket_io_clock_reset(struct bch_fs *c, struct bch_dev *ca,
64 bucket(ca, b)->io_time[rw] = c->bucket_clock[rw].hand;
67 static inline u16 bucket_last_io(struct bch_fs *c, struct bucket *g, int rw)
69 return c->bucket_clock[rw].hand - g->io_time[rw];
73 * bucket_gc_gen() returns the difference between the bucket's current gen and
74 * the oldest gen of any pointer into that bucket in the btree.
77 static inline u8 bucket_gc_gen(struct bch_dev *ca, size_t b)
79 struct bucket *g = bucket(ca, b);
81 return g->mark.gen - g->oldest_gen;
84 static inline size_t PTR_BUCKET_NR(const struct bch_dev *ca,
85 const struct bch_extent_ptr *ptr)
87 return sector_to_bucket(ca, ptr->offset);
90 static inline struct bucket *PTR_BUCKET(struct bch_dev *ca,
91 const struct bch_extent_ptr *ptr,
94 return __bucket(ca, PTR_BUCKET_NR(ca, ptr), gc);
97 static inline enum bch_data_type ptr_data_type(const struct bkey *k,
98 const struct bch_extent_ptr *ptr)
100 if (k->type == KEY_TYPE_btree_ptr ||
101 k->type == KEY_TYPE_btree_ptr_v2)
102 return BCH_DATA_btree;
104 return ptr->cached ? BCH_DATA_cached : BCH_DATA_user;
107 static inline struct bucket_mark ptr_bucket_mark(struct bch_dev *ca,
108 const struct bch_extent_ptr *ptr)
110 struct bucket_mark m;
113 m = READ_ONCE(PTR_BUCKET(ca, ptr, 0)->mark);
119 static inline int gen_cmp(u8 a, u8 b)
124 static inline int gen_after(u8 a, u8 b)
126 int r = gen_cmp(a, b);
128 return r > 0 ? r : 0;
132 * ptr_stale() - check if a pointer points into a bucket that has been
135 static inline u8 ptr_stale(struct bch_dev *ca,
136 const struct bch_extent_ptr *ptr)
138 return gen_after(ptr_bucket_mark(ca, ptr).gen, ptr->gen);
141 static inline s64 __ptr_disk_sectors(struct extent_ptr_decoded p,
144 return live_size && p.crc.compression_type
145 ? max(1U, DIV_ROUND_UP(live_size * p.crc.compressed_size,
146 p.crc.uncompressed_size))
150 static inline s64 ptr_disk_sectors(struct extent_ptr_decoded p)
152 return __ptr_disk_sectors(p, p.crc.live_size);
155 /* bucket gc marks */
157 static inline unsigned bucket_sectors_used(struct bucket_mark mark)
159 return mark.dirty_sectors + mark.cached_sectors;
162 static inline bool bucket_unused(struct bucket_mark mark)
164 return !mark.owned_by_allocator &&
166 !bucket_sectors_used(mark);
169 static inline bool is_available_bucket(struct bucket_mark mark)
171 return (!mark.owned_by_allocator &&
172 !mark.dirty_sectors &&
176 static inline bool bucket_needs_journal_commit(struct bucket_mark m,
179 return m.journal_seq_valid &&
180 ((s16) m.journal_seq - (s16) last_seq_ondisk > 0);
185 struct bch_dev_usage bch2_dev_usage_read(struct bch_dev *);
187 void bch2_dev_usage_from_buckets(struct bch_fs *);
189 static inline u64 __dev_buckets_available(struct bch_dev *ca,
190 struct bch_dev_usage stats)
192 u64 total = ca->mi.nbuckets - ca->mi.first_bucket;
194 if (WARN_ONCE(stats.buckets_unavailable > total,
195 "buckets_unavailable overflow (%llu > %llu)\n",
196 stats.buckets_unavailable, total))
199 return total - stats.buckets_unavailable;
203 * Number of reclaimable buckets - only for use by the allocator thread:
205 static inline u64 dev_buckets_available(struct bch_dev *ca)
207 return __dev_buckets_available(ca, bch2_dev_usage_read(ca));
210 static inline u64 __dev_buckets_free(struct bch_dev *ca,
211 struct bch_dev_usage stats)
213 return __dev_buckets_available(ca, stats) +
214 fifo_used(&ca->free[RESERVE_NONE]) +
215 fifo_used(&ca->free_inc);
218 static inline u64 dev_buckets_free(struct bch_dev *ca)
220 return __dev_buckets_free(ca, bch2_dev_usage_read(ca));
223 /* Filesystem usage: */
225 static inline unsigned fs_usage_u64s(struct bch_fs *c)
228 return sizeof(struct bch_fs_usage) / sizeof(u64) +
229 READ_ONCE(c->replicas.nr);
232 void bch2_fs_usage_scratch_put(struct bch_fs *, struct bch_fs_usage *);
233 struct bch_fs_usage *bch2_fs_usage_scratch_get(struct bch_fs *);
235 u64 bch2_fs_usage_read_one(struct bch_fs *, u64 *);
237 struct bch_fs_usage *bch2_fs_usage_read(struct bch_fs *);
239 void bch2_fs_usage_acc_to_base(struct bch_fs *, unsigned);
241 void bch2_fs_usage_to_text(struct printbuf *,
242 struct bch_fs *, struct bch_fs_usage *);
244 u64 bch2_fs_sectors_used(struct bch_fs *, struct bch_fs_usage *);
246 struct bch_fs_usage_short
247 bch2_fs_usage_read_short(struct bch_fs *);
249 /* key/bucket marking: */
251 void bch2_bucket_seq_cleanup(struct bch_fs *);
252 void bch2_fs_usage_initialize(struct bch_fs *);
254 void bch2_invalidate_bucket(struct bch_fs *, struct bch_dev *,
255 size_t, struct bucket_mark *);
256 void bch2_mark_alloc_bucket(struct bch_fs *, struct bch_dev *,
257 size_t, bool, struct gc_pos, unsigned);
258 void bch2_mark_metadata_bucket(struct bch_fs *, struct bch_dev *,
259 size_t, enum bch_data_type, unsigned,
260 struct gc_pos, unsigned);
262 int bch2_mark_key(struct bch_fs *, struct bkey_s_c, unsigned,
263 s64, struct bch_fs_usage *, u64, unsigned);
264 int bch2_fs_usage_apply(struct bch_fs *, struct bch_fs_usage *,
265 struct disk_reservation *, unsigned);
267 int bch2_mark_update(struct btree_trans *, struct btree_iter *,
268 struct bkey_i *, struct bch_fs_usage *, unsigned);
270 int bch2_replicas_delta_list_apply(struct bch_fs *,
271 struct bch_fs_usage *,
272 struct replicas_delta_list *);
273 int bch2_trans_mark_key(struct btree_trans *, struct bkey_s_c,
274 unsigned, s64, unsigned);
275 int bch2_trans_mark_update(struct btree_trans *, struct btree_iter *iter,
276 struct bkey_i *insert, unsigned);
277 void bch2_trans_fs_usage_apply(struct btree_trans *, struct bch_fs_usage *);
279 /* disk reservations: */
281 void __bch2_disk_reservation_put(struct bch_fs *, struct disk_reservation *);
283 static inline void bch2_disk_reservation_put(struct bch_fs *c,
284 struct disk_reservation *res)
287 __bch2_disk_reservation_put(c, res);
290 #define BCH_DISK_RESERVATION_NOFAIL (1 << 0)
292 int bch2_disk_reservation_add(struct bch_fs *,
293 struct disk_reservation *,
296 static inline struct disk_reservation
297 bch2_disk_reservation_init(struct bch_fs *c, unsigned nr_replicas)
299 return (struct disk_reservation) {
303 .gen = c->capacity_gen,
305 .nr_replicas = nr_replicas,
309 static inline int bch2_disk_reservation_get(struct bch_fs *c,
310 struct disk_reservation *res,
312 unsigned nr_replicas,
315 *res = bch2_disk_reservation_init(c, nr_replicas);
317 return bch2_disk_reservation_add(c, res, sectors * nr_replicas, flags);
320 int bch2_dev_buckets_resize(struct bch_fs *, struct bch_dev *, u64);
321 void bch2_dev_buckets_free(struct bch_dev *);
322 int bch2_dev_buckets_alloc(struct bch_fs *, struct bch_dev *);
324 #endif /* _BUCKETS_H */