Skip to contentSkip to conversation

gfs2: quota changes

PATCH In review Andreas Gruenbacher 2 participants 16 messages Jun 18, 2024 – Jun 20, 2024 GFS2 file system

lore
Share & tools
lore

Andreas Gruenbacher

author maintainer
#0 lore raw

cover gfs2: quota changes

We have recently discovered that commit 4c6a08125f22 ("gfs2: ignore negated quota changes") was causing corruption in "quota_change%u" files. A more in-depth review of the quota code has revealed several more problems, both long-standing and in the quota changes merged last September. Here are a number of fixes; please review.

Thanks,
Andreas

Andreas Gruenbacher (13):
gfs2: Minor gfs2_quota_init error path cleanup
gfs2: Check quota consistency on mount
gfs2: Revert "introduce qd_bh_get_or_undo"
gfs2: qd_check_sync cleanups
gfs2: Revert "ignore negated quota changes"
gfs2: Revert "Add quota_change type"
gfs2: Fix and clean up function do_qc
gfs2: quota need_sync cleanup
gfs2: Fold qd_fish into gfs2_quota_sync
gfs2: Add some missing quota locking
gfs2: Get rid of some unnecessary quota locking
gfs2: Be more careful with the quota sync generation
gfs2: Revert "check for no eligible quota changes"

 fs/gfs2/incore.h     |   1 -
 fs/gfs2/ops_fstype.c |   1 -
 fs/gfs2/quota.c      | 390 ++++++++++++++++++++++---------------------
 fs/gfs2/util.c       |   6 +-
 4 files changed, 205 insertions(+), 193 deletions(-)
Signature · 1 line
2.45.1
author maintainer
#1 lore raw

PATCH 1/13 gfs2: Minor gfs2_quota_init error path cleanup

Add a fail_brelse label and use it where useful. Move variable bh out of the loop to extend its visibility to the new label. No functional change.

---
 fs/gfs2/quota.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
fs/gfs2/quota.c +7 -9
index aa9cf0102848..d75eeb327060 100644
@@ -1407,6 +1407,7 @@ int gfs2_quota_init(struct gfs2_sbd *sdp)
 	unsigned int found = 0;
 	unsigned int hash;
 	unsigned int bm_size;
+	struct buffer_head *bh;
 	u64 dblock;
 	u32 extlen = 0;
 	int error;
@@ -1426,7 +1427,6 @@ int gfs2_quota_init(struct gfs2_sbd *sdp)
 		return error;
 
 	for (x = 0; x < blocks; x++) {
-		struct buffer_head *bh;
 		const struct gfs2_quota_change *qc;
 		unsigned int y;
 
@@ -1440,10 +1440,8 @@ int gfs2_quota_init(struct gfs2_sbd *sdp)
 		bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
 		if (!bh)
 			goto fail;
-		if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC)) {
-			brelse(bh);
-			goto fail;
-		}
+		if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC))
+			goto fail_brelse;
 
 		qc = (const struct gfs2_quota_change *)(bh->b_data + sizeof(struct gfs2_meta_header));
 		for (y = 0; y < sdp->sd_qc_per_block && slot < sdp->sd_quota_slots;
@@ -1461,10 +1459,8 @@ int gfs2_quota_init(struct gfs2_sbd *sdp)
 
 			hash = gfs2_qd_hash(sdp, qc_id);
 			qd = qd_alloc(hash, sdp, qc_id);
-			if (qd == NULL) {
-				brelse(bh);
-				goto fail;
-			}
+			if (qd == NULL)
+				goto fail_brelse;
 
 			set_bit(QDF_CHANGE, &qd->qd_flags);
 			qd->qd_change = qc_change;
@@ -1494,6 +1490,8 @@ int gfs2_quota_init(struct gfs2_sbd *sdp)
 
 	return 0;
 
+fail_brelse:
+	brelse(bh);
 fail:
 	gfs2_quota_cleanup(sdp);
 	return error;
Signature · 1 line
2.45.1
author maintainer
#2 lore raw

PATCH 2/13 gfs2: Check quota consistency on mount

In reply to Andreas Gruenbacher

In gfs2_quota_init(), make sure that the per-node "quota_change%u" file doesn't contain duplicate uids/gids. Those duplicates would cause us to acquire the glock corresponding to those ids repeatedly, which the glock code doesn't allow.

When finding inconsistencies, we wipe them out and ignore them. The resulting quotas will likely be inconsistent, and running quotacheck(1) is advised.

---
 fs/gfs2/quota.c | 37 +++++++++++++++++++++++++++++++------
 1 file changed, 31 insertions(+), 6 deletions(-)
fs/gfs2/quota.c +31 -6
index d75eeb327060..2984eaafdf6f 100644
@@ -1427,7 +1427,7 @@ int gfs2_quota_init(struct gfs2_sbd *sdp)
 		return error;
 
 	for (x = 0; x < blocks; x++) {
-		const struct gfs2_quota_change *qc;
+		struct gfs2_quota_change *qc;
 		unsigned int y;
 
 		if (!extlen) {
@@ -1443,10 +1443,10 @@ int gfs2_quota_init(struct gfs2_sbd *sdp)
 		if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC))
 			goto fail_brelse;
 
-		qc = (const struct gfs2_quota_change *)(bh->b_data + sizeof(struct gfs2_meta_header));
+		qc = (struct gfs2_quota_change *)(bh->b_data + sizeof(struct gfs2_meta_header));
 		for (y = 0; y < sdp->sd_qc_per_block && slot < sdp->sd_quota_slots;
 		     y++, slot++) {
-			struct gfs2_quota_data *qd;
+			struct gfs2_quota_data *old_qd, *qd;
 			s64 qc_change = be64_to_cpu(qc->qc_change);
 			u32 qc_flags = be32_to_cpu(qc->qc_flags);
 			enum quota_type qtype = (qc_flags & GFS2_QCF_USER) ?
@@ -1468,18 +1468,41 @@ int gfs2_quota_init(struct gfs2_sbd *sdp)
 			qd->qd_slot_ref = 1;
 
 			spin_lock(&qd_lock);
+			spin_lock_bucket(hash);
+			old_qd = gfs2_qd_search_bucket(hash, sdp, qc_id);
+			if (old_qd) {
+				fs_err(sdp, "Corruption found in quota_change%u"
+					    "file: duplicate identifier in "
+					    "slot %u\n",
+					    sdp->sd_jdesc->jd_jid, slot);
+
+				spin_unlock_bucket(hash);
+				spin_unlock(&qd_lock);
+				qd_put(old_qd);
+
+				gfs2_glock_put(qd->qd_gl);
+				kmem_cache_free(gfs2_quotad_cachep, qd);
+
+				/* zero out the duplicate slot */
+				lock_buffer(bh);
+				memset(qc, 0, sizeof(*qc));
+				mark_buffer_dirty(bh);
+				unlock_buffer(bh);
+
+				continue;
+			}
 			BUG_ON(test_and_set_bit(slot, sdp->sd_quota_bitmap));
 			list_add(&qd->qd_list, &sdp->sd_quota_list);
 			atomic_inc(&sdp->sd_quota_count);
-			spin_unlock(&qd_lock);
-
-			spin_lock_bucket(hash);
 			hlist_bl_add_head_rcu(&qd->qd_hlist, &qd_hash_table[hash]);
 			spin_unlock_bucket(hash);
+			spin_unlock(&qd_lock);
 
 			found++;
 		}
 
+		if (buffer_dirty(bh))
+			sync_dirty_buffer(bh);
 		brelse(bh);
 		dblock++;
 		extlen--;
@@ -1491,6 +1514,8 @@ int gfs2_quota_init(struct gfs2_sbd *sdp)
 	return 0;
 
 fail_brelse:
+	if (buffer_dirty(bh))
+		sync_dirty_buffer(bh);
 	brelse(bh);
 fail:
 	gfs2_quota_cleanup(sdp);
Signature · 1 line
2.45.1
author maintainer
#3 lore raw

PATCH 3/13 gfs2: Revert "introduce qd_bh_get_or_undo"

In reply to Andreas Gruenbacher

The qd_bh_get_or_undo() helper introduced by that commit doesn't improve the code much, so revert it and clean things up in a more useful way in the next commit.

This reverts commit 7dbc6ae60dd7089d8ed42892b6a66c138f0aa7a0.

---
 fs/gfs2/quota.c | 36 +++++++++++++++++-------------------
 1 file changed, 17 insertions(+), 19 deletions(-)
fs/gfs2/quota.c +17 -19
index 2984eaafdf6f..e0840b412d28 100644
@@ -491,20 +491,6 @@ static int qd_check_sync(struct gfs2_sbd *sdp, struct gfs2_quota_data *qd,
 	return 1;
 }
 
-static int qd_bh_get_or_undo(struct gfs2_sbd *sdp, struct gfs2_quota_data *qd)
-{
-	int error;
-
-	error = bh_get(qd);
-	if (!error)
-		return 0;
-
-	clear_bit(QDF_LOCKED, &qd->qd_flags);
-	slot_put(qd);
-	qd_put(qd);
-	return error;
-}
-
 static int qd_fish(struct gfs2_sbd *sdp, struct gfs2_quota_data **qdp)
 {
 	struct gfs2_quota_data *qd = NULL, *iter;
@@ -527,12 +513,17 @@ static int qd_fish(struct gfs2_sbd *sdp, struct gfs2_quota_data **qdp)
 	spin_unlock(&qd_lock);
 
 	if (qd) {
-		error = qd_bh_get_or_undo(sdp, qd);
-		if (error)
+		error = bh_get(qd);
+		if (error) {
+			clear_bit(QDF_LOCKED, &qd->qd_flags);
+			slot_put(qd);
+			qd_put(qd);
 			return error;
-		*qdp = qd;
+		}
 	}
 
+	*qdp = qd;
+
 	return 0;
 }
 
@@ -1189,8 +1180,15 @@ void gfs2_quota_unlock(struct gfs2_inode *ip)
 		if (!found)
 			continue;
 
-		if (!qd_bh_get_or_undo(sdp, qd))
-			qda[count++] = qd;
+		gfs2_assert_warn(sdp, qd->qd_change_sync);
+		if (bh_get(qd)) {
+			clear_bit(QDF_LOCKED, &qd->qd_flags);
+			slot_put(qd);
+			qd_put(qd);
+			continue;
+		}
+
+		qda[count++] = qd;
 	}
 
 	if (count) {
Signature · 1 line
2.45.1
author maintainer
#4 lore raw

PATCH 4/13 gfs2: qd_check_sync cleanups

In reply to Andreas Gruenbacher

Rename qd_check_sync() to qd_grab_sync() and make it return a bool. Turn the sync_gen pointer into a regular u64 and pass in U64_MAX instead of a NULL pointer when sync generation checking isn't needed.

Introduce a new qd_ungrab_sync() helper for undoing the effects of qd_grab_sync() if the subsequent bh_get() on the qd object fails.

---
 fs/gfs2/quota.c | 40 ++++++++++++++++++++++------------------
 1 file changed, 22 insertions(+), 18 deletions(-)
fs/gfs2/quota.c +22 -18
index e0840b412d28..bbd66adaec3d 100644
@@ -462,13 +462,13 @@ static void bh_put(struct gfs2_quota_data *qd)
 	mutex_unlock(&sdp->sd_quota_mutex);
 }
 
