1 // SPDX-License-Identifier: GPL-2.0
3 * Some low level IO code, and hacks for various block layer limitations
6 * Copyright 2012 Google, Inc.
10 #include "alloc_background.h"
11 #include "alloc_foreground.h"
12 #include "btree_update.h"
17 #include "data_update.h"
18 #include "disk_groups.h"
24 #include "subvolume.h"
27 #include <linux/sched/mm.h>
29 #ifndef CONFIG_BCACHEFS_NO_LATENCY_ACCT
31 static bool bch2_target_congested(struct bch_fs *c, u16 target)
33 const struct bch_devs_mask *devs;
34 unsigned d, nr = 0, total = 0;
35 u64 now = local_clock(), last;
43 devs = bch2_target_to_mask(c, target) ?:
44 &c->rw_devs[BCH_DATA_user];
46 for_each_set_bit(d, devs->d, BCH_SB_MEMBERS_MAX) {
47 ca = rcu_dereference(c->devs[d]);
51 congested = atomic_read(&ca->congested);
52 last = READ_ONCE(ca->congested_last);
53 if (time_after64(now, last))
54 congested -= (now - last) >> 12;
56 total += max(congested, 0LL);
61 return bch2_rand_range(nr * CONGESTED_MAX) < total;
66 static bool bch2_target_congested(struct bch_fs *c, u16 target)
73 /* Cache promotion on read */
79 struct rhash_head hash;
82 struct data_update write;
83 struct bio_vec bi_inline_vecs[]; /* must be last */
86 static const struct rhashtable_params bch_promote_params = {
87 .head_offset = offsetof(struct promote_op, hash),
88 .key_offset = offsetof(struct promote_op, pos),
89 .key_len = sizeof(struct bpos),
92 static inline int should_promote(struct bch_fs *c, struct bkey_s_c k,
94 struct bch_io_opts opts,
97 BUG_ON(!opts.promote_target);
99 if (!(flags & BCH_READ_MAY_PROMOTE))
100 return -BCH_ERR_nopromote_may_not;
102 if (bch2_bkey_has_target(c, k, opts.promote_target))
103 return -BCH_ERR_nopromote_already_promoted;
105 if (bkey_extent_is_unwritten(k))
106 return -BCH_ERR_nopromote_unwritten;
108 if (bch2_target_congested(c, opts.promote_target))
109 return -BCH_ERR_nopromote_congested;
111 if (rhashtable_lookup_fast(&c->promote_table, &pos,
113 return -BCH_ERR_nopromote_in_flight;
118 static void promote_free(struct bch_fs *c, struct promote_op *op)
122 bch2_data_update_exit(&op->write);
124 ret = rhashtable_remove_fast(&c->promote_table, &op->hash,
127 bch2_write_ref_put(c, BCH_WRITE_REF_promote);
131 static void promote_done(struct bch_write_op *wop)
133 struct promote_op *op =
134 container_of(wop, struct promote_op, write.op);
135 struct bch_fs *c = op->write.op.c;
137 bch2_time_stats_update(&c->times[BCH_TIME_data_promote],
142 static void promote_start(struct promote_op *op, struct bch_read_bio *rbio)
144 struct bio *bio = &op->write.op.wbio.bio;
146 trace_and_count(op->write.op.c, read_promote, &rbio->bio);
148 /* we now own pages: */
149 BUG_ON(!rbio->bounce);
150 BUG_ON(rbio->bio.bi_vcnt > bio->bi_max_vecs);
152 memcpy(bio->bi_io_vec, rbio->bio.bi_io_vec,
153 sizeof(struct bio_vec) * rbio->bio.bi_vcnt);
154 swap(bio->bi_vcnt, rbio->bio.bi_vcnt);
156 bch2_data_update_read_done(&op->write, rbio->pick.crc);
159 static struct promote_op *__promote_alloc(struct btree_trans *trans,
160 enum btree_id btree_id,
163 struct extent_ptr_decoded *pick,
164 struct bch_io_opts opts,
166 struct bch_read_bio **rbio)
168 struct bch_fs *c = trans->c;
169 struct promote_op *op = NULL;
171 unsigned pages = DIV_ROUND_UP(sectors, PAGE_SECTORS);
174 if (!bch2_write_ref_tryget(c, BCH_WRITE_REF_promote))
175 return ERR_PTR(-BCH_ERR_nopromote_no_writes);
177 op = kzalloc(struct_size(op, bi_inline_vecs, pages), GFP_KERNEL);
179 ret = -BCH_ERR_nopromote_enomem;
183 op->start_time = local_clock();
187 * We don't use the mempool here because extents that aren't
188 * checksummed or compressed can be too big for the mempool:
190 *rbio = kzalloc(sizeof(struct bch_read_bio) +
191 sizeof(struct bio_vec) * pages,
194 ret = -BCH_ERR_nopromote_enomem;
198 rbio_init(&(*rbio)->bio, opts);
199 bio_init(&(*rbio)->bio, NULL, (*rbio)->bio.bi_inline_vecs, pages, 0);
201 if (bch2_bio_alloc_pages(&(*rbio)->bio, sectors << 9, GFP_KERNEL)) {
202 ret = -BCH_ERR_nopromote_enomem;
206 (*rbio)->bounce = true;
207 (*rbio)->split = true;
208 (*rbio)->kmalloc = true;
210 if (rhashtable_lookup_insert_fast(&c->promote_table, &op->hash,
211 bch_promote_params)) {
212 ret = -BCH_ERR_nopromote_in_flight;
216 bio = &op->write.op.wbio.bio;
217 bio_init(bio, NULL, bio->bi_inline_vecs, pages, 0);
219 ret = bch2_data_update_init(trans, NULL, NULL, &op->write,
220 writepoint_hashed((unsigned long) current),
222 (struct data_update_opts) {
223 .target = opts.promote_target,
225 .write_flags = BCH_WRITE_ALLOC_NOWAIT|BCH_WRITE_CACHED,
229 * possible errors: -BCH_ERR_nocow_lock_blocked,
230 * -BCH_ERR_ENOSPC_disk_reservation:
233 BUG_ON(rhashtable_remove_fast(&c->promote_table, &op->hash,
234 bch_promote_params));
238 op->write.op.end_io = promote_done;
243 bio_free_pages(&(*rbio)->bio);
247 bch2_write_ref_put(c, BCH_WRITE_REF_promote);
252 static struct promote_op *promote_alloc(struct btree_trans *trans,
253 struct bvec_iter iter,
255 struct extent_ptr_decoded *pick,
256 struct bch_io_opts opts,
258 struct bch_read_bio **rbio,
262 struct bch_fs *c = trans->c;
263 bool promote_full = *read_full || READ_ONCE(c->promote_whole_extents);
264 /* data might have to be decompressed in the write path: */
265 unsigned sectors = promote_full
266 ? max(pick->crc.compressed_size, pick->crc.live_size)
267 : bvec_iter_sectors(iter);
268 struct bpos pos = promote_full
269 ? bkey_start_pos(k.k)
270 : POS(k.k->p.inode, iter.bi_sector);
271 struct promote_op *promote;
274 ret = should_promote(c, k, pos, opts, flags);
278 promote = __promote_alloc(trans,
279 k.k->type == KEY_TYPE_reflink_v
282 k, pos, pick, opts, sectors, rbio);
283 ret = PTR_ERR_OR_ZERO(promote);
288 *read_full = promote_full;
291 trace_read_nopromote(c, ret);
297 #define READ_RETRY_AVOID 1
303 RBIO_CONTEXT_HIGHPRI,
304 RBIO_CONTEXT_UNBOUND,
307 static inline struct bch_read_bio *
308 bch2_rbio_parent(struct bch_read_bio *rbio)
310 return rbio->split ? rbio->parent : rbio;
314 static void bch2_rbio_punt(struct bch_read_bio *rbio, work_func_t fn,
315 enum rbio_context context,
316 struct workqueue_struct *wq)
318 if (context <= rbio->context) {
321 rbio->work.func = fn;
322 rbio->context = context;
323 queue_work(wq, &rbio->work);
327 static inline struct bch_read_bio *bch2_rbio_free(struct bch_read_bio *rbio)
329 BUG_ON(rbio->bounce && !rbio->split);
332 promote_free(rbio->c, rbio->promote);
333 rbio->promote = NULL;
336 bch2_bio_free_pages_pool(rbio->c, &rbio->bio);
339 struct bch_read_bio *parent = rbio->parent;
353 * Only called on a top level bch_read_bio to complete an entire read request,
356 static void bch2_rbio_done(struct bch_read_bio *rbio)
358 if (rbio->start_time)
359 bch2_time_stats_update(&rbio->c->times[BCH_TIME_data_read],
361 bio_endio(&rbio->bio);
364 static void bch2_read_retry_nodecode(struct bch_fs *c, struct bch_read_bio *rbio,
365 struct bvec_iter bvec_iter,
366 struct bch_io_failures *failed,
369 struct btree_trans *trans = bch2_trans_get(c);
370 struct btree_iter iter;
375 flags &= ~BCH_READ_LAST_FRAGMENT;
376 flags |= BCH_READ_MUST_CLONE;
378 bch2_bkey_buf_init(&sk);
380 bch2_trans_iter_init(trans, &iter, rbio->data_btree,
381 rbio->read_pos, BTREE_ITER_slots);
383 rbio->bio.bi_status = 0;
385 k = bch2_btree_iter_peek_slot(&iter);
389 bch2_bkey_buf_reassemble(&sk, c, k);
390 k = bkey_i_to_s_c(sk.k);
391 bch2_trans_unlock(trans);
393 if (!bch2_bkey_matches_ptr(c, k,
395 rbio->data_pos.offset -
396 rbio->pick.crc.offset)) {
397 /* extent we wanted to read no longer exists: */
402 ret = __bch2_read_extent(trans, rbio, bvec_iter,
405 k, 0, failed, flags);
406 if (ret == READ_RETRY)
411 bch2_rbio_done(rbio);
412 bch2_trans_iter_exit(trans, &iter);
413 bch2_trans_put(trans);
414 bch2_bkey_buf_exit(&sk, c);
417 rbio->bio.bi_status = BLK_STS_IOERR;
421 static void bch2_rbio_retry(struct work_struct *work)
423 struct bch_read_bio *rbio =
424 container_of(work, struct bch_read_bio, work);
425 struct bch_fs *c = rbio->c;
426 struct bvec_iter iter = rbio->bvec_iter;
427 unsigned flags = rbio->flags;
429 .subvol = rbio->subvol,
430 .inum = rbio->read_pos.inode,
432 struct bch_io_failures failed = { .nr = 0 };
434 trace_and_count(c, read_retry, &rbio->bio);
436 if (rbio->retry == READ_RETRY_AVOID)
437 bch2_mark_io_failure(&failed, &rbio->pick);
439 rbio->bio.bi_status = 0;
441 rbio = bch2_rbio_free(rbio);
443 flags |= BCH_READ_IN_RETRY;
444 flags &= ~BCH_READ_MAY_PROMOTE;
446 if (flags & BCH_READ_NODECODE) {
447 bch2_read_retry_nodecode(c, rbio, iter, &failed, flags);
449 flags &= ~BCH_READ_LAST_FRAGMENT;
450 flags |= BCH_READ_MUST_CLONE;
452 __bch2_read(c, rbio, iter, inum, &failed, flags);
456 static void bch2_rbio_error(struct bch_read_bio *rbio, int retry,
461 if (rbio->flags & BCH_READ_IN_RETRY)
464 if (retry == READ_ERR) {
465 rbio = bch2_rbio_free(rbio);
467 rbio->bio.bi_status = error;
468 bch2_rbio_done(rbio);
470 bch2_rbio_punt(rbio, bch2_rbio_retry,
471 RBIO_CONTEXT_UNBOUND, system_unbound_wq);
475 static int __bch2_rbio_narrow_crcs(struct btree_trans *trans,
476 struct bch_read_bio *rbio)
478 struct bch_fs *c = rbio->c;
479 u64 data_offset = rbio->data_pos.offset - rbio->pick.crc.offset;
480 struct bch_extent_crc_unpacked new_crc;
481 struct btree_iter iter;
486 if (crc_is_compressed(rbio->pick.crc))
489 k = bch2_bkey_get_iter(trans, &iter, rbio->data_btree, rbio->data_pos,
490 BTREE_ITER_slots|BTREE_ITER_intent);
491 if ((ret = bkey_err(k)))
494 if (bversion_cmp(k.k->version, rbio->version) ||
495 !bch2_bkey_matches_ptr(c, k, rbio->pick.ptr, data_offset))
498 /* Extent was merged? */
499 if (bkey_start_offset(k.k) < data_offset ||
500 k.k->p.offset > data_offset + rbio->pick.crc.uncompressed_size)
503 if (bch2_rechecksum_bio(c, &rbio->bio, rbio->version,
504 rbio->pick.crc, NULL, &new_crc,
505 bkey_start_offset(k.k) - data_offset, k.k->size,
506 rbio->pick.crc.csum_type)) {
507 bch_err(c, "error verifying existing checksum while narrowing checksum (memory corruption?)");
513 * going to be temporarily appending another checksum entry:
515 new = bch2_trans_kmalloc(trans, bkey_bytes(k.k) +
516 sizeof(struct bch_extent_crc128));
517 if ((ret = PTR_ERR_OR_ZERO(new)))
520 bkey_reassemble(new, k);
522 if (!bch2_bkey_narrow_crcs(new, new_crc))
525 ret = bch2_trans_update(trans, &iter, new,
526 BTREE_UPDATE_internal_snapshot_node);
528 bch2_trans_iter_exit(trans, &iter);
532 static noinline void bch2_rbio_narrow_crcs(struct bch_read_bio *rbio)
534 bch2_trans_do(rbio->c, NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
535 __bch2_rbio_narrow_crcs(trans, rbio));
538 /* Inner part that may run in process context */
539 static void __bch2_read_endio(struct work_struct *work)
541 struct bch_read_bio *rbio =
542 container_of(work, struct bch_read_bio, work);
543 struct bch_fs *c = rbio->c;
544 struct bio *src = &rbio->bio;
545 struct bio *dst = &bch2_rbio_parent(rbio)->bio;
546 struct bvec_iter dst_iter = rbio->bvec_iter;
547 struct bch_extent_crc_unpacked crc = rbio->pick.crc;
548 struct nonce nonce = extent_nonce(rbio->version, crc);
550 struct bch_csum csum;
553 nofs_flags = memalloc_nofs_save();
555 /* Reset iterator for checksumming and copying bounced data: */
557 src->bi_iter.bi_size = crc.compressed_size << 9;
558 src->bi_iter.bi_idx = 0;
559 src->bi_iter.bi_bvec_done = 0;
561 src->bi_iter = rbio->bvec_iter;
564 csum = bch2_checksum_bio(c, crc.csum_type, nonce, src);
565 if (bch2_crc_cmp(csum, rbio->pick.crc.csum) && !c->opts.no_data_io)
570 * We need to rework the narrow_crcs path to deliver the read completion
571 * first, and then punt to a different workqueue, otherwise we're
572 * holding up reads while doing btree updates which is bad for memory
575 if (unlikely(rbio->narrow_crcs))
576 bch2_rbio_narrow_crcs(rbio);
578 if (rbio->flags & BCH_READ_NODECODE)
581 /* Adjust crc to point to subset of data we want: */
582 crc.offset += rbio->offset_into_extent;
583 crc.live_size = bvec_iter_sectors(rbio->bvec_iter);
585 if (crc_is_compressed(crc)) {
586 ret = bch2_encrypt_bio(c, crc.csum_type, nonce, src);
590 if (bch2_bio_uncompress(c, src, dst, dst_iter, crc) &&
592 goto decompression_err;
594 /* don't need to decrypt the entire bio: */
595 nonce = nonce_add(nonce, crc.offset << 9);
596 bio_advance(src, crc.offset << 9);
598 BUG_ON(src->bi_iter.bi_size < dst_iter.bi_size);
599 src->bi_iter.bi_size = dst_iter.bi_size;
601 ret = bch2_encrypt_bio(c, crc.csum_type, nonce, src);
606 struct bvec_iter src_iter = src->bi_iter;
608 bio_copy_data_iter(dst, &dst_iter, src, &src_iter);
614 * Re encrypt data we decrypted, so it's consistent with
617 ret = bch2_encrypt_bio(c, crc.csum_type, nonce, src);
621 promote_start(rbio->promote, rbio);
622 rbio->promote = NULL;
625 if (likely(!(rbio->flags & BCH_READ_IN_RETRY))) {
626 rbio = bch2_rbio_free(rbio);
627 bch2_rbio_done(rbio);
630 memalloc_nofs_restore(nofs_flags);
634 * Checksum error: if the bio wasn't bounced, we may have been
635 * reading into buffers owned by userspace (that userspace can
636 * scribble over) - retry the read, bouncing it this time:
638 if (!rbio->bounce && (rbio->flags & BCH_READ_USER_MAPPED)) {
639 rbio->flags |= BCH_READ_MUST_BOUNCE;
640 bch2_rbio_error(rbio, READ_RETRY, BLK_STS_IOERR);
644 struct printbuf buf = PRINTBUF;
646 prt_str(&buf, "data ");
647 bch2_csum_err_msg(&buf, crc.csum_type, rbio->pick.crc.csum, csum);
649 struct bch_dev *ca = rbio->have_ioref ? bch2_dev_have_ref(c, rbio->pick.ptr.dev) : NULL;
651 bch_err_inum_offset_ratelimited(ca,
652 rbio->read_pos.inode,
653 rbio->read_pos.offset << 9,
655 bch2_io_error(ca, BCH_MEMBER_ERROR_checksum);
658 bch2_rbio_error(rbio, READ_RETRY_AVOID, BLK_STS_IOERR);
661 bch_err_inum_offset_ratelimited(c, rbio->read_pos.inode,
662 rbio->read_pos.offset << 9,
663 "decompression error");
664 bch2_rbio_error(rbio, READ_ERR, BLK_STS_IOERR);
667 bch_err_inum_offset_ratelimited(c, rbio->read_pos.inode,
668 rbio->read_pos.offset << 9,
670 bch2_rbio_error(rbio, READ_ERR, BLK_STS_IOERR);
674 static void bch2_read_endio(struct bio *bio)
676 struct bch_read_bio *rbio =
677 container_of(bio, struct bch_read_bio, bio);
678 struct bch_fs *c = rbio->c;
679 struct bch_dev *ca = rbio->have_ioref ? bch2_dev_have_ref(c, rbio->pick.ptr.dev) : NULL;
680 struct workqueue_struct *wq = NULL;
681 enum rbio_context context = RBIO_CONTEXT_NULL;
683 if (rbio->have_ioref) {
684 bch2_latency_acct(ca, rbio->submit_time, READ);
685 percpu_ref_put(&ca->io_ref);
689 rbio->bio.bi_end_io = rbio->end_io;
691 if (bio->bi_status) {
693 bch_err_inum_offset_ratelimited(ca,
694 rbio->read_pos.inode,
695 rbio->read_pos.offset,
696 "data read error: %s",
697 bch2_blk_status_to_str(bio->bi_status));
698 bch2_io_error(ca, BCH_MEMBER_ERROR_read);
700 bch2_rbio_error(rbio, READ_RETRY_AVOID, bio->bi_status);
704 if (((rbio->flags & BCH_READ_RETRY_IF_STALE) && race_fault()) ||
705 (ca && dev_ptr_stale(ca, &rbio->pick.ptr))) {
706 trace_and_count(c, read_reuse_race, &rbio->bio);
708 if (rbio->flags & BCH_READ_RETRY_IF_STALE)
709 bch2_rbio_error(rbio, READ_RETRY, BLK_STS_AGAIN);
711 bch2_rbio_error(rbio, READ_ERR, BLK_STS_AGAIN);
715 if (rbio->narrow_crcs ||
717 crc_is_compressed(rbio->pick.crc) ||
718 bch2_csum_type_is_encryption(rbio->pick.crc.csum_type))
719 context = RBIO_CONTEXT_UNBOUND, wq = system_unbound_wq;
720 else if (rbio->pick.crc.csum_type)
721 context = RBIO_CONTEXT_HIGHPRI, wq = system_highpri_wq;
723 bch2_rbio_punt(rbio, __bch2_read_endio, context, wq);
726 int __bch2_read_indirect_extent(struct btree_trans *trans,
727 unsigned *offset_into_extent,
728 struct bkey_buf *orig_k)
730 struct btree_iter iter;
735 reflink_offset = le64_to_cpu(bkey_i_to_reflink_p(orig_k->k)->v.idx) +
738 k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_reflink,
739 POS(0, reflink_offset), 0);
744 if (k.k->type != KEY_TYPE_reflink_v &&
745 k.k->type != KEY_TYPE_indirect_inline_data) {
746 bch_err_inum_offset_ratelimited(trans->c,
747 orig_k->k->k.p.inode,
748 orig_k->k->k.p.offset << 9,
749 "%llu len %u points to nonexistent indirect extent %llu",
750 orig_k->k->k.p.offset,
753 bch2_inconsistent_error(trans->c);
758 *offset_into_extent = iter.pos.offset - bkey_start_offset(k.k);
759 bch2_bkey_buf_reassemble(orig_k, trans->c, k);
761 bch2_trans_iter_exit(trans, &iter);
765 static noinline void read_from_stale_dirty_pointer(struct btree_trans *trans,
768 struct bch_extent_ptr ptr)
770 struct bch_fs *c = trans->c;
771 struct btree_iter iter;
772 struct printbuf buf = PRINTBUF;
775 bch2_trans_iter_init(trans, &iter, BTREE_ID_alloc,
776 PTR_BUCKET_POS(ca, &ptr),
779 prt_printf(&buf, "Attempting to read from stale dirty pointer:\n");
780 printbuf_indent_add(&buf, 2);
782 bch2_bkey_val_to_text(&buf, c, k);
785 prt_printf(&buf, "memory gen: %u", *bucket_gen(ca, iter.pos.offset));
787 ret = lockrestart_do(trans, bkey_err(k = bch2_btree_iter_peek_slot(&iter)));
790 bch2_bkey_val_to_text(&buf, c, k);
793 bch2_fs_inconsistent(c, "%s", buf.buf);
795 bch2_trans_iter_exit(trans, &iter);
799 int __bch2_read_extent(struct btree_trans *trans, struct bch_read_bio *orig,
800 struct bvec_iter iter, struct bpos read_pos,
801 enum btree_id data_btree, struct bkey_s_c k,
802 unsigned offset_into_extent,
803 struct bch_io_failures *failed, unsigned flags)
805 struct bch_fs *c = trans->c;
806 struct extent_ptr_decoded pick;
807 struct bch_read_bio *rbio = NULL;
808 struct promote_op *promote = NULL;
809 bool bounce = false, read_full = false, narrow_crcs = false;
810 struct bpos data_pos = bkey_start_pos(k.k);
813 if (bkey_extent_is_inline_data(k.k)) {
814 unsigned bytes = min_t(unsigned, iter.bi_size,
815 bkey_inline_data_bytes(k.k));
817 swap(iter.bi_size, bytes);
818 memcpy_to_bio(&orig->bio, iter, bkey_inline_data_p(k));
819 swap(iter.bi_size, bytes);
820 bio_advance_iter(&orig->bio, &iter, bytes);
821 zero_fill_bio_iter(&orig->bio, iter);
825 pick_ret = bch2_bkey_pick_read_device(c, k, failed, &pick);
827 /* hole or reservation - just zero fill: */
832 bch_err_inum_offset_ratelimited(c,
833 read_pos.inode, read_pos.offset << 9,
834 "no device to read from");
838 struct bch_dev *ca = bch2_dev_get_ioref(c, pick.ptr.dev, READ);
841 * Stale dirty pointers are treated as IO errors, but @failed isn't
842 * allocated unless we're in the retry path - so if we're not in the
843 * retry path, don't check here, it'll be caught in bch2_read_endio()
844 * and we'll end up in the retry path:
846 if ((flags & BCH_READ_IN_RETRY) &&
849 unlikely(dev_ptr_stale(ca, &pick.ptr))) {
850 read_from_stale_dirty_pointer(trans, ca, k, pick.ptr);
851 bch2_mark_io_failure(failed, &pick);
852 percpu_ref_put(&ca->io_ref);
857 * Unlock the iterator while the btree node's lock is still in
858 * cache, before doing the IO:
860 bch2_trans_unlock(trans);
862 if (flags & BCH_READ_NODECODE) {
864 * can happen if we retry, and the extent we were going to read
865 * has been merged in the meantime:
867 if (pick.crc.compressed_size > orig->bio.bi_vcnt * PAGE_SECTORS) {
869 percpu_ref_put(&ca->io_ref);
873 iter.bi_size = pick.crc.compressed_size << 9;
877 if (!(flags & BCH_READ_LAST_FRAGMENT) ||
878 bio_flagged(&orig->bio, BIO_CHAIN))
879 flags |= BCH_READ_MUST_CLONE;
881 narrow_crcs = !(flags & BCH_READ_IN_RETRY) &&
882 bch2_can_narrow_extent_crcs(k, pick.crc);
884 if (narrow_crcs && (flags & BCH_READ_USER_MAPPED))
885 flags |= BCH_READ_MUST_BOUNCE;
887 EBUG_ON(offset_into_extent + bvec_iter_sectors(iter) > k.k->size);
889 if (crc_is_compressed(pick.crc) ||
890 (pick.crc.csum_type != BCH_CSUM_none &&
891 (bvec_iter_sectors(iter) != pick.crc.uncompressed_size ||
892 (bch2_csum_type_is_encryption(pick.crc.csum_type) &&
893 (flags & BCH_READ_USER_MAPPED)) ||
894 (flags & BCH_READ_MUST_BOUNCE)))) {
899 if (orig->opts.promote_target)
900 promote = promote_alloc(trans, iter, k, &pick, orig->opts, flags,
901 &rbio, &bounce, &read_full);
904 EBUG_ON(crc_is_compressed(pick.crc));
905 EBUG_ON(pick.crc.csum_type &&
906 (bvec_iter_sectors(iter) != pick.crc.uncompressed_size ||
907 bvec_iter_sectors(iter) != pick.crc.live_size ||
909 offset_into_extent));
911 data_pos.offset += offset_into_extent;
912 pick.ptr.offset += pick.crc.offset +
914 offset_into_extent = 0;
915 pick.crc.compressed_size = bvec_iter_sectors(iter);
916 pick.crc.uncompressed_size = bvec_iter_sectors(iter);
918 pick.crc.live_size = bvec_iter_sectors(iter);
923 * promote already allocated bounce rbio:
924 * promote needs to allocate a bio big enough for uncompressing
925 * data in the write path, but we're not going to use it all
928 EBUG_ON(rbio->bio.bi_iter.bi_size <
929 pick.crc.compressed_size << 9);
930 rbio->bio.bi_iter.bi_size =
931 pick.crc.compressed_size << 9;
933 unsigned sectors = pick.crc.compressed_size;
935 rbio = rbio_init(bio_alloc_bioset(NULL,
936 DIV_ROUND_UP(sectors, PAGE_SECTORS),
942 bch2_bio_alloc_pages_pool(c, &rbio->bio, sectors << 9);
945 } else if (flags & BCH_READ_MUST_CLONE) {
947 * Have to clone if there were any splits, due to error
948 * reporting issues (if a split errored, and retrying didn't
949 * work, when it reports the error to its parent (us) we don't
950 * know if the error was from our bio, and we should retry, or
951 * from the whole bio, in which case we don't want to retry and
954 rbio = rbio_init(bio_alloc_clone(NULL, &orig->bio, GFP_NOFS,
957 rbio->bio.bi_iter = iter;
961 rbio->bio.bi_iter = iter;
962 EBUG_ON(bio_flagged(&rbio->bio, BIO_CHAIN));
965 EBUG_ON(bio_sectors(&rbio->bio) != pick.crc.compressed_size);
968 rbio->submit_time = local_clock();
972 rbio->end_io = orig->bio.bi_end_io;
973 rbio->bvec_iter = iter;
974 rbio->offset_into_extent= offset_into_extent;
976 rbio->have_ioref = ca != NULL;
977 rbio->narrow_crcs = narrow_crcs;
981 /* XXX: only initialize this if needed */
982 rbio->devs_have = bch2_bkey_devs(k);
984 rbio->subvol = orig->subvol;
985 rbio->read_pos = read_pos;
986 rbio->data_btree = data_btree;
987 rbio->data_pos = data_pos;
988 rbio->version = k.k->version;
989 rbio->promote = promote;
990 INIT_WORK(&rbio->work, NULL);
992 rbio->bio.bi_opf = orig->bio.bi_opf;
993 rbio->bio.bi_iter.bi_sector = pick.ptr.offset;
994 rbio->bio.bi_end_io = bch2_read_endio;
997 trace_and_count(c, read_bounce, &rbio->bio);
999 this_cpu_add(c->counters[BCH_COUNTER_io_read], bio_sectors(&rbio->bio));
1000 bch2_increment_clock(c, bio_sectors(&rbio->bio), READ);
1003 * If it's being moved internally, we don't want to flag it as a cache
1006 if (ca && pick.ptr.cached && !(flags & BCH_READ_NODECODE))
1007 bch2_bucket_io_time_reset(trans, pick.ptr.dev,
1008 PTR_BUCKET_NR(ca, &pick.ptr), READ);
1010 if (!(flags & (BCH_READ_IN_RETRY|BCH_READ_LAST_FRAGMENT))) {
1011 bio_inc_remaining(&orig->bio);
1012 trace_and_count(c, read_split, &orig->bio);
1015 if (!rbio->pick.idx) {
1016 if (!rbio->have_ioref) {
1017 bch_err_inum_offset_ratelimited(c,
1019 read_pos.offset << 9,
1020 "no device to read from");
1021 bch2_rbio_error(rbio, READ_RETRY_AVOID, BLK_STS_IOERR);
1025 this_cpu_add(ca->io_done->sectors[READ][BCH_DATA_user],
1026 bio_sectors(&rbio->bio));
1027 bio_set_dev(&rbio->bio, ca->disk_sb.bdev);
1029 if (unlikely(c->opts.no_data_io)) {
1030 if (likely(!(flags & BCH_READ_IN_RETRY)))
1031 bio_endio(&rbio->bio);
1033 if (likely(!(flags & BCH_READ_IN_RETRY)))
1034 submit_bio(&rbio->bio);
1036 submit_bio_wait(&rbio->bio);
1040 * We just submitted IO which may block, we expect relock fail
1041 * events and shouldn't count them:
1043 trans->notrace_relock_fail = true;
1045 /* Attempting reconstruct read: */
1046 if (bch2_ec_read_extent(trans, rbio)) {
1047 bch2_rbio_error(rbio, READ_RETRY_AVOID, BLK_STS_IOERR);
1051 if (likely(!(flags & BCH_READ_IN_RETRY)))
1052 bio_endio(&rbio->bio);
1055 if (likely(!(flags & BCH_READ_IN_RETRY))) {
1060 rbio->context = RBIO_CONTEXT_UNBOUND;
1061 bch2_read_endio(&rbio->bio);
1064 rbio = bch2_rbio_free(rbio);
1066 if (ret == READ_RETRY_AVOID) {
1067 bch2_mark_io_failure(failed, &pick);
1078 if (flags & BCH_READ_IN_RETRY)
1081 orig->bio.bi_status = BLK_STS_IOERR;
1086 * won't normally happen in the BCH_READ_NODECODE
1087 * (bch2_move_extent()) path, but if we retry and the extent we wanted
1088 * to read no longer exists we have to signal that:
1090 if (flags & BCH_READ_NODECODE)
1093 zero_fill_bio_iter(&orig->bio, iter);
1095 if (flags & BCH_READ_LAST_FRAGMENT)
1096 bch2_rbio_done(orig);
1100 void __bch2_read(struct bch_fs *c, struct bch_read_bio *rbio,
1101 struct bvec_iter bvec_iter, subvol_inum inum,
1102 struct bch_io_failures *failed, unsigned flags)
1104 struct btree_trans *trans = bch2_trans_get(c);
1105 struct btree_iter iter;
1111 BUG_ON(flags & BCH_READ_NODECODE);
1113 bch2_bkey_buf_init(&sk);
1115 bch2_trans_begin(trans);
1116 iter = (struct btree_iter) { NULL };
1118 ret = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot);
1122 bch2_trans_iter_init(trans, &iter, BTREE_ID_extents,
1123 SPOS(inum.inum, bvec_iter.bi_sector, snapshot),
1126 unsigned bytes, sectors, offset_into_extent;
1127 enum btree_id data_btree = BTREE_ID_extents;
1130 * read_extent -> io_time_reset may cause a transaction restart
1131 * without returning an error, we need to check for that here:
1133 ret = bch2_trans_relock(trans);
1137 bch2_btree_iter_set_pos(&iter,
1138 POS(inum.inum, bvec_iter.bi_sector));
1140 k = bch2_btree_iter_peek_slot(&iter);
1145 offset_into_extent = iter.pos.offset -
1146 bkey_start_offset(k.k);
1147 sectors = k.k->size - offset_into_extent;
1149 bch2_bkey_buf_reassemble(&sk, c, k);
1151 ret = bch2_read_indirect_extent(trans, &data_btree,
1152 &offset_into_extent, &sk);
1156 k = bkey_i_to_s_c(sk.k);
1159 * With indirect extents, the amount of data to read is the min
1160 * of the original extent and the indirect extent:
1162 sectors = min(sectors, k.k->size - offset_into_extent);
1164 bytes = min(sectors, bvec_iter_sectors(bvec_iter)) << 9;
1165 swap(bvec_iter.bi_size, bytes);
1167 if (bvec_iter.bi_size == bytes)
1168 flags |= BCH_READ_LAST_FRAGMENT;
1170 ret = __bch2_read_extent(trans, rbio, bvec_iter, iter.pos,
1172 offset_into_extent, failed, flags);
1176 if (flags & BCH_READ_LAST_FRAGMENT)
1179 swap(bvec_iter.bi_size, bytes);
1180 bio_advance_iter(&rbio->bio, &bvec_iter, bytes);
1182 ret = btree_trans_too_many_iters(trans);
1187 bch2_trans_iter_exit(trans, &iter);
1189 if (bch2_err_matches(ret, BCH_ERR_transaction_restart) ||
1190 ret == READ_RETRY ||
1191 ret == READ_RETRY_AVOID)
1194 bch2_trans_put(trans);
1195 bch2_bkey_buf_exit(&sk, c);
1198 bch_err_inum_offset_ratelimited(c, inum.inum,
1199 bvec_iter.bi_sector << 9,
1200 "read error %i from btree lookup", ret);
1201 rbio->bio.bi_status = BLK_STS_IOERR;
1202 bch2_rbio_done(rbio);
1206 void bch2_fs_io_read_exit(struct bch_fs *c)
1208 if (c->promote_table.tbl)
1209 rhashtable_destroy(&c->promote_table);
1210 bioset_exit(&c->bio_read_split);
1211 bioset_exit(&c->bio_read);
1214 int bch2_fs_io_read_init(struct bch_fs *c)
1216 if (bioset_init(&c->bio_read, 1, offsetof(struct bch_read_bio, bio),
1218 return -BCH_ERR_ENOMEM_bio_read_init;
1220 if (bioset_init(&c->bio_read_split, 1, offsetof(struct bch_read_bio, bio),
1222 return -BCH_ERR_ENOMEM_bio_read_split_init;
1224 if (rhashtable_init(&c->promote_table, &bch_promote_params))
1225 return -BCH_ERR_ENOMEM_promote_table_init;