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