]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/alloc_background.h
501c444353fbf0e4a3fe6e38eaf1216c066c2a3a
[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 /* returns true if not equal */
17 static inline bool bkey_alloc_unpacked_cmp(struct bkey_alloc_unpacked l,
18                                            struct bkey_alloc_unpacked r)
19 {
20         return l.gen != r.gen
21 #define x(_name, _bits) || l._name != r._name
22         BCH_ALLOC_FIELDS()
23 #undef  x
24         ;
25 }
26
27 struct bkey_alloc_unpacked bch2_alloc_unpack(struct bkey_s_c);
28 void bch2_alloc_pack(struct bkey_i_alloc *,
29                      const struct bkey_alloc_unpacked);
30
31 static inline struct bkey_alloc_unpacked
32 alloc_mem_to_key(struct bucket *g, struct bucket_mark m)
33 {
34         return (struct bkey_alloc_unpacked) {
35                 .gen            = m.gen,
36                 .oldest_gen     = g->oldest_gen,
37                 .data_type      = m.data_type,
38                 .dirty_sectors  = m.dirty_sectors,
39                 .cached_sectors = m.cached_sectors,
40                 .read_time      = g->io_time[READ],
41                 .write_time     = g->io_time[WRITE],
42         };
43 }
44
45 #define ALLOC_SCAN_BATCH(ca)            max_t(size_t, 1, (ca)->mi.nbuckets >> 9)
46
47 const char *bch2_alloc_invalid(const struct bch_fs *, struct bkey_s_c);
48 void bch2_alloc_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
49
50 #define bch2_bkey_ops_alloc (struct bkey_ops) {         \
51         .key_invalid    = bch2_alloc_invalid,           \
52         .val_to_text    = bch2_alloc_to_text,           \
53 }
54
55 struct journal_keys;
56 int bch2_alloc_read(struct bch_fs *, struct journal_keys *);
57 int bch2_alloc_replay_key(struct bch_fs *, struct bkey_i *);
58
59 static inline void bch2_wake_allocator(struct bch_dev *ca)
60 {
61         struct task_struct *p;
62
63         rcu_read_lock();
64         p = rcu_dereference(ca->alloc_thread);
65         if (p)
66                 wake_up_process(p);
67         rcu_read_unlock();
68 }
69
70 static inline void verify_not_on_freelist(struct bch_fs *c, struct bch_dev *ca,
71                                           size_t bucket)
72 {
73         if (expensive_debug_checks(c) &&
74             test_bit(BCH_FS_ALLOCATOR_STARTED, &c->flags)) {
75                 size_t iter;
76                 long i;
77                 unsigned j;
78
79                 for (j = 0; j < RESERVE_NR; j++)
80                         fifo_for_each_entry(i, &ca->free[j], iter)
81                                 BUG_ON(i == bucket);
82                 fifo_for_each_entry(i, &ca->free_inc, iter)
83                         BUG_ON(i == bucket);
84         }
85 }
86
87 void bch2_recalc_capacity(struct bch_fs *);
88
89 void bch2_dev_allocator_remove(struct bch_fs *, struct bch_dev *);
90 void bch2_dev_allocator_add(struct bch_fs *, struct bch_dev *);
91
92 void bch2_dev_allocator_quiesce(struct bch_fs *, struct bch_dev *);
93 void bch2_dev_allocator_stop(struct bch_dev *);
94 int bch2_dev_allocator_start(struct bch_dev *);
95
96 int bch2_alloc_write(struct bch_fs *, unsigned, bool *);
97 int bch2_fs_allocator_start(struct bch_fs *);
98 void bch2_fs_allocator_background_init(struct bch_fs *);
99
100 #endif /* _BCACHEFS_ALLOC_BACKGROUND_H */