]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/alloc_background.h
Update bcachefs sources to e48731a188 bcachefs: Fix BTREE_TRIGGER_WANTS_OLD_AND_NEW
[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 "buckets.h"
8 #include "debug.h"
9 #include "super.h"
10
11 extern const char * const bch2_allocator_states[];
12
13 struct bkey_alloc_unpacked {
14         u64             journal_seq;
15         u64             bucket;
16         u8              dev;
17         u8              gen;
18         u8              oldest_gen;
19         u8              data_type;
20 #define x(_name, _bits) u##_bits _name;
21         BCH_ALLOC_FIELDS_V2()
22 #undef  x
23 };
24
25 /* How out of date a pointer gen is allowed to be: */
26 #define BUCKET_GC_GEN_MAX       96U
27
28 /* returns true if not equal */
29 static inline bool bkey_alloc_unpacked_cmp(struct bkey_alloc_unpacked l,
30                                            struct bkey_alloc_unpacked r)
31 {
32         return  l.gen != r.gen                  ||
33                 l.oldest_gen != r.oldest_gen    ||
34                 l.data_type != r.data_type
35 #define x(_name, ...)   || l._name != r._name
36         BCH_ALLOC_FIELDS_V2()
37 #undef  x
38         ;
39 }
40
41 struct bkey_alloc_buf {
42         struct bkey_i   k;
43         struct bch_alloc_v3 v;
44
45 #define x(_name,  _bits)                + _bits / 8
46         u8              _pad[0 + BCH_ALLOC_FIELDS_V2()];
47 #undef  x
48 } __attribute__((packed, aligned(8)));
49
50 struct bkey_alloc_unpacked bch2_alloc_unpack(struct bkey_s_c);
51 struct bkey_alloc_buf *bch2_alloc_pack(struct btree_trans *,
52                                        const struct bkey_alloc_unpacked);
53 int bch2_alloc_write(struct btree_trans *, struct btree_iter *,
54                      struct bkey_alloc_unpacked *, unsigned);
55
56 int bch2_bucket_io_time_reset(struct btree_trans *, unsigned, size_t, int);
57
58 #define ALLOC_SCAN_BATCH(ca)            max_t(size_t, 1, (ca)->mi.nbuckets >> 9)
59
60 const char *bch2_alloc_v1_invalid(const struct bch_fs *, struct bkey_s_c);
61 const char *bch2_alloc_v2_invalid(const struct bch_fs *, struct bkey_s_c);
62 const char *bch2_alloc_v3_invalid(const struct bch_fs *, struct bkey_s_c);
63 void bch2_alloc_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
64
65 #define bch2_bkey_ops_alloc (struct bkey_ops) {         \
66         .key_invalid    = bch2_alloc_v1_invalid,        \
67         .val_to_text    = bch2_alloc_to_text,           \
68         .atomic_trigger = bch2_mark_alloc,              \
69 }
70
71 #define bch2_bkey_ops_alloc_v2 (struct bkey_ops) {      \
72         .key_invalid    = bch2_alloc_v2_invalid,        \
73         .val_to_text    = bch2_alloc_to_text,           \
74         .atomic_trigger = bch2_mark_alloc,              \
75 }
76
77 #define bch2_bkey_ops_alloc_v3 (struct bkey_ops) {      \
78         .key_invalid    = bch2_alloc_v3_invalid,        \
79         .val_to_text    = bch2_alloc_to_text,           \
80         .atomic_trigger = bch2_mark_alloc,              \
81 }
82
83 static inline bool bkey_is_alloc(const struct bkey *k)
84 {
85         return  k->type == KEY_TYPE_alloc ||
86                 k->type == KEY_TYPE_alloc_v2 ||
87                 k->type == KEY_TYPE_alloc_v3;
88 }
89
90 int bch2_alloc_read(struct bch_fs *, bool, bool);
91
92 static inline void bch2_wake_allocator(struct bch_dev *ca)
93 {
94         struct task_struct *p;
95
96         rcu_read_lock();
97         p = rcu_dereference(ca->alloc_thread);
98         if (p)
99                 wake_up_process(p);
100         rcu_read_unlock();
101 }
102
103 static inline void verify_not_on_freelist(struct bch_fs *c, struct bch_dev *ca,
104                                           size_t bucket)
105 {
106         if (bch2_expensive_debug_checks) {
107                 size_t iter;
108                 long i;
109                 unsigned j;
110
111                 for (j = 0; j < RESERVE_NR; j++)
112                         fifo_for_each_entry(i, &ca->free[j], iter)
113                                 BUG_ON(i == bucket);
114                 fifo_for_each_entry(i, &ca->free_inc, iter)
115                         BUG_ON(i == bucket);
116         }
117 }
118
119 void bch2_recalc_capacity(struct bch_fs *);
120
121 void bch2_dev_allocator_remove(struct bch_fs *, struct bch_dev *);
122 void bch2_dev_allocator_add(struct bch_fs *, struct bch_dev *);
123
124 void bch2_dev_allocator_quiesce(struct bch_fs *, struct bch_dev *);
125 void bch2_dev_allocator_stop(struct bch_dev *);
126 int bch2_dev_allocator_start(struct bch_dev *);
127
128 void bch2_fs_allocator_background_init(struct bch_fs *);
129
130 #endif /* _BCACHEFS_ALLOC_BACKGROUND_H */