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 int bch2_stripe_invalid(const struct bch_fs *c, struct bkey_s_c k,
106 int rw, struct printbuf *err)
108 const struct bch_stripe *s = bkey_s_c_to_stripe(k).v;
110 if (!bkey_cmp(k.k->p, POS_MIN)) {
111 prt_printf(err, "stripe at POS_MIN");
116 prt_printf(err, "nonzero inode field");
120 if (bkey_val_bytes(k.k) < sizeof(*s)) {
121 prt_printf(err, "incorrect value size (%zu < %zu)",
122 bkey_val_bytes(k.k), sizeof(*s));
126 if (bkey_val_u64s(k.k) < stripe_val_u64s(s)) {
127 prt_printf(err, "incorrect value size (%zu < %u)",
128 bkey_val_u64s(k.k), stripe_val_u64s(s));
132 return bch2_bkey_ptrs_invalid(c, k, rw, err);
135 void bch2_stripe_to_text(struct printbuf *out, struct bch_fs *c,
138 const struct bch_stripe *s = bkey_s_c_to_stripe(k).v;
141 prt_printf(out, "algo %u sectors %u blocks %u:%u csum %u gran %u",
143 le16_to_cpu(s->sectors),
144 s->nr_blocks - s->nr_redundant,
147 1U << s->csum_granularity_bits);
149 for (i = 0; i < s->nr_blocks; i++)
150 prt_printf(out, " %u:%llu:%u", s->ptrs[i].dev,
151 (u64) s->ptrs[i].offset,
152 stripe_blockcount_get(s, i));
155 /* returns blocknr in stripe that we matched: */
156 static const struct bch_extent_ptr *bkey_matches_stripe(struct bch_stripe *s,
157 struct bkey_s_c k, unsigned *block)
159 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
160 const struct bch_extent_ptr *ptr;
161 unsigned i, nr_data = s->nr_blocks - s->nr_redundant;
163 bkey_for_each_ptr(ptrs, ptr)
164 for (i = 0; i < nr_data; i++)
165 if (__bch2_ptr_matches_stripe(&s->ptrs[i], ptr,
166 le16_to_cpu(s->sectors))) {
174 static bool extent_has_stripe_ptr(struct bkey_s_c k, u64 idx)
177 case KEY_TYPE_extent: {
178 struct bkey_s_c_extent e = bkey_s_c_to_extent(k);
179 const union bch_extent_entry *entry;
181 extent_for_each_entry(e, entry)
182 if (extent_entry_type(entry) ==
183 BCH_EXTENT_ENTRY_stripe_ptr &&
184 entry->stripe_ptr.idx == idx)
196 static void ec_stripe_buf_exit(struct ec_stripe_buf *buf)
200 for (i = 0; i < buf->key.v.nr_blocks; i++) {
201 kvpfree(buf->data[i], buf->size << 9);
206 static int ec_stripe_buf_init(struct ec_stripe_buf *buf,
207 unsigned offset, unsigned size)
209 struct bch_stripe *v = &buf->key.v;
210 unsigned csum_granularity = 1U << v->csum_granularity_bits;
211 unsigned end = offset + size;
214 BUG_ON(end > le16_to_cpu(v->sectors));
216 offset = round_down(offset, csum_granularity);
217 end = min_t(unsigned, le16_to_cpu(v->sectors),
218 round_up(end, csum_granularity));
220 buf->offset = offset;
221 buf->size = end - offset;
223 memset(buf->valid, 0xFF, sizeof(buf->valid));
225 for (i = 0; i < buf->key.v.nr_blocks; i++) {
226 buf->data[i] = kvpmalloc(buf->size << 9, GFP_KERNEL);
233 ec_stripe_buf_exit(buf);
239 static struct bch_csum ec_block_checksum(struct ec_stripe_buf *buf,
240 unsigned block, unsigned offset)
242 struct bch_stripe *v = &buf->key.v;
243 unsigned csum_granularity = 1 << v->csum_granularity_bits;
244 unsigned end = buf->offset + buf->size;
245 unsigned len = min(csum_granularity, end - offset);
247 BUG_ON(offset >= end);
248 BUG_ON(offset < buf->offset);
249 BUG_ON(offset & (csum_granularity - 1));
250 BUG_ON(offset + len != le16_to_cpu(v->sectors) &&
251 (len & (csum_granularity - 1)));
253 return bch2_checksum(NULL, v->csum_type,
255 buf->data[block] + ((offset - buf->offset) << 9),
259 static void ec_generate_checksums(struct ec_stripe_buf *buf)
261 struct bch_stripe *v = &buf->key.v;
262 unsigned i, j, csums_per_device = stripe_csums_per_device(v);
268 BUG_ON(buf->size != le16_to_cpu(v->sectors));
270 for (i = 0; i < v->nr_blocks; i++)
271 for (j = 0; j < csums_per_device; j++)
272 stripe_csum_set(v, i, j,
273 ec_block_checksum(buf, i, j << v->csum_granularity_bits));
276 static void ec_validate_checksums(struct bch_fs *c, struct ec_stripe_buf *buf)
278 struct bch_stripe *v = &buf->key.v;
279 unsigned csum_granularity = 1 << v->csum_granularity_bits;
285 for (i = 0; i < v->nr_blocks; i++) {
286 unsigned offset = buf->offset;
287 unsigned end = buf->offset + buf->size;
289 if (!test_bit(i, buf->valid))
292 while (offset < end) {
293 unsigned j = offset >> v->csum_granularity_bits;
294 unsigned len = min(csum_granularity, end - offset);
295 struct bch_csum want = stripe_csum_get(v, i, j);
296 struct bch_csum got = ec_block_checksum(buf, i, offset);
298 if (bch2_crc_cmp(want, got)) {
299 struct printbuf buf2 = PRINTBUF;
301 bch2_bkey_val_to_text(&buf2, c, bkey_i_to_s_c(&buf->key.k_i));
303 bch_err_ratelimited(c,
304 "stripe checksum error for %ps at %u:%u: csum type %u, expected %llx got %llx\n%s",
305 (void *) _RET_IP_, i, j, v->csum_type,
306 want.lo, got.lo, buf2.buf);
307 printbuf_exit(&buf2);
308 clear_bit(i, buf->valid);
317 /* Erasure coding: */
319 static void ec_generate_ec(struct ec_stripe_buf *buf)
321 struct bch_stripe *v = &buf->key.v;
322 unsigned nr_data = v->nr_blocks - v->nr_redundant;
323 unsigned bytes = le16_to_cpu(v->sectors) << 9;
325 raid_gen(nr_data, v->nr_redundant, bytes, buf->data);
328 static unsigned ec_nr_failed(struct ec_stripe_buf *buf)
330 return buf->key.v.nr_blocks -
331 bitmap_weight(buf->valid, buf->key.v.nr_blocks);
334 static int ec_do_recov(struct bch_fs *c, struct ec_stripe_buf *buf)
336 struct bch_stripe *v = &buf->key.v;
337 unsigned i, failed[BCH_BKEY_PTRS_MAX], nr_failed = 0;
338 unsigned nr_data = v->nr_blocks - v->nr_redundant;
339 unsigned bytes = buf->size << 9;
341 if (ec_nr_failed(buf) > v->nr_redundant) {
342 bch_err_ratelimited(c,
343 "error doing reconstruct read: unable to read enough blocks");
347 for (i = 0; i < nr_data; i++)
348 if (!test_bit(i, buf->valid))
349 failed[nr_failed++] = i;
351 raid_rec(nr_failed, failed, nr_data, v->nr_redundant, bytes, buf->data);
357 static void ec_block_endio(struct bio *bio)
359 struct ec_bio *ec_bio = container_of(bio, struct ec_bio, bio);
360 struct bch_stripe *v = &ec_bio->buf->key.v;
361 struct bch_extent_ptr *ptr = &v->ptrs[ec_bio->idx];
362 struct bch_dev *ca = ec_bio->ca;
363 struct closure *cl = bio->bi_private;
365 if (bch2_dev_io_err_on(bio->bi_status, ca, "erasure coding %s error: %s",
366 bio_data_dir(bio) ? "write" : "read",
367 bch2_blk_status_to_str(bio->bi_status)))
368 clear_bit(ec_bio->idx, ec_bio->buf->valid);
370 if (ptr_stale(ca, ptr)) {
371 bch_err_ratelimited(ca->fs,
372 "error %s stripe: stale pointer after io",
373 bio_data_dir(bio) == READ ? "reading from" : "writing to");
374 clear_bit(ec_bio->idx, ec_bio->buf->valid);
377 bio_put(&ec_bio->bio);
378 percpu_ref_put(&ca->io_ref);
382 static void ec_block_io(struct bch_fs *c, struct ec_stripe_buf *buf,
383 unsigned rw, unsigned idx, struct closure *cl)
385 struct bch_stripe *v = &buf->key.v;
386 unsigned offset = 0, bytes = buf->size << 9;
387 struct bch_extent_ptr *ptr = &v->ptrs[idx];
388 struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
389 enum bch_data_type data_type = idx < buf->key.v.nr_blocks - buf->key.v.nr_redundant
393 if (ptr_stale(ca, ptr)) {
394 bch_err_ratelimited(c,
395 "error %s stripe: stale pointer",
396 rw == READ ? "reading from" : "writing to");
397 clear_bit(idx, buf->valid);
401 if (!bch2_dev_get_ioref(ca, rw)) {
402 clear_bit(idx, buf->valid);
406 this_cpu_add(ca->io_done->sectors[rw][data_type], buf->size);
408 while (offset < bytes) {
409 unsigned nr_iovecs = min_t(size_t, BIO_MAX_VECS,
410 DIV_ROUND_UP(bytes, PAGE_SIZE));
411 unsigned b = min_t(size_t, bytes - offset,
412 nr_iovecs << PAGE_SHIFT);
413 struct ec_bio *ec_bio;
415 ec_bio = container_of(bio_alloc_bioset(ca->disk_sb.bdev,
426 ec_bio->bio.bi_iter.bi_sector = ptr->offset + buf->offset + (offset >> 9);
427 ec_bio->bio.bi_end_io = ec_block_endio;
428 ec_bio->bio.bi_private = cl;
430 bch2_bio_map(&ec_bio->bio, buf->data[idx] + offset, b);
433 percpu_ref_get(&ca->io_ref);
435 submit_bio(&ec_bio->bio);
440 percpu_ref_put(&ca->io_ref);
443 static int get_stripe_key(struct bch_fs *c, u64 idx, struct ec_stripe_buf *stripe)
445 struct btree_trans trans;
446 struct btree_iter iter;
450 bch2_trans_init(&trans, c, 0, 0);
451 bch2_trans_iter_init(&trans, &iter, BTREE_ID_stripes,
452 POS(0, idx), BTREE_ITER_SLOTS);
453 k = bch2_btree_iter_peek_slot(&iter);
457 if (k.k->type != KEY_TYPE_stripe) {
461 bkey_reassemble(&stripe->key.k_i, k);
463 bch2_trans_iter_exit(&trans, &iter);
464 bch2_trans_exit(&trans);
468 /* recovery read path: */
469 int bch2_ec_read_extent(struct bch_fs *c, struct bch_read_bio *rbio)
471 struct ec_stripe_buf *buf;
473 struct bch_stripe *v;
477 closure_init_stack(&cl);
479 BUG_ON(!rbio->pick.has_ec);
481 buf = kzalloc(sizeof(*buf), GFP_NOIO);
485 ret = get_stripe_key(c, rbio->pick.ec.idx, buf);
487 bch_err_ratelimited(c,
488 "error doing reconstruct read: error %i looking up stripe", ret);
495 if (!bch2_ptr_matches_stripe(v, rbio->pick)) {
496 bch_err_ratelimited(c,
497 "error doing reconstruct read: pointer doesn't match stripe");
502 offset = rbio->bio.bi_iter.bi_sector - v->ptrs[rbio->pick.ec.block].offset;
503 if (offset + bio_sectors(&rbio->bio) > le16_to_cpu(v->sectors)) {
504 bch_err_ratelimited(c,
505 "error doing reconstruct read: read is bigger than stripe");
510 ret = ec_stripe_buf_init(buf, offset, bio_sectors(&rbio->bio));
514 for (i = 0; i < v->nr_blocks; i++)
515 ec_block_io(c, buf, REQ_OP_READ, i, &cl);
519 if (ec_nr_failed(buf) > v->nr_redundant) {
520 bch_err_ratelimited(c,
521 "error doing reconstruct read: unable to read enough blocks");
526 ec_validate_checksums(c, buf);
528 ret = ec_do_recov(c, buf);
532 memcpy_to_bio(&rbio->bio, rbio->bio.bi_iter,
533 buf->data[rbio->pick.ec.block] + ((offset - buf->offset) << 9));
535 ec_stripe_buf_exit(buf);
540 /* stripe bucket accounting: */
542 static int __ec_stripe_mem_alloc(struct bch_fs *c, size_t idx, gfp_t gfp)
544 ec_stripes_heap n, *h = &c->ec_stripes_heap;
546 if (idx >= h->size) {
547 if (!init_heap(&n, max(1024UL, roundup_pow_of_two(idx + 1)), gfp))
550 spin_lock(&c->ec_stripes_heap_lock);
551 if (n.size > h->size) {
552 memcpy(n.data, h->data, h->used * sizeof(h->data[0]));
556 spin_unlock(&c->ec_stripes_heap_lock);
561 if (!genradix_ptr_alloc(&c->stripes, idx, gfp))
564 if (c->gc_pos.phase != GC_PHASE_NOT_RUNNING &&
565 !genradix_ptr_alloc(&c->gc_stripes, idx, gfp))
571 static int ec_stripe_mem_alloc(struct btree_trans *trans,
572 struct btree_iter *iter)
574 size_t idx = iter->pos.offset;
576 if (!__ec_stripe_mem_alloc(trans->c, idx, GFP_NOWAIT|__GFP_NOWARN))
579 bch2_trans_unlock(trans);
581 return __ec_stripe_mem_alloc(trans->c, idx, GFP_KERNEL) ?:
582 bch2_trans_relock(trans);
585 static ssize_t stripe_idx_to_delete(struct bch_fs *c)
587 ec_stripes_heap *h = &c->ec_stripes_heap;
589 return h->used && h->data[0].blocks_nonempty == 0
590 ? h->data[0].idx : -1;
593 static inline int ec_stripes_heap_cmp(ec_stripes_heap *h,
594 struct ec_stripe_heap_entry l,
595 struct ec_stripe_heap_entry r)
597 return ((l.blocks_nonempty > r.blocks_nonempty) -
598 (l.blocks_nonempty < r.blocks_nonempty));
601 static inline void ec_stripes_heap_set_backpointer(ec_stripes_heap *h,
604 struct bch_fs *c = container_of(h, struct bch_fs, ec_stripes_heap);
606 genradix_ptr(&c->stripes, h->data[i].idx)->heap_idx = i;
609 static void heap_verify_backpointer(struct bch_fs *c, size_t idx)
611 ec_stripes_heap *h = &c->ec_stripes_heap;
612 struct stripe *m = genradix_ptr(&c->stripes, idx);
615 BUG_ON(m->heap_idx >= h->used);
616 BUG_ON(h->data[m->heap_idx].idx != idx);
619 void bch2_stripes_heap_del(struct bch_fs *c,
620 struct stripe *m, size_t idx)
627 heap_verify_backpointer(c, idx);
629 heap_del(&c->ec_stripes_heap, m->heap_idx,
631 ec_stripes_heap_set_backpointer);
634 void bch2_stripes_heap_insert(struct bch_fs *c,
635 struct stripe *m, size_t idx)
640 BUG_ON(heap_full(&c->ec_stripes_heap));
644 heap_add(&c->ec_stripes_heap, ((struct ec_stripe_heap_entry) {
646 .blocks_nonempty = m->blocks_nonempty,
649 ec_stripes_heap_set_backpointer);
651 heap_verify_backpointer(c, idx);
654 void bch2_stripes_heap_update(struct bch_fs *c,
655 struct stripe *m, size_t idx)
657 ec_stripes_heap *h = &c->ec_stripes_heap;
663 heap_verify_backpointer(c, idx);
665 h->data[m->heap_idx].blocks_nonempty = m->blocks_nonempty;
668 heap_sift_up(h, i, ec_stripes_heap_cmp,
669 ec_stripes_heap_set_backpointer);
670 heap_sift_down(h, i, ec_stripes_heap_cmp,
671 ec_stripes_heap_set_backpointer);
673 heap_verify_backpointer(c, idx);
675 if (stripe_idx_to_delete(c) >= 0 &&
676 !percpu_ref_is_dying(&c->writes))
677 schedule_work(&c->ec_stripe_delete_work);
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))
712 /* stripe creation: */
714 static int ec_stripe_bkey_insert(struct btree_trans *trans,
715 struct bkey_i_stripe *stripe,
716 struct disk_reservation *res)
718 struct bch_fs *c = trans->c;
719 struct btree_iter iter;
721 struct bpos min_pos = POS(0, 1);
722 struct bpos start_pos = bpos_max(min_pos, POS(0, c->ec_stripe_hint));
725 for_each_btree_key_norestart(trans, iter, BTREE_ID_stripes, start_pos,
726 BTREE_ITER_SLOTS|BTREE_ITER_INTENT, k, ret) {
727 if (bkey_cmp(k.k->p, POS(0, U32_MAX)) > 0) {
728 if (start_pos.offset) {
730 bch2_btree_iter_set_pos(&iter, start_pos);
734 ret = -BCH_ERR_ENOSPC_stripe_create;
738 if (bkey_deleted(k.k))
742 c->ec_stripe_hint = iter.pos.offset;
747 ret = ec_stripe_mem_alloc(trans, &iter);
751 stripe->k.p = iter.pos;
753 ret = bch2_trans_update(trans, &iter, &stripe->k_i, 0);
755 bch2_trans_iter_exit(trans, &iter);
760 static int ec_stripe_bkey_update(struct btree_trans *trans,
761 struct bkey_i_stripe *new,
762 struct disk_reservation *res)
764 struct btree_iter iter;
766 const struct bch_stripe *existing;
770 bch2_trans_iter_init(trans, &iter, BTREE_ID_stripes,
771 new->k.p, BTREE_ITER_INTENT);
772 k = bch2_btree_iter_peek_slot(&iter);
777 if (!k.k || k.k->type != KEY_TYPE_stripe) {
778 bch_err(trans->c, "error updating stripe: not found");
783 existing = bkey_s_c_to_stripe(k).v;
785 if (existing->nr_blocks != new->v.nr_blocks) {
786 bch_err(trans->c, "error updating stripe: nr_blocks does not match");
791 for (i = 0; i < new->v.nr_blocks; i++)
792 stripe_blockcount_set(&new->v, i,
793 stripe_blockcount_get(existing, i));
795 ret = bch2_trans_update(trans, &iter, &new->k_i, 0);
797 bch2_trans_iter_exit(trans, &iter);
801 static void extent_stripe_ptr_add(struct bkey_s_extent e,
802 struct ec_stripe_buf *s,
803 struct bch_extent_ptr *ptr,
806 struct bch_extent_stripe_ptr *dst = (void *) ptr;
807 union bch_extent_entry *end = extent_entry_last(e);
809 memmove_u64s_up(dst + 1, dst, (u64 *) end - (u64 *) dst);
810 e.k->u64s += sizeof(*dst) / sizeof(u64);
812 *dst = (struct bch_extent_stripe_ptr) {
813 .type = 1 << BCH_EXTENT_ENTRY_stripe_ptr,
815 .redundancy = s->key.v.nr_redundant,
816 .idx = s->key.k.p.offset,
820 static int ec_stripe_update_extent(struct btree_trans *trans,
821 struct btree_iter *iter,
823 struct ec_stripe_buf *s,
826 const struct bch_extent_ptr *ptr_c;
827 struct bch_extent_ptr *ptr, *ec_ptr = NULL;
831 if (bkey_cmp(bkey_start_pos(k.k), end) >= 0)
834 if (extent_has_stripe_ptr(k, s->key.k.p.offset))
837 ptr_c = bkey_matches_stripe(&s->key.v, k, &block);
839 * It doesn't generally make sense to erasure code cached ptrs:
840 * XXX: should we be incrementing a counter?
842 if (!ptr_c || ptr_c->cached)
845 dev = s->key.v.ptrs[block].dev;
847 n = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
848 ret = PTR_ERR_OR_ZERO(n);
852 bkey_reassemble(n, k);
854 bch2_bkey_drop_ptrs(bkey_i_to_s(n), ptr, ptr->dev != dev);
855 ec_ptr = (void *) bch2_bkey_has_device(bkey_i_to_s_c(n), dev);
858 extent_stripe_ptr_add(bkey_i_to_s_extent(n), s, ec_ptr, block);
860 return bch2_trans_update(trans, iter, n, 0);
863 static int ec_stripe_update_extents(struct bch_fs *c,
864 struct ec_stripe_buf *s,
867 struct btree_iter iter;
870 return bch2_trans_run(c,
871 for_each_btree_key_commit(&trans, iter,
872 BTREE_ID_extents, bkey_start_pos(pos),
873 BTREE_ITER_NOT_EXTENTS|BTREE_ITER_INTENT, k,
874 NULL, NULL, BTREE_INSERT_NOFAIL,
875 ec_stripe_update_extent(&trans, &iter, k, s, pos->p)));
879 * data buckets of new stripe all written: create the stripe
881 static void ec_stripe_create(struct ec_stripe_new *s)
883 struct bch_fs *c = s->c;
884 struct open_bucket *ob;
887 struct bch_stripe *v = &s->new_stripe.key.v;
888 unsigned i, nr_data = v->nr_blocks - v->nr_redundant;
891 BUG_ON(s->h->s == s);
893 closure_sync(&s->iodone);
896 if (s->err != -EROFS)
897 bch_err(c, "error creating stripe: error writing data buckets");
901 if (s->have_existing_stripe) {
902 ec_validate_checksums(c, &s->existing_stripe);
904 if (ec_do_recov(c, &s->existing_stripe)) {
905 bch_err(c, "error creating stripe: error reading existing stripe");
909 for (i = 0; i < nr_data; i++)
910 if (stripe_blockcount_get(&s->existing_stripe.key.v, i))
911 swap(s->new_stripe.data[i],
912 s->existing_stripe.data[i]);
914 ec_stripe_buf_exit(&s->existing_stripe);
917 BUG_ON(!s->allocated);
919 if (!percpu_ref_tryget_live(&c->writes))
922 ec_generate_ec(&s->new_stripe);
924 ec_generate_checksums(&s->new_stripe);
927 for (i = nr_data; i < v->nr_blocks; i++)
928 ec_block_io(c, &s->new_stripe, REQ_OP_WRITE, i, &s->iodone);
929 closure_sync(&s->iodone);
931 if (ec_nr_failed(&s->new_stripe)) {
932 bch_err(c, "error creating stripe: error writing redundancy buckets");
936 ret = bch2_trans_do(c, &s->res, NULL, BTREE_INSERT_NOFAIL,
937 s->have_existing_stripe
938 ? ec_stripe_bkey_update(&trans, &s->new_stripe.key, &s->res)
939 : ec_stripe_bkey_insert(&trans, &s->new_stripe.key, &s->res));
941 bch_err(c, "error creating stripe: error creating stripe key");
945 for_each_keylist_key(&s->keys, k) {
946 ret = ec_stripe_update_extents(c, &s->new_stripe, &k->k);
948 bch_err(c, "error creating stripe: error updating pointers: %s",
954 spin_lock(&c->ec_stripes_heap_lock);
955 m = genradix_ptr(&c->stripes, s->new_stripe.key.k.p.offset);
958 bch2_stripes_heap_insert(c, m, s->new_stripe.key.k.p.offset);
959 spin_unlock(&c->ec_stripes_heap_lock);
961 percpu_ref_put(&c->writes);
963 bch2_disk_reservation_put(c, &s->res);
965 for (i = 0; i < v->nr_blocks; i++)
967 ob = c->open_buckets + s->blocks[i];
971 __bch2_open_bucket_put(c, ob);
973 bch2_open_bucket_put(c, ob);
977 bch2_keylist_free(&s->keys, s->inline_keys);
979 ec_stripe_buf_exit(&s->existing_stripe);
980 ec_stripe_buf_exit(&s->new_stripe);
981 closure_debug_destroy(&s->iodone);
985 static void ec_stripe_create_work(struct work_struct *work)
987 struct bch_fs *c = container_of(work,
988 struct bch_fs, ec_stripe_create_work);
989 struct ec_stripe_new *s, *n;
991 mutex_lock(&c->ec_stripe_new_lock);
992 list_for_each_entry_safe(s, n, &c->ec_stripe_new_list, list)
993 if (!atomic_read(&s->pin)) {
995 mutex_unlock(&c->ec_stripe_new_lock);
999 mutex_unlock(&c->ec_stripe_new_lock);
1002 static void ec_stripe_new_put(struct bch_fs *c, struct ec_stripe_new *s)
1004 BUG_ON(atomic_read(&s->pin) <= 0);
1006 if (atomic_dec_and_test(&s->pin)) {
1007 BUG_ON(!s->pending);
1008 queue_work(system_long_wq, &c->ec_stripe_create_work);
1012 static void ec_stripe_set_pending(struct bch_fs *c, struct ec_stripe_head *h)
1014 struct ec_stripe_new *s = h->s;
1016 BUG_ON(!s->allocated && !s->err);
1021 mutex_lock(&c->ec_stripe_new_lock);
1022 list_add(&s->list, &c->ec_stripe_new_list);
1023 mutex_unlock(&c->ec_stripe_new_lock);
1025 ec_stripe_new_put(c, s);
1028 /* have a full bucket - hand it off to be erasure coded: */
1029 void bch2_ec_bucket_written(struct bch_fs *c, struct open_bucket *ob)
1031 struct ec_stripe_new *s = ob->ec;
1033 if (ob->sectors_free)
1036 ec_stripe_new_put(c, s);
1039 void bch2_ec_bucket_cancel(struct bch_fs *c, struct open_bucket *ob)
1041 struct ec_stripe_new *s = ob->ec;
1046 void *bch2_writepoint_ec_buf(struct bch_fs *c, struct write_point *wp)
1048 struct open_bucket *ob = ec_open_bucket(c, &wp->ptrs);
1055 ca = bch_dev_bkey_exists(c, ob->dev);
1056 offset = ca->mi.bucket_size - ob->sectors_free;
1058 return ob->ec->new_stripe.data[ob->ec_idx] + (offset << 9);
1061 void bch2_ob_add_backpointer(struct bch_fs *c, struct open_bucket *ob,
1064 struct ec_stripe_new *ec = ob->ec;
1069 mutex_lock(&ec->lock);
1071 if (bch2_keylist_realloc(&ec->keys, ec->inline_keys,
1072 ARRAY_SIZE(ec->inline_keys),
1077 bkey_init(&ec->keys.top->k);
1078 ec->keys.top->k.p = k->p;
1079 ec->keys.top->k.size = k->size;
1080 bch2_keylist_push(&ec->keys);
1082 mutex_unlock(&ec->lock);
1085 static int unsigned_cmp(const void *_l, const void *_r)
1087 unsigned l = *((const unsigned *) _l);
1088 unsigned r = *((const unsigned *) _r);
1090 return cmp_int(l, r);
1093 /* pick most common bucket size: */
1094 static unsigned pick_blocksize(struct bch_fs *c,
1095 struct bch_devs_mask *devs)
1098 unsigned i, nr = 0, sizes[BCH_SB_MEMBERS_MAX];
1101 } cur = { 0, 0 }, best = { 0, 0 };
1103 for_each_member_device_rcu(ca, c, i, devs)
1104 sizes[nr++] = ca->mi.bucket_size;
1106 sort(sizes, nr, sizeof(unsigned), unsigned_cmp, NULL);
1108 for (i = 0; i < nr; i++) {
1109 if (sizes[i] != cur.size) {
1110 if (cur.nr > best.nr)
1114 cur.size = sizes[i];
1120 if (cur.nr > best.nr)
1126 static bool may_create_new_stripe(struct bch_fs *c)
1131 static void ec_stripe_key_init(struct bch_fs *c,
1132 struct bkey_i_stripe *s,
1135 unsigned stripe_size)
1139 bkey_stripe_init(&s->k_i);
1140 s->v.sectors = cpu_to_le16(stripe_size);
1142 s->v.nr_blocks = nr_data + nr_parity;
1143 s->v.nr_redundant = nr_parity;
1144 s->v.csum_granularity_bits = ilog2(c->opts.encoded_extent_max >> 9);
1145 s->v.csum_type = BCH_CSUM_crc32c;
1148 while ((u64s = stripe_val_u64s(&s->v)) > BKEY_VAL_U64s_MAX) {
1149 BUG_ON(1 << s->v.csum_granularity_bits >=
1150 le16_to_cpu(s->v.sectors) ||
1151 s->v.csum_granularity_bits == U8_MAX);
1152 s->v.csum_granularity_bits++;
1155 set_bkey_val_u64s(&s->k, u64s);
1158 static int ec_new_stripe_alloc(struct bch_fs *c, struct ec_stripe_head *h)
1160 struct ec_stripe_new *s;
1162 lockdep_assert_held(&h->lock);
1164 s = kzalloc(sizeof(*s), GFP_KERNEL);
1168 mutex_init(&s->lock);
1169 closure_init(&s->iodone, NULL);
1170 atomic_set(&s->pin, 1);
1173 s->nr_data = min_t(unsigned, h->nr_active_devs,
1174 BCH_BKEY_PTRS_MAX) - h->redundancy;
1175 s->nr_parity = h->redundancy;
1177 bch2_keylist_init(&s->keys, s->inline_keys);
1179 ec_stripe_key_init(c, &s->new_stripe.key, s->nr_data,
1180 s->nr_parity, h->blocksize);
1186 static struct ec_stripe_head *
1187 ec_new_stripe_head_alloc(struct bch_fs *c, unsigned target,
1188 unsigned algo, unsigned redundancy,
1191 struct ec_stripe_head *h;
1195 h = kzalloc(sizeof(*h), GFP_KERNEL);
1199 mutex_init(&h->lock);
1200 mutex_lock(&h->lock);
1204 h->redundancy = redundancy;
1208 h->devs = target_rw_devs(c, BCH_DATA_user, target);
1210 for_each_member_device_rcu(ca, c, i, &h->devs)
1211 if (!ca->mi.durability)
1212 __clear_bit(i, h->devs.d);
1214 h->blocksize = pick_blocksize(c, &h->devs);
1216 for_each_member_device_rcu(ca, c, i, &h->devs)
1217 if (ca->mi.bucket_size == h->blocksize)
1218 h->nr_active_devs++;
1221 list_add(&h->list, &c->ec_stripe_head_list);
1225 void bch2_ec_stripe_head_put(struct bch_fs *c, struct ec_stripe_head *h)
1229 bitmap_weight(h->s->blocks_allocated,
1230 h->s->nr_data) == h->s->nr_data)
1231 ec_stripe_set_pending(c, h);
1233 mutex_unlock(&h->lock);
1236 struct ec_stripe_head *__bch2_ec_stripe_head_get(struct bch_fs *c,
1239 unsigned redundancy,
1242 struct ec_stripe_head *h;
1247 mutex_lock(&c->ec_stripe_head_lock);
1248 list_for_each_entry(h, &c->ec_stripe_head_list, list)
1249 if (h->target == target &&
1251 h->redundancy == redundancy &&
1252 h->copygc == copygc) {
1253 mutex_lock(&h->lock);
1257 h = ec_new_stripe_head_alloc(c, target, algo, redundancy, copygc);
1259 mutex_unlock(&c->ec_stripe_head_lock);
1263 static int new_stripe_alloc_buckets(struct bch_fs *c, struct ec_stripe_head *h,
1266 struct bch_devs_mask devs = h->devs;
1267 struct open_bucket *ob;
1268 struct open_buckets buckets;
1269 unsigned i, j, nr_have_parity = 0, nr_have_data = 0;
1270 bool have_cache = true;
1273 for (i = 0; i < h->s->new_stripe.key.v.nr_blocks; i++) {
1274 if (test_bit(i, h->s->blocks_gotten)) {
1275 __clear_bit(h->s->new_stripe.key.v.ptrs[i].dev, devs.d);
1276 if (i < h->s->nr_data)
1283 BUG_ON(nr_have_data > h->s->nr_data);
1284 BUG_ON(nr_have_parity > h->s->nr_parity);
1287 if (nr_have_parity < h->s->nr_parity) {
1288 ret = bch2_bucket_alloc_set(c, &buckets,
1300 open_bucket_for_each(c, &buckets, ob, i) {
1301 j = find_next_zero_bit(h->s->blocks_gotten,
1302 h->s->nr_data + h->s->nr_parity,
1304 BUG_ON(j >= h->s->nr_data + h->s->nr_parity);
1306 h->s->blocks[j] = buckets.v[i];
1307 h->s->new_stripe.key.v.ptrs[j] = bch2_ob_ptr(c, ob);
1308 __set_bit(j, h->s->blocks_gotten);
1316 if (nr_have_data < h->s->nr_data) {
1317 ret = bch2_bucket_alloc_set(c, &buckets,
1329 open_bucket_for_each(c, &buckets, ob, i) {
1330 j = find_next_zero_bit(h->s->blocks_gotten,
1332 BUG_ON(j >= h->s->nr_data);
1334 h->s->blocks[j] = buckets.v[i];
1335 h->s->new_stripe.key.v.ptrs[j] = bch2_ob_ptr(c, ob);
1336 __set_bit(j, h->s->blocks_gotten);
1346 /* XXX: doesn't obey target: */
1347 static s64 get_existing_stripe(struct bch_fs *c,
1348 struct ec_stripe_head *head)
1350 ec_stripes_heap *h = &c->ec_stripes_heap;
1356 if (may_create_new_stripe(c))
1359 spin_lock(&c->ec_stripes_heap_lock);
1360 for (heap_idx = 0; heap_idx < h->used; heap_idx++) {
1361 /* No blocks worth reusing, stripe will just be deleted: */
1362 if (!h->data[heap_idx].blocks_nonempty)
1365 stripe_idx = h->data[heap_idx].idx;
1366 m = genradix_ptr(&c->stripes, stripe_idx);
1368 if (m->algorithm == head->algo &&
1369 m->nr_redundant == head->redundancy &&
1370 m->sectors == head->blocksize &&
1371 m->blocks_nonempty < m->nr_blocks - m->nr_redundant) {
1372 bch2_stripes_heap_del(c, m, stripe_idx);
1377 spin_unlock(&c->ec_stripes_heap_lock);
1381 static int __bch2_ec_stripe_head_reuse(struct bch_fs *c,
1382 struct ec_stripe_head *h)
1388 idx = get_existing_stripe(c, h);
1390 bch_err(c, "failed to find an existing stripe");
1391 return -BCH_ERR_ENOSPC_stripe_reuse;
1394 h->s->have_existing_stripe = true;
1395 ret = get_stripe_key(c, idx, &h->s->existing_stripe);
1397 bch2_fs_fatal_error(c, "error reading stripe key: %i", ret);
1401 if (ec_stripe_buf_init(&h->s->existing_stripe, 0, h->blocksize)) {
1403 * this is a problem: we have deleted from the
1404 * stripes heap already
1409 BUG_ON(h->s->existing_stripe.size != h->blocksize);
1410 BUG_ON(h->s->existing_stripe.size != h->s->existing_stripe.key.v.sectors);
1412 for (i = 0; i < h->s->existing_stripe.key.v.nr_blocks; i++) {
1413 if (stripe_blockcount_get(&h->s->existing_stripe.key.v, i)) {
1414 __set_bit(i, h->s->blocks_gotten);
1415 __set_bit(i, h->s->blocks_allocated);
1418 ec_block_io(c, &h->s->existing_stripe, READ, i, &h->s->iodone);
1421 bkey_copy(&h->s->new_stripe.key.k_i,
1422 &h->s->existing_stripe.key.k_i);
1427 static int __bch2_ec_stripe_head_reserve(struct bch_fs *c,
1428 struct ec_stripe_head *h)
1432 ret = bch2_disk_reservation_get(c, &h->s->res,
1434 h->s->nr_parity, 0);
1438 * This means we need to wait for copygc to
1439 * empty out buckets from existing stripes:
1441 bch_err(c, "failed to reserve stripe");
1447 struct ec_stripe_head *bch2_ec_stripe_head_get(struct bch_fs *c,
1450 unsigned redundancy,
1454 struct ec_stripe_head *h;
1456 bool needs_stripe_new;
1458 h = __bch2_ec_stripe_head_get(c, target, algo, redundancy, copygc);
1460 bch_err(c, "no stripe head");
1464 needs_stripe_new = !h->s;
1465 if (needs_stripe_new) {
1466 if (ec_new_stripe_alloc(c, h)) {
1468 bch_err(c, "failed to allocate new stripe");
1472 if (ec_stripe_buf_init(&h->s->new_stripe, 0, h->blocksize))
1477 * Try reserve a new stripe before reusing an
1478 * existing stripe. This will prevent unnecessary
1479 * read amplification during write oriented workloads.
1482 if (!h->s->allocated && !h->s->res.sectors && !h->s->have_existing_stripe)
1483 ret = __bch2_ec_stripe_head_reserve(c, h);
1484 if (ret && needs_stripe_new)
1485 ret = __bch2_ec_stripe_head_reuse(c, h);
1489 if (!h->s->allocated) {
1490 ret = new_stripe_alloc_buckets(c, h, cl);
1494 h->s->allocated = true;
1500 bch2_ec_stripe_head_put(c, h);
1501 return ERR_PTR(ret);
1504 void bch2_ec_stop_dev(struct bch_fs *c, struct bch_dev *ca)
1506 struct ec_stripe_head *h;
1507 struct open_bucket *ob;
1510 mutex_lock(&c->ec_stripe_head_lock);
1511 list_for_each_entry(h, &c->ec_stripe_head_list, list) {
1513 mutex_lock(&h->lock);
1517 for (i = 0; i < h->s->new_stripe.key.v.nr_blocks; i++) {
1518 if (!h->s->blocks[i])
1521 ob = c->open_buckets + h->s->blocks[i];
1522 if (ob->dev == ca->dev_idx)
1528 ec_stripe_set_pending(c, h);
1530 mutex_unlock(&h->lock);
1532 mutex_unlock(&c->ec_stripe_head_lock);
1535 void bch2_stripes_heap_start(struct bch_fs *c)
1537 struct genradix_iter iter;
1540 genradix_for_each(&c->stripes, iter, m)
1542 bch2_stripes_heap_insert(c, m, iter.pos);
1545 int bch2_stripes_read(struct bch_fs *c)
1547 struct btree_trans trans;
1548 struct btree_iter iter;
1550 const struct bch_stripe *s;
1555 bch2_trans_init(&trans, c, 0, 0);
1557 for_each_btree_key(&trans, iter, BTREE_ID_stripes, POS_MIN,
1558 BTREE_ITER_PREFETCH, k, ret) {
1559 if (k.k->type != KEY_TYPE_stripe)
1562 ret = __ec_stripe_mem_alloc(c, k.k->p.offset, GFP_KERNEL);
1566 s = bkey_s_c_to_stripe(k).v;
1568 m = genradix_ptr(&c->stripes, k.k->p.offset);
1570 m->sectors = le16_to_cpu(s->sectors);
1571 m->algorithm = s->algorithm;
1572 m->nr_blocks = s->nr_blocks;
1573 m->nr_redundant = s->nr_redundant;
1574 m->blocks_nonempty = 0;
1576 for (i = 0; i < s->nr_blocks; i++)
1577 m->blocks_nonempty += !!stripe_blockcount_get(s, i);
1579 spin_lock(&c->ec_stripes_heap_lock);
1580 bch2_stripes_heap_update(c, m, k.k->p.offset);
1581 spin_unlock(&c->ec_stripes_heap_lock);
1583 bch2_trans_iter_exit(&trans, &iter);
1585 bch2_trans_exit(&trans);
1588 bch_err(c, "error reading stripes: %i", ret);
1593 void bch2_stripes_heap_to_text(struct printbuf *out, struct bch_fs *c)
1595 ec_stripes_heap *h = &c->ec_stripes_heap;
1599 spin_lock(&c->ec_stripes_heap_lock);
1600 for (i = 0; i < min_t(size_t, h->used, 20); i++) {
1601 m = genradix_ptr(&c->stripes, h->data[i].idx);
1603 prt_printf(out, "%zu %u/%u+%u\n", h->data[i].idx,
1604 h->data[i].blocks_nonempty,
1605 m->nr_blocks - m->nr_redundant,
1608 spin_unlock(&c->ec_stripes_heap_lock);
1611 void bch2_new_stripes_to_text(struct printbuf *out, struct bch_fs *c)
1613 struct ec_stripe_head *h;
1614 struct ec_stripe_new *s;
1616 mutex_lock(&c->ec_stripe_head_lock);
1617 list_for_each_entry(h, &c->ec_stripe_head_list, list) {
1618 prt_printf(out, "target %u algo %u redundancy %u:\n",
1619 h->target, h->algo, h->redundancy);
1622 prt_printf(out, "\tpending: blocks %u+%u allocated %u\n",
1623 h->s->nr_data, h->s->nr_parity,
1624 bitmap_weight(h->s->blocks_allocated,
1627 mutex_unlock(&c->ec_stripe_head_lock);
1629 mutex_lock(&c->ec_stripe_new_lock);
1630 list_for_each_entry(s, &c->ec_stripe_new_list, list) {
1631 prt_printf(out, "\tin flight: blocks %u+%u pin %u\n",
1632 s->nr_data, s->nr_parity,
1633 atomic_read(&s->pin));
1635 mutex_unlock(&c->ec_stripe_new_lock);
1638 void bch2_fs_ec_exit(struct bch_fs *c)
1640 struct ec_stripe_head *h;
1643 mutex_lock(&c->ec_stripe_head_lock);
1644 h = list_first_entry_or_null(&c->ec_stripe_head_list,
1645 struct ec_stripe_head, list);
1648 mutex_unlock(&c->ec_stripe_head_lock);
1656 BUG_ON(!list_empty(&c->ec_stripe_new_list));
1658 free_heap(&c->ec_stripes_heap);
1659 genradix_free(&c->stripes);
1660 bioset_exit(&c->ec_bioset);
1663 void bch2_fs_ec_init_early(struct bch_fs *c)
1665 INIT_WORK(&c->ec_stripe_create_work, ec_stripe_create_work);
1666 INIT_WORK(&c->ec_stripe_delete_work, ec_stripe_delete_work);
1669 int bch2_fs_ec_init(struct bch_fs *c)
1671 return bioset_init(&c->ec_bioset, 1, offsetof(struct ec_bio, bio),