]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/ec.h
New upstream release
[bcachefs-tools-debian] / libbcachefs / ec.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_EC_H
3 #define _BCACHEFS_EC_H
4
5 #include "ec_types.h"
6 #include "buckets_types.h"
7
8 int bch2_stripe_invalid(const struct bch_fs *, struct bkey_s_c,
9                         int rw, struct printbuf *);
10 void bch2_stripe_to_text(struct printbuf *, struct bch_fs *,
11                          struct bkey_s_c);
12
13 #define bch2_bkey_ops_stripe ((struct bkey_ops) {       \
14         .key_invalid    = bch2_stripe_invalid,          \
15         .val_to_text    = bch2_stripe_to_text,          \
16         .swab           = bch2_ptr_swab,                \
17         .trans_trigger  = bch2_trans_mark_stripe,       \
18         .atomic_trigger = bch2_mark_stripe,             \
19 })
20
21 static inline unsigned stripe_csums_per_device(const struct bch_stripe *s)
22 {
23         return DIV_ROUND_UP(le16_to_cpu(s->sectors),
24                             1 << s->csum_granularity_bits);
25 }
26
27 static inline unsigned stripe_csum_offset(const struct bch_stripe *s,
28                                           unsigned dev, unsigned csum_idx)
29 {
30         unsigned csum_bytes = bch_crc_bytes[s->csum_type];
31
32         return sizeof(struct bch_stripe) +
33                 sizeof(struct bch_extent_ptr) * s->nr_blocks +
34                 (dev * stripe_csums_per_device(s) + csum_idx) * csum_bytes;
35 }
36
37 static inline unsigned stripe_blockcount_offset(const struct bch_stripe *s,
38                                                 unsigned idx)
39 {
40         return stripe_csum_offset(s, s->nr_blocks, 0) +
41                 sizeof(u16) * idx;
42 }
43
44 static inline unsigned stripe_blockcount_get(const struct bch_stripe *s,
45                                              unsigned idx)
46 {
47         return le16_to_cpup((void *) s + stripe_blockcount_offset(s, idx));
48 }
49
50 static inline void stripe_blockcount_set(struct bch_stripe *s,
51                                          unsigned idx, unsigned v)
52 {
53         __le16 *p = (void *) s + stripe_blockcount_offset(s, idx);
54
55         *p = cpu_to_le16(v);
56 }
57
58 static inline unsigned stripe_val_u64s(const struct bch_stripe *s)
59 {
60         return DIV_ROUND_UP(stripe_blockcount_offset(s, s->nr_blocks),
61                             sizeof(u64));
62 }
63
64 static inline void *stripe_csum(struct bch_stripe *s,
65                                 unsigned block, unsigned csum_idx)
66 {
67         EBUG_ON(block >= s->nr_blocks);
68         EBUG_ON(csum_idx >= stripe_csums_per_device(s));
69
70         return (void *) s + stripe_csum_offset(s, block, csum_idx);
71 }
72
73 static inline struct bch_csum stripe_csum_get(struct bch_stripe *s,
74                                    unsigned block, unsigned csum_idx)
75 {
76         struct bch_csum csum = { 0 };
77
78         memcpy(&csum, stripe_csum(s, block, csum_idx), bch_crc_bytes[s->csum_type]);
79         return csum;
80 }
81
82 static inline void stripe_csum_set(struct bch_stripe *s,
83                                    unsigned block, unsigned csum_idx,
84                                    struct bch_csum csum)
85 {
86         memcpy(stripe_csum(s, block, csum_idx), &csum, bch_crc_bytes[s->csum_type]);
87 }
88
89 static inline bool __bch2_ptr_matches_stripe(const struct bch_extent_ptr *stripe_ptr,
90                                              const struct bch_extent_ptr *data_ptr,
91                                              unsigned sectors)
92 {
93         return  data_ptr->dev    == stripe_ptr->dev &&
94                 data_ptr->gen    == stripe_ptr->gen &&
95                 data_ptr->offset >= stripe_ptr->offset &&
96                 data_ptr->offset  < stripe_ptr->offset + sectors;
97 }
98
99 static inline bool bch2_ptr_matches_stripe(const struct bch_stripe *s,
100                                            struct extent_ptr_decoded p)
101 {
102         unsigned nr_data = s->nr_blocks - s->nr_redundant;
103
104         BUG_ON(!p.has_ec);
105
106         if (p.ec.block >= nr_data)
107                 return false;
108
109         return __bch2_ptr_matches_stripe(&s->ptrs[p.ec.block], &p.ptr,
110                                          le16_to_cpu(s->sectors));
111 }
112
113 static inline bool bch2_ptr_matches_stripe_m(const struct gc_stripe *m,
114                                              struct extent_ptr_decoded p)
115 {
116         unsigned nr_data = m->nr_blocks - m->nr_redundant;
117
118         BUG_ON(!p.has_ec);
119
120         if (p.ec.block >= nr_data)
121                 return false;
122
123         return __bch2_ptr_matches_stripe(&m->ptrs[p.ec.block], &p.ptr,
124                                          m->sectors);
125 }
126
127 struct bch_read_bio;
128
129 struct ec_stripe_buf {
130         /* might not be buffering the entire stripe: */
131         unsigned                offset;
132         unsigned                size;
133         unsigned long           valid[BITS_TO_LONGS(BCH_BKEY_PTRS_MAX)];
134
135         void                    *data[BCH_BKEY_PTRS_MAX];
136
137         union {
138                 struct bkey_i_stripe    key;
139                 u64                     pad[255];
140         };
141 };
142
143 struct ec_stripe_head;
144
145 struct ec_stripe_new {
146         struct bch_fs           *c;
147         struct ec_stripe_head   *h;
148         struct mutex            lock;
149         struct list_head        list;
150         struct closure          iodone;
151
152         /* counts in flight writes, stripe is created when pin == 0 */
153         atomic_t                pin;
154
155         int                     err;
156
157         u8                      nr_data;
158         u8                      nr_parity;
159         bool                    allocated;
160         bool                    pending;
161         bool                    have_existing_stripe;
162
163         unsigned long           blocks_gotten[BITS_TO_LONGS(BCH_BKEY_PTRS_MAX)];
164         unsigned long           blocks_allocated[BITS_TO_LONGS(BCH_BKEY_PTRS_MAX)];
165         open_bucket_idx_t       blocks[BCH_BKEY_PTRS_MAX];
166         struct disk_reservation res;
167
168         struct ec_stripe_buf    new_stripe;
169         struct ec_stripe_buf    existing_stripe;
170 };
171
172 struct ec_stripe_head {
173         struct list_head        list;
174         struct mutex            lock;
175
176         unsigned                target;
177         unsigned                algo;
178         unsigned                redundancy;
179         bool                    copygc;
180
181         struct bch_devs_mask    devs;
182         unsigned                nr_active_devs;
183
184         unsigned                blocksize;
185
186         struct dev_stripe_state block_stripe;
187         struct dev_stripe_state parity_stripe;
188
189         struct ec_stripe_new    *s;
190 };
191
192 int bch2_ec_read_extent(struct bch_fs *, struct bch_read_bio *);
193
194 void *bch2_writepoint_ec_buf(struct bch_fs *, struct write_point *);
195
196 void bch2_ec_bucket_written(struct bch_fs *, struct open_bucket *);
197 void bch2_ec_bucket_cancel(struct bch_fs *, struct open_bucket *);
198
199 int bch2_ec_stripe_new_alloc(struct bch_fs *, struct ec_stripe_head *);
200
201 void bch2_ec_stripe_head_put(struct bch_fs *, struct ec_stripe_head *);
202 struct ec_stripe_head *bch2_ec_stripe_head_get(struct bch_fs *,
203                         unsigned, unsigned, unsigned, bool, struct closure *);
204
205 void bch2_stripes_heap_update(struct bch_fs *, struct stripe *, size_t);
206 void bch2_stripes_heap_del(struct bch_fs *, struct stripe *, size_t);
207 void bch2_stripes_heap_insert(struct bch_fs *, struct stripe *, size_t);
208
209 void bch2_ec_stop_dev(struct bch_fs *, struct bch_dev *);
210
211 void bch2_ec_flush_new_stripes(struct bch_fs *);
212
213 void bch2_stripes_heap_start(struct bch_fs *);
214
215 int bch2_stripes_read(struct bch_fs *);
216
217 void bch2_stripes_heap_to_text(struct printbuf *, struct bch_fs *);
218 void bch2_new_stripes_to_text(struct printbuf *, struct bch_fs *);
219
220 void bch2_fs_ec_exit(struct bch_fs *);
221 void bch2_fs_ec_init_early(struct bch_fs *);
222 int bch2_fs_ec_init(struct bch_fs *);
223
224 #endif /* _BCACHEFS_EC_H */