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)
33 return rcu_dereference_check(ca->buckets,
35 percpu_rwsem_is_held(&ca->fs->usage_lock) ||
36 lockdep_is_held(&ca->fs->gc_lock) ||
37 lockdep_is_held(&ca->bucket_lock));
40 static inline struct bucket *bucket(struct bch_dev *ca, size_t b)
42 struct bucket_array *buckets = bucket_array(ca);
44 BUG_ON(b < buckets->first_bucket || b >= buckets->nbuckets);
45 return buckets->b + b;
48 static inline void bucket_io_clock_reset(struct bch_fs *c, struct bch_dev *ca,
51 bucket(ca, b)->io_time[rw] = c->bucket_clock[rw].hand;
54 static inline u16 bucket_last_io(struct bch_fs *c, struct bucket *g, int rw)
56 return c->bucket_clock[rw].hand - g->io_time[rw];
60 * bucket_gc_gen() returns the difference between the bucket's current gen and
61 * the oldest gen of any pointer into that bucket in the btree.
64 static inline u8 bucket_gc_gen(struct bch_dev *ca, size_t b)
66 return bucket(ca, b)->mark.gen - ca->oldest_gens[b];
69 static inline size_t PTR_BUCKET_NR(const struct bch_dev *ca,
70 const struct bch_extent_ptr *ptr)
72 return sector_to_bucket(ca, ptr->offset);
75 static inline struct bucket *PTR_BUCKET(struct bch_dev *ca,
76 const struct bch_extent_ptr *ptr)
78 return bucket(ca, PTR_BUCKET_NR(ca, ptr));
81 static inline struct bucket_mark ptr_bucket_mark(struct bch_dev *ca,
82 const struct bch_extent_ptr *ptr)
87 m = READ_ONCE(bucket(ca, PTR_BUCKET_NR(ca, ptr))->mark);
93 static inline int gen_cmp(u8 a, u8 b)
98 static inline int gen_after(u8 a, u8 b)
100 int r = gen_cmp(a, b);
102 return r > 0 ? r : 0;
106 * ptr_stale() - check if a pointer points into a bucket that has been
109 static inline u8 ptr_stale(struct bch_dev *ca,
110 const struct bch_extent_ptr *ptr)
112 return gen_after(ptr_bucket_mark(ca, ptr).gen, ptr->gen);
115 /* bucket gc marks */
117 static inline unsigned bucket_sectors_used(struct bucket_mark mark)
119 return mark.dirty_sectors + mark.cached_sectors;
122 static inline bool bucket_unused(struct bucket_mark mark)
124 return !mark.owned_by_allocator &&
126 !bucket_sectors_used(mark);
131 struct bch_dev_usage __bch2_dev_usage_read(struct bch_dev *);
132 struct bch_dev_usage bch2_dev_usage_read(struct bch_fs *, struct bch_dev *);
134 static inline u64 __dev_buckets_available(struct bch_dev *ca,
135 struct bch_dev_usage stats)
137 u64 total = ca->mi.nbuckets - ca->mi.first_bucket;
139 if (WARN_ONCE(stats.buckets_unavailable > total,
140 "buckets_unavailable overflow (%llu > %llu)\n",
141 stats.buckets_unavailable, total))
144 return total - stats.buckets_unavailable;
148 * Number of reclaimable buckets - only for use by the allocator thread:
150 static inline u64 dev_buckets_available(struct bch_fs *c, struct bch_dev *ca)
152 return __dev_buckets_available(ca, bch2_dev_usage_read(c, ca));
155 static inline u64 __dev_buckets_free(struct bch_dev *ca,
156 struct bch_dev_usage stats)
158 return __dev_buckets_available(ca, stats) +
159 fifo_used(&ca->free[RESERVE_NONE]) +
160 fifo_used(&ca->free_inc);
163 static inline u64 dev_buckets_free(struct bch_fs *c, struct bch_dev *ca)
165 return __dev_buckets_free(ca, bch2_dev_usage_read(c, ca));
168 /* Filesystem usage: */
170 struct bch_fs_usage __bch2_fs_usage_read(struct bch_fs *);
171 struct bch_fs_usage bch2_fs_usage_read(struct bch_fs *);
172 void bch2_fs_usage_apply(struct bch_fs *, struct bch_fs_usage *,
173 struct disk_reservation *, struct gc_pos);
175 u64 bch2_fs_sectors_used(struct bch_fs *, struct bch_fs_usage);
177 static inline bool is_available_bucket(struct bucket_mark mark)
179 return (!mark.owned_by_allocator &&
180 !mark.dirty_sectors &&
184 static inline bool bucket_needs_journal_commit(struct bucket_mark m,
187 return m.journal_seq_valid &&
188 ((s16) m.journal_seq - (s16) last_seq_ondisk > 0);
191 void bch2_bucket_seq_cleanup(struct bch_fs *);
193 void bch2_invalidate_bucket(struct bch_fs *, struct bch_dev *,
194 size_t, struct bucket_mark *);
195 void bch2_mark_alloc_bucket(struct bch_fs *, struct bch_dev *,
196 size_t, bool, struct gc_pos, unsigned);
197 void bch2_mark_metadata_bucket(struct bch_fs *, struct bch_dev *,
198 size_t, enum bch_data_type, unsigned,
199 struct gc_pos, unsigned);
201 #define BCH_BUCKET_MARK_NOATOMIC (1 << 0)
202 #define BCH_BUCKET_MARK_MAY_MAKE_UNAVAILABLE (1 << 1)
203 #define BCH_BUCKET_MARK_GC_WILL_VISIT (1 << 2)
204 #define BCH_BUCKET_MARK_GC_LOCK_HELD (1 << 3)
206 void bch2_mark_key(struct bch_fs *, enum bkey_type, struct bkey_s_c,
207 bool, s64, struct gc_pos,
208 struct bch_fs_usage *, u64, unsigned);
210 void bch2_recalc_sectors_available(struct bch_fs *);
212 void __bch2_disk_reservation_put(struct bch_fs *, struct disk_reservation *);
214 static inline void bch2_disk_reservation_put(struct bch_fs *c,
215 struct disk_reservation *res)
218 __bch2_disk_reservation_put(c, res);
221 #define BCH_DISK_RESERVATION_NOFAIL (1 << 0)
222 #define BCH_DISK_RESERVATION_GC_LOCK_HELD (1 << 1)
223 #define BCH_DISK_RESERVATION_BTREE_LOCKS_HELD (1 << 2)
225 int bch2_disk_reservation_add(struct bch_fs *,
226 struct disk_reservation *,
229 static inline struct disk_reservation
230 bch2_disk_reservation_init(struct bch_fs *c, unsigned nr_replicas)
232 return (struct disk_reservation) {
236 .gen = c->capacity_gen,
238 .nr_replicas = nr_replicas,
242 static inline int bch2_disk_reservation_get(struct bch_fs *c,
243 struct disk_reservation *res,
245 unsigned nr_replicas,
248 *res = bch2_disk_reservation_init(c, nr_replicas);
250 return bch2_disk_reservation_add(c, res, sectors * nr_replicas, flags);
253 int bch2_dev_buckets_resize(struct bch_fs *, struct bch_dev *, u64);
254 void bch2_dev_buckets_free(struct bch_dev *);
255 int bch2_dev_buckets_alloc(struct bch_fs *, struct bch_dev *);
257 #endif /* _BUCKETS_H */