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