1 /* SPDX-License-Identifier: GPL-2.0 */
6 #include "buckets_types.h"
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 *,
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, \
21 static inline unsigned stripe_csums_per_device(const struct bch_stripe *s)
23 return DIV_ROUND_UP(le16_to_cpu(s->sectors),
24 1 << s->csum_granularity_bits);
27 static inline unsigned stripe_csum_offset(const struct bch_stripe *s,
28 unsigned dev, unsigned csum_idx)
30 unsigned csum_bytes = bch_crc_bytes[s->csum_type];
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;
37 static inline unsigned stripe_blockcount_offset(const struct bch_stripe *s,
40 return stripe_csum_offset(s, s->nr_blocks, 0) +
44 static inline unsigned stripe_blockcount_get(const struct bch_stripe *s,
47 return le16_to_cpup((void *) s + stripe_blockcount_offset(s, idx));
50 static inline void stripe_blockcount_set(struct bch_stripe *s,
51 unsigned idx, unsigned v)
53 __le16 *p = (void *) s + stripe_blockcount_offset(s, idx);
58 static inline unsigned stripe_val_u64s(const struct bch_stripe *s)
60 return DIV_ROUND_UP(stripe_blockcount_offset(s, s->nr_blocks),
64 static inline void *stripe_csum(struct bch_stripe *s,
65 unsigned block, unsigned csum_idx)
67 EBUG_ON(block >= s->nr_blocks);
68 EBUG_ON(csum_idx >= stripe_csums_per_device(s));
70 return (void *) s + stripe_csum_offset(s, block, csum_idx);
73 static inline struct bch_csum stripe_csum_get(struct bch_stripe *s,
74 unsigned block, unsigned csum_idx)
76 struct bch_csum csum = { 0 };
78 memcpy(&csum, stripe_csum(s, block, csum_idx), bch_crc_bytes[s->csum_type]);
82 static inline void stripe_csum_set(struct bch_stripe *s,
83 unsigned block, unsigned csum_idx,
86 memcpy(stripe_csum(s, block, csum_idx), &csum, bch_crc_bytes[s->csum_type]);
89 static inline bool __bch2_ptr_matches_stripe(const struct bch_extent_ptr *stripe_ptr,
90 const struct bch_extent_ptr *data_ptr,
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;
99 static inline bool bch2_ptr_matches_stripe(const struct bch_stripe *s,
100 struct extent_ptr_decoded p)
102 unsigned nr_data = s->nr_blocks - s->nr_redundant;
106 if (p.ec.block >= nr_data)
109 return __bch2_ptr_matches_stripe(&s->ptrs[p.ec.block], &p.ptr,
110 le16_to_cpu(s->sectors));
113 static inline bool bch2_ptr_matches_stripe_m(const struct gc_stripe *m,
114 struct extent_ptr_decoded p)
116 unsigned nr_data = m->nr_blocks - m->nr_redundant;
120 if (p.ec.block >= nr_data)
123 return __bch2_ptr_matches_stripe(&m->ptrs[p.ec.block], &p.ptr,
129 struct ec_stripe_buf {
130 /* might not be buffering the entire stripe: */
133 unsigned long valid[BITS_TO_LONGS(BCH_BKEY_PTRS_MAX)];
135 void *data[BCH_BKEY_PTRS_MAX];
138 struct bkey_i_stripe key;
143 struct ec_stripe_head;
145 struct ec_stripe_new {
147 struct ec_stripe_head *h;
149 struct list_head list;
150 struct closure iodone;
152 /* counts in flight writes, stripe is created when pin == 0 */
161 bool have_existing_stripe;
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;
168 struct ec_stripe_buf new_stripe;
169 struct ec_stripe_buf existing_stripe;
172 struct ec_stripe_head {
173 struct list_head list;
181 struct bch_devs_mask devs;
182 unsigned nr_active_devs;
186 struct dev_stripe_state block_stripe;
187 struct dev_stripe_state parity_stripe;
189 struct ec_stripe_new *s;
192 int bch2_ec_read_extent(struct bch_fs *, struct bch_read_bio *);
194 void *bch2_writepoint_ec_buf(struct bch_fs *, struct write_point *);
196 void bch2_ec_bucket_written(struct bch_fs *, struct open_bucket *);
197 void bch2_ec_bucket_cancel(struct bch_fs *, struct open_bucket *);
199 int bch2_ec_stripe_new_alloc(struct bch_fs *, struct ec_stripe_head *);
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 *);
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);
209 void bch2_ec_stop_dev(struct bch_fs *, struct bch_dev *);
211 void bch2_ec_flush_new_stripes(struct bch_fs *);
213 void bch2_stripes_heap_start(struct bch_fs *);
215 int bch2_stripes_read(struct bch_fs *);
217 void bch2_stripes_heap_to_text(struct printbuf *, struct bch_fs *);
218 void bch2_new_stripes_to_text(struct printbuf *, struct bch_fs *);
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 *);
224 #endif /* _BCACHEFS_EC_H */