Commit 54607494 authored by Chao Yu's avatar Chao Yu Committed by Jaegeuk Kim

f2fs: compress: fix to avoid inconsistence bewteen i_blocks and dnode

In reserve_compress_blocks(), we update blkaddrs of dnode in prior to
inc_valid_block_count(), it may cause inconsistent status bewteen
i_blocks and blkaddrs once inc_valid_block_count() fails.

To fix this issue, it needs to reverse their invoking order.

Fixes: c75488fb ("f2fs: introduce F2FS_IOC_RESERVE_COMPRESS_BLOCKS")
Reviewed-by: default avatarDaeho Jeong <daehojeong@google.com>
Signed-off-by: default avatarChao Yu <chao@kernel.org>
Signed-off-by: default avatarJaegeuk Kim <jaegeuk@kernel.org>
parent eb8fbaa5
...@@ -1219,7 +1219,8 @@ int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count) ...@@ -1219,7 +1219,8 @@ int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC))) if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
return -EPERM; return -EPERM;
if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count)))) err = inc_valid_block_count(sbi, dn->inode, &count, true);
if (unlikely(err))
return err; return err;
trace_f2fs_reserve_new_blocks(dn->inode, dn->nid, trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
...@@ -1476,7 +1477,7 @@ static int __allocate_data_block(struct dnode_of_data *dn, int seg_type) ...@@ -1476,7 +1477,7 @@ static int __allocate_data_block(struct dnode_of_data *dn, int seg_type)
dn->data_blkaddr = f2fs_data_blkaddr(dn); dn->data_blkaddr = f2fs_data_blkaddr(dn);
if (dn->data_blkaddr == NULL_ADDR) { if (dn->data_blkaddr == NULL_ADDR) {
err = inc_valid_block_count(sbi, dn->inode, &count); err = inc_valid_block_count(sbi, dn->inode, &count, true);
if (unlikely(err)) if (unlikely(err))
return err; return err;
} }
......
...@@ -2252,7 +2252,7 @@ static inline bool __allow_reserved_blocks(struct f2fs_sb_info *sbi, ...@@ -2252,7 +2252,7 @@ static inline bool __allow_reserved_blocks(struct f2fs_sb_info *sbi,
static inline void f2fs_i_blocks_write(struct inode *, block_t, bool, bool); static inline void f2fs_i_blocks_write(struct inode *, block_t, bool, bool);
static inline int inc_valid_block_count(struct f2fs_sb_info *sbi, static inline int inc_valid_block_count(struct f2fs_sb_info *sbi,
struct inode *inode, blkcnt_t *count) struct inode *inode, blkcnt_t *count, bool partial)
{ {
blkcnt_t diff = 0, release = 0; blkcnt_t diff = 0, release = 0;
block_t avail_user_block_count; block_t avail_user_block_count;
...@@ -2292,6 +2292,11 @@ static inline int inc_valid_block_count(struct f2fs_sb_info *sbi, ...@@ -2292,6 +2292,11 @@ static inline int inc_valid_block_count(struct f2fs_sb_info *sbi,
avail_user_block_count = 0; avail_user_block_count = 0;
} }
if (unlikely(sbi->total_valid_block_count > avail_user_block_count)) { if (unlikely(sbi->total_valid_block_count > avail_user_block_count)) {
if (!partial) {
spin_unlock(&sbi->stat_lock);
goto enospc;
}
diff = sbi->total_valid_block_count - avail_user_block_count; diff = sbi->total_valid_block_count - avail_user_block_count;
if (diff > *count) if (diff > *count)
diff = *count; diff = *count;
......
...@@ -3614,14 +3614,16 @@ static int reserve_compress_blocks(struct dnode_of_data *dn, pgoff_t count) ...@@ -3614,14 +3614,16 @@ static int reserve_compress_blocks(struct dnode_of_data *dn, pgoff_t count)
blkcnt_t reserved; blkcnt_t reserved;
int ret; int ret;
for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) { for (i = 0; i < cluster_size; i++) {
blkaddr = f2fs_data_blkaddr(dn); blkaddr = data_blkaddr(dn->inode, dn->node_page,
dn->ofs_in_node + i);
if (i == 0) { if (i == 0) {
if (blkaddr == COMPRESS_ADDR) if (blkaddr != COMPRESS_ADDR) {
continue; dn->ofs_in_node += cluster_size;
dn->ofs_in_node += cluster_size; goto next;
goto next; }
continue;
} }
/* /*
...@@ -3634,8 +3636,6 @@ static int reserve_compress_blocks(struct dnode_of_data *dn, pgoff_t count) ...@@ -3634,8 +3636,6 @@ static int reserve_compress_blocks(struct dnode_of_data *dn, pgoff_t count)
compr_blocks++; compr_blocks++;
continue; continue;
} }
f2fs_set_data_blkaddr(dn, NEW_ADDR);
} }
reserved = cluster_size - compr_blocks; reserved = cluster_size - compr_blocks;
...@@ -3644,12 +3644,14 @@ static int reserve_compress_blocks(struct dnode_of_data *dn, pgoff_t count) ...@@ -3644,12 +3644,14 @@ static int reserve_compress_blocks(struct dnode_of_data *dn, pgoff_t count)
if (reserved == 1) if (reserved == 1)
goto next; goto next;
ret = inc_valid_block_count(sbi, dn->inode, &reserved); ret = inc_valid_block_count(sbi, dn->inode, &reserved, false);
if (ret) if (unlikely(ret))
return ret; return ret;
if (reserved != cluster_size - compr_blocks) for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) {
return -ENOSPC; if (f2fs_data_blkaddr(dn) == NULL_ADDR)
f2fs_set_data_blkaddr(dn, NEW_ADDR);
}
f2fs_i_compr_blocks_update(dn->inode, compr_blocks, true); f2fs_i_compr_blocks_update(dn->inode, compr_blocks, true);
......
...@@ -248,7 +248,7 @@ static int __replace_atomic_write_block(struct inode *inode, pgoff_t index, ...@@ -248,7 +248,7 @@ static int __replace_atomic_write_block(struct inode *inode, pgoff_t index,
} else { } else {
blkcnt_t count = 1; blkcnt_t count = 1;
err = inc_valid_block_count(sbi, inode, &count); err = inc_valid_block_count(sbi, inode, &count, true);
if (err) { if (err) {
f2fs_put_dnode(&dn); f2fs_put_dnode(&dn);
return err; return err;
......
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