-static int qd_check_sync(struct gfs2_sbd *sdp, struct gfs2_quota_data *qd,
-			 u64 *sync_gen)
+static bool qd_grab_sync(struct gfs2_sbd *sdp, struct gfs2_quota_data *qd,
+			 u64 sync_gen)
 {
 	if (test_bit(QDF_LOCKED, &qd->qd_flags) ||
 	    !test_bit(QDF_CHANGE, &qd->qd_flags) ||
-	    (sync_gen && (qd->qd_sync_gen >= *sync_gen)))
-		return 0;
+	    qd->qd_sync_gen >= sync_gen)
+		return false;
 
 	/*
 	 * If qd_change is 0 it means a pending quota change was negated.
@@ -478,17 +478,24 @@ static int qd_check_sync(struct gfs2_sbd *sdp, struct gfs2_quota_data *qd,
 	if (!qd->qd_change && test_and_clear_bit(QDF_CHANGE, &qd->qd_flags)) {
 		slot_put(qd);
 		qd_put(qd);
-		return 0;
+		return false;
 	}
 
 	if (!lockref_get_not_dead(&qd->qd_lockref))
-		return 0;
+		return false;
 
 	list_move_tail(&qd->qd_list, &sdp->sd_quota_list);
 	set_bit(QDF_LOCKED, &qd->qd_flags);
 	qd->qd_change_sync = qd->qd_change;
 	slot_hold(qd);
-	return 1;
+	return true;
+}
+
+static void qd_ungrab_sync(struct gfs2_quota_data *qd)
+{
+	clear_bit(QDF_LOCKED, &qd->qd_flags);
+	slot_put(qd);
+	qd_put(qd);
 }
 
 static int qd_fish(struct gfs2_sbd *sdp, struct gfs2_quota_data **qdp)
@@ -504,7 +511,7 @@ static int qd_fish(struct gfs2_sbd *sdp, struct gfs2_quota_data **qdp)
 	spin_lock(&qd_lock);
 
 	list_for_each_entry(iter, &sdp->sd_quota_list, qd_list) {
-		if (qd_check_sync(sdp, iter, &sdp->sd_quota_sync_gen)) {
+		if (qd_grab_sync(sdp, iter, sdp->sd_quota_sync_gen)) {
 			qd = iter;
 			break;
 		}
@@ -515,9 +522,7 @@ static int qd_fish(struct gfs2_sbd *sdp, struct gfs2_quota_data **qdp)
 	if (qd) {
 		error = bh_get(qd);
 		if (error) {
-			clear_bit(QDF_LOCKED, &qd->qd_flags);
-			slot_put(qd);
-			qd_put(qd);
+			qd_ungrab_sync(qd);
 			return error;
 		}
 	}
@@ -1157,7 +1162,6 @@ void gfs2_quota_unlock(struct gfs2_inode *ip)
 	struct gfs2_quota_data *qda[2 * GFS2_MAXQUOTAS];
 	unsigned int count = 0;
 	u32 x;
-	int found;
 
 	if (!test_and_clear_bit(GIF_QD_LOCKED, &ip->i_flags))
 		return;
@@ -1165,6 +1169,7 @@ void gfs2_quota_unlock(struct gfs2_inode *ip)
 	for (x = 0; x < ip->i_qadata->qa_qd_num; x++) {
 		struct gfs2_quota_data *qd;
 		bool sync;
+		int error;
 
 		qd = ip->i_qadata->qa_qd[x];
 		sync = need_sync(qd);
@@ -1174,17 +1179,16 @@ void gfs2_quota_unlock(struct gfs2_inode *ip)
 			continue;
 
 		spin_lock(&qd_lock);
-		found = qd_check_sync(sdp, qd, NULL);
+		sync = qd_grab_sync(sdp, qd, U64_MAX);
 		spin_unlock(&qd_lock);
 
-		if (!found)
+		if (!sync)
 			continue;
 
 		gfs2_assert_warn(sdp, qd->qd_change_sync);
-		if (bh_get(qd)) {
-			clear_bit(QDF_LOCKED, &qd->qd_flags);
-			slot_put(qd);
-			qd_put(qd);
+		error = bh_get(qd);
+		if (error) {
+			qd_ungrab_sync(qd);
 			continue;
 		}
 
Signature · 1 line
2.45.1
author maintainer
#5 lore raw

PATCH 5/13 gfs2: Revert "ignore negated quota changes"

In reply to Andreas Gruenbacher

Commit 4c6a08125f22 ("gfs2: ignore negated quota changes") skips quota changes with qd_change == 0 instead of writing them back, which leaves behind non-zero qd_change values in the affected slots. The kernel then assumes that those slots are unused, while the qd_change values on disk indicate that they are indeed still in use. The next time the filesystem is mounted, those invalid slots are read in from disk, which will cause inconsistencies.

Revert that commit to avoid filesystem corruption.

This reverts commit 4c6a08125f2249531ec01783a5f4317d7342add5.

---
 fs/gfs2/quota.c | 11 -----------
 1 file changed, 11 deletions(-)
fs/gfs2/quota.c +0 -11
index bbd66adaec3d..f0aaae07a598 100644
@@ -470,17 +470,6 @@ static bool qd_grab_sync(struct gfs2_sbd *sdp, struct gfs2_quota_data *qd,
 	    qd->qd_sync_gen >= sync_gen)
 		return false;
 
-	/*
-	 * If qd_change is 0 it means a pending quota change was negated.
-	 * We should not sync it, but we still have a qd reference and slot
-	 * reference taken by gfs2_quota_change -> do_qc that need to be put.
-	 */
-	if (!qd->qd_change && test_and_clear_bit(QDF_CHANGE, &qd->qd_flags)) {
-		slot_put(qd);
-		qd_put(qd);
-		return false;
-	}
-
 	if (!lockref_get_not_dead(&qd->qd_lockref))
 		return false;
 
