]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/alloc_background.h
Update bcachefs sources to 4837f82ee1 bcachefs: Use cached iterators for alloc btree
[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
58 static inline void bch2_wake_allocator(struct bch_dev *ca)
59 {
60         struct task_struct *p;
61
62         rcu_read_lock();
63         p = rcu_dereference(ca->alloc_thread);
64         if (p)
65                 wake_up_process(p);
66         rcu_read_unlock();
67 }
68
69 static inline void verify_not_on_freelist(struct bch_fs *c, struct bch_dev *ca,
70                                           size_t bucket)
71 {
72         if (expensive_debug_checks(c)) {
73                 size_t iter;
74                 long i;
75                 unsigned j;
76
77                 for (j = 0; j < RESERVE_NR; j++)
78                         fifo_for_each_entry(i, &ca->free[j], iter)
79                                 BUG_ON(i == bucket);
80                 fifo_for_each_entry(i, &ca->free_inc, iter)
81                         BUG_ON(i == bucket);
82         }
83 }
84
85 void bch2_recalc_capacity(struct bch_fs *);
86
87 void bch2_dev_allocator_remove(struct bch_fs *, struct bch_dev *);
88 void bch2_dev_allocator_add(struct bch_fs *, struct bch_dev *);
89
90 void bch2_dev_allocator_quiesce(struct bch_fs *, struct bch_dev *);
91 void bch2_dev_allocator_stop(struct bch_dev *);
92 int bch2_dev_allocator_start(struct bch_dev *);
93
94 int bch2_alloc_write(struct bch_fs *, unsigned, bool *);
95 void bch2_fs_allocator_background_init(struct bch_fs *);
96
97 #endif /* _BCACHEFS_ALLOC_BACKGROUND_H */