Commit e35fd660 authored by Theodore Ts'o's avatar Theodore Ts'o

ext4: Add new abstraction ext4_map_blocks() underneath ext4_get_blocks()

Jack up ext4_get_blocks() and add a new function, ext4_map_blocks()
which uses a much smaller structure, struct ext4_map_blocks which is
20 bytes, as opposed to a struct buffer_head, which nearly 5 times
bigger on an x86_64 machine.  By switching things to use
ext4_map_blocks(), we can save stack space by using ext4_map_blocks()
since we can avoid allocating a struct buffer_head on the stack.
Signed-off-by: default avatar"Theodore Ts'o" <tytso@mit.edu>
parent 8e48dcfb
...@@ -125,6 +125,29 @@ struct ext4_allocation_request { ...@@ -125,6 +125,29 @@ struct ext4_allocation_request {
unsigned int flags; unsigned int flags;
}; };
/*
* Logical to physical block mapping, used by ext4_map_blocks()
*
* This structure is used to pass requests into ext4_map_blocks() as
* well as to store the information returned by ext4_map_blocks(). It
* takes less room on the stack than a struct buffer_head.
*/
#define EXT4_MAP_NEW (1 << BH_New)
#define EXT4_MAP_MAPPED (1 << BH_Mapped)
#define EXT4_MAP_UNWRITTEN (1 << BH_Unwritten)
#define EXT4_MAP_BOUNDARY (1 << BH_Boundary)
#define EXT4_MAP_UNINIT (1 << BH_Uninit)
#define EXT4_MAP_FLAGS (EXT4_MAP_NEW | EXT4_MAP_MAPPED |\
EXT4_MAP_UNWRITTEN | EXT4_MAP_BOUNDARY |\
EXT4_MAP_UNINIT)
struct ext4_map_blocks {
ext4_fsblk_t m_pblk;
ext4_lblk_t m_lblk;
unsigned int m_len;
unsigned int m_flags;
};
/* /*
* For delayed allocation tracking * For delayed allocation tracking
*/ */
...@@ -1773,9 +1796,8 @@ extern int ext4_ext_tree_init(handle_t *handle, struct inode *); ...@@ -1773,9 +1796,8 @@ extern int ext4_ext_tree_init(handle_t *handle, struct inode *);
extern int ext4_ext_writepage_trans_blocks(struct inode *, int); extern int ext4_ext_writepage_trans_blocks(struct inode *, int);
extern int ext4_ext_index_trans_blocks(struct inode *inode, int nrblocks, extern int ext4_ext_index_trans_blocks(struct inode *inode, int nrblocks,
int chunk); int chunk);
extern int ext4_ext_get_blocks(handle_t *handle, struct inode *inode, extern int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
ext4_lblk_t iblock, unsigned int max_blocks, struct ext4_map_blocks *map, int flags);
struct buffer_head *bh_result, int flags);
extern void ext4_ext_truncate(struct inode *); extern void ext4_ext_truncate(struct inode *);
extern void ext4_ext_init(struct super_block *); extern void ext4_ext_init(struct super_block *);
extern void ext4_ext_release(struct super_block *); extern void ext4_ext_release(struct super_block *);
...@@ -1783,6 +1805,8 @@ extern long ext4_fallocate(struct inode *inode, int mode, loff_t offset, ...@@ -1783,6 +1805,8 @@ extern long ext4_fallocate(struct inode *inode, int mode, loff_t offset,
loff_t len); loff_t len);
extern int ext4_convert_unwritten_extents(struct inode *inode, loff_t offset, extern int ext4_convert_unwritten_extents(struct inode *inode, loff_t offset,
ssize_t len); ssize_t len);
extern int ext4_map_blocks(handle_t *handle, struct inode *inode,
struct ext4_map_blocks *map, int flags);
extern int ext4_get_blocks(handle_t *handle, struct inode *inode, extern int ext4_get_blocks(handle_t *handle, struct inode *inode,
sector_t block, unsigned int max_blocks, sector_t block, unsigned int max_blocks,
struct buffer_head *bh, int flags); struct buffer_head *bh, int flags);
......
This diff is collapsed.
...@@ -149,7 +149,7 @@ int ext4_truncate_restart_trans(handle_t *handle, struct inode *inode, ...@@ -149,7 +149,7 @@ int ext4_truncate_restart_trans(handle_t *handle, struct inode *inode,
int ret; int ret;
/* /*
* Drop i_data_sem to avoid deadlock with ext4_get_blocks At this * Drop i_data_sem to avoid deadlock with ext4_map_blocks. At this
* moment, get_block can be called only for blocks inside i_size since * moment, get_block can be called only for blocks inside i_size since
* page cache has been already dropped and writes are blocked by * page cache has been already dropped and writes are blocked by
* i_mutex. So we can safely drop the i_data_sem here. * i_mutex. So we can safely drop the i_data_sem here.
...@@ -890,9 +890,9 @@ static int ext4_splice_branch(handle_t *handle, struct inode *inode, ...@@ -890,9 +890,9 @@ static int ext4_splice_branch(handle_t *handle, struct inode *inode,
} }
/* /*
* The ext4_ind_get_blocks() function handles non-extents inodes * The ext4_ind_map_blocks() function handles non-extents inodes
* (i.e., using the traditional indirect/double-indirect i_blocks * (i.e., using the traditional indirect/double-indirect i_blocks
* scheme) for ext4_get_blocks(). * scheme) for ext4_map_blocks().
* *
* Allocation strategy is simple: if we have to allocate something, we will * Allocation strategy is simple: if we have to allocate something, we will
* have to go the whole way to leaf. So let's do it before attaching anything * have to go the whole way to leaf. So let's do it before attaching anything
...@@ -917,9 +917,8 @@ static int ext4_splice_branch(handle_t *handle, struct inode *inode, ...@@ -917,9 +917,8 @@ static int ext4_splice_branch(handle_t *handle, struct inode *inode,
* down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system
* blocks. * blocks.
*/ */
static int ext4_ind_get_blocks(handle_t *handle, struct inode *inode, static int ext4_ind_map_blocks(handle_t *handle, struct inode *inode,
ext4_lblk_t iblock, unsigned int maxblocks, struct ext4_map_blocks *map,
struct buffer_head *bh_result,
int flags) int flags)
{ {
int err = -EIO; int err = -EIO;
...@@ -935,7 +934,7 @@ static int ext4_ind_get_blocks(handle_t *handle, struct inode *inode, ...@@ -935,7 +934,7 @@ static int ext4_ind_get_blocks(handle_t *handle, struct inode *inode,
J_ASSERT(!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)); J_ASSERT(!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL));
J_ASSERT(handle != NULL || (flags & EXT4_GET_BLOCKS_CREATE) == 0); J_ASSERT(handle != NULL || (flags & EXT4_GET_BLOCKS_CREATE) == 0);
depth = ext4_block_to_path(inode, iblock, offsets, depth = ext4_block_to_path(inode, map->m_lblk, offsets,
&blocks_to_boundary); &blocks_to_boundary);
if (depth == 0) if (depth == 0)
...@@ -946,10 +945,9 @@ static int ext4_ind_get_blocks(handle_t *handle, struct inode *inode, ...@@ -946,10 +945,9 @@ static int ext4_ind_get_blocks(handle_t *handle, struct inode *inode,
/* Simplest case - block found, no allocation needed */ /* Simplest case - block found, no allocation needed */
if (!partial) { if (!partial) {
first_block = le32_to_cpu(chain[depth - 1].key); first_block = le32_to_cpu(chain[depth - 1].key);
clear_buffer_new(bh_result);
count++; count++;
/*map more blocks*/ /*map more blocks*/
while (count < maxblocks && count <= blocks_to_boundary) { while (count < map->m_len && count <= blocks_to_boundary) {
ext4_fsblk_t blk; ext4_fsblk_t blk;
blk = le32_to_cpu(*(chain[depth-1].p + count)); blk = le32_to_cpu(*(chain[depth-1].p + count));
...@@ -969,7 +967,7 @@ static int ext4_ind_get_blocks(handle_t *handle, struct inode *inode, ...@@ -969,7 +967,7 @@ static int ext4_ind_get_blocks(handle_t *handle, struct inode *inode,
/* /*
* Okay, we need to do block allocation. * Okay, we need to do block allocation.
*/ */
goal = ext4_find_goal(inode, iblock, partial); goal = ext4_find_goal(inode, map->m_lblk, partial);
/* the number of blocks need to allocate for [d,t]indirect blocks */ /* the number of blocks need to allocate for [d,t]indirect blocks */
indirect_blks = (chain + depth) - partial - 1; indirect_blks = (chain + depth) - partial - 1;
...@@ -979,11 +977,11 @@ static int ext4_ind_get_blocks(handle_t *handle, struct inode *inode, ...@@ -979,11 +977,11 @@ static int ext4_ind_get_blocks(handle_t *handle, struct inode *inode,
* direct blocks to allocate for this branch. * direct blocks to allocate for this branch.
*/ */
count = ext4_blks_to_allocate(partial, indirect_blks, count = ext4_blks_to_allocate(partial, indirect_blks,
maxblocks, blocks_to_boundary); map->m_len, blocks_to_boundary);
/* /*
* Block out ext4_truncate while we alter the tree * Block out ext4_truncate while we alter the tree
*/ */
err = ext4_alloc_branch(handle, inode, iblock, indirect_blks, err = ext4_alloc_branch(handle, inode, map->m_lblk, indirect_blks,
&count, goal, &count, goal,
offsets + (partial - chain), partial); offsets + (partial - chain), partial);
...@@ -995,18 +993,20 @@ static int ext4_ind_get_blocks(handle_t *handle, struct inode *inode, ...@@ -995,18 +993,20 @@ static int ext4_ind_get_blocks(handle_t *handle, struct inode *inode,
* may need to return -EAGAIN upwards in the worst case. --sct * may need to return -EAGAIN upwards in the worst case. --sct
*/ */
if (!err) if (!err)
err = ext4_splice_branch(handle, inode, iblock, err = ext4_splice_branch(handle, inode, map->m_lblk,
partial, indirect_blks, count); partial, indirect_blks, count);
if (err) if (err)
goto cleanup; goto cleanup;
set_buffer_new(bh_result); map->m_flags |= EXT4_MAP_NEW;
ext4_update_inode_fsync_trans(handle, inode, 1); ext4_update_inode_fsync_trans(handle, inode, 1);
got_it: got_it:
map_bh(bh_result, inode->i_sb, le32_to_cpu(chain[depth-1].key)); map->m_flags |= EXT4_MAP_MAPPED;
map->m_pblk = le32_to_cpu(chain[depth-1].key);
map->m_len = count;
if (count > blocks_to_boundary) if (count > blocks_to_boundary)
set_buffer_boundary(bh_result); map->m_flags |= EXT4_MAP_BOUNDARY;
err = count; err = count;
/* Clean up and exit */ /* Clean up and exit */
partial = chain + depth - 1; /* the whole chain */ partial = chain + depth - 1; /* the whole chain */
...@@ -1016,7 +1016,6 @@ static int ext4_ind_get_blocks(handle_t *handle, struct inode *inode, ...@@ -1016,7 +1016,6 @@ static int ext4_ind_get_blocks(handle_t *handle, struct inode *inode,
brelse(partial->bh); brelse(partial->bh);
partial--; partial--;
} }
BUFFER_TRACE(bh_result, "returned");
out: out:
return err; return err;
} }
...@@ -1203,15 +1202,15 @@ static pgoff_t ext4_num_dirty_pages(struct inode *inode, pgoff_t idx, ...@@ -1203,15 +1202,15 @@ static pgoff_t ext4_num_dirty_pages(struct inode *inode, pgoff_t idx,
} }
/* /*
* The ext4_get_blocks() function tries to look up the requested blocks, * The ext4_map_blocks() function tries to look up the requested blocks,
* and returns if the blocks are already mapped. * and returns if the blocks are already mapped.
* *
* Otherwise it takes the write lock of the i_data_sem and allocate blocks * Otherwise it takes the write lock of the i_data_sem and allocate blocks
* and store the allocated blocks in the result buffer head and mark it * and store the allocated blocks in the result buffer head and mark it
* mapped. * mapped.
* *
* If file type is extents based, it will call ext4_ext_get_blocks(), * If file type is extents based, it will call ext4_ext_map_blocks(),
* Otherwise, call with ext4_ind_get_blocks() to handle indirect mapping * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping
* based files * based files
* *
* On success, it returns the number of blocks being mapped or allocate. * On success, it returns the number of blocks being mapped or allocate.
...@@ -1224,35 +1223,30 @@ static pgoff_t ext4_num_dirty_pages(struct inode *inode, pgoff_t idx, ...@@ -1224,35 +1223,30 @@ static pgoff_t ext4_num_dirty_pages(struct inode *inode, pgoff_t idx,
* *
* It returns the error in case of allocation failure. * It returns the error in case of allocation failure.
*/ */
int ext4_get_blocks(handle_t *handle, struct inode *inode, sector_t block, int ext4_map_blocks(handle_t *handle, struct inode *inode,
unsigned int max_blocks, struct buffer_head *bh, struct ext4_map_blocks *map, int flags)
int flags)
{ {
int retval; int retval;
clear_buffer_mapped(bh); map->m_flags = 0;
clear_buffer_unwritten(bh); ext_debug("ext4_map_blocks(): inode %lu, flag %d, max_blocks %u,"
"logical block %lu\n", inode->i_ino, flags, map->m_len,
ext_debug("ext4_get_blocks(): inode %lu, flag %d, max_blocks %u," (unsigned long) map->m_lblk);
"logical block %lu\n", inode->i_ino, flags, max_blocks,
(unsigned long)block);
/* /*
* Try to see if we can get the block without requesting a new * Try to see if we can get the block without requesting a new
* file system block. * file system block.
*/ */
down_read((&EXT4_I(inode)->i_data_sem)); down_read((&EXT4_I(inode)->i_data_sem));
if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) { if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) {
retval = ext4_ext_get_blocks(handle, inode, block, max_blocks, retval = ext4_ext_map_blocks(handle, inode, map, 0);
bh, 0);
} else { } else {
retval = ext4_ind_get_blocks(handle, inode, block, max_blocks, retval = ext4_ind_map_blocks(handle, inode, map, 0);
bh, 0);
} }
up_read((&EXT4_I(inode)->i_data_sem)); up_read((&EXT4_I(inode)->i_data_sem));
if (retval > 0 && buffer_mapped(bh)) { if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
int ret = check_block_validity(inode, "file system corruption", int ret = check_block_validity(inode, "file system corruption",
block, bh->b_blocknr, retval); map->m_lblk, map->m_pblk, retval);
if (ret != 0) if (ret != 0)
return ret; return ret;
} }
...@@ -1268,7 +1262,7 @@ int ext4_get_blocks(handle_t *handle, struct inode *inode, sector_t block, ...@@ -1268,7 +1262,7 @@ int ext4_get_blocks(handle_t *handle, struct inode *inode, sector_t block,
* ext4_ext_get_block() returns th create = 0 * ext4_ext_get_block() returns th create = 0
* with buffer head unmapped. * with buffer head unmapped.
*/ */
if (retval > 0 && buffer_mapped(bh)) if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED)
return retval; return retval;
/* /*
...@@ -1281,7 +1275,7 @@ int ext4_get_blocks(handle_t *handle, struct inode *inode, sector_t block, ...@@ -1281,7 +1275,7 @@ int ext4_get_blocks(handle_t *handle, struct inode *inode, sector_t block,
* of BH_Unwritten and BH_Mapped flags being simultaneously * of BH_Unwritten and BH_Mapped flags being simultaneously
* set on the buffer_head. * set on the buffer_head.
*/ */
clear_buffer_unwritten(bh); map->m_flags &= ~EXT4_MAP_UNWRITTEN;
/* /*
* New blocks allocate and/or writing to uninitialized extent * New blocks allocate and/or writing to uninitialized extent
...@@ -1304,13 +1298,11 @@ int ext4_get_blocks(handle_t *handle, struct inode *inode, sector_t block, ...@@ -1304,13 +1298,11 @@ int ext4_get_blocks(handle_t *handle, struct inode *inode, sector_t block,
* could have changed the inode type in between * could have changed the inode type in between
*/ */
if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) { if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) {
retval = ext4_ext_get_blocks(handle, inode, block, max_blocks, retval = ext4_ext_map_blocks(handle, inode, map, flags);
bh, flags);
} else { } else {
retval = ext4_ind_get_blocks(handle, inode, block, retval = ext4_ind_map_blocks(handle, inode, map, flags);
max_blocks, bh, flags);
if (retval > 0 && buffer_new(bh)) { if (retval > 0 && map->m_flags & EXT4_MAP_NEW) {
/* /*
* We allocated new blocks which will result in * We allocated new blocks which will result in
* i_data's format changing. Force the migrate * i_data's format changing. Force the migrate
...@@ -1333,16 +1325,38 @@ int ext4_get_blocks(handle_t *handle, struct inode *inode, sector_t block, ...@@ -1333,16 +1325,38 @@ int ext4_get_blocks(handle_t *handle, struct inode *inode, sector_t block,
EXT4_I(inode)->i_delalloc_reserved_flag = 0; EXT4_I(inode)->i_delalloc_reserved_flag = 0;
up_write((&EXT4_I(inode)->i_data_sem)); up_write((&EXT4_I(inode)->i_data_sem));
if (retval > 0 && buffer_mapped(bh)) { if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
int ret = check_block_validity(inode, "file system " int ret = check_block_validity(inode, "file system "
"corruption after allocation", "corruption after allocation",
block, bh->b_blocknr, retval); map->m_lblk, map->m_pblk,
retval);
if (ret != 0) if (ret != 0)
return ret; return ret;
} }
return retval; return retval;
} }
int ext4_get_blocks(handle_t *handle, struct inode *inode, sector_t block,
unsigned int max_blocks, struct buffer_head *bh,
int flags)
{
struct ext4_map_blocks map;
int ret;
map.m_lblk = block;
map.m_len = max_blocks;
ret = ext4_map_blocks(handle, inode, &map, flags);
if (ret < 0)
return ret;
bh->b_blocknr = map.m_pblk;
bh->b_size = inode->i_sb->s_blocksize * map.m_len;
bh->b_bdev = inode->i_sb->s_bdev;
bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags;
return ret;
}
/* Maximum number of blocks we map for direct IO at once. */ /* Maximum number of blocks we map for direct IO at once. */
#define DIO_MAX_BLOCKS 4096 #define DIO_MAX_BLOCKS 4096
......
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