49a3807a5eabff66b89a38970f9816a943076661
[linux-2.6-block.git] / fs / bcachefs / 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 "sb-members.h"
14
15 static inline u64 sector_to_bucket(const struct bch_dev *ca, sector_t s)
16 {
17         return div_u64(s, ca->mi.bucket_size);
18 }
19
20 static inline sector_t bucket_to_sector(const struct bch_dev *ca, size_t b)
21 {
22         return ((sector_t) b) * ca->mi.bucket_size;
23 }
24
25 static inline sector_t bucket_remainder(const struct bch_dev *ca, sector_t s)
26 {
27         u32 remainder;
28
29         div_u64_rem(s, ca->mi.bucket_size, &remainder);
30         return remainder;
31 }
32
33 static inline u64 sector_to_bucket_and_offset(const struct bch_dev *ca, sector_t s, u32 *offset)
34 {
35         return div_u64_rem(s, ca->mi.bucket_size, offset);
36 }
37
38 #define for_each_bucket(_b, _buckets)                           \
39         for (_b = (_buckets)->b + (_buckets)->first_bucket;     \
40              _b < (_buckets)->b + (_buckets)->nbuckets; _b++)
41
42 static inline void bucket_unlock(struct bucket *b)
43 {
44         BUILD_BUG_ON(!((union ulong_byte_assert) { .ulong = 1UL << BUCKET_LOCK_BITNR }).byte);
45
46         clear_bit_unlock(BUCKET_LOCK_BITNR, (void *) &b->lock);
47         smp_mb__after_atomic();
48         wake_up_bit((void *) &b->lock, BUCKET_LOCK_BITNR);
49 }
50
51 static inline void bucket_lock(struct bucket *b)
52 {
53         wait_on_bit_lock((void *) &b->lock, BUCKET_LOCK_BITNR,
54                          TASK_UNINTERRUPTIBLE);
55 }
56
57 static inline struct bucket *gc_bucket(struct bch_dev *ca, size_t b)
58 {
59         return bucket_valid(ca, b)
60                 ? genradix_ptr(&ca->buckets_gc, b)
61                 : NULL;
62 }
63
64 static inline struct bucket_gens *bucket_gens(struct bch_dev *ca)
65 {
66         return rcu_dereference_check(ca->bucket_gens,
67                                      lockdep_is_held(&ca->fs->state_lock));
68 }
69
70 static inline u8 *bucket_gen(struct bch_dev *ca, size_t b)
71 {
72         struct bucket_gens *gens = bucket_gens(ca);
73
74         if (b - gens->first_bucket >= gens->nbuckets_minus_first)
75                 return NULL;
76         return gens->b + b;
77 }
78
79 static inline int bucket_gen_get_rcu(struct bch_dev *ca, size_t b)
80 {
81         u8 *gen = bucket_gen(ca, b);
82         return gen ? *gen : -1;
83 }
84
85 static inline int bucket_gen_get(struct bch_dev *ca, size_t b)
86 {
87         guard(rcu)();
88         return bucket_gen_get_rcu(ca, b);
89 }
90
91 static inline size_t PTR_BUCKET_NR(const struct bch_dev *ca,
92                                    const struct bch_extent_ptr *ptr)
93 {
94         return sector_to_bucket(ca, ptr->offset);
95 }
96
97 static inline struct bpos PTR_BUCKET_POS(const struct bch_dev *ca,
98                                          const struct bch_extent_ptr *ptr)
99 {
100         return POS(ptr->dev, PTR_BUCKET_NR(ca, ptr));
101 }
102
103 static inline struct bpos PTR_BUCKET_POS_OFFSET(const struct bch_dev *ca,
104                                                 const struct bch_extent_ptr *ptr,
105                                                 u32 *bucket_offset)
106 {
107         return POS(ptr->dev, sector_to_bucket_and_offset(ca, ptr->offset, bucket_offset));
108 }
109
110 static inline struct bucket *PTR_GC_BUCKET(struct bch_dev *ca,
111                                            const struct bch_extent_ptr *ptr)
112 {
113         return gc_bucket(ca, PTR_BUCKET_NR(ca, ptr));
114 }
115
116 static inline enum bch_data_type ptr_data_type(const struct bkey *k,
117                                                const struct bch_extent_ptr *ptr)
118 {
119         if (bkey_is_btree_ptr(k))
120                 return BCH_DATA_btree;
121
122         return ptr->cached ? BCH_DATA_cached : BCH_DATA_user;
123 }
124
125 static inline s64 ptr_disk_sectors(s64 sectors, struct extent_ptr_decoded p)
126 {
127         EBUG_ON(sectors < 0);
128
129         return crc_is_compressed(p.crc)
130                 ? DIV_ROUND_UP_ULL(sectors * p.crc.compressed_size,
131                                    p.crc.uncompressed_size)
132                 : sectors;
133 }
134
135 static inline int gen_cmp(u8 a, u8 b)
136 {
137         return (s8) (a - b);
138 }
139
140 static inline int gen_after(u8 a, u8 b)
141 {
142         return max(0, gen_cmp(a, b));
143 }
144
145 static inline int dev_ptr_stale_rcu(struct bch_dev *ca, const struct bch_extent_ptr *ptr)
146 {
147         int gen = bucket_gen_get_rcu(ca, PTR_BUCKET_NR(ca, ptr));
148         return gen < 0 ? gen : gen_after(gen, ptr->gen);
149 }
150
151 /**
152  * dev_ptr_stale() - check if a pointer points into a bucket that has been
153  * invalidated.
154  */
155 static inline int dev_ptr_stale(struct bch_dev *ca, const struct bch_extent_ptr *ptr)
156 {
157         guard(rcu)();
158         return dev_ptr_stale_rcu(ca, ptr);
159 }
160
161 /* Device usage: */
162
163 void bch2_dev_usage_read_fast(struct bch_dev *, struct bch_dev_usage *);
164 static inline struct bch_dev_usage bch2_dev_usage_read(struct bch_dev *ca)
165 {
166         struct bch_dev_usage ret;
167
168         bch2_dev_usage_read_fast(ca, &ret);
169         return ret;
170 }
171
172 void bch2_dev_usage_full_read_fast(struct bch_dev *, struct bch_dev_usage_full *);
173 static inline struct bch_dev_usage_full bch2_dev_usage_full_read(struct bch_dev *ca)
174 {
175         struct bch_dev_usage_full ret;
176
177         bch2_dev_usage_full_read_fast(ca, &ret);
178         return ret;
179 }
180
181 void bch2_dev_usage_to_text(struct printbuf *, struct bch_dev *, struct bch_dev_usage_full *);
182
183 static inline u64 bch2_dev_buckets_reserved(struct bch_dev *ca, enum bch_watermark watermark)
184 {
185         s64 reserved = 0;
186
187         switch (watermark) {
188         case BCH_WATERMARK_NR:
189                 BUG();
190         case BCH_WATERMARK_stripe:
191                 reserved += ca->mi.nbuckets >> 6;
192                 fallthrough;
193         case BCH_WATERMARK_normal:
194                 reserved += ca->mi.nbuckets >> 6;
195                 fallthrough;
196         case BCH_WATERMARK_copygc:
197                 reserved += ca->nr_btree_reserve;
198                 fallthrough;
199         case BCH_WATERMARK_btree:
200                 reserved += ca->nr_btree_reserve;
201                 fallthrough;
202         case BCH_WATERMARK_btree_copygc:
203         case BCH_WATERMARK_reclaim:
204         case BCH_WATERMARK_interior_updates:
205                 break;
206         }
207
208         return reserved;
209 }
210
211 static inline u64 dev_buckets_free(struct bch_dev *ca,
212                                    struct bch_dev_usage usage,
213                                    enum bch_watermark watermark)
214 {
215         return max_t(s64, 0,
216                      usage.buckets[BCH_DATA_free]-
217                      ca->nr_open_buckets -
218                      bch2_dev_buckets_reserved(ca, watermark));
219 }
220
221 static inline u64 __dev_buckets_available(struct bch_dev *ca,
222                                           struct bch_dev_usage usage,
223                                           enum bch_watermark watermark)
224 {
225         return max_t(s64, 0,
226                        usage.buckets[BCH_DATA_free]
227                      + usage.buckets[BCH_DATA_cached]
228                      + usage.buckets[BCH_DATA_need_gc_gens]
229                      + usage.buckets[BCH_DATA_need_discard]
230                      - ca->nr_open_buckets
231                      - bch2_dev_buckets_reserved(ca, watermark));
232 }
233
234 static inline u64 dev_buckets_available(struct bch_dev *ca,
235                                         enum bch_watermark watermark)
236 {
237         return __dev_buckets_available(ca, bch2_dev_usage_read(ca), watermark);
238 }
239
240 /* Filesystem usage: */
241
242 struct bch_fs_usage_short
243 bch2_fs_usage_read_short(struct bch_fs *);
244
245 int bch2_bucket_ref_update(struct btree_trans *, struct bch_dev *,
246                            struct bkey_s_c, const struct bch_extent_ptr *,
247                            s64, enum bch_data_type, u8, u8, u32 *);
248
249 int bch2_check_fix_ptrs(struct btree_trans *,
250                         enum btree_id, unsigned, struct bkey_s_c,
251                         enum btree_iter_update_trigger_flags);
252
253 int bch2_trigger_extent(struct btree_trans *, enum btree_id, unsigned,
254                         struct bkey_s_c, struct bkey_s,
255                         enum btree_iter_update_trigger_flags);
256 int bch2_trigger_reservation(struct btree_trans *, enum btree_id, unsigned,
257                           struct bkey_s_c, struct bkey_s,
258                           enum btree_iter_update_trigger_flags);
259
260 #define trigger_run_overwrite_then_insert(_fn, _trans, _btree_id, _level, _old, _new, _flags)\
261 ({                                                                                              \
262         int ret = 0;                                                                            \
263                                                                                                 \
264         if (_old.k->type)                                                                       \
265                 ret = _fn(_trans, _btree_id, _level, _old, _flags & ~BTREE_TRIGGER_insert);     \
266         if (!ret && _new.k->type)                                                               \
267                 ret = _fn(_trans, _btree_id, _level, _new.s_c, _flags & ~BTREE_TRIGGER_overwrite);\
268         ret;                                                                                    \
269 })
270
271 void bch2_trans_account_disk_usage_change(struct btree_trans *);
272
273 int bch2_trans_mark_metadata_bucket(struct btree_trans *, struct bch_dev *, u64,
274                                     enum bch_data_type, unsigned,
275                                     enum btree_iter_update_trigger_flags);
276 int bch2_trans_mark_dev_sb(struct bch_fs *, struct bch_dev *,
277                                     enum btree_iter_update_trigger_flags);
278 int bch2_trans_mark_dev_sbs_flags(struct bch_fs *,
279                                     enum btree_iter_update_trigger_flags);
280 int bch2_trans_mark_dev_sbs(struct bch_fs *);
281
282 bool bch2_is_superblock_bucket(struct bch_dev *, u64);
283
284 static inline const char *bch2_data_type_str(enum bch_data_type type)
285 {
286         return type < BCH_DATA_NR
287                 ? __bch2_data_types[type]
288                 : "(invalid data type)";
289 }
290
291 /* disk reservations: */
292
293 static inline void bch2_disk_reservation_put(struct bch_fs *c,
294                                              struct disk_reservation *res)
295 {
296         if (res->sectors) {
297                 this_cpu_sub(*c->online_reserved, res->sectors);
298                 res->sectors = 0;
299         }
300 }
301
302 enum bch_reservation_flags {
303         BCH_DISK_RESERVATION_NOFAIL     = 1 << 0,
304         BCH_DISK_RESERVATION_PARTIAL    = 1 << 1,
305 };
306
307 int __bch2_disk_reservation_add(struct bch_fs *, struct disk_reservation *,
308                                 u64, enum bch_reservation_flags);
309
310 static inline int bch2_disk_reservation_add(struct bch_fs *c, struct disk_reservation *res,
311                                             u64 sectors, enum bch_reservation_flags flags)
312 {
313 #ifdef __KERNEL__
314         u64 old, new;
315
316         old = this_cpu_read(c->pcpu->sectors_available);
317         do {
318                 if (sectors > old)
319                         return __bch2_disk_reservation_add(c, res, sectors, flags);
320
321                 new = old - sectors;
322         } while (!this_cpu_try_cmpxchg(c->pcpu->sectors_available, &old, new));
323
324         this_cpu_add(*c->online_reserved, sectors);
325         res->sectors                    += sectors;
326         return 0;
327 #else
328         return __bch2_disk_reservation_add(c, res, sectors, flags);
329 #endif
330 }
331
332 static inline struct disk_reservation
333 bch2_disk_reservation_init(struct bch_fs *c, unsigned nr_replicas)
334 {
335         return (struct disk_reservation) {
336                 .sectors        = 0,
337 #if 0
338                 /* not used yet: */
339                 .gen            = c->capacity_gen,
340 #endif
341                 .nr_replicas    = nr_replicas,
342         };
343 }
344
345 static inline int bch2_disk_reservation_get(struct bch_fs *c,
346                                             struct disk_reservation *res,
347                                             u64 sectors, unsigned nr_replicas,
348                                             int flags)
349 {
350         *res = bch2_disk_reservation_init(c, nr_replicas);
351
352         return bch2_disk_reservation_add(c, res, sectors * nr_replicas, flags);
353 }
354
355 #define RESERVE_FACTOR  6
356
357 static inline u64 avail_factor(u64 r)
358 {
359         return div_u64(r << RESERVE_FACTOR, (1 << RESERVE_FACTOR) + 1);
360 }
361
362 void bch2_buckets_nouse_free(struct bch_fs *);
363 int bch2_buckets_nouse_alloc(struct bch_fs *);
364
365 int bch2_dev_buckets_resize(struct bch_fs *, struct bch_dev *, u64);
366 void bch2_dev_buckets_free(struct bch_dev *);
367 int bch2_dev_buckets_alloc(struct bch_fs *, struct bch_dev *);
368
369 #endif /* _BUCKETS_H */