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, ca) \
14 for (b = (ca)->buckets + (ca)->mi.first_bucket; \
15 b < (ca)->buckets + (ca)->mi.nbuckets; b++)
17 #define bucket_cmpxchg(g, new, expr) \
19 u64 _v = READ_ONCE((g)->_mark.counter); \
20 struct bucket_mark _old; \
23 (new).counter = _old.counter = _v; \
25 } while ((_v = cmpxchg(&(g)->_mark.counter, \
27 (new).counter)) != _old.counter);\
32 * bucket_gc_gen() returns the difference between the bucket's current gen and
33 * the oldest gen of any pointer into that bucket in the btree.
36 static inline u8 bucket_gc_gen(struct bch_dev *ca, struct bucket *g)
38 unsigned long r = g - ca->buckets;
39 return g->mark.gen - ca->oldest_gens[r];
42 static inline size_t PTR_BUCKET_NR(const struct bch_dev *ca,
43 const struct bch_extent_ptr *ptr)
45 return sector_to_bucket(ca, ptr->offset);
48 static inline struct bucket *PTR_BUCKET(const struct bch_dev *ca,
49 const struct bch_extent_ptr *ptr)
51 return ca->buckets + PTR_BUCKET_NR(ca, ptr);
54 static inline int gen_cmp(u8 a, u8 b)
59 static inline int gen_after(u8 a, u8 b)
61 int r = gen_cmp(a, b);
67 * ptr_stale() - check if a pointer points into a bucket that has been
70 static inline u8 ptr_stale(const struct bch_dev *ca,
71 const struct bch_extent_ptr *ptr)
73 return gen_after(PTR_BUCKET(ca, ptr)->mark.gen, ptr->gen);
78 /* The dirty and cached sector counts saturate. If this occurs,
79 * reference counting alone will not free the bucket, and a btree
80 * GC must be performed. */
81 #define GC_MAX_SECTORS_USED ((1U << 15) - 1)
83 static inline unsigned bucket_sectors_used(struct bucket_mark mark)
85 return mark.dirty_sectors + mark.cached_sectors;
88 static inline bool bucket_unused(struct bucket_mark mark)
90 return !mark.owned_by_allocator &&
92 !bucket_sectors_used(mark);
95 /* Per device stats: */
97 struct bch_dev_usage __bch2_dev_usage_read(struct bch_dev *);
98 struct bch_dev_usage bch2_dev_usage_read(struct bch_dev *);
100 static inline u64 __dev_buckets_available(struct bch_dev *ca,
101 struct bch_dev_usage stats)
104 ca->mi.nbuckets - ca->mi.first_bucket -
105 stats.buckets[S_META] -
106 stats.buckets[S_DIRTY] -
107 stats.buckets_alloc);
111 * Number of reclaimable buckets - only for use by the allocator thread:
113 static inline u64 dev_buckets_available(struct bch_dev *ca)
115 return __dev_buckets_available(ca, bch2_dev_usage_read(ca));
118 static inline u64 __dev_buckets_free(struct bch_dev *ca,
119 struct bch_dev_usage stats)
121 return __dev_buckets_available(ca, stats) +
122 fifo_used(&ca->free[RESERVE_NONE]) +
123 fifo_used(&ca->free_inc);
126 static inline u64 dev_buckets_free(struct bch_dev *ca)
128 return __dev_buckets_free(ca, bch2_dev_usage_read(ca));
131 /* Cache set stats: */
133 struct bch_fs_usage __bch2_fs_usage_read(struct bch_fs *);
134 struct bch_fs_usage bch2_fs_usage_read(struct bch_fs *);
135 void bch2_fs_usage_apply(struct bch_fs *, struct bch_fs_usage *,
136 struct disk_reservation *, struct gc_pos);
138 struct fs_usage_sum {
143 static inline struct fs_usage_sum __fs_usage_sum(struct bch_fs_usage stats)
145 struct fs_usage_sum sum = { 0 };
148 for (i = 0; i < BCH_REPLICAS_MAX; i++) {
149 sum.data += (stats.s[i].data[S_META] +
150 stats.s[i].data[S_DIRTY]) * (i + 1);
151 sum.reserved += stats.s[i].persistent_reserved * (i + 1);
154 sum.reserved += stats.online_reserved;
158 static inline u64 __bch2_fs_sectors_used(struct bch_fs *c)
160 struct fs_usage_sum sum = __fs_usage_sum(__bch2_fs_usage_read(c));
162 return sum.data + sum.reserved + (sum.reserved >> 7);
165 static inline u64 bch2_fs_sectors_used(struct bch_fs *c)
167 return min(c->capacity, __bch2_fs_sectors_used(c));
170 static inline bool is_available_bucket(struct bucket_mark mark)
172 return (!mark.owned_by_allocator &&
173 mark.data_type == BUCKET_DATA &&
174 !mark.dirty_sectors &&
178 static inline bool bucket_needs_journal_commit(struct bucket_mark m,
181 return m.journal_seq_valid &&
182 ((s16) m.journal_seq - (s16) last_seq_ondisk > 0);
185 void bch2_bucket_seq_cleanup(struct bch_fs *);
187 bool bch2_invalidate_bucket(struct bch_dev *, struct bucket *,
188 struct bucket_mark *);
189 bool bch2_mark_alloc_bucket_startup(struct bch_dev *, struct bucket *);
190 void bch2_mark_free_bucket(struct bch_dev *, struct bucket *);
191 void bch2_mark_alloc_bucket(struct bch_dev *, struct bucket *, bool);
192 void bch2_mark_metadata_bucket(struct bch_dev *, struct bucket *,
193 enum bucket_data_type, bool);
195 #define BCH_BUCKET_MARK_NOATOMIC (1 << 0)
196 #define BCH_BUCKET_MARK_GC_WILL_VISIT (1 << 1)
197 #define BCH_BUCKET_MARK_MAY_MAKE_UNAVAILABLE (1 << 2)
199 void __bch2_mark_key(struct bch_fs *, struct bkey_s_c, s64, bool,
200 struct bch_fs_usage *, u64, unsigned);
202 void bch2_gc_mark_key(struct bch_fs *, struct bkey_s_c,
203 s64, bool, unsigned);
204 void bch2_mark_key(struct bch_fs *, struct bkey_s_c, s64, bool,
205 struct gc_pos, struct bch_fs_usage *, u64);
207 void bch2_recalc_sectors_available(struct bch_fs *);
209 void bch2_disk_reservation_put(struct bch_fs *,
210 struct disk_reservation *);
212 #define BCH_DISK_RESERVATION_NOFAIL (1 << 0)
213 #define BCH_DISK_RESERVATION_METADATA (1 << 1)
214 #define BCH_DISK_RESERVATION_GC_LOCK_HELD (1 << 2)
215 #define BCH_DISK_RESERVATION_BTREE_LOCKS_HELD (1 << 3)
217 int bch2_disk_reservation_add(struct bch_fs *,
218 struct disk_reservation *,
220 int bch2_disk_reservation_get(struct bch_fs *,
221 struct disk_reservation *,
224 #endif /* _BUCKETS_H */