1 /* SPDX-License-Identifier: GPL-2.0 */
6 #include "buckets_types.h"
7 #include "extents_types.h"
9 enum bkey_invalid_flags;
11 int bch2_stripe_invalid(struct bch_fs *, struct bkey_s_c,
12 enum bkey_invalid_flags, struct printbuf *);
13 void bch2_stripe_to_text(struct printbuf *, struct bch_fs *,
15 int bch2_trigger_stripe(struct btree_trans *, enum btree_id, unsigned,
16 struct bkey_s_c, struct bkey_s, unsigned);
18 #define bch2_bkey_ops_stripe ((struct bkey_ops) { \
19 .key_invalid = bch2_stripe_invalid, \
20 .val_to_text = bch2_stripe_to_text, \
21 .swab = bch2_ptr_swab, \
22 .trigger = bch2_trigger_stripe, \
26 static inline unsigned stripe_csums_per_device(const struct bch_stripe *s)
28 return DIV_ROUND_UP(le16_to_cpu(s->sectors),
29 1 << s->csum_granularity_bits);
32 static inline unsigned stripe_csum_offset(const struct bch_stripe *s,
33 unsigned dev, unsigned csum_idx)
35 unsigned csum_bytes = bch_crc_bytes[s->csum_type];
37 return sizeof(struct bch_stripe) +
38 sizeof(struct bch_extent_ptr) * s->nr_blocks +
39 (dev * stripe_csums_per_device(s) + csum_idx) * csum_bytes;
42 static inline unsigned stripe_blockcount_offset(const struct bch_stripe *s,
45 return stripe_csum_offset(s, s->nr_blocks, 0) +
49 static inline unsigned stripe_blockcount_get(const struct bch_stripe *s,
52 return le16_to_cpup((void *) s + stripe_blockcount_offset(s, idx));
55 static inline void stripe_blockcount_set(struct bch_stripe *s,
56 unsigned idx, unsigned v)
58 __le16 *p = (void *) s + stripe_blockcount_offset(s, idx);
63 static inline unsigned stripe_val_u64s(const struct bch_stripe *s)
65 return DIV_ROUND_UP(stripe_blockcount_offset(s, s->nr_blocks),
69 static inline void *stripe_csum(struct bch_stripe *s,
70 unsigned block, unsigned csum_idx)
72 EBUG_ON(block >= s->nr_blocks);
73 EBUG_ON(csum_idx >= stripe_csums_per_device(s));
75 return (void *) s + stripe_csum_offset(s, block, csum_idx);
78 static inline struct bch_csum stripe_csum_get(struct bch_stripe *s,
79 unsigned block, unsigned csum_idx)
81 struct bch_csum csum = { 0 };
83 memcpy(&csum, stripe_csum(s, block, csum_idx), bch_crc_bytes[s->csum_type]);
87 static inline void stripe_csum_set(struct bch_stripe *s,
88 unsigned block, unsigned csum_idx,
91 memcpy(stripe_csum(s, block, csum_idx), &csum, bch_crc_bytes[s->csum_type]);
94 static inline bool __bch2_ptr_matches_stripe(const struct bch_extent_ptr *stripe_ptr,
95 const struct bch_extent_ptr *data_ptr,
98 return data_ptr->dev == stripe_ptr->dev &&
99 data_ptr->gen == stripe_ptr->gen &&
100 data_ptr->offset >= stripe_ptr->offset &&
101 data_ptr->offset < stripe_ptr->offset + sectors;
104 static inline bool bch2_ptr_matches_stripe(const struct bch_stripe *s,
105 struct extent_ptr_decoded p)
107 unsigned nr_data = s->nr_blocks - s->nr_redundant;
111 if (p.ec.block >= nr_data)
114 return __bch2_ptr_matches_stripe(&s->ptrs[p.ec.block], &p.ptr,
115 le16_to_cpu(s->sectors));
118 static inline bool bch2_ptr_matches_stripe_m(const struct gc_stripe *m,
119 struct extent_ptr_decoded p)
121 unsigned nr_data = m->nr_blocks - m->nr_redundant;
125 if (p.ec.block >= nr_data)
128 return __bch2_ptr_matches_stripe(&m->ptrs[p.ec.block], &p.ptr,
134 struct ec_stripe_buf {
135 /* might not be buffering the entire stripe: */
138 unsigned long valid[BITS_TO_LONGS(BCH_BKEY_PTRS_MAX)];
140 void *data[BCH_BKEY_PTRS_MAX];
142 __BKEY_PADDED(key, 255);
145 struct ec_stripe_head;
153 struct ec_stripe_new {
155 struct ec_stripe_head *h;
157 struct list_head list;
159 struct hlist_node hash;
162 struct closure iodone;
164 atomic_t ref[STRIPE_REF_NR];
172 bool have_existing_stripe;
174 unsigned long blocks_gotten[BITS_TO_LONGS(BCH_BKEY_PTRS_MAX)];
175 unsigned long blocks_allocated[BITS_TO_LONGS(BCH_BKEY_PTRS_MAX)];
176 open_bucket_idx_t blocks[BCH_BKEY_PTRS_MAX];
177 struct disk_reservation res;
179 struct ec_stripe_buf new_stripe;
180 struct ec_stripe_buf existing_stripe;
183 struct ec_stripe_head {
184 struct list_head list;
190 enum bch_watermark watermark;
192 struct bch_devs_mask devs;
193 unsigned nr_active_devs;
197 struct dev_stripe_state block_stripe;
198 struct dev_stripe_state parity_stripe;
200 struct ec_stripe_new *s;
203 int bch2_ec_read_extent(struct btree_trans *, struct bch_read_bio *);
205 void *bch2_writepoint_ec_buf(struct bch_fs *, struct write_point *);
207 void bch2_ec_bucket_cancel(struct bch_fs *, struct open_bucket *);
209 int bch2_ec_stripe_new_alloc(struct bch_fs *, struct ec_stripe_head *);
211 void bch2_ec_stripe_head_put(struct bch_fs *, struct ec_stripe_head *);
212 struct ec_stripe_head *bch2_ec_stripe_head_get(struct btree_trans *,
213 unsigned, unsigned, unsigned,
214 enum bch_watermark, struct closure *);
216 void bch2_stripes_heap_update(struct bch_fs *, struct stripe *, size_t);
217 void bch2_stripes_heap_del(struct bch_fs *, struct stripe *, size_t);
218 void bch2_stripes_heap_insert(struct bch_fs *, struct stripe *, size_t);
220 void bch2_do_stripe_deletes(struct bch_fs *);
221 void bch2_ec_do_stripe_creates(struct bch_fs *);
222 void bch2_ec_stripe_new_free(struct bch_fs *, struct ec_stripe_new *);
224 static inline void ec_stripe_new_get(struct ec_stripe_new *s,
225 enum ec_stripe_ref ref)
227 atomic_inc(&s->ref[ref]);
230 static inline void ec_stripe_new_put(struct bch_fs *c, struct ec_stripe_new *s,
231 enum ec_stripe_ref ref)
233 BUG_ON(atomic_read(&s->ref[ref]) <= 0);
235 if (atomic_dec_and_test(&s->ref[ref]))
237 case STRIPE_REF_stripe:
238 bch2_ec_stripe_new_free(c, s);
241 bch2_ec_do_stripe_creates(c);
248 void bch2_ec_stop_dev(struct bch_fs *, struct bch_dev *);
249 void bch2_fs_ec_stop(struct bch_fs *);
250 void bch2_fs_ec_flush(struct bch_fs *);
252 int bch2_stripes_read(struct bch_fs *);
254 void bch2_stripes_heap_to_text(struct printbuf *, struct bch_fs *);
255 void bch2_new_stripes_to_text(struct printbuf *, struct bch_fs *);
257 void bch2_fs_ec_exit(struct bch_fs *);
258 void bch2_fs_ec_init_early(struct bch_fs *);
259 int bch2_fs_ec_init(struct bch_fs *);
261 #endif /* _BCACHEFS_EC_H */