1 // SPDX-License-Identifier: GPL-2.0
3 * linux/drivers/staging/erofs/data.c
5 * Copyright (C) 2017-2018 HUAWEI, Inc.
6 * http://www.huawei.com/
7 * Created by Gao Xiang <gaoxiang25@huawei.com>
9 * This file is subject to the terms and conditions of the GNU General Public
10 * License. See the file COPYING in the main directory of the Linux
11 * distribution for more details.
14 #include <linux/prefetch.h>
16 #include <trace/events/erofs.h>
18 static inline void read_endio(struct bio *bio)
22 const blk_status_t err = bio->bi_status;
24 bio_for_each_segment_all(bvec, bio, i) {
25 struct page *page = bvec->bv_page;
27 /* page is already locked */
28 DBG_BUGON(PageUptodate(page));
33 SetPageUptodate(page);
36 /* page could be reclaimed now */
41 /* prio -- true is used for dir */
42 struct page *__erofs_get_meta_page(struct super_block *sb,
43 erofs_blk_t blkaddr, bool prio, bool nofail)
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;
55 page = find_or_create_page(mapping, blkaddr, gfp);
56 if (unlikely(!page)) {
58 return ERR_PTR(-ENOMEM);
60 DBG_BUGON(!PageLocked(page));
62 if (!PageUptodate(page)) {
65 bio = erofs_grab_bio(sb, blkaddr, 1, read_endio, nofail);
72 err = bio_add_page(bio, page, PAGE_SIZE, 0);
73 if (unlikely(err != PAGE_SIZE)) {
78 __submit_bio(bio, REQ_OP_READ,
79 REQ_META | (prio ? REQ_PRIO : 0));
83 /* this page has been truncated by others */
84 if (unlikely(page->mapping != mapping)) {
91 /* more likely a read error */
92 if (unlikely(!PageUptodate(page))) {
109 static int erofs_map_blocks_flatmode(struct inode *inode,
110 struct erofs_map_blocks *map,
114 erofs_blk_t nblocks, lastblk;
115 u64 offset = map->m_la;
116 struct erofs_vnode *vi = EROFS_V(inode);
118 trace_erofs_map_blocks_flatmode_enter(inode, map, flags);
120 nblocks = DIV_ROUND_UP(inode->i_size, PAGE_SIZE);
121 lastblk = nblocks - is_inode_layout_inline(inode);
123 if (unlikely(offset >= inode->i_size)) {
124 /* leave out-of-bound access unmapped */
130 /* there is no hole in flatmode */
131 map->m_flags = EROFS_MAP_MAPPED;
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;
136 } else if (is_inode_layout_inline(inode)) {
137 /* 2 - inode inline B: inode, [xattrs], inline last blk... */
138 struct erofs_sb_info *sbi = EROFS_SB(inode->i_sb);
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;
144 /* inline data should locate in one meta block */
145 if (erofs_blkoff(map->m_pa) + map->m_plen > PAGE_SIZE) {
151 map->m_flags |= EROFS_MAP_META;
153 errln("internal error @ nid: %llu (size %llu), m_la 0x%llx",
154 vi->nid, inode->i_size, map->m_la);
161 map->m_llen = map->m_plen;
164 trace_erofs_map_blocks_flatmode_exit(inode, map, flags, 0);
168 #ifdef CONFIG_EROFS_FS_ZIP
169 extern int z_erofs_map_blocks_iter(struct inode *,
170 struct erofs_map_blocks *,
171 struct page **, int);
174 int erofs_map_blocks_iter(struct inode *inode,
175 struct erofs_map_blocks *map,
176 struct page **mpage_ret, int flags)
178 /* by default, reading raw data never use erofs_map_blocks_iter */
179 if (unlikely(!is_inode_layout_compression(inode))) {
181 put_page(*mpage_ret);
184 return erofs_map_blocks(inode, map, flags);
187 #ifdef CONFIG_EROFS_FS_ZIP
188 return z_erofs_map_blocks_iter(inode, map, mpage_ret, flags);
190 /* data compression is not available */
195 int erofs_map_blocks(struct inode *inode,
196 struct erofs_map_blocks *map, int flags)
198 if (unlikely(is_inode_layout_compression(inode))) {
199 struct page *mpage = NULL;
202 err = erofs_map_blocks_iter(inode, map, &mpage, flags);
207 return erofs_map_blocks_flatmode(inode, map, flags);
210 static inline struct bio *erofs_read_raw_page(struct bio *bio,
211 struct address_space *mapping,
213 erofs_off_t *last_block,
214 unsigned int nblocks,
217 struct inode *inode = mapping->host;
218 erofs_off_t current_block = (erofs_off_t)page->index;
223 if (PageUptodate(page)) {
228 if (cleancache_get_page(page) == 0) {
230 SetPageUptodate(page);
234 /* note that for readpage case, bio also equals to NULL */
237 *last_block + 1 != current_block) {
239 __submit_bio(bio, REQ_OP_READ, 0);
244 struct erofs_map_blocks map = {
245 .m_la = blknr_to_addr(current_block),
250 err = erofs_map_blocks(inode, &map, EROFS_GET_BLOCKS_RAW);
254 /* zero out the holed page */
255 if (unlikely(!(map.m_flags & EROFS_MAP_MAPPED))) {
256 zero_user_segment(page, 0, PAGE_SIZE);
257 SetPageUptodate(page);
259 /* imply err = 0, see erofs_map_blocks */
263 /* for RAW access mode, m_plen must be equal to m_llen */
264 DBG_BUGON(map.m_plen != map.m_llen);
266 blknr = erofs_blknr(map.m_pa);
267 blkoff = erofs_blkoff(map.m_pa);
269 /* deal with inline page */
270 if (map.m_flags & EROFS_MAP_META) {
274 DBG_BUGON(map.m_plen > PAGE_SIZE);
276 ipage = erofs_get_meta_page(inode->i_sb, blknr, 0);
279 err = PTR_ERR(ipage);
283 vsrc = kmap_atomic(ipage);
284 vto = kmap_atomic(page);
285 memcpy(vto, vsrc + blkoff, map.m_plen);
286 memset(vto + map.m_plen, 0, PAGE_SIZE - map.m_plen);
289 flush_dcache_page(page);
291 SetPageUptodate(page);
292 /* TODO: could we unlock the page earlier? */
296 /* imply err = 0, see erofs_map_blocks */
300 /* pa must be block-aligned for raw reading */
301 DBG_BUGON(erofs_blkoff(map.m_pa));
303 /* max # of continuous pages */
304 if (nblocks > DIV_ROUND_UP(map.m_plen, PAGE_SIZE))
305 nblocks = DIV_ROUND_UP(map.m_plen, PAGE_SIZE);
306 if (nblocks > BIO_MAX_PAGES)
307 nblocks = BIO_MAX_PAGES;
309 bio = erofs_grab_bio(inode->i_sb,
310 blknr, nblocks, read_endio, false);
319 err = bio_add_page(bio, page, PAGE_SIZE, 0);
320 /* out of the extent or bio is full */
322 goto submit_bio_retry;
324 *last_block = current_block;
326 /* shift in advance in case of it followed by too many gaps */
327 if (unlikely(bio->bi_vcnt >= bio->bi_max_vecs)) {
328 /* err should reassign to 0 after submitting */
336 /* for sync reading, set page error immediately */
339 ClearPageUptodate(page);
344 /* if updated manually, continuous pages has a gap */
347 __submit_bio(bio, REQ_OP_READ, 0);
349 return unlikely(err) ? ERR_PTR(err) : NULL;
353 * since we dont have write or truncate flows, so no inode
354 * locking needs to be held at the moment.
356 static int erofs_raw_access_readpage(struct file *file, struct page *page)
358 erofs_off_t last_block;
361 trace_erofs_readpage(page, true);
363 bio = erofs_read_raw_page(NULL, page->mapping,
364 page, &last_block, 1, false);
369 DBG_BUGON(bio); /* since we have only one bio -- must be NULL */
373 static int erofs_raw_access_readpages(struct file *filp,
374 struct address_space *mapping,
375 struct list_head *pages,
376 unsigned int nr_pages)
378 erofs_off_t last_block;
379 struct bio *bio = NULL;
380 gfp_t gfp = readahead_gfp_mask(mapping);
381 struct page *page = list_last_entry(pages, struct page, lru);
383 trace_erofs_readpages(mapping->host, page, nr_pages, true);
385 for (; nr_pages; --nr_pages) {
386 page = list_entry(pages->prev, struct page, lru);
388 prefetchw(&page->flags);
389 list_del(&page->lru);
391 if (!add_to_page_cache_lru(page, mapping, page->index, gfp)) {
392 bio = erofs_read_raw_page(bio, mapping, page,
393 &last_block, nr_pages, true);
395 /* all the page errors are ignored when readahead */
397 pr_err("%s, readahead error at page %lu of nid %llu\n",
398 __func__, page->index,
399 EROFS_V(mapping->host)->nid);
405 /* pages could still be locked */
408 DBG_BUGON(!list_empty(pages));
410 /* the rare case (end in gaps) */
412 __submit_bio(bio, REQ_OP_READ, 0);
416 /* for uncompressed (aligned) files and raw access for other files */
417 const struct address_space_operations erofs_raw_access_aops = {
418 .readpage = erofs_raw_access_readpage,
419 .readpages = erofs_raw_access_readpages,