ext4: s_freeclusters_counter should not tranform to unit of block before assigning to "free_clusters" in ext4_has_free_cluste
Subsystems (2)
PATCH 1/3 ext4: s_freeclusters_counter should not tranform to unit of block before assigning to "free_clusters" in ext4_has_free_cluste
Creating 4-byte files until ENOSPC in a delay-allocation and bigalloc ext4 fs and then sync it, the dmseg will report like:
[ 482.154538] EXT4-fs (sdb6): delayed block allocation failed for inode 1664 at logical offset 0 with max blocks 1 with error -28 [ 482.154540] EXT4-fs (sdb6): This should not happen!! Data will be lost
The reason is ext4_has_free_clusters reporting wrong result. Actually, the unit of sbi->s_dirtyclusters_counter is block, so we should tranform it to cluster for argument "dirty_clusters", just like "free_clusters".
--- fs/ext4/balloc.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
fs/ext4/balloc.c +1 -1
@@ -426,7 +426,7 @@ static int ext4_has_free_clusters(struct ext4_sb_info *sbi,if (free_clusters - (nclusters + root_clusters + dirty_clusters) <EXT4_FREECLUSTERS_WATERMARK ) { - free_clusters =EXT4_C2B (sbi,percpu_counter_sum_positive (fcc)); + free_clusters =percpu_counter_sum_positive (fcc); dirty_clusters =percpu_counter_sum_positive (dcc); }/* Check whether we have space after accounting for current
Signature · 1 line
1.7.3.2
PATCH 2/3 ext4: modify the implementation of quota reservation in bigalloc-delay-allocation
Change the argument of i_{reserved_data,reserved_meta,allocated_meta}_blocks to i_*_clusters
In the old logic When we try writing to a block which does not have a mapping, and bigalloc is enabled:
a) one block have been mapped, a whole cluster will be reserved.
b) While the block has not been mapped (and so the data on disk is uninitialized) the cluster has indeed been allocated,
so we don't need to reserve any additional room for the block.
Imaging s_cluster_ratio is 4 and the clusters is: (follow the comment in fs/ext4/extent.c:4100)
[0-3], [4-7], [8-11]
The new quota-reservation logic have changed to this now:
1. delay-allocation write blocks 10&11
we reserve 1 cluster, the i_reserved_data_blocks is 1 now
2. delay-allocation write blocks 3 to 8
we reserve other 2 clusters, so 3 clusters totally, the i_reserved_data_blocks is 3 now
3. writeout the blocks 3 to 8
claim all 3 clusters, i_reserved_data_blocks is 0 now
At this moment, we really have allocated 3 clusters, and the remain 10&11 block would never occupy another cluster (it would definitely go into the [8-11] cluster), so, we don't need to reserve one more cluster quota (which is the old logic).
4. writeout the 10&11 blocks
the 10&11 blocks will be set EXT4_MAP_FROM_CLUSTER flag ( by get_implied_cluster_alloc as the 8~9 block have been allocated), and it will not call ext4_da_update_reserve_space any more
As this, no warning, no reserve-and-release ping pong.
--- fs/ext4/balloc.c | 2 +- fs/ext4/ext4.h | 6 +- fs/ext4/extents.c | 97 +++++++++++-------------------------------- fs/ext4/file.c | 2 +- fs/ext4/inode.c | 66 +++++++++++++++-------------- fs/ext4/super.c | 7 ++- include/trace/events/ext4.h | 42 +++++++++--------- 7 files changed, 89 insertions(+), 133 deletions(-)
fs/ext4/balloc.c +1 -1
@@ -518,7 +518,7 @@ ext4_fsblk_t ext4_new_meta_blocks(handle_t *handle, struct inode *inode,if (!(*errp) &&ext4_test_inode_state (inode,EXT4_STATE_DELALLOC_RESERVED )) {spin_lock (&EXT4_I (inode)->i_block_reservation_lock); -EXT4_I (inode)->i_allocated_meta_blocks += ar.len; +EXT4_I (inode)->i_allocated_meta_clusters += ar.len;spin_unlock (&EXT4_I (inode)->i_block_reservation_lock);dquot_alloc_block_nofail (inode,EXT4_C2B (EXT4_SB (inode->i_sb), ar.len));
fs/ext4/ext4.h +3 -3
@@ -869,9 +869,9 @@ struct ext4_inode_info {/* allocation reservation info for delalloc */ /* In case of bigalloc, these refer to clusters rather than blocks */ -unsigned int i_reserved_data_blocks; -unsigned int i_reserved_meta_blocks; -unsigned int i_allocated_meta_blocks; +unsigned int i_reserved_data_clusters; +unsigned int i_reserved_meta_clusters; +unsigned int i_allocated_meta_clusters;ext4_lblk_t i_da_metadata_calc_last_lblock;int i_da_metadata_calc_len;
fs/ext4/extents.c +25 -72
120 lines not on this page · raw message
fs/ext4/file.c +1 -1
@@ -43,7 +43,7 @@ static int ext4_release_file(struct inode *inode, struct file *filp)/* if we are the last writer on the inode, drop the block reservation */ if ((filp->f_mode &FMODE_WRITE ) && (atomic_read (&inode->i_writecount) ==1 ) && - !EXT4_I (inode)->i_reserved_data_blocks) + !EXT4_I (inode)->i_reserved_data_clusters) {down_write (&EXT4_I (inode)->i_data_sem);ext4_discard_preallocations (inode);
fs/ext4/inode.c +34 -32
145 lines not on this page · raw message
fs/ext4/super.c +4 -3
@@ -899,9 +899,9 @@ static struct inode *ext4_alloc_inode(struct super_block *sb)memset (&ei->i_cached_extent,0 ,sizeof (struct ext4_ext_cache ));INIT_LIST_HEAD (&ei->i_prealloc_list);spin_lock_init (&ei->i_prealloc_lock); - ei->i_reserved_data_blocks =0 ; - ei->i_reserved_meta_blocks =0 ; - ei->i_allocated_meta_blocks =0 ; + ei->i_reserved_data_clusters =0 ; + ei->i_reserved_meta_clusters =0 ; + ei->i_allocated_meta_clusters =0 ; ei->i_da_metadata_calc_len =0 ;spin_lock_init (&(ei->i_block_reservation_lock));#ifdef CONFIG_QUOTA
@@ -4646,6 +4646,7 @@ static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf) EXT4_C2B(sbi, sbi->s_overhead_last)); bfree =percpu_counter_sum_positive (&sbi->s_freeclusters_counter) -percpu_counter_sum_positive (&sbi->s_dirtyclusters_counter); +/* prevent underflow in case that few free space is available */ buf->f_bfree =EXT4_C2B (sbi,max_t (s64, bfree,0 )); buf->f_bavail = buf->f_bfree -ext4_r_blocks_count (es);
include/trace/events/ext4.h +21 -21
89 lines not on this page · raw message
Signature · 1 line
1.7.3.2
PATCH 3/3 ext4: modify the process of invalidate_page for the new implemention of quota reservation
In reply to Robin DongSince buffer_head with PG_Delay does not means "quota already occupied" any more, we should check whether to release reservation in ext4_da_invalidatepage.
--- fs/ext4/inode.c | 25 ++++++++++++++++--------- 1 files changed, 16 insertions(+), 9 deletions(-)
fs/ext4/inode.c +1 -7
@@ -1202,6 +1202,7 @@ static void ext4_da_page_release_reservation(struct page *page,unsigned int curr_off =0 ;struct inode *inode = page->mapping->host;struct ext4_sb_info *sbi =EXT4_SB (inode->i_sb); +struct ext4_map_blocks map;int num_clusters; head =page_buffers (page);
@@ -1220,15 +1221,21 @@ static void ext4_da_page_release_reservation(struct page *page,/* If we have released all the blocks belonging to a cluster, then we * need to release the reserved space for that cluster. */ num_clusters =EXT4_NUM_B2C (sbi, to_release); -while (num_clusters >0 ) { -ext4_fsblk_t lblk; - lblk = (page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits)) + - ((num_clusters -1 ) << sbi->s_cluster_bits); -if (sbi->s_cluster_ratio ==1 || - !ext4_find_delalloc_cluster (inode, lblk,1 )) -ext4_da_release_space (inode,1 );
Ted Ts'o
maintainerQuoted 9 lines · Robin Dong
Creating 4-byte files until ENOSPC in a delay-allocation and bigalloc ext4 fs and then sync it, the dmseg will report like:
[ 482.154538] EXT4-fs (sdb6): delayed block allocation failed for inode 1664 at logical offset 0 with max blocks 1 with error -28 [ 482.154540] EXT4-fs (sdb6): This should not happen!! Data will be lostThe reason is
ext4_has_free_clustersreporting wrong result. Actually, the unit ofsbi->s_dirtyclusters_counteris block, so we should tranform it to cluster for argument "dirty_clusters", just like "free_clusters".
We have a bigger problem here, which is that this is not the only place where s_dirty_clusters_counter is being used in units of clusters. (See ext4_claim_free_clusters, which when called by mballoc is using units of clusters.)
We definitely have brokeness here, but this is not the whole story. We need to take a step back here and decide whether the correct units is clusters or blocks. Ultimately I think it does need to be clusters, because we can't just convert blocks and clusters by using B2C; we could dirty 3 blocks, but if those 3 blocks span two 64-block clusters, what's important is that we have to reserve space for 2 clusters. We can't just calculate "3 >> 6" and assume that we can reserve 0 clusters and be done with it!
This is one of the places where I think we need to solve things by having a better data structure for tracking which pages have been subject to delayed allocation, since if we touch another block in a cluster where we've done a delayed allocation, we don't need to bump s_dirtyclusters_counter. However, if this is the first time we've touched a block in a particular cluster, then we *do* need to bump s_dirtyclusters_counter --- and if we need to search all of the pages in the page cache to make this determination, it's going to be painful....
- Ted
Quoted 16 lines · 在 2012年3月25日 上午5:05,Ted Ts'o <[email protected]> 写道:
Quoted 9 lines · Robin Dong
Creating 4-byte files until ENOSPC in a delay-allocation and bigalloc ext4 fs and then sync it, the dmseg will report like:
[ 482.154538] EXT4-fs (sdb6): delayed block allocation failed for inode 1664 at logical offset 0 with max blocks 1 with error -28 [ 482.154540] EXT4-fs (sdb6): This should not happen!! Data will be lostThe reason is
ext4_has_free_clustersreporting wrong result. Actually, the unit ofsbi->s_dirtyclusters_counteris block, so we should tranform it to cluster for argument "dirty_clusters", just like "free_clusters".We have a bigger problem here, which is that this is not the only place where
s_dirty_clusters_counteris being used in units of clusters. (Seeext4_claim_free_clusters, which when called by mballoc is using units of clusters.)
Quoted 8 lines
We definitely have brokeness here, but this is not the whole story. We need to take a step back here and decide whether the correct units is clusters or blocks. Ultimately I think it does need to be clusters, because we can't just convert blocks and clusters by using B2C; we could dirty 3 blocks, but if those 3 blocks span two 64-block clusters, what's important is that we have to reserve space for 2 clusters. We can't just calculate "3 >> 6" and assume that we can reserve 0 clusters and be done with it!
Hi, Ted
Actually, I have complete this logic in my "[PATCH 2/3] ext4: modify the implementation of quota reservation in bigalloc-delay-allocation"
(http://marc.info/?l=linux-ext4&m=133163876628132&w=2) patch, when those 3 blocks span two clusters, the logic in ext4_ext_map_blocks() will check whether a block is already in a delay-allocated-cluster (or already be
allocated in disk) and ultimately reserved 2 clusters for the 3 blocks.
Imaging block0 and block1 in cluster0, block2 in cluster1. When ext4_da_map_blocks() process block0, we will call ext4_da_reserve_space() (which will
reserve a cluster) and bump s_dirtycluster_counter , but when process block1, ext4_ext_map_blocks will return flag with EXT4_MAP_FROM_CLUSTER
and we will not bump s_dirtycluster_counter this time since block1 is belong to a already reserved cluster.
Maybe I misunderstanding your meaning, could you please point out my fault?
Thanks.
Quoted 12 lines
This is one of the places where I think we need to solve things by having a better data structure for tracking which pages have been subject to delayed allocation, since if we touch another block in a cluster where we've done a delayed allocation, we don't need to bump
s_dirtyclusters_counter. However, if this is the first time we've touched a block in a particular cluster, then we *do* need to bumps_dirtyclusters_counter--- and if we need to search all of the pages in the page cache to make this determination, it's going to be painful....- Ted
Signature · 7 lines
-- Best Regard Robin Dong -- To unsubscribe from this list: send the line "unsubscribe linux-ext4" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html