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"
15 #define for_each_bucket(_b, _buckets) \
16 for (_b = (_buckets)->b + (_buckets)->first_bucket; \
17 _b < (_buckets)->b + (_buckets)->nbuckets; _b++)
19 static inline void bucket_unlock(struct bucket *b)
21 smp_store_release(&b->lock, 0);
24 static inline void bucket_lock(struct bucket *b)
26 while (xchg(&b->lock, 1))
30 static inline struct bucket_array *gc_bucket_array(struct bch_dev *ca)
32 return rcu_dereference_check(ca->buckets_gc,
34 percpu_rwsem_is_held(&ca->fs->mark_lock) ||
35 lockdep_is_held(&ca->fs->gc_lock) ||
36 lockdep_is_held(&ca->bucket_lock));
39 static inline struct bucket *gc_bucket(struct bch_dev *ca, size_t b)
41 struct bucket_array *buckets = gc_bucket_array(ca);
43 BUG_ON(b < buckets->first_bucket || b >= buckets->nbuckets);
44 return buckets->b + b;
47 static inline struct bucket_gens *bucket_gens(struct bch_dev *ca)
49 return rcu_dereference_check(ca->bucket_gens,
51 percpu_rwsem_is_held(&ca->fs->mark_lock) ||
52 lockdep_is_held(&ca->fs->gc_lock) ||
53 lockdep_is_held(&ca->bucket_lock));
56 static inline u8 *bucket_gen(struct bch_dev *ca, size_t b)
58 struct bucket_gens *gens = bucket_gens(ca);
60 BUG_ON(b < gens->first_bucket || b >= gens->nbuckets);
64 static inline size_t PTR_BUCKET_NR(const struct bch_dev *ca,
65 const struct bch_extent_ptr *ptr)
67 return sector_to_bucket(ca, ptr->offset);
70 static inline struct bpos PTR_BUCKET_POS(const struct bch_fs *c,
71 const struct bch_extent_ptr *ptr)
73 struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
75 return POS(ptr->dev, PTR_BUCKET_NR(ca, ptr));
78 static inline struct bpos PTR_BUCKET_POS_OFFSET(const struct bch_fs *c,
79 const struct bch_extent_ptr *ptr,
82 struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
84 return POS(ptr->dev, sector_to_bucket_and_offset(ca, ptr->offset, bucket_offset));
87 static inline struct bucket *PTR_GC_BUCKET(struct bch_dev *ca,
88 const struct bch_extent_ptr *ptr)
90 return gc_bucket(ca, PTR_BUCKET_NR(ca, ptr));
93 static inline enum bch_data_type ptr_data_type(const struct bkey *k,
94 const struct bch_extent_ptr *ptr)
96 if (bkey_is_btree_ptr(k))
97 return BCH_DATA_btree;
99 return ptr->cached ? BCH_DATA_cached : BCH_DATA_user;
102 static inline s64 ptr_disk_sectors(s64 sectors, struct extent_ptr_decoded p)
104 EBUG_ON(sectors < 0);
106 return crc_is_compressed(p.crc)
107 ? DIV_ROUND_UP_ULL(sectors * p.crc.compressed_size,
108 p.crc.uncompressed_size)
112 static inline int gen_cmp(u8 a, u8 b)
117 static inline int gen_after(u8 a, u8 b)
119 int r = gen_cmp(a, b);
121 return r > 0 ? r : 0;
125 * ptr_stale() - check if a pointer points into a bucket that has been
128 static inline u8 ptr_stale(struct bch_dev *ca,
129 const struct bch_extent_ptr *ptr)
134 ret = gen_after(*bucket_gen(ca, PTR_BUCKET_NR(ca, ptr)), ptr->gen);
142 void bch2_dev_usage_read_fast(struct bch_dev *, struct bch_dev_usage *);
143 static inline struct bch_dev_usage bch2_dev_usage_read(struct bch_dev *ca)
145 struct bch_dev_usage ret;
147 bch2_dev_usage_read_fast(ca, &ret);
151 void bch2_dev_usage_init(struct bch_dev *);
153 static inline u64 bch2_dev_buckets_reserved(struct bch_dev *ca, enum alloc_reserve reserve)
159 reserved += ca->mi.nbuckets >> 6;
161 case RESERVE_movinggc:
162 reserved += ca->nr_btree_reserve;
165 reserved += ca->nr_btree_reserve;
167 case RESERVE_btree_movinggc:
174 static inline u64 dev_buckets_free(struct bch_dev *ca,
175 struct bch_dev_usage usage,
176 enum alloc_reserve reserve)
179 usage.d[BCH_DATA_free].buckets -
180 ca->nr_open_buckets -
181 bch2_dev_buckets_reserved(ca, reserve));
184 static inline u64 __dev_buckets_available(struct bch_dev *ca,
185 struct bch_dev_usage usage,
186 enum alloc_reserve reserve)
189 usage.d[BCH_DATA_free].buckets
190 + usage.d[BCH_DATA_cached].buckets
191 + usage.d[BCH_DATA_need_gc_gens].buckets
192 + usage.d[BCH_DATA_need_discard].buckets
193 - ca->nr_open_buckets
194 - bch2_dev_buckets_reserved(ca, reserve));
197 static inline u64 dev_buckets_available(struct bch_dev *ca,
198 enum alloc_reserve reserve)
200 return __dev_buckets_available(ca, bch2_dev_usage_read(ca), reserve);
203 /* Filesystem usage: */
205 static inline unsigned fs_usage_u64s(struct bch_fs *c)
207 return sizeof(struct bch_fs_usage) / sizeof(u64) +
208 READ_ONCE(c->replicas.nr);
211 static inline unsigned dev_usage_u64s(void)
213 return sizeof(struct bch_dev_usage) / sizeof(u64);
216 u64 bch2_fs_usage_read_one(struct bch_fs *, u64 *);
218 struct bch_fs_usage_online *bch2_fs_usage_read(struct bch_fs *);
220 void bch2_fs_usage_acc_to_base(struct bch_fs *, unsigned);
222 void bch2_fs_usage_to_text(struct printbuf *,
223 struct bch_fs *, struct bch_fs_usage_online *);
225 u64 bch2_fs_sectors_used(struct bch_fs *, struct bch_fs_usage_online *);
227 struct bch_fs_usage_short
228 bch2_fs_usage_read_short(struct bch_fs *);
230 /* key/bucket marking: */
232 void bch2_fs_usage_initialize(struct bch_fs *);
234 int bch2_mark_metadata_bucket(struct bch_fs *, struct bch_dev *,
235 size_t, enum bch_data_type, unsigned,
236 struct gc_pos, unsigned);
238 int bch2_mark_alloc(struct btree_trans *, struct bkey_s_c, struct bkey_s_c, unsigned);
239 int bch2_mark_extent(struct btree_trans *, struct bkey_s_c, struct bkey_s_c, unsigned);
240 int bch2_mark_stripe(struct btree_trans *, struct bkey_s_c, struct bkey_s_c, unsigned);
241 int bch2_mark_inode(struct btree_trans *, struct bkey_s_c, struct bkey_s_c, unsigned);
242 int bch2_mark_reservation(struct btree_trans *, struct bkey_s_c, struct bkey_s_c, unsigned);
243 int bch2_mark_reflink_p(struct btree_trans *, struct bkey_s_c, struct bkey_s_c, unsigned);
245 int bch2_trans_mark_extent(struct btree_trans *, enum btree_id, unsigned, struct bkey_s_c, struct bkey_i *, unsigned);
246 int bch2_trans_mark_stripe(struct btree_trans *, enum btree_id, unsigned, struct bkey_s_c, struct bkey_i *, unsigned);
247 int bch2_trans_mark_inode(struct btree_trans *, enum btree_id, unsigned, struct bkey_s_c, struct bkey_i *, unsigned);
248 int bch2_trans_mark_reservation(struct btree_trans *, enum btree_id, unsigned, struct bkey_s_c, struct bkey_i *, unsigned);
249 int bch2_trans_mark_reflink_p(struct btree_trans *, enum btree_id, unsigned, struct bkey_s_c, struct bkey_i *, unsigned);
251 void bch2_trans_fs_usage_revert(struct btree_trans *, struct replicas_delta_list *);
252 int bch2_trans_fs_usage_apply(struct btree_trans *, struct replicas_delta_list *);
254 int bch2_trans_mark_metadata_bucket(struct btree_trans *, struct bch_dev *,
255 size_t, enum bch_data_type, unsigned);
256 int bch2_trans_mark_dev_sb(struct bch_fs *, struct bch_dev *);
258 /* disk reservations: */
260 static inline void bch2_disk_reservation_put(struct bch_fs *c,
261 struct disk_reservation *res)
264 this_cpu_sub(*c->online_reserved, res->sectors);
269 #define BCH_DISK_RESERVATION_NOFAIL (1 << 0)
271 int __bch2_disk_reservation_add(struct bch_fs *,
272 struct disk_reservation *,
275 static inline int bch2_disk_reservation_add(struct bch_fs *c, struct disk_reservation *res,
276 u64 sectors, int flags)
282 old = this_cpu_read(c->pcpu->sectors_available);
284 return __bch2_disk_reservation_add(c, res, sectors, flags);
287 } while (this_cpu_cmpxchg(c->pcpu->sectors_available, old, new) != old);
289 this_cpu_add(*c->online_reserved, sectors);
290 res->sectors += sectors;
293 return __bch2_disk_reservation_add(c, res, sectors, flags);
297 static inline struct disk_reservation
298 bch2_disk_reservation_init(struct bch_fs *c, unsigned nr_replicas)
300 return (struct disk_reservation) {
304 .gen = c->capacity_gen,
306 .nr_replicas = nr_replicas,
310 static inline int bch2_disk_reservation_get(struct bch_fs *c,
311 struct disk_reservation *res,
312 u64 sectors, 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 #define RESERVE_FACTOR 6
322 static inline u64 avail_factor(u64 r)
324 return div_u64(r << RESERVE_FACTOR, (1 << RESERVE_FACTOR) + 1);
327 int bch2_dev_buckets_resize(struct bch_fs *, struct bch_dev *, u64);
328 void bch2_dev_buckets_free(struct bch_dev *);
329 int bch2_dev_buckets_alloc(struct bch_fs *, struct bch_dev *);
331 #endif /* _BUCKETS_H */