1 // SPDX-License-Identifier: GPL-2.0
6 #include "alloc_foreground.h"
7 #include "backpointers.h"
11 #include "btree_update.h"
12 #include "btree_write_buffer.h"
15 #include "disk_groups.h"
25 #include <linux/sort.h>
29 #include <linux/raid/pq.h>
30 #include <linux/raid/xor.h>
32 static void raid5_recov(unsigned disks, unsigned failed_idx,
33 size_t size, void **data)
37 BUG_ON(failed_idx >= disks);
39 swap(data[0], data[failed_idx]);
40 memcpy(data[0], data[1], size);
43 nr = min_t(unsigned, disks - i, MAX_XOR_BLOCKS);
44 xor_blocks(nr, size, data[0], data + i);
48 swap(data[0], data[failed_idx]);
51 static void raid_gen(int nd, int np, size_t size, void **v)
54 raid5_recov(nd + np, nd, size, v);
56 raid6_call.gen_syndrome(nd + np, size, v);
60 static void raid_rec(int nr, int *ir, int nd, int np, size_t size, void **v)
67 raid5_recov(nd + 1, ir[0], size, v);
69 raid6_call.gen_syndrome(nd + np, size, v);
73 /* data+data failure. */
74 raid6_2data_recov(nd + np, size, ir[0], ir[1], v);
75 } else if (ir[0] < nd) {
76 /* data + p/q failure */
78 if (ir[1] == nd) /* data + p failure */
79 raid6_datap_recov(nd + np, size, ir[0], v);
80 else { /* data + q failure */
81 raid5_recov(nd + 1, ir[0], size, v);
82 raid6_call.gen_syndrome(nd + np, size, v);
85 raid_gen(nd, np, size, v);
95 #include <raid/raid.h>
101 struct ec_stripe_buf *buf;
106 /* Stripes btree keys: */
108 int bch2_stripe_invalid(struct bch_fs *c, struct bkey_s_c k,
109 enum bkey_invalid_flags flags,
110 struct printbuf *err)
112 const struct bch_stripe *s = bkey_s_c_to_stripe(k).v;
115 bkey_fsck_err_on(bkey_eq(k.k->p, POS_MIN) ||
116 bpos_gt(k.k->p, POS(0, U32_MAX)), c, err,
118 "stripe at bad pos");
120 bkey_fsck_err_on(bkey_val_u64s(k.k) < stripe_val_u64s(s), c, err,
122 "incorrect value size (%zu < %u)",
123 bkey_val_u64s(k.k), stripe_val_u64s(s));
125 ret = bch2_bkey_ptrs_invalid(c, k, flags, err);
130 void bch2_stripe_to_text(struct printbuf *out, struct bch_fs *c,
133 const struct bch_stripe *s = bkey_s_c_to_stripe(k).v;
134 unsigned i, nr_data = s->nr_blocks - s->nr_redundant;
136 prt_printf(out, "algo %u sectors %u blocks %u:%u csum %u gran %u",
138 le16_to_cpu(s->sectors),
142 1U << s->csum_granularity_bits);
144 for (i = 0; i < s->nr_blocks; i++) {
145 const struct bch_extent_ptr *ptr = s->ptrs + i;
146 struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
148 u64 b = sector_to_bucket_and_offset(ca, ptr->offset, &offset);
150 prt_printf(out, " %u:%llu:%u", ptr->dev, b, offset);
152 prt_printf(out, "#%u", stripe_blockcount_get(s, i));
153 if (ptr_stale(ca, ptr))
154 prt_printf(out, " stale");
158 /* returns blocknr in stripe that we matched: */
159 static const struct bch_extent_ptr *bkey_matches_stripe(struct bch_stripe *s,
160 struct bkey_s_c k, unsigned *block)
162 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
163 const struct bch_extent_ptr *ptr;
164 unsigned i, nr_data = s->nr_blocks - s->nr_redundant;
166 bkey_for_each_ptr(ptrs, ptr)
167 for (i = 0; i < nr_data; i++)
168 if (__bch2_ptr_matches_stripe(&s->ptrs[i], ptr,
169 le16_to_cpu(s->sectors))) {
177 static bool extent_has_stripe_ptr(struct bkey_s_c k, u64 idx)
180 case KEY_TYPE_extent: {
181 struct bkey_s_c_extent e = bkey_s_c_to_extent(k);
182 const union bch_extent_entry *entry;
184 extent_for_each_entry(e, entry)
185 if (extent_entry_type(entry) ==
186 BCH_EXTENT_ENTRY_stripe_ptr &&
187 entry->stripe_ptr.idx == idx)
199 static void ec_stripe_buf_exit(struct ec_stripe_buf *buf)
201 if (buf->key.k.type == KEY_TYPE_stripe) {
202 struct bkey_i_stripe *s = bkey_i_to_stripe(&buf->key);
205 for (i = 0; i < s->v.nr_blocks; i++) {
206 kvpfree(buf->data[i], buf->size << 9);
212 /* XXX: this is a non-mempoolified memory allocation: */
213 static int ec_stripe_buf_init(struct ec_stripe_buf *buf,
214 unsigned offset, unsigned size)
216 struct bch_stripe *v = &bkey_i_to_stripe(&buf->key)->v;
217 unsigned csum_granularity = 1U << v->csum_granularity_bits;
218 unsigned end = offset + size;
221 BUG_ON(end > le16_to_cpu(v->sectors));
223 offset = round_down(offset, csum_granularity);
224 end = min_t(unsigned, le16_to_cpu(v->sectors),
225 round_up(end, csum_granularity));
227 buf->offset = offset;
228 buf->size = end - offset;
230 memset(buf->valid, 0xFF, sizeof(buf->valid));
232 for (i = 0; i < v->nr_blocks; i++) {
233 buf->data[i] = kvpmalloc(buf->size << 9, GFP_KERNEL);
240 ec_stripe_buf_exit(buf);
241 return -BCH_ERR_ENOMEM_stripe_buf;
246 static struct bch_csum ec_block_checksum(struct ec_stripe_buf *buf,
247 unsigned block, unsigned offset)
249 struct bch_stripe *v = &bkey_i_to_stripe(&buf->key)->v;
250 unsigned csum_granularity = 1 << v->csum_granularity_bits;
251 unsigned end = buf->offset + buf->size;
252 unsigned len = min(csum_granularity, end - offset);
254 BUG_ON(offset >= end);
255 BUG_ON(offset < buf->offset);
256 BUG_ON(offset & (csum_granularity - 1));
257 BUG_ON(offset + len != le16_to_cpu(v->sectors) &&
258 (len & (csum_granularity - 1)));
260 return bch2_checksum(NULL, v->csum_type,
262 buf->data[block] + ((offset - buf->offset) << 9),
266 static void ec_generate_checksums(struct ec_stripe_buf *buf)
268 struct bch_stripe *v = &bkey_i_to_stripe(&buf->key)->v;
269 unsigned i, j, csums_per_device = stripe_csums_per_device(v);
275 BUG_ON(buf->size != le16_to_cpu(v->sectors));
277 for (i = 0; i < v->nr_blocks; i++)
278 for (j = 0; j < csums_per_device; j++)
279 stripe_csum_set(v, i, j,
280 ec_block_checksum(buf, i, j << v->csum_granularity_bits));
283 static void ec_validate_checksums(struct bch_fs *c, struct ec_stripe_buf *buf)
285 struct bch_stripe *v = &bkey_i_to_stripe(&buf->key)->v;
286 unsigned csum_granularity = 1 << v->csum_granularity_bits;
292 for (i = 0; i < v->nr_blocks; i++) {
293 unsigned offset = buf->offset;
294 unsigned end = buf->offset + buf->size;
296 if (!test_bit(i, buf->valid))
299 while (offset < end) {
300 unsigned j = offset >> v->csum_granularity_bits;
301 unsigned len = min(csum_granularity, end - offset);
302 struct bch_csum want = stripe_csum_get(v, i, j);
303 struct bch_csum got = ec_block_checksum(buf, i, offset);
305 if (bch2_crc_cmp(want, got)) {
306 struct printbuf buf2 = PRINTBUF;
308 bch2_bkey_val_to_text(&buf2, c, bkey_i_to_s_c(&buf->key));
310 bch_err_ratelimited(c,
311 "stripe checksum error for %ps at %u:%u: csum type %u, expected %llx got %llx\n%s",
312 (void *) _RET_IP_, i, j, v->csum_type,
313 want.lo, got.lo, buf2.buf);
314 printbuf_exit(&buf2);
315 clear_bit(i, buf->valid);
324 /* Erasure coding: */
326 static void ec_generate_ec(struct ec_stripe_buf *buf)
328 struct bch_stripe *v = &bkey_i_to_stripe(&buf->key)->v;
329 unsigned nr_data = v->nr_blocks - v->nr_redundant;
330 unsigned bytes = le16_to_cpu(v->sectors) << 9;
332 raid_gen(nr_data, v->nr_redundant, bytes, buf->data);
335 static unsigned ec_nr_failed(struct ec_stripe_buf *buf)
337 struct bch_stripe *v = &bkey_i_to_stripe(&buf->key)->v;
339 return v->nr_blocks - bitmap_weight(buf->valid, v->nr_blocks);
342 static int ec_do_recov(struct bch_fs *c, struct ec_stripe_buf *buf)
344 struct bch_stripe *v = &bkey_i_to_stripe(&buf->key)->v;
345 unsigned i, failed[BCH_BKEY_PTRS_MAX], nr_failed = 0;
346 unsigned nr_data = v->nr_blocks - v->nr_redundant;
347 unsigned bytes = buf->size << 9;
349 if (ec_nr_failed(buf) > v->nr_redundant) {
350 bch_err_ratelimited(c,
351 "error doing reconstruct read: unable to read enough blocks");
355 for (i = 0; i < nr_data; i++)
356 if (!test_bit(i, buf->valid))
357 failed[nr_failed++] = i;
359 raid_rec(nr_failed, failed, nr_data, v->nr_redundant, bytes, buf->data);
365 static void ec_block_endio(struct bio *bio)
367 struct ec_bio *ec_bio = container_of(bio, struct ec_bio, bio);
368 struct bch_stripe *v = &bkey_i_to_stripe(&ec_bio->buf->key)->v;
369 struct bch_extent_ptr *ptr = &v->ptrs[ec_bio->idx];
370 struct bch_dev *ca = ec_bio->ca;
371 struct closure *cl = bio->bi_private;
373 if (bch2_dev_io_err_on(bio->bi_status, ca,
375 ? BCH_MEMBER_ERROR_write
376 : BCH_MEMBER_ERROR_read,
377 "erasure coding %s error: %s",
378 bio_data_dir(bio) ? "write" : "read",
379 bch2_blk_status_to_str(bio->bi_status)))
380 clear_bit(ec_bio->idx, ec_bio->buf->valid);
382 if (ptr_stale(ca, ptr)) {
383 bch_err_ratelimited(ca->fs,
384 "error %s stripe: stale pointer after io",
385 bio_data_dir(bio) == READ ? "reading from" : "writing to");
386 clear_bit(ec_bio->idx, ec_bio->buf->valid);
389 bio_put(&ec_bio->bio);
390 percpu_ref_put(&ca->io_ref);
394 static void ec_block_io(struct bch_fs *c, struct ec_stripe_buf *buf,
395 blk_opf_t opf, unsigned idx, struct closure *cl)
397 struct bch_stripe *v = &bkey_i_to_stripe(&buf->key)->v;
398 unsigned offset = 0, bytes = buf->size << 9;
399 struct bch_extent_ptr *ptr = &v->ptrs[idx];
400 struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
401 enum bch_data_type data_type = idx < v->nr_blocks - v->nr_redundant
404 int rw = op_is_write(opf);
406 if (ptr_stale(ca, ptr)) {
407 bch_err_ratelimited(c,
408 "error %s stripe: stale pointer",
409 rw == READ ? "reading from" : "writing to");
410 clear_bit(idx, buf->valid);
414 if (!bch2_dev_get_ioref(ca, rw)) {
415 clear_bit(idx, buf->valid);
419 this_cpu_add(ca->io_done->sectors[rw][data_type], buf->size);
421 while (offset < bytes) {
422 unsigned nr_iovecs = min_t(size_t, BIO_MAX_VECS,
423 DIV_ROUND_UP(bytes, PAGE_SIZE));
424 unsigned b = min_t(size_t, bytes - offset,
425 nr_iovecs << PAGE_SHIFT);
426 struct ec_bio *ec_bio;
428 ec_bio = container_of(bio_alloc_bioset(ca->disk_sb.bdev,
439 ec_bio->bio.bi_iter.bi_sector = ptr->offset + buf->offset + (offset >> 9);
440 ec_bio->bio.bi_end_io = ec_block_endio;
441 ec_bio->bio.bi_private = cl;
443 bch2_bio_map(&ec_bio->bio, buf->data[idx] + offset, b);
446 percpu_ref_get(&ca->io_ref);
448 submit_bio(&ec_bio->bio);
453 percpu_ref_put(&ca->io_ref);
456 static int get_stripe_key_trans(struct btree_trans *trans, u64 idx,
457 struct ec_stripe_buf *stripe)
459 struct btree_iter iter;
463 k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_stripes,
464 POS(0, idx), BTREE_ITER_SLOTS);
468 if (k.k->type != KEY_TYPE_stripe) {
472 bkey_reassemble(&stripe->key, k);
474 bch2_trans_iter_exit(trans, &iter);
478 static int get_stripe_key(struct bch_fs *c, u64 idx, struct ec_stripe_buf *stripe)
480 return bch2_trans_run(c, get_stripe_key_trans(trans, idx, stripe));
483 /* recovery read path: */
484 int bch2_ec_read_extent(struct bch_fs *c, struct bch_read_bio *rbio)
486 struct ec_stripe_buf *buf;
488 struct bch_stripe *v;
492 closure_init_stack(&cl);
494 BUG_ON(!rbio->pick.has_ec);
496 buf = kzalloc(sizeof(*buf), GFP_NOFS);
498 return -BCH_ERR_ENOMEM_ec_read_extent;
500 ret = get_stripe_key(c, rbio->pick.ec.idx, buf);
502 bch_err_ratelimited(c,
503 "error doing reconstruct read: error %i looking up stripe", ret);
508 v = &bkey_i_to_stripe(&buf->key)->v;
510 if (!bch2_ptr_matches_stripe(v, rbio->pick)) {
511 bch_err_ratelimited(c,
512 "error doing reconstruct read: pointer doesn't match stripe");
517 offset = rbio->bio.bi_iter.bi_sector - v->ptrs[rbio->pick.ec.block].offset;
518 if (offset + bio_sectors(&rbio->bio) > le16_to_cpu(v->sectors)) {
519 bch_err_ratelimited(c,
520 "error doing reconstruct read: read is bigger than stripe");
525 ret = ec_stripe_buf_init(buf, offset, bio_sectors(&rbio->bio));
529 for (i = 0; i < v->nr_blocks; i++)
530 ec_block_io(c, buf, REQ_OP_READ, i, &cl);
534 if (ec_nr_failed(buf) > v->nr_redundant) {
535 bch_err_ratelimited(c,
536 "error doing reconstruct read: unable to read enough blocks");
541 ec_validate_checksums(c, buf);
543 ret = ec_do_recov(c, buf);
547 memcpy_to_bio(&rbio->bio, rbio->bio.bi_iter,
548 buf->data[rbio->pick.ec.block] + ((offset - buf->offset) << 9));
550 ec_stripe_buf_exit(buf);
555 /* stripe bucket accounting: */
557 static int __ec_stripe_mem_alloc(struct bch_fs *c, size_t idx, gfp_t gfp)
559 ec_stripes_heap n, *h = &c->ec_stripes_heap;
561 if (idx >= h->size) {
562 if (!init_heap(&n, max(1024UL, roundup_pow_of_two(idx + 1)), gfp))
563 return -BCH_ERR_ENOMEM_ec_stripe_mem_alloc;
565 mutex_lock(&c->ec_stripes_heap_lock);
566 if (n.size > h->size) {
567 memcpy(n.data, h->data, h->used * sizeof(h->data[0]));
571 mutex_unlock(&c->ec_stripes_heap_lock);
576 if (!genradix_ptr_alloc(&c->stripes, idx, gfp))
577 return -BCH_ERR_ENOMEM_ec_stripe_mem_alloc;
579 if (c->gc_pos.phase != GC_PHASE_NOT_RUNNING &&
580 !genradix_ptr_alloc(&c->gc_stripes, idx, gfp))
581 return -BCH_ERR_ENOMEM_ec_stripe_mem_alloc;
586 static int ec_stripe_mem_alloc(struct btree_trans *trans,
587 struct btree_iter *iter)
589 return allocate_dropping_locks_errcode(trans,
590 __ec_stripe_mem_alloc(trans->c, iter->pos.offset, _gfp));
594 * Hash table of open stripes:
595 * Stripes that are being created or modified are kept in a hash table, so that
596 * stripe deletion can skip them.
599 static bool __bch2_stripe_is_open(struct bch_fs *c, u64 idx)
601 unsigned hash = hash_64(idx, ilog2(ARRAY_SIZE(c->ec_stripes_new)));
602 struct ec_stripe_new *s;
604 hlist_for_each_entry(s, &c->ec_stripes_new[hash], hash)
610 static bool bch2_stripe_is_open(struct bch_fs *c, u64 idx)
614 spin_lock(&c->ec_stripes_new_lock);
615 ret = __bch2_stripe_is_open(c, idx);
616 spin_unlock(&c->ec_stripes_new_lock);
621 static bool bch2_try_open_stripe(struct bch_fs *c,
622 struct ec_stripe_new *s,
627 spin_lock(&c->ec_stripes_new_lock);
628 ret = !__bch2_stripe_is_open(c, idx);
630 unsigned hash = hash_64(idx, ilog2(ARRAY_SIZE(c->ec_stripes_new)));
633 hlist_add_head(&s->hash, &c->ec_stripes_new[hash]);
635 spin_unlock(&c->ec_stripes_new_lock);
640 static void bch2_stripe_close(struct bch_fs *c, struct ec_stripe_new *s)
644 spin_lock(&c->ec_stripes_new_lock);
645 hlist_del_init(&s->hash);
646 spin_unlock(&c->ec_stripes_new_lock);
651 /* Heap of all existing stripes, ordered by blocks_nonempty */
653 static u64 stripe_idx_to_delete(struct bch_fs *c)
655 ec_stripes_heap *h = &c->ec_stripes_heap;
657 lockdep_assert_held(&c->ec_stripes_heap_lock);
660 h->data[0].blocks_nonempty == 0 &&
661 !bch2_stripe_is_open(c, h->data[0].idx))
662 return h->data[0].idx;
667 static inline int ec_stripes_heap_cmp(ec_stripes_heap *h,
668 struct ec_stripe_heap_entry l,
669 struct ec_stripe_heap_entry r)
671 return ((l.blocks_nonempty > r.blocks_nonempty) -
672 (l.blocks_nonempty < r.blocks_nonempty));
675 static inline void ec_stripes_heap_set_backpointer(ec_stripes_heap *h,
678 struct bch_fs *c = container_of(h, struct bch_fs, ec_stripes_heap);
680 genradix_ptr(&c->stripes, h->data[i].idx)->heap_idx = i;
683 static void heap_verify_backpointer(struct bch_fs *c, size_t idx)
685 ec_stripes_heap *h = &c->ec_stripes_heap;
686 struct stripe *m = genradix_ptr(&c->stripes, idx);
688 BUG_ON(m->heap_idx >= h->used);
689 BUG_ON(h->data[m->heap_idx].idx != idx);
692 void bch2_stripes_heap_del(struct bch_fs *c,
693 struct stripe *m, size_t idx)
695 mutex_lock(&c->ec_stripes_heap_lock);
696 heap_verify_backpointer(c, idx);
698 heap_del(&c->ec_stripes_heap, m->heap_idx,
700 ec_stripes_heap_set_backpointer);
701 mutex_unlock(&c->ec_stripes_heap_lock);
704 void bch2_stripes_heap_insert(struct bch_fs *c,
705 struct stripe *m, size_t idx)
707 mutex_lock(&c->ec_stripes_heap_lock);
708 BUG_ON(heap_full(&c->ec_stripes_heap));
710 heap_add(&c->ec_stripes_heap, ((struct ec_stripe_heap_entry) {
712 .blocks_nonempty = m->blocks_nonempty,
715 ec_stripes_heap_set_backpointer);
717 heap_verify_backpointer(c, idx);
718 mutex_unlock(&c->ec_stripes_heap_lock);
721 void bch2_stripes_heap_update(struct bch_fs *c,
722 struct stripe *m, size_t idx)
724 ec_stripes_heap *h = &c->ec_stripes_heap;
728 mutex_lock(&c->ec_stripes_heap_lock);
729 heap_verify_backpointer(c, idx);
731 h->data[m->heap_idx].blocks_nonempty = m->blocks_nonempty;
734 heap_sift_up(h, i, ec_stripes_heap_cmp,
735 ec_stripes_heap_set_backpointer);
736 heap_sift_down(h, i, ec_stripes_heap_cmp,
737 ec_stripes_heap_set_backpointer);
739 heap_verify_backpointer(c, idx);
741 do_deletes = stripe_idx_to_delete(c) != 0;
742 mutex_unlock(&c->ec_stripes_heap_lock);
745 bch2_do_stripe_deletes(c);
748 /* stripe deletion */
750 static int ec_stripe_delete(struct btree_trans *trans, u64 idx)
752 struct bch_fs *c = trans->c;
753 struct btree_iter iter;
755 struct bkey_s_c_stripe s;
758 k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_stripes, POS(0, idx),
764 if (k.k->type != KEY_TYPE_stripe) {
765 bch2_fs_inconsistent(c, "attempting to delete nonexistent stripe %llu", idx);
770 s = bkey_s_c_to_stripe(k);
771 for (unsigned i = 0; i < s.v->nr_blocks; i++)
772 if (stripe_blockcount_get(s.v, i)) {
773 struct printbuf buf = PRINTBUF;
775 bch2_bkey_val_to_text(&buf, c, k);
776 bch2_fs_inconsistent(c, "attempting to delete nonempty stripe %s", buf.buf);
782 ret = bch2_btree_delete_at(trans, &iter, 0);
784 bch2_trans_iter_exit(trans, &iter);
788 static void ec_stripe_delete_work(struct work_struct *work)
791 container_of(work, struct bch_fs, ec_stripe_delete_work);
792 struct btree_trans *trans = bch2_trans_get(c);
797 mutex_lock(&c->ec_stripes_heap_lock);
798 idx = stripe_idx_to_delete(c);
799 mutex_unlock(&c->ec_stripes_heap_lock);
804 ret = commit_do(trans, NULL, NULL, BTREE_INSERT_NOFAIL,
805 ec_stripe_delete(trans, idx));
812 bch2_trans_put(trans);
814 bch2_write_ref_put(c, BCH_WRITE_REF_stripe_delete);
817 void bch2_do_stripe_deletes(struct bch_fs *c)
819 if (bch2_write_ref_tryget(c, BCH_WRITE_REF_stripe_delete) &&
820 !queue_work(c->write_ref_wq, &c->ec_stripe_delete_work))
821 bch2_write_ref_put(c, BCH_WRITE_REF_stripe_delete);
824 /* stripe creation: */
826 static int ec_stripe_key_update(struct btree_trans *trans,
827 struct bkey_i_stripe *new,
830 struct bch_fs *c = trans->c;
831 struct btree_iter iter;
835 k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_stripes,
836 new->k.p, BTREE_ITER_INTENT);
841 if (k.k->type != (create ? KEY_TYPE_deleted : KEY_TYPE_stripe)) {
842 bch2_fs_inconsistent(c, "error %s stripe: got existing key type %s",
843 create ? "creating" : "updating",
844 bch2_bkey_types[k.k->type]);
849 if (k.k->type == KEY_TYPE_stripe) {
850 const struct bch_stripe *old = bkey_s_c_to_stripe(k).v;
853 if (old->nr_blocks != new->v.nr_blocks) {
854 bch_err(c, "error updating stripe: nr_blocks does not match");
859 for (i = 0; i < new->v.nr_blocks; i++) {
860 unsigned v = stripe_blockcount_get(old, i);
863 (old->ptrs[i].dev != new->v.ptrs[i].dev ||
864 old->ptrs[i].gen != new->v.ptrs[i].gen ||
865 old->ptrs[i].offset != new->v.ptrs[i].offset));
867 stripe_blockcount_set(&new->v, i, v);
871 ret = bch2_trans_update(trans, &iter, &new->k_i, 0);
873 bch2_trans_iter_exit(trans, &iter);
877 static int ec_stripe_update_extent(struct btree_trans *trans,
878 struct bpos bucket, u8 gen,
879 struct ec_stripe_buf *s,
882 struct bch_stripe *v = &bkey_i_to_stripe(&s->key)->v;
883 struct bch_fs *c = trans->c;
884 struct bch_backpointer bp;
885 struct btree_iter iter;
887 const struct bch_extent_ptr *ptr_c;
888 struct bch_extent_ptr *ptr, *ec_ptr = NULL;
889 struct bch_extent_stripe_ptr stripe_ptr;
893 ret = bch2_get_next_backpointer(trans, bucket, gen,
894 bp_pos, &bp, BTREE_ITER_CACHED);
897 if (bpos_eq(*bp_pos, SPOS_MAX))
901 struct printbuf buf = PRINTBUF;
902 struct btree_iter node_iter;
905 b = bch2_backpointer_get_node(trans, &node_iter, *bp_pos, bp);
906 bch2_trans_iter_exit(trans, &node_iter);
911 prt_printf(&buf, "found btree node in erasure coded bucket: b=%px\n", b);
912 bch2_backpointer_to_text(&buf, &bp);
914 bch2_fs_inconsistent(c, "%s", buf.buf);
919 k = bch2_backpointer_get_key(trans, &iter, *bp_pos, bp, BTREE_ITER_INTENT);
925 * extent no longer exists - we could flush the btree
926 * write buffer and retry to verify, but no need:
931 if (extent_has_stripe_ptr(k, s->key.k.p.offset))
934 ptr_c = bkey_matches_stripe(v, k, &block);
936 * It doesn't generally make sense to erasure code cached ptrs:
937 * XXX: should we be incrementing a counter?
939 if (!ptr_c || ptr_c->cached)
942 dev = v->ptrs[block].dev;
944 n = bch2_trans_kmalloc(trans, bkey_bytes(k.k) + sizeof(stripe_ptr));
945 ret = PTR_ERR_OR_ZERO(n);
949 bkey_reassemble(n, k);
951 bch2_bkey_drop_ptrs(bkey_i_to_s(n), ptr, ptr->dev != dev);
952 ec_ptr = bch2_bkey_has_device(bkey_i_to_s(n), dev);
955 stripe_ptr = (struct bch_extent_stripe_ptr) {
956 .type = 1 << BCH_EXTENT_ENTRY_stripe_ptr,
958 .redundancy = v->nr_redundant,
959 .idx = s->key.k.p.offset,
962 __extent_entry_insert(n,
963 (union bch_extent_entry *) ec_ptr,
964 (union bch_extent_entry *) &stripe_ptr);
966 ret = bch2_trans_update(trans, &iter, n, 0);
968 bch2_trans_iter_exit(trans, &iter);
972 static int ec_stripe_update_bucket(struct btree_trans *trans, struct ec_stripe_buf *s,
975 struct bch_fs *c = trans->c;
976 struct bch_stripe *v = &bkey_i_to_stripe(&s->key)->v;
977 struct bch_extent_ptr bucket = v->ptrs[block];
978 struct bpos bucket_pos = PTR_BUCKET_POS(c, &bucket);
979 struct bpos bp_pos = POS_MIN;
983 ret = commit_do(trans, NULL, NULL,
984 BTREE_INSERT_NOCHECK_RW|
986 ec_stripe_update_extent(trans, bucket_pos, bucket.gen,
990 if (bkey_eq(bp_pos, POS_MAX))
993 bp_pos = bpos_nosnap_successor(bp_pos);
999 static int ec_stripe_update_extents(struct bch_fs *c, struct ec_stripe_buf *s)
1001 struct btree_trans *trans = bch2_trans_get(c);
1002 struct bch_stripe *v = &bkey_i_to_stripe(&s->key)->v;
1003 unsigned i, nr_data = v->nr_blocks - v->nr_redundant;
1006 ret = bch2_btree_write_buffer_flush(trans);
1010 for (i = 0; i < nr_data; i++) {
1011 ret = ec_stripe_update_bucket(trans, s, i);
1016 bch2_trans_put(trans);
1021 static void zero_out_rest_of_ec_bucket(struct bch_fs *c,
1022 struct ec_stripe_new *s,
1024 struct open_bucket *ob)
1026 struct bch_dev *ca = bch_dev_bkey_exists(c, ob->dev);
1027 unsigned offset = ca->mi.bucket_size - ob->sectors_free;
1030 if (!bch2_dev_get_ioref(ca, WRITE)) {
1031 s->err = -BCH_ERR_erofs_no_writes;
1035 memset(s->new_stripe.data[block] + (offset << 9),
1037 ob->sectors_free << 9);
1039 ret = blkdev_issue_zeroout(ca->disk_sb.bdev,
1040 ob->bucket * ca->mi.bucket_size + offset,
1044 percpu_ref_put(&ca->io_ref);
1050 void bch2_ec_stripe_new_free(struct bch_fs *c, struct ec_stripe_new *s)
1053 bch2_stripe_close(c, s);
1058 * data buckets of new stripe all written: create the stripe
1060 static void ec_stripe_create(struct ec_stripe_new *s)
1062 struct bch_fs *c = s->c;
1063 struct open_bucket *ob;
1064 struct bch_stripe *v = &bkey_i_to_stripe(&s->new_stripe.key)->v;
1065 unsigned i, nr_data = v->nr_blocks - v->nr_redundant;
1068 BUG_ON(s->h->s == s);
1070 closure_sync(&s->iodone);
1073 for (i = 0; i < nr_data; i++)
1075 ob = c->open_buckets + s->blocks[i];
1077 if (ob->sectors_free)
1078 zero_out_rest_of_ec_bucket(c, s, i, ob);
1083 if (!bch2_err_matches(s->err, EROFS))
1084 bch_err(c, "error creating stripe: error writing data buckets");
1088 if (s->have_existing_stripe) {
1089 ec_validate_checksums(c, &s->existing_stripe);
1091 if (ec_do_recov(c, &s->existing_stripe)) {
1092 bch_err(c, "error creating stripe: error reading existing stripe");
1096 for (i = 0; i < nr_data; i++)
1097 if (stripe_blockcount_get(&bkey_i_to_stripe(&s->existing_stripe.key)->v, i))
1098 swap(s->new_stripe.data[i],
1099 s->existing_stripe.data[i]);
1101 ec_stripe_buf_exit(&s->existing_stripe);
1104 BUG_ON(!s->allocated);
1107 ec_generate_ec(&s->new_stripe);
1109 ec_generate_checksums(&s->new_stripe);
1112 for (i = nr_data; i < v->nr_blocks; i++)
1113 ec_block_io(c, &s->new_stripe, REQ_OP_WRITE, i, &s->iodone);
1114 closure_sync(&s->iodone);
1116 if (ec_nr_failed(&s->new_stripe)) {
1117 bch_err(c, "error creating stripe: error writing redundancy buckets");
1121 ret = bch2_trans_do(c, &s->res, NULL,
1122 BTREE_INSERT_NOCHECK_RW|
1123 BTREE_INSERT_NOFAIL,
1124 ec_stripe_key_update(trans,
1125 bkey_i_to_stripe(&s->new_stripe.key),
1126 !s->have_existing_stripe));
1128 bch_err(c, "error creating stripe: error creating stripe key");
1132 ret = ec_stripe_update_extents(c, &s->new_stripe);
1134 bch_err_msg(c, ret, "creating stripe: error updating pointers");
1138 bch2_disk_reservation_put(c, &s->res);
1140 for (i = 0; i < v->nr_blocks; i++)
1142 ob = c->open_buckets + s->blocks[i];
1146 __bch2_open_bucket_put(c, ob);
1148 bch2_open_bucket_put(c, ob);
1152 mutex_lock(&c->ec_stripe_new_lock);
1154 mutex_unlock(&c->ec_stripe_new_lock);
1155 wake_up(&c->ec_stripe_new_wait);
1157 ec_stripe_buf_exit(&s->existing_stripe);
1158 ec_stripe_buf_exit(&s->new_stripe);
1159 closure_debug_destroy(&s->iodone);
1161 ec_stripe_new_put(c, s, STRIPE_REF_stripe);
1164 static struct ec_stripe_new *get_pending_stripe(struct bch_fs *c)
1166 struct ec_stripe_new *s;
1168 mutex_lock(&c->ec_stripe_new_lock);
1169 list_for_each_entry(s, &c->ec_stripe_new_list, list)
1170 if (!atomic_read(&s->ref[STRIPE_REF_io]))
1174 mutex_unlock(&c->ec_stripe_new_lock);
1179 static void ec_stripe_create_work(struct work_struct *work)
1181 struct bch_fs *c = container_of(work,
1182 struct bch_fs, ec_stripe_create_work);
1183 struct ec_stripe_new *s;
1185 while ((s = get_pending_stripe(c)))
1186 ec_stripe_create(s);
1188 bch2_write_ref_put(c, BCH_WRITE_REF_stripe_create);
1191 void bch2_ec_do_stripe_creates(struct bch_fs *c)
1193 bch2_write_ref_get(c, BCH_WRITE_REF_stripe_create);
1195 if (!queue_work(system_long_wq, &c->ec_stripe_create_work))
1196 bch2_write_ref_put(c, BCH_WRITE_REF_stripe_create);
1199 static void ec_stripe_set_pending(struct bch_fs *c, struct ec_stripe_head *h)
1201 struct ec_stripe_new *s = h->s;
1203 BUG_ON(!s->allocated && !s->err);
1208 mutex_lock(&c->ec_stripe_new_lock);
1209 list_add(&s->list, &c->ec_stripe_new_list);
1210 mutex_unlock(&c->ec_stripe_new_lock);
1212 ec_stripe_new_put(c, s, STRIPE_REF_io);
1215 void bch2_ec_bucket_cancel(struct bch_fs *c, struct open_bucket *ob)
1217 struct ec_stripe_new *s = ob->ec;
1222 void *bch2_writepoint_ec_buf(struct bch_fs *c, struct write_point *wp)
1224 struct open_bucket *ob = ec_open_bucket(c, &wp->ptrs);
1231 BUG_ON(!ob->ec->new_stripe.data[ob->ec_idx]);
1233 ca = bch_dev_bkey_exists(c, ob->dev);
1234 offset = ca->mi.bucket_size - ob->sectors_free;
1236 return ob->ec->new_stripe.data[ob->ec_idx] + (offset << 9);
1239 static int unsigned_cmp(const void *_l, const void *_r)
1241 unsigned l = *((const unsigned *) _l);
1242 unsigned r = *((const unsigned *) _r);
1244 return cmp_int(l, r);
1247 /* pick most common bucket size: */
1248 static unsigned pick_blocksize(struct bch_fs *c,
1249 struct bch_devs_mask *devs)
1252 unsigned i, nr = 0, sizes[BCH_SB_MEMBERS_MAX];
1255 } cur = { 0, 0 }, best = { 0, 0 };
1257 for_each_member_device_rcu(ca, c, i, devs)
1258 sizes[nr++] = ca->mi.bucket_size;
1260 sort(sizes, nr, sizeof(unsigned), unsigned_cmp, NULL);
1262 for (i = 0; i < nr; i++) {
1263 if (sizes[i] != cur.size) {
1264 if (cur.nr > best.nr)
1268 cur.size = sizes[i];
1274 if (cur.nr > best.nr)
1280 static bool may_create_new_stripe(struct bch_fs *c)
1285 static void ec_stripe_key_init(struct bch_fs *c,
1289 unsigned stripe_size)
1291 struct bkey_i_stripe *s = bkey_stripe_init(k);
1294 s->v.sectors = cpu_to_le16(stripe_size);
1296 s->v.nr_blocks = nr_data + nr_parity;
1297 s->v.nr_redundant = nr_parity;
1298 s->v.csum_granularity_bits = ilog2(c->opts.encoded_extent_max >> 9);
1299 s->v.csum_type = BCH_CSUM_crc32c;
1302 while ((u64s = stripe_val_u64s(&s->v)) > BKEY_VAL_U64s_MAX) {
1303 BUG_ON(1 << s->v.csum_granularity_bits >=
1304 le16_to_cpu(s->v.sectors) ||
1305 s->v.csum_granularity_bits == U8_MAX);
1306 s->v.csum_granularity_bits++;
1309 set_bkey_val_u64s(&s->k, u64s);
1312 static int ec_new_stripe_alloc(struct bch_fs *c, struct ec_stripe_head *h)
1314 struct ec_stripe_new *s;
1316 lockdep_assert_held(&h->lock);
1318 s = kzalloc(sizeof(*s), GFP_KERNEL);
1320 return -BCH_ERR_ENOMEM_ec_new_stripe_alloc;
1322 mutex_init(&s->lock);
1323 closure_init(&s->iodone, NULL);
1324 atomic_set(&s->ref[STRIPE_REF_stripe], 1);
1325 atomic_set(&s->ref[STRIPE_REF_io], 1);
1328 s->nr_data = min_t(unsigned, h->nr_active_devs,
1329 BCH_BKEY_PTRS_MAX) - h->redundancy;
1330 s->nr_parity = h->redundancy;
1332 ec_stripe_key_init(c, &s->new_stripe.key,
1333 s->nr_data, s->nr_parity, h->blocksize);
1339 static struct ec_stripe_head *
1340 ec_new_stripe_head_alloc(struct bch_fs *c, unsigned target,
1341 unsigned algo, unsigned redundancy,
1342 enum bch_watermark watermark)
1344 struct ec_stripe_head *h;
1348 h = kzalloc(sizeof(*h), GFP_KERNEL);
1352 mutex_init(&h->lock);
1353 BUG_ON(!mutex_trylock(&h->lock));
1357 h->redundancy = redundancy;
1358 h->watermark = watermark;
1361 h->devs = target_rw_devs(c, BCH_DATA_user, target);
1363 for_each_member_device_rcu(ca, c, i, &h->devs)
1364 if (!ca->mi.durability)
1365 __clear_bit(i, h->devs.d);
1367 h->blocksize = pick_blocksize(c, &h->devs);
1369 for_each_member_device_rcu(ca, c, i, &h->devs)
1370 if (ca->mi.bucket_size == h->blocksize)
1371 h->nr_active_devs++;
1374 list_add(&h->list, &c->ec_stripe_head_list);
1378 void bch2_ec_stripe_head_put(struct bch_fs *c, struct ec_stripe_head *h)
1382 bitmap_weight(h->s->blocks_allocated,
1383 h->s->nr_data) == h->s->nr_data)
1384 ec_stripe_set_pending(c, h);
1386 mutex_unlock(&h->lock);
1389 static struct ec_stripe_head *
1390 __bch2_ec_stripe_head_get(struct btree_trans *trans,
1393 unsigned redundancy,
1394 enum bch_watermark watermark)
1396 struct bch_fs *c = trans->c;
1397 struct ec_stripe_head *h;
1403 ret = bch2_trans_mutex_lock(trans, &c->ec_stripe_head_lock);
1405 return ERR_PTR(ret);
1407 if (test_bit(BCH_FS_GOING_RO, &c->flags)) {
1408 h = ERR_PTR(-BCH_ERR_erofs_no_writes);
1412 list_for_each_entry(h, &c->ec_stripe_head_list, list)
1413 if (h->target == target &&
1415 h->redundancy == redundancy &&
1416 h->watermark == watermark) {
1417 ret = bch2_trans_mutex_lock(trans, &h->lock);
1423 h = ec_new_stripe_head_alloc(c, target, algo, redundancy, watermark);
1425 mutex_unlock(&c->ec_stripe_head_lock);
1429 static int new_stripe_alloc_buckets(struct btree_trans *trans, struct ec_stripe_head *h,
1430 enum bch_watermark watermark, struct closure *cl)
1432 struct bch_fs *c = trans->c;
1433 struct bch_devs_mask devs = h->devs;
1434 struct open_bucket *ob;
1435 struct open_buckets buckets;
1436 struct bch_stripe *v = &bkey_i_to_stripe(&h->s->new_stripe.key)->v;
1437 unsigned i, j, nr_have_parity = 0, nr_have_data = 0;
1438 bool have_cache = true;
1441 BUG_ON(v->nr_blocks != h->s->nr_data + h->s->nr_parity);
1442 BUG_ON(v->nr_redundant != h->s->nr_parity);
1444 for_each_set_bit(i, h->s->blocks_gotten, v->nr_blocks) {
1445 __clear_bit(v->ptrs[i].dev, devs.d);
1446 if (i < h->s->nr_data)
1452 BUG_ON(nr_have_data > h->s->nr_data);
1453 BUG_ON(nr_have_parity > h->s->nr_parity);
1456 if (nr_have_parity < h->s->nr_parity) {
1457 ret = bch2_bucket_alloc_set_trans(trans, &buckets,
1467 open_bucket_for_each(c, &buckets, ob, i) {
1468 j = find_next_zero_bit(h->s->blocks_gotten,
1469 h->s->nr_data + h->s->nr_parity,
1471 BUG_ON(j >= h->s->nr_data + h->s->nr_parity);
1473 h->s->blocks[j] = buckets.v[i];
1474 v->ptrs[j] = bch2_ob_ptr(c, ob);
1475 __set_bit(j, h->s->blocks_gotten);
1483 if (nr_have_data < h->s->nr_data) {
1484 ret = bch2_bucket_alloc_set_trans(trans, &buckets,
1494 open_bucket_for_each(c, &buckets, ob, i) {
1495 j = find_next_zero_bit(h->s->blocks_gotten,
1497 BUG_ON(j >= h->s->nr_data);
1499 h->s->blocks[j] = buckets.v[i];
1500 v->ptrs[j] = bch2_ob_ptr(c, ob);
1501 __set_bit(j, h->s->blocks_gotten);
1511 /* XXX: doesn't obey target: */
1512 static s64 get_existing_stripe(struct bch_fs *c,
1513 struct ec_stripe_head *head)
1515 ec_stripes_heap *h = &c->ec_stripes_heap;
1521 if (may_create_new_stripe(c))
1524 mutex_lock(&c->ec_stripes_heap_lock);
1525 for (heap_idx = 0; heap_idx < h->used; heap_idx++) {
1526 /* No blocks worth reusing, stripe will just be deleted: */
1527 if (!h->data[heap_idx].blocks_nonempty)
1530 stripe_idx = h->data[heap_idx].idx;
1532 m = genradix_ptr(&c->stripes, stripe_idx);
1534 if (m->algorithm == head->algo &&
1535 m->nr_redundant == head->redundancy &&
1536 m->sectors == head->blocksize &&
1537 m->blocks_nonempty < m->nr_blocks - m->nr_redundant &&
1538 bch2_try_open_stripe(c, head->s, stripe_idx)) {
1543 mutex_unlock(&c->ec_stripes_heap_lock);
1547 static int __bch2_ec_stripe_head_reuse(struct btree_trans *trans, struct ec_stripe_head *h)
1549 struct bch_fs *c = trans->c;
1550 struct bch_stripe *new_v = &bkey_i_to_stripe(&h->s->new_stripe.key)->v;
1551 struct bch_stripe *existing_v;
1557 * If we can't allocate a new stripe, and there's no stripes with empty
1558 * blocks for us to reuse, that means we have to wait on copygc:
1560 idx = get_existing_stripe(c, h);
1562 return -BCH_ERR_stripe_alloc_blocked;
1564 ret = get_stripe_key_trans(trans, idx, &h->s->existing_stripe);
1566 bch2_stripe_close(c, h->s);
1567 if (!bch2_err_matches(ret, BCH_ERR_transaction_restart))
1568 bch2_fs_fatal_error(c, "error reading stripe key: %s", bch2_err_str(ret));
1572 existing_v = &bkey_i_to_stripe(&h->s->existing_stripe.key)->v;
1574 BUG_ON(existing_v->nr_redundant != h->s->nr_parity);
1575 h->s->nr_data = existing_v->nr_blocks -
1576 existing_v->nr_redundant;
1578 ret = ec_stripe_buf_init(&h->s->existing_stripe, 0, h->blocksize);
1580 bch2_stripe_close(c, h->s);
1584 BUG_ON(h->s->existing_stripe.size != h->blocksize);
1585 BUG_ON(h->s->existing_stripe.size != le16_to_cpu(existing_v->sectors));
1588 * Free buckets we initially allocated - they might conflict with
1589 * blocks from the stripe we're reusing:
1591 for_each_set_bit(i, h->s->blocks_gotten, new_v->nr_blocks) {
1592 bch2_open_bucket_put(c, c->open_buckets + h->s->blocks[i]);
1593 h->s->blocks[i] = 0;
1595 memset(h->s->blocks_gotten, 0, sizeof(h->s->blocks_gotten));
1596 memset(h->s->blocks_allocated, 0, sizeof(h->s->blocks_allocated));
1598 for (i = 0; i < existing_v->nr_blocks; i++) {
1599 if (stripe_blockcount_get(existing_v, i)) {
1600 __set_bit(i, h->s->blocks_gotten);
1601 __set_bit(i, h->s->blocks_allocated);
1604 ec_block_io(c, &h->s->existing_stripe, READ, i, &h->s->iodone);
1607 bkey_copy(&h->s->new_stripe.key, &h->s->existing_stripe.key);
1608 h->s->have_existing_stripe = true;
1613 static int __bch2_ec_stripe_head_reserve(struct btree_trans *trans, struct ec_stripe_head *h)
1615 struct bch_fs *c = trans->c;
1616 struct btree_iter iter;
1618 struct bpos min_pos = POS(0, 1);
1619 struct bpos start_pos = bpos_max(min_pos, POS(0, c->ec_stripe_hint));
1622 if (!h->s->res.sectors) {
1623 ret = bch2_disk_reservation_get(c, &h->s->res,
1626 BCH_DISK_RESERVATION_NOFAIL);
1631 for_each_btree_key_norestart(trans, iter, BTREE_ID_stripes, start_pos,
1632 BTREE_ITER_SLOTS|BTREE_ITER_INTENT, k, ret) {
1633 if (bkey_gt(k.k->p, POS(0, U32_MAX))) {
1634 if (start_pos.offset) {
1635 start_pos = min_pos;
1636 bch2_btree_iter_set_pos(&iter, start_pos);
1640 ret = -BCH_ERR_ENOSPC_stripe_create;
1644 if (bkey_deleted(k.k) &&
1645 bch2_try_open_stripe(c, h->s, k.k->p.offset))
1649 c->ec_stripe_hint = iter.pos.offset;
1654 ret = ec_stripe_mem_alloc(trans, &iter);
1656 bch2_stripe_close(c, h->s);
1660 h->s->new_stripe.key.k.p = iter.pos;
1662 bch2_trans_iter_exit(trans, &iter);
1665 bch2_disk_reservation_put(c, &h->s->res);
1669 struct ec_stripe_head *bch2_ec_stripe_head_get(struct btree_trans *trans,
1672 unsigned redundancy,
1673 enum bch_watermark watermark,
1676 struct bch_fs *c = trans->c;
1677 struct ec_stripe_head *h;
1678 bool waiting = false;
1681 h = __bch2_ec_stripe_head_get(trans, target, algo, redundancy, watermark);
1683 bch_err(c, "no stripe head");
1684 if (IS_ERR_OR_NULL(h))
1688 ret = ec_new_stripe_alloc(c, h);
1690 bch_err(c, "failed to allocate new stripe");
1695 if (h->s->allocated)
1698 if (h->s->have_existing_stripe)
1699 goto alloc_existing;
1701 /* First, try to allocate a full stripe: */
1702 ret = new_stripe_alloc_buckets(trans, h, BCH_WATERMARK_stripe, NULL) ?:
1703 __bch2_ec_stripe_head_reserve(trans, h);
1706 if (bch2_err_matches(ret, BCH_ERR_transaction_restart) ||
1707 bch2_err_matches(ret, ENOMEM))
1711 * Not enough buckets available for a full stripe: we must reuse an
1715 ret = __bch2_ec_stripe_head_reuse(trans, h);
1718 if (waiting || !cl || ret != -BCH_ERR_stripe_alloc_blocked)
1721 if (watermark == BCH_WATERMARK_copygc) {
1722 ret = new_stripe_alloc_buckets(trans, h, watermark, NULL) ?:
1723 __bch2_ec_stripe_head_reserve(trans, h);
1729 /* XXX freelist_wait? */
1730 closure_wait(&c->freelist_wait, cl);
1735 closure_wake_up(&c->freelist_wait);
1738 * Retry allocating buckets, with the watermark for this
1741 ret = new_stripe_alloc_buckets(trans, h, watermark, cl);
1746 ret = ec_stripe_buf_init(&h->s->new_stripe, 0, h->blocksize);
1750 h->s->allocated = true;
1753 BUG_ON(!h->s->new_stripe.data[0]);
1754 BUG_ON(trans->restarted);
1757 bch2_ec_stripe_head_put(c, h);
1758 return ERR_PTR(ret);
1761 static void __bch2_ec_stop(struct bch_fs *c, struct bch_dev *ca)
1763 struct ec_stripe_head *h;
1764 struct open_bucket *ob;
1767 mutex_lock(&c->ec_stripe_head_lock);
1768 list_for_each_entry(h, &c->ec_stripe_head_list, list) {
1769 mutex_lock(&h->lock);
1776 for (i = 0; i < bkey_i_to_stripe(&h->s->new_stripe.key)->v.nr_blocks; i++) {
1777 if (!h->s->blocks[i])
1780 ob = c->open_buckets + h->s->blocks[i];
1781 if (ob->dev == ca->dev_idx)
1786 h->s->err = -BCH_ERR_erofs_no_writes;
1787 ec_stripe_set_pending(c, h);
1789 mutex_unlock(&h->lock);
1791 mutex_unlock(&c->ec_stripe_head_lock);
1794 void bch2_ec_stop_dev(struct bch_fs *c, struct bch_dev *ca)
1796 __bch2_ec_stop(c, ca);
1799 void bch2_fs_ec_stop(struct bch_fs *c)
1801 __bch2_ec_stop(c, NULL);
1804 static bool bch2_fs_ec_flush_done(struct bch_fs *c)
1808 mutex_lock(&c->ec_stripe_new_lock);
1809 ret = list_empty(&c->ec_stripe_new_list);
1810 mutex_unlock(&c->ec_stripe_new_lock);
1815 void bch2_fs_ec_flush(struct bch_fs *c)
1817 wait_event(c->ec_stripe_new_wait, bch2_fs_ec_flush_done(c));
1820 int bch2_stripes_read(struct bch_fs *c)
1822 struct btree_trans *trans = bch2_trans_get(c);
1823 struct btree_iter iter;
1825 const struct bch_stripe *s;
1830 for_each_btree_key(trans, iter, BTREE_ID_stripes, POS_MIN,
1831 BTREE_ITER_PREFETCH, k, ret) {
1832 if (k.k->type != KEY_TYPE_stripe)
1835 ret = __ec_stripe_mem_alloc(c, k.k->p.offset, GFP_KERNEL);
1839 s = bkey_s_c_to_stripe(k).v;
1841 m = genradix_ptr(&c->stripes, k.k->p.offset);
1842 m->sectors = le16_to_cpu(s->sectors);
1843 m->algorithm = s->algorithm;
1844 m->nr_blocks = s->nr_blocks;
1845 m->nr_redundant = s->nr_redundant;
1846 m->blocks_nonempty = 0;
1848 for (i = 0; i < s->nr_blocks; i++)
1849 m->blocks_nonempty += !!stripe_blockcount_get(s, i);
1851 bch2_stripes_heap_insert(c, m, k.k->p.offset);
1853 bch2_trans_iter_exit(trans, &iter);
1855 bch2_trans_put(trans);
1863 void bch2_stripes_heap_to_text(struct printbuf *out, struct bch_fs *c)
1865 ec_stripes_heap *h = &c->ec_stripes_heap;
1869 mutex_lock(&c->ec_stripes_heap_lock);
1870 for (i = 0; i < min_t(size_t, h->used, 50); i++) {
1871 m = genradix_ptr(&c->stripes, h->data[i].idx);
1873 prt_printf(out, "%zu %u/%u+%u", h->data[i].idx,
1874 h->data[i].blocks_nonempty,
1875 m->nr_blocks - m->nr_redundant,
1877 if (bch2_stripe_is_open(c, h->data[i].idx))
1878 prt_str(out, " open");
1881 mutex_unlock(&c->ec_stripes_heap_lock);
1884 void bch2_new_stripes_to_text(struct printbuf *out, struct bch_fs *c)
1886 struct ec_stripe_head *h;
1887 struct ec_stripe_new *s;
1889 mutex_lock(&c->ec_stripe_head_lock);
1890 list_for_each_entry(h, &c->ec_stripe_head_list, list) {
1891 prt_printf(out, "target %u algo %u redundancy %u %s:\n",
1892 h->target, h->algo, h->redundancy,
1893 bch2_watermarks[h->watermark]);
1896 prt_printf(out, "\tidx %llu blocks %u+%u allocated %u\n",
1897 h->s->idx, h->s->nr_data, h->s->nr_parity,
1898 bitmap_weight(h->s->blocks_allocated,
1901 mutex_unlock(&c->ec_stripe_head_lock);
1903 prt_printf(out, "in flight:\n");
1905 mutex_lock(&c->ec_stripe_new_lock);
1906 list_for_each_entry(s, &c->ec_stripe_new_list, list) {
1907 prt_printf(out, "\tidx %llu blocks %u+%u ref %u %u %s\n",
1908 s->idx, s->nr_data, s->nr_parity,
1909 atomic_read(&s->ref[STRIPE_REF_io]),
1910 atomic_read(&s->ref[STRIPE_REF_stripe]),
1911 bch2_watermarks[s->h->watermark]);
1913 mutex_unlock(&c->ec_stripe_new_lock);
1916 void bch2_fs_ec_exit(struct bch_fs *c)
1918 struct ec_stripe_head *h;
1922 mutex_lock(&c->ec_stripe_head_lock);
1923 h = list_first_entry_or_null(&c->ec_stripe_head_list,
1924 struct ec_stripe_head, list);
1927 mutex_unlock(&c->ec_stripe_head_lock);
1932 for (i = 0; i < bkey_i_to_stripe(&h->s->new_stripe.key)->v.nr_blocks; i++)
1933 BUG_ON(h->s->blocks[i]);
1940 BUG_ON(!list_empty(&c->ec_stripe_new_list));
1942 free_heap(&c->ec_stripes_heap);
1943 genradix_free(&c->stripes);
1944 bioset_exit(&c->ec_bioset);
1947 void bch2_fs_ec_init_early(struct bch_fs *c)
1949 spin_lock_init(&c->ec_stripes_new_lock);
1950 mutex_init(&c->ec_stripes_heap_lock);
1952 INIT_LIST_HEAD(&c->ec_stripe_head_list);
1953 mutex_init(&c->ec_stripe_head_lock);
1955 INIT_LIST_HEAD(&c->ec_stripe_new_list);
1956 mutex_init(&c->ec_stripe_new_lock);
1957 init_waitqueue_head(&c->ec_stripe_new_wait);
1959 INIT_WORK(&c->ec_stripe_create_work, ec_stripe_create_work);
1960 INIT_WORK(&c->ec_stripe_delete_work, ec_stripe_delete_work);
1963 int bch2_fs_ec_init(struct bch_fs *c)
1965 return bioset_init(&c->ec_bioset, 1, offsetof(struct ec_bio, bio),