Skip to contentSkip to conversation

ext4: Quota enabling error handling fixes

PATCH Applied Jan Kara 2 participants 4 messages Oct 7, 2021 – Dec 27, 2021 ext4 file system

lore
Share & tools
lore

Jan Kara

author reviewer
#0 lore raw

cover ext4: Quota enabling error handling fixes

Hello,

when enabling of quotas fails in the middle, we could be leaving quota subsystem in unexpected state or free inodes with locking classes set. Make sure to do proper cleanup in case quota setup fails.

Honza

author reviewer
#1 lore raw

PATCH 1/2 ext4: Make sure quota gets properly shutdown on error

When we hit an error when enabling quotas and setting inode flags, we do not properly shutdown quota subsystem despite returning error from Q_QUOTAON quotactl. This can lead to some odd situations like kernel using quota file while it is still writeable for userspace. Make sure we properly cleanup the quota subsystem in case of error.

---
 fs/ext4/super.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
fs/ext4/super.c +6 -4
index 88d5d274a868..fbe9cae63786 100644
@@ -6267,10 +6267,7 @@ static int ext4_quota_on(struct super_block *sb, int type, int format_id,
 
 	lockdep_set_quota_inode(path->dentry->d_inode, I_DATA_SEM_QUOTA);
 	err = dquot_quota_on(sb, type, format_id, path);
-	if (err) {
-		lockdep_set_quota_inode(path->dentry->d_inode,
-					     I_DATA_SEM_NORMAL);
-	} else {
+	if (!err) {
 		struct inode *inode = d_inode(path->dentry);
 		handle_t *handle;
 
@@ -6290,7 +6287,12 @@ static int ext4_quota_on(struct super_block *sb, int type, int format_id,
 		ext4_journal_stop(handle);
 	unlock_inode:
 		inode_unlock(inode);
+		if (err)
+			dquot_quota_off(sb, type);
 	}
+	if (err)
+		lockdep_set_quota_inode(path->dentry->d_inode,
+					     I_DATA_SEM_NORMAL);
 	return err;
 }
 
Signature · 1 line
2.26.2
author reviewer
#2 lore raw

PATCH 2/2 ext4: Make sure to reset inode lockdep class when quota enabling fails

In reply to Jan Kara

When we succeed in enabling some quota type but fail to enable another one with quota feature, we correctly disable all enabled quota types. However we forget to reset i_data_sem lockdep class. When the inode gets freed and reused, it will inherit this lockdep class (i_data_sem is initialized only when a slab is created) and thus eventually lockdep barfs about possible deadlocks.

Reported-and-tested-by [email protected]
---
 fs/ext4/super.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
fs/ext4/super.c +12 -1
index fbe9cae63786..70b5fcbd351a 100644
@@ -6355,8 +6355,19 @@ int ext4_enable_quotas(struct super_block *sb)
 					"Failed to enable quota tracking "
 					"(type=%d, err=%d). Please run "
 					"e2fsck to fix.", type, err);
-				for (type--; type >= 0; type--)
+				for (type--; type >= 0; type--) {
+					struct inode *inode;
+
+					inode = sb_dqopt(sb)->files[type];
+					if (inode)
+						inode = igrab(inode);
 					dquot_quota_off(sb, type);
+					if (inode) {
+						lockdep_set_quota_inode(inode,
+							I_DATA_SEM_NORMAL);
+						iput(inode);
+					}
+				}
 
 				return err;
 			}
Signature · 1 line
2.26.2
maintainer Applied
#3 lore raw
In reply to Jan Kara
Quoted 6 lines · Jan Kara

when enabling of quotas fails in the middle, we could be leaving quota subsystem in unexpected state or free inodes with locking classes set. Make sure to do proper cleanup in case quota setup fails.

Honza

Applied, thanks!

[1/2] ext4: Make sure quota gets properly shutdown on error

      commit: 298503cc5015e0512294788127a853a730d47fb1
[2/2] ext4: Make sure to reset inode lockdep class when quota enabling fails
      commit: c5725ba32f98d128d5d83cab6d0b24ceb4ecf731

Best regards,

Signature · 1 line
Theodore Ts'o <[email protected]>