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