It's simple enough to be folded into erofs_map_blocks().

Signed-off-by: Gao Xiang <hsiang...@linux.alibaba.com>
---
 fs/erofs/data.c | 117 +++++++++++++++++++-----------------------------
 1 file changed, 47 insertions(+), 70 deletions(-)

diff --git a/fs/erofs/data.c b/fs/erofs/data.c
index 1d2cb0fa1baf..2f45e39ce8c7 100644
--- a/fs/erofs/data.c
+++ b/fs/erofs/data.c
@@ -70,58 +70,39 @@ void *erofs_read_metabuf(struct erofs_buf *buf, struct 
super_block *sb,
        return erofs_bread(buf, offset, need_kmap);
 }
 
-static int erofs_map_blocks_flatmode(struct inode *inode,
-                                    struct erofs_map_blocks *map)
-{
-       struct erofs_inode *vi = EROFS_I(inode);
-       struct super_block *sb = inode->i_sb;
-       bool tailendpacking = (vi->datalayout == EROFS_INODE_FLAT_INLINE);
-       erofs_blk_t lastblk = erofs_iblks(inode) - tailendpacking;
-
-       map->m_flags = EROFS_MAP_MAPPED;        /* no hole in flat inodes */
-       if (map->m_la < erofs_pos(sb, lastblk)) {
-               map->m_pa = erofs_pos(sb, vi->raw_blkaddr) + map->m_la;
-               map->m_plen = erofs_pos(sb, lastblk) - map->m_la;
-       } else {
-               DBG_BUGON(!tailendpacking);
-               map->m_pa = erofs_iloc(inode) + vi->inode_isize +
-                       vi->xattr_isize + erofs_blkoff(sb, map->m_la);
-               map->m_plen = inode->i_size - map->m_la;
-
-               /* inline data should be located in the same meta block */
-               if (erofs_blkoff(sb, map->m_pa) + map->m_plen > 
sb->s_blocksize) {
-                       erofs_err(sb, "inline data across blocks @ nid %llu", 
vi->nid);
-                       DBG_BUGON(1);
-                       return -EFSCORRUPTED;
-               }
-               map->m_flags |= EROFS_MAP_META;
-       }
-       return 0;
-}
-
 int erofs_map_blocks(struct inode *inode, struct erofs_map_blocks *map)
 {
+       struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
        struct super_block *sb = inode->i_sb;
+       unsigned int unit, blksz = sb->s_blocksize;
        struct erofs_inode *vi = EROFS_I(inode);
        struct erofs_inode_chunk_index *idx;
-       struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
-       u64 chunknr;
-       unsigned int unit;
+       erofs_blk_t startblk;
+       bool tailpacking;
        erofs_off_t pos;
-       void *kaddr;
+       u64 chunknr;
        int err = 0;
 
        trace_erofs_map_blocks_enter(inode, map, 0);
        map->m_deviceid = 0;
-       if (map->m_la >= inode->i_size) {
-               /* leave out-of-bound access unmapped */
-               map->m_flags = 0;
-               map->m_plen = map->m_llen;
+       map->m_flags = 0;
+       if (map->m_la >= inode->i_size)
                goto out;
-       }
 
        if (vi->datalayout != EROFS_INODE_CHUNK_BASED) {
-               err = erofs_map_blocks_flatmode(inode, map);
+               tailpacking = (vi->datalayout == EROFS_INODE_FLAT_INLINE);
+               pos = erofs_pos(sb, erofs_iblks(inode) - tailpacking);
+
+               map->m_flags = EROFS_MAP_MAPPED;
+               if (map->m_la < pos) {
+                       map->m_pa = erofs_pos(sb, vi->raw_blkaddr) + map->m_la;
+                       map->m_llen = pos - map->m_la;
+               } else {
+                       map->m_pa = erofs_iloc(inode) + vi->inode_isize +
+                               vi->xattr_isize + erofs_blkoff(sb, map->m_la);
+                       map->m_llen = inode->i_size - map->m_la;
+                       map->m_flags |= EROFS_MAP_META;
+               }
                goto out;
        }
 
@@ -134,45 +115,41 @@ int erofs_map_blocks(struct inode *inode, struct 
erofs_map_blocks *map)
        pos = ALIGN(erofs_iloc(inode) + vi->inode_isize +
                    vi->xattr_isize, unit) + unit * chunknr;
 
-       kaddr = erofs_read_metabuf(&buf, sb, pos, true);
-       if (IS_ERR(kaddr)) {
-               err = PTR_ERR(kaddr);
+       idx = erofs_read_metabuf(&buf, sb, pos, true);
+       if (IS_ERR(idx)) {
+               err = PTR_ERR(idx);
                goto out;
        }
        map->m_la = chunknr << vi->chunkbits;
-       map->m_plen = min_t(erofs_off_t, 1UL << vi->chunkbits,
-                       round_up(inode->i_size - map->m_la, sb->s_blocksize));
-
-       /* handle block map */
-       if (!(vi->chunkformat & EROFS_CHUNK_FORMAT_INDEXES)) {
-               __le32 *blkaddr = kaddr;
-
-               if (le32_to_cpu(*blkaddr) == EROFS_NULL_ADDR) {
-                       map->m_flags = 0;
-               } else {
-                       map->m_pa = erofs_pos(sb, le32_to_cpu(*blkaddr));
+       map->m_llen = min_t(erofs_off_t, 1UL << vi->chunkbits,
+                           round_up(inode->i_size - map->m_la, blksz));
+       if (vi->chunkformat & EROFS_CHUNK_FORMAT_INDEXES) {
+               startblk = le32_to_cpu(idx->blkaddr);
+               if (startblk != EROFS_NULL_ADDR) {
+                       map->m_deviceid = le16_to_cpu(idx->device_id) &
+                               EROFS_SB(sb)->device_id_mask;
+                       map->m_pa = erofs_pos(sb, startblk);
+                       map->m_flags = EROFS_MAP_MAPPED;
+               }
+       } else {
+               startblk = le32_to_cpu(*(__le32 *)idx);
+               if (startblk != EROFS_NULL_ADDR) {
+                       map->m_pa = erofs_pos(sb, startblk);
                        map->m_flags = EROFS_MAP_MAPPED;
                }
-               goto out_unlock;
-       }
-       /* parse chunk indexes */
-       idx = kaddr;
-       switch (le32_to_cpu(idx->blkaddr)) {
-       case EROFS_NULL_ADDR:
-               map->m_flags = 0;
-               break;
-       default:
-               map->m_deviceid = le16_to_cpu(idx->device_id) &
-                       EROFS_SB(sb)->device_id_mask;
-               map->m_pa = erofs_pos(sb, le32_to_cpu(idx->blkaddr));
-               map->m_flags = EROFS_MAP_MAPPED;
-               break;
        }
-out_unlock:
        erofs_put_metabuf(&buf);
 out:
-       if (!err)
-               map->m_llen = map->m_plen;
+       if (!err) {
+               map->m_plen = map->m_llen;
+               /* inline data should be located in the same meta block */
+               if ((map->m_flags & EROFS_MAP_META) &&
+                   erofs_blkoff(sb, map->m_pa) + map->m_plen > blksz) {
+                       erofs_err(sb, "inline data across blocks @ nid %llu", 
vi->nid);
+                       DBG_BUGON(1);
+                       return -EFSCORRUPTED;
+               }
+       }
        trace_erofs_map_blocks_exit(inode, map, 0, err);
        return err;
 }
-- 
2.43.5


Reply via email to