Merge tag 'sound-5.1' of ssh://gitolite.kernel.org/pub/scm/linux/kernel/git/tiwai...
[linux-2.6-microblaze.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         struct bvec_iter_all iter_all;
24
25         bio_for_each_segment_all(bvec, bio, i, iter_all) {
26                 struct page *page = bvec->bv_page;
27
28                 /* page is already locked */
29                 DBG_BUGON(PageUptodate(page));
30
31                 if (unlikely(err))
32                         SetPageError(page);
33                 else
34                         SetPageUptodate(page);
35
36                 unlock_page(page);
37                 /* page could be reclaimed now */
38         }
39         bio_put(bio);
40 }
41
42 /* prio -- true is used for dir */
43 struct page *__erofs_get_meta_page(struct super_block *sb,
44                                    erofs_blk_t blkaddr, bool prio, bool nofail)
45 {
46         struct inode *const bd_inode = sb->s_bdev->bd_inode;
47         struct address_space *const mapping = bd_inode->i_mapping;
48         /* prefer retrying in the allocator to blindly looping below */
49         const gfp_t gfp = mapping_gfp_constraint(mapping, ~__GFP_FS) |
50                 (nofail ? __GFP_NOFAIL : 0);
51         unsigned int io_retries = nofail ? EROFS_IO_MAX_RETRIES_NOFAIL : 0;
52         struct page *page;
53         int err;
54
55 repeat:
56         page = find_or_create_page(mapping, blkaddr, gfp);
57         if (unlikely(!page)) {
58                 DBG_BUGON(nofail);
59                 return ERR_PTR(-ENOMEM);
60         }
61         DBG_BUGON(!PageLocked(page));
62
63         if (!PageUptodate(page)) {
64                 struct bio *bio;
65
66                 bio = erofs_grab_bio(sb, blkaddr, 1, read_endio, nofail);
67                 if (IS_ERR(bio)) {
68                         DBG_BUGON(nofail);
69                         err = PTR_ERR(bio);
70                         goto err_out;
71                 }
72
73                 err = bio_add_page(bio, page, PAGE_SIZE, 0);
74                 if (unlikely(err != PAGE_SIZE)) {
75                         err = -EFAULT;
76                         goto err_out;
77                 }
78
79                 __submit_bio(bio, REQ_OP_READ,
80                              REQ_META | (prio ? REQ_PRIO : 0));
81
82                 lock_page(page);
83
84                 /* this page has been truncated by others */
85                 if (unlikely(page->mapping != mapping)) {
86 unlock_repeat:
87                         unlock_page(page);
88                         put_page(page);
89                         goto repeat;
90                 }
91
92                 /* more likely a read error */
93                 if (unlikely(!PageUptodate(page))) {
94                         if (io_retries) {
95                                 --io_retries;
96                                 goto unlock_repeat;
97                         }
98                         err = -EIO;
99                         goto err_out;
100                 }
101         }
102         return page;
103
104 err_out:
105         unlock_page(page);
106         put_page(page);
107         return ERR_PTR(err);
108 }
109
110 static int erofs_map_blocks_flatmode(struct inode *inode,
111                                      struct erofs_map_blocks *map,
112                                      int flags)
113 {
114         int err = 0;
115         erofs_blk_t nblocks, lastblk;
116         u64 offset = map->m_la;
117         struct erofs_vnode *vi = EROFS_V(inode);
118
119         trace_erofs_map_blocks_flatmode_enter(inode, map, flags);
120
121         nblocks = DIV_ROUND_UP(inode->i_size, PAGE_SIZE);
122         lastblk = nblocks - is_inode_layout_inline(inode);
123
124         if (unlikely(offset >= inode->i_size)) {
125                 /* leave out-of-bound access unmapped */
126                 map->m_flags = 0;
127                 map->m_plen = 0;
128                 goto out;
129         }
130
131         /* there is no hole in flatmode */
132         map->m_flags = EROFS_MAP_MAPPED;
133
134         if (offset < blknr_to_addr(lastblk)) {
135                 map->m_pa = blknr_to_addr(vi->raw_blkaddr) + map->m_la;
136                 map->m_plen = blknr_to_addr(lastblk) - offset;
137         } else if (is_inode_layout_inline(inode)) {
138                 /* 2 - inode inline B: inode, [xattrs], inline last blk... */
139                 struct erofs_sb_info *sbi = EROFS_SB(inode->i_sb);
140
141                 map->m_pa = iloc(sbi, vi->nid) + vi->inode_isize +
142                         vi->xattr_isize + erofs_blkoff(map->m_la);
143                 map->m_plen = inode->i_size - offset;
144
145                 /* inline data should locate in one meta block */
146                 if (erofs_blkoff(map->m_pa) + map->m_plen > PAGE_SIZE) {
147                         DBG_BUGON(1);
148                         err = -EIO;
149                         goto err_out;
150                 }
151
152                 map->m_flags |= EROFS_MAP_META;
153         } else {
154                 errln("internal error @ nid: %llu (size %llu), m_la 0x%llx",
155                       vi->nid, inode->i_size, map->m_la);
156                 DBG_BUGON(1);
157                 err = -EIO;
158                 goto err_out;
159         }
160
161 out:
162         map->m_llen = map->m_plen;
163
164 err_out:
165         trace_erofs_map_blocks_flatmode_exit(inode, map, flags, 0);
166         return err;
167 }
168
169 int erofs_map_blocks(struct inode *inode,
170                      struct erofs_map_blocks *map, int flags)
171 {
172         if (unlikely(is_inode_layout_compression(inode))) {
173                 int err = z_erofs_map_blocks_iter(inode, map, flags);
174
175                 if (map->mpage) {
176                         put_page(map->mpage);
177                         map->mpage = NULL;
178                 }
179                 return err;
180         }
181         return erofs_map_blocks_flatmode(inode, map, flags);
182 }
183
184 static inline struct bio *erofs_read_raw_page(struct bio *bio,
185                                               struct address_space *mapping,
186                                               struct page *page,
187                                               erofs_off_t *last_block,
188                                               unsigned int nblocks,
189                                               bool ra)
190 {
191         struct inode *inode = mapping->host;
192         erofs_off_t current_block = (erofs_off_t)page->index;
193         int err;
194
195         DBG_BUGON(!nblocks);
196
197         if (PageUptodate(page)) {
198                 err = 0;
199                 goto has_updated;
200         }
201
202         if (cleancache_get_page(page) == 0) {
203                 err = 0;
204                 SetPageUptodate(page);
205                 goto has_updated;
206         }
207
208         /* note that for readpage case, bio also equals to NULL */
209         if (bio &&
210             /* not continuous */
211             *last_block + 1 != current_block) {
212 submit_bio_retry:
213                 __submit_bio(bio, REQ_OP_READ, 0);
214                 bio = NULL;
215         }
216
217         if (!bio) {
218                 struct erofs_map_blocks map = {
219                         .m_la = blknr_to_addr(current_block),
220                 };
221                 erofs_blk_t blknr;
222                 unsigned int blkoff;
223
224                 err = erofs_map_blocks(inode, &map, EROFS_GET_BLOCKS_RAW);
225                 if (unlikely(err))
226                         goto err_out;
227
228                 /* zero out the holed page */
229                 if (unlikely(!(map.m_flags & EROFS_MAP_MAPPED))) {
230                         zero_user_segment(page, 0, PAGE_SIZE);
231                         SetPageUptodate(page);
232
233                         /* imply err = 0, see erofs_map_blocks */
234                         goto has_updated;
235                 }
236
237                 /* for RAW access mode, m_plen must be equal to m_llen */
238                 DBG_BUGON(map.m_plen != map.m_llen);
239
240                 blknr = erofs_blknr(map.m_pa);
241                 blkoff = erofs_blkoff(map.m_pa);
242
243                 /* deal with inline page */
244                 if (map.m_flags & EROFS_MAP_META) {
245                         void *vsrc, *vto;
246                         struct page *ipage;
247
248                         DBG_BUGON(map.m_plen > PAGE_SIZE);
249
250                         ipage = erofs_get_meta_page(inode->i_sb, blknr, 0);
251
252                         if (IS_ERR(ipage)) {
253                                 err = PTR_ERR(ipage);
254                                 goto err_out;
255                         }
256
257                         vsrc = kmap_atomic(ipage);
258                         vto = kmap_atomic(page);
259                         memcpy(vto, vsrc + blkoff, map.m_plen);
260                         memset(vto + map.m_plen, 0, PAGE_SIZE - map.m_plen);
261                         kunmap_atomic(vto);
262                         kunmap_atomic(vsrc);
263                         flush_dcache_page(page);
264
265                         SetPageUptodate(page);
266                         /* TODO: could we unlock the page earlier? */
267                         unlock_page(ipage);
268                         put_page(ipage);
269
270                         /* imply err = 0, see erofs_map_blocks */
271                         goto has_updated;
272                 }
273
274                 /* pa must be block-aligned for raw reading */
275                 DBG_BUGON(erofs_blkoff(map.m_pa));
276
277                 /* max # of continuous pages */
278                 if (nblocks > DIV_ROUND_UP(map.m_plen, PAGE_SIZE))
279                         nblocks = DIV_ROUND_UP(map.m_plen, PAGE_SIZE);
280                 if (nblocks > BIO_MAX_PAGES)
281                         nblocks = BIO_MAX_PAGES;
282
283                 bio = erofs_grab_bio(inode->i_sb,
284                                      blknr, nblocks, read_endio, false);
285
286                 if (IS_ERR(bio)) {
287                         err = PTR_ERR(bio);
288                         bio = NULL;
289                         goto err_out;
290                 }
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 (bio->bi_iter.bi_size >= bio->bi_max_vecs * PAGE_SIZE) {
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)
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,
350                                       unsigned int nr_pages)
351 {
352         erofs_off_t last_block;
353         struct bio *bio = NULL;
354         gfp_t gfp = readahead_gfp_mask(mapping);
355         struct page *page = list_last_entry(pages, struct page, lru);
356
357         trace_erofs_readpages(mapping->host, page, nr_pages, true);
358
359         for (; nr_pages; --nr_pages) {
360                 page = list_entry(pages->prev, struct page, lru);
361
362                 prefetchw(&page->flags);
363                 list_del(&page->lru);
364
365                 if (!add_to_page_cache_lru(page, mapping, page->index, gfp)) {
366                         bio = erofs_read_raw_page(bio, mapping, page,
367                                                   &last_block, nr_pages, true);
368
369                         /* all the page errors are ignored when readahead */
370                         if (IS_ERR(bio)) {
371                                 pr_err("%s, readahead error at page %lu of nid %llu\n",
372                                        __func__, page->index,
373                                        EROFS_V(mapping->host)->nid);
374
375                                 bio = NULL;
376                         }
377                 }
378
379                 /* pages could still be locked */
380                 put_page(page);
381         }
382         DBG_BUGON(!list_empty(pages));
383
384         /* the rare case (end in gaps) */
385         if (unlikely(bio))
386                 __submit_bio(bio, REQ_OP_READ, 0);
387         return 0;
388 }
389
390 /* for uncompressed (aligned) files and raw access for other files */
391 const struct address_space_operations erofs_raw_access_aops = {
392         .readpage = erofs_raw_access_readpage,
393         .readpages = erofs_raw_access_readpages,
394 };
395