blob: 28eda71bb1a9411094d6c4b4bdb657079d6545a1 [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 Xianga5c0b782019-09-04 10:09:02 +080041static 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 Xiange655b5b2019-09-04 10:09:03 +080054struct page *erofs_get_meta_page(struct super_block *sb, erofs_blk_t blkaddr)
Gao Xiang81781b02018-07-26 20:21:47 +080055{
Gao Xiang6e789012018-08-21 22:49:30 +080056 struct inode *const bd_inode = sb->s_bdev->bd_inode;
57 struct address_space *const mapping = bd_inode->i_mapping;
Gao Xiange655b5b2019-09-04 10:09:03 +080058 const gfp_t gfp = mapping_gfp_constraint(mapping, ~__GFP_FS);
Gao Xiang81781b02018-07-26 20:21:47 +080059 struct page *page;
Gao Xiang6e789012018-08-21 22:49:30 +080060 int err;
Gao Xiang81781b02018-07-26 20:21:47 +080061
62repeat:
Gao Xiang6e789012018-08-21 22:49:30 +080063 page = find_or_create_page(mapping, blkaddr, gfp);
Gao Xiange655b5b2019-09-04 10:09:03 +080064 if (!page)
Gao Xiang6e789012018-08-21 22:49:30 +080065 return ERR_PTR(-ENOMEM);
Gao Xiange655b5b2019-09-04 10:09:03 +080066
Gao Xiang6e789012018-08-21 22:49:30 +080067 DBG_BUGON(!PageLocked(page));
Gao Xiang81781b02018-07-26 20:21:47 +080068
69 if (!PageUptodate(page)) {
70 struct bio *bio;
Gao Xiang81781b02018-07-26 20:21:47 +080071
Gao Xianga5c0b782019-09-04 10:09:02 +080072 bio = erofs_grab_raw_bio(sb, blkaddr, 1);
Gao Xiang8be31272018-08-21 22:49:29 +080073
Gao Xiang097a8022019-08-30 01:17:41 +080074 if (bio_add_page(bio, page, PAGE_SIZE, 0) != PAGE_SIZE) {
Gao Xiang6e789012018-08-21 22:49:30 +080075 err = -EFAULT;
76 goto err_out;
77 }
Gao Xiang81781b02018-07-26 20:21:47 +080078
Gao Xiange655b5b2019-09-04 10:09:03 +080079 __submit_bio(bio, REQ_OP_READ, REQ_META);
Gao Xiang81781b02018-07-26 20:21:47 +080080 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 Xiang81781b02018-07-26 20:21:47 +080084 unlock_page(page);
85 put_page(page);
86 goto repeat;
87 }
88
89 /* more likely a read error */
Gao Xiang8d8a09b2019-08-30 00:38:27 +080090 if (!PageUptodate(page)) {
Gao Xiang6e789012018-08-21 22:49:30 +080091 err = -EIO;
92 goto err_out;
Gao Xiang81781b02018-07-26 20:21:47 +080093 }
94 }
95 return page;
Gao Xiang6e789012018-08-21 22:49:30 +080096
97err_out:
98 unlock_page(page);
99 put_page(page);
100 return ERR_PTR(err);
Gao Xiang81781b02018-07-26 20:21:47 +0800101}
102
103static int erofs_map_blocks_flatmode(struct inode *inode,
Bhagyashri P. Digholef0950b02018-11-05 19:48:38 +0000104 struct erofs_map_blocks *map,
105 int flags)
Gao Xiang81781b02018-07-26 20:21:47 +0800106{
Chen Gong9141b602018-09-18 22:27:28 +0800107 int err = 0;
Gao Xiang81781b02018-07-26 20:21:47 +0800108 erofs_blk_t nblocks, lastblk;
109 u64 offset = map->m_la;
Gao Xianga5876e22019-09-04 10:08:56 +0800110 struct erofs_inode *vi = EROFS_I(inode);
Gao Xiang8a765682019-09-04 10:08:54 +0800111 bool tailendpacking = (vi->datalayout == EROFS_INODE_FLAT_INLINE);
Gao Xiang81781b02018-07-26 20:21:47 +0800112
Chao Yu13f06f42018-07-26 20:21:55 +0800113 trace_erofs_map_blocks_flatmode_enter(inode, map, flags);
Gao Xiang81781b02018-07-26 20:21:47 +0800114
115 nblocks = DIV_ROUND_UP(inode->i_size, PAGE_SIZE);
Gao Xiang8a765682019-09-04 10:08:54 +0800116 lastblk = nblocks - tailendpacking;
Gao Xiang81781b02018-07-26 20:21:47 +0800117
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800118 if (offset >= inode->i_size) {
Gao Xiang81781b02018-07-26 20:21:47 +0800119 /* 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 Xiang8a765682019-09-04 10:08:54 +0800131 } else if (tailendpacking) {
Gao Xiang81781b02018-07-26 20:21:47 +0800132 /* 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 Xianga6b9b1d2019-08-14 18:37:03 +0800139 /* inline data should be located in one meta block */
Chen Gong9141b602018-09-18 22:27:28 +0800140 if (erofs_blkoff(map->m_pa) + map->m_plen > PAGE_SIZE) {
Gao Xianga6b9b1d2019-08-14 18:37:03 +0800141 errln("inline data cross block boundary @ nid %llu",
142 vi->nid);
Chen Gong9141b602018-09-18 22:27:28 +0800143 DBG_BUGON(1);
Gao Xianga6b9b1d2019-08-14 18:37:03 +0800144 err = -EFSCORRUPTED;
Chen Gong9141b602018-09-18 22:27:28 +0800145 goto err_out;
146 }
147
Gao Xiang81781b02018-07-26 20:21:47 +0800148 map->m_flags |= EROFS_MAP_META;
149 } else {
150 errln("internal error @ nid: %llu (size %llu), m_la 0x%llx",
Bhagyashri P. Digholef0950b02018-11-05 19:48:38 +0000151 vi->nid, inode->i_size, map->m_la);
Chen Gong9141b602018-09-18 22:27:28 +0800152 DBG_BUGON(1);
153 err = -EIO;
154 goto err_out;
Gao Xiang81781b02018-07-26 20:21:47 +0800155 }
156
157out:
158 map->m_llen = map->m_plen;
Chen Gong9141b602018-09-18 22:27:28 +0800159
160err_out:
Chao Yu13f06f42018-07-26 20:21:55 +0800161 trace_erofs_map_blocks_flatmode_exit(inode, map, flags, 0);
Chen Gong9141b602018-09-18 22:27:28 +0800162 return err;
Gao Xiang81781b02018-07-26 20:21:47 +0800163}
164
165int erofs_map_blocks(struct inode *inode,
Bhagyashri P. Digholef0950b02018-11-05 19:48:38 +0000166 struct erofs_map_blocks *map, int flags)
Gao Xiang81781b02018-07-26 20:21:47 +0800167{
Gao Xianga5876e22019-09-04 10:08:56 +0800168 if (erofs_inode_is_data_compressed(EROFS_I(inode)->datalayout)) {
Chao Yu3b423412019-01-15 09:42:21 +0800169 int err = z_erofs_map_blocks_iter(inode, map, flags);
Gao Xiang81781b02018-07-26 20:21:47 +0800170
Chao Yu3b423412019-01-15 09:42:21 +0800171 if (map->mpage) {
172 put_page(map->mpage);
173 map->mpage = NULL;
174 }
Gao Xiang02827e12018-07-26 20:21:58 +0800175 return err;
176 }
Gao Xiang81781b02018-07-26 20:21:47 +0800177 return erofs_map_blocks_flatmode(inode, map, flags);
178}
179
Bhagyashri P. Digholef0950b02018-11-05 19:48:38 +0000180static 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 Xiang81781b02018-07-26 20:21:47 +0800186{
Gao Xiang14a56ec2019-03-25 11:40:09 +0800187 struct inode *const inode = mapping->host;
188 struct super_block *const sb = inode->i_sb;
Gao Xiang81781b02018-07-26 20:21:47 +0800189 erofs_off_t current_block = (erofs_off_t)page->index;
190 int err;
191
Chen Gong9141b602018-09-18 22:27:28 +0800192 DBG_BUGON(!nblocks);
Gao Xiang81781b02018-07-26 20:21:47 +0800193
194 if (PageUptodate(page)) {
195 err = 0;
196 goto has_updated;
197 }
198
Gao Xiang81781b02018-07-26 20:21:47 +0800199 /* note that for readpage case, bio also equals to NULL */
Bhagyashri P. Digholed1ab82442018-11-05 19:49:05 +0000200 if (bio &&
Bhagyashri P. Digholef0950b02018-11-05 19:48:38 +0000201 /* not continuous */
202 *last_block + 1 != current_block) {
Gao Xiang81781b02018-07-26 20:21:47 +0800203submit_bio_retry:
204 __submit_bio(bio, REQ_OP_READ, 0);
205 bio = NULL;
206 }
207
Bhagyashri P. Digholed1ab82442018-11-05 19:49:05 +0000208 if (!bio) {
Gao Xiang81781b02018-07-26 20:21:47 +0800209 struct erofs_map_blocks map = {
210 .m_la = blknr_to_addr(current_block),
211 };
Gao Xiang55441952018-07-26 20:22:00 +0800212 erofs_blk_t blknr;
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200213 unsigned int blkoff;
Gao Xiang81781b02018-07-26 20:21:47 +0800214
215 err = erofs_map_blocks(inode, &map, EROFS_GET_BLOCKS_RAW);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800216 if (err)
Gao Xiang81781b02018-07-26 20:21:47 +0800217 goto err_out;
218
219 /* zero out the holed page */
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800220 if (!(map.m_flags & EROFS_MAP_MAPPED)) {
Gao Xiang81781b02018-07-26 20:21:47 +0800221 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 Gong9141b602018-09-18 22:27:28 +0800229 DBG_BUGON(map.m_plen != map.m_llen);
Gao Xiang81781b02018-07-26 20:21:47 +0800230
Gao Xiang55441952018-07-26 20:22:00 +0800231 blknr = erofs_blknr(map.m_pa);
232 blkoff = erofs_blkoff(map.m_pa);
233
Gao Xiang81781b02018-07-26 20:21:47 +0800234 /* deal with inline page */
235 if (map.m_flags & EROFS_MAP_META) {
236 void *vsrc, *vto;
237 struct page *ipage;
238
Chen Gong9141b602018-09-18 22:27:28 +0800239 DBG_BUGON(map.m_plen > PAGE_SIZE);
Gao Xiang81781b02018-07-26 20:21:47 +0800240
Gao Xiange655b5b2019-09-04 10:09:03 +0800241 ipage = erofs_get_meta_page(inode->i_sb, blknr);
Gao Xiang81781b02018-07-26 20:21:47 +0800242
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 Xiang55441952018-07-26 20:22:00 +0800250 memcpy(vto, vsrc + blkoff, map.m_plen);
Gao Xiang81781b02018-07-26 20:21:47 +0800251 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 Gong9141b602018-09-18 22:27:28 +0800266 DBG_BUGON(erofs_blkoff(map.m_pa));
Gao Xiang81781b02018-07-26 20:21:47 +0800267
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 Xianga5c0b782019-09-04 10:09:02 +0800274 bio = erofs_grab_raw_bio(sb, blknr, nblocks);
Gao Xiang81781b02018-07-26 20:21:47 +0800275 }
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 Xiangf4e97f52019-04-12 17:53:14 +0800285 if (bio->bi_iter.bi_size >= bio->bi_max_vecs * PAGE_SIZE) {
Gao Xiang81781b02018-07-26 20:21:47 +0800286 /* err should reassign to 0 after submitting */
287 err = 0;
288 goto submit_bio_out;
289 }
290
291 return bio;
292
293err_out:
294 /* for sync reading, set page error immediately */
295 if (!ra) {
296 SetPageError(page);
297 ClearPageUptodate(page);
298 }
299has_updated:
300 unlock_page(page);
301
302 /* if updated manually, continuous pages has a gap */
Bhagyashri P. Digholed1ab82442018-11-05 19:49:05 +0000303 if (bio)
Gao Xiang81781b02018-07-26 20:21:47 +0800304submit_bio_out:
305 __submit_bio(bio, REQ_OP_READ, 0);
306
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800307 return err ? ERR_PTR(err) : NULL;
Gao Xiang81781b02018-07-26 20:21:47 +0800308}
309
310/*
311 * since we dont have write or truncate flows, so no inode
312 * locking needs to be held at the moment.
313 */
314static int erofs_raw_access_readpage(struct file *file, struct page *page)
315{
316 erofs_off_t last_block;
317 struct bio *bio;
318
Chao Yu13f06f42018-07-26 20:21:55 +0800319 trace_erofs_readpage(page, true);
320
Gao Xiang81781b02018-07-26 20:21:47 +0800321 bio = erofs_read_raw_page(NULL, page->mapping,
Bhagyashri P. Digholef0950b02018-11-05 19:48:38 +0000322 page, &last_block, 1, false);
Gao Xiang81781b02018-07-26 20:21:47 +0800323
324 if (IS_ERR(bio))
325 return PTR_ERR(bio);
326
Chen Gong9141b602018-09-18 22:27:28 +0800327 DBG_BUGON(bio); /* since we have only one bio -- must be NULL */
Gao Xiang81781b02018-07-26 20:21:47 +0800328 return 0;
329}
330
331static int erofs_raw_access_readpages(struct file *filp,
Bhagyashri P. Digholef0950b02018-11-05 19:48:38 +0000332 struct address_space *mapping,
333 struct list_head *pages,
334 unsigned int nr_pages)
Gao Xiang81781b02018-07-26 20:21:47 +0800335{
336 erofs_off_t last_block;
337 struct bio *bio = NULL;
338 gfp_t gfp = readahead_gfp_mask(mapping);
Chao Yu13f06f42018-07-26 20:21:55 +0800339 struct page *page = list_last_entry(pages, struct page, lru);
340
341 trace_erofs_readpages(mapping->host, page, nr_pages, true);
Gao Xiang81781b02018-07-26 20:21:47 +0800342
343 for (; nr_pages; --nr_pages) {
Chao Yu13f06f42018-07-26 20:21:55 +0800344 page = list_entry(pages->prev, struct page, lru);
Gao Xiang81781b02018-07-26 20:21:47 +0800345
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. Digholef0950b02018-11-05 19:48:38 +0000351 &last_block, nr_pages, true);
Gao Xiang81781b02018-07-26 20:21:47 +0800352
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. Digholef0950b02018-11-05 19:48:38 +0000356 __func__, page->index,
Gao Xianga5876e22019-09-04 10:08:56 +0800357 EROFS_I(mapping->host)->nid);
Gao Xiang81781b02018-07-26 20:21:47 +0800358
359 bio = NULL;
360 }
361 }
362
363 /* pages could still be locked */
364 put_page(page);
365 }
Chen Gong9141b602018-09-18 22:27:28 +0800366 DBG_BUGON(!list_empty(pages));
Gao Xiang81781b02018-07-26 20:21:47 +0800367
368 /* the rare case (end in gaps) */
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800369 if (bio)
Gao Xiang81781b02018-07-26 20:21:47 +0800370 __submit_bio(bio, REQ_OP_READ, 0);
371 return 0;
372}
373
Chao Yu9da681e2019-07-16 17:32:56 +0800374static 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
392static sector_t erofs_bmap(struct address_space *mapping, sector_t block)
393{
394 struct inode *inode = mapping->host;
395
Gao Xianga5876e22019-09-04 10:08:56 +0800396 if (EROFS_I(inode)->datalayout == EROFS_INODE_FLAT_INLINE) {
Chao Yu9da681e2019-07-16 17:32:56 +0800397 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 Xiang81781b02018-07-26 20:21:47 +0800406/* for uncompressed (aligned) files and raw access for other files */
407const struct address_space_operations erofs_raw_access_aops = {
408 .readpage = erofs_raw_access_readpage,
409 .readpages = erofs_raw_access_readpages,
Chao Yu9da681e2019-07-16 17:32:56 +0800410 .bmap = erofs_bmap,
Gao Xiang81781b02018-07-26 20:21:47 +0800411};
412