Skip to contentSkip to conversation

gfs2: Move gfs2_dinode_dealloc

PATCH In review Andreas Gruenbacher 2 participants 6 messages Apr 22, 2025 – Apr 23, 2025 GFS2 file system

lore
Share & tools
lore

Andreas Gruenbacher

author maintainer
#0 lore raw

PATCH 1/3 gfs2: Move gfs2_dinode_dealloc

Move gfs2_dinode_dealloc() and its helper gfs2_final_release_pages() from super.c to inode.c.

---
 fs/gfs2/inode.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++
 fs/gfs2/inode.h |  1 +
 fs/gfs2/super.c | 68 -------------------------------------------------
 3 files changed, 69 insertions(+), 68 deletions(-)
fs/gfs2/inode.c +68 -0
index 62ff09264f0f..0b2d75106b1c 100644
@@ -439,6 +439,74 @@ static int alloc_dinode(struct gfs2_inode *ip, u32 flags, unsigned *dblocks)
 	return error;
 }
 
+static void gfs2_final_release_pages(struct gfs2_inode *ip)
+{
+	struct inode *inode = &ip->i_inode;
+	struct gfs2_glock *gl = ip->i_gl;
+
+	if (unlikely(!gl)) {
+		/* This can only happen during incomplete inode creation. */
+		BUG_ON(!test_bit(GIF_ALLOC_FAILED, &ip->i_flags));
+		return;
+	}
+
+	truncate_inode_pages(gfs2_glock2aspace(gl), 0);
+	truncate_inode_pages(&inode->i_data, 0);
+
+	if (atomic_read(&gl->gl_revokes) == 0) {
+		clear_bit(GLF_LFLUSH, &gl->gl_flags);
+		clear_bit(GLF_DIRTY, &gl->gl_flags);
+	}
+}
+
+int gfs2_dinode_dealloc(struct gfs2_inode *ip)
+{
+	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
+	struct gfs2_rgrpd *rgd;
+	struct gfs2_holder gh;
+	int error;
+
+	if (gfs2_get_inode_blocks(&ip->i_inode) != 1) {
+		gfs2_consist_inode(ip);
+		return -EIO;
+	}
+
+	gfs2_rindex_update(sdp);
+
+	error = gfs2_quota_hold(ip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE);
+	if (error)
+		return error;
+
+	rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr, 1);
+	if (!rgd) {
+		gfs2_consist_inode(ip);
+		error = -EIO;
+		goto out_qs;
+	}
+
+	error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE,
+				   LM_FLAG_NODE_SCOPE, &gh);
+	if (error)
+		goto out_qs;
+
+	error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA,
+				 sdp->sd_jdesc->jd_blocks);
+	if (error)
+		goto out_rg_gunlock;
+
+	gfs2_free_di(rgd, ip);
+
+	gfs2_final_release_pages(ip);
+
+	gfs2_trans_end(sdp);
+
+out_rg_gunlock:
+	gfs2_glock_dq_uninit(&gh);
+out_qs:
+	gfs2_quota_unhold(ip);
+	return error;
+}
+
 static void gfs2_init_dir(struct buffer_head *dibh,
 			  const struct gfs2_inode *parent)
 {
fs/gfs2/inode.h +1 -0
index 9e5e1622d50a..eafe123617e6 100644
@@ -92,6 +92,7 @@ struct inode *gfs2_inode_lookup(struct super_block *sb, unsigned type,
 struct inode *gfs2_lookup_by_inum(struct gfs2_sbd *sdp, u64 no_addr,
 				  u64 no_formal_ino,
 				  unsigned int blktype);
+int gfs2_dinode_dealloc(struct gfs2_inode *ip);
 
 struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
 			   int is_root);
fs/gfs2/super.c +0 -68

74 lines not on this page · raw message

Signature · 1 line
2.49.0
author maintainer
#1 lore raw

PATCH 2/3 gfs2: Move GIF_ALLOC_FAILED check out of gfs2_ea_dealloc

Don't check for the GIF_ALLOC_FAILED flag in gfs2_ea_dealloc() and pass that information explicitly instead. This allows for a cleaner follow-up patch.

---
 fs/gfs2/super.c |  2 +-
 fs/gfs2/xattr.c | 11 ++++++-----
 fs/gfs2/xattr.h |  2 +-
 3 files changed, 8 insertions(+), 7 deletions(-)
fs/gfs2/super.c +1 -1
index 3f49f848c6b4..e25a24ae2197 100644
@@ -1314,7 +1314,7 @@ static int evict_unlinked_inode(struct inode *inode)
 	}
 
 	if (ip->i_eattr) {
-		ret = gfs2_ea_dealloc(ip);
+		ret = gfs2_ea_dealloc(ip, !test_bit(GIF_ALLOC_FAILED, &ip->i_flags));
 		if (ret)
 			goto out;
 	}
