Commit 1139575a authored by Theodore Ts'o's avatar Theodore Ts'o

ext4: start handle at the last possible moment when creating inodes

In ext4_{create,mknod,mkdir,symlink}(), don't start the journal handle
until the inode has been succesfully allocated.  In order to do this,
we need to start the handle in the ext4_new_inode().  So create a new
variant of this function, ext4_new_inode_start_handle(), so the handle
can be created at the last possible minute, before we need to modify
the inode allocation bitmap block.
Signed-off-by: default avatar"Theodore Ts'o" <tytso@mit.edu>
parent 95eaefbd
...@@ -2004,9 +2004,20 @@ extern int ext4fs_dirhash(const char *name, int len, struct ...@@ -2004,9 +2004,20 @@ extern int ext4fs_dirhash(const char *name, int len, struct
dx_hash_info *hinfo); dx_hash_info *hinfo);
/* ialloc.c */ /* ialloc.c */
extern struct inode *ext4_new_inode(handle_t *, struct inode *, umode_t, extern struct inode *__ext4_new_inode(handle_t *, struct inode *, umode_t,
const struct qstr *qstr, __u32 goal, const struct qstr *qstr, __u32 goal,
uid_t *owner); uid_t *owner, int handle_type,
unsigned int line_no, int nblocks);
#define ext4_new_inode(handle, dir, mode, qstr, goal, owner) \
__ext4_new_inode((handle), (dir), (mode), (qstr), (goal), (owner), \
0, 0, 0)
#define ext4_new_inode_start_handle(dir, mode, qstr, goal, owner, \
type, nblocks) \
__ext4_new_inode(NULL, (dir), (mode), (qstr), (goal), (owner), \
(type), __LINE__, (nblocks))
extern void ext4_free_inode(handle_t *, struct inode *); extern void ext4_free_inode(handle_t *, struct inode *);
extern struct inode * ext4_orphan_get(struct super_block *, unsigned long); extern struct inode * ext4_orphan_get(struct super_block *, unsigned long);
extern unsigned long ext4_count_free_inodes(struct super_block *); extern unsigned long ext4_count_free_inodes(struct super_block *);
......
...@@ -634,8 +634,10 @@ static int find_group_other(struct super_block *sb, struct inode *parent, ...@@ -634,8 +634,10 @@ static int find_group_other(struct super_block *sb, struct inode *parent,
* For other inodes, search forward from the parent directory's block * For other inodes, search forward from the parent directory's block
* group to find a free inode. * group to find a free inode.
*/ */
struct inode *ext4_new_inode(handle_t *handle, struct inode *dir, umode_t mode, struct inode *__ext4_new_inode(handle_t *handle, struct inode *dir,
const struct qstr *qstr, __u32 goal, uid_t *owner) umode_t mode, const struct qstr *qstr,
__u32 goal, uid_t *owner, int handle_type,
unsigned int line_no, int nblocks)
{ {
struct super_block *sb; struct super_block *sb;
struct buffer_head *inode_bitmap_bh = NULL; struct buffer_head *inode_bitmap_bh = NULL;
...@@ -725,6 +727,15 @@ struct inode *ext4_new_inode(handle_t *handle, struct inode *dir, umode_t mode, ...@@ -725,6 +727,15 @@ struct inode *ext4_new_inode(handle_t *handle, struct inode *dir, umode_t mode,
"inode=%lu", ino + 1); "inode=%lu", ino + 1);
continue; continue;
} }
if (!handle) {
BUG_ON(nblocks <= 0);
handle = __ext4_journal_start_sb(dir->i_sb, line_no,
handle_type, nblocks);
if (IS_ERR(handle)) {
err = PTR_ERR(handle);
goto fail;
}
}
BUFFER_TRACE(inode_bitmap_bh, "get_write_access"); BUFFER_TRACE(inode_bitmap_bh, "get_write_access");
err = ext4_journal_get_write_access(handle, inode_bitmap_bh); err = ext4_journal_get_write_access(handle, inode_bitmap_bh);
if (err) if (err)
......
...@@ -2257,30 +2257,28 @@ static int ext4_create(struct inode *dir, struct dentry *dentry, umode_t mode, ...@@ -2257,30 +2257,28 @@ static int ext4_create(struct inode *dir, struct dentry *dentry, umode_t mode,
{ {
handle_t *handle; handle_t *handle;
struct inode *inode; struct inode *inode;
int err, retries = 0; int err, credits, retries = 0;
dquot_initialize(dir); dquot_initialize(dir);
credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb));
retry: retry:
handle = ext4_journal_start(dir, EXT4_HT_DIR, inode = ext4_new_inode_start_handle(dir, mode, &dentry->d_name, 0,
(EXT4_DATA_TRANS_BLOCKS(dir->i_sb) + NULL, EXT4_HT_DIR, credits);
EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 + handle = ext4_journal_current_handle();
EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb)));
if (IS_ERR(handle))
return PTR_ERR(handle);
if (IS_DIRSYNC(dir))
ext4_handle_sync(handle);
inode = ext4_new_inode(handle, dir, mode, &dentry->d_name, 0, NULL);
err = PTR_ERR(inode); err = PTR_ERR(inode);
if (!IS_ERR(inode)) { if (!IS_ERR(inode)) {
inode->i_op = &ext4_file_inode_operations; inode->i_op = &ext4_file_inode_operations;
inode->i_fop = &ext4_file_operations; inode->i_fop = &ext4_file_operations;
ext4_set_aops(inode); ext4_set_aops(inode);
err = ext4_add_nondir(handle, dentry, inode); err = ext4_add_nondir(handle, dentry, inode);
if (!err && IS_DIRSYNC(dir))
ext4_handle_sync(handle);
} }
ext4_journal_stop(handle); if (handle)
ext4_journal_stop(handle);
if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries)) if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
goto retry; goto retry;
return err; return err;
...@@ -2291,32 +2289,30 @@ static int ext4_mknod(struct inode *dir, struct dentry *dentry, ...@@ -2291,32 +2289,30 @@ static int ext4_mknod(struct inode *dir, struct dentry *dentry,
{ {
handle_t *handle; handle_t *handle;
struct inode *inode; struct inode *inode;
int err, retries = 0; int err, credits, retries = 0;
if (!new_valid_dev(rdev)) if (!new_valid_dev(rdev))
return -EINVAL; return -EINVAL;
dquot_initialize(dir); dquot_initialize(dir);
credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb));
retry: retry:
handle = ext4_journal_start(dir, EXT4_HT_DIR, inode = ext4_new_inode_start_handle(dir, mode, &dentry->d_name, 0,
(EXT4_DATA_TRANS_BLOCKS(dir->i_sb) + NULL, EXT4_HT_DIR, credits);
EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 + handle = ext4_journal_current_handle();
EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb)));
if (IS_ERR(handle))
return PTR_ERR(handle);
if (IS_DIRSYNC(dir))
ext4_handle_sync(handle);
inode = ext4_new_inode(handle, dir, mode, &dentry->d_name, 0, NULL);
err = PTR_ERR(inode); err = PTR_ERR(inode);
if (!IS_ERR(inode)) { if (!IS_ERR(inode)) {
init_special_inode(inode, inode->i_mode, rdev); init_special_inode(inode, inode->i_mode, rdev);
inode->i_op = &ext4_special_inode_operations; inode->i_op = &ext4_special_inode_operations;
err = ext4_add_nondir(handle, dentry, inode); err = ext4_add_nondir(handle, dentry, inode);
if (!err && IS_DIRSYNC(dir))
ext4_handle_sync(handle);
} }
ext4_journal_stop(handle); if (handle)
ext4_journal_stop(handle);
if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries)) if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
goto retry; goto retry;
return err; return err;
...@@ -2408,26 +2404,21 @@ static int ext4_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) ...@@ -2408,26 +2404,21 @@ static int ext4_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
{ {
handle_t *handle; handle_t *handle;
struct inode *inode; struct inode *inode;
int err, retries = 0; int err, credits, retries = 0;
if (EXT4_DIR_LINK_MAX(dir)) if (EXT4_DIR_LINK_MAX(dir))
return -EMLINK; return -EMLINK;
dquot_initialize(dir); dquot_initialize(dir);
credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb));
retry: retry:
handle = ext4_journal_start(dir, EXT4_HT_DIR, inode = ext4_new_inode_start_handle(dir, S_IFDIR | mode,
(EXT4_DATA_TRANS_BLOCKS(dir->i_sb) + &dentry->d_name,
EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 + 0, NULL, EXT4_HT_DIR, credits);
EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb))); handle = ext4_journal_current_handle();
if (IS_ERR(handle))
return PTR_ERR(handle);
if (IS_DIRSYNC(dir))
ext4_handle_sync(handle);
inode = ext4_new_inode(handle, dir, S_IFDIR | mode,
&dentry->d_name, 0, NULL);
err = PTR_ERR(inode); err = PTR_ERR(inode);
if (IS_ERR(inode)) if (IS_ERR(inode))
goto out_stop; goto out_stop;
...@@ -2455,8 +2446,12 @@ static int ext4_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) ...@@ -2455,8 +2446,12 @@ static int ext4_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
goto out_clear_inode; goto out_clear_inode;
unlock_new_inode(inode); unlock_new_inode(inode);
d_instantiate(dentry, inode); d_instantiate(dentry, inode);
if (IS_DIRSYNC(dir))
ext4_handle_sync(handle);
out_stop: out_stop:
ext4_journal_stop(handle); if (handle)
ext4_journal_stop(handle);
if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries)) if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
goto retry; goto retry;
return err; return err;
...@@ -2883,15 +2878,10 @@ static int ext4_symlink(struct inode *dir, ...@@ -2883,15 +2878,10 @@ static int ext4_symlink(struct inode *dir,
EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb); EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb);
} }
retry: retry:
handle = ext4_journal_start(dir, EXT4_HT_DIR, credits); inode = ext4_new_inode_start_handle(dir, S_IFLNK|S_IRWXUGO,
if (IS_ERR(handle)) &dentry->d_name, 0, NULL,
return PTR_ERR(handle); EXT4_HT_DIR, credits);
handle = ext4_journal_current_handle();
if (IS_DIRSYNC(dir))
ext4_handle_sync(handle);
inode = ext4_new_inode(handle, dir, S_IFLNK|S_IRWXUGO,
&dentry->d_name, 0, NULL);
err = PTR_ERR(inode); err = PTR_ERR(inode);
if (IS_ERR(inode)) if (IS_ERR(inode))
goto out_stop; goto out_stop;
...@@ -2944,8 +2934,12 @@ static int ext4_symlink(struct inode *dir, ...@@ -2944,8 +2934,12 @@ static int ext4_symlink(struct inode *dir,
} }
EXT4_I(inode)->i_disksize = inode->i_size; EXT4_I(inode)->i_disksize = inode->i_size;
err = ext4_add_nondir(handle, dentry, inode); err = ext4_add_nondir(handle, dentry, inode);
if (!err && IS_DIRSYNC(dir))
ext4_handle_sync(handle);
out_stop: out_stop:
ext4_journal_stop(handle); if (handle)
ext4_journal_stop(handle);
if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries)) if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
goto retry; goto retry;
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