|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | +/* |
| 3 | + * Copyright (C) 2024, Alibaba Cloud |
| 4 | + */ |
| 5 | +#include "internal.h" |
| 6 | +#include <trace/events/erofs.h> |
| 7 | + |
| 8 | +struct erofs_fileio_rq { |
| 9 | + struct bio_vec bvecs[BIO_MAX_VECS]; |
| 10 | + struct bio bio; |
| 11 | + struct kiocb iocb; |
| 12 | +}; |
| 13 | + |
| 14 | +struct erofs_fileio { |
| 15 | + struct erofs_map_blocks map; |
| 16 | + struct erofs_map_dev dev; |
| 17 | + struct erofs_fileio_rq *rq; |
| 18 | +}; |
| 19 | + |
| 20 | +static void erofs_fileio_ki_complete(struct kiocb *iocb, long ret) |
| 21 | +{ |
| 22 | + struct erofs_fileio_rq *rq = |
| 23 | + container_of(iocb, struct erofs_fileio_rq, iocb); |
| 24 | + struct folio_iter fi; |
| 25 | + |
| 26 | + DBG_BUGON(rq->bio.bi_end_io); |
| 27 | + if (ret > 0) { |
| 28 | + if (ret != rq->bio.bi_iter.bi_size) { |
| 29 | + bio_advance(&rq->bio, ret); |
| 30 | + zero_fill_bio(&rq->bio); |
| 31 | + } |
| 32 | + ret = 0; |
| 33 | + } |
| 34 | + bio_for_each_folio_all(fi, &rq->bio) { |
| 35 | + DBG_BUGON(folio_test_uptodate(fi.folio)); |
| 36 | + erofs_onlinefolio_end(fi.folio, ret); |
| 37 | + } |
| 38 | + bio_uninit(&rq->bio); |
| 39 | + kfree(rq); |
| 40 | +} |
| 41 | + |
| 42 | +static void erofs_fileio_rq_submit(struct erofs_fileio_rq *rq) |
| 43 | +{ |
| 44 | + struct iov_iter iter; |
| 45 | + int ret; |
| 46 | + |
| 47 | + if (!rq) |
| 48 | + return; |
| 49 | + rq->iocb.ki_pos = rq->bio.bi_iter.bi_sector << SECTOR_SHIFT; |
| 50 | + rq->iocb.ki_ioprio = get_current_ioprio(); |
| 51 | + rq->iocb.ki_complete = erofs_fileio_ki_complete; |
| 52 | + rq->iocb.ki_flags = (rq->iocb.ki_filp->f_mode & FMODE_CAN_ODIRECT) ? |
| 53 | + IOCB_DIRECT : 0; |
| 54 | + iov_iter_bvec(&iter, ITER_DEST, rq->bvecs, rq->bio.bi_vcnt, |
| 55 | + rq->bio.bi_iter.bi_size); |
| 56 | + ret = vfs_iocb_iter_read(rq->iocb.ki_filp, &rq->iocb, &iter); |
| 57 | + if (ret != -EIOCBQUEUED) |
| 58 | + erofs_fileio_ki_complete(&rq->iocb, ret); |
| 59 | +} |
| 60 | + |
| 61 | +static struct erofs_fileio_rq *erofs_fileio_rq_alloc(struct erofs_map_dev *mdev) |
| 62 | +{ |
| 63 | + struct erofs_fileio_rq *rq = kzalloc(sizeof(*rq), |
| 64 | + GFP_KERNEL | __GFP_NOFAIL); |
| 65 | + |
| 66 | + bio_init(&rq->bio, NULL, rq->bvecs, BIO_MAX_VECS, REQ_OP_READ); |
| 67 | + rq->iocb.ki_filp = mdev->m_fp; |
| 68 | + return rq; |
| 69 | +} |
| 70 | + |
| 71 | +static int erofs_fileio_scan_folio(struct erofs_fileio *io, struct folio *folio) |
| 72 | +{ |
| 73 | + struct inode *inode = folio_inode(folio); |
| 74 | + struct erofs_map_blocks *map = &io->map; |
| 75 | + unsigned int cur = 0, end = folio_size(folio), len, attached = 0; |
| 76 | + loff_t pos = folio_pos(folio), ofs; |
| 77 | + struct iov_iter iter; |
| 78 | + struct bio_vec bv; |
| 79 | + int err = 0; |
| 80 | + |
| 81 | + erofs_onlinefolio_init(folio); |
| 82 | + while (cur < end) { |
| 83 | + if (!in_range(pos + cur, map->m_la, map->m_llen)) { |
| 84 | + map->m_la = pos + cur; |
| 85 | + map->m_llen = end - cur; |
| 86 | + err = erofs_map_blocks(inode, map); |
| 87 | + if (err) |
| 88 | + break; |
| 89 | + } |
| 90 | + |
| 91 | + ofs = folio_pos(folio) + cur - map->m_la; |
| 92 | + len = min_t(loff_t, map->m_llen - ofs, end - cur); |
| 93 | + if (map->m_flags & EROFS_MAP_META) { |
| 94 | + struct erofs_buf buf = __EROFS_BUF_INITIALIZER; |
| 95 | + void *src; |
| 96 | + |
| 97 | + src = erofs_read_metabuf(&buf, inode->i_sb, |
| 98 | + map->m_pa + ofs, EROFS_KMAP); |
| 99 | + if (IS_ERR(src)) { |
| 100 | + err = PTR_ERR(src); |
| 101 | + break; |
| 102 | + } |
| 103 | + bvec_set_folio(&bv, folio, len, cur); |
| 104 | + iov_iter_bvec(&iter, ITER_DEST, &bv, 1, len); |
| 105 | + if (copy_to_iter(src, len, &iter) != len) { |
| 106 | + erofs_put_metabuf(&buf); |
| 107 | + err = -EIO; |
| 108 | + break; |
| 109 | + } |
| 110 | + erofs_put_metabuf(&buf); |
| 111 | + } else if (!(map->m_flags & EROFS_MAP_MAPPED)) { |
| 112 | + folio_zero_segment(folio, cur, cur + len); |
| 113 | + attached = 0; |
| 114 | + } else { |
| 115 | + if (io->rq && (map->m_pa + ofs != io->dev.m_pa || |
| 116 | + map->m_deviceid != io->dev.m_deviceid)) { |
| 117 | +io_retry: |
| 118 | + erofs_fileio_rq_submit(io->rq); |
| 119 | + io->rq = NULL; |
| 120 | + } |
| 121 | + |
| 122 | + if (!io->rq) { |
| 123 | + io->dev = (struct erofs_map_dev) { |
| 124 | + .m_pa = io->map.m_pa + ofs, |
| 125 | + .m_deviceid = io->map.m_deviceid, |
| 126 | + }; |
| 127 | + err = erofs_map_dev(inode->i_sb, &io->dev); |
| 128 | + if (err) |
| 129 | + break; |
| 130 | + io->rq = erofs_fileio_rq_alloc(&io->dev); |
| 131 | + io->rq->bio.bi_iter.bi_sector = io->dev.m_pa >> 9; |
| 132 | + attached = 0; |
| 133 | + } |
| 134 | + if (!attached++) |
| 135 | + erofs_onlinefolio_split(folio); |
| 136 | + if (!bio_add_folio(&io->rq->bio, folio, len, cur)) |
| 137 | + goto io_retry; |
| 138 | + io->dev.m_pa += len; |
| 139 | + } |
| 140 | + cur += len; |
| 141 | + } |
| 142 | + erofs_onlinefolio_end(folio, err); |
| 143 | + return err; |
| 144 | +} |
| 145 | + |
| 146 | +static int erofs_fileio_read_folio(struct file *file, struct folio *folio) |
| 147 | +{ |
| 148 | + struct erofs_fileio io = {}; |
| 149 | + int err; |
| 150 | + |
| 151 | + trace_erofs_read_folio(folio, true); |
| 152 | + err = erofs_fileio_scan_folio(&io, folio); |
| 153 | + erofs_fileio_rq_submit(io.rq); |
| 154 | + return err; |
| 155 | +} |
| 156 | + |
| 157 | +static void erofs_fileio_readahead(struct readahead_control *rac) |
| 158 | +{ |
| 159 | + struct inode *inode = rac->mapping->host; |
| 160 | + struct erofs_fileio io = {}; |
| 161 | + struct folio *folio; |
| 162 | + int err; |
| 163 | + |
| 164 | + trace_erofs_readpages(inode, readahead_index(rac), |
| 165 | + readahead_count(rac), true); |
| 166 | + while ((folio = readahead_folio(rac))) { |
| 167 | + err = erofs_fileio_scan_folio(&io, folio); |
| 168 | + if (err && err != -EINTR) |
| 169 | + erofs_err(inode->i_sb, "readahead error at folio %lu @ nid %llu", |
| 170 | + folio->index, EROFS_I(inode)->nid); |
| 171 | + } |
| 172 | + erofs_fileio_rq_submit(io.rq); |
| 173 | +} |
| 174 | + |
| 175 | +const struct address_space_operations erofs_fileio_aops = { |
| 176 | + .read_folio = erofs_fileio_read_folio, |
| 177 | + .readahead = erofs_fileio_readahead, |
| 178 | +}; |
0 commit comments