Signature · 1 line
2.45.1
author maintainer
#6 lore raw

PATCH 6/13 gfs2: Revert "Add quota_change type"

In reply to Andreas Gruenbacher

Commit 432928c93779 ("gfs2: Add quota_change type") makes the incorrect assertion that function do_qc() should behave differently in the two contexts it is used in, but that isn't actually true. In all cases, do_qc() grabs a "reference" when it starts using a slot in the per-node quota changes file, and it releases that "reference" when no more residual changes remain. Revert that broken commit.

There are some remaining issues with function do_qc() which are addressed in the next commit.

This reverts commit 432928c9377959684c748a9bc6553ed2d3c2ea4f.

---
 fs/gfs2/quota.c | 19 +++++++------------
 fs/gfs2/util.c  |  6 +++---
 2 files changed, 10 insertions(+), 15 deletions(-)
fs/gfs2/quota.c +7 -12
index f0aaae07a598..371fad7d64e7 100644
@@ -75,9 +75,6 @@
 #define GFS2_QD_HASH_SIZE       BIT(GFS2_QD_HASH_SHIFT)
 #define GFS2_QD_HASH_MASK       (GFS2_QD_HASH_SIZE - 1)
 
-#define QC_CHANGE 0
-#define QC_SYNC 1
-
 /* Lock order: qd_lock -> bucket lock -> qd->lockref.lock -> lru lock */
 /*                     -> sd_bitmap_lock                              */
 static DEFINE_SPINLOCK(qd_lock);
@@ -695,7 +692,7 @@ static int sort_qd(const void *a, const void *b)
 	return 0;
 }
 
-static void do_qc(struct gfs2_quota_data *qd, s64 change, int qc_type)
+static void do_qc(struct gfs2_quota_data *qd, s64 change)
 {
 	struct gfs2_sbd *sdp = qd->qd_sbd;
 	struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
@@ -720,18 +717,16 @@ static void do_qc(struct gfs2_quota_data *qd, s64 change, int qc_type)
 	qd->qd_change = x;
 	spin_unlock(&qd_lock);
 
-	if (qc_type == QC_CHANGE) {
-		if (!test_and_set_bit(QDF_CHANGE, &qd->qd_flags)) {
-			qd_hold(qd);
-			slot_hold(qd);
-		}
-	} else {
+	if (!x) {
 		gfs2_assert_warn(sdp, test_bit(QDF_CHANGE, &qd->qd_flags));
 		clear_bit(QDF_CHANGE, &qd->qd_flags);
 		qc->qc_flags = 0;
 		qc->qc_id = 0;
 		slot_put(qd);
 		qd_put(qd);
+	} else if (!test_and_set_bit(QDF_CHANGE, &qd->qd_flags)) {
+		qd_hold(qd);
+		slot_hold(qd);
 	}
 
 	if (change < 0) /* Reset quiet flag if we freed some blocks */
@@ -977,7 +972,7 @@ static int do_sync(unsigned int num_qd, struct gfs2_quota_data **qda)
 		if (error)
 			goto out_end_trans;
 
-		do_qc(qd, -qd->qd_change_sync, QC_SYNC);
+		do_qc(qd, -qd->qd_change_sync);
 		set_bit(QDF_REFRESH, &qd->qd_flags);
 	}
 
@@ -1303,7 +1298,7 @@ void gfs2_quota_change(struct gfs2_inode *ip, s64 change,
 
 		if (qid_eq(qd->qd_id, make_kqid_uid(uid)) ||
 		    qid_eq(qd->qd_id, make_kqid_gid(gid))) {
-			do_qc(qd, change, QC_CHANGE);
+			do_qc(qd, change);
 		}
 	}
 }
