Commit 4fe71e88 authored by Chao Yu's avatar Chao Yu Committed by Jaegeuk Kim

f2fs: simplify f2fs_map_blocks

In f2fs_map_blocks, we use duplicated codes to handle first block mapping
and the following blocks mapping, it's unnecessary. This patch simplifies
f2fs_map_blocks to avoid using copied codes.
Signed-off-by: default avatarChao Yu <chao2.yu@samsung.com>
Signed-off-by: default avatarJaegeuk Kim <jaegeuk@kernel.org>
parent 8f1dbbbb
...@@ -631,6 +631,7 @@ int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map, ...@@ -631,6 +631,7 @@ int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
goto out; goto out;
} }
next_dnode:
if (create) if (create)
f2fs_lock_op(sbi); f2fs_lock_op(sbi);
...@@ -643,47 +644,57 @@ int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map, ...@@ -643,47 +644,57 @@ int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
goto unlock_out; goto unlock_out;
} }
if (dn.data_blkaddr == NEW_ADDR || dn.data_blkaddr == NULL_ADDR) { end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
next_block:
blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR) {
if (create) { if (create) {
if (unlikely(f2fs_cp_error(sbi))) { if (unlikely(f2fs_cp_error(sbi))) {
err = -EIO; err = -EIO;
goto put_out; goto sync_out;
} }
err = __allocate_data_block(&dn); err = __allocate_data_block(&dn);
if (err) if (err)
goto put_out; goto sync_out;
allocated = true; allocated = true;
map->m_flags = F2FS_MAP_NEW; map->m_flags = F2FS_MAP_NEW;
blkaddr = dn.data_blkaddr;
} else { } else {
if (flag != F2FS_GET_BLOCK_FIEMAP || if (flag != F2FS_GET_BLOCK_FIEMAP ||
dn.data_blkaddr != NEW_ADDR) { blkaddr != NEW_ADDR) {
if (flag == F2FS_GET_BLOCK_BMAP) if (flag == F2FS_GET_BLOCK_BMAP)
err = -ENOENT; err = -ENOENT;
goto put_out; goto sync_out;
} }
/*
* preallocated unwritten block should be mapped
* for fiemap.
*/
if (dn.data_blkaddr == NEW_ADDR)
map->m_flags = F2FS_MAP_UNWRITTEN;
} }
} }
map->m_flags |= F2FS_MAP_MAPPED; if (map->m_len == 0) {
map->m_pblk = dn.data_blkaddr; /* preallocated unwritten block should be mapped for fiemap. */
map->m_len = 1; if (blkaddr == NEW_ADDR)
map->m_flags |= F2FS_MAP_UNWRITTEN;
map->m_flags |= F2FS_MAP_MAPPED;
map->m_pblk = blkaddr;
map->m_len = 1;
} else if ((map->m_pblk != NEW_ADDR &&
blkaddr == (map->m_pblk + ofs)) ||
(map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR)) {
ofs++;
map->m_len++;
} else {
goto sync_out;
}
end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
dn.ofs_in_node++; dn.ofs_in_node++;
pgofs++; pgofs++;
get_next: if (map->m_len < maxblocks) {
if (map->m_len >= maxblocks) if (dn.ofs_in_node < end_offset)
goto sync_out; goto next_block;
if (dn.ofs_in_node >= end_offset) {
if (allocated) if (allocated)
sync_inode_page(&dn); sync_inode_page(&dn);
f2fs_put_dnode(&dn); f2fs_put_dnode(&dn);
...@@ -691,62 +702,14 @@ int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map, ...@@ -691,62 +702,14 @@ int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
if (create) { if (create) {
f2fs_unlock_op(sbi); f2fs_unlock_op(sbi);
f2fs_balance_fs(sbi, allocated); f2fs_balance_fs(sbi, allocated);
f2fs_lock_op(sbi);
} }
allocated = false; allocated = false;
goto next_dnode;
set_new_dnode(&dn, inode, NULL, NULL, 0);
err = get_dnode_of_data(&dn, pgofs, mode);
if (err) {
if (err == -ENOENT)
err = 0;
goto unlock_out;
}
end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
}
blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR) {
if (create) {
if (unlikely(f2fs_cp_error(sbi))) {
err = -EIO;
goto sync_out;
}
err = __allocate_data_block(&dn);
if (err)
goto sync_out;
allocated = true;
map->m_flags |= F2FS_MAP_NEW;
blkaddr = dn.data_blkaddr;
} else {
/*
* we only merge preallocated unwritten blocks
* for fiemap.
*/
if (flag != F2FS_GET_BLOCK_FIEMAP ||
blkaddr != NEW_ADDR)
goto sync_out;
}
}
/* Give more consecutive addresses for the readahead */
if ((map->m_pblk != NEW_ADDR &&
blkaddr == (map->m_pblk + ofs)) ||
(map->m_pblk == NEW_ADDR &&
blkaddr == NEW_ADDR)) {
ofs++;
dn.ofs_in_node++;
pgofs++;
map->m_len++;
goto get_next;
} }
sync_out: sync_out:
if (allocated) if (allocated)
sync_inode_page(&dn); sync_inode_page(&dn);
put_out:
f2fs_put_dnode(&dn); f2fs_put_dnode(&dn);
unlock_out: unlock_out:
if (create) { if (create) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment