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