GNU Linux-libre 4.19.286-gnu1
[releases.git] / drivers / staging / erofs / data.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * linux/drivers/staging/erofs/data.c
4  *
5  * Copyright (C) 2017-2018 HUAWEI, Inc.
6  *             http://www.huawei.com/
7  * Created by Gao Xiang <gaoxiang25@huawei.com>
8  *
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.
12  */
13 #include "internal.h"
14 #include <linux/prefetch.h>
15
16 #include <trace/events/erofs.h>
17
18 static inline void read_endio(struct bio *bio)
19 {
20         int i;
21         struct bio_vec *bvec;
22         const blk_status_t err = bio->bi_status;
23
24         bio_for_each_segment_all(bvec, bio, i) {
25                 struct page *page = bvec->bv_page;
26
27                 /* page is already locked */
28                 DBG_BUGON(PageUptodate(page));
29
30                 if (unlikely(err))
31                         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
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)
44 {
45         struct inode *bd_inode = sb->s_bdev->bd_inode;
46         struct address_space *mapping = bd_inode->i_mapping;
47         struct page *page;
48
49 repeat:
50         page = find_or_create_page(mapping, blkaddr,
51         /*
52          * Prefer looping in the allocator rather than here,
53          * at least that code knows what it's doing.
54          */
55                 mapping_gfp_constraint(mapping, ~__GFP_FS) | __GFP_NOFAIL);
56
57         BUG_ON(!page || !PageLocked(page));
58
59         if (!PageUptodate(page)) {
60                 struct bio *bio;
61                 int err;
62
63                 bio = prepare_bio(sb, blkaddr, 1, read_endio);
64                 err = bio_add_page(bio, page, PAGE_SIZE, 0);
65                 BUG_ON(err != PAGE_SIZE);
66
67                 __submit_bio(bio, REQ_OP_READ,
68                         REQ_META | (prio ? REQ_PRIO : 0));
69
70                 lock_page(page);
71
72                 /* the page has been truncated by others? */
73                 if (unlikely(page->mapping != mapping)) {
74                         unlock_page(page);
75                         put_page(page);
76                         goto repeat;
77                 }
78
79                 /* more likely a read error */
80                 if (unlikely(!PageUptodate(page))) {
81                         unlock_page(page);
82                         put_page(page);
83
84                         page = ERR_PTR(-EIO);
85                 }
86         }
87         return page;
88 }
89
90 static int erofs_map_blocks_flatmode(struct inode *inode,
91         struct erofs_map_blocks *map,
92         int flags)
93 {
94         int err = 0;
95         erofs_blk_t nblocks, lastblk;
96         u64 offset = map->m_la;
97         struct erofs_vnode *vi = EROFS_V(inode);
98
99         trace_erofs_map_blocks_flatmode_enter(inode, map, flags);
100
101         nblocks = DIV_ROUND_UP(inode->i_size, PAGE_SIZE);
102         lastblk = nblocks - is_inode_layout_inline(inode);
103
104         if (unlikely(offset >= inode->i_size)) {
105                 /* leave out-of-bound access unmapped */
106                 map->m_flags = 0;
107                 map->m_plen = 0;
108                 goto out;
109         }
110
111         /* there is no hole in flatmode */
112         map->m_flags = EROFS_MAP_MAPPED;
113
114         if (offset < blknr_to_addr(lastblk)) {
115                 map->m_pa = blknr_to_addr(vi->raw_blkaddr) + map->m_la;
116                 map->m_plen = blknr_to_addr(lastblk) - offset;
117         } else if (is_inode_layout_inline(inode)) {
118                 /* 2 - inode inline B: inode, [xattrs], inline last blk... */
119                 struct erofs_sb_info *sbi = EROFS_SB(inode->i_sb);
120
121                 map->m_pa = iloc(sbi, vi->nid) + vi->inode_isize +
122                         vi->xattr_isize + erofs_blkoff(map->m_la);
123                 map->m_plen = inode->i_size - offset;
124
125                 /* inline data should locate in one meta block */
126                 if (erofs_blkoff(map->m_pa) + map->m_plen > PAGE_SIZE) {
127                         DBG_BUGON(1);
128                         err = -EIO;
129                         goto err_out;
130                 }
131
132                 map->m_flags |= EROFS_MAP_META;
133         } else {
134                 errln("internal error @ nid: %llu (size %llu), m_la 0x%llx",
135                         vi->nid, inode->i_size, map->m_la);
136                 DBG_BUGON(1);
137                 err = -EIO;
138                 goto err_out;
139         }
140
141 out:
142         map->m_llen = map->m_plen;
143
144 err_out:
145         trace_erofs_map_blocks_flatmode_exit(inode, map, flags, 0);
146         return err;
147 }
148
149 #ifdef CONFIG_EROFS_FS_ZIP
150 extern int z_erofs_map_blocks_iter(struct inode *,
151         struct erofs_map_blocks *, struct page **, int);
152 #endif
153
154 int erofs_map_blocks_iter(struct inode *inode,
155         struct erofs_map_blocks *map,
156         struct page **mpage_ret, int flags)
157 {
158         /* by default, reading raw data never use erofs_map_blocks_iter */
159         if (unlikely(!is_inode_layout_compression(inode))) {
160                 if (*mpage_ret != NULL)
161                         put_page(*mpage_ret);
162                 *mpage_ret = NULL;
163
164                 return erofs_map_blocks(inode, map, flags);
165         }
166
167 #ifdef CONFIG_EROFS_FS_ZIP
168         return z_erofs_map_blocks_iter(inode, map, mpage_ret, flags);
169 #else
170         /* data compression is not available */
171         return -ENOTSUPP;
172 #endif
173 }
174
175 int erofs_map_blocks(struct inode *inode,
176         struct erofs_map_blocks *map, int flags)
177 {
178         if (unlikely(is_inode_layout_compression(inode))) {
179                 struct page *mpage = NULL;
180                 int err;
181
182                 err = erofs_map_blocks_iter(inode, map, &mpage, flags);
183                 if (mpage != NULL)
184                         put_page(mpage);
185                 return err;
186         }
187         return erofs_map_blocks_flatmode(inode, map, flags);
188 }
189
190 static inline struct bio *erofs_read_raw_page(
191         struct bio *bio,
192         struct address_space *mapping,
193         struct page *page,
194         erofs_off_t *last_block,
195         unsigned nblocks,
196         bool ra)
197 {
198         struct inode *inode = mapping->host;
199         erofs_off_t current_block = (erofs_off_t)page->index;
200         int err;
201
202         DBG_BUGON(!nblocks);
203
204         if (PageUptodate(page)) {
205                 err = 0;
206                 goto has_updated;
207         }
208
209         if (cleancache_get_page(page) == 0) {
210                 err = 0;
211                 SetPageUptodate(page);
212                 goto has_updated;
213         }
214
215         /* note that for readpage case, bio also equals to NULL */
216         if (bio != NULL &&
217                 /* not continuous */
218                 *last_block + 1 != current_block) {
219 submit_bio_retry:
220                 __submit_bio(bio, REQ_OP_READ, 0);
221                 bio = NULL;
222         }
223
224         if (bio == NULL) {
225                 struct erofs_map_blocks map = {
226                         .m_la = blknr_to_addr(current_block),
227                 };
228                 erofs_blk_t blknr;
229                 unsigned blkoff;
230
231                 err = erofs_map_blocks(inode, &map, EROFS_GET_BLOCKS_RAW);
232                 if (unlikely(err))
233                         goto err_out;
234
235                 /* zero out the holed page */
236                 if (unlikely(!(map.m_flags & EROFS_MAP_MAPPED))) {
237                         zero_user_segment(page, 0, PAGE_SIZE);
238                         SetPageUptodate(page);
239
240                         /* imply err = 0, see erofs_map_blocks */
241                         goto has_updated;
242                 }
243
244                 /* for RAW access mode, m_plen must be equal to m_llen */
245                 DBG_BUGON(map.m_plen != map.m_llen);
246
247                 blknr = erofs_blknr(map.m_pa);
248                 blkoff = erofs_blkoff(map.m_pa);
249
250                 /* deal with inline page */
251                 if (map.m_flags & EROFS_MAP_META) {
252                         void *vsrc, *vto;
253                         struct page *ipage;
254
255                         DBG_BUGON(map.m_plen > PAGE_SIZE);
256
257                         ipage = erofs_get_meta_page(inode->i_sb, blknr, 0);
258
259                         if (IS_ERR(ipage)) {
260                                 err = PTR_ERR(ipage);
261                                 goto err_out;
262                         }
263
264                         vsrc = kmap_atomic(ipage);
265                         vto = kmap_atomic(page);
266                         memcpy(vto, vsrc + blkoff, map.m_plen);
267                         memset(vto + map.m_plen, 0, PAGE_SIZE - map.m_plen);
268                         kunmap_atomic(vto);
269                         kunmap_atomic(vsrc);
270                         flush_dcache_page(page);
271
272                         SetPageUptodate(page);
273                         /* TODO: could we unlock the page earlier? */
274                         unlock_page(ipage);
275                         put_page(ipage);
276
277                         /* imply err = 0, see erofs_map_blocks */
278                         goto has_updated;
279                 }
280
281                 /* pa must be block-aligned for raw reading */
282                 DBG_BUGON(erofs_blkoff(map.m_pa));
283
284                 /* max # of continuous pages */
285                 if (nblocks > DIV_ROUND_UP(map.m_plen, PAGE_SIZE))
286                         nblocks = DIV_ROUND_UP(map.m_plen, PAGE_SIZE);
287                 if (nblocks > BIO_MAX_PAGES)
288                         nblocks = BIO_MAX_PAGES;
289
290                 bio = prepare_bio(inode->i_sb, blknr, nblocks, read_endio);
291         }
292
293         err = bio_add_page(bio, page, PAGE_SIZE, 0);
294         /* out of the extent or bio is full */
295         if (err < PAGE_SIZE)
296                 goto submit_bio_retry;
297
298         *last_block = current_block;
299
300         /* shift in advance in case of it followed by too many gaps */
301         if (unlikely(bio->bi_vcnt >= bio->bi_max_vecs)) {
302                 /* err should reassign to 0 after submitting */
303                 err = 0;
304                 goto submit_bio_out;
305         }
306
307         return bio;
308
309 err_out:
310         /* for sync reading, set page error immediately */
311         if (!ra) {
312                 SetPageError(page);
313                 ClearPageUptodate(page);
314         }
315 has_updated:
316         unlock_page(page);
317
318         /* if updated manually, continuous pages has a gap */
319         if (bio != NULL)
320 submit_bio_out:
321                 __submit_bio(bio, REQ_OP_READ, 0);
322
323         return unlikely(err) ? ERR_PTR(err) : NULL;
324 }
325
326 /*
327  * since we dont have write or truncate flows, so no inode
328  * locking needs to be held at the moment.
329  */
330 static int erofs_raw_access_readpage(struct file *file, struct page *page)
331 {
332         erofs_off_t last_block;
333         struct bio *bio;
334
335         trace_erofs_readpage(page, true);
336
337         bio = erofs_read_raw_page(NULL, page->mapping,
338                 page, &last_block, 1, false);
339
340         if (IS_ERR(bio))
341                 return PTR_ERR(bio);
342
343         DBG_BUGON(bio); /* since we have only one bio -- must be NULL */
344         return 0;
345 }
346
347 static int erofs_raw_access_readpages(struct file *filp,
348         struct address_space *mapping,
349         struct list_head *pages, unsigned nr_pages)
350 {
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);
355
356         trace_erofs_readpages(mapping->host, page, nr_pages, true);
357
358         for (; nr_pages; --nr_pages) {
359                 page = list_entry(pages->prev, struct page, lru);
360
361                 prefetchw(&page->flags);
362                 list_del(&page->lru);
363
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);
367
368                         /* all the page errors are ignored when readahead */
369                         if (IS_ERR(bio)) {
370                                 pr_err("%s, readahead error at page %lu of nid %llu\n",
371                                         __func__, page->index,
372                                         EROFS_V(mapping->host)->nid);
373
374                                 bio = NULL;
375                         }
376                 }
377
378                 /* pages could still be locked */
379                 put_page(page);
380         }
381         DBG_BUGON(!list_empty(pages));
382
383         /* the rare case (end in gaps) */
384         if (unlikely(bio != NULL))
385                 __submit_bio(bio, REQ_OP_READ, 0);
386         return 0;
387 }
388
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,
393 };
394