]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/buckets.h
8f360b37927dc5d804ee934f1a43886e6c455072
[bcachefs-tools-debian] / libbcachefs / buckets.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Code for manipulating bucket marks for garbage collection.
4  *
5  * Copyright 2014 Datera, Inc.
6  */
7
8 #ifndef _BUCKETS_H
9 #define _BUCKETS_H
10
11 #include "buckets_types.h"
12 #include "extents.h"
13 #include "super.h"
14
15 #define for_each_bucket(_b, _buckets)                           \
16         for (_b = (_buckets)->b + (_buckets)->first_bucket;     \
17              _b < (_buckets)->b + (_buckets)->nbuckets; _b++)
18
19 static inline void bucket_unlock(struct bucket *b)
20 {
21         smp_store_release(&b->lock, 0);
22 }
23
24 static inline void bucket_lock(struct bucket *b)
25 {
26         while (xchg(&b->lock, 1))
27                 cpu_relax();
28 }
29
30 static inline struct bucket_array *gc_bucket_array(struct bch_dev *ca)
31 {
32         return rcu_dereference_check(ca->buckets_gc,
33                                      !ca->fs ||
34                                      percpu_rwsem_is_held(&ca->fs->mark_lock) ||
35                                      lockdep_is_held(&ca->fs->gc_lock) ||
36                                      lockdep_is_held(&ca->bucket_lock));
37 }
38
39 static inline struct bucket *gc_bucket(struct bch_dev *ca, size_t b)
40 {
41         struct bucket_array *buckets = gc_bucket_array(ca);
42
43         BUG_ON(b < buckets->first_bucket || b >= buckets->nbuckets);
44         return buckets->b + b;
45 }
46
47 static inline struct bucket_gens *bucket_gens(struct bch_dev *ca)
48 {
49         return rcu_dereference_check(ca->bucket_gens,
50                                      !ca->fs ||
51                                      percpu_rwsem_is_held(&ca->fs->mark_lock) ||
52                                      lockdep_is_held(&ca->fs->gc_lock) ||
53                                      lockdep_is_held(&ca->bucket_lock));
54 }
55
56 static inline u8 *bucket_gen(struct bch_dev *ca, size_t b)
57 {
58         struct bucket_gens *gens = bucket_gens(ca);
59
60         BUG_ON(b < gens->first_bucket || b >= gens->nbuckets);
61         return gens->b + b;
62 }
63
64 static inline size_t PTR_BUCKET_NR(const struct bch_dev *ca,
65                                    const struct bch_extent_ptr *ptr)
66 {
67         return sector_to_bucket(ca, ptr->offset);
68 }
69
70 static inline struct bpos PTR_BUCKET_POS(const struct bch_fs *c,
71                                    const struct bch_extent_ptr *ptr)
72 {
73         struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
74
75         return POS(ptr->dev, PTR_BUCKET_NR(ca, ptr));
76 }
77
78 static inline struct bucket *PTR_GC_BUCKET(struct bch_dev *ca,
79                                            const struct bch_extent_ptr *ptr)
80 {
81         return gc_bucket(ca, PTR_BUCKET_NR(ca, ptr));
82 }
83
84 static inline enum bch_data_type ptr_data_type(const struct bkey *k,
85                                                const struct bch_extent_ptr *ptr)
86 {
87         if (bkey_is_btree_ptr(k))
88                 return BCH_DATA_btree;
89
90         return ptr->cached ? BCH_DATA_cached : BCH_DATA_user;
91 }
92
93 static inline int gen_cmp(u8 a, u8 b)
94 {
95         return (s8) (a - b);
96 }
97
98 static inline int gen_after(u8 a, u8 b)
99 {
100         int r = gen_cmp(a, b);
101
102         return r > 0 ? r : 0;
103 }
104
105 /**
106  * ptr_stale() - check if a pointer points into a bucket that has been
107  * invalidated.
108  */
109 static inline u8 ptr_stale(struct bch_dev *ca,
110                            const struct bch_extent_ptr *ptr)
111 {
112         u8 ret;
113
114         rcu_read_lock();
115         ret = gen_after(*bucket_gen(ca, PTR_BUCKET_NR(ca, ptr)), ptr->gen);
116         rcu_read_unlock();
117
118         return ret;
119 }
120
121 /* Device usage: */
122
123 struct bch_dev_usage bch2_dev_usage_read(struct bch_dev *);
124 void bch2_dev_usage_init(struct bch_dev *);
125
126 static inline u64 bch2_dev_buckets_reserved(struct bch_dev *ca, enum alloc_reserve reserve)
127 {
128         s64 reserved = 0;
129
130         switch (reserve) {
131         case RESERVE_none:
132                 reserved += ca->mi.nbuckets >> 6;
133                 fallthrough;
134         case RESERVE_movinggc:
135                 reserved += ca->nr_btree_reserve;
136                 fallthrough;
137         case RESERVE_btree:
138                 reserved += ca->nr_btree_reserve;
139                 fallthrough;
140         case RESERVE_btree_movinggc:
141                 break;
142         }
143
144         return reserved;
145 }
146
147 static inline u64 __dev_buckets_available(struct bch_dev *ca,
148                                           struct bch_dev_usage usage,
149                                           enum alloc_reserve reserve)
150 {
151         return max_t(s64, 0,
152                      usage.d[BCH_DATA_free].buckets -
153                      ca->nr_open_buckets -
154                      bch2_dev_buckets_reserved(ca, reserve));
155 }
156
157 static inline u64 dev_buckets_available(struct bch_dev *ca,
158                                         enum alloc_reserve reserve)
159 {
160         return __dev_buckets_available(ca, bch2_dev_usage_read(ca), reserve);
161 }
162
163 /* Filesystem usage: */
164
165 static inline unsigned fs_usage_u64s(struct bch_fs *c)
166 {
167         return sizeof(struct bch_fs_usage) / sizeof(u64) +
168                 READ_ONCE(c->replicas.nr);
169 }
170
171 static inline unsigned dev_usage_u64s(void)
172 {
173         return sizeof(struct bch_dev_usage) / sizeof(u64);
174 }
175
176 u64 bch2_fs_usage_read_one(struct bch_fs *, u64 *);
177
178 struct bch_fs_usage_online *bch2_fs_usage_read(struct bch_fs *);
179
180 void bch2_fs_usage_acc_to_base(struct bch_fs *, unsigned);
181
182 void bch2_fs_usage_to_text(struct printbuf *,
183                            struct bch_fs *, struct bch_fs_usage_online *);
184
185 u64 bch2_fs_sectors_used(struct bch_fs *, struct bch_fs_usage_online *);
186
187 struct bch_fs_usage_short
188 bch2_fs_usage_read_short(struct bch_fs *);
189
190 /* key/bucket marking: */
191
192 void bch2_fs_usage_initialize(struct bch_fs *);
193
194 int bch2_mark_metadata_bucket(struct bch_fs *, struct bch_dev *,
195                               size_t, enum bch_data_type, unsigned,
196                               struct gc_pos, unsigned);
197
198 int bch2_mark_alloc(struct btree_trans *, struct bkey_s_c, struct bkey_s_c, unsigned);
199 int bch2_mark_extent(struct btree_trans *, struct bkey_s_c, struct bkey_s_c, unsigned);
200 int bch2_mark_stripe(struct btree_trans *, struct bkey_s_c, struct bkey_s_c, unsigned);
201 int bch2_mark_inode(struct btree_trans *, struct bkey_s_c, struct bkey_s_c, unsigned);
202 int bch2_mark_reservation(struct btree_trans *, struct bkey_s_c, struct bkey_s_c, unsigned);
203 int bch2_mark_reflink_p(struct btree_trans *, struct bkey_s_c, struct bkey_s_c, unsigned);
204
205 int bch2_trans_mark_extent(struct btree_trans *, struct bkey_s_c, struct bkey_i *, unsigned);
206 int bch2_trans_mark_stripe(struct btree_trans *, struct bkey_s_c, struct bkey_i *, unsigned);
207 int bch2_trans_mark_inode(struct btree_trans *, struct bkey_s_c, struct bkey_i *, unsigned);
208 int bch2_trans_mark_reservation(struct btree_trans *, struct bkey_s_c, struct bkey_i *, unsigned);
209 int bch2_trans_mark_reflink_p(struct btree_trans *, struct bkey_s_c, struct bkey_i *, unsigned);
210
211 int bch2_mark_key(struct btree_trans *, struct bkey_s_c, struct bkey_s_c, unsigned);
212
213 int bch2_trans_mark_key(struct btree_trans *, struct bkey_s_c,
214                         struct bkey_i *, unsigned);
215
216 static inline int bch2_trans_mark_old(struct btree_trans *trans,
217                                       struct bkey_s_c old, unsigned flags)
218 {
219         struct bkey_i deleted;
220
221         bkey_init(&deleted.k);
222         deleted.k.p = old.k->p;
223
224         return bch2_trans_mark_key(trans, old, &deleted,
225                                    BTREE_TRIGGER_OVERWRITE|flags);
226 }
227
228 static inline int bch2_trans_mark_new(struct btree_trans *trans,
229                                       struct bkey_i *new, unsigned flags)
230 {
231         struct bkey_i deleted;
232
233         bkey_init(&deleted.k);
234         deleted.k.p = new->k.p;
235
236         return bch2_trans_mark_key(trans, bkey_i_to_s_c(&deleted), new,
237                                    BTREE_TRIGGER_INSERT|flags);
238 }
239
240 int bch2_trans_fs_usage_apply(struct btree_trans *, struct replicas_delta_list *);
241
242 int bch2_trans_mark_metadata_bucket(struct btree_trans *, struct bch_dev *,
243                                     size_t, enum bch_data_type, unsigned);
244 int bch2_trans_mark_dev_sb(struct bch_fs *, struct bch_dev *);
245
246 /* disk reservations: */
247
248 static inline void bch2_disk_reservation_put(struct bch_fs *c,
249                                              struct disk_reservation *res)
250 {
251         this_cpu_sub(*c->online_reserved, res->sectors);
252         res->sectors = 0;
253 }
254
255 #define BCH_DISK_RESERVATION_NOFAIL             (1 << 0)
256
257 int bch2_disk_reservation_add(struct bch_fs *,
258                               struct disk_reservation *,
259                               u64, int);
260
261 static inline struct disk_reservation
262 bch2_disk_reservation_init(struct bch_fs *c, unsigned nr_replicas)
263 {
264         return (struct disk_reservation) {
265                 .sectors        = 0,
266 #if 0
267                 /* not used yet: */
268                 .gen            = c->capacity_gen,
269 #endif
270                 .nr_replicas    = nr_replicas,
271         };
272 }
273
274 static inline int bch2_disk_reservation_get(struct bch_fs *c,
275                                             struct disk_reservation *res,
276                                             u64 sectors, unsigned nr_replicas,
277                                             int flags)
278 {
279         *res = bch2_disk_reservation_init(c, nr_replicas);
280
281         return bch2_disk_reservation_add(c, res, sectors * nr_replicas, flags);
282 }
283
284 #define RESERVE_FACTOR  6
285
286 static inline u64 avail_factor(u64 r)
287 {
288         return div_u64(r << RESERVE_FACTOR, (1 << RESERVE_FACTOR) + 1);
289 }
290
291 int bch2_dev_buckets_resize(struct bch_fs *, struct bch_dev *, u64);
292 void bch2_dev_buckets_free(struct bch_dev *);
293 int bch2_dev_buckets_alloc(struct bch_fs *, struct bch_dev *);
294
295 #endif /* _BUCKETS_H */