gfs2: deallocate inodes in gfs2_create_inode
Andreas Gruenbacher
maintainerPATCH v2 3/3 gfs2: deallocate inodes in gfs2_create_inode
When creating and destroying inodes, we are relying on the inode hash table to make sure that for a given inode number, only a single inode will exist. We then link that inode to its inode and iopen glock and let those glocks point back at the inode. However, when iget_failed() is called, the inode is removed from the inode hash table before gfs_evict_inode() is called, and uniqueness is no longer guaranteed.
Commit f1046a472b70 ("gfs2: gl_object races fix") was trying to work around that problem by detaching the inode glock from the inode before calling iget_failed(), but that broke the inode deallocation code in gfs_evict_inode().
To fix that, deallocate partially created inodes in gfs2_create_inode() instead of relying on gfs_evict_inode() for doing that.
This means that gfs2_evict_inode() and its helper functions will no longer see partially created inodes, and so some simplifications are possible there.
--- fs/gfs2/inode.c | 23 +++++++++++++++-------- fs/gfs2/super.c | 8 ++------ fs/gfs2/super.h | 2 ++ 3 files changed, 19 insertions(+), 14 deletions(-)
fs/gfs2/inode.c +15 -8
@@ -697,10 +697,11 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,struct gfs2_inode *dip =GFS2_I (dir), *ip;struct gfs2_sbd *sdp =GFS2_SB (&dip->i_inode);struct gfs2_glock *io_gl; -int error; +int error, error2;u32 aflags =0 ;unsigned blocks =1 ;struct gfs2_diradd da = { .bh =NULL , .save_loc =1 , }; +bool xattr_initialized = false;if (!name->len || name->len >GFS2_FNAMESIZE )return -ENAMETOOLONG ;
@@ -813,11 +814,11 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry, error =gfs2_glock_get (sdp, ip->i_no_addr, &gfs2_inode_glops,CREATE , &ip->i_gl);if (error) - goto fail_free_inode; + goto fail_dealloc_inode; error =gfs2_glock_get (sdp, ip->i_no_addr, &gfs2_iopen_glops,CREATE , &io_gl);if (error) - goto fail_free_inode; + goto fail_dealloc_inode;gfs2_cancel_delete_work (io_gl); io_gl->gl_no_formal_ino = ip->i_no_formal_ino;
@@ -842,8 +843,10 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,if (error) goto fail_gunlock3; -if (blocks >1 ) +if (blocks >1 ) {gfs2_init_xattr (ip); + xattr_initialized = true; + }init_dinode (dip, ip, symname);gfs2_trans_end (sdp);
@@ -898,6 +901,14 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,gfs2_glock_dq_uninit (&ip->i_iopen_gh); fail_gunlock2:gfs2_glock_put (io_gl); +fail_dealloc_inode: +set_bit (GIF_ALLOC_FAILED , &ip->i_flags); +clear_nlink (inode); +mark_inode_dirty (inode); + error2 =evict_unlinked_inode (inode); +if (error2) +fs_warn (sdp,"%s: %d\n" , __func__, error2); + ip->i_no_addr =0 ; fail_free_inode:if (ip->i_gl) {gfs2_glock_put (ip->i_gl);
@@ -912,10 +923,6 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,gfs2_dir_no_add (&da);gfs2_glock_dq_uninit (&d_gh);if (!IS_ERR_OR_NULL (inode)) { -set_bit (GIF_ALLOC_FAILED , &ip->i_flags); -clear_nlink (inode); -if (ip->i_no_addr) -mark_inode_dirty (inode);if (inode->i_state &I_NEW )iget_failed (inode);else
fs/gfs2/super.c +2 -6
@@ -1258,9 +1258,6 @@ static enum evict_behavior evict_should_delete(struct inode *inode,struct gfs2_sbd *sdp = sb->s_fs_info;int ret; -if (unlikely (test_bit (GIF_ALLOC_FAILED , &ip->i_flags))) - goto should_delete; -if (gfs2_holder_initialized (&ip->i_iopen_gh) &&test_bit (GLF_DEFER_DELETE , &ip->i_iopen_gh.gh_gl->gl_flags))return EVICT_SHOULD_DEFER_DELETE ;
@@ -1290,7 +1287,6 @@ static enum evict_behavior evict_should_delete(struct inode *inode,if (inode->i_nlink)return EVICT_SHOULD_SKIP_DELETE ; -should_delete:if (gfs2_holder_initialized (&ip->i_iopen_gh) &&test_bit (HIF_HOLDER , &ip->i_iopen_gh.gh_iflags))return gfs2_upgrade_iopen_glock (inode);
@@ -1301,7 +1297,7 @@ static enum evict_behavior evict_should_delete(struct inode *inode,* evict_unlinked_inode - delete the pieces of an unlinked evicted inode * @inode: The inode to evict */ -static int evict_unlinked_inode (struct inode *inode) +int evict_unlinked_inode (struct inode *inode) {struct gfs2_inode *ip =GFS2_I (inode);int ret;
@@ -1314,7 +1310,7 @@ static int evict_unlinked_inode(struct inode *inode) }if (ip->i_eattr) { - ret =gfs2_ea_dealloc (ip, !test_bit (GIF_ALLOC_FAILED , &ip->i_flags)); + ret =gfs2_ea_dealloc (ip, true);if (ret) goto out; }
fs/gfs2/super.h +2 -0
@@ -15,6 +15,8 @@#define GFS2_FS_FORMAT_MIN (1801)#define GFS2_FS_FORMAT_MAX (1802) +int evict_unlinked_inode (struct inode *inode); +void gfs2_lm_unmount (struct gfs2_sbd *sdp);static inline unsigned int gfs2_jindex_size (struct gfs2_sbd *sdp)
Signature · 1 line
2.49.0
Quoted 32 lines · Andreas Gruenbacher
When creating and destroying inodes, we are relying on the inode hash table to make sure that for a given inode number, only a single inode will exist. We then link that inode to its inode and iopen glock and let those glocks point back at the inode. However, when
iget_failed()is called, the inode is removed from the inode hash table beforegfs_evict_inode()is called, and uniqueness is no longer guaranteed.Commit
f1046a472b70("gfs2:gl_objectraces fix") was trying to work around that problem by detaching the inode glock from the inode before callingiget_failed(), but that broke the inode deallocation code ingfs_evict_inode().To fix that, deallocate partially created inodes in
gfs2_create_inode()instead of relying ongfs_evict_inode()for doing that.This means that
gfs2_evict_inode()and its helper functions will no longer see partially created inodes, and so some simplifications are possible there.Fixes f1046a472b70 ("gfs2: gl_object races fix")Signed-off-by Andreas Gruenbacher <[email protected]>--- fs/gfs2/inode.c | 23 +++++++++++++++-------- fs/gfs2/super.c | 8 ++------ fs/gfs2/super.h | 2 ++ 3 files changed, 19 insertions(+), 14 deletions(-)fs/gfs2/inode.c +0
-0
fs/gfs2/inode.c static int gfs2_create_inode(struct inode *dir, struct dentry *dentry, · line 704
Quoted 9 lines
fs/gfs2/inode.c +2
-1@@ -697,10 +697,11 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,struct gfs2_inode *dip =GFS2_I (dir), *ip;struct gfs2_sbd *sdp =GFS2_SB (&dip->i_inode);struct gfs2_glock *io_gl; -int error; +int error, error2;u32 aflags =0 ;unsigned blocks =1 ;struct gfs2_diradd da = { .bh =NULL , .save_loc =1 , }; +bool xattr_initialized = false;
This variable isn't used in this version.
Quoted 30 lines
fs/gfs2/inode.c +5
-3if (!name->len || name->len >GFS2_FNAMESIZE )return -ENAMETOOLONG ;@@ -813,11 +814,11 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry, error =gfs2_glock_get (sdp, ip->i_no_addr, &gfs2_inode_glops,CREATE , &ip->i_gl);if (error) - goto fail_free_inode; + goto fail_dealloc_inode; error =gfs2_glock_get (sdp, ip->i_no_addr, &gfs2_iopen_glops,CREATE , &io_gl);if (error) - goto fail_free_inode; + goto fail_dealloc_inode;gfs2_cancel_delete_work (io_gl); io_gl->gl_no_formal_ino = ip->i_no_formal_ino;@@ -842,8 +843,10 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,if (error) goto fail_gunlock3; -if (blocks >1 ) +if (blocks >1 ) {gfs2_init_xattr (ip); + xattr_initialized = true; + }init_dinode (dip, ip, symname);gfs2_trans_end (sdp);
fs/gfs2/inode.c static int gfs2_create_inode(struct inode *dir, struct dentry *dentry, · line 908
Quoted 8 lines
fs/gfs2/inode.c +5
-0@@ -898,6 +901,14 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,gfs2_glock_dq_uninit (&ip->i_iopen_gh); fail_gunlock2:gfs2_glock_put (io_gl); +fail_dealloc_inode: +set_bit (GIF_ALLOC_FAILED , &ip->i_flags); +clear_nlink (inode); +mark_inode_dirty (inode); + error2 =evict_unlinked_inode (inode);
So we're switching to only checking ip->i_eattr and calling gfs2_ea_dealloc() indirectly with the initialized argument always true. I don't think that's right as ip->i_eattr gets set much earlier than the gfs2_init_xattr() call.
Andy
Quoted 70 lines
fs/gfs2/inode.c +3
-4+if (error2) +fs_warn (sdp,"%s: %d\n" , __func__, error2); + ip->i_no_addr =0 ; fail_free_inode:if (ip->i_gl) {gfs2_glock_put (ip->i_gl);@@ -912,10 +923,6 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,gfs2_dir_no_add (&da);gfs2_glock_dq_uninit (&d_gh);if (!IS_ERR_OR_NULL (inode)) { -set_bit (GIF_ALLOC_FAILED , &ip->i_flags); -clear_nlink (inode); -if (ip->i_no_addr) -mark_inode_dirty (inode);if (inode->i_state &I_NEW )iget_failed (inode);else fs/gfs2/super.c +2
-6@@ -1258,9 +1258,6 @@ static enum evict_behavior evict_should_delete(struct inode *inode,struct gfs2_sbd *sdp = sb->s_fs_info;int ret; -if (unlikely (test_bit (GIF_ALLOC_FAILED , &ip->i_flags))) - goto should_delete; -if (gfs2_holder_initialized (&ip->i_iopen_gh) &&test_bit (GLF_DEFER_DELETE , &ip->i_iopen_gh.gh_gl->gl_flags))return EVICT_SHOULD_DEFER_DELETE ;@@ -1290,7 +1287,6 @@ static enum evict_behavior evict_should_delete(struct inode *inode,if (inode->i_nlink)return EVICT_SHOULD_SKIP_DELETE ; -should_delete:if (gfs2_holder_initialized (&ip->i_iopen_gh) &&test_bit (HIF_HOLDER , &ip->i_iopen_gh.gh_iflags))return gfs2_upgrade_iopen_glock (inode);@@ -1301,7 +1297,7 @@ static enum evict_behavior evict_should_delete(struct inode *inode,* evict_unlinked_inode - delete the pieces of an unlinked evicted inode * @inode: The inode to evict */ -static int evict_unlinked_inode (struct inode *inode) +int evict_unlinked_inode (struct inode *inode) {struct gfs2_inode *ip =GFS2_I (inode);int ret;@@ -1314,7 +1310,7 @@ static int evict_unlinked_inode(struct inode *inode) }if (ip->i_eattr) { - ret =gfs2_ea_dealloc (ip, !test_bit (GIF_ALLOC_FAILED , &ip->i_flags)); + ret =gfs2_ea_dealloc (ip, true);if (ret) goto out; }fs/gfs2/super.h +2
-0@@ -15,6 +15,8 @@#define GFS2_FS_FORMAT_MIN (1801)#define GFS2_FS_FORMAT_MAX (1802) +int evict_unlinked_inode (struct inode *inode); +void gfs2_lm_unmount (struct gfs2_sbd *sdp);static inline unsigned int gfs2_jindex_size (struct gfs2_sbd *sdp)
Andreas Gruenbacher
maintainerQuoted 74 lines · Andrew Price
Quoted 32 lines · Andreas Gruenbacher
When creating and destroying inodes, we are relying on the inode hash table to make sure that for a given inode number, only a single inode will exist. We then link that inode to its inode and iopen glock and let those glocks point back at the inode. However, when
iget_failed()is called, the inode is removed from the inode hash table beforegfs_evict_inode()is called, and uniqueness is no longer guaranteed.Commit
f1046a472b70("gfs2:gl_objectraces fix") was trying to work around that problem by detaching the inode glock from the inode before callingiget_failed(), but that broke the inode deallocation code ingfs_evict_inode().To fix that, deallocate partially created inodes in
gfs2_create_inode()instead of relying ongfs_evict_inode()for doing that.This means that
gfs2_evict_inode()and its helper functions will no longer see partially created inodes, and so some simplifications are possible there.Fixes f1046a472b70 ("gfs2: gl_object races fix")Signed-off-by Andreas Gruenbacher <[email protected]>--- fs/gfs2/inode.c | 23 +++++++++++++++-------- fs/gfs2/super.c | 8 ++------ fs/gfs2/super.h | 2 ++ 3 files changed, 19 insertions(+), 14 deletions(-)fs/gfs2/inode.c +0
-0fs/gfs2/inode.c static int gfs2_create_inode(struct inode *dir, struct dentry *dentry, · line 704
Quoted 9 lines
fs/gfs2/inode.c +2
-1@@ -697,10 +697,11 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,struct gfs2_inode *dip =GFS2_I (dir), *ip;struct gfs2_sbd *sdp =GFS2_SB (&dip->i_inode);struct gfs2_glock *io_gl; -int error; +int error, error2;u32 aflags =0 ;unsigned blocks =1 ;struct gfs2_diradd da = { .bh =NULL , .save_loc =1 , }; +bool xattr_initialized = false;This variable isn't used in this version.
Quoted 29 lines
fs/gfs2/inode.c +5
-3if (!name->len || name->len >GFS2_FNAMESIZE )return -ENAMETOOLONG ;@@ -813,11 +814,11 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry, error =gfs2_glock_get (sdp, ip->i_no_addr, &gfs2_inode_glops,CREATE , &ip->i_gl);if (error) - goto fail_free_inode; + goto fail_dealloc_inode; error =gfs2_glock_get (sdp, ip->i_no_addr, &gfs2_iopen_glops,CREATE , &io_gl);if (error) - goto fail_free_inode; + goto fail_dealloc_inode;gfs2_cancel_delete_work (io_gl); io_gl->gl_no_formal_ino = ip->i_no_formal_ino;@@ -842,8 +843,10 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,if (error) goto fail_gunlock3; -if (blocks >1 ) +if (blocks >1 ) {gfs2_init_xattr (ip); + xattr_initialized = true; + }init_dinode (dip, ip, symname);gfs2_trans_end (sdp);
Quoted 14 lines
fs/gfs2/inode.c static int gfs2_create_inode(struct inode *dir, struct dentry *dentry, · line 908
Quoted 9 lines
fs/gfs2/inode.c +5
-0@@ -898,6 +901,14 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,gfs2_glock_dq_uninit (&ip->i_iopen_gh); fail_gunlock2:gfs2_glock_put (io_gl); +fail_dealloc_inode: +set_bit (GIF_ALLOC_FAILED , &ip->i_flags); +clear_nlink (inode); +mark_inode_dirty (inode); + error2 =evict_unlinked_inode (inode);So we're switching to only checking
ip->i_eattrand callinggfs2_ea_dealloc()indirectly with the initialized argument always true. I don't think that's right asip->i_eattrgets set much earlier than thegfs2_init_xattr()call.
Right ... this code is so awful ...
I've sent a fixed version that will hopefully be final.
Thanks,
Andreas
Quoted 73 lines
Andy
Quoted 70 lines
fs/gfs2/inode.c +3
-4+if (error2) +fs_warn (sdp,"%s: %d\n" , __func__, error2); + ip->i_no_addr =0 ; fail_free_inode:if (ip->i_gl) {gfs2_glock_put (ip->i_gl);@@ -912,10 +923,6 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,gfs2_dir_no_add (&da);gfs2_glock_dq_uninit (&d_gh);if (!IS_ERR_OR_NULL (inode)) { -set_bit (GIF_ALLOC_FAILED , &ip->i_flags); -clear_nlink (inode); -if (ip->i_no_addr) -mark_inode_dirty (inode);if (inode->i_state &I_NEW )iget_failed (inode);else fs/gfs2/super.c +2
-6@@ -1258,9 +1258,6 @@ static enum evict_behavior evict_should_delete(struct inode *inode,struct gfs2_sbd *sdp = sb->s_fs_info;int ret; -if (unlikely (test_bit (GIF_ALLOC_FAILED , &ip->i_flags))) - goto should_delete; -if (gfs2_holder_initialized (&ip->i_iopen_gh) &&test_bit (GLF_DEFER_DELETE , &ip->i_iopen_gh.gh_gl->gl_flags))return EVICT_SHOULD_DEFER_DELETE ;@@ -1290,7 +1287,6 @@ static enum evict_behavior evict_should_delete(struct inode *inode,if (inode->i_nlink)return EVICT_SHOULD_SKIP_DELETE ; -should_delete:if (gfs2_holder_initialized (&ip->i_iopen_gh) &&test_bit (HIF_HOLDER , &ip->i_iopen_gh.gh_iflags))return gfs2_upgrade_iopen_glock (inode);@@ -1301,7 +1297,7 @@ static enum evict_behavior evict_should_delete(struct inode *inode,* evict_unlinked_inode - delete the pieces of an unlinked evicted inode * @inode: The inode to evict */ -static int evict_unlinked_inode (struct inode *inode) +int evict_unlinked_inode (struct inode *inode) {struct gfs2_inode *ip =GFS2_I (inode);int ret;@@ -1314,7 +1310,7 @@ static int evict_unlinked_inode(struct inode *inode) }if (ip->i_eattr) { - ret =gfs2_ea_dealloc (ip, !test_bit (GIF_ALLOC_FAILED , &ip->i_flags)); + ret =gfs2_ea_dealloc (ip, true);if (ret) goto out; }fs/gfs2/super.h +2
-0@@ -15,6 +15,8 @@#define GFS2_FS_FORMAT_MIN (1801)#define GFS2_FS_FORMAT_MAX (1802) +int evict_unlinked_inode (struct inode *inode); +void gfs2_lm_unmount (struct gfs2_sbd *sdp);static inline unsigned int gfs2_jindex_size (struct gfs2_sbd *sdp)