fs/gfs2/util.c +3 -3
index 7e79b1a9e35f..13be8d1d228b 100644
@@ -99,12 +99,12 @@ int check_journal_clean(struct gfs2_sbd *sdp, struct gfs2_jdesc *jd,
  */
 int gfs2_freeze_lock_shared(struct gfs2_sbd *sdp)
 {
+	int flags = LM_FLAG_NOEXP | GL_EXACT;
 	int error;
 
-	error = gfs2_glock_nq_init(sdp->sd_freeze_gl, LM_ST_SHARED,
-				   LM_FLAG_NOEXP | GL_EXACT,
+	error = gfs2_glock_nq_init(sdp->sd_freeze_gl, LM_ST_SHARED, flags,
 				   &sdp->sd_freeze_gh);
-	if (error)
+	if (error && error != GLR_TRYFAILED)
 		fs_err(sdp, "can't lock the freeze glock: %d\n", error);
 	return error;
 }
Signature · 1 line
2.45.1
author maintainer
#7 lore raw

PATCH 7/13 gfs2: Fix and clean up function do_qc

In reply to Andreas Gruenbacher

Function do_qc() is supposed to be conceptually simple: it alters the current in-memory and on-disk quota change values for a given uid/gid by a given delta. If the on-disk record isn't defined yet, a new record is created. If the on-disk record exists and the resulting change value is zero, there no longer is a need for that record and so the record is deleted. On top of that, some reference counting is involved when creating and deleting records.

Currently, instead of doing the above, do_qc() alters the on-disk value and then it sets the in-memory value to the on-disk value. This is incorrect when the on-disk value differs from the in-memory value. The two values are allowed to differ when quota changes are synced to the global quota file. Fix by changing both values by the same amount.

In addition, do_qc() currently gets confused when the delta value is 0. It isn't supposed to be called that way, but that assumption isn't mentioned and it makes the code harder to read. Make the code more explicit.

---
 fs/gfs2/quota.c | 34 +++++++++++++++++++++-------------
 1 file changed, 21 insertions(+), 13 deletions(-)
fs/gfs2/quota.c +21 -13
index 371fad7d64e7..29a69756cc32 100644
@@ -702,32 +702,40 @@ static void do_qc(struct gfs2_quota_data *qd, s64 change)
 	mutex_lock(&sdp->sd_quota_mutex);
 	gfs2_trans_add_meta(ip->i_gl, qd->qd_bh);
 
-	if (!test_bit(QDF_CHANGE, &qd->qd_flags)) {
-		qc->qc_change = 0;
-		qc->qc_flags = 0;
-		if (qd->qd_id.type == USRQUOTA)
-			qc->qc_flags = cpu_to_be32(GFS2_QCF_USER);
-		qc->qc_id = cpu_to_be32(from_kqid(&init_user_ns, qd->qd_id));
-	}
+	/*
+	 * The QDF_CHANGE flag indicates that the slot in the quota change file
+	 * is used.  Here, we use the value of qc->qc_change when the slot is
+	 * used, and we assume a value of 0 otherwise.
+	 */
 
-	x = be64_to_cpu(qc->qc_change) + change;
-	qc->qc_change = cpu_to_be64(x);
+	x = 0;
+	if (test_bit(QDF_CHANGE, &qd->qd_flags))
+		x = be64_to_cpu(qc->qc_change);
+	x += change;
 
 	spin_lock(&qd_lock);
-	qd->qd_change = x;
+	qd->qd_change += change;
 	spin_unlock(&qd_lock);
 
-	if (!x) {
-		gfs2_assert_warn(sdp, test_bit(QDF_CHANGE, &qd->qd_flags));
+	if (!x && test_bit(QDF_CHANGE, &qd->qd_flags)) {
+		/* The slot in the quota change file becomes unused. */
 		clear_bit(QDF_CHANGE, &qd->qd_flags);
 		qc->qc_flags = 0;
 		qc->qc_id = 0;
 		slot_put(qd);
 		qd_put(qd);
-	} else if (!test_and_set_bit(QDF_CHANGE, &qd->qd_flags)) {
+	} else if (x && !test_bit(QDF_CHANGE, &qd->qd_flags)) {
+		/* The slot in the quota change file becomes used. */
+		set_bit(QDF_CHANGE, &qd->qd_flags);
 		qd_hold(qd);
 		slot_hold(qd);
+
+		qc->qc_flags = 0;
+		if (qd->qd_id.type == USRQUOTA)
+			qc->qc_flags = cpu_to_be32(GFS2_QCF_USER);
+		qc->qc_id = cpu_to_be32(from_kqid(&init_user_ns, qd->qd_id));
 	}
+	qc->qc_change = cpu_to_be64(x);
 
 	if (change < 0) /* Reset quiet flag if we freed some blocks */
 		clear_bit(QDF_QMSG_QUIET, &qd->qd_flags);
Signature · 1 line
2.45.1
author maintainer
#8 lore raw

PATCH 8/13 gfs2: quota need_sync cleanup

In reply to Andreas Gruenbacher

Rename variable 'value' to 'change' as it stores a change in value.

Add new 'value' and 'limit' variables for the current value and limit.

Only fetch the tuning parameters when we need them.

Get rid of unnecessary nesting.

No change in functionality.

---
 fs/gfs2/quota.c | 26 ++++++++++++--------------
 1 file changed, 12 insertions(+), 14 deletions(-)
fs/gfs2/quota.c +12 -14
index 29a69756cc32..11bd16d8f81c 100644
@@ -1117,34 +1117,32 @@ static bool need_sync(struct gfs2_quota_data *qd)
 {
 	struct gfs2_sbd *sdp = qd->qd_sbd;
 	struct gfs2_tune *gt = &sdp->sd_tune;
-	s64 value;
+	s64 value, change, limit;
 	unsigned int num, den;
 
 	if (!qd->qd_qb.qb_limit)
 		return false;
 
 	spin_lock(&qd_lock);
-	value = qd->qd_change;
+	change = qd->qd_change;
 	spin_unlock(&qd_lock);
 
+	if (change <= 0)
+		return false;
+	value = (s64)be64_to_cpu(qd->qd_qb.qb_value);
+	limit = (s64)be64_to_cpu(qd->qd_qb.qb_limit);
+	if (value >= limit)
+		return false;
+
 	spin_lock(&gt->gt_spin);
 	num = gt->gt_quota_scale_num;
 	den = gt->gt_quota_scale_den;
 	spin_unlock(&gt->gt_spin);
 
-	if (value <= 0)
-		return false;
-	else if ((s64)be64_to_cpu(qd->qd_qb.qb_value) >=
-		 (s64)be64_to_cpu(qd->qd_qb.qb_limit))
+	change *= gfs2_jindex_size(sdp) * num;
+	change = div_s64(change, den);
+	if (value + change < limit)
 		return false;
-	else {
-		value *= gfs2_jindex_size(sdp) * num;
-		value = div_s64(value, den);
-		value += (s64)be64_to_cpu(qd->qd_qb.qb_value);
-		if (value < (s64)be64_to_cpu(qd->qd_qb.qb_limit))
-			return false;
-	}
-
 	return true;
 }
 
Signature · 1 line
2.45.1
author maintainer
#9 lore raw

PATCH 9/13 gfs2: Fold qd_fish into gfs2_quota_sync

In reply to Andreas Gruenbacher

The split between qd_fish() and gfs2_quota_sync() is rather unfortunate as qd_fish() is repeatedly called to scan sdp->sd_quota_list only to find the next object to that needs syncing; if there are multiple objects on the list that need syncing, it makes more sense to grab them all in one go. This is relatively easy to do when qd_fish() is folded into gfs2_quota_sync().

---
 fs/gfs2/quota.c | 77 +++++++++++++++++++------------------------------
 1 file changed, 29 insertions(+), 48 deletions(-)
fs/gfs2/quota.c +29 -48
index 11bd16d8f81c..ae02403407aa 100644
@@ -484,40 +484,6 @@ static void qd_ungrab_sync(struct gfs2_quota_data *qd)
 	qd_put(qd);
 }
 
-static int qd_fish(struct gfs2_sbd *sdp, struct gfs2_quota_data **qdp)
-{
-	struct gfs2_quota_data *qd = NULL, *iter;
-	int error;
-
-	*qdp = NULL;
-
-	if (sb_rdonly(sdp->sd_vfs))
-		return 0;
-
-	spin_lock(&qd_lock);
-
-	list_for_each_entry(iter, &sdp->sd_quota_list, qd_list) {
-		if (qd_grab_sync(sdp, iter, sdp->sd_quota_sync_gen)) {
-			qd = iter;
-			break;
-		}
-	}
-
-	spin_unlock(&qd_lock);
-
-	if (qd) {
-		error = bh_get(qd);
-		if (error) {
-			qd_ungrab_sync(qd);
-			return error;
-		}
-	}
-
-	*qdp = qd;
-
-	return 0;
-}
-
 static void qdsb_put(struct gfs2_quota_data *qd)
 {
 	bh_put(qd);
@@ -1332,10 +1298,10 @@ int gfs2_quota_sync(struct super_block *sb, int type)
 	struct gfs2_sbd *sdp = sb->s_fs_info;
 	struct gfs2_quota_data **qda;
 	unsigned int max_qd = PAGE_SIZE / sizeof(struct gfs2_holder);
-	unsigned int num_qd;
-	unsigned int x;
 	int error = 0;
 
+	if (sb_rdonly(sdp->sd_vfs))
+		return 0;
 	if (!qd_changed(sdp))
 		return 0;
 
@@ -1347,24 +1313,39 @@ int gfs2_quota_sync(struct super_block *sb, int type)
 	sdp->sd_quota_sync_gen++;
 
 	do {
-		num_qd = 0;
+		struct gfs2_quota_data *iter;
+		unsigned int num_qd = 0;
+		unsigned int x;
 
-		for (;;) {
-			error = qd_fish(sdp, qda + num_qd);
-			if (error || !qda[num_qd])
-				break;
-			if (++num_qd == max_qd)
-				break;
+		spin_lock(&qd_lock);
+		list_for_each_entry(iter, &sdp->sd_quota_list, qd_list) {
+			if (qd_grab_sync(sdp, iter, sdp->sd_quota_sync_gen)) {
+				qda[num_qd++] = iter;
+				if (num_qd == max_qd)
+					break;
+			}
 		}
+		spin_unlock(&qd_lock);
 
-		if (num_qd) {
+		if (!num_qd)
+			break;
+
+		for (x = 0; x < num_qd; x++) {
+			error = bh_get(qda[x]);
 			if (!error)
-				error = do_sync(num_qd, qda);
+				continue;
 
-			for (x = 0; x < num_qd; x++)
-				qd_unlock(qda[x]);
+			while (x < num_qd)
+				qd_ungrab_sync(qda[--num_qd]);
+			break;
 		}
-	} while (!error && num_qd == max_qd);
+
+		if (!error)
+			error = do_sync(num_qd, qda);
+
+		for (x = 0; x < num_qd; x++)
+			qd_unlock(qda[x]);
+	} while (!error);
 
 	mutex_unlock(&sdp->sd_quota_sync_mutex);
 	kfree(qda);
Signature · 1 line
2.45.1
author maintainer
#10 lore raw

PATCH 10/13 gfs2: Add some missing quota locking

In reply to Andreas Gruenbacher

The quota code is missing some locking between local quota changes and syncing those quota changes to the global quota file (gfs2_quota_sync); in particular, qd->qd_change needs to be kept in sync with the QDF_CHANGE change flag and the number of references held. Use the qd->qd_lockref.lock spinlock for that.

With the qd->qd_lockref.lock spinlock held, we can no longer call lockref_get(), so turn qd_hold() into a variant that assumes that the lock is held. This function is really supposed to take an additional reference when one or more references are already held, so check for that instead of checking if the lockref is dead.

---
 fs/gfs2/quota.c | 82 ++++++++++++++++++++++++++++++++-----------------
 1 file changed, 53 insertions(+), 29 deletions(-)
fs/gfs2/quota.c +53 -29
index ae02403407aa..283c6ff21911 100644
@@ -316,11 +316,11 @@ static int qd_get(struct gfs2_sbd *sdp, struct kqid qid,
 }
 
 
-static void qd_hold(struct gfs2_quota_data *qd)
+static void __qd_hold(struct gfs2_quota_data *qd)
 {
 	struct gfs2_sbd *sdp = qd->qd_sbd;
-	gfs2_assert(sdp, !__lockref_is_dead(&qd->qd_lockref));
-	lockref_get(&qd->qd_lockref);
+	gfs2_assert(sdp, qd->qd_lockref.count > 0);
+	qd->qd_lockref.count++;
 }
 
 static void qd_put(struct gfs2_quota_data *qd)
@@ -462,19 +462,27 @@ static void bh_put(struct gfs2_quota_data *qd)
 static bool qd_grab_sync(struct gfs2_sbd *sdp, struct gfs2_quota_data *qd,
 			 u64 sync_gen)
 {
+	bool ret = false;
+
+	spin_lock(&qd->qd_lockref.lock);
 	if (test_bit(QDF_LOCKED, &qd->qd_flags) ||
 	    !test_bit(QDF_CHANGE, &qd->qd_flags) ||
 	    qd->qd_sync_gen >= sync_gen)
-		return false;
+		goto out;
 
-	if (!lockref_get_not_dead(&qd->qd_lockref))
-		return false;
+	if (__lockref_is_dead(&qd->qd_lockref))
+		goto out;
+	qd->qd_lockref.count++;
 
 	list_move_tail(&qd->qd_list, &sdp->sd_quota_list);
 	set_bit(QDF_LOCKED, &qd->qd_flags);
 	qd->qd_change_sync = qd->qd_change;
 	slot_hold(qd);
-	return true;
+	ret = true;
+
+out:
+	spin_unlock(&qd->qd_lockref.lock);
+	return ret;
 }
 
 static void qd_ungrab_sync(struct gfs2_quota_data *qd)
@@ -493,8 +501,10 @@ static void qdsb_put(struct gfs2_quota_data *qd)
 
 static void qd_unlock(struct gfs2_quota_data *qd)
 {
+	spin_lock(&qd->qd_lockref.lock);
 	gfs2_assert_warn(qd->qd_sbd, test_bit(QDF_LOCKED, &qd->qd_flags));
 	clear_bit(QDF_LOCKED, &qd->qd_flags);
+	spin_unlock(&qd->qd_lockref.lock);
 	qdsb_put(qd);
 }
 
@@ -663,6 +673,7 @@ static void do_qc(struct gfs2_quota_data *qd, s64 change)
 	struct gfs2_sbd *sdp = qd->qd_sbd;
 	struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
 	struct gfs2_quota_change *qc = qd->qd_bh_qc;
+	bool needs_put = false;
 	s64 x;
 
 	mutex_lock(&sdp->sd_quota_mutex);
@@ -674,26 +685,24 @@ static void do_qc(struct gfs2_quota_data *qd, s64 change)
 	 * used, and we assume a value of 0 otherwise.
 	 */
 
+	spin_lock(&qd->qd_lockref.lock);
+
 	x = 0;
 	if (test_bit(QDF_CHANGE, &qd->qd_flags))
 		x = be64_to_cpu(qc->qc_change);
 	x += change;
-
-	spin_lock(&qd_lock);
 	qd->qd_change += change;
-	spin_unlock(&qd_lock);
 
 	if (!x && test_bit(QDF_CHANGE, &qd->qd_flags)) {
 		/* The slot in the quota change file becomes unused. */
 		clear_bit(QDF_CHANGE, &qd->qd_flags);
 		qc->qc_flags = 0;
 		qc->qc_id = 0;
-		slot_put(qd);
-		qd_put(qd);
+		needs_put = true;
 	} else if (x && !test_bit(QDF_CHANGE, &qd->qd_flags)) {
 		/* The slot in the quota change file becomes used. */
 		set_bit(QDF_CHANGE, &qd->qd_flags);
-		qd_hold(qd);
+		__qd_hold(qd);
 		slot_hold(qd);
 
 		qc->qc_flags = 0;
@@ -703,6 +712,12 @@ static void do_qc(struct gfs2_quota_data *qd, s64 change)
 	}
 	qc->qc_change = cpu_to_be64(x);
 
+	spin_unlock(&qd->qd_lockref.lock);
+
+	if (needs_put) {
+		slot_put(qd);
+		qd_put(qd);
+	}
 	if (change < 0) /* Reset quiet flag if we freed some blocks */
 		clear_bit(QDF_QMSG_QUIET, &qd->qd_flags);
 	mutex_unlock(&sdp->sd_quota_mutex);
@@ -844,6 +859,7 @@ static int gfs2_adjust_quota(struct gfs2_sbd *sdp, loff_t loc,
 	be64_add_cpu(&q.qu_value, change);
 	if (((s64)be64_to_cpu(q.qu_value)) < 0)
 		q.qu_value = 0; /* Never go negative on quota usage */
+	spin_lock(&qd->qd_lockref.lock);
 	qd->qd_qb.qb_value = q.qu_value;
 	if (fdq) {
 		if (fdq->d_fieldmask & QC_SPC_SOFT) {
@@ -859,6 +875,7 @@ static int gfs2_adjust_quota(struct gfs2_sbd *sdp, loff_t loc,
 			qd->qd_qb.qb_value = q.qu_value;
 		}
 	}
+	spin_unlock(&qd->qd_lockref.lock);
 
 	err = gfs2_write_disk_quota(sdp, &q, loc);
 	if (!err) {
@@ -990,7 +1007,9 @@ static int update_qd(struct gfs2_sbd *sdp, struct gfs2_quota_data *qd)
 	qlvb->qb_limit = q.qu_limit;
 	qlvb->qb_warn = q.qu_warn;
 	qlvb->qb_value = q.qu_value;
+	spin_lock(&qd->qd_lockref.lock);
 	qd->qd_qb = *qlvb;
+	spin_unlock(&qd->qd_lockref.lock);
 
 	return 0;
 }
@@ -1012,7 +1031,9 @@ static int do_glock(struct gfs2_quota_data *qd, int force_refresh,
 	if (test_and_clear_bit(QDF_REFRESH, &qd->qd_flags))
 		force_refresh = FORCE;
 
+	spin_lock(&qd->qd_lockref.lock);
 	qd->qd_qb = *(struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr;
+	spin_unlock(&qd->qd_lockref.lock);
 
 	if (force_refresh || qd->qd_qb.qb_magic != cpu_to_be32(GFS2_MAGIC)) {
 		gfs2_glock_dq_uninit(q_gh);
@@ -1085,20 +1106,19 @@ static bool need_sync(struct gfs2_quota_data *qd)
 	struct gfs2_tune *gt = &sdp->sd_tune;
 	s64 value, change, limit;
 	unsigned int num, den;
+	int ret = false;
 
+	spin_lock(&qd->qd_lockref.lock);
 	if (!qd->qd_qb.qb_limit)
-		return false;
+		goto out;
 
-	spin_lock(&qd_lock);
 	change = qd->qd_change;
-	spin_unlock(&qd_lock);
-
 	if (change <= 0)
-		return false;
+		goto out;
 	value = (s64)be64_to_cpu(qd->qd_qb.qb_value);
 	limit = (s64)be64_to_cpu(qd->qd_qb.qb_limit);
 	if (value >= limit)
-		return false;
+		goto out;
 
 	spin_lock(&gt->gt_spin);
 	num = gt->gt_quota_scale_num;
@@ -1108,8 +1128,12 @@ static bool need_sync(struct gfs2_quota_data *qd)
 	change *= gfs2_jindex_size(sdp) * num;
 	change = div_s64(change, den);
 	if (value + change < limit)
-		return false;
-	return true;
+		goto out;
+
+	ret = true;
+out:
+	spin_unlock(&qd->qd_lockref.lock);
+	return ret;
 }
 
 void gfs2_quota_unlock(struct gfs2_inode *ip)
@@ -1211,12 +1235,12 @@ int gfs2_quota_check(struct gfs2_inode *ip, kuid_t uid, kgid_t gid,
 		      qid_eq(qd->qd_id, make_kqid_gid(gid))))
 			continue;
 
+		spin_lock(&qd->qd_lockref.lock);
 		warn = (s64)be64_to_cpu(qd->qd_qb.qb_warn);
 		limit = (s64)be64_to_cpu(qd->qd_qb.qb_limit);
 		value = (s64)be64_to_cpu(qd->qd_qb.qb_value);
-		spin_lock(&qd_lock);
 		value += qd->qd_change;
-		spin_unlock(&qd_lock);
+		spin_unlock(&qd->qd_lockref.lock);
 
 		if (limit > 0 && (limit - value) < ap->allowed)
 			ap->allowed = limit - value;
@@ -1282,12 +1306,12 @@ static bool qd_changed(struct gfs2_sbd *sdp)
 
 	spin_lock(&qd_lock);
 	list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) {
-		if (test_bit(QDF_LOCKED, &qd->qd_flags) ||
-		    !test_bit(QDF_CHANGE, &qd->qd_flags))
-			continue;
-
-		changed = true;
-		break;
+		spin_lock(&qd->qd_lockref.lock);
+		changed = !test_bit(QDF_LOCKED, &qd->qd_flags) &&
+			  test_bit(QDF_CHANGE, &qd->qd_flags);
+		spin_unlock(&qd->qd_lockref.lock);
+		if (changed)
+			break;
 	}
 	spin_unlock(&qd_lock);
 	return changed;
Signature · 1 line
2.45.1
author maintainer
#11 lore raw

PATCH 11/13 gfs2: Get rid of some unnecessary quota locking

In reply to Andreas Gruenbacher

With the locking the previous patch has introduced for each struct gfs2_quota_data object, sd_quota_mutex has become largely irrelevant. By waiting on the buffer head instead of waiting on the mutex in get_bh(), it becomes completely irrelevant and can be removed.

---
 fs/gfs2/incore.h     |  1 -
 fs/gfs2/ops_fstype.c |  1 -
 fs/gfs2/quota.c      | 55 +++++++++++++++++++++++---------------------
 3 files changed, 29 insertions(+), 28 deletions(-)
fs/gfs2/incore.h +0 -1
index 90fd2584357a..aa4ef67a34e0 100644
@@ -782,7 +782,6 @@ struct gfs2_sbd {
 
 	struct list_head sd_quota_list;
 	atomic_t sd_quota_count;
-	struct mutex sd_quota_mutex;
 	struct mutex sd_quota_sync_mutex;
 	wait_queue_head_t sd_quota_wait;
 
fs/gfs2/ops_fstype.c +0 -1
index 0561edd6cc86..ff1f3e3dc65c 100644
@@ -103,7 +103,6 @@ static struct gfs2_sbd *init_sbd(struct super_block *sb)
 	init_completion(&sdp->sd_journal_ready);
 
 	INIT_LIST_HEAD(&sdp->sd_quota_list);
-	mutex_init(&sdp->sd_quota_mutex);
 	mutex_init(&sdp->sd_quota_sync_mutex);
 	init_waitqueue_head(&sdp->sd_quota_wait);
 	spin_lock_init(&sdp->sd_bitmap_lock);
fs/gfs2/quota.c +29 -26

107 lines not on this page · raw message

Signature · 1 line
2.45.1
#12 lore raw

Re: [PATCH 11/13] gfs2: Get rid of some unnecessary quota locking

Hi,

Quoted 6 lines · Andreas Gruenbacher

With the locking the previous patch has introduced for each struct gfs2_quota_data object, sd_quota_mutex has become largely irrelevant. By waiting on the buffer head instead of waiting on the mutex in get_bh(), it becomes completely irrelevant and can be removed.

...

Quoted 10 lines

error = -EIO;
if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC))

fs/gfs2/quota.c +0 -8
-               goto fail_brelse;
-
-       qd->qd_bh = bh;
-       qd->qd_bh_qc = (struct gfs2_quota_change *)
-               (bh->b_data + sizeof(struct gfs2_meta_header) +
-                offset * sizeof(struct gfs2_quota_change));
-
-       mutex_unlock(&sdp->sd_quota_mutex);

fs/gfs2/quota.c static int bh_get(struct gfs2_quota_data *qd) · line 438

Quoted 10 lines
fs/gfs2/quota.c +8 -1
+               goto out;
 
-       return 0;
+       spin_lock(&qd->qd_lockref.lock);
+       if (qd->qd_bh == NULL) {
+               qd->qd_bh = bh;
+               qd->qd_bh_qc = (struct gfs2_quota_change *)
+                       (bh->b_data + sizeof(struct gfs2_meta_header) +
+                        offset * sizeof(struct gfs2_quota_change));
+               bh = NULL;

here you set bh to NULL.

fs/gfs2/quota.c static int bh_get(struct gfs2_quota_data *qd) · line 445

Quoted 8 lines
fs/gfs2/quota.c +5 -1
+       }
+       qd->qd_bh_count++;
+       spin_unlock(&qd->qd_lockref.lock);
+       error = 0;
 
-fail_brelse:
+out:
        brelse(bh);

It might be called with bh == NULL?

Quoted 15 lines
fs/gfs2/quota.c +2 -4
-fail:
-       qd->qd_bh_count--;
-       mutex_unlock(&sdp->sd_quota_mutex);
        return error;
 }
 
 static void bh_put(struct gfs2_quota_data *qd)
 {
        struct gfs2_sbd *sdp = qd->qd_sbd;
+       struct buffer_head *bh = NULL;
 
-       mutex_lock(&sdp->sd_quota_mutex);
+       spin_lock(&qd->qd_lockref.lock);
        gfs2_assert(sdp, qd->qd_bh_count);
        if (!--qd->qd_bh_count) {

fs/gfs2/quota.c static int bh_get(struct gfs2_quota_data *qd) · line 464

Quoted 10 lines
fs/gfs2/quota.c +5 -2
-               brelse(qd->qd_bh);
+               bh = qd->qd_bh;
                qd->qd_bh = NULL;
                qd->qd_bh_qc = NULL;
        }
-       mutex_unlock(&sdp->sd_quota_mutex);
+       spin_unlock(&qd->qd_lockref.lock);
+
+       if (bh)
+               brelse(bh);

Here you care about that bh is != NULL.

is breise() now NULL proofed?

- Alex

author maintainer
#13 lore raw

Re: [PATCH 11/13] gfs2: Get rid of some unnecessary quota locking

Quoted 39 lines · Alexander Aring

Hi,

Quoted 6 lines · Andreas Gruenbacher

With the locking the previous patch has introduced for each struct gfs2_quota_data object, sd_quota_mutex has become largely irrelevant. By waiting on the buffer head instead of waiting on the mutex in get_bh(), it becomes completely irrelevant and can be removed.

...

Quoted 10 lines

error = -EIO;
if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC))

fs/gfs2/quota.c +0 -8
-               goto fail_brelse;
-
-       qd->qd_bh = bh;
-       qd->qd_bh_qc = (struct gfs2_quota_change *)
-               (bh->b_data + sizeof(struct gfs2_meta_header) +
-                offset * sizeof(struct gfs2_quota_change));
-
-       mutex_unlock(&sdp->sd_quota_mutex);

fs/gfs2/quota.c static int bh_get(struct gfs2_quota_data *qd) · line 438

Quoted 10 lines
fs/gfs2/quota.c +8 -1
+               goto out;
 
-       return 0;
+       spin_lock(&qd->qd_lockref.lock);
+       if (qd->qd_bh == NULL) {
+               qd->qd_bh = bh;
+               qd->qd_bh_qc = (struct gfs2_quota_change *)
+                       (bh->b_data + sizeof(struct gfs2_meta_header) +
+                        offset * sizeof(struct gfs2_quota_change));
+               bh = NULL;

here you set bh to NULL.

Quoted 5 lines
fs/gfs2/quota.c +4 -0
+       }
+       qd->qd_bh_count++;
+       spin_unlock(&qd->qd_lockref.lock);
+       error = 0;
Quoted 5 lines

fs/gfs2/quota.c static int bh_get(struct gfs2_quota_data *qd) · line 445

Quoted 3 lines
fs/gfs2/quota.c +1 -1
-fail_brelse:
+out:
        brelse(bh);

It might be called with bh == NULL?

Yes, brelse() can handle that.

Quoted 28 lines
Quoted 15 lines
fs/gfs2/quota.c +2 -4
-fail:
-       qd->qd_bh_count--;
-       mutex_unlock(&sdp->sd_quota_mutex);
        return error;
 }
 
 static void bh_put(struct gfs2_quota_data *qd)
 {
        struct gfs2_sbd *sdp = qd->qd_sbd;
+       struct buffer_head *bh = NULL;
 
-       mutex_lock(&sdp->sd_quota_mutex);
+       spin_lock(&qd->qd_lockref.lock);
        gfs2_assert(sdp, qd->qd_bh_count);
        if (!--qd->qd_bh_count) {

fs/gfs2/quota.c static int bh_get(struct gfs2_quota_data *qd) · line 464

Quoted 10 lines
fs/gfs2/quota.c +5 -2
-               brelse(qd->qd_bh);
+               bh = qd->qd_bh;
                qd->qd_bh = NULL;
                qd->qd_bh_qc = NULL;
        }
-       mutex_unlock(&sdp->sd_quota_mutex);
+       spin_unlock(&qd->qd_lockref.lock);
+
+       if (bh)
+               brelse(bh);

Here you care about that bh is != NULL.

Quoted 1 line

is breise() now NULL proofed?

Yes, always has been. I'll remove that unnecessary NULL check.

Thanks,
Andreas

author maintainer
#14 lore raw

PATCH 12/13 gfs2: Be more careful with the quota sync generation

In reply to Andreas Gruenbacher

The quota sync generation is only ever updated under sd_quota_sync_mutex by gfs2_quota_sync(), but its current value is fetched ouside of that mutex, so use WRITE_ONCE() and READ_ONCE() when accessing it without holding that mutex.

Pass the current sync generation to do_sync() from its callers to ensure that we're not recording the wrong generation when the syncing is done. Also, make sure that qd->qd_sync_gen only ever moves forward.

In gfs2_quota_sync(), only write the new sync generation when we know that there are changes. This eliminates the need for function sd_changed(), which we will remove in the next commit.

---
 fs/gfs2/quota.c | 27 +++++++++++++++++++--------
 1 file changed, 19 insertions(+), 8 deletions(-)
fs/gfs2/quota.c +19 -8
index 2490117afb96..e27fceee9c63 100644
@@ -893,7 +893,8 @@ static int gfs2_adjust_quota(struct gfs2_sbd *sdp, loff_t loc,
 	return err;
 }
 
-static int do_sync(unsigned int num_qd, struct gfs2_quota_data **qda)
+static int do_sync(unsigned int num_qd, struct gfs2_quota_data **qda,
+		   u64 sync_gen)
 {
 	struct gfs2_sbd *sdp = (*qda)->qd_sbd;
 	struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
@@ -984,8 +985,13 @@ static int do_sync(unsigned int num_qd, struct gfs2_quota_data **qda)
 	gfs2_log_flush(ip->i_gl->gl_name.ln_sbd, ip->i_gl,
 		       GFS2_LOG_HEAD_FLUSH_NORMAL | GFS2_LFC_DO_SYNC);
 	if (!error) {
-		for (x = 0; x < num_qd; x++)
-			qda[x]->qd_sync_gen = sdp->sd_quota_sync_gen;
+		for (x = 0; x < num_qd; x++) {
+			qd = qda[x];
+			spin_lock(&qd->qd_lockref.lock);
+			if (qd->qd_sync_gen < sync_gen)
+				qd->qd_sync_gen = sync_gen;
+			spin_unlock(&qd->qd_lockref.lock);
+		}
 	}
 	return error;
 }
@@ -1179,7 +1185,9 @@ void gfs2_quota_unlock(struct gfs2_inode *ip)
 	}
 
 	if (count) {
-		do_sync(count, qda);
+		u64 sync_gen = READ_ONCE(sdp->sd_quota_sync_gen);
+
+		do_sync(count, qda, sync_gen);
 		for (x = 0; x < count; x++)
 			qd_unlock(qda[x]);
 	}
@@ -1325,6 +1333,7 @@ int gfs2_quota_sync(struct super_block *sb, int type)
 	struct gfs2_sbd *sdp = sb->s_fs_info;
 	struct gfs2_quota_data **qda;
 	unsigned int max_qd = PAGE_SIZE / sizeof(struct gfs2_holder);
+	u64 sync_gen;
 	int error = 0;
 
 	if (sb_rdonly(sdp->sd_vfs))
@@ -1337,7 +1346,7 @@ int gfs2_quota_sync(struct super_block *sb, int type)
 		return -ENOMEM;
 
 	mutex_lock(&sdp->sd_quota_sync_mutex);
-	sdp->sd_quota_sync_gen++;
+	sync_gen = sdp->sd_quota_sync_gen + 1;
 
 	do {
 		struct gfs2_quota_data *iter;
@@ -1346,7 +1355,7 @@ int gfs2_quota_sync(struct super_block *sb, int type)
 
 		spin_lock(&qd_lock);
 		list_for_each_entry(iter, &sdp->sd_quota_list, qd_list) {
-			if (qd_grab_sync(sdp, iter, sdp->sd_quota_sync_gen)) {
+			if (qd_grab_sync(sdp, iter, sync_gen)) {
 				qda[num_qd++] = iter;
 				if (num_qd == max_qd)
 					break;
@@ -1367,8 +1376,10 @@ int gfs2_quota_sync(struct super_block *sb, int type)
 			break;
 		}
 
-		if (!error)
-			error = do_sync(num_qd, qda);
+		if (!error) {
+			WRITE_ONCE(sdp->sd_quota_sync_gen, sync_gen);
+			error = do_sync(num_qd, qda, sync_gen);
+		}
 
 		for (x = 0; x < num_qd; x++)
 			qd_unlock(qda[x]);
Signature · 1 line
2.45.1
author maintainer
#15 lore raw

PATCH 13/13 gfs2: Revert "check for no eligible quota changes"

In reply to Andreas Gruenbacher

Since the previous commit, function gfs2_quota_sync() will not cause the sync generation to creep forward by one every time the function is called; this helps keep things a but more tidy. We also don't care that this function allocates a page of memory every time it is called, so no good reason for keeping qd_changed() anymore, which just duplicates qd_grab_sync().

This reverts commit 06aa6fd31a5f402b055e12ea53bb7b086359d3c8.

---
 fs/gfs2/quota.c | 20 --------------------
 1 file changed, 20 deletions(-)
fs/gfs2/quota.c +0 -20
index e27fceee9c63..9a8a30b5aba4 100644
@@ -1310,24 +1310,6 @@ void gfs2_quota_change(struct gfs2_inode *ip, s64 change,
 	}
 }
 
-static bool qd_changed(struct gfs2_sbd *sdp)
-{
-	struct gfs2_quota_data *qd;
-	bool changed = false;
-
-	spin_lock(&qd_lock);
-	list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) {
-		spin_lock(&qd->qd_lockref.lock);
-		changed = !test_bit(QDF_LOCKED, &qd->qd_flags) &&
-			  test_bit(QDF_CHANGE, &qd->qd_flags);
-		spin_unlock(&qd->qd_lockref.lock);
-		if (changed)
-			break;
-	}
-	spin_unlock(&qd_lock);
-	return changed;
-}
-
 int gfs2_quota_sync(struct super_block *sb, int type)
 {
 	struct gfs2_sbd *sdp = sb->s_fs_info;
@@ -1338,8 +1320,6 @@ int gfs2_quota_sync(struct super_block *sb, int type)
 
 	if (sb_rdonly(sdp->sd_vfs))
 		return 0;
-	if (!qd_changed(sdp))
-		return 0;
 
 	qda = kcalloc(max_qd, sizeof(struct gfs2_quota_data *), GFP_KERNEL);
 	if (!qda)
Signature · 1 line
2.45.1