xfs: inodes are new until the dentry cache is set up
PATCH 1/1 xfs: inodes are new until the dentry cache is set up
From: Dave Chinner <[email protected]>
Al Viro noticed a generic set of issues to do with filehandle lookup racing with dentry cache setup. They involve a filehandle lookup occurring while an inode is being created and the filehandle lookup racing with the dentry creation for the real file. This can lead to multiple dentries for the one path being instantiated. There are a host of other issues around this same set of paths.
The underlying cause is that file handle lookup only waits on inode cache instantiation rather than full dentry cache instantiation. XFS is mostly immune to the problems discovered due to it's own internal inode cache, but there are a couple of corner cases where races can happen.
We currently clear the XFS_INEW flag when the inode is fully set up after insertion into the cache. Newly allocated inodes are inserted locked and so aren't usable until the allocation transaction commits. This, however, occurs before the dentry and security information is fully initialised and hence the inode is unlocked and available for lookups to find too early.
To solve the problem, only clear the XFS_INEW flag for newly created inodes once the dentry is fully instantiated. This means lookups will retry until the XFS_INEW flag is removed from the inode and hence avoids the race conditions in questions.
--- fs/xfs/xfs_icache.c | 4 ++-- fs/xfs/xfs_inode.c | 2 +- fs/xfs/xfs_inode.h | 22 ++++++++++++++++++++++ fs/xfs/xfs_iops.c | 24 ++++++++++-------------- fs/xfs/xfs_iops.h | 2 -- fs/xfs/xfs_qm.c | 13 +++++++++---- 6 files changed, 44 insertions(+), 23 deletions(-)
fs/xfs/xfs_icache.c +2 -2
@@ -439,11 +439,11 @@ again: *ipp = ip;/* -* If we have a real type for an on-disk inode, we can set ops(&unlock) +* If we have a real type for an on-disk inode, we can setup the inode * now. If it's a new inode being created, xfs_ialloc will handle it. */ if (xfs_iflags_test (ip,XFS_INEW ) && ip->i_d.di_mode !=0 ) -xfs_setup_inode (ip); +xfs_setup_existing_inode (ip);return 0 ; out_error_or_again:
fs/xfs/xfs_inode.c +1 -1
@@ -818,7 +818,7 @@ xfs_ialloc(xfs_trans_ijoin (tp, ip,XFS_ILOCK_EXCL );xfs_trans_log_inode (tp, ip, flags); -/* now that we have an i_mode we can setup inode ops and unlock */ +/* now that we have an i_mode we can setup the inode structure */ xfs_setup_inode (ip); *ipp = ip;
fs/xfs/xfs_inode.h +22 -0
@@ -381,6 +381,28 @@ int xfs_zero_eof(struct xfs_inode *, xfs_off_t, xfs_fsize_t);int xfs_iozero (struct xfs_inode *,loff_t ,size_t ); +/* from xfs_iops.c */ +/* +* When setting up a newly allocated inode, we need to call +* xfs_finish_inode_setup() once the inode is fully instantiated at +* the VFS level to prevent the rest of the world seeing the inode +* before we've completed instantiation. Otherwise we can do it +* the moment the inode lookup is complete. +*/ +extern void xfs_setup_inode (struct xfs_inode *ip); +static inline void xfs_finish_inode_setup (struct xfs_inode *ip) +{ +xfs_iflags_clear (ip,XFS_INEW ); +barrier (); +unlock_new_inode (VFS_I (ip)); +} + +static inline void xfs_setup_existing_inode (struct xfs_inode *ip) +{ +xfs_setup_inode (ip); +xfs_finish_inode_setup (ip); +} +#define IHOLD (ip) \ do { \ ASSERT(atomic_read(&VFS_I(ip)->i_count) > 0) ; \
fs/xfs/xfs_iops.c +10 -14
@@ -186,6 +186,8 @@ xfs_generic_create(else d_instantiate (dentry, inode); +xfs_finish_inode_setup (ip); + out_free_acl:if (default_acl)posix_acl_release (default_acl);
@@ -194,6 +196,7 @@ xfs_generic_create(return error; out_cleanup_inode: +xfs_finish_inode_setup (ip);if (!tmpfile)xfs_cleanup_inode (dir, inode, dentry);iput (inode);
@@ -366,9 +369,11 @@ xfs_vn_symlink( goto out_cleanup_inode;d_instantiate (dentry, inode); +xfs_finish_inode_setup (cip);return 0 ; out_cleanup_inode: +xfs_finish_inode_setup (cip);xfs_cleanup_inode (dir, inode, dentry);iput (inode); out:
@@ -1231,16 +1236,12 @@ xfs_diflags_to_iflags( }/* -* Initialize the Linux inode, set up the operation vectors and -* unlock the inode. -* -* When reading existing inodes from disk this is called directly -* from xfs_iget, when creating a new inode it is called from -* xfs_ialloc after setting up the inode. +* Initialize the Linux inode and set up the operation vectors. * -* We are always called with an uninitialised linux inode here. -* We need to initialise the necessary fields and take a reference -* on it. +* When reading existing inodes from disk this is called directly from xfs_iget, +* when creating a new inode it is called from xfs_ialloc after setting up the +* inode. These callers have different criteria for clearing XFS_INEW, so leave +* it up to the caller to deal with unlocking the inode appropriately. */ void xfs_setup_inode(
@@ -1327,9 +1328,4 @@ xfs_setup_inode(inode_has_no_xattr (inode);cache_no_acl (inode); } - -xfs_iflags_clear (ip,XFS_INEW ); -barrier (); - -unlock_new_inode (inode); }
fs/xfs/xfs_iops.h +0 -2
@@ -25,8 +25,6 @@ extern const struct file_operations xfs_dir_file_operations;extern ssize_t xfs_vn_listxattr (struct dentry *,char *data,size_t size); -extern void xfs_setup_inode (struct xfs_inode *); -/* * Internal setattr interfaces. */
fs/xfs/xfs_qm.c +9 -4
@@ -719,6 +719,7 @@ xfs_qm_qino_alloc(xfs_trans_t *tp;int error;int committed; +bool need_alloc = true; *ip =NULL ;/*
@@ -747,6 +748,7 @@ xfs_qm_qino_alloc(return error; mp->m_sb.sb_gquotino =NULLFSINO ; mp->m_sb.sb_pquotino =NULLFSINO ; + need_alloc = false; } }
@@ -758,7 +760,7 @@ xfs_qm_qino_alloc(return error; } -if (!*ip) { +if (need_alloc) { error =xfs_dir_ialloc (&tp,NULL ,S_IFREG ,1 ,0 ,0 ,1 , ip, &committed);if (error) {
@@ -794,11 +796,14 @@ xfs_qm_qino_alloc(spin_unlock (&mp->m_sb_lock);xfs_log_sb (tp); -if ((error =xfs_trans_commit (tp,XFS_TRANS_RELEASE_LOG_RES ))) { + error =xfs_trans_commit (tp,XFS_TRANS_RELEASE_LOG_RES ); +if (error) { +ASSERT (XFS_FORCED_SHUTDOWN (mp));xfs_alert (mp,"%s failed (error %d)!" , __func__, error); -return error; } -return 0 ; +if (need_alloc) +xfs_finish_inode_setup (*ip); +return error; }
Signature · 6 lines
2.0.0 _______________________________________________ xfs mailing list [email protected] http://oss.sgi.com/mailman/listinfo/xfs
Quoted 124 lines · Dave Chinner
From: Dave Chinner <[email protected]>
Al Viro noticed a generic set of issues to do with filehandle lookup racing with dentry cache setup. They involve a filehandle lookup occurring while an inode is being created and the filehandle lookup racing with the dentry creation for the real file. This can lead to multiple dentries for the one path being instantiated. There are a host of other issues around this same set of paths.
The underlying cause is that file handle lookup only waits on inode cache instantiation rather than full dentry cache instantiation. XFS is mostly immune to the problems discovered due to it's own internal inode cache, but there are a couple of corner cases where races can happen.
We currently clear the
XFS_INEWflag when the inode is fully set up after insertion into the cache. Newly allocated inodes are inserted locked and so aren't usable until the allocation transaction commits. This, however, occurs before the dentry and security information is fully initialised and hence the inode is unlocked and available for lookups to find too early.To solve the problem, only clear the
XFS_INEWflag for newly created inodes once the dentry is fully instantiated. This means lookups will retry until theXFS_INEWflag is removed from the inode and hence avoids the race conditions in questions.Signed-off-by Dave Chinner <[email protected]>--- fs/xfs/xfs_icache.c | 4 ++-- fs/xfs/xfs_inode.c | 2 +- fs/xfs/xfs_inode.h | 22 ++++++++++++++++++++++ fs/xfs/xfs_iops.c | 24 ++++++++++-------------- fs/xfs/xfs_iops.h | 2 -- fs/xfs/xfs_qm.c | 13 +++++++++---- 6 files changed, 44 insertions(+), 23 deletions(-)fs/xfs/xfs_icache.c +2
-2@@ -439,11 +439,11 @@ again: *ipp = ip;/* -* If we have a real type for an on-disk inode, we can set ops(&unlock) +* If we have a real type for an on-disk inode, we can setup the inode * now. If it's a new inode being created, xfs_ialloc will handle it. */ if (xfs_iflags_test (ip,XFS_INEW ) && ip->i_d.di_mode !=0 ) -xfs_setup_inode (ip); +xfs_setup_existing_inode (ip);return 0 ; out_error_or_again:fs/xfs/xfs_inode.c +1
-1@@ -818,7 +818,7 @@ xfs_ialloc(xfs_trans_ijoin (tp, ip,XFS_ILOCK_EXCL );xfs_trans_log_inode (tp, ip, flags); -/* now that we have an i_mode we can setup inode ops and unlock */ +/* now that we have an i_mode we can setup the inode structure */ xfs_setup_inode (ip); *ipp = ip;fs/xfs/xfs_inode.h +22
-0@@ -381,6 +381,28 @@ int xfs_zero_eof(struct xfs_inode *, xfs_off_t, xfs_fsize_t);int xfs_iozero (struct xfs_inode *,loff_t ,size_t ); +/* from xfs_iops.c */ +/* +* When setting up a newly allocated inode, we need to call +* xfs_finish_inode_setup() once the inode is fully instantiated at +* the VFS level to prevent the rest of the world seeing the inode +* before we've completed instantiation. Otherwise we can do it +* the moment the inode lookup is complete. +*/ +extern void xfs_setup_inode (struct xfs_inode *ip); +static inline void xfs_finish_inode_setup (struct xfs_inode *ip) +{ +xfs_iflags_clear (ip,XFS_INEW ); +barrier (); +unlock_new_inode (VFS_I (ip)); +} + +static inline void xfs_setup_existing_inode (struct xfs_inode *ip) +{ +xfs_setup_inode (ip); +xfs_finish_inode_setup (ip); +} +#define IHOLD (ip) \ do { \ ASSERT(atomic_read(&VFS_I(ip)->i_count) > 0) ; \fs/xfs/xfs_iops.c +3
-0@@ -186,6 +186,8 @@ xfs_generic_create(else d_instantiate (dentry, inode); +xfs_finish_inode_setup (ip); + out_free_acl:if (default_acl)posix_acl_release (default_acl);@@ -194,6 +196,7 @@ xfs_generic_create(return error; out_cleanup_inode: +xfs_finish_inode_setup (ip);if (!tmpfile)xfs_cleanup_inode (dir, inode, dentry);iput (inode);@@ -366,9 +369,11 @@ xfs_vn_symlink( goto out_cleanup_inode;
fs/xfs/xfs_iops.c xfs_vn_symlink( · line 379
Quoted 10 lines
fs/xfs/xfs_iops.c +2
-0d_instantiate (dentry, inode); +xfs_finish_inode_setup (cip);return 0 ; out_cleanup_inode: +xfs_finish_inode_setup (cip);xfs_cleanup_inode (dir, inode, dentry);iput (inode); out:
Ok, but what about post-inode-allocation failure conditions down in xfs_create()? I don't know if there's any real harm in releasing an I_NEW inode, but iput_final() does throw a warning. Same general question applies to xfs_create_tmpfile(), etc..
Brian
Quoted 99 lines
fs/xfs/xfs_iops.c +5
-14@@ -1231,16 +1236,12 @@ xfs_diflags_to_iflags( }/* -* Initialize the Linux inode, set up the operation vectors and -* unlock the inode. -* -* When reading existing inodes from disk this is called directly -* from xfs_iget, when creating a new inode it is called from -* xfs_ialloc after setting up the inode. +* Initialize the Linux inode and set up the operation vectors. * -* We are always called with an uninitialised linux inode here. -* We need to initialise the necessary fields and take a reference -* on it. +* When reading existing inodes from disk this is called directly from xfs_iget, +* when creating a new inode it is called from xfs_ialloc after setting up the +* inode. These callers have different criteria for clearing XFS_INEW, so leave +* it up to the caller to deal with unlocking the inode appropriately. */ void xfs_setup_inode(@@ -1327,9 +1328,4 @@ xfs_setup_inode(inode_has_no_xattr (inode);cache_no_acl (inode); } - -xfs_iflags_clear (ip,XFS_INEW ); -barrier (); - -unlock_new_inode (inode); }fs/xfs/xfs_iops.h +0
-2@@ -25,8 +25,6 @@ extern const struct file_operations xfs_dir_file_operations;extern ssize_t xfs_vn_listxattr (struct dentry *,char *data,size_t size); -extern void xfs_setup_inode (struct xfs_inode *); -/* * Internal setattr interfaces. */ fs/xfs/xfs_qm.c +9
-4@@ -719,6 +719,7 @@ xfs_qm_qino_alloc(xfs_trans_t *tp;int error;int committed; +bool need_alloc = true; *ip =NULL ;/* @@ -747,6 +748,7 @@ xfs_qm_qino_alloc(return error; mp->m_sb.sb_gquotino =NULLFSINO ; mp->m_sb.sb_pquotino =NULLFSINO ; + need_alloc = false; } }@@ -758,7 +760,7 @@ xfs_qm_qino_alloc(return error; } -if (!*ip) { +if (need_alloc) { error =xfs_dir_ialloc (&tp,NULL ,S_IFREG ,1 ,0 ,0 ,1 , ip, &committed);if (error) {@@ -794,11 +796,14 @@ xfs_qm_qino_alloc(spin_unlock (&mp->m_sb_lock);xfs_log_sb (tp); -if ((error =xfs_trans_commit (tp,XFS_TRANS_RELEASE_LOG_RES ))) { + error =xfs_trans_commit (tp,XFS_TRANS_RELEASE_LOG_RES ); +if (error) { +ASSERT (XFS_FORCED_SHUTDOWN (mp));xfs_alert (mp,"%s failed (error %d)!" , __func__, error); -return error; } -return 0 ; +if (need_alloc) +xfs_finish_inode_setup (*ip); +return error; }Signature · 6 lines
2.0.0 _______________________________________________ xfs mailing list [email protected] http://oss.sgi.com/mailman/listinfo/xfs
_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
Quoted 29 lines · Brian Foster
Quoted 28 lines · Dave Chinner
From: Dave Chinner <[email protected]>
Al Viro noticed a generic set of issues to do with filehandle lookup racing with dentry cache setup. They involve a filehandle lookup occurring while an inode is being created and the filehandle lookup racing with the dentry creation for the real file. This can lead to multiple dentries for the one path being instantiated. There are a host of other issues around this same set of paths.
The underlying cause is that file handle lookup only waits on inode cache instantiation rather than full dentry cache instantiation. XFS is mostly immune to the problems discovered due to it's own internal inode cache, but there are a couple of corner cases where races can happen.
We currently clear the
XFS_INEWflag when the inode is fully set up after insertion into the cache. Newly allocated inodes are inserted locked and so aren't usable until the allocation transaction commits. This, however, occurs before the dentry and security information is fully initialised and hence the inode is unlocked and available for lookups to find too early.To solve the problem, only clear the
XFS_INEWflag for newly created inodes once the dentry is fully instantiated. This means lookups will retry until theXFS_INEWflag is removed from the inode and hence avoids the race conditions in questions.Signed-off-by Dave Chinner <[email protected]>
....
Quoted 28 lines
Quoted 28 lines
fs/xfs/xfs_iops.c +4
-0@@ -186,6 +186,8 @@ xfs_generic_create(else d_instantiate (dentry, inode); +xfs_finish_inode_setup (ip); + out_free_acl:if (default_acl)posix_acl_release (default_acl);@@ -194,6 +196,7 @@ xfs_generic_create(return error; out_cleanup_inode: +xfs_finish_inode_setup (ip);if (!tmpfile)xfs_cleanup_inode (dir, inode, dentry);iput (inode);@@ -366,9 +369,11 @@ xfs_vn_symlink( goto out_cleanup_inode;d_instantiate (dentry, inode); +xfs_finish_inode_setup (cip);return 0 ;
Quoted 10 lines
fs/xfs/xfs_iops.c xfs_vn_symlink( · line 379
Quoted 5 lines
fs/xfs/xfs_iops.c +1
-0out_cleanup_inode: +xfs_finish_inode_setup (cip);xfs_cleanup_inode (dir, inode, dentry);iput (inode); out:Ok, but what about post-inode-allocation failure conditions down in
xfs_create()? I don't know if there's any real harm in releasing anI_NEWinode, butiput_final()does throw a warning. Same general question applies toxfs_create_tmpfile(), etc..
Ah, good point, I missed those.
Where/how are you getting warnings thrown? I'm not seeing anything from xfstests runs?
-Dave.
Signature · 7 lines
Dave Chinner [email protected] _______________________________________________ xfs mailing list [email protected] http://oss.sgi.com/mailman/listinfo/xfs
Quoted 70 lines · Dave Chinner
Quoted 29 lines · Brian Foster
Quoted 28 lines · Dave Chinner
From: Dave Chinner <[email protected]>
Al Viro noticed a generic set of issues to do with filehandle lookup racing with dentry cache setup. They involve a filehandle lookup occurring while an inode is being created and the filehandle lookup racing with the dentry creation for the real file. This can lead to multiple dentries for the one path being instantiated. There are a host of other issues around this same set of paths.
The underlying cause is that file handle lookup only waits on inode cache instantiation rather than full dentry cache instantiation. XFS is mostly immune to the problems discovered due to it's own internal inode cache, but there are a couple of corner cases where races can happen.
We currently clear the
XFS_INEWflag when the inode is fully set up after insertion into the cache. Newly allocated inodes are inserted locked and so aren't usable until the allocation transaction commits. This, however, occurs before the dentry and security information is fully initialised and hence the inode is unlocked and available for lookups to find too early.To solve the problem, only clear the
XFS_INEWflag for newly created inodes once the dentry is fully instantiated. This means lookups will retry until theXFS_INEWflag is removed from the inode and hence avoids the race conditions in questions.Signed-off-by Dave Chinner <[email protected]>....
Quoted 28 lines
Quoted 28 lines
fs/xfs/xfs_iops.c +4
-0@@ -186,6 +186,8 @@ xfs_generic_create(else d_instantiate (dentry, inode); +xfs_finish_inode_setup (ip); + out_free_acl:if (default_acl)posix_acl_release (default_acl);@@ -194,6 +196,7 @@ xfs_generic_create(return error; out_cleanup_inode: +xfs_finish_inode_setup (ip);if (!tmpfile)xfs_cleanup_inode (dir, inode, dentry);iput (inode);@@ -366,9 +369,11 @@ xfs_vn_symlink( goto out_cleanup_inode;d_instantiate (dentry, inode); +xfs_finish_inode_setup (cip);return 0 ;Quoted 10 lines
fs/xfs/xfs_iops.c xfs_vn_symlink( · line 379
Quoted 5 lines
fs/xfs/xfs_iops.c +1
-0out_cleanup_inode: +xfs_finish_inode_setup (cip);xfs_cleanup_inode (dir, inode, dentry);iput (inode); out:Ok, but what about post-inode-allocation failure conditions down in
xfs_create()? I don't know if there's any real harm in releasing anI_NEWinode, butiput_final()does throw a warning. Same general question applies toxfs_create_tmpfile(), etc..
Quoted 5 lines
Ah, good point, I missed those.
Where/how are you getting warnings thrown? I'm not seeing anything from xfstests runs?
Oh, I hadn't reproduced warnings as such. I was just digging down that path and came across this in iput_final():
WARN_ON(inode->i_state & I_NEW);
Brian
Quoted 4 lines
-Dave.
Signature · 2 lines
Dave Chinner [email protected]
_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs