Skip to contentSkip to conversation

Clean up and fix quota data allocate and free

PATCH Superseded Bob Peterson 2 participants 5 messages Feb 26, 2020 GFS2 file system

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

cover Clean up and fix quota data allocate and free

This patch set cleans up a recently discovered race between file close and chown. The problem was that file close would free the quota data structures attached to the in-core inode, but chown relied on them to exist. So if the timing of the close is wrong, the chown can result in kernel panic. The problem is easily recreated with this:

On terminal session 1:
while true; do chown test /mnt/gfs2/test; chown root /mnt/gfs2/test; done

On terminal session 2:
while true; do echo "a" > /mnt/gfs2/test; echo "b" > /mnt/gfs2/test;done

The first patch changes the sequence so that the quota data structure isn't freed until evict time, rather than at close time. The other two patches are cleanups that natually follow from the first patch.

Bob Peterson (3):
gfs2: free quota data struct during evict, not file close
gfs2: rename gfs2_rsqa_delete to gfs2_rs_delete
gfs2: eliminate gfs2_rsqa_alloc in favor of gfs2_qa_alloc

 fs/gfs2/acl.c   |  3 ++-
 fs/gfs2/bmap.c  |  4 ++--
 fs/gfs2/file.c  | 10 +++++-----
 fs/gfs2/inode.c | 14 +++++++-------
 fs/gfs2/quota.c | 16 +++-------------
 fs/gfs2/quota.h |  1 -
 fs/gfs2/rgrp.c  | 15 ++-------------
 fs/gfs2/rgrp.h  |  3 +--
 fs/gfs2/super.c |  5 ++++-
 fs/gfs2/xattr.c |  2 +-
 10 files changed, 27 insertions(+), 46 deletions(-)
Signature · 1 line
2.24.1
#1 lore raw

PATCH 1/3 gfs2: free quota data struct during evict, not file close

Before this patch, whenever a file was closed, if it was the last process out, it freed the quota data via gfs2_qa_delete(). However, that created race conditions between closers and other operations like chown that relied upon the structure. This patch moves the freeing of quota data to inode evict, and eliminates function gfs2_qa_delete in favor of just freeing the structure.

---
 fs/gfs2/quota.c | 10 ----------
 fs/gfs2/quota.h |  1 -
 fs/gfs2/rgrp.c  |  1 -
 fs/gfs2/super.c |  3 +++
 4 files changed, 3 insertions(+), 12 deletions(-)
fs/gfs2/quota.c +0 -10
index 43ffe5997098..15158b6c933b 100644
@@ -547,16 +547,6 @@ int gfs2_qa_alloc(struct gfs2_inode *ip)
 	return error;
 }
 
