]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/ec.h
Update bcachefs sources to 99750eab4d bcachefs: Persist stripe blocks_used
[bcachefs-tools-debian] / libbcachefs / ec.h
1 #ifndef _BCACHEFS_EC_H
2 #define _BCACHEFS_EC_H
3
4 #include "ec_types.h"
5 #include "keylist_types.h"
6
7 const char *bch2_stripe_invalid(const struct bch_fs *, struct bkey_s_c);
8 void bch2_stripe_to_text(struct printbuf *, struct bch_fs *,
9                          struct bkey_s_c);
10
11 #define bch2_bkey_ops_stripe (struct bkey_ops) {        \
12         .key_invalid    = bch2_stripe_invalid,          \
13         .val_to_text    = bch2_stripe_to_text,          \
14 }
15
16 static inline unsigned stripe_csums_per_device(const struct bch_stripe *s)
17 {
18         return DIV_ROUND_UP(le16_to_cpu(s->sectors),
19                             1 << s->csum_granularity_bits);
20 }
21
22 static inline unsigned stripe_csum_offset(const struct bch_stripe *s,
23                                           unsigned dev, unsigned csum_idx)
24 {
25         unsigned csum_bytes = bch_crc_bytes[s->csum_type];
26
27         return sizeof(struct bch_stripe) +
28                 sizeof(struct bch_extent_ptr) * s->nr_blocks +
29                 (dev * stripe_csums_per_device(s) + csum_idx) * csum_bytes;
30 }
31
32 static inline unsigned stripe_blockcount_offset(const struct bch_stripe *s,
33                                                 unsigned idx)
34 {
35         return stripe_csum_offset(s, s->nr_blocks, 0) +
36                 sizeof(u16) * idx;
37 }
38
39 static inline unsigned stripe_blockcount_get(const struct bch_stripe *s,
40                                              unsigned idx)
41 {
42         return le16_to_cpup((void *) s + stripe_blockcount_offset(s, idx));
43 }
44
45 static inline void stripe_blockcount_set(struct bch_stripe *s,
46                                          unsigned idx, unsigned v)
47 {
48         __le16 *p = (void *) s + stripe_blockcount_offset(s, idx);
49
50         *p = cpu_to_le16(v);
51 }
52
53 static inline unsigned stripe_val_u64s(const struct bch_stripe *s)
54 {
55         return DIV_ROUND_UP(stripe_blockcount_offset(s, s->nr_blocks),
56                             sizeof(u64));
57 }
58
59 static inline void *stripe_csum(struct bch_stripe *s,
60                                 unsigned dev, unsigned csum_idx)
61 {
62         return (void *) s + stripe_csum_offset(s, dev, csum_idx);
63 }
64
65 struct bch_read_bio;
66
67 struct ec_stripe_buf {
68         /* might not be buffering the entire stripe: */
69         unsigned                offset;
70         unsigned                size;
71         unsigned long           valid[BITS_TO_LONGS(EC_STRIPE_MAX)];
72
73         void                    *data[EC_STRIPE_MAX];
74
75         union {
76                 struct bkey_i_stripe    key;
77                 u64                     pad[255];
78         };
79 };
80
81 struct ec_stripe_head;
82
83 struct ec_stripe_new {
84         struct bch_fs           *c;
85         struct ec_stripe_head   *h;
86         struct mutex            lock;
87         struct list_head        list;
88
89         /* counts in flight writes, stripe is created when pin == 0 */
90         atomic_t                pin;
91
92         int                     err;
93
94         unsigned long           blocks_allocated[BITS_TO_LONGS(EC_STRIPE_MAX)];
95
96         struct open_buckets     blocks;
97         struct open_buckets     parity;
98
99         struct keylist          keys;
100         u64                     inline_keys[BKEY_U64s * 8];
101
102         struct ec_stripe_buf    stripe;
103 };
104
105 struct ec_stripe_head {
106         struct list_head        list;
107         struct mutex            lock;
108
109         struct list_head        stripes;
110
111         unsigned                target;
112         unsigned                algo;
113         unsigned                redundancy;
114
115         struct bch_devs_mask    devs;
116         unsigned                nr_active_devs;
117
118         unsigned                blocksize;
119
120         struct dev_stripe_state block_stripe;
121         struct dev_stripe_state parity_stripe;
122
123         struct open_buckets     blocks;
124         struct open_buckets     parity;
125
126         struct ec_stripe_new    *s;
127 };
128
129 int bch2_ec_read_extent(struct bch_fs *, struct bch_read_bio *);
130
131 void *bch2_writepoint_ec_buf(struct bch_fs *, struct write_point *);
132 void bch2_ec_add_backpointer(struct bch_fs *, struct write_point *,
133                              struct bpos, unsigned);
134
135 void bch2_ec_bucket_written(struct bch_fs *, struct open_bucket *);
136 void bch2_ec_bucket_cancel(struct bch_fs *, struct open_bucket *);
137
138 int bch2_ec_stripe_new_alloc(struct bch_fs *, struct ec_stripe_head *);
139
140 void bch2_ec_stripe_head_put(struct ec_stripe_head *);
141 struct ec_stripe_head *bch2_ec_stripe_head_get(struct bch_fs *, unsigned,
142                                                unsigned, unsigned);
143
144 void bch2_stripes_heap_update(struct bch_fs *, struct stripe *, size_t);
145 void bch2_stripes_heap_del(struct bch_fs *, struct stripe *, size_t);
146 void bch2_stripes_heap_insert(struct bch_fs *, struct stripe *, size_t);
147
148 void bch2_ec_stop_dev(struct bch_fs *, struct bch_dev *);
149
150 void bch2_ec_flush_new_stripes(struct bch_fs *);
151
152 int bch2_stripes_read(struct bch_fs *, struct list_head *);
153 int bch2_stripes_write(struct bch_fs *, bool *);
154
155 int bch2_ec_mem_alloc(struct bch_fs *, bool);
156
157 int bch2_fs_ec_start(struct bch_fs *);
158
159 void bch2_fs_ec_exit(struct bch_fs *);
160 int bch2_fs_ec_init(struct bch_fs *);
161
162 #endif /* _BCACHEFS_EC_H */