f2fs: don't count inode block in in-memory inode.i_blocks

Previously, we count all inode consumed blocks including inode block,
xattr block, index block, data block into i_blocks, for other generic
filesystems, they won't count inode block into i_blocks, so for
userspace applications or quota system, they may detect incorrect block
count according to i_blocks value in inode.

This patch changes to count all blocks into inode.i_blocks excluding
inode block, for on-disk i_blocks, we keep counting inode block for
backward compatibility.

Signed-off-by: Chao Yu <yuchao0@huawei.com>
Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
This commit is contained in:
Chao Yu 2017-07-06 01:11:31 +08:00 committed by Jaegeuk Kim
parent 6ac851ba89
commit 000519f278
3 changed files with 20 additions and 22 deletions

View File

@ -1355,8 +1355,6 @@ static inline int check_nid_range(struct f2fs_sb_info *sbi, nid_t nid)
return 0; return 0;
} }
#define F2FS_DEFAULT_ALLOCATED_BLOCKS 1
/* /*
* Check whether the inode has blocks or not * Check whether the inode has blocks or not
*/ */
@ -1364,8 +1362,7 @@ static inline int F2FS_HAS_BLOCKS(struct inode *inode)
{ {
block_t xattr_block = F2FS_I(inode)->i_xattr_nid ? 1 : 0; block_t xattr_block = F2FS_I(inode)->i_xattr_nid ? 1 : 0;
return (inode->i_blocks >> F2FS_LOG_SECTORS_PER_BLOCK) > return (inode->i_blocks >> F2FS_LOG_SECTORS_PER_BLOCK) > xattr_block;
(F2FS_DEFAULT_ALLOCATED_BLOCKS + xattr_block);
} }
static inline bool f2fs_has_xattr_block(unsigned int ofs) static inline bool f2fs_has_xattr_block(unsigned int ofs)
@ -1552,7 +1549,7 @@ static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
} }
static inline bool inc_valid_node_count(struct f2fs_sb_info *sbi, static inline bool inc_valid_node_count(struct f2fs_sb_info *sbi,
struct inode *inode) struct inode *inode, bool is_inode)
{ {
block_t valid_block_count; block_t valid_block_count;
unsigned int valid_node_count; unsigned int valid_node_count;
@ -1572,8 +1569,12 @@ static inline bool inc_valid_node_count(struct f2fs_sb_info *sbi,
return false; return false;
} }
if (inode) if (inode) {
f2fs_i_blocks_write(inode, 1, true); if (is_inode)
f2fs_mark_inode_dirty_sync(inode, true);
else
f2fs_i_blocks_write(inode, 1, true);
}
sbi->total_valid_node_count++; sbi->total_valid_node_count++;
sbi->total_valid_block_count++; sbi->total_valid_block_count++;
@ -1584,15 +1585,16 @@ static inline bool inc_valid_node_count(struct f2fs_sb_info *sbi,
} }
static inline void dec_valid_node_count(struct f2fs_sb_info *sbi, static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
struct inode *inode) struct inode *inode, bool is_inode)
{ {
spin_lock(&sbi->stat_lock); spin_lock(&sbi->stat_lock);
f2fs_bug_on(sbi, !sbi->total_valid_block_count); f2fs_bug_on(sbi, !sbi->total_valid_block_count);
f2fs_bug_on(sbi, !sbi->total_valid_node_count); f2fs_bug_on(sbi, !sbi->total_valid_node_count);
f2fs_bug_on(sbi, !inode->i_blocks); f2fs_bug_on(sbi, !is_inode && !inode->i_blocks);
f2fs_i_blocks_write(inode, 1, false); if (!is_inode)
f2fs_i_blocks_write(inode, 1, false);
sbi->total_valid_node_count--; sbi->total_valid_node_count--;
sbi->total_valid_block_count--; sbi->total_valid_block_count--;

View File

@ -130,7 +130,7 @@ static int do_read_inode(struct inode *inode)
i_gid_write(inode, le32_to_cpu(ri->i_gid)); i_gid_write(inode, le32_to_cpu(ri->i_gid));
set_nlink(inode, le32_to_cpu(ri->i_links)); set_nlink(inode, le32_to_cpu(ri->i_links));
inode->i_size = le64_to_cpu(ri->i_size); inode->i_size = le64_to_cpu(ri->i_size);
inode->i_blocks = SECTOR_FROM_BLOCK(le64_to_cpu(ri->i_blocks)); inode->i_blocks = SECTOR_FROM_BLOCK(le64_to_cpu(ri->i_blocks) - 1);
inode->i_atime.tv_sec = le64_to_cpu(ri->i_atime); inode->i_atime.tv_sec = le64_to_cpu(ri->i_atime);
inode->i_ctime.tv_sec = le64_to_cpu(ri->i_ctime); inode->i_ctime.tv_sec = le64_to_cpu(ri->i_ctime);
@ -268,7 +268,7 @@ int update_inode(struct inode *inode, struct page *node_page)
ri->i_gid = cpu_to_le32(i_gid_read(inode)); ri->i_gid = cpu_to_le32(i_gid_read(inode));
ri->i_links = cpu_to_le32(inode->i_nlink); ri->i_links = cpu_to_le32(inode->i_nlink);
ri->i_size = cpu_to_le64(i_size_read(inode)); ri->i_size = cpu_to_le64(i_size_read(inode));
ri->i_blocks = cpu_to_le64(SECTOR_TO_BLOCK(inode->i_blocks)); ri->i_blocks = cpu_to_le64(SECTOR_TO_BLOCK(inode->i_blocks) + 1);
if (et) { if (et) {
read_lock(&et->lock); read_lock(&et->lock);

View File

@ -678,15 +678,11 @@ static void truncate_node(struct dnode_of_data *dn)
struct node_info ni; struct node_info ni;
get_node_info(sbi, dn->nid, &ni); get_node_info(sbi, dn->nid, &ni);
if (dn->inode->i_blocks == 0) {
f2fs_bug_on(sbi, ni.blk_addr != NULL_ADDR);
goto invalidate;
}
f2fs_bug_on(sbi, ni.blk_addr == NULL_ADDR); f2fs_bug_on(sbi, ni.blk_addr == NULL_ADDR);
/* Deallocate node address */ /* Deallocate node address */
invalidate_blocks(sbi, ni.blk_addr); invalidate_blocks(sbi, ni.blk_addr);
dec_valid_node_count(sbi, dn->inode); dec_valid_node_count(sbi, dn->inode, dn->nid == dn->inode->i_ino);
set_node_addr(sbi, &ni, NULL_ADDR, false); set_node_addr(sbi, &ni, NULL_ADDR, false);
if (dn->nid == dn->inode->i_ino) { if (dn->nid == dn->inode->i_ino) {
@ -694,7 +690,7 @@ static void truncate_node(struct dnode_of_data *dn)
dec_valid_inode_count(sbi); dec_valid_inode_count(sbi);
f2fs_inode_synced(dn->inode); f2fs_inode_synced(dn->inode);
} }
invalidate:
clear_node_page_dirty(dn->node_page); clear_node_page_dirty(dn->node_page);
set_sbi_flag(sbi, SBI_IS_DIRTY); set_sbi_flag(sbi, SBI_IS_DIRTY);
@ -1044,7 +1040,7 @@ struct page *new_node_page(struct dnode_of_data *dn,
if (!page) if (!page)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
if (unlikely(!inc_valid_node_count(sbi, dn->inode))) { if (unlikely(!inc_valid_node_count(sbi, dn->inode, !ofs))) {
err = -ENOSPC; err = -ENOSPC;
goto fail; goto fail;
} }
@ -2207,14 +2203,14 @@ int recover_xattr_data(struct inode *inode, struct page *page, block_t blkaddr)
get_node_info(sbi, prev_xnid, &ni); get_node_info(sbi, prev_xnid, &ni);
f2fs_bug_on(sbi, ni.blk_addr == NULL_ADDR); f2fs_bug_on(sbi, ni.blk_addr == NULL_ADDR);
invalidate_blocks(sbi, ni.blk_addr); invalidate_blocks(sbi, ni.blk_addr);
dec_valid_node_count(sbi, inode); dec_valid_node_count(sbi, inode, false);
set_node_addr(sbi, &ni, NULL_ADDR, false); set_node_addr(sbi, &ni, NULL_ADDR, false);
recover_xnid: recover_xnid:
/* 2: update xattr nid in inode */ /* 2: update xattr nid in inode */
remove_free_nid(sbi, new_xnid); remove_free_nid(sbi, new_xnid);
f2fs_i_xnid_write(inode, new_xnid); f2fs_i_xnid_write(inode, new_xnid);
if (unlikely(!inc_valid_node_count(sbi, inode))) if (unlikely(!inc_valid_node_count(sbi, inode, false)))
f2fs_bug_on(sbi, 1); f2fs_bug_on(sbi, 1);
update_inode_page(inode); update_inode_page(inode);
@ -2272,7 +2268,7 @@ int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page)
new_ni = old_ni; new_ni = old_ni;
new_ni.ino = ino; new_ni.ino = ino;
if (unlikely(!inc_valid_node_count(sbi, NULL))) if (unlikely(!inc_valid_node_count(sbi, NULL, true)))
WARN_ON(1); WARN_ON(1);
set_node_addr(sbi, &new_ni, NEW_ADDR, false); set_node_addr(sbi, &new_ni, NEW_ADDR, false);
inc_valid_inode_count(sbi); inc_valid_inode_count(sbi);