xref: /linux/fs/bcachefs/ec.h (revision 2622f290417001b0440f4a48dc6978f5f1e12a56)
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 "extents_types.h"
8 
9 int bch2_stripe_validate(struct bch_fs *, struct bkey_s_c,
10 			 struct bkey_validate_context);
11 void bch2_stripe_to_text(struct printbuf *, struct bch_fs *,
12 			 struct bkey_s_c);
13 int bch2_trigger_stripe(struct btree_trans *, enum btree_id, unsigned,
14 			struct bkey_s_c, struct bkey_s,
15 			enum btree_iter_update_trigger_flags);
16 
17 #define bch2_bkey_ops_stripe ((struct bkey_ops) {	\
18 	.key_validate	= bch2_stripe_validate,		\
19 	.val_to_text	= bch2_stripe_to_text,		\
20 	.swab		= bch2_ptr_swab,		\
21 	.trigger	= bch2_trigger_stripe,		\
22 	.min_val_size	= 8,				\
23 })
24 
stripe_csums_per_device(const struct bch_stripe * s)25 static inline unsigned stripe_csums_per_device(const struct bch_stripe *s)
26 {
27 	return DIV_ROUND_UP(le16_to_cpu(s->sectors),
28 			    1 << s->csum_granularity_bits);
29 }
30 
stripe_csum_offset(const struct bch_stripe * s,unsigned dev,unsigned csum_idx)31 static inline unsigned stripe_csum_offset(const struct bch_stripe *s,
32 					  unsigned dev, unsigned csum_idx)
33 {
34 	EBUG_ON(s->csum_type >= BCH_CSUM_NR);
35 
36 	unsigned csum_bytes = bch_crc_bytes[s->csum_type];
37 
38 	return sizeof(struct bch_stripe) +
39 		sizeof(struct bch_extent_ptr) * s->nr_blocks +
40 		(dev * stripe_csums_per_device(s) + csum_idx) * csum_bytes;
41 }
42 
stripe_blockcount_offset(const struct bch_stripe * s,unsigned idx)43 static inline unsigned stripe_blockcount_offset(const struct bch_stripe *s,
44 						unsigned idx)
45 {
46 	return stripe_csum_offset(s, s->nr_blocks, 0) +
47 		sizeof(u16) * idx;
48 }
49 
stripe_blockcount_get(const struct bch_stripe * s,unsigned idx)50 static inline unsigned stripe_blockcount_get(const struct bch_stripe *s,
51 					     unsigned idx)
52 {
53 	return le16_to_cpup((void *) s + stripe_blockcount_offset(s, idx));
54 }
55 
stripe_blockcount_set(struct bch_stripe * s,unsigned idx,unsigned v)56 static inline void stripe_blockcount_set(struct bch_stripe *s,
57 					 unsigned idx, unsigned v)
58 {
59 	__le16 *p = (void *) s + stripe_blockcount_offset(s, idx);
60 
61 	*p = cpu_to_le16(v);
62 }
63 
stripe_val_u64s(const struct bch_stripe * s)64 static inline unsigned stripe_val_u64s(const struct bch_stripe *s)
65 {
66 	return DIV_ROUND_UP(stripe_blockcount_offset(s, s->nr_blocks),
67 			    sizeof(u64));
68 }
69 
stripe_csum(struct bch_stripe * s,unsigned block,unsigned csum_idx)70 static inline void *stripe_csum(struct bch_stripe *s,
71 				unsigned block, unsigned csum_idx)
72 {
73 	EBUG_ON(block >= s->nr_blocks);
74 	EBUG_ON(csum_idx >= stripe_csums_per_device(s));
75 
76 	return (void *) s + stripe_csum_offset(s, block, csum_idx);
77 }
78 
stripe_csum_get(struct bch_stripe * s,unsigned block,unsigned csum_idx)79 static inline struct bch_csum stripe_csum_get(struct bch_stripe *s,
80 				   unsigned block, unsigned csum_idx)
81 {
82 	struct bch_csum csum = { 0 };
83 
84 	memcpy(&csum, stripe_csum(s, block, csum_idx), bch_crc_bytes[s->csum_type]);
85 	return csum;
86 }
87 
stripe_csum_set(struct bch_stripe * s,unsigned block,unsigned csum_idx,struct bch_csum csum)88 static inline void stripe_csum_set(struct bch_stripe *s,
89 				   unsigned block, unsigned csum_idx,
90 				   struct bch_csum csum)
91 {
92 	memcpy(stripe_csum(s, block, csum_idx), &csum, bch_crc_bytes[s->csum_type]);
93 }
94 
__bch2_ptr_matches_stripe(const struct bch_extent_ptr * stripe_ptr,const struct bch_extent_ptr * data_ptr,unsigned sectors)95 static inline bool __bch2_ptr_matches_stripe(const struct bch_extent_ptr *stripe_ptr,
96 					     const struct bch_extent_ptr *data_ptr,
97 					     unsigned sectors)
98 {
99 	return  (data_ptr->dev    == stripe_ptr->dev ||
100 		 data_ptr->dev    == BCH_SB_MEMBER_INVALID ||
101 		 stripe_ptr->dev  == BCH_SB_MEMBER_INVALID) &&
102 		data_ptr->gen    == stripe_ptr->gen &&
103 		data_ptr->offset >= stripe_ptr->offset &&
104 		data_ptr->offset  < stripe_ptr->offset + sectors;
105 }
106 
bch2_ptr_matches_stripe(const struct bch_stripe * s,struct extent_ptr_decoded p)107 static inline bool bch2_ptr_matches_stripe(const struct bch_stripe *s,
108 					   struct extent_ptr_decoded p)
109 {
110 	unsigned nr_data = s->nr_blocks - s->nr_redundant;
111 
112 	BUG_ON(!p.has_ec);
113 
114 	if (p.ec.block >= nr_data)
115 		return false;
116 
117 	return __bch2_ptr_matches_stripe(&s->ptrs[p.ec.block], &p.ptr,
118 					 le16_to_cpu(s->sectors));
119 }
120 
bch2_ptr_matches_stripe_m(const struct gc_stripe * m,struct extent_ptr_decoded p)121 static inline bool bch2_ptr_matches_stripe_m(const struct gc_stripe *m,
122 					     struct extent_ptr_decoded p)
123 {
124 	unsigned nr_data = m->nr_blocks - m->nr_redundant;
125 
126 	BUG_ON(!p.has_ec);
127 
128 	if (p.ec.block >= nr_data)
129 		return false;
130 
131 	return __bch2_ptr_matches_stripe(&m->ptrs[p.ec.block], &p.ptr,
132 					 m->sectors);
133 }
134 
135 struct bch_read_bio;
136 
137 struct ec_stripe_buf {
138 	/* might not be buffering the entire stripe: */
139 	unsigned		offset;
140 	unsigned		size;
141 	unsigned long		valid[BITS_TO_LONGS(BCH_BKEY_PTRS_MAX)];
142 
143 	void			*data[BCH_BKEY_PTRS_MAX];
144 
145 	__BKEY_PADDED(key, 255);
146 };
147 
148 struct ec_stripe_head;
149 
150 enum ec_stripe_ref {
151 	STRIPE_REF_io,
152 	STRIPE_REF_stripe,
153 	STRIPE_REF_NR
154 };
155 
156 struct ec_stripe_new {
157 	struct bch_fs		*c;
158 	struct ec_stripe_head	*h;
159 	struct mutex		lock;
160 	struct list_head	list;
161 
162 	struct hlist_node	hash;
163 	u64			idx;
164 
165 	struct closure		iodone;
166 
167 	atomic_t		ref[STRIPE_REF_NR];
168 
169 	int			err;
170 
171 	u8			nr_data;
172 	u8			nr_parity;
173 	bool			allocated;
174 	bool			pending;
175 	bool			have_existing_stripe;
176 
177 	unsigned long		blocks_gotten[BITS_TO_LONGS(BCH_BKEY_PTRS_MAX)];
178 	unsigned long		blocks_allocated[BITS_TO_LONGS(BCH_BKEY_PTRS_MAX)];
179 	open_bucket_idx_t	blocks[BCH_BKEY_PTRS_MAX];
180 	struct disk_reservation	res;
181 
182 	struct ec_stripe_buf	new_stripe;
183 	struct ec_stripe_buf	existing_stripe;
184 };
185 
186 struct ec_stripe_head {
187 	struct list_head	list;
188 	struct mutex		lock;
189 
190 	unsigned		disk_label;
191 	unsigned		algo;
192 	unsigned		redundancy;
193 	enum bch_watermark	watermark;
194 	bool			insufficient_devs;
195 
196 	unsigned long		rw_devs_change_count;
197 
198 	u64			nr_created;
199 
200 	struct bch_devs_mask	devs;
201 	unsigned		nr_active_devs;
202 
203 	unsigned		blocksize;
204 
205 	struct dev_stripe_state	block_stripe;
206 	struct dev_stripe_state	parity_stripe;
207 
208 	struct ec_stripe_new	*s;
209 };
210 
211 int bch2_ec_read_extent(struct btree_trans *, struct bch_read_bio *, struct bkey_s_c);
212 
213 void *bch2_writepoint_ec_buf(struct bch_fs *, struct write_point *);
214 
215 void bch2_ec_bucket_cancel(struct bch_fs *, struct open_bucket *);
216 
217 int bch2_ec_stripe_new_alloc(struct bch_fs *, struct ec_stripe_head *);
218 
219 void bch2_ec_stripe_head_put(struct bch_fs *, struct ec_stripe_head *);
220 struct ec_stripe_head *bch2_ec_stripe_head_get(struct btree_trans *,
221 			unsigned, unsigned, unsigned,
222 			enum bch_watermark, struct closure *);
223 
224 void bch2_stripes_heap_update(struct bch_fs *, struct stripe *, size_t);
225 void bch2_stripes_heap_del(struct bch_fs *, struct stripe *, size_t);
226 void bch2_stripes_heap_insert(struct bch_fs *, struct stripe *, size_t);
227 
228 void bch2_do_stripe_deletes(struct bch_fs *);
229 void bch2_ec_do_stripe_creates(struct bch_fs *);
230 void bch2_ec_stripe_new_free(struct bch_fs *, struct ec_stripe_new *);
231 
ec_stripe_new_get(struct ec_stripe_new * s,enum ec_stripe_ref ref)232 static inline void ec_stripe_new_get(struct ec_stripe_new *s,
233 				     enum ec_stripe_ref ref)
234 {
235 	atomic_inc(&s->ref[ref]);
236 }
237 
ec_stripe_new_put(struct bch_fs * c,struct ec_stripe_new * s,enum ec_stripe_ref ref)238 static inline void ec_stripe_new_put(struct bch_fs *c, struct ec_stripe_new *s,
239 				     enum ec_stripe_ref ref)
240 {
241 	BUG_ON(atomic_read(&s->ref[ref]) <= 0);
242 
243 	if (atomic_dec_and_test(&s->ref[ref]))
244 		switch (ref) {
245 		case STRIPE_REF_stripe:
246 			bch2_ec_stripe_new_free(c, s);
247 			break;
248 		case STRIPE_REF_io:
249 			bch2_ec_do_stripe_creates(c);
250 			break;
251 		default:
252 			BUG();
253 		}
254 }
255 
256 int bch2_dev_remove_stripes(struct bch_fs *, unsigned);
257 
258 void bch2_ec_stop_dev(struct bch_fs *, struct bch_dev *);
259 void bch2_fs_ec_stop(struct bch_fs *);
260 void bch2_fs_ec_flush(struct bch_fs *);
261 
262 int bch2_stripes_read(struct bch_fs *);
263 
264 void bch2_stripes_heap_to_text(struct printbuf *, struct bch_fs *);
265 void bch2_new_stripes_to_text(struct printbuf *, struct bch_fs *);
266 
267 void bch2_fs_ec_exit(struct bch_fs *);
268 void bch2_fs_ec_init_early(struct bch_fs *);
269 int bch2_fs_ec_init(struct bch_fs *);
270 
271 #endif /* _BCACHEFS_EC_H */
272