Commit 6bc6e63f authored by Aneesh Kumar K.V's avatar Aneesh Kumar K.V Committed by Theodore Ts'o

ext4: Add percpu dirty block accounting.

This patch adds dirty block accounting using percpu_counters.  Delayed
allocation block reservation is now done by updating dirty block
counter.  In a later patch we switch to non delalloc mode if the
filesystem free blocks is greater than 150% of total filesystem dirty
blocks
Signed-off-by: default avatarAneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
Signed-off-by: Mingming Cao<cmm@us.ibm.com>
Signed-off-by: default avatar"Theodore Ts'o" <tytso@mit.edu>
parent 030ba6bc
...@@ -1605,26 +1605,38 @@ ext4_try_to_allocate_with_rsv(struct super_block *sb, handle_t *handle, ...@@ -1605,26 +1605,38 @@ ext4_try_to_allocate_with_rsv(struct super_block *sb, handle_t *handle,
int ext4_claim_free_blocks(struct ext4_sb_info *sbi, int ext4_claim_free_blocks(struct ext4_sb_info *sbi,
ext4_fsblk_t nblocks) ext4_fsblk_t nblocks)
{ {
s64 free_blocks; s64 free_blocks, dirty_blocks;
ext4_fsblk_t root_blocks = 0; ext4_fsblk_t root_blocks = 0;
struct percpu_counter *fbc = &sbi->s_freeblocks_counter; struct percpu_counter *fbc = &sbi->s_freeblocks_counter;
struct percpu_counter *dbc = &sbi->s_dirtyblocks_counter;
free_blocks = percpu_counter_read(fbc); free_blocks = percpu_counter_read_positive(fbc);
dirty_blocks = percpu_counter_read_positive(dbc);
if (!capable(CAP_SYS_RESOURCE) && if (!capable(CAP_SYS_RESOURCE) &&
sbi->s_resuid != current->fsuid && sbi->s_resuid != current->fsuid &&
(sbi->s_resgid == 0 || !in_group_p(sbi->s_resgid))) (sbi->s_resgid == 0 || !in_group_p(sbi->s_resgid)))
root_blocks = ext4_r_blocks_count(sbi->s_es); root_blocks = ext4_r_blocks_count(sbi->s_es);
if (free_blocks - (nblocks + root_blocks) < EXT4_FREEBLOCKS_WATERMARK) if (free_blocks - (nblocks + root_blocks + dirty_blocks) <
free_blocks = percpu_counter_sum(&sbi->s_freeblocks_counter); EXT4_FREEBLOCKS_WATERMARK) {
free_blocks = percpu_counter_sum(fbc);
if (free_blocks < (root_blocks + nblocks)) dirty_blocks = percpu_counter_sum(dbc);
if (dirty_blocks < 0) {
printk(KERN_CRIT "Dirty block accounting "
"went wrong %lld\n",
dirty_blocks);
}
}
/* Check whether we have space after
* accounting for current dirty blocks
*/
if (free_blocks < ((s64)(root_blocks + nblocks) + dirty_blocks))
/* we don't have free space */ /* we don't have free space */
return -ENOSPC; return -ENOSPC;
/* reduce fs free blocks counter */ /* Add the blocks to nblocks */
percpu_counter_sub(fbc, nblocks); percpu_counter_add(dbc, nblocks);
return 0; return 0;
} }
...@@ -1640,23 +1652,28 @@ int ext4_claim_free_blocks(struct ext4_sb_info *sbi, ...@@ -1640,23 +1652,28 @@ int ext4_claim_free_blocks(struct ext4_sb_info *sbi,
ext4_fsblk_t ext4_has_free_blocks(struct ext4_sb_info *sbi, ext4_fsblk_t ext4_has_free_blocks(struct ext4_sb_info *sbi,
ext4_fsblk_t nblocks) ext4_fsblk_t nblocks)
{ {
ext4_fsblk_t free_blocks; ext4_fsblk_t free_blocks, dirty_blocks;
ext4_fsblk_t root_blocks = 0; ext4_fsblk_t root_blocks = 0;
struct percpu_counter *fbc = &sbi->s_freeblocks_counter;
struct percpu_counter *dbc = &sbi->s_dirtyblocks_counter;
free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter); free_blocks = percpu_counter_read_positive(fbc);
dirty_blocks = percpu_counter_read_positive(dbc);
if (!capable(CAP_SYS_RESOURCE) && if (!capable(CAP_SYS_RESOURCE) &&
sbi->s_resuid != current->fsuid && sbi->s_resuid != current->fsuid &&
(sbi->s_resgid == 0 || !in_group_p(sbi->s_resgid))) (sbi->s_resgid == 0 || !in_group_p(sbi->s_resgid)))
root_blocks = ext4_r_blocks_count(sbi->s_es); root_blocks = ext4_r_blocks_count(sbi->s_es);
if (free_blocks - (nblocks + root_blocks) < EXT4_FREEBLOCKS_WATERMARK) if (free_blocks - (nblocks + root_blocks + dirty_blocks) <
free_blocks = percpu_counter_sum_positive(&sbi->s_freeblocks_counter); EXT4_FREEBLOCKS_WATERMARK) {
free_blocks = percpu_counter_sum_positive(fbc);
if (free_blocks <= root_blocks) dirty_blocks = percpu_counter_sum_positive(dbc);
}
if (free_blocks <= (root_blocks + dirty_blocks))
/* we don't have free space */ /* we don't have free space */
return 0; return 0;
if (free_blocks - root_blocks < nblocks) if (free_blocks - (root_blocks + dirty_blocks) < nblocks)
return free_blocks - root_blocks; return free_blocks - root_blocks;
return nblocks; return nblocks;
} }
...@@ -1943,13 +1960,14 @@ ext4_fsblk_t ext4_old_new_blocks(handle_t *handle, struct inode *inode, ...@@ -1943,13 +1960,14 @@ ext4_fsblk_t ext4_old_new_blocks(handle_t *handle, struct inode *inode,
le16_add_cpu(&gdp->bg_free_blocks_count, -num); le16_add_cpu(&gdp->bg_free_blocks_count, -num);
gdp->bg_checksum = ext4_group_desc_csum(sbi, group_no, gdp); gdp->bg_checksum = ext4_group_desc_csum(sbi, group_no, gdp);
spin_unlock(sb_bgl_lock(sbi, group_no)); spin_unlock(sb_bgl_lock(sbi, group_no));
if (!EXT4_I(inode)->i_delalloc_reserved_flag && (*count != num)) { percpu_counter_sub(&sbi->s_freeblocks_counter, num);
/* /*
* we allocated less blocks than we * Now reduce the dirty block count also. Should not go negative
* claimed. Add the difference back. */
*/ if (!EXT4_I(inode)->i_delalloc_reserved_flag)
percpu_counter_add(&sbi->s_freeblocks_counter, *count - num); percpu_counter_sub(&sbi->s_dirtyblocks_counter, *count);
} else
percpu_counter_sub(&sbi->s_dirtyblocks_counter, num);
if (sbi->s_log_groups_per_flex) { if (sbi->s_log_groups_per_flex) {
ext4_group_t flex_group = ext4_flex_group(sbi, group_no); ext4_group_t flex_group = ext4_flex_group(sbi, group_no);
spin_lock(sb_bgl_lock(sbi, flex_group)); spin_lock(sb_bgl_lock(sbi, flex_group));
......
...@@ -59,6 +59,7 @@ struct ext4_sb_info { ...@@ -59,6 +59,7 @@ struct ext4_sb_info {
struct percpu_counter s_freeblocks_counter; struct percpu_counter s_freeblocks_counter;
struct percpu_counter s_freeinodes_counter; struct percpu_counter s_freeinodes_counter;
struct percpu_counter s_dirs_counter; struct percpu_counter s_dirs_counter;
struct percpu_counter s_dirtyblocks_counter;
struct blockgroup_lock s_blockgroup_lock; struct blockgroup_lock s_blockgroup_lock;
/* root of the per fs reservation window tree */ /* root of the per fs reservation window tree */
......
...@@ -1030,19 +1030,20 @@ static void ext4_da_update_reserve_space(struct inode *inode, int used) ...@@ -1030,19 +1030,20 @@ static void ext4_da_update_reserve_space(struct inode *inode, int used)
BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks); BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks);
mdb_free = EXT4_I(inode)->i_reserved_meta_blocks - mdb; mdb_free = EXT4_I(inode)->i_reserved_meta_blocks - mdb;
/* Account for allocated meta_blocks */ if (mdb_free) {
mdb_free -= EXT4_I(inode)->i_allocated_meta_blocks; /* Account for allocated meta_blocks */
mdb_free -= EXT4_I(inode)->i_allocated_meta_blocks;
/* update fs free blocks counter for truncate case */
percpu_counter_add(&sbi->s_freeblocks_counter, mdb_free); /* update fs dirty blocks counter */
percpu_counter_sub(&sbi->s_dirtyblocks_counter, mdb_free);
EXT4_I(inode)->i_allocated_meta_blocks = 0;
EXT4_I(inode)->i_reserved_meta_blocks = mdb;
}
/* update per-inode reservations */ /* update per-inode reservations */
BUG_ON(used > EXT4_I(inode)->i_reserved_data_blocks); BUG_ON(used > EXT4_I(inode)->i_reserved_data_blocks);
EXT4_I(inode)->i_reserved_data_blocks -= used; EXT4_I(inode)->i_reserved_data_blocks -= used;
BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks);
EXT4_I(inode)->i_reserved_meta_blocks = mdb;
EXT4_I(inode)->i_allocated_meta_blocks = 0;
spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
} }
...@@ -1588,8 +1589,8 @@ static void ext4_da_release_space(struct inode *inode, int to_free) ...@@ -1588,8 +1589,8 @@ static void ext4_da_release_space(struct inode *inode, int to_free)
release = to_free + mdb_free; release = to_free + mdb_free;
/* update fs free blocks counter for truncate case */ /* update fs dirty blocks counter for truncate case */
percpu_counter_add(&sbi->s_freeblocks_counter, release); percpu_counter_sub(&sbi->s_dirtyblocks_counter, release);
/* update per-inode reservations */ /* update per-inode reservations */
BUG_ON(to_free > EXT4_I(inode)->i_reserved_data_blocks); BUG_ON(to_free > EXT4_I(inode)->i_reserved_data_blocks);
...@@ -2471,7 +2472,6 @@ static int ext4_da_write_begin(struct file *file, struct address_space *mapping, ...@@ -2471,7 +2472,6 @@ static int ext4_da_write_begin(struct file *file, struct address_space *mapping,
index = pos >> PAGE_CACHE_SHIFT; index = pos >> PAGE_CACHE_SHIFT;
from = pos & (PAGE_CACHE_SIZE - 1); from = pos & (PAGE_CACHE_SIZE - 1);
to = from + len; to = from + len;
retry: retry:
/* /*
* With delayed allocation, we don't log the i_disksize update * With delayed allocation, we don't log the i_disksize update
......
...@@ -2880,7 +2880,7 @@ void exit_ext4_mballoc(void) ...@@ -2880,7 +2880,7 @@ void exit_ext4_mballoc(void)
*/ */
static noinline_for_stack int static noinline_for_stack int
ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac, ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac,
handle_t *handle) handle_t *handle, unsigned long reserv_blks)
{ {
struct buffer_head *bitmap_bh = NULL; struct buffer_head *bitmap_bh = NULL;
struct ext4_super_block *es; struct ext4_super_block *es;
...@@ -2969,21 +2969,16 @@ ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac, ...@@ -2969,21 +2969,16 @@ ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac,
le16_add_cpu(&gdp->bg_free_blocks_count, -ac->ac_b_ex.fe_len); le16_add_cpu(&gdp->bg_free_blocks_count, -ac->ac_b_ex.fe_len);
gdp->bg_checksum = ext4_group_desc_csum(sbi, ac->ac_b_ex.fe_group, gdp); gdp->bg_checksum = ext4_group_desc_csum(sbi, ac->ac_b_ex.fe_group, gdp);
spin_unlock(sb_bgl_lock(sbi, ac->ac_b_ex.fe_group)); spin_unlock(sb_bgl_lock(sbi, ac->ac_b_ex.fe_group));
percpu_counter_sub(&sbi->s_freeblocks_counter, ac->ac_b_ex.fe_len);
/* /*
* free blocks account has already be reduced/reserved * Now reduce the dirty block count also. Should not go negative
* at write_begin() time for delayed allocation
* do not double accounting
*/ */
if (!(ac->ac_flags & EXT4_MB_DELALLOC_RESERVED) && if (!(ac->ac_flags & EXT4_MB_DELALLOC_RESERVED))
ac->ac_o_ex.fe_len != ac->ac_b_ex.fe_len) { /* release all the reserved blocks if non delalloc */
/* percpu_counter_sub(&sbi->s_dirtyblocks_counter, reserv_blks);
* we allocated less blocks than we calimed else
* Add the difference back percpu_counter_sub(&sbi->s_dirtyblocks_counter,
*/ ac->ac_b_ex.fe_len);
percpu_counter_add(&sbi->s_freeblocks_counter,
ac->ac_o_ex.fe_len - ac->ac_b_ex.fe_len);
}
if (sbi->s_log_groups_per_flex) { if (sbi->s_log_groups_per_flex) {
ext4_group_t flex_group = ext4_flex_group(sbi, ext4_group_t flex_group = ext4_flex_group(sbi,
...@@ -4376,12 +4371,13 @@ static int ext4_mb_discard_preallocations(struct super_block *sb, int needed) ...@@ -4376,12 +4371,13 @@ static int ext4_mb_discard_preallocations(struct super_block *sb, int needed)
ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle, ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
struct ext4_allocation_request *ar, int *errp) struct ext4_allocation_request *ar, int *errp)
{ {
int freed;
struct ext4_allocation_context *ac = NULL; struct ext4_allocation_context *ac = NULL;
struct ext4_sb_info *sbi; struct ext4_sb_info *sbi;
struct super_block *sb; struct super_block *sb;
ext4_fsblk_t block = 0; ext4_fsblk_t block = 0;
int freed; unsigned long inquota;
int inquota; unsigned long reserv_blks = 0;
sb = ar->inode->i_sb; sb = ar->inode->i_sb;
sbi = EXT4_SB(sb); sbi = EXT4_SB(sb);
...@@ -4404,6 +4400,7 @@ ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle, ...@@ -4404,6 +4400,7 @@ ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
*errp = -ENOSPC; *errp = -ENOSPC;
return 0; return 0;
} }
reserv_blks = ar->len;
} }
while (ar->len && DQUOT_ALLOC_BLOCK(ar->inode, ar->len)) { while (ar->len && DQUOT_ALLOC_BLOCK(ar->inode, ar->len)) {
ar->flags |= EXT4_MB_HINT_NOPREALLOC; ar->flags |= EXT4_MB_HINT_NOPREALLOC;
...@@ -4450,7 +4447,7 @@ ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle, ...@@ -4450,7 +4447,7 @@ ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
} }
if (likely(ac->ac_status == AC_STATUS_FOUND)) { if (likely(ac->ac_status == AC_STATUS_FOUND)) {
*errp = ext4_mb_mark_diskspace_used(ac, handle); *errp = ext4_mb_mark_diskspace_used(ac, handle, reserv_blks);
if (*errp == -EAGAIN) { if (*errp == -EAGAIN) {
ac->ac_b_ex.fe_group = 0; ac->ac_b_ex.fe_group = 0;
ac->ac_b_ex.fe_start = 0; ac->ac_b_ex.fe_start = 0;
......
...@@ -520,6 +520,7 @@ static void ext4_put_super(struct super_block *sb) ...@@ -520,6 +520,7 @@ static void ext4_put_super(struct super_block *sb)
percpu_counter_destroy(&sbi->s_freeblocks_counter); percpu_counter_destroy(&sbi->s_freeblocks_counter);
percpu_counter_destroy(&sbi->s_freeinodes_counter); percpu_counter_destroy(&sbi->s_freeinodes_counter);
percpu_counter_destroy(&sbi->s_dirs_counter); percpu_counter_destroy(&sbi->s_dirs_counter);
percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
brelse(sbi->s_sbh); brelse(sbi->s_sbh);
#ifdef CONFIG_QUOTA #ifdef CONFIG_QUOTA
for (i = 0; i < MAXQUOTAS; i++) for (i = 0; i < MAXQUOTAS; i++)
...@@ -2259,6 +2260,9 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent) ...@@ -2259,6 +2260,9 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)
err = percpu_counter_init(&sbi->s_dirs_counter, err = percpu_counter_init(&sbi->s_dirs_counter,
ext4_count_dirs(sb)); ext4_count_dirs(sb));
} }
if (!err) {
err = percpu_counter_init(&sbi->s_dirtyblocks_counter, 0);
}
if (err) { if (err) {
printk(KERN_ERR "EXT4-fs: insufficient memory\n"); printk(KERN_ERR "EXT4-fs: insufficient memory\n");
goto failed_mount3; goto failed_mount3;
...@@ -2491,6 +2495,7 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent) ...@@ -2491,6 +2495,7 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)
percpu_counter_destroy(&sbi->s_freeblocks_counter); percpu_counter_destroy(&sbi->s_freeblocks_counter);
percpu_counter_destroy(&sbi->s_freeinodes_counter); percpu_counter_destroy(&sbi->s_freeinodes_counter);
percpu_counter_destroy(&sbi->s_dirs_counter); percpu_counter_destroy(&sbi->s_dirs_counter);
percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
failed_mount2: failed_mount2:
for (i = 0; i < db_count; i++) for (i = 0; i < db_count; i++)
brelse(sbi->s_group_desc[i]); brelse(sbi->s_group_desc[i]);
...@@ -3169,7 +3174,8 @@ static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf) ...@@ -3169,7 +3174,8 @@ static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
buf->f_type = EXT4_SUPER_MAGIC; buf->f_type = EXT4_SUPER_MAGIC;
buf->f_bsize = sb->s_blocksize; buf->f_bsize = sb->s_blocksize;
buf->f_blocks = ext4_blocks_count(es) - sbi->s_overhead_last; buf->f_blocks = ext4_blocks_count(es) - sbi->s_overhead_last;
buf->f_bfree = percpu_counter_sum_positive(&sbi->s_freeblocks_counter); buf->f_bfree = percpu_counter_sum_positive(&sbi->s_freeblocks_counter) -
percpu_counter_sum_positive(&sbi->s_dirtyblocks_counter);
ext4_free_blocks_count_set(es, buf->f_bfree); ext4_free_blocks_count_set(es, buf->f_bfree);
buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es); buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
if (buf->f_bfree < ext4_r_blocks_count(es)) if (buf->f_bfree < ext4_r_blocks_count(es))
......
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