xref: /linux/fs/bcachefs/io_read.c (revision feb5cc398120ce09fd7c72d361b3d14d9e280b96)
11809b8cbSKent Overstreet // SPDX-License-Identifier: GPL-2.0
21809b8cbSKent Overstreet /*
31809b8cbSKent Overstreet  * Some low level IO code, and hacks for various block layer limitations
41809b8cbSKent Overstreet  *
51809b8cbSKent Overstreet  * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
61809b8cbSKent Overstreet  * Copyright 2012 Google, Inc.
71809b8cbSKent Overstreet  */
81809b8cbSKent Overstreet 
91809b8cbSKent Overstreet #include "bcachefs.h"
101809b8cbSKent Overstreet #include "alloc_background.h"
111809b8cbSKent Overstreet #include "alloc_foreground.h"
121809b8cbSKent Overstreet #include "btree_update.h"
131809b8cbSKent Overstreet #include "buckets.h"
141809b8cbSKent Overstreet #include "checksum.h"
151809b8cbSKent Overstreet #include "clock.h"
161809b8cbSKent Overstreet #include "compress.h"
171809b8cbSKent Overstreet #include "data_update.h"
181809b8cbSKent Overstreet #include "disk_groups.h"
191809b8cbSKent Overstreet #include "ec.h"
201809b8cbSKent Overstreet #include "error.h"
211809b8cbSKent Overstreet #include "io_read.h"
221809b8cbSKent Overstreet #include "io_misc.h"
231809b8cbSKent Overstreet #include "io_write.h"
241809b8cbSKent Overstreet #include "subvolume.h"
251809b8cbSKent Overstreet #include "trace.h"
261809b8cbSKent Overstreet 
271809b8cbSKent Overstreet #ifndef CONFIG_BCACHEFS_NO_LATENCY_ACCT
281809b8cbSKent Overstreet 
291809b8cbSKent Overstreet static bool bch2_target_congested(struct bch_fs *c, u16 target)
301809b8cbSKent Overstreet {
311809b8cbSKent Overstreet 	const struct bch_devs_mask *devs;
321809b8cbSKent Overstreet 	unsigned d, nr = 0, total = 0;
331809b8cbSKent Overstreet 	u64 now = local_clock(), last;
341809b8cbSKent Overstreet 	s64 congested;
351809b8cbSKent Overstreet 	struct bch_dev *ca;
361809b8cbSKent Overstreet 
371809b8cbSKent Overstreet 	if (!target)
381809b8cbSKent Overstreet 		return false;
391809b8cbSKent Overstreet 
401809b8cbSKent Overstreet 	rcu_read_lock();
411809b8cbSKent Overstreet 	devs = bch2_target_to_mask(c, target) ?:
421809b8cbSKent Overstreet 		&c->rw_devs[BCH_DATA_user];
431809b8cbSKent Overstreet 
441809b8cbSKent Overstreet 	for_each_set_bit(d, devs->d, BCH_SB_MEMBERS_MAX) {
451809b8cbSKent Overstreet 		ca = rcu_dereference(c->devs[d]);
461809b8cbSKent Overstreet 		if (!ca)
471809b8cbSKent Overstreet 			continue;
481809b8cbSKent Overstreet 
491809b8cbSKent Overstreet 		congested = atomic_read(&ca->congested);
501809b8cbSKent Overstreet 		last = READ_ONCE(ca->congested_last);
511809b8cbSKent Overstreet 		if (time_after64(now, last))
521809b8cbSKent Overstreet 			congested -= (now - last) >> 12;
531809b8cbSKent Overstreet 
541809b8cbSKent Overstreet 		total += max(congested, 0LL);
551809b8cbSKent Overstreet 		nr++;
561809b8cbSKent Overstreet 	}
571809b8cbSKent Overstreet 	rcu_read_unlock();
581809b8cbSKent Overstreet 
591809b8cbSKent Overstreet 	return bch2_rand_range(nr * CONGESTED_MAX) < total;
601809b8cbSKent Overstreet }
611809b8cbSKent Overstreet 
621809b8cbSKent Overstreet #else
631809b8cbSKent Overstreet 
641809b8cbSKent Overstreet static bool bch2_target_congested(struct bch_fs *c, u16 target)
651809b8cbSKent Overstreet {
661809b8cbSKent Overstreet 	return false;
671809b8cbSKent Overstreet }
681809b8cbSKent Overstreet 
691809b8cbSKent Overstreet #endif
701809b8cbSKent Overstreet 
711809b8cbSKent Overstreet /* Cache promotion on read */
721809b8cbSKent Overstreet 
731809b8cbSKent Overstreet struct promote_op {
741809b8cbSKent Overstreet 	struct rcu_head		rcu;
751809b8cbSKent Overstreet 	u64			start_time;
761809b8cbSKent Overstreet 
771809b8cbSKent Overstreet 	struct rhash_head	hash;
781809b8cbSKent Overstreet 	struct bpos		pos;
791809b8cbSKent Overstreet 
801809b8cbSKent Overstreet 	struct data_update	write;
811809b8cbSKent Overstreet 	struct bio_vec		bi_inline_vecs[0]; /* must be last */
821809b8cbSKent Overstreet };
831809b8cbSKent Overstreet 
841809b8cbSKent Overstreet static const struct rhashtable_params bch_promote_params = {
851809b8cbSKent Overstreet 	.head_offset	= offsetof(struct promote_op, hash),
861809b8cbSKent Overstreet 	.key_offset	= offsetof(struct promote_op, pos),
871809b8cbSKent Overstreet 	.key_len	= sizeof(struct bpos),
881809b8cbSKent Overstreet };
891809b8cbSKent Overstreet 
90*feb5cc39SKent Overstreet static inline int should_promote(struct bch_fs *c, struct bkey_s_c k,
911809b8cbSKent Overstreet 				  struct bpos pos,
921809b8cbSKent Overstreet 				  struct bch_io_opts opts,
931809b8cbSKent Overstreet 				  unsigned flags)
941809b8cbSKent Overstreet {
95*feb5cc39SKent Overstreet 	BUG_ON(!opts.promote_target);
961809b8cbSKent Overstreet 
97*feb5cc39SKent Overstreet 	if (!(flags & BCH_READ_MAY_PROMOTE))
98*feb5cc39SKent Overstreet 		return -BCH_ERR_nopromote_may_not;
991809b8cbSKent Overstreet 
1001809b8cbSKent Overstreet 	if (bch2_bkey_has_target(c, k, opts.promote_target))
101*feb5cc39SKent Overstreet 		return -BCH_ERR_nopromote_already_promoted;
1021809b8cbSKent Overstreet 
1031809b8cbSKent Overstreet 	if (bkey_extent_is_unwritten(k))
104*feb5cc39SKent Overstreet 		return -BCH_ERR_nopromote_unwritten;
1051809b8cbSKent Overstreet 
106*feb5cc39SKent Overstreet 	if (bch2_target_congested(c, opts.promote_target))
107*feb5cc39SKent Overstreet 		return -BCH_ERR_nopromote_congested;
1081809b8cbSKent Overstreet 
1091809b8cbSKent Overstreet 	if (rhashtable_lookup_fast(&c->promote_table, &pos,
1101809b8cbSKent Overstreet 				   bch_promote_params))
111*feb5cc39SKent Overstreet 		return -BCH_ERR_nopromote_in_flight;
1121809b8cbSKent Overstreet 
113*feb5cc39SKent Overstreet 	return 0;
1141809b8cbSKent Overstreet }
1151809b8cbSKent Overstreet 
1161809b8cbSKent Overstreet static void promote_free(struct bch_fs *c, struct promote_op *op)
1171809b8cbSKent Overstreet {
1181809b8cbSKent Overstreet 	int ret;
1191809b8cbSKent Overstreet 
1201809b8cbSKent Overstreet 	bch2_data_update_exit(&op->write);
1211809b8cbSKent Overstreet 
1221809b8cbSKent Overstreet 	ret = rhashtable_remove_fast(&c->promote_table, &op->hash,
1231809b8cbSKent Overstreet 				     bch_promote_params);
1241809b8cbSKent Overstreet 	BUG_ON(ret);
1251809b8cbSKent Overstreet 	bch2_write_ref_put(c, BCH_WRITE_REF_promote);
1261809b8cbSKent Overstreet 	kfree_rcu(op, rcu);
1271809b8cbSKent Overstreet }
1281809b8cbSKent Overstreet 
1291809b8cbSKent Overstreet static void promote_done(struct bch_write_op *wop)
1301809b8cbSKent Overstreet {
1311809b8cbSKent Overstreet 	struct promote_op *op =
1321809b8cbSKent Overstreet 		container_of(wop, struct promote_op, write.op);
1331809b8cbSKent Overstreet 	struct bch_fs *c = op->write.op.c;
1341809b8cbSKent Overstreet 
1351809b8cbSKent Overstreet 	bch2_time_stats_update(&c->times[BCH_TIME_data_promote],
1361809b8cbSKent Overstreet 			       op->start_time);
1371809b8cbSKent Overstreet 	promote_free(c, op);
1381809b8cbSKent Overstreet }
1391809b8cbSKent Overstreet 
1401809b8cbSKent Overstreet static void promote_start(struct promote_op *op, struct bch_read_bio *rbio)
1411809b8cbSKent Overstreet {
1421809b8cbSKent Overstreet 	struct bio *bio = &op->write.op.wbio.bio;
1431809b8cbSKent Overstreet 
1441809b8cbSKent Overstreet 	trace_and_count(op->write.op.c, read_promote, &rbio->bio);
1451809b8cbSKent Overstreet 
1461809b8cbSKent Overstreet 	/* we now own pages: */
1471809b8cbSKent Overstreet 	BUG_ON(!rbio->bounce);
1481809b8cbSKent Overstreet 	BUG_ON(rbio->bio.bi_vcnt > bio->bi_max_vecs);
1491809b8cbSKent Overstreet 
1501809b8cbSKent Overstreet 	memcpy(bio->bi_io_vec, rbio->bio.bi_io_vec,
1511809b8cbSKent Overstreet 	       sizeof(struct bio_vec) * rbio->bio.bi_vcnt);
1521809b8cbSKent Overstreet 	swap(bio->bi_vcnt, rbio->bio.bi_vcnt);
1531809b8cbSKent Overstreet 
1541809b8cbSKent Overstreet 	bch2_data_update_read_done(&op->write, rbio->pick.crc);
1551809b8cbSKent Overstreet }
1561809b8cbSKent Overstreet 
1571809b8cbSKent Overstreet static struct promote_op *__promote_alloc(struct btree_trans *trans,
1581809b8cbSKent Overstreet 					  enum btree_id btree_id,
1591809b8cbSKent Overstreet 					  struct bkey_s_c k,
1601809b8cbSKent Overstreet 					  struct bpos pos,
1611809b8cbSKent Overstreet 					  struct extent_ptr_decoded *pick,
1621809b8cbSKent Overstreet 					  struct bch_io_opts opts,
1631809b8cbSKent Overstreet 					  unsigned sectors,
1641809b8cbSKent Overstreet 					  struct bch_read_bio **rbio)
1651809b8cbSKent Overstreet {
1661809b8cbSKent Overstreet 	struct bch_fs *c = trans->c;
1671809b8cbSKent Overstreet 	struct promote_op *op = NULL;
1681809b8cbSKent Overstreet 	struct bio *bio;
1691809b8cbSKent Overstreet 	unsigned pages = DIV_ROUND_UP(sectors, PAGE_SECTORS);
1701809b8cbSKent Overstreet 	int ret;
1711809b8cbSKent Overstreet 
1721809b8cbSKent Overstreet 	if (!bch2_write_ref_tryget(c, BCH_WRITE_REF_promote))
1731809b8cbSKent Overstreet 		return NULL;
1741809b8cbSKent Overstreet 
1751809b8cbSKent Overstreet 	op = kzalloc(sizeof(*op) + sizeof(struct bio_vec) * pages, GFP_NOFS);
1761809b8cbSKent Overstreet 	if (!op)
1771809b8cbSKent Overstreet 		goto err;
1781809b8cbSKent Overstreet 
1791809b8cbSKent Overstreet 	op->start_time = local_clock();
1801809b8cbSKent Overstreet 	op->pos = pos;
1811809b8cbSKent Overstreet 
1821809b8cbSKent Overstreet 	/*
1831809b8cbSKent Overstreet 	 * We don't use the mempool here because extents that aren't
1841809b8cbSKent Overstreet 	 * checksummed or compressed can be too big for the mempool:
1851809b8cbSKent Overstreet 	 */
1861809b8cbSKent Overstreet 	*rbio = kzalloc(sizeof(struct bch_read_bio) +
1871809b8cbSKent Overstreet 			sizeof(struct bio_vec) * pages,
1881809b8cbSKent Overstreet 			GFP_NOFS);
1891809b8cbSKent Overstreet 	if (!*rbio)
1901809b8cbSKent Overstreet 		goto err;
1911809b8cbSKent Overstreet 
1921809b8cbSKent Overstreet 	rbio_init(&(*rbio)->bio, opts);
1931809b8cbSKent Overstreet 	bio_init(&(*rbio)->bio, NULL, (*rbio)->bio.bi_inline_vecs, pages, 0);
1941809b8cbSKent Overstreet 
1951809b8cbSKent Overstreet 	if (bch2_bio_alloc_pages(&(*rbio)->bio, sectors << 9,
1961809b8cbSKent Overstreet 				 GFP_NOFS))
1971809b8cbSKent Overstreet 		goto err;
1981809b8cbSKent Overstreet 
1991809b8cbSKent Overstreet 	(*rbio)->bounce		= true;
2001809b8cbSKent Overstreet 	(*rbio)->split		= true;
2011809b8cbSKent Overstreet 	(*rbio)->kmalloc	= true;
2021809b8cbSKent Overstreet 
2031809b8cbSKent Overstreet 	if (rhashtable_lookup_insert_fast(&c->promote_table, &op->hash,
2041809b8cbSKent Overstreet 					  bch_promote_params))
2051809b8cbSKent Overstreet 		goto err;
2061809b8cbSKent Overstreet 
2071809b8cbSKent Overstreet 	bio = &op->write.op.wbio.bio;
2081809b8cbSKent Overstreet 	bio_init(bio, NULL, bio->bi_inline_vecs, pages, 0);
2091809b8cbSKent Overstreet 
2101809b8cbSKent Overstreet 	ret = bch2_data_update_init(trans, NULL, &op->write,
2111809b8cbSKent Overstreet 			writepoint_hashed((unsigned long) current),
2121809b8cbSKent Overstreet 			opts,
2131809b8cbSKent Overstreet 			(struct data_update_opts) {
2141809b8cbSKent Overstreet 				.target		= opts.promote_target,
2151809b8cbSKent Overstreet 				.extra_replicas	= 1,
2161809b8cbSKent Overstreet 				.write_flags	= BCH_WRITE_ALLOC_NOWAIT|BCH_WRITE_CACHED,
2171809b8cbSKent Overstreet 			},
2181809b8cbSKent Overstreet 			btree_id, k);
2191809b8cbSKent Overstreet 	/*
2201809b8cbSKent Overstreet 	 * possible errors: -BCH_ERR_nocow_lock_blocked,
2211809b8cbSKent Overstreet 	 * -BCH_ERR_ENOSPC_disk_reservation:
2221809b8cbSKent Overstreet 	 */
2231809b8cbSKent Overstreet 	if (ret) {
2241809b8cbSKent Overstreet 		ret = rhashtable_remove_fast(&c->promote_table, &op->hash,
2251809b8cbSKent Overstreet 					bch_promote_params);
2261809b8cbSKent Overstreet 		BUG_ON(ret);
2271809b8cbSKent Overstreet 		goto err;
2281809b8cbSKent Overstreet 	}
2291809b8cbSKent Overstreet 
2301809b8cbSKent Overstreet 	op->write.op.end_io = promote_done;
2311809b8cbSKent Overstreet 
2321809b8cbSKent Overstreet 	return op;
2331809b8cbSKent Overstreet err:
2341809b8cbSKent Overstreet 	if (*rbio)
2351809b8cbSKent Overstreet 		bio_free_pages(&(*rbio)->bio);
2361809b8cbSKent Overstreet 	kfree(*rbio);
2371809b8cbSKent Overstreet 	*rbio = NULL;
2381809b8cbSKent Overstreet 	kfree(op);
2391809b8cbSKent Overstreet 	bch2_write_ref_put(c, BCH_WRITE_REF_promote);
2401809b8cbSKent Overstreet 	return NULL;
2411809b8cbSKent Overstreet }
2421809b8cbSKent Overstreet 
2431809b8cbSKent Overstreet noinline
2441809b8cbSKent Overstreet static struct promote_op *promote_alloc(struct btree_trans *trans,
2451809b8cbSKent Overstreet 					struct bvec_iter iter,
2461809b8cbSKent Overstreet 					struct bkey_s_c k,
2471809b8cbSKent Overstreet 					struct extent_ptr_decoded *pick,
2481809b8cbSKent Overstreet 					struct bch_io_opts opts,
2491809b8cbSKent Overstreet 					unsigned flags,
2501809b8cbSKent Overstreet 					struct bch_read_bio **rbio,
2511809b8cbSKent Overstreet 					bool *bounce,
2521809b8cbSKent Overstreet 					bool *read_full)
2531809b8cbSKent Overstreet {
2541809b8cbSKent Overstreet 	struct bch_fs *c = trans->c;
2551809b8cbSKent Overstreet 	bool promote_full = *read_full || READ_ONCE(c->promote_whole_extents);
2561809b8cbSKent Overstreet 	/* data might have to be decompressed in the write path: */
2571809b8cbSKent Overstreet 	unsigned sectors = promote_full
2581809b8cbSKent Overstreet 		? max(pick->crc.compressed_size, pick->crc.live_size)
2591809b8cbSKent Overstreet 		: bvec_iter_sectors(iter);
2601809b8cbSKent Overstreet 	struct bpos pos = promote_full
2611809b8cbSKent Overstreet 		? bkey_start_pos(k.k)
2621809b8cbSKent Overstreet 		: POS(k.k->p.inode, iter.bi_sector);
2631809b8cbSKent Overstreet 	struct promote_op *promote;
264*feb5cc39SKent Overstreet 	int ret;
2651809b8cbSKent Overstreet 
266*feb5cc39SKent Overstreet 	ret = should_promote(c, k, pos, opts, flags);
267*feb5cc39SKent Overstreet 	if (ret)
268*feb5cc39SKent Overstreet 		goto nopromote;
2691809b8cbSKent Overstreet 
2701809b8cbSKent Overstreet 	promote = __promote_alloc(trans,
2711809b8cbSKent Overstreet 				  k.k->type == KEY_TYPE_reflink_v
2721809b8cbSKent Overstreet 				  ? BTREE_ID_reflink
2731809b8cbSKent Overstreet 				  : BTREE_ID_extents,
2741809b8cbSKent Overstreet 				  k, pos, pick, opts, sectors, rbio);
275*feb5cc39SKent Overstreet 	if (!promote) {
276*feb5cc39SKent Overstreet 		ret = -BCH_ERR_nopromote_enomem;
277*feb5cc39SKent Overstreet 		goto nopromote;
278*feb5cc39SKent Overstreet 	}
2791809b8cbSKent Overstreet 
2801809b8cbSKent Overstreet 	*bounce		= true;
2811809b8cbSKent Overstreet 	*read_full	= promote_full;
2821809b8cbSKent Overstreet 	return promote;
283*feb5cc39SKent Overstreet nopromote:
284*feb5cc39SKent Overstreet 	trace_read_nopromote(c, ret);
285*feb5cc39SKent Overstreet 	return NULL;
2861809b8cbSKent Overstreet }
2871809b8cbSKent Overstreet 
2881809b8cbSKent Overstreet /* Read */
2891809b8cbSKent Overstreet 
2901809b8cbSKent Overstreet #define READ_RETRY_AVOID	1
2911809b8cbSKent Overstreet #define READ_RETRY		2
2921809b8cbSKent Overstreet #define READ_ERR		3
2931809b8cbSKent Overstreet 
2941809b8cbSKent Overstreet enum rbio_context {
2951809b8cbSKent Overstreet 	RBIO_CONTEXT_NULL,
2961809b8cbSKent Overstreet 	RBIO_CONTEXT_HIGHPRI,
2971809b8cbSKent Overstreet 	RBIO_CONTEXT_UNBOUND,
2981809b8cbSKent Overstreet };
2991809b8cbSKent Overstreet 
3001809b8cbSKent Overstreet static inline struct bch_read_bio *
3011809b8cbSKent Overstreet bch2_rbio_parent(struct bch_read_bio *rbio)
3021809b8cbSKent Overstreet {
3031809b8cbSKent Overstreet 	return rbio->split ? rbio->parent : rbio;
3041809b8cbSKent Overstreet }
3051809b8cbSKent Overstreet 
3061809b8cbSKent Overstreet __always_inline
3071809b8cbSKent Overstreet static void bch2_rbio_punt(struct bch_read_bio *rbio, work_func_t fn,
3081809b8cbSKent Overstreet 			   enum rbio_context context,
3091809b8cbSKent Overstreet 			   struct workqueue_struct *wq)
3101809b8cbSKent Overstreet {
3111809b8cbSKent Overstreet 	if (context <= rbio->context) {
3121809b8cbSKent Overstreet 		fn(&rbio->work);
3131809b8cbSKent Overstreet 	} else {
3141809b8cbSKent Overstreet 		rbio->work.func		= fn;
3151809b8cbSKent Overstreet 		rbio->context		= context;
3161809b8cbSKent Overstreet 		queue_work(wq, &rbio->work);
3171809b8cbSKent Overstreet 	}
3181809b8cbSKent Overstreet }
3191809b8cbSKent Overstreet 
3201809b8cbSKent Overstreet static inline struct bch_read_bio *bch2_rbio_free(struct bch_read_bio *rbio)
3211809b8cbSKent Overstreet {
3221809b8cbSKent Overstreet 	BUG_ON(rbio->bounce && !rbio->split);
3231809b8cbSKent Overstreet 
3241809b8cbSKent Overstreet 	if (rbio->promote)
3251809b8cbSKent Overstreet 		promote_free(rbio->c, rbio->promote);
3261809b8cbSKent Overstreet 	rbio->promote = NULL;
3271809b8cbSKent Overstreet 
3281809b8cbSKent Overstreet 	if (rbio->bounce)
3291809b8cbSKent Overstreet 		bch2_bio_free_pages_pool(rbio->c, &rbio->bio);
3301809b8cbSKent Overstreet 
3311809b8cbSKent Overstreet 	if (rbio->split) {
3321809b8cbSKent Overstreet 		struct bch_read_bio *parent = rbio->parent;
3331809b8cbSKent Overstreet 
3341809b8cbSKent Overstreet 		if (rbio->kmalloc)
3351809b8cbSKent Overstreet 			kfree(rbio);
3361809b8cbSKent Overstreet 		else
3371809b8cbSKent Overstreet 			bio_put(&rbio->bio);
3381809b8cbSKent Overstreet 
3391809b8cbSKent Overstreet 		rbio = parent;
3401809b8cbSKent Overstreet 	}
3411809b8cbSKent Overstreet 
3421809b8cbSKent Overstreet 	return rbio;
3431809b8cbSKent Overstreet }
3441809b8cbSKent Overstreet 
3451809b8cbSKent Overstreet /*
3461809b8cbSKent Overstreet  * Only called on a top level bch_read_bio to complete an entire read request,
3471809b8cbSKent Overstreet  * not a split:
3481809b8cbSKent Overstreet  */
3491809b8cbSKent Overstreet static void bch2_rbio_done(struct bch_read_bio *rbio)
3501809b8cbSKent Overstreet {
3511809b8cbSKent Overstreet 	if (rbio->start_time)
3521809b8cbSKent Overstreet 		bch2_time_stats_update(&rbio->c->times[BCH_TIME_data_read],
3531809b8cbSKent Overstreet 				       rbio->start_time);
3541809b8cbSKent Overstreet 	bio_endio(&rbio->bio);
3551809b8cbSKent Overstreet }
3561809b8cbSKent Overstreet 
3571809b8cbSKent Overstreet static void bch2_read_retry_nodecode(struct bch_fs *c, struct bch_read_bio *rbio,
3581809b8cbSKent Overstreet 				     struct bvec_iter bvec_iter,
3591809b8cbSKent Overstreet 				     struct bch_io_failures *failed,
3601809b8cbSKent Overstreet 				     unsigned flags)
3611809b8cbSKent Overstreet {
3621809b8cbSKent Overstreet 	struct btree_trans trans;
3631809b8cbSKent Overstreet 	struct btree_iter iter;
3641809b8cbSKent Overstreet 	struct bkey_buf sk;
3651809b8cbSKent Overstreet 	struct bkey_s_c k;
3661809b8cbSKent Overstreet 	int ret;
3671809b8cbSKent Overstreet 
3681809b8cbSKent Overstreet 	flags &= ~BCH_READ_LAST_FRAGMENT;
3691809b8cbSKent Overstreet 	flags |= BCH_READ_MUST_CLONE;
3701809b8cbSKent Overstreet 
3711809b8cbSKent Overstreet 	bch2_bkey_buf_init(&sk);
3721809b8cbSKent Overstreet 	bch2_trans_init(&trans, c, 0, 0);
3731809b8cbSKent Overstreet 
3741809b8cbSKent Overstreet 	bch2_trans_iter_init(&trans, &iter, rbio->data_btree,
3751809b8cbSKent Overstreet 			     rbio->read_pos, BTREE_ITER_SLOTS);
3761809b8cbSKent Overstreet retry:
3771809b8cbSKent Overstreet 	rbio->bio.bi_status = 0;
3781809b8cbSKent Overstreet 
3791809b8cbSKent Overstreet 	k = bch2_btree_iter_peek_slot(&iter);
3801809b8cbSKent Overstreet 	if (bkey_err(k))
3811809b8cbSKent Overstreet 		goto err;
3821809b8cbSKent Overstreet 
3831809b8cbSKent Overstreet 	bch2_bkey_buf_reassemble(&sk, c, k);
3841809b8cbSKent Overstreet 	k = bkey_i_to_s_c(sk.k);
3851809b8cbSKent Overstreet 	bch2_trans_unlock(&trans);
3861809b8cbSKent Overstreet 
3871809b8cbSKent Overstreet 	if (!bch2_bkey_matches_ptr(c, k,
3881809b8cbSKent Overstreet 				   rbio->pick.ptr,
3891809b8cbSKent Overstreet 				   rbio->data_pos.offset -
3901809b8cbSKent Overstreet 				   rbio->pick.crc.offset)) {
3911809b8cbSKent Overstreet 		/* extent we wanted to read no longer exists: */
3921809b8cbSKent Overstreet 		rbio->hole = true;
3931809b8cbSKent Overstreet 		goto out;
3941809b8cbSKent Overstreet 	}
3951809b8cbSKent Overstreet 
3961809b8cbSKent Overstreet 	ret = __bch2_read_extent(&trans, rbio, bvec_iter,
3971809b8cbSKent Overstreet 				 rbio->read_pos,
3981809b8cbSKent Overstreet 				 rbio->data_btree,
3991809b8cbSKent Overstreet 				 k, 0, failed, flags);
4001809b8cbSKent Overstreet 	if (ret == READ_RETRY)
4011809b8cbSKent Overstreet 		goto retry;
4021809b8cbSKent Overstreet 	if (ret)
4031809b8cbSKent Overstreet 		goto err;
4041809b8cbSKent Overstreet out:
4051809b8cbSKent Overstreet 	bch2_rbio_done(rbio);
4061809b8cbSKent Overstreet 	bch2_trans_iter_exit(&trans, &iter);
4071809b8cbSKent Overstreet 	bch2_trans_exit(&trans);
4081809b8cbSKent Overstreet 	bch2_bkey_buf_exit(&sk, c);
4091809b8cbSKent Overstreet 	return;
4101809b8cbSKent Overstreet err:
4111809b8cbSKent Overstreet 	rbio->bio.bi_status = BLK_STS_IOERR;
4121809b8cbSKent Overstreet 	goto out;
4131809b8cbSKent Overstreet }
4141809b8cbSKent Overstreet 
4151809b8cbSKent Overstreet static void bch2_rbio_retry(struct work_struct *work)
4161809b8cbSKent Overstreet {
4171809b8cbSKent Overstreet 	struct bch_read_bio *rbio =
4181809b8cbSKent Overstreet 		container_of(work, struct bch_read_bio, work);
4191809b8cbSKent Overstreet 	struct bch_fs *c	= rbio->c;
4201809b8cbSKent Overstreet 	struct bvec_iter iter	= rbio->bvec_iter;
4211809b8cbSKent Overstreet 	unsigned flags		= rbio->flags;
4221809b8cbSKent Overstreet 	subvol_inum inum = {
4231809b8cbSKent Overstreet 		.subvol = rbio->subvol,
4241809b8cbSKent Overstreet 		.inum	= rbio->read_pos.inode,
4251809b8cbSKent Overstreet 	};
4261809b8cbSKent Overstreet 	struct bch_io_failures failed = { .nr = 0 };
4271809b8cbSKent Overstreet 
4281809b8cbSKent Overstreet 	trace_and_count(c, read_retry, &rbio->bio);
4291809b8cbSKent Overstreet 
4301809b8cbSKent Overstreet 	if (rbio->retry == READ_RETRY_AVOID)
4311809b8cbSKent Overstreet 		bch2_mark_io_failure(&failed, &rbio->pick);
4321809b8cbSKent Overstreet 
4331809b8cbSKent Overstreet 	rbio->bio.bi_status = 0;
4341809b8cbSKent Overstreet 
4351809b8cbSKent Overstreet 	rbio = bch2_rbio_free(rbio);
4361809b8cbSKent Overstreet 
4371809b8cbSKent Overstreet 	flags |= BCH_READ_IN_RETRY;
4381809b8cbSKent Overstreet 	flags &= ~BCH_READ_MAY_PROMOTE;
4391809b8cbSKent Overstreet 
4401809b8cbSKent Overstreet 	if (flags & BCH_READ_NODECODE) {
4411809b8cbSKent Overstreet 		bch2_read_retry_nodecode(c, rbio, iter, &failed, flags);
4421809b8cbSKent Overstreet 	} else {
4431809b8cbSKent Overstreet 		flags &= ~BCH_READ_LAST_FRAGMENT;
4441809b8cbSKent Overstreet 		flags |= BCH_READ_MUST_CLONE;
4451809b8cbSKent Overstreet 
4461809b8cbSKent Overstreet 		__bch2_read(c, rbio, iter, inum, &failed, flags);
4471809b8cbSKent Overstreet 	}
4481809b8cbSKent Overstreet }
4491809b8cbSKent Overstreet 
4501809b8cbSKent Overstreet static void bch2_rbio_error(struct bch_read_bio *rbio, int retry,
4511809b8cbSKent Overstreet 			    blk_status_t error)
4521809b8cbSKent Overstreet {
4531809b8cbSKent Overstreet 	rbio->retry = retry;
4541809b8cbSKent Overstreet 
4551809b8cbSKent Overstreet 	if (rbio->flags & BCH_READ_IN_RETRY)
4561809b8cbSKent Overstreet 		return;
4571809b8cbSKent Overstreet 
4581809b8cbSKent Overstreet 	if (retry == READ_ERR) {
4591809b8cbSKent Overstreet 		rbio = bch2_rbio_free(rbio);
4601809b8cbSKent Overstreet 
4611809b8cbSKent Overstreet 		rbio->bio.bi_status = error;
4621809b8cbSKent Overstreet 		bch2_rbio_done(rbio);
4631809b8cbSKent Overstreet 	} else {
4641809b8cbSKent Overstreet 		bch2_rbio_punt(rbio, bch2_rbio_retry,
4651809b8cbSKent Overstreet 			       RBIO_CONTEXT_UNBOUND, system_unbound_wq);
4661809b8cbSKent Overstreet 	}
4671809b8cbSKent Overstreet }
4681809b8cbSKent Overstreet 
4691809b8cbSKent Overstreet static int __bch2_rbio_narrow_crcs(struct btree_trans *trans,
4701809b8cbSKent Overstreet 				   struct bch_read_bio *rbio)
4711809b8cbSKent Overstreet {
4721809b8cbSKent Overstreet 	struct bch_fs *c = rbio->c;
4731809b8cbSKent Overstreet 	u64 data_offset = rbio->data_pos.offset - rbio->pick.crc.offset;
4741809b8cbSKent Overstreet 	struct bch_extent_crc_unpacked new_crc;
4751809b8cbSKent Overstreet 	struct btree_iter iter;
4761809b8cbSKent Overstreet 	struct bkey_i *new;
4771809b8cbSKent Overstreet 	struct bkey_s_c k;
4781809b8cbSKent Overstreet 	int ret = 0;
4791809b8cbSKent Overstreet 
4801809b8cbSKent Overstreet 	if (crc_is_compressed(rbio->pick.crc))
4811809b8cbSKent Overstreet 		return 0;
4821809b8cbSKent Overstreet 
4831809b8cbSKent Overstreet 	k = bch2_bkey_get_iter(trans, &iter, rbio->data_btree, rbio->data_pos,
4841809b8cbSKent Overstreet 			       BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
4851809b8cbSKent Overstreet 	if ((ret = bkey_err(k)))
4861809b8cbSKent Overstreet 		goto out;
4871809b8cbSKent Overstreet 
4881809b8cbSKent Overstreet 	if (bversion_cmp(k.k->version, rbio->version) ||
4891809b8cbSKent Overstreet 	    !bch2_bkey_matches_ptr(c, k, rbio->pick.ptr, data_offset))
4901809b8cbSKent Overstreet 		goto out;
4911809b8cbSKent Overstreet 
4921809b8cbSKent Overstreet 	/* Extent was merged? */
4931809b8cbSKent Overstreet 	if (bkey_start_offset(k.k) < data_offset ||
4941809b8cbSKent Overstreet 	    k.k->p.offset > data_offset + rbio->pick.crc.uncompressed_size)
4951809b8cbSKent Overstreet 		goto out;
4961809b8cbSKent Overstreet 
4971809b8cbSKent Overstreet 	if (bch2_rechecksum_bio(c, &rbio->bio, rbio->version,
4981809b8cbSKent Overstreet 			rbio->pick.crc, NULL, &new_crc,
4991809b8cbSKent Overstreet 			bkey_start_offset(k.k) - data_offset, k.k->size,
5001809b8cbSKent Overstreet 			rbio->pick.crc.csum_type)) {
5011809b8cbSKent Overstreet 		bch_err(c, "error verifying existing checksum while narrowing checksum (memory corruption?)");
5021809b8cbSKent Overstreet 		ret = 0;
5031809b8cbSKent Overstreet 		goto out;
5041809b8cbSKent Overstreet 	}
5051809b8cbSKent Overstreet 
5061809b8cbSKent Overstreet 	/*
5071809b8cbSKent Overstreet 	 * going to be temporarily appending another checksum entry:
5081809b8cbSKent Overstreet 	 */
5091809b8cbSKent Overstreet 	new = bch2_trans_kmalloc(trans, bkey_bytes(k.k) +
5101809b8cbSKent Overstreet 				 sizeof(struct bch_extent_crc128));
5111809b8cbSKent Overstreet 	if ((ret = PTR_ERR_OR_ZERO(new)))
5121809b8cbSKent Overstreet 		goto out;
5131809b8cbSKent Overstreet 
5141809b8cbSKent Overstreet 	bkey_reassemble(new, k);
5151809b8cbSKent Overstreet 
5161809b8cbSKent Overstreet 	if (!bch2_bkey_narrow_crcs(new, new_crc))
5171809b8cbSKent Overstreet 		goto out;
5181809b8cbSKent Overstreet 
5191809b8cbSKent Overstreet 	ret = bch2_trans_update(trans, &iter, new,
5201809b8cbSKent Overstreet 				BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
5211809b8cbSKent Overstreet out:
5221809b8cbSKent Overstreet 	bch2_trans_iter_exit(trans, &iter);
5231809b8cbSKent Overstreet 	return ret;
5241809b8cbSKent Overstreet }
5251809b8cbSKent Overstreet 
5261809b8cbSKent Overstreet static noinline void bch2_rbio_narrow_crcs(struct bch_read_bio *rbio)
5271809b8cbSKent Overstreet {
5281809b8cbSKent Overstreet 	bch2_trans_do(rbio->c, NULL, NULL, BTREE_INSERT_NOFAIL,
5291809b8cbSKent Overstreet 		      __bch2_rbio_narrow_crcs(&trans, rbio));
5301809b8cbSKent Overstreet }
5311809b8cbSKent Overstreet 
5321809b8cbSKent Overstreet /* Inner part that may run in process context */
5331809b8cbSKent Overstreet static void __bch2_read_endio(struct work_struct *work)
5341809b8cbSKent Overstreet {
5351809b8cbSKent Overstreet 	struct bch_read_bio *rbio =
5361809b8cbSKent Overstreet 		container_of(work, struct bch_read_bio, work);
5371809b8cbSKent Overstreet 	struct bch_fs *c	= rbio->c;
5381809b8cbSKent Overstreet 	struct bch_dev *ca	= bch_dev_bkey_exists(c, rbio->pick.ptr.dev);
5391809b8cbSKent Overstreet 	struct bio *src		= &rbio->bio;
5401809b8cbSKent Overstreet 	struct bio *dst		= &bch2_rbio_parent(rbio)->bio;
5411809b8cbSKent Overstreet 	struct bvec_iter dst_iter = rbio->bvec_iter;
5421809b8cbSKent Overstreet 	struct bch_extent_crc_unpacked crc = rbio->pick.crc;
5431809b8cbSKent Overstreet 	struct nonce nonce = extent_nonce(rbio->version, crc);
5441809b8cbSKent Overstreet 	unsigned nofs_flags;
5451809b8cbSKent Overstreet 	struct bch_csum csum;
5461809b8cbSKent Overstreet 	int ret;
5471809b8cbSKent Overstreet 
5481809b8cbSKent Overstreet 	nofs_flags = memalloc_nofs_save();
5491809b8cbSKent Overstreet 
5501809b8cbSKent Overstreet 	/* Reset iterator for checksumming and copying bounced data: */
5511809b8cbSKent Overstreet 	if (rbio->bounce) {
5521809b8cbSKent Overstreet 		src->bi_iter.bi_size		= crc.compressed_size << 9;
5531809b8cbSKent Overstreet 		src->bi_iter.bi_idx		= 0;
5541809b8cbSKent Overstreet 		src->bi_iter.bi_bvec_done	= 0;
5551809b8cbSKent Overstreet 	} else {
5561809b8cbSKent Overstreet 		src->bi_iter			= rbio->bvec_iter;
5571809b8cbSKent Overstreet 	}
5581809b8cbSKent Overstreet 
5591809b8cbSKent Overstreet 	csum = bch2_checksum_bio(c, crc.csum_type, nonce, src);
5601809b8cbSKent Overstreet 	if (bch2_crc_cmp(csum, rbio->pick.crc.csum) && !c->opts.no_data_io)
5611809b8cbSKent Overstreet 		goto csum_err;
5621809b8cbSKent Overstreet 
5631809b8cbSKent Overstreet 	/*
5641809b8cbSKent Overstreet 	 * XXX
5651809b8cbSKent Overstreet 	 * We need to rework the narrow_crcs path to deliver the read completion
5661809b8cbSKent Overstreet 	 * first, and then punt to a different workqueue, otherwise we're
5671809b8cbSKent Overstreet 	 * holding up reads while doing btree updates which is bad for memory
5681809b8cbSKent Overstreet 	 * reclaim.
5691809b8cbSKent Overstreet 	 */
5701809b8cbSKent Overstreet 	if (unlikely(rbio->narrow_crcs))
5711809b8cbSKent Overstreet 		bch2_rbio_narrow_crcs(rbio);
5721809b8cbSKent Overstreet 
5731809b8cbSKent Overstreet 	if (rbio->flags & BCH_READ_NODECODE)
5741809b8cbSKent Overstreet 		goto nodecode;
5751809b8cbSKent Overstreet 
5761809b8cbSKent Overstreet 	/* Adjust crc to point to subset of data we want: */
5771809b8cbSKent Overstreet 	crc.offset     += rbio->offset_into_extent;
5781809b8cbSKent Overstreet 	crc.live_size	= bvec_iter_sectors(rbio->bvec_iter);
5791809b8cbSKent Overstreet 
5801809b8cbSKent Overstreet 	if (crc_is_compressed(crc)) {
5811809b8cbSKent Overstreet 		ret = bch2_encrypt_bio(c, crc.csum_type, nonce, src);
5821809b8cbSKent Overstreet 		if (ret)
5831809b8cbSKent Overstreet 			goto decrypt_err;
5841809b8cbSKent Overstreet 
5851809b8cbSKent Overstreet 		if (bch2_bio_uncompress(c, src, dst, dst_iter, crc) &&
5861809b8cbSKent Overstreet 		    !c->opts.no_data_io)
5871809b8cbSKent Overstreet 			goto decompression_err;
5881809b8cbSKent Overstreet 	} else {
5891809b8cbSKent Overstreet 		/* don't need to decrypt the entire bio: */
5901809b8cbSKent Overstreet 		nonce = nonce_add(nonce, crc.offset << 9);
5911809b8cbSKent Overstreet 		bio_advance(src, crc.offset << 9);
5921809b8cbSKent Overstreet 
5931809b8cbSKent Overstreet 		BUG_ON(src->bi_iter.bi_size < dst_iter.bi_size);
5941809b8cbSKent Overstreet 		src->bi_iter.bi_size = dst_iter.bi_size;
5951809b8cbSKent Overstreet 
5961809b8cbSKent Overstreet 		ret = bch2_encrypt_bio(c, crc.csum_type, nonce, src);
5971809b8cbSKent Overstreet 		if (ret)
5981809b8cbSKent Overstreet 			goto decrypt_err;
5991809b8cbSKent Overstreet 
6001809b8cbSKent Overstreet 		if (rbio->bounce) {
6011809b8cbSKent Overstreet 			struct bvec_iter src_iter = src->bi_iter;
6021809b8cbSKent Overstreet 
6031809b8cbSKent Overstreet 			bio_copy_data_iter(dst, &dst_iter, src, &src_iter);
6041809b8cbSKent Overstreet 		}
6051809b8cbSKent Overstreet 	}
6061809b8cbSKent Overstreet 
6071809b8cbSKent Overstreet 	if (rbio->promote) {
6081809b8cbSKent Overstreet 		/*
6091809b8cbSKent Overstreet 		 * Re encrypt data we decrypted, so it's consistent with
6101809b8cbSKent Overstreet 		 * rbio->crc:
6111809b8cbSKent Overstreet 		 */
6121809b8cbSKent Overstreet 		ret = bch2_encrypt_bio(c, crc.csum_type, nonce, src);
6131809b8cbSKent Overstreet 		if (ret)
6141809b8cbSKent Overstreet 			goto decrypt_err;
6151809b8cbSKent Overstreet 
6161809b8cbSKent Overstreet 		promote_start(rbio->promote, rbio);
6171809b8cbSKent Overstreet 		rbio->promote = NULL;
6181809b8cbSKent Overstreet 	}
6191809b8cbSKent Overstreet nodecode:
6201809b8cbSKent Overstreet 	if (likely(!(rbio->flags & BCH_READ_IN_RETRY))) {
6211809b8cbSKent Overstreet 		rbio = bch2_rbio_free(rbio);
6221809b8cbSKent Overstreet 		bch2_rbio_done(rbio);
6231809b8cbSKent Overstreet 	}
6241809b8cbSKent Overstreet out:
6251809b8cbSKent Overstreet 	memalloc_nofs_restore(nofs_flags);
6261809b8cbSKent Overstreet 	return;
6271809b8cbSKent Overstreet csum_err:
6281809b8cbSKent Overstreet 	/*
6291809b8cbSKent Overstreet 	 * Checksum error: if the bio wasn't bounced, we may have been
6301809b8cbSKent Overstreet 	 * reading into buffers owned by userspace (that userspace can
6311809b8cbSKent Overstreet 	 * scribble over) - retry the read, bouncing it this time:
6321809b8cbSKent Overstreet 	 */
6331809b8cbSKent Overstreet 	if (!rbio->bounce && (rbio->flags & BCH_READ_USER_MAPPED)) {
6341809b8cbSKent Overstreet 		rbio->flags |= BCH_READ_MUST_BOUNCE;
6351809b8cbSKent Overstreet 		bch2_rbio_error(rbio, READ_RETRY, BLK_STS_IOERR);
6361809b8cbSKent Overstreet 		goto out;
6371809b8cbSKent Overstreet 	}
6381809b8cbSKent Overstreet 
6391809b8cbSKent Overstreet 	bch_err_inum_offset_ratelimited(ca,
6401809b8cbSKent Overstreet 		rbio->read_pos.inode,
6411809b8cbSKent Overstreet 		rbio->read_pos.offset << 9,
6421809b8cbSKent Overstreet 		"data checksum error: expected %0llx:%0llx got %0llx:%0llx (type %s)",
6431809b8cbSKent Overstreet 		rbio->pick.crc.csum.hi, rbio->pick.crc.csum.lo,
6441809b8cbSKent Overstreet 		csum.hi, csum.lo, bch2_csum_types[crc.csum_type]);
6451809b8cbSKent Overstreet 	bch2_io_error(ca);
6461809b8cbSKent Overstreet 	bch2_rbio_error(rbio, READ_RETRY_AVOID, BLK_STS_IOERR);
6471809b8cbSKent Overstreet 	goto out;
6481809b8cbSKent Overstreet decompression_err:
6491809b8cbSKent Overstreet 	bch_err_inum_offset_ratelimited(c, rbio->read_pos.inode,
6501809b8cbSKent Overstreet 					rbio->read_pos.offset << 9,
6511809b8cbSKent Overstreet 					"decompression error");
6521809b8cbSKent Overstreet 	bch2_rbio_error(rbio, READ_ERR, BLK_STS_IOERR);
6531809b8cbSKent Overstreet 	goto out;
6541809b8cbSKent Overstreet decrypt_err:
6551809b8cbSKent Overstreet 	bch_err_inum_offset_ratelimited(c, rbio->read_pos.inode,
6561809b8cbSKent Overstreet 					rbio->read_pos.offset << 9,
6571809b8cbSKent Overstreet 					"decrypt error");
6581809b8cbSKent Overstreet 	bch2_rbio_error(rbio, READ_ERR, BLK_STS_IOERR);
6591809b8cbSKent Overstreet 	goto out;
6601809b8cbSKent Overstreet }
6611809b8cbSKent Overstreet 
6621809b8cbSKent Overstreet static void bch2_read_endio(struct bio *bio)
6631809b8cbSKent Overstreet {
6641809b8cbSKent Overstreet 	struct bch_read_bio *rbio =
6651809b8cbSKent Overstreet 		container_of(bio, struct bch_read_bio, bio);
6661809b8cbSKent Overstreet 	struct bch_fs *c	= rbio->c;
6671809b8cbSKent Overstreet 	struct bch_dev *ca	= bch_dev_bkey_exists(c, rbio->pick.ptr.dev);
6681809b8cbSKent Overstreet 	struct workqueue_struct *wq = NULL;
6691809b8cbSKent Overstreet 	enum rbio_context context = RBIO_CONTEXT_NULL;
6701809b8cbSKent Overstreet 
6711809b8cbSKent Overstreet 	if (rbio->have_ioref) {
6721809b8cbSKent Overstreet 		bch2_latency_acct(ca, rbio->submit_time, READ);
6731809b8cbSKent Overstreet 		percpu_ref_put(&ca->io_ref);
6741809b8cbSKent Overstreet 	}
6751809b8cbSKent Overstreet 
6761809b8cbSKent Overstreet 	if (!rbio->split)
6771809b8cbSKent Overstreet 		rbio->bio.bi_end_io = rbio->end_io;
6781809b8cbSKent Overstreet 
6791809b8cbSKent Overstreet 	if (bch2_dev_inum_io_err_on(bio->bi_status, ca,
6801809b8cbSKent Overstreet 				    rbio->read_pos.inode,
6811809b8cbSKent Overstreet 				    rbio->read_pos.offset,
6821809b8cbSKent Overstreet 				    "data read error: %s",
6831809b8cbSKent Overstreet 			       bch2_blk_status_to_str(bio->bi_status))) {
6841809b8cbSKent Overstreet 		bch2_rbio_error(rbio, READ_RETRY_AVOID, bio->bi_status);
6851809b8cbSKent Overstreet 		return;
6861809b8cbSKent Overstreet 	}
6871809b8cbSKent Overstreet 
6881809b8cbSKent Overstreet 	if (((rbio->flags & BCH_READ_RETRY_IF_STALE) && race_fault()) ||
6891809b8cbSKent Overstreet 	    ptr_stale(ca, &rbio->pick.ptr)) {
6901809b8cbSKent Overstreet 		trace_and_count(c, read_reuse_race, &rbio->bio);
6911809b8cbSKent Overstreet 
6921809b8cbSKent Overstreet 		if (rbio->flags & BCH_READ_RETRY_IF_STALE)
6931809b8cbSKent Overstreet 			bch2_rbio_error(rbio, READ_RETRY, BLK_STS_AGAIN);
6941809b8cbSKent Overstreet 		else
6951809b8cbSKent Overstreet 			bch2_rbio_error(rbio, READ_ERR, BLK_STS_AGAIN);
6961809b8cbSKent Overstreet 		return;
6971809b8cbSKent Overstreet 	}
6981809b8cbSKent Overstreet 
6991809b8cbSKent Overstreet 	if (rbio->narrow_crcs ||
7001809b8cbSKent Overstreet 	    rbio->promote ||
7011809b8cbSKent Overstreet 	    crc_is_compressed(rbio->pick.crc) ||
7021809b8cbSKent Overstreet 	    bch2_csum_type_is_encryption(rbio->pick.crc.csum_type))
7031809b8cbSKent Overstreet 		context = RBIO_CONTEXT_UNBOUND,	wq = system_unbound_wq;
7041809b8cbSKent Overstreet 	else if (rbio->pick.crc.csum_type)
7051809b8cbSKent Overstreet 		context = RBIO_CONTEXT_HIGHPRI,	wq = system_highpri_wq;
7061809b8cbSKent Overstreet 
7071809b8cbSKent Overstreet 	bch2_rbio_punt(rbio, __bch2_read_endio, context, wq);
7081809b8cbSKent Overstreet }
7091809b8cbSKent Overstreet 
7101809b8cbSKent Overstreet int __bch2_read_indirect_extent(struct btree_trans *trans,
7111809b8cbSKent Overstreet 				unsigned *offset_into_extent,
7121809b8cbSKent Overstreet 				struct bkey_buf *orig_k)
7131809b8cbSKent Overstreet {
7141809b8cbSKent Overstreet 	struct btree_iter iter;
7151809b8cbSKent Overstreet 	struct bkey_s_c k;
7161809b8cbSKent Overstreet 	u64 reflink_offset;
7171809b8cbSKent Overstreet 	int ret;
7181809b8cbSKent Overstreet 
7191809b8cbSKent Overstreet 	reflink_offset = le64_to_cpu(bkey_i_to_reflink_p(orig_k->k)->v.idx) +
7201809b8cbSKent Overstreet 		*offset_into_extent;
7211809b8cbSKent Overstreet 
7221809b8cbSKent Overstreet 	k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_reflink,
7231809b8cbSKent Overstreet 			       POS(0, reflink_offset), 0);
7241809b8cbSKent Overstreet 	ret = bkey_err(k);
7251809b8cbSKent Overstreet 	if (ret)
7261809b8cbSKent Overstreet 		goto err;
7271809b8cbSKent Overstreet 
7281809b8cbSKent Overstreet 	if (k.k->type != KEY_TYPE_reflink_v &&
7291809b8cbSKent Overstreet 	    k.k->type != KEY_TYPE_indirect_inline_data) {
7301809b8cbSKent Overstreet 		bch_err_inum_offset_ratelimited(trans->c,
7311809b8cbSKent Overstreet 			orig_k->k->k.p.inode,
7321809b8cbSKent Overstreet 			orig_k->k->k.p.offset << 9,
7331809b8cbSKent Overstreet 			"%llu len %u points to nonexistent indirect extent %llu",
7341809b8cbSKent Overstreet 			orig_k->k->k.p.offset,
7351809b8cbSKent Overstreet 			orig_k->k->k.size,
7361809b8cbSKent Overstreet 			reflink_offset);
7371809b8cbSKent Overstreet 		bch2_inconsistent_error(trans->c);
7381809b8cbSKent Overstreet 		ret = -EIO;
7391809b8cbSKent Overstreet 		goto err;
7401809b8cbSKent Overstreet 	}
7411809b8cbSKent Overstreet 
7421809b8cbSKent Overstreet 	*offset_into_extent = iter.pos.offset - bkey_start_offset(k.k);
7431809b8cbSKent Overstreet 	bch2_bkey_buf_reassemble(orig_k, trans->c, k);
7441809b8cbSKent Overstreet err:
7451809b8cbSKent Overstreet 	bch2_trans_iter_exit(trans, &iter);
7461809b8cbSKent Overstreet 	return ret;
7471809b8cbSKent Overstreet }
7481809b8cbSKent Overstreet 
7491809b8cbSKent Overstreet static noinline void read_from_stale_dirty_pointer(struct btree_trans *trans,
7501809b8cbSKent Overstreet 						   struct bkey_s_c k,
7511809b8cbSKent Overstreet 						   struct bch_extent_ptr ptr)
7521809b8cbSKent Overstreet {
7531809b8cbSKent Overstreet 	struct bch_fs *c = trans->c;
7541809b8cbSKent Overstreet 	struct bch_dev *ca = bch_dev_bkey_exists(c, ptr.dev);
7551809b8cbSKent Overstreet 	struct btree_iter iter;
7561809b8cbSKent Overstreet 	struct printbuf buf = PRINTBUF;
7571809b8cbSKent Overstreet 	int ret;
7581809b8cbSKent Overstreet 
7591809b8cbSKent Overstreet 	bch2_trans_iter_init(trans, &iter, BTREE_ID_alloc,
7601809b8cbSKent Overstreet 			     PTR_BUCKET_POS(c, &ptr),
7611809b8cbSKent Overstreet 			     BTREE_ITER_CACHED);
7621809b8cbSKent Overstreet 
7631809b8cbSKent Overstreet 	prt_printf(&buf, "Attempting to read from stale dirty pointer:");
7641809b8cbSKent Overstreet 	printbuf_indent_add(&buf, 2);
7651809b8cbSKent Overstreet 	prt_newline(&buf);
7661809b8cbSKent Overstreet 
7671809b8cbSKent Overstreet 	bch2_bkey_val_to_text(&buf, c, k);
7681809b8cbSKent Overstreet 	prt_newline(&buf);
7691809b8cbSKent Overstreet 
7701809b8cbSKent Overstreet 	prt_printf(&buf, "memory gen: %u", *bucket_gen(ca, iter.pos.offset));
7711809b8cbSKent Overstreet 
7721809b8cbSKent Overstreet 	ret = lockrestart_do(trans, bkey_err(k = bch2_btree_iter_peek_slot(&iter)));
7731809b8cbSKent Overstreet 	if (!ret) {
7741809b8cbSKent Overstreet 		prt_newline(&buf);
7751809b8cbSKent Overstreet 		bch2_bkey_val_to_text(&buf, c, k);
7761809b8cbSKent Overstreet 	}
7771809b8cbSKent Overstreet 
7781809b8cbSKent Overstreet 	bch2_fs_inconsistent(c, "%s", buf.buf);
7791809b8cbSKent Overstreet 
7801809b8cbSKent Overstreet 	bch2_trans_iter_exit(trans, &iter);
7811809b8cbSKent Overstreet 	printbuf_exit(&buf);
7821809b8cbSKent Overstreet }
7831809b8cbSKent Overstreet 
7841809b8cbSKent Overstreet int __bch2_read_extent(struct btree_trans *trans, struct bch_read_bio *orig,
7851809b8cbSKent Overstreet 		       struct bvec_iter iter, struct bpos read_pos,
7861809b8cbSKent Overstreet 		       enum btree_id data_btree, struct bkey_s_c k,
7871809b8cbSKent Overstreet 		       unsigned offset_into_extent,
7881809b8cbSKent Overstreet 		       struct bch_io_failures *failed, unsigned flags)
7891809b8cbSKent Overstreet {
7901809b8cbSKent Overstreet 	struct bch_fs *c = trans->c;
7911809b8cbSKent Overstreet 	struct extent_ptr_decoded pick;
7921809b8cbSKent Overstreet 	struct bch_read_bio *rbio = NULL;
7931809b8cbSKent Overstreet 	struct bch_dev *ca = NULL;
7941809b8cbSKent Overstreet 	struct promote_op *promote = NULL;
7951809b8cbSKent Overstreet 	bool bounce = false, read_full = false, narrow_crcs = false;
7961809b8cbSKent Overstreet 	struct bpos data_pos = bkey_start_pos(k.k);
7971809b8cbSKent Overstreet 	int pick_ret;
7981809b8cbSKent Overstreet 
7991809b8cbSKent Overstreet 	if (bkey_extent_is_inline_data(k.k)) {
8001809b8cbSKent Overstreet 		unsigned bytes = min_t(unsigned, iter.bi_size,
8011809b8cbSKent Overstreet 				       bkey_inline_data_bytes(k.k));
8021809b8cbSKent Overstreet 
8031809b8cbSKent Overstreet 		swap(iter.bi_size, bytes);
8041809b8cbSKent Overstreet 		memcpy_to_bio(&orig->bio, iter, bkey_inline_data_p(k));
8051809b8cbSKent Overstreet 		swap(iter.bi_size, bytes);
8061809b8cbSKent Overstreet 		bio_advance_iter(&orig->bio, &iter, bytes);
8071809b8cbSKent Overstreet 		zero_fill_bio_iter(&orig->bio, iter);
8081809b8cbSKent Overstreet 		goto out_read_done;
8091809b8cbSKent Overstreet 	}
8101809b8cbSKent Overstreet retry_pick:
8111809b8cbSKent Overstreet 	pick_ret = bch2_bkey_pick_read_device(c, k, failed, &pick);
8121809b8cbSKent Overstreet 
8131809b8cbSKent Overstreet 	/* hole or reservation - just zero fill: */
8141809b8cbSKent Overstreet 	if (!pick_ret)
8151809b8cbSKent Overstreet 		goto hole;
8161809b8cbSKent Overstreet 
8171809b8cbSKent Overstreet 	if (pick_ret < 0) {
8181809b8cbSKent Overstreet 		bch_err_inum_offset_ratelimited(c,
8191809b8cbSKent Overstreet 				read_pos.inode, read_pos.offset << 9,
8201809b8cbSKent Overstreet 				"no device to read from");
8211809b8cbSKent Overstreet 		goto err;
8221809b8cbSKent Overstreet 	}
8231809b8cbSKent Overstreet 
8241809b8cbSKent Overstreet 	ca = bch_dev_bkey_exists(c, pick.ptr.dev);
8251809b8cbSKent Overstreet 
8261809b8cbSKent Overstreet 	/*
8271809b8cbSKent Overstreet 	 * Stale dirty pointers are treated as IO errors, but @failed isn't
8281809b8cbSKent Overstreet 	 * allocated unless we're in the retry path - so if we're not in the
8291809b8cbSKent Overstreet 	 * retry path, don't check here, it'll be caught in bch2_read_endio()
8301809b8cbSKent Overstreet 	 * and we'll end up in the retry path:
8311809b8cbSKent Overstreet 	 */
8321809b8cbSKent Overstreet 	if ((flags & BCH_READ_IN_RETRY) &&
8331809b8cbSKent Overstreet 	    !pick.ptr.cached &&
8341809b8cbSKent Overstreet 	    unlikely(ptr_stale(ca, &pick.ptr))) {
8351809b8cbSKent Overstreet 		read_from_stale_dirty_pointer(trans, k, pick.ptr);
8361809b8cbSKent Overstreet 		bch2_mark_io_failure(failed, &pick);
8371809b8cbSKent Overstreet 		goto retry_pick;
8381809b8cbSKent Overstreet 	}
8391809b8cbSKent Overstreet 
8401809b8cbSKent Overstreet 	/*
8411809b8cbSKent Overstreet 	 * Unlock the iterator while the btree node's lock is still in
8421809b8cbSKent Overstreet 	 * cache, before doing the IO:
8431809b8cbSKent Overstreet 	 */
8441809b8cbSKent Overstreet 	bch2_trans_unlock(trans);
8451809b8cbSKent Overstreet 
8461809b8cbSKent Overstreet 	if (flags & BCH_READ_NODECODE) {
8471809b8cbSKent Overstreet 		/*
8481809b8cbSKent Overstreet 		 * can happen if we retry, and the extent we were going to read
8491809b8cbSKent Overstreet 		 * has been merged in the meantime:
8501809b8cbSKent Overstreet 		 */
8511809b8cbSKent Overstreet 		if (pick.crc.compressed_size > orig->bio.bi_vcnt * PAGE_SECTORS)
8521809b8cbSKent Overstreet 			goto hole;
8531809b8cbSKent Overstreet 
8541809b8cbSKent Overstreet 		iter.bi_size	= pick.crc.compressed_size << 9;
8551809b8cbSKent Overstreet 		goto get_bio;
8561809b8cbSKent Overstreet 	}
8571809b8cbSKent Overstreet 
8581809b8cbSKent Overstreet 	if (!(flags & BCH_READ_LAST_FRAGMENT) ||
8591809b8cbSKent Overstreet 	    bio_flagged(&orig->bio, BIO_CHAIN))
8601809b8cbSKent Overstreet 		flags |= BCH_READ_MUST_CLONE;
8611809b8cbSKent Overstreet 
8621809b8cbSKent Overstreet 	narrow_crcs = !(flags & BCH_READ_IN_RETRY) &&
8631809b8cbSKent Overstreet 		bch2_can_narrow_extent_crcs(k, pick.crc);
8641809b8cbSKent Overstreet 
8651809b8cbSKent Overstreet 	if (narrow_crcs && (flags & BCH_READ_USER_MAPPED))
8661809b8cbSKent Overstreet 		flags |= BCH_READ_MUST_BOUNCE;
8671809b8cbSKent Overstreet 
8681809b8cbSKent Overstreet 	EBUG_ON(offset_into_extent + bvec_iter_sectors(iter) > k.k->size);
8691809b8cbSKent Overstreet 
8701809b8cbSKent Overstreet 	if (crc_is_compressed(pick.crc) ||
8711809b8cbSKent Overstreet 	    (pick.crc.csum_type != BCH_CSUM_none &&
8721809b8cbSKent Overstreet 	     (bvec_iter_sectors(iter) != pick.crc.uncompressed_size ||
8731809b8cbSKent Overstreet 	      (bch2_csum_type_is_encryption(pick.crc.csum_type) &&
8741809b8cbSKent Overstreet 	       (flags & BCH_READ_USER_MAPPED)) ||
8751809b8cbSKent Overstreet 	      (flags & BCH_READ_MUST_BOUNCE)))) {
8761809b8cbSKent Overstreet 		read_full = true;
8771809b8cbSKent Overstreet 		bounce = true;
8781809b8cbSKent Overstreet 	}
8791809b8cbSKent Overstreet 
8801809b8cbSKent Overstreet 	if (orig->opts.promote_target)
8811809b8cbSKent Overstreet 		promote = promote_alloc(trans, iter, k, &pick, orig->opts, flags,
8821809b8cbSKent Overstreet 					&rbio, &bounce, &read_full);
8831809b8cbSKent Overstreet 
8841809b8cbSKent Overstreet 	if (!read_full) {
8851809b8cbSKent Overstreet 		EBUG_ON(crc_is_compressed(pick.crc));
8861809b8cbSKent Overstreet 		EBUG_ON(pick.crc.csum_type &&
8871809b8cbSKent Overstreet 			(bvec_iter_sectors(iter) != pick.crc.uncompressed_size ||
8881809b8cbSKent Overstreet 			 bvec_iter_sectors(iter) != pick.crc.live_size ||
8891809b8cbSKent Overstreet 			 pick.crc.offset ||
8901809b8cbSKent Overstreet 			 offset_into_extent));
8911809b8cbSKent Overstreet 
8921809b8cbSKent Overstreet 		data_pos.offset += offset_into_extent;
8931809b8cbSKent Overstreet 		pick.ptr.offset += pick.crc.offset +
8941809b8cbSKent Overstreet 			offset_into_extent;
8951809b8cbSKent Overstreet 		offset_into_extent		= 0;
8961809b8cbSKent Overstreet 		pick.crc.compressed_size	= bvec_iter_sectors(iter);
8971809b8cbSKent Overstreet 		pick.crc.uncompressed_size	= bvec_iter_sectors(iter);
8981809b8cbSKent Overstreet 		pick.crc.offset			= 0;
8991809b8cbSKent Overstreet 		pick.crc.live_size		= bvec_iter_sectors(iter);
9001809b8cbSKent Overstreet 		offset_into_extent		= 0;
9011809b8cbSKent Overstreet 	}
9021809b8cbSKent Overstreet get_bio:
9031809b8cbSKent Overstreet 	if (rbio) {
9041809b8cbSKent Overstreet 		/*
9051809b8cbSKent Overstreet 		 * promote already allocated bounce rbio:
9061809b8cbSKent Overstreet 		 * promote needs to allocate a bio big enough for uncompressing
9071809b8cbSKent Overstreet 		 * data in the write path, but we're not going to use it all
9081809b8cbSKent Overstreet 		 * here:
9091809b8cbSKent Overstreet 		 */
9101809b8cbSKent Overstreet 		EBUG_ON(rbio->bio.bi_iter.bi_size <
9111809b8cbSKent Overstreet 		       pick.crc.compressed_size << 9);
9121809b8cbSKent Overstreet 		rbio->bio.bi_iter.bi_size =
9131809b8cbSKent Overstreet 			pick.crc.compressed_size << 9;
9141809b8cbSKent Overstreet 	} else if (bounce) {
9151809b8cbSKent Overstreet 		unsigned sectors = pick.crc.compressed_size;
9161809b8cbSKent Overstreet 
9171809b8cbSKent Overstreet 		rbio = rbio_init(bio_alloc_bioset(NULL,
9181809b8cbSKent Overstreet 						  DIV_ROUND_UP(sectors, PAGE_SECTORS),
9191809b8cbSKent Overstreet 						  0,
9201809b8cbSKent Overstreet 						  GFP_NOFS,
9211809b8cbSKent Overstreet 						  &c->bio_read_split),
9221809b8cbSKent Overstreet 				 orig->opts);
9231809b8cbSKent Overstreet 
9241809b8cbSKent Overstreet 		bch2_bio_alloc_pages_pool(c, &rbio->bio, sectors << 9);
9251809b8cbSKent Overstreet 		rbio->bounce	= true;
9261809b8cbSKent Overstreet 		rbio->split	= true;
9271809b8cbSKent Overstreet 	} else if (flags & BCH_READ_MUST_CLONE) {
9281809b8cbSKent Overstreet 		/*
9291809b8cbSKent Overstreet 		 * Have to clone if there were any splits, due to error
9301809b8cbSKent Overstreet 		 * reporting issues (if a split errored, and retrying didn't
9311809b8cbSKent Overstreet 		 * work, when it reports the error to its parent (us) we don't
9321809b8cbSKent Overstreet 		 * know if the error was from our bio, and we should retry, or
9331809b8cbSKent Overstreet 		 * from the whole bio, in which case we don't want to retry and
9341809b8cbSKent Overstreet 		 * lose the error)
9351809b8cbSKent Overstreet 		 */
9361809b8cbSKent Overstreet 		rbio = rbio_init(bio_alloc_clone(NULL, &orig->bio, GFP_NOFS,
9371809b8cbSKent Overstreet 						 &c->bio_read_split),
9381809b8cbSKent Overstreet 				 orig->opts);
9391809b8cbSKent Overstreet 		rbio->bio.bi_iter = iter;
9401809b8cbSKent Overstreet 		rbio->split	= true;
9411809b8cbSKent Overstreet 	} else {
9421809b8cbSKent Overstreet 		rbio = orig;
9431809b8cbSKent Overstreet 		rbio->bio.bi_iter = iter;
9441809b8cbSKent Overstreet 		EBUG_ON(bio_flagged(&rbio->bio, BIO_CHAIN));
9451809b8cbSKent Overstreet 	}
9461809b8cbSKent Overstreet 
9471809b8cbSKent Overstreet 	EBUG_ON(bio_sectors(&rbio->bio) != pick.crc.compressed_size);
9481809b8cbSKent Overstreet 
9491809b8cbSKent Overstreet 	rbio->c			= c;
9501809b8cbSKent Overstreet 	rbio->submit_time	= local_clock();
9511809b8cbSKent Overstreet 	if (rbio->split)
9521809b8cbSKent Overstreet 		rbio->parent	= orig;
9531809b8cbSKent Overstreet 	else
9541809b8cbSKent Overstreet 		rbio->end_io	= orig->bio.bi_end_io;
9551809b8cbSKent Overstreet 	rbio->bvec_iter		= iter;
9561809b8cbSKent Overstreet 	rbio->offset_into_extent= offset_into_extent;
9571809b8cbSKent Overstreet 	rbio->flags		= flags;
9581809b8cbSKent Overstreet 	rbio->have_ioref	= pick_ret > 0 && bch2_dev_get_ioref(ca, READ);
9591809b8cbSKent Overstreet 	rbio->narrow_crcs	= narrow_crcs;
9601809b8cbSKent Overstreet 	rbio->hole		= 0;
9611809b8cbSKent Overstreet 	rbio->retry		= 0;
9621809b8cbSKent Overstreet 	rbio->context		= 0;
9631809b8cbSKent Overstreet 	/* XXX: only initialize this if needed */
9641809b8cbSKent Overstreet 	rbio->devs_have		= bch2_bkey_devs(k);
9651809b8cbSKent Overstreet 	rbio->pick		= pick;
9661809b8cbSKent Overstreet 	rbio->subvol		= orig->subvol;
9671809b8cbSKent Overstreet 	rbio->read_pos		= read_pos;
9681809b8cbSKent Overstreet 	rbio->data_btree	= data_btree;
9691809b8cbSKent Overstreet 	rbio->data_pos		= data_pos;
9701809b8cbSKent Overstreet 	rbio->version		= k.k->version;
9711809b8cbSKent Overstreet 	rbio->promote		= promote;
9721809b8cbSKent Overstreet 	INIT_WORK(&rbio->work, NULL);
9731809b8cbSKent Overstreet 
9741809b8cbSKent Overstreet 	rbio->bio.bi_opf	= orig->bio.bi_opf;
9751809b8cbSKent Overstreet 	rbio->bio.bi_iter.bi_sector = pick.ptr.offset;
9761809b8cbSKent Overstreet 	rbio->bio.bi_end_io	= bch2_read_endio;
9771809b8cbSKent Overstreet 
9781809b8cbSKent Overstreet 	if (rbio->bounce)
9791809b8cbSKent Overstreet 		trace_and_count(c, read_bounce, &rbio->bio);
9801809b8cbSKent Overstreet 
9811809b8cbSKent Overstreet 	this_cpu_add(c->counters[BCH_COUNTER_io_read], bio_sectors(&rbio->bio));
9821809b8cbSKent Overstreet 	bch2_increment_clock(c, bio_sectors(&rbio->bio), READ);
9831809b8cbSKent Overstreet 
9841809b8cbSKent Overstreet 	/*
9851809b8cbSKent Overstreet 	 * If it's being moved internally, we don't want to flag it as a cache
9861809b8cbSKent Overstreet 	 * hit:
9871809b8cbSKent Overstreet 	 */
9881809b8cbSKent Overstreet 	if (pick.ptr.cached && !(flags & BCH_READ_NODECODE))
9891809b8cbSKent Overstreet 		bch2_bucket_io_time_reset(trans, pick.ptr.dev,
9901809b8cbSKent Overstreet 			PTR_BUCKET_NR(ca, &pick.ptr), READ);
9911809b8cbSKent Overstreet 
9921809b8cbSKent Overstreet 	if (!(flags & (BCH_READ_IN_RETRY|BCH_READ_LAST_FRAGMENT))) {
9931809b8cbSKent Overstreet 		bio_inc_remaining(&orig->bio);
9941809b8cbSKent Overstreet 		trace_and_count(c, read_split, &orig->bio);
9951809b8cbSKent Overstreet 	}
9961809b8cbSKent Overstreet 
9971809b8cbSKent Overstreet 	if (!rbio->pick.idx) {
9981809b8cbSKent Overstreet 		if (!rbio->have_ioref) {
9991809b8cbSKent Overstreet 			bch_err_inum_offset_ratelimited(c,
10001809b8cbSKent Overstreet 					read_pos.inode,
10011809b8cbSKent Overstreet 					read_pos.offset << 9,
10021809b8cbSKent Overstreet 					"no device to read from");
10031809b8cbSKent Overstreet 			bch2_rbio_error(rbio, READ_RETRY_AVOID, BLK_STS_IOERR);
10041809b8cbSKent Overstreet 			goto out;
10051809b8cbSKent Overstreet 		}
10061809b8cbSKent Overstreet 
10071809b8cbSKent Overstreet 		this_cpu_add(ca->io_done->sectors[READ][BCH_DATA_user],
10081809b8cbSKent Overstreet 			     bio_sectors(&rbio->bio));
10091809b8cbSKent Overstreet 		bio_set_dev(&rbio->bio, ca->disk_sb.bdev);
10101809b8cbSKent Overstreet 
10111809b8cbSKent Overstreet 		if (unlikely(c->opts.no_data_io)) {
10121809b8cbSKent Overstreet 			if (likely(!(flags & BCH_READ_IN_RETRY)))
10131809b8cbSKent Overstreet 				bio_endio(&rbio->bio);
10141809b8cbSKent Overstreet 		} else {
10151809b8cbSKent Overstreet 			if (likely(!(flags & BCH_READ_IN_RETRY)))
10161809b8cbSKent Overstreet 				submit_bio(&rbio->bio);
10171809b8cbSKent Overstreet 			else
10181809b8cbSKent Overstreet 				submit_bio_wait(&rbio->bio);
10191809b8cbSKent Overstreet 		}
10201809b8cbSKent Overstreet 
10211809b8cbSKent Overstreet 		/*
10221809b8cbSKent Overstreet 		 * We just submitted IO which may block, we expect relock fail
10231809b8cbSKent Overstreet 		 * events and shouldn't count them:
10241809b8cbSKent Overstreet 		 */
10251809b8cbSKent Overstreet 		trans->notrace_relock_fail = true;
10261809b8cbSKent Overstreet 	} else {
10271809b8cbSKent Overstreet 		/* Attempting reconstruct read: */
10281809b8cbSKent Overstreet 		if (bch2_ec_read_extent(c, rbio)) {
10291809b8cbSKent Overstreet 			bch2_rbio_error(rbio, READ_RETRY_AVOID, BLK_STS_IOERR);
10301809b8cbSKent Overstreet 			goto out;
10311809b8cbSKent Overstreet 		}
10321809b8cbSKent Overstreet 
10331809b8cbSKent Overstreet 		if (likely(!(flags & BCH_READ_IN_RETRY)))
10341809b8cbSKent Overstreet 			bio_endio(&rbio->bio);
10351809b8cbSKent Overstreet 	}
10361809b8cbSKent Overstreet out:
10371809b8cbSKent Overstreet 	if (likely(!(flags & BCH_READ_IN_RETRY))) {
10381809b8cbSKent Overstreet 		return 0;
10391809b8cbSKent Overstreet 	} else {
10401809b8cbSKent Overstreet 		int ret;
10411809b8cbSKent Overstreet 
10421809b8cbSKent Overstreet 		rbio->context = RBIO_CONTEXT_UNBOUND;
10431809b8cbSKent Overstreet 		bch2_read_endio(&rbio->bio);
10441809b8cbSKent Overstreet 
10451809b8cbSKent Overstreet 		ret = rbio->retry;
10461809b8cbSKent Overstreet 		rbio = bch2_rbio_free(rbio);
10471809b8cbSKent Overstreet 
10481809b8cbSKent Overstreet 		if (ret == READ_RETRY_AVOID) {
10491809b8cbSKent Overstreet 			bch2_mark_io_failure(failed, &pick);
10501809b8cbSKent Overstreet 			ret = READ_RETRY;
10511809b8cbSKent Overstreet 		}
10521809b8cbSKent Overstreet 
10531809b8cbSKent Overstreet 		if (!ret)
10541809b8cbSKent Overstreet 			goto out_read_done;
10551809b8cbSKent Overstreet 
10561809b8cbSKent Overstreet 		return ret;
10571809b8cbSKent Overstreet 	}
10581809b8cbSKent Overstreet 
10591809b8cbSKent Overstreet err:
10601809b8cbSKent Overstreet 	if (flags & BCH_READ_IN_RETRY)
10611809b8cbSKent Overstreet 		return READ_ERR;
10621809b8cbSKent Overstreet 
10631809b8cbSKent Overstreet 	orig->bio.bi_status = BLK_STS_IOERR;
10641809b8cbSKent Overstreet 	goto out_read_done;
10651809b8cbSKent Overstreet 
10661809b8cbSKent Overstreet hole:
10671809b8cbSKent Overstreet 	/*
10681809b8cbSKent Overstreet 	 * won't normally happen in the BCH_READ_NODECODE
10691809b8cbSKent Overstreet 	 * (bch2_move_extent()) path, but if we retry and the extent we wanted
10701809b8cbSKent Overstreet 	 * to read no longer exists we have to signal that:
10711809b8cbSKent Overstreet 	 */
10721809b8cbSKent Overstreet 	if (flags & BCH_READ_NODECODE)
10731809b8cbSKent Overstreet 		orig->hole = true;
10741809b8cbSKent Overstreet 
10751809b8cbSKent Overstreet 	zero_fill_bio_iter(&orig->bio, iter);
10761809b8cbSKent Overstreet out_read_done:
10771809b8cbSKent Overstreet 	if (flags & BCH_READ_LAST_FRAGMENT)
10781809b8cbSKent Overstreet 		bch2_rbio_done(orig);
10791809b8cbSKent Overstreet 	return 0;
10801809b8cbSKent Overstreet }
10811809b8cbSKent Overstreet 
10821809b8cbSKent Overstreet void __bch2_read(struct bch_fs *c, struct bch_read_bio *rbio,
10831809b8cbSKent Overstreet 		 struct bvec_iter bvec_iter, subvol_inum inum,
10841809b8cbSKent Overstreet 		 struct bch_io_failures *failed, unsigned flags)
10851809b8cbSKent Overstreet {
10861809b8cbSKent Overstreet 	struct btree_trans trans;
10871809b8cbSKent Overstreet 	struct btree_iter iter;
10881809b8cbSKent Overstreet 	struct bkey_buf sk;
10891809b8cbSKent Overstreet 	struct bkey_s_c k;
10901809b8cbSKent Overstreet 	u32 snapshot;
10911809b8cbSKent Overstreet 	int ret;
10921809b8cbSKent Overstreet 
10931809b8cbSKent Overstreet 	BUG_ON(flags & BCH_READ_NODECODE);
10941809b8cbSKent Overstreet 
10951809b8cbSKent Overstreet 	bch2_bkey_buf_init(&sk);
10961809b8cbSKent Overstreet 	bch2_trans_init(&trans, c, 0, 0);
10971809b8cbSKent Overstreet retry:
10981809b8cbSKent Overstreet 	bch2_trans_begin(&trans);
10991809b8cbSKent Overstreet 	iter = (struct btree_iter) { NULL };
11001809b8cbSKent Overstreet 
11011809b8cbSKent Overstreet 	ret = bch2_subvolume_get_snapshot(&trans, inum.subvol, &snapshot);
11021809b8cbSKent Overstreet 	if (ret)
11031809b8cbSKent Overstreet 		goto err;
11041809b8cbSKent Overstreet 
11051809b8cbSKent Overstreet 	bch2_trans_iter_init(&trans, &iter, BTREE_ID_extents,
11061809b8cbSKent Overstreet 			     SPOS(inum.inum, bvec_iter.bi_sector, snapshot),
11071809b8cbSKent Overstreet 			     BTREE_ITER_SLOTS);
11081809b8cbSKent Overstreet 	while (1) {
11091809b8cbSKent Overstreet 		unsigned bytes, sectors, offset_into_extent;
11101809b8cbSKent Overstreet 		enum btree_id data_btree = BTREE_ID_extents;
11111809b8cbSKent Overstreet 
11121809b8cbSKent Overstreet 		/*
11131809b8cbSKent Overstreet 		 * read_extent -> io_time_reset may cause a transaction restart
11141809b8cbSKent Overstreet 		 * without returning an error, we need to check for that here:
11151809b8cbSKent Overstreet 		 */
11161809b8cbSKent Overstreet 		ret = bch2_trans_relock(&trans);
11171809b8cbSKent Overstreet 		if (ret)
11181809b8cbSKent Overstreet 			break;
11191809b8cbSKent Overstreet 
11201809b8cbSKent Overstreet 		bch2_btree_iter_set_pos(&iter,
11211809b8cbSKent Overstreet 				POS(inum.inum, bvec_iter.bi_sector));
11221809b8cbSKent Overstreet 
11231809b8cbSKent Overstreet 		k = bch2_btree_iter_peek_slot(&iter);
11241809b8cbSKent Overstreet 		ret = bkey_err(k);
11251809b8cbSKent Overstreet 		if (ret)
11261809b8cbSKent Overstreet 			break;
11271809b8cbSKent Overstreet 
11281809b8cbSKent Overstreet 		offset_into_extent = iter.pos.offset -
11291809b8cbSKent Overstreet 			bkey_start_offset(k.k);
11301809b8cbSKent Overstreet 		sectors = k.k->size - offset_into_extent;
11311809b8cbSKent Overstreet 
11321809b8cbSKent Overstreet 		bch2_bkey_buf_reassemble(&sk, c, k);
11331809b8cbSKent Overstreet 
11341809b8cbSKent Overstreet 		ret = bch2_read_indirect_extent(&trans, &data_btree,
11351809b8cbSKent Overstreet 					&offset_into_extent, &sk);
11361809b8cbSKent Overstreet 		if (ret)
11371809b8cbSKent Overstreet 			break;
11381809b8cbSKent Overstreet 
11391809b8cbSKent Overstreet 		k = bkey_i_to_s_c(sk.k);
11401809b8cbSKent Overstreet 
11411809b8cbSKent Overstreet 		/*
11421809b8cbSKent Overstreet 		 * With indirect extents, the amount of data to read is the min
11431809b8cbSKent Overstreet 		 * of the original extent and the indirect extent:
11441809b8cbSKent Overstreet 		 */
11451809b8cbSKent Overstreet 		sectors = min(sectors, k.k->size - offset_into_extent);
11461809b8cbSKent Overstreet 
11471809b8cbSKent Overstreet 		bytes = min(sectors, bvec_iter_sectors(bvec_iter)) << 9;
11481809b8cbSKent Overstreet 		swap(bvec_iter.bi_size, bytes);
11491809b8cbSKent Overstreet 
11501809b8cbSKent Overstreet 		if (bvec_iter.bi_size == bytes)
11511809b8cbSKent Overstreet 			flags |= BCH_READ_LAST_FRAGMENT;
11521809b8cbSKent Overstreet 
11531809b8cbSKent Overstreet 		ret = __bch2_read_extent(&trans, rbio, bvec_iter, iter.pos,
11541809b8cbSKent Overstreet 					 data_btree, k,
11551809b8cbSKent Overstreet 					 offset_into_extent, failed, flags);
11561809b8cbSKent Overstreet 		if (ret)
11571809b8cbSKent Overstreet 			break;
11581809b8cbSKent Overstreet 
11591809b8cbSKent Overstreet 		if (flags & BCH_READ_LAST_FRAGMENT)
11601809b8cbSKent Overstreet 			break;
11611809b8cbSKent Overstreet 
11621809b8cbSKent Overstreet 		swap(bvec_iter.bi_size, bytes);
11631809b8cbSKent Overstreet 		bio_advance_iter(&rbio->bio, &bvec_iter, bytes);
11641809b8cbSKent Overstreet 
11651809b8cbSKent Overstreet 		ret = btree_trans_too_many_iters(&trans);
11661809b8cbSKent Overstreet 		if (ret)
11671809b8cbSKent Overstreet 			break;
11681809b8cbSKent Overstreet 	}
11691809b8cbSKent Overstreet err:
11701809b8cbSKent Overstreet 	bch2_trans_iter_exit(&trans, &iter);
11711809b8cbSKent Overstreet 
11721809b8cbSKent Overstreet 	if (bch2_err_matches(ret, BCH_ERR_transaction_restart) ||
11731809b8cbSKent Overstreet 	    ret == READ_RETRY ||
11741809b8cbSKent Overstreet 	    ret == READ_RETRY_AVOID)
11751809b8cbSKent Overstreet 		goto retry;
11761809b8cbSKent Overstreet 
11771809b8cbSKent Overstreet 	bch2_trans_exit(&trans);
11781809b8cbSKent Overstreet 	bch2_bkey_buf_exit(&sk, c);
11791809b8cbSKent Overstreet 
11801809b8cbSKent Overstreet 	if (ret) {
11811809b8cbSKent Overstreet 		bch_err_inum_offset_ratelimited(c, inum.inum,
11821809b8cbSKent Overstreet 						bvec_iter.bi_sector << 9,
11831809b8cbSKent Overstreet 						"read error %i from btree lookup", ret);
11841809b8cbSKent Overstreet 		rbio->bio.bi_status = BLK_STS_IOERR;
11851809b8cbSKent Overstreet 		bch2_rbio_done(rbio);
11861809b8cbSKent Overstreet 	}
11871809b8cbSKent Overstreet }
11881809b8cbSKent Overstreet 
11891809b8cbSKent Overstreet void bch2_fs_io_read_exit(struct bch_fs *c)
11901809b8cbSKent Overstreet {
11911809b8cbSKent Overstreet 	if (c->promote_table.tbl)
11921809b8cbSKent Overstreet 		rhashtable_destroy(&c->promote_table);
11931809b8cbSKent Overstreet 	bioset_exit(&c->bio_read_split);
11941809b8cbSKent Overstreet 	bioset_exit(&c->bio_read);
11951809b8cbSKent Overstreet }
11961809b8cbSKent Overstreet 
11971809b8cbSKent Overstreet int bch2_fs_io_read_init(struct bch_fs *c)
11981809b8cbSKent Overstreet {
11991809b8cbSKent Overstreet 	if (bioset_init(&c->bio_read, 1, offsetof(struct bch_read_bio, bio),
12001809b8cbSKent Overstreet 			BIOSET_NEED_BVECS))
12011809b8cbSKent Overstreet 		return -BCH_ERR_ENOMEM_bio_read_init;
12021809b8cbSKent Overstreet 
12031809b8cbSKent Overstreet 	if (bioset_init(&c->bio_read_split, 1, offsetof(struct bch_read_bio, bio),
12041809b8cbSKent Overstreet 			BIOSET_NEED_BVECS))
12051809b8cbSKent Overstreet 		return -BCH_ERR_ENOMEM_bio_read_split_init;
12061809b8cbSKent Overstreet 
12071809b8cbSKent Overstreet 	if (rhashtable_init(&c->promote_table, &bch_promote_params))
12081809b8cbSKent Overstreet 		return -BCH_ERR_ENOMEM_promote_table_init;
12091809b8cbSKent Overstreet 
12101809b8cbSKent Overstreet 	return 0;
12111809b8cbSKent Overstreet }
1212