1 // SPDX-License-Identifier: GPL-2.0
6 #include "alloc_foreground.h"
7 #include "backpointers.h"
11 #include "btree_update.h"
13 #include "disk_groups.h"
23 #include <linux/sort.h>
27 #include <linux/raid/pq.h>
28 #include <linux/raid/xor.h>
30 static void raid5_recov(unsigned disks, unsigned failed_idx,
31 size_t size, void **data)
35 BUG_ON(failed_idx >= disks);
37 swap(data[0], data[failed_idx]);
38 memcpy(data[0], data[1], size);
41 nr = min_t(unsigned, disks - i, MAX_XOR_BLOCKS);
42 xor_blocks(nr, size, data[0], data + i);
46 swap(data[0], data[failed_idx]);
49 static void raid_gen(int nd, int np, size_t size, void **v)
52 raid5_recov(nd + np, nd, size, v);
54 raid6_call.gen_syndrome(nd + np, size, v);
58 static void raid_rec(int nr, int *ir, int nd, int np, size_t size, void **v)
65 raid5_recov(nd + 1, ir[0], size, v);
67 raid6_call.gen_syndrome(nd + np, size, v);
71 /* data+data failure. */
72 raid6_2data_recov(nd + np, size, ir[0], ir[1], v);
73 } else if (ir[0] < nd) {
74 /* data + p/q failure */
76 if (ir[1] == nd) /* data + p failure */
77 raid6_datap_recov(nd + np, size, ir[0], v);
78 else { /* data + q failure */
79 raid5_recov(nd + 1, ir[0], size, v);
80 raid6_call.gen_syndrome(nd + np, size, v);
83 raid_gen(nd, np, size, v);
93 #include <raid/raid.h>
99 struct ec_stripe_buf *buf;
104 /* Stripes btree keys: */
106 int bch2_stripe_invalid(const struct bch_fs *c, struct bkey_s_c k,
107 unsigned flags, struct printbuf *err)
109 const struct bch_stripe *s = bkey_s_c_to_stripe(k).v;
111 if (bkey_eq(k.k->p, POS_MIN)) {
112 prt_printf(err, "stripe at POS_MIN");
113 return -BCH_ERR_invalid_bkey;
117 prt_printf(err, "nonzero inode field");
118 return -BCH_ERR_invalid_bkey;
121 if (bkey_val_bytes(k.k) < sizeof(*s)) {
122 prt_printf(err, "incorrect value size (%zu < %zu)",
123 bkey_val_bytes(k.k), sizeof(*s));
124 return -BCH_ERR_invalid_bkey;
127 if (bkey_val_u64s(k.k) < stripe_val_u64s(s)) {
128 prt_printf(err, "incorrect value size (%zu < %u)",
129 bkey_val_u64s(k.k), stripe_val_u64s(s));
130 return -BCH_ERR_invalid_bkey;
133 return bch2_bkey_ptrs_invalid(c, k, flags, err);
136 void bch2_stripe_to_text(struct printbuf *out, struct bch_fs *c,
139 const struct bch_stripe *s = bkey_s_c_to_stripe(k).v;
142 prt_printf(out, "algo %u sectors %u blocks %u:%u csum %u gran %u",
144 le16_to_cpu(s->sectors),
145 s->nr_blocks - s->nr_redundant,
148 1U << s->csum_granularity_bits);
150 for (i = 0; i < s->nr_blocks; i++)
151 prt_printf(out, " %u:%llu:%u", s->ptrs[i].dev,
152 (u64) s->ptrs[i].offset,
153 stripe_blockcount_get(s, i));
156 /* returns blocknr in stripe that we matched: */
157 static const struct bch_extent_ptr *bkey_matches_stripe(struct bch_stripe *s,
158 struct bkey_s_c k, unsigned *block)
160 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
161 const struct bch_extent_ptr *ptr;
162 unsigned i, nr_data = s->nr_blocks - s->nr_redundant;
164 bkey_for_each_ptr(ptrs, ptr)
165 for (i = 0; i < nr_data; i++)
166 if (__bch2_ptr_matches_stripe(&s->ptrs[i], ptr,
167 le16_to_cpu(s->sectors))) {
175 static bool extent_has_stripe_ptr(struct bkey_s_c k, u64 idx)
178 case KEY_TYPE_extent: {
179 struct bkey_s_c_extent e = bkey_s_c_to_extent(k);
180 const union bch_extent_entry *entry;
182 extent_for_each_entry(e, entry)
183 if (extent_entry_type(entry) ==
184 BCH_EXTENT_ENTRY_stripe_ptr &&
185 entry->stripe_ptr.idx == idx)
197 static void ec_stripe_buf_exit(struct ec_stripe_buf *buf)
201 for (i = 0; i < buf->key.v.nr_blocks; i++) {
202 kvpfree(buf->data[i], buf->size << 9);
207 static int ec_stripe_buf_init(struct ec_stripe_buf *buf,
208 unsigned offset, unsigned size)
210 struct bch_stripe *v = &buf->key.v;
211 unsigned csum_granularity = 1U << v->csum_granularity_bits;
212 unsigned end = offset + size;
215 BUG_ON(end > le16_to_cpu(v->sectors));
217 offset = round_down(offset, csum_granularity);
218 end = min_t(unsigned, le16_to_cpu(v->sectors),
219 round_up(end, csum_granularity));
221 buf->offset = offset;
222 buf->size = end - offset;
224 memset(buf->valid, 0xFF, sizeof(buf->valid));
226 for (i = 0; i < buf->key.v.nr_blocks; i++) {
227 buf->data[i] = kvpmalloc(buf->size << 9, GFP_KERNEL);
234 ec_stripe_buf_exit(buf);
240 static struct bch_csum ec_block_checksum(struct ec_stripe_buf *buf,
241 unsigned block, unsigned offset)
243 struct bch_stripe *v = &buf->key.v;
244 unsigned csum_granularity = 1 << v->csum_granularity_bits;
245 unsigned end = buf->offset + buf->size;
246 unsigned len = min(csum_granularity, end - offset);
248 BUG_ON(offset >= end);
249 BUG_ON(offset < buf->offset);
250 BUG_ON(offset & (csum_granularity - 1));
251 BUG_ON(offset + len != le16_to_cpu(v->sectors) &&
252 (len & (csum_granularity - 1)));
254 return bch2_checksum(NULL, v->csum_type,
256 buf->data[block] + ((offset - buf->offset) << 9),
260 static void ec_generate_checksums(struct ec_stripe_buf *buf)
262 struct bch_stripe *v = &buf->key.v;
263 unsigned i, j, csums_per_device = stripe_csums_per_device(v);
269 BUG_ON(buf->size != le16_to_cpu(v->sectors));
271 for (i = 0; i < v->nr_blocks; i++)
272 for (j = 0; j < csums_per_device; j++)
273 stripe_csum_set(v, i, j,
274 ec_block_checksum(buf, i, j << v->csum_granularity_bits));
277 static void ec_validate_checksums(struct bch_fs *c, struct ec_stripe_buf *buf)
279 struct bch_stripe *v = &buf->key.v;
280 unsigned csum_granularity = 1 << v->csum_granularity_bits;
286 for (i = 0; i < v->nr_blocks; i++) {
287 unsigned offset = buf->offset;
288 unsigned end = buf->offset + buf->size;
290 if (!test_bit(i, buf->valid))
293 while (offset < end) {
294 unsigned j = offset >> v->csum_granularity_bits;
295 unsigned len = min(csum_granularity, end - offset);
296 struct bch_csum want = stripe_csum_get(v, i, j);
297 struct bch_csum got = ec_block_checksum(buf, i, offset);
299 if (bch2_crc_cmp(want, got)) {
300 struct printbuf buf2 = PRINTBUF;
302 bch2_bkey_val_to_text(&buf2, c, bkey_i_to_s_c(&buf->key.k_i));
304 bch_err_ratelimited(c,
305 "stripe checksum error for %ps at %u:%u: csum type %u, expected %llx got %llx\n%s",
306 (void *) _RET_IP_, i, j, v->csum_type,
307 want.lo, got.lo, buf2.buf);
308 printbuf_exit(&buf2);
309 clear_bit(i, buf->valid);
318 /* Erasure coding: */
320 static void ec_generate_ec(struct ec_stripe_buf *buf)
322 struct bch_stripe *v = &buf->key.v;
323 unsigned nr_data = v->nr_blocks - v->nr_redundant;
324 unsigned bytes = le16_to_cpu(v->sectors) << 9;
326 raid_gen(nr_data, v->nr_redundant, bytes, buf->data);
329 static unsigned ec_nr_failed(struct ec_stripe_buf *buf)
331 return buf->key.v.nr_blocks -
332 bitmap_weight(buf->valid, buf->key.v.nr_blocks);
335 static int ec_do_recov(struct bch_fs *c, struct ec_stripe_buf *buf)
337 struct bch_stripe *v = &buf->key.v;
338 unsigned i, failed[BCH_BKEY_PTRS_MAX], nr_failed = 0;
339 unsigned nr_data = v->nr_blocks - v->nr_redundant;
340 unsigned bytes = buf->size << 9;
342 if (ec_nr_failed(buf) > v->nr_redundant) {
343 bch_err_ratelimited(c,
344 "error doing reconstruct read: unable to read enough blocks");
348 for (i = 0; i < nr_data; i++)
349 if (!test_bit(i, buf->valid))
350 failed[nr_failed++] = i;
352 raid_rec(nr_failed, failed, nr_data, v->nr_redundant, bytes, buf->data);
358 static void ec_block_endio(struct bio *bio)
360 struct ec_bio *ec_bio = container_of(bio, struct ec_bio, bio);
361 struct bch_stripe *v = &ec_bio->buf->key.v;
362 struct bch_extent_ptr *ptr = &v->ptrs[ec_bio->idx];
363 struct bch_dev *ca = ec_bio->ca;
364 struct closure *cl = bio->bi_private;
366 if (bch2_dev_io_err_on(bio->bi_status, ca, "erasure coding %s error: %s",
367 bio_data_dir(bio) ? "write" : "read",
368 bch2_blk_status_to_str(bio->bi_status)))
369 clear_bit(ec_bio->idx, ec_bio->buf->valid);
371 if (ptr_stale(ca, ptr)) {
372 bch_err_ratelimited(ca->fs,
373 "error %s stripe: stale pointer after io",
374 bio_data_dir(bio) == READ ? "reading from" : "writing to");
375 clear_bit(ec_bio->idx, ec_bio->buf->valid);
378 bio_put(&ec_bio->bio);
379 percpu_ref_put(&ca->io_ref);
383 static void ec_block_io(struct bch_fs *c, struct ec_stripe_buf *buf,
384 unsigned rw, unsigned idx, struct closure *cl)
386 struct bch_stripe *v = &buf->key.v;
387 unsigned offset = 0, bytes = buf->size << 9;
388 struct bch_extent_ptr *ptr = &v->ptrs[idx];
389 struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
390 enum bch_data_type data_type = idx < buf->key.v.nr_blocks - buf->key.v.nr_redundant
394 if (ptr_stale(ca, ptr)) {
395 bch_err_ratelimited(c,
396 "error %s stripe: stale pointer",
397 rw == READ ? "reading from" : "writing to");
398 clear_bit(idx, buf->valid);
402 if (!bch2_dev_get_ioref(ca, rw)) {
403 clear_bit(idx, buf->valid);
407 this_cpu_add(ca->io_done->sectors[rw][data_type], buf->size);
409 while (offset < bytes) {
410 unsigned nr_iovecs = min_t(size_t, BIO_MAX_VECS,
411 DIV_ROUND_UP(bytes, PAGE_SIZE));
412 unsigned b = min_t(size_t, bytes - offset,
413 nr_iovecs << PAGE_SHIFT);
414 struct ec_bio *ec_bio;
416 ec_bio = container_of(bio_alloc_bioset(ca->disk_sb.bdev,
427 ec_bio->bio.bi_iter.bi_sector = ptr->offset + buf->offset + (offset >> 9);
428 ec_bio->bio.bi_end_io = ec_block_endio;
429 ec_bio->bio.bi_private = cl;
431 bch2_bio_map(&ec_bio->bio, buf->data[idx] + offset, b);
434 percpu_ref_get(&ca->io_ref);
436 submit_bio(&ec_bio->bio);
441 percpu_ref_put(&ca->io_ref);
444 static int get_stripe_key(struct bch_fs *c, u64 idx, struct ec_stripe_buf *stripe)
446 struct btree_trans trans;
447 struct btree_iter iter;
451 bch2_trans_init(&trans, c, 0, 0);
452 bch2_trans_iter_init(&trans, &iter, BTREE_ID_stripes,
453 POS(0, idx), BTREE_ITER_SLOTS);
454 k = bch2_btree_iter_peek_slot(&iter);
458 if (k.k->type != KEY_TYPE_stripe) {
462 bkey_reassemble(&stripe->key.k_i, k);
464 bch2_trans_iter_exit(&trans, &iter);
465 bch2_trans_exit(&trans);
469 /* recovery read path: */
470 int bch2_ec_read_extent(struct bch_fs *c, struct bch_read_bio *rbio)
472 struct ec_stripe_buf *buf;
474 struct bch_stripe *v;
478 closure_init_stack(&cl);
480 BUG_ON(!rbio->pick.has_ec);
482 buf = kzalloc(sizeof(*buf), GFP_NOIO);
486 ret = get_stripe_key(c, rbio->pick.ec.idx, buf);
488 bch_err_ratelimited(c,
489 "error doing reconstruct read: error %i looking up stripe", ret);
496 if (!bch2_ptr_matches_stripe(v, rbio->pick)) {
497 bch_err_ratelimited(c,
498 "error doing reconstruct read: pointer doesn't match stripe");
503 offset = rbio->bio.bi_iter.bi_sector - v->ptrs[rbio->pick.ec.block].offset;
504 if (offset + bio_sectors(&rbio->bio) > le16_to_cpu(v->sectors)) {
505 bch_err_ratelimited(c,
506 "error doing reconstruct read: read is bigger than stripe");
511 ret = ec_stripe_buf_init(buf, offset, bio_sectors(&rbio->bio));
515 for (i = 0; i < v->nr_blocks; i++)
516 ec_block_io(c, buf, REQ_OP_READ, i, &cl);
520 if (ec_nr_failed(buf) > v->nr_redundant) {
521 bch_err_ratelimited(c,
522 "error doing reconstruct read: unable to read enough blocks");
527 ec_validate_checksums(c, buf);
529 ret = ec_do_recov(c, buf);
533 memcpy_to_bio(&rbio->bio, rbio->bio.bi_iter,
534 buf->data[rbio->pick.ec.block] + ((offset - buf->offset) << 9));
536 ec_stripe_buf_exit(buf);
541 /* stripe bucket accounting: */
543 static int __ec_stripe_mem_alloc(struct bch_fs *c, size_t idx, gfp_t gfp)
545 ec_stripes_heap n, *h = &c->ec_stripes_heap;
547 if (idx >= h->size) {
548 if (!init_heap(&n, max(1024UL, roundup_pow_of_two(idx + 1)), gfp))
551 spin_lock(&c->ec_stripes_heap_lock);
552 if (n.size > h->size) {
553 memcpy(n.data, h->data, h->used * sizeof(h->data[0]));
557 spin_unlock(&c->ec_stripes_heap_lock);
562 if (!genradix_ptr_alloc(&c->stripes, idx, gfp))
565 if (c->gc_pos.phase != GC_PHASE_NOT_RUNNING &&
566 !genradix_ptr_alloc(&c->gc_stripes, idx, gfp))
572 static int ec_stripe_mem_alloc(struct btree_trans *trans,
573 struct btree_iter *iter)
575 size_t idx = iter->pos.offset;
577 if (!__ec_stripe_mem_alloc(trans->c, idx, GFP_NOWAIT|__GFP_NOWARN))
580 bch2_trans_unlock(trans);
582 return __ec_stripe_mem_alloc(trans->c, idx, GFP_KERNEL) ?:
583 bch2_trans_relock(trans);
586 static ssize_t stripe_idx_to_delete(struct bch_fs *c)
588 ec_stripes_heap *h = &c->ec_stripes_heap;
590 return h->used && h->data[0].blocks_nonempty == 0
591 ? h->data[0].idx : -1;
594 static inline int ec_stripes_heap_cmp(ec_stripes_heap *h,
595 struct ec_stripe_heap_entry l,
596 struct ec_stripe_heap_entry r)
598 return ((l.blocks_nonempty > r.blocks_nonempty) -
599 (l.blocks_nonempty < r.blocks_nonempty));
602 static inline void ec_stripes_heap_set_backpointer(ec_stripes_heap *h,
605 struct bch_fs *c = container_of(h, struct bch_fs, ec_stripes_heap);
607 genradix_ptr(&c->stripes, h->data[i].idx)->heap_idx = i;
610 static void heap_verify_backpointer(struct bch_fs *c, size_t idx)
612 ec_stripes_heap *h = &c->ec_stripes_heap;
613 struct stripe *m = genradix_ptr(&c->stripes, idx);
616 BUG_ON(m->heap_idx >= h->used);
617 BUG_ON(h->data[m->heap_idx].idx != idx);
620 void bch2_stripes_heap_del(struct bch_fs *c,
621 struct stripe *m, size_t idx)
628 heap_verify_backpointer(c, idx);
630 heap_del(&c->ec_stripes_heap, m->heap_idx,
632 ec_stripes_heap_set_backpointer);
635 void bch2_stripes_heap_insert(struct bch_fs *c,
636 struct stripe *m, size_t idx)
641 BUG_ON(heap_full(&c->ec_stripes_heap));
645 heap_add(&c->ec_stripes_heap, ((struct ec_stripe_heap_entry) {
647 .blocks_nonempty = m->blocks_nonempty,
650 ec_stripes_heap_set_backpointer);
652 heap_verify_backpointer(c, idx);
655 void bch2_stripes_heap_update(struct bch_fs *c,
656 struct stripe *m, size_t idx)
658 ec_stripes_heap *h = &c->ec_stripes_heap;
664 heap_verify_backpointer(c, idx);
666 h->data[m->heap_idx].blocks_nonempty = m->blocks_nonempty;
669 heap_sift_up(h, i, ec_stripes_heap_cmp,
670 ec_stripes_heap_set_backpointer);
671 heap_sift_down(h, i, ec_stripes_heap_cmp,
672 ec_stripes_heap_set_backpointer);
674 heap_verify_backpointer(c, idx);
676 if (stripe_idx_to_delete(c) >= 0)
677 bch2_do_stripe_deletes(c);
680 /* stripe deletion */
682 static int ec_stripe_delete(struct bch_fs *c, size_t idx)
684 return bch2_btree_delete_range(c, BTREE_ID_stripes,
690 static void ec_stripe_delete_work(struct work_struct *work)
693 container_of(work, struct bch_fs, ec_stripe_delete_work);
697 spin_lock(&c->ec_stripes_heap_lock);
698 idx = stripe_idx_to_delete(c);
700 spin_unlock(&c->ec_stripes_heap_lock);
704 bch2_stripes_heap_del(c, genradix_ptr(&c->stripes, idx), idx);
705 spin_unlock(&c->ec_stripes_heap_lock);
707 if (ec_stripe_delete(c, idx))
711 bch2_write_ref_put(c, BCH_WRITE_REF_stripe_delete);
714 void bch2_do_stripe_deletes(struct bch_fs *c)
716 if (bch2_write_ref_tryget(c, BCH_WRITE_REF_stripe_delete) &&
717 !schedule_work(&c->ec_stripe_delete_work))
718 bch2_write_ref_put(c, BCH_WRITE_REF_stripe_delete);
721 /* stripe creation: */
723 static int ec_stripe_bkey_insert(struct btree_trans *trans,
724 struct bkey_i_stripe *stripe,
725 struct disk_reservation *res)
727 struct bch_fs *c = trans->c;
728 struct btree_iter iter;
730 struct bpos min_pos = POS(0, 1);
731 struct bpos start_pos = bpos_max(min_pos, POS(0, c->ec_stripe_hint));
734 for_each_btree_key_norestart(trans, iter, BTREE_ID_stripes, start_pos,
735 BTREE_ITER_SLOTS|BTREE_ITER_INTENT, k, ret) {
736 if (bkey_gt(k.k->p, POS(0, U32_MAX))) {
737 if (start_pos.offset) {
739 bch2_btree_iter_set_pos(&iter, start_pos);
743 ret = -BCH_ERR_ENOSPC_stripe_create;
747 if (bkey_deleted(k.k))
751 c->ec_stripe_hint = iter.pos.offset;
756 ret = ec_stripe_mem_alloc(trans, &iter);
760 stripe->k.p = iter.pos;
762 ret = bch2_trans_update(trans, &iter, &stripe->k_i, 0);
764 bch2_trans_iter_exit(trans, &iter);
769 static int ec_stripe_bkey_update(struct btree_trans *trans,
770 struct bkey_i_stripe *new,
771 struct disk_reservation *res)
773 struct btree_iter iter;
775 const struct bch_stripe *existing;
779 bch2_trans_iter_init(trans, &iter, BTREE_ID_stripes,
780 new->k.p, BTREE_ITER_INTENT);
781 k = bch2_btree_iter_peek_slot(&iter);
786 if (!k.k || k.k->type != KEY_TYPE_stripe) {
787 bch_err(trans->c, "error updating stripe: not found");
792 existing = bkey_s_c_to_stripe(k).v;
794 if (existing->nr_blocks != new->v.nr_blocks) {
795 bch_err(trans->c, "error updating stripe: nr_blocks does not match");
800 for (i = 0; i < new->v.nr_blocks; i++)
801 stripe_blockcount_set(&new->v, i,
802 stripe_blockcount_get(existing, i));
804 ret = bch2_trans_update(trans, &iter, &new->k_i, 0);
806 bch2_trans_iter_exit(trans, &iter);
810 static void extent_stripe_ptr_add(struct bkey_s_extent e,
811 struct ec_stripe_buf *s,
812 struct bch_extent_ptr *ptr,
815 struct bch_extent_stripe_ptr *dst = (void *) ptr;
816 union bch_extent_entry *end = extent_entry_last(e);
818 memmove_u64s_up(dst + 1, dst, (u64 *) end - (u64 *) dst);
819 e.k->u64s += sizeof(*dst) / sizeof(u64);
821 *dst = (struct bch_extent_stripe_ptr) {
822 .type = 1 << BCH_EXTENT_ENTRY_stripe_ptr,
824 .redundancy = s->key.v.nr_redundant,
825 .idx = s->key.k.p.offset,
829 static int ec_stripe_update_extent(struct btree_trans *trans,
830 struct btree_iter *iter,
832 struct ec_stripe_buf *s)
834 const struct bch_extent_ptr *ptr_c;
835 struct bch_extent_ptr *ptr, *ec_ptr = NULL;
839 if (extent_has_stripe_ptr(k, s->key.k.p.offset))
842 ptr_c = bkey_matches_stripe(&s->key.v, k, &block);
844 * It doesn't generally make sense to erasure code cached ptrs:
845 * XXX: should we be incrementing a counter?
847 if (!ptr_c || ptr_c->cached)
850 dev = s->key.v.ptrs[block].dev;
852 n = bch2_bkey_make_mut(trans, k);
853 ret = PTR_ERR_OR_ZERO(n);
857 bch2_bkey_drop_ptrs(bkey_i_to_s(n), ptr, ptr->dev != dev);
858 ec_ptr = (void *) bch2_bkey_has_device(bkey_i_to_s_c(n), dev);
861 extent_stripe_ptr_add(bkey_i_to_s_extent(n), s, ec_ptr, block);
863 return bch2_trans_update(trans, iter, n, 0);
866 static int ec_stripe_update_bucket(struct btree_trans *trans, struct ec_stripe_buf *s,
869 struct bch_fs *c = trans->c;
870 struct bch_extent_ptr bucket = s->key.v.ptrs[block];
871 struct bpos bucket_pos = PTR_BUCKET_POS(c, &bucket);
872 struct bch_backpointer bp;
873 struct btree_iter iter;
879 bch2_trans_begin(trans);
881 ret = bch2_get_next_backpointer(trans, bucket_pos, bucket.gen,
886 if (bp_offset == U64_MAX)
889 if (bch2_fs_inconsistent_on(bp.level, c, "found btree node in erasure coded bucket!?")) {
894 k = bch2_backpointer_get_key(trans, &iter, bucket_pos, bp_offset, bp);
901 ret = ec_stripe_update_extent(trans, &iter, k, s);
902 bch2_trans_iter_exit(trans, &iter);
909 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
915 static int ec_stripe_update_extents(struct bch_fs *c, struct ec_stripe_buf *s)
917 struct btree_trans trans;
918 struct bch_stripe *v = &s->key.v;
919 unsigned i, nr_data = v->nr_blocks - v->nr_redundant;
922 bch2_trans_init(&trans, c, 0, 0);
924 for (i = 0; i < nr_data; i++) {
925 ret = ec_stripe_update_bucket(&trans, s, i);
931 bch2_trans_exit(&trans);
937 * data buckets of new stripe all written: create the stripe
939 static void ec_stripe_create(struct ec_stripe_new *s)
941 struct bch_fs *c = s->c;
942 struct open_bucket *ob;
944 struct bch_stripe *v = &s->new_stripe.key.v;
945 unsigned i, nr_data = v->nr_blocks - v->nr_redundant;
948 BUG_ON(s->h->s == s);
950 closure_sync(&s->iodone);
953 if (!bch2_err_matches(s->err, EROFS))
954 bch_err(c, "error creating stripe: error writing data buckets");
958 if (s->have_existing_stripe) {
959 ec_validate_checksums(c, &s->existing_stripe);
961 if (ec_do_recov(c, &s->existing_stripe)) {
962 bch_err(c, "error creating stripe: error reading existing stripe");
966 for (i = 0; i < nr_data; i++)
967 if (stripe_blockcount_get(&s->existing_stripe.key.v, i))
968 swap(s->new_stripe.data[i],
969 s->existing_stripe.data[i]);
971 ec_stripe_buf_exit(&s->existing_stripe);
974 BUG_ON(!s->allocated);
976 if (!bch2_write_ref_tryget(c, BCH_WRITE_REF_stripe_create))
979 ec_generate_ec(&s->new_stripe);
981 ec_generate_checksums(&s->new_stripe);
984 for (i = nr_data; i < v->nr_blocks; i++)
985 ec_block_io(c, &s->new_stripe, REQ_OP_WRITE, i, &s->iodone);
986 closure_sync(&s->iodone);
988 if (ec_nr_failed(&s->new_stripe)) {
989 bch_err(c, "error creating stripe: error writing redundancy buckets");
993 ret = bch2_trans_do(c, &s->res, NULL, BTREE_INSERT_NOFAIL,
994 s->have_existing_stripe
995 ? ec_stripe_bkey_update(&trans, &s->new_stripe.key, &s->res)
996 : ec_stripe_bkey_insert(&trans, &s->new_stripe.key, &s->res));
998 bch_err(c, "error creating stripe: error creating stripe key");
1002 ret = ec_stripe_update_extents(c, &s->new_stripe);
1004 bch_err(c, "error creating stripe: error updating pointers: %s",
1007 spin_lock(&c->ec_stripes_heap_lock);
1008 m = genradix_ptr(&c->stripes, s->new_stripe.key.k.p.offset);
1011 bch2_stripes_heap_insert(c, m, s->new_stripe.key.k.p.offset);
1012 spin_unlock(&c->ec_stripes_heap_lock);
1014 bch2_write_ref_put(c, BCH_WRITE_REF_stripe_create);
1016 bch2_disk_reservation_put(c, &s->res);
1018 for (i = 0; i < v->nr_blocks; i++)
1020 ob = c->open_buckets + s->blocks[i];
1024 __bch2_open_bucket_put(c, ob);
1026 bch2_open_bucket_put(c, ob);
1030 ec_stripe_buf_exit(&s->existing_stripe);
1031 ec_stripe_buf_exit(&s->new_stripe);
1032 closure_debug_destroy(&s->iodone);
1036 static void ec_stripe_create_work(struct work_struct *work)
1038 struct bch_fs *c = container_of(work,
1039 struct bch_fs, ec_stripe_create_work);
1040 struct ec_stripe_new *s, *n;
1042 mutex_lock(&c->ec_stripe_new_lock);
1043 list_for_each_entry_safe(s, n, &c->ec_stripe_new_list, list)
1044 if (!atomic_read(&s->pin)) {
1046 mutex_unlock(&c->ec_stripe_new_lock);
1047 ec_stripe_create(s);
1050 mutex_unlock(&c->ec_stripe_new_lock);
1053 static void ec_stripe_new_put(struct bch_fs *c, struct ec_stripe_new *s)
1055 BUG_ON(atomic_read(&s->pin) <= 0);
1057 if (atomic_dec_and_test(&s->pin)) {
1058 BUG_ON(!s->pending);
1059 queue_work(system_long_wq, &c->ec_stripe_create_work);
1063 static void ec_stripe_set_pending(struct bch_fs *c, struct ec_stripe_head *h)
1065 struct ec_stripe_new *s = h->s;
1067 BUG_ON(!s->allocated && !s->err);
1072 mutex_lock(&c->ec_stripe_new_lock);
1073 list_add(&s->list, &c->ec_stripe_new_list);
1074 mutex_unlock(&c->ec_stripe_new_lock);
1076 ec_stripe_new_put(c, s);
1079 /* have a full bucket - hand it off to be erasure coded: */
1080 void bch2_ec_bucket_written(struct bch_fs *c, struct open_bucket *ob)
1082 struct ec_stripe_new *s = ob->ec;
1084 if (ob->sectors_free)
1087 ec_stripe_new_put(c, s);
1090 void bch2_ec_bucket_cancel(struct bch_fs *c, struct open_bucket *ob)
1092 struct ec_stripe_new *s = ob->ec;
1097 void *bch2_writepoint_ec_buf(struct bch_fs *c, struct write_point *wp)
1099 struct open_bucket *ob = ec_open_bucket(c, &wp->ptrs);
1106 ca = bch_dev_bkey_exists(c, ob->dev);
1107 offset = ca->mi.bucket_size - ob->sectors_free;
1109 return ob->ec->new_stripe.data[ob->ec_idx] + (offset << 9);
1112 static int unsigned_cmp(const void *_l, const void *_r)
1114 unsigned l = *((const unsigned *) _l);
1115 unsigned r = *((const unsigned *) _r);
1117 return cmp_int(l, r);
1120 /* pick most common bucket size: */
1121 static unsigned pick_blocksize(struct bch_fs *c,
1122 struct bch_devs_mask *devs)
1125 unsigned i, nr = 0, sizes[BCH_SB_MEMBERS_MAX];
1128 } cur = { 0, 0 }, best = { 0, 0 };
1130 for_each_member_device_rcu(ca, c, i, devs)
1131 sizes[nr++] = ca->mi.bucket_size;
1133 sort(sizes, nr, sizeof(unsigned), unsigned_cmp, NULL);
1135 for (i = 0; i < nr; i++) {
1136 if (sizes[i] != cur.size) {
1137 if (cur.nr > best.nr)
1141 cur.size = sizes[i];
1147 if (cur.nr > best.nr)
1153 static bool may_create_new_stripe(struct bch_fs *c)
1158 static void ec_stripe_key_init(struct bch_fs *c,
1159 struct bkey_i_stripe *s,
1162 unsigned stripe_size)
1166 bkey_stripe_init(&s->k_i);
1167 s->v.sectors = cpu_to_le16(stripe_size);
1169 s->v.nr_blocks = nr_data + nr_parity;
1170 s->v.nr_redundant = nr_parity;
1171 s->v.csum_granularity_bits = ilog2(c->opts.encoded_extent_max >> 9);
1172 s->v.csum_type = BCH_CSUM_crc32c;
1175 while ((u64s = stripe_val_u64s(&s->v)) > BKEY_VAL_U64s_MAX) {
1176 BUG_ON(1 << s->v.csum_granularity_bits >=
1177 le16_to_cpu(s->v.sectors) ||
1178 s->v.csum_granularity_bits == U8_MAX);
1179 s->v.csum_granularity_bits++;
1182 set_bkey_val_u64s(&s->k, u64s);
1185 static int ec_new_stripe_alloc(struct bch_fs *c, struct ec_stripe_head *h)
1187 struct ec_stripe_new *s;
1189 lockdep_assert_held(&h->lock);
1191 s = kzalloc(sizeof(*s), GFP_KERNEL);
1195 mutex_init(&s->lock);
1196 closure_init(&s->iodone, NULL);
1197 atomic_set(&s->pin, 1);
1200 s->nr_data = min_t(unsigned, h->nr_active_devs,
1201 BCH_BKEY_PTRS_MAX) - h->redundancy;
1202 s->nr_parity = h->redundancy;
1204 ec_stripe_key_init(c, &s->new_stripe.key, s->nr_data,
1205 s->nr_parity, h->blocksize);
1211 static struct ec_stripe_head *
1212 ec_new_stripe_head_alloc(struct bch_fs *c, unsigned target,
1213 unsigned algo, unsigned redundancy,
1216 struct ec_stripe_head *h;
1220 h = kzalloc(sizeof(*h), GFP_KERNEL);
1224 mutex_init(&h->lock);
1225 mutex_lock(&h->lock);
1229 h->redundancy = redundancy;
1233 h->devs = target_rw_devs(c, BCH_DATA_user, target);
1235 for_each_member_device_rcu(ca, c, i, &h->devs)
1236 if (!ca->mi.durability)
1237 __clear_bit(i, h->devs.d);
1239 h->blocksize = pick_blocksize(c, &h->devs);
1241 for_each_member_device_rcu(ca, c, i, &h->devs)
1242 if (ca->mi.bucket_size == h->blocksize)
1243 h->nr_active_devs++;
1246 list_add(&h->list, &c->ec_stripe_head_list);
1250 void bch2_ec_stripe_head_put(struct bch_fs *c, struct ec_stripe_head *h)
1254 bitmap_weight(h->s->blocks_allocated,
1255 h->s->nr_data) == h->s->nr_data)
1256 ec_stripe_set_pending(c, h);
1258 mutex_unlock(&h->lock);
1261 struct ec_stripe_head *__bch2_ec_stripe_head_get(struct bch_fs *c,
1264 unsigned redundancy,
1267 struct ec_stripe_head *h;
1272 mutex_lock(&c->ec_stripe_head_lock);
1273 list_for_each_entry(h, &c->ec_stripe_head_list, list)
1274 if (h->target == target &&
1276 h->redundancy == redundancy &&
1277 h->copygc == copygc) {
1278 mutex_lock(&h->lock);
1282 h = ec_new_stripe_head_alloc(c, target, algo, redundancy, copygc);
1284 mutex_unlock(&c->ec_stripe_head_lock);
1288 static int new_stripe_alloc_buckets(struct bch_fs *c, struct ec_stripe_head *h,
1291 struct bch_devs_mask devs = h->devs;
1292 struct open_bucket *ob;
1293 struct open_buckets buckets;
1294 unsigned i, j, nr_have_parity = 0, nr_have_data = 0;
1295 bool have_cache = true;
1298 for (i = 0; i < h->s->new_stripe.key.v.nr_blocks; i++) {
1299 if (test_bit(i, h->s->blocks_gotten)) {
1300 __clear_bit(h->s->new_stripe.key.v.ptrs[i].dev, devs.d);
1301 if (i < h->s->nr_data)
1308 BUG_ON(nr_have_data > h->s->nr_data);
1309 BUG_ON(nr_have_parity > h->s->nr_parity);
1312 if (nr_have_parity < h->s->nr_parity) {
1313 ret = bch2_bucket_alloc_set(c, &buckets,
1325 open_bucket_for_each(c, &buckets, ob, i) {
1326 j = find_next_zero_bit(h->s->blocks_gotten,
1327 h->s->nr_data + h->s->nr_parity,
1329 BUG_ON(j >= h->s->nr_data + h->s->nr_parity);
1331 h->s->blocks[j] = buckets.v[i];
1332 h->s->new_stripe.key.v.ptrs[j] = bch2_ob_ptr(c, ob);
1333 __set_bit(j, h->s->blocks_gotten);
1341 if (nr_have_data < h->s->nr_data) {
1342 ret = bch2_bucket_alloc_set(c, &buckets,
1354 open_bucket_for_each(c, &buckets, ob, i) {
1355 j = find_next_zero_bit(h->s->blocks_gotten,
1357 BUG_ON(j >= h->s->nr_data);
1359 h->s->blocks[j] = buckets.v[i];
1360 h->s->new_stripe.key.v.ptrs[j] = bch2_ob_ptr(c, ob);
1361 __set_bit(j, h->s->blocks_gotten);
1371 /* XXX: doesn't obey target: */
1372 static s64 get_existing_stripe(struct bch_fs *c,
1373 struct ec_stripe_head *head)
1375 ec_stripes_heap *h = &c->ec_stripes_heap;
1381 if (may_create_new_stripe(c))
1384 spin_lock(&c->ec_stripes_heap_lock);
1385 for (heap_idx = 0; heap_idx < h->used; heap_idx++) {
1386 /* No blocks worth reusing, stripe will just be deleted: */
1387 if (!h->data[heap_idx].blocks_nonempty)
1390 stripe_idx = h->data[heap_idx].idx;
1391 m = genradix_ptr(&c->stripes, stripe_idx);
1393 if (m->algorithm == head->algo &&
1394 m->nr_redundant == head->redundancy &&
1395 m->sectors == head->blocksize &&
1396 m->blocks_nonempty < m->nr_blocks - m->nr_redundant) {
1397 bch2_stripes_heap_del(c, m, stripe_idx);
1402 spin_unlock(&c->ec_stripes_heap_lock);
1406 static int __bch2_ec_stripe_head_reuse(struct bch_fs *c,
1407 struct ec_stripe_head *h)
1413 idx = get_existing_stripe(c, h);
1415 return -BCH_ERR_ENOSPC_stripe_reuse;
1417 h->s->have_existing_stripe = true;
1418 ret = get_stripe_key(c, idx, &h->s->existing_stripe);
1420 bch2_fs_fatal_error(c, "error reading stripe key: %i", ret);
1424 if (ec_stripe_buf_init(&h->s->existing_stripe, 0, h->blocksize)) {
1426 * this is a problem: we have deleted from the
1427 * stripes heap already
1432 BUG_ON(h->s->existing_stripe.size != h->blocksize);
1433 BUG_ON(h->s->existing_stripe.size != h->s->existing_stripe.key.v.sectors);
1435 for (i = 0; i < h->s->existing_stripe.key.v.nr_blocks; i++) {
1436 if (stripe_blockcount_get(&h->s->existing_stripe.key.v, i)) {
1437 __set_bit(i, h->s->blocks_gotten);
1438 __set_bit(i, h->s->blocks_allocated);
1441 ec_block_io(c, &h->s->existing_stripe, READ, i, &h->s->iodone);
1444 bkey_copy(&h->s->new_stripe.key.k_i,
1445 &h->s->existing_stripe.key.k_i);
1450 static int __bch2_ec_stripe_head_reserve(struct bch_fs *c,
1451 struct ec_stripe_head *h)
1453 return bch2_disk_reservation_get(c, &h->s->res,
1455 h->s->nr_parity, 0);
1458 struct ec_stripe_head *bch2_ec_stripe_head_get(struct bch_fs *c,
1461 unsigned redundancy,
1465 struct ec_stripe_head *h;
1467 bool needs_stripe_new;
1469 h = __bch2_ec_stripe_head_get(c, target, algo, redundancy, copygc);
1471 bch_err(c, "no stripe head");
1475 needs_stripe_new = !h->s;
1476 if (needs_stripe_new) {
1477 if (ec_new_stripe_alloc(c, h)) {
1479 bch_err(c, "failed to allocate new stripe");
1483 if (ec_stripe_buf_init(&h->s->new_stripe, 0, h->blocksize))
1488 * Try reserve a new stripe before reusing an
1489 * existing stripe. This will prevent unnecessary
1490 * read amplification during write oriented workloads.
1493 if (!h->s->allocated && !h->s->res.sectors && !h->s->have_existing_stripe)
1494 ret = __bch2_ec_stripe_head_reserve(c, h);
1495 if (ret && needs_stripe_new)
1496 ret = __bch2_ec_stripe_head_reuse(c, h);
1498 bch_err_ratelimited(c, "failed to get stripe: %s", bch2_err_str(ret));
1502 if (!h->s->allocated) {
1503 ret = new_stripe_alloc_buckets(c, h, cl);
1507 h->s->allocated = true;
1513 bch2_ec_stripe_head_put(c, h);
1514 return ERR_PTR(ret);
1517 void bch2_ec_stop_dev(struct bch_fs *c, struct bch_dev *ca)
1519 struct ec_stripe_head *h;
1520 struct open_bucket *ob;
1523 mutex_lock(&c->ec_stripe_head_lock);
1524 list_for_each_entry(h, &c->ec_stripe_head_list, list) {
1526 mutex_lock(&h->lock);
1530 for (i = 0; i < h->s->new_stripe.key.v.nr_blocks; i++) {
1531 if (!h->s->blocks[i])
1534 ob = c->open_buckets + h->s->blocks[i];
1535 if (ob->dev == ca->dev_idx)
1541 ec_stripe_set_pending(c, h);
1543 mutex_unlock(&h->lock);
1545 mutex_unlock(&c->ec_stripe_head_lock);
1548 void bch2_stripes_heap_start(struct bch_fs *c)
1550 struct genradix_iter iter;
1553 genradix_for_each(&c->stripes, iter, m)
1555 bch2_stripes_heap_insert(c, m, iter.pos);
1558 int bch2_stripes_read(struct bch_fs *c)
1560 struct btree_trans trans;
1561 struct btree_iter iter;
1563 const struct bch_stripe *s;
1568 bch2_trans_init(&trans, c, 0, 0);
1570 for_each_btree_key(&trans, iter, BTREE_ID_stripes, POS_MIN,
1571 BTREE_ITER_PREFETCH, k, ret) {
1572 if (k.k->type != KEY_TYPE_stripe)
1575 ret = __ec_stripe_mem_alloc(c, k.k->p.offset, GFP_KERNEL);
1579 s = bkey_s_c_to_stripe(k).v;
1581 m = genradix_ptr(&c->stripes, k.k->p.offset);
1583 m->sectors = le16_to_cpu(s->sectors);
1584 m->algorithm = s->algorithm;
1585 m->nr_blocks = s->nr_blocks;
1586 m->nr_redundant = s->nr_redundant;
1587 m->blocks_nonempty = 0;
1589 for (i = 0; i < s->nr_blocks; i++)
1590 m->blocks_nonempty += !!stripe_blockcount_get(s, i);
1592 spin_lock(&c->ec_stripes_heap_lock);
1593 bch2_stripes_heap_update(c, m, k.k->p.offset);
1594 spin_unlock(&c->ec_stripes_heap_lock);
1596 bch2_trans_iter_exit(&trans, &iter);
1598 bch2_trans_exit(&trans);
1601 bch_err(c, "error reading stripes: %i", ret);
1606 void bch2_stripes_heap_to_text(struct printbuf *out, struct bch_fs *c)
1608 ec_stripes_heap *h = &c->ec_stripes_heap;
1612 spin_lock(&c->ec_stripes_heap_lock);
1613 for (i = 0; i < min_t(size_t, h->used, 20); i++) {
1614 m = genradix_ptr(&c->stripes, h->data[i].idx);
1616 prt_printf(out, "%zu %u/%u+%u\n", h->data[i].idx,
1617 h->data[i].blocks_nonempty,
1618 m->nr_blocks - m->nr_redundant,
1621 spin_unlock(&c->ec_stripes_heap_lock);
1624 void bch2_new_stripes_to_text(struct printbuf *out, struct bch_fs *c)
1626 struct ec_stripe_head *h;
1627 struct ec_stripe_new *s;
1629 mutex_lock(&c->ec_stripe_head_lock);
1630 list_for_each_entry(h, &c->ec_stripe_head_list, list) {
1631 prt_printf(out, "target %u algo %u redundancy %u:\n",
1632 h->target, h->algo, h->redundancy);
1635 prt_printf(out, "\tpending: blocks %u+%u allocated %u\n",
1636 h->s->nr_data, h->s->nr_parity,
1637 bitmap_weight(h->s->blocks_allocated,
1640 mutex_unlock(&c->ec_stripe_head_lock);
1642 mutex_lock(&c->ec_stripe_new_lock);
1643 list_for_each_entry(s, &c->ec_stripe_new_list, list) {
1644 prt_printf(out, "\tin flight: blocks %u+%u pin %u\n",
1645 s->nr_data, s->nr_parity,
1646 atomic_read(&s->pin));
1648 mutex_unlock(&c->ec_stripe_new_lock);
1651 void bch2_fs_ec_exit(struct bch_fs *c)
1653 struct ec_stripe_head *h;
1656 mutex_lock(&c->ec_stripe_head_lock);
1657 h = list_first_entry_or_null(&c->ec_stripe_head_list,
1658 struct ec_stripe_head, list);
1661 mutex_unlock(&c->ec_stripe_head_lock);
1669 BUG_ON(!list_empty(&c->ec_stripe_new_list));
1671 free_heap(&c->ec_stripes_heap);
1672 genradix_free(&c->stripes);
1673 bioset_exit(&c->ec_bioset);
1676 void bch2_fs_ec_init_early(struct bch_fs *c)
1678 INIT_WORK(&c->ec_stripe_create_work, ec_stripe_create_work);
1679 INIT_WORK(&c->ec_stripe_delete_work, ec_stripe_delete_work);
1682 int bch2_fs_ec_init(struct bch_fs *c)
1684 return bioset_init(&c->ec_bioset, 1, offsetof(struct ec_bio, bio),