erofs: register fscache context for extra data blobs
[linux-2.6-block.git] / fs / erofs / data.c
CommitLineData
29b24f6c 1// SPDX-License-Identifier: GPL-2.0-only
81781b02 2/*
81781b02 3 * Copyright (C) 2017-2018 HUAWEI, Inc.
592e7cd0 4 * https://www.huawei.com/
c5aa903a 5 * Copyright (C) 2021, Alibaba Cloud
81781b02
GX
6 */
7#include "internal.h"
8#include <linux/prefetch.h>
06252e9c 9#include <linux/dax.h>
13f06f48
CY
10#include <trace/events/erofs.h>
11
fdf80a47
GX
12void erofs_unmap_metabuf(struct erofs_buf *buf)
13{
14 if (buf->kmap_type == EROFS_KMAP)
15 kunmap(buf->page);
16 else if (buf->kmap_type == EROFS_KMAP_ATOMIC)
17 kunmap_atomic(buf->base);
18 buf->base = NULL;
19 buf->kmap_type = EROFS_NO_KMAP;
20}
21
22void erofs_put_metabuf(struct erofs_buf *buf)
23{
24 if (!buf->page)
25 return;
26 erofs_unmap_metabuf(buf);
27 put_page(buf->page);
28 buf->page = NULL;
29}
30
fe5de585
GX
31void *erofs_bread(struct erofs_buf *buf, struct inode *inode,
32 erofs_blk_t blkaddr, enum erofs_kmap_type type)
fdf80a47 33{
fe5de585 34 struct address_space *const mapping = inode->i_mapping;
fdf80a47
GX
35 erofs_off_t offset = blknr_to_addr(blkaddr);
36 pgoff_t index = offset >> PAGE_SHIFT;
37 struct page *page = buf->page;
38
39 if (!page || page->index != index) {
40 erofs_put_metabuf(buf);
41 page = read_cache_page_gfp(mapping, index,
42 mapping_gfp_constraint(mapping, ~__GFP_FS));
43 if (IS_ERR(page))
44 return page;
45 /* should already be PageUptodate, no need to lock page */
46 buf->page = page;
47 }
48 if (buf->kmap_type == EROFS_NO_KMAP) {
49 if (type == EROFS_KMAP)
50 buf->base = kmap(page);
51 else if (type == EROFS_KMAP_ATOMIC)
52 buf->base = kmap_atomic(page);
53 buf->kmap_type = type;
54 } else if (buf->kmap_type != type) {
55 DBG_BUGON(1);
56 return ERR_PTR(-EFAULT);
57 }
58 if (type == EROFS_NO_KMAP)
59 return NULL;
60 return buf->base + (offset & ~PAGE_MASK);
61}
62
fe5de585
GX
63void *erofs_read_metabuf(struct erofs_buf *buf, struct super_block *sb,
64 erofs_blk_t blkaddr, enum erofs_kmap_type type)
65{
66 return erofs_bread(buf, sb->s_bdev->bd_inode, blkaddr, type);
67}
68
81781b02 69static int erofs_map_blocks_flatmode(struct inode *inode,
f0950b02
BD
70 struct erofs_map_blocks *map,
71 int flags)
81781b02
GX
72{
73 erofs_blk_t nblocks, lastblk;
74 u64 offset = map->m_la;
a5876e24 75 struct erofs_inode *vi = EROFS_I(inode);
8a765682 76 bool tailendpacking = (vi->datalayout == EROFS_INODE_FLAT_INLINE);
81781b02 77
fdf80a47 78 nblocks = DIV_ROUND_UP(inode->i_size, EROFS_BLKSIZ);
8a765682 79 lastblk = nblocks - tailendpacking;
81781b02 80
81781b02
GX
81 /* there is no hole in flatmode */
82 map->m_flags = EROFS_MAP_MAPPED;
81781b02
GX
83 if (offset < blknr_to_addr(lastblk)) {
84 map->m_pa = blknr_to_addr(vi->raw_blkaddr) + map->m_la;
85 map->m_plen = blknr_to_addr(lastblk) - offset;
8a765682 86 } else if (tailendpacking) {
81781b02
GX
87 /* 2 - inode inline B: inode, [xattrs], inline last blk... */
88 struct erofs_sb_info *sbi = EROFS_SB(inode->i_sb);
89
90 map->m_pa = iloc(sbi, vi->nid) + vi->inode_isize +
91 vi->xattr_isize + erofs_blkoff(map->m_la);
92 map->m_plen = inode->i_size - offset;
93
469407a3
GX
94 /* inline data should be located in the same meta block */
95 if (erofs_blkoff(map->m_pa) + map->m_plen > EROFS_BLKSIZ) {
4f761fa2
GX
96 erofs_err(inode->i_sb,
97 "inline data cross block boundary @ nid %llu",
98 vi->nid);
9141b60c 99 DBG_BUGON(1);
469407a3 100 return -EFSCORRUPTED;
9141b60c 101 }
81781b02
GX
102 map->m_flags |= EROFS_MAP_META;
103 } else {
4f761fa2
GX
104 erofs_err(inode->i_sb,
105 "internal error @ nid: %llu (size %llu), m_la 0x%llx",
106 vi->nid, inode->i_size, map->m_la);
9141b60c 107 DBG_BUGON(1);
469407a3 108 return -EIO;
81781b02 109 }
469407a3 110 return 0;
81781b02
GX
111}
112
94d78946
JX
113int erofs_map_blocks(struct inode *inode,
114 struct erofs_map_blocks *map, int flags)
c5aa903a
GX
115{
116 struct super_block *sb = inode->i_sb;
117 struct erofs_inode *vi = EROFS_I(inode);
118 struct erofs_inode_chunk_index *idx;
fdf80a47 119 struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
c5aa903a
GX
120 u64 chunknr;
121 unsigned int unit;
122 erofs_off_t pos;
fdf80a47 123 void *kaddr;
c5aa903a
GX
124 int err = 0;
125
469407a3 126 trace_erofs_map_blocks_enter(inode, map, flags);
dfeab2e9 127 map->m_deviceid = 0;
c5aa903a
GX
128 if (map->m_la >= inode->i_size) {
129 /* leave out-of-bound access unmapped */
130 map->m_flags = 0;
131 map->m_plen = 0;
132 goto out;
133 }
134
469407a3
GX
135 if (vi->datalayout != EROFS_INODE_CHUNK_BASED) {
136 err = erofs_map_blocks_flatmode(inode, map, flags);
137 goto out;
138 }
c5aa903a
GX
139
140 if (vi->chunkformat & EROFS_CHUNK_FORMAT_INDEXES)
141 unit = sizeof(*idx); /* chunk index */
142 else
143 unit = EROFS_BLOCK_MAP_ENTRY_SIZE; /* block map */
144
145 chunknr = map->m_la >> vi->chunkbits;
146 pos = ALIGN(iloc(EROFS_SB(sb), vi->nid) + vi->inode_isize +
147 vi->xattr_isize, unit) + unit * chunknr;
148
fdf80a47
GX
149 kaddr = erofs_read_metabuf(&buf, sb, erofs_blknr(pos), EROFS_KMAP);
150 if (IS_ERR(kaddr)) {
151 err = PTR_ERR(kaddr);
469407a3
GX
152 goto out;
153 }
c5aa903a
GX
154 map->m_la = chunknr << vi->chunkbits;
155 map->m_plen = min_t(erofs_off_t, 1UL << vi->chunkbits,
156 roundup(inode->i_size - map->m_la, EROFS_BLKSIZ));
157
158 /* handle block map */
159 if (!(vi->chunkformat & EROFS_CHUNK_FORMAT_INDEXES)) {
fdf80a47 160 __le32 *blkaddr = kaddr + erofs_blkoff(pos);
c5aa903a
GX
161
162 if (le32_to_cpu(*blkaddr) == EROFS_NULL_ADDR) {
163 map->m_flags = 0;
164 } else {
165 map->m_pa = blknr_to_addr(le32_to_cpu(*blkaddr));
166 map->m_flags = EROFS_MAP_MAPPED;
167 }
168 goto out_unlock;
169 }
170 /* parse chunk indexes */
fdf80a47 171 idx = kaddr + erofs_blkoff(pos);
c5aa903a
GX
172 switch (le32_to_cpu(idx->blkaddr)) {
173 case EROFS_NULL_ADDR:
174 map->m_flags = 0;
175 break;
176 default:
dfeab2e9
GX
177 map->m_deviceid = le16_to_cpu(idx->device_id) &
178 EROFS_SB(sb)->device_id_mask;
c5aa903a
GX
179 map->m_pa = blknr_to_addr(le32_to_cpu(idx->blkaddr));
180 map->m_flags = EROFS_MAP_MAPPED;
181 break;
182 }
183out_unlock:
fdf80a47 184 erofs_put_metabuf(&buf);
c5aa903a 185out:
469407a3
GX
186 if (!err)
187 map->m_llen = map->m_plen;
188 trace_erofs_map_blocks_exit(inode, map, flags, 0);
c5aa903a
GX
189 return err;
190}
191
dfeab2e9
GX
192int erofs_map_dev(struct super_block *sb, struct erofs_map_dev *map)
193{
194 struct erofs_dev_context *devs = EROFS_SB(sb)->devs;
195 struct erofs_device_info *dif;
196 int id;
197
198 /* primary device by default */
199 map->m_bdev = sb->s_bdev;
200 map->m_daxdev = EROFS_SB(sb)->dax_dev;
de205114 201 map->m_dax_part_off = EROFS_SB(sb)->dax_part_off;
955b478e 202 map->m_fscache = EROFS_SB(sb)->s_fscache;
dfeab2e9
GX
203
204 if (map->m_deviceid) {
205 down_read(&devs->rwsem);
206 dif = idr_find(&devs->tree, map->m_deviceid - 1);
207 if (!dif) {
208 up_read(&devs->rwsem);
209 return -ENODEV;
210 }
211 map->m_bdev = dif->bdev;
212 map->m_daxdev = dif->dax_dev;
de205114 213 map->m_dax_part_off = dif->dax_part_off;
955b478e 214 map->m_fscache = dif->fscache;
dfeab2e9
GX
215 up_read(&devs->rwsem);
216 } else if (devs->extra_devices) {
217 down_read(&devs->rwsem);
218 idr_for_each_entry(&devs->tree, dif, id) {
219 erofs_off_t startoff, length;
220
221 if (!dif->mapped_blkaddr)
222 continue;
223 startoff = blknr_to_addr(dif->mapped_blkaddr);
224 length = blknr_to_addr(dif->blocks);
225
226 if (map->m_pa >= startoff &&
227 map->m_pa < startoff + length) {
228 map->m_pa -= startoff;
229 map->m_bdev = dif->bdev;
230 map->m_daxdev = dif->dax_dev;
de205114 231 map->m_dax_part_off = dif->dax_part_off;
955b478e 232 map->m_fscache = dif->fscache;
dfeab2e9
GX
233 break;
234 }
235 }
236 up_read(&devs->rwsem);
237 }
238 return 0;
239}
240
a08e67a0
HJ
241static int erofs_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
242 unsigned int flags, struct iomap *iomap, struct iomap *srcmap)
243{
244 int ret;
245 struct erofs_map_blocks map;
dfeab2e9 246 struct erofs_map_dev mdev;
a08e67a0
HJ
247
248 map.m_la = offset;
249 map.m_llen = length;
250
c5aa903a 251 ret = erofs_map_blocks(inode, &map, EROFS_GET_BLOCKS_RAW);
a08e67a0
HJ
252 if (ret < 0)
253 return ret;
254
dfeab2e9
GX
255 mdev = (struct erofs_map_dev) {
256 .m_deviceid = map.m_deviceid,
257 .m_pa = map.m_pa,
258 };
259 ret = erofs_map_dev(inode->i_sb, &mdev);
260 if (ret)
261 return ret;
262
a08e67a0 263 iomap->offset = map.m_la;
e33f42b2 264 if (flags & IOMAP_DAX)
de205114 265 iomap->dax_dev = mdev.m_daxdev;
e33f42b2 266 else
de205114 267 iomap->bdev = mdev.m_bdev;
a08e67a0
HJ
268 iomap->length = map.m_llen;
269 iomap->flags = 0;
771c994e 270 iomap->private = NULL;
a08e67a0
HJ
271
272 if (!(map.m_flags & EROFS_MAP_MAPPED)) {
273 iomap->type = IOMAP_HOLE;
274 iomap->addr = IOMAP_NULL_ADDR;
275 if (!iomap->length)
276 iomap->length = length;
277 return 0;
278 }
279
a08e67a0 280 if (map.m_flags & EROFS_MAP_META) {
fdf80a47
GX
281 void *ptr;
282 struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
771c994e
GX
283
284 iomap->type = IOMAP_INLINE;
fdf80a47
GX
285 ptr = erofs_read_metabuf(&buf, inode->i_sb,
286 erofs_blknr(mdev.m_pa), EROFS_KMAP);
287 if (IS_ERR(ptr))
288 return PTR_ERR(ptr);
289 iomap->inline_data = ptr + erofs_blkoff(mdev.m_pa);
290 iomap->private = buf.base;
771c994e
GX
291 } else {
292 iomap->type = IOMAP_MAPPED;
dfeab2e9 293 iomap->addr = mdev.m_pa;
e33f42b2
GX
294 if (flags & IOMAP_DAX)
295 iomap->addr += mdev.m_dax_part_off;
a08e67a0 296 }
a08e67a0
HJ
297 return 0;
298}
299
771c994e
GX
300static int erofs_iomap_end(struct inode *inode, loff_t pos, loff_t length,
301 ssize_t written, unsigned int flags, struct iomap *iomap)
302{
fdf80a47
GX
303 void *ptr = iomap->private;
304
305 if (ptr) {
306 struct erofs_buf buf = {
307 .page = kmap_to_page(ptr),
308 .base = ptr,
309 .kmap_type = EROFS_KMAP,
310 };
771c994e 311
771c994e 312 DBG_BUGON(iomap->type != IOMAP_INLINE);
fdf80a47 313 erofs_put_metabuf(&buf);
771c994e
GX
314 } else {
315 DBG_BUGON(iomap->type == IOMAP_INLINE);
316 }
317 return written;
318}
319
a08e67a0
HJ
320static const struct iomap_ops erofs_iomap_ops = {
321 .iomap_begin = erofs_iomap_begin,
771c994e 322 .iomap_end = erofs_iomap_end,
a08e67a0
HJ
323};
324
eadcd6b5
GX
325int erofs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
326 u64 start, u64 len)
327{
328 if (erofs_inode_is_data_compressed(EROFS_I(inode)->datalayout)) {
329#ifdef CONFIG_EROFS_FS_ZIP
330 return iomap_fiemap(inode, fieinfo, start, len,
331 &z_erofs_iomap_report_ops);
332#else
333 return -EOPNOTSUPP;
334#endif
335 }
336 return iomap_fiemap(inode, fieinfo, start, len, &erofs_iomap_ops);
337}
338
771c994e
GX
339/*
340 * since we dont have write or truncate flows, so no inode
341 * locking needs to be held at the moment.
342 */
343static int erofs_readpage(struct file *file, struct page *page)
344{
345 return iomap_readpage(page, &erofs_iomap_ops);
346}
347
348static void erofs_readahead(struct readahead_control *rac)
349{
350 return iomap_readahead(rac, &erofs_iomap_ops);
351}
352
353static sector_t erofs_bmap(struct address_space *mapping, sector_t block)
354{
355 return iomap_bmap(mapping, block, &erofs_iomap_ops);
356}
357
a08e67a0
HJ
358static int erofs_prepare_dio(struct kiocb *iocb, struct iov_iter *to)
359{
360 struct inode *inode = file_inode(iocb->ki_filp);
361 loff_t align = iocb->ki_pos | iov_iter_count(to) |
362 iov_iter_alignment(to);
363 struct block_device *bdev = inode->i_sb->s_bdev;
364 unsigned int blksize_mask;
365
366 if (bdev)
367 blksize_mask = (1 << ilog2(bdev_logical_block_size(bdev))) - 1;
368 else
369 blksize_mask = (1 << inode->i_blkbits) - 1;
370
371 if (align & blksize_mask)
372 return -EINVAL;
a08e67a0
HJ
373 return 0;
374}
375
376static ssize_t erofs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
377{
378 /* no need taking (shared) inode lock since it's a ro filesystem */
379 if (!iov_iter_count(to))
380 return 0;
381
06252e9c
GX
382#ifdef CONFIG_FS_DAX
383 if (IS_DAX(iocb->ki_filp->f_mapping->host))
384 return dax_iomap_rw(iocb, to, &erofs_iomap_ops);
385#endif
a08e67a0
HJ
386 if (iocb->ki_flags & IOCB_DIRECT) {
387 int err = erofs_prepare_dio(iocb, to);
388
389 if (!err)
390 return iomap_dio_rw(iocb, to, &erofs_iomap_ops,
4fdccaa0 391 NULL, 0, 0);
a08e67a0
HJ
392 if (err < 0)
393 return err;
394 }
395 return filemap_read(iocb, to, 0);
396}
397
81781b02
GX
398/* for uncompressed (aligned) files and raw access for other files */
399const struct address_space_operations erofs_raw_access_aops = {
771c994e
GX
400 .readpage = erofs_readpage,
401 .readahead = erofs_readahead,
9da681e0 402 .bmap = erofs_bmap,
a08e67a0
HJ
403 .direct_IO = noop_direct_IO,
404};
405
06252e9c
GX
406#ifdef CONFIG_FS_DAX
407static vm_fault_t erofs_dax_huge_fault(struct vm_fault *vmf,
408 enum page_entry_size pe_size)
409{
410 return dax_iomap_fault(vmf, pe_size, NULL, NULL, &erofs_iomap_ops);
411}
412
413static vm_fault_t erofs_dax_fault(struct vm_fault *vmf)
414{
415 return erofs_dax_huge_fault(vmf, PE_SIZE_PTE);
416}
417
418static const struct vm_operations_struct erofs_dax_vm_ops = {
419 .fault = erofs_dax_fault,
420 .huge_fault = erofs_dax_huge_fault,
421};
422
423static int erofs_file_mmap(struct file *file, struct vm_area_struct *vma)
424{
425 if (!IS_DAX(file_inode(file)))
426 return generic_file_readonly_mmap(file, vma);
427
428 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
429 return -EINVAL;
430
431 vma->vm_ops = &erofs_dax_vm_ops;
432 vma->vm_flags |= VM_HUGEPAGE;
433 return 0;
434}
435#else
436#define erofs_file_mmap generic_file_readonly_mmap
437#endif
438
a08e67a0
HJ
439const struct file_operations erofs_file_fops = {
440 .llseek = generic_file_llseek,
441 .read_iter = erofs_file_read_iter,
06252e9c 442 .mmap = erofs_file_mmap,
a08e67a0 443 .splice_read = generic_file_splice_read,
81781b02 444};