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"
25 #include "subvolume.h"
28 #include <linux/sched/mm.h>
30 #ifndef CONFIG_BCACHEFS_NO_LATENCY_ACCT
32 static bool bch2_target_congested(struct bch_fs *c, u16 target)
34 const struct bch_devs_mask *devs;
35 unsigned d, nr = 0, total = 0;
36 u64 now = local_clock(), last;
44 devs = bch2_target_to_mask(c, target) ?:
45 &c->rw_devs[BCH_DATA_user];
47 for_each_set_bit(d, devs->d, BCH_SB_MEMBERS_MAX) {
48 ca = rcu_dereference(c->devs[d]);
52 congested = atomic_read(&ca->congested);
53 last = READ_ONCE(ca->congested_last);
54 if (time_after64(now, last))
55 congested -= (now - last) >> 12;
57 total += max(congested, 0LL);
62 return bch2_rand_range(nr * CONGESTED_MAX) < total;
67 static bool bch2_target_congested(struct bch_fs *c, u16 target)
74 /* Cache promotion on read */
80 struct rhash_head hash;
83 struct data_update write;
84 struct bio_vec bi_inline_vecs[]; /* must be last */
87 static const struct rhashtable_params bch_promote_params = {
88 .head_offset = offsetof(struct promote_op, hash),
89 .key_offset = offsetof(struct promote_op, pos),
90 .key_len = sizeof(struct bpos),
91 .automatic_shrinking = true,
94 static inline bool have_io_error(struct bch_io_failures *failed)
96 return failed && failed->nr;
99 static inline int should_promote(struct bch_fs *c, struct bkey_s_c k,
101 struct bch_io_opts opts,
103 struct bch_io_failures *failed)
105 if (!have_io_error(failed)) {
106 BUG_ON(!opts.promote_target);
108 if (!(flags & BCH_READ_MAY_PROMOTE))
109 return -BCH_ERR_nopromote_may_not;
111 if (bch2_bkey_has_target(c, k, opts.promote_target))
112 return -BCH_ERR_nopromote_already_promoted;
114 if (bkey_extent_is_unwritten(k))
115 return -BCH_ERR_nopromote_unwritten;
117 if (bch2_target_congested(c, opts.promote_target))
118 return -BCH_ERR_nopromote_congested;
121 if (rhashtable_lookup_fast(&c->promote_table, &pos,
123 return -BCH_ERR_nopromote_in_flight;
128 static void promote_free(struct bch_fs *c, struct promote_op *op)
132 bch2_data_update_exit(&op->write);
134 ret = rhashtable_remove_fast(&c->promote_table, &op->hash,
137 bch2_write_ref_put(c, BCH_WRITE_REF_promote);
141 static void promote_done(struct bch_write_op *wop)
143 struct promote_op *op =
144 container_of(wop, struct promote_op, write.op);
145 struct bch_fs *c = op->write.op.c;
147 bch2_time_stats_update(&c->times[BCH_TIME_data_promote],
152 static void promote_start(struct promote_op *op, struct bch_read_bio *rbio)
154 struct bio *bio = &op->write.op.wbio.bio;
156 trace_and_count(op->write.op.c, read_promote, &rbio->bio);
158 /* we now own pages: */
159 BUG_ON(!rbio->bounce);
160 BUG_ON(rbio->bio.bi_vcnt > bio->bi_max_vecs);
162 memcpy(bio->bi_io_vec, rbio->bio.bi_io_vec,
163 sizeof(struct bio_vec) * rbio->bio.bi_vcnt);
164 swap(bio->bi_vcnt, rbio->bio.bi_vcnt);
166 bch2_data_update_read_done(&op->write, rbio->pick.crc);
169 static struct promote_op *__promote_alloc(struct btree_trans *trans,
170 enum btree_id btree_id,
173 struct extent_ptr_decoded *pick,
174 struct bch_io_opts opts,
176 struct bch_read_bio **rbio,
177 struct bch_io_failures *failed)
179 struct bch_fs *c = trans->c;
180 struct promote_op *op = NULL;
182 unsigned pages = DIV_ROUND_UP(sectors, PAGE_SECTORS);
185 if (!bch2_write_ref_tryget(c, BCH_WRITE_REF_promote))
186 return ERR_PTR(-BCH_ERR_nopromote_no_writes);
188 op = kzalloc(struct_size(op, bi_inline_vecs, pages), GFP_KERNEL);
190 ret = -BCH_ERR_nopromote_enomem;
194 op->start_time = local_clock();
198 * We don't use the mempool here because extents that aren't
199 * checksummed or compressed can be too big for the mempool:
201 *rbio = kzalloc(sizeof(struct bch_read_bio) +
202 sizeof(struct bio_vec) * pages,
205 ret = -BCH_ERR_nopromote_enomem;
209 rbio_init(&(*rbio)->bio, opts);
210 bio_init(&(*rbio)->bio, NULL, (*rbio)->bio.bi_inline_vecs, pages, 0);
212 if (bch2_bio_alloc_pages(&(*rbio)->bio, sectors << 9, GFP_KERNEL)) {
213 ret = -BCH_ERR_nopromote_enomem;
217 (*rbio)->bounce = true;
218 (*rbio)->split = true;
219 (*rbio)->kmalloc = true;
221 if (rhashtable_lookup_insert_fast(&c->promote_table, &op->hash,
222 bch_promote_params)) {
223 ret = -BCH_ERR_nopromote_in_flight;
227 bio = &op->write.op.wbio.bio;
228 bio_init(bio, NULL, bio->bi_inline_vecs, pages, 0);
230 struct data_update_opts update_opts = {};
232 if (!have_io_error(failed)) {
233 update_opts.target = opts.promote_target;
234 update_opts.extra_replicas = 1;
235 update_opts.write_flags = BCH_WRITE_ALLOC_NOWAIT|BCH_WRITE_CACHED;
237 update_opts.target = opts.foreground_target;
239 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
240 unsigned ptr_bit = 1;
241 bkey_for_each_ptr(ptrs, ptr) {
242 if (bch2_dev_io_failures(failed, ptr->dev))
243 update_opts.rewrite_ptrs |= ptr_bit;
248 ret = bch2_data_update_init(trans, NULL, NULL, &op->write,
249 writepoint_hashed((unsigned long) current),
254 * possible errors: -BCH_ERR_nocow_lock_blocked,
255 * -BCH_ERR_ENOSPC_disk_reservation:
258 BUG_ON(rhashtable_remove_fast(&c->promote_table, &op->hash,
259 bch_promote_params));
263 op->write.op.end_io = promote_done;
268 bio_free_pages(&(*rbio)->bio);
271 /* We may have added to the rhashtable and thus need rcu freeing: */
273 bch2_write_ref_put(c, BCH_WRITE_REF_promote);
278 static struct promote_op *promote_alloc(struct btree_trans *trans,
279 struct bvec_iter iter,
281 struct extent_ptr_decoded *pick,
282 struct bch_io_opts opts,
284 struct bch_read_bio **rbio,
287 struct bch_io_failures *failed)
289 struct bch_fs *c = trans->c;
291 * if failed != NULL we're not actually doing a promote, we're
292 * recovering from an io/checksum error
294 bool promote_full = (have_io_error(failed) ||
296 READ_ONCE(c->opts.promote_whole_extents));
297 /* data might have to be decompressed in the write path: */
298 unsigned sectors = promote_full
299 ? max(pick->crc.compressed_size, pick->crc.live_size)
300 : bvec_iter_sectors(iter);
301 struct bpos pos = promote_full
302 ? bkey_start_pos(k.k)
303 : POS(k.k->p.inode, iter.bi_sector);
304 struct promote_op *promote;
307 ret = should_promote(c, k, pos, opts, flags, failed);
311 promote = __promote_alloc(trans,
312 k.k->type == KEY_TYPE_reflink_v
315 k, pos, pick, opts, sectors, rbio, failed);
316 ret = PTR_ERR_OR_ZERO(promote);
321 *read_full = promote_full;
324 trace_read_nopromote(c, ret);
330 static int bch2_read_err_msg_trans(struct btree_trans *trans, struct printbuf *out,
331 struct bch_read_bio *rbio, struct bpos read_pos)
333 return bch2_inum_offset_err_msg_trans(trans, out,
334 (subvol_inum) { rbio->subvol, read_pos.inode },
335 read_pos.offset << 9);
338 static void bch2_read_err_msg(struct bch_fs *c, struct printbuf *out,
339 struct bch_read_bio *rbio, struct bpos read_pos)
341 bch2_trans_run(c, bch2_read_err_msg_trans(trans, out, rbio, read_pos));
344 #define READ_RETRY_AVOID 1
350 RBIO_CONTEXT_HIGHPRI,
351 RBIO_CONTEXT_UNBOUND,
354 static inline struct bch_read_bio *
355 bch2_rbio_parent(struct bch_read_bio *rbio)
357 return rbio->split ? rbio->parent : rbio;
361 static void bch2_rbio_punt(struct bch_read_bio *rbio, work_func_t fn,
362 enum rbio_context context,
363 struct workqueue_struct *wq)
365 if (context <= rbio->context) {
368 rbio->work.func = fn;
369 rbio->context = context;
370 queue_work(wq, &rbio->work);
374 static inline struct bch_read_bio *bch2_rbio_free(struct bch_read_bio *rbio)
376 BUG_ON(rbio->bounce && !rbio->split);
379 promote_free(rbio->c, rbio->promote);
380 rbio->promote = NULL;
383 bch2_bio_free_pages_pool(rbio->c, &rbio->bio);
386 struct bch_read_bio *parent = rbio->parent;
400 * Only called on a top level bch_read_bio to complete an entire read request,
403 static void bch2_rbio_done(struct bch_read_bio *rbio)
405 if (rbio->start_time)
406 bch2_time_stats_update(&rbio->c->times[BCH_TIME_data_read],
408 bio_endio(&rbio->bio);
411 static void bch2_read_retry_nodecode(struct bch_fs *c, struct bch_read_bio *rbio,
412 struct bvec_iter bvec_iter,
413 struct bch_io_failures *failed,
416 struct btree_trans *trans = bch2_trans_get(c);
417 struct btree_iter iter;
422 flags &= ~BCH_READ_LAST_FRAGMENT;
423 flags |= BCH_READ_MUST_CLONE;
425 bch2_bkey_buf_init(&sk);
427 bch2_trans_iter_init(trans, &iter, rbio->data_btree,
428 rbio->read_pos, BTREE_ITER_slots);
430 bch2_trans_begin(trans);
431 rbio->bio.bi_status = 0;
433 ret = lockrestart_do(trans, bkey_err(k = bch2_btree_iter_peek_slot(&iter)));
437 bch2_bkey_buf_reassemble(&sk, c, k);
438 k = bkey_i_to_s_c(sk.k);
440 if (!bch2_bkey_matches_ptr(c, k,
442 rbio->data_pos.offset -
443 rbio->pick.crc.offset)) {
444 /* extent we wanted to read no longer exists: */
449 ret = __bch2_read_extent(trans, rbio, bvec_iter,
452 k, 0, failed, flags);
453 if (ret == READ_RETRY)
458 bch2_rbio_done(rbio);
459 bch2_trans_iter_exit(trans, &iter);
460 bch2_trans_put(trans);
461 bch2_bkey_buf_exit(&sk, c);
464 rbio->bio.bi_status = BLK_STS_IOERR;
468 static void bch2_rbio_retry(struct work_struct *work)
470 struct bch_read_bio *rbio =
471 container_of(work, struct bch_read_bio, work);
472 struct bch_fs *c = rbio->c;
473 struct bvec_iter iter = rbio->bvec_iter;
474 unsigned flags = rbio->flags;
476 .subvol = rbio->subvol,
477 .inum = rbio->read_pos.inode,
479 struct bch_io_failures failed = { .nr = 0 };
481 trace_and_count(c, read_retry, &rbio->bio);
483 if (rbio->retry == READ_RETRY_AVOID)
484 bch2_mark_io_failure(&failed, &rbio->pick);
486 rbio->bio.bi_status = 0;
488 rbio = bch2_rbio_free(rbio);
490 flags |= BCH_READ_IN_RETRY;
491 flags &= ~BCH_READ_MAY_PROMOTE;
493 if (flags & BCH_READ_NODECODE) {
494 bch2_read_retry_nodecode(c, rbio, iter, &failed, flags);
496 flags &= ~BCH_READ_LAST_FRAGMENT;
497 flags |= BCH_READ_MUST_CLONE;
499 __bch2_read(c, rbio, iter, inum, &failed, flags);
503 static void bch2_rbio_error(struct bch_read_bio *rbio, int retry,
508 if (rbio->flags & BCH_READ_IN_RETRY)
511 if (retry == READ_ERR) {
512 rbio = bch2_rbio_free(rbio);
514 rbio->bio.bi_status = error;
515 bch2_rbio_done(rbio);
517 bch2_rbio_punt(rbio, bch2_rbio_retry,
518 RBIO_CONTEXT_UNBOUND, system_unbound_wq);
522 static void bch2_read_io_err(struct work_struct *work)
524 struct bch_read_bio *rbio =
525 container_of(work, struct bch_read_bio, work);
526 struct bio *bio = &rbio->bio;
527 struct bch_fs *c = rbio->c;
528 struct bch_dev *ca = rbio->have_ioref ? bch2_dev_have_ref(c, rbio->pick.ptr.dev) : NULL;
529 struct printbuf buf = PRINTBUF;
531 bch2_read_err_msg(c, &buf, rbio, rbio->read_pos);
532 prt_printf(&buf, "data read error: %s", bch2_blk_status_to_str(bio->bi_status));
535 bch2_io_error(ca, BCH_MEMBER_ERROR_read);
536 bch_err_ratelimited(ca, "%s", buf.buf);
538 bch_err_ratelimited(c, "%s", buf.buf);
542 bch2_rbio_error(rbio, READ_RETRY_AVOID, bio->bi_status);
545 static int __bch2_rbio_narrow_crcs(struct btree_trans *trans,
546 struct bch_read_bio *rbio)
548 struct bch_fs *c = rbio->c;
549 u64 data_offset = rbio->data_pos.offset - rbio->pick.crc.offset;
550 struct bch_extent_crc_unpacked new_crc;
551 struct btree_iter iter;
556 if (crc_is_compressed(rbio->pick.crc))
559 k = bch2_bkey_get_iter(trans, &iter, rbio->data_btree, rbio->data_pos,
560 BTREE_ITER_slots|BTREE_ITER_intent);
561 if ((ret = bkey_err(k)))
564 if (bversion_cmp(k.k->bversion, rbio->version) ||
565 !bch2_bkey_matches_ptr(c, k, rbio->pick.ptr, data_offset))
568 /* Extent was merged? */
569 if (bkey_start_offset(k.k) < data_offset ||
570 k.k->p.offset > data_offset + rbio->pick.crc.uncompressed_size)
573 if (bch2_rechecksum_bio(c, &rbio->bio, rbio->version,
574 rbio->pick.crc, NULL, &new_crc,
575 bkey_start_offset(k.k) - data_offset, k.k->size,
576 rbio->pick.crc.csum_type)) {
577 bch_err(c, "error verifying existing checksum while narrowing checksum (memory corruption?)");
583 * going to be temporarily appending another checksum entry:
585 new = bch2_trans_kmalloc(trans, bkey_bytes(k.k) +
586 sizeof(struct bch_extent_crc128));
587 if ((ret = PTR_ERR_OR_ZERO(new)))
590 bkey_reassemble(new, k);
592 if (!bch2_bkey_narrow_crcs(new, new_crc))
595 ret = bch2_trans_update(trans, &iter, new,
596 BTREE_UPDATE_internal_snapshot_node);
598 bch2_trans_iter_exit(trans, &iter);
602 static noinline void bch2_rbio_narrow_crcs(struct bch_read_bio *rbio)
604 bch2_trans_commit_do(rbio->c, NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
605 __bch2_rbio_narrow_crcs(trans, rbio));
608 static void bch2_read_csum_err(struct work_struct *work)
610 struct bch_read_bio *rbio =
611 container_of(work, struct bch_read_bio, work);
612 struct bch_fs *c = rbio->c;
613 struct bio *src = &rbio->bio;
614 struct bch_extent_crc_unpacked crc = rbio->pick.crc;
615 struct nonce nonce = extent_nonce(rbio->version, crc);
616 struct bch_csum csum = bch2_checksum_bio(c, crc.csum_type, nonce, src);
617 struct printbuf buf = PRINTBUF;
619 bch2_read_err_msg(c, &buf, rbio, rbio->read_pos);
620 prt_str(&buf, "data ");
621 bch2_csum_err_msg(&buf, crc.csum_type, rbio->pick.crc.csum, csum);
623 struct bch_dev *ca = rbio->have_ioref ? bch2_dev_have_ref(c, rbio->pick.ptr.dev) : NULL;
625 bch2_io_error(ca, BCH_MEMBER_ERROR_checksum);
626 bch_err_ratelimited(ca, "%s", buf.buf);
628 bch_err_ratelimited(c, "%s", buf.buf);
631 bch2_rbio_error(rbio, READ_RETRY_AVOID, BLK_STS_IOERR);
635 static void bch2_read_decompress_err(struct work_struct *work)
637 struct bch_read_bio *rbio =
638 container_of(work, struct bch_read_bio, work);
639 struct bch_fs *c = rbio->c;
640 struct printbuf buf = PRINTBUF;
642 bch2_read_err_msg(c, &buf, rbio, rbio->read_pos);
643 prt_str(&buf, "decompression error");
645 struct bch_dev *ca = rbio->have_ioref ? bch2_dev_have_ref(c, rbio->pick.ptr.dev) : NULL;
647 bch_err_ratelimited(ca, "%s", buf.buf);
649 bch_err_ratelimited(c, "%s", buf.buf);
651 bch2_rbio_error(rbio, READ_ERR, BLK_STS_IOERR);
655 static void bch2_read_decrypt_err(struct work_struct *work)
657 struct bch_read_bio *rbio =
658 container_of(work, struct bch_read_bio, work);
659 struct bch_fs *c = rbio->c;
660 struct printbuf buf = PRINTBUF;
662 bch2_read_err_msg(c, &buf, rbio, rbio->read_pos);
663 prt_str(&buf, "decrypt error");
665 struct bch_dev *ca = rbio->have_ioref ? bch2_dev_have_ref(c, rbio->pick.ptr.dev) : NULL;
667 bch_err_ratelimited(ca, "%s", buf.buf);
669 bch_err_ratelimited(c, "%s", buf.buf);
671 bch2_rbio_error(rbio, READ_ERR, BLK_STS_IOERR);
675 /* Inner part that may run in process context */
676 static void __bch2_read_endio(struct work_struct *work)
678 struct bch_read_bio *rbio =
679 container_of(work, struct bch_read_bio, work);
680 struct bch_fs *c = rbio->c;
681 struct bio *src = &rbio->bio;
682 struct bio *dst = &bch2_rbio_parent(rbio)->bio;
683 struct bvec_iter dst_iter = rbio->bvec_iter;
684 struct bch_extent_crc_unpacked crc = rbio->pick.crc;
685 struct nonce nonce = extent_nonce(rbio->version, crc);
687 struct bch_csum csum;
690 nofs_flags = memalloc_nofs_save();
692 /* Reset iterator for checksumming and copying bounced data: */
694 src->bi_iter.bi_size = crc.compressed_size << 9;
695 src->bi_iter.bi_idx = 0;
696 src->bi_iter.bi_bvec_done = 0;
698 src->bi_iter = rbio->bvec_iter;
701 csum = bch2_checksum_bio(c, crc.csum_type, nonce, src);
702 if (bch2_crc_cmp(csum, rbio->pick.crc.csum) && !c->opts.no_data_io)
707 * We need to rework the narrow_crcs path to deliver the read completion
708 * first, and then punt to a different workqueue, otherwise we're
709 * holding up reads while doing btree updates which is bad for memory
712 if (unlikely(rbio->narrow_crcs))
713 bch2_rbio_narrow_crcs(rbio);
715 if (rbio->flags & BCH_READ_NODECODE)
718 /* Adjust crc to point to subset of data we want: */
719 crc.offset += rbio->offset_into_extent;
720 crc.live_size = bvec_iter_sectors(rbio->bvec_iter);
722 if (crc_is_compressed(crc)) {
723 ret = bch2_encrypt_bio(c, crc.csum_type, nonce, src);
727 if (bch2_bio_uncompress(c, src, dst, dst_iter, crc) &&
729 goto decompression_err;
731 /* don't need to decrypt the entire bio: */
732 nonce = nonce_add(nonce, crc.offset << 9);
733 bio_advance(src, crc.offset << 9);
735 BUG_ON(src->bi_iter.bi_size < dst_iter.bi_size);
736 src->bi_iter.bi_size = dst_iter.bi_size;
738 ret = bch2_encrypt_bio(c, crc.csum_type, nonce, src);
743 struct bvec_iter src_iter = src->bi_iter;
745 bio_copy_data_iter(dst, &dst_iter, src, &src_iter);
751 * Re encrypt data we decrypted, so it's consistent with
754 ret = bch2_encrypt_bio(c, crc.csum_type, nonce, src);
758 promote_start(rbio->promote, rbio);
759 rbio->promote = NULL;
762 if (likely(!(rbio->flags & BCH_READ_IN_RETRY))) {
763 rbio = bch2_rbio_free(rbio);
764 bch2_rbio_done(rbio);
767 memalloc_nofs_restore(nofs_flags);
771 * Checksum error: if the bio wasn't bounced, we may have been
772 * reading into buffers owned by userspace (that userspace can
773 * scribble over) - retry the read, bouncing it this time:
775 if (!rbio->bounce && (rbio->flags & BCH_READ_USER_MAPPED)) {
776 rbio->flags |= BCH_READ_MUST_BOUNCE;
777 bch2_rbio_error(rbio, READ_RETRY, BLK_STS_IOERR);
781 bch2_rbio_punt(rbio, bch2_read_csum_err, RBIO_CONTEXT_UNBOUND, system_unbound_wq);
784 bch2_rbio_punt(rbio, bch2_read_decompress_err, RBIO_CONTEXT_UNBOUND, system_unbound_wq);
787 bch2_rbio_punt(rbio, bch2_read_decrypt_err, RBIO_CONTEXT_UNBOUND, system_unbound_wq);
791 static void bch2_read_endio(struct bio *bio)
793 struct bch_read_bio *rbio =
794 container_of(bio, struct bch_read_bio, bio);
795 struct bch_fs *c = rbio->c;
796 struct bch_dev *ca = rbio->have_ioref ? bch2_dev_have_ref(c, rbio->pick.ptr.dev) : NULL;
797 struct workqueue_struct *wq = NULL;
798 enum rbio_context context = RBIO_CONTEXT_NULL;
800 if (rbio->have_ioref) {
801 bch2_latency_acct(ca, rbio->submit_time, READ);
802 percpu_ref_put(&ca->io_ref);
806 rbio->bio.bi_end_io = rbio->end_io;
808 if (unlikely(bio->bi_status)) {
809 bch2_rbio_punt(rbio, bch2_read_io_err, RBIO_CONTEXT_UNBOUND, system_unbound_wq);
813 if (((rbio->flags & BCH_READ_RETRY_IF_STALE) && race_fault()) ||
814 (ca && dev_ptr_stale(ca, &rbio->pick.ptr))) {
815 trace_and_count(c, read_reuse_race, &rbio->bio);
817 if (rbio->flags & BCH_READ_RETRY_IF_STALE)
818 bch2_rbio_error(rbio, READ_RETRY, BLK_STS_AGAIN);
820 bch2_rbio_error(rbio, READ_ERR, BLK_STS_AGAIN);
824 if (rbio->narrow_crcs ||
826 crc_is_compressed(rbio->pick.crc) ||
827 bch2_csum_type_is_encryption(rbio->pick.crc.csum_type))
828 context = RBIO_CONTEXT_UNBOUND, wq = system_unbound_wq;
829 else if (rbio->pick.crc.csum_type)
830 context = RBIO_CONTEXT_HIGHPRI, wq = system_highpri_wq;
832 bch2_rbio_punt(rbio, __bch2_read_endio, context, wq);
835 static noinline void read_from_stale_dirty_pointer(struct btree_trans *trans,
838 struct bch_extent_ptr ptr)
840 struct bch_fs *c = trans->c;
841 struct btree_iter iter;
842 struct printbuf buf = PRINTBUF;
845 bch2_trans_iter_init(trans, &iter, BTREE_ID_alloc,
846 PTR_BUCKET_POS(ca, &ptr),
849 int gen = bucket_gen_get(ca, iter.pos.offset);
851 prt_printf(&buf, "Attempting to read from stale dirty pointer:\n");
852 printbuf_indent_add(&buf, 2);
854 bch2_bkey_val_to_text(&buf, c, k);
857 prt_printf(&buf, "memory gen: %u", gen);
859 ret = lockrestart_do(trans, bkey_err(k = bch2_btree_iter_peek_slot(&iter)));
862 bch2_bkey_val_to_text(&buf, c, k);
865 prt_printf(&buf, "Attempting to read from invalid bucket %llu:%llu:\n",
866 iter.pos.inode, iter.pos.offset);
867 printbuf_indent_add(&buf, 2);
869 prt_printf(&buf, "first bucket %u nbuckets %llu\n",
870 ca->mi.first_bucket, ca->mi.nbuckets);
872 bch2_bkey_val_to_text(&buf, c, k);
876 bch2_fs_inconsistent(c, "%s", buf.buf);
878 bch2_trans_iter_exit(trans, &iter);
882 int __bch2_read_extent(struct btree_trans *trans, struct bch_read_bio *orig,
883 struct bvec_iter iter, struct bpos read_pos,
884 enum btree_id data_btree, struct bkey_s_c k,
885 unsigned offset_into_extent,
886 struct bch_io_failures *failed, unsigned flags)
888 struct bch_fs *c = trans->c;
889 struct extent_ptr_decoded pick;
890 struct bch_read_bio *rbio = NULL;
891 struct promote_op *promote = NULL;
892 bool bounce = false, read_full = false, narrow_crcs = false;
893 struct bpos data_pos = bkey_start_pos(k.k);
896 if (bkey_extent_is_inline_data(k.k)) {
897 unsigned bytes = min_t(unsigned, iter.bi_size,
898 bkey_inline_data_bytes(k.k));
900 swap(iter.bi_size, bytes);
901 memcpy_to_bio(&orig->bio, iter, bkey_inline_data_p(k));
902 swap(iter.bi_size, bytes);
903 bio_advance_iter(&orig->bio, &iter, bytes);
904 zero_fill_bio_iter(&orig->bio, iter);
908 pick_ret = bch2_bkey_pick_read_device(c, k, failed, &pick);
910 /* hole or reservation - just zero fill: */
914 if (unlikely(pick_ret < 0)) {
915 struct printbuf buf = PRINTBUF;
916 bch2_read_err_msg_trans(trans, &buf, orig, read_pos);
917 prt_printf(&buf, "no device to read from: %s\n ", bch2_err_str(pick_ret));
918 bch2_bkey_val_to_text(&buf, c, k);
920 bch_err_ratelimited(c, "%s", buf.buf);
925 if (unlikely(bch2_csum_type_is_encryption(pick.crc.csum_type)) && !c->chacha20) {
926 struct printbuf buf = PRINTBUF;
927 bch2_read_err_msg_trans(trans, &buf, orig, read_pos);
928 prt_printf(&buf, "attempting to read encrypted data without encryption key\n ");
929 bch2_bkey_val_to_text(&buf, c, k);
931 bch_err_ratelimited(c, "%s", buf.buf);
936 struct bch_dev *ca = bch2_dev_get_ioref(c, pick.ptr.dev, READ);
939 * Stale dirty pointers are treated as IO errors, but @failed isn't
940 * allocated unless we're in the retry path - so if we're not in the
941 * retry path, don't check here, it'll be caught in bch2_read_endio()
942 * and we'll end up in the retry path:
944 if ((flags & BCH_READ_IN_RETRY) &&
947 unlikely(dev_ptr_stale(ca, &pick.ptr))) {
948 read_from_stale_dirty_pointer(trans, ca, k, pick.ptr);
949 bch2_mark_io_failure(failed, &pick);
950 percpu_ref_put(&ca->io_ref);
955 * Unlock the iterator while the btree node's lock is still in
956 * cache, before doing the IO:
958 bch2_trans_unlock(trans);
960 if (flags & BCH_READ_NODECODE) {
962 * can happen if we retry, and the extent we were going to read
963 * has been merged in the meantime:
965 if (pick.crc.compressed_size > orig->bio.bi_vcnt * PAGE_SECTORS) {
967 percpu_ref_put(&ca->io_ref);
971 iter.bi_size = pick.crc.compressed_size << 9;
975 if (!(flags & BCH_READ_LAST_FRAGMENT) ||
976 bio_flagged(&orig->bio, BIO_CHAIN))
977 flags |= BCH_READ_MUST_CLONE;
979 narrow_crcs = !(flags & BCH_READ_IN_RETRY) &&
980 bch2_can_narrow_extent_crcs(k, pick.crc);
982 if (narrow_crcs && (flags & BCH_READ_USER_MAPPED))
983 flags |= BCH_READ_MUST_BOUNCE;
985 EBUG_ON(offset_into_extent + bvec_iter_sectors(iter) > k.k->size);
987 if (crc_is_compressed(pick.crc) ||
988 (pick.crc.csum_type != BCH_CSUM_none &&
989 (bvec_iter_sectors(iter) != pick.crc.uncompressed_size ||
990 (bch2_csum_type_is_encryption(pick.crc.csum_type) &&
991 (flags & BCH_READ_USER_MAPPED)) ||
992 (flags & BCH_READ_MUST_BOUNCE)))) {
997 if (orig->opts.promote_target || have_io_error(failed))
998 promote = promote_alloc(trans, iter, k, &pick, orig->opts, flags,
999 &rbio, &bounce, &read_full, failed);
1002 EBUG_ON(crc_is_compressed(pick.crc));
1003 EBUG_ON(pick.crc.csum_type &&
1004 (bvec_iter_sectors(iter) != pick.crc.uncompressed_size ||
1005 bvec_iter_sectors(iter) != pick.crc.live_size ||
1007 offset_into_extent));
1009 data_pos.offset += offset_into_extent;
1010 pick.ptr.offset += pick.crc.offset +
1012 offset_into_extent = 0;
1013 pick.crc.compressed_size = bvec_iter_sectors(iter);
1014 pick.crc.uncompressed_size = bvec_iter_sectors(iter);
1015 pick.crc.offset = 0;
1016 pick.crc.live_size = bvec_iter_sectors(iter);
1021 * promote already allocated bounce rbio:
1022 * promote needs to allocate a bio big enough for uncompressing
1023 * data in the write path, but we're not going to use it all
1026 EBUG_ON(rbio->bio.bi_iter.bi_size <
1027 pick.crc.compressed_size << 9);
1028 rbio->bio.bi_iter.bi_size =
1029 pick.crc.compressed_size << 9;
1030 } else if (bounce) {
1031 unsigned sectors = pick.crc.compressed_size;
1033 rbio = rbio_init(bio_alloc_bioset(NULL,
1034 DIV_ROUND_UP(sectors, PAGE_SECTORS),
1037 &c->bio_read_split),
1040 bch2_bio_alloc_pages_pool(c, &rbio->bio, sectors << 9);
1041 rbio->bounce = true;
1043 } else if (flags & BCH_READ_MUST_CLONE) {
1045 * Have to clone if there were any splits, due to error
1046 * reporting issues (if a split errored, and retrying didn't
1047 * work, when it reports the error to its parent (us) we don't
1048 * know if the error was from our bio, and we should retry, or
1049 * from the whole bio, in which case we don't want to retry and
1052 rbio = rbio_init(bio_alloc_clone(NULL, &orig->bio, GFP_NOFS,
1053 &c->bio_read_split),
1055 rbio->bio.bi_iter = iter;
1059 rbio->bio.bi_iter = iter;
1060 EBUG_ON(bio_flagged(&rbio->bio, BIO_CHAIN));
1063 EBUG_ON(bio_sectors(&rbio->bio) != pick.crc.compressed_size);
1066 rbio->submit_time = local_clock();
1068 rbio->parent = orig;
1070 rbio->end_io = orig->bio.bi_end_io;
1071 rbio->bvec_iter = iter;
1072 rbio->offset_into_extent= offset_into_extent;
1073 rbio->flags = flags;
1074 rbio->have_ioref = ca != NULL;
1075 rbio->narrow_crcs = narrow_crcs;
1079 /* XXX: only initialize this if needed */
1080 rbio->devs_have = bch2_bkey_devs(k);
1082 rbio->subvol = orig->subvol;
1083 rbio->read_pos = read_pos;
1084 rbio->data_btree = data_btree;
1085 rbio->data_pos = data_pos;
1086 rbio->version = k.k->bversion;
1087 rbio->promote = promote;
1088 INIT_WORK(&rbio->work, NULL);
1090 if (flags & BCH_READ_NODECODE)
1093 rbio->bio.bi_opf = orig->bio.bi_opf;
1094 rbio->bio.bi_iter.bi_sector = pick.ptr.offset;
1095 rbio->bio.bi_end_io = bch2_read_endio;
1098 trace_and_count(c, read_bounce, &rbio->bio);
1100 this_cpu_add(c->counters[BCH_COUNTER_io_read], bio_sectors(&rbio->bio));
1101 bch2_increment_clock(c, bio_sectors(&rbio->bio), READ);
1104 * If it's being moved internally, we don't want to flag it as a cache
1107 if (ca && pick.ptr.cached && !(flags & BCH_READ_NODECODE))
1108 bch2_bucket_io_time_reset(trans, pick.ptr.dev,
1109 PTR_BUCKET_NR(ca, &pick.ptr), READ);
1111 if (!(flags & (BCH_READ_IN_RETRY|BCH_READ_LAST_FRAGMENT))) {
1112 bio_inc_remaining(&orig->bio);
1113 trace_and_count(c, read_split, &orig->bio);
1116 if (!rbio->pick.idx) {
1117 if (unlikely(!rbio->have_ioref)) {
1118 struct printbuf buf = PRINTBUF;
1119 bch2_read_err_msg_trans(trans, &buf, rbio, read_pos);
1120 prt_printf(&buf, "no device to read from:\n ");
1121 bch2_bkey_val_to_text(&buf, c, k);
1123 bch_err_ratelimited(c, "%s", buf.buf);
1124 printbuf_exit(&buf);
1126 bch2_rbio_error(rbio, READ_RETRY_AVOID, BLK_STS_IOERR);
1130 this_cpu_add(ca->io_done->sectors[READ][BCH_DATA_user],
1131 bio_sectors(&rbio->bio));
1132 bio_set_dev(&rbio->bio, ca->disk_sb.bdev);
1134 if (unlikely(c->opts.no_data_io)) {
1135 if (likely(!(flags & BCH_READ_IN_RETRY)))
1136 bio_endio(&rbio->bio);
1138 if (likely(!(flags & BCH_READ_IN_RETRY)))
1139 submit_bio(&rbio->bio);
1141 submit_bio_wait(&rbio->bio);
1145 * We just submitted IO which may block, we expect relock fail
1146 * events and shouldn't count them:
1148 trans->notrace_relock_fail = true;
1150 /* Attempting reconstruct read: */
1151 if (bch2_ec_read_extent(trans, rbio, k)) {
1152 bch2_rbio_error(rbio, READ_RETRY_AVOID, BLK_STS_IOERR);
1156 if (likely(!(flags & BCH_READ_IN_RETRY)))
1157 bio_endio(&rbio->bio);
1160 if (likely(!(flags & BCH_READ_IN_RETRY))) {
1165 rbio->context = RBIO_CONTEXT_UNBOUND;
1166 bch2_read_endio(&rbio->bio);
1169 rbio = bch2_rbio_free(rbio);
1171 if (ret == READ_RETRY_AVOID) {
1172 bch2_mark_io_failure(failed, &pick);
1183 if (flags & BCH_READ_IN_RETRY)
1186 orig->bio.bi_status = BLK_STS_IOERR;
1191 * won't normally happen in the BCH_READ_NODECODE
1192 * (bch2_move_extent()) path, but if we retry and the extent we wanted
1193 * to read no longer exists we have to signal that:
1195 if (flags & BCH_READ_NODECODE)
1198 zero_fill_bio_iter(&orig->bio, iter);
1200 if (flags & BCH_READ_LAST_FRAGMENT)
1201 bch2_rbio_done(orig);
1205 void __bch2_read(struct bch_fs *c, struct bch_read_bio *rbio,
1206 struct bvec_iter bvec_iter, subvol_inum inum,
1207 struct bch_io_failures *failed, unsigned flags)
1209 struct btree_trans *trans = bch2_trans_get(c);
1210 struct btree_iter iter;
1215 BUG_ON(flags & BCH_READ_NODECODE);
1217 bch2_bkey_buf_init(&sk);
1218 bch2_trans_iter_init(trans, &iter, BTREE_ID_extents,
1219 POS(inum.inum, bvec_iter.bi_sector),
1223 enum btree_id data_btree = BTREE_ID_extents;
1225 bch2_trans_begin(trans);
1228 ret = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot);
1232 bch2_btree_iter_set_snapshot(&iter, snapshot);
1234 bch2_btree_iter_set_pos(&iter,
1235 POS(inum.inum, bvec_iter.bi_sector));
1237 k = bch2_btree_iter_peek_slot(&iter);
1242 s64 offset_into_extent = iter.pos.offset -
1243 bkey_start_offset(k.k);
1244 unsigned sectors = k.k->size - offset_into_extent;
1246 bch2_bkey_buf_reassemble(&sk, c, k);
1248 ret = bch2_read_indirect_extent(trans, &data_btree,
1249 &offset_into_extent, &sk);
1253 k = bkey_i_to_s_c(sk.k);
1256 * With indirect extents, the amount of data to read is the min
1257 * of the original extent and the indirect extent:
1259 sectors = min_t(unsigned, sectors, k.k->size - offset_into_extent);
1261 unsigned bytes = min(sectors, bvec_iter_sectors(bvec_iter)) << 9;
1262 swap(bvec_iter.bi_size, bytes);
1264 if (bvec_iter.bi_size == bytes)
1265 flags |= BCH_READ_LAST_FRAGMENT;
1267 ret = __bch2_read_extent(trans, rbio, bvec_iter, iter.pos,
1269 offset_into_extent, failed, flags);
1273 if (flags & BCH_READ_LAST_FRAGMENT)
1276 swap(bvec_iter.bi_size, bytes);
1277 bio_advance_iter(&rbio->bio, &bvec_iter, bytes);
1280 !bch2_err_matches(ret, BCH_ERR_transaction_restart) &&
1281 ret != READ_RETRY &&
1282 ret != READ_RETRY_AVOID)
1286 bch2_trans_iter_exit(trans, &iter);
1289 struct printbuf buf = PRINTBUF;
1290 bch2_inum_offset_err_msg_trans(trans, &buf, inum, bvec_iter.bi_sector << 9);
1291 prt_printf(&buf, "read error %i from btree lookup", ret);
1292 bch_err_ratelimited(c, "%s", buf.buf);
1293 printbuf_exit(&buf);
1295 rbio->bio.bi_status = BLK_STS_IOERR;
1296 bch2_rbio_done(rbio);
1299 bch2_trans_put(trans);
1300 bch2_bkey_buf_exit(&sk, c);
1303 void bch2_fs_io_read_exit(struct bch_fs *c)
1305 if (c->promote_table.tbl)
1306 rhashtable_destroy(&c->promote_table);
1307 bioset_exit(&c->bio_read_split);
1308 bioset_exit(&c->bio_read);
1311 int bch2_fs_io_read_init(struct bch_fs *c)
1313 if (bioset_init(&c->bio_read, 1, offsetof(struct bch_read_bio, bio),
1315 return -BCH_ERR_ENOMEM_bio_read_init;
1317 if (bioset_init(&c->bio_read_split, 1, offsetof(struct bch_read_bio, bio),
1319 return -BCH_ERR_ENOMEM_bio_read_split_init;
1321 if (rhashtable_init(&c->promote_table, &bch_promote_params))
1322 return -BCH_ERR_ENOMEM_promote_table_init;