ext4: Update inode i_size after the preallocation
Subsystems (2)
PATCH v3 1/3 ext4: Update inode i_size after the preallocation
Currently in ext4_fallocate we would update inode size, c_time and sync the file with every partial allocation which is entirely unnecessary. It is true that if the crash happens in the middle of truncate we might end up with unchanged i size, or c_time which I do not think is really a problem - it does not mean file system corruption in any way. Note that xfs is doing things the same way e.g. update all of the mentioned after the allocation is done.
This commit moves all the updates after the allocation is done. In addition we also need to change m_time as not only inode has been change bot also data regions might have changed (unwritten extents). However m_time will be only updated when i_size changed.
Also we do not need to be paranoid about changing the c_time only if the actual allocation have happened, we can change it even if we try to allocate only to find out that there are already block allocated. It's not really a big deal and it will save us some additional complexity.
Also use ext4_debug, instead of ext4_warning in #ifdef EXT4FS_DEBUG section.
---
v3: Do not remove the code to set EXT4_INODE_EOFBLOCKS flag
fs/ext4/extents.c | 96 ++++++++++++++++++++++++------------------------------- 1 file changed, 42 insertions(+), 54 deletions(-)
fs/ext4/extents.c +42 -54
@@ -4546,36 +4546,6 @@ retry:ext4_std_error (inode->i_sb, err); } -static void ext4_falloc_update_inode (struct inode *inode, -int mode,loff_t new_size,int update_ctime) -{ -struct timespec now; - -if (update_ctime) { - now =current_fs_time (inode->i_sb); -if (!timespec_equal (&inode->i_ctime, &now)) - inode->i_ctime = now; - } -/* -* Update only when preallocation was requested beyond -* the file size. -*/ -if (!(mode &FALLOC_FL_KEEP_SIZE )) { -if (new_size >i_size_read (inode)) -i_size_write (inode, new_size); -if (new_size >EXT4_I (inode)->i_disksize) -ext4_update_i_disksize (inode, new_size); - }else { -/* -* Mark that we allocate beyond EOF so the subsequent truncate -* can proceed even if the new size is the same as i_size. -*/ -if (new_size >i_size_read (inode)) -ext4_set_inode_flag (inode,EXT4_INODE_EOFBLOCKS ); - } - -} -/* * preallocate space for a file. This implements ext4's fallocate file * operation, which gets called from sys_fallocate system call.
@@ -4587,13 +4557,14 @@ long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len) {struct inode *inode =file_inode (file);handle_t *handle; -loff_t new_size; +loff_t new_size =0 ;unsigned int max_blocks;int ret =0 ;int ret2 =0 ;int retries =0 ;int flags;struct ext4_map_blocks map; +struct timespec tv;unsigned int credits, blkbits = inode->i_blkbits;/* Return error if mode is not supported */
@@ -4631,12 +4602,15 @@ long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len)*/ credits =ext4_chunk_trans_blocks (inode, max_blocks);mutex_lock (&inode->i_mutex); - ret =inode_newsize_ok (inode, (len + offset)); -if (ret) { -mutex_unlock (&inode->i_mutex); -trace_ext4_fallocate_exit (inode, offset, max_blocks, ret); -return ret; + +if (!(mode &FALLOC_FL_KEEP_SIZE ) && + offset + len >i_size_read (inode)) { + new_size = offset + len; + ret =inode_newsize_ok (inode, new_size); +if (ret) + goto out; } + flags =EXT4_GET_BLOCKS_CREATE_UNINIT_EXT ;if (mode &FALLOC_FL_KEEP_SIZE ) flags |=EXT4_GET_BLOCKS_KEEP_SIZE ;
@@ -4660,28 +4634,14 @@ retry: } ret =ext4_map_blocks (handle, inode, &map, flags);if (ret <=0 ) { -#ifdef EXT4FS_DEBUG -ext4_warning (inode->i_sb, -"inode #%lu: block %u: len %u: " -"ext4_ext_map_blocks returned %d" , - inode->i_ino, map.m_lblk, - map.m_len, ret); -#endif +ext4_debug ("inode #%lu: block %u: len %u: " +"ext4_ext_map_blocks returned %d" , + inode->i_ino, map.m_lblk, + map.m_len, ret);ext4_mark_inode_dirty (handle, inode); ret2 =ext4_journal_stop (handle);break ; } -if ((map.m_lblk + ret) >= (EXT4_BLOCK_ALIGN (offset + len, - blkbits) >> blkbits)) - new_size = offset + len; -else - new_size = ((loff_t ) map.m_lblk + ret) << blkbits; - -ext4_falloc_update_inode (inode, mode, new_size, - (map.m_flags &EXT4_MAP_NEW )); -ext4_mark_inode_dirty (handle, inode); -if ((file->f_flags &O_SYNC ) && ret >= max_blocks) -ext4_handle_sync (handle); ret2 =ext4_journal_stop (handle);if (ret2)break ;
@@ -4691,6 +4651,34 @@ retry: ret =0 ; goto retry; } + + handle =ext4_journal_start (inode,EXT4_HT_INODE ,2 ); +if (IS_ERR (handle)) + goto out; + + tv = inode->i_ctime =ext4_current_time (inode); + +if (ret >0 && new_size) { +if (new_size >i_size_read (inode)) { +i_size_write (inode, new_size); + inode->i_mtime = tv; + } +if (new_size >EXT4_I (inode)->i_disksize) +ext4_update_i_disksize (inode, new_size); + }else if (ret >0 && !new_size) { +/* +* Mark that we allocate beyond EOF so the subsequent truncate +* can proceed even if the new size is the same as i_size. +*/ +if ((offset + len) >i_size_read (inode)) +ext4_set_inode_flag (inode,EXT4_INODE_EOFBLOCKS ); + } +ext4_mark_inode_dirty (handle, inode); +if (file->f_flags &O_SYNC ) +ext4_handle_sync (handle); + +ext4_journal_stop (handle); +out:mutex_unlock (&inode->i_mutex);trace_ext4_fallocate_exit (inode, offset, max_blocks, ret >0 ? ret2 : ret);
Signature · 1 line
1.8.3.1
PATCH v3 2/3 ext4: refactor ext4_fallocate code
Move block allocation out of the ext4_fallocate into separate function called ext4_alloc_file_blocks(). This will allow us to use the same allocation code for other allocation operations such as zero range which is commit in the next patch.
--- fs/ext4/extents.c | 129 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 74 insertions(+), 55 deletions(-)
fs/ext4/extents.c +74 -55
@@ -4546,6 +4546,64 @@ retry:ext4_std_error (inode->i_sb, err); } +static int ext4_alloc_file_blocks (struct file *file,ext4_lblk_t offset, +ext4_lblk_t len,int flags,int mode) +{ +struct inode *inode =file_inode (file); +handle_t *handle; +int ret =0 ; +int ret2 =0 ; +int retries =0 ; +struct ext4_map_blocks map; +unsigned int credits; + + map.m_lblk = offset; +/* +* Don't normalize the request if it can fit in one extent so +* that it doesn't get unnecessarily split into multiple +* extents. +*/ +if (len <=EXT_UNINIT_MAX_LEN ) + flags |=EXT4_GET_BLOCKS_NO_NORMALIZE ; + +/* +* credits to insert 1 extent into extent tree +*/ + credits =ext4_chunk_trans_blocks (inode, len); + +retry: +while (ret >=0 && ret < len) { + map.m_lblk = map.m_lblk + ret; + map.m_len = len = len - ret; + handle =ext4_journal_start (inode,EXT4_HT_MAP_BLOCKS , + credits); +if (IS_ERR (handle)) { + ret =PTR_ERR (handle); +break ; + } + ret =ext4_map_blocks (handle, inode, &map, flags); +if (ret <=0 ) { +ext4_debug ("inode #%lu: block %u: len %u: " +"ext4_ext_map_blocks returned %d" , + inode->i_ino, map.m_lblk, + map.m_len, ret); +ext4_mark_inode_dirty (handle, inode); + ret2 =ext4_journal_stop (handle); +break ; + } + ret2 =ext4_journal_stop (handle); +if (ret2) +break ; + } +if (ret == -ENOSPC && +ext4_should_retry_alloc (inode->i_sb, &retries)) { + ret =0 ; + goto retry; + } + +return ret >0 ? ret2 : ret; +} +/* * preallocate space for a file. This implements ext4's fallocate file * operation, which gets called from sys_fallocate system call.
@@ -4560,12 +4618,10 @@ long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len)loff_t new_size =0 ;unsigned int max_blocks;int ret =0 ; -int ret2 =0 ; -int retries =0 ;int flags; -struct ext4_map_blocks map; +ext4_lblk_t lblk;struct timespec tv; -unsigned int credits, blkbits = inode->i_blkbits; +unsigned int blkbits = inode->i_blkbits;/* Return error if mode is not supported */ if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
@@ -4590,17 +4646,18 @@ long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len)return -EOPNOTSUPP ;trace_ext4_fallocate_enter (inode, offset, len, mode); - map.m_lblk = offset >> blkbits; + lblk = offset >> blkbits;/* * We can't just convert len to max_blocks because * If blocksize = 4096 offset = 3072 and len = 2048 */ max_blocks = (EXT4_BLOCK_ALIGN (len + offset, blkbits) >> blkbits) - - map.m_lblk; -/* -* credits to insert 1 extent into extent tree -*/ - credits =ext4_chunk_trans_blocks (inode, max_blocks); + - lblk; + + flags =EXT4_GET_BLOCKS_CREATE_UNINIT_EXT ; +if (mode &FALLOC_FL_KEEP_SIZE ) + flags |=EXT4_GET_BLOCKS_KEEP_SIZE ; +mutex_lock (&inode->i_mutex); if (!(mode & FALLOC_FL_KEEP_SIZE) &&
@@ -4611,46 +4668,9 @@ long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len) goto out; } - flags =EXT4_GET_BLOCKS_CREATE_UNINIT_EXT ; -if (mode &FALLOC_FL_KEEP_SIZE ) - flags |=EXT4_GET_BLOCKS_KEEP_SIZE ; -/* -* Don't normalize the request if it can fit in one extent so -* that it doesn't get unnecessarily split into multiple -* extents. -*/ -if (len <=EXT_UNINIT_MAX_LEN << blkbits) - flags |=EXT4_GET_BLOCKS_NO_NORMALIZE ; - -retry: -while (ret >=0 && ret < max_blocks) { - map.m_lblk = map.m_lblk + ret; - map.m_len = max_blocks = max_blocks - ret; - handle =ext4_journal_start (inode,EXT4_HT_MAP_BLOCKS , - credits); -if (IS_ERR (handle)) { - ret =PTR_ERR (handle); -break ; - } - ret =ext4_map_blocks (handle, inode, &map, flags); -if (ret <=0 ) { -ext4_debug ("inode #%lu: block %u: len %u: " -"ext4_ext_map_blocks returned %d" , - inode->i_ino, map.m_lblk, - map.m_len, ret); -ext4_mark_inode_dirty (handle, inode); - ret2 =ext4_journal_stop (handle); -break ; - } - ret2 =ext4_journal_stop (handle); -if (ret2) -break ; - } -if (ret == -ENOSPC && -ext4_should_retry_alloc (inode->i_sb, &retries)) { - ret =0 ; - goto retry; - } + ret =ext4_alloc_file_blocks (file, lblk, max_blocks, flags, mode); +if (ret) + goto out; handle =ext4_journal_start (inode,EXT4_HT_INODE ,2 );if (IS_ERR (handle))
@@ -4658,14 +4678,14 @@ retry: tv = inode->i_ctime =ext4_current_time (inode); -if (ret >0 && new_size) { +if (!ret && new_size) {if (new_size >i_size_read (inode)) {i_size_write (inode, new_size); inode->i_mtime = tv; }if (new_size >EXT4_I (inode)->i_disksize)ext4_update_i_disksize (inode, new_size); - }else if (ret >0 && !new_size) { + }else if (!ret && !new_size) {/* * Mark that we allocate beyond EOF so the subsequent truncate * can proceed even if the new size is the same as i_size.
@@ -4680,9 +4700,8 @@ retry:ext4_journal_stop (handle); out:mutex_unlock (&inode->i_mutex); -trace_ext4_fallocate_exit (inode, offset, max_blocks, - ret >0 ? ret2 : ret); -return ret >0 ? ret2 : ret; +trace_ext4_fallocate_exit (inode, offset, max_blocks, ret); +return ret; }/*
Signature · 1 line
1.8.3.1
PATCH v3 3/3 ext4: Introduce FALLOC_FL_ZERO_RANGE flag for fallocate
In reply to Lukas CzernerIntroduce new FALLOC_FL_ZERO_RANGE flag for fallocate. This has the same functionality as xfs ioctl XFS_IOC_ZERO_RANGE.
It can be used to convert a range of file to zeros preferably without issuing data IO. Blocks should be preallocated for the regions that span holes in the file, and the entire range is preferable converted to unwritten extents
This can be also used to preallocate blocks past EOF in the same way as with fallocate. Flag FALLOC_FL_KEEP_SIZE which should cause the inode size to remain the same.
Also add appropriate tracepoints.
---
v3: Set EXT4_INODE_EOFBLOCKS when needed in zero_range
Fix the problems when rebased on ext4/dev branch
fs/ext4/ext4.h | 2 + fs/ext4/extents.c | 273 +++++++++++++++++++++++++++++++++++++++++--- fs/ext4/inode.c | 17 ++- include/trace/events/ext4.h | 68 +++++------ 4 files changed, 307 insertions(+), 53 deletions(-)
fs/ext4/ext4.h +2 -0
@@ -568,6 +568,8 @@ enum {#define EXT4_GET_BLOCKS_NO_LOCK 0x0100/* Do not put hole in extent cache */ #define EXT4_GET_BLOCKS_NO_PUT_HOLE 0x0200 +/* Convert written extents to unwritten */ +#define EXT4_GET_BLOCKS_CONVERT_UNWRITTEN 0x0400/* * The bit position of these flags must not overlap with any of the
fs/ext4/extents.c +259 -14
349 lines not on this page · raw message
fs/ext4/inode.c +11 -6
@@ -503,6 +503,7 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode, {struct extent_status es;int retval; +int ret =0 ;#ifdef ES_AGGRESSIVE_TEST struct ext4_map_blocks orig_map;
@@ -558,7 +559,6 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode, EXT4_GET_BLOCKS_KEEP_SIZE); }if (retval >0 ) { -int ret;unsigned int status;if (unlikely (retval != map->m_len)) {
@@ -585,7 +585,7 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode, found:if (retval >0 && map->m_flags &EXT4_MAP_MAPPED ) { -int ret =check_block_validity (inode, map); + ret =check_block_validity (inode, map);if (ret !=0 )return ret; }
@@ -602,7 +602,13 @@ found:* with buffer head unmapped. */ if (retval >0 && map->m_flags &EXT4_MAP_MAPPED ) -return retval; +/* +* If we need to convert extent to unwritten +* we continue and do the actual work in +* ext4_ext_map_blocks() +*/ +if (!(flags &EXT4_GET_BLOCKS_CONVERT_UNWRITTEN )) +return retval;/* * Here we clear m_flags because after allocating an new extent,
@@ -658,7 +664,6 @@ found:ext4_clear_inode_state (inode,EXT4_STATE_DELALLOC_RESERVED );if (retval >0 ) { -int ret;unsigned int status;if (unlikely (retval != map->m_len)) {
@@ -693,7 +698,7 @@ found: has_zeroout:up_write ((&EXT4_I (inode)->i_data_sem));if (retval >0 && map->m_flags &EXT4_MAP_MAPPED ) { -int ret =check_block_validity (inode, map); + ret =check_block_validity (inode, map);if (ret !=0 )return ret; }
@@ -3507,7 +3512,7 @@ int ext4_punch_hole(struct inode *inode, loff_t offset, loff_t length)if (!S_ISREG (inode->i_mode))return -EOPNOTSUPP ; -trace_ext4_punch_hole (inode, offset, length); +trace_ext4_punch_hole (inode, offset, length,0 );/* * Write out all dirty pages to avoid race conditions
include/trace/events/ext4.h +35 -32
105 lines not on this page · raw message
[email protected]
maintainerQuoted 18 lines · Lukas Czerner
Currently in
ext4_fallocatewe would update inode size,c_timeand sync the file with every partial allocation which is entirely unnecessary. It is true that if the crash happens in the middle of truncate we might end up with unchanged i size, orc_timewhich I do not think is really a problem - it does not mean file system corruption in any way. Note that xfs is doing things the same way e.g. update all of the mentioned after the allocation is done.This commit moves all the updates after the allocation is done. In addition we also need to change
m_timeas not only inode has been change bot also data regions might have changed (unwritten extents). Howeverm_timewill be only updated wheni_sizechanged.Also we do not need to be paranoid about changing the
c_timeonly if the actual allocation have happened, we can change it even if we try to allocate only to find out that there are already block allocated. It's not really a big deal and it will save us some additional complexity.
Quoted 4 lines
Also use
ext4_debug, instead ofext4_warningin #ifdefEXT4FS_DEBUGsection.Signed-off-by Lukas Czerner <[email protected]>
I noticed I didn't explicitly ack this in patchwork, but as you've probably noticed, I've merged the v3 version of these patches into the ext4 patch queue.
Cheers,
- Ted