Commit 03bb7588 authored by Al Viro's avatar Al Viro Committed by Greg Kroah-Hartman

do d_instantiate/unlock_new_inode combinations safely

commit 1e2e547a upstream.

For anything NFS-exported we do _not_ want to unlock new inode
before it has grown an alias; original set of fixes got the
ordering right, but missed the nasty complication in case of
lockdep being enabled - unlock_new_inode() does
	lockdep_annotate_inode_mutex_key(inode)
which can only be done before anyone gets a chance to touch
->i_mutex.  Unfortunately, flipping the order and doing
unlock_new_inode() before d_instantiate() opens a window when
mkdir can race with open-by-fhandle on a guessed fhandle, leading
to multiple aliases for a directory inode and all the breakage
that follows from that.

	Correct solution: a new primitive (d_instantiate_new())
combining these two in the right order - lockdep annotate, then
d_instantiate(), then the rest of unlock_new_inode().  All
combinations of d_instantiate() with unlock_new_inode() should
be converted to that.

Cc: stable@kernel.org	# 2.6.29 and later
Tested-by: default avatarMike Marshall <hubcap@omnibond.com>
Reviewed-by: default avatarAndreas Dilger <adilger@dilger.ca>
Signed-off-by: default avatarAl Viro <viro@zeniv.linux.org.uk>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 1a54258e
...@@ -6413,8 +6413,7 @@ static int btrfs_mknod(struct inode *dir, struct dentry *dentry, ...@@ -6413,8 +6413,7 @@ static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
goto out_unlock_inode; goto out_unlock_inode;
} else { } else {
btrfs_update_inode(trans, root, inode); btrfs_update_inode(trans, root, inode);
unlock_new_inode(inode); d_instantiate_new(dentry, inode);
d_instantiate(dentry, inode);
} }
out_unlock: out_unlock:
...@@ -6489,8 +6488,7 @@ static int btrfs_create(struct inode *dir, struct dentry *dentry, ...@@ -6489,8 +6488,7 @@ static int btrfs_create(struct inode *dir, struct dentry *dentry,
goto out_unlock_inode; goto out_unlock_inode;
BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
unlock_new_inode(inode); d_instantiate_new(dentry, inode);
d_instantiate(dentry, inode);
out_unlock: out_unlock:
btrfs_end_transaction(trans, root); btrfs_end_transaction(trans, root);
...@@ -6633,12 +6631,7 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) ...@@ -6633,12 +6631,7 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
if (err) if (err)
goto out_fail_inode; goto out_fail_inode;
d_instantiate(dentry, inode); d_instantiate_new(dentry, inode);
/*
* mkdir is special. We're unlocking after we call d_instantiate
* to avoid a race with nfsd calling d_instantiate.
*/
unlock_new_inode(inode);
drop_on_err = 0; drop_on_err = 0;
out_fail: out_fail:
...@@ -9789,8 +9782,7 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry, ...@@ -9789,8 +9782,7 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
goto out_unlock_inode; goto out_unlock_inode;
} }
unlock_new_inode(inode); d_instantiate_new(dentry, inode);
d_instantiate(dentry, inode);
out_unlock: out_unlock:
btrfs_end_transaction(trans, root); btrfs_end_transaction(trans, root);
......
...@@ -1897,6 +1897,28 @@ struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode) ...@@ -1897,6 +1897,28 @@ struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
EXPORT_SYMBOL(d_instantiate_unique); EXPORT_SYMBOL(d_instantiate_unique);
/*
* This should be equivalent to d_instantiate() + unlock_new_inode(),
* with lockdep-related part of unlock_new_inode() done before
* anything else. Use that instead of open-coding d_instantiate()/
* unlock_new_inode() combinations.
*/
void d_instantiate_new(struct dentry *entry, struct inode *inode)
{
BUG_ON(!hlist_unhashed(&entry->d_u.d_alias));
BUG_ON(!inode);
lockdep_annotate_inode_mutex_key(inode);
security_d_instantiate(entry, inode);
spin_lock(&inode->i_lock);
__d_instantiate(entry, inode);
WARN_ON(!(inode->i_state & I_NEW));
inode->i_state &= ~I_NEW;
smp_mb();
wake_up_bit(&inode->i_state, __I_NEW);
spin_unlock(&inode->i_lock);
}
EXPORT_SYMBOL(d_instantiate_new);
/** /**
* d_instantiate_no_diralias - instantiate a non-aliased dentry * d_instantiate_no_diralias - instantiate a non-aliased dentry
* @entry: dentry to complete * @entry: dentry to complete
......
...@@ -287,8 +287,7 @@ ecryptfs_create(struct inode *directory_inode, struct dentry *ecryptfs_dentry, ...@@ -287,8 +287,7 @@ ecryptfs_create(struct inode *directory_inode, struct dentry *ecryptfs_dentry,
iput(ecryptfs_inode); iput(ecryptfs_inode);
goto out; goto out;
} }
unlock_new_inode(ecryptfs_inode); d_instantiate_new(ecryptfs_dentry, ecryptfs_inode);
d_instantiate(ecryptfs_dentry, ecryptfs_inode);
out: out:
return rc; return rc;
} }
......
...@@ -40,8 +40,7 @@ static inline int ext2_add_nondir(struct dentry *dentry, struct inode *inode) ...@@ -40,8 +40,7 @@ static inline int ext2_add_nondir(struct dentry *dentry, struct inode *inode)
{ {
int err = ext2_add_link(dentry, inode); int err = ext2_add_link(dentry, inode);
if (!err) { if (!err) {
unlock_new_inode(inode); d_instantiate_new(dentry, inode);
d_instantiate(dentry, inode);
return 0; return 0;
} }
inode_dec_link_count(inode); inode_dec_link_count(inode);
...@@ -267,8 +266,7 @@ static int ext2_mkdir(struct inode * dir, struct dentry * dentry, umode_t mode) ...@@ -267,8 +266,7 @@ static int ext2_mkdir(struct inode * dir, struct dentry * dentry, umode_t mode)
if (err) if (err)
goto out_fail; goto out_fail;
unlock_new_inode(inode); d_instantiate_new(dentry, inode);
d_instantiate(dentry, inode);
out: out:
return err; return err;
......
...@@ -2429,8 +2429,7 @@ static int ext4_add_nondir(handle_t *handle, ...@@ -2429,8 +2429,7 @@ static int ext4_add_nondir(handle_t *handle,
int err = ext4_add_entry(handle, dentry, inode); int err = ext4_add_entry(handle, dentry, inode);
if (!err) { if (!err) {
ext4_mark_inode_dirty(handle, inode); ext4_mark_inode_dirty(handle, inode);
unlock_new_inode(inode); d_instantiate_new(dentry, inode);
d_instantiate(dentry, inode);
return 0; return 0;
} }
drop_nlink(inode); drop_nlink(inode);
...@@ -2669,8 +2668,7 @@ static int ext4_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) ...@@ -2669,8 +2668,7 @@ static int ext4_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
err = ext4_mark_inode_dirty(handle, dir); err = ext4_mark_inode_dirty(handle, dir);
if (err) if (err)
goto out_clear_inode; goto out_clear_inode;
unlock_new_inode(inode); d_instantiate_new(dentry, inode);
d_instantiate(dentry, inode);
if (IS_DIRSYNC(dir)) if (IS_DIRSYNC(dir))
ext4_handle_sync(handle); ext4_handle_sync(handle);
......
...@@ -150,8 +150,7 @@ static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode, ...@@ -150,8 +150,7 @@ static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
alloc_nid_done(sbi, ino); alloc_nid_done(sbi, ino);
d_instantiate(dentry, inode); d_instantiate_new(dentry, inode);
unlock_new_inode(inode);
if (IS_DIRSYNC(dir)) if (IS_DIRSYNC(dir))
f2fs_sync_fs(sbi->sb, 1); f2fs_sync_fs(sbi->sb, 1);
...@@ -399,8 +398,7 @@ static int f2fs_symlink(struct inode *dir, struct dentry *dentry, ...@@ -399,8 +398,7 @@ static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
err = page_symlink(inode, p_str, p_len); err = page_symlink(inode, p_str, p_len);
err_out: err_out:
d_instantiate(dentry, inode); d_instantiate_new(dentry, inode);
unlock_new_inode(inode);
/* /*
* Let's flush symlink data in order to avoid broken symlink as much as * Let's flush symlink data in order to avoid broken symlink as much as
...@@ -454,8 +452,7 @@ static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) ...@@ -454,8 +452,7 @@ static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
alloc_nid_done(sbi, inode->i_ino); alloc_nid_done(sbi, inode->i_ino);
d_instantiate(dentry, inode); d_instantiate_new(dentry, inode);
unlock_new_inode(inode);
if (IS_DIRSYNC(dir)) if (IS_DIRSYNC(dir))
f2fs_sync_fs(sbi->sb, 1); f2fs_sync_fs(sbi->sb, 1);
...@@ -499,8 +496,7 @@ static int f2fs_mknod(struct inode *dir, struct dentry *dentry, ...@@ -499,8 +496,7 @@ static int f2fs_mknod(struct inode *dir, struct dentry *dentry,
alloc_nid_done(sbi, inode->i_ino); alloc_nid_done(sbi, inode->i_ino);
d_instantiate(dentry, inode); d_instantiate_new(dentry, inode);
unlock_new_inode(inode);
if (IS_DIRSYNC(dir)) if (IS_DIRSYNC(dir))
f2fs_sync_fs(sbi->sb, 1); f2fs_sync_fs(sbi->sb, 1);
......
...@@ -207,8 +207,7 @@ static int jffs2_create(struct inode *dir_i, struct dentry *dentry, ...@@ -207,8 +207,7 @@ static int jffs2_create(struct inode *dir_i, struct dentry *dentry,
__func__, inode->i_ino, inode->i_mode, inode->i_nlink, __func__, inode->i_ino, inode->i_mode, inode->i_nlink,
f->inocache->pino_nlink, inode->i_mapping->nrpages); f->inocache->pino_nlink, inode->i_mapping->nrpages);
unlock_new_inode(inode); d_instantiate_new(dentry, inode);
d_instantiate(dentry, inode);
return 0; return 0;
fail: fail:
...@@ -428,8 +427,7 @@ static int jffs2_symlink (struct inode *dir_i, struct dentry *dentry, const char ...@@ -428,8 +427,7 @@ static int jffs2_symlink (struct inode *dir_i, struct dentry *dentry, const char
mutex_unlock(&dir_f->sem); mutex_unlock(&dir_f->sem);
jffs2_complete_reservation(c); jffs2_complete_reservation(c);
unlock_new_inode(inode); d_instantiate_new(dentry, inode);
d_instantiate(dentry, inode);
return 0; return 0;
fail: fail:
...@@ -573,8 +571,7 @@ static int jffs2_mkdir (struct inode *dir_i, struct dentry *dentry, umode_t mode ...@@ -573,8 +571,7 @@ static int jffs2_mkdir (struct inode *dir_i, struct dentry *dentry, umode_t mode
mutex_unlock(&dir_f->sem); mutex_unlock(&dir_f->sem);
jffs2_complete_reservation(c); jffs2_complete_reservation(c);
unlock_new_inode(inode); d_instantiate_new(dentry, inode);
d_instantiate(dentry, inode);
return 0; return 0;
fail: fail:
...@@ -745,8 +742,7 @@ static int jffs2_mknod (struct inode *dir_i, struct dentry *dentry, umode_t mode ...@@ -745,8 +742,7 @@ static int jffs2_mknod (struct inode *dir_i, struct dentry *dentry, umode_t mode
mutex_unlock(&dir_f->sem); mutex_unlock(&dir_f->sem);
jffs2_complete_reservation(c); jffs2_complete_reservation(c);
unlock_new_inode(inode); d_instantiate_new(dentry, inode);
d_instantiate(dentry, inode);
return 0; return 0;
fail: fail:
......
...@@ -178,8 +178,7 @@ static int jfs_create(struct inode *dip, struct dentry *dentry, umode_t mode, ...@@ -178,8 +178,7 @@ static int jfs_create(struct inode *dip, struct dentry *dentry, umode_t mode,
unlock_new_inode(ip); unlock_new_inode(ip);
iput(ip); iput(ip);
} else { } else {
unlock_new_inode(ip); d_instantiate_new(dentry, ip);
d_instantiate(dentry, ip);
} }
out2: out2:
...@@ -313,8 +312,7 @@ static int jfs_mkdir(struct inode *dip, struct dentry *dentry, umode_t mode) ...@@ -313,8 +312,7 @@ static int jfs_mkdir(struct inode *dip, struct dentry *dentry, umode_t mode)
unlock_new_inode(ip); unlock_new_inode(ip);
iput(ip); iput(ip);
} else { } else {
unlock_new_inode(ip); d_instantiate_new(dentry, ip);
d_instantiate(dentry, ip);
} }
out2: out2:
...@@ -1058,8 +1056,7 @@ static int jfs_symlink(struct inode *dip, struct dentry *dentry, ...@@ -1058,8 +1056,7 @@ static int jfs_symlink(struct inode *dip, struct dentry *dentry,
unlock_new_inode(ip); unlock_new_inode(ip);
iput(ip); iput(ip);
} else { } else {
unlock_new_inode(ip); d_instantiate_new(dentry, ip);
d_instantiate(dentry, ip);
} }
out2: out2:
...@@ -1443,8 +1440,7 @@ static int jfs_mknod(struct inode *dir, struct dentry *dentry, ...@@ -1443,8 +1440,7 @@ static int jfs_mknod(struct inode *dir, struct dentry *dentry,
unlock_new_inode(ip); unlock_new_inode(ip);
iput(ip); iput(ip);
} else { } else {
unlock_new_inode(ip); d_instantiate_new(dentry, ip);
d_instantiate(dentry, ip);
} }
out1: out1:
......
...@@ -50,8 +50,7 @@ static inline int nilfs_add_nondir(struct dentry *dentry, struct inode *inode) ...@@ -50,8 +50,7 @@ static inline int nilfs_add_nondir(struct dentry *dentry, struct inode *inode)
{ {
int err = nilfs_add_link(dentry, inode); int err = nilfs_add_link(dentry, inode);
if (!err) { if (!err) {
d_instantiate(dentry, inode); d_instantiate_new(dentry, inode);
unlock_new_inode(inode);
return 0; return 0;
} }
inode_dec_link_count(inode); inode_dec_link_count(inode);
...@@ -246,8 +245,7 @@ static int nilfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) ...@@ -246,8 +245,7 @@ static int nilfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
goto out_fail; goto out_fail;
nilfs_mark_inode_dirty(inode); nilfs_mark_inode_dirty(inode);
d_instantiate(dentry, inode); d_instantiate_new(dentry, inode);
unlock_new_inode(inode);
out: out:
if (!err) if (!err)
err = nilfs_transaction_commit(dir->i_sb); err = nilfs_transaction_commit(dir->i_sb);
......
...@@ -687,8 +687,7 @@ static int reiserfs_create(struct inode *dir, struct dentry *dentry, umode_t mod ...@@ -687,8 +687,7 @@ static int reiserfs_create(struct inode *dir, struct dentry *dentry, umode_t mod
reiserfs_update_inode_transaction(inode); reiserfs_update_inode_transaction(inode);
reiserfs_update_inode_transaction(dir); reiserfs_update_inode_transaction(dir);
unlock_new_inode(inode); d_instantiate_new(dentry, inode);
d_instantiate(dentry, inode);
retval = journal_end(&th); retval = journal_end(&th);
out_failed: out_failed:
...@@ -771,8 +770,7 @@ static int reiserfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode ...@@ -771,8 +770,7 @@ static int reiserfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode
goto out_failed; goto out_failed;
} }
unlock_new_inode(inode); d_instantiate_new(dentry, inode);
d_instantiate(dentry, inode);
retval = journal_end(&th); retval = journal_end(&th);
out_failed: out_failed:
...@@ -871,8 +869,7 @@ static int reiserfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode ...@@ -871,8 +869,7 @@ static int reiserfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode
/* the above add_entry did not update dir's stat data */ /* the above add_entry did not update dir's stat data */
reiserfs_update_sd(&th, dir); reiserfs_update_sd(&th, dir);
unlock_new_inode(inode); d_instantiate_new(dentry, inode);
d_instantiate(dentry, inode);
retval = journal_end(&th); retval = journal_end(&th);
out_failed: out_failed:
reiserfs_write_unlock(dir->i_sb); reiserfs_write_unlock(dir->i_sb);
...@@ -1186,8 +1183,7 @@ static int reiserfs_symlink(struct inode *parent_dir, ...@@ -1186,8 +1183,7 @@ static int reiserfs_symlink(struct inode *parent_dir,
goto out_failed; goto out_failed;
} }
unlock_new_inode(inode); d_instantiate_new(dentry, inode);
d_instantiate(dentry, inode);
retval = journal_end(&th); retval = journal_end(&th);
out_failed: out_failed:
reiserfs_write_unlock(parent_dir->i_sb); reiserfs_write_unlock(parent_dir->i_sb);
......
...@@ -611,8 +611,7 @@ static int udf_add_nondir(struct dentry *dentry, struct inode *inode) ...@@ -611,8 +611,7 @@ static int udf_add_nondir(struct dentry *dentry, struct inode *inode)
if (fibh.sbh != fibh.ebh) if (fibh.sbh != fibh.ebh)
brelse(fibh.ebh); brelse(fibh.ebh);
brelse(fibh.sbh); brelse(fibh.sbh);
unlock_new_inode(inode); d_instantiate_new(dentry, inode);
d_instantiate(dentry, inode);
return 0; return 0;
} }
...@@ -722,8 +721,7 @@ static int udf_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) ...@@ -722,8 +721,7 @@ static int udf_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
inc_nlink(dir); inc_nlink(dir);
dir->i_ctime = dir->i_mtime = current_fs_time(dir->i_sb); dir->i_ctime = dir->i_mtime = current_fs_time(dir->i_sb);
mark_inode_dirty(dir); mark_inode_dirty(dir);
unlock_new_inode(inode); d_instantiate_new(dentry, inode);
d_instantiate(dentry, inode);
if (fibh.sbh != fibh.ebh) if (fibh.sbh != fibh.ebh)
brelse(fibh.ebh); brelse(fibh.ebh);
brelse(fibh.sbh); brelse(fibh.sbh);
......
...@@ -38,8 +38,7 @@ static inline int ufs_add_nondir(struct dentry *dentry, struct inode *inode) ...@@ -38,8 +38,7 @@ static inline int ufs_add_nondir(struct dentry *dentry, struct inode *inode)
{ {
int err = ufs_add_link(dentry, inode); int err = ufs_add_link(dentry, inode);
if (!err) { if (!err) {
unlock_new_inode(inode); d_instantiate_new(dentry, inode);
d_instantiate(dentry, inode);
return 0; return 0;
} }
inode_dec_link_count(inode); inode_dec_link_count(inode);
...@@ -191,8 +190,7 @@ static int ufs_mkdir(struct inode * dir, struct dentry * dentry, umode_t mode) ...@@ -191,8 +190,7 @@ static int ufs_mkdir(struct inode * dir, struct dentry * dentry, umode_t mode)
if (err) if (err)
goto out_fail; goto out_fail;
unlock_new_inode(inode); d_instantiate_new(dentry, inode);
d_instantiate(dentry, inode);
return 0; return 0;
out_fail: out_fail:
......
...@@ -236,6 +236,7 @@ extern seqlock_t rename_lock; ...@@ -236,6 +236,7 @@ extern seqlock_t rename_lock;
* These are the low-level FS interfaces to the dcache.. * These are the low-level FS interfaces to the dcache..
*/ */
extern void d_instantiate(struct dentry *, struct inode *); extern void d_instantiate(struct dentry *, struct inode *);
extern void d_instantiate_new(struct dentry *, struct inode *);
extern struct dentry * d_instantiate_unique(struct dentry *, struct inode *); extern struct dentry * d_instantiate_unique(struct dentry *, struct inode *);
extern int d_instantiate_no_diralias(struct dentry *, struct inode *); extern int d_instantiate_no_diralias(struct dentry *, struct inode *);
extern void __d_drop(struct dentry *dentry); extern void __d_drop(struct dentry *dentry);
......
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