1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2017-2018 HUAWEI, Inc.
4 * https://www.huawei.com/
5 * Copyright (C) 2021, Alibaba Cloud
8 #include <linux/prefetch.h>
9 #include <linux/sched/mm.h>
10 #include <linux/dax.h>
11 #include <trace/events/erofs.h>
13 void erofs_unmap_metabuf(struct erofs_buf *buf)
15 if (buf->kmap_type == EROFS_KMAP)
16 kunmap_local(buf->base);
18 buf->kmap_type = EROFS_NO_KMAP;
21 void erofs_put_metabuf(struct erofs_buf *buf)
25 erofs_unmap_metabuf(buf);
31 * Derive the block size from inode->i_blkbits to make compatible with
32 * anonymous inode in fscache mode.
34 void *erofs_bread(struct erofs_buf *buf, erofs_blk_t blkaddr,
35 enum erofs_kmap_type type)
37 struct inode *inode = buf->inode;
38 erofs_off_t offset = (erofs_off_t)blkaddr << inode->i_blkbits;
39 pgoff_t index = offset >> PAGE_SHIFT;
40 struct page *page = buf->page;
42 unsigned int nofs_flag;
44 if (!page || page->index != index) {
45 erofs_put_metabuf(buf);
47 nofs_flag = memalloc_nofs_save();
48 folio = read_cache_folio(inode->i_mapping, index, NULL, NULL);
49 memalloc_nofs_restore(nofs_flag);
53 /* should already be PageUptodate, no need to lock page */
54 page = folio_file_page(folio, index);
57 if (buf->kmap_type == EROFS_NO_KMAP) {
58 if (type == EROFS_KMAP)
59 buf->base = kmap_local_page(page);
60 buf->kmap_type = type;
61 } else if (buf->kmap_type != type) {
63 return ERR_PTR(-EFAULT);
65 if (type == EROFS_NO_KMAP)
67 return buf->base + (offset & ~PAGE_MASK);
70 void erofs_init_metabuf(struct erofs_buf *buf, struct super_block *sb)
72 if (erofs_is_fscache_mode(sb))
73 buf->inode = EROFS_SB(sb)->s_fscache->inode;
75 buf->inode = sb->s_bdev->bd_inode;
78 void *erofs_read_metabuf(struct erofs_buf *buf, struct super_block *sb,
79 erofs_blk_t blkaddr, enum erofs_kmap_type type)
81 erofs_init_metabuf(buf, sb);
82 return erofs_bread(buf, blkaddr, type);
85 static int erofs_map_blocks_flatmode(struct inode *inode,
86 struct erofs_map_blocks *map)
88 erofs_blk_t nblocks, lastblk;
89 u64 offset = map->m_la;
90 struct erofs_inode *vi = EROFS_I(inode);
91 struct super_block *sb = inode->i_sb;
92 bool tailendpacking = (vi->datalayout == EROFS_INODE_FLAT_INLINE);
94 nblocks = erofs_iblks(inode);
95 lastblk = nblocks - tailendpacking;
97 /* there is no hole in flatmode */
98 map->m_flags = EROFS_MAP_MAPPED;
99 if (offset < erofs_pos(sb, lastblk)) {
100 map->m_pa = erofs_pos(sb, vi->raw_blkaddr) + map->m_la;
101 map->m_plen = erofs_pos(sb, lastblk) - offset;
102 } else if (tailendpacking) {
103 map->m_pa = erofs_iloc(inode) + vi->inode_isize +
104 vi->xattr_isize + erofs_blkoff(sb, offset);
105 map->m_plen = inode->i_size - offset;
107 /* inline data should be located in the same meta block */
108 if (erofs_blkoff(sb, map->m_pa) + map->m_plen > sb->s_blocksize) {
109 erofs_err(sb, "inline data cross block boundary @ nid %llu",
112 return -EFSCORRUPTED;
114 map->m_flags |= EROFS_MAP_META;
116 erofs_err(sb, "internal error @ nid: %llu (size %llu), m_la 0x%llx",
117 vi->nid, inode->i_size, map->m_la);
124 int erofs_map_blocks(struct inode *inode, struct erofs_map_blocks *map)
126 struct super_block *sb = inode->i_sb;
127 struct erofs_inode *vi = EROFS_I(inode);
128 struct erofs_inode_chunk_index *idx;
129 struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
136 trace_erofs_map_blocks_enter(inode, map, 0);
138 if (map->m_la >= inode->i_size) {
139 /* leave out-of-bound access unmapped */
145 if (vi->datalayout != EROFS_INODE_CHUNK_BASED) {
146 err = erofs_map_blocks_flatmode(inode, map);
150 if (vi->chunkformat & EROFS_CHUNK_FORMAT_INDEXES)
151 unit = sizeof(*idx); /* chunk index */
153 unit = EROFS_BLOCK_MAP_ENTRY_SIZE; /* block map */
155 chunknr = map->m_la >> vi->chunkbits;
156 pos = ALIGN(erofs_iloc(inode) + vi->inode_isize +
157 vi->xattr_isize, unit) + unit * chunknr;
159 kaddr = erofs_read_metabuf(&buf, sb, erofs_blknr(sb, pos), EROFS_KMAP);
161 err = PTR_ERR(kaddr);
164 map->m_la = chunknr << vi->chunkbits;
165 map->m_plen = min_t(erofs_off_t, 1UL << vi->chunkbits,
166 round_up(inode->i_size - map->m_la, sb->s_blocksize));
168 /* handle block map */
169 if (!(vi->chunkformat & EROFS_CHUNK_FORMAT_INDEXES)) {
170 __le32 *blkaddr = kaddr + erofs_blkoff(sb, pos);
172 if (le32_to_cpu(*blkaddr) == EROFS_NULL_ADDR) {
175 map->m_pa = erofs_pos(sb, le32_to_cpu(*blkaddr));
176 map->m_flags = EROFS_MAP_MAPPED;
180 /* parse chunk indexes */
181 idx = kaddr + erofs_blkoff(sb, pos);
182 switch (le32_to_cpu(idx->blkaddr)) {
183 case EROFS_NULL_ADDR:
187 map->m_deviceid = le16_to_cpu(idx->device_id) &
188 EROFS_SB(sb)->device_id_mask;
189 map->m_pa = erofs_pos(sb, le32_to_cpu(idx->blkaddr));
190 map->m_flags = EROFS_MAP_MAPPED;
194 erofs_put_metabuf(&buf);
197 map->m_llen = map->m_plen;
198 trace_erofs_map_blocks_exit(inode, map, 0, err);
202 int erofs_map_dev(struct super_block *sb, struct erofs_map_dev *map)
204 struct erofs_dev_context *devs = EROFS_SB(sb)->devs;
205 struct erofs_device_info *dif;
208 map->m_bdev = sb->s_bdev;
209 map->m_daxdev = EROFS_SB(sb)->dax_dev;
210 map->m_dax_part_off = EROFS_SB(sb)->dax_part_off;
211 map->m_fscache = EROFS_SB(sb)->s_fscache;
213 if (map->m_deviceid) {
214 down_read(&devs->rwsem);
215 dif = idr_find(&devs->tree, map->m_deviceid - 1);
217 up_read(&devs->rwsem);
221 map->m_pa += erofs_pos(sb, dif->mapped_blkaddr);
222 up_read(&devs->rwsem);
225 map->m_bdev = dif->bdev;
226 map->m_daxdev = dif->dax_dev;
227 map->m_dax_part_off = dif->dax_part_off;
228 map->m_fscache = dif->fscache;
229 up_read(&devs->rwsem);
230 } else if (devs->extra_devices && !devs->flatdev) {
231 down_read(&devs->rwsem);
232 idr_for_each_entry(&devs->tree, dif, id) {
233 erofs_off_t startoff, length;
235 if (!dif->mapped_blkaddr)
237 startoff = erofs_pos(sb, dif->mapped_blkaddr);
238 length = erofs_pos(sb, dif->blocks);
240 if (map->m_pa >= startoff &&
241 map->m_pa < startoff + length) {
242 map->m_pa -= startoff;
243 map->m_bdev = dif->bdev;
244 map->m_daxdev = dif->dax_dev;
245 map->m_dax_part_off = dif->dax_part_off;
246 map->m_fscache = dif->fscache;
250 up_read(&devs->rwsem);
255 static int erofs_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
256 unsigned int flags, struct iomap *iomap, struct iomap *srcmap)
259 struct super_block *sb = inode->i_sb;
260 struct erofs_map_blocks map;
261 struct erofs_map_dev mdev;
266 ret = erofs_map_blocks(inode, &map);
270 mdev = (struct erofs_map_dev) {
271 .m_deviceid = map.m_deviceid,
274 ret = erofs_map_dev(sb, &mdev);
278 iomap->offset = map.m_la;
279 if (flags & IOMAP_DAX)
280 iomap->dax_dev = mdev.m_daxdev;
282 iomap->bdev = mdev.m_bdev;
283 iomap->length = map.m_llen;
285 iomap->private = NULL;
287 if (!(map.m_flags & EROFS_MAP_MAPPED)) {
288 iomap->type = IOMAP_HOLE;
289 iomap->addr = IOMAP_NULL_ADDR;
291 iomap->length = length;
295 if (map.m_flags & EROFS_MAP_META) {
297 struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
299 iomap->type = IOMAP_INLINE;
300 ptr = erofs_read_metabuf(&buf, sb,
301 erofs_blknr(sb, mdev.m_pa), EROFS_KMAP);
304 iomap->inline_data = ptr + erofs_blkoff(sb, mdev.m_pa);
305 iomap->private = buf.base;
307 iomap->type = IOMAP_MAPPED;
308 iomap->addr = mdev.m_pa;
309 if (flags & IOMAP_DAX)
310 iomap->addr += mdev.m_dax_part_off;
315 static int erofs_iomap_end(struct inode *inode, loff_t pos, loff_t length,
316 ssize_t written, unsigned int flags, struct iomap *iomap)
318 void *ptr = iomap->private;
321 struct erofs_buf buf = {
322 .page = kmap_to_page(ptr),
324 .kmap_type = EROFS_KMAP,
327 DBG_BUGON(iomap->type != IOMAP_INLINE);
328 erofs_put_metabuf(&buf);
330 DBG_BUGON(iomap->type == IOMAP_INLINE);
335 static const struct iomap_ops erofs_iomap_ops = {
336 .iomap_begin = erofs_iomap_begin,
337 .iomap_end = erofs_iomap_end,
340 int erofs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
343 if (erofs_inode_is_data_compressed(EROFS_I(inode)->datalayout)) {
344 #ifdef CONFIG_EROFS_FS_ZIP
345 return iomap_fiemap(inode, fieinfo, start, len,
346 &z_erofs_iomap_report_ops);
351 return iomap_fiemap(inode, fieinfo, start, len, &erofs_iomap_ops);
355 * since we dont have write or truncate flows, so no inode
356 * locking needs to be held at the moment.
358 static int erofs_read_folio(struct file *file, struct folio *folio)
360 return iomap_read_folio(folio, &erofs_iomap_ops);
363 static void erofs_readahead(struct readahead_control *rac)
365 return iomap_readahead(rac, &erofs_iomap_ops);
368 static sector_t erofs_bmap(struct address_space *mapping, sector_t block)
370 return iomap_bmap(mapping, block, &erofs_iomap_ops);
373 static ssize_t erofs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
375 struct inode *inode = file_inode(iocb->ki_filp);
377 /* no need taking (shared) inode lock since it's a ro filesystem */
378 if (!iov_iter_count(to))
383 return dax_iomap_rw(iocb, to, &erofs_iomap_ops);
385 if (iocb->ki_flags & IOCB_DIRECT) {
386 struct block_device *bdev = inode->i_sb->s_bdev;
387 unsigned int blksize_mask;
390 blksize_mask = bdev_logical_block_size(bdev) - 1;
392 blksize_mask = i_blocksize(inode) - 1;
394 if ((iocb->ki_pos | iov_iter_count(to) |
395 iov_iter_alignment(to)) & blksize_mask)
398 return iomap_dio_rw(iocb, to, &erofs_iomap_ops,
401 return filemap_read(iocb, to, 0);
404 /* for uncompressed (aligned) files and raw access for other files */
405 const struct address_space_operations erofs_raw_access_aops = {
406 .read_folio = erofs_read_folio,
407 .readahead = erofs_readahead,
409 .direct_IO = noop_direct_IO,
410 .release_folio = iomap_release_folio,
411 .invalidate_folio = iomap_invalidate_folio,
415 static vm_fault_t erofs_dax_huge_fault(struct vm_fault *vmf,
418 return dax_iomap_fault(vmf, order, NULL, NULL, &erofs_iomap_ops);
421 static vm_fault_t erofs_dax_fault(struct vm_fault *vmf)
423 return erofs_dax_huge_fault(vmf, 0);
426 static const struct vm_operations_struct erofs_dax_vm_ops = {
427 .fault = erofs_dax_fault,
428 .huge_fault = erofs_dax_huge_fault,
431 static int erofs_file_mmap(struct file *file, struct vm_area_struct *vma)
433 if (!IS_DAX(file_inode(file)))
434 return generic_file_readonly_mmap(file, vma);
436 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
439 vma->vm_ops = &erofs_dax_vm_ops;
440 vm_flags_set(vma, VM_HUGEPAGE);
444 #define erofs_file_mmap generic_file_readonly_mmap
447 const struct file_operations erofs_file_fops = {
448 .llseek = generic_file_llseek,
449 .read_iter = erofs_file_read_iter,
450 .mmap = erofs_file_mmap,
451 .splice_read = filemap_splice_read,