]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/alloc_background.h
870714ff16a7f496972bc1fec2c2fa564cd4c9f4
[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 int bch2_bucket_io_time_reset(struct btree_trans *, unsigned, size_t, int);
32
33 static inline struct bkey_alloc_unpacked
34 alloc_mem_to_key(struct bucket *g, struct bucket_mark m)
35 {
36         return (struct bkey_alloc_unpacked) {
37                 .gen            = m.gen,
38                 .oldest_gen     = g->oldest_gen,
39                 .data_type      = m.data_type,
40                 .dirty_sectors  = m.dirty_sectors,
41                 .cached_sectors = m.cached_sectors,
42                 .read_time      = g->io_time[READ],
43                 .write_time     = g->io_time[WRITE],
44         };
45 }
46
47 #define ALLOC_SCAN_BATCH(ca)            max_t(size_t, 1, (ca)->mi.nbuckets >> 9)
48
49 const char *bch2_alloc_invalid(const struct bch_fs *, struct bkey_s_c);
50 void bch2_alloc_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
51
52 #define bch2_bkey_ops_alloc (struct bkey_ops) {         \
53         .key_invalid    = bch2_alloc_invalid,           \
54         .val_to_text    = bch2_alloc_to_text,           \
55 }
56
57 struct journal_keys;
58 int bch2_alloc_read(struct bch_fs *, struct journal_keys *);
59
60 static inline void bch2_wake_allocator(struct bch_dev *ca)
61 {
62         struct task_struct *p;
63
64         rcu_read_lock();
65         p = rcu_dereference(ca->alloc_thread);
66         if (p) {
67                 wake_up_process(p);
68                 ca->allocator_state = ALLOCATOR_RUNNING;
69         }
70         rcu_read_unlock();
71 }
72
73 static inline void verify_not_on_freelist(struct bch_fs *c, struct bch_dev *ca,
74                                           size_t bucket)
75 {
76         if (expensive_debug_checks(c)) {
77                 size_t iter;
78                 long i;
79                 unsigned j;
80
81                 for (j = 0; j < RESERVE_NR; j++)
82                         fifo_for_each_entry(i, &ca->free[j], iter)
83                                 BUG_ON(i == bucket);
84                 fifo_for_each_entry(i, &ca->free_inc, iter)
85                         BUG_ON(i == bucket);
86         }
87 }
88
89 void bch2_recalc_capacity(struct bch_fs *);
90
91 void bch2_dev_allocator_remove(struct bch_fs *, struct bch_dev *);
92 void bch2_dev_allocator_add(struct bch_fs *, struct bch_dev *);
93
94 void bch2_dev_allocator_quiesce(struct bch_fs *, struct bch_dev *);
95 void bch2_dev_allocator_stop(struct bch_dev *);
96 int bch2_dev_allocator_start(struct bch_dev *);
97
98 int bch2_dev_alloc_write(struct bch_fs *, struct bch_dev *, unsigned);
99 int bch2_alloc_write(struct bch_fs *, unsigned);
100 void bch2_fs_allocator_background_init(struct bch_fs *);
101
102 #endif /* _BCACHEFS_ALLOC_BACKGROUND_H */