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 *gc_bucket(struct bch_dev *ca, size_t b)
58 return __bucket(ca, b, true);
61 static inline struct bucket *bucket(struct bch_dev *ca, size_t b)
63 return __bucket(ca, b, false);
66 static inline struct bucket_gens *bucket_gens(struct bch_dev *ca)
68 return rcu_dereference_check(ca->bucket_gens,
70 percpu_rwsem_is_held(&ca->fs->mark_lock) ||
71 lockdep_is_held(&ca->fs->gc_lock) ||
72 lockdep_is_held(&ca->bucket_lock));
76 static inline u8 *bucket_gen(struct bch_dev *ca, size_t b)
78 struct bucket_gens *gens = bucket_gens(ca);
80 BUG_ON(b < gens->first_bucket || b >= gens->nbuckets);
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.
89 static inline u8 bucket_gc_gen(struct bucket *g)
91 return g->mark.gen - g->oldest_gen;
94 static inline size_t PTR_BUCKET_NR(const struct bch_dev *ca,
95 const struct bch_extent_ptr *ptr)
97 return sector_to_bucket(ca, ptr->offset);
100 static inline struct bucket *PTR_GC_BUCKET(struct bch_dev *ca,
101 const struct bch_extent_ptr *ptr)
103 return gc_bucket(ca, PTR_BUCKET_NR(ca, ptr));
106 static inline enum bch_data_type ptr_data_type(const struct bkey *k,
107 const struct bch_extent_ptr *ptr)
109 if (k->type == KEY_TYPE_btree_ptr ||
110 k->type == KEY_TYPE_btree_ptr_v2)
111 return BCH_DATA_btree;
113 return ptr->cached ? BCH_DATA_cached : BCH_DATA_user;
116 static inline int gen_cmp(u8 a, u8 b)
121 static inline int gen_after(u8 a, u8 b)
123 int r = gen_cmp(a, b);
125 return r > 0 ? r : 0;
129 * ptr_stale() - check if a pointer points into a bucket that has been
132 static inline u8 ptr_stale(struct bch_dev *ca,
133 const struct bch_extent_ptr *ptr)
138 ret = gen_after(*bucket_gen(ca, PTR_BUCKET_NR(ca, ptr)), ptr->gen);
144 /* bucket gc marks */
146 static inline bool is_available_bucket(struct bucket_mark mark)
148 return !mark.dirty_sectors && !mark.stripe;
153 struct bch_dev_usage bch2_dev_usage_read(struct bch_dev *);
155 static inline u64 __dev_buckets_available(struct bch_dev *ca,
156 struct bch_dev_usage stats)
158 u64 total = ca->mi.nbuckets - ca->mi.first_bucket;
160 if (WARN_ONCE(stats.buckets_unavailable > total,
161 "buckets_unavailable overflow (%llu > %llu)\n",
162 stats.buckets_unavailable, total))
165 return total - stats.buckets_unavailable;
168 static inline u64 dev_buckets_available(struct bch_dev *ca)
170 return __dev_buckets_available(ca, bch2_dev_usage_read(ca));
173 static inline u64 __dev_buckets_reclaimable(struct bch_dev *ca,
174 struct bch_dev_usage stats)
176 struct bch_fs *c = ca->fs;
177 s64 available = __dev_buckets_available(ca, stats);
180 spin_lock(&c->freelist_lock);
181 for (i = 0; i < RESERVE_NR; i++)
182 available -= fifo_used(&ca->free[i]);
183 available -= fifo_used(&ca->free_inc);
184 available -= ca->nr_open_buckets;
185 spin_unlock(&c->freelist_lock);
187 return max(available, 0LL);
190 static inline u64 dev_buckets_reclaimable(struct bch_dev *ca)
192 return __dev_buckets_reclaimable(ca, bch2_dev_usage_read(ca));
195 /* Filesystem usage: */
197 static inline unsigned fs_usage_u64s(struct bch_fs *c)
200 return sizeof(struct bch_fs_usage) / sizeof(u64) +
201 READ_ONCE(c->replicas.nr);
204 static inline unsigned dev_usage_u64s(void)
206 return sizeof(struct bch_dev_usage) / sizeof(u64);
209 u64 bch2_fs_usage_read_one(struct bch_fs *, u64 *);
211 struct bch_fs_usage_online *bch2_fs_usage_read(struct bch_fs *);
213 void bch2_fs_usage_acc_to_base(struct bch_fs *, unsigned);
215 void bch2_fs_usage_to_text(struct printbuf *,
216 struct bch_fs *, struct bch_fs_usage_online *);
218 u64 bch2_fs_sectors_used(struct bch_fs *, struct bch_fs_usage_online *);
220 struct bch_fs_usage_short
221 bch2_fs_usage_read_short(struct bch_fs *);
223 /* key/bucket marking: */
225 void bch2_fs_usage_initialize(struct bch_fs *);
227 void bch2_mark_alloc_bucket(struct bch_fs *, struct bch_dev *, size_t, bool);
228 void bch2_mark_metadata_bucket(struct bch_fs *, struct bch_dev *,
229 size_t, enum bch_data_type, unsigned,
230 struct gc_pos, unsigned);
232 int bch2_mark_key(struct btree_trans *, struct bkey_s_c, struct bkey_s_c, unsigned);
234 int bch2_mark_update(struct btree_trans *, struct btree_path *,
235 struct bkey_i *, unsigned);
237 int bch2_trans_mark_key(struct btree_trans *, struct bkey_s_c,
238 struct bkey_s_c, unsigned);
239 int bch2_trans_fs_usage_apply(struct btree_trans *, struct replicas_delta_list *);
241 int bch2_trans_mark_metadata_bucket(struct btree_trans *, struct bch_dev *,
242 size_t, enum bch_data_type, unsigned);
243 int bch2_trans_mark_dev_sb(struct bch_fs *, struct bch_dev *);
245 /* disk reservations: */
247 static inline void bch2_disk_reservation_put(struct bch_fs *c,
248 struct disk_reservation *res)
250 this_cpu_sub(*c->online_reserved, res->sectors);
254 #define BCH_DISK_RESERVATION_NOFAIL (1 << 0)
256 int bch2_disk_reservation_add(struct bch_fs *,
257 struct disk_reservation *,
260 static inline struct disk_reservation
261 bch2_disk_reservation_init(struct bch_fs *c, unsigned nr_replicas)
263 return (struct disk_reservation) {
267 .gen = c->capacity_gen,
269 .nr_replicas = nr_replicas,
273 static inline int bch2_disk_reservation_get(struct bch_fs *c,
274 struct disk_reservation *res,
275 u64 sectors, unsigned nr_replicas,
278 *res = bch2_disk_reservation_init(c, nr_replicas);
280 return bch2_disk_reservation_add(c, res, sectors * nr_replicas, flags);
283 #define RESERVE_FACTOR 6
285 static inline u64 avail_factor(u64 r)
287 return div_u64(r << RESERVE_FACTOR, (1 << RESERVE_FACTOR) + 1);
290 int bch2_dev_buckets_resize(struct bch_fs *, struct bch_dev *, u64);
291 void bch2_dev_buckets_free(struct bch_dev *);
292 int bch2_dev_buckets_alloc(struct bch_fs *, struct bch_dev *);
294 #endif /* _BUCKETS_H */