2 * Code for manipulating bucket marks for garbage collection.
4 * Copyright 2014 Datera, Inc.
10 #include "buckets_types.h"
13 #define for_each_bucket(_b, _buckets) \
14 for (_b = (_buckets)->b + (_buckets)->first_bucket; \
15 _b < (_buckets)->b + (_buckets)->nbuckets; _b++)
17 #define bucket_cmpxchg(g, new, expr) \
19 u64 _v = atomic64_read(&(g)->_mark.v); \
20 struct bucket_mark _old; \
23 (new).v.counter = _old.v.counter = _v; \
25 } while ((_v = atomic64_cmpxchg(&(g)->_mark.v, \
27 (new).v.counter)) != _old.v.counter);\
31 static inline struct bucket_array *__bucket_array(struct bch_dev *ca,
34 return rcu_dereference_check(ca->buckets[gc],
36 percpu_rwsem_is_held(&ca->fs->mark_lock) ||
37 lockdep_is_held(&ca->fs->gc_lock) ||
38 lockdep_is_held(&ca->bucket_lock));
41 static inline struct bucket_array *bucket_array(struct bch_dev *ca)
43 return __bucket_array(ca, false);
46 static inline struct bucket *__bucket(struct bch_dev *ca, size_t b, bool gc)
48 struct bucket_array *buckets = __bucket_array(ca, gc);
50 BUG_ON(b < buckets->first_bucket || b >= buckets->nbuckets);
51 return buckets->b + b;
54 static inline struct bucket *bucket(struct bch_dev *ca, size_t b)
56 return __bucket(ca, b, false);
59 static inline void bucket_set_dirty(struct bch_dev *ca, size_t b)
66 bucket_cmpxchg(g, m, m.dirty = true);
71 static inline void bucket_io_clock_reset(struct bch_fs *c, struct bch_dev *ca,
74 bucket(ca, b)->io_time[rw] = c->bucket_clock[rw].hand;
77 static inline u16 bucket_last_io(struct bch_fs *c, struct bucket *g, int rw)
79 return c->bucket_clock[rw].hand - g->io_time[rw];
83 * bucket_gc_gen() returns the difference between the bucket's current gen and
84 * the oldest gen of any pointer into that bucket in the btree.
87 static inline u8 bucket_gc_gen(struct bch_dev *ca, size_t b)
89 return bucket(ca, b)->mark.gen - ca->oldest_gens[b];
92 static inline size_t PTR_BUCKET_NR(const struct bch_dev *ca,
93 const struct bch_extent_ptr *ptr)
95 return sector_to_bucket(ca, ptr->offset);
98 static inline struct bucket *PTR_BUCKET(struct bch_dev *ca,
99 const struct bch_extent_ptr *ptr)
101 return bucket(ca, PTR_BUCKET_NR(ca, ptr));
104 static inline struct bucket_mark ptr_bucket_mark(struct bch_dev *ca,
105 const struct bch_extent_ptr *ptr)
107 struct bucket_mark m;
110 m = READ_ONCE(bucket(ca, PTR_BUCKET_NR(ca, ptr))->mark);
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)
135 return gen_after(ptr_bucket_mark(ca, ptr).gen, ptr->gen);
138 static inline unsigned __ptr_disk_sectors(struct extent_ptr_decoded p,
141 return live_size && p.crc.compression_type
142 ? max(1U, DIV_ROUND_UP(live_size * p.crc.compressed_size,
143 p.crc.uncompressed_size))
147 static inline unsigned ptr_disk_sectors(struct extent_ptr_decoded p)
149 return __ptr_disk_sectors(p, p.crc.live_size);
152 /* bucket gc marks */
154 static inline unsigned bucket_sectors_used(struct bucket_mark mark)
156 return mark.dirty_sectors + mark.cached_sectors;
159 static inline bool bucket_unused(struct bucket_mark mark)
161 return !mark.owned_by_allocator &&
163 !bucket_sectors_used(mark);
166 static inline bool is_available_bucket(struct bucket_mark mark)
168 return (!mark.owned_by_allocator &&
169 !mark.dirty_sectors &&
173 static inline bool bucket_needs_journal_commit(struct bucket_mark m,
176 return m.journal_seq_valid &&
177 ((s16) m.journal_seq - (s16) last_seq_ondisk > 0);
182 struct bch_dev_usage __bch2_dev_usage_read(struct bch_dev *, bool);
183 struct bch_dev_usage bch2_dev_usage_read(struct bch_fs *, struct bch_dev *);
185 static inline u64 __dev_buckets_available(struct bch_dev *ca,
186 struct bch_dev_usage stats)
188 u64 total = ca->mi.nbuckets - ca->mi.first_bucket;
190 if (WARN_ONCE(stats.buckets_unavailable > total,
191 "buckets_unavailable overflow (%llu > %llu)\n",
192 stats.buckets_unavailable, total))
195 return total - stats.buckets_unavailable;
199 * Number of reclaimable buckets - only for use by the allocator thread:
201 static inline u64 dev_buckets_available(struct bch_fs *c, struct bch_dev *ca)
203 return __dev_buckets_available(ca, bch2_dev_usage_read(c, ca));
206 static inline u64 __dev_buckets_free(struct bch_dev *ca,
207 struct bch_dev_usage stats)
209 return __dev_buckets_available(ca, stats) +
210 fifo_used(&ca->free[RESERVE_NONE]) +
211 fifo_used(&ca->free_inc);
214 static inline u64 dev_buckets_free(struct bch_fs *c, struct bch_dev *ca)
216 return __dev_buckets_free(ca, bch2_dev_usage_read(c, ca));
219 /* Filesystem usage: */
221 struct bch_fs_usage __bch2_fs_usage_read(struct bch_fs *, bool);
222 struct bch_fs_usage bch2_fs_usage_read(struct bch_fs *);
224 u64 bch2_fs_sectors_used(struct bch_fs *, struct bch_fs_usage);
226 struct bch_fs_usage_short
227 bch2_fs_usage_read_short(struct bch_fs *);
229 static inline u64 bch2_fs_sectors_free(struct bch_fs *c)
231 struct bch_fs_usage_short usage = bch2_fs_usage_read_short(c);
233 return usage.capacity - usage.used;
236 /* key/bucket marking: */
238 void bch2_bucket_seq_cleanup(struct bch_fs *);
240 void bch2_invalidate_bucket(struct bch_fs *, struct bch_dev *,
241 size_t, struct bucket_mark *);
242 void bch2_mark_alloc_bucket(struct bch_fs *, struct bch_dev *,
243 size_t, bool, struct gc_pos, unsigned);
244 void bch2_mark_metadata_bucket(struct bch_fs *, struct bch_dev *,
245 size_t, enum bch_data_type, unsigned,
246 struct gc_pos, unsigned);
248 #define BCH_BUCKET_MARK_NOATOMIC (1 << 0)
249 #define BCH_BUCKET_MARK_GC (1 << 1)
251 int bch2_mark_key_locked(struct bch_fs *, struct bkey_s_c,
252 bool, s64, struct gc_pos,
253 struct bch_fs_usage *, u64, unsigned);
254 int bch2_mark_key(struct bch_fs *, struct bkey_s_c,
255 bool, s64, struct gc_pos,
256 struct bch_fs_usage *, u64, unsigned);
257 void bch2_mark_update(struct btree_insert *, struct btree_insert_entry *);
258 void bch2_fs_usage_apply(struct bch_fs *, struct bch_fs_usage *,
259 struct disk_reservation *, struct gc_pos);
261 /* disk reservations: */
263 void __bch2_disk_reservation_put(struct bch_fs *, struct disk_reservation *);
265 static inline void bch2_disk_reservation_put(struct bch_fs *c,
266 struct disk_reservation *res)
269 __bch2_disk_reservation_put(c, res);
272 #define BCH_DISK_RESERVATION_NOFAIL (1 << 0)
273 #define BCH_DISK_RESERVATION_GC_LOCK_HELD (1 << 1)
274 #define BCH_DISK_RESERVATION_BTREE_LOCKS_HELD (1 << 2)
276 int bch2_disk_reservation_add(struct bch_fs *,
277 struct disk_reservation *,
280 static inline struct disk_reservation
281 bch2_disk_reservation_init(struct bch_fs *c, unsigned nr_replicas)
283 return (struct disk_reservation) {
287 .gen = c->capacity_gen,
289 .nr_replicas = nr_replicas,
293 static inline int bch2_disk_reservation_get(struct bch_fs *c,
294 struct disk_reservation *res,
296 unsigned nr_replicas,
299 *res = bch2_disk_reservation_init(c, nr_replicas);
301 return bch2_disk_reservation_add(c, res, sectors * nr_replicas, flags);
304 int bch2_dev_buckets_resize(struct bch_fs *, struct bch_dev *, u64);
305 void bch2_dev_buckets_free(struct bch_dev *);
306 int bch2_dev_buckets_alloc(struct bch_fs *, struct bch_dev *);
308 #endif /* _BUCKETS_H */