1 /* SPDX-License-Identifier: GPL-2.0 */
6 #include "buckets_types.h"
7 #include "keylist_types.h"
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 *,
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, \
19 static inline unsigned stripe_csums_per_device(const struct bch_stripe *s)
21 return DIV_ROUND_UP(le16_to_cpu(s->sectors),
22 1 << s->csum_granularity_bits);
25 static inline unsigned stripe_csum_offset(const struct bch_stripe *s,
26 unsigned dev, unsigned csum_idx)
28 unsigned csum_bytes = bch_crc_bytes[s->csum_type];
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;
35 static inline unsigned stripe_blockcount_offset(const struct bch_stripe *s,
38 return stripe_csum_offset(s, s->nr_blocks, 0) +
42 static inline unsigned stripe_blockcount_get(const struct bch_stripe *s,
45 return le16_to_cpup((void *) s + stripe_blockcount_offset(s, idx));
48 static inline void stripe_blockcount_set(struct bch_stripe *s,
49 unsigned idx, unsigned v)
51 __le16 *p = (void *) s + stripe_blockcount_offset(s, idx);
56 static inline unsigned stripe_val_u64s(const struct bch_stripe *s)
58 return DIV_ROUND_UP(stripe_blockcount_offset(s, s->nr_blocks),
62 static inline void *stripe_csum(struct bch_stripe *s,
63 unsigned block, unsigned csum_idx)
65 EBUG_ON(block >= s->nr_blocks);
66 EBUG_ON(csum_idx >= stripe_csums_per_device(s));
68 return (void *) s + stripe_csum_offset(s, block, csum_idx);
71 static inline struct bch_csum stripe_csum_get(struct bch_stripe *s,
72 unsigned block, unsigned csum_idx)
74 struct bch_csum csum = { 0 };
76 memcpy(&csum, stripe_csum(s, block, csum_idx), bch_crc_bytes[s->csum_type]);
80 static inline void stripe_csum_set(struct bch_stripe *s,
81 unsigned block, unsigned csum_idx,
84 memcpy(stripe_csum(s, block, csum_idx), &csum, bch_crc_bytes[s->csum_type]);
87 static inline bool __bch2_ptr_matches_stripe(const struct bch_stripe *s,
88 const struct bch_extent_ptr *ptr,
91 unsigned nr_data = s->nr_blocks - s->nr_redundant;
96 return ptr->dev == s->ptrs[block].dev &&
97 ptr->gen == s->ptrs[block].gen &&
98 ptr->offset >= s->ptrs[block].offset &&
99 ptr->offset < s->ptrs[block].offset + le16_to_cpu(s->sectors);
102 static inline bool bch2_ptr_matches_stripe(const struct bch_stripe *s,
103 struct extent_ptr_decoded p)
107 return __bch2_ptr_matches_stripe(s, &p.ptr, p.ec.block);
112 struct ec_stripe_buf {
113 /* might not be buffering the entire stripe: */
116 unsigned long valid[BITS_TO_LONGS(BCH_BKEY_PTRS_MAX)];
118 void *data[BCH_BKEY_PTRS_MAX];
121 struct bkey_i_stripe key;
126 struct ec_stripe_head;
128 struct ec_stripe_new {
130 struct ec_stripe_head *h;
132 struct list_head list;
133 struct closure iodone;
135 /* counts in flight writes, stripe is created when pin == 0 */
144 bool have_existing_stripe;
146 unsigned long blocks_gotten[BITS_TO_LONGS(BCH_BKEY_PTRS_MAX)];
147 unsigned long blocks_allocated[BITS_TO_LONGS(BCH_BKEY_PTRS_MAX)];
148 open_bucket_idx_t blocks[BCH_BKEY_PTRS_MAX];
149 struct disk_reservation res;
152 u64 inline_keys[BKEY_U64s * 8];
154 struct ec_stripe_buf new_stripe;
155 struct ec_stripe_buf existing_stripe;
158 struct ec_stripe_head {
159 struct list_head list;
167 struct bch_devs_mask devs;
168 unsigned nr_active_devs;
172 struct dev_stripe_state block_stripe;
173 struct dev_stripe_state parity_stripe;
175 struct ec_stripe_new *s;
178 int bch2_ec_read_extent(struct bch_fs *, struct bch_read_bio *);
180 void *bch2_writepoint_ec_buf(struct bch_fs *, struct write_point *);
181 void bch2_ec_add_backpointer(struct bch_fs *, struct write_point *,
182 struct bpos, unsigned);
184 void bch2_ec_bucket_written(struct bch_fs *, struct open_bucket *);
185 void bch2_ec_bucket_cancel(struct bch_fs *, struct open_bucket *);
187 int bch2_ec_stripe_new_alloc(struct bch_fs *, struct ec_stripe_head *);
189 void bch2_ec_stripe_head_put(struct bch_fs *, struct ec_stripe_head *);
190 struct ec_stripe_head *bch2_ec_stripe_head_get(struct bch_fs *,
191 unsigned, unsigned, unsigned, bool, struct closure *);
193 void bch2_stripes_heap_update(struct bch_fs *, struct stripe *, size_t);
194 void bch2_stripes_heap_del(struct bch_fs *, struct stripe *, size_t);
195 void bch2_stripes_heap_insert(struct bch_fs *, struct stripe *, size_t);
197 void bch2_ec_stop_dev(struct bch_fs *, struct bch_dev *);
199 void bch2_ec_flush_new_stripes(struct bch_fs *);
201 void bch2_stripes_heap_start(struct bch_fs *);
204 int bch2_stripes_read(struct bch_fs *, struct journal_keys *);
205 int bch2_stripes_write(struct bch_fs *, unsigned);
207 int bch2_ec_mem_alloc(struct bch_fs *, bool);
209 void bch2_stripes_heap_to_text(struct printbuf *, struct bch_fs *);
210 void bch2_new_stripes_to_text(struct printbuf *, struct bch_fs *);
212 void bch2_fs_ec_exit(struct bch_fs *);
213 int bch2_fs_ec_init(struct bch_fs *);
215 #endif /* _BCACHEFS_EC_H */