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 static inline void bucket_unlock(struct bucket *b)
20 smp_store_release(&b->lock, 0);
23 static inline void bucket_lock(struct bucket *b)
25 while (xchg(&b->lock, 1))
29 static inline struct bucket_array *gc_bucket_array(struct bch_dev *ca)
31 return rcu_dereference_check(ca->buckets_gc,
33 percpu_rwsem_is_held(&ca->fs->mark_lock) ||
34 lockdep_is_held(&ca->fs->gc_lock) ||
35 lockdep_is_held(&ca->bucket_lock));
38 static inline struct bucket *gc_bucket(struct bch_dev *ca, size_t b)
40 struct bucket_array *buckets = gc_bucket_array(ca);
42 BUG_ON(b < buckets->first_bucket || b >= buckets->nbuckets);
43 return buckets->b + b;
46 static inline struct bucket_gens *bucket_gens(struct bch_dev *ca)
48 return rcu_dereference_check(ca->bucket_gens,
50 percpu_rwsem_is_held(&ca->fs->mark_lock) ||
51 lockdep_is_held(&ca->fs->gc_lock) ||
52 lockdep_is_held(&ca->bucket_lock));
55 static inline u8 *bucket_gen(struct bch_dev *ca, size_t b)
57 struct bucket_gens *gens = bucket_gens(ca);
59 BUG_ON(b < gens->first_bucket || b >= gens->nbuckets);
63 static inline size_t PTR_BUCKET_NR(const struct bch_dev *ca,
64 const struct bch_extent_ptr *ptr)
66 return sector_to_bucket(ca, ptr->offset);
69 static inline struct bpos PTR_BUCKET_POS(const struct bch_fs *c,
70 const struct bch_extent_ptr *ptr)
72 struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
74 return POS(ptr->dev, PTR_BUCKET_NR(ca, ptr));
77 static inline struct bucket *PTR_GC_BUCKET(struct bch_dev *ca,
78 const struct bch_extent_ptr *ptr)
80 return gc_bucket(ca, PTR_BUCKET_NR(ca, ptr));
83 static inline enum bch_data_type ptr_data_type(const struct bkey *k,
84 const struct bch_extent_ptr *ptr)
86 if (k->type == KEY_TYPE_btree_ptr ||
87 k->type == KEY_TYPE_btree_ptr_v2)
88 return BCH_DATA_btree;
90 return ptr->cached ? BCH_DATA_cached : BCH_DATA_user;
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)
115 ret = gen_after(*bucket_gen(ca, PTR_BUCKET_NR(ca, ptr)), ptr->gen);
123 struct bch_dev_usage bch2_dev_usage_read(struct bch_dev *);
125 static inline u64 __dev_buckets_available(struct bch_dev *ca,
126 struct bch_dev_usage stats,
127 enum alloc_reserve reserve)
129 s64 total = ca->mi.nbuckets - ca->mi.first_bucket;
134 reserved += ca->mi.nbuckets >> 6;
136 case RESERVE_movinggc:
137 reserved += ca->nr_btree_reserve;
140 reserved += ca->nr_btree_reserve;
142 case RESERVE_btree_movinggc:
148 if (WARN_ONCE(stats.buckets_unavailable > total,
149 "buckets_unavailable overflow (%llu > %llu)\n",
150 stats.buckets_unavailable, total))
155 stats.buckets_unavailable -
156 ca->nr_open_buckets -
160 static inline u64 dev_buckets_available(struct bch_dev *ca,
161 enum alloc_reserve reserve)
163 return __dev_buckets_available(ca, bch2_dev_usage_read(ca), reserve);
166 /* Filesystem usage: */
168 static inline unsigned fs_usage_u64s(struct bch_fs *c)
170 return sizeof(struct bch_fs_usage) / sizeof(u64) +
171 READ_ONCE(c->replicas.nr);
174 static inline unsigned dev_usage_u64s(void)
176 return sizeof(struct bch_dev_usage) / sizeof(u64);
179 u64 bch2_fs_usage_read_one(struct bch_fs *, u64 *);
181 struct bch_fs_usage_online *bch2_fs_usage_read(struct bch_fs *);
183 void bch2_fs_usage_acc_to_base(struct bch_fs *, unsigned);
185 void bch2_fs_usage_to_text(struct printbuf *,
186 struct bch_fs *, struct bch_fs_usage_online *);
188 u64 bch2_fs_sectors_used(struct bch_fs *, struct bch_fs_usage_online *);
190 struct bch_fs_usage_short
191 bch2_fs_usage_read_short(struct bch_fs *);
193 /* key/bucket marking: */
195 void bch2_fs_usage_initialize(struct bch_fs *);
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 int bch2_mark_alloc(struct btree_trans *, struct bkey_s_c, struct bkey_s_c, unsigned);
202 int bch2_mark_extent(struct btree_trans *, struct bkey_s_c, struct bkey_s_c, unsigned);
203 int bch2_mark_stripe(struct btree_trans *, struct bkey_s_c, struct bkey_s_c, unsigned);
204 int bch2_mark_inode(struct btree_trans *, struct bkey_s_c, struct bkey_s_c, unsigned);
205 int bch2_mark_reservation(struct btree_trans *, struct bkey_s_c, struct bkey_s_c, unsigned);
206 int bch2_mark_reflink_p(struct btree_trans *, struct bkey_s_c, struct bkey_s_c, unsigned);
208 int bch2_trans_mark_extent(struct btree_trans *, struct bkey_s_c, struct bkey_i *, unsigned);
209 int bch2_trans_mark_stripe(struct btree_trans *, struct bkey_s_c, struct bkey_i *, unsigned);
210 int bch2_trans_mark_inode(struct btree_trans *, struct bkey_s_c, struct bkey_i *, unsigned);
211 int bch2_trans_mark_reservation(struct btree_trans *, struct bkey_s_c, struct bkey_i *, unsigned);
212 int bch2_trans_mark_reflink_p(struct btree_trans *, struct bkey_s_c, struct bkey_i *, unsigned);
214 int bch2_mark_key(struct btree_trans *, struct bkey_s_c, struct bkey_s_c, unsigned);
216 int bch2_trans_mark_key(struct btree_trans *, struct bkey_s_c,
217 struct bkey_i *, unsigned);
219 static inline int bch2_trans_mark_old(struct btree_trans *trans,
220 struct bkey_s_c old, unsigned flags)
222 struct bkey_i deleted;
224 bkey_init(&deleted.k);
225 deleted.k.p = old.k->p;
227 return bch2_trans_mark_key(trans, old, &deleted,
228 BTREE_TRIGGER_OVERWRITE|flags);
231 static inline int bch2_trans_mark_new(struct btree_trans *trans,
232 struct bkey_i *new, unsigned flags)
234 struct bkey_i deleted;
236 bkey_init(&deleted.k);
237 deleted.k.p = new->k.p;
239 return bch2_trans_mark_key(trans, bkey_i_to_s_c(&deleted), new,
240 BTREE_TRIGGER_INSERT|flags);
243 int bch2_trans_fs_usage_apply(struct btree_trans *, struct replicas_delta_list *);
245 int bch2_trans_mark_metadata_bucket(struct btree_trans *, struct bch_dev *,
246 size_t, enum bch_data_type, unsigned);
247 int bch2_trans_mark_dev_sb(struct bch_fs *, struct bch_dev *);
249 /* disk reservations: */
251 static inline void bch2_disk_reservation_put(struct bch_fs *c,
252 struct disk_reservation *res)
254 this_cpu_sub(*c->online_reserved, res->sectors);
258 #define BCH_DISK_RESERVATION_NOFAIL (1 << 0)
260 int bch2_disk_reservation_add(struct bch_fs *,
261 struct disk_reservation *,
264 static inline struct disk_reservation
265 bch2_disk_reservation_init(struct bch_fs *c, unsigned nr_replicas)
267 return (struct disk_reservation) {
271 .gen = c->capacity_gen,
273 .nr_replicas = nr_replicas,
277 static inline int bch2_disk_reservation_get(struct bch_fs *c,
278 struct disk_reservation *res,
279 u64 sectors, unsigned nr_replicas,
282 *res = bch2_disk_reservation_init(c, nr_replicas);
284 return bch2_disk_reservation_add(c, res, sectors * nr_replicas, flags);
287 #define RESERVE_FACTOR 6
289 static inline u64 avail_factor(u64 r)
291 return div_u64(r << RESERVE_FACTOR, (1 << RESERVE_FACTOR) + 1);
294 int bch2_dev_buckets_resize(struct bch_fs *, struct bch_dev *, u64);
295 void bch2_dev_buckets_free(struct bch_dev *);
296 int bch2_dev_buckets_alloc(struct bch_fs *, struct bch_dev *);
298 #endif /* _BUCKETS_H */