]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/alloc_background.h
0c1a0f0dd2ab558835474ec18a65b9d21a596a00
[bcachefs-tools-debian] / libbcachefs / alloc_background.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_ALLOC_BACKGROUND_H
3 #define _BCACHEFS_ALLOC_BACKGROUND_H
4
5 #include "bcachefs.h"
6 #include "alloc_types.h"
7 #include "debug.h"
8
9 struct bkey_alloc_unpacked {
10         u8              gen;
11 #define x(_name, _bits) u##_bits _name;
12         BCH_ALLOC_FIELDS()
13 #undef  x
14 };
15
16 struct bkey_alloc_unpacked bch2_alloc_unpack(struct bkey_s_c);
17 void bch2_alloc_pack(struct bkey_i_alloc *,
18                      const struct bkey_alloc_unpacked);
19
20 #define ALLOC_SCAN_BATCH(ca)            max_t(size_t, 1, (ca)->mi.nbuckets >> 9)
21
22 const char *bch2_alloc_invalid(const struct bch_fs *, struct bkey_s_c);
23 void bch2_alloc_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
24
25 #define bch2_bkey_ops_alloc (struct bkey_ops) {         \
26         .key_invalid    = bch2_alloc_invalid,           \
27         .val_to_text    = bch2_alloc_to_text,           \
28 }
29
30 struct journal_keys;
31 int bch2_alloc_read(struct bch_fs *, struct journal_keys *);
32 int bch2_alloc_replay_key(struct bch_fs *, struct bkey_i *);
33
34 static inline void bch2_wake_allocator(struct bch_dev *ca)
35 {
36         struct task_struct *p;
37
38         rcu_read_lock();
39         p = rcu_dereference(ca->alloc_thread);
40         if (p)
41                 wake_up_process(p);
42         rcu_read_unlock();
43 }
44
45 static inline void verify_not_on_freelist(struct bch_fs *c, struct bch_dev *ca,
46                                           size_t bucket)
47 {
48         if (expensive_debug_checks(c) &&
49             test_bit(BCH_FS_ALLOCATOR_STARTED, &c->flags)) {
50                 size_t iter;
51                 long i;
52                 unsigned j;
53
54                 for (j = 0; j < RESERVE_NR; j++)
55                         fifo_for_each_entry(i, &ca->free[j], iter)
56                                 BUG_ON(i == bucket);
57                 fifo_for_each_entry(i, &ca->free_inc, iter)
58                         BUG_ON(i == bucket);
59         }
60 }
61
62 void bch2_recalc_capacity(struct bch_fs *);
63
64 void bch2_dev_allocator_remove(struct bch_fs *, struct bch_dev *);
65 void bch2_dev_allocator_add(struct bch_fs *, struct bch_dev *);
66
67 void bch2_dev_allocator_quiesce(struct bch_fs *, struct bch_dev *);
68 void bch2_dev_allocator_stop(struct bch_dev *);
69 int bch2_dev_allocator_start(struct bch_dev *);
70
71 int bch2_alloc_write(struct bch_fs *, unsigned, bool *);
72 int bch2_fs_allocator_start(struct bch_fs *);
73 void bch2_fs_allocator_background_init(struct bch_fs *);
74
75 #endif /* _BCACHEFS_ALLOC_BACKGROUND_H */