-void gfs2_qa_delete(struct gfs2_inode *ip, atomic_t *wcount)
-{
-	down_write(&ip->i_rw_mutex);
-	if (ip->i_qadata && ((wcount == NULL) || (atomic_read(wcount) <= 1))) {
-		kmem_cache_free(gfs2_qadata_cachep, ip->i_qadata);
-		ip->i_qadata = NULL;
-	}
-	up_write(&ip->i_rw_mutex);
-}
-
 int gfs2_quota_hold(struct gfs2_inode *ip, kuid_t uid, kgid_t gid)
 {
 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
fs/gfs2/quota.h +0 -1
index 765627d9a91e..4a4a09aee7d9 100644
@@ -16,7 +16,6 @@ struct gfs2_sbd;
 #define NO_GID_QUOTA_CHANGE INVALID_GID
 
 extern int gfs2_qa_alloc(struct gfs2_inode *ip);
-extern void gfs2_qa_delete(struct gfs2_inode *ip, atomic_t *wcount);
 extern int gfs2_quota_hold(struct gfs2_inode *ip, kuid_t uid, kgid_t gid);
 extern void gfs2_quota_unhold(struct gfs2_inode *ip);
 
fs/gfs2/rgrp.c +0 -1
index 2ee2f7d48bc1..7efc765ebc6a 100644
@@ -683,7 +683,6 @@ void gfs2_rsqa_delete(struct gfs2_inode *ip, atomic_t *wcount)
 	if ((wcount == NULL) || (atomic_read(wcount) <= 1))
 		gfs2_rs_deltree(&ip->i_res);
 	up_write(&ip->i_rw_mutex);
-	gfs2_qa_delete(ip, wcount);
 }
 
 /**
fs/gfs2/super.c +3 -0
index 693c6d13473c..3d36d8671cf6 100644
@@ -1410,6 +1410,9 @@ static void gfs2_evict_inode(struct inode *inode)
 out:
 	truncate_inode_pages_final(&inode->i_data);
 	gfs2_rsqa_delete(ip, NULL);
+	if (ip->i_qadata)
+		kmem_cache_free(gfs2_qadata_cachep, ip->i_qadata);
+	ip->i_qadata = NULL;
 	gfs2_ordered_del_inode(ip);
 	clear_inode(inode);
 	gfs2_dir_hash_inval(ip);
Signature · 1 line
2.24.1
#2 lore raw

PATCH 1/3 gfs2: free quota data struct during evict, not file close

Quoted 6 lines · Bob Peterson

Before this patch, whenever a file was closed, if it was the last process out, it freed the quota data via gfs2_qa_delete(). However, that created race conditions between closers and other operations like chown that relied upon the structure. This patch moves the freeing of quota data to inode evict, and eliminates function gfs2_qa_delete in favor of just freeing the structure.

This will use sizeof(struct gfs2_qadata) == 264 more bytes for each inode when quotas are used. I'm not categorically against that, but maybe the locking can be fixed instead.

Please explain why we don't have the same race for reservations that we have for quotas.

The other two patches look fine.

Quoted 68 lines
---
 fs/gfs2/quota.c | 10 ----------
 fs/gfs2/quota.h |  1 -
 fs/gfs2/rgrp.c  |  1 -
 fs/gfs2/super.c |  3 +++
 4 files changed, 3 insertions(+), 12 deletions(-)
fs/gfs2/quota.c +0 -10
index 43ffe5997098..15158b6c933b 100644
@@ -547,16 +547,6 @@ int gfs2_qa_alloc(struct gfs2_inode *ip)
        return error;
 }
 
-void gfs2_qa_delete(struct gfs2_inode *ip, atomic_t *wcount)
-{
-       down_write(&ip->i_rw_mutex);
-       if (ip->i_qadata && ((wcount == NULL) || (atomic_read(wcount) <= 1))) {
-               kmem_cache_free(gfs2_qadata_cachep, ip->i_qadata);
-               ip->i_qadata = NULL;
-       }
-       up_write(&ip->i_rw_mutex);
-}
-
 int gfs2_quota_hold(struct gfs2_inode *ip, kuid_t uid, kgid_t gid)
 {
        struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
fs/gfs2/quota.h +0 -1
index 765627d9a91e..4a4a09aee7d9 100644
@@ -16,7 +16,6 @@ struct gfs2_sbd;
 #define NO_GID_QUOTA_CHANGE INVALID_GID
 
 extern int gfs2_qa_alloc(struct gfs2_inode *ip);
-extern void gfs2_qa_delete(struct gfs2_inode *ip, atomic_t *wcount);
 extern int gfs2_quota_hold(struct gfs2_inode *ip, kuid_t uid, kgid_t gid);
 extern void gfs2_quota_unhold(struct gfs2_inode *ip);
 
fs/gfs2/rgrp.c +0 -1
index 2ee2f7d48bc1..7efc765ebc6a 100644
@@ -683,7 +683,6 @@ void gfs2_rsqa_delete(struct gfs2_inode *ip, atomic_t *wcount)
        if ((wcount == NULL) || (atomic_read(wcount) <= 1))
                gfs2_rs_deltree(&ip->i_res);
        up_write(&ip->i_rw_mutex);
-       gfs2_qa_delete(ip, wcount);
 }
 
 /**
fs/gfs2/super.c +3 -1
index 693c6d13473c..3d36d8671cf6 100644
@@ -1410,6 +1410,9 @@ static void gfs2_evict_inode(struct inode *inode)
 out:
        truncate_inode_pages_final(&inode->i_data);
        gfs2_rsqa_delete(ip, NULL);
+       if (ip->i_qadata)
+               kmem_cache_free(gfs2_qadata_cachep, ip->i_qadata);
+       ip->i_qadata = NULL;
        gfs2_ordered_del_inode(ip);
        clear_inode(inode);
        gfs2_dir_hash_inval(ip);
--
Quoted 2 lines

2.24.1

Thanks,
Andreas

#3 lore raw

PATCH 2/3 gfs2: rename gfs2_rsqa_delete to gfs2_rs_delete

In reply to Bob Peterson

Now that freeing the inode's quota data structure is separate from freeing rgrp reservations, we rename gfs2_rsqa_delete to gfs2_rs_delete.

---
 fs/gfs2/bmap.c  | 2 +-
 fs/gfs2/file.c  | 2 +-
 fs/gfs2/inode.c | 2 +-
 fs/gfs2/rgrp.c  | 4 ++--
 fs/gfs2/rgrp.h  | 2 +-
 fs/gfs2/super.c | 2 +-
 6 files changed, 7 insertions(+), 7 deletions(-)
fs/gfs2/bmap.c +1 -1
index 08f6fbb3655e..fa50482b8c92 100644
@@ -2194,7 +2194,7 @@ int gfs2_setattr_size(struct inode *inode, u64 newsize)
 
 	ret = do_shrink(inode, newsize);
 out:
-	gfs2_rsqa_delete(ip, NULL);
+	gfs2_rs_delete(ip, NULL);
 	return ret;
 }
 
fs/gfs2/file.c +1 -1
index cb26be6f4351..4d19ca3e4784 100644
@@ -693,7 +693,7 @@ static int gfs2_release(struct inode *inode, struct file *file)
 	if (!(file->f_mode & FMODE_WRITE))
 		return 0;
 
-	gfs2_rsqa_delete(ip, &inode->i_writecount);
+	gfs2_rs_delete(ip, &inode->i_writecount);
 	return 0;
 }
 
fs/gfs2/inode.c +1 -1
index 2716d56ed0a0..a56ce1aa5f93 100644
@@ -786,7 +786,7 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
 		glock_clear_object(ip->i_gl, ip);
 		gfs2_glock_put(ip->i_gl);
 	}
-	gfs2_rsqa_delete(ip, NULL);
+	gfs2_rs_delete(ip, NULL);
 fail_free_acls:
 	posix_acl_release(default_acl);
 	posix_acl_release(acl);
fs/gfs2/rgrp.c +2 -2
index 7efc765ebc6a..59bc4d1a4d89 100644
@@ -672,12 +672,12 @@ void gfs2_rs_deltree(struct gfs2_blkreserv *rs)
 }
 
 /**
- * gfs2_rsqa_delete - delete a multi-block reservation and quota allocation
+ * gfs2_rs_delete - delete a multi-block reservation
  * @ip: The inode for this reservation
  * @wcount: The inode's write count, or NULL
  *
  */
