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/sched/mm.h>
9 #include <trace/events/erofs.h>
11 void erofs_unmap_metabuf(struct erofs_buf *buf)
13 if (buf->kmap_type == EROFS_KMAP)
14 kunmap_local(buf->base);
16 buf->kmap_type = EROFS_NO_KMAP;
19 void erofs_put_metabuf(struct erofs_buf *buf)
23 erofs_unmap_metabuf(buf);
24 folio_put(page_folio(buf->page));
28 void *erofs_bread(struct erofs_buf *buf, erofs_off_t offset,
29 enum erofs_kmap_type type)
31 pgoff_t index = offset >> PAGE_SHIFT;
32 struct folio *folio = NULL;
35 folio = page_folio(buf->page);
36 if (folio_file_page(folio, index) != buf->page)
37 erofs_unmap_metabuf(buf);
39 if (!folio || !folio_contains(folio, index)) {
40 erofs_put_metabuf(buf);
41 folio = read_mapping_folio(buf->mapping, index, NULL);
45 buf->page = folio_file_page(folio, index);
47 if (buf->kmap_type == EROFS_NO_KMAP) {
48 if (type == EROFS_KMAP)
49 buf->base = kmap_local_page(buf->page);
50 buf->kmap_type = type;
51 } else if (buf->kmap_type != type) {
53 return ERR_PTR(-EFAULT);
55 if (type == EROFS_NO_KMAP)
57 return buf->base + (offset & ~PAGE_MASK);
60 void erofs_init_metabuf(struct erofs_buf *buf, struct super_block *sb)
62 struct erofs_sb_info *sbi = EROFS_SB(sb);
64 if (erofs_is_fileio_mode(sbi))
65 buf->mapping = file_inode(sbi->fdev)->i_mapping;
66 else if (erofs_is_fscache_mode(sb))
67 buf->mapping = sbi->s_fscache->inode->i_mapping;
69 buf->mapping = sb->s_bdev->bd_mapping;
72 void *erofs_read_metabuf(struct erofs_buf *buf, struct super_block *sb,
73 erofs_off_t offset, enum erofs_kmap_type type)
75 erofs_init_metabuf(buf, sb);
76 return erofs_bread(buf, offset, type);
79 static int erofs_map_blocks_flatmode(struct inode *inode,
80 struct erofs_map_blocks *map)
82 struct erofs_inode *vi = EROFS_I(inode);
83 struct super_block *sb = inode->i_sb;
84 bool tailendpacking = (vi->datalayout == EROFS_INODE_FLAT_INLINE);
85 erofs_blk_t lastblk = erofs_iblks(inode) - tailendpacking;
87 map->m_flags = EROFS_MAP_MAPPED; /* no hole in flat inodes */
88 if (map->m_la < erofs_pos(sb, lastblk)) {
89 map->m_pa = erofs_pos(sb, vi->raw_blkaddr) + map->m_la;
90 map->m_plen = erofs_pos(sb, lastblk) - map->m_la;
92 DBG_BUGON(!tailendpacking);
93 map->m_pa = erofs_iloc(inode) + vi->inode_isize +
94 vi->xattr_isize + erofs_blkoff(sb, map->m_la);
95 map->m_plen = inode->i_size - map->m_la;
97 /* inline data should be located in the same meta block */
98 if (erofs_blkoff(sb, map->m_pa) + map->m_plen > sb->s_blocksize) {
99 erofs_err(sb, "inline data across blocks @ nid %llu", vi->nid);
101 return -EFSCORRUPTED;
103 map->m_flags |= EROFS_MAP_META;
108 int erofs_map_blocks(struct inode *inode, struct erofs_map_blocks *map)
110 struct super_block *sb = inode->i_sb;
111 struct erofs_inode *vi = EROFS_I(inode);
112 struct erofs_inode_chunk_index *idx;
113 struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
120 trace_erofs_map_blocks_enter(inode, map, 0);
122 if (map->m_la >= inode->i_size) {
123 /* leave out-of-bound access unmapped */
125 map->m_plen = map->m_llen;
129 if (vi->datalayout != EROFS_INODE_CHUNK_BASED) {
130 err = erofs_map_blocks_flatmode(inode, map);
134 if (vi->chunkformat & EROFS_CHUNK_FORMAT_INDEXES)
135 unit = sizeof(*idx); /* chunk index */
137 unit = EROFS_BLOCK_MAP_ENTRY_SIZE; /* block map */
139 chunknr = map->m_la >> vi->chunkbits;
140 pos = ALIGN(erofs_iloc(inode) + vi->inode_isize +
141 vi->xattr_isize, unit) + unit * chunknr;
143 kaddr = erofs_read_metabuf(&buf, sb, pos, EROFS_KMAP);
145 err = PTR_ERR(kaddr);
148 map->m_la = chunknr << vi->chunkbits;
149 map->m_plen = min_t(erofs_off_t, 1UL << vi->chunkbits,
150 round_up(inode->i_size - map->m_la, sb->s_blocksize));
152 /* handle block map */
153 if (!(vi->chunkformat & EROFS_CHUNK_FORMAT_INDEXES)) {
154 __le32 *blkaddr = kaddr;
156 if (le32_to_cpu(*blkaddr) == EROFS_NULL_ADDR) {
159 map->m_pa = erofs_pos(sb, le32_to_cpu(*blkaddr));
160 map->m_flags = EROFS_MAP_MAPPED;
164 /* parse chunk indexes */
166 switch (le32_to_cpu(idx->blkaddr)) {
167 case EROFS_NULL_ADDR:
171 map->m_deviceid = le16_to_cpu(idx->device_id) &
172 EROFS_SB(sb)->device_id_mask;
173 map->m_pa = erofs_pos(sb, le32_to_cpu(idx->blkaddr));
174 map->m_flags = EROFS_MAP_MAPPED;
178 erofs_put_metabuf(&buf);
181 map->m_llen = map->m_plen;
182 trace_erofs_map_blocks_exit(inode, map, 0, err);
186 static void erofs_fill_from_devinfo(struct erofs_map_dev *map,
187 struct erofs_device_info *dif)
192 if (S_ISBLK(file_inode(dif->file)->i_mode))
193 map->m_bdev = file_bdev(dif->file);
195 map->m_fp = dif->file;
197 map->m_daxdev = dif->dax_dev;
198 map->m_dax_part_off = dif->dax_part_off;
199 map->m_fscache = dif->fscache;
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;
206 erofs_off_t startoff, length;
209 map->m_bdev = sb->s_bdev;
210 map->m_daxdev = EROFS_SB(sb)->dax_dev;
211 map->m_dax_part_off = EROFS_SB(sb)->dax_part_off;
212 map->m_fscache = EROFS_SB(sb)->s_fscache;
213 map->m_fp = EROFS_SB(sb)->fdev;
215 if (map->m_deviceid) {
216 down_read(&devs->rwsem);
217 dif = idr_find(&devs->tree, map->m_deviceid - 1);
219 up_read(&devs->rwsem);
223 map->m_pa += erofs_pos(sb, dif->mapped_blkaddr);
224 up_read(&devs->rwsem);
227 erofs_fill_from_devinfo(map, dif);
228 up_read(&devs->rwsem);
229 } else if (devs->extra_devices && !devs->flatdev) {
230 down_read(&devs->rwsem);
231 idr_for_each_entry(&devs->tree, dif, id) {
232 if (!dif->mapped_blkaddr)
235 startoff = erofs_pos(sb, dif->mapped_blkaddr);
236 length = erofs_pos(sb, dif->blocks);
237 if (map->m_pa >= startoff &&
238 map->m_pa < startoff + length) {
239 map->m_pa -= startoff;
240 erofs_fill_from_devinfo(map, dif);
244 up_read(&devs->rwsem);
250 * bit 30: I/O error occurred on this folio
251 * bit 0 - 29: remaining parts to complete this folio
253 #define EROFS_ONLINEFOLIO_EIO (1 << 30)
255 void erofs_onlinefolio_init(struct folio *folio)
260 } u = { .o = ATOMIC_INIT(1) };
262 folio->private = u.v; /* valid only if file-backed folio is locked */
265 void erofs_onlinefolio_split(struct folio *folio)
267 atomic_inc((atomic_t *)&folio->private);
270 void erofs_onlinefolio_end(struct folio *folio, int err)
275 orig = atomic_read((atomic_t *)&folio->private);
276 v = (orig - 1) | (err ? EROFS_ONLINEFOLIO_EIO : 0);
277 } while (atomic_cmpxchg((atomic_t *)&folio->private, orig, v) != orig);
279 if (v & ~EROFS_ONLINEFOLIO_EIO)
282 folio_end_read(folio, !(v & EROFS_ONLINEFOLIO_EIO));
285 static int erofs_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
286 unsigned int flags, struct iomap *iomap, struct iomap *srcmap)
289 struct super_block *sb = inode->i_sb;
290 struct erofs_map_blocks map;
291 struct erofs_map_dev mdev;
296 ret = erofs_map_blocks(inode, &map);
300 mdev = (struct erofs_map_dev) {
301 .m_deviceid = map.m_deviceid,
304 ret = erofs_map_dev(sb, &mdev);
308 iomap->offset = map.m_la;
309 if (flags & IOMAP_DAX)
310 iomap->dax_dev = mdev.m_daxdev;
312 iomap->bdev = mdev.m_bdev;
313 iomap->length = map.m_llen;
315 iomap->private = NULL;
317 if (!(map.m_flags & EROFS_MAP_MAPPED)) {
318 iomap->type = IOMAP_HOLE;
319 iomap->addr = IOMAP_NULL_ADDR;
321 iomap->length = length;
325 if (map.m_flags & EROFS_MAP_META) {
327 struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
329 iomap->type = IOMAP_INLINE;
330 ptr = erofs_read_metabuf(&buf, sb, mdev.m_pa, EROFS_KMAP);
333 iomap->inline_data = ptr;
334 iomap->private = buf.base;
336 iomap->type = IOMAP_MAPPED;
337 iomap->addr = mdev.m_pa;
338 if (flags & IOMAP_DAX)
339 iomap->addr += mdev.m_dax_part_off;
344 static int erofs_iomap_end(struct inode *inode, loff_t pos, loff_t length,
345 ssize_t written, unsigned int flags, struct iomap *iomap)
347 void *ptr = iomap->private;
350 struct erofs_buf buf = {
351 .page = kmap_to_page(ptr),
353 .kmap_type = EROFS_KMAP,
356 DBG_BUGON(iomap->type != IOMAP_INLINE);
357 erofs_put_metabuf(&buf);
359 DBG_BUGON(iomap->type == IOMAP_INLINE);
364 static const struct iomap_ops erofs_iomap_ops = {
365 .iomap_begin = erofs_iomap_begin,
366 .iomap_end = erofs_iomap_end,
369 int erofs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
372 if (erofs_inode_is_data_compressed(EROFS_I(inode)->datalayout)) {
373 #ifdef CONFIG_EROFS_FS_ZIP
374 return iomap_fiemap(inode, fieinfo, start, len,
375 &z_erofs_iomap_report_ops);
380 return iomap_fiemap(inode, fieinfo, start, len, &erofs_iomap_ops);
384 * since we dont have write or truncate flows, so no inode
385 * locking needs to be held at the moment.
387 static int erofs_read_folio(struct file *file, struct folio *folio)
389 return iomap_read_folio(folio, &erofs_iomap_ops);
392 static void erofs_readahead(struct readahead_control *rac)
394 return iomap_readahead(rac, &erofs_iomap_ops);
397 static sector_t erofs_bmap(struct address_space *mapping, sector_t block)
399 return iomap_bmap(mapping, block, &erofs_iomap_ops);
402 static ssize_t erofs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
404 struct inode *inode = file_inode(iocb->ki_filp);
406 /* no need taking (shared) inode lock since it's a ro filesystem */
407 if (!iov_iter_count(to))
412 return dax_iomap_rw(iocb, to, &erofs_iomap_ops);
414 if (iocb->ki_flags & IOCB_DIRECT) {
415 struct block_device *bdev = inode->i_sb->s_bdev;
416 unsigned int blksize_mask;
419 blksize_mask = bdev_logical_block_size(bdev) - 1;
421 blksize_mask = i_blocksize(inode) - 1;
423 if ((iocb->ki_pos | iov_iter_count(to) |
424 iov_iter_alignment(to)) & blksize_mask)
427 return iomap_dio_rw(iocb, to, &erofs_iomap_ops,
430 return filemap_read(iocb, to, 0);
433 /* for uncompressed (aligned) files and raw access for other files */
434 const struct address_space_operations erofs_aops = {
435 .read_folio = erofs_read_folio,
436 .readahead = erofs_readahead,
438 .direct_IO = noop_direct_IO,
439 .release_folio = iomap_release_folio,
440 .invalidate_folio = iomap_invalidate_folio,
444 static vm_fault_t erofs_dax_huge_fault(struct vm_fault *vmf,
447 return dax_iomap_fault(vmf, order, NULL, NULL, &erofs_iomap_ops);
450 static vm_fault_t erofs_dax_fault(struct vm_fault *vmf)
452 return erofs_dax_huge_fault(vmf, 0);
455 static const struct vm_operations_struct erofs_dax_vm_ops = {
456 .fault = erofs_dax_fault,
457 .huge_fault = erofs_dax_huge_fault,
460 static int erofs_file_mmap(struct file *file, struct vm_area_struct *vma)
462 if (!IS_DAX(file_inode(file)))
463 return generic_file_readonly_mmap(file, vma);
465 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
468 vma->vm_ops = &erofs_dax_vm_ops;
469 vm_flags_set(vma, VM_HUGEPAGE);
473 #define erofs_file_mmap generic_file_readonly_mmap
476 const struct file_operations erofs_file_fops = {
477 .llseek = generic_file_llseek,
478 .read_iter = erofs_file_read_iter,
479 .mmap = erofs_file_mmap,
480 .get_unmapped_area = thp_get_unmapped_area,
481 .splice_read = filemap_splice_read,