fs/gfs2/xattr.c +6 -5
index 17ae5070a90e..df9c93de94c7 100644
@@ -1383,7 +1383,7 @@ static int ea_dealloc_indirect(struct gfs2_inode *ip)
 	return error;
 }
 
-static int ea_dealloc_block(struct gfs2_inode *ip)
+static int ea_dealloc_block(struct gfs2_inode *ip, bool initialized)
 {
 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
 	struct gfs2_rgrpd *rgd;
@@ -1416,7 +1416,7 @@ static int ea_dealloc_block(struct gfs2_inode *ip)
 	ip->i_eattr = 0;
 	gfs2_add_inode_blocks(&ip->i_inode, -1);
 
-	if (likely(!test_bit(GIF_ALLOC_FAILED, &ip->i_flags))) {
+	if (initialized) {
 		error = gfs2_meta_inode_buffer(ip, &dibh);
 		if (!error) {
 			gfs2_trans_add_meta(ip->i_gl, dibh);
@@ -1435,11 +1435,12 @@ static int ea_dealloc_block(struct gfs2_inode *ip)
 /**
  * gfs2_ea_dealloc - deallocate the extended attribute fork
  * @ip: the inode
+ * @initialized: xattrs have been initialized
  *
  * Returns: errno
  */
 
-int gfs2_ea_dealloc(struct gfs2_inode *ip)
+int gfs2_ea_dealloc(struct gfs2_inode *ip, bool initialized)
 {
 	int error;
 
@@ -1451,7 +1452,7 @@ int gfs2_ea_dealloc(struct gfs2_inode *ip)
 	if (error)
 		return error;
 
-	if (likely(!test_bit(GIF_ALLOC_FAILED, &ip->i_flags))) {
+	if (initialized) {
 		error = ea_foreach(ip, ea_dealloc_unstuffed, NULL);
 		if (error)
 			goto out_quota;
@@ -1463,7 +1464,7 @@ int gfs2_ea_dealloc(struct gfs2_inode *ip)
 		}
 	}
 
-	error = ea_dealloc_block(ip);
+	error = ea_dealloc_block(ip, initialized);
 
 out_quota:
 	gfs2_quota_unhold(ip);
fs/gfs2/xattr.h +1 -1
index eb12eb7e37c1..3c9788e0e137 100644
@@ -54,7 +54,7 @@ int __gfs2_xattr_set(struct inode *inode, const char *name,
 		     const void *value, size_t size,
 		     int flags, int type);
 ssize_t gfs2_listxattr(struct dentry *dentry, char *buffer, size_t size);
-int gfs2_ea_dealloc(struct gfs2_inode *ip);
+int gfs2_ea_dealloc(struct gfs2_inode *ip, bool initialized);
 
 /* Exported to acl.c */
 
Signature · 1 line
2.49.0
author maintainer
#2 lore raw

PATCH 3/3 gfs2: deallocate inodes in gfs2_create_inode

In reply to 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 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 some simplifications are possible there.

Fixes f1046a472b70 ("gfs2: gl_object races fix")
---
 fs/gfs2/inode.c | 21 ++++++++++++++-------
 fs/gfs2/super.c |  6 +-----
 2 files changed, 15 insertions(+), 12 deletions(-)
fs/gfs2/inode.c +14 -7
index 0b2d75106b1c..2812cce21817 100644
@@ -701,6 +701,7 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
 	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);
+	if (ip->i_eattr)
+		gfs2_ea_dealloc(ip, xattr_initialized);
+	clear_nlink(inode);
+	mark_inode_dirty(inode);
+	gfs2_dinode_dealloc(ip);
+	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 +1 -5
index e25a24ae2197..54f1efd47a3e 100644
@@ -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);
@@ -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;
 	}
Signature · 1 line
2.49.0
#3 lore raw

Re: [PATCH 3/3] gfs2: deallocate inodes in gfs2_create_inode

Quoted 12 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 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().

Quoted 6 lines

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 some simplifications are possible there.

Looks good. It survived an overnight run of generic/256 on 3 VMs.

Andy

Quoted 103 lines
Fixes f1046a472b70 ("gfs2: gl_object races fix")
---
  fs/gfs2/inode.c | 21 ++++++++++++++-------
  fs/gfs2/super.c |  6 +-----
  2 files changed, 15 insertions(+), 12 deletions(-)
fs/gfs2/inode.c +14 -7
index 0b2d75106b1c..2812cce21817 100644
@@ -701,6 +701,7 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
  	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);
+	if (ip->i_eattr)
+		gfs2_ea_dealloc(ip, xattr_initialized);
+	clear_nlink(inode);
+	mark_inode_dirty(inode);
+	gfs2_dinode_dealloc(ip);
+	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 +1 -5
index e25a24ae2197..54f1efd47a3e 100644
@@ -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);
@@ -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;
  	}
