2010-11-16 11:18:19

by Namhyung Kim

[permalink] [raw]
Subject: [PATCH 1/2] ext3: Add error check in ext3_mkdir()

Check return value of ext3_journal_get_write_access, ext3_journal_dirty_metadata
and ext3_mark_inode_dirty. Consolidate error path under new label 'out_clear_inode'
and adjust bh releasing appropriately.

Signed-off-by: Namhyung Kim <[email protected]>
---
fs/ext3/namei.c | 36 ++++++++++++++++++++++--------------
1 files changed, 22 insertions(+), 14 deletions(-)

diff --git a/fs/ext3/namei.c b/fs/ext3/namei.c
index bce9dce..03fccc5 100644
--- a/fs/ext3/namei.c
+++ b/fs/ext3/namei.c
@@ -1762,7 +1762,7 @@ static int ext3_mkdir(struct inode * dir, struct dentry * dentry, int mode)
{
handle_t *handle;
struct inode * inode;
- struct buffer_head * dir_block;
+ struct buffer_head * dir_block = NULL;
struct ext3_dir_entry_2 * de;
int err, retries = 0;

@@ -1790,15 +1790,14 @@ retry:
inode->i_fop = &ext3_dir_operations;
inode->i_size = EXT3_I(inode)->i_disksize = inode->i_sb->s_blocksize;
dir_block = ext3_bread (handle, inode, 0, 1, &err);
- if (!dir_block) {
- drop_nlink(inode); /* is this nlink == 0? */
- unlock_new_inode(inode);
- ext3_mark_inode_dirty(handle, inode);
- iput (inode);
- goto out_stop;
- }
+ if (!dir_block)
+ goto out_clear_inode;
+
BUFFER_TRACE(dir_block, "get_write_access");
- ext3_journal_get_write_access(handle, dir_block);
+ err = ext3_journal_get_write_access(handle, dir_block);
+ if (err)
+ goto out_clear_inode;
+
de = (struct ext3_dir_entry_2 *) dir_block->b_data;
de->inode = cpu_to_le32(inode->i_ino);
de->name_len = 1;
@@ -1814,11 +1813,16 @@ retry:
ext3_set_de_type(dir->i_sb, de, S_IFDIR);
inode->i_nlink = 2;
BUFFER_TRACE(dir_block, "call ext3_journal_dirty_metadata");
- ext3_journal_dirty_metadata(handle, dir_block);
- brelse (dir_block);
- ext3_mark_inode_dirty(handle, inode);
- err = ext3_add_entry (handle, dentry, inode);
+ err = ext3_journal_dirty_metadata(handle, dir_block);
+ if (err)
+ goto out_clear_inode;
+
+ err = ext3_mark_inode_dirty(handle, inode);
+ if (!err)
+ err = ext3_add_entry (handle, dentry, inode);
+
if (err) {
+out_clear_inode:
inode->i_nlink = 0;
unlock_new_inode(inode);
ext3_mark_inode_dirty(handle, inode);
@@ -1827,10 +1831,14 @@ retry:
}
inc_nlink(dir);
ext3_update_dx_flag(dir);
- ext3_mark_inode_dirty(handle, dir);
+ err = ext3_mark_inode_dirty(handle, dir);
+ if (err)
+ goto out_clear_inode;
+
d_instantiate(dentry, inode);
unlock_new_inode(inode);
out_stop:
+ brelse(dir_block);
ext3_journal_stop(handle);
if (err == -ENOSPC && ext3_should_retry_alloc(dir->i_sb, &retries))
goto retry;
--
1.7.0.4


2010-11-16 11:19:56

by Namhyung Kim

[permalink] [raw]
Subject: [PATCH 2/2] ext4: Add error check in ext4_mkdir()

Check return value of ext4_journal_get_write_access, ext4_journal_dirty_metadata
and ext4_mark_inode_dirty. Move brelse() under 'out_stop' to release bh properly
in case of journal error.

Signed-off-by: Namhyung Kim <[email protected]>
---
fs/ext4/namei.c | 21 ++++++++++++++-------
1 files changed, 14 insertions(+), 7 deletions(-)

diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
index 92203b8..a09fbbd 100644
--- a/fs/ext4/namei.c
+++ b/fs/ext4/namei.c
@@ -1789,7 +1789,7 @@ static int ext4_mkdir(struct inode *dir, struct dentry *dentry, int mode)
{
handle_t *handle;
struct inode *inode;
- struct buffer_head *dir_block;
+ struct buffer_head *dir_block = NULL;
struct ext4_dir_entry_2 *de;
unsigned int blocksize = dir->i_sb->s_blocksize;
int err, retries = 0;
@@ -1822,7 +1822,9 @@ retry:
if (!dir_block)
goto out_clear_inode;
BUFFER_TRACE(dir_block, "get_write_access");
- ext4_journal_get_write_access(handle, dir_block);
+ err = ext4_journal_get_write_access(handle, dir_block);
+ if (err)
+ goto out_clear_inode;
de = (struct ext4_dir_entry_2 *) dir_block->b_data;
de->inode = cpu_to_le32(inode->i_ino);
de->name_len = 1;
@@ -1839,10 +1841,12 @@ retry:
ext4_set_de_type(dir->i_sb, de, S_IFDIR);
inode->i_nlink = 2;
BUFFER_TRACE(dir_block, "call ext4_handle_dirty_metadata");
- ext4_handle_dirty_metadata(handle, dir, dir_block);
- brelse(dir_block);
- ext4_mark_inode_dirty(handle, inode);
- err = ext4_add_entry(handle, dentry, inode);
+ err = ext4_handle_dirty_metadata(handle, dir, dir_block);
+ if (err)
+ goto out_clear_inode;
+ err = ext4_mark_inode_dirty(handle, inode);
+ if (!err)
+ err = ext4_add_entry(handle, dentry, inode);
if (err) {
out_clear_inode:
clear_nlink(inode);
@@ -1853,10 +1857,13 @@ out_clear_inode:
}
ext4_inc_count(handle, dir);
ext4_update_dx_flag(dir);
- ext4_mark_inode_dirty(handle, dir);
+ err = ext4_mark_inode_dirty(handle, dir);
+ if (err)
+ goto out_clear_inode;
d_instantiate(dentry, inode);
unlock_new_inode(inode);
out_stop:
+ brelse(dir_block);
ext4_journal_stop(handle);
if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
goto retry;
--
1.7.0.4

2010-11-16 13:28:22

by Jan Kara

[permalink] [raw]
Subject: Re: [PATCH 1/2] ext3: Add error check in ext3_mkdir()

On Tue 16-11-10 20:18:12, Namhyung Kim wrote:
> Check return value of ext3_journal_get_write_access, ext3_journal_dirty_metadata
> and ext3_mark_inode_dirty. Consolidate error path under new label 'out_clear_inode'
> and adjust bh releasing appropriately.
Thanks. Merged.

Honza

>
> Signed-off-by: Namhyung Kim <[email protected]>
> ---
> fs/ext3/namei.c | 36 ++++++++++++++++++++++--------------
> 1 files changed, 22 insertions(+), 14 deletions(-)
>
> diff --git a/fs/ext3/namei.c b/fs/ext3/namei.c
> index bce9dce..03fccc5 100644
> --- a/fs/ext3/namei.c
> +++ b/fs/ext3/namei.c
> @@ -1762,7 +1762,7 @@ static int ext3_mkdir(struct inode * dir, struct dentry * dentry, int mode)
> {
> handle_t *handle;
> struct inode * inode;
> - struct buffer_head * dir_block;
> + struct buffer_head * dir_block = NULL;
> struct ext3_dir_entry_2 * de;
> int err, retries = 0;
>
> @@ -1790,15 +1790,14 @@ retry:
> inode->i_fop = &ext3_dir_operations;
> inode->i_size = EXT3_I(inode)->i_disksize = inode->i_sb->s_blocksize;
> dir_block = ext3_bread (handle, inode, 0, 1, &err);
> - if (!dir_block) {
> - drop_nlink(inode); /* is this nlink == 0? */
> - unlock_new_inode(inode);
> - ext3_mark_inode_dirty(handle, inode);
> - iput (inode);
> - goto out_stop;
> - }
> + if (!dir_block)
> + goto out_clear_inode;
> +
> BUFFER_TRACE(dir_block, "get_write_access");
> - ext3_journal_get_write_access(handle, dir_block);
> + err = ext3_journal_get_write_access(handle, dir_block);
> + if (err)
> + goto out_clear_inode;
> +
> de = (struct ext3_dir_entry_2 *) dir_block->b_data;
> de->inode = cpu_to_le32(inode->i_ino);
> de->name_len = 1;
> @@ -1814,11 +1813,16 @@ retry:
> ext3_set_de_type(dir->i_sb, de, S_IFDIR);
> inode->i_nlink = 2;
> BUFFER_TRACE(dir_block, "call ext3_journal_dirty_metadata");
> - ext3_journal_dirty_metadata(handle, dir_block);
> - brelse (dir_block);
> - ext3_mark_inode_dirty(handle, inode);
> - err = ext3_add_entry (handle, dentry, inode);
> + err = ext3_journal_dirty_metadata(handle, dir_block);
> + if (err)
> + goto out_clear_inode;
> +
> + err = ext3_mark_inode_dirty(handle, inode);
> + if (!err)
> + err = ext3_add_entry (handle, dentry, inode);
> +
> if (err) {
> +out_clear_inode:
> inode->i_nlink = 0;
> unlock_new_inode(inode);
> ext3_mark_inode_dirty(handle, inode);
> @@ -1827,10 +1831,14 @@ retry:
> }
> inc_nlink(dir);
> ext3_update_dx_flag(dir);
> - ext3_mark_inode_dirty(handle, dir);
> + err = ext3_mark_inode_dirty(handle, dir);
> + if (err)
> + goto out_clear_inode;
> +
> d_instantiate(dentry, inode);
> unlock_new_inode(inode);
> out_stop:
> + brelse(dir_block);
> ext3_journal_stop(handle);
> if (err == -ENOSPC && ext3_should_retry_alloc(dir->i_sb, &retries))
> goto retry;
> --
> 1.7.0.4
>
--
Jan Kara <[email protected]>
SUSE Labs, CR

2010-12-30 13:30:58

by Theodore Ts'o

[permalink] [raw]
Subject: Re: [PATCH 2/2] ext4: Add error check in ext4_mkdir()

On Tue, Nov 16, 2010 at 08:19:52PM +0900, Namhyung Kim wrote:
> Check return value of ext4_journal_get_write_access,
> ext4_journal_dirty_metadata and ext4_mark_inode_dirty. Move brelse()
> under 'out_stop' to release bh properly in case of journal error.
>
> Signed-off-by: Namhyung Kim <[email protected]>

Thanks, added to the ext4 patch tree.

- Ted