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