1 // SPDX-License-Identifier: GPL-2.0
6 #include "alloc_foreground.h"
10 #include "btree_update.h"
12 #include "disk_groups.h"
22 #include <linux/sort.h>
26 #include <linux/raid/pq.h>
27 #include <linux/raid/xor.h>
29 static void raid5_recov(unsigned disks, unsigned failed_idx,
30 size_t size, void **data)
34 BUG_ON(failed_idx >= disks);
36 swap(data[0], data[failed_idx]);
37 memcpy(data[0], data[1], size);
40 nr = min_t(unsigned, disks - i, MAX_XOR_BLOCKS);
41 xor_blocks(nr, size, data[0], data + i);
45 swap(data[0], data[failed_idx]);
48 static void raid_gen(int nd, int np, size_t size, void **v)
51 raid5_recov(nd + np, nd, size, v);
53 raid6_call.gen_syndrome(nd + np, size, v);
57 static void raid_rec(int nr, int *ir, int nd, int np, size_t size, void **v)
64 raid5_recov(nd + 1, ir[0], size, v);
66 raid6_call.gen_syndrome(nd + np, size, v);
70 /* data+data failure. */
71 raid6_2data_recov(nd + np, size, ir[0], ir[1], v);
72 } else if (ir[0] < nd) {
73 /* data + p/q failure */
75 if (ir[1] == nd) /* data + p failure */
76 raid6_datap_recov(nd + np, size, ir[0], v);
77 else { /* data + q failure */
78 raid5_recov(nd + 1, ir[0], size, v);
79 raid6_call.gen_syndrome(nd + np, size, v);
82 raid_gen(nd, np, size, v);
92 #include <raid/raid.h>
98 struct ec_stripe_buf *buf;
103 /* Stripes btree keys: */
105 const char *bch2_stripe_invalid(const struct bch_fs *c, struct bkey_s_c k)
107 const struct bch_stripe *s = bkey_s_c_to_stripe(k).v;
109 if (!bkey_cmp(k.k->p, POS_MIN))
110 return "stripe at pos 0";
113 return "invalid stripe key";
115 if (bkey_val_bytes(k.k) < sizeof(*s))
116 return "incorrect value size";
118 if (bkey_val_bytes(k.k) < sizeof(*s) ||
119 bkey_val_u64s(k.k) < stripe_val_u64s(s))
120 return "incorrect value size";
122 return bch2_bkey_ptrs_invalid(c, k);
125 void bch2_stripe_to_text(struct printbuf *out, struct bch_fs *c,
128 const struct bch_stripe *s = bkey_s_c_to_stripe(k).v;
131 pr_buf(out, "algo %u sectors %u blocks %u:%u csum %u gran %u",
133 le16_to_cpu(s->sectors),
134 s->nr_blocks - s->nr_redundant,
137 1U << s->csum_granularity_bits);
139 for (i = 0; i < s->nr_blocks; i++)
140 pr_buf(out, " %u:%llu:%u", s->ptrs[i].dev,
141 (u64) s->ptrs[i].offset,
142 stripe_blockcount_get(s, i));
145 /* returns blocknr in stripe that we matched: */
146 static const struct bch_extent_ptr *bkey_matches_stripe(struct bch_stripe *s,
147 struct bkey_s_c k, unsigned *block)
149 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
150 const struct bch_extent_ptr *ptr;
151 unsigned i, nr_data = s->nr_blocks - s->nr_redundant;
153 bkey_for_each_ptr(ptrs, ptr)
154 for (i = 0; i < nr_data; i++)
155 if (__bch2_ptr_matches_stripe(&s->ptrs[i], ptr,
156 le16_to_cpu(s->sectors))) {
164 static bool extent_has_stripe_ptr(struct bkey_s_c k, u64 idx)
167 case KEY_TYPE_extent: {
168 struct bkey_s_c_extent e = bkey_s_c_to_extent(k);
169 const union bch_extent_entry *entry;
171 extent_for_each_entry(e, entry)
172 if (extent_entry_type(entry) ==
173 BCH_EXTENT_ENTRY_stripe_ptr &&
174 entry->stripe_ptr.idx == idx)
186 static void ec_stripe_buf_exit(struct ec_stripe_buf *buf)
190 for (i = 0; i < buf->key.v.nr_blocks; i++) {
191 kvpfree(buf->data[i], buf->size << 9);
196 static int ec_stripe_buf_init(struct ec_stripe_buf *buf,
197 unsigned offset, unsigned size)
199 struct bch_stripe *v = &buf->key.v;
200 unsigned csum_granularity = 1U << v->csum_granularity_bits;
201 unsigned end = offset + size;
204 BUG_ON(end > le16_to_cpu(v->sectors));
206 offset = round_down(offset, csum_granularity);
207 end = min_t(unsigned, le16_to_cpu(v->sectors),
208 round_up(end, csum_granularity));
210 buf->offset = offset;
211 buf->size = end - offset;
213 memset(buf->valid, 0xFF, sizeof(buf->valid));
215 for (i = 0; i < buf->key.v.nr_blocks; i++) {
216 buf->data[i] = kvpmalloc(buf->size << 9, GFP_KERNEL);
223 ec_stripe_buf_exit(buf);
229 static struct bch_csum ec_block_checksum(struct ec_stripe_buf *buf,
230 unsigned block, unsigned offset)
232 struct bch_stripe *v = &buf->key.v;
233 unsigned csum_granularity = 1 << v->csum_granularity_bits;
234 unsigned end = buf->offset + buf->size;
235 unsigned len = min(csum_granularity, end - offset);
237 BUG_ON(offset >= end);
238 BUG_ON(offset < buf->offset);
239 BUG_ON(offset & (csum_granularity - 1));
240 BUG_ON(offset + len != le16_to_cpu(v->sectors) &&
241 (len & (csum_granularity - 1)));
243 return bch2_checksum(NULL, v->csum_type,
245 buf->data[block] + ((offset - buf->offset) << 9),
249 static void ec_generate_checksums(struct ec_stripe_buf *buf)
251 struct bch_stripe *v = &buf->key.v;
252 unsigned i, j, csums_per_device = stripe_csums_per_device(v);
258 BUG_ON(buf->size != le16_to_cpu(v->sectors));
260 for (i = 0; i < v->nr_blocks; i++)
261 for (j = 0; j < csums_per_device; j++)
262 stripe_csum_set(v, i, j,
263 ec_block_checksum(buf, i, j << v->csum_granularity_bits));
266 static void ec_validate_checksums(struct bch_fs *c, struct ec_stripe_buf *buf)
268 struct bch_stripe *v = &buf->key.v;
269 unsigned csum_granularity = 1 << v->csum_granularity_bits;
275 for (i = 0; i < v->nr_blocks; i++) {
276 unsigned offset = buf->offset;
277 unsigned end = buf->offset + buf->size;
279 if (!test_bit(i, buf->valid))
282 while (offset < end) {
283 unsigned j = offset >> v->csum_granularity_bits;
284 unsigned len = min(csum_granularity, end - offset);
285 struct bch_csum want = stripe_csum_get(v, i, j);
286 struct bch_csum got = ec_block_checksum(buf, i, offset);
288 if (bch2_crc_cmp(want, got)) {
289 struct printbuf buf2 = PRINTBUF;
291 bch2_bkey_val_to_text(&buf2, c, bkey_i_to_s_c(&buf->key.k_i));
293 bch_err_ratelimited(c,
294 "stripe checksum error for %ps at %u:%u: csum type %u, expected %llx got %llx\n%s",
295 (void *) _RET_IP_, i, j, v->csum_type,
296 want.lo, got.lo, buf2.buf);
297 printbuf_exit(&buf2);
298 clear_bit(i, buf->valid);
307 /* Erasure coding: */
309 static void ec_generate_ec(struct ec_stripe_buf *buf)
311 struct bch_stripe *v = &buf->key.v;
312 unsigned nr_data = v->nr_blocks - v->nr_redundant;
313 unsigned bytes = le16_to_cpu(v->sectors) << 9;
315 raid_gen(nr_data, v->nr_redundant, bytes, buf->data);
318 static unsigned ec_nr_failed(struct ec_stripe_buf *buf)
320 return buf->key.v.nr_blocks -
321 bitmap_weight(buf->valid, buf->key.v.nr_blocks);
324 static int ec_do_recov(struct bch_fs *c, struct ec_stripe_buf *buf)
326 struct bch_stripe *v = &buf->key.v;
327 unsigned i, failed[BCH_BKEY_PTRS_MAX], nr_failed = 0;
328 unsigned nr_data = v->nr_blocks - v->nr_redundant;
329 unsigned bytes = buf->size << 9;
331 if (ec_nr_failed(buf) > v->nr_redundant) {
332 bch_err_ratelimited(c,
333 "error doing reconstruct read: unable to read enough blocks");
337 for (i = 0; i < nr_data; i++)
338 if (!test_bit(i, buf->valid))
339 failed[nr_failed++] = i;
341 raid_rec(nr_failed, failed, nr_data, v->nr_redundant, bytes, buf->data);
347 static void ec_block_endio(struct bio *bio)
349 struct ec_bio *ec_bio = container_of(bio, struct ec_bio, bio);
350 struct bch_stripe *v = &ec_bio->buf->key.v;
351 struct bch_extent_ptr *ptr = &v->ptrs[ec_bio->idx];
352 struct bch_dev *ca = ec_bio->ca;
353 struct closure *cl = bio->bi_private;
355 if (bch2_dev_io_err_on(bio->bi_status, ca, "erasure coding %s error: %s",
356 bio_data_dir(bio) ? "write" : "read",
357 bch2_blk_status_to_str(bio->bi_status)))
358 clear_bit(ec_bio->idx, ec_bio->buf->valid);
360 if (ptr_stale(ca, ptr)) {
361 bch_err_ratelimited(ca->fs,
362 "error %s stripe: stale pointer after io",
363 bio_data_dir(bio) == READ ? "reading from" : "writing to");
364 clear_bit(ec_bio->idx, ec_bio->buf->valid);
367 bio_put(&ec_bio->bio);
368 percpu_ref_put(&ca->io_ref);
372 static void ec_block_io(struct bch_fs *c, struct ec_stripe_buf *buf,
373 unsigned rw, unsigned idx, struct closure *cl)
375 struct bch_stripe *v = &buf->key.v;
376 unsigned offset = 0, bytes = buf->size << 9;
377 struct bch_extent_ptr *ptr = &v->ptrs[idx];
378 struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
379 enum bch_data_type data_type = idx < buf->key.v.nr_blocks - buf->key.v.nr_redundant
383 if (ptr_stale(ca, ptr)) {
384 bch_err_ratelimited(c,
385 "error %s stripe: stale pointer",
386 rw == READ ? "reading from" : "writing to");
387 clear_bit(idx, buf->valid);
391 if (!bch2_dev_get_ioref(ca, rw)) {
392 clear_bit(idx, buf->valid);
396 this_cpu_add(ca->io_done->sectors[rw][data_type], buf->size);
398 while (offset < bytes) {
399 unsigned nr_iovecs = min_t(size_t, BIO_MAX_VECS,
400 DIV_ROUND_UP(bytes, PAGE_SIZE));
401 unsigned b = min_t(size_t, bytes - offset,
402 nr_iovecs << PAGE_SHIFT);
403 struct ec_bio *ec_bio;
405 ec_bio = container_of(bio_alloc_bioset(GFP_KERNEL, nr_iovecs,
413 bio_set_dev(&ec_bio->bio, ca->disk_sb.bdev);
414 bio_set_op_attrs(&ec_bio->bio, rw, 0);
416 ec_bio->bio.bi_iter.bi_sector = ptr->offset + buf->offset + (offset >> 9);
417 ec_bio->bio.bi_end_io = ec_block_endio;
418 ec_bio->bio.bi_private = cl;
420 bch2_bio_map(&ec_bio->bio, buf->data[idx] + offset, b);
423 percpu_ref_get(&ca->io_ref);
425 submit_bio(&ec_bio->bio);
430 percpu_ref_put(&ca->io_ref);
433 static int get_stripe_key(struct bch_fs *c, u64 idx, struct ec_stripe_buf *stripe)
435 struct btree_trans trans;
436 struct btree_iter iter;
440 bch2_trans_init(&trans, c, 0, 0);
441 bch2_trans_iter_init(&trans, &iter, BTREE_ID_stripes,
442 POS(0, idx), BTREE_ITER_SLOTS);
443 k = bch2_btree_iter_peek_slot(&iter);
447 if (k.k->type != KEY_TYPE_stripe) {
451 bkey_reassemble(&stripe->key.k_i, k);
453 bch2_trans_iter_exit(&trans, &iter);
454 bch2_trans_exit(&trans);
458 /* recovery read path: */
459 int bch2_ec_read_extent(struct bch_fs *c, struct bch_read_bio *rbio)
461 struct ec_stripe_buf *buf;
463 struct bch_stripe *v;
467 closure_init_stack(&cl);
469 BUG_ON(!rbio->pick.has_ec);
471 buf = kzalloc(sizeof(*buf), GFP_NOIO);
475 ret = get_stripe_key(c, rbio->pick.ec.idx, buf);
477 bch_err_ratelimited(c,
478 "error doing reconstruct read: error %i looking up stripe", ret);
485 if (!bch2_ptr_matches_stripe(v, rbio->pick)) {
486 bch_err_ratelimited(c,
487 "error doing reconstruct read: pointer doesn't match stripe");
492 offset = rbio->bio.bi_iter.bi_sector - v->ptrs[rbio->pick.ec.block].offset;
493 if (offset + bio_sectors(&rbio->bio) > le16_to_cpu(v->sectors)) {
494 bch_err_ratelimited(c,
495 "error doing reconstruct read: read is bigger than stripe");
500 ret = ec_stripe_buf_init(buf, offset, bio_sectors(&rbio->bio));
504 for (i = 0; i < v->nr_blocks; i++)
505 ec_block_io(c, buf, REQ_OP_READ, i, &cl);
509 if (ec_nr_failed(buf) > v->nr_redundant) {
510 bch_err_ratelimited(c,
511 "error doing reconstruct read: unable to read enough blocks");
516 ec_validate_checksums(c, buf);
518 ret = ec_do_recov(c, buf);
522 memcpy_to_bio(&rbio->bio, rbio->bio.bi_iter,
523 buf->data[rbio->pick.ec.block] + ((offset - buf->offset) << 9));
525 ec_stripe_buf_exit(buf);
530 /* stripe bucket accounting: */
532 static int __ec_stripe_mem_alloc(struct bch_fs *c, size_t idx, gfp_t gfp)
534 ec_stripes_heap n, *h = &c->ec_stripes_heap;
536 if (idx >= h->size) {
537 if (!init_heap(&n, max(1024UL, roundup_pow_of_two(idx + 1)), gfp))
540 spin_lock(&c->ec_stripes_heap_lock);
541 if (n.size > h->size) {
542 memcpy(n.data, h->data, h->used * sizeof(h->data[0]));
546 spin_unlock(&c->ec_stripes_heap_lock);
551 if (!genradix_ptr_alloc(&c->stripes, idx, gfp))
554 if (c->gc_pos.phase != GC_PHASE_NOT_RUNNING &&
555 !genradix_ptr_alloc(&c->gc_stripes, idx, gfp))
561 static int ec_stripe_mem_alloc(struct btree_trans *trans,
562 struct btree_iter *iter)
564 size_t idx = iter->pos.offset;
567 if (!__ec_stripe_mem_alloc(trans->c, idx, GFP_NOWAIT|__GFP_NOWARN))
570 bch2_trans_unlock(trans);
573 if (!__ec_stripe_mem_alloc(trans->c, idx, GFP_KERNEL))
579 static ssize_t stripe_idx_to_delete(struct bch_fs *c)
581 ec_stripes_heap *h = &c->ec_stripes_heap;
583 return h->used && h->data[0].blocks_nonempty == 0
584 ? h->data[0].idx : -1;
587 static inline int ec_stripes_heap_cmp(ec_stripes_heap *h,
588 struct ec_stripe_heap_entry l,
589 struct ec_stripe_heap_entry r)
591 return ((l.blocks_nonempty > r.blocks_nonempty) -
592 (l.blocks_nonempty < r.blocks_nonempty));
595 static inline void ec_stripes_heap_set_backpointer(ec_stripes_heap *h,
598 struct bch_fs *c = container_of(h, struct bch_fs, ec_stripes_heap);
600 genradix_ptr(&c->stripes, h->data[i].idx)->heap_idx = i;
603 static void heap_verify_backpointer(struct bch_fs *c, size_t idx)
605 ec_stripes_heap *h = &c->ec_stripes_heap;
606 struct stripe *m = genradix_ptr(&c->stripes, idx);
609 BUG_ON(m->heap_idx >= h->used);
610 BUG_ON(h->data[m->heap_idx].idx != idx);
613 void bch2_stripes_heap_del(struct bch_fs *c,
614 struct stripe *m, size_t idx)
621 heap_verify_backpointer(c, idx);
623 heap_del(&c->ec_stripes_heap, m->heap_idx,
625 ec_stripes_heap_set_backpointer);
628 void bch2_stripes_heap_insert(struct bch_fs *c,
629 struct stripe *m, size_t idx)
634 BUG_ON(heap_full(&c->ec_stripes_heap));
638 heap_add(&c->ec_stripes_heap, ((struct ec_stripe_heap_entry) {
640 .blocks_nonempty = m->blocks_nonempty,
643 ec_stripes_heap_set_backpointer);
645 heap_verify_backpointer(c, idx);
648 void bch2_stripes_heap_update(struct bch_fs *c,
649 struct stripe *m, size_t idx)
651 ec_stripes_heap *h = &c->ec_stripes_heap;
657 heap_verify_backpointer(c, idx);
659 h->data[m->heap_idx].blocks_nonempty = m->blocks_nonempty;
662 heap_sift_up(h, i, ec_stripes_heap_cmp,
663 ec_stripes_heap_set_backpointer);
664 heap_sift_down(h, i, ec_stripes_heap_cmp,
665 ec_stripes_heap_set_backpointer);
667 heap_verify_backpointer(c, idx);
669 if (stripe_idx_to_delete(c) >= 0 &&
670 !percpu_ref_is_dying(&c->writes))
671 schedule_work(&c->ec_stripe_delete_work);
674 /* stripe deletion */
676 static int ec_stripe_delete(struct bch_fs *c, size_t idx)
678 return bch2_btree_delete_range(c, BTREE_ID_stripes,
684 static void ec_stripe_delete_work(struct work_struct *work)
687 container_of(work, struct bch_fs, ec_stripe_delete_work);
691 spin_lock(&c->ec_stripes_heap_lock);
692 idx = stripe_idx_to_delete(c);
694 spin_unlock(&c->ec_stripes_heap_lock);
698 bch2_stripes_heap_del(c, genradix_ptr(&c->stripes, idx), idx);
699 spin_unlock(&c->ec_stripes_heap_lock);
701 if (ec_stripe_delete(c, idx))
706 /* stripe creation: */
708 static int ec_stripe_bkey_insert(struct btree_trans *trans,
709 struct bkey_i_stripe *stripe,
710 struct disk_reservation *res)
712 struct bch_fs *c = trans->c;
713 struct btree_iter iter;
715 struct bpos min_pos = POS(0, 1);
716 struct bpos start_pos = bpos_max(min_pos, POS(0, c->ec_stripe_hint));
719 for_each_btree_key(trans, iter, BTREE_ID_stripes, start_pos,
720 BTREE_ITER_SLOTS|BTREE_ITER_INTENT, k, ret) {
721 if (bkey_cmp(k.k->p, POS(0, U32_MAX)) > 0) {
722 if (start_pos.offset) {
724 bch2_btree_iter_set_pos(&iter, start_pos);
732 if (bkey_deleted(k.k))
738 start_pos = iter.pos;
740 ret = ec_stripe_mem_alloc(trans, &iter);
744 stripe->k.p = iter.pos;
746 ret = bch2_trans_update(trans, &iter, &stripe->k_i, 0);
748 c->ec_stripe_hint = start_pos.offset;
750 bch2_trans_iter_exit(trans, &iter);
755 static int ec_stripe_bkey_update(struct btree_trans *trans,
756 struct bkey_i_stripe *new,
757 struct disk_reservation *res)
759 struct btree_iter iter;
761 const struct bch_stripe *existing;
765 bch2_trans_iter_init(trans, &iter, BTREE_ID_stripes,
766 new->k.p, BTREE_ITER_INTENT);
767 k = bch2_btree_iter_peek_slot(&iter);
772 if (!k.k || k.k->type != KEY_TYPE_stripe) {
773 bch_err(trans->c, "error updating stripe: not found");
778 existing = bkey_s_c_to_stripe(k).v;
780 if (existing->nr_blocks != new->v.nr_blocks) {
781 bch_err(trans->c, "error updating stripe: nr_blocks does not match");
786 for (i = 0; i < new->v.nr_blocks; i++)
787 stripe_blockcount_set(&new->v, i,
788 stripe_blockcount_get(existing, i));
790 ret = bch2_trans_update(trans, &iter, &new->k_i, 0);
792 bch2_trans_iter_exit(trans, &iter);
796 static void extent_stripe_ptr_add(struct bkey_s_extent e,
797 struct ec_stripe_buf *s,
798 struct bch_extent_ptr *ptr,
801 struct bch_extent_stripe_ptr *dst = (void *) ptr;
802 union bch_extent_entry *end = extent_entry_last(e);
804 memmove_u64s_up(dst + 1, dst, (u64 *) end - (u64 *) dst);
805 e.k->u64s += sizeof(*dst) / sizeof(u64);
807 *dst = (struct bch_extent_stripe_ptr) {
808 .type = 1 << BCH_EXTENT_ENTRY_stripe_ptr,
810 .redundancy = s->key.v.nr_redundant,
811 .idx = s->key.k.p.offset,
815 static int ec_stripe_update_ptrs(struct bch_fs *c,
816 struct ec_stripe_buf *s,
819 struct btree_trans trans;
820 struct btree_iter iter;
822 struct bkey_s_extent e;
824 struct bpos next_pos;
825 int ret = 0, dev, block;
827 bch2_bkey_buf_init(&sk);
828 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
830 /* XXX this doesn't support the reflink btree */
832 bch2_trans_iter_init(&trans, &iter, BTREE_ID_extents,
836 while (bch2_trans_begin(&trans),
837 (k = bch2_btree_iter_peek(&iter)).k &&
838 !(ret = bkey_err(k)) &&
839 bkey_cmp(bkey_start_pos(k.k), pos->p) < 0) {
840 const struct bch_extent_ptr *ptr_c;
841 struct bch_extent_ptr *ptr, *ec_ptr = NULL;
843 if (extent_has_stripe_ptr(k, s->key.k.p.offset)) {
844 bch2_btree_iter_advance(&iter);
848 ptr_c = bkey_matches_stripe(&s->key.v, k, &block);
850 * It doesn't generally make sense to erasure code cached ptrs:
851 * XXX: should we be incrementing a counter?
853 if (!ptr_c || ptr_c->cached) {
854 bch2_btree_iter_advance(&iter);
858 dev = s->key.v.ptrs[block].dev;
860 bch2_bkey_buf_reassemble(&sk, c, k);
861 e = bkey_i_to_s_extent(sk.k);
863 bch2_bkey_drop_ptrs(e.s, ptr, ptr->dev != dev);
864 ec_ptr = (void *) bch2_bkey_has_device(e.s_c, dev);
867 extent_stripe_ptr_add(e, s, ec_ptr, block);
869 bch2_btree_iter_set_pos(&iter, bkey_start_pos(&sk.k->k));
870 next_pos = sk.k->k.p;
872 ret = bch2_btree_iter_traverse(&iter) ?:
873 bch2_trans_update(&trans, &iter, sk.k, 0) ?:
874 bch2_trans_commit(&trans, NULL, NULL,
875 BTREE_INSERT_NOFAIL);
877 bch2_btree_iter_set_pos(&iter, next_pos);
883 bch2_trans_iter_exit(&trans, &iter);
885 bch2_trans_exit(&trans);
886 bch2_bkey_buf_exit(&sk, c);
892 * data buckets of new stripe all written: create the stripe
894 static void ec_stripe_create(struct ec_stripe_new *s)
896 struct bch_fs *c = s->c;
897 struct open_bucket *ob;
900 struct bch_stripe *v = &s->new_stripe.key.v;
901 unsigned i, nr_data = v->nr_blocks - v->nr_redundant;
904 BUG_ON(s->h->s == s);
906 closure_sync(&s->iodone);
909 if (s->err != -EROFS)
910 bch_err(c, "error creating stripe: error writing data buckets");
914 if (s->have_existing_stripe) {
915 ec_validate_checksums(c, &s->existing_stripe);
917 if (ec_do_recov(c, &s->existing_stripe)) {
918 bch_err(c, "error creating stripe: error reading existing stripe");
922 for (i = 0; i < nr_data; i++)
923 if (stripe_blockcount_get(&s->existing_stripe.key.v, i))
924 swap(s->new_stripe.data[i],
925 s->existing_stripe.data[i]);
927 ec_stripe_buf_exit(&s->existing_stripe);
930 BUG_ON(!s->allocated);
932 if (!percpu_ref_tryget(&c->writes))
935 ec_generate_ec(&s->new_stripe);
937 ec_generate_checksums(&s->new_stripe);
940 for (i = nr_data; i < v->nr_blocks; i++)
941 ec_block_io(c, &s->new_stripe, REQ_OP_WRITE, i, &s->iodone);
942 closure_sync(&s->iodone);
944 if (ec_nr_failed(&s->new_stripe)) {
945 bch_err(c, "error creating stripe: error writing redundancy buckets");
949 ret = bch2_trans_do(c, &s->res, NULL, BTREE_INSERT_NOFAIL,
950 s->have_existing_stripe
951 ? ec_stripe_bkey_update(&trans, &s->new_stripe.key, &s->res)
952 : ec_stripe_bkey_insert(&trans, &s->new_stripe.key, &s->res));
954 bch_err(c, "error creating stripe: error creating stripe key");
958 for_each_keylist_key(&s->keys, k) {
959 ret = ec_stripe_update_ptrs(c, &s->new_stripe, &k->k);
961 bch_err(c, "error creating stripe: error %i updating pointers", ret);
966 spin_lock(&c->ec_stripes_heap_lock);
967 m = genradix_ptr(&c->stripes, s->new_stripe.key.k.p.offset);
970 bch2_stripes_heap_insert(c, m, s->new_stripe.key.k.p.offset);
971 spin_unlock(&c->ec_stripes_heap_lock);
973 percpu_ref_put(&c->writes);
975 bch2_disk_reservation_put(c, &s->res);
977 for (i = 0; i < v->nr_blocks; i++)
979 ob = c->open_buckets + s->blocks[i];
983 __bch2_open_bucket_put(c, ob);
985 bch2_open_bucket_put(c, ob);
989 bch2_keylist_free(&s->keys, s->inline_keys);
991 ec_stripe_buf_exit(&s->existing_stripe);
992 ec_stripe_buf_exit(&s->new_stripe);
993 closure_debug_destroy(&s->iodone);
997 static void ec_stripe_create_work(struct work_struct *work)
999 struct bch_fs *c = container_of(work,
1000 struct bch_fs, ec_stripe_create_work);
1001 struct ec_stripe_new *s, *n;
1003 mutex_lock(&c->ec_stripe_new_lock);
1004 list_for_each_entry_safe(s, n, &c->ec_stripe_new_list, list)
1005 if (!atomic_read(&s->pin)) {
1007 mutex_unlock(&c->ec_stripe_new_lock);
1008 ec_stripe_create(s);
1011 mutex_unlock(&c->ec_stripe_new_lock);
1014 static void ec_stripe_new_put(struct bch_fs *c, struct ec_stripe_new *s)
1016 BUG_ON(atomic_read(&s->pin) <= 0);
1018 if (atomic_dec_and_test(&s->pin)) {
1019 BUG_ON(!s->pending);
1020 queue_work(system_long_wq, &c->ec_stripe_create_work);
1024 static void ec_stripe_set_pending(struct bch_fs *c, struct ec_stripe_head *h)
1026 struct ec_stripe_new *s = h->s;
1028 BUG_ON(!s->allocated && !s->err);
1033 mutex_lock(&c->ec_stripe_new_lock);
1034 list_add(&s->list, &c->ec_stripe_new_list);
1035 mutex_unlock(&c->ec_stripe_new_lock);
1037 ec_stripe_new_put(c, s);
1040 /* have a full bucket - hand it off to be erasure coded: */
1041 void bch2_ec_bucket_written(struct bch_fs *c, struct open_bucket *ob)
1043 struct ec_stripe_new *s = ob->ec;
1045 if (ob->sectors_free)
1048 ec_stripe_new_put(c, s);
1051 void bch2_ec_bucket_cancel(struct bch_fs *c, struct open_bucket *ob)
1053 struct ec_stripe_new *s = ob->ec;
1058 void *bch2_writepoint_ec_buf(struct bch_fs *c, struct write_point *wp)
1060 struct open_bucket *ob = ec_open_bucket(c, &wp->ptrs);
1067 ca = bch_dev_bkey_exists(c, ob->dev);
1068 offset = ca->mi.bucket_size - ob->sectors_free;
1070 return ob->ec->new_stripe.data[ob->ec_idx] + (offset << 9);
1073 void bch2_ob_add_backpointer(struct bch_fs *c, struct open_bucket *ob,
1076 struct ec_stripe_new *ec = ob->ec;
1081 mutex_lock(&ec->lock);
1083 if (bch2_keylist_realloc(&ec->keys, ec->inline_keys,
1084 ARRAY_SIZE(ec->inline_keys),
1089 bkey_init(&ec->keys.top->k);
1090 ec->keys.top->k.p = k->p;
1091 ec->keys.top->k.size = k->size;
1092 bch2_keylist_push(&ec->keys);
1094 mutex_unlock(&ec->lock);
1097 static int unsigned_cmp(const void *_l, const void *_r)
1099 unsigned l = *((const unsigned *) _l);
1100 unsigned r = *((const unsigned *) _r);
1102 return cmp_int(l, r);
1105 /* pick most common bucket size: */
1106 static unsigned pick_blocksize(struct bch_fs *c,
1107 struct bch_devs_mask *devs)
1110 unsigned i, nr = 0, sizes[BCH_SB_MEMBERS_MAX];
1113 } cur = { 0, 0 }, best = { 0, 0 };
1115 for_each_member_device_rcu(ca, c, i, devs)
1116 sizes[nr++] = ca->mi.bucket_size;
1118 sort(sizes, nr, sizeof(unsigned), unsigned_cmp, NULL);
1120 for (i = 0; i < nr; i++) {
1121 if (sizes[i] != cur.size) {
1122 if (cur.nr > best.nr)
1126 cur.size = sizes[i];
1132 if (cur.nr > best.nr)
1138 static bool may_create_new_stripe(struct bch_fs *c)
1143 static void ec_stripe_key_init(struct bch_fs *c,
1144 struct bkey_i_stripe *s,
1147 unsigned stripe_size)
1151 bkey_stripe_init(&s->k_i);
1152 s->v.sectors = cpu_to_le16(stripe_size);
1154 s->v.nr_blocks = nr_data + nr_parity;
1155 s->v.nr_redundant = nr_parity;
1156 s->v.csum_granularity_bits = ilog2(c->opts.encoded_extent_max >> 9);
1157 s->v.csum_type = BCH_CSUM_crc32c;
1160 while ((u64s = stripe_val_u64s(&s->v)) > BKEY_VAL_U64s_MAX) {
1161 BUG_ON(1 << s->v.csum_granularity_bits >=
1162 le16_to_cpu(s->v.sectors) ||
1163 s->v.csum_granularity_bits == U8_MAX);
1164 s->v.csum_granularity_bits++;
1167 set_bkey_val_u64s(&s->k, u64s);
1170 static int ec_new_stripe_alloc(struct bch_fs *c, struct ec_stripe_head *h)
1172 struct ec_stripe_new *s;
1174 lockdep_assert_held(&h->lock);
1176 s = kzalloc(sizeof(*s), GFP_KERNEL);
1180 mutex_init(&s->lock);
1181 closure_init(&s->iodone, NULL);
1182 atomic_set(&s->pin, 1);
1185 s->nr_data = min_t(unsigned, h->nr_active_devs,
1186 BCH_BKEY_PTRS_MAX) - h->redundancy;
1187 s->nr_parity = h->redundancy;
1189 bch2_keylist_init(&s->keys, s->inline_keys);
1191 ec_stripe_key_init(c, &s->new_stripe.key, s->nr_data,
1192 s->nr_parity, h->blocksize);
1198 static struct ec_stripe_head *
1199 ec_new_stripe_head_alloc(struct bch_fs *c, unsigned target,
1200 unsigned algo, unsigned redundancy,
1203 struct ec_stripe_head *h;
1207 h = kzalloc(sizeof(*h), GFP_KERNEL);
1211 mutex_init(&h->lock);
1212 mutex_lock(&h->lock);
1216 h->redundancy = redundancy;
1220 h->devs = target_rw_devs(c, BCH_DATA_user, target);
1222 for_each_member_device_rcu(ca, c, i, &h->devs)
1223 if (!ca->mi.durability)
1224 __clear_bit(i, h->devs.d);
1226 h->blocksize = pick_blocksize(c, &h->devs);
1228 for_each_member_device_rcu(ca, c, i, &h->devs)
1229 if (ca->mi.bucket_size == h->blocksize)
1230 h->nr_active_devs++;
1233 list_add(&h->list, &c->ec_stripe_head_list);
1237 void bch2_ec_stripe_head_put(struct bch_fs *c, struct ec_stripe_head *h)
1241 bitmap_weight(h->s->blocks_allocated,
1242 h->s->nr_data) == h->s->nr_data)
1243 ec_stripe_set_pending(c, h);
1245 mutex_unlock(&h->lock);
1248 struct ec_stripe_head *__bch2_ec_stripe_head_get(struct bch_fs *c,
1251 unsigned redundancy,
1254 struct ec_stripe_head *h;
1259 mutex_lock(&c->ec_stripe_head_lock);
1260 list_for_each_entry(h, &c->ec_stripe_head_list, list)
1261 if (h->target == target &&
1263 h->redundancy == redundancy &&
1264 h->copygc == copygc) {
1265 mutex_lock(&h->lock);
1269 h = ec_new_stripe_head_alloc(c, target, algo, redundancy, copygc);
1271 mutex_unlock(&c->ec_stripe_head_lock);
1275 static int new_stripe_alloc_buckets(struct bch_fs *c, struct ec_stripe_head *h,
1278 struct bch_devs_mask devs = h->devs;
1279 struct open_bucket *ob;
1280 struct open_buckets buckets;
1281 unsigned i, j, nr_have_parity = 0, nr_have_data = 0;
1282 bool have_cache = true;
1285 for (i = 0; i < h->s->new_stripe.key.v.nr_blocks; i++) {
1286 if (test_bit(i, h->s->blocks_gotten)) {
1287 __clear_bit(h->s->new_stripe.key.v.ptrs[i].dev, devs.d);
1288 if (i < h->s->nr_data)
1295 BUG_ON(nr_have_data > h->s->nr_data);
1296 BUG_ON(nr_have_parity > h->s->nr_parity);
1299 if (nr_have_parity < h->s->nr_parity) {
1300 ret = bch2_bucket_alloc_set(c, &buckets,
1312 open_bucket_for_each(c, &buckets, ob, i) {
1313 j = find_next_zero_bit(h->s->blocks_gotten,
1314 h->s->nr_data + h->s->nr_parity,
1316 BUG_ON(j >= h->s->nr_data + h->s->nr_parity);
1318 h->s->blocks[j] = buckets.v[i];
1319 h->s->new_stripe.key.v.ptrs[j] = bch2_ob_ptr(c, ob);
1320 __set_bit(j, h->s->blocks_gotten);
1328 if (nr_have_data < h->s->nr_data) {
1329 ret = bch2_bucket_alloc_set(c, &buckets,
1341 open_bucket_for_each(c, &buckets, ob, i) {
1342 j = find_next_zero_bit(h->s->blocks_gotten,
1344 BUG_ON(j >= h->s->nr_data);
1346 h->s->blocks[j] = buckets.v[i];
1347 h->s->new_stripe.key.v.ptrs[j] = bch2_ob_ptr(c, ob);
1348 __set_bit(j, h->s->blocks_gotten);
1358 /* XXX: doesn't obey target: */
1359 static s64 get_existing_stripe(struct bch_fs *c,
1360 struct ec_stripe_head *head)
1362 ec_stripes_heap *h = &c->ec_stripes_heap;
1368 if (may_create_new_stripe(c))
1371 spin_lock(&c->ec_stripes_heap_lock);
1372 for (heap_idx = 0; heap_idx < h->used; heap_idx++) {
1373 /* No blocks worth reusing, stripe will just be deleted: */
1374 if (!h->data[heap_idx].blocks_nonempty)
1377 stripe_idx = h->data[heap_idx].idx;
1378 m = genradix_ptr(&c->stripes, stripe_idx);
1380 if (m->algorithm == head->algo &&
1381 m->nr_redundant == head->redundancy &&
1382 m->sectors == head->blocksize &&
1383 m->blocks_nonempty < m->nr_blocks - m->nr_redundant) {
1384 bch2_stripes_heap_del(c, m, stripe_idx);
1389 spin_unlock(&c->ec_stripes_heap_lock);
1393 static int __bch2_ec_stripe_head_reuse(struct bch_fs *c,
1394 struct ec_stripe_head *h)
1400 idx = get_existing_stripe(c, h);
1402 bch_err(c, "failed to find an existing stripe");
1406 h->s->have_existing_stripe = true;
1407 ret = get_stripe_key(c, idx, &h->s->existing_stripe);
1409 bch2_fs_fatal_error(c, "error reading stripe key: %i", ret);
1413 if (ec_stripe_buf_init(&h->s->existing_stripe, 0, h->blocksize)) {
1415 * this is a problem: we have deleted from the
1416 * stripes heap already
1421 BUG_ON(h->s->existing_stripe.size != h->blocksize);
1422 BUG_ON(h->s->existing_stripe.size != h->s->existing_stripe.key.v.sectors);
1424 for (i = 0; i < h->s->existing_stripe.key.v.nr_blocks; i++) {
1425 if (stripe_blockcount_get(&h->s->existing_stripe.key.v, i)) {
1426 __set_bit(i, h->s->blocks_gotten);
1427 __set_bit(i, h->s->blocks_allocated);
1430 ec_block_io(c, &h->s->existing_stripe, READ, i, &h->s->iodone);
1433 bkey_copy(&h->s->new_stripe.key.k_i,
1434 &h->s->existing_stripe.key.k_i);
1439 static int __bch2_ec_stripe_head_reserve(struct bch_fs *c,
1440 struct ec_stripe_head *h)
1444 ret = bch2_disk_reservation_get(c, &h->s->res,
1446 h->s->nr_parity, 0);
1450 * This means we need to wait for copygc to
1451 * empty out buckets from existing stripes:
1453 bch_err(c, "failed to reserve stripe");
1459 struct ec_stripe_head *bch2_ec_stripe_head_get(struct bch_fs *c,
1462 unsigned redundancy,
1466 struct ec_stripe_head *h;
1468 bool needs_stripe_new;
1470 h = __bch2_ec_stripe_head_get(c, target, algo, redundancy, copygc);
1472 bch_err(c, "no stripe head");
1476 needs_stripe_new = !h->s;
1477 if (needs_stripe_new) {
1478 if (ec_new_stripe_alloc(c, h)) {
1480 bch_err(c, "failed to allocate new stripe");
1484 if (ec_stripe_buf_init(&h->s->new_stripe, 0, h->blocksize))
1489 * Try reserve a new stripe before reusing an
1490 * existing stripe. This will prevent unnecessary
1491 * read amplification during write oriented workloads.
1494 if (!h->s->allocated && !h->s->res.sectors && !h->s->have_existing_stripe)
1495 ret = __bch2_ec_stripe_head_reserve(c, h);
1496 if (ret && needs_stripe_new)
1497 ret = __bch2_ec_stripe_head_reuse(c, h);
1501 if (!h->s->allocated) {
1502 ret = new_stripe_alloc_buckets(c, h, cl);
1506 h->s->allocated = true;
1512 bch2_ec_stripe_head_put(c, h);
1513 return ERR_PTR(ret);
1516 void bch2_ec_stop_dev(struct bch_fs *c, struct bch_dev *ca)
1518 struct ec_stripe_head *h;
1519 struct open_bucket *ob;
1522 mutex_lock(&c->ec_stripe_head_lock);
1523 list_for_each_entry(h, &c->ec_stripe_head_list, list) {
1525 mutex_lock(&h->lock);
1529 for (i = 0; i < h->s->new_stripe.key.v.nr_blocks; i++) {
1530 if (!h->s->blocks[i])
1533 ob = c->open_buckets + h->s->blocks[i];
1534 if (ob->dev == ca->dev_idx)
1540 ec_stripe_set_pending(c, h);
1542 mutex_unlock(&h->lock);
1544 mutex_unlock(&c->ec_stripe_head_lock);
1547 void bch2_stripes_heap_start(struct bch_fs *c)
1549 struct genradix_iter iter;
1552 genradix_for_each(&c->stripes, iter, m)
1554 bch2_stripes_heap_insert(c, m, iter.pos);
1557 int bch2_stripes_read(struct bch_fs *c)
1559 struct btree_trans trans;
1560 struct btree_iter iter;
1562 const struct bch_stripe *s;
1567 bch2_trans_init(&trans, c, 0, 0);
1569 for_each_btree_key(&trans, iter, BTREE_ID_stripes, POS_MIN,
1570 BTREE_ITER_PREFETCH, k, ret) {
1571 if (k.k->type != KEY_TYPE_stripe)
1574 ret = __ec_stripe_mem_alloc(c, k.k->p.offset, GFP_KERNEL);
1578 s = bkey_s_c_to_stripe(k).v;
1580 m = genradix_ptr(&c->stripes, k.k->p.offset);
1582 m->sectors = le16_to_cpu(s->sectors);
1583 m->algorithm = s->algorithm;
1584 m->nr_blocks = s->nr_blocks;
1585 m->nr_redundant = s->nr_redundant;
1586 m->blocks_nonempty = 0;
1588 for (i = 0; i < s->nr_blocks; i++)
1589 m->blocks_nonempty += !!stripe_blockcount_get(s, i);
1591 spin_lock(&c->ec_stripes_heap_lock);
1592 bch2_stripes_heap_update(c, m, k.k->p.offset);
1593 spin_unlock(&c->ec_stripes_heap_lock);
1595 bch2_trans_iter_exit(&trans, &iter);
1597 bch2_trans_exit(&trans);
1600 bch_err(c, "error reading stripes: %i", ret);
1605 void bch2_stripes_heap_to_text(struct printbuf *out, struct bch_fs *c)
1607 ec_stripes_heap *h = &c->ec_stripes_heap;
1611 spin_lock(&c->ec_stripes_heap_lock);
1612 for (i = 0; i < min_t(size_t, h->used, 20); i++) {
1613 m = genradix_ptr(&c->stripes, h->data[i].idx);
1615 pr_buf(out, "%zu %u/%u+%u\n", h->data[i].idx,
1616 h->data[i].blocks_nonempty,
1617 m->nr_blocks - m->nr_redundant,
1620 spin_unlock(&c->ec_stripes_heap_lock);
1623 void bch2_new_stripes_to_text(struct printbuf *out, struct bch_fs *c)
1625 struct ec_stripe_head *h;
1626 struct ec_stripe_new *s;
1628 mutex_lock(&c->ec_stripe_head_lock);
1629 list_for_each_entry(h, &c->ec_stripe_head_list, list) {
1630 pr_buf(out, "target %u algo %u redundancy %u:\n",
1631 h->target, h->algo, h->redundancy);
1634 pr_buf(out, "\tpending: blocks %u+%u allocated %u\n",
1635 h->s->nr_data, h->s->nr_parity,
1636 bitmap_weight(h->s->blocks_allocated,
1639 mutex_unlock(&c->ec_stripe_head_lock);
1641 mutex_lock(&c->ec_stripe_new_lock);
1642 list_for_each_entry(s, &c->ec_stripe_new_list, list) {
1643 pr_buf(out, "\tin flight: blocks %u+%u pin %u\n",
1644 s->nr_data, s->nr_parity,
1645 atomic_read(&s->pin));
1647 mutex_unlock(&c->ec_stripe_new_lock);
1650 void bch2_fs_ec_exit(struct bch_fs *c)
1652 struct ec_stripe_head *h;
1655 mutex_lock(&c->ec_stripe_head_lock);
1656 h = list_first_entry_or_null(&c->ec_stripe_head_list,
1657 struct ec_stripe_head, list);
1660 mutex_unlock(&c->ec_stripe_head_lock);
1668 BUG_ON(!list_empty(&c->ec_stripe_new_list));
1670 free_heap(&c->ec_stripes_heap);
1671 genradix_free(&c->stripes);
1672 bioset_exit(&c->ec_bioset);
1675 int bch2_fs_ec_init(struct bch_fs *c)
1677 INIT_WORK(&c->ec_stripe_create_work, ec_stripe_create_work);
1678 INIT_WORK(&c->ec_stripe_delete_work, ec_stripe_delete_work);
1680 return bioset_init(&c->ec_bioset, 1, offsetof(struct ec_bio, bio),