bcachefs: Erasure coding fixes & refactoring
[linux-block.git] / fs / bcachefs / alloc_background.h
CommitLineData
7b3f84ea
KO
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
8fe826f9
KO
9struct bkey_alloc_unpacked {
10 u8 gen;
11#define x(_name, _bits) u##_bits _name;
12 BCH_ALLOC_FIELDS()
13#undef x
14};
15
39283c71
KO
16/* How out of date a pointer gen is allowed to be: */
17#define BUCKET_GC_GEN_MAX 96U
18
6671a708
KO
19/* returns true if not equal */
20static inline bool bkey_alloc_unpacked_cmp(struct bkey_alloc_unpacked l,
21 struct bkey_alloc_unpacked r)
22{
23 return l.gen != r.gen
24#define x(_name, _bits) || l._name != r._name
25 BCH_ALLOC_FIELDS()
26#undef x
27 ;
28}
29
460651ee 30struct bkey_alloc_unpacked bch2_alloc_unpack(struct bkey_s_c);
932aa837
KO
31void bch2_alloc_pack(struct bkey_i_alloc *,
32 const struct bkey_alloc_unpacked);
8fe826f9 33
f30dd860
KO
34int bch2_bucket_io_time_reset(struct btree_trans *, unsigned, size_t, int);
35
67163cde
KO
36static inline struct bkey_alloc_unpacked
37alloc_mem_to_key(struct bucket *g, struct bucket_mark m)
38{
39 return (struct bkey_alloc_unpacked) {
40 .gen = m.gen,
41 .oldest_gen = g->oldest_gen,
42 .data_type = m.data_type,
43 .dirty_sectors = m.dirty_sectors,
44 .cached_sectors = m.cached_sectors,
45 .read_time = g->io_time[READ],
46 .write_time = g->io_time[WRITE],
47 };
48}
49
8b335bae 50#define ALLOC_SCAN_BATCH(ca) max_t(size_t, 1, (ca)->mi.nbuckets >> 9)
7b3f84ea
KO
51
52const char *bch2_alloc_invalid(const struct bch_fs *, struct bkey_s_c);
319f9ac3 53void bch2_alloc_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
7b3f84ea 54
26609b61 55#define bch2_bkey_ops_alloc (struct bkey_ops) { \
7b3f84ea
KO
56 .key_invalid = bch2_alloc_invalid, \
57 .val_to_text = bch2_alloc_to_text, \
58}
59
d0734356
KO
60struct journal_keys;
61int bch2_alloc_read(struct bch_fs *, struct journal_keys *);
7b3f84ea
KO
62
63static inline void bch2_wake_allocator(struct bch_dev *ca)
64{
65 struct task_struct *p;
66
67 rcu_read_lock();
68 p = rcu_dereference(ca->alloc_thread);
9f20ed15 69 if (p) {
7b3f84ea 70 wake_up_process(p);
9f20ed15
KO
71 ca->allocator_state = ALLOCATOR_RUNNING;
72 }
7b3f84ea
KO
73 rcu_read_unlock();
74}
75
76static inline void verify_not_on_freelist(struct bch_fs *c, struct bch_dev *ca,
77 size_t bucket)
78{
29364f34 79 if (bch2_expensive_debug_checks) {
7b3f84ea
KO
80 size_t iter;
81 long i;
82 unsigned j;
83
84 for (j = 0; j < RESERVE_NR; j++)
85 fifo_for_each_entry(i, &ca->free[j], iter)
86 BUG_ON(i == bucket);
87 fifo_for_each_entry(i, &ca->free_inc, iter)
88 BUG_ON(i == bucket);
89 }
90}
91
92void bch2_recalc_capacity(struct bch_fs *);
93
94void bch2_dev_allocator_remove(struct bch_fs *, struct bch_dev *);
95void bch2_dev_allocator_add(struct bch_fs *, struct bch_dev *);
96
430735cd 97void bch2_dev_allocator_quiesce(struct bch_fs *, struct bch_dev *);
7b3f84ea
KO
98void bch2_dev_allocator_stop(struct bch_dev *);
99int bch2_dev_allocator_start(struct bch_dev *);
100
8d6b6222
KO
101int bch2_dev_alloc_write(struct bch_fs *, struct bch_dev *, unsigned);
102int bch2_alloc_write(struct bch_fs *, unsigned);
b092dadd 103void bch2_fs_allocator_background_init(struct bch_fs *);
7b3f84ea
KO
104
105#endif /* _BCACHEFS_ALLOC_BACKGROUND_H */