#4 lore raw

Re: [PATCH 3/3] gfs2: deallocate inodes in gfs2_create_inode

In reply to Andreas Gruenbacher

Something occurred to me...

On 22/04/2025 10:54, Andreas Gruenbacher wrote:
<snip>

fs/gfs2/inode.c static int gfs2_create_inode(struct inode *dir, struct dentry *dentry, · line 907

Quoted 8 lines
fs/gfs2/inode.c +4 -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);
+	if (ip->i_eattr)
+		gfs2_ea_dealloc(ip, xattr_initialized);

Is it safe to throw away the return value here? I guess it probably is if gfs2_ea_dealloc() failures always flag a withdraw, but it's difficult to check all the paths.

Andy

Quoted 7 lines
fs/gfs2/inode.c +4 -0
+	clear_nlink(inode);
+	mark_inode_dirty(inode);
+	gfs2_dinode_dealloc(ip);
+	ip->i_no_addr = 0;
  fail_free_inode:
  	if (ip->i_gl) {
  		gfs2_glock_put(ip->i_gl);

<snip>

author maintainer
#5 lore raw

Re: [PATCH 3/3] gfs2: deallocate inodes in gfs2_create_inode

Quoted 4 lines · Andrew Price

Something occurred to me...

On 22/04/2025 10:54, Andreas Gruenbacher wrote:
<snip>

Quoted 10 lines

fs/gfs2/inode.c static int gfs2_create_inode(struct inode *dir, struct dentry *dentry, · line 907

Quoted 8 lines
fs/gfs2/inode.c +4 -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);
+     if (ip->i_eattr)
+             gfs2_ea_dealloc(ip, xattr_initialized);

Is it safe to throw away the return value here?

At this point, we've already encountered an error. When deallocation results in an additional error, the best we can do is report that error. And that's actually what gfs2_evict_inode() does, so let me change gfs2_create_inode() to do the same.

Quoted 2 lines

I guess it probably is if gfs2_ea_dealloc() failures always flag a withdraw, but it's difficult to check all the paths.

It's really gfs2_ea_dealloc()'s problem to trigger a withdraw when necessary; not all failures lead to a withdraw.

Quoted 10 lines

Andy

Quoted 7 lines
fs/gfs2/inode.c +4 -0
+     clear_nlink(inode);
+     mark_inode_dirty(inode);
+     gfs2_dinode_dealloc(ip);
+     ip->i_no_addr = 0;
  fail_free_inode:
      if (ip->i_gl) {
              gfs2_glock_put(ip->i_gl);

<snip>

Thanks,
Andreas