blob: be11b5ea9d2e58e075ec139b5cd757ba30b5180f [file] [log] [blame]
Gao Xiang29b24f62019-07-31 23:57:31 +08001// SPDX-License-Identifier: GPL-2.0-only
Gao Xiang81781b02018-07-26 20:21:47 +08002/*
Gao Xiang81781b02018-07-26 20:21:47 +08003 * Copyright (C) 2017-2018 HUAWEI, Inc.
4 * http://www.huawei.com/
5 * Created by Gao Xiang <gaoxiang25@huawei.com>
Gao Xiang81781b02018-07-26 20:21:47 +08006 */
7#include "internal.h"
8#include <linux/prefetch.h>
9
Chao Yu13f06f42018-07-26 20:21:55 +080010#include <trace/events/erofs.h>
11
Gao Xiang81781b02018-07-26 20:21:47 +080012static inline void read_endio(struct bio *bio)
13{
Gao Xiang14a56ec2019-03-25 11:40:09 +080014 struct super_block *const sb = bio->bi_private;
Gao Xiang81781b02018-07-26 20:21:47 +080015 struct bio_vec *bvec;
Gao Xiang14a56ec2019-03-25 11:40:09 +080016 blk_status_t err = bio->bi_status;
Ming Lei6dc4f102019-02-15 19:13:19 +080017 struct bvec_iter_all iter_all;
Gao Xiang81781b02018-07-26 20:21:47 +080018
Gao Xiang14a56ec2019-03-25 11:40:09 +080019 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 Hellwig2b070cf2019-04-25 09:03:00 +020024 bio_for_each_segment_all(bvec, bio, iter_all) {
Gao Xiang81781b02018-07-26 20:21:47 +080025 struct page *page = bvec->bv_page;
26
27 /* page is already locked */
Chen Gong9141b602018-09-18 22:27:28 +080028 DBG_BUGON(PageUptodate(page));
Gao Xiang81781b02018-07-26 20:21:47 +080029
Gao Xiang8d8a09b2019-08-30 00:38:27 +080030 if (err)
Gao Xiang81781b02018-07-26 20:21:47 +080031 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 Xiang81781b02018-07-26 20:21:47 +080041/* prio -- true is used for dir */
Gao Xiang6e789012018-08-21 22:49:30 +080042struct page *__erofs_get_meta_page(struct super_block *sb,
Bhagyashri P. Digholef0950b02018-11-05 19:48:38 +000043 erofs_blk_t blkaddr, bool prio, bool nofail)
Gao Xiang81781b02018-07-26 20:21:47 +080044{
Gao Xiang6e789012018-08-21 22:49:30 +080045 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 Xiang81781b02018-07-26 20:21:47 +080051 struct page *page;
Gao Xiang6e789012018-08-21 22:49:30 +080052 int err;
Gao Xiang81781b02018-07-26 20:21:47 +080053
54repeat:
Gao Xiang6e789012018-08-21 22:49:30 +080055 page = find_or_create_page(mapping, blkaddr, gfp);
Gao Xiang8d8a09b2019-08-30 00:38:27 +080056 if (!page) {
Gao Xiang6e789012018-08-21 22:49:30 +080057 DBG_BUGON(nofail);
58 return ERR_PTR(-ENOMEM);
59 }
60 DBG_BUGON(!PageLocked(page));
Gao Xiang81781b02018-07-26 20:21:47 +080061
62 if (!PageUptodate(page)) {
63 struct bio *bio;
Gao Xiang81781b02018-07-26 20:21:47 +080064
Gao Xiang14a56ec2019-03-25 11:40:09 +080065 bio = erofs_grab_bio(sb, blkaddr, 1, sb, read_endio, nofail);
Gao Xiang6e789012018-08-21 22:49:30 +080066 if (IS_ERR(bio)) {
67 DBG_BUGON(nofail);
68 err = PTR_ERR(bio);
69 goto err_out;
70 }
Gao Xiang8be31272018-08-21 22:49:29 +080071
Gao Xiang097a8022019-08-30 01:17:41 +080072 if (bio_add_page(bio, page, PAGE_SIZE, 0) != PAGE_SIZE) {
Gao Xiang6e789012018-08-21 22:49:30 +080073 err = -EFAULT;
74 goto err_out;
75 }
Gao Xiang81781b02018-07-26 20:21:47 +080076
77 __submit_bio(bio, REQ_OP_READ,
Bhagyashri P. Digholef0950b02018-11-05 19:48:38 +000078 REQ_META | (prio ? REQ_PRIO : 0));
Gao Xiang81781b02018-07-26 20:21:47 +080079
80 lock_page(page);
81
Gao Xiang6e789012018-08-21 22:49:30 +080082 /* this page has been truncated by others */
Gao Xiang8d8a09b2019-08-30 00:38:27 +080083 if (page->mapping != mapping) {
Gao Xiang6e789012018-08-21 22:49:30 +080084unlock_repeat:
Gao Xiang81781b02018-07-26 20:21:47 +080085 unlock_page(page);
86 put_page(page);
87 goto repeat;
88 }
89
90 /* more likely a read error */
Gao Xiang8d8a09b2019-08-30 00:38:27 +080091 if (!PageUptodate(page)) {
Gao Xiang6e789012018-08-21 22:49:30 +080092 if (io_retries) {
93 --io_retries;
94 goto unlock_repeat;
95 }
96 err = -EIO;
97 goto err_out;
Gao Xiang81781b02018-07-26 20:21:47 +080098 }
99 }
100 return page;
Gao Xiang6e789012018-08-21 22:49:30 +0800101
102err_out:
103 unlock_page(page);
104 put_page(page);
105 return ERR_PTR(err);
Gao Xiang81781b02018-07-26 20:21:47 +0800106}
107
108static int erofs_map_blocks_flatmode(struct inode *inode,
Bhagyashri P. Digholef0950b02018-11-05 19:48:38 +0000109 struct erofs_map_blocks *map,
110 int flags)
Gao Xiang81781b02018-07-26 20:21:47 +0800111{
Chen Gong9141b602018-09-18 22:27:28 +0800112 int err = 0;
Gao Xiang81781b02018-07-26 20:21:47 +0800113 erofs_blk_t nblocks, lastblk;
114 u64 offset = map->m_la;
Gao Xianga5876e22019-09-04 10:08:56 +0800115 struct erofs_inode *vi = EROFS_I(inode);
Gao Xiang8a765682019-09-04 10:08:54 +0800116 bool tailendpacking = (vi->datalayout == EROFS_INODE_FLAT_INLINE);
Gao Xiang81781b02018-07-26 20:21:47 +0800117
Chao Yu13f06f42018-07-26 20:21:55 +0800118 trace_erofs_map_blocks_flatmode_enter(inode, map, flags);
Gao Xiang81781b02018-07-26 20:21:47 +0800119
120 nblocks = DIV_ROUND_UP(inode->i_size, PAGE_SIZE);
Gao Xiang8a765682019-09-04 10:08:54 +0800121 lastblk = nblocks - tailendpacking;
Gao Xiang81781b02018-07-26 20:21:47 +0800122
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800123 if (offset >= inode->i_size) {
Gao Xiang81781b02018-07-26 20:21:47 +0800124 /* 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 Xiang8a765682019-09-04 10:08:54 +0800136 } else if (tailendpacking) {
Gao Xiang81781b02018-07-26 20:21:47 +0800137 /* 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 Xianga6b9b1d2019-08-14 18:37:03 +0800144 /* inline data should be located in one meta block */
Chen Gong9141b602018-09-18 22:27:28 +0800145 if (erofs_blkoff(map->m_pa) + map->m_plen > PAGE_SIZE) {
Gao Xianga6b9b1d2019-08-14 18:37:03 +0800146 errln("inline data cross block boundary @ nid %llu",
147 vi->nid);
Chen Gong9141b602018-09-18 22:27:28 +0800148 DBG_BUGON(1);
Gao Xianga6b9b1d2019-08-14 18:37:03 +0800149 err = -EFSCORRUPTED;
Chen Gong9141b602018-09-18 22:27:28 +0800150 goto err_out;
151 }
152
Gao Xiang81781b02018-07-26 20:21:47 +0800153 map->m_flags |= EROFS_MAP_META;
154 } else {
155 errln("internal error @ nid: %llu (size %llu), m_la 0x%llx",
Bhagyashri P. Digholef0950b02018-11-05 19:48:38 +0000156 vi->nid, inode->i_size, map->m_la);
Chen Gong9141b602018-09-18 22:27:28 +0800157 DBG_BUGON(1);
158 err = -EIO;
159 goto err_out;
Gao Xiang81781b02018-07-26 20:21:47 +0800160 }
161
162out:
163 map->m_llen = map->m_plen;
Chen Gong9141b602018-09-18 22:27:28 +0800164
165err_out:
Chao Yu13f06f42018-07-26 20:21:55 +0800166 trace_erofs_map_blocks_flatmode_exit(inode, map, flags, 0);
Chen Gong9141b602018-09-18 22:27:28 +0800167 return err;
Gao Xiang81781b02018-07-26 20:21:47 +0800168}
169
170int erofs_map_blocks(struct inode *inode,
Bhagyashri P. Digholef0950b02018-11-05 19:48:38 +0000171 struct erofs_map_blocks *map, int flags)
Gao Xiang81781b02018-07-26 20:21:47 +0800172{
Gao Xianga5876e22019-09-04 10:08:56 +0800173 if (erofs_inode_is_data_compressed(EROFS_I(inode)->datalayout)) {
Chao Yu3b423412019-01-15 09:42:21 +0800174 int err = z_erofs_map_blocks_iter(inode, map, flags);
Gao Xiang81781b02018-07-26 20:21:47 +0800175
Chao Yu3b423412019-01-15 09:42:21 +0800176 if (map->mpage) {
177 put_page(map->mpage);
178 map->mpage = NULL;
179 }
Gao Xiang02827e12018-07-26 20:21:58 +0800180 return err;
181 }
Gao Xiang81781b02018-07-26 20:21:47 +0800182 return erofs_map_blocks_flatmode(inode, map, flags);
183}
184
Bhagyashri P. Digholef0950b02018-11-05 19:48:38 +0000185static 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 Xiang81781b02018-07-26 20:21:47 +0800191{
Gao Xiang14a56ec2019-03-25 11:40:09 +0800192 struct inode *const inode = mapping->host;
193 struct super_block *const sb = inode->i_sb;
Gao Xiang81781b02018-07-26 20:21:47 +0800194 erofs_off_t current_block = (erofs_off_t)page->index;
195 int err;
196
Chen Gong9141b602018-09-18 22:27:28 +0800197 DBG_BUGON(!nblocks);
Gao Xiang81781b02018-07-26 20:21:47 +0800198
199 if (PageUptodate(page)) {
200 err = 0;
201 goto has_updated;
202 }
203
Gao Xiang81781b02018-07-26 20:21:47 +0800204 /* note that for readpage case, bio also equals to NULL */
Bhagyashri P. Digholed1ab82442018-11-05 19:49:05 +0000205 if (bio &&
Bhagyashri P. Digholef0950b02018-11-05 19:48:38 +0000206 /* not continuous */
207 *last_block + 1 != current_block) {
Gao Xiang81781b02018-07-26 20:21:47 +0800208submit_bio_retry:
209 __submit_bio(bio, REQ_OP_READ, 0);
210 bio = NULL;
211 }
212
Bhagyashri P. Digholed1ab82442018-11-05 19:49:05 +0000213 if (!bio) {
Gao Xiang81781b02018-07-26 20:21:47 +0800214 struct erofs_map_blocks map = {
215 .m_la = blknr_to_addr(current_block),
216 };
Gao Xiang55441952018-07-26 20:22:00 +0800217 erofs_blk_t blknr;
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200218 unsigned int blkoff;
Gao Xiang81781b02018-07-26 20:21:47 +0800219
220 err = erofs_map_blocks(inode, &map, EROFS_GET_BLOCKS_RAW);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800221 if (err)
Gao Xiang81781b02018-07-26 20:21:47 +0800222 goto err_out;
223
224 /* zero out the holed page */
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800225 if (!(map.m_flags & EROFS_MAP_MAPPED)) {
Gao Xiang81781b02018-07-26 20:21:47 +0800226 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 Gong9141b602018-09-18 22:27:28 +0800234 DBG_BUGON(map.m_plen != map.m_llen);
Gao Xiang81781b02018-07-26 20:21:47 +0800235
Gao Xiang55441952018-07-26 20:22:00 +0800236 blknr = erofs_blknr(map.m_pa);
237 blkoff = erofs_blkoff(map.m_pa);
238
Gao Xiang81781b02018-07-26 20:21:47 +0800239 /* deal with inline page */
240 if (map.m_flags & EROFS_MAP_META) {
241 void *vsrc, *vto;
242 struct page *ipage;
243
Chen Gong9141b602018-09-18 22:27:28 +0800244 DBG_BUGON(map.m_plen > PAGE_SIZE);
Gao Xiang81781b02018-07-26 20:21:47 +0800245
Gao Xiang55441952018-07-26 20:22:00 +0800246 ipage = erofs_get_meta_page(inode->i_sb, blknr, 0);
Gao Xiang81781b02018-07-26 20:21:47 +0800247
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 Xiang55441952018-07-26 20:22:00 +0800255 memcpy(vto, vsrc + blkoff, map.m_plen);
Gao Xiang81781b02018-07-26 20:21:47 +0800256 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 Gong9141b602018-09-18 22:27:28 +0800271 DBG_BUGON(erofs_blkoff(map.m_pa));
Gao Xiang81781b02018-07-26 20:21:47 +0800272
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 Xiang14a56ec2019-03-25 11:40:09 +0800279 bio = erofs_grab_bio(sb, blknr, nblocks, sb,
280 read_endio, false);
Gao Xiang8be31272018-08-21 22:49:29 +0800281 if (IS_ERR(bio)) {
282 err = PTR_ERR(bio);
283 bio = NULL;
284 goto err_out;
285 }
Gao Xiang81781b02018-07-26 20:21:47 +0800286 }
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 Xiangf4e97f52019-04-12 17:53:14 +0800296 if (bio->bi_iter.bi_size >= bio->bi_max_vecs * PAGE_SIZE) {
Gao Xiang81781b02018-07-26 20:21:47 +0800297 /* err should reassign to 0 after submitting */
298 err = 0;
299 goto submit_bio_out;
300 }
301
302 return bio;
303
304err_out:
305 /* for sync reading, set page error immediately */
306 if (!ra) {
307 SetPageError(page);
308 ClearPageUptodate(page);
309 }
310has_updated:
311 unlock_page(page);
312
313 /* if updated manually, continuous pages has a gap */
Bhagyashri P. Digholed1ab82442018-11-05 19:49:05 +0000314 if (bio)
Gao Xiang81781b02018-07-26 20:21:47 +0800315submit_bio_out:
316 __submit_bio(bio, REQ_OP_READ, 0);
317
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800318 return err ? ERR_PTR(err) : NULL;
Gao Xiang81781b02018-07-26 20:21:47 +0800319}
320
321/*
322 * since we dont have write or truncate flows, so no inode
323 * locking needs to be held at the moment.
324 */
325static int erofs_raw_access_readpage(struct file *file, struct page *page)
326{
327 erofs_off_t last_block;
328 struct bio *bio;
329
Chao Yu13f06f42018-07-26 20:21:55 +0800330 trace_erofs_readpage(page, true);
331
Gao Xiang81781b02018-07-26 20:21:47 +0800332 bio = erofs_read_raw_page(NULL, page->mapping,
Bhagyashri P. Digholef0950b02018-11-05 19:48:38 +0000333 page, &last_block, 1, false);
Gao Xiang81781b02018-07-26 20:21:47 +0800334
335 if (IS_ERR(bio))
336 return PTR_ERR(bio);
337
Chen Gong9141b602018-09-18 22:27:28 +0800338 DBG_BUGON(bio); /* since we have only one bio -- must be NULL */
Gao Xiang81781b02018-07-26 20:21:47 +0800339 return 0;
340}
341
342static int erofs_raw_access_readpages(struct file *filp,
Bhagyashri P. Digholef0950b02018-11-05 19:48:38 +0000343 struct address_space *mapping,
344 struct list_head *pages,
345 unsigned int nr_pages)
Gao Xiang81781b02018-07-26 20:21:47 +0800346{
347 erofs_off_t last_block;
348 struct bio *bio = NULL;
349 gfp_t gfp = readahead_gfp_mask(mapping);
Chao Yu13f06f42018-07-26 20:21:55 +0800350 struct page *page = list_last_entry(pages, struct page, lru);
351
352 trace_erofs_readpages(mapping->host, page, nr_pages, true);
Gao Xiang81781b02018-07-26 20:21:47 +0800353
354 for (; nr_pages; --nr_pages) {
Chao Yu13f06f42018-07-26 20:21:55 +0800355 page = list_entry(pages->prev, struct page, lru);
Gao Xiang81781b02018-07-26 20:21:47 +0800356
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. Digholef0950b02018-11-05 19:48:38 +0000362 &last_block, nr_pages, true);
Gao Xiang81781b02018-07-26 20:21:47 +0800363
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. Digholef0950b02018-11-05 19:48:38 +0000367 __func__, page->index,
Gao Xianga5876e22019-09-04 10:08:56 +0800368 EROFS_I(mapping->host)->nid);
Gao Xiang81781b02018-07-26 20:21:47 +0800369
370 bio = NULL;
371 }
372 }
373
374 /* pages could still be locked */
375 put_page(page);
376 }
Chen Gong9141b602018-09-18 22:27:28 +0800377 DBG_BUGON(!list_empty(pages));
Gao Xiang81781b02018-07-26 20:21:47 +0800378
379 /* the rare case (end in gaps) */
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800380 if (bio)
Gao Xiang81781b02018-07-26 20:21:47 +0800381 __submit_bio(bio, REQ_OP_READ, 0);
382 return 0;
383}
384
Chao Yu9da681e2019-07-16 17:32:56 +0800385static 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
403static sector_t erofs_bmap(struct address_space *mapping, sector_t block)
404{
405 struct inode *inode = mapping->host;
406
Gao Xianga5876e22019-09-04 10:08:56 +0800407 if (EROFS_I(inode)->datalayout == EROFS_INODE_FLAT_INLINE) {
Chao Yu9da681e2019-07-16 17:32:56 +0800408 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 Xiang81781b02018-07-26 20:21:47 +0800417/* for uncompressed (aligned) files and raw access for other files */
418const struct address_space_operations erofs_raw_access_aops = {
419 .readpage = erofs_raw_access_readpage,
420 .readpages = erofs_raw_access_readpages,
Chao Yu9da681e2019-07-16 17:32:56 +0800421 .bmap = erofs_bmap,
Gao Xiang81781b02018-07-26 20:21:47 +0800422};
423