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);
30 void *erofs_bread(struct erofs_buf *buf, struct inode *inode,
31 erofs_blk_t blkaddr, enum erofs_kmap_type type)
33 struct address_space *const mapping = inode->i_mapping;
34 erofs_off_t offset = blknr_to_addr(blkaddr);
35 pgoff_t index = offset >> PAGE_SHIFT;
36 struct page *page = buf->page;
38 unsigned int nofs_flag;
40 if (!page || page->index != index) {
41 erofs_put_metabuf(buf);
43 nofs_flag = memalloc_nofs_save();
44 folio = read_cache_folio(mapping, index, NULL, NULL);
45 memalloc_nofs_restore(nofs_flag);
49 /* should already be PageUptodate, no need to lock page */
50 page = folio_file_page(folio, index);
53 if (buf->kmap_type == EROFS_NO_KMAP) {
54 if (type == EROFS_KMAP)
55 buf->base = kmap_local_page(page);
56 buf->kmap_type = type;
57 } else if (buf->kmap_type != type) {
59 return ERR_PTR(-EFAULT);
61 if (type == EROFS_NO_KMAP)
63 return buf->base + (offset & ~PAGE_MASK);
66 void *erofs_read_metabuf(struct erofs_buf *buf, struct super_block *sb,
67 erofs_blk_t blkaddr, enum erofs_kmap_type type)
69 if (erofs_is_fscache_mode(sb))
70 return erofs_bread(buf, EROFS_SB(sb)->s_fscache->inode,
73 return erofs_bread(buf, sb->s_bdev->bd_inode, blkaddr, type);
76 static int erofs_map_blocks_flatmode(struct inode *inode,
77 struct erofs_map_blocks *map)
79 erofs_blk_t nblocks, lastblk;
80 u64 offset = map->m_la;
81 struct erofs_inode *vi = EROFS_I(inode);
82 bool tailendpacking = (vi->datalayout == EROFS_INODE_FLAT_INLINE);
84 nblocks = DIV_ROUND_UP(inode->i_size, EROFS_BLKSIZ);
85 lastblk = nblocks - tailendpacking;
87 /* there is no hole in flatmode */
88 map->m_flags = EROFS_MAP_MAPPED;
89 if (offset < blknr_to_addr(lastblk)) {
90 map->m_pa = blknr_to_addr(vi->raw_blkaddr) + map->m_la;
91 map->m_plen = blknr_to_addr(lastblk) - offset;
92 } else if (tailendpacking) {
93 map->m_pa = erofs_iloc(inode) + vi->inode_isize +
94 vi->xattr_isize + erofs_blkoff(offset);
95 map->m_plen = inode->i_size - offset;
97 /* inline data should be located in the same meta block */
98 if (erofs_blkoff(map->m_pa) + map->m_plen > EROFS_BLKSIZ) {
99 erofs_err(inode->i_sb,
100 "inline data cross block boundary @ nid %llu",
103 return -EFSCORRUPTED;
105 map->m_flags |= EROFS_MAP_META;
107 erofs_err(inode->i_sb,
108 "internal error @ nid: %llu (size %llu), m_la 0x%llx",
109 vi->nid, inode->i_size, map->m_la);
116 int erofs_map_blocks(struct inode *inode, struct erofs_map_blocks *map)
118 struct super_block *sb = inode->i_sb;
119 struct erofs_inode *vi = EROFS_I(inode);
120 struct erofs_inode_chunk_index *idx;
121 struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
128 trace_erofs_map_blocks_enter(inode, map, 0);
130 if (map->m_la >= inode->i_size) {
131 /* leave out-of-bound access unmapped */
137 if (vi->datalayout != EROFS_INODE_CHUNK_BASED) {
138 err = erofs_map_blocks_flatmode(inode, map);
142 if (vi->chunkformat & EROFS_CHUNK_FORMAT_INDEXES)
143 unit = sizeof(*idx); /* chunk index */
145 unit = EROFS_BLOCK_MAP_ENTRY_SIZE; /* block map */
147 chunknr = map->m_la >> vi->chunkbits;
148 pos = ALIGN(erofs_iloc(inode) + vi->inode_isize +
149 vi->xattr_isize, unit) + unit * chunknr;
151 kaddr = erofs_read_metabuf(&buf, sb, erofs_blknr(pos), EROFS_KMAP);
153 err = PTR_ERR(kaddr);
156 map->m_la = chunknr << vi->chunkbits;
157 map->m_plen = min_t(erofs_off_t, 1UL << vi->chunkbits,
158 roundup(inode->i_size - map->m_la, EROFS_BLKSIZ));
160 /* handle block map */
161 if (!(vi->chunkformat & EROFS_CHUNK_FORMAT_INDEXES)) {
162 __le32 *blkaddr = kaddr + erofs_blkoff(pos);
164 if (le32_to_cpu(*blkaddr) == EROFS_NULL_ADDR) {
167 map->m_pa = blknr_to_addr(le32_to_cpu(*blkaddr));
168 map->m_flags = EROFS_MAP_MAPPED;
172 /* parse chunk indexes */
173 idx = kaddr + erofs_blkoff(pos);
174 switch (le32_to_cpu(idx->blkaddr)) {
175 case EROFS_NULL_ADDR:
179 map->m_deviceid = le16_to_cpu(idx->device_id) &
180 EROFS_SB(sb)->device_id_mask;
181 map->m_pa = blknr_to_addr(le32_to_cpu(idx->blkaddr));
182 map->m_flags = EROFS_MAP_MAPPED;
186 erofs_put_metabuf(&buf);
189 map->m_llen = map->m_plen;
190 trace_erofs_map_blocks_exit(inode, map, 0, err);
194 int erofs_map_dev(struct super_block *sb, struct erofs_map_dev *map)
196 struct erofs_dev_context *devs = EROFS_SB(sb)->devs;
197 struct erofs_device_info *dif;
200 /* primary device by default */
201 map->m_bdev = sb->s_bdev;
202 map->m_daxdev = EROFS_SB(sb)->dax_dev;
203 map->m_dax_part_off = EROFS_SB(sb)->dax_part_off;
204 map->m_fscache = EROFS_SB(sb)->s_fscache;
206 if (map->m_deviceid) {
207 down_read(&devs->rwsem);
208 dif = idr_find(&devs->tree, map->m_deviceid - 1);
210 up_read(&devs->rwsem);
213 map->m_bdev = dif->bdev;
214 map->m_daxdev = dif->dax_dev;
215 map->m_dax_part_off = dif->dax_part_off;
216 map->m_fscache = dif->fscache;
217 up_read(&devs->rwsem);
218 } else if (devs->extra_devices) {
219 down_read(&devs->rwsem);
220 idr_for_each_entry(&devs->tree, dif, id) {
221 erofs_off_t startoff, length;
223 if (!dif->mapped_blkaddr)
225 startoff = blknr_to_addr(dif->mapped_blkaddr);
226 length = blknr_to_addr(dif->blocks);
228 if (map->m_pa >= startoff &&
229 map->m_pa < startoff + length) {
230 map->m_pa -= startoff;
231 map->m_bdev = dif->bdev;
232 map->m_daxdev = dif->dax_dev;
233 map->m_dax_part_off = dif->dax_part_off;
234 map->m_fscache = dif->fscache;
238 up_read(&devs->rwsem);
243 static int erofs_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
244 unsigned int flags, struct iomap *iomap, struct iomap *srcmap)
247 struct erofs_map_blocks map;
248 struct erofs_map_dev mdev;
253 ret = erofs_map_blocks(inode, &map);
257 mdev = (struct erofs_map_dev) {
258 .m_deviceid = map.m_deviceid,
261 ret = erofs_map_dev(inode->i_sb, &mdev);
265 iomap->offset = map.m_la;
266 if (flags & IOMAP_DAX)
267 iomap->dax_dev = mdev.m_daxdev;
269 iomap->bdev = mdev.m_bdev;
270 iomap->length = map.m_llen;
272 iomap->private = NULL;
274 if (!(map.m_flags & EROFS_MAP_MAPPED)) {
275 iomap->type = IOMAP_HOLE;
276 iomap->addr = IOMAP_NULL_ADDR;
278 iomap->length = length;
282 if (map.m_flags & EROFS_MAP_META) {
284 struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
286 iomap->type = IOMAP_INLINE;
287 ptr = erofs_read_metabuf(&buf, inode->i_sb,
288 erofs_blknr(mdev.m_pa), EROFS_KMAP);
291 iomap->inline_data = ptr + erofs_blkoff(mdev.m_pa);
292 iomap->private = buf.base;
294 iomap->type = IOMAP_MAPPED;
295 iomap->addr = mdev.m_pa;
296 if (flags & IOMAP_DAX)
297 iomap->addr += mdev.m_dax_part_off;
302 static int erofs_iomap_end(struct inode *inode, loff_t pos, loff_t length,
303 ssize_t written, unsigned int flags, struct iomap *iomap)
305 void *ptr = iomap->private;
308 struct erofs_buf buf = {
309 .page = kmap_to_page(ptr),
311 .kmap_type = EROFS_KMAP,
314 DBG_BUGON(iomap->type != IOMAP_INLINE);
315 erofs_put_metabuf(&buf);
317 DBG_BUGON(iomap->type == IOMAP_INLINE);
322 static const struct iomap_ops erofs_iomap_ops = {
323 .iomap_begin = erofs_iomap_begin,
324 .iomap_end = erofs_iomap_end,
327 int erofs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
330 if (erofs_inode_is_data_compressed(EROFS_I(inode)->datalayout)) {
331 #ifdef CONFIG_EROFS_FS_ZIP
332 return iomap_fiemap(inode, fieinfo, start, len,
333 &z_erofs_iomap_report_ops);
338 return iomap_fiemap(inode, fieinfo, start, len, &erofs_iomap_ops);
342 * since we dont have write or truncate flows, so no inode
343 * locking needs to be held at the moment.
345 static int erofs_read_folio(struct file *file, struct folio *folio)
347 return iomap_read_folio(folio, &erofs_iomap_ops);
350 static void erofs_readahead(struct readahead_control *rac)
352 return iomap_readahead(rac, &erofs_iomap_ops);
355 static sector_t erofs_bmap(struct address_space *mapping, sector_t block)
357 return iomap_bmap(mapping, block, &erofs_iomap_ops);
360 static ssize_t erofs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
362 struct inode *inode = file_inode(iocb->ki_filp);
364 /* no need taking (shared) inode lock since it's a ro filesystem */
365 if (!iov_iter_count(to))
370 return dax_iomap_rw(iocb, to, &erofs_iomap_ops);
372 if (iocb->ki_flags & IOCB_DIRECT) {
373 struct block_device *bdev = inode->i_sb->s_bdev;
374 unsigned int blksize_mask;
377 blksize_mask = bdev_logical_block_size(bdev) - 1;
379 blksize_mask = i_blocksize(inode) - 1;
381 if ((iocb->ki_pos | iov_iter_count(to) |
382 iov_iter_alignment(to)) & blksize_mask)
385 return iomap_dio_rw(iocb, to, &erofs_iomap_ops,
388 return filemap_read(iocb, to, 0);
391 /* for uncompressed (aligned) files and raw access for other files */
392 const struct address_space_operations erofs_raw_access_aops = {
393 .read_folio = erofs_read_folio,
394 .readahead = erofs_readahead,
396 .direct_IO = noop_direct_IO,
397 .release_folio = iomap_release_folio,
398 .invalidate_folio = iomap_invalidate_folio,
402 static vm_fault_t erofs_dax_huge_fault(struct vm_fault *vmf,
403 enum page_entry_size pe_size)
405 return dax_iomap_fault(vmf, pe_size, NULL, NULL, &erofs_iomap_ops);
408 static vm_fault_t erofs_dax_fault(struct vm_fault *vmf)
410 return erofs_dax_huge_fault(vmf, PE_SIZE_PTE);
413 static const struct vm_operations_struct erofs_dax_vm_ops = {
414 .fault = erofs_dax_fault,
415 .huge_fault = erofs_dax_huge_fault,
418 static int erofs_file_mmap(struct file *file, struct vm_area_struct *vma)
420 if (!IS_DAX(file_inode(file)))
421 return generic_file_readonly_mmap(file, vma);
423 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
426 vma->vm_ops = &erofs_dax_vm_ops;
427 vm_flags_set(vma, VM_HUGEPAGE);
431 #define erofs_file_mmap generic_file_readonly_mmap
434 const struct file_operations erofs_file_fops = {
435 .llseek = generic_file_llseek,
436 .read_iter = erofs_file_read_iter,
437 .mmap = erofs_file_mmap,
438 .splice_read = generic_file_splice_read,