1 // SPDX-License-Identifier: GPL-2.0
6 #include "alloc_foreground.h"
7 #include "backpointers.h"
11 #include "btree_update.h"
12 #include "btree_write_buffer.h"
15 #include "disk_groups.h"
25 #include <linux/sort.h>
29 #include <linux/raid/pq.h>
30 #include <linux/raid/xor.h>
32 static void raid5_recov(unsigned disks, unsigned failed_idx,
33 size_t size, void **data)
37 BUG_ON(failed_idx >= disks);
39 swap(data[0], data[failed_idx]);
40 memcpy(data[0], data[1], size);
43 nr = min_t(unsigned, disks - i, MAX_XOR_BLOCKS);
44 xor_blocks(nr, size, data[0], data + i);
48 swap(data[0], data[failed_idx]);
51 static void raid_gen(int nd, int np, size_t size, void **v)
54 raid5_recov(nd + np, nd, size, v);
56 raid6_call.gen_syndrome(nd + np, size, v);
60 static void raid_rec(int nr, int *ir, int nd, int np, size_t size, void **v)
67 raid5_recov(nd + 1, ir[0], size, v);
69 raid6_call.gen_syndrome(nd + np, size, v);
73 /* data+data failure. */
74 raid6_2data_recov(nd + np, size, ir[0], ir[1], v);
75 } else if (ir[0] < nd) {
76 /* data + p/q failure */
78 if (ir[1] == nd) /* data + p failure */
79 raid6_datap_recov(nd + np, size, ir[0], v);
80 else { /* data + q failure */
81 raid5_recov(nd + 1, ir[0], size, v);
82 raid6_call.gen_syndrome(nd + np, size, v);
85 raid_gen(nd, np, size, v);
95 #include <raid/raid.h>
101 struct ec_stripe_buf *buf;
106 /* Stripes btree keys: */
108 int bch2_stripe_invalid(struct bch_fs *c, struct bkey_s_c k,
109 enum bkey_invalid_flags flags,
110 struct printbuf *err)
112 const struct bch_stripe *s = bkey_s_c_to_stripe(k).v;
115 bkey_fsck_err_on(bkey_eq(k.k->p, POS_MIN) ||
116 bpos_gt(k.k->p, POS(0, U32_MAX)), c, err,
118 "stripe at bad pos");
120 bkey_fsck_err_on(bkey_val_u64s(k.k) < stripe_val_u64s(s), c, err,
122 "incorrect value size (%zu < %u)",
123 bkey_val_u64s(k.k), stripe_val_u64s(s));
125 ret = bch2_bkey_ptrs_invalid(c, k, flags, err);
130 void bch2_stripe_to_text(struct printbuf *out, struct bch_fs *c,
133 const struct bch_stripe *s = bkey_s_c_to_stripe(k).v;
134 unsigned i, nr_data = s->nr_blocks - s->nr_redundant;
136 prt_printf(out, "algo %u sectors %u blocks %u:%u csum %u gran %u",
138 le16_to_cpu(s->sectors),
142 1U << s->csum_granularity_bits);
144 for (i = 0; i < s->nr_blocks; i++) {
145 const struct bch_extent_ptr *ptr = s->ptrs + i;
146 struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
148 u64 b = sector_to_bucket_and_offset(ca, ptr->offset, &offset);
150 prt_printf(out, " %u:%llu:%u", ptr->dev, b, offset);
152 prt_printf(out, "#%u", stripe_blockcount_get(s, i));
153 prt_printf(out, " gen %u", ptr->gen);
154 if (ptr_stale(ca, ptr))
155 prt_printf(out, " stale");
159 /* returns blocknr in stripe that we matched: */
160 static const struct bch_extent_ptr *bkey_matches_stripe(struct bch_stripe *s,
161 struct bkey_s_c k, unsigned *block)
163 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
164 const struct bch_extent_ptr *ptr;
165 unsigned i, nr_data = s->nr_blocks - s->nr_redundant;
167 bkey_for_each_ptr(ptrs, ptr)
168 for (i = 0; i < nr_data; i++)
169 if (__bch2_ptr_matches_stripe(&s->ptrs[i], ptr,
170 le16_to_cpu(s->sectors))) {
178 static bool extent_has_stripe_ptr(struct bkey_s_c k, u64 idx)
181 case KEY_TYPE_extent: {
182 struct bkey_s_c_extent e = bkey_s_c_to_extent(k);
183 const union bch_extent_entry *entry;
185 extent_for_each_entry(e, entry)
186 if (extent_entry_type(entry) ==
187 BCH_EXTENT_ENTRY_stripe_ptr &&
188 entry->stripe_ptr.idx == idx)
200 static void ec_stripe_buf_exit(struct ec_stripe_buf *buf)
202 if (buf->key.k.type == KEY_TYPE_stripe) {
203 struct bkey_i_stripe *s = bkey_i_to_stripe(&buf->key);
206 for (i = 0; i < s->v.nr_blocks; i++) {
207 kvpfree(buf->data[i], buf->size << 9);
213 /* XXX: this is a non-mempoolified memory allocation: */
214 static int ec_stripe_buf_init(struct ec_stripe_buf *buf,
215 unsigned offset, unsigned size)
217 struct bch_stripe *v = &bkey_i_to_stripe(&buf->key)->v;
218 unsigned csum_granularity = 1U << v->csum_granularity_bits;
219 unsigned end = offset + size;
222 BUG_ON(end > le16_to_cpu(v->sectors));
224 offset = round_down(offset, csum_granularity);
225 end = min_t(unsigned, le16_to_cpu(v->sectors),
226 round_up(end, csum_granularity));
228 buf->offset = offset;
229 buf->size = end - offset;
231 memset(buf->valid, 0xFF, sizeof(buf->valid));
233 for (i = 0; i < v->nr_blocks; i++) {
234 buf->data[i] = kvpmalloc(buf->size << 9, GFP_KERNEL);
241 ec_stripe_buf_exit(buf);
242 return -BCH_ERR_ENOMEM_stripe_buf;
247 static struct bch_csum ec_block_checksum(struct ec_stripe_buf *buf,
248 unsigned block, unsigned offset)
250 struct bch_stripe *v = &bkey_i_to_stripe(&buf->key)->v;
251 unsigned csum_granularity = 1 << v->csum_granularity_bits;
252 unsigned end = buf->offset + buf->size;
253 unsigned len = min(csum_granularity, end - offset);
255 BUG_ON(offset >= end);
256 BUG_ON(offset < buf->offset);
257 BUG_ON(offset & (csum_granularity - 1));
258 BUG_ON(offset + len != le16_to_cpu(v->sectors) &&
259 (len & (csum_granularity - 1)));
261 return bch2_checksum(NULL, v->csum_type,
263 buf->data[block] + ((offset - buf->offset) << 9),
267 static void ec_generate_checksums(struct ec_stripe_buf *buf)
269 struct bch_stripe *v = &bkey_i_to_stripe(&buf->key)->v;
270 unsigned i, j, csums_per_device = stripe_csums_per_device(v);
276 BUG_ON(buf->size != le16_to_cpu(v->sectors));
278 for (i = 0; i < v->nr_blocks; i++)
279 for (j = 0; j < csums_per_device; j++)
280 stripe_csum_set(v, i, j,
281 ec_block_checksum(buf, i, j << v->csum_granularity_bits));
284 static void ec_validate_checksums(struct bch_fs *c, struct ec_stripe_buf *buf)
286 struct bch_stripe *v = &bkey_i_to_stripe(&buf->key)->v;
287 unsigned csum_granularity = 1 << v->csum_granularity_bits;
293 for (i = 0; i < v->nr_blocks; i++) {
294 unsigned offset = buf->offset;
295 unsigned end = buf->offset + buf->size;
297 if (!test_bit(i, buf->valid))
300 while (offset < end) {
301 unsigned j = offset >> v->csum_granularity_bits;
302 unsigned len = min(csum_granularity, end - offset);
303 struct bch_csum want = stripe_csum_get(v, i, j);
304 struct bch_csum got = ec_block_checksum(buf, i, offset);
306 if (bch2_crc_cmp(want, got)) {
307 struct printbuf err = PRINTBUF;
308 struct bch_dev *ca = bch_dev_bkey_exists(c, v->ptrs[i].dev);
310 prt_printf(&err, "stripe checksum error: expected %0llx:%0llx got %0llx:%0llx (type %s)\n",
313 bch2_csum_types[v->csum_type]);
314 prt_printf(&err, " for %ps at %u of\n ", (void *) _RET_IP_, i);
315 bch2_bkey_val_to_text(&err, c, bkey_i_to_s_c(&buf->key));
316 bch_err_ratelimited(ca, "%s", err.buf);
319 clear_bit(i, buf->valid);
321 bch2_io_error(ca, BCH_MEMBER_ERROR_checksum);
330 /* Erasure coding: */
332 static void ec_generate_ec(struct ec_stripe_buf *buf)
334 struct bch_stripe *v = &bkey_i_to_stripe(&buf->key)->v;
335 unsigned nr_data = v->nr_blocks - v->nr_redundant;
336 unsigned bytes = le16_to_cpu(v->sectors) << 9;
338 raid_gen(nr_data, v->nr_redundant, bytes, buf->data);
341 static unsigned ec_nr_failed(struct ec_stripe_buf *buf)
343 struct bch_stripe *v = &bkey_i_to_stripe(&buf->key)->v;
345 return v->nr_blocks - bitmap_weight(buf->valid, v->nr_blocks);
348 static int ec_do_recov(struct bch_fs *c, struct ec_stripe_buf *buf)
350 struct bch_stripe *v = &bkey_i_to_stripe(&buf->key)->v;
351 unsigned i, failed[BCH_BKEY_PTRS_MAX], nr_failed = 0;
352 unsigned nr_data = v->nr_blocks - v->nr_redundant;
353 unsigned bytes = buf->size << 9;
355 if (ec_nr_failed(buf) > v->nr_redundant) {
356 bch_err_ratelimited(c,
357 "error doing reconstruct read: unable to read enough blocks");
361 for (i = 0; i < nr_data; i++)
362 if (!test_bit(i, buf->valid))
363 failed[nr_failed++] = i;
365 raid_rec(nr_failed, failed, nr_data, v->nr_redundant, bytes, buf->data);
371 static void ec_block_endio(struct bio *bio)
373 struct ec_bio *ec_bio = container_of(bio, struct ec_bio, bio);
374 struct bch_stripe *v = &bkey_i_to_stripe(&ec_bio->buf->key)->v;
375 struct bch_extent_ptr *ptr = &v->ptrs[ec_bio->idx];
376 struct bch_dev *ca = ec_bio->ca;
377 struct closure *cl = bio->bi_private;
379 if (bch2_dev_io_err_on(bio->bi_status, ca,
381 ? BCH_MEMBER_ERROR_write
382 : BCH_MEMBER_ERROR_read,
383 "erasure coding %s error: %s",
384 bio_data_dir(bio) ? "write" : "read",
385 bch2_blk_status_to_str(bio->bi_status)))
386 clear_bit(ec_bio->idx, ec_bio->buf->valid);
388 if (ptr_stale(ca, ptr)) {
389 bch_err_ratelimited(ca->fs,
390 "error %s stripe: stale pointer after io",
391 bio_data_dir(bio) == READ ? "reading from" : "writing to");
392 clear_bit(ec_bio->idx, ec_bio->buf->valid);
395 bio_put(&ec_bio->bio);
396 percpu_ref_put(&ca->io_ref);
400 static void ec_block_io(struct bch_fs *c, struct ec_stripe_buf *buf,
401 blk_opf_t opf, unsigned idx, struct closure *cl)
403 struct bch_stripe *v = &bkey_i_to_stripe(&buf->key)->v;
404 unsigned offset = 0, bytes = buf->size << 9;
405 struct bch_extent_ptr *ptr = &v->ptrs[idx];
406 struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
407 enum bch_data_type data_type = idx < v->nr_blocks - v->nr_redundant
410 int rw = op_is_write(opf);
412 if (ptr_stale(ca, ptr)) {
413 bch_err_ratelimited(c,
414 "error %s stripe: stale pointer",
415 rw == READ ? "reading from" : "writing to");
416 clear_bit(idx, buf->valid);
420 if (!bch2_dev_get_ioref(ca, rw)) {
421 clear_bit(idx, buf->valid);
425 this_cpu_add(ca->io_done->sectors[rw][data_type], buf->size);
427 while (offset < bytes) {
428 unsigned nr_iovecs = min_t(size_t, BIO_MAX_VECS,
429 DIV_ROUND_UP(bytes, PAGE_SIZE));
430 unsigned b = min_t(size_t, bytes - offset,
431 nr_iovecs << PAGE_SHIFT);
432 struct ec_bio *ec_bio;
434 ec_bio = container_of(bio_alloc_bioset(ca->disk_sb.bdev,
445 ec_bio->bio.bi_iter.bi_sector = ptr->offset + buf->offset + (offset >> 9);
446 ec_bio->bio.bi_end_io = ec_block_endio;
447 ec_bio->bio.bi_private = cl;
449 bch2_bio_map(&ec_bio->bio, buf->data[idx] + offset, b);
452 percpu_ref_get(&ca->io_ref);
454 submit_bio(&ec_bio->bio);
459 percpu_ref_put(&ca->io_ref);
462 static int get_stripe_key_trans(struct btree_trans *trans, u64 idx,
463 struct ec_stripe_buf *stripe)
465 struct btree_iter iter;
469 k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_stripes,
470 POS(0, idx), BTREE_ITER_SLOTS);
474 if (k.k->type != KEY_TYPE_stripe) {
478 bkey_reassemble(&stripe->key, k);
480 bch2_trans_iter_exit(trans, &iter);
484 /* recovery read path: */
485 int bch2_ec_read_extent(struct btree_trans *trans, struct bch_read_bio *rbio)
487 struct bch_fs *c = trans->c;
488 struct ec_stripe_buf *buf;
490 struct bch_stripe *v;
494 closure_init_stack(&cl);
496 BUG_ON(!rbio->pick.has_ec);
498 buf = kzalloc(sizeof(*buf), GFP_NOFS);
500 return -BCH_ERR_ENOMEM_ec_read_extent;
502 ret = lockrestart_do(trans, get_stripe_key_trans(trans, rbio->pick.ec.idx, buf));
504 bch_err_ratelimited(c,
505 "error doing reconstruct read: error %i looking up stripe", ret);
510 v = &bkey_i_to_stripe(&buf->key)->v;
512 if (!bch2_ptr_matches_stripe(v, rbio->pick)) {
513 bch_err_ratelimited(c,
514 "error doing reconstruct read: pointer doesn't match stripe");
519 offset = rbio->bio.bi_iter.bi_sector - v->ptrs[rbio->pick.ec.block].offset;
520 if (offset + bio_sectors(&rbio->bio) > le16_to_cpu(v->sectors)) {
521 bch_err_ratelimited(c,
522 "error doing reconstruct read: read is bigger than stripe");
527 ret = ec_stripe_buf_init(buf, offset, bio_sectors(&rbio->bio));
531 for (i = 0; i < v->nr_blocks; i++)
532 ec_block_io(c, buf, REQ_OP_READ, i, &cl);
536 if (ec_nr_failed(buf) > v->nr_redundant) {
537 bch_err_ratelimited(c,
538 "error doing reconstruct read: unable to read enough blocks");
543 ec_validate_checksums(c, buf);
545 ret = ec_do_recov(c, buf);
549 memcpy_to_bio(&rbio->bio, rbio->bio.bi_iter,
550 buf->data[rbio->pick.ec.block] + ((offset - buf->offset) << 9));
552 ec_stripe_buf_exit(buf);
557 /* stripe bucket accounting: */
559 static int __ec_stripe_mem_alloc(struct bch_fs *c, size_t idx, gfp_t gfp)
561 ec_stripes_heap n, *h = &c->ec_stripes_heap;
563 if (idx >= h->size) {
564 if (!init_heap(&n, max(1024UL, roundup_pow_of_two(idx + 1)), gfp))
565 return -BCH_ERR_ENOMEM_ec_stripe_mem_alloc;
567 mutex_lock(&c->ec_stripes_heap_lock);
568 if (n.size > h->size) {
569 memcpy(n.data, h->data, h->used * sizeof(h->data[0]));
573 mutex_unlock(&c->ec_stripes_heap_lock);
578 if (!genradix_ptr_alloc(&c->stripes, idx, gfp))
579 return -BCH_ERR_ENOMEM_ec_stripe_mem_alloc;
581 if (c->gc_pos.phase != GC_PHASE_NOT_RUNNING &&
582 !genradix_ptr_alloc(&c->gc_stripes, idx, gfp))
583 return -BCH_ERR_ENOMEM_ec_stripe_mem_alloc;
588 static int ec_stripe_mem_alloc(struct btree_trans *trans,
589 struct btree_iter *iter)
591 return allocate_dropping_locks_errcode(trans,
592 __ec_stripe_mem_alloc(trans->c, iter->pos.offset, _gfp));
596 * Hash table of open stripes:
597 * Stripes that are being created or modified are kept in a hash table, so that
598 * stripe deletion can skip them.
601 static bool __bch2_stripe_is_open(struct bch_fs *c, u64 idx)
603 unsigned hash = hash_64(idx, ilog2(ARRAY_SIZE(c->ec_stripes_new)));
604 struct ec_stripe_new *s;
606 hlist_for_each_entry(s, &c->ec_stripes_new[hash], hash)
612 static bool bch2_stripe_is_open(struct bch_fs *c, u64 idx)
616 spin_lock(&c->ec_stripes_new_lock);
617 ret = __bch2_stripe_is_open(c, idx);
618 spin_unlock(&c->ec_stripes_new_lock);
623 static bool bch2_try_open_stripe(struct bch_fs *c,
624 struct ec_stripe_new *s,
629 spin_lock(&c->ec_stripes_new_lock);
630 ret = !__bch2_stripe_is_open(c, idx);
632 unsigned hash = hash_64(idx, ilog2(ARRAY_SIZE(c->ec_stripes_new)));
635 hlist_add_head(&s->hash, &c->ec_stripes_new[hash]);
637 spin_unlock(&c->ec_stripes_new_lock);
642 static void bch2_stripe_close(struct bch_fs *c, struct ec_stripe_new *s)
646 spin_lock(&c->ec_stripes_new_lock);
647 hlist_del_init(&s->hash);
648 spin_unlock(&c->ec_stripes_new_lock);
653 /* Heap of all existing stripes, ordered by blocks_nonempty */
655 static u64 stripe_idx_to_delete(struct bch_fs *c)
657 ec_stripes_heap *h = &c->ec_stripes_heap;
659 lockdep_assert_held(&c->ec_stripes_heap_lock);
662 h->data[0].blocks_nonempty == 0 &&
663 !bch2_stripe_is_open(c, h->data[0].idx))
664 return h->data[0].idx;
669 static inline int ec_stripes_heap_cmp(ec_stripes_heap *h,
670 struct ec_stripe_heap_entry l,
671 struct ec_stripe_heap_entry r)
673 return ((l.blocks_nonempty > r.blocks_nonempty) -
674 (l.blocks_nonempty < r.blocks_nonempty));
677 static inline void ec_stripes_heap_set_backpointer(ec_stripes_heap *h,
680 struct bch_fs *c = container_of(h, struct bch_fs, ec_stripes_heap);
682 genradix_ptr(&c->stripes, h->data[i].idx)->heap_idx = i;
685 static void heap_verify_backpointer(struct bch_fs *c, size_t idx)
687 ec_stripes_heap *h = &c->ec_stripes_heap;
688 struct stripe *m = genradix_ptr(&c->stripes, idx);
690 BUG_ON(m->heap_idx >= h->used);
691 BUG_ON(h->data[m->heap_idx].idx != idx);
694 void bch2_stripes_heap_del(struct bch_fs *c,
695 struct stripe *m, size_t idx)
697 mutex_lock(&c->ec_stripes_heap_lock);
698 heap_verify_backpointer(c, idx);
700 heap_del(&c->ec_stripes_heap, m->heap_idx,
702 ec_stripes_heap_set_backpointer);
703 mutex_unlock(&c->ec_stripes_heap_lock);
706 void bch2_stripes_heap_insert(struct bch_fs *c,
707 struct stripe *m, size_t idx)
709 mutex_lock(&c->ec_stripes_heap_lock);
710 BUG_ON(heap_full(&c->ec_stripes_heap));
712 heap_add(&c->ec_stripes_heap, ((struct ec_stripe_heap_entry) {
714 .blocks_nonempty = m->blocks_nonempty,
717 ec_stripes_heap_set_backpointer);
719 heap_verify_backpointer(c, idx);
720 mutex_unlock(&c->ec_stripes_heap_lock);
723 void bch2_stripes_heap_update(struct bch_fs *c,
724 struct stripe *m, size_t idx)
726 ec_stripes_heap *h = &c->ec_stripes_heap;
730 mutex_lock(&c->ec_stripes_heap_lock);
731 heap_verify_backpointer(c, idx);
733 h->data[m->heap_idx].blocks_nonempty = m->blocks_nonempty;
736 heap_sift_up(h, i, ec_stripes_heap_cmp,
737 ec_stripes_heap_set_backpointer);
738 heap_sift_down(h, i, ec_stripes_heap_cmp,
739 ec_stripes_heap_set_backpointer);
741 heap_verify_backpointer(c, idx);
743 do_deletes = stripe_idx_to_delete(c) != 0;
744 mutex_unlock(&c->ec_stripes_heap_lock);
747 bch2_do_stripe_deletes(c);
750 /* stripe deletion */
752 static int ec_stripe_delete(struct btree_trans *trans, u64 idx)
754 struct bch_fs *c = trans->c;
755 struct btree_iter iter;
757 struct bkey_s_c_stripe s;
760 k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_stripes, POS(0, idx),
766 if (k.k->type != KEY_TYPE_stripe) {
767 bch2_fs_inconsistent(c, "attempting to delete nonexistent stripe %llu", idx);
772 s = bkey_s_c_to_stripe(k);
773 for (unsigned i = 0; i < s.v->nr_blocks; i++)
774 if (stripe_blockcount_get(s.v, i)) {
775 struct printbuf buf = PRINTBUF;
777 bch2_bkey_val_to_text(&buf, c, k);
778 bch2_fs_inconsistent(c, "attempting to delete nonempty stripe %s", buf.buf);
784 ret = bch2_btree_delete_at(trans, &iter, 0);
786 bch2_trans_iter_exit(trans, &iter);
790 static void ec_stripe_delete_work(struct work_struct *work)
793 container_of(work, struct bch_fs, ec_stripe_delete_work);
796 mutex_lock(&c->ec_stripes_heap_lock);
797 u64 idx = stripe_idx_to_delete(c);
798 mutex_unlock(&c->ec_stripes_heap_lock);
803 int ret = bch2_trans_do(c, NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
804 ec_stripe_delete(trans, idx));
810 bch2_write_ref_put(c, BCH_WRITE_REF_stripe_delete);
813 void bch2_do_stripe_deletes(struct bch_fs *c)
815 if (bch2_write_ref_tryget(c, BCH_WRITE_REF_stripe_delete) &&
816 !queue_work(c->write_ref_wq, &c->ec_stripe_delete_work))
817 bch2_write_ref_put(c, BCH_WRITE_REF_stripe_delete);
820 /* stripe creation: */
822 static int ec_stripe_key_update(struct btree_trans *trans,
823 struct bkey_i_stripe *new,
826 struct bch_fs *c = trans->c;
827 struct btree_iter iter;
831 k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_stripes,
832 new->k.p, BTREE_ITER_INTENT);
837 if (k.k->type != (create ? KEY_TYPE_deleted : KEY_TYPE_stripe)) {
838 bch2_fs_inconsistent(c, "error %s stripe: got existing key type %s",
839 create ? "creating" : "updating",
840 bch2_bkey_types[k.k->type]);
845 if (k.k->type == KEY_TYPE_stripe) {
846 const struct bch_stripe *old = bkey_s_c_to_stripe(k).v;
849 if (old->nr_blocks != new->v.nr_blocks) {
850 bch_err(c, "error updating stripe: nr_blocks does not match");
855 for (i = 0; i < new->v.nr_blocks; i++) {
856 unsigned v = stripe_blockcount_get(old, i);
859 (old->ptrs[i].dev != new->v.ptrs[i].dev ||
860 old->ptrs[i].gen != new->v.ptrs[i].gen ||
861 old->ptrs[i].offset != new->v.ptrs[i].offset));
863 stripe_blockcount_set(&new->v, i, v);
867 ret = bch2_trans_update(trans, &iter, &new->k_i, 0);
869 bch2_trans_iter_exit(trans, &iter);
873 static int ec_stripe_update_extent(struct btree_trans *trans,
874 struct bpos bucket, u8 gen,
875 struct ec_stripe_buf *s,
878 struct bch_stripe *v = &bkey_i_to_stripe(&s->key)->v;
879 struct bch_fs *c = trans->c;
880 struct bch_backpointer bp;
881 struct btree_iter iter;
883 const struct bch_extent_ptr *ptr_c;
884 struct bch_extent_ptr *ptr, *ec_ptr = NULL;
885 struct bch_extent_stripe_ptr stripe_ptr;
889 ret = bch2_get_next_backpointer(trans, bucket, gen,
890 bp_pos, &bp, BTREE_ITER_CACHED);
893 if (bpos_eq(*bp_pos, SPOS_MAX))
897 struct printbuf buf = PRINTBUF;
898 struct btree_iter node_iter;
901 b = bch2_backpointer_get_node(trans, &node_iter, *bp_pos, bp);
902 bch2_trans_iter_exit(trans, &node_iter);
907 prt_printf(&buf, "found btree node in erasure coded bucket: b=%px\n", b);
908 bch2_backpointer_to_text(&buf, &bp);
910 bch2_fs_inconsistent(c, "%s", buf.buf);
915 k = bch2_backpointer_get_key(trans, &iter, *bp_pos, bp, BTREE_ITER_INTENT);
921 * extent no longer exists - we could flush the btree
922 * write buffer and retry to verify, but no need:
927 if (extent_has_stripe_ptr(k, s->key.k.p.offset))
930 ptr_c = bkey_matches_stripe(v, k, &block);
932 * It doesn't generally make sense to erasure code cached ptrs:
933 * XXX: should we be incrementing a counter?
935 if (!ptr_c || ptr_c->cached)
938 dev = v->ptrs[block].dev;
940 n = bch2_trans_kmalloc(trans, bkey_bytes(k.k) + sizeof(stripe_ptr));
941 ret = PTR_ERR_OR_ZERO(n);
945 bkey_reassemble(n, k);
947 bch2_bkey_drop_ptrs(bkey_i_to_s(n), ptr, ptr->dev != dev);
948 ec_ptr = bch2_bkey_has_device(bkey_i_to_s(n), dev);
951 stripe_ptr = (struct bch_extent_stripe_ptr) {
952 .type = 1 << BCH_EXTENT_ENTRY_stripe_ptr,
954 .redundancy = v->nr_redundant,
955 .idx = s->key.k.p.offset,
958 __extent_entry_insert(n,
959 (union bch_extent_entry *) ec_ptr,
960 (union bch_extent_entry *) &stripe_ptr);
962 ret = bch2_trans_update(trans, &iter, n, 0);
964 bch2_trans_iter_exit(trans, &iter);
968 static int ec_stripe_update_bucket(struct btree_trans *trans, struct ec_stripe_buf *s,
971 struct bch_fs *c = trans->c;
972 struct bch_stripe *v = &bkey_i_to_stripe(&s->key)->v;
973 struct bch_extent_ptr bucket = v->ptrs[block];
974 struct bpos bucket_pos = PTR_BUCKET_POS(c, &bucket);
975 struct bpos bp_pos = POS_MIN;
979 ret = commit_do(trans, NULL, NULL,
980 BCH_TRANS_COMMIT_no_check_rw|
981 BCH_TRANS_COMMIT_no_enospc,
982 ec_stripe_update_extent(trans, bucket_pos, bucket.gen,
986 if (bkey_eq(bp_pos, POS_MAX))
989 bp_pos = bpos_nosnap_successor(bp_pos);
995 static int ec_stripe_update_extents(struct bch_fs *c, struct ec_stripe_buf *s)
997 struct btree_trans *trans = bch2_trans_get(c);
998 struct bch_stripe *v = &bkey_i_to_stripe(&s->key)->v;
999 unsigned i, nr_data = v->nr_blocks - v->nr_redundant;
1002 ret = bch2_btree_write_buffer_flush_sync(trans);
1006 for (i = 0; i < nr_data; i++) {
1007 ret = ec_stripe_update_bucket(trans, s, i);
1012 bch2_trans_put(trans);
1017 static void zero_out_rest_of_ec_bucket(struct bch_fs *c,
1018 struct ec_stripe_new *s,
1020 struct open_bucket *ob)
1022 struct bch_dev *ca = bch_dev_bkey_exists(c, ob->dev);
1023 unsigned offset = ca->mi.bucket_size - ob->sectors_free;
1026 if (!bch2_dev_get_ioref(ca, WRITE)) {
1027 s->err = -BCH_ERR_erofs_no_writes;
1031 memset(s->new_stripe.data[block] + (offset << 9),
1033 ob->sectors_free << 9);
1035 ret = blkdev_issue_zeroout(ca->disk_sb.bdev,
1036 ob->bucket * ca->mi.bucket_size + offset,
1040 percpu_ref_put(&ca->io_ref);
1046 void bch2_ec_stripe_new_free(struct bch_fs *c, struct ec_stripe_new *s)
1049 bch2_stripe_close(c, s);
1054 * data buckets of new stripe all written: create the stripe
1056 static void ec_stripe_create(struct ec_stripe_new *s)
1058 struct bch_fs *c = s->c;
1059 struct open_bucket *ob;
1060 struct bch_stripe *v = &bkey_i_to_stripe(&s->new_stripe.key)->v;
1061 unsigned i, nr_data = v->nr_blocks - v->nr_redundant;
1064 BUG_ON(s->h->s == s);
1066 closure_sync(&s->iodone);
1069 for (i = 0; i < nr_data; i++)
1071 ob = c->open_buckets + s->blocks[i];
1073 if (ob->sectors_free)
1074 zero_out_rest_of_ec_bucket(c, s, i, ob);
1079 if (!bch2_err_matches(s->err, EROFS))
1080 bch_err(c, "error creating stripe: error writing data buckets");
1084 if (s->have_existing_stripe) {
1085 ec_validate_checksums(c, &s->existing_stripe);
1087 if (ec_do_recov(c, &s->existing_stripe)) {
1088 bch_err(c, "error creating stripe: error reading existing stripe");
1092 for (i = 0; i < nr_data; i++)
1093 if (stripe_blockcount_get(&bkey_i_to_stripe(&s->existing_stripe.key)->v, i))
1094 swap(s->new_stripe.data[i],
1095 s->existing_stripe.data[i]);
1097 ec_stripe_buf_exit(&s->existing_stripe);
1100 BUG_ON(!s->allocated);
1103 ec_generate_ec(&s->new_stripe);
1105 ec_generate_checksums(&s->new_stripe);
1108 for (i = nr_data; i < v->nr_blocks; i++)
1109 ec_block_io(c, &s->new_stripe, REQ_OP_WRITE, i, &s->iodone);
1110 closure_sync(&s->iodone);
1112 if (ec_nr_failed(&s->new_stripe)) {
1113 bch_err(c, "error creating stripe: error writing redundancy buckets");
1117 ret = bch2_trans_do(c, &s->res, NULL,
1118 BCH_TRANS_COMMIT_no_check_rw|
1119 BCH_TRANS_COMMIT_no_enospc,
1120 ec_stripe_key_update(trans,
1121 bkey_i_to_stripe(&s->new_stripe.key),
1122 !s->have_existing_stripe));
1123 bch_err_msg(c, ret, "creating stripe key");
1128 ret = ec_stripe_update_extents(c, &s->new_stripe);
1129 bch_err_msg(c, ret, "error updating extents");
1133 bch2_disk_reservation_put(c, &s->res);
1135 for (i = 0; i < v->nr_blocks; i++)
1137 ob = c->open_buckets + s->blocks[i];
1141 __bch2_open_bucket_put(c, ob);
1143 bch2_open_bucket_put(c, ob);
1147 mutex_lock(&c->ec_stripe_new_lock);
1149 mutex_unlock(&c->ec_stripe_new_lock);
1150 wake_up(&c->ec_stripe_new_wait);
1152 ec_stripe_buf_exit(&s->existing_stripe);
1153 ec_stripe_buf_exit(&s->new_stripe);
1154 closure_debug_destroy(&s->iodone);
1156 ec_stripe_new_put(c, s, STRIPE_REF_stripe);
1159 static struct ec_stripe_new *get_pending_stripe(struct bch_fs *c)
1161 struct ec_stripe_new *s;
1163 mutex_lock(&c->ec_stripe_new_lock);
1164 list_for_each_entry(s, &c->ec_stripe_new_list, list)
1165 if (!atomic_read(&s->ref[STRIPE_REF_io]))
1169 mutex_unlock(&c->ec_stripe_new_lock);
1174 static void ec_stripe_create_work(struct work_struct *work)
1176 struct bch_fs *c = container_of(work,
1177 struct bch_fs, ec_stripe_create_work);
1178 struct ec_stripe_new *s;
1180 while ((s = get_pending_stripe(c)))
1181 ec_stripe_create(s);
1183 bch2_write_ref_put(c, BCH_WRITE_REF_stripe_create);
1186 void bch2_ec_do_stripe_creates(struct bch_fs *c)
1188 bch2_write_ref_get(c, BCH_WRITE_REF_stripe_create);
1190 if (!queue_work(system_long_wq, &c->ec_stripe_create_work))
1191 bch2_write_ref_put(c, BCH_WRITE_REF_stripe_create);
1194 static void ec_stripe_set_pending(struct bch_fs *c, struct ec_stripe_head *h)
1196 struct ec_stripe_new *s = h->s;
1198 BUG_ON(!s->allocated && !s->err);
1203 mutex_lock(&c->ec_stripe_new_lock);
1204 list_add(&s->list, &c->ec_stripe_new_list);
1205 mutex_unlock(&c->ec_stripe_new_lock);
1207 ec_stripe_new_put(c, s, STRIPE_REF_io);
1210 void bch2_ec_bucket_cancel(struct bch_fs *c, struct open_bucket *ob)
1212 struct ec_stripe_new *s = ob->ec;
1217 void *bch2_writepoint_ec_buf(struct bch_fs *c, struct write_point *wp)
1219 struct open_bucket *ob = ec_open_bucket(c, &wp->ptrs);
1226 BUG_ON(!ob->ec->new_stripe.data[ob->ec_idx]);
1228 ca = bch_dev_bkey_exists(c, ob->dev);
1229 offset = ca->mi.bucket_size - ob->sectors_free;
1231 return ob->ec->new_stripe.data[ob->ec_idx] + (offset << 9);
1234 static int unsigned_cmp(const void *_l, const void *_r)
1236 unsigned l = *((const unsigned *) _l);
1237 unsigned r = *((const unsigned *) _r);
1239 return cmp_int(l, r);
1242 /* pick most common bucket size: */
1243 static unsigned pick_blocksize(struct bch_fs *c,
1244 struct bch_devs_mask *devs)
1246 unsigned nr = 0, sizes[BCH_SB_MEMBERS_MAX];
1249 } cur = { 0, 0 }, best = { 0, 0 };
1251 for_each_member_device_rcu(c, ca, devs)
1252 sizes[nr++] = ca->mi.bucket_size;
1254 sort(sizes, nr, sizeof(unsigned), unsigned_cmp, NULL);
1256 for (unsigned i = 0; i < nr; i++) {
1257 if (sizes[i] != cur.size) {
1258 if (cur.nr > best.nr)
1262 cur.size = sizes[i];
1268 if (cur.nr > best.nr)
1274 static bool may_create_new_stripe(struct bch_fs *c)
1279 static void ec_stripe_key_init(struct bch_fs *c,
1283 unsigned stripe_size)
1285 struct bkey_i_stripe *s = bkey_stripe_init(k);
1288 s->v.sectors = cpu_to_le16(stripe_size);
1290 s->v.nr_blocks = nr_data + nr_parity;
1291 s->v.nr_redundant = nr_parity;
1292 s->v.csum_granularity_bits = ilog2(c->opts.encoded_extent_max >> 9);
1293 s->v.csum_type = BCH_CSUM_crc32c;
1296 while ((u64s = stripe_val_u64s(&s->v)) > BKEY_VAL_U64s_MAX) {
1297 BUG_ON(1 << s->v.csum_granularity_bits >=
1298 le16_to_cpu(s->v.sectors) ||
1299 s->v.csum_granularity_bits == U8_MAX);
1300 s->v.csum_granularity_bits++;
1303 set_bkey_val_u64s(&s->k, u64s);
1306 static int ec_new_stripe_alloc(struct bch_fs *c, struct ec_stripe_head *h)
1308 struct ec_stripe_new *s;
1310 lockdep_assert_held(&h->lock);
1312 s = kzalloc(sizeof(*s), GFP_KERNEL);
1314 return -BCH_ERR_ENOMEM_ec_new_stripe_alloc;
1316 mutex_init(&s->lock);
1317 closure_init(&s->iodone, NULL);
1318 atomic_set(&s->ref[STRIPE_REF_stripe], 1);
1319 atomic_set(&s->ref[STRIPE_REF_io], 1);
1322 s->nr_data = min_t(unsigned, h->nr_active_devs,
1323 BCH_BKEY_PTRS_MAX) - h->redundancy;
1324 s->nr_parity = h->redundancy;
1326 ec_stripe_key_init(c, &s->new_stripe.key,
1327 s->nr_data, s->nr_parity, h->blocksize);
1333 static struct ec_stripe_head *
1334 ec_new_stripe_head_alloc(struct bch_fs *c, unsigned target,
1335 unsigned algo, unsigned redundancy,
1336 enum bch_watermark watermark)
1338 struct ec_stripe_head *h;
1340 h = kzalloc(sizeof(*h), GFP_KERNEL);
1344 mutex_init(&h->lock);
1345 BUG_ON(!mutex_trylock(&h->lock));
1349 h->redundancy = redundancy;
1350 h->watermark = watermark;
1353 h->devs = target_rw_devs(c, BCH_DATA_user, target);
1355 for_each_member_device_rcu(c, ca, &h->devs)
1356 if (!ca->mi.durability)
1357 __clear_bit(ca->dev_idx, h->devs.d);
1359 h->blocksize = pick_blocksize(c, &h->devs);
1361 for_each_member_device_rcu(c, ca, &h->devs)
1362 if (ca->mi.bucket_size == h->blocksize)
1363 h->nr_active_devs++;
1368 * If we only have redundancy + 1 devices, we're better off with just
1371 if (h->nr_active_devs < h->redundancy + 2)
1372 bch_err(c, "insufficient devices available to create stripe (have %u, need %u) - mismatched bucket sizes?",
1373 h->nr_active_devs, h->redundancy + 2);
1375 list_add(&h->list, &c->ec_stripe_head_list);
1379 void bch2_ec_stripe_head_put(struct bch_fs *c, struct ec_stripe_head *h)
1383 bitmap_weight(h->s->blocks_allocated,
1384 h->s->nr_data) == h->s->nr_data)
1385 ec_stripe_set_pending(c, h);
1387 mutex_unlock(&h->lock);
1390 static struct ec_stripe_head *
1391 __bch2_ec_stripe_head_get(struct btree_trans *trans,
1394 unsigned redundancy,
1395 enum bch_watermark watermark)
1397 struct bch_fs *c = trans->c;
1398 struct ec_stripe_head *h;
1404 ret = bch2_trans_mutex_lock(trans, &c->ec_stripe_head_lock);
1406 return ERR_PTR(ret);
1408 if (test_bit(BCH_FS_going_ro, &c->flags)) {
1409 h = ERR_PTR(-BCH_ERR_erofs_no_writes);
1413 list_for_each_entry(h, &c->ec_stripe_head_list, list)
1414 if (h->target == target &&
1416 h->redundancy == redundancy &&
1417 h->watermark == watermark) {
1418 ret = bch2_trans_mutex_lock(trans, &h->lock);
1424 h = ec_new_stripe_head_alloc(c, target, algo, redundancy, watermark);
1426 if (!IS_ERR_OR_NULL(h) &&
1427 h->nr_active_devs < h->redundancy + 2) {
1428 mutex_unlock(&h->lock);
1431 mutex_unlock(&c->ec_stripe_head_lock);
1435 static int new_stripe_alloc_buckets(struct btree_trans *trans, struct ec_stripe_head *h,
1436 enum bch_watermark watermark, struct closure *cl)
1438 struct bch_fs *c = trans->c;
1439 struct bch_devs_mask devs = h->devs;
1440 struct open_bucket *ob;
1441 struct open_buckets buckets;
1442 struct bch_stripe *v = &bkey_i_to_stripe(&h->s->new_stripe.key)->v;
1443 unsigned i, j, nr_have_parity = 0, nr_have_data = 0;
1444 bool have_cache = true;
1447 BUG_ON(v->nr_blocks != h->s->nr_data + h->s->nr_parity);
1448 BUG_ON(v->nr_redundant != h->s->nr_parity);
1450 for_each_set_bit(i, h->s->blocks_gotten, v->nr_blocks) {
1451 __clear_bit(v->ptrs[i].dev, devs.d);
1452 if (i < h->s->nr_data)
1458 BUG_ON(nr_have_data > h->s->nr_data);
1459 BUG_ON(nr_have_parity > h->s->nr_parity);
1462 if (nr_have_parity < h->s->nr_parity) {
1463 ret = bch2_bucket_alloc_set_trans(trans, &buckets,
1473 open_bucket_for_each(c, &buckets, ob, i) {
1474 j = find_next_zero_bit(h->s->blocks_gotten,
1475 h->s->nr_data + h->s->nr_parity,
1477 BUG_ON(j >= h->s->nr_data + h->s->nr_parity);
1479 h->s->blocks[j] = buckets.v[i];
1480 v->ptrs[j] = bch2_ob_ptr(c, ob);
1481 __set_bit(j, h->s->blocks_gotten);
1489 if (nr_have_data < h->s->nr_data) {
1490 ret = bch2_bucket_alloc_set_trans(trans, &buckets,
1500 open_bucket_for_each(c, &buckets, ob, i) {
1501 j = find_next_zero_bit(h->s->blocks_gotten,
1503 BUG_ON(j >= h->s->nr_data);
1505 h->s->blocks[j] = buckets.v[i];
1506 v->ptrs[j] = bch2_ob_ptr(c, ob);
1507 __set_bit(j, h->s->blocks_gotten);
1517 /* XXX: doesn't obey target: */
1518 static s64 get_existing_stripe(struct bch_fs *c,
1519 struct ec_stripe_head *head)
1521 ec_stripes_heap *h = &c->ec_stripes_heap;
1527 if (may_create_new_stripe(c))
1530 mutex_lock(&c->ec_stripes_heap_lock);
1531 for (heap_idx = 0; heap_idx < h->used; heap_idx++) {
1532 /* No blocks worth reusing, stripe will just be deleted: */
1533 if (!h->data[heap_idx].blocks_nonempty)
1536 stripe_idx = h->data[heap_idx].idx;
1538 m = genradix_ptr(&c->stripes, stripe_idx);
1540 if (m->algorithm == head->algo &&
1541 m->nr_redundant == head->redundancy &&
1542 m->sectors == head->blocksize &&
1543 m->blocks_nonempty < m->nr_blocks - m->nr_redundant &&
1544 bch2_try_open_stripe(c, head->s, stripe_idx)) {
1549 mutex_unlock(&c->ec_stripes_heap_lock);
1553 static int __bch2_ec_stripe_head_reuse(struct btree_trans *trans, struct ec_stripe_head *h)
1555 struct bch_fs *c = trans->c;
1556 struct bch_stripe *new_v = &bkey_i_to_stripe(&h->s->new_stripe.key)->v;
1557 struct bch_stripe *existing_v;
1563 * If we can't allocate a new stripe, and there's no stripes with empty
1564 * blocks for us to reuse, that means we have to wait on copygc:
1566 idx = get_existing_stripe(c, h);
1568 return -BCH_ERR_stripe_alloc_blocked;
1570 ret = get_stripe_key_trans(trans, idx, &h->s->existing_stripe);
1572 bch2_stripe_close(c, h->s);
1573 if (!bch2_err_matches(ret, BCH_ERR_transaction_restart))
1574 bch2_fs_fatal_error(c, "error reading stripe key: %s", bch2_err_str(ret));
1578 existing_v = &bkey_i_to_stripe(&h->s->existing_stripe.key)->v;
1580 BUG_ON(existing_v->nr_redundant != h->s->nr_parity);
1581 h->s->nr_data = existing_v->nr_blocks -
1582 existing_v->nr_redundant;
1584 ret = ec_stripe_buf_init(&h->s->existing_stripe, 0, h->blocksize);
1586 bch2_stripe_close(c, h->s);
1590 BUG_ON(h->s->existing_stripe.size != h->blocksize);
1591 BUG_ON(h->s->existing_stripe.size != le16_to_cpu(existing_v->sectors));
1594 * Free buckets we initially allocated - they might conflict with
1595 * blocks from the stripe we're reusing:
1597 for_each_set_bit(i, h->s->blocks_gotten, new_v->nr_blocks) {
1598 bch2_open_bucket_put(c, c->open_buckets + h->s->blocks[i]);
1599 h->s->blocks[i] = 0;
1601 memset(h->s->blocks_gotten, 0, sizeof(h->s->blocks_gotten));
1602 memset(h->s->blocks_allocated, 0, sizeof(h->s->blocks_allocated));
1604 for (i = 0; i < existing_v->nr_blocks; i++) {
1605 if (stripe_blockcount_get(existing_v, i)) {
1606 __set_bit(i, h->s->blocks_gotten);
1607 __set_bit(i, h->s->blocks_allocated);
1610 ec_block_io(c, &h->s->existing_stripe, READ, i, &h->s->iodone);
1613 bkey_copy(&h->s->new_stripe.key, &h->s->existing_stripe.key);
1614 h->s->have_existing_stripe = true;
1619 static int __bch2_ec_stripe_head_reserve(struct btree_trans *trans, struct ec_stripe_head *h)
1621 struct bch_fs *c = trans->c;
1622 struct btree_iter iter;
1624 struct bpos min_pos = POS(0, 1);
1625 struct bpos start_pos = bpos_max(min_pos, POS(0, c->ec_stripe_hint));
1628 if (!h->s->res.sectors) {
1629 ret = bch2_disk_reservation_get(c, &h->s->res,
1632 BCH_DISK_RESERVATION_NOFAIL);
1637 for_each_btree_key_norestart(trans, iter, BTREE_ID_stripes, start_pos,
1638 BTREE_ITER_SLOTS|BTREE_ITER_INTENT, k, ret) {
1639 if (bkey_gt(k.k->p, POS(0, U32_MAX))) {
1640 if (start_pos.offset) {
1641 start_pos = min_pos;
1642 bch2_btree_iter_set_pos(&iter, start_pos);
1646 ret = -BCH_ERR_ENOSPC_stripe_create;
1650 if (bkey_deleted(k.k) &&
1651 bch2_try_open_stripe(c, h->s, k.k->p.offset))
1655 c->ec_stripe_hint = iter.pos.offset;
1660 ret = ec_stripe_mem_alloc(trans, &iter);
1662 bch2_stripe_close(c, h->s);
1666 h->s->new_stripe.key.k.p = iter.pos;
1668 bch2_trans_iter_exit(trans, &iter);
1671 bch2_disk_reservation_put(c, &h->s->res);
1675 struct ec_stripe_head *bch2_ec_stripe_head_get(struct btree_trans *trans,
1678 unsigned redundancy,
1679 enum bch_watermark watermark,
1682 struct bch_fs *c = trans->c;
1683 struct ec_stripe_head *h;
1684 bool waiting = false;
1687 h = __bch2_ec_stripe_head_get(trans, target, algo, redundancy, watermark);
1688 if (IS_ERR_OR_NULL(h))
1692 ret = ec_new_stripe_alloc(c, h);
1694 bch_err(c, "failed to allocate new stripe");
1699 if (h->s->allocated)
1702 if (h->s->have_existing_stripe)
1703 goto alloc_existing;
1705 /* First, try to allocate a full stripe: */
1706 ret = new_stripe_alloc_buckets(trans, h, BCH_WATERMARK_stripe, NULL) ?:
1707 __bch2_ec_stripe_head_reserve(trans, h);
1710 if (bch2_err_matches(ret, BCH_ERR_transaction_restart) ||
1711 bch2_err_matches(ret, ENOMEM))
1715 * Not enough buckets available for a full stripe: we must reuse an
1719 ret = __bch2_ec_stripe_head_reuse(trans, h);
1722 if (waiting || !cl || ret != -BCH_ERR_stripe_alloc_blocked)
1725 if (watermark == BCH_WATERMARK_copygc) {
1726 ret = new_stripe_alloc_buckets(trans, h, watermark, NULL) ?:
1727 __bch2_ec_stripe_head_reserve(trans, h);
1733 /* XXX freelist_wait? */
1734 closure_wait(&c->freelist_wait, cl);
1739 closure_wake_up(&c->freelist_wait);
1742 * Retry allocating buckets, with the watermark for this
1745 ret = new_stripe_alloc_buckets(trans, h, watermark, cl);
1750 ret = ec_stripe_buf_init(&h->s->new_stripe, 0, h->blocksize);
1754 h->s->allocated = true;
1757 BUG_ON(!h->s->new_stripe.data[0]);
1758 BUG_ON(trans->restarted);
1761 bch2_ec_stripe_head_put(c, h);
1762 return ERR_PTR(ret);
1765 static void __bch2_ec_stop(struct bch_fs *c, struct bch_dev *ca)
1767 struct ec_stripe_head *h;
1768 struct open_bucket *ob;
1771 mutex_lock(&c->ec_stripe_head_lock);
1772 list_for_each_entry(h, &c->ec_stripe_head_list, list) {
1773 mutex_lock(&h->lock);
1780 for (i = 0; i < bkey_i_to_stripe(&h->s->new_stripe.key)->v.nr_blocks; i++) {
1781 if (!h->s->blocks[i])
1784 ob = c->open_buckets + h->s->blocks[i];
1785 if (ob->dev == ca->dev_idx)
1790 h->s->err = -BCH_ERR_erofs_no_writes;
1791 ec_stripe_set_pending(c, h);
1793 mutex_unlock(&h->lock);
1795 mutex_unlock(&c->ec_stripe_head_lock);
1798 void bch2_ec_stop_dev(struct bch_fs *c, struct bch_dev *ca)
1800 __bch2_ec_stop(c, ca);
1803 void bch2_fs_ec_stop(struct bch_fs *c)
1805 __bch2_ec_stop(c, NULL);
1808 static bool bch2_fs_ec_flush_done(struct bch_fs *c)
1812 mutex_lock(&c->ec_stripe_new_lock);
1813 ret = list_empty(&c->ec_stripe_new_list);
1814 mutex_unlock(&c->ec_stripe_new_lock);
1819 void bch2_fs_ec_flush(struct bch_fs *c)
1821 wait_event(c->ec_stripe_new_wait, bch2_fs_ec_flush_done(c));
1824 int bch2_stripes_read(struct bch_fs *c)
1826 int ret = bch2_trans_run(c,
1827 for_each_btree_key(trans, iter, BTREE_ID_stripes, POS_MIN,
1828 BTREE_ITER_PREFETCH, k, ({
1829 if (k.k->type != KEY_TYPE_stripe)
1832 ret = __ec_stripe_mem_alloc(c, k.k->p.offset, GFP_KERNEL);
1836 const struct bch_stripe *s = bkey_s_c_to_stripe(k).v;
1838 struct stripe *m = genradix_ptr(&c->stripes, k.k->p.offset);
1839 m->sectors = le16_to_cpu(s->sectors);
1840 m->algorithm = s->algorithm;
1841 m->nr_blocks = s->nr_blocks;
1842 m->nr_redundant = s->nr_redundant;
1843 m->blocks_nonempty = 0;
1845 for (unsigned i = 0; i < s->nr_blocks; i++)
1846 m->blocks_nonempty += !!stripe_blockcount_get(s, i);
1848 bch2_stripes_heap_insert(c, m, k.k->p.offset);
1855 void bch2_stripes_heap_to_text(struct printbuf *out, struct bch_fs *c)
1857 ec_stripes_heap *h = &c->ec_stripes_heap;
1861 mutex_lock(&c->ec_stripes_heap_lock);
1862 for (i = 0; i < min_t(size_t, h->used, 50); i++) {
1863 m = genradix_ptr(&c->stripes, h->data[i].idx);
1865 prt_printf(out, "%zu %u/%u+%u", h->data[i].idx,
1866 h->data[i].blocks_nonempty,
1867 m->nr_blocks - m->nr_redundant,
1869 if (bch2_stripe_is_open(c, h->data[i].idx))
1870 prt_str(out, " open");
1873 mutex_unlock(&c->ec_stripes_heap_lock);
1876 void bch2_new_stripes_to_text(struct printbuf *out, struct bch_fs *c)
1878 struct ec_stripe_head *h;
1879 struct ec_stripe_new *s;
1881 mutex_lock(&c->ec_stripe_head_lock);
1882 list_for_each_entry(h, &c->ec_stripe_head_list, list) {
1883 prt_printf(out, "target %u algo %u redundancy %u %s:\n",
1884 h->target, h->algo, h->redundancy,
1885 bch2_watermarks[h->watermark]);
1888 prt_printf(out, "\tidx %llu blocks %u+%u allocated %u\n",
1889 h->s->idx, h->s->nr_data, h->s->nr_parity,
1890 bitmap_weight(h->s->blocks_allocated,
1893 mutex_unlock(&c->ec_stripe_head_lock);
1895 prt_printf(out, "in flight:\n");
1897 mutex_lock(&c->ec_stripe_new_lock);
1898 list_for_each_entry(s, &c->ec_stripe_new_list, list) {
1899 prt_printf(out, "\tidx %llu blocks %u+%u ref %u %u %s\n",
1900 s->idx, s->nr_data, s->nr_parity,
1901 atomic_read(&s->ref[STRIPE_REF_io]),
1902 atomic_read(&s->ref[STRIPE_REF_stripe]),
1903 bch2_watermarks[s->h->watermark]);
1905 mutex_unlock(&c->ec_stripe_new_lock);
1908 void bch2_fs_ec_exit(struct bch_fs *c)
1910 struct ec_stripe_head *h;
1914 mutex_lock(&c->ec_stripe_head_lock);
1915 h = list_first_entry_or_null(&c->ec_stripe_head_list,
1916 struct ec_stripe_head, list);
1919 mutex_unlock(&c->ec_stripe_head_lock);
1924 for (i = 0; i < bkey_i_to_stripe(&h->s->new_stripe.key)->v.nr_blocks; i++)
1925 BUG_ON(h->s->blocks[i]);
1932 BUG_ON(!list_empty(&c->ec_stripe_new_list));
1934 free_heap(&c->ec_stripes_heap);
1935 genradix_free(&c->stripes);
1936 bioset_exit(&c->ec_bioset);
1939 void bch2_fs_ec_init_early(struct bch_fs *c)
1941 spin_lock_init(&c->ec_stripes_new_lock);
1942 mutex_init(&c->ec_stripes_heap_lock);
1944 INIT_LIST_HEAD(&c->ec_stripe_head_list);
1945 mutex_init(&c->ec_stripe_head_lock);
1947 INIT_LIST_HEAD(&c->ec_stripe_new_list);
1948 mutex_init(&c->ec_stripe_new_lock);
1949 init_waitqueue_head(&c->ec_stripe_new_wait);
1951 INIT_WORK(&c->ec_stripe_create_work, ec_stripe_create_work);
1952 INIT_WORK(&c->ec_stripe_delete_work, ec_stripe_delete_work);
1955 int bch2_fs_ec_init(struct bch_fs *c)
1957 return bioset_init(&c->ec_bioset, 1, offsetof(struct ec_bio, bio),