-void gfs2_rsqa_delete(struct gfs2_inode *ip, atomic_t *wcount)
+void gfs2_rs_delete(struct gfs2_inode *ip, atomic_t *wcount)
 {
 	down_write(&ip->i_rw_mutex);
 	if ((wcount == NULL) || (atomic_read(wcount) <= 1))
fs/gfs2/rgrp.h +1 -1
index a584f3096418..008ef9736f1c 100644
@@ -46,7 +46,7 @@ extern int gfs2_alloc_blocks(struct gfs2_inode *ip, u64 *bn, unsigned int *n,
 
 extern int gfs2_rsqa_alloc(struct gfs2_inode *ip);
 extern void gfs2_rs_deltree(struct gfs2_blkreserv *rs);
-extern void gfs2_rsqa_delete(struct gfs2_inode *ip, atomic_t *wcount);
+extern void gfs2_rs_delete(struct gfs2_inode *ip, atomic_t *wcount);
 extern void __gfs2_free_blocks(struct gfs2_inode *ip, struct gfs2_rgrpd *rgd,
 			       u64 bstart, u32 blen, int meta);
 extern void gfs2_free_meta(struct gfs2_inode *ip, struct gfs2_rgrpd *rgd,
fs/gfs2/super.c +1 -1
index 3d36d8671cf6..c029a49871d3 100644
@@ -1409,7 +1409,7 @@ static void gfs2_evict_inode(struct inode *inode)
 		fs_warn(sdp, "gfs2_evict_inode: %d\n", error);
 out:
 	truncate_inode_pages_final(&inode->i_data);
-	gfs2_rsqa_delete(ip, NULL);
+	gfs2_rs_delete(ip, NULL);
 	if (ip->i_qadata)
 		kmem_cache_free(gfs2_qadata_cachep, ip->i_qadata);
 	ip->i_qadata = NULL;
Signature · 1 line
2.24.1
#4 lore raw

PATCH 3/3 gfs2: eliminate gfs2_rsqa_alloc in favor of gfs2_qa_alloc

In reply to Bob Peterson

Before this patch, multiple callers called gfs2_rsqa_alloc to force the existence of a reservations structure and a quota data structure if needed. However, now the reservations are handled separately, so the quota data is only the quota data. So we eliminate the one in favor of just calling gfs2_qa_alloc directly.

---
 fs/gfs2/acl.c   |  3 ++-
 fs/gfs2/bmap.c  |  2 +-
 fs/gfs2/file.c  |  8 ++++----
 fs/gfs2/inode.c | 12 ++++++------
 fs/gfs2/quota.c |  6 +++---
 fs/gfs2/rgrp.c  | 10 ----------
 fs/gfs2/rgrp.h  |  1 -
 fs/gfs2/xattr.c |  2 +-
 8 files changed, 17 insertions(+), 27 deletions(-)
fs/gfs2/acl.c +2 -1
index 09e6be8aa036..cb09b85c5b10 100644
@@ -21,6 +21,7 @@
 #include "glock.h"
 #include "inode.h"
 #include "meta_io.h"
+#include "quota.h"
 #include "rgrp.h"
 #include "trans.h"
 #include "util.h"
@@ -116,7 +117,7 @@ int gfs2_set_acl(struct inode *inode, struct posix_acl *acl, int type)
 	if (acl && acl->a_count > GFS2_ACL_MAX_ENTRIES(GFS2_SB(inode)))
 		return -E2BIG;
 
-	ret = gfs2_rsqa_alloc(ip);
+	ret = gfs2_qa_alloc(ip);
 	if (ret)
 		return ret;
 
fs/gfs2/bmap.c +1 -1
index fa50482b8c92..5c818284f8c4 100644
@@ -2183,7 +2183,7 @@ int gfs2_setattr_size(struct inode *inode, u64 newsize)
 
 	inode_dio_wait(inode);
 
-	ret = gfs2_rsqa_alloc(ip);
+	ret = gfs2_qa_alloc(ip);
 	if (ret)
 		goto out;
 
fs/gfs2/file.c +4 -4
index 4d19ca3e4784..3fbd67b3bb15 100644
@@ -458,7 +458,7 @@ static vm_fault_t gfs2_page_mkwrite(struct vm_fault *vmf)
 
 	sb_start_pagefault(inode->i_sb);
 
-	ret = gfs2_rsqa_alloc(ip);
+	ret = gfs2_qa_alloc(ip);
 	if (ret)
 		goto out;
 
@@ -849,7 +849,7 @@ static ssize_t gfs2_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
 	struct gfs2_inode *ip = GFS2_I(inode);
 	ssize_t ret;
 
-	ret = gfs2_rsqa_alloc(ip);
+	ret = gfs2_qa_alloc(ip);
 	if (ret)
 		return ret;
 
@@ -1149,7 +1149,7 @@ static long gfs2_fallocate(struct file *file, int mode, loff_t offset, loff_t le
 	if (mode & FALLOC_FL_PUNCH_HOLE) {
 		ret = __gfs2_punch_hole(file, offset, len);
 	} else {
-		ret = gfs2_rsqa_alloc(ip);
+		ret = gfs2_qa_alloc(ip);
 		if (ret)
 			goto out_putw;
 
@@ -1176,7 +1176,7 @@ static ssize_t gfs2_file_splice_write(struct pipe_inode_info *pipe,
 	int error;
 	struct gfs2_inode *ip = GFS2_I(out->f_mapping->host);
 
-	error = gfs2_rsqa_alloc(ip);
+	error = gfs2_qa_alloc(ip);
 	if (error)
 		return (ssize_t)error;
 
fs/gfs2/inode.c +6 -6
index a56ce1aa5f93..99620e58dd66 100644
@@ -594,7 +594,7 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
 	if (!name->len || name->len > GFS2_FNAMESIZE)
 		return -ENAMETOOLONG;
 
-	error = gfs2_rsqa_alloc(dip);
+	error = gfs2_qa_alloc(dip);
 	if (error)
 		return error;
 
@@ -647,7 +647,7 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
 		goto fail_gunlock;
 
 	ip = GFS2_I(inode);
-	error = gfs2_rsqa_alloc(ip);
+	error = gfs2_qa_alloc(ip);
 	if (error)
 		goto fail_free_acls;
 
@@ -905,7 +905,7 @@ static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
 	if (S_ISDIR(inode->i_mode))
 		return -EPERM;
 
-	error = gfs2_rsqa_alloc(dip);
+	error = gfs2_qa_alloc(dip);
 	if (error)
 		return error;
 
@@ -1368,7 +1368,7 @@ static int gfs2_rename(struct inode *odir, struct dentry *odentry,
 	if (error)
 		return error;
 
-	error = gfs2_rsqa_alloc(ndip);
+	error = gfs2_qa_alloc(ndip);
 	if (error)
 		return error;
 
@@ -1880,7 +1880,7 @@ static int setattr_chown(struct inode *inode, struct iattr *attr)
 	if (!(attr->ia_valid & ATTR_GID) || gid_eq(ogid, ngid))
 		ogid = ngid = NO_GID_QUOTA_CHANGE;
 
-	error = gfs2_rsqa_alloc(ip);
+	error = gfs2_qa_alloc(ip);
 	if (error)
 		goto out;
 
@@ -1941,7 +1941,7 @@ static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
 	struct gfs2_holder i_gh;
 	int error;
 
-	error = gfs2_rsqa_alloc(ip);
+	error = gfs2_qa_alloc(ip);
 	if (error)
 		return error;
 
fs/gfs2/quota.c +3 -3
index 15158b6c933b..0754c5caf666 100644
@@ -557,7 +557,7 @@ int gfs2_quota_hold(struct gfs2_inode *ip, kuid_t uid, kgid_t gid)
 		return 0;
 
 	if (ip->i_qadata == NULL) {
-		error = gfs2_rsqa_alloc(ip);
+		error = gfs2_qa_alloc(ip);
 		if (error)
 			return error;
 	}
@@ -866,7 +866,7 @@ static int do_sync(unsigned int num_qd, struct gfs2_quota_data **qda)
 	unsigned int nalloc = 0, blocks;
 	int error;
 
-	error = gfs2_rsqa_alloc(ip);
+	error = gfs2_qa_alloc(ip);
 	if (error)
 		return error;
 
@@ -1667,7 +1667,7 @@ static int gfs2_set_dqblk(struct super_block *sb, struct kqid qid,
 	if (error)
 		return error;
 
-	error = gfs2_rsqa_alloc(ip);
+	error = gfs2_qa_alloc(ip);
 	if (error)
 		goto out_put;
 
fs/gfs2/rgrp.c +0 -10
index 59bc4d1a4d89..692dc11d0f13 100644
@@ -590,16 +590,6 @@ void gfs2_free_clones(struct gfs2_rgrpd *rgd)
 	}
 }
 
-/**
- * gfs2_rsqa_alloc - make sure we have a reservation assigned to the inode
- *                 plus a quota allocations data structure, if necessary
- * @ip: the inode for this reservation
- */
-int gfs2_rsqa_alloc(struct gfs2_inode *ip)
-{
-	return gfs2_qa_alloc(ip);
-}
-
 static void dump_rs(struct seq_file *seq, const struct gfs2_blkreserv *rs,
 		    const char *fs_id_buf)
 {
fs/gfs2/rgrp.h +0 -1
index 008ef9736f1c..a1d7e14fc55b 100644
@@ -44,7 +44,6 @@ extern void gfs2_inplace_release(struct gfs2_inode *ip);
 extern int gfs2_alloc_blocks(struct gfs2_inode *ip, u64 *bn, unsigned int *n,
 			     bool dinode, u64 *generation);
 
-extern int gfs2_rsqa_alloc(struct gfs2_inode *ip);
 extern void gfs2_rs_deltree(struct gfs2_blkreserv *rs);
 extern void gfs2_rs_delete(struct gfs2_inode *ip, atomic_t *wcount);
 extern void __gfs2_free_blocks(struct gfs2_inode *ip, struct gfs2_rgrpd *rgd,
fs/gfs2/xattr.c +1 -1
index bbe593d16bea..c4fbb96e001f 100644
@@ -1222,7 +1222,7 @@ static int gfs2_xattr_set(const struct xattr_handler *handler,
 	struct gfs2_holder gh;
 	int ret;
 
-	ret = gfs2_rsqa_alloc(ip);
+	ret = gfs2_qa_alloc(ip);
 	if (ret)
 		return ret;
 
Signature · 1 line
2.24.1