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)
20 struct super_block *const sb = bio->bi_private;
22 blk_status_t err = bio->bi_status;
23 struct bvec_iter_all iter_all;
25 if (time_to_inject(EROFS_SB(sb), FAULT_READ_IO)) {
26 erofs_show_injection_info(FAULT_READ_IO);
30 bio_for_each_segment_all(bvec, bio, iter_all) {
31 struct page *page = bvec->bv_page;
33 /* page is already locked */
34 DBG_BUGON(PageUptodate(page));
39 SetPageUptodate(page);
42 /* page could be reclaimed now */
47 /* prio -- true is used for dir */
48 struct page *__erofs_get_meta_page(struct super_block *sb,
49 erofs_blk_t blkaddr, bool prio, bool nofail)
51 struct inode *const bd_inode = sb->s_bdev->bd_inode;
52 struct address_space *const mapping = bd_inode->i_mapping;
53 /* prefer retrying in the allocator to blindly looping below */
54 const gfp_t gfp = mapping_gfp_constraint(mapping, ~__GFP_FS) |
55 (nofail ? __GFP_NOFAIL : 0);
56 unsigned int io_retries = nofail ? EROFS_IO_MAX_RETRIES_NOFAIL : 0;
61 page = find_or_create_page(mapping, blkaddr, gfp);
62 if (unlikely(!page)) {
64 return ERR_PTR(-ENOMEM);
66 DBG_BUGON(!PageLocked(page));
68 if (!PageUptodate(page)) {
71 bio = erofs_grab_bio(sb, blkaddr, 1, sb, read_endio, nofail);
78 err = bio_add_page(bio, page, PAGE_SIZE, 0);
79 if (unlikely(err != PAGE_SIZE)) {
84 __submit_bio(bio, REQ_OP_READ,
85 REQ_META | (prio ? REQ_PRIO : 0));
89 /* this page has been truncated by others */
90 if (unlikely(page->mapping != mapping)) {
97 /* more likely a read error */
98 if (unlikely(!PageUptodate(page))) {
115 static int erofs_map_blocks_flatmode(struct inode *inode,
116 struct erofs_map_blocks *map,
120 erofs_blk_t nblocks, lastblk;
121 u64 offset = map->m_la;
122 struct erofs_vnode *vi = EROFS_V(inode);
124 trace_erofs_map_blocks_flatmode_enter(inode, map, flags);
126 nblocks = DIV_ROUND_UP(inode->i_size, PAGE_SIZE);
127 lastblk = nblocks - is_inode_flat_inline(inode);
129 if (unlikely(offset >= inode->i_size)) {
130 /* leave out-of-bound access unmapped */
136 /* there is no hole in flatmode */
137 map->m_flags = EROFS_MAP_MAPPED;
139 if (offset < blknr_to_addr(lastblk)) {
140 map->m_pa = blknr_to_addr(vi->raw_blkaddr) + map->m_la;
141 map->m_plen = blknr_to_addr(lastblk) - offset;
142 } else if (is_inode_flat_inline(inode)) {
143 /* 2 - inode inline B: inode, [xattrs], inline last blk... */
144 struct erofs_sb_info *sbi = EROFS_SB(inode->i_sb);
146 map->m_pa = iloc(sbi, vi->nid) + vi->inode_isize +
147 vi->xattr_isize + erofs_blkoff(map->m_la);
148 map->m_plen = inode->i_size - offset;
150 /* inline data should locate in one meta block */
151 if (erofs_blkoff(map->m_pa) + map->m_plen > PAGE_SIZE) {
157 map->m_flags |= EROFS_MAP_META;
159 errln("internal error @ nid: %llu (size %llu), m_la 0x%llx",
160 vi->nid, inode->i_size, map->m_la);
167 map->m_llen = map->m_plen;
170 trace_erofs_map_blocks_flatmode_exit(inode, map, flags, 0);
174 int erofs_map_blocks(struct inode *inode,
175 struct erofs_map_blocks *map, int flags)
177 if (unlikely(is_inode_layout_compression(inode))) {
178 int err = z_erofs_map_blocks_iter(inode, map, flags);
181 put_page(map->mpage);
186 return erofs_map_blocks_flatmode(inode, map, flags);
189 static inline struct bio *erofs_read_raw_page(struct bio *bio,
190 struct address_space *mapping,
192 erofs_off_t *last_block,
193 unsigned int nblocks,
196 struct inode *const inode = mapping->host;
197 struct super_block *const sb = inode->i_sb;
198 erofs_off_t current_block = (erofs_off_t)page->index;
203 if (PageUptodate(page)) {
208 if (cleancache_get_page(page) == 0) {
210 SetPageUptodate(page);
214 /* note that for readpage case, bio also equals to NULL */
217 *last_block + 1 != current_block) {
219 __submit_bio(bio, REQ_OP_READ, 0);
224 struct erofs_map_blocks map = {
225 .m_la = blknr_to_addr(current_block),
230 err = erofs_map_blocks(inode, &map, EROFS_GET_BLOCKS_RAW);
234 /* zero out the holed page */
235 if (unlikely(!(map.m_flags & EROFS_MAP_MAPPED))) {
236 zero_user_segment(page, 0, PAGE_SIZE);
237 SetPageUptodate(page);
239 /* imply err = 0, see erofs_map_blocks */
243 /* for RAW access mode, m_plen must be equal to m_llen */
244 DBG_BUGON(map.m_plen != map.m_llen);
246 blknr = erofs_blknr(map.m_pa);
247 blkoff = erofs_blkoff(map.m_pa);
249 /* deal with inline page */
250 if (map.m_flags & EROFS_MAP_META) {
254 DBG_BUGON(map.m_plen > PAGE_SIZE);
256 ipage = erofs_get_meta_page(inode->i_sb, blknr, 0);
259 err = PTR_ERR(ipage);
263 vsrc = kmap_atomic(ipage);
264 vto = kmap_atomic(page);
265 memcpy(vto, vsrc + blkoff, map.m_plen);
266 memset(vto + map.m_plen, 0, PAGE_SIZE - map.m_plen);
269 flush_dcache_page(page);
271 SetPageUptodate(page);
272 /* TODO: could we unlock the page earlier? */
276 /* imply err = 0, see erofs_map_blocks */
280 /* pa must be block-aligned for raw reading */
281 DBG_BUGON(erofs_blkoff(map.m_pa));
283 /* max # of continuous pages */
284 if (nblocks > DIV_ROUND_UP(map.m_plen, PAGE_SIZE))
285 nblocks = DIV_ROUND_UP(map.m_plen, PAGE_SIZE);
286 if (nblocks > BIO_MAX_PAGES)
287 nblocks = BIO_MAX_PAGES;
289 bio = erofs_grab_bio(sb, blknr, nblocks, sb,
298 err = bio_add_page(bio, page, PAGE_SIZE, 0);
299 /* out of the extent or bio is full */
301 goto submit_bio_retry;
303 *last_block = current_block;
305 /* shift in advance in case of it followed by too many gaps */
306 if (bio->bi_iter.bi_size >= bio->bi_max_vecs * PAGE_SIZE) {
307 /* err should reassign to 0 after submitting */
315 /* for sync reading, set page error immediately */
318 ClearPageUptodate(page);
323 /* if updated manually, continuous pages has a gap */
326 __submit_bio(bio, REQ_OP_READ, 0);
328 return unlikely(err) ? ERR_PTR(err) : NULL;
332 * since we dont have write or truncate flows, so no inode
333 * locking needs to be held at the moment.
335 static int erofs_raw_access_readpage(struct file *file, struct page *page)
337 erofs_off_t last_block;
340 trace_erofs_readpage(page, true);
342 bio = erofs_read_raw_page(NULL, page->mapping,
343 page, &last_block, 1, false);
348 DBG_BUGON(bio); /* since we have only one bio -- must be NULL */
352 static int erofs_raw_access_readpages(struct file *filp,
353 struct address_space *mapping,
354 struct list_head *pages,
355 unsigned int nr_pages)
357 erofs_off_t last_block;
358 struct bio *bio = NULL;
359 gfp_t gfp = readahead_gfp_mask(mapping);
360 struct page *page = list_last_entry(pages, struct page, lru);
362 trace_erofs_readpages(mapping->host, page, nr_pages, true);
364 for (; nr_pages; --nr_pages) {
365 page = list_entry(pages->prev, struct page, lru);
367 prefetchw(&page->flags);
368 list_del(&page->lru);
370 if (!add_to_page_cache_lru(page, mapping, page->index, gfp)) {
371 bio = erofs_read_raw_page(bio, mapping, page,
372 &last_block, nr_pages, true);
374 /* all the page errors are ignored when readahead */
376 pr_err("%s, readahead error at page %lu of nid %llu\n",
377 __func__, page->index,
378 EROFS_V(mapping->host)->nid);
384 /* pages could still be locked */
387 DBG_BUGON(!list_empty(pages));
389 /* the rare case (end in gaps) */
391 __submit_bio(bio, REQ_OP_READ, 0);
395 /* for uncompressed (aligned) files and raw access for other files */
396 const struct address_space_operations erofs_raw_access_aops = {
397 .readpage = erofs_raw_access_readpage,
398 .readpages = erofs_raw_access_readpages,