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 int rw, struct printbuf *err)
109 const struct bch_stripe *s = bkey_s_c_to_stripe(k).v;
111 if (!bkey_cmp(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, rw, 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 !percpu_ref_is_dying(&c->writes))
678 schedule_work(&c->ec_stripe_delete_work);
681 /* stripe deletion */
683 static int ec_stripe_delete(struct bch_fs *c, size_t idx)
685 return bch2_btree_delete_range(c, BTREE_ID_stripes,
691 static void ec_stripe_delete_work(struct work_struct *work)
694 container_of(work, struct bch_fs, ec_stripe_delete_work);
698 spin_lock(&c->ec_stripes_heap_lock);
699 idx = stripe_idx_to_delete(c);
701 spin_unlock(&c->ec_stripes_heap_lock);
705 bch2_stripes_heap_del(c, genradix_ptr(&c->stripes, idx), idx);
706 spin_unlock(&c->ec_stripes_heap_lock);
708 if (ec_stripe_delete(c, idx))
713 /* stripe creation: */
715 static int ec_stripe_bkey_insert(struct btree_trans *trans,
716 struct bkey_i_stripe *stripe,
717 struct disk_reservation *res)
719 struct bch_fs *c = trans->c;
720 struct btree_iter iter;
722 struct bpos min_pos = POS(0, 1);
723 struct bpos start_pos = bpos_max(min_pos, POS(0, c->ec_stripe_hint));
726 for_each_btree_key_norestart(trans, iter, BTREE_ID_stripes, start_pos,
727 BTREE_ITER_SLOTS|BTREE_ITER_INTENT, k, ret) {
728 if (bkey_cmp(k.k->p, POS(0, U32_MAX)) > 0) {
729 if (start_pos.offset) {
731 bch2_btree_iter_set_pos(&iter, start_pos);
735 ret = -BCH_ERR_ENOSPC_stripe_create;
739 if (bkey_deleted(k.k))
743 c->ec_stripe_hint = iter.pos.offset;
748 ret = ec_stripe_mem_alloc(trans, &iter);
752 stripe->k.p = iter.pos;
754 ret = bch2_trans_update(trans, &iter, &stripe->k_i, 0);
756 bch2_trans_iter_exit(trans, &iter);
761 static int ec_stripe_bkey_update(struct btree_trans *trans,
762 struct bkey_i_stripe *new,
763 struct disk_reservation *res)
765 struct btree_iter iter;
767 const struct bch_stripe *existing;
771 bch2_trans_iter_init(trans, &iter, BTREE_ID_stripes,
772 new->k.p, BTREE_ITER_INTENT);
773 k = bch2_btree_iter_peek_slot(&iter);
778 if (!k.k || k.k->type != KEY_TYPE_stripe) {
779 bch_err(trans->c, "error updating stripe: not found");
784 existing = bkey_s_c_to_stripe(k).v;
786 if (existing->nr_blocks != new->v.nr_blocks) {
787 bch_err(trans->c, "error updating stripe: nr_blocks does not match");
792 for (i = 0; i < new->v.nr_blocks; i++)
793 stripe_blockcount_set(&new->v, i,
794 stripe_blockcount_get(existing, i));
796 ret = bch2_trans_update(trans, &iter, &new->k_i, 0);
798 bch2_trans_iter_exit(trans, &iter);
802 static void extent_stripe_ptr_add(struct bkey_s_extent e,
803 struct ec_stripe_buf *s,
804 struct bch_extent_ptr *ptr,
807 struct bch_extent_stripe_ptr *dst = (void *) ptr;
808 union bch_extent_entry *end = extent_entry_last(e);
810 memmove_u64s_up(dst + 1, dst, (u64 *) end - (u64 *) dst);
811 e.k->u64s += sizeof(*dst) / sizeof(u64);
813 *dst = (struct bch_extent_stripe_ptr) {
814 .type = 1 << BCH_EXTENT_ENTRY_stripe_ptr,
816 .redundancy = s->key.v.nr_redundant,
817 .idx = s->key.k.p.offset,
821 static int ec_stripe_update_extent(struct btree_trans *trans,
822 struct btree_iter *iter,
824 struct ec_stripe_buf *s)
826 const struct bch_extent_ptr *ptr_c;
827 struct bch_extent_ptr *ptr, *ec_ptr = NULL;
831 if (extent_has_stripe_ptr(k, s->key.k.p.offset))
834 ptr_c = bkey_matches_stripe(&s->key.v, k, &block);
836 * It doesn't generally make sense to erasure code cached ptrs:
837 * XXX: should we be incrementing a counter?
839 if (!ptr_c || ptr_c->cached)
842 dev = s->key.v.ptrs[block].dev;
844 n = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
845 ret = PTR_ERR_OR_ZERO(n);
849 bkey_reassemble(n, k);
851 bch2_bkey_drop_ptrs(bkey_i_to_s(n), ptr, ptr->dev != dev);
852 ec_ptr = (void *) bch2_bkey_has_device(bkey_i_to_s_c(n), dev);
855 extent_stripe_ptr_add(bkey_i_to_s_extent(n), s, ec_ptr, block);
857 return bch2_trans_update(trans, iter, n, 0);
860 static int ec_stripe_update_bucket(struct btree_trans *trans, struct ec_stripe_buf *s,
863 struct bch_fs *c = trans->c;
864 struct bch_extent_ptr bucket = s->key.v.ptrs[block];
865 struct bpos bucket_pos = PTR_BUCKET_POS(c, &bucket);
866 struct bch_backpointer bp;
867 struct btree_iter iter;
873 bch2_trans_begin(trans);
875 ret = bch2_get_next_backpointer(trans, bucket_pos, bucket.gen,
880 if (bp_offset == U64_MAX)
883 if (bch2_fs_inconsistent_on(bp.level, c, "found btree node in erasure coded bucket!?")) {
888 k = bch2_backpointer_get_key(trans, &iter, bucket_pos, bp_offset, bp);
895 ret = ec_stripe_update_extent(trans, &iter, k, s);
896 bch2_trans_iter_exit(trans, &iter);
903 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
909 static int ec_stripe_update_extents(struct bch_fs *c, struct ec_stripe_buf *s)
911 struct btree_trans trans;
912 struct bch_stripe *v = &s->key.v;
913 unsigned i, nr_data = v->nr_blocks - v->nr_redundant;
916 bch2_trans_init(&trans, c, 0, 0);
918 for (i = 0; i < nr_data; i++) {
919 ret = ec_stripe_update_bucket(&trans, s, i);
925 bch2_trans_exit(&trans);
931 * data buckets of new stripe all written: create the stripe
933 static void ec_stripe_create(struct ec_stripe_new *s)
935 struct bch_fs *c = s->c;
936 struct open_bucket *ob;
938 struct bch_stripe *v = &s->new_stripe.key.v;
939 unsigned i, nr_data = v->nr_blocks - v->nr_redundant;
942 BUG_ON(s->h->s == s);
944 closure_sync(&s->iodone);
947 if (s->err != -EROFS)
948 bch_err(c, "error creating stripe: error writing data buckets");
952 if (s->have_existing_stripe) {
953 ec_validate_checksums(c, &s->existing_stripe);
955 if (ec_do_recov(c, &s->existing_stripe)) {
956 bch_err(c, "error creating stripe: error reading existing stripe");
960 for (i = 0; i < nr_data; i++)
961 if (stripe_blockcount_get(&s->existing_stripe.key.v, i))
962 swap(s->new_stripe.data[i],
963 s->existing_stripe.data[i]);
965 ec_stripe_buf_exit(&s->existing_stripe);
968 BUG_ON(!s->allocated);
970 if (!percpu_ref_tryget_live(&c->writes))
973 ec_generate_ec(&s->new_stripe);
975 ec_generate_checksums(&s->new_stripe);
978 for (i = nr_data; i < v->nr_blocks; i++)
979 ec_block_io(c, &s->new_stripe, REQ_OP_WRITE, i, &s->iodone);
980 closure_sync(&s->iodone);
982 if (ec_nr_failed(&s->new_stripe)) {
983 bch_err(c, "error creating stripe: error writing redundancy buckets");
987 ret = bch2_trans_do(c, &s->res, NULL, BTREE_INSERT_NOFAIL,
988 s->have_existing_stripe
989 ? ec_stripe_bkey_update(&trans, &s->new_stripe.key, &s->res)
990 : ec_stripe_bkey_insert(&trans, &s->new_stripe.key, &s->res));
992 bch_err(c, "error creating stripe: error creating stripe key");
996 ret = ec_stripe_update_extents(c, &s->new_stripe);
998 bch_err(c, "error creating stripe: error updating pointers: %s",
1001 spin_lock(&c->ec_stripes_heap_lock);
1002 m = genradix_ptr(&c->stripes, s->new_stripe.key.k.p.offset);
1005 bch2_stripes_heap_insert(c, m, s->new_stripe.key.k.p.offset);
1006 spin_unlock(&c->ec_stripes_heap_lock);
1008 percpu_ref_put(&c->writes);
1010 bch2_disk_reservation_put(c, &s->res);
1012 for (i = 0; i < v->nr_blocks; i++)
1014 ob = c->open_buckets + s->blocks[i];
1018 __bch2_open_bucket_put(c, ob);
1020 bch2_open_bucket_put(c, ob);
1024 ec_stripe_buf_exit(&s->existing_stripe);
1025 ec_stripe_buf_exit(&s->new_stripe);
1026 closure_debug_destroy(&s->iodone);
1030 static void ec_stripe_create_work(struct work_struct *work)
1032 struct bch_fs *c = container_of(work,
1033 struct bch_fs, ec_stripe_create_work);
1034 struct ec_stripe_new *s, *n;
1036 mutex_lock(&c->ec_stripe_new_lock);
1037 list_for_each_entry_safe(s, n, &c->ec_stripe_new_list, list)
1038 if (!atomic_read(&s->pin)) {
1040 mutex_unlock(&c->ec_stripe_new_lock);
1041 ec_stripe_create(s);
1044 mutex_unlock(&c->ec_stripe_new_lock);
1047 static void ec_stripe_new_put(struct bch_fs *c, struct ec_stripe_new *s)
1049 BUG_ON(atomic_read(&s->pin) <= 0);
1051 if (atomic_dec_and_test(&s->pin)) {
1052 BUG_ON(!s->pending);
1053 queue_work(system_long_wq, &c->ec_stripe_create_work);
1057 static void ec_stripe_set_pending(struct bch_fs *c, struct ec_stripe_head *h)
1059 struct ec_stripe_new *s = h->s;
1061 BUG_ON(!s->allocated && !s->err);
1066 mutex_lock(&c->ec_stripe_new_lock);
1067 list_add(&s->list, &c->ec_stripe_new_list);
1068 mutex_unlock(&c->ec_stripe_new_lock);
1070 ec_stripe_new_put(c, s);
1073 /* have a full bucket - hand it off to be erasure coded: */
1074 void bch2_ec_bucket_written(struct bch_fs *c, struct open_bucket *ob)
1076 struct ec_stripe_new *s = ob->ec;
1078 if (ob->sectors_free)
1081 ec_stripe_new_put(c, s);
1084 void bch2_ec_bucket_cancel(struct bch_fs *c, struct open_bucket *ob)
1086 struct ec_stripe_new *s = ob->ec;
1091 void *bch2_writepoint_ec_buf(struct bch_fs *c, struct write_point *wp)
1093 struct open_bucket *ob = ec_open_bucket(c, &wp->ptrs);
1100 ca = bch_dev_bkey_exists(c, ob->dev);
1101 offset = ca->mi.bucket_size - ob->sectors_free;
1103 return ob->ec->new_stripe.data[ob->ec_idx] + (offset << 9);
1106 static int unsigned_cmp(const void *_l, const void *_r)
1108 unsigned l = *((const unsigned *) _l);
1109 unsigned r = *((const unsigned *) _r);
1111 return cmp_int(l, r);
1114 /* pick most common bucket size: */
1115 static unsigned pick_blocksize(struct bch_fs *c,
1116 struct bch_devs_mask *devs)
1119 unsigned i, nr = 0, sizes[BCH_SB_MEMBERS_MAX];
1122 } cur = { 0, 0 }, best = { 0, 0 };
1124 for_each_member_device_rcu(ca, c, i, devs)
1125 sizes[nr++] = ca->mi.bucket_size;
1127 sort(sizes, nr, sizeof(unsigned), unsigned_cmp, NULL);
1129 for (i = 0; i < nr; i++) {
1130 if (sizes[i] != cur.size) {
1131 if (cur.nr > best.nr)
1135 cur.size = sizes[i];
1141 if (cur.nr > best.nr)
1147 static bool may_create_new_stripe(struct bch_fs *c)
1152 static void ec_stripe_key_init(struct bch_fs *c,
1153 struct bkey_i_stripe *s,
1156 unsigned stripe_size)
1160 bkey_stripe_init(&s->k_i);
1161 s->v.sectors = cpu_to_le16(stripe_size);
1163 s->v.nr_blocks = nr_data + nr_parity;
1164 s->v.nr_redundant = nr_parity;
1165 s->v.csum_granularity_bits = ilog2(c->opts.encoded_extent_max >> 9);
1166 s->v.csum_type = BCH_CSUM_crc32c;
1169 while ((u64s = stripe_val_u64s(&s->v)) > BKEY_VAL_U64s_MAX) {
1170 BUG_ON(1 << s->v.csum_granularity_bits >=
1171 le16_to_cpu(s->v.sectors) ||
1172 s->v.csum_granularity_bits == U8_MAX);
1173 s->v.csum_granularity_bits++;
1176 set_bkey_val_u64s(&s->k, u64s);
1179 static int ec_new_stripe_alloc(struct bch_fs *c, struct ec_stripe_head *h)
1181 struct ec_stripe_new *s;
1183 lockdep_assert_held(&h->lock);
1185 s = kzalloc(sizeof(*s), GFP_KERNEL);
1189 mutex_init(&s->lock);
1190 closure_init(&s->iodone, NULL);
1191 atomic_set(&s->pin, 1);
1194 s->nr_data = min_t(unsigned, h->nr_active_devs,
1195 BCH_BKEY_PTRS_MAX) - h->redundancy;
1196 s->nr_parity = h->redundancy;
1198 ec_stripe_key_init(c, &s->new_stripe.key, s->nr_data,
1199 s->nr_parity, h->blocksize);
1205 static struct ec_stripe_head *
1206 ec_new_stripe_head_alloc(struct bch_fs *c, unsigned target,
1207 unsigned algo, unsigned redundancy,
1210 struct ec_stripe_head *h;
1214 h = kzalloc(sizeof(*h), GFP_KERNEL);
1218 mutex_init(&h->lock);
1219 mutex_lock(&h->lock);
1223 h->redundancy = redundancy;
1227 h->devs = target_rw_devs(c, BCH_DATA_user, target);
1229 for_each_member_device_rcu(ca, c, i, &h->devs)
1230 if (!ca->mi.durability)
1231 __clear_bit(i, h->devs.d);
1233 h->blocksize = pick_blocksize(c, &h->devs);
1235 for_each_member_device_rcu(ca, c, i, &h->devs)
1236 if (ca->mi.bucket_size == h->blocksize)
1237 h->nr_active_devs++;
1240 list_add(&h->list, &c->ec_stripe_head_list);
1244 void bch2_ec_stripe_head_put(struct bch_fs *c, struct ec_stripe_head *h)
1248 bitmap_weight(h->s->blocks_allocated,
1249 h->s->nr_data) == h->s->nr_data)
1250 ec_stripe_set_pending(c, h);
1252 mutex_unlock(&h->lock);
1255 struct ec_stripe_head *__bch2_ec_stripe_head_get(struct bch_fs *c,
1258 unsigned redundancy,
1261 struct ec_stripe_head *h;
1266 mutex_lock(&c->ec_stripe_head_lock);
1267 list_for_each_entry(h, &c->ec_stripe_head_list, list)
1268 if (h->target == target &&
1270 h->redundancy == redundancy &&
1271 h->copygc == copygc) {
1272 mutex_lock(&h->lock);
1276 h = ec_new_stripe_head_alloc(c, target, algo, redundancy, copygc);
1278 mutex_unlock(&c->ec_stripe_head_lock);
1282 static int new_stripe_alloc_buckets(struct bch_fs *c, struct ec_stripe_head *h,
1285 struct bch_devs_mask devs = h->devs;
1286 struct open_bucket *ob;
1287 struct open_buckets buckets;
1288 unsigned i, j, nr_have_parity = 0, nr_have_data = 0;
1289 bool have_cache = true;
1292 for (i = 0; i < h->s->new_stripe.key.v.nr_blocks; i++) {
1293 if (test_bit(i, h->s->blocks_gotten)) {
1294 __clear_bit(h->s->new_stripe.key.v.ptrs[i].dev, devs.d);
1295 if (i < h->s->nr_data)
1302 BUG_ON(nr_have_data > h->s->nr_data);
1303 BUG_ON(nr_have_parity > h->s->nr_parity);
1306 if (nr_have_parity < h->s->nr_parity) {
1307 ret = bch2_bucket_alloc_set(c, &buckets,
1319 open_bucket_for_each(c, &buckets, ob, i) {
1320 j = find_next_zero_bit(h->s->blocks_gotten,
1321 h->s->nr_data + h->s->nr_parity,
1323 BUG_ON(j >= h->s->nr_data + h->s->nr_parity);
1325 h->s->blocks[j] = buckets.v[i];
1326 h->s->new_stripe.key.v.ptrs[j] = bch2_ob_ptr(c, ob);
1327 __set_bit(j, h->s->blocks_gotten);
1335 if (nr_have_data < h->s->nr_data) {
1336 ret = bch2_bucket_alloc_set(c, &buckets,
1348 open_bucket_for_each(c, &buckets, ob, i) {
1349 j = find_next_zero_bit(h->s->blocks_gotten,
1351 BUG_ON(j >= h->s->nr_data);
1353 h->s->blocks[j] = buckets.v[i];
1354 h->s->new_stripe.key.v.ptrs[j] = bch2_ob_ptr(c, ob);
1355 __set_bit(j, h->s->blocks_gotten);
1365 /* XXX: doesn't obey target: */
1366 static s64 get_existing_stripe(struct bch_fs *c,
1367 struct ec_stripe_head *head)
1369 ec_stripes_heap *h = &c->ec_stripes_heap;
1375 if (may_create_new_stripe(c))
1378 spin_lock(&c->ec_stripes_heap_lock);
1379 for (heap_idx = 0; heap_idx < h->used; heap_idx++) {
1380 /* No blocks worth reusing, stripe will just be deleted: */
1381 if (!h->data[heap_idx].blocks_nonempty)
1384 stripe_idx = h->data[heap_idx].idx;
1385 m = genradix_ptr(&c->stripes, stripe_idx);
1387 if (m->algorithm == head->algo &&
1388 m->nr_redundant == head->redundancy &&
1389 m->sectors == head->blocksize &&
1390 m->blocks_nonempty < m->nr_blocks - m->nr_redundant) {
1391 bch2_stripes_heap_del(c, m, stripe_idx);
1396 spin_unlock(&c->ec_stripes_heap_lock);
1400 static int __bch2_ec_stripe_head_reuse(struct bch_fs *c,
1401 struct ec_stripe_head *h)
1407 idx = get_existing_stripe(c, h);
1409 return -BCH_ERR_ENOSPC_stripe_reuse;
1411 h->s->have_existing_stripe = true;
1412 ret = get_stripe_key(c, idx, &h->s->existing_stripe);
1414 bch2_fs_fatal_error(c, "error reading stripe key: %i", ret);
1418 if (ec_stripe_buf_init(&h->s->existing_stripe, 0, h->blocksize)) {
1420 * this is a problem: we have deleted from the
1421 * stripes heap already
1426 BUG_ON(h->s->existing_stripe.size != h->blocksize);
1427 BUG_ON(h->s->existing_stripe.size != h->s->existing_stripe.key.v.sectors);
1429 for (i = 0; i < h->s->existing_stripe.key.v.nr_blocks; i++) {
1430 if (stripe_blockcount_get(&h->s->existing_stripe.key.v, i)) {
1431 __set_bit(i, h->s->blocks_gotten);
1432 __set_bit(i, h->s->blocks_allocated);
1435 ec_block_io(c, &h->s->existing_stripe, READ, i, &h->s->iodone);
1438 bkey_copy(&h->s->new_stripe.key.k_i,
1439 &h->s->existing_stripe.key.k_i);
1444 static int __bch2_ec_stripe_head_reserve(struct bch_fs *c,
1445 struct ec_stripe_head *h)
1447 return bch2_disk_reservation_get(c, &h->s->res,
1449 h->s->nr_parity, 0);
1452 struct ec_stripe_head *bch2_ec_stripe_head_get(struct bch_fs *c,
1455 unsigned redundancy,
1459 struct ec_stripe_head *h;
1461 bool needs_stripe_new;
1463 h = __bch2_ec_stripe_head_get(c, target, algo, redundancy, copygc);
1465 bch_err(c, "no stripe head");
1469 needs_stripe_new = !h->s;
1470 if (needs_stripe_new) {
1471 if (ec_new_stripe_alloc(c, h)) {
1473 bch_err(c, "failed to allocate new stripe");
1477 if (ec_stripe_buf_init(&h->s->new_stripe, 0, h->blocksize))
1482 * Try reserve a new stripe before reusing an
1483 * existing stripe. This will prevent unnecessary
1484 * read amplification during write oriented workloads.
1487 if (!h->s->allocated && !h->s->res.sectors && !h->s->have_existing_stripe)
1488 ret = __bch2_ec_stripe_head_reserve(c, h);
1489 if (ret && needs_stripe_new)
1490 ret = __bch2_ec_stripe_head_reuse(c, h);
1492 bch_err_ratelimited(c, "failed to get stripe: %s", bch2_err_str(ret));
1496 if (!h->s->allocated) {
1497 ret = new_stripe_alloc_buckets(c, h, cl);
1501 h->s->allocated = true;
1507 bch2_ec_stripe_head_put(c, h);
1508 return ERR_PTR(ret);
1511 void bch2_ec_stop_dev(struct bch_fs *c, struct bch_dev *ca)
1513 struct ec_stripe_head *h;
1514 struct open_bucket *ob;
1517 mutex_lock(&c->ec_stripe_head_lock);
1518 list_for_each_entry(h, &c->ec_stripe_head_list, list) {
1520 mutex_lock(&h->lock);
1524 for (i = 0; i < h->s->new_stripe.key.v.nr_blocks; i++) {
1525 if (!h->s->blocks[i])
1528 ob = c->open_buckets + h->s->blocks[i];
1529 if (ob->dev == ca->dev_idx)
1535 ec_stripe_set_pending(c, h);
1537 mutex_unlock(&h->lock);
1539 mutex_unlock(&c->ec_stripe_head_lock);
1542 void bch2_stripes_heap_start(struct bch_fs *c)
1544 struct genradix_iter iter;
1547 genradix_for_each(&c->stripes, iter, m)
1549 bch2_stripes_heap_insert(c, m, iter.pos);
1552 int bch2_stripes_read(struct bch_fs *c)
1554 struct btree_trans trans;
1555 struct btree_iter iter;
1557 const struct bch_stripe *s;
1562 bch2_trans_init(&trans, c, 0, 0);
1564 for_each_btree_key(&trans, iter, BTREE_ID_stripes, POS_MIN,
1565 BTREE_ITER_PREFETCH, k, ret) {
1566 if (k.k->type != KEY_TYPE_stripe)
1569 ret = __ec_stripe_mem_alloc(c, k.k->p.offset, GFP_KERNEL);
1573 s = bkey_s_c_to_stripe(k).v;
1575 m = genradix_ptr(&c->stripes, k.k->p.offset);
1577 m->sectors = le16_to_cpu(s->sectors);
1578 m->algorithm = s->algorithm;
1579 m->nr_blocks = s->nr_blocks;
1580 m->nr_redundant = s->nr_redundant;
1581 m->blocks_nonempty = 0;
1583 for (i = 0; i < s->nr_blocks; i++)
1584 m->blocks_nonempty += !!stripe_blockcount_get(s, i);
1586 spin_lock(&c->ec_stripes_heap_lock);
1587 bch2_stripes_heap_update(c, m, k.k->p.offset);
1588 spin_unlock(&c->ec_stripes_heap_lock);
1590 bch2_trans_iter_exit(&trans, &iter);
1592 bch2_trans_exit(&trans);
1595 bch_err(c, "error reading stripes: %i", ret);
1600 void bch2_stripes_heap_to_text(struct printbuf *out, struct bch_fs *c)
1602 ec_stripes_heap *h = &c->ec_stripes_heap;
1606 spin_lock(&c->ec_stripes_heap_lock);
1607 for (i = 0; i < min_t(size_t, h->used, 20); i++) {
1608 m = genradix_ptr(&c->stripes, h->data[i].idx);
1610 prt_printf(out, "%zu %u/%u+%u\n", h->data[i].idx,
1611 h->data[i].blocks_nonempty,
1612 m->nr_blocks - m->nr_redundant,
1615 spin_unlock(&c->ec_stripes_heap_lock);
1618 void bch2_new_stripes_to_text(struct printbuf *out, struct bch_fs *c)
1620 struct ec_stripe_head *h;
1621 struct ec_stripe_new *s;
1623 mutex_lock(&c->ec_stripe_head_lock);
1624 list_for_each_entry(h, &c->ec_stripe_head_list, list) {
1625 prt_printf(out, "target %u algo %u redundancy %u:\n",
1626 h->target, h->algo, h->redundancy);
1629 prt_printf(out, "\tpending: blocks %u+%u allocated %u\n",
1630 h->s->nr_data, h->s->nr_parity,
1631 bitmap_weight(h->s->blocks_allocated,
1634 mutex_unlock(&c->ec_stripe_head_lock);
1636 mutex_lock(&c->ec_stripe_new_lock);
1637 list_for_each_entry(s, &c->ec_stripe_new_list, list) {
1638 prt_printf(out, "\tin flight: blocks %u+%u pin %u\n",
1639 s->nr_data, s->nr_parity,
1640 atomic_read(&s->pin));
1642 mutex_unlock(&c->ec_stripe_new_lock);
1645 void bch2_fs_ec_exit(struct bch_fs *c)
1647 struct ec_stripe_head *h;
1650 mutex_lock(&c->ec_stripe_head_lock);
1651 h = list_first_entry_or_null(&c->ec_stripe_head_list,
1652 struct ec_stripe_head, list);
1655 mutex_unlock(&c->ec_stripe_head_lock);
1663 BUG_ON(!list_empty(&c->ec_stripe_new_list));
1665 free_heap(&c->ec_stripes_heap);
1666 genradix_free(&c->stripes);
1667 bioset_exit(&c->ec_bioset);
1670 void bch2_fs_ec_init_early(struct bch_fs *c)
1672 INIT_WORK(&c->ec_stripe_create_work, ec_stripe_create_work);
1673 INIT_WORK(&c->ec_stripe_delete_work, ec_stripe_delete_work);
1676 int bch2_fs_ec_init(struct bch_fs *c)
1678 return bioset_init(&c->ec_bioset, 1, offsetof(struct ec_bio, bio),