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 enum bkey_invalid_flags flags,
109 struct printbuf *err)
111 const struct bch_stripe *s = bkey_s_c_to_stripe(k).v;
113 if (bkey_eq(k.k->p, POS_MIN)) {
114 prt_printf(err, "stripe at POS_MIN");
115 return -BCH_ERR_invalid_bkey;
119 prt_printf(err, "nonzero inode field");
120 return -BCH_ERR_invalid_bkey;
123 if (bkey_val_u64s(k.k) < stripe_val_u64s(s)) {
124 prt_printf(err, "incorrect value size (%zu < %u)",
125 bkey_val_u64s(k.k), stripe_val_u64s(s));
126 return -BCH_ERR_invalid_bkey;
129 return bch2_bkey_ptrs_invalid(c, k, flags, err);
132 void bch2_stripe_to_text(struct printbuf *out, struct bch_fs *c,
135 const struct bch_stripe *s = bkey_s_c_to_stripe(k).v;
136 unsigned i, nr_data = s->nr_blocks - s->nr_redundant;
138 prt_printf(out, "algo %u sectors %u blocks %u:%u csum %u gran %u",
140 le16_to_cpu(s->sectors),
144 1U << s->csum_granularity_bits);
146 for (i = 0; i < s->nr_blocks; i++) {
147 const struct bch_extent_ptr *ptr = s->ptrs + i;
148 struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
150 u64 b = sector_to_bucket_and_offset(ca, ptr->offset, &offset);
152 prt_printf(out, " %u:%llu:%u", ptr->dev, b, offset);
154 prt_printf(out, "#%u", stripe_blockcount_get(s, i));
155 if (ptr_stale(ca, ptr))
156 prt_printf(out, " stale");
160 /* returns blocknr in stripe that we matched: */
161 static const struct bch_extent_ptr *bkey_matches_stripe(struct bch_stripe *s,
162 struct bkey_s_c k, unsigned *block)
164 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
165 const struct bch_extent_ptr *ptr;
166 unsigned i, nr_data = s->nr_blocks - s->nr_redundant;
168 bkey_for_each_ptr(ptrs, ptr)
169 for (i = 0; i < nr_data; i++)
170 if (__bch2_ptr_matches_stripe(&s->ptrs[i], ptr,
171 le16_to_cpu(s->sectors))) {
179 static bool extent_has_stripe_ptr(struct bkey_s_c k, u64 idx)
182 case KEY_TYPE_extent: {
183 struct bkey_s_c_extent e = bkey_s_c_to_extent(k);
184 const union bch_extent_entry *entry;
186 extent_for_each_entry(e, entry)
187 if (extent_entry_type(entry) ==
188 BCH_EXTENT_ENTRY_stripe_ptr &&
189 entry->stripe_ptr.idx == idx)
201 static void ec_stripe_buf_exit(struct ec_stripe_buf *buf)
205 for (i = 0; i < buf->key.v.nr_blocks; i++) {
206 kvpfree(buf->data[i], buf->size << 9);
211 /* XXX: this is a non-mempoolified memory allocation: */
212 static int ec_stripe_buf_init(struct ec_stripe_buf *buf,
213 unsigned offset, unsigned size)
215 struct bch_stripe *v = &buf->key.v;
216 unsigned csum_granularity = 1U << v->csum_granularity_bits;
217 unsigned end = offset + size;
220 BUG_ON(end > le16_to_cpu(v->sectors));
222 offset = round_down(offset, csum_granularity);
223 end = min_t(unsigned, le16_to_cpu(v->sectors),
224 round_up(end, csum_granularity));
226 buf->offset = offset;
227 buf->size = end - offset;
229 memset(buf->valid, 0xFF, sizeof(buf->valid));
231 for (i = 0; i < buf->key.v.nr_blocks; i++) {
232 buf->data[i] = kvpmalloc(buf->size << 9, GFP_KERNEL);
239 ec_stripe_buf_exit(buf);
240 return -BCH_ERR_ENOMEM_stripe_buf;
245 static struct bch_csum ec_block_checksum(struct ec_stripe_buf *buf,
246 unsigned block, unsigned offset)
248 struct bch_stripe *v = &buf->key.v;
249 unsigned csum_granularity = 1 << v->csum_granularity_bits;
250 unsigned end = buf->offset + buf->size;
251 unsigned len = min(csum_granularity, end - offset);
253 BUG_ON(offset >= end);
254 BUG_ON(offset < buf->offset);
255 BUG_ON(offset & (csum_granularity - 1));
256 BUG_ON(offset + len != le16_to_cpu(v->sectors) &&
257 (len & (csum_granularity - 1)));
259 return bch2_checksum(NULL, v->csum_type,
261 buf->data[block] + ((offset - buf->offset) << 9),
265 static void ec_generate_checksums(struct ec_stripe_buf *buf)
267 struct bch_stripe *v = &buf->key.v;
268 unsigned i, j, csums_per_device = stripe_csums_per_device(v);
274 BUG_ON(buf->size != le16_to_cpu(v->sectors));
276 for (i = 0; i < v->nr_blocks; i++)
277 for (j = 0; j < csums_per_device; j++)
278 stripe_csum_set(v, i, j,
279 ec_block_checksum(buf, i, j << v->csum_granularity_bits));
282 static void ec_validate_checksums(struct bch_fs *c, struct ec_stripe_buf *buf)
284 struct bch_stripe *v = &buf->key.v;
285 unsigned csum_granularity = 1 << v->csum_granularity_bits;
291 for (i = 0; i < v->nr_blocks; i++) {
292 unsigned offset = buf->offset;
293 unsigned end = buf->offset + buf->size;
295 if (!test_bit(i, buf->valid))
298 while (offset < end) {
299 unsigned j = offset >> v->csum_granularity_bits;
300 unsigned len = min(csum_granularity, end - offset);
301 struct bch_csum want = stripe_csum_get(v, i, j);
302 struct bch_csum got = ec_block_checksum(buf, i, offset);
304 if (bch2_crc_cmp(want, got)) {
305 struct printbuf buf2 = PRINTBUF;
307 bch2_bkey_val_to_text(&buf2, c, bkey_i_to_s_c(&buf->key.k_i));
309 bch_err_ratelimited(c,
310 "stripe checksum error for %ps at %u:%u: csum type %u, expected %llx got %llx\n%s",
311 (void *) _RET_IP_, i, j, v->csum_type,
312 want.lo, got.lo, buf2.buf);
313 printbuf_exit(&buf2);
314 clear_bit(i, buf->valid);
323 /* Erasure coding: */
325 static void ec_generate_ec(struct ec_stripe_buf *buf)
327 struct bch_stripe *v = &buf->key.v;
328 unsigned nr_data = v->nr_blocks - v->nr_redundant;
329 unsigned bytes = le16_to_cpu(v->sectors) << 9;
331 raid_gen(nr_data, v->nr_redundant, bytes, buf->data);
334 static unsigned ec_nr_failed(struct ec_stripe_buf *buf)
336 return buf->key.v.nr_blocks -
337 bitmap_weight(buf->valid, buf->key.v.nr_blocks);
340 static int ec_do_recov(struct bch_fs *c, struct ec_stripe_buf *buf)
342 struct bch_stripe *v = &buf->key.v;
343 unsigned i, failed[BCH_BKEY_PTRS_MAX], nr_failed = 0;
344 unsigned nr_data = v->nr_blocks - v->nr_redundant;
345 unsigned bytes = buf->size << 9;
347 if (ec_nr_failed(buf) > v->nr_redundant) {
348 bch_err_ratelimited(c,
349 "error doing reconstruct read: unable to read enough blocks");
353 for (i = 0; i < nr_data; i++)
354 if (!test_bit(i, buf->valid))
355 failed[nr_failed++] = i;
357 raid_rec(nr_failed, failed, nr_data, v->nr_redundant, bytes, buf->data);
363 static void ec_block_endio(struct bio *bio)
365 struct ec_bio *ec_bio = container_of(bio, struct ec_bio, bio);
366 struct bch_stripe *v = &ec_bio->buf->key.v;
367 struct bch_extent_ptr *ptr = &v->ptrs[ec_bio->idx];
368 struct bch_dev *ca = ec_bio->ca;
369 struct closure *cl = bio->bi_private;
371 if (bch2_dev_io_err_on(bio->bi_status, ca, "erasure coding %s error: %s",
372 bio_data_dir(bio) ? "write" : "read",
373 bch2_blk_status_to_str(bio->bi_status)))
374 clear_bit(ec_bio->idx, ec_bio->buf->valid);
376 if (ptr_stale(ca, ptr)) {
377 bch_err_ratelimited(ca->fs,
378 "error %s stripe: stale pointer after io",
379 bio_data_dir(bio) == READ ? "reading from" : "writing to");
380 clear_bit(ec_bio->idx, ec_bio->buf->valid);
383 bio_put(&ec_bio->bio);
384 percpu_ref_put(&ca->io_ref);
388 static void ec_block_io(struct bch_fs *c, struct ec_stripe_buf *buf,
389 blk_opf_t opf, unsigned idx, struct closure *cl)
391 struct bch_stripe *v = &buf->key.v;
392 unsigned offset = 0, bytes = buf->size << 9;
393 struct bch_extent_ptr *ptr = &v->ptrs[idx];
394 struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
395 enum bch_data_type data_type = idx < buf->key.v.nr_blocks - buf->key.v.nr_redundant
398 int rw = op_is_write(opf);
400 if (ptr_stale(ca, ptr)) {
401 bch_err_ratelimited(c,
402 "error %s stripe: stale pointer",
403 rw == READ ? "reading from" : "writing to");
404 clear_bit(idx, buf->valid);
408 if (!bch2_dev_get_ioref(ca, rw)) {
409 clear_bit(idx, buf->valid);
413 this_cpu_add(ca->io_done->sectors[rw][data_type], buf->size);
415 while (offset < bytes) {
416 unsigned nr_iovecs = min_t(size_t, BIO_MAX_VECS,
417 DIV_ROUND_UP(bytes, PAGE_SIZE));
418 unsigned b = min_t(size_t, bytes - offset,
419 nr_iovecs << PAGE_SHIFT);
420 struct ec_bio *ec_bio;
422 ec_bio = container_of(bio_alloc_bioset(ca->disk_sb.bdev,
433 ec_bio->bio.bi_iter.bi_sector = ptr->offset + buf->offset + (offset >> 9);
434 ec_bio->bio.bi_end_io = ec_block_endio;
435 ec_bio->bio.bi_private = cl;
437 bch2_bio_map(&ec_bio->bio, buf->data[idx] + offset, b);
440 percpu_ref_get(&ca->io_ref);
442 submit_bio(&ec_bio->bio);
447 percpu_ref_put(&ca->io_ref);
450 static int get_stripe_key_trans(struct btree_trans *trans, u64 idx,
451 struct ec_stripe_buf *stripe)
453 struct btree_iter iter;
457 k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_stripes,
458 POS(0, idx), BTREE_ITER_SLOTS);
462 if (k.k->type != KEY_TYPE_stripe) {
466 bkey_reassemble(&stripe->key.k_i, k);
468 bch2_trans_iter_exit(trans, &iter);
472 static int get_stripe_key(struct bch_fs *c, u64 idx, struct ec_stripe_buf *stripe)
474 return bch2_trans_run(c, get_stripe_key_trans(&trans, idx, stripe));
477 /* recovery read path: */
478 int bch2_ec_read_extent(struct bch_fs *c, struct bch_read_bio *rbio)
480 struct ec_stripe_buf *buf;
482 struct bch_stripe *v;
486 closure_init_stack(&cl);
488 BUG_ON(!rbio->pick.has_ec);
490 buf = kzalloc(sizeof(*buf), GFP_NOFS);
492 return -BCH_ERR_ENOMEM_ec_read_extent;
494 ret = get_stripe_key(c, rbio->pick.ec.idx, buf);
496 bch_err_ratelimited(c,
497 "error doing reconstruct read: error %i looking up stripe", ret);
504 if (!bch2_ptr_matches_stripe(v, rbio->pick)) {
505 bch_err_ratelimited(c,
506 "error doing reconstruct read: pointer doesn't match stripe");
511 offset = rbio->bio.bi_iter.bi_sector - v->ptrs[rbio->pick.ec.block].offset;
512 if (offset + bio_sectors(&rbio->bio) > le16_to_cpu(v->sectors)) {
513 bch_err_ratelimited(c,
514 "error doing reconstruct read: read is bigger than stripe");
519 ret = ec_stripe_buf_init(buf, offset, bio_sectors(&rbio->bio));
523 for (i = 0; i < v->nr_blocks; i++)
524 ec_block_io(c, buf, REQ_OP_READ, i, &cl);
528 if (ec_nr_failed(buf) > v->nr_redundant) {
529 bch_err_ratelimited(c,
530 "error doing reconstruct read: unable to read enough blocks");
535 ec_validate_checksums(c, buf);
537 ret = ec_do_recov(c, buf);
541 memcpy_to_bio(&rbio->bio, rbio->bio.bi_iter,
542 buf->data[rbio->pick.ec.block] + ((offset - buf->offset) << 9));
544 ec_stripe_buf_exit(buf);
549 /* stripe bucket accounting: */
551 static int __ec_stripe_mem_alloc(struct bch_fs *c, size_t idx, gfp_t gfp)
553 ec_stripes_heap n, *h = &c->ec_stripes_heap;
555 if (idx >= h->size) {
556 if (!init_heap(&n, max(1024UL, roundup_pow_of_two(idx + 1)), gfp))
557 return -BCH_ERR_ENOMEM_ec_stripe_mem_alloc;
559 mutex_lock(&c->ec_stripes_heap_lock);
560 if (n.size > h->size) {
561 memcpy(n.data, h->data, h->used * sizeof(h->data[0]));
565 mutex_unlock(&c->ec_stripes_heap_lock);
570 if (!genradix_ptr_alloc(&c->stripes, idx, gfp))
571 return -BCH_ERR_ENOMEM_ec_stripe_mem_alloc;
573 if (c->gc_pos.phase != GC_PHASE_NOT_RUNNING &&
574 !genradix_ptr_alloc(&c->gc_stripes, idx, gfp))
575 return -BCH_ERR_ENOMEM_ec_stripe_mem_alloc;
580 static int ec_stripe_mem_alloc(struct btree_trans *trans,
581 struct btree_iter *iter)
583 return allocate_dropping_locks_errcode(trans,
584 __ec_stripe_mem_alloc(trans->c, iter->pos.offset, _gfp));
588 * Hash table of open stripes:
589 * Stripes that are being created or modified are kept in a hash table, so that
590 * stripe deletion can skip them.
593 static bool __bch2_stripe_is_open(struct bch_fs *c, u64 idx)
595 unsigned hash = hash_64(idx, ilog2(ARRAY_SIZE(c->ec_stripes_new)));
596 struct ec_stripe_new *s;
598 hlist_for_each_entry(s, &c->ec_stripes_new[hash], hash)
604 static bool bch2_stripe_is_open(struct bch_fs *c, u64 idx)
608 spin_lock(&c->ec_stripes_new_lock);
609 ret = __bch2_stripe_is_open(c, idx);
610 spin_unlock(&c->ec_stripes_new_lock);
615 static bool bch2_try_open_stripe(struct bch_fs *c,
616 struct ec_stripe_new *s,
621 spin_lock(&c->ec_stripes_new_lock);
622 ret = !__bch2_stripe_is_open(c, idx);
624 unsigned hash = hash_64(idx, ilog2(ARRAY_SIZE(c->ec_stripes_new)));
627 hlist_add_head(&s->hash, &c->ec_stripes_new[hash]);
629 spin_unlock(&c->ec_stripes_new_lock);
634 static void bch2_stripe_close(struct bch_fs *c, struct ec_stripe_new *s)
638 spin_lock(&c->ec_stripes_new_lock);
639 hlist_del_init(&s->hash);
640 spin_unlock(&c->ec_stripes_new_lock);
645 /* Heap of all existing stripes, ordered by blocks_nonempty */
647 static u64 stripe_idx_to_delete(struct bch_fs *c)
649 ec_stripes_heap *h = &c->ec_stripes_heap;
651 lockdep_assert_held(&c->ec_stripes_heap_lock);
654 h->data[0].blocks_nonempty == 0 &&
655 !bch2_stripe_is_open(c, h->data[0].idx))
656 return h->data[0].idx;
661 static inline int ec_stripes_heap_cmp(ec_stripes_heap *h,
662 struct ec_stripe_heap_entry l,
663 struct ec_stripe_heap_entry r)
665 return ((l.blocks_nonempty > r.blocks_nonempty) -
666 (l.blocks_nonempty < r.blocks_nonempty));
669 static inline void ec_stripes_heap_set_backpointer(ec_stripes_heap *h,
672 struct bch_fs *c = container_of(h, struct bch_fs, ec_stripes_heap);
674 genradix_ptr(&c->stripes, h->data[i].idx)->heap_idx = i;
677 static void heap_verify_backpointer(struct bch_fs *c, size_t idx)
679 ec_stripes_heap *h = &c->ec_stripes_heap;
680 struct stripe *m = genradix_ptr(&c->stripes, idx);
682 BUG_ON(m->heap_idx >= h->used);
683 BUG_ON(h->data[m->heap_idx].idx != idx);
686 void bch2_stripes_heap_del(struct bch_fs *c,
687 struct stripe *m, size_t idx)
689 mutex_lock(&c->ec_stripes_heap_lock);
690 heap_verify_backpointer(c, idx);
692 heap_del(&c->ec_stripes_heap, m->heap_idx,
694 ec_stripes_heap_set_backpointer);
695 mutex_unlock(&c->ec_stripes_heap_lock);
698 void bch2_stripes_heap_insert(struct bch_fs *c,
699 struct stripe *m, size_t idx)
701 mutex_lock(&c->ec_stripes_heap_lock);
702 BUG_ON(heap_full(&c->ec_stripes_heap));
704 heap_add(&c->ec_stripes_heap, ((struct ec_stripe_heap_entry) {
706 .blocks_nonempty = m->blocks_nonempty,
709 ec_stripes_heap_set_backpointer);
711 heap_verify_backpointer(c, idx);
712 mutex_unlock(&c->ec_stripes_heap_lock);
715 void bch2_stripes_heap_update(struct bch_fs *c,
716 struct stripe *m, size_t idx)
718 ec_stripes_heap *h = &c->ec_stripes_heap;
722 mutex_lock(&c->ec_stripes_heap_lock);
723 heap_verify_backpointer(c, idx);
725 h->data[m->heap_idx].blocks_nonempty = m->blocks_nonempty;
728 heap_sift_up(h, i, ec_stripes_heap_cmp,
729 ec_stripes_heap_set_backpointer);
730 heap_sift_down(h, i, ec_stripes_heap_cmp,
731 ec_stripes_heap_set_backpointer);
733 heap_verify_backpointer(c, idx);
735 do_deletes = stripe_idx_to_delete(c) != 0;
736 mutex_unlock(&c->ec_stripes_heap_lock);
739 bch2_do_stripe_deletes(c);
742 /* stripe deletion */
744 static int ec_stripe_delete(struct btree_trans *trans, u64 idx)
746 struct bch_fs *c = trans->c;
747 struct btree_iter iter;
749 struct bkey_s_c_stripe s;
752 k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_stripes, POS(0, idx),
758 if (k.k->type != KEY_TYPE_stripe) {
759 bch2_fs_inconsistent(c, "attempting to delete nonexistent stripe %llu", idx);
764 s = bkey_s_c_to_stripe(k);
765 for (unsigned i = 0; i < s.v->nr_blocks; i++)
766 if (stripe_blockcount_get(s.v, i)) {
767 struct printbuf buf = PRINTBUF;
769 bch2_bkey_val_to_text(&buf, c, k);
770 bch2_fs_inconsistent(c, "attempting to delete nonempty stripe %s", buf.buf);
776 ret = bch2_btree_delete_at(trans, &iter, 0);
778 bch2_trans_iter_exit(trans, &iter);
782 static void ec_stripe_delete_work(struct work_struct *work)
785 container_of(work, struct bch_fs, ec_stripe_delete_work);
786 struct btree_trans trans;
790 bch2_trans_init(&trans, c, 0, 0);
793 mutex_lock(&c->ec_stripes_heap_lock);
794 idx = stripe_idx_to_delete(c);
795 mutex_unlock(&c->ec_stripes_heap_lock);
800 ret = commit_do(&trans, NULL, NULL, BTREE_INSERT_NOFAIL,
801 ec_stripe_delete(&trans, idx));
808 bch2_trans_exit(&trans);
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_fs *c = trans->c;
879 struct bch_backpointer bp;
880 struct btree_iter iter;
882 const struct bch_extent_ptr *ptr_c;
883 struct bch_extent_ptr *ptr, *ec_ptr = NULL;
884 struct bch_extent_stripe_ptr stripe_ptr;
888 ret = bch2_get_next_backpointer(trans, bucket, gen,
889 bp_pos, &bp, BTREE_ITER_CACHED);
892 if (bpos_eq(*bp_pos, SPOS_MAX))
896 struct printbuf buf = PRINTBUF;
897 struct btree_iter node_iter;
900 b = bch2_backpointer_get_node(trans, &node_iter, *bp_pos, bp);
901 bch2_trans_iter_exit(trans, &node_iter);
906 prt_printf(&buf, "found btree node in erasure coded bucket: b=%px\n", b);
907 bch2_backpointer_to_text(&buf, &bp);
909 bch2_fs_inconsistent(c, "%s", buf.buf);
914 k = bch2_backpointer_get_key(trans, &iter, *bp_pos, bp, BTREE_ITER_INTENT);
920 * extent no longer exists - we could flush the btree
921 * write buffer and retry to verify, but no need:
926 if (extent_has_stripe_ptr(k, s->key.k.p.offset))
929 ptr_c = bkey_matches_stripe(&s->key.v, k, &block);
931 * It doesn't generally make sense to erasure code cached ptrs:
932 * XXX: should we be incrementing a counter?
934 if (!ptr_c || ptr_c->cached)
937 dev = s->key.v.ptrs[block].dev;
939 n = bch2_trans_kmalloc(trans, bkey_bytes(k.k) + sizeof(stripe_ptr));
940 ret = PTR_ERR_OR_ZERO(n);
944 bkey_reassemble(n, k);
946 bch2_bkey_drop_ptrs(bkey_i_to_s(n), ptr, ptr->dev != dev);
947 ec_ptr = bch2_bkey_has_device(bkey_i_to_s(n), dev);
950 stripe_ptr = (struct bch_extent_stripe_ptr) {
951 .type = 1 << BCH_EXTENT_ENTRY_stripe_ptr,
953 .redundancy = s->key.v.nr_redundant,
954 .idx = s->key.k.p.offset,
957 __extent_entry_insert(n,
958 (union bch_extent_entry *) ec_ptr,
959 (union bch_extent_entry *) &stripe_ptr);
961 ret = bch2_trans_update(trans, &iter, n, 0);
963 bch2_trans_iter_exit(trans, &iter);
967 static int ec_stripe_update_bucket(struct btree_trans *trans, struct ec_stripe_buf *s,
970 struct bch_fs *c = trans->c;
971 struct bch_extent_ptr bucket = s->key.v.ptrs[block];
972 struct bpos bucket_pos = PTR_BUCKET_POS(c, &bucket);
973 struct bpos bp_pos = POS_MIN;
977 ret = commit_do(trans, NULL, NULL,
978 BTREE_INSERT_NOCHECK_RW|
980 ec_stripe_update_extent(trans, bucket_pos, bucket.gen,
984 if (bkey_eq(bp_pos, POS_MAX))
987 bp_pos = bpos_nosnap_successor(bp_pos);
993 static int ec_stripe_update_extents(struct bch_fs *c, struct ec_stripe_buf *s)
995 struct btree_trans trans;
996 struct bch_stripe *v = &s->key.v;
997 unsigned i, nr_data = v->nr_blocks - v->nr_redundant;
1000 bch2_trans_init(&trans, c, 0, 0);
1002 ret = bch2_btree_write_buffer_flush(&trans);
1006 for (i = 0; i < nr_data; i++) {
1007 ret = ec_stripe_update_bucket(&trans, s, i);
1012 bch2_trans_exit(&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 = &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(&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 BTREE_INSERT_NOCHECK_RW|
1119 BTREE_INSERT_NOFAIL,
1120 ec_stripe_key_update(&trans, &s->new_stripe.key,
1121 !s->have_existing_stripe));
1123 bch_err(c, "error creating stripe: error creating stripe key");
1127 ret = ec_stripe_update_extents(c, &s->new_stripe);
1129 bch_err(c, "error creating stripe: error updating pointers: %s",
1134 bch2_disk_reservation_put(c, &s->res);
1136 for (i = 0; i < v->nr_blocks; i++)
1138 ob = c->open_buckets + s->blocks[i];
1142 __bch2_open_bucket_put(c, ob);
1144 bch2_open_bucket_put(c, ob);
1148 mutex_lock(&c->ec_stripe_new_lock);
1150 mutex_unlock(&c->ec_stripe_new_lock);
1151 wake_up(&c->ec_stripe_new_wait);
1153 ec_stripe_buf_exit(&s->existing_stripe);
1154 ec_stripe_buf_exit(&s->new_stripe);
1155 closure_debug_destroy(&s->iodone);
1157 ec_stripe_new_put(c, s, STRIPE_REF_stripe);
1160 static struct ec_stripe_new *get_pending_stripe(struct bch_fs *c)
1162 struct ec_stripe_new *s;
1164 mutex_lock(&c->ec_stripe_new_lock);
1165 list_for_each_entry(s, &c->ec_stripe_new_list, list)
1166 if (!atomic_read(&s->ref[STRIPE_REF_io]))
1170 mutex_unlock(&c->ec_stripe_new_lock);
1175 static void ec_stripe_create_work(struct work_struct *work)
1177 struct bch_fs *c = container_of(work,
1178 struct bch_fs, ec_stripe_create_work);
1179 struct ec_stripe_new *s;
1181 while ((s = get_pending_stripe(c)))
1182 ec_stripe_create(s);
1184 bch2_write_ref_put(c, BCH_WRITE_REF_stripe_create);
1187 void bch2_ec_do_stripe_creates(struct bch_fs *c)
1189 bch2_write_ref_get(c, BCH_WRITE_REF_stripe_create);
1191 if (!queue_work(system_long_wq, &c->ec_stripe_create_work))
1192 bch2_write_ref_put(c, BCH_WRITE_REF_stripe_create);
1195 static void ec_stripe_set_pending(struct bch_fs *c, struct ec_stripe_head *h)
1197 struct ec_stripe_new *s = h->s;
1199 BUG_ON(!s->allocated && !s->err);
1204 mutex_lock(&c->ec_stripe_new_lock);
1205 list_add(&s->list, &c->ec_stripe_new_list);
1206 mutex_unlock(&c->ec_stripe_new_lock);
1208 ec_stripe_new_put(c, s, STRIPE_REF_io);
1211 void bch2_ec_bucket_cancel(struct bch_fs *c, struct open_bucket *ob)
1213 struct ec_stripe_new *s = ob->ec;
1218 void *bch2_writepoint_ec_buf(struct bch_fs *c, struct write_point *wp)
1220 struct open_bucket *ob = ec_open_bucket(c, &wp->ptrs);
1227 BUG_ON(!ob->ec->new_stripe.data[ob->ec_idx]);
1229 ca = bch_dev_bkey_exists(c, ob->dev);
1230 offset = ca->mi.bucket_size - ob->sectors_free;
1232 return ob->ec->new_stripe.data[ob->ec_idx] + (offset << 9);
1235 static int unsigned_cmp(const void *_l, const void *_r)
1237 unsigned l = *((const unsigned *) _l);
1238 unsigned r = *((const unsigned *) _r);
1240 return cmp_int(l, r);
1243 /* pick most common bucket size: */
1244 static unsigned pick_blocksize(struct bch_fs *c,
1245 struct bch_devs_mask *devs)
1248 unsigned i, nr = 0, sizes[BCH_SB_MEMBERS_MAX];
1251 } cur = { 0, 0 }, best = { 0, 0 };
1253 for_each_member_device_rcu(ca, c, i, devs)
1254 sizes[nr++] = ca->mi.bucket_size;
1256 sort(sizes, nr, sizeof(unsigned), unsigned_cmp, NULL);
1258 for (i = 0; i < nr; i++) {
1259 if (sizes[i] != cur.size) {
1260 if (cur.nr > best.nr)
1264 cur.size = sizes[i];
1270 if (cur.nr > best.nr)
1276 static bool may_create_new_stripe(struct bch_fs *c)
1281 static void ec_stripe_key_init(struct bch_fs *c,
1282 struct bkey_i_stripe *s,
1285 unsigned stripe_size)
1289 bkey_stripe_init(&s->k_i);
1290 s->v.sectors = cpu_to_le16(stripe_size);
1292 s->v.nr_blocks = nr_data + nr_parity;
1293 s->v.nr_redundant = nr_parity;
1294 s->v.csum_granularity_bits = ilog2(c->opts.encoded_extent_max >> 9);
1295 s->v.csum_type = BCH_CSUM_crc32c;
1298 while ((u64s = stripe_val_u64s(&s->v)) > BKEY_VAL_U64s_MAX) {
1299 BUG_ON(1 << s->v.csum_granularity_bits >=
1300 le16_to_cpu(s->v.sectors) ||
1301 s->v.csum_granularity_bits == U8_MAX);
1302 s->v.csum_granularity_bits++;
1305 set_bkey_val_u64s(&s->k, u64s);
1308 static int ec_new_stripe_alloc(struct bch_fs *c, struct ec_stripe_head *h)
1310 struct ec_stripe_new *s;
1312 lockdep_assert_held(&h->lock);
1314 s = kzalloc(sizeof(*s), GFP_KERNEL);
1316 return -BCH_ERR_ENOMEM_ec_new_stripe_alloc;
1318 mutex_init(&s->lock);
1319 closure_init(&s->iodone, NULL);
1320 atomic_set(&s->ref[STRIPE_REF_stripe], 1);
1321 atomic_set(&s->ref[STRIPE_REF_io], 1);
1324 s->nr_data = min_t(unsigned, h->nr_active_devs,
1325 BCH_BKEY_PTRS_MAX) - h->redundancy;
1326 s->nr_parity = h->redundancy;
1328 ec_stripe_key_init(c, &s->new_stripe.key, s->nr_data,
1329 s->nr_parity, h->blocksize);
1335 static struct ec_stripe_head *
1336 ec_new_stripe_head_alloc(struct bch_fs *c, unsigned target,
1337 unsigned algo, unsigned redundancy,
1338 enum bch_watermark watermark)
1340 struct ec_stripe_head *h;
1344 h = kzalloc(sizeof(*h), GFP_KERNEL);
1348 mutex_init(&h->lock);
1349 BUG_ON(!mutex_trylock(&h->lock));
1353 h->redundancy = redundancy;
1354 h->watermark = watermark;
1357 h->devs = target_rw_devs(c, BCH_DATA_user, target);
1359 for_each_member_device_rcu(ca, c, i, &h->devs)
1360 if (!ca->mi.durability)
1361 __clear_bit(i, h->devs.d);
1363 h->blocksize = pick_blocksize(c, &h->devs);
1365 for_each_member_device_rcu(ca, c, i, &h->devs)
1366 if (ca->mi.bucket_size == h->blocksize)
1367 h->nr_active_devs++;
1370 list_add(&h->list, &c->ec_stripe_head_list);
1374 void bch2_ec_stripe_head_put(struct bch_fs *c, struct ec_stripe_head *h)
1378 bitmap_weight(h->s->blocks_allocated,
1379 h->s->nr_data) == h->s->nr_data)
1380 ec_stripe_set_pending(c, h);
1382 mutex_unlock(&h->lock);
1385 static struct ec_stripe_head *
1386 __bch2_ec_stripe_head_get(struct btree_trans *trans,
1389 unsigned redundancy,
1390 enum bch_watermark watermark)
1392 struct bch_fs *c = trans->c;
1393 struct ec_stripe_head *h;
1399 ret = bch2_trans_mutex_lock(trans, &c->ec_stripe_head_lock);
1401 return ERR_PTR(ret);
1403 if (test_bit(BCH_FS_GOING_RO, &c->flags)) {
1404 h = ERR_PTR(-BCH_ERR_erofs_no_writes);
1408 list_for_each_entry(h, &c->ec_stripe_head_list, list)
1409 if (h->target == target &&
1411 h->redundancy == redundancy &&
1412 h->watermark == watermark) {
1413 ret = bch2_trans_mutex_lock(trans, &h->lock);
1419 h = ec_new_stripe_head_alloc(c, target, algo, redundancy, watermark);
1421 mutex_unlock(&c->ec_stripe_head_lock);
1425 static int new_stripe_alloc_buckets(struct btree_trans *trans, struct ec_stripe_head *h,
1426 enum bch_watermark watermark, struct closure *cl)
1428 struct bch_fs *c = trans->c;
1429 struct bch_devs_mask devs = h->devs;
1430 struct open_bucket *ob;
1431 struct open_buckets buckets;
1432 unsigned i, j, nr_have_parity = 0, nr_have_data = 0;
1433 bool have_cache = true;
1436 BUG_ON(h->s->new_stripe.key.v.nr_blocks != h->s->nr_data + h->s->nr_parity);
1437 BUG_ON(h->s->new_stripe.key.v.nr_redundant != h->s->nr_parity);
1439 for_each_set_bit(i, h->s->blocks_gotten, h->s->new_stripe.key.v.nr_blocks) {
1440 __clear_bit(h->s->new_stripe.key.v.ptrs[i].dev, devs.d);
1441 if (i < h->s->nr_data)
1447 BUG_ON(nr_have_data > h->s->nr_data);
1448 BUG_ON(nr_have_parity > h->s->nr_parity);
1451 if (nr_have_parity < h->s->nr_parity) {
1452 ret = bch2_bucket_alloc_set_trans(trans, &buckets,
1462 open_bucket_for_each(c, &buckets, ob, i) {
1463 j = find_next_zero_bit(h->s->blocks_gotten,
1464 h->s->nr_data + h->s->nr_parity,
1466 BUG_ON(j >= h->s->nr_data + h->s->nr_parity);
1468 h->s->blocks[j] = buckets.v[i];
1469 h->s->new_stripe.key.v.ptrs[j] = bch2_ob_ptr(c, ob);
1470 __set_bit(j, h->s->blocks_gotten);
1478 if (nr_have_data < h->s->nr_data) {
1479 ret = bch2_bucket_alloc_set_trans(trans, &buckets,
1489 open_bucket_for_each(c, &buckets, ob, i) {
1490 j = find_next_zero_bit(h->s->blocks_gotten,
1492 BUG_ON(j >= h->s->nr_data);
1494 h->s->blocks[j] = buckets.v[i];
1495 h->s->new_stripe.key.v.ptrs[j] = bch2_ob_ptr(c, ob);
1496 __set_bit(j, h->s->blocks_gotten);
1506 /* XXX: doesn't obey target: */
1507 static s64 get_existing_stripe(struct bch_fs *c,
1508 struct ec_stripe_head *head)
1510 ec_stripes_heap *h = &c->ec_stripes_heap;
1516 if (may_create_new_stripe(c))
1519 mutex_lock(&c->ec_stripes_heap_lock);
1520 for (heap_idx = 0; heap_idx < h->used; heap_idx++) {
1521 /* No blocks worth reusing, stripe will just be deleted: */
1522 if (!h->data[heap_idx].blocks_nonempty)
1525 stripe_idx = h->data[heap_idx].idx;
1527 m = genradix_ptr(&c->stripes, stripe_idx);
1529 if (m->algorithm == head->algo &&
1530 m->nr_redundant == head->redundancy &&
1531 m->sectors == head->blocksize &&
1532 m->blocks_nonempty < m->nr_blocks - m->nr_redundant &&
1533 bch2_try_open_stripe(c, head->s, stripe_idx)) {
1538 mutex_unlock(&c->ec_stripes_heap_lock);
1542 static int __bch2_ec_stripe_head_reuse(struct btree_trans *trans, struct ec_stripe_head *h)
1544 struct bch_fs *c = trans->c;
1550 * If we can't allocate a new stripe, and there's no stripes with empty
1551 * blocks for us to reuse, that means we have to wait on copygc:
1553 idx = get_existing_stripe(c, h);
1555 return -BCH_ERR_stripe_alloc_blocked;
1557 ret = get_stripe_key_trans(trans, idx, &h->s->existing_stripe);
1559 bch2_stripe_close(c, h->s);
1560 if (!bch2_err_matches(ret, BCH_ERR_transaction_restart))
1561 bch2_fs_fatal_error(c, "error reading stripe key: %s", bch2_err_str(ret));
1565 BUG_ON(h->s->existing_stripe.key.v.nr_redundant != h->s->nr_parity);
1566 h->s->nr_data = h->s->existing_stripe.key.v.nr_blocks -
1567 h->s->existing_stripe.key.v.nr_redundant;
1569 ret = ec_stripe_buf_init(&h->s->existing_stripe, 0, h->blocksize);
1571 bch2_stripe_close(c, h->s);
1575 BUG_ON(h->s->existing_stripe.size != h->blocksize);
1576 BUG_ON(h->s->existing_stripe.size != le16_to_cpu(h->s->existing_stripe.key.v.sectors));
1579 * Free buckets we initially allocated - they might conflict with
1580 * blocks from the stripe we're reusing:
1582 for_each_set_bit(i, h->s->blocks_gotten, h->s->new_stripe.key.v.nr_blocks) {
1583 bch2_open_bucket_put(c, c->open_buckets + h->s->blocks[i]);
1584 h->s->blocks[i] = 0;
1586 memset(h->s->blocks_gotten, 0, sizeof(h->s->blocks_gotten));
1587 memset(h->s->blocks_allocated, 0, sizeof(h->s->blocks_allocated));
1589 for (i = 0; i < h->s->existing_stripe.key.v.nr_blocks; i++) {
1590 if (stripe_blockcount_get(&h->s->existing_stripe.key.v, i)) {
1591 __set_bit(i, h->s->blocks_gotten);
1592 __set_bit(i, h->s->blocks_allocated);
1595 ec_block_io(c, &h->s->existing_stripe, READ, i, &h->s->iodone);
1598 bkey_copy(&h->s->new_stripe.key.k_i, &h->s->existing_stripe.key.k_i);
1599 h->s->have_existing_stripe = true;
1604 static int __bch2_ec_stripe_head_reserve(struct btree_trans *trans, struct ec_stripe_head *h)
1606 struct bch_fs *c = trans->c;
1607 struct btree_iter iter;
1609 struct bpos min_pos = POS(0, 1);
1610 struct bpos start_pos = bpos_max(min_pos, POS(0, c->ec_stripe_hint));
1613 if (!h->s->res.sectors) {
1614 ret = bch2_disk_reservation_get(c, &h->s->res,
1617 BCH_DISK_RESERVATION_NOFAIL);
1622 for_each_btree_key_norestart(trans, iter, BTREE_ID_stripes, start_pos,
1623 BTREE_ITER_SLOTS|BTREE_ITER_INTENT, k, ret) {
1624 if (bkey_gt(k.k->p, POS(0, U32_MAX))) {
1625 if (start_pos.offset) {
1626 start_pos = min_pos;
1627 bch2_btree_iter_set_pos(&iter, start_pos);
1631 ret = -BCH_ERR_ENOSPC_stripe_create;
1635 if (bkey_deleted(k.k) &&
1636 bch2_try_open_stripe(c, h->s, k.k->p.offset))
1640 c->ec_stripe_hint = iter.pos.offset;
1645 ret = ec_stripe_mem_alloc(trans, &iter);
1647 bch2_stripe_close(c, h->s);
1651 h->s->new_stripe.key.k.p = iter.pos;
1653 bch2_trans_iter_exit(trans, &iter);
1656 bch2_disk_reservation_put(c, &h->s->res);
1660 struct ec_stripe_head *bch2_ec_stripe_head_get(struct btree_trans *trans,
1663 unsigned redundancy,
1664 enum bch_watermark watermark,
1667 struct bch_fs *c = trans->c;
1668 struct ec_stripe_head *h;
1669 bool waiting = false;
1672 h = __bch2_ec_stripe_head_get(trans, target, algo, redundancy, watermark);
1674 bch_err(c, "no stripe head");
1675 if (IS_ERR_OR_NULL(h))
1679 ret = ec_new_stripe_alloc(c, h);
1681 bch_err(c, "failed to allocate new stripe");
1686 if (h->s->allocated)
1689 if (h->s->have_existing_stripe)
1690 goto alloc_existing;
1692 /* First, try to allocate a full stripe: */
1693 ret = new_stripe_alloc_buckets(trans, h, BCH_WATERMARK_stripe, NULL) ?:
1694 __bch2_ec_stripe_head_reserve(trans, h);
1697 if (bch2_err_matches(ret, BCH_ERR_transaction_restart) ||
1698 bch2_err_matches(ret, ENOMEM))
1702 * Not enough buckets available for a full stripe: we must reuse an
1706 ret = __bch2_ec_stripe_head_reuse(trans, h);
1709 if (waiting || !cl || ret != -BCH_ERR_stripe_alloc_blocked)
1712 if (watermark == BCH_WATERMARK_copygc) {
1713 ret = new_stripe_alloc_buckets(trans, h, watermark, NULL) ?:
1714 __bch2_ec_stripe_head_reserve(trans, h);
1720 /* XXX freelist_wait? */
1721 closure_wait(&c->freelist_wait, cl);
1726 closure_wake_up(&c->freelist_wait);
1729 * Retry allocating buckets, with the watermark for this
1732 ret = new_stripe_alloc_buckets(trans, h, watermark, cl);
1737 ret = ec_stripe_buf_init(&h->s->new_stripe, 0, h->blocksize);
1741 h->s->allocated = true;
1744 BUG_ON(!h->s->new_stripe.data[0]);
1745 BUG_ON(trans->restarted);
1748 bch2_ec_stripe_head_put(c, h);
1749 return ERR_PTR(ret);
1752 static void __bch2_ec_stop(struct bch_fs *c, struct bch_dev *ca)
1754 struct ec_stripe_head *h;
1755 struct open_bucket *ob;
1758 mutex_lock(&c->ec_stripe_head_lock);
1759 list_for_each_entry(h, &c->ec_stripe_head_list, list) {
1760 mutex_lock(&h->lock);
1767 for (i = 0; i < h->s->new_stripe.key.v.nr_blocks; i++) {
1768 if (!h->s->blocks[i])
1771 ob = c->open_buckets + h->s->blocks[i];
1772 if (ob->dev == ca->dev_idx)
1777 h->s->err = -BCH_ERR_erofs_no_writes;
1778 ec_stripe_set_pending(c, h);
1780 mutex_unlock(&h->lock);
1782 mutex_unlock(&c->ec_stripe_head_lock);
1785 void bch2_ec_stop_dev(struct bch_fs *c, struct bch_dev *ca)
1787 __bch2_ec_stop(c, ca);
1790 void bch2_fs_ec_stop(struct bch_fs *c)
1792 __bch2_ec_stop(c, NULL);
1795 static bool bch2_fs_ec_flush_done(struct bch_fs *c)
1799 mutex_lock(&c->ec_stripe_new_lock);
1800 ret = list_empty(&c->ec_stripe_new_list);
1801 mutex_unlock(&c->ec_stripe_new_lock);
1806 void bch2_fs_ec_flush(struct bch_fs *c)
1808 wait_event(c->ec_stripe_new_wait, bch2_fs_ec_flush_done(c));
1811 int bch2_stripes_read(struct bch_fs *c)
1813 struct btree_trans trans;
1814 struct btree_iter iter;
1816 const struct bch_stripe *s;
1821 bch2_trans_init(&trans, c, 0, 0);
1823 for_each_btree_key(&trans, iter, BTREE_ID_stripes, POS_MIN,
1824 BTREE_ITER_PREFETCH, k, ret) {
1825 if (k.k->type != KEY_TYPE_stripe)
1828 ret = __ec_stripe_mem_alloc(c, k.k->p.offset, GFP_KERNEL);
1832 s = bkey_s_c_to_stripe(k).v;
1834 m = genradix_ptr(&c->stripes, k.k->p.offset);
1835 m->sectors = le16_to_cpu(s->sectors);
1836 m->algorithm = s->algorithm;
1837 m->nr_blocks = s->nr_blocks;
1838 m->nr_redundant = s->nr_redundant;
1839 m->blocks_nonempty = 0;
1841 for (i = 0; i < s->nr_blocks; i++)
1842 m->blocks_nonempty += !!stripe_blockcount_get(s, i);
1844 bch2_stripes_heap_insert(c, m, k.k->p.offset);
1846 bch2_trans_iter_exit(&trans, &iter);
1848 bch2_trans_exit(&trans);
1856 void bch2_stripes_heap_to_text(struct printbuf *out, struct bch_fs *c)
1858 ec_stripes_heap *h = &c->ec_stripes_heap;
1862 mutex_lock(&c->ec_stripes_heap_lock);
1863 for (i = 0; i < min_t(size_t, h->used, 50); i++) {
1864 m = genradix_ptr(&c->stripes, h->data[i].idx);
1866 prt_printf(out, "%zu %u/%u+%u", h->data[i].idx,
1867 h->data[i].blocks_nonempty,
1868 m->nr_blocks - m->nr_redundant,
1870 if (bch2_stripe_is_open(c, h->data[i].idx))
1871 prt_str(out, " open");
1874 mutex_unlock(&c->ec_stripes_heap_lock);
1877 void bch2_new_stripes_to_text(struct printbuf *out, struct bch_fs *c)
1879 struct ec_stripe_head *h;
1880 struct ec_stripe_new *s;
1882 mutex_lock(&c->ec_stripe_head_lock);
1883 list_for_each_entry(h, &c->ec_stripe_head_list, list) {
1884 prt_printf(out, "target %u algo %u redundancy %u %s:\n",
1885 h->target, h->algo, h->redundancy,
1886 bch2_watermarks[h->watermark]);
1889 prt_printf(out, "\tidx %llu blocks %u+%u allocated %u\n",
1890 h->s->idx, h->s->nr_data, h->s->nr_parity,
1891 bitmap_weight(h->s->blocks_allocated,
1894 mutex_unlock(&c->ec_stripe_head_lock);
1896 prt_printf(out, "in flight:\n");
1898 mutex_lock(&c->ec_stripe_new_lock);
1899 list_for_each_entry(s, &c->ec_stripe_new_list, list) {
1900 prt_printf(out, "\tidx %llu blocks %u+%u ref %u %u %s\n",
1901 s->idx, s->nr_data, s->nr_parity,
1902 atomic_read(&s->ref[STRIPE_REF_io]),
1903 atomic_read(&s->ref[STRIPE_REF_stripe]),
1904 bch2_watermarks[s->h->watermark]);
1906 mutex_unlock(&c->ec_stripe_new_lock);
1909 void bch2_fs_ec_exit(struct bch_fs *c)
1911 struct ec_stripe_head *h;
1915 mutex_lock(&c->ec_stripe_head_lock);
1916 h = list_first_entry_or_null(&c->ec_stripe_head_list,
1917 struct ec_stripe_head, list);
1920 mutex_unlock(&c->ec_stripe_head_lock);
1925 for (i = 0; i < h->s->new_stripe.key.v.nr_blocks; i++)
1926 BUG_ON(h->s->blocks[i]);
1933 BUG_ON(!list_empty(&c->ec_stripe_new_list));
1935 free_heap(&c->ec_stripes_heap);
1936 genradix_free(&c->stripes);
1937 bioset_exit(&c->ec_bioset);
1940 void bch2_fs_ec_init_early(struct bch_fs *c)
1942 spin_lock_init(&c->ec_stripes_new_lock);
1943 mutex_init(&c->ec_stripes_heap_lock);
1945 INIT_LIST_HEAD(&c->ec_stripe_head_list);
1946 mutex_init(&c->ec_stripe_head_lock);
1948 INIT_LIST_HEAD(&c->ec_stripe_new_list);
1949 mutex_init(&c->ec_stripe_new_lock);
1950 init_waitqueue_head(&c->ec_stripe_new_wait);
1952 INIT_WORK(&c->ec_stripe_create_work, ec_stripe_create_work);
1953 INIT_WORK(&c->ec_stripe_delete_work, ec_stripe_delete_work);
1956 int bch2_fs_ec_init(struct bch_fs *c)
1958 return bioset_init(&c->ec_bioset, 1, offsetof(struct ec_bio, bio),