1 // SPDX-License-Identifier: GPL-2.0
2
3 /* erasure coding */
4
5 #include "bcachefs.h"
6 #include "alloc_background.h"
7 #include "alloc_foreground.h"
8 #include "backpointers.h"
9 #include "bkey_buf.h"
10 #include "bset.h"
11 #include "btree_gc.h"
12 #include "btree_update.h"
13 #include "btree_write_buffer.h"
14 #include "buckets.h"
15 #include "checksum.h"
16 #include "disk_accounting.h"
17 #include "disk_groups.h"
18 #include "ec.h"
19 #include "enumerated_ref.h"
20 #include "error.h"
21 #include "io_read.h"
22 #include "io_write.h"
23 #include "keylist.h"
24 #include "lru.h"
25 #include "recovery.h"
26 #include "replicas.h"
27 #include "super-io.h"
28 #include "util.h"
29
30 #include <linux/sort.h>
31 #include <linux/string_choices.h>
32
33 #ifdef __KERNEL__
34
35 #include <linux/raid/pq.h>
36 #include <linux/raid/xor.h>
37
raid5_recov(unsigned disks,unsigned failed_idx,size_t size,void ** data)38 static void raid5_recov(unsigned disks, unsigned failed_idx,
39 size_t size, void **data)
40 {
41 unsigned i = 2, nr;
42
43 BUG_ON(failed_idx >= disks);
44
45 swap(data[0], data[failed_idx]);
46 memcpy(data[0], data[1], size);
47
48 while (i < disks) {
49 nr = min_t(unsigned, disks - i, MAX_XOR_BLOCKS);
50 xor_blocks(nr, size, data[0], data + i);
51 i += nr;
52 }
53
54 swap(data[0], data[failed_idx]);
55 }
56
raid_gen(int nd,int np,size_t size,void ** v)57 static void raid_gen(int nd, int np, size_t size, void **v)
58 {
59 if (np >= 1)
60 raid5_recov(nd + np, nd, size, v);
61 if (np >= 2)
62 raid6_call.gen_syndrome(nd + np, size, v);
63 BUG_ON(np > 2);
64 }
65
raid_rec(int nr,int * ir,int nd,int np,size_t size,void ** v)66 static void raid_rec(int nr, int *ir, int nd, int np, size_t size, void **v)
67 {
68 switch (nr) {
69 case 0:
70 break;
71 case 1:
72 if (ir[0] < nd + 1)
73 raid5_recov(nd + 1, ir[0], size, v);
74 else
75 raid6_call.gen_syndrome(nd + np, size, v);
76 break;
77 case 2:
78 if (ir[1] < nd) {
79 /* data+data failure. */
80 raid6_2data_recov(nd + np, size, ir[0], ir[1], v);
81 } else if (ir[0] < nd) {
82 /* data + p/q failure */
83
84 if (ir[1] == nd) /* data + p failure */
85 raid6_datap_recov(nd + np, size, ir[0], v);
86 else { /* data + q failure */
87 raid5_recov(nd + 1, ir[0], size, v);
88 raid6_call.gen_syndrome(nd + np, size, v);
89 }
90 } else {
91 raid_gen(nd, np, size, v);
92 }
93 break;
94 default:
95 BUG();
96 }
97 }
98
99 #else
100
101 #include <raid/raid.h>
102
103 #endif
104
105 struct ec_bio {
106 struct bch_dev *ca;
107 struct ec_stripe_buf *buf;
108 size_t idx;
109 int rw;
110 u64 submit_time;
111 struct bio bio;
112 };
113
114 /* Stripes btree keys: */
115
bch2_stripe_validate(struct bch_fs * c,struct bkey_s_c k,struct bkey_validate_context from)116 int bch2_stripe_validate(struct bch_fs *c, struct bkey_s_c k,
117 struct bkey_validate_context from)
118 {
119 const struct bch_stripe *s = bkey_s_c_to_stripe(k).v;
120 int ret = 0;
121
122 bkey_fsck_err_on(bkey_eq(k.k->p, POS_MIN) ||
123 bpos_gt(k.k->p, POS(0, U32_MAX)),
124 c, stripe_pos_bad,
125 "stripe at bad pos");
126
127 bkey_fsck_err_on(bkey_val_u64s(k.k) < stripe_val_u64s(s),
128 c, stripe_val_size_bad,
129 "incorrect value size (%zu < %u)",
130 bkey_val_u64s(k.k), stripe_val_u64s(s));
131
132 bkey_fsck_err_on(s->csum_granularity_bits >= 64,
133 c, stripe_csum_granularity_bad,
134 "invalid csum granularity (%u >= 64)",
135 s->csum_granularity_bits);
136
137 ret = bch2_bkey_ptrs_validate(c, k, from);
138 fsck_err:
139 return ret;
140 }
141
bch2_stripe_to_text(struct printbuf * out,struct bch_fs * c,struct bkey_s_c k)142 void bch2_stripe_to_text(struct printbuf *out, struct bch_fs *c,
143 struct bkey_s_c k)
144 {
145 const struct bch_stripe *sp = bkey_s_c_to_stripe(k).v;
146 struct bch_stripe s = {};
147
148 memcpy(&s, sp, min(sizeof(s), bkey_val_bytes(k.k)));
149
150 unsigned nr_data = s.nr_blocks - s.nr_redundant;
151
152 prt_printf(out, "algo %u sectors %u blocks %u:%u csum ",
153 s.algorithm,
154 le16_to_cpu(s.sectors),
155 nr_data,
156 s.nr_redundant);
157 bch2_prt_csum_type(out, s.csum_type);
158 prt_str(out, " gran ");
159 if (s.csum_granularity_bits < 64)
160 prt_printf(out, "%llu", 1ULL << s.csum_granularity_bits);
161 else
162 prt_printf(out, "(invalid shift %u)", s.csum_granularity_bits);
163
164 if (s.disk_label) {
165 prt_str(out, " label");
166 bch2_disk_path_to_text(out, c, s.disk_label - 1);
167 }
168
169 for (unsigned i = 0; i < s.nr_blocks; i++) {
170 const struct bch_extent_ptr *ptr = sp->ptrs + i;
171
172 if ((void *) ptr >= bkey_val_end(k))
173 break;
174
175 prt_char(out, ' ');
176 bch2_extent_ptr_to_text(out, c, ptr);
177
178 if (s.csum_type < BCH_CSUM_NR &&
179 i < nr_data &&
180 stripe_blockcount_offset(&s, i) < bkey_val_bytes(k.k))
181 prt_printf(out, "#%u", stripe_blockcount_get(sp, i));
182 }
183 }
184
185 /* Triggers: */
186
__mark_stripe_bucket(struct btree_trans * trans,struct bch_dev * ca,struct bkey_s_c_stripe s,unsigned ptr_idx,bool deleting,struct bpos bucket,struct bch_alloc_v4 * a,enum btree_iter_update_trigger_flags flags)187 static int __mark_stripe_bucket(struct btree_trans *trans,
188 struct bch_dev *ca,
189 struct bkey_s_c_stripe s,
190 unsigned ptr_idx, bool deleting,
191 struct bpos bucket,
192 struct bch_alloc_v4 *a,
193 enum btree_iter_update_trigger_flags flags)
194 {
195 const struct bch_extent_ptr *ptr = s.v->ptrs + ptr_idx;
196 unsigned nr_data = s.v->nr_blocks - s.v->nr_redundant;
197 bool parity = ptr_idx >= nr_data;
198 enum bch_data_type data_type = parity ? BCH_DATA_parity : BCH_DATA_stripe;
199 s64 sectors = parity ? le16_to_cpu(s.v->sectors) : 0;
200 struct printbuf buf = PRINTBUF;
201 int ret = 0;
202
203 struct bch_fs *c = trans->c;
204 if (deleting)
205 sectors = -sectors;
206
207 if (!deleting) {
208 if (bch2_trans_inconsistent_on(a->stripe ||
209 a->stripe_redundancy, trans,
210 "bucket %llu:%llu gen %u data type %s dirty_sectors %u: multiple stripes using same bucket (%u, %llu)\n%s",
211 bucket.inode, bucket.offset, a->gen,
212 bch2_data_type_str(a->data_type),
213 a->dirty_sectors,
214 a->stripe, s.k->p.offset,
215 (bch2_bkey_val_to_text(&buf, c, s.s_c), buf.buf))) {
216 ret = bch_err_throw(c, mark_stripe);
217 goto err;
218 }
219
220 if (bch2_trans_inconsistent_on(parity && bch2_bucket_sectors_total(*a), trans,
221 "bucket %llu:%llu gen %u data type %s dirty_sectors %u cached_sectors %u: data already in parity bucket\n%s",
222 bucket.inode, bucket.offset, a->gen,
223 bch2_data_type_str(a->data_type),
224 a->dirty_sectors,
225 a->cached_sectors,
226 (bch2_bkey_val_to_text(&buf, c, s.s_c), buf.buf))) {
227 ret = bch_err_throw(c, mark_stripe);
228 goto err;
229 }
230 } else {
231 if (bch2_trans_inconsistent_on(a->stripe != s.k->p.offset ||
232 a->stripe_redundancy != s.v->nr_redundant, trans,
233 "bucket %llu:%llu gen %u: not marked as stripe when deleting stripe (got %u)\n%s",
234 bucket.inode, bucket.offset, a->gen,
235 a->stripe,
236 (bch2_bkey_val_to_text(&buf, c, s.s_c), buf.buf))) {
237 ret = bch_err_throw(c, mark_stripe);
238 goto err;
239 }
240
241 if (bch2_trans_inconsistent_on(a->data_type != data_type, trans,
242 "bucket %llu:%llu gen %u data type %s: wrong data type when stripe, should be %s\n%s",
243 bucket.inode, bucket.offset, a->gen,
244 bch2_data_type_str(a->data_type),
245 bch2_data_type_str(data_type),
246 (bch2_bkey_val_to_text(&buf, c, s.s_c), buf.buf))) {
247 ret = bch_err_throw(c, mark_stripe);
248 goto err;
249 }
250
251 if (bch2_trans_inconsistent_on(parity &&
252 (a->dirty_sectors != -sectors ||
253 a->cached_sectors), trans,
254 "bucket %llu:%llu gen %u dirty_sectors %u cached_sectors %u: wrong sectors when deleting parity block of stripe\n%s",
255 bucket.inode, bucket.offset, a->gen,
256 a->dirty_sectors,
257 a->cached_sectors,
258 (bch2_bkey_val_to_text(&buf, c, s.s_c), buf.buf))) {
259 ret = bch_err_throw(c, mark_stripe);
260 goto err;
261 }
262 }
263
264 if (sectors) {
265 ret = bch2_bucket_ref_update(trans, ca, s.s_c, ptr, sectors, data_type,
266 a->gen, a->data_type, &a->dirty_sectors);
267 if (ret)
268 goto err;
269 }
270
271 if (!deleting) {
272 a->stripe = s.k->p.offset;
273 a->stripe_redundancy = s.v->nr_redundant;
274 alloc_data_type_set(a, data_type);
275 } else {
276 a->stripe = 0;
277 a->stripe_redundancy = 0;
278 alloc_data_type_set(a, BCH_DATA_user);
279 }
280 err:
281 printbuf_exit(&buf);
282 return ret;
283 }
284
mark_stripe_bucket(struct btree_trans * trans,struct bkey_s_c_stripe s,unsigned ptr_idx,bool deleting,enum btree_iter_update_trigger_flags flags)285 static int mark_stripe_bucket(struct btree_trans *trans,
286 struct bkey_s_c_stripe s,
287 unsigned ptr_idx, bool deleting,
288 enum btree_iter_update_trigger_flags flags)
289 {
290 struct bch_fs *c = trans->c;
291 const struct bch_extent_ptr *ptr = s.v->ptrs + ptr_idx;
292 struct printbuf buf = PRINTBUF;
293 int ret = 0;
294
295 struct bch_dev *ca = bch2_dev_tryget(c, ptr->dev);
296 if (unlikely(!ca)) {
297 if (ptr->dev != BCH_SB_MEMBER_INVALID && !(flags & BTREE_TRIGGER_overwrite))
298 ret = bch_err_throw(c, mark_stripe);
299 goto err;
300 }
301
302 struct bpos bucket = PTR_BUCKET_POS(ca, ptr);
303
304 if (flags & BTREE_TRIGGER_transactional) {
305 struct extent_ptr_decoded p = {
306 .ptr = *ptr,
307 .crc = bch2_extent_crc_unpack(s.k, NULL),
308 };
309 struct bkey_i_backpointer bp;
310 bch2_extent_ptr_to_bp(c, BTREE_ID_stripes, 0, s.s_c, p,
311 (const union bch_extent_entry *) ptr, &bp);
312
313 struct bkey_i_alloc_v4 *a =
314 bch2_trans_start_alloc_update(trans, bucket, 0);
315 ret = PTR_ERR_OR_ZERO(a) ?:
316 __mark_stripe_bucket(trans, ca, s, ptr_idx, deleting, bucket, &a->v, flags) ?:
317 bch2_bucket_backpointer_mod(trans, s.s_c, &bp,
318 !(flags & BTREE_TRIGGER_overwrite));
319 if (ret)
320 goto err;
321 }
322
323 if (flags & BTREE_TRIGGER_gc) {
324 struct bucket *g = gc_bucket(ca, bucket.offset);
325 if (bch2_fs_inconsistent_on(!g, c, "reference to invalid bucket on device %u\n%s",
326 ptr->dev,
327 (bch2_bkey_val_to_text(&buf, c, s.s_c), buf.buf))) {
328 ret = bch_err_throw(c, mark_stripe);
329 goto err;
330 }
331
332 bucket_lock(g);
333 struct bch_alloc_v4 old = bucket_m_to_alloc(*g), new = old;
334 ret = __mark_stripe_bucket(trans, ca, s, ptr_idx, deleting, bucket, &new, flags);
335 alloc_to_bucket(g, new);
336 bucket_unlock(g);
337
338 if (!ret)
339 ret = bch2_alloc_key_to_dev_counters(trans, ca, &old, &new, flags);
340 }
341 err:
342 bch2_dev_put(ca);
343 printbuf_exit(&buf);
344 return ret;
345 }
346
mark_stripe_buckets(struct btree_trans * trans,struct bkey_s_c old,struct bkey_s_c new,enum btree_iter_update_trigger_flags flags)347 static int mark_stripe_buckets(struct btree_trans *trans,
348 struct bkey_s_c old, struct bkey_s_c new,
349 enum btree_iter_update_trigger_flags flags)
350 {
351 const struct bch_stripe *old_s = old.k->type == KEY_TYPE_stripe
352 ? bkey_s_c_to_stripe(old).v : NULL;
353 const struct bch_stripe *new_s = new.k->type == KEY_TYPE_stripe
354 ? bkey_s_c_to_stripe(new).v : NULL;
355
356 BUG_ON(old_s && new_s && old_s->nr_blocks != new_s->nr_blocks);
357
358 unsigned nr_blocks = new_s ? new_s->nr_blocks : old_s->nr_blocks;
359
360 for (unsigned i = 0; i < nr_blocks; i++) {
361 if (new_s && old_s &&
362 !memcmp(&new_s->ptrs[i],
363 &old_s->ptrs[i],
364 sizeof(new_s->ptrs[i])))
365 continue;
366
367 if (new_s) {
368 int ret = mark_stripe_bucket(trans,
369 bkey_s_c_to_stripe(new), i, false, flags);
370 if (ret)
371 return ret;
372 }
373
374 if (old_s) {
375 int ret = mark_stripe_bucket(trans,
376 bkey_s_c_to_stripe(old), i, true, flags);
377 if (ret)
378 return ret;
379 }
380 }
381
382 return 0;
383 }
384
bch2_trigger_stripe(struct btree_trans * trans,enum btree_id btree,unsigned level,struct bkey_s_c old,struct bkey_s _new,enum btree_iter_update_trigger_flags flags)385 int bch2_trigger_stripe(struct btree_trans *trans,
386 enum btree_id btree, unsigned level,
387 struct bkey_s_c old, struct bkey_s _new,
388 enum btree_iter_update_trigger_flags flags)
389 {
390 struct bkey_s_c new = _new.s_c;
391 struct bch_fs *c = trans->c;
392 u64 idx = new.k->p.offset;
393 const struct bch_stripe *old_s = old.k->type == KEY_TYPE_stripe
394 ? bkey_s_c_to_stripe(old).v : NULL;
395 const struct bch_stripe *new_s = new.k->type == KEY_TYPE_stripe
396 ? bkey_s_c_to_stripe(new).v : NULL;
397
398 if (unlikely(flags & BTREE_TRIGGER_check_repair))
399 return bch2_check_fix_ptrs(trans, btree, level, _new.s_c, flags);
400
401 BUG_ON(new_s && old_s &&
402 (new_s->nr_blocks != old_s->nr_blocks ||
403 new_s->nr_redundant != old_s->nr_redundant));
404
405 if (flags & BTREE_TRIGGER_transactional) {
406 int ret = bch2_lru_change(trans,
407 BCH_LRU_STRIPE_FRAGMENTATION,
408 idx,
409 stripe_lru_pos(old_s),
410 stripe_lru_pos(new_s));
411 if (ret)
412 return ret;
413 }
414
415 if (flags & (BTREE_TRIGGER_transactional|BTREE_TRIGGER_gc)) {
416 /*
417 * If the pointers aren't changing, we don't need to do anything:
418 */
419 if (new_s && old_s &&
420 new_s->nr_blocks == old_s->nr_blocks &&
421 new_s->nr_redundant == old_s->nr_redundant &&
422 !memcmp(old_s->ptrs, new_s->ptrs,
423 new_s->nr_blocks * sizeof(struct bch_extent_ptr)))
424 return 0;
425
426 struct gc_stripe *gc = NULL;
427 if (flags & BTREE_TRIGGER_gc) {
428 gc = genradix_ptr_alloc(&c->gc_stripes, idx, GFP_KERNEL);
429 if (!gc) {
430 bch_err(c, "error allocating memory for gc_stripes, idx %llu", idx);
431 return bch_err_throw(c, ENOMEM_mark_stripe);
432 }
433
434 /*
435 * This will be wrong when we bring back runtime gc: we should
436 * be unmarking the old key and then marking the new key
437 *
438 * Also: when we bring back runtime gc, locking
439 */
440 gc->alive = true;
441 gc->sectors = le16_to_cpu(new_s->sectors);
442 gc->nr_blocks = new_s->nr_blocks;
443 gc->nr_redundant = new_s->nr_redundant;
444
445 for (unsigned i = 0; i < new_s->nr_blocks; i++)
446 gc->ptrs[i] = new_s->ptrs[i];
447
448 /*
449 * gc recalculates this field from stripe ptr
450 * references:
451 */
452 memset(gc->block_sectors, 0, sizeof(gc->block_sectors));
453 }
454
455 if (new_s) {
456 s64 sectors = (u64) le16_to_cpu(new_s->sectors) * new_s->nr_redundant;
457
458 struct disk_accounting_pos acc;
459 memset(&acc, 0, sizeof(acc));
460 acc.type = BCH_DISK_ACCOUNTING_replicas;
461 bch2_bkey_to_replicas(&acc.replicas, new);
462 int ret = bch2_disk_accounting_mod(trans, &acc, §ors, 1, gc);
463 if (ret)
464 return ret;
465
466 if (gc)
467 unsafe_memcpy(&gc->r.e, &acc.replicas,
468 replicas_entry_bytes(&acc.replicas), "VLA");
469 }
470
471 if (old_s) {
472 s64 sectors = -((s64) le16_to_cpu(old_s->sectors)) * old_s->nr_redundant;
473
474 struct disk_accounting_pos acc;
475 memset(&acc, 0, sizeof(acc));
476 acc.type = BCH_DISK_ACCOUNTING_replicas;
477 bch2_bkey_to_replicas(&acc.replicas, old);
478 int ret = bch2_disk_accounting_mod(trans, &acc, §ors, 1, gc);
479 if (ret)
480 return ret;
481 }
482
483 int ret = mark_stripe_buckets(trans, old, new, flags);
484 if (ret)
485 return ret;
486 }
487
488 return 0;
489 }
490
491 /* returns blocknr in stripe that we matched: */
bkey_matches_stripe(struct bch_stripe * s,struct bkey_s_c k,unsigned * block)492 static const struct bch_extent_ptr *bkey_matches_stripe(struct bch_stripe *s,
493 struct bkey_s_c k, unsigned *block)
494 {
495 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
496 unsigned i, nr_data = s->nr_blocks - s->nr_redundant;
497
498 bkey_for_each_ptr(ptrs, ptr)
499 for (i = 0; i < nr_data; i++)
500 if (__bch2_ptr_matches_stripe(&s->ptrs[i], ptr,
501 le16_to_cpu(s->sectors))) {
502 *block = i;
503 return ptr;
504 }
505
506 return NULL;
507 }
508
extent_has_stripe_ptr(struct bkey_s_c k,u64 idx)509 static bool extent_has_stripe_ptr(struct bkey_s_c k, u64 idx)
510 {
511 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
512 const union bch_extent_entry *entry;
513
514 bkey_extent_entry_for_each(ptrs, entry)
515 if (extent_entry_type(entry) ==
516 BCH_EXTENT_ENTRY_stripe_ptr &&
517 entry->stripe_ptr.idx == idx)
518 return true;
519
520 return false;
521 }
522
523 /* Stripe bufs: */
524
ec_stripe_buf_exit(struct ec_stripe_buf * buf)525 static void ec_stripe_buf_exit(struct ec_stripe_buf *buf)
526 {
527 if (buf->key.k.type == KEY_TYPE_stripe) {
528 struct bkey_i_stripe *s = bkey_i_to_stripe(&buf->key);
529 unsigned i;
530
531 for (i = 0; i < s->v.nr_blocks; i++) {
532 kvfree(buf->data[i]);
533 buf->data[i] = NULL;
534 }
535 }
536 }
537
538 /* XXX: this is a non-mempoolified memory allocation: */
ec_stripe_buf_init(struct bch_fs * c,struct ec_stripe_buf * buf,unsigned offset,unsigned size)539 static int ec_stripe_buf_init(struct bch_fs *c,
540 struct ec_stripe_buf *buf,
541 unsigned offset, unsigned size)
542 {
543 struct bch_stripe *v = &bkey_i_to_stripe(&buf->key)->v;
544 unsigned csum_granularity = 1U << v->csum_granularity_bits;
545 unsigned end = offset + size;
546 unsigned i;
547
548 BUG_ON(end > le16_to_cpu(v->sectors));
549
550 offset = round_down(offset, csum_granularity);
551 end = min_t(unsigned, le16_to_cpu(v->sectors),
552 round_up(end, csum_granularity));
553
554 buf->offset = offset;
555 buf->size = end - offset;
556
557 memset(buf->valid, 0xFF, sizeof(buf->valid));
558
559 for (i = 0; i < v->nr_blocks; i++) {
560 buf->data[i] = kvmalloc(buf->size << 9, GFP_KERNEL);
561 if (!buf->data[i])
562 goto err;
563 }
564
565 return 0;
566 err:
567 ec_stripe_buf_exit(buf);
568 return bch_err_throw(c, ENOMEM_stripe_buf);
569 }
570
571 /* Checksumming: */
572
ec_block_checksum(struct ec_stripe_buf * buf,unsigned block,unsigned offset)573 static struct bch_csum ec_block_checksum(struct ec_stripe_buf *buf,
574 unsigned block, unsigned offset)
575 {
576 struct bch_stripe *v = &bkey_i_to_stripe(&buf->key)->v;
577 unsigned csum_granularity = 1 << v->csum_granularity_bits;
578 unsigned end = buf->offset + buf->size;
579 unsigned len = min(csum_granularity, end - offset);
580
581 BUG_ON(offset >= end);
582 BUG_ON(offset < buf->offset);
583 BUG_ON(offset & (csum_granularity - 1));
584 BUG_ON(offset + len != le16_to_cpu(v->sectors) &&
585 (len & (csum_granularity - 1)));
586
587 return bch2_checksum(NULL, v->csum_type,
588 null_nonce(),
589 buf->data[block] + ((offset - buf->offset) << 9),
590 len << 9);
591 }
592
ec_generate_checksums(struct ec_stripe_buf * buf)593 static void ec_generate_checksums(struct ec_stripe_buf *buf)
594 {
595 struct bch_stripe *v = &bkey_i_to_stripe(&buf->key)->v;
596 unsigned i, j, csums_per_device = stripe_csums_per_device(v);
597
598 if (!v->csum_type)
599 return;
600
601 BUG_ON(buf->offset);
602 BUG_ON(buf->size != le16_to_cpu(v->sectors));
603
604 for (i = 0; i < v->nr_blocks; i++)
605 for (j = 0; j < csums_per_device; j++)
606 stripe_csum_set(v, i, j,
607 ec_block_checksum(buf, i, j << v->csum_granularity_bits));
608 }
609
ec_validate_checksums(struct bch_fs * c,struct ec_stripe_buf * buf)610 static void ec_validate_checksums(struct bch_fs *c, struct ec_stripe_buf *buf)
611 {
612 struct bch_stripe *v = &bkey_i_to_stripe(&buf->key)->v;
613 unsigned csum_granularity = 1 << v->csum_granularity_bits;
614 unsigned i;
615
616 if (!v->csum_type)
617 return;
618
619 for (i = 0; i < v->nr_blocks; i++) {
620 unsigned offset = buf->offset;
621 unsigned end = buf->offset + buf->size;
622
623 if (!test_bit(i, buf->valid))
624 continue;
625
626 while (offset < end) {
627 unsigned j = offset >> v->csum_granularity_bits;
628 unsigned len = min(csum_granularity, end - offset);
629 struct bch_csum want = stripe_csum_get(v, i, j);
630 struct bch_csum got = ec_block_checksum(buf, i, offset);
631
632 if (bch2_crc_cmp(want, got)) {
633 struct bch_dev *ca = bch2_dev_tryget(c, v->ptrs[i].dev);
634 if (ca) {
635 struct printbuf err = PRINTBUF;
636
637 prt_str(&err, "stripe ");
638 bch2_csum_err_msg(&err, v->csum_type, want, got);
639 prt_printf(&err, " for %ps at %u of\n ", (void *) _RET_IP_, i);
640 bch2_bkey_val_to_text(&err, c, bkey_i_to_s_c(&buf->key));
641 bch_err_ratelimited(ca, "%s", err.buf);
642 printbuf_exit(&err);
643
644 bch2_io_error(ca, BCH_MEMBER_ERROR_checksum);
645 }
646
647 clear_bit(i, buf->valid);
648 break;
649 }
650
651 offset += len;
652 }
653 }
654 }
655
656 /* Erasure coding: */
657
ec_generate_ec(struct ec_stripe_buf * buf)658 static void ec_generate_ec(struct ec_stripe_buf *buf)
659 {
660 struct bch_stripe *v = &bkey_i_to_stripe(&buf->key)->v;
661 unsigned nr_data = v->nr_blocks - v->nr_redundant;
662 unsigned bytes = le16_to_cpu(v->sectors) << 9;
663
664 raid_gen(nr_data, v->nr_redundant, bytes, buf->data);
665 }
666
ec_nr_failed(struct ec_stripe_buf * buf)667 static unsigned ec_nr_failed(struct ec_stripe_buf *buf)
668 {
669 struct bch_stripe *v = &bkey_i_to_stripe(&buf->key)->v;
670
671 return v->nr_blocks - bitmap_weight(buf->valid, v->nr_blocks);
672 }
673
ec_do_recov(struct bch_fs * c,struct ec_stripe_buf * buf)674 static int ec_do_recov(struct bch_fs *c, struct ec_stripe_buf *buf)
675 {
676 struct bch_stripe *v = &bkey_i_to_stripe(&buf->key)->v;
677 unsigned i, failed[BCH_BKEY_PTRS_MAX], nr_failed = 0;
678 unsigned nr_data = v->nr_blocks - v->nr_redundant;
679 unsigned bytes = buf->size << 9;
680
681 if (ec_nr_failed(buf) > v->nr_redundant) {
682 bch_err_ratelimited(c,
683 "error doing reconstruct read: unable to read enough blocks");
684 return -1;
685 }
686
687 for (i = 0; i < nr_data; i++)
688 if (!test_bit(i, buf->valid))
689 failed[nr_failed++] = i;
690
691 raid_rec(nr_failed, failed, nr_data, v->nr_redundant, bytes, buf->data);
692 return 0;
693 }
694
695 /* IO: */
696
ec_block_endio(struct bio * bio)697 static void ec_block_endio(struct bio *bio)
698 {
699 struct ec_bio *ec_bio = container_of(bio, struct ec_bio, bio);
700 struct bch_stripe *v = &bkey_i_to_stripe(&ec_bio->buf->key)->v;
701 struct bch_extent_ptr *ptr = &v->ptrs[ec_bio->idx];
702 struct bch_dev *ca = ec_bio->ca;
703 struct closure *cl = bio->bi_private;
704 int rw = ec_bio->rw;
705 unsigned ref = rw == READ
706 ? BCH_DEV_READ_REF_ec_block
707 : BCH_DEV_WRITE_REF_ec_block;
708
709 bch2_account_io_completion(ca, bio_data_dir(bio),
710 ec_bio->submit_time, !bio->bi_status);
711
712 if (bio->bi_status) {
713 bch_err_dev_ratelimited(ca, "erasure coding %s error: %s",
714 str_write_read(bio_data_dir(bio)),
715 bch2_blk_status_to_str(bio->bi_status));
716 clear_bit(ec_bio->idx, ec_bio->buf->valid);
717 }
718
719 int stale = dev_ptr_stale(ca, ptr);
720 if (stale) {
721 bch_err_ratelimited(ca->fs,
722 "error %s stripe: stale/invalid pointer (%i) after io",
723 bio_data_dir(bio) == READ ? "reading from" : "writing to",
724 stale);
725 clear_bit(ec_bio->idx, ec_bio->buf->valid);
726 }
727
728 bio_put(&ec_bio->bio);
729 enumerated_ref_put(&ca->io_ref[rw], ref);
730 closure_put(cl);
731 }
732
ec_block_io(struct bch_fs * c,struct ec_stripe_buf * buf,blk_opf_t opf,unsigned idx,struct closure * cl)733 static void ec_block_io(struct bch_fs *c, struct ec_stripe_buf *buf,
734 blk_opf_t opf, unsigned idx, struct closure *cl)
735 {
736 struct bch_stripe *v = &bkey_i_to_stripe(&buf->key)->v;
737 unsigned offset = 0, bytes = buf->size << 9;
738 struct bch_extent_ptr *ptr = &v->ptrs[idx];
739 enum bch_data_type data_type = idx < v->nr_blocks - v->nr_redundant
740 ? BCH_DATA_user
741 : BCH_DATA_parity;
742 int rw = op_is_write(opf);
743 unsigned ref = rw == READ
744 ? BCH_DEV_READ_REF_ec_block
745 : BCH_DEV_WRITE_REF_ec_block;
746
747 struct bch_dev *ca = bch2_dev_get_ioref(c, ptr->dev, rw, ref);
748 if (!ca) {
749 clear_bit(idx, buf->valid);
750 return;
751 }
752
753 int stale = dev_ptr_stale(ca, ptr);
754 if (stale) {
755 bch_err_ratelimited(c,
756 "error %s stripe: stale pointer (%i)",
757 rw == READ ? "reading from" : "writing to",
758 stale);
759 clear_bit(idx, buf->valid);
760 return;
761 }
762
763
764 this_cpu_add(ca->io_done->sectors[rw][data_type], buf->size);
765
766 while (offset < bytes) {
767 unsigned nr_iovecs = min_t(size_t, BIO_MAX_VECS,
768 DIV_ROUND_UP(bytes, PAGE_SIZE));
769 unsigned b = min_t(size_t, bytes - offset,
770 nr_iovecs << PAGE_SHIFT);
771 struct ec_bio *ec_bio;
772
773 ec_bio = container_of(bio_alloc_bioset(ca->disk_sb.bdev,
774 nr_iovecs,
775 opf,
776 GFP_KERNEL,
777 &c->ec_bioset),
778 struct ec_bio, bio);
779
780 ec_bio->ca = ca;
781 ec_bio->buf = buf;
782 ec_bio->idx = idx;
783 ec_bio->rw = rw;
784 ec_bio->submit_time = local_clock();
785
786 ec_bio->bio.bi_iter.bi_sector = ptr->offset + buf->offset + (offset >> 9);
787 ec_bio->bio.bi_end_io = ec_block_endio;
788 ec_bio->bio.bi_private = cl;
789
790 bch2_bio_map(&ec_bio->bio, buf->data[idx] + offset, b);
791
792 closure_get(cl);
793 enumerated_ref_get(&ca->io_ref[rw], ref);
794
795 submit_bio(&ec_bio->bio);
796
797 offset += b;
798 }
799
800 enumerated_ref_put(&ca->io_ref[rw], ref);
801 }
802
get_stripe_key_trans(struct btree_trans * trans,u64 idx,struct ec_stripe_buf * stripe)803 static int get_stripe_key_trans(struct btree_trans *trans, u64 idx,
804 struct ec_stripe_buf *stripe)
805 {
806 struct btree_iter iter;
807 struct bkey_s_c k;
808 int ret;
809
810 k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_stripes,
811 POS(0, idx), BTREE_ITER_slots);
812 ret = bkey_err(k);
813 if (ret)
814 goto err;
815 if (k.k->type != KEY_TYPE_stripe) {
816 ret = -ENOENT;
817 goto err;
818 }
819 bkey_reassemble(&stripe->key, k);
820 err:
821 bch2_trans_iter_exit(trans, &iter);
822 return ret;
823 }
824
825 /* recovery read path: */
bch2_ec_read_extent(struct btree_trans * trans,struct bch_read_bio * rbio,struct bkey_s_c orig_k)826 int bch2_ec_read_extent(struct btree_trans *trans, struct bch_read_bio *rbio,
827 struct bkey_s_c orig_k)
828 {
829 struct bch_fs *c = trans->c;
830 struct ec_stripe_buf *buf = NULL;
831 struct closure cl;
832 struct bch_stripe *v;
833 unsigned i, offset;
834 const char *msg = NULL;
835 struct printbuf msgbuf = PRINTBUF;
836 int ret = 0;
837
838 closure_init_stack(&cl);
839
840 BUG_ON(!rbio->pick.has_ec);
841
842 buf = kzalloc(sizeof(*buf), GFP_NOFS);
843 if (!buf)
844 return bch_err_throw(c, ENOMEM_ec_read_extent);
845
846 ret = lockrestart_do(trans, get_stripe_key_trans(trans, rbio->pick.ec.idx, buf));
847 if (ret) {
848 msg = "stripe not found";
849 goto err;
850 }
851
852 v = &bkey_i_to_stripe(&buf->key)->v;
853
854 if (!bch2_ptr_matches_stripe(v, rbio->pick)) {
855 msg = "pointer doesn't match stripe";
856 goto err;
857 }
858
859 offset = rbio->bio.bi_iter.bi_sector - v->ptrs[rbio->pick.ec.block].offset;
860 if (offset + bio_sectors(&rbio->bio) > le16_to_cpu(v->sectors)) {
861 msg = "read is bigger than stripe";
862 goto err;
863 }
864
865 ret = ec_stripe_buf_init(c, buf, offset, bio_sectors(&rbio->bio));
866 if (ret) {
867 msg = "-ENOMEM";
868 goto err;
869 }
870
871 for (i = 0; i < v->nr_blocks; i++)
872 ec_block_io(c, buf, REQ_OP_READ, i, &cl);
873
874 closure_sync(&cl);
875
876 if (ec_nr_failed(buf) > v->nr_redundant) {
877 msg = "unable to read enough blocks";
878 goto err;
879 }
880
881 ec_validate_checksums(c, buf);
882
883 ret = ec_do_recov(c, buf);
884 if (ret)
885 goto err;
886
887 memcpy_to_bio(&rbio->bio, rbio->bio.bi_iter,
888 buf->data[rbio->pick.ec.block] + ((offset - buf->offset) << 9));
889 out:
890 ec_stripe_buf_exit(buf);
891 kfree(buf);
892 return ret;
893 err:
894 bch2_bkey_val_to_text(&msgbuf, c, orig_k);
895 bch_err_ratelimited(c,
896 "error doing reconstruct read: %s\n %s", msg, msgbuf.buf);
897 printbuf_exit(&msgbuf);
898 ret = bch_err_throw(c, stripe_reconstruct);
899 goto out;
900 }
901
902 /* stripe bucket accounting: */
903
__ec_stripe_mem_alloc(struct bch_fs * c,size_t idx,gfp_t gfp)904 static int __ec_stripe_mem_alloc(struct bch_fs *c, size_t idx, gfp_t gfp)
905 {
906 if (c->gc_pos.phase != GC_PHASE_not_running &&
907 !genradix_ptr_alloc(&c->gc_stripes, idx, gfp))
908 return bch_err_throw(c, ENOMEM_ec_stripe_mem_alloc);
909
910 return 0;
911 }
912
ec_stripe_mem_alloc(struct btree_trans * trans,struct btree_iter * iter)913 static int ec_stripe_mem_alloc(struct btree_trans *trans,
914 struct btree_iter *iter)
915 {
916 return allocate_dropping_locks_errcode(trans,
917 __ec_stripe_mem_alloc(trans->c, iter->pos.offset, _gfp));
918 }
919
920 /*
921 * Hash table of open stripes:
922 * Stripes that are being created or modified are kept in a hash table, so that
923 * stripe deletion can skip them.
924 */
925
__bch2_stripe_is_open(struct bch_fs * c,u64 idx)926 static bool __bch2_stripe_is_open(struct bch_fs *c, u64 idx)
927 {
928 unsigned hash = hash_64(idx, ilog2(ARRAY_SIZE(c->ec_stripes_new)));
929 struct ec_stripe_new *s;
930
931 hlist_for_each_entry(s, &c->ec_stripes_new[hash], hash)
932 if (s->idx == idx)
933 return true;
934 return false;
935 }
936
bch2_stripe_is_open(struct bch_fs * c,u64 idx)937 static bool bch2_stripe_is_open(struct bch_fs *c, u64 idx)
938 {
939 bool ret = false;
940
941 spin_lock(&c->ec_stripes_new_lock);
942 ret = __bch2_stripe_is_open(c, idx);
943 spin_unlock(&c->ec_stripes_new_lock);
944
945 return ret;
946 }
947
bch2_try_open_stripe(struct bch_fs * c,struct ec_stripe_new * s,u64 idx)948 static bool bch2_try_open_stripe(struct bch_fs *c,
949 struct ec_stripe_new *s,
950 u64 idx)
951 {
952 bool ret;
953
954 spin_lock(&c->ec_stripes_new_lock);
955 ret = !__bch2_stripe_is_open(c, idx);
956 if (ret) {
957 unsigned hash = hash_64(idx, ilog2(ARRAY_SIZE(c->ec_stripes_new)));
958
959 s->idx = idx;
960 hlist_add_head(&s->hash, &c->ec_stripes_new[hash]);
961 }
962 spin_unlock(&c->ec_stripes_new_lock);
963
964 return ret;
965 }
966
bch2_stripe_close(struct bch_fs * c,struct ec_stripe_new * s)967 static void bch2_stripe_close(struct bch_fs *c, struct ec_stripe_new *s)
968 {
969 BUG_ON(!s->idx);
970
971 spin_lock(&c->ec_stripes_new_lock);
972 hlist_del_init(&s->hash);
973 spin_unlock(&c->ec_stripes_new_lock);
974
975 s->idx = 0;
976 }
977
978 /* stripe deletion */
979
ec_stripe_delete(struct btree_trans * trans,u64 idx)980 static int ec_stripe_delete(struct btree_trans *trans, u64 idx)
981 {
982 struct btree_iter iter;
983 struct bkey_s_c k = bch2_bkey_get_iter(trans, &iter,
984 BTREE_ID_stripes, POS(0, idx),
985 BTREE_ITER_intent);
986 int ret = bkey_err(k);
987 if (ret)
988 goto err;
989
990 /*
991 * We expect write buffer races here
992 * Important: check stripe_is_open with stripe key locked:
993 */
994 if (k.k->type == KEY_TYPE_stripe &&
995 !bch2_stripe_is_open(trans->c, idx) &&
996 stripe_lru_pos(bkey_s_c_to_stripe(k).v) == 1)
997 ret = bch2_btree_delete_at(trans, &iter, 0);
998 err:
999 bch2_trans_iter_exit(trans, &iter);
1000 return ret;
1001 }
1002
1003 /*
1004 * XXX
1005 * can we kill this and delete stripes from the trigger?
1006 */
ec_stripe_delete_work(struct work_struct * work)1007 static void ec_stripe_delete_work(struct work_struct *work)
1008 {
1009 struct bch_fs *c =
1010 container_of(work, struct bch_fs, ec_stripe_delete_work);
1011
1012 bch2_trans_run(c,
1013 bch2_btree_write_buffer_tryflush(trans) ?:
1014 for_each_btree_key_max_commit(trans, lru_iter, BTREE_ID_lru,
1015 lru_pos(BCH_LRU_STRIPE_FRAGMENTATION, 1, 0),
1016 lru_pos(BCH_LRU_STRIPE_FRAGMENTATION, 1, LRU_TIME_MAX),
1017 0, lru_k,
1018 NULL, NULL,
1019 BCH_TRANS_COMMIT_no_enospc, ({
1020 ec_stripe_delete(trans, lru_k.k->p.offset);
1021 })));
1022 enumerated_ref_put(&c->writes, BCH_WRITE_REF_stripe_delete);
1023 }
1024
bch2_do_stripe_deletes(struct bch_fs * c)1025 void bch2_do_stripe_deletes(struct bch_fs *c)
1026 {
1027 if (enumerated_ref_tryget(&c->writes, BCH_WRITE_REF_stripe_delete) &&
1028 !queue_work(c->write_ref_wq, &c->ec_stripe_delete_work))
1029 enumerated_ref_put(&c->writes, BCH_WRITE_REF_stripe_delete);
1030 }
1031
1032 /* stripe creation: */
1033
ec_stripe_key_update(struct btree_trans * trans,struct bkey_i_stripe * old,struct bkey_i_stripe * new)1034 static int ec_stripe_key_update(struct btree_trans *trans,
1035 struct bkey_i_stripe *old,
1036 struct bkey_i_stripe *new)
1037 {
1038 struct bch_fs *c = trans->c;
1039 bool create = !old;
1040
1041 struct btree_iter iter;
1042 struct bkey_s_c k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_stripes,
1043 new->k.p, BTREE_ITER_intent);
1044 int ret = bkey_err(k);
1045 if (ret)
1046 goto err;
1047
1048 if (bch2_fs_inconsistent_on(k.k->type != (create ? KEY_TYPE_deleted : KEY_TYPE_stripe),
1049 c, "error %s stripe: got existing key type %s",
1050 create ? "creating" : "updating",
1051 bch2_bkey_types[k.k->type])) {
1052 ret = -EINVAL;
1053 goto err;
1054 }
1055
1056 if (k.k->type == KEY_TYPE_stripe) {
1057 const struct bch_stripe *v = bkey_s_c_to_stripe(k).v;
1058
1059 BUG_ON(old->v.nr_blocks != new->v.nr_blocks);
1060 BUG_ON(old->v.nr_blocks != v->nr_blocks);
1061
1062 for (unsigned i = 0; i < new->v.nr_blocks; i++) {
1063 unsigned sectors = stripe_blockcount_get(v, i);
1064
1065 if (!bch2_extent_ptr_eq(old->v.ptrs[i], new->v.ptrs[i]) && sectors) {
1066 struct printbuf buf = PRINTBUF;
1067
1068 prt_printf(&buf, "stripe changed nonempty block %u", i);
1069 prt_str(&buf, "\nold: ");
1070 bch2_bkey_val_to_text(&buf, c, k);
1071 prt_str(&buf, "\nnew: ");
1072 bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(&new->k_i));
1073 bch2_fs_inconsistent(c, "%s", buf.buf);
1074 printbuf_exit(&buf);
1075 ret = -EINVAL;
1076 goto err;
1077 }
1078
1079 /*
1080 * If the stripe ptr changed underneath us, it must have
1081 * been dev_remove_stripes() -> * invalidate_stripe_to_dev()
1082 */
1083 if (!bch2_extent_ptr_eq(old->v.ptrs[i], v->ptrs[i])) {
1084 BUG_ON(v->ptrs[i].dev != BCH_SB_MEMBER_INVALID);
1085
1086 if (bch2_extent_ptr_eq(old->v.ptrs[i], new->v.ptrs[i]))
1087 new->v.ptrs[i].dev = BCH_SB_MEMBER_INVALID;
1088 }
1089
1090 stripe_blockcount_set(&new->v, i, sectors);
1091 }
1092 }
1093
1094 ret = bch2_trans_update(trans, &iter, &new->k_i, 0);
1095 err:
1096 bch2_trans_iter_exit(trans, &iter);
1097 return ret;
1098 }
1099
ec_stripe_update_extent(struct btree_trans * trans,struct bch_dev * ca,struct bpos bucket,u8 gen,struct ec_stripe_buf * s,struct bkey_s_c_backpointer bp,struct bkey_buf * last_flushed)1100 static int ec_stripe_update_extent(struct btree_trans *trans,
1101 struct bch_dev *ca,
1102 struct bpos bucket, u8 gen,
1103 struct ec_stripe_buf *s,
1104 struct bkey_s_c_backpointer bp,
1105 struct bkey_buf *last_flushed)
1106 {
1107 struct bch_stripe *v = &bkey_i_to_stripe(&s->key)->v;
1108 struct bch_fs *c = trans->c;
1109 struct btree_iter iter;
1110 struct bkey_s_c k;
1111 const struct bch_extent_ptr *ptr_c;
1112 struct bch_extent_ptr *ec_ptr = NULL;
1113 struct bch_extent_stripe_ptr stripe_ptr;
1114 struct bkey_i *n;
1115 int ret, dev, block;
1116
1117 if (bp.v->level) {
1118 struct printbuf buf = PRINTBUF;
1119 struct btree_iter node_iter;
1120 struct btree *b;
1121
1122 b = bch2_backpointer_get_node(trans, bp, &node_iter, last_flushed);
1123 bch2_trans_iter_exit(trans, &node_iter);
1124
1125 if (!b)
1126 return 0;
1127
1128 prt_printf(&buf, "found btree node in erasure coded bucket: b=%px\n", b);
1129 bch2_bkey_val_to_text(&buf, c, bp.s_c);
1130
1131 bch2_fs_inconsistent(c, "%s", buf.buf);
1132 printbuf_exit(&buf);
1133 return bch_err_throw(c, erasure_coding_found_btree_node);
1134 }
1135
1136 k = bch2_backpointer_get_key(trans, bp, &iter, BTREE_ITER_intent, last_flushed);
1137 ret = bkey_err(k);
1138 if (ret)
1139 return ret;
1140 if (!k.k) {
1141 /*
1142 * extent no longer exists - we could flush the btree
1143 * write buffer and retry to verify, but no need:
1144 */
1145 return 0;
1146 }
1147
1148 if (extent_has_stripe_ptr(k, s->key.k.p.offset))
1149 goto out;
1150
1151 ptr_c = bkey_matches_stripe(v, k, &block);
1152 /*
1153 * It doesn't generally make sense to erasure code cached ptrs:
1154 * XXX: should we be incrementing a counter?
1155 */
1156 if (!ptr_c || ptr_c->cached)
1157 goto out;
1158
1159 dev = v->ptrs[block].dev;
1160
1161 n = bch2_trans_kmalloc(trans, bkey_bytes(k.k) + sizeof(stripe_ptr));
1162 ret = PTR_ERR_OR_ZERO(n);
1163 if (ret)
1164 goto out;
1165
1166 bkey_reassemble(n, k);
1167
1168 bch2_bkey_drop_ptrs_noerror(bkey_i_to_s(n), ptr, ptr->dev != dev);
1169 ec_ptr = bch2_bkey_has_device(bkey_i_to_s(n), dev);
1170 BUG_ON(!ec_ptr);
1171
1172 stripe_ptr = (struct bch_extent_stripe_ptr) {
1173 .type = 1 << BCH_EXTENT_ENTRY_stripe_ptr,
1174 .block = block,
1175 .redundancy = v->nr_redundant,
1176 .idx = s->key.k.p.offset,
1177 };
1178
1179 __extent_entry_insert(n,
1180 (union bch_extent_entry *) ec_ptr,
1181 (union bch_extent_entry *) &stripe_ptr);
1182
1183 ret = bch2_trans_update(trans, &iter, n, 0);
1184 out:
1185 bch2_trans_iter_exit(trans, &iter);
1186 return ret;
1187 }
1188
ec_stripe_update_bucket(struct btree_trans * trans,struct ec_stripe_buf * s,unsigned block)1189 static int ec_stripe_update_bucket(struct btree_trans *trans, struct ec_stripe_buf *s,
1190 unsigned block)
1191 {
1192 struct bch_fs *c = trans->c;
1193 struct bch_stripe *v = &bkey_i_to_stripe(&s->key)->v;
1194 struct bch_extent_ptr ptr = v->ptrs[block];
1195 int ret = 0;
1196
1197 struct bch_dev *ca = bch2_dev_tryget(c, ptr.dev);
1198 if (!ca)
1199 return bch_err_throw(c, ENOENT_dev_not_found);
1200
1201 struct bpos bucket_pos = PTR_BUCKET_POS(ca, &ptr);
1202
1203 struct bkey_buf last_flushed;
1204 bch2_bkey_buf_init(&last_flushed);
1205 bkey_init(&last_flushed.k->k);
1206
1207 ret = for_each_btree_key_max_commit(trans, bp_iter, BTREE_ID_backpointers,
1208 bucket_pos_to_bp_start(ca, bucket_pos),
1209 bucket_pos_to_bp_end(ca, bucket_pos), 0, bp_k,
1210 NULL, NULL,
1211 BCH_TRANS_COMMIT_no_check_rw|
1212 BCH_TRANS_COMMIT_no_enospc, ({
1213 if (bkey_ge(bp_k.k->p, bucket_pos_to_bp(ca, bpos_nosnap_successor(bucket_pos), 0)))
1214 break;
1215
1216 if (bp_k.k->type != KEY_TYPE_backpointer)
1217 continue;
1218
1219 struct bkey_s_c_backpointer bp = bkey_s_c_to_backpointer(bp_k);
1220 if (bp.v->btree_id == BTREE_ID_stripes)
1221 continue;
1222
1223 ec_stripe_update_extent(trans, ca, bucket_pos, ptr.gen, s,
1224 bp, &last_flushed);
1225 }));
1226
1227 bch2_bkey_buf_exit(&last_flushed, c);
1228 bch2_dev_put(ca);
1229 return ret;
1230 }
1231
ec_stripe_update_extents(struct bch_fs * c,struct ec_stripe_buf * s)1232 static int ec_stripe_update_extents(struct bch_fs *c, struct ec_stripe_buf *s)
1233 {
1234 struct btree_trans *trans = bch2_trans_get(c);
1235 struct bch_stripe *v = &bkey_i_to_stripe(&s->key)->v;
1236 unsigned nr_data = v->nr_blocks - v->nr_redundant;
1237
1238 int ret = bch2_btree_write_buffer_flush_sync(trans);
1239 if (ret)
1240 goto err;
1241
1242 for (unsigned i = 0; i < nr_data; i++) {
1243 ret = ec_stripe_update_bucket(trans, s, i);
1244 if (ret)
1245 break;
1246 }
1247 err:
1248 bch2_trans_put(trans);
1249 return ret;
1250 }
1251
zero_out_rest_of_ec_bucket(struct bch_fs * c,struct ec_stripe_new * s,unsigned block,struct open_bucket * ob)1252 static void zero_out_rest_of_ec_bucket(struct bch_fs *c,
1253 struct ec_stripe_new *s,
1254 unsigned block,
1255 struct open_bucket *ob)
1256 {
1257 struct bch_dev *ca = bch2_dev_get_ioref(c, ob->dev, WRITE,
1258 BCH_DEV_WRITE_REF_ec_bucket_zero);
1259 if (!ca) {
1260 s->err = bch_err_throw(c, erofs_no_writes);
1261 return;
1262 }
1263
1264 unsigned offset = ca->mi.bucket_size - ob->sectors_free;
1265 memset(s->new_stripe.data[block] + (offset << 9),
1266 0,
1267 ob->sectors_free << 9);
1268
1269 int ret = blkdev_issue_zeroout(ca->disk_sb.bdev,
1270 ob->bucket * ca->mi.bucket_size + offset,
1271 ob->sectors_free,
1272 GFP_KERNEL, 0);
1273
1274 enumerated_ref_put(&ca->io_ref[WRITE], BCH_DEV_WRITE_REF_ec_bucket_zero);
1275
1276 if (ret)
1277 s->err = ret;
1278 }
1279
bch2_ec_stripe_new_free(struct bch_fs * c,struct ec_stripe_new * s)1280 void bch2_ec_stripe_new_free(struct bch_fs *c, struct ec_stripe_new *s)
1281 {
1282 if (s->idx)
1283 bch2_stripe_close(c, s);
1284 kfree(s);
1285 }
1286
1287 /*
1288 * data buckets of new stripe all written: create the stripe
1289 */
ec_stripe_create(struct ec_stripe_new * s)1290 static void ec_stripe_create(struct ec_stripe_new *s)
1291 {
1292 struct bch_fs *c = s->c;
1293 struct open_bucket *ob;
1294 struct bch_stripe *v = &bkey_i_to_stripe(&s->new_stripe.key)->v;
1295 unsigned i, nr_data = v->nr_blocks - v->nr_redundant;
1296 int ret;
1297
1298 BUG_ON(s->h->s == s);
1299
1300 closure_sync(&s->iodone);
1301
1302 if (!s->err) {
1303 for (i = 0; i < nr_data; i++)
1304 if (s->blocks[i]) {
1305 ob = c->open_buckets + s->blocks[i];
1306
1307 if (ob->sectors_free)
1308 zero_out_rest_of_ec_bucket(c, s, i, ob);
1309 }
1310 }
1311
1312 if (s->err) {
1313 if (!bch2_err_matches(s->err, EROFS))
1314 bch_err(c, "error creating stripe: error writing data buckets");
1315 ret = s->err;
1316 goto err;
1317 }
1318
1319 if (s->have_existing_stripe) {
1320 ec_validate_checksums(c, &s->existing_stripe);
1321
1322 if (ec_do_recov(c, &s->existing_stripe)) {
1323 bch_err(c, "error creating stripe: error reading existing stripe");
1324 ret = bch_err_throw(c, ec_block_read);
1325 goto err;
1326 }
1327
1328 for (i = 0; i < nr_data; i++)
1329 if (stripe_blockcount_get(&bkey_i_to_stripe(&s->existing_stripe.key)->v, i))
1330 swap(s->new_stripe.data[i],
1331 s->existing_stripe.data[i]);
1332
1333 ec_stripe_buf_exit(&s->existing_stripe);
1334 }
1335
1336 BUG_ON(!s->allocated);
1337 BUG_ON(!s->idx);
1338
1339 ec_generate_ec(&s->new_stripe);
1340
1341 ec_generate_checksums(&s->new_stripe);
1342
1343 /* write p/q: */
1344 for (i = nr_data; i < v->nr_blocks; i++)
1345 ec_block_io(c, &s->new_stripe, REQ_OP_WRITE, i, &s->iodone);
1346 closure_sync(&s->iodone);
1347
1348 if (ec_nr_failed(&s->new_stripe)) {
1349 bch_err(c, "error creating stripe: error writing redundancy buckets");
1350 ret = bch_err_throw(c, ec_block_write);
1351 goto err;
1352 }
1353
1354 ret = bch2_trans_commit_do(c, &s->res, NULL,
1355 BCH_TRANS_COMMIT_no_check_rw|
1356 BCH_TRANS_COMMIT_no_enospc,
1357 ec_stripe_key_update(trans,
1358 s->have_existing_stripe
1359 ? bkey_i_to_stripe(&s->existing_stripe.key)
1360 : NULL,
1361 bkey_i_to_stripe(&s->new_stripe.key)));
1362 bch_err_msg(c, ret, "creating stripe key");
1363 if (ret) {
1364 goto err;
1365 }
1366
1367 ret = ec_stripe_update_extents(c, &s->new_stripe);
1368 bch_err_msg(c, ret, "error updating extents");
1369 if (ret)
1370 goto err;
1371 err:
1372 trace_stripe_create(c, s->idx, ret);
1373
1374 bch2_disk_reservation_put(c, &s->res);
1375
1376 for (i = 0; i < v->nr_blocks; i++)
1377 if (s->blocks[i]) {
1378 ob = c->open_buckets + s->blocks[i];
1379
1380 if (i < nr_data) {
1381 ob->ec = NULL;
1382 __bch2_open_bucket_put(c, ob);
1383 } else {
1384 bch2_open_bucket_put(c, ob);
1385 }
1386 }
1387
1388 mutex_lock(&c->ec_stripe_new_lock);
1389 list_del(&s->list);
1390 mutex_unlock(&c->ec_stripe_new_lock);
1391 wake_up(&c->ec_stripe_new_wait);
1392
1393 ec_stripe_buf_exit(&s->existing_stripe);
1394 ec_stripe_buf_exit(&s->new_stripe);
1395 closure_debug_destroy(&s->iodone);
1396
1397 ec_stripe_new_put(c, s, STRIPE_REF_stripe);
1398 }
1399
get_pending_stripe(struct bch_fs * c)1400 static struct ec_stripe_new *get_pending_stripe(struct bch_fs *c)
1401 {
1402 struct ec_stripe_new *s;
1403
1404 mutex_lock(&c->ec_stripe_new_lock);
1405 list_for_each_entry(s, &c->ec_stripe_new_list, list)
1406 if (!atomic_read(&s->ref[STRIPE_REF_io]))
1407 goto out;
1408 s = NULL;
1409 out:
1410 mutex_unlock(&c->ec_stripe_new_lock);
1411
1412 return s;
1413 }
1414
ec_stripe_create_work(struct work_struct * work)1415 static void ec_stripe_create_work(struct work_struct *work)
1416 {
1417 struct bch_fs *c = container_of(work,
1418 struct bch_fs, ec_stripe_create_work);
1419 struct ec_stripe_new *s;
1420
1421 while ((s = get_pending_stripe(c)))
1422 ec_stripe_create(s);
1423
1424 enumerated_ref_put(&c->writes, BCH_WRITE_REF_stripe_create);
1425 }
1426
bch2_ec_do_stripe_creates(struct bch_fs * c)1427 void bch2_ec_do_stripe_creates(struct bch_fs *c)
1428 {
1429 enumerated_ref_get(&c->writes, BCH_WRITE_REF_stripe_create);
1430
1431 if (!queue_work(system_long_wq, &c->ec_stripe_create_work))
1432 enumerated_ref_put(&c->writes, BCH_WRITE_REF_stripe_create);
1433 }
1434
ec_stripe_new_set_pending(struct bch_fs * c,struct ec_stripe_head * h)1435 static void ec_stripe_new_set_pending(struct bch_fs *c, struct ec_stripe_head *h)
1436 {
1437 struct ec_stripe_new *s = h->s;
1438
1439 lockdep_assert_held(&h->lock);
1440
1441 BUG_ON(!s->allocated && !s->err);
1442
1443 h->s = NULL;
1444 s->pending = true;
1445
1446 mutex_lock(&c->ec_stripe_new_lock);
1447 list_add(&s->list, &c->ec_stripe_new_list);
1448 mutex_unlock(&c->ec_stripe_new_lock);
1449
1450 ec_stripe_new_put(c, s, STRIPE_REF_io);
1451 }
1452
ec_stripe_new_cancel(struct bch_fs * c,struct ec_stripe_head * h,int err)1453 static void ec_stripe_new_cancel(struct bch_fs *c, struct ec_stripe_head *h, int err)
1454 {
1455 h->s->err = err;
1456 ec_stripe_new_set_pending(c, h);
1457 }
1458
bch2_ec_bucket_cancel(struct bch_fs * c,struct open_bucket * ob,int err)1459 void bch2_ec_bucket_cancel(struct bch_fs *c, struct open_bucket *ob, int err)
1460 {
1461 struct ec_stripe_new *s = ob->ec;
1462
1463 s->err = err;
1464 }
1465
bch2_writepoint_ec_buf(struct bch_fs * c,struct write_point * wp)1466 void *bch2_writepoint_ec_buf(struct bch_fs *c, struct write_point *wp)
1467 {
1468 struct open_bucket *ob = ec_open_bucket(c, &wp->ptrs);
1469 if (!ob)
1470 return NULL;
1471
1472 BUG_ON(!ob->ec->new_stripe.data[ob->ec_idx]);
1473
1474 struct bch_dev *ca = ob_dev(c, ob);
1475 unsigned offset = ca->mi.bucket_size - ob->sectors_free;
1476
1477 return ob->ec->new_stripe.data[ob->ec_idx] + (offset << 9);
1478 }
1479
unsigned_cmp(const void * _l,const void * _r)1480 static int unsigned_cmp(const void *_l, const void *_r)
1481 {
1482 unsigned l = *((const unsigned *) _l);
1483 unsigned r = *((const unsigned *) _r);
1484
1485 return cmp_int(l, r);
1486 }
1487
1488 /* pick most common bucket size: */
pick_blocksize(struct bch_fs * c,struct bch_devs_mask * devs)1489 static unsigned pick_blocksize(struct bch_fs *c,
1490 struct bch_devs_mask *devs)
1491 {
1492 unsigned nr = 0, sizes[BCH_SB_MEMBERS_MAX];
1493 struct {
1494 unsigned nr, size;
1495 } cur = { 0, 0 }, best = { 0, 0 };
1496
1497 for_each_member_device_rcu(c, ca, devs)
1498 sizes[nr++] = ca->mi.bucket_size;
1499
1500 sort(sizes, nr, sizeof(unsigned), unsigned_cmp, NULL);
1501
1502 for (unsigned i = 0; i < nr; i++) {
1503 if (sizes[i] != cur.size) {
1504 if (cur.nr > best.nr)
1505 best = cur;
1506
1507 cur.nr = 0;
1508 cur.size = sizes[i];
1509 }
1510
1511 cur.nr++;
1512 }
1513
1514 if (cur.nr > best.nr)
1515 best = cur;
1516
1517 return best.size;
1518 }
1519
may_create_new_stripe(struct bch_fs * c)1520 static bool may_create_new_stripe(struct bch_fs *c)
1521 {
1522 return false;
1523 }
1524
ec_stripe_key_init(struct bch_fs * c,struct bkey_i * k,unsigned nr_data,unsigned nr_parity,unsigned stripe_size,unsigned disk_label)1525 static void ec_stripe_key_init(struct bch_fs *c,
1526 struct bkey_i *k,
1527 unsigned nr_data,
1528 unsigned nr_parity,
1529 unsigned stripe_size,
1530 unsigned disk_label)
1531 {
1532 struct bkey_i_stripe *s = bkey_stripe_init(k);
1533 unsigned u64s;
1534
1535 s->v.sectors = cpu_to_le16(stripe_size);
1536 s->v.algorithm = 0;
1537 s->v.nr_blocks = nr_data + nr_parity;
1538 s->v.nr_redundant = nr_parity;
1539 s->v.csum_granularity_bits = ilog2(c->opts.encoded_extent_max >> 9);
1540 s->v.csum_type = BCH_CSUM_crc32c;
1541 s->v.disk_label = disk_label;
1542
1543 while ((u64s = stripe_val_u64s(&s->v)) > BKEY_VAL_U64s_MAX) {
1544 BUG_ON(1 << s->v.csum_granularity_bits >=
1545 le16_to_cpu(s->v.sectors) ||
1546 s->v.csum_granularity_bits == U8_MAX);
1547 s->v.csum_granularity_bits++;
1548 }
1549
1550 set_bkey_val_u64s(&s->k, u64s);
1551 }
1552
ec_new_stripe_alloc(struct bch_fs * c,struct ec_stripe_head * h)1553 static struct ec_stripe_new *ec_new_stripe_alloc(struct bch_fs *c, struct ec_stripe_head *h)
1554 {
1555 struct ec_stripe_new *s;
1556
1557 lockdep_assert_held(&h->lock);
1558
1559 s = kzalloc(sizeof(*s), GFP_KERNEL);
1560 if (!s)
1561 return NULL;
1562
1563 mutex_init(&s->lock);
1564 closure_init(&s->iodone, NULL);
1565 atomic_set(&s->ref[STRIPE_REF_stripe], 1);
1566 atomic_set(&s->ref[STRIPE_REF_io], 1);
1567 s->c = c;
1568 s->h = h;
1569 s->nr_data = min_t(unsigned, h->nr_active_devs,
1570 BCH_BKEY_PTRS_MAX) - h->redundancy;
1571 s->nr_parity = h->redundancy;
1572
1573 ec_stripe_key_init(c, &s->new_stripe.key,
1574 s->nr_data, s->nr_parity,
1575 h->blocksize, h->disk_label);
1576 return s;
1577 }
1578
ec_stripe_head_devs_update(struct bch_fs * c,struct ec_stripe_head * h)1579 static void ec_stripe_head_devs_update(struct bch_fs *c, struct ec_stripe_head *h)
1580 {
1581 struct bch_devs_mask devs = h->devs;
1582 unsigned nr_devs, nr_devs_with_durability;
1583
1584 scoped_guard(rcu) {
1585 h->devs = target_rw_devs(c, BCH_DATA_user, h->disk_label
1586 ? group_to_target(h->disk_label - 1)
1587 : 0);
1588 nr_devs = dev_mask_nr(&h->devs);
1589
1590 for_each_member_device_rcu(c, ca, &h->devs)
1591 if (!ca->mi.durability)
1592 __clear_bit(ca->dev_idx, h->devs.d);
1593 nr_devs_with_durability = dev_mask_nr(&h->devs);
1594
1595 h->blocksize = pick_blocksize(c, &h->devs);
1596
1597 h->nr_active_devs = 0;
1598 for_each_member_device_rcu(c, ca, &h->devs)
1599 if (ca->mi.bucket_size == h->blocksize)
1600 h->nr_active_devs++;
1601 }
1602
1603 /*
1604 * If we only have redundancy + 1 devices, we're better off with just
1605 * replication:
1606 */
1607 h->insufficient_devs = h->nr_active_devs < h->redundancy + 2;
1608
1609 if (h->insufficient_devs) {
1610 const char *err;
1611
1612 if (nr_devs < h->redundancy + 2)
1613 err = NULL;
1614 else if (nr_devs_with_durability < h->redundancy + 2)
1615 err = "cannot use durability=0 devices";
1616 else
1617 err = "mismatched bucket sizes";
1618
1619 if (err)
1620 bch_err(c, "insufficient devices available to create stripe (have %u, need %u): %s",
1621 h->nr_active_devs, h->redundancy + 2, err);
1622 }
1623
1624 struct bch_devs_mask devs_leaving;
1625 bitmap_andnot(devs_leaving.d, devs.d, h->devs.d, BCH_SB_MEMBERS_MAX);
1626
1627 if (h->s && !h->s->allocated && dev_mask_nr(&devs_leaving))
1628 ec_stripe_new_cancel(c, h, -EINTR);
1629
1630 h->rw_devs_change_count = c->rw_devs_change_count;
1631 }
1632
1633 static struct ec_stripe_head *
ec_new_stripe_head_alloc(struct bch_fs * c,unsigned disk_label,unsigned algo,unsigned redundancy,enum bch_watermark watermark)1634 ec_new_stripe_head_alloc(struct bch_fs *c, unsigned disk_label,
1635 unsigned algo, unsigned redundancy,
1636 enum bch_watermark watermark)
1637 {
1638 struct ec_stripe_head *h;
1639
1640 h = kzalloc(sizeof(*h), GFP_KERNEL);
1641 if (!h)
1642 return NULL;
1643
1644 mutex_init(&h->lock);
1645 BUG_ON(!mutex_trylock(&h->lock));
1646
1647 h->disk_label = disk_label;
1648 h->algo = algo;
1649 h->redundancy = redundancy;
1650 h->watermark = watermark;
1651
1652 list_add(&h->list, &c->ec_stripe_head_list);
1653 return h;
1654 }
1655
bch2_ec_stripe_head_put(struct bch_fs * c,struct ec_stripe_head * h)1656 void bch2_ec_stripe_head_put(struct bch_fs *c, struct ec_stripe_head *h)
1657 {
1658 if (h->s &&
1659 h->s->allocated &&
1660 bitmap_weight(h->s->blocks_allocated,
1661 h->s->nr_data) == h->s->nr_data)
1662 ec_stripe_new_set_pending(c, h);
1663
1664 mutex_unlock(&h->lock);
1665 }
1666
1667 static struct ec_stripe_head *
__bch2_ec_stripe_head_get(struct btree_trans * trans,unsigned disk_label,unsigned algo,unsigned redundancy,enum bch_watermark watermark)1668 __bch2_ec_stripe_head_get(struct btree_trans *trans,
1669 unsigned disk_label,
1670 unsigned algo,
1671 unsigned redundancy,
1672 enum bch_watermark watermark)
1673 {
1674 struct bch_fs *c = trans->c;
1675 struct ec_stripe_head *h;
1676 int ret;
1677
1678 if (!redundancy)
1679 return NULL;
1680
1681 ret = bch2_trans_mutex_lock(trans, &c->ec_stripe_head_lock);
1682 if (ret)
1683 return ERR_PTR(ret);
1684
1685 if (test_bit(BCH_FS_going_ro, &c->flags)) {
1686 h = ERR_PTR(-BCH_ERR_erofs_no_writes);
1687 goto err;
1688 }
1689
1690 list_for_each_entry(h, &c->ec_stripe_head_list, list)
1691 if (h->disk_label == disk_label &&
1692 h->algo == algo &&
1693 h->redundancy == redundancy &&
1694 h->watermark == watermark) {
1695 ret = bch2_trans_mutex_lock(trans, &h->lock);
1696 if (ret) {
1697 h = ERR_PTR(ret);
1698 goto err;
1699 }
1700 goto found;
1701 }
1702
1703 h = ec_new_stripe_head_alloc(c, disk_label, algo, redundancy, watermark);
1704 if (!h) {
1705 h = ERR_PTR(-BCH_ERR_ENOMEM_stripe_head_alloc);
1706 goto err;
1707 }
1708 found:
1709 if (h->rw_devs_change_count != c->rw_devs_change_count)
1710 ec_stripe_head_devs_update(c, h);
1711
1712 if (h->insufficient_devs) {
1713 mutex_unlock(&h->lock);
1714 h = NULL;
1715 }
1716 err:
1717 mutex_unlock(&c->ec_stripe_head_lock);
1718 return h;
1719 }
1720
new_stripe_alloc_buckets(struct btree_trans * trans,struct alloc_request * req,struct ec_stripe_head * h,struct ec_stripe_new * s,struct closure * cl)1721 static int new_stripe_alloc_buckets(struct btree_trans *trans,
1722 struct alloc_request *req,
1723 struct ec_stripe_head *h, struct ec_stripe_new *s,
1724 struct closure *cl)
1725 {
1726 struct bch_fs *c = trans->c;
1727 struct open_bucket *ob;
1728 struct bch_stripe *v = &bkey_i_to_stripe(&s->new_stripe.key)->v;
1729 unsigned i, j, nr_have_parity = 0, nr_have_data = 0;
1730 int ret = 0;
1731
1732 req->scratch_data_type = req->data_type;
1733 req->scratch_ptrs = req->ptrs;
1734 req->scratch_nr_replicas = req->nr_replicas;
1735 req->scratch_nr_effective = req->nr_effective;
1736 req->scratch_have_cache = req->have_cache;
1737 req->scratch_devs_may_alloc = req->devs_may_alloc;
1738
1739 req->devs_may_alloc = h->devs;
1740 req->have_cache = true;
1741
1742 BUG_ON(v->nr_blocks != s->nr_data + s->nr_parity);
1743 BUG_ON(v->nr_redundant != s->nr_parity);
1744
1745 /* * We bypass the sector allocator which normally does this: */
1746 bitmap_and(req->devs_may_alloc.d, req->devs_may_alloc.d,
1747 c->rw_devs[BCH_DATA_user].d, BCH_SB_MEMBERS_MAX);
1748
1749 for_each_set_bit(i, s->blocks_gotten, v->nr_blocks) {
1750 /*
1751 * Note: we don't yet repair invalid blocks (failed/removed
1752 * devices) when reusing stripes - we still need a codepath to
1753 * walk backpointers and update all extents that point to that
1754 * block when updating the stripe
1755 */
1756 if (v->ptrs[i].dev != BCH_SB_MEMBER_INVALID)
1757 __clear_bit(v->ptrs[i].dev, req->devs_may_alloc.d);
1758
1759 if (i < s->nr_data)
1760 nr_have_data++;
1761 else
1762 nr_have_parity++;
1763 }
1764
1765 BUG_ON(nr_have_data > s->nr_data);
1766 BUG_ON(nr_have_parity > s->nr_parity);
1767
1768 req->ptrs.nr = 0;
1769 if (nr_have_parity < s->nr_parity) {
1770 req->nr_replicas = s->nr_parity;
1771 req->nr_effective = nr_have_parity;
1772 req->data_type = BCH_DATA_parity;
1773
1774 ret = bch2_bucket_alloc_set_trans(trans, req, &h->parity_stripe, cl);
1775
1776 open_bucket_for_each(c, &req->ptrs, ob, i) {
1777 j = find_next_zero_bit(s->blocks_gotten,
1778 s->nr_data + s->nr_parity,
1779 s->nr_data);
1780 BUG_ON(j >= s->nr_data + s->nr_parity);
1781
1782 s->blocks[j] = req->ptrs.v[i];
1783 v->ptrs[j] = bch2_ob_ptr(c, ob);
1784 __set_bit(j, s->blocks_gotten);
1785 }
1786
1787 if (ret)
1788 goto err;
1789 }
1790
1791 req->ptrs.nr = 0;
1792 if (nr_have_data < s->nr_data) {
1793 req->nr_replicas = s->nr_data;
1794 req->nr_effective = nr_have_data;
1795 req->data_type = BCH_DATA_user;
1796
1797 ret = bch2_bucket_alloc_set_trans(trans, req, &h->block_stripe, cl);
1798
1799 open_bucket_for_each(c, &req->ptrs, ob, i) {
1800 j = find_next_zero_bit(s->blocks_gotten,
1801 s->nr_data, 0);
1802 BUG_ON(j >= s->nr_data);
1803
1804 s->blocks[j] = req->ptrs.v[i];
1805 v->ptrs[j] = bch2_ob_ptr(c, ob);
1806 __set_bit(j, s->blocks_gotten);
1807 }
1808
1809 if (ret)
1810 goto err;
1811 }
1812 err:
1813 req->data_type = req->scratch_data_type;
1814 req->ptrs = req->scratch_ptrs;
1815 req->nr_replicas = req->scratch_nr_replicas;
1816 req->nr_effective = req->scratch_nr_effective;
1817 req->have_cache = req->scratch_have_cache;
1818 req->devs_may_alloc = req->scratch_devs_may_alloc;
1819 return ret;
1820 }
1821
__get_existing_stripe(struct btree_trans * trans,struct ec_stripe_head * head,struct ec_stripe_buf * stripe,u64 idx)1822 static int __get_existing_stripe(struct btree_trans *trans,
1823 struct ec_stripe_head *head,
1824 struct ec_stripe_buf *stripe,
1825 u64 idx)
1826 {
1827 struct bch_fs *c = trans->c;
1828
1829 struct btree_iter iter;
1830 struct bkey_s_c k = bch2_bkey_get_iter(trans, &iter,
1831 BTREE_ID_stripes, POS(0, idx), 0);
1832 int ret = bkey_err(k);
1833 if (ret)
1834 goto err;
1835
1836 /* We expect write buffer races here */
1837 if (k.k->type != KEY_TYPE_stripe)
1838 goto out;
1839
1840 struct bkey_s_c_stripe s = bkey_s_c_to_stripe(k);
1841 if (stripe_lru_pos(s.v) <= 1)
1842 goto out;
1843
1844 if (s.v->disk_label == head->disk_label &&
1845 s.v->algorithm == head->algo &&
1846 s.v->nr_redundant == head->redundancy &&
1847 le16_to_cpu(s.v->sectors) == head->blocksize &&
1848 bch2_try_open_stripe(c, head->s, idx)) {
1849 bkey_reassemble(&stripe->key, k);
1850 ret = 1;
1851 }
1852 out:
1853 bch2_set_btree_iter_dontneed(trans, &iter);
1854 err:
1855 bch2_trans_iter_exit(trans, &iter);
1856 return ret;
1857 }
1858
init_new_stripe_from_existing(struct bch_fs * c,struct ec_stripe_new * s)1859 static int init_new_stripe_from_existing(struct bch_fs *c, struct ec_stripe_new *s)
1860 {
1861 struct bch_stripe *new_v = &bkey_i_to_stripe(&s->new_stripe.key)->v;
1862 struct bch_stripe *existing_v = &bkey_i_to_stripe(&s->existing_stripe.key)->v;
1863 unsigned i;
1864
1865 BUG_ON(existing_v->nr_redundant != s->nr_parity);
1866 s->nr_data = existing_v->nr_blocks -
1867 existing_v->nr_redundant;
1868
1869 int ret = ec_stripe_buf_init(c, &s->existing_stripe, 0, le16_to_cpu(existing_v->sectors));
1870 if (ret) {
1871 bch2_stripe_close(c, s);
1872 return ret;
1873 }
1874
1875 BUG_ON(s->existing_stripe.size != le16_to_cpu(existing_v->sectors));
1876
1877 /*
1878 * Free buckets we initially allocated - they might conflict with
1879 * blocks from the stripe we're reusing:
1880 */
1881 for_each_set_bit(i, s->blocks_gotten, new_v->nr_blocks) {
1882 bch2_open_bucket_put(c, c->open_buckets + s->blocks[i]);
1883 s->blocks[i] = 0;
1884 }
1885 memset(s->blocks_gotten, 0, sizeof(s->blocks_gotten));
1886 memset(s->blocks_allocated, 0, sizeof(s->blocks_allocated));
1887
1888 for (unsigned i = 0; i < existing_v->nr_blocks; i++) {
1889 if (stripe_blockcount_get(existing_v, i)) {
1890 __set_bit(i, s->blocks_gotten);
1891 __set_bit(i, s->blocks_allocated);
1892 }
1893
1894 ec_block_io(c, &s->existing_stripe, READ, i, &s->iodone);
1895 }
1896
1897 bkey_copy(&s->new_stripe.key, &s->existing_stripe.key);
1898 s->have_existing_stripe = true;
1899
1900 return 0;
1901 }
1902
__bch2_ec_stripe_head_reuse(struct btree_trans * trans,struct ec_stripe_head * h,struct ec_stripe_new * s)1903 static int __bch2_ec_stripe_head_reuse(struct btree_trans *trans, struct ec_stripe_head *h,
1904 struct ec_stripe_new *s)
1905 {
1906 struct bch_fs *c = trans->c;
1907
1908 /*
1909 * If we can't allocate a new stripe, and there's no stripes with empty
1910 * blocks for us to reuse, that means we have to wait on copygc:
1911 */
1912 if (may_create_new_stripe(c))
1913 return -1;
1914
1915 struct btree_iter lru_iter;
1916 struct bkey_s_c lru_k;
1917 int ret = 0;
1918
1919 for_each_btree_key_max_norestart(trans, lru_iter, BTREE_ID_lru,
1920 lru_pos(BCH_LRU_STRIPE_FRAGMENTATION, 2, 0),
1921 lru_pos(BCH_LRU_STRIPE_FRAGMENTATION, 2, LRU_TIME_MAX),
1922 0, lru_k, ret) {
1923 ret = __get_existing_stripe(trans, h, &s->existing_stripe, lru_k.k->p.offset);
1924 if (ret)
1925 break;
1926 }
1927 bch2_trans_iter_exit(trans, &lru_iter);
1928 if (!ret)
1929 ret = bch_err_throw(c, stripe_alloc_blocked);
1930 if (ret == 1)
1931 ret = 0;
1932 if (ret)
1933 return ret;
1934
1935 return init_new_stripe_from_existing(c, s);
1936 }
1937
__bch2_ec_stripe_head_reserve(struct btree_trans * trans,struct ec_stripe_head * h,struct ec_stripe_new * s)1938 static int __bch2_ec_stripe_head_reserve(struct btree_trans *trans, struct ec_stripe_head *h,
1939 struct ec_stripe_new *s)
1940 {
1941 struct bch_fs *c = trans->c;
1942 struct btree_iter iter;
1943 struct bkey_s_c k;
1944 struct bpos min_pos = POS(0, 1);
1945 struct bpos start_pos = bpos_max(min_pos, POS(0, c->ec_stripe_hint));
1946 int ret;
1947
1948 if (!s->res.sectors) {
1949 ret = bch2_disk_reservation_get(c, &s->res,
1950 h->blocksize,
1951 s->nr_parity,
1952 BCH_DISK_RESERVATION_NOFAIL);
1953 if (ret)
1954 return ret;
1955 }
1956
1957 /*
1958 * Allocate stripe slot
1959 * XXX: we're going to need a bitrange btree of free stripes
1960 */
1961 for_each_btree_key_norestart(trans, iter, BTREE_ID_stripes, start_pos,
1962 BTREE_ITER_slots|BTREE_ITER_intent, k, ret) {
1963 if (bkey_gt(k.k->p, POS(0, U32_MAX))) {
1964 if (start_pos.offset) {
1965 start_pos = min_pos;
1966 bch2_btree_iter_set_pos(trans, &iter, start_pos);
1967 continue;
1968 }
1969
1970 ret = bch_err_throw(c, ENOSPC_stripe_create);
1971 break;
1972 }
1973
1974 if (bkey_deleted(k.k) &&
1975 bch2_try_open_stripe(c, s, k.k->p.offset))
1976 break;
1977 }
1978
1979 c->ec_stripe_hint = iter.pos.offset;
1980
1981 if (ret)
1982 goto err;
1983
1984 ret = ec_stripe_mem_alloc(trans, &iter);
1985 if (ret) {
1986 bch2_stripe_close(c, s);
1987 goto err;
1988 }
1989
1990 s->new_stripe.key.k.p = iter.pos;
1991 out:
1992 bch2_trans_iter_exit(trans, &iter);
1993 return ret;
1994 err:
1995 bch2_disk_reservation_put(c, &s->res);
1996 goto out;
1997 }
1998
bch2_ec_stripe_head_get(struct btree_trans * trans,struct alloc_request * req,unsigned algo,struct closure * cl)1999 struct ec_stripe_head *bch2_ec_stripe_head_get(struct btree_trans *trans,
2000 struct alloc_request *req,
2001 unsigned algo,
2002 struct closure *cl)
2003 {
2004 struct bch_fs *c = trans->c;
2005 unsigned redundancy = req->nr_replicas - 1;
2006 unsigned disk_label = 0;
2007 struct target t = target_decode(req->target);
2008 bool waiting = false;
2009 int ret;
2010
2011 if (t.type == TARGET_GROUP) {
2012 if (t.group > U8_MAX) {
2013 bch_err(c, "cannot create a stripe when disk_label > U8_MAX");
2014 return NULL;
2015 }
2016 disk_label = t.group + 1; /* 0 == no label */
2017 }
2018
2019 struct ec_stripe_head *h =
2020 __bch2_ec_stripe_head_get(trans, disk_label, algo,
2021 redundancy, req->watermark);
2022 if (IS_ERR_OR_NULL(h))
2023 return h;
2024
2025 if (!h->s) {
2026 h->s = ec_new_stripe_alloc(c, h);
2027 if (!h->s) {
2028 ret = bch_err_throw(c, ENOMEM_ec_new_stripe_alloc);
2029 bch_err(c, "failed to allocate new stripe");
2030 goto err;
2031 }
2032
2033 h->nr_created++;
2034 }
2035
2036 struct ec_stripe_new *s = h->s;
2037
2038 if (s->allocated)
2039 goto allocated;
2040
2041 if (s->have_existing_stripe)
2042 goto alloc_existing;
2043
2044 /* First, try to allocate a full stripe: */
2045 enum bch_watermark saved_watermark = BCH_WATERMARK_stripe;
2046 swap(req->watermark, saved_watermark);
2047 ret = new_stripe_alloc_buckets(trans, req, h, s, NULL) ?:
2048 __bch2_ec_stripe_head_reserve(trans, h, s);
2049 swap(req->watermark, saved_watermark);
2050
2051 if (!ret)
2052 goto allocate_buf;
2053 if (bch2_err_matches(ret, BCH_ERR_transaction_restart) ||
2054 bch2_err_matches(ret, ENOMEM))
2055 goto err;
2056
2057 /*
2058 * Not enough buckets available for a full stripe: we must reuse an
2059 * existing stripe:
2060 */
2061 while (1) {
2062 ret = __bch2_ec_stripe_head_reuse(trans, h, s);
2063 if (!ret)
2064 break;
2065 if (waiting || !cl || ret != -BCH_ERR_stripe_alloc_blocked)
2066 goto err;
2067
2068 if (req->watermark == BCH_WATERMARK_copygc) {
2069 ret = new_stripe_alloc_buckets(trans, req, h, s, NULL) ?:
2070 __bch2_ec_stripe_head_reserve(trans, h, s);
2071 if (ret)
2072 goto err;
2073 goto allocate_buf;
2074 }
2075
2076 /* XXX freelist_wait? */
2077 closure_wait(&c->freelist_wait, cl);
2078 waiting = true;
2079 }
2080
2081 if (waiting)
2082 closure_wake_up(&c->freelist_wait);
2083 alloc_existing:
2084 /*
2085 * Retry allocating buckets, with the watermark for this
2086 * particular write:
2087 */
2088 ret = new_stripe_alloc_buckets(trans, req, h, s, cl);
2089 if (ret)
2090 goto err;
2091
2092 allocate_buf:
2093 ret = ec_stripe_buf_init(c, &s->new_stripe, 0, h->blocksize);
2094 if (ret)
2095 goto err;
2096
2097 s->allocated = true;
2098 allocated:
2099 BUG_ON(!s->idx);
2100 BUG_ON(!s->new_stripe.data[0]);
2101 BUG_ON(trans->restarted);
2102 return h;
2103 err:
2104 bch2_ec_stripe_head_put(c, h);
2105 return ERR_PTR(ret);
2106 }
2107
2108 /* device removal */
2109
bch2_invalidate_stripe_to_dev(struct btree_trans * trans,struct btree_iter * iter,struct bkey_s_c k,unsigned dev_idx,unsigned flags)2110 int bch2_invalidate_stripe_to_dev(struct btree_trans *trans,
2111 struct btree_iter *iter,
2112 struct bkey_s_c k,
2113 unsigned dev_idx,
2114 unsigned flags)
2115 {
2116 if (k.k->type != KEY_TYPE_stripe)
2117 return 0;
2118
2119 struct bch_fs *c = trans->c;
2120 struct bkey_i_stripe *s =
2121 bch2_bkey_make_mut_typed(trans, iter, &k, 0, stripe);
2122 int ret = PTR_ERR_OR_ZERO(s);
2123 if (ret)
2124 return ret;
2125
2126 struct disk_accounting_pos acc;
2127
2128 s64 sectors = 0;
2129 for (unsigned i = 0; i < s->v.nr_blocks; i++)
2130 sectors -= stripe_blockcount_get(&s->v, i);
2131
2132 memset(&acc, 0, sizeof(acc));
2133 acc.type = BCH_DISK_ACCOUNTING_replicas;
2134 bch2_bkey_to_replicas(&acc.replicas, bkey_i_to_s_c(&s->k_i));
2135 acc.replicas.data_type = BCH_DATA_user;
2136 ret = bch2_disk_accounting_mod(trans, &acc, §ors, 1, false);
2137 if (ret)
2138 return ret;
2139
2140 struct bkey_ptrs ptrs = bch2_bkey_ptrs(bkey_i_to_s(&s->k_i));
2141
2142 /* XXX: how much redundancy do we still have? check degraded flags */
2143
2144 unsigned nr_good = 0;
2145
2146 scoped_guard(rcu)
2147 bkey_for_each_ptr(ptrs, ptr) {
2148 if (ptr->dev == dev_idx)
2149 ptr->dev = BCH_SB_MEMBER_INVALID;
2150
2151 struct bch_dev *ca = bch2_dev_rcu(c, ptr->dev);
2152 nr_good += ca && ca->mi.state != BCH_MEMBER_STATE_failed;
2153 }
2154
2155 if (nr_good < s->v.nr_blocks && !(flags & BCH_FORCE_IF_DATA_DEGRADED))
2156 return bch_err_throw(c, remove_would_lose_data);
2157
2158 unsigned nr_data = s->v.nr_blocks - s->v.nr_redundant;
2159
2160 if (nr_good < nr_data && !(flags & BCH_FORCE_IF_DATA_LOST))
2161 return bch_err_throw(c, remove_would_lose_data);
2162
2163 sectors = -sectors;
2164
2165 memset(&acc, 0, sizeof(acc));
2166 acc.type = BCH_DISK_ACCOUNTING_replicas;
2167 bch2_bkey_to_replicas(&acc.replicas, bkey_i_to_s_c(&s->k_i));
2168 acc.replicas.data_type = BCH_DATA_user;
2169 return bch2_disk_accounting_mod(trans, &acc, §ors, 1, false);
2170 }
2171
bch2_invalidate_stripe_to_dev_from_alloc(struct btree_trans * trans,struct bkey_s_c k_a,unsigned flags)2172 static int bch2_invalidate_stripe_to_dev_from_alloc(struct btree_trans *trans, struct bkey_s_c k_a,
2173 unsigned flags)
2174 {
2175 struct bch_alloc_v4 a_convert;
2176 const struct bch_alloc_v4 *a = bch2_alloc_to_v4(k_a, &a_convert);
2177
2178 if (!a->stripe)
2179 return 0;
2180
2181 if (a->stripe_sectors) {
2182 struct bch_fs *c = trans->c;
2183 bch_err(c, "trying to invalidate device in stripe when bucket has stripe data");
2184 return bch_err_throw(c, invalidate_stripe_to_dev);
2185 }
2186
2187 struct btree_iter iter;
2188 struct bkey_s_c_stripe s =
2189 bch2_bkey_get_iter_typed(trans, &iter, BTREE_ID_stripes, POS(0, a->stripe),
2190 BTREE_ITER_slots, stripe);
2191 int ret = bkey_err(s);
2192 if (ret)
2193 return ret;
2194
2195 ret = bch2_invalidate_stripe_to_dev(trans, &iter, s.s_c, k_a.k->p.inode, flags);
2196 bch2_trans_iter_exit(trans, &iter);
2197 return ret;
2198 }
2199
bch2_dev_remove_stripes(struct bch_fs * c,unsigned dev_idx,unsigned flags)2200 int bch2_dev_remove_stripes(struct bch_fs *c, unsigned dev_idx, unsigned flags)
2201 {
2202 int ret = bch2_trans_run(c,
2203 for_each_btree_key_max_commit(trans, iter,
2204 BTREE_ID_alloc, POS(dev_idx, 0), POS(dev_idx, U64_MAX),
2205 BTREE_ITER_intent, k,
2206 NULL, NULL, 0, ({
2207 bch2_invalidate_stripe_to_dev_from_alloc(trans, k, flags);
2208 })));
2209 bch_err_fn(c, ret);
2210 return ret;
2211 }
2212
2213 /* startup/shutdown */
2214
__bch2_ec_stop(struct bch_fs * c,struct bch_dev * ca)2215 static void __bch2_ec_stop(struct bch_fs *c, struct bch_dev *ca)
2216 {
2217 struct ec_stripe_head *h;
2218 struct open_bucket *ob;
2219 unsigned i;
2220
2221 mutex_lock(&c->ec_stripe_head_lock);
2222 list_for_each_entry(h, &c->ec_stripe_head_list, list) {
2223 mutex_lock(&h->lock);
2224 if (!h->s)
2225 goto unlock;
2226
2227 if (!ca)
2228 goto found;
2229
2230 for (i = 0; i < bkey_i_to_stripe(&h->s->new_stripe.key)->v.nr_blocks; i++) {
2231 if (!h->s->blocks[i])
2232 continue;
2233
2234 ob = c->open_buckets + h->s->blocks[i];
2235 if (ob->dev == ca->dev_idx)
2236 goto found;
2237 }
2238 goto unlock;
2239 found:
2240 ec_stripe_new_cancel(c, h, -BCH_ERR_erofs_no_writes);
2241 unlock:
2242 mutex_unlock(&h->lock);
2243 }
2244 mutex_unlock(&c->ec_stripe_head_lock);
2245 }
2246
bch2_ec_stop_dev(struct bch_fs * c,struct bch_dev * ca)2247 void bch2_ec_stop_dev(struct bch_fs *c, struct bch_dev *ca)
2248 {
2249 __bch2_ec_stop(c, ca);
2250 }
2251
bch2_fs_ec_stop(struct bch_fs * c)2252 void bch2_fs_ec_stop(struct bch_fs *c)
2253 {
2254 __bch2_ec_stop(c, NULL);
2255 }
2256
bch2_fs_ec_flush_done(struct bch_fs * c)2257 static bool bch2_fs_ec_flush_done(struct bch_fs *c)
2258 {
2259 sched_annotate_sleep();
2260
2261 mutex_lock(&c->ec_stripe_new_lock);
2262 bool ret = list_empty(&c->ec_stripe_new_list);
2263 mutex_unlock(&c->ec_stripe_new_lock);
2264
2265 return ret;
2266 }
2267
bch2_fs_ec_flush(struct bch_fs * c)2268 void bch2_fs_ec_flush(struct bch_fs *c)
2269 {
2270 wait_event(c->ec_stripe_new_wait, bch2_fs_ec_flush_done(c));
2271 }
2272
bch2_stripes_read(struct bch_fs * c)2273 int bch2_stripes_read(struct bch_fs *c)
2274 {
2275 return 0;
2276 }
2277
bch2_new_stripe_to_text(struct printbuf * out,struct bch_fs * c,struct ec_stripe_new * s)2278 static void bch2_new_stripe_to_text(struct printbuf *out, struct bch_fs *c,
2279 struct ec_stripe_new *s)
2280 {
2281 prt_printf(out, "\tidx %llu blocks %u+%u allocated %u ref %u %u %s obs",
2282 s->idx, s->nr_data, s->nr_parity,
2283 bitmap_weight(s->blocks_allocated, s->nr_data),
2284 atomic_read(&s->ref[STRIPE_REF_io]),
2285 atomic_read(&s->ref[STRIPE_REF_stripe]),
2286 bch2_watermarks[s->h->watermark]);
2287
2288 struct bch_stripe *v = &bkey_i_to_stripe(&s->new_stripe.key)->v;
2289 unsigned i;
2290 for_each_set_bit(i, s->blocks_gotten, v->nr_blocks)
2291 prt_printf(out, " %u", s->blocks[i]);
2292 prt_newline(out);
2293 bch2_bkey_val_to_text(out, c, bkey_i_to_s_c(&s->new_stripe.key));
2294 prt_newline(out);
2295 }
2296
bch2_new_stripes_to_text(struct printbuf * out,struct bch_fs * c)2297 void bch2_new_stripes_to_text(struct printbuf *out, struct bch_fs *c)
2298 {
2299 struct ec_stripe_head *h;
2300 struct ec_stripe_new *s;
2301
2302 mutex_lock(&c->ec_stripe_head_lock);
2303 list_for_each_entry(h, &c->ec_stripe_head_list, list) {
2304 prt_printf(out, "disk label %u algo %u redundancy %u %s nr created %llu:\n",
2305 h->disk_label, h->algo, h->redundancy,
2306 bch2_watermarks[h->watermark],
2307 h->nr_created);
2308
2309 if (h->s)
2310 bch2_new_stripe_to_text(out, c, h->s);
2311 }
2312 mutex_unlock(&c->ec_stripe_head_lock);
2313
2314 prt_printf(out, "in flight:\n");
2315
2316 mutex_lock(&c->ec_stripe_new_lock);
2317 list_for_each_entry(s, &c->ec_stripe_new_list, list)
2318 bch2_new_stripe_to_text(out, c, s);
2319 mutex_unlock(&c->ec_stripe_new_lock);
2320 }
2321
bch2_fs_ec_exit(struct bch_fs * c)2322 void bch2_fs_ec_exit(struct bch_fs *c)
2323 {
2324 struct ec_stripe_head *h;
2325 unsigned i;
2326
2327 while (1) {
2328 mutex_lock(&c->ec_stripe_head_lock);
2329 h = list_pop_entry(&c->ec_stripe_head_list, struct ec_stripe_head, list);
2330 mutex_unlock(&c->ec_stripe_head_lock);
2331
2332 if (!h)
2333 break;
2334
2335 if (h->s) {
2336 for (i = 0; i < bkey_i_to_stripe(&h->s->new_stripe.key)->v.nr_blocks; i++)
2337 BUG_ON(h->s->blocks[i]);
2338
2339 kfree(h->s);
2340 }
2341 kfree(h);
2342 }
2343
2344 BUG_ON(!list_empty(&c->ec_stripe_new_list));
2345
2346 bioset_exit(&c->ec_bioset);
2347 }
2348
bch2_fs_ec_init_early(struct bch_fs * c)2349 void bch2_fs_ec_init_early(struct bch_fs *c)
2350 {
2351 spin_lock_init(&c->ec_stripes_new_lock);
2352
2353 INIT_LIST_HEAD(&c->ec_stripe_head_list);
2354 mutex_init(&c->ec_stripe_head_lock);
2355
2356 INIT_LIST_HEAD(&c->ec_stripe_new_list);
2357 mutex_init(&c->ec_stripe_new_lock);
2358 init_waitqueue_head(&c->ec_stripe_new_wait);
2359
2360 INIT_WORK(&c->ec_stripe_create_work, ec_stripe_create_work);
2361 INIT_WORK(&c->ec_stripe_delete_work, ec_stripe_delete_work);
2362 }
2363
bch2_fs_ec_init(struct bch_fs * c)2364 int bch2_fs_ec_init(struct bch_fs *c)
2365 {
2366 return bioset_init(&c->ec_bioset, 1, offsetof(struct ec_bio, bio),
2367 BIOSET_NEED_BVECS);
2368 }
2369
bch2_check_stripe_to_lru_ref(struct btree_trans * trans,struct bkey_s_c k,struct bkey_buf * last_flushed)2370 static int bch2_check_stripe_to_lru_ref(struct btree_trans *trans,
2371 struct bkey_s_c k,
2372 struct bkey_buf *last_flushed)
2373 {
2374 if (k.k->type != KEY_TYPE_stripe)
2375 return 0;
2376
2377 struct bkey_s_c_stripe s = bkey_s_c_to_stripe(k);
2378
2379 u64 lru_idx = stripe_lru_pos(s.v);
2380 if (lru_idx) {
2381 int ret = bch2_lru_check_set(trans, BCH_LRU_STRIPE_FRAGMENTATION,
2382 k.k->p.offset, lru_idx, k, last_flushed);
2383 if (ret)
2384 return ret;
2385 }
2386 return 0;
2387 }
2388
bch2_check_stripe_to_lru_refs(struct bch_fs * c)2389 int bch2_check_stripe_to_lru_refs(struct bch_fs *c)
2390 {
2391 struct bkey_buf last_flushed;
2392
2393 bch2_bkey_buf_init(&last_flushed);
2394 bkey_init(&last_flushed.k->k);
2395
2396 int ret = bch2_trans_run(c,
2397 for_each_btree_key_commit(trans, iter, BTREE_ID_stripes,
2398 POS_MIN, BTREE_ITER_prefetch, k,
2399 NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
2400 bch2_check_stripe_to_lru_ref(trans, k, &last_flushed)));
2401
2402 bch2_bkey_buf_exit(&last_flushed, c);
2403 bch_err_fn(c, ret);
2404 return ret;
2405 }
2406