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)
21 const blk_status_t err = bio->bi_status;
22 struct bvec_iter_all iter_all;
24 bio_for_each_segment_all(bvec, bio, iter_all) {
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 int erofs_map_blocks(struct inode *inode,
169 struct erofs_map_blocks *map, int flags)
171 if (unlikely(is_inode_layout_compression(inode))) {
172 int err = z_erofs_map_blocks_iter(inode, map, flags);
175 put_page(map->mpage);
180 return erofs_map_blocks_flatmode(inode, map, flags);
183 static inline struct bio *erofs_read_raw_page(struct bio *bio,
184 struct address_space *mapping,
186 erofs_off_t *last_block,
187 unsigned int nblocks,
190 struct inode *inode = mapping->host;
191 erofs_off_t current_block = (erofs_off_t)page->index;
196 if (PageUptodate(page)) {
201 if (cleancache_get_page(page) == 0) {
203 SetPageUptodate(page);
207 /* note that for readpage case, bio also equals to NULL */
210 *last_block + 1 != current_block) {
212 __submit_bio(bio, REQ_OP_READ, 0);
217 struct erofs_map_blocks map = {
218 .m_la = blknr_to_addr(current_block),
223 err = erofs_map_blocks(inode, &map, EROFS_GET_BLOCKS_RAW);
227 /* zero out the holed page */
228 if (unlikely(!(map.m_flags & EROFS_MAP_MAPPED))) {
229 zero_user_segment(page, 0, PAGE_SIZE);
230 SetPageUptodate(page);
232 /* imply err = 0, see erofs_map_blocks */
236 /* for RAW access mode, m_plen must be equal to m_llen */
237 DBG_BUGON(map.m_plen != map.m_llen);
239 blknr = erofs_blknr(map.m_pa);
240 blkoff = erofs_blkoff(map.m_pa);
242 /* deal with inline page */
243 if (map.m_flags & EROFS_MAP_META) {
247 DBG_BUGON(map.m_plen > PAGE_SIZE);
249 ipage = erofs_get_meta_page(inode->i_sb, blknr, 0);
252 err = PTR_ERR(ipage);
256 vsrc = kmap_atomic(ipage);
257 vto = kmap_atomic(page);
258 memcpy(vto, vsrc + blkoff, map.m_plen);
259 memset(vto + map.m_plen, 0, PAGE_SIZE - map.m_plen);
262 flush_dcache_page(page);
264 SetPageUptodate(page);
265 /* TODO: could we unlock the page earlier? */
269 /* imply err = 0, see erofs_map_blocks */
273 /* pa must be block-aligned for raw reading */
274 DBG_BUGON(erofs_blkoff(map.m_pa));
276 /* max # of continuous pages */
277 if (nblocks > DIV_ROUND_UP(map.m_plen, PAGE_SIZE))
278 nblocks = DIV_ROUND_UP(map.m_plen, PAGE_SIZE);
279 if (nblocks > BIO_MAX_PAGES)
280 nblocks = BIO_MAX_PAGES;
282 bio = erofs_grab_bio(inode->i_sb,
283 blknr, nblocks, read_endio, false);
292 err = bio_add_page(bio, page, PAGE_SIZE, 0);
293 /* out of the extent or bio is full */
295 goto submit_bio_retry;
297 *last_block = current_block;
299 /* shift in advance in case of it followed by too many gaps */
300 if (bio->bi_iter.bi_size >= bio->bi_max_vecs * PAGE_SIZE) {
301 /* err should reassign to 0 after submitting */
309 /* for sync reading, set page error immediately */
312 ClearPageUptodate(page);
317 /* if updated manually, continuous pages has a gap */
320 __submit_bio(bio, REQ_OP_READ, 0);
322 return unlikely(err) ? ERR_PTR(err) : NULL;
326 * since we dont have write or truncate flows, so no inode
327 * locking needs to be held at the moment.
329 static int erofs_raw_access_readpage(struct file *file, struct page *page)
331 erofs_off_t last_block;
334 trace_erofs_readpage(page, true);
336 bio = erofs_read_raw_page(NULL, page->mapping,
337 page, &last_block, 1, false);
342 DBG_BUGON(bio); /* since we have only one bio -- must be NULL */
346 static int erofs_raw_access_readpages(struct file *filp,
347 struct address_space *mapping,
348 struct list_head *pages,
349 unsigned int nr_pages)
351 erofs_off_t last_block;
352 struct bio *bio = NULL;
353 gfp_t gfp = readahead_gfp_mask(mapping);
354 struct page *page = list_last_entry(pages, struct page, lru);
356 trace_erofs_readpages(mapping->host, page, nr_pages, true);
358 for (; nr_pages; --nr_pages) {
359 page = list_entry(pages->prev, struct page, lru);
361 prefetchw(&page->flags);
362 list_del(&page->lru);
364 if (!add_to_page_cache_lru(page, mapping, page->index, gfp)) {
365 bio = erofs_read_raw_page(bio, mapping, page,
366 &last_block, nr_pages, true);
368 /* all the page errors are ignored when readahead */
370 pr_err("%s, readahead error at page %lu of nid %llu\n",
371 __func__, page->index,
372 EROFS_V(mapping->host)->nid);
378 /* pages could still be locked */
381 DBG_BUGON(!list_empty(pages));
383 /* the rare case (end in gaps) */
385 __submit_bio(bio, REQ_OP_READ, 0);
389 /* for uncompressed (aligned) files and raw access for other files */
390 const struct address_space_operations erofs_raw_access_aops = {
391 .readpage = erofs_raw_access_readpage,
392 .readpages = erofs_raw_access_readpages,