Gao Xiang | 29b24f6 | 2019-07-31 23:57:31 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 2 | /* |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 3 | * Copyright (C) 2017-2018 HUAWEI, Inc. |
| 4 | * http://www.huawei.com/ |
| 5 | * Created by Gao Xiang <gaoxiang25@huawei.com> |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 6 | */ |
| 7 | #include "internal.h" |
| 8 | #include <linux/prefetch.h> |
| 9 | |
Chao Yu | 13f06f4 | 2018-07-26 20:21:55 +0800 | [diff] [blame] | 10 | #include <trace/events/erofs.h> |
| 11 | |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 12 | static inline void read_endio(struct bio *bio) |
| 13 | { |
Gao Xiang | 14a56ec | 2019-03-25 11:40:09 +0800 | [diff] [blame] | 14 | struct super_block *const sb = bio->bi_private; |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 15 | struct bio_vec *bvec; |
Gao Xiang | 14a56ec | 2019-03-25 11:40:09 +0800 | [diff] [blame] | 16 | blk_status_t err = bio->bi_status; |
Ming Lei | 6dc4f10 | 2019-02-15 19:13:19 +0800 | [diff] [blame] | 17 | struct bvec_iter_all iter_all; |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 18 | |
Gao Xiang | 14a56ec | 2019-03-25 11:40:09 +0800 | [diff] [blame] | 19 | if (time_to_inject(EROFS_SB(sb), FAULT_READ_IO)) { |
| 20 | erofs_show_injection_info(FAULT_READ_IO); |
| 21 | err = BLK_STS_IOERR; |
| 22 | } |
| 23 | |
Christoph Hellwig | 2b070cf | 2019-04-25 09:03:00 +0200 | [diff] [blame] | 24 | bio_for_each_segment_all(bvec, bio, iter_all) { |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 25 | struct page *page = bvec->bv_page; |
| 26 | |
| 27 | /* page is already locked */ |
Chen Gong | 9141b60 | 2018-09-18 22:27:28 +0800 | [diff] [blame] | 28 | DBG_BUGON(PageUptodate(page)); |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 29 | |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 30 | if (err) |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 31 | SetPageError(page); |
| 32 | else |
| 33 | SetPageUptodate(page); |
| 34 | |
| 35 | unlock_page(page); |
| 36 | /* page could be reclaimed now */ |
| 37 | } |
| 38 | bio_put(bio); |
| 39 | } |
| 40 | |
Gao Xiang | a5c0b78 | 2019-09-04 10:09:02 +0800 | [diff] [blame] | 41 | static struct bio *erofs_grab_raw_bio(struct super_block *sb, |
| 42 | erofs_blk_t blkaddr, |
| 43 | unsigned int nr_pages) |
| 44 | { |
| 45 | struct bio *bio = bio_alloc(GFP_NOIO, nr_pages); |
| 46 | |
| 47 | bio->bi_end_io = read_endio; |
| 48 | bio_set_dev(bio, sb->s_bdev); |
| 49 | bio->bi_iter.bi_sector = (sector_t)blkaddr << LOG_SECTORS_PER_BLOCK; |
| 50 | bio->bi_private = sb; |
| 51 | return bio; |
| 52 | } |
| 53 | |
Gao Xiang | e655b5b | 2019-09-04 10:09:03 +0800 | [diff] [blame^] | 54 | struct page *erofs_get_meta_page(struct super_block *sb, erofs_blk_t blkaddr) |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 55 | { |
Gao Xiang | 6e78901 | 2018-08-21 22:49:30 +0800 | [diff] [blame] | 56 | struct inode *const bd_inode = sb->s_bdev->bd_inode; |
| 57 | struct address_space *const mapping = bd_inode->i_mapping; |
Gao Xiang | e655b5b | 2019-09-04 10:09:03 +0800 | [diff] [blame^] | 58 | const gfp_t gfp = mapping_gfp_constraint(mapping, ~__GFP_FS); |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 59 | struct page *page; |
Gao Xiang | 6e78901 | 2018-08-21 22:49:30 +0800 | [diff] [blame] | 60 | int err; |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 61 | |
| 62 | repeat: |
Gao Xiang | 6e78901 | 2018-08-21 22:49:30 +0800 | [diff] [blame] | 63 | page = find_or_create_page(mapping, blkaddr, gfp); |
Gao Xiang | e655b5b | 2019-09-04 10:09:03 +0800 | [diff] [blame^] | 64 | if (!page) |
Gao Xiang | 6e78901 | 2018-08-21 22:49:30 +0800 | [diff] [blame] | 65 | return ERR_PTR(-ENOMEM); |
Gao Xiang | e655b5b | 2019-09-04 10:09:03 +0800 | [diff] [blame^] | 66 | |
Gao Xiang | 6e78901 | 2018-08-21 22:49:30 +0800 | [diff] [blame] | 67 | DBG_BUGON(!PageLocked(page)); |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 68 | |
| 69 | if (!PageUptodate(page)) { |
| 70 | struct bio *bio; |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 71 | |
Gao Xiang | a5c0b78 | 2019-09-04 10:09:02 +0800 | [diff] [blame] | 72 | bio = erofs_grab_raw_bio(sb, blkaddr, 1); |
Gao Xiang | 8be3127 | 2018-08-21 22:49:29 +0800 | [diff] [blame] | 73 | |
Gao Xiang | 097a802 | 2019-08-30 01:17:41 +0800 | [diff] [blame] | 74 | if (bio_add_page(bio, page, PAGE_SIZE, 0) != PAGE_SIZE) { |
Gao Xiang | 6e78901 | 2018-08-21 22:49:30 +0800 | [diff] [blame] | 75 | err = -EFAULT; |
| 76 | goto err_out; |
| 77 | } |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 78 | |
Gao Xiang | e655b5b | 2019-09-04 10:09:03 +0800 | [diff] [blame^] | 79 | __submit_bio(bio, REQ_OP_READ, REQ_META); |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 80 | lock_page(page); |
| 81 | |
Gao Xiang | 6e78901 | 2018-08-21 22:49:30 +0800 | [diff] [blame] | 82 | /* this page has been truncated by others */ |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 83 | if (page->mapping != mapping) { |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 84 | unlock_page(page); |
| 85 | put_page(page); |
| 86 | goto repeat; |
| 87 | } |
| 88 | |
| 89 | /* more likely a read error */ |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 90 | if (!PageUptodate(page)) { |
Gao Xiang | 6e78901 | 2018-08-21 22:49:30 +0800 | [diff] [blame] | 91 | err = -EIO; |
| 92 | goto err_out; |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | return page; |
Gao Xiang | 6e78901 | 2018-08-21 22:49:30 +0800 | [diff] [blame] | 96 | |
| 97 | err_out: |
| 98 | unlock_page(page); |
| 99 | put_page(page); |
| 100 | return ERR_PTR(err); |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | static int erofs_map_blocks_flatmode(struct inode *inode, |
Bhagyashri P. Dighole | f0950b0 | 2018-11-05 19:48:38 +0000 | [diff] [blame] | 104 | struct erofs_map_blocks *map, |
| 105 | int flags) |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 106 | { |
Chen Gong | 9141b60 | 2018-09-18 22:27:28 +0800 | [diff] [blame] | 107 | int err = 0; |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 108 | erofs_blk_t nblocks, lastblk; |
| 109 | u64 offset = map->m_la; |
Gao Xiang | a5876e2 | 2019-09-04 10:08:56 +0800 | [diff] [blame] | 110 | struct erofs_inode *vi = EROFS_I(inode); |
Gao Xiang | 8a76568 | 2019-09-04 10:08:54 +0800 | [diff] [blame] | 111 | bool tailendpacking = (vi->datalayout == EROFS_INODE_FLAT_INLINE); |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 112 | |
Chao Yu | 13f06f4 | 2018-07-26 20:21:55 +0800 | [diff] [blame] | 113 | trace_erofs_map_blocks_flatmode_enter(inode, map, flags); |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 114 | |
| 115 | nblocks = DIV_ROUND_UP(inode->i_size, PAGE_SIZE); |
Gao Xiang | 8a76568 | 2019-09-04 10:08:54 +0800 | [diff] [blame] | 116 | lastblk = nblocks - tailendpacking; |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 117 | |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 118 | if (offset >= inode->i_size) { |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 119 | /* leave out-of-bound access unmapped */ |
| 120 | map->m_flags = 0; |
| 121 | map->m_plen = 0; |
| 122 | goto out; |
| 123 | } |
| 124 | |
| 125 | /* there is no hole in flatmode */ |
| 126 | map->m_flags = EROFS_MAP_MAPPED; |
| 127 | |
| 128 | if (offset < blknr_to_addr(lastblk)) { |
| 129 | map->m_pa = blknr_to_addr(vi->raw_blkaddr) + map->m_la; |
| 130 | map->m_plen = blknr_to_addr(lastblk) - offset; |
Gao Xiang | 8a76568 | 2019-09-04 10:08:54 +0800 | [diff] [blame] | 131 | } else if (tailendpacking) { |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 132 | /* 2 - inode inline B: inode, [xattrs], inline last blk... */ |
| 133 | struct erofs_sb_info *sbi = EROFS_SB(inode->i_sb); |
| 134 | |
| 135 | map->m_pa = iloc(sbi, vi->nid) + vi->inode_isize + |
| 136 | vi->xattr_isize + erofs_blkoff(map->m_la); |
| 137 | map->m_plen = inode->i_size - offset; |
| 138 | |
Gao Xiang | a6b9b1d | 2019-08-14 18:37:03 +0800 | [diff] [blame] | 139 | /* inline data should be located in one meta block */ |
Chen Gong | 9141b60 | 2018-09-18 22:27:28 +0800 | [diff] [blame] | 140 | if (erofs_blkoff(map->m_pa) + map->m_plen > PAGE_SIZE) { |
Gao Xiang | a6b9b1d | 2019-08-14 18:37:03 +0800 | [diff] [blame] | 141 | errln("inline data cross block boundary @ nid %llu", |
| 142 | vi->nid); |
Chen Gong | 9141b60 | 2018-09-18 22:27:28 +0800 | [diff] [blame] | 143 | DBG_BUGON(1); |
Gao Xiang | a6b9b1d | 2019-08-14 18:37:03 +0800 | [diff] [blame] | 144 | err = -EFSCORRUPTED; |
Chen Gong | 9141b60 | 2018-09-18 22:27:28 +0800 | [diff] [blame] | 145 | goto err_out; |
| 146 | } |
| 147 | |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 148 | map->m_flags |= EROFS_MAP_META; |
| 149 | } else { |
| 150 | errln("internal error @ nid: %llu (size %llu), m_la 0x%llx", |
Bhagyashri P. Dighole | f0950b0 | 2018-11-05 19:48:38 +0000 | [diff] [blame] | 151 | vi->nid, inode->i_size, map->m_la); |
Chen Gong | 9141b60 | 2018-09-18 22:27:28 +0800 | [diff] [blame] | 152 | DBG_BUGON(1); |
| 153 | err = -EIO; |
| 154 | goto err_out; |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | out: |
| 158 | map->m_llen = map->m_plen; |
Chen Gong | 9141b60 | 2018-09-18 22:27:28 +0800 | [diff] [blame] | 159 | |
| 160 | err_out: |
Chao Yu | 13f06f4 | 2018-07-26 20:21:55 +0800 | [diff] [blame] | 161 | trace_erofs_map_blocks_flatmode_exit(inode, map, flags, 0); |
Chen Gong | 9141b60 | 2018-09-18 22:27:28 +0800 | [diff] [blame] | 162 | return err; |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | int erofs_map_blocks(struct inode *inode, |
Bhagyashri P. Dighole | f0950b0 | 2018-11-05 19:48:38 +0000 | [diff] [blame] | 166 | struct erofs_map_blocks *map, int flags) |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 167 | { |
Gao Xiang | a5876e2 | 2019-09-04 10:08:56 +0800 | [diff] [blame] | 168 | if (erofs_inode_is_data_compressed(EROFS_I(inode)->datalayout)) { |
Chao Yu | 3b42341 | 2019-01-15 09:42:21 +0800 | [diff] [blame] | 169 | int err = z_erofs_map_blocks_iter(inode, map, flags); |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 170 | |
Chao Yu | 3b42341 | 2019-01-15 09:42:21 +0800 | [diff] [blame] | 171 | if (map->mpage) { |
| 172 | put_page(map->mpage); |
| 173 | map->mpage = NULL; |
| 174 | } |
Gao Xiang | 02827e1 | 2018-07-26 20:21:58 +0800 | [diff] [blame] | 175 | return err; |
| 176 | } |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 177 | return erofs_map_blocks_flatmode(inode, map, flags); |
| 178 | } |
| 179 | |
Bhagyashri P. Dighole | f0950b0 | 2018-11-05 19:48:38 +0000 | [diff] [blame] | 180 | static inline struct bio *erofs_read_raw_page(struct bio *bio, |
| 181 | struct address_space *mapping, |
| 182 | struct page *page, |
| 183 | erofs_off_t *last_block, |
| 184 | unsigned int nblocks, |
| 185 | bool ra) |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 186 | { |
Gao Xiang | 14a56ec | 2019-03-25 11:40:09 +0800 | [diff] [blame] | 187 | struct inode *const inode = mapping->host; |
| 188 | struct super_block *const sb = inode->i_sb; |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 189 | erofs_off_t current_block = (erofs_off_t)page->index; |
| 190 | int err; |
| 191 | |
Chen Gong | 9141b60 | 2018-09-18 22:27:28 +0800 | [diff] [blame] | 192 | DBG_BUGON(!nblocks); |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 193 | |
| 194 | if (PageUptodate(page)) { |
| 195 | err = 0; |
| 196 | goto has_updated; |
| 197 | } |
| 198 | |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 199 | /* note that for readpage case, bio also equals to NULL */ |
Bhagyashri P. Dighole | d1ab8244 | 2018-11-05 19:49:05 +0000 | [diff] [blame] | 200 | if (bio && |
Bhagyashri P. Dighole | f0950b0 | 2018-11-05 19:48:38 +0000 | [diff] [blame] | 201 | /* not continuous */ |
| 202 | *last_block + 1 != current_block) { |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 203 | submit_bio_retry: |
| 204 | __submit_bio(bio, REQ_OP_READ, 0); |
| 205 | bio = NULL; |
| 206 | } |
| 207 | |
Bhagyashri P. Dighole | d1ab8244 | 2018-11-05 19:49:05 +0000 | [diff] [blame] | 208 | if (!bio) { |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 209 | struct erofs_map_blocks map = { |
| 210 | .m_la = blknr_to_addr(current_block), |
| 211 | }; |
Gao Xiang | 5544195 | 2018-07-26 20:22:00 +0800 | [diff] [blame] | 212 | erofs_blk_t blknr; |
Thomas Weißschuh | 7dd68b1 | 2018-09-10 21:41:14 +0200 | [diff] [blame] | 213 | unsigned int blkoff; |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 214 | |
| 215 | err = erofs_map_blocks(inode, &map, EROFS_GET_BLOCKS_RAW); |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 216 | if (err) |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 217 | goto err_out; |
| 218 | |
| 219 | /* zero out the holed page */ |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 220 | if (!(map.m_flags & EROFS_MAP_MAPPED)) { |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 221 | zero_user_segment(page, 0, PAGE_SIZE); |
| 222 | SetPageUptodate(page); |
| 223 | |
| 224 | /* imply err = 0, see erofs_map_blocks */ |
| 225 | goto has_updated; |
| 226 | } |
| 227 | |
| 228 | /* for RAW access mode, m_plen must be equal to m_llen */ |
Chen Gong | 9141b60 | 2018-09-18 22:27:28 +0800 | [diff] [blame] | 229 | DBG_BUGON(map.m_plen != map.m_llen); |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 230 | |
Gao Xiang | 5544195 | 2018-07-26 20:22:00 +0800 | [diff] [blame] | 231 | blknr = erofs_blknr(map.m_pa); |
| 232 | blkoff = erofs_blkoff(map.m_pa); |
| 233 | |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 234 | /* deal with inline page */ |
| 235 | if (map.m_flags & EROFS_MAP_META) { |
| 236 | void *vsrc, *vto; |
| 237 | struct page *ipage; |
| 238 | |
Chen Gong | 9141b60 | 2018-09-18 22:27:28 +0800 | [diff] [blame] | 239 | DBG_BUGON(map.m_plen > PAGE_SIZE); |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 240 | |
Gao Xiang | e655b5b | 2019-09-04 10:09:03 +0800 | [diff] [blame^] | 241 | ipage = erofs_get_meta_page(inode->i_sb, blknr); |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 242 | |
| 243 | if (IS_ERR(ipage)) { |
| 244 | err = PTR_ERR(ipage); |
| 245 | goto err_out; |
| 246 | } |
| 247 | |
| 248 | vsrc = kmap_atomic(ipage); |
| 249 | vto = kmap_atomic(page); |
Gao Xiang | 5544195 | 2018-07-26 20:22:00 +0800 | [diff] [blame] | 250 | memcpy(vto, vsrc + blkoff, map.m_plen); |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 251 | memset(vto + map.m_plen, 0, PAGE_SIZE - map.m_plen); |
| 252 | kunmap_atomic(vto); |
| 253 | kunmap_atomic(vsrc); |
| 254 | flush_dcache_page(page); |
| 255 | |
| 256 | SetPageUptodate(page); |
| 257 | /* TODO: could we unlock the page earlier? */ |
| 258 | unlock_page(ipage); |
| 259 | put_page(ipage); |
| 260 | |
| 261 | /* imply err = 0, see erofs_map_blocks */ |
| 262 | goto has_updated; |
| 263 | } |
| 264 | |
| 265 | /* pa must be block-aligned for raw reading */ |
Chen Gong | 9141b60 | 2018-09-18 22:27:28 +0800 | [diff] [blame] | 266 | DBG_BUGON(erofs_blkoff(map.m_pa)); |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 267 | |
| 268 | /* max # of continuous pages */ |
| 269 | if (nblocks > DIV_ROUND_UP(map.m_plen, PAGE_SIZE)) |
| 270 | nblocks = DIV_ROUND_UP(map.m_plen, PAGE_SIZE); |
| 271 | if (nblocks > BIO_MAX_PAGES) |
| 272 | nblocks = BIO_MAX_PAGES; |
| 273 | |
Gao Xiang | a5c0b78 | 2019-09-04 10:09:02 +0800 | [diff] [blame] | 274 | bio = erofs_grab_raw_bio(sb, blknr, nblocks); |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | err = bio_add_page(bio, page, PAGE_SIZE, 0); |
| 278 | /* out of the extent or bio is full */ |
| 279 | if (err < PAGE_SIZE) |
| 280 | goto submit_bio_retry; |
| 281 | |
| 282 | *last_block = current_block; |
| 283 | |
| 284 | /* shift in advance in case of it followed by too many gaps */ |
Gao Xiang | f4e97f5 | 2019-04-12 17:53:14 +0800 | [diff] [blame] | 285 | if (bio->bi_iter.bi_size >= bio->bi_max_vecs * PAGE_SIZE) { |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 286 | /* err should reassign to 0 after submitting */ |
| 287 | err = 0; |
| 288 | goto submit_bio_out; |
| 289 | } |
| 290 | |
| 291 | return bio; |
| 292 | |
| 293 | err_out: |
| 294 | /* for sync reading, set page error immediately */ |
| 295 | if (!ra) { |
| 296 | SetPageError(page); |
| 297 | ClearPageUptodate(page); |
| 298 | } |
| 299 | has_updated: |
| 300 | unlock_page(page); |
| 301 | |
| 302 | /* if updated manually, continuous pages has a gap */ |
Bhagyashri P. Dighole | d1ab8244 | 2018-11-05 19:49:05 +0000 | [diff] [blame] | 303 | if (bio) |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 304 | submit_bio_out: |
| 305 | __submit_bio(bio, REQ_OP_READ, 0); |
| 306 | |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 307 | return err ? ERR_PTR(err) : NULL; |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | /* |
| 311 | * since we dont have write or truncate flows, so no inode |
| 312 | * locking needs to be held at the moment. |
| 313 | */ |
| 314 | static int erofs_raw_access_readpage(struct file *file, struct page *page) |
| 315 | { |
| 316 | erofs_off_t last_block; |
| 317 | struct bio *bio; |
| 318 | |
Chao Yu | 13f06f4 | 2018-07-26 20:21:55 +0800 | [diff] [blame] | 319 | trace_erofs_readpage(page, true); |
| 320 | |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 321 | bio = erofs_read_raw_page(NULL, page->mapping, |
Bhagyashri P. Dighole | f0950b0 | 2018-11-05 19:48:38 +0000 | [diff] [blame] | 322 | page, &last_block, 1, false); |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 323 | |
| 324 | if (IS_ERR(bio)) |
| 325 | return PTR_ERR(bio); |
| 326 | |
Chen Gong | 9141b60 | 2018-09-18 22:27:28 +0800 | [diff] [blame] | 327 | DBG_BUGON(bio); /* since we have only one bio -- must be NULL */ |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | static int erofs_raw_access_readpages(struct file *filp, |
Bhagyashri P. Dighole | f0950b0 | 2018-11-05 19:48:38 +0000 | [diff] [blame] | 332 | struct address_space *mapping, |
| 333 | struct list_head *pages, |
| 334 | unsigned int nr_pages) |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 335 | { |
| 336 | erofs_off_t last_block; |
| 337 | struct bio *bio = NULL; |
| 338 | gfp_t gfp = readahead_gfp_mask(mapping); |
Chao Yu | 13f06f4 | 2018-07-26 20:21:55 +0800 | [diff] [blame] | 339 | struct page *page = list_last_entry(pages, struct page, lru); |
| 340 | |
| 341 | trace_erofs_readpages(mapping->host, page, nr_pages, true); |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 342 | |
| 343 | for (; nr_pages; --nr_pages) { |
Chao Yu | 13f06f4 | 2018-07-26 20:21:55 +0800 | [diff] [blame] | 344 | page = list_entry(pages->prev, struct page, lru); |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 345 | |
| 346 | prefetchw(&page->flags); |
| 347 | list_del(&page->lru); |
| 348 | |
| 349 | if (!add_to_page_cache_lru(page, mapping, page->index, gfp)) { |
| 350 | bio = erofs_read_raw_page(bio, mapping, page, |
Bhagyashri P. Dighole | f0950b0 | 2018-11-05 19:48:38 +0000 | [diff] [blame] | 351 | &last_block, nr_pages, true); |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 352 | |
| 353 | /* all the page errors are ignored when readahead */ |
| 354 | if (IS_ERR(bio)) { |
| 355 | pr_err("%s, readahead error at page %lu of nid %llu\n", |
Bhagyashri P. Dighole | f0950b0 | 2018-11-05 19:48:38 +0000 | [diff] [blame] | 356 | __func__, page->index, |
Gao Xiang | a5876e2 | 2019-09-04 10:08:56 +0800 | [diff] [blame] | 357 | EROFS_I(mapping->host)->nid); |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 358 | |
| 359 | bio = NULL; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | /* pages could still be locked */ |
| 364 | put_page(page); |
| 365 | } |
Chen Gong | 9141b60 | 2018-09-18 22:27:28 +0800 | [diff] [blame] | 366 | DBG_BUGON(!list_empty(pages)); |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 367 | |
| 368 | /* the rare case (end in gaps) */ |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 369 | if (bio) |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 370 | __submit_bio(bio, REQ_OP_READ, 0); |
| 371 | return 0; |
| 372 | } |
| 373 | |
Chao Yu | 9da681e | 2019-07-16 17:32:56 +0800 | [diff] [blame] | 374 | static int erofs_get_block(struct inode *inode, sector_t iblock, |
| 375 | struct buffer_head *bh, int create) |
| 376 | { |
| 377 | struct erofs_map_blocks map = { |
| 378 | .m_la = iblock << 9, |
| 379 | }; |
| 380 | int err; |
| 381 | |
| 382 | err = erofs_map_blocks(inode, &map, EROFS_GET_BLOCKS_RAW); |
| 383 | if (err) |
| 384 | return err; |
| 385 | |
| 386 | if (map.m_flags & EROFS_MAP_MAPPED) |
| 387 | bh->b_blocknr = erofs_blknr(map.m_pa); |
| 388 | |
| 389 | return err; |
| 390 | } |
| 391 | |
| 392 | static sector_t erofs_bmap(struct address_space *mapping, sector_t block) |
| 393 | { |
| 394 | struct inode *inode = mapping->host; |
| 395 | |
Gao Xiang | a5876e2 | 2019-09-04 10:08:56 +0800 | [diff] [blame] | 396 | if (EROFS_I(inode)->datalayout == EROFS_INODE_FLAT_INLINE) { |
Chao Yu | 9da681e | 2019-07-16 17:32:56 +0800 | [diff] [blame] | 397 | erofs_blk_t blks = i_size_read(inode) >> LOG_BLOCK_SIZE; |
| 398 | |
| 399 | if (block >> LOG_SECTORS_PER_BLOCK >= blks) |
| 400 | return 0; |
| 401 | } |
| 402 | |
| 403 | return generic_block_bmap(mapping, block, erofs_get_block); |
| 404 | } |
| 405 | |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 406 | /* for uncompressed (aligned) files and raw access for other files */ |
| 407 | const struct address_space_operations erofs_raw_access_aops = { |
| 408 | .readpage = erofs_raw_access_readpage, |
| 409 | .readpages = erofs_raw_access_readpages, |
Chao Yu | 9da681e | 2019-07-16 17:32:56 +0800 | [diff] [blame] | 410 | .bmap = erofs_bmap, |
Gao Xiang | 81781b0 | 2018-07-26 20:21:47 +0800 | [diff] [blame] | 411 | }; |
| 412 | |