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"
14 #include "disk_groups.h"
24 #include <linux/sort.h>
28 #include <linux/raid/pq.h>
29 #include <linux/raid/xor.h>
31 static void raid5_recov(unsigned disks, unsigned failed_idx,
32 size_t size, void **data)
36 BUG_ON(failed_idx >= disks);
38 swap(data[0], data[failed_idx]);
39 memcpy(data[0], data[1], size);
42 nr = min_t(unsigned, disks - i, MAX_XOR_BLOCKS);
43 xor_blocks(nr, size, data[0], data + i);
47 swap(data[0], data[failed_idx]);
50 static void raid_gen(int nd, int np, size_t size, void **v)
53 raid5_recov(nd + np, nd, size, v);
55 raid6_call.gen_syndrome(nd + np, size, v);
59 static void raid_rec(int nr, int *ir, int nd, int np, size_t size, void **v)
66 raid5_recov(nd + 1, ir[0], size, v);
68 raid6_call.gen_syndrome(nd + np, size, v);
72 /* data+data failure. */
73 raid6_2data_recov(nd + np, size, ir[0], ir[1], v);
74 } else if (ir[0] < nd) {
75 /* data + p/q failure */
77 if (ir[1] == nd) /* data + p failure */
78 raid6_datap_recov(nd + np, size, ir[0], v);
79 else { /* data + q failure */
80 raid5_recov(nd + 1, ir[0], size, v);
81 raid6_call.gen_syndrome(nd + np, size, v);
84 raid_gen(nd, np, size, v);
94 #include <raid/raid.h>
100 struct ec_stripe_buf *buf;
105 /* Stripes btree keys: */
107 int bch2_stripe_invalid(const struct bch_fs *c, struct bkey_s_c k,
108 unsigned flags, struct printbuf *err)
110 const struct bch_stripe *s = bkey_s_c_to_stripe(k).v;
112 if (bkey_eq(k.k->p, POS_MIN)) {
113 prt_printf(err, "stripe at POS_MIN");
114 return -BCH_ERR_invalid_bkey;
118 prt_printf(err, "nonzero inode field");
119 return -BCH_ERR_invalid_bkey;
122 if (bkey_val_u64s(k.k) < stripe_val_u64s(s)) {
123 prt_printf(err, "incorrect value size (%zu < %u)",
124 bkey_val_u64s(k.k), stripe_val_u64s(s));
125 return -BCH_ERR_invalid_bkey;
128 return bch2_bkey_ptrs_invalid(c, k, flags, err);
131 void bch2_stripe_to_text(struct printbuf *out, struct bch_fs *c,
134 const struct bch_stripe *s = bkey_s_c_to_stripe(k).v;
135 unsigned i, nr_data = s->nr_blocks - s->nr_redundant;
137 prt_printf(out, "algo %u sectors %u blocks %u:%u csum %u gran %u",
139 le16_to_cpu(s->sectors),
143 1U << s->csum_granularity_bits);
145 for (i = 0; i < s->nr_blocks; i++) {
146 const struct bch_extent_ptr *ptr = s->ptrs + i;
147 struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
149 u64 b = sector_to_bucket_and_offset(ca, ptr->offset, &offset);
151 prt_printf(out, " %u:%llu:%u", ptr->dev, b, offset);
153 prt_printf(out, "#%u", stripe_blockcount_get(s, i));
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)
204 for (i = 0; i < buf->key.v.nr_blocks; i++) {
205 kvpfree(buf->data[i], buf->size << 9);
210 /* XXX: this is a non-mempoolified memory allocation: */
211 static int ec_stripe_buf_init(struct ec_stripe_buf *buf,
212 unsigned offset, unsigned size)
214 struct bch_stripe *v = &buf->key.v;
215 unsigned csum_granularity = 1U << v->csum_granularity_bits;
216 unsigned end = offset + size;
219 BUG_ON(end > le16_to_cpu(v->sectors));
221 offset = round_down(offset, csum_granularity);
222 end = min_t(unsigned, le16_to_cpu(v->sectors),
223 round_up(end, csum_granularity));
225 buf->offset = offset;
226 buf->size = end - offset;
228 memset(buf->valid, 0xFF, sizeof(buf->valid));
230 for (i = 0; i < buf->key.v.nr_blocks; i++) {
231 buf->data[i] = kvpmalloc(buf->size << 9, GFP_KERNEL);
238 ec_stripe_buf_exit(buf);
239 return -BCH_ERR_ENOMEM_stripe_buf;
244 static struct bch_csum ec_block_checksum(struct ec_stripe_buf *buf,
245 unsigned block, unsigned offset)
247 struct bch_stripe *v = &buf->key.v;
248 unsigned csum_granularity = 1 << v->csum_granularity_bits;
249 unsigned end = buf->offset + buf->size;
250 unsigned len = min(csum_granularity, end - offset);
252 BUG_ON(offset >= end);
253 BUG_ON(offset < buf->offset);
254 BUG_ON(offset & (csum_granularity - 1));
255 BUG_ON(offset + len != le16_to_cpu(v->sectors) &&
256 (len & (csum_granularity - 1)));
258 return bch2_checksum(NULL, v->csum_type,
260 buf->data[block] + ((offset - buf->offset) << 9),
264 static void ec_generate_checksums(struct ec_stripe_buf *buf)
266 struct bch_stripe *v = &buf->key.v;
267 unsigned i, j, csums_per_device = stripe_csums_per_device(v);
273 BUG_ON(buf->size != le16_to_cpu(v->sectors));
275 for (i = 0; i < v->nr_blocks; i++)
276 for (j = 0; j < csums_per_device; j++)
277 stripe_csum_set(v, i, j,
278 ec_block_checksum(buf, i, j << v->csum_granularity_bits));
281 static void ec_validate_checksums(struct bch_fs *c, struct ec_stripe_buf *buf)
283 struct bch_stripe *v = &buf->key.v;
284 unsigned csum_granularity = 1 << v->csum_granularity_bits;
290 for (i = 0; i < v->nr_blocks; i++) {
291 unsigned offset = buf->offset;
292 unsigned end = buf->offset + buf->size;
294 if (!test_bit(i, buf->valid))
297 while (offset < end) {
298 unsigned j = offset >> v->csum_granularity_bits;
299 unsigned len = min(csum_granularity, end - offset);
300 struct bch_csum want = stripe_csum_get(v, i, j);
301 struct bch_csum got = ec_block_checksum(buf, i, offset);
303 if (bch2_crc_cmp(want, got)) {
304 struct printbuf buf2 = PRINTBUF;
306 bch2_bkey_val_to_text(&buf2, c, bkey_i_to_s_c(&buf->key.k_i));
308 bch_err_ratelimited(c,
309 "stripe checksum error for %ps at %u:%u: csum type %u, expected %llx got %llx\n%s",
310 (void *) _RET_IP_, i, j, v->csum_type,
311 want.lo, got.lo, buf2.buf);
312 printbuf_exit(&buf2);
313 clear_bit(i, buf->valid);
322 /* Erasure coding: */
324 static void ec_generate_ec(struct ec_stripe_buf *buf)
326 struct bch_stripe *v = &buf->key.v;
327 unsigned nr_data = v->nr_blocks - v->nr_redundant;
328 unsigned bytes = le16_to_cpu(v->sectors) << 9;
330 raid_gen(nr_data, v->nr_redundant, bytes, buf->data);
333 static unsigned ec_nr_failed(struct ec_stripe_buf *buf)
335 return buf->key.v.nr_blocks -
336 bitmap_weight(buf->valid, buf->key.v.nr_blocks);
339 static int ec_do_recov(struct bch_fs *c, struct ec_stripe_buf *buf)
341 struct bch_stripe *v = &buf->key.v;
342 unsigned i, failed[BCH_BKEY_PTRS_MAX], nr_failed = 0;
343 unsigned nr_data = v->nr_blocks - v->nr_redundant;
344 unsigned bytes = buf->size << 9;
346 if (ec_nr_failed(buf) > v->nr_redundant) {
347 bch_err_ratelimited(c,
348 "error doing reconstruct read: unable to read enough blocks");
352 for (i = 0; i < nr_data; i++)
353 if (!test_bit(i, buf->valid))
354 failed[nr_failed++] = i;
356 raid_rec(nr_failed, failed, nr_data, v->nr_redundant, bytes, buf->data);
362 static void ec_block_endio(struct bio *bio)
364 struct ec_bio *ec_bio = container_of(bio, struct ec_bio, bio);
365 struct bch_stripe *v = &ec_bio->buf->key.v;
366 struct bch_extent_ptr *ptr = &v->ptrs[ec_bio->idx];
367 struct bch_dev *ca = ec_bio->ca;
368 struct closure *cl = bio->bi_private;
370 if (bch2_dev_io_err_on(bio->bi_status, ca, "erasure coding %s error: %s",
371 bio_data_dir(bio) ? "write" : "read",
372 bch2_blk_status_to_str(bio->bi_status)))
373 clear_bit(ec_bio->idx, ec_bio->buf->valid);
375 if (ptr_stale(ca, ptr)) {
376 bch_err_ratelimited(ca->fs,
377 "error %s stripe: stale pointer after io",
378 bio_data_dir(bio) == READ ? "reading from" : "writing to");
379 clear_bit(ec_bio->idx, ec_bio->buf->valid);
382 bio_put(&ec_bio->bio);
383 percpu_ref_put(&ca->io_ref);
387 static void ec_block_io(struct bch_fs *c, struct ec_stripe_buf *buf,
388 unsigned rw, unsigned idx, struct closure *cl)
390 struct bch_stripe *v = &buf->key.v;
391 unsigned offset = 0, bytes = buf->size << 9;
392 struct bch_extent_ptr *ptr = &v->ptrs[idx];
393 struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
394 enum bch_data_type data_type = idx < buf->key.v.nr_blocks - buf->key.v.nr_redundant
398 if (ptr_stale(ca, ptr)) {
399 bch_err_ratelimited(c,
400 "error %s stripe: stale pointer",
401 rw == READ ? "reading from" : "writing to");
402 clear_bit(idx, buf->valid);
406 if (!bch2_dev_get_ioref(ca, rw)) {
407 clear_bit(idx, buf->valid);
411 this_cpu_add(ca->io_done->sectors[rw][data_type], buf->size);
413 while (offset < bytes) {
414 unsigned nr_iovecs = min_t(size_t, BIO_MAX_VECS,
415 DIV_ROUND_UP(bytes, PAGE_SIZE));
416 unsigned b = min_t(size_t, bytes - offset,
417 nr_iovecs << PAGE_SHIFT);
418 struct ec_bio *ec_bio;
420 ec_bio = container_of(bio_alloc_bioset(ca->disk_sb.bdev,
431 ec_bio->bio.bi_iter.bi_sector = ptr->offset + buf->offset + (offset >> 9);
432 ec_bio->bio.bi_end_io = ec_block_endio;
433 ec_bio->bio.bi_private = cl;
435 bch2_bio_map(&ec_bio->bio, buf->data[idx] + offset, b);
438 percpu_ref_get(&ca->io_ref);
440 submit_bio(&ec_bio->bio);
445 percpu_ref_put(&ca->io_ref);
448 static int get_stripe_key_trans(struct btree_trans *trans, u64 idx,
449 struct ec_stripe_buf *stripe)
451 struct btree_iter iter;
455 k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_stripes,
456 POS(0, idx), BTREE_ITER_SLOTS);
460 if (k.k->type != KEY_TYPE_stripe) {
464 bkey_reassemble(&stripe->key.k_i, k);
466 bch2_trans_iter_exit(trans, &iter);
470 static int get_stripe_key(struct bch_fs *c, u64 idx, struct ec_stripe_buf *stripe)
472 return bch2_trans_run(c, get_stripe_key_trans(&trans, idx, stripe));
475 /* recovery read path: */
476 int bch2_ec_read_extent(struct bch_fs *c, struct bch_read_bio *rbio)
478 struct ec_stripe_buf *buf;
480 struct bch_stripe *v;
484 closure_init_stack(&cl);
486 BUG_ON(!rbio->pick.has_ec);
488 buf = kzalloc(sizeof(*buf), GFP_NOFS);
490 return -BCH_ERR_ENOMEM_ec_read_extent;
492 ret = get_stripe_key(c, rbio->pick.ec.idx, buf);
494 bch_err_ratelimited(c,
495 "error doing reconstruct read: error %i looking up stripe", ret);
502 if (!bch2_ptr_matches_stripe(v, rbio->pick)) {
503 bch_err_ratelimited(c,
504 "error doing reconstruct read: pointer doesn't match stripe");
509 offset = rbio->bio.bi_iter.bi_sector - v->ptrs[rbio->pick.ec.block].offset;
510 if (offset + bio_sectors(&rbio->bio) > le16_to_cpu(v->sectors)) {
511 bch_err_ratelimited(c,
512 "error doing reconstruct read: read is bigger than stripe");
517 ret = ec_stripe_buf_init(buf, offset, bio_sectors(&rbio->bio));
521 for (i = 0; i < v->nr_blocks; i++)
522 ec_block_io(c, buf, REQ_OP_READ, i, &cl);
526 if (ec_nr_failed(buf) > v->nr_redundant) {
527 bch_err_ratelimited(c,
528 "error doing reconstruct read: unable to read enough blocks");
533 ec_validate_checksums(c, buf);
535 ret = ec_do_recov(c, buf);
539 memcpy_to_bio(&rbio->bio, rbio->bio.bi_iter,
540 buf->data[rbio->pick.ec.block] + ((offset - buf->offset) << 9));
542 ec_stripe_buf_exit(buf);
547 /* stripe bucket accounting: */
549 static int __ec_stripe_mem_alloc(struct bch_fs *c, size_t idx, gfp_t gfp)
551 ec_stripes_heap n, *h = &c->ec_stripes_heap;
553 if (idx >= h->size) {
554 if (!init_heap(&n, max(1024UL, roundup_pow_of_two(idx + 1)), gfp))
555 return -BCH_ERR_ENOMEM_ec_stripe_mem_alloc;
557 mutex_lock(&c->ec_stripes_heap_lock);
558 if (n.size > h->size) {
559 memcpy(n.data, h->data, h->used * sizeof(h->data[0]));
563 mutex_unlock(&c->ec_stripes_heap_lock);
568 if (!genradix_ptr_alloc(&c->stripes, idx, gfp))
569 return -BCH_ERR_ENOMEM_ec_stripe_mem_alloc;
571 if (c->gc_pos.phase != GC_PHASE_NOT_RUNNING &&
572 !genradix_ptr_alloc(&c->gc_stripes, idx, gfp))
573 return -BCH_ERR_ENOMEM_ec_stripe_mem_alloc;
578 static int ec_stripe_mem_alloc(struct btree_trans *trans,
579 struct btree_iter *iter)
581 return allocate_dropping_locks_errcode(trans,
582 __ec_stripe_mem_alloc(trans->c, iter->pos.offset, _gfp));
586 * Hash table of open stripes:
587 * Stripes that are being created or modified are kept in a hash table, so that
588 * stripe deletion can skip them.
591 static bool __bch2_stripe_is_open(struct bch_fs *c, u64 idx)
593 unsigned hash = hash_64(idx, ilog2(ARRAY_SIZE(c->ec_stripes_new)));
594 struct ec_stripe_new *s;
596 hlist_for_each_entry(s, &c->ec_stripes_new[hash], hash)
602 static bool bch2_stripe_is_open(struct bch_fs *c, u64 idx)
606 spin_lock(&c->ec_stripes_new_lock);
607 ret = __bch2_stripe_is_open(c, idx);
608 spin_unlock(&c->ec_stripes_new_lock);
613 static bool bch2_try_open_stripe(struct bch_fs *c,
614 struct ec_stripe_new *s,
619 spin_lock(&c->ec_stripes_new_lock);
620 ret = !__bch2_stripe_is_open(c, idx);
622 unsigned hash = hash_64(idx, ilog2(ARRAY_SIZE(c->ec_stripes_new)));
625 hlist_add_head(&s->hash, &c->ec_stripes_new[hash]);
627 spin_unlock(&c->ec_stripes_new_lock);
632 static void bch2_stripe_close(struct bch_fs *c, struct ec_stripe_new *s)
636 spin_lock(&c->ec_stripes_new_lock);
637 hlist_del_init(&s->hash);
638 spin_unlock(&c->ec_stripes_new_lock);
643 /* Heap of all existing stripes, ordered by blocks_nonempty */
645 static u64 stripe_idx_to_delete(struct bch_fs *c)
647 ec_stripes_heap *h = &c->ec_stripes_heap;
649 lockdep_assert_held(&c->ec_stripes_heap_lock);
652 h->data[0].blocks_nonempty == 0 &&
653 !bch2_stripe_is_open(c, h->data[0].idx))
654 return h->data[0].idx;
659 static inline int ec_stripes_heap_cmp(ec_stripes_heap *h,
660 struct ec_stripe_heap_entry l,
661 struct ec_stripe_heap_entry r)
663 return ((l.blocks_nonempty > r.blocks_nonempty) -
664 (l.blocks_nonempty < r.blocks_nonempty));
667 static inline void ec_stripes_heap_set_backpointer(ec_stripes_heap *h,
670 struct bch_fs *c = container_of(h, struct bch_fs, ec_stripes_heap);
672 genradix_ptr(&c->stripes, h->data[i].idx)->heap_idx = i;
675 static void heap_verify_backpointer(struct bch_fs *c, size_t idx)
677 ec_stripes_heap *h = &c->ec_stripes_heap;
678 struct stripe *m = genradix_ptr(&c->stripes, idx);
680 BUG_ON(m->heap_idx >= h->used);
681 BUG_ON(h->data[m->heap_idx].idx != idx);
684 void bch2_stripes_heap_del(struct bch_fs *c,
685 struct stripe *m, size_t idx)
687 mutex_lock(&c->ec_stripes_heap_lock);
688 heap_verify_backpointer(c, idx);
690 heap_del(&c->ec_stripes_heap, m->heap_idx,
692 ec_stripes_heap_set_backpointer);
693 mutex_unlock(&c->ec_stripes_heap_lock);
696 void bch2_stripes_heap_insert(struct bch_fs *c,
697 struct stripe *m, size_t idx)
699 mutex_lock(&c->ec_stripes_heap_lock);
700 BUG_ON(heap_full(&c->ec_stripes_heap));
702 heap_add(&c->ec_stripes_heap, ((struct ec_stripe_heap_entry) {
704 .blocks_nonempty = m->blocks_nonempty,
707 ec_stripes_heap_set_backpointer);
709 heap_verify_backpointer(c, idx);
710 mutex_unlock(&c->ec_stripes_heap_lock);
713 void bch2_stripes_heap_update(struct bch_fs *c,
714 struct stripe *m, size_t idx)
716 ec_stripes_heap *h = &c->ec_stripes_heap;
720 mutex_lock(&c->ec_stripes_heap_lock);
721 heap_verify_backpointer(c, idx);
723 h->data[m->heap_idx].blocks_nonempty = m->blocks_nonempty;
726 heap_sift_up(h, i, ec_stripes_heap_cmp,
727 ec_stripes_heap_set_backpointer);
728 heap_sift_down(h, i, ec_stripes_heap_cmp,
729 ec_stripes_heap_set_backpointer);
731 heap_verify_backpointer(c, idx);
733 do_deletes = stripe_idx_to_delete(c) != 0;
734 mutex_unlock(&c->ec_stripes_heap_lock);
737 bch2_do_stripe_deletes(c);
740 /* stripe deletion */
742 static int ec_stripe_delete(struct btree_trans *trans, u64 idx)
744 struct bch_fs *c = trans->c;
745 struct btree_iter iter;
747 struct bkey_s_c_stripe s;
750 k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_stripes, POS(0, idx),
756 if (k.k->type != KEY_TYPE_stripe) {
757 bch2_fs_inconsistent(c, "attempting to delete nonexistent stripe %llu", idx);
762 s = bkey_s_c_to_stripe(k);
763 for (unsigned i = 0; i < s.v->nr_blocks; i++)
764 if (stripe_blockcount_get(s.v, i)) {
765 struct printbuf buf = PRINTBUF;
767 bch2_bkey_val_to_text(&buf, c, k);
768 bch2_fs_inconsistent(c, "attempting to delete nonempty stripe %s", buf.buf);
774 ret = bch2_btree_delete_at(trans, &iter, 0);
776 bch2_trans_iter_exit(trans, &iter);
780 static void ec_stripe_delete_work(struct work_struct *work)
783 container_of(work, struct bch_fs, ec_stripe_delete_work);
784 struct btree_trans trans;
788 bch2_trans_init(&trans, c, 0, 0);
791 mutex_lock(&c->ec_stripes_heap_lock);
792 idx = stripe_idx_to_delete(c);
793 mutex_unlock(&c->ec_stripes_heap_lock);
798 ret = commit_do(&trans, NULL, NULL, BTREE_INSERT_NOFAIL,
799 ec_stripe_delete(&trans, idx));
801 bch_err(c, "%s: err %s", __func__, bch2_err_str(ret));
806 bch2_trans_exit(&trans);
808 bch2_write_ref_put(c, BCH_WRITE_REF_stripe_delete);
811 void bch2_do_stripe_deletes(struct bch_fs *c)
813 if (bch2_write_ref_tryget(c, BCH_WRITE_REF_stripe_delete) &&
814 !queue_work(c->write_ref_wq, &c->ec_stripe_delete_work))
815 bch2_write_ref_put(c, BCH_WRITE_REF_stripe_delete);
818 /* stripe creation: */
820 static int ec_stripe_key_update(struct btree_trans *trans,
821 struct bkey_i_stripe *new,
824 struct bch_fs *c = trans->c;
825 struct btree_iter iter;
829 k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_stripes,
830 new->k.p, BTREE_ITER_INTENT);
835 if (k.k->type != (create ? KEY_TYPE_deleted : KEY_TYPE_stripe)) {
836 bch2_fs_inconsistent(c, "error %s stripe: got existing key type %s",
837 create ? "creating" : "updating",
838 bch2_bkey_types[k.k->type]);
843 if (k.k->type == KEY_TYPE_stripe) {
844 const struct bch_stripe *old = bkey_s_c_to_stripe(k).v;
847 if (old->nr_blocks != new->v.nr_blocks) {
848 bch_err(c, "error updating stripe: nr_blocks does not match");
853 for (i = 0; i < new->v.nr_blocks; i++) {
854 unsigned v = stripe_blockcount_get(old, i);
857 (old->ptrs[i].dev != new->v.ptrs[i].dev ||
858 old->ptrs[i].gen != new->v.ptrs[i].gen ||
859 old->ptrs[i].offset != new->v.ptrs[i].offset));
861 stripe_blockcount_set(&new->v, i, v);
865 ret = bch2_trans_update(trans, &iter, &new->k_i, 0);
867 bch2_trans_iter_exit(trans, &iter);
871 static int ec_stripe_update_extent(struct btree_trans *trans,
872 struct bpos bucket, u8 gen,
873 struct ec_stripe_buf *s,
876 struct bch_fs *c = trans->c;
877 struct bch_backpointer bp;
878 struct btree_iter iter;
880 const struct bch_extent_ptr *ptr_c;
881 struct bch_extent_ptr *ptr, *ec_ptr = NULL;
882 struct bch_extent_stripe_ptr stripe_ptr;
886 ret = bch2_get_next_backpointer(trans, bucket, gen,
887 bp_pos, &bp, BTREE_ITER_CACHED);
890 if (bpos_eq(*bp_pos, SPOS_MAX))
894 struct printbuf buf = PRINTBUF;
895 struct btree_iter node_iter;
898 b = bch2_backpointer_get_node(trans, &node_iter, *bp_pos, bp);
899 bch2_trans_iter_exit(trans, &node_iter);
904 prt_printf(&buf, "found btree node in erasure coded bucket: b=%px\n", b);
905 bch2_backpointer_to_text(&buf, &bp);
907 bch2_fs_inconsistent(c, "%s", buf.buf);
912 k = bch2_backpointer_get_key(trans, &iter, *bp_pos, bp, BTREE_ITER_INTENT);
918 * extent no longer exists - we could flush the btree
919 * write buffer and retry to verify, but no need:
924 if (extent_has_stripe_ptr(k, s->key.k.p.offset))
927 ptr_c = bkey_matches_stripe(&s->key.v, k, &block);
929 * It doesn't generally make sense to erasure code cached ptrs:
930 * XXX: should we be incrementing a counter?
932 if (!ptr_c || ptr_c->cached)
935 dev = s->key.v.ptrs[block].dev;
937 n = bch2_trans_kmalloc(trans, bkey_bytes(k.k) + sizeof(stripe_ptr));
938 ret = PTR_ERR_OR_ZERO(n);
942 bkey_reassemble(n, k);
944 bch2_bkey_drop_ptrs(bkey_i_to_s(n), ptr, ptr->dev != dev);
945 ec_ptr = bch2_bkey_has_device(bkey_i_to_s(n), dev);
948 stripe_ptr = (struct bch_extent_stripe_ptr) {
949 .type = 1 << BCH_EXTENT_ENTRY_stripe_ptr,
951 .redundancy = s->key.v.nr_redundant,
952 .idx = s->key.k.p.offset,
955 __extent_entry_insert(n,
956 (union bch_extent_entry *) ec_ptr,
957 (union bch_extent_entry *) &stripe_ptr);
959 ret = bch2_trans_update(trans, &iter, n, 0);
961 bch2_trans_iter_exit(trans, &iter);
965 static int ec_stripe_update_bucket(struct btree_trans *trans, struct ec_stripe_buf *s,
968 struct bch_fs *c = trans->c;
969 struct bch_extent_ptr bucket = s->key.v.ptrs[block];
970 struct bpos bucket_pos = PTR_BUCKET_POS(c, &bucket);
971 struct bpos bp_pos = POS_MIN;
975 ret = commit_do(trans, NULL, NULL,
976 BTREE_INSERT_NOCHECK_RW|
978 ec_stripe_update_extent(trans, bucket_pos, bucket.gen,
982 if (bkey_eq(bp_pos, POS_MAX))
985 bp_pos = bpos_nosnap_successor(bp_pos);
991 static int ec_stripe_update_extents(struct bch_fs *c, struct ec_stripe_buf *s)
993 struct btree_trans trans;
994 struct bch_stripe *v = &s->key.v;
995 unsigned i, nr_data = v->nr_blocks - v->nr_redundant;
998 bch2_trans_init(&trans, c, 0, 0);
1000 ret = bch2_btree_write_buffer_flush(&trans);
1004 for (i = 0; i < nr_data; i++) {
1005 ret = ec_stripe_update_bucket(&trans, s, i);
1010 bch2_trans_exit(&trans);
1015 static void zero_out_rest_of_ec_bucket(struct bch_fs *c,
1016 struct ec_stripe_new *s,
1018 struct open_bucket *ob)
1020 struct bch_dev *ca = bch_dev_bkey_exists(c, ob->dev);
1021 unsigned offset = ca->mi.bucket_size - ob->sectors_free;
1024 if (!bch2_dev_get_ioref(ca, WRITE)) {
1029 memset(s->new_stripe.data[block] + (offset << 9),
1031 ob->sectors_free << 9);
1033 ret = blkdev_issue_zeroout(ca->disk_sb.bdev,
1034 ob->bucket * ca->mi.bucket_size + offset,
1038 percpu_ref_put(&ca->io_ref);
1044 void bch2_ec_stripe_new_free(struct bch_fs *c, struct ec_stripe_new *s)
1047 bch2_stripe_close(c, s);
1052 * data buckets of new stripe all written: create the stripe
1054 static void ec_stripe_create(struct ec_stripe_new *s)
1056 struct bch_fs *c = s->c;
1057 struct open_bucket *ob;
1058 struct bch_stripe *v = &s->new_stripe.key.v;
1059 unsigned i, nr_data = v->nr_blocks - v->nr_redundant;
1062 BUG_ON(s->h->s == s);
1064 closure_sync(&s->iodone);
1067 for (i = 0; i < nr_data; i++)
1069 ob = c->open_buckets + s->blocks[i];
1071 if (ob->sectors_free)
1072 zero_out_rest_of_ec_bucket(c, s, i, ob);
1077 if (!bch2_err_matches(s->err, EROFS))
1078 bch_err(c, "error creating stripe: error writing data buckets");
1082 if (s->have_existing_stripe) {
1083 ec_validate_checksums(c, &s->existing_stripe);
1085 if (ec_do_recov(c, &s->existing_stripe)) {
1086 bch_err(c, "error creating stripe: error reading existing stripe");
1090 for (i = 0; i < nr_data; i++)
1091 if (stripe_blockcount_get(&s->existing_stripe.key.v, i))
1092 swap(s->new_stripe.data[i],
1093 s->existing_stripe.data[i]);
1095 ec_stripe_buf_exit(&s->existing_stripe);
1098 BUG_ON(!s->allocated);
1101 ec_generate_ec(&s->new_stripe);
1103 ec_generate_checksums(&s->new_stripe);
1106 for (i = nr_data; i < v->nr_blocks; i++)
1107 ec_block_io(c, &s->new_stripe, REQ_OP_WRITE, i, &s->iodone);
1108 closure_sync(&s->iodone);
1110 if (ec_nr_failed(&s->new_stripe)) {
1111 bch_err(c, "error creating stripe: error writing redundancy buckets");
1115 ret = bch2_trans_do(c, &s->res, NULL,
1116 BTREE_INSERT_NOCHECK_RW|
1117 BTREE_INSERT_NOFAIL,
1118 ec_stripe_key_update(&trans, &s->new_stripe.key,
1119 !s->have_existing_stripe));
1121 bch_err(c, "error creating stripe: error creating stripe key");
1125 ret = ec_stripe_update_extents(c, &s->new_stripe);
1127 bch_err(c, "error creating stripe: error updating pointers: %s",
1132 bch2_disk_reservation_put(c, &s->res);
1134 for (i = 0; i < v->nr_blocks; i++)
1136 ob = c->open_buckets + s->blocks[i];
1140 __bch2_open_bucket_put(c, ob);
1142 bch2_open_bucket_put(c, ob);
1146 mutex_lock(&c->ec_stripe_new_lock);
1148 mutex_unlock(&c->ec_stripe_new_lock);
1149 wake_up(&c->ec_stripe_new_wait);
1151 ec_stripe_buf_exit(&s->existing_stripe);
1152 ec_stripe_buf_exit(&s->new_stripe);
1153 closure_debug_destroy(&s->iodone);
1155 ec_stripe_new_put(c, s, STRIPE_REF_stripe);
1158 static struct ec_stripe_new *get_pending_stripe(struct bch_fs *c)
1160 struct ec_stripe_new *s;
1162 mutex_lock(&c->ec_stripe_new_lock);
1163 list_for_each_entry(s, &c->ec_stripe_new_list, list)
1164 if (!atomic_read(&s->ref[STRIPE_REF_io]))
1168 mutex_unlock(&c->ec_stripe_new_lock);
1173 static void ec_stripe_create_work(struct work_struct *work)
1175 struct bch_fs *c = container_of(work,
1176 struct bch_fs, ec_stripe_create_work);
1177 struct ec_stripe_new *s;
1179 while ((s = get_pending_stripe(c)))
1180 ec_stripe_create(s);
1182 bch2_write_ref_put(c, BCH_WRITE_REF_stripe_create);
1185 void bch2_ec_do_stripe_creates(struct bch_fs *c)
1187 bch2_write_ref_get(c, BCH_WRITE_REF_stripe_create);
1189 if (!queue_work(system_long_wq, &c->ec_stripe_create_work))
1190 bch2_write_ref_put(c, BCH_WRITE_REF_stripe_create);
1193 static void ec_stripe_set_pending(struct bch_fs *c, struct ec_stripe_head *h)
1195 struct ec_stripe_new *s = h->s;
1197 BUG_ON(!s->allocated && !s->err);
1202 mutex_lock(&c->ec_stripe_new_lock);
1203 list_add(&s->list, &c->ec_stripe_new_list);
1204 mutex_unlock(&c->ec_stripe_new_lock);
1206 ec_stripe_new_put(c, s, STRIPE_REF_io);
1209 void bch2_ec_bucket_cancel(struct bch_fs *c, struct open_bucket *ob)
1211 struct ec_stripe_new *s = ob->ec;
1216 void *bch2_writepoint_ec_buf(struct bch_fs *c, struct write_point *wp)
1218 struct open_bucket *ob = ec_open_bucket(c, &wp->ptrs);
1225 BUG_ON(!ob->ec->new_stripe.data[ob->ec_idx]);
1227 ca = bch_dev_bkey_exists(c, ob->dev);
1228 offset = ca->mi.bucket_size - ob->sectors_free;
1230 return ob->ec->new_stripe.data[ob->ec_idx] + (offset << 9);
1233 static int unsigned_cmp(const void *_l, const void *_r)
1235 unsigned l = *((const unsigned *) _l);
1236 unsigned r = *((const unsigned *) _r);
1238 return cmp_int(l, r);
1241 /* pick most common bucket size: */
1242 static unsigned pick_blocksize(struct bch_fs *c,
1243 struct bch_devs_mask *devs)
1246 unsigned i, nr = 0, sizes[BCH_SB_MEMBERS_MAX];
1249 } cur = { 0, 0 }, best = { 0, 0 };
1251 for_each_member_device_rcu(ca, c, i, devs)
1252 sizes[nr++] = ca->mi.bucket_size;
1254 sort(sizes, nr, sizeof(unsigned), unsigned_cmp, NULL);
1256 for (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,
1280 struct bkey_i_stripe *s,
1283 unsigned stripe_size)
1287 bkey_stripe_init(&s->k_i);
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, s->nr_data,
1327 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 alloc_reserve reserve)
1338 struct ec_stripe_head *h;
1342 h = kzalloc(sizeof(*h), GFP_KERNEL);
1346 mutex_init(&h->lock);
1347 BUG_ON(!mutex_trylock(&h->lock));
1351 h->redundancy = redundancy;
1352 h->reserve = reserve;
1355 h->devs = target_rw_devs(c, BCH_DATA_user, target);
1357 for_each_member_device_rcu(ca, c, i, &h->devs)
1358 if (!ca->mi.durability)
1359 __clear_bit(i, h->devs.d);
1361 h->blocksize = pick_blocksize(c, &h->devs);
1363 for_each_member_device_rcu(ca, c, i, &h->devs)
1364 if (ca->mi.bucket_size == h->blocksize)
1365 h->nr_active_devs++;
1368 list_add(&h->list, &c->ec_stripe_head_list);
1372 void bch2_ec_stripe_head_put(struct bch_fs *c, struct ec_stripe_head *h)
1376 bitmap_weight(h->s->blocks_allocated,
1377 h->s->nr_data) == h->s->nr_data)
1378 ec_stripe_set_pending(c, h);
1380 mutex_unlock(&h->lock);
1383 struct ec_stripe_head *__bch2_ec_stripe_head_get(struct btree_trans *trans,
1386 unsigned redundancy,
1387 enum alloc_reserve reserve)
1389 struct bch_fs *c = trans->c;
1390 struct ec_stripe_head *h;
1396 ret = bch2_trans_mutex_lock(trans, &c->ec_stripe_head_lock);
1398 return ERR_PTR(ret);
1400 if (test_bit(BCH_FS_GOING_RO, &c->flags)) {
1401 h = ERR_PTR(-EROFS);
1405 list_for_each_entry(h, &c->ec_stripe_head_list, list)
1406 if (h->target == target &&
1408 h->redundancy == redundancy &&
1409 h->reserve == reserve) {
1410 ret = bch2_trans_mutex_lock(trans, &h->lock);
1416 h = ec_new_stripe_head_alloc(c, target, algo, redundancy, reserve);
1418 mutex_unlock(&c->ec_stripe_head_lock);
1422 static int new_stripe_alloc_buckets(struct btree_trans *trans, struct ec_stripe_head *h,
1423 enum alloc_reserve reserve, struct closure *cl)
1425 struct bch_fs *c = trans->c;
1426 struct bch_devs_mask devs = h->devs;
1427 struct open_bucket *ob;
1428 struct open_buckets buckets;
1429 unsigned i, j, nr_have_parity = 0, nr_have_data = 0;
1430 bool have_cache = true;
1433 BUG_ON(h->s->new_stripe.key.v.nr_blocks != h->s->nr_data + h->s->nr_parity);
1434 BUG_ON(h->s->new_stripe.key.v.nr_redundant != h->s->nr_parity);
1436 for_each_set_bit(i, h->s->blocks_gotten, h->s->new_stripe.key.v.nr_blocks) {
1437 __clear_bit(h->s->new_stripe.key.v.ptrs[i].dev, devs.d);
1438 if (i < h->s->nr_data)
1444 BUG_ON(nr_have_data > h->s->nr_data);
1445 BUG_ON(nr_have_parity > h->s->nr_parity);
1448 if (nr_have_parity < h->s->nr_parity) {
1449 ret = bch2_bucket_alloc_set_trans(trans, &buckets,
1459 open_bucket_for_each(c, &buckets, ob, i) {
1460 j = find_next_zero_bit(h->s->blocks_gotten,
1461 h->s->nr_data + h->s->nr_parity,
1463 BUG_ON(j >= h->s->nr_data + h->s->nr_parity);
1465 h->s->blocks[j] = buckets.v[i];
1466 h->s->new_stripe.key.v.ptrs[j] = bch2_ob_ptr(c, ob);
1467 __set_bit(j, h->s->blocks_gotten);
1475 if (nr_have_data < h->s->nr_data) {
1476 ret = bch2_bucket_alloc_set_trans(trans, &buckets,
1486 open_bucket_for_each(c, &buckets, ob, i) {
1487 j = find_next_zero_bit(h->s->blocks_gotten,
1489 BUG_ON(j >= h->s->nr_data);
1491 h->s->blocks[j] = buckets.v[i];
1492 h->s->new_stripe.key.v.ptrs[j] = bch2_ob_ptr(c, ob);
1493 __set_bit(j, h->s->blocks_gotten);
1503 /* XXX: doesn't obey target: */
1504 static s64 get_existing_stripe(struct bch_fs *c,
1505 struct ec_stripe_head *head)
1507 ec_stripes_heap *h = &c->ec_stripes_heap;
1513 if (may_create_new_stripe(c))
1516 mutex_lock(&c->ec_stripes_heap_lock);
1517 for (heap_idx = 0; heap_idx < h->used; heap_idx++) {
1518 /* No blocks worth reusing, stripe will just be deleted: */
1519 if (!h->data[heap_idx].blocks_nonempty)
1522 stripe_idx = h->data[heap_idx].idx;
1524 m = genradix_ptr(&c->stripes, stripe_idx);
1526 if (m->algorithm == head->algo &&
1527 m->nr_redundant == head->redundancy &&
1528 m->sectors == head->blocksize &&
1529 m->blocks_nonempty < m->nr_blocks - m->nr_redundant &&
1530 bch2_try_open_stripe(c, head->s, stripe_idx)) {
1535 mutex_unlock(&c->ec_stripes_heap_lock);
1539 static int __bch2_ec_stripe_head_reuse(struct btree_trans *trans, struct ec_stripe_head *h)
1541 struct bch_fs *c = trans->c;
1547 * If we can't allocate a new stripe, and there's no stripes with empty
1548 * blocks for us to reuse, that means we have to wait on copygc:
1550 idx = get_existing_stripe(c, h);
1552 return -BCH_ERR_stripe_alloc_blocked;
1554 ret = get_stripe_key_trans(trans, idx, &h->s->existing_stripe);
1556 bch2_stripe_close(c, h->s);
1557 if (!bch2_err_matches(ret, BCH_ERR_transaction_restart))
1558 bch2_fs_fatal_error(c, "error reading stripe key: %s", bch2_err_str(ret));
1562 BUG_ON(h->s->existing_stripe.key.v.nr_redundant != h->s->nr_parity);
1563 h->s->nr_data = h->s->existing_stripe.key.v.nr_blocks -
1564 h->s->existing_stripe.key.v.nr_redundant;
1566 ret = ec_stripe_buf_init(&h->s->existing_stripe, 0, h->blocksize);
1568 bch2_stripe_close(c, h->s);
1572 BUG_ON(h->s->existing_stripe.size != h->blocksize);
1573 BUG_ON(h->s->existing_stripe.size != h->s->existing_stripe.key.v.sectors);
1576 * Free buckets we initially allocated - they might conflict with
1577 * blocks from the stripe we're reusing:
1579 for_each_set_bit(i, h->s->blocks_gotten, h->s->new_stripe.key.v.nr_blocks) {
1580 bch2_open_bucket_put(c, c->open_buckets + h->s->blocks[i]);
1581 h->s->blocks[i] = 0;
1583 memset(h->s->blocks_gotten, 0, sizeof(h->s->blocks_gotten));
1584 memset(h->s->blocks_allocated, 0, sizeof(h->s->blocks_allocated));
1586 for (i = 0; i < h->s->existing_stripe.key.v.nr_blocks; i++) {
1587 if (stripe_blockcount_get(&h->s->existing_stripe.key.v, i)) {
1588 __set_bit(i, h->s->blocks_gotten);
1589 __set_bit(i, h->s->blocks_allocated);
1592 ec_block_io(c, &h->s->existing_stripe, READ, i, &h->s->iodone);
1595 bkey_copy(&h->s->new_stripe.key.k_i, &h->s->existing_stripe.key.k_i);
1596 h->s->have_existing_stripe = true;
1601 static int __bch2_ec_stripe_head_reserve(struct btree_trans *trans, struct ec_stripe_head *h)
1603 struct bch_fs *c = trans->c;
1604 struct btree_iter iter;
1606 struct bpos min_pos = POS(0, 1);
1607 struct bpos start_pos = bpos_max(min_pos, POS(0, c->ec_stripe_hint));
1610 if (!h->s->res.sectors) {
1611 ret = bch2_disk_reservation_get(c, &h->s->res,
1614 BCH_DISK_RESERVATION_NOFAIL);
1619 for_each_btree_key_norestart(trans, iter, BTREE_ID_stripes, start_pos,
1620 BTREE_ITER_SLOTS|BTREE_ITER_INTENT, k, ret) {
1621 if (bkey_gt(k.k->p, POS(0, U32_MAX))) {
1622 if (start_pos.offset) {
1623 start_pos = min_pos;
1624 bch2_btree_iter_set_pos(&iter, start_pos);
1628 ret = -BCH_ERR_ENOSPC_stripe_create;
1632 if (bkey_deleted(k.k) &&
1633 bch2_try_open_stripe(c, h->s, k.k->p.offset))
1637 c->ec_stripe_hint = iter.pos.offset;
1642 ret = ec_stripe_mem_alloc(trans, &iter);
1644 bch2_stripe_close(c, h->s);
1648 h->s->new_stripe.key.k.p = iter.pos;
1650 bch2_trans_iter_exit(trans, &iter);
1653 bch2_disk_reservation_put(c, &h->s->res);
1657 struct ec_stripe_head *bch2_ec_stripe_head_get(struct btree_trans *trans,
1660 unsigned redundancy,
1661 enum alloc_reserve reserve,
1664 struct bch_fs *c = trans->c;
1665 struct ec_stripe_head *h;
1666 bool waiting = false;
1669 h = __bch2_ec_stripe_head_get(trans, target, algo, redundancy, reserve);
1671 bch_err(c, "no stripe head");
1672 if (IS_ERR_OR_NULL(h))
1676 ret = ec_new_stripe_alloc(c, h);
1678 bch_err(c, "failed to allocate new stripe");
1683 if (h->s->allocated)
1686 if (h->s->have_existing_stripe)
1687 goto alloc_existing;
1689 /* First, try to allocate a full stripe: */
1690 ret = new_stripe_alloc_buckets(trans, h, RESERVE_stripe, NULL) ?:
1691 __bch2_ec_stripe_head_reserve(trans, h);
1694 if (bch2_err_matches(ret, BCH_ERR_transaction_restart) ||
1695 bch2_err_matches(ret, ENOMEM))
1699 * Not enough buckets available for a full stripe: we must reuse an
1703 ret = __bch2_ec_stripe_head_reuse(trans, h);
1706 if (waiting || !cl || ret != -BCH_ERR_stripe_alloc_blocked)
1709 if (reserve == RESERVE_movinggc) {
1710 ret = new_stripe_alloc_buckets(trans, h, reserve, NULL) ?:
1711 __bch2_ec_stripe_head_reserve(trans, h);
1717 /* XXX freelist_wait? */
1718 closure_wait(&c->freelist_wait, cl);
1723 closure_wake_up(&c->freelist_wait);
1726 * Retry allocating buckets, with the reserve watermark for this
1729 ret = new_stripe_alloc_buckets(trans, h, reserve, cl);
1734 ret = ec_stripe_buf_init(&h->s->new_stripe, 0, h->blocksize);
1738 h->s->allocated = true;
1741 BUG_ON(!h->s->new_stripe.data[0]);
1742 BUG_ON(trans->restarted);
1745 bch2_ec_stripe_head_put(c, h);
1746 return ERR_PTR(ret);
1749 static void __bch2_ec_stop(struct bch_fs *c, struct bch_dev *ca)
1751 struct ec_stripe_head *h;
1752 struct open_bucket *ob;
1755 mutex_lock(&c->ec_stripe_head_lock);
1756 list_for_each_entry(h, &c->ec_stripe_head_list, list) {
1757 mutex_lock(&h->lock);
1764 for (i = 0; i < h->s->new_stripe.key.v.nr_blocks; i++) {
1765 if (!h->s->blocks[i])
1768 ob = c->open_buckets + h->s->blocks[i];
1769 if (ob->dev == ca->dev_idx)
1775 ec_stripe_set_pending(c, h);
1777 mutex_unlock(&h->lock);
1779 mutex_unlock(&c->ec_stripe_head_lock);
1782 void bch2_ec_stop_dev(struct bch_fs *c, struct bch_dev *ca)
1784 __bch2_ec_stop(c, ca);
1787 void bch2_fs_ec_stop(struct bch_fs *c)
1789 __bch2_ec_stop(c, NULL);
1792 static bool bch2_fs_ec_flush_done(struct bch_fs *c)
1796 mutex_lock(&c->ec_stripe_new_lock);
1797 ret = list_empty(&c->ec_stripe_new_list);
1798 mutex_unlock(&c->ec_stripe_new_lock);
1803 void bch2_fs_ec_flush(struct bch_fs *c)
1805 wait_event(c->ec_stripe_new_wait, bch2_fs_ec_flush_done(c));
1808 int bch2_stripes_read(struct bch_fs *c)
1810 struct btree_trans trans;
1811 struct btree_iter iter;
1813 const struct bch_stripe *s;
1818 bch2_trans_init(&trans, c, 0, 0);
1820 for_each_btree_key(&trans, iter, BTREE_ID_stripes, POS_MIN,
1821 BTREE_ITER_PREFETCH, k, ret) {
1822 if (k.k->type != KEY_TYPE_stripe)
1825 ret = __ec_stripe_mem_alloc(c, k.k->p.offset, GFP_KERNEL);
1829 s = bkey_s_c_to_stripe(k).v;
1831 m = genradix_ptr(&c->stripes, k.k->p.offset);
1832 m->sectors = le16_to_cpu(s->sectors);
1833 m->algorithm = s->algorithm;
1834 m->nr_blocks = s->nr_blocks;
1835 m->nr_redundant = s->nr_redundant;
1836 m->blocks_nonempty = 0;
1838 for (i = 0; i < s->nr_blocks; i++)
1839 m->blocks_nonempty += !!stripe_blockcount_get(s, i);
1841 bch2_stripes_heap_insert(c, m, k.k->p.offset);
1843 bch2_trans_iter_exit(&trans, &iter);
1845 bch2_trans_exit(&trans);
1848 bch_err(c, "error reading stripes: %i", ret);
1853 void bch2_stripes_heap_to_text(struct printbuf *out, struct bch_fs *c)
1855 ec_stripes_heap *h = &c->ec_stripes_heap;
1859 mutex_lock(&c->ec_stripes_heap_lock);
1860 for (i = 0; i < min_t(size_t, h->used, 50); i++) {
1861 m = genradix_ptr(&c->stripes, h->data[i].idx);
1863 prt_printf(out, "%zu %u/%u+%u", h->data[i].idx,
1864 h->data[i].blocks_nonempty,
1865 m->nr_blocks - m->nr_redundant,
1867 if (bch2_stripe_is_open(c, h->data[i].idx))
1868 prt_str(out, " open");
1871 mutex_unlock(&c->ec_stripes_heap_lock);
1874 void bch2_new_stripes_to_text(struct printbuf *out, struct bch_fs *c)
1876 struct ec_stripe_head *h;
1877 struct ec_stripe_new *s;
1879 mutex_lock(&c->ec_stripe_head_lock);
1880 list_for_each_entry(h, &c->ec_stripe_head_list, list) {
1881 prt_printf(out, "target %u algo %u redundancy %u %s:\n",
1882 h->target, h->algo, h->redundancy,
1883 bch2_alloc_reserves[h->reserve]);
1886 prt_printf(out, "\tidx %llu blocks %u+%u allocated %u\n",
1887 h->s->idx, h->s->nr_data, h->s->nr_parity,
1888 bitmap_weight(h->s->blocks_allocated,
1891 mutex_unlock(&c->ec_stripe_head_lock);
1893 prt_printf(out, "in flight:\n");
1895 mutex_lock(&c->ec_stripe_new_lock);
1896 list_for_each_entry(s, &c->ec_stripe_new_list, list) {
1897 prt_printf(out, "\tidx %llu blocks %u+%u ref %u %u %s\n",
1898 s->idx, s->nr_data, s->nr_parity,
1899 atomic_read(&s->ref[STRIPE_REF_io]),
1900 atomic_read(&s->ref[STRIPE_REF_stripe]),
1901 bch2_alloc_reserves[s->h->reserve]);
1903 mutex_unlock(&c->ec_stripe_new_lock);
1906 void bch2_fs_ec_exit(struct bch_fs *c)
1908 struct ec_stripe_head *h;
1912 mutex_lock(&c->ec_stripe_head_lock);
1913 h = list_first_entry_or_null(&c->ec_stripe_head_list,
1914 struct ec_stripe_head, list);
1917 mutex_unlock(&c->ec_stripe_head_lock);
1922 for (i = 0; i < h->s->new_stripe.key.v.nr_blocks; i++)
1923 BUG_ON(h->s->blocks[i]);
1930 BUG_ON(!list_empty(&c->ec_stripe_new_list));
1932 free_heap(&c->ec_stripes_heap);
1933 genradix_free(&c->stripes);
1934 bioset_exit(&c->ec_bioset);
1937 void bch2_fs_ec_init_early(struct bch_fs *c)
1939 spin_lock_init(&c->ec_stripes_new_lock);
1940 mutex_init(&c->ec_stripes_heap_lock);
1942 INIT_LIST_HEAD(&c->ec_stripe_head_list);
1943 mutex_init(&c->ec_stripe_head_lock);
1945 INIT_LIST_HEAD(&c->ec_stripe_new_list);
1946 mutex_init(&c->ec_stripe_new_lock);
1947 init_waitqueue_head(&c->ec_stripe_new_wait);
1949 INIT_WORK(&c->ec_stripe_create_work, ec_stripe_create_work);
1950 INIT_WORK(&c->ec_stripe_delete_work, ec_stripe_delete_work);
1953 int bch2_fs_ec_init(struct bch_fs *c)
1955 return bioset_init(&c->ec_bioset, 1, offsetof(struct ec_bio, bio),