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_bytes(k.k) < sizeof(*s)) {
123 prt_printf(err, "incorrect value size (%zu < %zu)",
124 bkey_val_bytes(k.k), sizeof(*s));
125 return -BCH_ERR_invalid_bkey;
128 if (bkey_val_u64s(k.k) < stripe_val_u64s(s)) {
129 prt_printf(err, "incorrect value size (%zu < %u)",
130 bkey_val_u64s(k.k), stripe_val_u64s(s));
131 return -BCH_ERR_invalid_bkey;
134 return bch2_bkey_ptrs_invalid(c, k, flags, err);
137 void bch2_stripe_to_text(struct printbuf *out, struct bch_fs *c,
140 const struct bch_stripe *s = bkey_s_c_to_stripe(k).v;
141 unsigned i, nr_data = s->nr_blocks - s->nr_redundant;
143 prt_printf(out, "algo %u sectors %u blocks %u:%u csum %u gran %u",
145 le16_to_cpu(s->sectors),
149 1U << s->csum_granularity_bits);
151 for (i = 0; i < s->nr_blocks; i++) {
152 const struct bch_extent_ptr *ptr = s->ptrs + i;
153 struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
155 u64 b = sector_to_bucket_and_offset(ca, ptr->offset, &offset);
157 prt_printf(out, " %u:%llu:%u", ptr->dev, b, offset);
159 prt_printf(out, "#%u", stripe_blockcount_get(s, i));
160 if (ptr_stale(ca, ptr))
161 prt_printf(out, " stale");
165 /* returns blocknr in stripe that we matched: */
166 static const struct bch_extent_ptr *bkey_matches_stripe(struct bch_stripe *s,
167 struct bkey_s_c k, unsigned *block)
169 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
170 const struct bch_extent_ptr *ptr;
171 unsigned i, nr_data = s->nr_blocks - s->nr_redundant;
173 bkey_for_each_ptr(ptrs, ptr)
174 for (i = 0; i < nr_data; i++)
175 if (__bch2_ptr_matches_stripe(&s->ptrs[i], ptr,
176 le16_to_cpu(s->sectors))) {
184 static bool extent_has_stripe_ptr(struct bkey_s_c k, u64 idx)
187 case KEY_TYPE_extent: {
188 struct bkey_s_c_extent e = bkey_s_c_to_extent(k);
189 const union bch_extent_entry *entry;
191 extent_for_each_entry(e, entry)
192 if (extent_entry_type(entry) ==
193 BCH_EXTENT_ENTRY_stripe_ptr &&
194 entry->stripe_ptr.idx == idx)
206 static void ec_stripe_buf_exit(struct ec_stripe_buf *buf)
210 for (i = 0; i < buf->key.v.nr_blocks; i++) {
211 kvpfree(buf->data[i], buf->size << 9);
216 static int ec_stripe_buf_init(struct ec_stripe_buf *buf,
217 unsigned offset, unsigned size)
219 struct bch_stripe *v = &buf->key.v;
220 unsigned csum_granularity = 1U << v->csum_granularity_bits;
221 unsigned end = offset + size;
224 BUG_ON(end > le16_to_cpu(v->sectors));
226 offset = round_down(offset, csum_granularity);
227 end = min_t(unsigned, le16_to_cpu(v->sectors),
228 round_up(end, csum_granularity));
230 buf->offset = offset;
231 buf->size = end - offset;
233 memset(buf->valid, 0xFF, sizeof(buf->valid));
235 for (i = 0; i < buf->key.v.nr_blocks; i++) {
236 buf->data[i] = kvpmalloc(buf->size << 9, GFP_KERNEL);
243 ec_stripe_buf_exit(buf);
249 static struct bch_csum ec_block_checksum(struct ec_stripe_buf *buf,
250 unsigned block, unsigned offset)
252 struct bch_stripe *v = &buf->key.v;
253 unsigned csum_granularity = 1 << v->csum_granularity_bits;
254 unsigned end = buf->offset + buf->size;
255 unsigned len = min(csum_granularity, end - offset);
257 BUG_ON(offset >= end);
258 BUG_ON(offset < buf->offset);
259 BUG_ON(offset & (csum_granularity - 1));
260 BUG_ON(offset + len != le16_to_cpu(v->sectors) &&
261 (len & (csum_granularity - 1)));
263 return bch2_checksum(NULL, v->csum_type,
265 buf->data[block] + ((offset - buf->offset) << 9),
269 static void ec_generate_checksums(struct ec_stripe_buf *buf)
271 struct bch_stripe *v = &buf->key.v;
272 unsigned i, j, csums_per_device = stripe_csums_per_device(v);
278 BUG_ON(buf->size != le16_to_cpu(v->sectors));
280 for (i = 0; i < v->nr_blocks; i++)
281 for (j = 0; j < csums_per_device; j++)
282 stripe_csum_set(v, i, j,
283 ec_block_checksum(buf, i, j << v->csum_granularity_bits));
286 static void ec_validate_checksums(struct bch_fs *c, struct ec_stripe_buf *buf)
288 struct bch_stripe *v = &buf->key.v;
289 unsigned csum_granularity = 1 << v->csum_granularity_bits;
295 for (i = 0; i < v->nr_blocks; i++) {
296 unsigned offset = buf->offset;
297 unsigned end = buf->offset + buf->size;
299 if (!test_bit(i, buf->valid))
302 while (offset < end) {
303 unsigned j = offset >> v->csum_granularity_bits;
304 unsigned len = min(csum_granularity, end - offset);
305 struct bch_csum want = stripe_csum_get(v, i, j);
306 struct bch_csum got = ec_block_checksum(buf, i, offset);
308 if (bch2_crc_cmp(want, got)) {
309 struct printbuf buf2 = PRINTBUF;
311 bch2_bkey_val_to_text(&buf2, c, bkey_i_to_s_c(&buf->key.k_i));
313 bch_err_ratelimited(c,
314 "stripe checksum error for %ps at %u:%u: csum type %u, expected %llx got %llx\n%s",
315 (void *) _RET_IP_, i, j, v->csum_type,
316 want.lo, got.lo, buf2.buf);
317 printbuf_exit(&buf2);
318 clear_bit(i, buf->valid);
327 /* Erasure coding: */
329 static void ec_generate_ec(struct ec_stripe_buf *buf)
331 struct bch_stripe *v = &buf->key.v;
332 unsigned nr_data = v->nr_blocks - v->nr_redundant;
333 unsigned bytes = le16_to_cpu(v->sectors) << 9;
335 raid_gen(nr_data, v->nr_redundant, bytes, buf->data);
338 static unsigned ec_nr_failed(struct ec_stripe_buf *buf)
340 return buf->key.v.nr_blocks -
341 bitmap_weight(buf->valid, buf->key.v.nr_blocks);
344 static int ec_do_recov(struct bch_fs *c, struct ec_stripe_buf *buf)
346 struct bch_stripe *v = &buf->key.v;
347 unsigned i, failed[BCH_BKEY_PTRS_MAX], nr_failed = 0;
348 unsigned nr_data = v->nr_blocks - v->nr_redundant;
349 unsigned bytes = buf->size << 9;
351 if (ec_nr_failed(buf) > v->nr_redundant) {
352 bch_err_ratelimited(c,
353 "error doing reconstruct read: unable to read enough blocks");
357 for (i = 0; i < nr_data; i++)
358 if (!test_bit(i, buf->valid))
359 failed[nr_failed++] = i;
361 raid_rec(nr_failed, failed, nr_data, v->nr_redundant, bytes, buf->data);
367 static void ec_block_endio(struct bio *bio)
369 struct ec_bio *ec_bio = container_of(bio, struct ec_bio, bio);
370 struct bch_stripe *v = &ec_bio->buf->key.v;
371 struct bch_extent_ptr *ptr = &v->ptrs[ec_bio->idx];
372 struct bch_dev *ca = ec_bio->ca;
373 struct closure *cl = bio->bi_private;
375 if (bch2_dev_io_err_on(bio->bi_status, ca, "erasure coding %s error: %s",
376 bio_data_dir(bio) ? "write" : "read",
377 bch2_blk_status_to_str(bio->bi_status)))
378 clear_bit(ec_bio->idx, ec_bio->buf->valid);
380 if (ptr_stale(ca, ptr)) {
381 bch_err_ratelimited(ca->fs,
382 "error %s stripe: stale pointer after io",
383 bio_data_dir(bio) == READ ? "reading from" : "writing to");
384 clear_bit(ec_bio->idx, ec_bio->buf->valid);
387 bio_put(&ec_bio->bio);
388 percpu_ref_put(&ca->io_ref);
392 static void ec_block_io(struct bch_fs *c, struct ec_stripe_buf *buf,
393 unsigned rw, unsigned idx, struct closure *cl)
395 struct bch_stripe *v = &buf->key.v;
396 unsigned offset = 0, bytes = buf->size << 9;
397 struct bch_extent_ptr *ptr = &v->ptrs[idx];
398 struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
399 enum bch_data_type data_type = idx < buf->key.v.nr_blocks - buf->key.v.nr_redundant
403 if (ptr_stale(ca, ptr)) {
404 bch_err_ratelimited(c,
405 "error %s stripe: stale pointer",
406 rw == READ ? "reading from" : "writing to");
407 clear_bit(idx, buf->valid);
411 if (!bch2_dev_get_ioref(ca, rw)) {
412 clear_bit(idx, buf->valid);
416 this_cpu_add(ca->io_done->sectors[rw][data_type], buf->size);
418 while (offset < bytes) {
419 unsigned nr_iovecs = min_t(size_t, BIO_MAX_VECS,
420 DIV_ROUND_UP(bytes, PAGE_SIZE));
421 unsigned b = min_t(size_t, bytes - offset,
422 nr_iovecs << PAGE_SHIFT);
423 struct ec_bio *ec_bio;
425 ec_bio = container_of(bio_alloc_bioset(ca->disk_sb.bdev,
436 ec_bio->bio.bi_iter.bi_sector = ptr->offset + buf->offset + (offset >> 9);
437 ec_bio->bio.bi_end_io = ec_block_endio;
438 ec_bio->bio.bi_private = cl;
440 bch2_bio_map(&ec_bio->bio, buf->data[idx] + offset, b);
443 percpu_ref_get(&ca->io_ref);
445 submit_bio(&ec_bio->bio);
450 percpu_ref_put(&ca->io_ref);
453 static int get_stripe_key_trans(struct btree_trans *trans, u64 idx,
454 struct ec_stripe_buf *stripe)
456 struct btree_iter iter;
460 bch2_trans_iter_init(trans, &iter, BTREE_ID_stripes,
461 POS(0, idx), BTREE_ITER_SLOTS);
462 k = bch2_btree_iter_peek_slot(&iter);
466 if (k.k->type != KEY_TYPE_stripe) {
470 bkey_reassemble(&stripe->key.k_i, k);
472 bch2_trans_iter_exit(trans, &iter);
476 static int get_stripe_key(struct bch_fs *c, u64 idx, struct ec_stripe_buf *stripe)
478 return bch2_trans_run(c, get_stripe_key_trans(&trans, idx, stripe));
481 /* recovery read path: */
482 int bch2_ec_read_extent(struct bch_fs *c, struct bch_read_bio *rbio)
484 struct ec_stripe_buf *buf;
486 struct bch_stripe *v;
490 closure_init_stack(&cl);
492 BUG_ON(!rbio->pick.has_ec);
494 buf = kzalloc(sizeof(*buf), GFP_NOIO);
498 ret = get_stripe_key(c, rbio->pick.ec.idx, buf);
500 bch_err_ratelimited(c,
501 "error doing reconstruct read: error %i looking up stripe", ret);
508 if (!bch2_ptr_matches_stripe(v, rbio->pick)) {
509 bch_err_ratelimited(c,
510 "error doing reconstruct read: pointer doesn't match stripe");
515 offset = rbio->bio.bi_iter.bi_sector - v->ptrs[rbio->pick.ec.block].offset;
516 if (offset + bio_sectors(&rbio->bio) > le16_to_cpu(v->sectors)) {
517 bch_err_ratelimited(c,
518 "error doing reconstruct read: read is bigger than stripe");
523 ret = ec_stripe_buf_init(buf, offset, bio_sectors(&rbio->bio));
527 for (i = 0; i < v->nr_blocks; i++)
528 ec_block_io(c, buf, REQ_OP_READ, i, &cl);
532 if (ec_nr_failed(buf) > v->nr_redundant) {
533 bch_err_ratelimited(c,
534 "error doing reconstruct read: unable to read enough blocks");
539 ec_validate_checksums(c, buf);
541 ret = ec_do_recov(c, buf);
545 memcpy_to_bio(&rbio->bio, rbio->bio.bi_iter,
546 buf->data[rbio->pick.ec.block] + ((offset - buf->offset) << 9));
548 ec_stripe_buf_exit(buf);
553 /* stripe bucket accounting: */
555 static int __ec_stripe_mem_alloc(struct bch_fs *c, size_t idx, gfp_t gfp)
557 ec_stripes_heap n, *h = &c->ec_stripes_heap;
559 if (idx >= h->size) {
560 if (!init_heap(&n, max(1024UL, roundup_pow_of_two(idx + 1)), gfp))
563 mutex_lock(&c->ec_stripes_heap_lock);
564 if (n.size > h->size) {
565 memcpy(n.data, h->data, h->used * sizeof(h->data[0]));
569 mutex_unlock(&c->ec_stripes_heap_lock);
574 if (!genradix_ptr_alloc(&c->stripes, idx, gfp))
577 if (c->gc_pos.phase != GC_PHASE_NOT_RUNNING &&
578 !genradix_ptr_alloc(&c->gc_stripes, idx, gfp))
584 static int ec_stripe_mem_alloc(struct btree_trans *trans,
585 struct btree_iter *iter)
587 size_t idx = iter->pos.offset;
589 if (!__ec_stripe_mem_alloc(trans->c, idx, GFP_NOWAIT|__GFP_NOWARN))
592 bch2_trans_unlock(trans);
594 return __ec_stripe_mem_alloc(trans->c, idx, GFP_KERNEL) ?:
595 bch2_trans_relock(trans);
599 * Hash table of open stripes:
600 * Stripes that are being created or modified are kept in a hash table, so that
601 * stripe deletion can skip them.
604 static bool __bch2_stripe_is_open(struct bch_fs *c, u64 idx)
606 unsigned hash = hash_64(idx, ilog2(ARRAY_SIZE(c->ec_stripes_new)));
607 struct ec_stripe_new *s;
609 hlist_for_each_entry(s, &c->ec_stripes_new[hash], hash)
615 static bool bch2_stripe_is_open(struct bch_fs *c, u64 idx)
619 spin_lock(&c->ec_stripes_new_lock);
620 ret = __bch2_stripe_is_open(c, idx);
621 spin_unlock(&c->ec_stripes_new_lock);
626 static bool bch2_try_open_stripe(struct bch_fs *c,
627 struct ec_stripe_new *s,
632 spin_lock(&c->ec_stripes_new_lock);
633 ret = !__bch2_stripe_is_open(c, idx);
635 unsigned hash = hash_64(idx, ilog2(ARRAY_SIZE(c->ec_stripes_new)));
638 hlist_add_head(&s->hash, &c->ec_stripes_new[hash]);
640 spin_unlock(&c->ec_stripes_new_lock);
645 static void bch2_stripe_close(struct bch_fs *c, struct ec_stripe_new *s)
649 spin_lock(&c->ec_stripes_new_lock);
651 spin_unlock(&c->ec_stripes_new_lock);
656 /* Heap of all existing stripes, ordered by blocks_nonempty */
658 static u64 stripe_idx_to_delete(struct bch_fs *c)
660 ec_stripes_heap *h = &c->ec_stripes_heap;
663 lockdep_assert_held(&c->ec_stripes_heap_lock);
665 for (heap_idx = 0; heap_idx < h->used; heap_idx++)
666 if (h->data[heap_idx].blocks_nonempty == 0 &&
667 !bch2_stripe_is_open(c, h->data[heap_idx].idx))
668 return h->data[heap_idx].idx;
673 static inline int ec_stripes_heap_cmp(ec_stripes_heap *h,
674 struct ec_stripe_heap_entry l,
675 struct ec_stripe_heap_entry r)
677 return ((l.blocks_nonempty > r.blocks_nonempty) -
678 (l.blocks_nonempty < r.blocks_nonempty));
681 static inline void ec_stripes_heap_set_backpointer(ec_stripes_heap *h,
684 struct bch_fs *c = container_of(h, struct bch_fs, ec_stripes_heap);
686 genradix_ptr(&c->stripes, h->data[i].idx)->heap_idx = i;
689 static void heap_verify_backpointer(struct bch_fs *c, size_t idx)
691 ec_stripes_heap *h = &c->ec_stripes_heap;
692 struct stripe *m = genradix_ptr(&c->stripes, idx);
694 BUG_ON(m->heap_idx >= h->used);
695 BUG_ON(h->data[m->heap_idx].idx != idx);
698 void bch2_stripes_heap_del(struct bch_fs *c,
699 struct stripe *m, size_t idx)
701 mutex_lock(&c->ec_stripes_heap_lock);
702 heap_verify_backpointer(c, idx);
704 heap_del(&c->ec_stripes_heap, m->heap_idx,
706 ec_stripes_heap_set_backpointer);
707 mutex_unlock(&c->ec_stripes_heap_lock);
710 void bch2_stripes_heap_insert(struct bch_fs *c,
711 struct stripe *m, size_t idx)
713 mutex_lock(&c->ec_stripes_heap_lock);
714 BUG_ON(heap_full(&c->ec_stripes_heap));
716 heap_add(&c->ec_stripes_heap, ((struct ec_stripe_heap_entry) {
718 .blocks_nonempty = m->blocks_nonempty,
721 ec_stripes_heap_set_backpointer);
723 heap_verify_backpointer(c, idx);
724 mutex_unlock(&c->ec_stripes_heap_lock);
727 void bch2_stripes_heap_update(struct bch_fs *c,
728 struct stripe *m, size_t idx)
730 ec_stripes_heap *h = &c->ec_stripes_heap;
734 mutex_lock(&c->ec_stripes_heap_lock);
735 heap_verify_backpointer(c, idx);
737 h->data[m->heap_idx].blocks_nonempty = m->blocks_nonempty;
740 heap_sift_up(h, i, ec_stripes_heap_cmp,
741 ec_stripes_heap_set_backpointer);
742 heap_sift_down(h, i, ec_stripes_heap_cmp,
743 ec_stripes_heap_set_backpointer);
745 heap_verify_backpointer(c, idx);
747 do_deletes = stripe_idx_to_delete(c) != 0;
748 mutex_unlock(&c->ec_stripes_heap_lock);
751 bch2_do_stripe_deletes(c);
754 /* stripe deletion */
756 static int ec_stripe_delete(struct btree_trans *trans, u64 idx)
758 struct bch_fs *c = trans->c;
759 struct btree_iter iter;
761 struct bkey_s_c_stripe s;
764 bch2_trans_iter_init(trans, &iter, BTREE_ID_stripes, POS(0, idx),
766 k = bch2_btree_iter_peek_slot(&iter);
771 if (k.k->type != KEY_TYPE_stripe) {
772 bch2_fs_inconsistent(c, "attempting to delete nonexistent stripe %llu", idx);
777 s = bkey_s_c_to_stripe(k);
778 for (unsigned i = 0; i < s.v->nr_blocks; i++)
779 if (stripe_blockcount_get(s.v, i)) {
780 struct printbuf buf = PRINTBUF;
782 bch2_bkey_val_to_text(&buf, c, k);
783 bch2_fs_inconsistent(c, "attempting to delete nonempty stripe %s", buf.buf);
789 ret = bch2_btree_delete_at(trans, &iter, 0);
791 bch2_trans_iter_exit(trans, &iter);
795 static void ec_stripe_delete_work(struct work_struct *work)
798 container_of(work, struct bch_fs, ec_stripe_delete_work);
799 struct btree_trans trans;
803 bch2_trans_init(&trans, c, 0, 0);
806 mutex_lock(&c->ec_stripes_heap_lock);
807 idx = stripe_idx_to_delete(c);
808 mutex_unlock(&c->ec_stripes_heap_lock);
813 ret = commit_do(&trans, NULL, NULL, BTREE_INSERT_NOFAIL,
814 ec_stripe_delete(&trans, idx));
816 bch_err(c, "%s: err %s", __func__, bch2_err_str(ret));
821 bch2_trans_exit(&trans);
823 bch2_write_ref_put(c, BCH_WRITE_REF_stripe_delete);
826 void bch2_do_stripe_deletes(struct bch_fs *c)
828 if (bch2_write_ref_tryget(c, BCH_WRITE_REF_stripe_delete) &&
829 !schedule_work(&c->ec_stripe_delete_work))
830 bch2_write_ref_put(c, BCH_WRITE_REF_stripe_delete);
833 /* stripe creation: */
835 static int ec_stripe_key_update(struct btree_trans *trans,
836 struct bkey_i_stripe *new,
839 struct bch_fs *c = trans->c;
840 struct btree_iter iter;
844 bch2_trans_iter_init(trans, &iter, BTREE_ID_stripes,
845 new->k.p, BTREE_ITER_INTENT);
846 k = bch2_btree_iter_peek_slot(&iter);
851 if (k.k->type != (create ? KEY_TYPE_deleted : KEY_TYPE_stripe)) {
852 bch2_fs_inconsistent(c, "error %s stripe: got existing key type %s",
853 create ? "creating" : "updating",
854 bch2_bkey_types[k.k->type]);
859 if (k.k->type == KEY_TYPE_stripe) {
860 const struct bch_stripe *old = bkey_s_c_to_stripe(k).v;
863 if (old->nr_blocks != new->v.nr_blocks) {
864 bch_err(c, "error updating stripe: nr_blocks does not match");
869 for (i = 0; i < new->v.nr_blocks; i++)
870 stripe_blockcount_set(&new->v, i, stripe_blockcount_get(old, i));
873 ret = bch2_trans_update(trans, &iter, &new->k_i, 0);
875 bch2_trans_iter_exit(trans, &iter);
879 static int ec_stripe_update_extent(struct btree_trans *trans,
880 struct bpos bucket, u8 gen,
881 struct ec_stripe_buf *s,
884 struct bch_fs *c = trans->c;
885 struct bch_backpointer bp;
886 struct btree_iter iter;
888 const struct bch_extent_ptr *ptr_c;
889 struct bch_extent_ptr *ptr, *ec_ptr = NULL;
890 struct bch_extent_stripe_ptr stripe_ptr;
894 ret = bch2_get_next_backpointer(trans, bucket, gen,
895 bp_offset, &bp, BTREE_ITER_CACHED);
898 if (*bp_offset == U64_MAX)
901 if (bch2_fs_inconsistent_on(bp.level, c, "found btree node in erasure coded bucket!?"))
904 k = bch2_backpointer_get_key(trans, &iter, bucket, *bp_offset, bp);
910 * extent no longer exists - we could flush the btree
911 * write buffer and retry to verify, but no need:
916 if (extent_has_stripe_ptr(k, s->key.k.p.offset))
919 ptr_c = bkey_matches_stripe(&s->key.v, k, &block);
921 * It doesn't generally make sense to erasure code cached ptrs:
922 * XXX: should we be incrementing a counter?
924 if (!ptr_c || ptr_c->cached)
927 dev = s->key.v.ptrs[block].dev;
929 n = bch2_trans_kmalloc(trans, bkey_bytes(k.k) + sizeof(stripe_ptr));
930 ret = PTR_ERR_OR_ZERO(n);
934 bkey_reassemble(n, k);
936 bch2_bkey_drop_ptrs(bkey_i_to_s(n), ptr, ptr->dev != dev);
937 ec_ptr = (void *) bch2_bkey_has_device(bkey_i_to_s_c(n), dev);
940 stripe_ptr = (struct bch_extent_stripe_ptr) {
941 .type = 1 << BCH_EXTENT_ENTRY_stripe_ptr,
943 .redundancy = s->key.v.nr_redundant,
944 .idx = s->key.k.p.offset,
947 __extent_entry_insert(n,
948 (union bch_extent_entry *) ec_ptr,
949 (union bch_extent_entry *) &stripe_ptr);
951 ret = bch2_trans_update(trans, &iter, n, 0);
953 bch2_trans_iter_exit(trans, &iter);
957 static int ec_stripe_update_bucket(struct btree_trans *trans, struct ec_stripe_buf *s,
960 struct bch_fs *c = trans->c;
961 struct bch_extent_ptr bucket = s->key.v.ptrs[block];
962 struct bpos bucket_pos = PTR_BUCKET_POS(c, &bucket);
967 ret = commit_do(trans, NULL, NULL,
969 ec_stripe_update_extent(trans, bucket_pos, bucket.gen,
973 if (bp_offset == U64_MAX)
982 static int ec_stripe_update_extents(struct bch_fs *c, struct ec_stripe_buf *s)
984 struct btree_trans trans;
985 struct bch_stripe *v = &s->key.v;
986 unsigned i, nr_data = v->nr_blocks - v->nr_redundant;
989 bch2_trans_init(&trans, c, 0, 0);
991 ret = bch2_btree_write_buffer_flush(&trans);
995 for (i = 0; i < nr_data; i++) {
996 ret = ec_stripe_update_bucket(&trans, s, i);
1001 bch2_trans_exit(&trans);
1006 static void zero_out_rest_of_ec_bucket(struct bch_fs *c,
1007 struct ec_stripe_new *s,
1009 struct open_bucket *ob)
1011 struct bch_dev *ca = bch_dev_bkey_exists(c, ob->dev);
1012 unsigned offset = ca->mi.bucket_size - ob->sectors_free;
1015 if (!bch2_dev_get_ioref(ca, WRITE)) {
1020 memset(s->new_stripe.data[block] + (offset << 9),
1022 ob->sectors_free << 9);
1024 ret = blkdev_issue_zeroout(ca->disk_sb.bdev,
1025 ob->bucket * ca->mi.bucket_size + offset,
1029 percpu_ref_put(&ca->io_ref);
1036 * data buckets of new stripe all written: create the stripe
1038 static void ec_stripe_create(struct ec_stripe_new *s)
1040 struct bch_fs *c = s->c;
1041 struct open_bucket *ob;
1042 struct bch_stripe *v = &s->new_stripe.key.v;
1043 unsigned i, nr_data = v->nr_blocks - v->nr_redundant;
1046 BUG_ON(s->h->s == s);
1048 closure_sync(&s->iodone);
1050 for (i = 0; i < nr_data; i++)
1052 ob = c->open_buckets + s->blocks[i];
1054 if (ob->sectors_free)
1055 zero_out_rest_of_ec_bucket(c, s, i, ob);
1059 if (!bch2_err_matches(s->err, EROFS))
1060 bch_err(c, "error creating stripe: error writing data buckets");
1064 if (s->have_existing_stripe) {
1065 ec_validate_checksums(c, &s->existing_stripe);
1067 if (ec_do_recov(c, &s->existing_stripe)) {
1068 bch_err(c, "error creating stripe: error reading existing stripe");
1072 for (i = 0; i < nr_data; i++)
1073 if (stripe_blockcount_get(&s->existing_stripe.key.v, i))
1074 swap(s->new_stripe.data[i],
1075 s->existing_stripe.data[i]);
1077 ec_stripe_buf_exit(&s->existing_stripe);
1080 BUG_ON(!s->allocated);
1082 ec_generate_ec(&s->new_stripe);
1084 ec_generate_checksums(&s->new_stripe);
1087 for (i = nr_data; i < v->nr_blocks; i++)
1088 ec_block_io(c, &s->new_stripe, REQ_OP_WRITE, i, &s->iodone);
1089 closure_sync(&s->iodone);
1091 if (ec_nr_failed(&s->new_stripe)) {
1092 bch_err(c, "error creating stripe: error writing redundancy buckets");
1096 ret = bch2_trans_do(c, &s->res, NULL, BTREE_INSERT_NOFAIL,
1097 ec_stripe_key_update(&trans, &s->new_stripe.key,
1098 !s->have_existing_stripe));
1100 bch_err(c, "error creating stripe: error creating stripe key");
1104 ret = ec_stripe_update_extents(c, &s->new_stripe);
1106 bch_err(c, "error creating stripe: error updating pointers: %s",
1111 bch2_stripe_close(c, s);
1113 bch2_disk_reservation_put(c, &s->res);
1115 for (i = 0; i < v->nr_blocks; i++)
1117 ob = c->open_buckets + s->blocks[i];
1121 __bch2_open_bucket_put(c, ob);
1123 bch2_open_bucket_put(c, ob);
1127 ec_stripe_buf_exit(&s->existing_stripe);
1128 ec_stripe_buf_exit(&s->new_stripe);
1129 closure_debug_destroy(&s->iodone);
1133 static struct ec_stripe_new *get_pending_stripe(struct bch_fs *c)
1135 struct ec_stripe_new *s;
1137 mutex_lock(&c->ec_stripe_new_lock);
1138 list_for_each_entry(s, &c->ec_stripe_new_list, list)
1139 if (!atomic_read(&s->pin)) {
1145 mutex_unlock(&c->ec_stripe_new_lock);
1150 static void ec_stripe_create_work(struct work_struct *work)
1152 struct bch_fs *c = container_of(work,
1153 struct bch_fs, ec_stripe_create_work);
1154 struct ec_stripe_new *s;
1156 while ((s = get_pending_stripe(c)))
1157 ec_stripe_create(s);
1159 bch2_write_ref_put(c, BCH_WRITE_REF_stripe_create);
1162 void bch2_ec_do_stripe_creates(struct bch_fs *c)
1164 bch2_write_ref_get(c, BCH_WRITE_REF_stripe_create);
1166 if (!queue_work(system_long_wq, &c->ec_stripe_create_work))
1167 bch2_write_ref_put(c, BCH_WRITE_REF_stripe_create);
1170 static void ec_stripe_new_put(struct bch_fs *c, struct ec_stripe_new *s)
1172 BUG_ON(atomic_read(&s->pin) <= 0);
1174 if (atomic_dec_and_test(&s->pin))
1175 bch2_ec_do_stripe_creates(c);
1178 static void ec_stripe_set_pending(struct bch_fs *c, struct ec_stripe_head *h)
1180 struct ec_stripe_new *s = h->s;
1182 BUG_ON(!s->allocated && !s->err);
1187 mutex_lock(&c->ec_stripe_new_lock);
1188 list_add(&s->list, &c->ec_stripe_new_list);
1189 mutex_unlock(&c->ec_stripe_new_lock);
1191 ec_stripe_new_put(c, s);
1194 /* have a full bucket - hand it off to be erasure coded: */
1195 void bch2_ec_bucket_written(struct bch_fs *c, struct open_bucket *ob)
1197 struct ec_stripe_new *s = ob->ec;
1199 ec_stripe_new_put(c, s);
1202 void bch2_ec_bucket_cancel(struct bch_fs *c, struct open_bucket *ob)
1204 struct ec_stripe_new *s = ob->ec;
1209 void *bch2_writepoint_ec_buf(struct bch_fs *c, struct write_point *wp)
1211 struct open_bucket *ob = ec_open_bucket(c, &wp->ptrs);
1218 ca = bch_dev_bkey_exists(c, ob->dev);
1219 offset = ca->mi.bucket_size - ob->sectors_free;
1221 return ob->ec->new_stripe.data[ob->ec_idx] + (offset << 9);
1224 static int unsigned_cmp(const void *_l, const void *_r)
1226 unsigned l = *((const unsigned *) _l);
1227 unsigned r = *((const unsigned *) _r);
1229 return cmp_int(l, r);
1232 /* pick most common bucket size: */
1233 static unsigned pick_blocksize(struct bch_fs *c,
1234 struct bch_devs_mask *devs)
1237 unsigned i, nr = 0, sizes[BCH_SB_MEMBERS_MAX];
1240 } cur = { 0, 0 }, best = { 0, 0 };
1242 for_each_member_device_rcu(ca, c, i, devs)
1243 sizes[nr++] = ca->mi.bucket_size;
1245 sort(sizes, nr, sizeof(unsigned), unsigned_cmp, NULL);
1247 for (i = 0; i < nr; i++) {
1248 if (sizes[i] != cur.size) {
1249 if (cur.nr > best.nr)
1253 cur.size = sizes[i];
1259 if (cur.nr > best.nr)
1265 static bool may_create_new_stripe(struct bch_fs *c)
1270 static void ec_stripe_key_init(struct bch_fs *c,
1271 struct bkey_i_stripe *s,
1274 unsigned stripe_size)
1278 bkey_stripe_init(&s->k_i);
1279 s->v.sectors = cpu_to_le16(stripe_size);
1281 s->v.nr_blocks = nr_data + nr_parity;
1282 s->v.nr_redundant = nr_parity;
1283 s->v.csum_granularity_bits = ilog2(c->opts.encoded_extent_max >> 9);
1284 s->v.csum_type = BCH_CSUM_crc32c;
1287 while ((u64s = stripe_val_u64s(&s->v)) > BKEY_VAL_U64s_MAX) {
1288 BUG_ON(1 << s->v.csum_granularity_bits >=
1289 le16_to_cpu(s->v.sectors) ||
1290 s->v.csum_granularity_bits == U8_MAX);
1291 s->v.csum_granularity_bits++;
1294 set_bkey_val_u64s(&s->k, u64s);
1297 static int ec_new_stripe_alloc(struct bch_fs *c, struct ec_stripe_head *h)
1299 struct ec_stripe_new *s;
1301 lockdep_assert_held(&h->lock);
1303 s = kzalloc(sizeof(*s), GFP_KERNEL);
1307 mutex_init(&s->lock);
1308 closure_init(&s->iodone, NULL);
1309 atomic_set(&s->pin, 1);
1312 s->nr_data = min_t(unsigned, h->nr_active_devs,
1313 BCH_BKEY_PTRS_MAX) - h->redundancy;
1314 s->nr_parity = h->redundancy;
1316 ec_stripe_key_init(c, &s->new_stripe.key, s->nr_data,
1317 s->nr_parity, h->blocksize);
1323 static struct ec_stripe_head *
1324 ec_new_stripe_head_alloc(struct bch_fs *c, unsigned target,
1325 unsigned algo, unsigned redundancy,
1328 struct ec_stripe_head *h;
1332 h = kzalloc(sizeof(*h), GFP_KERNEL);
1336 mutex_init(&h->lock);
1337 BUG_ON(!mutex_trylock(&h->lock));
1341 h->redundancy = redundancy;
1345 h->devs = target_rw_devs(c, BCH_DATA_user, target);
1347 for_each_member_device_rcu(ca, c, i, &h->devs)
1348 if (!ca->mi.durability)
1349 __clear_bit(i, h->devs.d);
1351 h->blocksize = pick_blocksize(c, &h->devs);
1353 for_each_member_device_rcu(ca, c, i, &h->devs)
1354 if (ca->mi.bucket_size == h->blocksize)
1355 h->nr_active_devs++;
1358 list_add(&h->list, &c->ec_stripe_head_list);
1362 void bch2_ec_stripe_head_put(struct bch_fs *c, struct ec_stripe_head *h)
1366 bitmap_weight(h->s->blocks_allocated,
1367 h->s->nr_data) == h->s->nr_data)
1368 ec_stripe_set_pending(c, h);
1370 mutex_unlock(&h->lock);
1373 struct ec_stripe_head *__bch2_ec_stripe_head_get(struct btree_trans *trans,
1376 unsigned redundancy,
1379 struct bch_fs *c = trans->c;
1380 struct ec_stripe_head *h;
1386 ret = bch2_trans_mutex_lock(trans, &c->ec_stripe_head_lock);
1388 return ERR_PTR(ret);
1390 list_for_each_entry(h, &c->ec_stripe_head_list, list)
1391 if (h->target == target &&
1393 h->redundancy == redundancy &&
1394 h->copygc == copygc) {
1395 ret = bch2_trans_mutex_lock(trans, &h->lock);
1401 h = ec_new_stripe_head_alloc(c, target, algo, redundancy, copygc);
1403 mutex_unlock(&c->ec_stripe_head_lock);
1407 static int new_stripe_alloc_buckets(struct btree_trans *trans, struct ec_stripe_head *h,
1410 struct bch_fs *c = trans->c;
1411 struct bch_devs_mask devs = h->devs;
1412 struct open_bucket *ob;
1413 struct open_buckets buckets;
1414 unsigned i, j, nr_have_parity = 0, nr_have_data = 0;
1415 bool have_cache = true;
1418 for (i = 0; i < h->s->new_stripe.key.v.nr_blocks; i++) {
1419 if (test_bit(i, h->s->blocks_gotten)) {
1420 __clear_bit(h->s->new_stripe.key.v.ptrs[i].dev, devs.d);
1421 if (i < h->s->nr_data)
1428 BUG_ON(nr_have_data > h->s->nr_data);
1429 BUG_ON(nr_have_parity > h->s->nr_parity);
1432 if (nr_have_parity < h->s->nr_parity) {
1433 ret = bch2_bucket_alloc_set_trans(trans, &buckets,
1445 open_bucket_for_each(c, &buckets, ob, i) {
1446 j = find_next_zero_bit(h->s->blocks_gotten,
1447 h->s->nr_data + h->s->nr_parity,
1449 BUG_ON(j >= h->s->nr_data + h->s->nr_parity);
1451 h->s->blocks[j] = buckets.v[i];
1452 h->s->new_stripe.key.v.ptrs[j] = bch2_ob_ptr(c, ob);
1453 __set_bit(j, h->s->blocks_gotten);
1461 if (nr_have_data < h->s->nr_data) {
1462 ret = bch2_bucket_alloc_set_trans(trans, &buckets,
1474 open_bucket_for_each(c, &buckets, ob, i) {
1475 j = find_next_zero_bit(h->s->blocks_gotten,
1477 BUG_ON(j >= h->s->nr_data);
1479 h->s->blocks[j] = buckets.v[i];
1480 h->s->new_stripe.key.v.ptrs[j] = bch2_ob_ptr(c, ob);
1481 __set_bit(j, h->s->blocks_gotten);
1491 /* XXX: doesn't obey target: */
1492 static s64 get_existing_stripe(struct bch_fs *c,
1493 struct ec_stripe_head *head)
1495 ec_stripes_heap *h = &c->ec_stripes_heap;
1501 if (may_create_new_stripe(c))
1504 mutex_lock(&c->ec_stripes_heap_lock);
1505 for (heap_idx = 0; heap_idx < h->used; heap_idx++) {
1506 /* No blocks worth reusing, stripe will just be deleted: */
1507 if (!h->data[heap_idx].blocks_nonempty)
1510 stripe_idx = h->data[heap_idx].idx;
1512 m = genradix_ptr(&c->stripes, stripe_idx);
1514 if (m->algorithm == head->algo &&
1515 m->nr_redundant == head->redundancy &&
1516 m->sectors == head->blocksize &&
1517 m->blocks_nonempty < m->nr_blocks - m->nr_redundant &&
1518 bch2_try_open_stripe(c, head->s, stripe_idx)) {
1523 mutex_unlock(&c->ec_stripes_heap_lock);
1527 static int __bch2_ec_stripe_head_reuse(struct btree_trans *trans, struct ec_stripe_head *h)
1529 struct bch_fs *c = trans->c;
1534 idx = get_existing_stripe(c, h);
1536 return -BCH_ERR_ENOSPC_stripe_reuse;
1538 h->s->have_existing_stripe = true;
1539 ret = get_stripe_key_trans(trans, idx, &h->s->existing_stripe);
1541 bch2_fs_fatal_error(c, "error reading stripe key: %i", ret);
1545 if (ec_stripe_buf_init(&h->s->existing_stripe, 0, h->blocksize)) {
1547 * this is a problem: we have deleted from the
1548 * stripes heap already
1553 BUG_ON(h->s->existing_stripe.size != h->blocksize);
1554 BUG_ON(h->s->existing_stripe.size != h->s->existing_stripe.key.v.sectors);
1556 for (i = 0; i < h->s->existing_stripe.key.v.nr_blocks; i++) {
1557 if (stripe_blockcount_get(&h->s->existing_stripe.key.v, i)) {
1558 __set_bit(i, h->s->blocks_gotten);
1559 __set_bit(i, h->s->blocks_allocated);
1562 ec_block_io(c, &h->s->existing_stripe, READ, i, &h->s->iodone);
1565 bkey_copy(&h->s->new_stripe.key.k_i,
1566 &h->s->existing_stripe.key.k_i);
1571 static int __bch2_ec_stripe_head_reserve(struct btree_trans *trans, struct ec_stripe_head *h)
1573 struct bch_fs *c = trans->c;
1574 struct btree_iter iter;
1576 struct bpos min_pos = POS(0, 1);
1577 struct bpos start_pos = bpos_max(min_pos, POS(0, c->ec_stripe_hint));
1580 BUG_ON(h->s->res.sectors);
1582 ret = bch2_disk_reservation_get(c, &h->s->res,
1584 h->s->nr_parity, 0);
1588 for_each_btree_key_norestart(trans, iter, BTREE_ID_stripes, start_pos,
1589 BTREE_ITER_SLOTS|BTREE_ITER_INTENT, k, ret) {
1590 if (bkey_gt(k.k->p, POS(0, U32_MAX))) {
1591 if (start_pos.offset) {
1592 start_pos = min_pos;
1593 bch2_btree_iter_set_pos(&iter, start_pos);
1597 ret = -BCH_ERR_ENOSPC_stripe_create;
1601 if (bkey_deleted(k.k) &&
1602 bch2_try_open_stripe(c, h->s, k.k->p.offset))
1606 c->ec_stripe_hint = iter.pos.offset;
1611 ret = ec_stripe_mem_alloc(trans, &iter);
1613 bch2_stripe_close(c, h->s);
1617 h->s->new_stripe.key.k.p = iter.pos;
1619 bch2_trans_iter_exit(trans, &iter);
1622 bch2_disk_reservation_put(c, &h->s->res);
1626 struct ec_stripe_head *bch2_ec_stripe_head_get(struct btree_trans *trans,
1629 unsigned redundancy,
1633 struct bch_fs *c = trans->c;
1634 struct ec_stripe_head *h;
1636 bool needs_stripe_new;
1638 h = __bch2_ec_stripe_head_get(trans, target, algo, redundancy, copygc);
1640 bch_err(c, "no stripe head");
1641 if (IS_ERR_OR_NULL(h))
1644 needs_stripe_new = !h->s;
1645 if (needs_stripe_new) {
1646 if (ec_new_stripe_alloc(c, h)) {
1648 bch_err(c, "failed to allocate new stripe");
1652 if (ec_stripe_buf_init(&h->s->new_stripe, 0, h->blocksize))
1657 * Try reserve a new stripe before reusing an
1658 * existing stripe. This will prevent unnecessary
1659 * read amplification during write oriented workloads.
1662 if (!h->s->allocated && !h->s->res.sectors && !h->s->have_existing_stripe)
1663 ret = __bch2_ec_stripe_head_reserve(trans, h);
1664 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
1667 if (ret && needs_stripe_new)
1668 ret = __bch2_ec_stripe_head_reuse(trans, h);
1670 bch_err_ratelimited(c, "failed to get stripe: %s", bch2_err_str(ret));
1674 if (!h->s->allocated) {
1675 ret = new_stripe_alloc_buckets(trans, h, cl);
1679 h->s->allocated = true;
1682 BUG_ON(trans->restarted);
1685 bch2_ec_stripe_head_put(c, h);
1686 return ERR_PTR(ret);
1689 void bch2_ec_stop_dev(struct bch_fs *c, struct bch_dev *ca)
1691 struct ec_stripe_head *h;
1692 struct open_bucket *ob;
1695 mutex_lock(&c->ec_stripe_head_lock);
1696 list_for_each_entry(h, &c->ec_stripe_head_list, list) {
1698 mutex_lock(&h->lock);
1702 for (i = 0; i < h->s->new_stripe.key.v.nr_blocks; i++) {
1703 if (!h->s->blocks[i])
1706 ob = c->open_buckets + h->s->blocks[i];
1707 if (ob->dev == ca->dev_idx)
1713 ec_stripe_set_pending(c, h);
1715 mutex_unlock(&h->lock);
1717 mutex_unlock(&c->ec_stripe_head_lock);
1720 int bch2_stripes_read(struct bch_fs *c)
1722 struct btree_trans trans;
1723 struct btree_iter iter;
1725 const struct bch_stripe *s;
1730 bch2_trans_init(&trans, c, 0, 0);
1732 for_each_btree_key(&trans, iter, BTREE_ID_stripes, POS_MIN,
1733 BTREE_ITER_PREFETCH, k, ret) {
1734 if (k.k->type != KEY_TYPE_stripe)
1737 ret = __ec_stripe_mem_alloc(c, k.k->p.offset, GFP_KERNEL);
1741 s = bkey_s_c_to_stripe(k).v;
1743 m = genradix_ptr(&c->stripes, k.k->p.offset);
1744 m->sectors = le16_to_cpu(s->sectors);
1745 m->algorithm = s->algorithm;
1746 m->nr_blocks = s->nr_blocks;
1747 m->nr_redundant = s->nr_redundant;
1748 m->blocks_nonempty = 0;
1750 for (i = 0; i < s->nr_blocks; i++)
1751 m->blocks_nonempty += !!stripe_blockcount_get(s, i);
1753 bch2_stripes_heap_insert(c, m, k.k->p.offset);
1755 bch2_trans_iter_exit(&trans, &iter);
1757 bch2_trans_exit(&trans);
1760 bch_err(c, "error reading stripes: %i", ret);
1765 void bch2_stripes_heap_to_text(struct printbuf *out, struct bch_fs *c)
1767 ec_stripes_heap *h = &c->ec_stripes_heap;
1771 mutex_lock(&c->ec_stripes_heap_lock);
1772 for (i = 0; i < min_t(size_t, h->used, 20); i++) {
1773 m = genradix_ptr(&c->stripes, h->data[i].idx);
1775 prt_printf(out, "%zu %u/%u+%u\n", h->data[i].idx,
1776 h->data[i].blocks_nonempty,
1777 m->nr_blocks - m->nr_redundant,
1780 mutex_unlock(&c->ec_stripes_heap_lock);
1783 void bch2_new_stripes_to_text(struct printbuf *out, struct bch_fs *c)
1785 struct ec_stripe_head *h;
1786 struct ec_stripe_new *s;
1788 mutex_lock(&c->ec_stripe_head_lock);
1789 list_for_each_entry(h, &c->ec_stripe_head_list, list) {
1790 prt_printf(out, "target %u algo %u redundancy %u:\n",
1791 h->target, h->algo, h->redundancy);
1794 prt_printf(out, "\tpending: blocks %u+%u allocated %u\n",
1795 h->s->nr_data, h->s->nr_parity,
1796 bitmap_weight(h->s->blocks_allocated,
1799 mutex_unlock(&c->ec_stripe_head_lock);
1801 mutex_lock(&c->ec_stripe_new_lock);
1802 list_for_each_entry(s, &c->ec_stripe_new_list, list) {
1803 prt_printf(out, "\tin flight: blocks %u+%u pin %u\n",
1804 s->nr_data, s->nr_parity,
1805 atomic_read(&s->pin));
1807 mutex_unlock(&c->ec_stripe_new_lock);
1810 void bch2_fs_ec_exit(struct bch_fs *c)
1812 struct ec_stripe_head *h;
1816 mutex_lock(&c->ec_stripe_head_lock);
1817 h = list_first_entry_or_null(&c->ec_stripe_head_list,
1818 struct ec_stripe_head, list);
1821 mutex_unlock(&c->ec_stripe_head_lock);
1826 for (i = 0; i < h->s->new_stripe.key.v.nr_blocks; i++)
1827 BUG_ON(h->s->blocks[i]);
1834 BUG_ON(!list_empty(&c->ec_stripe_new_list));
1836 free_heap(&c->ec_stripes_heap);
1837 genradix_free(&c->stripes);
1838 bioset_exit(&c->ec_bioset);
1841 void bch2_fs_ec_init_early(struct bch_fs *c)
1843 INIT_WORK(&c->ec_stripe_create_work, ec_stripe_create_work);
1844 INIT_WORK(&c->ec_stripe_delete_work, ec_stripe_delete_work);
1847 int bch2_fs_ec_init(struct bch_fs *c)
1849 spin_lock_init(&c->ec_stripes_new_lock);
1851 return bioset_init(&c->ec_bioset, 1, offsetof(struct ec_bio, bio),