blob: 1249e74b3bf04267cf2f67dfd5af4596e0e9bdcf [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.
Alexander A. Klimov592e7cd2020-07-13 15:09:44 +02004 * https://www.huawei.com/
Gao Xiang81781b02018-07-26 20:21:47 +08005 * 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 Xiang99634bf2019-09-04 10:09:05 +080012static void erofs_readendio(struct bio *bio)
Gao Xiang81781b02018-07-26 20:21:47 +080013{
Gao Xiang81781b02018-07-26 20:21:47 +080014 struct bio_vec *bvec;
Gao Xiang14a56ec2019-03-25 11:40:09 +080015 blk_status_t err = bio->bi_status;
Ming Lei6dc4f102019-02-15 19:13:19 +080016 struct bvec_iter_all iter_all;
Gao Xiang81781b02018-07-26 20:21:47 +080017
Christoph Hellwig2b070cf2019-04-25 09:03:00 +020018 bio_for_each_segment_all(bvec, bio, iter_all) {
Gao Xiang81781b02018-07-26 20:21:47 +080019 struct page *page = bvec->bv_page;
20
21 /* page is already locked */
Chen Gong9141b602018-09-18 22:27:28 +080022 DBG_BUGON(PageUptodate(page));
Gao Xiang81781b02018-07-26 20:21:47 +080023
Gao Xiang8d8a09b2019-08-30 00:38:27 +080024 if (err)
Gao Xiang81781b02018-07-26 20:21:47 +080025 SetPageError(page);
26 else
27 SetPageUptodate(page);
28
29 unlock_page(page);
30 /* page could be reclaimed now */
31 }
32 bio_put(bio);
33}
34
Gao Xiange655b5b2019-09-04 10:09:03 +080035struct page *erofs_get_meta_page(struct super_block *sb, erofs_blk_t blkaddr)
Gao Xiang81781b02018-07-26 20:21:47 +080036{
Gao Xiang55252ab2019-09-22 02:43:55 +080037 struct address_space *const mapping = sb->s_bdev->bd_inode->i_mapping;
38 struct page *page;
Gao Xiang81781b02018-07-26 20:21:47 +080039
Gao Xiang55252ab2019-09-22 02:43:55 +080040 page = read_cache_page_gfp(mapping, blkaddr,
Gao Xiang618f40e2019-09-04 10:09:12 +080041 mapping_gfp_constraint(mapping, ~__GFP_FS));
Gao Xiang55252ab2019-09-22 02:43:55 +080042 /* should already be PageUptodate */
43 if (!IS_ERR(page))
44 lock_page(page);
45 return page;
Gao Xiang81781b02018-07-26 20:21:47 +080046}
47
48static int erofs_map_blocks_flatmode(struct inode *inode,
Bhagyashri P. Digholef0950b02018-11-05 19:48:38 +000049 struct erofs_map_blocks *map,
50 int flags)
Gao Xiang81781b02018-07-26 20:21:47 +080051{
Chen Gong9141b602018-09-18 22:27:28 +080052 int err = 0;
Gao Xiang81781b02018-07-26 20:21:47 +080053 erofs_blk_t nblocks, lastblk;
54 u64 offset = map->m_la;
Gao Xianga5876e22019-09-04 10:08:56 +080055 struct erofs_inode *vi = EROFS_I(inode);
Gao Xiang8a765682019-09-04 10:08:54 +080056 bool tailendpacking = (vi->datalayout == EROFS_INODE_FLAT_INLINE);
Gao Xiang81781b02018-07-26 20:21:47 +080057
Chao Yu13f06f42018-07-26 20:21:55 +080058 trace_erofs_map_blocks_flatmode_enter(inode, map, flags);
Gao Xiang81781b02018-07-26 20:21:47 +080059
60 nblocks = DIV_ROUND_UP(inode->i_size, PAGE_SIZE);
Gao Xiang8a765682019-09-04 10:08:54 +080061 lastblk = nblocks - tailendpacking;
Gao Xiang81781b02018-07-26 20:21:47 +080062
Gao Xiang8d8a09b2019-08-30 00:38:27 +080063 if (offset >= inode->i_size) {
Gao Xiang81781b02018-07-26 20:21:47 +080064 /* leave out-of-bound access unmapped */
65 map->m_flags = 0;
66 map->m_plen = 0;
67 goto out;
68 }
69
70 /* there is no hole in flatmode */
71 map->m_flags = EROFS_MAP_MAPPED;
72
73 if (offset < blknr_to_addr(lastblk)) {
74 map->m_pa = blknr_to_addr(vi->raw_blkaddr) + map->m_la;
75 map->m_plen = blknr_to_addr(lastblk) - offset;
Gao Xiang8a765682019-09-04 10:08:54 +080076 } else if (tailendpacking) {
Gao Xiang81781b02018-07-26 20:21:47 +080077 /* 2 - inode inline B: inode, [xattrs], inline last blk... */
78 struct erofs_sb_info *sbi = EROFS_SB(inode->i_sb);
79
80 map->m_pa = iloc(sbi, vi->nid) + vi->inode_isize +
81 vi->xattr_isize + erofs_blkoff(map->m_la);
82 map->m_plen = inode->i_size - offset;
83
Gao Xianga6b9b1d2019-08-14 18:37:03 +080084 /* inline data should be located in one meta block */
Chen Gong9141b602018-09-18 22:27:28 +080085 if (erofs_blkoff(map->m_pa) + map->m_plen > PAGE_SIZE) {
Gao Xiang4f761fa2019-09-04 10:09:09 +080086 erofs_err(inode->i_sb,
87 "inline data cross block boundary @ nid %llu",
88 vi->nid);
Chen Gong9141b602018-09-18 22:27:28 +080089 DBG_BUGON(1);
Gao Xianga6b9b1d2019-08-14 18:37:03 +080090 err = -EFSCORRUPTED;
Chen Gong9141b602018-09-18 22:27:28 +080091 goto err_out;
92 }
93
Gao Xiang81781b02018-07-26 20:21:47 +080094 map->m_flags |= EROFS_MAP_META;
95 } else {
Gao Xiang4f761fa2019-09-04 10:09:09 +080096 erofs_err(inode->i_sb,
97 "internal error @ nid: %llu (size %llu), m_la 0x%llx",
98 vi->nid, inode->i_size, map->m_la);
Chen Gong9141b602018-09-18 22:27:28 +080099 DBG_BUGON(1);
100 err = -EIO;
101 goto err_out;
Gao Xiang81781b02018-07-26 20:21:47 +0800102 }
103
104out:
105 map->m_llen = map->m_plen;
Chen Gong9141b602018-09-18 22:27:28 +0800106
107err_out:
Chao Yu13f06f42018-07-26 20:21:55 +0800108 trace_erofs_map_blocks_flatmode_exit(inode, map, flags, 0);
Chen Gong9141b602018-09-18 22:27:28 +0800109 return err;
Gao Xiang81781b02018-07-26 20:21:47 +0800110}
111
112int erofs_map_blocks(struct inode *inode,
Bhagyashri P. Digholef0950b02018-11-05 19:48:38 +0000113 struct erofs_map_blocks *map, int flags)
Gao Xiang81781b02018-07-26 20:21:47 +0800114{
Gao Xianga5876e22019-09-04 10:08:56 +0800115 if (erofs_inode_is_data_compressed(EROFS_I(inode)->datalayout)) {
Chao Yu3b423412019-01-15 09:42:21 +0800116 int err = z_erofs_map_blocks_iter(inode, map, flags);
Gao Xiang81781b02018-07-26 20:21:47 +0800117
Chao Yu3b423412019-01-15 09:42:21 +0800118 if (map->mpage) {
119 put_page(map->mpage);
120 map->mpage = NULL;
121 }
Gao Xiang02827e12018-07-26 20:21:58 +0800122 return err;
123 }
Gao Xiang81781b02018-07-26 20:21:47 +0800124 return erofs_map_blocks_flatmode(inode, map, flags);
125}
126
Bhagyashri P. Digholef0950b02018-11-05 19:48:38 +0000127static inline struct bio *erofs_read_raw_page(struct bio *bio,
128 struct address_space *mapping,
129 struct page *page,
130 erofs_off_t *last_block,
131 unsigned int nblocks,
Gao Xiang9f377622021-03-06 12:04:38 +0800132 unsigned int *eblks,
Bhagyashri P. Digholef0950b02018-11-05 19:48:38 +0000133 bool ra)
Gao Xiang81781b02018-07-26 20:21:47 +0800134{
Gao Xiang14a56ec2019-03-25 11:40:09 +0800135 struct inode *const inode = mapping->host;
136 struct super_block *const sb = inode->i_sb;
Gao Xiang81781b02018-07-26 20:21:47 +0800137 erofs_off_t current_block = (erofs_off_t)page->index;
138 int err;
139
Chen Gong9141b602018-09-18 22:27:28 +0800140 DBG_BUGON(!nblocks);
Gao Xiang81781b02018-07-26 20:21:47 +0800141
142 if (PageUptodate(page)) {
143 err = 0;
144 goto has_updated;
145 }
146
Gao Xiang81781b02018-07-26 20:21:47 +0800147 /* note that for readpage case, bio also equals to NULL */
Bhagyashri P. Digholed1ab82442018-11-05 19:49:05 +0000148 if (bio &&
Gao Xiang9f377622021-03-06 12:04:38 +0800149 (*last_block + 1 != current_block || !*eblks)) {
Gao Xiang81781b02018-07-26 20:21:47 +0800150submit_bio_retry:
Gao Xiang94e4e152019-09-04 10:09:04 +0800151 submit_bio(bio);
Gao Xiang81781b02018-07-26 20:21:47 +0800152 bio = NULL;
153 }
154
Bhagyashri P. Digholed1ab82442018-11-05 19:49:05 +0000155 if (!bio) {
Gao Xiang81781b02018-07-26 20:21:47 +0800156 struct erofs_map_blocks map = {
157 .m_la = blknr_to_addr(current_block),
158 };
Gao Xiang55441952018-07-26 20:22:00 +0800159 erofs_blk_t blknr;
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200160 unsigned int blkoff;
Gao Xiang81781b02018-07-26 20:21:47 +0800161
162 err = erofs_map_blocks(inode, &map, EROFS_GET_BLOCKS_RAW);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800163 if (err)
Gao Xiang81781b02018-07-26 20:21:47 +0800164 goto err_out;
165
166 /* zero out the holed page */
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800167 if (!(map.m_flags & EROFS_MAP_MAPPED)) {
Gao Xiang81781b02018-07-26 20:21:47 +0800168 zero_user_segment(page, 0, PAGE_SIZE);
169 SetPageUptodate(page);
170
171 /* imply err = 0, see erofs_map_blocks */
172 goto has_updated;
173 }
174
175 /* for RAW access mode, m_plen must be equal to m_llen */
Chen Gong9141b602018-09-18 22:27:28 +0800176 DBG_BUGON(map.m_plen != map.m_llen);
Gao Xiang81781b02018-07-26 20:21:47 +0800177
Gao Xiang55441952018-07-26 20:22:00 +0800178 blknr = erofs_blknr(map.m_pa);
179 blkoff = erofs_blkoff(map.m_pa);
180
Gao Xiang81781b02018-07-26 20:21:47 +0800181 /* deal with inline page */
182 if (map.m_flags & EROFS_MAP_META) {
183 void *vsrc, *vto;
184 struct page *ipage;
185
Chen Gong9141b602018-09-18 22:27:28 +0800186 DBG_BUGON(map.m_plen > PAGE_SIZE);
Gao Xiang81781b02018-07-26 20:21:47 +0800187
Gao Xiange655b5b2019-09-04 10:09:03 +0800188 ipage = erofs_get_meta_page(inode->i_sb, blknr);
Gao Xiang81781b02018-07-26 20:21:47 +0800189
190 if (IS_ERR(ipage)) {
191 err = PTR_ERR(ipage);
192 goto err_out;
193 }
194
195 vsrc = kmap_atomic(ipage);
196 vto = kmap_atomic(page);
Gao Xiang55441952018-07-26 20:22:00 +0800197 memcpy(vto, vsrc + blkoff, map.m_plen);
Gao Xiang81781b02018-07-26 20:21:47 +0800198 memset(vto + map.m_plen, 0, PAGE_SIZE - map.m_plen);
199 kunmap_atomic(vto);
200 kunmap_atomic(vsrc);
201 flush_dcache_page(page);
202
203 SetPageUptodate(page);
204 /* TODO: could we unlock the page earlier? */
205 unlock_page(ipage);
206 put_page(ipage);
207
208 /* imply err = 0, see erofs_map_blocks */
209 goto has_updated;
210 }
211
212 /* pa must be block-aligned for raw reading */
Chen Gong9141b602018-09-18 22:27:28 +0800213 DBG_BUGON(erofs_blkoff(map.m_pa));
Gao Xiang81781b02018-07-26 20:21:47 +0800214
215 /* max # of continuous pages */
216 if (nblocks > DIV_ROUND_UP(map.m_plen, PAGE_SIZE))
217 nblocks = DIV_ROUND_UP(map.m_plen, PAGE_SIZE);
Gao Xiang81781b02018-07-26 20:21:47 +0800218
Gao Xiang9f377622021-03-06 12:04:38 +0800219 *eblks = bio_max_segs(nblocks);
220 bio = bio_alloc(GFP_NOIO, *eblks);
Gao Xiang618f40e2019-09-04 10:09:12 +0800221
222 bio->bi_end_io = erofs_readendio;
223 bio_set_dev(bio, sb->s_bdev);
224 bio->bi_iter.bi_sector = (sector_t)blknr <<
225 LOG_SECTORS_PER_BLOCK;
Gao Xiang6ea5aad2020-09-19 15:27:30 +0800226 bio->bi_opf = REQ_OP_READ | (ra ? REQ_RAHEAD : 0);
Gao Xiang81781b02018-07-26 20:21:47 +0800227 }
228
229 err = bio_add_page(bio, page, PAGE_SIZE, 0);
230 /* out of the extent or bio is full */
231 if (err < PAGE_SIZE)
232 goto submit_bio_retry;
Gao Xiang9f377622021-03-06 12:04:38 +0800233 --*eblks;
Gao Xiang81781b02018-07-26 20:21:47 +0800234 *last_block = current_block;
Gao Xiang81781b02018-07-26 20:21:47 +0800235 return bio;
236
237err_out:
238 /* for sync reading, set page error immediately */
239 if (!ra) {
240 SetPageError(page);
241 ClearPageUptodate(page);
242 }
243has_updated:
244 unlock_page(page);
245
246 /* if updated manually, continuous pages has a gap */
Bhagyashri P. Digholed1ab82442018-11-05 19:49:05 +0000247 if (bio)
Gao Xiang94e4e152019-09-04 10:09:04 +0800248 submit_bio(bio);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800249 return err ? ERR_PTR(err) : NULL;
Gao Xiang81781b02018-07-26 20:21:47 +0800250}
251
252/*
253 * since we dont have write or truncate flows, so no inode
254 * locking needs to be held at the moment.
255 */
256static int erofs_raw_access_readpage(struct file *file, struct page *page)
257{
Kees Cook3f649ab2020-06-03 13:09:38 -0700258 erofs_off_t last_block;
Gao Xiang9f377622021-03-06 12:04:38 +0800259 unsigned int eblks;
Gao Xiang81781b02018-07-26 20:21:47 +0800260 struct bio *bio;
261
Chao Yu13f06f42018-07-26 20:21:55 +0800262 trace_erofs_readpage(page, true);
263
Gao Xiang81781b02018-07-26 20:21:47 +0800264 bio = erofs_read_raw_page(NULL, page->mapping,
Gao Xiang9f377622021-03-06 12:04:38 +0800265 page, &last_block, 1, &eblks, false);
Gao Xiang81781b02018-07-26 20:21:47 +0800266
267 if (IS_ERR(bio))
268 return PTR_ERR(bio);
269
Gao Xiang9f377622021-03-06 12:04:38 +0800270 if (bio)
271 submit_bio(bio);
Gao Xiang81781b02018-07-26 20:21:47 +0800272 return 0;
273}
274
Matthew Wilcox (Oracle)0c07a9f2020-06-01 21:47:09 -0700275static void erofs_raw_access_readahead(struct readahead_control *rac)
Gao Xiang81781b02018-07-26 20:21:47 +0800276{
Kees Cook3f649ab2020-06-03 13:09:38 -0700277 erofs_off_t last_block;
Gao Xiang9f377622021-03-06 12:04:38 +0800278 unsigned int eblks;
Gao Xiang81781b02018-07-26 20:21:47 +0800279 struct bio *bio = NULL;
Matthew Wilcox (Oracle)0c07a9f2020-06-01 21:47:09 -0700280 struct page *page;
Chao Yu13f06f42018-07-26 20:21:55 +0800281
Matthew Wilcox (Oracle)0c07a9f2020-06-01 21:47:09 -0700282 trace_erofs_readpages(rac->mapping->host, readahead_index(rac),
283 readahead_count(rac), true);
Gao Xiang81781b02018-07-26 20:21:47 +0800284
Matthew Wilcox (Oracle)0c07a9f2020-06-01 21:47:09 -0700285 while ((page = readahead_page(rac))) {
Gao Xiang81781b02018-07-26 20:21:47 +0800286 prefetchw(&page->flags);
Gao Xiang81781b02018-07-26 20:21:47 +0800287
Matthew Wilcox (Oracle)0c07a9f2020-06-01 21:47:09 -0700288 bio = erofs_read_raw_page(bio, rac->mapping, page, &last_block,
Gao Xiang9f377622021-03-06 12:04:38 +0800289 readahead_count(rac), &eblks, true);
Gao Xiang81781b02018-07-26 20:21:47 +0800290
Matthew Wilcox (Oracle)0c07a9f2020-06-01 21:47:09 -0700291 /* all the page errors are ignored when readahead */
292 if (IS_ERR(bio)) {
293 pr_err("%s, readahead error at page %lu of nid %llu\n",
294 __func__, page->index,
295 EROFS_I(rac->mapping->host)->nid);
Gao Xiang81781b02018-07-26 20:21:47 +0800296
Matthew Wilcox (Oracle)0c07a9f2020-06-01 21:47:09 -0700297 bio = NULL;
Gao Xiang81781b02018-07-26 20:21:47 +0800298 }
299
Gao Xiang81781b02018-07-26 20:21:47 +0800300 put_page(page);
301 }
Gao Xiang81781b02018-07-26 20:21:47 +0800302
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800303 if (bio)
Gao Xiang94e4e152019-09-04 10:09:04 +0800304 submit_bio(bio);
Gao Xiang81781b02018-07-26 20:21:47 +0800305}
306
Chao Yu9da681e2019-07-16 17:32:56 +0800307static sector_t erofs_bmap(struct address_space *mapping, sector_t block)
308{
309 struct inode *inode = mapping->host;
Huang Jianand8b3df82020-12-09 19:57:40 +0800310 struct erofs_map_blocks map = {
311 .m_la = blknr_to_addr(block),
312 };
Chao Yu9da681e2019-07-16 17:32:56 +0800313
Gao Xianga5876e22019-09-04 10:08:56 +0800314 if (EROFS_I(inode)->datalayout == EROFS_INODE_FLAT_INLINE) {
Chao Yu9da681e2019-07-16 17:32:56 +0800315 erofs_blk_t blks = i_size_read(inode) >> LOG_BLOCK_SIZE;
316
317 if (block >> LOG_SECTORS_PER_BLOCK >= blks)
318 return 0;
319 }
320
Huang Jianand8b3df82020-12-09 19:57:40 +0800321 if (!erofs_map_blocks(inode, &map, EROFS_GET_BLOCKS_RAW))
322 return erofs_blknr(map.m_pa);
323
324 return 0;
Chao Yu9da681e2019-07-16 17:32:56 +0800325}
326
Gao Xiang81781b02018-07-26 20:21:47 +0800327/* for uncompressed (aligned) files and raw access for other files */
328const struct address_space_operations erofs_raw_access_aops = {
329 .readpage = erofs_raw_access_readpage,
Matthew Wilcox (Oracle)0c07a9f2020-06-01 21:47:09 -0700330 .readahead = erofs_raw_access_readahead,
Chao Yu9da681e2019-07-16 17:32:56 +0800331 .bmap = erofs_bmap,
Gao Xiang81781b02018-07-26 20:21:47 +0800332};
333