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)
17 else if (buf->kmap_type == EROFS_KMAP_ATOMIC)
18 kunmap_atomic(buf->base);
20 buf->kmap_type = EROFS_NO_KMAP;
23 void erofs_put_metabuf(struct erofs_buf *buf)
27 erofs_unmap_metabuf(buf);
32 void *erofs_bread(struct erofs_buf *buf, struct inode *inode,
33 erofs_blk_t blkaddr, enum erofs_kmap_type type)
35 struct address_space *const mapping = inode->i_mapping;
36 erofs_off_t offset = blknr_to_addr(blkaddr);
37 pgoff_t index = offset >> PAGE_SHIFT;
38 struct page *page = buf->page;
40 unsigned int nofs_flag;
42 if (!page || page->index != index) {
43 erofs_put_metabuf(buf);
45 nofs_flag = memalloc_nofs_save();
46 folio = read_cache_folio(mapping, index, NULL, NULL);
47 memalloc_nofs_restore(nofs_flag);
51 /* should already be PageUptodate, no need to lock page */
52 page = folio_file_page(folio, index);
55 if (buf->kmap_type == EROFS_NO_KMAP) {
56 if (type == EROFS_KMAP)
57 buf->base = kmap(page);
58 else if (type == EROFS_KMAP_ATOMIC)
59 buf->base = kmap_atomic(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_read_metabuf(struct erofs_buf *buf, struct super_block *sb,
71 erofs_blk_t blkaddr, enum erofs_kmap_type type)
73 if (erofs_is_fscache_mode(sb))
74 return erofs_bread(buf, EROFS_SB(sb)->s_fscache->inode,
77 return erofs_bread(buf, sb->s_bdev->bd_inode, blkaddr, type);
80 static int erofs_map_blocks_flatmode(struct inode *inode,
81 struct erofs_map_blocks *map,
84 erofs_blk_t nblocks, lastblk;
85 u64 offset = map->m_la;
86 struct erofs_inode *vi = EROFS_I(inode);
87 bool tailendpacking = (vi->datalayout == EROFS_INODE_FLAT_INLINE);
89 nblocks = DIV_ROUND_UP(inode->i_size, EROFS_BLKSIZ);
90 lastblk = nblocks - tailendpacking;
92 /* there is no hole in flatmode */
93 map->m_flags = EROFS_MAP_MAPPED;
94 if (offset < blknr_to_addr(lastblk)) {
95 map->m_pa = blknr_to_addr(vi->raw_blkaddr) + map->m_la;
96 map->m_plen = blknr_to_addr(lastblk) - offset;
97 } else if (tailendpacking) {
98 /* 2 - inode inline B: inode, [xattrs], inline last blk... */
99 struct erofs_sb_info *sbi = EROFS_SB(inode->i_sb);
101 map->m_pa = iloc(sbi, vi->nid) + vi->inode_isize +
102 vi->xattr_isize + erofs_blkoff(map->m_la);
103 map->m_plen = inode->i_size - offset;
105 /* inline data should be located in the same meta block */
106 if (erofs_blkoff(map->m_pa) + map->m_plen > EROFS_BLKSIZ) {
107 erofs_err(inode->i_sb,
108 "inline data cross block boundary @ nid %llu",
111 return -EFSCORRUPTED;
113 map->m_flags |= EROFS_MAP_META;
115 erofs_err(inode->i_sb,
116 "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,
125 struct erofs_map_blocks *map, int flags)
127 struct super_block *sb = inode->i_sb;
128 struct erofs_inode *vi = EROFS_I(inode);
129 struct erofs_inode_chunk_index *idx;
130 struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
137 trace_erofs_map_blocks_enter(inode, map, flags);
139 if (map->m_la >= inode->i_size) {
140 /* leave out-of-bound access unmapped */
146 if (vi->datalayout != EROFS_INODE_CHUNK_BASED) {
147 err = erofs_map_blocks_flatmode(inode, map, flags);
151 if (vi->chunkformat & EROFS_CHUNK_FORMAT_INDEXES)
152 unit = sizeof(*idx); /* chunk index */
154 unit = EROFS_BLOCK_MAP_ENTRY_SIZE; /* block map */
156 chunknr = map->m_la >> vi->chunkbits;
157 pos = ALIGN(iloc(EROFS_SB(sb), vi->nid) + vi->inode_isize +
158 vi->xattr_isize, unit) + unit * chunknr;
160 kaddr = erofs_read_metabuf(&buf, sb, erofs_blknr(pos), EROFS_KMAP);
162 err = PTR_ERR(kaddr);
165 map->m_la = chunknr << vi->chunkbits;
166 map->m_plen = min_t(erofs_off_t, 1UL << vi->chunkbits,
167 roundup(inode->i_size - map->m_la, EROFS_BLKSIZ));
169 /* handle block map */
170 if (!(vi->chunkformat & EROFS_CHUNK_FORMAT_INDEXES)) {
171 __le32 *blkaddr = kaddr + erofs_blkoff(pos);
173 if (le32_to_cpu(*blkaddr) == EROFS_NULL_ADDR) {
176 map->m_pa = blknr_to_addr(le32_to_cpu(*blkaddr));
177 map->m_flags = EROFS_MAP_MAPPED;
181 /* parse chunk indexes */
182 idx = kaddr + erofs_blkoff(pos);
183 switch (le32_to_cpu(idx->blkaddr)) {
184 case EROFS_NULL_ADDR:
188 map->m_deviceid = le16_to_cpu(idx->device_id) &
189 EROFS_SB(sb)->device_id_mask;
190 map->m_pa = blknr_to_addr(le32_to_cpu(idx->blkaddr));
191 map->m_flags = EROFS_MAP_MAPPED;
195 erofs_put_metabuf(&buf);
198 map->m_llen = map->m_plen;
199 trace_erofs_map_blocks_exit(inode, map, flags, 0);
203 int erofs_map_dev(struct super_block *sb, struct erofs_map_dev *map)
205 struct erofs_dev_context *devs = EROFS_SB(sb)->devs;
206 struct erofs_device_info *dif;
209 /* primary device by default */
210 map->m_bdev = sb->s_bdev;
211 map->m_daxdev = EROFS_SB(sb)->dax_dev;
212 map->m_dax_part_off = EROFS_SB(sb)->dax_part_off;
213 map->m_fscache = EROFS_SB(sb)->s_fscache;
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);
222 map->m_bdev = dif->bdev;
223 map->m_daxdev = dif->dax_dev;
224 map->m_dax_part_off = dif->dax_part_off;
225 map->m_fscache = dif->fscache;
226 up_read(&devs->rwsem);
227 } else if (devs->extra_devices) {
228 down_read(&devs->rwsem);
229 idr_for_each_entry(&devs->tree, dif, id) {
230 erofs_off_t startoff, length;
232 if (!dif->mapped_blkaddr)
234 startoff = blknr_to_addr(dif->mapped_blkaddr);
235 length = blknr_to_addr(dif->blocks);
237 if (map->m_pa >= startoff &&
238 map->m_pa < startoff + length) {
239 map->m_pa -= startoff;
240 map->m_bdev = dif->bdev;
241 map->m_daxdev = dif->dax_dev;
242 map->m_dax_part_off = dif->dax_part_off;
243 map->m_fscache = dif->fscache;
247 up_read(&devs->rwsem);
252 static int erofs_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
253 unsigned int flags, struct iomap *iomap, struct iomap *srcmap)
256 struct erofs_map_blocks map;
257 struct erofs_map_dev mdev;
262 ret = erofs_map_blocks(inode, &map, EROFS_GET_BLOCKS_RAW);
266 mdev = (struct erofs_map_dev) {
267 .m_deviceid = map.m_deviceid,
270 ret = erofs_map_dev(inode->i_sb, &mdev);
274 iomap->offset = map.m_la;
275 if (flags & IOMAP_DAX)
276 iomap->dax_dev = mdev.m_daxdev;
278 iomap->bdev = mdev.m_bdev;
279 iomap->length = map.m_llen;
281 iomap->private = NULL;
283 if (!(map.m_flags & EROFS_MAP_MAPPED)) {
284 iomap->type = IOMAP_HOLE;
285 iomap->addr = IOMAP_NULL_ADDR;
287 iomap->length = length;
291 if (map.m_flags & EROFS_MAP_META) {
293 struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
295 iomap->type = IOMAP_INLINE;
296 ptr = erofs_read_metabuf(&buf, inode->i_sb,
297 erofs_blknr(mdev.m_pa), EROFS_KMAP);
300 iomap->inline_data = ptr + erofs_blkoff(mdev.m_pa);
301 iomap->private = buf.base;
303 iomap->type = IOMAP_MAPPED;
304 iomap->addr = mdev.m_pa;
305 if (flags & IOMAP_DAX)
306 iomap->addr += mdev.m_dax_part_off;
311 static int erofs_iomap_end(struct inode *inode, loff_t pos, loff_t length,
312 ssize_t written, unsigned int flags, struct iomap *iomap)
314 void *ptr = iomap->private;
317 struct erofs_buf buf = {
318 .page = kmap_to_page(ptr),
320 .kmap_type = EROFS_KMAP,
323 DBG_BUGON(iomap->type != IOMAP_INLINE);
324 erofs_put_metabuf(&buf);
326 DBG_BUGON(iomap->type == IOMAP_INLINE);
331 static const struct iomap_ops erofs_iomap_ops = {
332 .iomap_begin = erofs_iomap_begin,
333 .iomap_end = erofs_iomap_end,
336 int erofs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
339 if (erofs_inode_is_data_compressed(EROFS_I(inode)->datalayout)) {
340 #ifdef CONFIG_EROFS_FS_ZIP
341 return iomap_fiemap(inode, fieinfo, start, len,
342 &z_erofs_iomap_report_ops);
347 return iomap_fiemap(inode, fieinfo, start, len, &erofs_iomap_ops);
351 * since we dont have write or truncate flows, so no inode
352 * locking needs to be held at the moment.
354 static int erofs_read_folio(struct file *file, struct folio *folio)
356 return iomap_read_folio(folio, &erofs_iomap_ops);
359 static void erofs_readahead(struct readahead_control *rac)
361 return iomap_readahead(rac, &erofs_iomap_ops);
364 static sector_t erofs_bmap(struct address_space *mapping, sector_t block)
366 return iomap_bmap(mapping, block, &erofs_iomap_ops);
369 static ssize_t erofs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
371 struct inode *inode = file_inode(iocb->ki_filp);
373 /* no need taking (shared) inode lock since it's a ro filesystem */
374 if (!iov_iter_count(to))
379 return dax_iomap_rw(iocb, to, &erofs_iomap_ops);
381 if (iocb->ki_flags & IOCB_DIRECT) {
382 struct block_device *bdev = inode->i_sb->s_bdev;
383 unsigned int blksize_mask;
386 blksize_mask = bdev_logical_block_size(bdev) - 1;
388 blksize_mask = (1 << inode->i_blkbits) - 1;
390 if ((iocb->ki_pos | iov_iter_count(to) |
391 iov_iter_alignment(to)) & blksize_mask)
394 return iomap_dio_rw(iocb, to, &erofs_iomap_ops,
397 return filemap_read(iocb, to, 0);
400 /* for uncompressed (aligned) files and raw access for other files */
401 const struct address_space_operations erofs_raw_access_aops = {
402 .read_folio = erofs_read_folio,
403 .readahead = erofs_readahead,
405 .direct_IO = noop_direct_IO,
409 static vm_fault_t erofs_dax_huge_fault(struct vm_fault *vmf,
410 enum page_entry_size pe_size)
412 return dax_iomap_fault(vmf, pe_size, NULL, NULL, &erofs_iomap_ops);
415 static vm_fault_t erofs_dax_fault(struct vm_fault *vmf)
417 return erofs_dax_huge_fault(vmf, PE_SIZE_PTE);
420 static const struct vm_operations_struct erofs_dax_vm_ops = {
421 .fault = erofs_dax_fault,
422 .huge_fault = erofs_dax_huge_fault,
425 static int erofs_file_mmap(struct file *file, struct vm_area_struct *vma)
427 if (!IS_DAX(file_inode(file)))
428 return generic_file_readonly_mmap(file, vma);
430 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
433 vma->vm_ops = &erofs_dax_vm_ops;
434 vma->vm_flags |= VM_HUGEPAGE;
438 #define erofs_file_mmap generic_file_readonly_mmap
441 const struct file_operations erofs_file_fops = {
442 .llseek = generic_file_llseek,
443 .read_iter = erofs_file_read_iter,
444 .mmap = erofs_file_mmap,
445 .splice_read = generic_file_splice_read,